blob: c052ffb8ed111bba64a25b6920eb7651650cdcde [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 }
122}
123
124AdrenoMemInfo::~AdrenoMemInfo()
125{
126 if (libadreno_utils) {
127 ::dlclose(libadreno_utils);
128 }
129}
130
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700131int AdrenoMemInfo::isMacroTilingSupportedByGPU()
132{
133 if ((libadreno_utils)) {
134 if(LINK_adreno_isMacroTilingSupportedByGpu) {
135 return LINK_adreno_isMacroTilingSupportedByGpu();
136 }
137 }
138 return 0;
139}
140
141
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800142void AdrenoMemInfo::getAlignedWidthAndHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800143 int usage, int& aligned_w, int& aligned_h)
Naomi Luisa44100c2013-02-08 12:42:03 -0800144{
Sushil Chauhan65e26302015-01-14 10:48:57 -0800145
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800146 // Currently surface padding is only computed for RGB* surfaces.
Jesse Hallfbe96d22013-09-20 01:39:43 -0700147 if (format <= HAL_PIXEL_FORMAT_sRGB_X_8888) {
Sushil Chauhan65e26302015-01-14 10:48:57 -0800148 int tileEnabled = isMacroTileEnabled(format, usage);
149 AdrenoMemInfo::getInstance().getGpuAlignedWidthHeight(width,
150 height, format, tileEnabled, aligned_w, aligned_h);
151 return;
Naomi Luisa44100c2013-02-08 12:42:03 -0800152 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800153
154 if (isUBwcEnabled(format, usage)) {
155 getUBwcWidthAndHeight(width, height, format, aligned_w, aligned_h);
156 return;
157 }
158
159 aligned_w = width;
160 aligned_h = height;
161 switch (format)
162 {
163 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
164 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
165 case HAL_PIXEL_FORMAT_RAW_SENSOR:
166 aligned_w = ALIGN(width, 32);
167 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700168 case HAL_PIXEL_FORMAT_RAW10:
169 aligned_w = ALIGN(width * 10 /8, 16);
170 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800171 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
172 aligned_w = ALIGN(width, 128);
173 break;
174 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
175 case HAL_PIXEL_FORMAT_YV12:
176 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
177 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
178 case HAL_PIXEL_FORMAT_YCbCr_422_I:
179 case HAL_PIXEL_FORMAT_YCrCb_422_I:
180 aligned_w = ALIGN(width, 16);
181 break;
182 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
183 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
184 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
185 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
186 break;
187 case HAL_PIXEL_FORMAT_BLOB:
188 break;
189 case HAL_PIXEL_FORMAT_NV21_ZSL:
190 aligned_w = ALIGN(width, 64);
191 aligned_h = ALIGN(height, 64);
192 break;
193 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
194 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
195 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
196 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
197 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
198 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
199 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
200 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
201 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
202 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
203 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
204 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
205 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
206 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
207 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
208 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
209 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
210 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
211 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
212 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
213 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
214 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
215 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
216 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
217 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
218 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
219 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
220 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
221 if(LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
222 int bytesPerPixel = 0;
223 int raster_mode = 0; //Adreno unknown raster mode.
224 int padding_threshold = 512; //Threshold for padding
225 //surfaces.
226
227 LINK_adreno_compute_compressedfmt_aligned_width_and_height(
228 width, height, format, 0,raster_mode, padding_threshold,
229 &aligned_w, &aligned_h, &bytesPerPixel);
230 } else {
231 ALOGW("%s: Warning!! Symbols" \
232 " compute_compressedfmt_aligned_width_and_height" \
233 " not found", __FUNCTION__);
234 }
235 break;
236 default: break;
237 }
238}
239
240void AdrenoMemInfo::getGpuAlignedWidthHeight(int width, int height, int format,
241 int tile_enabled, int& aligned_w, int& aligned_h)
242{
243 aligned_w = ALIGN(width, 32);
244 aligned_h = ALIGN(height, 32);
245
246 // Don't add any additional padding if debug.gralloc.map_fb_memory
247 // is enabled
248 char property[PROPERTY_VALUE_MAX];
249 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
250 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
251 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
252 return;
253 }
254
255 int bpp = 4;
256 switch(format)
257 {
258 case HAL_PIXEL_FORMAT_RGB_888:
259 bpp = 3;
260 break;
261 case HAL_PIXEL_FORMAT_RGB_565:
262 case HAL_PIXEL_FORMAT_RGBA_5551:
263 case HAL_PIXEL_FORMAT_RGBA_4444:
264 bpp = 2;
265 break;
266 default: break;
267 }
268
269 if (libadreno_utils) {
270 int raster_mode = 0; // Adreno unknown raster mode.
271 int padding_threshold = 512; // Threshold for padding surfaces.
272 // the function below computes aligned width and aligned height
273 // based on linear or macro tile mode selected.
274 if(LINK_adreno_compute_aligned_width_and_height) {
275 LINK_adreno_compute_aligned_width_and_height(width,
276 height, bpp, tile_enabled,
277 raster_mode, padding_threshold,
278 &aligned_w, &aligned_h);
279
280 } else if(LINK_adreno_compute_padding) {
281 int surface_tile_height = 1; // Linear surface
282 aligned_w = LINK_adreno_compute_padding(width, bpp,
283 surface_tile_height, raster_mode,
284 padding_threshold);
285 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
286 __FUNCTION__);
287 } else {
288 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
289 "compute_aligned_width_and_height not found", __FUNCTION__);
290 }
291 }
292}
293
294int AdrenoMemInfo::isUBWCSupportedByGPU(int format)
295{
Sushil Chauhan082acd62015-01-14 16:49:29 -0800296 if (libadreno_utils) {
297 if (LINK_adreno_isUBWCSupportedByGpu) {
298 ADRENOPIXELFORMAT gpu_format = getGpuPixelFormat(format);
299 return LINK_adreno_isUBWCSupportedByGpu(gpu_format);
300 }
301 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800302 return 0;
Naomi Luisa44100c2013-02-08 12:42:03 -0800303}
304
Sushil Chauhan082acd62015-01-14 16:49:29 -0800305ADRENOPIXELFORMAT AdrenoMemInfo::getGpuPixelFormat(int hal_format)
306{
307 switch (hal_format) {
308 case HAL_PIXEL_FORMAT_RGBA_8888:
309 return ADRENO_PIXELFORMAT_R8G8B8A8;
Sushil Chauhan6686c802015-04-15 11:30:39 -0700310 case HAL_PIXEL_FORMAT_RGBX_8888:
311 return ADRENO_PIXELFORMAT_R8G8B8X8;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800312 case HAL_PIXEL_FORMAT_RGB_565:
313 return ADRENO_PIXELFORMAT_B5G6R5;
314 case HAL_PIXEL_FORMAT_sRGB_A_8888:
315 return ADRENO_PIXELFORMAT_R8G8B8A8_SRGB;
316 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800317 return ADRENO_PIXELFORMAT_NV12;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800318 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
319 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800320 return ADRENO_PIXELFORMAT_NV12_EXT;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800321 default:
322 ALOGE("%s: No map for format: 0x%x", __FUNCTION__, hal_format);
323 break;
324 }
325 return ADRENO_PIXELFORMAT_UNKNOWN;
326}
327
Naomi Luisa44100c2013-02-08 12:42:03 -0800328//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700329IAllocController* IAllocController::sController = NULL;
330IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700331{
332 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700333 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700334 }
335 return sController;
336}
337
338
339//-------------- IonController-----------------------//
340IonController::IonController()
341{
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530342 allocateIonMem();
343}
344
345void IonController::allocateIonMem()
346{
347 mIonAlloc = new IonAlloc();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700348}
349
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700350int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700351{
352 int ionFlags = 0;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500353 int ionHeapId = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700354 int ret;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700355
356 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700357 data.allocType = 0;
358
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530359 if(usage & GRALLOC_USAGE_PROTECTED) {
Prabhanjan Kandulae8f4bec2013-10-24 16:32:51 +0530360 if (usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800361 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
Shalaj Jain3c490412015-04-22 16:52:03 -0700362 ionHeapId = ION_HEAP(SD_HEAP_ID);
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800363 /*
364 * There is currently no flag in ION for Secure Display
Shalaj Jain3c490412015-04-22 16:52:03 -0700365 * VM. Please add it to the define once available.
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800366 */
Shalaj Jain3c490412015-04-22 16:52:03 -0700367 ionFlags |= ION_SD_FLAGS;
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800368 } else {
Shalaj Jain3c490412015-04-22 16:52:03 -0700369 ionHeapId = ION_HEAP(CP_HEAP_ID);
370 ionFlags |= ION_CP_FLAGS;
Shalaj Jain13cdf812014-12-02 16:20:54 -0800371 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530372 } else {
373 // for targets/OEMs which do not need HW level protection
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500374 // do not set ion secure flag & MM heap. Fallback to system heap.
375 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Justin Philipd6166602014-08-12 13:42:21 +0530376 data.allocType |= private_handle_t::PRIV_FLAGS_PROTECTED_BUFFER;
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500377 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530378 } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
379 //MM Heap is exclusively a secure heap.
380 //If it is used for non secure cases, fallback to IOMMU heap
381 ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
382 cannot be used as an insecure heap!\
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500383 trying to use system heap instead !!");
384 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500385 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700386
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700387 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500388 ionHeapId |= ION_HEAP(ION_CAMERA_HEAP_ID);
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700389
Arun Kumar K.R0daaa992013-03-12 15:08:29 -0700390 if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500391 ionHeapId |= ION_HEAP(ION_ADSP_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700392
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530393 if(ionFlags & ION_SECURE)
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500394 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700395
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500396 // if no ion heap flags are set, default to system heap
397 if(!ionHeapId)
398 ionHeapId = ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700399
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500400 //At this point we should have the right heap set, there is no fallback
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700401 data.flags = ionFlags;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500402 data.heapId = ionHeapId;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700403 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700404
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700405 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700406 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500407 } else {
408 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x",
409 __FUNCTION__, ionHeapId, ionFlags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700410 }
411
412 return ret;
413}
414
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700415IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700416{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700417 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700418 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
419 memalloc = mIonAlloc;
420 } else {
421 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
422 }
423
424 return memalloc;
425}
426
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700427bool isMacroTileEnabled(int format, int usage)
428{
429 bool tileEnabled = false;
430
431 // Check whether GPU & MDSS supports MacroTiling feature
432 if(AdrenoMemInfo::getInstance().isMacroTilingSupportedByGPU() &&
433 qdutils::MDPVersion::getInstance().supportsMacroTile())
434 {
435 // check the format
436 switch(format)
437 {
438 case HAL_PIXEL_FORMAT_RGBA_8888:
439 case HAL_PIXEL_FORMAT_RGBX_8888:
440 case HAL_PIXEL_FORMAT_BGRA_8888:
Manoj Kumar AVM5a5529b2014-02-24 18:16:37 -0800441 case HAL_PIXEL_FORMAT_RGB_565:
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700442 {
443 tileEnabled = true;
444 // check the usage flags
445 if (usage & (GRALLOC_USAGE_SW_READ_MASK |
446 GRALLOC_USAGE_SW_WRITE_MASK)) {
447 // Application intends to use CPU for rendering
448 tileEnabled = false;
449 }
450 break;
451 }
452 default:
453 break;
454 }
455 }
456 return tileEnabled;
457}
458
459// helper function
Sushil Chauhan65e26302015-01-14 10:48:57 -0800460unsigned int getSize(int format, int width, int height, int usage,
461 const int alignedw, const int alignedh) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700462
Sushil Chauhan65e26302015-01-14 10:48:57 -0800463 if (isUBwcEnabled(format, usage)) {
464 return getUBwcSize(width, height, format, alignedw, alignedh);
465 }
466
467 unsigned int size = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700468 switch (format) {
469 case HAL_PIXEL_FORMAT_RGBA_8888:
470 case HAL_PIXEL_FORMAT_RGBX_8888:
471 case HAL_PIXEL_FORMAT_BGRA_8888:
Naseer Ahmed82fc4b72013-09-20 01:31:37 -0700472 case HAL_PIXEL_FORMAT_sRGB_A_8888:
Jesse Hallfbe96d22013-09-20 01:39:43 -0700473 case HAL_PIXEL_FORMAT_sRGB_X_8888:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700474 size = alignedw * alignedh * 4;
475 break;
476 case HAL_PIXEL_FORMAT_RGB_888:
477 size = alignedw * alignedh * 3;
478 break;
479 case HAL_PIXEL_FORMAT_RGB_565:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700480 case HAL_PIXEL_FORMAT_RGBA_5551:
481 case HAL_PIXEL_FORMAT_RGBA_4444:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400482 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700483 size = alignedw * alignedh * 2;
484 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700485 case HAL_PIXEL_FORMAT_RAW10:
486 size = ALIGN(alignedw * alignedh, 4096);
487 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700488
489 // adreno formats
490 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
491 size = ALIGN(alignedw*alignedh, 4096);
492 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
493 break;
494 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
495 // The chroma plane is subsampled,
496 // but the pitch in bytes is unchanged
497 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700498 size = ALIGN( alignedw * alignedh, 8192);
499 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
500 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700501 case HAL_PIXEL_FORMAT_YV12:
502 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
503 ALOGE("w or h is odd for the YV12 format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800504 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700505 }
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400506 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700507 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700508 size = ALIGN(size, (unsigned int)4096);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700509 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800510 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
511 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naseer Ahmed2c215292013-09-18 23:47:42 -0400512 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800513 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700514 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
515 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700516 case HAL_PIXEL_FORMAT_YCbCr_422_I:
517 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700518 if(width & 1) {
519 ALOGE("width is odd for the YUV422_SP format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800520 return 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700521 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700522 size = ALIGN(alignedw * alignedh * 2, 4096);
523 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700524 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400525 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhane8a01792012-11-01 16:25:45 -0700526 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700527 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400528 case HAL_PIXEL_FORMAT_BLOB:
529 if(height != 1) {
530 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
531 must have height==1 ", __FUNCTION__);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800532 return 0;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400533 }
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400534 size = width;
535 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700536 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700537 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
538 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500539 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
540 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
541 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
542 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
543 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
544 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
545 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
546 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
547 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
548 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
549 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
550 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
551 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
552 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
553 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
554 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
555 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
556 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
557 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
558 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
559 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
560 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
561 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
562 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
563 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
564 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
565 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Jeykumar Sankaran8f4585f2014-02-05 15:23:40 -0800566 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500567 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
568 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700569 default:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800570 ALOGE("Unrecognized pixel format: 0x%x", __FUNCTION__, format);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800571 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700572 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700573 return size;
574}
575
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700576unsigned int getBufferSizeAndDimensions(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700577 int& alignedw, int &alignedh)
578{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700579 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700580
581 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
582 height,
583 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800584 0,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700585 alignedw,
586 alignedh);
587
Sushil Chauhan65e26302015-01-14 10:48:57 -0800588 size = getSize(format, width, height, 0 /* usage */, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700589
590 return size;
591}
592
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700593
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700594unsigned int getBufferSizeAndDimensions(int width, int height, int format,
595 int usage, int& alignedw, int &alignedh)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700596{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700597 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700598
599 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
600 height,
601 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800602 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700603 alignedw,
604 alignedh);
605
Sushil Chauhan65e26302015-01-14 10:48:57 -0800606 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700607
608 return size;
609}
610
611
612void getBufferAttributes(int width, int height, int format, int usage,
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700613 int& alignedw, int &alignedh, int& tileEnabled, unsigned int& size)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700614{
615 tileEnabled = isMacroTileEnabled(format, usage);
616
617 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
618 height,
619 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800620 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700621 alignedw,
622 alignedh);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800623 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700624}
625
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400626int getYUVPlaneInfo(private_handle_t* hnd, struct android_ycbcr* ycbcr)
627{
628 int err = 0;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700629 int width = hnd->width;
630 int height = hnd->height;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700631 unsigned int ystride, cstride;
Sushil Chauhan4686c972015-02-20 15:44:52 -0800632 unsigned int alignment = 4096;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700633
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400634 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
635
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700636 // Check metadata if the geometry has been updated.
637 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
638 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
639 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(metadata->bufferDim.sliceWidth,
640 metadata->bufferDim.sliceHeight, hnd->format, 0, width, height);
641 }
642
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400643 // Get the chroma offsets from the handle width/height. We take advantage
644 // of the fact the width _is_ the stride
645 switch (hnd->format) {
646 //Semiplanar
647 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
648 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
649 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
650 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: //Same as YCbCr_420_SP_VENUS
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700651 ystride = cstride = width;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400652 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700653 ycbcr->cb = (void*)(hnd->base + ystride * height);
654 ycbcr->cr = (void*)(hnd->base + ystride * height + 1);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400655 ycbcr->ystride = ystride;
656 ycbcr->cstride = cstride;
657 ycbcr->chroma_step = 2;
658 break;
659
Sushil Chauhan4686c972015-02-20 15:44:52 -0800660 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
661 // NV12_UBWC buffer has these 4 planes in the following sequence:
662 // Y_Meta_Plane, Y_Plane, UV_Meta_Plane, UV_Plane
663 unsigned int y_meta_stride, y_meta_height, y_meta_size;
664 unsigned int y_stride, y_height, y_size;
665 unsigned int c_meta_stride, c_meta_height, c_meta_size;
666
667 y_meta_stride = VENUS_Y_META_STRIDE(COLOR_FMT_NV12_UBWC, width);
668 y_meta_height = VENUS_Y_META_SCANLINES(COLOR_FMT_NV12_UBWC, height);
669 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
670
671 y_stride = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
672 y_height = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
673 y_size = ALIGN((y_stride * y_height), alignment);
674
675 c_meta_stride = VENUS_UV_META_STRIDE(COLOR_FMT_NV12_UBWC, width);
676 c_meta_height = VENUS_UV_META_SCANLINES(COLOR_FMT_NV12_UBWC, height);
677 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
678
679 ycbcr->y = (void*)(hnd->base + y_meta_size);
680 ycbcr->cb = (void*)(hnd->base + y_meta_size + y_size + c_meta_size);
681 ycbcr->cr = (void*)(hnd->base + y_meta_size + y_size +
682 c_meta_size + 1);
683 ycbcr->ystride = y_stride;
684 ycbcr->cstride = VENUS_UV_STRIDE(COLOR_FMT_NV12_UBWC, width);
685 ycbcr->chroma_step = 2;
686 break;
687
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400688 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
689 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
690 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
691 case HAL_PIXEL_FORMAT_NV21_ZSL:
692 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700693 case HAL_PIXEL_FORMAT_RAW10:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700694 ystride = cstride = width;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400695 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700696 ycbcr->cr = (void*)(hnd->base + ystride * height);
697 ycbcr->cb = (void*)(hnd->base + ystride * height + 1);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400698 ycbcr->ystride = ystride;
699 ycbcr->cstride = cstride;
700 ycbcr->chroma_step = 2;
701 break;
702
703 //Planar
704 case HAL_PIXEL_FORMAT_YV12:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700705 ystride = width;
706 cstride = ALIGN(width/2, 16);
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 +
710 cstride * height/2);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400711 ycbcr->ystride = ystride;
712 ycbcr->cstride = cstride;
713 ycbcr->chroma_step = 1;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400714 break;
715 //Unsupported formats
716 case HAL_PIXEL_FORMAT_YCbCr_422_I:
717 case HAL_PIXEL_FORMAT_YCrCb_422_I:
718 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
719 default:
720 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__,
721 hnd->format);
722 err = -EINVAL;
723 }
724 return err;
725
726}
727
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700728
729
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700730// Allocate buffer from width, height and format into a
731// private_handle_t. It is the responsibility of the caller
732// to free the buffer using the free_buffer function
733int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
734{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700735 alloc_data data;
736 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700737 gralloc::IAllocController* sAlloc =
738 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700739 data.base = 0;
740 data.fd = -1;
741 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700742 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
743 alignedh);
744
Naseer Ahmed29a26812012-06-14 00:56:20 -0700745 data.align = getpagesize();
746 data.uncached = useUncached(usage);
747 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700748
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700749 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700750 if (0 != err) {
751 ALOGE("%s: allocate failed", __FUNCTION__);
752 return -ENOMEM;
753 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700754
Naseer Ahmed29a26812012-06-14 00:56:20 -0700755 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700756 data.allocType, 0, format,
757 alignedw, alignedh);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700758 hnd->base = (uint64_t) data.base;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700759 hnd->offset = data.offset;
760 hnd->gpuaddr = 0;
761 *pHnd = hnd;
762 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700763}
764
765void free_buffer(private_handle_t *hnd)
766{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700767 gralloc::IAllocController* sAlloc =
768 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700769 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700770 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700771 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
772 }
773 if(hnd)
774 delete hnd;
775
776}
Sushil Chauhan65e26302015-01-14 10:48:57 -0800777
778// UBWC helper functions
779static bool isUBwcFormat(int format)
780{
781 // Explicitly defined UBWC formats
782 switch(format)
783 {
784 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
785 return true;
786 default:
787 return false;
788 }
789}
790
791static bool isUBwcSupported(int format)
792{
793 // Existing HAL formats with UBWC support
794 switch(format)
795 {
796 case HAL_PIXEL_FORMAT_RGB_565:
797 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -0700798 case HAL_PIXEL_FORMAT_RGBX_8888:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800799 case HAL_PIXEL_FORMAT_sRGB_A_8888:
800 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
801 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
802 return true;
803 default:
804 return false;
805 }
806}
807
808bool isUBwcEnabled(int format, int usage)
809{
Sushil Chauhan81594f62015-01-26 16:00:51 -0800810 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
811 if (isUBwcFormat(format))
812 return true;
813
814 // Allow UBWC, if client sets UBWC gralloc usage flag & GPU supports format.
815 if ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) && isUBwcSupported(format) &&
816 AdrenoMemInfo::getInstance().isUBWCSupportedByGPU(format)) {
817 // Allow UBWC, only if CPU usage flags are not set
818 if (!(usage & (GRALLOC_USAGE_SW_READ_MASK |
Sushil Chauhan65e26302015-01-14 10:48:57 -0800819 GRALLOC_USAGE_SW_WRITE_MASK))) {
820 return true;
821 }
822 }
823 return false;
824}
825
826static void getUBwcWidthAndHeight(int width, int height, int format,
827 int& aligned_w, int& aligned_h)
828{
829 switch (format)
830 {
831 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
832 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
833 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
834 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
835 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
836 break;
837 default:
838 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
839 aligned_w = 0;
840 aligned_h = 0;
841 break;
842 }
843}
844
845static void getUBwcBlockSize(int bpp, int& block_width, int& block_height)
846{
847 block_width = 0;
848 block_height = 0;
849
850 switch(bpp)
851 {
852 case 2:
853 case 4:
854 block_width = 16;
855 block_height = 4;
856 break;
857 case 8:
858 block_width = 8;
859 block_height = 4;
860 break;
861 case 16:
862 block_width = 4;
863 block_height = 4;
864 break;
865 default:
866 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
867 break;
868 }
869}
870
871static unsigned int getUBwcMetaBufferSize(int width, int height, int bpp)
872{
873 unsigned int size = 0;
874 int meta_width, meta_height;
875 int block_width, block_height;
876
877 getUBwcBlockSize(bpp, block_width, block_height);
878
879 if (!block_width || !block_height) {
880 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
881 return size;
882 }
883
884 // Align meta buffer height to 16 blocks
885 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
886
887 // Align meta buffer width to 64 blocks
888 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
889
890 // Align meta buffer size to 4K
891 size = ((meta_width * meta_height), 4096);
892 return size;
893}
894
895static unsigned int getUBwcSize(int width, int height, int format,
896 const int alignedw, const int alignedh) {
897
898 unsigned int size = 0;
899 switch (format) {
900 case HAL_PIXEL_FORMAT_RGB_565:
901 size = alignedw * alignedh * 2;
902 size += getUBwcMetaBufferSize(width, height, 2);
903 break;
904 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -0700905 case HAL_PIXEL_FORMAT_RGBX_8888:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800906 case HAL_PIXEL_FORMAT_sRGB_A_8888:
907 size = alignedw * alignedh * 4;
908 size += getUBwcMetaBufferSize(width, height, 4);
909 break;
910 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
911 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
912 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
913 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
914 break;
915 default:
916 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
917 break;
918 }
919 return size;
920}