blob: 8d18769e5a83e4f19290e10b4b2884e6dafc8cec [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
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010078bool LayerInfo::isFrameTimeValid(const FrameTimeData& frameTime) const {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000079 return frameTime.queueTime >= std::chrono::duration_cast<std::chrono::nanoseconds>(
80 mFrameTimeValidSince.time_since_epoch())
81 .count();
82}
Ady Abraham1adbb722020-05-15 11:51:48 -070083
ramindani63db24e2023-04-03 10:56:05 -070084LayerInfo::Frequent LayerInfo::isFrequent(nsecs_t now) const {
Ady Abraham86ac5c52023-01-11 15:24:03 -080085 // If we know nothing about this layer (e.g. after touch event),
86 // we consider it as frequent as it might be the start of an animation.
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010087 if (mFrameTimes.size() < kFrequentLayerWindowSize) {
ramindani63db24e2023-04-03 10:56:05 -070088 return {/* isFrequent */ true, /* clearHistory */ false, /* isConclusive */ true};
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000089 }
Ady Abraham86ac5c52023-01-11 15:24:03 -080090
91 // Non-active layers are also infrequent
92 if (mLastUpdatedTime < getActiveLayerThreshold(now)) {
ramindani63db24e2023-04-03 10:56:05 -070093 return {/* isFrequent */ false, /* clearHistory */ false, /* isConclusive */ true};
Ady Abraham86ac5c52023-01-11 15:24:03 -080094 }
95
96 // We check whether we can classify this layer as frequent or infrequent:
97 // - frequent: a layer posted kFrequentLayerWindowSize within
98 // kMaxPeriodForFrequentLayerNs of each other.
99 // - infrequent: a layer posted kFrequentLayerWindowSize with longer
100 // gaps than kFrequentLayerWindowSize.
101 // If we can't determine the layer classification yet, we return the last
102 // classification.
103 bool isFrequent = true;
104 bool isInfrequent = true;
Arthur Hungc70bee22023-06-02 01:35:52 +0000105 int32_t smallDirtyCount = 0;
Ady Abraham86ac5c52023-01-11 15:24:03 -0800106 const auto n = mFrameTimes.size() - 1;
107 for (size_t i = 0; i < kFrequentLayerWindowSize - 1; i++) {
108 if (mFrameTimes[n - i].queueTime - mFrameTimes[n - i - 1].queueTime <
109 kMaxPeriodForFrequentLayerNs.count()) {
110 isInfrequent = false;
Arthur Hungc70bee22023-06-02 01:35:52 +0000111 if (mFrameTimes[n - i].presentTime == 0 && mFrameTimes[n - i].isSmallDirty) {
112 smallDirtyCount++;
113 }
Ady Abraham86ac5c52023-01-11 15:24:03 -0800114 } else {
115 isFrequent = false;
116 }
117 }
118
Arthur Hung8144e022023-09-08 10:26:23 +0000119 // Vote the small dirty when a layer contains at least HISTORY_SIZE of small dirty updates.
120 bool isSmallDirty = false;
121 if (smallDirtyCount >= kNumSmallDirtyThreshold) {
122 if (mLastSmallDirtyCount >= HISTORY_SIZE) {
123 isSmallDirty = true;
124 } else {
125 mLastSmallDirtyCount++;
126 }
127 } else {
128 mLastSmallDirtyCount = 0;
129 }
130
Ady Abraham86ac5c52023-01-11 15:24:03 -0800131 if (isFrequent || isInfrequent) {
ramindani63db24e2023-04-03 10:56:05 -0700132 // If the layer was previously inconclusive, we clear
133 // the history as indeterminate layers changed to frequent,
134 // and we should not look at the stale data.
Arthur Hungc70bee22023-06-02 01:35:52 +0000135 return {isFrequent, isFrequent && !mIsFrequencyConclusive, /* isConclusive */ true,
Arthur Hung8144e022023-09-08 10:26:23 +0000136 isSmallDirty};
Ady Abraham86ac5c52023-01-11 15:24:03 -0800137 }
138
139 // If we can't determine whether the layer is frequent or not, we return
ramindani63db24e2023-04-03 10:56:05 -0700140 // the last known classification and mark the layer frequency as inconclusive.
141 isFrequent = !mLastRefreshRate.infrequent;
142
143 // If the layer was previously tagged as animating, we clear
144 // the history as it is likely the layer just changed its behavior,
145 // and we should not look at stale data.
146 return {isFrequent, isFrequent && mLastRefreshRate.animating, /* isConclusive */ false};
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400147}
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000148
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400149Fps LayerInfo::getFps(nsecs_t now) const {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000150 // Find the first active frame
Ady Abraham983e5682020-05-28 16:49:18 -0700151 auto it = mFrameTimes.begin();
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000152 for (; it != mFrameTimes.end(); ++it) {
153 if (it->queueTime >= getActiveLayerThreshold(now)) {
154 break;
155 }
156 }
157
158 const auto numFrames = std::distance(it, mFrameTimes.end());
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100159 if (numFrames < kFrequentLayerWindowSize) {
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400160 return Fps();
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000161 }
162
163 // Layer is considered frequent if the average frame rate is higher than the threshold
164 const auto totalTime = mFrameTimes.back().queueTime - it->queueTime;
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400165 return Fps::fromPeriodNsecs(totalTime / (numFrames - 1));
Ady Abraham8a82ba62020-01-17 12:43:17 -0800166}
167
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100168bool LayerInfo::isAnimating(nsecs_t now) const {
Ady Abraham5def7332020-05-29 16:13:47 -0700169 return mLastAnimationTime >= getActiveLayerThreshold(now);
170}
171
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100172bool LayerInfo::hasEnoughDataForHeuristic() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700173 // The layer had to publish at least HISTORY_SIZE or HISTORY_DURATION of updates
Ady Abrahama61edcb2020-01-30 18:32:03 -0800174 if (mFrameTimes.size() < 2) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700175 ALOGV("fewer than 2 frames recorded: %zu", mFrameTimes.size());
Ady Abrahama61edcb2020-01-30 18:32:03 -0800176 return false;
177 }
178
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000179 if (!isFrameTimeValid(mFrameTimes.front())) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700180 ALOGV("stale frames still captured");
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000181 return false;
182 }
183
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700184 const auto totalDuration = mFrameTimes.back().queueTime - mFrameTimes.front().queueTime;
185 if (mFrameTimes.size() < HISTORY_SIZE && totalDuration < HISTORY_DURATION.count()) {
186 ALOGV("not enough frames captured: %zu | %.2f seconds", mFrameTimes.size(),
187 totalDuration / 1e9f);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800188 return false;
189 }
190
191 return true;
192}
193
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100194std::optional<nsecs_t> LayerInfo::calculateAverageFrameTime() const {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100195 // Ignore frames captured during a mode change
196 const bool isDuringModeChange =
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100197 std::any_of(mFrameTimes.begin(), mFrameTimes.end(),
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100198 [](const auto& frame) { return frame.pendingModeChange; });
199 if (isDuringModeChange) {
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100200 return std::nullopt;
201 }
Ady Abraham32efd542020-05-19 17:49:26 -0700202
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100203 const bool isMissingPresentTime =
204 std::any_of(mFrameTimes.begin(), mFrameTimes.end(),
205 [](auto frame) { return frame.presentTime == 0; });
206 if (isMissingPresentTime && !mLastRefreshRate.reported.isValid()) {
207 // If there are no presentation timestamps and we haven't calculated
208 // one in the past then we can't calculate the refresh rate
209 return std::nullopt;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800210 }
Ady Abrahamc9664832020-05-12 14:16:56 -0700211
Ady Abrahamc9664832020-05-12 14:16:56 -0700212 // Calculate the average frame time based on presentation timestamps. If those
213 // doesn't exist, we look at the time the buffer was queued only. We can do that only if
214 // we calculated a refresh rate based on presentation timestamps in the past. The reason
215 // we look at the queue time is to handle cases where hwui attaches presentation timestamps
216 // when implementing render ahead for specific refresh rates. When hwui no longer provides
217 // presentation timestamps we look at the queue time to see if the current refresh rate still
218 // matches the content.
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700219
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100220 auto getFrameTime = isMissingPresentTime ? [](FrameTimeData data) { return data.queueTime; }
221 : [](FrameTimeData data) { return data.presentTime; };
222
223 nsecs_t totalDeltas = 0;
224 int numDeltas = 0;
Arthur Hungc70bee22023-06-02 01:35:52 +0000225 int32_t smallDirtyCount = 0;
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100226 auto prevFrame = mFrameTimes.begin();
227 for (auto it = mFrameTimes.begin() + 1; it != mFrameTimes.end(); ++it) {
228 const auto currDelta = getFrameTime(*it) - getFrameTime(*prevFrame);
229 if (currDelta < kMinPeriodBetweenFrames) {
230 // Skip this frame, but count the delta into the next frame
231 continue;
232 }
233
Arthur Hungc70bee22023-06-02 01:35:52 +0000234 // If this is a small area update, we don't want to consider it for calculating the average
235 // frame time. Instead, we let the bigger frame updates to drive the calculation.
236 if (it->isSmallDirty && currDelta < kMinPeriodBetweenSmallDirtyFrames) {
237 smallDirtyCount++;
238 continue;
239 }
240
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100241 prevFrame = it;
242
243 if (currDelta > kMaxPeriodBetweenFrames) {
244 // Skip this frame and the current delta.
245 continue;
246 }
247
248 totalDeltas += currDelta;
249 numDeltas++;
250 }
251
Arthur Hungc70bee22023-06-02 01:35:52 +0000252 if (smallDirtyCount > 0) {
253 ATRACE_FORMAT_INSTANT("small dirty = %" PRIu32, smallDirtyCount);
254 }
255
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100256 if (numDeltas == 0) {
257 return std::nullopt;
258 }
259
260 const auto averageFrameTime = static_cast<double>(totalDeltas) / static_cast<double>(numDeltas);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700261 return static_cast<nsecs_t>(averageFrameTime);
Ady Abraham32efd542020-05-19 17:49:26 -0700262}
Ady Abraham8a82ba62020-01-17 12:43:17 -0800263
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400264std::optional<Fps> LayerInfo::calculateRefreshRateIfPossible(const RefreshRateSelector& selector,
265 nsecs_t now) {
Ady Abraham73c3df52023-01-12 18:09:31 -0800266 ATRACE_CALL();
Ady Abraham32efd542020-05-19 17:49:26 -0700267 static constexpr float MARGIN = 1.0f; // 1Hz
Ady Abraham32efd542020-05-19 17:49:26 -0700268 if (!hasEnoughDataForHeuristic()) {
269 ALOGV("Not enough data");
270 return std::nullopt;
271 }
272
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700273 if (const auto averageFrameTime = calculateAverageFrameTime()) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100274 const auto refreshRate = Fps::fromPeriodNsecs(*averageFrameTime);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700275 const bool refreshRateConsistent = mRefreshRateHistory.add(refreshRate, now);
276 if (refreshRateConsistent) {
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400277 const auto knownRefreshRate = selector.findClosestKnownFrameRate(refreshRate);
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700278 using fps_approx_ops::operator!=;
279
280 // To avoid oscillation, use the last calculated refresh rate if it is close enough.
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100281 if (std::abs(mLastRefreshRate.calculated.getValue() - refreshRate.getValue()) >
282 MARGIN &&
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700283 mLastRefreshRate.reported != knownRefreshRate) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700284 mLastRefreshRate.calculated = refreshRate;
285 mLastRefreshRate.reported = knownRefreshRate;
286 }
287
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100288 ALOGV("%s %s rounded to nearest known frame rate %s", mName.c_str(),
289 to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700290 } else {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100291 ALOGV("%s Not stable (%s) returning last known frame rate %s", mName.c_str(),
292 to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700293 }
Ady Abraham32efd542020-05-19 17:49:26 -0700294 }
295
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100296 return mLastRefreshRate.reported.isValid() ? std::make_optional(mLastRefreshRate.reported)
297 : std::nullopt;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800298}
299
Rachel Leece6e0042023-06-27 11:22:54 -0700300LayerInfo::RefreshRateVotes LayerInfo::getRefreshRateVote(const RefreshRateSelector& selector,
301 nsecs_t now) {
Ady Abraham73c3df52023-01-12 18:09:31 -0800302 ATRACE_CALL();
Rachel Leece6e0042023-06-27 11:22:54 -0700303 LayerInfo::RefreshRateVotes votes;
304
Ady Abraham8a82ba62020-01-17 12:43:17 -0800305 if (mLayerVote.type != LayerHistory::LayerVoteType::Heuristic) {
Rachel Leece6e0042023-06-27 11:22:54 -0700306 if (mLayerVote.category != FrameRateCategory::Default) {
Rachel Lee414f4082023-10-23 14:48:44 -0700307 const auto voteType = mLayerVote.type == LayerHistory::LayerVoteType::NoVote
308 ? LayerHistory::LayerVoteType::NoVote
309 : LayerHistory::LayerVoteType::ExplicitCategory;
310 ATRACE_FORMAT_INSTANT("Vote %s (category=%s)", ftl::enum_string(voteType).c_str(),
Rachel Leec0e40bc2023-09-12 13:41:15 -0700311 ftl::enum_string(mLayerVote.category).c_str());
Rachel Lee414f4082023-10-23 14:48:44 -0700312 ALOGV("%s voted %s with category: %s", mName.c_str(),
313 ftl::enum_string(voteType).c_str(),
314 ftl::enum_string(mLayerVote.category).c_str());
315 votes.push_back({voteType, Fps(), Seamlessness::Default, mLayerVote.category,
Rachel Lee67afbea2023-09-28 15:35:07 -0700316 mLayerVote.categorySmoothSwitchOnly});
Rachel Leece6e0042023-06-27 11:22:54 -0700317 }
318
319 if (mLayerVote.fps.isValid() ||
320 mLayerVote.type != LayerHistory::LayerVoteType::ExplicitDefault) {
Rachel Leec0e40bc2023-09-12 13:41:15 -0700321 ATRACE_FORMAT_INSTANT("Vote %s", ftl::enum_string(mLayerVote.type).c_str());
Rachel Lee414f4082023-10-23 14:48:44 -0700322 ALOGV("%s voted %d", mName.c_str(), static_cast<int>(mLayerVote.type));
Rachel Leece6e0042023-06-27 11:22:54 -0700323 votes.push_back(mLayerVote);
324 }
325
326 return votes;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800327 }
328
Ady Abraham5def7332020-05-29 16:13:47 -0700329 if (isAnimating(now)) {
Ady Abraham73c3df52023-01-12 18:09:31 -0800330 ATRACE_FORMAT_INSTANT("animating");
Ady Abraham5def7332020-05-29 16:13:47 -0700331 ALOGV("%s is animating", mName.c_str());
Ady Abraham86ac5c52023-01-11 15:24:03 -0800332 mLastRefreshRate.animating = true;
Rachel Leece6e0042023-06-27 11:22:54 -0700333 votes.push_back({LayerHistory::LayerVoteType::Max, Fps()});
334 return votes;
Ady Abraham5def7332020-05-29 16:13:47 -0700335 }
336
Alec Mouri89f5d4e2023-10-20 17:12:49 +0000337 // Vote for max refresh rate whenever we're front-buffered.
338 if (FlagManager::getInstance().vrr_config() && isFrontBuffered()) {
339 ATRACE_FORMAT_INSTANT("front buffered");
340 ALOGV("%s is front-buffered", mName.c_str());
341 votes.push_back({LayerHistory::LayerVoteType::Max, Fps()});
342 return votes;
343 }
344
ramindani63db24e2023-04-03 10:56:05 -0700345 const LayerInfo::Frequent frequent = isFrequent(now);
346 mIsFrequencyConclusive = frequent.isConclusive;
347 if (!frequent.isFrequent) {
Ady Abraham73c3df52023-01-12 18:09:31 -0800348 ATRACE_FORMAT_INSTANT("infrequent");
Ady Abrahama6b676e2020-05-27 14:29:09 -0700349 ALOGV("%s is infrequent", mName.c_str());
Ady Abraham86ac5c52023-01-11 15:24:03 -0800350 mLastRefreshRate.infrequent = true;
Arthur Hung8144e022023-09-08 10:26:23 +0000351 mLastSmallDirtyCount = 0;
ramindani63db24e2023-04-03 10:56:05 -0700352 // Infrequent layers vote for minimal refresh rate for
Marin Shalamanov29e25402021-04-07 21:09:58 +0200353 // battery saving purposes and also to prevent b/135718869.
Rachel Leece6e0042023-06-27 11:22:54 -0700354 votes.push_back({LayerHistory::LayerVoteType::Min, Fps()});
355 return votes;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800356 }
357
ramindani63db24e2023-04-03 10:56:05 -0700358 if (frequent.clearHistory) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700359 clearHistory(now);
360 }
361
Arthur Hung8144e022023-09-08 10:26:23 +0000362 // Return no vote if the recent frames are small dirty.
Arthur Hungc70bee22023-06-02 01:35:52 +0000363 if (frequent.isSmallDirty && !mLastRefreshRate.reported.isValid()) {
364 ATRACE_FORMAT_INSTANT("NoVote (small dirty)");
365 ALOGV("%s is small dirty", mName.c_str());
366 votes.push_back({LayerHistory::LayerVoteType::NoVote, Fps()});
367 return votes;
368 }
369
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400370 auto refreshRate = calculateRefreshRateIfPossible(selector, now);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800371 if (refreshRate.has_value()) {
Rachel Leec0e40bc2023-09-12 13:41:15 -0700372 ATRACE_FORMAT_INSTANT("calculated (%s)", to_string(*refreshRate).c_str());
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100373 ALOGV("%s calculated refresh rate: %s", mName.c_str(), to_string(*refreshRate).c_str());
Rachel Leece6e0042023-06-27 11:22:54 -0700374 votes.push_back({LayerHistory::LayerVoteType::Heuristic, refreshRate.value()});
375 return votes;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800376 }
377
Rachel Leec0e40bc2023-09-12 13:41:15 -0700378 ATRACE_FORMAT_INSTANT("Max (can't resolve refresh rate)");
Ady Abrahama6b676e2020-05-27 14:29:09 -0700379 ALOGV("%s Max (can't resolve refresh rate)", mName.c_str());
Rachel Leece6e0042023-06-27 11:22:54 -0700380 votes.push_back({LayerHistory::LayerVoteType::Max, Fps()});
381 return votes;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800382}
383
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700384const char* LayerInfo::getTraceTag(LayerHistory::LayerVoteType type) const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700385 if (mTraceTags.count(type) == 0) {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700386 auto tag = "LFPS " + mName + " " + ftl::enum_string(type);
387 mTraceTags.emplace(type, std::move(tag));
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700388 }
389
390 return mTraceTags.at(type).c_str();
391}
392
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000393LayerInfo::FrameRate LayerInfo::getSetFrameRateVote() const {
394 return mLayerProps->setFrameRateVote;
395}
396
397bool LayerInfo::isVisible() const {
398 return mLayerProps->visible;
399}
400
401int32_t LayerInfo::getFrameRateSelectionPriority() const {
402 return mLayerProps->frameRateSelectionPriority;
403}
404
Alec Mouri89f5d4e2023-10-20 17:12:49 +0000405bool LayerInfo::isFrontBuffered() const {
406 return mLayerProps->isFrontBuffered;
407}
408
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000409FloatRect LayerInfo::getBounds() const {
410 return mLayerProps->bounds;
411}
412
413ui::Transform LayerInfo::getTransform() const {
414 return mLayerProps->transform;
415}
416
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100417LayerInfo::RefreshRateHistory::HeuristicTraceTagData
418LayerInfo::RefreshRateHistory::makeHeuristicTraceTagData() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700419 const std::string prefix = "LFPS ";
420 const std::string suffix = "Heuristic ";
421 return {.min = prefix + mName + suffix + "min",
422 .max = prefix + mName + suffix + "max",
423 .consistent = prefix + mName + suffix + "consistent",
424 .average = prefix + mName + suffix + "average"};
425}
426
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100427void LayerInfo::RefreshRateHistory::clear() {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700428 mRefreshRates.clear();
429}
430
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100431bool LayerInfo::RefreshRateHistory::add(Fps refreshRate, nsecs_t now) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700432 mRefreshRates.push_back({refreshRate, now});
433 while (mRefreshRates.size() >= HISTORY_SIZE ||
434 now - mRefreshRates.front().timestamp > HISTORY_DURATION.count()) {
435 mRefreshRates.pop_front();
436 }
437
438 if (CC_UNLIKELY(sTraceEnabled)) {
439 if (!mHeuristicTraceTagData.has_value()) {
440 mHeuristicTraceTagData = makeHeuristicTraceTagData();
441 }
442
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100443 ATRACE_INT(mHeuristicTraceTagData->average.c_str(), refreshRate.getIntValue());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700444 }
445
446 return isConsistent();
447}
448
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100449bool LayerInfo::RefreshRateHistory::isConsistent() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700450 if (mRefreshRates.empty()) return true;
451
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700452 const auto [min, max] =
453 std::minmax_element(mRefreshRates.begin(), mRefreshRates.end(),
454 [](const auto& lhs, const auto& rhs) {
455 return isStrictlyLess(lhs.refreshRate, rhs.refreshRate);
456 });
457
458 const bool consistent =
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100459 max->refreshRate.getValue() - min->refreshRate.getValue() < MARGIN_CONSISTENT_FPS;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700460
461 if (CC_UNLIKELY(sTraceEnabled)) {
462 if (!mHeuristicTraceTagData.has_value()) {
463 mHeuristicTraceTagData = makeHeuristicTraceTagData();
464 }
465
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100466 ATRACE_INT(mHeuristicTraceTagData->max.c_str(), max->refreshRate.getIntValue());
467 ATRACE_INT(mHeuristicTraceTagData->min.c_str(), min->refreshRate.getIntValue());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700468 ATRACE_INT(mHeuristicTraceTagData->consistent.c_str(), consistent);
469 }
470
471 return consistent;
472}
473
Vishnu Nair3fbe3262023-09-29 17:07:00 -0700474FrameRateCompatibility LayerInfo::FrameRate::convertCompatibility(int8_t compatibility) {
Rachel Leece6e0042023-06-27 11:22:54 -0700475 switch (compatibility) {
476 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT:
477 return FrameRateCompatibility::Default;
478 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE:
479 return FrameRateCompatibility::ExactOrMultiple;
480 case ANATIVEWINDOW_FRAME_RATE_EXACT:
481 return FrameRateCompatibility::Exact;
482 case ANATIVEWINDOW_FRAME_RATE_MIN:
483 return FrameRateCompatibility::Min;
484 case ANATIVEWINDOW_FRAME_RATE_NO_VOTE:
485 return FrameRateCompatibility::NoVote;
486 default:
487 LOG_ALWAYS_FATAL("Invalid frame rate compatibility value %d", compatibility);
488 return FrameRateCompatibility::Default;
489 }
490}
491
492Seamlessness LayerInfo::FrameRate::convertChangeFrameRateStrategy(int8_t strategy) {
493 switch (strategy) {
494 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS:
495 return Seamlessness::OnlySeamless;
496 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS:
497 return Seamlessness::SeamedAndSeamless;
498 default:
499 LOG_ALWAYS_FATAL("Invalid change frame sate strategy value %d", strategy);
500 return Seamlessness::Default;
501 }
502}
503
504FrameRateCategory LayerInfo::FrameRate::convertCategory(int8_t category) {
505 switch (category) {
506 case ANATIVEWINDOW_FRAME_RATE_CATEGORY_DEFAULT:
507 return FrameRateCategory::Default;
508 case ANATIVEWINDOW_FRAME_RATE_CATEGORY_NO_PREFERENCE:
509 return FrameRateCategory::NoPreference;
510 case ANATIVEWINDOW_FRAME_RATE_CATEGORY_LOW:
511 return FrameRateCategory::Low;
512 case ANATIVEWINDOW_FRAME_RATE_CATEGORY_NORMAL:
513 return FrameRateCategory::Normal;
514 case ANATIVEWINDOW_FRAME_RATE_CATEGORY_HIGH:
515 return FrameRateCategory::High;
516 default:
517 LOG_ALWAYS_FATAL("Invalid frame rate category value %d", category);
518 return FrameRateCategory::Default;
519 }
520}
521
Rachel Lee58cc90d2023-09-05 18:50:20 -0700522LayerInfo::FrameRateSelectionStrategy LayerInfo::convertFrameRateSelectionStrategy(
523 int8_t strategy) {
524 switch (strategy) {
525 case ANATIVEWINDOW_FRAME_RATE_SELECTION_STRATEGY_SELF:
526 return FrameRateSelectionStrategy::Self;
527 case ANATIVEWINDOW_FRAME_RATE_SELECTION_STRATEGY_OVERRIDE_CHILDREN:
528 return FrameRateSelectionStrategy::OverrideChildren;
529 default:
530 LOG_ALWAYS_FATAL("Invalid frame rate selection strategy value %d", strategy);
531 return FrameRateSelectionStrategy::Self;
532 }
533}
534
Rachel Leece6e0042023-06-27 11:22:54 -0700535bool LayerInfo::FrameRate::isNoVote() const {
Rachel Leed0694bc2023-09-12 14:57:58 -0700536 return vote.type == FrameRateCompatibility::NoVote;
Rachel Leece6e0042023-06-27 11:22:54 -0700537}
538
539bool LayerInfo::FrameRate::isValid() const {
540 return isNoVote() || vote.rate.isValid() || category != FrameRateCategory::Default;
541}
542
543std::ostream& operator<<(std::ostream& stream, const LayerInfo::FrameRate& rate) {
544 return stream << "{rate=" << rate.vote.rate << " type=" << ftl::enum_string(rate.vote.type)
545 << " seamlessness=" << ftl::enum_string(rate.vote.seamlessness) << '}';
546}
547
Ady Abraham8a82ba62020-01-17 12:43:17 -0800548} // namespace android::scheduler
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100549
550// TODO(b/129481165): remove the #pragma below and fix conversion issues
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700551#pragma clang diagnostic pop // ignored "-Wextra"