blob: f680c674db89d78dca259a511c6edccd25d8e6a5 [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>
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
50//Common functions
Naseer Ahmed29a26812012-06-14 00:56:20 -070051static bool canFallback(int usage, bool triedSystem)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070052{
53 // Fallback to system heap when alloc fails unless
54 // 1. Composition type is MDP
55 // 2. Alloc from system heap was already tried
56 // 3. The heap type is requsted explicitly
57 // 4. The heap type is protected
58 // 5. The buffer is meant for external display only
59
Naseer Ahmeda87da602012-07-01 23:54:19 -070060 if(QCCompositionType::getInstance().getCompositionType() &
61 COMPOSITION_TYPE_MDP)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070062 return false;
63 if(triedSystem)
64 return false;
Sushil Chauhan7651a802013-01-08 16:08:09 -080065 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PROTECTED))
Iliyan Malchev202a77d2012-06-11 14:41:12 -070066 return false;
Naseer Ahmed4c588a22012-07-31 19:12:17 -070067 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY))
Iliyan Malchev202a77d2012-06-11 14:41:12 -070068 return false;
69 //Return true by default
70 return true;
71}
72
73static bool useUncached(int usage)
74{
75 // System heaps cannot be uncached
76 if(usage & (GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP |
77 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP))
78 return false;
79 if (usage & GRALLOC_USAGE_PRIVATE_UNCACHED)
80 return true;
81 return false;
82}
83
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070084IAllocController* IAllocController::sController = NULL;
85IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070086{
87 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -070088 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -070089 }
90 return sController;
91}
92
93
94//-------------- IonController-----------------------//
95IonController::IonController()
96{
97 mIonAlloc = new IonAlloc();
98}
99
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700100int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700101{
102 int ionFlags = 0;
103 int ret;
104 bool noncontig = false;
105
106 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700107 data.allocType = 0;
108
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700109 if(usage & GRALLOC_USAGE_PRIVATE_UI_CONTIG_HEAP)
110 ionFlags |= ION_HEAP(ION_SF_HEAP_ID);
111
112 if(usage & GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP) {
113 ionFlags |= ION_HEAP(ION_SYSTEM_HEAP_ID);
114 noncontig = true;
115 }
116
Naseer Ahmed4b9784e2013-02-07 13:55:43 -0500117 if(usage & GRALLOC_USAGE_PRIVATE_IOMMU_HEAP) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700118 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
Naseer Ahmed4b9784e2013-02-07 13:55:43 -0500119 noncontig = true;
120 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700121
122 if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP)
123 ionFlags |= ION_HEAP(ION_CP_MM_HEAP_ID);
124
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700125 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
126 ionFlags |= ION_HEAP(ION_CAMERA_HEAP_ID);
127
Naseer Ahmed4b9784e2013-02-07 13:55:43 -0500128 if(usage & GRALLOC_USAGE_PROTECTED && !noncontig)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700129 ionFlags |= ION_SECURE;
130
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700131 // if no flags are set, default to
132 // SF + IOMMU heaps, so that bypass can work
133 // we can fall back to system heap if
134 // we run out.
135 if(!ionFlags)
136 ionFlags = ION_HEAP(ION_SF_HEAP_ID) | ION_HEAP(ION_IOMMU_HEAP_ID);
137
138 data.flags = ionFlags;
139 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700140
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700141 // Fallback
Naseer Ahmed29a26812012-06-14 00:56:20 -0700142 if(ret < 0 && canFallback(usage,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700143 (ionFlags & ION_SYSTEM_HEAP_ID)))
144 {
145 ALOGW("Falling back to system heap");
146 data.flags = ION_HEAP(ION_SYSTEM_HEAP_ID);
147 noncontig = true;
148 ret = mIonAlloc->alloc_buffer(data);
149 }
150
151 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700152 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700153 if(noncontig)
154 data.allocType |= private_handle_t::PRIV_FLAGS_NONCONTIGUOUS_MEM;
155 if(ionFlags & ION_SECURE)
156 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
157 }
158
159 return ret;
160}
161
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700162IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700163{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700164 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700165 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
166 memalloc = mIonAlloc;
167 } else {
168 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
169 }
170
171 return memalloc;
172}
173
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700174size_t getBufferSizeAndDimensions(int width, int height, int format,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700175 int& alignedw, int &alignedh)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700176{
177 size_t size;
178
179 alignedw = ALIGN(width, 32);
180 alignedh = ALIGN(height, 32);
181 switch (format) {
182 case HAL_PIXEL_FORMAT_RGBA_8888:
183 case HAL_PIXEL_FORMAT_RGBX_8888:
184 case HAL_PIXEL_FORMAT_BGRA_8888:
185 size = alignedw * alignedh * 4;
186 break;
187 case HAL_PIXEL_FORMAT_RGB_888:
188 size = alignedw * alignedh * 3;
189 break;
190 case HAL_PIXEL_FORMAT_RGB_565:
191 case HAL_PIXEL_FORMAT_RGBA_5551:
192 case HAL_PIXEL_FORMAT_RGBA_4444:
193 size = alignedw * alignedh * 2;
194 break;
195
196 // adreno formats
197 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
198 size = ALIGN(alignedw*alignedh, 4096);
199 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
200 break;
201 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
202 // The chroma plane is subsampled,
203 // but the pitch in bytes is unchanged
204 // The GPU needs 4K alignment, but the video decoder needs 8K
205 alignedw = ALIGN(width, 128);
206 size = ALIGN( alignedw * alignedh, 8192);
207 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
208 break;
209 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
210 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
211 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
212 case HAL_PIXEL_FORMAT_YV12:
213 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
214 ALOGE("w or h is odd for the YV12 format");
215 return -EINVAL;
216 }
217 alignedw = ALIGN(width, 16);
218 alignedh = height;
219 if (HAL_PIXEL_FORMAT_NV12_ENCODEABLE == format) {
220 // The encoder requires a 2K aligned chroma offset.
221 size = ALIGN(alignedw*alignedh, 2048) +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700222 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700223 } else {
224 size = alignedw*alignedh +
225 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
226 }
227 size = ALIGN(size, 4096);
228 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700229 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
230 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
231 if(width & 1) {
232 ALOGE("width is odd for the YUV422_SP format");
233 return -EINVAL;
234 }
235 alignedw = ALIGN(width, 16);
236 alignedh = height;
237 size = ALIGN(alignedw * alignedh * 2, 4096);
238 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700239 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Sushil Chauhan73dcce42012-11-15 14:25:19 -0800240 alignedw = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
241 alignedh = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
Sushil Chauhane8a01792012-11-01 16:25:45 -0700242 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700243 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700244 default:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700245 ALOGE("unrecognized pixel format: 0x%x", format);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700246 return -EINVAL;
247 }
248
249 return size;
250}
251
252// Allocate buffer from width, height and format into a
253// private_handle_t. It is the responsibility of the caller
254// to free the buffer using the free_buffer function
255int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
256{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700257 alloc_data data;
258 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700259 gralloc::IAllocController* sAlloc =
260 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700261 data.base = 0;
262 data.fd = -1;
263 data.offset = 0;
264 data.size = getBufferSizeAndDimensions(w, h, format, alignedw, alignedh);
265 data.align = getpagesize();
266 data.uncached = useUncached(usage);
267 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700268
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700269 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700270 if (0 != err) {
271 ALOGE("%s: allocate failed", __FUNCTION__);
272 return -ENOMEM;
273 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700274
Naseer Ahmed29a26812012-06-14 00:56:20 -0700275 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700276 data.allocType, 0, format,
277 alignedw, alignedh);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700278 hnd->base = (int) data.base;
279 hnd->offset = data.offset;
280 hnd->gpuaddr = 0;
281 *pHnd = hnd;
282 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700283}
284
285void free_buffer(private_handle_t *hnd)
286{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700287 gralloc::IAllocController* sAlloc =
288 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700289 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700290 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700291 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
292 }
293 if(hnd)
294 delete hnd;
295
296}