blob: 7d2444c94b6b5ca0a3e204ee2b85719a0b20d79e [file] [log] [blame]
Ady Abraham8a82ba62020-01-17 12:43:17 -08001/*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080019#include <chrono>
20#include <deque>
21#include <optional>
22#include <string>
23#include <unordered_map>
24
Ady Abrahambdda8f02021-04-01 16:06:11 -070025#include <ui/Transform.h>
Ady Abraham8a82ba62020-01-17 12:43:17 -080026#include <utils/Timers.h>
27
Rachel Leece6e0042023-06-27 11:22:54 -070028#include <scheduler/Fps.h>
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080029#include <scheduler/Seamlessness.h>
Ady Abraham8a82ba62020-01-17 12:43:17 -080030
31#include "LayerHistory.h"
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040032#include "RefreshRateSelector.h"
Ady Abraham8a82ba62020-01-17 12:43:17 -080033
34namespace android {
35
36class Layer;
37
38namespace scheduler {
39
40using namespace std::chrono_literals;
Vishnu Nairef68d6d2023-02-28 06:18:27 +000041struct LayerProps;
Ady Abraham8a82ba62020-01-17 12:43:17 -080042// Maximum period between presents for a layer to be considered active.
43constexpr std::chrono::nanoseconds MAX_ACTIVE_LAYER_PERIOD_NS = 1200ms;
44
45// Earliest present time for a layer to be considered active.
46constexpr nsecs_t getActiveLayerThreshold(nsecs_t now) {
47 return now - MAX_ACTIVE_LAYER_PERIOD_NS.count();
48}
49
50// Stores history of present times and refresh rates for a layer.
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010051class LayerInfo {
Ady Abraham5def7332020-05-29 16:13:47 -070052 using LayerUpdateType = LayerHistory::LayerUpdateType;
53
Ady Abraham8a82ba62020-01-17 12:43:17 -080054 // Layer is considered frequent if the earliest value in the window of most recent present times
55 // is within a threshold. If a layer is infrequent, its average refresh rate is disregarded in
56 // favor of a low refresh rate.
Ady Abraham86ac5c52023-01-11 15:24:03 -080057 static constexpr size_t kFrequentLayerWindowSize = 4;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070058 static constexpr Fps kMinFpsForFrequentLayer = 10_Hz;
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010059 static constexpr auto kMaxPeriodForFrequentLayerNs =
60 std::chrono::nanoseconds(kMinFpsForFrequentLayer.getPeriodNsecs()) + 1ms;
Ady Abraham8a82ba62020-01-17 12:43:17 -080061
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010062 friend class LayerHistoryTest;
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010063 friend class LayerInfoTest;
Ady Abraham8a82ba62020-01-17 12:43:17 -080064
65public:
Marin Shalamanov46084422020-10-13 12:33:42 +020066 // Holds information about the layer vote
67 struct LayerVote {
68 LayerHistory::LayerVoteType type = LayerHistory::LayerVoteType::Heuristic;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070069 Fps fps;
Marin Shalamanov53fc11d2020-11-20 14:00:13 +010070 Seamlessness seamlessness = Seamlessness::Default;
Rachel Leece6e0042023-06-27 11:22:54 -070071 // Category is in effect if fps is not specified.
72 FrameRateCategory category = FrameRateCategory::Default;
73
74 // Returns true if the layer explicitly should contribute to frame rate scoring.
75 bool isNoVote() const { return RefreshRateSelector::isNoVote(type, category); }
Marin Shalamanov46084422020-10-13 12:33:42 +020076 };
77
Rachel Leece6e0042023-06-27 11:22:54 -070078 using RefreshRateVotes = ftl::SmallVector<LayerInfo::LayerVote, 2>;
79
Ady Abrahambdda8f02021-04-01 16:06:11 -070080 // FrameRateCompatibility specifies how we should interpret the frame rate associated with
81 // the layer.
82 enum class FrameRateCompatibility {
83 Default, // Layer didn't specify any specific handling strategy
84
Andy Labrada096227e2022-06-15 16:58:11 +000085 Min, // Layer needs the minimum frame rate.
86
Ady Abrahambdda8f02021-04-01 16:06:11 -070087 Exact, // Layer needs the exact frame rate.
88
89 ExactOrMultiple, // Layer needs the exact frame rate (or a multiple of it) to present the
90 // content properly. Any other value will result in a pull down.
91
92 NoVote, // Layer doesn't have any requirements for the refresh rate and
93 // should not be considered when the display refresh rate is determined.
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070094
95 ftl_last = NoVote
Ady Abrahambdda8f02021-04-01 16:06:11 -070096 };
97
Rachel Leece6e0042023-06-27 11:22:54 -070098 // Encapsulates the frame rate specifications of the layer. This information will be used
Ady Abrahambdda8f02021-04-01 16:06:11 -070099 // when the display refresh rate is determined.
100 struct FrameRate {
101 using Seamlessness = scheduler::Seamlessness;
102
Rachel Leece6e0042023-06-27 11:22:54 -0700103 // Information related to a specific desired frame rate vote.
104 struct FrameRateVote {
105 Fps rate;
106 FrameRateCompatibility type = FrameRateCompatibility::Default;
107 Seamlessness seamlessness = Seamlessness::Default;
108
109 bool operator==(const FrameRateVote& other) const {
110 return isApproxEqual(rate, other.rate) && type == other.type &&
111 seamlessness == other.seamlessness;
112 }
113
114 FrameRateVote() = default;
115
116 FrameRateVote(Fps rate, FrameRateCompatibility type,
117 Seamlessness seamlessness = Seamlessness::OnlySeamless)
118 : rate(rate), type(type), seamlessness(getSeamlessness(rate, seamlessness)) {}
119 } vote;
120
121 FrameRateCategory category = FrameRateCategory::Default;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700122
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700123 FrameRate() = default;
124
Ady Abrahambdda8f02021-04-01 16:06:11 -0700125 FrameRate(Fps rate, FrameRateCompatibility type,
Rachel Leece6e0042023-06-27 11:22:54 -0700126 Seamlessness seamlessness = Seamlessness::OnlySeamless,
127 FrameRateCategory category = FrameRateCategory::Default)
128 : vote(FrameRateVote(rate, type, seamlessness)), category(category) {}
Ady Abrahambdda8f02021-04-01 16:06:11 -0700129
130 bool operator==(const FrameRate& other) const {
Rachel Leece6e0042023-06-27 11:22:54 -0700131 return vote == other.vote && category == other.category;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700132 }
133
134 bool operator!=(const FrameRate& other) const { return !(*this == other); }
135
136 // Convert an ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_* value to a
137 // Layer::FrameRateCompatibility. Logs fatal if the compatibility value is invalid.
138 static FrameRateCompatibility convertCompatibility(int8_t compatibility);
Rachel Leece6e0042023-06-27 11:22:54 -0700139
140 // Convert an ANATIVEWINDOW_CHANGE_FRAME_RATE_* value to a scheduler::Seamlessness.
141 // Logs fatal if the compatibility value is invalid.
Ady Abrahambdda8f02021-04-01 16:06:11 -0700142 static scheduler::Seamlessness convertChangeFrameRateStrategy(int8_t strategy);
143
Rachel Leece6e0042023-06-27 11:22:54 -0700144 // Convert an ANATIVEWINDOW_FRAME_RATE_CATEGORY_* value to a FrameRateCategory.
145 // Logs fatal if the compatibility value is invalid.
146 static FrameRateCategory convertCategory(int8_t category);
147
148 // True if the FrameRate has explicit frame rate specifications.
149 bool isValid() const;
150
151 // Returns true if the FrameRate explicitly instructs to not contribute to frame rate
152 // selection.
153 bool isNoVote() const;
154
Ady Abrahambdda8f02021-04-01 16:06:11 -0700155 private:
156 static Seamlessness getSeamlessness(Fps rate, Seamlessness seamlessness) {
157 if (!rate.isValid()) {
158 // Refresh rate of 0 is a special value which should reset the vote to
159 // its default value.
160 return Seamlessness::Default;
161 }
162 return seamlessness;
163 }
164 };
165
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700166 static void setTraceEnabled(bool enabled) { sTraceEnabled = enabled; }
167
Ady Abrahambdda8f02021-04-01 16:06:11 -0700168 LayerInfo(const std::string& name, uid_t ownerUid, LayerHistory::LayerVoteType defaultVote);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800169
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100170 LayerInfo(const LayerInfo&) = delete;
171 LayerInfo& operator=(const LayerInfo&) = delete;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800172
173 // Records the last requested present time. It also stores information about when
174 // the layer was last updated. If the present time is farther in the future than the
175 // updated time, the updated time is the present time.
Ady Abraham5def7332020-05-29 16:13:47 -0700176 void setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, LayerUpdateType updateType,
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000177 bool pendingModeChange, const LayerProps& props);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800178
Ady Abraham8a82ba62020-01-17 12:43:17 -0800179 // Sets an explicit layer vote. This usually comes directly from the application via
180 // ANativeWindow_setFrameRate API
Marin Shalamanov46084422020-10-13 12:33:42 +0200181 void setLayerVote(LayerVote vote) { mLayerVote = vote; }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800182
183 // Sets the default layer vote. This will be the layer vote after calling to resetLayerVote().
184 // This is used for layers that called to setLayerVote() and then removed the vote, so that the
185 // layer can go back to whatever vote it had before the app voted for it.
186 void setDefaultLayerVote(LayerHistory::LayerVoteType type) { mDefaultVote = type; }
187
188 // Resets the layer vote to its default.
Rachel Leece6e0042023-06-27 11:22:54 -0700189 void resetLayerVote() {
190 mLayerVote = {mDefaultVote, Fps(), Seamlessness::Default, FrameRateCategory::Default};
191 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800192
Ady Abrahambdda8f02021-04-01 16:06:11 -0700193 std::string getName() const { return mName; }
194
195 uid_t getOwnerUid() const { return mOwnerUid; }
196
Rachel Leece6e0042023-06-27 11:22:54 -0700197 RefreshRateVotes getRefreshRateVote(const RefreshRateSelector&, nsecs_t now);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800198
199 // Return the last updated time. If the present time is farther in the future than the
200 // updated time, the updated time is the present time.
201 nsecs_t getLastUpdatedTime() const { return mLastUpdatedTime; }
202
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000203 FrameRate getSetFrameRateVote() const;
204 bool isVisible() const;
205 int32_t getFrameRateSelectionPriority() const;
206 FloatRect getBounds() const;
207 ui::Transform getTransform() const;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700208
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700209 // Returns a C string for tracing a vote
210 const char* getTraceTag(LayerHistory::LayerVoteType type) const;
211
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400212 // Return the framerate of this layer.
213 Fps getFps(nsecs_t now) const;
214
Ady Abraham983e5682020-05-28 16:49:18 -0700215 void onLayerInactive(nsecs_t now) {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000216 // Mark mFrameTimeValidSince to now to ignore all previous frame times.
217 // We are not deleting the old frame to keep track of whether we should treat the first
218 // buffer as Max as we don't know anything about this layer or Min as this layer is
219 // posting infrequent updates.
Ady Abraham983e5682020-05-28 16:49:18 -0700220 const auto timePoint = std::chrono::nanoseconds(now);
221 mFrameTimeValidSince = std::chrono::time_point<std::chrono::steady_clock>(timePoint);
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700222 mLastRefreshRate = {};
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700223 mRefreshRateHistory.clear();
ramindani63db24e2023-04-03 10:56:05 -0700224 mIsFrequencyConclusive = true;
Ady Abrahama61edcb2020-01-30 18:32:03 -0800225 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800226
Ady Abraham983e5682020-05-28 16:49:18 -0700227 void clearHistory(nsecs_t now) {
228 onLayerInactive(now);
229 mFrameTimes.clear();
230 }
231
Ady Abraham8a82ba62020-01-17 12:43:17 -0800232private:
Ady Abrahama61edcb2020-01-30 18:32:03 -0800233 // Used to store the layer timestamps
234 struct FrameTimeData {
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100235 nsecs_t presentTime; // desiredPresentTime, if provided
Ady Abrahama61edcb2020-01-30 18:32:03 -0800236 nsecs_t queueTime; // buffer queue time
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100237 bool pendingModeChange;
Ady Abrahama61edcb2020-01-30 18:32:03 -0800238 };
239
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700240 // Holds information about the calculated and reported refresh rate
241 struct RefreshRateHeuristicData {
242 // Rate calculated on the layer
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700243 Fps calculated;
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100244 // Last reported rate for LayerInfo::getRefreshRate()
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700245 Fps reported;
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100246 // Whether the last reported rate for LayerInfo::getRefreshRate()
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700247 // was due to animation or infrequent updates
Ady Abraham86ac5c52023-01-11 15:24:03 -0800248 bool animating = false;
249 // Whether the last reported rate for LayerInfo::getRefreshRate()
250 // was due to infrequent updates
251 bool infrequent = false;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700252 };
253
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700254 // Class to store past calculated refresh rate and determine whether
255 // the refresh rate calculated is consistent with past values
256 class RefreshRateHistory {
257 public:
258 static constexpr auto HISTORY_SIZE = 90;
259 static constexpr std::chrono::nanoseconds HISTORY_DURATION = 2s;
260
261 RefreshRateHistory(const std::string& name) : mName(name) {}
262
263 // Clears History
264 void clear();
265
266 // Adds a new refresh rate and returns true if it is consistent
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100267 bool add(Fps refreshRate, nsecs_t now);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700268
269 private:
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100270 friend class LayerHistoryTest;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700271
272 // Holds the refresh rate when it was calculated
273 struct RefreshRateData {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700274 Fps refreshRate;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700275 nsecs_t timestamp = 0;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700276 };
277
278 // Holds tracing strings
279 struct HeuristicTraceTagData {
280 std::string min;
281 std::string max;
282 std::string consistent;
283 std::string average;
284 };
285
286 bool isConsistent() const;
287 HeuristicTraceTagData makeHeuristicTraceTagData() const;
288
289 const std::string mName;
290 mutable std::optional<HeuristicTraceTagData> mHeuristicTraceTagData;
291 std::deque<RefreshRateData> mRefreshRates;
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100292 static constexpr float MARGIN_CONSISTENT_FPS = 1.0;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700293 };
294
ramindani63db24e2023-04-03 10:56:05 -0700295 // Represents whether we were able to determine either layer is frequent or infrequent
296 bool mIsFrequencyConclusive = true;
297 struct Frequent {
298 bool isFrequent;
299 bool clearHistory;
300 // Represents whether we were able to determine isFrequent conclusively
301 bool isConclusive;
302 };
303 Frequent isFrequent(nsecs_t now) const;
Ady Abraham5def7332020-05-29 16:13:47 -0700304 bool isAnimating(nsecs_t now) const;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800305 bool hasEnoughDataForHeuristic() const;
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400306 std::optional<Fps> calculateRefreshRateIfPossible(const RefreshRateSelector&, nsecs_t now);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700307 std::optional<nsecs_t> calculateAverageFrameTime() const;
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000308 bool isFrameTimeValid(const FrameTimeData&) const;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800309
Ady Abrahama6b676e2020-05-27 14:29:09 -0700310 const std::string mName;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700311 const uid_t mOwnerUid;
Ady Abrahama6b676e2020-05-27 14:29:09 -0700312
Marin Shalamanov4ad8b302020-12-11 15:50:08 +0100313 // Used for sanitizing the heuristic data. If two frames are less than
314 // this period apart from each other they'll be considered as duplicates.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700315 static constexpr nsecs_t kMinPeriodBetweenFrames = (240_Hz).getPeriodNsecs();
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100316 // Used for sanitizing the heuristic data. If two frames are more than
317 // this period apart from each other, the interval between them won't be
318 // taken into account when calculating average frame rate.
319 static constexpr nsecs_t kMaxPeriodBetweenFrames = kMinFpsForFrequentLayer.getPeriodNsecs();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800320 LayerHistory::LayerVoteType mDefaultVote;
321
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700322 LayerVote mLayerVote;
323
Ady Abraham8a82ba62020-01-17 12:43:17 -0800324 nsecs_t mLastUpdatedTime = 0;
325
Ady Abraham5def7332020-05-29 16:13:47 -0700326 nsecs_t mLastAnimationTime = 0;
327
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700328 RefreshRateHeuristicData mLastRefreshRate;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800329
Ady Abraham8a82ba62020-01-17 12:43:17 -0800330 std::deque<FrameTimeData> mFrameTimes;
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000331 std::chrono::time_point<std::chrono::steady_clock> mFrameTimeValidSince =
332 std::chrono::steady_clock::now();
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700333 static constexpr size_t HISTORY_SIZE = RefreshRateHistory::HISTORY_SIZE;
334 static constexpr std::chrono::nanoseconds HISTORY_DURATION = 1s;
335
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000336 std::unique_ptr<LayerProps> mLayerProps;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700337
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700338 RefreshRateHistory mRefreshRateHistory;
339
340 mutable std::unordered_map<LayerHistory::LayerVoteType, std::string> mTraceTags;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700341
342 // Shared for all LayerInfo instances
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700343 static bool sTraceEnabled;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800344};
345
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000346struct LayerProps {
347 bool visible = false;
348 FloatRect bounds;
349 ui::Transform transform;
350 LayerInfo::FrameRate setFrameRateVote;
351 int32_t frameRateSelectionPriority = -1;
352};
353
Ady Abraham8a82ba62020-01-17 12:43:17 -0800354} // namespace scheduler
355} // namespace android