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