blob: 1c0065c9adc2c8933bf7965886ec4718106457ac [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 Shalamanov1bc43ee2020-11-20 16:56:52 +010036LayerInfo::LayerInfo(const std::string& name, nsecs_t highRefreshRatePeriod,
37 LayerHistory::LayerVoteType defaultVote)
Ady Abrahama6b676e2020-05-27 14:29:09 -070038 : mName(name),
39 mHighRefreshRatePeriod(highRefreshRatePeriod),
Ady Abraham8a82ba62020-01-17 12:43:17 -080040 mDefaultVote(defaultVote),
Marin Shalamanove8a663d2020-11-24 17:48:00 +010041 mLayerVote({defaultVote, Fps(0.0f)}),
Ady Abraham0ccd79b2020-06-10 10:11:17 -070042 mRefreshRateHistory(name) {}
Ady Abraham8a82ba62020-01-17 12:43:17 -080043
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010044void LayerInfo::setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, LayerUpdateType updateType,
45 bool pendingConfigChange) {
Ady Abraham8a82ba62020-01-17 12:43:17 -080046 lastPresentTime = std::max(lastPresentTime, static_cast<nsecs_t>(0));
47
48 mLastUpdatedTime = std::max(lastPresentTime, now);
Ady Abraham5def7332020-05-29 16:13:47 -070049 switch (updateType) {
50 case LayerUpdateType::AnimationTX:
51 mLastAnimationTime = std::max(lastPresentTime, now);
52 break;
53 case LayerUpdateType::SetFrameRate:
54 case LayerUpdateType::Buffer:
55 FrameTimeData frameTime = {.presetTime = lastPresentTime,
56 .queueTime = mLastUpdatedTime,
57 .pendingConfigChange = pendingConfigChange};
58 mFrameTimes.push_back(frameTime);
59 if (mFrameTimes.size() > HISTORY_SIZE) {
60 mFrameTimes.pop_front();
61 }
62 break;
Ady Abraham8a82ba62020-01-17 12:43:17 -080063 }
64}
65
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010066bool LayerInfo::isFrameTimeValid(const FrameTimeData& frameTime) const {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000067 return frameTime.queueTime >= std::chrono::duration_cast<std::chrono::nanoseconds>(
68 mFrameTimeValidSince.time_since_epoch())
69 .count();
70}
Ady Abraham1adbb722020-05-15 11:51:48 -070071
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010072bool LayerInfo::isFrequent(nsecs_t now) const {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000073 // If we know nothing about this layer we consider it as frequent as it might be the start
74 // of an animation.
Ady Abraham983e5682020-05-28 16:49:18 -070075 if (mFrameTimes.size() < FREQUENT_LAYER_WINDOW_SIZE) {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000076 return true;
77 }
78
79 // Find the first active frame
Ady Abraham983e5682020-05-28 16:49:18 -070080 auto it = mFrameTimes.begin();
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000081 for (; it != mFrameTimes.end(); ++it) {
82 if (it->queueTime >= getActiveLayerThreshold(now)) {
83 break;
84 }
85 }
86
87 const auto numFrames = std::distance(it, mFrameTimes.end());
88 if (numFrames < FREQUENT_LAYER_WINDOW_SIZE) {
89 return false;
90 }
91
92 // Layer is considered frequent if the average frame rate is higher than the threshold
93 const auto totalTime = mFrameTimes.back().queueTime - it->queueTime;
Marin Shalamanove8a663d2020-11-24 17:48:00 +010094 return Fps::fromPeriodNsecs(totalTime / (numFrames - 1))
95 .greaterThanOrEqualWithMargin(MIN_FPS_FOR_FREQUENT_LAYER);
Ady Abraham8a82ba62020-01-17 12:43:17 -080096}
97
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010098bool LayerInfo::isAnimating(nsecs_t now) const {
Ady Abraham5def7332020-05-29 16:13:47 -070099 return mLastAnimationTime >= getActiveLayerThreshold(now);
100}
101
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100102bool LayerInfo::hasEnoughDataForHeuristic() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700103 // The layer had to publish at least HISTORY_SIZE or HISTORY_DURATION of updates
Ady Abrahama61edcb2020-01-30 18:32:03 -0800104 if (mFrameTimes.size() < 2) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700105 ALOGV("fewer than 2 frames recorded: %zu", mFrameTimes.size());
Ady Abrahama61edcb2020-01-30 18:32:03 -0800106 return false;
107 }
108
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000109 if (!isFrameTimeValid(mFrameTimes.front())) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700110 ALOGV("stale frames still captured");
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000111 return false;
112 }
113
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700114 const auto totalDuration = mFrameTimes.back().queueTime - mFrameTimes.front().queueTime;
115 if (mFrameTimes.size() < HISTORY_SIZE && totalDuration < HISTORY_DURATION.count()) {
116 ALOGV("not enough frames captured: %zu | %.2f seconds", mFrameTimes.size(),
117 totalDuration / 1e9f);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800118 return false;
119 }
120
121 return true;
122}
123
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100124std::optional<nsecs_t> LayerInfo::calculateAverageFrameTime() const {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800125 nsecs_t totalPresentTimeDeltas = 0;
Ady Abrahamc9664832020-05-12 14:16:56 -0700126 nsecs_t totalQueueTimeDeltas = 0;
Ady Abraham32efd542020-05-19 17:49:26 -0700127 bool missingPresentTime = false;
128 int numFrames = 0;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800129 for (auto it = mFrameTimes.begin(); it != mFrameTimes.end() - 1; ++it) {
Ady Abraham32efd542020-05-19 17:49:26 -0700130 // Ignore frames captured during a config change
131 if (it->pendingConfigChange || (it + 1)->pendingConfigChange) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700132 return std::nullopt;
Ady Abraham32efd542020-05-19 17:49:26 -0700133 }
134
Ady Abrahamc9664832020-05-12 14:16:56 -0700135 totalQueueTimeDeltas +=
136 std::max(((it + 1)->queueTime - it->queueTime), mHighRefreshRatePeriod);
Ady Abraham32efd542020-05-19 17:49:26 -0700137 numFrames++;
Ady Abrahamc9664832020-05-12 14:16:56 -0700138
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700139 if (!missingPresentTime && (it->presetTime == 0 || (it + 1)->presetTime == 0)) {
Ady Abrahamc9664832020-05-12 14:16:56 -0700140 missingPresentTime = true;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700141 // If there are no presentation timestamps and we haven't calculated
142 // one in the past then we can't calculate the refresh rate
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100143 if (!mLastRefreshRate.reported.isValid()) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700144 return std::nullopt;
145 }
Ady Abrahamc9664832020-05-12 14:16:56 -0700146 continue;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800147 }
148
149 totalPresentTimeDeltas +=
150 std::max(((it + 1)->presetTime - it->presetTime), mHighRefreshRatePeriod);
151 }
Ady Abrahamc9664832020-05-12 14:16:56 -0700152
Ady Abrahamc9664832020-05-12 14:16:56 -0700153 // Calculate the average frame time based on presentation timestamps. If those
154 // doesn't exist, we look at the time the buffer was queued only. We can do that only if
155 // we calculated a refresh rate based on presentation timestamps in the past. The reason
156 // we look at the queue time is to handle cases where hwui attaches presentation timestamps
157 // when implementing render ahead for specific refresh rates. When hwui no longer provides
158 // presentation timestamps we look at the queue time to see if the current refresh rate still
159 // matches the content.
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700160
Ady Abraham32efd542020-05-19 17:49:26 -0700161 const auto averageFrameTime =
Ady Abrahamc9664832020-05-12 14:16:56 -0700162 static_cast<float>(missingPresentTime ? totalQueueTimeDeltas : totalPresentTimeDeltas) /
Ady Abraham32efd542020-05-19 17:49:26 -0700163 numFrames;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700164 return static_cast<nsecs_t>(averageFrameTime);
Ady Abraham32efd542020-05-19 17:49:26 -0700165}
Ady Abraham8a82ba62020-01-17 12:43:17 -0800166
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100167std::optional<Fps> LayerInfo::calculateRefreshRateIfPossible(nsecs_t now) {
Ady Abraham32efd542020-05-19 17:49:26 -0700168 static constexpr float MARGIN = 1.0f; // 1Hz
Ady Abraham32efd542020-05-19 17:49:26 -0700169 if (!hasEnoughDataForHeuristic()) {
170 ALOGV("Not enough data");
171 return std::nullopt;
172 }
173
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700174 const auto averageFrameTime = calculateAverageFrameTime();
175 if (averageFrameTime.has_value()) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100176 const auto refreshRate = Fps::fromPeriodNsecs(*averageFrameTime);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700177 const bool refreshRateConsistent = mRefreshRateHistory.add(refreshRate, now);
178 if (refreshRateConsistent) {
179 const auto knownRefreshRate =
180 sRefreshRateConfigs->findClosestKnownFrameRate(refreshRate);
Ady Abraham32efd542020-05-19 17:49:26 -0700181
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700182 // To avoid oscillation, use the last calculated refresh rate if it is
183 // close enough
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100184 if (std::abs(mLastRefreshRate.calculated.getValue() - refreshRate.getValue()) >
185 MARGIN &&
186 !mLastRefreshRate.reported.equalsWithMargin(knownRefreshRate)) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700187 mLastRefreshRate.calculated = refreshRate;
188 mLastRefreshRate.reported = knownRefreshRate;
189 }
190
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100191 ALOGV("%s %s rounded to nearest known frame rate %s", mName.c_str(),
192 to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700193 } else {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100194 ALOGV("%s Not stable (%s) returning last known frame rate %s", mName.c_str(),
195 to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700196 }
Ady Abraham32efd542020-05-19 17:49:26 -0700197 }
198
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100199 return mLastRefreshRate.reported.isValid() ? std::make_optional(mLastRefreshRate.reported)
200 : std::nullopt;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800201}
202
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100203LayerInfo::LayerVote LayerInfo::getRefreshRateVote(nsecs_t now) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800204 if (mLayerVote.type != LayerHistory::LayerVoteType::Heuristic) {
Ady Abrahama6b676e2020-05-27 14:29:09 -0700205 ALOGV("%s voted %d ", mName.c_str(), static_cast<int>(mLayerVote.type));
Marin Shalamanov46084422020-10-13 12:33:42 +0200206 return mLayerVote;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800207 }
208
Ady Abraham5def7332020-05-29 16:13:47 -0700209 if (isAnimating(now)) {
210 ALOGV("%s is animating", mName.c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700211 mLastRefreshRate.animatingOrInfrequent = true;
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100212 return {LayerHistory::LayerVoteType::Max, Fps(0.0f)};
Ady Abraham5def7332020-05-29 16:13:47 -0700213 }
214
Ady Abraham8a82ba62020-01-17 12:43:17 -0800215 if (!isFrequent(now)) {
Ady Abrahama6b676e2020-05-27 14:29:09 -0700216 ALOGV("%s is infrequent", mName.c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700217 mLastRefreshRate.animatingOrInfrequent = true;
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100218 return {LayerHistory::LayerVoteType::Min, Fps(0.0f)};
Ady Abraham8a82ba62020-01-17 12:43:17 -0800219 }
220
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700221 // If the layer was previously tagged as animating or infrequent, we clear
222 // the history as it is likely the layer just changed its behavior
223 // and we should not look at stale data
224 if (mLastRefreshRate.animatingOrInfrequent) {
225 clearHistory(now);
226 }
227
228 auto refreshRate = calculateRefreshRateIfPossible(now);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800229 if (refreshRate.has_value()) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100230 ALOGV("%s calculated refresh rate: %s", mName.c_str(), to_string(*refreshRate).c_str());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800231 return {LayerHistory::LayerVoteType::Heuristic, refreshRate.value()};
232 }
233
Ady Abrahama6b676e2020-05-27 14:29:09 -0700234 ALOGV("%s Max (can't resolve refresh rate)", mName.c_str());
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100235 return {LayerHistory::LayerVoteType::Max, Fps(0.0f)};
Ady Abraham8a82ba62020-01-17 12:43:17 -0800236}
237
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100238const char* LayerInfo::getTraceTag(android::scheduler::LayerHistory::LayerVoteType type) const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700239 if (mTraceTags.count(type) == 0) {
240 const auto tag = "LFPS " + mName + " " + RefreshRateConfigs::layerVoteTypeString(type);
241 mTraceTags.emplace(type, tag);
242 }
243
244 return mTraceTags.at(type).c_str();
245}
246
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100247LayerInfo::RefreshRateHistory::HeuristicTraceTagData
248LayerInfo::RefreshRateHistory::makeHeuristicTraceTagData() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700249 const std::string prefix = "LFPS ";
250 const std::string suffix = "Heuristic ";
251 return {.min = prefix + mName + suffix + "min",
252 .max = prefix + mName + suffix + "max",
253 .consistent = prefix + mName + suffix + "consistent",
254 .average = prefix + mName + suffix + "average"};
255}
256
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100257void LayerInfo::RefreshRateHistory::clear() {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700258 mRefreshRates.clear();
259}
260
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100261bool LayerInfo::RefreshRateHistory::add(Fps refreshRate, nsecs_t now) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700262 mRefreshRates.push_back({refreshRate, now});
263 while (mRefreshRates.size() >= HISTORY_SIZE ||
264 now - mRefreshRates.front().timestamp > HISTORY_DURATION.count()) {
265 mRefreshRates.pop_front();
266 }
267
268 if (CC_UNLIKELY(sTraceEnabled)) {
269 if (!mHeuristicTraceTagData.has_value()) {
270 mHeuristicTraceTagData = makeHeuristicTraceTagData();
271 }
272
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100273 ATRACE_INT(mHeuristicTraceTagData->average.c_str(), refreshRate.getIntValue());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700274 }
275
276 return isConsistent();
277}
278
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100279bool LayerInfo::RefreshRateHistory::isConsistent() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700280 if (mRefreshRates.empty()) return true;
281
282 const auto max = std::max_element(mRefreshRates.begin(), mRefreshRates.end());
283 const auto min = std::min_element(mRefreshRates.begin(), mRefreshRates.end());
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100284 const auto consistent =
285 max->refreshRate.getValue() - min->refreshRate.getValue() < MARGIN_CONSISTENT_FPS;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700286
287 if (CC_UNLIKELY(sTraceEnabled)) {
288 if (!mHeuristicTraceTagData.has_value()) {
289 mHeuristicTraceTagData = makeHeuristicTraceTagData();
290 }
291
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100292 ATRACE_INT(mHeuristicTraceTagData->max.c_str(), max->refreshRate.getIntValue());
293 ATRACE_INT(mHeuristicTraceTagData->min.c_str(), min->refreshRate.getIntValue());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700294 ATRACE_INT(mHeuristicTraceTagData->consistent.c_str(), consistent);
295 }
296
297 return consistent;
298}
299
Ady Abraham8a82ba62020-01-17 12:43:17 -0800300} // namespace android::scheduler