From 923c0668eb2427bb148a56bd1ac7834cc371d5a0 Mon Sep 17 00:00:00 2001 From: Dan Stoza Date: Tue, 21 Jun 2016 16:22:06 -0700 Subject: gralloc1: Add mutexes Adds mutexes to protect both the buffer list and the descriptor list from concurrent access Bug: 29420918 Change-Id: I4d14353ceb167276570c56c9f92d48b28b2d4c53 --- include/ui/Gralloc1On0Adapter.h | 3 +++ libs/ui/Gralloc1On0Adapter.cpp | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/include/ui/Gralloc1On0Adapter.h b/include/ui/Gralloc1On0Adapter.h index edcfb65e91..97c9a89d32 100644 --- a/include/ui/Gralloc1On0Adapter.h +++ b/include/ui/Gralloc1On0Adapter.h @@ -22,6 +22,7 @@ #include +#include #include #include #include @@ -468,8 +469,10 @@ private: std::shared_ptr getBuffer(buffer_handle_t bufferHandle); static std::atomic sNextBufferDescriptorId; + std::mutex mDescriptorMutex; std::unordered_map> mDescriptors; + std::mutex mBufferMutex; std::unordered_map> mBuffers; }; diff --git a/libs/ui/Gralloc1On0Adapter.cpp b/libs/ui/Gralloc1On0Adapter.cpp index 6e69df1621..d5b88deb9a 100644 --- a/libs/ui/Gralloc1On0Adapter.cpp +++ b/libs/ui/Gralloc1On0Adapter.cpp @@ -193,6 +193,7 @@ gralloc1_error_t Gralloc1On0Adapter::createDescriptor( gralloc1_buffer_descriptor_t* outDescriptor) { auto descriptorId = sNextBufferDescriptorId++; + std::lock_guard lock(mDescriptorMutex); mDescriptors.emplace(descriptorId, std::make_shared(this, descriptorId)); @@ -207,6 +208,7 @@ gralloc1_error_t Gralloc1On0Adapter::destroyDescriptor( { ALOGV("Destroying descriptor %" PRIu64, descriptor); + std::lock_guard lock(mDescriptorMutex); if (mDescriptors.count(descriptor) == 0) { return GRALLOC1_ERROR_BAD_DESCRIPTOR; } @@ -255,6 +257,8 @@ gralloc1_error_t Gralloc1On0Adapter::allocate( *outBufferHandle = handle; auto buffer = std::make_shared(handle, store, *descriptor, stride, true); + + std::lock_guard lock(mBufferMutex); mBuffers.emplace(handle, std::move(buffer)); return GRALLOC1_ERROR_NONE; @@ -309,6 +313,8 @@ gralloc1_error_t Gralloc1On0Adapter::release( ALOGE("gralloc0 unregister failed: %d", result); } } + + std::lock_guard lock(mBufferMutex); mBuffers.erase(handle); return GRALLOC1_ERROR_NONE; } @@ -320,6 +326,7 @@ gralloc1_error_t Gralloc1On0Adapter::retain( graphicBuffer->getNativeBuffer()->handle, graphicBuffer->getId()); buffer_handle_t handle = graphicBuffer->getNativeBuffer()->handle; + std::lock_guard lock(mBufferMutex); if (mBuffers.count(handle) != 0) { mBuffers[handle]->retain(); return GRALLOC1_ERROR_NONE; @@ -446,6 +453,7 @@ gralloc1_error_t Gralloc1On0Adapter::unlock( std::shared_ptr Gralloc1On0Adapter::getDescriptor(gralloc1_buffer_descriptor_t descriptorId) { + std::lock_guard lock(mDescriptorMutex); if (mDescriptors.count(descriptorId) == 0) { return nullptr; } @@ -456,6 +464,7 @@ Gralloc1On0Adapter::getDescriptor(gralloc1_buffer_descriptor_t descriptorId) std::shared_ptr Gralloc1On0Adapter::getBuffer( buffer_handle_t bufferHandle) { + std::lock_guard lock(mBufferMutex); if (mBuffers.count(bufferHandle) == 0) { return nullptr; } -- cgit v1.2.3-59-g8ed1b