blob: f43f64505c80c3ee4a96248aafc75fcf4c4fa764 [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
Mansoor Aftabe9912a62014-07-15 01:40:26 -07002 * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
Iliyan Malchev202a77d2012-06-11 14:41:12 -07003
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
Duy Truong73d36df2013-02-09 20:33:23 -080013 * * Neither the name of The Linux Foundation nor the names of its
Iliyan Malchev202a77d2012-06-11 14:41:12 -070014 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <cutils/log.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070031#include <fcntl.h>
Naomi Luis01f5c8e2013-02-11 12:46:24 -080032#include <dlfcn.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070033#include "gralloc_priv.h"
34#include "alloc_controller.h"
35#include "memalloc.h"
36#include "ionalloc.h"
Iliyan Malchev202a77d2012-06-11 14:41:12 -070037#include "gr.h"
Naseer Ahmeda87da602012-07-01 23:54:19 -070038#include "comptype.h"
Manoj Kumar AVM8a220812013-10-10 11:46:06 -070039#include "mdp_version.h"
Kaushik Kanetkar071aca62015-01-22 23:16:26 -070040#include <qdMetaData.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070041
Sushil Chauhanc6bd6d92012-12-12 12:33:01 -080042#ifdef VENUS_COLOR_FORMAT
43#include <media/msm_media_info.h>
44#else
45#define VENUS_Y_STRIDE(args...) 0
46#define VENUS_Y_SCANLINES(args...) 0
47#define VENUS_BUFFER_SIZE(args...) 0
48#endif
49
Naseer Ahmed63326f42013-12-18 02:45:48 -050050#define ASTC_BLOCK_SIZE 16
Naseer Ahmed63326f42013-12-18 02:45:48 -050051
Shalaj Jain3c490412015-04-22 16:52:03 -070052#ifndef ION_FLAG_CP_PIXEL
Shalaj Jain1f9725a2015-03-04 17:53:49 -080053#define ION_FLAG_CP_PIXEL 0
Shalaj Jain1f9725a2015-03-04 17:53:49 -080054#endif
55
56#ifndef ION_FLAG_ALLOW_NON_CONTIG
57#define ION_FLAG_ALLOW_NON_CONTIG 0
58#endif
59
Shalaj Jain3c490412015-04-22 16:52:03 -070060#ifdef MASTER_SIDE_CP
61#define CP_HEAP_ID ION_SECURE_HEAP_ID
62/* Please Add the new SD ION Heap here */
63#define SD_HEAP_ID 0
64#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_CP_PIXEL)
65/* Please Add the new SD ION Flag here */
66#define ION_SD_FLAGS ION_SECURE
67#else // SLAVE_SIDE_CP
68#define CP_HEAP_ID ION_CP_MM_HEAP_ID
69#define SD_HEAP_ID CP_HEAP_ID
70#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_ALLOW_NON_CONTIG)
71#define ION_SD_FLAGS ION_SECURE
72#endif
73
Iliyan Malchev202a77d2012-06-11 14:41:12 -070074using namespace gralloc;
Naseer Ahmeda87da602012-07-01 23:54:19 -070075using namespace qdutils;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070076
Naomi Luisa44100c2013-02-08 12:42:03 -080077ANDROID_SINGLETON_STATIC_INSTANCE(AdrenoMemInfo);
78
Sushil Chauhan65e26302015-01-14 10:48:57 -080079static void getUBwcWidthAndHeight(int, int, int, int&, int&);
80static unsigned int getUBwcSize(int, int, int, const int, const int);
81
Iliyan Malchev202a77d2012-06-11 14:41:12 -070082//Common functions
Iliyan Malchev202a77d2012-06-11 14:41:12 -070083
Saurabh Shah1adcafe2014-12-19 10:05:41 -080084/* The default policy is to return cached buffers unless the client explicity
85 * sets the PRIVATE_UNCACHED flag or indicates that the buffer will be rarely
86 * read or written in software. Any combination with a _RARELY_ flag will be
87 * treated as uncached. */
88static bool useUncached(const int& usage) {
89 if((usage & GRALLOC_USAGE_PRIVATE_UNCACHED) or
90 ((usage & GRALLOC_USAGE_SW_WRITE_MASK) ==
91 GRALLOC_USAGE_SW_WRITE_RARELY) or
92 ((usage & GRALLOC_USAGE_SW_READ_MASK) ==
93 GRALLOC_USAGE_SW_READ_RARELY))
94 return true;
95
96 return false;
97}
98
Naomi Luisa44100c2013-02-08 12:42:03 -080099//-------------- AdrenoMemInfo-----------------------//
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800100AdrenoMemInfo::AdrenoMemInfo()
101{
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800102 LINK_adreno_compute_aligned_width_and_height = NULL;
103 LINK_adreno_compute_padding = NULL;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700104 LINK_adreno_isMacroTilingSupportedByGpu = NULL;
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800105 LINK_adreno_compute_compressedfmt_aligned_width_and_height = NULL;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800106 LINK_adreno_isUBWCSupportedByGpu = NULL;
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800107
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800108 libadreno_utils = ::dlopen("libadreno_utils.so", RTLD_NOW);
109 if (libadreno_utils) {
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800110 *(void **)&LINK_adreno_compute_aligned_width_and_height =
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700111 ::dlsym(libadreno_utils, "compute_aligned_width_and_height");
112 *(void **)&LINK_adreno_compute_padding =
113 ::dlsym(libadreno_utils, "compute_surface_padding");
114 *(void **)&LINK_adreno_isMacroTilingSupportedByGpu =
115 ::dlsym(libadreno_utils, "isMacroTilingSupportedByGpu");
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800116 *(void **)&LINK_adreno_compute_compressedfmt_aligned_width_and_height =
117 ::dlsym(libadreno_utils,
118 "compute_compressedfmt_aligned_width_and_height");
Sushil Chauhan082acd62015-01-14 16:49:29 -0800119 *(void **)&LINK_adreno_isUBWCSupportedByGpu =
120 ::dlsym(libadreno_utils, "isUBWCSupportedByGpu");
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800121 }
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700122
123 // Check if the overriding property debug.gralloc.gfx_ubwc_disable
124 // that disables UBWC allocations for the graphics stack is set
125 gfx_ubwc_disable = 0;
126 char property[PROPERTY_VALUE_MAX];
127 property_get("debug.gralloc.gfx_ubwc_disable", property, "0");
128 if(!(strncmp(property, "1", PROPERTY_VALUE_MAX)) ||
129 !(strncmp(property, "true", PROPERTY_VALUE_MAX))) {
130 gfx_ubwc_disable = 1;
131 }
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800132}
133
134AdrenoMemInfo::~AdrenoMemInfo()
135{
136 if (libadreno_utils) {
137 ::dlclose(libadreno_utils);
138 }
139}
140
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700141int AdrenoMemInfo::isMacroTilingSupportedByGPU()
142{
143 if ((libadreno_utils)) {
144 if(LINK_adreno_isMacroTilingSupportedByGpu) {
145 return LINK_adreno_isMacroTilingSupportedByGpu();
146 }
147 }
148 return 0;
149}
150
151
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800152void AdrenoMemInfo::getAlignedWidthAndHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800153 int usage, int& aligned_w, int& aligned_h)
Naomi Luisa44100c2013-02-08 12:42:03 -0800154{
Sushil Chauhan65e26302015-01-14 10:48:57 -0800155
Sushil Chauhane61fac52015-04-30 17:14:37 -0700156 bool ubwc_enabled = isUBwcEnabled(format, usage);
157
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800158 // Currently surface padding is only computed for RGB* surfaces.
Jesse Hallfbe96d22013-09-20 01:39:43 -0700159 if (format <= HAL_PIXEL_FORMAT_sRGB_X_8888) {
Sushil Chauhane61fac52015-04-30 17:14:37 -0700160 int tileEnabled = ubwc_enabled || isMacroTileEnabled(format, usage);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800161 AdrenoMemInfo::getInstance().getGpuAlignedWidthHeight(width,
162 height, format, tileEnabled, aligned_w, aligned_h);
163 return;
Naomi Luisa44100c2013-02-08 12:42:03 -0800164 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800165
Sushil Chauhane61fac52015-04-30 17:14:37 -0700166 if (ubwc_enabled) {
Sushil Chauhan65e26302015-01-14 10:48:57 -0800167 getUBwcWidthAndHeight(width, height, format, aligned_w, aligned_h);
168 return;
169 }
170
171 aligned_w = width;
172 aligned_h = height;
173 switch (format)
174 {
175 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
176 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
177 case HAL_PIXEL_FORMAT_RAW_SENSOR:
178 aligned_w = ALIGN(width, 32);
179 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700180 case HAL_PIXEL_FORMAT_RAW10:
181 aligned_w = ALIGN(width * 10 /8, 16);
182 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800183 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
184 aligned_w = ALIGN(width, 128);
185 break;
186 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
187 case HAL_PIXEL_FORMAT_YV12:
188 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
189 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
190 case HAL_PIXEL_FORMAT_YCbCr_422_I:
191 case HAL_PIXEL_FORMAT_YCrCb_422_I:
192 aligned_w = ALIGN(width, 16);
193 break;
194 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
195 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
196 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
197 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
198 break;
199 case HAL_PIXEL_FORMAT_BLOB:
200 break;
201 case HAL_PIXEL_FORMAT_NV21_ZSL:
202 aligned_w = ALIGN(width, 64);
203 aligned_h = ALIGN(height, 64);
204 break;
205 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
206 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
207 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
208 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
209 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
210 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
211 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
212 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
213 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
214 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
215 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
216 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
217 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
218 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
219 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
220 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
221 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
222 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
223 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
224 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
225 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
226 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
227 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
228 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
229 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
230 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
231 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
232 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
233 if(LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
234 int bytesPerPixel = 0;
235 int raster_mode = 0; //Adreno unknown raster mode.
236 int padding_threshold = 512; //Threshold for padding
237 //surfaces.
238
239 LINK_adreno_compute_compressedfmt_aligned_width_and_height(
240 width, height, format, 0,raster_mode, padding_threshold,
241 &aligned_w, &aligned_h, &bytesPerPixel);
242 } else {
243 ALOGW("%s: Warning!! Symbols" \
244 " compute_compressedfmt_aligned_width_and_height" \
245 " not found", __FUNCTION__);
246 }
247 break;
248 default: break;
249 }
250}
251
252void AdrenoMemInfo::getGpuAlignedWidthHeight(int width, int height, int format,
253 int tile_enabled, int& aligned_w, int& aligned_h)
254{
255 aligned_w = ALIGN(width, 32);
256 aligned_h = ALIGN(height, 32);
257
258 // Don't add any additional padding if debug.gralloc.map_fb_memory
259 // is enabled
260 char property[PROPERTY_VALUE_MAX];
261 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
262 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
263 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
264 return;
265 }
266
267 int bpp = 4;
268 switch(format)
269 {
270 case HAL_PIXEL_FORMAT_RGB_888:
271 bpp = 3;
272 break;
273 case HAL_PIXEL_FORMAT_RGB_565:
274 case HAL_PIXEL_FORMAT_RGBA_5551:
275 case HAL_PIXEL_FORMAT_RGBA_4444:
276 bpp = 2;
277 break;
278 default: break;
279 }
280
281 if (libadreno_utils) {
282 int raster_mode = 0; // Adreno unknown raster mode.
283 int padding_threshold = 512; // Threshold for padding surfaces.
284 // the function below computes aligned width and aligned height
285 // based on linear or macro tile mode selected.
286 if(LINK_adreno_compute_aligned_width_and_height) {
287 LINK_adreno_compute_aligned_width_and_height(width,
288 height, bpp, tile_enabled,
289 raster_mode, padding_threshold,
290 &aligned_w, &aligned_h);
291
292 } else if(LINK_adreno_compute_padding) {
293 int surface_tile_height = 1; // Linear surface
294 aligned_w = LINK_adreno_compute_padding(width, bpp,
295 surface_tile_height, raster_mode,
296 padding_threshold);
297 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
298 __FUNCTION__);
299 } else {
300 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
301 "compute_aligned_width_and_height not found", __FUNCTION__);
302 }
303 }
304}
305
306int AdrenoMemInfo::isUBWCSupportedByGPU(int format)
307{
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700308 if (!gfx_ubwc_disable && libadreno_utils) {
Sushil Chauhan082acd62015-01-14 16:49:29 -0800309 if (LINK_adreno_isUBWCSupportedByGpu) {
310 ADRENOPIXELFORMAT gpu_format = getGpuPixelFormat(format);
311 return LINK_adreno_isUBWCSupportedByGpu(gpu_format);
312 }
313 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800314 return 0;
Naomi Luisa44100c2013-02-08 12:42:03 -0800315}
316
Sushil Chauhan082acd62015-01-14 16:49:29 -0800317ADRENOPIXELFORMAT AdrenoMemInfo::getGpuPixelFormat(int hal_format)
318{
319 switch (hal_format) {
320 case HAL_PIXEL_FORMAT_RGBA_8888:
321 return ADRENO_PIXELFORMAT_R8G8B8A8;
Sushil Chauhan6686c802015-04-15 11:30:39 -0700322 case HAL_PIXEL_FORMAT_RGBX_8888:
323 return ADRENO_PIXELFORMAT_R8G8B8X8;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800324 case HAL_PIXEL_FORMAT_RGB_565:
325 return ADRENO_PIXELFORMAT_B5G6R5;
326 case HAL_PIXEL_FORMAT_sRGB_A_8888:
327 return ADRENO_PIXELFORMAT_R8G8B8A8_SRGB;
328 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800329 return ADRENO_PIXELFORMAT_NV12;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800330 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
331 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800332 return ADRENO_PIXELFORMAT_NV12_EXT;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800333 default:
334 ALOGE("%s: No map for format: 0x%x", __FUNCTION__, hal_format);
335 break;
336 }
337 return ADRENO_PIXELFORMAT_UNKNOWN;
338}
339
Naomi Luisa44100c2013-02-08 12:42:03 -0800340//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700341IAllocController* IAllocController::sController = NULL;
342IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700343{
344 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700345 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700346 }
347 return sController;
348}
349
350
351//-------------- IonController-----------------------//
352IonController::IonController()
353{
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530354 allocateIonMem();
355}
356
357void IonController::allocateIonMem()
358{
359 mIonAlloc = new IonAlloc();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700360}
361
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700362int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700363{
364 int ionFlags = 0;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500365 int ionHeapId = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700366 int ret;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700367
368 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700369 data.allocType = 0;
370
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530371 if(usage & GRALLOC_USAGE_PROTECTED) {
Prabhanjan Kandulae8f4bec2013-10-24 16:32:51 +0530372 if (usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800373 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
Shalaj Jain3c490412015-04-22 16:52:03 -0700374 ionHeapId = ION_HEAP(SD_HEAP_ID);
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800375 /*
376 * There is currently no flag in ION for Secure Display
Shalaj Jain3c490412015-04-22 16:52:03 -0700377 * VM. Please add it to the define once available.
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800378 */
Shalaj Jain3c490412015-04-22 16:52:03 -0700379 ionFlags |= ION_SD_FLAGS;
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800380 } else {
Shalaj Jain3c490412015-04-22 16:52:03 -0700381 ionHeapId = ION_HEAP(CP_HEAP_ID);
382 ionFlags |= ION_CP_FLAGS;
Shalaj Jain13cdf812014-12-02 16:20:54 -0800383 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530384 } else {
385 // for targets/OEMs which do not need HW level protection
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500386 // do not set ion secure flag & MM heap. Fallback to system heap.
387 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Justin Philipd6166602014-08-12 13:42:21 +0530388 data.allocType |= private_handle_t::PRIV_FLAGS_PROTECTED_BUFFER;
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500389 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530390 } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
391 //MM Heap is exclusively a secure heap.
392 //If it is used for non secure cases, fallback to IOMMU heap
393 ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
394 cannot be used as an insecure heap!\
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500395 trying to use system heap instead !!");
396 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500397 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700398
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700399 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500400 ionHeapId |= ION_HEAP(ION_CAMERA_HEAP_ID);
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700401
Arun Kumar K.R0daaa992013-03-12 15:08:29 -0700402 if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500403 ionHeapId |= ION_HEAP(ION_ADSP_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700404
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530405 if(ionFlags & ION_SECURE)
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500406 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700407
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500408 // if no ion heap flags are set, default to system heap
409 if(!ionHeapId)
410 ionHeapId = ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700411
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500412 //At this point we should have the right heap set, there is no fallback
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700413 data.flags = ionFlags;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500414 data.heapId = ionHeapId;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700415 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700416
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700417 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700418 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500419 } else {
420 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x",
421 __FUNCTION__, ionHeapId, ionFlags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700422 }
423
424 return ret;
425}
426
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700427IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700428{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700429 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700430 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
431 memalloc = mIonAlloc;
432 } else {
433 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
434 }
435
436 return memalloc;
437}
438
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700439bool isMacroTileEnabled(int format, int usage)
440{
441 bool tileEnabled = false;
442
443 // Check whether GPU & MDSS supports MacroTiling feature
444 if(AdrenoMemInfo::getInstance().isMacroTilingSupportedByGPU() &&
445 qdutils::MDPVersion::getInstance().supportsMacroTile())
446 {
447 // check the format
448 switch(format)
449 {
450 case HAL_PIXEL_FORMAT_RGBA_8888:
451 case HAL_PIXEL_FORMAT_RGBX_8888:
452 case HAL_PIXEL_FORMAT_BGRA_8888:
Manoj Kumar AVM5a5529b2014-02-24 18:16:37 -0800453 case HAL_PIXEL_FORMAT_RGB_565:
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700454 {
455 tileEnabled = true;
456 // check the usage flags
457 if (usage & (GRALLOC_USAGE_SW_READ_MASK |
458 GRALLOC_USAGE_SW_WRITE_MASK)) {
459 // Application intends to use CPU for rendering
460 tileEnabled = false;
461 }
462 break;
463 }
464 default:
465 break;
466 }
467 }
468 return tileEnabled;
469}
470
471// helper function
Sushil Chauhan65e26302015-01-14 10:48:57 -0800472unsigned int getSize(int format, int width, int height, int usage,
473 const int alignedw, const int alignedh) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700474
Sushil Chauhan65e26302015-01-14 10:48:57 -0800475 if (isUBwcEnabled(format, usage)) {
476 return getUBwcSize(width, height, format, alignedw, alignedh);
477 }
478
479 unsigned int size = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700480 switch (format) {
481 case HAL_PIXEL_FORMAT_RGBA_8888:
482 case HAL_PIXEL_FORMAT_RGBX_8888:
483 case HAL_PIXEL_FORMAT_BGRA_8888:
Naseer Ahmed82fc4b72013-09-20 01:31:37 -0700484 case HAL_PIXEL_FORMAT_sRGB_A_8888:
Jesse Hallfbe96d22013-09-20 01:39:43 -0700485 case HAL_PIXEL_FORMAT_sRGB_X_8888:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700486 size = alignedw * alignedh * 4;
487 break;
488 case HAL_PIXEL_FORMAT_RGB_888:
489 size = alignedw * alignedh * 3;
490 break;
491 case HAL_PIXEL_FORMAT_RGB_565:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700492 case HAL_PIXEL_FORMAT_RGBA_5551:
493 case HAL_PIXEL_FORMAT_RGBA_4444:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400494 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700495 size = alignedw * alignedh * 2;
496 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700497 case HAL_PIXEL_FORMAT_RAW10:
498 size = ALIGN(alignedw * alignedh, 4096);
499 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700500
501 // adreno formats
502 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
503 size = ALIGN(alignedw*alignedh, 4096);
504 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
505 break;
506 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
507 // The chroma plane is subsampled,
508 // but the pitch in bytes is unchanged
509 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700510 size = ALIGN( alignedw * alignedh, 8192);
511 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
512 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700513 case HAL_PIXEL_FORMAT_YV12:
514 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
515 ALOGE("w or h is odd for the YV12 format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800516 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700517 }
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400518 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700519 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700520 size = ALIGN(size, (unsigned int)4096);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700521 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800522 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
523 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naseer Ahmed2c215292013-09-18 23:47:42 -0400524 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800525 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700526 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
527 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700528 case HAL_PIXEL_FORMAT_YCbCr_422_I:
529 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700530 if(width & 1) {
531 ALOGE("width is odd for the YUV422_SP format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800532 return 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700533 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700534 size = ALIGN(alignedw * alignedh * 2, 4096);
535 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700536 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400537 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhane8a01792012-11-01 16:25:45 -0700538 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700539 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400540 case HAL_PIXEL_FORMAT_BLOB:
541 if(height != 1) {
542 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
543 must have height==1 ", __FUNCTION__);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800544 return 0;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400545 }
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400546 size = width;
547 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700548 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700549 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
550 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500551 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
552 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
553 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
554 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
555 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
556 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
557 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
558 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
559 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
560 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
561 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
562 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
563 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
564 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
565 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
566 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
567 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
568 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
569 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
570 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
571 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
572 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
573 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
574 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
575 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
576 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
577 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Jeykumar Sankaran8f4585f2014-02-05 15:23:40 -0800578 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500579 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
580 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700581 default:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800582 ALOGE("Unrecognized pixel format: 0x%x", __FUNCTION__, format);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800583 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700584 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700585 return size;
586}
587
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700588unsigned int getBufferSizeAndDimensions(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700589 int& alignedw, int &alignedh)
590{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700591 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700592
593 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
594 height,
595 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800596 0,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700597 alignedw,
598 alignedh);
599
Sushil Chauhan65e26302015-01-14 10:48:57 -0800600 size = getSize(format, width, height, 0 /* usage */, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700601
602 return size;
603}
604
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700605
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700606unsigned int getBufferSizeAndDimensions(int width, int height, int format,
607 int usage, int& alignedw, int &alignedh)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700608{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700609 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700610
611 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
612 height,
613 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800614 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700615 alignedw,
616 alignedh);
617
Sushil Chauhan65e26302015-01-14 10:48:57 -0800618 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700619
620 return size;
621}
622
623
624void getBufferAttributes(int width, int height, int format, int usage,
Sushil Chauhane61fac52015-04-30 17:14:37 -0700625 int& alignedw, int &alignedh, int& tiled, unsigned int& size)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700626{
Sushil Chauhane61fac52015-04-30 17:14:37 -0700627 tiled = isUBwcEnabled(format, usage) || isMacroTileEnabled(format, usage);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700628
629 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
630 height,
631 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800632 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700633 alignedw,
634 alignedh);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800635 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700636}
637
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400638int getYUVPlaneInfo(private_handle_t* hnd, struct android_ycbcr* ycbcr)
639{
640 int err = 0;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700641 int width = hnd->width;
642 int height = hnd->height;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700643 unsigned int ystride, cstride;
Sushil Chauhan4686c972015-02-20 15:44:52 -0800644 unsigned int alignment = 4096;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700645
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400646 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
647
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700648 // Check metadata if the geometry has been updated.
649 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
650 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
651 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(metadata->bufferDim.sliceWidth,
652 metadata->bufferDim.sliceHeight, hnd->format, 0, width, height);
653 }
654
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400655 // Get the chroma offsets from the handle width/height. We take advantage
656 // of the fact the width _is_ the stride
657 switch (hnd->format) {
658 //Semiplanar
659 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
660 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
661 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
662 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: //Same as YCbCr_420_SP_VENUS
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700663 ystride = cstride = width;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400664 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700665 ycbcr->cb = (void*)(hnd->base + ystride * height);
666 ycbcr->cr = (void*)(hnd->base + ystride * height + 1);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400667 ycbcr->ystride = ystride;
668 ycbcr->cstride = cstride;
669 ycbcr->chroma_step = 2;
670 break;
671
Sushil Chauhan4686c972015-02-20 15:44:52 -0800672 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
673 // NV12_UBWC buffer has these 4 planes in the following sequence:
674 // Y_Meta_Plane, Y_Plane, UV_Meta_Plane, UV_Plane
675 unsigned int y_meta_stride, y_meta_height, y_meta_size;
676 unsigned int y_stride, y_height, y_size;
677 unsigned int c_meta_stride, c_meta_height, c_meta_size;
678
679 y_meta_stride = VENUS_Y_META_STRIDE(COLOR_FMT_NV12_UBWC, width);
680 y_meta_height = VENUS_Y_META_SCANLINES(COLOR_FMT_NV12_UBWC, height);
681 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
682
683 y_stride = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
684 y_height = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
685 y_size = ALIGN((y_stride * y_height), alignment);
686
687 c_meta_stride = VENUS_UV_META_STRIDE(COLOR_FMT_NV12_UBWC, width);
688 c_meta_height = VENUS_UV_META_SCANLINES(COLOR_FMT_NV12_UBWC, height);
689 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
690
691 ycbcr->y = (void*)(hnd->base + y_meta_size);
692 ycbcr->cb = (void*)(hnd->base + y_meta_size + y_size + c_meta_size);
693 ycbcr->cr = (void*)(hnd->base + y_meta_size + y_size +
694 c_meta_size + 1);
695 ycbcr->ystride = y_stride;
696 ycbcr->cstride = VENUS_UV_STRIDE(COLOR_FMT_NV12_UBWC, width);
697 ycbcr->chroma_step = 2;
698 break;
699
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400700 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
701 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
702 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
703 case HAL_PIXEL_FORMAT_NV21_ZSL:
704 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700705 case HAL_PIXEL_FORMAT_RAW10:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700706 ystride = cstride = width;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400707 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700708 ycbcr->cr = (void*)(hnd->base + ystride * height);
709 ycbcr->cb = (void*)(hnd->base + ystride * height + 1);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400710 ycbcr->ystride = ystride;
711 ycbcr->cstride = cstride;
712 ycbcr->chroma_step = 2;
713 break;
714
715 //Planar
716 case HAL_PIXEL_FORMAT_YV12:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700717 ystride = width;
718 cstride = ALIGN(width/2, 16);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400719 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700720 ycbcr->cr = (void*)(hnd->base + ystride * height);
721 ycbcr->cb = (void*)(hnd->base + ystride * height +
722 cstride * height/2);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400723 ycbcr->ystride = ystride;
724 ycbcr->cstride = cstride;
725 ycbcr->chroma_step = 1;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400726 break;
727 //Unsupported formats
728 case HAL_PIXEL_FORMAT_YCbCr_422_I:
729 case HAL_PIXEL_FORMAT_YCrCb_422_I:
730 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
731 default:
732 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__,
733 hnd->format);
734 err = -EINVAL;
735 }
736 return err;
737
738}
739
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700740
741
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700742// Allocate buffer from width, height and format into a
743// private_handle_t. It is the responsibility of the caller
744// to free the buffer using the free_buffer function
745int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
746{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700747 alloc_data data;
748 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700749 gralloc::IAllocController* sAlloc =
750 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700751 data.base = 0;
752 data.fd = -1;
753 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700754 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
755 alignedh);
756
Naseer Ahmed29a26812012-06-14 00:56:20 -0700757 data.align = getpagesize();
758 data.uncached = useUncached(usage);
759 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700760
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700761 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700762 if (0 != err) {
763 ALOGE("%s: allocate failed", __FUNCTION__);
764 return -ENOMEM;
765 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700766
Naseer Ahmed29a26812012-06-14 00:56:20 -0700767 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700768 data.allocType, 0, format,
769 alignedw, alignedh);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700770 hnd->base = (uint64_t) data.base;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700771 hnd->offset = data.offset;
772 hnd->gpuaddr = 0;
773 *pHnd = hnd;
774 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700775}
776
777void free_buffer(private_handle_t *hnd)
778{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700779 gralloc::IAllocController* sAlloc =
780 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700781 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700782 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700783 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
784 }
785 if(hnd)
786 delete hnd;
787
788}
Sushil Chauhan65e26302015-01-14 10:48:57 -0800789
790// UBWC helper functions
791static bool isUBwcFormat(int format)
792{
793 // Explicitly defined UBWC formats
794 switch(format)
795 {
796 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
797 return true;
798 default:
799 return false;
800 }
801}
802
803static bool isUBwcSupported(int format)
804{
805 // Existing HAL formats with UBWC support
806 switch(format)
807 {
808 case HAL_PIXEL_FORMAT_RGB_565:
809 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -0700810 case HAL_PIXEL_FORMAT_RGBX_8888:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800811 case HAL_PIXEL_FORMAT_sRGB_A_8888:
812 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
813 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
814 return true;
815 default:
816 return false;
817 }
818}
819
820bool isUBwcEnabled(int format, int usage)
821{
Sushil Chauhan81594f62015-01-26 16:00:51 -0800822 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
823 if (isUBwcFormat(format))
824 return true;
825
826 // Allow UBWC, if client sets UBWC gralloc usage flag & GPU supports format.
827 if ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) && isUBwcSupported(format) &&
828 AdrenoMemInfo::getInstance().isUBWCSupportedByGPU(format)) {
829 // Allow UBWC, only if CPU usage flags are not set
830 if (!(usage & (GRALLOC_USAGE_SW_READ_MASK |
Sushil Chauhan65e26302015-01-14 10:48:57 -0800831 GRALLOC_USAGE_SW_WRITE_MASK))) {
832 return true;
833 }
834 }
835 return false;
836}
837
838static void getUBwcWidthAndHeight(int width, int height, int format,
839 int& aligned_w, int& aligned_h)
840{
841 switch (format)
842 {
843 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
844 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
845 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
846 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
847 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
848 break;
849 default:
850 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
851 aligned_w = 0;
852 aligned_h = 0;
853 break;
854 }
855}
856
857static void getUBwcBlockSize(int bpp, int& block_width, int& block_height)
858{
859 block_width = 0;
860 block_height = 0;
861
862 switch(bpp)
863 {
864 case 2:
865 case 4:
866 block_width = 16;
867 block_height = 4;
868 break;
869 case 8:
870 block_width = 8;
871 block_height = 4;
872 break;
873 case 16:
874 block_width = 4;
875 block_height = 4;
876 break;
877 default:
878 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
879 break;
880 }
881}
882
883static unsigned int getUBwcMetaBufferSize(int width, int height, int bpp)
884{
885 unsigned int size = 0;
886 int meta_width, meta_height;
887 int block_width, block_height;
888
889 getUBwcBlockSize(bpp, block_width, block_height);
890
891 if (!block_width || !block_height) {
892 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
893 return size;
894 }
895
896 // Align meta buffer height to 16 blocks
897 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
898
899 // Align meta buffer width to 64 blocks
900 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
901
902 // Align meta buffer size to 4K
903 size = ((meta_width * meta_height), 4096);
904 return size;
905}
906
907static unsigned int getUBwcSize(int width, int height, int format,
908 const int alignedw, const int alignedh) {
909
910 unsigned int size = 0;
911 switch (format) {
912 case HAL_PIXEL_FORMAT_RGB_565:
913 size = alignedw * alignedh * 2;
914 size += getUBwcMetaBufferSize(width, height, 2);
915 break;
916 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -0700917 case HAL_PIXEL_FORMAT_RGBX_8888:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800918 case HAL_PIXEL_FORMAT_sRGB_A_8888:
919 size = alignedw * alignedh * 4;
920 size += getUBwcMetaBufferSize(width, height, 4);
921 break;
922 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
923 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
924 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
925 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
926 break;
927 default:
928 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
929 break;
930 }
931 return size;
932}
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700933
934int getRgbDataAddress(private_handle_t* hnd, void* rgb_data)
935{
936 int err = 0;
937
938 // This api is for RGB* formats
939 if (hnd->format > HAL_PIXEL_FORMAT_sRGB_X_8888) {
940 return -EINVAL;
941 }
942
943 // linear buffer
944 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
945 rgb_data = (void*)hnd->base;
946 return err;
947 }
948
949 unsigned int meta_size = 0;
950 switch (hnd->format) {
951 case HAL_PIXEL_FORMAT_RGB_565:
952 meta_size = getUBwcMetaBufferSize(hnd->width, hnd->height, 2);
953 break;
954 case HAL_PIXEL_FORMAT_RGBA_8888:
955 case HAL_PIXEL_FORMAT_RGBX_8888:
956 case HAL_PIXEL_FORMAT_sRGB_A_8888:
957 meta_size = getUBwcMetaBufferSize(hnd->width, hnd->height, 4);
958 break;
959 default:
960 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
961 err = -EINVAL;
962 break;
963 }
964
965 rgb_data = (void*)(hnd->base + meta_size);
966 return err;
967}