blob: 291f5647fe343423193e17b4a91d024d65e25fc4 [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 * Copyright (c) 2011-2012 Code Aurora Forum. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <limits.h>
19#include <unistd.h>
20#include <fcntl.h>
21#include <cutils/properties.h>
22#include <sys/mman.h>
23
24#include <genlock.h>
25
26#include "gr.h"
27#include "gpu.h"
28#include "memalloc.h"
29#include "alloc_controller.h"
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080030#include <qdMetaData.h>
Praveen Chavan4e931c12012-11-28 19:43:17 -080031#include "mdp_version.h"
Iliyan Malchev202a77d2012-06-11 14:41:12 -070032
33using namespace gralloc;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070034
Praveen Chavan4e931c12012-11-28 19:43:17 -080035#define SZ_1M 0x100000
36
Iliyan Malchev202a77d2012-06-11 14:41:12 -070037gpu_context_t::gpu_context_t(const private_module_t* module,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070038 IAllocController* alloc_ctrl ) :
Iliyan Malchev202a77d2012-06-11 14:41:12 -070039 mAllocCtrl(alloc_ctrl)
40{
41 // Zero out the alloc_device_t
42 memset(static_cast<alloc_device_t*>(this), 0, sizeof(alloc_device_t));
43
Iliyan Malchev202a77d2012-06-11 14:41:12 -070044 // Initialize the procs
45 common.tag = HARDWARE_DEVICE_TAG;
46 common.version = 0;
47 common.module = const_cast<hw_module_t*>(&module->base.common);
48 common.close = gralloc_close;
49 alloc = gralloc_alloc;
Ramkumar Radhakrishnan3d4c0ac2012-12-10 15:42:54 -080050 allocSize = gralloc_alloc_size;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070051 free = gralloc_free;
52
53}
54
55int gpu_context_t::gralloc_alloc_framebuffer_locked(size_t size, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -070056 buffer_handle_t* pHandle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070057{
58 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
59
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070060 // we don't support framebuffer allocations with graphics heap flags
61 if (usage & GRALLOC_HEAP_MASK) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -070062 return -EINVAL;
63 }
64
65 if (m->framebuffer == NULL) {
66 ALOGE("%s: Invalid framebuffer", __FUNCTION__);
67 return -EINVAL;
68 }
69
70 const uint32_t bufferMask = m->bufferMask;
71 const uint32_t numBuffers = m->numBuffers;
72 size_t bufferSize = m->finfo.line_length * m->info.yres;
73
74 //adreno needs FB size to be page aligned
75 bufferSize = roundUpToPageSize(bufferSize);
76
77 if (numBuffers == 1) {
78 // If we have only one buffer, we never use page-flipping. Instead,
79 // we return a regular buffer which will be memcpy'ed to the main
80 // screen when post is called.
81 int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
82 return gralloc_alloc_buffer(bufferSize, newUsage, pHandle, BUFFER_TYPE_UI,
83 m->fbFormat, m->info.xres, m->info.yres);
84 }
85
86 if (bufferMask >= ((1LU<<numBuffers)-1)) {
87 // We ran out of buffers.
88 return -ENOMEM;
89 }
90
Naseer Ahmedd7f84272012-12-13 13:09:53 -050091 // create a "fake" handle for it
Iliyan Malchev202a77d2012-06-11 14:41:12 -070092 intptr_t vaddr = intptr_t(m->framebuffer->base);
Naseer Ahmedd7f84272012-12-13 13:09:53 -050093 private_handle_t* hnd = new private_handle_t(
94 dup(m->framebuffer->fd), bufferSize,
95 private_handle_t::PRIV_FLAGS_USES_ION |
96 private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
97 BUFFER_TYPE_UI, m->fbFormat, m->info.xres,
98 m->info.yres);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070099
100 // find a free slot
101 for (uint32_t i=0 ; i<numBuffers ; i++) {
102 if ((bufferMask & (1LU<<i)) == 0) {
103 m->bufferMask |= (1LU<<i);
104 break;
105 }
106 vaddr += bufferSize;
107 }
108
109 hnd->base = vaddr;
110 hnd->offset = vaddr - intptr_t(m->framebuffer->base);
111 *pHandle = hnd;
112 return 0;
113}
114
115
116int gpu_context_t::gralloc_alloc_framebuffer(size_t size, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700117 buffer_handle_t* pHandle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700118{
119 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
120 pthread_mutex_lock(&m->lock);
121 int err = gralloc_alloc_framebuffer_locked(size, usage, pHandle);
122 pthread_mutex_unlock(&m->lock);
123 return err;
124}
125
126int gpu_context_t::gralloc_alloc_buffer(size_t size, int usage,
127 buffer_handle_t* pHandle, int bufferType,
128 int format, int width, int height)
129{
130 int err = 0;
131 int flags = 0;
132 size = roundUpToPageSize(size);
133 alloc_data data;
134 data.offset = 0;
135 data.fd = -1;
136 data.base = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700137 if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED)
138 data.align = 8192;
139 else
140 data.align = getpagesize();
Praveen Chavan4e931c12012-11-28 19:43:17 -0800141
142 /* force 1MB alignment selectively for secure buffers, MDP5 onwards */
143 if ((qdutils::MDPVersion::getInstance().getMDPVersion() >= \
144 qdutils::MDSS_V5) && (usage & GRALLOC_USAGE_PROTECTED)) {
145 data.align = ALIGN(data.align, SZ_1M);
146 size = ALIGN(size, data.align);
147 }
148 data.size = size;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700149 data.pHandle = (unsigned int) pHandle;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700150 err = mAllocCtrl->allocate(data, usage);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700151
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800152 if (!err) {
153 /* allocate memory for enhancement data */
154 alloc_data eData;
155 eData.fd = -1;
156 eData.base = 0;
157 eData.offset = 0;
158 eData.size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
159 eData.pHandle = data.pHandle;
160 eData.align = getpagesize();
161 int eDataUsage = GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP;
162 int eDataErr = mAllocCtrl->allocate(eData, eDataUsage);
163 ALOGE_IF(eDataErr, "gralloc failed for eDataErr=%s",
164 strerror(-eDataErr));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700165
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800166 if (usage & GRALLOC_USAGE_PRIVATE_UNSYNCHRONIZED) {
167 flags |= private_handle_t::PRIV_FLAGS_UNSYNCHRONIZED;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700168 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700169
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800170 if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY) {
171 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY;
172 //The EXTERNAL_BLOCK flag is always an add-on
173 if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_BLOCK) {
174 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_BLOCK;
175 }
176 if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_CC) {
177 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_CC;
178 }
179 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400180
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800181 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER ) {
182 flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
183 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400184
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800185 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
186 flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
187 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400188
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800189 if (usage & GRALLOC_USAGE_HW_CAMERA_READ) {
190 flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
191 }
192
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700193 flags |= data.allocType;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800194 int eBaseAddr = int(eData.base) + eData.offset;
195 private_handle_t *hnd = new private_handle_t(data.fd, size, flags,
196 bufferType, format, width, height, eData.fd, eData.offset,
197 eBaseAddr);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700198
199 hnd->offset = data.offset;
200 hnd->base = int(data.base) + data.offset;
201 *pHandle = hnd;
202 }
203
204 ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800205
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700206 return err;
207}
208
209void gpu_context_t::getGrallocInformationFromFormat(int inputFormat,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700210 int *bufferType)
211{
212 *bufferType = BUFFER_TYPE_VIDEO;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700213
Naseer Ahmed65f16562012-06-15 20:53:42 -0700214 if (inputFormat < 0x7) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700215 // RGB formats
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700216 *bufferType = BUFFER_TYPE_UI;
217 } else if ((inputFormat == HAL_PIXEL_FORMAT_R_8) ||
218 (inputFormat == HAL_PIXEL_FORMAT_RG_88)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700219 *bufferType = BUFFER_TYPE_UI;
220 }
221}
222
223int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700224 buffer_handle_t* pHandle, int* pStride,
225 size_t bufferSize) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700226 if (!pHandle || !pStride)
227 return -EINVAL;
228
229 size_t size;
230 int alignedw, alignedh;
Naseer Ahmed59802502012-08-21 17:03:27 -0400231 int grallocFormat = format;
232 int bufferType;
Naseer Ahmed59802502012-08-21 17:03:27 -0400233
Saurabh Shahaedf2362012-09-05 11:30:59 -0700234 //If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
235 //the usage bits, gralloc assigns a format.
236 if(format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
237 if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER)
238 grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP; //NV12
239 else if(usage & GRALLOC_USAGE_HW_CAMERA_READ)
240 grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
241 else if(usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
242 grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
243 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400244
Saurabh Shahaedf2362012-09-05 11:30:59 -0700245 getGrallocInformationFromFormat(grallocFormat, &bufferType);
Naseer Ahmed59802502012-08-21 17:03:27 -0400246 size = getBufferSizeAndDimensions(w, h, grallocFormat, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700247
248 if ((ssize_t)size <= 0)
249 return -EINVAL;
Ramkumar Radhakrishnan3d4c0ac2012-12-10 15:42:54 -0800250 size = (bufferSize != 0)? bufferSize : size;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700251
252 // All buffers marked as protected or for external
253 // display need to go to overlay
254 if ((usage & GRALLOC_USAGE_EXTERNAL_DISP) ||
Sushil Chauhan7651a802013-01-08 16:08:09 -0800255 (usage & GRALLOC_USAGE_PROTECTED)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700256 bufferType = BUFFER_TYPE_VIDEO;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700257 }
Naseer Ahmed768619e2012-11-28 17:02:52 -0500258
259 int err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
260 grallocFormat, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700261
262 if (err < 0) {
263 return err;
264 }
265
266 // Create a genlock lock for this buffer handle.
267 err = genlock_create_lock((native_handle_t*)(*pHandle));
268 if (err) {
269 ALOGE("%s: genlock_create_lock failed", __FUNCTION__);
270 free_impl(reinterpret_cast<private_handle_t*>(pHandle));
271 return err;
272 }
273 *pStride = alignedw;
274 return 0;
275}
276
277int gpu_context_t::free_impl(private_handle_t const* hnd) {
278 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
279 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
280 // free this buffer
281 const size_t bufferSize = m->finfo.line_length * m->info.yres;
282 int index = (hnd->base - m->framebuffer->base) / bufferSize;
283 m->bufferMask &= ~(1<<index);
284 } else {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700285 terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700286 IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700287 int err = memalloc->free_buffer((void*)hnd->base, (size_t) hnd->size,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700288 hnd->offset, hnd->fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700289 if(err)
290 return err;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800291 // free the metadata space
292 unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
293 err = memalloc->free_buffer((void*)hnd->base_metadata,
294 (size_t) size, hnd->offset_metadata,
295 hnd->fd_metadata);
296 if (err)
297 return err;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700298 }
299
300 // Release the genlock
301 int err = genlock_release_lock((native_handle_t*)hnd);
302 if (err) {
303 ALOGE("%s: genlock_release_lock failed", __FUNCTION__);
304 }
305
306 delete hnd;
307 return 0;
308}
309
310int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700311 int usage, buffer_handle_t* pHandle,
312 int* pStride)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700313{
314 if (!dev) {
315 return -EINVAL;
316 }
317 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
318 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0);
319}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700320int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h,
321 int format, int usage,
322 buffer_handle_t* pHandle, int* pStride,
323 int bufferSize)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700324{
325 if (!dev) {
326 return -EINVAL;
327 }
328 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
329 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize);
330}
331
332
333int gpu_context_t::gralloc_free(alloc_device_t* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700334 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700335{
336 if (private_handle_t::validate(handle) < 0)
337 return -EINVAL;
338
339 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
340 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
341 return gpu->free_impl(hnd);
342}
343
344/*****************************************************************************/
345
346int gpu_context_t::gralloc_close(struct hw_device_t *dev)
347{
348 gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev);
349 if (ctx) {
350 /* TODO: keep a list of all buffer_handle_t created, and free them
351 * all here.
352 */
353 delete ctx;
354 }
355 return 0;
356}
357