This repository was archived by the owner on Oct 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathphysicaldisplay.cpp
More file actions
676 lines (561 loc) · 18.8 KB
/
Copy pathphysicaldisplay.cpp
File metadata and controls
676 lines (561 loc) · 18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/*
// Copyright (c) 2016 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
#include "physicaldisplay.h"
#include <cmath>
#include <hwcdefs.h>
#include <hwclayer.h>
#include <hwctrace.h>
#include <algorithm>
#include <sstream>
#include <string>
#include "displayplanemanager.h"
#include "displayqueue.h"
#include "hwcutils.h"
#include "wsi_utils.h"
namespace hwcomposer {
PhysicalDisplay::PhysicalDisplay(uint32_t gpu_fd, uint32_t pipe_id)
: pipe_(pipe_id),
width_(0),
height_(0),
custom_resolution_(false),
gpu_fd_(gpu_fd),
power_mode_(kOn) {
}
PhysicalDisplay::~PhysicalDisplay() {
}
bool PhysicalDisplay::Initialize(NativeBufferHandler *buffer_handler) {
display_queue_.reset(new DisplayQueue(gpu_fd_, false, buffer_handler, this));
InitializeDisplay();
return true;
}
const NativeBufferHandler *PhysicalDisplay::GetNativeBufferHandler() const {
if (display_queue_) {
return display_queue_->GetNativeBufferHandler();
}
return NULL;
}
void PhysicalDisplay::MarkForDisconnect() {
SPIN_LOCK(modeset_lock_);
IHOTPLUGEVENTTRACE("PhysicalDisplay::MarkForDisconnect recieved.");
connection_state_ |= kDisconnectionInProgress;
display_state_ |= kRefreshClonedDisplays;
SPIN_UNLOCK(modeset_lock_);
}
void PhysicalDisplay::NotifyClientOfConnectedState() {
SPIN_LOCK(modeset_lock_);
bool refresh_needed = false;
if (hotplug_callback_ && (connection_state_ & kConnected) &&
(display_state_ & kNotifyClient)) {
IHOTPLUGEVENTTRACE(
"PhysicalDisplay Sent Hotplug even call back with connected value set "
"to true. %p hotplugdisplayid: %d \n",
this, hot_plug_display_id_);
hotplug_callback_->Callback(hot_plug_display_id_, true);
display_state_ &= ~kNotifyClient;
#ifdef ENABLE_ANDROID_WA
if (ordered_display_id_ == 0) {
refresh_needed = true;
}
#endif
}
SPIN_UNLOCK(modeset_lock_);
if (refresh_needed) {
if (!display_queue_->IsIgnoreUpdates()) {
display_queue_->ForceRefresh();
}
}
}
void PhysicalDisplay::NotifyClientOfDisConnectedState() {
SPIN_LOCK(modeset_lock_);
if (hotplug_callback_ && !(connection_state_ & kConnected) &&
(display_state_ & kNotifyClient)) {
IHOTPLUGEVENTTRACE(
"PhysicalDisplay Sent Hotplug even call back with connected value set "
"to false. %p hotplugdisplayid: %d \n",
this, hot_plug_display_id_);
hotplug_callback_->Callback(hot_plug_display_id_, false);
display_state_ &= ~kNotifyClient;
}
SPIN_UNLOCK(modeset_lock_);
}
void PhysicalDisplay::DisConnect() {
SPIN_LOCK(modeset_lock_);
connection_state_ &= ~kDisconnectionInProgress;
if (!(connection_state_ & kConnected)) {
SPIN_UNLOCK(modeset_lock_);
return;
}
IHOTPLUGEVENTTRACE(
"PhysicalDisplay DisConnect called for Display: %p hotplugdisplayid: %d "
"\n",
this, hot_plug_display_id_);
display_state_ |= kNotifyClient;
if (power_mode_ != kOff) {
display_queue_->SetPowerMode(kOff);
}
connection_state_ &= ~kConnected;
display_state_ &= ~kUpdateDisplay;
SPIN_UNLOCK(modeset_lock_);
}
void PhysicalDisplay::Connect() {
SPIN_LOCK(modeset_lock_);
connection_state_ &= ~kDisconnectionInProgress;
SPIN_UNLOCK(modeset_lock_);
if (source_display_) {
// Current display is a cloned display, set the source_display_'s
// k_RefreshClonedDisplays flag. This makes clone parent have a
// chance to update it's cloned display list
PhysicalDisplay *p_clone_parent = (PhysicalDisplay *)source_display_;
SPIN_LOCK(p_clone_parent->modeset_lock_);
p_clone_parent->display_state_ |= kRefreshClonedDisplays;
SPIN_UNLOCK(p_clone_parent->modeset_lock_);
}
SPIN_LOCK(modeset_lock_);
if (connection_state_ & kConnected) {
IHOTPLUGEVENTTRACE(
"PhysicalDisplay::Connect connected already, return with power mode "
"update.");
UpdatePowerMode();
SPIN_UNLOCK(modeset_lock_);
return;
}
connection_state_ |= kConnected;
display_state_ &= ~kInitialized;
display_state_ |= kNotifyClient;
IHOTPLUGEVENTTRACE("PhysicalDisplay::Connect recieved. %p \n", this);
if (!display_queue_->Initialize(pipe_, width_, height_, this)) {
ETRACE("Failed to initialize Display Queue.");
} else {
display_state_ |= kInitialized;
}
if (display_state_ & kUpdateConfig) {
display_state_ &= ~kUpdateConfig;
display_queue_->DisplayConfigurationChanged();
UpdateDisplayConfig();
}
UpdatePowerMode();
SPIN_UNLOCK(modeset_lock_);
}
bool PhysicalDisplay::IsConnected() const {
if (connection_state_ & kDisconnectionInProgress)
return false;
return connection_state_ & kConnected;
}
uint32_t PhysicalDisplay::PowerMode() const {
return power_mode_;
}
int PhysicalDisplay::GetDisplayPipe() {
return pipe_;
}
bool PhysicalDisplay::EnableDRMCommit(bool enable) {
display_queue_->ForceIgnoreUpdates(!enable);
if (enable)
return !display_queue_->IsIgnoreUpdates();
else
return display_queue_->IsIgnoreUpdates();
}
bool PhysicalDisplay::SetActiveConfig(uint32_t config) {
// update the activeConfig
IHOTPLUGEVENTTRACE(
"SetActiveConfig: New config to be used %d pipe: %p display: %p", config,
pipe_, this);
config_ = config;
display_state_ |= kNeedsModeset;
if (connection_state_ & kConnected) {
display_queue_->DisplayConfigurationChanged();
UpdateDisplayConfig();
} else {
display_state_ |= kUpdateConfig;
}
return true;
}
bool PhysicalDisplay::GetActiveConfig(uint32_t *config) {
if (!config)
return false;
IHOTPLUGEVENTTRACE(
"GetActiveConfig: Current config being used Config: %d pipe: %d display: "
"%p",
config_, pipe_, this);
*config = config_;
return true;
}
bool PhysicalDisplay::SetPowerMode(uint32_t power_mode) {
#ifndef DISABLE_HOTPLUG_NOTIFICATION
ScopedSpinLock lock(modeset_lock_);
#endif
if (power_mode_ == power_mode) {
return true;
}
power_mode_ = power_mode;
if (!(connection_state_ & kConnected)) {
IHOTPLUGEVENTTRACE(
"PhysicalDisplay is not connected, postponing power mode update.");
display_state_ |= kPendingPowerMode;
return true;
} else if (connection_state_ & kDisconnectionInProgress) {
IHOTPLUGEVENTTRACE(
"PhysicalDisplay diconnection in progress, postponing power mode "
"update.");
// Don't update power mode in case disconnect is in
// progress.
display_state_ |= kPendingPowerMode;
return true;
}
return UpdatePowerMode();
}
bool PhysicalDisplay::UpdatePowerMode() {
display_state_ &= ~kPendingPowerMode;
if (power_mode_ == kOn) {
display_state_ |= kNeedsModeset;
display_state_ |= kUpdateDisplay;
IHOTPLUGEVENTTRACE(
"UpdatePowerMode: Powering on Display: pipe: %d display: "
"%p",
pipe_, this);
PowerOn();
} else {
IHOTPLUGEVENTTRACE(
"UpdatePowerMode: Power mode is not kOn: pipe: %d display: "
"%p",
pipe_, this);
display_state_ &= ~kUpdateDisplay;
}
if (!(display_state_ & kInitialized))
return true;
return display_queue_->SetPowerMode(power_mode_);
}
bool PhysicalDisplay::Present(std::vector<HwcLayer *> &source_layers,
int32_t *retire_fence,
PixelUploaderCallback *call_back,
bool handle_constraints) {
CTRACE();
SPIN_LOCK(modeset_lock_);
bool handle_hotplug_notifications = false;
if (display_state_ & kHandlePendingHotPlugNotifications) {
display_state_ &= ~kHandlePendingHotPlugNotifications;
handle_hotplug_notifications = true;
}
if (!(display_state_ & kUpdateDisplay)) {
bool success = true;
if (power_mode_ != kDozeSuspend) {
ETRACE("Trying to update an Disconnected Display.%p \n", this);
}
SPIN_UNLOCK(modeset_lock_);
if (handle_hotplug_notifications) {
NotifyClientsOfDisplayChangeStatus();
}
return success;
}
if (source_display_) {
ETRACE("Trying to update display independently when in cloned mode.%p \n",
this);
}
if (display_state_ & kRefreshClonedDisplays) {
RefreshClones();
}
SPIN_UNLOCK(modeset_lock_);
if (handle_hotplug_notifications) {
NotifyClientsOfDisplayChangeStatus();
IHOTPLUGEVENTTRACE("Handle_hoplug_notifications done. %p \n", this);
}
bool ignore_clone_update = false;
bool success = display_queue_->QueueUpdate(source_layers, retire_fence,
&ignore_clone_update, call_back,
handle_constraints);
if (success && !clones_.empty() && !ignore_clone_update) {
HandleClonedDisplays(this);
}
size_t size = source_layers.size();
for (size_t layer_index = 0; layer_index < size; layer_index++) {
HwcLayer *layer = source_layers.at(layer_index);
if (!layer->IsVisible())
continue;
layer->Validate();
}
return success;
}
bool PhysicalDisplay::PresentClone(NativeDisplay *display) {
CTRACE();
SPIN_LOCK(modeset_lock_);
if (display_state_ & kRefreshClonedDisplays) {
RefreshClones();
}
SPIN_UNLOCK(modeset_lock_);
display_queue_->PresentClonedCommit(
static_cast<PhysicalDisplay *>(display)->display_queue_.get());
HandleClonedDisplays(display);
return true;
}
void PhysicalDisplay::HandleClonedDisplays(NativeDisplay *display) {
if (clones_.empty())
return;
for (auto clone_display : clones_) {
clone_display->PresentClone(display);
}
}
int PhysicalDisplay::RegisterVsyncCallback(
std::shared_ptr<VsyncCallback> callback, uint32_t display_id) {
return display_queue_->RegisterVsyncCallback(callback, display_id);
}
int PhysicalDisplay::RegisterVsyncPeriodCallback(
std::shared_ptr<VsyncPeriodCallback> callback, uint32_t display_id) {
return display_queue_->RegisterVsyncPeriodCallback(callback, display_id);
}
void PhysicalDisplay::RegisterRefreshCallback(
std::shared_ptr<RefreshCallback> callback, uint32_t display_id) {
display_queue_->RegisterRefreshCallback(callback, display_id);
}
void PhysicalDisplay::RegisterHotPlugCallback(
std::shared_ptr<HotPlugCallback> callback, uint32_t display_id) {
SPIN_LOCK(modeset_lock_);
hot_plug_display_id_ = display_id;
hotplug_callback_ = callback;
#ifndef ENABLE_ANDROID_WA
bool connected = connection_state_ & kConnected;
#endif
SPIN_UNLOCK(modeset_lock_);
#ifdef ENABLE_ANDROID_WA
if (hotplug_callback_ && ordered_display_id_ == 0) {
display_state_ &= ~kNotifyClient;
display_state_ |= kHandlePendingHotPlugNotifications;
IHOTPLUGEVENTTRACE("RegisterHotPlugCallback: pipe: %d display: %p", pipe_,
this);
hotplug_callback_->Callback(hot_plug_display_id_, true);
}
#else
if (hotplug_callback_) {
if (connected) {
hotplug_callback_->Callback(hot_plug_display_id_, true);
} else {
hotplug_callback_->Callback(hot_plug_display_id_, false);
}
}
#endif
}
void PhysicalDisplay::VSyncControl(bool enabled) {
display_queue_->VSyncControl(enabled);
}
bool PhysicalDisplay::CheckPlaneFormat(uint32_t format) {
return display_queue_->CheckPlaneFormat(format);
}
void PhysicalDisplay::SetGamma(float red, float green, float blue) {
display_queue_->SetGamma(red, green, blue);
}
void PhysicalDisplay::SetColorTransform(const float *matrix,
HWCColorTransform hint) {
display_queue_->SetColorTransform(matrix, hint);
}
void PhysicalDisplay::SetContrast(uint32_t red, uint32_t green, uint32_t blue) {
display_queue_->SetContrast(red, green, blue);
}
void PhysicalDisplay::SetBrightness(uint32_t red, uint32_t green,
uint32_t blue) {
display_queue_->SetBrightness(red, green, blue);
}
void PhysicalDisplay::SetDisableExplicitSync(bool disable_explicit_sync) {
display_queue_->SetDisableExplicitSync(disable_explicit_sync);
}
void PhysicalDisplay::SetVideoScalingMode(uint32_t mode) {
display_queue_->SetVideoScalingMode(mode);
}
void PhysicalDisplay::SetVideoColor(HWCColorControl color, float value) {
display_queue_->SetVideoColor(color, value);
}
void PhysicalDisplay::GetVideoColor(HWCColorControl color, float *value,
float *start, float *end) {
display_queue_->GetVideoColor(color, value, start, end);
}
void PhysicalDisplay::RestoreVideoDefaultColor(HWCColorControl color) {
display_queue_->RestoreVideoDefaultColor(color);
}
void PhysicalDisplay::SetVideoDeinterlace(HWCDeinterlaceFlag flag,
HWCDeinterlaceControl mode) {
display_queue_->SetVideoDeinterlace(flag, mode);
}
void PhysicalDisplay::RestoreVideoDefaultDeinterlace() {
display_queue_->RestoreVideoDefaultDeinterlace();
}
void PhysicalDisplay::SetCanvasColor(uint16_t bpc, uint16_t red, uint16_t green,
uint16_t blue, uint16_t alpha) {
display_queue_->SetCanvasColor(bpc, red, green, blue, alpha);
}
void PhysicalDisplay::SetPAVPSessionStatus(bool enabled,
uint32_t pavp_session_id,
uint32_t pavp_instance_id) {
display_queue_->SetPAVPSessionStatus(enabled, pavp_session_id,
pavp_instance_id);
}
bool PhysicalDisplay::PopulatePlanes(
std::vector<std::unique_ptr<DisplayPlane>> & /*overlay_planes*/) {
ETRACE("PopulatePlanes unimplemented in PhysicalDisplay.");
return false;
}
bool PhysicalDisplay::TestCommit(
const DisplayPlaneStateList & /*commit_planes*/) const {
ETRACE("TestCommit unimplemented in PhysicalDisplay.");
return false;
}
void PhysicalDisplay::UpdateScalingRatio(uint32_t primary_width,
uint32_t primary_height,
uint32_t display_width,
uint32_t display_height) {
display_queue_->UpdateScalingRatio(primary_width, primary_height,
display_width, display_height);
}
void PhysicalDisplay::CloneDisplay(NativeDisplay *source_display) {
if (source_display_) {
source_display_->DisOwnPresentation(this);
display_queue_->SetCloneMode(false);
source_display_ = NULL;
}
source_display_ = source_display;
if (source_display_) {
source_display_->OwnPresentation(this);
display_queue_->SetCloneMode(true);
}
}
void PhysicalDisplay::OwnPresentation(NativeDisplay *clone) {
cloned_displays_.emplace_back(clone);
display_state_ |= kRefreshClonedDisplays;
}
void PhysicalDisplay::DisOwnPresentation(NativeDisplay *clone) {
if (cloned_displays_.empty())
return;
std::vector<NativeDisplay *> displays;
size_t size = cloned_displays_.size();
for (size_t i = 0; i < size; i++) {
NativeDisplay *display = cloned_displays_.at(i);
if (display == clone)
continue;
displays.emplace_back(display);
}
cloned_displays_.swap(displays);
display_state_ |= kRefreshClonedDisplays;
}
void PhysicalDisplay::SetDisplayOrder(uint32_t display_order) {
ordered_display_id_ = display_order;
}
void PhysicalDisplay::RotateDisplay(HWCRotation rotation) {
display_queue_->RotateDisplay(rotation);
}
void PhysicalDisplay::RefreshClones() {
display_state_ &= ~kRefreshClonedDisplays;
std::vector<NativeDisplay *>().swap(clones_);
if (cloned_displays_.empty())
return;
size_t size = cloned_displays_.size();
for (size_t i = 0; i < size; i++) {
NativeDisplay *display = cloned_displays_.at(i);
if (!display->IsConnected())
continue;
clones_.emplace_back(display);
}
uint32_t primary_width = Width();
uint32_t primary_height = Height();
for (auto display : clones_) {
uint32_t display_width = display->Width();
uint32_t display_height = display->Height();
if ((primary_width == display_width) && (primary_height == display_height))
return;
display->UpdateScalingRatio(primary_width, primary_height, display_width,
display_height);
}
}
bool PhysicalDisplay::GetDisplayAttribute(uint32_t /*config*/,
HWCDisplayAttribute attribute,
int32_t *value) {
// We always get the values from preferred mode config.
switch (attribute) {
case HWCDisplayAttribute::kWidth:
*value = 1920;
break;
case HWCDisplayAttribute::kHeight:
*value = 1080;
break;
case HWCDisplayAttribute::kRefreshRate:
// in nanoseconds
*value = 16666666;
break;
case HWCDisplayAttribute::kDpiX:
// Dots per 1000 inches
*value = 1;
break;
case HWCDisplayAttribute::kDpiY:
// Dots per 1000 inches
*value = 1;
break;
default:
*value = -1;
return false;
}
return true;
}
/* Setting custom resolution instead of preferred as fetched from display */
bool PhysicalDisplay::SetCustomResolution(const HwcRect<int32_t> &rect) {
if ((rect.right - rect.left) && (rect.bottom - rect.top)) {
// Custom rectangle with valid width and height
rect_.left = rect.left;
rect_.top = rect.top;
rect_.right = rect.right;
rect_.bottom = rect.bottom;
custom_resolution_ = true;
IHOTPLUGEVENTTRACE(
"SetCustomResolution: custom width %d, height %d, bool %d",
rect_.right - rect_.left, rect_.bottom - rect_.top, custom_resolution_);
return true;
} else {
// Default display rectangle
custom_resolution_ = false;
return false;
}
}
bool PhysicalDisplay::GetDisplayConfigs(uint32_t *num_configs,
uint32_t *configs) {
if (!num_configs)
return false;
*num_configs = 1;
if (configs) {
configs[0] = DEFAULT_CONFIG_ID;
}
connection_state_ |= kFakeConnected;
return true;
}
bool PhysicalDisplay::GetDisplayName(uint32_t *size, char *name) {
std::ostringstream stream;
stream << "Headless";
std::string string = stream.str();
size_t length = string.length();
if (!name) {
*size = length;
return true;
}
*size = std::min<uint32_t>(static_cast<uint32_t>(length + 1), *size);
strncpy(name, string.c_str(), *size);
return true;
}
int PhysicalDisplay::GetTotalOverlays() const {
if (display_queue_)
return display_queue_->GetTotalOverlays();
else
return 0;
}
bool PhysicalDisplay::IsBypassClientCTM() const {
return bypassClientCTM_;
}
void PhysicalDisplay::GetDisplayCapabilities(uint32_t *numCapabilities,
uint32_t *capabilities) {
}
} // namespace hwcomposer