blob: 4a4f9c81112cceb0589a6268699964b753ade362 [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 Abraham1adbb722020-05-15 11:51:48 -0700106 if (touchConsidered) *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
Steven Thomasf734df42020-04-13 21:09:28 -0700131 // Consider the touch event if there are no Explicit* layers. Otherwise wait until after we've
132 // selected a refresh rate to see if we should apply touch boost.
133 if (touchActive && explicitDefaultVoteLayers == 0 && explicitExactOrMultipleVoteLayers == 0) {
Ady Abraham1adbb722020-05-15 11:51:48 -0700134 ALOGV("TouchBoost - choose %s", getMaxRefreshRateByPolicyLocked().getName().c_str());
135 if (touchConsidered) *touchConsidered = true;
Steven Thomasf734df42020-04-13 21:09:28 -0700136 return getMaxRefreshRateByPolicyLocked();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800137 }
138
Steven Thomasbb374322020-04-28 22:47:16 -0700139 if (!touchActive && idle) {
140 return getMinRefreshRateByPolicyLocked();
141 }
142
Steven Thomasdebafed2020-05-18 17:30:35 -0700143 if (layers.empty() || noVoteLayers == layers.size()) {
144 return getMaxRefreshRateByPolicyLocked();
Steven Thomasbb374322020-04-28 22:47:16 -0700145 }
146
Ady Abraham8a82ba62020-01-17 12:43:17 -0800147 // Only if all layers want Min we should return Min
148 if (noVoteLayers + minVoteLayers == layers.size()) {
Ady Abraham1adbb722020-05-15 11:51:48 -0700149 ALOGV("all layers Min - choose %s", getMinRefreshRateByPolicyLocked().getName().c_str());
Steven Thomasf734df42020-04-13 21:09:28 -0700150 return getMinRefreshRateByPolicyLocked();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800151 }
152
Steven Thomasf734df42020-04-13 21:09:28 -0700153 const Policy* policy = getCurrentPolicyLocked();
154
Ady Abraham8a82ba62020-01-17 12:43:17 -0800155 // Find the best refresh rate based on score
156 std::vector<std::pair<const RefreshRate*, float>> scores;
Steven Thomasf734df42020-04-13 21:09:28 -0700157 scores.reserve(mAppRequestRefreshRates.size());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800158
Steven Thomasf734df42020-04-13 21:09:28 -0700159 for (const auto refreshRate : mAppRequestRefreshRates) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800160 scores.emplace_back(refreshRate, 0.0f);
161 }
162
163 for (const auto& layer : layers) {
Ady Abrahamf6b77072020-01-30 14:22:54 -0800164 ALOGV("Calculating score for %s (type: %d)", layer.name.c_str(), layer.vote);
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800165 if (layer.vote == LayerVoteType::NoVote || layer.vote == LayerVoteType::Min) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800166 continue;
167 }
168
Ady Abraham71c437d2020-01-31 15:56:57 -0800169 auto weight = layer.weight;
Ady Abraham71c437d2020-01-31 15:56:57 -0800170
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800171 for (auto i = 0u; i < scores.size(); i++) {
Steven Thomasf734df42020-04-13 21:09:28 -0700172 bool inPrimaryRange =
173 scores[i].first->inPolicy(policy->primaryRange.min, policy->primaryRange.max);
174 if (!inPrimaryRange && layer.vote != LayerVoteType::ExplicitDefault &&
175 layer.vote != LayerVoteType::ExplicitExactOrMultiple) {
176 // Only layers with explicit frame rate settings are allowed to score refresh rates
177 // outside the primary range.
178 continue;
179 }
180
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800181 // If the layer wants Max, give higher score to the higher refresh rate
182 if (layer.vote == LayerVoteType::Max) {
183 const auto ratio = scores[i].first->fps / scores.back().first->fps;
184 // use ratio^2 to get a lower score the more we get further from peak
185 const auto layerScore = ratio * ratio;
186 ALOGV("%s (Max, weight %.2f) gives %s score of %.2f", layer.name.c_str(), weight,
187 scores[i].first->name.c_str(), layerScore);
188 scores[i].second += weight * layerScore;
189 continue;
Ady Abraham71c437d2020-01-31 15:56:57 -0800190 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800191
Ady Abrahamabc27602020-04-08 17:20:29 -0700192 const auto displayPeriod = scores[i].first->hwcConfig->getVsyncPeriod();
Ady Abrahamdec1a412020-01-24 10:23:50 -0800193 const auto layerPeriod = round<nsecs_t>(1e9f / layer.desiredRefreshRate);
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800194 if (layer.vote == LayerVoteType::ExplicitDefault) {
195 const auto layerScore = [&]() {
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800196 // Find the actual rate the layer will render, assuming
197 // that layerPeriod is the minimal time to render a frame
198 auto actualLayerPeriod = displayPeriod;
199 int multiplier = 1;
200 while (layerPeriod > actualLayerPeriod + MARGIN_FOR_PERIOD_CALCULATION) {
201 multiplier++;
202 actualLayerPeriod = displayPeriod * multiplier;
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800203 }
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800204 return std::min(1.0f,
205 static_cast<float>(layerPeriod) /
206 static_cast<float>(actualLayerPeriod));
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800207 }();
208
209 ALOGV("%s (ExplicitDefault, weight %.2f) %.2fHz gives %s score of %.2f",
210 layer.name.c_str(), weight, 1e9f / layerPeriod, scores[i].first->name.c_str(),
211 layerScore);
212 scores[i].second += weight * layerScore;
213 continue;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800214 }
215
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800216 if (layer.vote == LayerVoteType::ExplicitExactOrMultiple ||
217 layer.vote == LayerVoteType::Heuristic) {
218 const auto layerScore = [&]() {
219 // Calculate how many display vsyncs we need to present a single frame for this
220 // layer
221 const auto [displayFramesQuot, displayFramesRem] =
222 getDisplayFrames(layerPeriod, displayPeriod);
223 static constexpr size_t MAX_FRAMES_TO_FIT =
224 10; // Stop calculating when score < 0.1
225 if (displayFramesRem == 0) {
226 // Layer desired refresh rate matches the display rate.
227 return 1.0f;
228 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800229
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800230 if (displayFramesQuot == 0) {
231 // Layer desired refresh rate is higher the display rate.
232 return (static_cast<float>(layerPeriod) /
233 static_cast<float>(displayPeriod)) *
234 (1.0f / (MAX_FRAMES_TO_FIT + 1));
235 }
236
237 // Layer desired refresh rate is lower the display rate. Check how well it fits
238 // the cadence
239 auto diff = std::abs(displayFramesRem - (displayPeriod - displayFramesRem));
240 int iter = 2;
241 while (diff > MARGIN_FOR_PERIOD_CALCULATION && iter < MAX_FRAMES_TO_FIT) {
242 diff = diff - (displayPeriod - diff);
243 iter++;
244 }
245
246 return 1.0f / iter;
247 }();
Ady Abraham1adbb722020-05-15 11:51:48 -0700248 ALOGV("%s (%s, weight %.2f) %.2fHz gives %s score of %.2f", layer.name.c_str(),
249 layer.vote == LayerVoteType::ExplicitExactOrMultiple
250 ? "ExplicitExactOrMultiple"
251 : "Heuristic",
252 weight, 1e9f / layerPeriod, scores[i].first->name.c_str(), layerScore);
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800253 scores[i].second += weight * layerScore;
254 continue;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800255 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800256 }
257 }
258
Ady Abraham34702102020-02-10 14:12:05 -0800259 // Now that we scored all the refresh rates we need to pick the one that got the highest score.
260 // In case of a tie we will pick the higher refresh rate if any of the layers wanted Max,
261 // or the lower otherwise.
262 const RefreshRate* bestRefreshRate = maxVoteLayers > 0
263 ? getBestRefreshRate(scores.rbegin(), scores.rend())
264 : getBestRefreshRate(scores.begin(), scores.end());
265
Steven Thomasf734df42020-04-13 21:09:28 -0700266 // Consider the touch event if there are no ExplicitDefault layers. ExplicitDefault are mostly
267 // interactive (as opposed to ExplicitExactOrMultiple) and therefore if those posted an explicit
268 // vote we should not change it if we get a touch event. Only apply touch boost if it will
269 // actually increase the refresh rate over the normal selection.
270 const RefreshRate& touchRefreshRate = getMaxRefreshRateByPolicyLocked();
271 if (touchActive && explicitDefaultVoteLayers == 0 &&
272 bestRefreshRate->fps < touchRefreshRate.fps) {
Ady Abraham1adbb722020-05-15 11:51:48 -0700273 if (touchConsidered) *touchConsidered = true;
274 ALOGV("TouchBoost - choose %s", touchRefreshRate.getName().c_str());
Steven Thomasf734df42020-04-13 21:09:28 -0700275 return touchRefreshRate;
276 }
277
Ady Abrahamde7156e2020-02-28 17:29:39 -0800278 return *bestRefreshRate;
Ady Abraham34702102020-02-10 14:12:05 -0800279}
280
281template <typename Iter>
282const RefreshRate* RefreshRateConfigs::getBestRefreshRate(Iter begin, Iter end) const {
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800283 constexpr auto EPSILON = 0.001f;
Ady Abrahamde7156e2020-02-28 17:29:39 -0800284 const RefreshRate* bestRefreshRate = begin->first;
285 float max = begin->second;
Ady Abraham34702102020-02-10 14:12:05 -0800286 for (auto i = begin; i != end; ++i) {
287 const auto [refreshRate, score] = *i;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800288 ALOGV("%s scores %.2f", refreshRate->name.c_str(), score);
289
Ady Abrahamdec1a412020-01-24 10:23:50 -0800290 ATRACE_INT(refreshRate->name.c_str(), round<int>(score * 100));
Ady Abraham8a82ba62020-01-17 12:43:17 -0800291
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800292 if (score > max * (1 + EPSILON)) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800293 max = score;
294 bestRefreshRate = refreshRate;
295 }
296 }
297
Ady Abraham34702102020-02-10 14:12:05 -0800298 return bestRefreshRate;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800299}
300
Ady Abraham2139f732019-11-13 18:56:40 -0800301const AllRefreshRatesMapType& RefreshRateConfigs::getAllRefreshRates() const {
302 return mRefreshRates;
303}
304
305const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicy() const {
306 std::lock_guard lock(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700307 return getMinRefreshRateByPolicyLocked();
308}
309
310const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicyLocked() const {
311 return *mPrimaryRefreshRates.front();
Ady Abraham2139f732019-11-13 18:56:40 -0800312}
313
314const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicy() const {
315 std::lock_guard lock(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700316 return getMaxRefreshRateByPolicyLocked();
317}
318
319const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicyLocked() const {
320 return *mPrimaryRefreshRates.back();
Ady Abraham2139f732019-11-13 18:56:40 -0800321}
322
323const RefreshRate& RefreshRateConfigs::getCurrentRefreshRate() const {
324 std::lock_guard lock(mLock);
325 return *mCurrentRefreshRate;
326}
327
Ana Krulec5d477912020-02-07 12:02:38 -0800328const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicy() const {
329 std::lock_guard lock(mLock);
Ana Krulec3d367c82020-02-25 15:02:01 -0800330 return getCurrentRefreshRateByPolicyLocked();
331}
332
333const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicyLocked() const {
Steven Thomasf734df42020-04-13 21:09:28 -0700334 if (std::find(mAppRequestRefreshRates.begin(), mAppRequestRefreshRates.end(),
335 mCurrentRefreshRate) != mAppRequestRefreshRates.end()) {
Ana Krulec5d477912020-02-07 12:02:38 -0800336 return *mCurrentRefreshRate;
337 }
Steven Thomasd4071902020-03-24 16:02:53 -0700338 return *mRefreshRates.at(getCurrentPolicyLocked()->defaultConfig);
Ana Krulec5d477912020-02-07 12:02:38 -0800339}
340
Ady Abraham2139f732019-11-13 18:56:40 -0800341void RefreshRateConfigs::setCurrentConfigId(HwcConfigIndexType configId) {
342 std::lock_guard lock(mLock);
Ady Abraham2e1dd892020-03-05 13:48:36 -0800343 mCurrentRefreshRate = mRefreshRates.at(configId).get();
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800344}
345
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800346RefreshRateConfigs::RefreshRateConfigs(
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800347 const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs,
Ana Krulec3f6a2062020-01-23 15:48:01 -0800348 HwcConfigIndexType currentConfigId) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700349 LOG_ALWAYS_FATAL_IF(configs.empty());
350 LOG_ALWAYS_FATAL_IF(currentConfigId.value() >= configs.size());
351
352 for (auto configId = HwcConfigIndexType(0); configId.value() < configs.size(); configId++) {
353 const auto& config = configs.at(static_cast<size_t>(configId.value()));
354 const float fps = 1e9f / config->getVsyncPeriod();
355 mRefreshRates.emplace(configId,
356 std::make_unique<RefreshRate>(configId, config,
Steven Thomasf734df42020-04-13 21:09:28 -0700357 base::StringPrintf("%.0ffps", fps), fps,
Ady Abrahamabc27602020-04-08 17:20:29 -0700358 RefreshRate::ConstructorTag(0)));
359 if (configId == currentConfigId) {
360 mCurrentRefreshRate = mRefreshRates.at(configId).get();
361 }
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800362 }
Ady Abrahamabc27602020-04-08 17:20:29 -0700363
364 std::vector<const RefreshRate*> sortedConfigs;
365 getSortedRefreshRateList([](const RefreshRate&) { return true; }, &sortedConfigs);
366 mDisplayManagerPolicy.defaultConfig = currentConfigId;
367 mMinSupportedRefreshRate = sortedConfigs.front();
368 mMaxSupportedRefreshRate = sortedConfigs.back();
369 constructAvailableRefreshRates();
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800370}
371
Steven Thomasd4071902020-03-24 16:02:53 -0700372bool RefreshRateConfigs::isPolicyValid(const Policy& policy) {
373 // defaultConfig must be a valid config, and within the given refresh rate range.
374 auto iter = mRefreshRates.find(policy.defaultConfig);
375 if (iter == mRefreshRates.end()) {
376 return false;
377 }
378 const RefreshRate& refreshRate = *iter->second;
Steven Thomasf734df42020-04-13 21:09:28 -0700379 if (!refreshRate.inPolicy(policy.primaryRange.min, policy.primaryRange.max)) {
Steven Thomasd4071902020-03-24 16:02:53 -0700380 return false;
381 }
Steven Thomasf734df42020-04-13 21:09:28 -0700382 return policy.appRequestRange.min <= policy.primaryRange.min &&
383 policy.appRequestRange.max >= policy.primaryRange.max;
Steven Thomasd4071902020-03-24 16:02:53 -0700384}
385
386status_t RefreshRateConfigs::setDisplayManagerPolicy(const Policy& policy) {
Ady Abraham2139f732019-11-13 18:56:40 -0800387 std::lock_guard lock(mLock);
Steven Thomasd4071902020-03-24 16:02:53 -0700388 if (!isPolicyValid(policy)) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100389 return BAD_VALUE;
390 }
Steven Thomasd4071902020-03-24 16:02:53 -0700391 Policy previousPolicy = *getCurrentPolicyLocked();
392 mDisplayManagerPolicy = policy;
393 if (*getCurrentPolicyLocked() == previousPolicy) {
394 return CURRENT_POLICY_UNCHANGED;
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100395 }
Ady Abraham2139f732019-11-13 18:56:40 -0800396 constructAvailableRefreshRates();
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100397 return NO_ERROR;
398}
399
Steven Thomasd4071902020-03-24 16:02:53 -0700400status_t RefreshRateConfigs::setOverridePolicy(const std::optional<Policy>& policy) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100401 std::lock_guard lock(mLock);
Steven Thomasd4071902020-03-24 16:02:53 -0700402 if (policy && !isPolicyValid(*policy)) {
403 return BAD_VALUE;
404 }
405 Policy previousPolicy = *getCurrentPolicyLocked();
406 mOverridePolicy = policy;
407 if (*getCurrentPolicyLocked() == previousPolicy) {
408 return CURRENT_POLICY_UNCHANGED;
409 }
410 constructAvailableRefreshRates();
411 return NO_ERROR;
412}
413
414const RefreshRateConfigs::Policy* RefreshRateConfigs::getCurrentPolicyLocked() const {
415 return mOverridePolicy ? &mOverridePolicy.value() : &mDisplayManagerPolicy;
416}
417
418RefreshRateConfigs::Policy RefreshRateConfigs::getCurrentPolicy() const {
419 std::lock_guard lock(mLock);
420 return *getCurrentPolicyLocked();
421}
422
423RefreshRateConfigs::Policy RefreshRateConfigs::getDisplayManagerPolicy() const {
424 std::lock_guard lock(mLock);
425 return mDisplayManagerPolicy;
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100426}
427
428bool RefreshRateConfigs::isConfigAllowed(HwcConfigIndexType config) const {
429 std::lock_guard lock(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700430 for (const RefreshRate* refreshRate : mAppRequestRefreshRates) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100431 if (refreshRate->configId == config) {
432 return true;
433 }
434 }
435 return false;
Ady Abraham2139f732019-11-13 18:56:40 -0800436}
437
438void RefreshRateConfigs::getSortedRefreshRateList(
439 const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate,
440 std::vector<const RefreshRate*>* outRefreshRates) {
441 outRefreshRates->clear();
442 outRefreshRates->reserve(mRefreshRates.size());
443 for (const auto& [type, refreshRate] : mRefreshRates) {
Ady Abraham2e1dd892020-03-05 13:48:36 -0800444 if (shouldAddRefreshRate(*refreshRate)) {
Ady Abraham2139f732019-11-13 18:56:40 -0800445 ALOGV("getSortedRefreshRateList: config %d added to list policy",
Ady Abraham2e1dd892020-03-05 13:48:36 -0800446 refreshRate->configId.value());
447 outRefreshRates->push_back(refreshRate.get());
Ady Abraham2139f732019-11-13 18:56:40 -0800448 }
449 }
450
451 std::sort(outRefreshRates->begin(), outRefreshRates->end(),
452 [](const auto refreshRate1, const auto refreshRate2) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700453 if (refreshRate1->hwcConfig->getVsyncPeriod() !=
454 refreshRate2->hwcConfig->getVsyncPeriod()) {
455 return refreshRate1->hwcConfig->getVsyncPeriod() >
456 refreshRate2->hwcConfig->getVsyncPeriod();
Steven Thomasd4071902020-03-24 16:02:53 -0700457 } else {
Ady Abrahamabc27602020-04-08 17:20:29 -0700458 return refreshRate1->hwcConfig->getConfigGroup() >
459 refreshRate2->hwcConfig->getConfigGroup();
Steven Thomasd4071902020-03-24 16:02:53 -0700460 }
Ady Abraham2139f732019-11-13 18:56:40 -0800461 });
462}
463
464void RefreshRateConfigs::constructAvailableRefreshRates() {
465 // Filter configs based on current policy and sort based on vsync period
Steven Thomasd4071902020-03-24 16:02:53 -0700466 const Policy* policy = getCurrentPolicyLocked();
Ady Abrahamabc27602020-04-08 17:20:29 -0700467 const auto& defaultConfig = mRefreshRates.at(policy->defaultConfig)->hwcConfig;
Steven Thomasf734df42020-04-13 21:09:28 -0700468 ALOGV("constructAvailableRefreshRates: default %d group %d primaryRange=[%.2f %.2f]"
469 " appRequestRange=[%.2f %.2f]",
470 policy->defaultConfig.value(), defaultConfig->getConfigGroup(), policy->primaryRange.min,
471 policy->primaryRange.max, policy->appRequestRange.min, policy->appRequestRange.max);
Ady Abrahamabc27602020-04-08 17:20:29 -0700472
Steven Thomasf734df42020-04-13 21:09:28 -0700473 auto filterRefreshRates = [&](float min, float max, const char* listName,
474 std::vector<const RefreshRate*>* outRefreshRates) {
475 getSortedRefreshRateList(
476 [&](const RefreshRate& refreshRate) REQUIRES(mLock) {
477 const auto& hwcConfig = refreshRate.hwcConfig;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800478
Steven Thomasf734df42020-04-13 21:09:28 -0700479 return hwcConfig->getHeight() == defaultConfig->getHeight() &&
480 hwcConfig->getWidth() == defaultConfig->getWidth() &&
481 hwcConfig->getDpiX() == defaultConfig->getDpiX() &&
482 hwcConfig->getDpiY() == defaultConfig->getDpiY() &&
483 (policy->allowGroupSwitching ||
484 hwcConfig->getConfigGroup() == defaultConfig->getConfigGroup()) &&
485 refreshRate.inPolicy(min, max);
486 },
487 outRefreshRates);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800488
Steven Thomasf734df42020-04-13 21:09:28 -0700489 LOG_ALWAYS_FATAL_IF(outRefreshRates->empty(),
490 "No matching configs for %s range: min=%.0f max=%.0f", listName, min,
491 max);
492 auto stringifyRefreshRates = [&]() -> std::string {
493 std::string str;
494 for (auto refreshRate : *outRefreshRates) {
495 base::StringAppendF(&str, "%s ", refreshRate->name.c_str());
496 }
497 return str;
498 };
499 ALOGV("%s refresh rates: %s", listName, stringifyRefreshRates().c_str());
500 };
501
502 filterRefreshRates(policy->primaryRange.min, policy->primaryRange.max, "primary",
503 &mPrimaryRefreshRates);
504 filterRefreshRates(policy->appRequestRange.min, policy->appRequestRange.max, "app request",
505 &mAppRequestRefreshRates);
Ady Abraham2139f732019-11-13 18:56:40 -0800506}
507
Ady Abraham2139f732019-11-13 18:56:40 -0800508} // namespace android::scheduler