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 ATRACE_CALL();
57
58 if (!fence) {
59 return false;
60 }
61
62 nsecs_t const signalTime = fence->getCachedSignalTime();
63 if (signalTime == Fence::SIGNAL_TIME_INVALID) {
64 return true;
65 }
66
67 std::lock_guard lock(mMutex);
68 if (mExternalIgnoreFences || mInternalIgnoreFences) {
69 ATRACE_FORMAT_INSTANT("mExternalIgnoreFences=%d mInternalIgnoreFences=%d",
70 mExternalIgnoreFences, mInternalIgnoreFences);
71 return true;
72 }
73
74 bool timestampAccepted = true;
75 for (auto it = mUnfiredFences.begin(); it != mUnfiredFences.end();) {
76 auto const time = (*it)->getCachedSignalTime();
77 if (time == Fence::SIGNAL_TIME_PENDING) {
78 it++;
79 } else if (time == Fence::SIGNAL_TIME_INVALID) {
80 it = mUnfiredFences.erase(it);
81 } else {
82 timestampAccepted &= mTracker.addVsyncTimestamp(time);
83
84 it = mUnfiredFences.erase(it);
85 }
86 }
87
88 if (signalTime == Fence::SIGNAL_TIME_PENDING) {
89 if (mPendingLimit == mUnfiredFences.size()) {
90 mUnfiredFences.erase(mUnfiredFences.begin());
91 }
92 mUnfiredFences.push_back(std::move(fence));
93 } else {
94 timestampAccepted &= mTracker.addVsyncTimestamp(signalTime);
95 }
96
97 if (!timestampAccepted) {
98 mMoreSamplesNeeded = true;
99 setIgnorePresentFencesInternal(true);
100 mPeriodConfirmationInProgress = true;
101 }
102
103 return mMoreSamplesNeeded;
104 }
105
setIgnorePresentFences(bool ignore)106 void VSyncReactor::setIgnorePresentFences(bool ignore) {
107 std::lock_guard lock(mMutex);
108 mExternalIgnoreFences = ignore;
109 updateIgnorePresentFencesInternal();
110 }
111
setIgnorePresentFencesInternal(bool ignore)112 void VSyncReactor::setIgnorePresentFencesInternal(bool ignore) {
113 mInternalIgnoreFences = ignore;
114 updateIgnorePresentFencesInternal();
115 }
116
updateIgnorePresentFencesInternal()117 void VSyncReactor::updateIgnorePresentFencesInternal() {
118 if (mExternalIgnoreFences || mInternalIgnoreFences) {
119 mUnfiredFences.clear();
120 }
121 }
122
startPeriodTransitionInternal(ftl::NonNull<DisplayModePtr> modePtr)123 void VSyncReactor::startPeriodTransitionInternal(ftl::NonNull<DisplayModePtr> modePtr) {
124 ATRACE_FORMAT("%s %" PRIu64, __func__, mId.value);
125 mPeriodConfirmationInProgress = true;
126 mModePtrTransitioningTo = modePtr.get();
127 mMoreSamplesNeeded = true;
128 setIgnorePresentFencesInternal(true);
129 }
130
endPeriodTransition()131 void VSyncReactor::endPeriodTransition() {
132 ATRACE_FORMAT("%s %" PRIu64, __func__, mId.value);
133 mModePtrTransitioningTo.reset();
134 mPeriodConfirmationInProgress = false;
135 mLastHwVsync.reset();
136 }
137
onDisplayModeChanged(ftl::NonNull<DisplayModePtr> modePtr,bool force)138 void VSyncReactor::onDisplayModeChanged(ftl::NonNull<DisplayModePtr> modePtr, bool force) {
139 ATRACE_INT64(ftl::Concat("VSR-", __func__, " ", mId.value).c_str(),
140 modePtr->getVsyncRate().getPeriodNsecs());
141 std::lock_guard lock(mMutex);
142 mLastHwVsync.reset();
143
144 if (!mSupportKernelIdleTimer && mTracker.isCurrentMode(modePtr) && !force) {
145 endPeriodTransition();
146 setIgnorePresentFencesInternal(false);
147 mMoreSamplesNeeded = false;
148 } else {
149 startPeriodTransitionInternal(modePtr);
150 }
151 }
152
periodConfirmed(nsecs_t vsync_timestamp,std::optional<nsecs_t> HwcVsyncPeriod)153 bool VSyncReactor::periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> HwcVsyncPeriod) {
154 if (!mPeriodConfirmationInProgress) {
155 return false;
156 }
157
158 if (mDisplayPowerMode == hal::PowerMode::DOZE ||
159 mDisplayPowerMode == hal::PowerMode::DOZE_SUSPEND) {
160 return true;
161 }
162
163 if (!mLastHwVsync && !HwcVsyncPeriod) {
164 return false;
165 }
166
167 const std::optional<Period> newPeriod = mModePtrTransitioningTo
168 ? mModePtrTransitioningTo->getVsyncRate().getPeriod()
169 : std::optional<Period>{};
170 const bool periodIsChanging = newPeriod && (newPeriod->ns() != mTracker.currentPeriod());
171 if (mSupportKernelIdleTimer && !periodIsChanging) {
172 // Clear out the Composer-provided period and use the allowance logic below
173 HwcVsyncPeriod = {};
174 }
175
176 auto const period = newPeriod ? newPeriod->ns() : mTracker.currentPeriod();
177 static constexpr int allowancePercent = 10;
178 static constexpr std::ratio<allowancePercent, 100> allowancePercentRatio;
179 auto const allowance = period * allowancePercentRatio.num / allowancePercentRatio.den;
180 if (HwcVsyncPeriod) {
181 return std::abs(*HwcVsyncPeriod - period) < allowance;
182 }
183
184 auto const distance = vsync_timestamp - *mLastHwVsync;
185 return std::abs(distance - period) < allowance;
186 }
187
addHwVsyncTimestamp(nsecs_t timestamp,std::optional<nsecs_t> hwcVsyncPeriod,bool * periodFlushed)188 bool VSyncReactor::addHwVsyncTimestamp(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
189 bool* periodFlushed) {
190 assert(periodFlushed);
191
192 std::lock_guard lock(mMutex);
193 if (periodConfirmed(timestamp, hwcVsyncPeriod)) {
194 ATRACE_FORMAT("VSR %" PRIu64 ": period confirmed", mId.value);
195 if (mModePtrTransitioningTo) {
196 mTracker.setDisplayModePtr(ftl::as_non_null(mModePtrTransitioningTo));
197 *periodFlushed = true;
198 }
199
200 if (mLastHwVsync) {
201 mTracker.addVsyncTimestamp(*mLastHwVsync);
202 }
203 mTracker.addVsyncTimestamp(timestamp);
204
205 endPeriodTransition();
206 mMoreSamplesNeeded = mTracker.needsMoreSamples();
207 } else if (mPeriodConfirmationInProgress) {
208 ATRACE_FORMAT("VSR %" PRIu64 ": still confirming period", mId.value);
209 mLastHwVsync = timestamp;
210 mMoreSamplesNeeded = true;
211 *periodFlushed = false;
212 } else {
213 ATRACE_FORMAT("VSR %" PRIu64 ": adding sample", mId.value);
214 *periodFlushed = false;
215 mTracker.addVsyncTimestamp(timestamp);
216 mMoreSamplesNeeded = mTracker.needsMoreSamples();
217 }
218
219 if (mExternalIgnoreFences) {
220 // keep HWVSync on as long as we ignore present fences.
221 mMoreSamplesNeeded = true;
222 }
223
224 if (!mMoreSamplesNeeded) {
225 setIgnorePresentFencesInternal(false);
226 }
227 return mMoreSamplesNeeded;
228 }
229
setDisplayPowerMode(hal::PowerMode powerMode)230 void VSyncReactor::setDisplayPowerMode(hal::PowerMode powerMode) {
231 std::scoped_lock lock(mMutex);
232 mDisplayPowerMode = powerMode;
233 }
234
dump(std::string & result) const235 void VSyncReactor::dump(std::string& result) const {
236 std::lock_guard lock(mMutex);
237 StringAppendF(&result, "VsyncReactor in use\n");
238 StringAppendF(&result, "Has %zu unfired fences\n", mUnfiredFences.size());
239 StringAppendF(&result, "mInternalIgnoreFences=%d mExternalIgnoreFences=%d\n",
240 mInternalIgnoreFences, mExternalIgnoreFences);
241 StringAppendF(&result, "mMoreSamplesNeeded=%d mPeriodConfirmationInProgress=%d\n",
242 mMoreSamplesNeeded, mPeriodConfirmationInProgress);
243 if (mModePtrTransitioningTo) {
244 StringAppendF(&result, "mModePtrTransitioningTo=%s\n",
245 to_string(*mModePtrTransitioningTo).c_str());
246 } else {
247 StringAppendF(&result, "mModePtrTransitioningTo=nullptr\n");
248 }
249
250 if (mLastHwVsync) {
251 StringAppendF(&result, "Last HW vsync was %.2fms ago\n",
252 (mClock->now() - *mLastHwVsync) / 1e6f);
253 } else {
254 StringAppendF(&result, "No Last HW vsync\n");
255 }
256
257 StringAppendF(&result, "VSyncTracker:\n");
258 mTracker.dump(result);
259 }
260
261 } // namespace android::scheduler
262