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 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 24 | #include "VSyncDispatchTimerQueue.h" |
| 25 | #include "VSyncPredictor.h" |
| 26 | #include "VSyncReactor.h" |
| 27 | |
| 28 | #include "../TracedOrdinal.h" |
| 29 | |
| 30 | namespace android::scheduler { |
| 31 | |
| 32 | class VsyncSchedule::PredictedVsyncTracer { |
| 33 | // Invoked from the thread of the VsyncDispatch owned by this VsyncSchedule. |
| 34 | constexpr auto makeVsyncCallback() { |
| 35 | return [this](nsecs_t, nsecs_t, nsecs_t) { |
| 36 | mParity = !mParity; |
| 37 | schedule(); |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | public: |
| 42 | explicit PredictedVsyncTracer(VsyncDispatch& dispatch) |
| 43 | : mRegistration(dispatch, makeVsyncCallback(), __func__) { |
| 44 | schedule(); |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | void schedule() { mRegistration.schedule({0, 0, 0}); } |
| 49 | |
| 50 | TracedOrdinal<bool> mParity = {"VSYNC-predicted", 0}; |
| 51 | VSyncCallbackRegistration mRegistration; |
| 52 | }; |
| 53 | |
| 54 | VsyncSchedule::VsyncSchedule(FeatureFlags features) |
| 55 | : mTracker(createTracker()), |
| 56 | mDispatch(createDispatch(*mTracker)), |
| 57 | mController(createController(*mTracker, features)) { |
| 58 | if (features.test(Feature::kTracePredictedVsync)) { |
| 59 | mTracer = std::make_unique<PredictedVsyncTracer>(*mDispatch); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | VsyncSchedule::VsyncSchedule(TrackerPtr tracker, DispatchPtr dispatch, ControllerPtr controller) |
| 64 | : mTracker(std::move(tracker)), |
| 65 | mDispatch(std::move(dispatch)), |
| 66 | mController(std::move(controller)) {} |
| 67 | |
| 68 | VsyncSchedule::VsyncSchedule(VsyncSchedule&&) = default; |
| 69 | VsyncSchedule::~VsyncSchedule() = default; |
| 70 | |
Dominik Laskowski | 5d164f2 | 2022-07-07 07:56:07 -0700 | [diff] [blame^] | 71 | Period VsyncSchedule::period() const { |
| 72 | return Period::fromNs(mTracker->currentPeriod()); |
| 73 | } |
| 74 | |
| 75 | TimePoint VsyncSchedule::vsyncDeadlineAfter(TimePoint timePoint) const { |
| 76 | return TimePoint::fromNs(mTracker->nextAnticipatedVSyncTimeFrom(timePoint.ns())); |
| 77 | } |
| 78 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 79 | void VsyncSchedule::dump(std::string& out) const { |
| 80 | out.append("VsyncController:\n"); |
| 81 | mController->dump(out); |
| 82 | |
| 83 | out.append("VsyncDispatch:\n"); |
| 84 | mDispatch->dump(out); |
| 85 | } |
| 86 | |
| 87 | VsyncSchedule::TrackerPtr VsyncSchedule::createTracker() { |
| 88 | // TODO(b/144707443): Tune constants. |
| 89 | constexpr nsecs_t kInitialPeriod = (60_Hz).getPeriodNsecs(); |
| 90 | constexpr size_t kHistorySize = 20; |
| 91 | constexpr size_t kMinSamplesForPrediction = 6; |
| 92 | constexpr uint32_t kDiscardOutlierPercent = 20; |
| 93 | |
| 94 | return std::make_unique<VSyncPredictor>(kInitialPeriod, kHistorySize, kMinSamplesForPrediction, |
| 95 | kDiscardOutlierPercent); |
| 96 | } |
| 97 | |
| 98 | VsyncSchedule::DispatchPtr VsyncSchedule::createDispatch(VsyncTracker& tracker) { |
| 99 | using namespace std::chrono_literals; |
| 100 | |
| 101 | // TODO(b/144707443): Tune constants. |
| 102 | constexpr std::chrono::nanoseconds kGroupDispatchWithin = 500us; |
| 103 | constexpr std::chrono::nanoseconds kSnapToSameVsyncWithin = 3ms; |
| 104 | |
| 105 | return std::make_unique<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), tracker, |
| 106 | kGroupDispatchWithin.count(), |
| 107 | kSnapToSameVsyncWithin.count()); |
| 108 | } |
| 109 | |
| 110 | VsyncSchedule::ControllerPtr VsyncSchedule::createController(VsyncTracker& tracker, |
| 111 | FeatureFlags features) { |
| 112 | // TODO(b/144707443): Tune constants. |
| 113 | constexpr size_t kMaxPendingFences = 20; |
| 114 | const bool hasKernelIdleTimer = features.test(Feature::kKernelIdleTimer); |
| 115 | |
| 116 | auto reactor = std::make_unique<VSyncReactor>(std::make_unique<SystemClock>(), tracker, |
| 117 | kMaxPendingFences, hasKernelIdleTimer); |
| 118 | |
| 119 | reactor->setIgnorePresentFences(!features.test(Feature::kPresentFences)); |
| 120 | return reactor; |
| 121 | } |
| 122 | |
| 123 | } // namespace android::scheduler |