blob: bf3a7bc8b7bda7995507a8966b7a0e62ab3f37fa [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
Rachel Leece6e0042023-06-27 11:22:54 -070029#include <android/native_window.h>
Ady Abraham0ccd79b2020-06-10 10:11:17 -070030#include <cutils/compiler.h>
31#include <cutils/trace.h>
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070032#include <ftl/enum.h>
Ady Abraham73c3df52023-01-12 18:09:31 -080033#include <gui/TraceUtils.h>
Rachel Leece6e0042023-06-27 11:22:54 -070034#include <system/window.h>
Ady Abraham0ccd79b2020-06-10 10:11:17 -070035
Ady Abraham8a82ba62020-01-17 12:43:17 -080036#undef LOG_TAG
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010037#define LOG_TAG "LayerInfo"
Ady Abraham8a82ba62020-01-17 12:43:17 -080038
39namespace android::scheduler {
40
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010041bool LayerInfo::sTraceEnabled = false;
Ady Abrahamb1b9d412020-06-01 19:53:52 -070042
Ady Abrahambdda8f02021-04-01 16:06:11 -070043LayerInfo::LayerInfo(const std::string& name, uid_t ownerUid,
44 LayerHistory::LayerVoteType defaultVote)
Ady Abrahama6b676e2020-05-27 14:29:09 -070045 : mName(name),
Ady Abrahambdda8f02021-04-01 16:06:11 -070046 mOwnerUid(ownerUid),
Ady Abraham8a82ba62020-01-17 12:43:17 -080047 mDefaultVote(defaultVote),
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070048 mLayerVote({defaultVote, Fps()}),
Vishnu Nairef68d6d2023-02-28 06:18:27 +000049 mLayerProps(std::make_unique<LayerProps>()),
50 mRefreshRateHistory(name) {
51 ;
52}
Ady Abraham8a82ba62020-01-17 12:43:17 -080053
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010054void LayerInfo::setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, LayerUpdateType updateType,
Vishnu Nairef68d6d2023-02-28 06:18:27 +000055 bool pendingModeChange, const LayerProps& props) {
Ady Abraham8a82ba62020-01-17 12:43:17 -080056 lastPresentTime = std::max(lastPresentTime, static_cast<nsecs_t>(0));
57
58 mLastUpdatedTime = std::max(lastPresentTime, now);
Vishnu Nairef68d6d2023-02-28 06:18:27 +000059 *mLayerProps = props;
Ady Abraham5def7332020-05-29 16:13:47 -070060 switch (updateType) {
61 case LayerUpdateType::AnimationTX:
62 mLastAnimationTime = std::max(lastPresentTime, now);
63 break;
64 case LayerUpdateType::SetFrameRate:
65 case LayerUpdateType::Buffer:
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010066 FrameTimeData frameTime = {.presentTime = lastPresentTime,
Ady Abraham5def7332020-05-29 16:13:47 -070067 .queueTime = mLastUpdatedTime,
Arthur Hungc70bee22023-06-02 01:35:52 +000068 .pendingModeChange = pendingModeChange,
69 .isSmallDirty = props.isSmallDirty};
Ady Abraham5def7332020-05-29 16:13:47 -070070 mFrameTimes.push_back(frameTime);
71 if (mFrameTimes.size() > HISTORY_SIZE) {
72 mFrameTimes.pop_front();
73 }
74 break;
Ady Abraham8a82ba62020-01-17 12:43:17 -080075 }
76}
77
Vishnu Nair41376b62023-11-08 05:08:58 -080078void LayerInfo::setProperties(const android::scheduler::LayerProps& properties) {
79 *mLayerProps = properties;
80}
81
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010082bool LayerInfo::isFrameTimeValid(const FrameTimeData& frameTime) const {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000083 return frameTime.queueTime >= std::chrono::duration_cast<std::chrono::nanoseconds>(
84 mFrameTimeValidSince.time_since_epoch())
85 .count();
86}
Ady Abraham1adbb722020-05-15 11:51:48 -070087
ramindani63db24e2023-04-03 10:56:05 -070088LayerInfo::Frequent LayerInfo::isFrequent(nsecs_t now) const {
Ady Abraham86ac5c52023-01-11 15:24:03 -080089 // If we know nothing about this layer (e.g. after touch event),
90 // we consider it as frequent as it might be the start of an animation.
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010091 if (mFrameTimes.size() < kFrequentLayerWindowSize) {
ramindani63db24e2023-04-03 10:56:05 -070092 return {/* isFrequent */ true, /* clearHistory */ false, /* isConclusive */ true};
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000093 }
Ady Abraham86ac5c52023-01-11 15:24:03 -080094
95 // Non-active layers are also infrequent
96 if (mLastUpdatedTime < getActiveLayerThreshold(now)) {
ramindani63db24e2023-04-03 10:56:05 -070097 return {/* isFrequent */ false, /* clearHistory */ false, /* isConclusive */ true};
Ady Abraham86ac5c52023-01-11 15:24:03 -080098 }
99
100 // We check whether we can classify this layer as frequent or infrequent:
101 // - frequent: a layer posted kFrequentLayerWindowSize within
102 // kMaxPeriodForFrequentLayerNs of each other.
103 // - infrequent: a layer posted kFrequentLayerWindowSize with longer
104 // gaps than kFrequentLayerWindowSize.
105 // If we can't determine the layer classification yet, we return the last
106 // classification.
107 bool isFrequent = true;
108 bool isInfrequent = true;
Arthur Hungc70bee22023-06-02 01:35:52 +0000109 int32_t smallDirtyCount = 0;
Ady Abraham86ac5c52023-01-11 15:24:03 -0800110 const auto n = mFrameTimes.size() - 1;
111 for (size_t i = 0; i < kFrequentLayerWindowSize - 1; i++) {
112 if (mFrameTimes[n - i].queueTime - mFrameTimes[n - i - 1].queueTime <
113 kMaxPeriodForFrequentLayerNs.count()) {
114 isInfrequent = false;
Arthur Hungc70bee22023-06-02 01:35:52 +0000115 if (mFrameTimes[n - i].presentTime == 0 && mFrameTimes[n - i].isSmallDirty) {
116 smallDirtyCount++;
117 }
Ady Abraham86ac5c52023-01-11 15:24:03 -0800118 } else {
119 isFrequent = false;
120 }
121 }
122
Arthur Hung8144e022023-09-08 10:26:23 +0000123 // Vote the small dirty when a layer contains at least HISTORY_SIZE of small dirty updates.
124 bool isSmallDirty = false;
125 if (smallDirtyCount >= kNumSmallDirtyThreshold) {
126 if (mLastSmallDirtyCount >= HISTORY_SIZE) {
127 isSmallDirty = true;
128 } else {
129 mLastSmallDirtyCount++;
130 }
131 } else {
132 mLastSmallDirtyCount = 0;
133 }
134
Ady Abraham86ac5c52023-01-11 15:24:03 -0800135 if (isFrequent || isInfrequent) {
ramindani63db24e2023-04-03 10:56:05 -0700136 // If the layer was previously inconclusive, we clear
137 // the history as indeterminate layers changed to frequent,
138 // and we should not look at the stale data.
Arthur Hungc70bee22023-06-02 01:35:52 +0000139 return {isFrequent, isFrequent && !mIsFrequencyConclusive, /* isConclusive */ true,
Arthur Hung8144e022023-09-08 10:26:23 +0000140 isSmallDirty};
Ady Abraham86ac5c52023-01-11 15:24:03 -0800141 }
142
143 // If we can't determine whether the layer is frequent or not, we return
ramindani63db24e2023-04-03 10:56:05 -0700144 // the last known classification and mark the layer frequency as inconclusive.
145 isFrequent = !mLastRefreshRate.infrequent;
146
147 // If the layer was previously tagged as animating, we clear
148 // the history as it is likely the layer just changed its behavior,
149 // and we should not look at stale data.
150 return {isFrequent, isFrequent && mLastRefreshRate.animating, /* isConclusive */ false};
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400151}
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000152
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400153Fps LayerInfo::getFps(nsecs_t now) const {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000154 // Find the first active frame
Ady Abraham983e5682020-05-28 16:49:18 -0700155 auto it = mFrameTimes.begin();
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000156 for (; it != mFrameTimes.end(); ++it) {
157 if (it->queueTime >= getActiveLayerThreshold(now)) {
158 break;
159 }
160 }
161
162 const auto numFrames = std::distance(it, mFrameTimes.end());
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100163 if (numFrames < kFrequentLayerWindowSize) {
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400164 return Fps();
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000165 }
166
167 // Layer is considered frequent if the average frame rate is higher than the threshold
168 const auto totalTime = mFrameTimes.back().queueTime - it->queueTime;
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400169 return Fps::fromPeriodNsecs(totalTime / (numFrames - 1));
Ady Abraham8a82ba62020-01-17 12:43:17 -0800170}
171
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100172bool LayerInfo::isAnimating(nsecs_t now) const {
Ady Abraham5def7332020-05-29 16:13:47 -0700173 return mLastAnimationTime >= getActiveLayerThreshold(now);
174}
175
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100176bool LayerInfo::hasEnoughDataForHeuristic() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700177 // The layer had to publish at least HISTORY_SIZE or HISTORY_DURATION of updates
Ady Abrahama61edcb2020-01-30 18:32:03 -0800178 if (mFrameTimes.size() < 2) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700179 ALOGV("fewer than 2 frames recorded: %zu", mFrameTimes.size());
Ady Abrahama61edcb2020-01-30 18:32:03 -0800180 return false;
181 }
182
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000183 if (!isFrameTimeValid(mFrameTimes.front())) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700184 ALOGV("stale frames still captured");
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000185 return false;
186 }
187
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700188 const auto totalDuration = mFrameTimes.back().queueTime - mFrameTimes.front().queueTime;
189 if (mFrameTimes.size() < HISTORY_SIZE && totalDuration < HISTORY_DURATION.count()) {
190 ALOGV("not enough frames captured: %zu | %.2f seconds", mFrameTimes.size(),
191 totalDuration / 1e9f);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800192 return false;
193 }
194
195 return true;
196}
197
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100198std::optional<nsecs_t> LayerInfo::calculateAverageFrameTime() const {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100199 // Ignore frames captured during a mode change
200 const bool isDuringModeChange =
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100201 std::any_of(mFrameTimes.begin(), mFrameTimes.end(),
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100202 [](const auto& frame) { return frame.pendingModeChange; });
203 if (isDuringModeChange) {
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100204 return std::nullopt;
205 }
Ady Abraham32efd542020-05-19 17:49:26 -0700206
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100207 const bool isMissingPresentTime =
208 std::any_of(mFrameTimes.begin(), mFrameTimes.end(),
209 [](auto frame) { return frame.presentTime == 0; });
210 if (isMissingPresentTime && !mLastRefreshRate.reported.isValid()) {
211 // If there are no presentation timestamps and we haven't calculated
212 // one in the past then we can't calculate the refresh rate
213 return std::nullopt;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800214 }
Ady Abrahamc9664832020-05-12 14:16:56 -0700215
Ady Abrahamc9664832020-05-12 14:16:56 -0700216 // Calculate the average frame time based on presentation timestamps. If those
217 // doesn't exist, we look at the time the buffer was queued only. We can do that only if
218 // we calculated a refresh rate based on presentation timestamps in the past. The reason
219 // we look at the queue time is to handle cases where hwui attaches presentation timestamps
220 // when implementing render ahead for specific refresh rates. When hwui no longer provides
221 // presentation timestamps we look at the queue time to see if the current refresh rate still
222 // matches the content.
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700223
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100224 auto getFrameTime = isMissingPresentTime ? [](FrameTimeData data) { return data.queueTime; }
225 : [](FrameTimeData data) { return data.presentTime; };
226
227 nsecs_t totalDeltas = 0;
228 int numDeltas = 0;
Arthur Hungc70bee22023-06-02 01:35:52 +0000229 int32_t smallDirtyCount = 0;
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100230 auto prevFrame = mFrameTimes.begin();
231 for (auto it = mFrameTimes.begin() + 1; it != mFrameTimes.end(); ++it) {
232 const auto currDelta = getFrameTime(*it) - getFrameTime(*prevFrame);
233 if (currDelta < kMinPeriodBetweenFrames) {
234 // Skip this frame, but count the delta into the next frame
235 continue;
236 }
237
Arthur Hungc70bee22023-06-02 01:35:52 +0000238 // If this is a small area update, we don't want to consider it for calculating the average
239 // frame time. Instead, we let the bigger frame updates to drive the calculation.
240 if (it->isSmallDirty && currDelta < kMinPeriodBetweenSmallDirtyFrames) {
241 smallDirtyCount++;
242 continue;
243 }
244
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100245 prevFrame = it;
246
247 if (currDelta > kMaxPeriodBetweenFrames) {
248 // Skip this frame and the current delta.
249 continue;
250 }
251
252 totalDeltas += currDelta;
253 numDeltas++;
254 }
255
Arthur Hungc70bee22023-06-02 01:35:52 +0000256 if (smallDirtyCount > 0) {
257 ATRACE_FORMAT_INSTANT("small dirty = %" PRIu32, smallDirtyCount);
258 }
259
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100260 if (numDeltas == 0) {
261 return std::nullopt;
262 }
263
264 const auto averageFrameTime = static_cast<double>(totalDeltas) / static_cast<double>(numDeltas);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700265 return static_cast<nsecs_t>(averageFrameTime);
Ady Abraham32efd542020-05-19 17:49:26 -0700266}
Ady Abraham8a82ba62020-01-17 12:43:17 -0800267
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400268std::optional<Fps> LayerInfo::calculateRefreshRateIfPossible(const RefreshRateSelector& selector,
269 nsecs_t now) {
Ady Abraham73c3df52023-01-12 18:09:31 -0800270 ATRACE_CALL();
Ady Abraham32efd542020-05-19 17:49:26 -0700271 static constexpr float MARGIN = 1.0f; // 1Hz
Ady Abraham32efd542020-05-19 17:49:26 -0700272 if (!hasEnoughDataForHeuristic()) {
273 ALOGV("Not enough data");
274 return std::nullopt;
275 }
276
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700277 if (const auto averageFrameTime = calculateAverageFrameTime()) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100278 const auto refreshRate = Fps::fromPeriodNsecs(*averageFrameTime);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700279 const bool refreshRateConsistent = mRefreshRateHistory.add(refreshRate, now);
280 if (refreshRateConsistent) {
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400281 const auto knownRefreshRate = selector.findClosestKnownFrameRate(refreshRate);
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700282 using fps_approx_ops::operator!=;
283
284 // To avoid oscillation, use the last calculated refresh rate if it is close enough.
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100285 if (std::abs(mLastRefreshRate.calculated.getValue() - refreshRate.getValue()) >
286 MARGIN &&
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700287 mLastRefreshRate.reported != knownRefreshRate) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700288 mLastRefreshRate.calculated = refreshRate;
289 mLastRefreshRate.reported = knownRefreshRate;
290 }
291
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100292 ALOGV("%s %s rounded to nearest known frame rate %s", mName.c_str(),
293 to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700294 } else {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100295 ALOGV("%s Not stable (%s) returning last known frame rate %s", mName.c_str(),
296 to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700297 }
Ady Abraham32efd542020-05-19 17:49:26 -0700298 }
299
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100300 return mLastRefreshRate.reported.isValid() ? std::make_optional(mLastRefreshRate.reported)
301 : std::nullopt;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800302}
303
Rachel Leece6e0042023-06-27 11:22:54 -0700304LayerInfo::RefreshRateVotes LayerInfo::getRefreshRateVote(const RefreshRateSelector& selector,
305 nsecs_t now) {
Ady Abraham73c3df52023-01-12 18:09:31 -0800306 ATRACE_CALL();
Rachel Leece6e0042023-06-27 11:22:54 -0700307 LayerInfo::RefreshRateVotes votes;
308
Ady Abraham8a82ba62020-01-17 12:43:17 -0800309 if (mLayerVote.type != LayerHistory::LayerVoteType::Heuristic) {
Rachel Leece6e0042023-06-27 11:22:54 -0700310 if (mLayerVote.category != FrameRateCategory::Default) {
Rachel Lee414f4082023-10-23 14:48:44 -0700311 const auto voteType = mLayerVote.type == LayerHistory::LayerVoteType::NoVote
312 ? LayerHistory::LayerVoteType::NoVote
313 : LayerHistory::LayerVoteType::ExplicitCategory;
314 ATRACE_FORMAT_INSTANT("Vote %s (category=%s)", ftl::enum_string(voteType).c_str(),
Rachel Leec0e40bc2023-09-12 13:41:15 -0700315 ftl::enum_string(mLayerVote.category).c_str());
Rachel Lee414f4082023-10-23 14:48:44 -0700316 ALOGV("%s voted %s with category: %s", mName.c_str(),
317 ftl::enum_string(voteType).c_str(),
318 ftl::enum_string(mLayerVote.category).c_str());
319 votes.push_back({voteType, Fps(), Seamlessness::Default, mLayerVote.category,
Rachel Lee67afbea2023-09-28 15:35:07 -0700320 mLayerVote.categorySmoothSwitchOnly});
Rachel Leece6e0042023-06-27 11:22:54 -0700321 }
322
323 if (mLayerVote.fps.isValid() ||
324 mLayerVote.type != LayerHistory::LayerVoteType::ExplicitDefault) {
Rachel Leec0e40bc2023-09-12 13:41:15 -0700325 ATRACE_FORMAT_INSTANT("Vote %s", ftl::enum_string(mLayerVote.type).c_str());
Rachel Lee414f4082023-10-23 14:48:44 -0700326 ALOGV("%s voted %d", mName.c_str(), static_cast<int>(mLayerVote.type));
Rachel Leece6e0042023-06-27 11:22:54 -0700327 votes.push_back(mLayerVote);
328 }
329
330 return votes;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800331 }
332
Ady Abraham5def7332020-05-29 16:13:47 -0700333 if (isAnimating(now)) {
Ady Abraham73c3df52023-01-12 18:09:31 -0800334 ATRACE_FORMAT_INSTANT("animating");
Ady Abraham5def7332020-05-29 16:13:47 -0700335 ALOGV("%s is animating", mName.c_str());
Ady Abraham86ac5c52023-01-11 15:24:03 -0800336 mLastRefreshRate.animating = true;
Rachel Leece6e0042023-06-27 11:22:54 -0700337 votes.push_back({LayerHistory::LayerVoteType::Max, Fps()});
338 return votes;
Ady Abraham5def7332020-05-29 16:13:47 -0700339 }
340
Alec Mouri89f5d4e2023-10-20 17:12:49 +0000341 // Vote for max refresh rate whenever we're front-buffered.
342 if (FlagManager::getInstance().vrr_config() && isFrontBuffered()) {
343 ATRACE_FORMAT_INSTANT("front buffered");
344 ALOGV("%s is front-buffered", mName.c_str());
345 votes.push_back({LayerHistory::LayerVoteType::Max, Fps()});
346 return votes;
347 }
348
ramindani63db24e2023-04-03 10:56:05 -0700349 const LayerInfo::Frequent frequent = isFrequent(now);
350 mIsFrequencyConclusive = frequent.isConclusive;
351 if (!frequent.isFrequent) {
Ady Abraham73c3df52023-01-12 18:09:31 -0800352 ATRACE_FORMAT_INSTANT("infrequent");
Ady Abrahama6b676e2020-05-27 14:29:09 -0700353 ALOGV("%s is infrequent", mName.c_str());
Ady Abraham86ac5c52023-01-11 15:24:03 -0800354 mLastRefreshRate.infrequent = true;
Arthur Hung8144e022023-09-08 10:26:23 +0000355 mLastSmallDirtyCount = 0;
ramindani63db24e2023-04-03 10:56:05 -0700356 // Infrequent layers vote for minimal refresh rate for
Marin Shalamanov29e25402021-04-07 21:09:58 +0200357 // battery saving purposes and also to prevent b/135718869.
Rachel Leece6e0042023-06-27 11:22:54 -0700358 votes.push_back({LayerHistory::LayerVoteType::Min, Fps()});
359 return votes;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800360 }
361
ramindani63db24e2023-04-03 10:56:05 -0700362 if (frequent.clearHistory) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700363 clearHistory(now);
364 }
365
Arthur Hung8144e022023-09-08 10:26:23 +0000366 // Return no vote if the recent frames are small dirty.
Arthur Hungc70bee22023-06-02 01:35:52 +0000367 if (frequent.isSmallDirty && !mLastRefreshRate.reported.isValid()) {
368 ATRACE_FORMAT_INSTANT("NoVote (small dirty)");
369 ALOGV("%s is small dirty", mName.c_str());
370 votes.push_back({LayerHistory::LayerVoteType::NoVote, Fps()});
371 return votes;
372 }
373
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400374 auto refreshRate = calculateRefreshRateIfPossible(selector, now);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800375 if (refreshRate.has_value()) {
Rachel Leec0e40bc2023-09-12 13:41:15 -0700376 ATRACE_FORMAT_INSTANT("calculated (%s)", to_string(*refreshRate).c_str());
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100377 ALOGV("%s calculated refresh rate: %s", mName.c_str(), to_string(*refreshRate).c_str());
Rachel Leece6e0042023-06-27 11:22:54 -0700378 votes.push_back({LayerHistory::LayerVoteType::Heuristic, refreshRate.value()});
379 return votes;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800380 }
381
Rachel Leec0e40bc2023-09-12 13:41:15 -0700382 ATRACE_FORMAT_INSTANT("Max (can't resolve refresh rate)");
Ady Abrahama6b676e2020-05-27 14:29:09 -0700383 ALOGV("%s Max (can't resolve refresh rate)", mName.c_str());
Rachel Leece6e0042023-06-27 11:22:54 -0700384 votes.push_back({LayerHistory::LayerVoteType::Max, Fps()});
385 return votes;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800386}
387
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700388const char* LayerInfo::getTraceTag(LayerHistory::LayerVoteType type) const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700389 if (mTraceTags.count(type) == 0) {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700390 auto tag = "LFPS " + mName + " " + ftl::enum_string(type);
391 mTraceTags.emplace(type, std::move(tag));
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700392 }
393
394 return mTraceTags.at(type).c_str();
395}
396
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000397LayerInfo::FrameRate LayerInfo::getSetFrameRateVote() const {
398 return mLayerProps->setFrameRateVote;
399}
400
401bool LayerInfo::isVisible() const {
402 return mLayerProps->visible;
403}
404
405int32_t LayerInfo::getFrameRateSelectionPriority() const {
406 return mLayerProps->frameRateSelectionPriority;
407}
408
Alec Mouri89f5d4e2023-10-20 17:12:49 +0000409bool LayerInfo::isFrontBuffered() const {
410 return mLayerProps->isFrontBuffered;
411}
412
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000413FloatRect LayerInfo::getBounds() const {
414 return mLayerProps->bounds;
415}
416
417ui::Transform LayerInfo::getTransform() const {
418 return mLayerProps->transform;
419}
420
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100421LayerInfo::RefreshRateHistory::HeuristicTraceTagData
422LayerInfo::RefreshRateHistory::makeHeuristicTraceTagData() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700423 const std::string prefix = "LFPS ";
424 const std::string suffix = "Heuristic ";
425 return {.min = prefix + mName + suffix + "min",
426 .max = prefix + mName + suffix + "max",
427 .consistent = prefix + mName + suffix + "consistent",
428 .average = prefix + mName + suffix + "average"};
429}
430
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100431void LayerInfo::RefreshRateHistory::clear() {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700432 mRefreshRates.clear();
433}
434
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100435bool LayerInfo::RefreshRateHistory::add(Fps refreshRate, nsecs_t now) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700436 mRefreshRates.push_back({refreshRate, now});
437 while (mRefreshRates.size() >= HISTORY_SIZE ||
438 now - mRefreshRates.front().timestamp > HISTORY_DURATION.count()) {
439 mRefreshRates.pop_front();
440 }
441
442 if (CC_UNLIKELY(sTraceEnabled)) {
443 if (!mHeuristicTraceTagData.has_value()) {
444 mHeuristicTraceTagData = makeHeuristicTraceTagData();
445 }
446
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100447 ATRACE_INT(mHeuristicTraceTagData->average.c_str(), refreshRate.getIntValue());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700448 }
449
450 return isConsistent();
451}
452
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100453bool LayerInfo::RefreshRateHistory::isConsistent() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700454 if (mRefreshRates.empty()) return true;
455
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700456 const auto [min, max] =
457 std::minmax_element(mRefreshRates.begin(), mRefreshRates.end(),
458 [](const auto& lhs, const auto& rhs) {
459 return isStrictlyLess(lhs.refreshRate, rhs.refreshRate);
460 });
461
462 const bool consistent =
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100463 max->refreshRate.getValue() - min->refreshRate.getValue() < MARGIN_CONSISTENT_FPS;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700464
465 if (CC_UNLIKELY(sTraceEnabled)) {
466 if (!mHeuristicTraceTagData.has_value()) {
467 mHeuristicTraceTagData = makeHeuristicTraceTagData();
468 }
469
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100470 ATRACE_INT(mHeuristicTraceTagData->max.c_str(), max->refreshRate.getIntValue());
471 ATRACE_INT(mHeuristicTraceTagData->min.c_str(), min->refreshRate.getIntValue());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700472 ATRACE_INT(mHeuristicTraceTagData->consistent.c_str(), consistent);
473 }
474
475 return consistent;
476}
477
Vishnu Nair3fbe3262023-09-29 17:07:00 -0700478FrameRateCompatibility LayerInfo::FrameRate::convertCompatibility(int8_t compatibility) {
Rachel Leece6e0042023-06-27 11:22:54 -0700479 switch (compatibility) {
480 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT:
481 return FrameRateCompatibility::Default;
482 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE:
483 return FrameRateCompatibility::ExactOrMultiple;
484 case ANATIVEWINDOW_FRAME_RATE_EXACT:
485 return FrameRateCompatibility::Exact;
486 case ANATIVEWINDOW_FRAME_RATE_MIN:
487 return FrameRateCompatibility::Min;
488 case ANATIVEWINDOW_FRAME_RATE_NO_VOTE:
489 return FrameRateCompatibility::NoVote;
490 default:
491 LOG_ALWAYS_FATAL("Invalid frame rate compatibility value %d", compatibility);
492 return FrameRateCompatibility::Default;
493 }
494}
495
496Seamlessness LayerInfo::FrameRate::convertChangeFrameRateStrategy(int8_t strategy) {
497 switch (strategy) {
498 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS:
499 return Seamlessness::OnlySeamless;
500 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS:
501 return Seamlessness::SeamedAndSeamless;
502 default:
503 LOG_ALWAYS_FATAL("Invalid change frame sate strategy value %d", strategy);
504 return Seamlessness::Default;
505 }
506}
507
508FrameRateCategory LayerInfo::FrameRate::convertCategory(int8_t category) {
509 switch (category) {
510 case ANATIVEWINDOW_FRAME_RATE_CATEGORY_DEFAULT:
511 return FrameRateCategory::Default;
512 case ANATIVEWINDOW_FRAME_RATE_CATEGORY_NO_PREFERENCE:
513 return FrameRateCategory::NoPreference;
514 case ANATIVEWINDOW_FRAME_RATE_CATEGORY_LOW:
515 return FrameRateCategory::Low;
516 case ANATIVEWINDOW_FRAME_RATE_CATEGORY_NORMAL:
517 return FrameRateCategory::Normal;
518 case ANATIVEWINDOW_FRAME_RATE_CATEGORY_HIGH:
519 return FrameRateCategory::High;
520 default:
521 LOG_ALWAYS_FATAL("Invalid frame rate category value %d", category);
522 return FrameRateCategory::Default;
523 }
524}
525
Rachel Lee58cc90d2023-09-05 18:50:20 -0700526LayerInfo::FrameRateSelectionStrategy LayerInfo::convertFrameRateSelectionStrategy(
527 int8_t strategy) {
528 switch (strategy) {
529 case ANATIVEWINDOW_FRAME_RATE_SELECTION_STRATEGY_SELF:
530 return FrameRateSelectionStrategy::Self;
531 case ANATIVEWINDOW_FRAME_RATE_SELECTION_STRATEGY_OVERRIDE_CHILDREN:
532 return FrameRateSelectionStrategy::OverrideChildren;
533 default:
534 LOG_ALWAYS_FATAL("Invalid frame rate selection strategy value %d", strategy);
535 return FrameRateSelectionStrategy::Self;
536 }
537}
538
Rachel Leece6e0042023-06-27 11:22:54 -0700539bool LayerInfo::FrameRate::isNoVote() const {
Rachel Leed0694bc2023-09-12 14:57:58 -0700540 return vote.type == FrameRateCompatibility::NoVote;
Rachel Leece6e0042023-06-27 11:22:54 -0700541}
542
543bool LayerInfo::FrameRate::isValid() const {
544 return isNoVote() || vote.rate.isValid() || category != FrameRateCategory::Default;
545}
546
547std::ostream& operator<<(std::ostream& stream, const LayerInfo::FrameRate& rate) {
548 return stream << "{rate=" << rate.vote.rate << " type=" << ftl::enum_string(rate.vote.type)
549 << " seamlessness=" << ftl::enum_string(rate.vote.seamlessness) << '}';
550}
551
Ady Abraham8a82ba62020-01-17 12:43:17 -0800552} // namespace android::scheduler
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100553
554// TODO(b/129481165): remove the #pragma below and fix conversion issues
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700555#pragma clang diagnostic pop // ignored "-Wextra"