blob: 6fb520df8137edb51dc71411a6fe7d238b182e4a [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
Duy Truong73d36df2013-02-09 20:33:23 -08002 * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
Iliyan Malchev202a77d2012-06-11 14:41:12 -07003
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.
Duy Truong73d36df2013-02-09 20:33:23 -080013 * * Neither the name of The Linux Foundation nor the names of its
Iliyan Malchev202a77d2012-06-11 14:41:12 -070014 * 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
117 if(usage & GRALLOC_USAGE_PRIVATE_IOMMU_HEAP)
118 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
119
120 if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP)
121 ionFlags |= ION_HEAP(ION_CP_MM_HEAP_ID);
122
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700123 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
124 ionFlags |= ION_HEAP(ION_CAMERA_HEAP_ID);
125
Sushil Chauhan7651a802013-01-08 16:08:09 -0800126 if(usage & GRALLOC_USAGE_PROTECTED)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700127 ionFlags |= ION_SECURE;
128
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700129 // if no flags are set, default to
130 // SF + IOMMU heaps, so that bypass can work
131 // we can fall back to system heap if
132 // we run out.
133 if(!ionFlags)
134 ionFlags = ION_HEAP(ION_SF_HEAP_ID) | ION_HEAP(ION_IOMMU_HEAP_ID);
135
136 data.flags = ionFlags;
137 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700138
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700139 // Fallback
Naseer Ahmed29a26812012-06-14 00:56:20 -0700140 if(ret < 0 && canFallback(usage,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700141 (ionFlags & ION_SYSTEM_HEAP_ID)))
142 {
143 ALOGW("Falling back to system heap");
144 data.flags = ION_HEAP(ION_SYSTEM_HEAP_ID);
145 noncontig = true;
146 ret = mIonAlloc->alloc_buffer(data);
147 }
148
149 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700150 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700151 if(noncontig)
152 data.allocType |= private_handle_t::PRIV_FLAGS_NONCONTIGUOUS_MEM;
153 if(ionFlags & ION_SECURE)
154 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
155 }
156
157 return ret;
158}
159
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700160IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700161{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700162 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700163 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
164 memalloc = mIonAlloc;
165 } else {
166 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
167 }
168
169 return memalloc;
170}
171
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700172size_t getBufferSizeAndDimensions(int width, int height, int format,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700173 int& alignedw, int &alignedh)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700174{
175 size_t size;
176
177 alignedw = ALIGN(width, 32);
178 alignedh = ALIGN(height, 32);
179 switch (format) {
180 case HAL_PIXEL_FORMAT_RGBA_8888:
181 case HAL_PIXEL_FORMAT_RGBX_8888:
182 case HAL_PIXEL_FORMAT_BGRA_8888:
183 size = alignedw * alignedh * 4;
184 break;
185 case HAL_PIXEL_FORMAT_RGB_888:
186 size = alignedw * alignedh * 3;
187 break;
188 case HAL_PIXEL_FORMAT_RGB_565:
189 case HAL_PIXEL_FORMAT_RGBA_5551:
190 case HAL_PIXEL_FORMAT_RGBA_4444:
191 size = alignedw * alignedh * 2;
192 break;
193
194 // adreno formats
195 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
196 size = ALIGN(alignedw*alignedh, 4096);
197 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
198 break;
199 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
200 // The chroma plane is subsampled,
201 // but the pitch in bytes is unchanged
202 // The GPU needs 4K alignment, but the video decoder needs 8K
203 alignedw = ALIGN(width, 128);
204 size = ALIGN( alignedw * alignedh, 8192);
205 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
206 break;
207 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
208 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
209 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
210 case HAL_PIXEL_FORMAT_YV12:
211 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
212 ALOGE("w or h is odd for the YV12 format");
213 return -EINVAL;
214 }
215 alignedw = ALIGN(width, 16);
216 alignedh = height;
217 if (HAL_PIXEL_FORMAT_NV12_ENCODEABLE == format) {
218 // The encoder requires a 2K aligned chroma offset.
219 size = ALIGN(alignedw*alignedh, 2048) +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700220 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700221 } else {
222 size = alignedw*alignedh +
223 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
224 }
225 size = ALIGN(size, 4096);
226 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700227 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
228 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
229 if(width & 1) {
230 ALOGE("width is odd for the YUV422_SP format");
231 return -EINVAL;
232 }
233 alignedw = ALIGN(width, 16);
234 alignedh = height;
235 size = ALIGN(alignedw * alignedh * 2, 4096);
236 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700237 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Sushil Chauhan73dcce42012-11-15 14:25:19 -0800238 alignedw = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
239 alignedh = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
Sushil Chauhane8a01792012-11-01 16:25:45 -0700240 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700241 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700242 default:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700243 ALOGE("unrecognized pixel format: 0x%x", format);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700244 return -EINVAL;
245 }
246
247 return size;
248}
249
250// Allocate buffer from width, height and format into a
251// private_handle_t. It is the responsibility of the caller
252// to free the buffer using the free_buffer function
253int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
254{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700255 alloc_data data;
256 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700257 gralloc::IAllocController* sAlloc =
258 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700259 data.base = 0;
260 data.fd = -1;
261 data.offset = 0;
262 data.size = getBufferSizeAndDimensions(w, h, format, alignedw, alignedh);
263 data.align = getpagesize();
264 data.uncached = useUncached(usage);
265 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700266
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700267 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700268 if (0 != err) {
269 ALOGE("%s: allocate failed", __FUNCTION__);
270 return -ENOMEM;
271 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700272
Naseer Ahmed29a26812012-06-14 00:56:20 -0700273 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700274 data.allocType, 0, format,
275 alignedw, alignedh);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700276 hnd->base = (int) data.base;
277 hnd->offset = data.offset;
278 hnd->gpuaddr = 0;
279 *pHnd = hnd;
280 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700281}
282
283void free_buffer(private_handle_t *hnd)
284{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700285 gralloc::IAllocController* sAlloc =
286 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700287 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700288 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700289 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
290 }
291 if(hnd)
292 delete hnd;
293
294}