blob: 45d1f23029c44a81b5db15b227b50ef4e2eb8327 [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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Ady Abraham2139f732019-11-13 18:56:40 -080021// #define LOG_NDEBUG 0
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080022#include "RefreshRateConfigs.h"
23
24namespace android::scheduler {
Ady Abraham2139f732019-11-13 18:56:40 -080025
26using AllRefreshRatesMapType = RefreshRateConfigs::AllRefreshRatesMapType;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080027using RefreshRate = RefreshRateConfigs::RefreshRate;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080028
29// Returns the refresh rate map. This map won't be modified at runtime, so it's safe to access
30// from multiple threads. This can only be called if refreshRateSwitching() returns true.
31// TODO(b/122916473): Get this information from configs prepared by vendors, instead of
32// baking them in.
Ady Abraham2139f732019-11-13 18:56:40 -080033const RefreshRate& RefreshRateConfigs::getRefreshRateForContent(float contentFramerate) const {
34 std::lock_guard lock(mLock);
35 // Find the appropriate refresh rate with minimal error
36 auto iter = min_element(mAvailableRefreshRates.cbegin(), mAvailableRefreshRates.cend(),
37 [contentFramerate](const auto& lhs, const auto& rhs) -> bool {
38 return std::abs(lhs->fps - contentFramerate) <
39 std::abs(rhs->fps - contentFramerate);
40 });
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080041
Ady Abraham2139f732019-11-13 18:56:40 -080042 // Some content aligns better on higher refresh rate. For example for 45fps we should choose
43 // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't
44 // align well with both
45 const RefreshRate* bestSoFar = *iter;
46 constexpr float MARGIN = 0.05f;
47 float ratio = (*iter)->fps / contentFramerate;
48 if (std::abs(std::round(ratio) - ratio) > MARGIN) {
49 while (iter != mAvailableRefreshRates.cend()) {
50 ratio = (*iter)->fps / contentFramerate;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080051
Ady Abraham2139f732019-11-13 18:56:40 -080052 if (std::abs(std::round(ratio) - ratio) <= MARGIN) {
53 bestSoFar = *iter;
54 break;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080055 }
Ady Abraham2139f732019-11-13 18:56:40 -080056 ++iter;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080057 }
58 }
59
Ady Abraham2139f732019-11-13 18:56:40 -080060 return *bestSoFar;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080061}
62
Ady Abraham2139f732019-11-13 18:56:40 -080063const AllRefreshRatesMapType& RefreshRateConfigs::getAllRefreshRates() const {
64 return mRefreshRates;
65}
66
67const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicy() const {
68 std::lock_guard lock(mLock);
69 if (!mRefreshRateSwitching) {
70 return *mCurrentRefreshRate;
71 } else {
72 return *mAvailableRefreshRates.front();
73 }
74}
75
76const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicy() const {
77 std::lock_guard lock(mLock);
78 if (!mRefreshRateSwitching) {
79 return *mCurrentRefreshRate;
80 } else {
81 return *mAvailableRefreshRates.back();
82 }
83}
84
85const RefreshRate& RefreshRateConfigs::getCurrentRefreshRate() const {
86 std::lock_guard lock(mLock);
87 return *mCurrentRefreshRate;
88}
89
90void RefreshRateConfigs::setCurrentConfigId(HwcConfigIndexType configId) {
91 std::lock_guard lock(mLock);
92 mCurrentRefreshRate = &mRefreshRates.at(configId);
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080093}
94
95RefreshRateConfigs::RefreshRateConfigs(bool refreshRateSwitching,
Ady Abraham2139f732019-11-13 18:56:40 -080096 const std::vector<InputConfig>& configs,
97 HwcConfigIndexType currentHwcConfig)
98 : mRefreshRateSwitching(refreshRateSwitching) {
99 init(configs, currentHwcConfig);
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800100}
101
102RefreshRateConfigs::RefreshRateConfigs(
103 bool refreshRateSwitching,
104 const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs,
Ady Abraham2139f732019-11-13 18:56:40 -0800105 HwcConfigIndexType currentConfigId)
106 : mRefreshRateSwitching(refreshRateSwitching) {
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800107 std::vector<InputConfig> inputConfigs;
Ady Abraham2139f732019-11-13 18:56:40 -0800108 for (auto configId = HwcConfigIndexType(0); configId < HwcConfigIndexType(configs.size());
109 ++configId) {
110 auto configGroup = HwcConfigGroupType(configs[configId.value()]->getConfigGroup());
111 inputConfigs.push_back(
112 {configId, configGroup, configs[configId.value()]->getVsyncPeriod()});
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800113 }
Ady Abraham2139f732019-11-13 18:56:40 -0800114 init(inputConfigs, currentConfigId);
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800115}
116
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100117status_t RefreshRateConfigs::setPolicy(HwcConfigIndexType defaultConfigId, float minRefreshRate,
118 float maxRefreshRate, bool* outPolicyChanged) {
Ady Abraham2139f732019-11-13 18:56:40 -0800119 std::lock_guard lock(mLock);
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100120 bool policyChanged = defaultConfigId != mDefaultConfig ||
121 minRefreshRate != mMinRefreshRateFps || maxRefreshRate != mMaxRefreshRateFps;
122 if (outPolicyChanged) {
123 *outPolicyChanged = policyChanged;
124 }
125 if (!policyChanged) {
126 return NO_ERROR;
127 }
128 // defaultConfigId must be a valid config ID, and within the given refresh rate range.
129 if (mRefreshRates.count(defaultConfigId) == 0) {
130 return BAD_VALUE;
131 }
132 const RefreshRate& refreshRate = mRefreshRates.at(defaultConfigId);
Ana Krulec72f0d6e2020-01-06 15:24:47 -0800133 if (!refreshRate.inPolicy(minRefreshRate, maxRefreshRate)) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100134 return BAD_VALUE;
135 }
136 mDefaultConfig = defaultConfigId;
Ady Abraham2139f732019-11-13 18:56:40 -0800137 mMinRefreshRateFps = minRefreshRate;
138 mMaxRefreshRateFps = maxRefreshRate;
139 constructAvailableRefreshRates();
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100140 return NO_ERROR;
141}
142
143void RefreshRateConfigs::getPolicy(HwcConfigIndexType* defaultConfigId, float* minRefreshRate,
144 float* maxRefreshRate) const {
145 std::lock_guard lock(mLock);
146 *defaultConfigId = mDefaultConfig;
147 *minRefreshRate = mMinRefreshRateFps;
148 *maxRefreshRate = mMaxRefreshRateFps;
149}
150
151bool RefreshRateConfigs::isConfigAllowed(HwcConfigIndexType config) const {
152 std::lock_guard lock(mLock);
153 for (const RefreshRate* refreshRate : mAvailableRefreshRates) {
154 if (refreshRate->configId == config) {
155 return true;
156 }
157 }
158 return false;
Ady Abraham2139f732019-11-13 18:56:40 -0800159}
160
161void RefreshRateConfigs::getSortedRefreshRateList(
162 const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate,
163 std::vector<const RefreshRate*>* outRefreshRates) {
164 outRefreshRates->clear();
165 outRefreshRates->reserve(mRefreshRates.size());
166 for (const auto& [type, refreshRate] : mRefreshRates) {
167 if (shouldAddRefreshRate(refreshRate)) {
168 ALOGV("getSortedRefreshRateList: config %d added to list policy",
169 refreshRate.configId.value());
170 outRefreshRates->push_back(&refreshRate);
171 }
172 }
173
174 std::sort(outRefreshRates->begin(), outRefreshRates->end(),
175 [](const auto refreshRate1, const auto refreshRate2) {
176 return refreshRate1->vsyncPeriod > refreshRate2->vsyncPeriod;
177 });
178}
179
180void RefreshRateConfigs::constructAvailableRefreshRates() {
181 // Filter configs based on current policy and sort based on vsync period
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100182 HwcConfigGroupType group = mRefreshRates.at(mDefaultConfig).configGroup;
183 ALOGV("constructRefreshRateMap: default %d group %d min %.2f max %.2f", mDefaultConfig.value(),
184 group.value(), mMinRefreshRateFps, mMaxRefreshRateFps);
Ady Abraham2139f732019-11-13 18:56:40 -0800185 getSortedRefreshRateList(
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100186 [&](const RefreshRate& refreshRate) REQUIRES(mLock) {
Ana Krulec72f0d6e2020-01-06 15:24:47 -0800187 return refreshRate.configGroup == group &&
188 refreshRate.inPolicy(mMinRefreshRateFps, mMaxRefreshRateFps);
Ady Abraham2139f732019-11-13 18:56:40 -0800189 },
190 &mAvailableRefreshRates);
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100191 LOG_ALWAYS_FATAL_IF(mAvailableRefreshRates.empty(),
192 "No compatible display configs for default=%d min=%.0f max=%.0f",
193 mDefaultConfig.value(), mMinRefreshRateFps, mMaxRefreshRateFps);
Ady Abraham2139f732019-11-13 18:56:40 -0800194}
195
196// NO_THREAD_SAFETY_ANALYSIS since this is called from the constructor
197void RefreshRateConfigs::init(const std::vector<InputConfig>& configs,
198 HwcConfigIndexType currentHwcConfig) NO_THREAD_SAFETY_ANALYSIS {
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800199 LOG_ALWAYS_FATAL_IF(configs.empty());
Ady Abraham2139f732019-11-13 18:56:40 -0800200 LOG_ALWAYS_FATAL_IF(currentHwcConfig.value() >= configs.size());
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800201
Ady Abraham2139f732019-11-13 18:56:40 -0800202 auto buildRefreshRate = [&](InputConfig config) -> RefreshRate {
203 const float fps = 1e9f / config.vsyncPeriod;
204 return RefreshRate(config.configId, config.vsyncPeriod, config.configGroup,
205 base::StringPrintf("%2.ffps", fps), fps);
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800206 };
207
Ady Abraham2139f732019-11-13 18:56:40 -0800208 for (const auto& config : configs) {
209 mRefreshRates.emplace(config.configId, buildRefreshRate(config));
210 if (config.configId == currentHwcConfig) {
211 mCurrentRefreshRate = &mRefreshRates.at(config.configId);
Ady Abraham2139f732019-11-13 18:56:40 -0800212 }
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800213 }
214
Ady Abraham2139f732019-11-13 18:56:40 -0800215 std::vector<const RefreshRate*> sortedConfigs;
216 getSortedRefreshRateList([](const RefreshRate&) { return true; }, &sortedConfigs);
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100217 mDefaultConfig = currentHwcConfig;
Ady Abraham2139f732019-11-13 18:56:40 -0800218 mMinSupportedRefreshRate = sortedConfigs.front();
219 mMaxSupportedRefreshRate = sortedConfigs.back();
220 constructAvailableRefreshRates();
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800221}
222
Ady Abraham2139f732019-11-13 18:56:40 -0800223} // namespace android::scheduler
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800224// TODO(b/129481165): remove the #pragma below and fix conversion issues
225#pragma clang diagnostic pop // ignored "-Wconversion"