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 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 19 | #include <scheduler/Fps.h> |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 20 | #include <scheduler/Timer.h> |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 21 | |
| 22 | #include "VsyncSchedule.h" |
| 23 | |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 24 | #include "ISchedulerCallback.h" |
| 25 | #include "Scheduler.h" |
| 26 | #include "Utils/Dumper.h" |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 27 | #include "VSyncDispatchTimerQueue.h" |
| 28 | #include "VSyncPredictor.h" |
| 29 | #include "VSyncReactor.h" |
| 30 | |
| 31 | #include "../TracedOrdinal.h" |
| 32 | |
| 33 | namespace android::scheduler { |
| 34 | |
| 35 | class 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 | |
| 44 | public: |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 45 | explicit PredictedVsyncTracer(std::shared_ptr<VsyncDispatch> dispatch) |
| 46 | : mRegistration(std::move(dispatch), makeVsyncCallback(), __func__) { |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 47 | schedule(); |
| 48 | } |
| 49 | |
| 50 | private: |
| 51 | void schedule() { mRegistration.schedule({0, 0, 0}); } |
| 52 | |
| 53 | TracedOrdinal<bool> mParity = {"VSYNC-predicted", 0}; |
| 54 | VSyncCallbackRegistration mRegistration; |
| 55 | }; |
| 56 | |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 57 | VsyncSchedule::VsyncSchedule(PhysicalDisplayId id, FeatureFlags features) |
| 58 | : mId(id), |
| 59 | mTracker(createTracker(id)), |
| 60 | mDispatch(createDispatch(mTracker)), |
| 61 | mController(createController(id, *mTracker, features)) { |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 62 | if (features.test(Feature::kTracePredictedVsync)) { |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 63 | mTracer = std::make_unique<PredictedVsyncTracer>(mDispatch); |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 67 | VsyncSchedule::VsyncSchedule(PhysicalDisplayId id, TrackerPtr tracker, DispatchPtr dispatch, |
| 68 | ControllerPtr controller) |
| 69 | : mId(id), |
| 70 | mTracker(std::move(tracker)), |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 71 | mDispatch(std::move(dispatch)), |
| 72 | mController(std::move(controller)) {} |
| 73 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 74 | VsyncSchedule::~VsyncSchedule() = default; |
| 75 | |
Dominik Laskowski | 5d164f2 | 2022-07-07 07:56:07 -0700 | [diff] [blame] | 76 | Period VsyncSchedule::period() const { |
| 77 | return Period::fromNs(mTracker->currentPeriod()); |
| 78 | } |
| 79 | |
| 80 | TimePoint VsyncSchedule::vsyncDeadlineAfter(TimePoint timePoint) const { |
| 81 | return TimePoint::fromNs(mTracker->nextAnticipatedVSyncTimeFrom(timePoint.ns())); |
| 82 | } |
| 83 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 84 | void VsyncSchedule::dump(std::string& out) const { |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 85 | 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 Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 92 | out.append("VsyncController:\n"); |
| 93 | mController->dump(out); |
| 94 | |
| 95 | out.append("VsyncDispatch:\n"); |
| 96 | mDispatch->dump(out); |
| 97 | } |
| 98 | |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 99 | VsyncSchedule::TrackerPtr VsyncSchedule::createTracker(PhysicalDisplayId id) { |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 100 | // 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 III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 106 | return std::make_unique<VSyncPredictor>(to_string(id), kInitialPeriod, kHistorySize, |
| 107 | kMinSamplesForPrediction, kDiscardOutlierPercent); |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 110 | VsyncSchedule::DispatchPtr VsyncSchedule::createDispatch(TrackerPtr tracker) { |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 111 | 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 III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 117 | return std::make_unique<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), std::move(tracker), |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 118 | kGroupDispatchWithin.count(), |
| 119 | kSnapToSameVsyncWithin.count()); |
| 120 | } |
| 121 | |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 122 | VsyncSchedule::ControllerPtr VsyncSchedule::createController(PhysicalDisplayId id, |
| 123 | VsyncTracker& tracker, |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 124 | 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 III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 129 | auto reactor = std::make_unique<VSyncReactor>(to_string(id), std::make_unique<SystemClock>(), |
| 130 | tracker, kMaxPendingFences, hasKernelIdleTimer); |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 131 | |
| 132 | reactor->setIgnorePresentFences(!features.test(Feature::kPresentFences)); |
| 133 | return reactor; |
| 134 | } |
| 135 | |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 136 | void 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 | |
| 146 | void 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 | |
| 155 | bool VsyncSchedule::isHardwareVsyncAllowed() const { |
| 156 | std::lock_guard<std::mutex> lock(mHwVsyncLock); |
| 157 | return mHwVsyncState != HwVsyncState::Disallowed; |
| 158 | } |
| 159 | |
| 160 | void VsyncSchedule::allowHardwareVsync() { |
| 161 | std::lock_guard<std::mutex> lock(mHwVsyncLock); |
| 162 | if (mHwVsyncState == HwVsyncState::Disallowed) { |
| 163 | mHwVsyncState = HwVsyncState::Disabled; |
| 164 | } |
| 165 | } |
| 166 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 167 | } // namespace android::scheduler |