blob: aaaf16b92bd94c3ebc1b927ae3f0437971236ff0 [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"
Naseer Ahmeda87da602012-07-01 23:54:19 -070038#include "comptype.h"
Manoj Kumar AVM8a220812013-10-10 11:46:06 -070039#include "mdp_version.h"
Kaushik Kanetkar071aca62015-01-22 23:16:26 -070040#include <qdMetaData.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070041
Sushil Chauhanc6bd6d92012-12-12 12:33:01 -080042#ifdef VENUS_COLOR_FORMAT
43#include <media/msm_media_info.h>
44#else
45#define VENUS_Y_STRIDE(args...) 0
46#define VENUS_Y_SCANLINES(args...) 0
47#define VENUS_BUFFER_SIZE(args...) 0
48#endif
49
Naseer Ahmed63326f42013-12-18 02:45:48 -050050#define ASTC_BLOCK_SIZE 16
Naseer Ahmed63326f42013-12-18 02:45:48 -050051
Shalaj Jain3c490412015-04-22 16:52:03 -070052#ifndef ION_FLAG_CP_PIXEL
Shalaj Jain1f9725a2015-03-04 17:53:49 -080053#define ION_FLAG_CP_PIXEL 0
Shalaj Jain1f9725a2015-03-04 17:53:49 -080054#endif
55
56#ifndef ION_FLAG_ALLOW_NON_CONTIG
57#define ION_FLAG_ALLOW_NON_CONTIG 0
58#endif
59
Shalaj Jain3c490412015-04-22 16:52:03 -070060#ifdef MASTER_SIDE_CP
61#define CP_HEAP_ID ION_SECURE_HEAP_ID
62/* Please Add the new SD ION Heap here */
63#define SD_HEAP_ID 0
64#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_CP_PIXEL)
65/* Please Add the new SD ION Flag here */
66#define ION_SD_FLAGS ION_SECURE
67#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;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070076
Naomi Luisa44100c2013-02-08 12:42:03 -080077ANDROID_SINGLETON_STATIC_INSTANCE(AdrenoMemInfo);
78
Sushil Chauhan65e26302015-01-14 10:48:57 -080079static void getUBwcWidthAndHeight(int, int, int, int&, int&);
80static unsigned int getUBwcSize(int, int, int, const int, const int);
81
Iliyan Malchev202a77d2012-06-11 14:41:12 -070082//Common functions
Iliyan Malchev202a77d2012-06-11 14:41:12 -070083
Saurabh Shah1adcafe2014-12-19 10:05:41 -080084/* The default policy is to return cached buffers unless the client explicity
85 * sets the PRIVATE_UNCACHED flag or indicates that the buffer will be rarely
86 * read or written in software. Any combination with a _RARELY_ flag will be
87 * treated as uncached. */
88static bool useUncached(const int& usage) {
89 if((usage & GRALLOC_USAGE_PRIVATE_UNCACHED) or
90 ((usage & GRALLOC_USAGE_SW_WRITE_MASK) ==
91 GRALLOC_USAGE_SW_WRITE_RARELY) or
92 ((usage & GRALLOC_USAGE_SW_READ_MASK) ==
93 GRALLOC_USAGE_SW_READ_RARELY))
94 return true;
95
96 return false;
97}
98
Naomi Luisa44100c2013-02-08 12:42:03 -080099//-------------- AdrenoMemInfo-----------------------//
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800100AdrenoMemInfo::AdrenoMemInfo()
101{
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800102 LINK_adreno_compute_aligned_width_and_height = NULL;
103 LINK_adreno_compute_padding = NULL;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700104 LINK_adreno_isMacroTilingSupportedByGpu = NULL;
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800105 LINK_adreno_compute_compressedfmt_aligned_width_and_height = NULL;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800106 LINK_adreno_isUBWCSupportedByGpu = NULL;
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800107
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800108 libadreno_utils = ::dlopen("libadreno_utils.so", RTLD_NOW);
109 if (libadreno_utils) {
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800110 *(void **)&LINK_adreno_compute_aligned_width_and_height =
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700111 ::dlsym(libadreno_utils, "compute_aligned_width_and_height");
112 *(void **)&LINK_adreno_compute_padding =
113 ::dlsym(libadreno_utils, "compute_surface_padding");
114 *(void **)&LINK_adreno_isMacroTilingSupportedByGpu =
115 ::dlsym(libadreno_utils, "isMacroTilingSupportedByGpu");
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800116 *(void **)&LINK_adreno_compute_compressedfmt_aligned_width_and_height =
117 ::dlsym(libadreno_utils,
118 "compute_compressedfmt_aligned_width_and_height");
Sushil Chauhan082acd62015-01-14 16:49:29 -0800119 *(void **)&LINK_adreno_isUBWCSupportedByGpu =
120 ::dlsym(libadreno_utils, "isUBWCSupportedByGpu");
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800121 }
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700122
123 // Check if the overriding property debug.gralloc.gfx_ubwc_disable
124 // that disables UBWC allocations for the graphics stack is set
125 gfx_ubwc_disable = 0;
126 char property[PROPERTY_VALUE_MAX];
127 property_get("debug.gralloc.gfx_ubwc_disable", property, "0");
128 if(!(strncmp(property, "1", PROPERTY_VALUE_MAX)) ||
129 !(strncmp(property, "true", PROPERTY_VALUE_MAX))) {
130 gfx_ubwc_disable = 1;
131 }
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800132}
133
134AdrenoMemInfo::~AdrenoMemInfo()
135{
136 if (libadreno_utils) {
137 ::dlclose(libadreno_utils);
138 }
139}
140
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700141int AdrenoMemInfo::isMacroTilingSupportedByGPU()
142{
143 if ((libadreno_utils)) {
144 if(LINK_adreno_isMacroTilingSupportedByGpu) {
145 return LINK_adreno_isMacroTilingSupportedByGpu();
146 }
147 }
148 return 0;
149}
150
151
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800152void AdrenoMemInfo::getAlignedWidthAndHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800153 int usage, int& aligned_w, int& aligned_h)
Naomi Luisa44100c2013-02-08 12:42:03 -0800154{
Sushil Chauhan65e26302015-01-14 10:48:57 -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.
Jesse Hallfbe96d22013-09-20 01:39:43 -0700159 if (format <= HAL_PIXEL_FORMAT_sRGB_X_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:
177 case HAL_PIXEL_FORMAT_RAW_SENSOR:
178 aligned_w = ALIGN(width, 32);
179 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700180 case HAL_PIXEL_FORMAT_RAW10:
181 aligned_w = ALIGN(width * 10 /8, 16);
182 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800183 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
184 aligned_w = ALIGN(width, 128);
185 break;
186 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
187 case HAL_PIXEL_FORMAT_YV12:
188 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
189 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
190 case HAL_PIXEL_FORMAT_YCbCr_422_I:
191 case HAL_PIXEL_FORMAT_YCrCb_422_I:
192 aligned_w = ALIGN(width, 16);
193 break;
194 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
195 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
196 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
197 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
198 break;
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530199 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
200 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV21, width);
201 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV21, height);
202 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800203 case HAL_PIXEL_FORMAT_BLOB:
204 break;
205 case HAL_PIXEL_FORMAT_NV21_ZSL:
206 aligned_w = ALIGN(width, 64);
207 aligned_h = ALIGN(height, 64);
208 break;
209 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
210 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
211 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
212 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
213 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
214 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
215 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
216 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
217 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
218 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
219 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
220 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
221 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
222 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
223 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
224 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
225 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
226 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
227 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
228 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
229 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
230 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
231 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
232 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
233 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
234 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
235 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
236 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
237 if(LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
238 int bytesPerPixel = 0;
239 int raster_mode = 0; //Adreno unknown raster mode.
240 int padding_threshold = 512; //Threshold for padding
241 //surfaces.
242
243 LINK_adreno_compute_compressedfmt_aligned_width_and_height(
244 width, height, format, 0,raster_mode, padding_threshold,
245 &aligned_w, &aligned_h, &bytesPerPixel);
246 } else {
247 ALOGW("%s: Warning!! Symbols" \
248 " compute_compressedfmt_aligned_width_and_height" \
249 " not found", __FUNCTION__);
250 }
251 break;
252 default: break;
253 }
254}
255
256void AdrenoMemInfo::getGpuAlignedWidthHeight(int width, int height, int format,
257 int tile_enabled, int& aligned_w, int& aligned_h)
258{
259 aligned_w = ALIGN(width, 32);
260 aligned_h = ALIGN(height, 32);
261
262 // Don't add any additional padding if debug.gralloc.map_fb_memory
263 // is enabled
264 char property[PROPERTY_VALUE_MAX];
265 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
266 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
267 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
268 return;
269 }
270
271 int bpp = 4;
272 switch(format)
273 {
274 case HAL_PIXEL_FORMAT_RGB_888:
275 bpp = 3;
276 break;
277 case HAL_PIXEL_FORMAT_RGB_565:
278 case HAL_PIXEL_FORMAT_RGBA_5551:
279 case HAL_PIXEL_FORMAT_RGBA_4444:
280 bpp = 2;
281 break;
282 default: break;
283 }
284
285 if (libadreno_utils) {
286 int raster_mode = 0; // Adreno unknown raster mode.
287 int padding_threshold = 512; // Threshold for padding surfaces.
288 // the function below computes aligned width and aligned height
289 // based on linear or macro tile mode selected.
290 if(LINK_adreno_compute_aligned_width_and_height) {
291 LINK_adreno_compute_aligned_width_and_height(width,
292 height, bpp, tile_enabled,
293 raster_mode, padding_threshold,
294 &aligned_w, &aligned_h);
295
296 } else if(LINK_adreno_compute_padding) {
297 int surface_tile_height = 1; // Linear surface
298 aligned_w = LINK_adreno_compute_padding(width, bpp,
299 surface_tile_height, raster_mode,
300 padding_threshold);
301 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
302 __FUNCTION__);
303 } else {
304 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
305 "compute_aligned_width_and_height not found", __FUNCTION__);
306 }
307 }
308}
309
310int AdrenoMemInfo::isUBWCSupportedByGPU(int format)
311{
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700312 if (!gfx_ubwc_disable && libadreno_utils) {
Sushil Chauhan082acd62015-01-14 16:49:29 -0800313 if (LINK_adreno_isUBWCSupportedByGpu) {
314 ADRENOPIXELFORMAT gpu_format = getGpuPixelFormat(format);
315 return LINK_adreno_isUBWCSupportedByGpu(gpu_format);
316 }
317 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800318 return 0;
Naomi Luisa44100c2013-02-08 12:42:03 -0800319}
320
Sushil Chauhan082acd62015-01-14 16:49:29 -0800321ADRENOPIXELFORMAT AdrenoMemInfo::getGpuPixelFormat(int hal_format)
322{
323 switch (hal_format) {
324 case HAL_PIXEL_FORMAT_RGBA_8888:
325 return ADRENO_PIXELFORMAT_R8G8B8A8;
Sushil Chauhan6686c802015-04-15 11:30:39 -0700326 case HAL_PIXEL_FORMAT_RGBX_8888:
327 return ADRENO_PIXELFORMAT_R8G8B8X8;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800328 case HAL_PIXEL_FORMAT_RGB_565:
329 return ADRENO_PIXELFORMAT_B5G6R5;
330 case HAL_PIXEL_FORMAT_sRGB_A_8888:
331 return ADRENO_PIXELFORMAT_R8G8B8A8_SRGB;
332 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;
446
447 // Check whether GPU & MDSS supports MacroTiling feature
448 if(AdrenoMemInfo::getInstance().isMacroTilingSupportedByGPU() &&
449 qdutils::MDPVersion::getInstance().supportsMacroTile())
450 {
451 // check the format
452 switch(format)
453 {
454 case HAL_PIXEL_FORMAT_RGBA_8888:
455 case HAL_PIXEL_FORMAT_RGBX_8888:
456 case HAL_PIXEL_FORMAT_BGRA_8888:
Manoj Kumar AVM5a5529b2014-02-24 18:16:37 -0800457 case HAL_PIXEL_FORMAT_RGB_565:
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700458 {
459 tileEnabled = true;
460 // check the usage flags
461 if (usage & (GRALLOC_USAGE_SW_READ_MASK |
462 GRALLOC_USAGE_SW_WRITE_MASK)) {
463 // Application intends to use CPU for rendering
464 tileEnabled = false;
465 }
466 break;
467 }
468 default:
469 break;
470 }
471 }
472 return tileEnabled;
473}
474
475// helper function
Sushil Chauhan65e26302015-01-14 10:48:57 -0800476unsigned int getSize(int format, int width, int height, int usage,
477 const int alignedw, const int alignedh) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700478
Sushil Chauhan65e26302015-01-14 10:48:57 -0800479 if (isUBwcEnabled(format, usage)) {
480 return getUBwcSize(width, height, format, alignedw, alignedh);
481 }
482
483 unsigned int size = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700484 switch (format) {
485 case HAL_PIXEL_FORMAT_RGBA_8888:
486 case HAL_PIXEL_FORMAT_RGBX_8888:
487 case HAL_PIXEL_FORMAT_BGRA_8888:
Naseer Ahmed82fc4b72013-09-20 01:31:37 -0700488 case HAL_PIXEL_FORMAT_sRGB_A_8888:
Jesse Hallfbe96d22013-09-20 01:39:43 -0700489 case HAL_PIXEL_FORMAT_sRGB_X_8888:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700490 size = alignedw * alignedh * 4;
491 break;
492 case HAL_PIXEL_FORMAT_RGB_888:
493 size = alignedw * alignedh * 3;
494 break;
495 case HAL_PIXEL_FORMAT_RGB_565:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700496 case HAL_PIXEL_FORMAT_RGBA_5551:
497 case HAL_PIXEL_FORMAT_RGBA_4444:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400498 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700499 size = alignedw * alignedh * 2;
500 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700501 case HAL_PIXEL_FORMAT_RAW10:
502 size = ALIGN(alignedw * alignedh, 4096);
503 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700504
505 // adreno formats
506 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
507 size = ALIGN(alignedw*alignedh, 4096);
508 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
509 break;
510 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
511 // The chroma plane is subsampled,
512 // but the pitch in bytes is unchanged
513 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700514 size = ALIGN( alignedw * alignedh, 8192);
515 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
516 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700517 case HAL_PIXEL_FORMAT_YV12:
518 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
519 ALOGE("w or h is odd for the YV12 format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800520 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700521 }
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400522 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700523 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700524 size = ALIGN(size, (unsigned int)4096);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700525 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800526 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
527 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naseer Ahmed2c215292013-09-18 23:47:42 -0400528 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800529 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700530 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
531 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700532 case HAL_PIXEL_FORMAT_YCbCr_422_I:
533 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700534 if(width & 1) {
535 ALOGE("width is odd for the YUV422_SP format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800536 return 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700537 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700538 size = ALIGN(alignedw * alignedh * 2, 4096);
539 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700540 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400541 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhane8a01792012-11-01 16:25:45 -0700542 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700543 break;
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530544 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
545 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
546 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400547 case HAL_PIXEL_FORMAT_BLOB:
548 if(height != 1) {
549 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
550 must have height==1 ", __FUNCTION__);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800551 return 0;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400552 }
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400553 size = width;
554 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700555 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700556 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
557 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500558 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
559 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
560 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
561 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
562 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
563 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
564 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
565 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
566 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
567 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
568 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
569 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
570 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
571 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
572 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
573 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
574 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
575 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
576 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
577 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
578 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
579 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
580 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
581 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
582 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
583 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
584 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Jeykumar Sankaran8f4585f2014-02-05 15:23:40 -0800585 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500586 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
587 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700588 default:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800589 ALOGE("Unrecognized pixel format: 0x%x", __FUNCTION__, format);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800590 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700591 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700592 return size;
593}
594
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700595unsigned int getBufferSizeAndDimensions(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700596 int& alignedw, int &alignedh)
597{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700598 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700599
600 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
601 height,
602 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800603 0,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700604 alignedw,
605 alignedh);
606
Sushil Chauhan65e26302015-01-14 10:48:57 -0800607 size = getSize(format, width, height, 0 /* usage */, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700608
609 return size;
610}
611
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700612
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700613unsigned int getBufferSizeAndDimensions(int width, int height, int format,
614 int usage, int& alignedw, int &alignedh)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700615{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700616 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700617
618 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
619 height,
620 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800621 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700622 alignedw,
623 alignedh);
624
Sushil Chauhan65e26302015-01-14 10:48:57 -0800625 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700626
627 return size;
628}
629
630
631void getBufferAttributes(int width, int height, int format, int usage,
Sushil Chauhane61fac52015-04-30 17:14:37 -0700632 int& alignedw, int &alignedh, int& tiled, unsigned int& size)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700633{
Sushil Chauhane61fac52015-04-30 17:14:37 -0700634 tiled = isUBwcEnabled(format, usage) || isMacroTileEnabled(format, usage);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700635
636 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
637 height,
638 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800639 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700640 alignedw,
641 alignedh);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800642 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700643}
644
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400645int getYUVPlaneInfo(private_handle_t* hnd, struct android_ycbcr* ycbcr)
646{
647 int err = 0;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700648 int width = hnd->width;
649 int height = hnd->height;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700650 unsigned int ystride, cstride;
Sushil Chauhan4686c972015-02-20 15:44:52 -0800651 unsigned int alignment = 4096;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700652
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400653 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
654
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700655 // Check metadata if the geometry has been updated.
656 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
657 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
658 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(metadata->bufferDim.sliceWidth,
659 metadata->bufferDim.sliceHeight, hnd->format, 0, width, height);
660 }
661
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400662 // Get the chroma offsets from the handle width/height. We take advantage
663 // of the fact the width _is_ the stride
664 switch (hnd->format) {
665 //Semiplanar
666 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
667 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
668 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
669 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: //Same as YCbCr_420_SP_VENUS
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700670 ystride = cstride = width;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400671 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700672 ycbcr->cb = (void*)(hnd->base + ystride * height);
673 ycbcr->cr = (void*)(hnd->base + ystride * height + 1);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400674 ycbcr->ystride = ystride;
675 ycbcr->cstride = cstride;
676 ycbcr->chroma_step = 2;
677 break;
678
Sushil Chauhan4686c972015-02-20 15:44:52 -0800679 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
680 // NV12_UBWC buffer has these 4 planes in the following sequence:
681 // Y_Meta_Plane, Y_Plane, UV_Meta_Plane, UV_Plane
682 unsigned int y_meta_stride, y_meta_height, y_meta_size;
683 unsigned int y_stride, y_height, y_size;
684 unsigned int c_meta_stride, c_meta_height, c_meta_size;
685
686 y_meta_stride = VENUS_Y_META_STRIDE(COLOR_FMT_NV12_UBWC, width);
687 y_meta_height = VENUS_Y_META_SCANLINES(COLOR_FMT_NV12_UBWC, height);
688 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
689
690 y_stride = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
691 y_height = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
692 y_size = ALIGN((y_stride * y_height), alignment);
693
694 c_meta_stride = VENUS_UV_META_STRIDE(COLOR_FMT_NV12_UBWC, width);
695 c_meta_height = VENUS_UV_META_SCANLINES(COLOR_FMT_NV12_UBWC, height);
696 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
697
698 ycbcr->y = (void*)(hnd->base + y_meta_size);
699 ycbcr->cb = (void*)(hnd->base + y_meta_size + y_size + c_meta_size);
700 ycbcr->cr = (void*)(hnd->base + y_meta_size + y_size +
701 c_meta_size + 1);
702 ycbcr->ystride = y_stride;
703 ycbcr->cstride = VENUS_UV_STRIDE(COLOR_FMT_NV12_UBWC, width);
704 ycbcr->chroma_step = 2;
705 break;
706
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400707 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
708 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
709 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530710 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400711 case HAL_PIXEL_FORMAT_NV21_ZSL:
712 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700713 case HAL_PIXEL_FORMAT_RAW10:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700714 ystride = cstride = width;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400715 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700716 ycbcr->cr = (void*)(hnd->base + ystride * height);
717 ycbcr->cb = (void*)(hnd->base + ystride * height + 1);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400718 ycbcr->ystride = ystride;
719 ycbcr->cstride = cstride;
720 ycbcr->chroma_step = 2;
721 break;
722
723 //Planar
724 case HAL_PIXEL_FORMAT_YV12:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700725 ystride = width;
726 cstride = ALIGN(width/2, 16);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400727 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700728 ycbcr->cr = (void*)(hnd->base + ystride * height);
729 ycbcr->cb = (void*)(hnd->base + ystride * height +
730 cstride * height/2);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400731 ycbcr->ystride = ystride;
732 ycbcr->cstride = cstride;
733 ycbcr->chroma_step = 1;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400734 break;
735 //Unsupported formats
736 case HAL_PIXEL_FORMAT_YCbCr_422_I:
737 case HAL_PIXEL_FORMAT_YCrCb_422_I:
738 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
739 default:
740 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__,
741 hnd->format);
742 err = -EINVAL;
743 }
744 return err;
745
746}
747
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700748
749
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700750// Allocate buffer from width, height and format into a
751// private_handle_t. It is the responsibility of the caller
752// to free the buffer using the free_buffer function
753int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
754{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700755 alloc_data data;
756 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700757 gralloc::IAllocController* sAlloc =
758 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700759 data.base = 0;
760 data.fd = -1;
761 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700762 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
763 alignedh);
764
Naseer Ahmed29a26812012-06-14 00:56:20 -0700765 data.align = getpagesize();
766 data.uncached = useUncached(usage);
767 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700768
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700769 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700770 if (0 != err) {
771 ALOGE("%s: allocate failed", __FUNCTION__);
772 return -ENOMEM;
773 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700774
Naseer Ahmed29a26812012-06-14 00:56:20 -0700775 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700776 data.allocType, 0, format,
777 alignedw, alignedh);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700778 hnd->base = (uint64_t) data.base;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700779 hnd->offset = data.offset;
780 hnd->gpuaddr = 0;
781 *pHnd = hnd;
782 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700783}
784
785void free_buffer(private_handle_t *hnd)
786{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700787 gralloc::IAllocController* sAlloc =
788 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700789 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700790 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700791 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
792 }
793 if(hnd)
794 delete hnd;
795
796}
Sushil Chauhan65e26302015-01-14 10:48:57 -0800797
798// UBWC helper functions
799static bool isUBwcFormat(int format)
800{
801 // Explicitly defined UBWC formats
802 switch(format)
803 {
804 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
805 return true;
806 default:
807 return false;
808 }
809}
810
811static bool isUBwcSupported(int format)
812{
813 // Existing HAL formats with UBWC support
814 switch(format)
815 {
816 case HAL_PIXEL_FORMAT_RGB_565:
817 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -0700818 case HAL_PIXEL_FORMAT_RGBX_8888:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800819 case HAL_PIXEL_FORMAT_sRGB_A_8888:
820 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
821 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
822 return true;
823 default:
824 return false;
825 }
826}
827
828bool isUBwcEnabled(int format, int usage)
829{
Sushil Chauhan81594f62015-01-26 16:00:51 -0800830 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
831 if (isUBwcFormat(format))
832 return true;
833
834 // Allow UBWC, if client sets UBWC gralloc usage flag & GPU supports format.
835 if ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) && isUBwcSupported(format) &&
836 AdrenoMemInfo::getInstance().isUBWCSupportedByGPU(format)) {
837 // Allow UBWC, only if CPU usage flags are not set
838 if (!(usage & (GRALLOC_USAGE_SW_READ_MASK |
Sushil Chauhan65e26302015-01-14 10:48:57 -0800839 GRALLOC_USAGE_SW_WRITE_MASK))) {
840 return true;
841 }
842 }
843 return false;
844}
845
846static void getUBwcWidthAndHeight(int width, int height, int format,
847 int& aligned_w, int& aligned_h)
848{
849 switch (format)
850 {
851 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
852 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
853 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
854 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
855 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
856 break;
857 default:
858 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
859 aligned_w = 0;
860 aligned_h = 0;
861 break;
862 }
863}
864
865static void getUBwcBlockSize(int bpp, int& block_width, int& block_height)
866{
867 block_width = 0;
868 block_height = 0;
869
870 switch(bpp)
871 {
872 case 2:
873 case 4:
874 block_width = 16;
875 block_height = 4;
876 break;
877 case 8:
878 block_width = 8;
879 block_height = 4;
880 break;
881 case 16:
882 block_width = 4;
883 block_height = 4;
884 break;
885 default:
886 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
887 break;
888 }
889}
890
891static unsigned int getUBwcMetaBufferSize(int width, int height, int bpp)
892{
893 unsigned int size = 0;
894 int meta_width, meta_height;
895 int block_width, block_height;
896
897 getUBwcBlockSize(bpp, block_width, block_height);
898
899 if (!block_width || !block_height) {
900 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
901 return size;
902 }
903
904 // Align meta buffer height to 16 blocks
905 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
906
907 // Align meta buffer width to 64 blocks
908 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
909
910 // Align meta buffer size to 4K
Sushil Chauhanc85b65b2015-04-30 11:05:36 -0700911 size = ALIGN((meta_width * meta_height), 4096);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800912 return size;
913}
914
915static unsigned int getUBwcSize(int width, int height, int format,
916 const int alignedw, const int alignedh) {
917
918 unsigned int size = 0;
919 switch (format) {
920 case HAL_PIXEL_FORMAT_RGB_565:
921 size = alignedw * alignedh * 2;
922 size += getUBwcMetaBufferSize(width, height, 2);
923 break;
924 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -0700925 case HAL_PIXEL_FORMAT_RGBX_8888:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800926 case HAL_PIXEL_FORMAT_sRGB_A_8888:
927 size = alignedw * alignedh * 4;
928 size += getUBwcMetaBufferSize(width, height, 4);
929 break;
930 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
931 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
932 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
933 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
934 break;
935 default:
936 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
937 break;
938 }
939 return size;
940}
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700941
Sushil Chauhanc85b65b2015-04-30 11:05:36 -0700942int getRgbDataAddress(private_handle_t* hnd, void** rgb_data)
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700943{
944 int err = 0;
945
946 // This api is for RGB* formats
947 if (hnd->format > HAL_PIXEL_FORMAT_sRGB_X_8888) {
948 return -EINVAL;
949 }
950
951 // linear buffer
952 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
Sushil Chauhanc85b65b2015-04-30 11:05:36 -0700953 *rgb_data = (void*)hnd->base;
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700954 return err;
955 }
956
957 unsigned int meta_size = 0;
958 switch (hnd->format) {
959 case HAL_PIXEL_FORMAT_RGB_565:
960 meta_size = getUBwcMetaBufferSize(hnd->width, hnd->height, 2);
961 break;
962 case HAL_PIXEL_FORMAT_RGBA_8888:
963 case HAL_PIXEL_FORMAT_RGBX_8888:
964 case HAL_PIXEL_FORMAT_sRGB_A_8888:
965 meta_size = getUBwcMetaBufferSize(hnd->width, hnd->height, 4);
966 break;
967 default:
968 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
969 err = -EINVAL;
970 break;
971 }
972
Sushil Chauhanc85b65b2015-04-30 11:05:36 -0700973 *rgb_data = (void*)(hnd->base + meta_size);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700974 return err;
975}