blob: f11a5feb1860f8e4b3df9299e777e2fe4d609d49 [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>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070031
32using namespace gralloc;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070033
34gpu_context_t::gpu_context_t(const private_module_t* module,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070035 IAllocController* alloc_ctrl ) :
Iliyan Malchev202a77d2012-06-11 14:41:12 -070036 mAllocCtrl(alloc_ctrl)
37{
38 // Zero out the alloc_device_t
39 memset(static_cast<alloc_device_t*>(this), 0, sizeof(alloc_device_t));
40
Iliyan Malchev202a77d2012-06-11 14:41:12 -070041 // Initialize the procs
42 common.tag = HARDWARE_DEVICE_TAG;
43 common.version = 0;
44 common.module = const_cast<hw_module_t*>(&module->base.common);
45 common.close = gralloc_close;
46 alloc = gralloc_alloc;
Ramkumar Radhakrishnan3d4c0ac2012-12-10 15:42:54 -080047 allocSize = gralloc_alloc_size;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070048 free = gralloc_free;
49
50}
51
52int gpu_context_t::gralloc_alloc_framebuffer_locked(size_t size, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -070053 buffer_handle_t* pHandle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070054{
55 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
56
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070057 // we don't support framebuffer allocations with graphics heap flags
58 if (usage & GRALLOC_HEAP_MASK) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -070059 return -EINVAL;
60 }
61
62 if (m->framebuffer == NULL) {
63 ALOGE("%s: Invalid framebuffer", __FUNCTION__);
64 return -EINVAL;
65 }
66
67 const uint32_t bufferMask = m->bufferMask;
68 const uint32_t numBuffers = m->numBuffers;
69 size_t bufferSize = m->finfo.line_length * m->info.yres;
70
71 //adreno needs FB size to be page aligned
72 bufferSize = roundUpToPageSize(bufferSize);
73
74 if (numBuffers == 1) {
75 // If we have only one buffer, we never use page-flipping. Instead,
76 // we return a regular buffer which will be memcpy'ed to the main
77 // screen when post is called.
78 int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
79 return gralloc_alloc_buffer(bufferSize, newUsage, pHandle, BUFFER_TYPE_UI,
80 m->fbFormat, m->info.xres, m->info.yres);
81 }
82
83 if (bufferMask >= ((1LU<<numBuffers)-1)) {
84 // We ran out of buffers.
85 return -ENOMEM;
86 }
87
Naseer Ahmedd7f84272012-12-13 13:09:53 -050088 // create a "fake" handle for it
Iliyan Malchev202a77d2012-06-11 14:41:12 -070089 intptr_t vaddr = intptr_t(m->framebuffer->base);
Naseer Ahmedd7f84272012-12-13 13:09:53 -050090 private_handle_t* hnd = new private_handle_t(
91 dup(m->framebuffer->fd), bufferSize,
92 private_handle_t::PRIV_FLAGS_USES_ION |
93 private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
94 BUFFER_TYPE_UI, m->fbFormat, m->info.xres,
95 m->info.yres);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070096
97 // find a free slot
98 for (uint32_t i=0 ; i<numBuffers ; i++) {
99 if ((bufferMask & (1LU<<i)) == 0) {
100 m->bufferMask |= (1LU<<i);
101 break;
102 }
103 vaddr += bufferSize;
104 }
105
106 hnd->base = vaddr;
107 hnd->offset = vaddr - intptr_t(m->framebuffer->base);
108 *pHandle = hnd;
109 return 0;
110}
111
112
113int gpu_context_t::gralloc_alloc_framebuffer(size_t size, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700114 buffer_handle_t* pHandle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700115{
116 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
117 pthread_mutex_lock(&m->lock);
118 int err = gralloc_alloc_framebuffer_locked(size, usage, pHandle);
119 pthread_mutex_unlock(&m->lock);
120 return err;
121}
122
123int gpu_context_t::gralloc_alloc_buffer(size_t size, int usage,
124 buffer_handle_t* pHandle, int bufferType,
125 int format, int width, int height)
126{
127 int err = 0;
128 int flags = 0;
129 size = roundUpToPageSize(size);
130 alloc_data data;
131 data.offset = 0;
132 data.fd = -1;
133 data.base = 0;
134 data.size = size;
135 if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED)
136 data.align = 8192;
137 else
138 data.align = getpagesize();
139 data.pHandle = (unsigned int) pHandle;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700140 err = mAllocCtrl->allocate(data, usage);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700141
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800142 if (!err) {
143 /* allocate memory for enhancement data */
144 alloc_data eData;
145 eData.fd = -1;
146 eData.base = 0;
147 eData.offset = 0;
148 eData.size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
149 eData.pHandle = data.pHandle;
150 eData.align = getpagesize();
151 int eDataUsage = GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP;
152 int eDataErr = mAllocCtrl->allocate(eData, eDataUsage);
153 ALOGE_IF(eDataErr, "gralloc failed for eDataErr=%s",
154 strerror(-eDataErr));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700155
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800156 if (usage & GRALLOC_USAGE_PRIVATE_UNSYNCHRONIZED) {
157 flags |= private_handle_t::PRIV_FLAGS_UNSYNCHRONIZED;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700158 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700159
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800160 if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY) {
161 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY;
162 //The EXTERNAL_BLOCK flag is always an add-on
163 if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_BLOCK) {
164 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_BLOCK;
165 }
166 if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_CC) {
167 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_CC;
168 }
169 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400170
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800171 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER ) {
172 flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
173 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400174
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800175 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
176 flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
177 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400178
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800179 if (usage & GRALLOC_USAGE_HW_CAMERA_READ) {
180 flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
181 }
182
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700183 flags |= data.allocType;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800184 int eBaseAddr = int(eData.base) + eData.offset;
185 private_handle_t *hnd = new private_handle_t(data.fd, size, flags,
186 bufferType, format, width, height, eData.fd, eData.offset,
187 eBaseAddr);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700188
189 hnd->offset = data.offset;
190 hnd->base = int(data.base) + data.offset;
191 *pHandle = hnd;
192 }
193
194 ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800195
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700196 return err;
197}
198
199void gpu_context_t::getGrallocInformationFromFormat(int inputFormat,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700200 int *bufferType)
201{
202 *bufferType = BUFFER_TYPE_VIDEO;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700203
Naseer Ahmed65f16562012-06-15 20:53:42 -0700204 if (inputFormat < 0x7) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700205 // RGB formats
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700206 *bufferType = BUFFER_TYPE_UI;
207 } else if ((inputFormat == HAL_PIXEL_FORMAT_R_8) ||
208 (inputFormat == HAL_PIXEL_FORMAT_RG_88)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700209 *bufferType = BUFFER_TYPE_UI;
210 }
211}
212
213int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700214 buffer_handle_t* pHandle, int* pStride,
215 size_t bufferSize) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700216 if (!pHandle || !pStride)
217 return -EINVAL;
218
219 size_t size;
220 int alignedw, alignedh;
Naseer Ahmed59802502012-08-21 17:03:27 -0400221 int grallocFormat = format;
222 int bufferType;
Naseer Ahmed59802502012-08-21 17:03:27 -0400223
Saurabh Shahaedf2362012-09-05 11:30:59 -0700224 //If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
225 //the usage bits, gralloc assigns a format.
226 if(format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
227 if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER)
228 grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP; //NV12
229 else if(usage & GRALLOC_USAGE_HW_CAMERA_READ)
230 grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
231 else if(usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
232 grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
233 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400234
Saurabh Shahaedf2362012-09-05 11:30:59 -0700235 getGrallocInformationFromFormat(grallocFormat, &bufferType);
Naseer Ahmed59802502012-08-21 17:03:27 -0400236 size = getBufferSizeAndDimensions(w, h, grallocFormat, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700237
238 if ((ssize_t)size <= 0)
239 return -EINVAL;
Ramkumar Radhakrishnan3d4c0ac2012-12-10 15:42:54 -0800240 size = (bufferSize != 0)? bufferSize : size;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700241
242 // All buffers marked as protected or for external
243 // display need to go to overlay
244 if ((usage & GRALLOC_USAGE_EXTERNAL_DISP) ||
Naseer Ahmed29a26812012-06-14 00:56:20 -0700245 (usage & GRALLOC_USAGE_PROTECTED) ||
246 (usage & GRALLOC_USAGE_PRIVATE_CP_BUFFER)) {
247 bufferType = BUFFER_TYPE_VIDEO;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700248 }
Naseer Ahmed768619e2012-11-28 17:02:52 -0500249
250 int err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
251 grallocFormat, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700252
253 if (err < 0) {
254 return err;
255 }
256
257 // Create a genlock lock for this buffer handle.
258 err = genlock_create_lock((native_handle_t*)(*pHandle));
259 if (err) {
260 ALOGE("%s: genlock_create_lock failed", __FUNCTION__);
261 free_impl(reinterpret_cast<private_handle_t*>(pHandle));
262 return err;
263 }
264 *pStride = alignedw;
265 return 0;
266}
267
268int gpu_context_t::free_impl(private_handle_t const* hnd) {
269 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
270 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
271 // free this buffer
272 const size_t bufferSize = m->finfo.line_length * m->info.yres;
273 int index = (hnd->base - m->framebuffer->base) / bufferSize;
274 m->bufferMask &= ~(1<<index);
275 } else {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700276 terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700277 IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700278 int err = memalloc->free_buffer((void*)hnd->base, (size_t) hnd->size,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700279 hnd->offset, hnd->fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700280 if(err)
281 return err;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800282 // free the metadata space
283 unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
284 err = memalloc->free_buffer((void*)hnd->base_metadata,
285 (size_t) size, hnd->offset_metadata,
286 hnd->fd_metadata);
287 if (err)
288 return err;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700289 }
290
291 // Release the genlock
292 int err = genlock_release_lock((native_handle_t*)hnd);
293 if (err) {
294 ALOGE("%s: genlock_release_lock failed", __FUNCTION__);
295 }
296
297 delete hnd;
298 return 0;
299}
300
301int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700302 int usage, buffer_handle_t* pHandle,
303 int* pStride)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700304{
305 if (!dev) {
306 return -EINVAL;
307 }
308 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
309 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0);
310}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700311int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h,
312 int format, int usage,
313 buffer_handle_t* pHandle, int* pStride,
314 int bufferSize)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700315{
316 if (!dev) {
317 return -EINVAL;
318 }
319 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
320 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize);
321}
322
323
324int gpu_context_t::gralloc_free(alloc_device_t* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700325 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700326{
327 if (private_handle_t::validate(handle) < 0)
328 return -EINVAL;
329
330 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
331 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
332 return gpu->free_impl(hnd);
333}
334
335/*****************************************************************************/
336
337int gpu_context_t::gralloc_close(struct hw_device_t *dev)
338{
339 gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev);
340 if (ctx) {
341 /* TODO: keep a list of all buffer_handle_t created, and free them
342 * all here.
343 */
344 delete ctx;
345 }
346 return 0;
347}
348