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 pathiahwc2.cpp
More file actions
1782 lines (1556 loc) · 61.7 KB
/
iahwc2.cpp
File metadata and controls
1782 lines (1556 loc) · 61.7 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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2016 The Android Open Source Project
*
* 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.
*/
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
#include "iahwc2.h"
#include "utils_android.h"
#include <inttypes.h>
#include <android/log.h>
#include <cutils/properties.h>
#include <hardware/hardware.h>
#include <hardware/hwcomposer2.h>
#include <system/graphics.h>
#include <gpudevice.h>
#include <hwcdefs.h>
#include <nativedisplay.h>
#include "mosaicdisplay.h"
#include <algorithm>
#include <memory>
#include <string>
#include <vector>
namespace android {
class IAVsyncCallback : public hwcomposer::VsyncCallback {
public:
IAVsyncCallback(hwc2_callback_data_t data, hwc2_function_pointer_t hook)
: data_(data), hook_(hook) {
}
void Callback(uint32_t display, int64_t timestamp) {
if (hook_ != NULL) {
auto hook = reinterpret_cast<HWC2_PFN_VSYNC>(hook_);
hook(data_, display, timestamp);
}
}
private:
hwc2_callback_data_t data_;
hwc2_function_pointer_t hook_;
};
class IAVsyncPeriodCallback : public hwcomposer::VsyncPeriodCallback {
public:
IAVsyncPeriodCallback(hwc2_callback_data_t data, hwc2_function_pointer_t hook)
: data_(data), hook_(hook) {
}
void Callback(uint32_t display, int64_t timestamp, uint32_t vsyncPeriodNanos) {
if (hook_ != NULL) {
auto hook = reinterpret_cast<HWC2_PFN_VSYNC_2_4>(hook_);
hook(data_, display, timestamp, vsyncPeriodNanos);
}
}
private:
hwc2_callback_data_t data_;
hwc2_function_pointer_t hook_;
};
class IARefreshCallback : public hwcomposer::RefreshCallback {
public:
IARefreshCallback(hwc2_callback_data_t data, hwc2_function_pointer_t hook)
: data_(data), hook_(hook) {
}
void Callback(uint32_t display) {
if (hook_ != NULL) {
auto hook = reinterpret_cast<HWC2_PFN_REFRESH>(hook_);
hook(data_, display);
}
}
private:
hwc2_callback_data_t data_;
hwc2_function_pointer_t hook_;
};
class IAHotPlugEventCallback : public hwcomposer::HotPlugCallback {
public:
IAHotPlugEventCallback(hwc2_callback_data_t data,
hwc2_function_pointer_t hook,
IAHWC2::HwcDisplay *display)
: data_(data), hook_(hook), display_(display) {
}
void Callback(uint32_t display, bool connected) {
if (display == 0) {
#ifdef ENABLE_PANORAMA
char dispname[128];
uint32_t size = sizeof(dispname);
memset(dispname, 0, sizeof(dispname));
display_->GetDisplayName(&size, dispname);
if (strstr(dispname, "Panorama")) {
// Allow the hotplug events to be emitted.
} else {
if (notified_) {
return;
}
if (connected) {
notified_ = true;
}
}
#else
// SF expects primary display to be always
// connected. Let's notify it first time and ignore
// any followup status changes.
if (notified_) {
return;
}
if (connected)
notified_ = true;
#endif
}
auto hook = reinterpret_cast<HWC2_PFN_HOTPLUG>(hook_);
int32_t status = static_cast<int32_t>(HWC2::Connection::Connected);
if (!connected) {
status = static_cast<int32_t>(HWC2::Connection::Disconnected);
} else {
hwc2_config_t default_config;
uint32_t num_configs = 1;
display_->GetDisplayConfigs(&num_configs, &default_config);
display_->SetActiveConfig(default_config);
}
IHOTPLUGEVENTTRACE(
"IAHotPlugEventCallback called displayid: %d status: %d \n", display,
status);
if (hook)
hook(data_, display, status);
// FIXME: SurfaceFlinger doesn't seem to reset layers correctly
// when display is connected/disconnected. We force it here.
// Remove this workaround once fixed correctly in SurfaceFlinger.
if (!connected && display > 0) {
display_->FreeAllLayers();
}
}
private:
hwc2_callback_data_t data_;
hwc2_function_pointer_t hook_;
IAHWC2::HwcDisplay *display_;
bool notified_ = false;
};
IAHWC2::IAHWC2() {
common.tag = HARDWARE_DEVICE_TAG;
common.version = HWC_DEVICE_API_VERSION_2_0;
common.close = HookDevClose;
getCapabilities = HookDevGetCapabilities;
getFunction = HookDevGetFunction;
}
HWC2::Error IAHWC2::Init() {
char value[PROPERTY_VALUE_MAX];
property_get("board.disable.explicit.sync", value, "0");
disable_explicit_sync_ = atoi(value);
/* Build wants to explicitly disable sync. */
#ifdef DISABLE_EXPLICIT_SYNC
disable_explicit_sync_ = true;
#endif
if (disable_explicit_sync_)
ALOGI("EXPLICIT SYNC support is disabled");
else
ALOGI("EXPLICIT SYNC support is enabled");
property_get("board.hwc.scaling.mode", value, "2");
scaling_mode_ = atoi(value);
switch (scaling_mode_) {
case 1:
ALOGI("HWC Scaling Mode Fast");
break;
case 2:
ALOGI("HWC Scaling Mode High Quality");
break;
default:
ALOGI("Unsupport HWC Scaling Mode");
break;
}
if (!device_.Initialize()) {
ALOGE("Can't initialize drm object.");
return HWC2::Error::NoResources;
}
const std::vector<NativeDisplay *> &displays = device_.GetAllDisplays();
NativeDisplay *primary_display = displays.at(0);
uint32_t external_display_id = 1;
primary_display_.Init(primary_display, 0, disable_explicit_sync_,
scaling_mode_);
size_t size = displays.size();
for (size_t i = 0; i < size; ++i) {
hwcomposer::NativeDisplay *display = displays.at(i);
if (display == primary_display)
continue;
std::unique_ptr<HwcDisplay> temp(new HwcDisplay());
temp->Init(display, external_display_id, disable_explicit_sync_,
scaling_mode_);
extended_displays_.emplace_back(std::move(temp));
external_display_id++;
// Let's not confuse things with Virtual Display.
if (external_display_id == HWC_DISPLAY_VIRTUAL)
external_display_id = HWC_DISPLAY_VIRTUAL + 1;
}
// Start the hwc service
hwcService_.Start(*this);
return HWC2::Error::None;
}
template <typename... Args>
static inline HWC2::Error unsupported(char const *func, Args... /*args*/) {
ALOGV("Unsupported function: %s", func);
return HWC2::Error::Unsupported;
}
static inline void supported(char const *func) {
ALOGV("supported function: %s", func);
}
HWC2::Error IAHWC2::CreateVirtualDisplay(uint32_t width, uint32_t height,
int32_t *format,
hwc2_display_t *display) {
if (NULL == display) {
ALOGE("Pointer of display is NULL");
return HWC2::Error::BadDisplay;
}
if (NULL == format) {
ALOGE("Pointer of format is NULL");
return HWC2::Error::BadParameter;
}
uint32_t free_display_index = 0;
spin_lock_.lock();
for (uint32_t disp_index = 0; disp_index < virtual_display_index_;
disp_index++) {
auto it = virtual_displays_.find(disp_index);
if (it == virtual_displays_.end()) {
free_display_index = disp_index;
break;
}
}
if (!free_display_index) {
free_display_index = virtual_display_index_;
virtual_display_index_++;
}
*display =
(hwc2_display_t)(free_display_index + HWC_DISPLAY_VIRTUAL + VDS_OFFSET);
std::unique_ptr<HwcDisplay> temp(new HwcDisplay());
temp->InitVirtualDisplay(device_.CreateVirtualDisplay(free_display_index),
width, height, free_display_index,
disable_explicit_sync_);
virtual_displays_.emplace(free_display_index, std::move(temp));
spin_lock_.unlock();
if (*format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
// fallback to RGBA_8888, align with framework requirement
*format = HAL_PIXEL_FORMAT_RGBA_8888;
}
return HWC2::Error::None;
}
HWC2::Error IAHWC2::DestroyVirtualDisplay(hwc2_display_t display) {
if (display < (hwc2_display_t)(HWC_DISPLAY_VIRTUAL + VDS_OFFSET)) {
ALOGE("Not Virtual Display Type in DestroyVirtualDisplay");
return HWC2::Error::BadDisplay;
}
if (~((uint32_t)display) == 0) {
return HWC2::Error::BadDisplay;
}
uint32_t display_id = (uint32_t)display - HWC_DISPLAY_VIRTUAL - VDS_OFFSET;
spin_lock_.lock();
auto it = virtual_displays_.find(display_id);
if (it != virtual_displays_.end()) {
device_.DestroyVirtualDisplay(display - HWC_DISPLAY_VIRTUAL - VDS_OFFSET);
virtual_displays_.at(display - HWC_DISPLAY_VIRTUAL - VDS_OFFSET)
.reset(nullptr);
virtual_displays_.erase(display - HWC_DISPLAY_VIRTUAL - VDS_OFFSET);
}
spin_lock_.unlock();
return HWC2::Error::None;
}
void IAHWC2::Dump(uint32_t *size, char *buffer) {
// TODO: Implement dump
unsupported(__func__, size, buffer);
}
uint32_t IAHWC2::GetMaxVirtualDisplayCount() {
return 10;
}
HWC2::Error IAHWC2::RegisterCallback(int32_t descriptor,
hwc2_callback_data_t data,
hwc2_function_pointer_t function) {
supported(__func__);
auto callback = static_cast<HWC2::Callback>(descriptor);
size_t size = extended_displays_.size();
HWC2::Error error = HWC2::Error::None;
switch (callback) {
case HWC2::Callback::Hotplug: {
primary_display_.RegisterHotPlugCallback(data, function);
for (size_t i = 0; i < size; ++i) {
IAHWC2::HwcDisplay *display = extended_displays_.at(i).get();
display->RegisterHotPlugCallback(data, function);
}
break;
}
case HWC2::Callback::Vsync: {
primary_display_.RegisterVsyncCallback(data, function);
for (size_t i = 0; i < size; ++i) {
IAHWC2::HwcDisplay *display = extended_displays_.at(i).get();
display->RegisterVsyncCallback(data, function);
}
break;
}
case HWC2::Callback::Vsync_2_4: {
primary_display_.RegisterVsyncPeriodCallback(data, function);
for (size_t i = 0; i < size; ++i) {
IAHWC2::HwcDisplay *display = extended_displays_.at(i).get();
display->RegisterVsyncPeriodCallback(data, function);
}
break;
}
case HWC2::Callback::Refresh: {
primary_display_.RegisterRefreshCallback(data, function);
for (size_t i = 0; i < size; ++i) {
IAHWC2::HwcDisplay *display = extended_displays_.at(i).get();
display->RegisterRefreshCallback(data, function);
}
break;
}
default:
error = HWC2::Error::BadParameter;
break;
}
return error;
}
IAHWC2::HwcDisplay::HwcDisplay()
: display_(nullptr),
handle_(0),
type_(HWC2::DisplayType::Invalid),
frame_no_(0),
check_validate_display_(false),
disable_explicit_sync_(false),
enable_nested_display_compose_(false),
scaling_mode_(0) {
supported(__func__);
}
// This function will be called only for Virtual Display Init
HWC2::Error IAHWC2::HwcDisplay::InitVirtualDisplay(
hwcomposer::NativeDisplay *display, uint32_t width, uint32_t height,
uint32_t display_index, bool disable_explicit_sync) {
supported(__func__);
display_ = display;
type_ = HWC2::DisplayType::Virtual;
handle_ = display_index + HWC_DISPLAY_VIRTUAL + VDS_OFFSET;
display_->InitVirtualDisplay(width, height);
disable_explicit_sync_ = disable_explicit_sync;
display_->SetDisableExplicitSync(disable_explicit_sync_);
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::Init(hwcomposer::NativeDisplay *display,
int display_index,
bool disable_explicit_sync,
uint32_t scaling_mode) {
supported(__func__);
display_ = display;
type_ = HWC2::DisplayType::Physical;
handle_ = display_index;
disable_explicit_sync_ = disable_explicit_sync;
scaling_mode_ = scaling_mode;
display_->SetDisableExplicitSync(disable_explicit_sync_);
display_->SetVideoScalingMode(scaling_mode_);
if (!display_->IsConnected()) {
return HWC2::Error::None;
}
// Fetch the number of modes from the display
uint32_t num_configs;
HWC2::Error err = GetDisplayConfigs(&num_configs, NULL);
if (err != HWC2::Error::None || !num_configs)
return err;
// Grab the first mode, we'll choose this as the active mode
hwc2_config_t default_config;
num_configs = 1;
err = GetDisplayConfigs(&num_configs, &default_config);
if (err != HWC2::Error::None)
return err;
display_->InitializeLayerHashGenerator(32);
return SetActiveConfig(default_config);
}
HWC2::Error IAHWC2::HwcDisplay::RegisterVsyncCallback(
hwc2_callback_data_t data, hwc2_function_pointer_t func) {
supported(__func__);
auto callback = std::make_shared<IAVsyncCallback>(data, func);
int ret = display_->RegisterVsyncCallback(std::move(callback),
static_cast<int>(handle_));
if (ret) {
ALOGE("Failed to register callback d=%" PRIu64 " ret=%d", handle_, ret);
return HWC2::Error::BadDisplay;
}
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::RegisterVsyncPeriodCallback(
hwc2_callback_data_t data, hwc2_function_pointer_t func) {
supported(__func__);
auto callback = std::make_shared<IAVsyncPeriodCallback>(data, func);
int ret = display_->RegisterVsyncPeriodCallback(std::move(callback),
static_cast<int>(handle_));
if (ret) {
ALOGE("Failed to register callback d=%" PRIu64 " ret=%d", handle_, ret);
return HWC2::Error::BadDisplay;
}
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::RegisterRefreshCallback(
hwc2_callback_data_t data, hwc2_function_pointer_t func) {
supported(__func__);
auto callback = std::make_shared<IARefreshCallback>(data, func);
display_->RegisterRefreshCallback(std::move(callback),
static_cast<int>(handle_));
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::RegisterHotPlugCallback(
hwc2_callback_data_t data, hwc2_function_pointer_t func) {
supported(__func__);
auto callback = std::make_shared<IAHotPlugEventCallback>(data, func, this);
display_->RegisterHotPlugCallback(std::move(callback),
static_cast<int>(handle_));
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::AcceptDisplayChanges() {
supported(__func__);
if (!check_validate_display_) {
ALOGV("AcceptChanges failed, not validated");
return HWC2::Error::NotValidated;
}
for (std::pair<const hwc2_layer_t, IAHWC2::Hwc2Layer> &l : layers_)
l.second.accept_type_change();
// reset the value to false
check_validate_display_ = false;
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
supported(__func__);
uint64_t id = display_->AcquireId();
layers_.emplace(static_cast<hwc2_layer_t>(id), IAHWC2::Hwc2Layer());
layers_.at(id).XTranslateCoordinates(display_->GetXTranslation());
layers_.at(id).YTranslateCoordinates(display_->GetYTranslation());
*layer = static_cast<hwc2_layer_t>(id);
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
supported(__func__);
if (layers_.empty() && layer > 0)
return HWC2::Error::BadLayer;
if (layers_.empty())
return HWC2::Error::None;
if (layers_.erase(layer))
display_->ReleaseId(layer);
return HWC2::Error::None;
}
void IAHWC2::HwcDisplay::FreeAllLayers() {
if (layers_.empty())
return;
display_->ResetLayerHashGenerator();
std::map<hwc2_layer_t, Hwc2Layer>().swap(layers_);
}
HWC2::Error IAHWC2::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
supported(__func__);
IHOTPLUGEVENTTRACE("GetActiveConfig called for Display: %p \n", display_);
if (!display_->GetActiveConfig(config))
return HWC2::Error::BadConfig;
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::GetChangedCompositionTypes(
uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
supported(__func__);
uint32_t num_changes = 0;
for (std::pair<const hwc2_layer_t, IAHWC2::Hwc2Layer> &l : layers_) {
if (l.second.type_changed()) {
if (layers && num_changes < *num_elements)
layers[num_changes] = l.first;
if (types && num_changes < *num_elements)
types[num_changes] = static_cast<int32_t>(l.second.validated_type());
++num_changes;
}
}
if (!layers && !types)
*num_elements = num_changes;
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::GetClientTargetSupport(uint32_t width,
uint32_t height,
int32_t format,
int32_t dataspace) {
if (width != display_->Width() || height != display_->Height()) {
return HWC2::Error::Unsupported;
}
if (format == HAL_PIXEL_FORMAT_RGBA_8888 &&
(dataspace == HAL_DATASPACE_UNKNOWN ||
dataspace == HAL_DATASPACE_STANDARD_UNSPECIFIED)) {
return HWC2::Error::None;
} else {
// Convert HAL to fourcc-based DRM formats
uint32_t drm_format = GetDrmFormatFromHALFormat(format);
if (display_->CheckPlaneFormat(drm_format) &&
(dataspace == HAL_DATASPACE_UNKNOWN ||
dataspace == HAL_DATASPACE_STANDARD_UNSPECIFIED))
return HWC2::Error::None;
}
return HWC2::Error::Unsupported;
}
HWC2::Error IAHWC2::HwcDisplay::GetColorModes(uint32_t *num_modes,
int32_t *modes) {
supported(__func__);
if (!modes)
*num_modes = 1;
if (modes)
*modes = HAL_COLOR_MODE_NATIVE;
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
int32_t attribute_in,
int32_t *value) {
supported(__func__);
auto attribute = static_cast<HWC2::Attribute>(attribute_in);
switch (attribute) {
case HWC2::Attribute::Width:
display_->GetDisplayAttribute(
config, hwcomposer::HWCDisplayAttribute::kWidth, value);
break;
case HWC2::Attribute::Height:
display_->GetDisplayAttribute(
config, hwcomposer::HWCDisplayAttribute::kHeight, value);
break;
case HWC2::Attribute::VsyncPeriod:
// in nanoseconds
display_->GetDisplayAttribute(
config, hwcomposer::HWCDisplayAttribute::kRefreshRate, value);
break;
case HWC2::Attribute::DpiX:
// Dots per 1000 inches
display_->GetDisplayAttribute(
config, hwcomposer::HWCDisplayAttribute::kDpiX, value);
break;
case HWC2::Attribute::DpiY:
// Dots per 1000 inches
display_->GetDisplayAttribute(
config, hwcomposer::HWCDisplayAttribute::kDpiY, value);
break;
default:
*value = -1;
return HWC2::Error::BadConfig;
}
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
hwc2_config_t *configs) {
supported(__func__);
if (!display_->GetDisplayConfigs(num_configs, configs))
return HWC2::Error::BadDisplay;
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
supported(__func__);
if (!display_->GetDisplayName(size, name))
return HWC2::Error::BadDisplay;
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
uint32_t *num_elements,
hwc2_layer_t *layers,
int32_t *layer_requests) {
supported(__func__);
// TODO: I think virtual display should request
// HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
unsupported(__func__, display_requests, num_elements, layers, layer_requests);
*num_elements = 0;
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::GetDisplayType(int32_t *type) {
supported(__func__);
*type = static_cast<int32_t>(type_);
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::GetDozeSupport(int32_t *support) {
supported(__func__);
*support = true;
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::GetHdrCapabilities(
uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
float * /*max_average_luminance*/, float * /*min_luminance*/) {
supported(__func__);
*num_types = 0;
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
hwc2_layer_t *layers,
int32_t *fences) {
supported(__func__);
if (layers == NULL || fences == NULL) {
*num_elements = layers_.size();
return HWC2::Error::None;
}
uint32_t num_layers = 0;
for (std::pair<const hwc2_layer_t, IAHWC2::Hwc2Layer> &l : layers_) {
++num_layers;
if (num_layers > *num_elements) {
ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
return HWC2::Error::None;
}
layers[num_layers - 1] = l.first;
fences[num_layers - 1] = l.second.GetLayer()->GetReleaseFence();
}
*num_elements = num_layers;
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::PresentDisplay(int32_t *retire_fence) {
supported(__func__);
// order the layers by z-order
bool use_client_layer = false;
uint32_t client_z_order = 0;
bool use_cursor_layer = false;
bool use_overlay_compose = false;
uint32_t cursor_z_order = 0;
IAHWC2::Hwc2Layer *cursor_layer;
*retire_fence = -1;
std::map<uint32_t, IAHWC2::Hwc2Layer *> z_map;
// if the power mode is doze suspend then its the hint that the drawing
// into the display has suspended and remain in the low power state and
// continue displaying the current state and stop applying display
// update from the client
if (display_->PowerMode() == HWC2_POWER_MODE_DOZE_SUSPEND)
return HWC2::Error::None;
size_t max_z_order = 0;
for (std::pair<const hwc2_layer_t, IAHWC2::Hwc2Layer> &l : layers_) {
if (l.second.IsCursorLayer()) {
use_cursor_layer = true;
cursor_layer = &l.second;
cursor_z_order = l.second.z_order();
continue;
}
#ifndef FORCE_ALL_DEVICE_TYPE
if ((l.second.validated_type() == HWC2::Composition::Client) &&
(l.second.GetLayer()->GetNativeHandle() == NULL)) {
use_client_layer = true;
ICOMPOSITORTRACE(
"Skip clinet layer without buffer which composed by SurfaceFlinger");
continue;
}
#endif
if ((l.second.validated_type() != HWC2::Composition::SolidColor) &&
(l.second.GetLayer()->GetNativeHandle() == NULL)) {
ETRACE(
"HWC don't support layer without buffer if not in type SolidColor");
continue;
}
switch (l.second.validated_type()) {
case HWC2::Composition::Device:
case HWC2::Composition::SolidColor:
use_overlay_compose = true;
z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
ICOMPOSITORTRACE(
"Add Device/SolidColor HWC2Layer[%d], displayFrame: %d, %d, %d, %d",
l.second.z_order(), l.second.GetLayer()->GetDisplayFrame().left,
l.second.GetLayer()->GetDisplayFrame().top,
l.second.GetLayer()->GetDisplayFrame().right,
l.second.GetLayer()->GetDisplayFrame().bottom);
if (l.second.z_order() > max_z_order)
max_z_order = l.second.z_order();
break;
case HWC2::Composition::Client:
// Place it at the z_order of the highest client layer
use_client_layer = true;
client_z_order = std::max(client_z_order, l.second.z_order());
break;
default:
continue;
}
}
if (use_client_layer && client_layer_.GetLayer() &&
client_layer_.GetLayer()->GetNativeHandle() &&
client_layer_.GetLayer()->GetNativeHandle()->handle_) {
z_map.emplace(std::make_pair(client_z_order, &client_layer_));
client_layer_.SetLayerZOrder(client_z_order);
ICOMPOSITORTRACE("Add Client HWC2Layer[%d], displayFrame: %d, %d, %d, %d",
client_z_order,
client_layer_.GetLayer()->GetDisplayFrame().left,
client_layer_.GetLayer()->GetDisplayFrame().top,
client_layer_.GetLayer()->GetDisplayFrame().right,
client_layer_.GetLayer()->GetDisplayFrame().bottom);
if (client_z_order > max_z_order)
max_z_order = client_z_order;
}
// Place the cursor at the highest z-order
if (use_cursor_layer) {
if (max_z_order > cursor_z_order) {
cursor_z_order = max_z_order + 1;
} else if (client_z_order > cursor_z_order) {
cursor_z_order = client_z_order + 1;
}
if (use_overlay_compose) {
z_map.emplace(std::make_pair(cursor_z_order, cursor_layer));
ICOMPOSITORTRACE("Add Cursor HWC2Layer[%d]", cursor_z_order);
}
}
std::vector<hwcomposer::HwcLayer *> layers;
// now that they're ordered by z, add them to the composition
for (std::pair<const uint32_t, IAHWC2::Hwc2Layer *> &l : z_map) {
layers.emplace_back(l.second->GetLayer());
ICOMPOSITORTRACE(
"Add HwcLayer[%d][%d], displayFrame: %d, %d, %d, %d. Blending: %d",
l.second->z_order(), l.second->validated_type(),
l.second->GetLayer()->GetDisplayFrame().left,
l.second->GetLayer()->GetDisplayFrame().top,
l.second->GetLayer()->GetDisplayFrame().right,
l.second->GetLayer()->GetDisplayFrame().bottom,
l.second->GetLayer()->GetBlending());
}
if (layers.empty() && display_->Type() != DisplayType::kLogical)
return HWC2::Error::None;
IHOTPLUGEVENTTRACE("PhysicalDisplay called for Display: %p \n", display_);
bool success = display_->Present(layers, retire_fence);
if (!success) {
ALOGE("Failed to set layers in the composition");
return HWC2::Error::BadLayer;
}
++frame_no_;
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
supported(__func__);
if (!display_->SetActiveConfig(config)) {
ALOGE("Could not find active mode for %d", config);
return HWC2::Error::BadConfig;
}
int display_width = 0;
int display_height = 0;
display_->GetDisplayAttribute(config, HWCDisplayAttribute::kWidth,
&display_width);
display_->GetDisplayAttribute(config, HWCDisplayAttribute::kHeight,
&display_height);
// Setup the client layer's dimensions
hwc_rect_t display_frame = {
.left = 0, .top = 0, .right = display_width, .bottom = display_height};
client_layer_.SetLayerDisplayFrame(display_frame);
hwc_frect_t source_crop = {.left = 0.0f,
.top = 0.0f,
.right = display_width + 0.0f,
.bottom = display_height + 0.0f};
client_layer_.SetLayerSourceCrop(source_crop);
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::SetActiveConfigWithConstraints(
hwc2_config_t config,
hwc_vsync_period_change_constraints_t* vsyncPeriodChangeConstraints,
hwc_vsync_period_change_timeline_t* outTimeline
) {
supported(__func__);
return SetActiveConfig(config);
}
HWC2::Error IAHWC2::HwcDisplay::GetDisplayVsyncPeriod(hwc2_vsync_period_t* outVsyncPeriod) {
supported(__func__);
//*outVsyncPeriod = 16666667;
//return HWC2::Error::None;
if(display_->GetDisplayVsyncPeriod(outVsyncPeriod))
return HWC2::Error::None;
else
return HWC2::Error::BadConfig;
}
HWC2::Error IAHWC2::HwcDisplay::SetClientTarget(buffer_handle_t target,
int32_t acquire_fence,
int32_t dataspace,
hwc_region_t damage) {
supported(__func__);
client_layer_.set_buffer(target);
client_layer_.set_acquire_fence(acquire_fence);
client_layer_.SetLayerDataspace(dataspace);
client_layer_.SetLayerSurfaceDamage(damage);
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::SetColorMode(int32_t mode) {
supported(__func__);
if (mode < 0)
return HWC2::Error::BadParameter;
// TODO: Use the parameter mode to set the color mode for the display to be
// used.
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::SetColorModeWithRenderIntent(int32_t mode,
int32_t intent) {
supported(__func__);
if (mode < 0)
return HWC2::Error::BadParameter;
if (intent < 0)
return HWC2::Error::BadParameter;
// TODO: Use the parameter mode to set the color mode for the display to be
// used.
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::GetRenderIntents(int32_t mode,
uint32_t *outNumIntents,
int32_t *outIntents) {
supported(__func__);
if (mode < 0) {
return HWC2::Error::BadParameter;
}
if (!outNumIntents) {
return HWC2::Error::BadParameter;
}
if (outIntents == NULL) {
*outNumIntents = GetNumRenderIntents();
return HWC2::Error::None;
}
if (mode == HAL_COLOR_MODE_NATIVE) {
*outIntents = HAL_RENDER_INTENT_COLORIMETRIC;
}
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::SetColorTransform(const float *matrix,
int32_t hint) {
supported(__func__);
// TODO: Force client composition if we get this
if (hint != HAL_COLOR_TRANSFORM_IDENTITY &&
hint != HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX &&
hint != HAL_COLOR_TRANSFORM_VALUE_INVERSE &&
hint != HAL_COLOR_TRANSFORM_GRAYSCALE &&
hint != HAL_COLOR_TRANSFORM_CORRECT_PROTANOPIA &&
hint != HAL_COLOR_TRANSFORM_CORRECT_DEUTERANOPIA &&
hint != HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
return HWC2::Error::BadParameter;
display_->SetColorTransform(matrix, (HWCColorTransform)hint);
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
int32_t release_fence) {
supported(__func__);
struct gralloc_handle *temp = new struct gralloc_handle();
temp->handle_ = buffer;
display_->SetOutputBuffer(temp, release_fence);
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::SetPowerMode(int32_t mode_in) {
supported(__func__);
if (mode_in < 0)
return HWC2::Error::BadParameter;
uint32_t power_mode = 0;
auto mode = static_cast<HWC2::PowerMode>(mode_in);
switch (mode) {
case HWC2::PowerMode::Off:
power_mode = HWC2_POWER_MODE_OFF;
break;
case HWC2::PowerMode::Doze:
power_mode = HWC2_POWER_MODE_DOZE;
break;
case HWC2::PowerMode::DozeSuspend:
power_mode = HWC2_POWER_MODE_DOZE_SUSPEND;
break;
case HWC2::PowerMode::On:
power_mode = HWC2_POWER_MODE_ON;
break;
default:
ALOGI("Power mode %d is unsupported\n", mode);
return HWC2::Error::BadParameter;
};
display_->SetPowerMode(power_mode);
return HWC2::Error::None;
}
HWC2::Error IAHWC2::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
supported(__func__);
switch (enabled) {
case HWC2_VSYNC_ENABLE: