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" |
| 26 | #include "SchedulerUtils.h" |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | class Layer; |
| 31 | |
| 32 | namespace scheduler { |
| 33 | |
| 34 | using namespace std::chrono_literals; |
| 35 | |
| 36 | // Maximum period between presents for a layer to be considered active. |
| 37 | constexpr std::chrono::nanoseconds MAX_ACTIVE_LAYER_PERIOD_NS = 1200ms; |
| 38 | |
| 39 | // Earliest present time for a layer to be considered active. |
| 40 | constexpr nsecs_t getActiveLayerThreshold(nsecs_t now) { |
| 41 | return now - MAX_ACTIVE_LAYER_PERIOD_NS.count(); |
| 42 | } |
| 43 | |
| 44 | // Stores history of present times and refresh rates for a layer. |
| 45 | class LayerInfoV2 { |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 46 | using LayerUpdateType = LayerHistory::LayerUpdateType; |
| 47 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 48 | // Layer is considered frequent if the earliest value in the window of most recent present times |
| 49 | // is within a threshold. If a layer is infrequent, its average refresh rate is disregarded in |
| 50 | // favor of a low refresh rate. |
| 51 | static constexpr size_t FREQUENT_LAYER_WINDOW_SIZE = 3; |
Ady Abraham | f5bc779 | 2020-05-27 20:04:20 +0000 | [diff] [blame] | 52 | static constexpr float MIN_FPS_FOR_FREQUENT_LAYER = 10.0f; |
| 53 | static constexpr auto MAX_FREQUENT_LAYER_PERIOD_NS = |
| 54 | std::chrono::nanoseconds(static_cast<nsecs_t>(1e9f / MIN_FPS_FOR_FREQUENT_LAYER)) + 1ms; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 55 | |
| 56 | friend class LayerHistoryTestV2; |
| 57 | |
| 58 | public: |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame^] | 59 | // Holds information about the layer vote |
| 60 | struct LayerVote { |
| 61 | LayerHistory::LayerVoteType type = LayerHistory::LayerVoteType::Heuristic; |
| 62 | float fps = 0.0f; |
| 63 | bool shouldBeSeamless = true; |
| 64 | }; |
| 65 | |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 66 | static void setTraceEnabled(bool enabled) { sTraceEnabled = enabled; } |
| 67 | |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 68 | static void setRefreshRateConfigs(const RefreshRateConfigs& refreshRateConfigs) { |
| 69 | sRefreshRateConfigs = &refreshRateConfigs; |
| 70 | } |
| 71 | |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 72 | LayerInfoV2(const std::string& name, nsecs_t highRefreshRatePeriod, |
| 73 | LayerHistory::LayerVoteType defaultVote); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 74 | |
| 75 | LayerInfoV2(const LayerInfo&) = delete; |
| 76 | LayerInfoV2& operator=(const LayerInfoV2&) = delete; |
| 77 | |
| 78 | // Records the last requested present time. It also stores information about when |
| 79 | // the layer was last updated. If the present time is farther in the future than the |
| 80 | // updated time, the updated time is the present time. |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 81 | void setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, LayerUpdateType updateType, |
| 82 | bool pendingConfigChange); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 83 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 84 | // Sets an explicit layer vote. This usually comes directly from the application via |
| 85 | // ANativeWindow_setFrameRate API |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame^] | 86 | void setLayerVote(LayerVote vote) { mLayerVote = vote; } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 87 | |
| 88 | // Sets the default layer vote. This will be the layer vote after calling to resetLayerVote(). |
| 89 | // This is used for layers that called to setLayerVote() and then removed the vote, so that the |
| 90 | // layer can go back to whatever vote it had before the app voted for it. |
| 91 | void setDefaultLayerVote(LayerHistory::LayerVoteType type) { mDefaultVote = type; } |
| 92 | |
| 93 | // Resets the layer vote to its default. |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame^] | 94 | void resetLayerVote() { mLayerVote = {mDefaultVote, 0.0f, true}; } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 95 | |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame^] | 96 | LayerVote getRefreshRateVote(nsecs_t now); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 97 | |
| 98 | // Return the last updated time. If the present time is farther in the future than the |
| 99 | // updated time, the updated time is the present time. |
| 100 | nsecs_t getLastUpdatedTime() const { return mLastUpdatedTime; } |
| 101 | |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 102 | // Returns a C string for tracing a vote |
| 103 | const char* getTraceTag(LayerHistory::LayerVoteType type) const; |
| 104 | |
Ady Abraham | 983e568 | 2020-05-28 16:49:18 -0700 | [diff] [blame] | 105 | void onLayerInactive(nsecs_t now) { |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 106 | // Mark mFrameTimeValidSince to now to ignore all previous frame times. |
| 107 | // We are not deleting the old frame to keep track of whether we should treat the first |
| 108 | // buffer as Max as we don't know anything about this layer or Min as this layer is |
| 109 | // posting infrequent updates. |
Ady Abraham | 983e568 | 2020-05-28 16:49:18 -0700 | [diff] [blame] | 110 | const auto timePoint = std::chrono::nanoseconds(now); |
| 111 | mFrameTimeValidSince = std::chrono::time_point<std::chrono::steady_clock>(timePoint); |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 112 | mLastRefreshRate = {}; |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 113 | mRefreshRateHistory.clear(); |
Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 114 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 115 | |
Ady Abraham | 983e568 | 2020-05-28 16:49:18 -0700 | [diff] [blame] | 116 | void clearHistory(nsecs_t now) { |
| 117 | onLayerInactive(now); |
| 118 | mFrameTimes.clear(); |
| 119 | } |
| 120 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 121 | private: |
Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 122 | // Used to store the layer timestamps |
| 123 | struct FrameTimeData { |
| 124 | nsecs_t presetTime; // desiredPresentTime, if provided |
| 125 | nsecs_t queueTime; // buffer queue time |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 126 | bool pendingConfigChange; |
Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 127 | }; |
| 128 | |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 129 | // Holds information about the calculated and reported refresh rate |
| 130 | struct RefreshRateHeuristicData { |
| 131 | // Rate calculated on the layer |
| 132 | float calculated = 0.0f; |
| 133 | // Last reported rate for LayerInfoV2::getRefreshRate() |
| 134 | float reported = 0.0f; |
| 135 | // Whether the last reported rate for LayerInfoV2::getRefreshRate() |
| 136 | // was due to animation or infrequent updates |
| 137 | bool animatingOrInfrequent = false; |
| 138 | }; |
| 139 | |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 140 | // Class to store past calculated refresh rate and determine whether |
| 141 | // the refresh rate calculated is consistent with past values |
| 142 | class RefreshRateHistory { |
| 143 | public: |
| 144 | static constexpr auto HISTORY_SIZE = 90; |
| 145 | static constexpr std::chrono::nanoseconds HISTORY_DURATION = 2s; |
| 146 | |
| 147 | RefreshRateHistory(const std::string& name) : mName(name) {} |
| 148 | |
| 149 | // Clears History |
| 150 | void clear(); |
| 151 | |
| 152 | // Adds a new refresh rate and returns true if it is consistent |
| 153 | bool add(float refreshRate, nsecs_t now); |
| 154 | |
| 155 | private: |
| 156 | friend class LayerHistoryTestV2; |
| 157 | |
| 158 | // Holds the refresh rate when it was calculated |
| 159 | struct RefreshRateData { |
| 160 | float refreshRate = 0.0f; |
| 161 | nsecs_t timestamp = 0; |
| 162 | |
| 163 | bool operator<(const RefreshRateData& other) const { |
| 164 | return refreshRate < other.refreshRate; |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | // Holds tracing strings |
| 169 | struct HeuristicTraceTagData { |
| 170 | std::string min; |
| 171 | std::string max; |
| 172 | std::string consistent; |
| 173 | std::string average; |
| 174 | }; |
| 175 | |
| 176 | bool isConsistent() const; |
| 177 | HeuristicTraceTagData makeHeuristicTraceTagData() const; |
| 178 | |
| 179 | const std::string mName; |
| 180 | mutable std::optional<HeuristicTraceTagData> mHeuristicTraceTagData; |
| 181 | std::deque<RefreshRateData> mRefreshRates; |
| 182 | static constexpr float MARGIN_FPS = 1.0; |
| 183 | }; |
| 184 | |
Ady Abraham | af6d8a4 | 2020-05-27 19:56:15 +0000 | [diff] [blame] | 185 | bool isFrequent(nsecs_t now) const; |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 186 | bool isAnimating(nsecs_t now) const; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 187 | bool hasEnoughDataForHeuristic() const; |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 188 | std::optional<float> calculateRefreshRateIfPossible(nsecs_t now); |
| 189 | std::optional<nsecs_t> calculateAverageFrameTime() const; |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 190 | bool isFrameTimeValid(const FrameTimeData&) const; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 191 | |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 192 | const std::string mName; |
| 193 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 194 | // Used for sanitizing the heuristic data |
| 195 | const nsecs_t mHighRefreshRatePeriod; |
| 196 | LayerHistory::LayerVoteType mDefaultVote; |
| 197 | |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 198 | LayerVote mLayerVote; |
| 199 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 200 | nsecs_t mLastUpdatedTime = 0; |
| 201 | |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 202 | nsecs_t mLastAnimationTime = 0; |
| 203 | |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 204 | RefreshRateHeuristicData mLastRefreshRate; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 205 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 206 | std::deque<FrameTimeData> mFrameTimes; |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 207 | std::chrono::time_point<std::chrono::steady_clock> mFrameTimeValidSince = |
| 208 | std::chrono::steady_clock::now(); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 209 | static constexpr size_t HISTORY_SIZE = RefreshRateHistory::HISTORY_SIZE; |
| 210 | static constexpr std::chrono::nanoseconds HISTORY_DURATION = 1s; |
| 211 | |
| 212 | RefreshRateHistory mRefreshRateHistory; |
| 213 | |
| 214 | mutable std::unordered_map<LayerHistory::LayerVoteType, std::string> mTraceTags; |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 215 | |
| 216 | // Shared for all LayerInfo instances |
| 217 | static const RefreshRateConfigs* sRefreshRateConfigs; |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 218 | static bool sTraceEnabled; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | } // namespace scheduler |
| 222 | } // namespace android |