blob: 03844ef18375a4f8638e2b105c63d181ece01ebf [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
119 if (isFrequent || isInfrequent) {
ramindani63db24e2023-04-03 10:56:05 -0700120 // If the layer was previously inconclusive, we clear
121 // the history as indeterminate layers changed to frequent,
122 // and we should not look at the stale data.
Arthur Hungc70bee22023-06-02 01:35:52 +0000123 return {isFrequent, isFrequent && !mIsFrequencyConclusive, /* isConclusive */ true,
124 /* isSmallDirty */ smallDirtyCount >= kNumSmallDirtyThreshold};
Ady Abraham86ac5c52023-01-11 15:24:03 -0800125 }
126
127 // If we can't determine whether the layer is frequent or not, we return
ramindani63db24e2023-04-03 10:56:05 -0700128 // the last known classification and mark the layer frequency as inconclusive.
129 isFrequent = !mLastRefreshRate.infrequent;
130
131 // If the layer was previously tagged as animating, we clear
132 // the history as it is likely the layer just changed its behavior,
133 // and we should not look at stale data.
134 return {isFrequent, isFrequent && mLastRefreshRate.animating, /* isConclusive */ false};
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400135}
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000136
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400137Fps LayerInfo::getFps(nsecs_t now) const {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000138 // Find the first active frame
Ady Abraham983e5682020-05-28 16:49:18 -0700139 auto it = mFrameTimes.begin();
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000140 for (; it != mFrameTimes.end(); ++it) {
141 if (it->queueTime >= getActiveLayerThreshold(now)) {
142 break;
143 }
144 }
145
146 const auto numFrames = std::distance(it, mFrameTimes.end());
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100147 if (numFrames < kFrequentLayerWindowSize) {
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400148 return Fps();
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000149 }
150
151 // Layer is considered frequent if the average frame rate is higher than the threshold
152 const auto totalTime = mFrameTimes.back().queueTime - it->queueTime;
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400153 return Fps::fromPeriodNsecs(totalTime / (numFrames - 1));
Ady Abraham8a82ba62020-01-17 12:43:17 -0800154}
155
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100156bool LayerInfo::isAnimating(nsecs_t now) const {
Ady Abraham5def7332020-05-29 16:13:47 -0700157 return mLastAnimationTime >= getActiveLayerThreshold(now);
158}
159
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100160bool LayerInfo::hasEnoughDataForHeuristic() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700161 // The layer had to publish at least HISTORY_SIZE or HISTORY_DURATION of updates
Ady Abrahama61edcb2020-01-30 18:32:03 -0800162 if (mFrameTimes.size() < 2) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700163 ALOGV("fewer than 2 frames recorded: %zu", mFrameTimes.size());
Ady Abrahama61edcb2020-01-30 18:32:03 -0800164 return false;
165 }
166
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000167 if (!isFrameTimeValid(mFrameTimes.front())) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700168 ALOGV("stale frames still captured");
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000169 return false;
170 }
171
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700172 const auto totalDuration = mFrameTimes.back().queueTime - mFrameTimes.front().queueTime;
173 if (mFrameTimes.size() < HISTORY_SIZE && totalDuration < HISTORY_DURATION.count()) {
174 ALOGV("not enough frames captured: %zu | %.2f seconds", mFrameTimes.size(),
175 totalDuration / 1e9f);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800176 return false;
177 }
178
179 return true;
180}
181
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100182std::optional<nsecs_t> LayerInfo::calculateAverageFrameTime() const {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100183 // Ignore frames captured during a mode change
184 const bool isDuringModeChange =
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100185 std::any_of(mFrameTimes.begin(), mFrameTimes.end(),
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100186 [](const auto& frame) { return frame.pendingModeChange; });
187 if (isDuringModeChange) {
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100188 return std::nullopt;
189 }
Ady Abraham32efd542020-05-19 17:49:26 -0700190
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100191 const bool isMissingPresentTime =
192 std::any_of(mFrameTimes.begin(), mFrameTimes.end(),
193 [](auto frame) { return frame.presentTime == 0; });
194 if (isMissingPresentTime && !mLastRefreshRate.reported.isValid()) {
195 // If there are no presentation timestamps and we haven't calculated
196 // one in the past then we can't calculate the refresh rate
197 return std::nullopt;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800198 }
Ady Abrahamc9664832020-05-12 14:16:56 -0700199
Ady Abrahamc9664832020-05-12 14:16:56 -0700200 // Calculate the average frame time based on presentation timestamps. If those
201 // doesn't exist, we look at the time the buffer was queued only. We can do that only if
202 // we calculated a refresh rate based on presentation timestamps in the past. The reason
203 // we look at the queue time is to handle cases where hwui attaches presentation timestamps
204 // when implementing render ahead for specific refresh rates. When hwui no longer provides
205 // presentation timestamps we look at the queue time to see if the current refresh rate still
206 // matches the content.
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700207
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100208 auto getFrameTime = isMissingPresentTime ? [](FrameTimeData data) { return data.queueTime; }
209 : [](FrameTimeData data) { return data.presentTime; };
210
211 nsecs_t totalDeltas = 0;
212 int numDeltas = 0;
Arthur Hungc70bee22023-06-02 01:35:52 +0000213 int32_t smallDirtyCount = 0;
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100214 auto prevFrame = mFrameTimes.begin();
215 for (auto it = mFrameTimes.begin() + 1; it != mFrameTimes.end(); ++it) {
216 const auto currDelta = getFrameTime(*it) - getFrameTime(*prevFrame);
217 if (currDelta < kMinPeriodBetweenFrames) {
218 // Skip this frame, but count the delta into the next frame
219 continue;
220 }
221
Arthur Hungc70bee22023-06-02 01:35:52 +0000222 // If this is a small area update, we don't want to consider it for calculating the average
223 // frame time. Instead, we let the bigger frame updates to drive the calculation.
224 if (it->isSmallDirty && currDelta < kMinPeriodBetweenSmallDirtyFrames) {
225 smallDirtyCount++;
226 continue;
227 }
228
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100229 prevFrame = it;
230
231 if (currDelta > kMaxPeriodBetweenFrames) {
232 // Skip this frame and the current delta.
233 continue;
234 }
235
236 totalDeltas += currDelta;
237 numDeltas++;
238 }
239
Arthur Hungc70bee22023-06-02 01:35:52 +0000240 if (smallDirtyCount > 0) {
241 ATRACE_FORMAT_INSTANT("small dirty = %" PRIu32, smallDirtyCount);
242 }
243
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100244 if (numDeltas == 0) {
245 return std::nullopt;
246 }
247
248 const auto averageFrameTime = static_cast<double>(totalDeltas) / static_cast<double>(numDeltas);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700249 return static_cast<nsecs_t>(averageFrameTime);
Ady Abraham32efd542020-05-19 17:49:26 -0700250}
Ady Abraham8a82ba62020-01-17 12:43:17 -0800251
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400252std::optional<Fps> LayerInfo::calculateRefreshRateIfPossible(const RefreshRateSelector& selector,
253 nsecs_t now) {
Ady Abraham73c3df52023-01-12 18:09:31 -0800254 ATRACE_CALL();
Ady Abraham32efd542020-05-19 17:49:26 -0700255 static constexpr float MARGIN = 1.0f; // 1Hz
Ady Abraham32efd542020-05-19 17:49:26 -0700256 if (!hasEnoughDataForHeuristic()) {
257 ALOGV("Not enough data");
258 return std::nullopt;
259 }
260
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700261 if (const auto averageFrameTime = calculateAverageFrameTime()) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100262 const auto refreshRate = Fps::fromPeriodNsecs(*averageFrameTime);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700263 const bool refreshRateConsistent = mRefreshRateHistory.add(refreshRate, now);
264 if (refreshRateConsistent) {
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400265 const auto knownRefreshRate = selector.findClosestKnownFrameRate(refreshRate);
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700266 using fps_approx_ops::operator!=;
267
268 // To avoid oscillation, use the last calculated refresh rate if it is close enough.
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100269 if (std::abs(mLastRefreshRate.calculated.getValue() - refreshRate.getValue()) >
270 MARGIN &&
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700271 mLastRefreshRate.reported != knownRefreshRate) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700272 mLastRefreshRate.calculated = refreshRate;
273 mLastRefreshRate.reported = knownRefreshRate;
274 }
275
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100276 ALOGV("%s %s rounded to nearest known frame rate %s", mName.c_str(),
277 to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700278 } else {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100279 ALOGV("%s Not stable (%s) returning last known frame rate %s", mName.c_str(),
280 to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700281 }
Ady Abraham32efd542020-05-19 17:49:26 -0700282 }
283
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100284 return mLastRefreshRate.reported.isValid() ? std::make_optional(mLastRefreshRate.reported)
285 : std::nullopt;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800286}
287
Rachel Leece6e0042023-06-27 11:22:54 -0700288LayerInfo::RefreshRateVotes LayerInfo::getRefreshRateVote(const RefreshRateSelector& selector,
289 nsecs_t now) {
Ady Abraham73c3df52023-01-12 18:09:31 -0800290 ATRACE_CALL();
Rachel Leece6e0042023-06-27 11:22:54 -0700291 LayerInfo::RefreshRateVotes votes;
292
Ady Abraham8a82ba62020-01-17 12:43:17 -0800293 if (mLayerVote.type != LayerHistory::LayerVoteType::Heuristic) {
Rachel Leece6e0042023-06-27 11:22:54 -0700294 if (mLayerVote.category != FrameRateCategory::Default) {
Rachel Leec0e40bc2023-09-12 13:41:15 -0700295 ATRACE_FORMAT_INSTANT("ExplicitCategory (%s)",
296 ftl::enum_string(mLayerVote.category).c_str());
Rachel Leece6e0042023-06-27 11:22:54 -0700297 ALOGV("%s uses frame rate category: %d", mName.c_str(),
298 static_cast<int>(mLayerVote.category));
Rachel Leef377b362023-09-06 15:01:06 -0700299 votes.push_back({LayerHistory::LayerVoteType::ExplicitCategory, Fps(),
Rachel Leece6e0042023-06-27 11:22:54 -0700300 Seamlessness::Default, mLayerVote.category});
301 }
302
303 if (mLayerVote.fps.isValid() ||
304 mLayerVote.type != LayerHistory::LayerVoteType::ExplicitDefault) {
Rachel Leec0e40bc2023-09-12 13:41:15 -0700305 ATRACE_FORMAT_INSTANT("Vote %s", ftl::enum_string(mLayerVote.type).c_str());
Rachel Leece6e0042023-06-27 11:22:54 -0700306 ALOGV("%s voted %d ", mName.c_str(), static_cast<int>(mLayerVote.type));
307 votes.push_back(mLayerVote);
308 }
309
310 return votes;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800311 }
312
Ady Abraham5def7332020-05-29 16:13:47 -0700313 if (isAnimating(now)) {
Ady Abraham73c3df52023-01-12 18:09:31 -0800314 ATRACE_FORMAT_INSTANT("animating");
Ady Abraham5def7332020-05-29 16:13:47 -0700315 ALOGV("%s is animating", mName.c_str());
Ady Abraham86ac5c52023-01-11 15:24:03 -0800316 mLastRefreshRate.animating = true;
Rachel Leece6e0042023-06-27 11:22:54 -0700317 votes.push_back({LayerHistory::LayerVoteType::Max, Fps()});
318 return votes;
Ady Abraham5def7332020-05-29 16:13:47 -0700319 }
320
ramindani63db24e2023-04-03 10:56:05 -0700321 const LayerInfo::Frequent frequent = isFrequent(now);
322 mIsFrequencyConclusive = frequent.isConclusive;
323 if (!frequent.isFrequent) {
Ady Abraham73c3df52023-01-12 18:09:31 -0800324 ATRACE_FORMAT_INSTANT("infrequent");
Ady Abrahama6b676e2020-05-27 14:29:09 -0700325 ALOGV("%s is infrequent", mName.c_str());
Ady Abraham86ac5c52023-01-11 15:24:03 -0800326 mLastRefreshRate.infrequent = true;
ramindani63db24e2023-04-03 10:56:05 -0700327 // Infrequent layers vote for minimal refresh rate for
Marin Shalamanov29e25402021-04-07 21:09:58 +0200328 // battery saving purposes and also to prevent b/135718869.
Rachel Leece6e0042023-06-27 11:22:54 -0700329 votes.push_back({LayerHistory::LayerVoteType::Min, Fps()});
330 return votes;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800331 }
332
ramindani63db24e2023-04-03 10:56:05 -0700333 if (frequent.clearHistory) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700334 clearHistory(now);
335 }
336
Arthur Hungc70bee22023-06-02 01:35:52 +0000337 // Return no vote if the latest frames are small dirty.
338 if (frequent.isSmallDirty && !mLastRefreshRate.reported.isValid()) {
339 ATRACE_FORMAT_INSTANT("NoVote (small dirty)");
340 ALOGV("%s is small dirty", mName.c_str());
341 votes.push_back({LayerHistory::LayerVoteType::NoVote, Fps()});
342 return votes;
343 }
344
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400345 auto refreshRate = calculateRefreshRateIfPossible(selector, now);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800346 if (refreshRate.has_value()) {
Rachel Leec0e40bc2023-09-12 13:41:15 -0700347 ATRACE_FORMAT_INSTANT("calculated (%s)", to_string(*refreshRate).c_str());
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100348 ALOGV("%s calculated refresh rate: %s", mName.c_str(), to_string(*refreshRate).c_str());
Rachel Leece6e0042023-06-27 11:22:54 -0700349 votes.push_back({LayerHistory::LayerVoteType::Heuristic, refreshRate.value()});
350 return votes;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800351 }
352
Rachel Leec0e40bc2023-09-12 13:41:15 -0700353 ATRACE_FORMAT_INSTANT("Max (can't resolve refresh rate)");
Ady Abrahama6b676e2020-05-27 14:29:09 -0700354 ALOGV("%s Max (can't resolve refresh rate)", mName.c_str());
Rachel Leece6e0042023-06-27 11:22:54 -0700355 votes.push_back({LayerHistory::LayerVoteType::Max, Fps()});
356 return votes;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800357}
358
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700359const char* LayerInfo::getTraceTag(LayerHistory::LayerVoteType type) const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700360 if (mTraceTags.count(type) == 0) {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700361 auto tag = "LFPS " + mName + " " + ftl::enum_string(type);
362 mTraceTags.emplace(type, std::move(tag));
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700363 }
364
365 return mTraceTags.at(type).c_str();
366}
367
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000368LayerInfo::FrameRate LayerInfo::getSetFrameRateVote() const {
369 return mLayerProps->setFrameRateVote;
370}
371
372bool LayerInfo::isVisible() const {
373 return mLayerProps->visible;
374}
375
376int32_t LayerInfo::getFrameRateSelectionPriority() const {
377 return mLayerProps->frameRateSelectionPriority;
378}
379
380FloatRect LayerInfo::getBounds() const {
381 return mLayerProps->bounds;
382}
383
384ui::Transform LayerInfo::getTransform() const {
385 return mLayerProps->transform;
386}
387
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100388LayerInfo::RefreshRateHistory::HeuristicTraceTagData
389LayerInfo::RefreshRateHistory::makeHeuristicTraceTagData() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700390 const std::string prefix = "LFPS ";
391 const std::string suffix = "Heuristic ";
392 return {.min = prefix + mName + suffix + "min",
393 .max = prefix + mName + suffix + "max",
394 .consistent = prefix + mName + suffix + "consistent",
395 .average = prefix + mName + suffix + "average"};
396}
397
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100398void LayerInfo::RefreshRateHistory::clear() {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700399 mRefreshRates.clear();
400}
401
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100402bool LayerInfo::RefreshRateHistory::add(Fps refreshRate, nsecs_t now) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700403 mRefreshRates.push_back({refreshRate, now});
404 while (mRefreshRates.size() >= HISTORY_SIZE ||
405 now - mRefreshRates.front().timestamp > HISTORY_DURATION.count()) {
406 mRefreshRates.pop_front();
407 }
408
409 if (CC_UNLIKELY(sTraceEnabled)) {
410 if (!mHeuristicTraceTagData.has_value()) {
411 mHeuristicTraceTagData = makeHeuristicTraceTagData();
412 }
413
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100414 ATRACE_INT(mHeuristicTraceTagData->average.c_str(), refreshRate.getIntValue());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700415 }
416
417 return isConsistent();
418}
419
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100420bool LayerInfo::RefreshRateHistory::isConsistent() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700421 if (mRefreshRates.empty()) return true;
422
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700423 const auto [min, max] =
424 std::minmax_element(mRefreshRates.begin(), mRefreshRates.end(),
425 [](const auto& lhs, const auto& rhs) {
426 return isStrictlyLess(lhs.refreshRate, rhs.refreshRate);
427 });
428
429 const bool consistent =
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100430 max->refreshRate.getValue() - min->refreshRate.getValue() < MARGIN_CONSISTENT_FPS;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700431
432 if (CC_UNLIKELY(sTraceEnabled)) {
433 if (!mHeuristicTraceTagData.has_value()) {
434 mHeuristicTraceTagData = makeHeuristicTraceTagData();
435 }
436
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100437 ATRACE_INT(mHeuristicTraceTagData->max.c_str(), max->refreshRate.getIntValue());
438 ATRACE_INT(mHeuristicTraceTagData->min.c_str(), min->refreshRate.getIntValue());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700439 ATRACE_INT(mHeuristicTraceTagData->consistent.c_str(), consistent);
440 }
441
442 return consistent;
443}
444
Rachel Leece6e0042023-06-27 11:22:54 -0700445LayerInfo::FrameRateCompatibility LayerInfo::FrameRate::convertCompatibility(int8_t compatibility) {
446 switch (compatibility) {
447 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT:
448 return FrameRateCompatibility::Default;
449 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE:
450 return FrameRateCompatibility::ExactOrMultiple;
451 case ANATIVEWINDOW_FRAME_RATE_EXACT:
452 return FrameRateCompatibility::Exact;
453 case ANATIVEWINDOW_FRAME_RATE_MIN:
454 return FrameRateCompatibility::Min;
455 case ANATIVEWINDOW_FRAME_RATE_NO_VOTE:
456 return FrameRateCompatibility::NoVote;
457 default:
458 LOG_ALWAYS_FATAL("Invalid frame rate compatibility value %d", compatibility);
459 return FrameRateCompatibility::Default;
460 }
461}
462
463Seamlessness LayerInfo::FrameRate::convertChangeFrameRateStrategy(int8_t strategy) {
464 switch (strategy) {
465 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS:
466 return Seamlessness::OnlySeamless;
467 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS:
468 return Seamlessness::SeamedAndSeamless;
469 default:
470 LOG_ALWAYS_FATAL("Invalid change frame sate strategy value %d", strategy);
471 return Seamlessness::Default;
472 }
473}
474
475FrameRateCategory LayerInfo::FrameRate::convertCategory(int8_t category) {
476 switch (category) {
477 case ANATIVEWINDOW_FRAME_RATE_CATEGORY_DEFAULT:
478 return FrameRateCategory::Default;
479 case ANATIVEWINDOW_FRAME_RATE_CATEGORY_NO_PREFERENCE:
480 return FrameRateCategory::NoPreference;
481 case ANATIVEWINDOW_FRAME_RATE_CATEGORY_LOW:
482 return FrameRateCategory::Low;
483 case ANATIVEWINDOW_FRAME_RATE_CATEGORY_NORMAL:
484 return FrameRateCategory::Normal;
485 case ANATIVEWINDOW_FRAME_RATE_CATEGORY_HIGH:
486 return FrameRateCategory::High;
487 default:
488 LOG_ALWAYS_FATAL("Invalid frame rate category value %d", category);
489 return FrameRateCategory::Default;
490 }
491}
492
Rachel Lee58cc90d2023-09-05 18:50:20 -0700493LayerInfo::FrameRateSelectionStrategy LayerInfo::convertFrameRateSelectionStrategy(
494 int8_t strategy) {
495 switch (strategy) {
496 case ANATIVEWINDOW_FRAME_RATE_SELECTION_STRATEGY_SELF:
497 return FrameRateSelectionStrategy::Self;
498 case ANATIVEWINDOW_FRAME_RATE_SELECTION_STRATEGY_OVERRIDE_CHILDREN:
499 return FrameRateSelectionStrategy::OverrideChildren;
500 default:
501 LOG_ALWAYS_FATAL("Invalid frame rate selection strategy value %d", strategy);
502 return FrameRateSelectionStrategy::Self;
503 }
504}
505
Rachel Leece6e0042023-06-27 11:22:54 -0700506bool LayerInfo::FrameRate::isNoVote() const {
Rachel Leed0694bc2023-09-12 14:57:58 -0700507 return vote.type == FrameRateCompatibility::NoVote;
Rachel Leece6e0042023-06-27 11:22:54 -0700508}
509
510bool LayerInfo::FrameRate::isValid() const {
511 return isNoVote() || vote.rate.isValid() || category != FrameRateCategory::Default;
512}
513
514std::ostream& operator<<(std::ostream& stream, const LayerInfo::FrameRate& rate) {
515 return stream << "{rate=" << rate.vote.rate << " type=" << ftl::enum_string(rate.vote.type)
516 << " seamlessness=" << ftl::enum_string(rate.vote.seamlessness) << '}';
517}
518
Ady Abraham8a82ba62020-01-17 12:43:17 -0800519} // namespace android::scheduler
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100520
521// TODO(b/129481165): remove the #pragma below and fix conversion issues
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700522#pragma clang diagnostic pop // ignored "-Wextra"