blob: af536ad9e356a05f0ea53a6098d144e7633efa2b [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 }
Ramkumar Radhakrishnan7bf31c32013-01-28 22:51:51 -0800120 /* need to initialize the pointer to NULL otherwise unmapping for that
121 * buffer happens twice which leads to crash */
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700122 hnd->base = 0;
Ramkumar Radhakrishnan7bf31c32013-01-28 22:51:51 -0800123 hnd->base_metadata = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700124 return 0;
125}
126
127/*****************************************************************************/
128
129static pthread_mutex_t sMapLock = PTHREAD_MUTEX_INITIALIZER;
130
131/*****************************************************************************/
132
133int gralloc_register_buffer(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700134 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700135{
136 if (private_handle_t::validate(handle) < 0)
137 return -EINVAL;
138
139 // In this implementation, we don't need to do anything here
140
141 /* NOTE: we need to initialize the buffer as not mapped/not locked
142 * because it shouldn't when this function is called the first time
143 * in a new process. Ideally these flags shouldn't be part of the
144 * handle, but instead maintained in the kernel or at least
145 * out-of-line
146 */
147
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700148 private_handle_t* hnd = (private_handle_t*)handle;
Kinjal Bhavsar139e1622012-09-10 12:35:11 -0700149 hnd->base = 0;
Ramkumar Radhakrishnan7bf31c32013-01-28 22:51:51 -0800150 hnd->base_metadata = 0;
Kinjal Bhavsar139e1622012-09-10 12:35:11 -0700151 void *vaddr;
152 int err = gralloc_map(module, handle, &vaddr);
153 if (err) {
154 ALOGE("%s: gralloc_map failed", __FUNCTION__);
155 return err;
156 }
157
158 // Reset the genlock private fd flag in the handle
159 hnd->genlockPrivFd = -1;
160
161 // Check if there is a valid lock attached to the handle.
162 if (-1 == hnd->genlockHandle) {
163 ALOGE("%s: the lock is invalid.", __FUNCTION__);
164 gralloc_unmap(module, handle);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700165 hnd->base = 0;
Kinjal Bhavsar139e1622012-09-10 12:35:11 -0700166 return -EINVAL;
167 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700168
Kinjal Bhavsar139e1622012-09-10 12:35:11 -0700169 // Attach the genlock handle
170 if (GENLOCK_NO_ERROR != genlock_attach_lock((native_handle_t *)handle)) {
171 ALOGE("%s: genlock_attach_lock failed", __FUNCTION__);
172 gralloc_unmap(module, handle);
173 hnd->base = 0;
174 return -EINVAL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700175 }
176 return 0;
177}
178
179int gralloc_unregister_buffer(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700180 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700181{
182 if (private_handle_t::validate(handle) < 0)
183 return -EINVAL;
184
185 /*
186 * If the buffer has been mapped during a lock operation, it's time
187 * to un-map it. It's an error to be here with a locked buffer.
188 * NOTE: the framebuffer is handled differently and is never unmapped.
189 */
190
191 private_handle_t* hnd = (private_handle_t*)handle;
192
Kinjal Bhavsar139e1622012-09-10 12:35:11 -0700193 if (hnd->base != 0) {
194 gralloc_unmap(module, handle);
195 }
196 hnd->base = 0;
Ramkumar Radhakrishnan7bf31c32013-01-28 22:51:51 -0800197 hnd->base_metadata = 0;
Kinjal Bhavsar139e1622012-09-10 12:35:11 -0700198 // Release the genlock
199 if (-1 != hnd->genlockHandle) {
200 return genlock_release_lock((native_handle_t *)handle);
201 } else {
202 ALOGE("%s: there was no genlock attached to this buffer", __FUNCTION__);
203 return -EINVAL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700204 }
205 return 0;
206}
207
208int terminateBuffer(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700209 private_handle_t* hnd)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700210{
211 /*
212 * If the buffer has been mapped during a lock operation, it's time
213 * to un-map it. It's an error to be here with a locked buffer.
214 */
215
216 if (hnd->base != 0) {
217 // this buffer was mapped, unmap it now
218 if (hnd->flags & (private_handle_t::PRIV_FLAGS_USES_PMEM |
219 private_handle_t::PRIV_FLAGS_USES_PMEM_ADSP |
220 private_handle_t::PRIV_FLAGS_USES_ASHMEM |
221 private_handle_t::PRIV_FLAGS_USES_ION)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700222 gralloc_unmap(module, hnd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700223 } else {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700224 ALOGE("terminateBuffer: unmapping a non pmem/ashmem buffer flags = 0x%x",
225 hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700226 gralloc_unmap(module, hnd);
227 }
228 }
229
230 return 0;
231}
232
233int gralloc_lock(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700234 buffer_handle_t handle, int usage,
235 int l, int t, int w, int h,
236 void** vaddr)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700237{
238 if (private_handle_t::validate(handle) < 0)
239 return -EINVAL;
240
241 int err = 0;
242 private_handle_t* hnd = (private_handle_t*)handle;
243 if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
244 if (hnd->base == 0) {
245 // we need to map for real
246 pthread_mutex_t* const lock = &sMapLock;
247 pthread_mutex_lock(lock);
248 err = gralloc_map(module, handle, vaddr);
249 pthread_mutex_unlock(lock);
250 }
251 *vaddr = (void*)hnd->base;
252
253 // Lock the buffer for read/write operation as specified. Write lock
254 // has a higher priority over read lock.
255 int lockType = 0;
256 if (usage & GRALLOC_USAGE_SW_WRITE_MASK) {
257 lockType = GENLOCK_WRITE_LOCK;
258 } else if (usage & GRALLOC_USAGE_SW_READ_MASK) {
259 lockType = GENLOCK_READ_LOCK;
260 }
261
262 int timeout = GENLOCK_MAX_TIMEOUT;
263 if (GENLOCK_NO_ERROR != genlock_lock_buffer((native_handle_t *)handle,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700264 (genlock_lock_type)lockType,
265 timeout)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700266 ALOGE("%s: genlock_lock_buffer (lockType=0x%x) failed", __FUNCTION__,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700267 lockType);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700268 return -EINVAL;
269 } else {
270 // Mark this buffer as locked for SW read/write operation.
271 hnd->flags |= private_handle_t::PRIV_FLAGS_SW_LOCK;
272 }
273
274 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) &&
275 !(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
276 // Mark the buffer to be flushed after cpu read/write
277 hnd->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
278 }
279 }
280 return err;
281}
282
283int gralloc_unlock(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700284 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700285{
286 if (private_handle_t::validate(handle) < 0)
287 return -EINVAL;
288
289 private_handle_t* hnd = (private_handle_t*)handle;
290
291 if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
292 int err;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700293 IMemAlloc* memalloc = getAllocator(hnd->flags) ;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700294 err = memalloc->clean_buffer((void*)hnd->base,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700295 hnd->size, hnd->offset, hnd->fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700296 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 -0700297 hnd, hnd->offset, hnd->size, hnd->flags, strerror(errno));
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800298 unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
299 err = memalloc->clean_buffer((void*)hnd->base_metadata, size,
300 hnd->offset_metadata, hnd->fd_metadata);
301 ALOGE_IF(err < 0, "cannot flush handle %p (offs=%x len=%lu, "
302 "flags = 0x%x) err=%s\n", hnd, hnd->offset_metadata, size,
303 hnd->flags, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700304 hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
305 }
306
307 if ((hnd->flags & private_handle_t::PRIV_FLAGS_SW_LOCK)) {
308 // Unlock the buffer.
309 if (GENLOCK_NO_ERROR != genlock_unlock_buffer((native_handle_t *)handle)) {
310 ALOGE("%s: genlock_unlock_buffer failed", __FUNCTION__);
311 return -EINVAL;
312 } else
313 hnd->flags &= ~private_handle_t::PRIV_FLAGS_SW_LOCK;
314 }
315 return 0;
316}
317
318/*****************************************************************************/
319
320int gralloc_perform(struct gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700321 int operation, ... )
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700322{
323 int res = -EINVAL;
324 va_list args;
325 va_start(args, operation);
326 switch (operation) {
327 case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER:
328 {
329 int fd = va_arg(args, int);
330 size_t size = va_arg(args, size_t);
331 size_t offset = va_arg(args, size_t);
332 void* base = va_arg(args, void*);
333 int width = va_arg(args, int);
334 int height = va_arg(args, int);
335 int format = va_arg(args, int);
336
337 native_handle_t** handle = va_arg(args, native_handle_t**);
338 int memoryFlags = va_arg(args, int);
339 private_handle_t* hnd = (private_handle_t*)native_handle_create(
Naseer Ahmed29a26812012-06-14 00:56:20 -0700340 private_handle_t::sNumFds, private_handle_t::sNumInts);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700341 hnd->magic = private_handle_t::sMagic;
342 hnd->fd = fd;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700343 hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700344 hnd->size = size;
345 hnd->offset = offset;
346 hnd->base = intptr_t(base) + offset;
347 hnd->gpuaddr = 0;
348 hnd->width = width;
349 hnd->height = height;
350 hnd->format = format;
351 *handle = (native_handle_t *)hnd;
352 res = 0;
353 break;
354
355 }
Naseer Ahmed74214722013-02-09 08:11:36 -0500356#ifdef QCOM_BSP
Ramkumar Radhakrishnan935cb682012-11-07 16:43:38 -0800357 case GRALLOC_MODULE_PERFORM_UPDATE_BUFFER_GEOMETRY:
358 {
359 int width = va_arg(args, int);
360 int height = va_arg(args, int);
361 int format = va_arg(args, int);
362 private_handle_t* hnd = va_arg(args, private_handle_t*);
363 if (private_handle_t::validate(hnd)) {
364 return res;
365 }
366 hnd->width = width;
367 hnd->height = height;
368 hnd->format = format;
369 res = 0;
370 }
371 break;
Naseer Ahmed74214722013-02-09 08:11:36 -0500372#endif
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700373 default:
374 break;
375 }
376 va_end(args);
377 return res;
378}