1 /*
2  * Copyright (C) 2018-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 #include <inttypes.h>
19 #include "gralloc_helper.h"
20 #include "mali_gralloc_formats.h"
21 #include "format_info.h"
22 #include "mali_gralloc_usages.h"
23 #include <exynos_format.h>
24 
25 #include <unordered_map>
26 #include <mutex>
27 
28 /* Default width aligned to whole pixel (CPU access). */
29 #define ALIGN_W_CPU_DEFAULT .align_w_cpu = 1
30 
31 #define ID(x) .id = x, .name = #x
32 
33 /*
34  * Format table, containing format properties.
35  *
36  * NOTE: This table should only be used within
37  * the gralloc library and not by clients directly.
38  */
39 const format_info_t formats[] = {
40 	/* BEGIN ALIGNED SECTION */
41 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_RGB_565),                .npln = 1, .ncmp = { 3, 0, 0 }, .bps = 6,  .bpp_afbc = { 16, 0,  0 }, .bpp = { 16, 0,  0 }, .hsub = 0, .vsub = 0, .align_w = 16, .align_h = 16, .align_w_cpu = 16,   .tile_size = 1,  .has_alpha = false, .is_rgb = true,  .is_yuv = false, .afbc = true,  .linear = true,  .yuv_transform = true,  .flex = false, .planes_contiguous = false },
42 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_RGB_888),                .npln = 1, .ncmp = { 3, 0, 0 }, .bps = 8,  .bpp_afbc = { 24, 0,  0 }, .bpp = { 24, 0,  0 }, .hsub = 0, .vsub = 0, .align_w = 16, .align_h = 16, .align_w_cpu = 16,   .tile_size = 1,  .has_alpha = false, .is_rgb = true,  .is_yuv = false, .afbc = true,  .linear = true,  .yuv_transform = true,  .flex = true,  .planes_contiguous = false },
43 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_RGBA_8888),              .npln = 1, .ncmp = { 4, 0, 0 }, .bps = 8,  .bpp_afbc = { 32, 0,  0 }, .bpp = { 32, 0,  0 }, .hsub = 0, .vsub = 0, .align_w = 16, .align_h = 16, .align_w_cpu = 16,   .tile_size = 1,  .has_alpha = true,  .is_rgb = true,  .is_yuv = false, .afbc = true,  .linear = true,  .yuv_transform = true,  .flex = true,  .planes_contiguous = false },
44 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_BGRA_8888),              .npln = 1, .ncmp = { 4, 0, 0 }, .bps = 8,  .bpp_afbc = { 32, 0,  0 }, .bpp = { 32, 0,  0 }, .hsub = 0, .vsub = 0, .align_w = 16, .align_h = 16, .align_w_cpu = 16,   .tile_size = 1,  .has_alpha = true,  .is_rgb = true,  .is_yuv = false, .afbc = true,  .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
45 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_RGBX_8888),              .npln = 1, .ncmp = { 3, 0, 0 }, .bps = 8,  .bpp_afbc = { 32, 0,  0 }, .bpp = { 32, 0,  0 }, .hsub = 0, .vsub = 0, .align_w = 16, .align_h = 16, .align_w_cpu = 16,   .tile_size = 1,  .has_alpha = false, .is_rgb = true,  .is_yuv = false, .afbc = true,  .linear = true,  .yuv_transform = true,  .flex = true,  .planes_contiguous = false },
46 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_RGBA_1010102),           .npln = 1, .ncmp = { 4, 0, 0 }, .bps = 10, .bpp_afbc = { 32, 0,  0 }, .bpp = { 32, 0,  0 }, .hsub = 0, .vsub = 0, .align_w = 16, .align_h = 16, .align_w_cpu = 16,   .tile_size = 1,  .has_alpha = true,  .is_rgb = true,  .is_yuv = false, .afbc = true,  .linear = true,  .yuv_transform = true,  .flex = false, .planes_contiguous = false },
47 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_RGBA_16161616),          .npln = 1, .ncmp = { 4, 0, 0 }, .bps = 16, .bpp_afbc = { 64, 0,  0 }, .bpp = { 64, 0,  0 }, .hsub = 0, .vsub = 0, .align_w = 16, .align_h = 16, .align_w_cpu = 16,   .tile_size = 1,  .has_alpha = true,  .is_rgb = true,  .is_yuv = false, .afbc = true,  .linear = true,  .yuv_transform = true,  .flex = true,  .planes_contiguous = false },
48 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_Y8),                     .npln = 1, .ncmp = { 1, 0, 0 }, .bps = 8,  .bpp_afbc = { 8,  0,  0 }, .bpp = { 8,  0,  0 }, .hsub = 1, .vsub = 1, .align_w = 2,  .align_h = 2,  .align_w_cpu = 16,   .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = true,  .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
49 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_Y16),                    .npln = 1, .ncmp = { 1, 0, 0 }, .bps = 16, .bpp_afbc = { 16, 0,  0 }, .bpp = { 16, 0,  0 }, .hsub = 1, .vsub = 1, .align_w = 2,  .align_h = 2,  .align_w_cpu = 16,   .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = true,  .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
50 	/* 420 (8-bit) */
51 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_YUV420_8BIT_I),          .npln = 1, .ncmp = { 3, 0, 0 }, .bps = 8,  .bpp_afbc = { 12, 0,  0 }, .bpp = { 0,  0,  0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = true,  .linear = false, .yuv_transform = false, .flex = false, .planes_contiguous = false },
52 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_NV12),                   .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 8,  .bpp_afbc = { 8,  16, 0 }, .bpp = { 8,  16, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = true,  .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
53 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_NV21),                   .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 8,  .bpp_afbc = { 8,  16, 0 }, .bpp = { 8,  16, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = true,  .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
54 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_YV12),                   .npln = 3, .ncmp = { 1, 1, 1 }, .bps = 8,  .bpp_afbc = { 8,  8,  8 }, .bpp = { 8,  8,  8 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  .align_w_cpu = 16,   .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = true,  .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
55 	/* 422 (8-bit) */
56 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_YUV422_8BIT),            .npln = 1, .ncmp = { 3, 0, 0 }, .bps = 8,  .bpp_afbc = { 16, 0,  0 }, .bpp = { 16, 0,  0 }, .hsub = 2, .vsub = 1, .align_w = 2,  .align_h = 1,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = true,  .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
57 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_NV16),                   .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 8,  .bpp_afbc = { 8,  16, 0 }, .bpp = { 8,  16, 0 }, .hsub = 2, .vsub = 1, .align_w = 2,  .align_h = 1,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = true,  .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
58 	/* 420 (10-bit) */
59 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_YUV420_10BIT_I),         .npln = 1, .ncmp = { 3, 0, 0 }, .bps = 10, .bpp_afbc = { 15, 0,  0 }, .bpp = { 0,  0,  0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = true,  .linear = false, .yuv_transform = false, .flex = false, .planes_contiguous = false },
60 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_Y0L2),                   .npln = 1, .ncmp = { 4, 0, 0 }, .bps = 10, .bpp_afbc = { 0,  0,  0 }, .bpp = { 16, 0,  0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 2,  .has_alpha = true,  .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = false, .planes_contiguous = false },
61 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_P010),                   .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 10, .bpp_afbc = { 10, 20, 0 }, .bpp = { 16, 32, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = true,  .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
62 	/* 422 (10-bit) */
63 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_Y210),                   .npln = 1, .ncmp = { 3, 0, 0 }, .bps = 10, .bpp_afbc = { 20, 0,  0 }, .bpp = { 32, 0,  0 }, .hsub = 2, .vsub = 1, .align_w = 2,  .align_h = 1,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = true,  .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
64 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_P210),                   .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 10, .bpp_afbc = { 10, 20, 0 }, .bpp = { 16, 32, 0 }, .hsub = 2, .vsub = 1, .align_w = 2,  .align_h = 1,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = true,  .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
65 	/* 444 (10-bit) */
66 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_YUV444_10BIT_I),         .npln = 1, .ncmp = { 3, 0, 0 }, .bps = 10, .bpp_afbc = { 30, 0,  0 }, .bpp = { 0,  0,  0 }, .hsub = 1, .vsub = 1, .align_w = 1,  .align_h = 1,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = true,  .linear = false, .yuv_transform = false, .flex = false, .planes_contiguous = false },
67 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_Y410),                   .npln = 1, .ncmp = { 4, 0, 0 }, .bps = 10, .bpp_afbc = { 0,  0,  0 }, .bpp = { 32, 0,  0 }, .hsub = 1, .vsub = 1, .align_w = 1,  .align_h = 1,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = true,  .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = false, .planes_contiguous = false },
68 	/* Other */
69 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_RAW16),                  .npln = 1, .ncmp = { 1, 0, 0 }, .bps = 16, .bpp_afbc = { 0,  0,  0 }, .bpp = { 16, 0,  0 }, .hsub = 0, .vsub = 0, .align_w = 2,  .align_h = 2,  .align_w_cpu = 16,   .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = false, .afbc = false, .linear = true,  .yuv_transform = false, .flex = false, .planes_contiguous = false },
70 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_RAW12),                  .npln = 1, .ncmp = { 1, 0, 0 }, .bps = 12, .bpp_afbc = { 0,  0,  0 }, .bpp = { 12, 0,  0 }, .hsub = 0, .vsub = 0, .align_w = 4,  .align_h = 2,  .align_w_cpu = 4,    .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = false, .afbc = false, .linear = true,  .yuv_transform = false, .flex = false, .planes_contiguous = false },
71 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_RAW10),                  .npln = 1, .ncmp = { 1, 0, 0 }, .bps = 10, .bpp_afbc = { 0,  0,  0 }, .bpp = { 10, 0,  0 }, .hsub = 0, .vsub = 0, .align_w = 4,  .align_h = 2,  .align_w_cpu = 4,    .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = false, .afbc = false, .linear = true,  .yuv_transform = false, .flex = false, .planes_contiguous = false },
72 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_BLOB),                   .npln = 1, .ncmp = { 1, 0, 0 }, .bps = 8,  .bpp_afbc = { 0,  0,  0 }, .bpp = { 8,  0,  0 }, .hsub = 0, .vsub = 0, .align_w = 1,  .align_h = 1,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = false, .afbc = false, .linear = true,  .yuv_transform = false, .flex = false, .planes_contiguous = false },
73 	/* Depth and Stencil */
74 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_DEPTH_16),               .npln = 1, .ncmp = { 1, 0, 0 }, .bps = 16, .bpp_afbc = { 0,  0,  0 }, .bpp = { 16, 0,  0 }, .hsub = 0, .vsub = 0, .align_w = 1,  .align_h = 1,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = false, .afbc = false, .linear = true,  .yuv_transform = false, .flex = false, .planes_contiguous = false },
75 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_DEPTH_24),               .npln = 1, .ncmp = { 1, 0, 0 }, .bps = 24, .bpp_afbc = { 0,  0,  0 }, .bpp = { 24, 0,  0 }, .hsub = 0, .vsub = 0, .align_w = 1,  .align_h = 1,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = false, .afbc = false, .linear = true,  .yuv_transform = false, .flex = false, .planes_contiguous = false },
76 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_DEPTH_24_STENCIL_8),     .npln = 1, .ncmp = { 2, 0, 0 }, .bps = 24, .bpp_afbc = { 0,  0,  0 }, .bpp = { 32, 0,  0 }, .hsub = 0, .vsub = 0, .align_w = 1,  .align_h = 1,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = false, .afbc = false, .linear = true,  .yuv_transform = false, .flex = false, .planes_contiguous = false },
77 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_DEPTH_32F),              .npln = 1, .ncmp = { 1, 0, 0 }, .bps = 32, .bpp_afbc = { 0,  0,  0 }, .bpp = { 32, 0,  0 }, .hsub = 0, .vsub = 0, .align_w = 1,  .align_h = 1,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = false, .afbc = false, .linear = true,  .yuv_transform = false, .flex = false, .planes_contiguous = false },
78 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_DEPTH_32F_STENCIL_8),    .npln = 1, .ncmp = { 2, 0, 0 }, .bps = 32, .bpp_afbc = { 0,  0,  0 }, .bpp = { 40, 0,  0 }, .hsub = 0, .vsub = 0, .align_w = 1,  .align_h = 1,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = false, .afbc = false, .linear = true,  .yuv_transform = false, .flex = false, .planes_contiguous = false },
79 	{ ID(MALI_GRALLOC_FORMAT_INTERNAL_STENCIL_8),              .npln = 1, .ncmp = { 1, 0, 0 }, .bps = 8,  .bpp_afbc = { 0,  0,  0 }, .bpp = { 8,  0,  0 }, .hsub = 0, .vsub = 0, .align_w = 1,  .align_h = 1,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = false, .afbc = false, .linear = true,  .yuv_transform = false, .flex = false, .planes_contiguous = false },
80 	/* Exynos Formats */
81 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M),              .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 8,  .bpp_afbc = { 0,  0,  0 }, .bpp = { 8,  16, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
82 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_FULL),         .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 8,  .bpp_afbc = { 0,  0,  0 }, .bpp = { 8,  16, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
83 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M),              .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 8,  .bpp_afbc = { 0,  0,  0 }, .bpp = { 8,  16, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
84 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B),         .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 10, .bpp_afbc = { 0,  0,  0 }, .bpp = { 16, 32, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
85 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_TILED),        .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 8,  .bpp_afbc = { 0,  0,  0 }, .bpp = { 8,  16, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 16, .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
86 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YV12_M),                      .npln = 3, .ncmp = { 1, 1, 1 }, .bps = 8,  .bpp_afbc = { 0,  0,  0 }, .bpp = { 8,  8,  8 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
87 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P_M),               .npln = 3, .ncmp = { 1, 1, 1 }, .bps = 8,  .bpp_afbc = { 0,  0,  0 }, .bpp = { 8,  8,  8 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
88 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN),               .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 8,  .bpp_afbc = { 0,  0,  0 }, .bpp = { 8,  16, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
89 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_S10B),          .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 10, .bpp_afbc = { 0,  0,  0 }, .bpp = { 16, 32, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
90 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_M),                .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 10, .bpp_afbc = { 0,  0,  0 }, .bpp = { 16, 32, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
91 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P),                 .npln = 3, .ncmp = { 1, 1, 1 }, .bps = 8,  .bpp_afbc = { 0,  0,  0 }, .bpp = { 8,  8,  8 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  .align_w_cpu = 16,   .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
92 	{ ID(HAL_PIXEL_FORMAT_YCrCb_420_SP),                       .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 8,  .bpp_afbc = { 0,  0,  0 }, .bpp = { 8,  16, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
93 
94 	/* SBWC Formats */
95 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC),          .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 8,  .bpp_afbc = { 0,  0,  0 }, .bpp = { 8,  16, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
96 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC),      .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 10, .bpp_afbc = { 0,  0,  0 }, .bpp = { 16, 32, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
97 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC),         .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 8,  .bpp_afbc = { 0,  0,  0 }, .bpp = { 8,  16, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
98 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC),     .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 10, .bpp_afbc = { 0,  0,  0 }, .bpp = { 16, 32, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
99 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_SBWC),         .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 8,  .bpp_afbc = { 0,  0,  0 }, .bpp = { 8,  16, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
100 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_10B_SBWC),     .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 10, .bpp_afbc = { 0,  0,  0 }, .bpp = { 16, 32, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
101 
102 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L50),     .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 8,  .bpp_afbc = { 0,  0,  0 }, .bpp = { 8,  16, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
103 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L75),     .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 8,  .bpp_afbc = { 0,  0,  0 }, .bpp = { 8,  16, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
104 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L50),      .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 8,  .bpp_afbc = { 0,  0,  0 }, .bpp = { 8,  16, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
105 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L75),      .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 8,  .bpp_afbc = { 0,  0,  0 }, .bpp = { 8,  16, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
106 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L40), .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 10, .bpp_afbc = { 0,  0,  0 }, .bpp = { 16, 32, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
107 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L60), .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 10, .bpp_afbc = { 0,  0,  0 }, .bpp = { 16, 32, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
108 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L80), .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 10, .bpp_afbc = { 0,  0,  0 }, .bpp = { 16, 32, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
109 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L40),  .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 10, .bpp_afbc = { 0,  0,  0 }, .bpp = { 16, 32, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
110 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L60),  .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 10, .bpp_afbc = { 0,  0,  0 }, .bpp = { 16, 32, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
111 	{ ID(HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L80),  .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 10, .bpp_afbc = { 0,  0,  0 }, .bpp = { 16, 32, 0 }, .hsub = 2, .vsub = 2, .align_w = 2,  .align_h = 2,  ALIGN_W_CPU_DEFAULT, .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
112 
113 	/* Google Formats */
114 	{ ID(HAL_PIXEL_FORMAT_GOOGLE_NV12_SP),                     .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 8,  .bpp_afbc = { 0,  0,  0 }, .bpp = { 8,  16, 0 }, .hsub = 2, .vsub = 2, .align_w = 64, .align_h = 8,  .align_w_cpu = 64,   .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = true },
115 	{ ID(HAL_PIXEL_FORMAT_GOOGLE_NV12_SP_10B),                 .npln = 2, .ncmp = { 1, 2, 0 }, .bps = 10, .bpp_afbc = { 0,  0,  0 }, .bpp = { 16, 32, 0 }, .hsub = 2, .vsub = 2, .align_w = 64, .align_h = 8,  .align_w_cpu = 64,   .tile_size = 1,  .has_alpha = false, .is_rgb = false, .is_yuv = true,  .afbc = false, .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = true },
116 	{ ID(HAL_PIXEL_FORMAT_GOOGLE_R_8),                         .npln = 1, .ncmp = { 1, 0, 0 }, .bps = 8,  .bpp_afbc = { 8,  0,  0 }, .bpp = { 8,  0,  0 }, .hsub = 0, .vsub = 0, .align_w = 16, .align_h = 16, .align_w_cpu = 16,   .tile_size = 1,  .has_alpha = false, .is_rgb = true,  .is_yuv = false, .afbc = true,  .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
117 	{ ID(HAL_PIXEL_FORMAT_GOOGLE_RG_88),                       .npln = 1, .ncmp = { 2, 0, 0 }, .bps = 8,  .bpp_afbc = { 16, 0,  0 }, .bpp = { 16, 0,  0 }, .hsub = 0, .vsub = 0, .align_w = 16, .align_h = 16, .align_w_cpu = 16,   .tile_size = 1,  .has_alpha = false, .is_rgb = true,  .is_yuv = false, .afbc = true,  .linear = true,  .yuv_transform = false, .flex = true,  .planes_contiguous = false },
118 	/* END ALIGNED SECTION */
119 };
120 const size_t num_formats = sizeof(formats)/sizeof(formats[0]);
121 
122 /*
123  * This table represents the superset of flags for each base format and producer/consumer.
124  * Where IP does not support a capability, it should be defined and not set.
125  */
126 const format_ip_support_t formats_ip_support[] = {
127 	/* BEGIN ALIGNED SECTION */
128 	/* TODO(b/189467474) AFBC disabled on the GPU for RGB_565 due to color swap in Vulkan */
129 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_RGB_565,             .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_LIN,        .gpu_wr = F_LIN,        .dpu_rd = F_LIN|F_AFBC, .dpu_wr = F_NONE, .dpu_aeu_wr = F_AFBC, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_NONE,  },
130 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_RGB_888,             .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_LIN|F_AFBC, .gpu_wr = F_LIN|F_AFBC, .dpu_rd = F_LIN|F_AFBC, .dpu_wr = F_LIN,  .dpu_aeu_wr = F_AFBC, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_NONE,  },
131 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_RGBA_8888,           .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_LIN|F_AFBC, .gpu_wr = F_LIN|F_AFBC, .dpu_rd = F_LIN|F_AFBC, .dpu_wr = F_LIN,  .dpu_aeu_wr = F_AFBC, .vpu_rd = F_LIN,  .vpu_wr = F_NONE, .cam_wr = F_LIN,   },
132 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_BGRA_8888,           .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_LIN,        .gpu_wr = F_LIN,        .dpu_rd = F_LIN,        .dpu_wr = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_rd = F_LIN,  .vpu_wr = F_NONE, .cam_wr = F_NONE,  },
133 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_RGBX_8888,           .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_LIN|F_AFBC, .gpu_wr = F_LIN|F_AFBC, .dpu_rd = F_LIN|F_AFBC, .dpu_wr = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_NONE,  },
134 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_RGBA_1010102,        .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_LIN|F_AFBC, .gpu_wr = F_LIN|F_AFBC, .dpu_rd = F_LIN|F_AFBC, .dpu_wr = F_LIN,  .dpu_aeu_wr = F_AFBC, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_NONE,  },
135 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_RGBA_16161616,       .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_LIN|F_AFBC, .gpu_wr = F_LIN|F_AFBC, .dpu_rd = F_NONE,       .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_NONE,  },
136 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_Y8,                  .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_NONE,       .gpu_wr = F_NONE,       .dpu_rd = F_NONE,       .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_LIN,  },
137 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_Y16,                 .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_NONE,       .gpu_wr = F_NONE,       .dpu_rd = F_NONE,       .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_NONE,  },
138 	/* 420 (8-bit) */
139 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_YUV420_8BIT_I,       .cpu_rd = F_NONE, .cpu_wr = F_NONE, .gpu_rd = F_AFBC,       .gpu_wr = F_NONE,       .dpu_rd = F_AFBC,       .dpu_wr = F_NONE, .dpu_aeu_wr = F_AFBC, .vpu_rd = F_AFBC, .vpu_wr = F_AFBC, .cam_wr = F_NONE,  },
140 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_NV12,                .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_LIN|F_AFBC, .gpu_wr = F_LIN,        .dpu_rd = F_LIN,        .dpu_wr = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_rd = F_LIN,  .vpu_wr = F_LIN,  .cam_wr = F_NONE,  },
141 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_NV21,                .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_LIN,        .gpu_wr = F_LIN,        .dpu_rd = F_NONE,       .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_LIN,  .vpu_wr = F_LIN,  .cam_wr = F_LIN,   },
142 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_YV12,                .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_LIN,        .gpu_wr = F_LIN,        .dpu_rd = F_LIN,        .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_LIN,  .vpu_wr = F_LIN,  .cam_wr = F_LIN,  },
143 	/* 422 (8-bit) */
144 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_YUV422_8BIT,         .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_LIN|F_AFBC, .gpu_wr = F_LIN|F_AFBC, .dpu_rd = F_LIN|F_AFBC, .dpu_wr = F_NONE, .dpu_aeu_wr = F_AFBC, .vpu_rd = F_LIN,  .vpu_wr = F_LIN,  .cam_wr = F_LIN,   },
145 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_NV16,                .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_LIN|F_AFBC, .gpu_wr = F_LIN,        .dpu_rd = F_NONE,       .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_NONE,  },
146 	/* 420 (10-bit) */
147 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_YUV420_10BIT_I,      .cpu_rd = F_NONE, .cpu_wr = F_NONE, .gpu_rd = F_AFBC,       .gpu_wr = F_AFBC,       .dpu_rd = F_AFBC,       .dpu_wr = F_NONE, .dpu_aeu_wr = F_AFBC, .vpu_rd = F_AFBC, .vpu_wr = F_AFBC, .cam_wr = F_NONE,  },
148 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_Y0L2,                .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_LIN,        .gpu_wr = F_LIN,        .dpu_rd = F_LIN,        .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_LIN,  .vpu_wr = F_LIN,  .cam_wr = F_NONE,  },
149 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_P010,                .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_LIN,        .gpu_wr = F_LIN,        .dpu_rd = F_LIN,        .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_LIN,  .vpu_wr = F_LIN,  .cam_wr = F_NONE,  },
150 	/* 422 (10-bit) */
151 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_Y210,                .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_LIN|F_AFBC, .gpu_wr = F_LIN|F_AFBC, .dpu_rd = F_NONE,       .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_NONE,  },
152 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_P210,                .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_LIN,        .gpu_wr = F_LIN,        .dpu_rd = F_NONE,       .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_NONE,  },
153 	/* 444 (10-bit) */
154 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_YUV444_10BIT_I,      .cpu_rd = F_NONE, .cpu_wr = F_NONE, .gpu_rd = F_NONE,       .gpu_wr = F_NONE,       .dpu_rd = F_NONE,       .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_NONE,  },
155 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_Y410,                .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_LIN,        .gpu_wr = F_LIN|F_AFBC, .dpu_rd = F_NONE,       .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_NONE,  },
156 	/* Other */
157 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_RAW16,               .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_LIN,        .gpu_wr = F_NONE,       .dpu_rd = F_NONE,       .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_LIN,   },
158 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_RAW12,               .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_NONE,       .gpu_wr = F_NONE,       .dpu_rd = F_NONE,       .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_LIN,  },
159 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_RAW10,               .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_LIN,        .gpu_wr = F_NONE,       .dpu_rd = F_NONE,       .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_LIN,  },
160 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_BLOB,                .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_LIN,        .gpu_wr = F_LIN,        .dpu_rd = F_NONE,       .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_LIN,  },
161 	/* Depth and Stencil */
162 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_DEPTH_16,            .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_NONE,       .gpu_wr = F_NONE,       .dpu_rd = F_NONE,       .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_NONE,  },
163 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_DEPTH_24,            .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_NONE,       .gpu_wr = F_NONE,       .dpu_rd = F_NONE,       .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_NONE,  },
164 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_DEPTH_24_STENCIL_8,  .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_NONE,       .gpu_wr = F_NONE,       .dpu_rd = F_NONE,       .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_NONE,  },
165 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_DEPTH_32F,           .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_NONE,       .gpu_wr = F_NONE,       .dpu_rd = F_NONE,       .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_NONE,  },
166 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_DEPTH_32F_STENCIL_8, .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_NONE,       .gpu_wr = F_NONE,       .dpu_rd = F_NONE,       .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_NONE,  },
167 	{ .id = MALI_GRALLOC_FORMAT_INTERNAL_STENCIL_8,           .cpu_rd = F_LIN,  .cpu_wr = F_LIN,  .gpu_rd = F_NONE,       .gpu_wr = F_NONE,       .dpu_rd = F_NONE,       .dpu_wr = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_rd = F_NONE, .vpu_wr = F_NONE, .cam_wr = F_NONE,  },
168 	/* END ALIGNED SECTION */
169 
170 	/* Exynos Formats */
171 	/* BEGIN ALIGNED SECTION */
172 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M,              .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
173 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_FULL,         .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
174 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M,              .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
175 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YV12_M,                      .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
176 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P_M,               .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
177 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B,         .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
178 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_TILED,        .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
179 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN,               .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
180 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_S10B,          .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
181 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_M,                .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
182 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P,                 .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
183 	{ .id = HAL_PIXEL_FORMAT_YCrCb_420_SP,                       .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
184 
185 	/* SBWC Formats */
186 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC,          .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
187 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC,      .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
188 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC,         .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
189 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC,     .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
190 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_SBWC,         .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
191 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_10B_SBWC,     .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
192 
193 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L50,     .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
194 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L75,     .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
195 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L50,      .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
196 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L75,      .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
197 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L40, .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
198 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L60, .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
199 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L80, .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
200 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L40,  .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
201 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L60,  .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
202 	{ .id = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L80,  .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN, .gpu_rd = F_LIN, .dpu_wr = F_NONE, .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN, .vpu_rd = F_LIN },
203 	/* END ALIGNED SECTION */
204 
205 	/* Google formats */
206 	/* BEGIN ALIGNED SECTION */
207 	{ .id = HAL_PIXEL_FORMAT_GOOGLE_NV12_SP,     .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN,        .gpu_rd = F_LIN,        .dpu_wr = F_LIN,  .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN,  .vpu_rd = F_LIN,  .cam_wr = F_LIN },
208 	{ .id = HAL_PIXEL_FORMAT_GOOGLE_NV12_SP_10B, .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN,        .gpu_rd = F_LIN,        .dpu_wr = F_LIN,  .dpu_rd = F_LIN,  .dpu_aeu_wr = F_NONE, .vpu_wr = F_LIN,  .vpu_rd = F_LIN,  .cam_wr = F_LIN },
209 	{ .id = HAL_PIXEL_FORMAT_GOOGLE_R_8,         .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN|F_AFBC, .gpu_rd = F_LIN|F_AFBC, .dpu_wr = F_NONE, .dpu_rd = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_wr = F_NONE, .vpu_rd = F_NONE, .cam_wr = F_LIN },
210 	{ .id = HAL_PIXEL_FORMAT_GOOGLE_RG_88,       .cpu_wr = F_LIN, .cpu_rd = F_LIN, .gpu_wr = F_LIN|F_AFBC, .gpu_rd = F_LIN|F_AFBC, .dpu_wr = F_NONE, .dpu_rd = F_NONE, .dpu_aeu_wr = F_NONE, .vpu_wr = F_NONE, .vpu_rd = F_NONE, .cam_wr = F_LIN },
211 	/* END ALIGNED SECTION */
212 };
213 
214 const size_t num_ip_formats = sizeof(formats_ip_support)/sizeof(formats_ip_support[0]);
215 
216 typedef struct
217 {
218     uint32_t hal_format;
219     bool is_flex;
220     uint32_t internal_format;
221 } hal_int_fmt;
222 
223 
224 static const hal_int_fmt hal_to_internal_format[] =
225 {
226 	{ HAL_PIXEL_FORMAT_RGBA_8888,              false, MALI_GRALLOC_FORMAT_INTERNAL_RGBA_8888 },
227 	{ HAL_PIXEL_FORMAT_RGBX_8888,              false, MALI_GRALLOC_FORMAT_INTERNAL_RGBX_8888 },
228 	{ HAL_PIXEL_FORMAT_RGB_888,                false, MALI_GRALLOC_FORMAT_INTERNAL_RGB_888 },
229 	{ HAL_PIXEL_FORMAT_RGB_565,                false, MALI_GRALLOC_FORMAT_INTERNAL_RGB_565 },
230 	{ HAL_PIXEL_FORMAT_BGRA_8888,              false, MALI_GRALLOC_FORMAT_INTERNAL_BGRA_8888 },
231 	{ HAL_PIXEL_FORMAT_YCbCr_422_SP,           false, MALI_GRALLOC_FORMAT_INTERNAL_NV16 },
232 	{ HAL_PIXEL_FORMAT_YCrCb_420_SP,           false, MALI_GRALLOC_FORMAT_INTERNAL_NV21 },
233 	{ HAL_PIXEL_FORMAT_YCbCr_422_I,            false, MALI_GRALLOC_FORMAT_INTERNAL_YUV422_8BIT },
234 	{ HAL_PIXEL_FORMAT_RGBA_FP16,              false, MALI_GRALLOC_FORMAT_INTERNAL_RGBA_16161616 },
235 	{ HAL_PIXEL_FORMAT_RAW16,                  false, MALI_GRALLOC_FORMAT_INTERNAL_RAW16 },
236 	{ HAL_PIXEL_FORMAT_BLOB,                   false, MALI_GRALLOC_FORMAT_INTERNAL_BLOB },
237 	{ HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, true,  MALI_GRALLOC_FORMAT_INTERNAL_NV12 },
238 	{ HAL_PIXEL_FORMAT_YCbCr_420_888,          true,  MALI_GRALLOC_FORMAT_INTERNAL_NV12 },
239 	{ HAL_PIXEL_FORMAT_RAW_OPAQUE,             false, MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED },
240 	{ HAL_PIXEL_FORMAT_RAW10,                  false, MALI_GRALLOC_FORMAT_INTERNAL_RAW10 },
241 	{ HAL_PIXEL_FORMAT_RAW12,                  false, MALI_GRALLOC_FORMAT_INTERNAL_RAW12 },
242 	{ HAL_PIXEL_FORMAT_YCbCr_422_888,          true,  MALI_GRALLOC_FORMAT_INTERNAL_YUV422_8BIT },
243 	{ HAL_PIXEL_FORMAT_YCbCr_444_888,          true,  MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED },
244 	{ HAL_PIXEL_FORMAT_FLEX_RGB_888,           true,  MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED },
245 	{ HAL_PIXEL_FORMAT_FLEX_RGBA_8888,         true,  MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED },
246 	{ HAL_PIXEL_FORMAT_RGBA_1010102,           false, MALI_GRALLOC_FORMAT_INTERNAL_RGBA_1010102 },
247 	{ HAL_PIXEL_FORMAT_DEPTH_16,               false, MALI_GRALLOC_FORMAT_INTERNAL_DEPTH_16 },
248 	{ HAL_PIXEL_FORMAT_DEPTH_24,               false, MALI_GRALLOC_FORMAT_INTERNAL_DEPTH_24 },
249 	{ HAL_PIXEL_FORMAT_DEPTH_24_STENCIL_8,     false, MALI_GRALLOC_FORMAT_INTERNAL_DEPTH_24_STENCIL_8 },
250 	{ HAL_PIXEL_FORMAT_DEPTH_32F,              false, MALI_GRALLOC_FORMAT_INTERNAL_DEPTH_32F },
251 	{ HAL_PIXEL_FORMAT_DEPTH_32F_STENCIL_8,    false, MALI_GRALLOC_FORMAT_INTERNAL_DEPTH_32F_STENCIL_8 },
252 	{ HAL_PIXEL_FORMAT_STENCIL_8,              false, MALI_GRALLOC_FORMAT_INTERNAL_STENCIL_8 },
253 	{ HAL_PIXEL_FORMAT_YCBCR_P010,             false, MALI_GRALLOC_FORMAT_INTERNAL_P010 },
254 	{ HAL_PIXEL_FORMAT_Y8,                     false, MALI_GRALLOC_FORMAT_INTERNAL_Y8 },
255 	{ HAL_PIXEL_FORMAT_Y16,                    false, MALI_GRALLOC_FORMAT_INTERNAL_Y16 },
256 	{ HAL_PIXEL_FORMAT_YV12,                   false, MALI_GRALLOC_FORMAT_INTERNAL_YV12 },
257 	/* Exynos Formats */
258 	{ HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP,         true, MALI_GRALLOC_FORMAT_INTERNAL_NV12 },
259 	{ HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M,       false, HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M      },
260 	{ HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_FULL,  false, HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_FULL },
261 	{ HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M,       false, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M      },
262 	{ HAL_PIXEL_FORMAT_EXYNOS_YV12_M,               false, HAL_PIXEL_FORMAT_EXYNOS_YV12_M              },
263 	{ HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P_M,        false, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P_M       },
264 	{ HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B,  false, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B },
265 	{ HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_TILED, false, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_TILED},
266 	{ HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN,        false, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN       },
267 	{ HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_S10B,   false, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_S10B  },
268 	{ HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_M,         false, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_M        },
269 	{ HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P,          false, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P         },
270 	/* SBWC Formats */
271 	{ HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC,       false, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC      },
272 	{ HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC,   false, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC  },
273 	{ HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC,      false, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC     },
274 	{ HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC,  false, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC },
275 	{ HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_SBWC,      false, HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_SBWC     },
276 	{ HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_10B_SBWC,  false, HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_10B_SBWC },
277 
278 	{ HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L50,     false, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L50     },
279 	{ HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L75,     false, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L75     },
280 	{ HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L50,      false, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L50      },
281 	{ HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L75,      false, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L75      },
282 	{ HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L40, false, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L40 },
283 	{ HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L60, false, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L60 },
284 	{ HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L80, false, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L80 },
285 	{ HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L40,  false, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L40  },
286 	{ HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L60,  false, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L60  },
287 	{ HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L80,  false, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L80  },
288 
289 	/* Google Formats */
290 	{ HAL_PIXEL_FORMAT_GOOGLE_NV12_SP,      true, HAL_PIXEL_FORMAT_GOOGLE_NV12_SP },
291 	{ HAL_PIXEL_FORMAT_GOOGLE_NV12_SP_10B,  true, HAL_PIXEL_FORMAT_GOOGLE_NV12_SP_10B },
292 	{ HAL_PIXEL_FORMAT_GOOGLE_R_8,          true, HAL_PIXEL_FORMAT_GOOGLE_R_8 },
293 	{ HAL_PIXEL_FORMAT_GOOGLE_RG_88,        true, HAL_PIXEL_FORMAT_GOOGLE_RG_88 },
294 };
295 
296 const size_t num_hal_formats = sizeof(hal_to_internal_format)/sizeof(hal_to_internal_format[0]);
297 
298 
299 /*
300  *  Finds "Look-up Table" index for the given format
301  *
302  * @param base_format [in]   Format for which index is required.
303  *
304  * @return index, when the format is found in the look up table
305  *         -1, otherwise
306  *
307  */
get_format_index(const uint32_t base_format)308 int32_t get_format_index(const uint32_t base_format)
309 {
310 	int32_t format_idx;
311 	for (format_idx = 0; format_idx < (int32_t)num_formats; format_idx++)
312 	{
313 		if (formats[format_idx].id == base_format)
314 		{
315 			break;
316 		}
317 	}
318 	if (format_idx >= (int32_t)num_formats)
319 	{
320 		MALI_GRALLOC_LOGE("ERROR: Format allocation info not found for format: %" PRIx32, base_format);
321 		return -1;
322 	}
323 
324 	return format_idx;
325 }
326 
327 
get_ip_format_index(const uint32_t base_format)328 int32_t get_ip_format_index(const uint32_t base_format)
329 {
330 	int32_t format_idx;
331 	for (format_idx = 0; format_idx < (int32_t)num_ip_formats; format_idx++)
332 	{
333 		if (formats_ip_support[format_idx].id == base_format)
334 		{
335 			break;
336 		}
337 	}
338 	if (format_idx >= (int32_t)num_ip_formats)
339 	{
340 		MALI_GRALLOC_LOGE("ERROR: IP support not found for format: %" PRIx32, base_format);
341 		return -1;
342 	}
343 
344 	return format_idx;
345 }
346 
347 
348 /*
349  * Attempt to map base HAL format to an internal format and
350  * validate format is supported for allocation.
351  *
352  * @param map_to_internal [in]  1: Forces mapping to (and validation of) internal format
353  *                              0: Only maps flex HAL formats to internal
354  *
355  * @return internal_format, where internal format is found
356  *         HAL format, where map_to_internal == 0 and base_format is not flex
357  *         MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED, otherwise
358  *
359  * NOTE: Base format might be either a HAL format or (already) an internal format.
360  *
361  */
get_internal_format(const uint32_t base_format,const bool map_to_internal)362 uint32_t get_internal_format(const uint32_t base_format, const bool map_to_internal)
363 {
364 	uint32_t internal_format = base_format;
365 
366 	for (int idx = 0; idx < (int)num_hal_formats; idx++)
367 	{
368 		if (hal_to_internal_format[idx].hal_format == base_format)
369 		{
370 			if (hal_to_internal_format[idx].is_flex || map_to_internal)
371 			{
372 				internal_format = hal_to_internal_format[idx].internal_format;
373 			}
374 			break;
375 		}
376 	}
377 
378 	/* Ensure internal format is valid when expected. */
379 	if (map_to_internal && get_format_index(internal_format) < 0)
380 	{
381 		internal_format = MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED;
382 	}
383 
384 	return internal_format;
385 }
386 
387 
388 /* Get the dataspace to use based on private usage and format. */
get_format_dataspace(uint32_t base_format,uint64_t usage,int width,int height,android_dataspace_t * dataspace)389 void get_format_dataspace(uint32_t base_format,
390                           uint64_t usage,
391                           int width,
392                           int height,
393                           android_dataspace_t *dataspace)
394 {
395 	GRALLOC_UNUSED(usage);
396 
397 	uint64_t color_space = HAL_DATASPACE_STANDARD_UNSPECIFIED;
398 	uint64_t range = HAL_DATASPACE_RANGE_UNSPECIFIED;
399 	*dataspace = HAL_DATASPACE_UNKNOWN;
400 
401 	const int32_t format_idx = get_format_index(base_format);
402 	if (format_idx < 0)
403 	{
404 		return;
405 	}
406 
407 	/* This resolution is the cut-off point at which BT709 is used (as default)
408 	 * instead of BT601 for YUV formats < 10 bits.
409 	 */
410 	constexpr int yuv_bt601_max_width = 1280;
411 	constexpr int yuv_bt601_max_height = 720;
412 
413 	if (formats[format_idx].is_yuv)
414 	{
415 		/* Default YUV dataspace. */
416 		color_space = HAL_DATASPACE_STANDARD_BT709;
417 		range = HAL_DATASPACE_RANGE_LIMITED;
418 
419 		/* 10-bit YUV is assumed to be wide BT2020.
420 		 */
421 		if (formats[format_idx].bps >= 10)
422 		{
423 			color_space = HAL_DATASPACE_STANDARD_BT2020;
424 			range = HAL_DATASPACE_RANGE_FULL;
425 		}
426 		else if (width < yuv_bt601_max_width || height < yuv_bt601_max_height)
427 		{
428 			color_space = HAL_DATASPACE_STANDARD_BT601_625;
429 			range = HAL_DATASPACE_RANGE_LIMITED;
430 		}
431 
432 		*dataspace = static_cast<android_dataspace_t>(color_space | range);
433 	}
434 	else if (formats[format_idx].is_rgb)
435 	{
436 		/* Default RGB dataspace. Expected by Mapper VTS. */
437 		*dataspace = static_cast<android_dataspace_t>(HAL_DATASPACE_UNKNOWN);
438 	}
439 }
440 
441 
sanitize_formats(void)442 bool sanitize_formats(void)
443 {
444 	bool fail = false;
445 
446 	for (int i = 0; i < (int)num_formats; i++)
447 	{
448 		const format_info_t * format = &formats[i];
449 
450 		/* Identify invalid values. */
451 		if (format->id == 0 ||
452 		    format->npln == 0 || format->npln > 3 ||
453 		    format->total_components() == 0 ||
454 		    format->bps == 0 ||
455 		    format->align_w == 0 ||
456 		    format->align_h == 0 ||
457 		    format->align_w_cpu == 0 ||
458 		    format->tile_size == 0)
459 		{
460 			MALI_GRALLOC_LOGE("Format [id:0x%" PRIx32 "] property zero/out of range (unexpected)", format->id);
461 			fail = true;
462 		}
463 
464 		if (format->is_rgb && format->is_yuv)
465 		{
466 			MALI_GRALLOC_LOGE("Format [id:0x%" PRIx32 "] cannot be both RGB and YUV", format->id);
467 			fail = true;
468 		}
469 
470 		if (format->npln > format->total_components())
471 		{
472 			MALI_GRALLOC_LOGE("Format [id:0x%" PRIx32 "] planes cannot exceed components", format->id);
473 			fail = true;
474 		}
475 
476 		if (format->linear && (format->bps > format->bpp[0]))
477 		{
478 			MALI_GRALLOC_LOGE("Format [id:0x%" PRIx32 "] bpp should be greater than/equal to bps", format->id);
479 			fail = true;
480 		}
481 
482 		if (format->afbc && (format->bps > format->bpp_afbc[0]))
483 		{
484 			MALI_GRALLOC_LOGE("Format [id:0x%" PRIx32 "] bpp_afbc should be greater than/equal to bps", format->id);
485 			fail = true;
486 		}
487 
488 		if (!format->linear && format->tile_size > 1)
489 		{
490 			MALI_GRALLOC_LOGE("Format [id:0x%" PRIx32 "] tile_size must be set to 1 for formats without linear support", format->id);
491 			fail = true;
492 		}
493 
494 		for (int pln = 0; pln < 3; pln++)
495 		{
496 			if (format->linear && (pln < format->npln) && (format->bpp[pln] == 0))
497 			{
498 				MALI_GRALLOC_LOGE("Format [id:0x%" PRIx32 "] does not have bpp defined for plane: %d", format->id, pln);
499 				fail = true;
500 			}
501 			else if (format->linear && (pln >= format->npln) && (format->bpp[pln] != 0))
502 			{
503 				MALI_GRALLOC_LOGE("Format [id:0x%" PRIx32 "] should not have bpp defined for plane: %d", format->id, pln);
504 				fail = true;
505 			}
506 			else if (!format->linear && (format->bpp[pln] != 0))
507 			{
508 				MALI_GRALLOC_LOGE("Format [id:0x%" PRIx32 "] which doesn't support linear should not have bpp defined", format->id);
509 				fail = true;
510 			}
511 
512 			if (format->afbc && (pln < format->npln) && (format->bpp_afbc[pln] == 0))
513 			{
514 				MALI_GRALLOC_LOGE("Format [id:0x%" PRIx32 "] does not have bpp_afbc defined for plane: %d", format->id, pln);
515 				fail = true;
516 			}
517 			else if (format->afbc && (pln >= format->npln) && (format->bpp_afbc[pln] != 0))
518 			{
519 				MALI_GRALLOC_LOGE("Format [id:0x%" PRIx32 "] should not have bpp_afbc defined for plane: %d", format->id, pln);
520 				fail = true;
521 			}
522 			else if (!format->afbc && (format->bpp_afbc[pln] != 0))
523 			{
524 				MALI_GRALLOC_LOGE("Format [id:0x%" PRIx32 "] which doesn't support afbc should not have bpp defined", format->id);
525 				fail = true;
526 			}
527 		}
528 
529 		if (format->is_yuv)
530 		{
531 			if (format->hsub == 0 || format->vsub == 0)
532 			{
533 				MALI_GRALLOC_LOGE("Format [id:0x%" PRIx32 "] hsub and vsub should be non-zero (YUV)", format->id);
534 				fail = true;
535 			}
536 
537 			if (!is_power2(format->hsub) || !is_power2(format->vsub))
538 			{
539 				MALI_GRALLOC_LOGE("Format [id:0x%" PRIx32 "] hsub and vsub should be powers of 2", format->id);
540 				fail = true;
541 			}
542 
543 			if ((format->align_w % format->hsub) != 0)
544 			{
545 				MALI_GRALLOC_LOGE("Format [id:0x%" PRIx32 "] align_w should be a multiple of hsub", format->id);
546 				fail = true;
547 			}
548 
549 			if ((format->align_h % format->vsub) != 0)
550 			{
551 				MALI_GRALLOC_LOGE("Format [id:0x%" PRIx32 "] align_h should be a multiple of vsub", format->id);
552 				fail = true;
553 			}
554 		}
555 		else
556 		{
557 			if (format->hsub != 0 || format->vsub != 0)
558 			{
559 				MALI_GRALLOC_LOGE("Format [id:0x%" PRIx32 "] hsub and vsub should be zero (non-YUV)", format->id);
560 				fail = true;
561 			}
562 		}
563 
564 		if (format->align_w == 0 || format->align_h == 0)
565 		{
566 			MALI_GRALLOC_LOGE("Format [id:0x%" PRIx32 "] align_w and align_h should be non-zero", format->id);
567 			fail = true;
568 		}
569 		else
570 		{
571 			if (!is_power2(format->align_w) || !is_power2(format->align_h))
572 			{
573 				MALI_GRALLOC_LOGE("Format [id:0x%" PRIx32 "] align_w and align_h should be powers of 2", format->id);
574 				fail = true;
575 			}
576 
577 			if (!is_power2(format->align_w_cpu))
578 			{
579 				MALI_GRALLOC_LOGE("Format [id:0x%" PRIx32 "] align_w_cpu should be a power of 2", format->id);
580 				fail = true;
581 			}
582 		}
583 	}
584 
585 	return fail;
586 }
587 
format_name(uint32_t base_format)588 const char *format_name(uint32_t base_format) {
589 	static std::once_flag name_map_flag;
590 	using NameMap = std::unordered_map<uint32_t, const char*>;
591 	static NameMap name_map;
592 
593 	std::call_once(name_map_flag, []() {
594 		for (size_t i = 0; i < num_formats; ++i) {
595 			const format_info_t &format = formats[i];
596 			if (name_map.count(format.id)) {
597 				ALOGE("Format id 0x%" PRIx32 " mapped to %s and to %s",
598 				     format.id, name_map[format.id], format.name);
599 			} else {
600 				name_map[format.id] = format.name;
601 			}
602 		}
603 	});
604 
605 	NameMap::const_iterator i = name_map.find(base_format);
606 	if (i == name_map.end()) {
607 		return "<unrecognized format>";
608 	}
609 	return i->second;
610 }
611