blob: 5634adb9fd7e721e1ca1a22fc19c09235a787557 [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
61 auto iter = min_element(mAvailableRefreshRates.cbegin(), mAvailableRefreshRates.cend(),
62 [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) {
74 while (iter != mAvailableRefreshRates.cend()) {
75 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;
113 return touchActive ? *mAvailableRefreshRates.back() : getCurrentRefreshRateByPolicyLocked();
Ana Krulec3d367c82020-02-25 15:02:01 -0800114 }
115
Ady Abraham8a82ba62020-01-17 12:43:17 -0800116 int noVoteLayers = 0;
117 int minVoteLayers = 0;
118 int maxVoteLayers = 0;
Ady Abraham71c437d2020-01-31 15:56:57 -0800119 int explicitDefaultVoteLayers = 0;
120 int explicitExactOrMultipleVoteLayers = 0;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800121 float maxExplicitWeight = 0;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800122 for (const auto& layer : layers) {
Ady Abraham6fb599b2020-03-05 13:48:22 -0800123 if (layer.vote == LayerVoteType::NoVote) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800124 noVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800125 } else if (layer.vote == LayerVoteType::Min) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800126 minVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800127 } else if (layer.vote == LayerVoteType::Max) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800128 maxVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800129 } else if (layer.vote == LayerVoteType::ExplicitDefault) {
Ady Abraham71c437d2020-01-31 15:56:57 -0800130 explicitDefaultVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800131 maxExplicitWeight = std::max(maxExplicitWeight, layer.weight);
132 } else if (layer.vote == LayerVoteType::ExplicitExactOrMultiple) {
Ady Abraham71c437d2020-01-31 15:56:57 -0800133 explicitExactOrMultipleVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800134 maxExplicitWeight = std::max(maxExplicitWeight, layer.weight);
135 }
136 }
137
138 // Consider the touch event if there are no ExplicitDefault layers.
139 // ExplicitDefault are mostly interactive (as opposed to ExplicitExactOrMultiple)
140 // and therefore if those posted an explicit vote we should not change it
141 // if get get a touch event.
142 if (touchActive && explicitDefaultVoteLayers == 0) {
143 *touchConsidered = true;
144 return *mAvailableRefreshRates.back();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800145 }
146
147 // Only if all layers want Min we should return Min
148 if (noVoteLayers + minVoteLayers == layers.size()) {
149 return *mAvailableRefreshRates.front();
150 }
151
Ady Abraham8a82ba62020-01-17 12:43:17 -0800152 // Find the best refresh rate based on score
153 std::vector<std::pair<const RefreshRate*, float>> scores;
154 scores.reserve(mAvailableRefreshRates.size());
155
156 for (const auto refreshRate : mAvailableRefreshRates) {
157 scores.emplace_back(refreshRate, 0.0f);
158 }
159
160 for (const auto& layer : layers) {
Ady Abrahamf6b77072020-01-30 14:22:54 -0800161 ALOGV("Calculating score for %s (type: %d)", layer.name.c_str(), layer.vote);
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800162 if (layer.vote == LayerVoteType::NoVote || layer.vote == LayerVoteType::Min) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800163 continue;
164 }
165
Ady Abraham71c437d2020-01-31 15:56:57 -0800166 auto weight = layer.weight;
Ady Abraham71c437d2020-01-31 15:56:57 -0800167
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800168 for (auto i = 0u; i < scores.size(); i++) {
169 // If the layer wants Max, give higher score to the higher refresh rate
170 if (layer.vote == LayerVoteType::Max) {
171 const auto ratio = scores[i].first->fps / scores.back().first->fps;
172 // use ratio^2 to get a lower score the more we get further from peak
173 const auto layerScore = ratio * ratio;
174 ALOGV("%s (Max, weight %.2f) gives %s score of %.2f", layer.name.c_str(), weight,
175 scores[i].first->name.c_str(), layerScore);
176 scores[i].second += weight * layerScore;
177 continue;
Ady Abraham71c437d2020-01-31 15:56:57 -0800178 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800179
Ady Abrahamabc27602020-04-08 17:20:29 -0700180 const auto displayPeriod = scores[i].first->hwcConfig->getVsyncPeriod();
Ady Abrahamdec1a412020-01-24 10:23:50 -0800181 const auto layerPeriod = round<nsecs_t>(1e9f / layer.desiredRefreshRate);
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800182 if (layer.vote == LayerVoteType::ExplicitDefault) {
183 const auto layerScore = [&]() {
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800184 // Find the actual rate the layer will render, assuming
185 // that layerPeriod is the minimal time to render a frame
186 auto actualLayerPeriod = displayPeriod;
187 int multiplier = 1;
188 while (layerPeriod > actualLayerPeriod + MARGIN_FOR_PERIOD_CALCULATION) {
189 multiplier++;
190 actualLayerPeriod = displayPeriod * multiplier;
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800191 }
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800192 return std::min(1.0f,
193 static_cast<float>(layerPeriod) /
194 static_cast<float>(actualLayerPeriod));
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800195 }();
196
197 ALOGV("%s (ExplicitDefault, weight %.2f) %.2fHz gives %s score of %.2f",
198 layer.name.c_str(), weight, 1e9f / layerPeriod, scores[i].first->name.c_str(),
199 layerScore);
200 scores[i].second += weight * layerScore;
201 continue;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800202 }
203
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800204 if (layer.vote == LayerVoteType::ExplicitExactOrMultiple ||
205 layer.vote == LayerVoteType::Heuristic) {
206 const auto layerScore = [&]() {
207 // Calculate how many display vsyncs we need to present a single frame for this
208 // layer
209 const auto [displayFramesQuot, displayFramesRem] =
210 getDisplayFrames(layerPeriod, displayPeriod);
211 static constexpr size_t MAX_FRAMES_TO_FIT =
212 10; // Stop calculating when score < 0.1
213 if (displayFramesRem == 0) {
214 // Layer desired refresh rate matches the display rate.
215 return 1.0f;
216 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800217
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800218 if (displayFramesQuot == 0) {
219 // Layer desired refresh rate is higher the display rate.
220 return (static_cast<float>(layerPeriod) /
221 static_cast<float>(displayPeriod)) *
222 (1.0f / (MAX_FRAMES_TO_FIT + 1));
223 }
224
225 // Layer desired refresh rate is lower the display rate. Check how well it fits
226 // the cadence
227 auto diff = std::abs(displayFramesRem - (displayPeriod - displayFramesRem));
228 int iter = 2;
229 while (diff > MARGIN_FOR_PERIOD_CALCULATION && iter < MAX_FRAMES_TO_FIT) {
230 diff = diff - (displayPeriod - diff);
231 iter++;
232 }
233
234 return 1.0f / iter;
235 }();
236 ALOGV("%s (ExplicitExactOrMultiple, weight %.2f) %.2fHz gives %s score of %.2f",
237 layer.name.c_str(), weight, 1e9f / layerPeriod, scores[i].first->name.c_str(),
238 layerScore);
239 scores[i].second += weight * layerScore;
240 continue;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800241 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800242 }
243 }
244
Ady Abraham34702102020-02-10 14:12:05 -0800245 // Now that we scored all the refresh rates we need to pick the one that got the highest score.
246 // In case of a tie we will pick the higher refresh rate if any of the layers wanted Max,
247 // or the lower otherwise.
248 const RefreshRate* bestRefreshRate = maxVoteLayers > 0
249 ? getBestRefreshRate(scores.rbegin(), scores.rend())
250 : getBestRefreshRate(scores.begin(), scores.end());
251
Ady Abrahamde7156e2020-02-28 17:29:39 -0800252 return *bestRefreshRate;
Ady Abraham34702102020-02-10 14:12:05 -0800253}
254
255template <typename Iter>
256const RefreshRate* RefreshRateConfigs::getBestRefreshRate(Iter begin, Iter end) const {
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800257 constexpr auto EPSILON = 0.001f;
Ady Abrahamde7156e2020-02-28 17:29:39 -0800258 const RefreshRate* bestRefreshRate = begin->first;
259 float max = begin->second;
Ady Abraham34702102020-02-10 14:12:05 -0800260 for (auto i = begin; i != end; ++i) {
261 const auto [refreshRate, score] = *i;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800262 ALOGV("%s scores %.2f", refreshRate->name.c_str(), score);
263
Ady Abrahamdec1a412020-01-24 10:23:50 -0800264 ATRACE_INT(refreshRate->name.c_str(), round<int>(score * 100));
Ady Abraham8a82ba62020-01-17 12:43:17 -0800265
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800266 if (score > max * (1 + EPSILON)) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800267 max = score;
268 bestRefreshRate = refreshRate;
269 }
270 }
271
Ady Abraham34702102020-02-10 14:12:05 -0800272 return bestRefreshRate;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800273}
274
Ady Abraham2139f732019-11-13 18:56:40 -0800275const AllRefreshRatesMapType& RefreshRateConfigs::getAllRefreshRates() const {
276 return mRefreshRates;
277}
278
279const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicy() const {
280 std::lock_guard lock(mLock);
Ana Krulec3f6a2062020-01-23 15:48:01 -0800281 return *mAvailableRefreshRates.front();
Ady Abraham2139f732019-11-13 18:56:40 -0800282}
283
284const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicy() const {
285 std::lock_guard lock(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800286 return *mAvailableRefreshRates.back();
Ady Abraham2139f732019-11-13 18:56:40 -0800287}
288
289const RefreshRate& RefreshRateConfigs::getCurrentRefreshRate() const {
290 std::lock_guard lock(mLock);
291 return *mCurrentRefreshRate;
292}
293
Ana Krulec5d477912020-02-07 12:02:38 -0800294const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicy() const {
295 std::lock_guard lock(mLock);
Ana Krulec3d367c82020-02-25 15:02:01 -0800296 return getCurrentRefreshRateByPolicyLocked();
297}
298
299const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicyLocked() const {
Ana Krulec5d477912020-02-07 12:02:38 -0800300 if (std::find(mAvailableRefreshRates.begin(), mAvailableRefreshRates.end(),
301 mCurrentRefreshRate) != mAvailableRefreshRates.end()) {
302 return *mCurrentRefreshRate;
303 }
Steven Thomasd4071902020-03-24 16:02:53 -0700304 return *mRefreshRates.at(getCurrentPolicyLocked()->defaultConfig);
Ana Krulec5d477912020-02-07 12:02:38 -0800305}
306
Ady Abraham2139f732019-11-13 18:56:40 -0800307void RefreshRateConfigs::setCurrentConfigId(HwcConfigIndexType configId) {
308 std::lock_guard lock(mLock);
Ady Abraham2e1dd892020-03-05 13:48:36 -0800309 mCurrentRefreshRate = mRefreshRates.at(configId).get();
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800310}
311
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800312RefreshRateConfigs::RefreshRateConfigs(
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800313 const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs,
Ana Krulec3f6a2062020-01-23 15:48:01 -0800314 HwcConfigIndexType currentConfigId) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700315 LOG_ALWAYS_FATAL_IF(configs.empty());
316 LOG_ALWAYS_FATAL_IF(currentConfigId.value() >= configs.size());
317
318 for (auto configId = HwcConfigIndexType(0); configId.value() < configs.size(); configId++) {
319 const auto& config = configs.at(static_cast<size_t>(configId.value()));
320 const float fps = 1e9f / config->getVsyncPeriod();
321 mRefreshRates.emplace(configId,
322 std::make_unique<RefreshRate>(configId, config,
323 base::StringPrintf("%2.ffps", fps), fps,
324 RefreshRate::ConstructorTag(0)));
325 if (configId == currentConfigId) {
326 mCurrentRefreshRate = mRefreshRates.at(configId).get();
327 }
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800328 }
Ady Abrahamabc27602020-04-08 17:20:29 -0700329
330 std::vector<const RefreshRate*> sortedConfigs;
331 getSortedRefreshRateList([](const RefreshRate&) { return true; }, &sortedConfigs);
332 mDisplayManagerPolicy.defaultConfig = currentConfigId;
333 mMinSupportedRefreshRate = sortedConfigs.front();
334 mMaxSupportedRefreshRate = sortedConfigs.back();
335 constructAvailableRefreshRates();
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800336}
337
Steven Thomasd4071902020-03-24 16:02:53 -0700338bool RefreshRateConfigs::isPolicyValid(const Policy& policy) {
339 // defaultConfig must be a valid config, and within the given refresh rate range.
340 auto iter = mRefreshRates.find(policy.defaultConfig);
341 if (iter == mRefreshRates.end()) {
342 return false;
343 }
344 const RefreshRate& refreshRate = *iter->second;
345 if (!refreshRate.inPolicy(policy.minRefreshRate, policy.maxRefreshRate)) {
346 return false;
347 }
348 return true;
349}
350
351status_t RefreshRateConfigs::setDisplayManagerPolicy(const Policy& policy) {
Ady Abraham2139f732019-11-13 18:56:40 -0800352 std::lock_guard lock(mLock);
Steven Thomasd4071902020-03-24 16:02:53 -0700353 if (!isPolicyValid(policy)) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100354 return BAD_VALUE;
355 }
Steven Thomasd4071902020-03-24 16:02:53 -0700356 Policy previousPolicy = *getCurrentPolicyLocked();
357 mDisplayManagerPolicy = policy;
358 if (*getCurrentPolicyLocked() == previousPolicy) {
359 return CURRENT_POLICY_UNCHANGED;
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100360 }
Ady Abraham2139f732019-11-13 18:56:40 -0800361 constructAvailableRefreshRates();
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100362 return NO_ERROR;
363}
364
Steven Thomasd4071902020-03-24 16:02:53 -0700365status_t RefreshRateConfigs::setOverridePolicy(const std::optional<Policy>& policy) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100366 std::lock_guard lock(mLock);
Steven Thomasd4071902020-03-24 16:02:53 -0700367 if (policy && !isPolicyValid(*policy)) {
368 return BAD_VALUE;
369 }
370 Policy previousPolicy = *getCurrentPolicyLocked();
371 mOverridePolicy = policy;
372 if (*getCurrentPolicyLocked() == previousPolicy) {
373 return CURRENT_POLICY_UNCHANGED;
374 }
375 constructAvailableRefreshRates();
376 return NO_ERROR;
377}
378
379const RefreshRateConfigs::Policy* RefreshRateConfigs::getCurrentPolicyLocked() const {
380 return mOverridePolicy ? &mOverridePolicy.value() : &mDisplayManagerPolicy;
381}
382
383RefreshRateConfigs::Policy RefreshRateConfigs::getCurrentPolicy() const {
384 std::lock_guard lock(mLock);
385 return *getCurrentPolicyLocked();
386}
387
388RefreshRateConfigs::Policy RefreshRateConfigs::getDisplayManagerPolicy() const {
389 std::lock_guard lock(mLock);
390 return mDisplayManagerPolicy;
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100391}
392
393bool RefreshRateConfigs::isConfigAllowed(HwcConfigIndexType config) const {
394 std::lock_guard lock(mLock);
395 for (const RefreshRate* refreshRate : mAvailableRefreshRates) {
396 if (refreshRate->configId == config) {
397 return true;
398 }
399 }
400 return false;
Ady Abraham2139f732019-11-13 18:56:40 -0800401}
402
403void RefreshRateConfigs::getSortedRefreshRateList(
404 const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate,
405 std::vector<const RefreshRate*>* outRefreshRates) {
406 outRefreshRates->clear();
407 outRefreshRates->reserve(mRefreshRates.size());
408 for (const auto& [type, refreshRate] : mRefreshRates) {
Ady Abraham2e1dd892020-03-05 13:48:36 -0800409 if (shouldAddRefreshRate(*refreshRate)) {
Ady Abraham2139f732019-11-13 18:56:40 -0800410 ALOGV("getSortedRefreshRateList: config %d added to list policy",
Ady Abraham2e1dd892020-03-05 13:48:36 -0800411 refreshRate->configId.value());
412 outRefreshRates->push_back(refreshRate.get());
Ady Abraham2139f732019-11-13 18:56:40 -0800413 }
414 }
415
416 std::sort(outRefreshRates->begin(), outRefreshRates->end(),
417 [](const auto refreshRate1, const auto refreshRate2) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700418 if (refreshRate1->hwcConfig->getVsyncPeriod() !=
419 refreshRate2->hwcConfig->getVsyncPeriod()) {
420 return refreshRate1->hwcConfig->getVsyncPeriod() >
421 refreshRate2->hwcConfig->getVsyncPeriod();
Steven Thomasd4071902020-03-24 16:02:53 -0700422 } else {
Ady Abrahamabc27602020-04-08 17:20:29 -0700423 return refreshRate1->hwcConfig->getConfigGroup() >
424 refreshRate2->hwcConfig->getConfigGroup();
Steven Thomasd4071902020-03-24 16:02:53 -0700425 }
Ady Abraham2139f732019-11-13 18:56:40 -0800426 });
427}
428
429void RefreshRateConfigs::constructAvailableRefreshRates() {
430 // Filter configs based on current policy and sort based on vsync period
Steven Thomasd4071902020-03-24 16:02:53 -0700431 const Policy* policy = getCurrentPolicyLocked();
Ady Abrahamabc27602020-04-08 17:20:29 -0700432 const auto& defaultConfig = mRefreshRates.at(policy->defaultConfig)->hwcConfig;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800433 ALOGV("constructAvailableRefreshRates: default %d group %d min %.2f max %.2f",
Ady Abrahamabc27602020-04-08 17:20:29 -0700434 policy->defaultConfig.value(), defaultConfig->getConfigGroup(), policy->minRefreshRate,
Steven Thomasd4071902020-03-24 16:02:53 -0700435 policy->maxRefreshRate);
Ady Abraham2139f732019-11-13 18:56:40 -0800436 getSortedRefreshRateList(
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100437 [&](const RefreshRate& refreshRate) REQUIRES(mLock) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700438 const auto& hwcConfig = refreshRate.hwcConfig;
439
440 return hwcConfig->getHeight() == defaultConfig->getHeight() &&
441 hwcConfig->getWidth() == defaultConfig->getWidth() &&
442 hwcConfig->getDpiX() == defaultConfig->getDpiX() &&
443 hwcConfig->getDpiY() == defaultConfig->getDpiY() &&
444 (policy->allowGroupSwitching ||
445 hwcConfig->getConfigGroup() == defaultConfig->getConfigGroup()) &&
Steven Thomasd4071902020-03-24 16:02:53 -0700446 refreshRate.inPolicy(policy->minRefreshRate, policy->maxRefreshRate);
Ady Abraham2139f732019-11-13 18:56:40 -0800447 },
448 &mAvailableRefreshRates);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800449
450 std::string availableRefreshRates;
451 for (const auto& refreshRate : mAvailableRefreshRates) {
452 base::StringAppendF(&availableRefreshRates, "%s ", refreshRate->name.c_str());
453 }
454
455 ALOGV("Available refresh rates: %s", availableRefreshRates.c_str());
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100456 LOG_ALWAYS_FATAL_IF(mAvailableRefreshRates.empty(),
457 "No compatible display configs for default=%d min=%.0f max=%.0f",
Steven Thomasd4071902020-03-24 16:02:53 -0700458 policy->defaultConfig.value(), policy->minRefreshRate,
459 policy->maxRefreshRate);
Ady Abraham2139f732019-11-13 18:56:40 -0800460}
461
Ady Abraham2139f732019-11-13 18:56:40 -0800462} // namespace android::scheduler