blob: 9867b41a2eb325abeea91f27c30f6f91fb35a33a [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 III4d5db7a2023-02-13 15:24:20 -050022#include <android-base/thread_annotations.h>
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050023#include <ftl/enum.h>
24#include <ftl/optional.h>
Leon Scroggins III67388622023-02-06 20:36:20 -050025#include <ui/DisplayId.h>
Dominik Laskowski068173d2021-08-11 17:22:59 -070026
Dominik Laskowskic183eed2023-01-21 10:02:15 -050027#include <scheduler/Features.h>
28#include <scheduler/IVsyncSource.h>
29#include <scheduler/Time.h>
30
31#include "ThreadContext.h"
32
Ady Abraham011f8ba2022-11-22 15:09:07 -080033namespace android {
34class EventThreadTest;
Leon Scroggins III4d5db7a2023-02-13 15:24:20 -050035class VsyncScheduleTest;
Ady Abraham011f8ba2022-11-22 15:09:07 -080036}
37
38namespace android::fuzz {
39class SchedulerFuzzer;
40}
41
Dominik Laskowski068173d2021-08-11 17:22:59 -070042namespace android::scheduler {
43
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050044struct ISchedulerCallback;
45
Dominik Laskowski068173d2021-08-11 17:22:59 -070046// TODO(b/185535769): Rename classes, and remove aliases.
47class VSyncDispatch;
48class VSyncTracker;
49
50class VsyncController;
51using VsyncDispatch = VSyncDispatch;
52using VsyncTracker = VSyncTracker;
53
54// Schedule that synchronizes to hardware VSYNC of a physical display.
Dominik Laskowskic183eed2023-01-21 10:02:15 -050055class VsyncSchedule final : public IVsyncSource {
Dominik Laskowski068173d2021-08-11 17:22:59 -070056public:
Leon Scroggins III67388622023-02-06 20:36:20 -050057 VsyncSchedule(PhysicalDisplayId, FeatureFlags);
Dominik Laskowski068173d2021-08-11 17:22:59 -070058 ~VsyncSchedule();
59
Dominik Laskowskic183eed2023-01-21 10:02:15 -050060 // IVsyncSource overrides:
61 Period period() const override;
62 TimePoint vsyncDeadlineAfter(TimePoint) const override;
Dominik Laskowski5d164f22022-07-07 07:56:07 -070063
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050064 // Inform the schedule that the period is changing and the schedule needs to recalibrate
65 // itself. The schedule will end the period transition internally. This will
66 // enable hardware VSYNCs in order to calibrate.
67 //
68 // \param [in] period The period that the system is changing into.
Leon Scroggins III67388622023-02-06 20:36:20 -050069 // \param [in] force True to force a transition even if it is not a
70 // change.
71 void startPeriodTransition(ISchedulerCallback&, Period period, bool force);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050072
73 // Pass a VSYNC sample to VsyncController. Return true if
74 // VsyncController detected that the VSYNC period changed. Enable or disable
75 // hardware VSYNCs depending on whether more samples are needed.
76 bool addResyncSample(ISchedulerCallback&, TimePoint timestamp,
77 ftl::Optional<Period> hwcVsyncPeriod);
78
Dominik Laskowski068173d2021-08-11 17:22:59 -070079 // TODO(b/185535769): Hide behind API.
80 const VsyncTracker& getTracker() const { return *mTracker; }
81 VsyncTracker& getTracker() { return *mTracker; }
82 VsyncController& getController() { return *mController; }
83
Leon Scroggins III67388622023-02-06 20:36:20 -050084 // TODO(b/185535769): Once these are hidden behind the API, they may no
85 // longer need to be shared_ptrs.
86 using DispatchPtr = std::shared_ptr<VsyncDispatch>;
87 using TrackerPtr = std::shared_ptr<VsyncTracker>;
88
Dominik Laskowski068173d2021-08-11 17:22:59 -070089 // TODO(b/185535769): Remove once VsyncSchedule owns all registrations.
Leon Scroggins III67388622023-02-06 20:36:20 -050090 DispatchPtr getDispatch() { return mDispatch; }
Dominik Laskowski068173d2021-08-11 17:22:59 -070091
92 void dump(std::string&) const;
93
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050094 // Turn on hardware VSYNCs, unless mHwVsyncState is Disallowed, in which
95 // case this call is ignored.
96 void enableHardwareVsync(ISchedulerCallback&) EXCLUDES(mHwVsyncLock);
97
98 // Disable hardware VSYNCs. If `disallow` is true, future calls to
Leon Scroggins III67388622023-02-06 20:36:20 -050099 // enableHardwareVsync are ineffective until isHardwareVsyncAllowed is
100 // called with `makeAllowed` set to true.
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500101 void disableHardwareVsync(ISchedulerCallback&, bool disallow) EXCLUDES(mHwVsyncLock);
102
103 // If true, enableHardwareVsync can enable hardware VSYNC (if not already
104 // enabled). If false, enableHardwareVsync does nothing.
105 bool isHardwareVsyncAllowed(bool makeAllowed) EXCLUDES(mHwVsyncLock);
106
107 void setPendingHardwareVsyncState(bool enabled) REQUIRES(kMainThreadContext);
108
109 bool getPendingHardwareVsyncState() const REQUIRES(kMainThreadContext);
110
Leon Scroggins III67388622023-02-06 20:36:20 -0500111protected:
112 using ControllerPtr = std::unique_ptr<VsyncController>;
113
114 // For tests.
115 VsyncSchedule(PhysicalDisplayId, TrackerPtr, DispatchPtr, ControllerPtr);
116
Dominik Laskowski068173d2021-08-11 17:22:59 -0700117private:
118 friend class TestableScheduler;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800119 friend class android::EventThreadTest;
Leon Scroggins III4d5db7a2023-02-13 15:24:20 -0500120 friend class android::VsyncScheduleTest;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800121 friend class android::fuzz::SchedulerFuzzer;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700122
Leon Scroggins III67388622023-02-06 20:36:20 -0500123 static TrackerPtr createTracker(PhysicalDisplayId);
124 static DispatchPtr createDispatch(TrackerPtr);
125 static ControllerPtr createController(PhysicalDisplayId, VsyncTracker&, FeatureFlags);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700126
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500127 void enableHardwareVsyncLocked(ISchedulerCallback&) REQUIRES(mHwVsyncLock);
128
129 mutable std::mutex mHwVsyncLock;
130 enum class HwVsyncState {
131 // Hardware VSYNCs are currently enabled.
132 Enabled,
133
134 // Hardware VSYNCs are currently disabled. They can be enabled by a call
135 // to `enableHardwareVsync`.
136 Disabled,
137
138 // Hardware VSYNCs are not currently allowed (e.g. because the display
139 // is off).
140 Disallowed,
141
142 ftl_last = Disallowed,
143 };
144 HwVsyncState mHwVsyncState GUARDED_BY(mHwVsyncLock) = HwVsyncState::Disallowed;
145
146 // Pending state, in case an attempt is made to set the state while the
147 // device is off.
148 HwVsyncState mPendingHwVsyncState GUARDED_BY(kMainThreadContext) = HwVsyncState::Disabled;
149
Dominik Laskowski068173d2021-08-11 17:22:59 -0700150 class PredictedVsyncTracer;
151 using TracerPtr = std::unique_ptr<PredictedVsyncTracer>;
152
Leon Scroggins III67388622023-02-06 20:36:20 -0500153 const PhysicalDisplayId mId;
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