blob: 7190fc0446ad328833f88e5b83db2644d6dd86ea [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
Saurabh Shahc5b2b702016-10-24 17:16:01 -07002 * Copyright (c) 2011 - 2017, The Linux Foundation. All rights reserved.
Iliyan Malchev202a77d2012-06-11 14:41:12 -07003
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.
Duy Truong73d36df2013-02-09 20:33:23 -080013 * * Neither the name of The Linux Foundation nor the names of its
Iliyan Malchev202a77d2012-06-11 14:41:12 -070014 * 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
30#include <cutils/log.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070031#include <fcntl.h>
Naomi Luis01f5c8e2013-02-11 12:46:24 -080032#include <dlfcn.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070033#include "gralloc_priv.h"
34#include "alloc_controller.h"
35#include "memalloc.h"
36#include "ionalloc.h"
Iliyan Malchev202a77d2012-06-11 14:41:12 -070037#include "gr.h"
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -070038#include "qd_utils.h"
Kaushik Kanetkar071aca62015-01-22 23:16:26 -070039#include <qdMetaData.h>
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -070040#include <utils/Singleton.h>
41#include <utils/Mutex.h>
Pullakavi Srinivasc6442662016-06-22 02:20:07 +053042#include <algorithm>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070043
Sushil Chauhanc6bd6d92012-12-12 12:33:01 -080044#ifdef VENUS_COLOR_FORMAT
45#include <media/msm_media_info.h>
46#else
47#define VENUS_Y_STRIDE(args...) 0
48#define VENUS_Y_SCANLINES(args...) 0
49#define VENUS_BUFFER_SIZE(args...) 0
50#endif
51
Naseer Ahmed63326f42013-12-18 02:45:48 -050052#define ASTC_BLOCK_SIZE 16
Naseer Ahmed63326f42013-12-18 02:45:48 -050053
Shalaj Jain3c490412015-04-22 16:52:03 -070054#ifndef ION_FLAG_CP_PIXEL
Shalaj Jain1f9725a2015-03-04 17:53:49 -080055#define ION_FLAG_CP_PIXEL 0
Shalaj Jain1f9725a2015-03-04 17:53:49 -080056#endif
57
58#ifndef ION_FLAG_ALLOW_NON_CONTIG
59#define ION_FLAG_ALLOW_NON_CONTIG 0
60#endif
61
Sushil Chauhandfe55a22016-09-12 15:48:29 -070062#ifndef ION_FLAG_CP_CAMERA_PREVIEW
63#define ION_FLAG_CP_CAMERA_PREVIEW 0
64#endif
65
Shalaj Jain3c490412015-04-22 16:52:03 -070066#ifdef MASTER_SIDE_CP
67#define CP_HEAP_ID ION_SECURE_HEAP_ID
Arun Kumar K.R7f0b24b2015-07-05 21:20:57 -070068#define SD_HEAP_ID ION_SECURE_DISPLAY_HEAP_ID
Shalaj Jain3c490412015-04-22 16:52:03 -070069#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_CP_PIXEL)
Arun Kumar K.R7f0b24b2015-07-05 21:20:57 -070070#define ION_SD_FLAGS (ION_SECURE | ION_FLAG_CP_SEC_DISPLAY)
Sushil Chauhandfe55a22016-09-12 15:48:29 -070071#define ION_SC_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA)
72#define ION_SC_PREVIEW_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA_PREVIEW)
Shalaj Jain3c490412015-04-22 16:52:03 -070073#else // SLAVE_SIDE_CP
74#define CP_HEAP_ID ION_CP_MM_HEAP_ID
75#define SD_HEAP_ID CP_HEAP_ID
76#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_ALLOW_NON_CONTIG)
77#define ION_SD_FLAGS ION_SECURE
Sushil Chauhandfe55a22016-09-12 15:48:29 -070078#define ION_SC_FLAGS ION_SECURE
79#define ION_SC_PREVIEW_FLAGS ION_SECURE
Shalaj Jain3c490412015-04-22 16:52:03 -070080#endif
81
Iliyan Malchev202a77d2012-06-11 14:41:12 -070082using namespace gralloc;
Naseer Ahmeda87da602012-07-01 23:54:19 -070083using namespace qdutils;
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -070084using namespace android;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070085
Naomi Luisa44100c2013-02-08 12:42:03 -080086ANDROID_SINGLETON_STATIC_INSTANCE(AdrenoMemInfo);
Ramakant Singhc85ccee2016-04-01 15:25:17 +053087ANDROID_SINGLETON_STATIC_INSTANCE(MDPCapabilityInfo);
Naomi Luisa44100c2013-02-08 12:42:03 -080088
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -080089static void getYuvUBwcWidthHeight(int, int, int, int&, int&);
Sushil Chauhan65e26302015-01-14 10:48:57 -080090static unsigned int getUBwcSize(int, int, int, const int, const int);
91
Iliyan Malchev202a77d2012-06-11 14:41:12 -070092//Common functions
Iliyan Malchev202a77d2012-06-11 14:41:12 -070093
Saurabh Shah1adcafe2014-12-19 10:05:41 -080094/* The default policy is to return cached buffers unless the client explicity
95 * sets the PRIVATE_UNCACHED flag or indicates that the buffer will be rarely
96 * read or written in software. Any combination with a _RARELY_ flag will be
97 * treated as uncached. */
98static bool useUncached(const int& usage) {
Naseer Ahmedde758fd2016-05-03 15:10:23 -040099 if ((usage & GRALLOC_USAGE_PROTECTED) or
100 (usage & GRALLOC_USAGE_PRIVATE_UNCACHED) or
101 ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY) or
102 ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY))
Saurabh Shah1adcafe2014-12-19 10:05:41 -0800103 return true;
104
105 return false;
106}
107
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530108//------------- MDPCapabilityInfo-----------------------//
109MDPCapabilityInfo :: MDPCapabilityInfo() {
Sushil Chauhan01361412016-04-25 16:36:18 -0700110 qdutils::querySDEInfo(HAS_UBWC, &isUBwcSupported);
Jeykumar Sankaran9bc1a782015-12-14 18:36:27 -0800111 qdutils::querySDEInfo(HAS_WB_UBWC, &isWBUBWCSupported);
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530112}
113
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530114//------------- AdrenoMemInfo-----------------------//
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800115AdrenoMemInfo::AdrenoMemInfo()
116{
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800117 LINK_adreno_compute_aligned_width_and_height = NULL;
118 LINK_adreno_compute_padding = NULL;
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800119 LINK_adreno_compute_compressedfmt_aligned_width_and_height = NULL;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800120 LINK_adreno_isUBWCSupportedByGpu = NULL;
Sushil Chauhan521ce352015-08-28 11:33:30 -0700121 LINK_adreno_get_gpu_pixel_alignment = NULL;
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800122
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800123 libadreno_utils = ::dlopen("libadreno_utils.so", RTLD_NOW);
124 if (libadreno_utils) {
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800125 *(void **)&LINK_adreno_compute_aligned_width_and_height =
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700126 ::dlsym(libadreno_utils, "compute_aligned_width_and_height");
127 *(void **)&LINK_adreno_compute_padding =
128 ::dlsym(libadreno_utils, "compute_surface_padding");
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800129 *(void **)&LINK_adreno_compute_compressedfmt_aligned_width_and_height =
130 ::dlsym(libadreno_utils,
131 "compute_compressedfmt_aligned_width_and_height");
Sushil Chauhan082acd62015-01-14 16:49:29 -0800132 *(void **)&LINK_adreno_isUBWCSupportedByGpu =
133 ::dlsym(libadreno_utils, "isUBWCSupportedByGpu");
Sushil Chauhan521ce352015-08-28 11:33:30 -0700134 *(void **)&LINK_adreno_get_gpu_pixel_alignment =
135 ::dlsym(libadreno_utils, "get_gpu_pixel_alignment");
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800136 }
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700137
138 // Check if the overriding property debug.gralloc.gfx_ubwc_disable
139 // that disables UBWC allocations for the graphics stack is set
140 gfx_ubwc_disable = 0;
141 char property[PROPERTY_VALUE_MAX];
142 property_get("debug.gralloc.gfx_ubwc_disable", property, "0");
143 if(!(strncmp(property, "1", PROPERTY_VALUE_MAX)) ||
144 !(strncmp(property, "true", PROPERTY_VALUE_MAX))) {
145 gfx_ubwc_disable = 1;
146 }
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800147}
148
149AdrenoMemInfo::~AdrenoMemInfo()
150{
151 if (libadreno_utils) {
152 ::dlclose(libadreno_utils);
153 }
154}
155
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700156void AdrenoMemInfo::getAlignedWidthAndHeight(const private_handle_t *hnd, int& aligned_w,
157 int& aligned_h) {
158 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
159 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
160 int w = metadata->bufferDim.sliceWidth;
161 int h = metadata->bufferDim.sliceHeight;
162 int f = hnd->format;
163 int usage = 0;
164
165 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
166 usage = GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
167 }
168
169 getAlignedWidthAndHeight(w, h, f, usage, aligned_w, aligned_h);
170 } else {
171 aligned_w = hnd->width;
172 aligned_h = hnd->height;
173 }
174
175}
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700176
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700177void AdrenoMemInfo::getUnalignedWidthAndHeight(const private_handle_t *hnd, int& unaligned_w,
178 int& unaligned_h) {
179 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
180 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
181 unaligned_w = metadata->bufferDim.sliceWidth;
182 unaligned_h = metadata->bufferDim.sliceHeight;
183 } else {
184 unaligned_w = hnd->unaligned_width;
185 unaligned_h = hnd->unaligned_height;
186 }
187}
188
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700189bool isUncompressedRgbFormat(int format)
190{
191 bool is_rgb_format = false;
192
193 switch (format)
194 {
195 case HAL_PIXEL_FORMAT_RGBA_8888:
196 case HAL_PIXEL_FORMAT_RGBX_8888:
197 case HAL_PIXEL_FORMAT_RGB_888:
198 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600199 case HAL_PIXEL_FORMAT_BGR_565:
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700200 case HAL_PIXEL_FORMAT_BGRA_8888:
201 case HAL_PIXEL_FORMAT_RGBA_5551:
202 case HAL_PIXEL_FORMAT_RGBA_4444:
203 case HAL_PIXEL_FORMAT_R_8:
204 case HAL_PIXEL_FORMAT_RG_88:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800205 case HAL_PIXEL_FORMAT_BGRX_8888:
206 case HAL_PIXEL_FORMAT_RGBA_1010102:
207 case HAL_PIXEL_FORMAT_ARGB_2101010:
208 case HAL_PIXEL_FORMAT_RGBX_1010102:
209 case HAL_PIXEL_FORMAT_XRGB_2101010:
210 case HAL_PIXEL_FORMAT_BGRA_1010102:
211 case HAL_PIXEL_FORMAT_ABGR_2101010:
212 case HAL_PIXEL_FORMAT_BGRX_1010102:
213 case HAL_PIXEL_FORMAT_XBGR_2101010: // Intentional fallthrough
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700214 is_rgb_format = true;
215 break;
216 default:
217 break;
218 }
219
220 return is_rgb_format;
221}
222
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800223void AdrenoMemInfo::getAlignedWidthAndHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800224 int usage, int& aligned_w, int& aligned_h)
Naomi Luisa44100c2013-02-08 12:42:03 -0800225{
Sushil Chauhane61fac52015-04-30 17:14:37 -0700226 bool ubwc_enabled = isUBwcEnabled(format, usage);
227
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800228 // Currently surface padding is only computed for RGB* surfaces.
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700229 if (isUncompressedRgbFormat(format) == true) {
Saurabh Shahc5b2b702016-10-24 17:16:01 -0700230 int tileEnabled = ubwc_enabled;
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700231 getGpuAlignedWidthHeight(width, height, format, tileEnabled, aligned_w, aligned_h);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800232 } else if (ubwc_enabled) {
233 getYuvUBwcWidthHeight(width, height, format, aligned_w, aligned_h);
234 } else {
235 aligned_w = width;
236 aligned_h = height;
237 int alignment = 32;
238 switch (format)
239 {
240 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
241 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
242 if (LINK_adreno_get_gpu_pixel_alignment) {
243 alignment = LINK_adreno_get_gpu_pixel_alignment();
244 }
245 aligned_w = ALIGN(width, alignment);
246 break;
247 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
248 aligned_w = ALIGN(width, alignment);
249 break;
250 case HAL_PIXEL_FORMAT_RAW16:
251 aligned_w = ALIGN(width, 16);
252 break;
253 case HAL_PIXEL_FORMAT_RAW10:
254 aligned_w = ALIGN(width * 10 / 8, 8);
255 break;
256 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
257 aligned_w = ALIGN(width, 128);
258 break;
259 case HAL_PIXEL_FORMAT_YV12:
260 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
261 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
262 case HAL_PIXEL_FORMAT_YCbCr_422_I:
263 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Ramkumar Radhakrishnan3083fe92016-12-15 18:05:40 -0800264 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800265 aligned_w = ALIGN(width, 16);
266 break;
267 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
268 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
269 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
270 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
271 break;
272 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
273 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV21, width);
274 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV21, height);
275 break;
276 case HAL_PIXEL_FORMAT_BLOB:
Naseer Ahmed6c6c58a2016-05-03 16:27:17 -0400277 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800278 break;
279 case HAL_PIXEL_FORMAT_NV21_ZSL:
280 aligned_w = ALIGN(width, 64);
281 aligned_h = ALIGN(height, 64);
282 break;
283 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
284 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
285 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
286 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
287 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
288 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
289 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
290 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
291 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
292 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
293 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
294 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
295 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
296 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
297 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
298 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
299 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
300 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
301 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
302 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
303 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
304 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
305 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
306 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
307 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
308 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
309 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
310 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
311 if(LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
312 int bytesPerPixel = 0;
313 int raster_mode = 0; //Adreno unknown raster mode.
314 int padding_threshold = 512; //Threshold for padding
315 //surfaces.
Sushil Chauhan65e26302015-01-14 10:48:57 -0800316
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800317 LINK_adreno_compute_compressedfmt_aligned_width_and_height(
318 width, height, format, 0,raster_mode, padding_threshold,
319 &aligned_w, &aligned_h, &bytesPerPixel);
320 } else {
321 ALOGW("%s: Warning!! Symbols" \
322 " compute_compressedfmt_aligned_width_and_height" \
323 " not found", __FUNCTION__);
324 }
325 break;
326 default: break;
327 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800328 }
329}
330
331void AdrenoMemInfo::getGpuAlignedWidthHeight(int width, int height, int format,
332 int tile_enabled, int& aligned_w, int& aligned_h)
333{
334 aligned_w = ALIGN(width, 32);
335 aligned_h = ALIGN(height, 32);
336
337 // Don't add any additional padding if debug.gralloc.map_fb_memory
338 // is enabled
339 char property[PROPERTY_VALUE_MAX];
340 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
341 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
342 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
343 return;
344 }
345
346 int bpp = 4;
347 switch(format)
348 {
349 case HAL_PIXEL_FORMAT_RGB_888:
350 bpp = 3;
351 break;
352 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600353 case HAL_PIXEL_FORMAT_BGR_565:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800354 case HAL_PIXEL_FORMAT_RGBA_5551:
355 case HAL_PIXEL_FORMAT_RGBA_4444:
356 bpp = 2;
357 break;
358 default: break;
359 }
360
361 if (libadreno_utils) {
362 int raster_mode = 0; // Adreno unknown raster mode.
363 int padding_threshold = 512; // Threshold for padding surfaces.
364 // the function below computes aligned width and aligned height
365 // based on linear or macro tile mode selected.
366 if(LINK_adreno_compute_aligned_width_and_height) {
367 LINK_adreno_compute_aligned_width_and_height(width,
368 height, bpp, tile_enabled,
369 raster_mode, padding_threshold,
370 &aligned_w, &aligned_h);
371
372 } else if(LINK_adreno_compute_padding) {
373 int surface_tile_height = 1; // Linear surface
374 aligned_w = LINK_adreno_compute_padding(width, bpp,
375 surface_tile_height, raster_mode,
376 padding_threshold);
377 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
378 __FUNCTION__);
379 } else {
380 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
381 "compute_aligned_width_and_height not found", __FUNCTION__);
382 }
383 }
384}
385
386int AdrenoMemInfo::isUBWCSupportedByGPU(int format)
387{
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700388 if (!gfx_ubwc_disable && libadreno_utils) {
Sushil Chauhan082acd62015-01-14 16:49:29 -0800389 if (LINK_adreno_isUBWCSupportedByGpu) {
390 ADRENOPIXELFORMAT gpu_format = getGpuPixelFormat(format);
391 return LINK_adreno_isUBWCSupportedByGpu(gpu_format);
392 }
393 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800394 return 0;
Naomi Luisa44100c2013-02-08 12:42:03 -0800395}
396
Sushil Chauhan082acd62015-01-14 16:49:29 -0800397ADRENOPIXELFORMAT AdrenoMemInfo::getGpuPixelFormat(int hal_format)
398{
399 switch (hal_format) {
400 case HAL_PIXEL_FORMAT_RGBA_8888:
401 return ADRENO_PIXELFORMAT_R8G8B8A8;
Sushil Chauhan6686c802015-04-15 11:30:39 -0700402 case HAL_PIXEL_FORMAT_RGBX_8888:
403 return ADRENO_PIXELFORMAT_R8G8B8X8;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800404 case HAL_PIXEL_FORMAT_RGB_565:
405 return ADRENO_PIXELFORMAT_B5G6R5;
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600406 case HAL_PIXEL_FORMAT_BGR_565:
407 return ADRENO_PIXELFORMAT_R5G6B5;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800408 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800409 return ADRENO_PIXELFORMAT_NV12;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800410 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
411 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800412 return ADRENO_PIXELFORMAT_NV12_EXT;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800413 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
414 return ADRENO_PIXELFORMAT_TP10;
415 case HAL_PIXEL_FORMAT_RGBA_1010102:
416 return ADRENO_PIXELFORMAT_R10G10B10A2_UNORM;
417 case HAL_PIXEL_FORMAT_RGBX_1010102:
418 return ADRENO_PIXELFORMAT_R10G10B10X2_UNORM;
419 case HAL_PIXEL_FORMAT_ABGR_2101010:
420 return ADRENO_PIXELFORMAT_A2B10G10R10_UNORM;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800421 default:
422 ALOGE("%s: No map for format: 0x%x", __FUNCTION__, hal_format);
423 break;
424 }
425 return ADRENO_PIXELFORMAT_UNKNOWN;
426}
427
Naomi Luisa44100c2013-02-08 12:42:03 -0800428//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700429IAllocController* IAllocController::sController = NULL;
430IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700431{
432 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700433 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700434 }
435 return sController;
436}
437
438
439//-------------- IonController-----------------------//
440IonController::IonController()
441{
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530442 allocateIonMem();
Manikanta Kanamarlapudicb44d972016-02-03 17:49:55 +0530443
444 char property[PROPERTY_VALUE_MAX];
445 property_get("video.disable.ubwc", property, "0");
446 mDisableUBWCForEncode = atoi(property);
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530447}
448
449void IonController::allocateIonMem()
450{
451 mIonAlloc = new IonAlloc();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700452}
453
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700454int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700455{
456 int ionFlags = 0;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500457 int ionHeapId = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700458 int ret;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700459
460 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700461 data.allocType = 0;
462
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530463 if(usage & GRALLOC_USAGE_PROTECTED) {
Naseer Ahmed934a4c32016-04-11 17:20:02 -0400464 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
465 ionHeapId = ION_HEAP(SD_HEAP_ID);
466 /*
467 * There is currently no flag in ION for Secure Display
468 * VM. Please add it to the define once available.
469 */
470 ionFlags |= ION_SD_FLAGS;
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700471 } else if (usage & GRALLOC_USAGE_HW_CAMERA_MASK) {
472 ionHeapId = ION_HEAP(SD_HEAP_ID);
473 ionFlags |= (usage & GRALLOC_USAGE_HW_COMPOSER) ? ION_SC_PREVIEW_FLAGS : ION_SC_FLAGS;
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530474 } else {
Naseer Ahmed934a4c32016-04-11 17:20:02 -0400475 ionHeapId = ION_HEAP(CP_HEAP_ID);
476 ionFlags |= ION_CP_FLAGS;
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500477 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530478 } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
479 //MM Heap is exclusively a secure heap.
480 //If it is used for non secure cases, fallback to IOMMU heap
481 ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
482 cannot be used as an insecure heap!\
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500483 trying to use system heap instead !!");
484 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500485 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700486
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700487 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500488 ionHeapId |= ION_HEAP(ION_CAMERA_HEAP_ID);
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700489
Arun Kumar K.R0daaa992013-03-12 15:08:29 -0700490 if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500491 ionHeapId |= ION_HEAP(ION_ADSP_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700492
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530493 if(ionFlags & ION_SECURE)
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500494 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700495
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500496 // if no ion heap flags are set, default to system heap
497 if(!ionHeapId)
498 ionHeapId = ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700499
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500500 //At this point we should have the right heap set, there is no fallback
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700501 data.flags = ionFlags;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500502 data.heapId = ionHeapId;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700503 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700504
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700505 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700506 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500507 } else {
508 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x",
509 __FUNCTION__, ionHeapId, ionFlags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700510 }
511
512 return ret;
513}
514
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700515IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700516{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700517 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700518 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
519 memalloc = mIonAlloc;
520 } else {
521 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
522 }
523
524 return memalloc;
525}
526
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700527// helper function
Sushil Chauhan65e26302015-01-14 10:48:57 -0800528unsigned int getSize(int format, int width, int height, int usage,
529 const int alignedw, const int alignedh) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700530
Sushil Chauhan65e26302015-01-14 10:48:57 -0800531 if (isUBwcEnabled(format, usage)) {
532 return getUBwcSize(width, height, format, alignedw, alignedh);
533 }
534
535 unsigned int size = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700536 switch (format) {
537 case HAL_PIXEL_FORMAT_RGBA_8888:
538 case HAL_PIXEL_FORMAT_RGBX_8888:
539 case HAL_PIXEL_FORMAT_BGRA_8888:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800540 case HAL_PIXEL_FORMAT_RGBA_1010102:
541 case HAL_PIXEL_FORMAT_ARGB_2101010:
542 case HAL_PIXEL_FORMAT_RGBX_1010102:
543 case HAL_PIXEL_FORMAT_XRGB_2101010:
544 case HAL_PIXEL_FORMAT_BGRA_1010102:
545 case HAL_PIXEL_FORMAT_ABGR_2101010:
546 case HAL_PIXEL_FORMAT_BGRX_1010102:
547 case HAL_PIXEL_FORMAT_XBGR_2101010:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700548 size = alignedw * alignedh * 4;
549 break;
550 case HAL_PIXEL_FORMAT_RGB_888:
551 size = alignedw * alignedh * 3;
552 break;
553 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600554 case HAL_PIXEL_FORMAT_BGR_565:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700555 case HAL_PIXEL_FORMAT_RGBA_5551:
556 case HAL_PIXEL_FORMAT_RGBA_4444:
Ajay Dudani4dc06492015-03-26 07:28:11 -0700557 case HAL_PIXEL_FORMAT_RAW16:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700558 size = alignedw * alignedh * 2;
559 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700560 case HAL_PIXEL_FORMAT_RAW10:
561 size = ALIGN(alignedw * alignedh, 4096);
562 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700563
564 // adreno formats
565 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
566 size = ALIGN(alignedw*alignedh, 4096);
567 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
568 break;
569 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
570 // The chroma plane is subsampled,
571 // but the pitch in bytes is unchanged
572 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700573 size = ALIGN( alignedw * alignedh, 8192);
574 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
575 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700576 case HAL_PIXEL_FORMAT_YV12:
577 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
578 ALOGE("w or h is odd for the YV12 format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800579 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700580 }
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400581 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700582 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700583 size = ALIGN(size, (unsigned int)4096);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700584 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800585 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
586 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naseer Ahmed2c215292013-09-18 23:47:42 -0400587 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800588 break;
Ramkumar Radhakrishnan3083fe92016-12-15 18:05:40 -0800589 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
590 size = ALIGN((alignedw * alignedh * 2) + (alignedw * alignedh) + 1, 4096);
591 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700592 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
593 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700594 case HAL_PIXEL_FORMAT_YCbCr_422_I:
595 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700596 if(width & 1) {
597 ALOGE("width is odd for the YUV422_SP format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800598 return 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700599 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700600 size = ALIGN(alignedw * alignedh * 2, 4096);
601 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700602 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400603 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhane8a01792012-11-01 16:25:45 -0700604 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700605 break;
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530606 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
607 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
608 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400609 case HAL_PIXEL_FORMAT_BLOB:
Naseer Ahmed6c6c58a2016-05-03 16:27:17 -0400610 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400611 if(height != 1) {
612 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
613 must have height==1 ", __FUNCTION__);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800614 return 0;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400615 }
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400616 size = width;
617 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700618 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700619 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
620 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500621 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
622 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
623 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
624 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
625 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
626 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
627 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
628 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
629 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
630 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
631 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
632 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
633 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
634 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
635 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
636 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
637 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
638 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
639 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
640 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
641 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
642 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
643 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
644 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
645 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
646 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
647 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Jeykumar Sankaran8f4585f2014-02-05 15:23:40 -0800648 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500649 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
650 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700651 default:
Naseer Ahmedb8ecfbf2015-11-02 20:34:29 -0500652 ALOGE("%s: Unrecognized pixel format: 0x%x", __FUNCTION__, format);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800653 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700654 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700655 return size;
656}
657
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700658unsigned int getBufferSizeAndDimensions(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700659 int& alignedw, int &alignedh)
660{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700661 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700662
663 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
664 height,
665 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800666 0,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700667 alignedw,
668 alignedh);
669
Sushil Chauhan65e26302015-01-14 10:48:57 -0800670 size = getSize(format, width, height, 0 /* usage */, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700671
672 return size;
673}
674
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700675
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700676unsigned int getBufferSizeAndDimensions(int width, int height, int format,
677 int usage, int& alignedw, int &alignedh)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700678{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700679 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700680
681 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
682 height,
683 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800684 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700685 alignedw,
686 alignedh);
687
Sushil Chauhan65e26302015-01-14 10:48:57 -0800688 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700689
690 return size;
691}
692
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700693void getYuvUbwcSPPlaneInfo(uint64_t base, int width, int height,
694 int color_format, struct android_ycbcr* ycbcr)
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800695{
696 // UBWC buffer has these 4 planes in the following sequence:
697 // Y_Meta_Plane, Y_Plane, UV_Meta_Plane, UV_Plane
698 unsigned int y_meta_stride, y_meta_height, y_meta_size;
699 unsigned int y_stride, y_height, y_size;
700 unsigned int c_meta_stride, c_meta_height, c_meta_size;
701 unsigned int alignment = 4096;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800702
703 y_meta_stride = VENUS_Y_META_STRIDE(color_format, width);
704 y_meta_height = VENUS_Y_META_SCANLINES(color_format, height);
705 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
706
707 y_stride = VENUS_Y_STRIDE(color_format, width);
708 y_height = VENUS_Y_SCANLINES(color_format, height);
709 y_size = ALIGN((y_stride * y_height), alignment);
710
711 c_meta_stride = VENUS_UV_META_STRIDE(color_format, width);
712 c_meta_height = VENUS_UV_META_SCANLINES(color_format, height);
713 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
714
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700715 ycbcr->y = (void*)(base + y_meta_size);
716 ycbcr->cb = (void*)(base + y_meta_size + y_size + c_meta_size);
717 ycbcr->cr = (void*)(base + y_meta_size + y_size +
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800718 c_meta_size + 1);
719 ycbcr->ystride = y_stride;
720 ycbcr->cstride = VENUS_UV_STRIDE(color_format, width);
721}
722
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700723void getYuvSPPlaneInfo(uint64_t base, int width, int height, int bpp,
724 struct android_ycbcr* ycbcr)
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800725{
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800726 unsigned int ystride, cstride;
727
728 ystride = cstride = width * bpp;
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700729 ycbcr->y = (void*)base;
730 ycbcr->cb = (void*)(base + ystride * height);
731 ycbcr->cr = (void*)(base + ystride * height + 1);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800732 ycbcr->ystride = ystride;
733 ycbcr->cstride = cstride;
734 ycbcr->chroma_step = 2 * bpp;
735}
736
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400737int getYUVPlaneInfo(private_handle_t* hnd, struct android_ycbcr* ycbcr)
738{
739 int err = 0;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700740 int width = hnd->width;
741 int height = hnd->height;
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700742 int format = hnd->format;
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700743
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700744 unsigned int ystride, cstride;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700745
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400746 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700747 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
748
749 // Check if UBWC buffer has been rendered in linear format.
750 if (metadata && (metadata->operation & LINEAR_FORMAT)) {
751 format = metadata->linearFormat;
752 }
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400753
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700754 // Check metadata if the geometry has been updated.
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700755 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700756 int usage = 0;
757
758 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
759 usage = GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
760 }
761
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700762 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(metadata->bufferDim.sliceWidth,
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700763 metadata->bufferDim.sliceHeight, format, usage, width, height);
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700764 }
765
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400766 // Get the chroma offsets from the handle width/height. We take advantage
767 // of the fact the width _is_ the stride
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700768 switch (format) {
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400769 //Semiplanar
770 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
771 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
772 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
773 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: //Same as YCbCr_420_SP_VENUS
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700774 getYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800775 break;
776
777 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700778 getYuvSPPlaneInfo(hnd->base, width, height, 2, ycbcr);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400779 break;
780
Sushil Chauhan4686c972015-02-20 15:44:52 -0800781 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700782 getYuvUbwcSPPlaneInfo(hnd->base, width, height,
783 COLOR_FMT_NV12_UBWC, ycbcr);
Sushil Chauhan4686c972015-02-20 15:44:52 -0800784 ycbcr->chroma_step = 2;
785 break;
786
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800787 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700788 getYuvUbwcSPPlaneInfo(hnd->base, width, height,
789 COLOR_FMT_NV12_BPP10_UBWC, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800790 ycbcr->chroma_step = 3;
791 break;
792
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400793 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
794 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
795 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530796 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400797 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ajay Dudani4dc06492015-03-26 07:28:11 -0700798 case HAL_PIXEL_FORMAT_RAW16:
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700799 case HAL_PIXEL_FORMAT_RAW10:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700800 getYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800801 std::swap(ycbcr->cb, ycbcr->cr);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400802 break;
803
804 //Planar
805 case HAL_PIXEL_FORMAT_YV12:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700806 ystride = width;
807 cstride = ALIGN(width/2, 16);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400808 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700809 ycbcr->cr = (void*)(hnd->base + ystride * height);
810 ycbcr->cb = (void*)(hnd->base + ystride * height +
811 cstride * height/2);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400812 ycbcr->ystride = ystride;
813 ycbcr->cstride = cstride;
814 ycbcr->chroma_step = 1;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400815 break;
816 //Unsupported formats
817 case HAL_PIXEL_FORMAT_YCbCr_422_I:
818 case HAL_PIXEL_FORMAT_YCrCb_422_I:
819 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
820 default:
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700821 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, format);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400822 err = -EINVAL;
823 }
824 return err;
825
826}
827
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700828
829
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700830// Allocate buffer from width, height and format into a
831// private_handle_t. It is the responsibility of the caller
832// to free the buffer using the free_buffer function
833int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
834{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700835 alloc_data data;
836 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700837 gralloc::IAllocController* sAlloc =
838 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700839 data.base = 0;
840 data.fd = -1;
841 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700842 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
843 alignedh);
844
Naseer Ahmed29a26812012-06-14 00:56:20 -0700845 data.align = getpagesize();
846 data.uncached = useUncached(usage);
847 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700848
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700849 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700850 if (0 != err) {
851 ALOGE("%s: allocate failed", __FUNCTION__);
852 return -ENOMEM;
853 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700854
radhakrishnad7131e62015-10-13 12:32:30 +0530855 if(isUBwcEnabled(format, usage)) {
856 data.allocType |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
857 }
858
Naseer Ahmed29a26812012-06-14 00:56:20 -0700859 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700860 data.allocType, 0, format,
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700861 alignedw, alignedh, -1, 0, 0, w, h);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700862 hnd->base = (uint64_t) data.base;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700863 hnd->offset = data.offset;
864 hnd->gpuaddr = 0;
865 *pHnd = hnd;
866 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700867}
868
869void free_buffer(private_handle_t *hnd)
870{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700871 gralloc::IAllocController* sAlloc =
872 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700873 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700874 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700875 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
876 }
877 if(hnd)
878 delete hnd;
879
880}
Sushil Chauhan65e26302015-01-14 10:48:57 -0800881
882// UBWC helper functions
883static bool isUBwcFormat(int format)
884{
885 // Explicitly defined UBWC formats
886 switch(format)
887 {
888 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800889 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800890 return true;
891 default:
892 return false;
893 }
894}
895
896static bool isUBwcSupported(int format)
897{
Sushil Chauhan01361412016-04-25 16:36:18 -0700898 if (MDPCapabilityInfo::getInstance().isUBwcSupportedByMDP()) {
899 // Existing HAL formats with UBWC support
900 switch(format)
901 {
902 case HAL_PIXEL_FORMAT_BGR_565:
903 case HAL_PIXEL_FORMAT_RGBA_8888:
904 case HAL_PIXEL_FORMAT_RGBX_8888:
905 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
906 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
907 case HAL_PIXEL_FORMAT_RGBA_1010102:
908 case HAL_PIXEL_FORMAT_RGBX_1010102:
909 return true;
910 default:
911 break;
912 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800913 }
Sushil Chauhan01361412016-04-25 16:36:18 -0700914 return false;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800915}
916
917bool isUBwcEnabled(int format, int usage)
918{
Sushil Chauhan81594f62015-01-26 16:00:51 -0800919 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
920 if (isUBwcFormat(format))
921 return true;
922
Manikanta Kanamarlapudicb44d972016-02-03 17:49:55 +0530923 if ((usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) &&
924 gralloc::IAllocController::getInstance()->isDisableUBWCForEncoder()) {
925 return false;
926 }
927
Sushil Chauhan7807d192015-08-13 10:10:48 -0700928 // Allow UBWC, if an OpenGL client sets UBWC usage flag and GPU plus MDP
929 // support the format. OR if a non-OpenGL client like Rotator, sets UBWC
930 // usage flag and MDP supports the format.
931 if ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) && isUBwcSupported(format)) {
932 bool enable = true;
933 // Query GPU for UBWC only if buffer is intended to be used by GPU.
934 if (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER)) {
935 enable = AdrenoMemInfo::getInstance().isUBWCSupportedByGPU(format);
936 }
Sushil Chauhan81594f62015-01-26 16:00:51 -0800937 // Allow UBWC, only if CPU usage flags are not set
Sushil Chauhan7807d192015-08-13 10:10:48 -0700938 if (enable && !(usage & (GRALLOC_USAGE_SW_READ_MASK |
939 GRALLOC_USAGE_SW_WRITE_MASK))) {
Sushil Chauhan65e26302015-01-14 10:48:57 -0800940 return true;
941 }
942 }
943 return false;
944}
945
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800946static void getYuvUBwcWidthHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800947 int& aligned_w, int& aligned_h)
948{
949 switch (format)
950 {
951 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
952 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
953 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
954 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
955 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
956 break;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800957 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Arun Kumar K.Rfc2a27f2016-09-07 18:59:46 -0700958 // The macro returns the stride which is 4/3 times the width, hence * 3/4
959 aligned_w = (VENUS_Y_STRIDE(COLOR_FMT_NV12_BPP10_UBWC, width) * 3) / 4;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800960 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_BPP10_UBWC, height);
961 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800962 default:
963 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
964 aligned_w = 0;
965 aligned_h = 0;
966 break;
967 }
968}
969
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800970static void getRgbUBwcBlockSize(int bpp, int& block_width, int& block_height)
Sushil Chauhan65e26302015-01-14 10:48:57 -0800971{
972 block_width = 0;
973 block_height = 0;
974
975 switch(bpp)
976 {
977 case 2:
978 case 4:
979 block_width = 16;
980 block_height = 4;
981 break;
982 case 8:
983 block_width = 8;
984 block_height = 4;
985 break;
986 case 16:
987 block_width = 4;
988 block_height = 4;
989 break;
990 default:
991 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
992 break;
993 }
994}
995
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800996static unsigned int getRgbUBwcMetaBufferSize(int width, int height, int bpp)
Sushil Chauhan65e26302015-01-14 10:48:57 -0800997{
998 unsigned int size = 0;
999 int meta_width, meta_height;
1000 int block_width, block_height;
1001
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001002 getRgbUBwcBlockSize(bpp, block_width, block_height);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001003
1004 if (!block_width || !block_height) {
1005 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
1006 return size;
1007 }
1008
1009 // Align meta buffer height to 16 blocks
1010 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
1011
1012 // Align meta buffer width to 64 blocks
1013 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
1014
1015 // Align meta buffer size to 4K
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001016 size = ALIGN((meta_width * meta_height), 4096);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001017 return size;
1018}
1019
1020static unsigned int getUBwcSize(int width, int height, int format,
1021 const int alignedw, const int alignedh) {
1022
1023 unsigned int size = 0;
1024 switch (format) {
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001025 case HAL_PIXEL_FORMAT_BGR_565:
Sushil Chauhan65e26302015-01-14 10:48:57 -08001026 size = alignedw * alignedh * 2;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001027 size += getRgbUBwcMetaBufferSize(width, height, 2);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001028 break;
1029 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -07001030 case HAL_PIXEL_FORMAT_RGBX_8888:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001031 case HAL_PIXEL_FORMAT_RGBA_1010102:
1032 case HAL_PIXEL_FORMAT_RGBX_1010102:
Sushil Chauhan65e26302015-01-14 10:48:57 -08001033 size = alignedw * alignedh * 4;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001034 size += getRgbUBwcMetaBufferSize(width, height, 4);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001035 break;
1036 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1037 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1038 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1039 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
1040 break;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001041 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1042 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_BPP10_UBWC, width, height);
1043 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -08001044 default:
1045 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
1046 break;
1047 }
1048 return size;
1049}
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001050
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001051int getRgbDataAddress(private_handle_t* hnd, void** rgb_data)
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001052{
1053 int err = 0;
1054
1055 // This api is for RGB* formats
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001056 if (!isUncompressedRgbFormat(hnd->format)) {
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001057 return -EINVAL;
1058 }
1059
1060 // linear buffer
1061 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001062 *rgb_data = (void*)hnd->base;
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001063 return err;
1064 }
1065
Prabhanjan Kandulaebc8ce32016-10-18 12:25:54 -07001066 // Ubwc buffers
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001067 unsigned int meta_size = 0;
1068 switch (hnd->format) {
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001069 case HAL_PIXEL_FORMAT_BGR_565:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001070 meta_size = getRgbUBwcMetaBufferSize(hnd->width, hnd->height, 2);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001071 break;
1072 case HAL_PIXEL_FORMAT_RGBA_8888:
1073 case HAL_PIXEL_FORMAT_RGBX_8888:
Prabhanjan Kandulaebc8ce32016-10-18 12:25:54 -07001074 case HAL_PIXEL_FORMAT_RGBA_1010102:
1075 case HAL_PIXEL_FORMAT_RGBX_1010102:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001076 meta_size = getRgbUBwcMetaBufferSize(hnd->width, hnd->height, 4);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001077 break;
1078 default:
1079 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
1080 err = -EINVAL;
1081 break;
1082 }
1083
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001084 *rgb_data = (void*)(hnd->base + meta_size);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001085 return err;
1086}