Merge "Recovery mode UI flicker"
am: 629bde8c6c

Change-Id: I2d9c2a88e8df9d5a096aa70a4a420754c668de26
diff --git a/minui/Android.mk b/minui/Android.mk
index 3057f45..380ec2b 100644
--- a/minui/Android.mk
+++ b/minui/Android.mk
@@ -11,6 +11,7 @@
 
 LOCAL_WHOLE_STATIC_LIBRARIES += libadf
 LOCAL_WHOLE_STATIC_LIBRARIES += libdrm
+LOCAL_WHOLE_STATIC_LIBRARIES += libsync_recovery
 LOCAL_STATIC_LIBRARIES += libpng
 
 LOCAL_MODULE := libminui
diff --git a/minui/graphics_adf.cpp b/minui/graphics_adf.cpp
index a72e40b..3c35410 100644
--- a/minui/graphics_adf.cpp
+++ b/minui/graphics_adf.cpp
@@ -26,11 +26,13 @@
 #include <sys/mman.h>
 
 #include <adf/adf.h>
+#include <sync/sync.h>
 
 #include "graphics.h"
 
 struct adf_surface_pdata {
     GRSurface base;
+    int fence_fd;
     int fd;
     __u32 offset;
     __u32 pitch;
@@ -55,6 +57,7 @@
 static int adf_surface_init(adf_pdata *pdata, drm_mode_modeinfo *mode, adf_surface_pdata *surf) {
     memset(surf, 0, sizeof(*surf));
 
+    surf->fence_fd = -1;
     surf->fd = adf_interface_simple_buffer_alloc(pdata->intf_fd, mode->hdisplay,
             mode->vdisplay, pdata->format, &surf->offset, &surf->pitch);
     if (surf->fd < 0)
@@ -194,6 +197,23 @@
     return ret;
 }
 
+static void adf_sync(adf_surface_pdata *surf)
+{
+    unsigned int warningTimeout = 3000;
+
+    if (surf == NULL)
+        return;
+
+    if (surf->fence_fd >= 0){
+        int err = sync_wait(surf->fence_fd, warningTimeout);
+        if (err < 0)
+            perror("adf sync fence wait error\n");
+
+        close(surf->fence_fd);
+        surf->fence_fd = -1;
+    }
+}
+
 static GRSurface* adf_flip(minui_backend *backend)
 {
     adf_pdata *pdata = (adf_pdata *)backend;
@@ -203,9 +223,10 @@
             surf->base.width, surf->base.height, pdata->format, surf->fd,
             surf->offset, surf->pitch, -1);
     if (fence_fd >= 0)
-        close(fence_fd);
+        surf->fence_fd = fence_fd;
 
     pdata->current_surface = (pdata->current_surface + 1) % pdata->n_surfaces;
+    adf_sync(&pdata->surfaces[pdata->current_surface]);
     return &pdata->surfaces[pdata->current_surface].base;
 }
 
@@ -219,6 +240,7 @@
 static void adf_surface_destroy(adf_surface_pdata *surf)
 {
     munmap(surf->base.data, surf->pitch * surf->base.height);
+    close(surf->fence_fd);
     close(surf->fd);
 }