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/services/surfaceflinger/Layer.cpp b/services/surfaceflinger/Layer.cpp
index c16af59..2334e47 100644
--- a/services/surfaceflinger/Layer.cpp
+++ b/services/surfaceflinger/Layer.cpp
@@ -2215,24 +2215,16 @@
     mFrameEventHistory.dump(result);
 }
 
-bool Layer::addAndGetFrameTimestamps(
-        const NewFrameEventsEntry* newTimestamps,
-        uint64_t frameNumber, FrameTimestamps *outTimestamps) {
+void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
+        FrameEventHistoryDelta *outDelta) {
     Mutex::Autolock lock(mFrameEventHistoryMutex);
     if (newTimestamps) {
         mFrameEventHistory.addQueue(*newTimestamps);
     }
 
-    if (outTimestamps) {
-        FrameEvents* frameEvents = mFrameEventHistory.getFrame(frameNumber);
-        if (frameEvents) {
-            frameEvents->checkFencesForCompletion();
-            *outTimestamps = FrameTimestamps(*frameEvents);
-            return true;
-        }
+    if (outDelta) {
+        mFrameEventHistory.getAndResetDelta(outDelta);
     }
-
-    return false;
 }
 
 std::vector<OccupancyTracker::Segment> Layer::getOccupancyHistory(
diff --git a/services/surfaceflinger/Layer.h b/services/surfaceflinger/Layer.h
index 98ea053..b4d8685 100644
--- a/services/surfaceflinger/Layer.h
+++ b/services/surfaceflinger/Layer.h
@@ -407,12 +407,10 @@
     void logFrameStats();
     void getFrameStats(FrameStats* outStats) const;
 
-
     std::vector<OccupancyTracker::Segment> getOccupancyHistory(bool forceFlush);
 
-    bool addAndGetFrameTimestamps(
-            const NewFrameEventsEntry* newTimestamps,
-            uint64_t frameNumber, FrameTimestamps* outTimestamps);
+    void addAndGetFrameTimestamps(const NewFrameEventsEntry* newEntry,
+            FrameEventHistoryDelta* outDelta);
 
     bool getTransformToDisplayInverse() const;
 
@@ -587,7 +585,7 @@
     // Timestamp history for the consumer to query.
     // Accessed by both consumer and producer on main and binder threads.
     Mutex mFrameEventHistoryMutex;
-    FrameEventHistory mFrameEventHistory;
+    ConsumerFrameEventHistory mFrameEventHistory;
 
     // main thread
     sp<GraphicBuffer> mActiveBuffer;
diff --git a/services/surfaceflinger/MonitoredProducer.cpp b/services/surfaceflinger/MonitoredProducer.cpp
index 4604e01..a2cc531 100644
--- a/services/surfaceflinger/MonitoredProducer.cpp
+++ b/services/surfaceflinger/MonitoredProducer.cpp
@@ -145,9 +145,8 @@
             outTransformMatrix);
 }
 
-bool MonitoredProducer::getFrameTimestamps(
-        uint64_t frameNumber, FrameTimestamps* outTimestamps) {
-    return mProducer->getFrameTimestamps(frameNumber, outTimestamps);
+void MonitoredProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
+    mProducer->getFrameTimestamps(outDelta);
 }
 
 status_t MonitoredProducer::getUniqueId(uint64_t* outId) const {
diff --git a/services/surfaceflinger/MonitoredProducer.h b/services/surfaceflinger/MonitoredProducer.h
index 5a2351f..3e64cae 100644
--- a/services/surfaceflinger/MonitoredProducer.h
+++ b/services/surfaceflinger/MonitoredProducer.h
@@ -63,8 +63,7 @@
     virtual IBinder* onAsBinder();
     virtual status_t setSharedBufferMode(bool sharedBufferMode) override;
     virtual status_t setAutoRefresh(bool autoRefresh) override;
-    virtual bool getFrameTimestamps(uint64_t frameNumber,
-            FrameTimestamps* outTimestamps) override;
+    virtual void getFrameTimestamps(FrameEventHistoryDelta *outDelta) override;
     virtual status_t getUniqueId(uint64_t* outId) const override;
 
 private:
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 8400136..45d1477 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -567,16 +567,15 @@
 }
 
 status_t SurfaceFlinger::getSupportedFrameTimestamps(
-        std::vector<SupportableFrameTimestamps>* outSupported) const {
+        std::vector<FrameEvent>* outSupported) const {
     *outSupported = {
-        SupportableFrameTimestamps::REQUESTED_PRESENT,
-        SupportableFrameTimestamps::ACQUIRE,
-        SupportableFrameTimestamps::REFRESH_START,
-        SupportableFrameTimestamps::GL_COMPOSITION_DONE_TIME,
+        FrameEvent::REQUESTED_PRESENT,
+        FrameEvent::ACQUIRE,
+        FrameEvent::FIRST_REFRESH_START,
+        FrameEvent::GL_COMPOSITION_DONE,
         getHwComposer().retireFenceRepresentsStartOfScanout() ?
-                SupportableFrameTimestamps::DISPLAY_PRESENT_TIME :
-                SupportableFrameTimestamps::DISPLAY_RETIRE_TIME,
-        SupportableFrameTimestamps::RELEASE_TIME,
+                FrameEvent::DISPLAY_PRESENT : FrameEvent::DISPLAY_RETIRE,
+        FrameEvent::RELEASE,
     };
     return NO_ERROR;
 }
diff --git a/services/surfaceflinger/SurfaceFlinger.h b/services/surfaceflinger/SurfaceFlinger.h
index 5b216dd..5bf989b 100644
--- a/services/surfaceflinger/SurfaceFlinger.h
+++ b/services/surfaceflinger/SurfaceFlinger.h
@@ -200,7 +200,7 @@
     virtual bool authenticateSurfaceTexture(
         const sp<IGraphicBufferProducer>& bufferProducer) const;
     virtual status_t getSupportedFrameTimestamps(
-            std::vector<SupportableFrameTimestamps>* outSupported) const;
+            std::vector<FrameEvent>* outSupported) const;
     virtual sp<IDisplayEventConnection> createDisplayEventConnection();
     virtual status_t captureScreen(const sp<IBinder>& display,
             const sp<IGraphicBufferProducer>& producer,
diff --git a/services/surfaceflinger/SurfaceFlingerConsumer.cpp b/services/surfaceflinger/SurfaceFlingerConsumer.cpp
index 1ef7065..029937a 100644
--- a/services/surfaceflinger/SurfaceFlingerConsumer.cpp
+++ b/services/surfaceflinger/SurfaceFlingerConsumer.cpp
@@ -248,15 +248,13 @@
     }
 }
 
-bool SurfaceFlingerConsumer::addAndGetFrameTimestamps(
+void SurfaceFlingerConsumer::addAndGetFrameTimestamps(
         const NewFrameEventsEntry* newTimestamps,
-        uint64_t frameNumber, FrameTimestamps *outTimestamps) {
+        FrameEventHistoryDelta *outDelta) {
     sp<Layer> l = mLayer.promote();
-    if (!l.get()) {
-        return false;
+    if (l.get()) {
+        l->addAndGetFrameTimestamps(newTimestamps, outDelta);
     }
-    return l->addAndGetFrameTimestamps(
-            newTimestamps, frameNumber, outTimestamps);
 }
 
 // ---------------------------------------------------------------------------
diff --git a/services/surfaceflinger/SurfaceFlingerConsumer.h b/services/surfaceflinger/SurfaceFlingerConsumer.h
index 0f46ddc..d3f0070 100644
--- a/services/surfaceflinger/SurfaceFlingerConsumer.h
+++ b/services/surfaceflinger/SurfaceFlingerConsumer.h
@@ -84,9 +84,9 @@
     void releasePendingBuffer();
 #endif
 
-    virtual bool addAndGetFrameTimestamps(
+    virtual void addAndGetFrameTimestamps(
             const NewFrameEventsEntry* newTimestamps,
-            uint64_t frameNumber, FrameTimestamps* outTimestamps) override;
+            FrameEventHistoryDelta* outDelta) override;
 
 private:
     virtual void onSidebandStreamChanged();
diff --git a/services/surfaceflinger/SurfaceFlinger_hwc1.cpp b/services/surfaceflinger/SurfaceFlinger_hwc1.cpp
index 3bdb021..66a3c42 100644
--- a/services/surfaceflinger/SurfaceFlinger_hwc1.cpp
+++ b/services/surfaceflinger/SurfaceFlinger_hwc1.cpp
@@ -600,14 +600,14 @@
 }
 
 status_t SurfaceFlinger::getSupportedFrameTimestamps(
-        std::vector<SupportableFrameTimestamps>* outSupported) const {
+        std::vector<FrameEvent>* outSupported) const {
     *outSupported = {
-        SupportableFrameTimestamps::REQUESTED_PRESENT,
-        SupportableFrameTimestamps::ACQUIRE,
-        SupportableFrameTimestamps::REFRESH_START,
-        SupportableFrameTimestamps::GL_COMPOSITION_DONE_TIME,
-        SupportableFrameTimestamps::DISPLAY_RETIRE_TIME,
-        SupportableFrameTimestamps::RELEASE_TIME,
+        FrameEvent::REQUESTED_PRESENT,
+        FrameEvent::ACQUIRE,
+        FrameEvent::FIRST_REFRESH_START,
+        FrameEvent::GL_COMPOSITION_DONE,
+        FrameEvent::DISPLAY_RETIRE,
+        FrameEvent::RELEASE,
     };
     return NO_ERROR;
 }