blob: f16b5b257776e964201947cd3d38e52eb3394883 [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
19#include <memory>
20#include <string>
21
Leon Scroggins III58af6ed2023-02-13 15:24:20 -050022#include <android-base/thread_annotations.h>
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050023#include <ThreadContext.h>
24#include <ftl/enum.h>
25#include <ftl/optional.h>
Dominik Laskowski068173d2021-08-11 17:22:59 -070026#include <scheduler/Features.h>
Dominik Laskowski5d164f22022-07-07 07:56:07 -070027#include <scheduler/Time.h>
Dominik Laskowski068173d2021-08-11 17:22:59 -070028
Ady Abraham011f8ba2022-11-22 15:09:07 -080029namespace android {
30class EventThreadTest;
Leon Scroggins III58af6ed2023-02-13 15:24:20 -050031class VsyncScheduleTest;
Ady Abraham011f8ba2022-11-22 15:09:07 -080032}
33
34namespace android::fuzz {
35class SchedulerFuzzer;
36}
37
Dominik Laskowski068173d2021-08-11 17:22:59 -070038namespace android::scheduler {
39
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050040struct ISchedulerCallback;
41
Dominik Laskowski068173d2021-08-11 17:22:59 -070042// 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.
51class VsyncSchedule {
52public:
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -050053 explicit VsyncSchedule(FeatureFlags);
Dominik Laskowski068173d2021-08-11 17:22:59 -070054 ~VsyncSchedule();
55
Dominik Laskowski5d164f22022-07-07 07:56:07 -070056 Period period() const;
57 TimePoint vsyncDeadlineAfter(TimePoint) const;
58
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050059 // 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 Laskowski068173d2021-08-11 17:22:59 -070072 // 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 IIIdb16a2b2023-02-06 17:50:05 -050078 VsyncDispatch& getDispatch() { return *mDispatch; }
Dominik Laskowski068173d2021-08-11 17:22:59 -070079
80 void dump(std::string&) const;
81
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050082 // 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 Laskowski068173d2021-08-11 17:22:59 -070098private:
99 friend class TestableScheduler;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800100 friend class android::EventThreadTest;
Leon Scroggins III58af6ed2023-02-13 15:24:20 -0500101 friend class android::VsyncScheduleTest;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800102 friend class android::fuzz::SchedulerFuzzer;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700103
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500104 using TrackerPtr = std::unique_ptr<VsyncTracker>;
105 using DispatchPtr = std::unique_ptr<VsyncDispatch>;
106 using ControllerPtr = std::unique_ptr<VsyncController>;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700107
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500108 // For tests.
109 VsyncSchedule(TrackerPtr, DispatchPtr, ControllerPtr);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700110
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500111 static TrackerPtr createTracker();
112 static DispatchPtr createDispatch(VsyncTracker&);
113 static ControllerPtr createController(VsyncTracker&, FeatureFlags);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700114
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500115 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 Laskowski068173d2021-08-11 17:22:59 -0700138 class PredictedVsyncTracer;
139 using TracerPtr = std::unique_ptr<PredictedVsyncTracer>;
140
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500141 const TrackerPtr mTracker;
142 const DispatchPtr mDispatch;
143 const ControllerPtr mController;
144 const TracerPtr mTracer;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700145};
146
147} // namespace android::scheduler