blob: 3088ecc72b2bfc16c2a083d37f7d459a503edc5c [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
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700132 private_handle_t* hnd = (private_handle_t*)handle;
Kinjal Bhavsar139e1622012-09-10 12:35:11 -0700133 hnd->base = 0;
134 void *vaddr;
135 int err = gralloc_map(module, handle, &vaddr);
136 if (err) {
137 ALOGE("%s: gralloc_map failed", __FUNCTION__);
138 return err;
139 }
140
141 // Reset the genlock private fd flag in the handle
142 hnd->genlockPrivFd = -1;
143
144 // Check if there is a valid lock attached to the handle.
145 if (-1 == hnd->genlockHandle) {
146 ALOGE("%s: the lock is invalid.", __FUNCTION__);
147 gralloc_unmap(module, handle);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700148 hnd->base = 0;
Kinjal Bhavsar139e1622012-09-10 12:35:11 -0700149 return -EINVAL;
150 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700151
Kinjal Bhavsar139e1622012-09-10 12:35:11 -0700152 // Attach the genlock handle
153 if (GENLOCK_NO_ERROR != genlock_attach_lock((native_handle_t *)handle)) {
154 ALOGE("%s: genlock_attach_lock failed", __FUNCTION__);
155 gralloc_unmap(module, handle);
156 hnd->base = 0;
157 return -EINVAL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700158 }
159 return 0;
160}
161
162int gralloc_unregister_buffer(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700163 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700164{
165 if (private_handle_t::validate(handle) < 0)
166 return -EINVAL;
167
168 /*
169 * If the buffer has been mapped during a lock operation, it's time
170 * to un-map it. It's an error to be here with a locked buffer.
171 * NOTE: the framebuffer is handled differently and is never unmapped.
172 */
173
174 private_handle_t* hnd = (private_handle_t*)handle;
175
Kinjal Bhavsar139e1622012-09-10 12:35:11 -0700176 if (hnd->base != 0) {
177 gralloc_unmap(module, handle);
178 }
179 hnd->base = 0;
180 // Release the genlock
181 if (-1 != hnd->genlockHandle) {
182 return genlock_release_lock((native_handle_t *)handle);
183 } else {
184 ALOGE("%s: there was no genlock attached to this buffer", __FUNCTION__);
185 return -EINVAL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700186 }
187 return 0;
188}
189
190int terminateBuffer(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700191 private_handle_t* hnd)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700192{
193 /*
194 * If the buffer has been mapped during a lock operation, it's time
195 * to un-map it. It's an error to be here with a locked buffer.
196 */
197
198 if (hnd->base != 0) {
199 // this buffer was mapped, unmap it now
200 if (hnd->flags & (private_handle_t::PRIV_FLAGS_USES_PMEM |
201 private_handle_t::PRIV_FLAGS_USES_PMEM_ADSP |
202 private_handle_t::PRIV_FLAGS_USES_ASHMEM |
203 private_handle_t::PRIV_FLAGS_USES_ION)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700204 gralloc_unmap(module, hnd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700205 } else {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700206 ALOGE("terminateBuffer: unmapping a non pmem/ashmem buffer flags = 0x%x",
207 hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700208 gralloc_unmap(module, hnd);
209 }
210 }
211
212 return 0;
213}
214
215int gralloc_lock(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700216 buffer_handle_t handle, int usage,
217 int l, int t, int w, int h,
218 void** vaddr)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700219{
220 if (private_handle_t::validate(handle) < 0)
221 return -EINVAL;
222
223 int err = 0;
224 private_handle_t* hnd = (private_handle_t*)handle;
225 if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
226 if (hnd->base == 0) {
227 // we need to map for real
228 pthread_mutex_t* const lock = &sMapLock;
229 pthread_mutex_lock(lock);
230 err = gralloc_map(module, handle, vaddr);
231 pthread_mutex_unlock(lock);
232 }
233 *vaddr = (void*)hnd->base;
234
235 // Lock the buffer for read/write operation as specified. Write lock
236 // has a higher priority over read lock.
237 int lockType = 0;
238 if (usage & GRALLOC_USAGE_SW_WRITE_MASK) {
239 lockType = GENLOCK_WRITE_LOCK;
240 } else if (usage & GRALLOC_USAGE_SW_READ_MASK) {
241 lockType = GENLOCK_READ_LOCK;
242 }
243
244 int timeout = GENLOCK_MAX_TIMEOUT;
245 if (GENLOCK_NO_ERROR != genlock_lock_buffer((native_handle_t *)handle,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700246 (genlock_lock_type)lockType,
247 timeout)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700248 ALOGE("%s: genlock_lock_buffer (lockType=0x%x) failed", __FUNCTION__,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700249 lockType);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700250 return -EINVAL;
251 } else {
252 // Mark this buffer as locked for SW read/write operation.
253 hnd->flags |= private_handle_t::PRIV_FLAGS_SW_LOCK;
254 }
255
256 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) &&
257 !(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
258 // Mark the buffer to be flushed after cpu read/write
259 hnd->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
260 }
261 }
262 return err;
263}
264
265int gralloc_unlock(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700266 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700267{
268 if (private_handle_t::validate(handle) < 0)
269 return -EINVAL;
270
271 private_handle_t* hnd = (private_handle_t*)handle;
272
273 if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
274 int err;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700275 IMemAlloc* memalloc = getAllocator(hnd->flags) ;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700276 err = memalloc->clean_buffer((void*)hnd->base,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700277 hnd->size, hnd->offset, hnd->fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700278 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 -0700279 hnd, hnd->offset, hnd->size, hnd->flags, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700280 hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
281 }
282
283 if ((hnd->flags & private_handle_t::PRIV_FLAGS_SW_LOCK)) {
284 // Unlock the buffer.
285 if (GENLOCK_NO_ERROR != genlock_unlock_buffer((native_handle_t *)handle)) {
286 ALOGE("%s: genlock_unlock_buffer failed", __FUNCTION__);
287 return -EINVAL;
288 } else
289 hnd->flags &= ~private_handle_t::PRIV_FLAGS_SW_LOCK;
290 }
291 return 0;
292}
293
294/*****************************************************************************/
295
296int gralloc_perform(struct gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700297 int operation, ... )
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700298{
299 int res = -EINVAL;
300 va_list args;
301 va_start(args, operation);
302 switch (operation) {
303 case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER:
304 {
305 int fd = va_arg(args, int);
306 size_t size = va_arg(args, size_t);
307 size_t offset = va_arg(args, size_t);
308 void* base = va_arg(args, void*);
309 int width = va_arg(args, int);
310 int height = va_arg(args, int);
311 int format = va_arg(args, int);
312
313 native_handle_t** handle = va_arg(args, native_handle_t**);
314 int memoryFlags = va_arg(args, int);
315 private_handle_t* hnd = (private_handle_t*)native_handle_create(
Naseer Ahmed29a26812012-06-14 00:56:20 -0700316 private_handle_t::sNumFds, private_handle_t::sNumInts);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700317 hnd->magic = private_handle_t::sMagic;
318 hnd->fd = fd;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700319 hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700320 hnd->size = size;
321 hnd->offset = offset;
322 hnd->base = intptr_t(base) + offset;
323 hnd->gpuaddr = 0;
324 hnd->width = width;
325 hnd->height = height;
326 hnd->format = format;
327 *handle = (native_handle_t *)hnd;
328 res = 0;
329 break;
330
331 }
332 default:
333 break;
334 }
335 va_end(args);
336 return res;
337}