blob: 99f9f08d8c502e3f0d306e100b5d30161c4eb2fe [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"
45
46using namespace gralloc;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070047/*****************************************************************************/
48
49// Return the type of allocator -
50// these are used for mapping/unmapping
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070051static IMemAlloc* getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070052{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070053 IMemAlloc* memalloc;
54 IAllocController* alloc_ctrl = IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -070055 memalloc = alloc_ctrl->getAllocator(flags);
56 return memalloc;
57}
58
59static int gralloc_map(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -070060 buffer_handle_t handle,
61 void** vaddr)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070062{
63 private_handle_t* hnd = (private_handle_t*)handle;
64 void *mappedAddress;
65 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) &&
66 !(hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER)) {
67 size_t size = hnd->size;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070068 IMemAlloc* memalloc = getAllocator(hnd->flags) ;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070069 int err = memalloc->map_buffer(&mappedAddress, size,
Naseer Ahmed29a26812012-06-14 00:56:20 -070070 hnd->offset, hnd->fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070071 if(err) {
72 ALOGE("Could not mmap handle %p, fd=%d (%s)",
Naseer Ahmed29a26812012-06-14 00:56:20 -070073 handle, hnd->fd, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -070074 hnd->base = 0;
75 return -errno;
76 }
77
78 if (mappedAddress == MAP_FAILED) {
79 ALOGE("Could not mmap handle %p, fd=%d (%s)",
Naseer Ahmed29a26812012-06-14 00:56:20 -070080 handle, hnd->fd, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -070081 hnd->base = 0;
82 return -errno;
83 }
84 hnd->base = intptr_t(mappedAddress) + hnd->offset;
Naseer Ahmed29a26812012-06-14 00:56:20 -070085 //LOGD("gralloc_map() succeeded fd=%d, off=%d, size=%d, vaddr=%p",
Iliyan Malchev202a77d2012-06-11 14:41:12 -070086 // hnd->fd, hnd->offset, hnd->size, mappedAddress);
87 }
88 *vaddr = (void*)hnd->base;
89 return 0;
90}
91
92static int gralloc_unmap(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -070093 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070094{
95 private_handle_t* hnd = (private_handle_t*)handle;
96 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
97 int err = -EINVAL;
98 void* base = (void*)hnd->base;
99 size_t size = hnd->size;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700100 IMemAlloc* memalloc = getAllocator(hnd->flags) ;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700101 if(memalloc != NULL)
102 err = memalloc->unmap_buffer(base, size, hnd->offset);
103 if (err) {
104 ALOGE("Could not unmap memory at address %p", base);
105 }
106 }
107 hnd->base = 0;
108 return 0;
109}
110
111/*****************************************************************************/
112
113static pthread_mutex_t sMapLock = PTHREAD_MUTEX_INITIALIZER;
114
115/*****************************************************************************/
116
117int gralloc_register_buffer(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700118 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700119{
120 if (private_handle_t::validate(handle) < 0)
121 return -EINVAL;
122
123 // In this implementation, we don't need to do anything here
124
125 /* NOTE: we need to initialize the buffer as not mapped/not locked
126 * because it shouldn't when this function is called the first time
127 * in a new process. Ideally these flags shouldn't be part of the
128 * handle, but instead maintained in the kernel or at least
129 * out-of-line
130 */
131
132 // if this handle was created in this process, then we keep it as is.
133 private_handle_t* hnd = (private_handle_t*)handle;
134 if (hnd->pid != getpid()) {
135 hnd->base = 0;
136 void *vaddr;
137 int err = gralloc_map(module, handle, &vaddr);
138 if (err) {
139 ALOGE("%s: gralloc_map failed", __FUNCTION__);
140 return err;
141 }
142
143 // Reset the genlock private fd flag in the handle
144 hnd->genlockPrivFd = -1;
145
146 // Check if there is a valid lock attached to the handle.
147 if (-1 == hnd->genlockHandle) {
148 ALOGE("%s: the lock is invalid.", __FUNCTION__);
149 gralloc_unmap(module, handle);
150 hnd->base = 0;
151 return -EINVAL;
152 }
153
154 // Attach the genlock handle
155 if (GENLOCK_NO_ERROR != genlock_attach_lock((native_handle_t *)handle)) {
156 ALOGE("%s: genlock_attach_lock failed", __FUNCTION__);
157 gralloc_unmap(module, handle);
158 hnd->base = 0;
159 return -EINVAL;
160 }
161 }
162 return 0;
163}
164
165int gralloc_unregister_buffer(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700166 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700167{
168 if (private_handle_t::validate(handle) < 0)
169 return -EINVAL;
170
171 /*
172 * If the buffer has been mapped during a lock operation, it's time
173 * to un-map it. It's an error to be here with a locked buffer.
174 * NOTE: the framebuffer is handled differently and is never unmapped.
175 */
176
177 private_handle_t* hnd = (private_handle_t*)handle;
178
179 // never unmap buffers that were created in this process
180 if (hnd->pid != getpid()) {
181 if (hnd->base != 0) {
182 gralloc_unmap(module, handle);
183 }
184 hnd->base = 0;
185 // Release the genlock
186 if (-1 != hnd->genlockHandle) {
187 return genlock_release_lock((native_handle_t *)handle);
188 } else {
189 ALOGE("%s: there was no genlock attached to this buffer", __FUNCTION__);
190 return -EINVAL;
191 }
192 }
193 return 0;
194}
195
196int terminateBuffer(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700197 private_handle_t* hnd)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700198{
199 /*
200 * If the buffer has been mapped during a lock operation, it's time
201 * to un-map it. It's an error to be here with a locked buffer.
202 */
203
204 if (hnd->base != 0) {
205 // this buffer was mapped, unmap it now
206 if (hnd->flags & (private_handle_t::PRIV_FLAGS_USES_PMEM |
207 private_handle_t::PRIV_FLAGS_USES_PMEM_ADSP |
208 private_handle_t::PRIV_FLAGS_USES_ASHMEM |
209 private_handle_t::PRIV_FLAGS_USES_ION)) {
210 if (hnd->pid != getpid()) {
211 // ... unless it's a "master" pmem buffer, that is a buffer
212 // mapped in the process it's been allocated.
213 // (see gralloc_alloc_buffer())
214 gralloc_unmap(module, hnd);
215 }
216 } else {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700217 ALOGE("terminateBuffer: unmapping a non pmem/ashmem buffer flags = 0x%x",
218 hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700219 gralloc_unmap(module, hnd);
220 }
221 }
222
223 return 0;
224}
225
226int gralloc_lock(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700227 buffer_handle_t handle, int usage,
228 int l, int t, int w, int h,
229 void** vaddr)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700230{
231 if (private_handle_t::validate(handle) < 0)
232 return -EINVAL;
233
234 int err = 0;
235 private_handle_t* hnd = (private_handle_t*)handle;
236 if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
237 if (hnd->base == 0) {
238 // we need to map for real
239 pthread_mutex_t* const lock = &sMapLock;
240 pthread_mutex_lock(lock);
241 err = gralloc_map(module, handle, vaddr);
242 pthread_mutex_unlock(lock);
243 }
244 *vaddr = (void*)hnd->base;
245
246 // Lock the buffer for read/write operation as specified. Write lock
247 // has a higher priority over read lock.
248 int lockType = 0;
249 if (usage & GRALLOC_USAGE_SW_WRITE_MASK) {
250 lockType = GENLOCK_WRITE_LOCK;
251 } else if (usage & GRALLOC_USAGE_SW_READ_MASK) {
252 lockType = GENLOCK_READ_LOCK;
253 }
254
255 int timeout = GENLOCK_MAX_TIMEOUT;
256 if (GENLOCK_NO_ERROR != genlock_lock_buffer((native_handle_t *)handle,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700257 (genlock_lock_type)lockType,
258 timeout)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700259 ALOGE("%s: genlock_lock_buffer (lockType=0x%x) failed", __FUNCTION__,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700260 lockType);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700261 return -EINVAL;
262 } else {
263 // Mark this buffer as locked for SW read/write operation.
264 hnd->flags |= private_handle_t::PRIV_FLAGS_SW_LOCK;
265 }
266
267 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) &&
268 !(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
269 // Mark the buffer to be flushed after cpu read/write
270 hnd->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
271 }
272 }
273 return err;
274}
275
276int gralloc_unlock(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700277 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700278{
279 if (private_handle_t::validate(handle) < 0)
280 return -EINVAL;
281
282 private_handle_t* hnd = (private_handle_t*)handle;
283
284 if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
285 int err;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700286 IMemAlloc* memalloc = getAllocator(hnd->flags) ;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700287 err = memalloc->clean_buffer((void*)hnd->base,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700288 hnd->size, hnd->offset, hnd->fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700289 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 -0700290 hnd, hnd->offset, hnd->size, hnd->flags, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700291 hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
292 }
293
294 if ((hnd->flags & private_handle_t::PRIV_FLAGS_SW_LOCK)) {
295 // Unlock the buffer.
296 if (GENLOCK_NO_ERROR != genlock_unlock_buffer((native_handle_t *)handle)) {
297 ALOGE("%s: genlock_unlock_buffer failed", __FUNCTION__);
298 return -EINVAL;
299 } else
300 hnd->flags &= ~private_handle_t::PRIV_FLAGS_SW_LOCK;
301 }
302 return 0;
303}
304
305/*****************************************************************************/
306
307int gralloc_perform(struct gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700308 int operation, ... )
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700309{
310 int res = -EINVAL;
311 va_list args;
312 va_start(args, operation);
313 switch (operation) {
314 case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER:
315 {
316 int fd = va_arg(args, int);
317 size_t size = va_arg(args, size_t);
318 size_t offset = va_arg(args, size_t);
319 void* base = va_arg(args, void*);
320 int width = va_arg(args, int);
321 int height = va_arg(args, int);
322 int format = va_arg(args, int);
323
324 native_handle_t** handle = va_arg(args, native_handle_t**);
325 int memoryFlags = va_arg(args, int);
326 private_handle_t* hnd = (private_handle_t*)native_handle_create(
Naseer Ahmed29a26812012-06-14 00:56:20 -0700327 private_handle_t::sNumFds, private_handle_t::sNumInts);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700328 hnd->magic = private_handle_t::sMagic;
329 hnd->fd = fd;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700330 hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700331 hnd->size = size;
332 hnd->offset = offset;
333 hnd->base = intptr_t(base) + offset;
334 hnd->gpuaddr = 0;
335 hnd->width = width;
336 hnd->height = height;
337 hnd->format = format;
338 *handle = (native_handle_t *)hnd;
339 res = 0;
340 break;
341
342 }
343 default:
344 break;
345 }
346 va_end(args);
347 return res;
348}