xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/Scheduler/LayerInfo.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright 2020 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #pragma once
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #include <chrono>
20*38e8c45fSAndroid Build Coastguard Worker #include <deque>
21*38e8c45fSAndroid Build Coastguard Worker #include <optional>
22*38e8c45fSAndroid Build Coastguard Worker #include <string>
23*38e8c45fSAndroid Build Coastguard Worker #include <unordered_map>
24*38e8c45fSAndroid Build Coastguard Worker 
25*38e8c45fSAndroid Build Coastguard Worker #include <ui/Transform.h>
26*38e8c45fSAndroid Build Coastguard Worker #include <utils/Timers.h>
27*38e8c45fSAndroid Build Coastguard Worker 
28*38e8c45fSAndroid Build Coastguard Worker #include <scheduler/Fps.h>
29*38e8c45fSAndroid Build Coastguard Worker #include <scheduler/Seamlessness.h>
30*38e8c45fSAndroid Build Coastguard Worker 
31*38e8c45fSAndroid Build Coastguard Worker #include "FrameRateCompatibility.h"
32*38e8c45fSAndroid Build Coastguard Worker #include "LayerHistory.h"
33*38e8c45fSAndroid Build Coastguard Worker #include "RefreshRateSelector.h"
34*38e8c45fSAndroid Build Coastguard Worker 
35*38e8c45fSAndroid Build Coastguard Worker namespace android {
36*38e8c45fSAndroid Build Coastguard Worker 
37*38e8c45fSAndroid Build Coastguard Worker class Layer;
38*38e8c45fSAndroid Build Coastguard Worker 
39*38e8c45fSAndroid Build Coastguard Worker namespace scheduler {
40*38e8c45fSAndroid Build Coastguard Worker 
41*38e8c45fSAndroid Build Coastguard Worker using namespace std::chrono_literals;
42*38e8c45fSAndroid Build Coastguard Worker struct LayerProps;
43*38e8c45fSAndroid Build Coastguard Worker // Maximum period between presents for a layer to be considered active.
44*38e8c45fSAndroid Build Coastguard Worker constexpr std::chrono::nanoseconds MAX_ACTIVE_LAYER_PERIOD_NS = 1200ms;
45*38e8c45fSAndroid Build Coastguard Worker 
46*38e8c45fSAndroid Build Coastguard Worker // Earliest present time for a layer to be considered active.
getActiveLayerThreshold(nsecs_t now)47*38e8c45fSAndroid Build Coastguard Worker constexpr nsecs_t getActiveLayerThreshold(nsecs_t now) {
48*38e8c45fSAndroid Build Coastguard Worker     return now - MAX_ACTIVE_LAYER_PERIOD_NS.count();
49*38e8c45fSAndroid Build Coastguard Worker }
50*38e8c45fSAndroid Build Coastguard Worker 
51*38e8c45fSAndroid Build Coastguard Worker // Stores history of present times and refresh rates for a layer.
52*38e8c45fSAndroid Build Coastguard Worker class LayerInfo {
53*38e8c45fSAndroid Build Coastguard Worker     using LayerUpdateType = LayerHistory::LayerUpdateType;
54*38e8c45fSAndroid Build Coastguard Worker 
55*38e8c45fSAndroid Build Coastguard Worker     // Layer is considered frequent if the earliest value in the window of most recent present times
56*38e8c45fSAndroid Build Coastguard Worker     // is within a threshold. If a layer is infrequent, its average refresh rate is disregarded in
57*38e8c45fSAndroid Build Coastguard Worker     // favor of a low refresh rate.
58*38e8c45fSAndroid Build Coastguard Worker     static constexpr size_t kFrequentLayerWindowSize = 4;
59*38e8c45fSAndroid Build Coastguard Worker     static constexpr Fps kMinFpsForFrequentLayer = 10_Hz;
60*38e8c45fSAndroid Build Coastguard Worker     static constexpr auto kMaxPeriodForFrequentLayerNs =
61*38e8c45fSAndroid Build Coastguard Worker             std::chrono::nanoseconds(kMinFpsForFrequentLayer.getPeriodNsecs()) + 1ms;
62*38e8c45fSAndroid Build Coastguard Worker     static constexpr size_t kNumSmallDirtyThreshold = 2;
63*38e8c45fSAndroid Build Coastguard Worker 
64*38e8c45fSAndroid Build Coastguard Worker     friend class LayerHistoryTest;
65*38e8c45fSAndroid Build Coastguard Worker     friend class LayerHistoryIntegrationTest;
66*38e8c45fSAndroid Build Coastguard Worker     friend class LayerInfoTest;
67*38e8c45fSAndroid Build Coastguard Worker 
68*38e8c45fSAndroid Build Coastguard Worker public:
69*38e8c45fSAndroid Build Coastguard Worker     // Holds information about the layer vote
70*38e8c45fSAndroid Build Coastguard Worker     struct LayerVote {
71*38e8c45fSAndroid Build Coastguard Worker         LayerHistory::LayerVoteType type = LayerHistory::LayerVoteType::Heuristic;
72*38e8c45fSAndroid Build Coastguard Worker         Fps fps;
73*38e8c45fSAndroid Build Coastguard Worker         Seamlessness seamlessness = Seamlessness::Default;
74*38e8c45fSAndroid Build Coastguard Worker         FrameRateCategory category = FrameRateCategory::Default;
75*38e8c45fSAndroid Build Coastguard Worker         bool categorySmoothSwitchOnly = false;
76*38e8c45fSAndroid Build Coastguard Worker 
77*38e8c45fSAndroid Build Coastguard Worker         // Returns true if the layer explicitly should contribute to frame rate scoring.
isNoVoteLayerVote78*38e8c45fSAndroid Build Coastguard Worker         bool isNoVote() const { return RefreshRateSelector::isNoVote(type); }
79*38e8c45fSAndroid Build Coastguard Worker     };
80*38e8c45fSAndroid Build Coastguard Worker 
81*38e8c45fSAndroid Build Coastguard Worker     using RefreshRateVotes = ftl::SmallVector<LayerInfo::LayerVote, 2>;
82*38e8c45fSAndroid Build Coastguard Worker 
83*38e8c45fSAndroid Build Coastguard Worker     enum class FrameRateSelectionStrategy {
84*38e8c45fSAndroid Build Coastguard Worker         Propagate,
85*38e8c45fSAndroid Build Coastguard Worker         OverrideChildren,
86*38e8c45fSAndroid Build Coastguard Worker         Self,
87*38e8c45fSAndroid Build Coastguard Worker 
88*38e8c45fSAndroid Build Coastguard Worker         ftl_last = Self
89*38e8c45fSAndroid Build Coastguard Worker     };
90*38e8c45fSAndroid Build Coastguard Worker 
91*38e8c45fSAndroid Build Coastguard Worker     // Encapsulates the frame rate specifications of the layer. This information will be used
92*38e8c45fSAndroid Build Coastguard Worker     // when the display refresh rate is determined.
93*38e8c45fSAndroid Build Coastguard Worker     struct FrameRate {
94*38e8c45fSAndroid Build Coastguard Worker         using Seamlessness = scheduler::Seamlessness;
95*38e8c45fSAndroid Build Coastguard Worker 
96*38e8c45fSAndroid Build Coastguard Worker         // Information related to a specific desired frame rate vote.
97*38e8c45fSAndroid Build Coastguard Worker         struct FrameRateVote {
98*38e8c45fSAndroid Build Coastguard Worker             Fps rate;
99*38e8c45fSAndroid Build Coastguard Worker             FrameRateCompatibility type = FrameRateCompatibility::Default;
100*38e8c45fSAndroid Build Coastguard Worker             Seamlessness seamlessness = Seamlessness::Default;
101*38e8c45fSAndroid Build Coastguard Worker 
102*38e8c45fSAndroid Build Coastguard Worker             bool operator==(const FrameRateVote& other) const {
103*38e8c45fSAndroid Build Coastguard Worker                 return isApproxEqual(rate, other.rate) && type == other.type &&
104*38e8c45fSAndroid Build Coastguard Worker                         seamlessness == other.seamlessness;
105*38e8c45fSAndroid Build Coastguard Worker             }
106*38e8c45fSAndroid Build Coastguard Worker 
107*38e8c45fSAndroid Build Coastguard Worker             FrameRateVote() = default;
108*38e8c45fSAndroid Build Coastguard Worker 
109*38e8c45fSAndroid Build Coastguard Worker             FrameRateVote(Fps rate, FrameRateCompatibility type,
110*38e8c45fSAndroid Build Coastguard Worker                           Seamlessness seamlessness = Seamlessness::OnlySeamless)
rateFrameRate::FrameRateVote111*38e8c45fSAndroid Build Coastguard Worker                   : rate(rate), type(type), seamlessness(getSeamlessness(rate, seamlessness)) {}
112*38e8c45fSAndroid Build Coastguard Worker         } vote;
113*38e8c45fSAndroid Build Coastguard Worker 
114*38e8c45fSAndroid Build Coastguard Worker         FrameRateCategory category = FrameRateCategory::Default;
115*38e8c45fSAndroid Build Coastguard Worker         bool categorySmoothSwitchOnly = false;
116*38e8c45fSAndroid Build Coastguard Worker 
117*38e8c45fSAndroid Build Coastguard Worker         FrameRate() = default;
118*38e8c45fSAndroid Build Coastguard Worker 
119*38e8c45fSAndroid Build Coastguard Worker         FrameRate(Fps rate, FrameRateCompatibility type,
120*38e8c45fSAndroid Build Coastguard Worker                   Seamlessness seamlessness = Seamlessness::OnlySeamless,
121*38e8c45fSAndroid Build Coastguard Worker                   FrameRateCategory category = FrameRateCategory::Default)
voteFrameRate122*38e8c45fSAndroid Build Coastguard Worker               : vote(FrameRateVote(rate, type, seamlessness)), category(category) {}
123*38e8c45fSAndroid Build Coastguard Worker 
124*38e8c45fSAndroid Build Coastguard Worker         bool operator==(const FrameRate& other) const {
125*38e8c45fSAndroid Build Coastguard Worker             return vote == other.vote && category == other.category;
126*38e8c45fSAndroid Build Coastguard Worker         }
127*38e8c45fSAndroid Build Coastguard Worker 
128*38e8c45fSAndroid Build Coastguard Worker         bool operator!=(const FrameRate& other) const { return !(*this == other); }
129*38e8c45fSAndroid Build Coastguard Worker 
130*38e8c45fSAndroid Build Coastguard Worker         // Convert an ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_* value to a
131*38e8c45fSAndroid Build Coastguard Worker         // Layer::FrameRateCompatibility. Logs fatal if the compatibility value is invalid.
132*38e8c45fSAndroid Build Coastguard Worker         static FrameRateCompatibility convertCompatibility(int8_t compatibility);
133*38e8c45fSAndroid Build Coastguard Worker 
134*38e8c45fSAndroid Build Coastguard Worker         // Convert an ANATIVEWINDOW_CHANGE_FRAME_RATE_* value to a scheduler::Seamlessness.
135*38e8c45fSAndroid Build Coastguard Worker         // Logs fatal if the strategy value is invalid.
136*38e8c45fSAndroid Build Coastguard Worker         static scheduler::Seamlessness convertChangeFrameRateStrategy(int8_t strategy);
137*38e8c45fSAndroid Build Coastguard Worker 
138*38e8c45fSAndroid Build Coastguard Worker         // Convert an ANATIVEWINDOW_FRAME_RATE_CATEGORY_* value to a FrameRateCategory.
139*38e8c45fSAndroid Build Coastguard Worker         // Logs fatal if the category value is invalid.
140*38e8c45fSAndroid Build Coastguard Worker         static FrameRateCategory convertCategory(int8_t category);
141*38e8c45fSAndroid Build Coastguard Worker 
142*38e8c45fSAndroid Build Coastguard Worker         // True if the FrameRate has explicit frame rate specifications.
143*38e8c45fSAndroid Build Coastguard Worker         bool isValid() const;
144*38e8c45fSAndroid Build Coastguard Worker 
145*38e8c45fSAndroid Build Coastguard Worker         // Returns true if the FrameRate explicitly instructs to not contribute to frame rate
146*38e8c45fSAndroid Build Coastguard Worker         // selection.
147*38e8c45fSAndroid Build Coastguard Worker         bool isNoVote() const;
148*38e8c45fSAndroid Build Coastguard Worker 
149*38e8c45fSAndroid Build Coastguard Worker         // Returns true if the FrameRate has a valid valueless (0 Hz) frame rate type.
150*38e8c45fSAndroid Build Coastguard Worker         bool isValuelessType() const;
151*38e8c45fSAndroid Build Coastguard Worker 
152*38e8c45fSAndroid Build Coastguard Worker         // Checks whether the given FrameRate's vote specifications is valid for MRR devices
153*38e8c45fSAndroid Build Coastguard Worker         // given the current flagging.
154*38e8c45fSAndroid Build Coastguard Worker         bool isVoteValidForMrr(bool isVrrDevice) const;
155*38e8c45fSAndroid Build Coastguard Worker 
156*38e8c45fSAndroid Build Coastguard Worker     private:
getSeamlessnessFrameRate157*38e8c45fSAndroid Build Coastguard Worker         static Seamlessness getSeamlessness(Fps rate, Seamlessness seamlessness) {
158*38e8c45fSAndroid Build Coastguard Worker             if (!rate.isValid()) {
159*38e8c45fSAndroid Build Coastguard Worker                 // Refresh rate of 0 is a special value which should reset the vote to
160*38e8c45fSAndroid Build Coastguard Worker                 // its default value.
161*38e8c45fSAndroid Build Coastguard Worker                 return Seamlessness::Default;
162*38e8c45fSAndroid Build Coastguard Worker             }
163*38e8c45fSAndroid Build Coastguard Worker             return seamlessness;
164*38e8c45fSAndroid Build Coastguard Worker         }
165*38e8c45fSAndroid Build Coastguard Worker     };
166*38e8c45fSAndroid Build Coastguard Worker 
167*38e8c45fSAndroid Build Coastguard Worker     // Convert an ANATIVEWINDOW_FRAME_RATE_SELECTION_STRATEGY_* value to FrameRateSelectionStrategy.
168*38e8c45fSAndroid Build Coastguard Worker     // Logs fatal if the strategy value is invalid.
169*38e8c45fSAndroid Build Coastguard Worker     static FrameRateSelectionStrategy convertFrameRateSelectionStrategy(int8_t strategy);
170*38e8c45fSAndroid Build Coastguard Worker 
setTraceEnabled(bool enabled)171*38e8c45fSAndroid Build Coastguard Worker     static void setTraceEnabled(bool enabled) { sTraceEnabled = enabled; }
172*38e8c45fSAndroid Build Coastguard Worker 
173*38e8c45fSAndroid Build Coastguard Worker     LayerInfo(const std::string& name, uid_t ownerUid, LayerHistory::LayerVoteType defaultVote);
174*38e8c45fSAndroid Build Coastguard Worker 
175*38e8c45fSAndroid Build Coastguard Worker     LayerInfo(const LayerInfo&) = delete;
176*38e8c45fSAndroid Build Coastguard Worker     LayerInfo& operator=(const LayerInfo&) = delete;
177*38e8c45fSAndroid Build Coastguard Worker 
178*38e8c45fSAndroid Build Coastguard Worker     // Records the last requested present time. It also stores information about when
179*38e8c45fSAndroid Build Coastguard Worker     // the layer was last updated. If the present time is farther in the future than the
180*38e8c45fSAndroid Build Coastguard Worker     // updated time, the updated time is the present time.
181*38e8c45fSAndroid Build Coastguard Worker     void setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, LayerUpdateType updateType,
182*38e8c45fSAndroid Build Coastguard Worker                             bool pendingModeChange, const LayerProps& props);
183*38e8c45fSAndroid Build Coastguard Worker 
184*38e8c45fSAndroid Build Coastguard Worker     // Sets an explicit layer vote. This usually comes directly from the application via
185*38e8c45fSAndroid Build Coastguard Worker     // ANativeWindow_setFrameRate API. This is also used by Game Default Frame Rate and
186*38e8c45fSAndroid Build Coastguard Worker     // Game Mode Intervention Frame Rate.
setLayerVote(LayerVote vote)187*38e8c45fSAndroid Build Coastguard Worker     void setLayerVote(LayerVote vote) { mLayerVote = vote; }
188*38e8c45fSAndroid Build Coastguard Worker 
189*38e8c45fSAndroid Build Coastguard Worker     // Sets the default layer vote. This will be the layer vote after calling to resetLayerVote().
190*38e8c45fSAndroid Build Coastguard Worker     // This is used for layers that called to setLayerVote() and then removed the vote, so that the
191*38e8c45fSAndroid Build Coastguard Worker     // layer can go back to whatever vote it had before the app voted for it.
setDefaultLayerVote(LayerHistory::LayerVoteType type)192*38e8c45fSAndroid Build Coastguard Worker     void setDefaultLayerVote(LayerHistory::LayerVoteType type) { mDefaultVote = type; }
193*38e8c45fSAndroid Build Coastguard Worker 
194*38e8c45fSAndroid Build Coastguard Worker     void setProperties(const LayerProps&);
195*38e8c45fSAndroid Build Coastguard Worker 
196*38e8c45fSAndroid Build Coastguard Worker     // Resets the layer vote to its default.
resetLayerVote()197*38e8c45fSAndroid Build Coastguard Worker     void resetLayerVote() {
198*38e8c45fSAndroid Build Coastguard Worker         mLayerVote = {mDefaultVote, Fps(), Seamlessness::Default, FrameRateCategory::Default};
199*38e8c45fSAndroid Build Coastguard Worker     }
200*38e8c45fSAndroid Build Coastguard Worker 
getName()201*38e8c45fSAndroid Build Coastguard Worker     std::string getName() const { return mName; }
202*38e8c45fSAndroid Build Coastguard Worker 
getOwnerUid()203*38e8c45fSAndroid Build Coastguard Worker     uid_t getOwnerUid() const { return mOwnerUid; }
204*38e8c45fSAndroid Build Coastguard Worker 
205*38e8c45fSAndroid Build Coastguard Worker     RefreshRateVotes getRefreshRateVote(const RefreshRateSelector&, nsecs_t now);
206*38e8c45fSAndroid Build Coastguard Worker 
207*38e8c45fSAndroid Build Coastguard Worker     // Return the last updated time. If the present time is farther in the future than the
208*38e8c45fSAndroid Build Coastguard Worker     // updated time, the updated time is the present time.
getLastUpdatedTime()209*38e8c45fSAndroid Build Coastguard Worker     nsecs_t getLastUpdatedTime() const { return mLastUpdatedTime; }
210*38e8c45fSAndroid Build Coastguard Worker 
211*38e8c45fSAndroid Build Coastguard Worker     FrameRate getSetFrameRateVote() const;
212*38e8c45fSAndroid Build Coastguard Worker     bool isVisible() const;
213*38e8c45fSAndroid Build Coastguard Worker     int32_t getFrameRateSelectionPriority() const;
214*38e8c45fSAndroid Build Coastguard Worker     bool isFrontBuffered() const;
215*38e8c45fSAndroid Build Coastguard Worker     FloatRect getBounds() const;
216*38e8c45fSAndroid Build Coastguard Worker     ui::Transform getTransform() const;
217*38e8c45fSAndroid Build Coastguard Worker 
218*38e8c45fSAndroid Build Coastguard Worker     // Returns a C string for tracing a vote
219*38e8c45fSAndroid Build Coastguard Worker     const char* getTraceTag(LayerHistory::LayerVoteType type) const;
220*38e8c45fSAndroid Build Coastguard Worker 
221*38e8c45fSAndroid Build Coastguard Worker     // Return the framerate of this layer.
222*38e8c45fSAndroid Build Coastguard Worker     Fps getFps(nsecs_t now) const;
223*38e8c45fSAndroid Build Coastguard Worker 
onLayerInactive(nsecs_t now)224*38e8c45fSAndroid Build Coastguard Worker     void onLayerInactive(nsecs_t now) {
225*38e8c45fSAndroid Build Coastguard Worker         // Mark mFrameTimeValidSince to now to ignore all previous frame times.
226*38e8c45fSAndroid Build Coastguard Worker         // We are not deleting the old frame to keep track of whether we should treat the first
227*38e8c45fSAndroid Build Coastguard Worker         // buffer as Max as we don't know anything about this layer or Min as this layer is
228*38e8c45fSAndroid Build Coastguard Worker         // posting infrequent updates.
229*38e8c45fSAndroid Build Coastguard Worker         const auto timePoint = std::chrono::nanoseconds(now);
230*38e8c45fSAndroid Build Coastguard Worker         mFrameTimeValidSince = std::chrono::time_point<std::chrono::steady_clock>(timePoint);
231*38e8c45fSAndroid Build Coastguard Worker         mLastRefreshRate = {};
232*38e8c45fSAndroid Build Coastguard Worker         mRefreshRateHistory.clear();
233*38e8c45fSAndroid Build Coastguard Worker         mIsFrequencyConclusive = true;
234*38e8c45fSAndroid Build Coastguard Worker     }
235*38e8c45fSAndroid Build Coastguard Worker 
clearHistory(nsecs_t now)236*38e8c45fSAndroid Build Coastguard Worker     void clearHistory(nsecs_t now) {
237*38e8c45fSAndroid Build Coastguard Worker         onLayerInactive(now);
238*38e8c45fSAndroid Build Coastguard Worker         mFrameTimes.clear();
239*38e8c45fSAndroid Build Coastguard Worker     }
240*38e8c45fSAndroid Build Coastguard Worker 
241*38e8c45fSAndroid Build Coastguard Worker private:
242*38e8c45fSAndroid Build Coastguard Worker     // Used to store the layer timestamps
243*38e8c45fSAndroid Build Coastguard Worker     struct FrameTimeData {
244*38e8c45fSAndroid Build Coastguard Worker         nsecs_t presentTime; // desiredPresentTime, if provided
245*38e8c45fSAndroid Build Coastguard Worker         nsecs_t queueTime;  // buffer queue time
246*38e8c45fSAndroid Build Coastguard Worker         bool pendingModeChange;
247*38e8c45fSAndroid Build Coastguard Worker         bool isSmallDirty;
248*38e8c45fSAndroid Build Coastguard Worker     };
249*38e8c45fSAndroid Build Coastguard Worker 
250*38e8c45fSAndroid Build Coastguard Worker     // Holds information about the calculated and reported refresh rate
251*38e8c45fSAndroid Build Coastguard Worker     struct RefreshRateHeuristicData {
252*38e8c45fSAndroid Build Coastguard Worker         // Rate calculated on the layer
253*38e8c45fSAndroid Build Coastguard Worker         Fps calculated;
254*38e8c45fSAndroid Build Coastguard Worker         // Last reported rate for LayerInfo::getRefreshRate()
255*38e8c45fSAndroid Build Coastguard Worker         Fps reported;
256*38e8c45fSAndroid Build Coastguard Worker         // Whether the last reported rate for LayerInfo::getRefreshRate()
257*38e8c45fSAndroid Build Coastguard Worker         // was due to animation or infrequent updates
258*38e8c45fSAndroid Build Coastguard Worker         bool animating = false;
259*38e8c45fSAndroid Build Coastguard Worker         // Whether the last reported rate for LayerInfo::getRefreshRate()
260*38e8c45fSAndroid Build Coastguard Worker         // was due to infrequent updates
261*38e8c45fSAndroid Build Coastguard Worker         bool infrequent = false;
262*38e8c45fSAndroid Build Coastguard Worker     };
263*38e8c45fSAndroid Build Coastguard Worker 
264*38e8c45fSAndroid Build Coastguard Worker     // Class to store past calculated refresh rate and determine whether
265*38e8c45fSAndroid Build Coastguard Worker     // the refresh rate calculated is consistent with past values
266*38e8c45fSAndroid Build Coastguard Worker     class RefreshRateHistory {
267*38e8c45fSAndroid Build Coastguard Worker     public:
268*38e8c45fSAndroid Build Coastguard Worker         static constexpr auto HISTORY_SIZE = 90;
269*38e8c45fSAndroid Build Coastguard Worker         static constexpr std::chrono::nanoseconds HISTORY_DURATION = 2s;
270*38e8c45fSAndroid Build Coastguard Worker 
RefreshRateHistory(const std::string & name)271*38e8c45fSAndroid Build Coastguard Worker         RefreshRateHistory(const std::string& name) : mName(name) {}
272*38e8c45fSAndroid Build Coastguard Worker 
273*38e8c45fSAndroid Build Coastguard Worker         // Clears History
274*38e8c45fSAndroid Build Coastguard Worker         void clear();
275*38e8c45fSAndroid Build Coastguard Worker 
276*38e8c45fSAndroid Build Coastguard Worker         // Adds a new refresh rate and returns valid refresh rate if it is consistent enough
277*38e8c45fSAndroid Build Coastguard Worker         Fps add(Fps refreshRate, nsecs_t now, const RefreshRateSelector&);
278*38e8c45fSAndroid Build Coastguard Worker 
279*38e8c45fSAndroid Build Coastguard Worker     private:
280*38e8c45fSAndroid Build Coastguard Worker         friend class LayerHistoryTest;
281*38e8c45fSAndroid Build Coastguard Worker         friend class LayerHistoryIntegrationTest;
282*38e8c45fSAndroid Build Coastguard Worker 
283*38e8c45fSAndroid Build Coastguard Worker         // Holds the refresh rate when it was calculated
284*38e8c45fSAndroid Build Coastguard Worker         struct RefreshRateData {
285*38e8c45fSAndroid Build Coastguard Worker             Fps refreshRate;
286*38e8c45fSAndroid Build Coastguard Worker             nsecs_t timestamp = 0;
287*38e8c45fSAndroid Build Coastguard Worker         };
288*38e8c45fSAndroid Build Coastguard Worker 
289*38e8c45fSAndroid Build Coastguard Worker         // Holds tracing strings
290*38e8c45fSAndroid Build Coastguard Worker         struct HeuristicTraceTagData {
291*38e8c45fSAndroid Build Coastguard Worker             std::string min;
292*38e8c45fSAndroid Build Coastguard Worker             std::string max;
293*38e8c45fSAndroid Build Coastguard Worker             std::string consistent;
294*38e8c45fSAndroid Build Coastguard Worker             std::string average;
295*38e8c45fSAndroid Build Coastguard Worker         };
296*38e8c45fSAndroid Build Coastguard Worker 
297*38e8c45fSAndroid Build Coastguard Worker         Fps selectRefreshRate(const RefreshRateSelector&) const;
298*38e8c45fSAndroid Build Coastguard Worker         HeuristicTraceTagData makeHeuristicTraceTagData() const;
299*38e8c45fSAndroid Build Coastguard Worker 
300*38e8c45fSAndroid Build Coastguard Worker         const std::string mName;
301*38e8c45fSAndroid Build Coastguard Worker         mutable std::optional<HeuristicTraceTagData> mHeuristicTraceTagData;
302*38e8c45fSAndroid Build Coastguard Worker         std::deque<RefreshRateData> mRefreshRates;
303*38e8c45fSAndroid Build Coastguard Worker         static constexpr float MARGIN_CONSISTENT_FPS = 1.0;
304*38e8c45fSAndroid Build Coastguard Worker         static constexpr float MARGIN_CONSISTENT_FPS_FOR_CLOSEST_REFRESH_RATE = 5.0;
305*38e8c45fSAndroid Build Coastguard Worker     };
306*38e8c45fSAndroid Build Coastguard Worker 
307*38e8c45fSAndroid Build Coastguard Worker     // Represents whether we were able to determine either layer is frequent or infrequent
308*38e8c45fSAndroid Build Coastguard Worker     bool mIsFrequencyConclusive = true;
309*38e8c45fSAndroid Build Coastguard Worker     struct Frequent {
310*38e8c45fSAndroid Build Coastguard Worker         bool isFrequent;
311*38e8c45fSAndroid Build Coastguard Worker         bool clearHistory;
312*38e8c45fSAndroid Build Coastguard Worker         // Represents whether we were able to determine isFrequent conclusively
313*38e8c45fSAndroid Build Coastguard Worker         bool isConclusive;
314*38e8c45fSAndroid Build Coastguard Worker         // Represents whether the latest frames are small dirty.
315*38e8c45fSAndroid Build Coastguard Worker         bool isSmallDirty = false;
316*38e8c45fSAndroid Build Coastguard Worker     };
317*38e8c45fSAndroid Build Coastguard Worker     Frequent isFrequent(nsecs_t now) const;
318*38e8c45fSAndroid Build Coastguard Worker     bool isAnimating(nsecs_t now) const;
319*38e8c45fSAndroid Build Coastguard Worker     bool hasEnoughDataForHeuristic() const;
320*38e8c45fSAndroid Build Coastguard Worker     std::optional<Fps> calculateRefreshRateIfPossible(const RefreshRateSelector&, nsecs_t now);
321*38e8c45fSAndroid Build Coastguard Worker     std::optional<nsecs_t> calculateAverageFrameTime() const;
322*38e8c45fSAndroid Build Coastguard Worker     bool isFrameTimeValid(const FrameTimeData&) const;
323*38e8c45fSAndroid Build Coastguard Worker 
324*38e8c45fSAndroid Build Coastguard Worker     const std::string mName;
325*38e8c45fSAndroid Build Coastguard Worker     const uid_t mOwnerUid;
326*38e8c45fSAndroid Build Coastguard Worker 
327*38e8c45fSAndroid Build Coastguard Worker     // Used for sanitizing the heuristic data. If two frames are less than
328*38e8c45fSAndroid Build Coastguard Worker     // this period apart from each other they'll be considered as duplicates.
329*38e8c45fSAndroid Build Coastguard Worker     static constexpr nsecs_t kMinPeriodBetweenFrames = (240_Hz).getPeriodNsecs();
330*38e8c45fSAndroid Build Coastguard Worker     // Used for sanitizing the heuristic data. If two frames are more than
331*38e8c45fSAndroid Build Coastguard Worker     // this period apart from each other, the interval between them won't be
332*38e8c45fSAndroid Build Coastguard Worker     // taken into account when calculating average frame rate.
333*38e8c45fSAndroid Build Coastguard Worker     static constexpr nsecs_t kMaxPeriodBetweenFrames = kMinFpsForFrequentLayer.getPeriodNsecs();
334*38e8c45fSAndroid Build Coastguard Worker     // Used for sanitizing the heuristic data. If frames are small dirty updating and are less
335*38e8c45fSAndroid Build Coastguard Worker     // than this period apart from each other, the interval between them won't be
336*38e8c45fSAndroid Build Coastguard Worker     // taken into account when calculating average frame rate.
337*38e8c45fSAndroid Build Coastguard Worker     static constexpr nsecs_t kMinPeriodBetweenSmallDirtyFrames = (60_Hz).getPeriodNsecs();
338*38e8c45fSAndroid Build Coastguard Worker 
339*38e8c45fSAndroid Build Coastguard Worker     LayerHistory::LayerVoteType mDefaultVote;
340*38e8c45fSAndroid Build Coastguard Worker 
341*38e8c45fSAndroid Build Coastguard Worker     LayerVote mLayerVote;
342*38e8c45fSAndroid Build Coastguard Worker 
343*38e8c45fSAndroid Build Coastguard Worker     nsecs_t mLastUpdatedTime = 0;
344*38e8c45fSAndroid Build Coastguard Worker 
345*38e8c45fSAndroid Build Coastguard Worker     nsecs_t mLastAnimationTime = 0;
346*38e8c45fSAndroid Build Coastguard Worker 
347*38e8c45fSAndroid Build Coastguard Worker     RefreshRateHeuristicData mLastRefreshRate;
348*38e8c45fSAndroid Build Coastguard Worker 
349*38e8c45fSAndroid Build Coastguard Worker     std::deque<FrameTimeData> mFrameTimes;
350*38e8c45fSAndroid Build Coastguard Worker     std::chrono::time_point<std::chrono::steady_clock> mFrameTimeValidSince =
351*38e8c45fSAndroid Build Coastguard Worker             std::chrono::steady_clock::now();
352*38e8c45fSAndroid Build Coastguard Worker     static constexpr size_t HISTORY_SIZE = RefreshRateHistory::HISTORY_SIZE;
353*38e8c45fSAndroid Build Coastguard Worker     static constexpr std::chrono::nanoseconds HISTORY_DURATION = LayerHistory::kMaxPeriodForHistory;
354*38e8c45fSAndroid Build Coastguard Worker 
355*38e8c45fSAndroid Build Coastguard Worker     std::unique_ptr<LayerProps> mLayerProps;
356*38e8c45fSAndroid Build Coastguard Worker 
357*38e8c45fSAndroid Build Coastguard Worker     RefreshRateHistory mRefreshRateHistory;
358*38e8c45fSAndroid Build Coastguard Worker 
359*38e8c45fSAndroid Build Coastguard Worker     // This will be accessed from only one thread when counting a layer is frequent or infrequent,
360*38e8c45fSAndroid Build Coastguard Worker     // and to determine whether a layer is in small dirty updating.
361*38e8c45fSAndroid Build Coastguard Worker     mutable int32_t mLastSmallDirtyCount = 0;
362*38e8c45fSAndroid Build Coastguard Worker 
363*38e8c45fSAndroid Build Coastguard Worker     mutable std::unordered_map<LayerHistory::LayerVoteType, std::string> mTraceTags;
364*38e8c45fSAndroid Build Coastguard Worker 
365*38e8c45fSAndroid Build Coastguard Worker     // Shared for all LayerInfo instances
366*38e8c45fSAndroid Build Coastguard Worker     static bool sTraceEnabled;
367*38e8c45fSAndroid Build Coastguard Worker };
368*38e8c45fSAndroid Build Coastguard Worker 
369*38e8c45fSAndroid Build Coastguard Worker struct LayerProps {
370*38e8c45fSAndroid Build Coastguard Worker     bool visible = false;
371*38e8c45fSAndroid Build Coastguard Worker     FloatRect bounds;
372*38e8c45fSAndroid Build Coastguard Worker     ui::Transform transform;
373*38e8c45fSAndroid Build Coastguard Worker     LayerInfo::FrameRate setFrameRateVote;
374*38e8c45fSAndroid Build Coastguard Worker     int32_t frameRateSelectionPriority = -1;
375*38e8c45fSAndroid Build Coastguard Worker     bool isSmallDirty = false;
376*38e8c45fSAndroid Build Coastguard Worker     bool isFrontBuffered = false;
377*38e8c45fSAndroid Build Coastguard Worker };
378*38e8c45fSAndroid Build Coastguard Worker 
379*38e8c45fSAndroid Build Coastguard Worker } // namespace scheduler
380*38e8c45fSAndroid Build Coastguard Worker } // namespace android
381