blob: 9eca92df2c9b431317a32231a630cdcd991c5a48 [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
797int GetRgbDataAddress(private_handle_t *hnd, void **rgb_data) {
798 int err = 0;
799
800 // This api is for RGB* formats
Naseer Ahmede36f2242017-12-01 15:33:56 -0500801 if (!IsUncompressedRGBFormat(hnd->format)) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700802 return -EINVAL;
803 }
804
805 // linear buffer, nothing to do further
806 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
807 *rgb_data = reinterpret_cast<void *>(hnd->base);
808 return err;
809 }
810
811 unsigned int meta_size = 0;
812 uint32_t bpp = GetBppForUncompressedRGB(hnd->format);
813 switch (hnd->format) {
814 case HAL_PIXEL_FORMAT_BGR_565:
815 case HAL_PIXEL_FORMAT_RGBA_8888:
816 case HAL_PIXEL_FORMAT_RGBX_8888:
817 case HAL_PIXEL_FORMAT_RGBA_1010102:
818 case HAL_PIXEL_FORMAT_RGBX_1010102:
819 meta_size = GetRgbUBwcMetaBufferSize(hnd->width, hnd->height, bpp);
820 break;
821 default:
822 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
823 err = -EINVAL;
824 break;
825 }
826 *rgb_data = reinterpret_cast<void *>(hnd->base + meta_size);
827
828 return err;
829}
830
Naseer Ahmede36f2242017-12-01 15:33:56 -0500831void GetCustomDimensions(private_handle_t *hnd, int *stride, int *height) {
832 BufferDim_t buffer_dim;
833 int interlaced = 0;
834
835 *stride = hnd->width;
836 *height = hnd->height;
837 if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
838 *stride = buffer_dim.sliceWidth;
839 *height = buffer_dim.sliceHeight;
840 } else if (getMetaData(hnd, GET_PP_PARAM_INTERLACED, &interlaced) == 0) {
841 if (interlaced && IsUBwcFormat(hnd->format)) {
842 unsigned int alignedw = 0, alignedh = 0;
843 // Get re-aligned height for single ubwc interlaced field and
844 // multiply by 2 to get frame height.
845 BufferInfo info(hnd->width, ((hnd->height + 1) >> 1), hnd->format);
846 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
847 *stride = static_cast<int>(alignedw);
848 *height = static_cast<int>(alignedh * 2);
849 }
850 }
851}
852
853void GetColorSpaceFromMetadata(private_handle_t *hnd, int *color_space) {
854 ColorMetaData color_metadata;
855 if (getMetaData(hnd, GET_COLOR_METADATA, &color_metadata) == 0) {
856 switch (color_metadata.colorPrimaries) {
857 case ColorPrimaries_BT709_5:
858 *color_space = HAL_CSC_ITU_R_709;
859 break;
860 case ColorPrimaries_BT601_6_525:
861 case ColorPrimaries_BT601_6_625:
862 *color_space = ((color_metadata.range) ? HAL_CSC_ITU_R_601_FR : HAL_CSC_ITU_R_601);
863 break;
864 case ColorPrimaries_BT2020:
865 *color_space = (color_metadata.range) ? HAL_CSC_ITU_R_2020_FR : HAL_CSC_ITU_R_2020;
866 break;
867 default:
868 ALOGE("Unknown Color Space = %d", color_metadata.colorPrimaries);
869 break;
870 }
871 } else if (getMetaData(hnd, GET_COLOR_SPACE, color_space) != 0) {
872 *color_space = 0;
873 }
874}
875
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700876void GetAlignedWidthAndHeight(const BufferInfo &info, unsigned int *alignedw,
877 unsigned int *alignedh) {
878 int width = info.width;
879 int height = info.height;
880 int format = info.format;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500881 uint64_t usage = info.usage;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700882
883 // Currently surface padding is only computed for RGB* surfaces.
Naseer Ahmede36f2242017-12-01 15:33:56 -0500884 bool ubwc_enabled = IsUBwcEnabled(format, usage);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700885 int tile = ubwc_enabled;
886
887 if (IsUncompressedRGBFormat(format)) {
Pramodh Kumar Mukunda927729d2017-06-15 16:18:51 +0530888 if (AdrenoMemInfo::GetInstance()) {
889 AdrenoMemInfo::GetInstance()->AlignUnCompressedRGB(width, height, format, tile, alignedw,
890 alignedh);
891 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700892 return;
893 }
894
895 if (ubwc_enabled) {
896 GetYuvUBwcWidthAndHeight(width, height, format, alignedw, alignedh);
897 return;
898 }
899
900 if (IsCompressedRGBFormat(format)) {
Pramodh Kumar Mukunda927729d2017-06-15 16:18:51 +0530901 if (AdrenoMemInfo::GetInstance()) {
902 AdrenoMemInfo::GetInstance()->AlignCompressedRGB(width, height, format, alignedw, alignedh);
903 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700904 return;
905 }
906
907 int aligned_w = width;
908 int aligned_h = height;
909 unsigned int alignment = 32;
910
911 // Below should be only YUV family
912 switch (format) {
913 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
914 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
Pramodh Kumar Mukunda927729d2017-06-15 16:18:51 +0530915 if (AdrenoMemInfo::GetInstance() == nullptr) {
916 return;
917 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700918 alignment = AdrenoMemInfo::GetInstance()->GetGpuPixelAlignment();
919 aligned_w = ALIGN(width, alignment);
920 break;
921 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
922 aligned_w = ALIGN(width, alignment);
923 break;
924 case HAL_PIXEL_FORMAT_RAW16:
Anjaneya Prasad Musunurib1654142017-04-18 10:32:42 +0530925 case HAL_PIXEL_FORMAT_Y16:
926 case HAL_PIXEL_FORMAT_Y8:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700927 aligned_w = ALIGN(width, 16);
928 break;
929 case HAL_PIXEL_FORMAT_RAW12:
Naseer Ahmed1187a572017-08-14 13:16:55 -0400930 aligned_w = ALIGN(width * 12 / 8, 16);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700931 break;
932 case HAL_PIXEL_FORMAT_RAW10:
Naseer Ahmed1187a572017-08-14 13:16:55 -0400933 aligned_w = ALIGN(width * 10 / 8, 16);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700934 break;
935 case HAL_PIXEL_FORMAT_RAW8:
Naseer Ahmed1187a572017-08-14 13:16:55 -0400936 aligned_w = ALIGN(width, 16);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700937 break;
938 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
939 aligned_w = ALIGN(width, 128);
940 break;
941 case HAL_PIXEL_FORMAT_YV12:
942 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
943 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
944 case HAL_PIXEL_FORMAT_YCbCr_422_I:
945 case HAL_PIXEL_FORMAT_YCrCb_422_I:
946 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
947 aligned_w = ALIGN(width, 16);
948 break;
Mathew Joseph Karimpanala73082e2017-10-16 18:01:21 +0530949 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
950 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_P010, width) / 2);
951 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_P010, height));
952 break;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700953 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
954 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
955 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV12, width));
956 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV12, height));
957 break;
958 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
959 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV21, width));
960 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV21, height));
961 break;
962 case HAL_PIXEL_FORMAT_BLOB:
963 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
964 break;
965 case HAL_PIXEL_FORMAT_NV21_ZSL:
966 aligned_w = ALIGN(width, 64);
967 aligned_h = ALIGN(height, 64);
968 break;
Rajavenu Kyatham6e8c1822018-07-31 10:27:19 +0530969 case HAL_PIXEL_FORMAT_NV12_HEIF:
970 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV12_512, width));
971 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV12_512, height));
972 break;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700973 default:
974 break;
975 }
976
977 *alignedw = (unsigned int)aligned_w;
978 *alignedh = (unsigned int)aligned_h;
979}
980
Naseer Ahmede36f2242017-12-01 15:33:56 -0500981int GetBufferLayout(private_handle_t *hnd, uint32_t stride[4], uint32_t offset[4],
982 uint32_t *num_planes) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700983 if (!hnd || !stride || !offset || !num_planes) {
984 return -EINVAL;
985 }
986
Naseer Ahmed7aff2612017-10-05 20:39:05 -0400987 struct android_ycbcr yuvPlaneInfo[2] = {};
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700988 *num_planes = 1;
989 stride[0] = 0;
990
991 switch (hnd->format) {
992 case HAL_PIXEL_FORMAT_RGB_565:
993 case HAL_PIXEL_FORMAT_BGR_565:
994 case HAL_PIXEL_FORMAT_RGBA_5551:
995 case HAL_PIXEL_FORMAT_RGBA_4444:
996 stride[0] = static_cast<uint32_t>(hnd->width * 2);
997 break;
998 case HAL_PIXEL_FORMAT_RGB_888:
999 stride[0] = static_cast<uint32_t>(hnd->width * 3);
1000 break;
1001 case HAL_PIXEL_FORMAT_RGBA_8888:
1002 case HAL_PIXEL_FORMAT_BGRA_8888:
1003 case HAL_PIXEL_FORMAT_RGBX_8888:
1004 case HAL_PIXEL_FORMAT_BGRX_8888:
1005 case HAL_PIXEL_FORMAT_RGBA_1010102:
1006 case HAL_PIXEL_FORMAT_ARGB_2101010:
1007 case HAL_PIXEL_FORMAT_RGBX_1010102:
1008 case HAL_PIXEL_FORMAT_XRGB_2101010:
1009 case HAL_PIXEL_FORMAT_BGRA_1010102:
1010 case HAL_PIXEL_FORMAT_ABGR_2101010:
1011 case HAL_PIXEL_FORMAT_BGRX_1010102:
1012 case HAL_PIXEL_FORMAT_XBGR_2101010:
1013 stride[0] = static_cast<uint32_t>(hnd->width * 4);
1014 break;
1015 }
1016
1017 // Format is RGB
1018 if (stride[0]) {
1019 return 0;
1020 }
1021
1022 (*num_planes)++;
Naseer Ahmed7aff2612017-10-05 20:39:05 -04001023 int ret = GetYUVPlaneInfo(hnd, yuvPlaneInfo);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001024 if (ret < 0) {
1025 ALOGE("%s failed", __FUNCTION__);
1026 return ret;
1027 }
1028
Naseer Ahmed7aff2612017-10-05 20:39:05 -04001029 // We are only returning buffer layout for progressive or single field formats.
1030 struct android_ycbcr yuvInfo = yuvPlaneInfo[0];
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001031 stride[0] = static_cast<uint32_t>(yuvInfo.ystride);
1032 offset[0] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.y) - hnd->base);
1033 stride[1] = static_cast<uint32_t>(yuvInfo.cstride);
1034 switch (hnd->format) {
1035 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
1036 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
1037 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1038 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1039 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1040 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
1041 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1042 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
Mathew Joseph Karimpanala73082e2017-10-16 18:01:21 +05301043 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
Rajavenu Kyatham6e8c1822018-07-31 10:27:19 +05301044 case HAL_PIXEL_FORMAT_NV12_HEIF:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001045 offset[1] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cb) - hnd->base);
1046 break;
1047 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1048 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
1049 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
1050 offset[1] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cr) - hnd->base);
1051 break;
1052 case HAL_PIXEL_FORMAT_YV12:
1053 offset[1] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cr) - hnd->base);
1054 stride[2] = static_cast<uint32_t>(yuvInfo.cstride);
1055 offset[2] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cb) - hnd->base);
1056 (*num_planes)++;
1057 break;
Camus Wonga300db62018-09-27 16:38:50 -04001058 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
1059 *num_planes = 1;
1060 break;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001061 default:
1062 ALOGW("%s: Unsupported format", __FUNCTION__);
1063 ret = -EINVAL;
1064 }
1065
1066 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
1067 std::fill(offset, offset + 4, 0);
1068 }
1069
1070 return 0;
1071}
1072
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -07001073int GetGpuResourceSizeAndDimensions(const BufferInfo &info, unsigned int *size,
1074 unsigned int *alignedw, unsigned int *alignedh,
1075 GraphicsMetadata *graphics_metadata) {
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301076 GetAlignedWidthAndHeight(info, alignedw, alignedh);
1077 AdrenoMemInfo* adreno_mem_info = AdrenoMemInfo::GetInstance();
1078 graphics_metadata->size = adreno_mem_info->AdrenoGetMetadataBlobSize();
1079 uint64_t adreno_usage = info.usage;
1080 // If gralloc disables UBWC based on any of the checks,
1081 // we pass modified usage flag to adreno to convey this.
1082 int is_ubwc_enabled = IsUBwcEnabled(info.format, info.usage);
1083 if (!is_ubwc_enabled) {
1084 adreno_usage &= ~(GRALLOC_USAGE_PRIVATE_ALLOC_UBWC);
Naseer Ahmed2023cf32019-01-28 19:02:40 -05001085 } else {
1086 adreno_usage |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301087 }
1088
1089 // Call adreno api for populating metadata blob
Saurabh Dubeyb637bb32018-05-15 16:20:10 +05301090 // Layer count is for 2D/Cubemap arrays and depth is used for 3D slice
1091 // Using depth to pass layer_count here
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301092 int ret = adreno_mem_info->AdrenoInitMemoryLayout(graphics_metadata->data, info.width,
Saurabh Dubeyb637bb32018-05-15 16:20:10 +05301093 info.height, info.layer_count, /* depth */
1094 info.format, 1, is_ubwc_enabled,
1095 adreno_usage, 1);
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301096 if (ret != 0) {
1097 ALOGE("%s Graphics metadata init failed", __FUNCTION__);
1098 *size = 0;
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -07001099 return -EINVAL;
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301100 }
1101 // Call adreno api with the metadata blob to get buffer size
1102 *size = adreno_mem_info->AdrenoGetAlignedGpuBufferSize(graphics_metadata->data);
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -07001103 return 0;
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301104}
1105
Saurabh Dubey52eb7a62018-06-15 16:31:23 +05301106bool CanUseAdrenoForSize(int buffer_type, uint64_t usage) {
1107 if (buffer_type == BUFFER_TYPE_VIDEO || !GetAdrenoSizeAPIStatus()) {
1108 return false;
1109 }
1110
1111 if ((usage & BufferUsage::PROTECTED) && ((usage & BufferUsage::CAMERA_OUTPUT) ||
1112 (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY))) {
1113 return false;
1114 }
1115
1116 return true;
1117}
1118
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301119bool GetAdrenoSizeAPIStatus() {
1120 AdrenoMemInfo* adreno_mem_info = AdrenoMemInfo::GetInstance();
1121 if (adreno_mem_info) {
1122 return adreno_mem_info->AdrenoSizeAPIAvaliable();
1123 }
1124 return false;
1125}
1126
Saurabh Dubey48b757d2018-05-24 19:13:53 +05301127bool UseUncached(int format, uint64_t usage) {
1128 if ((usage & GRALLOC_USAGE_PRIVATE_UNCACHED) || (usage & BufferUsage::PROTECTED)) {
1129 return true;
1130 }
1131
1132 // CPU read rarely
1133 if ((usage & BufferUsage::CPU_READ_MASK) == static_cast<uint64_t>(BufferUsage::CPU_READ_RARELY)) {
1134 return true;
1135 }
1136
1137 // CPU write rarely
1138 if ((usage & BufferUsage::CPU_WRITE_MASK) ==
1139 static_cast<uint64_t>(BufferUsage::CPU_WRITE_RARELY)) {
1140 return true;
1141 }
1142
1143 if ((usage & BufferUsage::SENSOR_DIRECT_DATA) || (usage & BufferUsage::GPU_DATA_BUFFER)) {
1144 return true;
1145 }
1146
1147 if (format && IsUBwcEnabled(format, usage)) {
1148 return true;
1149 }
1150
1151 return false;
1152}
1153
1154uint64_t GetHandleFlags(int format, uint64_t usage) {
1155 uint64_t priv_flags = 0;
1156
1157 if (usage & BufferUsage::VIDEO_ENCODER) {
1158 priv_flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
1159 }
1160
1161 if (usage & BufferUsage::CAMERA_OUTPUT) {
1162 priv_flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
1163 }
1164
1165 if (usage & BufferUsage::CAMERA_INPUT) {
1166 priv_flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
1167 }
1168
1169 if (usage & BufferUsage::COMPOSER_OVERLAY) {
1170 priv_flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER;
1171 }
1172
1173 if (usage & BufferUsage::GPU_TEXTURE) {
1174 priv_flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
1175 }
1176
1177 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
1178 priv_flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
1179 }
1180
1181 if (IsUBwcEnabled(format, usage)) {
1182 if (IsUBwcPISupported(format, usage)) {
1183 priv_flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED_PI;
1184 } else {
1185 priv_flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
1186 }
1187 }
1188
1189 if (usage & (BufferUsage::CPU_READ_MASK | BufferUsage::CPU_WRITE_MASK)) {
1190 priv_flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED;
1191 }
1192
1193 if ((usage & (BufferUsage::VIDEO_ENCODER | BufferUsage::VIDEO_DECODER |
1194 BufferUsage::CAMERA_OUTPUT | BufferUsage::GPU_RENDER_TARGET))) {
1195 priv_flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER;
1196 }
1197
1198 if (!UseUncached(format, usage)) {
1199 priv_flags |= private_handle_t::PRIV_FLAGS_CACHED;
1200 }
1201
1202 return priv_flags;
1203}
1204
1205int GetImplDefinedFormat(uint64_t usage, int format) {
1206 int gr_format = format;
1207
1208 // If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
1209 // the usage bits, gralloc assigns a format.
1210 if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED ||
1211 format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
Mathew Joseph Karimpanal71517182018-08-14 16:40:34 +05301212 if ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC || usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC_PI)
1213 && format != HAL_PIXEL_FORMAT_YCbCr_420_888) {
Saurabh Dubey81aef932018-06-27 09:41:31 +05301214 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
Saurabh Dubey48b757d2018-05-24 19:13:53 +05301215 } else if (usage & BufferUsage::VIDEO_ENCODER) {
1216 if (usage & GRALLOC_USAGE_PRIVATE_VIDEO_NV21_ENCODER) {
1217 gr_format = HAL_PIXEL_FORMAT_NV21_ENCODEABLE; // NV21
Manikanta Kanamarlapudidfa32e82019-03-26 14:06:47 +05301218 } else if (usage & GRALLOC_USAGE_PRIVATE_HEIF) {
1219 gr_format = HAL_PIXEL_FORMAT_NV12_HEIF;
Mathew Joseph Karimpanal71517182018-08-14 16:40:34 +05301220 } else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
1221 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS;
Saurabh Dubey48b757d2018-05-24 19:13:53 +05301222 } else {
1223 gr_format = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; // NV12
1224 }
1225 } else if (usage & BufferUsage::CAMERA_INPUT) {
1226 if (usage & BufferUsage::CAMERA_OUTPUT) {
1227 // Assumed ZSL if both producer and consumer camera flags set
1228 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL; // NV21
1229 } else {
1230 gr_format = HAL_PIXEL_FORMAT_YCrCb_420_SP; // NV21
1231 }
1232 } else if (usage & BufferUsage::CAMERA_OUTPUT) {
1233 if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
1234 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL; // NV21
1235 } else {
1236 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS; // NV12 preview
1237 }
1238 } else if (usage & BufferUsage::COMPOSER_OVERLAY) {
1239 // XXX: If we still haven't set a format, default to RGBA8888
1240 gr_format = HAL_PIXEL_FORMAT_RGBA_8888;
1241 } else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
1242 // If no other usage flags are detected, default the
1243 // flexible YUV format to NV21_ZSL
1244 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL;
1245 }
1246 }
1247
1248 return gr_format;
1249}
1250
1251int GetCustomFormatFlags(int format, uint64_t usage,
1252 int *custom_format, uint64_t *priv_flags) {
1253 *custom_format = GetImplDefinedFormat(usage, format);
1254 *priv_flags = GetHandleFlags(*custom_format, usage);
1255
Saurabh Dubey81aef932018-06-27 09:41:31 +05301256 if (usage & GRALLOC_USAGE_PROTECTED) {
1257 *priv_flags |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
1258 }
1259
1260 *priv_flags |= private_handle_t::PRIV_FLAGS_USES_ION;
1261
Saurabh Dubey48b757d2018-05-24 19:13:53 +05301262 return 0;
1263}
1264
Saurabh Dubeyefd7f9e2018-06-15 18:00:02 +05301265int GetBufferType(int inputFormat) {
1266 return IsYuvFormat(inputFormat) ? BUFFER_TYPE_VIDEO : BUFFER_TYPE_UI;
1267}
1268
Ashish Kumarb9392ff2019-03-27 18:30:39 +05301269int GetYUVPlaneInfo(const BufferInfo &info, int32_t format, int32_t width, int32_t height,
1270 int32_t flags, int *plane_count, PlaneLayoutInfo *plane_info) {
1271 int err = 0;
1272 unsigned int y_stride, c_stride, y_height, c_height, y_size, c_size;
1273 uint64_t yOffset, cOffset, crOffset, cbOffset;
1274 int h_subsampling = 0, v_subsampling = 0;
1275 switch (format) {
1276 // Semiplanar
1277 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
1278 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
1279 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1280 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1281 case HAL_PIXEL_FORMAT_NV12_HEIF: // Same as YCbCr_420_SP_VENUS
1282 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1283 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
1284 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
1285 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
1286 case HAL_PIXEL_FORMAT_NV21_ZSL:
1287 case HAL_PIXEL_FORMAT_RAW16:
1288 case HAL_PIXEL_FORMAT_Y16:
1289 case HAL_PIXEL_FORMAT_RAW10:
1290 case HAL_PIXEL_FORMAT_RAW8:
1291 case HAL_PIXEL_FORMAT_Y8:
1292 *plane_count = 2;
1293 GetYuvSPPlaneInfo(info, format, width, height, 1, plane_info);
1294 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1295 plane_info[0].h_subsampling = 0;
1296 plane_info[0].v_subsampling = 0;
1297 plane_info[1].h_subsampling = h_subsampling;
1298 plane_info[1].v_subsampling = v_subsampling;
1299 break;
1300
1301 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1302 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1303 if (flags & LAYOUT_INTERLACED_FLAG) {
1304 *plane_count = 8;
1305 GetYuvUbwcInterlacedSPPlaneInfo(width, height, plane_info);
1306 plane_info[0].step = plane_info[4].step = 1;
1307 plane_info[1].step = plane_info[5].step = 2;
1308 plane_info[0].h_subsampling = plane_info[4].h_subsampling = 0;
1309 plane_info[0].v_subsampling = plane_info[4].v_subsampling = 0;
1310 plane_info[1].h_subsampling = plane_info[5].h_subsampling = h_subsampling;
1311 plane_info[1].v_subsampling = plane_info[5].v_subsampling = v_subsampling;
1312 plane_info[2].h_subsampling = plane_info[3].h_subsampling = 0;
1313 plane_info[2].v_subsampling = plane_info[3].v_subsampling = 0;
1314 plane_info[2].step = plane_info[3].step = 0;
1315 plane_info[6].h_subsampling = plane_info[7].h_subsampling = 0;
1316 plane_info[6].v_subsampling = plane_info[7].v_subsampling = 0;
1317 plane_info[6].step = plane_info[7].step = 0;
1318 } else {
1319 *plane_count = 4;
1320 GetYuvUbwcSPPlaneInfo(width, height, COLOR_FMT_NV12_UBWC, plane_info);
1321 plane_info[0].h_subsampling = 0;
1322 plane_info[0].v_subsampling = 0;
1323 plane_info[0].step = 1;
1324 plane_info[1].h_subsampling = h_subsampling;
1325 plane_info[1].v_subsampling = v_subsampling;
1326 plane_info[1].step = 2;
1327 plane_info[2].h_subsampling = plane_info[3].h_subsampling = 0;
1328 plane_info[2].v_subsampling = plane_info[3].v_subsampling = 0;
1329 plane_info[2].step = plane_info[3].step = 0;
1330 }
1331 break;
1332
1333 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
1334 *plane_count = 2;
1335 GetYuvSPPlaneInfo(info, format, width, height, 2, plane_info);
1336 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1337 plane_info[0].h_subsampling = 0;
1338 plane_info[0].v_subsampling = 0;
1339 plane_info[1].h_subsampling = h_subsampling;
1340 plane_info[1].v_subsampling = v_subsampling;
1341 break;
1342
1343 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1344 *plane_count = 4;
1345 GetYuvUbwcSPPlaneInfo(width, height, COLOR_FMT_NV12_BPP10_UBWC, plane_info);
1346 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1347 plane_info[0].h_subsampling = 0;
1348 plane_info[0].v_subsampling = 0;
1349 plane_info[1].step = 1;
1350 plane_info[1].h_subsampling = h_subsampling;
1351 plane_info[1].v_subsampling = v_subsampling;
1352 plane_info[1].step = 3;
1353 plane_info[2].h_subsampling = plane_info[3].h_subsampling = 0;
1354 plane_info[2].v_subsampling = plane_info[3].v_subsampling = 0;
1355 plane_info[2].step = plane_info[3].step = 0;
1356 break;
1357
1358 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
1359 *plane_count = 4;
1360 GetYuvUbwcSPPlaneInfo(width, height, COLOR_FMT_P010_UBWC, plane_info);
1361 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1362 plane_info[0].h_subsampling = 0;
1363 plane_info[0].v_subsampling = 0;
1364 plane_info[1].step = 1;
1365 plane_info[1].h_subsampling = h_subsampling;
1366 plane_info[1].v_subsampling = v_subsampling;
1367 plane_info[1].step = 4;
1368 plane_info[2].h_subsampling = plane_info[3].h_subsampling = 0;
1369 plane_info[2].v_subsampling = plane_info[3].v_subsampling = 0;
1370 plane_info[2].step = plane_info[3].step = 0;
1371 break;
1372
1373 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
1374 *plane_count = 2;
1375 y_stride = VENUS_Y_STRIDE(COLOR_FMT_P010, width);
1376 c_stride = VENUS_UV_STRIDE(COLOR_FMT_P010, width);
1377 y_height = VENUS_Y_SCANLINES(COLOR_FMT_P010, height);
1378 y_size = y_stride * y_height;
1379 yOffset = 0;
1380 cOffset = y_size;
1381 c_height = VENUS_UV_SCANLINES(COLOR_FMT_P010, INT(height));
1382 c_size = c_stride * c_height;
1383 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1384
1385 plane_info[0].component = (PlaneComponent)PLANE_COMPONENT_Y;
1386 plane_info[0].offset = (uint32_t)yOffset;
1387 plane_info[0].stride = static_cast<int32_t>(UINT(width));
1388 plane_info[0].stride_bytes = static_cast<int32_t>(y_stride);
1389 plane_info[0].scanlines = static_cast<int32_t>(y_height);
1390 plane_info[0].size = static_cast<uint32_t>(y_size);
1391 plane_info[0].step = 1;
1392 plane_info[0].h_subsampling = 0;
1393 plane_info[0].v_subsampling = 0;
1394
1395 plane_info[1].component = (PlaneComponent)(PLANE_COMPONENT_Cb | PLANE_COMPONENT_Cr);
1396 plane_info[1].offset = (uint32_t)cOffset;
1397 plane_info[1].stride = static_cast<int32_t>(UINT(width));
1398 plane_info[1].stride_bytes = static_cast<int32_t>(c_stride);
1399 plane_info[1].scanlines = static_cast<int32_t>(c_height);
1400 plane_info[1].size = static_cast<uint32_t>(c_size);
1401 plane_info[1].step = 4;
1402 plane_info[1].h_subsampling = h_subsampling;
1403 plane_info[1].v_subsampling = v_subsampling;
1404 break;
1405
1406 // Planar
1407 case HAL_PIXEL_FORMAT_YV12:
1408 if ((info.width & 1) || (info.height & 1)) {
1409 ALOGE("w or h is odd for the YV12 format");
1410 err = -EINVAL;
1411 return err;
1412 }
1413 *plane_count = 3;
1414 y_stride = width;
1415 c_stride = ALIGN(width / 2, 16);
1416 y_height = UINT(height);
1417 y_size = (y_stride * y_height);
1418 height = height >> 1;
1419 c_height = UINT(height);
1420 c_size = (c_stride * c_height);
1421 yOffset = 0;
1422 crOffset = y_size;
1423 cbOffset = (y_size + c_size);
1424 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1425
1426 plane_info[0].component = (PlaneComponent)PLANE_COMPONENT_Y;
1427 plane_info[0].offset = (uint32_t)yOffset;
1428 plane_info[0].stride = static_cast<int32_t>(UINT(width));
1429 plane_info[0].stride_bytes = static_cast<int32_t>(y_stride);
1430 plane_info[0].scanlines = static_cast<int32_t>(y_height);
1431 plane_info[0].size = static_cast<uint32_t>(y_size);
1432 plane_info[0].step = 1;
1433 plane_info[0].h_subsampling = 0;
1434 plane_info[0].v_subsampling = 0;
1435
1436 plane_info[1].component = (PlaneComponent)PLANE_COMPONENT_Cb;
1437 plane_info[1].offset = (uint32_t)cbOffset;
1438 plane_info[2].component = (PlaneComponent)PLANE_COMPONENT_Cr;
1439 plane_info[2].offset = (uint32_t)crOffset;
1440 for (int i = 1; i < 3; i++) {
1441 plane_info[i].stride = static_cast<int32_t>(UINT(width));
1442 plane_info[i].stride_bytes = static_cast<int32_t>(c_stride);
1443 plane_info[i].scanlines = static_cast<int32_t>(c_height);
1444 plane_info[i].size = static_cast<uint32_t>(c_size);
1445 plane_info[i].step = 1;
1446 plane_info[i].h_subsampling = h_subsampling;
1447 plane_info[i].v_subsampling = v_subsampling;
1448 }
1449 break;
1450 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
1451 if (info.width & 1) {
1452 ALOGE("width is odd for the YUV422_SP format");
1453 err = -EINVAL;
1454 return err;
1455 }
1456 *plane_count = 1;
1457 y_stride = width * 2;
1458 y_height = UINT(height);
1459 y_size = y_stride * y_height;
1460 yOffset = 0;
1461 plane_info[0].component = (PlaneComponent)PLANE_COMPONENT_Y;
1462 plane_info[0].offset = (uint32_t)yOffset;
1463 plane_info[0].stride = static_cast<int32_t>(UINT(width));
1464 plane_info[0].stride_bytes = static_cast<int32_t>(y_stride);
1465 plane_info[0].scanlines = static_cast<int32_t>(y_height);
1466 plane_info[0].size = static_cast<uint32_t>(y_size);
1467 plane_info[0].step = 1;
1468 plane_info[0].h_subsampling = 0;
1469 plane_info[0].v_subsampling = 0;
1470 break;
1471
1472 // Unsupported formats
1473 case HAL_PIXEL_FORMAT_YCbCr_422_I:
1474 case HAL_PIXEL_FORMAT_YCrCb_422_I:
1475 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
1476 default:
1477 *plane_count = 0;
1478 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, format);
1479 err = -EINVAL;
1480 }
1481 return err;
1482}
1483
1484void GetYuvSubSamplingFactor(int32_t format, int *h_subsampling, int *v_subsampling) {
1485 switch (format) {
1486 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
1487 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
1488 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1489 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
1490 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
1491 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1492 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1493 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
1494 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
1495 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1496 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: // Same as YCbCr_420_SP_VENUS
1497 case HAL_PIXEL_FORMAT_NV21_ZSL:
1498 case HAL_PIXEL_FORMAT_YV12:
1499 *h_subsampling = 1;
1500 *v_subsampling = 1;
1501 break;
1502 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
1503 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
1504 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
1505 *h_subsampling = 1;
1506 *v_subsampling = 0;
1507 break;
1508 case HAL_PIXEL_FORMAT_RAW16:
1509 case HAL_PIXEL_FORMAT_Y16:
1510 case HAL_PIXEL_FORMAT_RAW12:
1511 case HAL_PIXEL_FORMAT_RAW10:
1512 case HAL_PIXEL_FORMAT_Y8:
1513 case HAL_PIXEL_FORMAT_BLOB:
1514 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
1515 case HAL_PIXEL_FORMAT_NV12_HEIF:
1516 default:
1517 *h_subsampling = 0;
1518 *v_subsampling = 0;
1519 break;
1520 }
1521}
1522
1523void CopyPlaneLayoutInfotoAndroidYcbcr(uint64_t base, int plane_count, PlaneLayoutInfo *plane_info,
1524 struct android_ycbcr *ycbcr) {
1525 ycbcr->y = reinterpret_cast<void *>(base + plane_info[0].offset);
1526 ycbcr->ystride = plane_info[0].stride_bytes;
1527 if (plane_count == 1) {
1528 ycbcr->cb = NULL;
1529 ycbcr->cr = NULL;
1530 ycbcr->cstride = 0;
1531 ycbcr->chroma_step = 0;
1532 } else if (plane_count == 2 || plane_count == 4 || plane_count == 8) {
1533 /* For YUV semiplanar :-
1534 * - In progressive & linear case plane count is 2 and plane_info[0] will
1535 * contain info about Y plane and plane_info[1] will contain info about UV plane.
1536 * - In progressive & compressed case plane count is 4 then plane_info[0] will
1537 * contain info about Y plane and plane_info[1] will contain info about UV plane.
1538 * Remaining two plane (plane_info[2] & plane_info[3]) contain info about the
1539 * Y_Meta_Plane and UV_Meta_Plane.
1540 * - In interlaced & compressed case plane count is 8 then plane_info[0], plane_info[1],
1541 * plane_info[4] & plane_info[5] will contain info about Y_plane, UV_plane, Y_plane
1542 * & UV_plane. Remaining plane will contain info about the meta planes. As in this case
1543 * this API is called twice through GetYUVPlaneInfo() with address of plane_info[0] &
1544 * plane_info[4], so this will calculate the information accordingly and will fill the
1545 * ycbcr structure with interlaced plane info only.
1546 */
1547 ycbcr->cb = reinterpret_cast<void *>(base + plane_info[1].offset);
1548 ycbcr->cr = reinterpret_cast<void *>(base + plane_info[1].offset + 1);
1549 ycbcr->cstride = plane_info[1].stride_bytes;
1550 ycbcr->chroma_step = plane_info[1].step;
1551 } else if (plane_count == 3) {
1552 /* For YUV planar :-
1553 * Plane size is 3 and plane_info[0], plane_info[1], plane_info[2] will
1554 * contain info about y_plane, cb_plane and cr_plane accordingly.
1555 */
1556 ycbcr->cb = reinterpret_cast<void *>(base + plane_info[1].offset);
1557 ycbcr->cr = reinterpret_cast<void *>(base + plane_info[2].offset);
1558 ycbcr->cstride = plane_info[1].stride_bytes;
1559 ycbcr->chroma_step = plane_info[1].step;
1560 }
1561}
1562
Naseer Ahmede36f2242017-12-01 15:33:56 -05001563} // namespace gralloc