blob: 0cd2276f3fccf898bf7d14377e278efec56c44b9 [file] [log] [blame]
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05301/*
Ramakant Singh13596ae2020-01-06 20:12:34 +05302 * Copyright (c) 2011-2020, 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>
Tharaga Balachandran74ab1112020-01-08 17:17:56 -050031
32#include <drm/drm_fourcc.h>
33
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070034#include <algorithm>
35
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070036#include "gr_adreno_info.h"
Naseer Ahmede36f2242017-12-01 15:33:56 -050037#include "gr_utils.h"
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070038
39#define ASTC_BLOCK_SIZE 16
40
41#ifndef COLOR_FMT_P010_UBWC
42#define COLOR_FMT_P010_UBWC 9
43#endif
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053044
Naseer Ahmede36f2242017-12-01 15:33:56 -050045namespace gralloc {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053046
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +053047bool IsYuvFormat(int format) {
48 switch (format) {
Saurabh Shahdbb572e2018-01-04 15:38:01 -080049 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
50 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
51 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmede36f2242017-12-01 15:33:56 -050052 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: // Same as YCbCr_420_SP_VENUS
Ramakant Singh13596ae2020-01-06 20:12:34 +053053 case HAL_PIXEL_FORMAT_NV21_ENCODEABLE:
Saurabh Shahdbb572e2018-01-04 15:38:01 -080054 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
55 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
56 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
57 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
58 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
59 case HAL_PIXEL_FORMAT_NV21_ZSL:
60 case HAL_PIXEL_FORMAT_RAW16:
61 case HAL_PIXEL_FORMAT_Y16:
62 case HAL_PIXEL_FORMAT_RAW12:
63 case HAL_PIXEL_FORMAT_RAW10:
64 case HAL_PIXEL_FORMAT_YV12:
65 case HAL_PIXEL_FORMAT_Y8:
66 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
67 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
68 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
69 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +053070 // Below formats used by camera and VR
71 case HAL_PIXEL_FORMAT_BLOB:
72 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
Rajavenu Kyatham6e8c1822018-07-31 10:27:19 +053073 case HAL_PIXEL_FORMAT_NV12_HEIF:
Camus Wonga300db62018-09-27 16:38:50 -040074 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
Saurabh Shahdbb572e2018-01-04 15:38:01 -080075 return true;
76 default:
77 return false;
78 }
79}
80
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053081bool IsUncompressedRGBFormat(int format) {
82 switch (format) {
83 case HAL_PIXEL_FORMAT_RGBA_8888:
84 case HAL_PIXEL_FORMAT_RGBX_8888:
85 case HAL_PIXEL_FORMAT_RGB_888:
86 case HAL_PIXEL_FORMAT_RGB_565:
87 case HAL_PIXEL_FORMAT_BGR_565:
88 case HAL_PIXEL_FORMAT_BGRA_8888:
89 case HAL_PIXEL_FORMAT_RGBA_5551:
90 case HAL_PIXEL_FORMAT_RGBA_4444:
91 case HAL_PIXEL_FORMAT_R_8:
92 case HAL_PIXEL_FORMAT_RG_88:
93 case HAL_PIXEL_FORMAT_BGRX_8888:
94 case HAL_PIXEL_FORMAT_RGBA_1010102:
95 case HAL_PIXEL_FORMAT_ARGB_2101010:
96 case HAL_PIXEL_FORMAT_RGBX_1010102:
97 case HAL_PIXEL_FORMAT_XRGB_2101010:
98 case HAL_PIXEL_FORMAT_BGRA_1010102:
99 case HAL_PIXEL_FORMAT_ABGR_2101010:
100 case HAL_PIXEL_FORMAT_BGRX_1010102:
101 case HAL_PIXEL_FORMAT_XBGR_2101010:
Naseer Ahmed1c473d82017-02-27 13:39:37 -0500102 case HAL_PIXEL_FORMAT_RGBA_FP16:
Camus Wong5c5e6fb2017-03-21 17:02:48 -0400103 case HAL_PIXEL_FORMAT_BGR_888:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530104 return true;
105 default:
106 break;
107 }
108
109 return false;
110}
111
112bool IsCompressedRGBFormat(int format) {
113 switch (format) {
114 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
115 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
116 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
117 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
118 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
119 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
120 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
121 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
122 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
123 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
124 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
125 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
126 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
127 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
128 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
129 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
130 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
131 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
132 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
133 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
134 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
135 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
136 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
137 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
138 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
139 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
140 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
141 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
142 return true;
143 default:
144 break;
145 }
146
147 return false;
148}
149
150uint32_t GetBppForUncompressedRGB(int format) {
151 uint32_t bpp = 0;
152 switch (format) {
Naseer Ahmed1c473d82017-02-27 13:39:37 -0500153 case HAL_PIXEL_FORMAT_RGBA_FP16:
154 bpp = 8;
155 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530156 case HAL_PIXEL_FORMAT_RGBA_8888:
157 case HAL_PIXEL_FORMAT_RGBX_8888:
158 case HAL_PIXEL_FORMAT_BGRA_8888:
159 case HAL_PIXEL_FORMAT_BGRX_8888:
Arun Kumar K.Rb97ca302017-04-06 15:59:33 -0700160 case HAL_PIXEL_FORMAT_RGBA_1010102:
161 case HAL_PIXEL_FORMAT_ARGB_2101010:
162 case HAL_PIXEL_FORMAT_RGBX_1010102:
163 case HAL_PIXEL_FORMAT_XRGB_2101010:
164 case HAL_PIXEL_FORMAT_BGRA_1010102:
165 case HAL_PIXEL_FORMAT_ABGR_2101010:
166 case HAL_PIXEL_FORMAT_BGRX_1010102:
167 case HAL_PIXEL_FORMAT_XBGR_2101010:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530168 bpp = 4;
169 break;
170 case HAL_PIXEL_FORMAT_RGB_888:
Camus Wong5c5e6fb2017-03-21 17:02:48 -0400171 case HAL_PIXEL_FORMAT_BGR_888:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530172 bpp = 3;
173 break;
174 case HAL_PIXEL_FORMAT_RGB_565:
175 case HAL_PIXEL_FORMAT_BGR_565:
176 case HAL_PIXEL_FORMAT_RGBA_5551:
177 case HAL_PIXEL_FORMAT_RGBA_4444:
178 bpp = 2;
179 break;
180 default:
Arun Kumar K.Rb97ca302017-04-06 15:59:33 -0700181 ALOGE("Error : %s New format request = 0x%x", __FUNCTION__, format);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530182 break;
183 }
184
185 return bpp;
186}
187
Naseer Ahmede36f2242017-12-01 15:33:56 -0500188bool CpuCanAccess(uint64_t usage) {
189 return CpuCanRead(usage) || CpuCanWrite(usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530190}
191
Naseer Ahmede36f2242017-12-01 15:33:56 -0500192bool CpuCanRead(uint64_t usage) {
193 if (usage & BufferUsage::CPU_READ_MASK) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530194 return true;
195 }
196
197 return false;
198}
199
Naseer Ahmede36f2242017-12-01 15:33:56 -0500200bool CpuCanWrite(uint64_t usage) {
201 if (usage & BufferUsage::CPU_WRITE_MASK) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530202 // Application intends to use CPU for rendering
203 return true;
204 }
205
206 return false;
207}
208
Naseer Ahmedda214ec2018-03-12 20:15:07 -0400209uint32_t GetDataAlignment(int format, uint64_t usage) {
210 uint32_t align = UINT(getpagesize());
211 if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) {
212 align = SIZE_8K;
213 }
214
215 if (usage & BufferUsage::PROTECTED) {
216 if ((usage & BufferUsage::CAMERA_OUTPUT) || (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY)) {
217 // The alignment here reflects qsee mmu V7L/V8L requirement
218 align = SZ_2M;
219 } else {
220 align = SECURE_ALIGN;
221 }
222 }
223
224 return align;
225}
226
Saurabh Dubey937c4e52018-06-26 11:31:18 +0530227bool IsGPUFlagSupported(uint64_t usage) {
228 bool ret = true;
229 if ((usage & BufferUsage::GPU_MIPMAP_COMPLETE)) {
230 ALOGE("GPU_MIPMAP_COMPLETE not supported");
231 ret = false;
232 }
233
234 if ((usage & BufferUsage::GPU_CUBE_MAP)) {
235 ALOGE("GPU_CUBE_MAP not supported");
236 ret = false;
237 }
238
239 return ret;
240}
241
Tharaga Balachandranab150ab2019-09-26 19:17:58 -0400242int GetBpp(int format) {
243 if (IsUncompressedRGBFormat(format)) {
244 return GetBppForUncompressedRGB(format);
245 }
246 switch (format) {
247 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
248 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
249 case HAL_PIXEL_FORMAT_RAW8:
250 case HAL_PIXEL_FORMAT_Y8:
251 return 1;
252 case HAL_PIXEL_FORMAT_RAW16:
253 case HAL_PIXEL_FORMAT_Y16:
254 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
255 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
256 case HAL_PIXEL_FORMAT_YCbCr_422_I:
257 case HAL_PIXEL_FORMAT_YCrCb_422_I:
258 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
259 return 2;
260 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
261 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
262 return 3;
263 default:
264 return -1;
265 }
266}
267
Naseer Ahmedda214ec2018-03-12 20:15:07 -0400268// Returns the final buffer size meant to be allocated with ion
Naseer Ahmede36f2242017-12-01 15:33:56 -0500269unsigned int GetSize(const BufferInfo &info, unsigned int alignedw, unsigned int alignedh) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700270 unsigned int size = 0;
271 int format = info.format;
272 int width = info.width;
273 int height = info.height;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500274 uint64_t usage = info.usage;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700275
Saurabh Dubey937c4e52018-06-26 11:31:18 +0530276 if (!IsGPUFlagSupported(usage)) {
277 ALOGE("Unsupported GPU usage flags present 0x%" PRIx64, usage);
278 return 0;
279 }
280
Naseer Ahmede36f2242017-12-01 15:33:56 -0500281 if (IsUBwcEnabled(format, usage)) {
Naseer Ahmedda214ec2018-03-12 20:15:07 -0400282 size = GetUBwcSize(width, height, format, alignedw, alignedh);
283 } else if (IsUncompressedRGBFormat(format)) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700284 uint32_t bpp = GetBppForUncompressedRGB(format);
285 size = alignedw * alignedh * bpp;
Naseer Ahmedda214ec2018-03-12 20:15:07 -0400286 } else if (IsCompressedRGBFormat(format)) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700287 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
Naseer Ahmedda214ec2018-03-12 20:15:07 -0400288 } else {
289 // Below switch should be for only YUV/custom formats
290 switch (format) {
291 case HAL_PIXEL_FORMAT_RAW16:
292 case HAL_PIXEL_FORMAT_Y16:size = alignedw * alignedh * 2;
293 break;
294 case HAL_PIXEL_FORMAT_RAW10:
295 case HAL_PIXEL_FORMAT_RAW12:size = ALIGN(alignedw * alignedh, SIZE_4K);
296 break;
297 case HAL_PIXEL_FORMAT_RAW8:
298 case HAL_PIXEL_FORMAT_Y8:size = alignedw * alignedh * 1;
299 break;
300 // adreno formats
301 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
302 size = ALIGN(alignedw * alignedh, SIZE_4K);
303 size += (unsigned int) ALIGN(2 * ALIGN(width / 2, 32) * ALIGN(height / 2, 32), SIZE_4K);
304 break;
305 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
306 // The chroma plane is subsampled,
307 // but the pitch in bytes is unchanged
308 // The GPU needs 4K alignment, but the video decoder needs 8K
309 size = ALIGN(alignedw * alignedh, SIZE_8K);
310 size += ALIGN(alignedw * (unsigned int) ALIGN(height / 2, 32), SIZE_8K);
311 break;
312 case HAL_PIXEL_FORMAT_YV12:
313 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width & 1) || (height & 1))) {
314 ALOGE("w or h is odd for the YV12 format");
315 return 0;
316 }
317 size = alignedw * alignedh + (ALIGN(alignedw / 2, 16) * (alignedh / 2)) * 2;
318 size = ALIGN(size, (unsigned int) SIZE_4K);
319 break;
320 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
321 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
322 size = ALIGN((alignedw * alignedh) + (alignedw * alignedh) / 2 + 1, SIZE_4K);
323 break;
324 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
325 size = ALIGN((alignedw * alignedh * 2) + (alignedw * alignedh) + 1, SIZE_4K);
326 break;
327 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
328 size = VENUS_BUFFER_SIZE(COLOR_FMT_P010,
329 width,
330 height);
331 break;
332 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
333 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
334 case HAL_PIXEL_FORMAT_YCbCr_422_I:
335 case HAL_PIXEL_FORMAT_YCrCb_422_I:
336 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
337 if (width & 1) {
338 ALOGE("width is odd for the YUV422_SP format");
339 return 0;
340 }
341 size = ALIGN(alignedw * alignedh * 2, SIZE_4K);
342 break;
343 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
344 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
345 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
346 break;
347 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
348 case HAL_PIXEL_FORMAT_NV21_ENCODEABLE:
349 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
350 break;
351 case HAL_PIXEL_FORMAT_BLOB:
352 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
353 if (height != 1) {
354 ALOGE("%s: Buffers with HAL_PIXEL_FORMAT_BLOB must have height 1 ", __FUNCTION__);
355 return 0;
356 }
357 size = (unsigned int) width;
358 break;
359 case HAL_PIXEL_FORMAT_NV21_ZSL:
360 size = ALIGN((alignedw * alignedh) + (alignedw * alignedh) / 2,
361 SIZE_4K);
362 break;
Rajavenu Kyatham6e8c1822018-07-31 10:27:19 +0530363 case HAL_PIXEL_FORMAT_NV12_HEIF:
364 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_512, width, height);
365 break;
Naseer Ahmedda214ec2018-03-12 20:15:07 -0400366 default:ALOGE("%s: Unrecognized pixel format: 0x%x", __FUNCTION__, format);
367 return 0;
368 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700369 }
Naseer Ahmedda214ec2018-03-12 20:15:07 -0400370 auto align = GetDataAlignment(format, usage);
371 size = ALIGN(size, align) * info.layer_count;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700372 return size;
373}
374
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -0700375int GetBufferSizeAndDimensions(const BufferInfo &info, unsigned int *size, unsigned int *alignedw,
376 unsigned int *alignedh) {
Saurabh Dubeyefd7f9e2018-06-15 18:00:02 +0530377 GraphicsMetadata graphics_metadata = {};
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -0700378 return GetBufferSizeAndDimensions(info, size, alignedw, alignedh, &graphics_metadata);
Saurabh Dubeyefd7f9e2018-06-15 18:00:02 +0530379}
380
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -0700381int GetBufferSizeAndDimensions(const BufferInfo &info, unsigned int *size, unsigned int *alignedw,
382 unsigned int *alignedh, GraphicsMetadata *graphics_metadata) {
Saurabh Dubeyefd7f9e2018-06-15 18:00:02 +0530383 int buffer_type = GetBufferType(info.format);
384 if (CanUseAdrenoForSize(buffer_type, info.usage)) {
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -0700385 return GetGpuResourceSizeAndDimensions(info, size, alignedw, alignedh, graphics_metadata);
Saurabh Dubeyefd7f9e2018-06-15 18:00:02 +0530386 } else {
387 GetAlignedWidthAndHeight(info, alignedw, alignedh);
388 *size = GetSize(info, *alignedw, *alignedh);
389 }
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -0700390 return 0;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700391}
392
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530393void GetYuvUbwcSPPlaneInfo(uint32_t width, uint32_t height, int color_format,
394 PlaneLayoutInfo *plane_info) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700395 // UBWC buffer has these 4 planes in the following sequence:
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530396 // Y_Plane, UV_Plane, Y_Meta_Plane, UV_Meta_Plane
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700397 unsigned int y_meta_stride, y_meta_height, y_meta_size;
398 unsigned int y_stride, y_height, y_size;
399 unsigned int c_meta_stride, c_meta_height, c_meta_size;
400 unsigned int alignment = 4096;
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530401 unsigned int c_stride, c_height, c_size;
402 uint64_t yOffset, cOffset, yMetaOffset, cMetaOffset;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700403
404 y_meta_stride = VENUS_Y_META_STRIDE(color_format, INT(width));
405 y_meta_height = VENUS_Y_META_SCANLINES(color_format, INT(height));
406 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
407
408 y_stride = VENUS_Y_STRIDE(color_format, INT(width));
409 y_height = VENUS_Y_SCANLINES(color_format, INT(height));
410 y_size = ALIGN((y_stride * y_height), alignment);
411
412 c_meta_stride = VENUS_UV_META_STRIDE(color_format, INT(width));
413 c_meta_height = VENUS_UV_META_SCANLINES(color_format, INT(height));
414 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
415
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530416 c_stride = VENUS_UV_STRIDE(color_format, INT(width));
417 c_height = VENUS_UV_SCANLINES(color_format, INT(height));
418 c_size = ALIGN((c_stride * c_height), alignment);
419
420 yMetaOffset = 0;
421 yOffset = y_meta_size;
422 cMetaOffset = y_meta_size + y_size;
423 cOffset = y_meta_size + y_size + c_meta_size;
424
425 plane_info[0].component = (PlaneComponent)PLANE_COMPONENT_Y;
426 plane_info[0].offset = (uint32_t)yOffset;
427 plane_info[0].stride = static_cast<int32_t>(UINT(width));
428 plane_info[0].stride_bytes = static_cast<int32_t>(y_stride);
429 plane_info[0].scanlines = static_cast<int32_t>(y_height);
430 plane_info[0].size = static_cast<uint32_t>(y_size);
431
432 plane_info[1].component = (PlaneComponent)(PLANE_COMPONENT_Cb | PLANE_COMPONENT_Cr);
433 plane_info[1].offset = (uint32_t)cOffset;
434 plane_info[1].stride = static_cast<int32_t>(UINT(width));
435 plane_info[1].stride_bytes = static_cast<int32_t>(c_stride);
436 plane_info[1].scanlines = static_cast<int32_t>(c_height);
437 plane_info[1].size = static_cast<uint32_t>(c_size);
438
439 plane_info[2].component = (PlaneComponent)(PLANE_COMPONENT_META | PLANE_COMPONENT_Y);
440 plane_info[2].offset = (uint32_t)yMetaOffset;
441 plane_info[2].stride = static_cast<int32_t>(UINT(width));
442 plane_info[2].stride_bytes = static_cast<int32_t>(y_meta_stride);
443 plane_info[2].scanlines = static_cast<int32_t>(y_meta_height);
444 plane_info[2].size = static_cast<uint32_t>(y_meta_size);
445
446 plane_info[3].component =
447 (PlaneComponent)(PLANE_COMPONENT_META | PLANE_COMPONENT_Cb | PLANE_COMPONENT_Cr);
448 plane_info[3].offset = (uint32_t)cMetaOffset;
449 plane_info[3].stride = static_cast<int32_t>(UINT(width));
450 plane_info[3].stride_bytes = static_cast<int32_t>(c_meta_stride);
451 plane_info[3].scanlines = static_cast<int32_t>(c_meta_height);
452 plane_info[3].size = static_cast<uint32_t>(c_meta_size);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700453}
454
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530455// This API gets information about 8 planes (Y_Plane, UV_Plane, Y_Meta_Plane, UV_Meta_Plane,
456// Y_Plane, UV_Plane, Y_Meta_Plane, UV_Meta_Plane) and it stores the
457// information in PlaneLayoutInfo array.
458void GetYuvUbwcInterlacedSPPlaneInfo(uint32_t width, uint32_t height,
459 PlaneLayoutInfo plane_info[8]) {
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700460 // UBWC interlaced has top-bottom field layout with each field as
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530461 // 8-plane (including meta plane also) NV12_UBWC with width = image_width
462 // & height = image_height / 2.
463 // Client passed plane_info argument is ptr to struct PlaneLayoutInfo[8].
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700464 // Plane info to be filled for each field separately.
465 height = (height + 1) >> 1;
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700466
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530467 GetYuvUbwcSPPlaneInfo(width, height, COLOR_FMT_NV12_UBWC, &plane_info[0]);
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700468
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530469 GetYuvUbwcSPPlaneInfo(width, height, COLOR_FMT_NV12_UBWC, &plane_info[4]);
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700470}
471
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530472// This API gets information about 2 planes (Y_Plane & UV_Plane)
473void GetYuvSPPlaneInfo(const BufferInfo &info, int format, uint32_t width, uint32_t height,
474 uint32_t bpp, PlaneLayoutInfo *plane_info) {
475 int unaligned_width = info.width;
476 int unaligned_height = info.height;
477 unsigned int y_stride = 0, y_height = 0, y_size = 0;
478 unsigned int c_stride = 0, c_height = 0, c_size = 0;
479 uint64_t yOffset, cOffset;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700480
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530481 y_stride = c_stride = UINT(width) * bpp;
482 y_height = INT(height);
483 y_size = y_stride * y_height;
484 switch (format) {
485 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
486 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
487 c_size = (width * height) / 2 + 1;
488 c_height = height >> 1;
489 break;
490 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
491 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
492 if (unaligned_width & 1) {
493 ALOGE("width is odd for the YUV422_SP format");
494 return;
495 }
496 c_size = width * height;
497 c_height = height;
498 break;
499 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
500 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
501 c_height = VENUS_UV_SCANLINES(COLOR_FMT_NV12, height);
502 c_size = c_stride * c_height;
503 break;
504 case HAL_PIXEL_FORMAT_NV12_HEIF:
505 c_height = VENUS_UV_SCANLINES(COLOR_FMT_NV12_512, height);
506 c_size = c_stride * c_height;
507 break;
508 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
509 y_size = ALIGN(width * height, 4096);
510 c_size = ALIGN(2 * ALIGN(unaligned_width / 2, 32) * ALIGN(unaligned_height / 2, 32), 4096);
511 break;
512 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
Ramakant Singh13596ae2020-01-06 20:12:34 +0530513 case HAL_PIXEL_FORMAT_NV21_ENCODEABLE:
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530514 c_height = VENUS_UV_SCANLINES(COLOR_FMT_NV21, height);
515 c_size = c_stride * c_height;
516 break;
517 case HAL_PIXEL_FORMAT_NV21_ZSL:
518 c_size = (width * height) / 2;
519 c_height = height >> 1;
520 break;
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530521 case HAL_PIXEL_FORMAT_Y16:
522 c_size = width * height;
523 c_height = height;
524 break;
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530525 case HAL_PIXEL_FORMAT_Y8:
526 c_size = 0;
527 break;
528 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
529 c_size = (width * height) + 1;
530 c_height = height;
531 break;
532 default:
533 break;
534 }
535
536 yOffset = 0;
537 cOffset = y_size;
538
539 plane_info[0].component = (PlaneComponent)PLANE_COMPONENT_Y;
540 plane_info[0].offset = (uint32_t)yOffset;
541 plane_info[0].step = 1;
542 plane_info[0].stride = static_cast<int32_t>(UINT(width));
543 plane_info[0].stride_bytes = static_cast<int32_t>(y_stride);
544 plane_info[0].scanlines = static_cast<int32_t>(y_height);
545 plane_info[0].size = static_cast<uint32_t>(y_size);
546
547 plane_info[1].component = (PlaneComponent)(PLANE_COMPONENT_Cb | PLANE_COMPONENT_Cr);
548 plane_info[1].offset = (uint32_t)cOffset;
549 plane_info[1].step = 2 * bpp;
550 plane_info[1].stride = static_cast<int32_t>(UINT(width));
551 plane_info[1].stride_bytes = static_cast<int32_t>(c_stride);
552 plane_info[1].scanlines = static_cast<int32_t>(c_height);
553 plane_info[1].size = static_cast<uint32_t>(c_size);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700554}
555
Naseer Ahmed7aff2612017-10-05 20:39:05 -0400556int GetYUVPlaneInfo(const private_handle_t *hnd, struct android_ycbcr ycbcr[2]) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700557 int err = 0;
558 uint32_t width = UINT(hnd->width);
559 uint32_t height = UINT(hnd->height);
560 int format = hnd->format;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500561 uint64_t usage = hnd->usage;
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530562 int32_t interlaced = 0;
563 int plane_count = 0;
564 int unaligned_width = INT(hnd->unaligned_width);
565 int unaligned_height = INT(hnd->unaligned_height);
566 BufferInfo info(unaligned_width, unaligned_height, format, usage);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700567
568 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700569
570 // Check if UBWC buffer has been rendered in linear format.
Ramakant Singhc7d07792017-07-26 15:36:33 +0530571 int linear_format = 0;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500572 if (getMetaData(const_cast<private_handle_t *>(hnd), GET_LINEAR_FORMAT, &linear_format) == 0) {
573 format = INT(linear_format);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700574 }
575
576 // Check metadata if the geometry has been updated.
Ramakant Singhc7d07792017-07-26 15:36:33 +0530577 BufferDim_t buffer_dim;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500578 if (getMetaData(const_cast<private_handle_t *>(hnd), GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
579 BufferInfo info(buffer_dim.sliceWidth, buffer_dim.sliceHeight, format, usage);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700580 GetAlignedWidthAndHeight(info, &width, &height);
581 }
582
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700583 // Check metadata for interlaced content.
Ramakant Singhc7d07792017-07-26 15:36:33 +0530584 int interlace_flag = 0;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500585 if (getMetaData(const_cast<private_handle_t *>(hnd), GET_PP_PARAM_INTERLACED, &interlace_flag) ==
586 0) {
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530587 if (interlace_flag) {
588 interlaced = LAYOUT_INTERLACED_FLAG;
589 }
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700590 }
591
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530592 PlaneLayoutInfo plane_info[8] = {};
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700593 // Get the chroma offsets from the handle width/height. We take advantage
594 // of the fact the width _is_ the stride
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530595 err = GetYUVPlaneInfo(info, format, width, height, interlaced, &plane_count, plane_info);
596 if (err == 0) {
597 if (interlaced && format == HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC) {
598 CopyPlaneLayoutInfotoAndroidYcbcr(hnd->base, plane_count, &plane_info[0], &ycbcr[0]);
599 unsigned int uv_stride, uv_height, uv_size;
600 unsigned int alignment = 4096;
601 uint64_t field_base;
602 height = (height + 1) >> 1;
603 uv_stride = VENUS_UV_STRIDE(COLOR_FMT_NV12_UBWC, INT(width));
604 uv_height = VENUS_UV_SCANLINES(COLOR_FMT_NV12_UBWC, INT(height));
605 uv_size = ALIGN((uv_stride * uv_height), alignment);
606 field_base = hnd->base + plane_info[1].offset + uv_size;
607 memset(ycbcr[1].reserved, 0, sizeof(ycbcr[1].reserved));
608 CopyPlaneLayoutInfotoAndroidYcbcr(field_base, plane_count, &plane_info[4], &ycbcr[1]);
609 } else {
610 CopyPlaneLayoutInfotoAndroidYcbcr(hnd->base, plane_count, plane_info, ycbcr);
611 switch (format) {
612 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
613 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
614 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
615 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
616 case HAL_PIXEL_FORMAT_NV21_ZSL:
617 case HAL_PIXEL_FORMAT_RAW16:
618 case HAL_PIXEL_FORMAT_Y16:
619 case HAL_PIXEL_FORMAT_RAW10:
620 case HAL_PIXEL_FORMAT_RAW8:
621 case HAL_PIXEL_FORMAT_Y8:
622 std::swap(ycbcr->cb, ycbcr->cr);
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700623 }
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530624 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700625 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700626 return err;
627}
628
Yichi Chen24d146c2020-05-27 20:16:13 +0800629int GetRawPlaneInfo(int32_t format, int32_t width, int32_t height, PlaneLayoutInfo *plane_info) {
630 int32_t step = 0;
631
632 switch (format) {
633 case HAL_PIXEL_FORMAT_RAW16:
634 step = 2;
635 break;
636 case HAL_PIXEL_FORMAT_RAW8:
637 step = 1;
638 break;
639 case HAL_PIXEL_FORMAT_RAW12:
640 case HAL_PIXEL_FORMAT_RAW10:
641 step = 0;
642 break;
643 default:
644 ALOGW("RawPlaneInfo is unsupported for format 0x%x", format);
645 return -EINVAL;
646 }
647
648 BufferInfo info(width, height, format);
649 uint32_t alignedWidth, alignedHeight;
650 GetAlignedWidthAndHeight(info, &alignedWidth, &alignedHeight);
651
652 uint32_t size = GetSize(info, alignedWidth, alignedHeight);
653
654 plane_info[0].component = (PlaneComponent)PLANE_COMPONENT_RAW;
655 plane_info[0].h_subsampling = 0;
656 plane_info[0].v_subsampling = 0;
657 plane_info[0].offset = 0;
658 plane_info[0].step = step;
659 plane_info[0].stride = width;
660 plane_info[0].stride_bytes = static_cast<int32_t>(alignedWidth);
661 plane_info[0].scanlines = height;
662 plane_info[0].size = size;
663
664 return 0;
665}
666
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700667// Explicitly defined UBWC formats
668bool IsUBwcFormat(int format) {
669 switch (format) {
670 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
671 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
672 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
673 return true;
674 default:
675 return false;
676 }
677}
678
679bool IsUBwcSupported(int format) {
680 // Existing HAL formats with UBWC support
681 switch (format) {
682 case HAL_PIXEL_FORMAT_BGR_565:
683 case HAL_PIXEL_FORMAT_RGBA_8888:
684 case HAL_PIXEL_FORMAT_RGBX_8888:
685 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
686 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
687 case HAL_PIXEL_FORMAT_RGBA_1010102:
688 case HAL_PIXEL_FORMAT_RGBX_1010102:
Saurabh Dubey11df8e12018-07-25 19:16:25 +0530689 case HAL_PIXEL_FORMAT_DEPTH_16:
690 case HAL_PIXEL_FORMAT_DEPTH_24:
691 case HAL_PIXEL_FORMAT_DEPTH_24_STENCIL_8:
692 case HAL_PIXEL_FORMAT_DEPTH_32F:
693 case HAL_PIXEL_FORMAT_STENCIL_8:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700694 return true;
695 default:
696 break;
697 }
698
699 return false;
700}
701
Saurabh Dubey48b757d2018-05-24 19:13:53 +0530702bool IsUBwcPISupported(int format, uint64_t usage) {
703 if (usage & BufferUsage::COMPOSER_OVERLAY || !(usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC_PI)) {
704 return false;
705 }
706
707 // As of now only two formats
708 switch (format) {
709 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
710 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC: {
711 if ((usage & BufferUsage::GPU_TEXTURE) || (usage & BufferUsage::GPU_RENDER_TARGET)) {
712 if (AdrenoMemInfo::GetInstance()) {
713 return AdrenoMemInfo::GetInstance()->IsPISupportedByGPU(format, usage);
714 }
715 } else {
716 return true;
717 }
718 }
719 }
720
721 return false;
722}
723
Naseer Ahmede36f2242017-12-01 15:33:56 -0500724bool IsUBwcEnabled(int format, uint64_t usage) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700725 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
726 if (IsUBwcFormat(format)) {
727 return true;
728 }
729
730 // Allow UBWC, if an OpenGL client sets UBWC usage flag and GPU plus MDP
731 // support the format. OR if a non-OpenGL client like Rotator, sets UBWC
732 // usage flag and MDP supports the format.
Naseer Ahmed2023cf32019-01-28 19:02:40 -0500733 if (((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) ||
734 (usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC_PI) ||
735 (usage & BufferUsage::COMPOSER_CLIENT_TARGET))
Saurabh Dubey48b757d2018-05-24 19:13:53 +0530736 && IsUBwcSupported(format)) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700737 bool enable = true;
738 // Query GPU for UBWC only if buffer is intended to be used by GPU.
Naseer Ahmede36f2242017-12-01 15:33:56 -0500739 if ((usage & BufferUsage::GPU_TEXTURE) || (usage & BufferUsage::GPU_RENDER_TARGET)) {
Pramodh Kumar Mukunda927729d2017-06-15 16:18:51 +0530740 if (AdrenoMemInfo::GetInstance()) {
741 enable = AdrenoMemInfo::GetInstance()->IsUBWCSupportedByGPU(format);
742 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700743 }
744
745 // Allow UBWC, only if CPU usage flags are not set
Naseer Ahmede36f2242017-12-01 15:33:56 -0500746 if (enable && !(CpuCanAccess(usage))) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700747 return true;
748 }
749 }
750
751 return false;
752}
753
754void GetYuvUBwcWidthAndHeight(int width, int height, int format, unsigned int *aligned_w,
755 unsigned int *aligned_h) {
756 switch (format) {
757 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
758 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Priyanka Gujjula5eb8c8b2020-07-16 00:51:40 +0530759 *aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
760 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
761 break;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700762 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
763 *aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
764 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
765 break;
766 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
767 // The macro returns the stride which is 4/3 times the width, hence * 3/4
768 *aligned_w = (VENUS_Y_STRIDE(COLOR_FMT_NV12_BPP10_UBWC, width) * 3) / 4;
769 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_BPP10_UBWC, height);
770 break;
771 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
772 // The macro returns the stride which is 2 times the width, hence / 2
773 *aligned_w = (VENUS_Y_STRIDE(COLOR_FMT_P010_UBWC, width) / 2);
774 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_P010_UBWC, height);
775 break;
776 default:
777 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
778 *aligned_w = 0;
779 *aligned_h = 0;
780 break;
781 }
782}
783
784void GetRgbUBwcBlockSize(uint32_t bpp, int *block_width, int *block_height) {
785 *block_width = 0;
786 *block_height = 0;
787
788 switch (bpp) {
789 case 2:
790 case 4:
791 *block_width = 16;
792 *block_height = 4;
793 break;
794 case 8:
795 *block_width = 8;
796 *block_height = 4;
797 break;
798 case 16:
799 *block_width = 4;
800 *block_height = 4;
801 break;
802 default:
803 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
804 break;
805 }
806}
807
808unsigned int GetRgbUBwcMetaBufferSize(int width, int height, uint32_t bpp) {
809 unsigned int size = 0;
810 int meta_width, meta_height;
811 int block_width, block_height;
812
813 GetRgbUBwcBlockSize(bpp, &block_width, &block_height);
814 if (!block_width || !block_height) {
815 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
816 return size;
817 }
818
819 // Align meta buffer height to 16 blocks
820 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
821
822 // Align meta buffer width to 64 blocks
823 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
824
825 // Align meta buffer size to 4K
826 size = (unsigned int)ALIGN((meta_width * meta_height), 4096);
827
828 return size;
829}
830
831unsigned int GetUBwcSize(int width, int height, int format, unsigned int alignedw,
832 unsigned int alignedh) {
833 unsigned int size = 0;
834 uint32_t bpp = 0;
835 switch (format) {
836 case HAL_PIXEL_FORMAT_BGR_565:
837 case HAL_PIXEL_FORMAT_RGBA_8888:
838 case HAL_PIXEL_FORMAT_RGBX_8888:
839 case HAL_PIXEL_FORMAT_RGBA_1010102:
840 case HAL_PIXEL_FORMAT_RGBX_1010102:
841 bpp = GetBppForUncompressedRGB(format);
842 size = alignedw * alignedh * bpp;
843 size += GetRgbUBwcMetaBufferSize(width, height, bpp);
844 break;
Priyanka Gujjula5eb8c8b2020-07-16 00:51:40 +0530845 /*
846 * 1. The CtsMediaV2TestCases#CodecEncoderSurfaceTest is a transcode use case and shares
847 * same surface between encoder and decoder.
848 * 2. Configures encoder with Opaque color format thus encoder sets ubwc usage bits and
849 * is configured with NV12_UBWC format.
850 * 3. Configures decoder as 'flexible', thus configuring decoder with NV12 format.
851 * 4. Decoder should produce output to surface that will be fed back to encoder as input.
852 * 5. Though UBWC is enabled, we need to compute the actual buffer size (including aligned
853 * width and height) based on pixel format that is set.
854 */
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700855 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
856 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Priyanka Gujjula5eb8c8b2020-07-16 00:51:40 +0530857 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
858 break;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700859 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
860 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
861 break;
862 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
863 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_BPP10_UBWC, width, height);
864 break;
865 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
866 size = VENUS_BUFFER_SIZE(COLOR_FMT_P010_UBWC, width, height);
867 break;
868 default:
869 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
870 break;
871 }
872
873 return size;
874}
875
Ashish Kumar8a1b56e2019-11-15 18:15:22 +0530876unsigned int GetRgbMetaSize(int format, uint32_t width, uint32_t height, uint64_t usage) {
877 unsigned int meta_size = 0;
878 if (!IsUBwcEnabled(format, usage)) {
879 return meta_size;
880 }
881 uint32_t bpp = GetBppForUncompressedRGB(format);
882 switch (format) {
883 case HAL_PIXEL_FORMAT_BGR_565:
884 case HAL_PIXEL_FORMAT_RGBA_8888:
885 case HAL_PIXEL_FORMAT_RGBX_8888:
886 case HAL_PIXEL_FORMAT_RGBA_1010102:
887 case HAL_PIXEL_FORMAT_RGBX_1010102:
888 case HAL_PIXEL_FORMAT_RGBA_FP16:
889 meta_size = GetRgbUBwcMetaBufferSize(width, height, bpp);
890 break;
891 default:
892 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, format);
893 break;
894 }
895 return meta_size;
896}
897
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700898int GetRgbDataAddress(private_handle_t *hnd, void **rgb_data) {
899 int err = 0;
900
901 // This api is for RGB* formats
Naseer Ahmede36f2242017-12-01 15:33:56 -0500902 if (!IsUncompressedRGBFormat(hnd->format)) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700903 return -EINVAL;
904 }
905
906 // linear buffer, nothing to do further
907 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
908 *rgb_data = reinterpret_cast<void *>(hnd->base);
909 return err;
910 }
Ashish Kumar8a1b56e2019-11-15 18:15:22 +0530911 unsigned int meta_size = GetRgbMetaSize(hnd->format, hnd->width, hnd->height, hnd->usage);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700912
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700913 *rgb_data = reinterpret_cast<void *>(hnd->base + meta_size);
914
915 return err;
916}
917
Naseer Ahmede36f2242017-12-01 15:33:56 -0500918void GetCustomDimensions(private_handle_t *hnd, int *stride, int *height) {
919 BufferDim_t buffer_dim;
920 int interlaced = 0;
921
922 *stride = hnd->width;
923 *height = hnd->height;
924 if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
925 *stride = buffer_dim.sliceWidth;
926 *height = buffer_dim.sliceHeight;
927 } else if (getMetaData(hnd, GET_PP_PARAM_INTERLACED, &interlaced) == 0) {
928 if (interlaced && IsUBwcFormat(hnd->format)) {
929 unsigned int alignedw = 0, alignedh = 0;
930 // Get re-aligned height for single ubwc interlaced field and
931 // multiply by 2 to get frame height.
932 BufferInfo info(hnd->width, ((hnd->height + 1) >> 1), hnd->format);
933 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
934 *stride = static_cast<int>(alignedw);
935 *height = static_cast<int>(alignedh * 2);
936 }
937 }
938}
939
940void GetColorSpaceFromMetadata(private_handle_t *hnd, int *color_space) {
941 ColorMetaData color_metadata;
942 if (getMetaData(hnd, GET_COLOR_METADATA, &color_metadata) == 0) {
943 switch (color_metadata.colorPrimaries) {
944 case ColorPrimaries_BT709_5:
945 *color_space = HAL_CSC_ITU_R_709;
946 break;
947 case ColorPrimaries_BT601_6_525:
948 case ColorPrimaries_BT601_6_625:
949 *color_space = ((color_metadata.range) ? HAL_CSC_ITU_R_601_FR : HAL_CSC_ITU_R_601);
950 break;
951 case ColorPrimaries_BT2020:
952 *color_space = (color_metadata.range) ? HAL_CSC_ITU_R_2020_FR : HAL_CSC_ITU_R_2020;
953 break;
954 default:
955 ALOGE("Unknown Color Space = %d", color_metadata.colorPrimaries);
956 break;
957 }
958 } else if (getMetaData(hnd, GET_COLOR_SPACE, color_space) != 0) {
959 *color_space = 0;
960 }
961}
962
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700963void GetAlignedWidthAndHeight(const BufferInfo &info, unsigned int *alignedw,
964 unsigned int *alignedh) {
965 int width = info.width;
966 int height = info.height;
967 int format = info.format;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500968 uint64_t usage = info.usage;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700969
970 // Currently surface padding is only computed for RGB* surfaces.
Naseer Ahmede36f2242017-12-01 15:33:56 -0500971 bool ubwc_enabled = IsUBwcEnabled(format, usage);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700972 int tile = ubwc_enabled;
973
974 if (IsUncompressedRGBFormat(format)) {
Pramodh Kumar Mukunda927729d2017-06-15 16:18:51 +0530975 if (AdrenoMemInfo::GetInstance()) {
976 AdrenoMemInfo::GetInstance()->AlignUnCompressedRGB(width, height, format, tile, alignedw,
977 alignedh);
978 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700979 return;
980 }
981
982 if (ubwc_enabled) {
983 GetYuvUBwcWidthAndHeight(width, height, format, alignedw, alignedh);
984 return;
985 }
986
987 if (IsCompressedRGBFormat(format)) {
Pramodh Kumar Mukunda927729d2017-06-15 16:18:51 +0530988 if (AdrenoMemInfo::GetInstance()) {
989 AdrenoMemInfo::GetInstance()->AlignCompressedRGB(width, height, format, alignedw, alignedh);
990 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700991 return;
992 }
993
994 int aligned_w = width;
995 int aligned_h = height;
996 unsigned int alignment = 32;
997
998 // Below should be only YUV family
999 switch (format) {
1000 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1001 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
Pramodh Kumar Mukunda927729d2017-06-15 16:18:51 +05301002 if (AdrenoMemInfo::GetInstance() == nullptr) {
1003 return;
1004 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001005 alignment = AdrenoMemInfo::GetInstance()->GetGpuPixelAlignment();
1006 aligned_w = ALIGN(width, alignment);
1007 break;
1008 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
1009 aligned_w = ALIGN(width, alignment);
1010 break;
1011 case HAL_PIXEL_FORMAT_RAW16:
Anjaneya Prasad Musunurib1654142017-04-18 10:32:42 +05301012 case HAL_PIXEL_FORMAT_Y16:
1013 case HAL_PIXEL_FORMAT_Y8:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001014 aligned_w = ALIGN(width, 16);
1015 break;
1016 case HAL_PIXEL_FORMAT_RAW12:
Naseer Ahmed1187a572017-08-14 13:16:55 -04001017 aligned_w = ALIGN(width * 12 / 8, 16);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001018 break;
1019 case HAL_PIXEL_FORMAT_RAW10:
Naseer Ahmed1187a572017-08-14 13:16:55 -04001020 aligned_w = ALIGN(width * 10 / 8, 16);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001021 break;
1022 case HAL_PIXEL_FORMAT_RAW8:
Naseer Ahmed1187a572017-08-14 13:16:55 -04001023 aligned_w = ALIGN(width, 16);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001024 break;
1025 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
1026 aligned_w = ALIGN(width, 128);
1027 break;
1028 case HAL_PIXEL_FORMAT_YV12:
1029 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
1030 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
1031 case HAL_PIXEL_FORMAT_YCbCr_422_I:
1032 case HAL_PIXEL_FORMAT_YCrCb_422_I:
1033 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
1034 aligned_w = ALIGN(width, 16);
1035 break;
Mathew Joseph Karimpanala73082e2017-10-16 18:01:21 +05301036 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
1037 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_P010, width) / 2);
1038 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_P010, height));
1039 break;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001040 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1041 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1042 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV12, width));
1043 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV12, height));
1044 break;
1045 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
Ramakant Singh13596ae2020-01-06 20:12:34 +05301046 case HAL_PIXEL_FORMAT_NV21_ENCODEABLE:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001047 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV21, width));
1048 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV21, height));
1049 break;
1050 case HAL_PIXEL_FORMAT_BLOB:
1051 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
1052 break;
1053 case HAL_PIXEL_FORMAT_NV21_ZSL:
1054 aligned_w = ALIGN(width, 64);
1055 aligned_h = ALIGN(height, 64);
1056 break;
Rajavenu Kyatham6e8c1822018-07-31 10:27:19 +05301057 case HAL_PIXEL_FORMAT_NV12_HEIF:
1058 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV12_512, width));
1059 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV12_512, height));
1060 break;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001061 default:
1062 break;
1063 }
1064
1065 *alignedw = (unsigned int)aligned_w;
1066 *alignedh = (unsigned int)aligned_h;
1067}
1068
Naseer Ahmede36f2242017-12-01 15:33:56 -05001069int GetBufferLayout(private_handle_t *hnd, uint32_t stride[4], uint32_t offset[4],
1070 uint32_t *num_planes) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001071 if (!hnd || !stride || !offset || !num_planes) {
1072 return -EINVAL;
1073 }
1074
Naseer Ahmed7aff2612017-10-05 20:39:05 -04001075 struct android_ycbcr yuvPlaneInfo[2] = {};
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001076 *num_planes = 1;
1077 stride[0] = 0;
1078
1079 switch (hnd->format) {
1080 case HAL_PIXEL_FORMAT_RGB_565:
1081 case HAL_PIXEL_FORMAT_BGR_565:
1082 case HAL_PIXEL_FORMAT_RGBA_5551:
1083 case HAL_PIXEL_FORMAT_RGBA_4444:
1084 stride[0] = static_cast<uint32_t>(hnd->width * 2);
1085 break;
1086 case HAL_PIXEL_FORMAT_RGB_888:
1087 stride[0] = static_cast<uint32_t>(hnd->width * 3);
1088 break;
1089 case HAL_PIXEL_FORMAT_RGBA_8888:
1090 case HAL_PIXEL_FORMAT_BGRA_8888:
1091 case HAL_PIXEL_FORMAT_RGBX_8888:
1092 case HAL_PIXEL_FORMAT_BGRX_8888:
1093 case HAL_PIXEL_FORMAT_RGBA_1010102:
1094 case HAL_PIXEL_FORMAT_ARGB_2101010:
1095 case HAL_PIXEL_FORMAT_RGBX_1010102:
1096 case HAL_PIXEL_FORMAT_XRGB_2101010:
1097 case HAL_PIXEL_FORMAT_BGRA_1010102:
1098 case HAL_PIXEL_FORMAT_ABGR_2101010:
1099 case HAL_PIXEL_FORMAT_BGRX_1010102:
1100 case HAL_PIXEL_FORMAT_XBGR_2101010:
1101 stride[0] = static_cast<uint32_t>(hnd->width * 4);
1102 break;
1103 }
1104
1105 // Format is RGB
1106 if (stride[0]) {
1107 return 0;
1108 }
1109
1110 (*num_planes)++;
Naseer Ahmed7aff2612017-10-05 20:39:05 -04001111 int ret = GetYUVPlaneInfo(hnd, yuvPlaneInfo);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001112 if (ret < 0) {
1113 ALOGE("%s failed", __FUNCTION__);
1114 return ret;
1115 }
1116
Naseer Ahmed7aff2612017-10-05 20:39:05 -04001117 // We are only returning buffer layout for progressive or single field formats.
1118 struct android_ycbcr yuvInfo = yuvPlaneInfo[0];
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001119 stride[0] = static_cast<uint32_t>(yuvInfo.ystride);
1120 offset[0] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.y) - hnd->base);
1121 stride[1] = static_cast<uint32_t>(yuvInfo.cstride);
1122 switch (hnd->format) {
1123 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
1124 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
1125 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1126 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1127 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1128 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
1129 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1130 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
Mathew Joseph Karimpanala73082e2017-10-16 18:01:21 +05301131 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
Rajavenu Kyatham6e8c1822018-07-31 10:27:19 +05301132 case HAL_PIXEL_FORMAT_NV12_HEIF:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001133 offset[1] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cb) - hnd->base);
1134 break;
1135 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1136 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
1137 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramakant Singh13596ae2020-01-06 20:12:34 +05301138 case HAL_PIXEL_FORMAT_NV21_ENCODEABLE:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001139 offset[1] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cr) - hnd->base);
1140 break;
1141 case HAL_PIXEL_FORMAT_YV12:
1142 offset[1] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cr) - hnd->base);
1143 stride[2] = static_cast<uint32_t>(yuvInfo.cstride);
1144 offset[2] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cb) - hnd->base);
1145 (*num_planes)++;
1146 break;
Camus Wonga300db62018-09-27 16:38:50 -04001147 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
1148 *num_planes = 1;
1149 break;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001150 default:
1151 ALOGW("%s: Unsupported format", __FUNCTION__);
1152 ret = -EINVAL;
1153 }
1154
1155 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
1156 std::fill(offset, offset + 4, 0);
1157 }
1158
1159 return 0;
1160}
1161
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -07001162int GetGpuResourceSizeAndDimensions(const BufferInfo &info, unsigned int *size,
1163 unsigned int *alignedw, unsigned int *alignedh,
1164 GraphicsMetadata *graphics_metadata) {
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301165 GetAlignedWidthAndHeight(info, alignedw, alignedh);
1166 AdrenoMemInfo* adreno_mem_info = AdrenoMemInfo::GetInstance();
1167 graphics_metadata->size = adreno_mem_info->AdrenoGetMetadataBlobSize();
1168 uint64_t adreno_usage = info.usage;
1169 // If gralloc disables UBWC based on any of the checks,
1170 // we pass modified usage flag to adreno to convey this.
1171 int is_ubwc_enabled = IsUBwcEnabled(info.format, info.usage);
1172 if (!is_ubwc_enabled) {
1173 adreno_usage &= ~(GRALLOC_USAGE_PRIVATE_ALLOC_UBWC);
Naseer Ahmed2023cf32019-01-28 19:02:40 -05001174 } else {
1175 adreno_usage |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301176 }
1177
1178 // Call adreno api for populating metadata blob
Saurabh Dubeyb637bb32018-05-15 16:20:10 +05301179 // Layer count is for 2D/Cubemap arrays and depth is used for 3D slice
1180 // Using depth to pass layer_count here
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301181 int ret = adreno_mem_info->AdrenoInitMemoryLayout(graphics_metadata->data, info.width,
Saurabh Dubeyb637bb32018-05-15 16:20:10 +05301182 info.height, info.layer_count, /* depth */
1183 info.format, 1, is_ubwc_enabled,
1184 adreno_usage, 1);
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301185 if (ret != 0) {
1186 ALOGE("%s Graphics metadata init failed", __FUNCTION__);
1187 *size = 0;
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -07001188 return -EINVAL;
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301189 }
1190 // Call adreno api with the metadata blob to get buffer size
1191 *size = adreno_mem_info->AdrenoGetAlignedGpuBufferSize(graphics_metadata->data);
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -07001192 return 0;
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301193}
1194
Saurabh Dubey52eb7a62018-06-15 16:31:23 +05301195bool CanUseAdrenoForSize(int buffer_type, uint64_t usage) {
1196 if (buffer_type == BUFFER_TYPE_VIDEO || !GetAdrenoSizeAPIStatus()) {
1197 return false;
1198 }
1199
1200 if ((usage & BufferUsage::PROTECTED) && ((usage & BufferUsage::CAMERA_OUTPUT) ||
1201 (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY))) {
1202 return false;
1203 }
1204
1205 return true;
1206}
1207
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301208bool GetAdrenoSizeAPIStatus() {
1209 AdrenoMemInfo* adreno_mem_info = AdrenoMemInfo::GetInstance();
1210 if (adreno_mem_info) {
1211 return adreno_mem_info->AdrenoSizeAPIAvaliable();
1212 }
1213 return false;
1214}
1215
Saurabh Dubey48b757d2018-05-24 19:13:53 +05301216bool UseUncached(int format, uint64_t usage) {
1217 if ((usage & GRALLOC_USAGE_PRIVATE_UNCACHED) || (usage & BufferUsage::PROTECTED)) {
1218 return true;
1219 }
1220
1221 // CPU read rarely
1222 if ((usage & BufferUsage::CPU_READ_MASK) == static_cast<uint64_t>(BufferUsage::CPU_READ_RARELY)) {
1223 return true;
1224 }
1225
1226 // CPU write rarely
1227 if ((usage & BufferUsage::CPU_WRITE_MASK) ==
1228 static_cast<uint64_t>(BufferUsage::CPU_WRITE_RARELY)) {
1229 return true;
1230 }
1231
1232 if ((usage & BufferUsage::SENSOR_DIRECT_DATA) || (usage & BufferUsage::GPU_DATA_BUFFER)) {
1233 return true;
1234 }
1235
1236 if (format && IsUBwcEnabled(format, usage)) {
1237 return true;
1238 }
1239
1240 return false;
1241}
1242
1243uint64_t GetHandleFlags(int format, uint64_t usage) {
1244 uint64_t priv_flags = 0;
1245
1246 if (usage & BufferUsage::VIDEO_ENCODER) {
1247 priv_flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
1248 }
1249
1250 if (usage & BufferUsage::CAMERA_OUTPUT) {
1251 priv_flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
1252 }
1253
1254 if (usage & BufferUsage::CAMERA_INPUT) {
1255 priv_flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
1256 }
1257
1258 if (usage & BufferUsage::COMPOSER_OVERLAY) {
1259 priv_flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER;
1260 }
1261
1262 if (usage & BufferUsage::GPU_TEXTURE) {
1263 priv_flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
1264 }
1265
1266 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
1267 priv_flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
1268 }
1269
1270 if (IsUBwcEnabled(format, usage)) {
1271 if (IsUBwcPISupported(format, usage)) {
1272 priv_flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED_PI;
1273 } else {
1274 priv_flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
1275 }
1276 }
1277
1278 if (usage & (BufferUsage::CPU_READ_MASK | BufferUsage::CPU_WRITE_MASK)) {
1279 priv_flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED;
1280 }
1281
1282 if ((usage & (BufferUsage::VIDEO_ENCODER | BufferUsage::VIDEO_DECODER |
1283 BufferUsage::CAMERA_OUTPUT | BufferUsage::GPU_RENDER_TARGET))) {
1284 priv_flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER;
1285 }
1286
1287 if (!UseUncached(format, usage)) {
1288 priv_flags |= private_handle_t::PRIV_FLAGS_CACHED;
1289 }
1290
1291 return priv_flags;
1292}
1293
1294int GetImplDefinedFormat(uint64_t usage, int format) {
1295 int gr_format = format;
1296
1297 // If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
1298 // the usage bits, gralloc assigns a format.
1299 if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED ||
1300 format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
Mathew Joseph Karimpanal71517182018-08-14 16:40:34 +05301301 if ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC || usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC_PI)
1302 && format != HAL_PIXEL_FORMAT_YCbCr_420_888) {
Saurabh Dubey81aef932018-06-27 09:41:31 +05301303 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
Saurabh Dubey48b757d2018-05-24 19:13:53 +05301304 } else if (usage & BufferUsage::VIDEO_ENCODER) {
1305 if (usage & GRALLOC_USAGE_PRIVATE_VIDEO_NV21_ENCODER) {
1306 gr_format = HAL_PIXEL_FORMAT_NV21_ENCODEABLE; // NV21
Manikanta Kanamarlapudidfa32e82019-03-26 14:06:47 +05301307 } else if (usage & GRALLOC_USAGE_PRIVATE_HEIF) {
1308 gr_format = HAL_PIXEL_FORMAT_NV12_HEIF;
Mathew Joseph Karimpanal71517182018-08-14 16:40:34 +05301309 } else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
1310 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS;
Saurabh Dubey48b757d2018-05-24 19:13:53 +05301311 } else {
1312 gr_format = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; // NV12
1313 }
1314 } else if (usage & BufferUsage::CAMERA_INPUT) {
1315 if (usage & BufferUsage::CAMERA_OUTPUT) {
1316 // Assumed ZSL if both producer and consumer camera flags set
1317 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL; // NV21
1318 } else {
1319 gr_format = HAL_PIXEL_FORMAT_YCrCb_420_SP; // NV21
1320 }
1321 } else if (usage & BufferUsage::CAMERA_OUTPUT) {
1322 if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
1323 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL; // NV21
1324 } else {
1325 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS; // NV12 preview
1326 }
1327 } else if (usage & BufferUsage::COMPOSER_OVERLAY) {
1328 // XXX: If we still haven't set a format, default to RGBA8888
1329 gr_format = HAL_PIXEL_FORMAT_RGBA_8888;
1330 } else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
1331 // If no other usage flags are detected, default the
1332 // flexible YUV format to NV21_ZSL
1333 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL;
1334 }
1335 }
1336
1337 return gr_format;
1338}
1339
1340int GetCustomFormatFlags(int format, uint64_t usage,
1341 int *custom_format, uint64_t *priv_flags) {
1342 *custom_format = GetImplDefinedFormat(usage, format);
1343 *priv_flags = GetHandleFlags(*custom_format, usage);
1344
Saurabh Dubey81aef932018-06-27 09:41:31 +05301345 if (usage & GRALLOC_USAGE_PROTECTED) {
1346 *priv_flags |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
1347 }
1348
1349 *priv_flags |= private_handle_t::PRIV_FLAGS_USES_ION;
1350
Saurabh Dubey48b757d2018-05-24 19:13:53 +05301351 return 0;
1352}
1353
Saurabh Dubeyefd7f9e2018-06-15 18:00:02 +05301354int GetBufferType(int inputFormat) {
1355 return IsYuvFormat(inputFormat) ? BUFFER_TYPE_VIDEO : BUFFER_TYPE_UI;
1356}
1357
Ashish Kumarb9392ff2019-03-27 18:30:39 +05301358int GetYUVPlaneInfo(const BufferInfo &info, int32_t format, int32_t width, int32_t height,
1359 int32_t flags, int *plane_count, PlaneLayoutInfo *plane_info) {
1360 int err = 0;
1361 unsigned int y_stride, c_stride, y_height, c_height, y_size, c_size;
1362 uint64_t yOffset, cOffset, crOffset, cbOffset;
1363 int h_subsampling = 0, v_subsampling = 0;
1364 switch (format) {
1365 // Semiplanar
1366 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
1367 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
1368 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1369 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Ramakant Singh13596ae2020-01-06 20:12:34 +05301370 case HAL_PIXEL_FORMAT_NV21_ENCODEABLE:
Ashish Kumarb9392ff2019-03-27 18:30:39 +05301371 case HAL_PIXEL_FORMAT_NV12_HEIF: // Same as YCbCr_420_SP_VENUS
1372 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1373 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
1374 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
1375 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
1376 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ashish Kumarb9392ff2019-03-27 18:30:39 +05301377 case HAL_PIXEL_FORMAT_Y16:
Ashish Kumarb9392ff2019-03-27 18:30:39 +05301378 case HAL_PIXEL_FORMAT_Y8:
1379 *plane_count = 2;
1380 GetYuvSPPlaneInfo(info, format, width, height, 1, plane_info);
1381 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1382 plane_info[0].h_subsampling = 0;
1383 plane_info[0].v_subsampling = 0;
1384 plane_info[1].h_subsampling = h_subsampling;
1385 plane_info[1].v_subsampling = v_subsampling;
1386 break;
1387
Yichi Chen24d146c2020-05-27 20:16:13 +08001388 case HAL_PIXEL_FORMAT_RAW10:
1389 case HAL_PIXEL_FORMAT_RAW8:
1390 case HAL_PIXEL_FORMAT_RAW16:
1391 case HAL_PIXEL_FORMAT_RAW12:
1392 *plane_count = 1;
1393 GetRawPlaneInfo(format, info.width, info.height, plane_info);
1394 break;
1395
1396
Ashish Kumarb9392ff2019-03-27 18:30:39 +05301397 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1398 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1399 if (flags & LAYOUT_INTERLACED_FLAG) {
1400 *plane_count = 8;
1401 GetYuvUbwcInterlacedSPPlaneInfo(width, height, plane_info);
1402 plane_info[0].step = plane_info[4].step = 1;
1403 plane_info[1].step = plane_info[5].step = 2;
1404 plane_info[0].h_subsampling = plane_info[4].h_subsampling = 0;
1405 plane_info[0].v_subsampling = plane_info[4].v_subsampling = 0;
1406 plane_info[1].h_subsampling = plane_info[5].h_subsampling = h_subsampling;
1407 plane_info[1].v_subsampling = plane_info[5].v_subsampling = v_subsampling;
1408 plane_info[2].h_subsampling = plane_info[3].h_subsampling = 0;
1409 plane_info[2].v_subsampling = plane_info[3].v_subsampling = 0;
1410 plane_info[2].step = plane_info[3].step = 0;
1411 plane_info[6].h_subsampling = plane_info[7].h_subsampling = 0;
1412 plane_info[6].v_subsampling = plane_info[7].v_subsampling = 0;
1413 plane_info[6].step = plane_info[7].step = 0;
1414 } else {
1415 *plane_count = 4;
1416 GetYuvUbwcSPPlaneInfo(width, height, COLOR_FMT_NV12_UBWC, plane_info);
1417 plane_info[0].h_subsampling = 0;
1418 plane_info[0].v_subsampling = 0;
1419 plane_info[0].step = 1;
1420 plane_info[1].h_subsampling = h_subsampling;
1421 plane_info[1].v_subsampling = v_subsampling;
1422 plane_info[1].step = 2;
1423 plane_info[2].h_subsampling = plane_info[3].h_subsampling = 0;
1424 plane_info[2].v_subsampling = plane_info[3].v_subsampling = 0;
1425 plane_info[2].step = plane_info[3].step = 0;
1426 }
1427 break;
1428
1429 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
1430 *plane_count = 2;
1431 GetYuvSPPlaneInfo(info, format, width, height, 2, plane_info);
1432 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1433 plane_info[0].h_subsampling = 0;
1434 plane_info[0].v_subsampling = 0;
1435 plane_info[1].h_subsampling = h_subsampling;
1436 plane_info[1].v_subsampling = v_subsampling;
1437 break;
1438
1439 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1440 *plane_count = 4;
1441 GetYuvUbwcSPPlaneInfo(width, height, COLOR_FMT_NV12_BPP10_UBWC, plane_info);
1442 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1443 plane_info[0].h_subsampling = 0;
1444 plane_info[0].v_subsampling = 0;
1445 plane_info[1].step = 1;
1446 plane_info[1].h_subsampling = h_subsampling;
1447 plane_info[1].v_subsampling = v_subsampling;
1448 plane_info[1].step = 3;
1449 plane_info[2].h_subsampling = plane_info[3].h_subsampling = 0;
1450 plane_info[2].v_subsampling = plane_info[3].v_subsampling = 0;
1451 plane_info[2].step = plane_info[3].step = 0;
1452 break;
1453
1454 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
1455 *plane_count = 4;
1456 GetYuvUbwcSPPlaneInfo(width, height, COLOR_FMT_P010_UBWC, plane_info);
1457 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1458 plane_info[0].h_subsampling = 0;
1459 plane_info[0].v_subsampling = 0;
1460 plane_info[1].step = 1;
1461 plane_info[1].h_subsampling = h_subsampling;
1462 plane_info[1].v_subsampling = v_subsampling;
1463 plane_info[1].step = 4;
1464 plane_info[2].h_subsampling = plane_info[3].h_subsampling = 0;
1465 plane_info[2].v_subsampling = plane_info[3].v_subsampling = 0;
1466 plane_info[2].step = plane_info[3].step = 0;
1467 break;
1468
1469 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
1470 *plane_count = 2;
1471 y_stride = VENUS_Y_STRIDE(COLOR_FMT_P010, width);
1472 c_stride = VENUS_UV_STRIDE(COLOR_FMT_P010, width);
1473 y_height = VENUS_Y_SCANLINES(COLOR_FMT_P010, height);
1474 y_size = y_stride * y_height;
1475 yOffset = 0;
1476 cOffset = y_size;
1477 c_height = VENUS_UV_SCANLINES(COLOR_FMT_P010, INT(height));
1478 c_size = c_stride * c_height;
1479 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1480
1481 plane_info[0].component = (PlaneComponent)PLANE_COMPONENT_Y;
1482 plane_info[0].offset = (uint32_t)yOffset;
1483 plane_info[0].stride = static_cast<int32_t>(UINT(width));
1484 plane_info[0].stride_bytes = static_cast<int32_t>(y_stride);
1485 plane_info[0].scanlines = static_cast<int32_t>(y_height);
1486 plane_info[0].size = static_cast<uint32_t>(y_size);
1487 plane_info[0].step = 1;
1488 plane_info[0].h_subsampling = 0;
1489 plane_info[0].v_subsampling = 0;
1490
1491 plane_info[1].component = (PlaneComponent)(PLANE_COMPONENT_Cb | PLANE_COMPONENT_Cr);
1492 plane_info[1].offset = (uint32_t)cOffset;
1493 plane_info[1].stride = static_cast<int32_t>(UINT(width));
1494 plane_info[1].stride_bytes = static_cast<int32_t>(c_stride);
1495 plane_info[1].scanlines = static_cast<int32_t>(c_height);
1496 plane_info[1].size = static_cast<uint32_t>(c_size);
1497 plane_info[1].step = 4;
1498 plane_info[1].h_subsampling = h_subsampling;
1499 plane_info[1].v_subsampling = v_subsampling;
1500 break;
1501
1502 // Planar
1503 case HAL_PIXEL_FORMAT_YV12:
1504 if ((info.width & 1) || (info.height & 1)) {
1505 ALOGE("w or h is odd for the YV12 format");
1506 err = -EINVAL;
1507 return err;
1508 }
1509 *plane_count = 3;
1510 y_stride = width;
1511 c_stride = ALIGN(width / 2, 16);
1512 y_height = UINT(height);
1513 y_size = (y_stride * y_height);
1514 height = height >> 1;
1515 c_height = UINT(height);
1516 c_size = (c_stride * c_height);
1517 yOffset = 0;
1518 crOffset = y_size;
1519 cbOffset = (y_size + c_size);
1520 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1521
1522 plane_info[0].component = (PlaneComponent)PLANE_COMPONENT_Y;
1523 plane_info[0].offset = (uint32_t)yOffset;
1524 plane_info[0].stride = static_cast<int32_t>(UINT(width));
1525 plane_info[0].stride_bytes = static_cast<int32_t>(y_stride);
1526 plane_info[0].scanlines = static_cast<int32_t>(y_height);
1527 plane_info[0].size = static_cast<uint32_t>(y_size);
1528 plane_info[0].step = 1;
1529 plane_info[0].h_subsampling = 0;
1530 plane_info[0].v_subsampling = 0;
1531
1532 plane_info[1].component = (PlaneComponent)PLANE_COMPONENT_Cb;
1533 plane_info[1].offset = (uint32_t)cbOffset;
1534 plane_info[2].component = (PlaneComponent)PLANE_COMPONENT_Cr;
1535 plane_info[2].offset = (uint32_t)crOffset;
1536 for (int i = 1; i < 3; i++) {
1537 plane_info[i].stride = static_cast<int32_t>(UINT(width));
1538 plane_info[i].stride_bytes = static_cast<int32_t>(c_stride);
1539 plane_info[i].scanlines = static_cast<int32_t>(c_height);
1540 plane_info[i].size = static_cast<uint32_t>(c_size);
1541 plane_info[i].step = 1;
1542 plane_info[i].h_subsampling = h_subsampling;
1543 plane_info[i].v_subsampling = v_subsampling;
1544 }
1545 break;
1546 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
1547 if (info.width & 1) {
1548 ALOGE("width is odd for the YUV422_SP format");
1549 err = -EINVAL;
1550 return err;
1551 }
1552 *plane_count = 1;
1553 y_stride = width * 2;
1554 y_height = UINT(height);
1555 y_size = y_stride * y_height;
1556 yOffset = 0;
1557 plane_info[0].component = (PlaneComponent)PLANE_COMPONENT_Y;
1558 plane_info[0].offset = (uint32_t)yOffset;
1559 plane_info[0].stride = static_cast<int32_t>(UINT(width));
1560 plane_info[0].stride_bytes = static_cast<int32_t>(y_stride);
1561 plane_info[0].scanlines = static_cast<int32_t>(y_height);
1562 plane_info[0].size = static_cast<uint32_t>(y_size);
1563 plane_info[0].step = 1;
1564 plane_info[0].h_subsampling = 0;
1565 plane_info[0].v_subsampling = 0;
1566 break;
1567
1568 // Unsupported formats
1569 case HAL_PIXEL_FORMAT_YCbCr_422_I:
1570 case HAL_PIXEL_FORMAT_YCrCb_422_I:
1571 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
1572 default:
1573 *plane_count = 0;
1574 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, format);
1575 err = -EINVAL;
1576 }
1577 return err;
1578}
1579
1580void GetYuvSubSamplingFactor(int32_t format, int *h_subsampling, int *v_subsampling) {
1581 switch (format) {
1582 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
1583 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
1584 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1585 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
1586 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
1587 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1588 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1589 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
1590 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
1591 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1592 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: // Same as YCbCr_420_SP_VENUS
Ramakant Singh13596ae2020-01-06 20:12:34 +05301593 case HAL_PIXEL_FORMAT_NV21_ENCODEABLE:
Ashish Kumarb9392ff2019-03-27 18:30:39 +05301594 case HAL_PIXEL_FORMAT_NV21_ZSL:
1595 case HAL_PIXEL_FORMAT_YV12:
1596 *h_subsampling = 1;
1597 *v_subsampling = 1;
1598 break;
1599 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
1600 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
1601 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
1602 *h_subsampling = 1;
1603 *v_subsampling = 0;
1604 break;
Ashish Kumarb9392ff2019-03-27 18:30:39 +05301605 case HAL_PIXEL_FORMAT_Y16:
Ashish Kumarb9392ff2019-03-27 18:30:39 +05301606 case HAL_PIXEL_FORMAT_Y8:
1607 case HAL_PIXEL_FORMAT_BLOB:
1608 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
1609 case HAL_PIXEL_FORMAT_NV12_HEIF:
1610 default:
1611 *h_subsampling = 0;
1612 *v_subsampling = 0;
1613 break;
1614 }
1615}
1616
1617void CopyPlaneLayoutInfotoAndroidYcbcr(uint64_t base, int plane_count, PlaneLayoutInfo *plane_info,
1618 struct android_ycbcr *ycbcr) {
1619 ycbcr->y = reinterpret_cast<void *>(base + plane_info[0].offset);
1620 ycbcr->ystride = plane_info[0].stride_bytes;
1621 if (plane_count == 1) {
1622 ycbcr->cb = NULL;
1623 ycbcr->cr = NULL;
1624 ycbcr->cstride = 0;
1625 ycbcr->chroma_step = 0;
1626 } else if (plane_count == 2 || plane_count == 4 || plane_count == 8) {
1627 /* For YUV semiplanar :-
1628 * - In progressive & linear case plane count is 2 and plane_info[0] will
1629 * contain info about Y plane and plane_info[1] will contain info about UV plane.
1630 * - In progressive & compressed case plane count is 4 then plane_info[0] will
1631 * contain info about Y plane and plane_info[1] will contain info about UV plane.
1632 * Remaining two plane (plane_info[2] & plane_info[3]) contain info about the
1633 * Y_Meta_Plane and UV_Meta_Plane.
1634 * - In interlaced & compressed case plane count is 8 then plane_info[0], plane_info[1],
1635 * plane_info[4] & plane_info[5] will contain info about Y_plane, UV_plane, Y_plane
1636 * & UV_plane. Remaining plane will contain info about the meta planes. As in this case
1637 * this API is called twice through GetYUVPlaneInfo() with address of plane_info[0] &
1638 * plane_info[4], so this will calculate the information accordingly and will fill the
1639 * ycbcr structure with interlaced plane info only.
1640 */
1641 ycbcr->cb = reinterpret_cast<void *>(base + plane_info[1].offset);
1642 ycbcr->cr = reinterpret_cast<void *>(base + plane_info[1].offset + 1);
1643 ycbcr->cstride = plane_info[1].stride_bytes;
1644 ycbcr->chroma_step = plane_info[1].step;
1645 } else if (plane_count == 3) {
1646 /* For YUV planar :-
1647 * Plane size is 3 and plane_info[0], plane_info[1], plane_info[2] will
1648 * contain info about y_plane, cb_plane and cr_plane accordingly.
1649 */
1650 ycbcr->cb = reinterpret_cast<void *>(base + plane_info[1].offset);
1651 ycbcr->cr = reinterpret_cast<void *>(base + plane_info[2].offset);
1652 ycbcr->cstride = plane_info[1].stride_bytes;
1653 ycbcr->chroma_step = plane_info[1].step;
1654 }
1655}
1656
Ashish Kumar8a1b56e2019-11-15 18:15:22 +05301657bool HasAlphaComponent(int32_t format) {
1658 switch (format) {
1659 case HAL_PIXEL_FORMAT_RGBA_8888:
1660 case HAL_PIXEL_FORMAT_BGRA_8888:
1661 case HAL_PIXEL_FORMAT_RGBA_5551:
1662 case HAL_PIXEL_FORMAT_RGBA_4444:
1663 case HAL_PIXEL_FORMAT_RGBA_1010102:
1664 case HAL_PIXEL_FORMAT_ARGB_2101010:
1665 case HAL_PIXEL_FORMAT_BGRA_1010102:
1666 case HAL_PIXEL_FORMAT_ABGR_2101010:
1667 case HAL_PIXEL_FORMAT_RGBA_FP16:
1668 return true;
1669 default:
1670 return false;
1671 }
1672}
1673
1674void GetRGBPlaneInfo(const BufferInfo &info, int32_t format, int32_t width, int32_t height,
1675 int32_t /* flags */, int *plane_count, PlaneLayoutInfo *plane_info) {
1676 uint64_t usage = info.usage;
1677 *plane_count = 1;
1678 uint32_t bpp = 0;
1679 if (IsUncompressedRGBFormat(format)) {
1680 bpp = GetBppForUncompressedRGB(format);
1681 }
1682 plane_info->component =
1683 (PlaneComponent)(PLANE_COMPONENT_R | PLANE_COMPONENT_G | PLANE_COMPONENT_B);
1684 if (HasAlphaComponent(format)) {
1685 plane_info->component = (PlaneComponent)(plane_info->component | PLANE_COMPONENT_A);
1686 }
1687 plane_info->size = GetSize(info, width, height);
1688 plane_info->step = bpp;
1689 plane_info->offset = GetRgbMetaSize(format, width, height, usage);
1690 plane_info->h_subsampling = 0;
1691 plane_info->v_subsampling = 0;
1692 plane_info->stride = width;
1693 plane_info->stride_bytes = width * plane_info->step;
1694 plane_info->scanlines = height;
1695}
1696
Tharaga Balachandran74ab1112020-01-08 17:17:56 -05001697// TODO(tbalacha): tile vs ubwc -- may need to find a diff way to differentiate
1698void GetDRMFormat(uint32_t format, uint32_t flags, uint32_t *drm_format,
1699 uint64_t *drm_format_modifier) {
1700 bool compressed = (flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) ? true : false;
1701 switch (format) {
1702 case HAL_PIXEL_FORMAT_RGBA_8888:
1703 *drm_format = DRM_FORMAT_ABGR8888;
1704 break;
1705 case HAL_PIXEL_FORMAT_RGBA_5551:
1706 *drm_format = DRM_FORMAT_ABGR1555;
1707 break;
1708 case HAL_PIXEL_FORMAT_RGBA_4444:
1709 *drm_format = DRM_FORMAT_ABGR4444;
1710 break;
1711 case HAL_PIXEL_FORMAT_BGRA_8888:
1712 *drm_format = DRM_FORMAT_ARGB8888;
1713 break;
1714 case HAL_PIXEL_FORMAT_RGBX_8888:
1715 *drm_format = DRM_FORMAT_XBGR8888;
1716 if (compressed)
1717 *drm_format_modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
1718 break;
1719 case HAL_PIXEL_FORMAT_BGRX_8888:
1720 *drm_format = DRM_FORMAT_XRGB8888;
1721 break;
1722 case HAL_PIXEL_FORMAT_RGB_888:
1723 *drm_format = DRM_FORMAT_BGR888;
1724 break;
1725 case HAL_PIXEL_FORMAT_RGB_565:
1726 *drm_format = DRM_FORMAT_BGR565;
1727 break;
1728 case HAL_PIXEL_FORMAT_BGR_565:
1729 *drm_format = DRM_FORMAT_BGR565;
1730 if (compressed)
1731 *drm_format_modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
1732 break;
1733 case HAL_PIXEL_FORMAT_RGBA_1010102:
1734 *drm_format = DRM_FORMAT_ABGR2101010;
1735 if (compressed)
1736 *drm_format_modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
1737 break;
1738 case HAL_PIXEL_FORMAT_ARGB_2101010:
1739 *drm_format = DRM_FORMAT_BGRA1010102;
1740 break;
1741 case HAL_PIXEL_FORMAT_RGBX_1010102:
1742 *drm_format = DRM_FORMAT_XBGR2101010;
1743 if (compressed)
1744 *drm_format_modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
1745 break;
1746 case HAL_PIXEL_FORMAT_XRGB_2101010:
1747 *drm_format = DRM_FORMAT_BGRX1010102;
1748 break;
1749 case HAL_PIXEL_FORMAT_BGRA_1010102:
1750 *drm_format = DRM_FORMAT_ARGB2101010;
1751 break;
1752 case HAL_PIXEL_FORMAT_ABGR_2101010:
1753 *drm_format = DRM_FORMAT_RGBA1010102;
1754 break;
1755 case HAL_PIXEL_FORMAT_BGRX_1010102:
1756 *drm_format = DRM_FORMAT_XRGB2101010;
1757 break;
1758 case HAL_PIXEL_FORMAT_XBGR_2101010:
1759 *drm_format = DRM_FORMAT_RGBX1010102;
1760 break;
1761 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1762 *drm_format = DRM_FORMAT_NV12;
1763 break;
1764 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1765 *drm_format = DRM_FORMAT_NV12;
1766 if (compressed) {
1767 *drm_format_modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
1768 } else {
1769 *drm_format_modifier = DRM_FORMAT_MOD_QCOM_TILE;
1770 }
1771 break;
1772 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1773 *drm_format = DRM_FORMAT_NV21;
1774 break;
1775 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
1776 *drm_format = DRM_FORMAT_NV21;
1777 break;
1778 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
1779 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
1780 *drm_format = DRM_FORMAT_NV12;
1781 *drm_format_modifier = DRM_FORMAT_MOD_QCOM_DX;
1782 break;
1783 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
1784 *drm_format = DRM_FORMAT_NV12;
1785 if (compressed) {
1786 *drm_format_modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED | DRM_FORMAT_MOD_QCOM_DX;
1787 } else {
1788 *drm_format_modifier = DRM_FORMAT_MOD_QCOM_TILE | DRM_FORMAT_MOD_QCOM_DX;
1789 }
1790 break;
1791 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1792 *drm_format = DRM_FORMAT_NV12;
1793 if (compressed) {
1794 *drm_format_modifier =
1795 DRM_FORMAT_MOD_QCOM_COMPRESSED | DRM_FORMAT_MOD_QCOM_DX | DRM_FORMAT_MOD_QCOM_TIGHT;
1796 } else {
1797 *drm_format_modifier =
1798 DRM_FORMAT_MOD_QCOM_TILE | DRM_FORMAT_MOD_QCOM_DX | DRM_FORMAT_MOD_QCOM_TIGHT;
1799 }
1800 break;
1801 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
1802 *drm_format = DRM_FORMAT_NV16;
1803 break;
1804 /*
1805 TODO: No HAL_PIXEL_FORMAT equivalent?
1806 case kFormatYCrCb422H2V1SemiPlanar:
1807 *drm_format = DRM_FORMAT_NV61;
1808 break;*/
1809 case HAL_PIXEL_FORMAT_YV12:
1810 *drm_format = DRM_FORMAT_YVU420;
1811 break;
1812 default:
1813 ALOGE("Unsupported format %d", format);
1814 }
1815}
1816
Naseer Ahmede36f2242017-12-01 15:33:56 -05001817} // namespace gralloc