blob: 651283f7b060424d45303b9eec6ca70f236f2cb0 [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 Abraham8a82ba62020-01-17 12:43:17 -080017// #define LOG_NDEBUG 0
18#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080020#include "RefreshRateConfigs.h"
Ady Abraham8a82ba62020-01-17 12:43:17 -080021#include <android-base/stringprintf.h>
22#include <utils/Trace.h>
23#include <chrono>
24#include <cmath>
25
Ady Abraham5b8afb5a2020-03-06 14:57:26 -080026#undef LOG_TAG
27#define LOG_TAG "RefreshRateConfigs"
28
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080029namespace android::scheduler {
Ady Abraham2139f732019-11-13 18:56:40 -080030
31using AllRefreshRatesMapType = RefreshRateConfigs::AllRefreshRatesMapType;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080032using RefreshRate = RefreshRateConfigs::RefreshRate;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080033
Ady Abraham8a82ba62020-01-17 12:43:17 -080034const RefreshRate& RefreshRateConfigs::getRefreshRateForContent(
35 const std::vector<LayerRequirement>& layers) const {
Ady Abraham2139f732019-11-13 18:56:40 -080036 std::lock_guard lock(mLock);
Ady Abrahamdec1a412020-01-24 10:23:50 -080037 int contentFramerate = 0;
38 int explicitContentFramerate = 0;
Ady Abraham8a82ba62020-01-17 12:43:17 -080039 for (const auto& layer : layers) {
Ady Abrahamdec1a412020-01-24 10:23:50 -080040 const auto desiredRefreshRateRound = round<int>(layer.desiredRefreshRate);
Ady Abraham71c437d2020-01-31 15:56:57 -080041 if (layer.vote == LayerVoteType::ExplicitDefault ||
42 layer.vote == LayerVoteType::ExplicitExactOrMultiple) {
Ady Abrahamdec1a412020-01-24 10:23:50 -080043 if (desiredRefreshRateRound > explicitContentFramerate) {
44 explicitContentFramerate = desiredRefreshRateRound;
Ady Abraham8a82ba62020-01-17 12:43:17 -080045 }
46 } else {
Ady Abrahamdec1a412020-01-24 10:23:50 -080047 if (desiredRefreshRateRound > contentFramerate) {
48 contentFramerate = desiredRefreshRateRound;
Ady Abraham8a82ba62020-01-17 12:43:17 -080049 }
50 }
51 }
52
Ady Abrahamdec1a412020-01-24 10:23:50 -080053 if (explicitContentFramerate != 0) {
Ady Abraham8a82ba62020-01-17 12:43:17 -080054 contentFramerate = explicitContentFramerate;
Ady Abrahamdec1a412020-01-24 10:23:50 -080055 } else if (contentFramerate == 0) {
Ady Abrahamabc27602020-04-08 17:20:29 -070056 contentFramerate = round<int>(mMaxSupportedRefreshRate->getFps());
Ady Abraham8a82ba62020-01-17 12:43:17 -080057 }
Ady Abraham8a82ba62020-01-17 12:43:17 -080058 ATRACE_INT("ContentFPS", contentFramerate);
59
Ady Abraham2139f732019-11-13 18:56:40 -080060 // Find the appropriate refresh rate with minimal error
Steven Thomasf734df42020-04-13 21:09:28 -070061 auto iter = min_element(mPrimaryRefreshRates.cbegin(), mPrimaryRefreshRates.cend(),
Ady Abraham2139f732019-11-13 18:56:40 -080062 [contentFramerate](const auto& lhs, const auto& rhs) -> bool {
63 return std::abs(lhs->fps - contentFramerate) <
64 std::abs(rhs->fps - contentFramerate);
65 });
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080066
Ady Abraham2139f732019-11-13 18:56:40 -080067 // Some content aligns better on higher refresh rate. For example for 45fps we should choose
68 // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't
69 // align well with both
70 const RefreshRate* bestSoFar = *iter;
71 constexpr float MARGIN = 0.05f;
72 float ratio = (*iter)->fps / contentFramerate;
73 if (std::abs(std::round(ratio) - ratio) > MARGIN) {
Steven Thomasf734df42020-04-13 21:09:28 -070074 while (iter != mPrimaryRefreshRates.cend()) {
Ady Abraham2139f732019-11-13 18:56:40 -080075 ratio = (*iter)->fps / contentFramerate;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080076
Ady Abraham2139f732019-11-13 18:56:40 -080077 if (std::abs(std::round(ratio) - ratio) <= MARGIN) {
78 bestSoFar = *iter;
79 break;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080080 }
Ady Abraham2139f732019-11-13 18:56:40 -080081 ++iter;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080082 }
83 }
84
Ady Abraham2139f732019-11-13 18:56:40 -080085 return *bestSoFar;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080086}
87
Ady Abraham4ccdcb42020-02-11 17:34:34 -080088std::pair<nsecs_t, nsecs_t> RefreshRateConfigs::getDisplayFrames(nsecs_t layerPeriod,
89 nsecs_t displayPeriod) const {
90 auto [displayFramesQuot, displayFramesRem] = std::div(layerPeriod, displayPeriod);
91 if (displayFramesRem <= MARGIN_FOR_PERIOD_CALCULATION ||
92 std::abs(displayFramesRem - displayPeriod) <= MARGIN_FOR_PERIOD_CALCULATION) {
93 displayFramesQuot++;
94 displayFramesRem = 0;
95 }
96
97 return {displayFramesQuot, displayFramesRem};
98}
99
Steven Thomasbb374322020-04-28 22:47:16 -0700100const RefreshRate& RefreshRateConfigs::getBestRefreshRate(
101 const std::vector<LayerRequirement>& layers, bool touchActive, bool idle,
Ady Abraham6fb599b2020-03-05 13:48:22 -0800102 bool* touchConsidered) const {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800103 ATRACE_CALL();
104 ALOGV("getRefreshRateForContent %zu layers", layers.size());
105
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000106 *touchConsidered = false;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800107 std::lock_guard lock(mLock);
108
109 int noVoteLayers = 0;
110 int minVoteLayers = 0;
111 int maxVoteLayers = 0;
Ady Abraham71c437d2020-01-31 15:56:57 -0800112 int explicitDefaultVoteLayers = 0;
113 int explicitExactOrMultipleVoteLayers = 0;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800114 float maxExplicitWeight = 0;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800115 for (const auto& layer : layers) {
Ady Abraham6fb599b2020-03-05 13:48:22 -0800116 if (layer.vote == LayerVoteType::NoVote) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800117 noVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800118 } else if (layer.vote == LayerVoteType::Min) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800119 minVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800120 } else if (layer.vote == LayerVoteType::Max) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800121 maxVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800122 } else if (layer.vote == LayerVoteType::ExplicitDefault) {
Ady Abraham71c437d2020-01-31 15:56:57 -0800123 explicitDefaultVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800124 maxExplicitWeight = std::max(maxExplicitWeight, layer.weight);
125 } else if (layer.vote == LayerVoteType::ExplicitExactOrMultiple) {
Ady Abraham71c437d2020-01-31 15:56:57 -0800126 explicitExactOrMultipleVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800127 maxExplicitWeight = std::max(maxExplicitWeight, layer.weight);
128 }
129 }
130
Alec Mouri11232a22020-05-14 18:06:25 -0700131 const bool hasExplicitVoteLayers =
132 explicitDefaultVoteLayers > 0 || explicitExactOrMultipleVoteLayers > 0;
133
Steven Thomasf734df42020-04-13 21:09:28 -0700134 // Consider the touch event if there are no Explicit* layers. Otherwise wait until after we've
135 // selected a refresh rate to see if we should apply touch boost.
Alec Mouri11232a22020-05-14 18:06:25 -0700136 if (touchActive && !hasExplicitVoteLayers) {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000137 *touchConsidered = true;
Steven Thomasf734df42020-04-13 21:09:28 -0700138 return getMaxRefreshRateByPolicyLocked();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800139 }
140
Alec Mouri11232a22020-05-14 18:06:25 -0700141 // If the primary range consists of a single refresh rate then we can only
142 // move out the of range if layers explicitly request a different refresh
143 // rate.
144 const Policy* policy = getCurrentPolicyLocked();
145 const bool primaryRangeIsSingleRate = policy->primaryRange.min == policy->primaryRange.max;
146
147 if (!touchActive && idle && !(primaryRangeIsSingleRate && hasExplicitVoteLayers)) {
Steven Thomasbb374322020-04-28 22:47:16 -0700148 return getMinRefreshRateByPolicyLocked();
149 }
150
Steven Thomasdebafed2020-05-18 17:30:35 -0700151 if (layers.empty() || noVoteLayers == layers.size()) {
152 return getMaxRefreshRateByPolicyLocked();
Steven Thomasbb374322020-04-28 22:47:16 -0700153 }
154
Ady Abraham8a82ba62020-01-17 12:43:17 -0800155 // Only if all layers want Min we should return Min
156 if (noVoteLayers + minVoteLayers == layers.size()) {
Steven Thomasf734df42020-04-13 21:09:28 -0700157 return getMinRefreshRateByPolicyLocked();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800158 }
159
Ady Abraham8a82ba62020-01-17 12:43:17 -0800160 // Find the best refresh rate based on score
161 std::vector<std::pair<const RefreshRate*, float>> scores;
Steven Thomasf734df42020-04-13 21:09:28 -0700162 scores.reserve(mAppRequestRefreshRates.size());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800163
Steven Thomasf734df42020-04-13 21:09:28 -0700164 for (const auto refreshRate : mAppRequestRefreshRates) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800165 scores.emplace_back(refreshRate, 0.0f);
166 }
167
168 for (const auto& layer : layers) {
Ady Abrahamf6b77072020-01-30 14:22:54 -0800169 ALOGV("Calculating score for %s (type: %d)", layer.name.c_str(), layer.vote);
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800170 if (layer.vote == LayerVoteType::NoVote || layer.vote == LayerVoteType::Min) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800171 continue;
172 }
173
Ady Abraham71c437d2020-01-31 15:56:57 -0800174 auto weight = layer.weight;
Ady Abraham71c437d2020-01-31 15:56:57 -0800175
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800176 for (auto i = 0u; i < scores.size(); i++) {
Steven Thomasf734df42020-04-13 21:09:28 -0700177 bool inPrimaryRange =
178 scores[i].first->inPolicy(policy->primaryRange.min, policy->primaryRange.max);
Alec Mouri11232a22020-05-14 18:06:25 -0700179 if ((primaryRangeIsSingleRate || !inPrimaryRange) &&
180 layer.vote != LayerVoteType::ExplicitDefault &&
Steven Thomasf734df42020-04-13 21:09:28 -0700181 layer.vote != LayerVoteType::ExplicitExactOrMultiple) {
182 // Only layers with explicit frame rate settings are allowed to score refresh rates
183 // outside the primary range.
184 continue;
185 }
186
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800187 // If the layer wants Max, give higher score to the higher refresh rate
188 if (layer.vote == LayerVoteType::Max) {
189 const auto ratio = scores[i].first->fps / scores.back().first->fps;
190 // use ratio^2 to get a lower score the more we get further from peak
191 const auto layerScore = ratio * ratio;
192 ALOGV("%s (Max, weight %.2f) gives %s score of %.2f", layer.name.c_str(), weight,
193 scores[i].first->name.c_str(), layerScore);
194 scores[i].second += weight * layerScore;
195 continue;
Ady Abraham71c437d2020-01-31 15:56:57 -0800196 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800197
Ady Abrahamabc27602020-04-08 17:20:29 -0700198 const auto displayPeriod = scores[i].first->hwcConfig->getVsyncPeriod();
Ady Abrahamdec1a412020-01-24 10:23:50 -0800199 const auto layerPeriod = round<nsecs_t>(1e9f / layer.desiredRefreshRate);
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800200 if (layer.vote == LayerVoteType::ExplicitDefault) {
201 const auto layerScore = [&]() {
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800202 // Find the actual rate the layer will render, assuming
203 // that layerPeriod is the minimal time to render a frame
204 auto actualLayerPeriod = displayPeriod;
205 int multiplier = 1;
206 while (layerPeriod > actualLayerPeriod + MARGIN_FOR_PERIOD_CALCULATION) {
207 multiplier++;
208 actualLayerPeriod = displayPeriod * multiplier;
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800209 }
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800210 return std::min(1.0f,
211 static_cast<float>(layerPeriod) /
212 static_cast<float>(actualLayerPeriod));
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800213 }();
214
215 ALOGV("%s (ExplicitDefault, weight %.2f) %.2fHz gives %s score of %.2f",
216 layer.name.c_str(), weight, 1e9f / layerPeriod, scores[i].first->name.c_str(),
217 layerScore);
218 scores[i].second += weight * layerScore;
219 continue;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800220 }
221
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800222 if (layer.vote == LayerVoteType::ExplicitExactOrMultiple ||
223 layer.vote == LayerVoteType::Heuristic) {
224 const auto layerScore = [&]() {
225 // Calculate how many display vsyncs we need to present a single frame for this
226 // layer
227 const auto [displayFramesQuot, displayFramesRem] =
228 getDisplayFrames(layerPeriod, displayPeriod);
229 static constexpr size_t MAX_FRAMES_TO_FIT =
230 10; // Stop calculating when score < 0.1
231 if (displayFramesRem == 0) {
232 // Layer desired refresh rate matches the display rate.
233 return 1.0f;
234 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800235
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800236 if (displayFramesQuot == 0) {
237 // Layer desired refresh rate is higher the display rate.
238 return (static_cast<float>(layerPeriod) /
239 static_cast<float>(displayPeriod)) *
240 (1.0f / (MAX_FRAMES_TO_FIT + 1));
241 }
242
243 // Layer desired refresh rate is lower the display rate. Check how well it fits
244 // the cadence
245 auto diff = std::abs(displayFramesRem - (displayPeriod - displayFramesRem));
246 int iter = 2;
247 while (diff > MARGIN_FOR_PERIOD_CALCULATION && iter < MAX_FRAMES_TO_FIT) {
248 diff = diff - (displayPeriod - diff);
249 iter++;
250 }
251
252 return 1.0f / iter;
253 }();
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000254 ALOGV("%s (ExplicitExactOrMultiple, weight %.2f) %.2fHz gives %s score of %.2f",
255 layer.name.c_str(), weight, 1e9f / layerPeriod, scores[i].first->name.c_str(),
256 layerScore);
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800257 scores[i].second += weight * layerScore;
258 continue;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800259 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800260 }
261 }
262
Ady Abraham34702102020-02-10 14:12:05 -0800263 // Now that we scored all the refresh rates we need to pick the one that got the highest score.
264 // In case of a tie we will pick the higher refresh rate if any of the layers wanted Max,
265 // or the lower otherwise.
266 const RefreshRate* bestRefreshRate = maxVoteLayers > 0
267 ? getBestRefreshRate(scores.rbegin(), scores.rend())
268 : getBestRefreshRate(scores.begin(), scores.end());
269
Alec Mouri11232a22020-05-14 18:06:25 -0700270 if (primaryRangeIsSingleRate) {
271 // If we never scored any layers, then choose the rate from the primary
272 // range instead of picking a random score from the app range.
273 if (std::all_of(scores.begin(), scores.end(),
274 [](std::pair<const RefreshRate*, float> p) { return p.second == 0; })) {
275 return getMaxRefreshRateByPolicyLocked();
276 } else {
277 return *bestRefreshRate;
278 }
279 }
280
Steven Thomasf734df42020-04-13 21:09:28 -0700281 // Consider the touch event if there are no ExplicitDefault layers. ExplicitDefault are mostly
282 // interactive (as opposed to ExplicitExactOrMultiple) and therefore if those posted an explicit
283 // vote we should not change it if we get a touch event. Only apply touch boost if it will
284 // actually increase the refresh rate over the normal selection.
285 const RefreshRate& touchRefreshRate = getMaxRefreshRateByPolicyLocked();
Alec Mouri11232a22020-05-14 18:06:25 -0700286
Steven Thomasf734df42020-04-13 21:09:28 -0700287 if (touchActive && explicitDefaultVoteLayers == 0 &&
288 bestRefreshRate->fps < touchRefreshRate.fps) {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000289 *touchConsidered = true;
Steven Thomasf734df42020-04-13 21:09:28 -0700290 return touchRefreshRate;
291 }
292
Ady Abrahamde7156e2020-02-28 17:29:39 -0800293 return *bestRefreshRate;
Ady Abraham34702102020-02-10 14:12:05 -0800294}
295
296template <typename Iter>
297const RefreshRate* RefreshRateConfigs::getBestRefreshRate(Iter begin, Iter end) const {
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800298 constexpr auto EPSILON = 0.001f;
Ady Abrahamde7156e2020-02-28 17:29:39 -0800299 const RefreshRate* bestRefreshRate = begin->first;
300 float max = begin->second;
Ady Abraham34702102020-02-10 14:12:05 -0800301 for (auto i = begin; i != end; ++i) {
302 const auto [refreshRate, score] = *i;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800303 ALOGV("%s scores %.2f", refreshRate->name.c_str(), score);
304
Ady Abrahamdec1a412020-01-24 10:23:50 -0800305 ATRACE_INT(refreshRate->name.c_str(), round<int>(score * 100));
Ady Abraham8a82ba62020-01-17 12:43:17 -0800306
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800307 if (score > max * (1 + EPSILON)) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800308 max = score;
309 bestRefreshRate = refreshRate;
310 }
311 }
312
Ady Abraham34702102020-02-10 14:12:05 -0800313 return bestRefreshRate;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800314}
315
Ady Abraham2139f732019-11-13 18:56:40 -0800316const AllRefreshRatesMapType& RefreshRateConfigs::getAllRefreshRates() const {
317 return mRefreshRates;
318}
319
320const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicy() const {
321 std::lock_guard lock(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700322 return getMinRefreshRateByPolicyLocked();
323}
324
325const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicyLocked() const {
326 return *mPrimaryRefreshRates.front();
Ady Abraham2139f732019-11-13 18:56:40 -0800327}
328
329const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicy() const {
330 std::lock_guard lock(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700331 return getMaxRefreshRateByPolicyLocked();
332}
333
334const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicyLocked() const {
335 return *mPrimaryRefreshRates.back();
Ady Abraham2139f732019-11-13 18:56:40 -0800336}
337
338const RefreshRate& RefreshRateConfigs::getCurrentRefreshRate() const {
339 std::lock_guard lock(mLock);
340 return *mCurrentRefreshRate;
341}
342
Ana Krulec5d477912020-02-07 12:02:38 -0800343const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicy() const {
344 std::lock_guard lock(mLock);
Ana Krulec3d367c82020-02-25 15:02:01 -0800345 return getCurrentRefreshRateByPolicyLocked();
346}
347
348const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicyLocked() const {
Steven Thomasf734df42020-04-13 21:09:28 -0700349 if (std::find(mAppRequestRefreshRates.begin(), mAppRequestRefreshRates.end(),
350 mCurrentRefreshRate) != mAppRequestRefreshRates.end()) {
Ana Krulec5d477912020-02-07 12:02:38 -0800351 return *mCurrentRefreshRate;
352 }
Steven Thomasd4071902020-03-24 16:02:53 -0700353 return *mRefreshRates.at(getCurrentPolicyLocked()->defaultConfig);
Ana Krulec5d477912020-02-07 12:02:38 -0800354}
355
Ady Abraham2139f732019-11-13 18:56:40 -0800356void RefreshRateConfigs::setCurrentConfigId(HwcConfigIndexType configId) {
357 std::lock_guard lock(mLock);
Ady Abraham2e1dd892020-03-05 13:48:36 -0800358 mCurrentRefreshRate = mRefreshRates.at(configId).get();
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800359}
360
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800361RefreshRateConfigs::RefreshRateConfigs(
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800362 const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs,
Ana Krulec3f6a2062020-01-23 15:48:01 -0800363 HwcConfigIndexType currentConfigId) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700364 LOG_ALWAYS_FATAL_IF(configs.empty());
365 LOG_ALWAYS_FATAL_IF(currentConfigId.value() >= configs.size());
366
367 for (auto configId = HwcConfigIndexType(0); configId.value() < configs.size(); configId++) {
368 const auto& config = configs.at(static_cast<size_t>(configId.value()));
369 const float fps = 1e9f / config->getVsyncPeriod();
370 mRefreshRates.emplace(configId,
371 std::make_unique<RefreshRate>(configId, config,
Steven Thomasf734df42020-04-13 21:09:28 -0700372 base::StringPrintf("%.0ffps", fps), fps,
Ady Abrahamabc27602020-04-08 17:20:29 -0700373 RefreshRate::ConstructorTag(0)));
374 if (configId == currentConfigId) {
375 mCurrentRefreshRate = mRefreshRates.at(configId).get();
376 }
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800377 }
Ady Abrahamabc27602020-04-08 17:20:29 -0700378
379 std::vector<const RefreshRate*> sortedConfigs;
380 getSortedRefreshRateList([](const RefreshRate&) { return true; }, &sortedConfigs);
381 mDisplayManagerPolicy.defaultConfig = currentConfigId;
382 mMinSupportedRefreshRate = sortedConfigs.front();
383 mMaxSupportedRefreshRate = sortedConfigs.back();
384 constructAvailableRefreshRates();
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800385}
386
Steven Thomasd4071902020-03-24 16:02:53 -0700387bool RefreshRateConfigs::isPolicyValid(const Policy& policy) {
388 // defaultConfig must be a valid config, and within the given refresh rate range.
389 auto iter = mRefreshRates.find(policy.defaultConfig);
390 if (iter == mRefreshRates.end()) {
391 return false;
392 }
393 const RefreshRate& refreshRate = *iter->second;
Steven Thomasf734df42020-04-13 21:09:28 -0700394 if (!refreshRate.inPolicy(policy.primaryRange.min, policy.primaryRange.max)) {
Steven Thomasd4071902020-03-24 16:02:53 -0700395 return false;
396 }
Steven Thomasf734df42020-04-13 21:09:28 -0700397 return policy.appRequestRange.min <= policy.primaryRange.min &&
398 policy.appRequestRange.max >= policy.primaryRange.max;
Steven Thomasd4071902020-03-24 16:02:53 -0700399}
400
401status_t RefreshRateConfigs::setDisplayManagerPolicy(const Policy& policy) {
Ady Abraham2139f732019-11-13 18:56:40 -0800402 std::lock_guard lock(mLock);
Steven Thomasd4071902020-03-24 16:02:53 -0700403 if (!isPolicyValid(policy)) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100404 return BAD_VALUE;
405 }
Steven Thomasd4071902020-03-24 16:02:53 -0700406 Policy previousPolicy = *getCurrentPolicyLocked();
407 mDisplayManagerPolicy = policy;
408 if (*getCurrentPolicyLocked() == previousPolicy) {
409 return CURRENT_POLICY_UNCHANGED;
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100410 }
Ady Abraham2139f732019-11-13 18:56:40 -0800411 constructAvailableRefreshRates();
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100412 return NO_ERROR;
413}
414
Steven Thomasd4071902020-03-24 16:02:53 -0700415status_t RefreshRateConfigs::setOverridePolicy(const std::optional<Policy>& policy) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100416 std::lock_guard lock(mLock);
Steven Thomasd4071902020-03-24 16:02:53 -0700417 if (policy && !isPolicyValid(*policy)) {
418 return BAD_VALUE;
419 }
420 Policy previousPolicy = *getCurrentPolicyLocked();
421 mOverridePolicy = policy;
422 if (*getCurrentPolicyLocked() == previousPolicy) {
423 return CURRENT_POLICY_UNCHANGED;
424 }
425 constructAvailableRefreshRates();
426 return NO_ERROR;
427}
428
429const RefreshRateConfigs::Policy* RefreshRateConfigs::getCurrentPolicyLocked() const {
430 return mOverridePolicy ? &mOverridePolicy.value() : &mDisplayManagerPolicy;
431}
432
433RefreshRateConfigs::Policy RefreshRateConfigs::getCurrentPolicy() const {
434 std::lock_guard lock(mLock);
435 return *getCurrentPolicyLocked();
436}
437
438RefreshRateConfigs::Policy RefreshRateConfigs::getDisplayManagerPolicy() const {
439 std::lock_guard lock(mLock);
440 return mDisplayManagerPolicy;
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100441}
442
443bool RefreshRateConfigs::isConfigAllowed(HwcConfigIndexType config) const {
444 std::lock_guard lock(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700445 for (const RefreshRate* refreshRate : mAppRequestRefreshRates) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100446 if (refreshRate->configId == config) {
447 return true;
448 }
449 }
450 return false;
Ady Abraham2139f732019-11-13 18:56:40 -0800451}
452
453void RefreshRateConfigs::getSortedRefreshRateList(
454 const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate,
455 std::vector<const RefreshRate*>* outRefreshRates) {
456 outRefreshRates->clear();
457 outRefreshRates->reserve(mRefreshRates.size());
458 for (const auto& [type, refreshRate] : mRefreshRates) {
Ady Abraham2e1dd892020-03-05 13:48:36 -0800459 if (shouldAddRefreshRate(*refreshRate)) {
Ady Abraham2139f732019-11-13 18:56:40 -0800460 ALOGV("getSortedRefreshRateList: config %d added to list policy",
Ady Abraham2e1dd892020-03-05 13:48:36 -0800461 refreshRate->configId.value());
462 outRefreshRates->push_back(refreshRate.get());
Ady Abraham2139f732019-11-13 18:56:40 -0800463 }
464 }
465
466 std::sort(outRefreshRates->begin(), outRefreshRates->end(),
467 [](const auto refreshRate1, const auto refreshRate2) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700468 if (refreshRate1->hwcConfig->getVsyncPeriod() !=
469 refreshRate2->hwcConfig->getVsyncPeriod()) {
470 return refreshRate1->hwcConfig->getVsyncPeriod() >
471 refreshRate2->hwcConfig->getVsyncPeriod();
Steven Thomasd4071902020-03-24 16:02:53 -0700472 } else {
Ady Abrahamabc27602020-04-08 17:20:29 -0700473 return refreshRate1->hwcConfig->getConfigGroup() >
474 refreshRate2->hwcConfig->getConfigGroup();
Steven Thomasd4071902020-03-24 16:02:53 -0700475 }
Ady Abraham2139f732019-11-13 18:56:40 -0800476 });
477}
478
479void RefreshRateConfigs::constructAvailableRefreshRates() {
480 // Filter configs based on current policy and sort based on vsync period
Steven Thomasd4071902020-03-24 16:02:53 -0700481 const Policy* policy = getCurrentPolicyLocked();
Ady Abrahamabc27602020-04-08 17:20:29 -0700482 const auto& defaultConfig = mRefreshRates.at(policy->defaultConfig)->hwcConfig;
Steven Thomasf734df42020-04-13 21:09:28 -0700483 ALOGV("constructAvailableRefreshRates: default %d group %d primaryRange=[%.2f %.2f]"
484 " appRequestRange=[%.2f %.2f]",
485 policy->defaultConfig.value(), defaultConfig->getConfigGroup(), policy->primaryRange.min,
486 policy->primaryRange.max, policy->appRequestRange.min, policy->appRequestRange.max);
Ady Abrahamabc27602020-04-08 17:20:29 -0700487
Steven Thomasf734df42020-04-13 21:09:28 -0700488 auto filterRefreshRates = [&](float min, float max, const char* listName,
489 std::vector<const RefreshRate*>* outRefreshRates) {
490 getSortedRefreshRateList(
491 [&](const RefreshRate& refreshRate) REQUIRES(mLock) {
492 const auto& hwcConfig = refreshRate.hwcConfig;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800493
Steven Thomasf734df42020-04-13 21:09:28 -0700494 return hwcConfig->getHeight() == defaultConfig->getHeight() &&
495 hwcConfig->getWidth() == defaultConfig->getWidth() &&
496 hwcConfig->getDpiX() == defaultConfig->getDpiX() &&
497 hwcConfig->getDpiY() == defaultConfig->getDpiY() &&
498 (policy->allowGroupSwitching ||
499 hwcConfig->getConfigGroup() == defaultConfig->getConfigGroup()) &&
500 refreshRate.inPolicy(min, max);
501 },
502 outRefreshRates);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800503
Steven Thomasf734df42020-04-13 21:09:28 -0700504 LOG_ALWAYS_FATAL_IF(outRefreshRates->empty(),
505 "No matching configs for %s range: min=%.0f max=%.0f", listName, min,
506 max);
507 auto stringifyRefreshRates = [&]() -> std::string {
508 std::string str;
509 for (auto refreshRate : *outRefreshRates) {
510 base::StringAppendF(&str, "%s ", refreshRate->name.c_str());
511 }
512 return str;
513 };
514 ALOGV("%s refresh rates: %s", listName, stringifyRefreshRates().c_str());
515 };
516
517 filterRefreshRates(policy->primaryRange.min, policy->primaryRange.max, "primary",
518 &mPrimaryRefreshRates);
519 filterRefreshRates(policy->appRequestRange.min, policy->appRequestRange.max, "app request",
520 &mAppRequestRefreshRates);
Ady Abraham2139f732019-11-13 18:56:40 -0800521}
522
Ady Abraham2139f732019-11-13 18:56:40 -0800523} // namespace android::scheduler