• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 <hardware/gralloc.h>
22 #include <inttypes.h>
23 #include <cutils/log.h>
24 
25 #include "mali_gralloc_formats.h"
26 #include "gralloc_priv.h"
27 
28 static mali_gralloc_format_caps dpu_runtime_caps;
29 static mali_gralloc_format_caps vpu_runtime_caps;
30 static mali_gralloc_format_caps gpu_runtime_caps;
31 static mali_gralloc_format_caps cam_runtime_caps;
32 static pthread_mutex_t caps_init_mutex = PTHREAD_MUTEX_INITIALIZER;
33 static bool runtime_caps_read = false;
34 
35 #define MALI_GRALLOC_GPU_LIB_NAME "libGLES_mali.so"
36 #if defined(__LP64__)
37 #define MALI_GRALLOC_GPU_LIBRARY_PATH1 "/vendor/lib64/egl/"
38 #define MALI_GRALLOC_GPU_LIBRARY_PATH2 "/system/lib64/egl/"
39 #else
40 #define MALI_GRALLOC_GPU_LIBRARY_PATH1 "/vendor/lib/egl/"
41 #define MALI_GRALLOC_GPU_LIBRARY_PATH2 "/system/lib/egl/"
42 #endif
43 
get_block_capabilities(bool hal_module,const char * name,mali_gralloc_format_caps * block_caps)44 static bool get_block_capabilities(bool hal_module, const char *name, mali_gralloc_format_caps *block_caps)
45 {
46     void *dso_handle = NULL;
47     bool rval = false;
48 
49     /* Look for MALI_GRALLOC_FORMATCAPS_SYM_NAME_STR symbol in user-space drivers
50      * to determine hw format capabilities.
51      */
52     if(!hal_module)
53     {
54         dso_handle = dlopen(name, RTLD_LAZY);
55     }
56     else
57     {
58         /* libhardware does some heuristics to find hal modules
59          * and then stores the dso handle internally. Use this.
60          */
61         const struct hw_module_t *module = {NULL};
62 
63         if(hw_get_module(name, &module) >= 0)
64         {
65             dso_handle = module->dso;
66         }
67     }
68 
69     if(dso_handle)
70     {
71         void *sym = dlsym(dso_handle, MALI_GRALLOC_FORMATCAPS_SYM_NAME_STR);
72 
73         if(sym)
74         {
75             memcpy((void*) block_caps, sym, sizeof(mali_gralloc_format_caps));
76             rval = true;
77         }
78 
79         if(!hal_module)
80         {
81             dlclose(dso_handle);
82         }
83     }
84 
85     return rval;
86 }
87 
map_flex_formats(int req_format,uint64_t * producer_runtime_mask)88 static int map_flex_formats(int req_format, uint64_t *producer_runtime_mask)
89 {
90     /* Map Android flexible formats to internal base formats */
91     if(req_format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED ||
92        req_format == HAL_PIXEL_FORMAT_YCbCr_420_888)
93     {
94         req_format = MALI_GRALLOC_FORMAT_INTERNAL_NV12;
95 
96         /*
97          * We disable AFBC for NV12 since neither VPU or DPU DDKs support
98          * them currently.
99          */
100         *producer_runtime_mask &= ~MALI_GRALLOC_FORMAT_CAPABILITY_AFBCENABLE_MASK;
101     }
102     else if(req_format == HAL_PIXEL_FORMAT_YCbCr_422_888)
103     {
104         /* To be determined */
105 
106         /* Disable AFBC until we know though */
107         *producer_runtime_mask &= ~MALI_GRALLOC_FORMAT_CAPABILITY_AFBCENABLE_MASK;
108     }
109     else if(req_format == HAL_PIXEL_FORMAT_YCbCr_444_888)
110     {
111         /* To be determined */
112 
113         /* Disable AFBC until we know though */
114         *producer_runtime_mask &= ~MALI_GRALLOC_FORMAT_CAPABILITY_AFBCENABLE_MASK;
115     }
116     return req_format;
117 }
118 
is_afbc_supported(int req_format_mapped)119 static bool is_afbc_supported(int req_format_mapped)
120 {
121     bool rval = true;
122 
123     /* These base formats we currently don't support with compression */
124     switch(req_format_mapped)
125     {
126         case MALI_GRALLOC_FORMAT_INTERNAL_RAW16:
127         case MALI_GRALLOC_FORMAT_INTERNAL_RAW12:
128         case MALI_GRALLOC_FORMAT_INTERNAL_RAW10:
129         case MALI_GRALLOC_FORMAT_INTERNAL_BLOB:
130         case MALI_GRALLOC_FORMAT_INTERNAL_P010:
131         case MALI_GRALLOC_FORMAT_INTERNAL_P210:
132         case MALI_GRALLOC_FORMAT_INTERNAL_Y410:
133         case HAL_PIXEL_FORMAT_YCbCr_422_I:
134             rval = false;
135             break;
136     }
137     return rval;
138 }
139 
is_android_yuv_format(int req_format)140 static bool is_android_yuv_format(int req_format)
141 {
142     bool rval = false;
143 
144     switch(req_format)
145     {
146         case HAL_PIXEL_FORMAT_YV12:
147         case HAL_PIXEL_FORMAT_Y8:
148         case HAL_PIXEL_FORMAT_Y16:
149         case HAL_PIXEL_FORMAT_YCbCr_420_888:
150         case HAL_PIXEL_FORMAT_YCbCr_422_888:
151         case HAL_PIXEL_FORMAT_YCbCr_444_888:
152             rval = true;
153             break;
154     }
155     return rval;
156 }
157 
is_afbc_allowed(int buffer_size)158 static bool is_afbc_allowed(int buffer_size)
159 {
160     bool afbc_allowed = false;
161 
162     (void) buffer_size;
163 
164 #if GRALLOC_DISP_W != 0 && GRALLOC_DISP_H != 0
165     afbc_allowed = ((buffer_size*100) / (GRALLOC_DISP_W*GRALLOC_DISP_H)) >= GRALLOC_AFBC_MIN_SIZE;
166 
167 #else
168     /* If display size is not valid then always allow AFBC */
169     afbc_allowed = true;
170 
171 #endif
172 
173     return afbc_allowed;
174 }
175 
is_afbc_format(uint64_t internal_format)176 static bool is_afbc_format(uint64_t internal_format)
177 {
178     return (internal_format & MALI_GRALLOC_FORMAT_CAPABILITY_AFBCENABLE_MASK) != 0;
179 }
180 
determine_best_format(int req_format,mali_gralloc_producer_type producer,mali_gralloc_consumer_type consumer,uint64_t producer_runtime_mask,uint64_t consumer_runtime_mask)181 static uint64_t determine_best_format(int req_format, mali_gralloc_producer_type producer, mali_gralloc_consumer_type consumer,
182                                       uint64_t producer_runtime_mask, uint64_t consumer_runtime_mask)
183 {
184     /* Default is to return the requested format */
185     uint64_t internal_format = req_format;
186     uint64_t dpu_mask = dpu_runtime_caps.caps_mask;
187     uint64_t gpu_mask = gpu_runtime_caps.caps_mask;
188     uint64_t vpu_mask = vpu_runtime_caps.caps_mask;
189     uint64_t cam_mask = cam_runtime_caps.caps_mask;
190 
191     if(producer == MALI_GRALLOC_PRODUCER_GPU && gpu_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
192     {
193         gpu_mask &= producer_runtime_mask;
194 
195         if(consumer == MALI_GRALLOC_CONSUMER_GPU_OR_DISPLAY)
196         {
197             gpu_mask &= consumer_runtime_mask;
198             dpu_mask &= consumer_runtime_mask;
199 
200             if(gpu_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK &&
201                dpu_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK)
202             {
203                 internal_format |= MALI_GRALLOC_INTFMT_AFBC_SPLITBLK;
204             }
205             else if(gpu_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC &&
206                     dpu_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC)
207             {
208                 internal_format |= MALI_GRALLOC_INTFMT_AFBC_BASIC;
209 
210                 if(gpu_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_TILED_HEADERS &&
211                    dpu_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_TILED_HEADERS)
212                 {
213                     internal_format |= MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS;
214                 }
215             }
216         }
217         else if(consumer == MALI_GRALLOC_CONSUMER_GPU_EXCL)
218         {
219             gpu_mask &= consumer_runtime_mask;
220 
221             /* When GPU acts as both producer and consumer it prefers 16x16 superblocks */
222             if(gpu_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC)
223             {
224                 internal_format |= MALI_GRALLOC_INTFMT_AFBC_BASIC;
225             }
226 
227             if(gpu_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_TILED_HEADERS)
228             {
229                 internal_format |= MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS;
230             }
231         }
232         else if(consumer == MALI_GRALLOC_CONSUMER_VIDEO_ENCODER)
233         {
234             vpu_mask &= consumer_runtime_mask;
235 
236             if(req_format == HAL_PIXEL_FORMAT_YV12)
237             {
238                 if(gpu_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC &&
239                    vpu_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC)
240                 {
241                     internal_format |= MALI_GRALLOC_INTFMT_AFBC_BASIC;
242                 }
243 
244                 if(gpu_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_TILED_HEADERS &&
245                    vpu_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_TILED_HEADERS)
246                 {
247                     internal_format |= MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS;
248                 }
249             }
250         }
251     }
252     else if(producer == MALI_GRALLOC_PRODUCER_VIDEO_DECODER && vpu_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
253     {
254         vpu_mask &= producer_runtime_mask;
255 
256         if(consumer == MALI_GRALLOC_CONSUMER_GPU_OR_DISPLAY)
257         {
258             gpu_mask &= consumer_runtime_mask;
259             dpu_mask &= consumer_runtime_mask;
260 
261             if(internal_format == HAL_PIXEL_FORMAT_YV12)
262             {
263                 if(vpu_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC &&
264                    gpu_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC &&
265                    dpu_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC)
266                 {
267                     internal_format |= MALI_GRALLOC_INTFMT_AFBC_BASIC;
268                 }
269 
270                 if(vpu_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_TILED_HEADERS &&
271                    gpu_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_TILED_HEADERS &&
272                    dpu_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_TILED_HEADERS)
273                 {
274                     internal_format |= MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS;
275                 }
276             }
277         }
278         else if(consumer == MALI_GRALLOC_CONSUMER_GPU_EXCL)
279         {
280             gpu_mask &= consumer_runtime_mask;
281 
282             if(internal_format == HAL_PIXEL_FORMAT_YV12)
283             {
284                 if(gpu_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC &&
285                    vpu_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC)
286                 {
287                     internal_format |= MALI_GRALLOC_INTFMT_AFBC_BASIC;
288                 }
289 
290                 if(gpu_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_TILED_HEADERS &&
291                    vpu_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_TILED_HEADERS)
292                 {
293                     internal_format |= MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS;
294                 }
295             }
296         }
297         else if(consumer == MALI_GRALLOC_CONSUMER_VIDEO_ENCODER)
298         {
299             /* Fall-through. To be decided.*/
300         }
301   }
302   else if(producer == MALI_GRALLOC_PRODUCER_CAMERA && cam_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
303   {
304         if(consumer == MALI_GRALLOC_CONSUMER_GPU_OR_DISPLAY)
305         {
306             /* Fall-through. To be decided.*/
307         }
308         else if(consumer == MALI_GRALLOC_CONSUMER_GPU_EXCL)
309         {
310             /* Fall-through. To be decided.*/
311         }
312         else if(consumer == MALI_GRALLOC_CONSUMER_VIDEO_ENCODER)
313         {
314             /* Fall-through. To be decided.*/
315         }
316   }
317   return internal_format;
318 }
319 
decode_internal_format(int req_format)320 static uint64_t decode_internal_format(int req_format)
321 {
322     uint64_t internal_format, me_mask, base_format, mapped_base_format;
323     uint64_t ignore_mask;
324 
325     internal_format = GRALLOC_PRIVATE_FORMAT_UNWRAP(req_format);
326 
327     me_mask = internal_format & MALI_GRALLOC_INTFMT_ME_EXT_MASK;
328     if(me_mask > 0 && ((me_mask - 1) & me_mask) != 0)
329     {
330         ALOGE("Internal format contains multiple mutually exclusive modifier bits: %" PRIx64, internal_format);
331         internal_format = 0;
332         goto out;
333     }
334 
335     base_format = internal_format & MALI_GRALLOC_INTFMT_FMT_MASK;
336 
337     /* Even though private format allocations are intended to be for specific
338      * formats, certain test cases uses the flexible formats that needs to be mapped
339      * to internal ones.
340      */
341     mapped_base_format = map_flex_formats((uint32_t ) base_format, &ignore_mask);
342 
343     /* Validate the internal base format passed in */
344     switch(mapped_base_format)
345     {
346         case MALI_GRALLOC_FORMAT_INTERNAL_RGBA_8888:
347         case MALI_GRALLOC_FORMAT_INTERNAL_RGBX_8888:
348         case MALI_GRALLOC_FORMAT_INTERNAL_RGB_888:
349         case MALI_GRALLOC_FORMAT_INTERNAL_RGB_565:
350         case MALI_GRALLOC_FORMAT_INTERNAL_BGRA_8888:
351         case MALI_GRALLOC_FORMAT_INTERNAL_YV12:
352         case MALI_GRALLOC_FORMAT_INTERNAL_Y8:
353         case MALI_GRALLOC_FORMAT_INTERNAL_Y16:
354         case MALI_GRALLOC_FORMAT_INTERNAL_RAW16:
355         case MALI_GRALLOC_FORMAT_INTERNAL_RAW12:
356         case MALI_GRALLOC_FORMAT_INTERNAL_RAW10:
357         case MALI_GRALLOC_FORMAT_INTERNAL_BLOB:
358         case MALI_GRALLOC_FORMAT_INTERNAL_NV12:
359         case MALI_GRALLOC_FORMAT_INTERNAL_NV21:
360         case MALI_GRALLOC_FORMAT_INTERNAL_YUV422_8BIT:
361         case MALI_GRALLOC_FORMAT_INTERNAL_Y0L2:
362         case MALI_GRALLOC_FORMAT_INTERNAL_P010:
363         case MALI_GRALLOC_FORMAT_INTERNAL_P210:
364         case MALI_GRALLOC_FORMAT_INTERNAL_Y210:
365         case MALI_GRALLOC_FORMAT_INTERNAL_Y410:
366             if(mapped_base_format != base_format)
367             {
368                 internal_format = (internal_format & MALI_GRALLOC_INTFMT_EXT_MASK) | mapped_base_format;
369             }
370             break;
371 
372         default:
373             ALOGE("Internal base format requested is unrecognized: %" PRIx64 ,internal_format);
374             internal_format = 0;
375             break;
376     }
377 out:
378     return internal_format;
379 }
380 
determine_producer(mali_gralloc_producer_type * producer,uint64_t * producer_runtime_mask,int req_format,int usage)381 static bool determine_producer(mali_gralloc_producer_type *producer, uint64_t *producer_runtime_mask, int req_format, int usage)
382 {
383     bool rval = true;
384 
385     /* Default to GPU */
386     *producer = MALI_GRALLOC_PRODUCER_GPU;
387 
388     if(usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK))
389     {
390         rval = false;
391     }
392     else if(usage & GRALLOC_USAGE_HW_RENDER)
393     {
394         if(is_android_yuv_format(req_format))
395         {
396             if(gpu_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_YUV_NOWRITE)
397             {
398                 *producer_runtime_mask &= ~MALI_GRALLOC_FORMAT_CAPABILITY_AFBCENABLE_MASK;
399             }
400             else
401             {
402                 /* All GPUs that can write YUV AFBC can only do it in 16x16, optionally with tiled */
403                 *producer_runtime_mask &= ~(MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK | MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_WIDEBLK);
404             }
405         }
406         *producer = MALI_GRALLOC_PRODUCER_GPU;
407     }
408     else if(usage & GRALLOC_USAGE_HW_CAMERA_MASK)
409     {
410         *producer = MALI_GRALLOC_PRODUCER_CAMERA;
411     }
412     /* HW_TEXTURE+HW_COMPOSER+EXTERNAL_DISP is a definition set by
413      * stagefright for "video decoder". We check for it here.
414      */
415     else if((usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_EXTERNAL_DISP)) ==
416                     (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_EXTERNAL_DISP))
417     {
418         *producer = MALI_GRALLOC_PRODUCER_VIDEO_DECODER;
419     }
420 
421    return rval;
422 }
423 
determine_consumer(mali_gralloc_consumer_type * consumer,uint64_t * consumer_runtime_mask,int req_format,int usage)424 static bool determine_consumer(mali_gralloc_consumer_type *consumer, uint64_t *consumer_runtime_mask, int req_format, int usage)
425 {
426     bool rval = true;
427 
428     /* Default to GPU */
429     *consumer = MALI_GRALLOC_CONSUMER_GPU_EXCL;
430 
431     if(usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK))
432     {
433         rval = false;
434     }
435     /* When usage explicitly targets a consumer, as it does with GRALLOC_USAGE_HW_FB,
436      * we pick DPU even if there are no runtime capabilities present.
437      */
438     else if( usage & GRALLOC_USAGE_HW_FB )
439     {
440         *consumer = MALI_GRALLOC_CONSUMER_GPU_OR_DISPLAY;
441     }
442     else if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER)
443     {
444         if((vpu_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_YUV_NOREAD) &&
445            is_android_yuv_format(req_format))
446         {
447             *consumer_runtime_mask &= ~MALI_GRALLOC_FORMAT_CAPABILITY_AFBCENABLE_MASK;
448         }
449         *consumer = MALI_GRALLOC_CONSUMER_VIDEO_ENCODER;
450     }
451     /* GRALLOC_USAGE_HW_COMPOSER is by default applied by SurfaceFlinger so we can't exclusively rely on it
452      * to determine consumer. When a buffer is targeted for either we reject the DPU when it lacks
453      * runtime capabilities, in favor of the more capable GPU.
454      */
455     else if((usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_COMPOSER )) == (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_COMPOSER ) &&
456             dpu_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT)
457     {
458         *consumer = MALI_GRALLOC_CONSUMER_GPU_OR_DISPLAY;
459     }
460     else if(usage & GRALLOC_USAGE_HW_TEXTURE)
461     {
462         *consumer = MALI_GRALLOC_CONSUMER_GPU_EXCL;
463     }
464     return rval;
465 }
466 
467 /*
468  * Here we determine format capabilities for the 4 IPs we support.
469  * For now these are controlled by build defines, but in the future
470  * they should be read out from each user-space driver.
471  */
determine_format_capabilities()472 static void determine_format_capabilities()
473 {
474     /* Loading libraries can take some time and
475      * we may see many allocations at boot.
476      */
477     pthread_mutex_lock(&caps_init_mutex);
478 
479     if(runtime_caps_read)
480     {
481         goto already_init;
482     }
483 
484     memset((void*) &dpu_runtime_caps,0,sizeof(dpu_runtime_caps));
485     memset((void*) &vpu_runtime_caps,0,sizeof(vpu_runtime_caps));
486     memset((void*) &gpu_runtime_caps,0,sizeof(gpu_runtime_caps));
487     memset((void*) &cam_runtime_caps,0,sizeof(cam_runtime_caps));
488 
489     /* Determine DPU format capabilities */
490     if(!get_block_capabilities(true, "hwcomposer", &dpu_runtime_caps))
491     {
492 #if MALI_DISPLAY_VERSION >= 500
493         dpu_runtime_caps.caps_mask |= MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT;
494         dpu_runtime_caps.caps_mask |=  MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC;
495 
496 #if MALI_DISPLAY_VERSION >= 550
497         dpu_runtime_caps.caps_mask |=  MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK;
498 #endif
499 #endif
500     }
501 
502     /* Determine GPU format capabilities */
503     if(access(MALI_GRALLOC_GPU_LIBRARY_PATH1 MALI_GRALLOC_GPU_LIB_NAME,R_OK) == 0)
504     {
505         get_block_capabilities(false, MALI_GRALLOC_GPU_LIBRARY_PATH1 MALI_GRALLOC_GPU_LIB_NAME, &gpu_runtime_caps);
506     }
507     else if(access(MALI_GRALLOC_GPU_LIBRARY_PATH2 MALI_GRALLOC_GPU_LIB_NAME,R_OK) == 0)
508     {
509         get_block_capabilities(false, MALI_GRALLOC_GPU_LIBRARY_PATH2 MALI_GRALLOC_GPU_LIB_NAME, &gpu_runtime_caps);
510     }
511 
512     if((gpu_runtime_caps.caps_mask & MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT) == 0)
513     {
514         ALOGW("Failed to find GPU block configuration in %s. Using static build configuration.", MALI_GRALLOC_GPU_LIB_NAME);
515 
516 #if MALI_GPU_SUPPORT_AFBC_BASIC == 1
517         gpu_runtime_caps.caps_mask |= MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT;
518         gpu_runtime_caps.caps_mask |= MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC;
519 
520         /* Need to verify when to remove this */
521         gpu_runtime_caps.caps_mask |= MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_YUV_NOWRITE;
522 
523 #if MALI_SUPPORT_AFBC_SPLITBLK == 1
524         gpu_runtime_caps.caps_mask |= MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK;
525 #endif
526 
527 #if MALI_SUPPORT_AFBC_WIDEBLK == 1
528         gpu_runtime_caps.caps_mask |= MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK;
529         gpu_runtime_caps.caps_mask |= MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_WIDEBLK;
530 #endif
531 
532 #if MALI_USE_YUV_AFBC_WIDEBLK != 1
533         gpu_runtime_caps.caps_mask |= MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_WIDEBLK_YUV_DISABLE;
534 #endif
535 
536 #if MALI_SUPPORT_AFBC_TILED_HEADERS == 1
537         gpu_runtime_caps.caps_mask |= MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK;
538         gpu_runtime_caps.caps_mask |= MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_WIDEBLK;
539         gpu_runtime_caps.caps_mask |= MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_TILED_HEADERS;
540 #endif
541 #endif /* MALI_GPU_SUPPORT_AFBC_BASIC == 1 */
542     }
543 
544     /* Determine VPU format capabilities */
545 #if MALI_VIDEO_VERSION == 500 || MALI_VIDEO_VERSION == 550
546     vpu_runtime_caps.caps_mask |= MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT;
547     vpu_runtime_caps.caps_mask |= MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC;
548     vpu_runtime_caps.caps_mask |= MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_YUV_NOREAD;
549 #endif
550 
551 #if MALI_VIDEO_VERSION == 61
552     vpu_runtime_caps.caps_mask |= MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT;
553     vpu_runtime_caps.caps_mask |= MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC;
554     vpu_runtime_caps.caps_mask |= MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_TILED_HEADERS;
555 #endif
556 
557 
558     /* Build specific capability changes */
559 #if GRALLOC_ARM_NO_EXTERNAL_AFBC == 1
560     {
561         dpu_runtime_caps.caps_mask &= ~MALI_GRALLOC_FORMAT_CAPABILITY_AFBCENABLE_MASK;
562         gpu_runtime_caps.caps_mask &= ~MALI_GRALLOC_FORMAT_CAPABILITY_AFBCENABLE_MASK;
563         vpu_runtime_caps.caps_mask &= ~MALI_GRALLOC_FORMAT_CAPABILITY_AFBCENABLE_MASK;
564         cam_runtime_caps.caps_mask &= ~MALI_GRALLOC_FORMAT_CAPABILITY_AFBCENABLE_MASK;
565     }
566 #endif
567 
568     runtime_caps_read = true;
569 
570 already_init:
571     pthread_mutex_unlock(&caps_init_mutex);
572 
573     ALOGV("GPU format capabilities 0x%" PRIx64 , gpu_runtime_caps.caps_mask);
574     ALOGV("DPU format capabilities 0x%" PRIx64 , dpu_runtime_caps.caps_mask);
575     ALOGV("VPU format capabilities 0x%" PRIx64 , vpu_runtime_caps.caps_mask);
576     ALOGV("CAM format capabilities 0x%" PRIx64 , cam_runtime_caps.caps_mask);
577 }
578 
mali_gralloc_select_format(int req_format,int usage,int buffer_size)579 uint64_t mali_gralloc_select_format(int req_format, int usage, int buffer_size)
580 {
581     uint64_t internal_format = 0;
582     mali_gralloc_consumer_type consumer;
583     mali_gralloc_producer_type producer;
584     uint64_t producer_runtime_mask = ~(0ULL);
585     uint64_t consumer_runtime_mask = ~(0ULL);
586     int req_format_mapped=0;
587 
588     if(!runtime_caps_read)
589     {
590         /*
591          * It is better to initialize these when needed because
592          * not all processes allocates memory.
593          */
594         determine_format_capabilities();
595     }
596 
597     /* A unique usage specifies that an internal format is in req_format */
598     if(usage & MALI_GRALLOC_USAGE_PRIVATE_FORMAT)
599     {
600         internal_format = decode_internal_format(req_format);
601         goto out;
602     }
603 
604     /* Re-map special Android formats */
605     req_format_mapped = map_flex_formats(req_format, &producer_runtime_mask);
606 
607     /* Determine producer/consumer */
608     if(!determine_producer(&producer, &producer_runtime_mask, req_format, usage) ||
609        !determine_consumer(&consumer, &consumer_runtime_mask, req_format, usage))
610     {
611         /* Failing to determine producer/consumer usually means
612          * client has requested sw rendering.
613          */
614         internal_format = req_format_mapped;
615         goto out;
616     }
617 
618     /*
619      * Determine runtime capability limitations
620      */
621 
622     /* Disable AFBC based on unique usage */
623     if ((usage & MALI_GRALLOC_USAGE_NO_AFBC) == MALI_GRALLOC_USAGE_NO_AFBC)
624     {
625         if(is_android_yuv_format(req_format_mapped))
626         {
627             ALOGE("It is invalid to specify NO_AFBC usage flags when allocating YUV formats.\
628                    Requested fmt: 0x%08X Re-Mapped fmt: 0x%08X",req_format,req_format_mapped);
629             internal_format = 0;
630             goto out;
631         }
632         producer_runtime_mask &= ~MALI_GRALLOC_FORMAT_CAPABILITY_AFBCENABLE_MASK;
633     }
634     /* Disable AFBC based on buffer dimensions */
635     else if(!is_afbc_allowed(buffer_size))
636     {
637         producer_runtime_mask &= ~MALI_GRALLOC_FORMAT_CAPABILITY_AFBCENABLE_MASK;
638     }
639     else if(!is_afbc_supported(req_format_mapped))
640     {
641         producer_runtime_mask &= ~MALI_GRALLOC_FORMAT_CAPABILITY_AFBCENABLE_MASK;
642     }
643 
644     /* Automatically select format in case producer/consumer identified */
645     internal_format = determine_best_format(req_format_mapped, producer, consumer, producer_runtime_mask, consumer_runtime_mask);
646 
647 out:
648     ALOGV("mali_gralloc_select_format: req_format=0x%08X req_fmt_mapped=0x%08X internal_format=0x%" PRIx64 " usage=0x%08X",req_format, req_format_mapped, internal_format, usage);
649 
650     return internal_format;
651 }
652 
653 extern "C"
654 {
mali_gralloc_get_gpu_caps(struct mali_gralloc_format_caps * gpu_caps)655 void mali_gralloc_get_gpu_caps(struct mali_gralloc_format_caps *gpu_caps)
656 {
657     if(gpu_caps != NULL)
658     {
659         if(!runtime_caps_read)
660         {
661             determine_format_capabilities();
662         }
663         memcpy(gpu_caps,(void*) &gpu_runtime_caps,sizeof(struct mali_gralloc_format_caps));
664     }
665 }
666 }
667