blob: 90d426969c2c2af53e67bce43b64435663239504 [file] [log] [blame]
Ady Abraham09bd3922019-04-08 10:44:56 -07001/*
2 * Copyright 2019 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 <cinttypes>
20#include <cstdint>
21#include <deque>
22#include <mutex>
23#include <numeric>
24#include <string>
25
26#include <log/log.h>
27
28#include <utils/Mutex.h>
29#include <utils/Timers.h>
30
31#include "SchedulerUtils.h"
32
33namespace android {
34namespace scheduler {
35
36/*
37 * This class represents information about individial layers.
38 */
39class LayerInfo {
40 /**
41 * Struct that keeps the information about the refresh rate for last
42 * HISTORY_SIZE frames. This is used to better determine the refresh rate
43 * for individual layers.
44 */
45 class RefreshRateHistory {
46 public:
47 explicit RefreshRateHistory(nsecs_t minRefreshDuration)
48 : mMinRefreshDuration(minRefreshDuration) {}
49 void insertRefreshRate(nsecs_t refreshRate) {
50 mElements.push_back(refreshRate);
51 if (mElements.size() > HISTORY_SIZE) {
52 mElements.pop_front();
53 }
54 }
55
56 float getRefreshRateAvg() const {
57 nsecs_t refreshDuration = mMinRefreshDuration;
Ady Abraham616b8322019-06-12 13:30:07 -070058 if (mElements.size() > 0) {
Ady Abraham09bd3922019-04-08 10:44:56 -070059 refreshDuration = scheduler::calculate_mean(mElements);
60 }
61
62 return 1e9f / refreshDuration;
63 }
64 void clearHistory() { mElements.clear(); }
65
66 private:
67 std::deque<nsecs_t> mElements;
68 static constexpr size_t HISTORY_SIZE = 30;
69 const nsecs_t mMinRefreshDuration;
70 };
71
72 /**
73 * Struct that keeps the information about the present time for last
74 * HISTORY_SIZE frames. This is used to better determine whether the given layer
75 * is still relevant and it's refresh rate should be considered.
76 */
77 class PresentTimeHistory {
78 public:
79 void insertPresentTime(nsecs_t presentTime) {
80 mElements.push_back(presentTime);
81 if (mElements.size() > HISTORY_SIZE) {
82 mElements.pop_front();
83 }
84 }
85
86 // Checks whether the present time that was inserted HISTORY_SIZE ago is within a
87 // certain threshold: TIME_EPSILON_NS.
88 bool isRelevant() const {
Ady Abraham616b8322019-06-12 13:30:07 -070089 if (mElements.size() < 2) {
90 return false;
Ady Abraham09bd3922019-04-08 10:44:56 -070091 }
Ady Abraham616b8322019-06-12 13:30:07 -070092
93 // The layer had to publish at least HISTORY_SIZE or HISTORY_TIME of updates
94 if (mElements.size() != HISTORY_SIZE &&
95 mElements.at(mElements.size() - 1) - mElements.at(0) < HISTORY_TIME.count()) {
96 return false;
97 }
98
99 // The last update should not be older than TIME_EPSILON_NS nanoseconds.
100 const int64_t obsoleteEpsilon = systemTime() - scheduler::TIME_EPSILON_NS.count();
101 if (mElements.at(mElements.size() - 1) < obsoleteEpsilon) {
102 return false;
103 }
104
105 return true;
Ady Abraham09bd3922019-04-08 10:44:56 -0700106 }
107
108 void clearHistory() { mElements.clear(); }
109
110 private:
111 std::deque<nsecs_t> mElements;
112 static constexpr size_t HISTORY_SIZE = 10;
Ady Abraham616b8322019-06-12 13:30:07 -0700113 static constexpr std::chrono::nanoseconds HISTORY_TIME = 500ms;
Ady Abraham09bd3922019-04-08 10:44:56 -0700114 };
115
116public:
117 LayerInfo(const std::string name, float maxRefreshRate);
118 ~LayerInfo();
119
120 LayerInfo(const LayerInfo&) = delete;
121 LayerInfo& operator=(const LayerInfo&) = delete;
122
123 // Records the last requested oresent time. It also stores information about when
124 // the layer was last updated. If the present time is farther in the future than the
125 // updated time, the updated time is the present time.
126 void setLastPresentTime(nsecs_t lastPresentTime);
127
Ady Abrahama315ce72019-04-24 14:35:20 -0700128 void setHDRContent(bool isHdr) {
129 std::lock_guard lock(mLock);
130 mIsHDR = isHdr;
131 }
132
133 void setVisibility(bool visible) {
134 std::lock_guard lock(mLock);
135 mIsVisible = visible;
136 }
137
Ady Abraham09bd3922019-04-08 10:44:56 -0700138 // Checks the present time history to see whether the layer is relevant.
139 bool isRecentlyActive() const {
140 std::lock_guard lock(mLock);
141 return mPresentTimeHistory.isRelevant();
142 }
143
144 // Calculate the average refresh rate.
145 float getDesiredRefreshRate() const {
146 std::lock_guard lock(mLock);
147 return mRefreshRateHistory.getRefreshRateAvg();
148 }
149
Ady Abrahama315ce72019-04-24 14:35:20 -0700150 bool getHDRContent() {
151 std::lock_guard lock(mLock);
152 return mIsHDR;
153 }
154
155 bool isVisible() {
156 std::lock_guard lock(mLock);
157 return mIsVisible;
158 }
159
Ady Abraham09bd3922019-04-08 10:44:56 -0700160 // Return the last updated time. If the present time is farther in the future than the
161 // updated time, the updated time is the present time.
162 nsecs_t getLastUpdatedTime() {
163 std::lock_guard lock(mLock);
164 return mLastUpdatedTime;
165 }
166
167 std::string getName() const { return mName; }
168
169 void clearHistory() {
170 std::lock_guard lock(mLock);
171 mRefreshRateHistory.clearHistory();
172 mPresentTimeHistory.clearHistory();
173 }
174
175private:
176 const std::string mName;
177 const nsecs_t mMinRefreshDuration;
178 mutable std::mutex mLock;
179 nsecs_t mLastUpdatedTime GUARDED_BY(mLock) = 0;
180 nsecs_t mLastPresentTime GUARDED_BY(mLock) = 0;
181 RefreshRateHistory mRefreshRateHistory GUARDED_BY(mLock);
182 PresentTimeHistory mPresentTimeHistory GUARDED_BY(mLock);
Ady Abrahama315ce72019-04-24 14:35:20 -0700183 bool mIsHDR GUARDED_BY(mLock) = false;
184 bool mIsVisible GUARDED_BY(mLock) = false;
Ady Abraham09bd3922019-04-08 10:44:56 -0700185};
186
187} // namespace scheduler
188} // namespace android