Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 1 | /* |
| 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 Abraham | c621d8d | 2022-06-22 17:01:25 +0000 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 18 | |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 19 | #include <ftl/fake_guard.h> |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 20 | #include <scheduler/Fps.h> |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 21 | #include <scheduler/Timer.h> |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 22 | |
| 23 | #include "VsyncSchedule.h" |
| 24 | |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 25 | #include "Utils/Dumper.h" |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 26 | #include "VSyncDispatchTimerQueue.h" |
| 27 | #include "VSyncPredictor.h" |
| 28 | #include "VSyncReactor.h" |
| 29 | |
| 30 | #include "../TracedOrdinal.h" |
| 31 | |
| 32 | namespace android::scheduler { |
| 33 | |
| 34 | class 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 | |
| 43 | public: |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 44 | explicit PredictedVsyncTracer(std::shared_ptr<VsyncDispatch> dispatch) |
| 45 | : mRegistration(std::move(dispatch), makeVsyncCallback(), __func__) { |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 46 | schedule(); |
| 47 | } |
| 48 | |
| 49 | private: |
| 50 | void schedule() { mRegistration.schedule({0, 0, 0}); } |
| 51 | |
| 52 | TracedOrdinal<bool> mParity = {"VSYNC-predicted", 0}; |
| 53 | VSyncCallbackRegistration mRegistration; |
| 54 | }; |
| 55 | |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 56 | VsyncSchedule::VsyncSchedule(PhysicalDisplayId id, FeatureFlags features, |
ramindani | d4354a9 | 2023-10-02 15:11:09 -0700 | [diff] [blame] | 57 | RequestHardwareVsync requestHardwareVsync, |
| 58 | IVsyncTrackerCallback& callback) |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 59 | : mId(id), |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 60 | mRequestHardwareVsync(std::move(requestHardwareVsync)), |
ramindani | d4354a9 | 2023-10-02 15:11:09 -0700 | [diff] [blame] | 61 | mTracker(createTracker(id, callback)), |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 62 | mDispatch(createDispatch(mTracker)), |
| 63 | mController(createController(id, *mTracker, features)), |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 64 | mTracer(features.test(Feature::kTracePredictedVsync) |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 65 | ? std::make_unique<PredictedVsyncTracer>(mDispatch) |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 66 | : nullptr) {} |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 67 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 68 | VsyncSchedule::VsyncSchedule(PhysicalDisplayId id, TrackerPtr tracker, DispatchPtr dispatch, |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 69 | ControllerPtr controller, RequestHardwareVsync requestHardwareVsync) |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 70 | : mId(id), |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 71 | mRequestHardwareVsync(std::move(requestHardwareVsync)), |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 72 | mTracker(std::move(tracker)), |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 73 | mDispatch(std::move(dispatch)), |
| 74 | mController(std::move(controller)) {} |
| 75 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 76 | VsyncSchedule::~VsyncSchedule() = default; |
| 77 | |
Dominik Laskowski | 5d164f2 | 2022-07-07 07:56:07 -0700 | [diff] [blame] | 78 | Period VsyncSchedule::period() const { |
| 79 | return Period::fromNs(mTracker->currentPeriod()); |
| 80 | } |
| 81 | |
| 82 | TimePoint VsyncSchedule::vsyncDeadlineAfter(TimePoint timePoint) const { |
| 83 | return TimePoint::fromNs(mTracker->nextAnticipatedVSyncTimeFrom(timePoint.ns())); |
| 84 | } |
| 85 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 86 | void VsyncSchedule::dump(std::string& out) const { |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 87 | utils::Dumper dumper(out); |
| 88 | { |
| 89 | std::lock_guard<std::mutex> lock(mHwVsyncLock); |
| 90 | dumper.dump("hwVsyncState", ftl::enum_string(mHwVsyncState)); |
| 91 | |
| 92 | ftl::FakeGuard guard(kMainThreadContext); |
| 93 | dumper.dump("pendingHwVsyncState", ftl::enum_string(mPendingHwVsyncState)); |
| 94 | dumper.eol(); |
| 95 | } |
| 96 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 97 | out.append("VsyncController:\n"); |
| 98 | mController->dump(out); |
| 99 | |
| 100 | out.append("VsyncDispatch:\n"); |
| 101 | mDispatch->dump(out); |
| 102 | } |
| 103 | |
ramindani | d4354a9 | 2023-10-02 15:11:09 -0700 | [diff] [blame] | 104 | VsyncSchedule::TrackerPtr VsyncSchedule::createTracker(PhysicalDisplayId id, |
| 105 | IVsyncTrackerCallback& callback) { |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 106 | // TODO(b/144707443): Tune constants. |
| 107 | constexpr nsecs_t kInitialPeriod = (60_Hz).getPeriodNsecs(); |
| 108 | constexpr size_t kHistorySize = 20; |
| 109 | constexpr size_t kMinSamplesForPrediction = 6; |
| 110 | constexpr uint32_t kDiscardOutlierPercent = 20; |
| 111 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 112 | return std::make_unique<VSyncPredictor>(id, kInitialPeriod, kHistorySize, |
ramindani | d4354a9 | 2023-10-02 15:11:09 -0700 | [diff] [blame] | 113 | kMinSamplesForPrediction, kDiscardOutlierPercent, |
| 114 | callback); |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 117 | VsyncSchedule::DispatchPtr VsyncSchedule::createDispatch(TrackerPtr tracker) { |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 118 | 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 III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 124 | return std::make_unique<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), std::move(tracker), |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 125 | kGroupDispatchWithin.count(), |
| 126 | kSnapToSameVsyncWithin.count()); |
| 127 | } |
| 128 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 129 | VsyncSchedule::ControllerPtr VsyncSchedule::createController(PhysicalDisplayId id, |
| 130 | VsyncTracker& tracker, |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 131 | 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 III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 136 | auto reactor = std::make_unique<VSyncReactor>(id, std::make_unique<SystemClock>(), tracker, |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 137 | kMaxPendingFences, hasKernelIdleTimer); |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 138 | |
| 139 | reactor->setIgnorePresentFences(!features.test(Feature::kPresentFences)); |
| 140 | return reactor; |
| 141 | } |
| 142 | |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 143 | void VsyncSchedule::startPeriodTransition(Period period, bool force) { |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 144 | std::lock_guard<std::mutex> lock(mHwVsyncLock); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 145 | mController->startPeriodTransition(period.ns(), force); |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 146 | enableHardwareVsyncLocked(); |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 147 | } |
| 148 | |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 149 | bool VsyncSchedule::addResyncSample(TimePoint timestamp, ftl::Optional<Period> hwcVsyncPeriod) { |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 150 | 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 Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 161 | enableHardwareVsync(); |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 162 | } else { |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 163 | constexpr bool kDisallow = false; |
| 164 | disableHardwareVsync(kDisallow); |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 165 | } |
| 166 | return periodFlushed; |
| 167 | } |
| 168 | |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 169 | void VsyncSchedule::enableHardwareVsync() { |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 170 | std::lock_guard<std::mutex> lock(mHwVsyncLock); |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 171 | enableHardwareVsyncLocked(); |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 172 | } |
| 173 | |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 174 | void VsyncSchedule::enableHardwareVsyncLocked() { |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 175 | if (mHwVsyncState == HwVsyncState::Disabled) { |
Rachel Lee | fe3af7c | 2023-06-20 21:11:43 +0000 | [diff] [blame] | 176 | getTracker().resetModel(); |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 177 | mRequestHardwareVsync(mId, true); |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 178 | mHwVsyncState = HwVsyncState::Enabled; |
| 179 | } |
| 180 | } |
| 181 | |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 182 | void VsyncSchedule::disableHardwareVsync(bool disallow) { |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 183 | std::lock_guard<std::mutex> lock(mHwVsyncLock); |
Leon Scroggins III | aadc62d | 2023-03-03 11:07:50 -0500 | [diff] [blame] | 184 | switch (mHwVsyncState) { |
| 185 | case HwVsyncState::Enabled: |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 186 | mRequestHardwareVsync(mId, false); |
Leon Scroggins III | aadc62d | 2023-03-03 11:07:50 -0500 | [diff] [blame] | 187 | [[fallthrough]]; |
| 188 | case HwVsyncState::Disabled: |
| 189 | mHwVsyncState = disallow ? HwVsyncState::Disallowed : HwVsyncState::Disabled; |
| 190 | break; |
| 191 | case HwVsyncState::Disallowed: |
| 192 | break; |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 193 | } |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | bool 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 | |
| 204 | void VsyncSchedule::setPendingHardwareVsyncState(bool enabled) { |
| 205 | mPendingHwVsyncState = enabled ? HwVsyncState::Enabled : HwVsyncState::Disabled; |
| 206 | } |
| 207 | |
| 208 | bool VsyncSchedule::getPendingHardwareVsyncState() const { |
| 209 | return mPendingHwVsyncState == HwVsyncState::Enabled; |
| 210 | } |
| 211 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 212 | } // namespace android::scheduler |