SF: Flag guard setFrameRate() API

- LayerHistory is now always created
- Do not use LayerHistory when content detection flag is turned off.

Bug: 148428554
Test: atest CtsGraphicsTestCases:SetFrameRateTest
Test: Play YouTube video. When content detection flag is off,
      display runs at 90, otherwise at 60.
Test: adb shell /data/.../libsurfaceflinger_unittest
Change-Id: Iab8b79e16a5257c770d9ddd204cfe26d22de83b6
diff --git a/services/surfaceflinger/Scheduler/Scheduler.cpp b/services/surfaceflinger/Scheduler/Scheduler.cpp
index 94791eb..2b70d65 100644
--- a/services/surfaceflinger/Scheduler/Scheduler.cpp
+++ b/services/surfaceflinger/Scheduler/Scheduler.cpp
@@ -108,13 +108,10 @@
         mUseContentDetectionV2(useContentDetectionV2) {
     using namespace sysprop;
 
-    if (property_get_bool("debug.sf.use_content_detection_for_refresh_rate", 0) ||
-        use_content_detection_for_refresh_rate(false)) {
-        if (mUseContentDetectionV2) {
-            mLayerHistory = std::make_unique<scheduler::impl::LayerHistoryV2>();
-        } else {
-            mLayerHistory = std::make_unique<scheduler::impl::LayerHistory>();
-        }
+    if (mUseContentDetectionV2) {
+        mLayerHistory = std::make_unique<scheduler::impl::LayerHistoryV2>();
+    } else {
+        mLayerHistory = std::make_unique<scheduler::impl::LayerHistory>();
     }
 
     const int setIdleTimerMs = property_get_int32("debug.sf.set_idle_timer_ms", 0);
@@ -438,7 +435,7 @@
         mFeatures.contentDetection =
                 !summary.empty() ? ContentDetectionState::On : ContentDetectionState::Off;
 
-        newConfigId = calculateRefreshRateType();
+        newConfigId = calculateRefreshRateConfigIndexType();
         if (mFeatures.configId == newConfigId) {
             return;
         }
@@ -531,8 +528,6 @@
     using base::StringAppendF;
     const char* const states[] = {"off", "on"};
 
-    StringAppendF(&result, "+  Content detection: %s\n", states[mLayerHistory != nullptr]);
-
     StringAppendF(&result, "+  Idle timer: %s\n",
                   mIdleTimer ? mIdleTimer->dump().c_str() : states[0]);
     StringAppendF(&result, "+  Touch timer: %s\n\n",
@@ -549,7 +544,7 @@
             return;
         }
         *currentState = newState;
-        newConfigId = calculateRefreshRateType();
+        newConfigId = calculateRefreshRateConfigIndexType();
         if (mFeatures.configId == newConfigId) {
             return;
         }
@@ -563,6 +558,7 @@
 }
 
 bool Scheduler::layerHistoryHasClientSpecifiedFrameRate() {
+    // Traverse all the layers to see if any of them requested frame rate.
     for (const auto& layer : mFeatures.contentRequirements) {
         if (layer.vote == scheduler::RefreshRateConfigs::LayerVoteType::ExplicitDefault ||
             layer.vote == scheduler::RefreshRateConfigs::LayerVoteType::ExplicitExactOrMultiple) {
@@ -573,10 +569,9 @@
     return false;
 }
 
-HwcConfigIndexType Scheduler::calculateRefreshRateType() {
+HwcConfigIndexType Scheduler::calculateRefreshRateConfigIndexType() {
     // This block of the code checks whether any layers used the SetFrameRate API. If they have,
-    // their request should be honored regardless of whether the device has refresh rate switching
-    // turned off.
+    // their request should be honored depending on other active layers.
     if (layerHistoryHasClientSpecifiedFrameRate()) {
         if (!mUseContentDetectionV2) {
             return mRefreshRateConfigs.getRefreshRateForContent(mFeatures.contentRequirements)
@@ -587,23 +582,23 @@
         }
     }
 
-    // If the layer history doesn't have the frame rate specified, use the old path. NOTE:
-    // if we remove the kernel idle timer, and use our internal idle timer, this code will have to
-    // be refactored.
-    // If Display Power is not in normal operation we want to be in performance mode.
-    // When coming back to normal mode, a grace period is given with DisplayPowerTimer
+    // If the layer history doesn't have the frame rate specified, check for other features and
+    // honor them. NOTE: If we remove the kernel idle timer, and use our internal idle timer, this
+    // code will have to be refactored. If Display Power is not in normal operation we want to be in
+    // performance mode. When coming back to normal mode, a grace period is given with
+    // DisplayPowerTimer.
     if (mDisplayPowerTimer &&
         (!mFeatures.isDisplayPowerStateNormal ||
          mFeatures.displayPowerTimer == TimerState::Reset)) {
         return mRefreshRateConfigs.getMaxRefreshRateByPolicy().configId;
     }
 
-    // As long as touch is active we want to be in performance mode
+    // As long as touch is active we want to be in performance mode.
     if (mTouchTimer && mFeatures.touch == TouchState::Active) {
         return mRefreshRateConfigs.getMaxRefreshRateByPolicy().configId;
     }
 
-    // If timer has expired as it means there is no new content on the screen
+    // If timer has expired as it means there is no new content on the screen.
     if (mIdleTimer && mFeatures.idleTimer == TimerState::Expired) {
         return mRefreshRateConfigs.getMinRefreshRateByPolicy().configId;
     }
@@ -611,7 +606,7 @@
     if (!mUseContentDetectionV2) {
         // If content detection is off we choose performance as we don't know the content fps.
         if (mFeatures.contentDetection == ContentDetectionState::Off) {
-            // TODO(b/148428554): Be careful to not always call this.
+            // NOTE: V1 always calls this, but this is not a default behavior for V2.
             return mRefreshRateConfigs.getMaxRefreshRateByPolicy().configId;
         }
 
@@ -633,7 +628,7 @@
     std::lock_guard<std::mutex> lock(mFeatureStateLock);
     // Make sure that the default config ID is first updated, before returned.
     if (mFeatures.configId.has_value()) {
-        mFeatures.configId = calculateRefreshRateType();
+        mFeatures.configId = calculateRefreshRateConfigIndexType();
     }
     return mFeatures.configId;
 }