blob: 2d88a4fb9d4806ca5c08b5b4f60acc88dca10209 [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
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080028#include <scheduler/Seamlessness.h>
Ady Abraham8a82ba62020-01-17 12:43:17 -080029
30#include "LayerHistory.h"
31#include "RefreshRateConfigs.h"
32#include "SchedulerUtils.h"
33
34namespace android {
35
36class Layer;
37
38namespace scheduler {
39
40using namespace std::chrono_literals;
41
42// 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.
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010057 static constexpr size_t kFrequentLayerWindowSize = 3;
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;
Marin Shalamanov46084422020-10-13 12:33:42 +020071 };
72
Ady Abrahambdda8f02021-04-01 16:06:11 -070073 // FrameRateCompatibility specifies how we should interpret the frame rate associated with
74 // the layer.
75 enum class FrameRateCompatibility {
76 Default, // Layer didn't specify any specific handling strategy
77
78 Exact, // Layer needs the exact frame rate.
79
80 ExactOrMultiple, // Layer needs the exact frame rate (or a multiple of it) to present the
81 // content properly. Any other value will result in a pull down.
82
83 NoVote, // Layer doesn't have any requirements for the refresh rate and
84 // should not be considered when the display refresh rate is determined.
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070085
86 ftl_last = NoVote
Ady Abrahambdda8f02021-04-01 16:06:11 -070087 };
88
89 // Encapsulates the frame rate and compatibility of the layer. This information will be used
90 // when the display refresh rate is determined.
91 struct FrameRate {
92 using Seamlessness = scheduler::Seamlessness;
93
94 Fps rate;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070095 FrameRateCompatibility type = FrameRateCompatibility::Default;
96 Seamlessness seamlessness = Seamlessness::Default;
Ady Abrahambdda8f02021-04-01 16:06:11 -070097
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070098 FrameRate() = default;
99
Ady Abrahambdda8f02021-04-01 16:06:11 -0700100 FrameRate(Fps rate, FrameRateCompatibility type,
101 Seamlessness seamlessness = Seamlessness::OnlySeamless)
102 : rate(rate), type(type), seamlessness(getSeamlessness(rate, seamlessness)) {}
103
104 bool operator==(const FrameRate& other) const {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700105 return isApproxEqual(rate, other.rate) && type == other.type &&
Ady Abrahambdda8f02021-04-01 16:06:11 -0700106 seamlessness == other.seamlessness;
107 }
108
109 bool operator!=(const FrameRate& other) const { return !(*this == other); }
110
111 // Convert an ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_* value to a
112 // Layer::FrameRateCompatibility. Logs fatal if the compatibility value is invalid.
113 static FrameRateCompatibility convertCompatibility(int8_t compatibility);
114 static scheduler::Seamlessness convertChangeFrameRateStrategy(int8_t strategy);
115
116 private:
117 static Seamlessness getSeamlessness(Fps rate, Seamlessness seamlessness) {
118 if (!rate.isValid()) {
119 // Refresh rate of 0 is a special value which should reset the vote to
120 // its default value.
121 return Seamlessness::Default;
122 }
123 return seamlessness;
124 }
125 };
126
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700127 static void setTraceEnabled(bool enabled) { sTraceEnabled = enabled; }
128
Ady Abrahambdda8f02021-04-01 16:06:11 -0700129 LayerInfo(const std::string& name, uid_t ownerUid, LayerHistory::LayerVoteType defaultVote);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800130
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100131 LayerInfo(const LayerInfo&) = delete;
132 LayerInfo& operator=(const LayerInfo&) = delete;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800133
Ady Abrahambdda8f02021-04-01 16:06:11 -0700134 struct LayerProps {
135 bool visible = false;
136 FloatRect bounds;
137 ui::Transform transform;
138 FrameRate setFrameRateVote;
139 int32_t frameRateSelectionPriority = -1;
140 };
141
Ady Abraham8a82ba62020-01-17 12:43:17 -0800142 // Records the last requested present time. It also stores information about when
143 // the layer was last updated. If the present time is farther in the future than the
144 // updated time, the updated time is the present time.
Ady Abraham5def7332020-05-29 16:13:47 -0700145 void setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, LayerUpdateType updateType,
Ady Abrahambdda8f02021-04-01 16:06:11 -0700146 bool pendingModeChange, LayerProps props);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800147
Ady Abraham8a82ba62020-01-17 12:43:17 -0800148 // Sets an explicit layer vote. This usually comes directly from the application via
149 // ANativeWindow_setFrameRate API
Marin Shalamanov46084422020-10-13 12:33:42 +0200150 void setLayerVote(LayerVote vote) { mLayerVote = vote; }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800151
152 // Sets the default layer vote. This will be the layer vote after calling to resetLayerVote().
153 // This is used for layers that called to setLayerVote() and then removed the vote, so that the
154 // layer can go back to whatever vote it had before the app voted for it.
155 void setDefaultLayerVote(LayerHistory::LayerVoteType type) { mDefaultVote = type; }
156
157 // Resets the layer vote to its default.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700158 void resetLayerVote() { mLayerVote = {mDefaultVote, Fps(), Seamlessness::Default}; }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800159
Ady Abrahambdda8f02021-04-01 16:06:11 -0700160 std::string getName() const { return mName; }
161
162 uid_t getOwnerUid() const { return mOwnerUid; }
163
Ady Abraham3efa3942021-06-24 19:01:25 -0700164 LayerVote getRefreshRateVote(const RefreshRateConfigs&, nsecs_t now);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800165
166 // Return the last updated time. If the present time is farther in the future than the
167 // updated time, the updated time is the present time.
168 nsecs_t getLastUpdatedTime() const { return mLastUpdatedTime; }
169
Ady Abrahambdda8f02021-04-01 16:06:11 -0700170 FrameRate getSetFrameRateVote() const { return mLayerProps.setFrameRateVote; }
171 bool isVisible() const { return mLayerProps.visible; }
172 int32_t getFrameRateSelectionPriority() const { return mLayerProps.frameRateSelectionPriority; }
173
174 FloatRect getBounds() const { return mLayerProps.bounds; }
175
176 ui::Transform getTransform() const { return mLayerProps.transform; }
177
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700178 // Returns a C string for tracing a vote
179 const char* getTraceTag(LayerHistory::LayerVoteType type) const;
180
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400181 // Return the framerate of this layer.
182 Fps getFps(nsecs_t now) const;
183
Ady Abraham983e5682020-05-28 16:49:18 -0700184 void onLayerInactive(nsecs_t now) {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000185 // Mark mFrameTimeValidSince to now to ignore all previous frame times.
186 // We are not deleting the old frame to keep track of whether we should treat the first
187 // buffer as Max as we don't know anything about this layer or Min as this layer is
188 // posting infrequent updates.
Ady Abraham983e5682020-05-28 16:49:18 -0700189 const auto timePoint = std::chrono::nanoseconds(now);
190 mFrameTimeValidSince = std::chrono::time_point<std::chrono::steady_clock>(timePoint);
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700191 mLastRefreshRate = {};
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700192 mRefreshRateHistory.clear();
Ady Abrahama61edcb2020-01-30 18:32:03 -0800193 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800194
Ady Abraham983e5682020-05-28 16:49:18 -0700195 void clearHistory(nsecs_t now) {
196 onLayerInactive(now);
197 mFrameTimes.clear();
198 }
199
Ady Abraham8a82ba62020-01-17 12:43:17 -0800200private:
Ady Abrahama61edcb2020-01-30 18:32:03 -0800201 // Used to store the layer timestamps
202 struct FrameTimeData {
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100203 nsecs_t presentTime; // desiredPresentTime, if provided
Ady Abrahama61edcb2020-01-30 18:32:03 -0800204 nsecs_t queueTime; // buffer queue time
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100205 bool pendingModeChange;
Ady Abrahama61edcb2020-01-30 18:32:03 -0800206 };
207
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700208 // Holds information about the calculated and reported refresh rate
209 struct RefreshRateHeuristicData {
210 // Rate calculated on the layer
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700211 Fps calculated;
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100212 // Last reported rate for LayerInfo::getRefreshRate()
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700213 Fps reported;
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100214 // Whether the last reported rate for LayerInfo::getRefreshRate()
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700215 // was due to animation or infrequent updates
216 bool animatingOrInfrequent = false;
217 };
218
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700219 // Class to store past calculated refresh rate and determine whether
220 // the refresh rate calculated is consistent with past values
221 class RefreshRateHistory {
222 public:
223 static constexpr auto HISTORY_SIZE = 90;
224 static constexpr std::chrono::nanoseconds HISTORY_DURATION = 2s;
225
226 RefreshRateHistory(const std::string& name) : mName(name) {}
227
228 // Clears History
229 void clear();
230
231 // Adds a new refresh rate and returns true if it is consistent
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100232 bool add(Fps refreshRate, nsecs_t now);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700233
234 private:
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100235 friend class LayerHistoryTest;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700236
237 // Holds the refresh rate when it was calculated
238 struct RefreshRateData {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700239 Fps refreshRate;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700240 nsecs_t timestamp = 0;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700241 };
242
243 // Holds tracing strings
244 struct HeuristicTraceTagData {
245 std::string min;
246 std::string max;
247 std::string consistent;
248 std::string average;
249 };
250
251 bool isConsistent() const;
252 HeuristicTraceTagData makeHeuristicTraceTagData() const;
253
254 const std::string mName;
255 mutable std::optional<HeuristicTraceTagData> mHeuristicTraceTagData;
256 std::deque<RefreshRateData> mRefreshRates;
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100257 static constexpr float MARGIN_CONSISTENT_FPS = 1.0;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700258 };
259
Ady Abrahamaf6d8a42020-05-27 19:56:15 +0000260 bool isFrequent(nsecs_t now) const;
Ady Abraham5def7332020-05-29 16:13:47 -0700261 bool isAnimating(nsecs_t now) const;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800262 bool hasEnoughDataForHeuristic() const;
Ady Abraham3efa3942021-06-24 19:01:25 -0700263 std::optional<Fps> calculateRefreshRateIfPossible(const RefreshRateConfigs&, nsecs_t now);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700264 std::optional<nsecs_t> calculateAverageFrameTime() const;
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000265 bool isFrameTimeValid(const FrameTimeData&) const;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800266
Ady Abrahama6b676e2020-05-27 14:29:09 -0700267 const std::string mName;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700268 const uid_t mOwnerUid;
Ady Abrahama6b676e2020-05-27 14:29:09 -0700269
Marin Shalamanov4ad8b302020-12-11 15:50:08 +0100270 // Used for sanitizing the heuristic data. If two frames are less than
271 // this period apart from each other they'll be considered as duplicates.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700272 static constexpr nsecs_t kMinPeriodBetweenFrames = (240_Hz).getPeriodNsecs();
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100273 // Used for sanitizing the heuristic data. If two frames are more than
274 // this period apart from each other, the interval between them won't be
275 // taken into account when calculating average frame rate.
276 static constexpr nsecs_t kMaxPeriodBetweenFrames = kMinFpsForFrequentLayer.getPeriodNsecs();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800277 LayerHistory::LayerVoteType mDefaultVote;
278
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700279 LayerVote mLayerVote;
280
Ady Abraham8a82ba62020-01-17 12:43:17 -0800281 nsecs_t mLastUpdatedTime = 0;
282
Ady Abraham5def7332020-05-29 16:13:47 -0700283 nsecs_t mLastAnimationTime = 0;
284
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700285 RefreshRateHeuristicData mLastRefreshRate;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800286
Ady Abraham8a82ba62020-01-17 12:43:17 -0800287 std::deque<FrameTimeData> mFrameTimes;
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000288 std::chrono::time_point<std::chrono::steady_clock> mFrameTimeValidSince =
289 std::chrono::steady_clock::now();
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700290 static constexpr size_t HISTORY_SIZE = RefreshRateHistory::HISTORY_SIZE;
291 static constexpr std::chrono::nanoseconds HISTORY_DURATION = 1s;
292
Ady Abrahambdda8f02021-04-01 16:06:11 -0700293 LayerProps mLayerProps;
294
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700295 RefreshRateHistory mRefreshRateHistory;
296
297 mutable std::unordered_map<LayerHistory::LayerVoteType, std::string> mTraceTags;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700298
299 // Shared for all LayerInfo instances
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700300 static bool sTraceEnabled;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800301};
302
303} // namespace scheduler
304} // namespace android