Merge "Surface: Ensure synchronisation of copyBlt" am: 88f77814a1
am: 5a2dcde971

Change-Id: Ifb17a3d79f367bf22e01746209453f8b36bb35b5
diff --git a/libs/gui/Surface.cpp b/libs/gui/Surface.cpp
index 8e6ab1c..7607b42 100644
--- a/libs/gui/Surface.cpp
+++ b/libs/gui/Surface.cpp
@@ -20,6 +20,13 @@
 
 #include <android/native_window.h>
 
+// We would eliminate the non-conforming zero-length array, but we can't since
+// this is effectively included from the Linux kernel
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wzero-length-array"
+#include <sync/sync.h>
+#pragma clang diagnostic pop
+
 #include <binder/Parcel.h>
 
 #include <utils/Log.h>
@@ -1269,8 +1276,14 @@
         if (canCopyBack) {
             // copy the area that is invalid and not repainted this round
             const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
-            if (!copyback.isEmpty())
+            if (!copyback.isEmpty()) {
+                if (fenceFd >= 0) {
+                    sync_wait(fenceFd, -1);
+                    close(fenceFd);
+                    fenceFd = -1;
+                }
                 copyBlt(backBuffer, frontBuffer, copyback);
+            }
         } else {
             // if we can't copy-back anything, modify the user's dirty
             // region to make sure they redraw the whole buffer