blob: b0dffd1846018fce440f6b3e6598d4db4834fb96 [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
Marin Shalamanovbed7fd32020-12-21 20:02:20 +010017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wextra"
20
Ady Abraham8a82ba62020-01-17 12:43:17 -080021// #define LOG_NDEBUG 0
Ady Abraham0ccd79b2020-06-10 10:11:17 -070022#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Ady Abraham8a82ba62020-01-17 12:43:17 -080023
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010024#include "LayerInfo.h"
Ady Abraham8a82ba62020-01-17 12:43:17 -080025
26#include <algorithm>
27#include <utility>
28
Ady Abraham0ccd79b2020-06-10 10:11:17 -070029#include <cutils/compiler.h>
30#include <cutils/trace.h>
31
Ady Abraham8a82ba62020-01-17 12:43:17 -080032#undef LOG_TAG
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010033#define LOG_TAG "LayerInfo"
Ady Abraham8a82ba62020-01-17 12:43:17 -080034
35namespace android::scheduler {
36
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010037const RefreshRateConfigs* LayerInfo::sRefreshRateConfigs = nullptr;
38bool LayerInfo::sTraceEnabled = false;
Ady Abrahamb1b9d412020-06-01 19:53:52 -070039
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010040LayerInfo::LayerInfo(const std::string& name, nsecs_t highRefreshRatePeriod,
41 LayerHistory::LayerVoteType defaultVote)
Ady Abrahama6b676e2020-05-27 14:29:09 -070042 : mName(name),
43 mHighRefreshRatePeriod(highRefreshRatePeriod),
Ady Abraham8a82ba62020-01-17 12:43:17 -080044 mDefaultVote(defaultVote),
Marin Shalamanove8a663d2020-11-24 17:48:00 +010045 mLayerVote({defaultVote, Fps(0.0f)}),
Ady Abraham0ccd79b2020-06-10 10:11:17 -070046 mRefreshRateHistory(name) {}
Ady Abraham8a82ba62020-01-17 12:43:17 -080047
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010048void LayerInfo::setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, LayerUpdateType updateType,
49 bool pendingConfigChange) {
Ady Abraham8a82ba62020-01-17 12:43:17 -080050 lastPresentTime = std::max(lastPresentTime, static_cast<nsecs_t>(0));
51
52 mLastUpdatedTime = std::max(lastPresentTime, now);
Ady Abraham5def7332020-05-29 16:13:47 -070053 switch (updateType) {
54 case LayerUpdateType::AnimationTX:
55 mLastAnimationTime = std::max(lastPresentTime, now);
56 break;
57 case LayerUpdateType::SetFrameRate:
58 case LayerUpdateType::Buffer:
59 FrameTimeData frameTime = {.presetTime = lastPresentTime,
60 .queueTime = mLastUpdatedTime,
61 .pendingConfigChange = pendingConfigChange};
62 mFrameTimes.push_back(frameTime);
63 if (mFrameTimes.size() > HISTORY_SIZE) {
64 mFrameTimes.pop_front();
65 }
66 break;
Ady Abraham8a82ba62020-01-17 12:43:17 -080067 }
68}
69
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010070bool LayerInfo::isFrameTimeValid(const FrameTimeData& frameTime) const {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000071 return frameTime.queueTime >= std::chrono::duration_cast<std::chrono::nanoseconds>(
72 mFrameTimeValidSince.time_since_epoch())
73 .count();
74}
Ady Abraham1adbb722020-05-15 11:51:48 -070075
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010076bool LayerInfo::isFrequent(nsecs_t now) const {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000077 // If we know nothing about this layer we consider it as frequent as it might be the start
78 // of an animation.
Ady Abraham983e5682020-05-28 16:49:18 -070079 if (mFrameTimes.size() < FREQUENT_LAYER_WINDOW_SIZE) {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000080 return true;
81 }
82
83 // Find the first active frame
Ady Abraham983e5682020-05-28 16:49:18 -070084 auto it = mFrameTimes.begin();
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000085 for (; it != mFrameTimes.end(); ++it) {
86 if (it->queueTime >= getActiveLayerThreshold(now)) {
87 break;
88 }
89 }
90
91 const auto numFrames = std::distance(it, mFrameTimes.end());
92 if (numFrames < FREQUENT_LAYER_WINDOW_SIZE) {
93 return false;
94 }
95
96 // Layer is considered frequent if the average frame rate is higher than the threshold
97 const auto totalTime = mFrameTimes.back().queueTime - it->queueTime;
Marin Shalamanove8a663d2020-11-24 17:48:00 +010098 return Fps::fromPeriodNsecs(totalTime / (numFrames - 1))
99 .greaterThanOrEqualWithMargin(MIN_FPS_FOR_FREQUENT_LAYER);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800100}
101
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100102bool LayerInfo::isAnimating(nsecs_t now) const {
Ady Abraham5def7332020-05-29 16:13:47 -0700103 return mLastAnimationTime >= getActiveLayerThreshold(now);
104}
105
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100106bool LayerInfo::hasEnoughDataForHeuristic() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700107 // The layer had to publish at least HISTORY_SIZE or HISTORY_DURATION of updates
Ady Abrahama61edcb2020-01-30 18:32:03 -0800108 if (mFrameTimes.size() < 2) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700109 ALOGV("fewer than 2 frames recorded: %zu", mFrameTimes.size());
Ady Abrahama61edcb2020-01-30 18:32:03 -0800110 return false;
111 }
112
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000113 if (!isFrameTimeValid(mFrameTimes.front())) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700114 ALOGV("stale frames still captured");
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000115 return false;
116 }
117
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700118 const auto totalDuration = mFrameTimes.back().queueTime - mFrameTimes.front().queueTime;
119 if (mFrameTimes.size() < HISTORY_SIZE && totalDuration < HISTORY_DURATION.count()) {
120 ALOGV("not enough frames captured: %zu | %.2f seconds", mFrameTimes.size(),
121 totalDuration / 1e9f);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800122 return false;
123 }
124
125 return true;
126}
127
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100128std::optional<nsecs_t> LayerInfo::calculateAverageFrameTime() const {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800129 nsecs_t totalPresentTimeDeltas = 0;
Ady Abrahamc9664832020-05-12 14:16:56 -0700130 nsecs_t totalQueueTimeDeltas = 0;
Ady Abraham32efd542020-05-19 17:49:26 -0700131 bool missingPresentTime = false;
132 int numFrames = 0;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800133 for (auto it = mFrameTimes.begin(); it != mFrameTimes.end() - 1; ++it) {
Ady Abraham32efd542020-05-19 17:49:26 -0700134 // Ignore frames captured during a config change
135 if (it->pendingConfigChange || (it + 1)->pendingConfigChange) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700136 return std::nullopt;
Ady Abraham32efd542020-05-19 17:49:26 -0700137 }
138
Ady Abrahamc9664832020-05-12 14:16:56 -0700139 totalQueueTimeDeltas +=
140 std::max(((it + 1)->queueTime - it->queueTime), mHighRefreshRatePeriod);
Ady Abraham32efd542020-05-19 17:49:26 -0700141 numFrames++;
Ady Abrahamc9664832020-05-12 14:16:56 -0700142
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700143 if (!missingPresentTime && (it->presetTime == 0 || (it + 1)->presetTime == 0)) {
Ady Abrahamc9664832020-05-12 14:16:56 -0700144 missingPresentTime = true;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700145 // If there are no presentation timestamps and we haven't calculated
146 // one in the past then we can't calculate the refresh rate
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100147 if (!mLastRefreshRate.reported.isValid()) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700148 return std::nullopt;
149 }
Ady Abrahamc9664832020-05-12 14:16:56 -0700150 continue;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800151 }
152
153 totalPresentTimeDeltas +=
154 std::max(((it + 1)->presetTime - it->presetTime), mHighRefreshRatePeriod);
155 }
Ady Abrahamc9664832020-05-12 14:16:56 -0700156
Ady Abrahamc9664832020-05-12 14:16:56 -0700157 // Calculate the average frame time based on presentation timestamps. If those
158 // doesn't exist, we look at the time the buffer was queued only. We can do that only if
159 // we calculated a refresh rate based on presentation timestamps in the past. The reason
160 // we look at the queue time is to handle cases where hwui attaches presentation timestamps
161 // when implementing render ahead for specific refresh rates. When hwui no longer provides
162 // presentation timestamps we look at the queue time to see if the current refresh rate still
163 // matches the content.
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700164
Ady Abraham32efd542020-05-19 17:49:26 -0700165 const auto averageFrameTime =
Ady Abrahamc9664832020-05-12 14:16:56 -0700166 static_cast<float>(missingPresentTime ? totalQueueTimeDeltas : totalPresentTimeDeltas) /
Ady Abraham32efd542020-05-19 17:49:26 -0700167 numFrames;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700168 return static_cast<nsecs_t>(averageFrameTime);
Ady Abraham32efd542020-05-19 17:49:26 -0700169}
Ady Abraham8a82ba62020-01-17 12:43:17 -0800170
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100171std::optional<Fps> LayerInfo::calculateRefreshRateIfPossible(nsecs_t now) {
Ady Abraham32efd542020-05-19 17:49:26 -0700172 static constexpr float MARGIN = 1.0f; // 1Hz
Ady Abraham32efd542020-05-19 17:49:26 -0700173 if (!hasEnoughDataForHeuristic()) {
174 ALOGV("Not enough data");
175 return std::nullopt;
176 }
177
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700178 const auto averageFrameTime = calculateAverageFrameTime();
179 if (averageFrameTime.has_value()) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100180 const auto refreshRate = Fps::fromPeriodNsecs(*averageFrameTime);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700181 const bool refreshRateConsistent = mRefreshRateHistory.add(refreshRate, now);
182 if (refreshRateConsistent) {
183 const auto knownRefreshRate =
184 sRefreshRateConfigs->findClosestKnownFrameRate(refreshRate);
Ady Abraham32efd542020-05-19 17:49:26 -0700185
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700186 // To avoid oscillation, use the last calculated refresh rate if it is
187 // close enough
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100188 if (std::abs(mLastRefreshRate.calculated.getValue() - refreshRate.getValue()) >
189 MARGIN &&
190 !mLastRefreshRate.reported.equalsWithMargin(knownRefreshRate)) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700191 mLastRefreshRate.calculated = refreshRate;
192 mLastRefreshRate.reported = knownRefreshRate;
193 }
194
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100195 ALOGV("%s %s rounded to nearest known frame rate %s", mName.c_str(),
196 to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700197 } else {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100198 ALOGV("%s Not stable (%s) returning last known frame rate %s", mName.c_str(),
199 to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700200 }
Ady Abraham32efd542020-05-19 17:49:26 -0700201 }
202
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100203 return mLastRefreshRate.reported.isValid() ? std::make_optional(mLastRefreshRate.reported)
204 : std::nullopt;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800205}
206
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100207LayerInfo::LayerVote LayerInfo::getRefreshRateVote(nsecs_t now) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800208 if (mLayerVote.type != LayerHistory::LayerVoteType::Heuristic) {
Ady Abrahama6b676e2020-05-27 14:29:09 -0700209 ALOGV("%s voted %d ", mName.c_str(), static_cast<int>(mLayerVote.type));
Marin Shalamanov46084422020-10-13 12:33:42 +0200210 return mLayerVote;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800211 }
212
Ady Abraham5def7332020-05-29 16:13:47 -0700213 if (isAnimating(now)) {
214 ALOGV("%s is animating", 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::Max, Fps(0.0f)};
Ady Abraham5def7332020-05-29 16:13:47 -0700217 }
218
Ady Abraham8a82ba62020-01-17 12:43:17 -0800219 if (!isFrequent(now)) {
Ady Abrahama6b676e2020-05-27 14:29:09 -0700220 ALOGV("%s is infrequent", mName.c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700221 mLastRefreshRate.animatingOrInfrequent = true;
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100222 return {LayerHistory::LayerVoteType::Min, Fps(0.0f)};
Ady Abraham8a82ba62020-01-17 12:43:17 -0800223 }
224
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700225 // If the layer was previously tagged as animating or infrequent, we clear
226 // the history as it is likely the layer just changed its behavior
227 // and we should not look at stale data
228 if (mLastRefreshRate.animatingOrInfrequent) {
229 clearHistory(now);
230 }
231
232 auto refreshRate = calculateRefreshRateIfPossible(now);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800233 if (refreshRate.has_value()) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100234 ALOGV("%s calculated refresh rate: %s", mName.c_str(), to_string(*refreshRate).c_str());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800235 return {LayerHistory::LayerVoteType::Heuristic, refreshRate.value()};
236 }
237
Ady Abrahama6b676e2020-05-27 14:29:09 -0700238 ALOGV("%s Max (can't resolve refresh rate)", mName.c_str());
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100239 return {LayerHistory::LayerVoteType::Max, Fps(0.0f)};
Ady Abraham8a82ba62020-01-17 12:43:17 -0800240}
241
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100242const char* LayerInfo::getTraceTag(android::scheduler::LayerHistory::LayerVoteType type) const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700243 if (mTraceTags.count(type) == 0) {
244 const auto tag = "LFPS " + mName + " " + RefreshRateConfigs::layerVoteTypeString(type);
245 mTraceTags.emplace(type, tag);
246 }
247
248 return mTraceTags.at(type).c_str();
249}
250
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100251LayerInfo::RefreshRateHistory::HeuristicTraceTagData
252LayerInfo::RefreshRateHistory::makeHeuristicTraceTagData() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700253 const std::string prefix = "LFPS ";
254 const std::string suffix = "Heuristic ";
255 return {.min = prefix + mName + suffix + "min",
256 .max = prefix + mName + suffix + "max",
257 .consistent = prefix + mName + suffix + "consistent",
258 .average = prefix + mName + suffix + "average"};
259}
260
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100261void LayerInfo::RefreshRateHistory::clear() {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700262 mRefreshRates.clear();
263}
264
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100265bool LayerInfo::RefreshRateHistory::add(Fps refreshRate, nsecs_t now) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700266 mRefreshRates.push_back({refreshRate, now});
267 while (mRefreshRates.size() >= HISTORY_SIZE ||
268 now - mRefreshRates.front().timestamp > HISTORY_DURATION.count()) {
269 mRefreshRates.pop_front();
270 }
271
272 if (CC_UNLIKELY(sTraceEnabled)) {
273 if (!mHeuristicTraceTagData.has_value()) {
274 mHeuristicTraceTagData = makeHeuristicTraceTagData();
275 }
276
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100277 ATRACE_INT(mHeuristicTraceTagData->average.c_str(), refreshRate.getIntValue());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700278 }
279
280 return isConsistent();
281}
282
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100283bool LayerInfo::RefreshRateHistory::isConsistent() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700284 if (mRefreshRates.empty()) return true;
285
286 const auto max = std::max_element(mRefreshRates.begin(), mRefreshRates.end());
287 const auto min = std::min_element(mRefreshRates.begin(), mRefreshRates.end());
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100288 const auto consistent =
289 max->refreshRate.getValue() - min->refreshRate.getValue() < MARGIN_CONSISTENT_FPS;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700290
291 if (CC_UNLIKELY(sTraceEnabled)) {
292 if (!mHeuristicTraceTagData.has_value()) {
293 mHeuristicTraceTagData = makeHeuristicTraceTagData();
294 }
295
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100296 ATRACE_INT(mHeuristicTraceTagData->max.c_str(), max->refreshRate.getIntValue());
297 ATRACE_INT(mHeuristicTraceTagData->min.c_str(), min->refreshRate.getIntValue());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700298 ATRACE_INT(mHeuristicTraceTagData->consistent.c_str(), consistent);
299 }
300
301 return consistent;
302}
303
Ady Abraham8a82ba62020-01-17 12:43:17 -0800304} // namespace android::scheduler
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100305
306// TODO(b/129481165): remove the #pragma below and fix conversion issues
307#pragma clang diagnostic pop // ignored "-Wextra"