copybit: Pass in proper acquire fence count to blit call

The counter that was incremented when an acquire fence was provided
was not the counter that was passed into the blit call. Due to
blit requests being processed in order that it is submitted, it is
not need to pass in previous release fence as acquire fence for
next blit request.

Change-Id: Ic259af24b33e5e69440d481c92788107ff639bed
diff --git a/libcopybit/copybit.cpp b/libcopybit/copybit.cpp
index 0fecd59..81907cc 100644
--- a/libcopybit/copybit.cpp
+++ b/libcopybit/copybit.cpp
@@ -384,8 +384,8 @@
 {
     struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
     if (acquireFenceFd != -1) {
-        if (ctx->sync.acq_fen_fd_cnt < (MDP_MAX_FENCE_FD - 1)) {
-            ctx->acqFence[ctx->sync.acq_fen_fd_cnt++] = acquireFenceFd;
+        if (ctx->list.sync.acq_fen_fd_cnt < (MDP_MAX_FENCE_FD - 1)) {
+            ctx->acqFence[ctx->list.sync.acq_fen_fd_cnt++] = acquireFenceFd;
         } else {
             int ret = -EINVAL;
             struct blitReq *list = &ctx->list;
@@ -397,9 +397,8 @@
                 return -EINVAL;
             }
             list->count = 0;
-            ctx->sync.acq_fen_fd_cnt = 0;
-            ctx->acqFence[ctx->sync.acq_fen_fd_cnt++] = ctx->relFence;
-            ctx->acqFence[ctx->sync.acq_fen_fd_cnt++] = acquireFenceFd;
+            list->sync.acq_fen_fd_cnt = 0;
+            ctx->acqFence[list->sync.acq_fen_fd_cnt++] = acquireFenceFd;
             ctx->relFence = -1;
         }
     }
@@ -508,10 +507,7 @@
             if (++list->count == maxCount) {
                 status = msm_copybit(ctx, list);
                 if (ctx->relFence != -1) {
-                    ctx->sync.acq_fen_fd_cnt = 1;
-                    ctx->sync.acq_fen_fd[0] = ctx->relFence;
-                } else {
-                    ctx->sync.acq_fen_fd_cnt = 0;
+                    list->sync.acq_fen_fd_cnt = 0;
                 }
                 list->count = 0;
             }
@@ -521,10 +517,7 @@
             if (list->count != 0) {
                 status = msm_copybit(ctx, list);
                 if (ctx->relFence != -1) {
-                    ctx->sync.acq_fen_fd_cnt = 1;
-                    ctx->sync.acq_fen_fd[0] = ctx->relFence;
-                } else {
-                    ctx->sync.acq_fen_fd_cnt = 0;
+                    list->sync.acq_fen_fd_cnt = 0;
                 }
                 list->count = 0;
             }
@@ -581,7 +574,7 @@
         list->count = 0;
     }
     *fd = ctx->relFence;
-    ctx->sync.acq_fen_fd_cnt = 0;
+    list->sync.acq_fen_fd_cnt = 0;
     ctx->relFence = -1;
     return ret;
 }
@@ -609,10 +602,10 @@
     ctx->mAlpha = MDP_ALPHA_NOP;
     ctx->mFlags = 0;
     ctx->sync.flags = 0;
-    ctx->sync.acq_fen_fd_cnt = 0;
     ctx->sync.acq_fen_fd = ctx->acqFence;
     ctx->sync.rel_fen_fd = &ctx->relFence;
     ctx->list.count = 0;
+    ctx->list.sync.acq_fen_fd_cnt = 0;
     ctx->list.sync.rel_fen_fd = ctx->sync.rel_fen_fd;
     ctx->list.sync.acq_fen_fd = ctx->sync.acq_fen_fd;
     ctx->mFD = open("/dev/graphics/fb0", O_RDWR, 0);
diff --git a/libhwcomposer/hwc_copybit.cpp b/libhwcomposer/hwc_copybit.cpp
index 287c11d..31c2d38 100644
--- a/libhwcomposer/hwc_copybit.cpp
+++ b/libhwcomposer/hwc_copybit.cpp
@@ -455,8 +455,11 @@
                 return err;
             }
             // use release fence as aquire fd for next stretch
-            if (ctx->mMDP.version < qdutils::MDP_V4_0)
+            if (ctx->mMDP.version < qdutils::MDP_V4_0) {
                 copybit->flush_get_fence(copybit, &acquireFd);
+                close(acquireFd);
+                acquireFd = -1;
+            }
             // copy new src and src rect crop
             src = tmp_dst;
             srcRect = tmp_rect;