blob: c037ac850257a49d6e8d69e935ae925222a2d585 [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 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 Laskowskie85d7fa2023-05-15 10:50:22 -040026#include <ui/DisplayId.h>
Dominik Laskowskic183eed2023-01-21 10:02:15 -050027
Dominik Laskowskib418dd72023-06-13 17:31:04 -040028#include <scheduler/Features.h>
29#include <scheduler/IVsyncSource.h>
30#include <scheduler/Time.h>
31
32#include "ThreadContext.h"
33
Ady Abraham011f8ba2022-11-22 15:09:07 -080034namespace android {
35class EventThreadTest;
Leon Scroggins III4d5db7a2023-02-13 15:24:20 -050036class VsyncScheduleTest;
Ady Abraham011f8ba2022-11-22 15:09:07 -080037}
38
39namespace android::fuzz {
40class SchedulerFuzzer;
41}
42
Dominik Laskowski068173d2021-08-11 17:22:59 -070043namespace android::scheduler {
44
45// TODO(b/185535769): Rename classes, and remove aliases.
46class VSyncDispatch;
47class VSyncTracker;
48
49class VsyncController;
50using VsyncDispatch = VSyncDispatch;
51using VsyncTracker = VSyncTracker;
52
53// Schedule that synchronizes to hardware VSYNC of a physical display.
Dominik Laskowskib418dd72023-06-13 17:31:04 -040054class VsyncSchedule final : public IVsyncSource {
Dominik Laskowski068173d2021-08-11 17:22:59 -070055public:
Dominik Laskowski66295432023-03-14 12:25:36 -040056 using RequestHardwareVsync = std::function<void(PhysicalDisplayId, bool enabled)>;
57
58 VsyncSchedule(PhysicalDisplayId, FeatureFlags, RequestHardwareVsync);
Dominik Laskowski068173d2021-08-11 17:22:59 -070059 ~VsyncSchedule();
60
Dominik Laskowskib418dd72023-06-13 17:31:04 -040061 // IVsyncSource overrides:
62 Period period() const override;
63 TimePoint vsyncDeadlineAfter(TimePoint) const override;
Dominik Laskowski5d164f22022-07-07 07:56:07 -070064
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050065 // Inform the schedule that the period is changing and the schedule needs to recalibrate
66 // itself. The schedule will end the period transition internally. This will
67 // enable hardware VSYNCs in order to calibrate.
68 //
69 // \param [in] period The period that the system is changing into.
Leon Scroggins III67388622023-02-06 20:36:20 -050070 // \param [in] force True to force a transition even if it is not a
71 // change.
Dominik Laskowski66295432023-03-14 12:25:36 -040072 void startPeriodTransition(Period period, bool force);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050073
74 // Pass a VSYNC sample to VsyncController. Return true if
75 // VsyncController detected that the VSYNC period changed. Enable or disable
76 // hardware VSYNCs depending on whether more samples are needed.
Dominik Laskowski66295432023-03-14 12:25:36 -040077 bool addResyncSample(TimePoint timestamp, ftl::Optional<Period> hwcVsyncPeriod);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050078
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.
Dominik Laskowski66295432023-03-14 12:25:36 -040096 void enableHardwareVsync() EXCLUDES(mHwVsyncLock);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050097
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.
Dominik Laskowski66295432023-03-14 12:25:36 -0400101 void disableHardwareVsync(bool disallow) EXCLUDES(mHwVsyncLock);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500102
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
Dominik Laskowski66295432023-03-14 12:25:36 -0400114 static void NoOpRequestHardwareVsync(PhysicalDisplayId, bool) {}
115
Leon Scroggins III67388622023-02-06 20:36:20 -0500116 // For tests.
Dominik Laskowski66295432023-03-14 12:25:36 -0400117 VsyncSchedule(PhysicalDisplayId, TrackerPtr, DispatchPtr, ControllerPtr,
118 RequestHardwareVsync = NoOpRequestHardwareVsync);
Leon Scroggins III67388622023-02-06 20:36:20 -0500119
Dominik Laskowski068173d2021-08-11 17:22:59 -0700120private:
121 friend class TestableScheduler;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800122 friend class android::EventThreadTest;
Leon Scroggins III4d5db7a2023-02-13 15:24:20 -0500123 friend class android::VsyncScheduleTest;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800124 friend class android::fuzz::SchedulerFuzzer;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700125
Leon Scroggins III67388622023-02-06 20:36:20 -0500126 static TrackerPtr createTracker(PhysicalDisplayId);
127 static DispatchPtr createDispatch(TrackerPtr);
128 static ControllerPtr createController(PhysicalDisplayId, VsyncTracker&, FeatureFlags);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700129
Dominik Laskowski66295432023-03-14 12:25:36 -0400130 void enableHardwareVsyncLocked() REQUIRES(mHwVsyncLock);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500131
132 mutable std::mutex mHwVsyncLock;
133 enum class HwVsyncState {
134 // Hardware VSYNCs are currently enabled.
135 Enabled,
136
137 // Hardware VSYNCs are currently disabled. They can be enabled by a call
138 // to `enableHardwareVsync`.
139 Disabled,
140
141 // Hardware VSYNCs are not currently allowed (e.g. because the display
142 // is off).
143 Disallowed,
144
145 ftl_last = Disallowed,
146 };
147 HwVsyncState mHwVsyncState GUARDED_BY(mHwVsyncLock) = HwVsyncState::Disallowed;
148
149 // Pending state, in case an attempt is made to set the state while the
150 // device is off.
151 HwVsyncState mPendingHwVsyncState GUARDED_BY(kMainThreadContext) = HwVsyncState::Disabled;
152
Rachel Lee2db205d2023-05-08 11:08:59 -0700153 // Whether to reset the timestamps stored in the vsync model on the next hw vsync sample. This
154 // is to avoid clearing the model when hw vsync is enabled, in order to be consistent with the
155 // stale timestamps. Instead, clear the model on the first hw vsync callback.
156 bool mClearTimestampsOnNextSample = false;
157
Dominik Laskowski068173d2021-08-11 17:22:59 -0700158 class PredictedVsyncTracer;
159 using TracerPtr = std::unique_ptr<PredictedVsyncTracer>;
160
Leon Scroggins III67388622023-02-06 20:36:20 -0500161 const PhysicalDisplayId mId;
Dominik Laskowski66295432023-03-14 12:25:36 -0400162 const RequestHardwareVsync mRequestHardwareVsync;
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500163 const TrackerPtr mTracker;
164 const DispatchPtr mDispatch;
165 const ControllerPtr mController;
166 const TracerPtr mTracer;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700167};
168
169} // namespace android::scheduler