blob: 197c37259ecff257382bf3500fc38356a7f4c971 [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
Naseer Ahmed29a26812012-06-14 00:56:20 -07003 * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Iliyan Malchev202a77d2012-06-11 14:41:12 -07004 *
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 <errno.h>
20#include <pthread.h>
21#include <unistd.h>
22#include <string.h>
23#include <stdarg.h>
24
25#include <sys/mman.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28#include <sys/ioctl.h>
29#include <linux/ashmem.h>
30
31#include <cutils/log.h>
32#include <cutils/atomic.h>
33#include <cutils/ashmem.h>
34
35#include <hardware/hardware.h>
36#include <hardware/gralloc.h>
37#include <genlock.h>
38
39#include <linux/android_pmem.h>
40
41#include "gralloc_priv.h"
42#include "gr.h"
43#include "alloc_controller.h"
44#include "memalloc.h"
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080045#include <qdMetaData.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070046
47using namespace gralloc;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070048/*****************************************************************************/
49
50// Return the type of allocator -
51// these are used for mapping/unmapping
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070052static IMemAlloc* getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070053{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070054 IMemAlloc* memalloc;
55 IAllocController* alloc_ctrl = IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -070056 memalloc = alloc_ctrl->getAllocator(flags);
57 return memalloc;
58}
59
60static int gralloc_map(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -070061 buffer_handle_t handle,
62 void** vaddr)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070063{
64 private_handle_t* hnd = (private_handle_t*)handle;
65 void *mappedAddress;
66 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) &&
67 !(hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER)) {
68 size_t size = hnd->size;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070069 IMemAlloc* memalloc = getAllocator(hnd->flags) ;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070070 int err = memalloc->map_buffer(&mappedAddress, size,
Naseer Ahmed29a26812012-06-14 00:56:20 -070071 hnd->offset, hnd->fd);
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080072 if(err || mappedAddress == MAP_FAILED) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -070073 ALOGE("Could not mmap handle %p, fd=%d (%s)",
Naseer Ahmed29a26812012-06-14 00:56:20 -070074 handle, hnd->fd, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -070075 hnd->base = 0;
76 return -errno;
77 }
78
Iliyan Malchev202a77d2012-06-11 14:41:12 -070079 hnd->base = intptr_t(mappedAddress) + hnd->offset;
Naseer Ahmed29a26812012-06-14 00:56:20 -070080 //LOGD("gralloc_map() succeeded fd=%d, off=%d, size=%d, vaddr=%p",
Iliyan Malchev202a77d2012-06-11 14:41:12 -070081 // hnd->fd, hnd->offset, hnd->size, mappedAddress);
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080082 mappedAddress = MAP_FAILED;
83 size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
84 err = memalloc->map_buffer(&mappedAddress, size,
85 hnd->offset_metadata, hnd->fd_metadata);
86 if(err || mappedAddress == MAP_FAILED) {
87 ALOGE("Could not mmap handle %p, fd=%d (%s)",
88 handle, hnd->fd_metadata, strerror(errno));
89 hnd->base_metadata = 0;
90 return -errno;
91 }
92 hnd->base_metadata = intptr_t(mappedAddress) + hnd->offset_metadata;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070093 }
94 *vaddr = (void*)hnd->base;
95 return 0;
96}
97
98static int gralloc_unmap(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -070099 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700100{
101 private_handle_t* hnd = (private_handle_t*)handle;
102 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
103 int err = -EINVAL;
104 void* base = (void*)hnd->base;
105 size_t size = hnd->size;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700106 IMemAlloc* memalloc = getAllocator(hnd->flags) ;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800107 if(memalloc != NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700108 err = memalloc->unmap_buffer(base, size, hnd->offset);
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800109 if (err) {
110 ALOGE("Could not unmap memory at address %p", base);
111 }
112 base = (void*)hnd->base_metadata;
113 size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
114 err = memalloc->unmap_buffer(base, size, hnd->offset_metadata);
115 if (err) {
116 ALOGE("Could not unmap memory at address %p", base);
117 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700118 }
119 }
120 hnd->base = 0;
121 return 0;
122}
123
124/*****************************************************************************/
125
126static pthread_mutex_t sMapLock = PTHREAD_MUTEX_INITIALIZER;
127
128/*****************************************************************************/
129
130int gralloc_register_buffer(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700131 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700132{
133 if (private_handle_t::validate(handle) < 0)
134 return -EINVAL;
135
136 // In this implementation, we don't need to do anything here
137
138 /* NOTE: we need to initialize the buffer as not mapped/not locked
139 * because it shouldn't when this function is called the first time
140 * in a new process. Ideally these flags shouldn't be part of the
141 * handle, but instead maintained in the kernel or at least
142 * out-of-line
143 */
144
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700145 private_handle_t* hnd = (private_handle_t*)handle;
Kinjal Bhavsar139e1622012-09-10 12:35:11 -0700146 hnd->base = 0;
147 void *vaddr;
148 int err = gralloc_map(module, handle, &vaddr);
149 if (err) {
150 ALOGE("%s: gralloc_map failed", __FUNCTION__);
151 return err;
152 }
153
154 // Reset the genlock private fd flag in the handle
155 hnd->genlockPrivFd = -1;
156
157 // Check if there is a valid lock attached to the handle.
158 if (-1 == hnd->genlockHandle) {
159 ALOGE("%s: the lock is invalid.", __FUNCTION__);
160 gralloc_unmap(module, handle);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700161 hnd->base = 0;
Kinjal Bhavsar139e1622012-09-10 12:35:11 -0700162 return -EINVAL;
163 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700164
Kinjal Bhavsar139e1622012-09-10 12:35:11 -0700165 // Attach the genlock handle
166 if (GENLOCK_NO_ERROR != genlock_attach_lock((native_handle_t *)handle)) {
167 ALOGE("%s: genlock_attach_lock failed", __FUNCTION__);
168 gralloc_unmap(module, handle);
169 hnd->base = 0;
170 return -EINVAL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700171 }
172 return 0;
173}
174
175int gralloc_unregister_buffer(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700176 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700177{
178 if (private_handle_t::validate(handle) < 0)
179 return -EINVAL;
180
181 /*
182 * If the buffer has been mapped during a lock operation, it's time
183 * to un-map it. It's an error to be here with a locked buffer.
184 * NOTE: the framebuffer is handled differently and is never unmapped.
185 */
186
187 private_handle_t* hnd = (private_handle_t*)handle;
188
Kinjal Bhavsar139e1622012-09-10 12:35:11 -0700189 if (hnd->base != 0) {
190 gralloc_unmap(module, handle);
191 }
192 hnd->base = 0;
193 // Release the genlock
194 if (-1 != hnd->genlockHandle) {
195 return genlock_release_lock((native_handle_t *)handle);
196 } else {
197 ALOGE("%s: there was no genlock attached to this buffer", __FUNCTION__);
198 return -EINVAL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700199 }
200 return 0;
201}
202
203int terminateBuffer(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700204 private_handle_t* hnd)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700205{
206 /*
207 * If the buffer has been mapped during a lock operation, it's time
208 * to un-map it. It's an error to be here with a locked buffer.
209 */
210
211 if (hnd->base != 0) {
212 // this buffer was mapped, unmap it now
213 if (hnd->flags & (private_handle_t::PRIV_FLAGS_USES_PMEM |
214 private_handle_t::PRIV_FLAGS_USES_PMEM_ADSP |
215 private_handle_t::PRIV_FLAGS_USES_ASHMEM |
216 private_handle_t::PRIV_FLAGS_USES_ION)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700217 gralloc_unmap(module, hnd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700218 } else {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700219 ALOGE("terminateBuffer: unmapping a non pmem/ashmem buffer flags = 0x%x",
220 hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700221 gralloc_unmap(module, hnd);
222 }
223 }
224
225 return 0;
226}
227
228int gralloc_lock(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700229 buffer_handle_t handle, int usage,
230 int l, int t, int w, int h,
231 void** vaddr)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700232{
233 if (private_handle_t::validate(handle) < 0)
234 return -EINVAL;
235
236 int err = 0;
237 private_handle_t* hnd = (private_handle_t*)handle;
238 if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
239 if (hnd->base == 0) {
240 // we need to map for real
241 pthread_mutex_t* const lock = &sMapLock;
242 pthread_mutex_lock(lock);
243 err = gralloc_map(module, handle, vaddr);
244 pthread_mutex_unlock(lock);
245 }
246 *vaddr = (void*)hnd->base;
247
248 // Lock the buffer for read/write operation as specified. Write lock
249 // has a higher priority over read lock.
250 int lockType = 0;
251 if (usage & GRALLOC_USAGE_SW_WRITE_MASK) {
252 lockType = GENLOCK_WRITE_LOCK;
253 } else if (usage & GRALLOC_USAGE_SW_READ_MASK) {
254 lockType = GENLOCK_READ_LOCK;
255 }
256
257 int timeout = GENLOCK_MAX_TIMEOUT;
258 if (GENLOCK_NO_ERROR != genlock_lock_buffer((native_handle_t *)handle,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700259 (genlock_lock_type)lockType,
260 timeout)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700261 ALOGE("%s: genlock_lock_buffer (lockType=0x%x) failed", __FUNCTION__,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700262 lockType);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700263 return -EINVAL;
264 } else {
265 // Mark this buffer as locked for SW read/write operation.
266 hnd->flags |= private_handle_t::PRIV_FLAGS_SW_LOCK;
267 }
268
269 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) &&
270 !(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
271 // Mark the buffer to be flushed after cpu read/write
272 hnd->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
273 }
274 }
275 return err;
276}
277
278int gralloc_unlock(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700279 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700280{
281 if (private_handle_t::validate(handle) < 0)
282 return -EINVAL;
283
284 private_handle_t* hnd = (private_handle_t*)handle;
285
286 if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
287 int err;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700288 IMemAlloc* memalloc = getAllocator(hnd->flags) ;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700289 err = memalloc->clean_buffer((void*)hnd->base,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700290 hnd->size, hnd->offset, hnd->fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700291 ALOGE_IF(err < 0, "cannot flush handle %p (offs=%x len=%x, flags = 0x%x) err=%s\n",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700292 hnd, hnd->offset, hnd->size, hnd->flags, strerror(errno));
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800293 unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
294 err = memalloc->clean_buffer((void*)hnd->base_metadata, size,
295 hnd->offset_metadata, hnd->fd_metadata);
296 ALOGE_IF(err < 0, "cannot flush handle %p (offs=%x len=%lu, "
297 "flags = 0x%x) err=%s\n", hnd, hnd->offset_metadata, size,
298 hnd->flags, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700299 hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
300 }
301
302 if ((hnd->flags & private_handle_t::PRIV_FLAGS_SW_LOCK)) {
303 // Unlock the buffer.
304 if (GENLOCK_NO_ERROR != genlock_unlock_buffer((native_handle_t *)handle)) {
305 ALOGE("%s: genlock_unlock_buffer failed", __FUNCTION__);
306 return -EINVAL;
307 } else
308 hnd->flags &= ~private_handle_t::PRIV_FLAGS_SW_LOCK;
309 }
310 return 0;
311}
312
313/*****************************************************************************/
314
315int gralloc_perform(struct gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700316 int operation, ... )
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700317{
318 int res = -EINVAL;
319 va_list args;
320 va_start(args, operation);
321 switch (operation) {
322 case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER:
323 {
324 int fd = va_arg(args, int);
325 size_t size = va_arg(args, size_t);
326 size_t offset = va_arg(args, size_t);
327 void* base = va_arg(args, void*);
328 int width = va_arg(args, int);
329 int height = va_arg(args, int);
330 int format = va_arg(args, int);
331
332 native_handle_t** handle = va_arg(args, native_handle_t**);
333 int memoryFlags = va_arg(args, int);
334 private_handle_t* hnd = (private_handle_t*)native_handle_create(
Naseer Ahmed29a26812012-06-14 00:56:20 -0700335 private_handle_t::sNumFds, private_handle_t::sNumInts);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700336 hnd->magic = private_handle_t::sMagic;
337 hnd->fd = fd;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700338 hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700339 hnd->size = size;
340 hnd->offset = offset;
341 hnd->base = intptr_t(base) + offset;
342 hnd->gpuaddr = 0;
343 hnd->width = width;
344 hnd->height = height;
345 hnd->format = format;
346 *handle = (native_handle_t *)hnd;
347 res = 0;
348 break;
349
350 }
Ramkumar Radhakrishnan935cb682012-11-07 16:43:38 -0800351 case GRALLOC_MODULE_PERFORM_UPDATE_BUFFER_GEOMETRY:
352 {
353 int width = va_arg(args, int);
354 int height = va_arg(args, int);
355 int format = va_arg(args, int);
356 private_handle_t* hnd = va_arg(args, private_handle_t*);
357 if (private_handle_t::validate(hnd)) {
358 return res;
359 }
360 hnd->width = width;
361 hnd->height = height;
362 hnd->format = format;
363 res = 0;
364 }
365 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700366 default:
367 break;
368 }
369 va_end(args);
370 return res;
371}