blob: 6f656d2029baa4a9c163e41678db4eb496090f7f [file] [log] [blame]
Dominik Laskowski068173d2021-08-11 17:22:59 -07001/*
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 Laskowski66295432023-03-14 12:25:36 -040019#include <functional>
Dominik Laskowski068173d2021-08-11 17:22:59 -070020#include <memory>
21#include <string>
22
Leon Scroggins III58af6ed2023-02-13 15:24:20 -050023#include <android-base/thread_annotations.h>
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050024#include <ThreadContext.h>
25#include <ftl/enum.h>
26#include <ftl/optional.h>
Dominik Laskowskie85d7fa2023-05-15 10:50:22 -040027#include <ui/DisplayId.h>
Dominik Laskowskic183eed2023-01-21 10:02:15 -050028
Dominik Laskowskib418dd72023-06-13 17:31:04 -040029#include <scheduler/Features.h>
30#include <scheduler/IVsyncSource.h>
31#include <scheduler/Time.h>
32
33#include "ThreadContext.h"
ramindanid4354a92023-10-02 15:11:09 -070034#include "VSyncTracker.h"
Dominik Laskowskib418dd72023-06-13 17:31:04 -040035
Ady Abraham011f8ba2022-11-22 15:09:07 -080036namespace android {
37class EventThreadTest;
Leon Scroggins III58af6ed2023-02-13 15:24:20 -050038class VsyncScheduleTest;
Ady Abraham011f8ba2022-11-22 15:09:07 -080039}
40
41namespace android::fuzz {
42class SchedulerFuzzer;
43}
44
Dominik Laskowski068173d2021-08-11 17:22:59 -070045namespace android::scheduler {
46
47// TODO(b/185535769): Rename classes, and remove aliases.
48class VSyncDispatch;
49class VSyncTracker;
50
51class VsyncController;
52using VsyncDispatch = VSyncDispatch;
53using VsyncTracker = VSyncTracker;
54
55// Schedule that synchronizes to hardware VSYNC of a physical display.
Dominik Laskowskib418dd72023-06-13 17:31:04 -040056class VsyncSchedule final : public IVsyncSource {
Dominik Laskowski068173d2021-08-11 17:22:59 -070057public:
Dominik Laskowski66295432023-03-14 12:25:36 -040058 using RequestHardwareVsync = std::function<void(PhysicalDisplayId, bool enabled)>;
59
Ady Abrahamc585dba2023-11-15 18:41:35 -080060 VsyncSchedule(ftl::NonNull<DisplayModePtr> modePtr, FeatureFlags, RequestHardwareVsync,
61 IVsyncTrackerCallback&);
Dominik Laskowski068173d2021-08-11 17:22:59 -070062 ~VsyncSchedule();
63
Dominik Laskowskib418dd72023-06-13 17:31:04 -040064 // IVsyncSource overrides:
65 Period period() const override;
Leon Scroggins IIIa0785012024-01-23 16:05:59 -050066 TimePoint vsyncDeadlineAfter(TimePoint,
67 ftl::Optional<TimePoint> lastVsyncOpt = {}) const override;
Ady Abraham3db8a3c2023-11-20 17:53:47 -080068 Period minFramePeriod() const override;
Dominik Laskowski5d164f22022-07-07 07:56:07 -070069
Ady Abrahamc585dba2023-11-15 18:41:35 -080070 // Inform the schedule that the display mode changed the schedule needs to recalibrate
71 // itself to the new vsync period. The schedule will end the period transition internally.
72 // This will enable hardware VSYNCs in order to calibrate.
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050073 //
Ady Abrahamc585dba2023-11-15 18:41:35 -080074 // \param [in] DisplayModePtr The mode that the display is changing to.
Leon Scroggins III67388622023-02-06 20:36:20 -050075 // \param [in] force True to force a transition even if it is not a
76 // change.
Ady Abrahamc585dba2023-11-15 18:41:35 -080077 void onDisplayModeChanged(ftl::NonNull<DisplayModePtr>, bool force);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050078
79 // Pass a VSYNC sample to VsyncController. Return true if
80 // VsyncController detected that the VSYNC period changed. Enable or disable
81 // hardware VSYNCs depending on whether more samples are needed.
Dominik Laskowski66295432023-03-14 12:25:36 -040082 bool addResyncSample(TimePoint timestamp, ftl::Optional<Period> hwcVsyncPeriod);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050083
Dominik Laskowski068173d2021-08-11 17:22:59 -070084 // TODO(b/185535769): Hide behind API.
85 const VsyncTracker& getTracker() const { return *mTracker; }
86 VsyncTracker& getTracker() { return *mTracker; }
87 VsyncController& getController() { return *mController; }
88
Leon Scroggins III67388622023-02-06 20:36:20 -050089 // TODO(b/185535769): Once these are hidden behind the API, they may no
90 // longer need to be shared_ptrs.
91 using DispatchPtr = std::shared_ptr<VsyncDispatch>;
92 using TrackerPtr = std::shared_ptr<VsyncTracker>;
93
Dominik Laskowski068173d2021-08-11 17:22:59 -070094 // TODO(b/185535769): Remove once VsyncSchedule owns all registrations.
Leon Scroggins III67388622023-02-06 20:36:20 -050095 DispatchPtr getDispatch() { return mDispatch; }
Dominik Laskowski068173d2021-08-11 17:22:59 -070096
97 void dump(std::string&) const;
98
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050099 // Turn on hardware VSYNCs, unless mHwVsyncState is Disallowed, in which
100 // case this call is ignored.
Dominik Laskowski66295432023-03-14 12:25:36 -0400101 void enableHardwareVsync() EXCLUDES(mHwVsyncLock);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500102
103 // Disable hardware VSYNCs. If `disallow` is true, future calls to
Leon Scroggins III67388622023-02-06 20:36:20 -0500104 // enableHardwareVsync are ineffective until isHardwareVsyncAllowed is
105 // called with `makeAllowed` set to true.
Dominik Laskowski66295432023-03-14 12:25:36 -0400106 void disableHardwareVsync(bool disallow) EXCLUDES(mHwVsyncLock);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500107
108 // If true, enableHardwareVsync can enable hardware VSYNC (if not already
109 // enabled). If false, enableHardwareVsync does nothing.
110 bool isHardwareVsyncAllowed(bool makeAllowed) EXCLUDES(mHwVsyncLock);
111
112 void setPendingHardwareVsyncState(bool enabled) REQUIRES(kMainThreadContext);
113
114 bool getPendingHardwareVsyncState() const REQUIRES(kMainThreadContext);
115
Leon Scroggins III67388622023-02-06 20:36:20 -0500116protected:
117 using ControllerPtr = std::unique_ptr<VsyncController>;
118
Dominik Laskowski66295432023-03-14 12:25:36 -0400119 static void NoOpRequestHardwareVsync(PhysicalDisplayId, bool) {}
120
Leon Scroggins III67388622023-02-06 20:36:20 -0500121 // For tests.
Dominik Laskowski66295432023-03-14 12:25:36 -0400122 VsyncSchedule(PhysicalDisplayId, TrackerPtr, DispatchPtr, ControllerPtr,
123 RequestHardwareVsync = NoOpRequestHardwareVsync);
Leon Scroggins III67388622023-02-06 20:36:20 -0500124
Dominik Laskowski068173d2021-08-11 17:22:59 -0700125private:
126 friend class TestableScheduler;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800127 friend class android::EventThreadTest;
Leon Scroggins III58af6ed2023-02-13 15:24:20 -0500128 friend class android::VsyncScheduleTest;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800129 friend class android::fuzz::SchedulerFuzzer;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700130
Ady Abrahamc585dba2023-11-15 18:41:35 -0800131 static TrackerPtr createTracker(ftl::NonNull<DisplayModePtr> modePtr, IVsyncTrackerCallback&);
Leon Scroggins III67388622023-02-06 20:36:20 -0500132 static DispatchPtr createDispatch(TrackerPtr);
133 static ControllerPtr createController(PhysicalDisplayId, VsyncTracker&, FeatureFlags);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700134
Dominik Laskowski66295432023-03-14 12:25:36 -0400135 void enableHardwareVsyncLocked() REQUIRES(mHwVsyncLock);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500136
137 mutable std::mutex mHwVsyncLock;
138 enum class HwVsyncState {
139 // Hardware VSYNCs are currently enabled.
140 Enabled,
141
142 // Hardware VSYNCs are currently disabled. They can be enabled by a call
143 // to `enableHardwareVsync`.
144 Disabled,
145
146 // Hardware VSYNCs are not currently allowed (e.g. because the display
147 // is off).
148 Disallowed,
149
150 ftl_last = Disallowed,
151 };
152 HwVsyncState mHwVsyncState GUARDED_BY(mHwVsyncLock) = HwVsyncState::Disallowed;
153
154 // Pending state, in case an attempt is made to set the state while the
155 // device is off.
156 HwVsyncState mPendingHwVsyncState GUARDED_BY(kMainThreadContext) = HwVsyncState::Disabled;
157
Dominik Laskowski068173d2021-08-11 17:22:59 -0700158 class PredictedVsyncTracer;
159 using TracerPtr = std::unique_ptr<PredictedVsyncTracer>;
160
Leon Scroggins III67388622023-02-06 20:36:20 -0500161 const PhysicalDisplayId mId;
Dominik Laskowski66295432023-03-14 12:25:36 -0400162 const RequestHardwareVsync mRequestHardwareVsync;
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500163 const TrackerPtr mTracker;
164 const DispatchPtr mDispatch;
165 const ControllerPtr mController;
166 const TracerPtr mTracer;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700167};
168
169} // namespace android::scheduler