1 /*
2 * Copyright 2021 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
19 #include <common/FlagManager.h>
20
21 #include <ftl/fake_guard.h>
22 #include <gui/TraceUtils.h>
23 #include <scheduler/Fps.h>
24 #include <scheduler/Timer.h>
25
26 #include "VsyncSchedule.h"
27
28 #include "Utils/Dumper.h"
29 #include "VSyncDispatchTimerQueue.h"
30 #include "VSyncPredictor.h"
31 #include "VSyncReactor.h"
32
33 #include "../TracedOrdinal.h"
34
35 namespace android::scheduler {
36
37 class VsyncSchedule::PredictedVsyncTracer {
38 // Invoked from the thread of the VsyncDispatch owned by this VsyncSchedule.
makeVsyncCallback()39 constexpr auto makeVsyncCallback() {
40 return [this](nsecs_t, nsecs_t, nsecs_t) {
41 mParity = !mParity;
42 schedule();
43 };
44 }
45
46 public:
PredictedVsyncTracer(std::shared_ptr<VsyncDispatch> dispatch)47 explicit PredictedVsyncTracer(std::shared_ptr<VsyncDispatch> dispatch)
48 : mRegistration(std::move(dispatch), makeVsyncCallback(), __func__) {
49 schedule();
50 }
51
52 private:
schedule()53 void schedule() { mRegistration.schedule({0, 0, 0}); }
54
55 TracedOrdinal<bool> mParity = {"VSYNC-predicted", 0};
56 VSyncCallbackRegistration mRegistration;
57 };
58
VsyncSchedule(ftl::NonNull<DisplayModePtr> modePtr,FeatureFlags features,RequestHardwareVsync requestHardwareVsync)59 VsyncSchedule::VsyncSchedule(ftl::NonNull<DisplayModePtr> modePtr, FeatureFlags features,
60 RequestHardwareVsync requestHardwareVsync)
61 : mId(modePtr->getPhysicalDisplayId()),
62 mRequestHardwareVsync(std::move(requestHardwareVsync)),
63 mTracker(createTracker(modePtr)),
64 mDispatch(createDispatch(mTracker)),
65 mController(createController(modePtr->getPhysicalDisplayId(), *mTracker, features)),
66 mTracer(features.test(Feature::kTracePredictedVsync)
67 ? std::make_unique<PredictedVsyncTracer>(mDispatch)
68 : nullptr) {}
69
VsyncSchedule(PhysicalDisplayId id,TrackerPtr tracker,DispatchPtr dispatch,ControllerPtr controller,RequestHardwareVsync requestHardwareVsync)70 VsyncSchedule::VsyncSchedule(PhysicalDisplayId id, TrackerPtr tracker, DispatchPtr dispatch,
71 ControllerPtr controller, RequestHardwareVsync requestHardwareVsync)
72 : mId(id),
73 mRequestHardwareVsync(std::move(requestHardwareVsync)),
74 mTracker(std::move(tracker)),
75 mDispatch(std::move(dispatch)),
76 mController(std::move(controller)) {}
77
78 VsyncSchedule::~VsyncSchedule() = default;
79
period() const80 Period VsyncSchedule::period() const {
81 return Period::fromNs(mTracker->currentPeriod());
82 }
83
minFramePeriod() const84 Period VsyncSchedule::minFramePeriod() const {
85 if (FlagManager::getInstance().vrr_config()) {
86 return mTracker->minFramePeriod();
87 }
88 return period();
89 }
90
vsyncDeadlineAfter(TimePoint timePoint,ftl::Optional<TimePoint> lastVsyncOpt) const91 TimePoint VsyncSchedule::vsyncDeadlineAfter(TimePoint timePoint,
92 ftl::Optional<TimePoint> lastVsyncOpt) const {
93 return TimePoint::fromNs(
94 mTracker->nextAnticipatedVSyncTimeFrom(timePoint.ns(),
95 lastVsyncOpt.transform(
96 [](TimePoint t) { return t.ns(); })));
97 }
98
dump(std::string & out) const99 void VsyncSchedule::dump(std::string& out) const {
100 utils::Dumper dumper(out);
101 {
102 std::lock_guard<std::mutex> lock(mHwVsyncLock);
103 dumper.dump("hwVsyncState", ftl::enum_string(mHwVsyncState));
104
105 ftl::FakeGuard guard(kMainThreadContext);
106 dumper.dump("pendingHwVsyncState", ftl::enum_string(mPendingHwVsyncState));
107 dumper.eol();
108 }
109
110 out.append("VsyncController:\n");
111 mController->dump(out);
112
113 out.append("VsyncDispatch:\n");
114 mDispatch->dump(out);
115 }
116
createTracker(ftl::NonNull<DisplayModePtr> modePtr)117 VsyncSchedule::TrackerPtr VsyncSchedule::createTracker(ftl::NonNull<DisplayModePtr> modePtr) {
118 // TODO(b/144707443): Tune constants.
119 constexpr size_t kHistorySize = 20;
120 constexpr size_t kMinSamplesForPrediction = 6;
121 constexpr uint32_t kDiscardOutlierPercent = 20;
122
123 return std::make_unique<VSyncPredictor>(std::make_unique<SystemClock>(), modePtr, kHistorySize,
124 kMinSamplesForPrediction, kDiscardOutlierPercent);
125 }
126
createDispatch(TrackerPtr tracker)127 VsyncSchedule::DispatchPtr VsyncSchedule::createDispatch(TrackerPtr tracker) {
128 using namespace std::chrono_literals;
129
130 // TODO(b/144707443): Tune constants.
131 constexpr std::chrono::nanoseconds kGroupDispatchWithin = 500us;
132 constexpr std::chrono::nanoseconds kSnapToSameVsyncWithin = 3ms;
133
134 return std::make_unique<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), std::move(tracker),
135 kGroupDispatchWithin.count(),
136 kSnapToSameVsyncWithin.count());
137 }
138
createController(PhysicalDisplayId id,VsyncTracker & tracker,FeatureFlags features)139 VsyncSchedule::ControllerPtr VsyncSchedule::createController(PhysicalDisplayId id,
140 VsyncTracker& tracker,
141 FeatureFlags features) {
142 // TODO(b/144707443): Tune constants.
143 constexpr size_t kMaxPendingFences = 20;
144 const bool hasKernelIdleTimer = features.test(Feature::kKernelIdleTimer);
145
146 auto reactor = std::make_unique<VSyncReactor>(id, std::make_unique<SystemClock>(), tracker,
147 kMaxPendingFences, hasKernelIdleTimer);
148
149 reactor->setIgnorePresentFences(!features.test(Feature::kPresentFences));
150 return reactor;
151 }
152
onDisplayModeChanged(ftl::NonNull<DisplayModePtr> modePtr,bool force)153 void VsyncSchedule::onDisplayModeChanged(ftl::NonNull<DisplayModePtr> modePtr, bool force) {
154 std::lock_guard<std::mutex> lock(mHwVsyncLock);
155 mController->onDisplayModeChanged(modePtr, force);
156 enableHardwareVsyncLocked();
157 }
158
addResyncSample(TimePoint timestamp,ftl::Optional<Period> hwcVsyncPeriod)159 bool VsyncSchedule::addResyncSample(TimePoint timestamp, ftl::Optional<Period> hwcVsyncPeriod) {
160 bool needsHwVsync = false;
161 bool periodFlushed = false;
162 {
163 std::lock_guard<std::mutex> lock(mHwVsyncLock);
164 if (mHwVsyncState == HwVsyncState::Enabled) {
165 needsHwVsync = mController->addHwVsyncTimestamp(timestamp.ns(),
166 hwcVsyncPeriod.transform(&Period::ns),
167 &periodFlushed);
168 }
169 }
170 if (needsHwVsync) {
171 enableHardwareVsync();
172 } else {
173 constexpr bool kDisallow = false;
174 disableHardwareVsync(kDisallow);
175 }
176 return periodFlushed;
177 }
178
enableHardwareVsync()179 void VsyncSchedule::enableHardwareVsync() {
180 std::lock_guard<std::mutex> lock(mHwVsyncLock);
181 enableHardwareVsyncLocked();
182 }
183
enableHardwareVsyncLocked()184 void VsyncSchedule::enableHardwareVsyncLocked() {
185 ATRACE_CALL();
186 if (mHwVsyncState == HwVsyncState::Disabled) {
187 getTracker().resetModel();
188 mRequestHardwareVsync(mId, true);
189 mHwVsyncState = HwVsyncState::Enabled;
190 }
191 }
192
disableHardwareVsync(bool disallow)193 void VsyncSchedule::disableHardwareVsync(bool disallow) {
194 ATRACE_CALL();
195 std::lock_guard<std::mutex> lock(mHwVsyncLock);
196 switch (mHwVsyncState) {
197 case HwVsyncState::Enabled:
198 mRequestHardwareVsync(mId, false);
199 [[fallthrough]];
200 case HwVsyncState::Disabled:
201 mHwVsyncState = disallow ? HwVsyncState::Disallowed : HwVsyncState::Disabled;
202 break;
203 case HwVsyncState::Disallowed:
204 break;
205 }
206 }
207
isHardwareVsyncAllowed(bool makeAllowed)208 bool VsyncSchedule::isHardwareVsyncAllowed(bool makeAllowed) {
209 std::lock_guard<std::mutex> lock(mHwVsyncLock);
210 if (makeAllowed && mHwVsyncState == HwVsyncState::Disallowed) {
211 mHwVsyncState = HwVsyncState::Disabled;
212 }
213 return mHwVsyncState != HwVsyncState::Disallowed;
214 }
215
setPendingHardwareVsyncState(bool enabled)216 void VsyncSchedule::setPendingHardwareVsyncState(bool enabled) {
217 mPendingHwVsyncState = enabled ? HwVsyncState::Enabled : HwVsyncState::Disabled;
218 }
219
getPendingHardwareVsyncState() const220 bool VsyncSchedule::getPendingHardwareVsyncState() const {
221 return mPendingHwVsyncState == HwVsyncState::Enabled;
222 }
223
224 } // namespace android::scheduler
225