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