blob: b03cea325f5517fbaf63e7dbcdb7e741f195b067 [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
Mansoor Aftabe9912a62014-07-15 01:40:26 -07002 * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
Iliyan Malchev202a77d2012-06-11 14:41:12 -07003
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
Duy Truong73d36df2013-02-09 20:33:23 -080013 * * Neither the name of The Linux Foundation nor the names of its
Iliyan Malchev202a77d2012-06-11 14:41:12 -070014 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <cutils/log.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070031#include <fcntl.h>
Naomi Luis01f5c8e2013-02-11 12:46:24 -080032#include <dlfcn.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070033#include "gralloc_priv.h"
34#include "alloc_controller.h"
35#include "memalloc.h"
36#include "ionalloc.h"
Iliyan Malchev202a77d2012-06-11 14:41:12 -070037#include "gr.h"
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -070038#include "qd_utils.h"
Kaushik Kanetkar071aca62015-01-22 23:16:26 -070039#include <qdMetaData.h>
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -070040#include <utils/Singleton.h>
41#include <utils/Mutex.h>
42
Iliyan Malchev202a77d2012-06-11 14:41:12 -070043
Sushil Chauhanc6bd6d92012-12-12 12:33:01 -080044#ifdef VENUS_COLOR_FORMAT
45#include <media/msm_media_info.h>
46#else
47#define VENUS_Y_STRIDE(args...) 0
48#define VENUS_Y_SCANLINES(args...) 0
49#define VENUS_BUFFER_SIZE(args...) 0
50#endif
51
Naseer Ahmed63326f42013-12-18 02:45:48 -050052#define ASTC_BLOCK_SIZE 16
Naseer Ahmed63326f42013-12-18 02:45:48 -050053
Shalaj Jain3c490412015-04-22 16:52:03 -070054#ifndef ION_FLAG_CP_PIXEL
Shalaj Jain1f9725a2015-03-04 17:53:49 -080055#define ION_FLAG_CP_PIXEL 0
Shalaj Jain1f9725a2015-03-04 17:53:49 -080056#endif
57
58#ifndef ION_FLAG_ALLOW_NON_CONTIG
59#define ION_FLAG_ALLOW_NON_CONTIG 0
60#endif
61
Shalaj Jain3c490412015-04-22 16:52:03 -070062#ifdef MASTER_SIDE_CP
63#define CP_HEAP_ID ION_SECURE_HEAP_ID
Arun Kumar K.R7f0b24b2015-07-05 21:20:57 -070064#define SD_HEAP_ID ION_SECURE_DISPLAY_HEAP_ID
Shalaj Jain3c490412015-04-22 16:52:03 -070065#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_CP_PIXEL)
Arun Kumar K.R7f0b24b2015-07-05 21:20:57 -070066#define ION_SD_FLAGS (ION_SECURE | ION_FLAG_CP_SEC_DISPLAY)
Shalaj Jain3c490412015-04-22 16:52:03 -070067#else // SLAVE_SIDE_CP
68#define CP_HEAP_ID ION_CP_MM_HEAP_ID
69#define SD_HEAP_ID CP_HEAP_ID
70#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_ALLOW_NON_CONTIG)
71#define ION_SD_FLAGS ION_SECURE
72#endif
73
Iliyan Malchev202a77d2012-06-11 14:41:12 -070074using namespace gralloc;
Naseer Ahmeda87da602012-07-01 23:54:19 -070075using namespace qdutils;
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -070076using namespace android;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070077
Naomi Luisa44100c2013-02-08 12:42:03 -080078ANDROID_SINGLETON_STATIC_INSTANCE(AdrenoMemInfo);
79
Sushil Chauhan65e26302015-01-14 10:48:57 -080080static void getUBwcWidthAndHeight(int, int, int, int&, int&);
81static unsigned int getUBwcSize(int, int, int, const int, const int);
82
Iliyan Malchev202a77d2012-06-11 14:41:12 -070083//Common functions
Iliyan Malchev202a77d2012-06-11 14:41:12 -070084
Saurabh Shah1adcafe2014-12-19 10:05:41 -080085/* The default policy is to return cached buffers unless the client explicity
86 * sets the PRIVATE_UNCACHED flag or indicates that the buffer will be rarely
87 * read or written in software. Any combination with a _RARELY_ flag will be
88 * treated as uncached. */
89static bool useUncached(const int& usage) {
90 if((usage & GRALLOC_USAGE_PRIVATE_UNCACHED) or
91 ((usage & GRALLOC_USAGE_SW_WRITE_MASK) ==
92 GRALLOC_USAGE_SW_WRITE_RARELY) or
93 ((usage & GRALLOC_USAGE_SW_READ_MASK) ==
94 GRALLOC_USAGE_SW_READ_RARELY))
95 return true;
96
97 return false;
98}
99
Naomi Luisa44100c2013-02-08 12:42:03 -0800100//-------------- AdrenoMemInfo-----------------------//
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800101AdrenoMemInfo::AdrenoMemInfo()
102{
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800103 LINK_adreno_compute_aligned_width_and_height = NULL;
104 LINK_adreno_compute_padding = NULL;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700105 LINK_adreno_isMacroTilingSupportedByGpu = NULL;
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800106 LINK_adreno_compute_compressedfmt_aligned_width_and_height = NULL;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800107 LINK_adreno_isUBWCSupportedByGpu = NULL;
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800108
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800109 libadreno_utils = ::dlopen("libadreno_utils.so", RTLD_NOW);
110 if (libadreno_utils) {
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800111 *(void **)&LINK_adreno_compute_aligned_width_and_height =
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700112 ::dlsym(libadreno_utils, "compute_aligned_width_and_height");
113 *(void **)&LINK_adreno_compute_padding =
114 ::dlsym(libadreno_utils, "compute_surface_padding");
115 *(void **)&LINK_adreno_isMacroTilingSupportedByGpu =
116 ::dlsym(libadreno_utils, "isMacroTilingSupportedByGpu");
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800117 *(void **)&LINK_adreno_compute_compressedfmt_aligned_width_and_height =
118 ::dlsym(libadreno_utils,
119 "compute_compressedfmt_aligned_width_and_height");
Sushil Chauhan082acd62015-01-14 16:49:29 -0800120 *(void **)&LINK_adreno_isUBWCSupportedByGpu =
121 ::dlsym(libadreno_utils, "isUBWCSupportedByGpu");
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800122 }
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700123
124 // Check if the overriding property debug.gralloc.gfx_ubwc_disable
125 // that disables UBWC allocations for the graphics stack is set
126 gfx_ubwc_disable = 0;
127 char property[PROPERTY_VALUE_MAX];
128 property_get("debug.gralloc.gfx_ubwc_disable", property, "0");
129 if(!(strncmp(property, "1", PROPERTY_VALUE_MAX)) ||
130 !(strncmp(property, "true", PROPERTY_VALUE_MAX))) {
131 gfx_ubwc_disable = 1;
132 }
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800133}
134
135AdrenoMemInfo::~AdrenoMemInfo()
136{
137 if (libadreno_utils) {
138 ::dlclose(libadreno_utils);
139 }
140}
141
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700142int AdrenoMemInfo::isMacroTilingSupportedByGPU()
143{
144 if ((libadreno_utils)) {
145 if(LINK_adreno_isMacroTilingSupportedByGpu) {
146 return LINK_adreno_isMacroTilingSupportedByGpu();
147 }
148 }
149 return 0;
150}
151
152
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800153void AdrenoMemInfo::getAlignedWidthAndHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800154 int usage, int& aligned_w, int& aligned_h)
Naomi Luisa44100c2013-02-08 12:42:03 -0800155{
Sushil Chauhan65e26302015-01-14 10:48:57 -0800156
Sushil Chauhane61fac52015-04-30 17:14:37 -0700157 bool ubwc_enabled = isUBwcEnabled(format, usage);
158
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800159 // Currently surface padding is only computed for RGB* surfaces.
Jesse Hallfbe96d22013-09-20 01:39:43 -0700160 if (format <= HAL_PIXEL_FORMAT_sRGB_X_8888) {
Sushil Chauhane61fac52015-04-30 17:14:37 -0700161 int tileEnabled = ubwc_enabled || isMacroTileEnabled(format, usage);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800162 AdrenoMemInfo::getInstance().getGpuAlignedWidthHeight(width,
163 height, format, tileEnabled, aligned_w, aligned_h);
164 return;
Naomi Luisa44100c2013-02-08 12:42:03 -0800165 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800166
Sushil Chauhane61fac52015-04-30 17:14:37 -0700167 if (ubwc_enabled) {
Sushil Chauhan65e26302015-01-14 10:48:57 -0800168 getUBwcWidthAndHeight(width, height, format, aligned_w, aligned_h);
169 return;
170 }
171
172 aligned_w = width;
173 aligned_h = height;
174 switch (format)
175 {
176 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
177 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800178 aligned_w = ALIGN(width, 32);
179 break;
Shuzhen Wang2a000b22014-08-20 00:15:51 -0700180 case HAL_PIXEL_FORMAT_RAW_SENSOR:
181 aligned_w = ALIGN(width, 16);
182 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700183 case HAL_PIXEL_FORMAT_RAW10:
184 aligned_w = ALIGN(width * 10 /8, 16);
185 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800186 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
187 aligned_w = ALIGN(width, 128);
188 break;
189 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
190 case HAL_PIXEL_FORMAT_YV12:
191 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
192 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
193 case HAL_PIXEL_FORMAT_YCbCr_422_I:
194 case HAL_PIXEL_FORMAT_YCrCb_422_I:
195 aligned_w = ALIGN(width, 16);
196 break;
197 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
198 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
199 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
200 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
201 break;
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530202 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
203 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV21, width);
204 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV21, height);
205 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800206 case HAL_PIXEL_FORMAT_BLOB:
207 break;
208 case HAL_PIXEL_FORMAT_NV21_ZSL:
209 aligned_w = ALIGN(width, 64);
210 aligned_h = ALIGN(height, 64);
211 break;
212 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
213 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
214 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
215 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
216 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
217 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
218 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
219 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
220 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
221 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
222 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
223 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
224 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
225 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
226 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
227 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
228 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
229 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
230 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
231 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
232 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
233 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
234 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
235 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
236 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
237 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
238 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
239 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
240 if(LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
241 int bytesPerPixel = 0;
242 int raster_mode = 0; //Adreno unknown raster mode.
243 int padding_threshold = 512; //Threshold for padding
244 //surfaces.
245
246 LINK_adreno_compute_compressedfmt_aligned_width_and_height(
247 width, height, format, 0,raster_mode, padding_threshold,
248 &aligned_w, &aligned_h, &bytesPerPixel);
249 } else {
250 ALOGW("%s: Warning!! Symbols" \
251 " compute_compressedfmt_aligned_width_and_height" \
252 " not found", __FUNCTION__);
253 }
254 break;
255 default: break;
256 }
257}
258
259void AdrenoMemInfo::getGpuAlignedWidthHeight(int width, int height, int format,
260 int tile_enabled, int& aligned_w, int& aligned_h)
261{
262 aligned_w = ALIGN(width, 32);
263 aligned_h = ALIGN(height, 32);
264
265 // Don't add any additional padding if debug.gralloc.map_fb_memory
266 // is enabled
267 char property[PROPERTY_VALUE_MAX];
268 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
269 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
270 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
271 return;
272 }
273
274 int bpp = 4;
275 switch(format)
276 {
277 case HAL_PIXEL_FORMAT_RGB_888:
278 bpp = 3;
279 break;
280 case HAL_PIXEL_FORMAT_RGB_565:
281 case HAL_PIXEL_FORMAT_RGBA_5551:
282 case HAL_PIXEL_FORMAT_RGBA_4444:
283 bpp = 2;
284 break;
285 default: break;
286 }
287
288 if (libadreno_utils) {
289 int raster_mode = 0; // Adreno unknown raster mode.
290 int padding_threshold = 512; // Threshold for padding surfaces.
291 // the function below computes aligned width and aligned height
292 // based on linear or macro tile mode selected.
293 if(LINK_adreno_compute_aligned_width_and_height) {
294 LINK_adreno_compute_aligned_width_and_height(width,
295 height, bpp, tile_enabled,
296 raster_mode, padding_threshold,
297 &aligned_w, &aligned_h);
298
299 } else if(LINK_adreno_compute_padding) {
300 int surface_tile_height = 1; // Linear surface
301 aligned_w = LINK_adreno_compute_padding(width, bpp,
302 surface_tile_height, raster_mode,
303 padding_threshold);
304 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
305 __FUNCTION__);
306 } else {
307 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
308 "compute_aligned_width_and_height not found", __FUNCTION__);
309 }
310 }
311}
312
313int AdrenoMemInfo::isUBWCSupportedByGPU(int format)
314{
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700315 if (!gfx_ubwc_disable && libadreno_utils) {
Sushil Chauhan082acd62015-01-14 16:49:29 -0800316 if (LINK_adreno_isUBWCSupportedByGpu) {
317 ADRENOPIXELFORMAT gpu_format = getGpuPixelFormat(format);
318 return LINK_adreno_isUBWCSupportedByGpu(gpu_format);
319 }
320 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800321 return 0;
Naomi Luisa44100c2013-02-08 12:42:03 -0800322}
323
Sushil Chauhan082acd62015-01-14 16:49:29 -0800324ADRENOPIXELFORMAT AdrenoMemInfo::getGpuPixelFormat(int hal_format)
325{
326 switch (hal_format) {
327 case HAL_PIXEL_FORMAT_RGBA_8888:
328 return ADRENO_PIXELFORMAT_R8G8B8A8;
Sushil Chauhan6686c802015-04-15 11:30:39 -0700329 case HAL_PIXEL_FORMAT_RGBX_8888:
330 return ADRENO_PIXELFORMAT_R8G8B8X8;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800331 case HAL_PIXEL_FORMAT_RGB_565:
332 return ADRENO_PIXELFORMAT_B5G6R5;
333 case HAL_PIXEL_FORMAT_sRGB_A_8888:
334 return ADRENO_PIXELFORMAT_R8G8B8A8_SRGB;
335 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800336 return ADRENO_PIXELFORMAT_NV12;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800337 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
338 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800339 return ADRENO_PIXELFORMAT_NV12_EXT;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800340 default:
341 ALOGE("%s: No map for format: 0x%x", __FUNCTION__, hal_format);
342 break;
343 }
344 return ADRENO_PIXELFORMAT_UNKNOWN;
345}
346
Naomi Luisa44100c2013-02-08 12:42:03 -0800347//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700348IAllocController* IAllocController::sController = NULL;
349IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700350{
351 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700352 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700353 }
354 return sController;
355}
356
357
358//-------------- IonController-----------------------//
359IonController::IonController()
360{
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530361 allocateIonMem();
362}
363
364void IonController::allocateIonMem()
365{
366 mIonAlloc = new IonAlloc();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700367}
368
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700369int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700370{
371 int ionFlags = 0;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500372 int ionHeapId = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700373 int ret;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700374
375 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700376 data.allocType = 0;
377
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530378 if(usage & GRALLOC_USAGE_PROTECTED) {
Prabhanjan Kandulae8f4bec2013-10-24 16:32:51 +0530379 if (usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800380 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
Shalaj Jain3c490412015-04-22 16:52:03 -0700381 ionHeapId = ION_HEAP(SD_HEAP_ID);
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800382 /*
383 * There is currently no flag in ION for Secure Display
Shalaj Jain3c490412015-04-22 16:52:03 -0700384 * VM. Please add it to the define once available.
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800385 */
Shalaj Jain3c490412015-04-22 16:52:03 -0700386 ionFlags |= ION_SD_FLAGS;
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800387 } else {
Shalaj Jain3c490412015-04-22 16:52:03 -0700388 ionHeapId = ION_HEAP(CP_HEAP_ID);
389 ionFlags |= ION_CP_FLAGS;
Shalaj Jain13cdf812014-12-02 16:20:54 -0800390 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530391 } else {
392 // for targets/OEMs which do not need HW level protection
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500393 // do not set ion secure flag & MM heap. Fallback to system heap.
394 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Justin Philipd6166602014-08-12 13:42:21 +0530395 data.allocType |= private_handle_t::PRIV_FLAGS_PROTECTED_BUFFER;
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500396 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530397 } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
398 //MM Heap is exclusively a secure heap.
399 //If it is used for non secure cases, fallback to IOMMU heap
400 ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
401 cannot be used as an insecure heap!\
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500402 trying to use system heap instead !!");
403 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500404 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700405
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700406 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500407 ionHeapId |= ION_HEAP(ION_CAMERA_HEAP_ID);
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700408
Arun Kumar K.R0daaa992013-03-12 15:08:29 -0700409 if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500410 ionHeapId |= ION_HEAP(ION_ADSP_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700411
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530412 if(ionFlags & ION_SECURE)
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500413 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700414
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500415 // if no ion heap flags are set, default to system heap
416 if(!ionHeapId)
417 ionHeapId = ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700418
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500419 //At this point we should have the right heap set, there is no fallback
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700420 data.flags = ionFlags;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500421 data.heapId = ionHeapId;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700422 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700423
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700424 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700425 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500426 } else {
427 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x",
428 __FUNCTION__, ionHeapId, ionFlags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700429 }
430
431 return ret;
432}
433
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700434IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700435{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700436 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700437 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
438 memalloc = mIonAlloc;
439 } else {
440 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
441 }
442
443 return memalloc;
444}
445
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700446bool isMacroTileEnabled(int format, int usage)
447{
448 bool tileEnabled = false;
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -0700449 int isMacroTileSupportedByMDP = 0;
450
451 qdutils::querySDEInfo(HAS_MACRO_TILE, &isMacroTileSupportedByMDP);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700452
453 // Check whether GPU & MDSS supports MacroTiling feature
454 if(AdrenoMemInfo::getInstance().isMacroTilingSupportedByGPU() &&
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -0700455 isMacroTileSupportedByMDP)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700456 {
457 // check the format
458 switch(format)
459 {
460 case HAL_PIXEL_FORMAT_RGBA_8888:
461 case HAL_PIXEL_FORMAT_RGBX_8888:
462 case HAL_PIXEL_FORMAT_BGRA_8888:
Manoj Kumar AVM5a5529b2014-02-24 18:16:37 -0800463 case HAL_PIXEL_FORMAT_RGB_565:
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700464 {
465 tileEnabled = true;
466 // check the usage flags
467 if (usage & (GRALLOC_USAGE_SW_READ_MASK |
468 GRALLOC_USAGE_SW_WRITE_MASK)) {
469 // Application intends to use CPU for rendering
470 tileEnabled = false;
471 }
472 break;
473 }
474 default:
475 break;
476 }
477 }
478 return tileEnabled;
479}
480
481// helper function
Sushil Chauhan65e26302015-01-14 10:48:57 -0800482unsigned int getSize(int format, int width, int height, int usage,
483 const int alignedw, const int alignedh) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700484
Sushil Chauhan65e26302015-01-14 10:48:57 -0800485 if (isUBwcEnabled(format, usage)) {
486 return getUBwcSize(width, height, format, alignedw, alignedh);
487 }
488
489 unsigned int size = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700490 switch (format) {
491 case HAL_PIXEL_FORMAT_RGBA_8888:
492 case HAL_PIXEL_FORMAT_RGBX_8888:
493 case HAL_PIXEL_FORMAT_BGRA_8888:
Naseer Ahmed82fc4b72013-09-20 01:31:37 -0700494 case HAL_PIXEL_FORMAT_sRGB_A_8888:
Jesse Hallfbe96d22013-09-20 01:39:43 -0700495 case HAL_PIXEL_FORMAT_sRGB_X_8888:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700496 size = alignedw * alignedh * 4;
497 break;
498 case HAL_PIXEL_FORMAT_RGB_888:
499 size = alignedw * alignedh * 3;
500 break;
501 case HAL_PIXEL_FORMAT_RGB_565:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700502 case HAL_PIXEL_FORMAT_RGBA_5551:
503 case HAL_PIXEL_FORMAT_RGBA_4444:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400504 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700505 size = alignedw * alignedh * 2;
506 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700507 case HAL_PIXEL_FORMAT_RAW10:
508 size = ALIGN(alignedw * alignedh, 4096);
509 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700510
511 // adreno formats
512 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
513 size = ALIGN(alignedw*alignedh, 4096);
514 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
515 break;
516 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
517 // The chroma plane is subsampled,
518 // but the pitch in bytes is unchanged
519 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700520 size = ALIGN( alignedw * alignedh, 8192);
521 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
522 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700523 case HAL_PIXEL_FORMAT_YV12:
524 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
525 ALOGE("w or h is odd for the YV12 format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800526 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700527 }
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400528 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700529 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700530 size = ALIGN(size, (unsigned int)4096);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700531 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800532 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
533 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naseer Ahmed2c215292013-09-18 23:47:42 -0400534 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800535 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700536 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
537 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700538 case HAL_PIXEL_FORMAT_YCbCr_422_I:
539 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700540 if(width & 1) {
541 ALOGE("width is odd for the YUV422_SP format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800542 return 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700543 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700544 size = ALIGN(alignedw * alignedh * 2, 4096);
545 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700546 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400547 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhane8a01792012-11-01 16:25:45 -0700548 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700549 break;
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530550 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
551 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
552 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400553 case HAL_PIXEL_FORMAT_BLOB:
554 if(height != 1) {
555 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
556 must have height==1 ", __FUNCTION__);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800557 return 0;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400558 }
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400559 size = width;
560 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700561 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700562 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
563 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500564 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
565 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
566 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
567 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
568 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
569 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
570 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
571 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
572 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
573 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
574 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
575 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
576 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
577 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
578 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
579 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
580 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
581 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
582 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
583 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
584 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
585 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
586 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
587 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
588 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
589 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
590 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Jeykumar Sankaran8f4585f2014-02-05 15:23:40 -0800591 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500592 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
593 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700594 default:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800595 ALOGE("Unrecognized pixel format: 0x%x", __FUNCTION__, format);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800596 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700597 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700598 return size;
599}
600
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700601unsigned int getBufferSizeAndDimensions(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700602 int& alignedw, int &alignedh)
603{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700604 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700605
606 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
607 height,
608 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800609 0,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700610 alignedw,
611 alignedh);
612
Sushil Chauhan65e26302015-01-14 10:48:57 -0800613 size = getSize(format, width, height, 0 /* usage */, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700614
615 return size;
616}
617
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700618
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700619unsigned int getBufferSizeAndDimensions(int width, int height, int format,
620 int usage, int& alignedw, int &alignedh)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700621{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700622 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700623
624 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
625 height,
626 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800627 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700628 alignedw,
629 alignedh);
630
Sushil Chauhan65e26302015-01-14 10:48:57 -0800631 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700632
633 return size;
634}
635
636
637void getBufferAttributes(int width, int height, int format, int usage,
Sushil Chauhane61fac52015-04-30 17:14:37 -0700638 int& alignedw, int &alignedh, int& tiled, unsigned int& size)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700639{
Sushil Chauhane61fac52015-04-30 17:14:37 -0700640 tiled = isUBwcEnabled(format, usage) || isMacroTileEnabled(format, usage);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700641
642 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
643 height,
644 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800645 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700646 alignedw,
647 alignedh);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800648 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700649}
650
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400651int getYUVPlaneInfo(private_handle_t* hnd, struct android_ycbcr* ycbcr)
652{
653 int err = 0;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700654 int width = hnd->width;
655 int height = hnd->height;
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700656 int format = hnd->format;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700657 unsigned int ystride, cstride;
Sushil Chauhan4686c972015-02-20 15:44:52 -0800658 unsigned int alignment = 4096;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700659
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400660 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700661 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
662
663 // Check if UBWC buffer has been rendered in linear format.
664 if (metadata && (metadata->operation & LINEAR_FORMAT)) {
665 format = metadata->linearFormat;
666 }
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400667
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700668 // Check metadata if the geometry has been updated.
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700669 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
670 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(metadata->bufferDim.sliceWidth,
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700671 metadata->bufferDim.sliceHeight, format, 0, width, height);
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700672 }
673
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400674 // Get the chroma offsets from the handle width/height. We take advantage
675 // of the fact the width _is_ the stride
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700676 switch (format) {
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400677 //Semiplanar
678 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
679 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
680 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
681 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: //Same as YCbCr_420_SP_VENUS
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700682 ystride = cstride = width;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400683 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700684 ycbcr->cb = (void*)(hnd->base + ystride * height);
685 ycbcr->cr = (void*)(hnd->base + ystride * height + 1);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400686 ycbcr->ystride = ystride;
687 ycbcr->cstride = cstride;
688 ycbcr->chroma_step = 2;
689 break;
690
Sushil Chauhan4686c972015-02-20 15:44:52 -0800691 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
692 // NV12_UBWC buffer has these 4 planes in the following sequence:
693 // Y_Meta_Plane, Y_Plane, UV_Meta_Plane, UV_Plane
694 unsigned int y_meta_stride, y_meta_height, y_meta_size;
695 unsigned int y_stride, y_height, y_size;
696 unsigned int c_meta_stride, c_meta_height, c_meta_size;
697
698 y_meta_stride = VENUS_Y_META_STRIDE(COLOR_FMT_NV12_UBWC, width);
699 y_meta_height = VENUS_Y_META_SCANLINES(COLOR_FMT_NV12_UBWC, height);
700 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
701
702 y_stride = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
703 y_height = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
704 y_size = ALIGN((y_stride * y_height), alignment);
705
706 c_meta_stride = VENUS_UV_META_STRIDE(COLOR_FMT_NV12_UBWC, width);
707 c_meta_height = VENUS_UV_META_SCANLINES(COLOR_FMT_NV12_UBWC, height);
708 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
709
710 ycbcr->y = (void*)(hnd->base + y_meta_size);
711 ycbcr->cb = (void*)(hnd->base + y_meta_size + y_size + c_meta_size);
712 ycbcr->cr = (void*)(hnd->base + y_meta_size + y_size +
713 c_meta_size + 1);
714 ycbcr->ystride = y_stride;
715 ycbcr->cstride = VENUS_UV_STRIDE(COLOR_FMT_NV12_UBWC, width);
716 ycbcr->chroma_step = 2;
717 break;
718
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400719 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
720 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
721 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530722 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400723 case HAL_PIXEL_FORMAT_NV21_ZSL:
724 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700725 case HAL_PIXEL_FORMAT_RAW10:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700726 ystride = cstride = width;
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 + 1);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400730 ycbcr->ystride = ystride;
731 ycbcr->cstride = cstride;
732 ycbcr->chroma_step = 2;
733 break;
734
735 //Planar
736 case HAL_PIXEL_FORMAT_YV12:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700737 ystride = width;
738 cstride = ALIGN(width/2, 16);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400739 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700740 ycbcr->cr = (void*)(hnd->base + ystride * height);
741 ycbcr->cb = (void*)(hnd->base + ystride * height +
742 cstride * height/2);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400743 ycbcr->ystride = ystride;
744 ycbcr->cstride = cstride;
745 ycbcr->chroma_step = 1;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400746 break;
747 //Unsupported formats
748 case HAL_PIXEL_FORMAT_YCbCr_422_I:
749 case HAL_PIXEL_FORMAT_YCrCb_422_I:
750 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
751 default:
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700752 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, format);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400753 err = -EINVAL;
754 }
755 return err;
756
757}
758
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700759
760
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700761// Allocate buffer from width, height and format into a
762// private_handle_t. It is the responsibility of the caller
763// to free the buffer using the free_buffer function
764int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
765{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700766 alloc_data data;
767 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700768 gralloc::IAllocController* sAlloc =
769 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700770 data.base = 0;
771 data.fd = -1;
772 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700773 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
774 alignedh);
775
Naseer Ahmed29a26812012-06-14 00:56:20 -0700776 data.align = getpagesize();
777 data.uncached = useUncached(usage);
778 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700779
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700780 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700781 if (0 != err) {
782 ALOGE("%s: allocate failed", __FUNCTION__);
783 return -ENOMEM;
784 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700785
Naseer Ahmed29a26812012-06-14 00:56:20 -0700786 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700787 data.allocType, 0, format,
788 alignedw, alignedh);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700789 hnd->base = (uint64_t) data.base;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700790 hnd->offset = data.offset;
791 hnd->gpuaddr = 0;
792 *pHnd = hnd;
793 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700794}
795
796void free_buffer(private_handle_t *hnd)
797{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700798 gralloc::IAllocController* sAlloc =
799 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700800 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700801 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700802 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
803 }
804 if(hnd)
805 delete hnd;
806
807}
Sushil Chauhan65e26302015-01-14 10:48:57 -0800808
809// UBWC helper functions
810static bool isUBwcFormat(int format)
811{
812 // Explicitly defined UBWC formats
813 switch(format)
814 {
815 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
816 return true;
817 default:
818 return false;
819 }
820}
821
822static bool isUBwcSupported(int format)
823{
824 // Existing HAL formats with UBWC support
825 switch(format)
826 {
827 case HAL_PIXEL_FORMAT_RGB_565:
828 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -0700829 case HAL_PIXEL_FORMAT_RGBX_8888:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800830 case HAL_PIXEL_FORMAT_sRGB_A_8888:
831 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
832 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
833 return true;
834 default:
835 return false;
836 }
837}
838
839bool isUBwcEnabled(int format, int usage)
840{
Sushil Chauhan81594f62015-01-26 16:00:51 -0800841 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
842 if (isUBwcFormat(format))
843 return true;
844
845 // Allow UBWC, if client sets UBWC gralloc usage flag & GPU supports format.
846 if ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) && isUBwcSupported(format) &&
847 AdrenoMemInfo::getInstance().isUBWCSupportedByGPU(format)) {
848 // Allow UBWC, only if CPU usage flags are not set
849 if (!(usage & (GRALLOC_USAGE_SW_READ_MASK |
Sushil Chauhan65e26302015-01-14 10:48:57 -0800850 GRALLOC_USAGE_SW_WRITE_MASK))) {
851 return true;
852 }
853 }
854 return false;
855}
856
857static void getUBwcWidthAndHeight(int width, int height, int format,
858 int& aligned_w, int& aligned_h)
859{
860 switch (format)
861 {
862 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
863 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
864 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
865 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
866 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
867 break;
868 default:
869 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
870 aligned_w = 0;
871 aligned_h = 0;
872 break;
873 }
874}
875
876static void getUBwcBlockSize(int bpp, int& block_width, int& block_height)
877{
878 block_width = 0;
879 block_height = 0;
880
881 switch(bpp)
882 {
883 case 2:
884 case 4:
885 block_width = 16;
886 block_height = 4;
887 break;
888 case 8:
889 block_width = 8;
890 block_height = 4;
891 break;
892 case 16:
893 block_width = 4;
894 block_height = 4;
895 break;
896 default:
897 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
898 break;
899 }
900}
901
902static unsigned int getUBwcMetaBufferSize(int width, int height, int bpp)
903{
904 unsigned int size = 0;
905 int meta_width, meta_height;
906 int block_width, block_height;
907
908 getUBwcBlockSize(bpp, block_width, block_height);
909
910 if (!block_width || !block_height) {
911 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
912 return size;
913 }
914
915 // Align meta buffer height to 16 blocks
916 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
917
918 // Align meta buffer width to 64 blocks
919 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
920
921 // Align meta buffer size to 4K
Sushil Chauhanc85b65b2015-04-30 11:05:36 -0700922 size = ALIGN((meta_width * meta_height), 4096);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800923 return size;
924}
925
926static unsigned int getUBwcSize(int width, int height, int format,
927 const int alignedw, const int alignedh) {
928
929 unsigned int size = 0;
930 switch (format) {
931 case HAL_PIXEL_FORMAT_RGB_565:
932 size = alignedw * alignedh * 2;
933 size += getUBwcMetaBufferSize(width, height, 2);
934 break;
935 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -0700936 case HAL_PIXEL_FORMAT_RGBX_8888:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800937 case HAL_PIXEL_FORMAT_sRGB_A_8888:
938 size = alignedw * alignedh * 4;
939 size += getUBwcMetaBufferSize(width, height, 4);
940 break;
941 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
942 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
943 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
944 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
945 break;
946 default:
947 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
948 break;
949 }
950 return size;
951}
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700952
Sushil Chauhanc85b65b2015-04-30 11:05:36 -0700953int getRgbDataAddress(private_handle_t* hnd, void** rgb_data)
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700954{
955 int err = 0;
956
957 // This api is for RGB* formats
958 if (hnd->format > HAL_PIXEL_FORMAT_sRGB_X_8888) {
959 return -EINVAL;
960 }
961
962 // linear buffer
963 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
Sushil Chauhanc85b65b2015-04-30 11:05:36 -0700964 *rgb_data = (void*)hnd->base;
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700965 return err;
966 }
967
968 unsigned int meta_size = 0;
969 switch (hnd->format) {
970 case HAL_PIXEL_FORMAT_RGB_565:
971 meta_size = getUBwcMetaBufferSize(hnd->width, hnd->height, 2);
972 break;
973 case HAL_PIXEL_FORMAT_RGBA_8888:
974 case HAL_PIXEL_FORMAT_RGBX_8888:
975 case HAL_PIXEL_FORMAT_sRGB_A_8888:
976 meta_size = getUBwcMetaBufferSize(hnd->width, hnd->height, 4);
977 break;
978 default:
979 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
980 err = -EINVAL;
981 break;
982 }
983
Sushil Chauhanc85b65b2015-04-30 11:05:36 -0700984 *rgb_data = (void*)(hnd->base + meta_size);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700985 return err;
986}