• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016-2020 ARM Limited. All rights reserved.
3  *
4  * Copyright (C) 2008 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include <string.h>
20 #include <dlfcn.h>
21 #include <inttypes.h>
22 #include <log/log.h>
23 #include <assert.h>
24 #include <vector>
25 #include <cutils/properties.h>
26 
27 #include "gralloc_priv.h"
28 #include "mali_gralloc_bufferallocation.h"
29 #include "mali_gralloc_usages.h"
30 #include "format_info.h"
31 #include "capabilities/gralloc_capabilities.h"
32 #include "exynos_format.h"
33 
34 /* Producer/consumer definitions.
35  * CPU: Software access
36  * GPU: Graphics processor
37  * DPU: Display processor
38  * VPU: Video processor
39  * CAM: Camera ISP
40  * TPU: ML accelerator
41  */
42 #define MALI_GRALLOC_PRODUCER_CPU		((uint16_t)1 << 0)
43 #define MALI_GRALLOC_PRODUCER_GPU		((uint16_t)1 << 1)
44 #define MALI_GRALLOC_PRODUCER_DPU		((uint16_t)1 << 2)
45 #define MALI_GRALLOC_PRODUCER_VPU		((uint16_t)1 << 4)
46 #define MALI_GRALLOC_PRODUCER_CAM		((uint16_t)1 << 5)
47 #define GOOGLE_GRALLOC_PRODUCER_BIG		((uint16_t)1 << 6)
48 #define GOOGLE_GRALLOC_PRODUCER_MFC		((uint16_t)1 << 7)
49 #define GOOGLE_GRALLOC_PRODUCER_VPUS_MASK	(MALI_GRALLOC_PRODUCER_VPU | GOOGLE_GRALLOC_PRODUCER_BIG | GOOGLE_GRALLOC_PRODUCER_MFC)
50 #define GOOGLE_GRALLOC_PRODUCER_TPU		((uint16_t)1 << 8)
51 
52 #define MALI_GRALLOC_CONSUMER_CPU		((uint16_t)1 << 0)
53 #define MALI_GRALLOC_CONSUMER_GPU		((uint16_t)1 << 1)
54 #define MALI_GRALLOC_CONSUMER_DPU		((uint16_t)1 << 2)
55 #define MALI_GRALLOC_CONSUMER_VPU		((uint16_t)1 << 3)
56 #define GOOGLE_GRALLOC_CONSUMER_BIG		((uint16_t)1 << 4)
57 #define GOOGLE_GRALLOC_CONSUMER_MFC		((uint16_t)1 << 5)
58 #define GOOGLE_GRALLOC_CONSUMER_VPUS_MASK	(MALI_GRALLOC_CONSUMER_VPU | GOOGLE_GRALLOC_CONSUMER_BIG | GOOGLE_GRALLOC_CONSUMER_MFC)
59 #define GOOGLE_GRALLOC_CONSUMER_TPU		((uint16_t)1 << 6)
60 
61 
62 typedef struct
63 {
64 	uint32_t base_format;
65 	uint64_t format_ext;
66 	format_support_flags f_flags;
67 } fmt_props;
68 
69 /*
70  * Video encoder consumer can be signalled by a combination of usage flags
71  * Besides VIDEO_ENCODER, clients can specify the specific IP block they belong
72  * to, for fine-grained control over capabilities.
73  *
74  * @param usage  [in]    Buffer usage.
75  *
76  * @return The corresponding consumer flag, or 0 if the consumer is not a VPU.
77  */
get_vpu_consumer(uint64_t usage)78 static uint16_t get_vpu_consumer(uint64_t usage)
79 {
80 	if (!(usage & hidl_common::BufferUsage::VIDEO_ENCODER))
81 		return 0;
82 
83 	/* When both the BIG and MFC flags are present, the assumption is BIG is the
84 	   producer and MFC is the consumer. There is no use case as of now in which
85 	   MFC is the producer and BIG is the consumer. */
86 	if (usage & GRALLOC_USAGE_GOOGLE_IP_MFC)
87 		return GOOGLE_GRALLOC_CONSUMER_MFC;
88 
89 	if (usage & GRALLOC_USAGE_GOOGLE_IP_BIG)
90 		return GOOGLE_GRALLOC_CONSUMER_BIG;
91 
92 	// TODO(b/185896428): Support 64-bits usage version for GraphicBufferSource
93 	return GOOGLE_GRALLOC_CONSUMER_MFC;
94 }
95 
96 /*
97  * Determines all IP consumers included by the requested buffer usage.
98  * Private usage flags are excluded from this process.
99  *
100  * @param usage   [in]    Buffer usage.
101  *
102  * @return flags word of all enabled consumers;
103  *         0, if no consumers are enabled
104  */
get_consumers(uint64_t usage)105 static uint16_t get_consumers(uint64_t usage)
106 {
107 	uint16_t consumers = 0;
108 
109 	if (usage & GS101_GRALLOC_USAGE_TPU_INPUT)
110 	{
111 		consumers |= GOOGLE_GRALLOC_CONSUMER_TPU;
112 	}
113 
114 	/* Exclude usages also not applicable to consumer derivation */
115 	usage &= ~GRALLOC_USAGE_PROTECTED;
116 
117 	get_ip_capabilities();
118 
119 	if ((usage & GRALLOC_USAGE_PUBLIC_MASK) == GRALLOC_USAGE_HW_COMPOSER)
120 	{
121 		consumers = MALI_GRALLOC_CONSUMER_DPU;
122 	}
123 	else
124 	{
125 		if (usage & GRALLOC_USAGE_SW_READ_MASK)
126 		{
127 			consumers |= MALI_GRALLOC_CONSUMER_CPU;
128 		}
129 
130 		/* GRALLOC_USAGE_HW_FB describes a framebuffer which contains a
131 		 * pre-composited scene that is scanned-out to a display. This buffer
132 		 * can be consumed by even the most basic display processor which does
133 		 * not support multi-layer composition.
134 		 */
135 		if (usage & GRALLOC_USAGE_HW_FB)
136 		{
137 			consumers |= MALI_GRALLOC_CONSUMER_DPU;
138 		}
139 
140 		consumers |= get_vpu_consumer(usage);
141 
142 		/* GRALLOC_USAGE_HW_COMPOSER does not explicitly define whether the
143 		 * display processor is producer or consumer. When used in combination
144 		 * with GRALLOC_USAGE_HW_TEXTURE, it is assumed to be consumer since the
145 		 * GPU and DPU both act as compositors.
146 		 */
147 		if ((usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_COMPOSER)) ==
148 		    (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_COMPOSER))
149 		{
150 			consumers |= MALI_GRALLOC_CONSUMER_DPU;
151 		}
152 
153 		if (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_GPU_DATA_BUFFER))
154 		{
155 			consumers |= MALI_GRALLOC_CONSUMER_GPU;
156 		}
157 	}
158 
159 	return consumers;
160 }
161 
162 /*
163  * Video decoder producer can be signalled by a combination of usage flags
164  * Besides VIDEO_DECODER, clients can specify the specific IP block they belong
165  * to, for fine-grained control over capabilities.
166  *
167  * @param usage  [in]    Buffer usage.
168  *
169  * @return The corresponding producer flag, or 0 if the producer is not a VPU.
170  */
get_vpu_producer(uint64_t usage)171 static uint16_t get_vpu_producer(uint64_t usage)
172 {
173 	if (!(usage & hidl_common::BufferUsage::VIDEO_DECODER))
174 		return 0;
175 
176 	/* When both the BIG and MFC flags are present, the assumption is BIG is the
177 	   producer and MFC is the consumer. There is no use case as of now in which
178 	   MFC is the producer and BIG is the consumer. */
179 	if (usage & GRALLOC_USAGE_GOOGLE_IP_BIG)
180 		return GOOGLE_GRALLOC_PRODUCER_BIG;
181 
182 	if (usage & GRALLOC_USAGE_GOOGLE_IP_MFC)
183 		return GOOGLE_GRALLOC_PRODUCER_MFC;
184 
185 	MALI_GRALLOC_LOGV("Video producer IP not specified, falling back to default VPU producer");
186 	return MALI_GRALLOC_PRODUCER_VPU;
187 }
188 
189 /*
190  * Determines all IP producers included by the requested buffer usage.
191  * Private usage flags are excluded from this process.
192  *
193  * @param usage   [in]    Buffer usage.
194  *
195  * @return flags word of all enabled producers;
196  *         0, if no producers are enabled
197  */
get_producers(uint64_t usage)198 static uint16_t get_producers(uint64_t usage)
199 {
200 	uint16_t producers = 0;
201 
202 	if (usage & GS101_GRALLOC_USAGE_TPU_OUTPUT)
203 	{
204 		producers |= GOOGLE_GRALLOC_PRODUCER_TPU;
205 	}
206 
207 	/* Exclude usages also not applicable to producer derivation */
208 	usage &= ~GRALLOC_USAGE_PROTECTED;
209 
210 	get_ip_capabilities();
211 
212 	if (usage & GRALLOC_USAGE_SW_WRITE_MASK)
213 	{
214 		producers |= MALI_GRALLOC_PRODUCER_CPU;
215 	}
216 
217 	/* DPU is normally consumer however, when there is an alternative
218 	 * consumer (VPU) and no other producer (e.g. VPU), it acts as a producer.
219 	 */
220 	if ((usage & GRALLOC_USAGE_DECODER) != GRALLOC_USAGE_DECODER &&
221 	    (usage & (GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_VIDEO_ENCODER)) ==
222 	    (GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_VIDEO_ENCODER))
223 	{
224 		producers |= MALI_GRALLOC_PRODUCER_DPU;
225 	}
226 
227 	if (usage & (GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_GPU_DATA_BUFFER))
228 	{
229 		producers |= MALI_GRALLOC_PRODUCER_GPU;
230 	}
231 
232 	if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
233 	{
234 		producers |= MALI_GRALLOC_PRODUCER_CAM;
235 	}
236 
237 	producers |= get_vpu_producer(usage);
238 
239 	return producers;
240 }
241 
consumers_use_cpu_caps(uint16_t consumers)242 static inline bool consumers_use_cpu_caps(uint16_t consumers)
243 {
244 	return consumers & (MALI_GRALLOC_CONSUMER_CPU | GOOGLE_GRALLOC_CONSUMER_TPU);
245 }
246 
producers_use_cpu_caps(uint16_t producers)247 static inline bool producers_use_cpu_caps(uint16_t producers)
248 {
249 	return producers & (MALI_GRALLOC_PRODUCER_CPU | GOOGLE_GRALLOC_PRODUCER_TPU);
250 }
251 
252 /*
253  * Determines the intersection of all IP consumers capability sets. Since all
254  * capabiltiies are positive, the intersection can be expressed via a logical
255  * AND operation. Capabilities must be defined (OPTIONS_PRESENT) to indicate
256  * that an IP is part of the media system (otherwise it will be ignored).
257  * See definition of MALI_GRALLOC_FORMAT_CAPABILITY_* for more information.
258  *
259  * @param consumers   [in]    Buffer consumers.
260  *
261  * @return flags word of common capabilities shared by *all* consumers;
262  *         0, if no capabilities are shared
263  */
get_consumer_caps(const uint16_t consumers)264 static uint64_t get_consumer_caps(const uint16_t consumers)
265 {
266 	uint64_t consumer_caps = ~0;
267 
268 	get_ip_capabilities();
269 
270 	/* Consumers can't write */
271 	consumer_caps &= ~MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_YUV_WRITE;
272 
273 	if (consumers_use_cpu_caps(consumers))
274 	{
275 		consumer_caps &= cpu_runtime_caps.caps_mask;
276 	}
277 
278 	if (consumers & MALI_GRALLOC_CONSUMER_GPU &&
279 	    gpu_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
280 	{
281 		consumer_caps &= gpu_runtime_caps.caps_mask;
282 	}
283 
284 	if (consumers & MALI_GRALLOC_CONSUMER_DPU &&
285 	    dpu_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
286 	{
287 		consumer_caps &= dpu_runtime_caps.caps_mask;
288 	}
289 
290 	if (consumers & MALI_GRALLOC_CONSUMER_VPU &&
291 	    vpu_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
292 	{
293 		consumer_caps &= vpu_runtime_caps.caps_mask;
294 	}
295 
296 	if (consumers & GOOGLE_GRALLOC_CONSUMER_BIG)
297 	{
298 #ifdef SOC_ZUMA
299 		if (bw_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
300 			consumer_caps &= bw_runtime_caps.caps_mask;
301 #else
302 		if (bo_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
303 			consumer_caps &= bo_runtime_caps.caps_mask;
304 #endif
305 	}
306 
307 	if (consumers & GOOGLE_GRALLOC_CONSUMER_MFC &&
308 	    mfc_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
309 	{
310 		consumer_caps &= mfc_runtime_caps.caps_mask;
311 	}
312 
313 	/* TODO: if consumers is 0, set consumer_caps to 0 as well */
314 
315 	return consumer_caps;
316 }
317 
318 
319 /*
320  * Determines the intersection of all IP producers capability sets. Since all
321  * capabiltiies are positive, the intersection can be expressed via a logical
322  * AND operation. Capabilities must be defined (OPTIONS_PRESENT) to indicate
323  * that an IP is part of the media system (otherwise it will be ignored).
324  * See definition of MALI_GRALLOC_FORMAT_CAPABILITY_* for more information.
325  *
326  * @param producers   [in]    Buffer producers.
327  *
328  * @return flags word of common capabilities shared by *all* producers;
329  *         0, if no capabilities are shared
330  */
get_producer_caps(const uint16_t producers)331 static uint64_t get_producer_caps(const uint16_t producers)
332 {
333 	uint64_t producer_caps = ~0;
334 
335 	if (producers == 0)
336 	{
337 		/* When no producer is specified assume no capabilities. */
338 		return 0;
339 	}
340 
341 	get_ip_capabilities();
342 
343 	/* Producers can't read */
344 	producer_caps &= ~MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_YUV_READ;
345 
346 	if (producers_use_cpu_caps(producers))
347 	{
348 		producer_caps &= cpu_runtime_caps.caps_mask;
349 	}
350 
351 	if (producers & MALI_GRALLOC_PRODUCER_GPU &&
352 	    gpu_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
353 	{
354 		producer_caps &= gpu_runtime_caps.caps_mask;
355 	}
356 
357 	if (producers & MALI_GRALLOC_PRODUCER_DPU &&
358 	    dpu_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
359 	{
360 		producer_caps &= dpu_runtime_caps.caps_mask;
361 	}
362 
363 	if (producers & MALI_GRALLOC_PRODUCER_CAM &&
364 	    cam_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
365 	{
366 		producer_caps &= cam_runtime_caps.caps_mask;
367 	}
368 
369 	if (producers & MALI_GRALLOC_PRODUCER_VPU &&
370 	    vpu_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
371 	{
372 		producer_caps &= vpu_runtime_caps.caps_mask;
373 	}
374 
375 	if (producers & GOOGLE_GRALLOC_PRODUCER_BIG)
376 	{
377 #ifdef SOC_ZUMA
378 		if (bw_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
379 			producer_caps &= bw_runtime_caps.caps_mask;
380 #else
381 		if (bo_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
382 			producer_caps &= bo_runtime_caps.caps_mask;
383 #endif
384 	}
385 
386 	if (producers & GOOGLE_GRALLOC_PRODUCER_MFC &&
387 	    mfc_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
388 	{
389 		producer_caps &= mfc_runtime_caps.caps_mask;
390 	}
391 
392 	return producer_caps;
393 }
394 
395 
396 /*
397  * Update buffer dimensions for producer/consumer constraints. This process is
398  * not valid with CPU producer/consumer since the new resolution cannot be
399  * communicated to generic clients through the public APIs. Adjustments are
400  * likely to be related to AFBC.
401  *
402  * @param alloc_format   [in]    Format (inc. modifiers) to be allocated.
403  * @param usage          [in]    Buffer usage.
404  * @param width          [inout] Buffer width (in pixels).
405  * @param height         [inout] Buffer height (in pixels).
406  *
407  * @return none.
408  */
mali_gralloc_adjust_dimensions(const uint64_t alloc_format,const uint64_t usage,uint64_t * const width,uint64_t * const height)409 void mali_gralloc_adjust_dimensions(const uint64_t alloc_format,
410                                     const uint64_t usage,
411                                     uint64_t* const width,
412                                     uint64_t* const height)
413 {
414 	/* Determine producers. */
415 	const uint16_t producers = get_producers(usage);
416 
417 	/*
418 	 * Video producer requires additional height padding of AFBC buffers (whole
419 	 * rows of 16x16 superblocks). Cropping will be applied to internal
420 	 * dimensions to fit the public size.
421 	 */
422 	if ((producers & GOOGLE_GRALLOC_PRODUCER_VPUS_MASK) &&
423 		(alloc_format & MALI_GRALLOC_INTFMT_AFBC_BASIC))
424 	{
425 		const int32_t idx = get_format_index(alloc_format & MALI_GRALLOC_INTFMT_FMT_MASK);
426 		if (idx != -1)
427 		{
428 			/* 8-bit/10-bit YUV420 formats. */
429 			if (formats[idx].is_yuv && formats[idx].hsub == 2 && formats[idx].vsub == 2)
430 			{
431 				*height += (alloc_format & MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS) ? 16 : 32;
432 			}
433 		}
434 	}
435 
436 	if (producers & MALI_GRALLOC_PRODUCER_GPU)
437 	{
438 		/* Pad all AFBC allocations to multiple of GPU tile size. */
439 		if (alloc_format & MALI_GRALLOC_INTFMT_AFBC_BASIC)
440 		{
441 			*width = GRALLOC_ALIGN(*width, 16);
442 			*height = GRALLOC_ALIGN(*height, 16);
443 		}
444 	}
445 
446 	MALI_GRALLOC_LOGV("%s: alloc_format=(%s 0x%" PRIx64 ") usage=(%s 0x%" PRIx64
447 		") alloc_width=%" PRIu64 ", alloc_height=%" PRIu64 "",
448 		__FUNCTION__, format_name(alloc_format), alloc_format, describe_usage(usage).c_str(),
449 		usage, *width, *height);
450 }
451 
452 
453 /*
454  * Obtain level of support for base format across all producers and consumers as
455  * defined by IP support table. This support is defined for the most capable IP -
456  * specific IP might have reduced support based on specific capabilities.
457  *
458  * @param producers      [in]    Producers (flags).
459  * @param consumers      [in]    Consumers (flags).
460  * @param format         [in]    Format entry in IP support table.
461  *
462  * @return format support flags.
463  */
ip_supports_base_format(const uint16_t producers,const uint16_t consumers,const format_ip_support_t * const format)464 static format_support_flags ip_supports_base_format(const uint16_t producers,
465                                                     const uint16_t consumers,
466                                                     const format_ip_support_t * const format)
467 {
468 	format_support_flags support = ~0;
469 
470 	/* Determine producer support for base format. */
471 	if (producers_use_cpu_caps(producers))
472 	{
473 		support &= format->cpu_wr;
474 	}
475 	if (producers & MALI_GRALLOC_PRODUCER_GPU)
476 	{
477 		support &= format->gpu_wr;
478 	}
479 	if (producers & MALI_GRALLOC_PRODUCER_DPU)
480 	{
481 		support &= format->dpu_wr;
482 	}
483 	if (producers & MALI_GRALLOC_PRODUCER_CAM)
484 	{
485 		support &= format->cam_wr;
486 	}
487 	if (producers & GOOGLE_GRALLOC_PRODUCER_VPUS_MASK)
488 	{
489 		support &= format->vpu_wr;
490 	}
491 
492 	/* Determine consumer support for base format. */
493 	if (consumers_use_cpu_caps(consumers))
494 	{
495 		support &= format->cpu_rd;
496 	}
497 	if (consumers & MALI_GRALLOC_CONSUMER_GPU)
498 	{
499 		support &= format->gpu_rd;
500 	}
501 	if (consumers & MALI_GRALLOC_CONSUMER_DPU)
502 	{
503 		support &= format->dpu_rd;
504 	}
505 	if (consumers & GOOGLE_GRALLOC_CONSUMER_VPUS_MASK)
506 	{
507 		support &= format->vpu_rd;
508 	}
509 
510 	return support;
511 }
512 
513 
514 /*
515  * Determines whether a base format is subsampled YUV, where each
516  * chroma channel has fewer samples than the luma channel. The
517  * sub-sampling is always a power of 2.
518  *
519  * @param base_format   [in]    Base format (internal).
520  *
521  * @return 1, where format is subsampled YUV;
522  *         0, otherwise
523  */
is_subsampled_yuv(const uint32_t base_format)524 bool is_subsampled_yuv(const uint32_t base_format)
525 {
526 	unsigned long i;
527 
528 	for (i = 0; i < num_formats; i++)
529 	{
530 		if (formats[i].id == (base_format & MALI_GRALLOC_INTFMT_FMT_MASK))
531 		{
532 			if (formats[i].is_yuv == true &&
533 			    (formats[i].hsub > 1 || formats[i].vsub > 1))
534 			{
535 				return true;
536 			}
537 		}
538 	}
539 	return false;
540 }
541 
542 
543 /*
544  * Determines whether multi-plane AFBC (requires specific IP capabiltiies) is
545  * supported across all producers and consumers.
546  *
547  * @param producers      [in]    Producers (flags).
548  * @param consumers      [in]    Consumers (flags).
549  * @param producer_caps  [in]    Producer capabilities (flags).
550  * @param consumer_caps  [in]    Consumer capabilities (flags).
551  *
552  * @return 1, multiplane AFBC is supported
553  *         0, otherwise
554  */
is_afbc_multiplane_supported(const uint16_t producers,const uint16_t consumers,const uint64_t producer_caps,const uint64_t consumer_caps)555 static inline bool is_afbc_multiplane_supported(const uint16_t producers,
556                                                 const uint16_t consumers,
557                                                 const uint64_t producer_caps,
558                                                 const uint64_t consumer_caps)
559 {
560 	GRALLOC_UNUSED(consumers);
561 
562 	return (producer_caps & consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC &&
563 	        producer_caps & consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_TILED_HEADERS &&
564 	        producer_caps & consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_EXTRAWIDEBLK &&
565 	        /*no_producer*/ consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_MULTIPLANE_READ &&
566 	        producers == 0) ? true : false;
567 }
568 
569 
570 /*
571  * Determines whether a given base format is supported by all producers and
572  * consumers. After checking broad support across producer/consumer IP, this
573  * function uses capabilities to disable features (base formats and AFBC
574  * modifiers) that are not supported by specific versions of each IP.
575  *
576  * @param fmt_idx        [in]    Index into format properties table (base format).
577  * @param ip_fmt_idx     [in]    Index into format IP support table (base format).
578  * @param usage          [in]    Buffer usage.
579  * @param producers      [in]    Producers (flags).
580  * @param consumers      [in]    Consumers (flags).
581  * @param producer_caps  [in]    Producer capabilities (flags).
582  * @param consumer_caps  [in]    Consumer capabilities (flags).
583  *
584  * @return format support flags.
585  */
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)586 static format_support_flags is_format_supported(const int32_t fmt_idx,
587                                                 const int32_t ip_fmt_idx,
588                                                 const uint64_t usage,
589                                                 const uint16_t producers,
590                                                 const uint16_t consumers,
591                                                 const uint64_t producer_caps,
592                                                 const uint64_t consumer_caps)
593 {
594 	/* Determine format support from table. */
595 	format_support_flags f_flags = ip_supports_base_format(producers, consumers,
596 	                                                       &formats_ip_support[ip_fmt_idx]);
597 
598 	/* Determine whether producers/consumers support required AFBC features. */
599 	if (f_flags & F_AFBC)
600 	{
601 		if (!formats[fmt_idx].afbc ||
602 		    (producer_caps & consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC) == 0)
603 		{
604 			f_flags &= ~F_AFBC;
605 		}
606 
607 		/* Check that multi-plane format supported by producers/consumers. */
608 		if (formats[fmt_idx].npln > 1 &&
609 		    !is_afbc_multiplane_supported(producers, consumers, producer_caps, consumer_caps))
610 		{
611 			f_flags &= ~F_AFBC;
612 		}
613 
614 		/* Apply some additional restrictions from producer_caps and consumer_caps */
615 		/* Some modifiers affect base format support */
616 		if (formats[fmt_idx].is_yuv)
617 		{
618 			if ((producer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_YUV_WRITE) == 0)
619 			{
620 				f_flags &= ~F_AFBC;
621 			}
622 
623 			if ((consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_YUV_READ) == 0)
624 			{
625 				f_flags &= ~F_AFBC;
626 			}
627 		}
628 
629 		if (usage & GRALLOC_USAGE_FRONT_BUFFER)
630 		{
631 			if ((producer_caps & consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_DOUBLE_BODY) == 0)
632 			{
633 				f_flags &= ~F_AFBC;
634 			}
635 		}
636 	}
637 	if (f_flags != F_NONE)
638 	{
639 		if (formats[fmt_idx].id == MALI_GRALLOC_FORMAT_INTERNAL_RGBA_1010102 &&
640 		    (producer_caps & consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_PIXFMT_RGBA1010102) == 0)
641 		{
642 			/* TODO: forcing F_LIN is a W/A. Make a proper solution */
643 			f_flags = F_NONE;
644 			f_flags = F_LIN;
645 		}
646 		else if (formats[fmt_idx].id == MALI_GRALLOC_FORMAT_INTERNAL_RGBA_16161616)
647 		{
648 			if ((producer_caps & consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_PIXFMT_RGBA16161616) == 0)
649 			{
650 				/* TODO: forcing F_LIN is a W/A. Make a proper solution */
651 				//f_flags = F_NONE;
652 				f_flags = F_LIN;
653 			}
654 			else if ((producer_caps & consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_RGBA16161616) == 0)
655 			{
656 				f_flags = F_LIN;
657 			}
658 		}
659 	}
660 
661 	return f_flags;
662 }
663 
664 
665 /*
666  * Ensures that the allocation format conforms to the AFBC specification and is
667  * supported by producers and consumers. Format modifiers are (in most cases)
668  * disabled as required to make valid. It is important to first resolve invalid
669  * combinations which are not dependent upon others to reduce the possibility of
670  * circular dependency.
671  *
672  * @param alloc_format          [in]    Allocation format (base + modifiers).
673  * @param producer_active_caps  [in]    Producer capabilities (flags).
674  * @param consumer_active_caps  [in]    Consumer capabilities (flags).
675  *
676  * @return valid alloc_format with AFBC possibly disabled (if required)
677  */
validate_afbc_format(uint64_t alloc_format,const uint64_t producer_active_caps,const uint64_t consumer_active_caps)678 static uint64_t validate_afbc_format(uint64_t alloc_format,
679                                      const uint64_t producer_active_caps,
680                                      const uint64_t consumer_active_caps)
681 {
682 	const uint32_t base_format = alloc_format & MALI_GRALLOC_INTFMT_FMT_MASK;
683 
684 	/*
685 	 * AFBC with tiled-headers must be enabled for AFBC front-buffer-safe allocations.
686 	 * NOTE: format selection algorithm will always try and enable AFBC with
687 	 * tiled-headers where supported by producer(s) and consumer(s).
688 	 */
689 	if (alloc_format & MALI_GRALLOC_INTFMT_AFBC_DOUBLE_BODY)
690 	{
691 		/*
692 		 * Disable (extra-) wide-block which is unsupported with front-buffer safe AFBC.
693 		 */
694 		alloc_format &= ~MALI_GRALLOC_INTFMT_AFBC_WIDEBLK;
695 		alloc_format &= ~MALI_GRALLOC_INTFMT_AFBC_EXTRAWIDEBLK;
696 	}
697 
698 	/*
699 	 * AFBC specification: Split-block is not supported for
700 	 * subsampled formats (YUV) when wide-block is enabled.
701 	 */
702 	if (alloc_format & MALI_GRALLOC_INTFMT_AFBC_WIDEBLK &&
703 	    alloc_format & MALI_GRALLOC_INTFMT_AFBC_SPLITBLK &&
704 	    is_subsampled_yuv(base_format))
705 	{
706 		/* Disable split-block instead of wide-block because because
707 		 * wide-block has greater impact on display performance.
708 		 */
709 		alloc_format &= ~MALI_GRALLOC_INTFMT_AFBC_SPLITBLK;
710 	}
711 
712 	/* AFBC specification: Split-block must be enabled for
713 	 * non-subsampled formats > 16 bpp, where wide-block is enabled.
714 	 */
715 	if (alloc_format & MALI_GRALLOC_INTFMT_AFBC_WIDEBLK &&
716 	   (alloc_format & MALI_GRALLOC_INTFMT_AFBC_SPLITBLK) == 0 &&
717 	   !is_subsampled_yuv(base_format) &&
718 	   base_format != MALI_GRALLOC_FORMAT_INTERNAL_RGB_565)
719 	{
720 		/* Enable split-block if supported by producer(s) & consumer(s),
721 		 * otherwise disable wide-block.
722 		 */
723 		if (producer_active_caps & consumer_active_caps &
724 			MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK)
725 		{
726 			alloc_format |= MALI_GRALLOC_INTFMT_AFBC_SPLITBLK;
727 		}
728 		else
729 		{
730 			alloc_format &= ~MALI_GRALLOC_INTFMT_AFBC_WIDEBLK;
731 		}
732 	}
733 
734 	/* Some RGB formats don't support split block. */
735 	if (base_format == MALI_GRALLOC_FORMAT_INTERNAL_RGB_565)
736 	{
737 		alloc_format &= ~MALI_GRALLOC_INTFMT_AFBC_SPLITBLK;
738 	}
739 
740 	/* Ensure that AFBC features are supported by producers/consumers. */
741 	if ((alloc_format & MALI_GRALLOC_INTFMT_AFBC_BASIC) &&
742 	    (producer_active_caps & consumer_active_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC) == 0)
743 	{
744 		MALI_GRALLOC_LOGE("AFBC basic selected but not supported by producer/consumer. Disabling "
745 		       "MALI_GRALLOC_INTFMT_AFBC_BASIC");
746 		alloc_format &= ~MALI_GRALLOC_INTFMT_AFBC_BASIC;
747 	}
748 
749 	if ((alloc_format & MALI_GRALLOC_INTFMT_AFBC_SPLITBLK) &&
750 	    (producer_active_caps & consumer_active_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK) == 0)
751 	{
752 		MALI_GRALLOC_LOGE("AFBC split-block selected but not supported by producer/consumer. Disabling "
753 		       "MALI_GRALLOC_INTFMT_AFBC_SPLITBLK");
754 		alloc_format &= ~MALI_GRALLOC_INTFMT_AFBC_SPLITBLK;
755 	}
756 
757 	if ((alloc_format & MALI_GRALLOC_INTFMT_AFBC_WIDEBLK) &&
758 	    (producer_active_caps & consumer_active_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_WIDEBLK) == 0)
759 	{
760 		MALI_GRALLOC_LOGE("AFBC wide-block selected but not supported by producer/consumer. Disabling "
761 		       "MALI_GRALLOC_INTFMT_AFBC_WIDEBLK");
762 		alloc_format &= ~MALI_GRALLOC_INTFMT_AFBC_WIDEBLK;
763 	}
764 
765 	if ((alloc_format & MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS) &&
766 	    (producer_active_caps & consumer_active_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_TILED_HEADERS) == 0)
767 	{
768 		MALI_GRALLOC_LOGE("AFBC tiled-headers selected but not supported by producer/consumer. Disabling "
769 		       "MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS");
770 		alloc_format &= ~MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS;
771 	}
772 
773 	if (!((alloc_format & MALI_GRALLOC_INTFMT_AFBC_SPARSE) ||
774 	      (producer_active_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_WRITE_NON_SPARSE)))
775 	{
776 		MALI_GRALLOC_LOGE("AFBC sparse not selected while producer cannot write non-sparse. Enabling "
777 		      "MALI_GRALLOC_INTFMT_AFBC_SPARSE");
778 		alloc_format |= MALI_GRALLOC_INTFMT_AFBC_SPARSE;
779 	}
780 
781 	return alloc_format;
782 }
783 
784 
785 /*
786  * Derives a valid AFBC format (via modifiers) for all producers and consumers.
787  * Formats are validated after enabling the largest feature set supported (and
788  * desirable) for the IP usage. Some format modifier combinations are not
789  * compatible. See MALI_GRALLOC_INTFMT_* modifiers for more information.
790  *
791  * @param base_format    [in]    Base format (internal).
792  * @param usage          [in]    Buffer usage.
793  * @param producer       [in]    Buffer producers (write).
794  * @param consumer       [in]    Buffer consumers (read).
795  * @param producer_caps  [in]    Buffer producer capabilities (intersection).
796  * @param consumer_caps  [in]    Buffer consumer capabilities (intersection).
797  *
798  * @return valid AFBC format, where modifiers are enabled (supported/preferred);
799  *         base format without modifers, otherwise
800  */
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)801 static uint64_t get_afbc_format(const uint32_t base_format,
802                                 const uint64_t usage,
803                                 const uint16_t producer,
804                                 const uint16_t consumer,
805                                 const uint64_t producer_caps,
806                                 const uint64_t consumer_caps)
807 {
808 	uint64_t alloc_format = base_format;
809 
810 	/*
811 	 * Determine AFBC modifiers where capabilities are defined for all producers
812 	 * and consumers. NOTE: AFBC is not supported for video transcode (VPU --> VPU).
813 	 */
814 	if (producer_caps & consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT &&
815 	    ((producer & GOOGLE_GRALLOC_PRODUCER_VPUS_MASK) == 0 || (consumer & GOOGLE_GRALLOC_CONSUMER_VPUS_MASK) == 0))
816 	{
817 		if (producer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC &&
818 		    consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC)
819 		{
820 			alloc_format |= MALI_GRALLOC_INTFMT_AFBC_BASIC;
821 
822 #if 0
823 			const int format_idx = get_format_index(base_format);
824 
825 			/* TODO: AFBC YUV TRANSFORM corrupts screen when enabled. find out why */
826 			if (format_idx != -1)
827 			{
828 				if (formats[format_idx].yuv_transform == true)
829 				{
830 					alloc_format |= MALI_GRALLOC_INTFMT_AFBC_YUV_TRANSFORM;
831 				}
832 			}
833 #endif
834 
835 			if (!(producer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_WRITE_NON_SPARSE))
836 			{
837 				alloc_format |= MALI_GRALLOC_INTFMT_AFBC_SPARSE;
838 			}
839 
840 			if (producer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_TILED_HEADERS &&
841 			    consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_TILED_HEADERS)
842 			{
843 				alloc_format |= MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS;
844 
845 				if (usage & GRALLOC_USAGE_FRONT_BUFFER &&
846 				    producer_caps & consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_DOUBLE_BODY)
847 				{
848 					alloc_format |= MALI_GRALLOC_INTFMT_AFBC_DOUBLE_BODY;
849 				}
850 			}
851 
852 			/*
853 			 * Specific producer/consumer combinations benefit from additional
854 			 * AFBC features (e.g. * --> GPU).
855 			 */
856 			if (consumer & MALI_GRALLOC_CONSUMER_GPU)
857 			{
858 				if (producer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK &&
859 				    consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK)
860 				{
861 					alloc_format |= MALI_GRALLOC_INTFMT_AFBC_SPLITBLK;
862 				}
863 
864 				/*
865 				 * NOTE: assume that all AFBC layers are pre-rotated. 16x16 SB
866 				 * must be used with DPU consumer when rotation is required.
867 				 */
868 				if (producer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_WIDEBLK &&
869 				    consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_WIDEBLK)
870 				{
871 					alloc_format |= MALI_GRALLOC_INTFMT_AFBC_WIDEBLK;
872 				}
873 			}
874 		}
875 	}
876 
877 	alloc_format = validate_afbc_format(alloc_format, producer_caps, consumer_caps);
878 
879 	return alloc_format;
880 }
881 
882 /*
883  * Obtains the 'active' capabilities (for producers/consumers) by applying
884  * additional constraints to the capabilities declared for each IP. Some rules
885  * are based on format, others specific to producer/consumer. This function must
886  * be careful not to make any assumptions about the base format properties since
887  * fallback might still occur. It is safe to use any properties which are common
888  * across all compatible formats as defined by is_format_compatible().
889  *
890  * @param format                 [in]    Base format requested.
891  * @param producers              [in]    Producers (flags).
892  * @param consumers              [in]    Consumers (flags).
893  * @param producer_active_caps   [out]   Active producer capabilities (flags).
894  * @param consumer_active_caps   [out]   Active consumer capabilities (flags).
895  *
896  * @return none.
897  */
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)898 static void get_active_caps(const format_info_t format,
899                             const uint16_t producers,
900                             const uint16_t consumers,
901                             uint64_t * const producer_active_caps,
902                             uint64_t * const consumer_active_caps)
903 {
904 	const uint64_t producer_caps = (producer_active_caps) ? *producer_active_caps : 0;
905 	const uint64_t consumer_caps = (consumer_active_caps) ? *consumer_active_caps : 0;
906 	uint64_t producer_mask = ~0;
907 	uint64_t consumer_mask = ~0;
908 
909 	if (format.is_yuv)
910 	{
911 		if ((producer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_YUV_WRITE) == 0)
912 		{
913 			producer_mask &= ~MALI_GRALLOC_FORMAT_CAPABILITY_AFBCENABLE_MASK;
914 		}
915 		else if (producers & MALI_GRALLOC_PRODUCER_GPU)
916 		{
917 			/* All GPUs that can write YUV AFBC can only do it in 16x16,
918 			 * optionally with tiled headers.
919 			 */
920 			producer_mask &=
921 				~(MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK |
922 				  MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_WIDEBLK);
923 		}
924 
925 		if ((consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_YUV_READ) == 0)
926 		{
927 			consumer_mask &= ~MALI_GRALLOC_FORMAT_CAPABILITY_AFBCENABLE_MASK;
928 		}
929 	}
930 
931 	// TODO: b/183385318 Prefer 16x16 AFBC over 32x8 for GPU --> GPU
932 	if ((producers & MALI_GRALLOC_PRODUCER_GPU) &&
933 	    (consumers & MALI_GRALLOC_CONSUMER_GPU) &&
934             (producer_caps & consumer_caps & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC))
935 	{
936 		producer_mask &=
937 			~(MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK |
938 			  MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_WIDEBLK);
939 
940 		consumer_mask &=
941 			~(MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK |
942 			  MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_WIDEBLK);
943 	}
944 
945 	if (consumers & MALI_GRALLOC_CONSUMER_DPU)
946 	{
947 		/* DPU does not support split-block other than RGB(A) 24/32-bit */
948 		if (!format.is_rgb || format.bpp[0] < 24)
949 		{
950 			if (consumers & MALI_GRALLOC_CONSUMER_DPU)
951 			{
952 				consumer_mask &= ~MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK;
953 			}
954 		}
955 	}
956 
957 	if (producer_active_caps)
958 	{
959 		*producer_active_caps &= producer_mask;
960 	}
961 	if (consumer_active_caps)
962 	{
963 		*consumer_active_caps &= consumer_mask;
964 	}
965 }
966 
967 
968 /*
969  * Obtains support flags and modifiers for base format.
970  *
971  * @param base_format           [in]    Base format for which to deduce support.
972  * @param usage                 [in]    Buffer usage.
973  * @param producers             [in]    Producers (flags).
974  * @param consumers             [in]    Consumers (flags).
975  * @param producer_active_caps  [in]    Producer capabilities (flags).
976  * @param consumer_active_caps  [in]    Consumer capabilities (flags).
977  * @param fmt_supported         [out]   Format (base + modifiers) and support (flags).
978  *
979  * @return 1, base format supported
980  *         0, otherwise
981  */
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)982 bool get_supported_format(const uint32_t base_format,
983                           const uint64_t usage,
984                           const uint16_t producers,
985                           const uint16_t consumers,
986                           const uint64_t producer_active_caps,
987                           const uint64_t consumer_active_caps,
988                           fmt_props * const fmt_supported)
989 {
990 	const int32_t fmt_idx = get_format_index(base_format);
991 	const int32_t ip_fmt_idx = get_ip_format_index(base_format);
992 	assert(fmt_idx >= 0);
993 	if (ip_fmt_idx == -1)
994 	{
995 		/* Return undefined base format. */
996 		MALI_GRALLOC_LOGE("Failed to find IP support info for format id: 0x%" PRIx32,
997 		      base_format);
998 		return false;
999 	}
1000 
1001 	fmt_supported->f_flags = is_format_supported(fmt_idx,
1002 	                                             ip_fmt_idx,
1003 	                                             usage,
1004 	                                             producers,
1005 	                                             consumers,
1006 	                                             producer_active_caps,
1007 	                                             consumer_active_caps);
1008 	MALI_GRALLOC_LOGV("IP support: 0x%" PRIx16, fmt_supported->f_flags);
1009 	if (fmt_supported->f_flags == F_NONE &&
1010 	    consumers & MALI_GRALLOC_CONSUMER_GPU &&
1011 	    consumers & MALI_GRALLOC_CONSUMER_DPU)
1012 	{
1013 		/* Determine alternative caps for formats when GPU/DPU consumer.
1014 		 * Although we normally combine capabilities for multiple consumers with "AND",
1015 		 * in some situations (e.g. formats) we make best effort and say that fallback
1016 		 * to GPU is acceptable and perferred over rejecting allocation. GPU composition
1017 		 * must always be supported in case of fallback from DPU.
1018 		 */
1019 		const uint16_t consumers_nodpu = consumers & ~MALI_GRALLOC_CONSUMER_DPU;
1020 		uint64_t consumer_nodpu_caps = consumer_active_caps;
1021 
1022 		/* Set consumer caps to GPU-only (assume superset of DPU). */
1023 		consumer_nodpu_caps = get_consumer_caps(consumers_nodpu);
1024 		get_active_caps(formats[fmt_idx],
1025 		                producers, consumers_nodpu,
1026 		                NULL, &consumer_nodpu_caps);
1027 
1028 		fmt_supported->f_flags = is_format_supported(fmt_idx,
1029 	                                                 ip_fmt_idx,
1030 		                                             usage,
1031 	                                                 producers,
1032 	                                                 consumers_nodpu,
1033 	                                                 producer_active_caps,
1034 	                                                 consumer_nodpu_caps);
1035 	}
1036 
1037 	fmt_supported->base_format = base_format;
1038 	if (fmt_supported->f_flags & F_AFBC)
1039 	{
1040 		const uint64_t afbc_format = get_afbc_format(base_format,
1041 		                                             usage,
1042 		                                             producers,
1043 		                                             consumers,
1044 		                                             producer_active_caps,
1045 		                                             consumer_active_caps);
1046 
1047 		MALI_GRALLOC_LOGV("AFBC format: (%s 0x%" PRIx64 ")",
1048 			format_name(afbc_format), afbc_format);
1049 
1050 		/* Disable AFBC when forced by usage or no format modifiers selected. */
1051 		if ((usage & MALI_GRALLOC_USAGE_NO_AFBC) == MALI_GRALLOC_USAGE_NO_AFBC ||
1052 		    afbc_format == fmt_supported->base_format)
1053 		{
1054 			fmt_supported->f_flags &= ~F_AFBC;
1055 		}
1056 
1057 		/* Check that AFBC features are correct for multiplane format. */
1058 		alloc_type_t alloc_type{};
1059 		get_alloc_type(afbc_format & MALI_GRALLOC_INTFMT_EXT_MASK,
1060 					   fmt_idx,
1061 					   usage,
1062 					   &alloc_type);
1063 		if (formats[fmt_idx].npln > 1 && alloc_type.is_multi_plane == false)
1064 		{
1065 			fmt_supported->f_flags &= ~F_AFBC;
1066 		}
1067 
1068 		/* Store any format modifiers */
1069 		fmt_supported->format_ext = afbc_format & MALI_GRALLOC_INTFMT_EXT_MASK;
1070 	}
1071 	if ((fmt_supported->f_flags & F_AFBC) == 0)
1072 	{
1073 		fmt_supported->format_ext = 0;
1074 	}
1075 
1076 	MALI_GRALLOC_LOGV("Ext format: (%s 0x%" PRIx64 ")", format_name(fmt_supported->format_ext),
1077 		fmt_supported->format_ext);
1078 
1079 	return (fmt_supported->f_flags == F_NONE) ? false : true;
1080 }
1081 
1082 
1083 /*
1084  * Determines whether two base formats have comparable 'color' components. Alpha
1085  * is considered unimportant for YUV formats.
1086  *
1087  * @param f_old     [in]    Format properties (old format).
1088  * @param f_new     [in]    Format properties (new format).
1089  *
1090  * @return 1, format components are equivalent
1091  *         0, otherwise
1092  */
comparable_components(const format_info_t * const f_old,const format_info_t * const f_new)1093 static bool comparable_components(const format_info_t * const f_old,
1094                                   const format_info_t * const f_new)
1095 {
1096 	if (f_old->is_yuv && f_new->bps == f_old->bps)
1097 	{
1098 		/* Formats have the same number of components. */
1099 		if (f_new->total_components() == f_old->total_components())
1100 		{
1101 			return true;
1102 		}
1103 
1104 		/* Alpha component can be dropped for yuv formats.
1105 		 * This assumption is required for mapping Y0L2 to
1106 		 * single plane 10-bit YUV420 AFBC.
1107 		 */
1108 		if (f_old->has_alpha)
1109 		{
1110 			if (f_new->total_components() == 3 &&
1111 			    f_new->is_yuv &&
1112 			    !f_new->has_alpha)
1113 			{
1114 				return true;
1115 			}
1116 		}
1117 	}
1118 	else if (f_old->is_rgb)
1119 	{
1120 		if (f_new->total_components() == f_old->total_components())
1121 		{
1122 			if (f_new->bpp[0] == f_old->bpp[0] && f_new->bps == f_old->bps)
1123 			{
1124 				return true;
1125 			}
1126 		}
1127 	}
1128 	else
1129 	{
1130 		if (f_new->id == f_old->id)
1131 		{
1132 			return true;
1133 		}
1134 	}
1135 
1136 	return false;
1137 }
1138 
1139 
1140 /*
1141  * Determines whether two base formats are compatible such that data from one
1142  * format could be accurately represented/interpreted in the other format.
1143  *
1144  * @param f_old     [in]    Format properties (old format).
1145  * @param f_new     [in]    Format properties (new format).
1146  *
1147  * @return 1, formats are equivalent
1148  *         0, otherwise
1149  */
is_format_compatible(const format_info_t * const f_old,const format_info_t * const f_new)1150 static bool is_format_compatible(const format_info_t * const f_old,
1151                                  const format_info_t * const f_new)
1152 {
1153 	if (f_new->hsub == f_old->hsub &&
1154 	    f_new->vsub == f_old->vsub &&
1155 	    f_new->is_rgb == f_old->is_rgb &&
1156 	    f_new->is_yuv == f_old->is_yuv &&
1157 	    comparable_components(f_old, f_new))
1158 	{
1159 		return true;
1160 	}
1161 	else
1162 	{
1163 		return false;
1164 	}
1165 }
1166 
1167 /**
1168  * Provide a grade for the compatible format with respect to the requested format. Used to find the best compatible
1169  * format.
1170  *
1171  * @param fmt[in]    Compatible format properties.
1172  * @param req_format Requested base format.
1173  *
1174  * @return The grade of the compatible format. Higher is better. Returns 0 if format extensions are incompatible with
1175  * requested format.
1176  */
grade_format(const fmt_props & fmt,uint32_t req_format)1177 uint64_t grade_format(const fmt_props &fmt, uint32_t req_format)
1178 {
1179 	uint64_t grade = 1;
1180 
1181 	GRALLOC_UNUSED(req_format);
1182 
1183 	static const struct {
1184 		uint64_t fmt_ext;
1185 		uint64_t value;
1186 	} fmt_ext_values[] {
1187 		{ MALI_GRALLOC_INTFMT_AFBC_BASIC, 1 },
1188 		{ MALI_GRALLOC_INTFMT_AFBC_SPLITBLK, 1 },
1189 		{ MALI_GRALLOC_INTFMT_AFBC_WIDEBLK, 1 },
1190 		{ MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS, 1 },
1191 		{ MALI_GRALLOC_INTFMT_AFBC_EXTRAWIDEBLK, 1 },
1192 		{ MALI_GRALLOC_INTFMT_AFBC_DOUBLE_BODY, 1 },
1193 		{ MALI_GRALLOC_INTFMT_AFBC_BCH, 1 },
1194 		{ MALI_GRALLOC_INTFMT_AFBC_YUV_TRANSFORM, 1 },
1195 		{ MALI_GRALLOC_INTFMT_AFBC_SPARSE, 1 },
1196 	};
1197 	for (auto& ext : fmt_ext_values)
1198 	{
1199 		if (fmt.format_ext & ext.fmt_ext)
1200 		{
1201 			grade += ext.value;
1202 		}
1203 	}
1204 
1205 	return grade;
1206 }
1207 
1208 /*
1209  * Obtains the 'best' allocation format for requested format and usage:
1210  * 1. Find compatible base formats (based on format properties alone)
1211  * 2. Find base formats supported by producers/consumers
1212  * 3. Find best modifiers from supported base formats
1213  * 4. Select allocation format from "best" base format with "best" modifiers
1214  *
1215  * NOTE: Base format re-mapping should not take place when CPU usage is
1216  * requested.
1217  *
1218  * @param req_base_format       [in]    Base format requested by client.
1219  * @param usage                 [in]    Buffer usage.
1220  * @param producers             [in]    Producers (flags).
1221  * @param consumers             [in]    Consumers (flags).
1222  * @param producer_active_caps  [in]    Producer capabilities (flags).
1223  * @param consumer_active_caps  [in]    Consumer capabilities (flags).
1224  *
1225  * @return alloc_format, supported for usage;
1226  *         MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED, otherwise
1227  */
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)1228 static uint64_t get_best_format(const uint32_t req_base_format,
1229                                 const uint64_t usage,
1230                                 const uint16_t producers,
1231                                 const uint16_t consumers,
1232                                 const uint64_t producer_active_caps,
1233                                 const uint64_t consumer_active_caps)
1234 {
1235 	uint64_t alloc_format = MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED;
1236 
1237 	assert(req_base_format != MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED);
1238 	const int32_t req_fmt_idx = get_format_index(req_base_format);
1239 	MALI_GRALLOC_LOGV("req_base_format: (%s 0x%" PRIx32 ")",
1240 		format_name(req_base_format), req_base_format);
1241 	MALI_GRALLOC_LOGV("req_fmt_idx: %d", req_fmt_idx);
1242 	assert(req_fmt_idx >= 0);
1243 
1244 	/* 1. Find compatible base formats. */
1245 	std::vector<fmt_props> f_compat;
1246 	for (uint16_t i = 0; i < num_formats; i++)
1247 	{
1248 		if (is_format_compatible(&formats[req_fmt_idx], &formats[i]))
1249 		{
1250 			fmt_props fmt = {0, 0, 0};
1251 			fmt.base_format = formats[i].id;
1252 			MALI_GRALLOC_LOGV("Compatible: Base-format: (%s 0x%" PRIx32 ")",
1253 				format_name(fmt.base_format), fmt.base_format);
1254 			f_compat.push_back(fmt);
1255 		}
1256 	}
1257 	assert(f_compat.size() > 0);
1258 
1259 	/* 2. Find base formats supported by IP and among them, find the highest
1260 	 * number of modifier enabled format and check if requested format is present
1261 	 */
1262 
1263 	int32_t num_supported_formats = 0;
1264 	uint64_t req_format_grade = 0;
1265 	uint64_t best_fmt_grade = 0;
1266 	uint64_t first_of_best_formats = MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED;
1267 	uint64_t req_format = MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED;
1268 
1269 	for (uint16_t i = 0; i < f_compat.size(); i++)
1270 	{
1271 		MALI_GRALLOC_LOGV("Compatible: Base-format: 0x%" PRIx32, f_compat[i].base_format);
1272 		fmt_props fmt = {0, 0, 0};
1273 		bool supported = get_supported_format(f_compat[i].base_format,
1274 		                                      usage,
1275 		                                      producers,
1276 		                                      consumers,
1277 		                                      producer_active_caps,
1278 		                                      consumer_active_caps,
1279 		                                      &fmt);
1280 		if (supported)
1281 		{
1282 			const uint64_t sup_fmt_grade = grade_format(fmt, req_base_format);
1283 			if (sup_fmt_grade)
1284 			{
1285 				num_supported_formats++;
1286 				MALI_GRALLOC_LOGV("Supported: Base-format: (%s 0x%" PRIx32 "), Modifiers: 0x%" PRIx64 ", Flags: 0x%" PRIx16,
1287 			      format_name(fmt.base_format), fmt.base_format, fmt.format_ext, fmt.f_flags);
1288 
1289 				/* 3. Find best modifiers from supported base formats */
1290 				if (sup_fmt_grade > best_fmt_grade)
1291 				{
1292 					best_fmt_grade = sup_fmt_grade;
1293 					first_of_best_formats = fmt.base_format | fmt.format_ext;
1294 				}
1295 
1296 				/* Check if current supported format is same as requested format */
1297 				if (fmt.base_format == req_base_format)
1298 				{
1299 					req_format_grade = sup_fmt_grade;
1300 					req_format = fmt.base_format | fmt.format_ext;
1301 				}
1302 			}
1303 		}
1304 	}
1305 
1306 	/* 4. Select allocation format from "best" base format with "best" modifiers */
1307 	if (num_supported_formats > 0)
1308 	{
1309 		/* Select first/one of best format when requested format is either not
1310 		* supported or requested format is not the best format.
1311 		*/
1312 		if ((req_format_grade != best_fmt_grade) &&
1313 			(((producers & MALI_GRALLOC_PRODUCER_CPU) == 0) &&
1314 			((consumers & MALI_GRALLOC_CONSUMER_CPU) == 0)))
1315 		{
1316 			alloc_format = first_of_best_formats;
1317 		}
1318 		else if (req_format_grade != 0)
1319 		{
1320 			alloc_format = req_format;
1321 		}
1322 	}
1323 
1324 	MALI_GRALLOC_LOGV("Selected format: (%s 0x%" PRIx64 ")",
1325 		format_name(alloc_format), alloc_format);
1326 	return alloc_format;
1327 }
1328 
1329 
1330 /*
1331  * Obtain format modifiers from requested format.
1332  *
1333  * @param req_format       [in]    Requested format (base + optional modifiers).
1334  * @param usage            [in]    Buffer usage.
1335  *
1336  * @return format modifiers, where extracted from requested format;
1337  *         0, otherwise
1338  */
get_format_ext(const uint64_t req_format,const uint64_t usage)1339 uint64_t get_format_ext(const uint64_t req_format, const uint64_t usage)
1340 {
1341 	/* TODO: clean up this function. Or remove it */
1342 	GRALLOC_UNUSED(usage);
1343 	return req_format & MALI_GRALLOC_INTFMT_EXT_MASK;
1344 }
1345 
1346 
1347 /*
1348  * Obtain base format from requested format. There are two primary ways in which
1349  * the client can specify requested format:
1350  * - Public API:
1351  *   - Normal usage, with HAL_PIXEL_FORMAT_* / MALI_GRALLOC_FORMAT_INTERNAL_*
1352  *   - Private usage, (as normal usage) with additional format modifiers (MALI_GRALLOC_INTFMT_*)
1353  * - Private API: allows private usage to be provided explicitly
1354  *                (type == MALI_GRALLOC_FORMAT_TYPE_INTERNAL)
1355  *
1356  * @param req_format       [in]    Requested format (base + optional modifiers).
1357  * @param usage            [in]    Buffer usage.
1358  * @param type             [in]    Format type (public usage or internal).
1359  * @param map_to_internal  [in]    Base format (if public HAL_PIXEL_FORMAT_*)
1360  *                                 should be mapped to internal representation.
1361  *
1362  * @return base format, where identified/translated from requested format;
1363  *         MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED, otherwise
1364  */
get_base_format(const uint64_t req_format,const uint64_t usage,const mali_gralloc_format_type type,const bool map_to_internal)1365 uint32_t get_base_format(const uint64_t req_format,
1366                          const uint64_t usage,
1367                          const mali_gralloc_format_type type,
1368                          const bool map_to_internal)
1369 {
1370 	GRALLOC_UNUSED(type);
1371 
1372 	uint32_t base_format = MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED;
1373 
1374 	/* Mask out extension bits which could be present with type 'internal'. */
1375 	base_format = req_format & MALI_GRALLOC_INTFMT_FMT_MASK;
1376 
1377 	/* Map Android flexible formats to internal base formats */
1378 	if (req_format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)
1379 	{
1380 		auto consumers = get_consumers(usage);
1381 		auto producers = get_producers(usage);
1382 
1383 		if ((usage & GRALLOC_USAGE_HW_TEXTURE) || (usage & GRALLOC_USAGE_HW_COMPOSER))
1384 		{
1385 			base_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN;
1386 		}
1387 		else if ((producers & MALI_GRALLOC_PRODUCER_CAM) &&
1388 			 !(producers & MALI_GRALLOC_PRODUCER_GPU) &&
1389 			 (consumers == GOOGLE_GRALLOC_CONSUMER_MFC))
1390 		{
1391 			// Allocated camera buffer is SBWC compressed when
1392 			// 1. Camera is one of the producers
1393 			// 2. GPU is not one of the producers
1394 			// 3. MFC is the sole consumer
1395 			if (property_get_bool("debug.vendor.gpu.record_sbwc", true)) {
1396 				base_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC;
1397 			} else {
1398 				base_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN;
1399 			}
1400 		}
1401 		else if (get_consumers(usage) & GOOGLE_GRALLOC_CONSUMER_BIG)
1402 		{
1403 			base_format = HAL_PIXEL_FORMAT_GOOGLE_NV12_SP;
1404 		}
1405 		else if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER)
1406 		{
1407 			base_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN;
1408 		}
1409 		else if (usage & GRALLOC_USAGE_VIDEO_PRIVATE_DATA)
1410 		{
1411 			base_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN;
1412 		}
1413 		else if ((usage & GRALLOC_USAGE_HW_CAMERA_READ) && (usage & GRALLOC_USAGE_HW_CAMERA_WRITE))
1414 		{
1415 			// Camera IMPLEMENTATION_DEFINED format output maps to NV21.
1416 			base_format = HAL_PIXEL_FORMAT_YCrCb_420_SP;
1417 		}
1418 		else
1419 		{
1420 			base_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN;
1421 		}
1422 	}
1423 	else if (req_format == HAL_PIXEL_FORMAT_YCbCr_420_888)
1424 	{
1425 		if (get_consumers(usage) & GOOGLE_GRALLOC_CONSUMER_BIG)
1426 		{
1427 			base_format = HAL_PIXEL_FORMAT_GOOGLE_NV12_SP;
1428 		}
1429 		else if (usage & (GRALLOC_USAGE_HW_VIDEO_ENCODER | GRALLOC_USAGE_HW_VIDEO_DECODER))
1430 		{
1431 			base_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN;
1432 		}
1433 		else if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
1434 		{
1435 			// Catchall for camera write. DO NOT CHANGE WITHOUT TESTING THESE SCENARIOS:
1436 			// 1. Camera capture and initial photo processing
1437 			// 2. Other major camera operations - video recording, portrait etc
1438 			// 3. Faceauth
1439 			// 4. Multi-profile user photo add
1440 			// 5. Capture and resize - use chat app to capture a photo
1441 			// Re-run these steps with GPU composition:
1442 			// adb shell service call SurfaceFlinger 1008 i32 1
1443 			base_format = HAL_PIXEL_FORMAT_YCrCb_420_SP;
1444 		}
1445 		else
1446 		{
1447 			base_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN;
1448 		}
1449 	}
1450 	else if (req_format == HAL_PIXEL_FORMAT_YCBCR_P010)
1451 	{
1452 		if (get_consumers(usage) & GOOGLE_GRALLOC_CONSUMER_BIG)
1453 		{
1454 			base_format = HAL_PIXEL_FORMAT_GOOGLE_NV12_SP_10B;
1455 		}
1456 		else if (usage & (GRALLOC_USAGE_HW_VIDEO_ENCODER | GRALLOC_USAGE_HW_VIDEO_DECODER))
1457 		{
1458 			base_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_SPN;
1459 		}
1460 	}
1461 
1462 	/* Obtain a valid base format, optionally mapped to internal. Flex formats
1463 	 * are always mapped to internal base format.
1464 	 * NOTE: Overlap between HAL_PIXEL_FORMAT_* and MALI_GRALLOC_FORMAT_INTERNAL_*
1465 	 * is intentional. See enumerations for more information.
1466 	 */
1467 	return get_internal_format(base_format, map_to_internal);
1468 }
1469 
1470 
1471 /*
1472  * Select pixel format (base + modifier) for allocation.
1473  *
1474  * @param req_format       [in]   Format (base + optional modifiers) requested by client.
1475  * @param type             [in]   Format type (public usage or internal).
1476  * @param usage            [in]   Buffer usage.
1477  *
1478  * @return alloc_format, format to be used in allocation;
1479  *         MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED, where no suitable
1480  *         format could be found.
1481  */
mali_gralloc_select_format(const uint64_t req_format,const mali_gralloc_format_type type,const uint64_t usage)1482 uint64_t mali_gralloc_select_format(const uint64_t req_format,
1483                                     const mali_gralloc_format_type type,
1484                                     const uint64_t usage)
1485 {
1486 	uint64_t alloc_format = MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED;
1487 
1488 	/*
1489 	 * Obtain base_format (no extension bits) and indexes into format tables.
1490 	 */
1491 	const uint32_t req_base_format = get_base_format(req_format, usage, type, true);
1492 	const int32_t req_fmt_idx = get_format_index(req_base_format);
1493 	if (req_base_format == MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED ||
1494 	    req_fmt_idx == -1)
1495 	{
1496 		MALI_GRALLOC_LOGE("Invalid base format! req_base_format = (%s 0x%" PRIx32
1497 			"), req_format = (%s 0x%" PRIx64 "), type = 0x%" PRIx32,
1498 			format_name(req_base_format), req_base_format, format_name(req_format), req_format, type);
1499 		goto out;
1500 	}
1501 
1502 	/* Reject if usage specified is outside white list of valid usages. */
1503 	if (type != MALI_GRALLOC_FORMAT_TYPE_INTERNAL && (usage & (~VALID_USAGE)) != 0)
1504 	{
1505 		MALI_GRALLOC_LOGW("Invalid usage specified: %s 0x%" PRIx64, describe_usage(usage).c_str(), usage);
1506 	}
1507 
1508 	/* TODO: Make a function for finding formats that should be allocated as the request format */
1509 	if (is_exynos_format(req_base_format) || req_base_format == HAL_PIXEL_FORMAT_BLOB)
1510 	{
1511 		alloc_format = req_base_format;
1512 	}
1513 	else if (usage == 0)
1514 	{
1515 		/* Allocate format as-is when no usage is specified */
1516 		alloc_format = req_base_format;
1517 	}
1518 	else
1519 	{
1520 		/* Determine producers and consumers. */
1521 		const uint16_t producers = get_producers(usage);
1522 		const uint16_t consumers = get_consumers(usage);
1523 
1524 		MALI_GRALLOC_LOGV("Producers: 0x%" PRIx16 ", Consumers: 0x%" PRIx16,
1525 		      producers, consumers);
1526 
1527 		/* Obtain producer and consumer capabilities. */
1528 		const uint64_t producer_caps = get_producer_caps(producers);
1529 
1530 		uint64_t consumer_caps = 0;
1531 #ifdef GRALLOC_HWC_FB_DISABLE_AFBC
1532 		if (GRALLOC_HWC_FB_DISABLE_AFBC && DISABLE_FRAMEBUFFER_HAL && (usage & GRALLOC_USAGE_HW_FB))
1533 		{
1534 			/* Override capabilities to disable AFBC for DRM HWC framebuffer surfaces. */
1535 			consumer_caps = MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT;
1536 		}
1537 		else
1538 #endif
1539 		{
1540 			consumer_caps = get_consumer_caps(consumers);
1541 		}
1542 
1543 		MALI_GRALLOC_LOGV("Producer caps: 0x%" PRIx64 ", Consumer caps: 0x%" PRIx64,
1544 		      producer_caps, consumer_caps);
1545 
1546 		if (producers == 0 && consumers == 0)
1547 		{
1548 			MALI_GRALLOC_LOGE("Producer and consumer not identified.");
1549 			goto out;
1550 		}
1551 		else if (producers == 0 || consumers == 0)
1552 		{
1553 			MALI_GRALLOC_LOGV("Producer or consumer not identified.");
1554 		}
1555 
1556 		if ((usage & MALI_GRALLOC_USAGE_NO_AFBC) == MALI_GRALLOC_USAGE_NO_AFBC &&
1557 		    formats[req_fmt_idx].is_yuv)
1558 		{
1559 			MALI_GRALLOC_LOGE("ERROR: Invalid usage 'MALI_GRALLOC_USAGE_NO_AFBC' when allocating YUV formats");
1560 			goto out;
1561 		}
1562 
1563 		uint64_t producer_active_caps = producer_caps;
1564 		uint64_t consumer_active_caps = consumer_caps;
1565 
1566 		get_active_caps(formats[req_fmt_idx],
1567 		                producers, consumers,
1568 		                &producer_active_caps, &consumer_active_caps);
1569 
1570 		MALI_GRALLOC_LOGV("Producer caps (active): 0x%" PRIx64 ", Consumer caps (active): 0x%" PRIx64,
1571 		      producer_active_caps, consumer_active_caps);
1572 
1573 		/* TODO: reimplment get_best_format */
1574 		alloc_format = get_best_format(formats[req_fmt_idx].id,
1575 		                               usage,
1576 		                               producers,
1577 		                               consumers,
1578 		                               producer_active_caps,
1579 		                               consumer_active_caps);
1580 	}
1581 
1582 out:
1583 	MALI_GRALLOC_LOGV("%s: req_format=(%s 0x%08" PRIx64 "), usage=(%s 0x%" PRIx64
1584 	      "), req_base_format=(%s 0x%" PRIx32 "), alloc_format=(%s, 0x%" PRIx64 ")", __func__,
1585 	      format_name(req_format), req_format, describe_usage(usage).c_str(), usage,
1586 		  format_name(req_base_format), req_base_format, format_name(alloc_format), alloc_format);
1587 
1588 	return alloc_format;
1589 }
1590 
is_exynos_format(uint32_t base_format)1591 bool is_exynos_format(uint32_t base_format)
1592 {
1593 	switch (base_format)
1594 	{
1595 		case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1596 		case HAL_PIXEL_FORMAT_EXYNOS_YV12_M:
1597 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P:
1598 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P_M:
1599 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_TILED:
1600 		case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M:
1601 		case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_FULL:
1602 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M:
1603 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN:
1604 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B:
1605 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_S10B:
1606 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_SPN:
1607 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_M:
1608 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC:
1609 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC:
1610 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC:
1611 		case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_SBWC:
1612 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC:
1613 		case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_10B_SBWC:
1614 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L50:
1615 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L75:
1616 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L50:
1617 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L75:
1618 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L40:
1619 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L60:
1620 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L80:
1621 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L40:
1622 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L60:
1623 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L80:
1624 			return true;
1625 	}
1626 
1627 	return false;
1628 }
1629 
get_exynos_fd_count(uint32_t format)1630 uint8_t get_exynos_fd_count(uint32_t format) {
1631 	switch (format)
1632 	{
1633 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC:
1634 		case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_SBWC:
1635 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC:
1636 		case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_10B_SBWC:
1637 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L50:
1638 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L75:
1639 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L40:
1640 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L60:
1641 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L80:
1642 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_TILED:
1643 		case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M:
1644 		case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_FULL:
1645 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M:
1646 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B:
1647 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_M:
1648 			return 2;
1649 		case HAL_PIXEL_FORMAT_EXYNOS_YV12_M:
1650 		case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P_M:
1651 			return 3;
1652 	}
1653 
1654 	return 1;
1655 }
1656 
1657