blob: 0efc64edda245bea1d15ecc417b66545c7acea1b [file] [log] [blame]
John Stultz16100f62017-05-03 11:12:18 -07001/*
John Stultz18814f62018-02-22 16:02:49 -08002 * Copyright (C) 2016-2017 ARM Limited. All rights reserved.
John Stultz16100f62017-05-03 11:12:18 -07003 *
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
John Stultz16100f62017-05-03 11:12:18 -070019#include <hardware/hardware.h>
John Stultz18814f62018-02-22 16:02:49 -080020#include <inttypes.h>
21#include <atomic>
22
23#if GRALLOC_USE_GRALLOC1_API == 1
24#include <hardware/gralloc1.h>
25#else
John Stultz16100f62017-05-03 11:12:18 -070026#include <hardware/gralloc.h>
John Stultz18814f62018-02-22 16:02:49 -080027#endif
John Stultz16100f62017-05-03 11:12:18 -070028
John Stultz18814f62018-02-22 16:02:49 -080029#include "mali_gralloc_module.h"
30#include "mali_gralloc_bufferallocation.h"
31#include "mali_gralloc_ion.h"
32#include "mali_gralloc_private_interface_types.h"
33#include "mali_gralloc_buffer.h"
John Stultz16100f62017-05-03 11:12:18 -070034#include "gralloc_buffer_priv.h"
John Stultz18814f62018-02-22 16:02:49 -080035#include "mali_gralloc_bufferdescriptor.h"
36#include "mali_gralloc_debug.h"
John Stultz16100f62017-05-03 11:12:18 -070037
John Stultz18814f62018-02-22 16:02:49 -080038#define AFBC_PIXELS_PER_BLOCK 16
39#define AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY 16
John Stultz16100f62017-05-03 11:12:18 -070040
John Stultz18814f62018-02-22 16:02:49 -080041#define AFBC_BODY_BUFFER_BYTE_ALIGNMENT 1024
42#define AFBC_NORMAL_WIDTH_ALIGN 16
43#define AFBC_NORMAL_HEIGHT_ALIGN 16
44#define AFBC_WIDEBLK_WIDTH_ALIGN 32
45#define AFBC_WIDEBLK_HEIGHT_ALIGN 16
John Stultz16100f62017-05-03 11:12:18 -070046// Regarding Tiled Headers AFBC mode, both header and body buffer should aligned to 4KB
47// and in non-wide mode (16x16), the width and height should be both rounded up to 128
48// in wide mode (32x8) the width should be rounded up to 256, the height should be rounded up to 64
John Stultz18814f62018-02-22 16:02:49 -080049#define AFBC_TILED_HEADERS_BASIC_WIDTH_ALIGN 128
50#define AFBC_TILED_HEADERS_BASIC_HEIGHT_ALIGN 128
51#define AFBC_TILED_HEADERS_WIDEBLK_WIDTH_ALIGN 256
52#define AFBC_TILED_HEADERS_WIDEBLK_HEIGHT_ALIGN 64
John Stultz16100f62017-05-03 11:12:18 -070053
54// This value is platform specific and should be set according to hardware YUV planes restrictions.
55// Please note that EGL winsys platform config file needs to use the same value when importing buffers.
56#define YUV_MALI_PLANE_ALIGN 128
57
58// Default YUV stride aligment in Android
59#define YUV_ANDROID_PLANE_ALIGN 16
60
John Stultz16100f62017-05-03 11:12:18 -070061/*
62 * Type of allocation
63 */
64enum AllocType
65{
66 UNCOMPRESSED = 0,
67 AFBC,
68 /* AFBC_WIDEBLK mode requires buffer to have 32 * 16 pixels alignment */
69 AFBC_WIDEBLK,
70 /* AN AFBC buffer with additional padding to ensure a 64-bte alignment
71 * for each row of blocks in the header */
72 AFBC_PADDED,
73 /* AFBC_TILED_HEADERS_AFBC_BASIC mode requires buffer to have 128*128 pixels alignment(16x16 superblocks) */
74 AFBC_TILED_HEADERS_BASIC,
75 /* AFBC_TILED_HEADERS_AFBC_WIDEBLK mode requires buffer to have 256*64 pixels alignment(32x8 superblocks) */
76 AFBC_TILED_HEADERS_WIDEBLK,
77};
78
John Stultz18814f62018-02-22 16:02:49 -080079static int mali_gralloc_buffer_free_internal(buffer_handle_t *pHandle, uint32_t num_hnds);
80
81/*
82 * Get a global unique ID
83 */
84static uint64_t getUniqueId()
85{
86 static std::atomic<uint32_t> counter(0);
87 uint64_t id = static_cast<uint64_t>(getpid()) << 32;
88 return id | counter++;
89}
90
John Stultz16100f62017-05-03 11:12:18 -070091/*
92 * Computes the strides and size for an RGB buffer
93 *
94 * width width of the buffer in pixels
95 * height height of the buffer in pixels
96 * pixel_size size of one pixel in bytes
97 *
98 * pixel_stride (out) stride of the buffer in pixels
99 * byte_stride (out) stride of the buffer in bytes
100 * size (out) size of the buffer in bytes
101 * type (in) if buffer should be allocated for afbc
102 */
John Stultz18814f62018-02-22 16:02:49 -0800103static void get_rgb_stride_and_size(int width, int height, int pixel_size, int *pixel_stride, int *byte_stride,
104 size_t *size, AllocType type)
John Stultz16100f62017-05-03 11:12:18 -0700105{
106 int stride;
107
108 stride = width * pixel_size;
109
110 /* Align the lines to 64 bytes.
111 * It's more efficient to write to 64-byte aligned addresses because it's the burst size on the bus */
112 stride = GRALLOC_ALIGN(stride, 64);
113
114 if (size != NULL)
115 {
John Stultz18814f62018-02-22 16:02:49 -0800116 *size = stride *height;
John Stultz16100f62017-05-03 11:12:18 -0700117 }
118
119 if (byte_stride != NULL)
120 {
121 *byte_stride = stride;
122 }
123
124 if (pixel_stride != NULL)
125 {
126 *pixel_stride = stride / pixel_size;
127 }
128
129 if (type != UNCOMPRESSED)
130 {
131 int w_aligned;
John Stultz18814f62018-02-22 16:02:49 -0800132 int h_aligned = GRALLOC_ALIGN(height, AFBC_NORMAL_HEIGHT_ALIGN);
John Stultz16100f62017-05-03 11:12:18 -0700133 int nblocks;
134 int buffer_byte_alignment = AFBC_BODY_BUFFER_BYTE_ALIGNMENT;
135
136 if (type == AFBC_TILED_HEADERS_BASIC)
137 {
John Stultz18814f62018-02-22 16:02:49 -0800138 w_aligned = GRALLOC_ALIGN(width, AFBC_TILED_HEADERS_BASIC_WIDTH_ALIGN);
139 h_aligned = GRALLOC_ALIGN(height, AFBC_TILED_HEADERS_BASIC_HEIGHT_ALIGN);
John Stultz16100f62017-05-03 11:12:18 -0700140 buffer_byte_alignment = 4 * AFBC_BODY_BUFFER_BYTE_ALIGNMENT;
141 }
142 else if (type == AFBC_TILED_HEADERS_WIDEBLK)
143 {
John Stultz18814f62018-02-22 16:02:49 -0800144 w_aligned = GRALLOC_ALIGN(width, AFBC_TILED_HEADERS_WIDEBLK_WIDTH_ALIGN);
145 h_aligned = GRALLOC_ALIGN(height, AFBC_TILED_HEADERS_WIDEBLK_HEIGHT_ALIGN);
John Stultz16100f62017-05-03 11:12:18 -0700146 buffer_byte_alignment = 4 * AFBC_BODY_BUFFER_BYTE_ALIGNMENT;
147 }
148 else if (type == AFBC_PADDED)
149 {
John Stultz18814f62018-02-22 16:02:49 -0800150 w_aligned = GRALLOC_ALIGN(width, 64);
John Stultz16100f62017-05-03 11:12:18 -0700151 }
152 else if (type == AFBC_WIDEBLK)
153 {
John Stultz18814f62018-02-22 16:02:49 -0800154 w_aligned = GRALLOC_ALIGN(width, AFBC_WIDEBLK_WIDTH_ALIGN);
155 h_aligned = GRALLOC_ALIGN(height, AFBC_WIDEBLK_HEIGHT_ALIGN);
John Stultz16100f62017-05-03 11:12:18 -0700156 }
157 else
158 {
John Stultz18814f62018-02-22 16:02:49 -0800159 w_aligned = GRALLOC_ALIGN(width, AFBC_NORMAL_WIDTH_ALIGN);
160 }
161
162 stride = w_aligned * pixel_size;
163 stride = GRALLOC_ALIGN(stride, 64);
164
165 if (byte_stride != NULL)
166 {
167 *byte_stride = stride;
168 }
169
170 if (pixel_stride != NULL)
171 {
172 *pixel_stride = stride / pixel_size;
John Stultz16100f62017-05-03 11:12:18 -0700173 }
174
175 nblocks = w_aligned / AFBC_PIXELS_PER_BLOCK * h_aligned / AFBC_PIXELS_PER_BLOCK;
176
John Stultz18814f62018-02-22 16:02:49 -0800177 if (size != NULL)
John Stultz16100f62017-05-03 11:12:18 -0700178 {
John Stultz18814f62018-02-22 16:02:49 -0800179 *size = stride *h_aligned +
180 GRALLOC_ALIGN(nblocks * AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY, buffer_byte_alignment);
John Stultz16100f62017-05-03 11:12:18 -0700181 }
182 }
183}
184
185/*
186 * Computes the strides and size for an AFBC 8BIT YUV 4:2:0 buffer
187 *
188 * width Public known width of the buffer in pixels
189 * height Public known height of the buffer in pixels
190 *
191 * pixel_stride (out) stride of the buffer in pixels
192 * byte_stride (out) stride of the buffer in bytes
193 * size (out) size of the buffer in bytes
194 * type if buffer should be allocated for a certain afbc type
195 * internalHeight (out) The internal height, which may be greater than the public known height.
196 */
John Stultz18814f62018-02-22 16:02:49 -0800197static bool get_afbc_yuv420_8bit_stride_and_size(int width, int height, int *pixel_stride, int *byte_stride,
198 size_t *size, AllocType type, int *internalHeight)
John Stultz16100f62017-05-03 11:12:18 -0700199{
200 int yuv420_afbc_luma_stride, yuv420_afbc_chroma_stride;
201 int buffer_byte_alignment = AFBC_BODY_BUFFER_BYTE_ALIGNMENT;
202
203 *internalHeight = height;
204
205#if MALI_VIDEO_VERSION != 0
206
207 /* If we have a greater internal height than public we set the internalHeight. This
208 * implies that cropping will be applied of internal dimensions to fit the public one.
209 *
210 * NOTE: This should really only be done when the producer is determined to be VPU decoder.
211 */
212 *internalHeight += AFBC_PIXELS_PER_BLOCK;
213#endif
214
215 /* The actual height used in size calculation must include the possible extra row. But
216 * it must also be AFBC-aligned. Only the extra row-padding should be reported back in
217 * internalHeight. This as only this row needs to be considered when cropping. */
218
219 if (type == UNCOMPRESSED)
220 {
221 AERR(" Buffer must be allocated with AFBC mode for internal pixel format YUV420_8BIT_AFBC!");
222 return false;
223 }
224 else if (type == AFBC_TILED_HEADERS_BASIC)
225 {
John Stultz18814f62018-02-22 16:02:49 -0800226 width = GRALLOC_ALIGN(width, AFBC_TILED_HEADERS_BASIC_WIDTH_ALIGN);
227 height = GRALLOC_ALIGN(*internalHeight, AFBC_TILED_HEADERS_BASIC_HEIGHT_ALIGN);
John Stultz16100f62017-05-03 11:12:18 -0700228 buffer_byte_alignment = 4 * AFBC_BODY_BUFFER_BYTE_ALIGNMENT;
229 }
230 else if (type == AFBC_TILED_HEADERS_WIDEBLK)
231 {
John Stultz18814f62018-02-22 16:02:49 -0800232 width = GRALLOC_ALIGN(width, AFBC_TILED_HEADERS_WIDEBLK_WIDTH_ALIGN);
233 height = GRALLOC_ALIGN(*internalHeight, AFBC_TILED_HEADERS_WIDEBLK_HEIGHT_ALIGN);
John Stultz16100f62017-05-03 11:12:18 -0700234 buffer_byte_alignment = 4 * AFBC_BODY_BUFFER_BYTE_ALIGNMENT;
235 }
236 else if (type == AFBC_PADDED)
237 {
238 AERR("GRALLOC_USAGE_PRIVATE_2 (64byte header row alignment for AFBC) is not supported for YUV");
239 return false;
240 }
241 else if (type == AFBC_WIDEBLK)
242 {
243 width = GRALLOC_ALIGN(width, AFBC_WIDEBLK_WIDTH_ALIGN);
John Stultz18814f62018-02-22 16:02:49 -0800244 height = GRALLOC_ALIGN(*internalHeight, AFBC_WIDEBLK_HEIGHT_ALIGN);
John Stultz16100f62017-05-03 11:12:18 -0700245 }
246 else
247 {
248 width = GRALLOC_ALIGN(width, AFBC_NORMAL_WIDTH_ALIGN);
John Stultz18814f62018-02-22 16:02:49 -0800249 height = GRALLOC_ALIGN(*internalHeight, AFBC_NORMAL_HEIGHT_ALIGN);
John Stultz16100f62017-05-03 11:12:18 -0700250 }
251
252 yuv420_afbc_luma_stride = width;
253 yuv420_afbc_chroma_stride = GRALLOC_ALIGN(yuv420_afbc_luma_stride / 2, 16); /* Horizontal downsampling*/
254
255 if (size != NULL)
256 {
257 int nblocks = width / AFBC_PIXELS_PER_BLOCK * height / AFBC_PIXELS_PER_BLOCK;
258 /* Simplification of (height * luma-stride + 2 * (height /2 * chroma_stride) */
John Stultz18814f62018-02-22 16:02:49 -0800259 *size = (yuv420_afbc_luma_stride + yuv420_afbc_chroma_stride) * height +
260 GRALLOC_ALIGN(nblocks * AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY, buffer_byte_alignment);
John Stultz16100f62017-05-03 11:12:18 -0700261 }
262
263 if (byte_stride != NULL)
264 {
265 *byte_stride = yuv420_afbc_luma_stride;
266 }
267
268 if (pixel_stride != NULL)
269 {
270 *pixel_stride = yuv420_afbc_luma_stride;
271 }
272
273 return true;
274}
275
276/*
277 * Computes the strides and size for an YV12 buffer
278 *
279 * width Public known width of the buffer in pixels
280 * height Public known height of the buffer in pixels
281 *
282 * pixel_stride (out) stride of the buffer in pixels
283 * byte_stride (out) stride of the buffer in bytes
284 * size (out) size of the buffer in bytes
285 * type (in) if buffer should be allocated for a certain afbc type
286 * internalHeight (out) The internal height, which may be greater than the public known height.
287 * stride_alignment (in) stride aligment value in bytes.
288 */
John Stultz18814f62018-02-22 16:02:49 -0800289static bool get_yv12_stride_and_size(int width, int height, int *pixel_stride, int *byte_stride, size_t *size,
290 AllocType type, int *internalHeight, int stride_alignment)
John Stultz16100f62017-05-03 11:12:18 -0700291{
292 int luma_stride;
293
294 if (type != UNCOMPRESSED)
295 {
John Stultz18814f62018-02-22 16:02:49 -0800296 return get_afbc_yuv420_8bit_stride_and_size(width, height, pixel_stride, byte_stride, size, type,
297 internalHeight);
John Stultz16100f62017-05-03 11:12:18 -0700298 }
299
300 /* 4:2:0 formats must have buffers with even height and width as the clump size is 2x2 pixels.
301 * Width will be even stride aligned anyway so just adjust height here for size calculation. */
302 height = GRALLOC_ALIGN(height, 2);
303
304 luma_stride = GRALLOC_ALIGN(width, stride_alignment);
305
306 if (size != NULL)
307 {
308 int chroma_stride = GRALLOC_ALIGN(luma_stride / 2, stride_alignment);
309 /* Simplification of ((height * luma_stride ) + 2 * ((height / 2) * chroma_stride)). */
John Stultz18814f62018-02-22 16:02:49 -0800310 *size = height *(luma_stride + chroma_stride);
John Stultz16100f62017-05-03 11:12:18 -0700311 }
312
313 if (byte_stride != NULL)
314 {
315 *byte_stride = luma_stride;
316 }
317
318 if (pixel_stride != NULL)
319 {
320 *pixel_stride = luma_stride;
321 }
322
323 return true;
324}
John Stultz18814f62018-02-22 16:02:49 -0800325
John Stultz16100f62017-05-03 11:12:18 -0700326/*
327 * Computes the strides and size for an 8 bit YUYV 422 buffer
328 *
329 * width Public known width of the buffer in pixels
330 * height Public known height of the buffer in pixels
331 *
332 * pixel_stride (out) stride of the buffer in pixels
333 * byte_stride (out) stride of the buffer in bytes
334 * size (out) size of the buffer in bytes
335 */
John Stultz18814f62018-02-22 16:02:49 -0800336static bool get_yuv422_8bit_stride_and_size(int width, int height, int *pixel_stride, int *byte_stride, size_t *size)
John Stultz16100f62017-05-03 11:12:18 -0700337{
338 int local_byte_stride, local_pixel_stride;
339
340 /* 4:2:2 formats must have buffers with even width as the clump size is 2x1 pixels.
341 * This is taken care of by the even stride alignment. */
342
343 local_pixel_stride = GRALLOC_ALIGN(width, YUV_MALI_PLANE_ALIGN);
John Stultz18814f62018-02-22 16:02:49 -0800344 local_byte_stride = GRALLOC_ALIGN(width * 2, YUV_MALI_PLANE_ALIGN); /* 4 bytes per 2 pixels */
John Stultz16100f62017-05-03 11:12:18 -0700345
346 if (size != NULL)
347 {
John Stultz18814f62018-02-22 16:02:49 -0800348 *size = local_byte_stride *height;
John Stultz16100f62017-05-03 11:12:18 -0700349 }
350
351 if (byte_stride != NULL)
352 {
353 *byte_stride = local_byte_stride;
354 }
355
356 if (pixel_stride != NULL)
357 {
358 *pixel_stride = local_pixel_stride;
359 }
360
361 return true;
362}
363
364/*
365 * Computes the strides and size for an AFBC 8BIT YUV 4:2:2 buffer
366 *
367 * width width of the buffer in pixels
368 * height height of the buffer in pixels
369 *
370 * pixel_stride (out) stride of the buffer in pixels
371 * byte_stride (out) stride of the buffer in bytes
372 * size (out) size of the buffer in bytes
373 * type if buffer should be allocated for a certain afbc type
374 */
John Stultz18814f62018-02-22 16:02:49 -0800375static bool get_afbc_yuv422_8bit_stride_and_size(int width, int height, int *pixel_stride, int *byte_stride,
376 size_t *size, AllocType type)
John Stultz16100f62017-05-03 11:12:18 -0700377{
378 int yuv422_afbc_luma_stride;
379 int buffer_byte_alignment = AFBC_BODY_BUFFER_BYTE_ALIGNMENT;
380
381 if (type == UNCOMPRESSED)
382 {
383 AERR(" Buffer must be allocated with AFBC mode for internal pixel format YUV422_8BIT_AFBC!");
384 return false;
385 }
386 else if (type == AFBC_TILED_HEADERS_BASIC)
387 {
388 width = GRALLOC_ALIGN(width, AFBC_TILED_HEADERS_BASIC_WIDTH_ALIGN);
389 height = GRALLOC_ALIGN(height, AFBC_TILED_HEADERS_BASIC_HEIGHT_ALIGN);
390 buffer_byte_alignment = 4 * AFBC_BODY_BUFFER_BYTE_ALIGNMENT;
391 }
392 else if (type == AFBC_TILED_HEADERS_WIDEBLK)
393 {
394 width = GRALLOC_ALIGN(width, AFBC_TILED_HEADERS_WIDEBLK_WIDTH_ALIGN);
395 height = GRALLOC_ALIGN(height, AFBC_TILED_HEADERS_WIDEBLK_HEIGHT_ALIGN);
396 buffer_byte_alignment = 4 * AFBC_BODY_BUFFER_BYTE_ALIGNMENT;
397 }
398 else if (type == AFBC_PADDED)
399 {
400 AERR("GRALLOC_USAGE_PRIVATE_2 (64byte header row alignment for AFBC) is not supported for YUV");
401 return false;
402 }
403 else if (type == AFBC_WIDEBLK)
404 {
405 width = GRALLOC_ALIGN(width, AFBC_WIDEBLK_WIDTH_ALIGN);
406 height = GRALLOC_ALIGN(height, AFBC_WIDEBLK_HEIGHT_ALIGN);
407 }
408 else
409 {
410 width = GRALLOC_ALIGN(width, AFBC_NORMAL_WIDTH_ALIGN);
411 height = GRALLOC_ALIGN(height, AFBC_NORMAL_HEIGHT_ALIGN);
412 }
413
414 yuv422_afbc_luma_stride = width;
415
416 if (size != NULL)
417 {
418 int nblocks = width / AFBC_PIXELS_PER_BLOCK * height / AFBC_PIXELS_PER_BLOCK;
419 /* YUV 4:2:2 luma size equals chroma size */
John Stultz18814f62018-02-22 16:02:49 -0800420 *size = yuv422_afbc_luma_stride *height * 2 +
421 GRALLOC_ALIGN(nblocks * AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY, buffer_byte_alignment);
John Stultz16100f62017-05-03 11:12:18 -0700422 }
423
424 if (byte_stride != NULL)
425 {
426 *byte_stride = yuv422_afbc_luma_stride;
427 }
428
429 if (pixel_stride != NULL)
430 {
431 *pixel_stride = yuv422_afbc_luma_stride;
432 }
433
434 return true;
435}
436
437/*
438 * Calculate strides and sizes for a P010 (Y-UV 4:2:0) or P210 (Y-UV 4:2:2) buffer.
439 *
440 * @param width [in] Buffer width.
441 * @param height [in] Buffer height.
442 * @param vss [in] Vertical sub-sampling factor (2 for P010, 1 for
443 * P210. Anything else is invalid).
444 * @param pixel_stride [out] Pixel stride; number of pixels between
445 * consecutive rows.
446 * @param byte_stride [out] Byte stride; number of bytes between
447 * consecutive rows.
448 * @param size [out] Size of the buffer in bytes. Cumulative sum of
449 * sizes of all planes.
450 *
451 * @return true if the calculation was successful; false otherwise (invalid
452 * parameter)
453 */
John Stultz18814f62018-02-22 16:02:49 -0800454static bool get_yuv_pX10_stride_and_size(int width, int height, int vss, int *pixel_stride, int *byte_stride,
455 size_t *size)
John Stultz16100f62017-05-03 11:12:18 -0700456{
457 int luma_pixel_stride, luma_byte_stride;
458
459 if (vss < 1 || vss > 2)
460 {
461 AERR("Invalid vertical sub-sampling factor: %d, should be 1 or 2", vss);
462 return false;
463 }
464
465 /* 4:2:2 must have even width as the clump size is 2x1 pixels. This will be taken care of by the
466 * even stride alignment */
467 if (vss == 2)
468 {
469 /* 4:2:0 must also have even height as the clump size is 2x2 */
470 height = GRALLOC_ALIGN(height, 2);
471 }
472
473 luma_pixel_stride = GRALLOC_ALIGN(width, YUV_MALI_PLANE_ALIGN);
John Stultz18814f62018-02-22 16:02:49 -0800474 luma_byte_stride = GRALLOC_ALIGN(width * 2, YUV_MALI_PLANE_ALIGN);
John Stultz16100f62017-05-03 11:12:18 -0700475
476 if (size != NULL)
477 {
478 int chroma_size = GRALLOC_ALIGN(width * 2, YUV_MALI_PLANE_ALIGN) * (height / vss);
John Stultz18814f62018-02-22 16:02:49 -0800479 *size = luma_byte_stride *height + chroma_size;
John Stultz16100f62017-05-03 11:12:18 -0700480 }
481
482 if (byte_stride != NULL)
483 {
484 *byte_stride = luma_byte_stride;
485 }
486
487 if (pixel_stride != NULL)
488 {
489 *pixel_stride = luma_pixel_stride;
490 }
491
492 return true;
493}
494
495/*
496 * Calculate strides and strides for Y210 (10 bit YUYV packed, 4:2:2) format buffer.
497 *
498 * @param width [in] Buffer width.
499 * @param height [in] Buffer height.
500 * @param pixel_stride [out] Pixel stride; number of pixels between
501 * consecutive rows.
502 * @param byte_stride [out] Byte stride; number of bytes between
503 * consecutive rows.
504 * @param size [out] Size of the buffer in bytes. Cumulative sum of
505 * sizes of all planes.
506 *
507 * @return true if the calculation was successful; false otherwise (invalid
508 * parameter)
509 */
John Stultz18814f62018-02-22 16:02:49 -0800510static bool get_yuv_y210_stride_and_size(int width, int height, int *pixel_stride, int *byte_stride, size_t *size)
John Stultz16100f62017-05-03 11:12:18 -0700511{
512 int y210_byte_stride, y210_pixel_stride;
513
514 /* 4:2:2 formats must have buffers with even width as the clump size is 2x1 pixels.
515 * This is taken care of by the even stride alignment */
516
517 y210_pixel_stride = GRALLOC_ALIGN(width, YUV_MALI_PLANE_ALIGN);
518 /* 4x16 bits per 2 pixels */
John Stultz18814f62018-02-22 16:02:49 -0800519 y210_byte_stride = GRALLOC_ALIGN(width * 4, YUV_MALI_PLANE_ALIGN);
John Stultz16100f62017-05-03 11:12:18 -0700520
521 if (size != NULL)
522 {
John Stultz18814f62018-02-22 16:02:49 -0800523 *size = y210_byte_stride *height;
John Stultz16100f62017-05-03 11:12:18 -0700524 }
525
526 if (byte_stride != NULL)
527 {
528 *byte_stride = y210_byte_stride;
529 }
530
531 if (pixel_stride != NULL)
532 {
533 *pixel_stride = y210_pixel_stride;
534 }
535
536 return true;
537}
538
539/*
540 * Calculate strides and strides for Y0L2 (YUYAAYVYAA, 4:2:0) format buffer.
541 *
542 * @param width [in] Buffer width.
543 * @param height [in] Buffer height.
544 * @param pixel_stride [out] Pixel stride; number of pixels between
545 * consecutive rows.
546 * @param byte_stride [out] Byte stride; number of bytes between
547 * consecutive rows.
548 * @param size [out] Size of the buffer in bytes. Cumulative sum of
549 * sizes of all planes.
550 *
551 * @return true if the calculation was successful; false otherwise (invalid
552 * parameter)
553 *
554 * @note Each YUYAAYVYAA clump encodes a 2x2 area of pixels. YU&V are 10 bits. A is 1 bit. total 8 bytes
555 *
556 */
John Stultz18814f62018-02-22 16:02:49 -0800557static bool get_yuv_y0l2_stride_and_size(int width, int height, int *pixel_stride, int *byte_stride, size_t *size)
John Stultz16100f62017-05-03 11:12:18 -0700558{
559 int y0l2_byte_stride, y0l2_pixel_stride;
560
561 /* 4:2:0 formats must have buffers with even height and width as the clump size is 2x2 pixels.
562 * Width is take care of by the even stride alignment so just adjust height here for size calculation. */
563 height = GRALLOC_ALIGN(height, 2);
564
565 y0l2_pixel_stride = GRALLOC_ALIGN(width, YUV_MALI_PLANE_ALIGN);
John Stultz18814f62018-02-22 16:02:49 -0800566 y0l2_byte_stride = GRALLOC_ALIGN(width * 4, YUV_MALI_PLANE_ALIGN); /* 2 horiz pixels per 8 byte clump */
John Stultz16100f62017-05-03 11:12:18 -0700567
568 if (size != NULL)
569 {
John Stultz18814f62018-02-22 16:02:49 -0800570 *size = y0l2_byte_stride *height / 2; /* byte stride covers 2 vert pixels */
John Stultz16100f62017-05-03 11:12:18 -0700571 }
572
573 if (byte_stride != NULL)
574 {
575 *byte_stride = y0l2_byte_stride;
576 }
577
578 if (pixel_stride != NULL)
579 {
580 *pixel_stride = y0l2_pixel_stride;
581 }
John Stultz18814f62018-02-22 16:02:49 -0800582
John Stultz16100f62017-05-03 11:12:18 -0700583 return true;
584}
John Stultz18814f62018-02-22 16:02:49 -0800585
John Stultz16100f62017-05-03 11:12:18 -0700586/*
587 * Calculate strides and strides for Y410 (AVYU packed, 4:4:4) format buffer.
588 *
589 * @param width [in] Buffer width.
590 * @param height [in] Buffer height.
591 * @param pixel_stride [out] Pixel stride; number of pixels between
592 * consecutive rows.
593 * @param byte_stride [out] Byte stride; number of bytes between
594 * consecutive rows.
595 * @param size [out] Size of the buffer in bytes. Cumulative sum of
596 * sizes of all planes.
597 *
598 * @return true if the calculation was successful; false otherwise (invalid
599 * parameter)
600 */
John Stultz18814f62018-02-22 16:02:49 -0800601static bool get_yuv_y410_stride_and_size(int width, int height, int *pixel_stride, int *byte_stride, size_t *size)
John Stultz16100f62017-05-03 11:12:18 -0700602{
603 int y410_byte_stride, y410_pixel_stride;
604
605 y410_pixel_stride = GRALLOC_ALIGN(width, YUV_MALI_PLANE_ALIGN);
John Stultz18814f62018-02-22 16:02:49 -0800606 y410_byte_stride = GRALLOC_ALIGN(width * 4, YUV_MALI_PLANE_ALIGN);
John Stultz16100f62017-05-03 11:12:18 -0700607
608 if (size != NULL)
609 {
610 /* 4x8bits per pixel */
John Stultz18814f62018-02-22 16:02:49 -0800611 *size = y410_byte_stride *height;
John Stultz16100f62017-05-03 11:12:18 -0700612 }
613
614 if (byte_stride != NULL)
615 {
616 *byte_stride = y410_byte_stride;
617 }
618
619 if (pixel_stride != NULL)
620 {
621 *pixel_stride = y410_pixel_stride;
622 }
John Stultz18814f62018-02-22 16:02:49 -0800623
John Stultz16100f62017-05-03 11:12:18 -0700624 return true;
625}
626
627/*
628 * Calculate strides and strides for YUV420_10BIT_AFBC (Compressed, 4:2:0) format buffer.
629 *
630 * @param width [in] Buffer width.
631 * @param height [in] Buffer height.
632 * @param pixel_stride [out] Pixel stride; number of pixels between
633 * consecutive rows.
634 * @param byte_stride [out] Byte stride; number of bytes between
635 * consecutive rows.
636 * @param size [out] Size of the buffer in bytes. Cumulative sum of
637 * sizes of all planes.
638 * @param type [in] afbc mode that buffer should be allocated with.
639 *
640 * @param internalHeight [out] Internal buffer height that used by consumer or producer
641 *
642 * @return true if the calculation was successful; false otherwise (invalid
643 * parameter)
644 */
John Stultz18814f62018-02-22 16:02:49 -0800645static bool get_yuv420_10bit_afbc_stride_and_size(int width, int height, int *pixel_stride, int *byte_stride,
646 size_t *size, AllocType type, int *internalHeight)
John Stultz16100f62017-05-03 11:12:18 -0700647{
648 int yuv420_afbc_byte_stride, yuv420_afbc_pixel_stride;
649 int buffer_byte_alignment = AFBC_BODY_BUFFER_BYTE_ALIGNMENT;
650
651 if (width & 3)
652 {
653 return false;
654 }
655
John Stultz18814f62018-02-22 16:02:49 -0800656 *internalHeight = height;
John Stultz16100f62017-05-03 11:12:18 -0700657#if MALI_VIDEO_VERSION
658 /* If we have a greater internal height than public we set the internalHeight. This
659 * implies that cropping will be applied of internal dimensions to fit the public one. */
John Stultz18814f62018-02-22 16:02:49 -0800660 *internalHeight += AFBC_PIXELS_PER_BLOCK;
John Stultz16100f62017-05-03 11:12:18 -0700661#endif
John Stultz18814f62018-02-22 16:02:49 -0800662
John Stultz16100f62017-05-03 11:12:18 -0700663 /* The actual height used in size calculation must include the possible extra row. But
664 * it must also be AFBC-aligned. Only the extra row-padding should be reported back in
665 * internalHeight. This as only this row needs to be considered when cropping. */
666 if (type == UNCOMPRESSED)
667 {
668 AERR(" Buffer must be allocated with AFBC mode for internal pixel format YUV420_10BIT_AFBC!");
669 return false;
670 }
671 else if (type == AFBC_TILED_HEADERS_BASIC)
672 {
673 width = GRALLOC_ALIGN(width, AFBC_TILED_HEADERS_BASIC_WIDTH_ALIGN);
John Stultz18814f62018-02-22 16:02:49 -0800674 height = GRALLOC_ALIGN(*internalHeight / 2, AFBC_TILED_HEADERS_BASIC_HEIGHT_ALIGN);
John Stultz16100f62017-05-03 11:12:18 -0700675 buffer_byte_alignment = 4 * AFBC_BODY_BUFFER_BYTE_ALIGNMENT;
676 }
677 else if (type == AFBC_TILED_HEADERS_WIDEBLK)
678 {
679 width = GRALLOC_ALIGN(width, AFBC_TILED_HEADERS_WIDEBLK_WIDTH_ALIGN);
John Stultz18814f62018-02-22 16:02:49 -0800680 height = GRALLOC_ALIGN(*internalHeight / 2, AFBC_TILED_HEADERS_WIDEBLK_HEIGHT_ALIGN);
John Stultz16100f62017-05-03 11:12:18 -0700681 buffer_byte_alignment = 4 * AFBC_BODY_BUFFER_BYTE_ALIGNMENT;
682 }
683 else if (type == AFBC_PADDED)
684 {
685 AERR("GRALLOC_USAGE_PRIVATE_2 (64byte header row alignment for AFBC) is not supported for YUV");
686 return false;
687 }
688 else if (type == AFBC_WIDEBLK)
689 {
690 width = GRALLOC_ALIGN(width, AFBC_WIDEBLK_WIDTH_ALIGN);
John Stultz18814f62018-02-22 16:02:49 -0800691 height = GRALLOC_ALIGN(*internalHeight / 2, AFBC_WIDEBLK_HEIGHT_ALIGN);
John Stultz16100f62017-05-03 11:12:18 -0700692 }
693 else
694 {
695 width = GRALLOC_ALIGN(width, AFBC_NORMAL_WIDTH_ALIGN);
John Stultz18814f62018-02-22 16:02:49 -0800696 height = GRALLOC_ALIGN(*internalHeight / 2, AFBC_NORMAL_HEIGHT_ALIGN);
John Stultz16100f62017-05-03 11:12:18 -0700697 }
698
699 yuv420_afbc_pixel_stride = GRALLOC_ALIGN(width, 16);
John Stultz18814f62018-02-22 16:02:49 -0800700 yuv420_afbc_byte_stride = GRALLOC_ALIGN(width * 4, 16); /* 64-bit packed and horizontally downsampled */
John Stultz16100f62017-05-03 11:12:18 -0700701
702 if (size != NULL)
703 {
704 int nblocks = width / AFBC_PIXELS_PER_BLOCK * (*internalHeight) / AFBC_PIXELS_PER_BLOCK;
John Stultz18814f62018-02-22 16:02:49 -0800705 *size = yuv420_afbc_byte_stride *height +
706 GRALLOC_ALIGN(nblocks * AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY, buffer_byte_alignment);
John Stultz16100f62017-05-03 11:12:18 -0700707 }
708
709 if (byte_stride != NULL)
710 {
711 *byte_stride = yuv420_afbc_pixel_stride;
712 }
713
714 if (pixel_stride != NULL)
715 {
716 *pixel_stride = yuv420_afbc_pixel_stride;
717 }
718
719 return true;
720}
721
722/*
723 * Calculate strides and strides for YUV422_10BIT_AFBC (Compressed, 4:2:2) format buffer.
724 *
725 * @param width [in] Buffer width.
726 * @param height [in] Buffer height.
727 * @param pixel_stride [out] Pixel stride; number of pixels between
728 * consecutive rows.
729 * @param byte_stride [out] Byte stride; number of bytes between
730 * consecutive rows.
731 * @param size [out] Size of the buffer in bytes. Cumulative sum of
732 * sizes of all planes.
733 * @param type [in] afbc mode that buffer should be allocated with.
734 *
735 * @return true if the calculation was successful; false otherwise (invalid
736 * parameter)
737 */
John Stultz18814f62018-02-22 16:02:49 -0800738static bool get_yuv422_10bit_afbc_stride_and_size(int width, int height, int *pixel_stride, int *byte_stride,
739 size_t *size, AllocType type)
John Stultz16100f62017-05-03 11:12:18 -0700740{
741 int yuv422_afbc_byte_stride, yuv422_afbc_pixel_stride;
742 int buffer_byte_alignment = AFBC_BODY_BUFFER_BYTE_ALIGNMENT;
743
744 if (width & 3)
745 {
746 return false;
747 }
748
749 if (type == UNCOMPRESSED)
750 {
751 AERR(" Buffer must be allocated with AFBC mode for internal pixel format YUV422_10BIT_AFBC!");
752 return false;
753 }
754 else if (type == AFBC_TILED_HEADERS_BASIC)
755 {
756 width = GRALLOC_ALIGN(width, AFBC_TILED_HEADERS_BASIC_WIDTH_ALIGN);
757 height = GRALLOC_ALIGN(height, AFBC_TILED_HEADERS_BASIC_HEIGHT_ALIGN);
758 buffer_byte_alignment = 4 * AFBC_BODY_BUFFER_BYTE_ALIGNMENT;
759 }
760 else if (type == AFBC_TILED_HEADERS_WIDEBLK)
761 {
762 width = GRALLOC_ALIGN(width, AFBC_TILED_HEADERS_WIDEBLK_WIDTH_ALIGN);
763 height = GRALLOC_ALIGN(height, AFBC_TILED_HEADERS_WIDEBLK_HEIGHT_ALIGN);
764 buffer_byte_alignment = 4 * AFBC_BODY_BUFFER_BYTE_ALIGNMENT;
765 }
766 else if (type == AFBC_PADDED)
767 {
768 AERR("GRALLOC_USAGE_PRIVATE_2 (64byte header row alignment for AFBC) is not supported for YUV");
769 return false;
770 }
771 else if (type == AFBC_WIDEBLK)
772 {
773 width = GRALLOC_ALIGN(width, AFBC_WIDEBLK_WIDTH_ALIGN);
774 height = GRALLOC_ALIGN(height, AFBC_WIDEBLK_HEIGHT_ALIGN);
775 }
776 else
777 {
778 width = GRALLOC_ALIGN(width, AFBC_NORMAL_WIDTH_ALIGN);
779 height = GRALLOC_ALIGN(height, AFBC_NORMAL_HEIGHT_ALIGN);
780 }
781
782 yuv422_afbc_pixel_stride = GRALLOC_ALIGN(width, 16);
John Stultz18814f62018-02-22 16:02:49 -0800783 yuv422_afbc_byte_stride = GRALLOC_ALIGN(width * 2, 16);
John Stultz16100f62017-05-03 11:12:18 -0700784
785 if (size != NULL)
786 {
787 int nblocks = width / AFBC_PIXELS_PER_BLOCK * height / AFBC_PIXELS_PER_BLOCK;
788 /* YUV 4:2:2 chroma size equals to luma size */
John Stultz18814f62018-02-22 16:02:49 -0800789 *size = yuv422_afbc_byte_stride *height * 2 +
790 GRALLOC_ALIGN(nblocks * AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY, buffer_byte_alignment);
John Stultz16100f62017-05-03 11:12:18 -0700791 }
792
793 if (byte_stride != NULL)
794 {
795 *byte_stride = yuv422_afbc_byte_stride;
796 }
797
798 if (pixel_stride != NULL)
799 {
800 *pixel_stride = yuv422_afbc_pixel_stride;
801 }
802
803 return true;
804}
805
806/*
807 * Calculate strides and strides for Camera RAW and Blob formats
808 *
809 * @param w [in] Buffer width.
810 * @param h [in] Buffer height.
811 * @param format [in] Requested HAL format
812 * @param out_stride [out] Pixel stride; number of pixels/bytes between
813 * consecutive rows. Format description calls for
814 * either bytes or pixels.
815 * @param size [out] Size of the buffer in bytes. Cumulative sum of
816 * sizes of all planes.
817 *
818 * @return true if the calculation was successful; false otherwise (invalid
819 * parameter)
820 */
821static bool get_camera_formats_stride_and_size(int w, int h, uint64_t format, int *out_stride, size_t *out_size)
822{
823 int stride, size;
824
825 switch (format)
826 {
John Stultz18814f62018-02-22 16:02:49 -0800827 case HAL_PIXEL_FORMAT_RAW16:
828 stride = w; /* Format assumes stride in pixels */
829 stride = GRALLOC_ALIGN(stride, 16); /* Alignment mandated by Android */
830 size = stride * h * 2; /* 2 bytes per pixel */
831 break;
John Stultz16100f62017-05-03 11:12:18 -0700832
John Stultz18814f62018-02-22 16:02:49 -0800833 case HAL_PIXEL_FORMAT_RAW12:
834 if (w % 4 != 0)
835 {
836 ALOGE("ERROR: Width for HAL_PIXEL_FORMAT_RAW12 buffers has to be multiple of 4.");
John Stultz16100f62017-05-03 11:12:18 -0700837 return false;
John Stultz18814f62018-02-22 16:02:49 -0800838 }
John Stultz16100f62017-05-03 11:12:18 -0700839
John Stultz18814f62018-02-22 16:02:49 -0800840 stride = (w / 2) * 3; /* Stride in bytes; 2 pixels in 3 bytes */
841 size = stride * h;
842 break;
843
844 case HAL_PIXEL_FORMAT_RAW10:
845 if (w % 4 != 0)
846 {
847 ALOGE("ERROR: Width for HAL_PIXEL_FORMAT_RAW10 buffers has to be multiple of 4.");
848 return false;
849 }
850
851 stride = (w / 4) * 5; /* Stride in bytes; 4 pixels in 5 bytes */
852 size = stride * h;
853 break;
854
855 case HAL_PIXEL_FORMAT_BLOB:
856 if (h != 1)
857 {
858 ALOGE("ERROR: Height for HAL_PIXEL_FORMAT_BLOB must be 1.");
859 return false;
860 }
861
862 stride = 0; /* No 'rows', it's effectively a long one dimensional array */
863 size = w;
864 break;
865
866 default:
867 return false;
John Stultz16100f62017-05-03 11:12:18 -0700868 }
869
870 if (out_size != NULL)
871 {
872 *out_size = size;
873 }
874
875 if (out_stride != NULL)
876 {
877 *out_stride = stride;
878 }
879
880 return true;
881}
882
John Stultz18814f62018-02-22 16:02:49 -0800883int mali_gralloc_buffer_allocate(mali_gralloc_module *m, const gralloc_buffer_descriptor_t *descriptors,
884 uint32_t numDescriptors, buffer_handle_t *pHandle, bool *shared_backend)
John Stultz16100f62017-05-03 11:12:18 -0700885{
John Stultz18814f62018-02-22 16:02:49 -0800886 bool shared = false;
887 uint64_t backing_store_id = 0x0;
888 AllocType alloc_type = UNCOMPRESSED;
889 uint64_t usage;
890 uint32_t i = 0;
891 int err;
John Stultz16100f62017-05-03 11:12:18 -0700892
John Stultz18814f62018-02-22 16:02:49 -0800893 for (i = 0; i < numDescriptors; i++)
John Stultz16100f62017-05-03 11:12:18 -0700894 {
John Stultz18814f62018-02-22 16:02:49 -0800895 buffer_descriptor_t *bufDescriptor = (buffer_descriptor_t *)(descriptors[i]);
John Stultz16100f62017-05-03 11:12:18 -0700896
John Stultz18814f62018-02-22 16:02:49 -0800897 /* Some formats require an internal width and height that may be used by
898 * consumers/producers.
899 */
900 bufDescriptor->internalWidth = bufDescriptor->width;
901 bufDescriptor->internalHeight = bufDescriptor->height;
902 usage = bufDescriptor->producer_usage | bufDescriptor->consumer_usage;
John Stultz16100f62017-05-03 11:12:18 -0700903
John Stultz18814f62018-02-22 16:02:49 -0800904 bufDescriptor->internal_format = mali_gralloc_select_format(
905 bufDescriptor->hal_format, bufDescriptor->format_type, usage, bufDescriptor->width * bufDescriptor->height);
John Stultz16100f62017-05-03 11:12:18 -0700906
John Stultz18814f62018-02-22 16:02:49 -0800907 if (bufDescriptor->internal_format == 0)
John Stultz16100f62017-05-03 11:12:18 -0700908 {
John Stultz18814f62018-02-22 16:02:49 -0800909 ALOGE("Unrecognized and/or unsupported format 0x%" PRIx64 " and usage 0x%" PRIx64,
910 bufDescriptor->hal_format, usage);
911 return -EINVAL;
912 }
913
914 /* Determine AFBC type for this format */
915 if (bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_AFBCENABLE_MASK)
916 {
917 if (bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS)
John Stultz16100f62017-05-03 11:12:18 -0700918 {
John Stultz18814f62018-02-22 16:02:49 -0800919 if (bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_AFBC_WIDEBLK)
920 {
921 alloc_type = AFBC_TILED_HEADERS_WIDEBLK;
922 }
923 else if (bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_AFBC_BASIC)
924 {
925 alloc_type = AFBC_TILED_HEADERS_BASIC;
926 }
927 else if (bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_AFBC_SPLITBLK)
928 {
929 ALOGE("Unsupported format. Splitblk in tiled header configuration.");
930 return -EINVAL;
931 }
John Stultz16100f62017-05-03 11:12:18 -0700932 }
John Stultz18814f62018-02-22 16:02:49 -0800933 else if (usage & MALI_GRALLOC_USAGE_AFBC_PADDING)
John Stultz16100f62017-05-03 11:12:18 -0700934 {
John Stultz18814f62018-02-22 16:02:49 -0800935 alloc_type = AFBC_PADDED;
John Stultz16100f62017-05-03 11:12:18 -0700936 }
John Stultz18814f62018-02-22 16:02:49 -0800937 else if (bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_AFBC_WIDEBLK)
John Stultz16100f62017-05-03 11:12:18 -0700938 {
John Stultz18814f62018-02-22 16:02:49 -0800939 alloc_type = AFBC_WIDEBLK;
940 }
941 else
942 {
943 alloc_type = AFBC;
John Stultz16100f62017-05-03 11:12:18 -0700944 }
945 }
John Stultz16100f62017-05-03 11:12:18 -0700946
John Stultz18814f62018-02-22 16:02:49 -0800947 uint64_t base_format = bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_FMT_MASK;
948
949 switch (base_format)
950 {
John Stultz16100f62017-05-03 11:12:18 -0700951 case HAL_PIXEL_FORMAT_RGBA_8888:
952 case HAL_PIXEL_FORMAT_RGBX_8888:
953 case HAL_PIXEL_FORMAT_BGRA_8888:
John Stultz18814f62018-02-22 16:02:49 -0800954 get_rgb_stride_and_size(bufDescriptor->width, bufDescriptor->height, 4, &bufDescriptor->pixel_stride,
955 &bufDescriptor->byte_stride, &bufDescriptor->size, alloc_type);
John Stultz16100f62017-05-03 11:12:18 -0700956 break;
John Stultz18814f62018-02-22 16:02:49 -0800957
John Stultz16100f62017-05-03 11:12:18 -0700958 case HAL_PIXEL_FORMAT_RGB_888:
John Stultz18814f62018-02-22 16:02:49 -0800959 get_rgb_stride_and_size(bufDescriptor->width, bufDescriptor->height, 3, &bufDescriptor->pixel_stride,
960 &bufDescriptor->byte_stride, &bufDescriptor->size, alloc_type);
John Stultz16100f62017-05-03 11:12:18 -0700961 break;
John Stultz18814f62018-02-22 16:02:49 -0800962
John Stultz16100f62017-05-03 11:12:18 -0700963 case HAL_PIXEL_FORMAT_RGB_565:
John Stultz18814f62018-02-22 16:02:49 -0800964 get_rgb_stride_and_size(bufDescriptor->width, bufDescriptor->height, 2, &bufDescriptor->pixel_stride,
965 &bufDescriptor->byte_stride, &bufDescriptor->size, alloc_type);
John Stultz16100f62017-05-03 11:12:18 -0700966 break;
967
968 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
969 case MALI_GRALLOC_FORMAT_INTERNAL_YV12:
970 case MALI_GRALLOC_FORMAT_INTERNAL_NV12:
971 case MALI_GRALLOC_FORMAT_INTERNAL_NV21:
972 {
973 /* Mali subsystem prefers higher stride alignment values (128 bytes) for YUV, but software components assume
974 * default of 16. We only need to care about YV12 as it's the only, implicit, HAL YUV format in Android.
975 */
976 int yv12_align = YUV_MALI_PLANE_ALIGN;
John Stultz18814f62018-02-22 16:02:49 -0800977
978 if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK))
John Stultz16100f62017-05-03 11:12:18 -0700979 {
980 yv12_align = YUV_ANDROID_PLANE_ALIGN;
981 }
982
John Stultz18814f62018-02-22 16:02:49 -0800983 if (!get_yv12_stride_and_size(bufDescriptor->width, bufDescriptor->height, &bufDescriptor->pixel_stride,
984 &bufDescriptor->byte_stride, &bufDescriptor->size, alloc_type,
985 &bufDescriptor->internalHeight, yv12_align))
John Stultz16100f62017-05-03 11:12:18 -0700986 {
987 return -EINVAL;
988 }
John Stultz18814f62018-02-22 16:02:49 -0800989
John Stultz16100f62017-05-03 11:12:18 -0700990 break;
991 }
John Stultz18814f62018-02-22 16:02:49 -0800992
John Stultz16100f62017-05-03 11:12:18 -0700993 case HAL_PIXEL_FORMAT_YCbCr_422_I:
994 {
995 /* YUYV 4:2:2 */
John Stultz18814f62018-02-22 16:02:49 -0800996 if (alloc_type != UNCOMPRESSED ||
997 !get_yuv422_8bit_stride_and_size(bufDescriptor->width, bufDescriptor->height,
998 &bufDescriptor->pixel_stride, &bufDescriptor->byte_stride,
999 &bufDescriptor->size))
John Stultz16100f62017-05-03 11:12:18 -07001000 {
1001 return -EINVAL;
1002 }
John Stultz18814f62018-02-22 16:02:49 -08001003
John Stultz16100f62017-05-03 11:12:18 -07001004 break;
1005 }
John Stultz18814f62018-02-22 16:02:49 -08001006
John Stultz16100f62017-05-03 11:12:18 -07001007 case HAL_PIXEL_FORMAT_RAW16:
1008 case HAL_PIXEL_FORMAT_RAW12:
1009 case HAL_PIXEL_FORMAT_RAW10:
1010 case HAL_PIXEL_FORMAT_BLOB:
John Stultz18814f62018-02-22 16:02:49 -08001011 if (alloc_type != UNCOMPRESSED)
John Stultz16100f62017-05-03 11:12:18 -07001012 {
1013 return -EINVAL;
1014 }
John Stultz18814f62018-02-22 16:02:49 -08001015
1016 get_camera_formats_stride_and_size(bufDescriptor->width, bufDescriptor->height, base_format,
1017 &bufDescriptor->pixel_stride, &bufDescriptor->size);
1018 /* For Raw/Blob formats stride is defined to be either in bytes or pixels per format */
1019 bufDescriptor->byte_stride = bufDescriptor->pixel_stride;
John Stultz16100f62017-05-03 11:12:18 -07001020 break;
1021
1022 case MALI_GRALLOC_FORMAT_INTERNAL_Y0L2:
John Stultz18814f62018-02-22 16:02:49 -08001023
John Stultz16100f62017-05-03 11:12:18 -07001024 /* YUYAAYUVAA 4:2:0 with and without AFBC */
John Stultz18814f62018-02-22 16:02:49 -08001025 if (alloc_type != UNCOMPRESSED)
John Stultz16100f62017-05-03 11:12:18 -07001026 {
John Stultz18814f62018-02-22 16:02:49 -08001027 if (!get_yuv420_10bit_afbc_stride_and_size(
1028 bufDescriptor->width, bufDescriptor->height, &bufDescriptor->pixel_stride,
1029 &bufDescriptor->byte_stride, &bufDescriptor->size, alloc_type, &bufDescriptor->internalHeight))
John Stultz16100f62017-05-03 11:12:18 -07001030 {
1031 return -EINVAL;
1032 }
1033 }
1034 else
1035 {
John Stultz18814f62018-02-22 16:02:49 -08001036 if (!get_yuv_y0l2_stride_and_size(bufDescriptor->width, bufDescriptor->height,
1037 &bufDescriptor->pixel_stride, &bufDescriptor->byte_stride,
1038 &bufDescriptor->size))
John Stultz16100f62017-05-03 11:12:18 -07001039 {
1040 return -EINVAL;
1041 }
1042 }
John Stultz18814f62018-02-22 16:02:49 -08001043
John Stultz16100f62017-05-03 11:12:18 -07001044 break;
1045
1046 case MALI_GRALLOC_FORMAT_INTERNAL_P010:
John Stultz18814f62018-02-22 16:02:49 -08001047
John Stultz16100f62017-05-03 11:12:18 -07001048 /* Y-UV 4:2:0 */
John Stultz18814f62018-02-22 16:02:49 -08001049 if (alloc_type != UNCOMPRESSED ||
1050 !get_yuv_pX10_stride_and_size(bufDescriptor->width, bufDescriptor->height, 2,
1051 &bufDescriptor->pixel_stride, &bufDescriptor->byte_stride,
1052 &bufDescriptor->size))
John Stultz16100f62017-05-03 11:12:18 -07001053 {
1054 return -EINVAL;
1055 }
John Stultz18814f62018-02-22 16:02:49 -08001056
John Stultz16100f62017-05-03 11:12:18 -07001057 break;
1058
1059 case MALI_GRALLOC_FORMAT_INTERNAL_P210:
John Stultz18814f62018-02-22 16:02:49 -08001060
John Stultz16100f62017-05-03 11:12:18 -07001061 /* Y-UV 4:2:2 */
John Stultz18814f62018-02-22 16:02:49 -08001062 if (alloc_type != UNCOMPRESSED ||
1063 !get_yuv_pX10_stride_and_size(bufDescriptor->width, bufDescriptor->height, 1,
1064 &bufDescriptor->pixel_stride, &bufDescriptor->byte_stride,
1065 &bufDescriptor->size))
John Stultz16100f62017-05-03 11:12:18 -07001066 {
1067 return -EINVAL;
1068 }
John Stultz18814f62018-02-22 16:02:49 -08001069
John Stultz16100f62017-05-03 11:12:18 -07001070 break;
1071
1072 case MALI_GRALLOC_FORMAT_INTERNAL_Y210:
John Stultz18814f62018-02-22 16:02:49 -08001073
John Stultz16100f62017-05-03 11:12:18 -07001074 /* YUYV 4:2:2 with and without AFBC */
John Stultz18814f62018-02-22 16:02:49 -08001075 if (alloc_type != UNCOMPRESSED)
John Stultz16100f62017-05-03 11:12:18 -07001076 {
John Stultz18814f62018-02-22 16:02:49 -08001077 if (!get_yuv422_10bit_afbc_stride_and_size(bufDescriptor->width, bufDescriptor->height,
1078 &bufDescriptor->pixel_stride, &bufDescriptor->byte_stride,
1079 &bufDescriptor->size, alloc_type))
John Stultz16100f62017-05-03 11:12:18 -07001080 {
1081 return -EINVAL;
1082 }
1083 }
1084 else
1085 {
John Stultz18814f62018-02-22 16:02:49 -08001086 if (!get_yuv_y210_stride_and_size(bufDescriptor->width, bufDescriptor->height,
1087 &bufDescriptor->pixel_stride, &bufDescriptor->byte_stride,
1088 &bufDescriptor->size))
John Stultz16100f62017-05-03 11:12:18 -07001089 {
1090 return -EINVAL;
1091 }
1092 }
John Stultz18814f62018-02-22 16:02:49 -08001093
John Stultz16100f62017-05-03 11:12:18 -07001094 break;
1095
1096 case MALI_GRALLOC_FORMAT_INTERNAL_Y410:
John Stultz18814f62018-02-22 16:02:49 -08001097
John Stultz16100f62017-05-03 11:12:18 -07001098 /* AVYU 2-10-10-10 */
John Stultz18814f62018-02-22 16:02:49 -08001099 if (alloc_type != UNCOMPRESSED ||
1100 !get_yuv_y410_stride_and_size(bufDescriptor->width, bufDescriptor->height, &bufDescriptor->pixel_stride,
1101 &bufDescriptor->byte_stride, &bufDescriptor->size))
John Stultz16100f62017-05-03 11:12:18 -07001102 {
1103 return -EINVAL;
1104 }
John Stultz18814f62018-02-22 16:02:49 -08001105
John Stultz16100f62017-05-03 11:12:18 -07001106 break;
1107
1108 case MALI_GRALLOC_FORMAT_INTERNAL_YUV422_8BIT:
John Stultz18814f62018-02-22 16:02:49 -08001109
John Stultz16100f62017-05-03 11:12:18 -07001110 /* 8BIT AFBC YUV4:2:2 testing usage */
1111
John Stultz18814f62018-02-22 16:02:49 -08001112 /* We only support compressed for this format right now.
1113 * Below will fail in case format is uncompressed.
1114 */
1115 if (!get_afbc_yuv422_8bit_stride_and_size(bufDescriptor->width, bufDescriptor->height,
1116 &bufDescriptor->pixel_stride, &bufDescriptor->byte_stride,
1117 &bufDescriptor->size, alloc_type))
John Stultz16100f62017-05-03 11:12:18 -07001118 {
1119 return -EINVAL;
1120 }
John Stultz18814f62018-02-22 16:02:49 -08001121
John Stultz16100f62017-05-03 11:12:18 -07001122 break;
John Stultz18814f62018-02-22 16:02:49 -08001123
1124 /*
1125 * Additional custom formats can be added here
1126 * and must fill the variables pixel_stride, byte_stride and size.
1127 */
John Stultz16100f62017-05-03 11:12:18 -07001128 default:
1129 return -EINVAL;
John Stultz16100f62017-05-03 11:12:18 -07001130 }
John Stultz18814f62018-02-22 16:02:49 -08001131 }
1132
1133 {
1134 /* Allocate ION backing store memory */
1135 err = mali_gralloc_ion_allocate(m, descriptors, numDescriptors, pHandle, &shared);
1136
1137 if (err < 0)
John Stultz16100f62017-05-03 11:12:18 -07001138 {
John Stultz16100f62017-05-03 11:12:18 -07001139 return err;
1140 }
1141 }
1142
John Stultz18814f62018-02-22 16:02:49 -08001143 if (shared)
John Stultz16100f62017-05-03 11:12:18 -07001144 {
John Stultz18814f62018-02-22 16:02:49 -08001145 backing_store_id = getUniqueId();
1146 }
1147
1148 for (i = 0; i < numDescriptors; i++)
1149 {
1150 buffer_descriptor_t *bufDescriptor = (buffer_descriptor_t *)descriptors[i];
1151 private_handle_t *hnd = (private_handle_t *)pHandle[i];
1152
1153 usage = bufDescriptor->consumer_usage | bufDescriptor->producer_usage;
1154
1155 err = gralloc_buffer_attr_allocate(hnd);
1156
1157 if (err < 0)
1158 {
1159 /* free all allocated ion buffer& attr buffer here.*/
1160 mali_gralloc_buffer_free_internal(pHandle, numDescriptors);
1161 return err;
1162 }
1163
1164 mali_gralloc_dump_buffer_add(hnd);
1165
1166 switch (usage & MALI_GRALLOC_USAGE_YUV_CONF_MASK)
1167 {
John Stultz16100f62017-05-03 11:12:18 -07001168 case MALI_GRALLOC_USAGE_YUV_CONF_0:
1169 hnd->yuv_info = MALI_YUV_BT601_NARROW;
1170 break;
John Stultz18814f62018-02-22 16:02:49 -08001171
John Stultz16100f62017-05-03 11:12:18 -07001172 case MALI_GRALLOC_USAGE_YUV_CONF_1:
1173 hnd->yuv_info = MALI_YUV_BT601_WIDE;
1174 break;
John Stultz18814f62018-02-22 16:02:49 -08001175
John Stultz16100f62017-05-03 11:12:18 -07001176 case MALI_GRALLOC_USAGE_YUV_CONF_2:
1177 hnd->yuv_info = MALI_YUV_BT709_NARROW;
1178 break;
John Stultz18814f62018-02-22 16:02:49 -08001179
John Stultz16100f62017-05-03 11:12:18 -07001180 case MALI_GRALLOC_USAGE_YUV_CONF_3:
1181 hnd->yuv_info = MALI_YUV_BT709_WIDE;
1182 break;
John Stultz18814f62018-02-22 16:02:49 -08001183 }
1184
1185 /* Workaround 10bit YUV only support BT709_WIDE in GPU DDK */
1186 if ((bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_FMT_MASK) == MALI_GRALLOC_FORMAT_INTERNAL_Y0L2)
1187 {
1188 hnd->yuv_info = MALI_YUV_BT709_WIDE;
1189 }
1190
1191 if (shared)
1192 {
1193 /*each buffer will share the same backing store id.*/
1194 hnd->backing_store_id = backing_store_id;
1195 }
1196 else
1197 {
1198 /* each buffer will have an unique backing store id.*/
1199 hnd->backing_store_id = getUniqueId();
1200 }
John Stultz16100f62017-05-03 11:12:18 -07001201 }
1202
John Stultz18814f62018-02-22 16:02:49 -08001203 if (NULL != shared_backend)
John Stultz16100f62017-05-03 11:12:18 -07001204 {
John Stultz18814f62018-02-22 16:02:49 -08001205 *shared_backend = shared;
John Stultz16100f62017-05-03 11:12:18 -07001206 }
John Stultz16100f62017-05-03 11:12:18 -07001207
1208 return 0;
1209}
1210
John Stultz18814f62018-02-22 16:02:49 -08001211int mali_gralloc_buffer_free(buffer_handle_t pHandle)
John Stultz16100f62017-05-03 11:12:18 -07001212{
John Stultz18814f62018-02-22 16:02:49 -08001213 int rval = -1;
1214 private_handle_t *hnd = (private_handle_t *)(pHandle);
John Stultz16100f62017-05-03 11:12:18 -07001215
John Stultz18814f62018-02-22 16:02:49 -08001216 if (hnd != NULL)
John Stultz16100f62017-05-03 11:12:18 -07001217 {
John Stultz18814f62018-02-22 16:02:49 -08001218 rval = gralloc_buffer_attr_free(hnd);
1219 mali_gralloc_ion_free(hnd);
John Stultz16100f62017-05-03 11:12:18 -07001220 }
1221
John Stultz18814f62018-02-22 16:02:49 -08001222 return rval;
1223}
John Stultz16100f62017-05-03 11:12:18 -07001224
John Stultz18814f62018-02-22 16:02:49 -08001225static int mali_gralloc_buffer_free_internal(buffer_handle_t *pHandle, uint32_t num_hnds)
1226{
1227 int err = -1;
1228 uint32_t i = 0;
John Stultz16100f62017-05-03 11:12:18 -07001229
John Stultz18814f62018-02-22 16:02:49 -08001230 for (i = 0; i < num_hnds; i++)
1231 {
1232 private_handle_t *hnd = (private_handle_t *)(pHandle[i]);
1233
1234 err = gralloc_buffer_attr_free(hnd);
1235 mali_gralloc_ion_free(hnd);
John Stultz16100f62017-05-03 11:12:18 -07001236 }
1237
John Stultz18814f62018-02-22 16:02:49 -08001238 return err;
John Stultz16100f62017-05-03 11:12:18 -07001239}