blob: ff1c9e909a65635497970b2f4beb2d00ab02c9a8 [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
Ady Abrahamc585dba2023-11-15 18:41:35 -080019#include <common/FlagManager.h>
20
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050021#include <ftl/fake_guard.h>
Dominik Laskowski068173d2021-08-11 17:22:59 -070022#include <scheduler/Fps.h>
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080023#include <scheduler/Timer.h>
Dominik Laskowski068173d2021-08-11 17:22:59 -070024
25#include "VsyncSchedule.h"
26
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050027#include "Utils/Dumper.h"
Dominik Laskowski068173d2021-08-11 17:22:59 -070028#include "VSyncDispatchTimerQueue.h"
29#include "VSyncPredictor.h"
30#include "VSyncReactor.h"
31
32#include "../TracedOrdinal.h"
33
34namespace android::scheduler {
35
36class VsyncSchedule::PredictedVsyncTracer {
37 // Invoked from the thread of the VsyncDispatch owned by this VsyncSchedule.
38 constexpr auto makeVsyncCallback() {
39 return [this](nsecs_t, nsecs_t, nsecs_t) {
40 mParity = !mParity;
41 schedule();
42 };
43 }
44
45public:
Leon Scroggins III67388622023-02-06 20:36:20 -050046 explicit PredictedVsyncTracer(std::shared_ptr<VsyncDispatch> dispatch)
47 : mRegistration(std::move(dispatch), makeVsyncCallback(), __func__) {
Dominik Laskowski068173d2021-08-11 17:22:59 -070048 schedule();
49 }
50
51private:
52 void schedule() { mRegistration.schedule({0, 0, 0}); }
53
54 TracedOrdinal<bool> mParity = {"VSYNC-predicted", 0};
55 VSyncCallbackRegistration mRegistration;
56};
57
Ady Abrahamc585dba2023-11-15 18:41:35 -080058VsyncSchedule::VsyncSchedule(ftl::NonNull<DisplayModePtr> modePtr, FeatureFlags features,
ramindanid4354a92023-10-02 15:11:09 -070059 RequestHardwareVsync requestHardwareVsync,
60 IVsyncTrackerCallback& callback)
Ady Abrahamc585dba2023-11-15 18:41:35 -080061 : mId(modePtr->getPhysicalDisplayId()),
Dominik Laskowski66295432023-03-14 12:25:36 -040062 mRequestHardwareVsync(std::move(requestHardwareVsync)),
Ady Abrahamc585dba2023-11-15 18:41:35 -080063 mTracker(createTracker(modePtr, callback)),
Leon Scroggins III67388622023-02-06 20:36:20 -050064 mDispatch(createDispatch(mTracker)),
Ady Abrahamc585dba2023-11-15 18:41:35 -080065 mController(createController(modePtr->getPhysicalDisplayId(), *mTracker, features)),
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050066 mTracer(features.test(Feature::kTracePredictedVsync)
Leon Scroggins III67388622023-02-06 20:36:20 -050067 ? std::make_unique<PredictedVsyncTracer>(mDispatch)
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050068 : nullptr) {}
Dominik Laskowski068173d2021-08-11 17:22:59 -070069
Leon Scroggins III67388622023-02-06 20:36:20 -050070VsyncSchedule::VsyncSchedule(PhysicalDisplayId id, TrackerPtr tracker, DispatchPtr dispatch,
Dominik Laskowski66295432023-03-14 12:25:36 -040071 ControllerPtr controller, RequestHardwareVsync requestHardwareVsync)
Leon Scroggins III67388622023-02-06 20:36:20 -050072 : mId(id),
Dominik Laskowski66295432023-03-14 12:25:36 -040073 mRequestHardwareVsync(std::move(requestHardwareVsync)),
Leon Scroggins III67388622023-02-06 20:36:20 -050074 mTracker(std::move(tracker)),
Dominik Laskowski068173d2021-08-11 17:22:59 -070075 mDispatch(std::move(dispatch)),
76 mController(std::move(controller)) {}
77
Dominik Laskowski068173d2021-08-11 17:22:59 -070078VsyncSchedule::~VsyncSchedule() = default;
79
Dominik Laskowski5d164f22022-07-07 07:56:07 -070080Period VsyncSchedule::period() const {
81 return Period::fromNs(mTracker->currentPeriod());
82}
83
Ady Abraham3db8a3c2023-11-20 17:53:47 -080084Period VsyncSchedule::minFramePeriod() const {
85 if (FlagManager::getInstance().vrr_config()) {
86 return mTracker->minFramePeriod();
87 }
88 return period();
89}
90
Dominik Laskowski5d164f22022-07-07 07:56:07 -070091TimePoint VsyncSchedule::vsyncDeadlineAfter(TimePoint timePoint) const {
92 return TimePoint::fromNs(mTracker->nextAnticipatedVSyncTimeFrom(timePoint.ns()));
93}
94
Dominik Laskowski068173d2021-08-11 17:22:59 -070095void VsyncSchedule::dump(std::string& out) const {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050096 utils::Dumper dumper(out);
97 {
98 std::lock_guard<std::mutex> lock(mHwVsyncLock);
99 dumper.dump("hwVsyncState", ftl::enum_string(mHwVsyncState));
100
101 ftl::FakeGuard guard(kMainThreadContext);
102 dumper.dump("pendingHwVsyncState", ftl::enum_string(mPendingHwVsyncState));
103 dumper.eol();
104 }
105
Dominik Laskowski068173d2021-08-11 17:22:59 -0700106 out.append("VsyncController:\n");
107 mController->dump(out);
108
109 out.append("VsyncDispatch:\n");
110 mDispatch->dump(out);
111}
112
Ady Abrahamc585dba2023-11-15 18:41:35 -0800113VsyncSchedule::TrackerPtr VsyncSchedule::createTracker(ftl::NonNull<DisplayModePtr> modePtr,
ramindanid4354a92023-10-02 15:11:09 -0700114 IVsyncTrackerCallback& callback) {
Dominik Laskowski068173d2021-08-11 17:22:59 -0700115 // TODO(b/144707443): Tune constants.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700116 constexpr size_t kHistorySize = 20;
117 constexpr size_t kMinSamplesForPrediction = 6;
118 constexpr uint32_t kDiscardOutlierPercent = 20;
119
Ady Abrahamc585dba2023-11-15 18:41:35 -0800120 return std::make_unique<VSyncPredictor>(modePtr, kHistorySize, kMinSamplesForPrediction,
121 kDiscardOutlierPercent, callback);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700122}
123
Leon Scroggins III67388622023-02-06 20:36:20 -0500124VsyncSchedule::DispatchPtr VsyncSchedule::createDispatch(TrackerPtr tracker) {
Dominik Laskowski068173d2021-08-11 17:22:59 -0700125 using namespace std::chrono_literals;
126
127 // TODO(b/144707443): Tune constants.
128 constexpr std::chrono::nanoseconds kGroupDispatchWithin = 500us;
129 constexpr std::chrono::nanoseconds kSnapToSameVsyncWithin = 3ms;
130
Leon Scroggins III67388622023-02-06 20:36:20 -0500131 return std::make_unique<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), std::move(tracker),
Dominik Laskowski068173d2021-08-11 17:22:59 -0700132 kGroupDispatchWithin.count(),
133 kSnapToSameVsyncWithin.count());
134}
135
Leon Scroggins III67388622023-02-06 20:36:20 -0500136VsyncSchedule::ControllerPtr VsyncSchedule::createController(PhysicalDisplayId id,
137 VsyncTracker& tracker,
Dominik Laskowski068173d2021-08-11 17:22:59 -0700138 FeatureFlags features) {
139 // TODO(b/144707443): Tune constants.
140 constexpr size_t kMaxPendingFences = 20;
141 const bool hasKernelIdleTimer = features.test(Feature::kKernelIdleTimer);
142
Leon Scroggins III67388622023-02-06 20:36:20 -0500143 auto reactor = std::make_unique<VSyncReactor>(id, std::make_unique<SystemClock>(), tracker,
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500144 kMaxPendingFences, hasKernelIdleTimer);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700145
146 reactor->setIgnorePresentFences(!features.test(Feature::kPresentFences));
147 return reactor;
148}
149
Ady Abrahamc585dba2023-11-15 18:41:35 -0800150void VsyncSchedule::onDisplayModeChanged(ftl::NonNull<DisplayModePtr> modePtr, bool force) {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500151 std::lock_guard<std::mutex> lock(mHwVsyncLock);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800152 mController->onDisplayModeChanged(modePtr, force);
Dominik Laskowski66295432023-03-14 12:25:36 -0400153 enableHardwareVsyncLocked();
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500154}
155
Dominik Laskowski66295432023-03-14 12:25:36 -0400156bool VsyncSchedule::addResyncSample(TimePoint timestamp, ftl::Optional<Period> hwcVsyncPeriod) {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500157 bool needsHwVsync = false;
158 bool periodFlushed = false;
159 {
160 std::lock_guard<std::mutex> lock(mHwVsyncLock);
161 if (mHwVsyncState == HwVsyncState::Enabled) {
162 needsHwVsync = mController->addHwVsyncTimestamp(timestamp.ns(),
163 hwcVsyncPeriod.transform(&Period::ns),
164 &periodFlushed);
165 }
166 }
167 if (needsHwVsync) {
Dominik Laskowski66295432023-03-14 12:25:36 -0400168 enableHardwareVsync();
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500169 } else {
Dominik Laskowski66295432023-03-14 12:25:36 -0400170 constexpr bool kDisallow = false;
171 disableHardwareVsync(kDisallow);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500172 }
173 return periodFlushed;
174}
175
Dominik Laskowski66295432023-03-14 12:25:36 -0400176void VsyncSchedule::enableHardwareVsync() {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500177 std::lock_guard<std::mutex> lock(mHwVsyncLock);
Dominik Laskowski66295432023-03-14 12:25:36 -0400178 enableHardwareVsyncLocked();
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500179}
180
Dominik Laskowski66295432023-03-14 12:25:36 -0400181void VsyncSchedule::enableHardwareVsyncLocked() {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500182 if (mHwVsyncState == HwVsyncState::Disabled) {
Rachel Leefe3af7c2023-06-20 21:11:43 +0000183 getTracker().resetModel();
Dominik Laskowski66295432023-03-14 12:25:36 -0400184 mRequestHardwareVsync(mId, true);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500185 mHwVsyncState = HwVsyncState::Enabled;
186 }
187}
188
Dominik Laskowski66295432023-03-14 12:25:36 -0400189void VsyncSchedule::disableHardwareVsync(bool disallow) {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500190 std::lock_guard<std::mutex> lock(mHwVsyncLock);
Leon Scroggins IIIaadc62d2023-03-03 11:07:50 -0500191 switch (mHwVsyncState) {
192 case HwVsyncState::Enabled:
Dominik Laskowski66295432023-03-14 12:25:36 -0400193 mRequestHardwareVsync(mId, false);
Leon Scroggins IIIaadc62d2023-03-03 11:07:50 -0500194 [[fallthrough]];
195 case HwVsyncState::Disabled:
196 mHwVsyncState = disallow ? HwVsyncState::Disallowed : HwVsyncState::Disabled;
197 break;
198 case HwVsyncState::Disallowed:
199 break;
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500200 }
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500201}
202
203bool VsyncSchedule::isHardwareVsyncAllowed(bool makeAllowed) {
204 std::lock_guard<std::mutex> lock(mHwVsyncLock);
205 if (makeAllowed && mHwVsyncState == HwVsyncState::Disallowed) {
206 mHwVsyncState = HwVsyncState::Disabled;
207 }
208 return mHwVsyncState != HwVsyncState::Disallowed;
209}
210
211void VsyncSchedule::setPendingHardwareVsyncState(bool enabled) {
212 mPendingHwVsyncState = enabled ? HwVsyncState::Enabled : HwVsyncState::Disabled;
213}
214
215bool VsyncSchedule::getPendingHardwareVsyncState() const {
216 return mPendingHwVsyncState == HwVsyncState::Enabled;
217}
218
Dominik Laskowski068173d2021-08-11 17:22:59 -0700219} // namespace android::scheduler