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 | |
Dominik Laskowski | f6b4ba6 | 2021-11-09 12:46:10 -0800 | [diff] [blame] | 19 | #include <chrono> |
| 20 | #include <deque> |
| 21 | #include <optional> |
| 22 | #include <string> |
| 23 | #include <unordered_map> |
| 24 | |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 25 | #include <ui/Transform.h> |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 26 | #include <utils/Timers.h> |
| 27 | |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 28 | #include <scheduler/Fps.h> |
Dominik Laskowski | f6b4ba6 | 2021-11-09 12:46:10 -0800 | [diff] [blame] | 29 | #include <scheduler/Seamlessness.h> |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 30 | |
Vishnu Nair | 3fbe326 | 2023-09-29 17:07:00 -0700 | [diff] [blame] | 31 | #include "FrameRateCompatibility.h" |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 32 | #include "LayerHistory.h" |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 33 | #include "RefreshRateSelector.h" |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 34 | |
| 35 | namespace android { |
| 36 | |
| 37 | class Layer; |
| 38 | |
| 39 | namespace scheduler { |
| 40 | |
| 41 | using namespace std::chrono_literals; |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 42 | struct LayerProps; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 43 | // Maximum period between presents for a layer to be considered active. |
| 44 | constexpr std::chrono::nanoseconds MAX_ACTIVE_LAYER_PERIOD_NS = 1200ms; |
| 45 | |
| 46 | // Earliest present time for a layer to be considered active. |
| 47 | constexpr 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 Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 52 | class LayerInfo { |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 53 | using LayerUpdateType = LayerHistory::LayerUpdateType; |
| 54 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 55 | // 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 Abraham | 86ac5c5 | 2023-01-11 15:24:03 -0800 | [diff] [blame] | 58 | static constexpr size_t kFrequentLayerWindowSize = 4; |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 59 | static constexpr Fps kMinFpsForFrequentLayer = 10_Hz; |
Marin Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame] | 60 | static constexpr auto kMaxPeriodForFrequentLayerNs = |
| 61 | std::chrono::nanoseconds(kMinFpsForFrequentLayer.getPeriodNsecs()) + 1ms; |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 62 | static constexpr size_t kNumSmallDirtyThreshold = 2; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 63 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 64 | friend class LayerHistoryTest; |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 65 | friend class LayerHistoryIntegrationTest; |
Marin Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame] | 66 | friend class LayerInfoTest; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 67 | |
| 68 | public: |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 69 | // Holds information about the layer vote |
| 70 | struct LayerVote { |
| 71 | LayerHistory::LayerVoteType type = LayerHistory::LayerVoteType::Heuristic; |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 72 | Fps fps; |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 73 | Seamlessness seamlessness = Seamlessness::Default; |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 74 | FrameRateCategory category = FrameRateCategory::Default; |
Rachel Lee | 67afbea | 2023-09-28 15:35:07 -0700 | [diff] [blame] | 75 | bool categorySmoothSwitchOnly = false; |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 76 | |
| 77 | // Returns true if the layer explicitly should contribute to frame rate scoring. |
Rachel Lee | d0694bc | 2023-09-12 14:57:58 -0700 | [diff] [blame] | 78 | bool isNoVote() const { return RefreshRateSelector::isNoVote(type); } |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 79 | }; |
| 80 | |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 81 | using RefreshRateVotes = ftl::SmallVector<LayerInfo::LayerVote, 2>; |
| 82 | |
Rachel Lee | 58cc90d | 2023-09-05 18:50:20 -0700 | [diff] [blame] | 83 | enum class FrameRateSelectionStrategy { |
| 84 | Self, |
| 85 | OverrideChildren, |
Rachel Lee | a021bb0 | 2023-11-20 21:51:09 -0800 | [diff] [blame^] | 86 | DoNotPropagate, |
Rachel Lee | 58cc90d | 2023-09-05 18:50:20 -0700 | [diff] [blame] | 87 | |
Rachel Lee | a021bb0 | 2023-11-20 21:51:09 -0800 | [diff] [blame^] | 88 | ftl_last = DoNotPropagate |
Rachel Lee | 58cc90d | 2023-09-05 18:50:20 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 91 | // Encapsulates the frame rate specifications of the layer. This information will be used |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 92 | // when the display refresh rate is determined. |
| 93 | struct FrameRate { |
| 94 | using Seamlessness = scheduler::Seamlessness; |
| 95 | |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 96 | // Information related to a specific desired frame rate vote. |
| 97 | struct FrameRateVote { |
| 98 | Fps rate; |
| 99 | FrameRateCompatibility type = FrameRateCompatibility::Default; |
| 100 | Seamlessness seamlessness = Seamlessness::Default; |
| 101 | |
| 102 | bool operator==(const FrameRateVote& other) const { |
| 103 | return isApproxEqual(rate, other.rate) && type == other.type && |
| 104 | seamlessness == other.seamlessness; |
| 105 | } |
| 106 | |
| 107 | FrameRateVote() = default; |
| 108 | |
| 109 | FrameRateVote(Fps rate, FrameRateCompatibility type, |
| 110 | Seamlessness seamlessness = Seamlessness::OnlySeamless) |
| 111 | : rate(rate), type(type), seamlessness(getSeamlessness(rate, seamlessness)) {} |
| 112 | } vote; |
| 113 | |
| 114 | FrameRateCategory category = FrameRateCategory::Default; |
Rachel Lee | 67afbea | 2023-09-28 15:35:07 -0700 | [diff] [blame] | 115 | bool categorySmoothSwitchOnly = false; |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 116 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 117 | FrameRate() = default; |
| 118 | |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 119 | FrameRate(Fps rate, FrameRateCompatibility type, |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 120 | Seamlessness seamlessness = Seamlessness::OnlySeamless, |
| 121 | FrameRateCategory category = FrameRateCategory::Default) |
| 122 | : vote(FrameRateVote(rate, type, seamlessness)), category(category) {} |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 123 | |
| 124 | bool operator==(const FrameRate& other) const { |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 125 | return vote == other.vote && category == other.category; |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | bool operator!=(const FrameRate& other) const { return !(*this == other); } |
| 129 | |
| 130 | // Convert an ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_* value to a |
| 131 | // Layer::FrameRateCompatibility. Logs fatal if the compatibility value is invalid. |
| 132 | static FrameRateCompatibility convertCompatibility(int8_t compatibility); |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 133 | |
| 134 | // Convert an ANATIVEWINDOW_CHANGE_FRAME_RATE_* value to a scheduler::Seamlessness. |
Rachel Lee | 58cc90d | 2023-09-05 18:50:20 -0700 | [diff] [blame] | 135 | // Logs fatal if the strategy value is invalid. |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 136 | static scheduler::Seamlessness convertChangeFrameRateStrategy(int8_t strategy); |
| 137 | |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 138 | // Convert an ANATIVEWINDOW_FRAME_RATE_CATEGORY_* value to a FrameRateCategory. |
Rachel Lee | 58cc90d | 2023-09-05 18:50:20 -0700 | [diff] [blame] | 139 | // Logs fatal if the category value is invalid. |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 140 | static FrameRateCategory convertCategory(int8_t category); |
| 141 | |
| 142 | // True if the FrameRate has explicit frame rate specifications. |
| 143 | bool isValid() const; |
| 144 | |
| 145 | // Returns true if the FrameRate explicitly instructs to not contribute to frame rate |
| 146 | // selection. |
| 147 | bool isNoVote() const; |
| 148 | |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 149 | private: |
| 150 | static Seamlessness getSeamlessness(Fps rate, Seamlessness seamlessness) { |
| 151 | if (!rate.isValid()) { |
| 152 | // Refresh rate of 0 is a special value which should reset the vote to |
| 153 | // its default value. |
| 154 | return Seamlessness::Default; |
| 155 | } |
| 156 | return seamlessness; |
| 157 | } |
| 158 | }; |
| 159 | |
Rachel Lee | 58cc90d | 2023-09-05 18:50:20 -0700 | [diff] [blame] | 160 | // Convert an ANATIVEWINDOW_FRAME_RATE_SELECTION_STRATEGY_* value to FrameRateSelectionStrategy. |
| 161 | // Logs fatal if the strategy value is invalid. |
| 162 | static FrameRateSelectionStrategy convertFrameRateSelectionStrategy(int8_t strategy); |
| 163 | |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 164 | static void setTraceEnabled(bool enabled) { sTraceEnabled = enabled; } |
| 165 | |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 166 | LayerInfo(const std::string& name, uid_t ownerUid, LayerHistory::LayerVoteType defaultVote); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 167 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 168 | LayerInfo(const LayerInfo&) = delete; |
| 169 | LayerInfo& operator=(const LayerInfo&) = delete; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 170 | |
| 171 | // Records the last requested present time. It also stores information about when |
| 172 | // the layer was last updated. If the present time is farther in the future than the |
| 173 | // updated time, the updated time is the present time. |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 174 | void setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, LayerUpdateType updateType, |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 175 | bool pendingModeChange, const LayerProps& props); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 176 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 177 | // Sets an explicit layer vote. This usually comes directly from the application via |
| 178 | // ANativeWindow_setFrameRate API |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 179 | void setLayerVote(LayerVote vote) { mLayerVote = vote; } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 180 | |
| 181 | // Sets the default layer vote. This will be the layer vote after calling to resetLayerVote(). |
| 182 | // This is used for layers that called to setLayerVote() and then removed the vote, so that the |
| 183 | // layer can go back to whatever vote it had before the app voted for it. |
| 184 | void setDefaultLayerVote(LayerHistory::LayerVoteType type) { mDefaultVote = type; } |
| 185 | |
Vishnu Nair | 41376b6 | 2023-11-08 05:08:58 -0800 | [diff] [blame] | 186 | void setProperties(const LayerProps&); |
| 187 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 188 | // Resets the layer vote to its default. |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 189 | void resetLayerVote() { |
| 190 | mLayerVote = {mDefaultVote, Fps(), Seamlessness::Default, FrameRateCategory::Default}; |
| 191 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 192 | |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 193 | std::string getName() const { return mName; } |
| 194 | |
| 195 | uid_t getOwnerUid() const { return mOwnerUid; } |
| 196 | |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 197 | RefreshRateVotes getRefreshRateVote(const RefreshRateSelector&, nsecs_t now); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 198 | |
| 199 | // Return the last updated time. If the present time is farther in the future than the |
| 200 | // updated time, the updated time is the present time. |
| 201 | nsecs_t getLastUpdatedTime() const { return mLastUpdatedTime; } |
| 202 | |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 203 | FrameRate getSetFrameRateVote() const; |
| 204 | bool isVisible() const; |
| 205 | int32_t getFrameRateSelectionPriority() const; |
Alec Mouri | 89f5d4e | 2023-10-20 17:12:49 +0000 | [diff] [blame] | 206 | bool isFrontBuffered() const; |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 207 | FloatRect getBounds() const; |
| 208 | ui::Transform getTransform() const; |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 209 | |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 210 | // Returns a C string for tracing a vote |
| 211 | const char* getTraceTag(LayerHistory::LayerVoteType type) const; |
| 212 | |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 213 | // Return the framerate of this layer. |
| 214 | Fps getFps(nsecs_t now) const; |
| 215 | |
Ady Abraham | 983e568 | 2020-05-28 16:49:18 -0700 | [diff] [blame] | 216 | void onLayerInactive(nsecs_t now) { |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 217 | // Mark mFrameTimeValidSince to now to ignore all previous frame times. |
| 218 | // We are not deleting the old frame to keep track of whether we should treat the first |
| 219 | // buffer as Max as we don't know anything about this layer or Min as this layer is |
| 220 | // posting infrequent updates. |
Ady Abraham | 983e568 | 2020-05-28 16:49:18 -0700 | [diff] [blame] | 221 | const auto timePoint = std::chrono::nanoseconds(now); |
| 222 | mFrameTimeValidSince = std::chrono::time_point<std::chrono::steady_clock>(timePoint); |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 223 | mLastRefreshRate = {}; |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 224 | mRefreshRateHistory.clear(); |
ramindani | 63db24e | 2023-04-03 10:56:05 -0700 | [diff] [blame] | 225 | mIsFrequencyConclusive = true; |
Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 226 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 227 | |
Ady Abraham | 983e568 | 2020-05-28 16:49:18 -0700 | [diff] [blame] | 228 | void clearHistory(nsecs_t now) { |
| 229 | onLayerInactive(now); |
| 230 | mFrameTimes.clear(); |
| 231 | } |
| 232 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 233 | private: |
Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 234 | // Used to store the layer timestamps |
| 235 | struct FrameTimeData { |
Marin Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame] | 236 | nsecs_t presentTime; // desiredPresentTime, if provided |
Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 237 | nsecs_t queueTime; // buffer queue time |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 238 | bool pendingModeChange; |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 239 | bool isSmallDirty; |
Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 240 | }; |
| 241 | |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 242 | // Holds information about the calculated and reported refresh rate |
| 243 | struct RefreshRateHeuristicData { |
| 244 | // Rate calculated on the layer |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 245 | Fps calculated; |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 246 | // Last reported rate for LayerInfo::getRefreshRate() |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 247 | Fps reported; |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 248 | // Whether the last reported rate for LayerInfo::getRefreshRate() |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 249 | // was due to animation or infrequent updates |
Ady Abraham | 86ac5c5 | 2023-01-11 15:24:03 -0800 | [diff] [blame] | 250 | bool animating = false; |
| 251 | // Whether the last reported rate for LayerInfo::getRefreshRate() |
| 252 | // was due to infrequent updates |
| 253 | bool infrequent = false; |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 254 | }; |
| 255 | |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 256 | // Class to store past calculated refresh rate and determine whether |
| 257 | // the refresh rate calculated is consistent with past values |
| 258 | class RefreshRateHistory { |
| 259 | public: |
| 260 | static constexpr auto HISTORY_SIZE = 90; |
| 261 | static constexpr std::chrono::nanoseconds HISTORY_DURATION = 2s; |
| 262 | |
| 263 | RefreshRateHistory(const std::string& name) : mName(name) {} |
| 264 | |
| 265 | // Clears History |
| 266 | void clear(); |
| 267 | |
| 268 | // Adds a new refresh rate and returns true if it is consistent |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 269 | bool add(Fps refreshRate, nsecs_t now); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 270 | |
| 271 | private: |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 272 | friend class LayerHistoryTest; |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 273 | friend class LayerHistoryIntegrationTest; |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 274 | |
| 275 | // Holds the refresh rate when it was calculated |
| 276 | struct RefreshRateData { |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 277 | Fps refreshRate; |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 278 | nsecs_t timestamp = 0; |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 279 | }; |
| 280 | |
| 281 | // Holds tracing strings |
| 282 | struct HeuristicTraceTagData { |
| 283 | std::string min; |
| 284 | std::string max; |
| 285 | std::string consistent; |
| 286 | std::string average; |
| 287 | }; |
| 288 | |
| 289 | bool isConsistent() const; |
| 290 | HeuristicTraceTagData makeHeuristicTraceTagData() const; |
| 291 | |
| 292 | const std::string mName; |
| 293 | mutable std::optional<HeuristicTraceTagData> mHeuristicTraceTagData; |
| 294 | std::deque<RefreshRateData> mRefreshRates; |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 295 | static constexpr float MARGIN_CONSISTENT_FPS = 1.0; |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 296 | }; |
| 297 | |
ramindani | 63db24e | 2023-04-03 10:56:05 -0700 | [diff] [blame] | 298 | // Represents whether we were able to determine either layer is frequent or infrequent |
| 299 | bool mIsFrequencyConclusive = true; |
| 300 | struct Frequent { |
| 301 | bool isFrequent; |
| 302 | bool clearHistory; |
| 303 | // Represents whether we were able to determine isFrequent conclusively |
| 304 | bool isConclusive; |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 305 | // Represents whether the latest frames are small dirty. |
| 306 | bool isSmallDirty = false; |
ramindani | 63db24e | 2023-04-03 10:56:05 -0700 | [diff] [blame] | 307 | }; |
| 308 | Frequent isFrequent(nsecs_t now) const; |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 309 | bool isAnimating(nsecs_t now) const; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 310 | bool hasEnoughDataForHeuristic() const; |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 311 | std::optional<Fps> calculateRefreshRateIfPossible(const RefreshRateSelector&, nsecs_t now); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 312 | std::optional<nsecs_t> calculateAverageFrameTime() const; |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 313 | bool isFrameTimeValid(const FrameTimeData&) const; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 314 | |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 315 | const std::string mName; |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 316 | const uid_t mOwnerUid; |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 317 | |
Marin Shalamanov | 4ad8b30 | 2020-12-11 15:50:08 +0100 | [diff] [blame] | 318 | // Used for sanitizing the heuristic data. If two frames are less than |
| 319 | // this period apart from each other they'll be considered as duplicates. |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 320 | static constexpr nsecs_t kMinPeriodBetweenFrames = (240_Hz).getPeriodNsecs(); |
Marin Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame] | 321 | // Used for sanitizing the heuristic data. If two frames are more than |
| 322 | // this period apart from each other, the interval between them won't be |
| 323 | // taken into account when calculating average frame rate. |
| 324 | static constexpr nsecs_t kMaxPeriodBetweenFrames = kMinFpsForFrequentLayer.getPeriodNsecs(); |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 325 | // Used for sanitizing the heuristic data. If frames are small dirty updating and are less |
| 326 | // than this period apart from each other, the interval between them won't be |
| 327 | // taken into account when calculating average frame rate. |
| 328 | static constexpr nsecs_t kMinPeriodBetweenSmallDirtyFrames = (60_Hz).getPeriodNsecs(); |
| 329 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 330 | LayerHistory::LayerVoteType mDefaultVote; |
| 331 | |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 332 | LayerVote mLayerVote; |
| 333 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 334 | nsecs_t mLastUpdatedTime = 0; |
| 335 | |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 336 | nsecs_t mLastAnimationTime = 0; |
| 337 | |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 338 | RefreshRateHeuristicData mLastRefreshRate; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 339 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 340 | std::deque<FrameTimeData> mFrameTimes; |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 341 | std::chrono::time_point<std::chrono::steady_clock> mFrameTimeValidSince = |
| 342 | std::chrono::steady_clock::now(); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 343 | static constexpr size_t HISTORY_SIZE = RefreshRateHistory::HISTORY_SIZE; |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 344 | static constexpr std::chrono::nanoseconds HISTORY_DURATION = LayerHistory::kMaxPeriodForHistory; |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 345 | |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 346 | std::unique_ptr<LayerProps> mLayerProps; |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 347 | |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 348 | RefreshRateHistory mRefreshRateHistory; |
| 349 | |
Arthur Hung | 8144e02 | 2023-09-08 10:26:23 +0000 | [diff] [blame] | 350 | // This will be accessed from only one thread when counting a layer is frequent or infrequent, |
| 351 | // and to determine whether a layer is in small dirty updating. |
| 352 | mutable int32_t mLastSmallDirtyCount = 0; |
| 353 | |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 354 | mutable std::unordered_map<LayerHistory::LayerVoteType, std::string> mTraceTags; |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 355 | |
| 356 | // Shared for all LayerInfo instances |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 357 | static bool sTraceEnabled; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 358 | }; |
| 359 | |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 360 | struct LayerProps { |
| 361 | bool visible = false; |
| 362 | FloatRect bounds; |
| 363 | ui::Transform transform; |
| 364 | LayerInfo::FrameRate setFrameRateVote; |
| 365 | int32_t frameRateSelectionPriority = -1; |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 366 | bool isSmallDirty = false; |
Alec Mouri | 89f5d4e | 2023-10-20 17:12:49 +0000 | [diff] [blame] | 367 | bool isFrontBuffered = false; |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 368 | }; |
| 369 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 370 | } // namespace scheduler |
| 371 | } // namespace android |