blob: 4b2862ebb276d7a9298bb7dc9dc4356d10a25974 [file] [log] [blame]
Ady Abraham8a82ba62020-01-17 12:43:17 -08001/*
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
17// #define LOG_NDEBUG 0
Ady Abraham0ccd79b2020-06-10 10:11:17 -070018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Ady Abraham8a82ba62020-01-17 12:43:17 -080019
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010020#include "LayerInfo.h"
Ady Abraham8a82ba62020-01-17 12:43:17 -080021
22#include <algorithm>
23#include <utility>
24
Ady Abraham0ccd79b2020-06-10 10:11:17 -070025#include <cutils/compiler.h>
26#include <cutils/trace.h>
27
Ady Abraham8a82ba62020-01-17 12:43:17 -080028#undef LOG_TAG
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010029#define LOG_TAG "LayerInfo"
Ady Abraham8a82ba62020-01-17 12:43:17 -080030
31namespace android::scheduler {
32
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010033const RefreshRateConfigs* LayerInfo::sRefreshRateConfigs = nullptr;
34bool LayerInfo::sTraceEnabled = false;
Ady Abrahamb1b9d412020-06-01 19:53:52 -070035
Marin Shalamanov4ad8b302020-12-11 15:50:08 +010036LayerInfo::LayerInfo(const std::string& name, LayerHistory::LayerVoteType defaultVote)
Ady Abrahama6b676e2020-05-27 14:29:09 -070037 : mName(name),
Ady Abraham8a82ba62020-01-17 12:43:17 -080038 mDefaultVote(defaultVote),
Marin Shalamanove8a663d2020-11-24 17:48:00 +010039 mLayerVote({defaultVote, Fps(0.0f)}),
Ady Abraham0ccd79b2020-06-10 10:11:17 -070040 mRefreshRateHistory(name) {}
Ady Abraham8a82ba62020-01-17 12:43:17 -080041
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010042void LayerInfo::setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, LayerUpdateType updateType,
43 bool pendingConfigChange) {
Ady Abraham8a82ba62020-01-17 12:43:17 -080044 lastPresentTime = std::max(lastPresentTime, static_cast<nsecs_t>(0));
45
46 mLastUpdatedTime = std::max(lastPresentTime, now);
Ady Abraham5def7332020-05-29 16:13:47 -070047 switch (updateType) {
48 case LayerUpdateType::AnimationTX:
49 mLastAnimationTime = std::max(lastPresentTime, now);
50 break;
51 case LayerUpdateType::SetFrameRate:
52 case LayerUpdateType::Buffer:
53 FrameTimeData frameTime = {.presetTime = lastPresentTime,
54 .queueTime = mLastUpdatedTime,
55 .pendingConfigChange = pendingConfigChange};
56 mFrameTimes.push_back(frameTime);
57 if (mFrameTimes.size() > HISTORY_SIZE) {
58 mFrameTimes.pop_front();
59 }
60 break;
Ady Abraham8a82ba62020-01-17 12:43:17 -080061 }
62}
63
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010064bool LayerInfo::isFrameTimeValid(const FrameTimeData& frameTime) const {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000065 return frameTime.queueTime >= std::chrono::duration_cast<std::chrono::nanoseconds>(
66 mFrameTimeValidSince.time_since_epoch())
67 .count();
68}
Ady Abraham1adbb722020-05-15 11:51:48 -070069
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010070bool LayerInfo::isFrequent(nsecs_t now) const {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000071 // If we know nothing about this layer we consider it as frequent as it might be the start
72 // of an animation.
Ady Abraham983e5682020-05-28 16:49:18 -070073 if (mFrameTimes.size() < FREQUENT_LAYER_WINDOW_SIZE) {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000074 return true;
75 }
76
77 // Find the first active frame
Ady Abraham983e5682020-05-28 16:49:18 -070078 auto it = mFrameTimes.begin();
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000079 for (; it != mFrameTimes.end(); ++it) {
80 if (it->queueTime >= getActiveLayerThreshold(now)) {
81 break;
82 }
83 }
84
85 const auto numFrames = std::distance(it, mFrameTimes.end());
86 if (numFrames < FREQUENT_LAYER_WINDOW_SIZE) {
87 return false;
88 }
89
90 // Layer is considered frequent if the average frame rate is higher than the threshold
91 const auto totalTime = mFrameTimes.back().queueTime - it->queueTime;
Marin Shalamanove8a663d2020-11-24 17:48:00 +010092 return Fps::fromPeriodNsecs(totalTime / (numFrames - 1))
93 .greaterThanOrEqualWithMargin(MIN_FPS_FOR_FREQUENT_LAYER);
Ady Abraham8a82ba62020-01-17 12:43:17 -080094}
95
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010096bool LayerInfo::isAnimating(nsecs_t now) const {
Ady Abraham5def7332020-05-29 16:13:47 -070097 return mLastAnimationTime >= getActiveLayerThreshold(now);
98}
99
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100100bool LayerInfo::hasEnoughDataForHeuristic() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700101 // The layer had to publish at least HISTORY_SIZE or HISTORY_DURATION of updates
Ady Abrahama61edcb2020-01-30 18:32:03 -0800102 if (mFrameTimes.size() < 2) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700103 ALOGV("fewer than 2 frames recorded: %zu", mFrameTimes.size());
Ady Abrahama61edcb2020-01-30 18:32:03 -0800104 return false;
105 }
106
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000107 if (!isFrameTimeValid(mFrameTimes.front())) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700108 ALOGV("stale frames still captured");
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000109 return false;
110 }
111
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700112 const auto totalDuration = mFrameTimes.back().queueTime - mFrameTimes.front().queueTime;
113 if (mFrameTimes.size() < HISTORY_SIZE && totalDuration < HISTORY_DURATION.count()) {
114 ALOGV("not enough frames captured: %zu | %.2f seconds", mFrameTimes.size(),
115 totalDuration / 1e9f);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800116 return false;
117 }
118
119 return true;
120}
121
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100122std::optional<nsecs_t> LayerInfo::calculateAverageFrameTime() const {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800123 nsecs_t totalPresentTimeDeltas = 0;
Ady Abrahamc9664832020-05-12 14:16:56 -0700124 nsecs_t totalQueueTimeDeltas = 0;
Ady Abraham32efd542020-05-19 17:49:26 -0700125 bool missingPresentTime = false;
126 int numFrames = 0;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800127 for (auto it = mFrameTimes.begin(); it != mFrameTimes.end() - 1; ++it) {
Ady Abraham32efd542020-05-19 17:49:26 -0700128 // Ignore frames captured during a config change
129 if (it->pendingConfigChange || (it + 1)->pendingConfigChange) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700130 return std::nullopt;
Ady Abraham32efd542020-05-19 17:49:26 -0700131 }
132
Ady Abrahamc9664832020-05-12 14:16:56 -0700133 totalQueueTimeDeltas +=
Marin Shalamanov4ad8b302020-12-11 15:50:08 +0100134 std::max(((it + 1)->queueTime - it->queueTime), kMinPeriodBetweenFrames);
Ady Abraham32efd542020-05-19 17:49:26 -0700135 numFrames++;
Ady Abrahamc9664832020-05-12 14:16:56 -0700136
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700137 if (!missingPresentTime && (it->presetTime == 0 || (it + 1)->presetTime == 0)) {
Ady Abrahamc9664832020-05-12 14:16:56 -0700138 missingPresentTime = true;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700139 // If there are no presentation timestamps and we haven't calculated
140 // one in the past then we can't calculate the refresh rate
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100141 if (!mLastRefreshRate.reported.isValid()) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700142 return std::nullopt;
143 }
Ady Abrahamc9664832020-05-12 14:16:56 -0700144 continue;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800145 }
146
147 totalPresentTimeDeltas +=
Marin Shalamanov4ad8b302020-12-11 15:50:08 +0100148 std::max(((it + 1)->presetTime - it->presetTime), kMinPeriodBetweenFrames);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800149 }
Ady Abrahamc9664832020-05-12 14:16:56 -0700150
Ady Abrahamc9664832020-05-12 14:16:56 -0700151 // Calculate the average frame time based on presentation timestamps. If those
152 // doesn't exist, we look at the time the buffer was queued only. We can do that only if
153 // we calculated a refresh rate based on presentation timestamps in the past. The reason
154 // we look at the queue time is to handle cases where hwui attaches presentation timestamps
155 // when implementing render ahead for specific refresh rates. When hwui no longer provides
156 // presentation timestamps we look at the queue time to see if the current refresh rate still
157 // matches the content.
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700158
Ady Abraham32efd542020-05-19 17:49:26 -0700159 const auto averageFrameTime =
Ady Abrahamc9664832020-05-12 14:16:56 -0700160 static_cast<float>(missingPresentTime ? totalQueueTimeDeltas : totalPresentTimeDeltas) /
Ady Abraham32efd542020-05-19 17:49:26 -0700161 numFrames;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700162 return static_cast<nsecs_t>(averageFrameTime);
Ady Abraham32efd542020-05-19 17:49:26 -0700163}
Ady Abraham8a82ba62020-01-17 12:43:17 -0800164
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100165std::optional<Fps> LayerInfo::calculateRefreshRateIfPossible(nsecs_t now) {
Ady Abraham32efd542020-05-19 17:49:26 -0700166 static constexpr float MARGIN = 1.0f; // 1Hz
Ady Abraham32efd542020-05-19 17:49:26 -0700167 if (!hasEnoughDataForHeuristic()) {
168 ALOGV("Not enough data");
169 return std::nullopt;
170 }
171
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700172 const auto averageFrameTime = calculateAverageFrameTime();
173 if (averageFrameTime.has_value()) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100174 const auto refreshRate = Fps::fromPeriodNsecs(*averageFrameTime);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700175 const bool refreshRateConsistent = mRefreshRateHistory.add(refreshRate, now);
176 if (refreshRateConsistent) {
177 const auto knownRefreshRate =
178 sRefreshRateConfigs->findClosestKnownFrameRate(refreshRate);
Ady Abraham32efd542020-05-19 17:49:26 -0700179
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700180 // To avoid oscillation, use the last calculated refresh rate if it is
181 // close enough
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100182 if (std::abs(mLastRefreshRate.calculated.getValue() - refreshRate.getValue()) >
183 MARGIN &&
184 !mLastRefreshRate.reported.equalsWithMargin(knownRefreshRate)) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700185 mLastRefreshRate.calculated = refreshRate;
186 mLastRefreshRate.reported = knownRefreshRate;
187 }
188
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100189 ALOGV("%s %s rounded to nearest known frame rate %s", mName.c_str(),
190 to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700191 } else {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100192 ALOGV("%s Not stable (%s) returning last known frame rate %s", mName.c_str(),
193 to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700194 }
Ady Abraham32efd542020-05-19 17:49:26 -0700195 }
196
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100197 return mLastRefreshRate.reported.isValid() ? std::make_optional(mLastRefreshRate.reported)
198 : std::nullopt;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800199}
200
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100201LayerInfo::LayerVote LayerInfo::getRefreshRateVote(nsecs_t now) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800202 if (mLayerVote.type != LayerHistory::LayerVoteType::Heuristic) {
Ady Abrahama6b676e2020-05-27 14:29:09 -0700203 ALOGV("%s voted %d ", mName.c_str(), static_cast<int>(mLayerVote.type));
Marin Shalamanov46084422020-10-13 12:33:42 +0200204 return mLayerVote;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800205 }
206
Ady Abraham5def7332020-05-29 16:13:47 -0700207 if (isAnimating(now)) {
208 ALOGV("%s is animating", mName.c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700209 mLastRefreshRate.animatingOrInfrequent = true;
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100210 return {LayerHistory::LayerVoteType::Max, Fps(0.0f)};
Ady Abraham5def7332020-05-29 16:13:47 -0700211 }
212
Ady Abraham8a82ba62020-01-17 12:43:17 -0800213 if (!isFrequent(now)) {
Ady Abrahama6b676e2020-05-27 14:29:09 -0700214 ALOGV("%s is infrequent", mName.c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700215 mLastRefreshRate.animatingOrInfrequent = true;
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100216 return {LayerHistory::LayerVoteType::Min, Fps(0.0f)};
Ady Abraham8a82ba62020-01-17 12:43:17 -0800217 }
218
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700219 // If the layer was previously tagged as animating or infrequent, we clear
220 // the history as it is likely the layer just changed its behavior
221 // and we should not look at stale data
222 if (mLastRefreshRate.animatingOrInfrequent) {
223 clearHistory(now);
224 }
225
226 auto refreshRate = calculateRefreshRateIfPossible(now);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800227 if (refreshRate.has_value()) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100228 ALOGV("%s calculated refresh rate: %s", mName.c_str(), to_string(*refreshRate).c_str());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800229 return {LayerHistory::LayerVoteType::Heuristic, refreshRate.value()};
230 }
231
Ady Abrahama6b676e2020-05-27 14:29:09 -0700232 ALOGV("%s Max (can't resolve refresh rate)", mName.c_str());
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100233 return {LayerHistory::LayerVoteType::Max, Fps(0.0f)};
Ady Abraham8a82ba62020-01-17 12:43:17 -0800234}
235
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100236const char* LayerInfo::getTraceTag(android::scheduler::LayerHistory::LayerVoteType type) const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700237 if (mTraceTags.count(type) == 0) {
238 const auto tag = "LFPS " + mName + " " + RefreshRateConfigs::layerVoteTypeString(type);
239 mTraceTags.emplace(type, tag);
240 }
241
242 return mTraceTags.at(type).c_str();
243}
244
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100245LayerInfo::RefreshRateHistory::HeuristicTraceTagData
246LayerInfo::RefreshRateHistory::makeHeuristicTraceTagData() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700247 const std::string prefix = "LFPS ";
248 const std::string suffix = "Heuristic ";
249 return {.min = prefix + mName + suffix + "min",
250 .max = prefix + mName + suffix + "max",
251 .consistent = prefix + mName + suffix + "consistent",
252 .average = prefix + mName + suffix + "average"};
253}
254
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100255void LayerInfo::RefreshRateHistory::clear() {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700256 mRefreshRates.clear();
257}
258
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100259bool LayerInfo::RefreshRateHistory::add(Fps refreshRate, nsecs_t now) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700260 mRefreshRates.push_back({refreshRate, now});
261 while (mRefreshRates.size() >= HISTORY_SIZE ||
262 now - mRefreshRates.front().timestamp > HISTORY_DURATION.count()) {
263 mRefreshRates.pop_front();
264 }
265
266 if (CC_UNLIKELY(sTraceEnabled)) {
267 if (!mHeuristicTraceTagData.has_value()) {
268 mHeuristicTraceTagData = makeHeuristicTraceTagData();
269 }
270
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100271 ATRACE_INT(mHeuristicTraceTagData->average.c_str(), refreshRate.getIntValue());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700272 }
273
274 return isConsistent();
275}
276
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100277bool LayerInfo::RefreshRateHistory::isConsistent() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700278 if (mRefreshRates.empty()) return true;
279
280 const auto max = std::max_element(mRefreshRates.begin(), mRefreshRates.end());
281 const auto min = std::min_element(mRefreshRates.begin(), mRefreshRates.end());
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100282 const auto consistent =
283 max->refreshRate.getValue() - min->refreshRate.getValue() < MARGIN_CONSISTENT_FPS;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700284
285 if (CC_UNLIKELY(sTraceEnabled)) {
286 if (!mHeuristicTraceTagData.has_value()) {
287 mHeuristicTraceTagData = makeHeuristicTraceTagData();
288 }
289
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100290 ATRACE_INT(mHeuristicTraceTagData->max.c_str(), max->refreshRate.getIntValue());
291 ATRACE_INT(mHeuristicTraceTagData->min.c_str(), min->refreshRate.getIntValue());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700292 ATRACE_INT(mHeuristicTraceTagData->consistent.c_str(), consistent);
293 }
294
295 return consistent;
296}
297
Ady Abraham8a82ba62020-01-17 12:43:17 -0800298} // namespace android::scheduler