blob: 3491600d8e2cec7c4c9971a8df644790e231929f [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,
ramindanid4354a92023-10-02 15:11:09 -070060 RequestHardwareVsync requestHardwareVsync,
61 IVsyncTrackerCallback& callback)
Ady Abrahamc585dba2023-11-15 18:41:35 -080062 : mId(modePtr->getPhysicalDisplayId()),
Dominik Laskowski66295432023-03-14 12:25:36 -040063 mRequestHardwareVsync(std::move(requestHardwareVsync)),
Ady Abrahamc585dba2023-11-15 18:41:35 -080064 mTracker(createTracker(modePtr, callback)),
Leon Scroggins III67388622023-02-06 20:36:20 -050065 mDispatch(createDispatch(mTracker)),
Ady Abrahamc585dba2023-11-15 18:41:35 -080066 mController(createController(modePtr->getPhysicalDisplayId(), *mTracker, features)),
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050067 mTracer(features.test(Feature::kTracePredictedVsync)
Leon Scroggins III67388622023-02-06 20:36:20 -050068 ? std::make_unique<PredictedVsyncTracer>(mDispatch)
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050069 : nullptr) {}
Dominik Laskowski068173d2021-08-11 17:22:59 -070070
Leon Scroggins III67388622023-02-06 20:36:20 -050071VsyncSchedule::VsyncSchedule(PhysicalDisplayId id, TrackerPtr tracker, DispatchPtr dispatch,
Dominik Laskowski66295432023-03-14 12:25:36 -040072 ControllerPtr controller, RequestHardwareVsync requestHardwareVsync)
Leon Scroggins III67388622023-02-06 20:36:20 -050073 : mId(id),
Dominik Laskowski66295432023-03-14 12:25:36 -040074 mRequestHardwareVsync(std::move(requestHardwareVsync)),
Leon Scroggins III67388622023-02-06 20:36:20 -050075 mTracker(std::move(tracker)),
Dominik Laskowski068173d2021-08-11 17:22:59 -070076 mDispatch(std::move(dispatch)),
77 mController(std::move(controller)) {}
78
Dominik Laskowski068173d2021-08-11 17:22:59 -070079VsyncSchedule::~VsyncSchedule() = default;
80
Dominik Laskowski5d164f22022-07-07 07:56:07 -070081Period VsyncSchedule::period() const {
82 return Period::fromNs(mTracker->currentPeriod());
83}
84
Ady Abraham3db8a3c2023-11-20 17:53:47 -080085Period VsyncSchedule::minFramePeriod() const {
86 if (FlagManager::getInstance().vrr_config()) {
87 return mTracker->minFramePeriod();
88 }
89 return period();
90}
91
Leon Scroggins IIIa0785012024-01-23 16:05:59 -050092TimePoint VsyncSchedule::vsyncDeadlineAfter(TimePoint timePoint,
93 ftl::Optional<TimePoint> lastVsyncOpt) const {
94 return TimePoint::fromNs(
95 mTracker->nextAnticipatedVSyncTimeFrom(timePoint.ns(),
96 lastVsyncOpt.transform(
97 [](TimePoint t) { return t.ns(); })));
Dominik Laskowski5d164f22022-07-07 07:56:07 -070098}
99
Dominik Laskowski068173d2021-08-11 17:22:59 -0700100void VsyncSchedule::dump(std::string& out) const {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500101 utils::Dumper dumper(out);
102 {
103 std::lock_guard<std::mutex> lock(mHwVsyncLock);
104 dumper.dump("hwVsyncState", ftl::enum_string(mHwVsyncState));
105
106 ftl::FakeGuard guard(kMainThreadContext);
107 dumper.dump("pendingHwVsyncState", ftl::enum_string(mPendingHwVsyncState));
108 dumper.eol();
109 }
110
Dominik Laskowski068173d2021-08-11 17:22:59 -0700111 out.append("VsyncController:\n");
112 mController->dump(out);
113
114 out.append("VsyncDispatch:\n");
115 mDispatch->dump(out);
116}
117
Ady Abrahamc585dba2023-11-15 18:41:35 -0800118VsyncSchedule::TrackerPtr VsyncSchedule::createTracker(ftl::NonNull<DisplayModePtr> modePtr,
ramindanid4354a92023-10-02 15:11:09 -0700119 IVsyncTrackerCallback& callback) {
Dominik Laskowski068173d2021-08-11 17:22:59 -0700120 // TODO(b/144707443): Tune constants.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700121 constexpr size_t kHistorySize = 20;
122 constexpr size_t kMinSamplesForPrediction = 6;
123 constexpr uint32_t kDiscardOutlierPercent = 20;
124
Ady Abrahamc585dba2023-11-15 18:41:35 -0800125 return std::make_unique<VSyncPredictor>(modePtr, kHistorySize, kMinSamplesForPrediction,
126 kDiscardOutlierPercent, callback);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700127}
128
Leon Scroggins III67388622023-02-06 20:36:20 -0500129VsyncSchedule::DispatchPtr VsyncSchedule::createDispatch(TrackerPtr tracker) {
Dominik Laskowski068173d2021-08-11 17:22:59 -0700130 using namespace std::chrono_literals;
131
132 // TODO(b/144707443): Tune constants.
133 constexpr std::chrono::nanoseconds kGroupDispatchWithin = 500us;
134 constexpr std::chrono::nanoseconds kSnapToSameVsyncWithin = 3ms;
135
Leon Scroggins III67388622023-02-06 20:36:20 -0500136 return std::make_unique<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), std::move(tracker),
Dominik Laskowski068173d2021-08-11 17:22:59 -0700137 kGroupDispatchWithin.count(),
138 kSnapToSameVsyncWithin.count());
139}
140
Leon Scroggins III67388622023-02-06 20:36:20 -0500141VsyncSchedule::ControllerPtr VsyncSchedule::createController(PhysicalDisplayId id,
142 VsyncTracker& tracker,
Dominik Laskowski068173d2021-08-11 17:22:59 -0700143 FeatureFlags features) {
144 // TODO(b/144707443): Tune constants.
145 constexpr size_t kMaxPendingFences = 20;
146 const bool hasKernelIdleTimer = features.test(Feature::kKernelIdleTimer);
147
Leon Scroggins III67388622023-02-06 20:36:20 -0500148 auto reactor = std::make_unique<VSyncReactor>(id, std::make_unique<SystemClock>(), tracker,
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500149 kMaxPendingFences, hasKernelIdleTimer);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700150
151 reactor->setIgnorePresentFences(!features.test(Feature::kPresentFences));
152 return reactor;
153}
154
Ady Abrahamc585dba2023-11-15 18:41:35 -0800155void VsyncSchedule::onDisplayModeChanged(ftl::NonNull<DisplayModePtr> modePtr, bool force) {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500156 std::lock_guard<std::mutex> lock(mHwVsyncLock);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800157 mController->onDisplayModeChanged(modePtr, force);
Dominik Laskowski66295432023-03-14 12:25:36 -0400158 enableHardwareVsyncLocked();
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500159}
160
Dominik Laskowski66295432023-03-14 12:25:36 -0400161bool VsyncSchedule::addResyncSample(TimePoint timestamp, ftl::Optional<Period> hwcVsyncPeriod) {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500162 bool needsHwVsync = false;
163 bool periodFlushed = false;
164 {
165 std::lock_guard<std::mutex> lock(mHwVsyncLock);
166 if (mHwVsyncState == HwVsyncState::Enabled) {
167 needsHwVsync = mController->addHwVsyncTimestamp(timestamp.ns(),
168 hwcVsyncPeriod.transform(&Period::ns),
169 &periodFlushed);
170 }
171 }
172 if (needsHwVsync) {
Dominik Laskowski66295432023-03-14 12:25:36 -0400173 enableHardwareVsync();
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500174 } else {
Dominik Laskowski66295432023-03-14 12:25:36 -0400175 constexpr bool kDisallow = false;
176 disableHardwareVsync(kDisallow);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500177 }
178 return periodFlushed;
179}
180
Dominik Laskowski66295432023-03-14 12:25:36 -0400181void VsyncSchedule::enableHardwareVsync() {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500182 std::lock_guard<std::mutex> lock(mHwVsyncLock);
Dominik Laskowski66295432023-03-14 12:25:36 -0400183 enableHardwareVsyncLocked();
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500184}
185
Dominik Laskowski66295432023-03-14 12:25:36 -0400186void VsyncSchedule::enableHardwareVsyncLocked() {
Ady Abrahamf0b2bf92023-12-13 23:36:35 +0000187 ATRACE_CALL();
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500188 if (mHwVsyncState == HwVsyncState::Disabled) {
Rachel Leefe3af7c2023-06-20 21:11:43 +0000189 getTracker().resetModel();
Dominik Laskowski66295432023-03-14 12:25:36 -0400190 mRequestHardwareVsync(mId, true);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500191 mHwVsyncState = HwVsyncState::Enabled;
192 }
193}
194
Dominik Laskowski66295432023-03-14 12:25:36 -0400195void VsyncSchedule::disableHardwareVsync(bool disallow) {
Ady Abrahamf0b2bf92023-12-13 23:36:35 +0000196 ATRACE_CALL();
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500197 std::lock_guard<std::mutex> lock(mHwVsyncLock);
Leon Scroggins IIIaadc62d2023-03-03 11:07:50 -0500198 switch (mHwVsyncState) {
199 case HwVsyncState::Enabled:
Dominik Laskowski66295432023-03-14 12:25:36 -0400200 mRequestHardwareVsync(mId, false);
Leon Scroggins IIIaadc62d2023-03-03 11:07:50 -0500201 [[fallthrough]];
202 case HwVsyncState::Disabled:
203 mHwVsyncState = disallow ? HwVsyncState::Disallowed : HwVsyncState::Disabled;
204 break;
205 case HwVsyncState::Disallowed:
206 break;
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500207 }
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500208}
209
210bool VsyncSchedule::isHardwareVsyncAllowed(bool makeAllowed) {
211 std::lock_guard<std::mutex> lock(mHwVsyncLock);
212 if (makeAllowed && mHwVsyncState == HwVsyncState::Disallowed) {
213 mHwVsyncState = HwVsyncState::Disabled;
214 }
215 return mHwVsyncState != HwVsyncState::Disallowed;
216}
217
218void VsyncSchedule::setPendingHardwareVsyncState(bool enabled) {
219 mPendingHwVsyncState = enabled ? HwVsyncState::Enabled : HwVsyncState::Disabled;
220}
221
222bool VsyncSchedule::getPendingHardwareVsyncState() const {
223 return mPendingHwVsyncState == HwVsyncState::Enabled;
224}
225
Dominik Laskowski068173d2021-08-11 17:22:59 -0700226} // namespace android::scheduler