SF: Optimize layer history in scheduler
Register layers with LayerHistory by storing a weak pointer rather than
allocating a LayerHandle. Query layer visibility when needed instead of
synchronizing it to LayerInfo whenever Layer::isVisible is called. Store
active/inactive layers in contiguous memory instead of two hash maps for
cache efficiency, and minimal allocation and run time of operations like
clearing history. Remove redundant ref-counting, locking, and frequency-
period conversion in LayerInfo. Avoid sleeping in unit tests.
This is also prework for per-display layer history.
Bug: 130554049
Bug: 134772048
Test: go/wm-smoke with debug.sf.layer_history_trace
Test: C2/F2 test cases from spreadsheet
Test: LayerHistoryTest with new test cases
Change-Id: Ibfcfe46cd76ebd93b916d4a0c737a19e837d4ff1
diff --git a/services/surfaceflinger/Scheduler/LayerInfo.cpp b/services/surfaceflinger/Scheduler/LayerInfo.cpp
index 723d71f..f3b0d56 100644
--- a/services/surfaceflinger/Scheduler/LayerInfo.cpp
+++ b/services/surfaceflinger/Scheduler/LayerInfo.cpp
@@ -16,27 +16,17 @@
#include "LayerInfo.h"
-#include <cinttypes>
-#include <cstdint>
-#include <numeric>
-#include <string>
+#include <algorithm>
+#include <utility>
-namespace android {
-namespace scheduler {
+namespace android::scheduler {
-LayerInfo::LayerInfo(const std::string name, float minRefreshRate, float maxRefreshRate)
- : mName(name),
- mMinRefreshDuration(1e9f / maxRefreshRate),
- mLowActivityRefreshDuration(1e9f / minRefreshRate),
- mRefreshRateHistory(mMinRefreshDuration) {}
+LayerInfo::LayerInfo(float lowRefreshRate, float highRefreshRate)
+ : mLowRefreshRate(lowRefreshRate), mHighRefreshRate(highRefreshRate) {}
-LayerInfo::~LayerInfo() = default;
-
-void LayerInfo::setLastPresentTime(nsecs_t lastPresentTime) {
- std::lock_guard lock(mLock);
-
+void LayerInfo::setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now) {
// Buffers can come with a present time far in the future. That keeps them relevant.
- mLastUpdatedTime = std::max(lastPresentTime, systemTime());
+ mLastUpdatedTime = std::max(lastPresentTime, now);
mPresentTimeHistory.insertPresentTime(mLastUpdatedTime);
if (mLastPresentTime == 0) {
@@ -45,14 +35,13 @@
return;
}
- const nsecs_t timeDiff = lastPresentTime - mLastPresentTime;
+ const nsecs_t period = lastPresentTime - mLastPresentTime;
mLastPresentTime = lastPresentTime;
// Ignore time diff that are too high - those are stale values
- if (timeDiff > OBSOLETE_TIME_EPSILON_NS.count()) return;
- const nsecs_t refreshDuration = std::max(timeDiff, mMinRefreshDuration);
- const int fps = 1e9f / refreshDuration;
+ if (period > MAX_ACTIVE_LAYER_PERIOD_NS.count()) return;
+
+ const float fps = std::min(1e9f / period, mHighRefreshRate);
mRefreshRateHistory.insertRefreshRate(fps);
}
-} // namespace scheduler
-} // namespace android
+} // namespace android::scheduler