blob: a7e00c5d1731dba6f51b4f0343823b84d1eb3755 [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
Mansoor Aftabe9912a62014-07-15 01:40:26 -07002 * Copyright (c) 2011-2015, 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>
42
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);
79
Sushil Chauhan65e26302015-01-14 10:48:57 -080080static void getUBwcWidthAndHeight(int, int, int, int&, int&);
81static unsigned int getUBwcSize(int, int, int, const int, const int);
82
Iliyan Malchev202a77d2012-06-11 14:41:12 -070083//Common functions
Iliyan Malchev202a77d2012-06-11 14:41:12 -070084
Saurabh Shah1adcafe2014-12-19 10:05:41 -080085/* The default policy is to return cached buffers unless the client explicity
86 * sets the PRIVATE_UNCACHED flag or indicates that the buffer will be rarely
87 * read or written in software. Any combination with a _RARELY_ flag will be
88 * treated as uncached. */
89static bool useUncached(const int& usage) {
90 if((usage & GRALLOC_USAGE_PRIVATE_UNCACHED) or
91 ((usage & GRALLOC_USAGE_SW_WRITE_MASK) ==
92 GRALLOC_USAGE_SW_WRITE_RARELY) or
93 ((usage & GRALLOC_USAGE_SW_READ_MASK) ==
94 GRALLOC_USAGE_SW_READ_RARELY))
95 return true;
96
97 return false;
98}
99
Naomi Luisa44100c2013-02-08 12:42:03 -0800100//-------------- AdrenoMemInfo-----------------------//
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800101AdrenoMemInfo::AdrenoMemInfo()
102{
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800103 LINK_adreno_compute_aligned_width_and_height = NULL;
104 LINK_adreno_compute_padding = NULL;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700105 LINK_adreno_isMacroTilingSupportedByGpu = NULL;
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800106 LINK_adreno_compute_compressedfmt_aligned_width_and_height = NULL;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800107 LINK_adreno_isUBWCSupportedByGpu = NULL;
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800108
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800109 libadreno_utils = ::dlopen("libadreno_utils.so", RTLD_NOW);
110 if (libadreno_utils) {
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800111 *(void **)&LINK_adreno_compute_aligned_width_and_height =
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700112 ::dlsym(libadreno_utils, "compute_aligned_width_and_height");
113 *(void **)&LINK_adreno_compute_padding =
114 ::dlsym(libadreno_utils, "compute_surface_padding");
115 *(void **)&LINK_adreno_isMacroTilingSupportedByGpu =
116 ::dlsym(libadreno_utils, "isMacroTilingSupportedByGpu");
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800117 *(void **)&LINK_adreno_compute_compressedfmt_aligned_width_and_height =
118 ::dlsym(libadreno_utils,
119 "compute_compressedfmt_aligned_width_and_height");
Sushil Chauhan082acd62015-01-14 16:49:29 -0800120 *(void **)&LINK_adreno_isUBWCSupportedByGpu =
121 ::dlsym(libadreno_utils, "isUBWCSupportedByGpu");
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800122 }
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700123
124 // Check if the overriding property debug.gralloc.gfx_ubwc_disable
125 // that disables UBWC allocations for the graphics stack is set
126 gfx_ubwc_disable = 0;
127 char property[PROPERTY_VALUE_MAX];
128 property_get("debug.gralloc.gfx_ubwc_disable", property, "0");
129 if(!(strncmp(property, "1", PROPERTY_VALUE_MAX)) ||
130 !(strncmp(property, "true", PROPERTY_VALUE_MAX))) {
131 gfx_ubwc_disable = 1;
132 }
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800133}
134
135AdrenoMemInfo::~AdrenoMemInfo()
136{
137 if (libadreno_utils) {
138 ::dlclose(libadreno_utils);
139 }
140}
141
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700142int AdrenoMemInfo::isMacroTilingSupportedByGPU()
143{
144 if ((libadreno_utils)) {
145 if(LINK_adreno_isMacroTilingSupportedByGpu) {
146 return LINK_adreno_isMacroTilingSupportedByGpu();
147 }
148 }
149 return 0;
150}
151
152
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800153void AdrenoMemInfo::getAlignedWidthAndHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800154 int usage, int& aligned_w, int& aligned_h)
Naomi Luisa44100c2013-02-08 12:42:03 -0800155{
Sushil Chauhane61fac52015-04-30 17:14:37 -0700156 bool ubwc_enabled = isUBwcEnabled(format, usage);
157
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800158 // Currently surface padding is only computed for RGB* surfaces.
Ajay Dudani4dc06492015-03-26 07:28:11 -0700159 if (format <= HAL_PIXEL_FORMAT_BGRA_8888) {
Sushil Chauhane61fac52015-04-30 17:14:37 -0700160 int tileEnabled = ubwc_enabled || isMacroTileEnabled(format, usage);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800161 AdrenoMemInfo::getInstance().getGpuAlignedWidthHeight(width,
162 height, format, tileEnabled, aligned_w, aligned_h);
163 return;
Naomi Luisa44100c2013-02-08 12:42:03 -0800164 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800165
Sushil Chauhane61fac52015-04-30 17:14:37 -0700166 if (ubwc_enabled) {
Sushil Chauhan65e26302015-01-14 10:48:57 -0800167 getUBwcWidthAndHeight(width, height, format, aligned_w, aligned_h);
168 return;
169 }
170
171 aligned_w = width;
172 aligned_h = height;
173 switch (format)
174 {
175 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
176 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800177 aligned_w = ALIGN(width, 32);
178 break;
Ajay Dudani4dc06492015-03-26 07:28:11 -0700179 case HAL_PIXEL_FORMAT_RAW16:
Shuzhen Wang2a000b22014-08-20 00:15:51 -0700180 aligned_w = ALIGN(width, 16);
181 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700182 case HAL_PIXEL_FORMAT_RAW10:
183 aligned_w = ALIGN(width * 10 /8, 16);
184 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800185 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
186 aligned_w = ALIGN(width, 128);
187 break;
188 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
189 case HAL_PIXEL_FORMAT_YV12:
190 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
191 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
192 case HAL_PIXEL_FORMAT_YCbCr_422_I:
193 case HAL_PIXEL_FORMAT_YCrCb_422_I:
194 aligned_w = ALIGN(width, 16);
195 break;
196 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
197 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
198 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
199 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
200 break;
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530201 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
202 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV21, width);
203 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV21, height);
204 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800205 case HAL_PIXEL_FORMAT_BLOB:
206 break;
207 case HAL_PIXEL_FORMAT_NV21_ZSL:
208 aligned_w = ALIGN(width, 64);
209 aligned_h = ALIGN(height, 64);
210 break;
211 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
212 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
213 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
214 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
215 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
216 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
217 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
218 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
219 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
220 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
221 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
222 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
223 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
224 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
225 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
226 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
227 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
228 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
229 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
230 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
231 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
232 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
233 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
234 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
235 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
236 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
237 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
238 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
239 if(LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
240 int bytesPerPixel = 0;
241 int raster_mode = 0; //Adreno unknown raster mode.
242 int padding_threshold = 512; //Threshold for padding
243 //surfaces.
244
245 LINK_adreno_compute_compressedfmt_aligned_width_and_height(
246 width, height, format, 0,raster_mode, padding_threshold,
247 &aligned_w, &aligned_h, &bytesPerPixel);
248 } else {
249 ALOGW("%s: Warning!! Symbols" \
250 " compute_compressedfmt_aligned_width_and_height" \
251 " not found", __FUNCTION__);
252 }
253 break;
254 default: break;
255 }
256}
257
258void AdrenoMemInfo::getGpuAlignedWidthHeight(int width, int height, int format,
259 int tile_enabled, int& aligned_w, int& aligned_h)
260{
261 aligned_w = ALIGN(width, 32);
262 aligned_h = ALIGN(height, 32);
263
264 // Don't add any additional padding if debug.gralloc.map_fb_memory
265 // is enabled
266 char property[PROPERTY_VALUE_MAX];
267 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
268 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
269 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
270 return;
271 }
272
273 int bpp = 4;
274 switch(format)
275 {
276 case HAL_PIXEL_FORMAT_RGB_888:
277 bpp = 3;
278 break;
279 case HAL_PIXEL_FORMAT_RGB_565:
280 case HAL_PIXEL_FORMAT_RGBA_5551:
281 case HAL_PIXEL_FORMAT_RGBA_4444:
282 bpp = 2;
283 break;
284 default: break;
285 }
286
287 if (libadreno_utils) {
288 int raster_mode = 0; // Adreno unknown raster mode.
289 int padding_threshold = 512; // Threshold for padding surfaces.
290 // the function below computes aligned width and aligned height
291 // based on linear or macro tile mode selected.
292 if(LINK_adreno_compute_aligned_width_and_height) {
293 LINK_adreno_compute_aligned_width_and_height(width,
294 height, bpp, tile_enabled,
295 raster_mode, padding_threshold,
296 &aligned_w, &aligned_h);
297
298 } else if(LINK_adreno_compute_padding) {
299 int surface_tile_height = 1; // Linear surface
300 aligned_w = LINK_adreno_compute_padding(width, bpp,
301 surface_tile_height, raster_mode,
302 padding_threshold);
303 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
304 __FUNCTION__);
305 } else {
306 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
307 "compute_aligned_width_and_height not found", __FUNCTION__);
308 }
309 }
310}
311
312int AdrenoMemInfo::isUBWCSupportedByGPU(int format)
313{
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700314 if (!gfx_ubwc_disable && libadreno_utils) {
Sushil Chauhan082acd62015-01-14 16:49:29 -0800315 if (LINK_adreno_isUBWCSupportedByGpu) {
316 ADRENOPIXELFORMAT gpu_format = getGpuPixelFormat(format);
317 return LINK_adreno_isUBWCSupportedByGpu(gpu_format);
318 }
319 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800320 return 0;
Naomi Luisa44100c2013-02-08 12:42:03 -0800321}
322
Sushil Chauhan082acd62015-01-14 16:49:29 -0800323ADRENOPIXELFORMAT AdrenoMemInfo::getGpuPixelFormat(int hal_format)
324{
325 switch (hal_format) {
326 case HAL_PIXEL_FORMAT_RGBA_8888:
327 return ADRENO_PIXELFORMAT_R8G8B8A8;
Sushil Chauhan6686c802015-04-15 11:30:39 -0700328 case HAL_PIXEL_FORMAT_RGBX_8888:
329 return ADRENO_PIXELFORMAT_R8G8B8X8;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800330 case HAL_PIXEL_FORMAT_RGB_565:
331 return ADRENO_PIXELFORMAT_B5G6R5;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800332 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800333 return ADRENO_PIXELFORMAT_NV12;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800334 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
335 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800336 return ADRENO_PIXELFORMAT_NV12_EXT;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800337 default:
338 ALOGE("%s: No map for format: 0x%x", __FUNCTION__, hal_format);
339 break;
340 }
341 return ADRENO_PIXELFORMAT_UNKNOWN;
342}
343
Naomi Luisa44100c2013-02-08 12:42:03 -0800344//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700345IAllocController* IAllocController::sController = NULL;
346IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700347{
348 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700349 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700350 }
351 return sController;
352}
353
354
355//-------------- IonController-----------------------//
356IonController::IonController()
357{
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530358 allocateIonMem();
359}
360
361void IonController::allocateIonMem()
362{
363 mIonAlloc = new IonAlloc();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700364}
365
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700366int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700367{
368 int ionFlags = 0;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500369 int ionHeapId = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700370 int ret;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700371
372 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700373 data.allocType = 0;
374
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530375 if(usage & GRALLOC_USAGE_PROTECTED) {
Prabhanjan Kandulae8f4bec2013-10-24 16:32:51 +0530376 if (usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800377 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
Shalaj Jain3c490412015-04-22 16:52:03 -0700378 ionHeapId = ION_HEAP(SD_HEAP_ID);
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800379 /*
380 * There is currently no flag in ION for Secure Display
Shalaj Jain3c490412015-04-22 16:52:03 -0700381 * VM. Please add it to the define once available.
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800382 */
Shalaj Jain3c490412015-04-22 16:52:03 -0700383 ionFlags |= ION_SD_FLAGS;
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800384 } else {
Shalaj Jain3c490412015-04-22 16:52:03 -0700385 ionHeapId = ION_HEAP(CP_HEAP_ID);
386 ionFlags |= ION_CP_FLAGS;
Shalaj Jain13cdf812014-12-02 16:20:54 -0800387 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530388 } else {
389 // for targets/OEMs which do not need HW level protection
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500390 // do not set ion secure flag & MM heap. Fallback to system heap.
391 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Justin Philipd6166602014-08-12 13:42:21 +0530392 data.allocType |= private_handle_t::PRIV_FLAGS_PROTECTED_BUFFER;
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500393 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530394 } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
395 //MM Heap is exclusively a secure heap.
396 //If it is used for non secure cases, fallback to IOMMU heap
397 ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
398 cannot be used as an insecure heap!\
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500399 trying to use system heap instead !!");
400 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500401 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700402
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700403 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500404 ionHeapId |= ION_HEAP(ION_CAMERA_HEAP_ID);
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700405
Arun Kumar K.R0daaa992013-03-12 15:08:29 -0700406 if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500407 ionHeapId |= ION_HEAP(ION_ADSP_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700408
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530409 if(ionFlags & ION_SECURE)
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500410 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700411
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500412 // if no ion heap flags are set, default to system heap
413 if(!ionHeapId)
414 ionHeapId = ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700415
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500416 //At this point we should have the right heap set, there is no fallback
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700417 data.flags = ionFlags;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500418 data.heapId = ionHeapId;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700419 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700420
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700421 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700422 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500423 } else {
424 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x",
425 __FUNCTION__, ionHeapId, ionFlags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700426 }
427
428 return ret;
429}
430
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700431IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700432{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700433 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700434 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
435 memalloc = mIonAlloc;
436 } else {
437 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
438 }
439
440 return memalloc;
441}
442
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700443bool isMacroTileEnabled(int format, int usage)
444{
445 bool tileEnabled = false;
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -0700446 int isMacroTileSupportedByMDP = 0;
447
448 qdutils::querySDEInfo(HAS_MACRO_TILE, &isMacroTileSupportedByMDP);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700449
450 // Check whether GPU & MDSS supports MacroTiling feature
451 if(AdrenoMemInfo::getInstance().isMacroTilingSupportedByGPU() &&
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -0700452 isMacroTileSupportedByMDP)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700453 {
454 // check the format
455 switch(format)
456 {
457 case HAL_PIXEL_FORMAT_RGBA_8888:
458 case HAL_PIXEL_FORMAT_RGBX_8888:
459 case HAL_PIXEL_FORMAT_BGRA_8888:
Manoj Kumar AVM5a5529b2014-02-24 18:16:37 -0800460 case HAL_PIXEL_FORMAT_RGB_565:
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700461 {
462 tileEnabled = true;
463 // check the usage flags
464 if (usage & (GRALLOC_USAGE_SW_READ_MASK |
465 GRALLOC_USAGE_SW_WRITE_MASK)) {
466 // Application intends to use CPU for rendering
467 tileEnabled = false;
468 }
469 break;
470 }
471 default:
472 break;
473 }
474 }
475 return tileEnabled;
476}
477
478// helper function
Sushil Chauhan65e26302015-01-14 10:48:57 -0800479unsigned int getSize(int format, int width, int height, int usage,
480 const int alignedw, const int alignedh) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700481
Sushil Chauhan65e26302015-01-14 10:48:57 -0800482 if (isUBwcEnabled(format, usage)) {
483 return getUBwcSize(width, height, format, alignedw, alignedh);
484 }
485
486 unsigned int size = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700487 switch (format) {
488 case HAL_PIXEL_FORMAT_RGBA_8888:
489 case HAL_PIXEL_FORMAT_RGBX_8888:
490 case HAL_PIXEL_FORMAT_BGRA_8888:
491 size = alignedw * alignedh * 4;
492 break;
493 case HAL_PIXEL_FORMAT_RGB_888:
494 size = alignedw * alignedh * 3;
495 break;
496 case HAL_PIXEL_FORMAT_RGB_565:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700497 case HAL_PIXEL_FORMAT_RGBA_5551:
498 case HAL_PIXEL_FORMAT_RGBA_4444:
Ajay Dudani4dc06492015-03-26 07:28:11 -0700499 case HAL_PIXEL_FORMAT_RAW16:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700500 size = alignedw * alignedh * 2;
501 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700502 case HAL_PIXEL_FORMAT_RAW10:
503 size = ALIGN(alignedw * alignedh, 4096);
504 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700505
506 // adreno formats
507 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
508 size = ALIGN(alignedw*alignedh, 4096);
509 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
510 break;
511 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
512 // The chroma plane is subsampled,
513 // but the pitch in bytes is unchanged
514 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700515 size = ALIGN( alignedw * alignedh, 8192);
516 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
517 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700518 case HAL_PIXEL_FORMAT_YV12:
519 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
520 ALOGE("w or h is odd for the YV12 format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800521 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700522 }
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400523 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700524 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700525 size = ALIGN(size, (unsigned int)4096);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700526 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800527 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
528 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naseer Ahmed2c215292013-09-18 23:47:42 -0400529 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800530 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700531 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
532 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700533 case HAL_PIXEL_FORMAT_YCbCr_422_I:
534 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700535 if(width & 1) {
536 ALOGE("width is odd for the YUV422_SP format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800537 return 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700538 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700539 size = ALIGN(alignedw * alignedh * 2, 4096);
540 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700541 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400542 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhane8a01792012-11-01 16:25:45 -0700543 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700544 break;
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530545 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
546 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
547 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400548 case HAL_PIXEL_FORMAT_BLOB:
549 if(height != 1) {
550 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
551 must have height==1 ", __FUNCTION__);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800552 return 0;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400553 }
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400554 size = width;
555 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700556 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700557 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
558 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500559 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
560 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
561 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
562 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
563 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
564 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
565 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
566 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
567 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
568 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
569 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
570 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
571 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
572 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
573 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
574 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
575 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
576 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
577 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
578 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
579 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
580 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
581 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
582 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
583 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
584 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
585 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Jeykumar Sankaran8f4585f2014-02-05 15:23:40 -0800586 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500587 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
588 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700589 default:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800590 ALOGE("Unrecognized pixel format: 0x%x", __FUNCTION__, format);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800591 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700592 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700593 return size;
594}
595
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700596unsigned int getBufferSizeAndDimensions(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700597 int& alignedw, int &alignedh)
598{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700599 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700600
601 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
602 height,
603 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800604 0,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700605 alignedw,
606 alignedh);
607
Sushil Chauhan65e26302015-01-14 10:48:57 -0800608 size = getSize(format, width, height, 0 /* usage */, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700609
610 return size;
611}
612
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700613
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700614unsigned int getBufferSizeAndDimensions(int width, int height, int format,
615 int usage, int& alignedw, int &alignedh)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700616{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700617 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700618
619 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
620 height,
621 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800622 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700623 alignedw,
624 alignedh);
625
Sushil Chauhan65e26302015-01-14 10:48:57 -0800626 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700627
628 return size;
629}
630
631
632void getBufferAttributes(int width, int height, int format, int usage,
Sushil Chauhane61fac52015-04-30 17:14:37 -0700633 int& alignedw, int &alignedh, int& tiled, unsigned int& size)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700634{
Sushil Chauhane61fac52015-04-30 17:14:37 -0700635 tiled = isUBwcEnabled(format, usage) || isMacroTileEnabled(format, usage);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700636
637 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
638 height,
639 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800640 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700641 alignedw,
642 alignedh);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800643 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700644}
645
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400646int getYUVPlaneInfo(private_handle_t* hnd, struct android_ycbcr* ycbcr)
647{
648 int err = 0;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700649 int width = hnd->width;
650 int height = hnd->height;
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700651 int format = hnd->format;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700652 unsigned int ystride, cstride;
Sushil Chauhan4686c972015-02-20 15:44:52 -0800653 unsigned int alignment = 4096;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700654
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400655 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700656 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
657
658 // Check if UBWC buffer has been rendered in linear format.
659 if (metadata && (metadata->operation & LINEAR_FORMAT)) {
660 format = metadata->linearFormat;
661 }
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400662
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700663 // Check metadata if the geometry has been updated.
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700664 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
665 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(metadata->bufferDim.sliceWidth,
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700666 metadata->bufferDim.sliceHeight, format, 0, width, height);
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700667 }
668
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400669 // Get the chroma offsets from the handle width/height. We take advantage
670 // of the fact the width _is_ the stride
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700671 switch (format) {
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400672 //Semiplanar
673 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
674 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
675 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
676 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: //Same as YCbCr_420_SP_VENUS
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700677 ystride = cstride = width;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400678 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700679 ycbcr->cb = (void*)(hnd->base + ystride * height);
680 ycbcr->cr = (void*)(hnd->base + ystride * height + 1);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400681 ycbcr->ystride = ystride;
682 ycbcr->cstride = cstride;
683 ycbcr->chroma_step = 2;
684 break;
685
Sushil Chauhan4686c972015-02-20 15:44:52 -0800686 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
687 // NV12_UBWC buffer has these 4 planes in the following sequence:
688 // Y_Meta_Plane, Y_Plane, UV_Meta_Plane, UV_Plane
689 unsigned int y_meta_stride, y_meta_height, y_meta_size;
690 unsigned int y_stride, y_height, y_size;
691 unsigned int c_meta_stride, c_meta_height, c_meta_size;
692
693 y_meta_stride = VENUS_Y_META_STRIDE(COLOR_FMT_NV12_UBWC, width);
694 y_meta_height = VENUS_Y_META_SCANLINES(COLOR_FMT_NV12_UBWC, height);
695 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
696
697 y_stride = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
698 y_height = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
699 y_size = ALIGN((y_stride * y_height), alignment);
700
701 c_meta_stride = VENUS_UV_META_STRIDE(COLOR_FMT_NV12_UBWC, width);
702 c_meta_height = VENUS_UV_META_SCANLINES(COLOR_FMT_NV12_UBWC, height);
703 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
704
705 ycbcr->y = (void*)(hnd->base + y_meta_size);
706 ycbcr->cb = (void*)(hnd->base + y_meta_size + y_size + c_meta_size);
707 ycbcr->cr = (void*)(hnd->base + y_meta_size + y_size +
708 c_meta_size + 1);
709 ycbcr->ystride = y_stride;
710 ycbcr->cstride = VENUS_UV_STRIDE(COLOR_FMT_NV12_UBWC, width);
711 ycbcr->chroma_step = 2;
712 break;
713
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400714 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
715 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
716 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530717 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400718 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ajay Dudani4dc06492015-03-26 07:28:11 -0700719 case HAL_PIXEL_FORMAT_RAW16:
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700720 case HAL_PIXEL_FORMAT_RAW10:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700721 ystride = cstride = width;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400722 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700723 ycbcr->cr = (void*)(hnd->base + ystride * height);
724 ycbcr->cb = (void*)(hnd->base + ystride * height + 1);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400725 ycbcr->ystride = ystride;
726 ycbcr->cstride = cstride;
727 ycbcr->chroma_step = 2;
728 break;
729
730 //Planar
731 case HAL_PIXEL_FORMAT_YV12:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700732 ystride = width;
733 cstride = ALIGN(width/2, 16);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400734 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700735 ycbcr->cr = (void*)(hnd->base + ystride * height);
736 ycbcr->cb = (void*)(hnd->base + ystride * height +
737 cstride * height/2);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400738 ycbcr->ystride = ystride;
739 ycbcr->cstride = cstride;
740 ycbcr->chroma_step = 1;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400741 break;
742 //Unsupported formats
743 case HAL_PIXEL_FORMAT_YCbCr_422_I:
744 case HAL_PIXEL_FORMAT_YCrCb_422_I:
745 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
746 default:
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700747 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, format);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400748 err = -EINVAL;
749 }
750 return err;
751
752}
753
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700754
755
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700756// Allocate buffer from width, height and format into a
757// private_handle_t. It is the responsibility of the caller
758// to free the buffer using the free_buffer function
759int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
760{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700761 alloc_data data;
762 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700763 gralloc::IAllocController* sAlloc =
764 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700765 data.base = 0;
766 data.fd = -1;
767 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700768 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
769 alignedh);
770
Naseer Ahmed29a26812012-06-14 00:56:20 -0700771 data.align = getpagesize();
772 data.uncached = useUncached(usage);
773 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700774
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700775 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700776 if (0 != err) {
777 ALOGE("%s: allocate failed", __FUNCTION__);
778 return -ENOMEM;
779 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700780
Naseer Ahmed29a26812012-06-14 00:56:20 -0700781 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700782 data.allocType, 0, format,
783 alignedw, alignedh);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700784 hnd->base = (uint64_t) data.base;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700785 hnd->offset = data.offset;
786 hnd->gpuaddr = 0;
787 *pHnd = hnd;
788 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700789}
790
791void free_buffer(private_handle_t *hnd)
792{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700793 gralloc::IAllocController* sAlloc =
794 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700795 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700796 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700797 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
798 }
799 if(hnd)
800 delete hnd;
801
802}
Sushil Chauhan65e26302015-01-14 10:48:57 -0800803
804// UBWC helper functions
805static bool isUBwcFormat(int format)
806{
807 // Explicitly defined UBWC formats
808 switch(format)
809 {
810 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
811 return true;
812 default:
813 return false;
814 }
815}
816
817static bool isUBwcSupported(int format)
818{
819 // Existing HAL formats with UBWC support
820 switch(format)
821 {
822 case HAL_PIXEL_FORMAT_RGB_565:
823 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -0700824 case HAL_PIXEL_FORMAT_RGBX_8888:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800825 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
826 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
827 return true;
828 default:
829 return false;
830 }
831}
832
833bool isUBwcEnabled(int format, int usage)
834{
Sushil Chauhan81594f62015-01-26 16:00:51 -0800835 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
836 if (isUBwcFormat(format))
837 return true;
838
839 // Allow UBWC, if client sets UBWC gralloc usage flag & GPU supports format.
840 if ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) && isUBwcSupported(format) &&
841 AdrenoMemInfo::getInstance().isUBWCSupportedByGPU(format)) {
842 // Allow UBWC, only if CPU usage flags are not set
843 if (!(usage & (GRALLOC_USAGE_SW_READ_MASK |
Sushil Chauhan65e26302015-01-14 10:48:57 -0800844 GRALLOC_USAGE_SW_WRITE_MASK))) {
845 return true;
846 }
847 }
848 return false;
849}
850
851static void getUBwcWidthAndHeight(int width, int height, int format,
852 int& aligned_w, int& aligned_h)
853{
854 switch (format)
855 {
856 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
857 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
858 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
859 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
860 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
861 break;
862 default:
863 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
864 aligned_w = 0;
865 aligned_h = 0;
866 break;
867 }
868}
869
870static void getUBwcBlockSize(int bpp, int& block_width, int& block_height)
871{
872 block_width = 0;
873 block_height = 0;
874
875 switch(bpp)
876 {
877 case 2:
878 case 4:
879 block_width = 16;
880 block_height = 4;
881 break;
882 case 8:
883 block_width = 8;
884 block_height = 4;
885 break;
886 case 16:
887 block_width = 4;
888 block_height = 4;
889 break;
890 default:
891 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
892 break;
893 }
894}
895
896static unsigned int getUBwcMetaBufferSize(int width, int height, int bpp)
897{
898 unsigned int size = 0;
899 int meta_width, meta_height;
900 int block_width, block_height;
901
902 getUBwcBlockSize(bpp, block_width, block_height);
903
904 if (!block_width || !block_height) {
905 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
906 return size;
907 }
908
909 // Align meta buffer height to 16 blocks
910 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
911
912 // Align meta buffer width to 64 blocks
913 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
914
915 // Align meta buffer size to 4K
Sushil Chauhanc85b65b2015-04-30 11:05:36 -0700916 size = ALIGN((meta_width * meta_height), 4096);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800917 return size;
918}
919
920static unsigned int getUBwcSize(int width, int height, int format,
921 const int alignedw, const int alignedh) {
922
923 unsigned int size = 0;
924 switch (format) {
925 case HAL_PIXEL_FORMAT_RGB_565:
926 size = alignedw * alignedh * 2;
927 size += getUBwcMetaBufferSize(width, height, 2);
928 break;
929 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -0700930 case HAL_PIXEL_FORMAT_RGBX_8888:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800931 size = alignedw * alignedh * 4;
932 size += getUBwcMetaBufferSize(width, height, 4);
933 break;
934 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
935 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
936 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
937 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
938 break;
939 default:
940 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
941 break;
942 }
943 return size;
944}
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700945
Sushil Chauhanc85b65b2015-04-30 11:05:36 -0700946int getRgbDataAddress(private_handle_t* hnd, void** rgb_data)
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700947{
948 int err = 0;
949
950 // This api is for RGB* formats
Ajay Dudani4dc06492015-03-26 07:28:11 -0700951 if (hnd->format > HAL_PIXEL_FORMAT_BGRA_8888) {
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700952 return -EINVAL;
953 }
954
955 // linear buffer
956 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
Sushil Chauhanc85b65b2015-04-30 11:05:36 -0700957 *rgb_data = (void*)hnd->base;
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700958 return err;
959 }
960
961 unsigned int meta_size = 0;
962 switch (hnd->format) {
963 case HAL_PIXEL_FORMAT_RGB_565:
964 meta_size = getUBwcMetaBufferSize(hnd->width, hnd->height, 2);
965 break;
966 case HAL_PIXEL_FORMAT_RGBA_8888:
967 case HAL_PIXEL_FORMAT_RGBX_8888:
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700968 meta_size = getUBwcMetaBufferSize(hnd->width, hnd->height, 4);
969 break;
970 default:
971 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
972 err = -EINVAL;
973 break;
974 }
975
Sushil Chauhanc85b65b2015-04-30 11:05:36 -0700976 *rgb_data = (void*)(hnd->base + meta_size);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700977 return err;
978}