blob: 8dcc219917c523de7ea42717b87bce61c4a7b0a7 [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
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800156 // Currently surface padding is only computed for RGB* surfaces.
Jesse Hallfbe96d22013-09-20 01:39:43 -0700157 if (format <= HAL_PIXEL_FORMAT_sRGB_X_8888) {
Sushil Chauhan65e26302015-01-14 10:48:57 -0800158 int tileEnabled = isMacroTileEnabled(format, usage);
159 AdrenoMemInfo::getInstance().getGpuAlignedWidthHeight(width,
160 height, format, tileEnabled, aligned_w, aligned_h);
161 return;
Naomi Luisa44100c2013-02-08 12:42:03 -0800162 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800163
164 if (isUBwcEnabled(format, usage)) {
165 getUBwcWidthAndHeight(width, height, format, aligned_w, aligned_h);
166 return;
167 }
168
169 aligned_w = width;
170 aligned_h = height;
171 switch (format)
172 {
173 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
174 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
175 case HAL_PIXEL_FORMAT_RAW_SENSOR:
176 aligned_w = ALIGN(width, 32);
177 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700178 case HAL_PIXEL_FORMAT_RAW10:
179 aligned_w = ALIGN(width * 10 /8, 16);
180 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800181 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
182 aligned_w = ALIGN(width, 128);
183 break;
184 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
185 case HAL_PIXEL_FORMAT_YV12:
186 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
187 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
188 case HAL_PIXEL_FORMAT_YCbCr_422_I:
189 case HAL_PIXEL_FORMAT_YCrCb_422_I:
190 aligned_w = ALIGN(width, 16);
191 break;
192 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
193 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
194 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
195 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
196 break;
197 case HAL_PIXEL_FORMAT_BLOB:
198 break;
199 case HAL_PIXEL_FORMAT_NV21_ZSL:
200 aligned_w = ALIGN(width, 64);
201 aligned_h = ALIGN(height, 64);
202 break;
203 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
204 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
205 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
206 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
207 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
208 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
209 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
210 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
211 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
212 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
213 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
214 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
215 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
216 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
217 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
218 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
219 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
220 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
221 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
222 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
223 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
224 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
225 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
226 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
227 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
228 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
229 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
230 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
231 if(LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
232 int bytesPerPixel = 0;
233 int raster_mode = 0; //Adreno unknown raster mode.
234 int padding_threshold = 512; //Threshold for padding
235 //surfaces.
236
237 LINK_adreno_compute_compressedfmt_aligned_width_and_height(
238 width, height, format, 0,raster_mode, padding_threshold,
239 &aligned_w, &aligned_h, &bytesPerPixel);
240 } else {
241 ALOGW("%s: Warning!! Symbols" \
242 " compute_compressedfmt_aligned_width_and_height" \
243 " not found", __FUNCTION__);
244 }
245 break;
246 default: break;
247 }
248}
249
250void AdrenoMemInfo::getGpuAlignedWidthHeight(int width, int height, int format,
251 int tile_enabled, int& aligned_w, int& aligned_h)
252{
253 aligned_w = ALIGN(width, 32);
254 aligned_h = ALIGN(height, 32);
255
256 // Don't add any additional padding if debug.gralloc.map_fb_memory
257 // is enabled
258 char property[PROPERTY_VALUE_MAX];
259 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
260 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
261 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
262 return;
263 }
264
265 int bpp = 4;
266 switch(format)
267 {
268 case HAL_PIXEL_FORMAT_RGB_888:
269 bpp = 3;
270 break;
271 case HAL_PIXEL_FORMAT_RGB_565:
272 case HAL_PIXEL_FORMAT_RGBA_5551:
273 case HAL_PIXEL_FORMAT_RGBA_4444:
274 bpp = 2;
275 break;
276 default: break;
277 }
278
279 if (libadreno_utils) {
280 int raster_mode = 0; // Adreno unknown raster mode.
281 int padding_threshold = 512; // Threshold for padding surfaces.
282 // the function below computes aligned width and aligned height
283 // based on linear or macro tile mode selected.
284 if(LINK_adreno_compute_aligned_width_and_height) {
285 LINK_adreno_compute_aligned_width_and_height(width,
286 height, bpp, tile_enabled,
287 raster_mode, padding_threshold,
288 &aligned_w, &aligned_h);
289
290 } else if(LINK_adreno_compute_padding) {
291 int surface_tile_height = 1; // Linear surface
292 aligned_w = LINK_adreno_compute_padding(width, bpp,
293 surface_tile_height, raster_mode,
294 padding_threshold);
295 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
296 __FUNCTION__);
297 } else {
298 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
299 "compute_aligned_width_and_height not found", __FUNCTION__);
300 }
301 }
302}
303
304int AdrenoMemInfo::isUBWCSupportedByGPU(int format)
305{
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700306 if (!gfx_ubwc_disable && libadreno_utils) {
Sushil Chauhan082acd62015-01-14 16:49:29 -0800307 if (LINK_adreno_isUBWCSupportedByGpu) {
308 ADRENOPIXELFORMAT gpu_format = getGpuPixelFormat(format);
309 return LINK_adreno_isUBWCSupportedByGpu(gpu_format);
310 }
311 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800312 return 0;
Naomi Luisa44100c2013-02-08 12:42:03 -0800313}
314
Sushil Chauhan082acd62015-01-14 16:49:29 -0800315ADRENOPIXELFORMAT AdrenoMemInfo::getGpuPixelFormat(int hal_format)
316{
317 switch (hal_format) {
318 case HAL_PIXEL_FORMAT_RGBA_8888:
319 return ADRENO_PIXELFORMAT_R8G8B8A8;
Sushil Chauhan6686c802015-04-15 11:30:39 -0700320 case HAL_PIXEL_FORMAT_RGBX_8888:
321 return ADRENO_PIXELFORMAT_R8G8B8X8;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800322 case HAL_PIXEL_FORMAT_RGB_565:
323 return ADRENO_PIXELFORMAT_B5G6R5;
324 case HAL_PIXEL_FORMAT_sRGB_A_8888:
325 return ADRENO_PIXELFORMAT_R8G8B8A8_SRGB;
326 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800327 return ADRENO_PIXELFORMAT_NV12;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800328 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
329 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800330 return ADRENO_PIXELFORMAT_NV12_EXT;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800331 default:
332 ALOGE("%s: No map for format: 0x%x", __FUNCTION__, hal_format);
333 break;
334 }
335 return ADRENO_PIXELFORMAT_UNKNOWN;
336}
337
Naomi Luisa44100c2013-02-08 12:42:03 -0800338//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700339IAllocController* IAllocController::sController = NULL;
340IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700341{
342 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700343 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700344 }
345 return sController;
346}
347
348
349//-------------- IonController-----------------------//
350IonController::IonController()
351{
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530352 allocateIonMem();
353}
354
355void IonController::allocateIonMem()
356{
357 mIonAlloc = new IonAlloc();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700358}
359
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700360int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700361{
362 int ionFlags = 0;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500363 int ionHeapId = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700364 int ret;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700365
366 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700367 data.allocType = 0;
368
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530369 if(usage & GRALLOC_USAGE_PROTECTED) {
Prabhanjan Kandulae8f4bec2013-10-24 16:32:51 +0530370 if (usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800371 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
Shalaj Jain3c490412015-04-22 16:52:03 -0700372 ionHeapId = ION_HEAP(SD_HEAP_ID);
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800373 /*
374 * There is currently no flag in ION for Secure Display
Shalaj Jain3c490412015-04-22 16:52:03 -0700375 * VM. Please add it to the define once available.
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800376 */
Shalaj Jain3c490412015-04-22 16:52:03 -0700377 ionFlags |= ION_SD_FLAGS;
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800378 } else {
Shalaj Jain3c490412015-04-22 16:52:03 -0700379 ionHeapId = ION_HEAP(CP_HEAP_ID);
380 ionFlags |= ION_CP_FLAGS;
Shalaj Jain13cdf812014-12-02 16:20:54 -0800381 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530382 } else {
383 // for targets/OEMs which do not need HW level protection
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500384 // do not set ion secure flag & MM heap. Fallback to system heap.
385 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Justin Philipd6166602014-08-12 13:42:21 +0530386 data.allocType |= private_handle_t::PRIV_FLAGS_PROTECTED_BUFFER;
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500387 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530388 } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
389 //MM Heap is exclusively a secure heap.
390 //If it is used for non secure cases, fallback to IOMMU heap
391 ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
392 cannot be used as an insecure heap!\
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500393 trying to use system heap instead !!");
394 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500395 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700396
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700397 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500398 ionHeapId |= ION_HEAP(ION_CAMERA_HEAP_ID);
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700399
Arun Kumar K.R0daaa992013-03-12 15:08:29 -0700400 if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500401 ionHeapId |= ION_HEAP(ION_ADSP_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700402
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530403 if(ionFlags & ION_SECURE)
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500404 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700405
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500406 // if no ion heap flags are set, default to system heap
407 if(!ionHeapId)
408 ionHeapId = ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700409
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500410 //At this point we should have the right heap set, there is no fallback
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700411 data.flags = ionFlags;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500412 data.heapId = ionHeapId;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700413 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700414
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700415 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700416 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500417 } else {
418 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x",
419 __FUNCTION__, ionHeapId, ionFlags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700420 }
421
422 return ret;
423}
424
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700425IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700426{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700427 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700428 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
429 memalloc = mIonAlloc;
430 } else {
431 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
432 }
433
434 return memalloc;
435}
436
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700437bool isMacroTileEnabled(int format, int usage)
438{
439 bool tileEnabled = false;
440
441 // Check whether GPU & MDSS supports MacroTiling feature
442 if(AdrenoMemInfo::getInstance().isMacroTilingSupportedByGPU() &&
443 qdutils::MDPVersion::getInstance().supportsMacroTile())
444 {
445 // check the format
446 switch(format)
447 {
448 case HAL_PIXEL_FORMAT_RGBA_8888:
449 case HAL_PIXEL_FORMAT_RGBX_8888:
450 case HAL_PIXEL_FORMAT_BGRA_8888:
Manoj Kumar AVM5a5529b2014-02-24 18:16:37 -0800451 case HAL_PIXEL_FORMAT_RGB_565:
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700452 {
453 tileEnabled = true;
454 // check the usage flags
455 if (usage & (GRALLOC_USAGE_SW_READ_MASK |
456 GRALLOC_USAGE_SW_WRITE_MASK)) {
457 // Application intends to use CPU for rendering
458 tileEnabled = false;
459 }
460 break;
461 }
462 default:
463 break;
464 }
465 }
466 return tileEnabled;
467}
468
469// helper function
Sushil Chauhan65e26302015-01-14 10:48:57 -0800470unsigned int getSize(int format, int width, int height, int usage,
471 const int alignedw, const int alignedh) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700472
Sushil Chauhan65e26302015-01-14 10:48:57 -0800473 if (isUBwcEnabled(format, usage)) {
474 return getUBwcSize(width, height, format, alignedw, alignedh);
475 }
476
477 unsigned int size = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700478 switch (format) {
479 case HAL_PIXEL_FORMAT_RGBA_8888:
480 case HAL_PIXEL_FORMAT_RGBX_8888:
481 case HAL_PIXEL_FORMAT_BGRA_8888:
Naseer Ahmed82fc4b72013-09-20 01:31:37 -0700482 case HAL_PIXEL_FORMAT_sRGB_A_8888:
Jesse Hallfbe96d22013-09-20 01:39:43 -0700483 case HAL_PIXEL_FORMAT_sRGB_X_8888:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700484 size = alignedw * alignedh * 4;
485 break;
486 case HAL_PIXEL_FORMAT_RGB_888:
487 size = alignedw * alignedh * 3;
488 break;
489 case HAL_PIXEL_FORMAT_RGB_565:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700490 case HAL_PIXEL_FORMAT_RGBA_5551:
491 case HAL_PIXEL_FORMAT_RGBA_4444:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400492 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700493 size = alignedw * alignedh * 2;
494 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700495 case HAL_PIXEL_FORMAT_RAW10:
496 size = ALIGN(alignedw * alignedh, 4096);
497 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700498
499 // adreno formats
500 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
501 size = ALIGN(alignedw*alignedh, 4096);
502 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
503 break;
504 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
505 // The chroma plane is subsampled,
506 // but the pitch in bytes is unchanged
507 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700508 size = ALIGN( alignedw * alignedh, 8192);
509 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
510 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700511 case HAL_PIXEL_FORMAT_YV12:
512 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
513 ALOGE("w or h is odd for the YV12 format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800514 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700515 }
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400516 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700517 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700518 size = ALIGN(size, (unsigned int)4096);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700519 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800520 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
521 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naseer Ahmed2c215292013-09-18 23:47:42 -0400522 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800523 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700524 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
525 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700526 case HAL_PIXEL_FORMAT_YCbCr_422_I:
527 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700528 if(width & 1) {
529 ALOGE("width is odd for the YUV422_SP format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800530 return 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700531 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700532 size = ALIGN(alignedw * alignedh * 2, 4096);
533 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700534 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400535 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhane8a01792012-11-01 16:25:45 -0700536 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700537 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400538 case HAL_PIXEL_FORMAT_BLOB:
539 if(height != 1) {
540 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
541 must have height==1 ", __FUNCTION__);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800542 return 0;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400543 }
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400544 size = width;
545 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700546 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700547 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
548 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500549 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
550 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
551 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
552 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
553 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
554 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
555 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
556 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
557 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
558 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
559 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
560 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
561 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
562 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
563 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
564 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
565 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
566 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
567 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
568 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
569 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
570 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
571 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
572 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
573 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
574 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
575 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Jeykumar Sankaran8f4585f2014-02-05 15:23:40 -0800576 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500577 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
578 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700579 default:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800580 ALOGE("Unrecognized pixel format: 0x%x", __FUNCTION__, format);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800581 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700582 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700583 return size;
584}
585
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700586unsigned int getBufferSizeAndDimensions(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700587 int& alignedw, int &alignedh)
588{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700589 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700590
591 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
592 height,
593 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800594 0,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700595 alignedw,
596 alignedh);
597
Sushil Chauhan65e26302015-01-14 10:48:57 -0800598 size = getSize(format, width, height, 0 /* usage */, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700599
600 return size;
601}
602
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700603
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700604unsigned int getBufferSizeAndDimensions(int width, int height, int format,
605 int usage, int& alignedw, int &alignedh)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700606{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700607 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700608
609 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
610 height,
611 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800612 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700613 alignedw,
614 alignedh);
615
Sushil Chauhan65e26302015-01-14 10:48:57 -0800616 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700617
618 return size;
619}
620
621
622void getBufferAttributes(int width, int height, int format, int usage,
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700623 int& alignedw, int &alignedh, int& tileEnabled, unsigned int& size)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700624{
625 tileEnabled = isMacroTileEnabled(format, usage);
626
627 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
628 height,
629 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800630 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700631 alignedw,
632 alignedh);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800633 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700634}
635
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400636int getYUVPlaneInfo(private_handle_t* hnd, struct android_ycbcr* ycbcr)
637{
638 int err = 0;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700639 int width = hnd->width;
640 int height = hnd->height;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700641 unsigned int ystride, cstride;
Sushil Chauhan4686c972015-02-20 15:44:52 -0800642 unsigned int alignment = 4096;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700643
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400644 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
645
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700646 // Check metadata if the geometry has been updated.
647 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
648 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
649 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(metadata->bufferDim.sliceWidth,
650 metadata->bufferDim.sliceHeight, hnd->format, 0, width, height);
651 }
652
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400653 // Get the chroma offsets from the handle width/height. We take advantage
654 // of the fact the width _is_ the stride
655 switch (hnd->format) {
656 //Semiplanar
657 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
658 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
659 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
660 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: //Same as YCbCr_420_SP_VENUS
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700661 ystride = cstride = width;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400662 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700663 ycbcr->cb = (void*)(hnd->base + ystride * height);
664 ycbcr->cr = (void*)(hnd->base + ystride * height + 1);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400665 ycbcr->ystride = ystride;
666 ycbcr->cstride = cstride;
667 ycbcr->chroma_step = 2;
668 break;
669
Sushil Chauhan4686c972015-02-20 15:44:52 -0800670 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
671 // NV12_UBWC buffer has these 4 planes in the following sequence:
672 // Y_Meta_Plane, Y_Plane, UV_Meta_Plane, UV_Plane
673 unsigned int y_meta_stride, y_meta_height, y_meta_size;
674 unsigned int y_stride, y_height, y_size;
675 unsigned int c_meta_stride, c_meta_height, c_meta_size;
676
677 y_meta_stride = VENUS_Y_META_STRIDE(COLOR_FMT_NV12_UBWC, width);
678 y_meta_height = VENUS_Y_META_SCANLINES(COLOR_FMT_NV12_UBWC, height);
679 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
680
681 y_stride = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
682 y_height = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
683 y_size = ALIGN((y_stride * y_height), alignment);
684
685 c_meta_stride = VENUS_UV_META_STRIDE(COLOR_FMT_NV12_UBWC, width);
686 c_meta_height = VENUS_UV_META_SCANLINES(COLOR_FMT_NV12_UBWC, height);
687 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
688
689 ycbcr->y = (void*)(hnd->base + y_meta_size);
690 ycbcr->cb = (void*)(hnd->base + y_meta_size + y_size + c_meta_size);
691 ycbcr->cr = (void*)(hnd->base + y_meta_size + y_size +
692 c_meta_size + 1);
693 ycbcr->ystride = y_stride;
694 ycbcr->cstride = VENUS_UV_STRIDE(COLOR_FMT_NV12_UBWC, width);
695 ycbcr->chroma_step = 2;
696 break;
697
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400698 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
699 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
700 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
701 case HAL_PIXEL_FORMAT_NV21_ZSL:
702 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700703 case HAL_PIXEL_FORMAT_RAW10:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700704 ystride = cstride = width;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400705 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700706 ycbcr->cr = (void*)(hnd->base + ystride * height);
707 ycbcr->cb = (void*)(hnd->base + ystride * height + 1);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400708 ycbcr->ystride = ystride;
709 ycbcr->cstride = cstride;
710 ycbcr->chroma_step = 2;
711 break;
712
713 //Planar
714 case HAL_PIXEL_FORMAT_YV12:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700715 ystride = width;
716 cstride = ALIGN(width/2, 16);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400717 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700718 ycbcr->cr = (void*)(hnd->base + ystride * height);
719 ycbcr->cb = (void*)(hnd->base + ystride * height +
720 cstride * height/2);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400721 ycbcr->ystride = ystride;
722 ycbcr->cstride = cstride;
723 ycbcr->chroma_step = 1;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400724 break;
725 //Unsupported formats
726 case HAL_PIXEL_FORMAT_YCbCr_422_I:
727 case HAL_PIXEL_FORMAT_YCrCb_422_I:
728 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
729 default:
730 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__,
731 hnd->format);
732 err = -EINVAL;
733 }
734 return err;
735
736}
737
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700738
739
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700740// Allocate buffer from width, height and format into a
741// private_handle_t. It is the responsibility of the caller
742// to free the buffer using the free_buffer function
743int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
744{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700745 alloc_data data;
746 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700747 gralloc::IAllocController* sAlloc =
748 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700749 data.base = 0;
750 data.fd = -1;
751 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700752 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
753 alignedh);
754
Naseer Ahmed29a26812012-06-14 00:56:20 -0700755 data.align = getpagesize();
756 data.uncached = useUncached(usage);
757 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700758
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700759 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700760 if (0 != err) {
761 ALOGE("%s: allocate failed", __FUNCTION__);
762 return -ENOMEM;
763 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700764
Naseer Ahmed29a26812012-06-14 00:56:20 -0700765 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700766 data.allocType, 0, format,
767 alignedw, alignedh);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700768 hnd->base = (uint64_t) data.base;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700769 hnd->offset = data.offset;
770 hnd->gpuaddr = 0;
771 *pHnd = hnd;
772 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700773}
774
775void free_buffer(private_handle_t *hnd)
776{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700777 gralloc::IAllocController* sAlloc =
778 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700779 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700780 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700781 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
782 }
783 if(hnd)
784 delete hnd;
785
786}
Sushil Chauhan65e26302015-01-14 10:48:57 -0800787
788// UBWC helper functions
789static bool isUBwcFormat(int format)
790{
791 // Explicitly defined UBWC formats
792 switch(format)
793 {
794 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
795 return true;
796 default:
797 return false;
798 }
799}
800
801static bool isUBwcSupported(int format)
802{
803 // Existing HAL formats with UBWC support
804 switch(format)
805 {
806 case HAL_PIXEL_FORMAT_RGB_565:
807 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -0700808 case HAL_PIXEL_FORMAT_RGBX_8888:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800809 case HAL_PIXEL_FORMAT_sRGB_A_8888:
810 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
811 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
812 return true;
813 default:
814 return false;
815 }
816}
817
818bool isUBwcEnabled(int format, int usage)
819{
Sushil Chauhan81594f62015-01-26 16:00:51 -0800820 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
821 if (isUBwcFormat(format))
822 return true;
823
824 // Allow UBWC, if client sets UBWC gralloc usage flag & GPU supports format.
825 if ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) && isUBwcSupported(format) &&
826 AdrenoMemInfo::getInstance().isUBWCSupportedByGPU(format)) {
827 // Allow UBWC, only if CPU usage flags are not set
828 if (!(usage & (GRALLOC_USAGE_SW_READ_MASK |
Sushil Chauhan65e26302015-01-14 10:48:57 -0800829 GRALLOC_USAGE_SW_WRITE_MASK))) {
830 return true;
831 }
832 }
833 return false;
834}
835
836static void getUBwcWidthAndHeight(int width, int height, int format,
837 int& aligned_w, int& aligned_h)
838{
839 switch (format)
840 {
841 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
842 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
843 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
844 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
845 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
846 break;
847 default:
848 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
849 aligned_w = 0;
850 aligned_h = 0;
851 break;
852 }
853}
854
855static void getUBwcBlockSize(int bpp, int& block_width, int& block_height)
856{
857 block_width = 0;
858 block_height = 0;
859
860 switch(bpp)
861 {
862 case 2:
863 case 4:
864 block_width = 16;
865 block_height = 4;
866 break;
867 case 8:
868 block_width = 8;
869 block_height = 4;
870 break;
871 case 16:
872 block_width = 4;
873 block_height = 4;
874 break;
875 default:
876 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
877 break;
878 }
879}
880
881static unsigned int getUBwcMetaBufferSize(int width, int height, int bpp)
882{
883 unsigned int size = 0;
884 int meta_width, meta_height;
885 int block_width, block_height;
886
887 getUBwcBlockSize(bpp, block_width, block_height);
888
889 if (!block_width || !block_height) {
890 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
891 return size;
892 }
893
894 // Align meta buffer height to 16 blocks
895 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
896
897 // Align meta buffer width to 64 blocks
898 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
899
900 // Align meta buffer size to 4K
Sushil Chauhanc85b65b2015-04-30 11:05:36 -0700901 size = ALIGN((meta_width * meta_height), 4096);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800902 return size;
903}
904
905static unsigned int getUBwcSize(int width, int height, int format,
906 const int alignedw, const int alignedh) {
907
908 unsigned int size = 0;
909 switch (format) {
910 case HAL_PIXEL_FORMAT_RGB_565:
911 size = alignedw * alignedh * 2;
912 size += getUBwcMetaBufferSize(width, height, 2);
913 break;
914 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -0700915 case HAL_PIXEL_FORMAT_RGBX_8888:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800916 case HAL_PIXEL_FORMAT_sRGB_A_8888:
917 size = alignedw * alignedh * 4;
918 size += getUBwcMetaBufferSize(width, height, 4);
919 break;
920 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
921 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
922 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
923 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
924 break;
925 default:
926 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
927 break;
928 }
929 return size;
930}
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700931
Sushil Chauhanc85b65b2015-04-30 11:05:36 -0700932int getRgbDataAddress(private_handle_t* hnd, void** rgb_data)
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700933{
934 int err = 0;
935
936 // This api is for RGB* formats
937 if (hnd->format > HAL_PIXEL_FORMAT_sRGB_X_8888) {
938 return -EINVAL;
939 }
940
941 // linear buffer
942 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
Sushil Chauhanc85b65b2015-04-30 11:05:36 -0700943 *rgb_data = (void*)hnd->base;
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700944 return err;
945 }
946
947 unsigned int meta_size = 0;
948 switch (hnd->format) {
949 case HAL_PIXEL_FORMAT_RGB_565:
950 meta_size = getUBwcMetaBufferSize(hnd->width, hnd->height, 2);
951 break;
952 case HAL_PIXEL_FORMAT_RGBA_8888:
953 case HAL_PIXEL_FORMAT_RGBX_8888:
954 case HAL_PIXEL_FORMAT_sRGB_A_8888:
955 meta_size = getUBwcMetaBufferSize(hnd->width, hnd->height, 4);
956 break;
957 default:
958 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
959 err = -EINVAL;
960 break;
961 }
962
Sushil Chauhanc85b65b2015-04-30 11:05:36 -0700963 *rgb_data = (void*)(hnd->base + meta_size);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700964 return err;
965}