blob: ca61f875c3c4f03a05e871f426d3b1eb8757d468 [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"
ramindanid4354a92023-10-02 15:11:09 -070034#include "VSyncTracker.h"
Dominik Laskowskib418dd72023-06-13 17:31:04 -040035
Ady Abraham011f8ba2022-11-22 15:09:07 -080036namespace android {
37class EventThreadTest;
Leon Scroggins III58af6ed2023-02-13 15:24:20 -050038class VsyncScheduleTest;
Ady Abraham011f8ba2022-11-22 15:09:07 -080039}
40
41namespace android::fuzz {
42class SchedulerFuzzer;
43}
44
Dominik Laskowski068173d2021-08-11 17:22:59 -070045namespace android::scheduler {
46
47// TODO(b/185535769): Rename classes, and remove aliases.
48class VSyncDispatch;
49class VSyncTracker;
50
51class VsyncController;
52using VsyncDispatch = VSyncDispatch;
53using VsyncTracker = VSyncTracker;
54
55// Schedule that synchronizes to hardware VSYNC of a physical display.
Dominik Laskowskib418dd72023-06-13 17:31:04 -040056class VsyncSchedule final : public IVsyncSource {
Dominik Laskowski068173d2021-08-11 17:22:59 -070057public:
Dominik Laskowski66295432023-03-14 12:25:36 -040058 using RequestHardwareVsync = std::function<void(PhysicalDisplayId, bool enabled)>;
59
ramindanid4354a92023-10-02 15:11:09 -070060 VsyncSchedule(PhysicalDisplayId, FeatureFlags, RequestHardwareVsync, IVsyncTrackerCallback&);
Dominik Laskowski068173d2021-08-11 17:22:59 -070061 ~VsyncSchedule();
62
Dominik Laskowskib418dd72023-06-13 17:31:04 -040063 // IVsyncSource overrides:
64 Period period() const override;
65 TimePoint vsyncDeadlineAfter(TimePoint) const override;
Dominik Laskowski5d164f22022-07-07 07:56:07 -070066
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050067 // Inform the schedule that the period is changing and the schedule needs to recalibrate
68 // itself. The schedule will end the period transition internally. This will
69 // enable hardware VSYNCs in order to calibrate.
70 //
71 // \param [in] period The period that the system is changing into.
Leon Scroggins III67388622023-02-06 20:36:20 -050072 // \param [in] force True to force a transition even if it is not a
73 // change.
Dominik Laskowski66295432023-03-14 12:25:36 -040074 void startPeriodTransition(Period period, bool force);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050075
76 // Pass a VSYNC sample to VsyncController. Return true if
77 // VsyncController detected that the VSYNC period changed. Enable or disable
78 // hardware VSYNCs depending on whether more samples are needed.
Dominik Laskowski66295432023-03-14 12:25:36 -040079 bool addResyncSample(TimePoint timestamp, ftl::Optional<Period> hwcVsyncPeriod);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050080
Dominik Laskowski068173d2021-08-11 17:22:59 -070081 // TODO(b/185535769): Hide behind API.
82 const VsyncTracker& getTracker() const { return *mTracker; }
83 VsyncTracker& getTracker() { return *mTracker; }
84 VsyncController& getController() { return *mController; }
85
Leon Scroggins III67388622023-02-06 20:36:20 -050086 // TODO(b/185535769): Once these are hidden behind the API, they may no
87 // longer need to be shared_ptrs.
88 using DispatchPtr = std::shared_ptr<VsyncDispatch>;
89 using TrackerPtr = std::shared_ptr<VsyncTracker>;
90
Dominik Laskowski068173d2021-08-11 17:22:59 -070091 // TODO(b/185535769): Remove once VsyncSchedule owns all registrations.
Leon Scroggins III67388622023-02-06 20:36:20 -050092 DispatchPtr getDispatch() { return mDispatch; }
Dominik Laskowski068173d2021-08-11 17:22:59 -070093
94 void dump(std::string&) const;
95
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050096 // Turn on hardware VSYNCs, unless mHwVsyncState is Disallowed, in which
97 // case this call is ignored.
Dominik Laskowski66295432023-03-14 12:25:36 -040098 void enableHardwareVsync() EXCLUDES(mHwVsyncLock);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -050099
100 // Disable hardware VSYNCs. If `disallow` is true, future calls to
Leon Scroggins III67388622023-02-06 20:36:20 -0500101 // enableHardwareVsync are ineffective until isHardwareVsyncAllowed is
102 // called with `makeAllowed` set to true.
Dominik Laskowski66295432023-03-14 12:25:36 -0400103 void disableHardwareVsync(bool disallow) EXCLUDES(mHwVsyncLock);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500104
105 // If true, enableHardwareVsync can enable hardware VSYNC (if not already
106 // enabled). If false, enableHardwareVsync does nothing.
107 bool isHardwareVsyncAllowed(bool makeAllowed) EXCLUDES(mHwVsyncLock);
108
109 void setPendingHardwareVsyncState(bool enabled) REQUIRES(kMainThreadContext);
110
111 bool getPendingHardwareVsyncState() const REQUIRES(kMainThreadContext);
112
Leon Scroggins III67388622023-02-06 20:36:20 -0500113protected:
114 using ControllerPtr = std::unique_ptr<VsyncController>;
115
Dominik Laskowski66295432023-03-14 12:25:36 -0400116 static void NoOpRequestHardwareVsync(PhysicalDisplayId, bool) {}
117
Leon Scroggins III67388622023-02-06 20:36:20 -0500118 // For tests.
Dominik Laskowski66295432023-03-14 12:25:36 -0400119 VsyncSchedule(PhysicalDisplayId, TrackerPtr, DispatchPtr, ControllerPtr,
120 RequestHardwareVsync = NoOpRequestHardwareVsync);
Leon Scroggins III67388622023-02-06 20:36:20 -0500121
Dominik Laskowski068173d2021-08-11 17:22:59 -0700122private:
123 friend class TestableScheduler;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800124 friend class android::EventThreadTest;
Leon Scroggins III58af6ed2023-02-13 15:24:20 -0500125 friend class android::VsyncScheduleTest;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800126 friend class android::fuzz::SchedulerFuzzer;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700127
ramindanid4354a92023-10-02 15:11:09 -0700128 static TrackerPtr createTracker(PhysicalDisplayId, IVsyncTrackerCallback&);
Leon Scroggins III67388622023-02-06 20:36:20 -0500129 static DispatchPtr createDispatch(TrackerPtr);
130 static ControllerPtr createController(PhysicalDisplayId, VsyncTracker&, FeatureFlags);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700131
Dominik Laskowski66295432023-03-14 12:25:36 -0400132 void enableHardwareVsyncLocked() REQUIRES(mHwVsyncLock);
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500133
134 mutable std::mutex mHwVsyncLock;
135 enum class HwVsyncState {
136 // Hardware VSYNCs are currently enabled.
137 Enabled,
138
139 // Hardware VSYNCs are currently disabled. They can be enabled by a call
140 // to `enableHardwareVsync`.
141 Disabled,
142
143 // Hardware VSYNCs are not currently allowed (e.g. because the display
144 // is off).
145 Disallowed,
146
147 ftl_last = Disallowed,
148 };
149 HwVsyncState mHwVsyncState GUARDED_BY(mHwVsyncLock) = HwVsyncState::Disallowed;
150
151 // Pending state, in case an attempt is made to set the state while the
152 // device is off.
153 HwVsyncState mPendingHwVsyncState GUARDED_BY(kMainThreadContext) = HwVsyncState::Disabled;
154
Dominik Laskowski068173d2021-08-11 17:22:59 -0700155 class PredictedVsyncTracer;
156 using TracerPtr = std::unique_ptr<PredictedVsyncTracer>;
157
Leon Scroggins III67388622023-02-06 20:36:20 -0500158 const PhysicalDisplayId mId;
Dominik Laskowski66295432023-03-14 12:25:36 -0400159 const RequestHardwareVsync mRequestHardwareVsync;
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500160 const TrackerPtr mTracker;
161 const DispatchPtr mDispatch;
162 const ControllerPtr mController;
163 const TracerPtr mTracer;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700164};
165
166} // namespace android::scheduler