blob: fb22359bfd271d98916ac86012ee5b712f81aad5 [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>
Saurabh Shah2693a502017-01-30 10:48:00 -080033#include <media/msm_media_info.h>
34#include <qdMetaData.h>
35#include <utils/Singleton.h>
36#include <utils/Mutex.h>
37#include <algorithm>
38
Iliyan Malchev202a77d2012-06-11 14:41:12 -070039#include "gralloc_priv.h"
40#include "alloc_controller.h"
41#include "memalloc.h"
42#include "ionalloc.h"
Iliyan Malchev202a77d2012-06-11 14:41:12 -070043#include "gr.h"
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -070044#include "qd_utils.h"
Sushil Chauhanc6bd6d92012-12-12 12:33:01 -080045
Naseer Ahmed63326f42013-12-18 02:45:48 -050046#define ASTC_BLOCK_SIZE 16
Naseer Ahmed63326f42013-12-18 02:45:48 -050047
Shalaj Jain3c490412015-04-22 16:52:03 -070048#ifndef ION_FLAG_CP_PIXEL
Shalaj Jain1f9725a2015-03-04 17:53:49 -080049#define ION_FLAG_CP_PIXEL 0
Shalaj Jain1f9725a2015-03-04 17:53:49 -080050#endif
51
52#ifndef ION_FLAG_ALLOW_NON_CONTIG
53#define ION_FLAG_ALLOW_NON_CONTIG 0
54#endif
55
Sushil Chauhandfe55a22016-09-12 15:48:29 -070056#ifndef ION_FLAG_CP_CAMERA_PREVIEW
57#define ION_FLAG_CP_CAMERA_PREVIEW 0
58#endif
59
Shalaj Jain3c490412015-04-22 16:52:03 -070060#ifdef MASTER_SIDE_CP
61#define CP_HEAP_ID ION_SECURE_HEAP_ID
Arun Kumar K.R7f0b24b2015-07-05 21:20:57 -070062#define SD_HEAP_ID ION_SECURE_DISPLAY_HEAP_ID
Shalaj Jain3c490412015-04-22 16:52:03 -070063#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_CP_PIXEL)
Arun Kumar K.R7f0b24b2015-07-05 21:20:57 -070064#define ION_SD_FLAGS (ION_SECURE | ION_FLAG_CP_SEC_DISPLAY)
Sushil Chauhandfe55a22016-09-12 15:48:29 -070065#define ION_SC_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA)
66#define ION_SC_PREVIEW_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA_PREVIEW)
Shalaj Jain3c490412015-04-22 16:52:03 -070067#else // SLAVE_SIDE_CP
68#define CP_HEAP_ID ION_CP_MM_HEAP_ID
69#define SD_HEAP_ID CP_HEAP_ID
70#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_ALLOW_NON_CONTIG)
71#define ION_SD_FLAGS ION_SECURE
Sushil Chauhandfe55a22016-09-12 15:48:29 -070072#define ION_SC_FLAGS ION_SECURE
73#define ION_SC_PREVIEW_FLAGS ION_SECURE
Shalaj Jain3c490412015-04-22 16:52:03 -070074#endif
75
Iliyan Malchev202a77d2012-06-11 14:41:12 -070076using namespace gralloc;
Naseer Ahmeda87da602012-07-01 23:54:19 -070077using namespace qdutils;
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -070078using namespace android;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070079
Naomi Luisa44100c2013-02-08 12:42:03 -080080ANDROID_SINGLETON_STATIC_INSTANCE(AdrenoMemInfo);
Ramakant Singhc85ccee2016-04-01 15:25:17 +053081ANDROID_SINGLETON_STATIC_INSTANCE(MDPCapabilityInfo);
Naomi Luisa44100c2013-02-08 12:42:03 -080082
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -080083static void getYuvUBwcWidthHeight(int, int, int, int&, int&);
Sushil Chauhan65e26302015-01-14 10:48:57 -080084static unsigned int getUBwcSize(int, int, int, const int, const int);
85
Iliyan Malchev202a77d2012-06-11 14:41:12 -070086//Common functions
Iliyan Malchev202a77d2012-06-11 14:41:12 -070087
Saurabh Shah1adcafe2014-12-19 10:05:41 -080088/* The default policy is to return cached buffers unless the client explicity
89 * sets the PRIVATE_UNCACHED flag or indicates that the buffer will be rarely
90 * read or written in software. Any combination with a _RARELY_ flag will be
91 * treated as uncached. */
92static bool useUncached(const int& usage) {
Naseer Ahmedde758fd2016-05-03 15:10:23 -040093 if ((usage & GRALLOC_USAGE_PROTECTED) or
94 (usage & GRALLOC_USAGE_PRIVATE_UNCACHED) or
95 ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY) or
96 ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY))
Saurabh Shah1adcafe2014-12-19 10:05:41 -080097 return true;
98
99 return false;
100}
101
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530102//------------- MDPCapabilityInfo-----------------------//
103MDPCapabilityInfo :: MDPCapabilityInfo() {
Sushil Chauhan01361412016-04-25 16:36:18 -0700104 qdutils::querySDEInfo(HAS_UBWC, &isUBwcSupported);
Jeykumar Sankaran9bc1a782015-12-14 18:36:27 -0800105 qdutils::querySDEInfo(HAS_WB_UBWC, &isWBUBWCSupported);
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530106}
107
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530108//------------- AdrenoMemInfo-----------------------//
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800109AdrenoMemInfo::AdrenoMemInfo()
110{
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800111 LINK_adreno_compute_aligned_width_and_height = NULL;
112 LINK_adreno_compute_padding = NULL;
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800113 LINK_adreno_compute_compressedfmt_aligned_width_and_height = NULL;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800114 LINK_adreno_isUBWCSupportedByGpu = NULL;
Sushil Chauhan521ce352015-08-28 11:33:30 -0700115 LINK_adreno_get_gpu_pixel_alignment = NULL;
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800116
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800117 libadreno_utils = ::dlopen("libadreno_utils.so", RTLD_NOW);
118 if (libadreno_utils) {
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800119 *(void **)&LINK_adreno_compute_aligned_width_and_height =
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700120 ::dlsym(libadreno_utils, "compute_aligned_width_and_height");
121 *(void **)&LINK_adreno_compute_padding =
122 ::dlsym(libadreno_utils, "compute_surface_padding");
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800123 *(void **)&LINK_adreno_compute_compressedfmt_aligned_width_and_height =
124 ::dlsym(libadreno_utils,
125 "compute_compressedfmt_aligned_width_and_height");
Sushil Chauhan082acd62015-01-14 16:49:29 -0800126 *(void **)&LINK_adreno_isUBWCSupportedByGpu =
127 ::dlsym(libadreno_utils, "isUBWCSupportedByGpu");
Sushil Chauhan521ce352015-08-28 11:33:30 -0700128 *(void **)&LINK_adreno_get_gpu_pixel_alignment =
129 ::dlsym(libadreno_utils, "get_gpu_pixel_alignment");
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800130 }
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700131
132 // Check if the overriding property debug.gralloc.gfx_ubwc_disable
133 // that disables UBWC allocations for the graphics stack is set
134 gfx_ubwc_disable = 0;
135 char property[PROPERTY_VALUE_MAX];
136 property_get("debug.gralloc.gfx_ubwc_disable", property, "0");
137 if(!(strncmp(property, "1", PROPERTY_VALUE_MAX)) ||
138 !(strncmp(property, "true", PROPERTY_VALUE_MAX))) {
139 gfx_ubwc_disable = 1;
140 }
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800141}
142
143AdrenoMemInfo::~AdrenoMemInfo()
144{
145 if (libadreno_utils) {
146 ::dlclose(libadreno_utils);
147 }
148}
149
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700150void AdrenoMemInfo::getAlignedWidthAndHeight(const private_handle_t *hnd, int& aligned_w,
151 int& aligned_h) {
152 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
153 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
154 int w = metadata->bufferDim.sliceWidth;
155 int h = metadata->bufferDim.sliceHeight;
156 int f = hnd->format;
157 int usage = 0;
158
159 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
160 usage = GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
161 }
162
163 getAlignedWidthAndHeight(w, h, f, usage, aligned_w, aligned_h);
164 } else {
165 aligned_w = hnd->width;
166 aligned_h = hnd->height;
167 }
168
169}
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700170
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700171void AdrenoMemInfo::getUnalignedWidthAndHeight(const private_handle_t *hnd, int& unaligned_w,
172 int& unaligned_h) {
173 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
174 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
175 unaligned_w = metadata->bufferDim.sliceWidth;
176 unaligned_h = metadata->bufferDim.sliceHeight;
177 } else {
178 unaligned_w = hnd->unaligned_width;
179 unaligned_h = hnd->unaligned_height;
180 }
181}
182
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700183bool isUncompressedRgbFormat(int format)
184{
185 bool is_rgb_format = false;
186
187 switch (format)
188 {
189 case HAL_PIXEL_FORMAT_RGBA_8888:
190 case HAL_PIXEL_FORMAT_RGBX_8888:
191 case HAL_PIXEL_FORMAT_RGB_888:
192 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600193 case HAL_PIXEL_FORMAT_BGR_565:
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700194 case HAL_PIXEL_FORMAT_BGRA_8888:
195 case HAL_PIXEL_FORMAT_RGBA_5551:
196 case HAL_PIXEL_FORMAT_RGBA_4444:
197 case HAL_PIXEL_FORMAT_R_8:
198 case HAL_PIXEL_FORMAT_RG_88:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800199 case HAL_PIXEL_FORMAT_BGRX_8888:
200 case HAL_PIXEL_FORMAT_RGBA_1010102:
201 case HAL_PIXEL_FORMAT_ARGB_2101010:
202 case HAL_PIXEL_FORMAT_RGBX_1010102:
203 case HAL_PIXEL_FORMAT_XRGB_2101010:
204 case HAL_PIXEL_FORMAT_BGRA_1010102:
205 case HAL_PIXEL_FORMAT_ABGR_2101010:
206 case HAL_PIXEL_FORMAT_BGRX_1010102:
207 case HAL_PIXEL_FORMAT_XBGR_2101010: // Intentional fallthrough
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700208 is_rgb_format = true;
209 break;
210 default:
211 break;
212 }
213
214 return is_rgb_format;
215}
216
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800217void AdrenoMemInfo::getAlignedWidthAndHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800218 int usage, int& aligned_w, int& aligned_h)
Naomi Luisa44100c2013-02-08 12:42:03 -0800219{
Sushil Chauhane61fac52015-04-30 17:14:37 -0700220 bool ubwc_enabled = isUBwcEnabled(format, usage);
221
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800222 // Currently surface padding is only computed for RGB* surfaces.
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700223 if (isUncompressedRgbFormat(format) == true) {
Saurabh Shahc5b2b702016-10-24 17:16:01 -0700224 int tileEnabled = ubwc_enabled;
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700225 getGpuAlignedWidthHeight(width, height, format, tileEnabled, aligned_w, aligned_h);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800226 } else if (ubwc_enabled) {
227 getYuvUBwcWidthHeight(width, height, format, aligned_w, aligned_h);
228 } else {
229 aligned_w = width;
230 aligned_h = height;
231 int alignment = 32;
232 switch (format)
233 {
234 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
235 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
236 if (LINK_adreno_get_gpu_pixel_alignment) {
237 alignment = LINK_adreno_get_gpu_pixel_alignment();
238 }
239 aligned_w = ALIGN(width, alignment);
240 break;
241 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
242 aligned_w = ALIGN(width, alignment);
243 break;
244 case HAL_PIXEL_FORMAT_RAW16:
245 aligned_w = ALIGN(width, 16);
246 break;
247 case HAL_PIXEL_FORMAT_RAW10:
248 aligned_w = ALIGN(width * 10 / 8, 8);
249 break;
250 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
251 aligned_w = ALIGN(width, 128);
252 break;
253 case HAL_PIXEL_FORMAT_YV12:
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:
Ramkumar Radhakrishnan3083fe92016-12-15 18:05:40 -0800258 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800259 aligned_w = ALIGN(width, 16);
260 break;
261 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
262 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
263 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
264 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
265 break;
266 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
267 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV21, width);
268 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV21, height);
269 break;
270 case HAL_PIXEL_FORMAT_BLOB:
Naseer Ahmed6c6c58a2016-05-03 16:27:17 -0400271 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800272 break;
273 case HAL_PIXEL_FORMAT_NV21_ZSL:
274 aligned_w = ALIGN(width, 64);
275 aligned_h = ALIGN(height, 64);
276 break;
277 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
278 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
279 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
280 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
281 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
282 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
283 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
284 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
285 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
286 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
287 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
288 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
289 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
290 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
291 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
292 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
293 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
294 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
295 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
296 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
297 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
298 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
299 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
300 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
301 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
302 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
303 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
304 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
305 if(LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
306 int bytesPerPixel = 0;
307 int raster_mode = 0; //Adreno unknown raster mode.
308 int padding_threshold = 512; //Threshold for padding
309 //surfaces.
Sushil Chauhan65e26302015-01-14 10:48:57 -0800310
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800311 LINK_adreno_compute_compressedfmt_aligned_width_and_height(
312 width, height, format, 0,raster_mode, padding_threshold,
313 &aligned_w, &aligned_h, &bytesPerPixel);
314 } else {
315 ALOGW("%s: Warning!! Symbols" \
316 " compute_compressedfmt_aligned_width_and_height" \
317 " not found", __FUNCTION__);
318 }
319 break;
320 default: break;
321 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800322 }
323}
324
325void AdrenoMemInfo::getGpuAlignedWidthHeight(int width, int height, int format,
326 int tile_enabled, int& aligned_w, int& aligned_h)
327{
328 aligned_w = ALIGN(width, 32);
329 aligned_h = ALIGN(height, 32);
330
331 // Don't add any additional padding if debug.gralloc.map_fb_memory
332 // is enabled
333 char property[PROPERTY_VALUE_MAX];
334 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
335 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
336 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
337 return;
338 }
339
340 int bpp = 4;
341 switch(format)
342 {
343 case HAL_PIXEL_FORMAT_RGB_888:
344 bpp = 3;
345 break;
346 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600347 case HAL_PIXEL_FORMAT_BGR_565:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800348 case HAL_PIXEL_FORMAT_RGBA_5551:
349 case HAL_PIXEL_FORMAT_RGBA_4444:
350 bpp = 2;
351 break;
352 default: break;
353 }
354
355 if (libadreno_utils) {
356 int raster_mode = 0; // Adreno unknown raster mode.
357 int padding_threshold = 512; // Threshold for padding surfaces.
358 // the function below computes aligned width and aligned height
359 // based on linear or macro tile mode selected.
360 if(LINK_adreno_compute_aligned_width_and_height) {
361 LINK_adreno_compute_aligned_width_and_height(width,
362 height, bpp, tile_enabled,
363 raster_mode, padding_threshold,
364 &aligned_w, &aligned_h);
365
366 } else if(LINK_adreno_compute_padding) {
367 int surface_tile_height = 1; // Linear surface
368 aligned_w = LINK_adreno_compute_padding(width, bpp,
369 surface_tile_height, raster_mode,
370 padding_threshold);
371 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
372 __FUNCTION__);
373 } else {
374 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
375 "compute_aligned_width_and_height not found", __FUNCTION__);
376 }
377 }
378}
379
380int AdrenoMemInfo::isUBWCSupportedByGPU(int format)
381{
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700382 if (!gfx_ubwc_disable && libadreno_utils) {
Sushil Chauhan082acd62015-01-14 16:49:29 -0800383 if (LINK_adreno_isUBWCSupportedByGpu) {
384 ADRENOPIXELFORMAT gpu_format = getGpuPixelFormat(format);
385 return LINK_adreno_isUBWCSupportedByGpu(gpu_format);
386 }
387 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800388 return 0;
Naomi Luisa44100c2013-02-08 12:42:03 -0800389}
390
Sushil Chauhan082acd62015-01-14 16:49:29 -0800391ADRENOPIXELFORMAT AdrenoMemInfo::getGpuPixelFormat(int hal_format)
392{
393 switch (hal_format) {
394 case HAL_PIXEL_FORMAT_RGBA_8888:
395 return ADRENO_PIXELFORMAT_R8G8B8A8;
Sushil Chauhan6686c802015-04-15 11:30:39 -0700396 case HAL_PIXEL_FORMAT_RGBX_8888:
397 return ADRENO_PIXELFORMAT_R8G8B8X8;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800398 case HAL_PIXEL_FORMAT_RGB_565:
399 return ADRENO_PIXELFORMAT_B5G6R5;
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600400 case HAL_PIXEL_FORMAT_BGR_565:
401 return ADRENO_PIXELFORMAT_R5G6B5;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800402 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800403 return ADRENO_PIXELFORMAT_NV12;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800404 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
405 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800406 return ADRENO_PIXELFORMAT_NV12_EXT;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800407 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
408 return ADRENO_PIXELFORMAT_TP10;
409 case HAL_PIXEL_FORMAT_RGBA_1010102:
410 return ADRENO_PIXELFORMAT_R10G10B10A2_UNORM;
411 case HAL_PIXEL_FORMAT_RGBX_1010102:
412 return ADRENO_PIXELFORMAT_R10G10B10X2_UNORM;
413 case HAL_PIXEL_FORMAT_ABGR_2101010:
414 return ADRENO_PIXELFORMAT_A2B10G10R10_UNORM;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800415 default:
416 ALOGE("%s: No map for format: 0x%x", __FUNCTION__, hal_format);
417 break;
418 }
419 return ADRENO_PIXELFORMAT_UNKNOWN;
420}
421
Naomi Luisa44100c2013-02-08 12:42:03 -0800422//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700423IAllocController* IAllocController::sController = NULL;
424IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700425{
426 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700427 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700428 }
429 return sController;
430}
431
432
433//-------------- IonController-----------------------//
434IonController::IonController()
435{
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530436 allocateIonMem();
Manikanta Kanamarlapudicb44d972016-02-03 17:49:55 +0530437
438 char property[PROPERTY_VALUE_MAX];
439 property_get("video.disable.ubwc", property, "0");
440 mDisableUBWCForEncode = atoi(property);
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530441}
442
443void IonController::allocateIonMem()
444{
445 mIonAlloc = new IonAlloc();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700446}
447
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700448int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700449{
450 int ionFlags = 0;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500451 int ionHeapId = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700452 int ret;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700453
454 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700455 data.allocType = 0;
456
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530457 if(usage & GRALLOC_USAGE_PROTECTED) {
Naseer Ahmed934a4c32016-04-11 17:20:02 -0400458 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
459 ionHeapId = ION_HEAP(SD_HEAP_ID);
460 /*
461 * There is currently no flag in ION for Secure Display
462 * VM. Please add it to the define once available.
463 */
464 ionFlags |= ION_SD_FLAGS;
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700465 } else if (usage & GRALLOC_USAGE_HW_CAMERA_MASK) {
466 ionHeapId = ION_HEAP(SD_HEAP_ID);
467 ionFlags |= (usage & GRALLOC_USAGE_HW_COMPOSER) ? ION_SC_PREVIEW_FLAGS : ION_SC_FLAGS;
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530468 } else {
Naseer Ahmed934a4c32016-04-11 17:20:02 -0400469 ionHeapId = ION_HEAP(CP_HEAP_ID);
470 ionFlags |= ION_CP_FLAGS;
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500471 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530472 } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
473 //MM Heap is exclusively a secure heap.
474 //If it is used for non secure cases, fallback to IOMMU heap
475 ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
476 cannot be used as an insecure heap!\
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500477 trying to use system heap instead !!");
478 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500479 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700480
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700481 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500482 ionHeapId |= ION_HEAP(ION_CAMERA_HEAP_ID);
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700483
Arun Kumar K.R0daaa992013-03-12 15:08:29 -0700484 if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500485 ionHeapId |= ION_HEAP(ION_ADSP_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700486
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530487 if(ionFlags & ION_SECURE)
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500488 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700489
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500490 // if no ion heap flags are set, default to system heap
491 if(!ionHeapId)
492 ionHeapId = ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700493
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500494 //At this point we should have the right heap set, there is no fallback
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700495 data.flags = ionFlags;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500496 data.heapId = ionHeapId;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700497 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700498
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700499 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700500 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500501 } else {
502 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x",
503 __FUNCTION__, ionHeapId, ionFlags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700504 }
505
506 return ret;
507}
508
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700509IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700510{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700511 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700512 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
513 memalloc = mIonAlloc;
514 } else {
515 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
516 }
517
518 return memalloc;
519}
520
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700521// helper function
Sushil Chauhan65e26302015-01-14 10:48:57 -0800522unsigned int getSize(int format, int width, int height, int usage,
523 const int alignedw, const int alignedh) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700524
Sushil Chauhan65e26302015-01-14 10:48:57 -0800525 if (isUBwcEnabled(format, usage)) {
526 return getUBwcSize(width, height, format, alignedw, alignedh);
527 }
528
529 unsigned int size = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700530 switch (format) {
531 case HAL_PIXEL_FORMAT_RGBA_8888:
532 case HAL_PIXEL_FORMAT_RGBX_8888:
533 case HAL_PIXEL_FORMAT_BGRA_8888:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800534 case HAL_PIXEL_FORMAT_RGBA_1010102:
535 case HAL_PIXEL_FORMAT_ARGB_2101010:
536 case HAL_PIXEL_FORMAT_RGBX_1010102:
537 case HAL_PIXEL_FORMAT_XRGB_2101010:
538 case HAL_PIXEL_FORMAT_BGRA_1010102:
539 case HAL_PIXEL_FORMAT_ABGR_2101010:
540 case HAL_PIXEL_FORMAT_BGRX_1010102:
541 case HAL_PIXEL_FORMAT_XBGR_2101010:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700542 size = alignedw * alignedh * 4;
543 break;
544 case HAL_PIXEL_FORMAT_RGB_888:
545 size = alignedw * alignedh * 3;
546 break;
547 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600548 case HAL_PIXEL_FORMAT_BGR_565:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700549 case HAL_PIXEL_FORMAT_RGBA_5551:
550 case HAL_PIXEL_FORMAT_RGBA_4444:
Ajay Dudani4dc06492015-03-26 07:28:11 -0700551 case HAL_PIXEL_FORMAT_RAW16:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700552 size = alignedw * alignedh * 2;
553 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700554 case HAL_PIXEL_FORMAT_RAW10:
555 size = ALIGN(alignedw * alignedh, 4096);
556 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700557
558 // adreno formats
559 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
560 size = ALIGN(alignedw*alignedh, 4096);
561 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
562 break;
563 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
564 // The chroma plane is subsampled,
565 // but the pitch in bytes is unchanged
566 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700567 size = ALIGN( alignedw * alignedh, 8192);
568 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
569 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700570 case HAL_PIXEL_FORMAT_YV12:
571 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
572 ALOGE("w or h is odd for the YV12 format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800573 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700574 }
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400575 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700576 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700577 size = ALIGN(size, (unsigned int)4096);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700578 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800579 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
580 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naseer Ahmed2c215292013-09-18 23:47:42 -0400581 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800582 break;
Ramkumar Radhakrishnan3083fe92016-12-15 18:05:40 -0800583 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
584 size = ALIGN((alignedw * alignedh * 2) + (alignedw * alignedh) + 1, 4096);
585 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700586 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
587 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700588 case HAL_PIXEL_FORMAT_YCbCr_422_I:
589 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700590 if(width & 1) {
591 ALOGE("width is odd for the YUV422_SP format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800592 return 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700593 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700594 size = ALIGN(alignedw * alignedh * 2, 4096);
595 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700596 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400597 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhane8a01792012-11-01 16:25:45 -0700598 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700599 break;
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530600 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
601 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
602 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400603 case HAL_PIXEL_FORMAT_BLOB:
Naseer Ahmed6c6c58a2016-05-03 16:27:17 -0400604 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400605 if(height != 1) {
606 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
607 must have height==1 ", __FUNCTION__);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800608 return 0;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400609 }
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400610 size = width;
611 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700612 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700613 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
614 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500615 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
616 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
617 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
618 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
619 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
620 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
621 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
622 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
623 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
624 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
625 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
626 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
627 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
628 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
629 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
630 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
631 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
632 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
633 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
634 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
635 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
636 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
637 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
638 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
639 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
640 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
641 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Jeykumar Sankaran8f4585f2014-02-05 15:23:40 -0800642 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500643 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
644 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700645 default:
Naseer Ahmedb8ecfbf2015-11-02 20:34:29 -0500646 ALOGE("%s: Unrecognized pixel format: 0x%x", __FUNCTION__, format);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800647 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700648 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700649 return size;
650}
651
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700652unsigned int getBufferSizeAndDimensions(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700653 int& alignedw, int &alignedh)
654{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700655 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700656
657 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
658 height,
659 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800660 0,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700661 alignedw,
662 alignedh);
663
Sushil Chauhan65e26302015-01-14 10:48:57 -0800664 size = getSize(format, width, height, 0 /* usage */, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700665
666 return size;
667}
668
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700669
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700670unsigned int getBufferSizeAndDimensions(int width, int height, int format,
671 int usage, int& alignedw, int &alignedh)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700672{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700673 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700674
675 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
676 height,
677 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800678 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700679 alignedw,
680 alignedh);
681
Sushil Chauhan65e26302015-01-14 10:48:57 -0800682 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700683
684 return size;
685}
686
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700687void getYuvUbwcSPPlaneInfo(uint64_t base, int width, int height,
688 int color_format, struct android_ycbcr* ycbcr)
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800689{
690 // UBWC buffer has these 4 planes in the following sequence:
691 // Y_Meta_Plane, Y_Plane, UV_Meta_Plane, UV_Plane
692 unsigned int y_meta_stride, y_meta_height, y_meta_size;
693 unsigned int y_stride, y_height, y_size;
694 unsigned int c_meta_stride, c_meta_height, c_meta_size;
695 unsigned int alignment = 4096;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800696
697 y_meta_stride = VENUS_Y_META_STRIDE(color_format, width);
698 y_meta_height = VENUS_Y_META_SCANLINES(color_format, height);
699 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
700
701 y_stride = VENUS_Y_STRIDE(color_format, width);
702 y_height = VENUS_Y_SCANLINES(color_format, height);
703 y_size = ALIGN((y_stride * y_height), alignment);
704
705 c_meta_stride = VENUS_UV_META_STRIDE(color_format, width);
706 c_meta_height = VENUS_UV_META_SCANLINES(color_format, height);
707 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
708
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700709 ycbcr->y = (void*)(base + y_meta_size);
710 ycbcr->cb = (void*)(base + y_meta_size + y_size + c_meta_size);
711 ycbcr->cr = (void*)(base + y_meta_size + y_size +
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800712 c_meta_size + 1);
713 ycbcr->ystride = y_stride;
714 ycbcr->cstride = VENUS_UV_STRIDE(color_format, width);
715}
716
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700717void getYuvSPPlaneInfo(uint64_t base, int width, int height, int bpp,
718 struct android_ycbcr* ycbcr)
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800719{
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800720 unsigned int ystride, cstride;
721
722 ystride = cstride = width * bpp;
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700723 ycbcr->y = (void*)base;
724 ycbcr->cb = (void*)(base + ystride * height);
725 ycbcr->cr = (void*)(base + ystride * height + 1);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800726 ycbcr->ystride = ystride;
727 ycbcr->cstride = cstride;
728 ycbcr->chroma_step = 2 * bpp;
729}
730
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400731int getYUVPlaneInfo(private_handle_t* hnd, struct android_ycbcr* ycbcr)
732{
733 int err = 0;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700734 int width = hnd->width;
735 int height = hnd->height;
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700736 int format = hnd->format;
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700737
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700738 unsigned int ystride, cstride;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700739
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400740 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700741 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
742
743 // Check if UBWC buffer has been rendered in linear format.
744 if (metadata && (metadata->operation & LINEAR_FORMAT)) {
745 format = metadata->linearFormat;
746 }
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400747
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700748 // Check metadata if the geometry has been updated.
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700749 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700750 int usage = 0;
751
752 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
753 usage = GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
754 }
755
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700756 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(metadata->bufferDim.sliceWidth,
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700757 metadata->bufferDim.sliceHeight, format, usage, width, height);
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700758 }
759
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400760 // Get the chroma offsets from the handle width/height. We take advantage
761 // of the fact the width _is_ the stride
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700762 switch (format) {
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400763 //Semiplanar
764 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
765 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
766 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
767 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: //Same as YCbCr_420_SP_VENUS
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700768 getYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800769 break;
770
771 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700772 getYuvSPPlaneInfo(hnd->base, width, height, 2, ycbcr);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400773 break;
774
Sushil Chauhan4686c972015-02-20 15:44:52 -0800775 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700776 getYuvUbwcSPPlaneInfo(hnd->base, width, height,
777 COLOR_FMT_NV12_UBWC, ycbcr);
Sushil Chauhan4686c972015-02-20 15:44:52 -0800778 ycbcr->chroma_step = 2;
779 break;
780
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800781 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700782 getYuvUbwcSPPlaneInfo(hnd->base, width, height,
783 COLOR_FMT_NV12_BPP10_UBWC, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800784 ycbcr->chroma_step = 3;
785 break;
786
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400787 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
788 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
789 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530790 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400791 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ajay Dudani4dc06492015-03-26 07:28:11 -0700792 case HAL_PIXEL_FORMAT_RAW16:
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700793 case HAL_PIXEL_FORMAT_RAW10:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700794 getYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800795 std::swap(ycbcr->cb, ycbcr->cr);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400796 break;
797
798 //Planar
799 case HAL_PIXEL_FORMAT_YV12:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700800 ystride = width;
801 cstride = ALIGN(width/2, 16);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400802 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700803 ycbcr->cr = (void*)(hnd->base + ystride * height);
804 ycbcr->cb = (void*)(hnd->base + ystride * height +
805 cstride * height/2);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400806 ycbcr->ystride = ystride;
807 ycbcr->cstride = cstride;
808 ycbcr->chroma_step = 1;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400809 break;
810 //Unsupported formats
811 case HAL_PIXEL_FORMAT_YCbCr_422_I:
812 case HAL_PIXEL_FORMAT_YCrCb_422_I:
813 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
814 default:
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700815 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, format);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400816 err = -EINVAL;
817 }
818 return err;
819
820}
821
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700822
823
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700824// Allocate buffer from width, height and format into a
825// private_handle_t. It is the responsibility of the caller
826// to free the buffer using the free_buffer function
827int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
828{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700829 alloc_data data;
830 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700831 gralloc::IAllocController* sAlloc =
832 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700833 data.base = 0;
834 data.fd = -1;
835 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700836 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
837 alignedh);
838
Naseer Ahmed29a26812012-06-14 00:56:20 -0700839 data.align = getpagesize();
840 data.uncached = useUncached(usage);
841 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700842
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700843 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700844 if (0 != err) {
845 ALOGE("%s: allocate failed", __FUNCTION__);
846 return -ENOMEM;
847 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700848
radhakrishnad7131e62015-10-13 12:32:30 +0530849 if(isUBwcEnabled(format, usage)) {
850 data.allocType |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
851 }
852
Naseer Ahmed29a26812012-06-14 00:56:20 -0700853 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700854 data.allocType, 0, format,
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700855 alignedw, alignedh, -1, 0, 0, w, h);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700856 hnd->base = (uint64_t) data.base;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700857 hnd->offset = data.offset;
858 hnd->gpuaddr = 0;
859 *pHnd = hnd;
860 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700861}
862
863void free_buffer(private_handle_t *hnd)
864{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700865 gralloc::IAllocController* sAlloc =
866 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700867 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700868 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700869 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
870 }
871 if(hnd)
872 delete hnd;
873
874}
Sushil Chauhan65e26302015-01-14 10:48:57 -0800875
876// UBWC helper functions
877static bool isUBwcFormat(int format)
878{
879 // Explicitly defined UBWC formats
880 switch(format)
881 {
882 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800883 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800884 return true;
885 default:
886 return false;
887 }
888}
889
890static bool isUBwcSupported(int format)
891{
Sushil Chauhan01361412016-04-25 16:36:18 -0700892 if (MDPCapabilityInfo::getInstance().isUBwcSupportedByMDP()) {
893 // Existing HAL formats with UBWC support
894 switch(format)
895 {
896 case HAL_PIXEL_FORMAT_BGR_565:
897 case HAL_PIXEL_FORMAT_RGBA_8888:
898 case HAL_PIXEL_FORMAT_RGBX_8888:
899 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
900 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
901 case HAL_PIXEL_FORMAT_RGBA_1010102:
902 case HAL_PIXEL_FORMAT_RGBX_1010102:
903 return true;
904 default:
905 break;
906 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800907 }
Sushil Chauhan01361412016-04-25 16:36:18 -0700908 return false;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800909}
910
911bool isUBwcEnabled(int format, int usage)
912{
Sushil Chauhan81594f62015-01-26 16:00:51 -0800913 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
914 if (isUBwcFormat(format))
915 return true;
916
Manikanta Kanamarlapudicb44d972016-02-03 17:49:55 +0530917 if ((usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) &&
918 gralloc::IAllocController::getInstance()->isDisableUBWCForEncoder()) {
919 return false;
920 }
921
Sushil Chauhan7807d192015-08-13 10:10:48 -0700922 // Allow UBWC, if an OpenGL client sets UBWC usage flag and GPU plus MDP
923 // support the format. OR if a non-OpenGL client like Rotator, sets UBWC
924 // usage flag and MDP supports the format.
925 if ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) && isUBwcSupported(format)) {
926 bool enable = true;
927 // Query GPU for UBWC only if buffer is intended to be used by GPU.
928 if (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER)) {
929 enable = AdrenoMemInfo::getInstance().isUBWCSupportedByGPU(format);
930 }
Sushil Chauhan81594f62015-01-26 16:00:51 -0800931 // Allow UBWC, only if CPU usage flags are not set
Sushil Chauhan7807d192015-08-13 10:10:48 -0700932 if (enable && !(usage & (GRALLOC_USAGE_SW_READ_MASK |
933 GRALLOC_USAGE_SW_WRITE_MASK))) {
Sushil Chauhan65e26302015-01-14 10:48:57 -0800934 return true;
935 }
936 }
937 return false;
938}
939
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800940static void getYuvUBwcWidthHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800941 int& aligned_w, int& aligned_h)
942{
943 switch (format)
944 {
945 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
946 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
947 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
948 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
949 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
950 break;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800951 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Arun Kumar K.Rfc2a27f2016-09-07 18:59:46 -0700952 // The macro returns the stride which is 4/3 times the width, hence * 3/4
953 aligned_w = (VENUS_Y_STRIDE(COLOR_FMT_NV12_BPP10_UBWC, width) * 3) / 4;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800954 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_BPP10_UBWC, height);
955 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800956 default:
957 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
958 aligned_w = 0;
959 aligned_h = 0;
960 break;
961 }
962}
963
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800964static void getRgbUBwcBlockSize(int bpp, int& block_width, int& block_height)
Sushil Chauhan65e26302015-01-14 10:48:57 -0800965{
966 block_width = 0;
967 block_height = 0;
968
969 switch(bpp)
970 {
971 case 2:
972 case 4:
973 block_width = 16;
974 block_height = 4;
975 break;
976 case 8:
977 block_width = 8;
978 block_height = 4;
979 break;
980 case 16:
981 block_width = 4;
982 block_height = 4;
983 break;
984 default:
985 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
986 break;
987 }
988}
989
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800990static unsigned int getRgbUBwcMetaBufferSize(int width, int height, int bpp)
Sushil Chauhan65e26302015-01-14 10:48:57 -0800991{
992 unsigned int size = 0;
993 int meta_width, meta_height;
994 int block_width, block_height;
995
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800996 getRgbUBwcBlockSize(bpp, block_width, block_height);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800997
998 if (!block_width || !block_height) {
999 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
1000 return size;
1001 }
1002
1003 // Align meta buffer height to 16 blocks
1004 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
1005
1006 // Align meta buffer width to 64 blocks
1007 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
1008
1009 // Align meta buffer size to 4K
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001010 size = ALIGN((meta_width * meta_height), 4096);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001011 return size;
1012}
1013
1014static unsigned int getUBwcSize(int width, int height, int format,
1015 const int alignedw, const int alignedh) {
1016
1017 unsigned int size = 0;
1018 switch (format) {
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001019 case HAL_PIXEL_FORMAT_BGR_565:
Sushil Chauhan65e26302015-01-14 10:48:57 -08001020 size = alignedw * alignedh * 2;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001021 size += getRgbUBwcMetaBufferSize(width, height, 2);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001022 break;
1023 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -07001024 case HAL_PIXEL_FORMAT_RGBX_8888:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001025 case HAL_PIXEL_FORMAT_RGBA_1010102:
1026 case HAL_PIXEL_FORMAT_RGBX_1010102:
Sushil Chauhan65e26302015-01-14 10:48:57 -08001027 size = alignedw * alignedh * 4;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001028 size += getRgbUBwcMetaBufferSize(width, height, 4);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001029 break;
1030 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1031 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1032 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1033 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
1034 break;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001035 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1036 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_BPP10_UBWC, width, height);
1037 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -08001038 default:
1039 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
1040 break;
1041 }
1042 return size;
1043}
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001044
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001045int getRgbDataAddress(private_handle_t* hnd, void** rgb_data)
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001046{
1047 int err = 0;
1048
1049 // This api is for RGB* formats
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001050 if (!isUncompressedRgbFormat(hnd->format)) {
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001051 return -EINVAL;
1052 }
1053
1054 // linear buffer
1055 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001056 *rgb_data = (void*)hnd->base;
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001057 return err;
1058 }
1059
Prabhanjan Kandulaebc8ce32016-10-18 12:25:54 -07001060 // Ubwc buffers
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001061 unsigned int meta_size = 0;
1062 switch (hnd->format) {
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001063 case HAL_PIXEL_FORMAT_BGR_565:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001064 meta_size = getRgbUBwcMetaBufferSize(hnd->width, hnd->height, 2);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001065 break;
1066 case HAL_PIXEL_FORMAT_RGBA_8888:
1067 case HAL_PIXEL_FORMAT_RGBX_8888:
Prabhanjan Kandulaebc8ce32016-10-18 12:25:54 -07001068 case HAL_PIXEL_FORMAT_RGBA_1010102:
1069 case HAL_PIXEL_FORMAT_RGBX_1010102:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001070 meta_size = getRgbUBwcMetaBufferSize(hnd->width, hnd->height, 4);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001071 break;
1072 default:
1073 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
1074 err = -EINVAL;
1075 break;
1076 }
1077
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001078 *rgb_data = (void*)(hnd->base + meta_size);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001079 return err;
1080}