blob: 8eaf7f4c8c97ecbdcf0c8fb937d6c759c9df0b32 [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
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700152void AdrenoMemInfo::getAlignedWidthAndHeight(const private_handle_t *hnd, int& aligned_w,
153 int& aligned_h) {
154 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
155 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
156 int w = metadata->bufferDim.sliceWidth;
157 int h = metadata->bufferDim.sliceHeight;
158 int f = hnd->format;
159 int usage = 0;
160
161 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
162 usage = GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
163 }
164
165 getAlignedWidthAndHeight(w, h, f, usage, aligned_w, aligned_h);
166 } else {
167 aligned_w = hnd->width;
168 aligned_h = hnd->height;
169 }
170
171}
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700172
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700173bool isUncompressedRgbFormat(int format)
174{
175 bool is_rgb_format = false;
176
177 switch (format)
178 {
179 case HAL_PIXEL_FORMAT_RGBA_8888:
180 case HAL_PIXEL_FORMAT_RGBX_8888:
181 case HAL_PIXEL_FORMAT_RGB_888:
182 case HAL_PIXEL_FORMAT_RGB_565:
183 case HAL_PIXEL_FORMAT_BGRA_8888:
184 case HAL_PIXEL_FORMAT_RGBA_5551:
185 case HAL_PIXEL_FORMAT_RGBA_4444:
186 case HAL_PIXEL_FORMAT_R_8:
187 case HAL_PIXEL_FORMAT_RG_88:
188 case HAL_PIXEL_FORMAT_BGRX_8888: // Intentional fallthrough
189 is_rgb_format = true;
190 break;
191 default:
192 break;
193 }
194
195 return is_rgb_format;
196}
197
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800198void AdrenoMemInfo::getAlignedWidthAndHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800199 int usage, int& aligned_w, int& aligned_h)
Naomi Luisa44100c2013-02-08 12:42:03 -0800200{
Sushil Chauhane61fac52015-04-30 17:14:37 -0700201 bool ubwc_enabled = isUBwcEnabled(format, usage);
202
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800203 // Currently surface padding is only computed for RGB* surfaces.
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700204 if (isUncompressedRgbFormat(format) == true) {
Sushil Chauhane61fac52015-04-30 17:14:37 -0700205 int tileEnabled = ubwc_enabled || isMacroTileEnabled(format, usage);
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700206 getGpuAlignedWidthHeight(width, height, format, tileEnabled, aligned_w, aligned_h);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800207 return;
Naomi Luisa44100c2013-02-08 12:42:03 -0800208 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800209
Sushil Chauhane61fac52015-04-30 17:14:37 -0700210 if (ubwc_enabled) {
Sushil Chauhan65e26302015-01-14 10:48:57 -0800211 getUBwcWidthAndHeight(width, height, format, aligned_w, aligned_h);
212 return;
213 }
214
215 aligned_w = width;
216 aligned_h = height;
217 switch (format)
218 {
219 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
220 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800221 aligned_w = ALIGN(width, 32);
222 break;
Ajay Dudani4dc06492015-03-26 07:28:11 -0700223 case HAL_PIXEL_FORMAT_RAW16:
Shuzhen Wang2a000b22014-08-20 00:15:51 -0700224 aligned_w = ALIGN(width, 16);
225 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700226 case HAL_PIXEL_FORMAT_RAW10:
227 aligned_w = ALIGN(width * 10 /8, 16);
228 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800229 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
230 aligned_w = ALIGN(width, 128);
231 break;
232 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
233 case HAL_PIXEL_FORMAT_YV12:
234 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
235 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
236 case HAL_PIXEL_FORMAT_YCbCr_422_I:
237 case HAL_PIXEL_FORMAT_YCrCb_422_I:
238 aligned_w = ALIGN(width, 16);
239 break;
240 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
241 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
242 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
243 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
244 break;
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530245 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
246 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV21, width);
247 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV21, height);
248 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800249 case HAL_PIXEL_FORMAT_BLOB:
250 break;
251 case HAL_PIXEL_FORMAT_NV21_ZSL:
252 aligned_w = ALIGN(width, 64);
253 aligned_h = ALIGN(height, 64);
254 break;
255 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
256 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
257 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
258 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
259 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
260 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
261 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
262 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
263 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
264 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
265 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
266 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
267 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
268 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
269 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
270 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
271 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
272 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
273 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
274 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
275 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
276 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
277 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
278 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
279 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
280 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
281 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
282 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
283 if(LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
284 int bytesPerPixel = 0;
285 int raster_mode = 0; //Adreno unknown raster mode.
286 int padding_threshold = 512; //Threshold for padding
287 //surfaces.
288
289 LINK_adreno_compute_compressedfmt_aligned_width_and_height(
290 width, height, format, 0,raster_mode, padding_threshold,
291 &aligned_w, &aligned_h, &bytesPerPixel);
292 } else {
293 ALOGW("%s: Warning!! Symbols" \
294 " compute_compressedfmt_aligned_width_and_height" \
295 " not found", __FUNCTION__);
296 }
297 break;
298 default: break;
299 }
300}
301
302void AdrenoMemInfo::getGpuAlignedWidthHeight(int width, int height, int format,
303 int tile_enabled, int& aligned_w, int& aligned_h)
304{
305 aligned_w = ALIGN(width, 32);
306 aligned_h = ALIGN(height, 32);
307
308 // Don't add any additional padding if debug.gralloc.map_fb_memory
309 // is enabled
310 char property[PROPERTY_VALUE_MAX];
311 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
312 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
313 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
314 return;
315 }
316
317 int bpp = 4;
318 switch(format)
319 {
320 case HAL_PIXEL_FORMAT_RGB_888:
321 bpp = 3;
322 break;
323 case HAL_PIXEL_FORMAT_RGB_565:
324 case HAL_PIXEL_FORMAT_RGBA_5551:
325 case HAL_PIXEL_FORMAT_RGBA_4444:
326 bpp = 2;
327 break;
328 default: break;
329 }
330
331 if (libadreno_utils) {
332 int raster_mode = 0; // Adreno unknown raster mode.
333 int padding_threshold = 512; // Threshold for padding surfaces.
334 // the function below computes aligned width and aligned height
335 // based on linear or macro tile mode selected.
336 if(LINK_adreno_compute_aligned_width_and_height) {
337 LINK_adreno_compute_aligned_width_and_height(width,
338 height, bpp, tile_enabled,
339 raster_mode, padding_threshold,
340 &aligned_w, &aligned_h);
341
342 } else if(LINK_adreno_compute_padding) {
343 int surface_tile_height = 1; // Linear surface
344 aligned_w = LINK_adreno_compute_padding(width, bpp,
345 surface_tile_height, raster_mode,
346 padding_threshold);
347 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
348 __FUNCTION__);
349 } else {
350 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
351 "compute_aligned_width_and_height not found", __FUNCTION__);
352 }
353 }
354}
355
356int AdrenoMemInfo::isUBWCSupportedByGPU(int format)
357{
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700358 if (!gfx_ubwc_disable && libadreno_utils) {
Sushil Chauhan082acd62015-01-14 16:49:29 -0800359 if (LINK_adreno_isUBWCSupportedByGpu) {
360 ADRENOPIXELFORMAT gpu_format = getGpuPixelFormat(format);
361 return LINK_adreno_isUBWCSupportedByGpu(gpu_format);
362 }
363 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800364 return 0;
Naomi Luisa44100c2013-02-08 12:42:03 -0800365}
366
Sushil Chauhan082acd62015-01-14 16:49:29 -0800367ADRENOPIXELFORMAT AdrenoMemInfo::getGpuPixelFormat(int hal_format)
368{
369 switch (hal_format) {
370 case HAL_PIXEL_FORMAT_RGBA_8888:
371 return ADRENO_PIXELFORMAT_R8G8B8A8;
Sushil Chauhan6686c802015-04-15 11:30:39 -0700372 case HAL_PIXEL_FORMAT_RGBX_8888:
373 return ADRENO_PIXELFORMAT_R8G8B8X8;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800374 case HAL_PIXEL_FORMAT_RGB_565:
375 return ADRENO_PIXELFORMAT_B5G6R5;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800376 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800377 return ADRENO_PIXELFORMAT_NV12;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800378 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
379 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800380 return ADRENO_PIXELFORMAT_NV12_EXT;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800381 default:
382 ALOGE("%s: No map for format: 0x%x", __FUNCTION__, hal_format);
383 break;
384 }
385 return ADRENO_PIXELFORMAT_UNKNOWN;
386}
387
Naomi Luisa44100c2013-02-08 12:42:03 -0800388//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700389IAllocController* IAllocController::sController = NULL;
390IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700391{
392 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700393 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700394 }
395 return sController;
396}
397
398
399//-------------- IonController-----------------------//
400IonController::IonController()
401{
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530402 allocateIonMem();
403}
404
405void IonController::allocateIonMem()
406{
407 mIonAlloc = new IonAlloc();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700408}
409
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700410int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700411{
412 int ionFlags = 0;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500413 int ionHeapId = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700414 int ret;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700415
416 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700417 data.allocType = 0;
418
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530419 if(usage & GRALLOC_USAGE_PROTECTED) {
Prabhanjan Kandulae8f4bec2013-10-24 16:32:51 +0530420 if (usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800421 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
Shalaj Jain3c490412015-04-22 16:52:03 -0700422 ionHeapId = ION_HEAP(SD_HEAP_ID);
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800423 /*
424 * There is currently no flag in ION for Secure Display
Shalaj Jain3c490412015-04-22 16:52:03 -0700425 * VM. Please add it to the define once available.
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800426 */
Shalaj Jain3c490412015-04-22 16:52:03 -0700427 ionFlags |= ION_SD_FLAGS;
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800428 } else {
Shalaj Jain3c490412015-04-22 16:52:03 -0700429 ionHeapId = ION_HEAP(CP_HEAP_ID);
430 ionFlags |= ION_CP_FLAGS;
Shalaj Jain13cdf812014-12-02 16:20:54 -0800431 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530432 } else {
433 // for targets/OEMs which do not need HW level protection
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500434 // do not set ion secure flag & MM heap. Fallback to system heap.
435 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Justin Philipd6166602014-08-12 13:42:21 +0530436 data.allocType |= private_handle_t::PRIV_FLAGS_PROTECTED_BUFFER;
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500437 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530438 } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
439 //MM Heap is exclusively a secure heap.
440 //If it is used for non secure cases, fallback to IOMMU heap
441 ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
442 cannot be used as an insecure heap!\
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500443 trying to use system heap instead !!");
444 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500445 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700446
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700447 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500448 ionHeapId |= ION_HEAP(ION_CAMERA_HEAP_ID);
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700449
Arun Kumar K.R0daaa992013-03-12 15:08:29 -0700450 if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500451 ionHeapId |= ION_HEAP(ION_ADSP_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700452
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530453 if(ionFlags & ION_SECURE)
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500454 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700455
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500456 // if no ion heap flags are set, default to system heap
457 if(!ionHeapId)
458 ionHeapId = ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700459
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500460 //At this point we should have the right heap set, there is no fallback
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700461 data.flags = ionFlags;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500462 data.heapId = ionHeapId;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700463 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700464
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700465 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700466 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500467 } else {
468 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x",
469 __FUNCTION__, ionHeapId, ionFlags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700470 }
471
472 return ret;
473}
474
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700475IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700476{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700477 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700478 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
479 memalloc = mIonAlloc;
480 } else {
481 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
482 }
483
484 return memalloc;
485}
486
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700487bool isMacroTileEnabled(int format, int usage)
488{
489 bool tileEnabled = false;
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -0700490 int isMacroTileSupportedByMDP = 0;
491
492 qdutils::querySDEInfo(HAS_MACRO_TILE, &isMacroTileSupportedByMDP);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700493
494 // Check whether GPU & MDSS supports MacroTiling feature
495 if(AdrenoMemInfo::getInstance().isMacroTilingSupportedByGPU() &&
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -0700496 isMacroTileSupportedByMDP)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700497 {
498 // check the format
499 switch(format)
500 {
501 case HAL_PIXEL_FORMAT_RGBA_8888:
502 case HAL_PIXEL_FORMAT_RGBX_8888:
503 case HAL_PIXEL_FORMAT_BGRA_8888:
Manoj Kumar AVM5a5529b2014-02-24 18:16:37 -0800504 case HAL_PIXEL_FORMAT_RGB_565:
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700505 {
506 tileEnabled = true;
507 // check the usage flags
508 if (usage & (GRALLOC_USAGE_SW_READ_MASK |
509 GRALLOC_USAGE_SW_WRITE_MASK)) {
510 // Application intends to use CPU for rendering
511 tileEnabled = false;
512 }
513 break;
514 }
515 default:
516 break;
517 }
518 }
519 return tileEnabled;
520}
521
522// helper function
Sushil Chauhan65e26302015-01-14 10:48:57 -0800523unsigned int getSize(int format, int width, int height, int usage,
524 const int alignedw, const int alignedh) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700525
Sushil Chauhan65e26302015-01-14 10:48:57 -0800526 if (isUBwcEnabled(format, usage)) {
527 return getUBwcSize(width, height, format, alignedw, alignedh);
528 }
529
530 unsigned int size = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700531 switch (format) {
532 case HAL_PIXEL_FORMAT_RGBA_8888:
533 case HAL_PIXEL_FORMAT_RGBX_8888:
534 case HAL_PIXEL_FORMAT_BGRA_8888:
535 size = alignedw * alignedh * 4;
536 break;
537 case HAL_PIXEL_FORMAT_RGB_888:
538 size = alignedw * alignedh * 3;
539 break;
540 case HAL_PIXEL_FORMAT_RGB_565:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700541 case HAL_PIXEL_FORMAT_RGBA_5551:
542 case HAL_PIXEL_FORMAT_RGBA_4444:
Ajay Dudani4dc06492015-03-26 07:28:11 -0700543 case HAL_PIXEL_FORMAT_RAW16:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700544 size = alignedw * alignedh * 2;
545 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700546 case HAL_PIXEL_FORMAT_RAW10:
547 size = ALIGN(alignedw * alignedh, 4096);
548 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700549
550 // adreno formats
551 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
552 size = ALIGN(alignedw*alignedh, 4096);
553 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
554 break;
555 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
556 // The chroma plane is subsampled,
557 // but the pitch in bytes is unchanged
558 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700559 size = ALIGN( alignedw * alignedh, 8192);
560 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
561 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700562 case HAL_PIXEL_FORMAT_YV12:
563 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
564 ALOGE("w or h is odd for the YV12 format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800565 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700566 }
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400567 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700568 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700569 size = ALIGN(size, (unsigned int)4096);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700570 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800571 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
572 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naseer Ahmed2c215292013-09-18 23:47:42 -0400573 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800574 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700575 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
576 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700577 case HAL_PIXEL_FORMAT_YCbCr_422_I:
578 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700579 if(width & 1) {
580 ALOGE("width is odd for the YUV422_SP format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800581 return 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700582 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700583 size = ALIGN(alignedw * alignedh * 2, 4096);
584 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700585 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400586 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhane8a01792012-11-01 16:25:45 -0700587 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700588 break;
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530589 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
590 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
591 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400592 case HAL_PIXEL_FORMAT_BLOB:
593 if(height != 1) {
594 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
595 must have height==1 ", __FUNCTION__);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800596 return 0;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400597 }
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400598 size = width;
599 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700600 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700601 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
602 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500603 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
604 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
605 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
606 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
607 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
608 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
609 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
610 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
611 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
612 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
613 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
614 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
615 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
616 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
617 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
618 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
619 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
620 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
621 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
622 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
623 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
624 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
625 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
626 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
627 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
628 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
629 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Jeykumar Sankaran8f4585f2014-02-05 15:23:40 -0800630 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500631 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
632 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700633 default:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800634 ALOGE("Unrecognized pixel format: 0x%x", __FUNCTION__, format);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800635 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700636 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700637 return size;
638}
639
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700640unsigned int getBufferSizeAndDimensions(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700641 int& alignedw, int &alignedh)
642{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700643 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700644
645 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
646 height,
647 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800648 0,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700649 alignedw,
650 alignedh);
651
Sushil Chauhan65e26302015-01-14 10:48:57 -0800652 size = getSize(format, width, height, 0 /* usage */, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700653
654 return size;
655}
656
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700657
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700658unsigned int getBufferSizeAndDimensions(int width, int height, int format,
659 int usage, int& alignedw, int &alignedh)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700660{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700661 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700662
663 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
664 height,
665 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800666 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700667 alignedw,
668 alignedh);
669
Sushil Chauhan65e26302015-01-14 10:48:57 -0800670 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700671
672 return size;
673}
674
675
676void getBufferAttributes(int width, int height, int format, int usage,
Sushil Chauhane61fac52015-04-30 17:14:37 -0700677 int& alignedw, int &alignedh, int& tiled, unsigned int& size)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700678{
Sushil Chauhane61fac52015-04-30 17:14:37 -0700679 tiled = isUBwcEnabled(format, usage) || isMacroTileEnabled(format, usage);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700680
681 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
682 height,
683 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800684 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700685 alignedw,
686 alignedh);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800687 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700688}
689
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400690int getYUVPlaneInfo(private_handle_t* hnd, struct android_ycbcr* ycbcr)
691{
692 int err = 0;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700693 int width = hnd->width;
694 int height = hnd->height;
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700695 int format = hnd->format;
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700696
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700697 unsigned int ystride, cstride;
Sushil Chauhan4686c972015-02-20 15:44:52 -0800698 unsigned int alignment = 4096;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700699
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400700 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700701 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
702
703 // Check if UBWC buffer has been rendered in linear format.
704 if (metadata && (metadata->operation & LINEAR_FORMAT)) {
705 format = metadata->linearFormat;
706 }
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400707
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700708 // Check metadata if the geometry has been updated.
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700709 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700710 int usage = 0;
711
712 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
713 usage = GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
714 }
715
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700716 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(metadata->bufferDim.sliceWidth,
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700717 metadata->bufferDim.sliceHeight, format, usage, width, height);
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700718 }
719
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400720 // Get the chroma offsets from the handle width/height. We take advantage
721 // of the fact the width _is_ the stride
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700722 switch (format) {
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400723 //Semiplanar
724 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
725 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
726 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
727 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: //Same as YCbCr_420_SP_VENUS
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700728 ystride = cstride = width;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400729 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700730 ycbcr->cb = (void*)(hnd->base + ystride * height);
731 ycbcr->cr = (void*)(hnd->base + ystride * height + 1);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400732 ycbcr->ystride = ystride;
733 ycbcr->cstride = cstride;
734 ycbcr->chroma_step = 2;
735 break;
736
Sushil Chauhan4686c972015-02-20 15:44:52 -0800737 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
738 // NV12_UBWC buffer has these 4 planes in the following sequence:
739 // Y_Meta_Plane, Y_Plane, UV_Meta_Plane, UV_Plane
740 unsigned int y_meta_stride, y_meta_height, y_meta_size;
741 unsigned int y_stride, y_height, y_size;
742 unsigned int c_meta_stride, c_meta_height, c_meta_size;
743
744 y_meta_stride = VENUS_Y_META_STRIDE(COLOR_FMT_NV12_UBWC, width);
745 y_meta_height = VENUS_Y_META_SCANLINES(COLOR_FMT_NV12_UBWC, height);
746 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
747
748 y_stride = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
749 y_height = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
750 y_size = ALIGN((y_stride * y_height), alignment);
751
752 c_meta_stride = VENUS_UV_META_STRIDE(COLOR_FMT_NV12_UBWC, width);
753 c_meta_height = VENUS_UV_META_SCANLINES(COLOR_FMT_NV12_UBWC, height);
754 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
755
756 ycbcr->y = (void*)(hnd->base + y_meta_size);
757 ycbcr->cb = (void*)(hnd->base + y_meta_size + y_size + c_meta_size);
758 ycbcr->cr = (void*)(hnd->base + y_meta_size + y_size +
759 c_meta_size + 1);
760 ycbcr->ystride = y_stride;
761 ycbcr->cstride = VENUS_UV_STRIDE(COLOR_FMT_NV12_UBWC, width);
762 ycbcr->chroma_step = 2;
763 break;
764
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400765 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
766 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
767 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530768 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400769 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ajay Dudani4dc06492015-03-26 07:28:11 -0700770 case HAL_PIXEL_FORMAT_RAW16:
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700771 case HAL_PIXEL_FORMAT_RAW10:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700772 ystride = cstride = width;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400773 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700774 ycbcr->cr = (void*)(hnd->base + ystride * height);
775 ycbcr->cb = (void*)(hnd->base + ystride * height + 1);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400776 ycbcr->ystride = ystride;
777 ycbcr->cstride = cstride;
778 ycbcr->chroma_step = 2;
779 break;
780
781 //Planar
782 case HAL_PIXEL_FORMAT_YV12:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700783 ystride = width;
784 cstride = ALIGN(width/2, 16);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400785 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700786 ycbcr->cr = (void*)(hnd->base + ystride * height);
787 ycbcr->cb = (void*)(hnd->base + ystride * height +
788 cstride * height/2);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400789 ycbcr->ystride = ystride;
790 ycbcr->cstride = cstride;
791 ycbcr->chroma_step = 1;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400792 break;
793 //Unsupported formats
794 case HAL_PIXEL_FORMAT_YCbCr_422_I:
795 case HAL_PIXEL_FORMAT_YCrCb_422_I:
796 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
797 default:
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700798 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, format);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400799 err = -EINVAL;
800 }
801 return err;
802
803}
804
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700805
806
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700807// Allocate buffer from width, height and format into a
808// private_handle_t. It is the responsibility of the caller
809// to free the buffer using the free_buffer function
810int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
811{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700812 alloc_data data;
813 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700814 gralloc::IAllocController* sAlloc =
815 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700816 data.base = 0;
817 data.fd = -1;
818 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700819 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
820 alignedh);
821
Naseer Ahmed29a26812012-06-14 00:56:20 -0700822 data.align = getpagesize();
823 data.uncached = useUncached(usage);
824 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700825
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700826 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700827 if (0 != err) {
828 ALOGE("%s: allocate failed", __FUNCTION__);
829 return -ENOMEM;
830 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700831
Naseer Ahmed29a26812012-06-14 00:56:20 -0700832 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700833 data.allocType, 0, format,
834 alignedw, alignedh);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700835 hnd->base = (uint64_t) data.base;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700836 hnd->offset = data.offset;
837 hnd->gpuaddr = 0;
838 *pHnd = hnd;
839 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700840}
841
842void free_buffer(private_handle_t *hnd)
843{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700844 gralloc::IAllocController* sAlloc =
845 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700846 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700847 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700848 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
849 }
850 if(hnd)
851 delete hnd;
852
853}
Sushil Chauhan65e26302015-01-14 10:48:57 -0800854
855// UBWC helper functions
856static bool isUBwcFormat(int format)
857{
858 // Explicitly defined UBWC formats
859 switch(format)
860 {
861 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
862 return true;
863 default:
864 return false;
865 }
866}
867
868static bool isUBwcSupported(int format)
869{
870 // Existing HAL formats with UBWC support
871 switch(format)
872 {
873 case HAL_PIXEL_FORMAT_RGB_565:
874 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -0700875 case HAL_PIXEL_FORMAT_RGBX_8888:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800876 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
877 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
878 return true;
879 default:
880 return false;
881 }
882}
883
884bool isUBwcEnabled(int format, int usage)
885{
Sushil Chauhan81594f62015-01-26 16:00:51 -0800886 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
887 if (isUBwcFormat(format))
888 return true;
889
Sushil Chauhan7807d192015-08-13 10:10:48 -0700890 // Allow UBWC, if an OpenGL client sets UBWC usage flag and GPU plus MDP
891 // support the format. OR if a non-OpenGL client like Rotator, sets UBWC
892 // usage flag and MDP supports the format.
893 if ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) && isUBwcSupported(format)) {
894 bool enable = true;
895 // Query GPU for UBWC only if buffer is intended to be used by GPU.
896 if (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER)) {
897 enable = AdrenoMemInfo::getInstance().isUBWCSupportedByGPU(format);
898 }
Sushil Chauhan81594f62015-01-26 16:00:51 -0800899 // Allow UBWC, only if CPU usage flags are not set
Sushil Chauhan7807d192015-08-13 10:10:48 -0700900 if (enable && !(usage & (GRALLOC_USAGE_SW_READ_MASK |
901 GRALLOC_USAGE_SW_WRITE_MASK))) {
Sushil Chauhan65e26302015-01-14 10:48:57 -0800902 return true;
903 }
904 }
905 return false;
906}
907
908static void getUBwcWidthAndHeight(int width, int height, int format,
909 int& aligned_w, int& aligned_h)
910{
911 switch (format)
912 {
913 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
914 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
915 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
916 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
917 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
918 break;
919 default:
920 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
921 aligned_w = 0;
922 aligned_h = 0;
923 break;
924 }
925}
926
927static void getUBwcBlockSize(int bpp, int& block_width, int& block_height)
928{
929 block_width = 0;
930 block_height = 0;
931
932 switch(bpp)
933 {
934 case 2:
935 case 4:
936 block_width = 16;
937 block_height = 4;
938 break;
939 case 8:
940 block_width = 8;
941 block_height = 4;
942 break;
943 case 16:
944 block_width = 4;
945 block_height = 4;
946 break;
947 default:
948 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
949 break;
950 }
951}
952
953static unsigned int getUBwcMetaBufferSize(int width, int height, int bpp)
954{
955 unsigned int size = 0;
956 int meta_width, meta_height;
957 int block_width, block_height;
958
959 getUBwcBlockSize(bpp, block_width, block_height);
960
961 if (!block_width || !block_height) {
962 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
963 return size;
964 }
965
966 // Align meta buffer height to 16 blocks
967 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
968
969 // Align meta buffer width to 64 blocks
970 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
971
972 // Align meta buffer size to 4K
Sushil Chauhanc85b65b2015-04-30 11:05:36 -0700973 size = ALIGN((meta_width * meta_height), 4096);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800974 return size;
975}
976
977static unsigned int getUBwcSize(int width, int height, int format,
978 const int alignedw, const int alignedh) {
979
980 unsigned int size = 0;
981 switch (format) {
982 case HAL_PIXEL_FORMAT_RGB_565:
983 size = alignedw * alignedh * 2;
984 size += getUBwcMetaBufferSize(width, height, 2);
985 break;
986 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -0700987 case HAL_PIXEL_FORMAT_RGBX_8888:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800988 size = alignedw * alignedh * 4;
989 size += getUBwcMetaBufferSize(width, height, 4);
990 break;
991 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
992 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
993 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
994 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
995 break;
996 default:
997 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
998 break;
999 }
1000 return size;
1001}
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001002
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001003int getRgbDataAddress(private_handle_t* hnd, void** rgb_data)
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001004{
1005 int err = 0;
1006
1007 // This api is for RGB* formats
Ajay Dudani4dc06492015-03-26 07:28:11 -07001008 if (hnd->format > HAL_PIXEL_FORMAT_BGRA_8888) {
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001009 return -EINVAL;
1010 }
1011
1012 // linear buffer
1013 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001014 *rgb_data = (void*)hnd->base;
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001015 return err;
1016 }
1017
1018 unsigned int meta_size = 0;
1019 switch (hnd->format) {
1020 case HAL_PIXEL_FORMAT_RGB_565:
1021 meta_size = getUBwcMetaBufferSize(hnd->width, hnd->height, 2);
1022 break;
1023 case HAL_PIXEL_FORMAT_RGBA_8888:
1024 case HAL_PIXEL_FORMAT_RGBX_8888:
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001025 meta_size = getUBwcMetaBufferSize(hnd->width, hnd->height, 4);
1026 break;
1027 default:
1028 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
1029 err = -EINVAL;
1030 break;
1031 }
1032
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001033 *rgb_data = (void*)(hnd->base + meta_size);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001034 return err;
1035}