John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1 | /* |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 2 | * Copyright (C) 2016-2017 ARM Limited. All rights reserved. |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 19 | #include <hardware/hardware.h> |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 20 | #include <inttypes.h> |
| 21 | #include <atomic> |
| 22 | |
| 23 | #if GRALLOC_USE_GRALLOC1_API == 1 |
| 24 | #include <hardware/gralloc1.h> |
| 25 | #else |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 26 | #include <hardware/gralloc.h> |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 27 | #endif |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 28 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 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" |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 34 | #include "gralloc_buffer_priv.h" |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 35 | #include "mali_gralloc_bufferdescriptor.h" |
| 36 | #include "mali_gralloc_debug.h" |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 37 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 38 | #define AFBC_PIXELS_PER_BLOCK 16 |
| 39 | #define AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY 16 |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 40 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 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 |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 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 |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 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 | */ |
| 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 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 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) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 116 | *size = stride *height; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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; |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 132 | int h_aligned = GRALLOC_ALIGN(height, AFBC_NORMAL_HEIGHT_ALIGN); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 133 | int nblocks; |
| 134 | int buffer_byte_alignment = AFBC_BODY_BUFFER_BYTE_ALIGNMENT; |
| 135 | |
| 136 | if (type == AFBC_TILED_HEADERS_BASIC) |
| 137 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 138 | w_aligned = GRALLOC_ALIGN(width, AFBC_TILED_HEADERS_BASIC_WIDTH_ALIGN); |
| 139 | h_aligned = GRALLOC_ALIGN(height, AFBC_TILED_HEADERS_BASIC_HEIGHT_ALIGN); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 140 | buffer_byte_alignment = 4 * AFBC_BODY_BUFFER_BYTE_ALIGNMENT; |
| 141 | } |
| 142 | else if (type == AFBC_TILED_HEADERS_WIDEBLK) |
| 143 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 144 | w_aligned = GRALLOC_ALIGN(width, AFBC_TILED_HEADERS_WIDEBLK_WIDTH_ALIGN); |
| 145 | h_aligned = GRALLOC_ALIGN(height, AFBC_TILED_HEADERS_WIDEBLK_HEIGHT_ALIGN); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 146 | buffer_byte_alignment = 4 * AFBC_BODY_BUFFER_BYTE_ALIGNMENT; |
| 147 | } |
| 148 | else if (type == AFBC_PADDED) |
| 149 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 150 | w_aligned = GRALLOC_ALIGN(width, 64); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 151 | } |
| 152 | else if (type == AFBC_WIDEBLK) |
| 153 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 154 | w_aligned = GRALLOC_ALIGN(width, AFBC_WIDEBLK_WIDTH_ALIGN); |
| 155 | h_aligned = GRALLOC_ALIGN(height, AFBC_WIDEBLK_HEIGHT_ALIGN); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 156 | } |
| 157 | else |
| 158 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 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; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | nblocks = w_aligned / AFBC_PIXELS_PER_BLOCK * h_aligned / AFBC_PIXELS_PER_BLOCK; |
| 176 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 177 | if (size != NULL) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 178 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 179 | *size = stride *h_aligned + |
| 180 | GRALLOC_ALIGN(nblocks * AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY, buffer_byte_alignment); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 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) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 226 | width = GRALLOC_ALIGN(width, AFBC_TILED_HEADERS_BASIC_WIDTH_ALIGN); |
| 227 | height = GRALLOC_ALIGN(*internalHeight, AFBC_TILED_HEADERS_BASIC_HEIGHT_ALIGN); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 228 | buffer_byte_alignment = 4 * AFBC_BODY_BUFFER_BYTE_ALIGNMENT; |
| 229 | } |
| 230 | else if (type == AFBC_TILED_HEADERS_WIDEBLK) |
| 231 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 232 | width = GRALLOC_ALIGN(width, AFBC_TILED_HEADERS_WIDEBLK_WIDTH_ALIGN); |
| 233 | height = GRALLOC_ALIGN(*internalHeight, AFBC_TILED_HEADERS_WIDEBLK_HEIGHT_ALIGN); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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); |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 244 | height = GRALLOC_ALIGN(*internalHeight, AFBC_WIDEBLK_HEIGHT_ALIGN); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 245 | } |
| 246 | else |
| 247 | { |
| 248 | width = GRALLOC_ALIGN(width, AFBC_NORMAL_WIDTH_ALIGN); |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 249 | height = GRALLOC_ALIGN(*internalHeight, AFBC_NORMAL_HEIGHT_ALIGN); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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) */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 259 | *size = (yuv420_afbc_luma_stride + yuv420_afbc_chroma_stride) * height + |
| 260 | GRALLOC_ALIGN(nblocks * AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY, buffer_byte_alignment); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 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) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 291 | { |
| 292 | int luma_stride; |
| 293 | |
| 294 | if (type != UNCOMPRESSED) |
| 295 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 296 | return get_afbc_yuv420_8bit_stride_and_size(width, height, pixel_stride, byte_stride, size, type, |
| 297 | internalHeight); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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)). */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 310 | *size = height *(luma_stride + chroma_stride); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | } |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 325 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 336 | static bool get_yuv422_8bit_stride_and_size(int width, int height, int *pixel_stride, int *byte_stride, size_t *size) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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); |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 344 | local_byte_stride = GRALLOC_ALIGN(width * 2, YUV_MALI_PLANE_ALIGN); /* 4 bytes per 2 pixels */ |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 345 | |
| 346 | if (size != NULL) |
| 347 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 348 | *size = local_byte_stride *height; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 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) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 420 | *size = yuv422_afbc_luma_stride *height * 2 + |
| 421 | GRALLOC_ALIGN(nblocks * AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY, buffer_byte_alignment); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 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) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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); |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 474 | luma_byte_stride = GRALLOC_ALIGN(width * 2, YUV_MALI_PLANE_ALIGN); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 475 | |
| 476 | if (size != NULL) |
| 477 | { |
| 478 | int chroma_size = GRALLOC_ALIGN(width * 2, YUV_MALI_PLANE_ALIGN) * (height / vss); |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 479 | *size = luma_byte_stride *height + chroma_size; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 510 | static bool get_yuv_y210_stride_and_size(int width, int height, int *pixel_stride, int *byte_stride, size_t *size) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 519 | y210_byte_stride = GRALLOC_ALIGN(width * 4, YUV_MALI_PLANE_ALIGN); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 520 | |
| 521 | if (size != NULL) |
| 522 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 523 | *size = y210_byte_stride *height; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 557 | static bool get_yuv_y0l2_stride_and_size(int width, int height, int *pixel_stride, int *byte_stride, size_t *size) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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); |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 566 | y0l2_byte_stride = GRALLOC_ALIGN(width * 4, YUV_MALI_PLANE_ALIGN); /* 2 horiz pixels per 8 byte clump */ |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 567 | |
| 568 | if (size != NULL) |
| 569 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 570 | *size = y0l2_byte_stride *height / 2; /* byte stride covers 2 vert pixels */ |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | } |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 582 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 583 | return true; |
| 584 | } |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 585 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 601 | static bool get_yuv_y410_stride_and_size(int width, int height, int *pixel_stride, int *byte_stride, size_t *size) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 602 | { |
| 603 | int y410_byte_stride, y410_pixel_stride; |
| 604 | |
| 605 | y410_pixel_stride = GRALLOC_ALIGN(width, YUV_MALI_PLANE_ALIGN); |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 606 | y410_byte_stride = GRALLOC_ALIGN(width * 4, YUV_MALI_PLANE_ALIGN); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 607 | |
| 608 | if (size != NULL) |
| 609 | { |
| 610 | /* 4x8bits per pixel */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 611 | *size = y410_byte_stride *height; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | } |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 623 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 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) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 656 | *internalHeight = height; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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. */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 660 | *internalHeight += AFBC_PIXELS_PER_BLOCK; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 661 | #endif |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 662 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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); |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 674 | height = GRALLOC_ALIGN(*internalHeight / 2, AFBC_TILED_HEADERS_BASIC_HEIGHT_ALIGN); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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); |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 680 | height = GRALLOC_ALIGN(*internalHeight / 2, AFBC_TILED_HEADERS_WIDEBLK_HEIGHT_ALIGN); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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); |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 691 | height = GRALLOC_ALIGN(*internalHeight / 2, AFBC_WIDEBLK_HEIGHT_ALIGN); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 692 | } |
| 693 | else |
| 694 | { |
| 695 | width = GRALLOC_ALIGN(width, AFBC_NORMAL_WIDTH_ALIGN); |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 696 | height = GRALLOC_ALIGN(*internalHeight / 2, AFBC_NORMAL_HEIGHT_ALIGN); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | yuv420_afbc_pixel_stride = GRALLOC_ALIGN(width, 16); |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 700 | yuv420_afbc_byte_stride = GRALLOC_ALIGN(width * 4, 16); /* 64-bit packed and horizontally downsampled */ |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 701 | |
| 702 | if (size != NULL) |
| 703 | { |
| 704 | int nblocks = width / AFBC_PIXELS_PER_BLOCK * (*internalHeight) / AFBC_PIXELS_PER_BLOCK; |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 705 | *size = yuv420_afbc_byte_stride *height + |
| 706 | GRALLOC_ALIGN(nblocks * AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY, buffer_byte_alignment); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 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) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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); |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 783 | yuv422_afbc_byte_stride = GRALLOC_ALIGN(width * 2, 16); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 789 | *size = yuv422_afbc_byte_stride *height * 2 + |
| 790 | GRALLOC_ALIGN(nblocks * AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY, buffer_byte_alignment); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | */ |
| 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 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 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; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 832 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 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."); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 837 | return false; |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 838 | } |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 839 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 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; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 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 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 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) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 885 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 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; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 892 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 893 | for (i = 0; i < numDescriptors; i++) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 894 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 895 | buffer_descriptor_t *bufDescriptor = (buffer_descriptor_t *)(descriptors[i]); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 896 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 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; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 903 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 904 | bufDescriptor->internal_format = mali_gralloc_select_format( |
| 905 | bufDescriptor->hal_format, bufDescriptor->format_type, usage, bufDescriptor->width * bufDescriptor->height); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 906 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 907 | if (bufDescriptor->internal_format == 0) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 908 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 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) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 918 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 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 | } |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 932 | } |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 933 | else if (usage & MALI_GRALLOC_USAGE_AFBC_PADDING) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 934 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 935 | alloc_type = AFBC_PADDED; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 936 | } |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 937 | else if (bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_AFBC_WIDEBLK) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 938 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 939 | alloc_type = AFBC_WIDEBLK; |
| 940 | } |
| 941 | else |
| 942 | { |
| 943 | alloc_type = AFBC; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 944 | } |
| 945 | } |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 946 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 947 | uint64_t base_format = bufDescriptor->internal_format & MALI_GRALLOC_INTFMT_FMT_MASK; |
| 948 | |
| 949 | switch (base_format) |
| 950 | { |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 951 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 952 | case HAL_PIXEL_FORMAT_RGBX_8888: |
| 953 | case HAL_PIXEL_FORMAT_BGRA_8888: |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 954 | get_rgb_stride_and_size(bufDescriptor->width, bufDescriptor->height, 4, &bufDescriptor->pixel_stride, |
| 955 | &bufDescriptor->byte_stride, &bufDescriptor->size, alloc_type); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 956 | break; |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 957 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 958 | case HAL_PIXEL_FORMAT_RGB_888: |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 959 | get_rgb_stride_and_size(bufDescriptor->width, bufDescriptor->height, 3, &bufDescriptor->pixel_stride, |
| 960 | &bufDescriptor->byte_stride, &bufDescriptor->size, alloc_type); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 961 | break; |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 962 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 963 | case HAL_PIXEL_FORMAT_RGB_565: |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 964 | get_rgb_stride_and_size(bufDescriptor->width, bufDescriptor->height, 2, &bufDescriptor->pixel_stride, |
| 965 | &bufDescriptor->byte_stride, &bufDescriptor->size, alloc_type); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 966 | 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 Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 977 | |
| 978 | if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 979 | { |
| 980 | yv12_align = YUV_ANDROID_PLANE_ALIGN; |
| 981 | } |
| 982 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 983 | 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 Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 986 | { |
| 987 | return -EINVAL; |
| 988 | } |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 989 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 990 | break; |
| 991 | } |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 992 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 993 | case HAL_PIXEL_FORMAT_YCbCr_422_I: |
| 994 | { |
| 995 | /* YUYV 4:2:2 */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 996 | 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 Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1000 | { |
| 1001 | return -EINVAL; |
| 1002 | } |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1003 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1004 | break; |
| 1005 | } |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1006 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1007 | case HAL_PIXEL_FORMAT_RAW16: |
| 1008 | case HAL_PIXEL_FORMAT_RAW12: |
| 1009 | case HAL_PIXEL_FORMAT_RAW10: |
| 1010 | case HAL_PIXEL_FORMAT_BLOB: |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1011 | if (alloc_type != UNCOMPRESSED) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1012 | { |
| 1013 | return -EINVAL; |
| 1014 | } |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1015 | |
| 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 Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1020 | break; |
| 1021 | |
| 1022 | case MALI_GRALLOC_FORMAT_INTERNAL_Y0L2: |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1023 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1024 | /* YUYAAYUVAA 4:2:0 with and without AFBC */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1025 | if (alloc_type != UNCOMPRESSED) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1026 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1027 | 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 Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1030 | { |
| 1031 | return -EINVAL; |
| 1032 | } |
| 1033 | } |
| 1034 | else |
| 1035 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1036 | if (!get_yuv_y0l2_stride_and_size(bufDescriptor->width, bufDescriptor->height, |
| 1037 | &bufDescriptor->pixel_stride, &bufDescriptor->byte_stride, |
| 1038 | &bufDescriptor->size)) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1039 | { |
| 1040 | return -EINVAL; |
| 1041 | } |
| 1042 | } |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1043 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1044 | break; |
| 1045 | |
| 1046 | case MALI_GRALLOC_FORMAT_INTERNAL_P010: |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1047 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1048 | /* Y-UV 4:2:0 */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1049 | 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 Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1053 | { |
| 1054 | return -EINVAL; |
| 1055 | } |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1056 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1057 | break; |
| 1058 | |
| 1059 | case MALI_GRALLOC_FORMAT_INTERNAL_P210: |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1060 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1061 | /* Y-UV 4:2:2 */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1062 | 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 Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1066 | { |
| 1067 | return -EINVAL; |
| 1068 | } |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1069 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1070 | break; |
| 1071 | |
| 1072 | case MALI_GRALLOC_FORMAT_INTERNAL_Y210: |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1073 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1074 | /* YUYV 4:2:2 with and without AFBC */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1075 | if (alloc_type != UNCOMPRESSED) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1076 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1077 | 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 Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1080 | { |
| 1081 | return -EINVAL; |
| 1082 | } |
| 1083 | } |
| 1084 | else |
| 1085 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1086 | if (!get_yuv_y210_stride_and_size(bufDescriptor->width, bufDescriptor->height, |
| 1087 | &bufDescriptor->pixel_stride, &bufDescriptor->byte_stride, |
| 1088 | &bufDescriptor->size)) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1089 | { |
| 1090 | return -EINVAL; |
| 1091 | } |
| 1092 | } |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1093 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1094 | break; |
| 1095 | |
| 1096 | case MALI_GRALLOC_FORMAT_INTERNAL_Y410: |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1097 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1098 | /* AVYU 2-10-10-10 */ |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1099 | 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 Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1102 | { |
| 1103 | return -EINVAL; |
| 1104 | } |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1105 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1106 | break; |
| 1107 | |
| 1108 | case MALI_GRALLOC_FORMAT_INTERNAL_YUV422_8BIT: |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1109 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1110 | /* 8BIT AFBC YUV4:2:2 testing usage */ |
| 1111 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1112 | /* 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 Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1118 | { |
| 1119 | return -EINVAL; |
| 1120 | } |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1121 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1122 | break; |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1123 | |
| 1124 | /* |
| 1125 | * Additional custom formats can be added here |
| 1126 | * and must fill the variables pixel_stride, byte_stride and size. |
| 1127 | */ |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1128 | default: |
| 1129 | return -EINVAL; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1130 | } |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1131 | } |
| 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 Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1138 | { |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1139 | return err; |
| 1140 | } |
| 1141 | } |
| 1142 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1143 | if (shared) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1144 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1145 | 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 Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1168 | case MALI_GRALLOC_USAGE_YUV_CONF_0: |
| 1169 | hnd->yuv_info = MALI_YUV_BT601_NARROW; |
| 1170 | break; |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1171 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1172 | case MALI_GRALLOC_USAGE_YUV_CONF_1: |
| 1173 | hnd->yuv_info = MALI_YUV_BT601_WIDE; |
| 1174 | break; |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1175 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1176 | case MALI_GRALLOC_USAGE_YUV_CONF_2: |
| 1177 | hnd->yuv_info = MALI_YUV_BT709_NARROW; |
| 1178 | break; |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1179 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1180 | case MALI_GRALLOC_USAGE_YUV_CONF_3: |
| 1181 | hnd->yuv_info = MALI_YUV_BT709_WIDE; |
| 1182 | break; |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1183 | } |
| 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 Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1203 | if (NULL != shared_backend) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1204 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1205 | *shared_backend = shared; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1206 | } |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1207 | |
| 1208 | return 0; |
| 1209 | } |
| 1210 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1211 | int mali_gralloc_buffer_free(buffer_handle_t pHandle) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1212 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1213 | int rval = -1; |
| 1214 | private_handle_t *hnd = (private_handle_t *)(pHandle); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1215 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1216 | if (hnd != NULL) |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1217 | { |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1218 | rval = gralloc_buffer_attr_free(hnd); |
| 1219 | mali_gralloc_ion_free(hnd); |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1222 | return rval; |
| 1223 | } |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1224 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1225 | static 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 Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1229 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1230 | 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 Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
John Stultz | 18814f6 | 2018-02-22 16:02:49 -0800 | [diff] [blame] | 1238 | return err; |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 1239 | } |