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