copybit: Enable copy bit to use sync pt

Since mdp composition is now an async call, the waiting for fences
is now no longer needed inside copybit. In addition, how
composition ioctl is called is slightly different as a result
needing to pass in aquire fences device performing compsition

Change-Id: Ia12dfb2960ba2fc78b14e776984ffe0c3fe45fdb
diff --git a/libhwcomposer/hwc_copybit.cpp b/libhwcomposer/hwc_copybit.cpp
index 8589204..d402e0c 100644
--- a/libhwcomposer/hwc_copybit.cpp
+++ b/libhwcomposer/hwc_copybit.cpp
@@ -267,7 +267,8 @@
             continue;
         }
         int ret = -1;
-        if (list->hwLayers[i].acquireFenceFd != -1 ) {
+        if (list->hwLayers[i].acquireFenceFd != -1
+                && ctx->mMDP.version >= qdutils::MDP_V4_0) {
             // Wait for acquire Fence on the App buffers.
             ret = sync_wait(list->hwLayers[i].acquireFenceFd, 1000);
             if(ret < 0) {
@@ -297,7 +298,7 @@
                                      private_handle_t *renderBuffer, int dpy)
 {
     hwc_context_t* ctx = (hwc_context_t*)(dev);
-    int err = 0;
+    int err = 0, acquireFd;
     if(!ctx) {
          ALOGE("%s: null context ", __FUNCTION__);
          return -1;
@@ -391,6 +392,7 @@
                                           scaleLimitMax,1/scaleLimitMin);
         return -1;
     }
+    acquireFd = layer->acquireFenceFd;
     if(dsdx > copybitsMaxScale ||
         dtdy > copybitsMaxScale ||
         dsdx < 1/copybitsMinScale ||
@@ -446,6 +448,7 @@
             copybit->set_parameter(copybit,COPYBIT_TRANSFORM,0);
             //TODO: once, we are able to read layer alpha, update this
             copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
+            copybit->set_sync(copybit, acquireFd);
             err = copybit->stretch(copybit,&tmp_dst, &src, &tmp_rect,
                                                            &srcRect, &tmp_it);
             if(err < 0){
@@ -455,6 +458,9 @@
                     free_buffer(tmpHnd);
                 return err;
             }
+            // use release fence as aquire fd for next stretch
+            if (ctx->mMDP.version < qdutils::MDP_V4_0)
+                copybit->flush_get_fence(copybit, &acquireFd);
             // copy new src and src rect crop
             src = tmp_dst;
             srcRect = tmp_rect;
@@ -479,13 +485,26 @@
                                              COPYBIT_ENABLE : COPYBIT_DISABLE);
     copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
                                                 COPYBIT_ENABLE);
+    copybit->set_sync(copybit, acquireFd);
     err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect,
                                                    &copybitRegion);
     copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
                                                COPYBIT_DISABLE);
 
-    if(tmpHnd)
+    if(tmpHnd) {
+        if (ctx->mMDP.version < qdutils::MDP_V4_0){
+            int ret = -1, releaseFd;
+            // we need to wait for the buffer before freeing
+            copybit->flush_get_fence(copybit, &releaseFd);
+            ret = sync_wait(releaseFd, 1000);
+            if(ret < 0) {
+                ALOGE("%s: sync_wait error!! error no = %d err str = %s",
+                    __FUNCTION__, errno, strerror(errno));
+            }
+            close(releaseFd);
+        }
         free_buffer(tmpHnd);
+    }
 
     if(err < 0)
         ALOGE("%s: copybit stretch failed",__FUNCTION__);