blob: 43e67c2bd18d795d69ad87625af83c14ec4d2678 [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
Ady Abraham8a82ba62020-01-17 12:43:17 -0800100const RefreshRate& RefreshRateConfigs::getRefreshRateForContentV2(
Ady Abraham6fb599b2020-03-05 13:48:22 -0800101 const std::vector<LayerRequirement>& layers, bool touchActive,
102 bool* touchConsidered) const {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800103 ATRACE_CALL();
104 ALOGV("getRefreshRateForContent %zu layers", layers.size());
105
Ady Abraham6fb599b2020-03-05 13:48:22 -0800106 *touchConsidered = false;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800107 std::lock_guard lock(mLock);
108
Ana Krulec3d367c82020-02-25 15:02:01 -0800109 // If there are not layers, there is not content detection, so return the current
110 // refresh rate.
111 if (layers.empty()) {
Ady Abraham6fb599b2020-03-05 13:48:22 -0800112 *touchConsidered = touchActive;
Steven Thomasf734df42020-04-13 21:09:28 -0700113 return touchActive ? getMaxRefreshRateByPolicyLocked()
114 : getCurrentRefreshRateByPolicyLocked();
Ana Krulec3d367c82020-02-25 15:02:01 -0800115 }
116
Ady Abraham8a82ba62020-01-17 12:43:17 -0800117 int noVoteLayers = 0;
118 int minVoteLayers = 0;
119 int maxVoteLayers = 0;
Ady Abraham71c437d2020-01-31 15:56:57 -0800120 int explicitDefaultVoteLayers = 0;
121 int explicitExactOrMultipleVoteLayers = 0;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800122 float maxExplicitWeight = 0;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800123 for (const auto& layer : layers) {
Ady Abraham6fb599b2020-03-05 13:48:22 -0800124 if (layer.vote == LayerVoteType::NoVote) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800125 noVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800126 } else if (layer.vote == LayerVoteType::Min) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800127 minVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800128 } else if (layer.vote == LayerVoteType::Max) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800129 maxVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800130 } else if (layer.vote == LayerVoteType::ExplicitDefault) {
Ady Abraham71c437d2020-01-31 15:56:57 -0800131 explicitDefaultVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800132 maxExplicitWeight = std::max(maxExplicitWeight, layer.weight);
133 } else if (layer.vote == LayerVoteType::ExplicitExactOrMultiple) {
Ady Abraham71c437d2020-01-31 15:56:57 -0800134 explicitExactOrMultipleVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800135 maxExplicitWeight = std::max(maxExplicitWeight, layer.weight);
136 }
137 }
138
Steven Thomasf734df42020-04-13 21:09:28 -0700139 // Consider the touch event if there are no Explicit* layers. Otherwise wait until after we've
140 // selected a refresh rate to see if we should apply touch boost.
141 if (touchActive && explicitDefaultVoteLayers == 0 && explicitExactOrMultipleVoteLayers == 0) {
Ady Abraham6fb599b2020-03-05 13:48:22 -0800142 *touchConsidered = true;
Steven Thomasf734df42020-04-13 21:09:28 -0700143 return getMaxRefreshRateByPolicyLocked();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800144 }
145
146 // Only if all layers want Min we should return Min
147 if (noVoteLayers + minVoteLayers == layers.size()) {
Steven Thomasf734df42020-04-13 21:09:28 -0700148 return getMinRefreshRateByPolicyLocked();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800149 }
150
Steven Thomasf734df42020-04-13 21:09:28 -0700151 const Policy* policy = getCurrentPolicyLocked();
152
Ady Abraham8a82ba62020-01-17 12:43:17 -0800153 // Find the best refresh rate based on score
154 std::vector<std::pair<const RefreshRate*, float>> scores;
Steven Thomasf734df42020-04-13 21:09:28 -0700155 scores.reserve(mAppRequestRefreshRates.size());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800156
Steven Thomasf734df42020-04-13 21:09:28 -0700157 for (const auto refreshRate : mAppRequestRefreshRates) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800158 scores.emplace_back(refreshRate, 0.0f);
159 }
160
161 for (const auto& layer : layers) {
Ady Abrahamf6b77072020-01-30 14:22:54 -0800162 ALOGV("Calculating score for %s (type: %d)", layer.name.c_str(), layer.vote);
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800163 if (layer.vote == LayerVoteType::NoVote || layer.vote == LayerVoteType::Min) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800164 continue;
165 }
166
Ady Abraham71c437d2020-01-31 15:56:57 -0800167 auto weight = layer.weight;
Ady Abraham71c437d2020-01-31 15:56:57 -0800168
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800169 for (auto i = 0u; i < scores.size(); i++) {
Steven Thomasf734df42020-04-13 21:09:28 -0700170 bool inPrimaryRange =
171 scores[i].first->inPolicy(policy->primaryRange.min, policy->primaryRange.max);
172 if (!inPrimaryRange && layer.vote != LayerVoteType::ExplicitDefault &&
173 layer.vote != LayerVoteType::ExplicitExactOrMultiple) {
174 // Only layers with explicit frame rate settings are allowed to score refresh rates
175 // outside the primary range.
176 continue;
177 }
178
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800179 // If the layer wants Max, give higher score to the higher refresh rate
180 if (layer.vote == LayerVoteType::Max) {
181 const auto ratio = scores[i].first->fps / scores.back().first->fps;
182 // use ratio^2 to get a lower score the more we get further from peak
183 const auto layerScore = ratio * ratio;
184 ALOGV("%s (Max, weight %.2f) gives %s score of %.2f", layer.name.c_str(), weight,
185 scores[i].first->name.c_str(), layerScore);
186 scores[i].second += weight * layerScore;
187 continue;
Ady Abraham71c437d2020-01-31 15:56:57 -0800188 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800189
Ady Abrahamabc27602020-04-08 17:20:29 -0700190 const auto displayPeriod = scores[i].first->hwcConfig->getVsyncPeriod();
Ady Abrahamdec1a412020-01-24 10:23:50 -0800191 const auto layerPeriod = round<nsecs_t>(1e9f / layer.desiredRefreshRate);
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800192 if (layer.vote == LayerVoteType::ExplicitDefault) {
193 const auto layerScore = [&]() {
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800194 // Find the actual rate the layer will render, assuming
195 // that layerPeriod is the minimal time to render a frame
196 auto actualLayerPeriod = displayPeriod;
197 int multiplier = 1;
198 while (layerPeriod > actualLayerPeriod + MARGIN_FOR_PERIOD_CALCULATION) {
199 multiplier++;
200 actualLayerPeriod = displayPeriod * multiplier;
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800201 }
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800202 return std::min(1.0f,
203 static_cast<float>(layerPeriod) /
204 static_cast<float>(actualLayerPeriod));
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800205 }();
206
207 ALOGV("%s (ExplicitDefault, weight %.2f) %.2fHz gives %s score of %.2f",
208 layer.name.c_str(), weight, 1e9f / layerPeriod, scores[i].first->name.c_str(),
209 layerScore);
210 scores[i].second += weight * layerScore;
211 continue;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800212 }
213
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800214 if (layer.vote == LayerVoteType::ExplicitExactOrMultiple ||
215 layer.vote == LayerVoteType::Heuristic) {
216 const auto layerScore = [&]() {
217 // Calculate how many display vsyncs we need to present a single frame for this
218 // layer
219 const auto [displayFramesQuot, displayFramesRem] =
220 getDisplayFrames(layerPeriod, displayPeriod);
221 static constexpr size_t MAX_FRAMES_TO_FIT =
222 10; // Stop calculating when score < 0.1
223 if (displayFramesRem == 0) {
224 // Layer desired refresh rate matches the display rate.
225 return 1.0f;
226 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800227
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800228 if (displayFramesQuot == 0) {
229 // Layer desired refresh rate is higher the display rate.
230 return (static_cast<float>(layerPeriod) /
231 static_cast<float>(displayPeriod)) *
232 (1.0f / (MAX_FRAMES_TO_FIT + 1));
233 }
234
235 // Layer desired refresh rate is lower the display rate. Check how well it fits
236 // the cadence
237 auto diff = std::abs(displayFramesRem - (displayPeriod - displayFramesRem));
238 int iter = 2;
239 while (diff > MARGIN_FOR_PERIOD_CALCULATION && iter < MAX_FRAMES_TO_FIT) {
240 diff = diff - (displayPeriod - diff);
241 iter++;
242 }
243
244 return 1.0f / iter;
245 }();
246 ALOGV("%s (ExplicitExactOrMultiple, weight %.2f) %.2fHz gives %s score of %.2f",
247 layer.name.c_str(), weight, 1e9f / layerPeriod, scores[i].first->name.c_str(),
248 layerScore);
249 scores[i].second += weight * layerScore;
250 continue;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800251 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800252 }
253 }
254
Ady Abraham34702102020-02-10 14:12:05 -0800255 // Now that we scored all the refresh rates we need to pick the one that got the highest score.
256 // In case of a tie we will pick the higher refresh rate if any of the layers wanted Max,
257 // or the lower otherwise.
258 const RefreshRate* bestRefreshRate = maxVoteLayers > 0
259 ? getBestRefreshRate(scores.rbegin(), scores.rend())
260 : getBestRefreshRate(scores.begin(), scores.end());
261
Steven Thomasf734df42020-04-13 21:09:28 -0700262 // Consider the touch event if there are no ExplicitDefault layers. ExplicitDefault are mostly
263 // interactive (as opposed to ExplicitExactOrMultiple) and therefore if those posted an explicit
264 // vote we should not change it if we get a touch event. Only apply touch boost if it will
265 // actually increase the refresh rate over the normal selection.
266 const RefreshRate& touchRefreshRate = getMaxRefreshRateByPolicyLocked();
267 if (touchActive && explicitDefaultVoteLayers == 0 &&
268 bestRefreshRate->fps < touchRefreshRate.fps) {
269 *touchConsidered = true;
270 return touchRefreshRate;
271 }
272
Ady Abrahamde7156e2020-02-28 17:29:39 -0800273 return *bestRefreshRate;
Ady Abraham34702102020-02-10 14:12:05 -0800274}
275
276template <typename Iter>
277const RefreshRate* RefreshRateConfigs::getBestRefreshRate(Iter begin, Iter end) const {
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800278 constexpr auto EPSILON = 0.001f;
Ady Abrahamde7156e2020-02-28 17:29:39 -0800279 const RefreshRate* bestRefreshRate = begin->first;
280 float max = begin->second;
Ady Abraham34702102020-02-10 14:12:05 -0800281 for (auto i = begin; i != end; ++i) {
282 const auto [refreshRate, score] = *i;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800283 ALOGV("%s scores %.2f", refreshRate->name.c_str(), score);
284
Ady Abrahamdec1a412020-01-24 10:23:50 -0800285 ATRACE_INT(refreshRate->name.c_str(), round<int>(score * 100));
Ady Abraham8a82ba62020-01-17 12:43:17 -0800286
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800287 if (score > max * (1 + EPSILON)) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800288 max = score;
289 bestRefreshRate = refreshRate;
290 }
291 }
292
Ady Abraham34702102020-02-10 14:12:05 -0800293 return bestRefreshRate;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800294}
295
Ady Abraham2139f732019-11-13 18:56:40 -0800296const AllRefreshRatesMapType& RefreshRateConfigs::getAllRefreshRates() const {
297 return mRefreshRates;
298}
299
300const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicy() const {
301 std::lock_guard lock(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700302 return getMinRefreshRateByPolicyLocked();
303}
304
305const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicyLocked() const {
306 return *mPrimaryRefreshRates.front();
Ady Abraham2139f732019-11-13 18:56:40 -0800307}
308
309const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicy() const {
310 std::lock_guard lock(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700311 return getMaxRefreshRateByPolicyLocked();
312}
313
314const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicyLocked() const {
315 return *mPrimaryRefreshRates.back();
Ady Abraham2139f732019-11-13 18:56:40 -0800316}
317
318const RefreshRate& RefreshRateConfigs::getCurrentRefreshRate() const {
319 std::lock_guard lock(mLock);
320 return *mCurrentRefreshRate;
321}
322
Ana Krulec5d477912020-02-07 12:02:38 -0800323const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicy() const {
324 std::lock_guard lock(mLock);
Ana Krulec3d367c82020-02-25 15:02:01 -0800325 return getCurrentRefreshRateByPolicyLocked();
326}
327
328const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicyLocked() const {
Steven Thomasf734df42020-04-13 21:09:28 -0700329 if (std::find(mAppRequestRefreshRates.begin(), mAppRequestRefreshRates.end(),
330 mCurrentRefreshRate) != mAppRequestRefreshRates.end()) {
Ana Krulec5d477912020-02-07 12:02:38 -0800331 return *mCurrentRefreshRate;
332 }
Steven Thomasd4071902020-03-24 16:02:53 -0700333 return *mRefreshRates.at(getCurrentPolicyLocked()->defaultConfig);
Ana Krulec5d477912020-02-07 12:02:38 -0800334}
335
Ady Abraham2139f732019-11-13 18:56:40 -0800336void RefreshRateConfigs::setCurrentConfigId(HwcConfigIndexType configId) {
337 std::lock_guard lock(mLock);
Ady Abraham2e1dd892020-03-05 13:48:36 -0800338 mCurrentRefreshRate = mRefreshRates.at(configId).get();
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800339}
340
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800341RefreshRateConfigs::RefreshRateConfigs(
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800342 const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs,
Ana Krulec3f6a2062020-01-23 15:48:01 -0800343 HwcConfigIndexType currentConfigId) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700344 LOG_ALWAYS_FATAL_IF(configs.empty());
345 LOG_ALWAYS_FATAL_IF(currentConfigId.value() >= configs.size());
346
347 for (auto configId = HwcConfigIndexType(0); configId.value() < configs.size(); configId++) {
348 const auto& config = configs.at(static_cast<size_t>(configId.value()));
349 const float fps = 1e9f / config->getVsyncPeriod();
350 mRefreshRates.emplace(configId,
351 std::make_unique<RefreshRate>(configId, config,
Steven Thomasf734df42020-04-13 21:09:28 -0700352 base::StringPrintf("%.0ffps", fps), fps,
Ady Abrahamabc27602020-04-08 17:20:29 -0700353 RefreshRate::ConstructorTag(0)));
354 if (configId == currentConfigId) {
355 mCurrentRefreshRate = mRefreshRates.at(configId).get();
356 }
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800357 }
Ady Abrahamabc27602020-04-08 17:20:29 -0700358
359 std::vector<const RefreshRate*> sortedConfigs;
360 getSortedRefreshRateList([](const RefreshRate&) { return true; }, &sortedConfigs);
361 mDisplayManagerPolicy.defaultConfig = currentConfigId;
362 mMinSupportedRefreshRate = sortedConfigs.front();
363 mMaxSupportedRefreshRate = sortedConfigs.back();
364 constructAvailableRefreshRates();
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800365}
366
Steven Thomasd4071902020-03-24 16:02:53 -0700367bool RefreshRateConfigs::isPolicyValid(const Policy& policy) {
368 // defaultConfig must be a valid config, and within the given refresh rate range.
369 auto iter = mRefreshRates.find(policy.defaultConfig);
370 if (iter == mRefreshRates.end()) {
371 return false;
372 }
373 const RefreshRate& refreshRate = *iter->second;
Steven Thomasf734df42020-04-13 21:09:28 -0700374 if (!refreshRate.inPolicy(policy.primaryRange.min, policy.primaryRange.max)) {
Steven Thomasd4071902020-03-24 16:02:53 -0700375 return false;
376 }
Steven Thomasf734df42020-04-13 21:09:28 -0700377 return policy.appRequestRange.min <= policy.primaryRange.min &&
378 policy.appRequestRange.max >= policy.primaryRange.max;
Steven Thomasd4071902020-03-24 16:02:53 -0700379}
380
381status_t RefreshRateConfigs::setDisplayManagerPolicy(const Policy& policy) {
Ady Abraham2139f732019-11-13 18:56:40 -0800382 std::lock_guard lock(mLock);
Steven Thomasd4071902020-03-24 16:02:53 -0700383 if (!isPolicyValid(policy)) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100384 return BAD_VALUE;
385 }
Steven Thomasd4071902020-03-24 16:02:53 -0700386 Policy previousPolicy = *getCurrentPolicyLocked();
387 mDisplayManagerPolicy = policy;
388 if (*getCurrentPolicyLocked() == previousPolicy) {
389 return CURRENT_POLICY_UNCHANGED;
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100390 }
Ady Abraham2139f732019-11-13 18:56:40 -0800391 constructAvailableRefreshRates();
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100392 return NO_ERROR;
393}
394
Steven Thomasd4071902020-03-24 16:02:53 -0700395status_t RefreshRateConfigs::setOverridePolicy(const std::optional<Policy>& policy) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100396 std::lock_guard lock(mLock);
Steven Thomasd4071902020-03-24 16:02:53 -0700397 if (policy && !isPolicyValid(*policy)) {
398 return BAD_VALUE;
399 }
400 Policy previousPolicy = *getCurrentPolicyLocked();
401 mOverridePolicy = policy;
402 if (*getCurrentPolicyLocked() == previousPolicy) {
403 return CURRENT_POLICY_UNCHANGED;
404 }
405 constructAvailableRefreshRates();
406 return NO_ERROR;
407}
408
409const RefreshRateConfigs::Policy* RefreshRateConfigs::getCurrentPolicyLocked() const {
410 return mOverridePolicy ? &mOverridePolicy.value() : &mDisplayManagerPolicy;
411}
412
413RefreshRateConfigs::Policy RefreshRateConfigs::getCurrentPolicy() const {
414 std::lock_guard lock(mLock);
415 return *getCurrentPolicyLocked();
416}
417
418RefreshRateConfigs::Policy RefreshRateConfigs::getDisplayManagerPolicy() const {
419 std::lock_guard lock(mLock);
420 return mDisplayManagerPolicy;
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100421}
422
423bool RefreshRateConfigs::isConfigAllowed(HwcConfigIndexType config) const {
424 std::lock_guard lock(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700425 for (const RefreshRate* refreshRate : mAppRequestRefreshRates) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100426 if (refreshRate->configId == config) {
427 return true;
428 }
429 }
430 return false;
Ady Abraham2139f732019-11-13 18:56:40 -0800431}
432
433void RefreshRateConfigs::getSortedRefreshRateList(
434 const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate,
435 std::vector<const RefreshRate*>* outRefreshRates) {
436 outRefreshRates->clear();
437 outRefreshRates->reserve(mRefreshRates.size());
438 for (const auto& [type, refreshRate] : mRefreshRates) {
Ady Abraham2e1dd892020-03-05 13:48:36 -0800439 if (shouldAddRefreshRate(*refreshRate)) {
Ady Abraham2139f732019-11-13 18:56:40 -0800440 ALOGV("getSortedRefreshRateList: config %d added to list policy",
Ady Abraham2e1dd892020-03-05 13:48:36 -0800441 refreshRate->configId.value());
442 outRefreshRates->push_back(refreshRate.get());
Ady Abraham2139f732019-11-13 18:56:40 -0800443 }
444 }
445
446 std::sort(outRefreshRates->begin(), outRefreshRates->end(),
447 [](const auto refreshRate1, const auto refreshRate2) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700448 if (refreshRate1->hwcConfig->getVsyncPeriod() !=
449 refreshRate2->hwcConfig->getVsyncPeriod()) {
450 return refreshRate1->hwcConfig->getVsyncPeriod() >
451 refreshRate2->hwcConfig->getVsyncPeriod();
Steven Thomasd4071902020-03-24 16:02:53 -0700452 } else {
Ady Abrahamabc27602020-04-08 17:20:29 -0700453 return refreshRate1->hwcConfig->getConfigGroup() >
454 refreshRate2->hwcConfig->getConfigGroup();
Steven Thomasd4071902020-03-24 16:02:53 -0700455 }
Ady Abraham2139f732019-11-13 18:56:40 -0800456 });
457}
458
459void RefreshRateConfigs::constructAvailableRefreshRates() {
460 // Filter configs based on current policy and sort based on vsync period
Steven Thomasd4071902020-03-24 16:02:53 -0700461 const Policy* policy = getCurrentPolicyLocked();
Ady Abrahamabc27602020-04-08 17:20:29 -0700462 const auto& defaultConfig = mRefreshRates.at(policy->defaultConfig)->hwcConfig;
Steven Thomasf734df42020-04-13 21:09:28 -0700463 ALOGV("constructAvailableRefreshRates: default %d group %d primaryRange=[%.2f %.2f]"
464 " appRequestRange=[%.2f %.2f]",
465 policy->defaultConfig.value(), defaultConfig->getConfigGroup(), policy->primaryRange.min,
466 policy->primaryRange.max, policy->appRequestRange.min, policy->appRequestRange.max);
Ady Abrahamabc27602020-04-08 17:20:29 -0700467
Steven Thomasf734df42020-04-13 21:09:28 -0700468 auto filterRefreshRates = [&](float min, float max, const char* listName,
469 std::vector<const RefreshRate*>* outRefreshRates) {
470 getSortedRefreshRateList(
471 [&](const RefreshRate& refreshRate) REQUIRES(mLock) {
472 const auto& hwcConfig = refreshRate.hwcConfig;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800473
Steven Thomasf734df42020-04-13 21:09:28 -0700474 return hwcConfig->getHeight() == defaultConfig->getHeight() &&
475 hwcConfig->getWidth() == defaultConfig->getWidth() &&
476 hwcConfig->getDpiX() == defaultConfig->getDpiX() &&
477 hwcConfig->getDpiY() == defaultConfig->getDpiY() &&
478 (policy->allowGroupSwitching ||
479 hwcConfig->getConfigGroup() == defaultConfig->getConfigGroup()) &&
480 refreshRate.inPolicy(min, max);
481 },
482 outRefreshRates);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800483
Steven Thomasf734df42020-04-13 21:09:28 -0700484 LOG_ALWAYS_FATAL_IF(outRefreshRates->empty(),
485 "No matching configs for %s range: min=%.0f max=%.0f", listName, min,
486 max);
487 auto stringifyRefreshRates = [&]() -> std::string {
488 std::string str;
489 for (auto refreshRate : *outRefreshRates) {
490 base::StringAppendF(&str, "%s ", refreshRate->name.c_str());
491 }
492 return str;
493 };
494 ALOGV("%s refresh rates: %s", listName, stringifyRefreshRates().c_str());
495 };
496
497 filterRefreshRates(policy->primaryRange.min, policy->primaryRange.max, "primary",
498 &mPrimaryRefreshRates);
499 filterRefreshRates(policy->appRequestRange.min, policy->appRequestRange.max, "app request",
500 &mAppRequestRefreshRates);
Ady Abraham2139f732019-11-13 18:56:40 -0800501}
502
Ady Abraham2139f732019-11-13 18:56:40 -0800503} // namespace android::scheduler