blob: 97255bab0dfd46197b921038778a5cb8c6ef2e40 [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
39using namespace gralloc;
Naseer Ahmeda87da602012-07-01 23:54:19 -070040using namespace qdutils;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070041
42//Common functions
Naseer Ahmed29a26812012-06-14 00:56:20 -070043static bool canFallback(int usage, bool triedSystem)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070044{
45 // Fallback to system heap when alloc fails unless
46 // 1. Composition type is MDP
47 // 2. Alloc from system heap was already tried
48 // 3. The heap type is requsted explicitly
49 // 4. The heap type is protected
50 // 5. The buffer is meant for external display only
51
Naseer Ahmeda87da602012-07-01 23:54:19 -070052 if(QCCompositionType::getInstance().getCompositionType() &
53 COMPOSITION_TYPE_MDP)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070054 return false;
55 if(triedSystem)
56 return false;
Naseer Ahmed29a26812012-06-14 00:56:20 -070057 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PROTECTED |
58 GRALLOC_USAGE_PRIVATE_CP_BUFFER))
Iliyan Malchev202a77d2012-06-11 14:41:12 -070059 return false;
60 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_EXTERNAL_ONLY))
61 return false;
62 //Return true by default
63 return true;
64}
65
66static bool useUncached(int usage)
67{
68 // System heaps cannot be uncached
69 if(usage & (GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP |
70 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP))
71 return false;
72 if (usage & GRALLOC_USAGE_PRIVATE_UNCACHED)
73 return true;
74 return false;
75}
76
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070077IAllocController* IAllocController::sController = NULL;
78IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070079{
80 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -070081 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -070082 }
83 return sController;
84}
85
86
87//-------------- IonController-----------------------//
88IonController::IonController()
89{
90 mIonAlloc = new IonAlloc();
91}
92
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070093int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070094{
95 int ionFlags = 0;
96 int ret;
97 bool noncontig = false;
98
99 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700100 data.allocType = 0;
101
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700102 if(usage & GRALLOC_USAGE_PRIVATE_UI_CONTIG_HEAP)
103 ionFlags |= ION_HEAP(ION_SF_HEAP_ID);
104
105 if(usage & GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP) {
106 ionFlags |= ION_HEAP(ION_SYSTEM_HEAP_ID);
107 noncontig = true;
108 }
109
110 if(usage & GRALLOC_USAGE_PRIVATE_IOMMU_HEAP)
111 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
112
113 if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP)
114 ionFlags |= ION_HEAP(ION_CP_MM_HEAP_ID);
115
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700116 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
117 ionFlags |= ION_HEAP(ION_CAMERA_HEAP_ID);
118
Naseer Ahmed29a26812012-06-14 00:56:20 -0700119 if(usage & GRALLOC_USAGE_PRIVATE_CP_BUFFER)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700120 ionFlags |= ION_SECURE;
121
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700122 // if no flags are set, default to
123 // SF + IOMMU heaps, so that bypass can work
124 // we can fall back to system heap if
125 // we run out.
126 if(!ionFlags)
127 ionFlags = ION_HEAP(ION_SF_HEAP_ID) | ION_HEAP(ION_IOMMU_HEAP_ID);
128
129 data.flags = ionFlags;
130 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700131
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700132 // Fallback
Naseer Ahmed29a26812012-06-14 00:56:20 -0700133 if(ret < 0 && canFallback(usage,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700134 (ionFlags & ION_SYSTEM_HEAP_ID)))
135 {
136 ALOGW("Falling back to system heap");
137 data.flags = ION_HEAP(ION_SYSTEM_HEAP_ID);
138 noncontig = true;
139 ret = mIonAlloc->alloc_buffer(data);
140 }
141
142 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700143 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700144 if(noncontig)
145 data.allocType |= private_handle_t::PRIV_FLAGS_NONCONTIGUOUS_MEM;
146 if(ionFlags & ION_SECURE)
147 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
148 }
149
150 return ret;
151}
152
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700153IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700154{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700155 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700156 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
157 memalloc = mIonAlloc;
158 } else {
159 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
160 }
161
162 return memalloc;
163}
164
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700165size_t getBufferSizeAndDimensions(int width, int height, int format,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700166 int& alignedw, int &alignedh)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700167{
168 size_t size;
169
170 alignedw = ALIGN(width, 32);
171 alignedh = ALIGN(height, 32);
172 switch (format) {
173 case HAL_PIXEL_FORMAT_RGBA_8888:
174 case HAL_PIXEL_FORMAT_RGBX_8888:
175 case HAL_PIXEL_FORMAT_BGRA_8888:
176 size = alignedw * alignedh * 4;
177 break;
178 case HAL_PIXEL_FORMAT_RGB_888:
179 size = alignedw * alignedh * 3;
180 break;
181 case HAL_PIXEL_FORMAT_RGB_565:
182 case HAL_PIXEL_FORMAT_RGBA_5551:
183 case HAL_PIXEL_FORMAT_RGBA_4444:
184 size = alignedw * alignedh * 2;
185 break;
186
187 // adreno formats
188 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
189 size = ALIGN(alignedw*alignedh, 4096);
190 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
191 break;
192 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
193 // The chroma plane is subsampled,
194 // but the pitch in bytes is unchanged
195 // The GPU needs 4K alignment, but the video decoder needs 8K
196 alignedw = ALIGN(width, 128);
197 size = ALIGN( alignedw * alignedh, 8192);
198 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
199 break;
200 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
201 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
202 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
203 case HAL_PIXEL_FORMAT_YV12:
204 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
205 ALOGE("w or h is odd for the YV12 format");
206 return -EINVAL;
207 }
208 alignedw = ALIGN(width, 16);
209 alignedh = height;
210 if (HAL_PIXEL_FORMAT_NV12_ENCODEABLE == format) {
211 // The encoder requires a 2K aligned chroma offset.
212 size = ALIGN(alignedw*alignedh, 2048) +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700213 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700214 } else {
215 size = alignedw*alignedh +
216 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
217 }
218 size = ALIGN(size, 4096);
219 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700220 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
221 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
222 if(width & 1) {
223 ALOGE("width is odd for the YUV422_SP format");
224 return -EINVAL;
225 }
226 alignedw = ALIGN(width, 16);
227 alignedh = height;
228 size = ALIGN(alignedw * alignedh * 2, 4096);
229 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700230 default:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700231 ALOGE("unrecognized pixel format: 0x%x", format);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700232 return -EINVAL;
233 }
234
235 return size;
236}
237
238// Allocate buffer from width, height and format into a
239// private_handle_t. It is the responsibility of the caller
240// to free the buffer using the free_buffer function
241int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
242{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700243 alloc_data data;
244 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700245 gralloc::IAllocController* sAlloc =
246 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700247 data.base = 0;
248 data.fd = -1;
249 data.offset = 0;
250 data.size = getBufferSizeAndDimensions(w, h, format, alignedw, alignedh);
251 data.align = getpagesize();
252 data.uncached = useUncached(usage);
253 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700254
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700255 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700256 if (0 != err) {
257 ALOGE("%s: allocate failed", __FUNCTION__);
258 return -ENOMEM;
259 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700260
Naseer Ahmed29a26812012-06-14 00:56:20 -0700261 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700262 data.allocType, 0, format,
263 alignedw, alignedh);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700264 hnd->base = (int) data.base;
265 hnd->offset = data.offset;
266 hnd->gpuaddr = 0;
267 *pHnd = hnd;
268 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700269}
270
271void free_buffer(private_handle_t *hnd)
272{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700273 gralloc::IAllocController* sAlloc =
274 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700275 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700276 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700277 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
278 }
279 if(hnd)
280 delete hnd;
281
282}