blob: 596ca77394b9f111e6e378f36fc721854951c01b [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
Saurabh Shah1adcafe2014-12-19 10:05:41 -080079/* The default policy is to return cached buffers unless the client explicity
80 * sets the PRIVATE_UNCACHED flag or indicates that the buffer will be rarely
81 * read or written in software. Any combination with a _RARELY_ flag will be
82 * treated as uncached. */
83static bool useUncached(const int& usage) {
84 if((usage & GRALLOC_USAGE_PRIVATE_UNCACHED) or
85 ((usage & GRALLOC_USAGE_SW_WRITE_MASK) ==
86 GRALLOC_USAGE_SW_WRITE_RARELY) or
87 ((usage & GRALLOC_USAGE_SW_READ_MASK) ==
88 GRALLOC_USAGE_SW_READ_RARELY))
89 return true;
90
91 return false;
92}
93
Naomi Luisa44100c2013-02-08 12:42:03 -080094//-------------- AdrenoMemInfo-----------------------//
Naomi Luis01f5c8e2013-02-11 12:46:24 -080095AdrenoMemInfo::AdrenoMemInfo()
96{
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -080097 LINK_adreno_compute_aligned_width_and_height = NULL;
98 LINK_adreno_compute_padding = NULL;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -070099 LINK_adreno_isMacroTilingSupportedByGpu = NULL;
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800100 LINK_adreno_compute_compressedfmt_aligned_width_and_height = NULL;
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800101
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800102 libadreno_utils = ::dlopen("libadreno_utils.so", RTLD_NOW);
103 if (libadreno_utils) {
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800104 *(void **)&LINK_adreno_compute_aligned_width_and_height =
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700105 ::dlsym(libadreno_utils, "compute_aligned_width_and_height");
106 *(void **)&LINK_adreno_compute_padding =
107 ::dlsym(libadreno_utils, "compute_surface_padding");
108 *(void **)&LINK_adreno_isMacroTilingSupportedByGpu =
109 ::dlsym(libadreno_utils, "isMacroTilingSupportedByGpu");
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800110 *(void **)&LINK_adreno_compute_compressedfmt_aligned_width_and_height =
111 ::dlsym(libadreno_utils,
112 "compute_compressedfmt_aligned_width_and_height");
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800113 }
114}
115
116AdrenoMemInfo::~AdrenoMemInfo()
117{
118 if (libadreno_utils) {
119 ::dlclose(libadreno_utils);
120 }
121}
122
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700123int AdrenoMemInfo::isMacroTilingSupportedByGPU()
124{
125 if ((libadreno_utils)) {
126 if(LINK_adreno_isMacroTilingSupportedByGpu) {
127 return LINK_adreno_isMacroTilingSupportedByGpu();
128 }
129 }
130 return 0;
131}
132
133
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800134void AdrenoMemInfo::getAlignedWidthAndHeight(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700135 int tile_enabled, int& aligned_w, int& aligned_h)
Naomi Luisa44100c2013-02-08 12:42:03 -0800136{
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800137 aligned_w = width;
138 aligned_h = height;
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800139 // Currently surface padding is only computed for RGB* surfaces.
Jesse Hallfbe96d22013-09-20 01:39:43 -0700140 if (format <= HAL_PIXEL_FORMAT_sRGB_X_8888) {
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800141 aligned_w = ALIGN(width, 32);
142 aligned_h = ALIGN(height, 32);
Naomi Luis1b379692013-05-06 12:13:07 -0700143 // Don't add any additional padding if debug.gralloc.map_fb_memory
144 // is enabled
145 char property[PROPERTY_VALUE_MAX];
146 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
147 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
148 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800149 return;
Naomi Luis1b379692013-05-06 12:13:07 -0700150 }
151
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800152 int bpp = 4;
153 switch(format)
154 {
155 case HAL_PIXEL_FORMAT_RGB_888:
156 bpp = 3;
157 break;
158 case HAL_PIXEL_FORMAT_RGB_565:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700159 case HAL_PIXEL_FORMAT_RGBA_5551:
160 case HAL_PIXEL_FORMAT_RGBA_4444:
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800161 bpp = 2;
162 break;
163 default: break;
164 }
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800165 if (libadreno_utils) {
Naomi Luisb65b2342013-03-27 23:32:06 -0700166 int raster_mode = 0; // Adreno unknown raster mode.
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800167 int padding_threshold = 512; // Threshold for padding surfaces.
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800168 // the function below computes aligned width and aligned height
169 // based on linear or macro tile mode selected.
170 if(LINK_adreno_compute_aligned_width_and_height) {
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700171 LINK_adreno_compute_aligned_width_and_height(width,
172 height, bpp, tile_enabled,
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800173 raster_mode, padding_threshold,
174 &aligned_w, &aligned_h);
175
176 } else if(LINK_adreno_compute_padding) {
177 int surface_tile_height = 1; // Linear surface
178 aligned_w = LINK_adreno_compute_padding(width, bpp,
179 surface_tile_height, raster_mode,
180 padding_threshold);
181 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
182 __FUNCTION__);
183 } else {
184 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
185 "compute_aligned_width_and_height not found", __FUNCTION__);
186 }
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800187 }
188 } else {
189 switch (format)
190 {
Naseer Ahmed0415b732014-05-29 16:36:34 -0400191 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800192 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400193 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800194 aligned_w = ALIGN(width, 32);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800195 break;
196 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800197 aligned_w = ALIGN(width, 128);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800198 break;
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800199 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800200 case HAL_PIXEL_FORMAT_YV12:
201 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
202 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700203 case HAL_PIXEL_FORMAT_YCbCr_422_I:
204 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800205 aligned_w = ALIGN(width, 16);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800206 break;
207 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400208 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800209 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800210 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800211 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400212 case HAL_PIXEL_FORMAT_BLOB:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400213 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700214 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800215 aligned_w = ALIGN(width, 64);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800216 aligned_h = ALIGN(height, 64);
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700217 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500218 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
219 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500220 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
221 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500222 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
223 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500224 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
225 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500226 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
227 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500228 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
229 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500230 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
231 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500232 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
233 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500234 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
235 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500236 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
237 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500238 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
239 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500240 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
241 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500242 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
243 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500244 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
245 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800246 if(LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
247 int bytesPerPixel = 0;
248 int raster_mode = 0; //Adreno unknown raster mode.
249 int padding_threshold = 512; //Threshold for padding
250 //surfaces.
251
252 LINK_adreno_compute_compressedfmt_aligned_width_and_height(
253 width, height, format, 0,raster_mode, padding_threshold,
254 &aligned_w, &aligned_h, &bytesPerPixel);
255
256 } else {
257 ALOGW("%s: Warning!! Symbols" \
258 " compute_compressedfmt_aligned_width_and_height" \
259 " not found", __FUNCTION__);
260 }
Naseer Ahmed63326f42013-12-18 02:45:48 -0500261 break;
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800262 default: break;
263 }
Naomi Luisa44100c2013-02-08 12:42:03 -0800264 }
Naomi Luisa44100c2013-02-08 12:42:03 -0800265}
266
267//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700268IAllocController* IAllocController::sController = NULL;
269IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700270{
271 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700272 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700273 }
274 return sController;
275}
276
277
278//-------------- IonController-----------------------//
279IonController::IonController()
280{
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530281 allocateIonMem();
282}
283
284void IonController::allocateIonMem()
285{
286 mIonAlloc = new IonAlloc();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700287}
288
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700289int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700290{
291 int ionFlags = 0;
292 int ret;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700293
294 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700295 data.allocType = 0;
296
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500297 if(usage & GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700298 ionFlags |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700299
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500300 if(usage & GRALLOC_USAGE_PRIVATE_IOMMU_HEAP)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700301 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
302
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530303 if(usage & GRALLOC_USAGE_PROTECTED) {
Prabhanjan Kandulae8f4bec2013-10-24 16:32:51 +0530304 if (usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500305 ionFlags |= ION_HEAP(ION_CP_MM_HEAP_ID);
306 ionFlags |= ION_SECURE;
Shalaj Jain13cdf812014-12-02 16:20:54 -0800307#ifdef ION_FLAG_ALLOW_NON_CONTIG
308 if (!(usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY)) {
309 ionFlags |= ION_FLAG_ALLOW_NON_CONTIG;
310 }
311#endif
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530312 } else {
313 // for targets/OEMs which do not need HW level protection
314 // do not set ion secure flag & MM heap. Fallback to IOMMU heap.
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500315 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
Justin Philipd6166602014-08-12 13:42:21 +0530316 data.allocType |= private_handle_t::PRIV_FLAGS_PROTECTED_BUFFER;
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500317 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530318 } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
319 //MM Heap is exclusively a secure heap.
320 //If it is used for non secure cases, fallback to IOMMU heap
321 ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
322 cannot be used as an insecure heap!\
323 trying to use IOMMU instead !!");
324 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500325 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700326
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700327 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
328 ionFlags |= ION_HEAP(ION_CAMERA_HEAP_ID);
329
Arun Kumar K.R0daaa992013-03-12 15:08:29 -0700330 if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
331 ionFlags |= ION_HEAP(ION_ADSP_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700332
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530333 if(ionFlags & ION_SECURE)
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500334 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700335
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700336 // if no flags are set, default to
337 // SF + IOMMU heaps, so that bypass can work
338 // we can fall back to system heap if
339 // we run out.
340 if(!ionFlags)
341 ionFlags = ION_HEAP(ION_SF_HEAP_ID) | ION_HEAP(ION_IOMMU_HEAP_ID);
342
343 data.flags = ionFlags;
344 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700345
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700346 // Fallback
Naseer Ahmed29a26812012-06-14 00:56:20 -0700347 if(ret < 0 && canFallback(usage,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700348 (ionFlags & ION_SYSTEM_HEAP_ID)))
349 {
350 ALOGW("Falling back to system heap");
351 data.flags = ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700352 ret = mIonAlloc->alloc_buffer(data);
353 }
354
355 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700356 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700357 }
358
359 return ret;
360}
361
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700362IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700363{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700364 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700365 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
366 memalloc = mIonAlloc;
367 } else {
368 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
369 }
370
371 return memalloc;
372}
373
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700374bool isMacroTileEnabled(int format, int usage)
375{
376 bool tileEnabled = false;
377
378 // Check whether GPU & MDSS supports MacroTiling feature
379 if(AdrenoMemInfo::getInstance().isMacroTilingSupportedByGPU() &&
380 qdutils::MDPVersion::getInstance().supportsMacroTile())
381 {
382 // check the format
383 switch(format)
384 {
385 case HAL_PIXEL_FORMAT_RGBA_8888:
386 case HAL_PIXEL_FORMAT_RGBX_8888:
387 case HAL_PIXEL_FORMAT_BGRA_8888:
Manoj Kumar AVM5a5529b2014-02-24 18:16:37 -0800388 case HAL_PIXEL_FORMAT_RGB_565:
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700389 {
390 tileEnabled = true;
391 // check the usage flags
392 if (usage & (GRALLOC_USAGE_SW_READ_MASK |
393 GRALLOC_USAGE_SW_WRITE_MASK)) {
394 // Application intends to use CPU for rendering
395 tileEnabled = false;
396 }
397 break;
398 }
399 default:
400 break;
401 }
402 }
403 return tileEnabled;
404}
405
406// helper function
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700407unsigned int getSize(int format, int width, int height, const int alignedw,
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800408 const int alignedh) {
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700409 unsigned int size = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700410
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700411 switch (format) {
412 case HAL_PIXEL_FORMAT_RGBA_8888:
413 case HAL_PIXEL_FORMAT_RGBX_8888:
414 case HAL_PIXEL_FORMAT_BGRA_8888:
Naseer Ahmed82fc4b72013-09-20 01:31:37 -0700415 case HAL_PIXEL_FORMAT_sRGB_A_8888:
Jesse Hallfbe96d22013-09-20 01:39:43 -0700416 case HAL_PIXEL_FORMAT_sRGB_X_8888:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700417 size = alignedw * alignedh * 4;
418 break;
419 case HAL_PIXEL_FORMAT_RGB_888:
420 size = alignedw * alignedh * 3;
421 break;
422 case HAL_PIXEL_FORMAT_RGB_565:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700423 case HAL_PIXEL_FORMAT_RGBA_5551:
424 case HAL_PIXEL_FORMAT_RGBA_4444:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400425 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700426 size = alignedw * alignedh * 2;
427 break;
428
429 // adreno formats
430 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
431 size = ALIGN(alignedw*alignedh, 4096);
432 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
433 break;
434 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
435 // The chroma plane is subsampled,
436 // but the pitch in bytes is unchanged
437 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700438 size = ALIGN( alignedw * alignedh, 8192);
439 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
440 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700441 case HAL_PIXEL_FORMAT_YV12:
442 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
443 ALOGE("w or h is odd for the YV12 format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800444 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700445 }
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400446 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700447 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700448 size = ALIGN(size, (unsigned int)4096);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700449 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800450 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
451 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naseer Ahmed2c215292013-09-18 23:47:42 -0400452 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800453 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700454 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
455 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700456 case HAL_PIXEL_FORMAT_YCbCr_422_I:
457 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700458 if(width & 1) {
459 ALOGE("width is odd for the YUV422_SP format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800460 return 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700461 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700462 size = ALIGN(alignedw * alignedh * 2, 4096);
463 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700464 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400465 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhane8a01792012-11-01 16:25:45 -0700466 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700467 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400468 case HAL_PIXEL_FORMAT_BLOB:
469 if(height != 1) {
470 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
471 must have height==1 ", __FUNCTION__);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800472 return 0;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400473 }
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400474 size = width;
475 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700476 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700477 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
478 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500479 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
480 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
481 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
482 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
483 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
484 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
485 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
486 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
487 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
488 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
489 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
490 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
491 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
492 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
493 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
494 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
495 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
496 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
497 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
498 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
499 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
500 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
501 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
502 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
503 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
504 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
505 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Jeykumar Sankaran8f4585f2014-02-05 15:23:40 -0800506 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500507 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
508 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700509 default:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700510 ALOGE("unrecognized pixel format: 0x%x", format);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800511 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700512 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700513 return size;
514}
515
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700516unsigned int getBufferSizeAndDimensions(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700517 int& alignedw, int &alignedh)
518{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700519 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700520
521 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
522 height,
523 format,
524 false,
525 alignedw,
526 alignedh);
527
528 size = getSize(format, width, height, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700529
530 return size;
531}
532
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700533
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700534unsigned int getBufferSizeAndDimensions(int width, int height, int format,
535 int usage, int& alignedw, int &alignedh)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700536{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700537 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700538 int tileEnabled = isMacroTileEnabled(format, usage);
539
540 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
541 height,
542 format,
543 tileEnabled,
544 alignedw,
545 alignedh);
546
547 size = getSize(format, width, height, alignedw, alignedh);
548
549 return size;
550}
551
552
553void getBufferAttributes(int width, int height, int format, int usage,
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700554 int& alignedw, int &alignedh, int& tileEnabled, unsigned int& size)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700555{
556 tileEnabled = isMacroTileEnabled(format, usage);
557
558 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
559 height,
560 format,
561 tileEnabled,
562 alignedw,
563 alignedh);
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800564 size = getSize(format, width, height, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700565}
566
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400567int getYUVPlaneInfo(private_handle_t* hnd, struct android_ycbcr* ycbcr)
568{
569 int err = 0;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700570 unsigned int ystride, cstride;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400571 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
572
573 // Get the chroma offsets from the handle width/height. We take advantage
574 // of the fact the width _is_ the stride
575 switch (hnd->format) {
576 //Semiplanar
577 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
578 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
579 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
580 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: //Same as YCbCr_420_SP_VENUS
Naseer Ahmeda28d31c2014-05-29 15:41:34 -0400581 ystride = cstride = hnd->width;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400582 ycbcr->y = (void*)hnd->base;
583 ycbcr->cb = (void*)(hnd->base + ystride * hnd->height);
584 ycbcr->cr = (void*)(hnd->base + ystride * hnd->height + 1);
585 ycbcr->ystride = ystride;
586 ycbcr->cstride = cstride;
587 ycbcr->chroma_step = 2;
588 break;
589
590 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
591 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
592 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
593 case HAL_PIXEL_FORMAT_NV21_ZSL:
594 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Naseer Ahmeda28d31c2014-05-29 15:41:34 -0400595 ystride = cstride = hnd->width;
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 + 1);
599 ycbcr->ystride = ystride;
600 ycbcr->cstride = cstride;
601 ycbcr->chroma_step = 2;
602 break;
603
604 //Planar
605 case HAL_PIXEL_FORMAT_YV12:
606 ystride = hnd->width;
Arun Kumar K.R6e332482014-08-21 10:57:37 -0700607 cstride = ALIGN(hnd->width/2, 16);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400608 ycbcr->y = (void*)hnd->base;
609 ycbcr->cr = (void*)(hnd->base + ystride * hnd->height);
610 ycbcr->cb = (void*)(hnd->base + ystride * hnd->height +
611 cstride * hnd->height/2);
612 ycbcr->ystride = ystride;
613 ycbcr->cstride = cstride;
614 ycbcr->chroma_step = 1;
615
616 break;
617 //Unsupported formats
618 case HAL_PIXEL_FORMAT_YCbCr_422_I:
619 case HAL_PIXEL_FORMAT_YCrCb_422_I:
620 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
621 default:
622 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__,
623 hnd->format);
624 err = -EINVAL;
625 }
626 return err;
627
628}
629
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700630
631
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700632// Allocate buffer from width, height and format into a
633// private_handle_t. It is the responsibility of the caller
634// to free the buffer using the free_buffer function
635int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
636{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700637 alloc_data data;
638 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700639 gralloc::IAllocController* sAlloc =
640 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700641 data.base = 0;
642 data.fd = -1;
643 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700644 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
645 alignedh);
646
Naseer Ahmed29a26812012-06-14 00:56:20 -0700647 data.align = getpagesize();
648 data.uncached = useUncached(usage);
649 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700650
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700651 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700652 if (0 != err) {
653 ALOGE("%s: allocate failed", __FUNCTION__);
654 return -ENOMEM;
655 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700656
Naseer Ahmed29a26812012-06-14 00:56:20 -0700657 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700658 data.allocType, 0, format,
659 alignedw, alignedh);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700660 hnd->base = (uint64_t) data.base;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700661 hnd->offset = data.offset;
662 hnd->gpuaddr = 0;
663 *pHnd = hnd;
664 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700665}
666
667void free_buffer(private_handle_t *hnd)
668{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700669 gralloc::IAllocController* sAlloc =
670 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700671 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700672 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700673 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
674 }
675 if(hnd)
676 delete hnd;
677
678}