Add Fps class
Add a class to wrap a fps value. This is useful because across
the code we
- convert between vsyncPeriod and fps
- compare with tolerance (this ensures we use consistent tolerance)
- consistent toString method
Bug: 159590486
Test: presubmit
Change-Id: Iebb77a33a2f822056642aa61bd6fac6514aa656d
diff --git a/services/surfaceflinger/Scheduler/LayerHistory.cpp b/services/surfaceflinger/Scheduler/LayerHistory.cpp
index c0d00f3..499daad 100644
--- a/services/surfaceflinger/Scheduler/LayerHistory.cpp
+++ b/services/surfaceflinger/Scheduler/LayerHistory.cpp
@@ -41,7 +41,7 @@
bool isLayerActive(const Layer& layer, const LayerInfo& info, nsecs_t threshold) {
// Layers with an explicit vote are always kept active
- if (layer.getFrameRateForLayerTree().rate > 0) {
+ if (layer.getFrameRateForLayerTree().rate.isValid()) {
return true;
}
@@ -86,9 +86,8 @@
LayerHistory::~LayerHistory() = default;
-void LayerHistory::registerLayer(Layer* layer, float /*lowRefreshRate*/, float highRefreshRate,
- LayerVoteType type) {
- const nsecs_t highRefreshRatePeriod = static_cast<nsecs_t>(1e9f / highRefreshRate);
+void LayerHistory::registerLayer(Layer* layer, Fps highRefreshRate, LayerVoteType type) {
+ const nsecs_t highRefreshRatePeriod = highRefreshRate.getPeriodNsecs();
auto info = std::make_unique<LayerInfo>(layer->getName(), highRefreshRatePeriod, type);
std::lock_guard lock(mLock);
mLayerInfos.emplace_back(layer, std::move(info));
@@ -148,7 +147,7 @@
{strong->getName(), vote.type, vote.fps, vote.seamlessness, weight, layerFocused});
if (CC_UNLIKELY(mTraceEnabled)) {
- trace(layer, *info, vote.type, static_cast<int>(std::round(vote.fps)));
+ trace(layer, *info, vote.type, vote.fps.getIntValue());
}
}
@@ -177,7 +176,7 @@
}
}();
- if (frameRate.rate > 0 || voteType == LayerVoteType::NoVote) {
+ if (frameRate.rate.isValid() || voteType == LayerVoteType::NoVote) {
const auto type = layer->isVisible() ? voteType : LayerVoteType::NoVote;
info->setLayerVote({type, frameRate.rate, frameRate.seamlessness});
} else {
diff --git a/services/surfaceflinger/Scheduler/LayerHistory.h b/services/surfaceflinger/Scheduler/LayerHistory.h
index 507ccc6..4214bab 100644
--- a/services/surfaceflinger/Scheduler/LayerHistory.h
+++ b/services/surfaceflinger/Scheduler/LayerHistory.h
@@ -46,7 +46,7 @@
~LayerHistory();
// Layers are unregistered when the weak reference expires.
- void registerLayer(Layer*, float lowRefreshRate, float highRefreshRate, LayerVoteType type);
+ void registerLayer(Layer*, Fps highRefreshRate, LayerVoteType type);
// Sets the display size. Client is responsible for synchronization.
void setDisplayArea(uint32_t displayArea) { mDisplayArea = displayArea; }
diff --git a/services/surfaceflinger/Scheduler/LayerInfo.cpp b/services/surfaceflinger/Scheduler/LayerInfo.cpp
index 66ac98a..1c0065c 100644
--- a/services/surfaceflinger/Scheduler/LayerInfo.cpp
+++ b/services/surfaceflinger/Scheduler/LayerInfo.cpp
@@ -38,7 +38,7 @@
: mName(name),
mHighRefreshRatePeriod(highRefreshRatePeriod),
mDefaultVote(defaultVote),
- mLayerVote({defaultVote, 0.0f}),
+ mLayerVote({defaultVote, Fps(0.0f)}),
mRefreshRateHistory(name) {}
void LayerInfo::setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, LayerUpdateType updateType,
@@ -91,7 +91,8 @@
// Layer is considered frequent if the average frame rate is higher than the threshold
const auto totalTime = mFrameTimes.back().queueTime - it->queueTime;
- return (1e9f * (numFrames - 1)) / totalTime >= MIN_FPS_FOR_FREQUENT_LAYER;
+ return Fps::fromPeriodNsecs(totalTime / (numFrames - 1))
+ .greaterThanOrEqualWithMargin(MIN_FPS_FOR_FREQUENT_LAYER);
}
bool LayerInfo::isAnimating(nsecs_t now) const {
@@ -139,7 +140,7 @@
missingPresentTime = true;
// If there are no presentation timestamps and we haven't calculated
// one in the past then we can't calculate the refresh rate
- if (mLastRefreshRate.reported == 0) {
+ if (!mLastRefreshRate.reported.isValid()) {
return std::nullopt;
}
continue;
@@ -163,7 +164,7 @@
return static_cast<nsecs_t>(averageFrameTime);
}
-std::optional<float> LayerInfo::calculateRefreshRateIfPossible(nsecs_t now) {
+std::optional<Fps> LayerInfo::calculateRefreshRateIfPossible(nsecs_t now) {
static constexpr float MARGIN = 1.0f; // 1Hz
if (!hasEnoughDataForHeuristic()) {
ALOGV("Not enough data");
@@ -172,7 +173,7 @@
const auto averageFrameTime = calculateAverageFrameTime();
if (averageFrameTime.has_value()) {
- const auto refreshRate = 1e9f / *averageFrameTime;
+ const auto refreshRate = Fps::fromPeriodNsecs(*averageFrameTime);
const bool refreshRateConsistent = mRefreshRateHistory.add(refreshRate, now);
if (refreshRateConsistent) {
const auto knownRefreshRate =
@@ -180,22 +181,23 @@
// To avoid oscillation, use the last calculated refresh rate if it is
// close enough
- if (std::abs(mLastRefreshRate.calculated - refreshRate) > MARGIN &&
- mLastRefreshRate.reported != knownRefreshRate) {
+ if (std::abs(mLastRefreshRate.calculated.getValue() - refreshRate.getValue()) >
+ MARGIN &&
+ !mLastRefreshRate.reported.equalsWithMargin(knownRefreshRate)) {
mLastRefreshRate.calculated = refreshRate;
mLastRefreshRate.reported = knownRefreshRate;
}
- ALOGV("%s %.2fHz rounded to nearest known frame rate %.2fHz", mName.c_str(),
- refreshRate, mLastRefreshRate.reported);
+ ALOGV("%s %s rounded to nearest known frame rate %s", mName.c_str(),
+ to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str());
} else {
- ALOGV("%s Not stable (%.2fHz) returning last known frame rate %.2fHz", mName.c_str(),
- refreshRate, mLastRefreshRate.reported);
+ ALOGV("%s Not stable (%s) returning last known frame rate %s", mName.c_str(),
+ to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str());
}
}
- return mLastRefreshRate.reported == 0 ? std::nullopt
- : std::make_optional(mLastRefreshRate.reported);
+ return mLastRefreshRate.reported.isValid() ? std::make_optional(mLastRefreshRate.reported)
+ : std::nullopt;
}
LayerInfo::LayerVote LayerInfo::getRefreshRateVote(nsecs_t now) {
@@ -207,13 +209,13 @@
if (isAnimating(now)) {
ALOGV("%s is animating", mName.c_str());
mLastRefreshRate.animatingOrInfrequent = true;
- return {LayerHistory::LayerVoteType::Max, 0};
+ return {LayerHistory::LayerVoteType::Max, Fps(0.0f)};
}
if (!isFrequent(now)) {
ALOGV("%s is infrequent", mName.c_str());
mLastRefreshRate.animatingOrInfrequent = true;
- return {LayerHistory::LayerVoteType::Min, 0};
+ return {LayerHistory::LayerVoteType::Min, Fps(0.0f)};
}
// If the layer was previously tagged as animating or infrequent, we clear
@@ -225,12 +227,12 @@
auto refreshRate = calculateRefreshRateIfPossible(now);
if (refreshRate.has_value()) {
- ALOGV("%s calculated refresh rate: %.2f", mName.c_str(), refreshRate.value());
+ ALOGV("%s calculated refresh rate: %s", mName.c_str(), to_string(*refreshRate).c_str());
return {LayerHistory::LayerVoteType::Heuristic, refreshRate.value()};
}
ALOGV("%s Max (can't resolve refresh rate)", mName.c_str());
- return {LayerHistory::LayerVoteType::Max, 0};
+ return {LayerHistory::LayerVoteType::Max, Fps(0.0f)};
}
const char* LayerInfo::getTraceTag(android::scheduler::LayerHistory::LayerVoteType type) const {
@@ -256,7 +258,7 @@
mRefreshRates.clear();
}
-bool LayerInfo::RefreshRateHistory::add(float refreshRate, nsecs_t now) {
+bool LayerInfo::RefreshRateHistory::add(Fps refreshRate, nsecs_t now) {
mRefreshRates.push_back({refreshRate, now});
while (mRefreshRates.size() >= HISTORY_SIZE ||
now - mRefreshRates.front().timestamp > HISTORY_DURATION.count()) {
@@ -268,7 +270,7 @@
mHeuristicTraceTagData = makeHeuristicTraceTagData();
}
- ATRACE_INT(mHeuristicTraceTagData->average.c_str(), static_cast<int>(refreshRate));
+ ATRACE_INT(mHeuristicTraceTagData->average.c_str(), refreshRate.getIntValue());
}
return isConsistent();
@@ -279,15 +281,16 @@
const auto max = std::max_element(mRefreshRates.begin(), mRefreshRates.end());
const auto min = std::min_element(mRefreshRates.begin(), mRefreshRates.end());
- const auto consistent = max->refreshRate - min->refreshRate <= MARGIN_FPS;
+ const auto consistent =
+ max->refreshRate.getValue() - min->refreshRate.getValue() < MARGIN_CONSISTENT_FPS;
if (CC_UNLIKELY(sTraceEnabled)) {
if (!mHeuristicTraceTagData.has_value()) {
mHeuristicTraceTagData = makeHeuristicTraceTagData();
}
- ATRACE_INT(mHeuristicTraceTagData->max.c_str(), static_cast<int>(max->refreshRate));
- ATRACE_INT(mHeuristicTraceTagData->min.c_str(), static_cast<int>(min->refreshRate));
+ ATRACE_INT(mHeuristicTraceTagData->max.c_str(), max->refreshRate.getIntValue());
+ ATRACE_INT(mHeuristicTraceTagData->min.c_str(), min->refreshRate.getIntValue());
ATRACE_INT(mHeuristicTraceTagData->consistent.c_str(), consistent);
}
diff --git a/services/surfaceflinger/Scheduler/LayerInfo.h b/services/surfaceflinger/Scheduler/LayerInfo.h
index e434670..9304e62 100644
--- a/services/surfaceflinger/Scheduler/LayerInfo.h
+++ b/services/surfaceflinger/Scheduler/LayerInfo.h
@@ -50,9 +50,9 @@
// 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 FREQUENT_LAYER_WINDOW_SIZE = 3;
- static constexpr float MIN_FPS_FOR_FREQUENT_LAYER = 10.0f;
+ static constexpr Fps MIN_FPS_FOR_FREQUENT_LAYER{10.0f};
static constexpr auto MAX_FREQUENT_LAYER_PERIOD_NS =
- std::chrono::nanoseconds(static_cast<nsecs_t>(1e9f / MIN_FPS_FOR_FREQUENT_LAYER)) + 1ms;
+ std::chrono::nanoseconds(MIN_FPS_FOR_FREQUENT_LAYER.getPeriodNsecs()) + 1ms;
friend class LayerHistoryTest;
@@ -60,7 +60,7 @@
// Holds information about the layer vote
struct LayerVote {
LayerHistory::LayerVoteType type = LayerHistory::LayerVoteType::Heuristic;
- float fps = 0.0f;
+ Fps fps{0.0f};
Seamlessness seamlessness = Seamlessness::Default;
};
@@ -92,7 +92,7 @@
void setDefaultLayerVote(LayerHistory::LayerVoteType type) { mDefaultVote = type; }
// Resets the layer vote to its default.
- void resetLayerVote() { mLayerVote = {mDefaultVote, 0.0f, Seamlessness::Default}; }
+ void resetLayerVote() { mLayerVote = {mDefaultVote, Fps(0.0f), Seamlessness::Default}; }
LayerVote getRefreshRateVote(nsecs_t now);
@@ -130,9 +130,9 @@
// Holds information about the calculated and reported refresh rate
struct RefreshRateHeuristicData {
// Rate calculated on the layer
- float calculated = 0.0f;
+ Fps calculated{0.0f};
// Last reported rate for LayerInfo::getRefreshRate()
- float reported = 0.0f;
+ Fps reported{0.0f};
// Whether the last reported rate for LayerInfo::getRefreshRate()
// was due to animation or infrequent updates
bool animatingOrInfrequent = false;
@@ -151,18 +151,20 @@
void clear();
// Adds a new refresh rate and returns true if it is consistent
- bool add(float refreshRate, nsecs_t now);
+ bool add(Fps refreshRate, nsecs_t now);
private:
friend class LayerHistoryTest;
// Holds the refresh rate when it was calculated
struct RefreshRateData {
- float refreshRate = 0.0f;
+ Fps refreshRate{0.0f};
nsecs_t timestamp = 0;
bool operator<(const RefreshRateData& other) const {
- return refreshRate < other.refreshRate;
+ // We don't need comparison with margins since we are using
+ // this to find the min and max refresh rates.
+ return refreshRate.getValue() < other.refreshRate.getValue();
}
};
@@ -180,13 +182,13 @@
const std::string mName;
mutable std::optional<HeuristicTraceTagData> mHeuristicTraceTagData;
std::deque<RefreshRateData> mRefreshRates;
- static constexpr float MARGIN_FPS = 1.0;
+ static constexpr float MARGIN_CONSISTENT_FPS = 1.0;
};
bool isFrequent(nsecs_t now) const;
bool isAnimating(nsecs_t now) const;
bool hasEnoughDataForHeuristic() const;
- std::optional<float> calculateRefreshRateIfPossible(nsecs_t now);
+ std::optional<Fps> calculateRefreshRateIfPossible(nsecs_t now);
std::optional<nsecs_t> calculateAverageFrameTime() const;
bool isFrameTimeValid(const FrameTimeData&) const;
diff --git a/services/surfaceflinger/Scheduler/RefreshRateConfigs.cpp b/services/surfaceflinger/Scheduler/RefreshRateConfigs.cpp
index 83fa20e..4b7251b 100644
--- a/services/surfaceflinger/Scheduler/RefreshRateConfigs.cpp
+++ b/services/surfaceflinger/Scheduler/RefreshRateConfigs.cpp
@@ -29,10 +29,10 @@
namespace android::scheduler {
namespace {
std::string formatLayerInfo(const RefreshRateConfigs::LayerRequirement& layer, float weight) {
- return base::StringPrintf("%s (type=%s, weight=%.2f seamlessness=%s) %.2fHz",
- layer.name.c_str(),
+ return base::StringPrintf("%s (type=%s, weight=%.2f seamlessness=%s) %s", layer.name.c_str(),
RefreshRateConfigs::layerVoteTypeString(layer.vote).c_str(), weight,
- toString(layer.seamlessness).c_str(), layer.desiredRefreshRate);
+ toString(layer.seamlessness).c_str(),
+ to_string(layer.desiredRefreshRate).c_str());
}
} // namespace
@@ -41,7 +41,7 @@
std::string RefreshRate::toString() const {
return base::StringPrintf("{id=%d, hwcId=%d, fps=%.2f, width=%d, height=%d group=%d}",
- getConfigId().value(), hwcConfig->getId(), getFps(),
+ getConfigId().value(), hwcConfig->getId(), getFps().getValue(),
hwcConfig->getWidth(), hwcConfig->getHeight(), getConfigGroup());
}
@@ -64,9 +64,9 @@
std::string RefreshRateConfigs::Policy::toString() const {
return base::StringPrintf("default config ID: %d, allowGroupSwitching = %d"
- ", primary range: [%.2f %.2f], app request range: [%.2f %.2f]",
- defaultConfig.value(), allowGroupSwitching, primaryRange.min,
- primaryRange.max, appRequestRange.min, appRequestRange.max);
+ ", primary range: %s, app request range: %s",
+ defaultConfig.value(), allowGroupSwitching,
+ primaryRange.toString().c_str(), appRequestRange.toString().c_str());
}
std::pair<nsecs_t, nsecs_t> RefreshRateConfigs::getDisplayFrames(nsecs_t layerPeriod,
@@ -144,7 +144,8 @@
// move out the of range if layers explicitly request a different refresh
// rate.
const Policy* policy = getCurrentPolicyLocked();
- const bool primaryRangeIsSingleRate = policy->primaryRange.min == policy->primaryRange.max;
+ const bool primaryRangeIsSingleRate =
+ policy->primaryRange.min.equalsWithMargin(policy->primaryRange.max);
if (!globalSignals.touch && globalSignals.idle &&
!(primaryRangeIsSingleRate && hasExplicitVoteLayers)) {
@@ -229,17 +230,18 @@
// If the layer wants Max, give higher score to the higher refresh rate
if (layer.vote == LayerVoteType::Max) {
- const auto ratio = scores[i].first->fps / scores.back().first->fps;
+ const auto ratio =
+ scores[i].first->fps.getValue() / scores.back().first->fps.getValue();
// use ratio^2 to get a lower score the more we get further from peak
const auto layerScore = ratio * ratio;
ALOGV("%s gives %s score of %.2f", formatLayerInfo(layer, weight).c_str(),
- scores[i].first->name.c_str(), layerScore);
+ scores[i].first->getName().c_str(), layerScore);
scores[i].second += weight * layerScore;
continue;
}
const auto displayPeriod = scores[i].first->hwcConfig->getVsyncPeriod();
- const auto layerPeriod = round<nsecs_t>(1e9f / layer.desiredRefreshRate);
+ const auto layerPeriod = layer.desiredRefreshRate.getPeriodNsecs();
if (layer.vote == LayerVoteType::ExplicitDefault) {
const auto layerScore = [&]() {
// Find the actual rate the layer will render, assuming
@@ -256,7 +258,7 @@
}();
ALOGV("%s gives %s score of %.2f", formatLayerInfo(layer, weight).c_str(),
- scores[i].first->name.c_str(), layerScore);
+ scores[i].first->getName().c_str(), layerScore);
scores[i].second += weight * layerScore;
continue;
}
@@ -297,7 +299,7 @@
constexpr float kSeamedSwitchPenalty = 0.95f;
const float seamlessness = isSeamlessSwitch ? 1.0f : kSeamedSwitchPenalty;
ALOGV("%s gives %s score of %.2f", formatLayerInfo(layer, weight).c_str(),
- scores[i].first->name.c_str(), layerScore);
+ scores[i].first->getName().c_str(), layerScore);
scores[i].second += weight * layerScore * seamlessness;
continue;
}
@@ -331,7 +333,7 @@
const RefreshRate& touchRefreshRate = getMaxRefreshRateByPolicyLocked();
if (globalSignals.touch && explicitDefaultVoteLayers == 0 &&
- bestRefreshRate->fps < touchRefreshRate.fps) {
+ bestRefreshRate->fps.lessThanWithMargin(touchRefreshRate.fps)) {
setTouchConsidered();
ALOGV("TouchBoost - choose %s", touchRefreshRate.getName().c_str());
return touchRefreshRate;
@@ -347,9 +349,9 @@
float max = begin->second;
for (auto i = begin; i != end; ++i) {
const auto [refreshRate, score] = *i;
- ALOGV("%s scores %.2f", refreshRate->name.c_str(), score);
+ ALOGV("%s scores %.2f", refreshRate->getName().c_str(), score);
- ATRACE_INT(refreshRate->name.c_str(), round<int>(score * 100));
+ ATRACE_INT(refreshRate->getName().c_str(), round<int>(score * 100));
if (score > max * (1 + EPSILON)) {
max = score;
@@ -433,10 +435,10 @@
for (auto configId = HwcConfigIndexType(0); configId.value() < configs.size(); configId++) {
const auto& config = configs.at(static_cast<size_t>(configId.value()));
- const float fps = 1e9f / config->getVsyncPeriod();
mRefreshRates.emplace(configId,
std::make_unique<RefreshRate>(configId, config,
- base::StringPrintf("%.2ffps", fps), fps,
+ Fps::fromPeriodNsecs(
+ config->getVsyncPeriod()),
RefreshRate::ConstructorTag(0)));
if (configId == currentConfigId) {
mCurrentRefreshRate = mRefreshRates.at(configId).get();
@@ -463,8 +465,8 @@
ALOGE("Default config is not in the primary range.");
return false;
}
- return policy.appRequestRange.min <= policy.primaryRange.min &&
- policy.appRequestRange.max >= policy.primaryRange.max;
+ return policy.appRequestRange.min.lessThanOrEqualWithMargin(policy.primaryRange.min) &&
+ policy.appRequestRange.max.greaterThanOrEqualWithMargin(policy.primaryRange.max);
}
status_t RefreshRateConfigs::setDisplayManagerPolicy(const Policy& policy) {
@@ -550,12 +552,9 @@
// Filter configs based on current policy and sort based on vsync period
const Policy* policy = getCurrentPolicyLocked();
const auto& defaultConfig = mRefreshRates.at(policy->defaultConfig)->hwcConfig;
- ALOGV("constructAvailableRefreshRates: default %d group %d primaryRange=[%.2f %.2f]"
- " appRequestRange=[%.2f %.2f]",
- policy->defaultConfig.value(), defaultConfig->getConfigGroup(), policy->primaryRange.min,
- policy->primaryRange.max, policy->appRequestRange.min, policy->appRequestRange.max);
+ ALOGV("constructAvailableRefreshRates: %s ", policy->toString().c_str());
- auto filterRefreshRates = [&](float min, float max, const char* listName,
+ auto filterRefreshRates = [&](Fps min, Fps max, const char* listName,
std::vector<const RefreshRate*>* outRefreshRates) {
getSortedRefreshRateList(
[&](const RefreshRate& refreshRate) REQUIRES(mLock) {
@@ -572,12 +571,12 @@
outRefreshRates);
LOG_ALWAYS_FATAL_IF(outRefreshRates->empty(),
- "No matching configs for %s range: min=%.0f max=%.0f", listName, min,
- max);
+ "No matching configs for %s range: min=%s max=%s", listName,
+ to_string(min).c_str(), to_string(max).c_str());
auto stringifyRefreshRates = [&]() -> std::string {
std::string str;
for (auto refreshRate : *outRefreshRates) {
- base::StringAppendF(&str, "%s ", refreshRate->name.c_str());
+ base::StringAppendF(&str, "%s ", refreshRate->getName().c_str());
}
return str;
};
@@ -590,39 +589,39 @@
&mAppRequestRefreshRates);
}
-std::vector<float> RefreshRateConfigs::constructKnownFrameRates(
+std::vector<Fps> RefreshRateConfigs::constructKnownFrameRates(
const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs) {
- std::vector<float> knownFrameRates = {24.0f, 30.0f, 45.0f, 60.0f, 72.0f};
+ std::vector<Fps> knownFrameRates = {Fps(24.0f), Fps(30.0f), Fps(45.0f), Fps(60.0f), Fps(72.0f)};
knownFrameRates.reserve(knownFrameRates.size() + configs.size());
// Add all supported refresh rates to the set
for (const auto& config : configs) {
- const auto refreshRate = 1e9f / config->getVsyncPeriod();
+ const auto refreshRate = Fps::fromPeriodNsecs(config->getVsyncPeriod());
knownFrameRates.emplace_back(refreshRate);
}
// Sort and remove duplicates
- const auto frameRatesEqual = [](float a, float b) { return std::abs(a - b) <= 0.01f; };
- std::sort(knownFrameRates.begin(), knownFrameRates.end());
+ std::sort(knownFrameRates.begin(), knownFrameRates.end(), Fps::comparesLess);
knownFrameRates.erase(std::unique(knownFrameRates.begin(), knownFrameRates.end(),
- frameRatesEqual),
+ Fps::EqualsWithMargin()),
knownFrameRates.end());
return knownFrameRates;
}
-float RefreshRateConfigs::findClosestKnownFrameRate(float frameRate) const {
- if (frameRate <= *mKnownFrameRates.begin()) {
+Fps RefreshRateConfigs::findClosestKnownFrameRate(Fps frameRate) const {
+ if (frameRate.lessThanOrEqualWithMargin(*mKnownFrameRates.begin())) {
return *mKnownFrameRates.begin();
}
- if (frameRate >= *std::prev(mKnownFrameRates.end())) {
+ if (frameRate.greaterThanOrEqualWithMargin(*std::prev(mKnownFrameRates.end()))) {
return *std::prev(mKnownFrameRates.end());
}
- auto lowerBound = std::lower_bound(mKnownFrameRates.begin(), mKnownFrameRates.end(), frameRate);
+ auto lowerBound = std::lower_bound(mKnownFrameRates.begin(), mKnownFrameRates.end(), frameRate,
+ Fps::comparesLess);
- const auto distance1 = std::abs(frameRate - *lowerBound);
- const auto distance2 = std::abs(frameRate - *std::prev(lowerBound));
+ const auto distance1 = std::abs((frameRate.getValue() - lowerBound->getValue()));
+ const auto distance2 = std::abs((frameRate.getValue() - std::prev(lowerBound)->getValue()));
return distance1 < distance2 ? *lowerBound : *std::prev(lowerBound);
}
@@ -657,7 +656,7 @@
std::lock_guard lock(mLock);
if (frameRateOverride.frameRateHz != 0) {
- mPreferredRefreshRateForUid[frameRateOverride.uid] = frameRateOverride.frameRateHz;
+ mPreferredRefreshRateForUid[frameRateOverride.uid] = Fps(frameRateOverride.frameRateHz);
} else {
mPreferredRefreshRateForUid.erase(frameRateOverride.uid);
}
@@ -675,7 +674,7 @@
// in DisplayManagerService.getDisplayInfoForFrameRateOverride
constexpr float kThreshold = 0.1f;
const auto refreshRateHz = iter->second;
- const auto numPeriods = mCurrentRefreshRate->getFps() / refreshRateHz;
+ const auto numPeriods = mCurrentRefreshRate->getFps().getValue() / refreshRateHz.getValue();
const auto numPeriodsRounded = std::round(numPeriods);
if (std::abs(numPeriods - numPeriodsRounded) > kThreshold) {
return 1;
@@ -690,7 +689,7 @@
overrides.reserve(mPreferredRefreshRateForUid.size());
for (const auto [uid, frameRate] : mPreferredRefreshRateForUid) {
- overrides.emplace_back(FrameRateOverride{uid, frameRate});
+ overrides.emplace_back(FrameRateOverride{uid, frameRate.getValue()});
}
return overrides;
diff --git a/services/surfaceflinger/Scheduler/RefreshRateConfigs.h b/services/surfaceflinger/Scheduler/RefreshRateConfigs.h
index 6e0c0d3..ec7ffe5 100644
--- a/services/surfaceflinger/Scheduler/RefreshRateConfigs.h
+++ b/services/surfaceflinger/Scheduler/RefreshRateConfigs.h
@@ -25,6 +25,7 @@
#include <type_traits>
#include "DisplayHardware/HWComposer.h"
+#include "Fps.h"
#include "HwcStrongTypes.h"
#include "Scheduler/SchedulerUtils.h"
#include "Scheduler/Seamlessness.h"
@@ -64,29 +65,31 @@
public:
RefreshRate(HwcConfigIndexType configId,
- std::shared_ptr<const HWC2::Display::Config> config, std::string name,
- float fps, ConstructorTag)
- : configId(configId), hwcConfig(config), name(std::move(name)), fps(fps) {}
+ std::shared_ptr<const HWC2::Display::Config> config, Fps fps, ConstructorTag)
+ : configId(configId), hwcConfig(config), fps(std::move(fps)) {}
RefreshRate(const RefreshRate&) = delete;
HwcConfigIndexType getConfigId() const { return configId; }
nsecs_t getVsyncPeriod() const { return hwcConfig->getVsyncPeriod(); }
int32_t getConfigGroup() const { return hwcConfig->getConfigGroup(); }
- const std::string& getName() const { return name; }
- float getFps() const { return fps; }
+ std::string getName() const { return to_string(fps); }
+ Fps getFps() const { return fps; }
// Checks whether the fps of this RefreshRate struct is within a given min and max refresh
- // rate passed in. FPS_EPSILON is applied to the boundaries for approximation.
- bool inPolicy(float minRefreshRate, float maxRefreshRate) const {
- return (fps >= (minRefreshRate - FPS_EPSILON) && fps <= (maxRefreshRate + FPS_EPSILON));
+ // rate passed in. Margin of error is applied to the boundaries for approximation.
+ bool inPolicy(Fps minRefreshRate, Fps maxRefreshRate) const {
+ return minRefreshRate.lessThanOrEqualWithMargin(fps) &&
+ fps.lessThanOrEqualWithMargin(maxRefreshRate);
}
bool operator!=(const RefreshRate& other) const {
return configId != other.configId || hwcConfig != other.hwcConfig;
}
- bool operator<(const RefreshRate& other) const { return getFps() < other.getFps(); }
+ bool operator<(const RefreshRate& other) const {
+ return getFps().getValue() < other.getFps().getValue();
+ }
bool operator==(const RefreshRate& other) const { return !(*this != other); }
@@ -96,18 +99,13 @@
friend RefreshRateConfigs;
friend class RefreshRateConfigsTest;
- // The tolerance within which we consider FPS approximately equals.
- static constexpr float FPS_EPSILON = 0.001f;
-
// This config ID corresponds to the position of the config in the vector that is stored
// on the device.
const HwcConfigIndexType configId;
// The config itself
std::shared_ptr<const HWC2::Display::Config> hwcConfig;
- // Human readable name of the refresh rate.
- const std::string name;
// Refresh rate in frames per second
- const float fps = 0;
+ const Fps fps{0.0f};
};
using AllRefreshRatesMapType =
@@ -119,14 +117,19 @@
public:
struct Range {
- float min = 0;
- float max = std::numeric_limits<float>::max();
+ Fps min{0.0f};
+ Fps max{std::numeric_limits<float>::max()};
bool operator==(const Range& other) const {
- return min == other.min && max == other.max;
+ return min.equalsWithMargin(other.min) && max.equalsWithMargin(other.max);
}
bool operator!=(const Range& other) const { return !(*this == other); }
+
+ std::string toString() const {
+ return base::StringPrintf("[%s %s]", to_string(min).c_str(),
+ to_string(max).c_str());
+ }
};
// The default config, used to ensure we only initiate display config switches within the
@@ -221,7 +224,7 @@
// Layer vote type.
LayerVoteType vote = LayerVoteType::NoVote;
// Layer's desired refresh rate, if applicable.
- float desiredRefreshRate = 0.0f;
+ Fps desiredRefreshRate{0.0f};
// If a seamless mode switch is required.
Seamlessness seamlessness = Seamlessness::Default;
// Layer's weight in the range of [0, 1]. The higher the weight the more impact this layer
@@ -232,7 +235,7 @@
bool operator==(const LayerRequirement& other) const {
return name == other.name && vote == other.vote &&
- desiredRefreshRate == other.desiredRefreshRate &&
+ desiredRefreshRate.equalsWithMargin(other.desiredRefreshRate) &&
seamlessness == other.seamlessness && weight == other.weight &&
focused == other.focused;
}
@@ -295,7 +298,7 @@
static std::string layerVoteTypeString(LayerVoteType vote);
// Returns a known frame rate that is the closest to frameRate
- float findClosestKnownFrameRate(float frameRate) const;
+ Fps findClosestKnownFrameRate(Fps frameRate) const;
RefreshRateConfigs(const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs,
HwcConfigIndexType currentConfigId);
@@ -332,7 +335,7 @@
friend class RefreshRateConfigsTest;
void constructAvailableRefreshRates() REQUIRES(mLock);
- static std::vector<float> constructKnownFrameRates(
+ static std::vector<Fps> constructKnownFrameRates(
const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs);
void getSortedRefreshRateList(
@@ -387,7 +390,7 @@
// A mapping between a UID and a preferred refresh rate that this app would
// run at.
- std::unordered_map<uid_t, float> mPreferredRefreshRateForUid GUARDED_BY(mLock);
+ std::unordered_map<uid_t, Fps> mPreferredRefreshRateForUid GUARDED_BY(mLock);
// The min and max refresh rates supported by the device.
// This will not change at runtime.
@@ -398,7 +401,7 @@
// A sorted list of known frame rates that a Heuristic layer will choose
// from based on the closest value.
- const std::vector<float> mKnownFrameRates;
+ const std::vector<Fps> mKnownFrameRates;
};
} // namespace android::scheduler
diff --git a/services/surfaceflinger/Scheduler/RefreshRateStats.h b/services/surfaceflinger/Scheduler/RefreshRateStats.h
index d9e7b37..ce91a76 100644
--- a/services/surfaceflinger/Scheduler/RefreshRateStats.h
+++ b/services/surfaceflinger/Scheduler/RefreshRateStats.h
@@ -115,8 +115,10 @@
mConfigModesTotalTime[mCurrentConfigMode] = 0;
}
mConfigModesTotalTime[mCurrentConfigMode] += timeElapsedMs;
- fps = static_cast<uint32_t>(std::round(
- mRefreshRateConfigs.getRefreshRateFromConfigId(mCurrentConfigMode).getFps()));
+ fps = static_cast<uint32_t>(
+ std::round(mRefreshRateConfigs.getRefreshRateFromConfigId(mCurrentConfigMode)
+ .getFps()
+ .getValue()));
} else {
mScreenOffTime += timeElapsedMs;
}
diff --git a/services/surfaceflinger/Scheduler/Scheduler.cpp b/services/surfaceflinger/Scheduler/Scheduler.cpp
index 52bf483..07411b0 100644
--- a/services/surfaceflinger/Scheduler/Scheduler.cpp
+++ b/services/surfaceflinger/Scheduler/Scheduler.cpp
@@ -510,25 +510,22 @@
void Scheduler::registerLayer(Layer* layer) {
if (!mLayerHistory) return;
- const auto minFps = mRefreshRateConfigs.getMinRefreshRate().getFps();
const auto maxFps = mRefreshRateConfigs.getMaxRefreshRate().getFps();
if (layer->getWindowType() == InputWindowInfo::Type::STATUS_BAR) {
- mLayerHistory->registerLayer(layer, minFps, maxFps,
- scheduler::LayerHistory::LayerVoteType::NoVote);
+ mLayerHistory->registerLayer(layer, maxFps, scheduler::LayerHistory::LayerVoteType::NoVote);
} else if (!mOptions.useContentDetection) {
// If the content detection feature is off, all layers are registered at Max. We still keep
// the layer history, since we use it for other features (like Frame Rate API), so layers
// still need to be registered.
- mLayerHistory->registerLayer(layer, minFps, maxFps,
- scheduler::LayerHistory::LayerVoteType::Max);
+ mLayerHistory->registerLayer(layer, maxFps, scheduler::LayerHistory::LayerVoteType::Max);
} else {
if (layer->getWindowType() == InputWindowInfo::Type::WALLPAPER) {
// Running Wallpaper at Min is considered as part of content detection.
- mLayerHistory->registerLayer(layer, minFps, maxFps,
+ mLayerHistory->registerLayer(layer, maxFps,
scheduler::LayerHistory::LayerVoteType::Min);
} else {
- mLayerHistory->registerLayer(layer, minFps, maxFps,
+ mLayerHistory->registerLayer(layer, maxFps,
scheduler::LayerHistory::LayerVoteType::Heuristic);
}
}
@@ -618,14 +615,15 @@
// TODO(145561154): cleanup the kernel idle timer implementation and the refresh rate
// magic number
const auto& refreshRate = mRefreshRateConfigs.getCurrentRefreshRate();
- constexpr float FPS_THRESHOLD_FOR_KERNEL_TIMER = 65.0f;
- if (state == TimerState::Reset && refreshRate.getFps() > FPS_THRESHOLD_FOR_KERNEL_TIMER) {
+ constexpr Fps FPS_THRESHOLD_FOR_KERNEL_TIMER{65.0f};
+ if (state == TimerState::Reset &&
+ refreshRate.getFps().greaterThanWithMargin(FPS_THRESHOLD_FOR_KERNEL_TIMER)) {
// If we're not in performance mode then the kernel timer shouldn't do
// anything, as the refresh rate during DPU power collapse will be the
// same.
resyncToHardwareVsync(true /* makeAvailable */, refreshRate.getVsyncPeriod());
} else if (state == TimerState::Expired &&
- refreshRate.getFps() <= FPS_THRESHOLD_FOR_KERNEL_TIMER) {
+ refreshRate.getFps().lessThanOrEqualWithMargin(FPS_THRESHOLD_FOR_KERNEL_TIMER)) {
// Disable HW VSYNC if the timer expired, as we don't need it enabled if
// we're not pushing frames, and if we're in PERFORMANCE mode then we'll
// need to update the VsyncController model anyway.
diff --git a/services/surfaceflinger/Scheduler/VsyncConfiguration.cpp b/services/surfaceflinger/Scheduler/VsyncConfiguration.cpp
index aac2569..8431323 100644
--- a/services/surfaceflinger/Scheduler/VsyncConfiguration.cpp
+++ b/services/surfaceflinger/Scheduler/VsyncConfiguration.cpp
@@ -31,15 +31,10 @@
return std::nullopt;
}
-bool fpsEqualsWithMargin(float fpsA, float fpsB) {
- static constexpr float MARGIN = 0.01f;
- return std::abs(fpsA - fpsB) <= MARGIN;
-}
-
-std::vector<float> getRefreshRatesFromConfigs(
+std::vector<android::Fps> getRefreshRatesFromConfigs(
const android::scheduler::RefreshRateConfigs& refreshRateConfigs) {
const auto& allRefreshRates = refreshRateConfigs.getAllRefreshRates();
- std::vector<float> refreshRates;
+ std::vector<android::Fps> refreshRates;
refreshRates.reserve(allRefreshRates.size());
for (const auto& [ignored, refreshRate] : allRefreshRates) {
@@ -53,12 +48,12 @@
namespace android::scheduler::impl {
-VsyncConfiguration::VsyncConfiguration(float currentFps) : mRefreshRateFps(currentFps) {}
+VsyncConfiguration::VsyncConfiguration(Fps currentFps) : mRefreshRateFps(currentFps) {}
-PhaseOffsets::VsyncConfigSet VsyncConfiguration::getConfigsForRefreshRate(float fps) const {
+PhaseOffsets::VsyncConfigSet VsyncConfiguration::getConfigsForRefreshRate(Fps fps) const {
const auto iter = std::find_if(mOffsets.begin(), mOffsets.end(),
- [&fps](const std::pair<float, VsyncConfigSet>& candidateFps) {
- return fpsEqualsWithMargin(fps, candidateFps.first);
+ [&fps](const std::pair<Fps, VsyncConfigSet>& candidateFps) {
+ return fps.equalsWithMargin(candidateFps.first);
});
if (iter != mOffsets.end()) {
@@ -67,13 +62,13 @@
// Unknown refresh rate. This might happen if we get a hotplug event for an external display.
// In this case just construct the offset.
- ALOGW("Can't find offset for %.2f fps", fps);
- return constructOffsets(static_cast<nsecs_t>(1e9f / fps));
+ ALOGW("Can't find offset for %s", to_string(fps).c_str());
+ return constructOffsets(fps.getPeriodNsecs());
}
-void VsyncConfiguration::initializeOffsets(const std::vector<float>& refreshRates) {
+void VsyncConfiguration::initializeOffsets(const std::vector<Fps>& refreshRates) {
for (const auto fps : refreshRates) {
- mOffsets.emplace(fps, constructOffsets(static_cast<nsecs_t>(1e9f / fps)));
+ mOffsets.emplace(fps, constructOffsets(fps.getPeriodNsecs()));
}
}
@@ -127,7 +122,7 @@
.value_or(std::numeric_limits<nsecs_t>::max())) {}
PhaseOffsets::PhaseOffsets(
- const std::vector<float>& refreshRates, float currentFps, nsecs_t vsyncPhaseOffsetNs,
+ const std::vector<Fps>& refreshRates, Fps currentFps, nsecs_t vsyncPhaseOffsetNs,
nsecs_t sfVSyncPhaseOffsetNs, std::optional<nsecs_t> earlySfOffsetNs,
std::optional<nsecs_t> earlyGpuSfOffsetNs, std::optional<nsecs_t> earlyAppOffsetNs,
std::optional<nsecs_t> earlyGpuAppOffsetNs, nsecs_t highFpsVsyncPhaseOffsetNs,
@@ -378,10 +373,9 @@
validateSysprops();
}
-WorkDuration::WorkDuration(const std::vector<float>& refreshRates, float currentFps,
- nsecs_t sfDuration, nsecs_t appDuration, nsecs_t sfEarlyDuration,
- nsecs_t appEarlyDuration, nsecs_t sfEarlyGpuDuration,
- nsecs_t appEarlyGpuDuration)
+WorkDuration::WorkDuration(const std::vector<Fps>& refreshRates, Fps currentFps, nsecs_t sfDuration,
+ nsecs_t appDuration, nsecs_t sfEarlyDuration, nsecs_t appEarlyDuration,
+ nsecs_t sfEarlyGpuDuration, nsecs_t appEarlyGpuDuration)
: VsyncConfiguration(currentFps),
mSfDuration(sfDuration),
mAppDuration(appDuration),
diff --git a/services/surfaceflinger/Scheduler/VsyncConfiguration.h b/services/surfaceflinger/Scheduler/VsyncConfiguration.h
index c27a25d..a120e97 100644
--- a/services/surfaceflinger/Scheduler/VsyncConfiguration.h
+++ b/services/surfaceflinger/Scheduler/VsyncConfiguration.h
@@ -18,6 +18,9 @@
#include <unordered_map>
+#include <utils/Timers.h>
+
+#include "Fps.h"
#include "RefreshRateConfigs.h"
#include "VsyncModulator.h"
@@ -35,9 +38,9 @@
virtual ~VsyncConfiguration() = default;
virtual VsyncConfigSet getCurrentConfigs() const = 0;
- virtual VsyncConfigSet getConfigsForRefreshRate(float fps) const = 0;
+ virtual VsyncConfigSet getConfigsForRefreshRate(Fps fps) const = 0;
- virtual void setRefreshRateFps(float fps) = 0;
+ virtual void setRefreshRateFps(Fps fps) = 0;
virtual void dump(std::string& result) const = 0;
};
@@ -51,10 +54,10 @@
*/
class VsyncConfiguration : public scheduler::VsyncConfiguration {
public:
- explicit VsyncConfiguration(float currentFps);
+ explicit VsyncConfiguration(Fps currentFps);
// Returns early, early GL, and late offsets for Apps and SF for a given refresh rate.
- VsyncConfigSet getConfigsForRefreshRate(float fps) const override;
+ VsyncConfigSet getConfigsForRefreshRate(Fps fps) const override;
// Returns early, early GL, and late offsets for Apps and SF.
VsyncConfigSet getCurrentConfigs() const override {
@@ -63,17 +66,17 @@
// This function should be called when the device is switching between different
// refresh rates, to properly update the offsets.
- void setRefreshRateFps(float fps) override { mRefreshRateFps = fps; }
+ void setRefreshRateFps(Fps fps) override { mRefreshRateFps = fps; }
// Returns current offsets in human friendly format.
void dump(std::string& result) const override;
protected:
- void initializeOffsets(const std::vector<float>& refreshRates);
+ void initializeOffsets(const std::vector<Fps>& refreshRates);
virtual VsyncConfiguration::VsyncConfigSet constructOffsets(nsecs_t vsyncDuration) const = 0;
- std::unordered_map<float, VsyncConfigSet> mOffsets;
- std::atomic<float> mRefreshRateFps;
+ std::unordered_map<Fps, VsyncConfigSet, std::hash<Fps>, Fps::EqualsInBuckets> mOffsets;
+ std::atomic<Fps> mRefreshRateFps;
};
/*
@@ -86,10 +89,9 @@
protected:
// Used for unit tests
- PhaseOffsets(const std::vector<float>& refreshRates, float currentFps,
- nsecs_t vsyncPhaseOffsetNs, nsecs_t sfVSyncPhaseOffsetNs,
- std::optional<nsecs_t> earlySfOffsetNs, std::optional<nsecs_t> earlyGpuSfOffsetNs,
- std::optional<nsecs_t> earlyAppOffsetNs,
+ PhaseOffsets(const std::vector<Fps>& refreshRates, Fps currentFps, nsecs_t vsyncPhaseOffsetNs,
+ nsecs_t sfVSyncPhaseOffsetNs, std::optional<nsecs_t> earlySfOffsetNs,
+ std::optional<nsecs_t> earlyGpuSfOffsetNs, std::optional<nsecs_t> earlyAppOffsetNs,
std::optional<nsecs_t> earlyGpuAppOffsetNs, nsecs_t highFpsVsyncPhaseOffsetNs,
nsecs_t highFpsSfVSyncPhaseOffsetNs, std::optional<nsecs_t> highFpsEarlySfOffsetNs,
std::optional<nsecs_t> highFpsEarlyGpuSfOffsetNs,
@@ -130,7 +132,7 @@
protected:
// Used for unit tests
- WorkDuration(const std::vector<float>& refreshRates, float currentFps, nsecs_t sfDuration,
+ WorkDuration(const std::vector<Fps>& refreshRates, Fps currentFps, nsecs_t sfDuration,
nsecs_t appDuration, nsecs_t sfEarlyDuration, nsecs_t appEarlyDuration,
nsecs_t sfEarlyGpuDuration, nsecs_t appEarlyGpuDuration);