blob: ff3f29dbbf4a39bb83cf4d7e20742c0e18bc5f16 [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
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050019#include <ftl/fake_guard.h>
Dominik Laskowski068173d2021-08-11 17:22:59 -070020#include <scheduler/Fps.h>
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080021#include <scheduler/Timer.h>
Dominik Laskowski068173d2021-08-11 17:22:59 -070022
23#include "VsyncSchedule.h"
24
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050025#include "Utils/Dumper.h"
Dominik Laskowski068173d2021-08-11 17:22:59 -070026#include "VSyncDispatchTimerQueue.h"
27#include "VSyncPredictor.h"
28#include "VSyncReactor.h"
29
30#include "../TracedOrdinal.h"
31
32namespace android::scheduler {
33
34class VsyncSchedule::PredictedVsyncTracer {
35 // Invoked from the thread of the VsyncDispatch owned by this VsyncSchedule.
36 constexpr auto makeVsyncCallback() {
37 return [this](nsecs_t, nsecs_t, nsecs_t) {
38 mParity = !mParity;
39 schedule();
40 };
41 }
42
43public:
Leon Scroggins III67388622023-02-06 20:36:20 -050044 explicit PredictedVsyncTracer(std::shared_ptr<VsyncDispatch> dispatch)
45 : mRegistration(std::move(dispatch), makeVsyncCallback(), __func__) {
Dominik Laskowski068173d2021-08-11 17:22:59 -070046 schedule();
47 }
48
49private:
50 void schedule() { mRegistration.schedule({0, 0, 0}); }
51
52 TracedOrdinal<bool> mParity = {"VSYNC-predicted", 0};
53 VSyncCallbackRegistration mRegistration;
54};
55
Dominik Laskowski66295432023-03-14 12:25:36 -040056VsyncSchedule::VsyncSchedule(PhysicalDisplayId id, FeatureFlags features,
57 RequestHardwareVsync requestHardwareVsync)
Leon Scroggins III67388622023-02-06 20:36:20 -050058 : mId(id),
Dominik Laskowski66295432023-03-14 12:25:36 -040059 mRequestHardwareVsync(std::move(requestHardwareVsync)),
Leon Scroggins III67388622023-02-06 20:36:20 -050060 mTracker(createTracker(id)),
61 mDispatch(createDispatch(mTracker)),
62 mController(createController(id, *mTracker, features)),
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050063 mTracer(features.test(Feature::kTracePredictedVsync)
Leon Scroggins III67388622023-02-06 20:36:20 -050064 ? std::make_unique<PredictedVsyncTracer>(mDispatch)
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050065 : nullptr) {}
Dominik Laskowski068173d2021-08-11 17:22:59 -070066
Leon Scroggins III67388622023-02-06 20:36:20 -050067VsyncSchedule::VsyncSchedule(PhysicalDisplayId id, TrackerPtr tracker, DispatchPtr dispatch,
Dominik Laskowski66295432023-03-14 12:25:36 -040068 ControllerPtr controller, RequestHardwareVsync requestHardwareVsync)
Leon Scroggins III67388622023-02-06 20:36:20 -050069 : mId(id),
Dominik Laskowski66295432023-03-14 12:25:36 -040070 mRequestHardwareVsync(std::move(requestHardwareVsync)),
Leon Scroggins III67388622023-02-06 20:36:20 -050071 mTracker(std::move(tracker)),
Dominik Laskowski068173d2021-08-11 17:22:59 -070072 mDispatch(std::move(dispatch)),
73 mController(std::move(controller)) {}
74
Dominik Laskowski068173d2021-08-11 17:22:59 -070075VsyncSchedule::~VsyncSchedule() = default;
76
Dominik Laskowski5d164f22022-07-07 07:56:07 -070077Period VsyncSchedule::period() const {
78 return Period::fromNs(mTracker->currentPeriod());
79}
80
81TimePoint VsyncSchedule::vsyncDeadlineAfter(TimePoint timePoint) const {
82 return TimePoint::fromNs(mTracker->nextAnticipatedVSyncTimeFrom(timePoint.ns()));
83}
84
Dominik Laskowski068173d2021-08-11 17:22:59 -070085void VsyncSchedule::dump(std::string& out) const {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050086 utils::Dumper dumper(out);
87 {
88 std::lock_guard<std::mutex> lock(mHwVsyncLock);
89 dumper.dump("hwVsyncState", ftl::enum_string(mHwVsyncState));
90
91 ftl::FakeGuard guard(kMainThreadContext);
92 dumper.dump("pendingHwVsyncState", ftl::enum_string(mPendingHwVsyncState));
93 dumper.eol();
94 }
95
Dominik Laskowski068173d2021-08-11 17:22:59 -070096 out.append("VsyncController:\n");
97 mController->dump(out);
98
99 out.append("VsyncDispatch:\n");
100 mDispatch->dump(out);
101}
102
Leon Scroggins III67388622023-02-06 20:36:20 -0500103VsyncSchedule::TrackerPtr VsyncSchedule::createTracker(PhysicalDisplayId id) {
Dominik Laskowski068173d2021-08-11 17:22:59 -0700104 // TODO(b/144707443): Tune constants.
105 constexpr nsecs_t kInitialPeriod = (60_Hz).getPeriodNsecs();
106 constexpr size_t kHistorySize = 20;
107 constexpr size_t kMinSamplesForPrediction = 6;
108 constexpr uint32_t kDiscardOutlierPercent = 20;
109
Leon Scroggins III67388622023-02-06 20:36:20 -0500110 return std::make_unique<VSyncPredictor>(id, kInitialPeriod, kHistorySize,
111 kMinSamplesForPrediction, kDiscardOutlierPercent);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700112}
113
Leon Scroggins III67388622023-02-06 20:36:20 -0500114VsyncSchedule::DispatchPtr VsyncSchedule::createDispatch(TrackerPtr tracker) {
Dominik Laskowski068173d2021-08-11 17:22:59 -0700115 using namespace std::chrono_literals;
116
117 // TODO(b/144707443): Tune constants.
118 constexpr std::chrono::nanoseconds kGroupDispatchWithin = 500us;
119 constexpr std::chrono::nanoseconds kSnapToSameVsyncWithin = 3ms;
120
Leon Scroggins III67388622023-02-06 20:36:20 -0500121 return std::make_unique<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), std::move(tracker),
Dominik Laskowski068173d2021-08-11 17:22:59 -0700122 kGroupDispatchWithin.count(),
123 kSnapToSameVsyncWithin.count());
124}
125
Leon Scroggins III67388622023-02-06 20:36:20 -0500126VsyncSchedule::ControllerPtr VsyncSchedule::createController(PhysicalDisplayId id,
127 VsyncTracker& tracker,
Dominik Laskowski068173d2021-08-11 17:22:59 -0700128 FeatureFlags features) {
129 // TODO(b/144707443): Tune constants.
130 constexpr size_t kMaxPendingFences = 20;
131 const bool hasKernelIdleTimer = features.test(Feature::kKernelIdleTimer);
132
Leon Scroggins III67388622023-02-06 20:36:20 -0500133 auto reactor = std::make_unique<VSyncReactor>(id, std::make_unique<SystemClock>(), tracker,
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500134 kMaxPendingFences, hasKernelIdleTimer);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700135
136 reactor->setIgnorePresentFences(!features.test(Feature::kPresentFences));
137 return reactor;
138}
139
Dominik Laskowski66295432023-03-14 12:25:36 -0400140void VsyncSchedule::startPeriodTransition(Period period, bool force) {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500141 std::lock_guard<std::mutex> lock(mHwVsyncLock);
Leon Scroggins III67388622023-02-06 20:36:20 -0500142 mController->startPeriodTransition(period.ns(), force);
Dominik Laskowski66295432023-03-14 12:25:36 -0400143 enableHardwareVsyncLocked();
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500144}
145
Dominik Laskowski66295432023-03-14 12:25:36 -0400146bool VsyncSchedule::addResyncSample(TimePoint timestamp, ftl::Optional<Period> hwcVsyncPeriod) {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500147 bool needsHwVsync = false;
148 bool periodFlushed = false;
149 {
150 std::lock_guard<std::mutex> lock(mHwVsyncLock);
151 if (mHwVsyncState == HwVsyncState::Enabled) {
152 needsHwVsync = mController->addHwVsyncTimestamp(timestamp.ns(),
153 hwcVsyncPeriod.transform(&Period::ns),
154 &periodFlushed);
155 }
156 }
157 if (needsHwVsync) {
Dominik Laskowski66295432023-03-14 12:25:36 -0400158 enableHardwareVsync();
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500159 } else {
Dominik Laskowski66295432023-03-14 12:25:36 -0400160 constexpr bool kDisallow = false;
161 disableHardwareVsync(kDisallow);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500162 }
163 return periodFlushed;
164}
165
Dominik Laskowski66295432023-03-14 12:25:36 -0400166void VsyncSchedule::enableHardwareVsync() {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500167 std::lock_guard<std::mutex> lock(mHwVsyncLock);
Dominik Laskowski66295432023-03-14 12:25:36 -0400168 enableHardwareVsyncLocked();
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500169}
170
Dominik Laskowski66295432023-03-14 12:25:36 -0400171void VsyncSchedule::enableHardwareVsyncLocked() {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500172 if (mHwVsyncState == HwVsyncState::Disabled) {
Rachel Leefe3af7c2023-06-20 21:11:43 +0000173 getTracker().resetModel();
Dominik Laskowski66295432023-03-14 12:25:36 -0400174 mRequestHardwareVsync(mId, true);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500175 mHwVsyncState = HwVsyncState::Enabled;
176 }
177}
178
Dominik Laskowski66295432023-03-14 12:25:36 -0400179void VsyncSchedule::disableHardwareVsync(bool disallow) {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500180 std::lock_guard<std::mutex> lock(mHwVsyncLock);
Leon Scroggins IIIaadc62d2023-03-03 11:07:50 -0500181 switch (mHwVsyncState) {
182 case HwVsyncState::Enabled:
Dominik Laskowski66295432023-03-14 12:25:36 -0400183 mRequestHardwareVsync(mId, false);
Leon Scroggins IIIaadc62d2023-03-03 11:07:50 -0500184 [[fallthrough]];
185 case HwVsyncState::Disabled:
186 mHwVsyncState = disallow ? HwVsyncState::Disallowed : HwVsyncState::Disabled;
187 break;
188 case HwVsyncState::Disallowed:
189 break;
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500190 }
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500191}
192
193bool VsyncSchedule::isHardwareVsyncAllowed(bool makeAllowed) {
194 std::lock_guard<std::mutex> lock(mHwVsyncLock);
195 if (makeAllowed && mHwVsyncState == HwVsyncState::Disallowed) {
196 mHwVsyncState = HwVsyncState::Disabled;
197 }
198 return mHwVsyncState != HwVsyncState::Disallowed;
199}
200
201void VsyncSchedule::setPendingHardwareVsyncState(bool enabled) {
202 mPendingHwVsyncState = enabled ? HwVsyncState::Enabled : HwVsyncState::Disabled;
203}
204
205bool VsyncSchedule::getPendingHardwareVsyncState() const {
206 return mPendingHwVsyncState == HwVsyncState::Enabled;
207}
208
Dominik Laskowski068173d2021-08-11 17:22:59 -0700209} // namespace android::scheduler