blob: cd6d5658b4b313c26e7cdc9b5b4bddedae11af15 [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
Naomi Luisa44100c2013-02-08 12:42:03 -080079//-------------- AdrenoMemInfo-----------------------//
Naomi Luis01f5c8e2013-02-11 12:46:24 -080080AdrenoMemInfo::AdrenoMemInfo()
81{
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -080082 LINK_adreno_compute_aligned_width_and_height = NULL;
83 LINK_adreno_compute_padding = NULL;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -070084 LINK_adreno_isMacroTilingSupportedByGpu = NULL;
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -080085 LINK_adreno_compute_compressedfmt_aligned_width_and_height = NULL;
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -080086
Naomi Luis01f5c8e2013-02-11 12:46:24 -080087 libadreno_utils = ::dlopen("libadreno_utils.so", RTLD_NOW);
88 if (libadreno_utils) {
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -080089 *(void **)&LINK_adreno_compute_aligned_width_and_height =
Manoj Kumar AVM8a220812013-10-10 11:46:06 -070090 ::dlsym(libadreno_utils, "compute_aligned_width_and_height");
91 *(void **)&LINK_adreno_compute_padding =
92 ::dlsym(libadreno_utils, "compute_surface_padding");
93 *(void **)&LINK_adreno_isMacroTilingSupportedByGpu =
94 ::dlsym(libadreno_utils, "isMacroTilingSupportedByGpu");
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -080095 *(void **)&LINK_adreno_compute_compressedfmt_aligned_width_and_height =
96 ::dlsym(libadreno_utils,
97 "compute_compressedfmt_aligned_width_and_height");
Naomi Luis01f5c8e2013-02-11 12:46:24 -080098 }
99}
100
101AdrenoMemInfo::~AdrenoMemInfo()
102{
103 if (libadreno_utils) {
104 ::dlclose(libadreno_utils);
105 }
106}
107
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700108int AdrenoMemInfo::isMacroTilingSupportedByGPU()
109{
110 if ((libadreno_utils)) {
111 if(LINK_adreno_isMacroTilingSupportedByGpu) {
112 return LINK_adreno_isMacroTilingSupportedByGpu();
113 }
114 }
115 return 0;
116}
117
118
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800119void AdrenoMemInfo::getAlignedWidthAndHeight(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700120 int tile_enabled, int& aligned_w, int& aligned_h)
Naomi Luisa44100c2013-02-08 12:42:03 -0800121{
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800122 aligned_w = width;
123 aligned_h = height;
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800124 // Currently surface padding is only computed for RGB* surfaces.
Jesse Hallfbe96d22013-09-20 01:39:43 -0700125 if (format <= HAL_PIXEL_FORMAT_sRGB_X_8888) {
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800126 aligned_w = ALIGN(width, 32);
127 aligned_h = ALIGN(height, 32);
Naomi Luis1b379692013-05-06 12:13:07 -0700128 // Don't add any additional padding if debug.gralloc.map_fb_memory
129 // is enabled
130 char property[PROPERTY_VALUE_MAX];
131 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
132 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
133 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800134 return;
Naomi Luis1b379692013-05-06 12:13:07 -0700135 }
136
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800137 int bpp = 4;
138 switch(format)
139 {
140 case HAL_PIXEL_FORMAT_RGB_888:
141 bpp = 3;
142 break;
143 case HAL_PIXEL_FORMAT_RGB_565:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700144 case HAL_PIXEL_FORMAT_RGBA_5551:
145 case HAL_PIXEL_FORMAT_RGBA_4444:
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800146 bpp = 2;
147 break;
148 default: break;
149 }
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800150 if (libadreno_utils) {
Naomi Luisb65b2342013-03-27 23:32:06 -0700151 int raster_mode = 0; // Adreno unknown raster mode.
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800152 int padding_threshold = 512; // Threshold for padding surfaces.
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800153 // the function below computes aligned width and aligned height
154 // based on linear or macro tile mode selected.
155 if(LINK_adreno_compute_aligned_width_and_height) {
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700156 LINK_adreno_compute_aligned_width_and_height(width,
157 height, bpp, tile_enabled,
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800158 raster_mode, padding_threshold,
159 &aligned_w, &aligned_h);
160
161 } else if(LINK_adreno_compute_padding) {
162 int surface_tile_height = 1; // Linear surface
163 aligned_w = LINK_adreno_compute_padding(width, bpp,
164 surface_tile_height, raster_mode,
165 padding_threshold);
166 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
167 __FUNCTION__);
168 } else {
169 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
170 "compute_aligned_width_and_height not found", __FUNCTION__);
171 }
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800172 }
173 } else {
174 switch (format)
175 {
Naseer Ahmed0415b732014-05-29 16:36:34 -0400176 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800177 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400178 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800179 aligned_w = ALIGN(width, 32);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800180 break;
181 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800182 aligned_w = ALIGN(width, 128);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800183 break;
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800184 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800185 case HAL_PIXEL_FORMAT_YV12:
186 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
187 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700188 case HAL_PIXEL_FORMAT_YCbCr_422_I:
189 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800190 aligned_w = ALIGN(width, 16);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800191 break;
192 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400193 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800194 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800195 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800196 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400197 case HAL_PIXEL_FORMAT_BLOB:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400198 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700199 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800200 aligned_w = ALIGN(width, 64);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800201 aligned_h = ALIGN(height, 64);
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700202 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500203 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
204 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500205 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
206 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500207 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
208 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500209 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
210 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500211 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
212 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500213 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
214 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500215 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
216 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500217 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
218 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500219 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
220 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500221 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
222 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500223 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
224 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500225 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
226 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500227 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
228 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500229 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
230 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800231 if(LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
232 int bytesPerPixel = 0;
233 int raster_mode = 0; //Adreno unknown raster mode.
234 int padding_threshold = 512; //Threshold for padding
235 //surfaces.
236
237 LINK_adreno_compute_compressedfmt_aligned_width_and_height(
238 width, height, format, 0,raster_mode, padding_threshold,
239 &aligned_w, &aligned_h, &bytesPerPixel);
240
241 } else {
242 ALOGW("%s: Warning!! Symbols" \
243 " compute_compressedfmt_aligned_width_and_height" \
244 " not found", __FUNCTION__);
245 }
Naseer Ahmed63326f42013-12-18 02:45:48 -0500246 break;
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800247 default: break;
248 }
Naomi Luisa44100c2013-02-08 12:42:03 -0800249 }
Naomi Luisa44100c2013-02-08 12:42:03 -0800250}
251
252//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700253IAllocController* IAllocController::sController = NULL;
254IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700255{
256 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700257 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700258 }
259 return sController;
260}
261
262
263//-------------- IonController-----------------------//
264IonController::IonController()
265{
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530266 allocateIonMem();
267}
268
269void IonController::allocateIonMem()
270{
271 mIonAlloc = new IonAlloc();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700272}
273
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700274int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700275{
276 int ionFlags = 0;
277 int ret;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700278
279 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700280 data.allocType = 0;
281
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700282 if(usage & GRALLOC_USAGE_PRIVATE_UI_CONTIG_HEAP)
283 ionFlags |= ION_HEAP(ION_SF_HEAP_ID);
284
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500285 if(usage & GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700286 ionFlags |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700287
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500288 if(usage & GRALLOC_USAGE_PRIVATE_IOMMU_HEAP)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700289 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
290
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530291 if(usage & GRALLOC_USAGE_PROTECTED) {
Prabhanjan Kandulae8f4bec2013-10-24 16:32:51 +0530292 if (usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500293 ionFlags |= ION_HEAP(ION_CP_MM_HEAP_ID);
294 ionFlags |= ION_SECURE;
Shalaj Jain13cdf812014-12-02 16:20:54 -0800295#ifdef ION_FLAG_ALLOW_NON_CONTIG
296 if (!(usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY)) {
297 ionFlags |= ION_FLAG_ALLOW_NON_CONTIG;
298 }
299#endif
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530300 } else {
301 // for targets/OEMs which do not need HW level protection
302 // do not set ion secure flag & MM heap. Fallback to IOMMU heap.
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500303 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
Justin Philipd6166602014-08-12 13:42:21 +0530304 data.allocType |= private_handle_t::PRIV_FLAGS_PROTECTED_BUFFER;
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500305 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530306 } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
307 //MM Heap is exclusively a secure heap.
308 //If it is used for non secure cases, fallback to IOMMU heap
309 ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
310 cannot be used as an insecure heap!\
311 trying to use IOMMU instead !!");
312 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500313 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700314
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700315 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
316 ionFlags |= ION_HEAP(ION_CAMERA_HEAP_ID);
317
Arun Kumar K.R0daaa992013-03-12 15:08:29 -0700318 if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
319 ionFlags |= ION_HEAP(ION_ADSP_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700320
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530321 if(ionFlags & ION_SECURE)
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500322 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700323
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700324 // if no flags are set, default to
325 // SF + IOMMU heaps, so that bypass can work
326 // we can fall back to system heap if
327 // we run out.
328 if(!ionFlags)
329 ionFlags = ION_HEAP(ION_SF_HEAP_ID) | ION_HEAP(ION_IOMMU_HEAP_ID);
330
331 data.flags = ionFlags;
332 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700333
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700334 // Fallback
Naseer Ahmed29a26812012-06-14 00:56:20 -0700335 if(ret < 0 && canFallback(usage,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700336 (ionFlags & ION_SYSTEM_HEAP_ID)))
337 {
338 ALOGW("Falling back to system heap");
339 data.flags = ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700340 ret = mIonAlloc->alloc_buffer(data);
341 }
342
343 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700344 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700345 }
346
347 return ret;
348}
349
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700350IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700351{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700352 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700353 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
354 memalloc = mIonAlloc;
355 } else {
356 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
357 }
358
359 return memalloc;
360}
361
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700362bool isMacroTileEnabled(int format, int usage)
363{
364 bool tileEnabled = false;
365
366 // Check whether GPU & MDSS supports MacroTiling feature
367 if(AdrenoMemInfo::getInstance().isMacroTilingSupportedByGPU() &&
368 qdutils::MDPVersion::getInstance().supportsMacroTile())
369 {
370 // check the format
371 switch(format)
372 {
373 case HAL_PIXEL_FORMAT_RGBA_8888:
374 case HAL_PIXEL_FORMAT_RGBX_8888:
375 case HAL_PIXEL_FORMAT_BGRA_8888:
Manoj Kumar AVM5a5529b2014-02-24 18:16:37 -0800376 case HAL_PIXEL_FORMAT_RGB_565:
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700377 {
378 tileEnabled = true;
379 // check the usage flags
380 if (usage & (GRALLOC_USAGE_SW_READ_MASK |
381 GRALLOC_USAGE_SW_WRITE_MASK)) {
382 // Application intends to use CPU for rendering
383 tileEnabled = false;
384 }
385 break;
386 }
387 default:
388 break;
389 }
390 }
391 return tileEnabled;
392}
393
394// helper function
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700395unsigned int getSize(int format, int width, int height, const int alignedw,
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800396 const int alignedh) {
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700397 unsigned int size = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700398
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700399 switch (format) {
400 case HAL_PIXEL_FORMAT_RGBA_8888:
401 case HAL_PIXEL_FORMAT_RGBX_8888:
402 case HAL_PIXEL_FORMAT_BGRA_8888:
Naseer Ahmed82fc4b72013-09-20 01:31:37 -0700403 case HAL_PIXEL_FORMAT_sRGB_A_8888:
Jesse Hallfbe96d22013-09-20 01:39:43 -0700404 case HAL_PIXEL_FORMAT_sRGB_X_8888:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700405 size = alignedw * alignedh * 4;
406 break;
407 case HAL_PIXEL_FORMAT_RGB_888:
408 size = alignedw * alignedh * 3;
409 break;
410 case HAL_PIXEL_FORMAT_RGB_565:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700411 case HAL_PIXEL_FORMAT_RGBA_5551:
412 case HAL_PIXEL_FORMAT_RGBA_4444:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400413 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700414 size = alignedw * alignedh * 2;
415 break;
416
417 // adreno formats
418 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
419 size = ALIGN(alignedw*alignedh, 4096);
420 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
421 break;
422 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
423 // The chroma plane is subsampled,
424 // but the pitch in bytes is unchanged
425 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700426 size = ALIGN( alignedw * alignedh, 8192);
427 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
428 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700429 case HAL_PIXEL_FORMAT_YV12:
430 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
431 ALOGE("w or h is odd for the YV12 format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800432 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700433 }
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400434 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700435 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700436 size = ALIGN(size, (unsigned int)4096);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700437 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800438 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
439 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naseer Ahmed2c215292013-09-18 23:47:42 -0400440 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800441 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700442 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
443 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700444 case HAL_PIXEL_FORMAT_YCbCr_422_I:
445 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700446 if(width & 1) {
447 ALOGE("width is odd for the YUV422_SP format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800448 return 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700449 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700450 size = ALIGN(alignedw * alignedh * 2, 4096);
451 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700452 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400453 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhane8a01792012-11-01 16:25:45 -0700454 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700455 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400456 case HAL_PIXEL_FORMAT_BLOB:
457 if(height != 1) {
458 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
459 must have height==1 ", __FUNCTION__);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800460 return 0;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400461 }
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400462 size = width;
463 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700464 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700465 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
466 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500467 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
468 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
469 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
470 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
471 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
472 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
473 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
474 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
475 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
476 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
477 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
478 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
479 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
480 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
481 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
482 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
483 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
484 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
485 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
486 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
487 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
488 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
489 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
490 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
491 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
492 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
493 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Jeykumar Sankaran8f4585f2014-02-05 15:23:40 -0800494 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500495 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
496 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700497 default:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700498 ALOGE("unrecognized pixel format: 0x%x", format);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800499 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700500 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700501 return size;
502}
503
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700504unsigned int getBufferSizeAndDimensions(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700505 int& alignedw, int &alignedh)
506{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700507 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700508
509 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
510 height,
511 format,
512 false,
513 alignedw,
514 alignedh);
515
516 size = getSize(format, width, height, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700517
518 return size;
519}
520
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700521
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700522unsigned int getBufferSizeAndDimensions(int width, int height, int format,
523 int usage, int& alignedw, int &alignedh)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700524{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700525 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700526 int tileEnabled = isMacroTileEnabled(format, usage);
527
528 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
529 height,
530 format,
531 tileEnabled,
532 alignedw,
533 alignedh);
534
535 size = getSize(format, width, height, alignedw, alignedh);
536
537 return size;
538}
539
540
541void getBufferAttributes(int width, int height, int format, int usage,
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700542 int& alignedw, int &alignedh, int& tileEnabled, unsigned int& size)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700543{
544 tileEnabled = isMacroTileEnabled(format, usage);
545
546 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
547 height,
548 format,
549 tileEnabled,
550 alignedw,
551 alignedh);
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800552 size = getSize(format, width, height, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700553}
554
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400555int getYUVPlaneInfo(private_handle_t* hnd, struct android_ycbcr* ycbcr)
556{
557 int err = 0;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700558 unsigned int ystride, cstride;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400559 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
560
561 // Get the chroma offsets from the handle width/height. We take advantage
562 // of the fact the width _is_ the stride
563 switch (hnd->format) {
564 //Semiplanar
565 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
566 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
567 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
568 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: //Same as YCbCr_420_SP_VENUS
Naseer Ahmeda28d31c2014-05-29 15:41:34 -0400569 ystride = cstride = hnd->width;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400570 ycbcr->y = (void*)hnd->base;
571 ycbcr->cb = (void*)(hnd->base + ystride * hnd->height);
572 ycbcr->cr = (void*)(hnd->base + ystride * hnd->height + 1);
573 ycbcr->ystride = ystride;
574 ycbcr->cstride = cstride;
575 ycbcr->chroma_step = 2;
576 break;
577
578 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
579 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
580 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
581 case HAL_PIXEL_FORMAT_NV21_ZSL:
582 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Naseer Ahmeda28d31c2014-05-29 15:41:34 -0400583 ystride = cstride = hnd->width;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400584 ycbcr->y = (void*)hnd->base;
585 ycbcr->cr = (void*)(hnd->base + ystride * hnd->height);
586 ycbcr->cb = (void*)(hnd->base + ystride * hnd->height + 1);
587 ycbcr->ystride = ystride;
588 ycbcr->cstride = cstride;
589 ycbcr->chroma_step = 2;
590 break;
591
592 //Planar
593 case HAL_PIXEL_FORMAT_YV12:
594 ystride = hnd->width;
Arun Kumar K.R6e332482014-08-21 10:57:37 -0700595 cstride = ALIGN(hnd->width/2, 16);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400596 ycbcr->y = (void*)hnd->base;
597 ycbcr->cr = (void*)(hnd->base + ystride * hnd->height);
598 ycbcr->cb = (void*)(hnd->base + ystride * hnd->height +
599 cstride * hnd->height/2);
600 ycbcr->ystride = ystride;
601 ycbcr->cstride = cstride;
602 ycbcr->chroma_step = 1;
603
604 break;
605 //Unsupported formats
606 case HAL_PIXEL_FORMAT_YCbCr_422_I:
607 case HAL_PIXEL_FORMAT_YCrCb_422_I:
608 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
609 default:
610 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__,
611 hnd->format);
612 err = -EINVAL;
613 }
614 return err;
615
616}
617
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700618
619
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700620// Allocate buffer from width, height and format into a
621// private_handle_t. It is the responsibility of the caller
622// to free the buffer using the free_buffer function
623int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
624{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700625 alloc_data data;
626 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700627 gralloc::IAllocController* sAlloc =
628 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700629 data.base = 0;
630 data.fd = -1;
631 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700632 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
633 alignedh);
634
Naseer Ahmed29a26812012-06-14 00:56:20 -0700635 data.align = getpagesize();
636 data.uncached = useUncached(usage);
637 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700638
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700639 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700640 if (0 != err) {
641 ALOGE("%s: allocate failed", __FUNCTION__);
642 return -ENOMEM;
643 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700644
Naseer Ahmed29a26812012-06-14 00:56:20 -0700645 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700646 data.allocType, 0, format,
647 alignedw, alignedh);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700648 hnd->base = (uint64_t) data.base;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700649 hnd->offset = data.offset;
650 hnd->gpuaddr = 0;
651 *pHnd = hnd;
652 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700653}
654
655void free_buffer(private_handle_t *hnd)
656{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700657 gralloc::IAllocController* sAlloc =
658 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700659 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700660 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700661 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
662 }
663 if(hnd)
664 delete hnd;
665
666}
Saurabh Shah9ff53a92014-09-17 16:40:44 -0700667
668bool useUncached(const int& usage) {
669 if(usage & GRALLOC_USAGE_PRIVATE_UNCACHED)
670 return true;
671
672 if(not (usage & (GRALLOC_USAGE_SW_WRITE_OFTEN |
673 GRALLOC_USAGE_SW_READ_OFTEN)))
674 return true;
675
676 return false;
677}