blob: 847e20cf89928519e6c812c4b8013440bb34bd82 [file] [log] [blame]
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -08001/*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ady Abraham2139f732019-11-13 18:56:40 -080016
17// #define LOG_NDEBUG 0
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080018#include "RefreshRateConfigs.h"
19
20namespace android::scheduler {
Ady Abraham2139f732019-11-13 18:56:40 -080021
22using AllRefreshRatesMapType = RefreshRateConfigs::AllRefreshRatesMapType;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080023using RefreshRate = RefreshRateConfigs::RefreshRate;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080024
25// Returns the refresh rate map. This map won't be modified at runtime, so it's safe to access
26// from multiple threads. This can only be called if refreshRateSwitching() returns true.
27// TODO(b/122916473): Get this information from configs prepared by vendors, instead of
28// baking them in.
Ady Abraham2139f732019-11-13 18:56:40 -080029const RefreshRate& RefreshRateConfigs::getRefreshRateForContent(float contentFramerate) const {
30 std::lock_guard lock(mLock);
31 // Find the appropriate refresh rate with minimal error
32 auto iter = min_element(mAvailableRefreshRates.cbegin(), mAvailableRefreshRates.cend(),
33 [contentFramerate](const auto& lhs, const auto& rhs) -> bool {
34 return std::abs(lhs->fps - contentFramerate) <
35 std::abs(rhs->fps - contentFramerate);
36 });
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080037
Ady Abraham2139f732019-11-13 18:56:40 -080038 // Some content aligns better on higher refresh rate. For example for 45fps we should choose
39 // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't
40 // align well with both
41 const RefreshRate* bestSoFar = *iter;
42 constexpr float MARGIN = 0.05f;
43 float ratio = (*iter)->fps / contentFramerate;
44 if (std::abs(std::round(ratio) - ratio) > MARGIN) {
45 while (iter != mAvailableRefreshRates.cend()) {
46 ratio = (*iter)->fps / contentFramerate;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080047
Ady Abraham2139f732019-11-13 18:56:40 -080048 if (std::abs(std::round(ratio) - ratio) <= MARGIN) {
49 bestSoFar = *iter;
50 break;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080051 }
Ady Abraham2139f732019-11-13 18:56:40 -080052 ++iter;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080053 }
54 }
55
Ady Abraham2139f732019-11-13 18:56:40 -080056 return *bestSoFar;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080057}
58
Ady Abraham2139f732019-11-13 18:56:40 -080059const AllRefreshRatesMapType& RefreshRateConfigs::getAllRefreshRates() const {
60 return mRefreshRates;
61}
62
63const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicy() const {
64 std::lock_guard lock(mLock);
65 if (!mRefreshRateSwitching) {
66 return *mCurrentRefreshRate;
67 } else {
68 return *mAvailableRefreshRates.front();
69 }
70}
71
72const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicy() const {
73 std::lock_guard lock(mLock);
74 if (!mRefreshRateSwitching) {
75 return *mCurrentRefreshRate;
76 } else {
77 return *mAvailableRefreshRates.back();
78 }
79}
80
81const RefreshRate& RefreshRateConfigs::getCurrentRefreshRate() const {
82 std::lock_guard lock(mLock);
83 return *mCurrentRefreshRate;
84}
85
86void RefreshRateConfigs::setCurrentConfigId(HwcConfigIndexType configId) {
87 std::lock_guard lock(mLock);
88 mCurrentRefreshRate = &mRefreshRates.at(configId);
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080089}
90
91RefreshRateConfigs::RefreshRateConfigs(bool refreshRateSwitching,
Ady Abraham2139f732019-11-13 18:56:40 -080092 const std::vector<InputConfig>& configs,
93 HwcConfigIndexType currentHwcConfig)
94 : mRefreshRateSwitching(refreshRateSwitching) {
95 init(configs, currentHwcConfig);
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080096}
97
98RefreshRateConfigs::RefreshRateConfigs(
99 bool refreshRateSwitching,
100 const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs,
Ady Abraham2139f732019-11-13 18:56:40 -0800101 HwcConfigIndexType currentConfigId)
102 : mRefreshRateSwitching(refreshRateSwitching) {
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800103 std::vector<InputConfig> inputConfigs;
Ady Abraham2139f732019-11-13 18:56:40 -0800104 for (auto configId = HwcConfigIndexType(0); configId < HwcConfigIndexType(configs.size());
105 ++configId) {
106 auto configGroup = HwcConfigGroupType(configs[configId.value()]->getConfigGroup());
107 inputConfigs.push_back(
108 {configId, configGroup, configs[configId.value()]->getVsyncPeriod()});
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800109 }
Ady Abraham2139f732019-11-13 18:56:40 -0800110 init(inputConfigs, currentConfigId);
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800111}
112
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100113status_t RefreshRateConfigs::setPolicy(HwcConfigIndexType defaultConfigId, float minRefreshRate,
114 float maxRefreshRate, bool* outPolicyChanged) {
Ady Abraham2139f732019-11-13 18:56:40 -0800115 std::lock_guard lock(mLock);
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100116 bool policyChanged = defaultConfigId != mDefaultConfig ||
117 minRefreshRate != mMinRefreshRateFps || maxRefreshRate != mMaxRefreshRateFps;
118 if (outPolicyChanged) {
119 *outPolicyChanged = policyChanged;
120 }
121 if (!policyChanged) {
122 return NO_ERROR;
123 }
124 // defaultConfigId must be a valid config ID, and within the given refresh rate range.
125 if (mRefreshRates.count(defaultConfigId) == 0) {
126 return BAD_VALUE;
127 }
128 const RefreshRate& refreshRate = mRefreshRates.at(defaultConfigId);
129 if (refreshRate.fps < minRefreshRate || refreshRate.fps > maxRefreshRate) {
130 return BAD_VALUE;
131 }
132 mDefaultConfig = defaultConfigId;
Ady Abraham2139f732019-11-13 18:56:40 -0800133 mMinRefreshRateFps = minRefreshRate;
134 mMaxRefreshRateFps = maxRefreshRate;
135 constructAvailableRefreshRates();
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100136 return NO_ERROR;
137}
138
139void RefreshRateConfigs::getPolicy(HwcConfigIndexType* defaultConfigId, float* minRefreshRate,
140 float* maxRefreshRate) const {
141 std::lock_guard lock(mLock);
142 *defaultConfigId = mDefaultConfig;
143 *minRefreshRate = mMinRefreshRateFps;
144 *maxRefreshRate = mMaxRefreshRateFps;
145}
146
147bool RefreshRateConfigs::isConfigAllowed(HwcConfigIndexType config) const {
148 std::lock_guard lock(mLock);
149 for (const RefreshRate* refreshRate : mAvailableRefreshRates) {
150 if (refreshRate->configId == config) {
151 return true;
152 }
153 }
154 return false;
Ady Abraham2139f732019-11-13 18:56:40 -0800155}
156
157void RefreshRateConfigs::getSortedRefreshRateList(
158 const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate,
159 std::vector<const RefreshRate*>* outRefreshRates) {
160 outRefreshRates->clear();
161 outRefreshRates->reserve(mRefreshRates.size());
162 for (const auto& [type, refreshRate] : mRefreshRates) {
163 if (shouldAddRefreshRate(refreshRate)) {
164 ALOGV("getSortedRefreshRateList: config %d added to list policy",
165 refreshRate.configId.value());
166 outRefreshRates->push_back(&refreshRate);
167 }
168 }
169
170 std::sort(outRefreshRates->begin(), outRefreshRates->end(),
171 [](const auto refreshRate1, const auto refreshRate2) {
172 return refreshRate1->vsyncPeriod > refreshRate2->vsyncPeriod;
173 });
174}
175
176void RefreshRateConfigs::constructAvailableRefreshRates() {
177 // Filter configs based on current policy and sort based on vsync period
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100178 HwcConfigGroupType group = mRefreshRates.at(mDefaultConfig).configGroup;
179 ALOGV("constructRefreshRateMap: default %d group %d min %.2f max %.2f", mDefaultConfig.value(),
180 group.value(), mMinRefreshRateFps, mMaxRefreshRateFps);
Ady Abraham2139f732019-11-13 18:56:40 -0800181 getSortedRefreshRateList(
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100182 [&](const RefreshRate& refreshRate) REQUIRES(mLock) {
183 return refreshRate.configGroup == group && refreshRate.fps >= mMinRefreshRateFps &&
Ady Abraham2139f732019-11-13 18:56:40 -0800184 refreshRate.fps <= mMaxRefreshRateFps;
185 },
186 &mAvailableRefreshRates);
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100187 LOG_ALWAYS_FATAL_IF(mAvailableRefreshRates.empty(),
188 "No compatible display configs for default=%d min=%.0f max=%.0f",
189 mDefaultConfig.value(), mMinRefreshRateFps, mMaxRefreshRateFps);
Ady Abraham2139f732019-11-13 18:56:40 -0800190}
191
192// NO_THREAD_SAFETY_ANALYSIS since this is called from the constructor
193void RefreshRateConfigs::init(const std::vector<InputConfig>& configs,
194 HwcConfigIndexType currentHwcConfig) NO_THREAD_SAFETY_ANALYSIS {
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800195 LOG_ALWAYS_FATAL_IF(configs.empty());
Ady Abraham2139f732019-11-13 18:56:40 -0800196 LOG_ALWAYS_FATAL_IF(currentHwcConfig.value() >= configs.size());
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800197
Ady Abraham2139f732019-11-13 18:56:40 -0800198 auto buildRefreshRate = [&](InputConfig config) -> RefreshRate {
199 const float fps = 1e9f / config.vsyncPeriod;
200 return RefreshRate(config.configId, config.vsyncPeriod, config.configGroup,
201 base::StringPrintf("%2.ffps", fps), fps);
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800202 };
203
Ady Abraham2139f732019-11-13 18:56:40 -0800204 for (const auto& config : configs) {
205 mRefreshRates.emplace(config.configId, buildRefreshRate(config));
206 if (config.configId == currentHwcConfig) {
207 mCurrentRefreshRate = &mRefreshRates.at(config.configId);
Ady Abraham2139f732019-11-13 18:56:40 -0800208 }
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800209 }
210
Ady Abraham2139f732019-11-13 18:56:40 -0800211 std::vector<const RefreshRate*> sortedConfigs;
212 getSortedRefreshRateList([](const RefreshRate&) { return true; }, &sortedConfigs);
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100213 mDefaultConfig = currentHwcConfig;
Ady Abraham2139f732019-11-13 18:56:40 -0800214 mMinSupportedRefreshRate = sortedConfigs.front();
215 mMaxSupportedRefreshRate = sortedConfigs.back();
216 constructAvailableRefreshRates();
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800217}
218
Ady Abraham2139f732019-11-13 18:56:40 -0800219} // namespace android::scheduler