blob: 47e92e107d01082c6e45906afd64347ae548626c [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
Dominik Laskowski66295432023-03-14 12:25:36 -040019#include <functional>
Dominik Laskowski068173d2021-08-11 17:22:59 -070020#include <memory>
21#include <string>
22
Leon Scroggins III58af6ed2023-02-13 15:24:20 -050023#include <android-base/thread_annotations.h>
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050024#include <ThreadContext.h>
25#include <ftl/enum.h>
26#include <ftl/optional.h>
Dominik Laskowskie85d7fa2023-05-15 10:50:22 -040027#include <ui/DisplayId.h>
Dominik Laskowskic183eed2023-01-21 10:02:15 -050028
Dominik Laskowskib418dd72023-06-13 17:31:04 -040029#include <scheduler/Features.h>
30#include <scheduler/IVsyncSource.h>
31#include <scheduler/Time.h>
32
33#include "ThreadContext.h"
34
Ady Abraham011f8ba2022-11-22 15:09:07 -080035namespace android {
36class EventThreadTest;
Leon Scroggins III58af6ed2023-02-13 15:24:20 -050037class VsyncScheduleTest;
Ady Abraham011f8ba2022-11-22 15:09:07 -080038}
39
40namespace android::fuzz {
41class SchedulerFuzzer;
42}
43
Dominik Laskowski068173d2021-08-11 17:22:59 -070044namespace android::scheduler {
45
46// 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 Laskowskib418dd72023-06-13 17:31:04 -040055class VsyncSchedule final : public IVsyncSource {
Dominik Laskowski068173d2021-08-11 17:22:59 -070056public:
Dominik Laskowski66295432023-03-14 12:25:36 -040057 using RequestHardwareVsync = std::function<void(PhysicalDisplayId, bool enabled)>;
58
59 VsyncSchedule(PhysicalDisplayId, FeatureFlags, RequestHardwareVsync);
Dominik Laskowski068173d2021-08-11 17:22:59 -070060 ~VsyncSchedule();
61
Dominik Laskowskib418dd72023-06-13 17:31:04 -040062 // IVsyncSource overrides:
63 Period period() const override;
64 TimePoint vsyncDeadlineAfter(TimePoint) const override;
Dominik Laskowski5d164f22022-07-07 07:56:07 -070065
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050066 // Inform the schedule that the period is changing and the schedule needs to recalibrate
67 // itself. The schedule will end the period transition internally. This will
68 // enable hardware VSYNCs in order to calibrate.
69 //
70 // \param [in] period The period that the system is changing into.
Leon Scroggins III67388622023-02-06 20:36:20 -050071 // \param [in] force True to force a transition even if it is not a
72 // change.
Dominik Laskowski66295432023-03-14 12:25:36 -040073 void startPeriodTransition(Period period, bool force);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050074
75 // Pass a VSYNC sample to VsyncController. Return true if
76 // VsyncController detected that the VSYNC period changed. Enable or disable
77 // hardware VSYNCs depending on whether more samples are needed.
Dominik Laskowski66295432023-03-14 12:25:36 -040078 bool addResyncSample(TimePoint timestamp, ftl::Optional<Period> hwcVsyncPeriod);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050079
Dominik Laskowski068173d2021-08-11 17:22:59 -070080 // TODO(b/185535769): Hide behind API.
81 const VsyncTracker& getTracker() const { return *mTracker; }
82 VsyncTracker& getTracker() { return *mTracker; }
83 VsyncController& getController() { return *mController; }
84
Leon Scroggins III67388622023-02-06 20:36:20 -050085 // TODO(b/185535769): Once these are hidden behind the API, they may no
86 // longer need to be shared_ptrs.
87 using DispatchPtr = std::shared_ptr<VsyncDispatch>;
88 using TrackerPtr = std::shared_ptr<VsyncTracker>;
89
Dominik Laskowski068173d2021-08-11 17:22:59 -070090 // TODO(b/185535769): Remove once VsyncSchedule owns all registrations.
Leon Scroggins III67388622023-02-06 20:36:20 -050091 DispatchPtr getDispatch() { return mDispatch; }
Dominik Laskowski068173d2021-08-11 17:22:59 -070092
93 void dump(std::string&) const;
94
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050095 // Turn on hardware VSYNCs, unless mHwVsyncState is Disallowed, in which
96 // case this call is ignored.
Dominik Laskowski66295432023-03-14 12:25:36 -040097 void enableHardwareVsync() EXCLUDES(mHwVsyncLock);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050098
99 // Disable hardware VSYNCs. If `disallow` is true, future calls to
Leon Scroggins III67388622023-02-06 20:36:20 -0500100 // enableHardwareVsync are ineffective until isHardwareVsyncAllowed is
101 // called with `makeAllowed` set to true.
Dominik Laskowski66295432023-03-14 12:25:36 -0400102 void disableHardwareVsync(bool disallow) EXCLUDES(mHwVsyncLock);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500103
104 // If true, enableHardwareVsync can enable hardware VSYNC (if not already
105 // enabled). If false, enableHardwareVsync does nothing.
106 bool isHardwareVsyncAllowed(bool makeAllowed) EXCLUDES(mHwVsyncLock);
107
108 void setPendingHardwareVsyncState(bool enabled) REQUIRES(kMainThreadContext);
109
110 bool getPendingHardwareVsyncState() const REQUIRES(kMainThreadContext);
111
Leon Scroggins III67388622023-02-06 20:36:20 -0500112protected:
113 using ControllerPtr = std::unique_ptr<VsyncController>;
114
Dominik Laskowski66295432023-03-14 12:25:36 -0400115 static void NoOpRequestHardwareVsync(PhysicalDisplayId, bool) {}
116
Leon Scroggins III67388622023-02-06 20:36:20 -0500117 // For tests.
Dominik Laskowski66295432023-03-14 12:25:36 -0400118 VsyncSchedule(PhysicalDisplayId, TrackerPtr, DispatchPtr, ControllerPtr,
119 RequestHardwareVsync = NoOpRequestHardwareVsync);
Leon Scroggins III67388622023-02-06 20:36:20 -0500120
Dominik Laskowski068173d2021-08-11 17:22:59 -0700121private:
122 friend class TestableScheduler;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800123 friend class android::EventThreadTest;
Leon Scroggins III58af6ed2023-02-13 15:24:20 -0500124 friend class android::VsyncScheduleTest;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800125 friend class android::fuzz::SchedulerFuzzer;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700126
Leon Scroggins III67388622023-02-06 20:36:20 -0500127 static TrackerPtr createTracker(PhysicalDisplayId);
128 static DispatchPtr createDispatch(TrackerPtr);
129 static ControllerPtr createController(PhysicalDisplayId, VsyncTracker&, FeatureFlags);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700130
Dominik Laskowski66295432023-03-14 12:25:36 -0400131 void enableHardwareVsyncLocked() REQUIRES(mHwVsyncLock);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500132
133 mutable std::mutex mHwVsyncLock;
134 enum class HwVsyncState {
135 // Hardware VSYNCs are currently enabled.
136 Enabled,
137
138 // Hardware VSYNCs are currently disabled. They can be enabled by a call
139 // to `enableHardwareVsync`.
140 Disabled,
141
142 // Hardware VSYNCs are not currently allowed (e.g. because the display
143 // is off).
144 Disallowed,
145
146 ftl_last = Disallowed,
147 };
148 HwVsyncState mHwVsyncState GUARDED_BY(mHwVsyncLock) = HwVsyncState::Disallowed;
149
150 // Pending state, in case an attempt is made to set the state while the
151 // device is off.
152 HwVsyncState mPendingHwVsyncState GUARDED_BY(kMainThreadContext) = HwVsyncState::Disabled;
153
Dominik Laskowski068173d2021-08-11 17:22:59 -0700154 class PredictedVsyncTracer;
155 using TracerPtr = std::unique_ptr<PredictedVsyncTracer>;
156
Leon Scroggins III67388622023-02-06 20:36:20 -0500157 const PhysicalDisplayId mId;
Dominik Laskowski66295432023-03-14 12:25:36 -0400158 const RequestHardwareVsync mRequestHardwareVsync;
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500159 const TrackerPtr mTracker;
160 const DispatchPtr mDispatch;
161 const ControllerPtr mController;
162 const TracerPtr mTracer;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700163};
164
165} // namespace android::scheduler