blob: 763d058e287cc5a659848f9050294f86069e488c [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>
Leon Scroggins III4d5db7a2023-02-13 15:24:20 -050023#include <android-base/thread_annotations.h>
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050024#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>
Leon Scroggins III67388622023-02-06 20:36:20 -050028#include <ui/DisplayId.h>
Dominik Laskowski068173d2021-08-11 17:22:59 -070029
Ady Abraham011f8ba2022-11-22 15:09:07 -080030namespace android {
31class EventThreadTest;
Leon Scroggins III4d5db7a2023-02-13 15:24:20 -050032class VsyncScheduleTest;
Ady Abraham011f8ba2022-11-22 15:09:07 -080033}
34
35namespace android::fuzz {
36class SchedulerFuzzer;
37}
38
Dominik Laskowski068173d2021-08-11 17:22:59 -070039namespace android::scheduler {
40
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050041struct ISchedulerCallback;
42
Dominik Laskowski068173d2021-08-11 17:22:59 -070043// TODO(b/185535769): Rename classes, and remove aliases.
44class VSyncDispatch;
45class VSyncTracker;
46
47class VsyncController;
48using VsyncDispatch = VSyncDispatch;
49using VsyncTracker = VSyncTracker;
50
51// Schedule that synchronizes to hardware VSYNC of a physical display.
52class VsyncSchedule {
53public:
Leon Scroggins III67388622023-02-06 20:36:20 -050054 VsyncSchedule(PhysicalDisplayId, FeatureFlags);
Dominik Laskowski068173d2021-08-11 17:22:59 -070055 ~VsyncSchedule();
56
Dominik Laskowski5d164f22022-07-07 07:56:07 -070057 Period period() const;
58 TimePoint vsyncDeadlineAfter(TimePoint) const;
59
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050060 // Inform the schedule that the period is changing and the schedule needs to recalibrate
61 // itself. The schedule will end the period transition internally. This will
62 // enable hardware VSYNCs in order to calibrate.
63 //
64 // \param [in] period The period that the system is changing into.
Leon Scroggins III67388622023-02-06 20:36:20 -050065 // \param [in] force True to force a transition even if it is not a
66 // change.
67 void startPeriodTransition(ISchedulerCallback&, Period period, bool force);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050068
69 // Pass a VSYNC sample to VsyncController. Return true if
70 // VsyncController detected that the VSYNC period changed. Enable or disable
71 // hardware VSYNCs depending on whether more samples are needed.
72 bool addResyncSample(ISchedulerCallback&, TimePoint timestamp,
73 ftl::Optional<Period> hwcVsyncPeriod);
74
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.
92 void enableHardwareVsync(ISchedulerCallback&) EXCLUDES(mHwVsyncLock);
93
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.
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050097 void disableHardwareVsync(ISchedulerCallback&, bool disallow) EXCLUDES(mHwVsyncLock);
98
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
110 // For tests.
111 VsyncSchedule(PhysicalDisplayId, TrackerPtr, DispatchPtr, ControllerPtr);
112
Dominik Laskowski068173d2021-08-11 17:22:59 -0700113private:
114 friend class TestableScheduler;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800115 friend class android::EventThreadTest;
Leon Scroggins III4d5db7a2023-02-13 15:24:20 -0500116 friend class android::VsyncScheduleTest;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800117 friend class android::fuzz::SchedulerFuzzer;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700118
Leon Scroggins III67388622023-02-06 20:36:20 -0500119 static TrackerPtr createTracker(PhysicalDisplayId);
120 static DispatchPtr createDispatch(TrackerPtr);
121 static ControllerPtr createController(PhysicalDisplayId, VsyncTracker&, FeatureFlags);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700122
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500123 void enableHardwareVsyncLocked(ISchedulerCallback&) REQUIRES(mHwVsyncLock);
124
125 mutable std::mutex mHwVsyncLock;
126 enum class HwVsyncState {
127 // Hardware VSYNCs are currently enabled.
128 Enabled,
129
130 // Hardware VSYNCs are currently disabled. They can be enabled by a call
131 // to `enableHardwareVsync`.
132 Disabled,
133
134 // Hardware VSYNCs are not currently allowed (e.g. because the display
135 // is off).
136 Disallowed,
137
138 ftl_last = Disallowed,
139 };
140 HwVsyncState mHwVsyncState GUARDED_BY(mHwVsyncLock) = HwVsyncState::Disallowed;
141
142 // Pending state, in case an attempt is made to set the state while the
143 // device is off.
144 HwVsyncState mPendingHwVsyncState GUARDED_BY(kMainThreadContext) = HwVsyncState::Disabled;
145
Dominik Laskowski068173d2021-08-11 17:22:59 -0700146 class PredictedVsyncTracer;
147 using TracerPtr = std::unique_ptr<PredictedVsyncTracer>;
148
Leon Scroggins III67388622023-02-06 20:36:20 -0500149 const PhysicalDisplayId mId;
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500150 const TrackerPtr mTracker;
151 const DispatchPtr mDispatch;
152 const ControllerPtr mController;
153 const TracerPtr mTracer;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700154};
155
156} // namespace android::scheduler