blob: 98079c20ef620b51f6fab30fbb9cb2c426264f93 [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
64/* Please Add the new SD ION Heap here */
65#define SD_HEAP_ID 0
66#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_CP_PIXEL)
67/* Please Add the new SD ION Flag here */
68#define ION_SD_FLAGS ION_SECURE
69#else // SLAVE_SIDE_CP
70#define CP_HEAP_ID ION_CP_MM_HEAP_ID
71#define SD_HEAP_ID CP_HEAP_ID
72#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_ALLOW_NON_CONTIG)
73#define ION_SD_FLAGS ION_SECURE
74#endif
75
Iliyan Malchev202a77d2012-06-11 14:41:12 -070076using namespace gralloc;
Naseer Ahmeda87da602012-07-01 23:54:19 -070077using namespace qdutils;
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -070078using namespace android;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070079
Naomi Luisa44100c2013-02-08 12:42:03 -080080ANDROID_SINGLETON_STATIC_INSTANCE(AdrenoMemInfo);
81
Sushil Chauhan65e26302015-01-14 10:48:57 -080082static void getUBwcWidthAndHeight(int, int, int, int&, int&);
83static unsigned int getUBwcSize(int, int, int, const int, const int);
84
Iliyan Malchev202a77d2012-06-11 14:41:12 -070085//Common functions
Iliyan Malchev202a77d2012-06-11 14:41:12 -070086
Saurabh Shah1adcafe2014-12-19 10:05:41 -080087/* The default policy is to return cached buffers unless the client explicity
88 * sets the PRIVATE_UNCACHED flag or indicates that the buffer will be rarely
89 * read or written in software. Any combination with a _RARELY_ flag will be
90 * treated as uncached. */
91static bool useUncached(const int& usage) {
92 if((usage & GRALLOC_USAGE_PRIVATE_UNCACHED) or
93 ((usage & GRALLOC_USAGE_SW_WRITE_MASK) ==
94 GRALLOC_USAGE_SW_WRITE_RARELY) or
95 ((usage & GRALLOC_USAGE_SW_READ_MASK) ==
96 GRALLOC_USAGE_SW_READ_RARELY))
97 return true;
98
99 return false;
100}
101
Naomi Luisa44100c2013-02-08 12:42:03 -0800102//-------------- AdrenoMemInfo-----------------------//
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800103AdrenoMemInfo::AdrenoMemInfo()
104{
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800105 LINK_adreno_compute_aligned_width_and_height = NULL;
106 LINK_adreno_compute_padding = NULL;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700107 LINK_adreno_isMacroTilingSupportedByGpu = NULL;
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800108 LINK_adreno_compute_compressedfmt_aligned_width_and_height = NULL;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800109 LINK_adreno_isUBWCSupportedByGpu = NULL;
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800110
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800111 libadreno_utils = ::dlopen("libadreno_utils.so", RTLD_NOW);
112 if (libadreno_utils) {
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800113 *(void **)&LINK_adreno_compute_aligned_width_and_height =
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700114 ::dlsym(libadreno_utils, "compute_aligned_width_and_height");
115 *(void **)&LINK_adreno_compute_padding =
116 ::dlsym(libadreno_utils, "compute_surface_padding");
117 *(void **)&LINK_adreno_isMacroTilingSupportedByGpu =
118 ::dlsym(libadreno_utils, "isMacroTilingSupportedByGpu");
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800119 *(void **)&LINK_adreno_compute_compressedfmt_aligned_width_and_height =
120 ::dlsym(libadreno_utils,
121 "compute_compressedfmt_aligned_width_and_height");
Sushil Chauhan082acd62015-01-14 16:49:29 -0800122 *(void **)&LINK_adreno_isUBWCSupportedByGpu =
123 ::dlsym(libadreno_utils, "isUBWCSupportedByGpu");
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800124 }
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700125
126 // Check if the overriding property debug.gralloc.gfx_ubwc_disable
127 // that disables UBWC allocations for the graphics stack is set
128 gfx_ubwc_disable = 0;
129 char property[PROPERTY_VALUE_MAX];
130 property_get("debug.gralloc.gfx_ubwc_disable", property, "0");
131 if(!(strncmp(property, "1", PROPERTY_VALUE_MAX)) ||
132 !(strncmp(property, "true", PROPERTY_VALUE_MAX))) {
133 gfx_ubwc_disable = 1;
134 }
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800135}
136
137AdrenoMemInfo::~AdrenoMemInfo()
138{
139 if (libadreno_utils) {
140 ::dlclose(libadreno_utils);
141 }
142}
143
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700144int AdrenoMemInfo::isMacroTilingSupportedByGPU()
145{
146 if ((libadreno_utils)) {
147 if(LINK_adreno_isMacroTilingSupportedByGpu) {
148 return LINK_adreno_isMacroTilingSupportedByGpu();
149 }
150 }
151 return 0;
152}
153
154
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800155void AdrenoMemInfo::getAlignedWidthAndHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800156 int usage, int& aligned_w, int& aligned_h)
Naomi Luisa44100c2013-02-08 12:42:03 -0800157{
Sushil Chauhan65e26302015-01-14 10:48:57 -0800158
Sushil Chauhane61fac52015-04-30 17:14:37 -0700159 bool ubwc_enabled = isUBwcEnabled(format, usage);
160
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800161 // Currently surface padding is only computed for RGB* surfaces.
Jesse Hallfbe96d22013-09-20 01:39:43 -0700162 if (format <= HAL_PIXEL_FORMAT_sRGB_X_8888) {
Sushil Chauhane61fac52015-04-30 17:14:37 -0700163 int tileEnabled = ubwc_enabled || isMacroTileEnabled(format, usage);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800164 AdrenoMemInfo::getInstance().getGpuAlignedWidthHeight(width,
165 height, format, tileEnabled, aligned_w, aligned_h);
166 return;
Naomi Luisa44100c2013-02-08 12:42:03 -0800167 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800168
Sushil Chauhane61fac52015-04-30 17:14:37 -0700169 if (ubwc_enabled) {
Sushil Chauhan65e26302015-01-14 10:48:57 -0800170 getUBwcWidthAndHeight(width, height, format, aligned_w, aligned_h);
171 return;
172 }
173
174 aligned_w = width;
175 aligned_h = height;
176 switch (format)
177 {
178 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
179 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800180 aligned_w = ALIGN(width, 32);
181 break;
Shuzhen Wang2a000b22014-08-20 00:15:51 -0700182 case HAL_PIXEL_FORMAT_RAW_SENSOR:
183 aligned_w = ALIGN(width, 16);
184 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700185 case HAL_PIXEL_FORMAT_RAW10:
186 aligned_w = ALIGN(width * 10 /8, 16);
187 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800188 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
189 aligned_w = ALIGN(width, 128);
190 break;
191 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
192 case HAL_PIXEL_FORMAT_YV12:
193 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
194 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
195 case HAL_PIXEL_FORMAT_YCbCr_422_I:
196 case HAL_PIXEL_FORMAT_YCrCb_422_I:
197 aligned_w = ALIGN(width, 16);
198 break;
199 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
200 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
201 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
202 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
203 break;
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530204 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
205 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV21, width);
206 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV21, height);
207 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800208 case HAL_PIXEL_FORMAT_BLOB:
209 break;
210 case HAL_PIXEL_FORMAT_NV21_ZSL:
211 aligned_w = ALIGN(width, 64);
212 aligned_h = ALIGN(height, 64);
213 break;
214 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
215 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
216 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
217 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
218 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
219 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
220 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
221 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
222 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
223 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
224 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
225 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
226 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
227 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
228 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
229 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
230 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
231 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
232 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
233 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
234 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
235 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
236 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
237 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
238 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
239 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
240 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
241 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
242 if(LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
243 int bytesPerPixel = 0;
244 int raster_mode = 0; //Adreno unknown raster mode.
245 int padding_threshold = 512; //Threshold for padding
246 //surfaces.
247
248 LINK_adreno_compute_compressedfmt_aligned_width_and_height(
249 width, height, format, 0,raster_mode, padding_threshold,
250 &aligned_w, &aligned_h, &bytesPerPixel);
251 } else {
252 ALOGW("%s: Warning!! Symbols" \
253 " compute_compressedfmt_aligned_width_and_height" \
254 " not found", __FUNCTION__);
255 }
256 break;
257 default: break;
258 }
259}
260
261void AdrenoMemInfo::getGpuAlignedWidthHeight(int width, int height, int format,
262 int tile_enabled, int& aligned_w, int& aligned_h)
263{
264 aligned_w = ALIGN(width, 32);
265 aligned_h = ALIGN(height, 32);
266
267 // Don't add any additional padding if debug.gralloc.map_fb_memory
268 // is enabled
269 char property[PROPERTY_VALUE_MAX];
270 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
271 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
272 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
273 return;
274 }
275
276 int bpp = 4;
277 switch(format)
278 {
279 case HAL_PIXEL_FORMAT_RGB_888:
280 bpp = 3;
281 break;
282 case HAL_PIXEL_FORMAT_RGB_565:
283 case HAL_PIXEL_FORMAT_RGBA_5551:
284 case HAL_PIXEL_FORMAT_RGBA_4444:
285 bpp = 2;
286 break;
287 default: break;
288 }
289
290 if (libadreno_utils) {
291 int raster_mode = 0; // Adreno unknown raster mode.
292 int padding_threshold = 512; // Threshold for padding surfaces.
293 // the function below computes aligned width and aligned height
294 // based on linear or macro tile mode selected.
295 if(LINK_adreno_compute_aligned_width_and_height) {
296 LINK_adreno_compute_aligned_width_and_height(width,
297 height, bpp, tile_enabled,
298 raster_mode, padding_threshold,
299 &aligned_w, &aligned_h);
300
301 } else if(LINK_adreno_compute_padding) {
302 int surface_tile_height = 1; // Linear surface
303 aligned_w = LINK_adreno_compute_padding(width, bpp,
304 surface_tile_height, raster_mode,
305 padding_threshold);
306 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
307 __FUNCTION__);
308 } else {
309 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
310 "compute_aligned_width_and_height not found", __FUNCTION__);
311 }
312 }
313}
314
315int AdrenoMemInfo::isUBWCSupportedByGPU(int format)
316{
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700317 if (!gfx_ubwc_disable && libadreno_utils) {
Sushil Chauhan082acd62015-01-14 16:49:29 -0800318 if (LINK_adreno_isUBWCSupportedByGpu) {
319 ADRENOPIXELFORMAT gpu_format = getGpuPixelFormat(format);
320 return LINK_adreno_isUBWCSupportedByGpu(gpu_format);
321 }
322 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800323 return 0;
Naomi Luisa44100c2013-02-08 12:42:03 -0800324}
325
Sushil Chauhan082acd62015-01-14 16:49:29 -0800326ADRENOPIXELFORMAT AdrenoMemInfo::getGpuPixelFormat(int hal_format)
327{
328 switch (hal_format) {
329 case HAL_PIXEL_FORMAT_RGBA_8888:
330 return ADRENO_PIXELFORMAT_R8G8B8A8;
Sushil Chauhan6686c802015-04-15 11:30:39 -0700331 case HAL_PIXEL_FORMAT_RGBX_8888:
332 return ADRENO_PIXELFORMAT_R8G8B8X8;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800333 case HAL_PIXEL_FORMAT_RGB_565:
334 return ADRENO_PIXELFORMAT_B5G6R5;
335 case HAL_PIXEL_FORMAT_sRGB_A_8888:
336 return ADRENO_PIXELFORMAT_R8G8B8A8_SRGB;
337 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800338 return ADRENO_PIXELFORMAT_NV12;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800339 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
340 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800341 return ADRENO_PIXELFORMAT_NV12_EXT;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800342 default:
343 ALOGE("%s: No map for format: 0x%x", __FUNCTION__, hal_format);
344 break;
345 }
346 return ADRENO_PIXELFORMAT_UNKNOWN;
347}
348
Naomi Luisa44100c2013-02-08 12:42:03 -0800349//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700350IAllocController* IAllocController::sController = NULL;
351IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700352{
353 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700354 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700355 }
356 return sController;
357}
358
359
360//-------------- IonController-----------------------//
361IonController::IonController()
362{
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530363 allocateIonMem();
364}
365
366void IonController::allocateIonMem()
367{
368 mIonAlloc = new IonAlloc();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700369}
370
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700371int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700372{
373 int ionFlags = 0;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500374 int ionHeapId = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700375 int ret;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700376
377 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700378 data.allocType = 0;
379
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530380 if(usage & GRALLOC_USAGE_PROTECTED) {
Prabhanjan Kandulae8f4bec2013-10-24 16:32:51 +0530381 if (usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800382 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
Shalaj Jain3c490412015-04-22 16:52:03 -0700383 ionHeapId = ION_HEAP(SD_HEAP_ID);
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800384 /*
385 * There is currently no flag in ION for Secure Display
Shalaj Jain3c490412015-04-22 16:52:03 -0700386 * VM. Please add it to the define once available.
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800387 */
Shalaj Jain3c490412015-04-22 16:52:03 -0700388 ionFlags |= ION_SD_FLAGS;
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800389 } else {
Shalaj Jain3c490412015-04-22 16:52:03 -0700390 ionHeapId = ION_HEAP(CP_HEAP_ID);
391 ionFlags |= ION_CP_FLAGS;
Shalaj Jain13cdf812014-12-02 16:20:54 -0800392 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530393 } else {
394 // for targets/OEMs which do not need HW level protection
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500395 // do not set ion secure flag & MM heap. Fallback to system heap.
396 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Justin Philipd6166602014-08-12 13:42:21 +0530397 data.allocType |= private_handle_t::PRIV_FLAGS_PROTECTED_BUFFER;
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500398 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530399 } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
400 //MM Heap is exclusively a secure heap.
401 //If it is used for non secure cases, fallback to IOMMU heap
402 ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
403 cannot be used as an insecure heap!\
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500404 trying to use system heap instead !!");
405 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500406 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700407
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700408 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500409 ionHeapId |= ION_HEAP(ION_CAMERA_HEAP_ID);
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700410
Arun Kumar K.R0daaa992013-03-12 15:08:29 -0700411 if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500412 ionHeapId |= ION_HEAP(ION_ADSP_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700413
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530414 if(ionFlags & ION_SECURE)
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500415 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700416
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500417 // if no ion heap flags are set, default to system heap
418 if(!ionHeapId)
419 ionHeapId = ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700420
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500421 //At this point we should have the right heap set, there is no fallback
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700422 data.flags = ionFlags;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500423 data.heapId = ionHeapId;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700424 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700425
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700426 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700427 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500428 } else {
429 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x",
430 __FUNCTION__, ionHeapId, ionFlags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700431 }
432
433 return ret;
434}
435
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700436IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700437{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700438 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700439 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
440 memalloc = mIonAlloc;
441 } else {
442 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
443 }
444
445 return memalloc;
446}
447
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700448bool isMacroTileEnabled(int format, int usage)
449{
450 bool tileEnabled = false;
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -0700451 int isMacroTileSupportedByMDP = 0;
452
453 qdutils::querySDEInfo(HAS_MACRO_TILE, &isMacroTileSupportedByMDP);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700454
455 // Check whether GPU & MDSS supports MacroTiling feature
456 if(AdrenoMemInfo::getInstance().isMacroTilingSupportedByGPU() &&
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -0700457 isMacroTileSupportedByMDP)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700458 {
459 // check the format
460 switch(format)
461 {
462 case HAL_PIXEL_FORMAT_RGBA_8888:
463 case HAL_PIXEL_FORMAT_RGBX_8888:
464 case HAL_PIXEL_FORMAT_BGRA_8888:
Manoj Kumar AVM5a5529b2014-02-24 18:16:37 -0800465 case HAL_PIXEL_FORMAT_RGB_565:
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700466 {
467 tileEnabled = true;
468 // check the usage flags
469 if (usage & (GRALLOC_USAGE_SW_READ_MASK |
470 GRALLOC_USAGE_SW_WRITE_MASK)) {
471 // Application intends to use CPU for rendering
472 tileEnabled = false;
473 }
474 break;
475 }
476 default:
477 break;
478 }
479 }
480 return tileEnabled;
481}
482
483// helper function
Sushil Chauhan65e26302015-01-14 10:48:57 -0800484unsigned int getSize(int format, int width, int height, int usage,
485 const int alignedw, const int alignedh) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700486
Sushil Chauhan65e26302015-01-14 10:48:57 -0800487 if (isUBwcEnabled(format, usage)) {
488 return getUBwcSize(width, height, format, alignedw, alignedh);
489 }
490
491 unsigned int size = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700492 switch (format) {
493 case HAL_PIXEL_FORMAT_RGBA_8888:
494 case HAL_PIXEL_FORMAT_RGBX_8888:
495 case HAL_PIXEL_FORMAT_BGRA_8888:
Naseer Ahmed82fc4b72013-09-20 01:31:37 -0700496 case HAL_PIXEL_FORMAT_sRGB_A_8888:
Jesse Hallfbe96d22013-09-20 01:39:43 -0700497 case HAL_PIXEL_FORMAT_sRGB_X_8888:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700498 size = alignedw * alignedh * 4;
499 break;
500 case HAL_PIXEL_FORMAT_RGB_888:
501 size = alignedw * alignedh * 3;
502 break;
503 case HAL_PIXEL_FORMAT_RGB_565:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700504 case HAL_PIXEL_FORMAT_RGBA_5551:
505 case HAL_PIXEL_FORMAT_RGBA_4444:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400506 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700507 size = alignedw * alignedh * 2;
508 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700509 case HAL_PIXEL_FORMAT_RAW10:
510 size = ALIGN(alignedw * alignedh, 4096);
511 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700512
513 // adreno formats
514 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
515 size = ALIGN(alignedw*alignedh, 4096);
516 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
517 break;
518 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
519 // The chroma plane is subsampled,
520 // but the pitch in bytes is unchanged
521 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700522 size = ALIGN( alignedw * alignedh, 8192);
523 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
524 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700525 case HAL_PIXEL_FORMAT_YV12:
526 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
527 ALOGE("w or h is odd for the YV12 format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800528 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700529 }
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400530 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700531 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700532 size = ALIGN(size, (unsigned int)4096);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700533 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800534 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
535 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naseer Ahmed2c215292013-09-18 23:47:42 -0400536 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800537 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700538 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
539 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700540 case HAL_PIXEL_FORMAT_YCbCr_422_I:
541 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700542 if(width & 1) {
543 ALOGE("width is odd for the YUV422_SP format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800544 return 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700545 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700546 size = ALIGN(alignedw * alignedh * 2, 4096);
547 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700548 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400549 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhane8a01792012-11-01 16:25:45 -0700550 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700551 break;
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530552 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
553 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
554 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400555 case HAL_PIXEL_FORMAT_BLOB:
556 if(height != 1) {
557 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
558 must have height==1 ", __FUNCTION__);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800559 return 0;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400560 }
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400561 size = width;
562 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700563 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700564 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
565 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500566 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
567 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
568 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
569 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
570 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
571 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
572 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
573 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
574 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
575 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
576 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
577 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
578 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
579 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
580 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
581 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
582 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
583 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
584 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
585 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
586 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
587 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
588 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
589 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
590 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
591 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
592 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Jeykumar Sankaran8f4585f2014-02-05 15:23:40 -0800593 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500594 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
595 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700596 default:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800597 ALOGE("Unrecognized pixel format: 0x%x", __FUNCTION__, format);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800598 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700599 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700600 return size;
601}
602
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700603unsigned int getBufferSizeAndDimensions(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700604 int& alignedw, int &alignedh)
605{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700606 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700607
608 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
609 height,
610 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800611 0,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700612 alignedw,
613 alignedh);
614
Sushil Chauhan65e26302015-01-14 10:48:57 -0800615 size = getSize(format, width, height, 0 /* usage */, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700616
617 return size;
618}
619
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700620
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700621unsigned int getBufferSizeAndDimensions(int width, int height, int format,
622 int usage, int& alignedw, int &alignedh)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700623{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700624 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700625
626 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
627 height,
628 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800629 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700630 alignedw,
631 alignedh);
632
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 return size;
636}
637
638
639void getBufferAttributes(int width, int height, int format, int usage,
Sushil Chauhane61fac52015-04-30 17:14:37 -0700640 int& alignedw, int &alignedh, int& tiled, unsigned int& size)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700641{
Sushil Chauhane61fac52015-04-30 17:14:37 -0700642 tiled = isUBwcEnabled(format, usage) || isMacroTileEnabled(format, usage);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700643
644 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
645 height,
646 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800647 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700648 alignedw,
649 alignedh);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800650 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700651}
652
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400653int getYUVPlaneInfo(private_handle_t* hnd, struct android_ycbcr* ycbcr)
654{
655 int err = 0;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700656 int width = hnd->width;
657 int height = hnd->height;
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700658 int format = hnd->format;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700659 unsigned int ystride, cstride;
Sushil Chauhan4686c972015-02-20 15:44:52 -0800660 unsigned int alignment = 4096;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700661
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400662 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700663 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
664
665 // Check if UBWC buffer has been rendered in linear format.
666 if (metadata && (metadata->operation & LINEAR_FORMAT)) {
667 format = metadata->linearFormat;
668 }
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400669
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700670 // Check metadata if the geometry has been updated.
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700671 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
672 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(metadata->bufferDim.sliceWidth,
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700673 metadata->bufferDim.sliceHeight, format, 0, width, height);
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700674 }
675
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400676 // Get the chroma offsets from the handle width/height. We take advantage
677 // of the fact the width _is_ the stride
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700678 switch (format) {
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400679 //Semiplanar
680 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
681 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
682 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
683 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: //Same as YCbCr_420_SP_VENUS
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700684 ystride = cstride = width;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400685 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700686 ycbcr->cb = (void*)(hnd->base + ystride * height);
687 ycbcr->cr = (void*)(hnd->base + ystride * height + 1);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400688 ycbcr->ystride = ystride;
689 ycbcr->cstride = cstride;
690 ycbcr->chroma_step = 2;
691 break;
692
Sushil Chauhan4686c972015-02-20 15:44:52 -0800693 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
694 // NV12_UBWC buffer has these 4 planes in the following sequence:
695 // Y_Meta_Plane, Y_Plane, UV_Meta_Plane, UV_Plane
696 unsigned int y_meta_stride, y_meta_height, y_meta_size;
697 unsigned int y_stride, y_height, y_size;
698 unsigned int c_meta_stride, c_meta_height, c_meta_size;
699
700 y_meta_stride = VENUS_Y_META_STRIDE(COLOR_FMT_NV12_UBWC, width);
701 y_meta_height = VENUS_Y_META_SCANLINES(COLOR_FMT_NV12_UBWC, height);
702 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
703
704 y_stride = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
705 y_height = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
706 y_size = ALIGN((y_stride * y_height), alignment);
707
708 c_meta_stride = VENUS_UV_META_STRIDE(COLOR_FMT_NV12_UBWC, width);
709 c_meta_height = VENUS_UV_META_SCANLINES(COLOR_FMT_NV12_UBWC, height);
710 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
711
712 ycbcr->y = (void*)(hnd->base + y_meta_size);
713 ycbcr->cb = (void*)(hnd->base + y_meta_size + y_size + c_meta_size);
714 ycbcr->cr = (void*)(hnd->base + y_meta_size + y_size +
715 c_meta_size + 1);
716 ycbcr->ystride = y_stride;
717 ycbcr->cstride = VENUS_UV_STRIDE(COLOR_FMT_NV12_UBWC, width);
718 ycbcr->chroma_step = 2;
719 break;
720
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400721 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
722 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
723 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530724 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400725 case HAL_PIXEL_FORMAT_NV21_ZSL:
726 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700727 case HAL_PIXEL_FORMAT_RAW10:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700728 ystride = cstride = width;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400729 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700730 ycbcr->cr = (void*)(hnd->base + ystride * height);
731 ycbcr->cb = (void*)(hnd->base + ystride * height + 1);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400732 ycbcr->ystride = ystride;
733 ycbcr->cstride = cstride;
734 ycbcr->chroma_step = 2;
735 break;
736
737 //Planar
738 case HAL_PIXEL_FORMAT_YV12:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700739 ystride = width;
740 cstride = ALIGN(width/2, 16);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400741 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700742 ycbcr->cr = (void*)(hnd->base + ystride * height);
743 ycbcr->cb = (void*)(hnd->base + ystride * height +
744 cstride * height/2);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400745 ycbcr->ystride = ystride;
746 ycbcr->cstride = cstride;
747 ycbcr->chroma_step = 1;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400748 break;
749 //Unsupported formats
750 case HAL_PIXEL_FORMAT_YCbCr_422_I:
751 case HAL_PIXEL_FORMAT_YCrCb_422_I:
752 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
753 default:
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700754 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, format);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400755 err = -EINVAL;
756 }
757 return err;
758
759}
760
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700761
762
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700763// Allocate buffer from width, height and format into a
764// private_handle_t. It is the responsibility of the caller
765// to free the buffer using the free_buffer function
766int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
767{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700768 alloc_data data;
769 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700770 gralloc::IAllocController* sAlloc =
771 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700772 data.base = 0;
773 data.fd = -1;
774 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700775 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
776 alignedh);
777
Naseer Ahmed29a26812012-06-14 00:56:20 -0700778 data.align = getpagesize();
779 data.uncached = useUncached(usage);
780 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700781
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700782 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700783 if (0 != err) {
784 ALOGE("%s: allocate failed", __FUNCTION__);
785 return -ENOMEM;
786 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700787
Naseer Ahmed29a26812012-06-14 00:56:20 -0700788 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700789 data.allocType, 0, format,
790 alignedw, alignedh);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700791 hnd->base = (uint64_t) data.base;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700792 hnd->offset = data.offset;
793 hnd->gpuaddr = 0;
794 *pHnd = hnd;
795 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700796}
797
798void free_buffer(private_handle_t *hnd)
799{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700800 gralloc::IAllocController* sAlloc =
801 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700802 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700803 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700804 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
805 }
806 if(hnd)
807 delete hnd;
808
809}
Sushil Chauhan65e26302015-01-14 10:48:57 -0800810
811// UBWC helper functions
812static bool isUBwcFormat(int format)
813{
814 // Explicitly defined UBWC formats
815 switch(format)
816 {
817 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
818 return true;
819 default:
820 return false;
821 }
822}
823
824static bool isUBwcSupported(int format)
825{
826 // Existing HAL formats with UBWC support
827 switch(format)
828 {
829 case HAL_PIXEL_FORMAT_RGB_565:
830 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -0700831 case HAL_PIXEL_FORMAT_RGBX_8888:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800832 case HAL_PIXEL_FORMAT_sRGB_A_8888:
833 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
834 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
835 return true;
836 default:
837 return false;
838 }
839}
840
841bool isUBwcEnabled(int format, int usage)
842{
Sushil Chauhan81594f62015-01-26 16:00:51 -0800843 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
844 if (isUBwcFormat(format))
845 return true;
846
847 // Allow UBWC, if client sets UBWC gralloc usage flag & GPU supports format.
848 if ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) && isUBwcSupported(format) &&
849 AdrenoMemInfo::getInstance().isUBWCSupportedByGPU(format)) {
850 // Allow UBWC, only if CPU usage flags are not set
851 if (!(usage & (GRALLOC_USAGE_SW_READ_MASK |
Sushil Chauhan65e26302015-01-14 10:48:57 -0800852 GRALLOC_USAGE_SW_WRITE_MASK))) {
853 return true;
854 }
855 }
856 return false;
857}
858
859static void getUBwcWidthAndHeight(int width, int height, int format,
860 int& aligned_w, int& aligned_h)
861{
862 switch (format)
863 {
864 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
865 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
866 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
867 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
868 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
869 break;
870 default:
871 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
872 aligned_w = 0;
873 aligned_h = 0;
874 break;
875 }
876}
877
878static void getUBwcBlockSize(int bpp, int& block_width, int& block_height)
879{
880 block_width = 0;
881 block_height = 0;
882
883 switch(bpp)
884 {
885 case 2:
886 case 4:
887 block_width = 16;
888 block_height = 4;
889 break;
890 case 8:
891 block_width = 8;
892 block_height = 4;
893 break;
894 case 16:
895 block_width = 4;
896 block_height = 4;
897 break;
898 default:
899 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
900 break;
901 }
902}
903
904static unsigned int getUBwcMetaBufferSize(int width, int height, int bpp)
905{
906 unsigned int size = 0;
907 int meta_width, meta_height;
908 int block_width, block_height;
909
910 getUBwcBlockSize(bpp, block_width, block_height);
911
912 if (!block_width || !block_height) {
913 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
914 return size;
915 }
916
917 // Align meta buffer height to 16 blocks
918 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
919
920 // Align meta buffer width to 64 blocks
921 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
922
923 // Align meta buffer size to 4K
Sushil Chauhanc85b65b2015-04-30 11:05:36 -0700924 size = ALIGN((meta_width * meta_height), 4096);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800925 return size;
926}
927
928static unsigned int getUBwcSize(int width, int height, int format,
929 const int alignedw, const int alignedh) {
930
931 unsigned int size = 0;
932 switch (format) {
933 case HAL_PIXEL_FORMAT_RGB_565:
934 size = alignedw * alignedh * 2;
935 size += getUBwcMetaBufferSize(width, height, 2);
936 break;
937 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -0700938 case HAL_PIXEL_FORMAT_RGBX_8888:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800939 case HAL_PIXEL_FORMAT_sRGB_A_8888:
940 size = alignedw * alignedh * 4;
941 size += getUBwcMetaBufferSize(width, height, 4);
942 break;
943 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
944 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
945 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
946 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
947 break;
948 default:
949 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
950 break;
951 }
952 return size;
953}
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700954
Sushil Chauhanc85b65b2015-04-30 11:05:36 -0700955int getRgbDataAddress(private_handle_t* hnd, void** rgb_data)
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700956{
957 int err = 0;
958
959 // This api is for RGB* formats
960 if (hnd->format > HAL_PIXEL_FORMAT_sRGB_X_8888) {
961 return -EINVAL;
962 }
963
964 // linear buffer
965 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
Sushil Chauhanc85b65b2015-04-30 11:05:36 -0700966 *rgb_data = (void*)hnd->base;
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700967 return err;
968 }
969
970 unsigned int meta_size = 0;
971 switch (hnd->format) {
972 case HAL_PIXEL_FORMAT_RGB_565:
973 meta_size = getUBwcMetaBufferSize(hnd->width, hnd->height, 2);
974 break;
975 case HAL_PIXEL_FORMAT_RGBA_8888:
976 case HAL_PIXEL_FORMAT_RGBX_8888:
977 case HAL_PIXEL_FORMAT_sRGB_A_8888:
978 meta_size = getUBwcMetaBufferSize(hnd->width, hnd->height, 4);
979 break;
980 default:
981 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
982 err = -EINVAL;
983 break;
984 }
985
Sushil Chauhanc85b65b2015-04-30 11:05:36 -0700986 *rgb_data = (void*)(hnd->base + meta_size);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700987 return err;
988}