SF: tune infrequent layers for animations
Classify a layer as frequent or not based the last buffer pattern.
When unknown (e.g. the first buffer after inactivity), return
the last known state of the layer. This change helps to correctly classify
animating layers that were inactive and now posts the first buffer.
Bug: 264952266
Test: atest libsurfaceflinger_unittest
Change-Id: Ie6a1a21c00f7bb58468da033e98d5e35eb0c4c8d
diff --git a/services/surfaceflinger/Scheduler/LayerInfo.cpp b/services/surfaceflinger/Scheduler/LayerInfo.cpp
index 7247e4b..9e5b4bd 100644
--- a/services/surfaceflinger/Scheduler/LayerInfo.cpp
+++ b/services/surfaceflinger/Scheduler/LayerInfo.cpp
@@ -76,12 +76,43 @@
bool LayerInfo::isFrequent(nsecs_t now) const {
using fps_approx_ops::operator>=;
- // If we know nothing about this layer we consider it as frequent as it might be the start
- // of an animation.
+ // If we know nothing about this layer (e.g. after touch event),
+ // we consider it as frequent as it might be the start of an animation.
if (mFrameTimes.size() < kFrequentLayerWindowSize) {
return true;
}
- return getFps(now) >= kMinFpsForFrequentLayer;
+
+ // Non-active layers are also infrequent
+ if (mLastUpdatedTime < getActiveLayerThreshold(now)) {
+ return false;
+ }
+
+ // We check whether we can classify this layer as frequent or infrequent:
+ // - frequent: a layer posted kFrequentLayerWindowSize within
+ // kMaxPeriodForFrequentLayerNs of each other.
+ // - infrequent: a layer posted kFrequentLayerWindowSize with longer
+ // gaps than kFrequentLayerWindowSize.
+ // If we can't determine the layer classification yet, we return the last
+ // classification.
+ bool isFrequent = true;
+ bool isInfrequent = true;
+ const auto n = mFrameTimes.size() - 1;
+ for (size_t i = 0; i < kFrequentLayerWindowSize - 1; i++) {
+ if (mFrameTimes[n - i].queueTime - mFrameTimes[n - i - 1].queueTime <
+ kMaxPeriodForFrequentLayerNs.count()) {
+ isInfrequent = false;
+ } else {
+ isFrequent = false;
+ }
+ }
+
+ if (isFrequent || isInfrequent) {
+ return isFrequent;
+ }
+
+ // If we can't determine whether the layer is frequent or not, we return
+ // the last known classification.
+ return !mLastRefreshRate.infrequent;
}
Fps LayerInfo::getFps(nsecs_t now) const {
@@ -231,13 +262,13 @@
if (isAnimating(now)) {
ALOGV("%s is animating", mName.c_str());
- mLastRefreshRate.animatingOrInfrequent = true;
+ mLastRefreshRate.animating = true;
return {LayerHistory::LayerVoteType::Max, Fps()};
}
if (!isFrequent(now)) {
ALOGV("%s is infrequent", mName.c_str());
- mLastRefreshRate.animatingOrInfrequent = true;
+ mLastRefreshRate.infrequent = true;
// Infrequent layers vote for mininal refresh rate for
// battery saving purposes and also to prevent b/135718869.
return {LayerHistory::LayerVoteType::Min, Fps()};
@@ -246,7 +277,7 @@
// If the layer was previously tagged as animating or infrequent, we clear
// the history as it is likely the layer just changed its behavior
// and we should not look at stale data
- if (mLastRefreshRate.animatingOrInfrequent) {
+ if (mLastRefreshRate.animating || mLastRefreshRate.infrequent) {
clearHistory(now);
}
diff --git a/services/surfaceflinger/Scheduler/LayerInfo.h b/services/surfaceflinger/Scheduler/LayerInfo.h
index a5ffbbe..93485be 100644
--- a/services/surfaceflinger/Scheduler/LayerInfo.h
+++ b/services/surfaceflinger/Scheduler/LayerInfo.h
@@ -53,7 +53,7 @@
// Layer is considered frequent if the earliest value in the window of most recent present times
// is within a threshold. If a layer is infrequent, its average refresh rate is disregarded in
// favor of a low refresh rate.
- static constexpr size_t kFrequentLayerWindowSize = 3;
+ static constexpr size_t kFrequentLayerWindowSize = 4;
static constexpr Fps kMinFpsForFrequentLayer = 10_Hz;
static constexpr auto kMaxPeriodForFrequentLayerNs =
std::chrono::nanoseconds(kMinFpsForFrequentLayer.getPeriodNsecs()) + 1ms;
@@ -214,7 +214,10 @@
Fps reported;
// Whether the last reported rate for LayerInfo::getRefreshRate()
// was due to animation or infrequent updates
- bool animatingOrInfrequent = false;
+ bool animating = false;
+ // Whether the last reported rate for LayerInfo::getRefreshRate()
+ // was due to infrequent updates
+ bool infrequent = false;
};
// Class to store past calculated refresh rate and determine whether
diff --git a/services/surfaceflinger/tests/unittests/LayerHistoryTest.cpp b/services/surfaceflinger/tests/unittests/LayerHistoryTest.cpp
index 979924a..8397f8d 100644
--- a/services/surfaceflinger/tests/unittests/LayerHistoryTest.cpp
+++ b/services/surfaceflinger/tests/unittests/LayerHistoryTest.cpp
@@ -51,8 +51,6 @@
static constexpr auto MAX_FREQUENT_LAYER_PERIOD_NS = LayerInfo::kMaxPeriodForFrequentLayerNs;
static constexpr auto FREQUENT_LAYER_WINDOW_SIZE = LayerInfo::kFrequentLayerWindowSize;
static constexpr auto PRESENT_TIME_HISTORY_DURATION = LayerInfo::HISTORY_DURATION;
- static constexpr auto REFRESH_RATE_AVERAGE_HISTORY_DURATION =
- LayerInfo::RefreshRateHistory::HISTORY_DURATION;
static constexpr Fps LO_FPS = 30_Hz;
static constexpr auto LO_FPS_PERIOD = LO_FPS.getPeriodNsecs();
@@ -607,7 +605,7 @@
// advance the time for the previous frame to be inactive
time += MAX_ACTIVE_LAYER_PERIOD_NS.count();
- // Now event if we post a quick few frame we should stay infrequent
+ // Now even if we post a quick few frame we should stay infrequent
for (int i = 0; i < FREQUENT_LAYER_WINDOW_SIZE - 1; i++) {
history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
time += HI_FPS_PERIOD;
@@ -706,6 +704,88 @@
EXPECT_EQ(1, animatingLayerCount(time));
}
+TEST_F(LayerHistoryTest, frequentLayerBecomingInfrequentAndBack) {
+ auto layer = createLayer();
+
+ EXPECT_CALL(*layer, isVisible()).WillRepeatedly(Return(true));
+ EXPECT_CALL(*layer, getFrameRateForLayerTree()).WillRepeatedly(Return(Layer::FrameRate()));
+
+ nsecs_t time = systemTime();
+
+ EXPECT_EQ(1, layerCount());
+ EXPECT_EQ(0, activeLayerCount());
+ EXPECT_EQ(0, frequentLayerCount(time));
+ EXPECT_EQ(0, animatingLayerCount(time));
+
+ // Fill up the window with frequent updates
+ for (int i = 0; i < FREQUENT_LAYER_WINDOW_SIZE; i++) {
+ history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
+ time += (60_Hz).getPeriodNsecs();
+
+ EXPECT_EQ(1, layerCount());
+ ASSERT_EQ(1, summarizeLayerHistory(time).size());
+ EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
+ EXPECT_EQ(1, activeLayerCount());
+ EXPECT_EQ(1, frequentLayerCount(time));
+ }
+
+ // posting a buffer after long inactivity should retain the layer as active
+ time += std::chrono::nanoseconds(3s).count();
+ history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
+ ASSERT_EQ(1, summarizeLayerHistory(time).size());
+ EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summarizeLayerHistory(time)[0].vote);
+ EXPECT_EQ(60_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
+ EXPECT_EQ(1, activeLayerCount());
+ EXPECT_EQ(1, frequentLayerCount(time));
+ EXPECT_EQ(0, animatingLayerCount(time));
+
+ // posting more infrequent buffer should make the layer infrequent
+ time += (MAX_FREQUENT_LAYER_PERIOD_NS + 1ms).count();
+ history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
+ time += (MAX_FREQUENT_LAYER_PERIOD_NS + 1ms).count();
+ history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
+ ASSERT_EQ(1, summarizeLayerHistory(time).size());
+ EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
+ EXPECT_EQ(1, activeLayerCount());
+ EXPECT_EQ(0, frequentLayerCount(time));
+ EXPECT_EQ(0, animatingLayerCount(time));
+
+ // posting another buffer should keep the layer infrequent
+ history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
+ ASSERT_EQ(1, summarizeLayerHistory(time).size());
+ EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
+ EXPECT_EQ(1, activeLayerCount());
+ EXPECT_EQ(0, frequentLayerCount(time));
+ EXPECT_EQ(0, animatingLayerCount(time));
+
+ // posting more buffers would mean starting of an animation, so making the layer frequent
+ history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
+ history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
+ ASSERT_EQ(1, summarizeLayerHistory(time).size());
+ EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
+ EXPECT_EQ(1, activeLayerCount());
+ EXPECT_EQ(1, frequentLayerCount(time));
+ EXPECT_EQ(0, animatingLayerCount(time));
+
+ // posting a buffer after long inactivity should retain the layer as active
+ time += std::chrono::nanoseconds(3s).count();
+ history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
+ ASSERT_EQ(1, summarizeLayerHistory(time).size());
+ EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
+ EXPECT_EQ(1, activeLayerCount());
+ EXPECT_EQ(1, frequentLayerCount(time));
+ EXPECT_EQ(0, animatingLayerCount(time));
+
+ // posting another buffer should keep the layer frequent
+ time += (60_Hz).getPeriodNsecs();
+ history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
+ ASSERT_EQ(1, summarizeLayerHistory(time).size());
+ EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
+ EXPECT_EQ(1, activeLayerCount());
+ EXPECT_EQ(1, frequentLayerCount(time));
+ EXPECT_EQ(0, animatingLayerCount(time));
+}
+
TEST_F(LayerHistoryTest, getFramerate) {
auto layer = createLayer();