1 /*
2 * Copyright (C) 2016-2017 ARM Limited. All rights reserved.
3 *
4 * Copyright (C) 2008 The Android Open Source Project
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #include <hardware/hardware.h>
20 #include <inttypes.h>
21 #include <atomic>
22
23 #if GRALLOC_USE_GRALLOC1_API == 1
24 #include <hardware/gralloc1.h>
25 #else
26 #include <hardware/gralloc.h>
27 #endif
28
29 #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"
34 #include "gralloc_buffer_priv.h"
35 #include "mali_gralloc_bufferdescriptor.h"
36 #include "mali_gralloc_debug.h"
37
38 #define AFBC_PIXELS_PER_BLOCK 16
39 #define AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY 16
40
41 #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
46 // 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
49 #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
53
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
61 /*
62 * Type of allocation
63 */
64 enum 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
79 static int mali_gralloc_buffer_free_internal(buffer_handle_t *pHandle, uint32_t num_hnds);
80
81 /*
82 * Get a global unique ID
83 */
getUniqueId()84 static 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
91 /*
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 */
get_rgb_stride_and_size(int width,int height,int pixel_size,int * pixel_stride,int * byte_stride,size_t * size,AllocType type)103 static 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)
105 {
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 {
116 *size = stride *height;
117 }
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;
132 int h_aligned = GRALLOC_ALIGN(height, AFBC_NORMAL_HEIGHT_ALIGN);
133 int nblocks;
134 int buffer_byte_alignment = AFBC_BODY_BUFFER_BYTE_ALIGNMENT;
135
136 if (type == AFBC_TILED_HEADERS_BASIC)
137 {
138 w_aligned = GRALLOC_ALIGN(width, AFBC_TILED_HEADERS_BASIC_WIDTH_ALIGN);
139 h_aligned = GRALLOC_ALIGN(height, AFBC_TILED_HEADERS_BASIC_HEIGHT_ALIGN);
140 buffer_byte_alignment = 4 * AFBC_BODY_BUFFER_BYTE_ALIGNMENT;
141 }
142 else if (type == AFBC_TILED_HEADERS_WIDEBLK)
143 {
144 w_aligned = GRALLOC_ALIGN(width, AFBC_TILED_HEADERS_WIDEBLK_WIDTH_ALIGN);
145 h_aligned = GRALLOC_ALIGN(height, AFBC_TILED_HEADERS_WIDEBLK_HEIGHT_ALIGN);
146 buffer_byte_alignment = 4 * AFBC_BODY_BUFFER_BYTE_ALIGNMENT;
147 }
148 else if (type == AFBC_PADDED)
149 {
150 w_aligned = GRALLOC_ALIGN(width, 64);
151 }
152 else if (type == AFBC_WIDEBLK)
153 {
154 w_aligned = GRALLOC_ALIGN(width, AFBC_WIDEBLK_WIDTH_ALIGN);
155 h_aligned = GRALLOC_ALIGN(height, AFBC_WIDEBLK_HEIGHT_ALIGN);
156 }
157 else
158 {
159 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;
173 }
174
175 nblocks = w_aligned / AFBC_PIXELS_PER_BLOCK * h_aligned / AFBC_PIXELS_PER_BLOCK;
176
177 if (size != NULL)
178 {
179 *size = stride *h_aligned +
180 GRALLOC_ALIGN(nblocks * AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY, buffer_byte_alignment);
181 }
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 */
get_afbc_yuv420_8bit_stride_and_size(int width,int height,int * pixel_stride,int * byte_stride,size_t * size,AllocType type,int * internalHeight)197 static 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)
199 {
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 {
226 width = GRALLOC_ALIGN(width, AFBC_TILED_HEADERS_BASIC_WIDTH_ALIGN);
227 height = GRALLOC_ALIGN(*internalHeight, AFBC_TILED_HEADERS_BASIC_HEIGHT_ALIGN);
228 buffer_byte_alignment = 4 * AFBC_BODY_BUFFER_BYTE_ALIGNMENT;
229 }
230 else if (type == AFBC_TILED_HEADERS_WIDEBLK)
231 {
232 width = GRALLOC_ALIGN(width, AFBC_TILED_HEADERS_WIDEBLK_WIDTH_ALIGN);
233 height = GRALLOC_ALIGN(*internalHeight, AFBC_TILED_HEADERS_WIDEBLK_HEIGHT_ALIGN);
234 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);
244 height = GRALLOC_ALIGN(*internalHeight, AFBC_WIDEBLK_HEIGHT_ALIGN);
245 }
246 else
247 {
248 width = GRALLOC_ALIGN(width, AFBC_NORMAL_WIDTH_ALIGN);
249 height = GRALLOC_ALIGN(*internalHeight, AFBC_NORMAL_HEIGHT_ALIGN);
250 }
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) */
259 *size = (yuv420_afbc_luma_stride + yuv420_afbc_chroma_stride) * height +
260 GRALLOC_ALIGN(nblocks * AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY, buffer_byte_alignment);
261 }
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 */
get_yv12_stride_and_size(int width,int height,int * pixel_stride,int * byte_stride,size_t * size,AllocType type,int * internalHeight,int stride_alignment)289 static 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)
291 {
292 int luma_stride;
293
294 if (type != UNCOMPRESSED)
295 {
296 return get_afbc_yuv420_8bit_stride_and_size(width, height, pixel_stride, byte_stride, size, type,
297 internalHeight);
298 }
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)). */
310 *size = height *(luma_stride + chroma_stride);
311 }
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 }
325
326 /*
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 */
get_yuv422_8bit_stride_and_size(int width,int height,int * pixel_stride,int * byte_stride,size_t * size)336 static bool get_yuv422_8bit_stride_and_size(int width, int height, int *pixel_stride, int *byte_stride, size_t *size)
337 {
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);
344 local_byte_stride = GRALLOC_ALIGN(width * 2, YUV_MALI_PLANE_ALIGN); /* 4 bytes per 2 pixels */
345
346 if (size != NULL)
347 {
348 *size = local_byte_stride *height;
349 }
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 */
get_afbc_yuv422_8bit_stride_and_size(int width,int height,int * pixel_stride,int * byte_stride,size_t * size,AllocType type)375 static bool get_afbc_yuv422_8bit_stride_and_size(int width, int height, int *pixel_stride, int *byte_stride,
376 size_t *size, AllocType type)
377 {
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 */
420 *size = yuv422_afbc_luma_stride *height * 2 +
421 GRALLOC_ALIGN(nblocks * AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY, buffer_byte_alignment);
422 }
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 */
get_yuv_pX10_stride_and_size(int width,int height,int vss,int * pixel_stride,int * byte_stride,size_t * size)454 static bool get_yuv_pX10_stride_and_size(int width, int height, int vss, int *pixel_stride, int *byte_stride,
455 size_t *size)
456 {
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);
474 luma_byte_stride = GRALLOC_ALIGN(width * 2, YUV_MALI_PLANE_ALIGN);
475
476 if (size != NULL)
477 {
478 int chroma_size = GRALLOC_ALIGN(width * 2, YUV_MALI_PLANE_ALIGN) * (height / vss);
479 *size = luma_byte_stride *height + chroma_size;
480 }
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 */
get_yuv_y210_stride_and_size(int width,int height,int * pixel_stride,int * byte_stride,size_t * size)510 static bool get_yuv_y210_stride_and_size(int width, int height, int *pixel_stride, int *byte_stride, size_t *size)
511 {
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 */
519 y210_byte_stride = GRALLOC_ALIGN(width * 4, YUV_MALI_PLANE_ALIGN);
520
521 if (size != NULL)
522 {
523 *size = y210_byte_stride *height;
524 }
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 */
get_yuv_y0l2_stride_and_size(int width,int height,int * pixel_stride,int * byte_stride,size_t * size)557 static bool get_yuv_y0l2_stride_and_size(int width, int height, int *pixel_stride, int *byte_stride, size_t *size)
558 {
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);
566 y0l2_byte_stride = GRALLOC_ALIGN(width * 4, YUV_MALI_PLANE_ALIGN); /* 2 horiz pixels per 8 byte clump */
567
568 if (size != NULL)
569 {
570 *size = y0l2_byte_stride *height / 2; /* byte stride covers 2 vert pixels */
571 }
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 }
582
583 return true;
584 }
585
586 /*
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 */
get_yuv_y410_stride_and_size(int width,int height,int * pixel_stride,int * byte_stride,size_t * size)601 static bool get_yuv_y410_stride_and_size(int width, int height, int *pixel_stride, int *byte_stride, size_t *size)
602 {
603 int y410_byte_stride, y410_pixel_stride;
604
605 y410_pixel_stride = GRALLOC_ALIGN(width, YUV_MALI_PLANE_ALIGN);
606 y410_byte_stride = GRALLOC_ALIGN(width * 4, YUV_MALI_PLANE_ALIGN);
607
608 if (size != NULL)
609 {
610 /* 4x8bits per pixel */
611 *size = y410_byte_stride *height;
612 }
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 }
623
624 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 */
get_yuv420_10bit_afbc_stride_and_size(int width,int height,int * pixel_stride,int * byte_stride,size_t * size,AllocType type,int * internalHeight)645 static 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)
647 {
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
656 *internalHeight = height;
657 #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. */
660 *internalHeight += AFBC_PIXELS_PER_BLOCK;
661 #endif
662
663 /* 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);
674 height = GRALLOC_ALIGN(*internalHeight / 2, AFBC_TILED_HEADERS_BASIC_HEIGHT_ALIGN);
675 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);
680 height = GRALLOC_ALIGN(*internalHeight / 2, AFBC_TILED_HEADERS_WIDEBLK_HEIGHT_ALIGN);
681 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);
691 height = GRALLOC_ALIGN(*internalHeight / 2, AFBC_WIDEBLK_HEIGHT_ALIGN);
692 }
693 else
694 {
695 width = GRALLOC_ALIGN(width, AFBC_NORMAL_WIDTH_ALIGN);
696 height = GRALLOC_ALIGN(*internalHeight / 2, AFBC_NORMAL_HEIGHT_ALIGN);
697 }
698
699 yuv420_afbc_pixel_stride = GRALLOC_ALIGN(width, 16);
700 yuv420_afbc_byte_stride = GRALLOC_ALIGN(width * 4, 16); /* 64-bit packed and horizontally downsampled */
701
702 if (size != NULL)
703 {
704 int nblocks = width / AFBC_PIXELS_PER_BLOCK * (*internalHeight) / AFBC_PIXELS_PER_BLOCK;
705 *size = yuv420_afbc_byte_stride *height +
706 GRALLOC_ALIGN(nblocks * AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY, buffer_byte_alignment);
707 }
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 */
get_yuv422_10bit_afbc_stride_and_size(int width,int height,int * pixel_stride,int * byte_stride,size_t * size,AllocType type)738 static bool get_yuv422_10bit_afbc_stride_and_size(int width, int height, int *pixel_stride, int *byte_stride,
739 size_t *size, AllocType type)
740 {
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);
783 yuv422_afbc_byte_stride = GRALLOC_ALIGN(width * 2, 16);
784
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 */
789 *size = yuv422_afbc_byte_stride *height * 2 +
790 GRALLOC_ALIGN(nblocks * AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY, buffer_byte_alignment);
791 }
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 */
get_camera_formats_stride_and_size(int w,int h,uint64_t format,int * out_stride,size_t * out_size)821 static 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 {
827 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;
832
833 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.");
837 return false;
838 }
839
840 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;
868 }
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
mali_gralloc_buffer_allocate(mali_gralloc_module * m,const gralloc_buffer_descriptor_t * descriptors,uint32_t numDescriptors,buffer_handle_t * pHandle,bool * shared_backend)883 int 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)
885 {
886 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;
892
893 for (i = 0; i < numDescriptors; i++)
894 {
895 buffer_descriptor_t *bufDescriptor = (buffer_descriptor_t *)(descriptors[i]);
896
897 /* 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;
903
904 bufDescriptor->internal_format = mali_gralloc_select_format(
905 bufDescriptor->hal_format, bufDescriptor->format_type, usage, bufDescriptor->width * bufDescriptor->height);
906
907 if (bufDescriptor->internal_format == 0)
908 {
909 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)
918 {
919 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 }
932 }
933 else if (usage & MALI_GRALLOC_USAGE_AFBC_PADDING)
934 {
935 alloc_type = AFBC_PADDED;
936 }
937 else if (bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_AFBC_WIDEBLK)
938 {
939 alloc_type = AFBC_WIDEBLK;
940 }
941 else
942 {
943 alloc_type = AFBC;
944 }
945 }
946
947 uint64_t base_format = bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_FMT_MASK;
948
949 switch (base_format)
950 {
951 case 43:
952 case HAL_PIXEL_FORMAT_RGBA_8888:
953 case HAL_PIXEL_FORMAT_RGBX_8888:
954 case HAL_PIXEL_FORMAT_BGRA_8888:
955 get_rgb_stride_and_size(bufDescriptor->width, bufDescriptor->height, 4, &bufDescriptor->pixel_stride,
956 &bufDescriptor->byte_stride, &bufDescriptor->size, alloc_type);
957 break;
958
959 case HAL_PIXEL_FORMAT_RGB_888:
960 get_rgb_stride_and_size(bufDescriptor->width, bufDescriptor->height, 3, &bufDescriptor->pixel_stride,
961 &bufDescriptor->byte_stride, &bufDescriptor->size, alloc_type);
962 break;
963
964 case HAL_PIXEL_FORMAT_RGB_565:
965 get_rgb_stride_and_size(bufDescriptor->width, bufDescriptor->height, 2, &bufDescriptor->pixel_stride,
966 &bufDescriptor->byte_stride, &bufDescriptor->size, alloc_type);
967 break;
968
969 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
970 case MALI_GRALLOC_FORMAT_INTERNAL_YV12:
971 case MALI_GRALLOC_FORMAT_INTERNAL_NV12:
972 case MALI_GRALLOC_FORMAT_INTERNAL_NV21:
973 {
974 /* Mali subsystem prefers higher stride alignment values (128 bytes) for YUV, but software components assume
975 * default of 16. We only need to care about YV12 as it's the only, implicit, HAL YUV format in Android.
976 */
977 int yv12_align = YUV_MALI_PLANE_ALIGN;
978
979 if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK))
980 {
981 yv12_align = YUV_ANDROID_PLANE_ALIGN;
982 }
983
984 if (!get_yv12_stride_and_size(bufDescriptor->width, bufDescriptor->height, &bufDescriptor->pixel_stride,
985 &bufDescriptor->byte_stride, &bufDescriptor->size, alloc_type,
986 &bufDescriptor->internalHeight, yv12_align))
987 {
988 return -EINVAL;
989 }
990
991 break;
992 }
993
994 case HAL_PIXEL_FORMAT_YCbCr_422_I:
995 {
996 /* YUYV 4:2:2 */
997 if (alloc_type != UNCOMPRESSED ||
998 !get_yuv422_8bit_stride_and_size(bufDescriptor->width, bufDescriptor->height,
999 &bufDescriptor->pixel_stride, &bufDescriptor->byte_stride,
1000 &bufDescriptor->size))
1001 {
1002 return -EINVAL;
1003 }
1004
1005 break;
1006 }
1007
1008 case HAL_PIXEL_FORMAT_RAW16:
1009 case HAL_PIXEL_FORMAT_RAW12:
1010 case HAL_PIXEL_FORMAT_RAW10:
1011 case HAL_PIXEL_FORMAT_BLOB:
1012 if (alloc_type != UNCOMPRESSED)
1013 {
1014 return -EINVAL;
1015 }
1016
1017 get_camera_formats_stride_and_size(bufDescriptor->width, bufDescriptor->height, base_format,
1018 &bufDescriptor->pixel_stride, &bufDescriptor->size);
1019 /* For Raw/Blob formats stride is defined to be either in bytes or pixels per format */
1020 bufDescriptor->byte_stride = bufDescriptor->pixel_stride;
1021 break;
1022
1023 case MALI_GRALLOC_FORMAT_INTERNAL_Y0L2:
1024
1025 /* YUYAAYUVAA 4:2:0 with and without AFBC */
1026 if (alloc_type != UNCOMPRESSED)
1027 {
1028 if (!get_yuv420_10bit_afbc_stride_and_size(
1029 bufDescriptor->width, bufDescriptor->height, &bufDescriptor->pixel_stride,
1030 &bufDescriptor->byte_stride, &bufDescriptor->size, alloc_type, &bufDescriptor->internalHeight))
1031 {
1032 return -EINVAL;
1033 }
1034 }
1035 else
1036 {
1037 if (!get_yuv_y0l2_stride_and_size(bufDescriptor->width, bufDescriptor->height,
1038 &bufDescriptor->pixel_stride, &bufDescriptor->byte_stride,
1039 &bufDescriptor->size))
1040 {
1041 return -EINVAL;
1042 }
1043 }
1044
1045 break;
1046
1047 case MALI_GRALLOC_FORMAT_INTERNAL_P010:
1048
1049 /* Y-UV 4:2:0 */
1050 if (alloc_type != UNCOMPRESSED ||
1051 !get_yuv_pX10_stride_and_size(bufDescriptor->width, bufDescriptor->height, 2,
1052 &bufDescriptor->pixel_stride, &bufDescriptor->byte_stride,
1053 &bufDescriptor->size))
1054 {
1055 return -EINVAL;
1056 }
1057
1058 break;
1059
1060 case MALI_GRALLOC_FORMAT_INTERNAL_P210:
1061
1062 /* Y-UV 4:2:2 */
1063 if (alloc_type != UNCOMPRESSED ||
1064 !get_yuv_pX10_stride_and_size(bufDescriptor->width, bufDescriptor->height, 1,
1065 &bufDescriptor->pixel_stride, &bufDescriptor->byte_stride,
1066 &bufDescriptor->size))
1067 {
1068 return -EINVAL;
1069 }
1070
1071 break;
1072
1073 case MALI_GRALLOC_FORMAT_INTERNAL_Y210:
1074
1075 /* YUYV 4:2:2 with and without AFBC */
1076 if (alloc_type != UNCOMPRESSED)
1077 {
1078 if (!get_yuv422_10bit_afbc_stride_and_size(bufDescriptor->width, bufDescriptor->height,
1079 &bufDescriptor->pixel_stride, &bufDescriptor->byte_stride,
1080 &bufDescriptor->size, alloc_type))
1081 {
1082 return -EINVAL;
1083 }
1084 }
1085 else
1086 {
1087 if (!get_yuv_y210_stride_and_size(bufDescriptor->width, bufDescriptor->height,
1088 &bufDescriptor->pixel_stride, &bufDescriptor->byte_stride,
1089 &bufDescriptor->size))
1090 {
1091 return -EINVAL;
1092 }
1093 }
1094
1095 break;
1096
1097 case MALI_GRALLOC_FORMAT_INTERNAL_Y410:
1098
1099 /* AVYU 2-10-10-10 */
1100 if (alloc_type != UNCOMPRESSED ||
1101 !get_yuv_y410_stride_and_size(bufDescriptor->width, bufDescriptor->height, &bufDescriptor->pixel_stride,
1102 &bufDescriptor->byte_stride, &bufDescriptor->size))
1103 {
1104 return -EINVAL;
1105 }
1106
1107 break;
1108
1109 case MALI_GRALLOC_FORMAT_INTERNAL_YUV422_8BIT:
1110
1111 /* 8BIT AFBC YUV4:2:2 testing usage */
1112
1113 /* We only support compressed for this format right now.
1114 * Below will fail in case format is uncompressed.
1115 */
1116 if (!get_afbc_yuv422_8bit_stride_and_size(bufDescriptor->width, bufDescriptor->height,
1117 &bufDescriptor->pixel_stride, &bufDescriptor->byte_stride,
1118 &bufDescriptor->size, alloc_type))
1119 {
1120 return -EINVAL;
1121 }
1122
1123 break;
1124
1125 /*
1126 * Additional custom formats can be added here
1127 * and must fill the variables pixel_stride, byte_stride and size.
1128 */
1129 default:
1130 return -EINVAL;
1131 }
1132
1133 /*
1134 * Each layer of a multi-layer buffer must be aligned so that
1135 * it is accessible by both producer and consumer. In most cases,
1136 * the stride alignment is also sufficient for each layer, however
1137 * for AFBC the header buffer alignment is more constrained (see
1138 * AFBC specification v3.4, section 2.15: "Alignment requirements").
1139 * Also update the buffer size to accommodate all layers.
1140 */
1141 if (bufDescriptor->layer_count > 1)
1142 {
1143 if (bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_AFBCENABLE_MASK)
1144 {
1145 if (bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS)
1146 {
1147 bufDescriptor->size = GRALLOC_ALIGN(bufDescriptor->size, 4096);
1148 }
1149 else
1150 {
1151 bufDescriptor->size = GRALLOC_ALIGN(bufDescriptor->size, 128);
1152 }
1153 }
1154
1155 bufDescriptor->size *= bufDescriptor->layer_count;
1156 }
1157 }
1158
1159 {
1160 /* Allocate ION backing store memory */
1161 err = mali_gralloc_ion_allocate(m, descriptors, numDescriptors, pHandle, &shared);
1162
1163 if (err < 0)
1164 {
1165 ALOGE("mali_gralloc_ion_allocate return error");
1166 return err;
1167 }
1168 }
1169
1170 if (shared)
1171 {
1172 backing_store_id = getUniqueId();
1173 }
1174
1175 for (i = 0; i < numDescriptors; i++)
1176 {
1177 buffer_descriptor_t *bufDescriptor = (buffer_descriptor_t *)descriptors[i];
1178 private_handle_t *hnd = (private_handle_t *)pHandle[i];
1179
1180 usage = bufDescriptor->consumer_usage | bufDescriptor->producer_usage;
1181
1182 err = gralloc_buffer_attr_allocate(hnd);
1183
1184 if (err < 0)
1185 {
1186 ALOGE("gralloc_buffer_attr_allocate return error");
1187 /* free all allocated ion buffer& attr buffer here.*/
1188 mali_gralloc_buffer_free_internal(pHandle, numDescriptors);
1189 return err;
1190 }
1191
1192 mali_gralloc_dump_buffer_add(hnd);
1193
1194 switch (usage & MALI_GRALLOC_USAGE_YUV_CONF_MASK)
1195 {
1196 case MALI_GRALLOC_USAGE_YUV_CONF_0:
1197 hnd->yuv_info = MALI_YUV_BT601_NARROW;
1198 break;
1199
1200 case MALI_GRALLOC_USAGE_YUV_CONF_1:
1201 hnd->yuv_info = MALI_YUV_BT601_WIDE;
1202 break;
1203
1204 case MALI_GRALLOC_USAGE_YUV_CONF_2:
1205 hnd->yuv_info = MALI_YUV_BT709_NARROW;
1206 break;
1207
1208 case MALI_GRALLOC_USAGE_YUV_CONF_3:
1209 hnd->yuv_info = MALI_YUV_BT709_WIDE;
1210 break;
1211 }
1212
1213 /* Workaround 10bit YUV only support BT709_WIDE in GPU DDK */
1214 if ((bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_FMT_MASK) == MALI_GRALLOC_FORMAT_INTERNAL_Y0L2)
1215 {
1216 hnd->yuv_info = MALI_YUV_BT709_WIDE;
1217 }
1218
1219 if (shared)
1220 {
1221 /*each buffer will share the same backing store id.*/
1222 hnd->backing_store_id = backing_store_id;
1223 }
1224 else
1225 {
1226 /* each buffer will have an unique backing store id.*/
1227 hnd->backing_store_id = getUniqueId();
1228 }
1229 }
1230
1231 if (NULL != shared_backend)
1232 {
1233 *shared_backend = shared;
1234 }
1235
1236 return 0;
1237 }
1238
mali_gralloc_buffer_free(buffer_handle_t pHandle)1239 int mali_gralloc_buffer_free(buffer_handle_t pHandle)
1240 {
1241 int rval = -1;
1242 private_handle_t *hnd = (private_handle_t *)(pHandle);
1243
1244 if (hnd != NULL)
1245 {
1246 rval = gralloc_buffer_attr_free(hnd);
1247 mali_gralloc_ion_free(hnd);
1248 }
1249
1250 return rval;
1251 }
1252
mali_gralloc_buffer_free_internal(buffer_handle_t * pHandle,uint32_t num_hnds)1253 static int mali_gralloc_buffer_free_internal(buffer_handle_t *pHandle, uint32_t num_hnds)
1254 {
1255 int err = -1;
1256 uint32_t i = 0;
1257
1258 for (i = 0; i < num_hnds; i++)
1259 {
1260 private_handle_t *hnd = (private_handle_t *)(pHandle[i]);
1261
1262 err = gralloc_buffer_attr_free(hnd);
1263 mali_gralloc_ion_free(hnd);
1264 }
1265
1266 return err;
1267 }
1268