blob: 5360554d5f36124f1f6c797a9267e28f77790e6e [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{
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800129 aligned_w = width;
130 aligned_h = height;
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) {
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800133 aligned_w = ALIGN(width, 32);
134 aligned_h = ALIGN(height, 32);
Naomi Luis1b379692013-05-06 12:13:07 -0700135 // Don't add any additional padding if debug.gralloc.map_fb_memory
136 // is enabled
137 char property[PROPERTY_VALUE_MAX];
138 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
139 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
140 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800141 return;
Naomi Luis1b379692013-05-06 12:13:07 -0700142 }
143
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800144 int bpp = 4;
145 switch(format)
146 {
147 case HAL_PIXEL_FORMAT_RGB_888:
148 bpp = 3;
149 break;
150 case HAL_PIXEL_FORMAT_RGB_565:
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800151 bpp = 2;
152 break;
153 default: break;
154 }
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800155 if (libadreno_utils) {
Naomi Luisb65b2342013-03-27 23:32:06 -0700156 int raster_mode = 0; // Adreno unknown raster mode.
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800157 int padding_threshold = 512; // Threshold for padding surfaces.
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800158 // the function below computes aligned width and aligned height
159 // based on linear or macro tile mode selected.
160 if(LINK_adreno_compute_aligned_width_and_height) {
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700161 LINK_adreno_compute_aligned_width_and_height(width,
162 height, bpp, tile_enabled,
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800163 raster_mode, padding_threshold,
164 &aligned_w, &aligned_h);
165
166 } else if(LINK_adreno_compute_padding) {
167 int surface_tile_height = 1; // Linear surface
168 aligned_w = LINK_adreno_compute_padding(width, bpp,
169 surface_tile_height, raster_mode,
170 padding_threshold);
171 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
172 __FUNCTION__);
173 } else {
174 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
175 "compute_aligned_width_and_height not found", __FUNCTION__);
176 }
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800177 }
178 } else {
179 switch (format)
180 {
181 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400182 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800183 aligned_w = ALIGN(width, 32);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800184 break;
185 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800186 aligned_w = ALIGN(width, 128);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800187 break;
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800188 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
189 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
190 case HAL_PIXEL_FORMAT_YV12:
191 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
192 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700193 case HAL_PIXEL_FORMAT_YCbCr_422_I:
194 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800195 aligned_w = ALIGN(width, 16);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800196 break;
197 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400198 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800199 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800200 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800201 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400202 case HAL_PIXEL_FORMAT_BLOB:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400203 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700204 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800205 aligned_w = ALIGN(width, 64);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800206 aligned_h = ALIGN(height, 64);
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700207 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500208 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
209 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
210 aligned_w = ASTC_IN_UNITS(width, 4);
211 aligned_h = ASTC_IN_UNITS(height, 4);
212 break;
213 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
214 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
215 aligned_w = ASTC_IN_UNITS(width, 5);
216 aligned_h = ASTC_IN_UNITS(height, 4);
217 break;
218 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
219 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
220 aligned_w = ASTC_IN_UNITS(width, 5);
221 aligned_h = ASTC_IN_UNITS(height, 5);
222 break;
223 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
224 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
225 aligned_w = ASTC_IN_UNITS(width, 6);
226 aligned_h = ASTC_IN_UNITS(height, 5);
227 break;
228 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
229 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
230 aligned_w = ASTC_IN_UNITS(width, 6);
231 aligned_h = ASTC_IN_UNITS(height, 6);
232 break;
233 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
234 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
235 aligned_w = ASTC_IN_UNITS(width, 8);
236 aligned_h = ASTC_IN_UNITS(height, 5);
237 break;
238 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
239 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
240 aligned_w = ASTC_IN_UNITS(width, 8);
241 aligned_h = ASTC_IN_UNITS(height, 6);
242 break;
243 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
244 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
245 aligned_w = ASTC_IN_UNITS(width, 8);
246 aligned_h = ASTC_IN_UNITS(height, 8);
247 break;
248 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
249 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
250 aligned_w = ASTC_IN_UNITS(width, 10);
251 aligned_h = ASTC_IN_UNITS(height, 5);
252 break;
253 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
254 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
255 aligned_w = ASTC_IN_UNITS(width, 10);
256 aligned_h = ASTC_IN_UNITS(height, 6);
257 break;
258 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
259 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
260 aligned_w = ASTC_IN_UNITS(width, 10);
261 aligned_h = ASTC_IN_UNITS(height, 8);
262 break;
263 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
264 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
265 aligned_w = ASTC_IN_UNITS(width, 10);
266 aligned_h = ASTC_IN_UNITS(height, 10);
267 break;
268 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
269 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
270 aligned_w = ASTC_IN_UNITS(width, 12);
271 aligned_h = ASTC_IN_UNITS(height, 10);
272 break;
273 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
274 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
275 aligned_w = ASTC_IN_UNITS(width, 12);
276 aligned_h = ASTC_IN_UNITS(height, 12);
277 break;
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800278 default: break;
279 }
Naomi Luisa44100c2013-02-08 12:42:03 -0800280 }
Naomi Luisa44100c2013-02-08 12:42:03 -0800281}
282
283//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700284IAllocController* IAllocController::sController = NULL;
285IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700286{
287 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700288 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700289 }
290 return sController;
291}
292
293
294//-------------- IonController-----------------------//
295IonController::IonController()
296{
297 mIonAlloc = new IonAlloc();
298}
299
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700300int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700301{
302 int ionFlags = 0;
303 int ret;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700304
305 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700306 data.allocType = 0;
307
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700308 if(usage & GRALLOC_USAGE_PRIVATE_UI_CONTIG_HEAP)
309 ionFlags |= ION_HEAP(ION_SF_HEAP_ID);
310
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500311 if(usage & GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700312 ionFlags |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700313
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500314 if(usage & GRALLOC_USAGE_PRIVATE_IOMMU_HEAP)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700315 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
316
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530317 if(usage & GRALLOC_USAGE_PROTECTED) {
Prabhanjan Kandulae8f4bec2013-10-24 16:32:51 +0530318 if (usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500319 ionFlags |= ION_HEAP(ION_CP_MM_HEAP_ID);
320 ionFlags |= ION_SECURE;
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530321 } else {
322 // for targets/OEMs which do not need HW level protection
323 // do not set ion secure flag & MM heap. Fallback to IOMMU heap.
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500324 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
325 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530326 } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
327 //MM Heap is exclusively a secure heap.
328 //If it is used for non secure cases, fallback to IOMMU heap
329 ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
330 cannot be used as an insecure heap!\
331 trying to use IOMMU instead !!");
332 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500333 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700334
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700335 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
336 ionFlags |= ION_HEAP(ION_CAMERA_HEAP_ID);
337
Arun Kumar K.R0daaa992013-03-12 15:08:29 -0700338 if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
339 ionFlags |= ION_HEAP(ION_ADSP_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700340
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530341 if(ionFlags & ION_SECURE)
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500342 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700343
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700344 // if no flags are set, default to
345 // SF + IOMMU heaps, so that bypass can work
346 // we can fall back to system heap if
347 // we run out.
348 if(!ionFlags)
349 ionFlags = ION_HEAP(ION_SF_HEAP_ID) | ION_HEAP(ION_IOMMU_HEAP_ID);
350
351 data.flags = ionFlags;
352 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700353
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700354 // Fallback
Naseer Ahmed29a26812012-06-14 00:56:20 -0700355 if(ret < 0 && canFallback(usage,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700356 (ionFlags & ION_SYSTEM_HEAP_ID)))
357 {
358 ALOGW("Falling back to system heap");
359 data.flags = ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700360 ret = mIonAlloc->alloc_buffer(data);
361 }
362
363 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700364 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700365 }
366
367 return ret;
368}
369
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700370IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700371{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700372 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700373 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
374 memalloc = mIonAlloc;
375 } else {
376 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
377 }
378
379 return memalloc;
380}
381
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700382bool isMacroTileEnabled(int format, int usage)
383{
384 bool tileEnabled = false;
385
386 // Check whether GPU & MDSS supports MacroTiling feature
387 if(AdrenoMemInfo::getInstance().isMacroTilingSupportedByGPU() &&
388 qdutils::MDPVersion::getInstance().supportsMacroTile())
389 {
390 // check the format
391 switch(format)
392 {
393 case HAL_PIXEL_FORMAT_RGBA_8888:
394 case HAL_PIXEL_FORMAT_RGBX_8888:
395 case HAL_PIXEL_FORMAT_BGRA_8888:
396 {
397 tileEnabled = true;
398 // check the usage flags
399 if (usage & (GRALLOC_USAGE_SW_READ_MASK |
400 GRALLOC_USAGE_SW_WRITE_MASK)) {
401 // Application intends to use CPU for rendering
402 tileEnabled = false;
403 }
404 break;
405 }
406 default:
407 break;
408 }
409 }
410 return tileEnabled;
411}
412
413// helper function
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800414size_t getSize(int format, int width, int height, const int alignedw,
415 const int alignedh) {
416 size_t size = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700417
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700418 switch (format) {
419 case HAL_PIXEL_FORMAT_RGBA_8888:
420 case HAL_PIXEL_FORMAT_RGBX_8888:
421 case HAL_PIXEL_FORMAT_BGRA_8888:
Naseer Ahmed82fc4b72013-09-20 01:31:37 -0700422 case HAL_PIXEL_FORMAT_sRGB_A_8888:
Jesse Hallfbe96d22013-09-20 01:39:43 -0700423 case HAL_PIXEL_FORMAT_sRGB_X_8888:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700424 size = alignedw * alignedh * 4;
425 break;
426 case HAL_PIXEL_FORMAT_RGB_888:
427 size = alignedw * alignedh * 3;
428 break;
429 case HAL_PIXEL_FORMAT_RGB_565:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400430 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700431 size = alignedw * alignedh * 2;
432 break;
433
434 // adreno formats
435 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
436 size = ALIGN(alignedw*alignedh, 4096);
437 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
438 break;
439 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
440 // The chroma plane is subsampled,
441 // but the pitch in bytes is unchanged
442 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700443 size = ALIGN( alignedw * alignedh, 8192);
444 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
445 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700446 case HAL_PIXEL_FORMAT_YV12:
447 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
448 ALOGE("w or h is odd for the YV12 format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800449 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700450 }
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400451 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700452 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700453 size = ALIGN(size, 4096);
454 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800455 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
456 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naseer Ahmed2c215292013-09-18 23:47:42 -0400457 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800458 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700459 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
460 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700461 case HAL_PIXEL_FORMAT_YCbCr_422_I:
462 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700463 if(width & 1) {
464 ALOGE("width is odd for the YUV422_SP format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800465 return 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700466 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700467 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 Chauhane8a01792012-11-01 16:25:45 -0700471 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700472 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400473 case HAL_PIXEL_FORMAT_BLOB:
474 if(height != 1) {
475 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
476 must have height==1 ", __FUNCTION__);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800477 return 0;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400478 }
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400479 size = width;
480 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700481 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700482 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
483 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500484 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
485 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
486 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
487 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
488 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
489 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
490 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
491 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
492 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
493 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
494 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
495 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
496 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
497 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
498 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
499 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
500 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
501 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
502 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
503 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
504 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
505 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
506 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
507 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
508 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
509 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
510 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
511 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
512 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700513 default:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700514 ALOGE("unrecognized pixel format: 0x%x", format);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800515 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700516 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700517 return size;
518}
519
520size_t getBufferSizeAndDimensions(int width, int height, int format,
521 int& alignedw, int &alignedh)
522{
523 size_t size;
524
525 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
526 height,
527 format,
528 false,
529 alignedw,
530 alignedh);
531
532 size = getSize(format, width, height, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700533
534 return size;
535}
536
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700537
538size_t getBufferSizeAndDimensions(int width, int height, int format, int usage,
539 int& alignedw, int &alignedh)
540{
541 size_t size;
542 int tileEnabled = isMacroTileEnabled(format, usage);
543
544 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
545 height,
546 format,
547 tileEnabled,
548 alignedw,
549 alignedh);
550
551 size = getSize(format, width, height, alignedw, alignedh);
552
553 return size;
554}
555
556
557void getBufferAttributes(int width, int height, int format, int usage,
558 int& alignedw, int &alignedh, int& tileEnabled, size_t& size)
559{
560 tileEnabled = isMacroTileEnabled(format, usage);
561
562 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
563 height,
564 format,
565 tileEnabled,
566 alignedw,
567 alignedh);
568
569 if(size)
570 size = getSize(format, width, height, alignedw, alignedh);
571}
572
573
574
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700575// Allocate buffer from width, height and format into a
576// private_handle_t. It is the responsibility of the caller
577// to free the buffer using the free_buffer function
578int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
579{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700580 alloc_data data;
581 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700582 gralloc::IAllocController* sAlloc =
583 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700584 data.base = 0;
585 data.fd = -1;
586 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700587 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
588 alignedh);
589
Naseer Ahmed29a26812012-06-14 00:56:20 -0700590 data.align = getpagesize();
591 data.uncached = useUncached(usage);
592 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700593
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700594 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700595 if (0 != err) {
596 ALOGE("%s: allocate failed", __FUNCTION__);
597 return -ENOMEM;
598 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700599
Naseer Ahmed29a26812012-06-14 00:56:20 -0700600 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700601 data.allocType, 0, format,
602 alignedw, alignedh);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700603 hnd->base = (int) data.base;
604 hnd->offset = data.offset;
605 hnd->gpuaddr = 0;
606 *pHnd = hnd;
607 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700608}
609
610void free_buffer(private_handle_t *hnd)
611{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700612 gralloc::IAllocController* sAlloc =
613 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700614 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700615 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700616 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
617 }
618 if(hnd)
619 delete hnd;
620
621}