blob: 68bdb4cbeab156c9f243fd7096d3d04ab9847a1d [file] [log] [blame]
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05301/*
Naseer Ahmeda422f352017-12-01 15:33:56 -05002 * Copyright (c) 2011-2018, The Linux Foundation. All rights reserved.
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05303
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070030#include <media/msm_media_info.h>
31#include <algorithm>
32
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070033#include "gr_adreno_info.h"
Naseer Ahmeda422f352017-12-01 15:33:56 -050034#include "gr_utils.h"
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070035#include "qdMetaData.h"
36
37#define ASTC_BLOCK_SIZE 16
38
39#ifndef COLOR_FMT_P010_UBWC
40#define COLOR_FMT_P010_UBWC 9
41#endif
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053042
Naseer Ahmeda422f352017-12-01 15:33:56 -050043namespace gralloc {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053044
Saurabh Shahdbb572e2018-01-04 15:38:01 -080045bool IsYuvFormat(const private_handle_t *hnd) {
46 switch (hnd->format) {
47 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
48 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
49 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmeda422f352017-12-01 15:33:56 -050050 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: // Same as YCbCr_420_SP_VENUS
Saurabh Shahdbb572e2018-01-04 15:38:01 -080051 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
52 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
53 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
54 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
55 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
56 case HAL_PIXEL_FORMAT_NV21_ZSL:
57 case HAL_PIXEL_FORMAT_RAW16:
58 case HAL_PIXEL_FORMAT_Y16:
59 case HAL_PIXEL_FORMAT_RAW12:
60 case HAL_PIXEL_FORMAT_RAW10:
61 case HAL_PIXEL_FORMAT_YV12:
62 case HAL_PIXEL_FORMAT_Y8:
63 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
64 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
65 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
66 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
67 return true;
68 default:
69 return false;
70 }
71}
72
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053073bool IsUncompressedRGBFormat(int format) {
74 switch (format) {
75 case HAL_PIXEL_FORMAT_RGBA_8888:
76 case HAL_PIXEL_FORMAT_RGBX_8888:
77 case HAL_PIXEL_FORMAT_RGB_888:
78 case HAL_PIXEL_FORMAT_RGB_565:
79 case HAL_PIXEL_FORMAT_BGR_565:
80 case HAL_PIXEL_FORMAT_BGRA_8888:
81 case HAL_PIXEL_FORMAT_RGBA_5551:
82 case HAL_PIXEL_FORMAT_RGBA_4444:
83 case HAL_PIXEL_FORMAT_R_8:
84 case HAL_PIXEL_FORMAT_RG_88:
85 case HAL_PIXEL_FORMAT_BGRX_8888:
86 case HAL_PIXEL_FORMAT_RGBA_1010102:
87 case HAL_PIXEL_FORMAT_ARGB_2101010:
88 case HAL_PIXEL_FORMAT_RGBX_1010102:
89 case HAL_PIXEL_FORMAT_XRGB_2101010:
90 case HAL_PIXEL_FORMAT_BGRA_1010102:
91 case HAL_PIXEL_FORMAT_ABGR_2101010:
92 case HAL_PIXEL_FORMAT_BGRX_1010102:
93 case HAL_PIXEL_FORMAT_XBGR_2101010:
Naseer Ahmed1c473d82017-02-27 13:39:37 -050094 case HAL_PIXEL_FORMAT_RGBA_FP16:
Camus Wong5c5e6fb2017-03-21 17:02:48 -040095 case HAL_PIXEL_FORMAT_BGR_888:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053096 return true;
97 default:
98 break;
99 }
100
101 return false;
102}
103
104bool IsCompressedRGBFormat(int format) {
105 switch (format) {
106 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
107 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
108 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
109 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
110 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
111 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
112 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
113 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
114 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
115 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
116 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
117 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
118 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
119 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
120 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
121 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
122 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
123 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
124 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
125 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
126 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
127 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
128 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
129 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
130 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
131 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
132 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
133 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
134 return true;
135 default:
136 break;
137 }
138
139 return false;
140}
141
142uint32_t GetBppForUncompressedRGB(int format) {
143 uint32_t bpp = 0;
144 switch (format) {
Naseer Ahmed1c473d82017-02-27 13:39:37 -0500145 case HAL_PIXEL_FORMAT_RGBA_FP16:
146 bpp = 8;
147 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530148 case HAL_PIXEL_FORMAT_RGBA_8888:
149 case HAL_PIXEL_FORMAT_RGBX_8888:
150 case HAL_PIXEL_FORMAT_BGRA_8888:
151 case HAL_PIXEL_FORMAT_BGRX_8888:
Arun Kumar K.Rb97ca302017-04-06 15:59:33 -0700152 case HAL_PIXEL_FORMAT_RGBA_1010102:
153 case HAL_PIXEL_FORMAT_ARGB_2101010:
154 case HAL_PIXEL_FORMAT_RGBX_1010102:
155 case HAL_PIXEL_FORMAT_XRGB_2101010:
156 case HAL_PIXEL_FORMAT_BGRA_1010102:
157 case HAL_PIXEL_FORMAT_ABGR_2101010:
158 case HAL_PIXEL_FORMAT_BGRX_1010102:
159 case HAL_PIXEL_FORMAT_XBGR_2101010:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530160 bpp = 4;
161 break;
162 case HAL_PIXEL_FORMAT_RGB_888:
Camus Wong5c5e6fb2017-03-21 17:02:48 -0400163 case HAL_PIXEL_FORMAT_BGR_888:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530164 bpp = 3;
165 break;
166 case HAL_PIXEL_FORMAT_RGB_565:
167 case HAL_PIXEL_FORMAT_BGR_565:
168 case HAL_PIXEL_FORMAT_RGBA_5551:
169 case HAL_PIXEL_FORMAT_RGBA_4444:
170 bpp = 2;
171 break;
172 default:
Arun Kumar K.Rb97ca302017-04-06 15:59:33 -0700173 ALOGE("Error : %s New format request = 0x%x", __FUNCTION__, format);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530174 break;
175 }
176
177 return bpp;
178}
179
Naseer Ahmeda422f352017-12-01 15:33:56 -0500180bool CpuCanAccess(uint64_t usage) {
181 return CpuCanRead(usage) || CpuCanWrite(usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530182}
183
Naseer Ahmeda422f352017-12-01 15:33:56 -0500184bool CpuCanRead(uint64_t usage) {
185 if (usage & BufferUsage::CPU_READ_MASK) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530186 return true;
187 }
188
189 return false;
190}
191
Naseer Ahmeda422f352017-12-01 15:33:56 -0500192bool CpuCanWrite(uint64_t usage) {
193 if (usage & BufferUsage::CPU_WRITE_MASK) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530194 // Application intends to use CPU for rendering
195 return true;
196 }
197
198 return false;
199}
200
Naseer Ahmeda422f352017-12-01 15:33:56 -0500201unsigned int GetSize(const BufferInfo &info, unsigned int alignedw, unsigned int alignedh) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700202 unsigned int size = 0;
203 int format = info.format;
204 int width = info.width;
205 int height = info.height;
Naseer Ahmeda422f352017-12-01 15:33:56 -0500206 uint64_t usage = info.usage;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700207
Naseer Ahmeda422f352017-12-01 15:33:56 -0500208 if (IsUBwcEnabled(format, usage)) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700209 return GetUBwcSize(width, height, format, alignedw, alignedh);
210 }
211
212 if (IsUncompressedRGBFormat(format)) {
213 uint32_t bpp = GetBppForUncompressedRGB(format);
214 size = alignedw * alignedh * bpp;
215 return size;
216 }
217
218 if (IsCompressedRGBFormat(format)) {
219 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
220 return size;
221 }
222
223 // Below switch should be for only YUV/custom formats
224 switch (format) {
225 case HAL_PIXEL_FORMAT_RAW16:
Anjaneya Prasad Musunurib1654142017-04-18 10:32:42 +0530226 case HAL_PIXEL_FORMAT_Y16:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700227 size = alignedw * alignedh * 2;
228 break;
229 case HAL_PIXEL_FORMAT_RAW10:
230 case HAL_PIXEL_FORMAT_RAW12:
231 size = ALIGN(alignedw * alignedh, SIZE_4K);
232 break;
233 case HAL_PIXEL_FORMAT_RAW8:
Anjaneya Prasad Musunurib1654142017-04-18 10:32:42 +0530234 case HAL_PIXEL_FORMAT_Y8:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700235 size = alignedw * alignedh * 1;
236 break;
237
238 // adreno formats
239 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
240 size = ALIGN(alignedw * alignedh, SIZE_4K);
241 size += (unsigned int)ALIGN(2 * ALIGN(width / 2, 32) * ALIGN(height / 2, 32), SIZE_4K);
242 break;
243 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
244 // The chroma plane is subsampled,
245 // but the pitch in bytes is unchanged
246 // The GPU needs 4K alignment, but the video decoder needs 8K
247 size = ALIGN(alignedw * alignedh, SIZE_8K);
248 size += ALIGN(alignedw * (unsigned int)ALIGN(height / 2, 32), SIZE_8K);
249 break;
250 case HAL_PIXEL_FORMAT_YV12:
251 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width & 1) || (height & 1))) {
252 ALOGE("w or h is odd for the YV12 format");
253 return 0;
254 }
255 size = alignedw * alignedh + (ALIGN(alignedw / 2, 16) * (alignedh / 2)) * 2;
256 size = ALIGN(size, (unsigned int)SIZE_4K);
257 break;
258 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
259 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
260 size = ALIGN((alignedw * alignedh) + (alignedw * alignedh) / 2 + 1, SIZE_4K);
261 break;
262 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
263 size = ALIGN((alignedw * alignedh * 2) + (alignedw * alignedh) + 1, SIZE_4K);
264 break;
Mathew Joseph Karimpanala73082e2017-10-16 18:01:21 +0530265 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
266 size = VENUS_BUFFER_SIZE(COLOR_FMT_P010, width, height);
267 break;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700268 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
269 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
270 case HAL_PIXEL_FORMAT_YCbCr_422_I:
271 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Camus Wong5c5e6fb2017-03-21 17:02:48 -0400272 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700273 if (width & 1) {
274 ALOGE("width is odd for the YUV422_SP format");
275 return 0;
276 }
277 size = ALIGN(alignedw * alignedh * 2, SIZE_4K);
278 break;
279 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
280 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
281 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
282 break;
283 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
284 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
285 break;
286 case HAL_PIXEL_FORMAT_BLOB:
287 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
288 if (height != 1) {
289 ALOGE("%s: Buffers with HAL_PIXEL_FORMAT_BLOB must have height 1 ", __FUNCTION__);
290 return 0;
291 }
292 size = (unsigned int)width;
293 break;
294 case HAL_PIXEL_FORMAT_NV21_ZSL:
295 size = ALIGN((alignedw * alignedh) + (alignedw * alignedh) / 2, SIZE_4K);
296 break;
297 default:
298 ALOGE("%s: Unrecognized pixel format: 0x%x", __FUNCTION__, format);
299 return 0;
300 }
301
302 return size;
303}
304
Naseer Ahmeda422f352017-12-01 15:33:56 -0500305void GetBufferSizeAndDimensions(const BufferInfo &info, unsigned int *size, unsigned int *alignedw,
306 unsigned int *alignedh) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700307 GetAlignedWidthAndHeight(info, alignedw, alignedh);
308 *size = GetSize(info, *alignedw, *alignedh);
309}
310
Naseer Ahmeda422f352017-12-01 15:33:56 -0500311void GetYuvUbwcSPPlaneInfo(uint64_t base, uint32_t width, uint32_t height, int color_format,
312 struct android_ycbcr *ycbcr) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700313 // UBWC buffer has these 4 planes in the following sequence:
314 // Y_Meta_Plane, Y_Plane, UV_Meta_Plane, UV_Plane
315 unsigned int y_meta_stride, y_meta_height, y_meta_size;
316 unsigned int y_stride, y_height, y_size;
317 unsigned int c_meta_stride, c_meta_height, c_meta_size;
318 unsigned int alignment = 4096;
319
320 y_meta_stride = VENUS_Y_META_STRIDE(color_format, INT(width));
321 y_meta_height = VENUS_Y_META_SCANLINES(color_format, INT(height));
322 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
323
324 y_stride = VENUS_Y_STRIDE(color_format, INT(width));
325 y_height = VENUS_Y_SCANLINES(color_format, INT(height));
326 y_size = ALIGN((y_stride * y_height), alignment);
327
328 c_meta_stride = VENUS_UV_META_STRIDE(color_format, INT(width));
329 c_meta_height = VENUS_UV_META_SCANLINES(color_format, INT(height));
330 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
331
332 ycbcr->y = reinterpret_cast<void *>(base + y_meta_size);
333 ycbcr->cb = reinterpret_cast<void *>(base + y_meta_size + y_size + c_meta_size);
334 ycbcr->cr = reinterpret_cast<void *>(base + y_meta_size + y_size + c_meta_size + 1);
335 ycbcr->ystride = y_stride;
336 ycbcr->cstride = VENUS_UV_STRIDE(color_format, INT(width));
337}
338
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700339void GetYuvUbwcInterlacedSPPlaneInfo(uint64_t base, uint32_t width, uint32_t height,
Naseer Ahmed7aff2612017-10-05 20:39:05 -0400340 int color_format, struct android_ycbcr ycbcr[2]) {
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700341 unsigned int uv_stride, uv_height, uv_size;
342 unsigned int alignment = 4096;
343 uint64_t field_base;
344
345 // UBWC interlaced has top-bottom field layout with each field as
346 // 4-plane NV12_UBWC with width = image_width & height = image_height / 2.
347 // Client passed ycbcr argument is ptr to struct android_ycbcr[2].
348 // Plane info to be filled for each field separately.
349 height = (height + 1) >> 1;
350 uv_stride = VENUS_UV_STRIDE(color_format, INT(width));
351 uv_height = VENUS_UV_SCANLINES(color_format, INT(height));
352 uv_size = ALIGN((uv_stride * uv_height), alignment);
353
354 field_base = base;
355 GetYuvUbwcSPPlaneInfo(field_base, width, height, COLOR_FMT_NV12_UBWC, &ycbcr[0]);
356
Naseer Ahmed7aff2612017-10-05 20:39:05 -0400357 memset(ycbcr[1].reserved, 0, sizeof(ycbcr[1].reserved));
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700358 field_base = reinterpret_cast<uint64_t>(ycbcr[0].cb) + uv_size;
359 GetYuvUbwcSPPlaneInfo(field_base, width, height, COLOR_FMT_NV12_UBWC, &ycbcr[1]);
360}
361
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700362void GetYuvSPPlaneInfo(uint64_t base, uint32_t width, uint32_t height, uint32_t bpp,
363 struct android_ycbcr *ycbcr) {
364 unsigned int ystride, cstride;
365
366 ystride = cstride = UINT(width) * bpp;
367 ycbcr->y = reinterpret_cast<void *>(base);
368 ycbcr->cb = reinterpret_cast<void *>(base + ystride * UINT(height));
369 ycbcr->cr = reinterpret_cast<void *>(base + ystride * UINT(height) + 1);
370 ycbcr->ystride = ystride;
371 ycbcr->cstride = cstride;
372 ycbcr->chroma_step = 2 * bpp;
373}
374
Naseer Ahmed7aff2612017-10-05 20:39:05 -0400375int GetYUVPlaneInfo(const private_handle_t *hnd, struct android_ycbcr ycbcr[2]) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700376 int err = 0;
377 uint32_t width = UINT(hnd->width);
378 uint32_t height = UINT(hnd->height);
379 int format = hnd->format;
Naseer Ahmeda422f352017-12-01 15:33:56 -0500380 uint64_t usage = hnd->usage;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700381 unsigned int ystride, cstride;
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700382 bool interlaced = false;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700383
384 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700385
386 // Check if UBWC buffer has been rendered in linear format.
Ramakant Singhc7d07792017-07-26 15:36:33 +0530387 int linear_format = 0;
Naseer Ahmeda422f352017-12-01 15:33:56 -0500388 if (getMetaData(const_cast<private_handle_t *>(hnd), GET_LINEAR_FORMAT, &linear_format) == 0) {
389 format = INT(linear_format);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700390 }
391
392 // Check metadata if the geometry has been updated.
Ramakant Singhc7d07792017-07-26 15:36:33 +0530393 BufferDim_t buffer_dim;
Naseer Ahmeda422f352017-12-01 15:33:56 -0500394 if (getMetaData(const_cast<private_handle_t *>(hnd), GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
395 BufferInfo info(buffer_dim.sliceWidth, buffer_dim.sliceHeight, format, usage);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700396 GetAlignedWidthAndHeight(info, &width, &height);
397 }
398
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700399 // Check metadata for interlaced content.
Ramakant Singhc7d07792017-07-26 15:36:33 +0530400 int interlace_flag = 0;
Naseer Ahmeda422f352017-12-01 15:33:56 -0500401 if (getMetaData(const_cast<private_handle_t *>(hnd), GET_PP_PARAM_INTERLACED, &interlace_flag) ==
402 0) {
Ramakant Singhc7d07792017-07-26 15:36:33 +0530403 interlaced = interlace_flag;
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700404 }
405
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700406 // Get the chroma offsets from the handle width/height. We take advantage
407 // of the fact the width _is_ the stride
408 switch (format) {
409 // Semiplanar
410 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
411 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
412 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
413 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
414 // Same as YCbCr_420_SP_VENUS
415 GetYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
416 break;
417
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700418 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700419 if (!interlaced) {
420 GetYuvUbwcSPPlaneInfo(hnd->base, width, height, COLOR_FMT_NV12_UBWC, ycbcr);
421 } else {
422 GetYuvUbwcInterlacedSPPlaneInfo(hnd->base, width, height, COLOR_FMT_NV12_UBWC, ycbcr);
423 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700424 ycbcr->chroma_step = 2;
425 break;
426
Mathew Joseph Karimpanala73082e2017-10-16 18:01:21 +0530427 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
428 GetYuvSPPlaneInfo(hnd->base, width, height, 2, ycbcr);
429 break;
430
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700431 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
432 GetYuvUbwcSPPlaneInfo(hnd->base, width, height, COLOR_FMT_NV12_BPP10_UBWC, ycbcr);
433 ycbcr->chroma_step = 3;
434 break;
435
436 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
437 GetYuvUbwcSPPlaneInfo(hnd->base, width, height, COLOR_FMT_P010_UBWC, ycbcr);
438 ycbcr->chroma_step = 4;
439 break;
440
Mathew Joseph Karimpanala73082e2017-10-16 18:01:21 +0530441 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
442 ystride = VENUS_Y_STRIDE(COLOR_FMT_P010, width);
443 cstride = VENUS_UV_STRIDE(COLOR_FMT_P010, width);
444 ycbcr->y = reinterpret_cast<void *>(hnd->base);
Naseer Ahmeda422f352017-12-01 15:33:56 -0500445 ycbcr->cb =
446 reinterpret_cast<void *>(hnd->base + ystride * VENUS_Y_SCANLINES(COLOR_FMT_P010, height));
Mathew Joseph Karimpanala73082e2017-10-16 18:01:21 +0530447 ycbcr->cr = reinterpret_cast<void *>(hnd->base +
448 ystride * VENUS_Y_SCANLINES(COLOR_FMT_P010, height) + 1);
449 ycbcr->ystride = ystride;
450 ycbcr->cstride = cstride;
451 ycbcr->chroma_step = 4;
452 break;
453
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700454 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
455 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
456 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
457 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
458 case HAL_PIXEL_FORMAT_NV21_ZSL:
459 case HAL_PIXEL_FORMAT_RAW16:
Anjaneya Prasad Musunurib1654142017-04-18 10:32:42 +0530460 case HAL_PIXEL_FORMAT_Y16:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700461 case HAL_PIXEL_FORMAT_RAW10:
462 case HAL_PIXEL_FORMAT_RAW8:
Anjaneya Prasad Musunurib1654142017-04-18 10:32:42 +0530463 case HAL_PIXEL_FORMAT_Y8:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700464 GetYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
465 std::swap(ycbcr->cb, ycbcr->cr);
466 break;
467
468 // Planar
469 case HAL_PIXEL_FORMAT_YV12:
470 ystride = width;
471 cstride = ALIGN(width / 2, 16);
472 ycbcr->y = reinterpret_cast<void *>(hnd->base);
473 ycbcr->cr = reinterpret_cast<void *>(hnd->base + ystride * height);
474 ycbcr->cb = reinterpret_cast<void *>(hnd->base + ystride * height + cstride * height / 2);
475 ycbcr->ystride = ystride;
476 ycbcr->cstride = cstride;
477 ycbcr->chroma_step = 1;
478 break;
Camus Wong5c5e6fb2017-03-21 17:02:48 -0400479 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
480 ystride = width * 2;
481 cstride = 0;
Naseer Ahmeda422f352017-12-01 15:33:56 -0500482 ycbcr->y = reinterpret_cast<void *>(hnd->base);
Camus Wong5c5e6fb2017-03-21 17:02:48 -0400483 ycbcr->cr = NULL;
484 ycbcr->cb = NULL;
485 ycbcr->ystride = ystride;
486 ycbcr->cstride = 0;
487 ycbcr->chroma_step = 0;
488 break;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700489 // Unsupported formats
490 case HAL_PIXEL_FORMAT_YCbCr_422_I:
491 case HAL_PIXEL_FORMAT_YCrCb_422_I:
492 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
493 default:
494 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, format);
495 err = -EINVAL;
496 }
497
498 return err;
499}
500
501// Explicitly defined UBWC formats
502bool IsUBwcFormat(int format) {
503 switch (format) {
504 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
505 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
506 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
507 return true;
508 default:
509 return false;
510 }
511}
512
513bool IsUBwcSupported(int format) {
514 // Existing HAL formats with UBWC support
515 switch (format) {
516 case HAL_PIXEL_FORMAT_BGR_565:
517 case HAL_PIXEL_FORMAT_RGBA_8888:
518 case HAL_PIXEL_FORMAT_RGBX_8888:
519 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
520 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
521 case HAL_PIXEL_FORMAT_RGBA_1010102:
522 case HAL_PIXEL_FORMAT_RGBX_1010102:
523 return true;
524 default:
525 break;
526 }
527
528 return false;
529}
530
Naseer Ahmeda422f352017-12-01 15:33:56 -0500531bool IsUBwcEnabled(int format, uint64_t usage) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700532 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
533 if (IsUBwcFormat(format)) {
534 return true;
535 }
536
537 // Allow UBWC, if an OpenGL client sets UBWC usage flag and GPU plus MDP
538 // support the format. OR if a non-OpenGL client like Rotator, sets UBWC
539 // usage flag and MDP supports the format.
Naseer Ahmeda422f352017-12-01 15:33:56 -0500540 if ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) && IsUBwcSupported(format)) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700541 bool enable = true;
542 // Query GPU for UBWC only if buffer is intended to be used by GPU.
Naseer Ahmeda422f352017-12-01 15:33:56 -0500543 if ((usage & BufferUsage::GPU_TEXTURE) || (usage & BufferUsage::GPU_RENDER_TARGET)) {
Pramodh Kumar Mukunda927729d2017-06-15 16:18:51 +0530544 if (AdrenoMemInfo::GetInstance()) {
545 enable = AdrenoMemInfo::GetInstance()->IsUBWCSupportedByGPU(format);
546 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700547 }
548
549 // Allow UBWC, only if CPU usage flags are not set
Naseer Ahmeda422f352017-12-01 15:33:56 -0500550 if (enable && !(CpuCanAccess(usage))) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700551 return true;
552 }
553 }
554
555 return false;
556}
557
558void GetYuvUBwcWidthAndHeight(int width, int height, int format, unsigned int *aligned_w,
559 unsigned int *aligned_h) {
560 switch (format) {
561 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
562 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
563 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
564 *aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
565 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
566 break;
567 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
568 // The macro returns the stride which is 4/3 times the width, hence * 3/4
569 *aligned_w = (VENUS_Y_STRIDE(COLOR_FMT_NV12_BPP10_UBWC, width) * 3) / 4;
570 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_BPP10_UBWC, height);
571 break;
572 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
573 // The macro returns the stride which is 2 times the width, hence / 2
574 *aligned_w = (VENUS_Y_STRIDE(COLOR_FMT_P010_UBWC, width) / 2);
575 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_P010_UBWC, height);
576 break;
577 default:
578 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
579 *aligned_w = 0;
580 *aligned_h = 0;
581 break;
582 }
583}
584
585void GetRgbUBwcBlockSize(uint32_t bpp, int *block_width, int *block_height) {
586 *block_width = 0;
587 *block_height = 0;
588
589 switch (bpp) {
590 case 2:
591 case 4:
592 *block_width = 16;
593 *block_height = 4;
594 break;
595 case 8:
596 *block_width = 8;
597 *block_height = 4;
598 break;
599 case 16:
600 *block_width = 4;
601 *block_height = 4;
602 break;
603 default:
604 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
605 break;
606 }
607}
608
609unsigned int GetRgbUBwcMetaBufferSize(int width, int height, uint32_t bpp) {
610 unsigned int size = 0;
611 int meta_width, meta_height;
612 int block_width, block_height;
613
614 GetRgbUBwcBlockSize(bpp, &block_width, &block_height);
615 if (!block_width || !block_height) {
616 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
617 return size;
618 }
619
620 // Align meta buffer height to 16 blocks
621 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
622
623 // Align meta buffer width to 64 blocks
624 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
625
626 // Align meta buffer size to 4K
627 size = (unsigned int)ALIGN((meta_width * meta_height), 4096);
628
629 return size;
630}
631
632unsigned int GetUBwcSize(int width, int height, int format, unsigned int alignedw,
633 unsigned int alignedh) {
634 unsigned int size = 0;
635 uint32_t bpp = 0;
636 switch (format) {
637 case HAL_PIXEL_FORMAT_BGR_565:
638 case HAL_PIXEL_FORMAT_RGBA_8888:
639 case HAL_PIXEL_FORMAT_RGBX_8888:
640 case HAL_PIXEL_FORMAT_RGBA_1010102:
641 case HAL_PIXEL_FORMAT_RGBX_1010102:
642 bpp = GetBppForUncompressedRGB(format);
643 size = alignedw * alignedh * bpp;
644 size += GetRgbUBwcMetaBufferSize(width, height, bpp);
645 break;
646 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
647 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
648 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
649 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
650 break;
651 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
652 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_BPP10_UBWC, width, height);
653 break;
654 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
655 size = VENUS_BUFFER_SIZE(COLOR_FMT_P010_UBWC, width, height);
656 break;
657 default:
658 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
659 break;
660 }
661
662 return size;
663}
664
665int GetRgbDataAddress(private_handle_t *hnd, void **rgb_data) {
666 int err = 0;
667
668 // This api is for RGB* formats
Naseer Ahmeda422f352017-12-01 15:33:56 -0500669 if (!IsUncompressedRGBFormat(hnd->format)) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700670 return -EINVAL;
671 }
672
673 // linear buffer, nothing to do further
674 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
675 *rgb_data = reinterpret_cast<void *>(hnd->base);
676 return err;
677 }
678
679 unsigned int meta_size = 0;
680 uint32_t bpp = GetBppForUncompressedRGB(hnd->format);
681 switch (hnd->format) {
682 case HAL_PIXEL_FORMAT_BGR_565:
683 case HAL_PIXEL_FORMAT_RGBA_8888:
684 case HAL_PIXEL_FORMAT_RGBX_8888:
685 case HAL_PIXEL_FORMAT_RGBA_1010102:
686 case HAL_PIXEL_FORMAT_RGBX_1010102:
687 meta_size = GetRgbUBwcMetaBufferSize(hnd->width, hnd->height, bpp);
688 break;
689 default:
690 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
691 err = -EINVAL;
692 break;
693 }
694 *rgb_data = reinterpret_cast<void *>(hnd->base + meta_size);
695
696 return err;
697}
698
Naseer Ahmeda422f352017-12-01 15:33:56 -0500699void GetCustomDimensions(private_handle_t *hnd, int *stride, int *height) {
700 BufferDim_t buffer_dim;
701 int interlaced = 0;
702
703 *stride = hnd->width;
704 *height = hnd->height;
705 if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
706 *stride = buffer_dim.sliceWidth;
707 *height = buffer_dim.sliceHeight;
708 } else if (getMetaData(hnd, GET_PP_PARAM_INTERLACED, &interlaced) == 0) {
709 if (interlaced && IsUBwcFormat(hnd->format)) {
710 unsigned int alignedw = 0, alignedh = 0;
711 // Get re-aligned height for single ubwc interlaced field and
712 // multiply by 2 to get frame height.
713 BufferInfo info(hnd->width, ((hnd->height + 1) >> 1), hnd->format);
714 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
715 *stride = static_cast<int>(alignedw);
716 *height = static_cast<int>(alignedh * 2);
717 }
718 }
719}
720
721void GetColorSpaceFromMetadata(private_handle_t *hnd, int *color_space) {
722 ColorMetaData color_metadata;
723 if (getMetaData(hnd, GET_COLOR_METADATA, &color_metadata) == 0) {
724 switch (color_metadata.colorPrimaries) {
725 case ColorPrimaries_BT709_5:
726 *color_space = HAL_CSC_ITU_R_709;
727 break;
728 case ColorPrimaries_BT601_6_525:
729 case ColorPrimaries_BT601_6_625:
730 *color_space = ((color_metadata.range) ? HAL_CSC_ITU_R_601_FR : HAL_CSC_ITU_R_601);
731 break;
732 case ColorPrimaries_BT2020:
733 *color_space = (color_metadata.range) ? HAL_CSC_ITU_R_2020_FR : HAL_CSC_ITU_R_2020;
734 break;
735 default:
736 ALOGE("Unknown Color Space = %d", color_metadata.colorPrimaries);
737 break;
738 }
739 } else if (getMetaData(hnd, GET_COLOR_SPACE, color_space) != 0) {
740 *color_space = 0;
741 }
742}
743
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700744void GetAlignedWidthAndHeight(const BufferInfo &info, unsigned int *alignedw,
745 unsigned int *alignedh) {
746 int width = info.width;
747 int height = info.height;
748 int format = info.format;
Naseer Ahmeda422f352017-12-01 15:33:56 -0500749 uint64_t usage = info.usage;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700750
751 // Currently surface padding is only computed for RGB* surfaces.
Naseer Ahmeda422f352017-12-01 15:33:56 -0500752 bool ubwc_enabled = IsUBwcEnabled(format, usage);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700753 int tile = ubwc_enabled;
754
755 if (IsUncompressedRGBFormat(format)) {
Pramodh Kumar Mukunda927729d2017-06-15 16:18:51 +0530756 if (AdrenoMemInfo::GetInstance()) {
757 AdrenoMemInfo::GetInstance()->AlignUnCompressedRGB(width, height, format, tile, alignedw,
758 alignedh);
759 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700760 return;
761 }
762
763 if (ubwc_enabled) {
764 GetYuvUBwcWidthAndHeight(width, height, format, alignedw, alignedh);
765 return;
766 }
767
768 if (IsCompressedRGBFormat(format)) {
Pramodh Kumar Mukunda927729d2017-06-15 16:18:51 +0530769 if (AdrenoMemInfo::GetInstance()) {
770 AdrenoMemInfo::GetInstance()->AlignCompressedRGB(width, height, format, alignedw, alignedh);
771 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700772 return;
773 }
774
775 int aligned_w = width;
776 int aligned_h = height;
777 unsigned int alignment = 32;
778
779 // Below should be only YUV family
780 switch (format) {
781 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
782 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
Pramodh Kumar Mukunda927729d2017-06-15 16:18:51 +0530783 if (AdrenoMemInfo::GetInstance() == nullptr) {
784 return;
785 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700786 alignment = AdrenoMemInfo::GetInstance()->GetGpuPixelAlignment();
787 aligned_w = ALIGN(width, alignment);
788 break;
789 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
790 aligned_w = ALIGN(width, alignment);
791 break;
792 case HAL_PIXEL_FORMAT_RAW16:
Anjaneya Prasad Musunurib1654142017-04-18 10:32:42 +0530793 case HAL_PIXEL_FORMAT_Y16:
794 case HAL_PIXEL_FORMAT_Y8:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700795 aligned_w = ALIGN(width, 16);
796 break;
797 case HAL_PIXEL_FORMAT_RAW12:
Naseer Ahmed1187a572017-08-14 13:16:55 -0400798 aligned_w = ALIGN(width * 12 / 8, 16);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700799 break;
800 case HAL_PIXEL_FORMAT_RAW10:
Naseer Ahmed1187a572017-08-14 13:16:55 -0400801 aligned_w = ALIGN(width * 10 / 8, 16);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700802 break;
803 case HAL_PIXEL_FORMAT_RAW8:
Naseer Ahmed1187a572017-08-14 13:16:55 -0400804 aligned_w = ALIGN(width, 16);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700805 break;
806 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
807 aligned_w = ALIGN(width, 128);
808 break;
809 case HAL_PIXEL_FORMAT_YV12:
810 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
811 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
812 case HAL_PIXEL_FORMAT_YCbCr_422_I:
813 case HAL_PIXEL_FORMAT_YCrCb_422_I:
814 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
815 aligned_w = ALIGN(width, 16);
816 break;
Mathew Joseph Karimpanala73082e2017-10-16 18:01:21 +0530817 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
818 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_P010, width) / 2);
819 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_P010, height));
820 break;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700821 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
822 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
823 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV12, width));
824 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV12, height));
825 break;
826 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
827 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV21, width));
828 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV21, height));
829 break;
830 case HAL_PIXEL_FORMAT_BLOB:
831 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
832 break;
833 case HAL_PIXEL_FORMAT_NV21_ZSL:
834 aligned_w = ALIGN(width, 64);
835 aligned_h = ALIGN(height, 64);
836 break;
837 default:
838 break;
839 }
840
841 *alignedw = (unsigned int)aligned_w;
842 *alignedh = (unsigned int)aligned_h;
843}
844
Naseer Ahmeda422f352017-12-01 15:33:56 -0500845int GetBufferLayout(private_handle_t *hnd, uint32_t stride[4], uint32_t offset[4],
846 uint32_t *num_planes) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700847 if (!hnd || !stride || !offset || !num_planes) {
848 return -EINVAL;
849 }
850
Naseer Ahmed7aff2612017-10-05 20:39:05 -0400851 struct android_ycbcr yuvPlaneInfo[2] = {};
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700852 *num_planes = 1;
853 stride[0] = 0;
854
855 switch (hnd->format) {
856 case HAL_PIXEL_FORMAT_RGB_565:
857 case HAL_PIXEL_FORMAT_BGR_565:
858 case HAL_PIXEL_FORMAT_RGBA_5551:
859 case HAL_PIXEL_FORMAT_RGBA_4444:
860 stride[0] = static_cast<uint32_t>(hnd->width * 2);
861 break;
862 case HAL_PIXEL_FORMAT_RGB_888:
863 stride[0] = static_cast<uint32_t>(hnd->width * 3);
864 break;
865 case HAL_PIXEL_FORMAT_RGBA_8888:
866 case HAL_PIXEL_FORMAT_BGRA_8888:
867 case HAL_PIXEL_FORMAT_RGBX_8888:
868 case HAL_PIXEL_FORMAT_BGRX_8888:
869 case HAL_PIXEL_FORMAT_RGBA_1010102:
870 case HAL_PIXEL_FORMAT_ARGB_2101010:
871 case HAL_PIXEL_FORMAT_RGBX_1010102:
872 case HAL_PIXEL_FORMAT_XRGB_2101010:
873 case HAL_PIXEL_FORMAT_BGRA_1010102:
874 case HAL_PIXEL_FORMAT_ABGR_2101010:
875 case HAL_PIXEL_FORMAT_BGRX_1010102:
876 case HAL_PIXEL_FORMAT_XBGR_2101010:
877 stride[0] = static_cast<uint32_t>(hnd->width * 4);
878 break;
879 }
880
881 // Format is RGB
882 if (stride[0]) {
883 return 0;
884 }
885
886 (*num_planes)++;
Naseer Ahmed7aff2612017-10-05 20:39:05 -0400887 int ret = GetYUVPlaneInfo(hnd, yuvPlaneInfo);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700888 if (ret < 0) {
889 ALOGE("%s failed", __FUNCTION__);
890 return ret;
891 }
892
Naseer Ahmed7aff2612017-10-05 20:39:05 -0400893 // We are only returning buffer layout for progressive or single field formats.
894 struct android_ycbcr yuvInfo = yuvPlaneInfo[0];
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700895 stride[0] = static_cast<uint32_t>(yuvInfo.ystride);
896 offset[0] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.y) - hnd->base);
897 stride[1] = static_cast<uint32_t>(yuvInfo.cstride);
898 switch (hnd->format) {
899 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
900 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
901 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
902 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
903 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
904 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
905 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
906 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
Mathew Joseph Karimpanala73082e2017-10-16 18:01:21 +0530907 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700908 offset[1] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cb) - hnd->base);
909 break;
910 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
911 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
912 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
913 offset[1] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cr) - hnd->base);
914 break;
915 case HAL_PIXEL_FORMAT_YV12:
916 offset[1] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cr) - hnd->base);
917 stride[2] = static_cast<uint32_t>(yuvInfo.cstride);
918 offset[2] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cb) - hnd->base);
919 (*num_planes)++;
920 break;
921 default:
922 ALOGW("%s: Unsupported format", __FUNCTION__);
923 ret = -EINVAL;
924 }
925
926 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
927 std::fill(offset, offset + 4, 0);
928 }
929
930 return 0;
931}
932
Naseer Ahmeda422f352017-12-01 15:33:56 -0500933} // namespace gralloc