blob: 2fa3318560d9d220f01d493600dd6258d29c44d8 [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>
Ady Abrahamf0b2bf92023-12-13 23:36:35 +000022#include <gui/TraceUtils.h>
Dominik Laskowski068173d2021-08-11 17:22:59 -070023#include <scheduler/Fps.h>
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080024#include <scheduler/Timer.h>
Dominik Laskowski068173d2021-08-11 17:22:59 -070025
26#include "VsyncSchedule.h"
27
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050028#include "Utils/Dumper.h"
Dominik Laskowski068173d2021-08-11 17:22:59 -070029#include "VSyncDispatchTimerQueue.h"
30#include "VSyncPredictor.h"
31#include "VSyncReactor.h"
32
33#include "../TracedOrdinal.h"
34
35namespace android::scheduler {
36
37class VsyncSchedule::PredictedVsyncTracer {
38 // Invoked from the thread of the VsyncDispatch owned by this VsyncSchedule.
39 constexpr auto makeVsyncCallback() {
40 return [this](nsecs_t, nsecs_t, nsecs_t) {
41 mParity = !mParity;
42 schedule();
43 };
44 }
45
46public:
Leon Scroggins III67388622023-02-06 20:36:20 -050047 explicit PredictedVsyncTracer(std::shared_ptr<VsyncDispatch> dispatch)
48 : mRegistration(std::move(dispatch), makeVsyncCallback(), __func__) {
Dominik Laskowski068173d2021-08-11 17:22:59 -070049 schedule();
50 }
51
52private:
53 void schedule() { mRegistration.schedule({0, 0, 0}); }
54
55 TracedOrdinal<bool> mParity = {"VSYNC-predicted", 0};
56 VSyncCallbackRegistration mRegistration;
57};
58
Ady Abrahamc585dba2023-11-15 18:41:35 -080059VsyncSchedule::VsyncSchedule(ftl::NonNull<DisplayModePtr> modePtr, FeatureFlags features,
ramindaniae645822024-01-11 10:57:29 -080060 RequestHardwareVsync requestHardwareVsync)
Ady Abrahamc585dba2023-11-15 18:41:35 -080061 : mId(modePtr->getPhysicalDisplayId()),
Dominik Laskowski66295432023-03-14 12:25:36 -040062 mRequestHardwareVsync(std::move(requestHardwareVsync)),
ramindaniae645822024-01-11 10:57:29 -080063 mTracker(createTracker(modePtr)),
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
Leon Scroggins IIIa0785012024-01-23 16:05:59 -050091TimePoint 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(); })));
Dominik Laskowski5d164f22022-07-07 07:56:07 -070097}
98
Dominik Laskowski068173d2021-08-11 17:22:59 -070099void VsyncSchedule::dump(std::string& out) const {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500100 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
Dominik Laskowski068173d2021-08-11 17:22:59 -0700110 out.append("VsyncController:\n");
111 mController->dump(out);
112
113 out.append("VsyncDispatch:\n");
114 mDispatch->dump(out);
115}
116
ramindaniae645822024-01-11 10:57:29 -0800117VsyncSchedule::TrackerPtr VsyncSchedule::createTracker(ftl::NonNull<DisplayModePtr> modePtr) {
Dominik Laskowski068173d2021-08-11 17:22:59 -0700118 // TODO(b/144707443): Tune constants.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700119 constexpr size_t kHistorySize = 20;
120 constexpr size_t kMinSamplesForPrediction = 6;
121 constexpr uint32_t kDiscardOutlierPercent = 20;
122
Ady Abraham20024aa2024-03-05 01:32:49 +0000123 return std::make_unique<VSyncPredictor>(std::make_unique<SystemClock>(), modePtr, kHistorySize,
124 kMinSamplesForPrediction, kDiscardOutlierPercent);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700125}
126
Leon Scroggins III67388622023-02-06 20:36:20 -0500127VsyncSchedule::DispatchPtr VsyncSchedule::createDispatch(TrackerPtr tracker) {
Dominik Laskowski068173d2021-08-11 17:22:59 -0700128 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
Leon Scroggins III67388622023-02-06 20:36:20 -0500134 return std::make_unique<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), std::move(tracker),
Dominik Laskowski068173d2021-08-11 17:22:59 -0700135 kGroupDispatchWithin.count(),
136 kSnapToSameVsyncWithin.count());
137}
138
Leon Scroggins III67388622023-02-06 20:36:20 -0500139VsyncSchedule::ControllerPtr VsyncSchedule::createController(PhysicalDisplayId id,
140 VsyncTracker& tracker,
Dominik Laskowski068173d2021-08-11 17:22:59 -0700141 FeatureFlags features) {
142 // TODO(b/144707443): Tune constants.
143 constexpr size_t kMaxPendingFences = 20;
144 const bool hasKernelIdleTimer = features.test(Feature::kKernelIdleTimer);
145
Leon Scroggins III67388622023-02-06 20:36:20 -0500146 auto reactor = std::make_unique<VSyncReactor>(id, std::make_unique<SystemClock>(), tracker,
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500147 kMaxPendingFences, hasKernelIdleTimer);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700148
149 reactor->setIgnorePresentFences(!features.test(Feature::kPresentFences));
150 return reactor;
151}
152
Ady Abrahamc585dba2023-11-15 18:41:35 -0800153void VsyncSchedule::onDisplayModeChanged(ftl::NonNull<DisplayModePtr> modePtr, bool force) {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500154 std::lock_guard<std::mutex> lock(mHwVsyncLock);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800155 mController->onDisplayModeChanged(modePtr, force);
Dominik Laskowski66295432023-03-14 12:25:36 -0400156 enableHardwareVsyncLocked();
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500157}
158
Dominik Laskowski66295432023-03-14 12:25:36 -0400159bool VsyncSchedule::addResyncSample(TimePoint timestamp, ftl::Optional<Period> hwcVsyncPeriod) {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500160 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) {
Dominik Laskowski66295432023-03-14 12:25:36 -0400171 enableHardwareVsync();
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500172 } else {
Dominik Laskowski66295432023-03-14 12:25:36 -0400173 constexpr bool kDisallow = false;
174 disableHardwareVsync(kDisallow);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500175 }
176 return periodFlushed;
177}
178
Dominik Laskowski66295432023-03-14 12:25:36 -0400179void VsyncSchedule::enableHardwareVsync() {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500180 std::lock_guard<std::mutex> lock(mHwVsyncLock);
Dominik Laskowski66295432023-03-14 12:25:36 -0400181 enableHardwareVsyncLocked();
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500182}
183
Dominik Laskowski66295432023-03-14 12:25:36 -0400184void VsyncSchedule::enableHardwareVsyncLocked() {
Ady Abrahamf0b2bf92023-12-13 23:36:35 +0000185 ATRACE_CALL();
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500186 if (mHwVsyncState == HwVsyncState::Disabled) {
Rachel Leefe3af7c2023-06-20 21:11:43 +0000187 getTracker().resetModel();
Dominik Laskowski66295432023-03-14 12:25:36 -0400188 mRequestHardwareVsync(mId, true);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500189 mHwVsyncState = HwVsyncState::Enabled;
190 }
191}
192
Dominik Laskowski66295432023-03-14 12:25:36 -0400193void VsyncSchedule::disableHardwareVsync(bool disallow) {
Ady Abrahamf0b2bf92023-12-13 23:36:35 +0000194 ATRACE_CALL();
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500195 std::lock_guard<std::mutex> lock(mHwVsyncLock);
Leon Scroggins IIIaadc62d2023-03-03 11:07:50 -0500196 switch (mHwVsyncState) {
197 case HwVsyncState::Enabled:
Dominik Laskowski66295432023-03-14 12:25:36 -0400198 mRequestHardwareVsync(mId, false);
Leon Scroggins IIIaadc62d2023-03-03 11:07:50 -0500199 [[fallthrough]];
200 case HwVsyncState::Disabled:
201 mHwVsyncState = disallow ? HwVsyncState::Disallowed : HwVsyncState::Disabled;
202 break;
203 case HwVsyncState::Disallowed:
204 break;
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500205 }
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500206}
207
208bool 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
216void VsyncSchedule::setPendingHardwareVsyncState(bool enabled) {
217 mPendingHwVsyncState = enabled ? HwVsyncState::Enabled : HwVsyncState::Disabled;
218}
219
220bool VsyncSchedule::getPendingHardwareVsyncState() const {
221 return mPendingHwVsyncState == HwVsyncState::Enabled;
222}
223
Dominik Laskowski068173d2021-08-11 17:22:59 -0700224} // namespace android::scheduler