Merge "sdm: Reduce scope of lock in deinit"
diff --git a/libgralloc1/gr_allocator.cpp b/libgralloc1/gr_allocator.cpp
index 5e3ee9b..045cc63 100644
--- a/libgralloc1/gr_allocator.cpp
+++ b/libgralloc1/gr_allocator.cpp
@@ -96,7 +96,7 @@
 int Allocator::AllocateMem(AllocData *alloc_data, gralloc1_producer_usage_t prod_usage,
                            gralloc1_consumer_usage_t cons_usage) {
   int ret;
-  alloc_data->uncached = UseUncached(prod_usage);
+  alloc_data->uncached = UseUncached(prod_usage, cons_usage);
 
   // After this point we should have the right heap set, there is no fallback
   GetIonHeapInfo(prod_usage, cons_usage, &alloc_data->heap_id, &alloc_data->alloc_type,
@@ -158,7 +158,8 @@
   *max_index = -1;
   for (uint32_t i = 0; i < num_descriptors; i++) {
     // Check Cached vs non-cached and all the ION flags
-    cur_uncached = UseUncached(descriptors[i]->GetProducerUsage());
+    cur_uncached = UseUncached(descriptors[i]->GetProducerUsage(),
+                               descriptors[i]->GetConsumerUsage());
     GetIonHeapInfo(descriptors[i]->GetProducerUsage(), descriptors[i]->GetConsumerUsage(),
                    &cur_heap_id, &cur_alloc_type, &cur_ion_flags);
 
@@ -225,22 +226,27 @@
 /* The default policy is to return cached buffers unless the client explicity
  * sets the PRIVATE_UNCACHED flag or indicates that the buffer will be rarely
  * read or written in software. */
-// TODO(user) : As of now relying only on producer usage
-bool Allocator::UseUncached(gralloc1_producer_usage_t usage) {
-  if ((usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_UNCACHED) ||
-      (usage & GRALLOC1_PRODUCER_USAGE_PROTECTED)) {
+bool Allocator::UseUncached(gralloc1_producer_usage_t prod_usage,
+                            gralloc1_consumer_usage_t cons_usage) {
+  if ((prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_UNCACHED) ||
+      (prod_usage & GRALLOC1_PRODUCER_USAGE_PROTECTED)) {
     return true;
   }
 
   // CPU read rarely
-  if ((usage & GRALLOC1_PRODUCER_USAGE_CPU_READ) &&
-      !(usage & GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN)) {
+  if ((prod_usage & GRALLOC1_PRODUCER_USAGE_CPU_READ) &&
+      !(prod_usage & GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN)) {
     return true;
   }
 
   // CPU  write rarely
-  if ((usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE) &&
-      !(usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN)) {
+  if ((prod_usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE) &&
+      !(prod_usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN)) {
+    return true;
+  }
+
+  if ((prod_usage & GRALLOC1_PRODUCER_USAGE_SENSOR_DIRECT_DATA) ||
+      (cons_usage & GRALLOC1_CONSUMER_USAGE_GPU_DATA_BUFFER)) {
     return true;
   }
 
@@ -302,5 +308,4 @@
 
   return;
 }
-
 }  // namespace gralloc1
diff --git a/libgralloc1/gr_allocator.h b/libgralloc1/gr_allocator.h
index 6f9de30..d57f50e 100644
--- a/libgralloc1/gr_allocator.h
+++ b/libgralloc1/gr_allocator.h
@@ -61,7 +61,7 @@
                              ssize_t *max_index);
   int GetImplDefinedFormat(gralloc1_producer_usage_t prod_usage,
                            gralloc1_consumer_usage_t cons_usage, int format);
-  bool UseUncached(gralloc1_producer_usage_t usage);
+  bool UseUncached(gralloc1_producer_usage_t prod_usage, gralloc1_consumer_usage_t cons_usage);
 
  private:
   void GetIonHeapInfo(gralloc1_producer_usage_t prod_usage, gralloc1_consumer_usage_t cons_usage,
diff --git a/libgralloc1/gr_buf_mgr.cpp b/libgralloc1/gr_buf_mgr.cpp
index e0d44e7..4c3f7d4 100644
--- a/libgralloc1/gr_buf_mgr.cpp
+++ b/libgralloc1/gr_buf_mgr.cpp
@@ -454,7 +454,7 @@
     flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER;
   }
 
-  if (!allocator_->UseUncached(prod_usage)) {
+  if (!allocator_->UseUncached(prod_usage, cons_usage)) {
     flags |= private_handle_t::PRIV_FLAGS_CACHED;
   }
 
@@ -499,7 +499,7 @@
   data.align = GetDataAlignment(format, prod_usage, cons_usage);
   data.size = ALIGN(size, data.align);
   data.handle = (uintptr_t) handle;
-  data.uncached = allocator_->UseUncached(prod_usage);
+  data.uncached = allocator_->UseUncached(prod_usage, cons_usage);
 
   // Allocate buffer memory
   err = allocator_->AllocateMem(&data, prod_usage, cons_usage);
diff --git a/libgralloc1/gr_device_impl.cpp b/libgralloc1/gr_device_impl.cpp
index 08173e8..98ce595 100644
--- a/libgralloc1/gr_device_impl.cpp
+++ b/libgralloc1/gr_device_impl.cpp
@@ -409,14 +409,26 @@
   return status;
 }
 
+static inline void CloseFdIfValid(int fd) {
+  if (fd > 0) {
+    close(fd);
+  }
+}
+
 gralloc1_error_t GrallocImpl::LockBuffer(gralloc1_device_t *device, buffer_handle_t buffer,
                                          gralloc1_producer_usage_t prod_usage,
                                          gralloc1_consumer_usage_t cons_usage,
                                          const gralloc1_rect_t *region, void **out_data,
                                          int32_t acquire_fence) {
   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
-  if (status == GRALLOC1_ERROR_NONE && (acquire_fence > 0)) {
+  if (status != GRALLOC1_ERROR_NONE) {
+    CloseFdIfValid(acquire_fence);
+    return status;
+  }
+
+  if (acquire_fence > 0) {
     int error = sync_wait(acquire_fence, 1000);
+    CloseFdIfValid(acquire_fence);
     if (error < 0) {
       ALOGE("%s: sync_wait timedout! error = %s", __FUNCTION__, strerror(errno));
       return GRALLOC1_ERROR_UNDEFINED;
diff --git a/sdm/libs/hwc2/hwc_layers.cpp b/sdm/libs/hwc2/hwc_layers.cpp
index a710543..cc090e5 100644
--- a/sdm/libs/hwc2/hwc_layers.cpp
+++ b/sdm/libs/hwc2/hwc_layers.cpp
@@ -599,7 +599,7 @@
   ColorPrimaries sdm_primaries = {};
   ColorRange sdm_range = {};
 
-  auto transfer = (dataspace_ & HAL_DATASPACE_TRANSFER_MASK) >> HAL_DATASPACE_TRANSFER_SHIFT;
+  auto transfer = dataspace_ & HAL_DATASPACE_TRANSFER_MASK;
   // Handle transfer
   switch (transfer) {
     case HAL_DATASPACE_TRANSFER_SRGB:
@@ -619,7 +619,7 @@
   }
 
   // Handle standard
-  auto standard = (dataspace_ & HAL_DATASPACE_STANDARD_MASK) >> HAL_DATASPACE_STANDARD_SHIFT;
+  auto standard = dataspace_ & HAL_DATASPACE_STANDARD_MASK;
   switch (standard) {
     case  HAL_DATASPACE_STANDARD_BT709:
       sdm_primaries = ColorPrimaries_BT709_5;
@@ -644,7 +644,7 @@
   // TODO(user): Check transfer + primary combination
 
   // Handle range
-  auto range = (dataspace_ & HAL_DATASPACE_RANGE_MASK) >> HAL_DATASPACE_RANGE_SHIFT;
+  auto range = dataspace_ & HAL_DATASPACE_RANGE_MASK;
   switch (range) {
     case HAL_DATASPACE_RANGE_FULL:
       sdm_range = Range_Full;