blob: 6eb414e2bedac8ba6c82a66f82bef8e5d803720a [file] [log] [blame]
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05301/*
Naseer Ahmed2023cf32019-01-28 19:02:40 -05002 * Copyright (c) 2011-2019, 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
36#define ASTC_BLOCK_SIZE 16
37
38#ifndef COLOR_FMT_P010_UBWC
39#define COLOR_FMT_P010_UBWC 9
40#endif
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053041
Naseer Ahmede36f2242017-12-01 15:33:56 -050042namespace gralloc {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053043
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +053044bool IsYuvFormat(int format) {
45 switch (format) {
Saurabh Shahdbb572e2018-01-04 15:38:01 -080046 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
47 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
48 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmede36f2242017-12-01 15:33:56 -050049 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: // Same as YCbCr_420_SP_VENUS
Saurabh Shahdbb572e2018-01-04 15:38:01 -080050 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
51 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
52 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
53 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
54 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
55 case HAL_PIXEL_FORMAT_NV21_ZSL:
56 case HAL_PIXEL_FORMAT_RAW16:
57 case HAL_PIXEL_FORMAT_Y16:
58 case HAL_PIXEL_FORMAT_RAW12:
59 case HAL_PIXEL_FORMAT_RAW10:
60 case HAL_PIXEL_FORMAT_YV12:
61 case HAL_PIXEL_FORMAT_Y8:
62 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
63 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
64 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
65 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +053066 // Below formats used by camera and VR
67 case HAL_PIXEL_FORMAT_BLOB:
68 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
Rajavenu Kyatham6e8c1822018-07-31 10:27:19 +053069 case HAL_PIXEL_FORMAT_NV12_HEIF:
Camus Wonga300db62018-09-27 16:38:50 -040070 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
Saurabh Shahdbb572e2018-01-04 15:38:01 -080071 return true;
72 default:
73 return false;
74 }
75}
76
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053077bool IsUncompressedRGBFormat(int format) {
78 switch (format) {
79 case HAL_PIXEL_FORMAT_RGBA_8888:
80 case HAL_PIXEL_FORMAT_RGBX_8888:
81 case HAL_PIXEL_FORMAT_RGB_888:
82 case HAL_PIXEL_FORMAT_RGB_565:
83 case HAL_PIXEL_FORMAT_BGR_565:
84 case HAL_PIXEL_FORMAT_BGRA_8888:
85 case HAL_PIXEL_FORMAT_RGBA_5551:
86 case HAL_PIXEL_FORMAT_RGBA_4444:
87 case HAL_PIXEL_FORMAT_R_8:
88 case HAL_PIXEL_FORMAT_RG_88:
89 case HAL_PIXEL_FORMAT_BGRX_8888:
90 case HAL_PIXEL_FORMAT_RGBA_1010102:
91 case HAL_PIXEL_FORMAT_ARGB_2101010:
92 case HAL_PIXEL_FORMAT_RGBX_1010102:
93 case HAL_PIXEL_FORMAT_XRGB_2101010:
94 case HAL_PIXEL_FORMAT_BGRA_1010102:
95 case HAL_PIXEL_FORMAT_ABGR_2101010:
96 case HAL_PIXEL_FORMAT_BGRX_1010102:
97 case HAL_PIXEL_FORMAT_XBGR_2101010:
Naseer Ahmed1c473d82017-02-27 13:39:37 -050098 case HAL_PIXEL_FORMAT_RGBA_FP16:
Camus Wong5c5e6fb2017-03-21 17:02:48 -040099 case HAL_PIXEL_FORMAT_BGR_888:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530100 return true;
101 default:
102 break;
103 }
104
105 return false;
106}
107
108bool IsCompressedRGBFormat(int format) {
109 switch (format) {
110 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
111 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
112 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
113 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
114 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
115 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
116 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
117 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
118 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
119 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
120 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
121 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
122 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
123 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
124 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
125 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
126 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
127 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
128 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
129 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
130 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
131 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
132 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
133 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
134 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
135 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
136 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
137 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
138 return true;
139 default:
140 break;
141 }
142
143 return false;
144}
145
146uint32_t GetBppForUncompressedRGB(int format) {
147 uint32_t bpp = 0;
148 switch (format) {
Naseer Ahmed1c473d82017-02-27 13:39:37 -0500149 case HAL_PIXEL_FORMAT_RGBA_FP16:
150 bpp = 8;
151 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530152 case HAL_PIXEL_FORMAT_RGBA_8888:
153 case HAL_PIXEL_FORMAT_RGBX_8888:
154 case HAL_PIXEL_FORMAT_BGRA_8888:
155 case HAL_PIXEL_FORMAT_BGRX_8888:
Arun Kumar K.Rb97ca302017-04-06 15:59:33 -0700156 case HAL_PIXEL_FORMAT_RGBA_1010102:
157 case HAL_PIXEL_FORMAT_ARGB_2101010:
158 case HAL_PIXEL_FORMAT_RGBX_1010102:
159 case HAL_PIXEL_FORMAT_XRGB_2101010:
160 case HAL_PIXEL_FORMAT_BGRA_1010102:
161 case HAL_PIXEL_FORMAT_ABGR_2101010:
162 case HAL_PIXEL_FORMAT_BGRX_1010102:
163 case HAL_PIXEL_FORMAT_XBGR_2101010:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530164 bpp = 4;
165 break;
166 case HAL_PIXEL_FORMAT_RGB_888:
Camus Wong5c5e6fb2017-03-21 17:02:48 -0400167 case HAL_PIXEL_FORMAT_BGR_888:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530168 bpp = 3;
169 break;
170 case HAL_PIXEL_FORMAT_RGB_565:
171 case HAL_PIXEL_FORMAT_BGR_565:
172 case HAL_PIXEL_FORMAT_RGBA_5551:
173 case HAL_PIXEL_FORMAT_RGBA_4444:
174 bpp = 2;
175 break;
176 default:
Arun Kumar K.Rb97ca302017-04-06 15:59:33 -0700177 ALOGE("Error : %s New format request = 0x%x", __FUNCTION__, format);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530178 break;
179 }
180
181 return bpp;
182}
183
Naseer Ahmede36f2242017-12-01 15:33:56 -0500184bool CpuCanAccess(uint64_t usage) {
185 return CpuCanRead(usage) || CpuCanWrite(usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530186}
187
Naseer Ahmede36f2242017-12-01 15:33:56 -0500188bool CpuCanRead(uint64_t usage) {
189 if (usage & BufferUsage::CPU_READ_MASK) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530190 return true;
191 }
192
193 return false;
194}
195
Naseer Ahmede36f2242017-12-01 15:33:56 -0500196bool CpuCanWrite(uint64_t usage) {
197 if (usage & BufferUsage::CPU_WRITE_MASK) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530198 // Application intends to use CPU for rendering
199 return true;
200 }
201
202 return false;
203}
204
Naseer Ahmedda214ec2018-03-12 20:15:07 -0400205uint32_t GetDataAlignment(int format, uint64_t usage) {
206 uint32_t align = UINT(getpagesize());
207 if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) {
208 align = SIZE_8K;
209 }
210
211 if (usage & BufferUsage::PROTECTED) {
212 if ((usage & BufferUsage::CAMERA_OUTPUT) || (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY)) {
213 // The alignment here reflects qsee mmu V7L/V8L requirement
214 align = SZ_2M;
215 } else {
216 align = SECURE_ALIGN;
217 }
218 }
219
220 return align;
221}
222
Saurabh Dubey937c4e52018-06-26 11:31:18 +0530223bool IsGPUFlagSupported(uint64_t usage) {
224 bool ret = true;
225 if ((usage & BufferUsage::GPU_MIPMAP_COMPLETE)) {
226 ALOGE("GPU_MIPMAP_COMPLETE not supported");
227 ret = false;
228 }
229
230 if ((usage & BufferUsage::GPU_CUBE_MAP)) {
231 ALOGE("GPU_CUBE_MAP not supported");
232 ret = false;
233 }
234
235 return ret;
236}
237
Naseer Ahmedda214ec2018-03-12 20:15:07 -0400238// Returns the final buffer size meant to be allocated with ion
Naseer Ahmede36f2242017-12-01 15:33:56 -0500239unsigned int GetSize(const BufferInfo &info, unsigned int alignedw, unsigned int alignedh) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700240 unsigned int size = 0;
241 int format = info.format;
242 int width = info.width;
243 int height = info.height;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500244 uint64_t usage = info.usage;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700245
Saurabh Dubey937c4e52018-06-26 11:31:18 +0530246 if (!IsGPUFlagSupported(usage)) {
247 ALOGE("Unsupported GPU usage flags present 0x%" PRIx64, usage);
248 return 0;
249 }
250
Naseer Ahmede36f2242017-12-01 15:33:56 -0500251 if (IsUBwcEnabled(format, usage)) {
Naseer Ahmedda214ec2018-03-12 20:15:07 -0400252 size = GetUBwcSize(width, height, format, alignedw, alignedh);
253 } else if (IsUncompressedRGBFormat(format)) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700254 uint32_t bpp = GetBppForUncompressedRGB(format);
255 size = alignedw * alignedh * bpp;
Naseer Ahmedda214ec2018-03-12 20:15:07 -0400256 } else if (IsCompressedRGBFormat(format)) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700257 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
Naseer Ahmedda214ec2018-03-12 20:15:07 -0400258 } else {
259 // Below switch should be for only YUV/custom formats
260 switch (format) {
261 case HAL_PIXEL_FORMAT_RAW16:
262 case HAL_PIXEL_FORMAT_Y16:size = alignedw * alignedh * 2;
263 break;
264 case HAL_PIXEL_FORMAT_RAW10:
265 case HAL_PIXEL_FORMAT_RAW12:size = ALIGN(alignedw * alignedh, SIZE_4K);
266 break;
267 case HAL_PIXEL_FORMAT_RAW8:
268 case HAL_PIXEL_FORMAT_Y8:size = alignedw * alignedh * 1;
269 break;
270 // adreno formats
271 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
272 size = ALIGN(alignedw * alignedh, SIZE_4K);
273 size += (unsigned int) ALIGN(2 * ALIGN(width / 2, 32) * ALIGN(height / 2, 32), SIZE_4K);
274 break;
275 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
276 // The chroma plane is subsampled,
277 // but the pitch in bytes is unchanged
278 // The GPU needs 4K alignment, but the video decoder needs 8K
279 size = ALIGN(alignedw * alignedh, SIZE_8K);
280 size += ALIGN(alignedw * (unsigned int) ALIGN(height / 2, 32), SIZE_8K);
281 break;
282 case HAL_PIXEL_FORMAT_YV12:
283 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width & 1) || (height & 1))) {
284 ALOGE("w or h is odd for the YV12 format");
285 return 0;
286 }
287 size = alignedw * alignedh + (ALIGN(alignedw / 2, 16) * (alignedh / 2)) * 2;
288 size = ALIGN(size, (unsigned int) SIZE_4K);
289 break;
290 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
291 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
292 size = ALIGN((alignedw * alignedh) + (alignedw * alignedh) / 2 + 1, SIZE_4K);
293 break;
294 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
295 size = ALIGN((alignedw * alignedh * 2) + (alignedw * alignedh) + 1, SIZE_4K);
296 break;
297 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
298 size = VENUS_BUFFER_SIZE(COLOR_FMT_P010,
299 width,
300 height);
301 break;
302 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
303 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
304 case HAL_PIXEL_FORMAT_YCbCr_422_I:
305 case HAL_PIXEL_FORMAT_YCrCb_422_I:
306 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
307 if (width & 1) {
308 ALOGE("width is odd for the YUV422_SP format");
309 return 0;
310 }
311 size = ALIGN(alignedw * alignedh * 2, SIZE_4K);
312 break;
313 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
314 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
315 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
316 break;
317 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
318 case HAL_PIXEL_FORMAT_NV21_ENCODEABLE:
319 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
320 break;
321 case HAL_PIXEL_FORMAT_BLOB:
322 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
323 if (height != 1) {
324 ALOGE("%s: Buffers with HAL_PIXEL_FORMAT_BLOB must have height 1 ", __FUNCTION__);
325 return 0;
326 }
327 size = (unsigned int) width;
328 break;
329 case HAL_PIXEL_FORMAT_NV21_ZSL:
330 size = ALIGN((alignedw * alignedh) + (alignedw * alignedh) / 2,
331 SIZE_4K);
332 break;
Rajavenu Kyatham6e8c1822018-07-31 10:27:19 +0530333 case HAL_PIXEL_FORMAT_NV12_HEIF:
334 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_512, width, height);
335 break;
Naseer Ahmedda214ec2018-03-12 20:15:07 -0400336 default:ALOGE("%s: Unrecognized pixel format: 0x%x", __FUNCTION__, format);
337 return 0;
338 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700339 }
Naseer Ahmedda214ec2018-03-12 20:15:07 -0400340 auto align = GetDataAlignment(format, usage);
341 size = ALIGN(size, align) * info.layer_count;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700342 return size;
343}
344
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -0700345int GetBufferSizeAndDimensions(const BufferInfo &info, unsigned int *size, unsigned int *alignedw,
346 unsigned int *alignedh) {
Saurabh Dubeyefd7f9e2018-06-15 18:00:02 +0530347 GraphicsMetadata graphics_metadata = {};
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -0700348 return GetBufferSizeAndDimensions(info, size, alignedw, alignedh, &graphics_metadata);
Saurabh Dubeyefd7f9e2018-06-15 18:00:02 +0530349}
350
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -0700351int GetBufferSizeAndDimensions(const BufferInfo &info, unsigned int *size, unsigned int *alignedw,
352 unsigned int *alignedh, GraphicsMetadata *graphics_metadata) {
Saurabh Dubeyefd7f9e2018-06-15 18:00:02 +0530353 int buffer_type = GetBufferType(info.format);
354 if (CanUseAdrenoForSize(buffer_type, info.usage)) {
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -0700355 return GetGpuResourceSizeAndDimensions(info, size, alignedw, alignedh, graphics_metadata);
Saurabh Dubeyefd7f9e2018-06-15 18:00:02 +0530356 } else {
357 GetAlignedWidthAndHeight(info, alignedw, alignedh);
358 *size = GetSize(info, *alignedw, *alignedh);
359 }
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -0700360 return 0;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700361}
362
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530363void GetYuvUbwcSPPlaneInfo(uint32_t width, uint32_t height, int color_format,
364 PlaneLayoutInfo *plane_info) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700365 // UBWC buffer has these 4 planes in the following sequence:
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530366 // Y_Plane, UV_Plane, Y_Meta_Plane, UV_Meta_Plane
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700367 unsigned int y_meta_stride, y_meta_height, y_meta_size;
368 unsigned int y_stride, y_height, y_size;
369 unsigned int c_meta_stride, c_meta_height, c_meta_size;
370 unsigned int alignment = 4096;
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530371 unsigned int c_stride, c_height, c_size;
372 uint64_t yOffset, cOffset, yMetaOffset, cMetaOffset;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700373
374 y_meta_stride = VENUS_Y_META_STRIDE(color_format, INT(width));
375 y_meta_height = VENUS_Y_META_SCANLINES(color_format, INT(height));
376 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
377
378 y_stride = VENUS_Y_STRIDE(color_format, INT(width));
379 y_height = VENUS_Y_SCANLINES(color_format, INT(height));
380 y_size = ALIGN((y_stride * y_height), alignment);
381
382 c_meta_stride = VENUS_UV_META_STRIDE(color_format, INT(width));
383 c_meta_height = VENUS_UV_META_SCANLINES(color_format, INT(height));
384 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
385
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530386 c_stride = VENUS_UV_STRIDE(color_format, INT(width));
387 c_height = VENUS_UV_SCANLINES(color_format, INT(height));
388 c_size = ALIGN((c_stride * c_height), alignment);
389
390 yMetaOffset = 0;
391 yOffset = y_meta_size;
392 cMetaOffset = y_meta_size + y_size;
393 cOffset = y_meta_size + y_size + c_meta_size;
394
395 plane_info[0].component = (PlaneComponent)PLANE_COMPONENT_Y;
396 plane_info[0].offset = (uint32_t)yOffset;
397 plane_info[0].stride = static_cast<int32_t>(UINT(width));
398 plane_info[0].stride_bytes = static_cast<int32_t>(y_stride);
399 plane_info[0].scanlines = static_cast<int32_t>(y_height);
400 plane_info[0].size = static_cast<uint32_t>(y_size);
401
402 plane_info[1].component = (PlaneComponent)(PLANE_COMPONENT_Cb | PLANE_COMPONENT_Cr);
403 plane_info[1].offset = (uint32_t)cOffset;
404 plane_info[1].stride = static_cast<int32_t>(UINT(width));
405 plane_info[1].stride_bytes = static_cast<int32_t>(c_stride);
406 plane_info[1].scanlines = static_cast<int32_t>(c_height);
407 plane_info[1].size = static_cast<uint32_t>(c_size);
408
409 plane_info[2].component = (PlaneComponent)(PLANE_COMPONENT_META | PLANE_COMPONENT_Y);
410 plane_info[2].offset = (uint32_t)yMetaOffset;
411 plane_info[2].stride = static_cast<int32_t>(UINT(width));
412 plane_info[2].stride_bytes = static_cast<int32_t>(y_meta_stride);
413 plane_info[2].scanlines = static_cast<int32_t>(y_meta_height);
414 plane_info[2].size = static_cast<uint32_t>(y_meta_size);
415
416 plane_info[3].component =
417 (PlaneComponent)(PLANE_COMPONENT_META | PLANE_COMPONENT_Cb | PLANE_COMPONENT_Cr);
418 plane_info[3].offset = (uint32_t)cMetaOffset;
419 plane_info[3].stride = static_cast<int32_t>(UINT(width));
420 plane_info[3].stride_bytes = static_cast<int32_t>(c_meta_stride);
421 plane_info[3].scanlines = static_cast<int32_t>(c_meta_height);
422 plane_info[3].size = static_cast<uint32_t>(c_meta_size);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700423}
424
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530425// This API gets information about 8 planes (Y_Plane, UV_Plane, Y_Meta_Plane, UV_Meta_Plane,
426// Y_Plane, UV_Plane, Y_Meta_Plane, UV_Meta_Plane) and it stores the
427// information in PlaneLayoutInfo array.
428void GetYuvUbwcInterlacedSPPlaneInfo(uint32_t width, uint32_t height,
429 PlaneLayoutInfo plane_info[8]) {
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700430 // UBWC interlaced has top-bottom field layout with each field as
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530431 // 8-plane (including meta plane also) NV12_UBWC with width = image_width
432 // & height = image_height / 2.
433 // Client passed plane_info argument is ptr to struct PlaneLayoutInfo[8].
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700434 // Plane info to be filled for each field separately.
435 height = (height + 1) >> 1;
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700436
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530437 GetYuvUbwcSPPlaneInfo(width, height, COLOR_FMT_NV12_UBWC, &plane_info[0]);
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700438
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530439 GetYuvUbwcSPPlaneInfo(width, height, COLOR_FMT_NV12_UBWC, &plane_info[4]);
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700440}
441
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530442// This API gets information about 2 planes (Y_Plane & UV_Plane)
443void GetYuvSPPlaneInfo(const BufferInfo &info, int format, uint32_t width, uint32_t height,
444 uint32_t bpp, PlaneLayoutInfo *plane_info) {
445 int unaligned_width = info.width;
446 int unaligned_height = info.height;
447 unsigned int y_stride = 0, y_height = 0, y_size = 0;
448 unsigned int c_stride = 0, c_height = 0, c_size = 0;
449 uint64_t yOffset, cOffset;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700450
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530451 y_stride = c_stride = UINT(width) * bpp;
452 y_height = INT(height);
453 y_size = y_stride * y_height;
454 switch (format) {
455 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
456 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
457 c_size = (width * height) / 2 + 1;
458 c_height = height >> 1;
459 break;
460 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
461 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
462 if (unaligned_width & 1) {
463 ALOGE("width is odd for the YUV422_SP format");
464 return;
465 }
466 c_size = width * height;
467 c_height = height;
468 break;
469 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
470 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
471 c_height = VENUS_UV_SCANLINES(COLOR_FMT_NV12, height);
472 c_size = c_stride * c_height;
473 break;
474 case HAL_PIXEL_FORMAT_NV12_HEIF:
475 c_height = VENUS_UV_SCANLINES(COLOR_FMT_NV12_512, height);
476 c_size = c_stride * c_height;
477 break;
478 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
479 y_size = ALIGN(width * height, 4096);
480 c_size = ALIGN(2 * ALIGN(unaligned_width / 2, 32) * ALIGN(unaligned_height / 2, 32), 4096);
481 break;
482 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
483 c_height = VENUS_UV_SCANLINES(COLOR_FMT_NV21, height);
484 c_size = c_stride * c_height;
485 break;
486 case HAL_PIXEL_FORMAT_NV21_ZSL:
487 c_size = (width * height) / 2;
488 c_height = height >> 1;
489 break;
490 case HAL_PIXEL_FORMAT_RAW16:
491 case HAL_PIXEL_FORMAT_Y16:
492 c_size = width * height;
493 c_height = height;
494 break;
495 case HAL_PIXEL_FORMAT_RAW10:
496 c_size = 0;
497 break;
498 case HAL_PIXEL_FORMAT_RAW8:
499 case HAL_PIXEL_FORMAT_Y8:
500 c_size = 0;
501 break;
502 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
503 c_size = (width * height) + 1;
504 c_height = height;
505 break;
506 default:
507 break;
508 }
509
510 yOffset = 0;
511 cOffset = y_size;
512
513 plane_info[0].component = (PlaneComponent)PLANE_COMPONENT_Y;
514 plane_info[0].offset = (uint32_t)yOffset;
515 plane_info[0].step = 1;
516 plane_info[0].stride = static_cast<int32_t>(UINT(width));
517 plane_info[0].stride_bytes = static_cast<int32_t>(y_stride);
518 plane_info[0].scanlines = static_cast<int32_t>(y_height);
519 plane_info[0].size = static_cast<uint32_t>(y_size);
520
521 plane_info[1].component = (PlaneComponent)(PLANE_COMPONENT_Cb | PLANE_COMPONENT_Cr);
522 plane_info[1].offset = (uint32_t)cOffset;
523 plane_info[1].step = 2 * bpp;
524 plane_info[1].stride = static_cast<int32_t>(UINT(width));
525 plane_info[1].stride_bytes = static_cast<int32_t>(c_stride);
526 plane_info[1].scanlines = static_cast<int32_t>(c_height);
527 plane_info[1].size = static_cast<uint32_t>(c_size);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700528}
529
Naseer Ahmed7aff2612017-10-05 20:39:05 -0400530int GetYUVPlaneInfo(const private_handle_t *hnd, struct android_ycbcr ycbcr[2]) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700531 int err = 0;
532 uint32_t width = UINT(hnd->width);
533 uint32_t height = UINT(hnd->height);
534 int format = hnd->format;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500535 uint64_t usage = hnd->usage;
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530536 int32_t interlaced = 0;
537 int plane_count = 0;
538 int unaligned_width = INT(hnd->unaligned_width);
539 int unaligned_height = INT(hnd->unaligned_height);
540 BufferInfo info(unaligned_width, unaligned_height, format, usage);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700541
542 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700543
544 // Check if UBWC buffer has been rendered in linear format.
Ramakant Singhc7d07792017-07-26 15:36:33 +0530545 int linear_format = 0;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500546 if (getMetaData(const_cast<private_handle_t *>(hnd), GET_LINEAR_FORMAT, &linear_format) == 0) {
547 format = INT(linear_format);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700548 }
549
550 // Check metadata if the geometry has been updated.
Ramakant Singhc7d07792017-07-26 15:36:33 +0530551 BufferDim_t buffer_dim;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500552 if (getMetaData(const_cast<private_handle_t *>(hnd), GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
553 BufferInfo info(buffer_dim.sliceWidth, buffer_dim.sliceHeight, format, usage);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700554 GetAlignedWidthAndHeight(info, &width, &height);
555 }
556
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700557 // Check metadata for interlaced content.
Ramakant Singhc7d07792017-07-26 15:36:33 +0530558 int interlace_flag = 0;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500559 if (getMetaData(const_cast<private_handle_t *>(hnd), GET_PP_PARAM_INTERLACED, &interlace_flag) ==
560 0) {
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530561 if (interlace_flag) {
562 interlaced = LAYOUT_INTERLACED_FLAG;
563 }
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700564 }
565
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530566 PlaneLayoutInfo plane_info[8] = {};
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700567 // Get the chroma offsets from the handle width/height. We take advantage
568 // of the fact the width _is_ the stride
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530569 err = GetYUVPlaneInfo(info, format, width, height, interlaced, &plane_count, plane_info);
570 if (err == 0) {
571 if (interlaced && format == HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC) {
572 CopyPlaneLayoutInfotoAndroidYcbcr(hnd->base, plane_count, &plane_info[0], &ycbcr[0]);
573 unsigned int uv_stride, uv_height, uv_size;
574 unsigned int alignment = 4096;
575 uint64_t field_base;
576 height = (height + 1) >> 1;
577 uv_stride = VENUS_UV_STRIDE(COLOR_FMT_NV12_UBWC, INT(width));
578 uv_height = VENUS_UV_SCANLINES(COLOR_FMT_NV12_UBWC, INT(height));
579 uv_size = ALIGN((uv_stride * uv_height), alignment);
580 field_base = hnd->base + plane_info[1].offset + uv_size;
581 memset(ycbcr[1].reserved, 0, sizeof(ycbcr[1].reserved));
582 CopyPlaneLayoutInfotoAndroidYcbcr(field_base, plane_count, &plane_info[4], &ycbcr[1]);
583 } else {
584 CopyPlaneLayoutInfotoAndroidYcbcr(hnd->base, plane_count, plane_info, ycbcr);
585 switch (format) {
586 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
587 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
588 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
589 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
590 case HAL_PIXEL_FORMAT_NV21_ZSL:
591 case HAL_PIXEL_FORMAT_RAW16:
592 case HAL_PIXEL_FORMAT_Y16:
593 case HAL_PIXEL_FORMAT_RAW10:
594 case HAL_PIXEL_FORMAT_RAW8:
595 case HAL_PIXEL_FORMAT_Y8:
596 std::swap(ycbcr->cb, ycbcr->cr);
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700597 }
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530598 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700599 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700600 return err;
601}
602
603// Explicitly defined UBWC formats
604bool IsUBwcFormat(int format) {
605 switch (format) {
606 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
607 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
608 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
609 return true;
610 default:
611 return false;
612 }
613}
614
615bool IsUBwcSupported(int format) {
616 // Existing HAL formats with UBWC support
617 switch (format) {
618 case HAL_PIXEL_FORMAT_BGR_565:
619 case HAL_PIXEL_FORMAT_RGBA_8888:
620 case HAL_PIXEL_FORMAT_RGBX_8888:
621 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
622 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
623 case HAL_PIXEL_FORMAT_RGBA_1010102:
624 case HAL_PIXEL_FORMAT_RGBX_1010102:
Saurabh Dubey11df8e12018-07-25 19:16:25 +0530625 case HAL_PIXEL_FORMAT_DEPTH_16:
626 case HAL_PIXEL_FORMAT_DEPTH_24:
627 case HAL_PIXEL_FORMAT_DEPTH_24_STENCIL_8:
628 case HAL_PIXEL_FORMAT_DEPTH_32F:
629 case HAL_PIXEL_FORMAT_STENCIL_8:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700630 return true;
631 default:
632 break;
633 }
634
635 return false;
636}
637
Saurabh Dubey48b757d2018-05-24 19:13:53 +0530638bool IsUBwcPISupported(int format, uint64_t usage) {
639 if (usage & BufferUsage::COMPOSER_OVERLAY || !(usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC_PI)) {
640 return false;
641 }
642
643 // As of now only two formats
644 switch (format) {
645 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
646 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC: {
647 if ((usage & BufferUsage::GPU_TEXTURE) || (usage & BufferUsage::GPU_RENDER_TARGET)) {
648 if (AdrenoMemInfo::GetInstance()) {
649 return AdrenoMemInfo::GetInstance()->IsPISupportedByGPU(format, usage);
650 }
651 } else {
652 return true;
653 }
654 }
655 }
656
657 return false;
658}
659
Naseer Ahmede36f2242017-12-01 15:33:56 -0500660bool IsUBwcEnabled(int format, uint64_t usage) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700661 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
662 if (IsUBwcFormat(format)) {
663 return true;
664 }
665
666 // Allow UBWC, if an OpenGL client sets UBWC usage flag and GPU plus MDP
667 // support the format. OR if a non-OpenGL client like Rotator, sets UBWC
668 // usage flag and MDP supports the format.
Naseer Ahmed2023cf32019-01-28 19:02:40 -0500669 if (((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) ||
670 (usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC_PI) ||
671 (usage & BufferUsage::COMPOSER_CLIENT_TARGET))
Saurabh Dubey48b757d2018-05-24 19:13:53 +0530672 && IsUBwcSupported(format)) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700673 bool enable = true;
674 // Query GPU for UBWC only if buffer is intended to be used by GPU.
Naseer Ahmede36f2242017-12-01 15:33:56 -0500675 if ((usage & BufferUsage::GPU_TEXTURE) || (usage & BufferUsage::GPU_RENDER_TARGET)) {
Pramodh Kumar Mukunda927729d2017-06-15 16:18:51 +0530676 if (AdrenoMemInfo::GetInstance()) {
677 enable = AdrenoMemInfo::GetInstance()->IsUBWCSupportedByGPU(format);
678 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700679 }
680
681 // Allow UBWC, only if CPU usage flags are not set
Naseer Ahmede36f2242017-12-01 15:33:56 -0500682 if (enable && !(CpuCanAccess(usage))) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700683 return true;
684 }
685 }
686
687 return false;
688}
689
690void GetYuvUBwcWidthAndHeight(int width, int height, int format, unsigned int *aligned_w,
691 unsigned int *aligned_h) {
692 switch (format) {
693 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
694 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
695 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
696 *aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
697 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
698 break;
699 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
700 // The macro returns the stride which is 4/3 times the width, hence * 3/4
701 *aligned_w = (VENUS_Y_STRIDE(COLOR_FMT_NV12_BPP10_UBWC, width) * 3) / 4;
702 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_BPP10_UBWC, height);
703 break;
704 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
705 // The macro returns the stride which is 2 times the width, hence / 2
706 *aligned_w = (VENUS_Y_STRIDE(COLOR_FMT_P010_UBWC, width) / 2);
707 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_P010_UBWC, height);
708 break;
709 default:
710 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
711 *aligned_w = 0;
712 *aligned_h = 0;
713 break;
714 }
715}
716
717void GetRgbUBwcBlockSize(uint32_t bpp, int *block_width, int *block_height) {
718 *block_width = 0;
719 *block_height = 0;
720
721 switch (bpp) {
722 case 2:
723 case 4:
724 *block_width = 16;
725 *block_height = 4;
726 break;
727 case 8:
728 *block_width = 8;
729 *block_height = 4;
730 break;
731 case 16:
732 *block_width = 4;
733 *block_height = 4;
734 break;
735 default:
736 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
737 break;
738 }
739}
740
741unsigned int GetRgbUBwcMetaBufferSize(int width, int height, uint32_t bpp) {
742 unsigned int size = 0;
743 int meta_width, meta_height;
744 int block_width, block_height;
745
746 GetRgbUBwcBlockSize(bpp, &block_width, &block_height);
747 if (!block_width || !block_height) {
748 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
749 return size;
750 }
751
752 // Align meta buffer height to 16 blocks
753 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
754
755 // Align meta buffer width to 64 blocks
756 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
757
758 // Align meta buffer size to 4K
759 size = (unsigned int)ALIGN((meta_width * meta_height), 4096);
760
761 return size;
762}
763
764unsigned int GetUBwcSize(int width, int height, int format, unsigned int alignedw,
765 unsigned int alignedh) {
766 unsigned int size = 0;
767 uint32_t bpp = 0;
768 switch (format) {
769 case HAL_PIXEL_FORMAT_BGR_565:
770 case HAL_PIXEL_FORMAT_RGBA_8888:
771 case HAL_PIXEL_FORMAT_RGBX_8888:
772 case HAL_PIXEL_FORMAT_RGBA_1010102:
773 case HAL_PIXEL_FORMAT_RGBX_1010102:
774 bpp = GetBppForUncompressedRGB(format);
775 size = alignedw * alignedh * bpp;
776 size += GetRgbUBwcMetaBufferSize(width, height, bpp);
777 break;
778 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
779 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
780 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
781 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
782 break;
783 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
784 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_BPP10_UBWC, width, height);
785 break;
786 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
787 size = VENUS_BUFFER_SIZE(COLOR_FMT_P010_UBWC, width, height);
788 break;
789 default:
790 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
791 break;
792 }
793
794 return size;
795}
796
Ashish Kumar8a1b56e2019-11-15 18:15:22 +0530797unsigned int GetRgbMetaSize(int format, uint32_t width, uint32_t height, uint64_t usage) {
798 unsigned int meta_size = 0;
799 if (!IsUBwcEnabled(format, usage)) {
800 return meta_size;
801 }
802 uint32_t bpp = GetBppForUncompressedRGB(format);
803 switch (format) {
804 case HAL_PIXEL_FORMAT_BGR_565:
805 case HAL_PIXEL_FORMAT_RGBA_8888:
806 case HAL_PIXEL_FORMAT_RGBX_8888:
807 case HAL_PIXEL_FORMAT_RGBA_1010102:
808 case HAL_PIXEL_FORMAT_RGBX_1010102:
809 case HAL_PIXEL_FORMAT_RGBA_FP16:
810 meta_size = GetRgbUBwcMetaBufferSize(width, height, bpp);
811 break;
812 default:
813 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, format);
814 break;
815 }
816 return meta_size;
817}
818
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700819int GetRgbDataAddress(private_handle_t *hnd, void **rgb_data) {
820 int err = 0;
821
822 // This api is for RGB* formats
Naseer Ahmede36f2242017-12-01 15:33:56 -0500823 if (!IsUncompressedRGBFormat(hnd->format)) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700824 return -EINVAL;
825 }
826
827 // linear buffer, nothing to do further
828 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
829 *rgb_data = reinterpret_cast<void *>(hnd->base);
830 return err;
831 }
Ashish Kumar8a1b56e2019-11-15 18:15:22 +0530832 unsigned int meta_size = GetRgbMetaSize(hnd->format, hnd->width, hnd->height, hnd->usage);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700833
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700834 *rgb_data = reinterpret_cast<void *>(hnd->base + meta_size);
835
836 return err;
837}
838
Naseer Ahmede36f2242017-12-01 15:33:56 -0500839void GetCustomDimensions(private_handle_t *hnd, int *stride, int *height) {
840 BufferDim_t buffer_dim;
841 int interlaced = 0;
842
843 *stride = hnd->width;
844 *height = hnd->height;
845 if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
846 *stride = buffer_dim.sliceWidth;
847 *height = buffer_dim.sliceHeight;
848 } else if (getMetaData(hnd, GET_PP_PARAM_INTERLACED, &interlaced) == 0) {
849 if (interlaced && IsUBwcFormat(hnd->format)) {
850 unsigned int alignedw = 0, alignedh = 0;
851 // Get re-aligned height for single ubwc interlaced field and
852 // multiply by 2 to get frame height.
853 BufferInfo info(hnd->width, ((hnd->height + 1) >> 1), hnd->format);
854 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
855 *stride = static_cast<int>(alignedw);
856 *height = static_cast<int>(alignedh * 2);
857 }
858 }
859}
860
861void GetColorSpaceFromMetadata(private_handle_t *hnd, int *color_space) {
862 ColorMetaData color_metadata;
863 if (getMetaData(hnd, GET_COLOR_METADATA, &color_metadata) == 0) {
864 switch (color_metadata.colorPrimaries) {
865 case ColorPrimaries_BT709_5:
866 *color_space = HAL_CSC_ITU_R_709;
867 break;
868 case ColorPrimaries_BT601_6_525:
869 case ColorPrimaries_BT601_6_625:
870 *color_space = ((color_metadata.range) ? HAL_CSC_ITU_R_601_FR : HAL_CSC_ITU_R_601);
871 break;
872 case ColorPrimaries_BT2020:
873 *color_space = (color_metadata.range) ? HAL_CSC_ITU_R_2020_FR : HAL_CSC_ITU_R_2020;
874 break;
875 default:
876 ALOGE("Unknown Color Space = %d", color_metadata.colorPrimaries);
877 break;
878 }
879 } else if (getMetaData(hnd, GET_COLOR_SPACE, color_space) != 0) {
880 *color_space = 0;
881 }
882}
883
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700884void GetAlignedWidthAndHeight(const BufferInfo &info, unsigned int *alignedw,
885 unsigned int *alignedh) {
886 int width = info.width;
887 int height = info.height;
888 int format = info.format;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500889 uint64_t usage = info.usage;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700890
891 // Currently surface padding is only computed for RGB* surfaces.
Naseer Ahmede36f2242017-12-01 15:33:56 -0500892 bool ubwc_enabled = IsUBwcEnabled(format, usage);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700893 int tile = ubwc_enabled;
894
895 if (IsUncompressedRGBFormat(format)) {
Pramodh Kumar Mukunda927729d2017-06-15 16:18:51 +0530896 if (AdrenoMemInfo::GetInstance()) {
897 AdrenoMemInfo::GetInstance()->AlignUnCompressedRGB(width, height, format, tile, alignedw,
898 alignedh);
899 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700900 return;
901 }
902
903 if (ubwc_enabled) {
904 GetYuvUBwcWidthAndHeight(width, height, format, alignedw, alignedh);
905 return;
906 }
907
908 if (IsCompressedRGBFormat(format)) {
Pramodh Kumar Mukunda927729d2017-06-15 16:18:51 +0530909 if (AdrenoMemInfo::GetInstance()) {
910 AdrenoMemInfo::GetInstance()->AlignCompressedRGB(width, height, format, alignedw, alignedh);
911 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700912 return;
913 }
914
915 int aligned_w = width;
916 int aligned_h = height;
917 unsigned int alignment = 32;
918
919 // Below should be only YUV family
920 switch (format) {
921 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
922 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
Pramodh Kumar Mukunda927729d2017-06-15 16:18:51 +0530923 if (AdrenoMemInfo::GetInstance() == nullptr) {
924 return;
925 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700926 alignment = AdrenoMemInfo::GetInstance()->GetGpuPixelAlignment();
927 aligned_w = ALIGN(width, alignment);
928 break;
929 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
930 aligned_w = ALIGN(width, alignment);
931 break;
932 case HAL_PIXEL_FORMAT_RAW16:
Anjaneya Prasad Musunurib1654142017-04-18 10:32:42 +0530933 case HAL_PIXEL_FORMAT_Y16:
934 case HAL_PIXEL_FORMAT_Y8:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700935 aligned_w = ALIGN(width, 16);
936 break;
937 case HAL_PIXEL_FORMAT_RAW12:
Naseer Ahmed1187a572017-08-14 13:16:55 -0400938 aligned_w = ALIGN(width * 12 / 8, 16);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700939 break;
940 case HAL_PIXEL_FORMAT_RAW10:
Naseer Ahmed1187a572017-08-14 13:16:55 -0400941 aligned_w = ALIGN(width * 10 / 8, 16);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700942 break;
943 case HAL_PIXEL_FORMAT_RAW8:
Naseer Ahmed1187a572017-08-14 13:16:55 -0400944 aligned_w = ALIGN(width, 16);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700945 break;
946 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
947 aligned_w = ALIGN(width, 128);
948 break;
949 case HAL_PIXEL_FORMAT_YV12:
950 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
951 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
952 case HAL_PIXEL_FORMAT_YCbCr_422_I:
953 case HAL_PIXEL_FORMAT_YCrCb_422_I:
954 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
955 aligned_w = ALIGN(width, 16);
956 break;
Mathew Joseph Karimpanala73082e2017-10-16 18:01:21 +0530957 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
958 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_P010, width) / 2);
959 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_P010, height));
960 break;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700961 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
962 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
963 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV12, width));
964 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV12, height));
965 break;
966 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
967 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV21, width));
968 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV21, height));
969 break;
970 case HAL_PIXEL_FORMAT_BLOB:
971 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
972 break;
973 case HAL_PIXEL_FORMAT_NV21_ZSL:
974 aligned_w = ALIGN(width, 64);
975 aligned_h = ALIGN(height, 64);
976 break;
Rajavenu Kyatham6e8c1822018-07-31 10:27:19 +0530977 case HAL_PIXEL_FORMAT_NV12_HEIF:
978 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV12_512, width));
979 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV12_512, height));
980 break;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700981 default:
982 break;
983 }
984
985 *alignedw = (unsigned int)aligned_w;
986 *alignedh = (unsigned int)aligned_h;
987}
988
Naseer Ahmede36f2242017-12-01 15:33:56 -0500989int GetBufferLayout(private_handle_t *hnd, uint32_t stride[4], uint32_t offset[4],
990 uint32_t *num_planes) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700991 if (!hnd || !stride || !offset || !num_planes) {
992 return -EINVAL;
993 }
994
Naseer Ahmed7aff2612017-10-05 20:39:05 -0400995 struct android_ycbcr yuvPlaneInfo[2] = {};
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700996 *num_planes = 1;
997 stride[0] = 0;
998
999 switch (hnd->format) {
1000 case HAL_PIXEL_FORMAT_RGB_565:
1001 case HAL_PIXEL_FORMAT_BGR_565:
1002 case HAL_PIXEL_FORMAT_RGBA_5551:
1003 case HAL_PIXEL_FORMAT_RGBA_4444:
1004 stride[0] = static_cast<uint32_t>(hnd->width * 2);
1005 break;
1006 case HAL_PIXEL_FORMAT_RGB_888:
1007 stride[0] = static_cast<uint32_t>(hnd->width * 3);
1008 break;
1009 case HAL_PIXEL_FORMAT_RGBA_8888:
1010 case HAL_PIXEL_FORMAT_BGRA_8888:
1011 case HAL_PIXEL_FORMAT_RGBX_8888:
1012 case HAL_PIXEL_FORMAT_BGRX_8888:
1013 case HAL_PIXEL_FORMAT_RGBA_1010102:
1014 case HAL_PIXEL_FORMAT_ARGB_2101010:
1015 case HAL_PIXEL_FORMAT_RGBX_1010102:
1016 case HAL_PIXEL_FORMAT_XRGB_2101010:
1017 case HAL_PIXEL_FORMAT_BGRA_1010102:
1018 case HAL_PIXEL_FORMAT_ABGR_2101010:
1019 case HAL_PIXEL_FORMAT_BGRX_1010102:
1020 case HAL_PIXEL_FORMAT_XBGR_2101010:
1021 stride[0] = static_cast<uint32_t>(hnd->width * 4);
1022 break;
1023 }
1024
1025 // Format is RGB
1026 if (stride[0]) {
1027 return 0;
1028 }
1029
1030 (*num_planes)++;
Naseer Ahmed7aff2612017-10-05 20:39:05 -04001031 int ret = GetYUVPlaneInfo(hnd, yuvPlaneInfo);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001032 if (ret < 0) {
1033 ALOGE("%s failed", __FUNCTION__);
1034 return ret;
1035 }
1036
Naseer Ahmed7aff2612017-10-05 20:39:05 -04001037 // We are only returning buffer layout for progressive or single field formats.
1038 struct android_ycbcr yuvInfo = yuvPlaneInfo[0];
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001039 stride[0] = static_cast<uint32_t>(yuvInfo.ystride);
1040 offset[0] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.y) - hnd->base);
1041 stride[1] = static_cast<uint32_t>(yuvInfo.cstride);
1042 switch (hnd->format) {
1043 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
1044 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
1045 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1046 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1047 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1048 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
1049 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1050 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
Mathew Joseph Karimpanala73082e2017-10-16 18:01:21 +05301051 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
Rajavenu Kyatham6e8c1822018-07-31 10:27:19 +05301052 case HAL_PIXEL_FORMAT_NV12_HEIF:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001053 offset[1] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cb) - hnd->base);
1054 break;
1055 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1056 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
1057 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
1058 offset[1] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cr) - hnd->base);
1059 break;
1060 case HAL_PIXEL_FORMAT_YV12:
1061 offset[1] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cr) - hnd->base);
1062 stride[2] = static_cast<uint32_t>(yuvInfo.cstride);
1063 offset[2] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cb) - hnd->base);
1064 (*num_planes)++;
1065 break;
Camus Wonga300db62018-09-27 16:38:50 -04001066 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
1067 *num_planes = 1;
1068 break;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001069 default:
1070 ALOGW("%s: Unsupported format", __FUNCTION__);
1071 ret = -EINVAL;
1072 }
1073
1074 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
1075 std::fill(offset, offset + 4, 0);
1076 }
1077
1078 return 0;
1079}
1080
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -07001081int GetGpuResourceSizeAndDimensions(const BufferInfo &info, unsigned int *size,
1082 unsigned int *alignedw, unsigned int *alignedh,
1083 GraphicsMetadata *graphics_metadata) {
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301084 GetAlignedWidthAndHeight(info, alignedw, alignedh);
1085 AdrenoMemInfo* adreno_mem_info = AdrenoMemInfo::GetInstance();
1086 graphics_metadata->size = adreno_mem_info->AdrenoGetMetadataBlobSize();
1087 uint64_t adreno_usage = info.usage;
1088 // If gralloc disables UBWC based on any of the checks,
1089 // we pass modified usage flag to adreno to convey this.
1090 int is_ubwc_enabled = IsUBwcEnabled(info.format, info.usage);
1091 if (!is_ubwc_enabled) {
1092 adreno_usage &= ~(GRALLOC_USAGE_PRIVATE_ALLOC_UBWC);
Naseer Ahmed2023cf32019-01-28 19:02:40 -05001093 } else {
1094 adreno_usage |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301095 }
1096
1097 // Call adreno api for populating metadata blob
Saurabh Dubeyb637bb32018-05-15 16:20:10 +05301098 // Layer count is for 2D/Cubemap arrays and depth is used for 3D slice
1099 // Using depth to pass layer_count here
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301100 int ret = adreno_mem_info->AdrenoInitMemoryLayout(graphics_metadata->data, info.width,
Saurabh Dubeyb637bb32018-05-15 16:20:10 +05301101 info.height, info.layer_count, /* depth */
1102 info.format, 1, is_ubwc_enabled,
1103 adreno_usage, 1);
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301104 if (ret != 0) {
1105 ALOGE("%s Graphics metadata init failed", __FUNCTION__);
1106 *size = 0;
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -07001107 return -EINVAL;
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301108 }
1109 // Call adreno api with the metadata blob to get buffer size
1110 *size = adreno_mem_info->AdrenoGetAlignedGpuBufferSize(graphics_metadata->data);
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -07001111 return 0;
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301112}
1113
Saurabh Dubey52eb7a62018-06-15 16:31:23 +05301114bool CanUseAdrenoForSize(int buffer_type, uint64_t usage) {
1115 if (buffer_type == BUFFER_TYPE_VIDEO || !GetAdrenoSizeAPIStatus()) {
1116 return false;
1117 }
1118
1119 if ((usage & BufferUsage::PROTECTED) && ((usage & BufferUsage::CAMERA_OUTPUT) ||
1120 (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY))) {
1121 return false;
1122 }
1123
1124 return true;
1125}
1126
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301127bool GetAdrenoSizeAPIStatus() {
1128 AdrenoMemInfo* adreno_mem_info = AdrenoMemInfo::GetInstance();
1129 if (adreno_mem_info) {
1130 return adreno_mem_info->AdrenoSizeAPIAvaliable();
1131 }
1132 return false;
1133}
1134
Saurabh Dubey48b757d2018-05-24 19:13:53 +05301135bool UseUncached(int format, uint64_t usage) {
1136 if ((usage & GRALLOC_USAGE_PRIVATE_UNCACHED) || (usage & BufferUsage::PROTECTED)) {
1137 return true;
1138 }
1139
1140 // CPU read rarely
1141 if ((usage & BufferUsage::CPU_READ_MASK) == static_cast<uint64_t>(BufferUsage::CPU_READ_RARELY)) {
1142 return true;
1143 }
1144
1145 // CPU write rarely
1146 if ((usage & BufferUsage::CPU_WRITE_MASK) ==
1147 static_cast<uint64_t>(BufferUsage::CPU_WRITE_RARELY)) {
1148 return true;
1149 }
1150
1151 if ((usage & BufferUsage::SENSOR_DIRECT_DATA) || (usage & BufferUsage::GPU_DATA_BUFFER)) {
1152 return true;
1153 }
1154
1155 if (format && IsUBwcEnabled(format, usage)) {
1156 return true;
1157 }
1158
1159 return false;
1160}
1161
1162uint64_t GetHandleFlags(int format, uint64_t usage) {
1163 uint64_t priv_flags = 0;
1164
1165 if (usage & BufferUsage::VIDEO_ENCODER) {
1166 priv_flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
1167 }
1168
1169 if (usage & BufferUsage::CAMERA_OUTPUT) {
1170 priv_flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
1171 }
1172
1173 if (usage & BufferUsage::CAMERA_INPUT) {
1174 priv_flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
1175 }
1176
1177 if (usage & BufferUsage::COMPOSER_OVERLAY) {
1178 priv_flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER;
1179 }
1180
1181 if (usage & BufferUsage::GPU_TEXTURE) {
1182 priv_flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
1183 }
1184
1185 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
1186 priv_flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
1187 }
1188
1189 if (IsUBwcEnabled(format, usage)) {
1190 if (IsUBwcPISupported(format, usage)) {
1191 priv_flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED_PI;
1192 } else {
1193 priv_flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
1194 }
1195 }
1196
1197 if (usage & (BufferUsage::CPU_READ_MASK | BufferUsage::CPU_WRITE_MASK)) {
1198 priv_flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED;
1199 }
1200
1201 if ((usage & (BufferUsage::VIDEO_ENCODER | BufferUsage::VIDEO_DECODER |
1202 BufferUsage::CAMERA_OUTPUT | BufferUsage::GPU_RENDER_TARGET))) {
1203 priv_flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER;
1204 }
1205
1206 if (!UseUncached(format, usage)) {
1207 priv_flags |= private_handle_t::PRIV_FLAGS_CACHED;
1208 }
1209
1210 return priv_flags;
1211}
1212
1213int GetImplDefinedFormat(uint64_t usage, int format) {
1214 int gr_format = format;
1215
1216 // If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
1217 // the usage bits, gralloc assigns a format.
1218 if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED ||
1219 format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
Mathew Joseph Karimpanal71517182018-08-14 16:40:34 +05301220 if ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC || usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC_PI)
1221 && format != HAL_PIXEL_FORMAT_YCbCr_420_888) {
Saurabh Dubey81aef932018-06-27 09:41:31 +05301222 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
Saurabh Dubey48b757d2018-05-24 19:13:53 +05301223 } else if (usage & BufferUsage::VIDEO_ENCODER) {
1224 if (usage & GRALLOC_USAGE_PRIVATE_VIDEO_NV21_ENCODER) {
1225 gr_format = HAL_PIXEL_FORMAT_NV21_ENCODEABLE; // NV21
Manikanta Kanamarlapudidfa32e82019-03-26 14:06:47 +05301226 } else if (usage & GRALLOC_USAGE_PRIVATE_HEIF) {
1227 gr_format = HAL_PIXEL_FORMAT_NV12_HEIF;
Mathew Joseph Karimpanal71517182018-08-14 16:40:34 +05301228 } else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
1229 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS;
Saurabh Dubey48b757d2018-05-24 19:13:53 +05301230 } else {
1231 gr_format = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; // NV12
1232 }
1233 } else if (usage & BufferUsage::CAMERA_INPUT) {
1234 if (usage & BufferUsage::CAMERA_OUTPUT) {
1235 // Assumed ZSL if both producer and consumer camera flags set
1236 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL; // NV21
1237 } else {
1238 gr_format = HAL_PIXEL_FORMAT_YCrCb_420_SP; // NV21
1239 }
1240 } else if (usage & BufferUsage::CAMERA_OUTPUT) {
1241 if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
1242 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL; // NV21
1243 } else {
1244 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS; // NV12 preview
1245 }
1246 } else if (usage & BufferUsage::COMPOSER_OVERLAY) {
1247 // XXX: If we still haven't set a format, default to RGBA8888
1248 gr_format = HAL_PIXEL_FORMAT_RGBA_8888;
1249 } else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
1250 // If no other usage flags are detected, default the
1251 // flexible YUV format to NV21_ZSL
1252 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL;
1253 }
1254 }
1255
1256 return gr_format;
1257}
1258
1259int GetCustomFormatFlags(int format, uint64_t usage,
1260 int *custom_format, uint64_t *priv_flags) {
1261 *custom_format = GetImplDefinedFormat(usage, format);
1262 *priv_flags = GetHandleFlags(*custom_format, usage);
1263
Saurabh Dubey81aef932018-06-27 09:41:31 +05301264 if (usage & GRALLOC_USAGE_PROTECTED) {
1265 *priv_flags |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
1266 }
1267
1268 *priv_flags |= private_handle_t::PRIV_FLAGS_USES_ION;
1269
Saurabh Dubey48b757d2018-05-24 19:13:53 +05301270 return 0;
1271}
1272
Saurabh Dubeyefd7f9e2018-06-15 18:00:02 +05301273int GetBufferType(int inputFormat) {
1274 return IsYuvFormat(inputFormat) ? BUFFER_TYPE_VIDEO : BUFFER_TYPE_UI;
1275}
1276
Ashish Kumarb9392ff2019-03-27 18:30:39 +05301277int GetYUVPlaneInfo(const BufferInfo &info, int32_t format, int32_t width, int32_t height,
1278 int32_t flags, int *plane_count, PlaneLayoutInfo *plane_info) {
1279 int err = 0;
1280 unsigned int y_stride, c_stride, y_height, c_height, y_size, c_size;
1281 uint64_t yOffset, cOffset, crOffset, cbOffset;
1282 int h_subsampling = 0, v_subsampling = 0;
1283 switch (format) {
1284 // Semiplanar
1285 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
1286 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
1287 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1288 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1289 case HAL_PIXEL_FORMAT_NV12_HEIF: // Same as YCbCr_420_SP_VENUS
1290 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1291 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
1292 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
1293 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
1294 case HAL_PIXEL_FORMAT_NV21_ZSL:
1295 case HAL_PIXEL_FORMAT_RAW16:
1296 case HAL_PIXEL_FORMAT_Y16:
1297 case HAL_PIXEL_FORMAT_RAW10:
1298 case HAL_PIXEL_FORMAT_RAW8:
1299 case HAL_PIXEL_FORMAT_Y8:
1300 *plane_count = 2;
1301 GetYuvSPPlaneInfo(info, format, width, height, 1, plane_info);
1302 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1303 plane_info[0].h_subsampling = 0;
1304 plane_info[0].v_subsampling = 0;
1305 plane_info[1].h_subsampling = h_subsampling;
1306 plane_info[1].v_subsampling = v_subsampling;
1307 break;
1308
1309 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1310 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1311 if (flags & LAYOUT_INTERLACED_FLAG) {
1312 *plane_count = 8;
1313 GetYuvUbwcInterlacedSPPlaneInfo(width, height, plane_info);
1314 plane_info[0].step = plane_info[4].step = 1;
1315 plane_info[1].step = plane_info[5].step = 2;
1316 plane_info[0].h_subsampling = plane_info[4].h_subsampling = 0;
1317 plane_info[0].v_subsampling = plane_info[4].v_subsampling = 0;
1318 plane_info[1].h_subsampling = plane_info[5].h_subsampling = h_subsampling;
1319 plane_info[1].v_subsampling = plane_info[5].v_subsampling = v_subsampling;
1320 plane_info[2].h_subsampling = plane_info[3].h_subsampling = 0;
1321 plane_info[2].v_subsampling = plane_info[3].v_subsampling = 0;
1322 plane_info[2].step = plane_info[3].step = 0;
1323 plane_info[6].h_subsampling = plane_info[7].h_subsampling = 0;
1324 plane_info[6].v_subsampling = plane_info[7].v_subsampling = 0;
1325 plane_info[6].step = plane_info[7].step = 0;
1326 } else {
1327 *plane_count = 4;
1328 GetYuvUbwcSPPlaneInfo(width, height, COLOR_FMT_NV12_UBWC, plane_info);
1329 plane_info[0].h_subsampling = 0;
1330 plane_info[0].v_subsampling = 0;
1331 plane_info[0].step = 1;
1332 plane_info[1].h_subsampling = h_subsampling;
1333 plane_info[1].v_subsampling = v_subsampling;
1334 plane_info[1].step = 2;
1335 plane_info[2].h_subsampling = plane_info[3].h_subsampling = 0;
1336 plane_info[2].v_subsampling = plane_info[3].v_subsampling = 0;
1337 plane_info[2].step = plane_info[3].step = 0;
1338 }
1339 break;
1340
1341 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
1342 *plane_count = 2;
1343 GetYuvSPPlaneInfo(info, format, width, height, 2, plane_info);
1344 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1345 plane_info[0].h_subsampling = 0;
1346 plane_info[0].v_subsampling = 0;
1347 plane_info[1].h_subsampling = h_subsampling;
1348 plane_info[1].v_subsampling = v_subsampling;
1349 break;
1350
1351 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1352 *plane_count = 4;
1353 GetYuvUbwcSPPlaneInfo(width, height, COLOR_FMT_NV12_BPP10_UBWC, plane_info);
1354 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1355 plane_info[0].h_subsampling = 0;
1356 plane_info[0].v_subsampling = 0;
1357 plane_info[1].step = 1;
1358 plane_info[1].h_subsampling = h_subsampling;
1359 plane_info[1].v_subsampling = v_subsampling;
1360 plane_info[1].step = 3;
1361 plane_info[2].h_subsampling = plane_info[3].h_subsampling = 0;
1362 plane_info[2].v_subsampling = plane_info[3].v_subsampling = 0;
1363 plane_info[2].step = plane_info[3].step = 0;
1364 break;
1365
1366 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
1367 *plane_count = 4;
1368 GetYuvUbwcSPPlaneInfo(width, height, COLOR_FMT_P010_UBWC, plane_info);
1369 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1370 plane_info[0].h_subsampling = 0;
1371 plane_info[0].v_subsampling = 0;
1372 plane_info[1].step = 1;
1373 plane_info[1].h_subsampling = h_subsampling;
1374 plane_info[1].v_subsampling = v_subsampling;
1375 plane_info[1].step = 4;
1376 plane_info[2].h_subsampling = plane_info[3].h_subsampling = 0;
1377 plane_info[2].v_subsampling = plane_info[3].v_subsampling = 0;
1378 plane_info[2].step = plane_info[3].step = 0;
1379 break;
1380
1381 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
1382 *plane_count = 2;
1383 y_stride = VENUS_Y_STRIDE(COLOR_FMT_P010, width);
1384 c_stride = VENUS_UV_STRIDE(COLOR_FMT_P010, width);
1385 y_height = VENUS_Y_SCANLINES(COLOR_FMT_P010, height);
1386 y_size = y_stride * y_height;
1387 yOffset = 0;
1388 cOffset = y_size;
1389 c_height = VENUS_UV_SCANLINES(COLOR_FMT_P010, INT(height));
1390 c_size = c_stride * c_height;
1391 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1392
1393 plane_info[0].component = (PlaneComponent)PLANE_COMPONENT_Y;
1394 plane_info[0].offset = (uint32_t)yOffset;
1395 plane_info[0].stride = static_cast<int32_t>(UINT(width));
1396 plane_info[0].stride_bytes = static_cast<int32_t>(y_stride);
1397 plane_info[0].scanlines = static_cast<int32_t>(y_height);
1398 plane_info[0].size = static_cast<uint32_t>(y_size);
1399 plane_info[0].step = 1;
1400 plane_info[0].h_subsampling = 0;
1401 plane_info[0].v_subsampling = 0;
1402
1403 plane_info[1].component = (PlaneComponent)(PLANE_COMPONENT_Cb | PLANE_COMPONENT_Cr);
1404 plane_info[1].offset = (uint32_t)cOffset;
1405 plane_info[1].stride = static_cast<int32_t>(UINT(width));
1406 plane_info[1].stride_bytes = static_cast<int32_t>(c_stride);
1407 plane_info[1].scanlines = static_cast<int32_t>(c_height);
1408 plane_info[1].size = static_cast<uint32_t>(c_size);
1409 plane_info[1].step = 4;
1410 plane_info[1].h_subsampling = h_subsampling;
1411 plane_info[1].v_subsampling = v_subsampling;
1412 break;
1413
1414 // Planar
1415 case HAL_PIXEL_FORMAT_YV12:
1416 if ((info.width & 1) || (info.height & 1)) {
1417 ALOGE("w or h is odd for the YV12 format");
1418 err = -EINVAL;
1419 return err;
1420 }
1421 *plane_count = 3;
1422 y_stride = width;
1423 c_stride = ALIGN(width / 2, 16);
1424 y_height = UINT(height);
1425 y_size = (y_stride * y_height);
1426 height = height >> 1;
1427 c_height = UINT(height);
1428 c_size = (c_stride * c_height);
1429 yOffset = 0;
1430 crOffset = y_size;
1431 cbOffset = (y_size + c_size);
1432 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1433
1434 plane_info[0].component = (PlaneComponent)PLANE_COMPONENT_Y;
1435 plane_info[0].offset = (uint32_t)yOffset;
1436 plane_info[0].stride = static_cast<int32_t>(UINT(width));
1437 plane_info[0].stride_bytes = static_cast<int32_t>(y_stride);
1438 plane_info[0].scanlines = static_cast<int32_t>(y_height);
1439 plane_info[0].size = static_cast<uint32_t>(y_size);
1440 plane_info[0].step = 1;
1441 plane_info[0].h_subsampling = 0;
1442 plane_info[0].v_subsampling = 0;
1443
1444 plane_info[1].component = (PlaneComponent)PLANE_COMPONENT_Cb;
1445 plane_info[1].offset = (uint32_t)cbOffset;
1446 plane_info[2].component = (PlaneComponent)PLANE_COMPONENT_Cr;
1447 plane_info[2].offset = (uint32_t)crOffset;
1448 for (int i = 1; i < 3; i++) {
1449 plane_info[i].stride = static_cast<int32_t>(UINT(width));
1450 plane_info[i].stride_bytes = static_cast<int32_t>(c_stride);
1451 plane_info[i].scanlines = static_cast<int32_t>(c_height);
1452 plane_info[i].size = static_cast<uint32_t>(c_size);
1453 plane_info[i].step = 1;
1454 plane_info[i].h_subsampling = h_subsampling;
1455 plane_info[i].v_subsampling = v_subsampling;
1456 }
1457 break;
1458 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
1459 if (info.width & 1) {
1460 ALOGE("width is odd for the YUV422_SP format");
1461 err = -EINVAL;
1462 return err;
1463 }
1464 *plane_count = 1;
1465 y_stride = width * 2;
1466 y_height = UINT(height);
1467 y_size = y_stride * y_height;
1468 yOffset = 0;
1469 plane_info[0].component = (PlaneComponent)PLANE_COMPONENT_Y;
1470 plane_info[0].offset = (uint32_t)yOffset;
1471 plane_info[0].stride = static_cast<int32_t>(UINT(width));
1472 plane_info[0].stride_bytes = static_cast<int32_t>(y_stride);
1473 plane_info[0].scanlines = static_cast<int32_t>(y_height);
1474 plane_info[0].size = static_cast<uint32_t>(y_size);
1475 plane_info[0].step = 1;
1476 plane_info[0].h_subsampling = 0;
1477 plane_info[0].v_subsampling = 0;
1478 break;
1479
1480 // Unsupported formats
1481 case HAL_PIXEL_FORMAT_YCbCr_422_I:
1482 case HAL_PIXEL_FORMAT_YCrCb_422_I:
1483 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
1484 default:
1485 *plane_count = 0;
1486 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, format);
1487 err = -EINVAL;
1488 }
1489 return err;
1490}
1491
1492void GetYuvSubSamplingFactor(int32_t format, int *h_subsampling, int *v_subsampling) {
1493 switch (format) {
1494 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
1495 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
1496 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1497 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
1498 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
1499 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1500 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1501 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
1502 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
1503 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1504 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: // Same as YCbCr_420_SP_VENUS
1505 case HAL_PIXEL_FORMAT_NV21_ZSL:
1506 case HAL_PIXEL_FORMAT_YV12:
1507 *h_subsampling = 1;
1508 *v_subsampling = 1;
1509 break;
1510 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
1511 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
1512 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
1513 *h_subsampling = 1;
1514 *v_subsampling = 0;
1515 break;
1516 case HAL_PIXEL_FORMAT_RAW16:
1517 case HAL_PIXEL_FORMAT_Y16:
1518 case HAL_PIXEL_FORMAT_RAW12:
1519 case HAL_PIXEL_FORMAT_RAW10:
1520 case HAL_PIXEL_FORMAT_Y8:
1521 case HAL_PIXEL_FORMAT_BLOB:
1522 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
1523 case HAL_PIXEL_FORMAT_NV12_HEIF:
1524 default:
1525 *h_subsampling = 0;
1526 *v_subsampling = 0;
1527 break;
1528 }
1529}
1530
1531void CopyPlaneLayoutInfotoAndroidYcbcr(uint64_t base, int plane_count, PlaneLayoutInfo *plane_info,
1532 struct android_ycbcr *ycbcr) {
1533 ycbcr->y = reinterpret_cast<void *>(base + plane_info[0].offset);
1534 ycbcr->ystride = plane_info[0].stride_bytes;
1535 if (plane_count == 1) {
1536 ycbcr->cb = NULL;
1537 ycbcr->cr = NULL;
1538 ycbcr->cstride = 0;
1539 ycbcr->chroma_step = 0;
1540 } else if (plane_count == 2 || plane_count == 4 || plane_count == 8) {
1541 /* For YUV semiplanar :-
1542 * - In progressive & linear case plane count is 2 and plane_info[0] will
1543 * contain info about Y plane and plane_info[1] will contain info about UV plane.
1544 * - In progressive & compressed case plane count is 4 then plane_info[0] will
1545 * contain info about Y plane and plane_info[1] will contain info about UV plane.
1546 * Remaining two plane (plane_info[2] & plane_info[3]) contain info about the
1547 * Y_Meta_Plane and UV_Meta_Plane.
1548 * - In interlaced & compressed case plane count is 8 then plane_info[0], plane_info[1],
1549 * plane_info[4] & plane_info[5] will contain info about Y_plane, UV_plane, Y_plane
1550 * & UV_plane. Remaining plane will contain info about the meta planes. As in this case
1551 * this API is called twice through GetYUVPlaneInfo() with address of plane_info[0] &
1552 * plane_info[4], so this will calculate the information accordingly and will fill the
1553 * ycbcr structure with interlaced plane info only.
1554 */
1555 ycbcr->cb = reinterpret_cast<void *>(base + plane_info[1].offset);
1556 ycbcr->cr = reinterpret_cast<void *>(base + plane_info[1].offset + 1);
1557 ycbcr->cstride = plane_info[1].stride_bytes;
1558 ycbcr->chroma_step = plane_info[1].step;
1559 } else if (plane_count == 3) {
1560 /* For YUV planar :-
1561 * Plane size is 3 and plane_info[0], plane_info[1], plane_info[2] will
1562 * contain info about y_plane, cb_plane and cr_plane accordingly.
1563 */
1564 ycbcr->cb = reinterpret_cast<void *>(base + plane_info[1].offset);
1565 ycbcr->cr = reinterpret_cast<void *>(base + plane_info[2].offset);
1566 ycbcr->cstride = plane_info[1].stride_bytes;
1567 ycbcr->chroma_step = plane_info[1].step;
1568 }
1569}
1570
Ashish Kumar8a1b56e2019-11-15 18:15:22 +05301571bool HasAlphaComponent(int32_t format) {
1572 switch (format) {
1573 case HAL_PIXEL_FORMAT_RGBA_8888:
1574 case HAL_PIXEL_FORMAT_BGRA_8888:
1575 case HAL_PIXEL_FORMAT_RGBA_5551:
1576 case HAL_PIXEL_FORMAT_RGBA_4444:
1577 case HAL_PIXEL_FORMAT_RGBA_1010102:
1578 case HAL_PIXEL_FORMAT_ARGB_2101010:
1579 case HAL_PIXEL_FORMAT_BGRA_1010102:
1580 case HAL_PIXEL_FORMAT_ABGR_2101010:
1581 case HAL_PIXEL_FORMAT_RGBA_FP16:
1582 return true;
1583 default:
1584 return false;
1585 }
1586}
1587
1588void GetRGBPlaneInfo(const BufferInfo &info, int32_t format, int32_t width, int32_t height,
1589 int32_t /* flags */, int *plane_count, PlaneLayoutInfo *plane_info) {
1590 uint64_t usage = info.usage;
1591 *plane_count = 1;
1592 uint32_t bpp = 0;
1593 if (IsUncompressedRGBFormat(format)) {
1594 bpp = GetBppForUncompressedRGB(format);
1595 }
1596 plane_info->component =
1597 (PlaneComponent)(PLANE_COMPONENT_R | PLANE_COMPONENT_G | PLANE_COMPONENT_B);
1598 if (HasAlphaComponent(format)) {
1599 plane_info->component = (PlaneComponent)(plane_info->component | PLANE_COMPONENT_A);
1600 }
1601 plane_info->size = GetSize(info, width, height);
1602 plane_info->step = bpp;
1603 plane_info->offset = GetRgbMetaSize(format, width, height, usage);
1604 plane_info->h_subsampling = 0;
1605 plane_info->v_subsampling = 0;
1606 plane_info->stride = width;
1607 plane_info->stride_bytes = width * plane_info->step;
1608 plane_info->scanlines = height;
1609}
1610
Naseer Ahmede36f2242017-12-01 15:33:56 -05001611} // namespace gralloc