From aa4994d543a6ac4b5b154aaf6fb81b2339d74e57 Mon Sep 17 00:00:00 2001 From: Naomi Luis Date: Mon, 21 Nov 2011 20:42:54 -0800 Subject: [PATCH 1/4] Add a generic buffer locking API. Add libgenlock which is a cross-process buffer locking API. libgenlock provides the ability to create, attach, release locks. It also provides a mechanism to lock and unlock buffers for read/write purposes. Change-Id: I5172a82539b83bcb1226e3fd4f7b80a5c7f31016 --- Android.mk | 2 +- libgenlock/Android.mk | 15 ++ libgenlock/genlock.cpp | 301 ++++++++++++++++++++++++++++++++++++++ libgenlock/genlock.h | 118 +++++++++++++++ libgralloc/gralloc_priv.h | 12 +- 5 files changed, 442 insertions(+), 6 deletions(-) create mode 100644 libgenlock/Android.mk create mode 100644 libgenlock/genlock.cpp create mode 100644 libgenlock/genlock.h diff --git a/Android.mk b/Android.mk index 1f6937f..a3295d0 100644 --- a/Android.mk +++ b/Android.mk @@ -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)) diff --git a/libgenlock/Android.mk b/libgenlock/Android.mk new file mode 100644 index 0000000..d8a3dfe --- /dev/null +++ b/libgenlock/Android.mk @@ -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) + diff --git a/libgenlock/genlock.cpp b/libgenlock/genlock.cpp new file mode 100644 index 0000000..36c2a22 --- /dev/null +++ b/libgenlock/genlock.cpp @@ -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 +#include +#include +#include +#include +#include + +#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(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(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(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(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(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; +} diff --git a/libgenlock/genlock.h b/libgenlock/genlock.h new file mode 100644 index 0000000..b394410 --- /dev/null +++ b/libgenlock/genlock.h @@ -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 + +#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 diff --git a/libgralloc/gralloc_priv.h b/libgralloc/gralloc_priv.h index 0130b6e..1d990ba 100644 --- a/libgralloc/gralloc_priv.h +++ b/libgralloc/gralloc_priv.h @@ -288,6 +288,7 @@ struct private_handle_t { // file-descriptors int fd; + int genlockHandle; // genlock handle to be dup'd by the binder // ints int magic; int flags; @@ -304,16 +305,17 @@ struct private_handle_t { 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 = 14; + 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), lockState(0), writeOwner(0), gpuaddr(0), + pid(getpid()), format(format), width(width), height(height), genlockPrivFd(-1) { version = sizeof(native_handle); numInts = sNumInts; From 1b5f80522ba8aa1d93873ec790777289d33ddced Mon Sep 17 00:00:00 2001 From: Naomi Luis Date: Sun, 20 Nov 2011 00:12:03 -0800 Subject: [PATCH 2/4] libgralloc: Create, attach and release genlock locks Add support in the gralloc to: - Create a lock during buffer allocation. - Release a lock when the buffer is freed or unregistered. - Attach a lock when the buffer is registered. Change-Id: I788e411f43d5fcf88d7dcf39c530559a12d6361c --- libgralloc/Android.mk | 2 ++ libgralloc/gpu.cpp | 16 ++++++++++++++++ libgralloc/mapper.cpp | 20 ++++++++++++++++++++ 3 files changed, 38 insertions(+) mode change 100644 => 100755 libgralloc/Android.mk mode change 100644 => 100755 libgralloc/gpu.cpp mode change 100644 => 100755 libgralloc/mapper.cpp diff --git a/libgralloc/Android.mk b/libgralloc/Android.mk old mode 100644 new mode 100755 index 545c1d3..a95babb --- a/libgralloc/Android.mk +++ b/libgralloc/Android.mk @@ -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 \ diff --git a/libgralloc/gpu.cpp b/libgralloc/gpu.cpp old mode 100644 new mode 100755 index e0f3ebb..da9f4c1 --- a/libgralloc/gpu.cpp +++ b/libgralloc/gpu.cpp @@ -21,6 +21,8 @@ #include #include +#include + #include "gr.h" #include "gpu.h" #include "memalloc.h" @@ -289,6 +291,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(pHandle)); + return err; + } *pStride = alignedw; return 0; } @@ -308,6 +317,13 @@ int gpu_context_t::free_impl(private_handle_t const* hnd) { return err; terminateBuffer(&m->base, const_cast(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; } diff --git a/libgralloc/mapper.cpp b/libgralloc/mapper.cpp old mode 100644 new mode 100755 index d6024b0..e324552 --- a/libgralloc/mapper.cpp +++ b/libgralloc/mapper.cpp @@ -34,6 +34,7 @@ #include #include +#include #include @@ -134,6 +135,17 @@ int gralloc_register_buffer(gralloc_module_t const* module, hnd->base = 0; hnd->lockState = 0; hnd->writeOwner = 0; + // 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__); + return -EINVAL; + } + + // Attach the genlock handle + return genlock_attach_lock((native_handle_t *)handle); } return 0; } @@ -164,6 +176,14 @@ int gralloc_unregister_buffer(gralloc_module_t const* module, 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; } From 09367ef6e8fb13ffe28945c5c73be8e03d756bdd Mon Sep 17 00:00:00 2001 From: Naomi Luis Date: Sun, 20 Nov 2011 00:21:11 -0800 Subject: [PATCH 3/4] libgralloc: Map buffers during registration Map the gralloc buffer during the register_buffer phase. This allows the clients to obtain the virtual address without the need to call an additional mmap. Change-Id: I9377584ee564ad5113fd353b727793f16d7b9b94 --- libgralloc/mapper.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/libgralloc/mapper.cpp b/libgralloc/mapper.cpp index e324552..da1b2fa 100755 --- a/libgralloc/mapper.cpp +++ b/libgralloc/mapper.cpp @@ -133,6 +133,13 @@ int gralloc_register_buffer(gralloc_module_t const* module, private_handle_t* hnd = (private_handle_t*)handle; if (hnd->pid != getpid()) { hnd->base = 0; + void *vaddr; + int err = gralloc_map(module, handle, &vaddr); + if (err) { + LOGE("%s: gralloc_map failed", __FUNCTION__); + return err; + } + hnd->lockState = 0; hnd->writeOwner = 0; // Reset the genlock private fd flag in the handle @@ -141,11 +148,18 @@ int gralloc_register_buffer(gralloc_module_t const* module, // 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 - return genlock_attach_lock((native_handle_t *)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; } From 1e746398262084c4d9edf5d34670cfe05189cc20 Mon Sep 17 00:00:00 2001 From: Naomi Luis Date: Sun, 20 Nov 2011 00:40:05 -0800 Subject: [PATCH 4/4] libgralloc: Invoke genlock during gralloc_lock/gralloc_unlock gralloc_lock and gralloc_unlock invokes genlock to perform the buffer locking operation. Remove unused gralloc handle variables. Change-Id: I5283613c5f67f708123916653af1b2d1ec155577 --- libgralloc/gpu.cpp | 1 - libgralloc/gralloc_priv.h | 15 ++--- libgralloc/mapper.cpp | 131 +++++++++++--------------------------- 3 files changed, 42 insertions(+), 105 deletions(-) diff --git a/libgralloc/gpu.cpp b/libgralloc/gpu.cpp index da9f4c1..014c54b 100755 --- a/libgralloc/gpu.cpp +++ b/libgralloc/gpu.cpp @@ -172,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; } diff --git a/libgralloc/gralloc_priv.h b/libgralloc/gralloc_priv.h index 1d990ba..ced31f6 100644 --- a/libgralloc/gralloc_priv.h +++ b/libgralloc/gralloc_priv.h @@ -278,12 +278,7 @@ 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 @@ -298,8 +293,6 @@ 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; @@ -308,14 +301,14 @@ struct private_handle_t { int genlockPrivFd; // local fd of the genlock device. #ifdef __cplusplus - static const int sNumInts = 14; + 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), genlockHandle(-1), 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), genlockPrivFd(-1) + bufferType(bufferType), base(0), gpuaddr(0), pid(getpid()), format(format), + width(width), height(height), genlockPrivFd(-1) { version = sizeof(native_handle); numInts = sNumInts; diff --git a/libgralloc/mapper.cpp b/libgralloc/mapper.cpp index da1b2fa..2c01b15 100755 --- a/libgralloc/mapper.cpp +++ b/libgralloc/mapper.cpp @@ -140,8 +140,6 @@ int gralloc_register_buffer(gralloc_module_t const* module, return err; } - hnd->lockState = 0; - hnd->writeOwner = 0; // Reset the genlock private fd flag in the handle hnd->genlockPrivFd = -1; @@ -178,19 +176,12 @@ 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); @@ -210,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 | @@ -245,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; } @@ -319,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; @@ -331,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; }