gralloc1: Add mutexes

Adds mutexes to protect both the buffer list and the descriptor list
from concurrent access

Bug: 29420918
Change-Id: I4d14353ceb167276570c56c9f92d48b28b2d4c53
diff --git a/libs/ui/Gralloc1On0Adapter.cpp b/libs/ui/Gralloc1On0Adapter.cpp
index 6e69df1..d5b88de 100644
--- a/libs/ui/Gralloc1On0Adapter.cpp
+++ b/libs/ui/Gralloc1On0Adapter.cpp
@@ -193,6 +193,7 @@
         gralloc1_buffer_descriptor_t* outDescriptor)
 {
     auto descriptorId = sNextBufferDescriptorId++;
+    std::lock_guard<std::mutex> lock(mDescriptorMutex);
     mDescriptors.emplace(descriptorId,
             std::make_shared<Descriptor>(this, descriptorId));
 
@@ -207,6 +208,7 @@
 {
     ALOGV("Destroying descriptor %" PRIu64, descriptor);
 
+    std::lock_guard<std::mutex> lock(mDescriptorMutex);
     if (mDescriptors.count(descriptor) == 0) {
         return GRALLOC1_ERROR_BAD_DESCRIPTOR;
     }
@@ -255,6 +257,8 @@
     *outBufferHandle = handle;
     auto buffer = std::make_shared<Buffer>(handle, store, *descriptor, stride,
             true);
+
+    std::lock_guard<std::mutex> lock(mBufferMutex);
     mBuffers.emplace(handle, std::move(buffer));
 
     return GRALLOC1_ERROR_NONE;
@@ -309,6 +313,8 @@
             ALOGE("gralloc0 unregister failed: %d", result);
         }
     }
+
+    std::lock_guard<std::mutex> lock(mBufferMutex);
     mBuffers.erase(handle);
     return GRALLOC1_ERROR_NONE;
 }
@@ -320,6 +326,7 @@
             graphicBuffer->getNativeBuffer()->handle, graphicBuffer->getId());
 
     buffer_handle_t handle = graphicBuffer->getNativeBuffer()->handle;
+    std::lock_guard<std::mutex> lock(mBufferMutex);
     if (mBuffers.count(handle) != 0) {
         mBuffers[handle]->retain();
         return GRALLOC1_ERROR_NONE;
@@ -446,6 +453,7 @@
 std::shared_ptr<Gralloc1On0Adapter::Descriptor>
 Gralloc1On0Adapter::getDescriptor(gralloc1_buffer_descriptor_t descriptorId)
 {
+    std::lock_guard<std::mutex> lock(mDescriptorMutex);
     if (mDescriptors.count(descriptorId) == 0) {
         return nullptr;
     }
@@ -456,6 +464,7 @@
 std::shared_ptr<Gralloc1On0Adapter::Buffer> Gralloc1On0Adapter::getBuffer(
         buffer_handle_t bufferHandle)
 {
+    std::lock_guard<std::mutex> lock(mBufferMutex);
     if (mBuffers.count(bufferHandle) == 0) {
         return nullptr;
     }