blob: a69e6eeac4b5eb6010d7cf1fba52e2f4c0e0339f [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
Iliyan Malchev202a77d2012-06-11 14:41:12 -070056//Common functions
Naseer Ahmed29a26812012-06-14 00:56:20 -070057static bool canFallback(int usage, bool triedSystem)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070058{
59 // Fallback to system heap when alloc fails unless
60 // 1. Composition type is MDP
61 // 2. Alloc from system heap was already tried
62 // 3. The heap type is requsted explicitly
63 // 4. The heap type is protected
64 // 5. The buffer is meant for external display only
65
Naseer Ahmeda87da602012-07-01 23:54:19 -070066 if(QCCompositionType::getInstance().getCompositionType() &
67 COMPOSITION_TYPE_MDP)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070068 return false;
69 if(triedSystem)
70 return false;
Sushil Chauhan7651a802013-01-08 16:08:09 -080071 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PROTECTED))
Iliyan Malchev202a77d2012-06-11 14:41:12 -070072 return false;
Naseer Ahmed4c588a22012-07-31 19:12:17 -070073 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY))
Iliyan Malchev202a77d2012-06-11 14:41:12 -070074 return false;
75 //Return true by default
76 return true;
77}
78
79static bool useUncached(int usage)
80{
Naseer Ahmed26867882013-08-21 15:16:24 -040081 if (usage & GRALLOC_USAGE_PRIVATE_UNCACHED)
82 return true;
83 if(((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY)
84 ||((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY))
Iliyan Malchev202a77d2012-06-11 14:41:12 -070085 return true;
86 return false;
87}
88
Naomi Luisa44100c2013-02-08 12:42:03 -080089//-------------- AdrenoMemInfo-----------------------//
Naomi Luis01f5c8e2013-02-11 12:46:24 -080090AdrenoMemInfo::AdrenoMemInfo()
91{
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -080092 LINK_adreno_compute_aligned_width_and_height = NULL;
93 LINK_adreno_compute_padding = NULL;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -070094 LINK_adreno_isMacroTilingSupportedByGpu = NULL;
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -080095 LINK_adreno_compute_compressedfmt_aligned_width_and_height = 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");
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800105 *(void **)&LINK_adreno_compute_compressedfmt_aligned_width_and_height =
106 ::dlsym(libadreno_utils,
107 "compute_compressedfmt_aligned_width_and_height");
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800108 }
109}
110
111AdrenoMemInfo::~AdrenoMemInfo()
112{
113 if (libadreno_utils) {
114 ::dlclose(libadreno_utils);
115 }
116}
117
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700118int AdrenoMemInfo::isMacroTilingSupportedByGPU()
119{
120 if ((libadreno_utils)) {
121 if(LINK_adreno_isMacroTilingSupportedByGpu) {
122 return LINK_adreno_isMacroTilingSupportedByGpu();
123 }
124 }
125 return 0;
126}
127
128
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800129void AdrenoMemInfo::getAlignedWidthAndHeight(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700130 int tile_enabled, int& aligned_w, int& aligned_h)
Naomi Luisa44100c2013-02-08 12:42:03 -0800131{
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800132 aligned_w = width;
133 aligned_h = height;
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800134 // Currently surface padding is only computed for RGB* surfaces.
Jesse Hallfbe96d22013-09-20 01:39:43 -0700135 if (format <= HAL_PIXEL_FORMAT_sRGB_X_8888) {
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800136 aligned_w = ALIGN(width, 32);
137 aligned_h = ALIGN(height, 32);
Naomi Luis1b379692013-05-06 12:13:07 -0700138 // Don't add any additional padding if debug.gralloc.map_fb_memory
139 // is enabled
140 char property[PROPERTY_VALUE_MAX];
141 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
142 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
143 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800144 return;
Naomi Luis1b379692013-05-06 12:13:07 -0700145 }
146
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800147 int bpp = 4;
148 switch(format)
149 {
150 case HAL_PIXEL_FORMAT_RGB_888:
151 bpp = 3;
152 break;
153 case HAL_PIXEL_FORMAT_RGB_565:
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800154 bpp = 2;
155 break;
156 default: break;
157 }
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800158 if (libadreno_utils) {
Naomi Luisb65b2342013-03-27 23:32:06 -0700159 int raster_mode = 0; // Adreno unknown raster mode.
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800160 int padding_threshold = 512; // Threshold for padding surfaces.
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800161 // the function below computes aligned width and aligned height
162 // based on linear or macro tile mode selected.
163 if(LINK_adreno_compute_aligned_width_and_height) {
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700164 LINK_adreno_compute_aligned_width_and_height(width,
165 height, bpp, tile_enabled,
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800166 raster_mode, padding_threshold,
167 &aligned_w, &aligned_h);
168
169 } else if(LINK_adreno_compute_padding) {
170 int surface_tile_height = 1; // Linear surface
171 aligned_w = LINK_adreno_compute_padding(width, bpp,
172 surface_tile_height, raster_mode,
173 padding_threshold);
174 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
175 __FUNCTION__);
176 } else {
177 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
178 "compute_aligned_width_and_height not found", __FUNCTION__);
179 }
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800180 }
181 } else {
182 switch (format)
183 {
184 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400185 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800186 aligned_w = ALIGN(width, 32);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800187 break;
188 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800189 aligned_w = ALIGN(width, 128);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800190 break;
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800191 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
192 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
193 case HAL_PIXEL_FORMAT_YV12:
194 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
195 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700196 case HAL_PIXEL_FORMAT_YCbCr_422_I:
197 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800198 aligned_w = ALIGN(width, 16);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800199 break;
200 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400201 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800202 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800203 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800204 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400205 case HAL_PIXEL_FORMAT_BLOB:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400206 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700207 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800208 aligned_w = ALIGN(width, 64);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800209 aligned_h = ALIGN(height, 64);
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700210 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500211 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
212 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500213 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
214 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500215 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
216 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500217 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
218 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500219 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
220 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500221 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
222 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500223 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
224 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500225 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
226 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500227 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
228 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500229 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
230 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500231 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
232 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500233 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
234 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500235 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
236 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500237 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
238 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800239 if(LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
240 int bytesPerPixel = 0;
241 int raster_mode = 0; //Adreno unknown raster mode.
242 int padding_threshold = 512; //Threshold for padding
243 //surfaces.
244
245 LINK_adreno_compute_compressedfmt_aligned_width_and_height(
246 width, height, format, 0,raster_mode, padding_threshold,
247 &aligned_w, &aligned_h, &bytesPerPixel);
248
249 } else {
250 ALOGW("%s: Warning!! Symbols" \
251 " compute_compressedfmt_aligned_width_and_height" \
252 " not found", __FUNCTION__);
253 }
Naseer Ahmed63326f42013-12-18 02:45:48 -0500254 break;
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800255 default: break;
256 }
Naomi Luisa44100c2013-02-08 12:42:03 -0800257 }
Naomi Luisa44100c2013-02-08 12:42:03 -0800258}
259
260//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700261IAllocController* IAllocController::sController = NULL;
262IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700263{
264 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700265 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700266 }
267 return sController;
268}
269
270
271//-------------- IonController-----------------------//
272IonController::IonController()
273{
274 mIonAlloc = new IonAlloc();
275}
276
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700277int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700278{
279 int ionFlags = 0;
280 int ret;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700281
282 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700283 data.allocType = 0;
284
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700285 if(usage & GRALLOC_USAGE_PRIVATE_UI_CONTIG_HEAP)
286 ionFlags |= ION_HEAP(ION_SF_HEAP_ID);
287
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500288 if(usage & GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700289 ionFlags |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700290
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500291 if(usage & GRALLOC_USAGE_PRIVATE_IOMMU_HEAP)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700292 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
293
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530294 if(usage & GRALLOC_USAGE_PROTECTED) {
Prabhanjan Kandulae8f4bec2013-10-24 16:32:51 +0530295 if (usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500296 ionFlags |= ION_HEAP(ION_CP_MM_HEAP_ID);
297 ionFlags |= ION_SECURE;
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530298 } else {
299 // for targets/OEMs which do not need HW level protection
300 // do not set ion secure flag & MM heap. Fallback to IOMMU heap.
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500301 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
302 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530303 } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
304 //MM Heap is exclusively a secure heap.
305 //If it is used for non secure cases, fallback to IOMMU heap
306 ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
307 cannot be used as an insecure heap!\
308 trying to use IOMMU instead !!");
309 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500310 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700311
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700312 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
313 ionFlags |= ION_HEAP(ION_CAMERA_HEAP_ID);
314
Arun Kumar K.R0daaa992013-03-12 15:08:29 -0700315 if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
316 ionFlags |= ION_HEAP(ION_ADSP_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700317
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530318 if(ionFlags & ION_SECURE)
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500319 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700320
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700321 // if no flags are set, default to
322 // SF + IOMMU heaps, so that bypass can work
323 // we can fall back to system heap if
324 // we run out.
325 if(!ionFlags)
326 ionFlags = ION_HEAP(ION_SF_HEAP_ID) | ION_HEAP(ION_IOMMU_HEAP_ID);
327
328 data.flags = ionFlags;
329 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700330
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700331 // Fallback
Naseer Ahmed29a26812012-06-14 00:56:20 -0700332 if(ret < 0 && canFallback(usage,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700333 (ionFlags & ION_SYSTEM_HEAP_ID)))
334 {
335 ALOGW("Falling back to system heap");
336 data.flags = ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700337 ret = mIonAlloc->alloc_buffer(data);
338 }
339
340 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700341 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700342 }
343
344 return ret;
345}
346
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700347IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700348{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700349 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700350 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
351 memalloc = mIonAlloc;
352 } else {
353 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
354 }
355
356 return memalloc;
357}
358
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700359bool isMacroTileEnabled(int format, int usage)
360{
361 bool tileEnabled = false;
362
363 // Check whether GPU & MDSS supports MacroTiling feature
364 if(AdrenoMemInfo::getInstance().isMacroTilingSupportedByGPU() &&
365 qdutils::MDPVersion::getInstance().supportsMacroTile())
366 {
367 // check the format
368 switch(format)
369 {
370 case HAL_PIXEL_FORMAT_RGBA_8888:
371 case HAL_PIXEL_FORMAT_RGBX_8888:
372 case HAL_PIXEL_FORMAT_BGRA_8888:
Manoj Kumar AVM5a5529b2014-02-24 18:16:37 -0800373 case HAL_PIXEL_FORMAT_RGB_565:
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700374 {
375 tileEnabled = true;
376 // check the usage flags
377 if (usage & (GRALLOC_USAGE_SW_READ_MASK |
378 GRALLOC_USAGE_SW_WRITE_MASK)) {
379 // Application intends to use CPU for rendering
380 tileEnabled = false;
381 }
382 break;
383 }
384 default:
385 break;
386 }
387 }
388 return tileEnabled;
389}
390
391// helper function
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800392size_t getSize(int format, int width, int height, const int alignedw,
393 const int alignedh) {
394 size_t size = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700395
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700396 switch (format) {
397 case HAL_PIXEL_FORMAT_RGBA_8888:
398 case HAL_PIXEL_FORMAT_RGBX_8888:
399 case HAL_PIXEL_FORMAT_BGRA_8888:
Naseer Ahmed82fc4b72013-09-20 01:31:37 -0700400 case HAL_PIXEL_FORMAT_sRGB_A_8888:
Jesse Hallfbe96d22013-09-20 01:39:43 -0700401 case HAL_PIXEL_FORMAT_sRGB_X_8888:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700402 size = alignedw * alignedh * 4;
403 break;
404 case HAL_PIXEL_FORMAT_RGB_888:
405 size = alignedw * alignedh * 3;
406 break;
407 case HAL_PIXEL_FORMAT_RGB_565:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400408 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700409 size = alignedw * alignedh * 2;
410 break;
411
412 // adreno formats
413 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
414 size = ALIGN(alignedw*alignedh, 4096);
415 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
416 break;
417 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
418 // The chroma plane is subsampled,
419 // but the pitch in bytes is unchanged
420 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700421 size = ALIGN( alignedw * alignedh, 8192);
422 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
423 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700424 case HAL_PIXEL_FORMAT_YV12:
425 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
426 ALOGE("w or h is odd for the YV12 format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800427 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700428 }
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400429 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700430 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800431 size = ALIGN(size, (size_t)4096);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700432 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800433 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
434 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naseer Ahmed2c215292013-09-18 23:47:42 -0400435 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800436 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700437 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
438 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700439 case HAL_PIXEL_FORMAT_YCbCr_422_I:
440 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700441 if(width & 1) {
442 ALOGE("width is odd for the YUV422_SP format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800443 return 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700444 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700445 size = ALIGN(alignedw * alignedh * 2, 4096);
446 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700447 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400448 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhane8a01792012-11-01 16:25:45 -0700449 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700450 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400451 case HAL_PIXEL_FORMAT_BLOB:
452 if(height != 1) {
453 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
454 must have height==1 ", __FUNCTION__);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800455 return 0;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400456 }
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400457 size = width;
458 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700459 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700460 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
461 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500462 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
463 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
464 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
465 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
466 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
467 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
468 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
469 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
470 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
471 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
472 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
473 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
474 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
475 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
476 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
477 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
478 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
479 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
480 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
481 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
482 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
483 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
484 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
485 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
486 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
487 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
488 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Jeykumar Sankaran8f4585f2014-02-05 15:23:40 -0800489 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500490 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
491 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700492 default:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700493 ALOGE("unrecognized pixel format: 0x%x", format);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800494 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700495 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700496 return size;
497}
498
499size_t getBufferSizeAndDimensions(int width, int height, int format,
500 int& alignedw, int &alignedh)
501{
502 size_t size;
503
504 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
505 height,
506 format,
507 false,
508 alignedw,
509 alignedh);
510
511 size = getSize(format, width, height, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700512
513 return size;
514}
515
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700516
517size_t getBufferSizeAndDimensions(int width, int height, int format, int usage,
518 int& alignedw, int &alignedh)
519{
520 size_t size;
521 int tileEnabled = isMacroTileEnabled(format, usage);
522
523 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
524 height,
525 format,
526 tileEnabled,
527 alignedw,
528 alignedh);
529
530 size = getSize(format, width, height, alignedw, alignedh);
531
532 return size;
533}
534
535
536void getBufferAttributes(int width, int height, int format, int usage,
537 int& alignedw, int &alignedh, int& tileEnabled, size_t& size)
538{
539 tileEnabled = isMacroTileEnabled(format, usage);
540
541 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
542 height,
543 format,
544 tileEnabled,
545 alignedw,
546 alignedh);
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800547 size = getSize(format, width, height, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700548}
549
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400550int getYUVPlaneInfo(private_handle_t* hnd, struct android_ycbcr* ycbcr)
551{
552 int err = 0;
553 size_t ystride, cstride;
554 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
555
556 // Get the chroma offsets from the handle width/height. We take advantage
557 // of the fact the width _is_ the stride
558 switch (hnd->format) {
559 //Semiplanar
560 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
561 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
562 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
563 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: //Same as YCbCr_420_SP_VENUS
564 ystride = hnd->width;
565 cstride = hnd->width/2;
566 ycbcr->y = (void*)hnd->base;
567 ycbcr->cb = (void*)(hnd->base + ystride * hnd->height);
568 ycbcr->cr = (void*)(hnd->base + ystride * hnd->height + 1);
569 ycbcr->ystride = ystride;
570 ycbcr->cstride = cstride;
571 ycbcr->chroma_step = 2;
572 break;
573
574 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
575 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
576 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
577 case HAL_PIXEL_FORMAT_NV21_ZSL:
578 case HAL_PIXEL_FORMAT_RAW_SENSOR:
579 ystride = hnd->width;
580 cstride = hnd->width/2;
581 ycbcr->y = (void*)hnd->base;
582 ycbcr->cr = (void*)(hnd->base + ystride * hnd->height);
583 ycbcr->cb = (void*)(hnd->base + ystride * hnd->height + 1);
584 ycbcr->ystride = ystride;
585 ycbcr->cstride = cstride;
586 ycbcr->chroma_step = 2;
587 break;
588
589 //Planar
590 case HAL_PIXEL_FORMAT_YV12:
591 ystride = hnd->width;
592 cstride = hnd->width/2;
593 ycbcr->y = (void*)hnd->base;
594 ycbcr->cr = (void*)(hnd->base + ystride * hnd->height);
595 ycbcr->cb = (void*)(hnd->base + ystride * hnd->height +
596 cstride * hnd->height/2);
597 ycbcr->ystride = ystride;
598 ycbcr->cstride = cstride;
599 ycbcr->chroma_step = 1;
600
601 break;
602 //Unsupported formats
603 case HAL_PIXEL_FORMAT_YCbCr_422_I:
604 case HAL_PIXEL_FORMAT_YCrCb_422_I:
605 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
606 default:
607 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__,
608 hnd->format);
609 err = -EINVAL;
610 }
611 return err;
612
613}
614
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700615
616
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700617// Allocate buffer from width, height and format into a
618// private_handle_t. It is the responsibility of the caller
619// to free the buffer using the free_buffer function
620int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
621{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700622 alloc_data data;
623 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700624 gralloc::IAllocController* sAlloc =
625 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700626 data.base = 0;
627 data.fd = -1;
628 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700629 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
630 alignedh);
631
Naseer Ahmed29a26812012-06-14 00:56:20 -0700632 data.align = getpagesize();
633 data.uncached = useUncached(usage);
634 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700635
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700636 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700637 if (0 != err) {
638 ALOGE("%s: allocate failed", __FUNCTION__);
639 return -ENOMEM;
640 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700641
Naseer Ahmed29a26812012-06-14 00:56:20 -0700642 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700643 data.allocType, 0, format,
644 alignedw, alignedh);
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800645 hnd->base = (uintptr_t) data.base;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700646 hnd->offset = data.offset;
647 hnd->gpuaddr = 0;
648 *pHnd = hnd;
649 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700650}
651
652void free_buffer(private_handle_t *hnd)
653{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700654 gralloc::IAllocController* sAlloc =
655 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700656 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700657 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700658 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
659 }
660 if(hnd)
661 delete hnd;
662
663}