blob: 524555681b5d6e2761096f2978f5e6b982c93b56 [file] [log] [blame]
Dominik Laskowski068173d2021-08-11 17:22:59 -07001/*
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
Ady Abrahamc621d8d2022-06-22 17:01:25 +000017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050019#include <ftl/fake_guard.h>
Dominik Laskowski068173d2021-08-11 17:22:59 -070020#include <scheduler/Fps.h>
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080021#include <scheduler/Timer.h>
Dominik Laskowski068173d2021-08-11 17:22:59 -070022
23#include "VsyncSchedule.h"
24
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050025#include "ISchedulerCallback.h"
26#include "Utils/Dumper.h"
Dominik Laskowski068173d2021-08-11 17:22:59 -070027#include "VSyncDispatchTimerQueue.h"
28#include "VSyncPredictor.h"
29#include "VSyncReactor.h"
30
31#include "../TracedOrdinal.h"
32
33namespace android::scheduler {
34
35class VsyncSchedule::PredictedVsyncTracer {
36 // Invoked from the thread of the VsyncDispatch owned by this VsyncSchedule.
37 constexpr auto makeVsyncCallback() {
38 return [this](nsecs_t, nsecs_t, nsecs_t) {
39 mParity = !mParity;
40 schedule();
41 };
42 }
43
44public:
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -050045 explicit PredictedVsyncTracer(VsyncDispatch& dispatch)
46 : mRegistration(dispatch, makeVsyncCallback(), __func__) {
Dominik Laskowski068173d2021-08-11 17:22:59 -070047 schedule();
48 }
49
50private:
51 void schedule() { mRegistration.schedule({0, 0, 0}); }
52
53 TracedOrdinal<bool> mParity = {"VSYNC-predicted", 0};
54 VSyncCallbackRegistration mRegistration;
55};
56
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -050057VsyncSchedule::VsyncSchedule(FeatureFlags features)
58 : mTracker(createTracker()),
59 mDispatch(createDispatch(*mTracker)),
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050060 mController(createController(*mTracker, features)),
61 mTracer(features.test(Feature::kTracePredictedVsync)
62 ? std::make_unique<PredictedVsyncTracer>(*mDispatch)
63 : nullptr) {}
Dominik Laskowski068173d2021-08-11 17:22:59 -070064
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -050065VsyncSchedule::VsyncSchedule(TrackerPtr tracker, DispatchPtr dispatch, ControllerPtr controller)
66 : mTracker(std::move(tracker)),
Dominik Laskowski068173d2021-08-11 17:22:59 -070067 mDispatch(std::move(dispatch)),
68 mController(std::move(controller)) {}
69
Dominik Laskowski068173d2021-08-11 17:22:59 -070070VsyncSchedule::~VsyncSchedule() = default;
71
Dominik Laskowski5d164f22022-07-07 07:56:07 -070072Period VsyncSchedule::period() const {
73 return Period::fromNs(mTracker->currentPeriod());
74}
75
76TimePoint VsyncSchedule::vsyncDeadlineAfter(TimePoint timePoint) const {
77 return TimePoint::fromNs(mTracker->nextAnticipatedVSyncTimeFrom(timePoint.ns()));
78}
79
Dominik Laskowski068173d2021-08-11 17:22:59 -070080void VsyncSchedule::dump(std::string& out) const {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050081 utils::Dumper dumper(out);
82 {
83 std::lock_guard<std::mutex> lock(mHwVsyncLock);
84 dumper.dump("hwVsyncState", ftl::enum_string(mHwVsyncState));
85
86 ftl::FakeGuard guard(kMainThreadContext);
87 dumper.dump("pendingHwVsyncState", ftl::enum_string(mPendingHwVsyncState));
88 dumper.eol();
89 }
90
Dominik Laskowski068173d2021-08-11 17:22:59 -070091 out.append("VsyncController:\n");
92 mController->dump(out);
93
94 out.append("VsyncDispatch:\n");
95 mDispatch->dump(out);
96}
97
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -050098VsyncSchedule::TrackerPtr VsyncSchedule::createTracker() {
Dominik Laskowski068173d2021-08-11 17:22:59 -070099 // TODO(b/144707443): Tune constants.
100 constexpr nsecs_t kInitialPeriod = (60_Hz).getPeriodNsecs();
101 constexpr size_t kHistorySize = 20;
102 constexpr size_t kMinSamplesForPrediction = 6;
103 constexpr uint32_t kDiscardOutlierPercent = 20;
104
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500105 return std::make_unique<VSyncPredictor>(kInitialPeriod, kHistorySize, kMinSamplesForPrediction,
106 kDiscardOutlierPercent);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700107}
108
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500109VsyncSchedule::DispatchPtr VsyncSchedule::createDispatch(VsyncTracker& tracker) {
Dominik Laskowski068173d2021-08-11 17:22:59 -0700110 using namespace std::chrono_literals;
111
112 // TODO(b/144707443): Tune constants.
113 constexpr std::chrono::nanoseconds kGroupDispatchWithin = 500us;
114 constexpr std::chrono::nanoseconds kSnapToSameVsyncWithin = 3ms;
115
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500116 return std::make_unique<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), tracker,
Dominik Laskowski068173d2021-08-11 17:22:59 -0700117 kGroupDispatchWithin.count(),
118 kSnapToSameVsyncWithin.count());
119}
120
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500121VsyncSchedule::ControllerPtr VsyncSchedule::createController(VsyncTracker& tracker,
Dominik Laskowski068173d2021-08-11 17:22:59 -0700122 FeatureFlags features) {
123 // TODO(b/144707443): Tune constants.
124 constexpr size_t kMaxPendingFences = 20;
125 const bool hasKernelIdleTimer = features.test(Feature::kKernelIdleTimer);
126
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500127 auto reactor = std::make_unique<VSyncReactor>(std::make_unique<SystemClock>(), tracker,
128 kMaxPendingFences, hasKernelIdleTimer);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700129
130 reactor->setIgnorePresentFences(!features.test(Feature::kPresentFences));
131 return reactor;
132}
133
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500134void VsyncSchedule::startPeriodTransition(ISchedulerCallback& callback, Period period) {
135 std::lock_guard<std::mutex> lock(mHwVsyncLock);
136 mController->startPeriodTransition(period.ns());
137 enableHardwareVsyncLocked(callback);
138}
139
140bool VsyncSchedule::addResyncSample(ISchedulerCallback& callback, TimePoint timestamp,
141 ftl::Optional<Period> hwcVsyncPeriod) {
142 bool needsHwVsync = false;
143 bool periodFlushed = false;
144 {
145 std::lock_guard<std::mutex> lock(mHwVsyncLock);
146 if (mHwVsyncState == HwVsyncState::Enabled) {
147 needsHwVsync = mController->addHwVsyncTimestamp(timestamp.ns(),
148 hwcVsyncPeriod.transform(&Period::ns),
149 &periodFlushed);
150 }
151 }
152 if (needsHwVsync) {
153 enableHardwareVsync(callback);
154 } else {
155 disableHardwareVsync(callback, false /* disallow */);
156 }
157 return periodFlushed;
158}
159
160void VsyncSchedule::enableHardwareVsync(ISchedulerCallback& callback) {
161 std::lock_guard<std::mutex> lock(mHwVsyncLock);
162 enableHardwareVsyncLocked(callback);
163}
164
165void VsyncSchedule::enableHardwareVsyncLocked(ISchedulerCallback& callback) {
166 if (mHwVsyncState == HwVsyncState::Disabled) {
167 getTracker().resetModel();
168 callback.setVsyncEnabled(true);
169 mHwVsyncState = HwVsyncState::Enabled;
170 }
171}
172
173void VsyncSchedule::disableHardwareVsync(ISchedulerCallback& callback, bool disallow) {
174 std::lock_guard<std::mutex> lock(mHwVsyncLock);
175 if (mHwVsyncState == HwVsyncState::Enabled) {
176 callback.setVsyncEnabled(false);
177 }
178 mHwVsyncState = disallow ? HwVsyncState::Disallowed : HwVsyncState::Disabled;
179}
180
181bool VsyncSchedule::isHardwareVsyncAllowed(bool makeAllowed) {
182 std::lock_guard<std::mutex> lock(mHwVsyncLock);
183 if (makeAllowed && mHwVsyncState == HwVsyncState::Disallowed) {
184 mHwVsyncState = HwVsyncState::Disabled;
185 }
186 return mHwVsyncState != HwVsyncState::Disallowed;
187}
188
189void VsyncSchedule::setPendingHardwareVsyncState(bool enabled) {
190 mPendingHwVsyncState = enabled ? HwVsyncState::Enabled : HwVsyncState::Disabled;
191}
192
193bool VsyncSchedule::getPendingHardwareVsyncState() const {
194 return mPendingHwVsyncState == HwVsyncState::Enabled;
195}
196
Dominik Laskowski068173d2021-08-11 17:22:59 -0700197} // namespace android::scheduler