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 int64_t *req_wh = new int64_t[numDescriptors];
893
894 for (i = 0; i < numDescriptors; i++)
895 {
896 buffer_descriptor_t *bufDescriptor = (buffer_descriptor_t *)(descriptors[i]);
897
898 /* Some formats require an internal width and height that may be used by
899 * consumers/producers.
900 */
901 bufDescriptor->internalWidth = bufDescriptor->width;
902 bufDescriptor->internalHeight = bufDescriptor->height;
903 usage = bufDescriptor->producer_usage | bufDescriptor->consumer_usage;
904
905 req_wh[i] = bufDescriptor->width | (bufDescriptor->height << 16);
906
907 bufDescriptor->internal_format = mali_gralloc_select_format(
908 bufDescriptor->hal_format, bufDescriptor->format_type, usage, bufDescriptor->width * bufDescriptor->height);
909
910 if (bufDescriptor->internal_format == 0)
911 {
912 ALOGE("Unrecognized and/or unsupported format 0x%" PRIx64 " and usage 0x%" PRIx64,
913 bufDescriptor->hal_format, usage);
914 delete []req_wh;
915 return -EINVAL;
916 }
917
918 /* Determine AFBC type for this format */
919 if (bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_AFBCENABLE_MASK)
920 {
921 if (bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS)
922 {
923 if (bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_AFBC_WIDEBLK)
924 {
925 alloc_type = AFBC_TILED_HEADERS_WIDEBLK;
926 }
927 else if (bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_AFBC_BASIC)
928 {
929 alloc_type = AFBC_TILED_HEADERS_BASIC;
930 }
931 else if (bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_AFBC_SPLITBLK)
932 {
933 ALOGE("Unsupported format. Splitblk in tiled header configuration.");
934 delete []req_wh;
935 return -EINVAL;
936 }
937 }
938 else if (usage & MALI_GRALLOC_USAGE_AFBC_PADDING)
939 {
940 alloc_type = AFBC_PADDED;
941 }
942 else if (bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_AFBC_WIDEBLK)
943 {
944 alloc_type = AFBC_WIDEBLK;
945 }
946 else
947 {
948 alloc_type = AFBC;
949 }
950 }
951
952 uint64_t base_format = bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_FMT_MASK;
953
954 switch (base_format)
955 {
956 case 43:
957 case HAL_PIXEL_FORMAT_RGBA_8888:
958 case HAL_PIXEL_FORMAT_RGBX_8888:
959 case HAL_PIXEL_FORMAT_BGRA_8888:
960 get_rgb_stride_and_size(bufDescriptor->width, bufDescriptor->height, 4, &bufDescriptor->pixel_stride,
961 &bufDescriptor->byte_stride, &bufDescriptor->size, alloc_type);
962 break;
963
964 case HAL_PIXEL_FORMAT_RGB_888:
965 get_rgb_stride_and_size(bufDescriptor->width, bufDescriptor->height, 3, &bufDescriptor->pixel_stride,
966 &bufDescriptor->byte_stride, &bufDescriptor->size, alloc_type);
967 break;
968
969 case HAL_PIXEL_FORMAT_RGB_565:
970 get_rgb_stride_and_size(bufDescriptor->width, bufDescriptor->height, 2, &bufDescriptor->pixel_stride,
971 &bufDescriptor->byte_stride, &bufDescriptor->size, alloc_type);
972 break;
973
974 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
975 case MALI_GRALLOC_FORMAT_INTERNAL_YV12:
976 case MALI_GRALLOC_FORMAT_INTERNAL_NV12:
977 case MALI_GRALLOC_FORMAT_INTERNAL_NV21:
978 {
979 /* Mali subsystem prefers higher stride alignment values (128 bytes) for YUV, but software components assume
980 * default of 16. We only need to care about YV12 as it's the only, implicit, HAL YUV format in Android.
981 */
982 int yv12_align = YUV_MALI_PLANE_ALIGN;
983
984 if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK))
985 {
986 yv12_align = YUV_ANDROID_PLANE_ALIGN;
987 }
988
989 if (!get_yv12_stride_and_size(bufDescriptor->width, bufDescriptor->height, &bufDescriptor->pixel_stride,
990 &bufDescriptor->byte_stride, &bufDescriptor->size, alloc_type,
991 &bufDescriptor->internalHeight, yv12_align))
992 {
993 delete []req_wh;
994 return -EINVAL;
995 }
996
997 break;
998 }
999
1000 case HAL_PIXEL_FORMAT_YCbCr_422_I:
1001 {
1002 /* YUYV 4:2:2 */
1003 if (alloc_type != UNCOMPRESSED ||
1004 !get_yuv422_8bit_stride_and_size(bufDescriptor->width, bufDescriptor->height,
1005 &bufDescriptor->pixel_stride, &bufDescriptor->byte_stride,
1006 &bufDescriptor->size))
1007 {
1008 delete []req_wh;
1009 return -EINVAL;
1010 }
1011
1012 break;
1013 }
1014
1015 case HAL_PIXEL_FORMAT_RAW16:
1016 case HAL_PIXEL_FORMAT_RAW12:
1017 case HAL_PIXEL_FORMAT_RAW10:
1018 case HAL_PIXEL_FORMAT_BLOB:
1019 if (alloc_type != UNCOMPRESSED)
1020 {
1021 delete []req_wh;
1022 return -EINVAL;
1023 }
1024
1025 get_camera_formats_stride_and_size(bufDescriptor->width, bufDescriptor->height, base_format,
1026 &bufDescriptor->pixel_stride, &bufDescriptor->size);
1027 /* For Raw/Blob formats stride is defined to be either in bytes or pixels per format */
1028 bufDescriptor->byte_stride = bufDescriptor->pixel_stride;
1029 break;
1030
1031 case MALI_GRALLOC_FORMAT_INTERNAL_Y0L2:
1032
1033 /* YUYAAYUVAA 4:2:0 with and without AFBC */
1034 if (alloc_type != UNCOMPRESSED)
1035 {
1036 if (!get_yuv420_10bit_afbc_stride_and_size(
1037 bufDescriptor->width, bufDescriptor->height, &bufDescriptor->pixel_stride,
1038 &bufDescriptor->byte_stride, &bufDescriptor->size, alloc_type, &bufDescriptor->internalHeight))
1039 {
1040 delete []req_wh;
1041 return -EINVAL;
1042 }
1043 }
1044 else
1045 {
1046 if (!get_yuv_y0l2_stride_and_size(bufDescriptor->width, bufDescriptor->height,
1047 &bufDescriptor->pixel_stride, &bufDescriptor->byte_stride,
1048 &bufDescriptor->size))
1049 {
1050 delete []req_wh;
1051 return -EINVAL;
1052 }
1053 }
1054
1055 break;
1056
1057 case MALI_GRALLOC_FORMAT_INTERNAL_P010:
1058
1059 /* Y-UV 4:2:0 */
1060 if (alloc_type != UNCOMPRESSED ||
1061 !get_yuv_pX10_stride_and_size(bufDescriptor->width, bufDescriptor->height, 2,
1062 &bufDescriptor->pixel_stride, &bufDescriptor->byte_stride,
1063 &bufDescriptor->size))
1064 {
1065 delete []req_wh;
1066 return -EINVAL;
1067 }
1068
1069 break;
1070
1071 case MALI_GRALLOC_FORMAT_INTERNAL_P210:
1072
1073 /* Y-UV 4:2:2 */
1074 if (alloc_type != UNCOMPRESSED ||
1075 !get_yuv_pX10_stride_and_size(bufDescriptor->width, bufDescriptor->height, 1,
1076 &bufDescriptor->pixel_stride, &bufDescriptor->byte_stride,
1077 &bufDescriptor->size))
1078 {
1079 delete []req_wh;
1080 return -EINVAL;
1081 }
1082
1083 break;
1084
1085 case MALI_GRALLOC_FORMAT_INTERNAL_Y210:
1086
1087 /* YUYV 4:2:2 with and without AFBC */
1088 if (alloc_type != UNCOMPRESSED)
1089 {
1090 if (!get_yuv422_10bit_afbc_stride_and_size(bufDescriptor->width, bufDescriptor->height,
1091 &bufDescriptor->pixel_stride, &bufDescriptor->byte_stride,
1092 &bufDescriptor->size, alloc_type))
1093 {
1094 delete []req_wh;
1095 return -EINVAL;
1096 }
1097 }
1098 else
1099 {
1100 if (!get_yuv_y210_stride_and_size(bufDescriptor->width, bufDescriptor->height,
1101 &bufDescriptor->pixel_stride, &bufDescriptor->byte_stride,
1102 &bufDescriptor->size))
1103 {
1104 delete []req_wh;
1105 return -EINVAL;
1106 }
1107 }
1108
1109 break;
1110
1111 case MALI_GRALLOC_FORMAT_INTERNAL_Y410:
1112
1113 /* AVYU 2-10-10-10 */
1114 if (alloc_type != UNCOMPRESSED ||
1115 !get_yuv_y410_stride_and_size(bufDescriptor->width, bufDescriptor->height, &bufDescriptor->pixel_stride,
1116 &bufDescriptor->byte_stride, &bufDescriptor->size))
1117 {
1118 delete []req_wh;
1119 return -EINVAL;
1120 }
1121
1122 break;
1123
1124 case MALI_GRALLOC_FORMAT_INTERNAL_YUV422_8BIT:
1125
1126 /* 8BIT AFBC YUV4:2:2 testing usage */
1127
1128 /* We only support compressed for this format right now.
1129 * Below will fail in case format is uncompressed.
1130 */
1131 if (!get_afbc_yuv422_8bit_stride_and_size(bufDescriptor->width, bufDescriptor->height,
1132 &bufDescriptor->pixel_stride, &bufDescriptor->byte_stride,
1133 &bufDescriptor->size, alloc_type))
1134 {
1135 delete []req_wh;
1136 return -EINVAL;
1137 }
1138
1139 break;
1140
1141 /*
1142 * Additional custom formats can be added here
1143 * and must fill the variables pixel_stride, byte_stride and size.
1144 */
1145 default:
1146 delete []req_wh;
1147 return -EINVAL;
1148 }
1149
1150 /*
1151 * Each layer of a multi-layer buffer must be aligned so that
1152 * it is accessible by both producer and consumer. In most cases,
1153 * the stride alignment is also sufficient for each layer, however
1154 * for AFBC the header buffer alignment is more constrained (see
1155 * AFBC specification v3.4, section 2.15: "Alignment requirements").
1156 * Also update the buffer size to accommodate all layers.
1157 */
1158 if (bufDescriptor->layer_count > 1)
1159 {
1160 if (bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_AFBCENABLE_MASK)
1161 {
1162 if (bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_AFBC_TILED_HEADERS)
1163 {
1164 bufDescriptor->size = GRALLOC_ALIGN(bufDescriptor->size, 4096);
1165 }
1166 else
1167 {
1168 bufDescriptor->size = GRALLOC_ALIGN(bufDescriptor->size, 128);
1169 }
1170 }
1171
1172 bufDescriptor->size *= bufDescriptor->layer_count;
1173 }
1174 }
1175
1176 {
1177 /* Allocate ION backing store memory */
1178 err = mali_gralloc_ion_allocate(m, descriptors, numDescriptors, pHandle, &shared);
1179
1180 if (err < 0)
1181 {
1182 ALOGE("mali_gralloc_ion_allocate return error");
1183 delete []req_wh;
1184 return err;
1185 }
1186 }
1187
1188 if (shared)
1189 {
1190 backing_store_id = getUniqueId();
1191 }
1192
1193 for (i = 0; i < numDescriptors; i++)
1194 {
1195 buffer_descriptor_t *bufDescriptor = (buffer_descriptor_t *)descriptors[i];
1196 private_handle_t *hnd = (private_handle_t *)pHandle[i];
1197
1198 usage = bufDescriptor->consumer_usage | bufDescriptor->producer_usage;
1199
1200 err = gralloc_buffer_attr_allocate(hnd);
1201
1202 if (err < 0)
1203 {
1204 ALOGE("gralloc_buffer_attr_allocate return error");
1205 /* free all allocated ion buffer& attr buffer here.*/
1206 mali_gralloc_buffer_free_internal(pHandle, numDescriptors);
1207 delete []req_wh;
1208 return err;
1209 }
1210
1211 mali_gralloc_dump_buffer_add(hnd);
1212
1213 switch (usage & MALI_GRALLOC_USAGE_YUV_CONF_MASK)
1214 {
1215 case MALI_GRALLOC_USAGE_YUV_CONF_0:
1216 hnd->yuv_info = MALI_YUV_BT601_NARROW;
1217 break;
1218
1219 case MALI_GRALLOC_USAGE_YUV_CONF_1:
1220 hnd->yuv_info = MALI_YUV_BT601_WIDE;
1221 break;
1222
1223 case MALI_GRALLOC_USAGE_YUV_CONF_2:
1224 hnd->yuv_info = MALI_YUV_BT709_NARROW;
1225 break;
1226
1227 case MALI_GRALLOC_USAGE_YUV_CONF_3:
1228 hnd->yuv_info = MALI_YUV_BT709_WIDE;
1229 break;
1230 }
1231
1232 /* Workaround 10bit YUV only support BT709_WIDE in GPU DDK */
1233 if ((bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_FMT_MASK) == MALI_GRALLOC_FORMAT_INTERNAL_Y0L2)
1234 {
1235 hnd->yuv_info = MALI_YUV_BT709_WIDE;
1236 }
1237
1238 if (shared)
1239 {
1240 /*each buffer will share the same backing store id.*/
1241 hnd->backing_store_id = backing_store_id;
1242 }
1243 else
1244 {
1245 /* each buffer will have an unique backing store id.*/
1246 hnd->backing_store_id = getUniqueId();
1247 }
1248
1249 hnd->req_width = req_wh[i] & 0xffff;
1250 hnd->req_height = req_wh[i] >> 16;
1251 }
1252
1253 if (NULL != shared_backend)
1254 {
1255 *shared_backend = shared;
1256 }
1257
1258 delete []req_wh;
1259 return 0;
1260 }
1261
mali_gralloc_buffer_free(buffer_handle_t pHandle)1262 int mali_gralloc_buffer_free(buffer_handle_t pHandle)
1263 {
1264 int rval = -1;
1265 private_handle_t *hnd = (private_handle_t *)(pHandle);
1266
1267 if (hnd != NULL)
1268 {
1269 rval = gralloc_buffer_attr_free(hnd);
1270 mali_gralloc_ion_free(hnd);
1271 }
1272
1273 return rval;
1274 }
1275
mali_gralloc_buffer_free_internal(buffer_handle_t * pHandle,uint32_t num_hnds)1276 static int mali_gralloc_buffer_free_internal(buffer_handle_t *pHandle, uint32_t num_hnds)
1277 {
1278 int err = -1;
1279 uint32_t i = 0;
1280
1281 for (i = 0; i < num_hnds; i++)
1282 {
1283 private_handle_t *hnd = (private_handle_t *)(pHandle[i]);
1284
1285 err = gralloc_buffer_attr_free(hnd);
1286 mali_gralloc_ion_free(hnd);
1287 }
1288
1289 return err;
1290 }
1291