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 #ifndef MALI_GRALLOC_FORMATS_H_
20 #define MALI_GRALLOC_FORMATS_H_
21
22 #include <system/graphics.h>
23
24 #include "mali_gralloc_log.h"
25
26 /* Internal formats are represented in gralloc as a 64bit identifier
27 * where the 32 lower bits are a base format and the 32 upper bits are modifiers.
28 *
29 * Modifier bits are divided into mutually exclusive ones and those that are not.
30 */
31 /* Internal format type */
32 typedef uint64_t mali_gralloc_internal_format;
33
34 /* Internal format masks */
35 #define MALI_GRALLOC_INTFMT_FMT_MASK 0x00000000ffffffffULL
36 #define MALI_GRALLOC_INTFMT_EXT_MASK 0xffffffff00000000ULL
37 #define MALI_GRALLOC_INTFMT_FMT_WRAP_MASK 0x0000ffffULL
38 #define MALI_GRALLOC_INTFMT_EXT_WRAP_MASK 0xffff0000ULL
39 #define MALI_GRALLOC_INTFMT_EXT_WRAP_SHIFT 16
40
41 /* Format Modifier Bits Locations */
42 #define MALI_GRALLOC_INTFMT_EXTENSION_BIT_START 32
43
44 /* Internal base formats */
45
46 /* Base formats that do not have an identical HAL match
47 * are defined starting at the Android private range
48 */
49 #define MALI_GRALLOC_FORMAT_INTERNAL_RANGE_BASE 0x1000
50
51 typedef enum
52 {
53 MALI_GRALLOC_FORMAT_TYPE_USAGE,
54 MALI_GRALLOC_FORMAT_TYPE_INTERNAL,
55 } mali_gralloc_format_type;
56
57 /*
58 * Internal formats defined to either match HAL_PIXEL_FORMAT_* or extend
59 * where missing. Private formats can be used where no CPU usage is requested.
60 * All pixel formats in this list must explicitly define a strict memory
61 * layout which can be allocated and used by producer(s) and consumer(s).
62 * Flex formats are therefore not included and will be mapped to suitable
63 * internal formats.
64 */
65 typedef enum
66 {
67 /* Internal definitions for HAL formats. */
68 MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED = 0,
69 MALI_GRALLOC_FORMAT_INTERNAL_RGBA_8888 = HAL_PIXEL_FORMAT_RGBA_8888,
70 MALI_GRALLOC_FORMAT_INTERNAL_RGBX_8888 = HAL_PIXEL_FORMAT_RGBX_8888,
71 MALI_GRALLOC_FORMAT_INTERNAL_RGB_888 = HAL_PIXEL_FORMAT_RGB_888,
72 MALI_GRALLOC_FORMAT_INTERNAL_RGB_565 = HAL_PIXEL_FORMAT_RGB_565,
73 MALI_GRALLOC_FORMAT_INTERNAL_BGRA_8888 = HAL_PIXEL_FORMAT_BGRA_8888,
74 MALI_GRALLOC_FORMAT_INTERNAL_RGBA_1010102 = HAL_PIXEL_FORMAT_RGBA_1010102,
75 /* 16-bit floating point format. */
76 MALI_GRALLOC_FORMAT_INTERNAL_RGBA_16161616 = HAL_PIXEL_FORMAT_RGBA_FP16,
77 MALI_GRALLOC_FORMAT_INTERNAL_YV12 = HAL_PIXEL_FORMAT_YV12,
78 MALI_GRALLOC_FORMAT_INTERNAL_Y8 = HAL_PIXEL_FORMAT_Y8,
79 MALI_GRALLOC_FORMAT_INTERNAL_Y16 = HAL_PIXEL_FORMAT_Y16,
80 MALI_GRALLOC_FORMAT_INTERNAL_NV16 = HAL_PIXEL_FORMAT_YCbCr_422_SP,
81
82 /* Camera specific HAL formats */
83 MALI_GRALLOC_FORMAT_INTERNAL_RAW16 = HAL_PIXEL_FORMAT_RAW16,
84 MALI_GRALLOC_FORMAT_INTERNAL_RAW12 = HAL_PIXEL_FORMAT_RAW12,
85 MALI_GRALLOC_FORMAT_INTERNAL_RAW10 = HAL_PIXEL_FORMAT_RAW10,
86 MALI_GRALLOC_FORMAT_INTERNAL_RAW_OPAQUE = HAL_PIXEL_FORMAT_RAW_OPAQUE,
87 MALI_GRALLOC_FORMAT_INTERNAL_BLOB = HAL_PIXEL_FORMAT_BLOB,
88
89 /* Depth and stencil formats */
90 MALI_GRALLOC_FORMAT_INTERNAL_DEPTH_16 = HAL_PIXEL_FORMAT_DEPTH_16,
91 MALI_GRALLOC_FORMAT_INTERNAL_DEPTH_24 = HAL_PIXEL_FORMAT_DEPTH_24,
92 MALI_GRALLOC_FORMAT_INTERNAL_DEPTH_24_STENCIL_8 = HAL_PIXEL_FORMAT_DEPTH_24_STENCIL_8,
93 MALI_GRALLOC_FORMAT_INTERNAL_DEPTH_32F = HAL_PIXEL_FORMAT_DEPTH_32F,
94 MALI_GRALLOC_FORMAT_INTERNAL_DEPTH_32F_STENCIL_8 = HAL_PIXEL_FORMAT_DEPTH_32F_STENCIL_8,
95 MALI_GRALLOC_FORMAT_INTERNAL_STENCIL_8 = HAL_PIXEL_FORMAT_STENCIL_8,
96
97 /* Flexible YUV formats would be parsed but not have any representation as
98 * internal format itself but one of the ones below.
99 */
100
101 /* The internal private formats that have no HAL equivivalent are defined
102 * afterwards starting at a specific base range.
103 */
104 MALI_GRALLOC_FORMAT_INTERNAL_NV12 = MALI_GRALLOC_FORMAT_INTERNAL_RANGE_BASE,
105 MALI_GRALLOC_FORMAT_INTERNAL_NV21,
106 MALI_GRALLOC_FORMAT_INTERNAL_YUV422_8BIT,
107
108 /* Extended YUV formats. */
109 MALI_GRALLOC_FORMAT_INTERNAL_Y0L2,
110 MALI_GRALLOC_FORMAT_INTERNAL_P010,
111 MALI_GRALLOC_FORMAT_INTERNAL_P210,
112 MALI_GRALLOC_FORMAT_INTERNAL_Y210,
113 MALI_GRALLOC_FORMAT_INTERNAL_Y410,
114
115 /*
116 * Single-plane (I = interleaved) variants of 8/10-bit YUV formats,
117 * where previously not defined.
118 */
119 MALI_GRALLOC_FORMAT_INTERNAL_YUV420_8BIT_I,
120 MALI_GRALLOC_FORMAT_INTERNAL_YUV420_10BIT_I,
121 MALI_GRALLOC_FORMAT_INTERNAL_YUV444_10BIT_I,
122
123 /* Add more internal formats here. */
124
125 /* These are legacy 0.3 gralloc formats used only by the wrap/unwrap macros. */
126 MALI_GRALLOC_FORMAT_INTERNAL_YV12_WRAP,
127 MALI_GRALLOC_FORMAT_INTERNAL_Y8_WRAP,
128 MALI_GRALLOC_FORMAT_INTERNAL_Y16_WRAP,
129
130 MALI_GRALLOC_FORMAT_INTERNAL_RANGE_LAST,
131 } mali_gralloc_pixel_format;
132
133 /*
134 * Compression type
135 */
136
137 /* This format will use AFBC */
138 #define MALI_GRALLOC_INTFMT_AFBC_BASIC (1ULL << (MALI_GRALLOC_INTFMT_EXTENSION_BIT_START + 0))
139
140
141 /*
142 * AFBC modifier bits (valid with MALI_GRALLOC_INTFMT_AFBC_BASIC)
143 */
144
145 /* This format uses AFBC split block mode */
146 #define MALI_GRALLOC_INTFMT_AFBC_SPLITBLK (1ULL << (MALI_GRALLOC_INTFMT_EXTENSION_BIT_START + 2))
147
148 /* This format uses AFBC wide block mode */
149 #define MALI_GRALLOC_INTFMT_AFBC_WIDEBLK (1ULL << (MALI_GRALLOC_INTFMT_EXTENSION_BIT_START + 3))
150
151 /* This format uses AFBC tiled headers */
152 #define MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS (1ULL << (MALI_GRALLOC_INTFMT_EXTENSION_BIT_START + 4))
153
154 /* This format uses AFBC extra wide superblocks. */
155 #define MALI_GRALLOC_INTFMT_AFBC_EXTRAWIDEBLK (1ULL << (MALI_GRALLOC_INTFMT_EXTENSION_BIT_START + 5))
156
157 /* This format is AFBC with double body buffer (used as a frontbuffer) */
158 #define MALI_GRALLOC_INTFMT_AFBC_DOUBLE_BODY (1ULL << (MALI_GRALLOC_INTFMT_EXTENSION_BIT_START + 6))
159
160 /* This format uses AFBC buffer content hints in LSB of superblock offset. */
161 #define MALI_GRALLOC_INTFMT_AFBC_BCH (1ULL << (MALI_GRALLOC_INTFMT_EXTENSION_BIT_START + 7))
162
163 /* This format uses AFBC with YUV transform. */
164 #define MALI_GRALLOC_INTFMT_AFBC_YUV_TRANSFORM (1ULL << (MALI_GRALLOC_INTFMT_EXTENSION_BIT_START + 8))
165
166 /* This format uses Sparse allocated AFBC. */
167 #define MALI_GRALLOC_INTFMT_AFBC_SPARSE (1ULL << (MALI_GRALLOC_INTFMT_EXTENSION_BIT_START + 9))
168
169 /* This mask should be used to check or clear support for AFBC for an internal format.
170 */
171 #define MALI_GRALLOC_INTFMT_AFBCENABLE_MASK (uint64_t)(MALI_GRALLOC_INTFMT_AFBC_BASIC)
172
173
174 /* These are legacy Gralloc 0.3 support macros for passing private formats through the 0.3 alloc interface.
175 * It packs modifier bits together with base format into a 32 bit format identifier.
176 * Gralloc 1.0 interface should use private functions to set private buffer format in the buffer descriptor.
177 *
178 * Packing:
179 *
180 * Bits 15-0: mali_gralloc_pixel_format format
181 * Bits 31-16: modifier bits
182 */
mali_gralloc_format_wrapper(int format,int modifiers)183 static inline int mali_gralloc_format_wrapper(int format, int modifiers)
184 {
185 /* Internal formats that are identical to HAL formats
186 * have the same definition. This is convenient for
187 * client parsing code to not have to parse them separately.
188 *
189 * For 3 of the HAL YUV formats that have very large definitions
190 * this causes problems for packing in modifier bits.
191 * Because of this reason we redefine these three formats
192 * while packing/unpacking them.
193 */
194 if (format == MALI_GRALLOC_FORMAT_INTERNAL_YV12)
195 {
196 format = MALI_GRALLOC_FORMAT_INTERNAL_YV12_WRAP;
197 }
198 else if (format == MALI_GRALLOC_FORMAT_INTERNAL_Y8)
199 {
200 format = MALI_GRALLOC_FORMAT_INTERNAL_Y8_WRAP;
201 }
202 else if (format == MALI_GRALLOC_FORMAT_INTERNAL_Y16)
203 {
204 format = MALI_GRALLOC_FORMAT_INTERNAL_Y16_WRAP;
205 }
206
207 if (format & ~MALI_GRALLOC_INTFMT_FMT_WRAP_MASK)
208 {
209 format = MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED;
210 MALI_GRALLOC_LOGE("Format is too large for private format wrapping");
211 }
212
213 return (modifiers | format);
214 }
215
mali_gralloc_format_unwrap(int x)216 static inline uint64_t mali_gralloc_format_unwrap(int x)
217 {
218 uint64_t internal_format = (uint64_t)(((((uint64_t)(x)) & MALI_GRALLOC_INTFMT_EXT_WRAP_MASK) << MALI_GRALLOC_INTFMT_EXT_WRAP_SHIFT) | // Modifier bits
219 (((uint64_t)(x)) & MALI_GRALLOC_INTFMT_FMT_WRAP_MASK)); // Private format
220
221 uint64_t base_format = internal_format & MALI_GRALLOC_INTFMT_FMT_MASK;
222 uint64_t modifiers = internal_format & MALI_GRALLOC_INTFMT_EXT_MASK;
223
224 if (base_format == MALI_GRALLOC_FORMAT_INTERNAL_YV12_WRAP)
225 {
226 base_format = MALI_GRALLOC_FORMAT_INTERNAL_YV12;
227 }
228 else if (base_format == MALI_GRALLOC_FORMAT_INTERNAL_Y8_WRAP)
229 {
230 base_format = MALI_GRALLOC_FORMAT_INTERNAL_Y8;
231 }
232 else if (base_format == MALI_GRALLOC_FORMAT_INTERNAL_Y16_WRAP)
233 {
234 base_format = MALI_GRALLOC_FORMAT_INTERNAL_Y16;
235 }
236
237 return (modifiers | base_format);
238 }
239
240 /*
241 * Macro to add additional modifier(s) to existing wrapped private format.
242 * Arguments include wrapped private format and new modifier(s) to add.
243 */
244 #define GRALLOC_PRIVATE_FORMAT_WRAPPER_ADD_MODIFIER(x, modifiers) \
245 ((int)((x) | ((unsigned)((modifiers) >> MALI_GRALLOC_INTFMT_EXT_WRAP_SHIFT))))
246
247 /*
248 * Macro to remove modifier(s) to existing wrapped private format.
249 * Arguments include wrapped private format and modifier(s) to remove.
250 */
251 #define GRALLOC_PRIVATE_FORMAT_WRAPPER_REMOVE_MODIFIER(x, modifiers) \
252 ((int)((x) & ~((unsigned)((modifiers) >> MALI_GRALLOC_INTFMT_EXT_WRAP_SHIFT))))
253
254 #define GRALLOC_PRIVATE_FORMAT_WRAPPER(x) (mali_gralloc_format_wrapper(x, 0))
255
256 #define GRALLOC_PRIVATE_FORMAT_WRAPPER_AFBC(x) \
257 mali_gralloc_format_wrapper(x, (MALI_GRALLOC_INTFMT_AFBC_BASIC | MALI_GRALLOC_INTFMT_AFBC_SPARSE) >> \
258 MALI_GRALLOC_INTFMT_EXT_WRAP_SHIFT)
259
260 #define GRALLOC_PRIVATE_FORMAT_WRAPPER_AFBC_SPLITBLK(x) \
261 GRALLOC_PRIVATE_FORMAT_WRAPPER_ADD_MODIFIER(GRALLOC_PRIVATE_FORMAT_WRAPPER_AFBC(x), \
262 MALI_GRALLOC_INTFMT_AFBC_SPLITBLK)
263
264 #define GRALLOC_PRIVATE_FORMAT_WRAPPER_AFBC_WIDEBLK(x) \
265 GRALLOC_PRIVATE_FORMAT_WRAPPER_ADD_MODIFIER(GRALLOC_PRIVATE_FORMAT_WRAPPER_AFBC(x), \
266 MALI_GRALLOC_INTFMT_AFBC_WIDEBLK)
267
268 #define GRALLOC_PRIVATE_FORMAT_WRAPPER_AFBC_WIDE_SPLIT(x) \
269 GRALLOC_PRIVATE_FORMAT_WRAPPER_ADD_MODIFIER(GRALLOC_PRIVATE_FORMAT_WRAPPER_AFBC_SPLITBLK(x), \
270 MALI_GRALLOC_INTFMT_AFBC_WIDEBLK)
271
272 #define GRALLOC_PRIVATE_FORMAT_WRAPPER_AFBC_TILED_HEADERS_BASIC(x) \
273 GRALLOC_PRIVATE_FORMAT_WRAPPER_ADD_MODIFIER(GRALLOC_PRIVATE_FORMAT_WRAPPER_AFBC(x), \
274 MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS)
275
276 #define GRALLOC_PRIVATE_FORMAT_WRAPPER_AFBC_TILED_HEADERS_WIDE(x) \
277 GRALLOC_PRIVATE_FORMAT_WRAPPER_ADD_MODIFIER(GRALLOC_PRIVATE_FORMAT_WRAPPER_AFBC_WIDEBLK(x), \
278 MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS)
279
280 #define GRALLOC_PRIVATE_FORMAT_WRAPPER_AFBC_TILED_HEADERS_SPLIT(x) \
281 GRALLOC_PRIVATE_FORMAT_WRAPPER_ADD_MODIFIER(GRALLOC_PRIVATE_FORMAT_WRAPPER_AFBC_SPLITBLK(x), \
282 MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS)
283
284 #define GRALLOC_PRIVATE_FORMAT_WRAPPER_AFBC_TILED_HEADERS_WIDE_SPLIT(x) \
285 GRALLOC_PRIVATE_FORMAT_WRAPPER_ADD_MODIFIER(GRALLOC_PRIVATE_FORMAT_WRAPPER_AFBC_WIDE_SPLIT(x), \
286 MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS)
287
288 /*
289 * AFBC format with extra-wide (64x4) superblocks.
290 *
291 * NOTE: Tiled headers are mandatory for this format.
292 */
293 #define GRALLOC_PRIVATE_FORMAT_WRAPPER_AFBC_EXTRAWIDEBLK(x) \
294 GRALLOC_PRIVATE_FORMAT_WRAPPER_ADD_MODIFIER(GRALLOC_PRIVATE_FORMAT_WRAPPER_AFBC_TILED_HEADERS_BASIC(x), \
295 MALI_GRALLOC_INTFMT_AFBC_EXTRAWIDEBLK)
296
297 /*
298 * AFBC multi-plane YUV format where luma (wide, 32x8) and
299 * chroma (extra-wide, 64x4) planes are stored in separate AFBC buffers.
300 *
301 * NOTE: Tiled headers are mandatory for this format.
302 * NOTE: Base format (x) must be a multi-plane YUV format.
303 */
304 #define GRALLOC_PRIVATE_FORMAT_WRAPPER_AFBC_WIDE_EXTRAWIDE(x) \
305 GRALLOC_PRIVATE_FORMAT_WRAPPER_ADD_MODIFIER(GRALLOC_PRIVATE_FORMAT_WRAPPER_AFBC_EXTRAWIDEBLK(x), \
306 MALI_GRALLOC_INTFMT_AFBC_WIDEBLK)
307
308 #define GRALLOC_PRIVATE_FORMAT_WRAPPER_AFBC_TILED_DOUBLE_BODY(x) \
309 GRALLOC_PRIVATE_FORMAT_WRAPPER_ADD_MODIFIER(GRALLOC_PRIVATE_FORMAT_WRAPPER_AFBC_TILED_HEADERS_BASIC(x), \
310 MALI_GRALLOC_INTFMT_AFBC_DOUBLE_BODY)
311
312 #define GRALLOC_PRIVATE_FORMAT_WRAPPER_AFBC_TILED_SPLIT_DOUBLE_BODY(x) \
313 GRALLOC_PRIVATE_FORMAT_WRAPPER_ADD_MODIFIER(GRALLOC_PRIVATE_FORMAT_WRAPPER_AFBC_TILED_HEADERS_SPLIT(x), \
314 MALI_GRALLOC_INTFMT_AFBC_DOUBLE_BODY)
315
316 #define GRALLOC_PRIVATE_FORMAT_UNWRAP(x) mali_gralloc_format_unwrap(x)
317
318 /* IP block capability masks */
319 #define MALI_GRALLOC_FORMAT_CAPABILITY_OPTIONS_PRESENT ((uint64_t)1 << 0)
320
321 /* For IPs which can't read/write YUV with AFBC encoding use flag AFBC_YUV_READ/AFBC_YUV_WRITE */
322 #define MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC ((uint64_t)1 << 1)
323 #define MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK ((uint64_t)1 << 2)
324 #define MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_WIDEBLK ((uint64_t)1 << 3)
325 #define MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_WRITE_NON_SPARSE ((uint64_t)1 << 4)
326 #define MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_RESERVED_2 ((uint64_t)1 << 5)
327 #define MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_RESERVED_3 ((uint64_t)1 << 6)
328 #define MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_TILED_HEADERS ((uint64_t)1 << 7)
329 #define MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_EXTRAWIDEBLK ((uint64_t)1 << 8)
330 #define MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_MULTIPLANE_READ ((uint64_t)1 << 9)
331 #define MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_DOUBLE_BODY ((uint64_t)1 << 10)
332 #define MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_YUV_READ ((uint64_t)1 << 11)
333 #define MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_YUV_WRITE ((uint64_t)1 << 12)
334 #define MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_RGBA16161616 ((uint64_t)1 << 13)
335
336
337 #define MALI_GRALLOC_FORMAT_CAPABILITY_PIXFMT_RGBA1010102 ((uint64_t)1 << 32)
338 #define MALI_GRALLOC_FORMAT_CAPABILITY_PIXFMT_RGBA16161616 ((uint64_t)1 << 33)
339
340 #define MALI_GRALLOC_FORMAT_CAPABILITY_AFBCENABLE_MASK \
341 ((uint64_t)(MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_BASIC | MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_SPLITBLK | \
342 MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_WIDEBLK | MALI_GRALLOC_FORMAT_CAPABILITY_AFBC_TILED_HEADERS))
343
344 struct mali_gralloc_format_caps
345 {
346 uint64_t caps_mask = 0;
347 };
348 typedef struct mali_gralloc_format_caps mali_gralloc_format_caps;
349
350 #define MALI_GRALLOC_FORMATCAPS_SYM_NAME mali_gralloc_format_capabilities
351 #define MALI_GRALLOC_FORMATCAPS_SYM_NAME_STR "mali_gralloc_format_capabilities"
352
353 /* Internal prototypes */
354 #if defined(GRALLOC_LIBRARY_BUILD)
355
356 void mali_gralloc_adjust_dimensions(const uint64_t internal_format,
357 const uint64_t usage,
358 int* const width,
359 int* const height);
360
361 uint64_t mali_gralloc_select_format(const uint64_t req_format,
362 const mali_gralloc_format_type type,
363 const uint64_t usage,
364 const int buffer_size);
365
366 bool is_subsampled_yuv(const uint32_t base_format);
367 #endif
368
369 bool is_exynos_format(uint32_t base_format);
370
371 #endif /* MALI_GRALLOC_FORMATS_H_ */
372