Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 1 | /* |
| 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 Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 16 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 17 | // #define LOG_NDEBUG 0 |
| 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 19 | |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 20 | #include "RefreshRateConfigs.h" |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 21 | #include <android-base/stringprintf.h> |
| 22 | #include <utils/Trace.h> |
| 23 | #include <chrono> |
| 24 | #include <cmath> |
| 25 | |
Ady Abraham | 5b8afb5a | 2020-03-06 14:57:26 -0800 | [diff] [blame] | 26 | #undef LOG_TAG |
| 27 | #define LOG_TAG "RefreshRateConfigs" |
| 28 | |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 29 | namespace android::scheduler { |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame^] | 30 | namespace { |
| 31 | std::string formatLayerInfo(const RefreshRateConfigs::LayerRequirement& layer, float weight) { |
| 32 | return base::StringPrintf("%s (type=%s, weight=%.2f seamlessness=%s) %.2fHz", |
| 33 | layer.name.c_str(), |
| 34 | RefreshRateConfigs::layerVoteTypeString(layer.vote).c_str(), weight, |
| 35 | toString(layer.seamlessness).c_str(), layer.desiredRefreshRate); |
| 36 | } |
| 37 | } // namespace |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 38 | |
| 39 | using AllRefreshRatesMapType = RefreshRateConfigs::AllRefreshRatesMapType; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 40 | using RefreshRate = RefreshRateConfigs::RefreshRate; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 41 | |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 42 | std::string RefreshRate::toString() const { |
| 43 | return base::StringPrintf("{id=%d, hwcId=%d, fps=%.2f, width=%d, height=%d group=%d}", |
| 44 | getConfigId().value(), hwcConfig->getId(), getFps(), |
| 45 | hwcConfig->getWidth(), hwcConfig->getHeight(), getConfigGroup()); |
| 46 | } |
| 47 | |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 48 | std::string RefreshRateConfigs::layerVoteTypeString(LayerVoteType vote) { |
| 49 | switch (vote) { |
| 50 | case LayerVoteType::NoVote: |
| 51 | return "NoVote"; |
| 52 | case LayerVoteType::Min: |
| 53 | return "Min"; |
| 54 | case LayerVoteType::Max: |
| 55 | return "Max"; |
| 56 | case LayerVoteType::Heuristic: |
| 57 | return "Heuristic"; |
| 58 | case LayerVoteType::ExplicitDefault: |
| 59 | return "ExplicitDefault"; |
| 60 | case LayerVoteType::ExplicitExactOrMultiple: |
| 61 | return "ExplicitExactOrMultiple"; |
| 62 | } |
| 63 | } |
| 64 | |
Marin Shalamanov | b6674e7 | 2020-11-06 13:05:57 +0100 | [diff] [blame] | 65 | std::string RefreshRateConfigs::Policy::toString() const { |
Marin Shalamanov | 30b0b3c | 2020-10-13 19:15:06 +0200 | [diff] [blame] | 66 | return base::StringPrintf("default config ID: %d, allowGroupSwitching = %d" |
| 67 | ", primary range: [%.2f %.2f], app request range: [%.2f %.2f]", |
| 68 | defaultConfig.value(), allowGroupSwitching, primaryRange.min, |
| 69 | primaryRange.max, appRequestRange.min, appRequestRange.max); |
| 70 | } |
| 71 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 72 | const RefreshRate& RefreshRateConfigs::getRefreshRateForContent( |
| 73 | const std::vector<LayerRequirement>& layers) const { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 74 | std::lock_guard lock(mLock); |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 75 | int contentFramerate = 0; |
| 76 | int explicitContentFramerate = 0; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 77 | for (const auto& layer : layers) { |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 78 | const auto desiredRefreshRateRound = round<int>(layer.desiredRefreshRate); |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 79 | if (layer.vote == LayerVoteType::ExplicitDefault || |
| 80 | layer.vote == LayerVoteType::ExplicitExactOrMultiple) { |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 81 | if (desiredRefreshRateRound > explicitContentFramerate) { |
| 82 | explicitContentFramerate = desiredRefreshRateRound; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 83 | } |
| 84 | } else { |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 85 | if (desiredRefreshRateRound > contentFramerate) { |
| 86 | contentFramerate = desiredRefreshRateRound; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 91 | if (explicitContentFramerate != 0) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 92 | contentFramerate = explicitContentFramerate; |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 93 | } else if (contentFramerate == 0) { |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 94 | contentFramerate = round<int>(mMaxSupportedRefreshRate->getFps()); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 95 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 96 | ATRACE_INT("ContentFPS", contentFramerate); |
| 97 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 98 | // Find the appropriate refresh rate with minimal error |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 99 | auto iter = min_element(mPrimaryRefreshRates.cbegin(), mPrimaryRefreshRates.cend(), |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 100 | [contentFramerate](const auto& lhs, const auto& rhs) -> bool { |
| 101 | return std::abs(lhs->fps - contentFramerate) < |
| 102 | std::abs(rhs->fps - contentFramerate); |
| 103 | }); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 104 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 105 | // Some content aligns better on higher refresh rate. For example for 45fps we should choose |
| 106 | // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't |
| 107 | // align well with both |
| 108 | const RefreshRate* bestSoFar = *iter; |
| 109 | constexpr float MARGIN = 0.05f; |
| 110 | float ratio = (*iter)->fps / contentFramerate; |
| 111 | if (std::abs(std::round(ratio) - ratio) > MARGIN) { |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 112 | while (iter != mPrimaryRefreshRates.cend()) { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 113 | ratio = (*iter)->fps / contentFramerate; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 114 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 115 | if (std::abs(std::round(ratio) - ratio) <= MARGIN) { |
| 116 | bestSoFar = *iter; |
| 117 | break; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 118 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 119 | ++iter; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 123 | return *bestSoFar; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 126 | std::pair<nsecs_t, nsecs_t> RefreshRateConfigs::getDisplayFrames(nsecs_t layerPeriod, |
| 127 | nsecs_t displayPeriod) const { |
| 128 | auto [displayFramesQuot, displayFramesRem] = std::div(layerPeriod, displayPeriod); |
| 129 | if (displayFramesRem <= MARGIN_FOR_PERIOD_CALCULATION || |
| 130 | std::abs(displayFramesRem - displayPeriod) <= MARGIN_FOR_PERIOD_CALCULATION) { |
| 131 | displayFramesQuot++; |
| 132 | displayFramesRem = 0; |
| 133 | } |
| 134 | |
| 135 | return {displayFramesQuot, displayFramesRem}; |
| 136 | } |
| 137 | |
Steven Thomas | bb37432 | 2020-04-28 22:47:16 -0700 | [diff] [blame] | 138 | const RefreshRate& RefreshRateConfigs::getBestRefreshRate( |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 139 | const std::vector<LayerRequirement>& layers, const GlobalSignals& globalSignals, |
| 140 | GlobalSignals* outSignalsConsidered) const { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 141 | ATRACE_CALL(); |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 142 | ALOGV("getBestRefreshRate %zu layers", layers.size()); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 143 | |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 144 | if (outSignalsConsidered) *outSignalsConsidered = {}; |
| 145 | const auto setTouchConsidered = [&] { |
| 146 | if (outSignalsConsidered) { |
| 147 | outSignalsConsidered->touch = true; |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | const auto setIdleConsidered = [&] { |
| 152 | if (outSignalsConsidered) { |
| 153 | outSignalsConsidered->idle = true; |
| 154 | } |
| 155 | }; |
| 156 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 157 | std::lock_guard lock(mLock); |
| 158 | |
| 159 | int noVoteLayers = 0; |
| 160 | int minVoteLayers = 0; |
| 161 | int maxVoteLayers = 0; |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 162 | int explicitDefaultVoteLayers = 0; |
| 163 | int explicitExactOrMultipleVoteLayers = 0; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 164 | float maxExplicitWeight = 0; |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 165 | int seamedLayers = 0; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 166 | for (const auto& layer : layers) { |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 167 | if (layer.vote == LayerVoteType::NoVote) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 168 | noVoteLayers++; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 169 | } else if (layer.vote == LayerVoteType::Min) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 170 | minVoteLayers++; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 171 | } else if (layer.vote == LayerVoteType::Max) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 172 | maxVoteLayers++; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 173 | } else if (layer.vote == LayerVoteType::ExplicitDefault) { |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 174 | explicitDefaultVoteLayers++; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 175 | maxExplicitWeight = std::max(maxExplicitWeight, layer.weight); |
| 176 | } else if (layer.vote == LayerVoteType::ExplicitExactOrMultiple) { |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 177 | explicitExactOrMultipleVoteLayers++; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 178 | maxExplicitWeight = std::max(maxExplicitWeight, layer.weight); |
| 179 | } |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 180 | |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame^] | 181 | if (layer.seamlessness == Seamlessness::SeamedAndSeamless) { |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 182 | seamedLayers++; |
| 183 | } |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Alec Mouri | 11232a2 | 2020-05-14 18:06:25 -0700 | [diff] [blame] | 186 | const bool hasExplicitVoteLayers = |
| 187 | explicitDefaultVoteLayers > 0 || explicitExactOrMultipleVoteLayers > 0; |
| 188 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 189 | // Consider the touch event if there are no Explicit* layers. Otherwise wait until after we've |
| 190 | // selected a refresh rate to see if we should apply touch boost. |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 191 | if (globalSignals.touch && !hasExplicitVoteLayers) { |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 192 | ALOGV("TouchBoost - choose %s", getMaxRefreshRateByPolicyLocked().getName().c_str()); |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 193 | setTouchConsidered(); |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 194 | return getMaxRefreshRateByPolicyLocked(); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Alec Mouri | 11232a2 | 2020-05-14 18:06:25 -0700 | [diff] [blame] | 197 | // If the primary range consists of a single refresh rate then we can only |
| 198 | // move out the of range if layers explicitly request a different refresh |
| 199 | // rate. |
| 200 | const Policy* policy = getCurrentPolicyLocked(); |
| 201 | const bool primaryRangeIsSingleRate = policy->primaryRange.min == policy->primaryRange.max; |
| 202 | |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 203 | if (!globalSignals.touch && globalSignals.idle && |
| 204 | !(primaryRangeIsSingleRate && hasExplicitVoteLayers)) { |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 205 | ALOGV("Idle - choose %s", getMinRefreshRateByPolicyLocked().getName().c_str()); |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 206 | setIdleConsidered(); |
Steven Thomas | bb37432 | 2020-04-28 22:47:16 -0700 | [diff] [blame] | 207 | return getMinRefreshRateByPolicyLocked(); |
| 208 | } |
| 209 | |
Steven Thomas | debafed | 2020-05-18 17:30:35 -0700 | [diff] [blame] | 210 | if (layers.empty() || noVoteLayers == layers.size()) { |
| 211 | return getMaxRefreshRateByPolicyLocked(); |
Steven Thomas | bb37432 | 2020-04-28 22:47:16 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 214 | // Only if all layers want Min we should return Min |
| 215 | if (noVoteLayers + minVoteLayers == layers.size()) { |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 216 | ALOGV("all layers Min - choose %s", getMinRefreshRateByPolicyLocked().getName().c_str()); |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 217 | return getMinRefreshRateByPolicyLocked(); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 220 | // Find the best refresh rate based on score |
| 221 | std::vector<std::pair<const RefreshRate*, float>> scores; |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 222 | scores.reserve(mAppRequestRefreshRates.size()); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 223 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 224 | for (const auto refreshRate : mAppRequestRefreshRates) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 225 | scores.emplace_back(refreshRate, 0.0f); |
| 226 | } |
| 227 | |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 228 | const auto& defaultConfig = mRefreshRates.at(policy->defaultConfig); |
| 229 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 230 | for (const auto& layer : layers) { |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 231 | ALOGV("Calculating score for %s (%s, weight %.2f)", layer.name.c_str(), |
| 232 | layerVoteTypeString(layer.vote).c_str(), layer.weight); |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 233 | if (layer.vote == LayerVoteType::NoVote || layer.vote == LayerVoteType::Min) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 234 | continue; |
| 235 | } |
| 236 | |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 237 | auto weight = layer.weight; |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 238 | |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 239 | for (auto i = 0u; i < scores.size(); i++) { |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame^] | 240 | const bool isSeamlessSwitch = |
| 241 | scores[i].first->getConfigGroup() == mCurrentRefreshRate->getConfigGroup(); |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 242 | |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame^] | 243 | if (layer.seamlessness == Seamlessness::OnlySeamless && !isSeamlessSwitch) { |
| 244 | ALOGV("%s ignores %s to avoid non-seamless switch. Current config = %s", |
| 245 | formatLayerInfo(layer, weight).c_str(), scores[i].first->toString().c_str(), |
| 246 | mCurrentRefreshRate->toString().c_str()); |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 247 | continue; |
| 248 | } |
| 249 | |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame^] | 250 | if (layer.seamlessness == Seamlessness::SeamedAndSeamless && !isSeamlessSwitch && |
| 251 | !layer.focused) { |
| 252 | ALOGV("%s ignores %s because it's not focused and the switch is going to be seamed." |
| 253 | " Current config = %s", |
| 254 | formatLayerInfo(layer, weight).c_str(), scores[i].first->toString().c_str(), |
| 255 | mCurrentRefreshRate->toString().c_str()); |
| 256 | continue; |
| 257 | } |
| 258 | |
| 259 | // Layers with default seamlessness vote for the current config group if |
| 260 | // there are layers with seamlessness=SeamedAndSeamless and for the default |
| 261 | // config group otherwise. In second case, if the current config group is different |
| 262 | // from the default, this means a layer with seamlessness=SeamedAndSeamless has just |
| 263 | // disappeared. |
| 264 | const bool isInPolicyForDefault = seamedLayers > 0 |
| 265 | ? scores[i].first->getConfigGroup() == mCurrentRefreshRate->getConfigGroup() |
| 266 | : scores[i].first->getConfigGroup() == defaultConfig->getConfigGroup(); |
| 267 | |
| 268 | if (layer.seamlessness == Seamlessness::Default && !isInPolicyForDefault && |
| 269 | !layer.focused) { |
| 270 | ALOGV("%s ignores %s. Current config = %s", formatLayerInfo(layer, weight).c_str(), |
| 271 | scores[i].first->toString().c_str(), mCurrentRefreshRate->toString().c_str()); |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 272 | continue; |
| 273 | } |
| 274 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 275 | bool inPrimaryRange = |
| 276 | scores[i].first->inPolicy(policy->primaryRange.min, policy->primaryRange.max); |
Alec Mouri | 11232a2 | 2020-05-14 18:06:25 -0700 | [diff] [blame] | 277 | if ((primaryRangeIsSingleRate || !inPrimaryRange) && |
Ady Abraham | 20c029c | 2020-07-06 12:58:05 -0700 | [diff] [blame] | 278 | !(layer.focused && layer.vote == LayerVoteType::ExplicitDefault)) { |
| 279 | // Only focused layers with ExplicitDefault frame rate settings are allowed to score |
Ady Abraham | aae5ed5 | 2020-06-26 09:32:43 -0700 | [diff] [blame] | 280 | // refresh rates outside the primary range. |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 281 | continue; |
| 282 | } |
| 283 | |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 284 | // If the layer wants Max, give higher score to the higher refresh rate |
| 285 | if (layer.vote == LayerVoteType::Max) { |
| 286 | const auto ratio = scores[i].first->fps / scores.back().first->fps; |
| 287 | // use ratio^2 to get a lower score the more we get further from peak |
| 288 | const auto layerScore = ratio * ratio; |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame^] | 289 | ALOGV("%s gives %s score of %.2f", formatLayerInfo(layer, weight).c_str(), |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 290 | scores[i].first->name.c_str(), layerScore); |
| 291 | scores[i].second += weight * layerScore; |
| 292 | continue; |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 293 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 294 | |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 295 | const auto displayPeriod = scores[i].first->hwcConfig->getVsyncPeriod(); |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 296 | const auto layerPeriod = round<nsecs_t>(1e9f / layer.desiredRefreshRate); |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 297 | if (layer.vote == LayerVoteType::ExplicitDefault) { |
| 298 | const auto layerScore = [&]() { |
Ady Abraham | 5b8afb5a | 2020-03-06 14:57:26 -0800 | [diff] [blame] | 299 | // Find the actual rate the layer will render, assuming |
| 300 | // that layerPeriod is the minimal time to render a frame |
| 301 | auto actualLayerPeriod = displayPeriod; |
| 302 | int multiplier = 1; |
| 303 | while (layerPeriod > actualLayerPeriod + MARGIN_FOR_PERIOD_CALCULATION) { |
| 304 | multiplier++; |
| 305 | actualLayerPeriod = displayPeriod * multiplier; |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 306 | } |
Ady Abraham | 5b8afb5a | 2020-03-06 14:57:26 -0800 | [diff] [blame] | 307 | return std::min(1.0f, |
| 308 | static_cast<float>(layerPeriod) / |
| 309 | static_cast<float>(actualLayerPeriod)); |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 310 | }(); |
| 311 | |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame^] | 312 | ALOGV("%s gives %s score of %.2f", formatLayerInfo(layer, weight).c_str(), |
| 313 | scores[i].first->name.c_str(), layerScore); |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 314 | scores[i].second += weight * layerScore; |
| 315 | continue; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 316 | } |
| 317 | |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 318 | if (layer.vote == LayerVoteType::ExplicitExactOrMultiple || |
| 319 | layer.vote == LayerVoteType::Heuristic) { |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 320 | const auto layerScore = [&] { |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 321 | // Calculate how many display vsyncs we need to present a single frame for this |
| 322 | // layer |
| 323 | const auto [displayFramesQuot, displayFramesRem] = |
| 324 | getDisplayFrames(layerPeriod, displayPeriod); |
| 325 | static constexpr size_t MAX_FRAMES_TO_FIT = |
| 326 | 10; // Stop calculating when score < 0.1 |
| 327 | if (displayFramesRem == 0) { |
| 328 | // Layer desired refresh rate matches the display rate. |
| 329 | return 1.0f; |
| 330 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 331 | |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 332 | if (displayFramesQuot == 0) { |
| 333 | // Layer desired refresh rate is higher the display rate. |
| 334 | return (static_cast<float>(layerPeriod) / |
| 335 | static_cast<float>(displayPeriod)) * |
| 336 | (1.0f / (MAX_FRAMES_TO_FIT + 1)); |
| 337 | } |
| 338 | |
| 339 | // Layer desired refresh rate is lower the display rate. Check how well it fits |
| 340 | // the cadence |
| 341 | auto diff = std::abs(displayFramesRem - (displayPeriod - displayFramesRem)); |
| 342 | int iter = 2; |
| 343 | while (diff > MARGIN_FOR_PERIOD_CALCULATION && iter < MAX_FRAMES_TO_FIT) { |
| 344 | diff = diff - (displayPeriod - diff); |
| 345 | iter++; |
| 346 | } |
| 347 | |
| 348 | return 1.0f / iter; |
| 349 | }(); |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 350 | // Slightly prefer seamless switches. |
| 351 | constexpr float kSeamedSwitchPenalty = 0.95f; |
| 352 | const float seamlessness = isSeamlessSwitch ? 1.0f : kSeamedSwitchPenalty; |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame^] | 353 | ALOGV("%s gives %s score of %.2f", formatLayerInfo(layer, weight).c_str(), |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 354 | scores[i].first->name.c_str(), layerScore); |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 355 | scores[i].second += weight * layerScore * seamlessness; |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 356 | continue; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 357 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
Ady Abraham | 3470210 | 2020-02-10 14:12:05 -0800 | [diff] [blame] | 361 | // Now that we scored all the refresh rates we need to pick the one that got the highest score. |
| 362 | // In case of a tie we will pick the higher refresh rate if any of the layers wanted Max, |
| 363 | // or the lower otherwise. |
| 364 | const RefreshRate* bestRefreshRate = maxVoteLayers > 0 |
| 365 | ? getBestRefreshRate(scores.rbegin(), scores.rend()) |
| 366 | : getBestRefreshRate(scores.begin(), scores.end()); |
| 367 | |
Alec Mouri | 11232a2 | 2020-05-14 18:06:25 -0700 | [diff] [blame] | 368 | if (primaryRangeIsSingleRate) { |
| 369 | // If we never scored any layers, then choose the rate from the primary |
| 370 | // range instead of picking a random score from the app range. |
| 371 | if (std::all_of(scores.begin(), scores.end(), |
| 372 | [](std::pair<const RefreshRate*, float> p) { return p.second == 0; })) { |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 373 | ALOGV("layers not scored - choose %s", |
| 374 | getMaxRefreshRateByPolicyLocked().getName().c_str()); |
Alec Mouri | 11232a2 | 2020-05-14 18:06:25 -0700 | [diff] [blame] | 375 | return getMaxRefreshRateByPolicyLocked(); |
| 376 | } else { |
| 377 | return *bestRefreshRate; |
| 378 | } |
| 379 | } |
| 380 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 381 | // Consider the touch event if there are no ExplicitDefault layers. ExplicitDefault are mostly |
| 382 | // interactive (as opposed to ExplicitExactOrMultiple) and therefore if those posted an explicit |
| 383 | // vote we should not change it if we get a touch event. Only apply touch boost if it will |
| 384 | // actually increase the refresh rate over the normal selection. |
| 385 | const RefreshRate& touchRefreshRate = getMaxRefreshRateByPolicyLocked(); |
Alec Mouri | 11232a2 | 2020-05-14 18:06:25 -0700 | [diff] [blame] | 386 | |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 387 | if (globalSignals.touch && explicitDefaultVoteLayers == 0 && |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 388 | bestRefreshRate->fps < touchRefreshRate.fps) { |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 389 | setTouchConsidered(); |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 390 | ALOGV("TouchBoost - choose %s", touchRefreshRate.getName().c_str()); |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 391 | return touchRefreshRate; |
| 392 | } |
| 393 | |
Ady Abraham | de7156e | 2020-02-28 17:29:39 -0800 | [diff] [blame] | 394 | return *bestRefreshRate; |
Ady Abraham | 3470210 | 2020-02-10 14:12:05 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | template <typename Iter> |
| 398 | const RefreshRate* RefreshRateConfigs::getBestRefreshRate(Iter begin, Iter end) const { |
Ady Abraham | 5b8afb5a | 2020-03-06 14:57:26 -0800 | [diff] [blame] | 399 | constexpr auto EPSILON = 0.001f; |
Ady Abraham | de7156e | 2020-02-28 17:29:39 -0800 | [diff] [blame] | 400 | const RefreshRate* bestRefreshRate = begin->first; |
| 401 | float max = begin->second; |
Ady Abraham | 3470210 | 2020-02-10 14:12:05 -0800 | [diff] [blame] | 402 | for (auto i = begin; i != end; ++i) { |
| 403 | const auto [refreshRate, score] = *i; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 404 | ALOGV("%s scores %.2f", refreshRate->name.c_str(), score); |
| 405 | |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 406 | ATRACE_INT(refreshRate->name.c_str(), round<int>(score * 100)); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 407 | |
Ady Abraham | 5b8afb5a | 2020-03-06 14:57:26 -0800 | [diff] [blame] | 408 | if (score > max * (1 + EPSILON)) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 409 | max = score; |
| 410 | bestRefreshRate = refreshRate; |
| 411 | } |
| 412 | } |
| 413 | |
Ady Abraham | 3470210 | 2020-02-10 14:12:05 -0800 | [diff] [blame] | 414 | return bestRefreshRate; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 415 | } |
| 416 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 417 | const AllRefreshRatesMapType& RefreshRateConfigs::getAllRefreshRates() const { |
| 418 | return mRefreshRates; |
| 419 | } |
| 420 | |
| 421 | const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicy() const { |
| 422 | std::lock_guard lock(mLock); |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 423 | return getMinRefreshRateByPolicyLocked(); |
| 424 | } |
| 425 | |
| 426 | const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicyLocked() const { |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 427 | for (auto refreshRate : mPrimaryRefreshRates) { |
| 428 | if (mCurrentRefreshRate->getConfigGroup() == refreshRate->getConfigGroup()) { |
| 429 | return *refreshRate; |
| 430 | } |
| 431 | } |
| 432 | ALOGE("Can't find min refresh rate by policy with the same config group" |
| 433 | " as the current config %s", |
| 434 | mCurrentRefreshRate->toString().c_str()); |
| 435 | // Defaulting to the lowest refresh rate |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 436 | return *mPrimaryRefreshRates.front(); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicy() const { |
| 440 | std::lock_guard lock(mLock); |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 441 | return getMaxRefreshRateByPolicyLocked(); |
| 442 | } |
| 443 | |
| 444 | const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicyLocked() const { |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 445 | for (auto it = mPrimaryRefreshRates.rbegin(); it != mPrimaryRefreshRates.rend(); it++) { |
| 446 | const auto& refreshRate = (**it); |
| 447 | if (mCurrentRefreshRate->getConfigGroup() == refreshRate.getConfigGroup()) { |
| 448 | return refreshRate; |
| 449 | } |
| 450 | } |
| 451 | ALOGE("Can't find max refresh rate by policy with the same config group" |
| 452 | " as the current config %s", |
| 453 | mCurrentRefreshRate->toString().c_str()); |
| 454 | // Defaulting to the highest refresh rate |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 455 | return *mPrimaryRefreshRates.back(); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | const RefreshRate& RefreshRateConfigs::getCurrentRefreshRate() const { |
| 459 | std::lock_guard lock(mLock); |
| 460 | return *mCurrentRefreshRate; |
| 461 | } |
| 462 | |
Ana Krulec | 5d47791 | 2020-02-07 12:02:38 -0800 | [diff] [blame] | 463 | const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicy() const { |
| 464 | std::lock_guard lock(mLock); |
Ana Krulec | 3d367c8 | 2020-02-25 15:02:01 -0800 | [diff] [blame] | 465 | return getCurrentRefreshRateByPolicyLocked(); |
| 466 | } |
| 467 | |
| 468 | const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicyLocked() const { |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 469 | if (std::find(mAppRequestRefreshRates.begin(), mAppRequestRefreshRates.end(), |
| 470 | mCurrentRefreshRate) != mAppRequestRefreshRates.end()) { |
Ana Krulec | 5d47791 | 2020-02-07 12:02:38 -0800 | [diff] [blame] | 471 | return *mCurrentRefreshRate; |
| 472 | } |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 473 | return *mRefreshRates.at(getCurrentPolicyLocked()->defaultConfig); |
Ana Krulec | 5d47791 | 2020-02-07 12:02:38 -0800 | [diff] [blame] | 474 | } |
| 475 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 476 | void RefreshRateConfigs::setCurrentConfigId(HwcConfigIndexType configId) { |
| 477 | std::lock_guard lock(mLock); |
Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 478 | mCurrentRefreshRate = mRefreshRates.at(configId).get(); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 479 | } |
| 480 | |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 481 | RefreshRateConfigs::RefreshRateConfigs( |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 482 | const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs, |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 483 | HwcConfigIndexType currentConfigId) |
| 484 | : mKnownFrameRates(constructKnownFrameRates(configs)) { |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 485 | LOG_ALWAYS_FATAL_IF(configs.empty()); |
| 486 | LOG_ALWAYS_FATAL_IF(currentConfigId.value() >= configs.size()); |
| 487 | |
| 488 | for (auto configId = HwcConfigIndexType(0); configId.value() < configs.size(); configId++) { |
| 489 | const auto& config = configs.at(static_cast<size_t>(configId.value())); |
| 490 | const float fps = 1e9f / config->getVsyncPeriod(); |
| 491 | mRefreshRates.emplace(configId, |
| 492 | std::make_unique<RefreshRate>(configId, config, |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 493 | base::StringPrintf("%.2ffps", fps), fps, |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 494 | RefreshRate::ConstructorTag(0))); |
| 495 | if (configId == currentConfigId) { |
| 496 | mCurrentRefreshRate = mRefreshRates.at(configId).get(); |
| 497 | } |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 498 | } |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 499 | |
| 500 | std::vector<const RefreshRate*> sortedConfigs; |
| 501 | getSortedRefreshRateList([](const RefreshRate&) { return true; }, &sortedConfigs); |
| 502 | mDisplayManagerPolicy.defaultConfig = currentConfigId; |
| 503 | mMinSupportedRefreshRate = sortedConfigs.front(); |
| 504 | mMaxSupportedRefreshRate = sortedConfigs.back(); |
| 505 | constructAvailableRefreshRates(); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 508 | bool RefreshRateConfigs::isPolicyValid(const Policy& policy) { |
| 509 | // defaultConfig must be a valid config, and within the given refresh rate range. |
| 510 | auto iter = mRefreshRates.find(policy.defaultConfig); |
| 511 | if (iter == mRefreshRates.end()) { |
Marin Shalamanov | b6674e7 | 2020-11-06 13:05:57 +0100 | [diff] [blame] | 512 | ALOGE("Default config is not found."); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 513 | return false; |
| 514 | } |
| 515 | const RefreshRate& refreshRate = *iter->second; |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 516 | if (!refreshRate.inPolicy(policy.primaryRange.min, policy.primaryRange.max)) { |
Marin Shalamanov | b6674e7 | 2020-11-06 13:05:57 +0100 | [diff] [blame] | 517 | ALOGE("Default config is not in the primary range."); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 518 | return false; |
| 519 | } |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 520 | return policy.appRequestRange.min <= policy.primaryRange.min && |
| 521 | policy.appRequestRange.max >= policy.primaryRange.max; |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | status_t RefreshRateConfigs::setDisplayManagerPolicy(const Policy& policy) { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 525 | std::lock_guard lock(mLock); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 526 | if (!isPolicyValid(policy)) { |
Marin Shalamanov | b6674e7 | 2020-11-06 13:05:57 +0100 | [diff] [blame] | 527 | ALOGE("Invalid refresh rate policy: %s", policy.toString().c_str()); |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 528 | return BAD_VALUE; |
| 529 | } |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 530 | Policy previousPolicy = *getCurrentPolicyLocked(); |
| 531 | mDisplayManagerPolicy = policy; |
| 532 | if (*getCurrentPolicyLocked() == previousPolicy) { |
| 533 | return CURRENT_POLICY_UNCHANGED; |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 534 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 535 | constructAvailableRefreshRates(); |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 536 | return NO_ERROR; |
| 537 | } |
| 538 | |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 539 | status_t RefreshRateConfigs::setOverridePolicy(const std::optional<Policy>& policy) { |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 540 | std::lock_guard lock(mLock); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 541 | if (policy && !isPolicyValid(*policy)) { |
| 542 | return BAD_VALUE; |
| 543 | } |
| 544 | Policy previousPolicy = *getCurrentPolicyLocked(); |
| 545 | mOverridePolicy = policy; |
| 546 | if (*getCurrentPolicyLocked() == previousPolicy) { |
| 547 | return CURRENT_POLICY_UNCHANGED; |
| 548 | } |
| 549 | constructAvailableRefreshRates(); |
| 550 | return NO_ERROR; |
| 551 | } |
| 552 | |
| 553 | const RefreshRateConfigs::Policy* RefreshRateConfigs::getCurrentPolicyLocked() const { |
| 554 | return mOverridePolicy ? &mOverridePolicy.value() : &mDisplayManagerPolicy; |
| 555 | } |
| 556 | |
| 557 | RefreshRateConfigs::Policy RefreshRateConfigs::getCurrentPolicy() const { |
| 558 | std::lock_guard lock(mLock); |
| 559 | return *getCurrentPolicyLocked(); |
| 560 | } |
| 561 | |
| 562 | RefreshRateConfigs::Policy RefreshRateConfigs::getDisplayManagerPolicy() const { |
| 563 | std::lock_guard lock(mLock); |
| 564 | return mDisplayManagerPolicy; |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | bool RefreshRateConfigs::isConfigAllowed(HwcConfigIndexType config) const { |
| 568 | std::lock_guard lock(mLock); |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 569 | for (const RefreshRate* refreshRate : mAppRequestRefreshRates) { |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 570 | if (refreshRate->configId == config) { |
| 571 | return true; |
| 572 | } |
| 573 | } |
| 574 | return false; |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | void RefreshRateConfigs::getSortedRefreshRateList( |
| 578 | const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate, |
| 579 | std::vector<const RefreshRate*>* outRefreshRates) { |
| 580 | outRefreshRates->clear(); |
| 581 | outRefreshRates->reserve(mRefreshRates.size()); |
| 582 | for (const auto& [type, refreshRate] : mRefreshRates) { |
Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 583 | if (shouldAddRefreshRate(*refreshRate)) { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 584 | ALOGV("getSortedRefreshRateList: config %d added to list policy", |
Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 585 | refreshRate->configId.value()); |
| 586 | outRefreshRates->push_back(refreshRate.get()); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
| 590 | std::sort(outRefreshRates->begin(), outRefreshRates->end(), |
| 591 | [](const auto refreshRate1, const auto refreshRate2) { |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 592 | if (refreshRate1->hwcConfig->getVsyncPeriod() != |
| 593 | refreshRate2->hwcConfig->getVsyncPeriod()) { |
| 594 | return refreshRate1->hwcConfig->getVsyncPeriod() > |
| 595 | refreshRate2->hwcConfig->getVsyncPeriod(); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 596 | } else { |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 597 | return refreshRate1->hwcConfig->getConfigGroup() > |
| 598 | refreshRate2->hwcConfig->getConfigGroup(); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 599 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 600 | }); |
| 601 | } |
| 602 | |
| 603 | void RefreshRateConfigs::constructAvailableRefreshRates() { |
| 604 | // Filter configs based on current policy and sort based on vsync period |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 605 | const Policy* policy = getCurrentPolicyLocked(); |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 606 | const auto& defaultConfig = mRefreshRates.at(policy->defaultConfig)->hwcConfig; |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 607 | ALOGV("constructAvailableRefreshRates: default %d group %d primaryRange=[%.2f %.2f]" |
| 608 | " appRequestRange=[%.2f %.2f]", |
| 609 | policy->defaultConfig.value(), defaultConfig->getConfigGroup(), policy->primaryRange.min, |
| 610 | policy->primaryRange.max, policy->appRequestRange.min, policy->appRequestRange.max); |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 611 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 612 | auto filterRefreshRates = [&](float min, float max, const char* listName, |
| 613 | std::vector<const RefreshRate*>* outRefreshRates) { |
| 614 | getSortedRefreshRateList( |
| 615 | [&](const RefreshRate& refreshRate) REQUIRES(mLock) { |
| 616 | const auto& hwcConfig = refreshRate.hwcConfig; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 617 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 618 | return hwcConfig->getHeight() == defaultConfig->getHeight() && |
| 619 | hwcConfig->getWidth() == defaultConfig->getWidth() && |
| 620 | hwcConfig->getDpiX() == defaultConfig->getDpiX() && |
| 621 | hwcConfig->getDpiY() == defaultConfig->getDpiY() && |
| 622 | (policy->allowGroupSwitching || |
| 623 | hwcConfig->getConfigGroup() == defaultConfig->getConfigGroup()) && |
| 624 | refreshRate.inPolicy(min, max); |
| 625 | }, |
| 626 | outRefreshRates); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 627 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 628 | LOG_ALWAYS_FATAL_IF(outRefreshRates->empty(), |
| 629 | "No matching configs for %s range: min=%.0f max=%.0f", listName, min, |
| 630 | max); |
| 631 | auto stringifyRefreshRates = [&]() -> std::string { |
| 632 | std::string str; |
| 633 | for (auto refreshRate : *outRefreshRates) { |
| 634 | base::StringAppendF(&str, "%s ", refreshRate->name.c_str()); |
| 635 | } |
| 636 | return str; |
| 637 | }; |
| 638 | ALOGV("%s refresh rates: %s", listName, stringifyRefreshRates().c_str()); |
| 639 | }; |
| 640 | |
| 641 | filterRefreshRates(policy->primaryRange.min, policy->primaryRange.max, "primary", |
| 642 | &mPrimaryRefreshRates); |
| 643 | filterRefreshRates(policy->appRequestRange.min, policy->appRequestRange.max, "app request", |
| 644 | &mAppRequestRefreshRates); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 645 | } |
| 646 | |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 647 | std::vector<float> RefreshRateConfigs::constructKnownFrameRates( |
| 648 | const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs) { |
| 649 | std::vector<float> knownFrameRates = {24.0f, 30.0f, 45.0f, 60.0f, 72.0f}; |
| 650 | knownFrameRates.reserve(knownFrameRates.size() + configs.size()); |
| 651 | |
| 652 | // Add all supported refresh rates to the set |
| 653 | for (const auto& config : configs) { |
| 654 | const auto refreshRate = 1e9f / config->getVsyncPeriod(); |
| 655 | knownFrameRates.emplace_back(refreshRate); |
| 656 | } |
| 657 | |
| 658 | // Sort and remove duplicates |
| 659 | const auto frameRatesEqual = [](float a, float b) { return std::abs(a - b) <= 0.01f; }; |
| 660 | std::sort(knownFrameRates.begin(), knownFrameRates.end()); |
| 661 | knownFrameRates.erase(std::unique(knownFrameRates.begin(), knownFrameRates.end(), |
| 662 | frameRatesEqual), |
| 663 | knownFrameRates.end()); |
| 664 | return knownFrameRates; |
| 665 | } |
| 666 | |
| 667 | float RefreshRateConfigs::findClosestKnownFrameRate(float frameRate) const { |
| 668 | if (frameRate <= *mKnownFrameRates.begin()) { |
| 669 | return *mKnownFrameRates.begin(); |
| 670 | } |
| 671 | |
| 672 | if (frameRate >= *std::prev(mKnownFrameRates.end())) { |
| 673 | return *std::prev(mKnownFrameRates.end()); |
| 674 | } |
| 675 | |
| 676 | auto lowerBound = std::lower_bound(mKnownFrameRates.begin(), mKnownFrameRates.end(), frameRate); |
| 677 | |
| 678 | const auto distance1 = std::abs(frameRate - *lowerBound); |
| 679 | const auto distance2 = std::abs(frameRate - *std::prev(lowerBound)); |
| 680 | return distance1 < distance2 ? *lowerBound : *std::prev(lowerBound); |
| 681 | } |
| 682 | |
Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 683 | RefreshRateConfigs::KernelIdleTimerAction RefreshRateConfigs::getIdleTimerAction() const { |
| 684 | std::lock_guard lock(mLock); |
| 685 | const auto& deviceMin = getMinRefreshRate(); |
| 686 | const auto& minByPolicy = getMinRefreshRateByPolicyLocked(); |
| 687 | const auto& maxByPolicy = getMaxRefreshRateByPolicyLocked(); |
| 688 | |
| 689 | // Kernel idle timer will set the refresh rate to the device min. If DisplayManager says that |
| 690 | // the min allowed refresh rate is higher than the device min, we do not want to enable the |
| 691 | // timer. |
| 692 | if (deviceMin < minByPolicy) { |
| 693 | return RefreshRateConfigs::KernelIdleTimerAction::TurnOff; |
| 694 | } |
| 695 | if (minByPolicy == maxByPolicy) { |
| 696 | // Do not sent the call to toggle off kernel idle timer if the device min and policy min and |
| 697 | // max are all the same. This saves us extra unnecessary calls to sysprop. |
| 698 | if (deviceMin == minByPolicy) { |
| 699 | return RefreshRateConfigs::KernelIdleTimerAction::NoChange; |
| 700 | } |
| 701 | return RefreshRateConfigs::KernelIdleTimerAction::TurnOff; |
| 702 | } |
| 703 | // Turn on the timer in all other cases. |
| 704 | return RefreshRateConfigs::KernelIdleTimerAction::TurnOn; |
| 705 | } |
| 706 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 707 | void RefreshRateConfigs::setPreferredRefreshRateForUid(uid_t uid, float refreshRateHz) { |
| 708 | if (refreshRateHz > 0 && refreshRateHz < 1) { |
| 709 | return; |
| 710 | } |
| 711 | |
| 712 | std::lock_guard lock(mLock); |
| 713 | if (refreshRateHz != 0) { |
| 714 | mPreferredRefreshRateForUid[uid] = refreshRateHz; |
| 715 | } else { |
| 716 | mPreferredRefreshRateForUid.erase(uid); |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | int RefreshRateConfigs::getRefreshRateDividerForUid(uid_t uid) const { |
| 721 | constexpr float kThreshold = 0.1f; |
| 722 | std::lock_guard lock(mLock); |
| 723 | |
| 724 | const auto iter = mPreferredRefreshRateForUid.find(uid); |
| 725 | if (iter == mPreferredRefreshRateForUid.end()) { |
| 726 | return 1; |
| 727 | } |
| 728 | |
| 729 | const auto refreshRateHz = iter->second; |
| 730 | const auto numPeriods = mCurrentRefreshRate->getFps() / refreshRateHz; |
| 731 | const auto numPeriodsRounded = std::round(numPeriods); |
| 732 | if (std::abs(numPeriods - numPeriodsRounded) > kThreshold) { |
| 733 | return 1; |
| 734 | } |
| 735 | |
| 736 | return static_cast<int>(numPeriods); |
| 737 | } |
| 738 | |
Marin Shalamanov | ba421a8 | 2020-11-10 21:49:26 +0100 | [diff] [blame] | 739 | void RefreshRateConfigs::dump(std::string& result) const { |
| 740 | std::lock_guard lock(mLock); |
| 741 | base::StringAppendF(&result, "DesiredDisplayConfigSpecs (DisplayManager): %s\n\n", |
| 742 | mDisplayManagerPolicy.toString().c_str()); |
| 743 | scheduler::RefreshRateConfigs::Policy currentPolicy = *getCurrentPolicyLocked(); |
| 744 | if (mOverridePolicy && currentPolicy != mDisplayManagerPolicy) { |
| 745 | base::StringAppendF(&result, "DesiredDisplayConfigSpecs (Override): %s\n\n", |
| 746 | currentPolicy.toString().c_str()); |
| 747 | } |
| 748 | |
| 749 | auto config = mCurrentRefreshRate->hwcConfig; |
| 750 | base::StringAppendF(&result, "Current config: %s\n", mCurrentRefreshRate->toString().c_str()); |
| 751 | |
| 752 | result.append("Refresh rates:\n"); |
| 753 | for (const auto& [id, refreshRate] : mRefreshRates) { |
| 754 | config = refreshRate->hwcConfig; |
| 755 | base::StringAppendF(&result, "\t%s\n", refreshRate->toString().c_str()); |
| 756 | } |
| 757 | |
| 758 | result.append("\n"); |
| 759 | } |
| 760 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 761 | } // namespace android::scheduler |