blob: 7b2c1d84fe5f4ed5b44e79a69d8a1a17a97abeed [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;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070047 free = gralloc_free;
48
49}
50
51int gpu_context_t::gralloc_alloc_framebuffer_locked(size_t size, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -070052 buffer_handle_t* pHandle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070053{
54 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
55
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070056 // we don't support framebuffer allocations with graphics heap flags
57 if (usage & GRALLOC_HEAP_MASK) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -070058 return -EINVAL;
59 }
60
61 if (m->framebuffer == NULL) {
62 ALOGE("%s: Invalid framebuffer", __FUNCTION__);
63 return -EINVAL;
64 }
65
66 const uint32_t bufferMask = m->bufferMask;
67 const uint32_t numBuffers = m->numBuffers;
68 size_t bufferSize = m->finfo.line_length * m->info.yres;
69
70 //adreno needs FB size to be page aligned
71 bufferSize = roundUpToPageSize(bufferSize);
72
73 if (numBuffers == 1) {
74 // If we have only one buffer, we never use page-flipping. Instead,
75 // we return a regular buffer which will be memcpy'ed to the main
76 // screen when post is called.
77 int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
78 return gralloc_alloc_buffer(bufferSize, newUsage, pHandle, BUFFER_TYPE_UI,
79 m->fbFormat, m->info.xres, m->info.yres);
80 }
81
82 if (bufferMask >= ((1LU<<numBuffers)-1)) {
83 // We ran out of buffers.
84 return -ENOMEM;
85 }
86
Naseer Ahmedd7f84272012-12-13 13:09:53 -050087 // create a "fake" handle for it
Iliyan Malchev202a77d2012-06-11 14:41:12 -070088 intptr_t vaddr = intptr_t(m->framebuffer->base);
Naseer Ahmedd7f84272012-12-13 13:09:53 -050089 private_handle_t* hnd = new private_handle_t(
90 dup(m->framebuffer->fd), bufferSize,
91 private_handle_t::PRIV_FLAGS_USES_ION |
92 private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
93 BUFFER_TYPE_UI, m->fbFormat, m->info.xres,
94 m->info.yres);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070095
96 // find a free slot
97 for (uint32_t i=0 ; i<numBuffers ; i++) {
98 if ((bufferMask & (1LU<<i)) == 0) {
99 m->bufferMask |= (1LU<<i);
100 break;
101 }
102 vaddr += bufferSize;
103 }
104
105 hnd->base = vaddr;
106 hnd->offset = vaddr - intptr_t(m->framebuffer->base);
107 *pHandle = hnd;
108 return 0;
109}
110
111
112int gpu_context_t::gralloc_alloc_framebuffer(size_t size, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700113 buffer_handle_t* pHandle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700114{
115 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
116 pthread_mutex_lock(&m->lock);
117 int err = gralloc_alloc_framebuffer_locked(size, usage, pHandle);
118 pthread_mutex_unlock(&m->lock);
119 return err;
120}
121
122int gpu_context_t::gralloc_alloc_buffer(size_t size, int usage,
123 buffer_handle_t* pHandle, int bufferType,
124 int format, int width, int height)
125{
126 int err = 0;
127 int flags = 0;
128 size = roundUpToPageSize(size);
129 alloc_data data;
130 data.offset = 0;
131 data.fd = -1;
132 data.base = 0;
133 data.size = size;
134 if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED)
135 data.align = 8192;
136 else
137 data.align = getpagesize();
138 data.pHandle = (unsigned int) pHandle;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700139 err = mAllocCtrl->allocate(data, usage);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700140
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800141 if (!err) {
142 /* allocate memory for enhancement data */
143 alloc_data eData;
144 eData.fd = -1;
145 eData.base = 0;
146 eData.offset = 0;
147 eData.size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
148 eData.pHandle = data.pHandle;
149 eData.align = getpagesize();
150 int eDataUsage = GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP;
151 int eDataErr = mAllocCtrl->allocate(eData, eDataUsage);
152 ALOGE_IF(eDataErr, "gralloc failed for eDataErr=%s",
153 strerror(-eDataErr));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700154
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800155 if (usage & GRALLOC_USAGE_PRIVATE_UNSYNCHRONIZED) {
156 flags |= private_handle_t::PRIV_FLAGS_UNSYNCHRONIZED;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700157 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700158
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800159 if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY) {
160 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY;
161 //The EXTERNAL_BLOCK flag is always an add-on
162 if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_BLOCK) {
163 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_BLOCK;
164 }
165 if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_CC) {
166 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_CC;
167 }
168 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400169
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800170 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER ) {
171 flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
172 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400173
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800174 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
175 flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
176 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400177
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800178 if (usage & GRALLOC_USAGE_HW_CAMERA_READ) {
179 flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
180 }
181
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700182 flags |= data.allocType;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800183 int eBaseAddr = int(eData.base) + eData.offset;
184 private_handle_t *hnd = new private_handle_t(data.fd, size, flags,
185 bufferType, format, width, height, eData.fd, eData.offset,
186 eBaseAddr);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700187
188 hnd->offset = data.offset;
189 hnd->base = int(data.base) + data.offset;
190 *pHandle = hnd;
191 }
192
193 ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800194
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700195 return err;
196}
197
198void gpu_context_t::getGrallocInformationFromFormat(int inputFormat,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700199 int *bufferType)
200{
201 *bufferType = BUFFER_TYPE_VIDEO;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700202
Naseer Ahmed65f16562012-06-15 20:53:42 -0700203 if (inputFormat < 0x7) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700204 // RGB formats
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700205 *bufferType = BUFFER_TYPE_UI;
206 } else if ((inputFormat == HAL_PIXEL_FORMAT_R_8) ||
207 (inputFormat == HAL_PIXEL_FORMAT_RG_88)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700208 *bufferType = BUFFER_TYPE_UI;
209 }
210}
211
212int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700213 buffer_handle_t* pHandle, int* pStride,
214 size_t bufferSize) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700215 if (!pHandle || !pStride)
216 return -EINVAL;
217
218 size_t size;
219 int alignedw, alignedh;
Naseer Ahmed59802502012-08-21 17:03:27 -0400220 int grallocFormat = format;
221 int bufferType;
Naseer Ahmed59802502012-08-21 17:03:27 -0400222
Saurabh Shahaedf2362012-09-05 11:30:59 -0700223 //If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
224 //the usage bits, gralloc assigns a format.
225 if(format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
226 if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER)
227 grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP; //NV12
228 else if(usage & GRALLOC_USAGE_HW_CAMERA_READ)
229 grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
230 else if(usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
231 grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
232 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400233
Saurabh Shahaedf2362012-09-05 11:30:59 -0700234 getGrallocInformationFromFormat(grallocFormat, &bufferType);
Naseer Ahmed59802502012-08-21 17:03:27 -0400235 size = getBufferSizeAndDimensions(w, h, grallocFormat, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700236
237 if ((ssize_t)size <= 0)
238 return -EINVAL;
239 size = (bufferSize >= size)? bufferSize : size;
240
241 // All buffers marked as protected or for external
242 // display need to go to overlay
243 if ((usage & GRALLOC_USAGE_EXTERNAL_DISP) ||
Naseer Ahmed29a26812012-06-14 00:56:20 -0700244 (usage & GRALLOC_USAGE_PROTECTED) ||
245 (usage & GRALLOC_USAGE_PRIVATE_CP_BUFFER)) {
246 bufferType = BUFFER_TYPE_VIDEO;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700247 }
Naseer Ahmed768619e2012-11-28 17:02:52 -0500248
249 int err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
250 grallocFormat, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700251
252 if (err < 0) {
253 return err;
254 }
255
256 // Create a genlock lock for this buffer handle.
257 err = genlock_create_lock((native_handle_t*)(*pHandle));
258 if (err) {
259 ALOGE("%s: genlock_create_lock failed", __FUNCTION__);
260 free_impl(reinterpret_cast<private_handle_t*>(pHandle));
261 return err;
262 }
263 *pStride = alignedw;
264 return 0;
265}
266
267int gpu_context_t::free_impl(private_handle_t const* hnd) {
268 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
269 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
270 // free this buffer
271 const size_t bufferSize = m->finfo.line_length * m->info.yres;
272 int index = (hnd->base - m->framebuffer->base) / bufferSize;
273 m->bufferMask &= ~(1<<index);
274 } else {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700275 terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700276 IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700277 int err = memalloc->free_buffer((void*)hnd->base, (size_t) hnd->size,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700278 hnd->offset, hnd->fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700279 if(err)
280 return err;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800281 // free the metadata space
282 unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
283 err = memalloc->free_buffer((void*)hnd->base_metadata,
284 (size_t) size, hnd->offset_metadata,
285 hnd->fd_metadata);
286 if (err)
287 return err;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700288 }
289
290 // Release the genlock
291 int err = genlock_release_lock((native_handle_t*)hnd);
292 if (err) {
293 ALOGE("%s: genlock_release_lock failed", __FUNCTION__);
294 }
295
296 delete hnd;
297 return 0;
298}
299
300int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700301 int usage, buffer_handle_t* pHandle,
302 int* pStride)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700303{
304 if (!dev) {
305 return -EINVAL;
306 }
307 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
308 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0);
309}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700310int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h,
311 int format, int usage,
312 buffer_handle_t* pHandle, int* pStride,
313 int bufferSize)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700314{
315 if (!dev) {
316 return -EINVAL;
317 }
318 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
319 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize);
320}
321
322
323int gpu_context_t::gralloc_free(alloc_device_t* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700324 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700325{
326 if (private_handle_t::validate(handle) < 0)
327 return -EINVAL;
328
329 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
330 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
331 return gpu->free_impl(hnd);
332}
333
334/*****************************************************************************/
335
336int gpu_context_t::gralloc_close(struct hw_device_t *dev)
337{
338 gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev);
339 if (ctx) {
340 /* TODO: keep a list of all buffer_handle_t created, and free them
341 * all here.
342 */
343 delete ctx;
344 }
345 return 0;
346}
347