gralloc1: Close acquire_fence in lock

Bug: 36635948
Bug: 36612621
Test: Verified to fix fence leak in SUW
Change-Id: I2069e828eedad0c0c23e6cd7ba05d26d7a2f2612
(cherry picked from commit c8261afb613ea9eb269951cc347fd97502e54b78)
diff --git a/msm8996/libgralloc1/gr_device_impl.cpp b/msm8996/libgralloc1/gr_device_impl.cpp
index 3649111..52bd6c5 100644
--- a/msm8996/libgralloc1/gr_device_impl.cpp
+++ b/msm8996/libgralloc1/gr_device_impl.cpp
@@ -381,14 +381,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;