blob: 3b4d8239d2a4394e5573c6c5d68da4d7241aede8 [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;
Arthur Hungc70bee22023-06-02 01:35:52 +000061 static constexpr size_t kNumSmallDirtyThreshold = 2;
Ady Abraham8a82ba62020-01-17 12:43:17 -080062
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010063 friend class LayerHistoryTest;
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010064 friend class LayerInfoTest;
Ady Abraham8a82ba62020-01-17 12:43:17 -080065
66public:
Marin Shalamanov46084422020-10-13 12:33:42 +020067 // Holds information about the layer vote
68 struct LayerVote {
69 LayerHistory::LayerVoteType type = LayerHistory::LayerVoteType::Heuristic;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070070 Fps fps;
Marin Shalamanov53fc11d2020-11-20 14:00:13 +010071 Seamlessness seamlessness = Seamlessness::Default;
Rachel Leece6e0042023-06-27 11:22:54 -070072 // Category is in effect if fps is not specified.
73 FrameRateCategory category = FrameRateCategory::Default;
74
75 // Returns true if the layer explicitly should contribute to frame rate scoring.
Rachel Leed0694bc2023-09-12 14:57:58 -070076 bool isNoVote() const { return RefreshRateSelector::isNoVote(type); }
Marin Shalamanov46084422020-10-13 12:33:42 +020077 };
78
Rachel Leece6e0042023-06-27 11:22:54 -070079 using RefreshRateVotes = ftl::SmallVector<LayerInfo::LayerVote, 2>;
80
Ady Abrahambdda8f02021-04-01 16:06:11 -070081 // FrameRateCompatibility specifies how we should interpret the frame rate associated with
82 // the layer.
83 enum class FrameRateCompatibility {
84 Default, // Layer didn't specify any specific handling strategy
85
Andy Labrada096227e2022-06-15 16:58:11 +000086 Min, // Layer needs the minimum frame rate.
87
Ady Abrahambdda8f02021-04-01 16:06:11 -070088 Exact, // Layer needs the exact frame rate.
89
90 ExactOrMultiple, // Layer needs the exact frame rate (or a multiple of it) to present the
91 // content properly. Any other value will result in a pull down.
92
93 NoVote, // Layer doesn't have any requirements for the refresh rate and
94 // should not be considered when the display refresh rate is determined.
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070095
96 ftl_last = NoVote
Ady Abrahambdda8f02021-04-01 16:06:11 -070097 };
98
Rachel Lee58cc90d2023-09-05 18:50:20 -070099 enum class FrameRateSelectionStrategy {
100 Self,
101 OverrideChildren,
102
103 ftl_last = OverrideChildren
104 };
105
Rachel Leece6e0042023-06-27 11:22:54 -0700106 // Encapsulates the frame rate specifications of the layer. This information will be used
Ady Abrahambdda8f02021-04-01 16:06:11 -0700107 // when the display refresh rate is determined.
108 struct FrameRate {
109 using Seamlessness = scheduler::Seamlessness;
110
Rachel Leece6e0042023-06-27 11:22:54 -0700111 // Information related to a specific desired frame rate vote.
112 struct FrameRateVote {
113 Fps rate;
114 FrameRateCompatibility type = FrameRateCompatibility::Default;
115 Seamlessness seamlessness = Seamlessness::Default;
116
117 bool operator==(const FrameRateVote& other) const {
118 return isApproxEqual(rate, other.rate) && type == other.type &&
119 seamlessness == other.seamlessness;
120 }
121
122 FrameRateVote() = default;
123
124 FrameRateVote(Fps rate, FrameRateCompatibility type,
125 Seamlessness seamlessness = Seamlessness::OnlySeamless)
126 : rate(rate), type(type), seamlessness(getSeamlessness(rate, seamlessness)) {}
127 } vote;
128
129 FrameRateCategory category = FrameRateCategory::Default;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700130
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700131 FrameRate() = default;
132
Ady Abrahambdda8f02021-04-01 16:06:11 -0700133 FrameRate(Fps rate, FrameRateCompatibility type,
Rachel Leece6e0042023-06-27 11:22:54 -0700134 Seamlessness seamlessness = Seamlessness::OnlySeamless,
135 FrameRateCategory category = FrameRateCategory::Default)
136 : vote(FrameRateVote(rate, type, seamlessness)), category(category) {}
Ady Abrahambdda8f02021-04-01 16:06:11 -0700137
138 bool operator==(const FrameRate& other) const {
Rachel Leece6e0042023-06-27 11:22:54 -0700139 return vote == other.vote && category == other.category;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700140 }
141
142 bool operator!=(const FrameRate& other) const { return !(*this == other); }
143
144 // Convert an ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_* value to a
145 // Layer::FrameRateCompatibility. Logs fatal if the compatibility value is invalid.
146 static FrameRateCompatibility convertCompatibility(int8_t compatibility);
Rachel Leece6e0042023-06-27 11:22:54 -0700147
148 // Convert an ANATIVEWINDOW_CHANGE_FRAME_RATE_* value to a scheduler::Seamlessness.
Rachel Lee58cc90d2023-09-05 18:50:20 -0700149 // Logs fatal if the strategy value is invalid.
Ady Abrahambdda8f02021-04-01 16:06:11 -0700150 static scheduler::Seamlessness convertChangeFrameRateStrategy(int8_t strategy);
151
Rachel Leece6e0042023-06-27 11:22:54 -0700152 // Convert an ANATIVEWINDOW_FRAME_RATE_CATEGORY_* value to a FrameRateCategory.
Rachel Lee58cc90d2023-09-05 18:50:20 -0700153 // Logs fatal if the category value is invalid.
Rachel Leece6e0042023-06-27 11:22:54 -0700154 static FrameRateCategory convertCategory(int8_t category);
155
156 // True if the FrameRate has explicit frame rate specifications.
157 bool isValid() const;
158
159 // Returns true if the FrameRate explicitly instructs to not contribute to frame rate
160 // selection.
161 bool isNoVote() const;
162
Ady Abrahambdda8f02021-04-01 16:06:11 -0700163 private:
164 static Seamlessness getSeamlessness(Fps rate, Seamlessness seamlessness) {
165 if (!rate.isValid()) {
166 // Refresh rate of 0 is a special value which should reset the vote to
167 // its default value.
168 return Seamlessness::Default;
169 }
170 return seamlessness;
171 }
172 };
173
Rachel Lee58cc90d2023-09-05 18:50:20 -0700174 // Convert an ANATIVEWINDOW_FRAME_RATE_SELECTION_STRATEGY_* value to FrameRateSelectionStrategy.
175 // Logs fatal if the strategy value is invalid.
176 static FrameRateSelectionStrategy convertFrameRateSelectionStrategy(int8_t strategy);
177
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700178 static void setTraceEnabled(bool enabled) { sTraceEnabled = enabled; }
179
Ady Abrahambdda8f02021-04-01 16:06:11 -0700180 LayerInfo(const std::string& name, uid_t ownerUid, LayerHistory::LayerVoteType defaultVote);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800181
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100182 LayerInfo(const LayerInfo&) = delete;
183 LayerInfo& operator=(const LayerInfo&) = delete;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800184
185 // Records the last requested present time. It also stores information about when
186 // the layer was last updated. If the present time is farther in the future than the
187 // updated time, the updated time is the present time.
Ady Abraham5def7332020-05-29 16:13:47 -0700188 void setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, LayerUpdateType updateType,
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000189 bool pendingModeChange, const LayerProps& props);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800190
Ady Abraham8a82ba62020-01-17 12:43:17 -0800191 // Sets an explicit layer vote. This usually comes directly from the application via
192 // ANativeWindow_setFrameRate API
Marin Shalamanov46084422020-10-13 12:33:42 +0200193 void setLayerVote(LayerVote vote) { mLayerVote = vote; }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800194
195 // Sets the default layer vote. This will be the layer vote after calling to resetLayerVote().
196 // This is used for layers that called to setLayerVote() and then removed the vote, so that the
197 // layer can go back to whatever vote it had before the app voted for it.
198 void setDefaultLayerVote(LayerHistory::LayerVoteType type) { mDefaultVote = type; }
199
200 // Resets the layer vote to its default.
Rachel Leece6e0042023-06-27 11:22:54 -0700201 void resetLayerVote() {
202 mLayerVote = {mDefaultVote, Fps(), Seamlessness::Default, FrameRateCategory::Default};
203 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800204
Ady Abrahambdda8f02021-04-01 16:06:11 -0700205 std::string getName() const { return mName; }
206
207 uid_t getOwnerUid() const { return mOwnerUid; }
208
Rachel Leece6e0042023-06-27 11:22:54 -0700209 RefreshRateVotes getRefreshRateVote(const RefreshRateSelector&, nsecs_t now);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800210
211 // Return the last updated time. If the present time is farther in the future than the
212 // updated time, the updated time is the present time.
213 nsecs_t getLastUpdatedTime() const { return mLastUpdatedTime; }
214
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000215 FrameRate getSetFrameRateVote() const;
216 bool isVisible() const;
217 int32_t getFrameRateSelectionPriority() const;
218 FloatRect getBounds() const;
219 ui::Transform getTransform() const;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700220
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700221 // Returns a C string for tracing a vote
222 const char* getTraceTag(LayerHistory::LayerVoteType type) const;
223
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400224 // Return the framerate of this layer.
225 Fps getFps(nsecs_t now) const;
226
Ady Abraham983e5682020-05-28 16:49:18 -0700227 void onLayerInactive(nsecs_t now) {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000228 // Mark mFrameTimeValidSince to now to ignore all previous frame times.
229 // We are not deleting the old frame to keep track of whether we should treat the first
230 // buffer as Max as we don't know anything about this layer or Min as this layer is
231 // posting infrequent updates.
Ady Abraham983e5682020-05-28 16:49:18 -0700232 const auto timePoint = std::chrono::nanoseconds(now);
233 mFrameTimeValidSince = std::chrono::time_point<std::chrono::steady_clock>(timePoint);
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700234 mLastRefreshRate = {};
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700235 mRefreshRateHistory.clear();
ramindani63db24e2023-04-03 10:56:05 -0700236 mIsFrequencyConclusive = true;
Ady Abrahama61edcb2020-01-30 18:32:03 -0800237 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800238
Ady Abraham983e5682020-05-28 16:49:18 -0700239 void clearHistory(nsecs_t now) {
240 onLayerInactive(now);
241 mFrameTimes.clear();
242 }
243
Ady Abraham8a82ba62020-01-17 12:43:17 -0800244private:
Ady Abrahama61edcb2020-01-30 18:32:03 -0800245 // Used to store the layer timestamps
246 struct FrameTimeData {
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100247 nsecs_t presentTime; // desiredPresentTime, if provided
Ady Abrahama61edcb2020-01-30 18:32:03 -0800248 nsecs_t queueTime; // buffer queue time
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100249 bool pendingModeChange;
Arthur Hungc70bee22023-06-02 01:35:52 +0000250 bool isSmallDirty;
Ady Abrahama61edcb2020-01-30 18:32:03 -0800251 };
252
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700253 // Holds information about the calculated and reported refresh rate
254 struct RefreshRateHeuristicData {
255 // Rate calculated on the layer
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700256 Fps calculated;
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100257 // Last reported rate for LayerInfo::getRefreshRate()
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700258 Fps reported;
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100259 // Whether the last reported rate for LayerInfo::getRefreshRate()
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700260 // was due to animation or infrequent updates
Ady Abraham86ac5c52023-01-11 15:24:03 -0800261 bool animating = false;
262 // Whether the last reported rate for LayerInfo::getRefreshRate()
263 // was due to infrequent updates
264 bool infrequent = false;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700265 };
266
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700267 // Class to store past calculated refresh rate and determine whether
268 // the refresh rate calculated is consistent with past values
269 class RefreshRateHistory {
270 public:
271 static constexpr auto HISTORY_SIZE = 90;
272 static constexpr std::chrono::nanoseconds HISTORY_DURATION = 2s;
273
274 RefreshRateHistory(const std::string& name) : mName(name) {}
275
276 // Clears History
277 void clear();
278
279 // Adds a new refresh rate and returns true if it is consistent
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100280 bool add(Fps refreshRate, nsecs_t now);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700281
282 private:
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100283 friend class LayerHistoryTest;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700284
285 // Holds the refresh rate when it was calculated
286 struct RefreshRateData {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700287 Fps refreshRate;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700288 nsecs_t timestamp = 0;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700289 };
290
291 // Holds tracing strings
292 struct HeuristicTraceTagData {
293 std::string min;
294 std::string max;
295 std::string consistent;
296 std::string average;
297 };
298
299 bool isConsistent() const;
300 HeuristicTraceTagData makeHeuristicTraceTagData() const;
301
302 const std::string mName;
303 mutable std::optional<HeuristicTraceTagData> mHeuristicTraceTagData;
304 std::deque<RefreshRateData> mRefreshRates;
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100305 static constexpr float MARGIN_CONSISTENT_FPS = 1.0;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700306 };
307
ramindani63db24e2023-04-03 10:56:05 -0700308 // Represents whether we were able to determine either layer is frequent or infrequent
309 bool mIsFrequencyConclusive = true;
310 struct Frequent {
311 bool isFrequent;
312 bool clearHistory;
313 // Represents whether we were able to determine isFrequent conclusively
314 bool isConclusive;
Arthur Hungc70bee22023-06-02 01:35:52 +0000315 // Represents whether the latest frames are small dirty.
316 bool isSmallDirty = false;
ramindani63db24e2023-04-03 10:56:05 -0700317 };
318 Frequent isFrequent(nsecs_t now) const;
Ady Abraham5def7332020-05-29 16:13:47 -0700319 bool isAnimating(nsecs_t now) const;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800320 bool hasEnoughDataForHeuristic() const;
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400321 std::optional<Fps> calculateRefreshRateIfPossible(const RefreshRateSelector&, nsecs_t now);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700322 std::optional<nsecs_t> calculateAverageFrameTime() const;
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000323 bool isFrameTimeValid(const FrameTimeData&) const;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800324
Ady Abrahama6b676e2020-05-27 14:29:09 -0700325 const std::string mName;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700326 const uid_t mOwnerUid;
Ady Abrahama6b676e2020-05-27 14:29:09 -0700327
Marin Shalamanov4ad8b302020-12-11 15:50:08 +0100328 // Used for sanitizing the heuristic data. If two frames are less than
329 // this period apart from each other they'll be considered as duplicates.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700330 static constexpr nsecs_t kMinPeriodBetweenFrames = (240_Hz).getPeriodNsecs();
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100331 // Used for sanitizing the heuristic data. If two frames are more than
332 // this period apart from each other, the interval between them won't be
333 // taken into account when calculating average frame rate.
334 static constexpr nsecs_t kMaxPeriodBetweenFrames = kMinFpsForFrequentLayer.getPeriodNsecs();
Arthur Hungc70bee22023-06-02 01:35:52 +0000335 // Used for sanitizing the heuristic data. If frames are small dirty updating and are less
336 // than this period apart from each other, the interval between them won't be
337 // taken into account when calculating average frame rate.
338 static constexpr nsecs_t kMinPeriodBetweenSmallDirtyFrames = (60_Hz).getPeriodNsecs();
339
Ady Abraham8a82ba62020-01-17 12:43:17 -0800340 LayerHistory::LayerVoteType mDefaultVote;
341
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700342 LayerVote mLayerVote;
343
Ady Abraham8a82ba62020-01-17 12:43:17 -0800344 nsecs_t mLastUpdatedTime = 0;
345
Ady Abraham5def7332020-05-29 16:13:47 -0700346 nsecs_t mLastAnimationTime = 0;
347
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700348 RefreshRateHeuristicData mLastRefreshRate;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800349
Ady Abraham8a82ba62020-01-17 12:43:17 -0800350 std::deque<FrameTimeData> mFrameTimes;
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000351 std::chrono::time_point<std::chrono::steady_clock> mFrameTimeValidSince =
352 std::chrono::steady_clock::now();
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700353 static constexpr size_t HISTORY_SIZE = RefreshRateHistory::HISTORY_SIZE;
Arthur Hungc70bee22023-06-02 01:35:52 +0000354 static constexpr std::chrono::nanoseconds HISTORY_DURATION = LayerHistory::kMaxPeriodForHistory;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700355
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000356 std::unique_ptr<LayerProps> mLayerProps;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700357
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700358 RefreshRateHistory mRefreshRateHistory;
359
360 mutable std::unordered_map<LayerHistory::LayerVoteType, std::string> mTraceTags;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700361
362 // Shared for all LayerInfo instances
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700363 static bool sTraceEnabled;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800364};
365
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000366struct LayerProps {
367 bool visible = false;
368 FloatRect bounds;
369 ui::Transform transform;
370 LayerInfo::FrameRate setFrameRateVote;
371 int32_t frameRateSelectionPriority = -1;
Arthur Hungc70bee22023-06-02 01:35:52 +0000372 bool isSmallDirty = false;
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000373};
374
Ady Abraham8a82ba62020-01-17 12:43:17 -0800375} // namespace scheduler
376} // namespace android