blob: ebd7e9879581bcdf127196dd1928153a73566326 [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"
Iliyan Malchev202a77d2012-06-11 14:41:12 -070039
Sushil Chauhanc6bd6d92012-12-12 12:33:01 -080040#ifdef VENUS_COLOR_FORMAT
41#include <media/msm_media_info.h>
42#else
43#define VENUS_Y_STRIDE(args...) 0
44#define VENUS_Y_SCANLINES(args...) 0
45#define VENUS_BUFFER_SIZE(args...) 0
46#endif
47
Iliyan Malchev202a77d2012-06-11 14:41:12 -070048using namespace gralloc;
Naseer Ahmeda87da602012-07-01 23:54:19 -070049using namespace qdutils;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070050
Naomi Luisa44100c2013-02-08 12:42:03 -080051ANDROID_SINGLETON_STATIC_INSTANCE(AdrenoMemInfo);
52
Iliyan Malchev202a77d2012-06-11 14:41:12 -070053//Common functions
Naseer Ahmed29a26812012-06-14 00:56:20 -070054static bool canFallback(int usage, bool triedSystem)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070055{
56 // Fallback to system heap when alloc fails unless
57 // 1. Composition type is MDP
58 // 2. Alloc from system heap was already tried
59 // 3. The heap type is requsted explicitly
60 // 4. The heap type is protected
61 // 5. The buffer is meant for external display only
62
Naseer Ahmeda87da602012-07-01 23:54:19 -070063 if(QCCompositionType::getInstance().getCompositionType() &
64 COMPOSITION_TYPE_MDP)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070065 return false;
66 if(triedSystem)
67 return false;
Sushil Chauhan7651a802013-01-08 16:08:09 -080068 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PROTECTED))
Iliyan Malchev202a77d2012-06-11 14:41:12 -070069 return false;
Naseer Ahmed4c588a22012-07-31 19:12:17 -070070 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY))
Iliyan Malchev202a77d2012-06-11 14:41:12 -070071 return false;
72 //Return true by default
73 return true;
74}
75
76static bool useUncached(int usage)
77{
78 // System heaps cannot be uncached
79 if(usage & (GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP |
80 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP))
81 return false;
82 if (usage & GRALLOC_USAGE_PRIVATE_UNCACHED)
83 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{
90 libadreno_utils = ::dlopen("libadreno_utils.so", RTLD_NOW);
91 if (libadreno_utils) {
92 *(void **)&LINK_adreno_compute_padding = ::dlsym(libadreno_utils,
93 "compute_surface_padding");
94 }
95}
96
97AdrenoMemInfo::~AdrenoMemInfo()
98{
99 if (libadreno_utils) {
100 ::dlclose(libadreno_utils);
101 }
102}
103
Naomi Luisa44100c2013-02-08 12:42:03 -0800104int AdrenoMemInfo::getStride(int width, int format)
105{
106 int stride = ALIGN(width, 32);
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800107 // Currently surface padding is only computed for RGB* surfaces.
108 if (format < 0x7) {
109 int bpp = 4;
110 switch(format)
111 {
112 case HAL_PIXEL_FORMAT_RGB_888:
113 bpp = 3;
114 break;
115 case HAL_PIXEL_FORMAT_RGB_565:
116 case HAL_PIXEL_FORMAT_RGBA_5551:
117 case HAL_PIXEL_FORMAT_RGBA_4444:
118 bpp = 2;
119 break;
120 default: break;
121 }
122 if ((libadreno_utils) && (LINK_adreno_compute_padding)) {
123 int surface_tile_height = 1; // Linear surface
124 int raster_mode = 1; // Adreno TW raster mode.
125 int padding_threshold = 512; // Threshold for padding surfaces.
126 // the function below expects the width to be a multiple of
127 // 32 pixels, hence we pass stride instead of width.
128 stride = LINK_adreno_compute_padding(stride, bpp,
129 surface_tile_height, raster_mode,
130 padding_threshold);
131 }
132 } else {
133 switch (format)
134 {
135 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
136 stride = ALIGN(width, 32);
137 break;
138 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
139 stride = ALIGN(width, 128);
140 break;
141 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
142 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
143 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
144 case HAL_PIXEL_FORMAT_YV12:
145 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
146 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
147 stride = ALIGN(width, 16);
148 break;
149 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
150 stride = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
151 break;
152 default: break;
153 }
Naomi Luisa44100c2013-02-08 12:42:03 -0800154 }
155 return stride;
156}
157
158//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700159IAllocController* IAllocController::sController = NULL;
160IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700161{
162 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700163 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700164 }
165 return sController;
166}
167
168
169//-------------- IonController-----------------------//
170IonController::IonController()
171{
172 mIonAlloc = new IonAlloc();
173}
174
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700175int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700176{
177 int ionFlags = 0;
178 int ret;
179 bool noncontig = false;
180
181 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700182 data.allocType = 0;
183
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700184 if(usage & GRALLOC_USAGE_PRIVATE_UI_CONTIG_HEAP)
185 ionFlags |= ION_HEAP(ION_SF_HEAP_ID);
186
187 if(usage & GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP) {
188 ionFlags |= ION_HEAP(ION_SYSTEM_HEAP_ID);
189 noncontig = true;
190 }
191
Naseer Ahmed4b9784e2013-02-07 13:55:43 -0500192 if(usage & GRALLOC_USAGE_PRIVATE_IOMMU_HEAP) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700193 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
Naseer Ahmed4b9784e2013-02-07 13:55:43 -0500194 noncontig = true;
195 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700196
197 if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP)
198 ionFlags |= ION_HEAP(ION_CP_MM_HEAP_ID);
199
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700200 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
201 ionFlags |= ION_HEAP(ION_CAMERA_HEAP_ID);
202
Naseer Ahmed4b9784e2013-02-07 13:55:43 -0500203 if(usage & GRALLOC_USAGE_PROTECTED && !noncontig)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700204 ionFlags |= ION_SECURE;
205
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700206 // if no flags are set, default to
207 // SF + IOMMU heaps, so that bypass can work
208 // we can fall back to system heap if
209 // we run out.
210 if(!ionFlags)
211 ionFlags = ION_HEAP(ION_SF_HEAP_ID) | ION_HEAP(ION_IOMMU_HEAP_ID);
212
213 data.flags = ionFlags;
214 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700215
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700216 // Fallback
Naseer Ahmed29a26812012-06-14 00:56:20 -0700217 if(ret < 0 && canFallback(usage,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700218 (ionFlags & ION_SYSTEM_HEAP_ID)))
219 {
220 ALOGW("Falling back to system heap");
221 data.flags = ION_HEAP(ION_SYSTEM_HEAP_ID);
222 noncontig = true;
223 ret = mIonAlloc->alloc_buffer(data);
224 }
225
226 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700227 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700228 if(noncontig)
229 data.allocType |= private_handle_t::PRIV_FLAGS_NONCONTIGUOUS_MEM;
230 if(ionFlags & ION_SECURE)
231 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
232 }
233
234 return ret;
235}
236
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700237IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700238{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700239 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700240 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
241 memalloc = mIonAlloc;
242 } else {
243 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
244 }
245
246 return memalloc;
247}
248
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700249size_t getBufferSizeAndDimensions(int width, int height, int format,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700250 int& alignedw, int &alignedh)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700251{
252 size_t size;
253
Naomi Luisa44100c2013-02-08 12:42:03 -0800254 alignedw = AdrenoMemInfo::getInstance().getStride(width, format);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700255 alignedh = ALIGN(height, 32);
256 switch (format) {
257 case HAL_PIXEL_FORMAT_RGBA_8888:
258 case HAL_PIXEL_FORMAT_RGBX_8888:
259 case HAL_PIXEL_FORMAT_BGRA_8888:
260 size = alignedw * alignedh * 4;
261 break;
262 case HAL_PIXEL_FORMAT_RGB_888:
263 size = alignedw * alignedh * 3;
264 break;
265 case HAL_PIXEL_FORMAT_RGB_565:
266 case HAL_PIXEL_FORMAT_RGBA_5551:
267 case HAL_PIXEL_FORMAT_RGBA_4444:
268 size = alignedw * alignedh * 2;
269 break;
270
271 // adreno formats
272 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
273 size = ALIGN(alignedw*alignedh, 4096);
274 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
275 break;
276 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
277 // The chroma plane is subsampled,
278 // but the pitch in bytes is unchanged
279 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700280 size = ALIGN( alignedw * alignedh, 8192);
281 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
282 break;
283 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
284 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
285 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
286 case HAL_PIXEL_FORMAT_YV12:
287 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
288 ALOGE("w or h is odd for the YV12 format");
289 return -EINVAL;
290 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700291 alignedh = height;
292 if (HAL_PIXEL_FORMAT_NV12_ENCODEABLE == format) {
293 // The encoder requires a 2K aligned chroma offset.
294 size = ALIGN(alignedw*alignedh, 2048) +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700295 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700296 } else {
297 size = alignedw*alignedh +
298 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
299 }
300 size = ALIGN(size, 4096);
301 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700302 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
303 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
304 if(width & 1) {
305 ALOGE("width is odd for the YUV422_SP format");
306 return -EINVAL;
307 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700308 alignedh = height;
309 size = ALIGN(alignedw * alignedh * 2, 4096);
310 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700311 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Sushil Chauhan73dcce42012-11-15 14:25:19 -0800312 alignedh = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
Sushil Chauhane8a01792012-11-01 16:25:45 -0700313 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700314 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700315 default:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700316 ALOGE("unrecognized pixel format: 0x%x", format);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700317 return -EINVAL;
318 }
319
320 return size;
321}
322
323// Allocate buffer from width, height and format into a
324// private_handle_t. It is the responsibility of the caller
325// to free the buffer using the free_buffer function
326int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
327{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700328 alloc_data data;
329 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700330 gralloc::IAllocController* sAlloc =
331 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700332 data.base = 0;
333 data.fd = -1;
334 data.offset = 0;
335 data.size = getBufferSizeAndDimensions(w, h, format, alignedw, alignedh);
336 data.align = getpagesize();
337 data.uncached = useUncached(usage);
338 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700339
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700340 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700341 if (0 != err) {
342 ALOGE("%s: allocate failed", __FUNCTION__);
343 return -ENOMEM;
344 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700345
Naseer Ahmed29a26812012-06-14 00:56:20 -0700346 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700347 data.allocType, 0, format,
348 alignedw, alignedh);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700349 hnd->base = (int) data.base;
350 hnd->offset = data.offset;
351 hnd->gpuaddr = 0;
352 *pHnd = hnd;
353 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700354}
355
356void free_buffer(private_handle_t *hnd)
357{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700358 gralloc::IAllocController* sAlloc =
359 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700360 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700361 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700362 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
363 }
364 if(hnd)
365 delete hnd;
366
367}