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