blob: 5d0cf2b940b11beca37d0393ed512c3623d19174 [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
2 * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
3
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.
13 * * Neither the name of Code Aurora Forum, Inc. nor the names of its
14 * 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>
31#include <utils/RefBase.h>
32#include <fcntl.h>
33#include "gralloc_priv.h"
34#include "alloc_controller.h"
35#include "memalloc.h"
36#include "ionalloc.h"
Naseer Ahmed29a26812012-06-14 00:56:20 -070037#include "pmemalloc.h"
Iliyan Malchev202a77d2012-06-11 14:41:12 -070038#include "ashmemalloc.h"
39#include "gr.h"
Naseer Ahmeda87da602012-07-01 23:54:19 -070040#include "comptype.h"
Iliyan Malchev202a77d2012-06-11 14:41:12 -070041
42using namespace gralloc;
Naseer Ahmeda87da602012-07-01 23:54:19 -070043using namespace qdutils;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070044using android::sp;
45
Naseer Ahmed29a26812012-06-14 00:56:20 -070046const int GRALLOC_HEAP_MASK = GRALLOC_USAGE_PRIVATE_ADSP_HEAP |
47 GRALLOC_USAGE_PRIVATE_UI_CONTIG_HEAP |
48 GRALLOC_USAGE_PRIVATE_SMI_HEAP |
49 GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP |
50 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP |
51 GRALLOC_USAGE_PRIVATE_MM_HEAP |
52 GRALLOC_USAGE_PRIVATE_WRITEBACK_HEAP |
53 GRALLOC_USAGE_PRIVATE_CAMERA_HEAP;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070054
55
56//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;
Naseer Ahmed29a26812012-06-14 00:56:20 -070071 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PROTECTED |
72 GRALLOC_USAGE_PRIVATE_CP_BUFFER))
Iliyan Malchev202a77d2012-06-11 14:41:12 -070073 return false;
74 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_EXTERNAL_ONLY))
75 return false;
76 //Return true by default
77 return true;
78}
79
80static bool useUncached(int usage)
81{
82 // System heaps cannot be uncached
83 if(usage & (GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP |
84 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP))
85 return false;
86 if (usage & GRALLOC_USAGE_PRIVATE_UNCACHED)
87 return true;
88 return false;
89}
90
91sp<IAllocController> IAllocController::sController = NULL;
92sp<IAllocController> IAllocController::getInstance(bool useMasterHeap)
93{
94 if(sController == NULL) {
95#ifdef USE_ION
96 sController = new IonController();
97#else
98 if(useMasterHeap)
99 sController = new PmemAshmemController();
100 else
101 sController = new PmemKernelController();
102#endif
103 }
104 return sController;
105}
106
107
108//-------------- IonController-----------------------//
109IonController::IonController()
110{
111 mIonAlloc = new IonAlloc();
112}
113
114int IonController::allocate(alloc_data& data, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700115 int compositionType)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700116{
117 int ionFlags = 0;
118 int ret;
119 bool noncontig = false;
120
121 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700122 data.allocType = 0;
123
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700124 if(usage & GRALLOC_USAGE_PRIVATE_UI_CONTIG_HEAP)
125 ionFlags |= ION_HEAP(ION_SF_HEAP_ID);
126
127 if(usage & GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP) {
128 ionFlags |= ION_HEAP(ION_SYSTEM_HEAP_ID);
129 noncontig = true;
130 }
131
132 if(usage & GRALLOC_USAGE_PRIVATE_IOMMU_HEAP)
133 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
134
135 if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP)
136 ionFlags |= ION_HEAP(ION_CP_MM_HEAP_ID);
137
138 if(usage & GRALLOC_USAGE_PRIVATE_WRITEBACK_HEAP)
139 ionFlags |= ION_HEAP(ION_CP_WB_HEAP_ID);
140
141 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
142 ionFlags |= ION_HEAP(ION_CAMERA_HEAP_ID);
143
Naseer Ahmed29a26812012-06-14 00:56:20 -0700144 if(usage & GRALLOC_USAGE_PRIVATE_CP_BUFFER)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700145 ionFlags |= ION_SECURE;
146
147 if(usage & GRALLOC_USAGE_PRIVATE_DO_NOT_MAP)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700148 data.allocType |= private_handle_t::PRIV_FLAGS_NOT_MAPPED;
149 else
150 data.allocType &= ~(private_handle_t::PRIV_FLAGS_NOT_MAPPED);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700151
152 // if no flags are set, default to
153 // SF + IOMMU heaps, so that bypass can work
154 // we can fall back to system heap if
155 // we run out.
156 if(!ionFlags)
157 ionFlags = ION_HEAP(ION_SF_HEAP_ID) | ION_HEAP(ION_IOMMU_HEAP_ID);
158
159 data.flags = ionFlags;
160 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700161
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700162 // Fallback
Naseer Ahmed29a26812012-06-14 00:56:20 -0700163 if(ret < 0 && canFallback(usage,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700164 (ionFlags & ION_SYSTEM_HEAP_ID)))
165 {
166 ALOGW("Falling back to system heap");
167 data.flags = ION_HEAP(ION_SYSTEM_HEAP_ID);
168 noncontig = true;
169 ret = mIonAlloc->alloc_buffer(data);
170 }
171
172 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700173 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700174 if(noncontig)
175 data.allocType |= private_handle_t::PRIV_FLAGS_NONCONTIGUOUS_MEM;
176 if(ionFlags & ION_SECURE)
177 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
178 }
179
180 return ret;
181}
182
183sp<IMemAlloc> IonController::getAllocator(int flags)
184{
185 sp<IMemAlloc> memalloc;
186 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
187 memalloc = mIonAlloc;
188 } else {
189 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
190 }
191
192 return memalloc;
193}
194
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700195//-------------- PmemKernelController-----------------------//
Naseer Ahmed29a26812012-06-14 00:56:20 -0700196//XXX: Remove - we're not using pmem anymore
197#if 0
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700198PmemKernelController::PmemKernelController()
199{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700200 mPmemAdspAlloc = new PmemKernelAlloc(DEVICE_PMEM_ADSP);
201 // XXX: Right now, there is no need to maintain an instance
202 // of the SMI allocator as we need it only in a few cases
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700203}
204
205PmemKernelController::~PmemKernelController()
206{
207}
208
209int PmemKernelController::allocate(alloc_data& data, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700210 int compositionType)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700211{
212 int ret = 0;
213 bool adspFallback = false;
214 if (!(usage & GRALLOC_USAGE_PRIVATE_SMI_HEAP))
215 adspFallback = true;
216
217 // Try SMI first
218 if ((usage & GRALLOC_USAGE_PRIVATE_SMI_HEAP) ||
219 (usage & GRALLOC_USAGE_EXTERNAL_DISP) ||
220 (usage & GRALLOC_USAGE_PROTECTED))
221 {
222 int tempFd = open(DEVICE_PMEM_SMIPOOL, O_RDWR, 0);
223 if(tempFd > 0) {
224 close(tempFd);
225 sp<IMemAlloc> memalloc;
226 memalloc = new PmemKernelAlloc(DEVICE_PMEM_SMIPOOL);
227 ret = memalloc->alloc_buffer(data);
228 if(ret >= 0)
229 return ret;
230 else {
231 if(adspFallback)
232 ALOGW("Allocation from SMI failed, trying ADSP");
233 }
234 }
235 }
236
237 if ((usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP) || adspFallback) {
238 ret = mPmemAdspAlloc->alloc_buffer(data);
239 }
240 return ret;
241}
242
243sp<IMemAlloc> PmemKernelController::getAllocator(int flags)
244{
245 sp<IMemAlloc> memalloc;
246 if (flags & private_handle_t::PRIV_FLAGS_USES_PMEM_ADSP)
247 memalloc = mPmemAdspAlloc;
248 else {
249 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
250 memalloc = NULL;
251 }
252
253 return memalloc;
254}
255
256//-------------- PmemAshmmemController-----------------------//
257
258PmemAshmemController::PmemAshmemController()
259{
260 mPmemUserspaceAlloc = new PmemUserspaceAlloc();
261 mAshmemAlloc = new AshmemAlloc();
262 mPmemKernelCtrl = new PmemKernelController();
263}
264
265PmemAshmemController::~PmemAshmemController()
266{
267}
268
269int PmemAshmemController::allocate(alloc_data& data, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700270 int compositionType)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700271{
272 int ret = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700273 data.allocType = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700274
275 // Make buffers cacheable by default
Naseer Ahmed29a26812012-06-14 00:56:20 -0700276 data.uncached = false;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700277
278 // Override if we explicitly need uncached buffers
279 if (usage & GRALLOC_USAGE_PRIVATE_UNCACHED)
280 data.uncached = true;
281
282 // If ADSP or SMI is requested use the kernel controller
283 if(usage & (GRALLOC_USAGE_PRIVATE_ADSP_HEAP|
284 GRALLOC_USAGE_PRIVATE_SMI_HEAP)) {
285 ret = mPmemKernelCtrl->allocate(data, usage, compositionType);
286 if(ret < 0)
287 ALOGE("%s: Failed to allocate ADSP/SMI memory", __func__);
288 else
289 data.allocType = private_handle_t::PRIV_FLAGS_USES_PMEM_ADSP;
290 return ret;
291 }
292
293 if(usage & GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP) {
294 ret = mAshmemAlloc->alloc_buffer(data);
295 if(ret >= 0) {
296 data.allocType = private_handle_t::PRIV_FLAGS_USES_ASHMEM;
297 data.allocType |= private_handle_t::PRIV_FLAGS_NONCONTIGUOUS_MEM;
298 }
299 return ret;
300 }
301
302 // if no memory specific flags are set,
303 // default to EBI heap, so that bypass
304 // can work. We can fall back to system
305 // heap if we run out.
306 ret = mPmemUserspaceAlloc->alloc_buffer(data);
307
308 // Fallback
309 if(ret >= 0 ) {
310 data.allocType = private_handle_t::PRIV_FLAGS_USES_PMEM;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700311 } else if(ret < 0 && canFallback(usage, false)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700312 ALOGW("Falling back to ashmem");
313 ret = mAshmemAlloc->alloc_buffer(data);
314 if(ret >= 0) {
315 data.allocType = private_handle_t::PRIV_FLAGS_USES_ASHMEM;
316 data.allocType |= private_handle_t::PRIV_FLAGS_NONCONTIGUOUS_MEM;
317 }
318 }
319
320 return ret;
321}
322
323sp<IMemAlloc> PmemAshmemController::getAllocator(int flags)
324{
325 sp<IMemAlloc> memalloc;
326 if (flags & private_handle_t::PRIV_FLAGS_USES_PMEM)
327 memalloc = mPmemUserspaceAlloc;
328 else if (flags & private_handle_t::PRIV_FLAGS_USES_PMEM_ADSP)
329 memalloc = mPmemKernelCtrl->getAllocator(flags);
330 else if (flags & private_handle_t::PRIV_FLAGS_USES_ASHMEM)
331 memalloc = mAshmemAlloc;
332 else {
333 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
334 memalloc = NULL;
335 }
336
337 return memalloc;
338}
339#endif
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700340size_t getBufferSizeAndDimensions(int width, int height, int format,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700341 int& alignedw, int &alignedh)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700342{
343 size_t size;
344
345 alignedw = ALIGN(width, 32);
346 alignedh = ALIGN(height, 32);
347 switch (format) {
348 case HAL_PIXEL_FORMAT_RGBA_8888:
349 case HAL_PIXEL_FORMAT_RGBX_8888:
350 case HAL_PIXEL_FORMAT_BGRA_8888:
351 size = alignedw * alignedh * 4;
352 break;
353 case HAL_PIXEL_FORMAT_RGB_888:
354 size = alignedw * alignedh * 3;
355 break;
356 case HAL_PIXEL_FORMAT_RGB_565:
357 case HAL_PIXEL_FORMAT_RGBA_5551:
358 case HAL_PIXEL_FORMAT_RGBA_4444:
359 size = alignedw * alignedh * 2;
360 break;
361
362 // adreno formats
363 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
364 size = ALIGN(alignedw*alignedh, 4096);
365 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
366 break;
367 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
368 // The chroma plane is subsampled,
369 // but the pitch in bytes is unchanged
370 // The GPU needs 4K alignment, but the video decoder needs 8K
371 alignedw = ALIGN(width, 128);
372 size = ALIGN( alignedw * alignedh, 8192);
373 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
374 break;
375 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
376 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
377 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
378 case HAL_PIXEL_FORMAT_YV12:
379 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
380 ALOGE("w or h is odd for the YV12 format");
381 return -EINVAL;
382 }
383 alignedw = ALIGN(width, 16);
384 alignedh = height;
385 if (HAL_PIXEL_FORMAT_NV12_ENCODEABLE == format) {
386 // The encoder requires a 2K aligned chroma offset.
387 size = ALIGN(alignedw*alignedh, 2048) +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700388 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700389 } else {
390 size = alignedw*alignedh +
391 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
392 }
393 size = ALIGN(size, 4096);
394 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700395 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
396 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
397 if(width & 1) {
398 ALOGE("width is odd for the YUV422_SP format");
399 return -EINVAL;
400 }
401 alignedw = ALIGN(width, 16);
402 alignedh = height;
403 size = ALIGN(alignedw * alignedh * 2, 4096);
404 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700405 default:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700406 ALOGE("unrecognized pixel format: 0x%x", format);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700407 return -EINVAL;
408 }
409
410 return size;
411}
412
413// Allocate buffer from width, height and format into a
414// private_handle_t. It is the responsibility of the caller
415// to free the buffer using the free_buffer function
416int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
417{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700418 alloc_data data;
419 int alignedw, alignedh;
420 android::sp<gralloc::IAllocController> sAlloc =
421 gralloc::IAllocController::getInstance(false);
422 data.base = 0;
423 data.fd = -1;
424 data.offset = 0;
425 data.size = getBufferSizeAndDimensions(w, h, format, alignedw, alignedh);
426 data.align = getpagesize();
427 data.uncached = useUncached(usage);
428 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700429
Naseer Ahmed29a26812012-06-14 00:56:20 -0700430 int err = sAlloc->allocate(data, allocFlags, 0);
431 if (0 != err) {
432 ALOGE("%s: allocate failed", __FUNCTION__);
433 return -ENOMEM;
434 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700435
Naseer Ahmed29a26812012-06-14 00:56:20 -0700436 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
437 data.allocType, 0, format, alignedw, alignedh);
438 hnd->base = (int) data.base;
439 hnd->offset = data.offset;
440 hnd->gpuaddr = 0;
441 *pHnd = hnd;
442 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700443}
444
445void free_buffer(private_handle_t *hnd)
446{
447 android::sp<gralloc::IAllocController> sAlloc =
448 gralloc::IAllocController::getInstance(false);
449 if (hnd && hnd->fd > 0) {
450 sp<IMemAlloc> memalloc = sAlloc->getAllocator(hnd->flags);
451 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
452 }
453 if(hnd)
454 delete hnd;
455
456}