blob: a7c4a9a19e908835ca7bfe7698f311b289857e89 [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
Arun Kumar K.R6c85f052014-01-21 21:47:41 -08002 * Copyright (c) 2011-2014, 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"
Iliyan Malchev202a77d2012-06-11 14:41:12 -070040
Sushil Chauhanc6bd6d92012-12-12 12:33:01 -080041#ifdef VENUS_COLOR_FORMAT
42#include <media/msm_media_info.h>
43#else
44#define VENUS_Y_STRIDE(args...) 0
45#define VENUS_Y_SCANLINES(args...) 0
46#define VENUS_BUFFER_SIZE(args...) 0
47#endif
48
Naseer Ahmed63326f42013-12-18 02:45:48 -050049#define ASTC_BLOCK_SIZE 16
Naseer Ahmed63326f42013-12-18 02:45:48 -050050
Iliyan Malchev202a77d2012-06-11 14:41:12 -070051using namespace gralloc;
Naseer Ahmeda87da602012-07-01 23:54:19 -070052using namespace qdutils;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070053
Naomi Luisa44100c2013-02-08 12:42:03 -080054ANDROID_SINGLETON_STATIC_INSTANCE(AdrenoMemInfo);
55
Sushil Chauhan65e26302015-01-14 10:48:57 -080056static void getUBwcWidthAndHeight(int, int, int, int&, int&);
57static unsigned int getUBwcSize(int, int, int, const int, const int);
58
Iliyan Malchev202a77d2012-06-11 14:41:12 -070059//Common functions
Naseer Ahmed29a26812012-06-14 00:56:20 -070060static bool canFallback(int usage, bool triedSystem)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070061{
62 // Fallback to system heap when alloc fails unless
63 // 1. Composition type is MDP
64 // 2. Alloc from system heap was already tried
65 // 3. The heap type is requsted explicitly
66 // 4. The heap type is protected
67 // 5. The buffer is meant for external display only
68
Naseer Ahmeda87da602012-07-01 23:54:19 -070069 if(QCCompositionType::getInstance().getCompositionType() &
70 COMPOSITION_TYPE_MDP)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070071 return false;
72 if(triedSystem)
73 return false;
Sushil Chauhan7651a802013-01-08 16:08:09 -080074 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PROTECTED))
Iliyan Malchev202a77d2012-06-11 14:41:12 -070075 return false;
Naseer Ahmed4c588a22012-07-31 19:12:17 -070076 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY))
Iliyan Malchev202a77d2012-06-11 14:41:12 -070077 return false;
78 //Return true by default
79 return true;
80}
81
Saurabh Shah1adcafe2014-12-19 10:05:41 -080082/* The default policy is to return cached buffers unless the client explicity
83 * sets the PRIVATE_UNCACHED flag or indicates that the buffer will be rarely
84 * read or written in software. Any combination with a _RARELY_ flag will be
85 * treated as uncached. */
86static bool useUncached(const int& usage) {
87 if((usage & GRALLOC_USAGE_PRIVATE_UNCACHED) or
88 ((usage & GRALLOC_USAGE_SW_WRITE_MASK) ==
89 GRALLOC_USAGE_SW_WRITE_RARELY) or
90 ((usage & GRALLOC_USAGE_SW_READ_MASK) ==
91 GRALLOC_USAGE_SW_READ_RARELY))
92 return true;
93
94 return false;
95}
96
Naomi Luisa44100c2013-02-08 12:42:03 -080097//-------------- AdrenoMemInfo-----------------------//
Naomi Luis01f5c8e2013-02-11 12:46:24 -080098AdrenoMemInfo::AdrenoMemInfo()
99{
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800100 LINK_adreno_compute_aligned_width_and_height = NULL;
101 LINK_adreno_compute_padding = NULL;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700102 LINK_adreno_isMacroTilingSupportedByGpu = NULL;
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800103 LINK_adreno_compute_compressedfmt_aligned_width_and_height = NULL;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800104 LINK_adreno_isUBWCSupportedByGpu = NULL;
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800105
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800106 libadreno_utils = ::dlopen("libadreno_utils.so", RTLD_NOW);
107 if (libadreno_utils) {
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800108 *(void **)&LINK_adreno_compute_aligned_width_and_height =
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700109 ::dlsym(libadreno_utils, "compute_aligned_width_and_height");
110 *(void **)&LINK_adreno_compute_padding =
111 ::dlsym(libadreno_utils, "compute_surface_padding");
112 *(void **)&LINK_adreno_isMacroTilingSupportedByGpu =
113 ::dlsym(libadreno_utils, "isMacroTilingSupportedByGpu");
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800114 *(void **)&LINK_adreno_compute_compressedfmt_aligned_width_and_height =
115 ::dlsym(libadreno_utils,
116 "compute_compressedfmt_aligned_width_and_height");
Sushil Chauhan082acd62015-01-14 16:49:29 -0800117 *(void **)&LINK_adreno_isUBWCSupportedByGpu =
118 ::dlsym(libadreno_utils, "isUBWCSupportedByGpu");
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800119 }
120}
121
122AdrenoMemInfo::~AdrenoMemInfo()
123{
124 if (libadreno_utils) {
125 ::dlclose(libadreno_utils);
126 }
127}
128
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700129int AdrenoMemInfo::isMacroTilingSupportedByGPU()
130{
131 if ((libadreno_utils)) {
132 if(LINK_adreno_isMacroTilingSupportedByGpu) {
133 return LINK_adreno_isMacroTilingSupportedByGpu();
134 }
135 }
136 return 0;
137}
138
139
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800140void AdrenoMemInfo::getAlignedWidthAndHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800141 int usage, int& aligned_w, int& aligned_h)
Naomi Luisa44100c2013-02-08 12:42:03 -0800142{
Sushil Chauhan65e26302015-01-14 10:48:57 -0800143
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800144 // Currently surface padding is only computed for RGB* surfaces.
Jesse Hallfbe96d22013-09-20 01:39:43 -0700145 if (format <= HAL_PIXEL_FORMAT_sRGB_X_8888) {
Sushil Chauhan65e26302015-01-14 10:48:57 -0800146 int tileEnabled = isMacroTileEnabled(format, usage);
147 AdrenoMemInfo::getInstance().getGpuAlignedWidthHeight(width,
148 height, format, tileEnabled, aligned_w, aligned_h);
149 return;
Naomi Luisa44100c2013-02-08 12:42:03 -0800150 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800151
152 if (isUBwcEnabled(format, usage)) {
153 getUBwcWidthAndHeight(width, height, format, aligned_w, aligned_h);
154 return;
155 }
156
157 aligned_w = width;
158 aligned_h = height;
159 switch (format)
160 {
161 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
162 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
163 case HAL_PIXEL_FORMAT_RAW_SENSOR:
164 aligned_w = ALIGN(width, 32);
165 break;
166 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
167 aligned_w = ALIGN(width, 128);
168 break;
169 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
170 case HAL_PIXEL_FORMAT_YV12:
171 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
172 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
173 case HAL_PIXEL_FORMAT_YCbCr_422_I:
174 case HAL_PIXEL_FORMAT_YCrCb_422_I:
175 aligned_w = ALIGN(width, 16);
176 break;
177 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
178 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
179 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
180 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
181 break;
182 case HAL_PIXEL_FORMAT_BLOB:
183 break;
184 case HAL_PIXEL_FORMAT_NV21_ZSL:
185 aligned_w = ALIGN(width, 64);
186 aligned_h = ALIGN(height, 64);
187 break;
188 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
189 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
190 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
191 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
192 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
193 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
194 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
195 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
196 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
197 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
198 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
199 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
200 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
201 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
202 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
203 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
204 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
205 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
206 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
207 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
208 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
209 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
210 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
211 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
212 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
213 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
214 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
215 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
216 if(LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
217 int bytesPerPixel = 0;
218 int raster_mode = 0; //Adreno unknown raster mode.
219 int padding_threshold = 512; //Threshold for padding
220 //surfaces.
221
222 LINK_adreno_compute_compressedfmt_aligned_width_and_height(
223 width, height, format, 0,raster_mode, padding_threshold,
224 &aligned_w, &aligned_h, &bytesPerPixel);
225 } else {
226 ALOGW("%s: Warning!! Symbols" \
227 " compute_compressedfmt_aligned_width_and_height" \
228 " not found", __FUNCTION__);
229 }
230 break;
231 default: break;
232 }
233}
234
235void AdrenoMemInfo::getGpuAlignedWidthHeight(int width, int height, int format,
236 int tile_enabled, int& aligned_w, int& aligned_h)
237{
238 aligned_w = ALIGN(width, 32);
239 aligned_h = ALIGN(height, 32);
240
241 // Don't add any additional padding if debug.gralloc.map_fb_memory
242 // is enabled
243 char property[PROPERTY_VALUE_MAX];
244 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
245 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
246 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
247 return;
248 }
249
250 int bpp = 4;
251 switch(format)
252 {
253 case HAL_PIXEL_FORMAT_RGB_888:
254 bpp = 3;
255 break;
256 case HAL_PIXEL_FORMAT_RGB_565:
257 case HAL_PIXEL_FORMAT_RGBA_5551:
258 case HAL_PIXEL_FORMAT_RGBA_4444:
259 bpp = 2;
260 break;
261 default: break;
262 }
263
264 if (libadreno_utils) {
265 int raster_mode = 0; // Adreno unknown raster mode.
266 int padding_threshold = 512; // Threshold for padding surfaces.
267 // the function below computes aligned width and aligned height
268 // based on linear or macro tile mode selected.
269 if(LINK_adreno_compute_aligned_width_and_height) {
270 LINK_adreno_compute_aligned_width_and_height(width,
271 height, bpp, tile_enabled,
272 raster_mode, padding_threshold,
273 &aligned_w, &aligned_h);
274
275 } else if(LINK_adreno_compute_padding) {
276 int surface_tile_height = 1; // Linear surface
277 aligned_w = LINK_adreno_compute_padding(width, bpp,
278 surface_tile_height, raster_mode,
279 padding_threshold);
280 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
281 __FUNCTION__);
282 } else {
283 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
284 "compute_aligned_width_and_height not found", __FUNCTION__);
285 }
286 }
287}
288
289int AdrenoMemInfo::isUBWCSupportedByGPU(int format)
290{
Sushil Chauhan082acd62015-01-14 16:49:29 -0800291 if (libadreno_utils) {
292 if (LINK_adreno_isUBWCSupportedByGpu) {
293 ADRENOPIXELFORMAT gpu_format = getGpuPixelFormat(format);
294 return LINK_adreno_isUBWCSupportedByGpu(gpu_format);
295 }
296 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800297 return 0;
Naomi Luisa44100c2013-02-08 12:42:03 -0800298}
299
Sushil Chauhan082acd62015-01-14 16:49:29 -0800300ADRENOPIXELFORMAT AdrenoMemInfo::getGpuPixelFormat(int hal_format)
301{
302 switch (hal_format) {
303 case HAL_PIXEL_FORMAT_RGBA_8888:
304 return ADRENO_PIXELFORMAT_R8G8B8A8;
305 case HAL_PIXEL_FORMAT_RGB_565:
306 return ADRENO_PIXELFORMAT_B5G6R5;
307 case HAL_PIXEL_FORMAT_sRGB_A_8888:
308 return ADRENO_PIXELFORMAT_R8G8B8A8_SRGB;
309 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
310 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
311 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
312 return ADRENO_PIXELFORMAT_NV12;
313 default:
314 ALOGE("%s: No map for format: 0x%x", __FUNCTION__, hal_format);
315 break;
316 }
317 return ADRENO_PIXELFORMAT_UNKNOWN;
318}
319
Naomi Luisa44100c2013-02-08 12:42:03 -0800320//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700321IAllocController* IAllocController::sController = NULL;
322IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700323{
324 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700325 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700326 }
327 return sController;
328}
329
330
331//-------------- IonController-----------------------//
332IonController::IonController()
333{
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530334 allocateIonMem();
335}
336
337void IonController::allocateIonMem()
338{
339 mIonAlloc = new IonAlloc();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700340}
341
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700342int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700343{
344 int ionFlags = 0;
345 int ret;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700346
347 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700348 data.allocType = 0;
349
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500350 if(usage & GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700351 ionFlags |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700352
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500353 if(usage & GRALLOC_USAGE_PRIVATE_IOMMU_HEAP)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700354 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
355
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530356 if(usage & GRALLOC_USAGE_PROTECTED) {
Prabhanjan Kandulae8f4bec2013-10-24 16:32:51 +0530357 if (usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500358 ionFlags |= ION_HEAP(ION_CP_MM_HEAP_ID);
359 ionFlags |= ION_SECURE;
Shalaj Jain13cdf812014-12-02 16:20:54 -0800360#ifdef ION_FLAG_ALLOW_NON_CONTIG
361 if (!(usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY)) {
362 ionFlags |= ION_FLAG_ALLOW_NON_CONTIG;
363 }
364#endif
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530365 } else {
366 // for targets/OEMs which do not need HW level protection
367 // do not set ion secure flag & MM heap. Fallback to IOMMU heap.
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500368 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
Justin Philipd6166602014-08-12 13:42:21 +0530369 data.allocType |= private_handle_t::PRIV_FLAGS_PROTECTED_BUFFER;
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500370 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530371 } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
372 //MM Heap is exclusively a secure heap.
373 //If it is used for non secure cases, fallback to IOMMU heap
374 ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
375 cannot be used as an insecure heap!\
376 trying to use IOMMU instead !!");
377 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500378 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700379
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700380 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
381 ionFlags |= ION_HEAP(ION_CAMERA_HEAP_ID);
382
Arun Kumar K.R0daaa992013-03-12 15:08:29 -0700383 if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
384 ionFlags |= ION_HEAP(ION_ADSP_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700385
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530386 if(ionFlags & ION_SECURE)
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500387 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700388
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700389 // if no flags are set, default to
390 // SF + IOMMU heaps, so that bypass can work
391 // we can fall back to system heap if
392 // we run out.
393 if(!ionFlags)
394 ionFlags = ION_HEAP(ION_SF_HEAP_ID) | ION_HEAP(ION_IOMMU_HEAP_ID);
395
396 data.flags = ionFlags;
397 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700398
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700399 // Fallback
Naseer Ahmed29a26812012-06-14 00:56:20 -0700400 if(ret < 0 && canFallback(usage,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700401 (ionFlags & ION_SYSTEM_HEAP_ID)))
402 {
403 ALOGW("Falling back to system heap");
404 data.flags = ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700405 ret = mIonAlloc->alloc_buffer(data);
406 }
407
408 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700409 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
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;
485
486 // adreno formats
487 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
488 size = ALIGN(alignedw*alignedh, 4096);
489 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
490 break;
491 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
492 // The chroma plane is subsampled,
493 // but the pitch in bytes is unchanged
494 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700495 size = ALIGN( alignedw * alignedh, 8192);
496 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
497 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700498 case HAL_PIXEL_FORMAT_YV12:
499 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
500 ALOGE("w or h is odd for the YV12 format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800501 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700502 }
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400503 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700504 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700505 size = ALIGN(size, (unsigned int)4096);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700506 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800507 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
508 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naseer Ahmed2c215292013-09-18 23:47:42 -0400509 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800510 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700511 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
512 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700513 case HAL_PIXEL_FORMAT_YCbCr_422_I:
514 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700515 if(width & 1) {
516 ALOGE("width is odd for the YUV422_SP format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800517 return 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700518 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700519 size = ALIGN(alignedw * alignedh * 2, 4096);
520 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700521 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400522 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhane8a01792012-11-01 16:25:45 -0700523 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700524 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400525 case HAL_PIXEL_FORMAT_BLOB:
526 if(height != 1) {
527 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
528 must have height==1 ", __FUNCTION__);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800529 return 0;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400530 }
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400531 size = width;
532 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700533 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700534 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
535 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500536 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
537 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
538 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
539 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
540 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
541 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
542 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
543 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
544 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
545 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
546 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
547 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
548 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
549 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
550 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
551 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
552 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
553 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
554 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
555 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
556 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
557 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
558 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
559 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
560 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
561 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
562 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Jeykumar Sankaran8f4585f2014-02-05 15:23:40 -0800563 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500564 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
565 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700566 default:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800567 ALOGE("Unrecognized pixel format: 0x%x", __FUNCTION__, format);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800568 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700569 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700570 return size;
571}
572
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700573unsigned int getBufferSizeAndDimensions(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700574 int& alignedw, int &alignedh)
575{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700576 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700577
578 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
579 height,
580 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800581 0,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700582 alignedw,
583 alignedh);
584
Sushil Chauhan65e26302015-01-14 10:48:57 -0800585 size = getSize(format, width, height, 0 /* usage */, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700586
587 return size;
588}
589
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700590
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700591unsigned int getBufferSizeAndDimensions(int width, int height, int format,
592 int usage, int& alignedw, int &alignedh)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700593{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700594 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700595
596 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
597 height,
598 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800599 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700600 alignedw,
601 alignedh);
602
Sushil Chauhan65e26302015-01-14 10:48:57 -0800603 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700604
605 return size;
606}
607
608
609void getBufferAttributes(int width, int height, int format, int usage,
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700610 int& alignedw, int &alignedh, int& tileEnabled, unsigned int& size)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700611{
612 tileEnabled = isMacroTileEnabled(format, usage);
613
614 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
615 height,
616 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800617 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700618 alignedw,
619 alignedh);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800620 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700621}
622
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400623int getYUVPlaneInfo(private_handle_t* hnd, struct android_ycbcr* ycbcr)
624{
625 int err = 0;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700626 unsigned int ystride, cstride;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400627 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
628
629 // Get the chroma offsets from the handle width/height. We take advantage
630 // of the fact the width _is_ the stride
631 switch (hnd->format) {
632 //Semiplanar
633 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
634 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
635 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
636 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: //Same as YCbCr_420_SP_VENUS
Naseer Ahmeda28d31c2014-05-29 15:41:34 -0400637 ystride = cstride = hnd->width;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400638 ycbcr->y = (void*)hnd->base;
639 ycbcr->cb = (void*)(hnd->base + ystride * hnd->height);
640 ycbcr->cr = (void*)(hnd->base + ystride * hnd->height + 1);
641 ycbcr->ystride = ystride;
642 ycbcr->cstride = cstride;
643 ycbcr->chroma_step = 2;
644 break;
645
646 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
647 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
648 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
649 case HAL_PIXEL_FORMAT_NV21_ZSL:
650 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Naseer Ahmeda28d31c2014-05-29 15:41:34 -0400651 ystride = cstride = hnd->width;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400652 ycbcr->y = (void*)hnd->base;
653 ycbcr->cr = (void*)(hnd->base + ystride * hnd->height);
654 ycbcr->cb = (void*)(hnd->base + ystride * hnd->height + 1);
655 ycbcr->ystride = ystride;
656 ycbcr->cstride = cstride;
657 ycbcr->chroma_step = 2;
658 break;
659
660 //Planar
661 case HAL_PIXEL_FORMAT_YV12:
662 ystride = hnd->width;
Arun Kumar K.R6e332482014-08-21 10:57:37 -0700663 cstride = ALIGN(hnd->width/2, 16);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400664 ycbcr->y = (void*)hnd->base;
665 ycbcr->cr = (void*)(hnd->base + ystride * hnd->height);
666 ycbcr->cb = (void*)(hnd->base + ystride * hnd->height +
667 cstride * hnd->height/2);
668 ycbcr->ystride = ystride;
669 ycbcr->cstride = cstride;
670 ycbcr->chroma_step = 1;
671
672 break;
673 //Unsupported formats
674 case HAL_PIXEL_FORMAT_YCbCr_422_I:
675 case HAL_PIXEL_FORMAT_YCrCb_422_I:
676 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
677 default:
678 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__,
679 hnd->format);
680 err = -EINVAL;
681 }
682 return err;
683
684}
685
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700686
687
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700688// Allocate buffer from width, height and format into a
689// private_handle_t. It is the responsibility of the caller
690// to free the buffer using the free_buffer function
691int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
692{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700693 alloc_data data;
694 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700695 gralloc::IAllocController* sAlloc =
696 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700697 data.base = 0;
698 data.fd = -1;
699 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700700 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
701 alignedh);
702
Naseer Ahmed29a26812012-06-14 00:56:20 -0700703 data.align = getpagesize();
704 data.uncached = useUncached(usage);
705 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700706
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700707 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700708 if (0 != err) {
709 ALOGE("%s: allocate failed", __FUNCTION__);
710 return -ENOMEM;
711 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700712
Naseer Ahmed29a26812012-06-14 00:56:20 -0700713 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700714 data.allocType, 0, format,
715 alignedw, alignedh);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700716 hnd->base = (uint64_t) data.base;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700717 hnd->offset = data.offset;
718 hnd->gpuaddr = 0;
719 *pHnd = hnd;
720 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700721}
722
723void free_buffer(private_handle_t *hnd)
724{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700725 gralloc::IAllocController* sAlloc =
726 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700727 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700728 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700729 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
730 }
731 if(hnd)
732 delete hnd;
733
734}
Sushil Chauhan65e26302015-01-14 10:48:57 -0800735
736// UBWC helper functions
737static bool isUBwcFormat(int format)
738{
739 // Explicitly defined UBWC formats
740 switch(format)
741 {
742 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
743 return true;
744 default:
745 return false;
746 }
747}
748
749static bool isUBwcSupported(int format)
750{
751 // Existing HAL formats with UBWC support
752 switch(format)
753 {
754 case HAL_PIXEL_FORMAT_RGB_565:
755 case HAL_PIXEL_FORMAT_RGBA_8888:
756 case HAL_PIXEL_FORMAT_sRGB_A_8888:
757 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
758 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
759 return true;
760 default:
761 return false;
762 }
763}
764
765bool isUBwcEnabled(int format, int usage)
766{
767 if (isUBwcFormat(format) ||
768 ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) && isUBwcSupported(format)))
769 {
770 // Allow UBWC, only if GPU supports it and CPU usage flags are not set
771 if (AdrenoMemInfo::getInstance().isUBWCSupportedByGPU(format) &&
772 !(usage & (GRALLOC_USAGE_SW_READ_MASK |
773 GRALLOC_USAGE_SW_WRITE_MASK))) {
774 return true;
775 }
776 }
777 return false;
778}
779
780static void getUBwcWidthAndHeight(int width, int height, int format,
781 int& aligned_w, int& aligned_h)
782{
783 switch (format)
784 {
785 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
786 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
787 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
788 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
789 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
790 break;
791 default:
792 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
793 aligned_w = 0;
794 aligned_h = 0;
795 break;
796 }
797}
798
799static void getUBwcBlockSize(int bpp, int& block_width, int& block_height)
800{
801 block_width = 0;
802 block_height = 0;
803
804 switch(bpp)
805 {
806 case 2:
807 case 4:
808 block_width = 16;
809 block_height = 4;
810 break;
811 case 8:
812 block_width = 8;
813 block_height = 4;
814 break;
815 case 16:
816 block_width = 4;
817 block_height = 4;
818 break;
819 default:
820 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
821 break;
822 }
823}
824
825static unsigned int getUBwcMetaBufferSize(int width, int height, int bpp)
826{
827 unsigned int size = 0;
828 int meta_width, meta_height;
829 int block_width, block_height;
830
831 getUBwcBlockSize(bpp, block_width, block_height);
832
833 if (!block_width || !block_height) {
834 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
835 return size;
836 }
837
838 // Align meta buffer height to 16 blocks
839 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
840
841 // Align meta buffer width to 64 blocks
842 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
843
844 // Align meta buffer size to 4K
845 size = ((meta_width * meta_height), 4096);
846 return size;
847}
848
849static unsigned int getUBwcSize(int width, int height, int format,
850 const int alignedw, const int alignedh) {
851
852 unsigned int size = 0;
853 switch (format) {
854 case HAL_PIXEL_FORMAT_RGB_565:
855 size = alignedw * alignedh * 2;
856 size += getUBwcMetaBufferSize(width, height, 2);
857 break;
858 case HAL_PIXEL_FORMAT_RGBA_8888:
859 case HAL_PIXEL_FORMAT_sRGB_A_8888:
860 size = alignedw * alignedh * 4;
861 size += getUBwcMetaBufferSize(width, height, 4);
862 break;
863 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
864 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
865 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
866 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
867 break;
868 default:
869 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
870 break;
871 }
872 return size;
873}