blob: 5eca29a742b4fb42c6fa4f0cdfac0b20ac1a40da [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
Dominik Laskowskie85d7fa2023-05-15 10:50:22 -040023#include <ThreadContext.h>
Leon Scroggins III4d5db7a2023-02-13 15:24:20 -050024#include <android-base/thread_annotations.h>
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050025#include <ftl/enum.h>
26#include <ftl/optional.h>
Dominik Laskowskic183eed2023-01-21 10:02:15 -050027#include <scheduler/Features.h>
Dominik Laskowskic183eed2023-01-21 10:02:15 -050028#include <scheduler/Time.h>
Dominik Laskowskie85d7fa2023-05-15 10:50:22 -040029#include <ui/DisplayId.h>
Dominik Laskowskic183eed2023-01-21 10:02:15 -050030
Ady Abraham011f8ba2022-11-22 15:09:07 -080031namespace android {
32class EventThreadTest;
Leon Scroggins III4d5db7a2023-02-13 15:24:20 -050033class VsyncScheduleTest;
Ady Abraham011f8ba2022-11-22 15:09:07 -080034}
35
36namespace android::fuzz {
37class SchedulerFuzzer;
38}
39
Dominik Laskowski068173d2021-08-11 17:22:59 -070040namespace android::scheduler {
41
42// TODO(b/185535769): Rename classes, and remove aliases.
43class VSyncDispatch;
44class VSyncTracker;
45
46class VsyncController;
47using VsyncDispatch = VSyncDispatch;
48using VsyncTracker = VSyncTracker;
49
50// Schedule that synchronizes to hardware VSYNC of a physical display.
Dominik Laskowskie85d7fa2023-05-15 10:50:22 -040051class VsyncSchedule {
Dominik Laskowski068173d2021-08-11 17:22:59 -070052public:
Dominik Laskowski66295432023-03-14 12:25:36 -040053 using RequestHardwareVsync = std::function<void(PhysicalDisplayId, bool enabled)>;
54
55 VsyncSchedule(PhysicalDisplayId, FeatureFlags, RequestHardwareVsync);
Dominik Laskowski068173d2021-08-11 17:22:59 -070056 ~VsyncSchedule();
57
Dominik Laskowskie85d7fa2023-05-15 10:50:22 -040058 Period period() const;
59 TimePoint vsyncDeadlineAfter(TimePoint) const;
Dominik Laskowski5d164f22022-07-07 07:56:07 -070060
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050061 // Inform the schedule that the period is changing and the schedule needs to recalibrate
62 // itself. The schedule will end the period transition internally. This will
63 // enable hardware VSYNCs in order to calibrate.
64 //
65 // \param [in] period The period that the system is changing into.
Leon Scroggins III67388622023-02-06 20:36:20 -050066 // \param [in] force True to force a transition even if it is not a
67 // change.
Dominik Laskowski66295432023-03-14 12:25:36 -040068 void startPeriodTransition(Period period, bool force);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050069
70 // Pass a VSYNC sample to VsyncController. Return true if
71 // VsyncController detected that the VSYNC period changed. Enable or disable
72 // hardware VSYNCs depending on whether more samples are needed.
Dominik Laskowski66295432023-03-14 12:25:36 -040073 bool addResyncSample(TimePoint timestamp, ftl::Optional<Period> hwcVsyncPeriod);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050074
Dominik Laskowski068173d2021-08-11 17:22:59 -070075 // TODO(b/185535769): Hide behind API.
76 const VsyncTracker& getTracker() const { return *mTracker; }
77 VsyncTracker& getTracker() { return *mTracker; }
78 VsyncController& getController() { return *mController; }
79
Leon Scroggins III67388622023-02-06 20:36:20 -050080 // TODO(b/185535769): Once these are hidden behind the API, they may no
81 // longer need to be shared_ptrs.
82 using DispatchPtr = std::shared_ptr<VsyncDispatch>;
83 using TrackerPtr = std::shared_ptr<VsyncTracker>;
84
Dominik Laskowski068173d2021-08-11 17:22:59 -070085 // TODO(b/185535769): Remove once VsyncSchedule owns all registrations.
Leon Scroggins III67388622023-02-06 20:36:20 -050086 DispatchPtr getDispatch() { return mDispatch; }
Dominik Laskowski068173d2021-08-11 17:22:59 -070087
88 void dump(std::string&) const;
89
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050090 // Turn on hardware VSYNCs, unless mHwVsyncState is Disallowed, in which
91 // case this call is ignored.
Dominik Laskowski66295432023-03-14 12:25:36 -040092 void enableHardwareVsync() EXCLUDES(mHwVsyncLock);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050093
94 // Disable hardware VSYNCs. If `disallow` is true, future calls to
Leon Scroggins III67388622023-02-06 20:36:20 -050095 // enableHardwareVsync are ineffective until isHardwareVsyncAllowed is
96 // called with `makeAllowed` set to true.
Dominik Laskowski66295432023-03-14 12:25:36 -040097 void disableHardwareVsync(bool disallow) EXCLUDES(mHwVsyncLock);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050098
99 // If true, enableHardwareVsync can enable hardware VSYNC (if not already
100 // enabled). If false, enableHardwareVsync does nothing.
101 bool isHardwareVsyncAllowed(bool makeAllowed) EXCLUDES(mHwVsyncLock);
102
103 void setPendingHardwareVsyncState(bool enabled) REQUIRES(kMainThreadContext);
104
105 bool getPendingHardwareVsyncState() const REQUIRES(kMainThreadContext);
106
Leon Scroggins III67388622023-02-06 20:36:20 -0500107protected:
108 using ControllerPtr = std::unique_ptr<VsyncController>;
109
Dominik Laskowski66295432023-03-14 12:25:36 -0400110 static void NoOpRequestHardwareVsync(PhysicalDisplayId, bool) {}
111
Leon Scroggins III67388622023-02-06 20:36:20 -0500112 // For tests.
Dominik Laskowski66295432023-03-14 12:25:36 -0400113 VsyncSchedule(PhysicalDisplayId, TrackerPtr, DispatchPtr, ControllerPtr,
114 RequestHardwareVsync = NoOpRequestHardwareVsync);
Leon Scroggins III67388622023-02-06 20:36:20 -0500115
Dominik Laskowski068173d2021-08-11 17:22:59 -0700116private:
117 friend class TestableScheduler;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800118 friend class android::EventThreadTest;
Leon Scroggins III4d5db7a2023-02-13 15:24:20 -0500119 friend class android::VsyncScheduleTest;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800120 friend class android::fuzz::SchedulerFuzzer;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700121
Leon Scroggins III67388622023-02-06 20:36:20 -0500122 static TrackerPtr createTracker(PhysicalDisplayId);
123 static DispatchPtr createDispatch(TrackerPtr);
124 static ControllerPtr createController(PhysicalDisplayId, VsyncTracker&, FeatureFlags);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700125
Dominik Laskowski66295432023-03-14 12:25:36 -0400126 void enableHardwareVsyncLocked() REQUIRES(mHwVsyncLock);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500127
128 mutable std::mutex mHwVsyncLock;
129 enum class HwVsyncState {
130 // Hardware VSYNCs are currently enabled.
131 Enabled,
132
133 // Hardware VSYNCs are currently disabled. They can be enabled by a call
134 // to `enableHardwareVsync`.
135 Disabled,
136
137 // Hardware VSYNCs are not currently allowed (e.g. because the display
138 // is off).
139 Disallowed,
140
141 ftl_last = Disallowed,
142 };
143 HwVsyncState mHwVsyncState GUARDED_BY(mHwVsyncLock) = HwVsyncState::Disallowed;
144
145 // Pending state, in case an attempt is made to set the state while the
146 // device is off.
147 HwVsyncState mPendingHwVsyncState GUARDED_BY(kMainThreadContext) = HwVsyncState::Disabled;
148
Dominik Laskowski068173d2021-08-11 17:22:59 -0700149 class PredictedVsyncTracer;
150 using TracerPtr = std::unique_ptr<PredictedVsyncTracer>;
151
Leon Scroggins III67388622023-02-06 20:36:20 -0500152 const PhysicalDisplayId mId;
Dominik Laskowski66295432023-03-14 12:25:36 -0400153 const RequestHardwareVsync mRequestHardwareVsync;
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500154 const TrackerPtr mTracker;
155 const DispatchPtr mDispatch;
156 const ControllerPtr mController;
157 const TracerPtr mTracer;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700158};
159
160} // namespace android::scheduler