summaryrefslogtreecommitdiff
path: root/libhwc2.1/libdevice/ExynosLayer.cpp
blob: 5d5a614eb5789ed6d78fb6d185fe990c3f203e4e (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
/*
 * 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.
 */

#include <aidl/android/hardware/graphics/common/BufferUsage.h>
#include <utils/Errors.h>
#include <linux/videodev2.h>
#include <sys/mman.h>
#include <hardware/hwcomposer_defs.h>
#include <hardware/exynos/ion.h>

#include "BrightnessController.h"
#include "ExynosLayer.h"
#include "ExynosResourceManager.h"
#include "ExynosHWCDebug.h"
#include "ExynosExternalDisplay.h"

#include "VendorVideoAPI.h"

/**
 * ExynosLayer implementation
 */

using AidlBufferUsage = ::aidl::android::hardware::graphics::common::BufferUsage;

ExynosLayer::ExynosLayer(ExynosDisplay* display)
      : ExynosMPPSource(MPP_SOURCE_LAYER, this),
        mDisplay(display),
        mCompositionType(HWC2_COMPOSITION_INVALID),
        mRequestedCompositionType(HWC2_COMPOSITION_INVALID),
        mExynosCompositionType(HWC2_COMPOSITION_INVALID),
        mValidateCompositionType(HWC2_COMPOSITION_INVALID),
        mPrevValidateCompositionType(HWC2_COMPOSITION_INVALID),
        mValidateExynosCompositionType(HWC2_COMPOSITION_INVALID),
        mOverlayInfo(0x0),
        mSupportedMPPFlag(0x0),
        mFps(0),
        mOverlayPriority(ePriorityLow),
        mGeometryChanged(0x0),
        mWindowIndex(0),
        mCompressionInfo({COMP_TYPE_NONE, 0}),
        mAcquireFence(-1),
        mPrevAcquireFence(-1),
        mReleaseFence(-1),
        mFrameCount(0),
        mLastFrameCount(0),
        mLastFpsTime(0),
        mNextLastFrameCount(0),
        mNextLastFpsTime(0),
        mLastLayerBuffer(NULL),
        mLayerBuffer(NULL),
        mLastUpdateTime(0),
        mDamageNum(0),
        mBlending(HWC2_BLEND_MODE_NONE),
        mPlaneAlpha(1.0),
        mTransform(0),
        mZOrder(0),
        mDataSpace(HAL_DATASPACE_UNKNOWN),
        mLayerFlag(0x0),
        mIsHdrLayer(false),
        mBufferHasMetaParcel(false),
        mMetaParcelFd(-1) {
    memset(&mDisplayFrame, 0, sizeof(mDisplayFrame));
    memset(&mSourceCrop, 0, sizeof(mSourceCrop));
    mVisibleRegionScreen.numRects = 0;
    mVisibleRegionScreen.rects = NULL;
    memset(&mColor, 0, sizeof(mColor));
    memset(&mPreprocessedInfo, 0, sizeof(mPreprocessedInfo));
    mCheckMPPFlag.clear();
    mCheckMPPFlag.reserve(MPP_LOGICAL_TYPE_NUM);
    mMetaParcel = NULL;
    mDamageRects.clear();
}

ExynosLayer::~ExynosLayer() {
    if (mMetaParcel != NULL) {
        munmap(mMetaParcel, sizeof(ExynosVideoMeta));
        mMetaParcel = NULL;
    }

    if (mMetaParcelFd >= 0) {
        close(mMetaParcelFd);
        mMetaParcelFd = -1;
    }

    if (mAcquireFence >= 0) {
        mAcquireFence =
                fence_close(mAcquireFence, mDisplay, FENCE_TYPE_SRC_ACQUIRE, FENCE_IP_UNDEFINED);
    }

    if (mPrevAcquireFence != -1)
        mPrevAcquireFence = fence_close(mPrevAcquireFence, mDisplay, FENCE_TYPE_SRC_ACQUIRE,
                                        FENCE_IP_UNDEFINED);
}

/**
 * @return float
 */
float ExynosLayer::checkFps(bool increaseCount) {
    uint32_t frameDiff;
    mFrameCount += increaseCount ? 1 : 0;

    nsecs_t now = systemTime();
    if (mLastFpsTime == 0) { // Initialize values
        mLastFpsTime = now;
        mNextLastFpsTime = now;
        // TODO(b/268474771): set the initial FPS to the correct peak refresh rate
        mFps = 120;
        return mFps;
    }

    nsecs_t diff = now - mNextLastFpsTime;
    // Update mLastFrameCount for every 5s, to ensure that FPS calculation is only based on
    // frames in the past at most 10s.
    if (diff >= kLayerFpsStableTimeNs) {
        mLastFrameCount = mNextLastFrameCount;
        mNextLastFrameCount = mFrameCount;

        mLastFpsTime = mNextLastFpsTime;
        mNextLastFpsTime = now;
    }

    bool wasLowFps = (mFps < LOW_FPS_THRESHOLD) ? true : false;

    if (mFrameCount >= mLastFrameCount)
        frameDiff = (mFrameCount - mLastFrameCount);
    else
        frameDiff = (mFrameCount + (UINT_MAX - mLastFrameCount));

    diff = now - mLastFpsTime;
    mFps = (frameDiff * float(s2ns(1))) / diff;

    bool nowLowFps = (mFps < LOW_FPS_THRESHOLD) ? true : false;

    if ((mDisplay->mDisplayControl.handleLowFpsLayers) &&
        (wasLowFps != nowLowFps))
        setGeometryChanged(GEOMETRY_LAYER_FPS_CHANGED);

    return mFps;
}

/**
 * @return float
 */
float ExynosLayer::getFps() {
    return mFps;
}

int32_t ExynosLayer::doPreProcess()
{
    overlay_priority priority = ePriorityLow;
    mIsHdrLayer = false;
    mBufferHasMetaParcel = false;
    mLayerFlag = 0x0;

    mPreprocessedInfo.preProcessed = false;
    mPreprocessedInfo.sourceCrop = mSourceCrop;
    mPreprocessedInfo.displayFrame = mDisplayFrame;
    mPreprocessedInfo.interlacedType = V4L2_FIELD_NONE;
    mPreprocessedInfo.sdrDimRatio = mBrightness;

    if (mCompositionType == HWC2_COMPOSITION_SOLID_COLOR) {
        mLayerFlag |= EXYNOS_HWC_DIM_LAYER;
    } else {
        mLayerFlag &= ~(EXYNOS_HWC_DIM_LAYER);
    }

    if (mLayerBuffer == NULL) {
        if (mOverlayPriority != priority)
            setGeometryChanged(GEOMETRY_LAYER_PRIORITY_CHANGED);

        mOverlayPriority = priority;
        return NO_ERROR;
    }

    VendorGraphicBufferMeta gmeta(mLayerBuffer);

    mPreprocessedInfo.mUsePrivateFormat = false;
    mPreprocessedInfo.mPrivateFormat = gmeta.format;

    if (isFormatYUV(gmeta.format)) {
        mPreprocessedInfo.sourceCrop.top = (int)mSourceCrop.top;
        mPreprocessedInfo.sourceCrop.left = (int)mSourceCrop.left;
        mPreprocessedInfo.sourceCrop.bottom = (int)(mSourceCrop.bottom + 0.9);
        mPreprocessedInfo.sourceCrop.right = (int)(mSourceCrop.right + 0.9);
        mPreprocessedInfo.preProcessed = true;
    }

    if (isFormatYUV(gmeta.format)) {

        ExynosVideoMeta *metaData = NULL;
        int priv_fd = -1;

        if (gmeta.flags & VendorGraphicBufferMeta::PRIV_FLAGS_USES_2PRIVATE_DATA)
            priv_fd = gmeta.fd1;
        else if (gmeta.flags & VendorGraphicBufferMeta::PRIV_FLAGS_USES_3PRIVATE_DATA)
            priv_fd = gmeta.fd2;

        if (priv_fd >= 0) {

            metaData = (ExynosVideoMeta*)mmap(0, sizeof(ExynosVideoMeta), PROT_READ|PROT_WRITE, MAP_SHARED, priv_fd, 0);

            if (metaData == NULL) {
                HWC_LOGE(mDisplay, "Layer's metadata is NULL!!");
            } else if (metaData == MAP_FAILED) {
                HWC_LOGE(mDisplay, "Layer's metadata map failed!!");
            } else {
                mBufferHasMetaParcel = true;
                if ((metaData->eType & VIDEO_INFO_TYPE_HDR_STATIC) ||
                        (metaData->eType & VIDEO_INFO_TYPE_HDR_DYNAMIC)) {
                    if (allocMetaParcel() == NO_ERROR) {
                        mMetaParcel->eType = metaData->eType;
                        if (metaData->eType & VIDEO_INFO_TYPE_HDR_STATIC) {
                            mMetaParcel->sHdrStaticInfo = metaData->sHdrStaticInfo;
                            HDEBUGLOGD(eDebugLayer, "HWC2: Static metadata min(%d), max(%d)",
                                    mMetaParcel->sHdrStaticInfo.sType1.mMinDisplayLuminance,
                                    mMetaParcel->sHdrStaticInfo.sType1.mMaxDisplayLuminance);
                        }
                        if (metaData->eType & VIDEO_INFO_TYPE_HDR_DYNAMIC) {
                            /* Reserved field for dynamic meta data */
                            /* Currently It's not be used not only HWC but also OMX */
                            mMetaParcel->sHdrDynamicInfo = metaData->sHdrDynamicInfo;
                            HDEBUGLOGD(eDebugLayer, "HWC2: Layer has dynamic metadata");
                        }
                    }
                }
                if (metaData->eType & VIDEO_INFO_TYPE_INTERLACED) {
                    mPreprocessedInfo.interlacedType = metaData->data.dec.nInterlacedType;
                    if (mPreprocessedInfo.interlacedType == V4L2_FIELD_INTERLACED_BT) {
                        if ((int)mSourceCrop.left < (int)(gmeta.stride)) {
                            mPreprocessedInfo.sourceCrop.left = (int)mSourceCrop.left + gmeta.stride;
                            mPreprocessedInfo.sourceCrop.right = (int)mSourceCrop.right + gmeta.stride;
                        }
                    }
                    if (mPreprocessedInfo.interlacedType == V4L2_FIELD_INTERLACED_TB ||
                            mPreprocessedInfo.interlacedType == V4L2_FIELD_INTERLACED_BT) {
                        mPreprocessedInfo.sourceCrop.top = (int)(mSourceCrop.top)/2;
                        mPreprocessedInfo.sourceCrop.bottom = (int)(mSourceCrop.bottom)/2;
                    }
                }
                if (metaData->eType & VIDEO_INFO_TYPE_CHECK_PIXEL_FORMAT) {
                    mPreprocessedInfo.mUsePrivateFormat = true;
                    mPreprocessedInfo.mPrivateFormat = metaData->nPixelFormat;
                }
                munmap(metaData, sizeof(ExynosVideoMeta));
            }
        }
        mPreprocessedInfo.preProcessed = true;
    }

    exynos_image src_img;
    exynos_image dst_img;
    setSrcExynosImage(&src_img);
    setDstExynosImage(&dst_img);
    ExynosMPP *exynosMPPVG = nullptr;
    if (isFormatYUV(gmeta.format)) {
        auto otfMPPs = ExynosResourceManager::getOtfMPPs();
        auto mpp_it = std::find_if(otfMPPs.begin(), otfMPPs.end(),
                [&src_img](auto m) { return m->isSrcFormatSupported(src_img); });
        exynosMPPVG = mpp_it == otfMPPs.end() ? nullptr : *mpp_it;
    }

    /* Set HDR Flag */
    if(hasHdrInfo(src_img)) mIsHdrLayer = true;

    if (isFormatYUV(gmeta.format) && exynosMPPVG) {
        /*
         * layer's sourceCrop should be aligned
         */
        uint32_t srcCropXAlign = exynosMPPVG->getSrcXOffsetAlign(src_img);
        uint32_t srcCropYAlign = exynosMPPVG->getSrcYOffsetAlign(src_img);
        uint32_t srcCropWidthAlign = exynosMPPVG->getSrcWidthAlign(src_img);
        uint32_t srcCropHeightAlign = exynosMPPVG->getSrcHeightAlign(src_img);
        mPreprocessedInfo.sourceCrop.left = pixel_align((int)mPreprocessedInfo.sourceCrop.left, srcCropXAlign);
        mPreprocessedInfo.sourceCrop.top = pixel_align((int)mPreprocessedInfo.sourceCrop.top, srcCropYAlign);
        mPreprocessedInfo.sourceCrop.right = mPreprocessedInfo.sourceCrop.left +
            pixel_align_down(WIDTH(mPreprocessedInfo.sourceCrop), srcCropWidthAlign);
        mPreprocessedInfo.sourceCrop.bottom = mPreprocessedInfo.sourceCrop.top +
            pixel_align_down(HEIGHT(mPreprocessedInfo.sourceCrop), srcCropHeightAlign);
        mPreprocessedInfo.preProcessed = true;
    }

    if (exynosMPPVG && ((getDrmMode(mLayerBuffer) != NO_DRM) ||
        (mIsHdrLayer == true)))
    {
        if ((mDisplay->mDisplayControl.adjustDisplayFrame == true) &&
            ((mSupportedMPPFlag & (MPP_LOGICAL_DPP_G | MPP_LOGICAL_DPP_VG | MPP_LOGICAL_DPP_VGFS | MPP_LOGICAL_DPP_VGRFS)) == 0))
        {
            /*
             * M2mMPP should be used for DRM, HDR video
             * layer's displayFrame is the source of DPP
             */
            uint32_t cropWidthAlign = exynosMPPVG->getSrcCropWidthAlign(src_img);
            uint32_t cropHeightAlign = exynosMPPVG->getSrcCropHeightAlign(src_img);

            mPreprocessedInfo.displayFrame.right = mDisplayFrame.left +
                pixel_align(WIDTH(mDisplayFrame), cropWidthAlign);
            mPreprocessedInfo.displayFrame.bottom = mDisplayFrame.top +
                pixel_align(HEIGHT(mDisplayFrame), cropHeightAlign);

            if (mPreprocessedInfo.displayFrame.right > (int)(mDisplay->mXres)) {
                mPreprocessedInfo.displayFrame.left = mDisplay->mXres -
                    pixel_align(WIDTH(mPreprocessedInfo.displayFrame), cropWidthAlign);
                mPreprocessedInfo.displayFrame.right = mDisplay->mXres;
            }

            if (mPreprocessedInfo.displayFrame.bottom > (int)(mDisplay->mYres)) {
                mPreprocessedInfo.displayFrame.top = mDisplay->mYres -
                    pixel_align_down(HEIGHT(mPreprocessedInfo.displayFrame), cropHeightAlign);
                mPreprocessedInfo.displayFrame.bottom = mDisplay->mYres;
            }
        }

        uint32_t minDstWidth = exynosMPPVG->getDstMinWidth(dst_img);
        uint32_t minDstHeight = exynosMPPVG->getDstMinHeight(dst_img);
        if ((uint32_t)WIDTH(mDisplayFrame) < minDstWidth) {
            ALOGI("%s DRM layer displayFrame width %d is smaller than otf minWidth %d",
                    mDisplay->mDisplayName.c_str(),
                    WIDTH(mDisplayFrame), minDstWidth);
            mPreprocessedInfo.displayFrame.right = mDisplayFrame.left +
                pixel_align(WIDTH(mDisplayFrame), minDstWidth);

            if (mPreprocessedInfo.displayFrame.right > (int)(mDisplay->mXres)) {
                mPreprocessedInfo.displayFrame.left = mDisplay->mXres -
                    pixel_align(WIDTH(mPreprocessedInfo.displayFrame), minDstWidth);
                mPreprocessedInfo.displayFrame.right = mDisplay->mXres;
            }
        }
        if ((uint32_t)HEIGHT(mDisplayFrame) < minDstHeight) {
            ALOGI("%s DRM layer displayFrame height %d is smaller than vpp minHeight %d",
                    mDisplay->mDisplayName.c_str(),
                    HEIGHT(mDisplayFrame), minDstHeight);
            mPreprocessedInfo.displayFrame.bottom = mDisplayFrame.top +
                pixel_align(HEIGHT(mDisplayFrame), minDstHeight);

            if (mPreprocessedInfo.displayFrame.bottom > (int)(mDisplay->mYres)) {
                mPreprocessedInfo.displayFrame.top = mDisplay->mYres -
                    pixel_align(HEIGHT(mPreprocessedInfo.displayFrame), minDstHeight);
                mPreprocessedInfo.displayFrame.bottom = mDisplay->mYres;
            }
        }
        mPreprocessedInfo.preProcessed = true;
    }

    if (VendorGraphicBufferMeta::get_usage(mLayerBuffer) &
               toUnderlying(AidlBufferUsage::FRONT_BUFFER)) {
        priority = ePriorityMax;
    } else if (getDrmMode(mLayerBuffer) != NO_DRM) {
        priority = ePriorityMax;
    } else if (mIsHdrLayer) {
        if (isFormatRgb(gmeta.format))
            priority = ePriorityMax;
        else
            priority = ePriorityHigh;
    } else if (isFormatYUV(gmeta.format)) {
        priority = ePriorityHigh;
    } else if ((mDisplay->mDisplayControl.cursorSupport == true) &&
               (mCompositionType == HWC2_COMPOSITION_CURSOR)) {
        priority = ePriorityMid;
    } else {
        priority = ePriorityLow;
    }

    if (mOverlayPriority != priority)
        setGeometryChanged(GEOMETRY_LAYER_PRIORITY_CHANGED);

    mOverlayPriority = priority;

    return NO_ERROR;
}

int32_t ExynosLayer::setCursorPosition(int32_t x, int32_t y) {
    return mDisplay->setCursorPositionAsync(x, y);
}


int32_t ExynosLayer::setLayerBuffer(buffer_handle_t buffer, int32_t acquireFence) {

    /* TODO : Exception here ? */
    //TODO mGeometryChanged  here

    uint64_t internal_format = 0;

    if (mDisplay->mPlugState == false)
        buffer = NULL;

    if (buffer != NULL) {
        if (VendorGraphicBufferMeta::get_fd(buffer,0) < 0)
            return HWC2_ERROR_BAD_LAYER;
    }

    VendorGraphicBufferMeta gmeta(mLayerBuffer);
    internal_format = gmeta.format;

    if ((mLayerBuffer == NULL) || (buffer == NULL))
        setGeometryChanged(GEOMETRY_LAYER_UNKNOWN_CHANGED);
    else {
        if (getDrmMode(VendorGraphicBufferMeta::get_producer_usage(mLayerBuffer)) != getDrmMode(gmeta.producer_usage))
            setGeometryChanged(GEOMETRY_LAYER_DRM_CHANGED);
        if (VendorGraphicBufferMeta::get_format(mLayerBuffer) != gmeta.format)
            setGeometryChanged(GEOMETRY_LAYER_FORMAT_CHANGED);
        if ((VendorGraphicBufferMeta::get_usage(buffer) &
                    toUnderlying(AidlBufferUsage::FRONT_BUFFER)) !=
                (VendorGraphicBufferMeta::get_usage(mLayerBuffer) &
                    toUnderlying(AidlBufferUsage::FRONT_BUFFER)))
            setGeometryChanged(GEOMETRY_LAYER_FRONT_BUFFER_USAGE_CHANGED);
    }

    {
        Mutex::Autolock lock(mDisplay->mDRMutex);
        mLayerBuffer = buffer;
        checkFps(mLastLayerBuffer != mLayerBuffer);
        if (mLayerBuffer != mLastLayerBuffer) {
            mLastUpdateTime = systemTime(CLOCK_MONOTONIC);
            if (mRequestedCompositionType != HWC2_COMPOSITION_REFRESH_RATE_INDICATOR)
                mDisplay->mBufferUpdates++;
        }
    }
    mPrevAcquireFence =
            fence_close(mPrevAcquireFence, mDisplay, FENCE_TYPE_SRC_ACQUIRE, FENCE_IP_UNDEFINED);
    mAcquireFence = fence_close(mAcquireFence, mDisplay, FENCE_TYPE_SRC_ACQUIRE, FENCE_IP_UNDEFINED);

    mAcquireFence = hwcCheckFenceDebug(mDisplay, FENCE_TYPE_SRC_ACQUIRE, FENCE_IP_LAYER, acquireFence);
    mPrevAcquireFence = hwcCheckFenceDebug(mDisplay, FENCE_TYPE_SRC_ACQUIRE, FENCE_IP_LAYER,
                                           hwc_dup(mAcquireFence, mDisplay, FENCE_TYPE_SRC_ACQUIRE,
                                                   FENCE_IP_LAYER, true));
    if (mReleaseFence >= 0)
        HWC_LOGE(NULL, "Layer's release fence is not initialized");
    mReleaseFence = -1;
#ifdef DISABLE_FENCE
    if (mAcquireFence >= 0)
        fence_close(mAcquireFence);
    mAcquireFence = -1;
#endif

    /* Set Compression Information from GraphicBuffer */
    uint32_t prevCompressionType = mCompressionInfo.type;
    mCompressionInfo = getCompressionInfo(mLayerBuffer);
    if (mCompressionInfo.type != prevCompressionType)
        setGeometryChanged(GEOMETRY_LAYER_COMPRESSED_CHANGED);

    if (buffer != NULL) {
        /*
         * HAL_DATASPACE_V0_JFIF = HAL_DATASPACE_STANDARD_BT601_625 |
         * HAL_DATASPACE_TRANSFER_SMPTE_170M | HAL_DATASPACE_RANGE_FULL,
         */
        if (gmeta.format == HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_FULL)
            setLayerDataspace(HAL_DATASPACE_V0_JFIF);
    } else {
        setLayerDataspace(HAL_DATASPACE_UNKNOWN);
    }

    HDEBUGLOGD(eDebugFence,
               "layers bufferHandle: %p, mDataSpace: 0x%8x, acquireFence: %d, compressionType: "
               "0x%x, internal_format: 0x%" PRIx64 "",
               mLayerBuffer, mDataSpace, mAcquireFence, mCompressionInfo.type, internal_format);

    return 0;
}


int32_t ExynosLayer::setLayerSurfaceDamage(hwc_region_t damage) {

    mDamageNum = damage.numRects;
    mDamageRects.clear();

    if (mDamageNum == 0) return 0;

    for (size_t i = 0; i<mDamageNum; i++){
        mDamageRects.push_back(damage.rects[i]);
    }

    return 0;
}

int32_t ExynosLayer::setLayerBlendMode(int32_t /*hwc2_blend_mode_t*/ mode) {

    //TODO mGeometryChanged  here
    if (mode < 0)
        return HWC2_ERROR_BAD_PARAMETER;
    if (mBlending != mode)
        setGeometryChanged(GEOMETRY_LAYER_BLEND_CHANGED);
    mBlending = mode;
    return HWC2_ERROR_NONE;
}


int32_t ExynosLayer::setLayerColor(hwc_color_t color) {
    /* TODO : Implementation here */
    mColor = color;
    return 0;
}

int32_t ExynosLayer::setLayerCompositionType(int32_t /*hwc2_composition_t*/ type) {

    if (type < 0)
        return HWC2_ERROR_BAD_PARAMETER;

    // FIXME: HWC2_COMPOSITION_SCREENSHOT is not defined in AOSP
    //        HWC guys should fix this.
#if 0
    if (mDisplay->mType == HWC_DISPLAY_PRIMARY)
        if (type == HWC2_COMPOSITION_SCREENSHOT)
            type = HWC2_COMPOSITION_DEVICE;
#endif

    if (type != mCompositionType) {
        setGeometryChanged(GEOMETRY_LAYER_TYPE_CHANGED);
    }

    mCompositionType = type;
    mRequestedCompositionType = type;

    return HWC2_ERROR_NONE;
}

int32_t ExynosLayer::setLayerDataspace(int32_t /*android_dataspace_t*/ dataspace) {
    android_dataspace currentDataSpace = (android_dataspace_t)dataspace;
    if ((mLayerBuffer != NULL) && (VendorGraphicBufferMeta::get_format(mLayerBuffer) == HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_FULL))
        currentDataSpace = HAL_DATASPACE_V0_JFIF;
    else {
        /* Change legacy dataspace */
        switch (dataspace) {
        case HAL_DATASPACE_SRGB_LINEAR:
            currentDataSpace = HAL_DATASPACE_V0_SRGB_LINEAR;
            break;
        case HAL_DATASPACE_SRGB:
            currentDataSpace = HAL_DATASPACE_V0_SRGB;
            break;
        case HAL_DATASPACE_JFIF:
            currentDataSpace = HAL_DATASPACE_V0_JFIF;
            break;
        case HAL_DATASPACE_BT601_625:
            currentDataSpace = HAL_DATASPACE_V0_BT601_625;
            break;
        case HAL_DATASPACE_BT601_525:
            currentDataSpace = HAL_DATASPACE_V0_BT601_525;
            break;
        case HAL_DATASPACE_BT709:
            currentDataSpace = HAL_DATASPACE_V0_BT709;
            break;
        default:
            currentDataSpace = (android_dataspace)dataspace;
            break;
        }
    }

    if (currentDataSpace != mDataSpace) {
        setGeometryChanged(GEOMETRY_LAYER_DATASPACE_CHANGED);
        // invalidate metadata if dataspace is changed. need metadata update
        // to be after dataspace update.
        if (mMetaParcel != nullptr) {
            mMetaParcel->eType = VIDEO_INFO_TYPE_INVALID;
        }
    }
    mDataSpace = currentDataSpace;

    return HWC2_ERROR_NONE;
}

int32_t ExynosLayer::setLayerDisplayFrame(hwc_rect_t frame) {

    if ((frame.left != mDisplayFrame.left) ||
        (frame.top != mDisplayFrame.top) ||
        (frame.right != mDisplayFrame.right) ||
        (frame.bottom != mDisplayFrame.bottom))
        setGeometryChanged(GEOMETRY_LAYER_DISPLAYFRAME_CHANGED);
    mDisplayFrame = frame;

    return HWC2_ERROR_NONE;
}

int32_t ExynosLayer::setLayerPlaneAlpha(float alpha) {
    if (alpha < 0.0f || alpha > 1.0f) {
        ALOGE("%s: invalid alpha %f", __func__, alpha);
        return HWC2_ERROR_BAD_PARAMETER;
    }

    if ((mPlaneAlpha != alpha) && ((mPlaneAlpha == 0.0) || (alpha == 0.0)))
        setGeometryChanged(GEOMETRY_LAYER_IGNORE_CHANGED);

    mPlaneAlpha = alpha;

    if (mPlaneAlpha > 0.0)
        mLayerFlag &= ~(EXYNOS_HWC_IGNORE_LAYER);
    else
        mLayerFlag |= EXYNOS_HWC_IGNORE_LAYER;

    return HWC2_ERROR_NONE;
}

int32_t ExynosLayer::setLayerSidebandStream(const native_handle_t* __unused stream) {
    return HWC2_ERROR_NONE;
}

int32_t ExynosLayer::setLayerSourceCrop(hwc_frect_t crop) {

    if ((crop.left != mSourceCrop.left) ||
        (crop.top != mSourceCrop.top) ||
        (crop.right != mSourceCrop.right) ||
        (crop.bottom != mSourceCrop.bottom)) {
        setGeometryChanged(GEOMETRY_LAYER_SOURCECROP_CHANGED);
        mSourceCrop = crop;
    }

    return HWC2_ERROR_NONE;
}

int32_t ExynosLayer::setLayerTransform(int32_t /*hwc_transform_t*/ transform) {

    if (mTransform != transform) {
        setGeometryChanged(GEOMETRY_LAYER_TRANSFORM_CHANGED);
        mTransform = transform;
    }

    return HWC2_ERROR_NONE;
}

int32_t ExynosLayer::setLayerVisibleRegion(hwc_region_t visible) {

    mVisibleRegionScreen = visible;

    return HWC2_ERROR_NONE;
}

int32_t ExynosLayer::setLayerZOrder(uint32_t z) {
    if (mZOrder != z) {
        setGeometryChanged(GEOMETRY_LAYER_ZORDER_CHANGED);
        mZOrder = z;
    }
    return HWC2_ERROR_NONE;
}

int32_t ExynosLayer::setLayerPerFrameMetadata(uint32_t numElements,
        const int32_t* /*hw2_per_frame_metadata_key_t*/ keys, const float* metadata)
{
    if (allocMetaParcel() != NO_ERROR)
        return -1;
    unsigned int multipliedVal = 50000;
    mMetaParcel->eType =
        static_cast<ExynosVideoInfoType>(mMetaParcel->eType | VIDEO_INFO_TYPE_HDR_STATIC);
    for (uint32_t i = 0; i < numElements; i++) {
        HDEBUGLOGD(eDebugLayer, "HWC2: setLayerPerFrameMetadata key(%d), value(%7.5f)",
                keys[i], metadata[i]);
        switch (keys[i]) {
            case HWC2_DISPLAY_RED_PRIMARY_X:
                mMetaParcel->sHdrStaticInfo.sType1.mR.x =
                    (unsigned int)(metadata[i] * multipliedVal);
                break;
            case HWC2_DISPLAY_RED_PRIMARY_Y:
                mMetaParcel->sHdrStaticInfo.sType1.mR.y =
                    (unsigned int)(metadata[i] * multipliedVal);
                break;
            case HWC2_DISPLAY_GREEN_PRIMARY_X:
                mMetaParcel->sHdrStaticInfo.sType1.mG.x =
                    (unsigned int)(metadata[i] * multipliedVal);
                break;
            case HWC2_DISPLAY_GREEN_PRIMARY_Y:
                mMetaParcel->sHdrStaticInfo.sType1.mG.y =
                    (unsigned int)(metadata[i] * multipliedVal);
                break;
            case HWC2_DISPLAY_BLUE_PRIMARY_X:
                mMetaParcel->sHdrStaticInfo.sType1.mB.x =
                    (unsigned int)(metadata[i] * multipliedVal);
                break;
            case HWC2_DISPLAY_BLUE_PRIMARY_Y:
                mMetaParcel->sHdrStaticInfo.sType1.mB.y =
                    (unsigned int)(metadata[i] * multipliedVal);
                break;
            case HWC2_WHITE_POINT_X:
                mMetaParcel->sHdrStaticInfo.sType1.mW.x =
                    (unsigned int)(metadata[i] * multipliedVal);
                break;
            case HWC2_WHITE_POINT_Y:
                mMetaParcel->sHdrStaticInfo.sType1.mW.y =
                    (unsigned int)(metadata[i] * multipliedVal);
                break;
            case HWC2_MAX_LUMINANCE:
                mMetaParcel->sHdrStaticInfo.sType1.mMaxDisplayLuminance =
                    (unsigned int)(metadata[i] * 10000);
                break;
            case HWC2_MIN_LUMINANCE:
                mMetaParcel->sHdrStaticInfo.sType1.mMinDisplayLuminance =
                    (unsigned int)(metadata[i] * 10000);
                break;
            case HWC2_MAX_CONTENT_LIGHT_LEVEL:
                /* Should be checked */
                mMetaParcel->sHdrStaticInfo.sType1.mMaxContentLightLevel =
                    (unsigned int)(metadata[i]);
                break;
            case HWC2_MAX_FRAME_AVERAGE_LIGHT_LEVEL:
                /* Should be checked */
                mMetaParcel->sHdrStaticInfo.sType1.mMaxFrameAverageLightLevel =
                    (unsigned int)(metadata[i]);
                break;
            default:
                return HWC2_ERROR_UNSUPPORTED;
        }
    }
    return NO_ERROR;
}

int32_t ExynosLayer::setLayerPerFrameMetadataBlobs(uint32_t numElements, const int32_t* keys, const uint32_t* sizes,
        const uint8_t* metadata)
{
    const uint8_t *metadata_start = metadata;
    for (uint32_t i = 0; i < numElements; i++) {
        HDEBUGLOGD(eDebugLayer, "HWC2: setLayerPerFrameMetadataBlobs key(%d)", keys[i]);
        switch (keys[i]) {
        case HWC2_HDR10_PLUS_SEI:
            if (allocMetaParcel() == NO_ERROR) {
                mMetaParcel->eType =
                    static_cast<ExynosVideoInfoType>(mMetaParcel->eType | VIDEO_INFO_TYPE_HDR_DYNAMIC);
                ExynosHdrDynamicInfo *info = &(mMetaParcel->sHdrDynamicInfo);
                Exynos_parsing_user_data_registered_itu_t_t35(info, (void*)metadata_start,
                                                              sizes[i]);
            } else {
                ALOGE("Layer has no metaParcel!");
                return HWC2_ERROR_UNSUPPORTED;
            }
            break;
        default:
            return HWC2_ERROR_BAD_PARAMETER;
        }
        metadata_start += sizes[i];
    }
    return HWC2_ERROR_NONE;
}

int32_t ExynosLayer::setLayerColorTransform(const float* matrix)
{
    mLayerColorTransform.enable = true;
    for (uint32_t i = 0; i < TRANSFORM_MAT_SIZE; i++)
    {
        mLayerColorTransform.mat[i] = matrix[i];
    }

    return 0;
}

int32_t ExynosLayer::setLayerGenericMetadata(hwc2_layer_t __unused layer,
        uint32_t __unused keyLength, const char* __unused key,
        bool __unused mandatory, uint32_t __unused valueLength, const uint8_t* __unused value)
{
    return HWC2_ERROR_UNSUPPORTED;
}

int32_t ExynosLayer::setLayerBrightness(float brightness) {
    if (mDisplay->mType == HWC_DISPLAY_EXTERNAL && mDisplay->mBrightnessController == nullptr) {
        if (brightness == 1.0f) {
            return HWC2_ERROR_NONE;
        } else {
            HWC_LOGE(mDisplay, "[ExternalDisplay layer] setLayerBrightness != 1.0");
            return HWC2_ERROR_BAD_PARAMETER;
        }
    }

    if (mDisplay->mBrightnessController == nullptr ||
        !mDisplay->mBrightnessController->validateLayerBrightness(brightness)) {
        return HWC2_ERROR_BAD_PARAMETER;
    }

    if (mBrightness != brightness) {
        // Trigger display validation in case client composition is needed.
        setGeometryChanged(GEOMETRY_LAYER_WHITEPOINT_CHANGED);
        mBrightness = brightness;
    }
    return HWC2_ERROR_NONE;
}

int32_t ExynosLayer::setLayerBlockingRegion(const std::vector<hwc_rect_t>& blockingRegion) {
    hwc_rect_t maxRect;

    for (auto rect : blockingRegion) {
        maxRect = std::max(maxRect, rect, [](const hwc_rect_t& lhs, const hwc_rect_t& rhs) {
            return rectSize(lhs) < rectSize(rhs);
        });
    }

    mBlockingRect = maxRect;

    return HWC2_ERROR_NONE;
}

void ExynosLayer::resetValidateData()
{
    mValidateCompositionType = HWC2_COMPOSITION_INVALID;
    mOtfMPP = NULL;
    mM2mMPP = NULL;
    mOverlayInfo = 0x0;
    mWindowIndex = 0;
}

int32_t ExynosLayer::setSrcExynosImage(exynos_image *src_img)
{
    buffer_handle_t handle = mLayerBuffer;
    if (isDimLayer()) {
        src_img->format = HAL_PIXEL_FORMAT_RGBA_8888;
        src_img->usageFlags = 0xb00;
        src_img->bufferHandle = 0;

        src_img->x = 0;
        src_img->y = 0;

        if (mDisplay != NULL) {
            src_img->fullWidth = src_img->w = mDisplay->mXres;
            src_img->fullHeight = src_img->h = mDisplay->mYres;
        } else {
            src_img->fullWidth = src_img->w = 1440;
            src_img->fullHeight = src_img->h = 2560;
        }

        src_img->layerFlags = mLayerFlag;
        src_img->acquireFenceFd = mAcquireFence;
        src_img->releaseFenceFd = -1;
        src_img->dataSpace = HAL_DATASPACE_V0_SRGB;
        src_img->blending = mBlending;
        src_img->transform = mTransform;
        src_img->compressionInfo = mCompressionInfo;
        src_img->planeAlpha = mPlaneAlpha;
        src_img->zOrder = mZOrder;


        return NO_ERROR;
    }

    if (handle == NULL) {
        src_img->fullWidth = 0;
        src_img->fullHeight = 0;
        src_img->format = 0;
        src_img->usageFlags = 0x0;
        src_img->bufferHandle = handle;
    } else {
        VendorGraphicBufferMeta gmeta(handle);

        if ((mPreprocessedInfo.interlacedType == V4L2_FIELD_INTERLACED_TB) ||
            (mPreprocessedInfo.interlacedType == V4L2_FIELD_INTERLACED_BT))
        {
            src_img->fullWidth = (gmeta.stride * 2);
            src_img->fullHeight = pixel_align_down((gmeta.vstride / 2), 2);
        } else {
            src_img->fullWidth = gmeta.stride;
            // The BW VDEC will generate AFBC streams based on the initial requested height
            // instead of the adjusted vstride from gralloc.
            src_img->fullHeight = (isAFBC32x8(mCompressionInfo) &&
                                   (gmeta.producer_usage & VendorGraphicBufferUsage::BW))
                    ? gmeta.height
                    : gmeta.vstride;
        }
        if (!mPreprocessedInfo.mUsePrivateFormat)
            src_img->format = gmeta.format;
        else
            src_img->format = mPreprocessedInfo.mPrivateFormat;
        src_img->usageFlags = gmeta.producer_usage;
        src_img->bufferHandle = handle;
    }
    src_img->x = (int)mPreprocessedInfo.sourceCrop.left;
    src_img->y = (int)mPreprocessedInfo.sourceCrop.top;
    src_img->w = (int)mPreprocessedInfo.sourceCrop.right - (int)mPreprocessedInfo.sourceCrop.left;
    src_img->h = (int)mPreprocessedInfo.sourceCrop.bottom - (int)mPreprocessedInfo.sourceCrop.top;
    if ((mPreprocessedInfo.interlacedType == V4L2_FIELD_INTERLACED_TB) ||
        (mPreprocessedInfo.interlacedType == V4L2_FIELD_INTERLACED_BT))
    {
        while ((src_img->h % 2 != 0) ||
               (src_img->h > src_img->fullHeight)) {
            src_img->h -= 1;
        }
    }
    src_img->layerFlags = mLayerFlag;
    src_img->acquireFenceFd = mAcquireFence;
    src_img->releaseFenceFd = -1;

    src_img->dataSpace = mDataSpace;
    if(src_img->dataSpace == HAL_DATASPACE_UNKNOWN)
        src_img->dataSpace = HAL_DATASPACE_V0_SRGB;

    src_img->blending = mBlending;
    src_img->transform = mTransform;
    src_img->compressionInfo = mCompressionInfo;
    src_img->planeAlpha = mPlaneAlpha;
    src_img->zOrder = mZOrder;
    /* Copy HDR metadata */
    memset(&(src_img->metaParcel), 0, sizeof(src_img->metaParcel));
    src_img->metaType = VIDEO_INFO_TYPE_INVALID;
    if (mMetaParcel != NULL) {
        memcpy(&(src_img->metaParcel), mMetaParcel, sizeof(src_img->metaParcel));
        src_img->metaType = mMetaParcel->eType;
        src_img->hasMetaParcel = true;
    } else {
        src_img->hasMetaParcel = false;
    }

    src_img->needColorTransform = mLayerColorTransform.enable;
    src_img->needPreblending = mNeedPreblending;

    return NO_ERROR;
}

int32_t ExynosLayer::setDstExynosImage(exynos_image *dst_img)
{
    buffer_handle_t handle = mLayerBuffer;

    if (handle == NULL) {
        dst_img->usageFlags = 0x0;
    } else {
        dst_img->usageFlags = VendorGraphicBufferMeta::get_producer_usage(handle);
    }

    if (isDimLayer()) {
        dst_img->usageFlags = 0xb00;
    }

    dst_img->format = DEFAULT_MPP_DST_FORMAT;
    dst_img->x = mPreprocessedInfo.displayFrame.left;
    dst_img->y = mPreprocessedInfo.displayFrame.top;
    dst_img->w = (mPreprocessedInfo.displayFrame.right - mPreprocessedInfo.displayFrame.left);
    dst_img->h = (mPreprocessedInfo.displayFrame.bottom - mPreprocessedInfo.displayFrame.top);
    dst_img->layerFlags = mLayerFlag;
    dst_img->acquireFenceFd = -1;
    dst_img->releaseFenceFd = -1;
    dst_img->bufferHandle = NULL;
    dst_img->dataSpace = HAL_DATASPACE_UNKNOWN;
    if (mDisplay != NULL) {
        dst_img->fullWidth = mDisplay->mXres;
        dst_img->fullHeight = mDisplay->mYres;
        if (mDisplay->mColorMode != HAL_COLOR_MODE_NATIVE) {
            dst_img->dataSpace = colorModeToDataspace(mDisplay->mColorMode);
        } else {
            if (hasHdrInfo(mDataSpace)) {
                android_dataspace hdrDataSpace =
                    (android_dataspace)(HAL_DATASPACE_STANDARD_DCI_P3 | HAL_DATASPACE_TRANSFER_GAMMA2_2 | HAL_DATASPACE_RANGE_LIMITED);
                if (mDisplay->mType == HWC_DISPLAY_EXTERNAL) {
                    ExynosExternalDisplay *externalDisplay = (ExynosExternalDisplay*)mDisplay;
                    if (externalDisplay->mExternalHdrSupported == true)
                        dst_img->dataSpace = HAL_DATASPACE_UNKNOWN;
                    else
                        dst_img->dataSpace = hdrDataSpace;
                } else {
                    dst_img->dataSpace = hdrDataSpace;
                }
            }
        }
    } else {
        HWC_LOGE(NULL, "mDisplay is NULL");
    }
    dst_img->blending = mBlending;
    dst_img->transform = 0;
    dst_img->compressionInfo.type = COMP_TYPE_NONE;
    dst_img->planeAlpha = mPlaneAlpha;
    dst_img->zOrder = mZOrder;

    /* Copy HDR metadata */
    memset(&(dst_img->metaParcel), 0, sizeof(dst_img->metaParcel));
    dst_img->metaType = VIDEO_INFO_TYPE_INVALID;
    if (mMetaParcel != NULL) {
        memcpy(&(dst_img->metaParcel), mMetaParcel, sizeof(dst_img->metaParcel));
        dst_img->metaType = mMetaParcel->eType;
        dst_img->hasMetaParcel = true;
    } else {
        dst_img->hasMetaParcel = false;
    }

    return NO_ERROR;
}

int32_t ExynosLayer::resetAssignedResource()
{
    int32_t ret = NO_ERROR;
    if (mM2mMPP != NULL) {
        HDEBUGLOGD(eDebugResourceManager, "\t\t %s mpp is reset", mM2mMPP->mName.c_str());
        mM2mMPP->resetAssignedState(this);
        mM2mMPP = NULL;
    }
    if (mOtfMPP != NULL) {
        HDEBUGLOGD(eDebugResourceManager, "\t\t %s mpp is reset", mOtfMPP->mName.c_str());
        mOtfMPP->resetAssignedState();
        mOtfMPP = NULL;
    }
    return ret;
}

bool ExynosLayer::checkBtsCap(const uint32_t bts_refresh_rate) {
    if (mOtfMPP == nullptr) return true;

    exynos_image src_img;
    exynos_image dst_img;
    setSrcExynosImage(&src_img);
    setDstExynosImage(&dst_img);
    if (mOtfMPP->checkSpecificRestriction(bts_refresh_rate, src_img, dst_img)) {
        return false;
    }

    const bool isPerpendicular = !!(src_img.transform & HAL_TRANSFORM_ROT_90);
    const uint32_t srcWidth = isPerpendicular ? src_img.h : src_img.w;
    const uint32_t srcHeight = isPerpendicular ? src_img.w : src_img.h;
    const bool scaleDown = (srcWidth > dst_img.w || srcHeight > dst_img.h);

    if (!scaleDown) return true;

    const float resolution = float(src_img.w) * float(src_img.h) * bts_refresh_rate / 1000;

    return mOtfMPP->checkDownscaleCap(resolution, float(dst_img.h) / float(mDisplay->mYres));
}

void ExynosLayer::setSrcAcquireFence() {
    if (mAcquireFence == -1 && mPrevAcquireFence != -1) {
        mAcquireFence = hwcCheckFenceDebug(mDisplay, FENCE_TYPE_SRC_ACQUIRE, FENCE_IP_LAYER,
                                           hwc_dup(mPrevAcquireFence, mDisplay,
                                                   FENCE_TYPE_SRC_ACQUIRE, FENCE_IP_LAYER));
    } else if (mAcquireFence != -1) {
        setFenceInfo(mAcquireFence, mDisplay, FENCE_TYPE_SRC_ACQUIRE, FENCE_IP_LAYER,
                     HwcFenceDirection::FROM);
    }
}

void ExynosLayer::dump(String8& result)
{
    int format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
    int32_t fd, fd1, fd2;
    if (mLayerBuffer != NULL)
    {
        VendorGraphicBufferMeta gmeta(mLayerBuffer);
        format = gmeta.format;
        fd = gmeta.fd;
        fd1 = gmeta.fd1;
        fd2 = gmeta.fd2;
    } else {
        format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
        fd = -1;
        fd1 = -1;
        fd2 = -1;
    }

    {
        TableBuilder tb;
        tb.add("zOrder", mZOrder)
          .add("priority", mOverlayPriority);
        if (mCompositionType == HWC2_COMPOSITION_SOLID_COLOR) {
            tb.add("color", std::vector<uint64_t>({mColor.r, mColor.g, mColor.b, mColor.a}), true);
        } else {
            tb.add("handle", mLayerBuffer)
                    .add("fd", std::vector<int>({fd, fd1, fd2}))
                    .add("compression", getCompressionStr(mCompressionInfo).c_str());
        }
        tb.add("format", getFormatStr(format, mCompressionInfo.type).c_str())
                .add("dataSpace", mDataSpace, true)
                .add("colorTr", mLayerColorTransform.enable)
                .add("blend", mBlending, true)
                .add("planeAlpha", mPlaneAlpha)
                .add("fps", mFps);
        result.append(tb.build().c_str());
    }

    result.append(TableBuilder()
                          .add("sourceCrop",
                               std::vector<double>({mPreprocessedInfo.sourceCrop.left,
                                                    mPreprocessedInfo.sourceCrop.top,
                                                    mPreprocessedInfo.sourceCrop.right,
                                                    mPreprocessedInfo.sourceCrop.bottom}))
                          .add("dispFrame",
                               std::vector<int>({mPreprocessedInfo.displayFrame.left,
                                                 mPreprocessedInfo.displayFrame.top,
                                                 mPreprocessedInfo.displayFrame.right,
                                                 mPreprocessedInfo.displayFrame.bottom}))
                          .add("blockRect",
                               std::vector<int>({mBlockingRect.left, mBlockingRect.top,
                                                 mBlockingRect.right, mBlockingRect.bottom}))
                          .add("tr", mTransform, true)
                          .add("windowIndex", mWindowIndex)
                          .add("type", mCompositionType)
                          .add("exynosType", mExynosCompositionType)
                          .add("validateType", mValidateCompositionType)
                          .add("overlayInfo", mOverlayInfo, true)
                          .build()
                          .c_str());

    result.append(TableBuilder()
                          .add("MPPFlag", mSupportedMPPFlag, true)
                          .add("dim ratio", mPreprocessedInfo.sdrDimRatio)
                          .build()
                          .c_str());

    if ((mDisplay != NULL) && (mDisplay->mResourceManager != NULL)) {
        result.appendFormat("MPPFlags for otfMPP\n");
        for (uint32_t i = 0; i < mDisplay->mResourceManager->getOtfMPPSize(); i++) {
            result.appendFormat("[%s: 0x%" PRIx64 "] ", mDisplay->mResourceManager->getOtfMPP(i)->mName.c_str(),
                    mCheckMPPFlag[mDisplay->mResourceManager->getOtfMPP(i)->mLogicalType]);
        }
        result.appendFormat("\n");
        result.appendFormat("MPPFlags for m2mMPP\n");
        for (uint32_t i = 0; i < mDisplay->mResourceManager->getM2mMPPSize(); i++) {
            result.appendFormat("[%s: 0x%" PRIx64 "] ", mDisplay->mResourceManager->getM2mMPP(i)->mName.c_str(),
                    mCheckMPPFlag[mDisplay->mResourceManager->getM2mMPP(i)->mLogicalType]);
            if ((i!=0) && (i%4==0)) result.appendFormat("\n");
        }
        result.appendFormat("\n");
    }
    result.appendFormat("acquireFence: %d\n", mAcquireFence);
    if ((mOtfMPP == NULL) && (mM2mMPP == NULL))
        result.appendFormat("\tresource is not assigned.\n");
    if (mOtfMPP != NULL)
        result.appendFormat("\tassignedMPP: %s\n", mOtfMPP->mName.c_str());
    if (mM2mMPP != NULL)
        result.appendFormat("\tassignedM2mMPP: %s\n", mM2mMPP->mName.c_str());
    result.appendFormat("\tdump midImg\n");
    dumpExynosImage(result, mMidImg);

}

void ExynosLayer::printLayer()
{
    int format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
    int32_t fd, fd1, fd2;
    String8 result;
    if (mLayerBuffer != NULL)
    {
        VendorGraphicBufferMeta gmeta(mLayerBuffer);
        format = gmeta.format;
        fd = gmeta.fd;
        fd1 = gmeta.fd1;
        fd2 = gmeta.fd2;
    } else {
        format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
        fd = -1;
        fd1 = -1;
        fd2 = -1;
    }
    result.appendFormat("handle: %p [fd: %d, %d, %d], acquireFence: %d, tr: 0x%2x, compression: "
                        "%s, dataSpace: 0x%8x, format: %s\n",
                        mLayerBuffer, fd, fd1, fd2, mAcquireFence, mTransform,
                        getCompressionStr(mCompressionInfo).c_str(), mDataSpace,
                        getFormatStr(format, mCompressionInfo.type).c_str());
    result.appendFormat("\tblend: 0x%4x, planeAlpha: %3.1f, zOrder: %d, color[0x%2x, 0x%2x, 0x%2x, 0x%2x]\n",
            mBlending, mPlaneAlpha, mZOrder, mColor.r, mColor.g, mColor.b, mColor.a);
    result.appendFormat("\tfps: %.2f, priority: %d, windowIndex: %d\n", mFps, mOverlayPriority,
                        mWindowIndex);
    result.appendFormat("\tsourceCrop[%7.1f,%7.1f,%7.1f,%7.1f], dispFrame[%5d,%5d,%5d,%5d]\n",
            mSourceCrop.left, mSourceCrop.top, mSourceCrop.right, mSourceCrop.bottom,
            mDisplayFrame.left, mDisplayFrame.top, mDisplayFrame.right, mDisplayFrame.bottom);
    result.appendFormat("\ttype: %2d, exynosType: %2d, validateType: %2d\n",
            mCompositionType, mExynosCompositionType, mValidateCompositionType);
    result.appendFormat("\toverlayInfo: 0x%8x, supportedMPPFlag: 0x%8x, geometryChanged: 0x%" PRIx64 "\n",
            mOverlayInfo, mSupportedMPPFlag, mGeometryChanged);

    if ((mDisplay != NULL) && (mDisplay->mResourceManager != NULL)) {
        result.appendFormat("MPPFlags for otfMPP\n");
        for (uint32_t i = 0; i < mDisplay->mResourceManager->getOtfMPPSize(); i++) {
            result.appendFormat("[%s: 0x%" PRIx64 "] ", mDisplay->mResourceManager->getOtfMPP(i)->mName.c_str(),
                    mCheckMPPFlag[mDisplay->mResourceManager->getOtfMPP(i)->mLogicalType]);
        }
        result.appendFormat("\n");
        result.appendFormat("MPPFlags for m2mMPP\n");
        for (uint32_t i = 0; i < mDisplay->mResourceManager->getM2mMPPSize(); i++) {
            result.appendFormat("[%s: 0x%" PRIx64 "] ", mDisplay->mResourceManager->getM2mMPP(i)->mName.c_str(),
                    mCheckMPPFlag[mDisplay->mResourceManager->getM2mMPP(i)->mLogicalType]);
            if ((i!=0) && (i%4==0)) result.appendFormat("\n");
        }
        result.appendFormat("\n");
    }

    ALOGD("%s", result.c_str());
    result.clear();

    if ((mOtfMPP == NULL) && (mM2mMPP == NULL))
        ALOGD("\tresource is not assigned.");
    if (mOtfMPP != NULL)
        ALOGD("\tassignedMPP: %s", mOtfMPP->mName.c_str());
    if (mM2mMPP != NULL)
        ALOGD("\tassignedM2mMPP: %s", mM2mMPP->mName.c_str());
    ALOGD("\t++ dump midImg ++");
    dumpExynosImage(result, mMidImg);
    ALOGD("%s", result.c_str());

}

void ExynosLayer::setGeometryChanged(uint64_t changedBit)
{
    mLastUpdateTime = systemTime(CLOCK_MONOTONIC);
    mGeometryChanged |= changedBit;
    if (mRequestedCompositionType != HWC2_COMPOSITION_REFRESH_RATE_INDICATOR)
        mDisplay->setGeometryChanged(changedBit);
}

int ExynosLayer::allocMetaParcel()
{
    /* Already allocated */
    if ((mMetaParcelFd >= 0) &&
        (mMetaParcel != NULL))
        return NO_ERROR;

    if (mMetaParcelFd < 0) {
         int ionFd = exynos_ion_open();
         if (ionFd >= 0) {
             mMetaParcelFd = exynos_ion_alloc(ionFd, sizeof(ExynosVideoMeta), EXYNOS_ION_HEAP_SYSTEM_MASK, 0);
             if (mMetaParcelFd < 0) {
                 ALOGE("Failed to ion alloc for metadata parcel");
                 return -1;
             }
             exynos_ion_close(ionFd);
         } else {
             ALOGE("Failed to open ion fd");
             return -1;
         }
    }

    mMetaParcel =
        (ExynosVideoMeta*)mmap(0, sizeof(ExynosVideoMeta), PROT_READ|PROT_WRITE, MAP_SHARED, mMetaParcelFd, 0);
    if (mMetaParcel == NULL) {
        ALOGE("Failed to map metadata parcel");
        return -1;
    }

    return NO_ERROR;
}

bool ExynosLayer::isDimLayer()
{
    if (mLayerFlag & EXYNOS_HWC_DIM_LAYER)
        return true;
    return false;
}