blob: a2c2f1461974a93325e8c1e83cc012a1475c0526 [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
Duy Truong73d36df2013-02-09 20:33:23 -08002 * Copyright (c) 2011-2012, 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
50#define ASTC_IN_UNITS(n, unit_size) (((n) + (unit_size) -1) / (unit_size))
51
Iliyan Malchev202a77d2012-06-11 14:41:12 -070052using namespace gralloc;
Naseer Ahmeda87da602012-07-01 23:54:19 -070053using namespace qdutils;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070054
Naomi Luisa44100c2013-02-08 12:42:03 -080055ANDROID_SINGLETON_STATIC_INSTANCE(AdrenoMemInfo);
56
Iliyan Malchev202a77d2012-06-11 14:41:12 -070057//Common functions
Naseer Ahmed29a26812012-06-14 00:56:20 -070058static bool canFallback(int usage, bool triedSystem)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070059{
60 // Fallback to system heap when alloc fails unless
61 // 1. Composition type is MDP
62 // 2. Alloc from system heap was already tried
63 // 3. The heap type is requsted explicitly
64 // 4. The heap type is protected
65 // 5. The buffer is meant for external display only
66
Naseer Ahmeda87da602012-07-01 23:54:19 -070067 if(QCCompositionType::getInstance().getCompositionType() &
68 COMPOSITION_TYPE_MDP)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070069 return false;
70 if(triedSystem)
71 return false;
Sushil Chauhan7651a802013-01-08 16:08:09 -080072 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PROTECTED))
Iliyan Malchev202a77d2012-06-11 14:41:12 -070073 return false;
Naseer Ahmed4c588a22012-07-31 19:12:17 -070074 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY))
Iliyan Malchev202a77d2012-06-11 14:41:12 -070075 return false;
76 //Return true by default
77 return true;
78}
79
80static bool useUncached(int usage)
81{
Naseer Ahmed26867882013-08-21 15:16:24 -040082 if (usage & GRALLOC_USAGE_PRIVATE_UNCACHED)
83 return true;
84 if(((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY)
85 ||((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY))
Iliyan Malchev202a77d2012-06-11 14:41:12 -070086 return true;
87 return false;
88}
89
Naomi Luisa44100c2013-02-08 12:42:03 -080090//-------------- AdrenoMemInfo-----------------------//
Naomi Luis01f5c8e2013-02-11 12:46:24 -080091AdrenoMemInfo::AdrenoMemInfo()
92{
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -080093 LINK_adreno_compute_aligned_width_and_height = NULL;
94 LINK_adreno_compute_padding = NULL;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -070095 LINK_adreno_isMacroTilingSupportedByGpu = NULL;
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -080096
Naomi Luis01f5c8e2013-02-11 12:46:24 -080097 libadreno_utils = ::dlopen("libadreno_utils.so", RTLD_NOW);
98 if (libadreno_utils) {
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -080099 *(void **)&LINK_adreno_compute_aligned_width_and_height =
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700100 ::dlsym(libadreno_utils, "compute_aligned_width_and_height");
101 *(void **)&LINK_adreno_compute_padding =
102 ::dlsym(libadreno_utils, "compute_surface_padding");
103 *(void **)&LINK_adreno_isMacroTilingSupportedByGpu =
104 ::dlsym(libadreno_utils, "isMacroTilingSupportedByGpu");
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800105 }
106}
107
108AdrenoMemInfo::~AdrenoMemInfo()
109{
110 if (libadreno_utils) {
111 ::dlclose(libadreno_utils);
112 }
113}
114
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700115int AdrenoMemInfo::isMacroTilingSupportedByGPU()
116{
117 if ((libadreno_utils)) {
118 if(LINK_adreno_isMacroTilingSupportedByGpu) {
119 return LINK_adreno_isMacroTilingSupportedByGpu();
120 }
121 }
122 return 0;
123}
124
125
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800126void AdrenoMemInfo::getAlignedWidthAndHeight(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700127 int tile_enabled, int& aligned_w, int& aligned_h)
Naomi Luisa44100c2013-02-08 12:42:03 -0800128{
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800129 aligned_w = ALIGN(width, 32);
130 aligned_h = ALIGN(height, 32);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800131 // Currently surface padding is only computed for RGB* surfaces.
Jesse Hallfbe96d22013-09-20 01:39:43 -0700132 if (format <= HAL_PIXEL_FORMAT_sRGB_X_8888) {
Naomi Luis1b379692013-05-06 12:13:07 -0700133 // Don't add any additional padding if debug.gralloc.map_fb_memory
134 // is enabled
135 char property[PROPERTY_VALUE_MAX];
136 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
137 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
138 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800139 return;
Naomi Luis1b379692013-05-06 12:13:07 -0700140 }
141
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800142 int bpp = 4;
143 switch(format)
144 {
145 case HAL_PIXEL_FORMAT_RGB_888:
146 bpp = 3;
147 break;
148 case HAL_PIXEL_FORMAT_RGB_565:
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800149 bpp = 2;
150 break;
151 default: break;
152 }
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800153 if (libadreno_utils) {
Naomi Luisb65b2342013-03-27 23:32:06 -0700154 int raster_mode = 0; // Adreno unknown raster mode.
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800155 int padding_threshold = 512; // Threshold for padding surfaces.
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800156 // the function below computes aligned width and aligned height
157 // based on linear or macro tile mode selected.
158 if(LINK_adreno_compute_aligned_width_and_height) {
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700159 LINK_adreno_compute_aligned_width_and_height(width,
160 height, bpp, tile_enabled,
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800161 raster_mode, padding_threshold,
162 &aligned_w, &aligned_h);
163
164 } else if(LINK_adreno_compute_padding) {
165 int surface_tile_height = 1; // Linear surface
166 aligned_w = LINK_adreno_compute_padding(width, bpp,
167 surface_tile_height, raster_mode,
168 padding_threshold);
169 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
170 __FUNCTION__);
171 } else {
172 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
173 "compute_aligned_width_and_height not found", __FUNCTION__);
174 }
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800175 }
176 } else {
177 switch (format)
178 {
179 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400180 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800181 aligned_w = ALIGN(width, 32);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800182 break;
183 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800184 aligned_w = ALIGN(width, 128);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800185 break;
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800186 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
187 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
188 case HAL_PIXEL_FORMAT_YV12:
189 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
190 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700191 case HAL_PIXEL_FORMAT_YCbCr_422_I:
192 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800193 aligned_w = ALIGN(width, 16);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800194 break;
195 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400196 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800197 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800198 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400199 case HAL_PIXEL_FORMAT_BLOB:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800200 aligned_w = width;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400201 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700202 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800203 aligned_w = ALIGN(width, 64);
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700204 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500205 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
206 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
207 aligned_w = ASTC_IN_UNITS(width, 4);
208 aligned_h = ASTC_IN_UNITS(height, 4);
209 break;
210 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
211 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
212 aligned_w = ASTC_IN_UNITS(width, 5);
213 aligned_h = ASTC_IN_UNITS(height, 4);
214 break;
215 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
216 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
217 aligned_w = ASTC_IN_UNITS(width, 5);
218 aligned_h = ASTC_IN_UNITS(height, 5);
219 break;
220 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
221 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
222 aligned_w = ASTC_IN_UNITS(width, 6);
223 aligned_h = ASTC_IN_UNITS(height, 5);
224 break;
225 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
226 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
227 aligned_w = ASTC_IN_UNITS(width, 6);
228 aligned_h = ASTC_IN_UNITS(height, 6);
229 break;
230 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
231 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
232 aligned_w = ASTC_IN_UNITS(width, 8);
233 aligned_h = ASTC_IN_UNITS(height, 5);
234 break;
235 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
236 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
237 aligned_w = ASTC_IN_UNITS(width, 8);
238 aligned_h = ASTC_IN_UNITS(height, 6);
239 break;
240 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
241 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
242 aligned_w = ASTC_IN_UNITS(width, 8);
243 aligned_h = ASTC_IN_UNITS(height, 8);
244 break;
245 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
246 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
247 aligned_w = ASTC_IN_UNITS(width, 10);
248 aligned_h = ASTC_IN_UNITS(height, 5);
249 break;
250 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
251 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
252 aligned_w = ASTC_IN_UNITS(width, 10);
253 aligned_h = ASTC_IN_UNITS(height, 6);
254 break;
255 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
256 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
257 aligned_w = ASTC_IN_UNITS(width, 10);
258 aligned_h = ASTC_IN_UNITS(height, 8);
259 break;
260 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
261 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
262 aligned_w = ASTC_IN_UNITS(width, 10);
263 aligned_h = ASTC_IN_UNITS(height, 10);
264 break;
265 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
266 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
267 aligned_w = ASTC_IN_UNITS(width, 12);
268 aligned_h = ASTC_IN_UNITS(height, 10);
269 break;
270 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
271 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
272 aligned_w = ASTC_IN_UNITS(width, 12);
273 aligned_h = ASTC_IN_UNITS(height, 12);
274 break;
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800275 default: break;
276 }
Naomi Luisa44100c2013-02-08 12:42:03 -0800277 }
Naomi Luisa44100c2013-02-08 12:42:03 -0800278}
279
280//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700281IAllocController* IAllocController::sController = NULL;
282IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700283{
284 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700285 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700286 }
287 return sController;
288}
289
290
291//-------------- IonController-----------------------//
292IonController::IonController()
293{
294 mIonAlloc = new IonAlloc();
295}
296
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700297int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700298{
299 int ionFlags = 0;
300 int ret;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700301
302 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700303 data.allocType = 0;
304
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700305 if(usage & GRALLOC_USAGE_PRIVATE_UI_CONTIG_HEAP)
306 ionFlags |= ION_HEAP(ION_SF_HEAP_ID);
307
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500308 if(usage & GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700309 ionFlags |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700310
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500311 if(usage & GRALLOC_USAGE_PRIVATE_IOMMU_HEAP)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700312 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
313
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530314 if(usage & GRALLOC_USAGE_PROTECTED) {
Prabhanjan Kandulae8f4bec2013-10-24 16:32:51 +0530315 if (usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500316 ionFlags |= ION_HEAP(ION_CP_MM_HEAP_ID);
317 ionFlags |= ION_SECURE;
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530318 } else {
319 // for targets/OEMs which do not need HW level protection
320 // do not set ion secure flag & MM heap. Fallback to IOMMU heap.
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500321 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
322 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530323 } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
324 //MM Heap is exclusively a secure heap.
325 //If it is used for non secure cases, fallback to IOMMU heap
326 ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
327 cannot be used as an insecure heap!\
328 trying to use IOMMU instead !!");
329 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500330 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700331
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700332 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
333 ionFlags |= ION_HEAP(ION_CAMERA_HEAP_ID);
334
Arun Kumar K.R0daaa992013-03-12 15:08:29 -0700335 if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
336 ionFlags |= ION_HEAP(ION_ADSP_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700337
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530338 if(ionFlags & ION_SECURE)
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500339 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700340
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700341 // if no flags are set, default to
342 // SF + IOMMU heaps, so that bypass can work
343 // we can fall back to system heap if
344 // we run out.
345 if(!ionFlags)
346 ionFlags = ION_HEAP(ION_SF_HEAP_ID) | ION_HEAP(ION_IOMMU_HEAP_ID);
347
348 data.flags = ionFlags;
349 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700350
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700351 // Fallback
Naseer Ahmed29a26812012-06-14 00:56:20 -0700352 if(ret < 0 && canFallback(usage,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700353 (ionFlags & ION_SYSTEM_HEAP_ID)))
354 {
355 ALOGW("Falling back to system heap");
356 data.flags = ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700357 ret = mIonAlloc->alloc_buffer(data);
358 }
359
360 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700361 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700362 }
363
364 return ret;
365}
366
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700367IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700368{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700369 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700370 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
371 memalloc = mIonAlloc;
372 } else {
373 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
374 }
375
376 return memalloc;
377}
378
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700379bool isMacroTileEnabled(int format, int usage)
380{
381 bool tileEnabled = false;
382
383 // Check whether GPU & MDSS supports MacroTiling feature
384 if(AdrenoMemInfo::getInstance().isMacroTilingSupportedByGPU() &&
385 qdutils::MDPVersion::getInstance().supportsMacroTile())
386 {
387 // check the format
388 switch(format)
389 {
390 case HAL_PIXEL_FORMAT_RGBA_8888:
391 case HAL_PIXEL_FORMAT_RGBX_8888:
392 case HAL_PIXEL_FORMAT_BGRA_8888:
393 {
394 tileEnabled = true;
395 // check the usage flags
396 if (usage & (GRALLOC_USAGE_SW_READ_MASK |
397 GRALLOC_USAGE_SW_WRITE_MASK)) {
398 // Application intends to use CPU for rendering
399 tileEnabled = false;
400 }
401 break;
402 }
403 default:
404 break;
405 }
406 }
407 return tileEnabled;
408}
409
410// helper function
411size_t getSize(int format, int width, int height, int alignedw, int alignedh)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700412{
413 size_t size;
414
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700415 switch (format) {
416 case HAL_PIXEL_FORMAT_RGBA_8888:
417 case HAL_PIXEL_FORMAT_RGBX_8888:
418 case HAL_PIXEL_FORMAT_BGRA_8888:
Naseer Ahmed82fc4b72013-09-20 01:31:37 -0700419 case HAL_PIXEL_FORMAT_sRGB_A_8888:
Jesse Hallfbe96d22013-09-20 01:39:43 -0700420 case HAL_PIXEL_FORMAT_sRGB_X_8888:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700421 size = alignedw * alignedh * 4;
422 break;
423 case HAL_PIXEL_FORMAT_RGB_888:
424 size = alignedw * alignedh * 3;
425 break;
426 case HAL_PIXEL_FORMAT_RGB_565:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400427 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700428 size = alignedw * alignedh * 2;
429 break;
430
431 // adreno formats
432 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
433 size = ALIGN(alignedw*alignedh, 4096);
434 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
435 break;
436 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
437 // The chroma plane is subsampled,
438 // but the pitch in bytes is unchanged
439 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700440 size = ALIGN( alignedw * alignedh, 8192);
441 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
442 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700443 case HAL_PIXEL_FORMAT_YV12:
444 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
445 ALOGE("w or h is odd for the YV12 format");
446 return -EINVAL;
447 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700448 alignedh = height;
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400449 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700450 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700451 size = ALIGN(size, 4096);
452 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800453 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
454 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
455 alignedh = height;
Naseer Ahmed2c215292013-09-18 23:47:42 -0400456 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800457 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700458 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
459 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700460 case HAL_PIXEL_FORMAT_YCbCr_422_I:
461 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700462 if(width & 1) {
463 ALOGE("width is odd for the YUV422_SP format");
464 return -EINVAL;
465 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700466 alignedh = height;
467 size = ALIGN(alignedw * alignedh * 2, 4096);
468 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700469 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400470 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhan73dcce42012-11-15 14:25:19 -0800471 alignedh = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
Sushil Chauhane8a01792012-11-01 16:25:45 -0700472 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700473 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400474 case HAL_PIXEL_FORMAT_BLOB:
475 if(height != 1) {
476 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
477 must have height==1 ", __FUNCTION__);
478 return -EINVAL;
479 }
480 alignedh = height;
481 alignedw = width;
482 size = width;
483 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700484 case HAL_PIXEL_FORMAT_NV21_ZSL:
485 alignedh = ALIGN(height, 64);
486 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
487 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500488 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
489 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
490 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
491 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
492 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
493 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
494 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
495 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
496 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
497 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
498 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
499 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
500 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
501 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
502 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
503 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
504 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
505 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
506 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
507 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
508 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
509 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
510 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
511 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
512 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
513 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
514 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
515 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
516 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700517 default:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700518 ALOGE("unrecognized pixel format: 0x%x", format);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700519 return -EINVAL;
520 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700521 return size;
522}
523
524size_t getBufferSizeAndDimensions(int width, int height, int format,
525 int& alignedw, int &alignedh)
526{
527 size_t size;
528
529 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
530 height,
531 format,
532 false,
533 alignedw,
534 alignedh);
535
536 size = getSize(format, width, height, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700537
538 return size;
539}
540
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700541
542size_t getBufferSizeAndDimensions(int width, int height, int format, int usage,
543 int& alignedw, int &alignedh)
544{
545 size_t size;
546 int tileEnabled = isMacroTileEnabled(format, usage);
547
548 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
549 height,
550 format,
551 tileEnabled,
552 alignedw,
553 alignedh);
554
555 size = getSize(format, width, height, alignedw, alignedh);
556
557 return size;
558}
559
560
561void getBufferAttributes(int width, int height, int format, int usage,
562 int& alignedw, int &alignedh, int& tileEnabled, size_t& size)
563{
564 tileEnabled = isMacroTileEnabled(format, usage);
565
566 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
567 height,
568 format,
569 tileEnabled,
570 alignedw,
571 alignedh);
572
573 if(size)
574 size = getSize(format, width, height, alignedw, alignedh);
575}
576
577
578
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700579// Allocate buffer from width, height and format into a
580// private_handle_t. It is the responsibility of the caller
581// to free the buffer using the free_buffer function
582int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
583{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700584 alloc_data data;
585 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700586 gralloc::IAllocController* sAlloc =
587 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700588 data.base = 0;
589 data.fd = -1;
590 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700591 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
592 alignedh);
593
Naseer Ahmed29a26812012-06-14 00:56:20 -0700594 data.align = getpagesize();
595 data.uncached = useUncached(usage);
596 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700597
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700598 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700599 if (0 != err) {
600 ALOGE("%s: allocate failed", __FUNCTION__);
601 return -ENOMEM;
602 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700603
Naseer Ahmed29a26812012-06-14 00:56:20 -0700604 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700605 data.allocType, 0, format,
606 alignedw, alignedh);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700607 hnd->base = (int) data.base;
608 hnd->offset = data.offset;
609 hnd->gpuaddr = 0;
610 *pHnd = hnd;
611 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700612}
613
614void free_buffer(private_handle_t *hnd)
615{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700616 gralloc::IAllocController* sAlloc =
617 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700618 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700619 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700620 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
621 }
622 if(hnd)
623 delete hnd;
624
625}