1 /*
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 <fcntl.h>
32 #include "gralloc_priv.h"
33 #include "alloc_controller.h"
34 #include "memalloc.h"
35 #include "ionalloc.h"
36 #include "gr.h"
37 #include "comptype.h"
38
39 using namespace gralloc;
40 using namespace qdutils;
41
42 //Common functions
canFallback(int usage,bool triedSystem)43 static bool canFallback(int usage, bool triedSystem)
44 {
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
52 if(QCCompositionType::getInstance().getCompositionType() &
53 COMPOSITION_TYPE_MDP)
54 return false;
55 if(triedSystem)
56 return false;
57 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PROTECTED |
58 GRALLOC_USAGE_PRIVATE_CP_BUFFER))
59 return false;
60 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY))
61 return false;
62 //Return true by default
63 return true;
64 }
65
useUncached(int usage)66 static 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
77 IAllocController* IAllocController::sController = NULL;
getInstance(void)78 IAllocController* IAllocController::getInstance(void)
79 {
80 if(sController == NULL) {
81 sController = new IonController();
82 }
83 return sController;
84 }
85
86
87 //-------------- IonController-----------------------//
IonController()88 IonController::IonController()
89 {
90 mIonAlloc = new IonAlloc();
91 }
92
allocate(alloc_data & data,int usage)93 int IonController::allocate(alloc_data& data, int usage)
94 {
95 int ionFlags = 0;
96 int ret;
97 bool noncontig = false;
98
99 data.uncached = useUncached(usage);
100 data.allocType = 0;
101
102 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
116 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
117 ionFlags |= ION_HEAP(ION_CAMERA_HEAP_ID);
118
119 if(usage & GRALLOC_USAGE_PRIVATE_CP_BUFFER)
120 ionFlags |= ION_SECURE;
121
122 // 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);
131
132 // Fallback
133 if(ret < 0 && canFallback(usage,
134 (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 ) {
143 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
144 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
getAllocator(int flags)153 IMemAlloc* IonController::getAllocator(int flags)
154 {
155 IMemAlloc* memalloc = NULL;
156 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
getBufferSizeAndDimensions(int width,int height,int format,int & alignedw,int & alignedh)165 size_t getBufferSizeAndDimensions(int width, int height, int format,
166 int& alignedw, int &alignedh)
167 {
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) +
213 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
214 } else {
215 size = alignedw*alignedh +
216 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
217 }
218 size = ALIGN(size, 4096);
219 break;
220 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;
230 default:
231 ALOGE("unrecognized pixel format: 0x%x", format);
232 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
alloc_buffer(private_handle_t ** pHnd,int w,int h,int format,int usage)241 int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
242 {
243 alloc_data data;
244 int alignedw, alignedh;
245 gralloc::IAllocController* sAlloc =
246 gralloc::IAllocController::getInstance();
247 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;
254
255 int err = sAlloc->allocate(data, allocFlags);
256 if (0 != err) {
257 ALOGE("%s: allocate failed", __FUNCTION__);
258 return -ENOMEM;
259 }
260
261 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
262 data.allocType, 0, format,
263 alignedw, alignedh);
264 hnd->base = (int) data.base;
265 hnd->offset = data.offset;
266 hnd->gpuaddr = 0;
267 *pHnd = hnd;
268 return 0;
269 }
270
free_buffer(private_handle_t * hnd)271 void free_buffer(private_handle_t *hnd)
272 {
273 gralloc::IAllocController* sAlloc =
274 gralloc::IAllocController::getInstance();
275 if (hnd && hnd->fd > 0) {
276 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
277 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
278 }
279 if(hnd)
280 delete hnd;
281
282 }
283