Merge changes I5283613c,I9377584e,I788e411f,I5172a825 into ics

* changes:
  libgralloc: Invoke genlock during gralloc_lock/gralloc_unlock
  libgralloc: Map buffers during registration
  libgralloc: Create, attach and release genlock locks
  Add a generic buffer locking API.
This commit is contained in:
Linux Build Service Account 2011-11-25 05:20:31 -08:00 committed by QuIC Gerrit Code Review
commit 1698fbaa95
8 changed files with 532 additions and 107 deletions

View File

@ -1,4 +1,4 @@
#Enables the listed display HAL modules
display-hals := libhwcomposer liboverlay libgralloc libcopybit
display-hals := libhwcomposer liboverlay libgralloc libgenlock libcopybit
include $(call all-named-subdir-makefiles,$(display-hals))

15
libgenlock/Android.mk Normal file
View File

@ -0,0 +1,15 @@
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_PRELINK_MODULE := false
LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)
LOCAL_SHARED_LIBRARIES := liblog libcutils
LOCAL_C_INCLUDES := $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr/include
LOCAL_C_INCLUDES += hardware/qcom/display/libgralloc
LOCAL_ADDITIONAL_DEPENDENCIES := $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr
LOCAL_SRC_FILES := genlock.cpp
LOCAL_CFLAGS:= -DLOG_TAG=\"libgenlock\"
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := libgenlock
include $(BUILD_SHARED_LIBRARY)

301
libgenlock/genlock.cpp Normal file
View File

@ -0,0 +1,301 @@
/*
* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Code Aurora Forum, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <cutils/log.h>
#include <cutils/native_handle.h>
#include <gralloc_priv.h>
#include <linux/genlock.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include "genlock.h"
#define GENLOCK_DEVICE "/dev/genlock"
namespace {
/* Internal function to map the userspace locks to the kernel lock types */
int get_kernel_lock_type(genlock_lock_type lockType)
{
int kLockType = 0;
// If the user sets both a read and write lock, higher preference is
// given to the write lock.
if (lockType & GENLOCK_WRITE_LOCK) {
kLockType = GENLOCK_WRLOCK;
} else if (lockType & GENLOCK_READ_LOCK) {
kLockType = GENLOCK_RDLOCK;
} else {
LOGE("%s: invalid lockType (lockType = %d)", __FUNCTION__, lockType);
return -1;
}
return kLockType;
}
/* Internal function to perform the actual lock/unlock operations */
genlock_status_t perform_lock_unlock_operation(native_handle_t *buffer_handle,
int lockType, int timeout)
{
if (private_handle_t::validate(buffer_handle)) {
LOGE("%s: handle is invalid", __FUNCTION__);
return GENLOCK_FAILURE;
}
private_handle_t *hnd = reinterpret_cast<private_handle_t*>(buffer_handle);
if (hnd->genlockPrivFd < 0) {
LOGE("%s: the lock has not been created, or has not been attached",
__FUNCTION__);
return GENLOCK_FAILURE;
}
genlock_lock lock;
lock.op = lockType;
lock.flags = 0;
lock.timeout = timeout;
lock.fd = hnd->genlockHandle;
if (ioctl(hnd->genlockPrivFd, GENLOCK_IOC_LOCK, &lock)) {
LOGE("%s: GENLOCK_IOC_LOCK failed (lockType0x%x, err=%s fd=%d)", __FUNCTION__,
lockType, strerror(errno), hnd->fd);
if (ETIMEDOUT == errno)
return GENLOCK_TIMEDOUT;
return GENLOCK_FAILURE;
}
return GENLOCK_NO_ERROR;
}
/* Internal function to close the fd and release the handle */
void close_genlock_fd_and_handle(int& fd, int& handle)
{
if (fd >=0 ) {
close(fd);
fd = -1;
}
if (handle >= 0) {
close(handle);
handle = -1;
}
}
}
/*
* Create a genlock lock. The genlock lock file descriptor and the lock
* handle are stored in the buffer_handle.
*
* @param: handle of the buffer
* @return error status.
*/
genlock_status_t genlock_create_lock(native_handle_t *buffer_handle)
{
if (private_handle_t::validate(buffer_handle)) {
LOGE("%s: handle is invalid", __FUNCTION__);
return GENLOCK_FAILURE;
}
// Open the genlock device
int fd = open(GENLOCK_DEVICE, O_RDWR);
if (fd < 0) {
LOGE("%s: open genlock device failed (err=%s)", __FUNCTION__,
strerror(errno));
return GENLOCK_FAILURE;
}
genlock_status_t ret = GENLOCK_NO_ERROR;
// Create a new lock
private_handle_t *hnd = reinterpret_cast<private_handle_t*>(buffer_handle);
genlock_lock lock;
if (ioctl(fd, GENLOCK_IOC_NEW, NULL)) {
LOGE("%s: GENLOCK_IOC_NEW failed (error=%s)", __FUNCTION__,
strerror(errno));
close_genlock_fd_and_handle(fd, lock.fd);
ret = GENLOCK_FAILURE;
}
// Export the lock for other processes to be able to use it.
if (GENLOCK_FAILURE != ret) {
if (ioctl(fd, GENLOCK_IOC_EXPORT, &lock)) {
LOGE("%s: GENLOCK_IOC_EXPORT failed (error=%s)", __FUNCTION__,
strerror(errno));
close_genlock_fd_and_handle(fd, lock.fd);
ret = GENLOCK_FAILURE;
}
}
// Store the lock params in the handle.
hnd->genlockPrivFd = fd;
hnd->genlockHandle = lock.fd;
return ret;
}
/*
* Release a genlock lock associated with the handle.
*
* @param: handle of the buffer
* @return error status.
*/
genlock_status_t genlock_release_lock(native_handle_t *buffer_handle)
{
if (private_handle_t::validate(buffer_handle)) {
LOGE("%s: handle is invalid", __FUNCTION__);
return GENLOCK_FAILURE;
}
genlock_status_t ret = GENLOCK_NO_ERROR;
private_handle_t *hnd = reinterpret_cast<private_handle_t*>(buffer_handle);
if (hnd->genlockPrivFd < 0) {
LOGE("%s: the lock is invalid", __FUNCTION__);
return GENLOCK_FAILURE;
}
if (ioctl(hnd->genlockPrivFd, GENLOCK_IOC_RELEASE, NULL)) {
LOGE("%s: GENLOCK_IOC_RELEASE failed (err=%s)", __FUNCTION__,
strerror(errno));
ret = GENLOCK_FAILURE;
}
// Close the fd and reset the parameters.
close_genlock_fd_and_handle(hnd->genlockPrivFd, hnd->genlockHandle);
return ret;
}
/*
* Attach a lock to the buffer handle passed via an IPC.
*
* @param: handle of the buffer
* @return error status.
*/
genlock_status_t genlock_attach_lock(native_handle_t *buffer_handle)
{
if (private_handle_t::validate(buffer_handle)) {
LOGE("%s: handle is invalid", __FUNCTION__);
return GENLOCK_FAILURE;
}
// Open the genlock device
int fd = open(GENLOCK_DEVICE, O_RDWR);
if (fd < 0) {
LOGE("%s: open genlock device failed (err=%s)", __FUNCTION__,
strerror(errno));
return GENLOCK_FAILURE;
}
genlock_status_t ret = GENLOCK_NO_ERROR;
// Attach the local handle to an existing lock
private_handle_t *hnd = reinterpret_cast<private_handle_t*>(buffer_handle);
genlock_lock lock;
lock.fd = hnd->genlockHandle;
if (ioctl(fd, GENLOCK_IOC_ATTACH, &lock)) {
LOGE("%s: GENLOCK_IOC_ATTACH failed (err=%s)", __FUNCTION__,
strerror(errno));
close_genlock_fd_and_handle(fd, lock.fd);
ret = GENLOCK_FAILURE;
}
// Store the relavant information in the handle
hnd->genlockPrivFd = fd;
return ret;
}
/*
* Lock the buffer specified by the buffer handle. The lock held by the buffer
* is specified by the lockType. This function will block if a write lock is
* requested on the buffer which has previously been locked for a read or write
* operation. A buffer can be locked by multiple clients for read. An optional
* timeout value can be specified. By default, there is no timeout.
*
* @param: handle of the buffer
* @param: type of lock to be acquired by the buffer.
* @param: timeout value in ms. GENLOCK_MAX_TIMEOUT is the maximum timeout value.
* @return error status.
*/
genlock_status_t genlock_lock_buffer(native_handle_t *buffer_handle,
genlock_lock_type_t lockType,
int timeout)
{
// Translate the locktype
int kLockType = get_kernel_lock_type(lockType);
if (-1 == kLockType) {
LOGE("%s: invalid lockType", __FUNCTION__);
return GENLOCK_FAILURE;
}
if (0 == timeout) {
LOGW("%s: trying to lock a buffer with timeout = 0", __FUNCTION__);
}
// Call the private function to perform the lock operation specified.
return perform_lock_unlock_operation(buffer_handle, kLockType, timeout);
}
/*
* Unlocks a buffer that has previously been locked by the client.
*
* @param: handle of the buffer to be unlocked.
* @return: error status.
*/
genlock_status_t genlock_unlock_buffer(native_handle_t *buffer_handle)
{
// Do the unlock operation by setting the unlock flag. Timeout is always
// 0 in this case.
return perform_lock_unlock_operation(buffer_handle, GENLOCK_UNLOCK, 0);
}
/*
* Blocks the calling process until the lock held on the handle is unlocked.
*
* @param: handle of the buffer
* @param: timeout value for the wait.
* return: error status.
*/
genlock_status_t genlock_wait(native_handle_t *buffer_handle, int timeout) {
if (private_handle_t::validate(buffer_handle)) {
LOGE("%s: handle is invalid", __FUNCTION__);
return GENLOCK_FAILURE;
}
private_handle_t *hnd = reinterpret_cast<private_handle_t*>(buffer_handle);
if (hnd->genlockPrivFd < 0) {
LOGE("%s: the lock is invalid", __FUNCTION__);
return GENLOCK_FAILURE;
}
if (0 == timeout)
LOGW("%s: timeout = 0", __FUNCTION__);
genlock_lock lock;
lock.fd = hnd->genlockHandle;
lock.timeout = timeout;
if (ioctl(hnd->genlockPrivFd, GENLOCK_IOC_WAIT, &lock)) {
LOGE("%s: GENLOCK_IOC_WAIT failed (err=%s)", __FUNCTION__, strerror(errno));
return GENLOCK_FAILURE;
}
return GENLOCK_NO_ERROR;
}

118
libgenlock/genlock.h Normal file
View File

@ -0,0 +1,118 @@
/*
* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Code Aurora Forum, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef INCLUDE_LIBGENLOCK
#define INCLUDE_LIBGENLOCK
#include <cutils/native_handle.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Genlock lock types */
typedef enum genlock_lock_type{
GENLOCK_READ_LOCK = 1<<0, // Read lock
GENLOCK_WRITE_LOCK = 1<<1, // Write lock
}genlock_lock_type_t;
/* Genlock return values */
typedef enum genlock_status{
GENLOCK_NO_ERROR = 0,
GENLOCK_TIMEDOUT,
GENLOCK_FAILURE,
} genlock_status_t;
/* Genlock defines */
#define GENLOCK_MAX_TIMEOUT 1000 // Max 1s timeout
/*
* Create a genlock lock. The genlock lock file descriptor and the lock
* handle are stored in the buffer_handle.
*
* @param: handle of the buffer
* @return error status.
*/
genlock_status_t genlock_create_lock(native_handle_t *buffer_handle);
/*
* Release a genlock lock associated with the handle.
*
* @param: handle of the buffer
* @return error status.
*/
genlock_status_t genlock_release_lock(native_handle_t *buffer_handle);
/*
* Attach a lock to the buffer handle passed via an IPC.
*
* @param: handle of the buffer
* @return error status.
*/
genlock_status_t genlock_attach_lock(native_handle_t *buffer_handle);
/*
* Lock the buffer specified by the buffer handle. The lock held by the buffer
* is specified by the lockType. This function will block if a write lock is
* requested on the buffer which has previously been locked for a read or write
* operation. A buffer can be locked by multiple clients for read. An optional
* timeout value can be specified. By default, there is no timeout.
*
* @param: handle of the buffer
* @param: type of lock to be acquired by the buffer.
* @param: timeout value in ms. GENLOCK_MAX_TIMEOUT is the maximum timeout value.
* @return error status.
*/
genlock_status_t genlock_lock_buffer(native_handle_t *buffer_handle,
genlock_lock_type_t lockType,
int timeout);
/*
* Unlocks a buffer that has previously been locked by the client.
*
* @param: handle of the buffer to be unlocked.
* @return: error status.
*/
genlock_status_t genlock_unlock_buffer(native_handle_t *buffer_handle);
/*
* Blocks the calling process until the lock held on the handle is unlocked.
*
* @param: handle of the buffer
* @param: timeout value for the wait.
* return: error status.
*/
genlock_status_t genlock_wait(native_handle_t *buffer_handle, int timeout);
#ifdef __cplusplus
}
#endif
#endif

2
libgralloc/Android.mk Normal file → Executable file
View File

@ -21,8 +21,10 @@ include $(CLEAR_VARS)
LOCAL_PRELINK_MODULE := false
LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
LOCAL_SHARED_LIBRARIES := liblog libcutils libGLESv1_CM libutils libmemalloc
LOCAL_SHARED_LIBRARIES += libgenlock
LOCAL_C_INCLUDES += $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr/include
LOCAL_C_INCLUDES += hardware/qcom/display/libgenlock
LOCAL_ADDITIONAL_DEPENDENCIES += $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr
LOCAL_SRC_FILES := framebuffer.cpp \
gpu.cpp \

17
libgralloc/gpu.cpp Normal file → Executable file
View File

@ -21,6 +21,8 @@
#include <cutils/properties.h>
#include <sys/mman.h>
#include <genlock.h>
#include "gr.h"
#include "gpu.h"
#include "memalloc.h"
@ -170,7 +172,6 @@ int gpu_context_t::gralloc_alloc_buffer(size_t size, int usage,
hnd->offset = data.offset;
hnd->base = int(data.base) + data.offset;
hnd->lockState = private_handle_t::LOCK_STATE_MAPPED;
*pHandle = hnd;
}
@ -289,6 +290,13 @@ int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
return err;
}
// Create a genlock lock for this buffer handle.
err = genlock_create_lock((native_handle_t*)(*pHandle));
if (err) {
LOGE("%s: genlock_create_lock failed", __FUNCTION__);
free_impl(reinterpret_cast<private_handle_t*>(pHandle));
return err;
}
*pStride = alignedw;
return 0;
}
@ -308,6 +316,13 @@ int gpu_context_t::free_impl(private_handle_t const* hnd) {
return err;
terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
}
// Release the genlock
int err = genlock_release_lock((native_handle_t*)hnd);
if (err) {
LOGE("%s: genlock_release_lock failed", __FUNCTION__);
}
delete hnd;
return 0;
}

View File

@ -278,16 +278,12 @@ struct private_handle_t {
PRIV_FLAGS_USES_ASHMEM = 0x00000010,
PRIV_FLAGS_NEEDS_FLUSH = 0x00000020,
PRIV_FLAGS_DO_NOT_FLUSH = 0x00000040,
};
enum {
LOCK_STATE_WRITE = 1<<31,
LOCK_STATE_MAPPED = 1<<30,
LOCK_STATE_READ_MASK = 0x3FFFFFFF
PRIV_FLAGS_SW_LOCK = 0x00000080,
};
// file-descriptors
int fd;
int genlockHandle; // genlock handle to be dup'd by the binder
// ints
int magic;
int flags;
@ -297,23 +293,22 @@ struct private_handle_t {
// FIXME: the attributes below should be out-of-line
int base;
int lockState;
int writeOwner;
int gpuaddr; // The gpu address mapped into the mmu. If using ashmem, set to 0 They don't care
int pid;
int format;
int width;
int height;
int genlockPrivFd; // local fd of the genlock device.
#ifdef __cplusplus
static const int sNumInts = 13;
static const int sNumFds = 1;
static const int sNumInts = 12;
static const int sNumFds = 2;
static const int sMagic = 'gmsm';
private_handle_t(int fd, int size, int flags, int bufferType, int format, int width, int height) :
fd(fd), magic(sMagic), flags(flags), size(size), offset(0), bufferType(bufferType),
base(0), lockState(0), writeOwner(0), gpuaddr(0), pid(getpid()), format(format), width(width),
height(height)
fd(fd), genlockHandle(-1), magic(sMagic), flags(flags), size(size), offset(0),
bufferType(bufferType), base(0), gpuaddr(0), pid(getpid()), format(format),
width(width), height(height), genlockPrivFd(-1)
{
version = sizeof(native_handle);
numInts = sNumInts;

163
libgralloc/mapper.cpp Normal file → Executable file
View File

@ -34,6 +34,7 @@
#include <hardware/hardware.h>
#include <hardware/gralloc.h>
#include <genlock.h>
#include <linux/android_pmem.h>
@ -132,8 +133,31 @@ int gralloc_register_buffer(gralloc_module_t const* module,
private_handle_t* hnd = (private_handle_t*)handle;
if (hnd->pid != getpid()) {
hnd->base = 0;
hnd->lockState = 0;
hnd->writeOwner = 0;
void *vaddr;
int err = gralloc_map(module, handle, &vaddr);
if (err) {
LOGE("%s: gralloc_map failed", __FUNCTION__);
return err;
}
// 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__);
gralloc_unmap(module, handle);
hnd->base = 0;
return -EINVAL;
}
// Attach the genlock handle
if (GENLOCK_FAILURE == genlock_attach_lock((native_handle_t *)handle)) {
LOGE("%s: genlock_attach_lock failed", __FUNCTION__);
gralloc_unmap(module, handle);
hnd->base = 0;
return -EINVAL;
}
}
return 0;
}
@ -152,18 +176,19 @@ int gralloc_unregister_buffer(gralloc_module_t const* module,
private_handle_t* hnd = (private_handle_t*)handle;
LOGE_IF(hnd->lockState & private_handle_t::LOCK_STATE_READ_MASK,
"[unregister] handle %p still locked (state=%08x)",
hnd, hnd->lockState);
// never unmap buffers that were created in this process
if (hnd->pid != getpid()) {
if (hnd->lockState & private_handle_t::LOCK_STATE_MAPPED) {
if (hnd->base != 0) {
gralloc_unmap(module, handle);
}
hnd->base = 0;
hnd->lockState = 0;
hnd->writeOwner = 0;
// 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;
}
}
return 0;
}
@ -176,11 +201,7 @@ int terminateBuffer(gralloc_module_t const* module,
* to un-map it. It's an error to be here with a locked buffer.
*/
LOGE_IF(hnd->lockState & private_handle_t::LOCK_STATE_READ_MASK,
"[terminate] handle %p still locked (state=%08x)",
hnd, hnd->lockState);
if (hnd->lockState & private_handle_t::LOCK_STATE_MAPPED) {
if (hnd->base != 0) {
// this buffer was mapped, unmap it now
if (hnd->flags & (private_handle_t::PRIV_FLAGS_USES_PMEM |
private_handle_t::PRIV_FLAGS_USES_PMEM_ADSP |
@ -211,70 +232,43 @@ int gralloc_lock(gralloc_module_t const* module,
int err = 0;
private_handle_t* hnd = (private_handle_t*)handle;
int32_t current_value, new_value;
int retry;
do {
current_value = hnd->lockState;
new_value = current_value;
if (current_value & private_handle_t::LOCK_STATE_WRITE) {
// already locked for write
LOGE("handle %p already locked for write", handle);
return -EBUSY;
} else if (current_value & private_handle_t::LOCK_STATE_READ_MASK) {
// already locked for read
if (usage & (GRALLOC_USAGE_SW_WRITE_MASK | GRALLOC_USAGE_HW_RENDER)) {
LOGE("handle %p already locked for read", handle);
return -EBUSY;
} else {
// this is not an error
//LOGD("%p already locked for read... count = %d",
// handle, (current_value & ~(1<<31)));
}
}
// not currently locked
if (usage & (GRALLOC_USAGE_SW_WRITE_MASK | GRALLOC_USAGE_HW_RENDER)) {
// locking for write
new_value |= private_handle_t::LOCK_STATE_WRITE;
}
new_value++;
retry = android_atomic_cmpxchg(current_value, new_value,
(volatile int32_t*)&hnd->lockState);
} while (retry);
if (new_value & private_handle_t::LOCK_STATE_WRITE) {
// locking for write, store the tid
hnd->writeOwner = gettid();
}
// if requesting sw write for non-framebuffer handles, flag for
// flushing at unlock
if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) &&
!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
hnd->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
}
if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
if (!(current_value & private_handle_t::LOCK_STATE_MAPPED)) {
if (hnd->base == 0) {
// we need to map for real
pthread_mutex_t* const lock = &sMapLock;
pthread_mutex_lock(lock);
if (!(hnd->lockState & private_handle_t::LOCK_STATE_MAPPED)) {
err = gralloc_map(module, handle, vaddr);
if (err == 0) {
android_atomic_or(private_handle_t::LOCK_STATE_MAPPED,
(volatile int32_t*)&(hnd->lockState));
}
}
err = gralloc_map(module, handle, vaddr);
pthread_mutex_unlock(lock);
}
*vaddr = (void*)hnd->base;
}
// 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;
if (GENLOCK_FAILURE == genlock_lock_buffer((native_handle_t *)handle,
(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;
}
}
return err;
}
@ -285,7 +279,6 @@ int gralloc_unlock(gralloc_module_t const* module,
return -EINVAL;
private_handle_t* hnd = (private_handle_t*)handle;
int32_t current_value, new_value;
if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
int err;
@ -297,28 +290,14 @@ int gralloc_unlock(gralloc_module_t const* module,
hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
}
do {
current_value = hnd->lockState;
new_value = current_value;
if (current_value & private_handle_t::LOCK_STATE_WRITE) {
// locked for write
if (hnd->writeOwner == gettid()) {
hnd->writeOwner = 0;
new_value &= ~private_handle_t::LOCK_STATE_WRITE;
}
}
if ((new_value & private_handle_t::LOCK_STATE_READ_MASK) == 0) {
LOGE("handle %p not locked", handle);
if ((hnd->flags & private_handle_t::PRIV_FLAGS_SW_LOCK)) {
// Unlock the buffer.
if (GENLOCK_FAILURE == genlock_unlock_buffer((native_handle_t *)handle)) {
LOGE("%s: genlock_unlock_buffer failed", __FUNCTION__);
return -EINVAL;
}
new_value--;
} while (android_atomic_cmpxchg(current_value, new_value,
(volatile int32_t*)&hnd->lockState));
} else
hnd->flags &= ~private_handle_t::PRIV_FLAGS_SW_LOCK;
}
return 0;
}