blob: f7ddac60f7988359873eaadddb9eb4e7f4d9907d [file] [log] [blame]
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05301/*
Yahui Wang55563ca2018-01-15 19:50:05 +08002 * 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 Ahmede36f2242017-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 Ahmede36f2242017-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 Ahmede36f2242017-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 Ahmede36f2242017-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 Ahmede36f2242017-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 Ahmede36f2242017-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 Ahmedda214ec2018-03-12 20:15:07 -0400201uint32_t GetDataAlignment(int format, uint64_t usage) {
202 uint32_t align = UINT(getpagesize());
203 if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) {
204 align = SIZE_8K;
205 }
206
207 if (usage & BufferUsage::PROTECTED) {
208 if ((usage & BufferUsage::CAMERA_OUTPUT) || (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY)) {
209 // The alignment here reflects qsee mmu V7L/V8L requirement
210 align = SZ_2M;
211 } else {
212 align = SECURE_ALIGN;
213 }
214 }
215
216 return align;
217}
218
219// Returns the final buffer size meant to be allocated with ion
Naseer Ahmede36f2242017-12-01 15:33:56 -0500220unsigned int GetSize(const BufferInfo &info, unsigned int alignedw, unsigned int alignedh) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700221 unsigned int size = 0;
222 int format = info.format;
223 int width = info.width;
224 int height = info.height;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500225 uint64_t usage = info.usage;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700226
Naseer Ahmede36f2242017-12-01 15:33:56 -0500227 if (IsUBwcEnabled(format, usage)) {
Naseer Ahmedda214ec2018-03-12 20:15:07 -0400228 size = GetUBwcSize(width, height, format, alignedw, alignedh);
229 } else if (IsUncompressedRGBFormat(format)) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700230 uint32_t bpp = GetBppForUncompressedRGB(format);
231 size = alignedw * alignedh * bpp;
Naseer Ahmedda214ec2018-03-12 20:15:07 -0400232 } else if (IsCompressedRGBFormat(format)) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700233 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
Naseer Ahmedda214ec2018-03-12 20:15:07 -0400234 } else {
235 // Below switch should be for only YUV/custom formats
236 switch (format) {
237 case HAL_PIXEL_FORMAT_RAW16:
238 case HAL_PIXEL_FORMAT_Y16:size = alignedw * alignedh * 2;
239 break;
240 case HAL_PIXEL_FORMAT_RAW10:
241 case HAL_PIXEL_FORMAT_RAW12:size = ALIGN(alignedw * alignedh, SIZE_4K);
242 break;
243 case HAL_PIXEL_FORMAT_RAW8:
244 case HAL_PIXEL_FORMAT_Y8:size = alignedw * alignedh * 1;
245 break;
246 // adreno formats
247 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
248 size = ALIGN(alignedw * alignedh, SIZE_4K);
249 size += (unsigned int) ALIGN(2 * ALIGN(width / 2, 32) * ALIGN(height / 2, 32), SIZE_4K);
250 break;
251 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
252 // The chroma plane is subsampled,
253 // but the pitch in bytes is unchanged
254 // The GPU needs 4K alignment, but the video decoder needs 8K
255 size = ALIGN(alignedw * alignedh, SIZE_8K);
256 size += ALIGN(alignedw * (unsigned int) ALIGN(height / 2, 32), SIZE_8K);
257 break;
258 case HAL_PIXEL_FORMAT_YV12:
259 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width & 1) || (height & 1))) {
260 ALOGE("w or h is odd for the YV12 format");
261 return 0;
262 }
263 size = alignedw * alignedh + (ALIGN(alignedw / 2, 16) * (alignedh / 2)) * 2;
264 size = ALIGN(size, (unsigned int) SIZE_4K);
265 break;
266 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
267 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
268 size = ALIGN((alignedw * alignedh) + (alignedw * alignedh) / 2 + 1, SIZE_4K);
269 break;
270 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
271 size = ALIGN((alignedw * alignedh * 2) + (alignedw * alignedh) + 1, SIZE_4K);
272 break;
273 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
274 size = VENUS_BUFFER_SIZE(COLOR_FMT_P010,
275 width,
276 height);
277 break;
278 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
279 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
280 case HAL_PIXEL_FORMAT_YCbCr_422_I:
281 case HAL_PIXEL_FORMAT_YCrCb_422_I:
282 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
283 if (width & 1) {
284 ALOGE("width is odd for the YUV422_SP format");
285 return 0;
286 }
287 size = ALIGN(alignedw * alignedh * 2, SIZE_4K);
288 break;
289 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
290 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
291 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
292 break;
293 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
294 case HAL_PIXEL_FORMAT_NV21_ENCODEABLE:
295 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
296 break;
297 case HAL_PIXEL_FORMAT_BLOB:
298 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
299 if (height != 1) {
300 ALOGE("%s: Buffers with HAL_PIXEL_FORMAT_BLOB must have height 1 ", __FUNCTION__);
301 return 0;
302 }
303 size = (unsigned int) width;
304 break;
305 case HAL_PIXEL_FORMAT_NV21_ZSL:
306 size = ALIGN((alignedw * alignedh) + (alignedw * alignedh) / 2,
307 SIZE_4K);
308 break;
309 default:ALOGE("%s: Unrecognized pixel format: 0x%x", __FUNCTION__, format);
310 return 0;
311 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700312 }
Naseer Ahmedda214ec2018-03-12 20:15:07 -0400313 auto align = GetDataAlignment(format, usage);
314 size = ALIGN(size, align) * info.layer_count;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700315 return size;
316}
317
Naseer Ahmede36f2242017-12-01 15:33:56 -0500318void GetBufferSizeAndDimensions(const BufferInfo &info, unsigned int *size, unsigned int *alignedw,
319 unsigned int *alignedh) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700320 GetAlignedWidthAndHeight(info, alignedw, alignedh);
321 *size = GetSize(info, *alignedw, *alignedh);
322}
323
Naseer Ahmede36f2242017-12-01 15:33:56 -0500324void GetYuvUbwcSPPlaneInfo(uint64_t base, uint32_t width, uint32_t height, int color_format,
325 struct android_ycbcr *ycbcr) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700326 // UBWC buffer has these 4 planes in the following sequence:
327 // Y_Meta_Plane, Y_Plane, UV_Meta_Plane, UV_Plane
328 unsigned int y_meta_stride, y_meta_height, y_meta_size;
329 unsigned int y_stride, y_height, y_size;
330 unsigned int c_meta_stride, c_meta_height, c_meta_size;
331 unsigned int alignment = 4096;
332
333 y_meta_stride = VENUS_Y_META_STRIDE(color_format, INT(width));
334 y_meta_height = VENUS_Y_META_SCANLINES(color_format, INT(height));
335 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
336
337 y_stride = VENUS_Y_STRIDE(color_format, INT(width));
338 y_height = VENUS_Y_SCANLINES(color_format, INT(height));
339 y_size = ALIGN((y_stride * y_height), alignment);
340
341 c_meta_stride = VENUS_UV_META_STRIDE(color_format, INT(width));
342 c_meta_height = VENUS_UV_META_SCANLINES(color_format, INT(height));
343 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
344
345 ycbcr->y = reinterpret_cast<void *>(base + y_meta_size);
346 ycbcr->cb = reinterpret_cast<void *>(base + y_meta_size + y_size + c_meta_size);
347 ycbcr->cr = reinterpret_cast<void *>(base + y_meta_size + y_size + c_meta_size + 1);
348 ycbcr->ystride = y_stride;
349 ycbcr->cstride = VENUS_UV_STRIDE(color_format, INT(width));
350}
351
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700352void GetYuvUbwcInterlacedSPPlaneInfo(uint64_t base, uint32_t width, uint32_t height,
Naseer Ahmed7aff2612017-10-05 20:39:05 -0400353 int color_format, struct android_ycbcr ycbcr[2]) {
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700354 unsigned int uv_stride, uv_height, uv_size;
355 unsigned int alignment = 4096;
356 uint64_t field_base;
357
358 // UBWC interlaced has top-bottom field layout with each field as
359 // 4-plane NV12_UBWC with width = image_width & height = image_height / 2.
360 // Client passed ycbcr argument is ptr to struct android_ycbcr[2].
361 // Plane info to be filled for each field separately.
362 height = (height + 1) >> 1;
363 uv_stride = VENUS_UV_STRIDE(color_format, INT(width));
364 uv_height = VENUS_UV_SCANLINES(color_format, INT(height));
365 uv_size = ALIGN((uv_stride * uv_height), alignment);
366
367 field_base = base;
368 GetYuvUbwcSPPlaneInfo(field_base, width, height, COLOR_FMT_NV12_UBWC, &ycbcr[0]);
369
Naseer Ahmed7aff2612017-10-05 20:39:05 -0400370 memset(ycbcr[1].reserved, 0, sizeof(ycbcr[1].reserved));
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700371 field_base = reinterpret_cast<uint64_t>(ycbcr[0].cb) + uv_size;
372 GetYuvUbwcSPPlaneInfo(field_base, width, height, COLOR_FMT_NV12_UBWC, &ycbcr[1]);
373}
374
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700375void GetYuvSPPlaneInfo(uint64_t base, uint32_t width, uint32_t height, uint32_t bpp,
376 struct android_ycbcr *ycbcr) {
377 unsigned int ystride, cstride;
378
379 ystride = cstride = UINT(width) * bpp;
380 ycbcr->y = reinterpret_cast<void *>(base);
381 ycbcr->cb = reinterpret_cast<void *>(base + ystride * UINT(height));
382 ycbcr->cr = reinterpret_cast<void *>(base + ystride * UINT(height) + 1);
383 ycbcr->ystride = ystride;
384 ycbcr->cstride = cstride;
385 ycbcr->chroma_step = 2 * bpp;
386}
387
Naseer Ahmed7aff2612017-10-05 20:39:05 -0400388int GetYUVPlaneInfo(const private_handle_t *hnd, struct android_ycbcr ycbcr[2]) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700389 int err = 0;
390 uint32_t width = UINT(hnd->width);
391 uint32_t height = UINT(hnd->height);
392 int format = hnd->format;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500393 uint64_t usage = hnd->usage;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700394 unsigned int ystride, cstride;
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700395 bool interlaced = false;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700396
397 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700398
399 // Check if UBWC buffer has been rendered in linear format.
Ramakant Singhc7d07792017-07-26 15:36:33 +0530400 int linear_format = 0;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500401 if (getMetaData(const_cast<private_handle_t *>(hnd), GET_LINEAR_FORMAT, &linear_format) == 0) {
402 format = INT(linear_format);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700403 }
404
405 // Check metadata if the geometry has been updated.
Ramakant Singhc7d07792017-07-26 15:36:33 +0530406 BufferDim_t buffer_dim;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500407 if (getMetaData(const_cast<private_handle_t *>(hnd), GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
408 BufferInfo info(buffer_dim.sliceWidth, buffer_dim.sliceHeight, format, usage);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700409 GetAlignedWidthAndHeight(info, &width, &height);
410 }
411
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700412 // Check metadata for interlaced content.
Ramakant Singhc7d07792017-07-26 15:36:33 +0530413 int interlace_flag = 0;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500414 if (getMetaData(const_cast<private_handle_t *>(hnd), GET_PP_PARAM_INTERLACED, &interlace_flag) ==
415 0) {
Ramakant Singhc7d07792017-07-26 15:36:33 +0530416 interlaced = interlace_flag;
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700417 }
418
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700419 // Get the chroma offsets from the handle width/height. We take advantage
420 // of the fact the width _is_ the stride
421 switch (format) {
422 // Semiplanar
423 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
424 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
425 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
426 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
427 // Same as YCbCr_420_SP_VENUS
428 GetYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
429 break;
430
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700431 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700432 if (!interlaced) {
433 GetYuvUbwcSPPlaneInfo(hnd->base, width, height, COLOR_FMT_NV12_UBWC, ycbcr);
434 } else {
435 GetYuvUbwcInterlacedSPPlaneInfo(hnd->base, width, height, COLOR_FMT_NV12_UBWC, ycbcr);
436 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700437 ycbcr->chroma_step = 2;
438 break;
439
Mathew Joseph Karimpanala73082e2017-10-16 18:01:21 +0530440 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
441 GetYuvSPPlaneInfo(hnd->base, width, height, 2, ycbcr);
442 break;
443
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700444 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
445 GetYuvUbwcSPPlaneInfo(hnd->base, width, height, COLOR_FMT_NV12_BPP10_UBWC, ycbcr);
446 ycbcr->chroma_step = 3;
447 break;
448
449 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
450 GetYuvUbwcSPPlaneInfo(hnd->base, width, height, COLOR_FMT_P010_UBWC, ycbcr);
451 ycbcr->chroma_step = 4;
452 break;
453
Mathew Joseph Karimpanala73082e2017-10-16 18:01:21 +0530454 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
455 ystride = VENUS_Y_STRIDE(COLOR_FMT_P010, width);
456 cstride = VENUS_UV_STRIDE(COLOR_FMT_P010, width);
457 ycbcr->y = reinterpret_cast<void *>(hnd->base);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500458 ycbcr->cb =
459 reinterpret_cast<void *>(hnd->base + ystride * VENUS_Y_SCANLINES(COLOR_FMT_P010, height));
Mathew Joseph Karimpanala73082e2017-10-16 18:01:21 +0530460 ycbcr->cr = reinterpret_cast<void *>(hnd->base +
461 ystride * VENUS_Y_SCANLINES(COLOR_FMT_P010, height) + 1);
462 ycbcr->ystride = ystride;
463 ycbcr->cstride = cstride;
464 ycbcr->chroma_step = 4;
465 break;
466
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700467 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
468 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
469 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
470 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
471 case HAL_PIXEL_FORMAT_NV21_ZSL:
472 case HAL_PIXEL_FORMAT_RAW16:
Anjaneya Prasad Musunurib1654142017-04-18 10:32:42 +0530473 case HAL_PIXEL_FORMAT_Y16:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700474 case HAL_PIXEL_FORMAT_RAW10:
475 case HAL_PIXEL_FORMAT_RAW8:
Anjaneya Prasad Musunurib1654142017-04-18 10:32:42 +0530476 case HAL_PIXEL_FORMAT_Y8:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700477 GetYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
478 std::swap(ycbcr->cb, ycbcr->cr);
479 break;
480
481 // Planar
482 case HAL_PIXEL_FORMAT_YV12:
483 ystride = width;
484 cstride = ALIGN(width / 2, 16);
485 ycbcr->y = reinterpret_cast<void *>(hnd->base);
486 ycbcr->cr = reinterpret_cast<void *>(hnd->base + ystride * height);
487 ycbcr->cb = reinterpret_cast<void *>(hnd->base + ystride * height + cstride * height / 2);
488 ycbcr->ystride = ystride;
489 ycbcr->cstride = cstride;
490 ycbcr->chroma_step = 1;
491 break;
Camus Wong5c5e6fb2017-03-21 17:02:48 -0400492 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
493 ystride = width * 2;
494 cstride = 0;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500495 ycbcr->y = reinterpret_cast<void *>(hnd->base);
Camus Wong5c5e6fb2017-03-21 17:02:48 -0400496 ycbcr->cr = NULL;
497 ycbcr->cb = NULL;
498 ycbcr->ystride = ystride;
499 ycbcr->cstride = 0;
500 ycbcr->chroma_step = 0;
501 break;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700502 // Unsupported formats
503 case HAL_PIXEL_FORMAT_YCbCr_422_I:
504 case HAL_PIXEL_FORMAT_YCrCb_422_I:
505 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
506 default:
507 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, format);
508 err = -EINVAL;
509 }
510
511 return err;
512}
513
514// Explicitly defined UBWC formats
515bool IsUBwcFormat(int format) {
516 switch (format) {
517 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
518 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
519 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
520 return true;
521 default:
522 return false;
523 }
524}
525
526bool IsUBwcSupported(int format) {
527 // Existing HAL formats with UBWC support
528 switch (format) {
529 case HAL_PIXEL_FORMAT_BGR_565:
530 case HAL_PIXEL_FORMAT_RGBA_8888:
531 case HAL_PIXEL_FORMAT_RGBX_8888:
532 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
533 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
534 case HAL_PIXEL_FORMAT_RGBA_1010102:
535 case HAL_PIXEL_FORMAT_RGBX_1010102:
536 return true;
537 default:
538 break;
539 }
540
541 return false;
542}
543
Naseer Ahmede36f2242017-12-01 15:33:56 -0500544bool IsUBwcEnabled(int format, uint64_t usage) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700545 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
546 if (IsUBwcFormat(format)) {
547 return true;
548 }
549
550 // Allow UBWC, if an OpenGL client sets UBWC usage flag and GPU plus MDP
551 // support the format. OR if a non-OpenGL client like Rotator, sets UBWC
552 // usage flag and MDP supports the format.
Naseer Ahmede36f2242017-12-01 15:33:56 -0500553 if ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) && IsUBwcSupported(format)) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700554 bool enable = true;
555 // Query GPU for UBWC only if buffer is intended to be used by GPU.
Naseer Ahmede36f2242017-12-01 15:33:56 -0500556 if ((usage & BufferUsage::GPU_TEXTURE) || (usage & BufferUsage::GPU_RENDER_TARGET)) {
Pramodh Kumar Mukunda927729d2017-06-15 16:18:51 +0530557 if (AdrenoMemInfo::GetInstance()) {
558 enable = AdrenoMemInfo::GetInstance()->IsUBWCSupportedByGPU(format);
559 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700560 }
561
562 // Allow UBWC, only if CPU usage flags are not set
Naseer Ahmede36f2242017-12-01 15:33:56 -0500563 if (enable && !(CpuCanAccess(usage))) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700564 return true;
565 }
566 }
567
568 return false;
569}
570
571void GetYuvUBwcWidthAndHeight(int width, int height, int format, unsigned int *aligned_w,
572 unsigned int *aligned_h) {
573 switch (format) {
574 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
575 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
576 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
577 *aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
578 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
579 break;
580 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
581 // The macro returns the stride which is 4/3 times the width, hence * 3/4
582 *aligned_w = (VENUS_Y_STRIDE(COLOR_FMT_NV12_BPP10_UBWC, width) * 3) / 4;
583 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_BPP10_UBWC, height);
584 break;
585 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
586 // The macro returns the stride which is 2 times the width, hence / 2
587 *aligned_w = (VENUS_Y_STRIDE(COLOR_FMT_P010_UBWC, width) / 2);
588 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_P010_UBWC, height);
589 break;
590 default:
591 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
592 *aligned_w = 0;
593 *aligned_h = 0;
594 break;
595 }
596}
597
598void GetRgbUBwcBlockSize(uint32_t bpp, int *block_width, int *block_height) {
599 *block_width = 0;
600 *block_height = 0;
601
602 switch (bpp) {
603 case 2:
604 case 4:
605 *block_width = 16;
606 *block_height = 4;
607 break;
608 case 8:
609 *block_width = 8;
610 *block_height = 4;
611 break;
612 case 16:
613 *block_width = 4;
614 *block_height = 4;
615 break;
616 default:
617 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
618 break;
619 }
620}
621
622unsigned int GetRgbUBwcMetaBufferSize(int width, int height, uint32_t bpp) {
623 unsigned int size = 0;
624 int meta_width, meta_height;
625 int block_width, block_height;
626
627 GetRgbUBwcBlockSize(bpp, &block_width, &block_height);
628 if (!block_width || !block_height) {
629 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
630 return size;
631 }
632
633 // Align meta buffer height to 16 blocks
634 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
635
636 // Align meta buffer width to 64 blocks
637 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
638
639 // Align meta buffer size to 4K
640 size = (unsigned int)ALIGN((meta_width * meta_height), 4096);
641
642 return size;
643}
644
645unsigned int GetUBwcSize(int width, int height, int format, unsigned int alignedw,
646 unsigned int alignedh) {
647 unsigned int size = 0;
648 uint32_t bpp = 0;
649 switch (format) {
650 case HAL_PIXEL_FORMAT_BGR_565:
651 case HAL_PIXEL_FORMAT_RGBA_8888:
652 case HAL_PIXEL_FORMAT_RGBX_8888:
653 case HAL_PIXEL_FORMAT_RGBA_1010102:
654 case HAL_PIXEL_FORMAT_RGBX_1010102:
655 bpp = GetBppForUncompressedRGB(format);
656 size = alignedw * alignedh * bpp;
657 size += GetRgbUBwcMetaBufferSize(width, height, bpp);
658 break;
659 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
660 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
661 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
662 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
663 break;
664 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
665 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_BPP10_UBWC, width, height);
666 break;
667 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
668 size = VENUS_BUFFER_SIZE(COLOR_FMT_P010_UBWC, width, height);
669 break;
670 default:
671 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
672 break;
673 }
674
675 return size;
676}
677
678int GetRgbDataAddress(private_handle_t *hnd, void **rgb_data) {
679 int err = 0;
680
681 // This api is for RGB* formats
Naseer Ahmede36f2242017-12-01 15:33:56 -0500682 if (!IsUncompressedRGBFormat(hnd->format)) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700683 return -EINVAL;
684 }
685
686 // linear buffer, nothing to do further
687 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
688 *rgb_data = reinterpret_cast<void *>(hnd->base);
689 return err;
690 }
691
692 unsigned int meta_size = 0;
693 uint32_t bpp = GetBppForUncompressedRGB(hnd->format);
694 switch (hnd->format) {
695 case HAL_PIXEL_FORMAT_BGR_565:
696 case HAL_PIXEL_FORMAT_RGBA_8888:
697 case HAL_PIXEL_FORMAT_RGBX_8888:
698 case HAL_PIXEL_FORMAT_RGBA_1010102:
699 case HAL_PIXEL_FORMAT_RGBX_1010102:
700 meta_size = GetRgbUBwcMetaBufferSize(hnd->width, hnd->height, bpp);
701 break;
702 default:
703 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
704 err = -EINVAL;
705 break;
706 }
707 *rgb_data = reinterpret_cast<void *>(hnd->base + meta_size);
708
709 return err;
710}
711
Naseer Ahmede36f2242017-12-01 15:33:56 -0500712void GetCustomDimensions(private_handle_t *hnd, int *stride, int *height) {
713 BufferDim_t buffer_dim;
714 int interlaced = 0;
715
716 *stride = hnd->width;
717 *height = hnd->height;
718 if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
719 *stride = buffer_dim.sliceWidth;
720 *height = buffer_dim.sliceHeight;
721 } else if (getMetaData(hnd, GET_PP_PARAM_INTERLACED, &interlaced) == 0) {
722 if (interlaced && IsUBwcFormat(hnd->format)) {
723 unsigned int alignedw = 0, alignedh = 0;
724 // Get re-aligned height for single ubwc interlaced field and
725 // multiply by 2 to get frame height.
726 BufferInfo info(hnd->width, ((hnd->height + 1) >> 1), hnd->format);
727 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
728 *stride = static_cast<int>(alignedw);
729 *height = static_cast<int>(alignedh * 2);
730 }
731 }
732}
733
734void GetColorSpaceFromMetadata(private_handle_t *hnd, int *color_space) {
735 ColorMetaData color_metadata;
736 if (getMetaData(hnd, GET_COLOR_METADATA, &color_metadata) == 0) {
737 switch (color_metadata.colorPrimaries) {
738 case ColorPrimaries_BT709_5:
739 *color_space = HAL_CSC_ITU_R_709;
740 break;
741 case ColorPrimaries_BT601_6_525:
742 case ColorPrimaries_BT601_6_625:
743 *color_space = ((color_metadata.range) ? HAL_CSC_ITU_R_601_FR : HAL_CSC_ITU_R_601);
744 break;
745 case ColorPrimaries_BT2020:
746 *color_space = (color_metadata.range) ? HAL_CSC_ITU_R_2020_FR : HAL_CSC_ITU_R_2020;
747 break;
748 default:
749 ALOGE("Unknown Color Space = %d", color_metadata.colorPrimaries);
750 break;
751 }
752 } else if (getMetaData(hnd, GET_COLOR_SPACE, color_space) != 0) {
753 *color_space = 0;
754 }
755}
756
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700757void GetAlignedWidthAndHeight(const BufferInfo &info, unsigned int *alignedw,
758 unsigned int *alignedh) {
759 int width = info.width;
760 int height = info.height;
761 int format = info.format;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500762 uint64_t usage = info.usage;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700763
764 // Currently surface padding is only computed for RGB* surfaces.
Naseer Ahmede36f2242017-12-01 15:33:56 -0500765 bool ubwc_enabled = IsUBwcEnabled(format, usage);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700766 int tile = ubwc_enabled;
767
768 if (IsUncompressedRGBFormat(format)) {
Pramodh Kumar Mukunda927729d2017-06-15 16:18:51 +0530769 if (AdrenoMemInfo::GetInstance()) {
770 AdrenoMemInfo::GetInstance()->AlignUnCompressedRGB(width, height, format, tile, alignedw,
771 alignedh);
772 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700773 return;
774 }
775
776 if (ubwc_enabled) {
777 GetYuvUBwcWidthAndHeight(width, height, format, alignedw, alignedh);
778 return;
779 }
780
781 if (IsCompressedRGBFormat(format)) {
Pramodh Kumar Mukunda927729d2017-06-15 16:18:51 +0530782 if (AdrenoMemInfo::GetInstance()) {
783 AdrenoMemInfo::GetInstance()->AlignCompressedRGB(width, height, format, alignedw, alignedh);
784 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700785 return;
786 }
787
788 int aligned_w = width;
789 int aligned_h = height;
790 unsigned int alignment = 32;
791
792 // Below should be only YUV family
793 switch (format) {
794 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
795 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
Pramodh Kumar Mukunda927729d2017-06-15 16:18:51 +0530796 if (AdrenoMemInfo::GetInstance() == nullptr) {
797 return;
798 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700799 alignment = AdrenoMemInfo::GetInstance()->GetGpuPixelAlignment();
800 aligned_w = ALIGN(width, alignment);
801 break;
802 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
803 aligned_w = ALIGN(width, alignment);
804 break;
805 case HAL_PIXEL_FORMAT_RAW16:
Anjaneya Prasad Musunurib1654142017-04-18 10:32:42 +0530806 case HAL_PIXEL_FORMAT_Y16:
807 case HAL_PIXEL_FORMAT_Y8:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700808 aligned_w = ALIGN(width, 16);
809 break;
810 case HAL_PIXEL_FORMAT_RAW12:
Naseer Ahmed1187a572017-08-14 13:16:55 -0400811 aligned_w = ALIGN(width * 12 / 8, 16);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700812 break;
813 case HAL_PIXEL_FORMAT_RAW10:
Naseer Ahmed1187a572017-08-14 13:16:55 -0400814 aligned_w = ALIGN(width * 10 / 8, 16);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700815 break;
816 case HAL_PIXEL_FORMAT_RAW8:
Naseer Ahmed1187a572017-08-14 13:16:55 -0400817 aligned_w = ALIGN(width, 16);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700818 break;
819 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
820 aligned_w = ALIGN(width, 128);
821 break;
822 case HAL_PIXEL_FORMAT_YV12:
823 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
824 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
825 case HAL_PIXEL_FORMAT_YCbCr_422_I:
826 case HAL_PIXEL_FORMAT_YCrCb_422_I:
827 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
828 aligned_w = ALIGN(width, 16);
829 break;
Mathew Joseph Karimpanala73082e2017-10-16 18:01:21 +0530830 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
831 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_P010, width) / 2);
832 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_P010, height));
833 break;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700834 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
835 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
836 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV12, width));
837 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV12, height));
838 break;
839 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
840 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV21, width));
841 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV21, height));
842 break;
843 case HAL_PIXEL_FORMAT_BLOB:
844 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
845 break;
846 case HAL_PIXEL_FORMAT_NV21_ZSL:
847 aligned_w = ALIGN(width, 64);
848 aligned_h = ALIGN(height, 64);
849 break;
850 default:
851 break;
852 }
853
854 *alignedw = (unsigned int)aligned_w;
855 *alignedh = (unsigned int)aligned_h;
856}
857
Naseer Ahmede36f2242017-12-01 15:33:56 -0500858int GetBufferLayout(private_handle_t *hnd, uint32_t stride[4], uint32_t offset[4],
859 uint32_t *num_planes) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700860 if (!hnd || !stride || !offset || !num_planes) {
861 return -EINVAL;
862 }
863
Naseer Ahmed7aff2612017-10-05 20:39:05 -0400864 struct android_ycbcr yuvPlaneInfo[2] = {};
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700865 *num_planes = 1;
866 stride[0] = 0;
867
868 switch (hnd->format) {
869 case HAL_PIXEL_FORMAT_RGB_565:
870 case HAL_PIXEL_FORMAT_BGR_565:
871 case HAL_PIXEL_FORMAT_RGBA_5551:
872 case HAL_PIXEL_FORMAT_RGBA_4444:
873 stride[0] = static_cast<uint32_t>(hnd->width * 2);
874 break;
875 case HAL_PIXEL_FORMAT_RGB_888:
876 stride[0] = static_cast<uint32_t>(hnd->width * 3);
877 break;
878 case HAL_PIXEL_FORMAT_RGBA_8888:
879 case HAL_PIXEL_FORMAT_BGRA_8888:
880 case HAL_PIXEL_FORMAT_RGBX_8888:
881 case HAL_PIXEL_FORMAT_BGRX_8888:
882 case HAL_PIXEL_FORMAT_RGBA_1010102:
883 case HAL_PIXEL_FORMAT_ARGB_2101010:
884 case HAL_PIXEL_FORMAT_RGBX_1010102:
885 case HAL_PIXEL_FORMAT_XRGB_2101010:
886 case HAL_PIXEL_FORMAT_BGRA_1010102:
887 case HAL_PIXEL_FORMAT_ABGR_2101010:
888 case HAL_PIXEL_FORMAT_BGRX_1010102:
889 case HAL_PIXEL_FORMAT_XBGR_2101010:
890 stride[0] = static_cast<uint32_t>(hnd->width * 4);
891 break;
892 }
893
894 // Format is RGB
895 if (stride[0]) {
896 return 0;
897 }
898
899 (*num_planes)++;
Naseer Ahmed7aff2612017-10-05 20:39:05 -0400900 int ret = GetYUVPlaneInfo(hnd, yuvPlaneInfo);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700901 if (ret < 0) {
902 ALOGE("%s failed", __FUNCTION__);
903 return ret;
904 }
905
Naseer Ahmed7aff2612017-10-05 20:39:05 -0400906 // We are only returning buffer layout for progressive or single field formats.
907 struct android_ycbcr yuvInfo = yuvPlaneInfo[0];
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700908 stride[0] = static_cast<uint32_t>(yuvInfo.ystride);
909 offset[0] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.y) - hnd->base);
910 stride[1] = static_cast<uint32_t>(yuvInfo.cstride);
911 switch (hnd->format) {
912 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
913 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
914 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
915 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
916 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
917 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
918 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
919 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
Mathew Joseph Karimpanala73082e2017-10-16 18:01:21 +0530920 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700921 offset[1] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cb) - hnd->base);
922 break;
923 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
924 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
925 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
926 offset[1] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cr) - hnd->base);
927 break;
928 case HAL_PIXEL_FORMAT_YV12:
929 offset[1] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cr) - hnd->base);
930 stride[2] = static_cast<uint32_t>(yuvInfo.cstride);
931 offset[2] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cb) - hnd->base);
932 (*num_planes)++;
933 break;
934 default:
935 ALOGW("%s: Unsupported format", __FUNCTION__);
936 ret = -EINVAL;
937 }
938
939 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
940 std::fill(offset, offset + 4, 0);
941 }
942
943 return 0;
944}
945
Naseer Ahmede36f2242017-12-01 15:33:56 -0500946} // namespace gralloc