summaryrefslogtreecommitdiff
path: root/libhwc2.1/libdevice/ExynosDevice.cpp
blob: 0f2d20284f4045d0b4236b28551357fe512b1c7e (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
/*
 * Copyright (C) 2012 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 | ATRACE_TAG_HAL)

#include "ExynosDevice.h"

#include <aidl/android/hardware/graphics/composer3/IComposerCallback.h>
#include <sync/sync.h>
#include <sys/mman.h>
#include <unistd.h>

#include "BrightnessController.h"
#include "ExynosDeviceDrmInterface.h"
#include "ExynosDisplay.h"
#include "ExynosExternalDisplayModule.h"
#include "ExynosHWCDebug.h"
#include "ExynosHWCHelper.h"
#include "ExynosLayer.h"
#include "ExynosPrimaryDisplayModule.h"
#include "ExynosResourceManagerModule.h"
#include "ExynosVirtualDisplayModule.h"
#include "VendorGraphicBuffer.h"

using namespace vendor::graphics;
using namespace SOC_VERSION;
using aidl::android::hardware::graphics::composer3::IComposerCallback;

/**
 * ExynosDevice implementation
 */

class ExynosDevice;

extern void PixelDisplayInit(ExynosDisplay *exynos_display, const std::string_view instance_str);

static const std::map<const uint32_t, const std::string_view> pixelDisplayIntfName =
        {{getDisplayId(HWC_DISPLAY_PRIMARY, 0), "default"},
#ifdef USES_IDISPLAY_INTF_SEC
         {getDisplayId(HWC_DISPLAY_PRIMARY, 1), "secondary"}
#endif
};

int hwcDebug;
int hwcFenceDebug[FENCE_IP_ALL];
struct exynos_hwc_control exynosHWCControl;
struct update_time_info updateTimeInfo;
char fence_names[FENCE_MAX][32];

uint32_t getDeviceInterfaceType()
{
    if (access(DRM_DEVICE_PATH, F_OK) == NO_ERROR)
        return INTERFACE_TYPE_DRM;
    else
        return INTERFACE_TYPE_FB;
}

ExynosDevice::ExynosDevice()
    : mGeometryChanged(0),
    mVsyncFd(-1),
    mExtVsyncFd(-1),
    mVsyncDisplayId(getDisplayId(HWC_DISPLAY_PRIMARY, 0)),
    mTimestamp(0),
    mDisplayMode(0),
    mInterfaceType(INTERFACE_TYPE_FB),
    mIsInTUI(false)
{
    exynosHWCControl.forceGpu = false;
    exynosHWCControl.windowUpdate = true;
    exynosHWCControl.forcePanic = false;
    exynosHWCControl.skipStaticLayers = true;
    exynosHWCControl.skipM2mProcessing = true;
    exynosHWCControl.skipResourceAssign = true;
    exynosHWCControl.multiResolution = true;
    exynosHWCControl.dumpMidBuf = false;
    exynosHWCControl.displayMode = DISPLAY_MODE_NUM;
    exynosHWCControl.setDDIScaler = false;
    exynosHWCControl.skipWinConfig = false;
    exynosHWCControl.skipValidate = true;
    exynosHWCControl.doFenceFileDump = false;
    exynosHWCControl.fenceTracer = 0;
    exynosHWCControl.sysFenceLogging = false;
    exynosHWCControl.useDynamicRecomp = false;

    hwcDebug = 0;

    mInterfaceType = getDeviceInterfaceType();
    ALOGD("HWC2 : %s : interface type(%d)", __func__, mInterfaceType);

    /*
     * This order should not be changed
     * new ExynosResourceManager ->
     * create displays and add them to the list ->
     * initDeviceInterface() ->
     * ExynosResourceManager::updateRestrictions()
     */
    mResourceManager = new ExynosResourceManagerModule(this);

    for (size_t i = 0; i < AVAILABLE_DISPLAY_UNITS.size(); i++) {
        exynos_display_t display_t = AVAILABLE_DISPLAY_UNITS[i];
        ExynosDisplay *exynos_display = NULL;
        ALOGD("Create display[%zu] type: %d, index: %d", i, display_t.type, display_t.index);
        switch(display_t.type) {
            case HWC_DISPLAY_PRIMARY:
                exynos_display =
                        (ExynosDisplay *)(new ExynosPrimaryDisplayModule(display_t.index, this,
                                                                         display_t.display_name));
                if(display_t.index == 0) {
                    exynos_display->mPlugState = true;
                    ExynosMPP::mainDisplayWidth = exynos_display->mXres;
                    if (ExynosMPP::mainDisplayWidth <= 0) {
                        ExynosMPP::mainDisplayWidth = 1440;
                    }
                    ExynosMPP::mainDisplayHeight = exynos_display->mYres;
                    if (ExynosMPP::mainDisplayHeight <= 0) {
                        ExynosMPP::mainDisplayHeight = 2560;
                    }
                }
                break;
            case HWC_DISPLAY_EXTERNAL:
                exynos_display =
                        (ExynosDisplay *)(new ExynosExternalDisplayModule(display_t.index, this,
                                                                          display_t.display_name));
                break;
            case HWC_DISPLAY_VIRTUAL:
                exynos_display =
                        (ExynosDisplay *)(new ExynosVirtualDisplayModule(display_t.index, this,
                                                                         display_t.display_name));
                mNumVirtualDisplay = 0;
                break;
            default:
                ALOGE("Unsupported display type(%d)", display_t.type);
                break;
        }
        exynos_display->mDeconNodeName.appendFormat("%s", display_t.decon_node_name.c_str());
        mDisplays.add(exynos_display);
        mDisplayMap.insert(std::make_pair(exynos_display->mDisplayId, exynos_display));

#ifndef FORCE_DISABLE_DR
        if (exynos_display->mDREnable)
            exynosHWCControl.useDynamicRecomp = true;
#endif
    }

    memset(mCallbackInfos, 0, sizeof(mCallbackInfos));

    dynamicRecompositionThreadCreate();

    for (uint32_t i = 0; i < FENCE_IP_ALL; i++)
        hwcFenceDebug[i] = 0;

    for (uint32_t i = 0; i < FENCE_MAX; i++) {
        memset(fence_names[i], 0, sizeof(fence_names[0]));
        sprintf(fence_names[i], "_%2dh", i);
    }

    for (auto it : mDisplays) {
        std::string displayName = std::string(it->mDisplayName.c_str());
        it->mErrLogFileWriter.setPrefixName(displayName + "_hwc_error_log");
        it->mDebugDumpFileWriter.setPrefixName(displayName + "_hwc_debug");
        it->mFenceFileWriter.setPrefixName(displayName + "_hwc_fence_state");
        String8 saveString;
        saveString.appendFormat("ExynosDisplay %s is initialized", it->mDisplayName.c_str());
        saveErrorLog(saveString, it);
    }

    initDeviceInterface(mInterfaceType);

    // registerRestrictions();
    mResourceManager->updateRestrictions();
    mResourceManager->initDisplays(mDisplays, mDisplayMap);
    mResourceManager->initDisplaysTDMInfo();

    if (mInterfaceType == INTERFACE_TYPE_DRM) {
        setVBlankOffDelay(1);
    }

    char value[PROPERTY_VALUE_MAX];
    property_get("vendor.display.lbe.supported", value, "0");
    const bool lbe_supported = atoi(value) ? true : false;

    for (size_t i = 0; i < mDisplays.size(); i++) {
        if (mDisplays[i]->mType == HWC_DISPLAY_PRIMARY) {
            auto iter = pixelDisplayIntfName.find(getDisplayId(HWC_DISPLAY_PRIMARY, i));
            if (iter != pixelDisplayIntfName.end()) {
                PixelDisplayInit(mDisplays[i], iter->second);
                if (lbe_supported) {
                    mDisplays[i]->initLbe();
                }
            }
        }
    }

    mDisplayOffAsync = property_get_bool("vendor.display.async_off.supported", false);
}

void ExynosDevice::initDeviceInterface(uint32_t interfaceType)
{
    if (interfaceType == INTERFACE_TYPE_DRM) {
        mDeviceInterface = std::make_unique<ExynosDeviceDrmInterface>(this);
    } else {
        LOG_ALWAYS_FATAL("%s::Unknown interface type(%d)",
                __func__, interfaceType);
    }

    mDeviceInterface->init(this);

    /* Remove display when display interface is not valid */
    for (uint32_t i = 0; i < mDisplays.size();) {
        ExynosDisplay* display = mDisplays[i];
        display->initDisplayInterface(interfaceType);
        if (mDeviceInterface->initDisplayInterface(
                    display->mDisplayInterface) != NO_ERROR) {
            ALOGD("Remove display[%d], Failed to initialize display interface", i);
            mDisplays.removeAt(i);
            mDisplayMap.erase(display->mDisplayId);
            delete display;
        } else {
            i++;
        }
    }

    mDeviceInterface->postInit();
}

ExynosDevice::~ExynosDevice() {
    mDRLoopStatus = false;
    mDRThread.join();
    for(auto& display : mDisplays) {
        delete display;
    }
    mDisplays.clear();
}

bool ExynosDevice::isFirstValidate()
{
    for (uint32_t i = 0; i < mDisplays.size(); i++) {
        if ((mDisplays[i]->mType != HWC_DISPLAY_VIRTUAL) &&
            (!mDisplays[i]->mPowerModeState.has_value() ||
             (mDisplays[i]->mPowerModeState.value() == (hwc2_power_mode_t)HWC_POWER_MODE_OFF)))
            continue;
        if ((mDisplays[i]->mPlugState == true) &&
            ((mDisplays[i]->mRenderingState != RENDERING_STATE_NONE) &&
             (mDisplays[i]->mRenderingState != RENDERING_STATE_PRESENTED)))
            return false;
    }

    return true;
}

bool ExynosDevice::isLastValidate(ExynosDisplay *display)
{
    for (uint32_t i = 0; i < mDisplays.size(); i++) {
        if (mDisplays[i] == display)
            continue;
        if ((mDisplays[i]->mType != HWC_DISPLAY_VIRTUAL) &&
            (!mDisplays[i]->mPowerModeState.has_value() ||
             (mDisplays[i]->mPowerModeState.value() == (hwc2_power_mode_t)HWC_POWER_MODE_OFF)))
            continue;
        if ((mDisplays[i]->mPlugState == true) &&
            (mDisplays[i]->mRenderingState != RENDERING_STATE_VALIDATED) &&
            (mDisplays[i]->mRenderingState != RENDERING_STATE_ACCEPTED_CHANGE))
            return false;
    }
    return true;
}

bool ExynosDevice::hasOtherDisplayOn(ExynosDisplay *display) {
    for (uint32_t i = 0; i < mDisplays.size(); i++) {
        if (mDisplays[i] == display) continue;
        if ((mDisplays[i]->mType != HWC_DISPLAY_VIRTUAL) &&
            mDisplays[i]->mPowerModeState.has_value() &&
            (mDisplays[i]->mPowerModeState.value() != (hwc2_power_mode_t)HWC_POWER_MODE_OFF))
            return true;
    }
    return false;
}

bool ExynosDevice::isDynamicRecompositionThreadAlive()
{
    android_atomic_acquire_load(&mDRThreadStatus);
    return (mDRThreadStatus > 0);
}

void ExynosDevice::checkDynamicRecompositionThread()
{
    ATRACE_CALL();
    // If thread was destroyed, create thread and run. (resume status)
    if (isDynamicRecompositionThreadAlive() == false) {
        for (uint32_t i = 0; i < mDisplays.size(); i++) {
            if (mDisplays[i]->mDREnable) {
                dynamicRecompositionThreadCreate();
                return;
            }
        }
    } else {
    // If thread is running and all displays turnned off DR, destroy the thread.
        for (uint32_t i = 0; i < mDisplays.size(); i++) {
            if (mDisplays[i]->mDREnable)
                return;
        }
        mDRLoopStatus = false;
        mDRWakeUpCondition.notify_one();
        mDRThread.join();
    }
}

void ExynosDevice::dynamicRecompositionThreadCreate()
{
    if (exynosHWCControl.useDynamicRecomp == true) {
        mDRLoopStatus = true;
        mDRThread = std::thread(&dynamicRecompositionThreadLoop, this);
    }
}

void *ExynosDevice::dynamicRecompositionThreadLoop(void *data)
{
    ExynosDevice *dev = (ExynosDevice *)data;
    ExynosDisplay *display[dev->mDisplays.size()];
    uint64_t event_cnt[dev->mDisplays.size()];

    for (uint32_t i = 0; i < dev->mDisplays.size(); i++) {
        display[i] = dev->mDisplays[i];
        event_cnt[i] = 0;
    }
    android_atomic_inc(&(dev->mDRThreadStatus));

    while (dev->mDRLoopStatus) {
        for (uint32_t i = 0; i < dev->mDisplays.size(); i++)
            event_cnt[i] = display[i]->mUpdateEventCnt;

        /*
         * If there is no update for more than 5s, favor the client composition mode.
         * If all other conditions are met, mode will be switched to client composition.
         */
        {
            std::unique_lock<std::mutex> lock(dev->mDRWakeUpMutex);
            dev->mDRWakeUpCondition.wait_for(lock, std::chrono::seconds(5));
            if (!dev->mDRLoopStatus) {
                break;
            }
        }
        for (uint32_t i = 0; i < dev->mDisplays.size(); i++) {
            if (display[i]->mDREnable &&
                display[i]->mPlugState == true &&
                event_cnt[i] == display[i]->mUpdateEventCnt) {
                if (display[i]->checkDynamicReCompMode() == DEVICE_2_CLIENT) {
                    display[i]->mUpdateEventCnt = 0;
                    display[i]->setGeometryChanged(GEOMETRY_DISPLAY_DYNAMIC_RECOMPOSITION);
                    dev->onRefresh(display[i]->mDisplayId);
                }
            }
        }
    }

    android_atomic_dec(&(dev->mDRThreadStatus));

    return NULL;
}

/**
 * Device Functions for HWC 2.0
 */

int32_t ExynosDevice::createVirtualDisplay(
        uint32_t width, uint32_t height, int32_t* /*android_pixel_format_t*/ format, ExynosDisplay* display) {
    ((ExynosVirtualDisplay*)display)->createVirtualDisplay(width, height, format);
    return 0;
}

/**
 * @param *display
 * @return int32_t
 */
int32_t ExynosDevice::destroyVirtualDisplay(ExynosDisplay* display) {
    ((ExynosVirtualDisplay *)display)->destroyVirtualDisplay();
    return 0;
}

void ExynosDevice::dump(uint32_t *outSize, char *outBuffer) {
    if (outSize == NULL) {
        ALOGE("%s:: outSize is null", __func__);
        return;
    }

    String8 result;
    dump(result);

    if (outBuffer == NULL) {
        *outSize = static_cast<uint32_t>(result.length());
    } else {
        if (*outSize == 0) {
            ALOGE("%s:: outSize is 0", __func__);
            return;
        }
        size_t copySize = min(static_cast<size_t>(*outSize), result.size());
        ALOGI("HWC dump:: resultSize(%zu), outSize(%d), copySize(%zu)", result.size(), *outSize,
              copySize);
        strlcpy(outBuffer, result.c_str(), copySize);
    }
}

void ExynosDevice::dump(String8 &result) {
    result.append("\n\n");

    struct tm* localTime = (struct tm*)localtime((time_t*)&updateTimeInfo.lastUeventTime.tv_sec);
    result.appendFormat("lastUeventTime(%02d:%02d:%02d.%03lu) lastTimestamp(%" PRIu64 ")\n",
            localTime->tm_hour, localTime->tm_min,
            localTime->tm_sec, updateTimeInfo.lastUeventTime.tv_usec/1000, mTimestamp);

    localTime = (struct tm*)localtime((time_t*)&updateTimeInfo.lastEnableVsyncTime.tv_sec);
    result.appendFormat("lastEnableVsyncTime(%02d:%02d:%02d.%03lu)\n",
            localTime->tm_hour, localTime->tm_min,
            localTime->tm_sec, updateTimeInfo.lastEnableVsyncTime.tv_usec/1000);

    localTime = (struct tm*)localtime((time_t*)&updateTimeInfo.lastDisableVsyncTime.tv_sec);
    result.appendFormat("lastDisableVsyncTime(%02d:%02d:%02d.%03lu)\n",
            localTime->tm_hour, localTime->tm_min,
            localTime->tm_sec, updateTimeInfo.lastDisableVsyncTime.tv_usec/1000);

    localTime = (struct tm*)localtime((time_t*)&updateTimeInfo.lastValidateTime.tv_sec);
    result.appendFormat("lastValidateTime(%02d:%02d:%02d.%03lu)\n",
            localTime->tm_hour, localTime->tm_min,
            localTime->tm_sec, updateTimeInfo.lastValidateTime.tv_usec/1000);

    localTime = (struct tm*)localtime((time_t*)&updateTimeInfo.lastPresentTime.tv_sec);
    result.appendFormat("lastPresentTime(%02d:%02d:%02d.%03lu)\n",
            localTime->tm_hour, localTime->tm_min,
            localTime->tm_sec, updateTimeInfo.lastPresentTime.tv_usec/1000);

    result.appendFormat("\n");
    mResourceManager->dump(result);

    result.appendFormat("special plane num: %d:\n", getSpecialPlaneNum());
    for (uint32_t index = 0; index < getSpecialPlaneNum(); index++) {
        result.appendFormat("\tindex: %d attribute 0x%" PRIx64 "\n", getSpecialPlaneId(index),
                            getSpecialPlaneAttr(index));
    }
    result.append("\n");

    for (size_t i = 0;i < mDisplays.size(); i++) {
        ExynosDisplay *display = mDisplays[i];
        if (display->mPlugState == true)
            display->dump(result);
    }
}

uint32_t ExynosDevice::getMaxVirtualDisplayCount() {
#ifdef USES_VIRTUAL_DISPLAY
    return 1;
#else
    return 0;
#endif
}

int32_t ExynosDevice::registerCallback (
        int32_t descriptor, hwc2_callback_data_t callbackData,
        hwc2_function_pointer_t point) {
    if (descriptor < 0 || descriptor > HWC2_CALLBACK_SEAMLESS_POSSIBLE)
        return HWC2_ERROR_BAD_PARAMETER;

    Mutex::Autolock lock(mDeviceCallbackMutex);
    mCallbackInfos[descriptor].callbackData = callbackData;
    mCallbackInfos[descriptor].funcPointer = point;

    /* Call hotplug callback for primary display*/
    if (descriptor == HWC2_CALLBACK_HOTPLUG) {
        HWC2_PFN_HOTPLUG callbackFunc =
                reinterpret_cast<HWC2_PFN_HOTPLUG>(mCallbackInfos[descriptor].funcPointer);
        if (callbackFunc != nullptr) {
            for (auto it : mDisplays) {
                if (it->mPlugState)
                    callbackFunc(callbackData, getDisplayId(it->mType, it->mIndex),
                            HWC2_CONNECTION_CONNECTED);
            }
        } else {
            // unregistering callback can be used as a sign of ComposerClient's death
            for (auto it : mDisplays) {
                it->cleanupAfterClientDeath();
            }
        }
    }
    /* TODO(b/265244856): called by register callback vsync. it's only hwc2. */
    if (descriptor == HWC2_CALLBACK_VSYNC)
        mResourceManager->doPreProcessing();

    return HWC2_ERROR_NONE;
}

bool ExynosDevice::isCallbackRegisteredLocked(int32_t descriptor) {
    if (descriptor < 0 || descriptor > HWC2_CALLBACK_SEAMLESS_POSSIBLE) {
        ALOGE("%s:: %d callback is unknown", __func__, descriptor);
        return false;
    }

    if (mCallbackInfos[descriptor].callbackData == nullptr ||
        mCallbackInfos[descriptor].funcPointer == nullptr) {
        ALOGE("%s:: %d callback is not registered", __func__, descriptor);
        return false;
    }

    return true;
}

bool ExynosDevice::isCallbackAvailable(int32_t descriptor) {
    Mutex::Autolock lock(mDeviceCallbackMutex);
    return isCallbackRegisteredLocked(descriptor);
}

void ExynosDevice::onHotPlug(uint32_t displayId, bool status) {
    Mutex::Autolock lock(mDeviceCallbackMutex);

    if (!isCallbackRegisteredLocked(HWC2_CALLBACK_HOTPLUG)) return;

    hwc2_callback_data_t callbackData = mCallbackInfos[HWC2_CALLBACK_HOTPLUG].callbackData;
    HWC2_PFN_HOTPLUG callbackFunc =
            reinterpret_cast<HWC2_PFN_HOTPLUG>(mCallbackInfos[HWC2_CALLBACK_HOTPLUG].funcPointer);
    callbackFunc(callbackData, displayId,
                 status ? HWC2_CONNECTION_CONNECTED : HWC2_CONNECTION_DISCONNECTED);
}
void ExynosDevice::onRefreshDisplays() {
    for (auto& display : mDisplays) {
         onRefresh(display->mDisplayId);
    }
}

void ExynosDevice::onRefresh(uint32_t displayId) {
    Mutex::Autolock lock(mDeviceCallbackMutex);

    if (!isCallbackRegisteredLocked(HWC2_CALLBACK_REFRESH)) return;

    if (!checkDisplayConnection(displayId)) return;

    ExynosDisplay *display = (ExynosDisplay *)getDisplay(displayId);

    if (!display->mPowerModeState.has_value() ||
             (display->mPowerModeState.value() == (hwc2_power_mode_t)HWC_POWER_MODE_OFF))
        return;

    hwc2_callback_data_t callbackData = mCallbackInfos[HWC2_CALLBACK_REFRESH].callbackData;
    HWC2_PFN_REFRESH callbackFunc =
            reinterpret_cast<HWC2_PFN_REFRESH>(mCallbackInfos[HWC2_CALLBACK_REFRESH].funcPointer);
    callbackFunc(callbackData, displayId);
}

void ExynosDevice::onVsync(uint32_t displayId, int64_t timestamp) {
    Mutex::Autolock lock(mDeviceCallbackMutex);

    if (!isCallbackRegisteredLocked(HWC2_CALLBACK_VSYNC)) return;

    hwc2_callback_data_t callbackData = mCallbackInfos[HWC2_CALLBACK_VSYNC].callbackData;
    HWC2_PFN_VSYNC callbackFunc =
            reinterpret_cast<HWC2_PFN_VSYNC>(mCallbackInfos[HWC2_CALLBACK_VSYNC].funcPointer);
    callbackFunc(callbackData, displayId, timestamp);
}

bool ExynosDevice::onVsync_2_4(uint32_t displayId, int64_t timestamp, uint32_t vsyncPeriod) {
    Mutex::Autolock lock(mDeviceCallbackMutex);

    if (!isCallbackRegisteredLocked(HWC2_CALLBACK_VSYNC_2_4)) return false;

    hwc2_callback_data_t callbackData = mCallbackInfos[HWC2_CALLBACK_VSYNC_2_4].callbackData;
    HWC2_PFN_VSYNC_2_4 callbackFunc = reinterpret_cast<HWC2_PFN_VSYNC_2_4>(
            mCallbackInfos[HWC2_CALLBACK_VSYNC_2_4].funcPointer);
    callbackFunc(callbackData, displayId, timestamp, vsyncPeriod);

    return true;
}

void ExynosDevice::onVsyncPeriodTimingChanged(uint32_t displayId,
                                              hwc_vsync_period_change_timeline_t *timeline) {
    Mutex::Autolock lock(mDeviceCallbackMutex);

    if (!timeline) {
        ALOGE("vsync period change timeline is null");
        return;
    }

    if (!isCallbackRegisteredLocked(HWC2_CALLBACK_VSYNC_PERIOD_TIMING_CHANGED)) return;

    hwc2_callback_data_t callbackData =
            mCallbackInfos[HWC2_CALLBACK_VSYNC_PERIOD_TIMING_CHANGED].callbackData;
    HWC2_PFN_VSYNC_PERIOD_TIMING_CHANGED callbackFunc =
            reinterpret_cast<HWC2_PFN_VSYNC_PERIOD_TIMING_CHANGED>(
                    mCallbackInfos[HWC2_CALLBACK_VSYNC_PERIOD_TIMING_CHANGED].funcPointer);
    callbackFunc(callbackData, displayId, timeline);
}

void ExynosDevice::setHWCDebug(unsigned int debug)
{
    hwcDebug = debug;
}

uint32_t ExynosDevice::getHWCDebug()
{
    return hwcDebug;
}

void ExynosDevice::setHWCFenceDebug(uint32_t typeNum, uint32_t ipNum, uint32_t mode)
{
    if (typeNum > FENCE_TYPE_ALL || typeNum < 0 || ipNum > FENCE_IP_ALL || ipNum < 0
            || mode > 1 || mode < 0) {
        ALOGE("%s:: input is not valid type(%u), IP(%u), mode(%d)", __func__, typeNum, ipNum, mode);
        return;
    }

    uint32_t value = 0;

    if (typeNum == FENCE_TYPE_ALL)
        value = (1 << FENCE_TYPE_ALL) - 1;
    else
        value = 1 << typeNum;

    if (ipNum == FENCE_IP_ALL) {
        for (uint32_t i = 0; i < FENCE_IP_ALL; i++) {
            if (mode)
                hwcFenceDebug[i] |= value;
            else
                hwcFenceDebug[i] &= (~value);
        }
    } else {
        if (mode)
            hwcFenceDebug[ipNum] |= value;
        else
            hwcFenceDebug[ipNum] &= (~value);
    }

}

void ExynosDevice::getHWCFenceDebug()
{
    for (uint32_t i = 0; i < FENCE_IP_ALL; i++)
        ALOGE("[HWCFenceDebug] IP_Number(%d) : Debug(%x)", i, hwcFenceDebug[i]);
}

void ExynosDevice::setHWCControl(uint32_t displayId, uint32_t ctrl, int32_t val) {
    ExynosDisplay *exynosDisplay = NULL;
    switch (ctrl) {
        case HWC_CTL_FORCE_GPU:
            ALOGI("%s::HWC_CTL_FORCE_GPU on/off=%d", __func__, val);
            exynosHWCControl.forceGpu = (unsigned int)val;
            setGeometryChanged(GEOMETRY_DEVICE_CONFIG_CHANGED);
            onRefresh(displayId);
            break;
        case HWC_CTL_WINDOW_UPDATE:
            ALOGI("%s::HWC_CTL_WINDOW_UPDATE on/off=%d", __func__, val);
            exynosHWCControl.windowUpdate = (unsigned int)val;
            setGeometryChanged(GEOMETRY_DEVICE_CONFIG_CHANGED);
            onRefresh(displayId);
            break;
        case HWC_CTL_FORCE_PANIC:
            ALOGI("%s::HWC_CTL_FORCE_PANIC on/off=%d", __func__, val);
            exynosHWCControl.forcePanic = (unsigned int)val;
            setGeometryChanged(GEOMETRY_DEVICE_CONFIG_CHANGED);
            break;
        case HWC_CTL_SKIP_STATIC:
            ALOGI("%s::HWC_CTL_SKIP_STATIC on/off=%d", __func__, val);
            exynosHWCControl.skipStaticLayers = (unsigned int)val;
            setGeometryChanged(GEOMETRY_DEVICE_CONFIG_CHANGED);
            break;
        case HWC_CTL_SKIP_M2M_PROCESSING:
            ALOGI("%s::HWC_CTL_SKIP_M2M_PROCESSING on/off=%d", __func__, val);
            exynosHWCControl.skipM2mProcessing = (unsigned int)val;
            setGeometryChanged(GEOMETRY_DEVICE_CONFIG_CHANGED);
            break;
        case HWC_CTL_SKIP_RESOURCE_ASSIGN:
            ALOGI("%s::HWC_CTL_SKIP_RESOURCE_ASSIGN on/off=%d", __func__, val);
            exynosHWCControl.skipResourceAssign = (unsigned int)val;
            setGeometryChanged(GEOMETRY_DEVICE_CONFIG_CHANGED);
            onRefreshDisplays();
            break;
        case HWC_CTL_SKIP_VALIDATE:
            ALOGI("%s::HWC_CTL_SKIP_VALIDATE on/off=%d", __func__, val);
            exynosHWCControl.skipValidate = (unsigned int)val;
            setGeometryChanged(GEOMETRY_DEVICE_CONFIG_CHANGED);
            onRefreshDisplays();
            break;
        case HWC_CTL_DUMP_MID_BUF:
            ALOGI("%s::HWC_CTL_DUMP_MID_BUF on/off=%d", __func__, val);
            exynosHWCControl.dumpMidBuf = (unsigned int)val;
            setGeometryChanged(GEOMETRY_DEVICE_CONFIG_CHANGED);
            onRefreshDisplays();
            break;
        case HWC_CTL_CAPTURE_READBACK:
            captureScreenWithReadback(displayId);
            break;
        case HWC_CTL_DISPLAY_MODE:
            ALOGI("%s::HWC_CTL_DISPLAY_MODE mode=%d", __func__, val);
            setDisplayMode((uint32_t)val);
            setGeometryChanged(GEOMETRY_DEVICE_CONFIG_CHANGED);
            onRefreshDisplays();
            break;
        // Support DDI scalser {
        case HWC_CTL_DDI_RESOLUTION_CHANGE:
            ALOGI("%s::HWC_CTL_DDI_RESOLUTION_CHANGE mode=%d", __func__, val);
            exynosDisplay = (ExynosDisplay *)getDisplay(displayId);
            uint32_t width, height;

            /* TODO: Add branch here for each resolution/index */
            switch(val) {
            case 1:
            case 2:
            case 3:
            default:
                width = 1440; height = 2960;
                break;
            }

            if (exynosDisplay == NULL) {
                for (uint32_t i = 0; i < mDisplays.size(); i++) {
                    mDisplays[i]->setDDIScalerEnable(width, height);
                }
            } else {
                exynosDisplay->setDDIScalerEnable(width, height);
            }
            setGeometryChanged(GEOMETRY_DISPLAY_RESOLUTION_CHANGED);
            onRefreshDisplays();
            break;
        // } Support DDI scaler
        case HWC_CTL_ENABLE_COMPOSITION_CROP:
        case HWC_CTL_ENABLE_EXYNOSCOMPOSITION_OPT:
        case HWC_CTL_ENABLE_CLIENTCOMPOSITION_OPT:
        case HWC_CTL_USE_MAX_G2D_SRC:
        case HWC_CTL_ENABLE_HANDLE_LOW_FPS:
        case HWC_CTL_ENABLE_EARLY_START_MPP:
            exynosDisplay = (ExynosDisplay *)getDisplay(displayId);
            if (exynosDisplay == NULL) {
                for (uint32_t i = 0; i < mDisplays.size(); i++) {
                    mDisplays[i]->setHWCControl(ctrl, val);
                }
            } else {
                exynosDisplay->setHWCControl(ctrl, val);
            }
            setGeometryChanged(GEOMETRY_DEVICE_CONFIG_CHANGED);
            onRefreshDisplays();
            break;
        case HWC_CTL_DYNAMIC_RECOMP:
            ALOGI("%s::HWC_CTL_DYNAMIC_RECOMP on/off = %d", __func__, val);
            setDynamicRecomposition(displayId, (unsigned int)val);
            break;
        case HWC_CTL_ENABLE_FENCE_TRACER:
            ALOGI("%s::HWC_CTL_ENABLE_FENCE_TRACER on/off=%d", __func__, val);
            exynosHWCControl.fenceTracer = (unsigned int)val;
            break;
        case HWC_CTL_SYS_FENCE_LOGGING:
            ALOGI("%s::HWC_CTL_SYS_FENCE_LOGGING on/off=%d", __func__, val);
            exynosHWCControl.sysFenceLogging = (unsigned int)val;
            break;
        case HWC_CTL_DO_FENCE_FILE_DUMP:
            ALOGI("%s::HWC_CTL_DO_FENCE_FILE_DUMP on/off=%d", __func__, val);
            exynosHWCControl.doFenceFileDump = (unsigned int)val;
            break;
        default:
            ALOGE("%s: unsupported HWC_CTL (%d)", __func__, ctrl);
            break;
    }
}

void ExynosDevice::setDisplayMode(uint32_t displayMode)
{
    exynosHWCControl.displayMode = displayMode;
}

void ExynosDevice::setDynamicRecomposition(uint32_t displayId, unsigned int on) {
    exynosHWCControl.useDynamicRecomp = on;
    ExynosDisplay *display = getDisplay(displayId);
    if (display) {
        display->mDRDefault = on;
        display->mDREnable = on;
        onRefresh(displayId);
    }
}

bool ExynosDevice::checkDisplayConnection(uint32_t displayId)
{
	ExynosDisplay *display = getDisplay(displayId);

    if (!display)
        return false;
    else
        return display->mPlugState;
}

bool ExynosDevice::checkNonInternalConnection()
{
    for (uint32_t i = 0; i < mDisplays.size(); i++) {
        switch(mDisplays[i]->mType) {
            case HWC_DISPLAY_PRIMARY:
                break;
            case HWC_DISPLAY_EXTERNAL:
            case HWC_DISPLAY_VIRTUAL:
                if (mDisplays[i]->mPlugState)
                    return true;
                break;
            default:
                break;
        }
    }
    return false;
}

void ExynosDevice::getCapabilitiesLegacy(uint32_t *outCount, int32_t *outCapabilities) {
    uint32_t capabilityNum = 0;
#ifdef HWC_SUPPORT_COLOR_TRANSFORM
    capabilityNum++;
#endif
#ifndef HWC_NO_SUPPORT_SKIP_VALIDATE
    capabilityNum++;
#endif
    if (outCapabilities == NULL) {
        *outCount = capabilityNum;
        return;
    }
    if (capabilityNum != *outCount) {
        ALOGE("%s:: invalid outCount(%d), should be(%d)", __func__, *outCount, capabilityNum);
        return;
    }
#if defined(HWC_SUPPORT_COLOR_TRANSFORM) || !defined(HWC_NO_SUPPORT_SKIP_VALIDATE)
    uint32_t index = 0;
#endif
#ifdef HWC_SUPPORT_COLOR_TRANSFORM
    outCapabilities[index++] = HWC2_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
#endif
#ifndef HWC_NO_SUPPORT_SKIP_VALIDATE
    outCapabilities[index++] = HWC2_CAPABILITY_SKIP_VALIDATE;
#endif
    return;
}

void ExynosDevice::getCapabilities(uint32_t *outCount, int32_t *outCapabilities) {
    uint32_t capabilityNum = 0;
#ifdef HWC_SUPPORT_COLOR_TRANSFORM
    capabilityNum++;
#endif
    if (outCapabilities == NULL) {
        *outCount = capabilityNum;
        return;
    }
    if (capabilityNum != *outCount) {
        ALOGE("%s:: invalid outCount(%d), should be(%d)", __func__, *outCount, capabilityNum);
        return;
    }
#if defined(HWC_SUPPORT_COLOR_TRANSFORM)
    uint32_t index = 0;
    outCapabilities[index++] = HWC2_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
#endif
    return;
}

void ExynosDevice::clearGeometryChanged()
{
    mGeometryChanged = 0;
}

bool ExynosDevice::canSkipValidate()
{
    /*
     * This should be called by presentDisplay()
     * when presentDisplay() is called without validateDisplay() call
     */

    int ret = 0;
    if (exynosHWCControl.skipValidate == false)
        return false;

    for (uint32_t i = 0; i < mDisplays.size(); i++) {
        /*
         * Check all displays.
         * Resource assignment can have problem if validateDisplay is skipped
         * on only some displays.
         * All display's validateDisplay should be skipped or all display's validateDisplay
         * should not be skipped.
         */
        if (mDisplays[i]->mPlugState && mDisplays[i]->mPowerModeState.has_value() &&
            mDisplays[i]->mPowerModeState.value() != HWC2_POWER_MODE_OFF) {
            /*
             * presentDisplay is called without validateDisplay.
             * Call functions that should be called in validateDiplay
             */
            mDisplays[i]->doPreProcessing();
            mDisplays[i]->checkLayerFps();

            if ((ret = mDisplays[i]->canSkipValidate()) != NO_ERROR) {
                ALOGD_AND_ATRACE_NAME(eDebugSkipValidate,
                                      "Display[%d] can't skip validate (%d), renderingState(%d), "
                                      "geometryChanged(0x%" PRIx64 ")",
                                      mDisplays[i]->mDisplayId, ret, mDisplays[i]->mRenderingState,
                                      mGeometryChanged);
                return false;
            } else {
                HDEBUGLOGD(eDebugSkipValidate, "Display[%d] can skip validate (%d), renderingState(%d), geometryChanged(0x%" PRIx64 ")",
                        mDisplays[i]->mDisplayId, ret,
                        mDisplays[i]->mRenderingState, mGeometryChanged);
            }
        }
    }
    return true;
}

bool ExynosDevice::validateFences(ExynosDisplay *display) {
    return mFenceTracker.validateFences(display);
}

void ExynosDevice::compareVsyncPeriod() {
    /* TODO(b/265244856): to clarify what purpose of the function */
    ExynosDisplay *primary_display = getDisplay(getDisplayId(HWC_DISPLAY_PRIMARY, 0));
    ExynosDisplay *external_display = getDisplay(getDisplayId(HWC_DISPLAY_EXTERNAL, 0));

    mVsyncDisplayId = getDisplayId(HWC_DISPLAY_PRIMARY, 0);

    if ((external_display == nullptr) ||
        (!external_display->mPowerModeState.has_value() ||
         (external_display->mPowerModeState.value() == HWC2_POWER_MODE_OFF))) {
        return;
    } else if (!primary_display->mPowerModeState.has_value() ||
               (primary_display->mPowerModeState.value() == HWC2_POWER_MODE_OFF)) {
        mVsyncDisplayId = getDisplayId(HWC_DISPLAY_EXTERNAL, 0);
        return;
    } else if (primary_display->mPowerModeState.has_value() &&
               ((primary_display->mPowerModeState.value() == HWC2_POWER_MODE_DOZE) ||
                (primary_display->mPowerModeState.value() == HWC2_POWER_MODE_DOZE_SUSPEND)) &&
               (external_display->mVsyncPeriod >= DOZE_VSYNC_PERIOD)) { /*30fps*/
        mVsyncDisplayId = getDisplayId(HWC_DISPLAY_EXTERNAL, 0);
        return;
    } else if (primary_display->mVsyncPeriod <= external_display->mVsyncPeriod) {
        mVsyncDisplayId = getDisplayId(HWC_DISPLAY_EXTERNAL, 0);
        return;
    }

    return;
}

ExynosDevice::captureReadbackClass::captureReadbackClass(
        ExynosDevice *device) :
    mDevice(device)
{
    if (device == nullptr)
        return;
}

ExynosDevice::captureReadbackClass::~captureReadbackClass()
{
    VendorGraphicBufferMapper& gMapper(VendorGraphicBufferMapper::get());
    if (mBuffer != nullptr)
        gMapper.freeBuffer(mBuffer);

    if (mDevice != nullptr)
        mDevice->clearWaitingReadbackReqDone();
}


int32_t ExynosDevice::captureReadbackClass::allocBuffer(
        uint32_t format, uint32_t w, uint32_t h)
{
    VendorGraphicBufferAllocator& gAllocator(VendorGraphicBufferAllocator::get());

    uint32_t dstStride = 0;
    uint64_t usage = static_cast<uint64_t>(GRALLOC1_CONSUMER_USAGE_HWCOMPOSER |
            GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN);

    status_t error = NO_ERROR;
    error = gAllocator.allocate(w, h, format, 1, usage, &mBuffer, &dstStride, "HWC");
    if ((error != NO_ERROR) || (mBuffer == nullptr)) {
        ALOGE("failed to allocate destination buffer(%dx%d): %d",
                w, h, error);
        return static_cast<int32_t>(error);
    }
    return NO_ERROR;
}

void  ExynosDevice::captureReadbackClass::saveToFile(const String8 &fileName)
{
    if (mBuffer == nullptr) {
        ALOGE("%s:: buffer is null", __func__);
        return;
    }

    char filePath[MAX_DEV_NAME] = {0};
    VendorGraphicBufferMeta gmeta(mBuffer);

    snprintf(filePath, MAX_DEV_NAME,
            "%s/%s", WRITEBACK_CAPTURE_PATH, fileName.c_str());
    FILE *fp = fopen(filePath, "w");
    if (fp) {
        uint32_t writeSize =
            gmeta.stride * gmeta.vstride * formatToBpp(gmeta.format)/8;
        void *writebackData = mmap(0, writeSize,
                PROT_READ|PROT_WRITE, MAP_SHARED, gmeta.fd, 0);
        if (writebackData != MAP_FAILED && writebackData != NULL) {
            size_t result = fwrite(writebackData, writeSize, 1, fp);
            munmap(writebackData, writeSize);
            ALOGD("Success to write %zu data, size(%d)", result, writeSize);
        } else {
            ALOGE("Fail to mmap");
        }
        fclose(fp);
    } else {
        ALOGE("Fail to open %s", filePath);
    }
}

void ExynosDevice::signalReadbackDone()
{
    if (mIsWaitingReadbackReqDone) {
        Mutex::Autolock lock(mCaptureMutex);
        mCaptureCondition.signal();
    }
}

void ExynosDevice::captureScreenWithReadback(uint32_t displayId) {
    ExynosDisplay *display = getDisplay(displayId);
    if (display == nullptr) {
        ALOGE("There is no display(%d)", displayId);
        return;
    }

    int32_t outFormat;
    int32_t outDataspace;
    int32_t ret = 0;
    if ((ret = display->getReadbackBufferAttributes(
                &outFormat, &outDataspace)) != HWC2_ERROR_NONE) {
        ALOGE("getReadbackBufferAttributes fail, ret(%d)", ret);
        return;
    }

    captureReadbackClass captureClass(this);
    if ((ret = captureClass.allocBuffer(outFormat, display->mXres, display->mYres))
            != NO_ERROR) {
        return;
    }

    mIsWaitingReadbackReqDone = true;

    if (display->setReadbackBuffer(captureClass.getBuffer(), -1, true) != HWC2_ERROR_NONE) {
        ALOGE("setReadbackBuffer fail");
        return;
    }

    /* Update screen */
    onRefresh(displayId);

    /* Wait for handling readback */
    uint32_t waitPeriod = display->mVsyncPeriod * 3;
    {
        Mutex::Autolock lock(mCaptureMutex);
        status_t err = mCaptureCondition.waitRelative(
                mCaptureMutex, us2ns(waitPeriod));
        if (err == TIMED_OUT) {
            ALOGE("timeout, readback is not requested");
            return;
        } else if (err != NO_ERROR) {
            ALOGE("error waiting for readback request: %s (%d)", strerror(-err), err);
            return;
        } else {
            ALOGD("readback request is done");
        }
    }

    int32_t fence = -1;
    if (display->getReadbackBufferFence(&fence) != HWC2_ERROR_NONE) {
        ALOGE("getReadbackBufferFence fail");
        return;
    }
    if (sync_wait(fence, 1000) < 0) {
        ALOGE("sync wait error, fence(%d)", fence);
    }
    hwcFdClose(fence);

    String8 fileName;
    time_t curTime = time(NULL);
    struct tm *tm = localtime(&curTime);
    fileName.appendFormat("capture_format%d_%dx%d_%04d-%02d-%02d_%02d_%02d_%02d.raw",
            outFormat, display->mXres, display->mYres,
            tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
            tm->tm_hour, tm->tm_min, tm->tm_sec);
    captureClass.saveToFile(fileName);
}

int32_t ExynosDevice::setDisplayDeviceMode(int32_t display_id, int32_t mode)
{
    int32_t ret = HWC2_ERROR_NONE;

    for (size_t i = 0; i < mDisplays.size(); i++) {
        if (mDisplays[i]->mType == HWC_DISPLAY_PRIMARY && mDisplays[i]->mDisplayId == display_id) {
            if (mode == static_cast<int32_t>(ext_hwc2_power_mode_t::PAUSE) ||
                mode == static_cast<int32_t>(ext_hwc2_power_mode_t::RESUME)) {
                ret = mDisplays[i]->setPowerMode(mode);
                if (mode == static_cast<int32_t>(ext_hwc2_power_mode_t::RESUME) &&
                    ret == HWC2_ERROR_NONE) {
                    onRefresh(display_id);
                }
                return ret;
            } else {
                return HWC2_ERROR_UNSUPPORTED;
            }
        }
    }
    return HWC2_ERROR_UNSUPPORTED;
}

int32_t ExynosDevice::setPanelGammaTableSource(int32_t display_id, int32_t type, int32_t source) {
    if (display_id < HWC_DISPLAY_PRIMARY || display_id >= HWC_NUM_DISPLAY_TYPES) {
        ALOGE("invalid display %d", display_id);
        return HWC2_ERROR_BAD_DISPLAY;
    }

    if (type < static_cast<int32_t>(DisplayType::DISPLAY_PRIMARY) ||
        type >= static_cast<int32_t>(DisplayType::DISPLAY_MAX)) {
        ALOGE("invalid display type %d", type);
        return HWC2_ERROR_BAD_PARAMETER;
    }

    if (source < static_cast<int32_t>(PanelGammaSource::GAMMA_DEFAULT) ||
        source >= static_cast<int32_t>(PanelGammaSource::GAMMA_TYPES)) {
        ALOGE("invalid gamma source %d", source);
        return HWC2_ERROR_BAD_PARAMETER;
    }

    return mDisplays[display_id]->SetCurrentPanelGammaSource(static_cast<DisplayType>(type),
                                                             static_cast<PanelGammaSource>(source));
}

void ExynosDevice::getLayerGenericMetadataKey(uint32_t __unused keyIndex,
        uint32_t* outKeyLength, char* __unused outKey, bool* __unused outMandatory)
{
    *outKeyLength = 0;
    return;
}

void ExynosDevice::setVBlankOffDelay(int vblankOffDelay) {
    static constexpr const char *kVblankOffDelayPath = "/sys/module/drm/parameters/vblankoffdelay";

    writeIntToFile(kVblankOffDelayPath, vblankOffDelay);
}

uint32_t ExynosDevice::getWindowPlaneNum()
{
    /*
     * ExynosDevice supports DPU Window Composition.
     * The number of windows can be composited is depends on the number of DPP planes.
     */
    return mDeviceInterface->getNumDPPChs();
}

uint32_t ExynosDevice::getSpecialPlaneNum()
{
    /*
     * ExynosDevice might support something special purpose planes.
     * These planes are different with DPP planes.
     */
    return mDeviceInterface->getNumSPPChs();
}

uint32_t ExynosDevice::getSpecialPlaneNum(uint32_t /*displayId*/) {
    /*
     * TODO: create the query function for each display
     */
    return mDeviceInterface->getNumSPPChs();
}

uint32_t ExynosDevice::getSpecialPlaneId(uint32_t index)
{
    return mDeviceInterface->getSPPChId(index);
}

uint64_t ExynosDevice::getSpecialPlaneAttr(uint32_t index)
{
    return mDeviceInterface->getSPPChAttr(index);
}

int32_t ExynosDevice::registerHwc3Callback(uint32_t descriptor, hwc2_callback_data_t callbackData,
                                           hwc2_function_pointer_t point) {
    Mutex::Autolock lock(mDeviceCallbackMutex);
    mHwc3CallbackInfos[descriptor].callbackData = callbackData;
    mHwc3CallbackInfos[descriptor].funcPointer = point;

    return HWC2_ERROR_NONE;
}

void ExynosDevice::onVsyncIdle(hwc2_display_t displayId) {
    Mutex::Autolock lock(mDeviceCallbackMutex);
    const auto &idleCallback = mHwc3CallbackInfos.find(IComposerCallback::TRANSACTION_onVsyncIdle);

    if (idleCallback == mHwc3CallbackInfos.end()) return;

    const auto &callbackInfo = idleCallback->second;
    if (callbackInfo.funcPointer == nullptr || callbackInfo.callbackData == nullptr) return;

    auto callbackFunc =
            reinterpret_cast<void (*)(hwc2_callback_data_t callbackData,
                                      hwc2_display_t hwcDisplay)>(callbackInfo.funcPointer);
    callbackFunc(callbackInfo.callbackData, displayId);
}

void ExynosDevice::handleHotplug() {
    bool hpdStatus = false;

    for (size_t i = 0; i < mDisplays.size(); i++) {
        if (mDisplays[i] == nullptr) {
            continue;
        }

        if (mDisplays[i]->checkHotplugEventUpdated(hpdStatus)) {
            mDisplays[i]->handleHotplugEvent(hpdStatus);
            mDisplays[i]->hotplug();
            mDisplays[i]->invalidate();
        }
    }
}

void ExynosDevice::onRefreshRateChangedDebug(hwc2_display_t displayId, uint32_t vsyncPeriod) {
    Mutex::Autolock lock(mDeviceCallbackMutex);
    const auto &refreshRateCallback =
            mHwc3CallbackInfos.find(IComposerCallback::TRANSACTION_onRefreshRateChangedDebug);

    if (refreshRateCallback == mHwc3CallbackInfos.end()) return;

    const auto &callbackInfo = refreshRateCallback->second;
    if (callbackInfo.funcPointer == nullptr || callbackInfo.callbackData == nullptr) return;

    auto callbackFunc =
            reinterpret_cast<void (*)(hwc2_callback_data_t callbackData, hwc2_display_t hwcDisplay,
                                      hwc2_vsync_period_t)>(callbackInfo.funcPointer);
    callbackFunc(callbackInfo.callbackData, displayId, vsyncPeriod);
}