blob: 82da7e3dd73b3433fc7948c1773e83670dc3bae6 [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"
26#include "SchedulerUtils.h"
27
28namespace android {
29
30class Layer;
31
32namespace scheduler {
33
34using namespace std::chrono_literals;
35
36// Maximum period between presents for a layer to be considered active.
37constexpr std::chrono::nanoseconds MAX_ACTIVE_LAYER_PERIOD_NS = 1200ms;
38
39// Earliest present time for a layer to be considered active.
40constexpr 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.
45class LayerInfoV2 {
46 // Layer is considered frequent if the earliest value in the window of most recent present times
47 // is within a threshold. If a layer is infrequent, its average refresh rate is disregarded in
48 // favor of a low refresh rate.
49 static constexpr size_t FREQUENT_LAYER_WINDOW_SIZE = 3;
Ady Abrahameeb74562020-05-20 13:37:56 -070050 static constexpr std::chrono::nanoseconds MAX_FREQUENT_LAYER_PERIOD_NS = 150ms;
Ady Abraham8a82ba62020-01-17 12:43:17 -080051
52 friend class LayerHistoryTestV2;
53
54public:
Ady Abraham1adbb722020-05-15 11:51:48 -070055 LayerInfoV2(const std::string& name, nsecs_t highRefreshRatePeriod,
56 LayerHistory::LayerVoteType defaultVote);
Ady Abraham8a82ba62020-01-17 12:43:17 -080057
58 LayerInfoV2(const LayerInfo&) = delete;
59 LayerInfoV2& operator=(const LayerInfoV2&) = delete;
60
61 // Records the last requested present time. It also stores information about when
62 // the layer was last updated. If the present time is farther in the future than the
63 // updated time, the updated time is the present time.
Ady Abraham32efd542020-05-19 17:49:26 -070064 void setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, bool pendingConfigChange);
Ady Abraham8a82ba62020-01-17 12:43:17 -080065
Ady Abraham8a82ba62020-01-17 12:43:17 -080066 // Sets an explicit layer vote. This usually comes directly from the application via
67 // ANativeWindow_setFrameRate API
68 void setLayerVote(LayerHistory::LayerVoteType type, float fps) { mLayerVote = {type, fps}; }
69
70 // Sets the default layer vote. This will be the layer vote after calling to resetLayerVote().
71 // This is used for layers that called to setLayerVote() and then removed the vote, so that the
72 // layer can go back to whatever vote it had before the app voted for it.
73 void setDefaultLayerVote(LayerHistory::LayerVoteType type) { mDefaultVote = type; }
74
75 // Resets the layer vote to its default.
76 void resetLayerVote() { mLayerVote = {mDefaultVote, 0.0f}; }
77
78 std::pair<LayerHistory::LayerVoteType, float> getRefreshRate(nsecs_t now);
79
80 // Return the last updated time. If the present time is farther in the future than the
81 // updated time, the updated time is the present time.
82 nsecs_t getLastUpdatedTime() const { return mLastUpdatedTime; }
83
Ady Abrahama61edcb2020-01-30 18:32:03 -080084 void clearHistory() {
Ady Abraham1adbb722020-05-15 11:51:48 -070085 mFrameTimes.clear();
Ady Abrahamc9664832020-05-12 14:16:56 -070086 mLastReportedRefreshRate = 0.0f;
Ady Abrahama61edcb2020-01-30 18:32:03 -080087 }
Ady Abraham8a82ba62020-01-17 12:43:17 -080088
89private:
Ady Abrahama61edcb2020-01-30 18:32:03 -080090 // Used to store the layer timestamps
91 struct FrameTimeData {
92 nsecs_t presetTime; // desiredPresentTime, if provided
93 nsecs_t queueTime; // buffer queue time
Ady Abraham32efd542020-05-19 17:49:26 -070094 bool pendingConfigChange;
Ady Abrahama61edcb2020-01-30 18:32:03 -080095 };
96
Ady Abraham39db2c92020-05-21 14:20:33 -070097 bool isFrequent(nsecs_t now);
Ady Abraham8a82ba62020-01-17 12:43:17 -080098 bool hasEnoughDataForHeuristic() const;
99 std::optional<float> calculateRefreshRateIfPossible();
Ady Abraham32efd542020-05-19 17:49:26 -0700100 std::pair<nsecs_t, bool> calculateAverageFrameTime() const;
101 bool isRefreshRateStable(nsecs_t averageFrameTime, bool missingPresentTime) const;
Ady Abraham1adbb722020-05-15 11:51:48 -0700102
103 const std::string mName;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800104
105 // Used for sanitizing the heuristic data
106 const nsecs_t mHighRefreshRatePeriod;
107 LayerHistory::LayerVoteType mDefaultVote;
108
109 nsecs_t mLastUpdatedTime = 0;
110
111 float mLastReportedRefreshRate = 0.0f;
112
Ady Abraham39db2c92020-05-21 14:20:33 -0700113 // Used to determine whether a layer should be considered frequent or
114 // not when we don't have enough frames. This member will not be cleared
115 // as part of clearHistory() to remember whether this layer was frequent
116 // or not before we processed touch boost (or anything else that would
117 // clear layer history).
118 bool mLastReportedIsFrequent = true;
119
Ady Abraham8a82ba62020-01-17 12:43:17 -0800120 // Holds information about the layer vote
121 struct {
122 LayerHistory::LayerVoteType type;
123 float fps;
124 } mLayerVote;
125
Ady Abraham8a82ba62020-01-17 12:43:17 -0800126 std::deque<FrameTimeData> mFrameTimes;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800127 static constexpr size_t HISTORY_SIZE = 90;
128 static constexpr std::chrono::nanoseconds HISTORY_TIME = 1s;
129};
130
131} // namespace scheduler
132} // namespace android