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