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 | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 22 | #include <ftl/enum.h> |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 23 | #include <scheduler/Features.h> |
Dominik Laskowski | 5d164f2 | 2022-07-07 07:56:07 -0700 | [diff] [blame] | 24 | #include <scheduler/Time.h> |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 25 | #include <ui/DisplayId.h> |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 26 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 27 | namespace android { |
| 28 | class EventThreadTest; |
| 29 | } |
| 30 | |
| 31 | namespace android::fuzz { |
| 32 | class SchedulerFuzzer; |
| 33 | } |
| 34 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 35 | namespace android::scheduler { |
| 36 | |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 37 | struct ISchedulerCallback; |
| 38 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 39 | // TODO(b/185535769): Rename classes, and remove aliases. |
| 40 | class VSyncDispatch; |
| 41 | class VSyncTracker; |
| 42 | |
| 43 | class VsyncController; |
| 44 | using VsyncDispatch = VSyncDispatch; |
| 45 | using VsyncTracker = VSyncTracker; |
| 46 | |
| 47 | // Schedule that synchronizes to hardware VSYNC of a physical display. |
| 48 | class VsyncSchedule { |
| 49 | public: |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 50 | VsyncSchedule(PhysicalDisplayId, FeatureFlags); |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 51 | ~VsyncSchedule(); |
| 52 | |
Dominik Laskowski | 5d164f2 | 2022-07-07 07:56:07 -0700 | [diff] [blame] | 53 | Period period() const; |
| 54 | TimePoint vsyncDeadlineAfter(TimePoint) const; |
| 55 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 56 | // TODO(b/185535769): Hide behind API. |
| 57 | const VsyncTracker& getTracker() const { return *mTracker; } |
| 58 | VsyncTracker& getTracker() { return *mTracker; } |
| 59 | VsyncController& getController() { return *mController; } |
| 60 | |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 61 | // TODO(b/185535769): Once these are hidden behind the API, they may no |
| 62 | // longer need to be shared_ptrs. |
| 63 | using DispatchPtr = std::shared_ptr<VsyncDispatch>; |
| 64 | using TrackerPtr = std::shared_ptr<VsyncTracker>; |
| 65 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 66 | // TODO(b/185535769): Remove once VsyncSchedule owns all registrations. |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 67 | DispatchPtr getDispatch() { return mDispatch; } |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 68 | |
| 69 | void dump(std::string&) const; |
| 70 | |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 71 | // Turn on hardware vsyncs, unless mHwVsyncState is Disallowed, in which |
| 72 | // case this call is ignored. |
| 73 | void enableHardwareVsync(ISchedulerCallback&) EXCLUDES(mHwVsyncLock); |
| 74 | |
| 75 | // Disable hardware vsyncs. If `disallow` is true, future calls to |
| 76 | // enableHardwareVsync are ineffective until allowHardwareVsync is called. |
| 77 | void disableHardwareVsync(ISchedulerCallback&, bool disallow) EXCLUDES(mHwVsyncLock); |
| 78 | |
| 79 | // Restore the ability to enable hardware vsync. |
| 80 | void allowHardwareVsync() EXCLUDES(mHwVsyncLock); |
| 81 | |
| 82 | // If true, enableHardwareVsync can enable hardware vsync (if not already |
| 83 | // enabled). If false, enableHardwareVsync does nothing. |
| 84 | bool isHardwareVsyncAllowed() const EXCLUDES(mHwVsyncLock); |
| 85 | |
| 86 | protected: |
| 87 | using ControllerPtr = std::unique_ptr<VsyncController>; |
| 88 | |
| 89 | // For tests. |
| 90 | VsyncSchedule(PhysicalDisplayId, TrackerPtr, DispatchPtr, ControllerPtr); |
| 91 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 92 | private: |
| 93 | friend class TestableScheduler; |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 94 | friend class android::EventThreadTest; |
| 95 | friend class android::fuzz::SchedulerFuzzer; |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 96 | |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 97 | static TrackerPtr createTracker(PhysicalDisplayId); |
| 98 | static DispatchPtr createDispatch(TrackerPtr); |
| 99 | static ControllerPtr createController(PhysicalDisplayId, VsyncTracker&, FeatureFlags); |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 100 | |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 101 | mutable std::mutex mHwVsyncLock; |
| 102 | enum class HwVsyncState { |
| 103 | // Hardware vsyncs are currently enabled. |
| 104 | Enabled, |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 105 | |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 106 | // Hardware vsyncs are currently disabled. They can be enabled by a call |
| 107 | // to `enableHardwareVsync`. |
| 108 | Disabled, |
| 109 | |
| 110 | // Hardware vsyncs are not currently allowed (e.g. because the display |
| 111 | // is off). |
| 112 | Disallowed, |
| 113 | |
| 114 | ftl_last = Disallowed, |
| 115 | }; |
| 116 | HwVsyncState mHwVsyncState GUARDED_BY(mHwVsyncLock) = HwVsyncState::Disallowed; |
| 117 | |
| 118 | // The last state, which may be the current state, or the state prior to setting to Disallowed. |
| 119 | HwVsyncState mLastHwVsyncState GUARDED_BY(mHwVsyncLock) = HwVsyncState::Disabled; |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 120 | |
| 121 | class PredictedVsyncTracer; |
| 122 | using TracerPtr = std::unique_ptr<PredictedVsyncTracer>; |
| 123 | |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 124 | const PhysicalDisplayId mId; |
| 125 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 126 | // Effectively const except in move constructor. |
| 127 | TrackerPtr mTracker; |
| 128 | DispatchPtr mDispatch; |
| 129 | ControllerPtr mController; |
| 130 | TracerPtr mTracer; |
| 131 | }; |
| 132 | |
| 133 | } // namespace android::scheduler |