summaryrefslogtreecommitdiff
path: root/libhwc2.1/libhwchelper/ExynosHWCHelper.h
blob: ed9964a0ae46c420a73f5e6ea5e26c853a90906d (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
/*
 * 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.
 */
#ifndef _EXYNOSHWCHELPER_H
#define _EXYNOSHWCHELPER_H

#include <drm/drm_fourcc.h>
#include <drm/samsung_drm.h>
#include <hardware/hwcomposer2.h>
#include <utils/String8.h>

#include <fstream>
#include <list>
#include <optional>
#include <sstream>
#include <string>
#include <vector>

#include "DeconCommonHeader.h"
#include "VendorGraphicBuffer.h"
#include "VendorVideoAPI.h"
#include "exynos_format.h"
#include "exynos_sync.h"
#include "mali_gralloc_formats.h"

#define MAX_FENCE_NAME 64
#define MAX_FENCE_THRESHOLD 500
#define MAX_FD_NUM      1024

#define MAX_USE_FORMAT 27
#ifndef P010M_Y_SIZE
#define P010M_Y_SIZE(w,h) (__ALIGN_UP((w), 16) * 2 * __ALIGN_UP((h), 16) + 256)
#endif
#ifndef P010M_CBCR_SIZE
#define P010M_CBCR_SIZE(w,h) ((__ALIGN_UP((w), 16) * 2 * __ALIGN_UP((h), 16) / 2) + 256)
#endif
#ifndef P010_Y_SIZE
#define P010_Y_SIZE(w, h) ((w) * (h) * 2)
#endif
#ifndef P010_CBCR_SIZE
#define P010_CBCR_SIZE(w, h) ((w) * (h))
#endif
#ifndef DRM_FORMAT_YUV420_8BIT
#define DRM_FORMAT_YUV420_8BIT fourcc_code('Y', 'U', '0', '8')
#endif
#ifndef DRM_FORMAT_YUV420_10BIT
#define DRM_FORMAT_YUV420_10BIT fourcc_code('Y', 'U', '1', '0')
#endif

static constexpr uint32_t DISPLAYID_MASK_LEN = 8;

template<typename T> inline T max(T a, T b) { return (a > b) ? a : b; }
template<typename T> inline T min(T a, T b) { return (a < b) ? a : b; }

class ExynosLayer;
class ExynosDisplay;

using namespace android;

static constexpr uint32_t TRANSFORM_MAT_SIZE = 4*4;

enum {
    EXYNOS_HWC_DIM_LAYER = 1 << 0,
    EXYNOS_HWC_IGNORE_LAYER = 1 << 1,
};

enum {
    INTERFACE_TYPE_NONE = 0,
    INTERFACE_TYPE_FB   = 1,
    INTERFACE_TYPE_DRM  = 2,
};

typedef enum format_type {
    TYPE_UNDEF = 0,

    /* format */
    FORMAT_SHIFT = 0,
    FORMAT_MASK = 0x00000fff,

    FORMAT_RGB_MASK = 0x0000000f,
    RGB = 0x00000001,

    FORMAT_YUV_MASK = 0x000000f0,
    YUV420 = 0x00000010,
    YUV422 = 0x00000020,
    P010 = 0x00000030,

    FORMAT_SBWC_MASK = 0x00000f00,
    SBWC_LOSSLESS = 0x00000100,
    SBWC_LOSSY_40 = 0x00000200,
    SBWC_LOSSY_50 = 0x00000300,
    SBWC_LOSSY_60 = 0x00000400,
    SBWC_LOSSY_75 = 0x00000500,
    SBWC_LOSSY_80 = 0x00000600,

    /* bit */
    BIT_SHIFT = 16,
    BIT_MASK = 0x000f0000,
    BIT8 = 0x00010000,
    BIT10 = 0x00020000,
    BIT8_2 = 0x00030000,
    BIT16 = 0x00040000,

    /* Compression types */
    /* Caution : This field use bit operations */
    COMP_SHIFT = 20,
    COMP_TYPE_MASK = 0x0ff00000,
    COMP_TYPE_NONE = 0x08000000,
    COMP_TYPE_AFBC = 0x00100000,
    COMP_TYPE_SBWC = 0x00200000,
} format_type_t;

typedef struct format_description {
    inline uint32_t getFormat() const { return type & FORMAT_MASK; }
    inline uint32_t getBit() const { return type & BIT_MASK; }
    inline bool isCompressionSupported(uint32_t inType) const {
        return (type & inType) != 0 ? true : false;
    }
    int halFormat;
    decon_pixel_format s3cFormat;
    int drmFormat;
    uint32_t planeNum;
    uint32_t bufferNum;
    uint8_t bpp;
    uint32_t type;
    bool hasAlpha;
    String8 name;
    uint32_t reserved;
} format_description_t;

constexpr int HAL_PIXEL_FORMAT_EXYNOS_UNDEFINED = 0;
constexpr int DRM_FORMAT_UNDEFINED = 0;

// clang-format off
const format_description_t exynos_format_desc[] = {
    /* RGB */
    {HAL_PIXEL_FORMAT_RGBA_8888, DECON_PIXEL_FORMAT_RGBA_8888, DRM_FORMAT_RGBA8888,
        1, 1, 32, RGB | BIT8 | COMP_TYPE_NONE | COMP_TYPE_AFBC, true, String8("RGBA_8888"), 0},
    {HAL_PIXEL_FORMAT_RGBX_8888, DECON_PIXEL_FORMAT_RGBX_8888, DRM_FORMAT_RGBX8888,
        1, 1, 32, RGB | BIT8 | COMP_TYPE_NONE | COMP_TYPE_AFBC, false, String8("RGBx_8888"), 0},
    {HAL_PIXEL_FORMAT_RGB_888, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_RGB888,
        1, 1, 24, RGB | BIT8 | COMP_TYPE_NONE | COMP_TYPE_AFBC, false, String8("RGB_888"), 0},
    {HAL_PIXEL_FORMAT_RGB_565, DECON_PIXEL_FORMAT_RGB_565, DRM_FORMAT_BGR565,
        1, 1, 16, RGB | COMP_TYPE_NONE, false, String8("RGB_565"), 0},
    {HAL_PIXEL_FORMAT_RGB_565, DECON_PIXEL_FORMAT_RGB_565, DRM_FORMAT_RGB565,
        1, 1, 16, RGB | COMP_TYPE_AFBC, false, String8("RGB_565_AFBC"), 0},
    {HAL_PIXEL_FORMAT_BGRA_8888, DECON_PIXEL_FORMAT_BGRA_8888, DRM_FORMAT_BGRA8888,
        1, 1, 32, RGB | BIT8 | COMP_TYPE_NONE | COMP_TYPE_AFBC, true, String8("BGRA_8888"), 0},
    {HAL_PIXEL_FORMAT_RGBA_1010102, DECON_PIXEL_FORMAT_ABGR_2101010, DRM_FORMAT_RGBA1010102,
        1, 1, 32, RGB | BIT10 | COMP_TYPE_NONE | COMP_TYPE_AFBC, true, String8("RGBA_1010102"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_ARGB_8888, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_ARGB8888,
        1, 1, 32, RGB | BIT8 | COMP_TYPE_NONE | COMP_TYPE_AFBC, true, String8("EXYNOS_ARGB_8888"), 0},
    {HAL_PIXEL_FORMAT_RGBA_FP16, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_ARGB16161616F,
        1, 1, 64, RGB | BIT16 | COMP_TYPE_NONE | COMP_TYPE_AFBC, true, String8("RGBA_FP16"), 0},

    /* YUV 420 */
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P_M, DECON_PIXEL_FORMAT_YUV420M, DRM_FORMAT_UNDEFINED,
        3, 3, 12, YUV420 | BIT8 | COMP_TYPE_NONE, false, String8("EXYNOS_YCbCr_420_P_M"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M, DECON_PIXEL_FORMAT_NV12M, DRM_FORMAT_NV12,
        2, 2, 12, YUV420 | BIT8 | COMP_TYPE_NONE, false, String8("EXYNOS_YCbCr_420_SP_M"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_TILED, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_UNDEFINED,
        2, 2, 12, YUV420 | BIT8 | COMP_TYPE_NONE, false, String8("EXYNOS_YCbCr_420_SP_M_TILED"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YV12_M, DECON_PIXEL_FORMAT_YVU420M, DRM_FORMAT_UNDEFINED,
        3, 3, 12, YUV420 | BIT8 | COMP_TYPE_NONE, false, String8("EXYNOS_YV12_M"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M, DECON_PIXEL_FORMAT_NV21M, DRM_FORMAT_NV21,
        2, 2, 12, YUV420 | BIT8 | COMP_TYPE_NONE, false, String8("EXYNOS_YCrCb_420_SP_M"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_FULL, DECON_PIXEL_FORMAT_NV21M, DRM_FORMAT_NV21,
        2, 2, 12, YUV420 | BIT8 | COMP_TYPE_NONE, false, String8("EXYNOS_YCrCb_420_SP_M_FULL"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_UNDEFINED,
        3, 1, 0, YUV420 | BIT8 | COMP_TYPE_NONE, false, String8("EXYNOS_YCbCr_420_P"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_UNDEFINED,
        2, 1, 0, YUV420 | BIT8 | COMP_TYPE_NONE, false, String8("EXYNOS_YCbCr_420_SP"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_PRIV, DECON_PIXEL_FORMAT_NV12M, DRM_FORMAT_NV12,
        2, 2, 12, YUV420 | BIT8 | COMP_TYPE_NONE, false, String8("EXYNOS_YCbCr_420_SP_M_PRIV"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_PN, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_UNDEFINED,
        3, 1, 12, YUV420 | BIT8 | COMP_TYPE_NONE, false, String8("EXYNOS_YCbCr_420_PN"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN, DECON_PIXEL_FORMAT_NV12N, DRM_FORMAT_NV12,
        2, 1, 12, YUV420 | BIT8 | COMP_TYPE_NONE, false, String8("EXYNOS_YCbCr_420_SPN"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_TILED, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_UNDEFINED,
        2, 1, 12, YUV420 | BIT8 | COMP_TYPE_NONE, false, String8("EXYNOS_YCbCr_420_SPN_TILED"), 0},
    {HAL_PIXEL_FORMAT_YCrCb_420_SP, DECON_PIXEL_FORMAT_NV21, DRM_FORMAT_NV21,
        2, 1, 12, YUV420 | BIT8 | COMP_TYPE_NONE, false, String8("YCrCb_420_SP"), 0},
    {HAL_PIXEL_FORMAT_YV12, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_UNDEFINED,
        3, 1, 12, YUV420 | BIT8 | COMP_TYPE_NONE, false, String8("YV12"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B, DECON_PIXEL_FORMAT_NV12M_S10B, DRM_FORMAT_UNDEFINED,
        2, 2, 12, YUV420 | BIT10 | BIT8_2 | COMP_TYPE_NONE, false, String8("EXYNOS_YCbCr_420_SP_M_S10B"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_S10B, DECON_PIXEL_FORMAT_NV12N_10B, DRM_FORMAT_UNDEFINED,
        2, 1, 12, YUV420 | BIT10 | BIT8_2 | COMP_TYPE_NONE, false, String8("EXYNOS_YCbCr_420_SPN_S10B"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_M, DECON_PIXEL_FORMAT_NV12M_P010, DRM_FORMAT_P010,
        2, 2, 24, YUV420 | BIT10 | P010 | COMP_TYPE_NONE, false, String8("EXYNOS_YCbCr_P010_M"), 0},
    {HAL_PIXEL_FORMAT_YCBCR_P010, DECON_PIXEL_FORMAT_NV12_P010, DRM_FORMAT_P010,
        2, 1, 24, YUV420 | BIT10 | P010 | COMP_TYPE_NONE, false, String8("EXYNOS_YCbCr_P010"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_SPN, DECON_PIXEL_FORMAT_NV12_P010, DRM_FORMAT_P010,
        2, 1, 24, YUV420 | BIT10 | P010 | COMP_TYPE_NONE, false, String8("EXYNOS_YCbCr_P010_SPN"), 0},
    {MALI_GRALLOC_FORMAT_INTERNAL_P010, DECON_PIXEL_FORMAT_NV12_P010, DRM_FORMAT_P010,
        2, 1, 24, YUV420 | BIT10 | P010 | COMP_TYPE_NONE, false, String8("MALI_GRALLOC_FORMAT_INTERNAL_P010"), 0},

    {HAL_PIXEL_FORMAT_GOOGLE_NV12_SP, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_NV12,
        2, 1, 12, YUV420 | BIT8 | COMP_TYPE_NONE, false, String8("GOOGLE_YCbCr_420_SP"), 0},
    {HAL_PIXEL_FORMAT_GOOGLE_NV12_SP_10B, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_P010,
        2, 1, 24, YUV420 | BIT10 | COMP_TYPE_NONE, false, String8("GOOGLE_YCbCr_P010"), 0},
    {MALI_GRALLOC_FORMAT_INTERNAL_YUV420_8BIT_I, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_YUV420_8BIT,
        1, 1, 12, YUV420 | BIT8 | COMP_TYPE_AFBC, false, String8("MALI_GRALLOC_FORMAT_INTERNAL_YUV420_8BIT_I"), 0},
    {MALI_GRALLOC_FORMAT_INTERNAL_YUV420_10BIT_I, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_YUV420_10BIT,
        1, 1, 15, YUV420 | BIT10 | COMP_TYPE_AFBC, false, String8("MALI_GRALLOC_FORMAT_INTERNAL_YUV420_10BIT_I"), 0},
    {MALI_GRALLOC_FORMAT_INTERNAL_NV21, DECON_PIXEL_FORMAT_NV21, DRM_FORMAT_NV21,
        2, 1, 12, YUV420 | BIT8 | COMP_TYPE_NONE, false, String8("MALI_GRALLOC_FORMAT_INTERNAL_NV21"), 0},

    /* YUV 422 */
    {HAL_PIXEL_FORMAT_EXYNOS_CbYCrY_422_I, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_UNDEFINED,
        0, 0, 0, YUV422 | BIT8 | COMP_TYPE_NONE, false, String8("EXYNOS_CbYCrY_422_I"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCrCb_422_SP, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_UNDEFINED,
        0, 0, 0, YUV422 | BIT8 | COMP_TYPE_NONE, false, String8("EXYNOS_YCrCb_422_SP"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCrCb_422_I, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_UNDEFINED,
        0, 0, 0, YUV422 | BIT8 | COMP_TYPE_NONE, false, String8("EXYNOS_YCrCb_422_I"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_CrYCbY_422_I, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_UNDEFINED,
        0, 0, 0, YUV422 | BIT8 | COMP_TYPE_NONE, false, String8("EXYNOS_CrYCbY_422_I"), 0},

    /* SBWC formats */
    /* NV12, YCbCr, Multi */
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC, DECON_PIXEL_FORMAT_NV12M_SBWC_8B, DRM_FORMAT_NV12,
        2, 2, 12, YUV420 | BIT8 | COMP_TYPE_SBWC | SBWC_LOSSLESS, false, String8("EXYNOS_YCbCr_420_SP_M_SBWC"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L50, DECON_PIXEL_FORMAT_NV12M_SBWC_8B_L50, DRM_FORMAT_NV12,
        2, 2, 12, YUV420 | BIT8 | COMP_TYPE_SBWC | SBWC_LOSSY_50, false, String8("EXYNOS_YCbCr_420_SP_M_SBWC_L50"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L75, DECON_PIXEL_FORMAT_NV12M_SBWC_8B_L75, DRM_FORMAT_NV12,
        2, 2, 12, YUV420 | BIT8 | COMP_TYPE_SBWC | SBWC_LOSSY_75, false, String8("EXYNOS_YCbCr_420_SP_M_SBWC_L75"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC, DECON_PIXEL_FORMAT_NV12M_SBWC_10B, DRM_FORMAT_UNDEFINED,
        2, 2, 12, YUV420 | BIT10 | COMP_TYPE_SBWC | SBWC_LOSSLESS, false, String8("EXYNOS_YCbCr_420_SP_M_10B_SBWC"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L40, DECON_PIXEL_FORMAT_NV12M_SBWC_10B_L40, DRM_FORMAT_UNDEFINED,
        2, 2, 12, YUV420 | BIT10 | COMP_TYPE_SBWC | SBWC_LOSSY_40, false, String8("EXYNOS_YCbCr_420_SP_M_10B_SBWC_L40"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L60, DECON_PIXEL_FORMAT_NV12M_SBWC_10B_L60, DRM_FORMAT_UNDEFINED,
        2, 2, 12, YUV420 | BIT10 | COMP_TYPE_SBWC | SBWC_LOSSY_60, false, String8("EXYNOS_YCbCr_420_SP_M_10B_SBWC_L60"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L80, DECON_PIXEL_FORMAT_NV12M_SBWC_10B_L80, DRM_FORMAT_UNDEFINED,
        2, 2, 12, YUV420 | BIT10 | COMP_TYPE_SBWC | SBWC_LOSSY_80, false, String8("EXYNOS_YCbCr_420_SP_M_10B_SBWC_L80"), 0},

    /* NV12, YCbCr, Single */
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC, DECON_PIXEL_FORMAT_NV12N_SBWC_8B, DRM_FORMAT_NV12,
        2, 1, 12, YUV420 | BIT8 | COMP_TYPE_SBWC | SBWC_LOSSLESS, false, String8("EXYNOS_YCbCr_420_SPN_SBWC"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L50, DECON_PIXEL_FORMAT_NV12N_SBWC_8B_L50, DRM_FORMAT_NV12,
        2, 1, 12, YUV420 | BIT8 | COMP_TYPE_SBWC | SBWC_LOSSY_50, false, String8("EXYNOS_YCbCr_420_SPN_SBWC_L50"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L75, DECON_PIXEL_FORMAT_NV12N_SBWC_8B_L75, DRM_FORMAT_NV12,
        2, 1, 12, YUV420 | BIT8 | COMP_TYPE_SBWC | SBWC_LOSSY_75, false, String8("EXYNOS_YCbCr_420_SPN_SBWC_75"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC, DECON_PIXEL_FORMAT_NV12N_SBWC_10B, DRM_FORMAT_UNDEFINED,
        2, 1, 12, YUV420 | BIT10 | COMP_TYPE_SBWC | SBWC_LOSSLESS, false, String8("EXYNOS_YCbCr_420_SPN_10B_SBWC"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L40, DECON_PIXEL_FORMAT_NV12N_SBWC_10B_L40, DRM_FORMAT_UNDEFINED,
        2, 1, 12, YUV420 | BIT10 | COMP_TYPE_SBWC | SBWC_LOSSY_40, false, String8("EXYNOS_YCbCr_420_SPN_10B_SBWC_L40"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L60, DECON_PIXEL_FORMAT_NV12N_SBWC_10B_L60, DRM_FORMAT_UNDEFINED,
        2, 1, 12, YUV420 | BIT10 | COMP_TYPE_SBWC | SBWC_LOSSY_60, false, String8("EXYNOS_YCbCr_420_SPN_10B_SBWC_L60"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L80, DECON_PIXEL_FORMAT_NV12N_SBWC_10B_L80, DRM_FORMAT_UNDEFINED,
        2, 1, 12, YUV420 | BIT10 | COMP_TYPE_SBWC | SBWC_LOSSY_80, false, String8("EXYNOS_YCbCr_420_SPN_10B_SBWC_L80"), 0},

    /* NV12, YCrCb */
    {HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_SBWC, DECON_PIXEL_FORMAT_NV21M_SBWC_8B, DRM_FORMAT_UNDEFINED,
        2, 2, 12, YUV420 | BIT8 | COMP_TYPE_SBWC | SBWC_LOSSLESS, false, String8("EXYNOS_YCrCb_420_SP_M_SBWC"), 0},
    {HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_10B_SBWC, DECON_PIXEL_FORMAT_NV21M_SBWC_10B, DRM_FORMAT_UNDEFINED,
        2, 2, 12, YUV420 | BIT10 | COMP_TYPE_SBWC | SBWC_LOSSLESS, false, String8("EXYNOS_YCrbCb_420_SP_M_10B_SBWC"), 0},

    {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_UNDEFINED,
        0, 0, 0, TYPE_UNDEF | COMP_TYPE_NONE, false, String8("ImplDef"), 0},

    {HAL_PIXEL_FORMAT_GOOGLE_R_8, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_C8,
        1, 1, 8, RGB | BIT8 | COMP_TYPE_NONE, true, String8("GOOGLE_R_8"), 0},
};
// clang-format on

constexpr size_t FORMAT_MAX_CNT = sizeof(exynos_format_desc) / sizeof(format_description);

enum HwcFdebugFenceType {
    FENCE_TYPE_SRC_RELEASE = 1,
    FENCE_TYPE_SRC_ACQUIRE = 2,
    FENCE_TYPE_DST_RELEASE = 3,
    FENCE_TYPE_DST_ACQUIRE = 4,
    FENCE_TYPE_FREE_RELEASE = 5,
    FENCE_TYPE_FREE_ACQUIRE = 6,
    FENCE_TYPE_HW_STATE = 7,
    FENCE_TYPE_RETIRE = 8,
    FENCE_TYPE_READBACK_ACQUIRE = 9,
    FENCE_TYPE_READBACK_RELEASE = 10,
    FENCE_TYPE_ALL = 11,
    FENCE_TYPE_UNDEFINED = 100
};

enum HwcFdebugIpType {
    FENCE_IP_DPP = 0,
    FENCE_IP_MSC = 1,
    FENCE_IP_G2D = 2,
    FENCE_IP_FB = 3,
    FENCE_IP_LAYER = 4,
    FENCE_IP_ALL = 5,
    FENCE_IP_UNDEFINED = 100
};

enum HwcFenceType {
    FENCE_LAYER_RELEASE_DPP = 0,
    FENCE_LAYER_RELEASE_MPP = 1,
    FENCE_LAYER_RELEASE_MSC = 2,
    FENCE_LAYER_RELEASE_G2D = 3,
    FENCE_DPP_HW_STATE = 4,
    FENCE_MSC_HW_STATE = 5,
    FENCE_G2D_HW_STATE = 6,
    FENCE_MSC_SRC_LAYER = 7,
    FENCE_G2D_SRC_LAYER = 8,
    FENCE_MPP_DST_DPP = 9,
    FENCE_MSC_DST_DPP = 10,
    FENCE_G2D_DST_DPP = 11,
    FENCE_DPP_SRC_MPP = 12,
    FENCE_DPP_SRC_MSC = 13,
    FENCE_DPP_SRC_G2D = 14,
    FENCE_DPP_SRC_LAYER = 15,
    FENCE_MPP_FREE_BUF_ACQUIRE = 16,
    FENCE_MPP_FREE_BUF_RELEASE = 17,
    FENCE_RETIRE = 18,
    FENCE_MAX
};

enum {
    EXYNOS_ERROR_NONE       = 0,
    EXYNOS_ERROR_CHANGED    = 1
};

enum {
    eSkipLayer                    =     0x00000001,
    eInvalidHandle                =     0x00000002,
    eHasFloatSrcCrop              =     0x00000004,
    eUpdateExynosComposition      =     0x00000008,
    eDynamicRecomposition         =     0x00000010,
    eForceFbEnabled               =     0x00000020,
    eSandwitchedBetweenGLES       =     0x00000040,
    eSandwitchedBetweenEXYNOS     =     0x00000080,
    eInsufficientWindow           =     0x00000100,
    eInsufficientMPP              =     0x00000200,
    eSkipStaticLayer              =     0x00000400,
    eUnSupportedUseCase           =     0x00000800,
    eDimLayer                     =     0x00001000,
    eResourcePendingWork          =     0x00002000,
    eSkipRotateAnim               =     0x00004000,
    eUnSupportedColorTransform    =     0x00008000,
    eLowFpsLayer                  =     0x00010000,
    eReallocOnGoingForDDI         =     0x00020000,
    eInvalidDispFrame             =     0x00040000,
    eExceedMaxLayerNum            =     0x00080000,
    eExceedSdrDimRatio            =     0x00100000,
    eResourceAssignFail           =     0x20000000,
    eMPPUnsupported               =     0x40000000,
    eUnknown                      =     0x80000000,
};

enum regionType {
    eTransparentRegion          =       0,
    eCoveredOpaqueRegion        =       1,
    eDamageRegionByDamage       =       2,
    eDamageRegionByLayer        =       3,
};

enum {
    eDamageRegionFull = 0,
    eDamageRegionPartial,
    eDamageRegionSkip,
    eDamageRegionError,
};

struct CompressionInfo {
    uint32_t type = COMP_TYPE_NONE;
    uint64_t modifier = 0;
};

/*
 * bufferHandle can be NULL if it is not allocated yet
 * or size or format information can be different between other field values and
 * member of bufferHandle. This means bufferHandle should be reallocated.
 * */
typedef struct exynos_image {
    uint32_t fullWidth = 0;
    uint32_t fullHeight = 0;
    uint32_t x = 0;
    uint32_t y = 0;
    uint32_t w = 0;
    uint32_t h = 0;
    uint32_t format= 0;
    uint64_t usageFlags = 0;
    uint32_t layerFlags = 0;
    int acquireFenceFd = -1;
    int releaseFenceFd = -1;
    buffer_handle_t bufferHandle = NULL;
    android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
    uint32_t blending = 0;
    uint32_t transform = 0;
    CompressionInfo compressionInfo;
    float planeAlpha = 0;
    uint32_t zOrder = 0;
    /* refer
     * frameworks/native/include/media/hardware/VideoAPI.h
     * frameworks/native/include/media/hardware/HardwareAPI.h */
    bool hasMetaParcel = false;
    ExynosVideoMeta metaParcel;
    ExynosVideoInfoType metaType = VIDEO_INFO_TYPE_INVALID;
    bool needColorTransform = false;
    bool needPreblending = false;

    bool isDimLayer()
    {
        if (layerFlags & EXYNOS_HWC_DIM_LAYER)
            return true;
        return false;
    };
} exynos_image_t;

uint32_t getHWC1CompType(int32_t /*hwc2_composition_t*/ type);

uint32_t getDrmMode(uint64_t flags);
uint32_t getDrmMode(const buffer_handle_t handle);

inline int WIDTH(const hwc_rect &rect) { return rect.right - rect.left; }
inline int HEIGHT(const hwc_rect &rect) { return rect.bottom - rect.top; }
inline int WIDTH(const hwc_frect_t &rect) { return (int)(rect.right - rect.left); }
inline int HEIGHT(const hwc_frect_t &rect) { return (int)(rect.bottom - rect.top); }

const format_description_t *halFormatToExynosFormat(int format, uint32_t compressType);

uint32_t halDataSpaceToV4L2ColorSpace(android_dataspace data_space);
enum decon_pixel_format halFormatToDpuFormat(int format, uint32_t compressType);
uint32_t DpuFormatToHalFormat(int format, uint32_t compressType);
int halFormatToDrmFormat(int format, uint32_t compressType);
int32_t drmFormatToHalFormats(int format, std::vector<uint32_t> *halFormats);
int drmFormatToHalFormat(int format);
uint8_t formatToBpp(int format);
uint8_t DpuFormatToBpp(decon_pixel_format format);
uint64_t halTransformToDrmRot(uint32_t halTransform);

bool isAFBCCompressed(const buffer_handle_t handle);
bool isSBWCCompressed(const buffer_handle_t handle);
uint32_t getFormat(const buffer_handle_t handle);
uint64_t getFormatModifier(const buffer_handle_t handle);
uint32_t getCompressionType(const buffer_handle_t handle);
CompressionInfo getCompressionInfo(buffer_handle_t handle);
String8 getCompressionStr(CompressionInfo compression);
bool isAFBC32x8(CompressionInfo compression);

bool isFormatRgb(int format);
bool isFormatYUV(int format);
bool isFormatYUV420(int format);
bool isFormatYUV422(int format);
bool isFormatYCrCb(int format);
bool isFormatYUV8_2(int format);
bool isFormat10BitYUV420(int format);
bool isFormatLossy(int format);
bool isFormatSBWC(int format);
bool isFormatP010(int format);
bool isFormat10Bit(int format);
bool isFormat8Bit(int format);
bool formatHasAlphaChannel(int format);
unsigned int isNarrowRgb(int format, android_dataspace data_space);
bool isSrcCropFloat(hwc_frect &frect);
bool isScaled(exynos_image &src, exynos_image &dst);
bool isScaledDown(exynos_image &src, exynos_image &dst);
bool hasHdrInfo(const exynos_image &img);
bool hasHdrInfo(android_dataspace dataSpace);
bool hasHdr10Plus(exynos_image &img);

void dumpExynosImage(uint32_t type, exynos_image &img);
void dumpExynosImage(String8& result, exynos_image &img);
void dumpHandle(uint32_t type, buffer_handle_t h);
void printExynosLayer(const ExynosLayer *layer);
String8 getFormatStr(int format, uint32_t compressType);
String8 getMPPStr(int typeId);
void adjustRect(hwc_rect_t &rect, int32_t width, int32_t height);
uint32_t getBufferNumOfFormat(int format, uint32_t compressType);
uint32_t getPlaneNumOfFormat(int format, uint32_t compressType);
uint32_t getBytePerPixelOfPrimaryPlane(int format);

int fence_close(int fence, ExynosDisplay *display, HwcFdebugFenceType type, HwcFdebugIpType ip);
bool fence_valid(int fence);

int hwcFdClose(int fd);
int hwc_dup(int fd, ExynosDisplay *display, HwcFdebugFenceType type, HwcFdebugIpType ip,
            bool pendingAllowed = false);
int hwc_print_stack();

inline hwc_rect expand(const hwc_rect &r1, const hwc_rect &r2)
{
    hwc_rect i;
    i.top = min(r1.top, r2.top);
    i.bottom = max(r1.bottom, r2.bottom);
    i.left = min(r1.left, r2.left);
    i.right = max(r1.right, r2.right);
    return i;
}

template <typename T>
inline T pixel_align_down(const T x, const uint32_t a) {
    static_assert(std::numeric_limits<T>::is_integer,
                  "Integer type is expected as the alignment input");
    return a ? (x / a) * a : x;
}

template <typename T>
inline T pixel_align(const T x, const uint32_t a) {
    static_assert(std::numeric_limits<T>::is_integer,
                  "Integer type is expected as the alignment input");
    return a ? ((x + a - 1) / a) * a : x;
}

uint32_t getExynosBufferYLength(uint32_t width, uint32_t height, int format);
int getBufLength(buffer_handle_t handle, uint32_t planer_num, size_t *length, int format, uint32_t width, uint32_t height);

enum class HwcFenceDirection {
    FROM = 0,
    TO,
    DUP,
    CLOSE,
    UPDATE,
};

struct HwcFenceTrace {
    HwcFenceDirection direction = HwcFenceDirection::FROM;
    HwcFdebugFenceType type = FENCE_TYPE_UNDEFINED;
    HwcFdebugIpType ip = FENCE_IP_UNDEFINED;
    struct timeval time = {0, 0};
};

struct HwcFenceInfo {
    uint32_t displayId = HWC_DISPLAY_PRIMARY;
    int32_t usage = 0;
    int32_t dupFrom = -1;
    bool pendingAllowed = false;
    bool leaking = false;
    std::list<HwcFenceTrace> traces = {};
};

class funcReturnCallback {
public:
    funcReturnCallback(const std::function<void(void)> cb) : mCb(cb) {}
    ~funcReturnCallback() { mCb(); }

private:
    const std::function<void(void)> mCb;
};

String8 getLocalTimeStr(struct timeval tv);

void setFenceName(int fenceFd, HwcFenceType fenceType);
void setFenceInfo(uint32_t fd, const ExynosDisplay *display, HwcFdebugFenceType type,
                  HwcFdebugIpType ip, HwcFenceDirection direction, bool pendingAllowed = false,
                  int32_t dupFrom = -1);

class FenceTracker {
public:
    void updateFenceInfo(uint32_t fd, const ExynosDisplay *display, HwcFdebugFenceType type,
                         HwcFdebugIpType ip, HwcFenceDirection direction,
                         bool pendingAllowed = false, int32_t dupFrom = -1);
    bool validateFences(ExynosDisplay *display);

private:
    void printLastFenceInfoLocked(uint32_t fd) REQUIRES(mFenceMutex);
    void dumpFenceInfoLocked(int32_t count) REQUIRES(mFenceMutex);
    void printLeakFdsLocked() REQUIRES(mFenceMutex);
    void dumpNCheckLeakLocked() REQUIRES(mFenceMutex);
    bool fenceWarnLocked(uint32_t threshold) REQUIRES(mFenceMutex);
    bool validateFencePerFrameLocked(const ExynosDisplay *display) REQUIRES(mFenceMutex);
    int32_t saveFenceTraceLocked(ExynosDisplay *display) REQUIRES(mFenceMutex);

    std::map<int, HwcFenceInfo> mFenceInfos GUARDED_BY(mFenceMutex);
    mutable std::mutex mFenceMutex;
};

android_dataspace colorModeToDataspace(android_color_mode_t mode);
bool hasPPC(uint32_t physicalType, uint32_t formatIndex, uint32_t rotIndex);

class TableBuilder {
public:
    template <typename T>
    TableBuilder& add(const std::string& key, const T& value) {
        std::stringstream v;
        v << value;
        data.emplace_back(std::make_pair(key, v.str()));
        return *this;
    }

    template <typename T>
    TableBuilder& add(const std::string& key, const std::vector<T>& values) {
        std::stringstream value;
        for (int i = 0; i < values.size(); i++) {
            if (i) value << ", ";
            value << values[i];
        }

        data.emplace_back(std::make_pair(key, value.str()));
        return *this;
    }

    // Template overrides for hex integers
    TableBuilder& add(const std::string& key, const uint64_t& value, bool toHex);
    TableBuilder& add(const std::string& key, const std::vector<uint64_t>& values, bool toHex);

    std::string build();

private:
    std::string buildPaddedString(const std::string& str, int size);

    using StringPairVec = std::vector<std::pair<std::string, std::string>>;
    StringPairVec data;
};

void writeFileNode(FILE *fd, int value);
int32_t writeIntToFile(const char *file, uint32_t value);
constexpr uint32_t getDisplayId(int32_t displayType, int32_t displayIndex) {
    return (displayType << DISPLAYID_MASK_LEN) | displayIndex;
}

int32_t load_png_image(const char *filepath, buffer_handle_t buffer);
int readLineFromFile(const std::string &filename, std::string &out, char delim);

template <typename T>
struct CtrlValue {
public:
    CtrlValue() : value_(), dirty_(false) {}
    CtrlValue(const T& value) : value_(value), dirty_(false) {}

    void store(T value) {
        if (value == value_) return;
        dirty_ = true;
        value_ = value;
    };
    const T &get() { return value_; };
    bool is_dirty() { return dirty_; };
    void clear_dirty() { dirty_ = false; };
    void set_dirty() { dirty_ = true; };
    void reset(T value) { value_ = value; dirty_ = false; }
private:
    T value_;
    bool dirty_;
};

template <typename T>
constexpr typename std::underlying_type<T>::type toUnderlying(T v) {
    return static_cast<typename std::underlying_type<T>::type>(v);
}

template <size_t bufferSize>
struct RollingAverage {
    std::array<int64_t, bufferSize> buffer{0};
    int64_t total = 0;
    int64_t average;
    size_t elems = 0;
    size_t buffer_index = 0;
    void insert(int64_t newTime) {
        total += newTime - buffer[buffer_index];
        buffer[buffer_index] = newTime;
        buffer_index = (buffer_index + 1) % bufferSize;
        elems = std::min(elems + 1, bufferSize);
        average = total / elems;
    }
};

// Waits for a given property value, or returns std::nullopt if unavailable
std::optional<std::string> waitForPropertyValue(const std::string &property, int64_t timeoutMs);

uint32_t rectSize(const hwc_rect_t &rect);
void assign(decon_win_rect &win_rect, uint32_t left, uint32_t right, uint32_t width,
            uint32_t height);
#endif