blob: d88f1d1f0b86bfc36942646d5832a8c2b604047a [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 IIIc275df42023-02-07 16:40:21 -050022#include <ThreadContext.h>
23#include <ftl/enum.h>
24#include <ftl/optional.h>
Dominik Laskowski068173d2021-08-11 17:22:59 -070025#include <scheduler/Features.h>
Dominik Laskowski5d164f22022-07-07 07:56:07 -070026#include <scheduler/Time.h>
Dominik Laskowski068173d2021-08-11 17:22:59 -070027
Ady Abraham011f8ba2022-11-22 15:09:07 -080028namespace android {
29class EventThreadTest;
30}
31
32namespace android::fuzz {
33class SchedulerFuzzer;
34}
35
Dominik Laskowski068173d2021-08-11 17:22:59 -070036namespace android::scheduler {
37
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050038struct ISchedulerCallback;
39
Dominik Laskowski068173d2021-08-11 17:22:59 -070040// TODO(b/185535769): Rename classes, and remove aliases.
41class VSyncDispatch;
42class VSyncTracker;
43
44class VsyncController;
45using VsyncDispatch = VSyncDispatch;
46using VsyncTracker = VSyncTracker;
47
48// Schedule that synchronizes to hardware VSYNC of a physical display.
49class VsyncSchedule {
50public:
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -050051 explicit VsyncSchedule(FeatureFlags);
Dominik Laskowski068173d2021-08-11 17:22:59 -070052 ~VsyncSchedule();
53
Dominik Laskowski5d164f22022-07-07 07:56:07 -070054 Period period() const;
55 TimePoint vsyncDeadlineAfter(TimePoint) const;
56
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050057 // Inform the schedule that the period is changing and the schedule needs to recalibrate
58 // itself. The schedule will end the period transition internally. This will
59 // enable hardware VSYNCs in order to calibrate.
60 //
61 // \param [in] period The period that the system is changing into.
62 void startPeriodTransition(ISchedulerCallback&, Period period);
63
64 // Pass a VSYNC sample to VsyncController. Return true if
65 // VsyncController detected that the VSYNC period changed. Enable or disable
66 // hardware VSYNCs depending on whether more samples are needed.
67 bool addResyncSample(ISchedulerCallback&, TimePoint timestamp,
68 ftl::Optional<Period> hwcVsyncPeriod);
69
Dominik Laskowski068173d2021-08-11 17:22:59 -070070 // TODO(b/185535769): Hide behind API.
71 const VsyncTracker& getTracker() const { return *mTracker; }
72 VsyncTracker& getTracker() { return *mTracker; }
73 VsyncController& getController() { return *mController; }
74
75 // TODO(b/185535769): Remove once VsyncSchedule owns all registrations.
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -050076 VsyncDispatch& getDispatch() { return *mDispatch; }
Dominik Laskowski068173d2021-08-11 17:22:59 -070077
78 void dump(std::string&) const;
79
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050080 // Turn on hardware VSYNCs, unless mHwVsyncState is Disallowed, in which
81 // case this call is ignored.
82 void enableHardwareVsync(ISchedulerCallback&) EXCLUDES(mHwVsyncLock);
83
84 // Disable hardware VSYNCs. If `disallow` is true, future calls to
85 // enableHardwareVsync are ineffective until allowHardwareVsync is called.
86 void disableHardwareVsync(ISchedulerCallback&, bool disallow) EXCLUDES(mHwVsyncLock);
87
88 // If true, enableHardwareVsync can enable hardware VSYNC (if not already
89 // enabled). If false, enableHardwareVsync does nothing.
90 bool isHardwareVsyncAllowed(bool makeAllowed) EXCLUDES(mHwVsyncLock);
91
92 void setPendingHardwareVsyncState(bool enabled) REQUIRES(kMainThreadContext);
93
94 bool getPendingHardwareVsyncState() const REQUIRES(kMainThreadContext);
95
Dominik Laskowski068173d2021-08-11 17:22:59 -070096private:
97 friend class TestableScheduler;
Ady Abraham011f8ba2022-11-22 15:09:07 -080098 friend class android::EventThreadTest;
99 friend class android::fuzz::SchedulerFuzzer;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700100
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500101 using TrackerPtr = std::unique_ptr<VsyncTracker>;
102 using DispatchPtr = std::unique_ptr<VsyncDispatch>;
103 using ControllerPtr = std::unique_ptr<VsyncController>;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700104
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500105 // For tests.
106 VsyncSchedule(TrackerPtr, DispatchPtr, ControllerPtr);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700107
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500108 static TrackerPtr createTracker();
109 static DispatchPtr createDispatch(VsyncTracker&);
110 static ControllerPtr createController(VsyncTracker&, FeatureFlags);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700111
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500112 void enableHardwareVsyncLocked(ISchedulerCallback&) REQUIRES(mHwVsyncLock);
113
114 mutable std::mutex mHwVsyncLock;
115 enum class HwVsyncState {
116 // Hardware VSYNCs are currently enabled.
117 Enabled,
118
119 // Hardware VSYNCs are currently disabled. They can be enabled by a call
120 // to `enableHardwareVsync`.
121 Disabled,
122
123 // Hardware VSYNCs are not currently allowed (e.g. because the display
124 // is off).
125 Disallowed,
126
127 ftl_last = Disallowed,
128 };
129 HwVsyncState mHwVsyncState GUARDED_BY(mHwVsyncLock) = HwVsyncState::Disallowed;
130
131 // Pending state, in case an attempt is made to set the state while the
132 // device is off.
133 HwVsyncState mPendingHwVsyncState GUARDED_BY(kMainThreadContext) = HwVsyncState::Disabled;
134
Dominik Laskowski068173d2021-08-11 17:22:59 -0700135 class PredictedVsyncTracer;
136 using TracerPtr = std::unique_ptr<PredictedVsyncTracer>;
137
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500138 const TrackerPtr mTracker;
139 const DispatchPtr mDispatch;
140 const ControllerPtr mController;
141 const TracerPtr mTracer;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700142};
143
144} // namespace android::scheduler