Support streaming fps updates for a layer subtree to sysui via listener.

The supported flow is:
* A binder listener is registered with SurfaceFlinger that is associated
with a task's SurfaceControl.
* Every frame, for every listener that is registered, the fps for each
layer subtree is computed and reported back to each listener via
onFpsUpdated.
* Fps for the layer subtree is computed via FrameTimeline: the layer IDs
for the subtree are gathered into a set, and FrameTimeline internally
finds all DisplayFrames containing at least one layer, then obtains the
present time for each of those frames, then computes the aggregated fps
from those frames. Pragmatically, this should correspond with the last
half second of activity for high refresh rate devices.

No heuristics are used to select the "correct" layer from the layer
subtree. If a game is rendering to two layers at a cadence of 30fps but
offset by one 60hz vsync, then the reported fps will be 60fps because
both layers will be counted on the same linear timeline.

No rate limiting is applied in this patch either. The caller is able to
hack in rate-limiting by immediately unregistering the listener after a
FPS callback is dispatched, and then reregistered. Architecturally, it
is not hard to add in rate-limiting at the SurfaceFlinger level, but
that can be added in a follow-up patch since this patch is already
large.

Bug: 174956756
Test: libsurfaceflinger_unittest
Test: E2E verification with custom listener outputting to logcat
Change-Id: I792bc19cad18b6aee6c6e644bca9da40a0f15099
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index da8360e..c42b3f7 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -108,6 +108,7 @@
 #include "DisplayRenderArea.h"
 #include "EffectLayer.h"
 #include "Effects/Daltonizer.h"
+#include "FpsReporter.h"
 #include "FrameTimeline/FrameTimeline.h"
 #include "FrameTracer/FrameTracer.h"
 #include "Layer.h"
@@ -1452,6 +1453,25 @@
     return NO_ERROR;
 }
 
+status_t SurfaceFlinger::addFpsListener(const sp<IBinder>& layerHandle,
+                                        const sp<gui::IFpsListener>& listener) {
+    if (!listener) {
+        return BAD_VALUE;
+    }
+
+    const wp<Layer> layer = fromHandle(layerHandle);
+    mFpsReporter->addListener(listener, layer);
+    return NO_ERROR;
+}
+
+status_t SurfaceFlinger::removeFpsListener(const sp<gui::IFpsListener>& listener) {
+    if (!listener) {
+        return BAD_VALUE;
+    }
+    mFpsReporter->removeListener(listener);
+    return NO_ERROR;
+}
+
 status_t SurfaceFlinger::getDisplayBrightnessSupport(const sp<IBinder>& displayToken,
                                                      bool* outSupport) const {
     if (!displayToken || !outSupport) {
@@ -2138,6 +2158,13 @@
         }
     });
 
+    {
+        Mutex::Autolock lock(mStateLock);
+        if (mFpsReporter) {
+            mFpsReporter->dispatchLayerFps();
+        }
+    }
+
     mTransactionCallbackInvoker.addPresentFence(mPreviousPresentFences[0]);
     mTransactionCallbackInvoker.sendCallbacks();
 
@@ -2954,6 +2981,7 @@
     mRegionSamplingThread =
             new RegionSamplingThread(*this, *mScheduler,
                                      RegionSamplingThread::EnvironmentTimingTunables());
+    mFpsReporter = new FpsReporter(*mFrameTimeline);
     // Dispatch a mode change request for the primary display on scheduler
     // initialization, so that the EventThreads always contain a reference to a
     // prior configuration.
@@ -4940,6 +4968,8 @@
         case GET_DISPLAYED_CONTENT_SAMPLE:
         case NOTIFY_POWER_BOOST:
         case SET_GLOBAL_SHADOW_SETTINGS:
+        case ADD_FPS_LISTENER:
+        case REMOVE_FPS_LISTENER:
         case ACQUIRE_FRAME_RATE_FLEXIBILITY_TOKEN: {
             // ACQUIRE_FRAME_RATE_FLEXIBILITY_TOKEN is used by CTS tests, which acquire the
             // necessary permission dynamically. Don't use the permission cache for this check.