Defer surfaceflinger composition until data latch

If layer data is not yet ready, latchBuffer will return an empty region.
SurfaceFlinger::handlePageFlip will now check if any layer has a queued
frame which succeeded in latching data.
If no new frame is latched, handlePageFlip will return false.
That will defer refresh until the next vsync.
That reduces HWC and GPU load, improving framerates.
It prevents dEQP tests from timing out on some devices.

Test: dEQP-VK.wsi.android.incremental_present.scale_down.fifo.reference
Bug: 37439915
Change-Id: Id46c82f79953f3a147af77bb0e8e81a395c606b3
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 746d3d9..9cd1214 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -2371,6 +2371,7 @@
 
     bool visibleRegions = false;
     bool frameQueued = false;
+    bool newDataLatched = false;
 
     // Store the set of layers that need updates. This set must not change as
     // buffers are being latched, as this could result in a deadlock.
@@ -2398,6 +2399,9 @@
         const Region dirty(layer->latchBuffer(visibleRegions, latchTime));
         layer->useSurfaceDamage();
         invalidateLayerStack(layer->getLayerStack(), dirty);
+        if (!dirty.isEmpty()) {
+            newDataLatched = true;
+        }
     }
 
     mVisibleRegionsDirty |= visibleRegions;
@@ -2410,7 +2414,7 @@
     }
 
     // Only continue with the refresh if there is actually new work to do
-    return !mLayersWithQueuedFrames.empty();
+    return !mLayersWithQueuedFrames.empty() && newDataLatched;
 }
 
 void SurfaceFlinger::invalidateHwcGeometry()