blob: 8d43f9803dbf454437d152c81925e78f4b44e29e [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08002 * Copyright (c) 2011 - 2016, 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
Shalaj Jain3c490412015-04-22 16:52:03 -070062#ifdef MASTER_SIDE_CP
63#define CP_HEAP_ID ION_SECURE_HEAP_ID
Arun Kumar K.R7f0b24b2015-07-05 21:20:57 -070064#define SD_HEAP_ID ION_SECURE_DISPLAY_HEAP_ID
Shalaj Jain3c490412015-04-22 16:52:03 -070065#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_CP_PIXEL)
Arun Kumar K.R7f0b24b2015-07-05 21:20:57 -070066#define ION_SD_FLAGS (ION_SECURE | ION_FLAG_CP_SEC_DISPLAY)
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
72#endif
73
Iliyan Malchev202a77d2012-06-11 14:41:12 -070074using namespace gralloc;
Naseer Ahmeda87da602012-07-01 23:54:19 -070075using namespace qdutils;
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -070076using namespace android;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070077
Naomi Luisa44100c2013-02-08 12:42:03 -080078ANDROID_SINGLETON_STATIC_INSTANCE(AdrenoMemInfo);
Ramakant Singhc85ccee2016-04-01 15:25:17 +053079ANDROID_SINGLETON_STATIC_INSTANCE(MDPCapabilityInfo);
Naomi Luisa44100c2013-02-08 12:42:03 -080080
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -080081static void getYuvUBwcWidthHeight(int, int, int, int&, int&);
Sushil Chauhan65e26302015-01-14 10:48:57 -080082static unsigned int getUBwcSize(int, int, int, const int, const int);
83
Iliyan Malchev202a77d2012-06-11 14:41:12 -070084//Common functions
Iliyan Malchev202a77d2012-06-11 14:41:12 -070085
Saurabh Shah1adcafe2014-12-19 10:05:41 -080086/* The default policy is to return cached buffers unless the client explicity
87 * sets the PRIVATE_UNCACHED flag or indicates that the buffer will be rarely
88 * read or written in software. Any combination with a _RARELY_ flag will be
89 * treated as uncached. */
90static bool useUncached(const int& usage) {
Naseer Ahmedde758fd2016-05-03 15:10:23 -040091 if ((usage & GRALLOC_USAGE_PROTECTED) or
92 (usage & GRALLOC_USAGE_PRIVATE_UNCACHED) or
93 ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY) or
94 ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY))
Saurabh Shah1adcafe2014-12-19 10:05:41 -080095 return true;
96
97 return false;
98}
99
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530100//------------- MDPCapabilityInfo-----------------------//
101MDPCapabilityInfo :: MDPCapabilityInfo() {
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530102 qdutils::querySDEInfo(HAS_MACRO_TILE, &isMacroTileSupported);
Sushil Chauhan01361412016-04-25 16:36:18 -0700103 qdutils::querySDEInfo(HAS_UBWC, &isUBwcSupported);
Jeykumar Sankaran9bc1a782015-12-14 18:36:27 -0800104 qdutils::querySDEInfo(HAS_WB_UBWC, &isWBUBWCSupported);
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530105}
106
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530107//------------- AdrenoMemInfo-----------------------//
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800108AdrenoMemInfo::AdrenoMemInfo()
109{
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800110 LINK_adreno_compute_aligned_width_and_height = NULL;
111 LINK_adreno_compute_padding = NULL;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700112 LINK_adreno_isMacroTilingSupportedByGpu = 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");
123 *(void **)&LINK_adreno_isMacroTilingSupportedByGpu =
124 ::dlsym(libadreno_utils, "isMacroTilingSupportedByGpu");
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800125 *(void **)&LINK_adreno_compute_compressedfmt_aligned_width_and_height =
126 ::dlsym(libadreno_utils,
127 "compute_compressedfmt_aligned_width_and_height");
Sushil Chauhan082acd62015-01-14 16:49:29 -0800128 *(void **)&LINK_adreno_isUBWCSupportedByGpu =
129 ::dlsym(libadreno_utils, "isUBWCSupportedByGpu");
Sushil Chauhan521ce352015-08-28 11:33:30 -0700130 *(void **)&LINK_adreno_get_gpu_pixel_alignment =
131 ::dlsym(libadreno_utils, "get_gpu_pixel_alignment");
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800132 }
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700133
134 // Check if the overriding property debug.gralloc.gfx_ubwc_disable
135 // that disables UBWC allocations for the graphics stack is set
136 gfx_ubwc_disable = 0;
137 char property[PROPERTY_VALUE_MAX];
138 property_get("debug.gralloc.gfx_ubwc_disable", property, "0");
139 if(!(strncmp(property, "1", PROPERTY_VALUE_MAX)) ||
140 !(strncmp(property, "true", PROPERTY_VALUE_MAX))) {
141 gfx_ubwc_disable = 1;
142 }
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800143}
144
145AdrenoMemInfo::~AdrenoMemInfo()
146{
147 if (libadreno_utils) {
148 ::dlclose(libadreno_utils);
149 }
150}
151
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700152int AdrenoMemInfo::isMacroTilingSupportedByGPU()
153{
154 if ((libadreno_utils)) {
155 if(LINK_adreno_isMacroTilingSupportedByGpu) {
156 return LINK_adreno_isMacroTilingSupportedByGpu();
157 }
158 }
159 return 0;
160}
161
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700162void AdrenoMemInfo::getAlignedWidthAndHeight(const private_handle_t *hnd, int& aligned_w,
163 int& aligned_h) {
164 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
165 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
166 int w = metadata->bufferDim.sliceWidth;
167 int h = metadata->bufferDim.sliceHeight;
168 int f = hnd->format;
169 int usage = 0;
170
171 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
172 usage = GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
173 }
174
175 getAlignedWidthAndHeight(w, h, f, usage, aligned_w, aligned_h);
176 } else {
177 aligned_w = hnd->width;
178 aligned_h = hnd->height;
179 }
180
181}
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700182
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700183void AdrenoMemInfo::getUnalignedWidthAndHeight(const private_handle_t *hnd, int& unaligned_w,
184 int& unaligned_h) {
185 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
186 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
187 unaligned_w = metadata->bufferDim.sliceWidth;
188 unaligned_h = metadata->bufferDim.sliceHeight;
189 } else {
190 unaligned_w = hnd->unaligned_width;
191 unaligned_h = hnd->unaligned_height;
192 }
193}
194
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700195bool isUncompressedRgbFormat(int format)
196{
197 bool is_rgb_format = false;
198
199 switch (format)
200 {
201 case HAL_PIXEL_FORMAT_RGBA_8888:
202 case HAL_PIXEL_FORMAT_RGBX_8888:
203 case HAL_PIXEL_FORMAT_RGB_888:
204 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600205 case HAL_PIXEL_FORMAT_BGR_565:
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700206 case HAL_PIXEL_FORMAT_BGRA_8888:
207 case HAL_PIXEL_FORMAT_RGBA_5551:
208 case HAL_PIXEL_FORMAT_RGBA_4444:
209 case HAL_PIXEL_FORMAT_R_8:
210 case HAL_PIXEL_FORMAT_RG_88:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800211 case HAL_PIXEL_FORMAT_BGRX_8888:
212 case HAL_PIXEL_FORMAT_RGBA_1010102:
213 case HAL_PIXEL_FORMAT_ARGB_2101010:
214 case HAL_PIXEL_FORMAT_RGBX_1010102:
215 case HAL_PIXEL_FORMAT_XRGB_2101010:
216 case HAL_PIXEL_FORMAT_BGRA_1010102:
217 case HAL_PIXEL_FORMAT_ABGR_2101010:
218 case HAL_PIXEL_FORMAT_BGRX_1010102:
219 case HAL_PIXEL_FORMAT_XBGR_2101010: // Intentional fallthrough
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700220 is_rgb_format = true;
221 break;
222 default:
223 break;
224 }
225
226 return is_rgb_format;
227}
228
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800229void AdrenoMemInfo::getAlignedWidthAndHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800230 int usage, int& aligned_w, int& aligned_h)
Naomi Luisa44100c2013-02-08 12:42:03 -0800231{
Sushil Chauhane61fac52015-04-30 17:14:37 -0700232 bool ubwc_enabled = isUBwcEnabled(format, usage);
233
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800234 // Currently surface padding is only computed for RGB* surfaces.
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700235 if (isUncompressedRgbFormat(format) == true) {
Sushil Chauhane61fac52015-04-30 17:14:37 -0700236 int tileEnabled = ubwc_enabled || isMacroTileEnabled(format, usage);
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700237 getGpuAlignedWidthHeight(width, height, format, tileEnabled, aligned_w, aligned_h);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800238 } else if (ubwc_enabled) {
239 getYuvUBwcWidthHeight(width, height, format, aligned_w, aligned_h);
240 } else {
241 aligned_w = width;
242 aligned_h = height;
243 int alignment = 32;
244 switch (format)
245 {
246 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
247 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
248 if (LINK_adreno_get_gpu_pixel_alignment) {
249 alignment = LINK_adreno_get_gpu_pixel_alignment();
250 }
251 aligned_w = ALIGN(width, alignment);
252 break;
253 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
254 aligned_w = ALIGN(width, alignment);
255 break;
256 case HAL_PIXEL_FORMAT_RAW16:
257 aligned_w = ALIGN(width, 16);
258 break;
259 case HAL_PIXEL_FORMAT_RAW10:
260 aligned_w = ALIGN(width * 10 / 8, 8);
261 break;
262 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
263 aligned_w = ALIGN(width, 128);
264 break;
265 case HAL_PIXEL_FORMAT_YV12:
266 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
267 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
268 case HAL_PIXEL_FORMAT_YCbCr_422_I:
269 case HAL_PIXEL_FORMAT_YCrCb_422_I:
270 aligned_w = ALIGN(width, 16);
271 break;
272 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
273 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
274 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
275 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
276 break;
277 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
278 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV21, width);
279 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV21, height);
280 break;
281 case HAL_PIXEL_FORMAT_BLOB:
Naseer Ahmed6c6c58a2016-05-03 16:27:17 -0400282 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800283 break;
284 case HAL_PIXEL_FORMAT_NV21_ZSL:
285 aligned_w = ALIGN(width, 64);
286 aligned_h = ALIGN(height, 64);
287 break;
288 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
289 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
290 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
291 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
292 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
293 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
294 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
295 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
296 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
297 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
298 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
299 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
300 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
301 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
302 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
303 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
304 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
305 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
306 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
307 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
308 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
309 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
310 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
311 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
312 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
313 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
314 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
315 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
316 if(LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
317 int bytesPerPixel = 0;
318 int raster_mode = 0; //Adreno unknown raster mode.
319 int padding_threshold = 512; //Threshold for padding
320 //surfaces.
Sushil Chauhan65e26302015-01-14 10:48:57 -0800321
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800322 LINK_adreno_compute_compressedfmt_aligned_width_and_height(
323 width, height, format, 0,raster_mode, padding_threshold,
324 &aligned_w, &aligned_h, &bytesPerPixel);
325 } else {
326 ALOGW("%s: Warning!! Symbols" \
327 " compute_compressedfmt_aligned_width_and_height" \
328 " not found", __FUNCTION__);
329 }
330 break;
331 default: break;
332 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800333 }
334}
335
336void AdrenoMemInfo::getGpuAlignedWidthHeight(int width, int height, int format,
337 int tile_enabled, int& aligned_w, int& aligned_h)
338{
339 aligned_w = ALIGN(width, 32);
340 aligned_h = ALIGN(height, 32);
341
342 // Don't add any additional padding if debug.gralloc.map_fb_memory
343 // is enabled
344 char property[PROPERTY_VALUE_MAX];
345 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
346 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
347 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
348 return;
349 }
350
351 int bpp = 4;
352 switch(format)
353 {
354 case HAL_PIXEL_FORMAT_RGB_888:
355 bpp = 3;
356 break;
357 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600358 case HAL_PIXEL_FORMAT_BGR_565:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800359 case HAL_PIXEL_FORMAT_RGBA_5551:
360 case HAL_PIXEL_FORMAT_RGBA_4444:
361 bpp = 2;
362 break;
363 default: break;
364 }
365
366 if (libadreno_utils) {
367 int raster_mode = 0; // Adreno unknown raster mode.
368 int padding_threshold = 512; // Threshold for padding surfaces.
369 // the function below computes aligned width and aligned height
370 // based on linear or macro tile mode selected.
371 if(LINK_adreno_compute_aligned_width_and_height) {
372 LINK_adreno_compute_aligned_width_and_height(width,
373 height, bpp, tile_enabled,
374 raster_mode, padding_threshold,
375 &aligned_w, &aligned_h);
376
377 } else if(LINK_adreno_compute_padding) {
378 int surface_tile_height = 1; // Linear surface
379 aligned_w = LINK_adreno_compute_padding(width, bpp,
380 surface_tile_height, raster_mode,
381 padding_threshold);
382 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
383 __FUNCTION__);
384 } else {
385 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
386 "compute_aligned_width_and_height not found", __FUNCTION__);
387 }
388 }
389}
390
391int AdrenoMemInfo::isUBWCSupportedByGPU(int format)
392{
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700393 if (!gfx_ubwc_disable && libadreno_utils) {
Sushil Chauhan082acd62015-01-14 16:49:29 -0800394 if (LINK_adreno_isUBWCSupportedByGpu) {
395 ADRENOPIXELFORMAT gpu_format = getGpuPixelFormat(format);
396 return LINK_adreno_isUBWCSupportedByGpu(gpu_format);
397 }
398 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800399 return 0;
Naomi Luisa44100c2013-02-08 12:42:03 -0800400}
401
Sushil Chauhan082acd62015-01-14 16:49:29 -0800402ADRENOPIXELFORMAT AdrenoMemInfo::getGpuPixelFormat(int hal_format)
403{
404 switch (hal_format) {
405 case HAL_PIXEL_FORMAT_RGBA_8888:
406 return ADRENO_PIXELFORMAT_R8G8B8A8;
Sushil Chauhan6686c802015-04-15 11:30:39 -0700407 case HAL_PIXEL_FORMAT_RGBX_8888:
408 return ADRENO_PIXELFORMAT_R8G8B8X8;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800409 case HAL_PIXEL_FORMAT_RGB_565:
410 return ADRENO_PIXELFORMAT_B5G6R5;
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600411 case HAL_PIXEL_FORMAT_BGR_565:
412 return ADRENO_PIXELFORMAT_R5G6B5;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800413 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800414 return ADRENO_PIXELFORMAT_NV12;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800415 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
416 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800417 return ADRENO_PIXELFORMAT_NV12_EXT;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800418 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
419 return ADRENO_PIXELFORMAT_TP10;
420 case HAL_PIXEL_FORMAT_RGBA_1010102:
421 return ADRENO_PIXELFORMAT_R10G10B10A2_UNORM;
422 case HAL_PIXEL_FORMAT_RGBX_1010102:
423 return ADRENO_PIXELFORMAT_R10G10B10X2_UNORM;
424 case HAL_PIXEL_FORMAT_ABGR_2101010:
425 return ADRENO_PIXELFORMAT_A2B10G10R10_UNORM;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800426 default:
427 ALOGE("%s: No map for format: 0x%x", __FUNCTION__, hal_format);
428 break;
429 }
430 return ADRENO_PIXELFORMAT_UNKNOWN;
431}
432
Naomi Luisa44100c2013-02-08 12:42:03 -0800433//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700434IAllocController* IAllocController::sController = NULL;
435IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700436{
437 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700438 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700439 }
440 return sController;
441}
442
443
444//-------------- IonController-----------------------//
445IonController::IonController()
446{
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530447 allocateIonMem();
Manikanta Kanamarlapudicb44d972016-02-03 17:49:55 +0530448
449 char property[PROPERTY_VALUE_MAX];
450 property_get("video.disable.ubwc", property, "0");
451 mDisableUBWCForEncode = atoi(property);
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530452}
453
454void IonController::allocateIonMem()
455{
456 mIonAlloc = new IonAlloc();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700457}
458
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700459int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700460{
461 int ionFlags = 0;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500462 int ionHeapId = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700463 int ret;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700464
465 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700466 data.allocType = 0;
467
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530468 if(usage & GRALLOC_USAGE_PROTECTED) {
Naseer Ahmed934a4c32016-04-11 17:20:02 -0400469 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
470 ionHeapId = ION_HEAP(SD_HEAP_ID);
471 /*
472 * There is currently no flag in ION for Secure Display
473 * VM. Please add it to the define once available.
474 */
475 ionFlags |= ION_SD_FLAGS;
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530476 } else {
Naseer Ahmed934a4c32016-04-11 17:20:02 -0400477 ionHeapId = ION_HEAP(CP_HEAP_ID);
478 ionFlags |= ION_CP_FLAGS;
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500479 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530480 } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
481 //MM Heap is exclusively a secure heap.
482 //If it is used for non secure cases, fallback to IOMMU heap
483 ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
484 cannot be used as an insecure heap!\
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500485 trying to use system heap instead !!");
486 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500487 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700488
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700489 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500490 ionHeapId |= ION_HEAP(ION_CAMERA_HEAP_ID);
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700491
Arun Kumar K.R0daaa992013-03-12 15:08:29 -0700492 if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500493 ionHeapId |= ION_HEAP(ION_ADSP_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700494
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530495 if(ionFlags & ION_SECURE)
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500496 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700497
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500498 // if no ion heap flags are set, default to system heap
499 if(!ionHeapId)
500 ionHeapId = ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700501
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500502 //At this point we should have the right heap set, there is no fallback
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700503 data.flags = ionFlags;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500504 data.heapId = ionHeapId;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700505 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700506
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700507 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700508 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500509 } else {
510 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x",
511 __FUNCTION__, ionHeapId, ionFlags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700512 }
513
514 return ret;
515}
516
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700517IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700518{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700519 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700520 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
521 memalloc = mIonAlloc;
522 } else {
523 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
524 }
525
526 return memalloc;
527}
528
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700529bool isMacroTileEnabled(int format, int usage)
530{
531 bool tileEnabled = false;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700532 // Check whether GPU & MDSS supports MacroTiling feature
533 if(AdrenoMemInfo::getInstance().isMacroTilingSupportedByGPU() &&
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530534 MDPCapabilityInfo::getInstance().isMacroTilingSupportedByMDP())
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700535 {
536 // check the format
537 switch(format)
538 {
539 case HAL_PIXEL_FORMAT_RGBA_8888:
540 case HAL_PIXEL_FORMAT_RGBX_8888:
541 case HAL_PIXEL_FORMAT_BGRA_8888:
Manoj Kumar AVM5a5529b2014-02-24 18:16:37 -0800542 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600543 case HAL_PIXEL_FORMAT_BGR_565:
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700544 {
545 tileEnabled = true;
546 // check the usage flags
547 if (usage & (GRALLOC_USAGE_SW_READ_MASK |
548 GRALLOC_USAGE_SW_WRITE_MASK)) {
549 // Application intends to use CPU for rendering
550 tileEnabled = false;
551 }
552 break;
553 }
554 default:
555 break;
556 }
557 }
558 return tileEnabled;
559}
560
561// helper function
Sushil Chauhan65e26302015-01-14 10:48:57 -0800562unsigned int getSize(int format, int width, int height, int usage,
563 const int alignedw, const int alignedh) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700564
Sushil Chauhan65e26302015-01-14 10:48:57 -0800565 if (isUBwcEnabled(format, usage)) {
566 return getUBwcSize(width, height, format, alignedw, alignedh);
567 }
568
569 unsigned int size = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700570 switch (format) {
571 case HAL_PIXEL_FORMAT_RGBA_8888:
572 case HAL_PIXEL_FORMAT_RGBX_8888:
573 case HAL_PIXEL_FORMAT_BGRA_8888:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800574 case HAL_PIXEL_FORMAT_RGBA_1010102:
575 case HAL_PIXEL_FORMAT_ARGB_2101010:
576 case HAL_PIXEL_FORMAT_RGBX_1010102:
577 case HAL_PIXEL_FORMAT_XRGB_2101010:
578 case HAL_PIXEL_FORMAT_BGRA_1010102:
579 case HAL_PIXEL_FORMAT_ABGR_2101010:
580 case HAL_PIXEL_FORMAT_BGRX_1010102:
581 case HAL_PIXEL_FORMAT_XBGR_2101010:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700582 size = alignedw * alignedh * 4;
583 break;
584 case HAL_PIXEL_FORMAT_RGB_888:
585 size = alignedw * alignedh * 3;
586 break;
587 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600588 case HAL_PIXEL_FORMAT_BGR_565:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700589 case HAL_PIXEL_FORMAT_RGBA_5551:
590 case HAL_PIXEL_FORMAT_RGBA_4444:
Ajay Dudani4dc06492015-03-26 07:28:11 -0700591 case HAL_PIXEL_FORMAT_RAW16:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700592 size = alignedw * alignedh * 2;
593 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700594 case HAL_PIXEL_FORMAT_RAW10:
595 size = ALIGN(alignedw * alignedh, 4096);
596 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700597
598 // adreno formats
599 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
600 size = ALIGN(alignedw*alignedh, 4096);
601 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
602 break;
603 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
604 // The chroma plane is subsampled,
605 // but the pitch in bytes is unchanged
606 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700607 size = ALIGN( alignedw * alignedh, 8192);
608 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
609 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700610 case HAL_PIXEL_FORMAT_YV12:
611 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
612 ALOGE("w or h is odd for the YV12 format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800613 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700614 }
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400615 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700616 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700617 size = ALIGN(size, (unsigned int)4096);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700618 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800619 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
620 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naseer Ahmed2c215292013-09-18 23:47:42 -0400621 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800622 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700623 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
624 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700625 case HAL_PIXEL_FORMAT_YCbCr_422_I:
626 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700627 if(width & 1) {
628 ALOGE("width is odd for the YUV422_SP format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800629 return 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700630 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700631 size = ALIGN(alignedw * alignedh * 2, 4096);
632 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700633 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400634 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhane8a01792012-11-01 16:25:45 -0700635 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700636 break;
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530637 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
638 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
639 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400640 case HAL_PIXEL_FORMAT_BLOB:
Naseer Ahmed6c6c58a2016-05-03 16:27:17 -0400641 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400642 if(height != 1) {
643 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
644 must have height==1 ", __FUNCTION__);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800645 return 0;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400646 }
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400647 size = width;
648 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700649 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700650 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
651 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500652 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
653 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
654 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
655 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
656 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
657 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
658 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
659 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
660 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
661 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
662 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
663 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
664 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
665 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
666 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
667 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
668 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
669 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
670 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
671 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
672 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
673 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
674 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
675 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
676 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
677 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
678 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Jeykumar Sankaran8f4585f2014-02-05 15:23:40 -0800679 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500680 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
681 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700682 default:
Naseer Ahmedb8ecfbf2015-11-02 20:34:29 -0500683 ALOGE("%s: Unrecognized pixel format: 0x%x", __FUNCTION__, format);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800684 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700685 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700686 return size;
687}
688
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700689unsigned int getBufferSizeAndDimensions(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700690 int& alignedw, int &alignedh)
691{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700692 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700693
694 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
695 height,
696 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800697 0,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700698 alignedw,
699 alignedh);
700
Sushil Chauhan65e26302015-01-14 10:48:57 -0800701 size = getSize(format, width, height, 0 /* usage */, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700702
703 return size;
704}
705
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700706
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700707unsigned int getBufferSizeAndDimensions(int width, int height, int format,
708 int usage, int& alignedw, int &alignedh)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700709{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700710 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700711
712 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
713 height,
714 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800715 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700716 alignedw,
717 alignedh);
718
Sushil Chauhan65e26302015-01-14 10:48:57 -0800719 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700720
721 return size;
722}
723
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700724void getBufferAttributes(int width, int height, int format, int usage,
Sushil Chauhane61fac52015-04-30 17:14:37 -0700725 int& alignedw, int &alignedh, int& tiled, unsigned int& size)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700726{
Sushil Chauhane61fac52015-04-30 17:14:37 -0700727 tiled = isUBwcEnabled(format, usage) || isMacroTileEnabled(format, usage);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700728
729 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
730 height,
731 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800732 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700733 alignedw,
734 alignedh);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800735 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700736}
737
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700738void getYuvUbwcSPPlaneInfo(uint64_t base, int width, int height,
739 int color_format, struct android_ycbcr* ycbcr)
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800740{
741 // UBWC buffer has these 4 planes in the following sequence:
742 // Y_Meta_Plane, Y_Plane, UV_Meta_Plane, UV_Plane
743 unsigned int y_meta_stride, y_meta_height, y_meta_size;
744 unsigned int y_stride, y_height, y_size;
745 unsigned int c_meta_stride, c_meta_height, c_meta_size;
746 unsigned int alignment = 4096;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800747
748 y_meta_stride = VENUS_Y_META_STRIDE(color_format, width);
749 y_meta_height = VENUS_Y_META_SCANLINES(color_format, height);
750 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
751
752 y_stride = VENUS_Y_STRIDE(color_format, width);
753 y_height = VENUS_Y_SCANLINES(color_format, height);
754 y_size = ALIGN((y_stride * y_height), alignment);
755
756 c_meta_stride = VENUS_UV_META_STRIDE(color_format, width);
757 c_meta_height = VENUS_UV_META_SCANLINES(color_format, height);
758 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
759
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700760 ycbcr->y = (void*)(base + y_meta_size);
761 ycbcr->cb = (void*)(base + y_meta_size + y_size + c_meta_size);
762 ycbcr->cr = (void*)(base + y_meta_size + y_size +
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800763 c_meta_size + 1);
764 ycbcr->ystride = y_stride;
765 ycbcr->cstride = VENUS_UV_STRIDE(color_format, width);
766}
767
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700768void getYuvSPPlaneInfo(uint64_t base, int width, int height, int bpp,
769 struct android_ycbcr* ycbcr)
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800770{
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800771 unsigned int ystride, cstride;
772
773 ystride = cstride = width * bpp;
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700774 ycbcr->y = (void*)base;
775 ycbcr->cb = (void*)(base + ystride * height);
776 ycbcr->cr = (void*)(base + ystride * height + 1);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800777 ycbcr->ystride = ystride;
778 ycbcr->cstride = cstride;
779 ycbcr->chroma_step = 2 * bpp;
780}
781
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400782int getYUVPlaneInfo(private_handle_t* hnd, struct android_ycbcr* ycbcr)
783{
784 int err = 0;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700785 int width = hnd->width;
786 int height = hnd->height;
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700787 int format = hnd->format;
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700788
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700789 unsigned int ystride, cstride;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700790
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400791 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700792 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
793
794 // Check if UBWC buffer has been rendered in linear format.
795 if (metadata && (metadata->operation & LINEAR_FORMAT)) {
796 format = metadata->linearFormat;
797 }
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400798
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700799 // Check metadata if the geometry has been updated.
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700800 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700801 int usage = 0;
802
803 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
804 usage = GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
805 }
806
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700807 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(metadata->bufferDim.sliceWidth,
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700808 metadata->bufferDim.sliceHeight, format, usage, width, height);
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700809 }
810
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400811 // Get the chroma offsets from the handle width/height. We take advantage
812 // of the fact the width _is_ the stride
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700813 switch (format) {
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400814 //Semiplanar
815 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
816 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
817 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
818 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: //Same as YCbCr_420_SP_VENUS
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700819 getYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800820 break;
821
822 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700823 getYuvSPPlaneInfo(hnd->base, width, height, 2, ycbcr);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400824 break;
825
Sushil Chauhan4686c972015-02-20 15:44:52 -0800826 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700827 getYuvUbwcSPPlaneInfo(hnd->base, width, height,
828 COLOR_FMT_NV12_UBWC, ycbcr);
Sushil Chauhan4686c972015-02-20 15:44:52 -0800829 ycbcr->chroma_step = 2;
830 break;
831
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800832 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700833 getYuvUbwcSPPlaneInfo(hnd->base, width, height,
834 COLOR_FMT_NV12_BPP10_UBWC, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800835 ycbcr->chroma_step = 3;
836 break;
837
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400838 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
839 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
840 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530841 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400842 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ajay Dudani4dc06492015-03-26 07:28:11 -0700843 case HAL_PIXEL_FORMAT_RAW16:
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700844 case HAL_PIXEL_FORMAT_RAW10:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700845 getYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800846 std::swap(ycbcr->cb, ycbcr->cr);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400847 break;
848
849 //Planar
850 case HAL_PIXEL_FORMAT_YV12:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700851 ystride = width;
852 cstride = ALIGN(width/2, 16);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400853 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700854 ycbcr->cr = (void*)(hnd->base + ystride * height);
855 ycbcr->cb = (void*)(hnd->base + ystride * height +
856 cstride * height/2);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400857 ycbcr->ystride = ystride;
858 ycbcr->cstride = cstride;
859 ycbcr->chroma_step = 1;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400860 break;
861 //Unsupported formats
862 case HAL_PIXEL_FORMAT_YCbCr_422_I:
863 case HAL_PIXEL_FORMAT_YCrCb_422_I:
864 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
865 default:
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700866 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, format);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400867 err = -EINVAL;
868 }
869 return err;
870
871}
872
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700873
874
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700875// Allocate buffer from width, height and format into a
876// private_handle_t. It is the responsibility of the caller
877// to free the buffer using the free_buffer function
878int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
879{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700880 alloc_data data;
881 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700882 gralloc::IAllocController* sAlloc =
883 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700884 data.base = 0;
885 data.fd = -1;
886 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700887 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
888 alignedh);
889
Naseer Ahmed29a26812012-06-14 00:56:20 -0700890 data.align = getpagesize();
891 data.uncached = useUncached(usage);
892 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700893
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700894 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700895 if (0 != err) {
896 ALOGE("%s: allocate failed", __FUNCTION__);
897 return -ENOMEM;
898 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700899
radhakrishnad7131e62015-10-13 12:32:30 +0530900 if(isUBwcEnabled(format, usage)) {
901 data.allocType |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
902 }
903
Naseer Ahmed29a26812012-06-14 00:56:20 -0700904 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700905 data.allocType, 0, format,
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700906 alignedw, alignedh, -1, 0, 0, w, h);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700907 hnd->base = (uint64_t) data.base;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700908 hnd->offset = data.offset;
909 hnd->gpuaddr = 0;
910 *pHnd = hnd;
911 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700912}
913
914void free_buffer(private_handle_t *hnd)
915{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700916 gralloc::IAllocController* sAlloc =
917 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700918 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700919 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700920 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
921 }
922 if(hnd)
923 delete hnd;
924
925}
Sushil Chauhan65e26302015-01-14 10:48:57 -0800926
927// UBWC helper functions
928static bool isUBwcFormat(int format)
929{
930 // Explicitly defined UBWC formats
931 switch(format)
932 {
933 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800934 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800935 return true;
936 default:
937 return false;
938 }
939}
940
941static bool isUBwcSupported(int format)
942{
Sushil Chauhan01361412016-04-25 16:36:18 -0700943 if (MDPCapabilityInfo::getInstance().isUBwcSupportedByMDP()) {
944 // Existing HAL formats with UBWC support
945 switch(format)
946 {
947 case HAL_PIXEL_FORMAT_BGR_565:
948 case HAL_PIXEL_FORMAT_RGBA_8888:
949 case HAL_PIXEL_FORMAT_RGBX_8888:
950 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
951 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
952 case HAL_PIXEL_FORMAT_RGBA_1010102:
953 case HAL_PIXEL_FORMAT_RGBX_1010102:
954 return true;
955 default:
956 break;
957 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800958 }
Sushil Chauhan01361412016-04-25 16:36:18 -0700959 return false;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800960}
961
962bool isUBwcEnabled(int format, int usage)
963{
Sushil Chauhan81594f62015-01-26 16:00:51 -0800964 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
965 if (isUBwcFormat(format))
966 return true;
967
Manikanta Kanamarlapudicb44d972016-02-03 17:49:55 +0530968 if ((usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) &&
969 gralloc::IAllocController::getInstance()->isDisableUBWCForEncoder()) {
970 return false;
971 }
972
Sushil Chauhan7807d192015-08-13 10:10:48 -0700973 // Allow UBWC, if an OpenGL client sets UBWC usage flag and GPU plus MDP
974 // support the format. OR if a non-OpenGL client like Rotator, sets UBWC
975 // usage flag and MDP supports the format.
976 if ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) && isUBwcSupported(format)) {
977 bool enable = true;
978 // Query GPU for UBWC only if buffer is intended to be used by GPU.
979 if (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER)) {
980 enable = AdrenoMemInfo::getInstance().isUBWCSupportedByGPU(format);
981 }
Sushil Chauhan81594f62015-01-26 16:00:51 -0800982 // Allow UBWC, only if CPU usage flags are not set
Sushil Chauhan7807d192015-08-13 10:10:48 -0700983 if (enable && !(usage & (GRALLOC_USAGE_SW_READ_MASK |
984 GRALLOC_USAGE_SW_WRITE_MASK))) {
Sushil Chauhan65e26302015-01-14 10:48:57 -0800985 return true;
986 }
987 }
988 return false;
989}
990
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800991static void getYuvUBwcWidthHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800992 int& aligned_w, int& aligned_h)
993{
994 switch (format)
995 {
996 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
997 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
998 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
999 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
1000 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
1001 break;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001002 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1003 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_BPP10_UBWC, width);
1004 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_BPP10_UBWC, height);
1005 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -08001006 default:
1007 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
1008 aligned_w = 0;
1009 aligned_h = 0;
1010 break;
1011 }
1012}
1013
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001014static void getRgbUBwcBlockSize(int bpp, int& block_width, int& block_height)
Sushil Chauhan65e26302015-01-14 10:48:57 -08001015{
1016 block_width = 0;
1017 block_height = 0;
1018
1019 switch(bpp)
1020 {
1021 case 2:
1022 case 4:
1023 block_width = 16;
1024 block_height = 4;
1025 break;
1026 case 8:
1027 block_width = 8;
1028 block_height = 4;
1029 break;
1030 case 16:
1031 block_width = 4;
1032 block_height = 4;
1033 break;
1034 default:
1035 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
1036 break;
1037 }
1038}
1039
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001040static unsigned int getRgbUBwcMetaBufferSize(int width, int height, int bpp)
Sushil Chauhan65e26302015-01-14 10:48:57 -08001041{
1042 unsigned int size = 0;
1043 int meta_width, meta_height;
1044 int block_width, block_height;
1045
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001046 getRgbUBwcBlockSize(bpp, block_width, block_height);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001047
1048 if (!block_width || !block_height) {
1049 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
1050 return size;
1051 }
1052
1053 // Align meta buffer height to 16 blocks
1054 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
1055
1056 // Align meta buffer width to 64 blocks
1057 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
1058
1059 // Align meta buffer size to 4K
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001060 size = ALIGN((meta_width * meta_height), 4096);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001061 return size;
1062}
1063
1064static unsigned int getUBwcSize(int width, int height, int format,
1065 const int alignedw, const int alignedh) {
1066
1067 unsigned int size = 0;
1068 switch (format) {
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001069 case HAL_PIXEL_FORMAT_BGR_565:
Sushil Chauhan65e26302015-01-14 10:48:57 -08001070 size = alignedw * alignedh * 2;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001071 size += getRgbUBwcMetaBufferSize(width, height, 2);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001072 break;
1073 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -07001074 case HAL_PIXEL_FORMAT_RGBX_8888:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001075 case HAL_PIXEL_FORMAT_RGBA_1010102:
1076 case HAL_PIXEL_FORMAT_RGBX_1010102:
Sushil Chauhan65e26302015-01-14 10:48:57 -08001077 size = alignedw * alignedh * 4;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001078 size += getRgbUBwcMetaBufferSize(width, height, 4);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001079 break;
1080 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1081 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1082 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1083 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
1084 break;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001085 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1086 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_BPP10_UBWC, width, height);
1087 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -08001088 default:
1089 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
1090 break;
1091 }
1092 return size;
1093}
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001094
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001095int getRgbDataAddress(private_handle_t* hnd, void** rgb_data)
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001096{
1097 int err = 0;
1098
1099 // This api is for RGB* formats
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001100 if (!isUncompressedRgbFormat(hnd->format)) {
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001101 return -EINVAL;
1102 }
1103
1104 // linear buffer
1105 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001106 *rgb_data = (void*)hnd->base;
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001107 return err;
1108 }
1109
1110 unsigned int meta_size = 0;
1111 switch (hnd->format) {
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001112 case HAL_PIXEL_FORMAT_BGR_565:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001113 meta_size = getRgbUBwcMetaBufferSize(hnd->width, hnd->height, 2);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001114 break;
1115 case HAL_PIXEL_FORMAT_RGBA_8888:
1116 case HAL_PIXEL_FORMAT_RGBX_8888:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001117 meta_size = getRgbUBwcMetaBufferSize(hnd->width, hnd->height, 4);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001118 break;
1119 default:
1120 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
1121 err = -EINVAL;
1122 break;
1123 }
1124
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001125 *rgb_data = (void*)(hnd->base + meta_size);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001126 return err;
1127}