summaryrefslogtreecommitdiff
path: root/gralloc4/src/core/mali_gralloc_formats.cpp
blob: 5048cace32ee606a8db21ecbf4e2d60d87a2de49 (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
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
/*
 * Copyright (C) 2016-2020 ARM Limited. All rights reserved.
 *
 * Copyright (C) 2008 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 <string.h>
#include <dlfcn.h>
#include <inttypes.h>
#include <log/log.h>
#include <assert.h>
#include <vector>
#include <cutils/properties.h>

#include "gralloc_priv.h"
#include "mali_gralloc_bufferallocation.h"
#include "mali_gralloc_usages.h"
#include "format_info.h"
#include "capabilities/gralloc_capabilities.h"
#include "exynos_format.h"

/* Producer/consumer definitions.
 * CPU: Software access
 * GPU: Graphics processor
 * DPU: Display processor
 * VPU: Video processor
 * CAM: Camera ISP
 * TPU: ML accelerator
 */
#define MALI_GRALLOC_PRODUCER_CPU		((uint16_t)1 << 0)
#define MALI_GRALLOC_PRODUCER_GPU		((uint16_t)1 << 1)
#define MALI_GRALLOC_PRODUCER_DPU		((uint16_t)1 << 2)
#define MALI_GRALLOC_PRODUCER_VPU		((uint16_t)1 << 4)
#define MALI_GRALLOC_PRODUCER_CAM		((uint16_t)1 << 5)
#define GOOGLE_GRALLOC_PRODUCER_BIG		((uint16_t)1 << 6)
#define GOOGLE_GRALLOC_PRODUCER_MFC		((uint16_t)1 << 7)
#define GOOGLE_GRALLOC_PRODUCER_VPUS_MASK	(MALI_GRALLOC_PRODUCER_VPU | GOOGLE_GRALLOC_PRODUCER_BIG | GOOGLE_GRALLOC_PRODUCER_MFC)
#define GOOGLE_GRALLOC_PRODUCER_TPU		((uint16_t)1 << 8)

#define MALI_GRALLOC_CONSUMER_CPU		((uint16_t)1 << 0)
#define MALI_GRALLOC_CONSUMER_GPU		((uint16_t)1 << 1)
#define MALI_GRALLOC_CONSUMER_DPU		((uint16_t)1 << 2)
#define MALI_GRALLOC_CONSUMER_VPU		((uint16_t)1 << 3)
#define GOOGLE_GRALLOC_CONSUMER_BIG		((uint16_t)1 << 4)
#define GOOGLE_GRALLOC_CONSUMER_MFC		((uint16_t)1 << 5)
#define GOOGLE_GRALLOC_CONSUMER_VPUS_MASK	(MALI_GRALLOC_CONSUMER_VPU | GOOGLE_GRALLOC_CONSUMER_BIG | GOOGLE_GRALLOC_CONSUMER_MFC)
#define GOOGLE_GRALLOC_CONSUMER_TPU		((uint16_t)1 << 6)


typedef struct
{
	uint32_t base_format;
	uint64_t format_ext;
	format_support_flags f_flags;
} fmt_props;

/*
 * Video encoder consumer can be signalled by a combination of usage flags
 * Besides VIDEO_ENCODER, clients can specify the specific IP block they belong
 * to, for fine-grained control over capabilities.
 *
 * @param usage  [in]    Buffer usage.
 *
 * @return The corresponding consumer flag, or 0 if the consumer is not a VPU.
 */
static uint16_t get_vpu_consumer(uint64_t usage)
{
	if (!(usage & hidl_common::BufferUsage::VIDEO_ENCODER))
		return 0;

	/* When both the BIG and MFC flags are present, the assumption is BIG is the
	   producer and MFC is the consumer. There is no use case as of now in which
	   MFC is the producer and BIG is the consumer. */
	if (usage & GRALLOC_USAGE_GOOGLE_IP_MFC)
		return GOOGLE_GRALLOC_CONSUMER_MFC;

	if (usage & GRALLOC_USAGE_GOOGLE_IP_BIG)
		return GOOGLE_GRALLOC_CONSUMER_BIG;

	// TODO(b/185896428): Support 64-bits usage version for GraphicBufferSource
	return GOOGLE_GRALLOC_CONSUMER_MFC;
}

/*
 * Determines all IP consumers included by the requested buffer usage.
 * Private usage flags are excluded from this process.
 *
 * @param usage   [in]    Buffer usage.
 *
 * @return flags word of all enabled consumers;
 *         0, if no consumers are enabled
 */
static uint16_t get_consumers(uint64_t usage)
{
	uint16_t consumers = 0;

	if (usage & GS101_GRALLOC_USAGE_TPU_INPUT)
	{
		consumers |= GOOGLE_GRALLOC_CONSUMER_TPU;
	}

	/* Exclude usages also not applicable to consumer derivation */
	usage &= ~GRALLOC_USAGE_PROTECTED;

	get_ip_capabilities();

	if ((usage & GRALLOC_USAGE_PUBLIC_MASK) == GRALLOC_USAGE_HW_COMPOSER)
	{
		consumers = MALI_GRALLOC_CONSUMER_DPU;
	}
	else
	{
		if (usage & GRALLOC_USAGE_SW_READ_MASK)
		{
			consumers |= MALI_GRALLOC_CONSUMER_CPU;
		}

		/* GRALLOC_USAGE_HW_FB describes a framebuffer which contains a
		 * pre-composited scene that is scanned-out to a display. This buffer
		 * can be consumed by even the most basic display processor which does
		 * not support multi-layer composition.
		 */
		if (usage & GRALLOC_USAGE_HW_FB)
		{
			consumers |= MALI_GRALLOC_CONSUMER_DPU;
		}

		consumers |= get_vpu_consumer(usage);

		/* GRALLOC_USAGE_HW_COMPOSER does not explicitly define whether the
		 * display processor is producer or consumer. When used in combination
		 * with GRALLOC_USAGE_HW_TEXTURE, it is assumed to be consumer since the
		 * GPU and DPU both act as compositors.
		 */
		if ((usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_COMPOSER)) ==
		    (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_COMPOSER))
		{
			consumers |= MALI_GRALLOC_CONSUMER_DPU;
		}

		if (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_GPU_DATA_BUFFER))
		{
			consumers |= MALI_GRALLOC_CONSUMER_GPU;
		}
	}

	return consumers;
}

/*
 * Video decoder producer can be signalled by a combination of usage flags
 * Besides VIDEO_DECODER, clients can specify the specific IP block they belong
 * to, for fine-grained control over capabilities.
 *
 * @param usage  [in]    Buffer usage.
 *
 * @return The corresponding producer flag, or 0 if the producer is not a VPU.
 */
static uint16_t get_vpu_producer(uint64_t usage)
{
	if (!(usage & hidl_common::BufferUsage::VIDEO_DECODER))
		return 0;

	/* When both the BIG and MFC flags are present, the assumption is BIG is the
	   producer and MFC is the consumer. There is no use case as of now in which
	   MFC is the producer and BIG is the consumer. */
	if (usage & GRALLOC_USAGE_GOOGLE_IP_BIG)
		return GOOGLE_GRALLOC_PRODUCER_BIG;

	if (usage & GRALLOC_USAGE_GOOGLE_IP_MFC)
		return GOOGLE_GRALLOC_PRODUCER_MFC;

	MALI_GRALLOC_LOGV("Video producer IP not specified, falling back to default VPU producer");
	return MALI_GRALLOC_PRODUCER_VPU;
}

/*
 * Determines all IP producers included by the requested buffer usage.
 * Private usage flags are excluded from this process.
 *
 * @param usage   [in]    Buffer usage.
 *
 * @return flags word of all enabled producers;
 *         0, if no producers are enabled
 */
static uint16_t get_producers(uint64_t usage)
{
	uint16_t producers = 0;

	if (usage & GS101_GRALLOC_USAGE_TPU_OUTPUT)
	{
		producers |= GOOGLE_GRALLOC_PRODUCER_TPU;
	}

	/* Exclude usages also not applicable to producer derivation */
	usage &= ~GRALLOC_USAGE_PROTECTED;

	get_ip_capabilities();

	if (usage & GRALLOC_USAGE_SW_WRITE_MASK)
	{
		producers |= MALI_GRALLOC_PRODUCER_CPU;
	}

	/* DPU is normally consumer however, when there is an alternative
	 * consumer (VPU) and no other producer (e.g. VPU), it acts as a producer.
	 */
	if ((usage & GRALLOC_USAGE_DECODER) != GRALLOC_USAGE_DECODER &&
	    (usage & (GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_VIDEO_ENCODER)) ==
	    (GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_VIDEO_ENCODER))
	{
		producers |= MALI_GRALLOC_PRODUCER_DPU;
	}

	if (usage & (GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_GPU_DATA_BUFFER))
	{
		producers |= MALI_GRALLOC_PRODUCER_GPU;
	}

	if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
	{
		producers |= MALI_GRALLOC_PRODUCER_CAM;
	}

	producers |= get_vpu_producer(usage);

	return producers;
}

static inline bool consumers_use_cpu_caps(uint16_t consumers)
{
	return consumers & (MALI_GRALLOC_CONSUMER_CPU | GOOGLE_GRALLOC_CONSUMER_TPU);
}

static inline bool producers_use_cpu_caps(uint16_t producers)
{
	return producers & (MALI_GRALLOC_PRODUCER_CPU | GOOGLE_GRALLOC_PRODUCER_TPU);
}

/*
 * Determines the intersection of all IP consumers capability sets. Since all
 * capabiltiies are positive, the intersection can be expressed via a logical
 * AND operation. Capabilities must be defined (OPTIONS_PRESENT) to indicate
 * that an IP is part of the media system (otherwise it will be ignored).
 * See definition of MALI_GRALLOC_FORMAT_CAPABILITY_* for more information.
 *
 * @param consumers   [in]    Buffer consumers.
 *
 * @return flags word of common capabilities shared by *all* consumers;
 *         0, if no capabilities are shared
 */
static uint64_t get_consumer_caps(const uint16_t consumers)
{
	uint64_t consumer_caps = ~0;

	get_ip_capabilities();

	/* Consumers can't write */
	consumer_caps &= ~MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_YUV_WRITE;

	if (consumers_use_cpu_caps(consumers))
	{
		consumer_caps &= cpu_runtime_caps.caps_mask;
	}

	if (consumers & MALI_GRALLOC_CONSUMER_GPU &&
	    gpu_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
	{
		consumer_caps &= gpu_runtime_caps.caps_mask;
	}

	if (consumers & MALI_GRALLOC_CONSUMER_DPU &&
	    dpu_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
	{
		consumer_caps &= dpu_runtime_caps.caps_mask;
	}

	if (consumers & MALI_GRALLOC_CONSUMER_VPU &&
	    vpu_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
	{
		consumer_caps &= vpu_runtime_caps.caps_mask;
	}

	if (consumers & GOOGLE_GRALLOC_CONSUMER_BIG)
	{
#ifdef SOC_ZUMA
		if (bw_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
			consumer_caps &= bw_runtime_caps.caps_mask;
#else
		if (bo_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
			consumer_caps &= bo_runtime_caps.caps_mask;
#endif
	}

	if (consumers & GOOGLE_GRALLOC_CONSUMER_MFC &&
	    mfc_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
	{
		consumer_caps &= mfc_runtime_caps.caps_mask;
	}

	/* TODO: if consumers is 0, set consumer_caps to 0 as well */

	return consumer_caps;
}


/*
 * Determines the intersection of all IP producers capability sets. Since all
 * capabiltiies are positive, the intersection can be expressed via a logical
 * AND operation. Capabilities must be defined (OPTIONS_PRESENT) to indicate
 * that an IP is part of the media system (otherwise it will be ignored).
 * See definition of MALI_GRALLOC_FORMAT_CAPABILITY_* for more information.
 *
 * @param producers   [in]    Buffer producers.
 *
 * @return flags word of common capabilities shared by *all* producers;
 *         0, if no capabilities are shared
 */
static uint64_t get_producer_caps(const uint16_t producers)
{
	uint64_t producer_caps = ~0;

	if (producers == 0)
	{
		/* When no producer is specified assume no capabilities. */
		return 0;
	}

	get_ip_capabilities();

	/* Producers can't read */
	producer_caps &= ~MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_YUV_READ;

	if (producers_use_cpu_caps(producers))
	{
		producer_caps &= cpu_runtime_caps.caps_mask;
	}

	if (producers & MALI_GRALLOC_PRODUCER_GPU &&
	    gpu_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
	{
		producer_caps &= gpu_runtime_caps.caps_mask;
	}

	if (producers & MALI_GRALLOC_PRODUCER_DPU &&
	    dpu_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
	{
		producer_caps &= dpu_runtime_caps.caps_mask;
	}

	if (producers & MALI_GRALLOC_PRODUCER_CAM &&
	    cam_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
	{
		producer_caps &= cam_runtime_caps.caps_mask;
	}

	if (producers & MALI_GRALLOC_PRODUCER_VPU &&
	    vpu_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
	{
		producer_caps &= vpu_runtime_caps.caps_mask;
	}

	if (producers & GOOGLE_GRALLOC_PRODUCER_BIG)
	{
#ifdef SOC_ZUMA
		if (bw_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
			producer_caps &= bw_runtime_caps.caps_mask;
#else
		if (bo_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
			producer_caps &= bo_runtime_caps.caps_mask;
#endif
	}

	if (producers & GOOGLE_GRALLOC_PRODUCER_MFC &&
	    mfc_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
	{
		producer_caps &= mfc_runtime_caps.caps_mask;
	}

	return producer_caps;
}


/*
 * Update buffer dimensions for producer/consumer constraints. This process is
 * not valid with CPU producer/consumer since the new resolution cannot be
 * communicated to generic clients through the public APIs. Adjustments are
 * likely to be related to AFBC.
 *
 * @param alloc_format   [in]    Format (inc. modifiers) to be allocated.
 * @param usage          [in]    Buffer usage.
 * @param width          [inout] Buffer width (in pixels).
 * @param height         [inout] Buffer height (in pixels).
 *
 * @return none.
 */
void mali_gralloc_adjust_dimensions(const uint64_t alloc_format,
                                    const uint64_t usage,
                                    int* const width,
                                    int* const height)
{
	/* Determine producers. */
	const uint16_t producers = get_producers(usage);

	/*
	 * Video producer requires additional height padding of AFBC buffers (whole
	 * rows of 16x16 superblocks). Cropping will be applied to internal
	 * dimensions to fit the public size.
	 */
	if ((producers & GOOGLE_GRALLOC_PRODUCER_VPUS_MASK) &&
		(alloc_format & MALI_GRALLOC_INTFMT_AFBC_BASIC))
	{
		const int32_t idx = get_format_index(alloc_format & MALI_GRALLOC_INTFMT_FMT_MASK);
		if (idx != -1)
		{
			/* 8-bit/10-bit YUV420 formats. */
			if (formats[idx].is_yuv && formats[idx].hsub == 2 && formats[idx].vsub == 2)
			{
				*height += (alloc_format & MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS) ? 16 : 32;
			}
		}
	}

	if (producers & MALI_GRALLOC_PRODUCER_GPU)
	{
		/* Pad all AFBC allocations to multiple of GPU tile size. */
		if (alloc_format & MALI_GRALLOC_INTFMT_AFBC_BASIC)
		{
			*width = GRALLOC_ALIGN(*width, 16);
			*height = GRALLOC_ALIGN(*height, 16);
		}
	}

	MALI_GRALLOC_LOGV("%s: alloc_format=(%s 0x%" PRIx64 ") usage=(%s 0x%" PRIx64
		") alloc_width=%u, alloc_height=%u",
		__FUNCTION__, format_name(alloc_format), alloc_format, describe_usage(usage).c_str(),
		usage, *width, *height);
}


/*
 * Obtain level of support for base format across all producers and consumers as
 * defined by IP support table. This support is defined for the most capable IP -
 * specific IP might have reduced support based on specific capabilities.
 *
 * @param producers      [in]    Producers (flags).
 * @param consumers      [in]    Consumers (flags).
 * @param format         [in]    Format entry in IP support table.
 *
 * @return format support flags.
 */
static format_support_flags ip_supports_base_format(const uint16_t producers,
                                                    const uint16_t consumers,
                                                    const format_ip_support_t * const format)
{
	format_support_flags support = ~0;

	/* Determine producer support for base format. */
	if (producers_use_cpu_caps(producers))
	{
		support &= format->cpu_wr;
	}
	if (producers & MALI_GRALLOC_PRODUCER_GPU)
	{
		support &= format->gpu_wr;
	}
	if (producers & MALI_GRALLOC_PRODUCER_DPU)
	{
		support &= format->dpu_wr;
	}
	if (producers & MALI_GRALLOC_PRODUCER_CAM)
	{
		support &= format->cam_wr;
	}
	if (producers & GOOGLE_GRALLOC_PRODUCER_VPUS_MASK)
	{
		support &= format->vpu_wr;
	}

	/* Determine consumer support for base format. */
	if (consumers_use_cpu_caps(consumers))
	{
		support &= format->cpu_rd;
	}
	if (consumers & MALI_GRALLOC_CONSUMER_GPU)
	{
		support &= format->gpu_rd;
	}
	if (consumers & MALI_GRALLOC_CONSUMER_DPU)
	{
		support &= format->dpu_rd;
	}
	if (consumers & GOOGLE_GRALLOC_CONSUMER_VPUS_MASK)
	{
		support &= format->vpu_rd;
	}

	return support;
}


/*
 * Determines whether a base format is subsampled YUV, where each
 * chroma channel has fewer samples than the luma channel. The
 * sub-sampling is always a power of 2.
 *
 * @param base_format   [in]    Base format (internal).
 *
 * @return 1, where format is subsampled YUV;
 *         0, otherwise
 */
bool is_subsampled_yuv(const uint32_t base_format)
{
	unsigned long i;

	for (i = 0; i < num_formats; i++)
	{
		if (formats[i].id == (base_format & MALI_GRALLOC_INTFMT_FMT_MASK))
		{
			if (formats[i].is_yuv == true &&
			    (formats[i].hsub > 1 || formats[i].vsub > 1))
			{
				return true;
			}
		}
	}
	return false;
}


/*
 * Determines whether multi-plane AFBC (requires specific IP capabiltiies) is
 * supported across all producers and consumers.
 *
 * @param producers      [in]    Producers (flags).
 * @param consumers      [in]    Consumers (flags).
 * @param producer_caps  [in]    Producer capabilities (flags).
 * @param consumer_caps  [in]    Consumer capabilities (flags).
 *
 * @return 1, multiplane AFBC is supported
 *         0, otherwise
 */
static inline bool is_afbc_multiplane_supported(const uint16_t producers,
                                                const uint16_t consumers,
                                                const uint64_t producer_caps,
                                                const uint64_t consumer_caps)
{
	GRALLOC_UNUSED(consumers);

	return (producer_caps & consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC &&
	        producer_caps & consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_TILED_HEADERS &&
	        producer_caps & consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_EXTRAWIDEBLK &&
	        /*no_producer*/ consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_MULTIPLANE_READ &&
	        producers == 0) ? true : false;
}


/*
 * Determines whether a given base format is supported by all producers and
 * consumers. After checking broad support across producer/consumer IP, this
 * function uses capabilities to disable features (base formats and AFBC
 * modifiers) that are not supported by specific versions of each IP.
 *
 * @param fmt_idx        [in]    Index into format properties table (base format).
 * @param ip_fmt_idx     [in]    Index into format IP support table (base format).
 * @param usage          [in]    Buffer usage.
 * @param producers      [in]    Producers (flags).
 * @param consumers      [in]    Consumers (flags).
 * @param producer_caps  [in]    Producer capabilities (flags).
 * @param consumer_caps  [in]    Consumer capabilities (flags).
 *
 * @return format support flags.
 */
static format_support_flags is_format_supported(const int32_t fmt_idx,
                                                const int32_t ip_fmt_idx,
                                                const uint64_t usage,
                                                const uint16_t producers,
                                                const uint16_t consumers,
                                                const uint64_t producer_caps,
                                                const uint64_t consumer_caps)
{
	/* Determine format support from table. */
	format_support_flags f_flags = ip_supports_base_format(producers, consumers,
	                                                       &formats_ip_support[ip_fmt_idx]);

	/* Determine whether producers/consumers support required AFBC features. */
	if (f_flags & F_AFBC)
	{
		if (!formats[fmt_idx].afbc ||
		    (producer_caps & consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC) == 0)
		{
			f_flags &= ~F_AFBC;
		}

		/* Check that multi-plane format supported by producers/consumers. */
		if (formats[fmt_idx].npln > 1 &&
		    !is_afbc_multiplane_supported(producers, consumers, producer_caps, consumer_caps))
		{
			f_flags &= ~F_AFBC;
		}

		/* Apply some additional restrictions from producer_caps and consumer_caps */
		/* Some modifiers affect base format support */
		if (formats[fmt_idx].is_yuv)
		{
			if ((producer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_YUV_WRITE) == 0)
			{
				f_flags &= ~F_AFBC;
			}

			if ((consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_YUV_READ) == 0)
			{
				f_flags &= ~F_AFBC;
			}
		}

		if (usage & GRALLOC_USAGE_FRONT_BUFFER)
		{
			if ((producer_caps & consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_DOUBLE_BODY) == 0)
			{
				f_flags &= ~F_AFBC;
			}
		}
	}
	if (f_flags != F_NONE)
	{
		if (formats[fmt_idx].id == MALI_GRALLOC_FORMAT_INTERNAL_RGBA_1010102 &&
		    (producer_caps & consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_PIXFMT_RGBA1010102) == 0)
		{
			/* TODO: forcing F_LIN is a W/A. Make a proper solution */
			f_flags = F_NONE;
			f_flags = F_LIN;
		}
		else if (formats[fmt_idx].id == MALI_GRALLOC_FORMAT_INTERNAL_RGBA_16161616)
		{
			if ((producer_caps & consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_PIXFMT_RGBA16161616) == 0)
			{
				/* TODO: forcing F_LIN is a W/A. Make a proper solution */
				//f_flags = F_NONE;
				f_flags = F_LIN;
			}
			else if ((producer_caps & consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_RGBA16161616) == 0)
			{
				f_flags = F_LIN;
			}
		}
	}

	return f_flags;
}


/*
 * Ensures that the allocation format conforms to the AFBC specification and is
 * supported by producers and consumers. Format modifiers are (in most cases)
 * disabled as required to make valid. It is important to first resolve invalid
 * combinations which are not dependent upon others to reduce the possibility of
 * circular dependency.
 *
 * @param alloc_format          [in]    Allocation format (base + modifiers).
 * @param producer_active_caps  [in]    Producer capabilities (flags).
 * @param consumer_active_caps  [in]    Consumer capabilities (flags).
 *
 * @return valid alloc_format with AFBC possibly disabled (if required)
 */
static uint64_t validate_afbc_format(uint64_t alloc_format,
                                     const uint64_t producer_active_caps,
                                     const uint64_t consumer_active_caps)
{
	const uint32_t base_format = alloc_format & MALI_GRALLOC_INTFMT_FMT_MASK;

	/*
	 * AFBC with tiled-headers must be enabled for AFBC front-buffer-safe allocations.
	 * NOTE: format selection algorithm will always try and enable AFBC with
	 * tiled-headers where supported by producer(s) and consumer(s).
	 */
	if (alloc_format & MALI_GRALLOC_INTFMT_AFBC_DOUBLE_BODY)
	{
		/*
		 * Disable (extra-) wide-block which is unsupported with front-buffer safe AFBC.
		 */
		alloc_format &= ~MALI_GRALLOC_INTFMT_AFBC_WIDEBLK;
		alloc_format &= ~MALI_GRALLOC_INTFMT_AFBC_EXTRAWIDEBLK;
	}

	/*
	 * AFBC specification: Split-block is not supported for
	 * subsampled formats (YUV) when wide-block is enabled.
	 */
	if (alloc_format & MALI_GRALLOC_INTFMT_AFBC_WIDEBLK &&
	    alloc_format & MALI_GRALLOC_INTFMT_AFBC_SPLITBLK &&
	    is_subsampled_yuv(base_format))
	{
		/* Disable split-block instead of wide-block because because
		 * wide-block has greater impact on display performance.
		 */
		alloc_format &= ~MALI_GRALLOC_INTFMT_AFBC_SPLITBLK;
	}

	/* AFBC specification: Split-block must be enabled for
	 * non-subsampled formats > 16 bpp, where wide-block is enabled.
	 */
	if (alloc_format & MALI_GRALLOC_INTFMT_AFBC_WIDEBLK &&
	   (alloc_format & MALI_GRALLOC_INTFMT_AFBC_SPLITBLK) == 0 &&
	   !is_subsampled_yuv(base_format) &&
	   base_format != MALI_GRALLOC_FORMAT_INTERNAL_RGB_565)
	{
		/* Enable split-block if supported by producer(s) & consumer(s),
		 * otherwise disable wide-block.
		 */
		if (producer_active_caps & consumer_active_caps &
			MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK)
		{
			alloc_format |= MALI_GRALLOC_INTFMT_AFBC_SPLITBLK;
		}
		else
		{
			alloc_format &= ~MALI_GRALLOC_INTFMT_AFBC_WIDEBLK;
		}
	}

	/* Some RGB formats don't support split block. */
	if (base_format == MALI_GRALLOC_FORMAT_INTERNAL_RGB_565)
	{
		alloc_format &= ~MALI_GRALLOC_INTFMT_AFBC_SPLITBLK;
	}

	/* Ensure that AFBC features are supported by producers/consumers. */
	if ((alloc_format & MALI_GRALLOC_INTFMT_AFBC_BASIC) &&
	    (producer_active_caps & consumer_active_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC) == 0)
	{
		MALI_GRALLOC_LOGE("AFBC basic selected but not supported by producer/consumer. Disabling "
		       "MALI_GRALLOC_INTFMT_AFBC_BASIC");
		alloc_format &= ~MALI_GRALLOC_INTFMT_AFBC_BASIC;
	}

	if ((alloc_format & MALI_GRALLOC_INTFMT_AFBC_SPLITBLK) &&
	    (producer_active_caps & consumer_active_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK) == 0)
	{
		MALI_GRALLOC_LOGE("AFBC split-block selected but not supported by producer/consumer. Disabling "
		       "MALI_GRALLOC_INTFMT_AFBC_SPLITBLK");
		alloc_format &= ~MALI_GRALLOC_INTFMT_AFBC_SPLITBLK;
	}

	if ((alloc_format & MALI_GRALLOC_INTFMT_AFBC_WIDEBLK) &&
	    (producer_active_caps & consumer_active_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_WIDEBLK) == 0)
	{
		MALI_GRALLOC_LOGE("AFBC wide-block selected but not supported by producer/consumer. Disabling "
		       "MALI_GRALLOC_INTFMT_AFBC_WIDEBLK");
		alloc_format &= ~MALI_GRALLOC_INTFMT_AFBC_WIDEBLK;
	}

	if ((alloc_format & MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS) &&
	    (producer_active_caps & consumer_active_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_TILED_HEADERS) == 0)
	{
		MALI_GRALLOC_LOGE("AFBC tiled-headers selected but not supported by producer/consumer. Disabling "
		       "MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS");
		alloc_format &= ~MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS;
	}

	if (!((alloc_format & MALI_GRALLOC_INTFMT_AFBC_SPARSE) ||
	      (producer_active_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_WRITE_NON_SPARSE)))
	{
		MALI_GRALLOC_LOGE("AFBC sparse not selected while producer cannot write non-sparse. Enabling "
		      "MALI_GRALLOC_INTFMT_AFBC_SPARSE");
		alloc_format |= MALI_GRALLOC_INTFMT_AFBC_SPARSE;
	}

	return alloc_format;
}


/*
 * Derives a valid AFBC format (via modifiers) for all producers and consumers.
 * Formats are validated after enabling the largest feature set supported (and
 * desirable) for the IP usage. Some format modifier combinations are not
 * compatible. See MALI_GRALLOC_INTFMT_* modifiers for more information.
 *
 * @param base_format    [in]    Base format (internal).
 * @param usage          [in]    Buffer usage.
 * @param producer       [in]    Buffer producers (write).
 * @param consumer       [in]    Buffer consumers (read).
 * @param producer_caps  [in]    Buffer producer capabilities (intersection).
 * @param consumer_caps  [in]    Buffer consumer capabilities (intersection).
 *
 * @return valid AFBC format, where modifiers are enabled (supported/preferred);
 *         base format without modifers, otherwise
 */
static uint64_t get_afbc_format(const uint32_t base_format,
                                const uint64_t usage,
                                const uint16_t producer,
                                const uint16_t consumer,
                                const uint64_t producer_caps,
                                const uint64_t consumer_caps)
{
	uint64_t alloc_format = base_format;

	/*
	 * Determine AFBC modifiers where capabilities are defined for all producers
	 * and consumers. NOTE: AFBC is not supported for video transcode (VPU --> VPU).
	 */
	if (producer_caps & consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT &&
	    ((producer & GOOGLE_GRALLOC_PRODUCER_VPUS_MASK) == 0 || (consumer & GOOGLE_GRALLOC_CONSUMER_VPUS_MASK) == 0))
	{
		if (producer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC &&
		    consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC)
		{
			alloc_format |= MALI_GRALLOC_INTFMT_AFBC_BASIC;

#if 0
			const int format_idx = get_format_index(base_format);

			/* TODO: AFBC YUV TRANSFORM corrupts screen when enabled. find out why */
			if (format_idx != -1)
			{
				if (formats[format_idx].yuv_transform == true)
				{
					alloc_format |= MALI_GRALLOC_INTFMT_AFBC_YUV_TRANSFORM;
				}
			}
#endif

			if (!(producer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_WRITE_NON_SPARSE))
			{
				alloc_format |= MALI_GRALLOC_INTFMT_AFBC_SPARSE;
			}

			if (producer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_TILED_HEADERS &&
			    consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_TILED_HEADERS)
			{
				alloc_format |= MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS;

				if (usage & GRALLOC_USAGE_FRONT_BUFFER &&
				    producer_caps & consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_DOUBLE_BODY)
				{
					alloc_format |= MALI_GRALLOC_INTFMT_AFBC_DOUBLE_BODY;
				}
			}

			/*
			 * Specific producer/consumer combinations benefit from additional
			 * AFBC features (e.g. * --> GPU).
			 */
			if (consumer & MALI_GRALLOC_CONSUMER_GPU)
			{
				if (producer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK &&
				    consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK)
				{
					alloc_format |= MALI_GRALLOC_INTFMT_AFBC_SPLITBLK;
				}

				/*
				 * NOTE: assume that all AFBC layers are pre-rotated. 16x16 SB
				 * must be used with DPU consumer when rotation is required.
				 */
				if (producer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_WIDEBLK &&
				    consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_WIDEBLK)
				{
					alloc_format |= MALI_GRALLOC_INTFMT_AFBC_WIDEBLK;
				}
			}
		}
	}

	alloc_format = validate_afbc_format(alloc_format, producer_caps, consumer_caps);

	return alloc_format;
}

/*
 * Obtains the 'active' capabilities (for producers/consumers) by applying
 * additional constraints to the capabilities declared for each IP. Some rules
 * are based on format, others specific to producer/consumer. This function must
 * be careful not to make any assumptions about the base format properties since
 * fallback might still occur. It is safe to use any properties which are common
 * across all compatible formats as defined by is_format_compatible().
 *
 * @param format                 [in]    Base format requested.
 * @param producers              [in]    Producers (flags).
 * @param consumers              [in]    Consumers (flags).
 * @param producer_active_caps   [out]   Active producer capabilities (flags).
 * @param consumer_active_caps   [out]   Active consumer capabilities (flags).
 *
 * @return none.
 */
static void get_active_caps(const format_info_t format,
                            const uint16_t producers,
                            const uint16_t consumers,
                            uint64_t * const producer_active_caps,
                            uint64_t * const consumer_active_caps)
{
	const uint64_t producer_caps = (producer_active_caps) ? *producer_active_caps : 0;
	const uint64_t consumer_caps = (consumer_active_caps) ? *consumer_active_caps : 0;
	uint64_t producer_mask = ~0;
	uint64_t consumer_mask = ~0;

	if (format.is_yuv)
	{
		if ((producer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_YUV_WRITE) == 0)
		{
			producer_mask &= ~MALI_GRALLOC_FORMAT_CAPABILITY_AFBCENABLE_MASK;
		}
		else if (producers & MALI_GRALLOC_PRODUCER_GPU)
		{
			/* All GPUs that can write YUV AFBC can only do it in 16x16,
			 * optionally with tiled headers.
			 */
			producer_mask &=
				~(MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK |
				  MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_WIDEBLK);
		}

		if ((consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_YUV_READ) == 0)
		{
			consumer_mask &= ~MALI_GRALLOC_FORMAT_CAPABILITY_AFBCENABLE_MASK;
		}
	}

	// TODO: b/183385318 Prefer 16x16 AFBC over 32x8 for GPU --> GPU
	if ((producers & MALI_GRALLOC_PRODUCER_GPU) &&
	    (consumers & MALI_GRALLOC_CONSUMER_GPU) &&
            (producer_caps & consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC))
	{
		producer_mask &=
			~(MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK |
			  MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_WIDEBLK);

		consumer_mask &=
			~(MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK |
			  MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_WIDEBLK);
	}

	if (consumers & MALI_GRALLOC_CONSUMER_DPU)
	{
		/* DPU does not support split-block other than RGB(A) 24/32-bit */
		if (!format.is_rgb || format.bpp[0] < 24)
		{
			if (consumers & MALI_GRALLOC_CONSUMER_DPU)
			{
				consumer_mask &= ~MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK;
			}
		}
	}

	if (producer_active_caps)
	{
		*producer_active_caps &= producer_mask;
	}
	if (consumer_active_caps)
	{
		*consumer_active_caps &= consumer_mask;
	}
}


/*
 * Obtains support flags and modifiers for base format.
 *
 * @param base_format           [in]    Base format for which to deduce support.
 * @param usage                 [in]    Buffer usage.
 * @param producers             [in]    Producers (flags).
 * @param consumers             [in]    Consumers (flags).
 * @param producer_active_caps  [in]    Producer capabilities (flags).
 * @param consumer_active_caps  [in]    Consumer capabilities (flags).
 * @param fmt_supported         [out]   Format (base + modifiers) and support (flags).
 *
 * @return 1, base format supported
 *         0, otherwise
 */
bool get_supported_format(const uint32_t base_format,
                          const uint64_t usage,
                          const uint16_t producers,
                          const uint16_t consumers,
                          const uint64_t producer_active_caps,
                          const uint64_t consumer_active_caps,
                          fmt_props * const fmt_supported)
{
	const int32_t fmt_idx = get_format_index(base_format);
	const int32_t ip_fmt_idx = get_ip_format_index(base_format);
	assert(fmt_idx >= 0);
	if (ip_fmt_idx == -1)
	{
		/* Return undefined base format. */
		MALI_GRALLOC_LOGE("Failed to find IP support info for format id: 0x%" PRIx32,
		      base_format);
		return false;
	}

	fmt_supported->f_flags = is_format_supported(fmt_idx,
	                                             ip_fmt_idx,
	                                             usage,
	                                             producers,
	                                             consumers,
	                                             producer_active_caps,
	                                             consumer_active_caps);
	MALI_GRALLOC_LOGV("IP support: 0x%" PRIx16, fmt_supported->f_flags);
	if (fmt_supported->f_flags == F_NONE &&
	    consumers & MALI_GRALLOC_CONSUMER_GPU &&
	    consumers & MALI_GRALLOC_CONSUMER_DPU)
	{
		/* Determine alternative caps for formats when GPU/DPU consumer.
		 * Although we normally combine capabilities for multiple consumers with "AND",
		 * in some situations (e.g. formats) we make best effort and say that fallback
		 * to GPU is acceptable and perferred over rejecting allocation. GPU composition
		 * must always be supported in case of fallback from DPU.
		 */
		const uint16_t consumers_nodpu = consumers & ~MALI_GRALLOC_CONSUMER_DPU;
		uint64_t consumer_nodpu_caps = consumer_active_caps;

		/* Set consumer caps to GPU-only (assume superset of DPU). */
		consumer_nodpu_caps = get_consumer_caps(consumers_nodpu);
		get_active_caps(formats[fmt_idx],
		                producers, consumers_nodpu,
		                NULL, &consumer_nodpu_caps);

		fmt_supported->f_flags = is_format_supported(fmt_idx,
	                                                 ip_fmt_idx,
		                                             usage,
	                                                 producers,
	                                                 consumers_nodpu,
	                                                 producer_active_caps,
	                                                 consumer_nodpu_caps);
	}

	fmt_supported->base_format = base_format;
	if (fmt_supported->f_flags & F_AFBC)
	{
		const uint64_t afbc_format = get_afbc_format(base_format,
		                                             usage,
		                                             producers,
		                                             consumers,
		                                             producer_active_caps,
		                                             consumer_active_caps);

		MALI_GRALLOC_LOGV("AFBC format: (%s 0x%" PRIx64 ")",
			format_name(afbc_format), afbc_format);

		/* Disable AFBC when forced by usage or no format modifiers selected. */
		if ((usage & MALI_GRALLOC_USAGE_NO_AFBC) == MALI_GRALLOC_USAGE_NO_AFBC ||
		    afbc_format == fmt_supported->base_format)
		{
			fmt_supported->f_flags &= ~F_AFBC;
		}

		/* Check that AFBC features are correct for multiplane format. */
		alloc_type_t alloc_type{};
		get_alloc_type(afbc_format & MALI_GRALLOC_INTFMT_EXT_MASK,
					   fmt_idx,
					   usage,
					   &alloc_type);
		if (formats[fmt_idx].npln > 1 && alloc_type.is_multi_plane == false)
		{
			fmt_supported->f_flags &= ~F_AFBC;
		}

		/* Store any format modifiers */
		fmt_supported->format_ext = afbc_format & MALI_GRALLOC_INTFMT_EXT_MASK;
	}
	if ((fmt_supported->f_flags & F_AFBC) == 0)
	{
		fmt_supported->format_ext = 0;
	}

	MALI_GRALLOC_LOGV("Ext format: (%s 0x%" PRIx64 ")", format_name(fmt_supported->format_ext),
		fmt_supported->format_ext);

	return (fmt_supported->f_flags == F_NONE) ? false : true;
}


/*
 * Determines whether two base formats have comparable 'color' components. Alpha
 * is considered unimportant for YUV formats.
 *
 * @param f_old     [in]    Format properties (old format).
 * @param f_new     [in]    Format properties (new format).
 *
 * @return 1, format components are equivalent
 *         0, otherwise
 */
static bool comparable_components(const format_info_t * const f_old,
                                  const format_info_t * const f_new)
{
	if (f_old->is_yuv && f_new->bps == f_old->bps)
	{
		/* Formats have the same number of components. */
		if (f_new->total_components() == f_old->total_components())
		{
			return true;
		}

		/* Alpha component can be dropped for yuv formats.
		 * This assumption is required for mapping Y0L2 to
		 * single plane 10-bit YUV420 AFBC.
		 */
		if (f_old->has_alpha)
		{
			if (f_new->total_components() == 3 &&
			    f_new->is_yuv &&
			    !f_new->has_alpha)
			{
				return true;
			}
		}
	}
	else if (f_old->is_rgb)
	{
		if (f_new->total_components() == f_old->total_components())
		{
			if (f_new->bpp[0] == f_old->bpp[0] && f_new->bps == f_old->bps)
			{
				return true;
			}
		}
	}
	else
	{
		if (f_new->id == f_old->id)
		{
			return true;
		}
	}

	return false;
}


/*
 * Determines whether two base formats are compatible such that data from one
 * format could be accurately represented/interpreted in the other format.
 *
 * @param f_old     [in]    Format properties (old format).
 * @param f_new     [in]    Format properties (new format).
 *
 * @return 1, formats are equivalent
 *         0, otherwise
 */
static bool is_format_compatible(const format_info_t * const f_old,
                                 const format_info_t * const f_new)
{
	if (f_new->hsub == f_old->hsub &&
	    f_new->vsub == f_old->vsub &&
	    f_new->is_rgb == f_old->is_rgb &&
	    f_new->is_yuv == f_old->is_yuv &&
	    comparable_components(f_old, f_new))
	{
		return true;
	}
	else
	{
		return false;
	}
}

/**
 * Provide a grade for the compatible format with respect to the requested format. Used to find the best compatible
 * format.
 *
 * @param fmt[in]    Compatible format properties.
 * @param req_format Requested base format.
 *
 * @return The grade of the compatible format. Higher is better. Returns 0 if format extensions are incompatible with
 * requested format.
 */
uint64_t grade_format(const fmt_props &fmt, uint32_t req_format)
{
	uint64_t grade = 1;

	GRALLOC_UNUSED(req_format);

	static const struct {
		uint64_t fmt_ext;
		uint64_t value;
	} fmt_ext_values[] {
		{ MALI_GRALLOC_INTFMT_AFBC_BASIC, 1 },
		{ MALI_GRALLOC_INTFMT_AFBC_SPLITBLK, 1 },
		{ MALI_GRALLOC_INTFMT_AFBC_WIDEBLK, 1 },
		{ MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS, 1 },
		{ MALI_GRALLOC_INTFMT_AFBC_EXTRAWIDEBLK, 1 },
		{ MALI_GRALLOC_INTFMT_AFBC_DOUBLE_BODY, 1 },
		{ MALI_GRALLOC_INTFMT_AFBC_BCH, 1 },
		{ MALI_GRALLOC_INTFMT_AFBC_YUV_TRANSFORM, 1 },
		{ MALI_GRALLOC_INTFMT_AFBC_SPARSE, 1 },
	};
	for (auto& ext : fmt_ext_values)
	{
		if (fmt.format_ext & ext.fmt_ext)
		{
			grade += ext.value;
		}
	}

	return grade;
}

/*
 * Obtains the 'best' allocation format for requested format and usage:
 * 1. Find compatible base formats (based on format properties alone)
 * 2. Find base formats supported by producers/consumers
 * 3. Find best modifiers from supported base formats
 * 4. Select allocation format from "best" base format with "best" modifiers
 *
 * NOTE: Base format re-mapping should not take place when CPU usage is
 * requested.
 *
 * @param req_base_format       [in]    Base format requested by client.
 * @param usage                 [in]    Buffer usage.
 * @param producers             [in]    Producers (flags).
 * @param consumers             [in]    Consumers (flags).
 * @param producer_active_caps  [in]    Producer capabilities (flags).
 * @param consumer_active_caps  [in]    Consumer capabilities (flags).
 *
 * @return alloc_format, supported for usage;
 *         MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED, otherwise
 */
static uint64_t get_best_format(const uint32_t req_base_format,
                                const uint64_t usage,
                                const uint16_t producers,
                                const uint16_t consumers,
                                const uint64_t producer_active_caps,
                                const uint64_t consumer_active_caps)
{
	uint64_t alloc_format = MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED;

	assert(req_base_format != MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED);
	const int32_t req_fmt_idx = get_format_index(req_base_format);
	MALI_GRALLOC_LOGV("req_base_format: (%s 0x%" PRIx32 ")",
		format_name(req_base_format), req_base_format);
	MALI_GRALLOC_LOGV("req_fmt_idx: %d", req_fmt_idx);
	assert(req_fmt_idx >= 0);

	/* 1. Find compatible base formats. */
	std::vector<fmt_props> f_compat;
	for (uint16_t i = 0; i < num_formats; i++)
	{
		if (is_format_compatible(&formats[req_fmt_idx], &formats[i]))
		{
			fmt_props fmt = {0, 0, 0};
			fmt.base_format = formats[i].id;
			MALI_GRALLOC_LOGV("Compatible: Base-format: (%s 0x%" PRIx32 ")",
				format_name(fmt.base_format), fmt.base_format);
			f_compat.push_back(fmt);
		}
	}
	assert(f_compat.size() > 0);

	/* 2. Find base formats supported by IP and among them, find the highest
	 * number of modifier enabled format and check if requested format is present
	 */

	int32_t num_supported_formats = 0;
	uint64_t req_format_grade = 0;
	uint64_t best_fmt_grade = 0;
	uint64_t first_of_best_formats = MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED;
	uint64_t req_format = MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED;

	for (uint16_t i = 0; i < f_compat.size(); i++)
	{
		MALI_GRALLOC_LOGV("Compatible: Base-format: 0x%" PRIx32, f_compat[i].base_format);
		fmt_props fmt = {0, 0, 0};
		bool supported = get_supported_format(f_compat[i].base_format,
		                                      usage,
		                                      producers,
		                                      consumers,
		                                      producer_active_caps,
		                                      consumer_active_caps,
		                                      &fmt);
		if (supported)
		{
			const uint64_t sup_fmt_grade = grade_format(fmt, req_base_format);
			if (sup_fmt_grade)
			{
				num_supported_formats++;
				MALI_GRALLOC_LOGV("Supported: Base-format: (%s 0x%" PRIx32 "), Modifiers: 0x%" PRIx64 ", Flags: 0x%" PRIx16,
			      format_name(fmt.base_format), fmt.base_format, fmt.format_ext, fmt.f_flags);

				/* 3. Find best modifiers from supported base formats */
				if (sup_fmt_grade > best_fmt_grade)
				{
					best_fmt_grade = sup_fmt_grade;
					first_of_best_formats = fmt.base_format | fmt.format_ext;
				}

				/* Check if current supported format is same as requested format */
				if (fmt.base_format == req_base_format)
				{
					req_format_grade = sup_fmt_grade;
					req_format = fmt.base_format | fmt.format_ext;
				}
			}
		}
	}

	/* 4. Select allocation format from "best" base format with "best" modifiers */
	if (num_supported_formats > 0)
	{
		/* Select first/one of best format when requested format is either not
		* supported or requested format is not the best format.
		*/
		if ((req_format_grade != best_fmt_grade) &&
			(((producers & MALI_GRALLOC_PRODUCER_CPU) == 0) &&
			((consumers & MALI_GRALLOC_CONSUMER_CPU) == 0)))
		{
			alloc_format = first_of_best_formats;
		}
		else if (req_format_grade != 0)
		{
			alloc_format = req_format;
		}
	}

	MALI_GRALLOC_LOGV("Selected format: (%s 0x%" PRIx64 ")",
		format_name(alloc_format), alloc_format);
	return alloc_format;
}


/*
 * Obtain format modifiers from requested format.
 *
 * @param req_format       [in]    Requested format (base + optional modifiers).
 * @param usage            [in]    Buffer usage.
 *
 * @return format modifiers, where extracted from requested format;
 *         0, otherwise
 */
uint64_t get_format_ext(const uint64_t req_format, const uint64_t usage)
{
	/* TODO: clean up this function. Or remove it */
	GRALLOC_UNUSED(usage);
	return req_format & MALI_GRALLOC_INTFMT_EXT_MASK;
}


/*
 * Obtain base format from requested format. There are two primary ways in which
 * the client can specify requested format:
 * - Public API:
 *   - Normal usage, with HAL_PIXEL_FORMAT_* / MALI_GRALLOC_FORMAT_INTERNAL_*
 *   - Private usage, (as normal usage) with additional format modifiers (MALI_GRALLOC_INTFMT_*)
 * - Private API: allows private usage to be provided explicitly
 *                (type == MALI_GRALLOC_FORMAT_TYPE_INTERNAL)
 *
 * @param req_format       [in]    Requested format (base + optional modifiers).
 * @param usage            [in]    Buffer usage.
 * @param type             [in]    Format type (public usage or internal).
 * @param map_to_internal  [in]    Base format (if public HAL_PIXEL_FORMAT_*)
 *                                 should be mapped to internal representation.
 *
 * @return base format, where identified/translated from requested format;
 *         MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED, otherwise
 */
uint32_t get_base_format(const uint64_t req_format,
                         const uint64_t usage,
                         const mali_gralloc_format_type type,
                         const bool map_to_internal)
{
	GRALLOC_UNUSED(type);

	uint32_t base_format = MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED;

	/* Mask out extension bits which could be present with type 'internal'. */
	base_format = req_format & MALI_GRALLOC_INTFMT_FMT_MASK;

	/* Map Android flexible formats to internal base formats */
	if (req_format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)
	{
		auto consumers = get_consumers(usage);
		auto producers = get_producers(usage);

		if ((usage & GRALLOC_USAGE_HW_TEXTURE) || (usage & GRALLOC_USAGE_HW_COMPOSER))
		{
			base_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN;
		}
		else if ((producers & MALI_GRALLOC_PRODUCER_CAM) &&
			 !(producers & MALI_GRALLOC_PRODUCER_GPU) &&
			 (consumers == GOOGLE_GRALLOC_CONSUMER_MFC))
		{
			// Allocated camera buffer is SBWC compressed when
			// 1. Camera is one of the producers
			// 2. GPU is not one of the producers
			// 3. MFC is the sole consumer
			if (property_get_bool("debug.vendor.gpu.record_sbwc", true)) {
				base_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC;
			} else {
				base_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN;
			}
		}
		else if (get_consumers(usage) & GOOGLE_GRALLOC_CONSUMER_BIG)
		{
			base_format = HAL_PIXEL_FORMAT_GOOGLE_NV12_SP;
		}
		else if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER)
		{
			base_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN;
		}
		else if (usage & GRALLOC_USAGE_VIDEO_PRIVATE_DATA)
		{
			base_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN;
		}
		else if ((usage & GRALLOC_USAGE_HW_CAMERA_READ) && (usage & GRALLOC_USAGE_HW_CAMERA_WRITE))
		{
			// Camera IMPLEMENTATION_DEFINED format output maps to NV21.
			base_format = HAL_PIXEL_FORMAT_YCrCb_420_SP;
		}
		else
		{
			base_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN;
		}
	}
	else if (req_format == HAL_PIXEL_FORMAT_YCbCr_420_888)
	{
		if (get_consumers(usage) & GOOGLE_GRALLOC_CONSUMER_BIG)
		{
			base_format = HAL_PIXEL_FORMAT_GOOGLE_NV12_SP;
		}
		else if (usage & (GRALLOC_USAGE_HW_VIDEO_ENCODER | GRALLOC_USAGE_HW_VIDEO_DECODER))
		{
			base_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN;
		}
		else if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
		{
			// Catchall for camera write. DO NOT CHANGE WITHOUT TESTING THESE SCENARIOS:
			// 1. Camera capture and initial photo processing
			// 2. Other major camera operations - video recording, portrait etc
			// 3. Faceauth
			// 4. Multi-profile user photo add
			// 5. Capture and resize - use chat app to capture a photo
			// Re-run these steps with GPU composition:
			// adb shell service call SurfaceFlinger 1008 i32 1
			base_format = HAL_PIXEL_FORMAT_YCrCb_420_SP;
		}
		else
		{
			base_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN;
		}
	}
	else if (req_format == HAL_PIXEL_FORMAT_YCBCR_P010)
	{
		if (get_consumers(usage) & GOOGLE_GRALLOC_CONSUMER_BIG)
		{
			base_format = HAL_PIXEL_FORMAT_GOOGLE_NV12_SP_10B;
		}
		else if (usage & (GRALLOC_USAGE_HW_VIDEO_ENCODER | GRALLOC_USAGE_HW_VIDEO_DECODER))
		{
			base_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_SPN;
		}
	}

	/* Obtain a valid base format, optionally mapped to internal. Flex formats
	 * are always mapped to internal base format.
	 * NOTE: Overlap between HAL_PIXEL_FORMAT_* and MALI_GRALLOC_FORMAT_INTERNAL_*
	 * is intentional. See enumerations for more information.
	 */
	return get_internal_format(base_format, map_to_internal);
}


/*
 * Select pixel format (base + modifier) for allocation.
 *
 * @param req_format       [in]   Format (base + optional modifiers) requested by client.
 * @param type             [in]   Format type (public usage or internal).
 * @param usage            [in]   Buffer usage.
 *
 * @return alloc_format, format to be used in allocation;
 *         MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED, where no suitable
 *         format could be found.
 */
uint64_t mali_gralloc_select_format(const uint64_t req_format,
                                    const mali_gralloc_format_type type,
                                    const uint64_t usage)
{
	uint64_t alloc_format = MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED;

	/*
	 * Obtain base_format (no extension bits) and indexes into format tables.
	 */
	const uint32_t req_base_format = get_base_format(req_format, usage, type, true);
	const int32_t req_fmt_idx = get_format_index(req_base_format);
	if (req_base_format == MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED ||
	    req_fmt_idx == -1)
	{
		MALI_GRALLOC_LOGE("Invalid base format! req_base_format = (%s 0x%" PRIx32
			"), req_format = (%s 0x%" PRIx64 "), type = 0x%" PRIx32,
			format_name(req_base_format), req_base_format, format_name(req_format), req_format, type);
		goto out;
	}

	/* Reject if usage specified is outside white list of valid usages. */
	if (type != MALI_GRALLOC_FORMAT_TYPE_INTERNAL && (usage & (~VALID_USAGE)) != 0)
	{
		MALI_GRALLOC_LOGE("Invalid usage specified: %s 0x%" PRIx64, describe_usage(usage).c_str(), usage);
	}

	/* TODO: Make a function for finding formats that should be allocated as the request format */
	if (is_exynos_format(req_base_format) || req_base_format == HAL_PIXEL_FORMAT_BLOB)
	{
		alloc_format = req_base_format;
	}
	else if (usage == 0)
	{
		/* Allocate format as-is when no usage is specified */
		alloc_format = req_base_format;
	}
	else
	{
		/* Determine producers and consumers. */
		const uint16_t producers = get_producers(usage);
		const uint16_t consumers = get_consumers(usage);

		MALI_GRALLOC_LOGV("Producers: 0x%" PRIx16 ", Consumers: 0x%" PRIx16,
		      producers, consumers);

		/* Obtain producer and consumer capabilities. */
		const uint64_t producer_caps = get_producer_caps(producers);

		uint64_t consumer_caps = 0;
#ifdef GRALLOC_HWC_FB_DISABLE_AFBC
		if (GRALLOC_HWC_FB_DISABLE_AFBC && DISABLE_FRAMEBUFFER_HAL && (usage & GRALLOC_USAGE_HW_FB))
		{
			/* Override capabilities to disable AFBC for DRM HWC framebuffer surfaces. */
			consumer_caps = MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT;
		}
		else
#endif
		{
			consumer_caps = get_consumer_caps(consumers);
		}

		MALI_GRALLOC_LOGV("Producer caps: 0x%" PRIx64 ", Consumer caps: 0x%" PRIx64,
		      producer_caps, consumer_caps);

		if (producers == 0 && consumers == 0)
		{
			MALI_GRALLOC_LOGE("Producer and consumer not identified.");
			goto out;
		}
		else if (producers == 0 || consumers == 0)
		{
			MALI_GRALLOC_LOGV("Producer or consumer not identified.");
		}

		if ((usage & MALI_GRALLOC_USAGE_NO_AFBC) == MALI_GRALLOC_USAGE_NO_AFBC &&
		    formats[req_fmt_idx].is_yuv)
		{
			MALI_GRALLOC_LOGE("ERROR: Invalid usage 'MALI_GRALLOC_USAGE_NO_AFBC' when allocating YUV formats");
			goto out;
		}

		uint64_t producer_active_caps = producer_caps;
		uint64_t consumer_active_caps = consumer_caps;

		get_active_caps(formats[req_fmt_idx],
		                producers, consumers,
		                &producer_active_caps, &consumer_active_caps);

		MALI_GRALLOC_LOGV("Producer caps (active): 0x%" PRIx64 ", Consumer caps (active): 0x%" PRIx64,
		      producer_active_caps, consumer_active_caps);

		/* TODO: reimplment get_best_format */
		alloc_format = get_best_format(formats[req_fmt_idx].id,
		                               usage,
		                               producers,
		                               consumers,
		                               producer_active_caps,
		                               consumer_active_caps);
	}

out:
	MALI_GRALLOC_LOGV("%s: req_format=(%s 0x%08" PRIx64 "), usage=(%s 0x%" PRIx64
	      "), req_base_format=(%s 0x%" PRIx32 "), alloc_format=(%s, 0x%" PRIx64 ")", __func__,
	      format_name(req_format), req_format, describe_usage(usage).c_str(), usage,
		  format_name(req_base_format), req_base_format, format_name(alloc_format), alloc_format);

	return alloc_format;
}

bool is_exynos_format(uint32_t base_format)
{
	switch (base_format)
	{
		case HAL_PIXEL_FORMAT_YCrCb_420_SP:
		case HAL_PIXEL_FORMAT_EXYNOS_YV12_M:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P_M:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_TILED:
		case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M:
		case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_FULL:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_S10B:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_SPN:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_M:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC:
		case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_SBWC:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC:
		case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_10B_SBWC:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L50:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L75:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L50:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L75:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L40:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L60:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L80:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L40:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L60:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L80:
			return true;
	}

	return false;
}

uint8_t get_exynos_fd_count(uint32_t format) {
	switch (format)
	{
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC:
		case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_SBWC:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC:
		case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_10B_SBWC:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L50:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L75:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L40:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L60:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L80:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_TILED:
		case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M:
		case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_FULL:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_M:
			return 2;
		case HAL_PIXEL_FORMAT_EXYNOS_YV12_M:
		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P_M:
			return 3;
	}

	return 1;
}