blob: 1eeccd3e6c26d1089ac6da87ff5a78189d724d24 [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
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
ramindaniae645822024-01-11 10:57:29 -0800113VsyncSchedule::TrackerPtr VsyncSchedule::createTracker(ftl::NonNull<DisplayModePtr> modePtr) {
Dominik Laskowski068173d2021-08-11 17:22:59 -0700114 // TODO(b/144707443): Tune constants.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700115 constexpr size_t kHistorySize = 20;
116 constexpr size_t kMinSamplesForPrediction = 6;
117 constexpr uint32_t kDiscardOutlierPercent = 20;
118
Ady Abrahamc585dba2023-11-15 18:41:35 -0800119 return std::make_unique<VSyncPredictor>(modePtr, kHistorySize, kMinSamplesForPrediction,
ramindaniae645822024-01-11 10:57:29 -0800120 kDiscardOutlierPercent);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700121}
122
Leon Scroggins III67388622023-02-06 20:36:20 -0500123VsyncSchedule::DispatchPtr VsyncSchedule::createDispatch(TrackerPtr tracker) {
Dominik Laskowski068173d2021-08-11 17:22:59 -0700124 using namespace std::chrono_literals;
125
126 // TODO(b/144707443): Tune constants.
127 constexpr std::chrono::nanoseconds kGroupDispatchWithin = 500us;
128 constexpr std::chrono::nanoseconds kSnapToSameVsyncWithin = 3ms;
129
Leon Scroggins III67388622023-02-06 20:36:20 -0500130 return std::make_unique<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), std::move(tracker),
Dominik Laskowski068173d2021-08-11 17:22:59 -0700131 kGroupDispatchWithin.count(),
132 kSnapToSameVsyncWithin.count());
133}
134
Leon Scroggins III67388622023-02-06 20:36:20 -0500135VsyncSchedule::ControllerPtr VsyncSchedule::createController(PhysicalDisplayId id,
136 VsyncTracker& tracker,
Dominik Laskowski068173d2021-08-11 17:22:59 -0700137 FeatureFlags features) {
138 // TODO(b/144707443): Tune constants.
139 constexpr size_t kMaxPendingFences = 20;
140 const bool hasKernelIdleTimer = features.test(Feature::kKernelIdleTimer);
141
Leon Scroggins III67388622023-02-06 20:36:20 -0500142 auto reactor = std::make_unique<VSyncReactor>(id, std::make_unique<SystemClock>(), tracker,
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500143 kMaxPendingFences, hasKernelIdleTimer);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700144
145 reactor->setIgnorePresentFences(!features.test(Feature::kPresentFences));
146 return reactor;
147}
148
Ady Abrahamc585dba2023-11-15 18:41:35 -0800149void VsyncSchedule::onDisplayModeChanged(ftl::NonNull<DisplayModePtr> modePtr, bool force) {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500150 std::lock_guard<std::mutex> lock(mHwVsyncLock);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800151 mController->onDisplayModeChanged(modePtr, force);
Dominik Laskowski66295432023-03-14 12:25:36 -0400152 enableHardwareVsyncLocked();
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500153}
154
Dominik Laskowski66295432023-03-14 12:25:36 -0400155bool VsyncSchedule::addResyncSample(TimePoint timestamp, ftl::Optional<Period> hwcVsyncPeriod) {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500156 bool needsHwVsync = false;
157 bool periodFlushed = false;
158 {
159 std::lock_guard<std::mutex> lock(mHwVsyncLock);
160 if (mHwVsyncState == HwVsyncState::Enabled) {
161 needsHwVsync = mController->addHwVsyncTimestamp(timestamp.ns(),
162 hwcVsyncPeriod.transform(&Period::ns),
163 &periodFlushed);
164 }
165 }
166 if (needsHwVsync) {
Dominik Laskowski66295432023-03-14 12:25:36 -0400167 enableHardwareVsync();
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500168 } else {
Dominik Laskowski66295432023-03-14 12:25:36 -0400169 constexpr bool kDisallow = false;
170 disableHardwareVsync(kDisallow);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500171 }
172 return periodFlushed;
173}
174
Dominik Laskowski66295432023-03-14 12:25:36 -0400175void VsyncSchedule::enableHardwareVsync() {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500176 std::lock_guard<std::mutex> lock(mHwVsyncLock);
Dominik Laskowski66295432023-03-14 12:25:36 -0400177 enableHardwareVsyncLocked();
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500178}
179
Dominik Laskowski66295432023-03-14 12:25:36 -0400180void VsyncSchedule::enableHardwareVsyncLocked() {
Ady Abrahamf0b2bf92023-12-13 23:36:35 +0000181 ATRACE_CALL();
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) {
Ady Abrahamf0b2bf92023-12-13 23:36:35 +0000190 ATRACE_CALL();
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500191 std::lock_guard<std::mutex> lock(mHwVsyncLock);
Leon Scroggins IIIaadc62d2023-03-03 11:07:50 -0500192 switch (mHwVsyncState) {
193 case HwVsyncState::Enabled:
Dominik Laskowski66295432023-03-14 12:25:36 -0400194 mRequestHardwareVsync(mId, false);
Leon Scroggins IIIaadc62d2023-03-03 11:07:50 -0500195 [[fallthrough]];
196 case HwVsyncState::Disabled:
197 mHwVsyncState = disallow ? HwVsyncState::Disallowed : HwVsyncState::Disabled;
198 break;
199 case HwVsyncState::Disallowed:
200 break;
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500201 }
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500202}
203
204bool VsyncSchedule::isHardwareVsyncAllowed(bool makeAllowed) {
205 std::lock_guard<std::mutex> lock(mHwVsyncLock);
206 if (makeAllowed && mHwVsyncState == HwVsyncState::Disallowed) {
207 mHwVsyncState = HwVsyncState::Disabled;
208 }
209 return mHwVsyncState != HwVsyncState::Disallowed;
210}
211
212void VsyncSchedule::setPendingHardwareVsyncState(bool enabled) {
213 mPendingHwVsyncState = enabled ? HwVsyncState::Enabled : HwVsyncState::Disabled;
214}
215
216bool VsyncSchedule::getPendingHardwareVsyncState() const {
217 return mPendingHwVsyncState == HwVsyncState::Enabled;
218}
219
Dominik Laskowski068173d2021-08-11 17:22:59 -0700220} // namespace android::scheduler