SF: Updating content FPS tracking

1) Each time SF creates a layer, register it with Scheduler and return handle
2) BufferQueueLayer and BufferStateLayer can now send information about buffers
   for given layers via layer handle.

Algorithm for detecting content fps:
1) Keep the refresh rate per layer (explicit timestamp, or 0).
2) Keep information about last 10 present or update timestamps. This will be an
   indicator for precedence.
3) Choose the MAX refresh rate among last updated layers.

For more info see go/surface-flinger-scheduler and
go/content-fps-detection-in-scheduler

Test: Updating unit tests. Systrace.

Change-Id: I988a7a79e9a9f0f61674c9b637c5142db3336177
Bug: 127727337
diff --git a/services/surfaceflinger/Scheduler/Scheduler.h b/services/surfaceflinger/Scheduler/Scheduler.h
index 73896d5..1d53252 100644
--- a/services/surfaceflinger/Scheduler/Scheduler.h
+++ b/services/surfaceflinger/Scheduler/Scheduler.h
@@ -90,7 +90,8 @@
         std::atomic<nsecs_t> lastResyncTime = 0;
     };
 
-    explicit Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function);
+    explicit Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function,
+                       const scheduler::RefreshRateConfigs& refreshRateConfig);
 
     virtual ~Scheduler();
 
@@ -145,16 +146,20 @@
     void addPresentFence(const std::shared_ptr<FenceTime>& fenceTime);
     void setIgnorePresentFences(bool ignore);
     nsecs_t expectedPresentTime();
-    // apiId indicates the API (NATIVE_WINDOW_API_xxx) that queues the buffer.
-    // TODO(b/123956502): Remove this call with V1 go/content-fps-detection-in-scheduler.
-    void addNativeWindowApi(int apiId);
-    // Updates FPS based on the most occured request for Native Window API.
-    void updateFpsBasedOnNativeWindowApi();
+    // Registers the layer in the scheduler, and returns the handle for future references.
+    std::unique_ptr<scheduler::LayerHistory::LayerHandle> registerLayer(const std::string name);
+    // Stores present time for a layer.
+    void addLayerPresentTime(
+            const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle,
+            nsecs_t presentTime);
+    // Updates FPS based on the most content presented.
+    void updateFpsBasedOnContent();
     // Callback that gets invoked when Scheduler wants to change the refresh rate.
     void setChangeRefreshRateCallback(const ChangeRefreshRateCallback& changeRefreshRateCallback);
 
     // Returns whether idle timer is enabled or not
     bool isIdleTimerEnabled() { return mSetIdleTimerMs > 0; }
+
     // Returns relevant information about Scheduler for dumpsys purposes.
     std::string doDump();
 
@@ -171,7 +176,7 @@
 
     // In order to make sure that the features don't override themselves, we need a state machine
     // to keep track which feature requested the config change.
-    enum class MediaFeatureState { MEDIA_PLAYING, MEDIA_OFF };
+    enum class ContentFeatureState { CONTENT_DETECTION_ON, CONTENT_DETECTION_OFF };
     enum class IdleTimerState { EXPIRED, RESET };
 
     // Creates a connection on the given EventThread and forwards the given callbacks.
@@ -188,12 +193,17 @@
     // Sets vsync period.
     void setVsyncPeriod(const nsecs_t period);
     // Media feature's function to change the refresh rate.
-    void mediaChangeRefreshRate(MediaFeatureState mediaFeatureState);
+    void contentChangeRefreshRate(ContentFeatureState contentFeatureState, uint32_t refreshRate);
     // Idle timer feature's function to change the refresh rate.
     void timerChangeRefreshRate(IdleTimerState idleTimerState);
+    // Calculate the new refresh rate type
+    RefreshRateType calculateRefreshRateType() REQUIRES(mFeatureStateLock);
     // Acquires a lock and calls the ChangeRefreshRateCallback() with given parameters.
     void changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent);
 
+    // Helper function to calculate error frames
+    float getErrorFrames(float contentFps, float configFps);
+
     // If fences from sync Framework are supported.
     const bool mHasSyncFramework;
 
@@ -226,13 +236,8 @@
     std::array<int64_t, scheduler::ARRAY_SIZE> mTimeDifferences{};
     size_t mCounter = 0;
 
-    // The following few fields follow native window api bits that come with buffers. If there are
-    // more buffers with NATIVE_WINDOW_API_MEDIA we render at 60Hz, otherwise we render at 90Hz.
-    // There is not dependency on timestamp for V0.
-    // TODO(b/123956502): Remove this when more robust logic for content fps detection is developed.
-    std::mutex mWindowApiHistoryLock;
-    std::array<int, scheduler::ARRAY_SIZE> mWindowApiHistory GUARDED_BY(mWindowApiHistoryLock);
-    int64_t mApiHistoryCounter = 0;
+    // Historical information about individual layers. Used for predicting the refresh rate.
+    scheduler::LayerHistory mLayerHistory;
 
     // Timer that records time between requests for next vsync. If the time is higher than a given
     // interval, a callback is fired. Set this variable to >0 to use this feature.
@@ -245,9 +250,12 @@
     // In order to make sure that the features don't override themselves, we need a state machine
     // to keep track which feature requested the config change.
     std::mutex mFeatureStateLock;
-    MediaFeatureState mCurrentMediaFeatureState GUARDED_BY(mFeatureStateLock) =
-            MediaFeatureState::MEDIA_OFF;
+    ContentFeatureState mCurrentContentFeatureState GUARDED_BY(mFeatureStateLock) =
+            ContentFeatureState::CONTENT_DETECTION_OFF;
     IdleTimerState mCurrentIdleTimerState GUARDED_BY(mFeatureStateLock) = IdleTimerState::RESET;
+    uint32_t mContentRefreshRate;
+
+    const scheduler::RefreshRateConfigs& mRefreshRateConfigs;
 };
 
 } // namespace android