Cache frame event history producer-side.

* Producer maintains a recent history of frames.
* Producer only does a binder call if requested
    informatiVon doesn't exist in the cache.
* Consumer sends fences to the producer, which
    can be queried for timestamps without a
    binder call.

Test: adb shell /data/nativetest/libgui_test/libgui_test
--gtest_filter=*GetFrameTimestamps*

Change-Id: I8a64579407cc2935f5c659462cb227b07ba27e43
diff --git a/libs/gui/Surface.cpp b/libs/gui/Surface.cpp
index 5203cce..c29101e 100644
--- a/libs/gui/Surface.cpp
+++ b/libs/gui/Surface.cpp
@@ -145,47 +145,61 @@
         nsecs_t* outReleaseTime) {
     ATRACE_CALL();
 
-    {
-        Mutex::Autolock lock(mMutex);
+    Mutex::Autolock lock(mMutex);
 
-        // Verify the requested timestamps are supported.
-        querySupportedTimestampsLocked();
-        if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
-            return BAD_VALUE;
-        }
-        if (outDisplayRetireTime != nullptr && !mFrameTimestampsSupportsRetire) {
-            return BAD_VALUE;
-        }
+    // Verify the requested timestamps are supported.
+    querySupportedTimestampsLocked();
+    if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
+        return BAD_VALUE;
+    }
+    if (outDisplayRetireTime != nullptr && !mFrameTimestampsSupportsRetire) {
+        return BAD_VALUE;
     }
 
-    FrameTimestamps timestamps;
-    bool found = mGraphicBufferProducer->getFrameTimestamps(frameNumber,
-            &timestamps);
+    FrameEvents* events = mFrameEventHistory.getFrame(frameNumber);
 
-    if (!found) {
+    // Update our cache of events if the requested events are not available.
+    if (events == nullptr ||
+        (outRequestedPresentTime && !events->hasRequestedPresentInfo()) ||
+        (outAcquireTime && !events->hasAcquireInfo()) ||
+        (outRefreshStartTime && !events->hasFirstRefreshStartInfo()) ||
+        (outGlCompositionDoneTime && !events->hasGpuCompositionDoneInfo()) ||
+        (outDisplayPresentTime && !events->hasDisplayPresentInfo()) ||
+        (outDisplayRetireTime && !events->hasDisplayRetireInfo()) ||
+        (outReleaseTime && !events->hasReleaseInfo())) {
+            FrameEventHistoryDelta delta;
+            mGraphicBufferProducer->getFrameTimestamps(&delta);
+            mFrameEventHistory.applyDelta(delta);
+            events = mFrameEventHistory.getFrame(frameNumber);
+    }
+
+    // A record for the requested frame does not exist.
+    if (events == nullptr) {
         return NAME_NOT_FOUND;
     }
 
+    events->checkFencesForCompletion();
+
     if (outRequestedPresentTime) {
-        *outRequestedPresentTime = timestamps.requestedPresentTime;
+        *outRequestedPresentTime = events->requestedPresentTime;
     }
     if (outAcquireTime) {
-        *outAcquireTime = timestamps.acquireTime;
+        *outAcquireTime = events->acquireTime;
     }
     if (outRefreshStartTime) {
-        *outRefreshStartTime = timestamps.refreshStartTime;
+        *outRefreshStartTime = events->firstRefreshStartTime;
     }
     if (outGlCompositionDoneTime) {
-        *outGlCompositionDoneTime = timestamps.glCompositionDoneTime;
+        *outGlCompositionDoneTime = events->gpuCompositionDoneTime;
     }
     if (outDisplayPresentTime) {
-        *outDisplayPresentTime = timestamps.displayPresentTime;
+        *outDisplayPresentTime = events->displayPresentTime;
     }
     if (outDisplayRetireTime) {
-        *outDisplayRetireTime = timestamps.displayRetireTime;
+        *outDisplayRetireTime = events->displayRetireTime;
     }
     if (outReleaseTime) {
-        *outReleaseTime = timestamps.releaseTime;
+        *outReleaseTime = events->releaseTime;
     }
 
     return NO_ERROR;
@@ -564,7 +578,7 @@
     }
     mQueriedSupportedTimestamps = true;
 
-    std::vector<SupportableFrameTimestamps> supportedFrameTimestamps;
+    std::vector<FrameEvent> supportedFrameTimestamps;
     sp<ISurfaceComposer> composer(ComposerService::getComposerService());
     status_t err = composer->getSupportedFrameTimestamps(
             &supportedFrameTimestamps);
@@ -574,9 +588,9 @@
     }
 
     for (auto sft : supportedFrameTimestamps) {
-        if (sft == SupportableFrameTimestamps::DISPLAY_PRESENT_TIME) {
+        if (sft == FrameEvent::DISPLAY_PRESENT) {
             mFrameTimestampsSupportsPresent = true;
-        } else if (sft == SupportableFrameTimestamps::DISPLAY_RETIRE_TIME) {
+        } else if (sft == FrameEvent::DISPLAY_RETIRE) {
             mFrameTimestampsSupportsRetire = true;
         }
     }