blob: 23fb96a38b8a438bf5d9056bf1e638c41b9d312e [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
Ady Abraham2139f732019-11-13 18:56:40 -0800113void RefreshRateConfigs::setPolicy(HwcConfigIndexType defaultConfigId, float minRefreshRate,
114 float maxRefreshRate) {
115 std::lock_guard lock(mLock);
116 mCurrentGroupId = mRefreshRates.at(defaultConfigId).configGroup;
117 mMinRefreshRateFps = minRefreshRate;
118 mMaxRefreshRateFps = maxRefreshRate;
119 constructAvailableRefreshRates();
120}
121
122void RefreshRateConfigs::getSortedRefreshRateList(
123 const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate,
124 std::vector<const RefreshRate*>* outRefreshRates) {
125 outRefreshRates->clear();
126 outRefreshRates->reserve(mRefreshRates.size());
127 for (const auto& [type, refreshRate] : mRefreshRates) {
128 if (shouldAddRefreshRate(refreshRate)) {
129 ALOGV("getSortedRefreshRateList: config %d added to list policy",
130 refreshRate.configId.value());
131 outRefreshRates->push_back(&refreshRate);
132 }
133 }
134
135 std::sort(outRefreshRates->begin(), outRefreshRates->end(),
136 [](const auto refreshRate1, const auto refreshRate2) {
137 return refreshRate1->vsyncPeriod > refreshRate2->vsyncPeriod;
138 });
139}
140
141void RefreshRateConfigs::constructAvailableRefreshRates() {
142 // Filter configs based on current policy and sort based on vsync period
143 ALOGV("constructRefreshRateMap: group %d min %.2f max %.2f", mCurrentGroupId.value(),
144 mMinRefreshRateFps, mMaxRefreshRateFps);
145 getSortedRefreshRateList(
146 [this](const RefreshRate& refreshRate) REQUIRES(mLock) {
147 return refreshRate.configGroup == mCurrentGroupId &&
148 refreshRate.fps >= mMinRefreshRateFps &&
149 refreshRate.fps <= mMaxRefreshRateFps;
150 },
151 &mAvailableRefreshRates);
152}
153
154// NO_THREAD_SAFETY_ANALYSIS since this is called from the constructor
155void RefreshRateConfigs::init(const std::vector<InputConfig>& configs,
156 HwcConfigIndexType currentHwcConfig) NO_THREAD_SAFETY_ANALYSIS {
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800157 LOG_ALWAYS_FATAL_IF(configs.empty());
Ady Abraham2139f732019-11-13 18:56:40 -0800158 LOG_ALWAYS_FATAL_IF(currentHwcConfig.value() >= configs.size());
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800159
Ady Abraham2139f732019-11-13 18:56:40 -0800160 auto buildRefreshRate = [&](InputConfig config) -> RefreshRate {
161 const float fps = 1e9f / config.vsyncPeriod;
162 return RefreshRate(config.configId, config.vsyncPeriod, config.configGroup,
163 base::StringPrintf("%2.ffps", fps), fps);
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800164 };
165
Ady Abraham2139f732019-11-13 18:56:40 -0800166 for (const auto& config : configs) {
167 mRefreshRates.emplace(config.configId, buildRefreshRate(config));
168 if (config.configId == currentHwcConfig) {
169 mCurrentRefreshRate = &mRefreshRates.at(config.configId);
170 mCurrentGroupId = config.configGroup;
171 }
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800172 }
173
Ady Abraham2139f732019-11-13 18:56:40 -0800174 std::vector<const RefreshRate*> sortedConfigs;
175 getSortedRefreshRateList([](const RefreshRate&) { return true; }, &sortedConfigs);
176 mMinSupportedRefreshRate = sortedConfigs.front();
177 mMaxSupportedRefreshRate = sortedConfigs.back();
178 constructAvailableRefreshRates();
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800179}
180
Ady Abraham2139f732019-11-13 18:56:40 -0800181} // namespace android::scheduler