blob: e181c1253c7b7721508b9c31c17ac99bb8fadf27 [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
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 Abraham1adbb722020-05-15 11:51:48 -0700137 ALOGV("TouchBoost - choose %s", getMaxRefreshRateByPolicyLocked().getName().c_str());
138 if (touchConsidered) *touchConsidered = true;
Steven Thomasf734df42020-04-13 21:09:28 -0700139 return getMaxRefreshRateByPolicyLocked();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800140 }
141
Alec Mouri11232a22020-05-14 18:06:25 -0700142 // If the primary range consists of a single refresh rate then we can only
143 // move out the of range if layers explicitly request a different refresh
144 // rate.
145 const Policy* policy = getCurrentPolicyLocked();
146 const bool primaryRangeIsSingleRate = policy->primaryRange.min == policy->primaryRange.max;
147
148 if (!touchActive && idle && !(primaryRangeIsSingleRate && hasExplicitVoteLayers)) {
Steven Thomasbb374322020-04-28 22:47:16 -0700149 return getMinRefreshRateByPolicyLocked();
150 }
151
Steven Thomasdebafed2020-05-18 17:30:35 -0700152 if (layers.empty() || noVoteLayers == layers.size()) {
153 return getMaxRefreshRateByPolicyLocked();
Steven Thomasbb374322020-04-28 22:47:16 -0700154 }
155
Ady Abraham8a82ba62020-01-17 12:43:17 -0800156 // Only if all layers want Min we should return Min
157 if (noVoteLayers + minVoteLayers == layers.size()) {
Ady Abraham1adbb722020-05-15 11:51:48 -0700158 ALOGV("all layers Min - choose %s", getMinRefreshRateByPolicyLocked().getName().c_str());
Steven Thomasf734df42020-04-13 21:09:28 -0700159 return getMinRefreshRateByPolicyLocked();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800160 }
161
Ady Abraham8a82ba62020-01-17 12:43:17 -0800162 // Find the best refresh rate based on score
163 std::vector<std::pair<const RefreshRate*, float>> scores;
Steven Thomasf734df42020-04-13 21:09:28 -0700164 scores.reserve(mAppRequestRefreshRates.size());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800165
Steven Thomasf734df42020-04-13 21:09:28 -0700166 for (const auto refreshRate : mAppRequestRefreshRates) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800167 scores.emplace_back(refreshRate, 0.0f);
168 }
169
170 for (const auto& layer : layers) {
Ady Abrahamf6b77072020-01-30 14:22:54 -0800171 ALOGV("Calculating score for %s (type: %d)", layer.name.c_str(), layer.vote);
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800172 if (layer.vote == LayerVoteType::NoVote || layer.vote == LayerVoteType::Min) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800173 continue;
174 }
175
Ady Abraham71c437d2020-01-31 15:56:57 -0800176 auto weight = layer.weight;
Ady Abraham71c437d2020-01-31 15:56:57 -0800177
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800178 for (auto i = 0u; i < scores.size(); i++) {
Steven Thomasf734df42020-04-13 21:09:28 -0700179 bool inPrimaryRange =
180 scores[i].first->inPolicy(policy->primaryRange.min, policy->primaryRange.max);
Alec Mouri11232a22020-05-14 18:06:25 -0700181 if ((primaryRangeIsSingleRate || !inPrimaryRange) &&
182 layer.vote != LayerVoteType::ExplicitDefault &&
Steven Thomasf734df42020-04-13 21:09:28 -0700183 layer.vote != LayerVoteType::ExplicitExactOrMultiple) {
184 // Only layers with explicit frame rate settings are allowed to score refresh rates
185 // outside the primary range.
186 continue;
187 }
188
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800189 // If the layer wants Max, give higher score to the higher refresh rate
190 if (layer.vote == LayerVoteType::Max) {
191 const auto ratio = scores[i].first->fps / scores.back().first->fps;
192 // use ratio^2 to get a lower score the more we get further from peak
193 const auto layerScore = ratio * ratio;
194 ALOGV("%s (Max, weight %.2f) gives %s score of %.2f", layer.name.c_str(), weight,
195 scores[i].first->name.c_str(), layerScore);
196 scores[i].second += weight * layerScore;
197 continue;
Ady Abraham71c437d2020-01-31 15:56:57 -0800198 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800199
Ady Abrahamabc27602020-04-08 17:20:29 -0700200 const auto displayPeriod = scores[i].first->hwcConfig->getVsyncPeriod();
Ady Abrahamdec1a412020-01-24 10:23:50 -0800201 const auto layerPeriod = round<nsecs_t>(1e9f / layer.desiredRefreshRate);
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800202 if (layer.vote == LayerVoteType::ExplicitDefault) {
203 const auto layerScore = [&]() {
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800204 // Find the actual rate the layer will render, assuming
205 // that layerPeriod is the minimal time to render a frame
206 auto actualLayerPeriod = displayPeriod;
207 int multiplier = 1;
208 while (layerPeriod > actualLayerPeriod + MARGIN_FOR_PERIOD_CALCULATION) {
209 multiplier++;
210 actualLayerPeriod = displayPeriod * multiplier;
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800211 }
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800212 return std::min(1.0f,
213 static_cast<float>(layerPeriod) /
214 static_cast<float>(actualLayerPeriod));
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800215 }();
216
217 ALOGV("%s (ExplicitDefault, weight %.2f) %.2fHz gives %s score of %.2f",
218 layer.name.c_str(), weight, 1e9f / layerPeriod, scores[i].first->name.c_str(),
219 layerScore);
220 scores[i].second += weight * layerScore;
221 continue;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800222 }
223
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800224 if (layer.vote == LayerVoteType::ExplicitExactOrMultiple ||
225 layer.vote == LayerVoteType::Heuristic) {
226 const auto layerScore = [&]() {
227 // Calculate how many display vsyncs we need to present a single frame for this
228 // layer
229 const auto [displayFramesQuot, displayFramesRem] =
230 getDisplayFrames(layerPeriod, displayPeriod);
231 static constexpr size_t MAX_FRAMES_TO_FIT =
232 10; // Stop calculating when score < 0.1
233 if (displayFramesRem == 0) {
234 // Layer desired refresh rate matches the display rate.
235 return 1.0f;
236 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800237
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800238 if (displayFramesQuot == 0) {
239 // Layer desired refresh rate is higher the display rate.
240 return (static_cast<float>(layerPeriod) /
241 static_cast<float>(displayPeriod)) *
242 (1.0f / (MAX_FRAMES_TO_FIT + 1));
243 }
244
245 // Layer desired refresh rate is lower the display rate. Check how well it fits
246 // the cadence
247 auto diff = std::abs(displayFramesRem - (displayPeriod - displayFramesRem));
248 int iter = 2;
249 while (diff > MARGIN_FOR_PERIOD_CALCULATION && iter < MAX_FRAMES_TO_FIT) {
250 diff = diff - (displayPeriod - diff);
251 iter++;
252 }
253
254 return 1.0f / iter;
255 }();
Ady Abraham1adbb722020-05-15 11:51:48 -0700256 ALOGV("%s (%s, weight %.2f) %.2fHz gives %s score of %.2f", layer.name.c_str(),
257 layer.vote == LayerVoteType::ExplicitExactOrMultiple
258 ? "ExplicitExactOrMultiple"
259 : "Heuristic",
260 weight, 1e9f / layerPeriod, scores[i].first->name.c_str(), layerScore);
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800261 scores[i].second += weight * layerScore;
262 continue;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800263 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800264 }
265 }
266
Ady Abraham34702102020-02-10 14:12:05 -0800267 // Now that we scored all the refresh rates we need to pick the one that got the highest score.
268 // In case of a tie we will pick the higher refresh rate if any of the layers wanted Max,
269 // or the lower otherwise.
270 const RefreshRate* bestRefreshRate = maxVoteLayers > 0
271 ? getBestRefreshRate(scores.rbegin(), scores.rend())
272 : getBestRefreshRate(scores.begin(), scores.end());
273
Alec Mouri11232a22020-05-14 18:06:25 -0700274 if (primaryRangeIsSingleRate) {
275 // If we never scored any layers, then choose the rate from the primary
276 // range instead of picking a random score from the app range.
277 if (std::all_of(scores.begin(), scores.end(),
278 [](std::pair<const RefreshRate*, float> p) { return p.second == 0; })) {
279 return getMaxRefreshRateByPolicyLocked();
280 } else {
281 return *bestRefreshRate;
282 }
283 }
284
Steven Thomasf734df42020-04-13 21:09:28 -0700285 // Consider the touch event if there are no ExplicitDefault layers. ExplicitDefault are mostly
286 // interactive (as opposed to ExplicitExactOrMultiple) and therefore if those posted an explicit
287 // vote we should not change it if we get a touch event. Only apply touch boost if it will
288 // actually increase the refresh rate over the normal selection.
289 const RefreshRate& touchRefreshRate = getMaxRefreshRateByPolicyLocked();
Alec Mouri11232a22020-05-14 18:06:25 -0700290
Steven Thomasf734df42020-04-13 21:09:28 -0700291 if (touchActive && explicitDefaultVoteLayers == 0 &&
292 bestRefreshRate->fps < touchRefreshRate.fps) {
Ady Abraham1adbb722020-05-15 11:51:48 -0700293 if (touchConsidered) *touchConsidered = true;
294 ALOGV("TouchBoost - choose %s", touchRefreshRate.getName().c_str());
Steven Thomasf734df42020-04-13 21:09:28 -0700295 return touchRefreshRate;
296 }
297
Ady Abrahamde7156e2020-02-28 17:29:39 -0800298 return *bestRefreshRate;
Ady Abraham34702102020-02-10 14:12:05 -0800299}
300
301template <typename Iter>
302const RefreshRate* RefreshRateConfigs::getBestRefreshRate(Iter begin, Iter end) const {
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800303 constexpr auto EPSILON = 0.001f;
Ady Abrahamde7156e2020-02-28 17:29:39 -0800304 const RefreshRate* bestRefreshRate = begin->first;
305 float max = begin->second;
Ady Abraham34702102020-02-10 14:12:05 -0800306 for (auto i = begin; i != end; ++i) {
307 const auto [refreshRate, score] = *i;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800308 ALOGV("%s scores %.2f", refreshRate->name.c_str(), score);
309
Ady Abrahamdec1a412020-01-24 10:23:50 -0800310 ATRACE_INT(refreshRate->name.c_str(), round<int>(score * 100));
Ady Abraham8a82ba62020-01-17 12:43:17 -0800311
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800312 if (score > max * (1 + EPSILON)) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800313 max = score;
314 bestRefreshRate = refreshRate;
315 }
316 }
317
Ady Abraham34702102020-02-10 14:12:05 -0800318 return bestRefreshRate;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800319}
320
Ady Abraham2139f732019-11-13 18:56:40 -0800321const AllRefreshRatesMapType& RefreshRateConfigs::getAllRefreshRates() const {
322 return mRefreshRates;
323}
324
325const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicy() const {
326 std::lock_guard lock(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700327 return getMinRefreshRateByPolicyLocked();
328}
329
330const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicyLocked() const {
331 return *mPrimaryRefreshRates.front();
Ady Abraham2139f732019-11-13 18:56:40 -0800332}
333
334const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicy() const {
335 std::lock_guard lock(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700336 return getMaxRefreshRateByPolicyLocked();
337}
338
339const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicyLocked() const {
340 return *mPrimaryRefreshRates.back();
Ady Abraham2139f732019-11-13 18:56:40 -0800341}
342
343const RefreshRate& RefreshRateConfigs::getCurrentRefreshRate() const {
344 std::lock_guard lock(mLock);
345 return *mCurrentRefreshRate;
346}
347
Ana Krulec5d477912020-02-07 12:02:38 -0800348const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicy() const {
349 std::lock_guard lock(mLock);
Ana Krulec3d367c82020-02-25 15:02:01 -0800350 return getCurrentRefreshRateByPolicyLocked();
351}
352
353const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicyLocked() const {
Steven Thomasf734df42020-04-13 21:09:28 -0700354 if (std::find(mAppRequestRefreshRates.begin(), mAppRequestRefreshRates.end(),
355 mCurrentRefreshRate) != mAppRequestRefreshRates.end()) {
Ana Krulec5d477912020-02-07 12:02:38 -0800356 return *mCurrentRefreshRate;
357 }
Steven Thomasd4071902020-03-24 16:02:53 -0700358 return *mRefreshRates.at(getCurrentPolicyLocked()->defaultConfig);
Ana Krulec5d477912020-02-07 12:02:38 -0800359}
360
Ady Abraham2139f732019-11-13 18:56:40 -0800361void RefreshRateConfigs::setCurrentConfigId(HwcConfigIndexType configId) {
362 std::lock_guard lock(mLock);
Ady Abraham2e1dd892020-03-05 13:48:36 -0800363 mCurrentRefreshRate = mRefreshRates.at(configId).get();
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800364}
365
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800366RefreshRateConfigs::RefreshRateConfigs(
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800367 const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs,
Ana Krulec3f6a2062020-01-23 15:48:01 -0800368 HwcConfigIndexType currentConfigId) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700369 LOG_ALWAYS_FATAL_IF(configs.empty());
370 LOG_ALWAYS_FATAL_IF(currentConfigId.value() >= configs.size());
371
372 for (auto configId = HwcConfigIndexType(0); configId.value() < configs.size(); configId++) {
373 const auto& config = configs.at(static_cast<size_t>(configId.value()));
374 const float fps = 1e9f / config->getVsyncPeriod();
375 mRefreshRates.emplace(configId,
376 std::make_unique<RefreshRate>(configId, config,
Steven Thomasf734df42020-04-13 21:09:28 -0700377 base::StringPrintf("%.0ffps", fps), fps,
Ady Abrahamabc27602020-04-08 17:20:29 -0700378 RefreshRate::ConstructorTag(0)));
379 if (configId == currentConfigId) {
380 mCurrentRefreshRate = mRefreshRates.at(configId).get();
381 }
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800382 }
Ady Abrahamabc27602020-04-08 17:20:29 -0700383
384 std::vector<const RefreshRate*> sortedConfigs;
385 getSortedRefreshRateList([](const RefreshRate&) { return true; }, &sortedConfigs);
386 mDisplayManagerPolicy.defaultConfig = currentConfigId;
387 mMinSupportedRefreshRate = sortedConfigs.front();
388 mMaxSupportedRefreshRate = sortedConfigs.back();
389 constructAvailableRefreshRates();
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800390}
391
Steven Thomasd4071902020-03-24 16:02:53 -0700392bool RefreshRateConfigs::isPolicyValid(const Policy& policy) {
393 // defaultConfig must be a valid config, and within the given refresh rate range.
394 auto iter = mRefreshRates.find(policy.defaultConfig);
395 if (iter == mRefreshRates.end()) {
396 return false;
397 }
398 const RefreshRate& refreshRate = *iter->second;
Steven Thomasf734df42020-04-13 21:09:28 -0700399 if (!refreshRate.inPolicy(policy.primaryRange.min, policy.primaryRange.max)) {
Steven Thomasd4071902020-03-24 16:02:53 -0700400 return false;
401 }
Steven Thomasf734df42020-04-13 21:09:28 -0700402 return policy.appRequestRange.min <= policy.primaryRange.min &&
403 policy.appRequestRange.max >= policy.primaryRange.max;
Steven Thomasd4071902020-03-24 16:02:53 -0700404}
405
406status_t RefreshRateConfigs::setDisplayManagerPolicy(const Policy& policy) {
Ady Abraham2139f732019-11-13 18:56:40 -0800407 std::lock_guard lock(mLock);
Steven Thomasd4071902020-03-24 16:02:53 -0700408 if (!isPolicyValid(policy)) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100409 return BAD_VALUE;
410 }
Steven Thomasd4071902020-03-24 16:02:53 -0700411 Policy previousPolicy = *getCurrentPolicyLocked();
412 mDisplayManagerPolicy = policy;
413 if (*getCurrentPolicyLocked() == previousPolicy) {
414 return CURRENT_POLICY_UNCHANGED;
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100415 }
Ady Abraham2139f732019-11-13 18:56:40 -0800416 constructAvailableRefreshRates();
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100417 return NO_ERROR;
418}
419
Steven Thomasd4071902020-03-24 16:02:53 -0700420status_t RefreshRateConfigs::setOverridePolicy(const std::optional<Policy>& policy) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100421 std::lock_guard lock(mLock);
Steven Thomasd4071902020-03-24 16:02:53 -0700422 if (policy && !isPolicyValid(*policy)) {
423 return BAD_VALUE;
424 }
425 Policy previousPolicy = *getCurrentPolicyLocked();
426 mOverridePolicy = policy;
427 if (*getCurrentPolicyLocked() == previousPolicy) {
428 return CURRENT_POLICY_UNCHANGED;
429 }
430 constructAvailableRefreshRates();
431 return NO_ERROR;
432}
433
434const RefreshRateConfigs::Policy* RefreshRateConfigs::getCurrentPolicyLocked() const {
435 return mOverridePolicy ? &mOverridePolicy.value() : &mDisplayManagerPolicy;
436}
437
438RefreshRateConfigs::Policy RefreshRateConfigs::getCurrentPolicy() const {
439 std::lock_guard lock(mLock);
440 return *getCurrentPolicyLocked();
441}
442
443RefreshRateConfigs::Policy RefreshRateConfigs::getDisplayManagerPolicy() const {
444 std::lock_guard lock(mLock);
445 return mDisplayManagerPolicy;
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100446}
447
448bool RefreshRateConfigs::isConfigAllowed(HwcConfigIndexType config) const {
449 std::lock_guard lock(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700450 for (const RefreshRate* refreshRate : mAppRequestRefreshRates) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100451 if (refreshRate->configId == config) {
452 return true;
453 }
454 }
455 return false;
Ady Abraham2139f732019-11-13 18:56:40 -0800456}
457
458void RefreshRateConfigs::getSortedRefreshRateList(
459 const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate,
460 std::vector<const RefreshRate*>* outRefreshRates) {
461 outRefreshRates->clear();
462 outRefreshRates->reserve(mRefreshRates.size());
463 for (const auto& [type, refreshRate] : mRefreshRates) {
Ady Abraham2e1dd892020-03-05 13:48:36 -0800464 if (shouldAddRefreshRate(*refreshRate)) {
Ady Abraham2139f732019-11-13 18:56:40 -0800465 ALOGV("getSortedRefreshRateList: config %d added to list policy",
Ady Abraham2e1dd892020-03-05 13:48:36 -0800466 refreshRate->configId.value());
467 outRefreshRates->push_back(refreshRate.get());
Ady Abraham2139f732019-11-13 18:56:40 -0800468 }
469 }
470
471 std::sort(outRefreshRates->begin(), outRefreshRates->end(),
472 [](const auto refreshRate1, const auto refreshRate2) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700473 if (refreshRate1->hwcConfig->getVsyncPeriod() !=
474 refreshRate2->hwcConfig->getVsyncPeriod()) {
475 return refreshRate1->hwcConfig->getVsyncPeriod() >
476 refreshRate2->hwcConfig->getVsyncPeriod();
Steven Thomasd4071902020-03-24 16:02:53 -0700477 } else {
Ady Abrahamabc27602020-04-08 17:20:29 -0700478 return refreshRate1->hwcConfig->getConfigGroup() >
479 refreshRate2->hwcConfig->getConfigGroup();
Steven Thomasd4071902020-03-24 16:02:53 -0700480 }
Ady Abraham2139f732019-11-13 18:56:40 -0800481 });
482}
483
484void RefreshRateConfigs::constructAvailableRefreshRates() {
485 // Filter configs based on current policy and sort based on vsync period
Steven Thomasd4071902020-03-24 16:02:53 -0700486 const Policy* policy = getCurrentPolicyLocked();
Ady Abrahamabc27602020-04-08 17:20:29 -0700487 const auto& defaultConfig = mRefreshRates.at(policy->defaultConfig)->hwcConfig;
Steven Thomasf734df42020-04-13 21:09:28 -0700488 ALOGV("constructAvailableRefreshRates: default %d group %d primaryRange=[%.2f %.2f]"
489 " appRequestRange=[%.2f %.2f]",
490 policy->defaultConfig.value(), defaultConfig->getConfigGroup(), policy->primaryRange.min,
491 policy->primaryRange.max, policy->appRequestRange.min, policy->appRequestRange.max);
Ady Abrahamabc27602020-04-08 17:20:29 -0700492
Steven Thomasf734df42020-04-13 21:09:28 -0700493 auto filterRefreshRates = [&](float min, float max, const char* listName,
494 std::vector<const RefreshRate*>* outRefreshRates) {
495 getSortedRefreshRateList(
496 [&](const RefreshRate& refreshRate) REQUIRES(mLock) {
497 const auto& hwcConfig = refreshRate.hwcConfig;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800498
Steven Thomasf734df42020-04-13 21:09:28 -0700499 return hwcConfig->getHeight() == defaultConfig->getHeight() &&
500 hwcConfig->getWidth() == defaultConfig->getWidth() &&
501 hwcConfig->getDpiX() == defaultConfig->getDpiX() &&
502 hwcConfig->getDpiY() == defaultConfig->getDpiY() &&
503 (policy->allowGroupSwitching ||
504 hwcConfig->getConfigGroup() == defaultConfig->getConfigGroup()) &&
505 refreshRate.inPolicy(min, max);
506 },
507 outRefreshRates);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800508
Steven Thomasf734df42020-04-13 21:09:28 -0700509 LOG_ALWAYS_FATAL_IF(outRefreshRates->empty(),
510 "No matching configs for %s range: min=%.0f max=%.0f", listName, min,
511 max);
512 auto stringifyRefreshRates = [&]() -> std::string {
513 std::string str;
514 for (auto refreshRate : *outRefreshRates) {
515 base::StringAppendF(&str, "%s ", refreshRate->name.c_str());
516 }
517 return str;
518 };
519 ALOGV("%s refresh rates: %s", listName, stringifyRefreshRates().c_str());
520 };
521
522 filterRefreshRates(policy->primaryRange.min, policy->primaryRange.max, "primary",
523 &mPrimaryRefreshRates);
524 filterRefreshRates(policy->appRequestRange.min, policy->appRequestRange.max, "app request",
525 &mAppRequestRefreshRates);
Ady Abraham2139f732019-11-13 18:56:40 -0800526}
527
Ady Abraham2139f732019-11-13 18:56:40 -0800528} // namespace android::scheduler