blob: e32ba099f6f94857478dcbab8821e8284881531f [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
19#include <utils/Timers.h>
20
21#include <chrono>
22#include <deque>
23
24#include "LayerHistory.h"
25#include "RefreshRateConfigs.h"
Marin Shalamanov53fc11d2020-11-20 14:00:13 +010026#include "Scheduler/Seamlessness.h"
Ady Abraham8a82ba62020-01-17 12:43:17 -080027#include "SchedulerUtils.h"
28
29namespace android {
30
31class Layer;
32
33namespace scheduler {
34
35using namespace std::chrono_literals;
36
37// Maximum period between presents for a layer to be considered active.
38constexpr std::chrono::nanoseconds MAX_ACTIVE_LAYER_PERIOD_NS = 1200ms;
39
40// Earliest present time for a layer to be considered active.
41constexpr nsecs_t getActiveLayerThreshold(nsecs_t now) {
42 return now - MAX_ACTIVE_LAYER_PERIOD_NS.count();
43}
44
45// Stores history of present times and refresh rates for a layer.
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010046class LayerInfo {
Ady Abraham5def7332020-05-29 16:13:47 -070047 using LayerUpdateType = LayerHistory::LayerUpdateType;
48
Ady Abraham8a82ba62020-01-17 12:43:17 -080049 // Layer is considered frequent if the earliest value in the window of most recent present times
50 // is within a threshold. If a layer is infrequent, its average refresh rate is disregarded in
51 // favor of a low refresh rate.
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010052 static constexpr size_t kFrequentLayerWindowSize = 3;
53 static constexpr Fps kMinFpsForFrequentLayer{10.0f};
54 static constexpr auto kMaxPeriodForFrequentLayerNs =
55 std::chrono::nanoseconds(kMinFpsForFrequentLayer.getPeriodNsecs()) + 1ms;
Ady Abraham8a82ba62020-01-17 12:43:17 -080056
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010057 friend class LayerHistoryTest;
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010058 friend class LayerInfoTest;
Ady Abraham8a82ba62020-01-17 12:43:17 -080059
60public:
Marin Shalamanov46084422020-10-13 12:33:42 +020061 // Holds information about the layer vote
62 struct LayerVote {
63 LayerHistory::LayerVoteType type = LayerHistory::LayerVoteType::Heuristic;
Marin Shalamanove8a663d2020-11-24 17:48:00 +010064 Fps fps{0.0f};
Marin Shalamanov53fc11d2020-11-20 14:00:13 +010065 Seamlessness seamlessness = Seamlessness::Default;
Marin Shalamanov46084422020-10-13 12:33:42 +020066 };
67
Ady Abraham0ccd79b2020-06-10 10:11:17 -070068 static void setTraceEnabled(bool enabled) { sTraceEnabled = enabled; }
69
Ady Abrahamb1b9d412020-06-01 19:53:52 -070070 static void setRefreshRateConfigs(const RefreshRateConfigs& refreshRateConfigs) {
71 sRefreshRateConfigs = &refreshRateConfigs;
72 }
73
Marin Shalamanov4ad8b302020-12-11 15:50:08 +010074 LayerInfo(const std::string& name, LayerHistory::LayerVoteType defaultVote);
Ady Abraham8a82ba62020-01-17 12:43:17 -080075
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010076 LayerInfo(const LayerInfo&) = delete;
77 LayerInfo& operator=(const LayerInfo&) = delete;
Ady Abraham8a82ba62020-01-17 12:43:17 -080078
79 // Records the last requested present time. It also stores information about when
80 // the layer was last updated. If the present time is farther in the future than the
81 // updated time, the updated time is the present time.
Ady Abraham5def7332020-05-29 16:13:47 -070082 void setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, LayerUpdateType updateType,
83 bool pendingConfigChange);
Ady Abraham8a82ba62020-01-17 12:43:17 -080084
Ady Abraham8a82ba62020-01-17 12:43:17 -080085 // Sets an explicit layer vote. This usually comes directly from the application via
86 // ANativeWindow_setFrameRate API
Marin Shalamanov46084422020-10-13 12:33:42 +020087 void setLayerVote(LayerVote vote) { mLayerVote = vote; }
Ady Abraham8a82ba62020-01-17 12:43:17 -080088
89 // Sets the default layer vote. This will be the layer vote after calling to resetLayerVote().
90 // This is used for layers that called to setLayerVote() and then removed the vote, so that the
91 // layer can go back to whatever vote it had before the app voted for it.
92 void setDefaultLayerVote(LayerHistory::LayerVoteType type) { mDefaultVote = type; }
93
94 // Resets the layer vote to its default.
Marin Shalamanove8a663d2020-11-24 17:48:00 +010095 void resetLayerVote() { mLayerVote = {mDefaultVote, Fps(0.0f), Seamlessness::Default}; }
Ady Abraham8a82ba62020-01-17 12:43:17 -080096
Marin Shalamanov46084422020-10-13 12:33:42 +020097 LayerVote getRefreshRateVote(nsecs_t now);
Ady Abraham8a82ba62020-01-17 12:43:17 -080098
99 // Return the last updated time. If the present time is farther in the future than the
100 // updated time, the updated time is the present time.
101 nsecs_t getLastUpdatedTime() const { return mLastUpdatedTime; }
102
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700103 // Returns a C string for tracing a vote
104 const char* getTraceTag(LayerHistory::LayerVoteType type) const;
105
Ady Abraham983e5682020-05-28 16:49:18 -0700106 void onLayerInactive(nsecs_t now) {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000107 // Mark mFrameTimeValidSince to now to ignore all previous frame times.
108 // We are not deleting the old frame to keep track of whether we should treat the first
109 // buffer as Max as we don't know anything about this layer or Min as this layer is
110 // posting infrequent updates.
Ady Abraham983e5682020-05-28 16:49:18 -0700111 const auto timePoint = std::chrono::nanoseconds(now);
112 mFrameTimeValidSince = std::chrono::time_point<std::chrono::steady_clock>(timePoint);
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700113 mLastRefreshRate = {};
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700114 mRefreshRateHistory.clear();
Ady Abrahama61edcb2020-01-30 18:32:03 -0800115 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800116
Ady Abraham983e5682020-05-28 16:49:18 -0700117 void clearHistory(nsecs_t now) {
118 onLayerInactive(now);
119 mFrameTimes.clear();
120 }
121
Ady Abraham8a82ba62020-01-17 12:43:17 -0800122private:
Ady Abrahama61edcb2020-01-30 18:32:03 -0800123 // Used to store the layer timestamps
124 struct FrameTimeData {
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100125 nsecs_t presentTime; // desiredPresentTime, if provided
Ady Abrahama61edcb2020-01-30 18:32:03 -0800126 nsecs_t queueTime; // buffer queue time
Ady Abraham32efd542020-05-19 17:49:26 -0700127 bool pendingConfigChange;
Ady Abrahama61edcb2020-01-30 18:32:03 -0800128 };
129
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700130 // Holds information about the calculated and reported refresh rate
131 struct RefreshRateHeuristicData {
132 // Rate calculated on the layer
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100133 Fps calculated{0.0f};
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100134 // Last reported rate for LayerInfo::getRefreshRate()
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100135 Fps reported{0.0f};
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100136 // Whether the last reported rate for LayerInfo::getRefreshRate()
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700137 // was due to animation or infrequent updates
138 bool animatingOrInfrequent = false;
139 };
140
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700141 // Class to store past calculated refresh rate and determine whether
142 // the refresh rate calculated is consistent with past values
143 class RefreshRateHistory {
144 public:
145 static constexpr auto HISTORY_SIZE = 90;
146 static constexpr std::chrono::nanoseconds HISTORY_DURATION = 2s;
147
148 RefreshRateHistory(const std::string& name) : mName(name) {}
149
150 // Clears History
151 void clear();
152
153 // Adds a new refresh rate and returns true if it is consistent
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100154 bool add(Fps refreshRate, nsecs_t now);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700155
156 private:
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100157 friend class LayerHistoryTest;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700158
159 // Holds the refresh rate when it was calculated
160 struct RefreshRateData {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100161 Fps refreshRate{0.0f};
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700162 nsecs_t timestamp = 0;
163
164 bool operator<(const RefreshRateData& other) const {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100165 // We don't need comparison with margins since we are using
166 // this to find the min and max refresh rates.
167 return refreshRate.getValue() < other.refreshRate.getValue();
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700168 }
169 };
170
171 // Holds tracing strings
172 struct HeuristicTraceTagData {
173 std::string min;
174 std::string max;
175 std::string consistent;
176 std::string average;
177 };
178
179 bool isConsistent() const;
180 HeuristicTraceTagData makeHeuristicTraceTagData() const;
181
182 const std::string mName;
183 mutable std::optional<HeuristicTraceTagData> mHeuristicTraceTagData;
184 std::deque<RefreshRateData> mRefreshRates;
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100185 static constexpr float MARGIN_CONSISTENT_FPS = 1.0;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700186 };
187
Ady Abrahamaf6d8a42020-05-27 19:56:15 +0000188 bool isFrequent(nsecs_t now) const;
Ady Abraham5def7332020-05-29 16:13:47 -0700189 bool isAnimating(nsecs_t now) const;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800190 bool hasEnoughDataForHeuristic() const;
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100191 std::optional<Fps> calculateRefreshRateIfPossible(nsecs_t now);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700192 std::optional<nsecs_t> calculateAverageFrameTime() const;
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000193 bool isFrameTimeValid(const FrameTimeData&) const;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800194
Ady Abrahama6b676e2020-05-27 14:29:09 -0700195 const std::string mName;
196
Marin Shalamanov4ad8b302020-12-11 15:50:08 +0100197 // Used for sanitizing the heuristic data. If two frames are less than
198 // this period apart from each other they'll be considered as duplicates.
199 static constexpr nsecs_t kMinPeriodBetweenFrames = Fps(120.f).getPeriodNsecs();
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100200 // Used for sanitizing the heuristic data. If two frames are more than
201 // this period apart from each other, the interval between them won't be
202 // taken into account when calculating average frame rate.
203 static constexpr nsecs_t kMaxPeriodBetweenFrames = kMinFpsForFrequentLayer.getPeriodNsecs();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800204 LayerHistory::LayerVoteType mDefaultVote;
205
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700206 LayerVote mLayerVote;
207
Ady Abraham8a82ba62020-01-17 12:43:17 -0800208 nsecs_t mLastUpdatedTime = 0;
209
Ady Abraham5def7332020-05-29 16:13:47 -0700210 nsecs_t mLastAnimationTime = 0;
211
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700212 RefreshRateHeuristicData mLastRefreshRate;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800213
Ady Abraham8a82ba62020-01-17 12:43:17 -0800214 std::deque<FrameTimeData> mFrameTimes;
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000215 std::chrono::time_point<std::chrono::steady_clock> mFrameTimeValidSince =
216 std::chrono::steady_clock::now();
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700217 static constexpr size_t HISTORY_SIZE = RefreshRateHistory::HISTORY_SIZE;
218 static constexpr std::chrono::nanoseconds HISTORY_DURATION = 1s;
219
220 RefreshRateHistory mRefreshRateHistory;
221
222 mutable std::unordered_map<LayerHistory::LayerVoteType, std::string> mTraceTags;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700223
224 // Shared for all LayerInfo instances
225 static const RefreshRateConfigs* sRefreshRateConfigs;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700226 static bool sTraceEnabled;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800227};
228
229} // namespace scheduler
230} // namespace android