blob: 3dc91d5b91623a9b0801693fc443a8fc3b3a5b85 [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;
521 case HAL_PIXEL_FORMAT_RAW16:
522 case HAL_PIXEL_FORMAT_Y16:
523 c_size = width * height;
524 c_height = height;
525 break;
526 case HAL_PIXEL_FORMAT_RAW10:
527 c_size = 0;
528 break;
529 case HAL_PIXEL_FORMAT_RAW8:
530 case HAL_PIXEL_FORMAT_Y8:
531 c_size = 0;
532 break;
533 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
534 c_size = (width * height) + 1;
535 c_height = height;
536 break;
537 default:
538 break;
539 }
540
541 yOffset = 0;
542 cOffset = y_size;
543
544 plane_info[0].component = (PlaneComponent)PLANE_COMPONENT_Y;
545 plane_info[0].offset = (uint32_t)yOffset;
546 plane_info[0].step = 1;
547 plane_info[0].stride = static_cast<int32_t>(UINT(width));
548 plane_info[0].stride_bytes = static_cast<int32_t>(y_stride);
549 plane_info[0].scanlines = static_cast<int32_t>(y_height);
550 plane_info[0].size = static_cast<uint32_t>(y_size);
551
552 plane_info[1].component = (PlaneComponent)(PLANE_COMPONENT_Cb | PLANE_COMPONENT_Cr);
553 plane_info[1].offset = (uint32_t)cOffset;
554 plane_info[1].step = 2 * bpp;
555 plane_info[1].stride = static_cast<int32_t>(UINT(width));
556 plane_info[1].stride_bytes = static_cast<int32_t>(c_stride);
557 plane_info[1].scanlines = static_cast<int32_t>(c_height);
558 plane_info[1].size = static_cast<uint32_t>(c_size);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700559}
560
Naseer Ahmed7aff2612017-10-05 20:39:05 -0400561int GetYUVPlaneInfo(const private_handle_t *hnd, struct android_ycbcr ycbcr[2]) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700562 int err = 0;
563 uint32_t width = UINT(hnd->width);
564 uint32_t height = UINT(hnd->height);
565 int format = hnd->format;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500566 uint64_t usage = hnd->usage;
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530567 int32_t interlaced = 0;
568 int plane_count = 0;
569 int unaligned_width = INT(hnd->unaligned_width);
570 int unaligned_height = INT(hnd->unaligned_height);
571 BufferInfo info(unaligned_width, unaligned_height, format, usage);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700572
573 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700574
575 // Check if UBWC buffer has been rendered in linear format.
Ramakant Singhc7d07792017-07-26 15:36:33 +0530576 int linear_format = 0;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500577 if (getMetaData(const_cast<private_handle_t *>(hnd), GET_LINEAR_FORMAT, &linear_format) == 0) {
578 format = INT(linear_format);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700579 }
580
581 // Check metadata if the geometry has been updated.
Ramakant Singhc7d07792017-07-26 15:36:33 +0530582 BufferDim_t buffer_dim;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500583 if (getMetaData(const_cast<private_handle_t *>(hnd), GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
584 BufferInfo info(buffer_dim.sliceWidth, buffer_dim.sliceHeight, format, usage);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700585 GetAlignedWidthAndHeight(info, &width, &height);
586 }
587
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700588 // Check metadata for interlaced content.
Ramakant Singhc7d07792017-07-26 15:36:33 +0530589 int interlace_flag = 0;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500590 if (getMetaData(const_cast<private_handle_t *>(hnd), GET_PP_PARAM_INTERLACED, &interlace_flag) ==
591 0) {
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530592 if (interlace_flag) {
593 interlaced = LAYOUT_INTERLACED_FLAG;
594 }
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700595 }
596
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530597 PlaneLayoutInfo plane_info[8] = {};
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700598 // Get the chroma offsets from the handle width/height. We take advantage
599 // of the fact the width _is_ the stride
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530600 err = GetYUVPlaneInfo(info, format, width, height, interlaced, &plane_count, plane_info);
601 if (err == 0) {
602 if (interlaced && format == HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC) {
603 CopyPlaneLayoutInfotoAndroidYcbcr(hnd->base, plane_count, &plane_info[0], &ycbcr[0]);
604 unsigned int uv_stride, uv_height, uv_size;
605 unsigned int alignment = 4096;
606 uint64_t field_base;
607 height = (height + 1) >> 1;
608 uv_stride = VENUS_UV_STRIDE(COLOR_FMT_NV12_UBWC, INT(width));
609 uv_height = VENUS_UV_SCANLINES(COLOR_FMT_NV12_UBWC, INT(height));
610 uv_size = ALIGN((uv_stride * uv_height), alignment);
611 field_base = hnd->base + plane_info[1].offset + uv_size;
612 memset(ycbcr[1].reserved, 0, sizeof(ycbcr[1].reserved));
613 CopyPlaneLayoutInfotoAndroidYcbcr(field_base, plane_count, &plane_info[4], &ycbcr[1]);
614 } else {
615 CopyPlaneLayoutInfotoAndroidYcbcr(hnd->base, plane_count, plane_info, ycbcr);
616 switch (format) {
617 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
618 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
619 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
620 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
621 case HAL_PIXEL_FORMAT_NV21_ZSL:
622 case HAL_PIXEL_FORMAT_RAW16:
623 case HAL_PIXEL_FORMAT_Y16:
624 case HAL_PIXEL_FORMAT_RAW10:
625 case HAL_PIXEL_FORMAT_RAW8:
626 case HAL_PIXEL_FORMAT_Y8:
627 std::swap(ycbcr->cb, ycbcr->cr);
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700628 }
Ashish Kumarb9392ff2019-03-27 18:30:39 +0530629 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700630 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700631 return err;
632}
633
634// Explicitly defined UBWC formats
635bool IsUBwcFormat(int format) {
636 switch (format) {
637 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
638 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
639 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
640 return true;
641 default:
642 return false;
643 }
644}
645
646bool IsUBwcSupported(int format) {
647 // Existing HAL formats with UBWC support
648 switch (format) {
649 case HAL_PIXEL_FORMAT_BGR_565:
650 case HAL_PIXEL_FORMAT_RGBA_8888:
651 case HAL_PIXEL_FORMAT_RGBX_8888:
652 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
653 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
654 case HAL_PIXEL_FORMAT_RGBA_1010102:
655 case HAL_PIXEL_FORMAT_RGBX_1010102:
Saurabh Dubey11df8e12018-07-25 19:16:25 +0530656 case HAL_PIXEL_FORMAT_DEPTH_16:
657 case HAL_PIXEL_FORMAT_DEPTH_24:
658 case HAL_PIXEL_FORMAT_DEPTH_24_STENCIL_8:
659 case HAL_PIXEL_FORMAT_DEPTH_32F:
660 case HAL_PIXEL_FORMAT_STENCIL_8:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700661 return true;
662 default:
663 break;
664 }
665
666 return false;
667}
668
Saurabh Dubey48b757d2018-05-24 19:13:53 +0530669bool IsUBwcPISupported(int format, uint64_t usage) {
670 if (usage & BufferUsage::COMPOSER_OVERLAY || !(usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC_PI)) {
671 return false;
672 }
673
674 // As of now only two formats
675 switch (format) {
676 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
677 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC: {
678 if ((usage & BufferUsage::GPU_TEXTURE) || (usage & BufferUsage::GPU_RENDER_TARGET)) {
679 if (AdrenoMemInfo::GetInstance()) {
680 return AdrenoMemInfo::GetInstance()->IsPISupportedByGPU(format, usage);
681 }
682 } else {
683 return true;
684 }
685 }
686 }
687
688 return false;
689}
690
Naseer Ahmede36f2242017-12-01 15:33:56 -0500691bool IsUBwcEnabled(int format, uint64_t usage) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700692 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
693 if (IsUBwcFormat(format)) {
694 return true;
695 }
696
697 // Allow UBWC, if an OpenGL client sets UBWC usage flag and GPU plus MDP
698 // support the format. OR if a non-OpenGL client like Rotator, sets UBWC
699 // usage flag and MDP supports the format.
Naseer Ahmed2023cf32019-01-28 19:02:40 -0500700 if (((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) ||
701 (usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC_PI) ||
702 (usage & BufferUsage::COMPOSER_CLIENT_TARGET))
Saurabh Dubey48b757d2018-05-24 19:13:53 +0530703 && IsUBwcSupported(format)) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700704 bool enable = true;
705 // Query GPU for UBWC only if buffer is intended to be used by GPU.
Naseer Ahmede36f2242017-12-01 15:33:56 -0500706 if ((usage & BufferUsage::GPU_TEXTURE) || (usage & BufferUsage::GPU_RENDER_TARGET)) {
Pramodh Kumar Mukunda927729d2017-06-15 16:18:51 +0530707 if (AdrenoMemInfo::GetInstance()) {
708 enable = AdrenoMemInfo::GetInstance()->IsUBWCSupportedByGPU(format);
709 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700710 }
711
712 // Allow UBWC, only if CPU usage flags are not set
Naseer Ahmede36f2242017-12-01 15:33:56 -0500713 if (enable && !(CpuCanAccess(usage))) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700714 return true;
715 }
716 }
717
718 return false;
719}
720
721void GetYuvUBwcWidthAndHeight(int width, int height, int format, unsigned int *aligned_w,
722 unsigned int *aligned_h) {
723 switch (format) {
724 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
725 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
726 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
727 *aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
728 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
729 break;
730 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
731 // The macro returns the stride which is 4/3 times the width, hence * 3/4
732 *aligned_w = (VENUS_Y_STRIDE(COLOR_FMT_NV12_BPP10_UBWC, width) * 3) / 4;
733 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_BPP10_UBWC, height);
734 break;
735 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
736 // The macro returns the stride which is 2 times the width, hence / 2
737 *aligned_w = (VENUS_Y_STRIDE(COLOR_FMT_P010_UBWC, width) / 2);
738 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_P010_UBWC, height);
739 break;
740 default:
741 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
742 *aligned_w = 0;
743 *aligned_h = 0;
744 break;
745 }
746}
747
748void GetRgbUBwcBlockSize(uint32_t bpp, int *block_width, int *block_height) {
749 *block_width = 0;
750 *block_height = 0;
751
752 switch (bpp) {
753 case 2:
754 case 4:
755 *block_width = 16;
756 *block_height = 4;
757 break;
758 case 8:
759 *block_width = 8;
760 *block_height = 4;
761 break;
762 case 16:
763 *block_width = 4;
764 *block_height = 4;
765 break;
766 default:
767 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
768 break;
769 }
770}
771
772unsigned int GetRgbUBwcMetaBufferSize(int width, int height, uint32_t bpp) {
773 unsigned int size = 0;
774 int meta_width, meta_height;
775 int block_width, block_height;
776
777 GetRgbUBwcBlockSize(bpp, &block_width, &block_height);
778 if (!block_width || !block_height) {
779 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
780 return size;
781 }
782
783 // Align meta buffer height to 16 blocks
784 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
785
786 // Align meta buffer width to 64 blocks
787 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
788
789 // Align meta buffer size to 4K
790 size = (unsigned int)ALIGN((meta_width * meta_height), 4096);
791
792 return size;
793}
794
795unsigned int GetUBwcSize(int width, int height, int format, unsigned int alignedw,
796 unsigned int alignedh) {
797 unsigned int size = 0;
798 uint32_t bpp = 0;
799 switch (format) {
800 case HAL_PIXEL_FORMAT_BGR_565:
801 case HAL_PIXEL_FORMAT_RGBA_8888:
802 case HAL_PIXEL_FORMAT_RGBX_8888:
803 case HAL_PIXEL_FORMAT_RGBA_1010102:
804 case HAL_PIXEL_FORMAT_RGBX_1010102:
805 bpp = GetBppForUncompressedRGB(format);
806 size = alignedw * alignedh * bpp;
807 size += GetRgbUBwcMetaBufferSize(width, height, bpp);
808 break;
809 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
810 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
811 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
812 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
813 break;
814 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
815 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_BPP10_UBWC, width, height);
816 break;
817 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
818 size = VENUS_BUFFER_SIZE(COLOR_FMT_P010_UBWC, width, height);
819 break;
820 default:
821 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
822 break;
823 }
824
825 return size;
826}
827
Ashish Kumar8a1b56e2019-11-15 18:15:22 +0530828unsigned int GetRgbMetaSize(int format, uint32_t width, uint32_t height, uint64_t usage) {
829 unsigned int meta_size = 0;
830 if (!IsUBwcEnabled(format, usage)) {
831 return meta_size;
832 }
833 uint32_t bpp = GetBppForUncompressedRGB(format);
834 switch (format) {
835 case HAL_PIXEL_FORMAT_BGR_565:
836 case HAL_PIXEL_FORMAT_RGBA_8888:
837 case HAL_PIXEL_FORMAT_RGBX_8888:
838 case HAL_PIXEL_FORMAT_RGBA_1010102:
839 case HAL_PIXEL_FORMAT_RGBX_1010102:
840 case HAL_PIXEL_FORMAT_RGBA_FP16:
841 meta_size = GetRgbUBwcMetaBufferSize(width, height, bpp);
842 break;
843 default:
844 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, format);
845 break;
846 }
847 return meta_size;
848}
849
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700850int GetRgbDataAddress(private_handle_t *hnd, void **rgb_data) {
851 int err = 0;
852
853 // This api is for RGB* formats
Naseer Ahmede36f2242017-12-01 15:33:56 -0500854 if (!IsUncompressedRGBFormat(hnd->format)) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700855 return -EINVAL;
856 }
857
858 // linear buffer, nothing to do further
859 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
860 *rgb_data = reinterpret_cast<void *>(hnd->base);
861 return err;
862 }
Ashish Kumar8a1b56e2019-11-15 18:15:22 +0530863 unsigned int meta_size = GetRgbMetaSize(hnd->format, hnd->width, hnd->height, hnd->usage);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700864
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700865 *rgb_data = reinterpret_cast<void *>(hnd->base + meta_size);
866
867 return err;
868}
869
Naseer Ahmede36f2242017-12-01 15:33:56 -0500870void GetCustomDimensions(private_handle_t *hnd, int *stride, int *height) {
871 BufferDim_t buffer_dim;
872 int interlaced = 0;
873
874 *stride = hnd->width;
875 *height = hnd->height;
876 if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
877 *stride = buffer_dim.sliceWidth;
878 *height = buffer_dim.sliceHeight;
879 } else if (getMetaData(hnd, GET_PP_PARAM_INTERLACED, &interlaced) == 0) {
880 if (interlaced && IsUBwcFormat(hnd->format)) {
881 unsigned int alignedw = 0, alignedh = 0;
882 // Get re-aligned height for single ubwc interlaced field and
883 // multiply by 2 to get frame height.
884 BufferInfo info(hnd->width, ((hnd->height + 1) >> 1), hnd->format);
885 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
886 *stride = static_cast<int>(alignedw);
887 *height = static_cast<int>(alignedh * 2);
888 }
889 }
890}
891
892void GetColorSpaceFromMetadata(private_handle_t *hnd, int *color_space) {
893 ColorMetaData color_metadata;
894 if (getMetaData(hnd, GET_COLOR_METADATA, &color_metadata) == 0) {
895 switch (color_metadata.colorPrimaries) {
896 case ColorPrimaries_BT709_5:
897 *color_space = HAL_CSC_ITU_R_709;
898 break;
899 case ColorPrimaries_BT601_6_525:
900 case ColorPrimaries_BT601_6_625:
901 *color_space = ((color_metadata.range) ? HAL_CSC_ITU_R_601_FR : HAL_CSC_ITU_R_601);
902 break;
903 case ColorPrimaries_BT2020:
904 *color_space = (color_metadata.range) ? HAL_CSC_ITU_R_2020_FR : HAL_CSC_ITU_R_2020;
905 break;
906 default:
907 ALOGE("Unknown Color Space = %d", color_metadata.colorPrimaries);
908 break;
909 }
910 } else if (getMetaData(hnd, GET_COLOR_SPACE, color_space) != 0) {
911 *color_space = 0;
912 }
913}
914
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700915void GetAlignedWidthAndHeight(const BufferInfo &info, unsigned int *alignedw,
916 unsigned int *alignedh) {
917 int width = info.width;
918 int height = info.height;
919 int format = info.format;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500920 uint64_t usage = info.usage;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700921
922 // Currently surface padding is only computed for RGB* surfaces.
Naseer Ahmede36f2242017-12-01 15:33:56 -0500923 bool ubwc_enabled = IsUBwcEnabled(format, usage);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700924 int tile = ubwc_enabled;
925
926 if (IsUncompressedRGBFormat(format)) {
Pramodh Kumar Mukunda927729d2017-06-15 16:18:51 +0530927 if (AdrenoMemInfo::GetInstance()) {
928 AdrenoMemInfo::GetInstance()->AlignUnCompressedRGB(width, height, format, tile, alignedw,
929 alignedh);
930 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700931 return;
932 }
933
934 if (ubwc_enabled) {
935 GetYuvUBwcWidthAndHeight(width, height, format, alignedw, alignedh);
936 return;
937 }
938
939 if (IsCompressedRGBFormat(format)) {
Pramodh Kumar Mukunda927729d2017-06-15 16:18:51 +0530940 if (AdrenoMemInfo::GetInstance()) {
941 AdrenoMemInfo::GetInstance()->AlignCompressedRGB(width, height, format, alignedw, alignedh);
942 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700943 return;
944 }
945
946 int aligned_w = width;
947 int aligned_h = height;
948 unsigned int alignment = 32;
949
950 // Below should be only YUV family
951 switch (format) {
952 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
953 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
Pramodh Kumar Mukunda927729d2017-06-15 16:18:51 +0530954 if (AdrenoMemInfo::GetInstance() == nullptr) {
955 return;
956 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700957 alignment = AdrenoMemInfo::GetInstance()->GetGpuPixelAlignment();
958 aligned_w = ALIGN(width, alignment);
959 break;
960 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
961 aligned_w = ALIGN(width, alignment);
962 break;
963 case HAL_PIXEL_FORMAT_RAW16:
Anjaneya Prasad Musunurib1654142017-04-18 10:32:42 +0530964 case HAL_PIXEL_FORMAT_Y16:
965 case HAL_PIXEL_FORMAT_Y8:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700966 aligned_w = ALIGN(width, 16);
967 break;
968 case HAL_PIXEL_FORMAT_RAW12:
Naseer Ahmed1187a572017-08-14 13:16:55 -0400969 aligned_w = ALIGN(width * 12 / 8, 16);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700970 break;
971 case HAL_PIXEL_FORMAT_RAW10:
Naseer Ahmed1187a572017-08-14 13:16:55 -0400972 aligned_w = ALIGN(width * 10 / 8, 16);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700973 break;
974 case HAL_PIXEL_FORMAT_RAW8:
Naseer Ahmed1187a572017-08-14 13:16:55 -0400975 aligned_w = ALIGN(width, 16);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700976 break;
977 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
978 aligned_w = ALIGN(width, 128);
979 break;
980 case HAL_PIXEL_FORMAT_YV12:
981 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
982 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
983 case HAL_PIXEL_FORMAT_YCbCr_422_I:
984 case HAL_PIXEL_FORMAT_YCrCb_422_I:
985 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
986 aligned_w = ALIGN(width, 16);
987 break;
Mathew Joseph Karimpanala73082e2017-10-16 18:01:21 +0530988 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
989 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_P010, width) / 2);
990 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_P010, height));
991 break;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700992 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
993 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
994 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV12, width));
995 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV12, height));
996 break;
997 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
Ramakant Singh13596ae2020-01-06 20:12:34 +0530998 case HAL_PIXEL_FORMAT_NV21_ENCODEABLE:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700999 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV21, width));
1000 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV21, height));
1001 break;
1002 case HAL_PIXEL_FORMAT_BLOB:
1003 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
1004 break;
1005 case HAL_PIXEL_FORMAT_NV21_ZSL:
1006 aligned_w = ALIGN(width, 64);
1007 aligned_h = ALIGN(height, 64);
1008 break;
Rajavenu Kyatham6e8c1822018-07-31 10:27:19 +05301009 case HAL_PIXEL_FORMAT_NV12_HEIF:
1010 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV12_512, width));
1011 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV12_512, height));
1012 break;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001013 default:
1014 break;
1015 }
1016
1017 *alignedw = (unsigned int)aligned_w;
1018 *alignedh = (unsigned int)aligned_h;
1019}
1020
Naseer Ahmede36f2242017-12-01 15:33:56 -05001021int GetBufferLayout(private_handle_t *hnd, uint32_t stride[4], uint32_t offset[4],
1022 uint32_t *num_planes) {
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001023 if (!hnd || !stride || !offset || !num_planes) {
1024 return -EINVAL;
1025 }
1026
Naseer Ahmed7aff2612017-10-05 20:39:05 -04001027 struct android_ycbcr yuvPlaneInfo[2] = {};
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001028 *num_planes = 1;
1029 stride[0] = 0;
1030
1031 switch (hnd->format) {
1032 case HAL_PIXEL_FORMAT_RGB_565:
1033 case HAL_PIXEL_FORMAT_BGR_565:
1034 case HAL_PIXEL_FORMAT_RGBA_5551:
1035 case HAL_PIXEL_FORMAT_RGBA_4444:
1036 stride[0] = static_cast<uint32_t>(hnd->width * 2);
1037 break;
1038 case HAL_PIXEL_FORMAT_RGB_888:
1039 stride[0] = static_cast<uint32_t>(hnd->width * 3);
1040 break;
1041 case HAL_PIXEL_FORMAT_RGBA_8888:
1042 case HAL_PIXEL_FORMAT_BGRA_8888:
1043 case HAL_PIXEL_FORMAT_RGBX_8888:
1044 case HAL_PIXEL_FORMAT_BGRX_8888:
1045 case HAL_PIXEL_FORMAT_RGBA_1010102:
1046 case HAL_PIXEL_FORMAT_ARGB_2101010:
1047 case HAL_PIXEL_FORMAT_RGBX_1010102:
1048 case HAL_PIXEL_FORMAT_XRGB_2101010:
1049 case HAL_PIXEL_FORMAT_BGRA_1010102:
1050 case HAL_PIXEL_FORMAT_ABGR_2101010:
1051 case HAL_PIXEL_FORMAT_BGRX_1010102:
1052 case HAL_PIXEL_FORMAT_XBGR_2101010:
1053 stride[0] = static_cast<uint32_t>(hnd->width * 4);
1054 break;
1055 }
1056
1057 // Format is RGB
1058 if (stride[0]) {
1059 return 0;
1060 }
1061
1062 (*num_planes)++;
Naseer Ahmed7aff2612017-10-05 20:39:05 -04001063 int ret = GetYUVPlaneInfo(hnd, yuvPlaneInfo);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001064 if (ret < 0) {
1065 ALOGE("%s failed", __FUNCTION__);
1066 return ret;
1067 }
1068
Naseer Ahmed7aff2612017-10-05 20:39:05 -04001069 // We are only returning buffer layout for progressive or single field formats.
1070 struct android_ycbcr yuvInfo = yuvPlaneInfo[0];
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001071 stride[0] = static_cast<uint32_t>(yuvInfo.ystride);
1072 offset[0] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.y) - hnd->base);
1073 stride[1] = static_cast<uint32_t>(yuvInfo.cstride);
1074 switch (hnd->format) {
1075 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
1076 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
1077 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1078 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1079 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1080 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
1081 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1082 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
Mathew Joseph Karimpanala73082e2017-10-16 18:01:21 +05301083 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
Rajavenu Kyatham6e8c1822018-07-31 10:27:19 +05301084 case HAL_PIXEL_FORMAT_NV12_HEIF:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001085 offset[1] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cb) - hnd->base);
1086 break;
1087 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1088 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
1089 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramakant Singh13596ae2020-01-06 20:12:34 +05301090 case HAL_PIXEL_FORMAT_NV21_ENCODEABLE:
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001091 offset[1] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cr) - hnd->base);
1092 break;
1093 case HAL_PIXEL_FORMAT_YV12:
1094 offset[1] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cr) - hnd->base);
1095 stride[2] = static_cast<uint32_t>(yuvInfo.cstride);
1096 offset[2] = static_cast<uint32_t>(reinterpret_cast<uint64_t>(yuvInfo.cb) - hnd->base);
1097 (*num_planes)++;
1098 break;
Camus Wonga300db62018-09-27 16:38:50 -04001099 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
1100 *num_planes = 1;
1101 break;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -07001102 default:
1103 ALOGW("%s: Unsupported format", __FUNCTION__);
1104 ret = -EINVAL;
1105 }
1106
1107 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
1108 std::fill(offset, offset + 4, 0);
1109 }
1110
1111 return 0;
1112}
1113
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -07001114int GetGpuResourceSizeAndDimensions(const BufferInfo &info, unsigned int *size,
1115 unsigned int *alignedw, unsigned int *alignedh,
1116 GraphicsMetadata *graphics_metadata) {
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301117 GetAlignedWidthAndHeight(info, alignedw, alignedh);
1118 AdrenoMemInfo* adreno_mem_info = AdrenoMemInfo::GetInstance();
1119 graphics_metadata->size = adreno_mem_info->AdrenoGetMetadataBlobSize();
1120 uint64_t adreno_usage = info.usage;
1121 // If gralloc disables UBWC based on any of the checks,
1122 // we pass modified usage flag to adreno to convey this.
1123 int is_ubwc_enabled = IsUBwcEnabled(info.format, info.usage);
1124 if (!is_ubwc_enabled) {
1125 adreno_usage &= ~(GRALLOC_USAGE_PRIVATE_ALLOC_UBWC);
Naseer Ahmed2023cf32019-01-28 19:02:40 -05001126 } else {
1127 adreno_usage |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301128 }
1129
1130 // Call adreno api for populating metadata blob
Saurabh Dubeyb637bb32018-05-15 16:20:10 +05301131 // Layer count is for 2D/Cubemap arrays and depth is used for 3D slice
1132 // Using depth to pass layer_count here
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301133 int ret = adreno_mem_info->AdrenoInitMemoryLayout(graphics_metadata->data, info.width,
Saurabh Dubeyb637bb32018-05-15 16:20:10 +05301134 info.height, info.layer_count, /* depth */
1135 info.format, 1, is_ubwc_enabled,
1136 adreno_usage, 1);
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301137 if (ret != 0) {
1138 ALOGE("%s Graphics metadata init failed", __FUNCTION__);
1139 *size = 0;
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -07001140 return -EINVAL;
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301141 }
1142 // Call adreno api with the metadata blob to get buffer size
1143 *size = adreno_mem_info->AdrenoGetAlignedGpuBufferSize(graphics_metadata->data);
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -07001144 return 0;
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301145}
1146
Saurabh Dubey52eb7a62018-06-15 16:31:23 +05301147bool CanUseAdrenoForSize(int buffer_type, uint64_t usage) {
1148 if (buffer_type == BUFFER_TYPE_VIDEO || !GetAdrenoSizeAPIStatus()) {
1149 return false;
1150 }
1151
1152 if ((usage & BufferUsage::PROTECTED) && ((usage & BufferUsage::CAMERA_OUTPUT) ||
1153 (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY))) {
1154 return false;
1155 }
1156
1157 return true;
1158}
1159
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +05301160bool GetAdrenoSizeAPIStatus() {
1161 AdrenoMemInfo* adreno_mem_info = AdrenoMemInfo::GetInstance();
1162 if (adreno_mem_info) {
1163 return adreno_mem_info->AdrenoSizeAPIAvaliable();
1164 }
1165 return false;
1166}
1167
Saurabh Dubey48b757d2018-05-24 19:13:53 +05301168bool UseUncached(int format, uint64_t usage) {
1169 if ((usage & GRALLOC_USAGE_PRIVATE_UNCACHED) || (usage & BufferUsage::PROTECTED)) {
1170 return true;
1171 }
1172
1173 // CPU read rarely
1174 if ((usage & BufferUsage::CPU_READ_MASK) == static_cast<uint64_t>(BufferUsage::CPU_READ_RARELY)) {
1175 return true;
1176 }
1177
1178 // CPU write rarely
1179 if ((usage & BufferUsage::CPU_WRITE_MASK) ==
1180 static_cast<uint64_t>(BufferUsage::CPU_WRITE_RARELY)) {
1181 return true;
1182 }
1183
1184 if ((usage & BufferUsage::SENSOR_DIRECT_DATA) || (usage & BufferUsage::GPU_DATA_BUFFER)) {
1185 return true;
1186 }
1187
1188 if (format && IsUBwcEnabled(format, usage)) {
1189 return true;
1190 }
1191
1192 return false;
1193}
1194
1195uint64_t GetHandleFlags(int format, uint64_t usage) {
1196 uint64_t priv_flags = 0;
1197
1198 if (usage & BufferUsage::VIDEO_ENCODER) {
1199 priv_flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
1200 }
1201
1202 if (usage & BufferUsage::CAMERA_OUTPUT) {
1203 priv_flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
1204 }
1205
1206 if (usage & BufferUsage::CAMERA_INPUT) {
1207 priv_flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
1208 }
1209
1210 if (usage & BufferUsage::COMPOSER_OVERLAY) {
1211 priv_flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER;
1212 }
1213
1214 if (usage & BufferUsage::GPU_TEXTURE) {
1215 priv_flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
1216 }
1217
1218 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
1219 priv_flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
1220 }
1221
1222 if (IsUBwcEnabled(format, usage)) {
1223 if (IsUBwcPISupported(format, usage)) {
1224 priv_flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED_PI;
1225 } else {
1226 priv_flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
1227 }
1228 }
1229
1230 if (usage & (BufferUsage::CPU_READ_MASK | BufferUsage::CPU_WRITE_MASK)) {
1231 priv_flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED;
1232 }
1233
1234 if ((usage & (BufferUsage::VIDEO_ENCODER | BufferUsage::VIDEO_DECODER |
1235 BufferUsage::CAMERA_OUTPUT | BufferUsage::GPU_RENDER_TARGET))) {
1236 priv_flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER;
1237 }
1238
1239 if (!UseUncached(format, usage)) {
1240 priv_flags |= private_handle_t::PRIV_FLAGS_CACHED;
1241 }
1242
1243 return priv_flags;
1244}
1245
1246int GetImplDefinedFormat(uint64_t usage, int format) {
1247 int gr_format = format;
1248
1249 // If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
1250 // the usage bits, gralloc assigns a format.
1251 if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED ||
1252 format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
Mathew Joseph Karimpanal71517182018-08-14 16:40:34 +05301253 if ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC || usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC_PI)
1254 && format != HAL_PIXEL_FORMAT_YCbCr_420_888) {
Saurabh Dubey81aef932018-06-27 09:41:31 +05301255 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
Saurabh Dubey48b757d2018-05-24 19:13:53 +05301256 } else if (usage & BufferUsage::VIDEO_ENCODER) {
1257 if (usage & GRALLOC_USAGE_PRIVATE_VIDEO_NV21_ENCODER) {
1258 gr_format = HAL_PIXEL_FORMAT_NV21_ENCODEABLE; // NV21
Manikanta Kanamarlapudidfa32e82019-03-26 14:06:47 +05301259 } else if (usage & GRALLOC_USAGE_PRIVATE_HEIF) {
1260 gr_format = HAL_PIXEL_FORMAT_NV12_HEIF;
Mathew Joseph Karimpanal71517182018-08-14 16:40:34 +05301261 } else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
1262 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS;
Saurabh Dubey48b757d2018-05-24 19:13:53 +05301263 } else {
1264 gr_format = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; // NV12
1265 }
1266 } else if (usage & BufferUsage::CAMERA_INPUT) {
1267 if (usage & BufferUsage::CAMERA_OUTPUT) {
1268 // Assumed ZSL if both producer and consumer camera flags set
1269 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL; // NV21
1270 } else {
1271 gr_format = HAL_PIXEL_FORMAT_YCrCb_420_SP; // NV21
1272 }
1273 } else if (usage & BufferUsage::CAMERA_OUTPUT) {
1274 if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
1275 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL; // NV21
1276 } else {
1277 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS; // NV12 preview
1278 }
1279 } else if (usage & BufferUsage::COMPOSER_OVERLAY) {
1280 // XXX: If we still haven't set a format, default to RGBA8888
1281 gr_format = HAL_PIXEL_FORMAT_RGBA_8888;
1282 } else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
1283 // If no other usage flags are detected, default the
1284 // flexible YUV format to NV21_ZSL
1285 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL;
1286 }
1287 }
1288
1289 return gr_format;
1290}
1291
1292int GetCustomFormatFlags(int format, uint64_t usage,
1293 int *custom_format, uint64_t *priv_flags) {
1294 *custom_format = GetImplDefinedFormat(usage, format);
1295 *priv_flags = GetHandleFlags(*custom_format, usage);
1296
Saurabh Dubey81aef932018-06-27 09:41:31 +05301297 if (usage & GRALLOC_USAGE_PROTECTED) {
1298 *priv_flags |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
1299 }
1300
1301 *priv_flags |= private_handle_t::PRIV_FLAGS_USES_ION;
1302
Saurabh Dubey48b757d2018-05-24 19:13:53 +05301303 return 0;
1304}
1305
Saurabh Dubeyefd7f9e2018-06-15 18:00:02 +05301306int GetBufferType(int inputFormat) {
1307 return IsYuvFormat(inputFormat) ? BUFFER_TYPE_VIDEO : BUFFER_TYPE_UI;
1308}
1309
Ashish Kumarb9392ff2019-03-27 18:30:39 +05301310int GetYUVPlaneInfo(const BufferInfo &info, int32_t format, int32_t width, int32_t height,
1311 int32_t flags, int *plane_count, PlaneLayoutInfo *plane_info) {
1312 int err = 0;
1313 unsigned int y_stride, c_stride, y_height, c_height, y_size, c_size;
1314 uint64_t yOffset, cOffset, crOffset, cbOffset;
1315 int h_subsampling = 0, v_subsampling = 0;
1316 switch (format) {
1317 // Semiplanar
1318 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
1319 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
1320 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1321 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Ramakant Singh13596ae2020-01-06 20:12:34 +05301322 case HAL_PIXEL_FORMAT_NV21_ENCODEABLE:
Ashish Kumarb9392ff2019-03-27 18:30:39 +05301323 case HAL_PIXEL_FORMAT_NV12_HEIF: // Same as YCbCr_420_SP_VENUS
1324 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1325 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
1326 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
1327 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
1328 case HAL_PIXEL_FORMAT_NV21_ZSL:
1329 case HAL_PIXEL_FORMAT_RAW16:
1330 case HAL_PIXEL_FORMAT_Y16:
1331 case HAL_PIXEL_FORMAT_RAW10:
1332 case HAL_PIXEL_FORMAT_RAW8:
1333 case HAL_PIXEL_FORMAT_Y8:
1334 *plane_count = 2;
1335 GetYuvSPPlaneInfo(info, format, width, height, 1, 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_SP_VENUS_UBWC:
1344 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1345 if (flags & LAYOUT_INTERLACED_FLAG) {
1346 *plane_count = 8;
1347 GetYuvUbwcInterlacedSPPlaneInfo(width, height, plane_info);
1348 plane_info[0].step = plane_info[4].step = 1;
1349 plane_info[1].step = plane_info[5].step = 2;
1350 plane_info[0].h_subsampling = plane_info[4].h_subsampling = 0;
1351 plane_info[0].v_subsampling = plane_info[4].v_subsampling = 0;
1352 plane_info[1].h_subsampling = plane_info[5].h_subsampling = h_subsampling;
1353 plane_info[1].v_subsampling = plane_info[5].v_subsampling = v_subsampling;
1354 plane_info[2].h_subsampling = plane_info[3].h_subsampling = 0;
1355 plane_info[2].v_subsampling = plane_info[3].v_subsampling = 0;
1356 plane_info[2].step = plane_info[3].step = 0;
1357 plane_info[6].h_subsampling = plane_info[7].h_subsampling = 0;
1358 plane_info[6].v_subsampling = plane_info[7].v_subsampling = 0;
1359 plane_info[6].step = plane_info[7].step = 0;
1360 } else {
1361 *plane_count = 4;
1362 GetYuvUbwcSPPlaneInfo(width, height, COLOR_FMT_NV12_UBWC, plane_info);
1363 plane_info[0].h_subsampling = 0;
1364 plane_info[0].v_subsampling = 0;
1365 plane_info[0].step = 1;
1366 plane_info[1].h_subsampling = h_subsampling;
1367 plane_info[1].v_subsampling = v_subsampling;
1368 plane_info[1].step = 2;
1369 plane_info[2].h_subsampling = plane_info[3].h_subsampling = 0;
1370 plane_info[2].v_subsampling = plane_info[3].v_subsampling = 0;
1371 plane_info[2].step = plane_info[3].step = 0;
1372 }
1373 break;
1374
1375 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
1376 *plane_count = 2;
1377 GetYuvSPPlaneInfo(info, format, width, height, 2, plane_info);
1378 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1379 plane_info[0].h_subsampling = 0;
1380 plane_info[0].v_subsampling = 0;
1381 plane_info[1].h_subsampling = h_subsampling;
1382 plane_info[1].v_subsampling = v_subsampling;
1383 break;
1384
1385 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1386 *plane_count = 4;
1387 GetYuvUbwcSPPlaneInfo(width, height, COLOR_FMT_NV12_BPP10_UBWC, plane_info);
1388 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1389 plane_info[0].h_subsampling = 0;
1390 plane_info[0].v_subsampling = 0;
1391 plane_info[1].step = 1;
1392 plane_info[1].h_subsampling = h_subsampling;
1393 plane_info[1].v_subsampling = v_subsampling;
1394 plane_info[1].step = 3;
1395 plane_info[2].h_subsampling = plane_info[3].h_subsampling = 0;
1396 plane_info[2].v_subsampling = plane_info[3].v_subsampling = 0;
1397 plane_info[2].step = plane_info[3].step = 0;
1398 break;
1399
1400 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
1401 *plane_count = 4;
1402 GetYuvUbwcSPPlaneInfo(width, height, COLOR_FMT_P010_UBWC, plane_info);
1403 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1404 plane_info[0].h_subsampling = 0;
1405 plane_info[0].v_subsampling = 0;
1406 plane_info[1].step = 1;
1407 plane_info[1].h_subsampling = h_subsampling;
1408 plane_info[1].v_subsampling = v_subsampling;
1409 plane_info[1].step = 4;
1410 plane_info[2].h_subsampling = plane_info[3].h_subsampling = 0;
1411 plane_info[2].v_subsampling = plane_info[3].v_subsampling = 0;
1412 plane_info[2].step = plane_info[3].step = 0;
1413 break;
1414
1415 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
1416 *plane_count = 2;
1417 y_stride = VENUS_Y_STRIDE(COLOR_FMT_P010, width);
1418 c_stride = VENUS_UV_STRIDE(COLOR_FMT_P010, width);
1419 y_height = VENUS_Y_SCANLINES(COLOR_FMT_P010, height);
1420 y_size = y_stride * y_height;
1421 yOffset = 0;
1422 cOffset = y_size;
1423 c_height = VENUS_UV_SCANLINES(COLOR_FMT_P010, INT(height));
1424 c_size = c_stride * c_height;
1425 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1426
1427 plane_info[0].component = (PlaneComponent)PLANE_COMPONENT_Y;
1428 plane_info[0].offset = (uint32_t)yOffset;
1429 plane_info[0].stride = static_cast<int32_t>(UINT(width));
1430 plane_info[0].stride_bytes = static_cast<int32_t>(y_stride);
1431 plane_info[0].scanlines = static_cast<int32_t>(y_height);
1432 plane_info[0].size = static_cast<uint32_t>(y_size);
1433 plane_info[0].step = 1;
1434 plane_info[0].h_subsampling = 0;
1435 plane_info[0].v_subsampling = 0;
1436
1437 plane_info[1].component = (PlaneComponent)(PLANE_COMPONENT_Cb | PLANE_COMPONENT_Cr);
1438 plane_info[1].offset = (uint32_t)cOffset;
1439 plane_info[1].stride = static_cast<int32_t>(UINT(width));
1440 plane_info[1].stride_bytes = static_cast<int32_t>(c_stride);
1441 plane_info[1].scanlines = static_cast<int32_t>(c_height);
1442 plane_info[1].size = static_cast<uint32_t>(c_size);
1443 plane_info[1].step = 4;
1444 plane_info[1].h_subsampling = h_subsampling;
1445 plane_info[1].v_subsampling = v_subsampling;
1446 break;
1447
1448 // Planar
1449 case HAL_PIXEL_FORMAT_YV12:
1450 if ((info.width & 1) || (info.height & 1)) {
1451 ALOGE("w or h is odd for the YV12 format");
1452 err = -EINVAL;
1453 return err;
1454 }
1455 *plane_count = 3;
1456 y_stride = width;
1457 c_stride = ALIGN(width / 2, 16);
1458 y_height = UINT(height);
1459 y_size = (y_stride * y_height);
1460 height = height >> 1;
1461 c_height = UINT(height);
1462 c_size = (c_stride * c_height);
1463 yOffset = 0;
1464 crOffset = y_size;
1465 cbOffset = (y_size + c_size);
1466 GetYuvSubSamplingFactor(format, &h_subsampling, &v_subsampling);
1467
1468 plane_info[0].component = (PlaneComponent)PLANE_COMPONENT_Y;
1469 plane_info[0].offset = (uint32_t)yOffset;
1470 plane_info[0].stride = static_cast<int32_t>(UINT(width));
1471 plane_info[0].stride_bytes = static_cast<int32_t>(y_stride);
1472 plane_info[0].scanlines = static_cast<int32_t>(y_height);
1473 plane_info[0].size = static_cast<uint32_t>(y_size);
1474 plane_info[0].step = 1;
1475 plane_info[0].h_subsampling = 0;
1476 plane_info[0].v_subsampling = 0;
1477
1478 plane_info[1].component = (PlaneComponent)PLANE_COMPONENT_Cb;
1479 plane_info[1].offset = (uint32_t)cbOffset;
1480 plane_info[2].component = (PlaneComponent)PLANE_COMPONENT_Cr;
1481 plane_info[2].offset = (uint32_t)crOffset;
1482 for (int i = 1; i < 3; i++) {
1483 plane_info[i].stride = static_cast<int32_t>(UINT(width));
1484 plane_info[i].stride_bytes = static_cast<int32_t>(c_stride);
1485 plane_info[i].scanlines = static_cast<int32_t>(c_height);
1486 plane_info[i].size = static_cast<uint32_t>(c_size);
1487 plane_info[i].step = 1;
1488 plane_info[i].h_subsampling = h_subsampling;
1489 plane_info[i].v_subsampling = v_subsampling;
1490 }
1491 break;
1492 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
1493 if (info.width & 1) {
1494 ALOGE("width is odd for the YUV422_SP format");
1495 err = -EINVAL;
1496 return err;
1497 }
1498 *plane_count = 1;
1499 y_stride = width * 2;
1500 y_height = UINT(height);
1501 y_size = y_stride * y_height;
1502 yOffset = 0;
1503 plane_info[0].component = (PlaneComponent)PLANE_COMPONENT_Y;
1504 plane_info[0].offset = (uint32_t)yOffset;
1505 plane_info[0].stride = static_cast<int32_t>(UINT(width));
1506 plane_info[0].stride_bytes = static_cast<int32_t>(y_stride);
1507 plane_info[0].scanlines = static_cast<int32_t>(y_height);
1508 plane_info[0].size = static_cast<uint32_t>(y_size);
1509 plane_info[0].step = 1;
1510 plane_info[0].h_subsampling = 0;
1511 plane_info[0].v_subsampling = 0;
1512 break;
1513
1514 // Unsupported formats
1515 case HAL_PIXEL_FORMAT_YCbCr_422_I:
1516 case HAL_PIXEL_FORMAT_YCrCb_422_I:
1517 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
1518 default:
1519 *plane_count = 0;
1520 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, format);
1521 err = -EINVAL;
1522 }
1523 return err;
1524}
1525
1526void GetYuvSubSamplingFactor(int32_t format, int *h_subsampling, int *v_subsampling) {
1527 switch (format) {
1528 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
1529 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
1530 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1531 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
1532 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
1533 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1534 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1535 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
1536 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
1537 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1538 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: // Same as YCbCr_420_SP_VENUS
Ramakant Singh13596ae2020-01-06 20:12:34 +05301539 case HAL_PIXEL_FORMAT_NV21_ENCODEABLE:
Ashish Kumarb9392ff2019-03-27 18:30:39 +05301540 case HAL_PIXEL_FORMAT_NV21_ZSL:
1541 case HAL_PIXEL_FORMAT_YV12:
1542 *h_subsampling = 1;
1543 *v_subsampling = 1;
1544 break;
1545 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
1546 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
1547 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
1548 *h_subsampling = 1;
1549 *v_subsampling = 0;
1550 break;
1551 case HAL_PIXEL_FORMAT_RAW16:
1552 case HAL_PIXEL_FORMAT_Y16:
1553 case HAL_PIXEL_FORMAT_RAW12:
1554 case HAL_PIXEL_FORMAT_RAW10:
1555 case HAL_PIXEL_FORMAT_Y8:
1556 case HAL_PIXEL_FORMAT_BLOB:
1557 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
1558 case HAL_PIXEL_FORMAT_NV12_HEIF:
1559 default:
1560 *h_subsampling = 0;
1561 *v_subsampling = 0;
1562 break;
1563 }
1564}
1565
1566void CopyPlaneLayoutInfotoAndroidYcbcr(uint64_t base, int plane_count, PlaneLayoutInfo *plane_info,
1567 struct android_ycbcr *ycbcr) {
1568 ycbcr->y = reinterpret_cast<void *>(base + plane_info[0].offset);
1569 ycbcr->ystride = plane_info[0].stride_bytes;
1570 if (plane_count == 1) {
1571 ycbcr->cb = NULL;
1572 ycbcr->cr = NULL;
1573 ycbcr->cstride = 0;
1574 ycbcr->chroma_step = 0;
1575 } else if (plane_count == 2 || plane_count == 4 || plane_count == 8) {
1576 /* For YUV semiplanar :-
1577 * - In progressive & linear case plane count is 2 and plane_info[0] will
1578 * contain info about Y plane and plane_info[1] will contain info about UV plane.
1579 * - In progressive & compressed case plane count is 4 then plane_info[0] will
1580 * contain info about Y plane and plane_info[1] will contain info about UV plane.
1581 * Remaining two plane (plane_info[2] & plane_info[3]) contain info about the
1582 * Y_Meta_Plane and UV_Meta_Plane.
1583 * - In interlaced & compressed case plane count is 8 then plane_info[0], plane_info[1],
1584 * plane_info[4] & plane_info[5] will contain info about Y_plane, UV_plane, Y_plane
1585 * & UV_plane. Remaining plane will contain info about the meta planes. As in this case
1586 * this API is called twice through GetYUVPlaneInfo() with address of plane_info[0] &
1587 * plane_info[4], so this will calculate the information accordingly and will fill the
1588 * ycbcr structure with interlaced plane info only.
1589 */
1590 ycbcr->cb = reinterpret_cast<void *>(base + plane_info[1].offset);
1591 ycbcr->cr = reinterpret_cast<void *>(base + plane_info[1].offset + 1);
1592 ycbcr->cstride = plane_info[1].stride_bytes;
1593 ycbcr->chroma_step = plane_info[1].step;
1594 } else if (plane_count == 3) {
1595 /* For YUV planar :-
1596 * Plane size is 3 and plane_info[0], plane_info[1], plane_info[2] will
1597 * contain info about y_plane, cb_plane and cr_plane accordingly.
1598 */
1599 ycbcr->cb = reinterpret_cast<void *>(base + plane_info[1].offset);
1600 ycbcr->cr = reinterpret_cast<void *>(base + plane_info[2].offset);
1601 ycbcr->cstride = plane_info[1].stride_bytes;
1602 ycbcr->chroma_step = plane_info[1].step;
1603 }
1604}
1605
Ashish Kumar8a1b56e2019-11-15 18:15:22 +05301606bool HasAlphaComponent(int32_t format) {
1607 switch (format) {
1608 case HAL_PIXEL_FORMAT_RGBA_8888:
1609 case HAL_PIXEL_FORMAT_BGRA_8888:
1610 case HAL_PIXEL_FORMAT_RGBA_5551:
1611 case HAL_PIXEL_FORMAT_RGBA_4444:
1612 case HAL_PIXEL_FORMAT_RGBA_1010102:
1613 case HAL_PIXEL_FORMAT_ARGB_2101010:
1614 case HAL_PIXEL_FORMAT_BGRA_1010102:
1615 case HAL_PIXEL_FORMAT_ABGR_2101010:
1616 case HAL_PIXEL_FORMAT_RGBA_FP16:
1617 return true;
1618 default:
1619 return false;
1620 }
1621}
1622
1623void GetRGBPlaneInfo(const BufferInfo &info, int32_t format, int32_t width, int32_t height,
1624 int32_t /* flags */, int *plane_count, PlaneLayoutInfo *plane_info) {
1625 uint64_t usage = info.usage;
1626 *plane_count = 1;
1627 uint32_t bpp = 0;
1628 if (IsUncompressedRGBFormat(format)) {
1629 bpp = GetBppForUncompressedRGB(format);
1630 }
1631 plane_info->component =
1632 (PlaneComponent)(PLANE_COMPONENT_R | PLANE_COMPONENT_G | PLANE_COMPONENT_B);
1633 if (HasAlphaComponent(format)) {
1634 plane_info->component = (PlaneComponent)(plane_info->component | PLANE_COMPONENT_A);
1635 }
1636 plane_info->size = GetSize(info, width, height);
1637 plane_info->step = bpp;
1638 plane_info->offset = GetRgbMetaSize(format, width, height, usage);
1639 plane_info->h_subsampling = 0;
1640 plane_info->v_subsampling = 0;
1641 plane_info->stride = width;
1642 plane_info->stride_bytes = width * plane_info->step;
1643 plane_info->scanlines = height;
1644}
1645
Tharaga Balachandran74ab1112020-01-08 17:17:56 -05001646// TODO(tbalacha): tile vs ubwc -- may need to find a diff way to differentiate
1647void GetDRMFormat(uint32_t format, uint32_t flags, uint32_t *drm_format,
1648 uint64_t *drm_format_modifier) {
1649 bool compressed = (flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) ? true : false;
1650 switch (format) {
1651 case HAL_PIXEL_FORMAT_RGBA_8888:
1652 *drm_format = DRM_FORMAT_ABGR8888;
1653 break;
1654 case HAL_PIXEL_FORMAT_RGBA_5551:
1655 *drm_format = DRM_FORMAT_ABGR1555;
1656 break;
1657 case HAL_PIXEL_FORMAT_RGBA_4444:
1658 *drm_format = DRM_FORMAT_ABGR4444;
1659 break;
1660 case HAL_PIXEL_FORMAT_BGRA_8888:
1661 *drm_format = DRM_FORMAT_ARGB8888;
1662 break;
1663 case HAL_PIXEL_FORMAT_RGBX_8888:
1664 *drm_format = DRM_FORMAT_XBGR8888;
1665 if (compressed)
1666 *drm_format_modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
1667 break;
1668 case HAL_PIXEL_FORMAT_BGRX_8888:
1669 *drm_format = DRM_FORMAT_XRGB8888;
1670 break;
1671 case HAL_PIXEL_FORMAT_RGB_888:
1672 *drm_format = DRM_FORMAT_BGR888;
1673 break;
1674 case HAL_PIXEL_FORMAT_RGB_565:
1675 *drm_format = DRM_FORMAT_BGR565;
1676 break;
1677 case HAL_PIXEL_FORMAT_BGR_565:
1678 *drm_format = DRM_FORMAT_BGR565;
1679 if (compressed)
1680 *drm_format_modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
1681 break;
1682 case HAL_PIXEL_FORMAT_RGBA_1010102:
1683 *drm_format = DRM_FORMAT_ABGR2101010;
1684 if (compressed)
1685 *drm_format_modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
1686 break;
1687 case HAL_PIXEL_FORMAT_ARGB_2101010:
1688 *drm_format = DRM_FORMAT_BGRA1010102;
1689 break;
1690 case HAL_PIXEL_FORMAT_RGBX_1010102:
1691 *drm_format = DRM_FORMAT_XBGR2101010;
1692 if (compressed)
1693 *drm_format_modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
1694 break;
1695 case HAL_PIXEL_FORMAT_XRGB_2101010:
1696 *drm_format = DRM_FORMAT_BGRX1010102;
1697 break;
1698 case HAL_PIXEL_FORMAT_BGRA_1010102:
1699 *drm_format = DRM_FORMAT_ARGB2101010;
1700 break;
1701 case HAL_PIXEL_FORMAT_ABGR_2101010:
1702 *drm_format = DRM_FORMAT_RGBA1010102;
1703 break;
1704 case HAL_PIXEL_FORMAT_BGRX_1010102:
1705 *drm_format = DRM_FORMAT_XRGB2101010;
1706 break;
1707 case HAL_PIXEL_FORMAT_XBGR_2101010:
1708 *drm_format = DRM_FORMAT_RGBX1010102;
1709 break;
1710 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1711 *drm_format = DRM_FORMAT_NV12;
1712 break;
1713 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1714 *drm_format = DRM_FORMAT_NV12;
1715 if (compressed) {
1716 *drm_format_modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
1717 } else {
1718 *drm_format_modifier = DRM_FORMAT_MOD_QCOM_TILE;
1719 }
1720 break;
1721 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1722 *drm_format = DRM_FORMAT_NV21;
1723 break;
1724 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
1725 *drm_format = DRM_FORMAT_NV21;
1726 break;
1727 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
1728 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
1729 *drm_format = DRM_FORMAT_NV12;
1730 *drm_format_modifier = DRM_FORMAT_MOD_QCOM_DX;
1731 break;
1732 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
1733 *drm_format = DRM_FORMAT_NV12;
1734 if (compressed) {
1735 *drm_format_modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED | DRM_FORMAT_MOD_QCOM_DX;
1736 } else {
1737 *drm_format_modifier = DRM_FORMAT_MOD_QCOM_TILE | DRM_FORMAT_MOD_QCOM_DX;
1738 }
1739 break;
1740 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1741 *drm_format = DRM_FORMAT_NV12;
1742 if (compressed) {
1743 *drm_format_modifier =
1744 DRM_FORMAT_MOD_QCOM_COMPRESSED | DRM_FORMAT_MOD_QCOM_DX | DRM_FORMAT_MOD_QCOM_TIGHT;
1745 } else {
1746 *drm_format_modifier =
1747 DRM_FORMAT_MOD_QCOM_TILE | DRM_FORMAT_MOD_QCOM_DX | DRM_FORMAT_MOD_QCOM_TIGHT;
1748 }
1749 break;
1750 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
1751 *drm_format = DRM_FORMAT_NV16;
1752 break;
1753 /*
1754 TODO: No HAL_PIXEL_FORMAT equivalent?
1755 case kFormatYCrCb422H2V1SemiPlanar:
1756 *drm_format = DRM_FORMAT_NV61;
1757 break;*/
1758 case HAL_PIXEL_FORMAT_YV12:
1759 *drm_format = DRM_FORMAT_YVU420;
1760 break;
1761 default:
1762 ALOGE("Unsupported format %d", format);
1763 }
1764}
1765
Naseer Ahmede36f2242017-12-01 15:33:56 -05001766} // namespace gralloc