blob: ffb7ad5b42e6d59c27fc1818e8c8a768bcbce236 [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 III31d41412022-11-18 16:42:53 -050022#include <ftl/enum.h>
Dominik Laskowski068173d2021-08-11 17:22:59 -070023#include <scheduler/Features.h>
Dominik Laskowski5d164f22022-07-07 07:56:07 -070024#include <scheduler/Time.h>
Leon Scroggins III31d41412022-11-18 16:42:53 -050025#include <ui/DisplayId.h>
Dominik Laskowski068173d2021-08-11 17:22:59 -070026
Ady Abraham011f8ba2022-11-22 15:09:07 -080027namespace android {
28class EventThreadTest;
29}
30
31namespace android::fuzz {
32class SchedulerFuzzer;
33}
34
Dominik Laskowski068173d2021-08-11 17:22:59 -070035namespace android::scheduler {
36
Leon Scroggins III31d41412022-11-18 16:42:53 -050037struct ISchedulerCallback;
38
Dominik Laskowski068173d2021-08-11 17:22:59 -070039// TODO(b/185535769): Rename classes, and remove aliases.
40class VSyncDispatch;
41class VSyncTracker;
42
43class VsyncController;
44using VsyncDispatch = VSyncDispatch;
45using VsyncTracker = VSyncTracker;
46
47// Schedule that synchronizes to hardware VSYNC of a physical display.
48class VsyncSchedule {
49public:
Leon Scroggins III31d41412022-11-18 16:42:53 -050050 VsyncSchedule(PhysicalDisplayId, FeatureFlags);
Dominik Laskowski068173d2021-08-11 17:22:59 -070051 ~VsyncSchedule();
52
Dominik Laskowski5d164f22022-07-07 07:56:07 -070053 Period period() const;
54 TimePoint vsyncDeadlineAfter(TimePoint) const;
55
Dominik Laskowski068173d2021-08-11 17:22:59 -070056 // TODO(b/185535769): Hide behind API.
57 const VsyncTracker& getTracker() const { return *mTracker; }
58 VsyncTracker& getTracker() { return *mTracker; }
59 VsyncController& getController() { return *mController; }
60
Leon Scroggins III31d41412022-11-18 16:42:53 -050061 // TODO(b/185535769): Once these are hidden behind the API, they may no
62 // longer need to be shared_ptrs.
63 using DispatchPtr = std::shared_ptr<VsyncDispatch>;
64 using TrackerPtr = std::shared_ptr<VsyncTracker>;
65
Dominik Laskowski068173d2021-08-11 17:22:59 -070066 // TODO(b/185535769): Remove once VsyncSchedule owns all registrations.
Leon Scroggins III31d41412022-11-18 16:42:53 -050067 DispatchPtr getDispatch() { return mDispatch; }
Dominik Laskowski068173d2021-08-11 17:22:59 -070068
69 void dump(std::string&) const;
70
Leon Scroggins III31d41412022-11-18 16:42:53 -050071 // Turn on hardware vsyncs, unless mHwVsyncState is Disallowed, in which
72 // case this call is ignored.
73 void enableHardwareVsync(ISchedulerCallback&) EXCLUDES(mHwVsyncLock);
74
75 // Disable hardware vsyncs. If `disallow` is true, future calls to
76 // enableHardwareVsync are ineffective until allowHardwareVsync is called.
77 void disableHardwareVsync(ISchedulerCallback&, bool disallow) EXCLUDES(mHwVsyncLock);
78
79 // Restore the ability to enable hardware vsync.
80 void allowHardwareVsync() EXCLUDES(mHwVsyncLock);
81
82 // If true, enableHardwareVsync can enable hardware vsync (if not already
83 // enabled). If false, enableHardwareVsync does nothing.
84 bool isHardwareVsyncAllowed() const EXCLUDES(mHwVsyncLock);
85
86protected:
87 using ControllerPtr = std::unique_ptr<VsyncController>;
88
89 // For tests.
90 VsyncSchedule(PhysicalDisplayId, TrackerPtr, DispatchPtr, ControllerPtr);
91
Dominik Laskowski068173d2021-08-11 17:22:59 -070092private:
93 friend class TestableScheduler;
Ady Abraham011f8ba2022-11-22 15:09:07 -080094 friend class android::EventThreadTest;
95 friend class android::fuzz::SchedulerFuzzer;
Dominik Laskowski068173d2021-08-11 17:22:59 -070096
Leon Scroggins III31d41412022-11-18 16:42:53 -050097 static TrackerPtr createTracker(PhysicalDisplayId);
98 static DispatchPtr createDispatch(TrackerPtr);
99 static ControllerPtr createController(PhysicalDisplayId, VsyncTracker&, FeatureFlags);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700100
Leon Scroggins III31d41412022-11-18 16:42:53 -0500101 mutable std::mutex mHwVsyncLock;
102 enum class HwVsyncState {
103 // Hardware vsyncs are currently enabled.
104 Enabled,
Dominik Laskowski068173d2021-08-11 17:22:59 -0700105
Leon Scroggins III31d41412022-11-18 16:42:53 -0500106 // Hardware vsyncs are currently disabled. They can be enabled by a call
107 // to `enableHardwareVsync`.
108 Disabled,
109
110 // Hardware vsyncs are not currently allowed (e.g. because the display
111 // is off).
112 Disallowed,
113
114 ftl_last = Disallowed,
115 };
116 HwVsyncState mHwVsyncState GUARDED_BY(mHwVsyncLock) = HwVsyncState::Disallowed;
117
118 // The last state, which may be the current state, or the state prior to setting to Disallowed.
119 HwVsyncState mLastHwVsyncState GUARDED_BY(mHwVsyncLock) = HwVsyncState::Disabled;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700120
121 class PredictedVsyncTracer;
122 using TracerPtr = std::unique_ptr<PredictedVsyncTracer>;
123
Leon Scroggins III31d41412022-11-18 16:42:53 -0500124 const PhysicalDisplayId mId;
125
Dominik Laskowski068173d2021-08-11 17:22:59 -0700126 // Effectively const except in move constructor.
127 TrackerPtr mTracker;
128 DispatchPtr mDispatch;
129 ControllerPtr mController;
130 TracerPtr mTracer;
131};
132
133} // namespace android::scheduler