blob: 9d7110327b01ef209e425869e464f0cc7ce20ef7 [file] [log] [blame]
Kevin DuBois305bef12019-10-09 13:23:27 -07001/*
2 * Copyright 2019 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
Kevin DuBois305bef12019-10-09 13:23:27 -070019#include <utils/Timers.h>
20#include <functional>
Kevin DuBois305bef12019-10-09 13:23:27 -070021#include <string>
Kevin DuBois305bef12019-10-09 13:23:27 -070022
23#include "StrongTyping.h"
24
25namespace android::scheduler {
26class TimeKeeper;
27class VSyncTracker;
28
Kevin DuBoisc94ca832019-11-26 12:56:24 -080029enum class ScheduleResult { Scheduled, CannotSchedule, Error };
Kevin DuBois305bef12019-10-09 13:23:27 -070030enum class CancelResult { Cancelled, TooLate, Error };
31
Kevin DuBois305bef12019-10-09 13:23:27 -070032/*
33 * VSyncDispatch is a class that will dispatch callbacks relative to system vsync events.
34 */
35class VSyncDispatch {
36public:
Ady Abraham2139f732019-11-13 18:56:40 -080037 using CallbackToken = StrongTyping<size_t, class CallbackTokenTag, Compare, Hash>;
Kevin DuBois305bef12019-10-09 13:23:27 -070038
Kevin DuBoise4f27a82019-11-12 11:41:41 -080039 virtual ~VSyncDispatch();
Kevin DuBois305bef12019-10-09 13:23:27 -070040
41 /*
Kevin DuBois2968afc2020-01-14 09:48:50 -080042 * A callback that can be registered to be awoken at a given time relative to a vsync event.
Ady Abraham9c53ee72020-07-22 21:16:18 -070043 * \param [in] vsyncTime: The timestamp of the vsync the callback is for.
44 * \param [in] targetWakeupTime: The timestamp of intended wakeup time of the cb.
45 * \param [in] readyTime: The timestamp of intended time where client needs to finish
46 * its work by.
Kevin DuBois2968afc2020-01-14 09:48:50 -080047 */
Ady Abraham9c53ee72020-07-22 21:16:18 -070048 using Callback =
49 std::function<void(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime)>;
Kevin DuBois2968afc2020-01-14 09:48:50 -080050
51 /*
Kevin DuBois305bef12019-10-09 13:23:27 -070052 * Registers a callback that will be called at designated points on the vsync timeline.
53 * The callback can be scheduled, rescheduled targeting vsync times, or cancelled.
54 * The token returned must be cleaned up via unregisterCallback.
55 *
56 * \param [in] callbackFn A function to schedule for callback. The resources needed to invoke
57 * callbackFn must have lifetimes encompassing the lifetime of the
58 * CallbackToken returned.
59 * \param [in] callbackName A human-readable, unique name to identify the callback.
60 * \return A token that can be used to schedule, reschedule, or cancel the
61 * invocation of callbackFn.
62 *
63 */
Kevin DuBois2968afc2020-01-14 09:48:50 -080064 virtual CallbackToken registerCallback(Callback const& callbackFn,
Kevin DuBoise4f27a82019-11-12 11:41:41 -080065 std::string callbackName) = 0;
Kevin DuBois305bef12019-10-09 13:23:27 -070066
67 /*
68 * Unregisters a callback.
69 *
70 * \param [in] token The callback to unregister.
71 *
72 */
Kevin DuBoise4f27a82019-11-12 11:41:41 -080073 virtual void unregisterCallback(CallbackToken token) = 0;
Kevin DuBois305bef12019-10-09 13:23:27 -070074
75 /*
Ady Abraham9c53ee72020-07-22 21:16:18 -070076 * Timing information about a scheduled callback
77 *
78 * @workDuration: The time needed for the client to perform its work
79 * @readyDuration: The time needed for the client to be ready before a vsync event.
80 * For external (non-SF) clients, not only do we need to account for their
81 * workDuration, but we also need to account for the time SF will take to
82 * process their buffer/transaction. In this case, readyDuration will be set
83 * to the SF duration in order to provide enough end-to-end time, and to be
84 * able to provide the ready-by time (deadline) on the callback.
85 * For internal clients, we don't need to add additional padding, so
86 * readyDuration will typically be 0.
87 * @earliestVsync: The targeted display time. This will be snapped to the closest
88 * predicted vsync time after earliestVsync.
89 *
90 * callback will be dispatched at 'workDuration + readyDuration' nanoseconds before a vsync
91 * event.
92 */
93 struct ScheduleTiming {
94 nsecs_t workDuration = 0;
95 nsecs_t readyDuration = 0;
96 nsecs_t earliestVsync = 0;
Ady Abraham55fa7272020-09-30 19:19:27 -070097
98 bool operator==(const ScheduleTiming& other) const {
99 return workDuration == other.workDuration && readyDuration == other.readyDuration &&
100 earliestVsync == other.earliestVsync;
101 }
102
103 bool operator!=(const ScheduleTiming& other) const { return !(*this == other); }
Ady Abraham9c53ee72020-07-22 21:16:18 -0700104 };
105
106 /*
Kevin DuBois305bef12019-10-09 13:23:27 -0700107 * Schedules the registered callback to be dispatched.
108 *
Ady Abraham9c53ee72020-07-22 21:16:18 -0700109 * The callback will be dispatched at 'workDuration + readyDuration' nanoseconds before a vsync
110 * event.
Kevin DuBois305bef12019-10-09 13:23:27 -0700111 *
112 * The caller designates the earliest vsync event that should be targeted by the earliestVsync
113 * parameter.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700114 * The callback will be scheduled at (workDuration + readyDuration - predictedVsync), where
115 * predictedVsync is the first vsync event time where ( predictedVsync >= earliestVsync ).
Kevin DuBois305bef12019-10-09 13:23:27 -0700116 *
Ady Abraham9c53ee72020-07-22 21:16:18 -0700117 * If (workDuration + readyDuration - earliestVsync) is in the past, or if a callback has
118 * already been dispatched for the predictedVsync, an error will be returned.
Kevin DuBois305bef12019-10-09 13:23:27 -0700119 *
120 * It is valid to reschedule a callback to a different time.
121 *
122 * \param [in] token The callback to schedule.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700123 * \param [in] scheduleTiming The timing information for this schedule call
Kevin DuBois305bef12019-10-09 13:23:27 -0700124 * \return A ScheduleResult::Scheduled if callback was scheduled.
Kevin DuBois305bef12019-10-09 13:23:27 -0700125 * A ScheduleResult::CannotSchedule
Ady Abraham9c53ee72020-07-22 21:16:18 -0700126 * if (workDuration + readyDuration - earliestVsync) is in the past,
127 * or if a callback was dispatched for the predictedVsync already. A ScheduleResult::Error if
128 * there was another error.
Kevin DuBois305bef12019-10-09 13:23:27 -0700129 */
Ady Abraham9c53ee72020-07-22 21:16:18 -0700130 virtual ScheduleResult schedule(CallbackToken token, ScheduleTiming scheduleTiming) = 0;
Kevin DuBois305bef12019-10-09 13:23:27 -0700131
132 /* Cancels a scheduled callback, if possible.
133 *
134 * \param [in] token The callback to cancel.
135 * \return A CancelResult::TooLate if the callback was already dispatched.
136 * A CancelResult::Cancelled if the callback was successfully cancelled.
137 * A CancelResult::Error if there was an pre-condition violation.
138 */
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800139 virtual CancelResult cancel(CallbackToken token) = 0;
Kevin DuBois305bef12019-10-09 13:23:27 -0700140
Ady Abraham5e7371c2020-03-24 14:47:24 -0700141 virtual void dump(std::string& result) const = 0;
142
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800143protected:
144 VSyncDispatch() = default;
Kevin DuBois305bef12019-10-09 13:23:27 -0700145 VSyncDispatch(VSyncDispatch const&) = delete;
146 VSyncDispatch& operator=(VSyncDispatch const&) = delete;
Kevin DuBois305bef12019-10-09 13:23:27 -0700147};
148
149/*
150 * Helper class to operate on registered callbacks. It is up to user of the class to ensure
151 * that VsyncDispatch lifetime exceeds the lifetime of VSyncCallbackRegistation.
152 */
153class VSyncCallbackRegistration {
154public:
Kevin DuBois2968afc2020-01-14 09:48:50 -0800155 VSyncCallbackRegistration(VSyncDispatch&, VSyncDispatch::Callback const& callbackFn,
Kevin DuBois305bef12019-10-09 13:23:27 -0700156 std::string const& callbackName);
157 VSyncCallbackRegistration(VSyncCallbackRegistration&&);
158 VSyncCallbackRegistration& operator=(VSyncCallbackRegistration&&);
159 ~VSyncCallbackRegistration();
160
161 // See documentation for VSyncDispatch::schedule.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700162 ScheduleResult schedule(VSyncDispatch::ScheduleTiming scheduleTiming);
Kevin DuBois305bef12019-10-09 13:23:27 -0700163
164 // See documentation for VSyncDispatch::cancel.
165 CancelResult cancel();
166
167private:
168 VSyncCallbackRegistration(VSyncCallbackRegistration const&) = delete;
169 VSyncCallbackRegistration& operator=(VSyncCallbackRegistration const&) = delete;
170
171 std::reference_wrapper<VSyncDispatch> mDispatch;
172 VSyncDispatch::CallbackToken mToken;
173 bool mValidToken;
174};
175
176} // namespace android::scheduler