blob: 951c1eca256e1ea8ce54874bc1d9860c4b1abf87 [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
Dominik Laskowski068173d2021-08-11 17:22:59 -070019#include <scheduler/Fps.h>
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080020#include <scheduler/Timer.h>
Dominik Laskowski068173d2021-08-11 17:22:59 -070021
22#include "VsyncSchedule.h"
23
Leon Scroggins III31d41412022-11-18 16:42:53 -050024#include "ISchedulerCallback.h"
25#include "Scheduler.h"
26#include "Utils/Dumper.h"
Dominik Laskowski068173d2021-08-11 17:22:59 -070027#include "VSyncDispatchTimerQueue.h"
28#include "VSyncPredictor.h"
29#include "VSyncReactor.h"
30
31#include "../TracedOrdinal.h"
32
33namespace android::scheduler {
34
35class VsyncSchedule::PredictedVsyncTracer {
36 // Invoked from the thread of the VsyncDispatch owned by this VsyncSchedule.
37 constexpr auto makeVsyncCallback() {
38 return [this](nsecs_t, nsecs_t, nsecs_t) {
39 mParity = !mParity;
40 schedule();
41 };
42 }
43
44public:
Leon Scroggins III31d41412022-11-18 16:42:53 -050045 explicit PredictedVsyncTracer(std::shared_ptr<VsyncDispatch> dispatch)
46 : mRegistration(std::move(dispatch), makeVsyncCallback(), __func__) {
Dominik Laskowski068173d2021-08-11 17:22:59 -070047 schedule();
48 }
49
50private:
51 void schedule() { mRegistration.schedule({0, 0, 0}); }
52
53 TracedOrdinal<bool> mParity = {"VSYNC-predicted", 0};
54 VSyncCallbackRegistration mRegistration;
55};
56
Leon Scroggins III31d41412022-11-18 16:42:53 -050057VsyncSchedule::VsyncSchedule(PhysicalDisplayId id, FeatureFlags features)
58 : mId(id),
59 mTracker(createTracker(id)),
60 mDispatch(createDispatch(mTracker)),
61 mController(createController(id, *mTracker, features)) {
Dominik Laskowski068173d2021-08-11 17:22:59 -070062 if (features.test(Feature::kTracePredictedVsync)) {
Leon Scroggins III31d41412022-11-18 16:42:53 -050063 mTracer = std::make_unique<PredictedVsyncTracer>(mDispatch);
Dominik Laskowski068173d2021-08-11 17:22:59 -070064 }
65}
66
Leon Scroggins III31d41412022-11-18 16:42:53 -050067VsyncSchedule::VsyncSchedule(PhysicalDisplayId id, TrackerPtr tracker, DispatchPtr dispatch,
68 ControllerPtr controller)
69 : mId(id),
70 mTracker(std::move(tracker)),
Dominik Laskowski068173d2021-08-11 17:22:59 -070071 mDispatch(std::move(dispatch)),
72 mController(std::move(controller)) {}
73
Dominik Laskowski068173d2021-08-11 17:22:59 -070074VsyncSchedule::~VsyncSchedule() = default;
75
Dominik Laskowski5d164f22022-07-07 07:56:07 -070076Period VsyncSchedule::period() const {
77 return Period::fromNs(mTracker->currentPeriod());
78}
79
80TimePoint VsyncSchedule::vsyncDeadlineAfter(TimePoint timePoint) const {
81 return TimePoint::fromNs(mTracker->nextAnticipatedVSyncTimeFrom(timePoint.ns()));
82}
83
Dominik Laskowski068173d2021-08-11 17:22:59 -070084void VsyncSchedule::dump(std::string& out) const {
Leon Scroggins III31d41412022-11-18 16:42:53 -050085 utils::Dumper dumper(out);
86 {
87 std::lock_guard<std::mutex> lock(mHwVsyncLock);
88 dumper.dump("hwVsyncState", ftl::enum_string(mHwVsyncState));
89 dumper.dump("lastHwVsyncState", ftl::enum_string(mLastHwVsyncState));
90 }
91
Dominik Laskowski068173d2021-08-11 17:22:59 -070092 out.append("VsyncController:\n");
93 mController->dump(out);
94
95 out.append("VsyncDispatch:\n");
96 mDispatch->dump(out);
97}
98
Leon Scroggins III31d41412022-11-18 16:42:53 -050099VsyncSchedule::TrackerPtr VsyncSchedule::createTracker(PhysicalDisplayId id) {
Dominik Laskowski068173d2021-08-11 17:22:59 -0700100 // TODO(b/144707443): Tune constants.
101 constexpr nsecs_t kInitialPeriod = (60_Hz).getPeriodNsecs();
102 constexpr size_t kHistorySize = 20;
103 constexpr size_t kMinSamplesForPrediction = 6;
104 constexpr uint32_t kDiscardOutlierPercent = 20;
105
Leon Scroggins III31d41412022-11-18 16:42:53 -0500106 return std::make_unique<VSyncPredictor>(to_string(id), kInitialPeriod, kHistorySize,
107 kMinSamplesForPrediction, kDiscardOutlierPercent);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700108}
109
Leon Scroggins III31d41412022-11-18 16:42:53 -0500110VsyncSchedule::DispatchPtr VsyncSchedule::createDispatch(TrackerPtr tracker) {
Dominik Laskowski068173d2021-08-11 17:22:59 -0700111 using namespace std::chrono_literals;
112
113 // TODO(b/144707443): Tune constants.
114 constexpr std::chrono::nanoseconds kGroupDispatchWithin = 500us;
115 constexpr std::chrono::nanoseconds kSnapToSameVsyncWithin = 3ms;
116
Leon Scroggins III31d41412022-11-18 16:42:53 -0500117 return std::make_unique<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), std::move(tracker),
Dominik Laskowski068173d2021-08-11 17:22:59 -0700118 kGroupDispatchWithin.count(),
119 kSnapToSameVsyncWithin.count());
120}
121
Leon Scroggins III31d41412022-11-18 16:42:53 -0500122VsyncSchedule::ControllerPtr VsyncSchedule::createController(PhysicalDisplayId id,
123 VsyncTracker& tracker,
Dominik Laskowski068173d2021-08-11 17:22:59 -0700124 FeatureFlags features) {
125 // TODO(b/144707443): Tune constants.
126 constexpr size_t kMaxPendingFences = 20;
127 const bool hasKernelIdleTimer = features.test(Feature::kKernelIdleTimer);
128
Leon Scroggins III31d41412022-11-18 16:42:53 -0500129 auto reactor = std::make_unique<VSyncReactor>(to_string(id), std::make_unique<SystemClock>(),
130 tracker, kMaxPendingFences, hasKernelIdleTimer);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700131
132 reactor->setIgnorePresentFences(!features.test(Feature::kPresentFences));
133 return reactor;
134}
135
Leon Scroggins III31d41412022-11-18 16:42:53 -0500136void VsyncSchedule::enableHardwareVsync(ISchedulerCallback& callback) {
137 std::lock_guard<std::mutex> lock(mHwVsyncLock);
138 if (mHwVsyncState == HwVsyncState::Disabled) {
139 getTracker().resetModel();
140 callback.setVsyncEnabled(mId, true);
141 mHwVsyncState = HwVsyncState::Enabled;
142 mLastHwVsyncState = HwVsyncState::Enabled;
143 }
144}
145
146void VsyncSchedule::disableHardwareVsync(ISchedulerCallback& callback, bool disallow) {
147 std::lock_guard<std::mutex> lock(mHwVsyncLock);
148 if (mHwVsyncState == HwVsyncState::Enabled) {
149 callback.setVsyncEnabled(mId, false);
150 mLastHwVsyncState = HwVsyncState::Disabled;
151 }
152 mHwVsyncState = disallow ? HwVsyncState::Disallowed : HwVsyncState::Disabled;
153}
154
155bool VsyncSchedule::isHardwareVsyncAllowed() const {
156 std::lock_guard<std::mutex> lock(mHwVsyncLock);
157 return mHwVsyncState != HwVsyncState::Disallowed;
158}
159
160void VsyncSchedule::allowHardwareVsync() {
161 std::lock_guard<std::mutex> lock(mHwVsyncLock);
162 if (mHwVsyncState == HwVsyncState::Disallowed) {
163 mHwVsyncState = HwVsyncState::Disabled;
164 }
165}
166
Dominik Laskowski068173d2021-08-11 17:22:59 -0700167} // namespace android::scheduler