hwc: Close acquireFenceFds always.

Close acquireFenceFds always even in case of failures if a layer is either
OVERLAY or FRAMEBUFFER_TARGET to prevent leaks in case of failures.
The framework is *not* responsible for closing acquire fds, for layers
marked as above two.

Change-Id: Ia6c751d3ec25f196f5503120894fc6cc692b9d25
diff --git a/libhwcomposer/hwc_utils.cpp b/libhwcomposer/hwc_utils.cpp
index fe3401b..9e01565 100644
--- a/libhwcomposer/hwc_utils.cpp
+++ b/libhwcomposer/hwc_utils.cpp
@@ -346,6 +346,17 @@
     return ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive;
 }
 
+void closeAcquireFds(hwc_display_contents_1_t* list) {
+    for(uint32_t i = 0; list && i < list->numHwLayers; i++) {
+        //Close the acquireFenceFds
+        //HWC_FRAMEBUFFER are -1 already by SF, rest we close.
+        if(list->hwLayers[i].acquireFenceFd >= 0) {
+            close(list->hwLayers[i].acquireFenceFd);
+            list->hwLayers[i].acquireFenceFd = -1;
+        }
+    }
+}
+
 int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy,
                                                         int fd) {
     int ret = 0;
@@ -397,22 +408,15 @@
         ALOGD_IF(HWC_UTILS_DEBUG, "%s: time taken for MSMFB_BUFFER_SYNC IOCTL = %d",
                             __FUNCTION__, (size_t) ns2ms(systemTime() - start));
     }
+
     if(ret < 0) {
         ALOGE("ioctl MSMFB_BUFFER_SYNC failed, err=%s",
                 strerror(errno));
     }
+
     for(uint32_t i = 0; i < list->numHwLayers; i++) {
         if(list->hwLayers[i].compositionType == HWC_OVERLAY ||
            list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
-            //Close the acquireFenceFds
-            if(list->hwLayers[i].acquireFenceFd >= 0) {
-                close(list->hwLayers[i].acquireFenceFd);
-                list->hwLayers[i].acquireFenceFd = -1;
-            }
-            if(fd >= 0) {
-                close(fd);
-                fd = -1;
-            }
             //Populate releaseFenceFds.
             if(UNLIKELY(swapzero))
                 list->hwLayers[i].releaseFenceFd = -1;
@@ -420,12 +424,19 @@
                 list->hwLayers[i].releaseFenceFd = dup(releaseFd);
         }
     }
+
+    if(fd >= 0) {
+        close(fd);
+        fd = -1;
+    }
+
     if(UNLIKELY(swapzero)){
         list->retireFenceFd = -1;
         close(releaseFd);
     } else {
         list->retireFenceFd = releaseFd;
     }
+
     return ret;
 }