SurfaceFlinger: fix early latching of buffers

Use the FrameTimeline::SurfaceFrame predicted present time
to avoid latching a buffer too early (i.e. before the time
SurfaceFlinger planned to latch this buffer when the app requested
a vsync callback).

Bug: 169901895
Test: expand notification shade and observe systrace
Change-Id: I823546992c89f88c0c29b839ce21c79ff1ffcfbd
diff --git a/services/surfaceflinger/BufferStateLayer.cpp b/services/surfaceflinger/BufferStateLayer.cpp
index df1472d..7ec7f36 100644
--- a/services/surfaceflinger/BufferStateLayer.cpp
+++ b/services/surfaceflinger/BufferStateLayer.cpp
@@ -601,6 +601,14 @@
     return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
 }
 
+nsecs_t BufferStateLayer::nextPredictedPresentTime() const {
+    if (!getDrawingState().isAutoTimestamp || !mSurfaceFrame) {
+        return 0;
+    }
+
+    return mSurfaceFrame->getPredictions().presentTime;
+}
+
 status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
                                           nsecs_t /*expectedPresentTime*/) {
     const State& s(getDrawingState());