Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 1 | /* |
| 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 Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 26 | #include "Scheduler/Seamlessness.h" |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 27 | #include "SchedulerUtils.h" |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | class Layer; |
| 32 | |
| 33 | namespace scheduler { |
| 34 | |
| 35 | using namespace std::chrono_literals; |
| 36 | |
| 37 | // Maximum period between presents for a layer to be considered active. |
| 38 | constexpr std::chrono::nanoseconds MAX_ACTIVE_LAYER_PERIOD_NS = 1200ms; |
| 39 | |
| 40 | // Earliest present time for a layer to be considered active. |
| 41 | constexpr 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 Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 46 | class LayerInfo { |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 47 | using LayerUpdateType = LayerHistory::LayerUpdateType; |
| 48 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 49 | // 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 Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame^] | 52 | 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 Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 56 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 57 | friend class LayerHistoryTest; |
Marin Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame^] | 58 | friend class LayerInfoTest; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 59 | |
| 60 | public: |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 61 | // Holds information about the layer vote |
| 62 | struct LayerVote { |
| 63 | LayerHistory::LayerVoteType type = LayerHistory::LayerVoteType::Heuristic; |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 64 | Fps fps{0.0f}; |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 65 | Seamlessness seamlessness = Seamlessness::Default; |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 66 | }; |
| 67 | |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 68 | static void setTraceEnabled(bool enabled) { sTraceEnabled = enabled; } |
| 69 | |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 70 | static void setRefreshRateConfigs(const RefreshRateConfigs& refreshRateConfigs) { |
| 71 | sRefreshRateConfigs = &refreshRateConfigs; |
| 72 | } |
| 73 | |
Marin Shalamanov | 4ad8b30 | 2020-12-11 15:50:08 +0100 | [diff] [blame] | 74 | LayerInfo(const std::string& name, LayerHistory::LayerVoteType defaultVote); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 75 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 76 | LayerInfo(const LayerInfo&) = delete; |
| 77 | LayerInfo& operator=(const LayerInfo&) = delete; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 78 | |
| 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 Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 82 | void setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, LayerUpdateType updateType, |
| 83 | bool pendingConfigChange); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 84 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 85 | // Sets an explicit layer vote. This usually comes directly from the application via |
| 86 | // ANativeWindow_setFrameRate API |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 87 | void setLayerVote(LayerVote vote) { mLayerVote = vote; } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 88 | |
| 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 Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 95 | void resetLayerVote() { mLayerVote = {mDefaultVote, Fps(0.0f), Seamlessness::Default}; } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 96 | |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 97 | LayerVote getRefreshRateVote(nsecs_t now); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 98 | |
| 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 Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 103 | // Returns a C string for tracing a vote |
| 104 | const char* getTraceTag(LayerHistory::LayerVoteType type) const; |
| 105 | |
Ady Abraham | 983e568 | 2020-05-28 16:49:18 -0700 | [diff] [blame] | 106 | void onLayerInactive(nsecs_t now) { |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 107 | // 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 Abraham | 983e568 | 2020-05-28 16:49:18 -0700 | [diff] [blame] | 111 | const auto timePoint = std::chrono::nanoseconds(now); |
| 112 | mFrameTimeValidSince = std::chrono::time_point<std::chrono::steady_clock>(timePoint); |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 113 | mLastRefreshRate = {}; |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 114 | mRefreshRateHistory.clear(); |
Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 115 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 116 | |
Ady Abraham | 983e568 | 2020-05-28 16:49:18 -0700 | [diff] [blame] | 117 | void clearHistory(nsecs_t now) { |
| 118 | onLayerInactive(now); |
| 119 | mFrameTimes.clear(); |
| 120 | } |
| 121 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 122 | private: |
Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 123 | // Used to store the layer timestamps |
| 124 | struct FrameTimeData { |
Marin Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame^] | 125 | nsecs_t presentTime; // desiredPresentTime, if provided |
Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 126 | nsecs_t queueTime; // buffer queue time |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 127 | bool pendingConfigChange; |
Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 128 | }; |
| 129 | |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 130 | // Holds information about the calculated and reported refresh rate |
| 131 | struct RefreshRateHeuristicData { |
| 132 | // Rate calculated on the layer |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 133 | Fps calculated{0.0f}; |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 134 | // Last reported rate for LayerInfo::getRefreshRate() |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 135 | Fps reported{0.0f}; |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 136 | // Whether the last reported rate for LayerInfo::getRefreshRate() |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 137 | // was due to animation or infrequent updates |
| 138 | bool animatingOrInfrequent = false; |
| 139 | }; |
| 140 | |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 141 | // 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 Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 154 | bool add(Fps refreshRate, nsecs_t now); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 155 | |
| 156 | private: |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 157 | friend class LayerHistoryTest; |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 158 | |
| 159 | // Holds the refresh rate when it was calculated |
| 160 | struct RefreshRateData { |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 161 | Fps refreshRate{0.0f}; |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 162 | nsecs_t timestamp = 0; |
| 163 | |
| 164 | bool operator<(const RefreshRateData& other) const { |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 165 | // 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 Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 168 | } |
| 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 Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 185 | static constexpr float MARGIN_CONSISTENT_FPS = 1.0; |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 186 | }; |
| 187 | |
Ady Abraham | af6d8a4 | 2020-05-27 19:56:15 +0000 | [diff] [blame] | 188 | bool isFrequent(nsecs_t now) const; |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 189 | bool isAnimating(nsecs_t now) const; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 190 | bool hasEnoughDataForHeuristic() const; |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 191 | std::optional<Fps> calculateRefreshRateIfPossible(nsecs_t now); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 192 | std::optional<nsecs_t> calculateAverageFrameTime() const; |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 193 | bool isFrameTimeValid(const FrameTimeData&) const; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 194 | |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 195 | const std::string mName; |
| 196 | |
Marin Shalamanov | 4ad8b30 | 2020-12-11 15:50:08 +0100 | [diff] [blame] | 197 | // 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 Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame^] | 200 | // 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 Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 204 | LayerHistory::LayerVoteType mDefaultVote; |
| 205 | |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 206 | LayerVote mLayerVote; |
| 207 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 208 | nsecs_t mLastUpdatedTime = 0; |
| 209 | |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 210 | nsecs_t mLastAnimationTime = 0; |
| 211 | |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 212 | RefreshRateHeuristicData mLastRefreshRate; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 213 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 214 | std::deque<FrameTimeData> mFrameTimes; |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 215 | std::chrono::time_point<std::chrono::steady_clock> mFrameTimeValidSince = |
| 216 | std::chrono::steady_clock::now(); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 217 | 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 Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 223 | |
| 224 | // Shared for all LayerInfo instances |
| 225 | static const RefreshRateConfigs* sRefreshRateConfigs; |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 226 | static bool sTraceEnabled; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | } // namespace scheduler |
| 230 | } // namespace android |