xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/Scheduler/VSyncDispatch.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright 2019 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #pragma once
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #include <functional>
20*38e8c45fSAndroid Build Coastguard Worker #include <optional>
21*38e8c45fSAndroid Build Coastguard Worker #include <string>
22*38e8c45fSAndroid Build Coastguard Worker 
23*38e8c45fSAndroid Build Coastguard Worker #include <ftl/mixins.h>
24*38e8c45fSAndroid Build Coastguard Worker #include <scheduler/Time.h>
25*38e8c45fSAndroid Build Coastguard Worker #include <utils/Timers.h>
26*38e8c45fSAndroid Build Coastguard Worker 
27*38e8c45fSAndroid Build Coastguard Worker namespace android::scheduler {
28*38e8c45fSAndroid Build Coastguard Worker 
29*38e8c45fSAndroid Build Coastguard Worker struct ScheduleResult {
30*38e8c45fSAndroid Build Coastguard Worker     TimePoint callbackTime;
31*38e8c45fSAndroid Build Coastguard Worker     TimePoint vsyncTime;
32*38e8c45fSAndroid Build Coastguard Worker };
33*38e8c45fSAndroid Build Coastguard Worker 
34*38e8c45fSAndroid Build Coastguard Worker enum class CancelResult { Cancelled, TooLate, Error };
35*38e8c45fSAndroid Build Coastguard Worker 
36*38e8c45fSAndroid Build Coastguard Worker /*
37*38e8c45fSAndroid Build Coastguard Worker  * VSyncDispatch is a class that will dispatch callbacks relative to system vsync events.
38*38e8c45fSAndroid Build Coastguard Worker  */
39*38e8c45fSAndroid Build Coastguard Worker class VSyncDispatch {
40*38e8c45fSAndroid Build Coastguard Worker public:
41*38e8c45fSAndroid Build Coastguard Worker     struct CallbackToken : ftl::DefaultConstructible<CallbackToken, size_t>,
42*38e8c45fSAndroid Build Coastguard Worker                            ftl::Equatable<CallbackToken>,
43*38e8c45fSAndroid Build Coastguard Worker                            ftl::Incrementable<CallbackToken> {
44*38e8c45fSAndroid Build Coastguard Worker         using DefaultConstructible::DefaultConstructible;
45*38e8c45fSAndroid Build Coastguard Worker     };
46*38e8c45fSAndroid Build Coastguard Worker 
47*38e8c45fSAndroid Build Coastguard Worker     virtual ~VSyncDispatch();
48*38e8c45fSAndroid Build Coastguard Worker 
49*38e8c45fSAndroid Build Coastguard Worker     /*
50*38e8c45fSAndroid Build Coastguard Worker      * A callback that can be registered to be awoken at a given time relative to a vsync event.
51*38e8c45fSAndroid Build Coastguard Worker      * \param [in] vsyncTime:        The timestamp of the vsync the callback is for.
52*38e8c45fSAndroid Build Coastguard Worker      * \param [in] targetWakeupTime: The timestamp of intended wakeup time of the cb.
53*38e8c45fSAndroid Build Coastguard Worker      * \param [in] readyTime:        The timestamp of intended time where client needs to finish
54*38e8c45fSAndroid Build Coastguard Worker      *                               its work by.
55*38e8c45fSAndroid Build Coastguard Worker      */
56*38e8c45fSAndroid Build Coastguard Worker     using Callback =
57*38e8c45fSAndroid Build Coastguard Worker             std::function<void(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime)>;
58*38e8c45fSAndroid Build Coastguard Worker 
59*38e8c45fSAndroid Build Coastguard Worker     /*
60*38e8c45fSAndroid Build Coastguard Worker      * Registers a callback that will be called at designated points on the vsync timeline.
61*38e8c45fSAndroid Build Coastguard Worker      * The callback can be scheduled, rescheduled targeting vsync times, or cancelled.
62*38e8c45fSAndroid Build Coastguard Worker      * The token returned must be cleaned up via unregisterCallback.
63*38e8c45fSAndroid Build Coastguard Worker      *
64*38e8c45fSAndroid Build Coastguard Worker      * \param [in] callbackFn   A function to schedule for callback. The resources needed to invoke
65*38e8c45fSAndroid Build Coastguard Worker      *                          callbackFn must have lifetimes encompassing the lifetime of the
66*38e8c45fSAndroid Build Coastguard Worker      *                          CallbackToken returned.
67*38e8c45fSAndroid Build Coastguard Worker      * \param [in] callbackName A human-readable, unique name to identify the callback.
68*38e8c45fSAndroid Build Coastguard Worker      * \return                  A token that can be used to schedule, reschedule, or cancel the
69*38e8c45fSAndroid Build Coastguard Worker      *                          invocation of callbackFn.
70*38e8c45fSAndroid Build Coastguard Worker      *
71*38e8c45fSAndroid Build Coastguard Worker      */
72*38e8c45fSAndroid Build Coastguard Worker     virtual CallbackToken registerCallback(Callback, std::string callbackName) = 0;
73*38e8c45fSAndroid Build Coastguard Worker 
74*38e8c45fSAndroid Build Coastguard Worker     /*
75*38e8c45fSAndroid Build Coastguard Worker      * Unregisters a callback.
76*38e8c45fSAndroid Build Coastguard Worker      *
77*38e8c45fSAndroid Build Coastguard Worker      * \param [in] token        The callback to unregister.
78*38e8c45fSAndroid Build Coastguard Worker      *
79*38e8c45fSAndroid Build Coastguard Worker      */
80*38e8c45fSAndroid Build Coastguard Worker     virtual void unregisterCallback(CallbackToken token) = 0;
81*38e8c45fSAndroid Build Coastguard Worker 
82*38e8c45fSAndroid Build Coastguard Worker     /*
83*38e8c45fSAndroid Build Coastguard Worker      * Timing information about a scheduled callback
84*38e8c45fSAndroid Build Coastguard Worker      *
85*38e8c45fSAndroid Build Coastguard Worker      * @workDuration:  The time needed for the client to perform its work
86*38e8c45fSAndroid Build Coastguard Worker      * @readyDuration: The time needed for the client to be ready before a vsync event.
87*38e8c45fSAndroid Build Coastguard Worker      *                 For external (non-SF) clients, not only do we need to account for their
88*38e8c45fSAndroid Build Coastguard Worker      *                 workDuration, but we also need to account for the time SF will take to
89*38e8c45fSAndroid Build Coastguard Worker      *                 process their buffer/transaction. In this case, readyDuration will be set
90*38e8c45fSAndroid Build Coastguard Worker      *                 to the SF duration in order to provide enough end-to-end time, and to be
91*38e8c45fSAndroid Build Coastguard Worker      *                 able to provide the ready-by time (deadline) on the callback.
92*38e8c45fSAndroid Build Coastguard Worker      *                 For internal clients, we don't need to add additional padding, so
93*38e8c45fSAndroid Build Coastguard Worker      *                 readyDuration will typically be 0.
94*38e8c45fSAndroid Build Coastguard Worker      * @lastVsync: The targeted display time. This will be snapped to the closest
95*38e8c45fSAndroid Build Coastguard Worker      *                 predicted vsync time after lastVsync.
96*38e8c45fSAndroid Build Coastguard Worker      * @committedVsyncOpt: The display time that is committed to the callback as the
97*38e8c45fSAndroid Build Coastguard Worker      *                 target vsync time.
98*38e8c45fSAndroid Build Coastguard Worker      *
99*38e8c45fSAndroid Build Coastguard Worker      * callback will be dispatched at 'workDuration + readyDuration' nanoseconds before a vsync
100*38e8c45fSAndroid Build Coastguard Worker      * event.
101*38e8c45fSAndroid Build Coastguard Worker      */
102*38e8c45fSAndroid Build Coastguard Worker     struct ScheduleTiming {
103*38e8c45fSAndroid Build Coastguard Worker         nsecs_t workDuration = 0;
104*38e8c45fSAndroid Build Coastguard Worker         nsecs_t readyDuration = 0;
105*38e8c45fSAndroid Build Coastguard Worker         nsecs_t lastVsync = 0;
106*38e8c45fSAndroid Build Coastguard Worker         std::optional<nsecs_t> committedVsyncOpt;
107*38e8c45fSAndroid Build Coastguard Worker 
108*38e8c45fSAndroid Build Coastguard Worker         bool operator==(const ScheduleTiming& other) const {
109*38e8c45fSAndroid Build Coastguard Worker             return workDuration == other.workDuration && readyDuration == other.readyDuration &&
110*38e8c45fSAndroid Build Coastguard Worker                     lastVsync == other.lastVsync && committedVsyncOpt == other.committedVsyncOpt;
111*38e8c45fSAndroid Build Coastguard Worker         }
112*38e8c45fSAndroid Build Coastguard Worker 
113*38e8c45fSAndroid Build Coastguard Worker         bool operator!=(const ScheduleTiming& other) const { return !(*this == other); }
114*38e8c45fSAndroid Build Coastguard Worker     };
115*38e8c45fSAndroid Build Coastguard Worker 
116*38e8c45fSAndroid Build Coastguard Worker     /*
117*38e8c45fSAndroid Build Coastguard Worker      * Schedules the registered callback to be dispatched.
118*38e8c45fSAndroid Build Coastguard Worker      *
119*38e8c45fSAndroid Build Coastguard Worker      * The callback will be dispatched at 'workDuration + readyDuration' nanoseconds before a vsync
120*38e8c45fSAndroid Build Coastguard Worker      * event.
121*38e8c45fSAndroid Build Coastguard Worker      *
122*38e8c45fSAndroid Build Coastguard Worker      * The caller designates the earliest vsync event that should be targeted by the lastVsync
123*38e8c45fSAndroid Build Coastguard Worker      * parameter.
124*38e8c45fSAndroid Build Coastguard Worker      * The callback will be scheduled at (workDuration + readyDuration - predictedVsync), where
125*38e8c45fSAndroid Build Coastguard Worker      * predictedVsync is the first vsync event time where ( predictedVsync >= lastVsync ).
126*38e8c45fSAndroid Build Coastguard Worker      *
127*38e8c45fSAndroid Build Coastguard Worker      * If (workDuration + readyDuration - lastVsync) is in the past, or if a callback has
128*38e8c45fSAndroid Build Coastguard Worker      * already been dispatched for the predictedVsync, an error will be returned.
129*38e8c45fSAndroid Build Coastguard Worker      *
130*38e8c45fSAndroid Build Coastguard Worker      * It is valid to reschedule a callback to a different time.
131*38e8c45fSAndroid Build Coastguard Worker      *
132*38e8c45fSAndroid Build Coastguard Worker      * \param [in] token           The callback to schedule.
133*38e8c45fSAndroid Build Coastguard Worker      * \param [in] scheduleTiming  The timing information for this schedule call
134*38e8c45fSAndroid Build Coastguard Worker      * \return                     The expected callback time if a callback was scheduled,
135*38e8c45fSAndroid Build Coastguard Worker      *                             along with VSYNC time for the callback scheduled.
136*38e8c45fSAndroid Build Coastguard Worker      *                             std::nullopt if the callback is not registered.
137*38e8c45fSAndroid Build Coastguard Worker      */
138*38e8c45fSAndroid Build Coastguard Worker     virtual std::optional<ScheduleResult> schedule(CallbackToken token,
139*38e8c45fSAndroid Build Coastguard Worker                                                    ScheduleTiming scheduleTiming) = 0;
140*38e8c45fSAndroid Build Coastguard Worker 
141*38e8c45fSAndroid Build Coastguard Worker     /*
142*38e8c45fSAndroid Build Coastguard Worker      * Update the timing information for a scheduled callback.
143*38e8c45fSAndroid Build Coastguard Worker      * If the callback is not scheduled, then this function does nothing.
144*38e8c45fSAndroid Build Coastguard Worker      *
145*38e8c45fSAndroid Build Coastguard Worker      * \param [in] token           The callback to schedule.
146*38e8c45fSAndroid Build Coastguard Worker      * \param [in] scheduleTiming  The timing information for this schedule call
147*38e8c45fSAndroid Build Coastguard Worker      * \return                     The expected callback time if a callback was scheduled,
148*38e8c45fSAndroid Build Coastguard Worker      *                             along with VSYNC time for the callback scheduled.
149*38e8c45fSAndroid Build Coastguard Worker      *                             std::nullopt if the callback is not registered.
150*38e8c45fSAndroid Build Coastguard Worker      */
151*38e8c45fSAndroid Build Coastguard Worker     virtual std::optional<ScheduleResult> update(CallbackToken token,
152*38e8c45fSAndroid Build Coastguard Worker                                                  ScheduleTiming scheduleTiming) = 0;
153*38e8c45fSAndroid Build Coastguard Worker 
154*38e8c45fSAndroid Build Coastguard Worker     /* Cancels a scheduled callback, if possible.
155*38e8c45fSAndroid Build Coastguard Worker      *
156*38e8c45fSAndroid Build Coastguard Worker      * \param [in] token    The callback to cancel.
157*38e8c45fSAndroid Build Coastguard Worker      * \return              A CancelResult::TooLate if the callback was already dispatched.
158*38e8c45fSAndroid Build Coastguard Worker      *                      A CancelResult::Cancelled if the callback was successfully cancelled.
159*38e8c45fSAndroid Build Coastguard Worker      *                      A CancelResult::Error if there was an pre-condition violation.
160*38e8c45fSAndroid Build Coastguard Worker      */
161*38e8c45fSAndroid Build Coastguard Worker     virtual CancelResult cancel(CallbackToken token) = 0;
162*38e8c45fSAndroid Build Coastguard Worker 
163*38e8c45fSAndroid Build Coastguard Worker     virtual void dump(std::string& result) const = 0;
164*38e8c45fSAndroid Build Coastguard Worker 
165*38e8c45fSAndroid Build Coastguard Worker protected:
166*38e8c45fSAndroid Build Coastguard Worker     VSyncDispatch() = default;
167*38e8c45fSAndroid Build Coastguard Worker 
168*38e8c45fSAndroid Build Coastguard Worker     VSyncDispatch(const VSyncDispatch&) = delete;
169*38e8c45fSAndroid Build Coastguard Worker     VSyncDispatch& operator=(const VSyncDispatch&) = delete;
170*38e8c45fSAndroid Build Coastguard Worker };
171*38e8c45fSAndroid Build Coastguard Worker 
172*38e8c45fSAndroid Build Coastguard Worker class VSyncCallbackRegistration {
173*38e8c45fSAndroid Build Coastguard Worker public:
174*38e8c45fSAndroid Build Coastguard Worker     VSyncCallbackRegistration(std::shared_ptr<VSyncDispatch>, VSyncDispatch::Callback,
175*38e8c45fSAndroid Build Coastguard Worker                               std::string callbackName);
176*38e8c45fSAndroid Build Coastguard Worker     ~VSyncCallbackRegistration();
177*38e8c45fSAndroid Build Coastguard Worker 
178*38e8c45fSAndroid Build Coastguard Worker     VSyncCallbackRegistration(VSyncCallbackRegistration&&);
179*38e8c45fSAndroid Build Coastguard Worker     VSyncCallbackRegistration& operator=(VSyncCallbackRegistration&&);
180*38e8c45fSAndroid Build Coastguard Worker 
181*38e8c45fSAndroid Build Coastguard Worker     // See documentation for VSyncDispatch::schedule.
182*38e8c45fSAndroid Build Coastguard Worker     std::optional<ScheduleResult> schedule(VSyncDispatch::ScheduleTiming scheduleTiming);
183*38e8c45fSAndroid Build Coastguard Worker 
184*38e8c45fSAndroid Build Coastguard Worker     // See documentation for VSyncDispatch::update.
185*38e8c45fSAndroid Build Coastguard Worker     std::optional<ScheduleResult> update(VSyncDispatch::ScheduleTiming scheduleTiming);
186*38e8c45fSAndroid Build Coastguard Worker 
187*38e8c45fSAndroid Build Coastguard Worker     // See documentation for VSyncDispatch::cancel.
188*38e8c45fSAndroid Build Coastguard Worker     CancelResult cancel();
189*38e8c45fSAndroid Build Coastguard Worker 
190*38e8c45fSAndroid Build Coastguard Worker private:
191*38e8c45fSAndroid Build Coastguard Worker     friend class VSyncCallbackRegistrationTest;
192*38e8c45fSAndroid Build Coastguard Worker 
193*38e8c45fSAndroid Build Coastguard Worker     std::shared_ptr<VSyncDispatch> mDispatch;
194*38e8c45fSAndroid Build Coastguard Worker     std::optional<VSyncDispatch::CallbackToken> mToken;
195*38e8c45fSAndroid Build Coastguard Worker };
196*38e8c45fSAndroid Build Coastguard Worker 
197*38e8c45fSAndroid Build Coastguard Worker } // namespace android::scheduler
198