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