1 /*
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 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
18 #undef LOG_TAG
19 #define LOG_TAG "VSyncReactor"
20 //#define LOG_NDEBUG 0
21
22 #include <assert.h>
23 #include <cutils/properties.h>
24 #include <ftl/concat.h>
25 #include <gui/TraceUtils.h>
26 #include <log/log.h>
27 #include <utils/Trace.h>
28
29 #include "../TracedOrdinal.h"
30 #include "VSyncDispatch.h"
31 #include "VSyncReactor.h"
32 #include "VSyncTracker.h"
33
34 namespace android::scheduler {
35
36 using base::StringAppendF;
37
38 VsyncController::~VsyncController() = default;
39
now() const40 nsecs_t SystemClock::now() const {
41 return systemTime(SYSTEM_TIME_MONOTONIC);
42 }
43
VSyncReactor(PhysicalDisplayId id,std::unique_ptr<Clock> clock,VSyncTracker & tracker,size_t pendingFenceLimit,bool supportKernelIdleTimer)44 VSyncReactor::VSyncReactor(PhysicalDisplayId id, std::unique_ptr<Clock> clock,
45 VSyncTracker& tracker, size_t pendingFenceLimit,
46 bool supportKernelIdleTimer)
47 : mId(id),
48 mClock(std::move(clock)),
49 mTracker(tracker),
50 mPendingLimit(pendingFenceLimit),
51 mSupportKernelIdleTimer(supportKernelIdleTimer) {}
52
53 VSyncReactor::~VSyncReactor() = default;
54
addPresentFence(std::shared_ptr<FenceTime> fence)55 bool VSyncReactor::addPresentFence(std::shared_ptr<FenceTime> fence) {
56 if (!fence) {
57 return false;
58 }
59
60 nsecs_t const signalTime = fence->getCachedSignalTime();
61 if (signalTime == Fence::SIGNAL_TIME_INVALID) {
62 return true;
63 }
64
65 std::lock_guard lock(mMutex);
66 if (mExternalIgnoreFences || mInternalIgnoreFences) {
67 return true;
68 }
69
70 bool timestampAccepted = true;
71 for (auto it = mUnfiredFences.begin(); it != mUnfiredFences.end();) {
72 auto const time = (*it)->getCachedSignalTime();
73 if (time == Fence::SIGNAL_TIME_PENDING) {
74 it++;
75 } else if (time == Fence::SIGNAL_TIME_INVALID) {
76 it = mUnfiredFences.erase(it);
77 } else {
78 timestampAccepted &= mTracker.addVsyncTimestamp(time);
79
80 it = mUnfiredFences.erase(it);
81 }
82 }
83
84 if (signalTime == Fence::SIGNAL_TIME_PENDING) {
85 if (mPendingLimit == mUnfiredFences.size()) {
86 mUnfiredFences.erase(mUnfiredFences.begin());
87 }
88 mUnfiredFences.push_back(std::move(fence));
89 } else {
90 timestampAccepted &= mTracker.addVsyncTimestamp(signalTime);
91 }
92
93 if (!timestampAccepted) {
94 mMoreSamplesNeeded = true;
95 setIgnorePresentFencesInternal(true);
96 mPeriodConfirmationInProgress = true;
97 }
98
99 return mMoreSamplesNeeded;
100 }
101
setIgnorePresentFences(bool ignore)102 void VSyncReactor::setIgnorePresentFences(bool ignore) {
103 std::lock_guard lock(mMutex);
104 mExternalIgnoreFences = ignore;
105 updateIgnorePresentFencesInternal();
106 }
107
setIgnorePresentFencesInternal(bool ignore)108 void VSyncReactor::setIgnorePresentFencesInternal(bool ignore) {
109 mInternalIgnoreFences = ignore;
110 updateIgnorePresentFencesInternal();
111 }
112
updateIgnorePresentFencesInternal()113 void VSyncReactor::updateIgnorePresentFencesInternal() {
114 if (mExternalIgnoreFences || mInternalIgnoreFences) {
115 mUnfiredFences.clear();
116 }
117 }
118
startPeriodTransitionInternal(nsecs_t newPeriod)119 void VSyncReactor::startPeriodTransitionInternal(nsecs_t newPeriod) {
120 ATRACE_FORMAT("%s %" PRIu64, __func__, mId.value);
121 mPeriodConfirmationInProgress = true;
122 mPeriodTransitioningTo = newPeriod;
123 mMoreSamplesNeeded = true;
124 setIgnorePresentFencesInternal(true);
125 }
126
endPeriodTransition()127 void VSyncReactor::endPeriodTransition() {
128 ATRACE_FORMAT("%s %" PRIu64, __func__, mId.value);
129 mPeriodTransitioningTo.reset();
130 mPeriodConfirmationInProgress = false;
131 mLastHwVsync.reset();
132 }
133
startPeriodTransition(nsecs_t period,bool force)134 void VSyncReactor::startPeriodTransition(nsecs_t period, bool force) {
135 ATRACE_INT64(ftl::Concat("VSR-", __func__, " ", mId.value).c_str(), period);
136 std::lock_guard lock(mMutex);
137 mLastHwVsync.reset();
138
139 if (!mSupportKernelIdleTimer && period == mTracker.currentPeriod() && !force) {
140 endPeriodTransition();
141 setIgnorePresentFencesInternal(false);
142 mMoreSamplesNeeded = false;
143 } else {
144 startPeriodTransitionInternal(period);
145 }
146 }
147
periodConfirmed(nsecs_t vsync_timestamp,std::optional<nsecs_t> HwcVsyncPeriod)148 bool VSyncReactor::periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> HwcVsyncPeriod) {
149 if (!mPeriodConfirmationInProgress) {
150 return false;
151 }
152
153 if (mDisplayPowerMode == hal::PowerMode::DOZE ||
154 mDisplayPowerMode == hal::PowerMode::DOZE_SUSPEND) {
155 return true;
156 }
157
158 if (!mLastHwVsync && !HwcVsyncPeriod) {
159 return false;
160 }
161
162 const bool periodIsChanging =
163 mPeriodTransitioningTo && (*mPeriodTransitioningTo != mTracker.currentPeriod());
164 if (mSupportKernelIdleTimer && !periodIsChanging) {
165 // Clear out the Composer-provided period and use the allowance logic below
166 HwcVsyncPeriod = {};
167 }
168
169 auto const period = mPeriodTransitioningTo ? *mPeriodTransitioningTo : mTracker.currentPeriod();
170 static constexpr int allowancePercent = 10;
171 static constexpr std::ratio<allowancePercent, 100> allowancePercentRatio;
172 auto const allowance = period * allowancePercentRatio.num / allowancePercentRatio.den;
173 if (HwcVsyncPeriod) {
174 return std::abs(*HwcVsyncPeriod - period) < allowance;
175 }
176
177 auto const distance = vsync_timestamp - *mLastHwVsync;
178 return std::abs(distance - period) < allowance;
179 }
180
addHwVsyncTimestamp(nsecs_t timestamp,std::optional<nsecs_t> hwcVsyncPeriod,bool * periodFlushed)181 bool VSyncReactor::addHwVsyncTimestamp(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
182 bool* periodFlushed) {
183 assert(periodFlushed);
184
185 std::lock_guard lock(mMutex);
186 if (periodConfirmed(timestamp, hwcVsyncPeriod)) {
187 ATRACE_FORMAT("VSR %" PRIu64 ": period confirmed", mId.value);
188 if (mPeriodTransitioningTo) {
189 mTracker.setPeriod(*mPeriodTransitioningTo);
190 *periodFlushed = true;
191 }
192
193 if (mLastHwVsync) {
194 mTracker.addVsyncTimestamp(*mLastHwVsync);
195 }
196 mTracker.addVsyncTimestamp(timestamp);
197
198 endPeriodTransition();
199 mMoreSamplesNeeded = mTracker.needsMoreSamples();
200 } else if (mPeriodConfirmationInProgress) {
201 ATRACE_FORMAT("VSR %" PRIu64 ": still confirming period", mId.value);
202 mLastHwVsync = timestamp;
203 mMoreSamplesNeeded = true;
204 *periodFlushed = false;
205 } else {
206 ATRACE_FORMAT("VSR %" PRIu64 ": adding sample", mId.value);
207 *periodFlushed = false;
208 mTracker.addVsyncTimestamp(timestamp);
209 mMoreSamplesNeeded = mTracker.needsMoreSamples();
210 }
211
212 if (!mMoreSamplesNeeded) {
213 setIgnorePresentFencesInternal(false);
214 }
215 return mMoreSamplesNeeded;
216 }
217
setDisplayPowerMode(hal::PowerMode powerMode)218 void VSyncReactor::setDisplayPowerMode(hal::PowerMode powerMode) {
219 std::scoped_lock lock(mMutex);
220 mDisplayPowerMode = powerMode;
221 }
222
dump(std::string & result) const223 void VSyncReactor::dump(std::string& result) const {
224 std::lock_guard lock(mMutex);
225 StringAppendF(&result, "VsyncReactor in use\n");
226 StringAppendF(&result, "Has %zu unfired fences\n", mUnfiredFences.size());
227 StringAppendF(&result, "mInternalIgnoreFences=%d mExternalIgnoreFences=%d\n",
228 mInternalIgnoreFences, mExternalIgnoreFences);
229 StringAppendF(&result, "mMoreSamplesNeeded=%d mPeriodConfirmationInProgress=%d\n",
230 mMoreSamplesNeeded, mPeriodConfirmationInProgress);
231 if (mPeriodTransitioningTo) {
232 StringAppendF(&result, "mPeriodTransitioningTo=%" PRId64 "\n", *mPeriodTransitioningTo);
233 } else {
234 StringAppendF(&result, "mPeriodTransitioningTo=nullptr\n");
235 }
236
237 if (mLastHwVsync) {
238 StringAppendF(&result, "Last HW vsync was %.2fms ago\n",
239 (mClock->now() - *mLastHwVsync) / 1e6f);
240 } else {
241 StringAppendF(&result, "No Last HW vsync\n");
242 }
243
244 StringAppendF(&result, "VSyncTracker:\n");
245 mTracker.dump(result);
246 }
247
248 } // namespace android::scheduler
249