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 | |
| 17 | #pragma once |
| 18 | |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 19 | #include <functional> |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 20 | #include <memory> |
| 21 | #include <string> |
| 22 | |
Leon Scroggins III | 4d5db7a | 2023-02-13 15:24:20 -0500 | [diff] [blame] | 23 | #include <android-base/thread_annotations.h> |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 24 | #include <ftl/enum.h> |
| 25 | #include <ftl/optional.h> |
Dominik Laskowski | e85d7fa | 2023-05-15 10:50:22 -0400 | [diff] [blame] | 26 | #include <ui/DisplayId.h> |
Dominik Laskowski | c183eed | 2023-01-21 10:02:15 -0500 | [diff] [blame] | 27 | |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame^] | 28 | #include <scheduler/Features.h> |
| 29 | #include <scheduler/IVsyncSource.h> |
| 30 | #include <scheduler/Time.h> |
| 31 | |
| 32 | #include "ThreadContext.h" |
| 33 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 34 | namespace android { |
| 35 | class EventThreadTest; |
Leon Scroggins III | 4d5db7a | 2023-02-13 15:24:20 -0500 | [diff] [blame] | 36 | class VsyncScheduleTest; |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | namespace android::fuzz { |
| 40 | class SchedulerFuzzer; |
| 41 | } |
| 42 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 43 | namespace android::scheduler { |
| 44 | |
| 45 | // TODO(b/185535769): Rename classes, and remove aliases. |
| 46 | class VSyncDispatch; |
| 47 | class VSyncTracker; |
| 48 | |
| 49 | class VsyncController; |
| 50 | using VsyncDispatch = VSyncDispatch; |
| 51 | using VsyncTracker = VSyncTracker; |
| 52 | |
| 53 | // Schedule that synchronizes to hardware VSYNC of a physical display. |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame^] | 54 | class VsyncSchedule final : public IVsyncSource { |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 55 | public: |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 56 | using RequestHardwareVsync = std::function<void(PhysicalDisplayId, bool enabled)>; |
| 57 | |
| 58 | VsyncSchedule(PhysicalDisplayId, FeatureFlags, RequestHardwareVsync); |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 59 | ~VsyncSchedule(); |
| 60 | |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame^] | 61 | // IVsyncSource overrides: |
| 62 | Period period() const override; |
| 63 | TimePoint vsyncDeadlineAfter(TimePoint) const override; |
Dominik Laskowski | 5d164f2 | 2022-07-07 07:56:07 -0700 | [diff] [blame] | 64 | |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 65 | // Inform the schedule that the period is changing and the schedule needs to recalibrate |
| 66 | // itself. The schedule will end the period transition internally. This will |
| 67 | // enable hardware VSYNCs in order to calibrate. |
| 68 | // |
| 69 | // \param [in] period The period that the system is changing into. |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 70 | // \param [in] force True to force a transition even if it is not a |
| 71 | // change. |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 72 | void startPeriodTransition(Period period, bool force); |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 73 | |
| 74 | // Pass a VSYNC sample to VsyncController. Return true if |
| 75 | // VsyncController detected that the VSYNC period changed. Enable or disable |
| 76 | // hardware VSYNCs depending on whether more samples are needed. |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 77 | bool addResyncSample(TimePoint timestamp, ftl::Optional<Period> hwcVsyncPeriod); |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 78 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 79 | // TODO(b/185535769): Hide behind API. |
| 80 | const VsyncTracker& getTracker() const { return *mTracker; } |
| 81 | VsyncTracker& getTracker() { return *mTracker; } |
| 82 | VsyncController& getController() { return *mController; } |
| 83 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 84 | // TODO(b/185535769): Once these are hidden behind the API, they may no |
| 85 | // longer need to be shared_ptrs. |
| 86 | using DispatchPtr = std::shared_ptr<VsyncDispatch>; |
| 87 | using TrackerPtr = std::shared_ptr<VsyncTracker>; |
| 88 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 89 | // TODO(b/185535769): Remove once VsyncSchedule owns all registrations. |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 90 | DispatchPtr getDispatch() { return mDispatch; } |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 91 | |
| 92 | void dump(std::string&) const; |
| 93 | |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 94 | // Turn on hardware VSYNCs, unless mHwVsyncState is Disallowed, in which |
| 95 | // case this call is ignored. |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 96 | void enableHardwareVsync() EXCLUDES(mHwVsyncLock); |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 97 | |
| 98 | // Disable hardware VSYNCs. If `disallow` is true, future calls to |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 99 | // enableHardwareVsync are ineffective until isHardwareVsyncAllowed is |
| 100 | // called with `makeAllowed` set to true. |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 101 | void disableHardwareVsync(bool disallow) EXCLUDES(mHwVsyncLock); |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 102 | |
| 103 | // If true, enableHardwareVsync can enable hardware VSYNC (if not already |
| 104 | // enabled). If false, enableHardwareVsync does nothing. |
| 105 | bool isHardwareVsyncAllowed(bool makeAllowed) EXCLUDES(mHwVsyncLock); |
| 106 | |
| 107 | void setPendingHardwareVsyncState(bool enabled) REQUIRES(kMainThreadContext); |
| 108 | |
| 109 | bool getPendingHardwareVsyncState() const REQUIRES(kMainThreadContext); |
| 110 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 111 | protected: |
| 112 | using ControllerPtr = std::unique_ptr<VsyncController>; |
| 113 | |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 114 | static void NoOpRequestHardwareVsync(PhysicalDisplayId, bool) {} |
| 115 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 116 | // For tests. |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 117 | VsyncSchedule(PhysicalDisplayId, TrackerPtr, DispatchPtr, ControllerPtr, |
| 118 | RequestHardwareVsync = NoOpRequestHardwareVsync); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 119 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 120 | private: |
| 121 | friend class TestableScheduler; |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 122 | friend class android::EventThreadTest; |
Leon Scroggins III | 4d5db7a | 2023-02-13 15:24:20 -0500 | [diff] [blame] | 123 | friend class android::VsyncScheduleTest; |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 124 | friend class android::fuzz::SchedulerFuzzer; |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 125 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 126 | static TrackerPtr createTracker(PhysicalDisplayId); |
| 127 | static DispatchPtr createDispatch(TrackerPtr); |
| 128 | static ControllerPtr createController(PhysicalDisplayId, VsyncTracker&, FeatureFlags); |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 129 | |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 130 | void enableHardwareVsyncLocked() REQUIRES(mHwVsyncLock); |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 131 | |
| 132 | mutable std::mutex mHwVsyncLock; |
| 133 | enum class HwVsyncState { |
| 134 | // Hardware VSYNCs are currently enabled. |
| 135 | Enabled, |
| 136 | |
| 137 | // Hardware VSYNCs are currently disabled. They can be enabled by a call |
| 138 | // to `enableHardwareVsync`. |
| 139 | Disabled, |
| 140 | |
| 141 | // Hardware VSYNCs are not currently allowed (e.g. because the display |
| 142 | // is off). |
| 143 | Disallowed, |
| 144 | |
| 145 | ftl_last = Disallowed, |
| 146 | }; |
| 147 | HwVsyncState mHwVsyncState GUARDED_BY(mHwVsyncLock) = HwVsyncState::Disallowed; |
| 148 | |
| 149 | // Pending state, in case an attempt is made to set the state while the |
| 150 | // device is off. |
| 151 | HwVsyncState mPendingHwVsyncState GUARDED_BY(kMainThreadContext) = HwVsyncState::Disabled; |
| 152 | |
Rachel Lee | 2db205d | 2023-05-08 11:08:59 -0700 | [diff] [blame] | 153 | // Whether to reset the timestamps stored in the vsync model on the next hw vsync sample. This |
| 154 | // is to avoid clearing the model when hw vsync is enabled, in order to be consistent with the |
| 155 | // stale timestamps. Instead, clear the model on the first hw vsync callback. |
| 156 | bool mClearTimestampsOnNextSample = false; |
| 157 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 158 | class PredictedVsyncTracer; |
| 159 | using TracerPtr = std::unique_ptr<PredictedVsyncTracer>; |
| 160 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 161 | const PhysicalDisplayId mId; |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 162 | const RequestHardwareVsync mRequestHardwareVsync; |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 163 | const TrackerPtr mTracker; |
| 164 | const DispatchPtr mDispatch; |
| 165 | const ControllerPtr mController; |
| 166 | const TracerPtr mTracer; |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | } // namespace android::scheduler |