blob: 5aca75828b988533fc480af27fbbf8096d6b8e6a [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>
32#include "gralloc_priv.h"
33#include "alloc_controller.h"
34#include "memalloc.h"
35#include "ionalloc.h"
Iliyan Malchev202a77d2012-06-11 14:41:12 -070036#include "gr.h"
Naseer Ahmeda87da602012-07-01 23:54:19 -070037#include "comptype.h"
Iliyan Malchev202a77d2012-06-11 14:41:12 -070038
Sushil Chauhanc6bd6d92012-12-12 12:33:01 -080039#ifdef VENUS_COLOR_FORMAT
40#include <media/msm_media_info.h>
41#else
42#define VENUS_Y_STRIDE(args...) 0
43#define VENUS_Y_SCANLINES(args...) 0
44#define VENUS_BUFFER_SIZE(args...) 0
45#endif
46
Iliyan Malchev202a77d2012-06-11 14:41:12 -070047using namespace gralloc;
Naseer Ahmeda87da602012-07-01 23:54:19 -070048using namespace qdutils;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070049
Naomi Luisa44100c2013-02-08 12:42:03 -080050ANDROID_SINGLETON_STATIC_INSTANCE(AdrenoMemInfo);
51
Iliyan Malchev202a77d2012-06-11 14:41:12 -070052//Common functions
Naseer Ahmed29a26812012-06-14 00:56:20 -070053static bool canFallback(int usage, bool triedSystem)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070054{
55 // Fallback to system heap when alloc fails unless
56 // 1. Composition type is MDP
57 // 2. Alloc from system heap was already tried
58 // 3. The heap type is requsted explicitly
59 // 4. The heap type is protected
60 // 5. The buffer is meant for external display only
61
Naseer Ahmeda87da602012-07-01 23:54:19 -070062 if(QCCompositionType::getInstance().getCompositionType() &
63 COMPOSITION_TYPE_MDP)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070064 return false;
65 if(triedSystem)
66 return false;
Sushil Chauhan7651a802013-01-08 16:08:09 -080067 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PROTECTED))
Iliyan Malchev202a77d2012-06-11 14:41:12 -070068 return false;
Naseer Ahmed4c588a22012-07-31 19:12:17 -070069 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY))
Iliyan Malchev202a77d2012-06-11 14:41:12 -070070 return false;
71 //Return true by default
72 return true;
73}
74
75static bool useUncached(int usage)
76{
77 // System heaps cannot be uncached
78 if(usage & (GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP |
79 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP))
80 return false;
81 if (usage & GRALLOC_USAGE_PRIVATE_UNCACHED)
82 return true;
83 return false;
84}
85
Naomi Luisa44100c2013-02-08 12:42:03 -080086//-------------- AdrenoMemInfo-----------------------//
87int AdrenoMemInfo::getStride(int width, int format)
88{
89 int stride = ALIGN(width, 32);
90 switch (format)
91 {
92 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
93 stride = ALIGN(width, 32);
94 break;
95 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
96 stride = ALIGN(width, 128);
97 break;
98 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
99 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
100 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
101 case HAL_PIXEL_FORMAT_YV12:
102 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
103 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
104 stride = ALIGN(width, 16);
105 break;
106 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
107 stride = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
108 break;
109 default: break;
110 }
111 return stride;
112}
113
114//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700115IAllocController* IAllocController::sController = NULL;
116IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700117{
118 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700119 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700120 }
121 return sController;
122}
123
124
125//-------------- IonController-----------------------//
126IonController::IonController()
127{
128 mIonAlloc = new IonAlloc();
129}
130
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700131int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700132{
133 int ionFlags = 0;
134 int ret;
135 bool noncontig = false;
136
137 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700138 data.allocType = 0;
139
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700140 if(usage & GRALLOC_USAGE_PRIVATE_UI_CONTIG_HEAP)
141 ionFlags |= ION_HEAP(ION_SF_HEAP_ID);
142
143 if(usage & GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP) {
144 ionFlags |= ION_HEAP(ION_SYSTEM_HEAP_ID);
145 noncontig = true;
146 }
147
Naseer Ahmed4b9784e2013-02-07 13:55:43 -0500148 if(usage & GRALLOC_USAGE_PRIVATE_IOMMU_HEAP) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700149 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
Naseer Ahmed4b9784e2013-02-07 13:55:43 -0500150 noncontig = true;
151 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700152
153 if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP)
154 ionFlags |= ION_HEAP(ION_CP_MM_HEAP_ID);
155
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700156 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
157 ionFlags |= ION_HEAP(ION_CAMERA_HEAP_ID);
158
Naseer Ahmed4b9784e2013-02-07 13:55:43 -0500159 if(usage & GRALLOC_USAGE_PROTECTED && !noncontig)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700160 ionFlags |= ION_SECURE;
161
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700162 // if no flags are set, default to
163 // SF + IOMMU heaps, so that bypass can work
164 // we can fall back to system heap if
165 // we run out.
166 if(!ionFlags)
167 ionFlags = ION_HEAP(ION_SF_HEAP_ID) | ION_HEAP(ION_IOMMU_HEAP_ID);
168
169 data.flags = ionFlags;
170 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700171
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700172 // Fallback
Naseer Ahmed29a26812012-06-14 00:56:20 -0700173 if(ret < 0 && canFallback(usage,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700174 (ionFlags & ION_SYSTEM_HEAP_ID)))
175 {
176 ALOGW("Falling back to system heap");
177 data.flags = ION_HEAP(ION_SYSTEM_HEAP_ID);
178 noncontig = true;
179 ret = mIonAlloc->alloc_buffer(data);
180 }
181
182 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700183 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700184 if(noncontig)
185 data.allocType |= private_handle_t::PRIV_FLAGS_NONCONTIGUOUS_MEM;
186 if(ionFlags & ION_SECURE)
187 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
188 }
189
190 return ret;
191}
192
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700193IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700194{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700195 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700196 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
197 memalloc = mIonAlloc;
198 } else {
199 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
200 }
201
202 return memalloc;
203}
204
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700205size_t getBufferSizeAndDimensions(int width, int height, int format,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700206 int& alignedw, int &alignedh)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700207{
208 size_t size;
209
Naomi Luisa44100c2013-02-08 12:42:03 -0800210 alignedw = AdrenoMemInfo::getInstance().getStride(width, format);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700211 alignedh = ALIGN(height, 32);
212 switch (format) {
213 case HAL_PIXEL_FORMAT_RGBA_8888:
214 case HAL_PIXEL_FORMAT_RGBX_8888:
215 case HAL_PIXEL_FORMAT_BGRA_8888:
216 size = alignedw * alignedh * 4;
217 break;
218 case HAL_PIXEL_FORMAT_RGB_888:
219 size = alignedw * alignedh * 3;
220 break;
221 case HAL_PIXEL_FORMAT_RGB_565:
222 case HAL_PIXEL_FORMAT_RGBA_5551:
223 case HAL_PIXEL_FORMAT_RGBA_4444:
224 size = alignedw * alignedh * 2;
225 break;
226
227 // adreno formats
228 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
229 size = ALIGN(alignedw*alignedh, 4096);
230 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
231 break;
232 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
233 // The chroma plane is subsampled,
234 // but the pitch in bytes is unchanged
235 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700236 size = ALIGN( alignedw * alignedh, 8192);
237 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
238 break;
239 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
240 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
241 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
242 case HAL_PIXEL_FORMAT_YV12:
243 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
244 ALOGE("w or h is odd for the YV12 format");
245 return -EINVAL;
246 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700247 alignedh = height;
248 if (HAL_PIXEL_FORMAT_NV12_ENCODEABLE == format) {
249 // The encoder requires a 2K aligned chroma offset.
250 size = ALIGN(alignedw*alignedh, 2048) +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700251 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700252 } else {
253 size = alignedw*alignedh +
254 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
255 }
256 size = ALIGN(size, 4096);
257 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700258 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
259 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
260 if(width & 1) {
261 ALOGE("width is odd for the YUV422_SP format");
262 return -EINVAL;
263 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700264 alignedh = height;
265 size = ALIGN(alignedw * alignedh * 2, 4096);
266 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700267 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Sushil Chauhan73dcce42012-11-15 14:25:19 -0800268 alignedh = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
Sushil Chauhane8a01792012-11-01 16:25:45 -0700269 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700270 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700271 default:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700272 ALOGE("unrecognized pixel format: 0x%x", format);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700273 return -EINVAL;
274 }
275
276 return size;
277}
278
279// Allocate buffer from width, height and format into a
280// private_handle_t. It is the responsibility of the caller
281// to free the buffer using the free_buffer function
282int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
283{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700284 alloc_data data;
285 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700286 gralloc::IAllocController* sAlloc =
287 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700288 data.base = 0;
289 data.fd = -1;
290 data.offset = 0;
291 data.size = getBufferSizeAndDimensions(w, h, format, alignedw, alignedh);
292 data.align = getpagesize();
293 data.uncached = useUncached(usage);
294 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700295
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700296 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700297 if (0 != err) {
298 ALOGE("%s: allocate failed", __FUNCTION__);
299 return -ENOMEM;
300 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700301
Naseer Ahmed29a26812012-06-14 00:56:20 -0700302 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700303 data.allocType, 0, format,
304 alignedw, alignedh);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700305 hnd->base = (int) data.base;
306 hnd->offset = data.offset;
307 hnd->gpuaddr = 0;
308 *pHnd = hnd;
309 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700310}
311
312void free_buffer(private_handle_t *hnd)
313{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700314 gralloc::IAllocController* sAlloc =
315 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700316 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700317 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700318 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
319 }
320 if(hnd)
321 delete hnd;
322
323}