2009-08-05 07:26:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
2011-11-16 21:41:37 +00:00
|
|
|
* Copyright (c) 2011 Code Aurora Forum. All rights reserved.
|
2009-08-05 07:26:21 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <unistd.h>
|
2009-08-19 01:39:57 +00:00
|
|
|
#include <string.h>
|
2009-10-30 02:50:47 +00:00
|
|
|
#include <stdarg.h>
|
2009-08-05 07:26:21 +00:00
|
|
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2010-01-20 21:13:08 +00:00
|
|
|
#include <sys/ioctl.h>
|
2011-03-08 01:31:51 +00:00
|
|
|
#include <linux/ashmem.h>
|
2009-08-05 07:26:21 +00:00
|
|
|
|
|
|
|
#include <cutils/log.h>
|
|
|
|
#include <cutils/atomic.h>
|
2011-03-08 01:31:51 +00:00
|
|
|
#include <cutils/ashmem.h>
|
2009-08-05 07:26:21 +00:00
|
|
|
|
|
|
|
#include <hardware/hardware.h>
|
|
|
|
#include <hardware/gralloc.h>
|
2011-11-20 08:12:03 +00:00
|
|
|
#include <genlock.h>
|
2009-08-05 07:26:21 +00:00
|
|
|
|
2010-01-20 21:13:08 +00:00
|
|
|
#include <linux/android_pmem.h>
|
|
|
|
|
2009-08-05 07:26:21 +00:00
|
|
|
#include "gralloc_priv.h"
|
2011-03-08 01:31:51 +00:00
|
|
|
#include "gr.h"
|
2011-11-19 17:55:49 +00:00
|
|
|
#include "alloc_controller.h"
|
|
|
|
#include "memalloc.h"
|
2009-08-05 07:26:21 +00:00
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
using namespace gralloc;
|
2011-11-16 21:41:37 +00:00
|
|
|
using android::sp;
|
|
|
|
/*****************************************************************************/
|
2009-08-05 07:26:21 +00:00
|
|
|
|
2011-11-16 21:41:37 +00:00
|
|
|
// Return the type of allocator -
|
|
|
|
// these are used for mapping/unmapping
|
|
|
|
static sp<IMemAlloc> getAllocator(int flags)
|
|
|
|
{
|
|
|
|
sp<IMemAlloc> memalloc;
|
2011-11-19 17:55:49 +00:00
|
|
|
sp<IAllocController> alloc_ctrl = IAllocController::getInstance(true);
|
|
|
|
memalloc = alloc_ctrl->getAllocator(flags);
|
2011-11-16 21:41:37 +00:00
|
|
|
return memalloc;
|
|
|
|
}
|
2009-08-05 07:26:21 +00:00
|
|
|
|
|
|
|
static int gralloc_map(gralloc_module_t const* module,
|
|
|
|
buffer_handle_t handle,
|
|
|
|
void** vaddr)
|
|
|
|
{
|
|
|
|
private_handle_t* hnd = (private_handle_t*)handle;
|
2011-03-08 01:31:51 +00:00
|
|
|
void *mappedAddress;
|
2009-08-05 07:26:21 +00:00
|
|
|
if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
|
|
|
|
size_t size = hnd->size;
|
2011-11-16 21:41:37 +00:00
|
|
|
sp<IMemAlloc> memalloc = getAllocator(hnd->flags) ;
|
|
|
|
int err = memalloc->map_buffer(&mappedAddress, size,
|
|
|
|
hnd->offset, hnd->fd);
|
|
|
|
if(err) {
|
|
|
|
LOGE("Could not mmap handle %p, fd=%d (%s)",
|
|
|
|
handle, hnd->fd, strerror(errno));
|
|
|
|
hnd->base = 0;
|
|
|
|
return -errno;
|
2011-03-08 01:31:51 +00:00
|
|
|
}
|
2011-11-16 21:41:37 +00:00
|
|
|
|
2009-08-05 07:26:21 +00:00
|
|
|
if (mappedAddress == MAP_FAILED) {
|
2009-08-19 01:39:57 +00:00
|
|
|
LOGE("Could not mmap handle %p, fd=%d (%s)",
|
|
|
|
handle, hnd->fd, strerror(errno));
|
|
|
|
hnd->base = 0;
|
2009-08-05 07:26:21 +00:00
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
hnd->base = intptr_t(mappedAddress) + hnd->offset;
|
2011-11-16 21:41:37 +00:00
|
|
|
//LOGD("gralloc_map() succeeded fd=%d, off=%d, size=%d, vaddr=%p",
|
2009-08-05 07:26:21 +00:00
|
|
|
// hnd->fd, hnd->offset, hnd->size, mappedAddress);
|
|
|
|
}
|
|
|
|
*vaddr = (void*)hnd->base;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gralloc_unmap(gralloc_module_t const* module,
|
|
|
|
buffer_handle_t handle)
|
|
|
|
{
|
|
|
|
private_handle_t* hnd = (private_handle_t*)handle;
|
|
|
|
if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
|
2011-11-16 21:41:37 +00:00
|
|
|
int err = -EINVAL;
|
2009-08-19 01:39:57 +00:00
|
|
|
void* base = (void*)hnd->base;
|
|
|
|
size_t size = hnd->size;
|
2011-11-16 21:41:37 +00:00
|
|
|
sp<IMemAlloc> memalloc = getAllocator(hnd->flags) ;
|
|
|
|
if(memalloc != NULL)
|
|
|
|
err = memalloc->unmap_buffer(base, size, hnd->offset);
|
|
|
|
if (err) {
|
|
|
|
LOGE("Could not unmap memory at address %p", base);
|
2009-08-05 07:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
hnd->base = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
2011-11-16 21:41:37 +00:00
|
|
|
static pthread_mutex_t sMapLock = PTHREAD_MUTEX_INITIALIZER;
|
2009-08-05 07:26:21 +00:00
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
int gralloc_register_buffer(gralloc_module_t const* module,
|
|
|
|
buffer_handle_t handle)
|
|
|
|
{
|
|
|
|
if (private_handle_t::validate(handle) < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
// In this implementation, we don't need to do anything here
|
|
|
|
|
|
|
|
/* NOTE: we need to initialize the buffer as not mapped/not locked
|
|
|
|
* because it shouldn't when this function is called the first time
|
|
|
|
* in a new process. Ideally these flags shouldn't be part of the
|
2011-11-16 21:41:37 +00:00
|
|
|
* handle, but instead maintained in the kernel or at least
|
2009-08-05 07:26:21 +00:00
|
|
|
* out-of-line
|
2011-11-16 21:41:37 +00:00
|
|
|
*/
|
2009-08-05 07:26:21 +00:00
|
|
|
|
|
|
|
// if this handle was created in this process, then we keep it as is.
|
|
|
|
private_handle_t* hnd = (private_handle_t*)handle;
|
|
|
|
if (hnd->pid != getpid()) {
|
|
|
|
hnd->base = 0;
|
2011-11-20 08:21:11 +00:00
|
|
|
void *vaddr;
|
|
|
|
int err = gralloc_map(module, handle, &vaddr);
|
|
|
|
if (err) {
|
|
|
|
LOGE("%s: gralloc_map failed", __FUNCTION__);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-11-20 08:12:03 +00:00
|
|
|
// Reset the genlock private fd flag in the handle
|
|
|
|
hnd->genlockPrivFd = -1;
|
|
|
|
|
|
|
|
// Check if there is a valid lock attached to the handle.
|
|
|
|
if (-1 == hnd->genlockHandle) {
|
|
|
|
LOGE("%s: the lock is invalid.", __FUNCTION__);
|
2011-11-20 08:21:11 +00:00
|
|
|
gralloc_unmap(module, handle);
|
|
|
|
hnd->base = 0;
|
2011-11-20 08:12:03 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attach the genlock handle
|
2011-12-16 18:44:48 +00:00
|
|
|
if (GENLOCK_NO_ERROR != genlock_attach_lock((native_handle_t *)handle)) {
|
2011-11-20 08:21:11 +00:00
|
|
|
LOGE("%s: genlock_attach_lock failed", __FUNCTION__);
|
|
|
|
gralloc_unmap(module, handle);
|
|
|
|
hnd->base = 0;
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2009-08-05 07:26:21 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int gralloc_unregister_buffer(gralloc_module_t const* module,
|
|
|
|
buffer_handle_t handle)
|
|
|
|
{
|
|
|
|
if (private_handle_t::validate(handle) < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the buffer has been mapped during a lock operation, it's time
|
|
|
|
* to un-map it. It's an error to be here with a locked buffer.
|
|
|
|
* NOTE: the framebuffer is handled differently and is never unmapped.
|
|
|
|
*/
|
|
|
|
|
|
|
|
private_handle_t* hnd = (private_handle_t*)handle;
|
2011-11-16 21:41:37 +00:00
|
|
|
|
2009-08-05 07:26:21 +00:00
|
|
|
// never unmap buffers that were created in this process
|
|
|
|
if (hnd->pid != getpid()) {
|
2011-11-20 08:40:05 +00:00
|
|
|
if (hnd->base != 0) {
|
2009-08-05 07:26:21 +00:00
|
|
|
gralloc_unmap(module, handle);
|
|
|
|
}
|
|
|
|
hnd->base = 0;
|
2011-11-20 08:12:03 +00:00
|
|
|
// Release the genlock
|
|
|
|
if (-1 != hnd->genlockHandle) {
|
|
|
|
return genlock_release_lock((native_handle_t *)handle);
|
|
|
|
} else {
|
|
|
|
LOGE("%s: there was no genlock attached to this buffer", __FUNCTION__);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2009-08-05 07:26:21 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-19 01:39:57 +00:00
|
|
|
int terminateBuffer(gralloc_module_t const* module,
|
|
|
|
private_handle_t* hnd)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If the buffer has been mapped during a lock operation, it's time
|
|
|
|
* to un-map it. It's an error to be here with a locked buffer.
|
|
|
|
*/
|
|
|
|
|
2011-11-20 08:40:05 +00:00
|
|
|
if (hnd->base != 0) {
|
2009-08-19 01:39:57 +00:00
|
|
|
// this buffer was mapped, unmap it now
|
2011-08-01 19:09:22 +00:00
|
|
|
if (hnd->flags & (private_handle_t::PRIV_FLAGS_USES_PMEM |
|
|
|
|
private_handle_t::PRIV_FLAGS_USES_PMEM_ADSP |
|
2011-11-19 17:55:49 +00:00
|
|
|
private_handle_t::PRIV_FLAGS_USES_ASHMEM |
|
|
|
|
private_handle_t::PRIV_FLAGS_USES_ION)) {
|
2009-08-19 01:39:57 +00:00
|
|
|
if (hnd->pid != getpid()) {
|
|
|
|
// ... unless it's a "master" pmem buffer, that is a buffer
|
|
|
|
// mapped in the process it's been allocated.
|
|
|
|
// (see gralloc_alloc_buffer())
|
|
|
|
gralloc_unmap(module, hnd);
|
|
|
|
}
|
|
|
|
} else {
|
2011-08-01 19:09:22 +00:00
|
|
|
LOGE("terminateBuffer: unmapping a non pmem/ashmem buffer flags = 0x%x", hnd->flags);
|
2009-08-19 01:39:57 +00:00
|
|
|
gralloc_unmap(module, hnd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-05 07:26:21 +00:00
|
|
|
int gralloc_lock(gralloc_module_t const* module,
|
|
|
|
buffer_handle_t handle, int usage,
|
|
|
|
int l, int t, int w, int h,
|
|
|
|
void** vaddr)
|
|
|
|
{
|
|
|
|
if (private_handle_t::validate(handle) < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
int err = 0;
|
|
|
|
private_handle_t* hnd = (private_handle_t*)handle;
|
|
|
|
if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
|
2011-11-20 08:40:05 +00:00
|
|
|
if (hnd->base == 0) {
|
2009-08-05 07:26:21 +00:00
|
|
|
// we need to map for real
|
|
|
|
pthread_mutex_t* const lock = &sMapLock;
|
|
|
|
pthread_mutex_lock(lock);
|
2011-11-20 08:40:05 +00:00
|
|
|
err = gralloc_map(module, handle, vaddr);
|
2009-08-05 07:26:21 +00:00
|
|
|
pthread_mutex_unlock(lock);
|
|
|
|
}
|
|
|
|
*vaddr = (void*)hnd->base;
|
|
|
|
|
2011-11-20 08:40:05 +00:00
|
|
|
// Lock the buffer for read/write operation as specified. Write lock
|
|
|
|
// has a higher priority over read lock.
|
|
|
|
int lockType = 0;
|
|
|
|
if (usage & GRALLOC_USAGE_SW_WRITE_MASK) {
|
|
|
|
lockType = GENLOCK_WRITE_LOCK;
|
|
|
|
} else if (usage & GRALLOC_USAGE_SW_READ_MASK) {
|
|
|
|
lockType = GENLOCK_READ_LOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int timeout = GENLOCK_MAX_TIMEOUT;
|
2011-12-16 18:44:48 +00:00
|
|
|
if (GENLOCK_NO_ERROR != genlock_lock_buffer((native_handle_t *)handle,
|
2011-11-20 08:40:05 +00:00
|
|
|
(genlock_lock_type)lockType,
|
|
|
|
timeout)) {
|
|
|
|
LOGE("%s: genlock_lock_buffer (lockType=0x%x) failed", __FUNCTION__,
|
|
|
|
lockType);
|
|
|
|
return -EINVAL;
|
|
|
|
} else {
|
|
|
|
// Mark this buffer as locked for SW read/write operation.
|
|
|
|
hnd->flags |= private_handle_t::PRIV_FLAGS_SW_LOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) &&
|
|
|
|
!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
|
|
|
|
// Mark the buffer to be flushed after cpu read/write
|
|
|
|
hnd->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
|
|
|
|
}
|
|
|
|
}
|
2009-08-05 07:26:21 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-11-16 21:41:37 +00:00
|
|
|
int gralloc_unlock(gralloc_module_t const* module,
|
2009-08-05 07:26:21 +00:00
|
|
|
buffer_handle_t handle)
|
|
|
|
{
|
|
|
|
if (private_handle_t::validate(handle) < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
private_handle_t* hnd = (private_handle_t*)handle;
|
|
|
|
|
2010-03-02 23:09:54 +00:00
|
|
|
if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
|
|
|
|
int err;
|
2011-11-16 21:41:37 +00:00
|
|
|
sp<IMemAlloc> memalloc = getAllocator(hnd->flags) ;
|
|
|
|
err = memalloc->clean_buffer((void*)hnd->base,
|
|
|
|
hnd->size, hnd->offset, hnd->fd);
|
2011-09-02 01:08:34 +00:00
|
|
|
LOGE_IF(err < 0, "cannot flush handle %p (offs=%x len=%x, flags = 0x%x) err=%s\n",
|
|
|
|
hnd, hnd->offset, hnd->size, hnd->flags, strerror(errno));
|
2010-03-02 23:09:54 +00:00
|
|
|
hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
|
|
|
|
}
|
|
|
|
|
2011-11-20 08:40:05 +00:00
|
|
|
if ((hnd->flags & private_handle_t::PRIV_FLAGS_SW_LOCK)) {
|
|
|
|
// Unlock the buffer.
|
2011-12-16 18:44:48 +00:00
|
|
|
if (GENLOCK_NO_ERROR != genlock_unlock_buffer((native_handle_t *)handle)) {
|
2011-11-20 08:40:05 +00:00
|
|
|
LOGE("%s: genlock_unlock_buffer failed", __FUNCTION__);
|
2009-08-05 07:26:21 +00:00
|
|
|
return -EINVAL;
|
2011-11-20 08:40:05 +00:00
|
|
|
} else
|
|
|
|
hnd->flags &= ~private_handle_t::PRIV_FLAGS_SW_LOCK;
|
|
|
|
}
|
2009-08-05 07:26:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2009-10-30 02:50:47 +00:00
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
int gralloc_perform(struct gralloc_module_t const* module,
|
|
|
|
int operation, ... )
|
|
|
|
{
|
|
|
|
int res = -EINVAL;
|
|
|
|
va_list args;
|
|
|
|
va_start(args, operation);
|
|
|
|
switch (operation) {
|
2011-11-16 21:41:37 +00:00
|
|
|
case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER:
|
|
|
|
{
|
|
|
|
int fd = va_arg(args, int);
|
|
|
|
size_t size = va_arg(args, size_t);
|
|
|
|
size_t offset = va_arg(args, size_t);
|
|
|
|
void* base = va_arg(args, void*);
|
2011-11-23 20:25:38 +00:00
|
|
|
int width = va_arg(args, int);
|
|
|
|
int height = va_arg(args, int);
|
|
|
|
int format = va_arg(args, int);
|
2011-11-16 21:41:37 +00:00
|
|
|
|
|
|
|
native_handle_t** handle = va_arg(args, native_handle_t**);
|
|
|
|
int memoryFlags = va_arg(args, int);
|
|
|
|
private_handle_t* hnd = (private_handle_t*)native_handle_create(
|
|
|
|
private_handle_t::sNumFds, private_handle_t::sNumInts);
|
|
|
|
hnd->magic = private_handle_t::sMagic;
|
|
|
|
hnd->fd = fd;
|
|
|
|
unsigned int contigFlags = GRALLOC_USAGE_PRIVATE_ADSP_HEAP |
|
2011-12-08 18:50:26 +00:00
|
|
|
GRALLOC_USAGE_PRIVATE_UI_CONTIG_HEAP |
|
2011-11-16 21:41:37 +00:00
|
|
|
GRALLOC_USAGE_PRIVATE_SMI_HEAP;
|
|
|
|
|
|
|
|
if (memoryFlags & contigFlags) {
|
|
|
|
// check if the buffer is a pmem buffer
|
|
|
|
pmem_region region;
|
|
|
|
if (ioctl(fd, PMEM_GET_SIZE, ®ion) < 0)
|
|
|
|
hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION;
|
|
|
|
else
|
|
|
|
hnd->flags = private_handle_t::PRIV_FLAGS_USES_PMEM |
|
|
|
|
private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH;
|
|
|
|
} else {
|
|
|
|
if (memoryFlags & GRALLOC_USAGE_PRIVATE_ION)
|
|
|
|
hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION;
|
|
|
|
else
|
|
|
|
hnd->flags = private_handle_t::PRIV_FLAGS_USES_ASHMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
hnd->size = size;
|
|
|
|
hnd->offset = offset;
|
|
|
|
hnd->base = intptr_t(base) + offset;
|
|
|
|
hnd->gpuaddr = 0;
|
2011-11-23 20:25:38 +00:00
|
|
|
hnd->width = width;
|
|
|
|
hnd->height = height;
|
|
|
|
hnd->format = format;
|
2011-11-16 21:41:37 +00:00
|
|
|
*handle = (native_handle_t *)hnd;
|
|
|
|
res = 0;
|
2010-01-20 21:13:08 +00:00
|
|
|
break;
|
|
|
|
|
2011-05-02 20:46:44 +00:00
|
|
|
}
|
2011-11-16 21:41:37 +00:00
|
|
|
default:
|
2011-04-22 17:39:09 +00:00
|
|
|
break;
|
2009-10-30 02:50:47 +00:00
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
return res;
|
|
|
|
}
|