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