blob: 4a8aac606bf17545372cf3551645cde212e85c81 [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
84TimePoint VsyncSchedule::vsyncDeadlineAfter(TimePoint timePoint) const {
85 return TimePoint::fromNs(mTracker->nextAnticipatedVSyncTimeFrom(timePoint.ns()));
86}
87
Dominik Laskowski068173d2021-08-11 17:22:59 -070088void VsyncSchedule::dump(std::string& out) const {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050089 utils::Dumper dumper(out);
90 {
91 std::lock_guard<std::mutex> lock(mHwVsyncLock);
92 dumper.dump("hwVsyncState", ftl::enum_string(mHwVsyncState));
93
94 ftl::FakeGuard guard(kMainThreadContext);
95 dumper.dump("pendingHwVsyncState", ftl::enum_string(mPendingHwVsyncState));
96 dumper.eol();
97 }
98
Dominik Laskowski068173d2021-08-11 17:22:59 -070099 out.append("VsyncController:\n");
100 mController->dump(out);
101
102 out.append("VsyncDispatch:\n");
103 mDispatch->dump(out);
104}
105
Ady Abrahamc585dba2023-11-15 18:41:35 -0800106VsyncSchedule::TrackerPtr VsyncSchedule::createTracker(ftl::NonNull<DisplayModePtr> modePtr,
ramindanid4354a92023-10-02 15:11:09 -0700107 IVsyncTrackerCallback& callback) {
Dominik Laskowski068173d2021-08-11 17:22:59 -0700108 // TODO(b/144707443): Tune constants.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700109 constexpr size_t kHistorySize = 20;
110 constexpr size_t kMinSamplesForPrediction = 6;
111 constexpr uint32_t kDiscardOutlierPercent = 20;
112
Ady Abrahamc585dba2023-11-15 18:41:35 -0800113 return std::make_unique<VSyncPredictor>(modePtr, kHistorySize, kMinSamplesForPrediction,
114 kDiscardOutlierPercent, callback);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700115}
116
Leon Scroggins III67388622023-02-06 20:36:20 -0500117VsyncSchedule::DispatchPtr VsyncSchedule::createDispatch(TrackerPtr tracker) {
Dominik Laskowski068173d2021-08-11 17:22:59 -0700118 using namespace std::chrono_literals;
119
120 // TODO(b/144707443): Tune constants.
121 constexpr std::chrono::nanoseconds kGroupDispatchWithin = 500us;
122 constexpr std::chrono::nanoseconds kSnapToSameVsyncWithin = 3ms;
123
Leon Scroggins III67388622023-02-06 20:36:20 -0500124 return std::make_unique<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), std::move(tracker),
Dominik Laskowski068173d2021-08-11 17:22:59 -0700125 kGroupDispatchWithin.count(),
126 kSnapToSameVsyncWithin.count());
127}
128
Leon Scroggins III67388622023-02-06 20:36:20 -0500129VsyncSchedule::ControllerPtr VsyncSchedule::createController(PhysicalDisplayId id,
130 VsyncTracker& tracker,
Dominik Laskowski068173d2021-08-11 17:22:59 -0700131 FeatureFlags features) {
132 // TODO(b/144707443): Tune constants.
133 constexpr size_t kMaxPendingFences = 20;
134 const bool hasKernelIdleTimer = features.test(Feature::kKernelIdleTimer);
135
Leon Scroggins III67388622023-02-06 20:36:20 -0500136 auto reactor = std::make_unique<VSyncReactor>(id, std::make_unique<SystemClock>(), tracker,
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500137 kMaxPendingFences, hasKernelIdleTimer);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700138
139 reactor->setIgnorePresentFences(!features.test(Feature::kPresentFences));
140 return reactor;
141}
142
Ady Abrahamc585dba2023-11-15 18:41:35 -0800143void VsyncSchedule::onDisplayModeChanged(ftl::NonNull<DisplayModePtr> modePtr, bool force) {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500144 std::lock_guard<std::mutex> lock(mHwVsyncLock);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800145 mController->onDisplayModeChanged(modePtr, force);
Dominik Laskowski66295432023-03-14 12:25:36 -0400146 enableHardwareVsyncLocked();
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500147}
148
Dominik Laskowski66295432023-03-14 12:25:36 -0400149bool VsyncSchedule::addResyncSample(TimePoint timestamp, ftl::Optional<Period> hwcVsyncPeriod) {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500150 bool needsHwVsync = false;
151 bool periodFlushed = false;
152 {
153 std::lock_guard<std::mutex> lock(mHwVsyncLock);
154 if (mHwVsyncState == HwVsyncState::Enabled) {
155 needsHwVsync = mController->addHwVsyncTimestamp(timestamp.ns(),
156 hwcVsyncPeriod.transform(&Period::ns),
157 &periodFlushed);
158 }
159 }
160 if (needsHwVsync) {
Dominik Laskowski66295432023-03-14 12:25:36 -0400161 enableHardwareVsync();
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500162 } else {
Dominik Laskowski66295432023-03-14 12:25:36 -0400163 constexpr bool kDisallow = false;
164 disableHardwareVsync(kDisallow);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500165 }
166 return periodFlushed;
167}
168
Dominik Laskowski66295432023-03-14 12:25:36 -0400169void VsyncSchedule::enableHardwareVsync() {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500170 std::lock_guard<std::mutex> lock(mHwVsyncLock);
Dominik Laskowski66295432023-03-14 12:25:36 -0400171 enableHardwareVsyncLocked();
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500172}
173
Dominik Laskowski66295432023-03-14 12:25:36 -0400174void VsyncSchedule::enableHardwareVsyncLocked() {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500175 if (mHwVsyncState == HwVsyncState::Disabled) {
Rachel Leefe3af7c2023-06-20 21:11:43 +0000176 getTracker().resetModel();
Dominik Laskowski66295432023-03-14 12:25:36 -0400177 mRequestHardwareVsync(mId, true);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500178 mHwVsyncState = HwVsyncState::Enabled;
179 }
180}
181
Dominik Laskowski66295432023-03-14 12:25:36 -0400182void VsyncSchedule::disableHardwareVsync(bool disallow) {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500183 std::lock_guard<std::mutex> lock(mHwVsyncLock);
Leon Scroggins IIIaadc62d2023-03-03 11:07:50 -0500184 switch (mHwVsyncState) {
185 case HwVsyncState::Enabled:
Dominik Laskowski66295432023-03-14 12:25:36 -0400186 mRequestHardwareVsync(mId, false);
Leon Scroggins IIIaadc62d2023-03-03 11:07:50 -0500187 [[fallthrough]];
188 case HwVsyncState::Disabled:
189 mHwVsyncState = disallow ? HwVsyncState::Disallowed : HwVsyncState::Disabled;
190 break;
191 case HwVsyncState::Disallowed:
192 break;
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500193 }
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500194}
195
196bool VsyncSchedule::isHardwareVsyncAllowed(bool makeAllowed) {
197 std::lock_guard<std::mutex> lock(mHwVsyncLock);
198 if (makeAllowed && mHwVsyncState == HwVsyncState::Disallowed) {
199 mHwVsyncState = HwVsyncState::Disabled;
200 }
201 return mHwVsyncState != HwVsyncState::Disallowed;
202}
203
204void VsyncSchedule::setPendingHardwareVsyncState(bool enabled) {
205 mPendingHwVsyncState = enabled ? HwVsyncState::Enabled : HwVsyncState::Disabled;
206}
207
208bool VsyncSchedule::getPendingHardwareVsyncState() const {
209 return mPendingHwVsyncState == HwVsyncState::Enabled;
210}
211
Dominik Laskowski068173d2021-08-11 17:22:59 -0700212} // namespace android::scheduler