Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 | */ |
| 16 | |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wextra" |
| 20 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 21 | // #define LOG_NDEBUG 0 |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 22 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 23 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 24 | #include "LayerInfo.h" |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 25 | |
| 26 | #include <algorithm> |
| 27 | #include <utility> |
| 28 | |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 29 | #include <cutils/compiler.h> |
| 30 | #include <cutils/trace.h> |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 31 | #include <ftl/enum.h> |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 32 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 33 | #undef LOG_TAG |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 34 | #define LOG_TAG "LayerInfo" |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 35 | |
| 36 | namespace android::scheduler { |
| 37 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 38 | bool LayerInfo::sTraceEnabled = false; |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 39 | |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 40 | LayerInfo::LayerInfo(const std::string& name, uid_t ownerUid, |
| 41 | LayerHistory::LayerVoteType defaultVote) |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 42 | : mName(name), |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 43 | mOwnerUid(ownerUid), |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 44 | mDefaultVote(defaultVote), |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 45 | mLayerVote({defaultVote, Fps()}), |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 46 | mRefreshRateHistory(name) {} |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 47 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 48 | void LayerInfo::setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, LayerUpdateType updateType, |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 49 | bool pendingModeChange, LayerProps props) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 50 | lastPresentTime = std::max(lastPresentTime, static_cast<nsecs_t>(0)); |
| 51 | |
| 52 | mLastUpdatedTime = std::max(lastPresentTime, now); |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 53 | mLayerProps = props; |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 54 | switch (updateType) { |
| 55 | case LayerUpdateType::AnimationTX: |
| 56 | mLastAnimationTime = std::max(lastPresentTime, now); |
| 57 | break; |
| 58 | case LayerUpdateType::SetFrameRate: |
| 59 | case LayerUpdateType::Buffer: |
Marin Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame] | 60 | FrameTimeData frameTime = {.presentTime = lastPresentTime, |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 61 | .queueTime = mLastUpdatedTime, |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 62 | .pendingModeChange = pendingModeChange}; |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 63 | mFrameTimes.push_back(frameTime); |
| 64 | if (mFrameTimes.size() > HISTORY_SIZE) { |
| 65 | mFrameTimes.pop_front(); |
| 66 | } |
| 67 | break; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 71 | bool LayerInfo::isFrameTimeValid(const FrameTimeData& frameTime) const { |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 72 | return frameTime.queueTime >= std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 73 | mFrameTimeValidSince.time_since_epoch()) |
| 74 | .count(); |
| 75 | } |
Ady Abraham | 1adbb72 | 2020-05-15 11:51:48 -0700 | [diff] [blame] | 76 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 77 | bool LayerInfo::isFrequent(nsecs_t now) const { |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 78 | // If we know nothing about this layer we consider it as frequent as it might be the start |
| 79 | // of an animation. |
Marin Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame] | 80 | if (mFrameTimes.size() < kFrequentLayerWindowSize) { |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 81 | return true; |
| 82 | } |
| 83 | |
| 84 | // Find the first active frame |
Ady Abraham | 983e568 | 2020-05-28 16:49:18 -0700 | [diff] [blame] | 85 | auto it = mFrameTimes.begin(); |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 86 | for (; it != mFrameTimes.end(); ++it) { |
| 87 | if (it->queueTime >= getActiveLayerThreshold(now)) { |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | const auto numFrames = std::distance(it, mFrameTimes.end()); |
Marin Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame] | 93 | if (numFrames < kFrequentLayerWindowSize) { |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 94 | return false; |
| 95 | } |
| 96 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 97 | using fps_approx_ops::operator>=; |
| 98 | |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 99 | // Layer is considered frequent if the average frame rate is higher than the threshold |
| 100 | const auto totalTime = mFrameTimes.back().queueTime - it->queueTime; |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 101 | return Fps::fromPeriodNsecs(totalTime / (numFrames - 1)) >= kMinFpsForFrequentLayer; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 104 | bool LayerInfo::isAnimating(nsecs_t now) const { |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 105 | return mLastAnimationTime >= getActiveLayerThreshold(now); |
| 106 | } |
| 107 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 108 | bool LayerInfo::hasEnoughDataForHeuristic() const { |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 109 | // The layer had to publish at least HISTORY_SIZE or HISTORY_DURATION of updates |
Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 110 | if (mFrameTimes.size() < 2) { |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 111 | ALOGV("fewer than 2 frames recorded: %zu", mFrameTimes.size()); |
Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 112 | return false; |
| 113 | } |
| 114 | |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 115 | if (!isFrameTimeValid(mFrameTimes.front())) { |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 116 | ALOGV("stale frames still captured"); |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 117 | return false; |
| 118 | } |
| 119 | |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 120 | const auto totalDuration = mFrameTimes.back().queueTime - mFrameTimes.front().queueTime; |
| 121 | if (mFrameTimes.size() < HISTORY_SIZE && totalDuration < HISTORY_DURATION.count()) { |
| 122 | ALOGV("not enough frames captured: %zu | %.2f seconds", mFrameTimes.size(), |
| 123 | totalDuration / 1e9f); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 124 | return false; |
| 125 | } |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 130 | std::optional<nsecs_t> LayerInfo::calculateAverageFrameTime() const { |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 131 | // Ignore frames captured during a mode change |
| 132 | const bool isDuringModeChange = |
Marin Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame] | 133 | std::any_of(mFrameTimes.begin(), mFrameTimes.end(), |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 134 | [](const auto& frame) { return frame.pendingModeChange; }); |
| 135 | if (isDuringModeChange) { |
Marin Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame] | 136 | return std::nullopt; |
| 137 | } |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 138 | |
Marin Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame] | 139 | const bool isMissingPresentTime = |
| 140 | std::any_of(mFrameTimes.begin(), mFrameTimes.end(), |
| 141 | [](auto frame) { return frame.presentTime == 0; }); |
| 142 | if (isMissingPresentTime && !mLastRefreshRate.reported.isValid()) { |
| 143 | // If there are no presentation timestamps and we haven't calculated |
| 144 | // one in the past then we can't calculate the refresh rate |
| 145 | return std::nullopt; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 146 | } |
Ady Abraham | c966483 | 2020-05-12 14:16:56 -0700 | [diff] [blame] | 147 | |
Ady Abraham | c966483 | 2020-05-12 14:16:56 -0700 | [diff] [blame] | 148 | // Calculate the average frame time based on presentation timestamps. If those |
| 149 | // doesn't exist, we look at the time the buffer was queued only. We can do that only if |
| 150 | // we calculated a refresh rate based on presentation timestamps in the past. The reason |
| 151 | // we look at the queue time is to handle cases where hwui attaches presentation timestamps |
| 152 | // when implementing render ahead for specific refresh rates. When hwui no longer provides |
| 153 | // presentation timestamps we look at the queue time to see if the current refresh rate still |
| 154 | // matches the content. |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 155 | |
Marin Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame] | 156 | auto getFrameTime = isMissingPresentTime ? [](FrameTimeData data) { return data.queueTime; } |
| 157 | : [](FrameTimeData data) { return data.presentTime; }; |
| 158 | |
| 159 | nsecs_t totalDeltas = 0; |
| 160 | int numDeltas = 0; |
| 161 | auto prevFrame = mFrameTimes.begin(); |
| 162 | for (auto it = mFrameTimes.begin() + 1; it != mFrameTimes.end(); ++it) { |
| 163 | const auto currDelta = getFrameTime(*it) - getFrameTime(*prevFrame); |
| 164 | if (currDelta < kMinPeriodBetweenFrames) { |
| 165 | // Skip this frame, but count the delta into the next frame |
| 166 | continue; |
| 167 | } |
| 168 | |
| 169 | prevFrame = it; |
| 170 | |
| 171 | if (currDelta > kMaxPeriodBetweenFrames) { |
| 172 | // Skip this frame and the current delta. |
| 173 | continue; |
| 174 | } |
| 175 | |
| 176 | totalDeltas += currDelta; |
| 177 | numDeltas++; |
| 178 | } |
| 179 | |
| 180 | if (numDeltas == 0) { |
| 181 | return std::nullopt; |
| 182 | } |
| 183 | |
| 184 | const auto averageFrameTime = static_cast<double>(totalDeltas) / static_cast<double>(numDeltas); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 185 | return static_cast<nsecs_t>(averageFrameTime); |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 186 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 187 | |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 188 | std::optional<Fps> LayerInfo::calculateRefreshRateIfPossible( |
| 189 | const RefreshRateConfigs& refreshRateConfigs, nsecs_t now) { |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 190 | static constexpr float MARGIN = 1.0f; // 1Hz |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 191 | if (!hasEnoughDataForHeuristic()) { |
| 192 | ALOGV("Not enough data"); |
| 193 | return std::nullopt; |
| 194 | } |
| 195 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 196 | if (const auto averageFrameTime = calculateAverageFrameTime()) { |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 197 | const auto refreshRate = Fps::fromPeriodNsecs(*averageFrameTime); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 198 | const bool refreshRateConsistent = mRefreshRateHistory.add(refreshRate, now); |
| 199 | if (refreshRateConsistent) { |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 200 | const auto knownRefreshRate = refreshRateConfigs.findClosestKnownFrameRate(refreshRate); |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 201 | using fps_approx_ops::operator!=; |
| 202 | |
| 203 | // To avoid oscillation, use the last calculated refresh rate if it is close enough. |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 204 | if (std::abs(mLastRefreshRate.calculated.getValue() - refreshRate.getValue()) > |
| 205 | MARGIN && |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 206 | mLastRefreshRate.reported != knownRefreshRate) { |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 207 | mLastRefreshRate.calculated = refreshRate; |
| 208 | mLastRefreshRate.reported = knownRefreshRate; |
| 209 | } |
| 210 | |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 211 | ALOGV("%s %s rounded to nearest known frame rate %s", mName.c_str(), |
| 212 | to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str()); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 213 | } else { |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 214 | ALOGV("%s Not stable (%s) returning last known frame rate %s", mName.c_str(), |
| 215 | to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str()); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 216 | } |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 219 | return mLastRefreshRate.reported.isValid() ? std::make_optional(mLastRefreshRate.reported) |
| 220 | : std::nullopt; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 223 | LayerInfo::LayerVote LayerInfo::getRefreshRateVote(const RefreshRateConfigs& refreshRateConfigs, |
| 224 | nsecs_t now) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 225 | if (mLayerVote.type != LayerHistory::LayerVoteType::Heuristic) { |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 226 | ALOGV("%s voted %d ", mName.c_str(), static_cast<int>(mLayerVote.type)); |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 227 | return mLayerVote; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 230 | if (isAnimating(now)) { |
| 231 | ALOGV("%s is animating", mName.c_str()); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 232 | mLastRefreshRate.animatingOrInfrequent = true; |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 233 | return {LayerHistory::LayerVoteType::Max, Fps()}; |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 236 | if (!isFrequent(now)) { |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 237 | ALOGV("%s is infrequent", mName.c_str()); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 238 | mLastRefreshRate.animatingOrInfrequent = true; |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 239 | // Infrequent layers vote for minimal refresh rate for |
Marin Shalamanov | 29e2540 | 2021-04-07 21:09:58 +0200 | [diff] [blame] | 240 | // battery saving purposes and also to prevent b/135718869. |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 241 | return {LayerHistory::LayerVoteType::Min, Fps()}; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 244 | // If the layer was previously tagged as animating or infrequent, we clear |
| 245 | // the history as it is likely the layer just changed its behavior |
| 246 | // and we should not look at stale data |
| 247 | if (mLastRefreshRate.animatingOrInfrequent) { |
| 248 | clearHistory(now); |
| 249 | } |
| 250 | |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 251 | auto refreshRate = calculateRefreshRateIfPossible(refreshRateConfigs, now); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 252 | if (refreshRate.has_value()) { |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 253 | ALOGV("%s calculated refresh rate: %s", mName.c_str(), to_string(*refreshRate).c_str()); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 254 | return {LayerHistory::LayerVoteType::Heuristic, refreshRate.value()}; |
| 255 | } |
| 256 | |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 257 | ALOGV("%s Max (can't resolve refresh rate)", mName.c_str()); |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 258 | return {LayerHistory::LayerVoteType::Max, Fps()}; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 261 | const char* LayerInfo::getTraceTag(LayerHistory::LayerVoteType type) const { |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 262 | if (mTraceTags.count(type) == 0) { |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 263 | auto tag = "LFPS " + mName + " " + ftl::enum_string(type); |
| 264 | mTraceTags.emplace(type, std::move(tag)); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | return mTraceTags.at(type).c_str(); |
| 268 | } |
| 269 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 270 | LayerInfo::RefreshRateHistory::HeuristicTraceTagData |
| 271 | LayerInfo::RefreshRateHistory::makeHeuristicTraceTagData() const { |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 272 | const std::string prefix = "LFPS "; |
| 273 | const std::string suffix = "Heuristic "; |
| 274 | return {.min = prefix + mName + suffix + "min", |
| 275 | .max = prefix + mName + suffix + "max", |
| 276 | .consistent = prefix + mName + suffix + "consistent", |
| 277 | .average = prefix + mName + suffix + "average"}; |
| 278 | } |
| 279 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 280 | void LayerInfo::RefreshRateHistory::clear() { |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 281 | mRefreshRates.clear(); |
| 282 | } |
| 283 | |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 284 | bool LayerInfo::RefreshRateHistory::add(Fps refreshRate, nsecs_t now) { |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 285 | mRefreshRates.push_back({refreshRate, now}); |
| 286 | while (mRefreshRates.size() >= HISTORY_SIZE || |
| 287 | now - mRefreshRates.front().timestamp > HISTORY_DURATION.count()) { |
| 288 | mRefreshRates.pop_front(); |
| 289 | } |
| 290 | |
| 291 | if (CC_UNLIKELY(sTraceEnabled)) { |
| 292 | if (!mHeuristicTraceTagData.has_value()) { |
| 293 | mHeuristicTraceTagData = makeHeuristicTraceTagData(); |
| 294 | } |
| 295 | |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 296 | ATRACE_INT(mHeuristicTraceTagData->average.c_str(), refreshRate.getIntValue()); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | return isConsistent(); |
| 300 | } |
| 301 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 302 | bool LayerInfo::RefreshRateHistory::isConsistent() const { |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 303 | if (mRefreshRates.empty()) return true; |
| 304 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 305 | const auto [min, max] = |
| 306 | std::minmax_element(mRefreshRates.begin(), mRefreshRates.end(), |
| 307 | [](const auto& lhs, const auto& rhs) { |
| 308 | return isStrictlyLess(lhs.refreshRate, rhs.refreshRate); |
| 309 | }); |
| 310 | |
| 311 | const bool consistent = |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 312 | max->refreshRate.getValue() - min->refreshRate.getValue() < MARGIN_CONSISTENT_FPS; |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 313 | |
| 314 | if (CC_UNLIKELY(sTraceEnabled)) { |
| 315 | if (!mHeuristicTraceTagData.has_value()) { |
| 316 | mHeuristicTraceTagData = makeHeuristicTraceTagData(); |
| 317 | } |
| 318 | |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 319 | ATRACE_INT(mHeuristicTraceTagData->max.c_str(), max->refreshRate.getIntValue()); |
| 320 | ATRACE_INT(mHeuristicTraceTagData->min.c_str(), min->refreshRate.getIntValue()); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 321 | ATRACE_INT(mHeuristicTraceTagData->consistent.c_str(), consistent); |
| 322 | } |
| 323 | |
| 324 | return consistent; |
| 325 | } |
| 326 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 327 | } // namespace android::scheduler |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 328 | |
| 329 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 330 | #pragma clang diagnostic pop // ignored "-Wextra" |