blob: dc5f6aeb0c307775d791be3a99b9abece7a4d05c [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
Duy Truong73d36df2013-02-09 20:33:23 -08002 * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
Iliyan Malchev202a77d2012-06-11 14:41:12 -07003
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
Duy Truong73d36df2013-02-09 20:33:23 -080013 * * Neither the name of The Linux Foundation nor the names of its
Iliyan Malchev202a77d2012-06-11 14:41:12 -070014 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <cutils/log.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070031#include <fcntl.h>
Naomi Luis01f5c8e2013-02-11 12:46:24 -080032#include <dlfcn.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070033#include "gralloc_priv.h"
34#include "alloc_controller.h"
35#include "memalloc.h"
36#include "ionalloc.h"
Iliyan Malchev202a77d2012-06-11 14:41:12 -070037#include "gr.h"
Naseer Ahmeda87da602012-07-01 23:54:19 -070038#include "comptype.h"
Manoj Kumar AVM8a220812013-10-10 11:46:06 -070039#include "mdp_version.h"
Iliyan Malchev202a77d2012-06-11 14:41:12 -070040
Sushil Chauhanc6bd6d92012-12-12 12:33:01 -080041#ifdef VENUS_COLOR_FORMAT
42#include <media/msm_media_info.h>
43#else
44#define VENUS_Y_STRIDE(args...) 0
45#define VENUS_Y_SCANLINES(args...) 0
46#define VENUS_BUFFER_SIZE(args...) 0
47#endif
48
Iliyan Malchev202a77d2012-06-11 14:41:12 -070049using namespace gralloc;
Naseer Ahmeda87da602012-07-01 23:54:19 -070050using namespace qdutils;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070051
Naomi Luisa44100c2013-02-08 12:42:03 -080052ANDROID_SINGLETON_STATIC_INSTANCE(AdrenoMemInfo);
53
Iliyan Malchev202a77d2012-06-11 14:41:12 -070054//Common functions
Naseer Ahmed29a26812012-06-14 00:56:20 -070055static bool canFallback(int usage, bool triedSystem)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070056{
57 // Fallback to system heap when alloc fails unless
58 // 1. Composition type is MDP
59 // 2. Alloc from system heap was already tried
60 // 3. The heap type is requsted explicitly
61 // 4. The heap type is protected
62 // 5. The buffer is meant for external display only
63
Naseer Ahmeda87da602012-07-01 23:54:19 -070064 if(QCCompositionType::getInstance().getCompositionType() &
65 COMPOSITION_TYPE_MDP)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070066 return false;
67 if(triedSystem)
68 return false;
Sushil Chauhan7651a802013-01-08 16:08:09 -080069 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PROTECTED))
Iliyan Malchev202a77d2012-06-11 14:41:12 -070070 return false;
Naseer Ahmed4c588a22012-07-31 19:12:17 -070071 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY))
Iliyan Malchev202a77d2012-06-11 14:41:12 -070072 return false;
73 //Return true by default
74 return true;
75}
76
77static bool useUncached(int usage)
78{
Naseer Ahmed26867882013-08-21 15:16:24 -040079 if (usage & GRALLOC_USAGE_PRIVATE_UNCACHED)
80 return true;
81 if(((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY)
82 ||((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY))
Iliyan Malchev202a77d2012-06-11 14:41:12 -070083 return true;
84 return false;
85}
86
Naomi Luisa44100c2013-02-08 12:42:03 -080087//-------------- AdrenoMemInfo-----------------------//
Naomi Luis01f5c8e2013-02-11 12:46:24 -080088AdrenoMemInfo::AdrenoMemInfo()
89{
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -080090 LINK_adreno_compute_aligned_width_and_height = NULL;
91 LINK_adreno_compute_padding = NULL;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -070092 LINK_adreno_isMacroTilingSupportedByGpu = NULL;
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -080093
Naomi Luis01f5c8e2013-02-11 12:46:24 -080094 libadreno_utils = ::dlopen("libadreno_utils.so", RTLD_NOW);
95 if (libadreno_utils) {
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -080096 *(void **)&LINK_adreno_compute_aligned_width_and_height =
Manoj Kumar AVM8a220812013-10-10 11:46:06 -070097 ::dlsym(libadreno_utils, "compute_aligned_width_and_height");
98 *(void **)&LINK_adreno_compute_padding =
99 ::dlsym(libadreno_utils, "compute_surface_padding");
100 *(void **)&LINK_adreno_isMacroTilingSupportedByGpu =
101 ::dlsym(libadreno_utils, "isMacroTilingSupportedByGpu");
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800102 }
103}
104
105AdrenoMemInfo::~AdrenoMemInfo()
106{
107 if (libadreno_utils) {
108 ::dlclose(libadreno_utils);
109 }
110}
111
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700112int AdrenoMemInfo::isMacroTilingSupportedByGPU()
113{
114 if ((libadreno_utils)) {
115 if(LINK_adreno_isMacroTilingSupportedByGpu) {
116 return LINK_adreno_isMacroTilingSupportedByGpu();
117 }
118 }
119 return 0;
120}
121
122
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800123void AdrenoMemInfo::getAlignedWidthAndHeight(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700124 int tile_enabled, int& aligned_w, int& aligned_h)
Naomi Luisa44100c2013-02-08 12:42:03 -0800125{
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800126 aligned_w = ALIGN(width, 32);
127 aligned_h = ALIGN(height, 32);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800128 // Currently surface padding is only computed for RGB* surfaces.
Jesse Hallfbe96d22013-09-20 01:39:43 -0700129 if (format <= HAL_PIXEL_FORMAT_sRGB_X_8888) {
Naomi Luis1b379692013-05-06 12:13:07 -0700130 // Don't add any additional padding if debug.gralloc.map_fb_memory
131 // is enabled
132 char property[PROPERTY_VALUE_MAX];
133 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
134 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
135 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800136 return;
Naomi Luis1b379692013-05-06 12:13:07 -0700137 }
138
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800139 int bpp = 4;
140 switch(format)
141 {
142 case HAL_PIXEL_FORMAT_RGB_888:
143 bpp = 3;
144 break;
145 case HAL_PIXEL_FORMAT_RGB_565:
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 {
176 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400177 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800178 aligned_w = ALIGN(width, 32);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800179 break;
180 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800181 aligned_w = ALIGN(width, 128);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800182 break;
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800183 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
184 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
185 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);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800195 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400196 case HAL_PIXEL_FORMAT_BLOB:
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800197 aligned_w = width;
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);
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700201 break;
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800202 default: break;
203 }
Naomi Luisa44100c2013-02-08 12:42:03 -0800204 }
Naomi Luisa44100c2013-02-08 12:42:03 -0800205}
206
207//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700208IAllocController* IAllocController::sController = NULL;
209IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700210{
211 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700212 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700213 }
214 return sController;
215}
216
217
218//-------------- IonController-----------------------//
219IonController::IonController()
220{
221 mIonAlloc = new IonAlloc();
222}
223
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700224int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700225{
226 int ionFlags = 0;
227 int ret;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700228
229 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700230 data.allocType = 0;
231
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700232 if(usage & GRALLOC_USAGE_PRIVATE_UI_CONTIG_HEAP)
233 ionFlags |= ION_HEAP(ION_SF_HEAP_ID);
234
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500235 if(usage & GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700236 ionFlags |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700237
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500238 if(usage & GRALLOC_USAGE_PRIVATE_IOMMU_HEAP)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700239 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
240
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530241 if(usage & GRALLOC_USAGE_PROTECTED) {
Prabhanjan Kandulae8f4bec2013-10-24 16:32:51 +0530242 if (usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500243 ionFlags |= ION_HEAP(ION_CP_MM_HEAP_ID);
244 ionFlags |= ION_SECURE;
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530245 } else {
246 // for targets/OEMs which do not need HW level protection
247 // do not set ion secure flag & MM heap. Fallback to IOMMU heap.
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500248 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
249 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530250 } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
251 //MM Heap is exclusively a secure heap.
252 //If it is used for non secure cases, fallback to IOMMU heap
253 ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
254 cannot be used as an insecure heap!\
255 trying to use IOMMU instead !!");
256 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500257 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700258
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700259 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
260 ionFlags |= ION_HEAP(ION_CAMERA_HEAP_ID);
261
Arun Kumar K.R0daaa992013-03-12 15:08:29 -0700262 if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
263 ionFlags |= ION_HEAP(ION_ADSP_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700264
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530265 if(ionFlags & ION_SECURE)
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500266 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700267
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700268 // if no flags are set, default to
269 // SF + IOMMU heaps, so that bypass can work
270 // we can fall back to system heap if
271 // we run out.
272 if(!ionFlags)
273 ionFlags = ION_HEAP(ION_SF_HEAP_ID) | ION_HEAP(ION_IOMMU_HEAP_ID);
274
275 data.flags = ionFlags;
276 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700277
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700278 // Fallback
Naseer Ahmed29a26812012-06-14 00:56:20 -0700279 if(ret < 0 && canFallback(usage,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700280 (ionFlags & ION_SYSTEM_HEAP_ID)))
281 {
282 ALOGW("Falling back to system heap");
283 data.flags = ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700284 ret = mIonAlloc->alloc_buffer(data);
285 }
286
287 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700288 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700289 }
290
291 return ret;
292}
293
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700294IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700295{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700296 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700297 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
298 memalloc = mIonAlloc;
299 } else {
300 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
301 }
302
303 return memalloc;
304}
305
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700306bool isMacroTileEnabled(int format, int usage)
307{
308 bool tileEnabled = false;
309
310 // Check whether GPU & MDSS supports MacroTiling feature
311 if(AdrenoMemInfo::getInstance().isMacroTilingSupportedByGPU() &&
312 qdutils::MDPVersion::getInstance().supportsMacroTile())
313 {
314 // check the format
315 switch(format)
316 {
317 case HAL_PIXEL_FORMAT_RGBA_8888:
318 case HAL_PIXEL_FORMAT_RGBX_8888:
319 case HAL_PIXEL_FORMAT_BGRA_8888:
320 {
321 tileEnabled = true;
322 // check the usage flags
323 if (usage & (GRALLOC_USAGE_SW_READ_MASK |
324 GRALLOC_USAGE_SW_WRITE_MASK)) {
325 // Application intends to use CPU for rendering
326 tileEnabled = false;
327 }
328 break;
329 }
330 default:
331 break;
332 }
333 }
334 return tileEnabled;
335}
336
337// helper function
338size_t getSize(int format, int width, int height, int alignedw, int alignedh)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700339{
340 size_t size;
341
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700342 switch (format) {
343 case HAL_PIXEL_FORMAT_RGBA_8888:
344 case HAL_PIXEL_FORMAT_RGBX_8888:
345 case HAL_PIXEL_FORMAT_BGRA_8888:
Naseer Ahmed82fc4b72013-09-20 01:31:37 -0700346 case HAL_PIXEL_FORMAT_sRGB_A_8888:
Jesse Hallfbe96d22013-09-20 01:39:43 -0700347 case HAL_PIXEL_FORMAT_sRGB_X_8888:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700348 size = alignedw * alignedh * 4;
349 break;
350 case HAL_PIXEL_FORMAT_RGB_888:
351 size = alignedw * alignedh * 3;
352 break;
353 case HAL_PIXEL_FORMAT_RGB_565:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400354 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700355 size = alignedw * alignedh * 2;
356 break;
357
358 // adreno formats
359 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
360 size = ALIGN(alignedw*alignedh, 4096);
361 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
362 break;
363 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
364 // The chroma plane is subsampled,
365 // but the pitch in bytes is unchanged
366 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700367 size = ALIGN( alignedw * alignedh, 8192);
368 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
369 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700370 case HAL_PIXEL_FORMAT_YV12:
371 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
372 ALOGE("w or h is odd for the YV12 format");
373 return -EINVAL;
374 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700375 alignedh = height;
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400376 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700377 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700378 size = ALIGN(size, 4096);
379 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800380 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
381 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
382 alignedh = height;
Naseer Ahmed2c215292013-09-18 23:47:42 -0400383 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800384 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700385 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
386 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700387 case HAL_PIXEL_FORMAT_YCbCr_422_I:
388 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700389 if(width & 1) {
390 ALOGE("width is odd for the YUV422_SP format");
391 return -EINVAL;
392 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700393 alignedh = height;
394 size = ALIGN(alignedw * alignedh * 2, 4096);
395 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700396 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400397 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhan73dcce42012-11-15 14:25:19 -0800398 alignedh = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
Sushil Chauhane8a01792012-11-01 16:25:45 -0700399 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700400 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400401 case HAL_PIXEL_FORMAT_BLOB:
402 if(height != 1) {
403 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
404 must have height==1 ", __FUNCTION__);
405 return -EINVAL;
406 }
407 alignedh = height;
408 alignedw = width;
409 size = width;
410 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700411 case HAL_PIXEL_FORMAT_NV21_ZSL:
412 alignedh = ALIGN(height, 64);
413 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
414 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700415 default:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700416 ALOGE("unrecognized pixel format: 0x%x", format);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700417 return -EINVAL;
418 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700419 return size;
420}
421
422size_t getBufferSizeAndDimensions(int width, int height, int format,
423 int& alignedw, int &alignedh)
424{
425 size_t size;
426
427 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
428 height,
429 format,
430 false,
431 alignedw,
432 alignedh);
433
434 size = getSize(format, width, height, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700435
436 return size;
437}
438
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700439
440size_t getBufferSizeAndDimensions(int width, int height, int format, int usage,
441 int& alignedw, int &alignedh)
442{
443 size_t size;
444 int tileEnabled = isMacroTileEnabled(format, usage);
445
446 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
447 height,
448 format,
449 tileEnabled,
450 alignedw,
451 alignedh);
452
453 size = getSize(format, width, height, alignedw, alignedh);
454
455 return size;
456}
457
458
459void getBufferAttributes(int width, int height, int format, int usage,
460 int& alignedw, int &alignedh, int& tileEnabled, size_t& size)
461{
462 tileEnabled = isMacroTileEnabled(format, usage);
463
464 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
465 height,
466 format,
467 tileEnabled,
468 alignedw,
469 alignedh);
470
471 if(size)
472 size = getSize(format, width, height, alignedw, alignedh);
473}
474
475
476
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700477// Allocate buffer from width, height and format into a
478// private_handle_t. It is the responsibility of the caller
479// to free the buffer using the free_buffer function
480int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
481{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700482 alloc_data data;
483 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700484 gralloc::IAllocController* sAlloc =
485 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700486 data.base = 0;
487 data.fd = -1;
488 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700489 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
490 alignedh);
491
Naseer Ahmed29a26812012-06-14 00:56:20 -0700492 data.align = getpagesize();
493 data.uncached = useUncached(usage);
494 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700495
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700496 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700497 if (0 != err) {
498 ALOGE("%s: allocate failed", __FUNCTION__);
499 return -ENOMEM;
500 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700501
Naseer Ahmed29a26812012-06-14 00:56:20 -0700502 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700503 data.allocType, 0, format,
504 alignedw, alignedh);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700505 hnd->base = (int) data.base;
506 hnd->offset = data.offset;
507 hnd->gpuaddr = 0;
508 *pHnd = hnd;
509 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700510}
511
512void free_buffer(private_handle_t *hnd)
513{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700514 gralloc::IAllocController* sAlloc =
515 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700516 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700517 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700518 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
519 }
520 if(hnd)
521 delete hnd;
522
523}