blob: f0b2ca292f08339ab9615e48bc39b148caa2fb76 [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;
Naseer Ahmed29a26812012-06-14 00:56:20 -070065 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PROTECTED |
66 GRALLOC_USAGE_PRIVATE_CP_BUFFER))
Iliyan Malchev202a77d2012-06-11 14:41:12 -070067 return false;
Naseer Ahmed4c588a22012-07-31 19:12:17 -070068 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY))
Iliyan Malchev202a77d2012-06-11 14:41:12 -070069 return false;
70 //Return true by default
71 return true;
72}
73
74static bool useUncached(int usage)
75{
76 // System heaps cannot be uncached
77 if(usage & (GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP |
78 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP))
79 return false;
80 if (usage & GRALLOC_USAGE_PRIVATE_UNCACHED)
81 return true;
82 return false;
83}
84
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070085IAllocController* IAllocController::sController = NULL;
86IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070087{
88 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -070089 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -070090 }
91 return sController;
92}
93
94
95//-------------- IonController-----------------------//
96IonController::IonController()
97{
98 mIonAlloc = new IonAlloc();
99}
100
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700101int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700102{
103 int ionFlags = 0;
104 int ret;
105 bool noncontig = false;
106
107 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700108 data.allocType = 0;
109
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700110 if(usage & GRALLOC_USAGE_PRIVATE_UI_CONTIG_HEAP)
111 ionFlags |= ION_HEAP(ION_SF_HEAP_ID);
112
113 if(usage & GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP) {
114 ionFlags |= ION_HEAP(ION_SYSTEM_HEAP_ID);
115 noncontig = true;
116 }
117
118 if(usage & GRALLOC_USAGE_PRIVATE_IOMMU_HEAP)
119 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
120
121 if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP)
122 ionFlags |= ION_HEAP(ION_CP_MM_HEAP_ID);
123
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700124 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
125 ionFlags |= ION_HEAP(ION_CAMERA_HEAP_ID);
126
Naseer Ahmed29a26812012-06-14 00:56:20 -0700127 if(usage & GRALLOC_USAGE_PRIVATE_CP_BUFFER)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700128 ionFlags |= ION_SECURE;
129
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700130 // if no flags are set, default to
131 // SF + IOMMU heaps, so that bypass can work
132 // we can fall back to system heap if
133 // we run out.
134 if(!ionFlags)
135 ionFlags = ION_HEAP(ION_SF_HEAP_ID) | ION_HEAP(ION_IOMMU_HEAP_ID);
136
137 data.flags = ionFlags;
138 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700139
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700140 // Fallback
Naseer Ahmed29a26812012-06-14 00:56:20 -0700141 if(ret < 0 && canFallback(usage,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700142 (ionFlags & ION_SYSTEM_HEAP_ID)))
143 {
144 ALOGW("Falling back to system heap");
145 data.flags = ION_HEAP(ION_SYSTEM_HEAP_ID);
146 noncontig = true;
147 ret = mIonAlloc->alloc_buffer(data);
148 }
149
150 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700151 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700152 if(noncontig)
153 data.allocType |= private_handle_t::PRIV_FLAGS_NONCONTIGUOUS_MEM;
154 if(ionFlags & ION_SECURE)
155 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
156 }
157
158 return ret;
159}
160
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700161IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700162{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700163 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700164 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
165 memalloc = mIonAlloc;
166 } else {
167 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
168 }
169
170 return memalloc;
171}
172
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700173size_t getBufferSizeAndDimensions(int width, int height, int format,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700174 int& alignedw, int &alignedh)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700175{
176 size_t size;
177
178 alignedw = ALIGN(width, 32);
179 alignedh = ALIGN(height, 32);
180 switch (format) {
181 case HAL_PIXEL_FORMAT_RGBA_8888:
182 case HAL_PIXEL_FORMAT_RGBX_8888:
183 case HAL_PIXEL_FORMAT_BGRA_8888:
184 size = alignedw * alignedh * 4;
185 break;
186 case HAL_PIXEL_FORMAT_RGB_888:
187 size = alignedw * alignedh * 3;
188 break;
189 case HAL_PIXEL_FORMAT_RGB_565:
190 case HAL_PIXEL_FORMAT_RGBA_5551:
191 case HAL_PIXEL_FORMAT_RGBA_4444:
192 size = alignedw * alignedh * 2;
193 break;
194
195 // adreno formats
196 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
197 size = ALIGN(alignedw*alignedh, 4096);
198 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
199 break;
200 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
201 // The chroma plane is subsampled,
202 // but the pitch in bytes is unchanged
203 // The GPU needs 4K alignment, but the video decoder needs 8K
204 alignedw = ALIGN(width, 128);
205 size = ALIGN( alignedw * alignedh, 8192);
206 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
207 break;
208 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
209 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
210 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
211 case HAL_PIXEL_FORMAT_YV12:
212 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
213 ALOGE("w or h is odd for the YV12 format");
214 return -EINVAL;
215 }
216 alignedw = ALIGN(width, 16);
217 alignedh = height;
218 if (HAL_PIXEL_FORMAT_NV12_ENCODEABLE == format) {
219 // The encoder requires a 2K aligned chroma offset.
220 size = ALIGN(alignedw*alignedh, 2048) +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700221 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700222 } else {
223 size = alignedw*alignedh +
224 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
225 }
226 size = ALIGN(size, 4096);
227 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700228 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
229 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
230 if(width & 1) {
231 ALOGE("width is odd for the YUV422_SP format");
232 return -EINVAL;
233 }
234 alignedw = ALIGN(width, 16);
235 alignedh = height;
236 size = ALIGN(alignedw * alignedh * 2, 4096);
237 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700238 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Sushil Chauhane8a01792012-11-01 16:25:45 -0700239 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700240 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700241 default:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700242 ALOGE("unrecognized pixel format: 0x%x", format);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700243 return -EINVAL;
244 }
245
246 return size;
247}
248
249// Allocate buffer from width, height and format into a
250// private_handle_t. It is the responsibility of the caller
251// to free the buffer using the free_buffer function
252int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
253{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700254 alloc_data data;
255 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700256 gralloc::IAllocController* sAlloc =
257 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700258 data.base = 0;
259 data.fd = -1;
260 data.offset = 0;
261 data.size = getBufferSizeAndDimensions(w, h, format, alignedw, alignedh);
262 data.align = getpagesize();
263 data.uncached = useUncached(usage);
264 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700265
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700266 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700267 if (0 != err) {
268 ALOGE("%s: allocate failed", __FUNCTION__);
269 return -ENOMEM;
270 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700271
Naseer Ahmed29a26812012-06-14 00:56:20 -0700272 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700273 data.allocType, 0, format,
274 alignedw, alignedh);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700275 hnd->base = (int) data.base;
276 hnd->offset = data.offset;
277 hnd->gpuaddr = 0;
278 *pHnd = hnd;
279 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700280}
281
282void free_buffer(private_handle_t *hnd)
283{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700284 gralloc::IAllocController* sAlloc =
285 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700286 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700287 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700288 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
289 }
290 if(hnd)
291 delete hnd;
292
293}