blob: 6286b285cfa2859685eeb954af499000f20caf81 [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
Vishnu Nair3fbe3262023-09-29 17:07:00 -070031#include "FrameRateCompatibility.h"
Ady Abraham8a82ba62020-01-17 12:43:17 -080032#include "LayerHistory.h"
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040033#include "RefreshRateSelector.h"
Ady Abraham8a82ba62020-01-17 12:43:17 -080034
35namespace android {
36
37class Layer;
38
39namespace scheduler {
40
41using namespace std::chrono_literals;
Vishnu Nairef68d6d2023-02-28 06:18:27 +000042struct LayerProps;
Ady Abraham8a82ba62020-01-17 12:43:17 -080043// Maximum period between presents for a layer to be considered active.
44constexpr std::chrono::nanoseconds MAX_ACTIVE_LAYER_PERIOD_NS = 1200ms;
45
46// Earliest present time for a layer to be considered active.
47constexpr nsecs_t getActiveLayerThreshold(nsecs_t now) {
48 return now - MAX_ACTIVE_LAYER_PERIOD_NS.count();
49}
50
51// Stores history of present times and refresh rates for a layer.
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010052class LayerInfo {
Ady Abraham5def7332020-05-29 16:13:47 -070053 using LayerUpdateType = LayerHistory::LayerUpdateType;
54
Ady Abraham8a82ba62020-01-17 12:43:17 -080055 // Layer is considered frequent if the earliest value in the window of most recent present times
56 // is within a threshold. If a layer is infrequent, its average refresh rate is disregarded in
57 // favor of a low refresh rate.
Ady Abraham86ac5c52023-01-11 15:24:03 -080058 static constexpr size_t kFrequentLayerWindowSize = 4;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070059 static constexpr Fps kMinFpsForFrequentLayer = 10_Hz;
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010060 static constexpr auto kMaxPeriodForFrequentLayerNs =
61 std::chrono::nanoseconds(kMinFpsForFrequentLayer.getPeriodNsecs()) + 1ms;
Arthur Hungc70bee22023-06-02 01:35:52 +000062 static constexpr size_t kNumSmallDirtyThreshold = 2;
Ady Abraham8a82ba62020-01-17 12:43:17 -080063
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010064 friend class LayerHistoryTest;
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010065 friend class LayerInfoTest;
Ady Abraham8a82ba62020-01-17 12:43:17 -080066
67public:
Marin Shalamanov46084422020-10-13 12:33:42 +020068 // Holds information about the layer vote
69 struct LayerVote {
70 LayerHistory::LayerVoteType type = LayerHistory::LayerVoteType::Heuristic;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070071 Fps fps;
Marin Shalamanov53fc11d2020-11-20 14:00:13 +010072 Seamlessness seamlessness = Seamlessness::Default;
Rachel Leece6e0042023-06-27 11:22:54 -070073 // Category is in effect if fps is not specified.
74 FrameRateCategory category = FrameRateCategory::Default;
75
76 // Returns true if the layer explicitly should contribute to frame rate scoring.
Rachel Leed0694bc2023-09-12 14:57:58 -070077 bool isNoVote() const { return RefreshRateSelector::isNoVote(type); }
Marin Shalamanov46084422020-10-13 12:33:42 +020078 };
79
Rachel Leece6e0042023-06-27 11:22:54 -070080 using RefreshRateVotes = ftl::SmallVector<LayerInfo::LayerVote, 2>;
81
Rachel Lee58cc90d2023-09-05 18:50:20 -070082 enum class FrameRateSelectionStrategy {
83 Self,
84 OverrideChildren,
85
86 ftl_last = OverrideChildren
87 };
88
Rachel Leece6e0042023-06-27 11:22:54 -070089 // Encapsulates the frame rate specifications of the layer. This information will be used
Ady Abrahambdda8f02021-04-01 16:06:11 -070090 // when the display refresh rate is determined.
91 struct FrameRate {
92 using Seamlessness = scheduler::Seamlessness;
93
Rachel Leece6e0042023-06-27 11:22:54 -070094 // Information related to a specific desired frame rate vote.
95 struct FrameRateVote {
96 Fps rate;
97 FrameRateCompatibility type = FrameRateCompatibility::Default;
98 Seamlessness seamlessness = Seamlessness::Default;
99
100 bool operator==(const FrameRateVote& other) const {
101 return isApproxEqual(rate, other.rate) && type == other.type &&
102 seamlessness == other.seamlessness;
103 }
104
105 FrameRateVote() = default;
106
107 FrameRateVote(Fps rate, FrameRateCompatibility type,
108 Seamlessness seamlessness = Seamlessness::OnlySeamless)
109 : rate(rate), type(type), seamlessness(getSeamlessness(rate, seamlessness)) {}
110 } vote;
111
112 FrameRateCategory category = FrameRateCategory::Default;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700113
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700114 FrameRate() = default;
115
Ady Abrahambdda8f02021-04-01 16:06:11 -0700116 FrameRate(Fps rate, FrameRateCompatibility type,
Rachel Leece6e0042023-06-27 11:22:54 -0700117 Seamlessness seamlessness = Seamlessness::OnlySeamless,
118 FrameRateCategory category = FrameRateCategory::Default)
119 : vote(FrameRateVote(rate, type, seamlessness)), category(category) {}
Ady Abrahambdda8f02021-04-01 16:06:11 -0700120
121 bool operator==(const FrameRate& other) const {
Rachel Leece6e0042023-06-27 11:22:54 -0700122 return vote == other.vote && category == other.category;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700123 }
124
125 bool operator!=(const FrameRate& other) const { return !(*this == other); }
126
127 // Convert an ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_* value to a
128 // Layer::FrameRateCompatibility. Logs fatal if the compatibility value is invalid.
129 static FrameRateCompatibility convertCompatibility(int8_t compatibility);
Rachel Leece6e0042023-06-27 11:22:54 -0700130
131 // Convert an ANATIVEWINDOW_CHANGE_FRAME_RATE_* value to a scheduler::Seamlessness.
Rachel Lee58cc90d2023-09-05 18:50:20 -0700132 // Logs fatal if the strategy value is invalid.
Ady Abrahambdda8f02021-04-01 16:06:11 -0700133 static scheduler::Seamlessness convertChangeFrameRateStrategy(int8_t strategy);
134
Rachel Leece6e0042023-06-27 11:22:54 -0700135 // Convert an ANATIVEWINDOW_FRAME_RATE_CATEGORY_* value to a FrameRateCategory.
Rachel Lee58cc90d2023-09-05 18:50:20 -0700136 // Logs fatal if the category value is invalid.
Rachel Leece6e0042023-06-27 11:22:54 -0700137 static FrameRateCategory convertCategory(int8_t category);
138
139 // True if the FrameRate has explicit frame rate specifications.
140 bool isValid() const;
141
142 // Returns true if the FrameRate explicitly instructs to not contribute to frame rate
143 // selection.
144 bool isNoVote() const;
145
Ady Abrahambdda8f02021-04-01 16:06:11 -0700146 private:
147 static Seamlessness getSeamlessness(Fps rate, Seamlessness seamlessness) {
148 if (!rate.isValid()) {
149 // Refresh rate of 0 is a special value which should reset the vote to
150 // its default value.
151 return Seamlessness::Default;
152 }
153 return seamlessness;
154 }
155 };
156
Rachel Lee58cc90d2023-09-05 18:50:20 -0700157 // Convert an ANATIVEWINDOW_FRAME_RATE_SELECTION_STRATEGY_* value to FrameRateSelectionStrategy.
158 // Logs fatal if the strategy value is invalid.
159 static FrameRateSelectionStrategy convertFrameRateSelectionStrategy(int8_t strategy);
160
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700161 static void setTraceEnabled(bool enabled) { sTraceEnabled = enabled; }
162
Ady Abrahambdda8f02021-04-01 16:06:11 -0700163 LayerInfo(const std::string& name, uid_t ownerUid, LayerHistory::LayerVoteType defaultVote);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800164
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100165 LayerInfo(const LayerInfo&) = delete;
166 LayerInfo& operator=(const LayerInfo&) = delete;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800167
168 // Records the last requested present time. It also stores information about when
169 // the layer was last updated. If the present time is farther in the future than the
170 // updated time, the updated time is the present time.
Ady Abraham5def7332020-05-29 16:13:47 -0700171 void setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, LayerUpdateType updateType,
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000172 bool pendingModeChange, const LayerProps& props);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800173
Ady Abraham8a82ba62020-01-17 12:43:17 -0800174 // Sets an explicit layer vote. This usually comes directly from the application via
175 // ANativeWindow_setFrameRate API
Marin Shalamanov46084422020-10-13 12:33:42 +0200176 void setLayerVote(LayerVote vote) { mLayerVote = vote; }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800177
178 // Sets the default layer vote. This will be the layer vote after calling to resetLayerVote().
179 // This is used for layers that called to setLayerVote() and then removed the vote, so that the
180 // layer can go back to whatever vote it had before the app voted for it.
181 void setDefaultLayerVote(LayerHistory::LayerVoteType type) { mDefaultVote = type; }
182
183 // Resets the layer vote to its default.
Rachel Leece6e0042023-06-27 11:22:54 -0700184 void resetLayerVote() {
185 mLayerVote = {mDefaultVote, Fps(), Seamlessness::Default, FrameRateCategory::Default};
186 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800187
Ady Abrahambdda8f02021-04-01 16:06:11 -0700188 std::string getName() const { return mName; }
189
190 uid_t getOwnerUid() const { return mOwnerUid; }
191
Rachel Leece6e0042023-06-27 11:22:54 -0700192 RefreshRateVotes getRefreshRateVote(const RefreshRateSelector&, nsecs_t now);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800193
194 // Return the last updated time. If the present time is farther in the future than the
195 // updated time, the updated time is the present time.
196 nsecs_t getLastUpdatedTime() const { return mLastUpdatedTime; }
197
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000198 FrameRate getSetFrameRateVote() const;
199 bool isVisible() const;
200 int32_t getFrameRateSelectionPriority() const;
201 FloatRect getBounds() const;
202 ui::Transform getTransform() const;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700203
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700204 // Returns a C string for tracing a vote
205 const char* getTraceTag(LayerHistory::LayerVoteType type) const;
206
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400207 // Return the framerate of this layer.
208 Fps getFps(nsecs_t now) const;
209
Ady Abraham983e5682020-05-28 16:49:18 -0700210 void onLayerInactive(nsecs_t now) {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000211 // Mark mFrameTimeValidSince to now to ignore all previous frame times.
212 // We are not deleting the old frame to keep track of whether we should treat the first
213 // buffer as Max as we don't know anything about this layer or Min as this layer is
214 // posting infrequent updates.
Ady Abraham983e5682020-05-28 16:49:18 -0700215 const auto timePoint = std::chrono::nanoseconds(now);
216 mFrameTimeValidSince = std::chrono::time_point<std::chrono::steady_clock>(timePoint);
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700217 mLastRefreshRate = {};
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700218 mRefreshRateHistory.clear();
ramindani63db24e2023-04-03 10:56:05 -0700219 mIsFrequencyConclusive = true;
Ady Abrahama61edcb2020-01-30 18:32:03 -0800220 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800221
Ady Abraham983e5682020-05-28 16:49:18 -0700222 void clearHistory(nsecs_t now) {
223 onLayerInactive(now);
224 mFrameTimes.clear();
225 }
226
Ady Abraham8a82ba62020-01-17 12:43:17 -0800227private:
Ady Abrahama61edcb2020-01-30 18:32:03 -0800228 // Used to store the layer timestamps
229 struct FrameTimeData {
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100230 nsecs_t presentTime; // desiredPresentTime, if provided
Ady Abrahama61edcb2020-01-30 18:32:03 -0800231 nsecs_t queueTime; // buffer queue time
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100232 bool pendingModeChange;
Arthur Hungc70bee22023-06-02 01:35:52 +0000233 bool isSmallDirty;
Ady Abrahama61edcb2020-01-30 18:32:03 -0800234 };
235
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700236 // Holds information about the calculated and reported refresh rate
237 struct RefreshRateHeuristicData {
238 // Rate calculated on the layer
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700239 Fps calculated;
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100240 // Last reported rate for LayerInfo::getRefreshRate()
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700241 Fps reported;
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100242 // Whether the last reported rate for LayerInfo::getRefreshRate()
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700243 // was due to animation or infrequent updates
Ady Abraham86ac5c52023-01-11 15:24:03 -0800244 bool animating = false;
245 // Whether the last reported rate for LayerInfo::getRefreshRate()
246 // was due to infrequent updates
247 bool infrequent = false;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700248 };
249
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700250 // Class to store past calculated refresh rate and determine whether
251 // the refresh rate calculated is consistent with past values
252 class RefreshRateHistory {
253 public:
254 static constexpr auto HISTORY_SIZE = 90;
255 static constexpr std::chrono::nanoseconds HISTORY_DURATION = 2s;
256
257 RefreshRateHistory(const std::string& name) : mName(name) {}
258
259 // Clears History
260 void clear();
261
262 // Adds a new refresh rate and returns true if it is consistent
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100263 bool add(Fps refreshRate, nsecs_t now);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700264
265 private:
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100266 friend class LayerHistoryTest;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700267
268 // Holds the refresh rate when it was calculated
269 struct RefreshRateData {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700270 Fps refreshRate;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700271 nsecs_t timestamp = 0;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700272 };
273
274 // Holds tracing strings
275 struct HeuristicTraceTagData {
276 std::string min;
277 std::string max;
278 std::string consistent;
279 std::string average;
280 };
281
282 bool isConsistent() const;
283 HeuristicTraceTagData makeHeuristicTraceTagData() const;
284
285 const std::string mName;
286 mutable std::optional<HeuristicTraceTagData> mHeuristicTraceTagData;
287 std::deque<RefreshRateData> mRefreshRates;
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100288 static constexpr float MARGIN_CONSISTENT_FPS = 1.0;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700289 };
290
ramindani63db24e2023-04-03 10:56:05 -0700291 // Represents whether we were able to determine either layer is frequent or infrequent
292 bool mIsFrequencyConclusive = true;
293 struct Frequent {
294 bool isFrequent;
295 bool clearHistory;
296 // Represents whether we were able to determine isFrequent conclusively
297 bool isConclusive;
Arthur Hungc70bee22023-06-02 01:35:52 +0000298 // Represents whether the latest frames are small dirty.
299 bool isSmallDirty = false;
ramindani63db24e2023-04-03 10:56:05 -0700300 };
301 Frequent isFrequent(nsecs_t now) const;
Ady Abraham5def7332020-05-29 16:13:47 -0700302 bool isAnimating(nsecs_t now) const;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800303 bool hasEnoughDataForHeuristic() const;
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400304 std::optional<Fps> calculateRefreshRateIfPossible(const RefreshRateSelector&, nsecs_t now);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700305 std::optional<nsecs_t> calculateAverageFrameTime() const;
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000306 bool isFrameTimeValid(const FrameTimeData&) const;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800307
Ady Abrahama6b676e2020-05-27 14:29:09 -0700308 const std::string mName;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700309 const uid_t mOwnerUid;
Ady Abrahama6b676e2020-05-27 14:29:09 -0700310
Marin Shalamanov4ad8b302020-12-11 15:50:08 +0100311 // Used for sanitizing the heuristic data. If two frames are less than
312 // this period apart from each other they'll be considered as duplicates.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700313 static constexpr nsecs_t kMinPeriodBetweenFrames = (240_Hz).getPeriodNsecs();
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100314 // Used for sanitizing the heuristic data. If two frames are more than
315 // this period apart from each other, the interval between them won't be
316 // taken into account when calculating average frame rate.
317 static constexpr nsecs_t kMaxPeriodBetweenFrames = kMinFpsForFrequentLayer.getPeriodNsecs();
Arthur Hungc70bee22023-06-02 01:35:52 +0000318 // Used for sanitizing the heuristic data. If frames are small dirty updating and are less
319 // than this period apart from each other, the interval between them won't be
320 // taken into account when calculating average frame rate.
321 static constexpr nsecs_t kMinPeriodBetweenSmallDirtyFrames = (60_Hz).getPeriodNsecs();
322
Ady Abraham8a82ba62020-01-17 12:43:17 -0800323 LayerHistory::LayerVoteType mDefaultVote;
324
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700325 LayerVote mLayerVote;
326
Ady Abraham8a82ba62020-01-17 12:43:17 -0800327 nsecs_t mLastUpdatedTime = 0;
328
Ady Abraham5def7332020-05-29 16:13:47 -0700329 nsecs_t mLastAnimationTime = 0;
330
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700331 RefreshRateHeuristicData mLastRefreshRate;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800332
Ady Abraham8a82ba62020-01-17 12:43:17 -0800333 std::deque<FrameTimeData> mFrameTimes;
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000334 std::chrono::time_point<std::chrono::steady_clock> mFrameTimeValidSince =
335 std::chrono::steady_clock::now();
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700336 static constexpr size_t HISTORY_SIZE = RefreshRateHistory::HISTORY_SIZE;
Arthur Hungc70bee22023-06-02 01:35:52 +0000337 static constexpr std::chrono::nanoseconds HISTORY_DURATION = LayerHistory::kMaxPeriodForHistory;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700338
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000339 std::unique_ptr<LayerProps> mLayerProps;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700340
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700341 RefreshRateHistory mRefreshRateHistory;
342
Arthur Hung8144e022023-09-08 10:26:23 +0000343 // This will be accessed from only one thread when counting a layer is frequent or infrequent,
344 // and to determine whether a layer is in small dirty updating.
345 mutable int32_t mLastSmallDirtyCount = 0;
346
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700347 mutable std::unordered_map<LayerHistory::LayerVoteType, std::string> mTraceTags;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700348
349 // Shared for all LayerInfo instances
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700350 static bool sTraceEnabled;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800351};
352
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000353struct LayerProps {
354 bool visible = false;
355 FloatRect bounds;
356 ui::Transform transform;
357 LayerInfo::FrameRate setFrameRateVote;
358 int32_t frameRateSelectionPriority = -1;
Arthur Hungc70bee22023-06-02 01:35:52 +0000359 bool isSmallDirty = false;
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000360};
361
Ady Abraham8a82ba62020-01-17 12:43:17 -0800362} // namespace scheduler
363} // namespace android