Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 1 | /* |
| 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 DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 19 | #include <functional> |
Ady Abraham | b5d3afa | 2021-05-07 11:22:23 -0700 | [diff] [blame] | 20 | #include <optional> |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 21 | #include <string> |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 22 | |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame^] | 23 | #include <ftl/mixins.h> |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 24 | #include <utils/Timers.h> |
| 25 | |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 26 | namespace android::scheduler { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 27 | |
Ady Abraham | b5d3afa | 2021-05-07 11:22:23 -0700 | [diff] [blame] | 28 | using ScheduleResult = std::optional<nsecs_t>; |
| 29 | |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 30 | enum class CancelResult { Cancelled, TooLate, Error }; |
| 31 | |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 32 | /* |
| 33 | * VSyncDispatch is a class that will dispatch callbacks relative to system vsync events. |
| 34 | */ |
| 35 | class VSyncDispatch { |
| 36 | public: |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame^] | 37 | struct CallbackToken : ftl::DefaultConstructible<CallbackToken, size_t>, |
| 38 | ftl::Equatable<CallbackToken>, |
| 39 | ftl::Incrementable<CallbackToken> { |
| 40 | using DefaultConstructible::DefaultConstructible; |
| 41 | }; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 42 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 43 | virtual ~VSyncDispatch(); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 44 | |
| 45 | /* |
Kevin DuBois | 2968afc | 2020-01-14 09:48:50 -0800 | [diff] [blame] | 46 | * A callback that can be registered to be awoken at a given time relative to a vsync event. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 47 | * \param [in] vsyncTime: The timestamp of the vsync the callback is for. |
| 48 | * \param [in] targetWakeupTime: The timestamp of intended wakeup time of the cb. |
| 49 | * \param [in] readyTime: The timestamp of intended time where client needs to finish |
| 50 | * its work by. |
Kevin DuBois | 2968afc | 2020-01-14 09:48:50 -0800 | [diff] [blame] | 51 | */ |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 52 | using Callback = |
| 53 | std::function<void(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime)>; |
Kevin DuBois | 2968afc | 2020-01-14 09:48:50 -0800 | [diff] [blame] | 54 | |
| 55 | /* |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 56 | * Registers a callback that will be called at designated points on the vsync timeline. |
| 57 | * The callback can be scheduled, rescheduled targeting vsync times, or cancelled. |
| 58 | * The token returned must be cleaned up via unregisterCallback. |
| 59 | * |
| 60 | * \param [in] callbackFn A function to schedule for callback. The resources needed to invoke |
| 61 | * callbackFn must have lifetimes encompassing the lifetime of the |
| 62 | * CallbackToken returned. |
| 63 | * \param [in] callbackName A human-readable, unique name to identify the callback. |
| 64 | * \return A token that can be used to schedule, reschedule, or cancel the |
| 65 | * invocation of callbackFn. |
| 66 | * |
| 67 | */ |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 68 | virtual CallbackToken registerCallback(Callback, std::string callbackName) = 0; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | * Unregisters a callback. |
| 72 | * |
| 73 | * \param [in] token The callback to unregister. |
| 74 | * |
| 75 | */ |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 76 | virtual void unregisterCallback(CallbackToken token) = 0; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 77 | |
| 78 | /* |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 79 | * Timing information about a scheduled callback |
| 80 | * |
| 81 | * @workDuration: The time needed for the client to perform its work |
| 82 | * @readyDuration: The time needed for the client to be ready before a vsync event. |
| 83 | * For external (non-SF) clients, not only do we need to account for their |
| 84 | * workDuration, but we also need to account for the time SF will take to |
| 85 | * process their buffer/transaction. In this case, readyDuration will be set |
| 86 | * to the SF duration in order to provide enough end-to-end time, and to be |
| 87 | * able to provide the ready-by time (deadline) on the callback. |
| 88 | * For internal clients, we don't need to add additional padding, so |
| 89 | * readyDuration will typically be 0. |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 90 | * @lastVsync: The targeted display time. This will be snapped to the closest |
| 91 | * predicted vsync time after lastVsync. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 92 | * |
| 93 | * callback will be dispatched at 'workDuration + readyDuration' nanoseconds before a vsync |
| 94 | * event. |
| 95 | */ |
| 96 | struct ScheduleTiming { |
| 97 | nsecs_t workDuration = 0; |
| 98 | nsecs_t readyDuration = 0; |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 99 | nsecs_t lastVsync = 0; |
Ady Abraham | 55fa727 | 2020-09-30 19:19:27 -0700 | [diff] [blame] | 100 | |
| 101 | bool operator==(const ScheduleTiming& other) const { |
| 102 | return workDuration == other.workDuration && readyDuration == other.readyDuration && |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 103 | lastVsync == other.lastVsync; |
Ady Abraham | 55fa727 | 2020-09-30 19:19:27 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | bool operator!=(const ScheduleTiming& other) const { return !(*this == other); } |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | /* |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 110 | * Schedules the registered callback to be dispatched. |
| 111 | * |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 112 | * The callback will be dispatched at 'workDuration + readyDuration' nanoseconds before a vsync |
| 113 | * event. |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 114 | * |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 115 | * The caller designates the earliest vsync event that should be targeted by the lastVsync |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 116 | * parameter. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 117 | * The callback will be scheduled at (workDuration + readyDuration - predictedVsync), where |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 118 | * predictedVsync is the first vsync event time where ( predictedVsync >= lastVsync ). |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 119 | * |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 120 | * If (workDuration + readyDuration - lastVsync) is in the past, or if a callback has |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 121 | * already been dispatched for the predictedVsync, an error will be returned. |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 122 | * |
| 123 | * It is valid to reschedule a callback to a different time. |
| 124 | * |
| 125 | * \param [in] token The callback to schedule. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 126 | * \param [in] scheduleTiming The timing information for this schedule call |
Ady Abraham | b5d3afa | 2021-05-07 11:22:23 -0700 | [diff] [blame] | 127 | * \return The expected callback time if a callback was scheduled. |
| 128 | * std::nullopt if the callback is not registered. |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 129 | */ |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 130 | virtual ScheduleResult schedule(CallbackToken token, ScheduleTiming scheduleTiming) = 0; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 131 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 132 | /* |
| 133 | * Update the timing information for a scheduled callback. |
| 134 | * If the callback is not scheduled, then this function does nothing. |
| 135 | * |
| 136 | * \param [in] token The callback to schedule. |
| 137 | * \param [in] scheduleTiming The timing information for this schedule call |
| 138 | * \return The expected callback time if a callback was scheduled. |
| 139 | * std::nullopt if the callback is not registered. |
| 140 | */ |
| 141 | virtual ScheduleResult update(CallbackToken token, ScheduleTiming scheduleTiming) = 0; |
| 142 | |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 143 | /* Cancels a scheduled callback, if possible. |
| 144 | * |
| 145 | * \param [in] token The callback to cancel. |
| 146 | * \return A CancelResult::TooLate if the callback was already dispatched. |
| 147 | * A CancelResult::Cancelled if the callback was successfully cancelled. |
| 148 | * A CancelResult::Error if there was an pre-condition violation. |
| 149 | */ |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 150 | virtual CancelResult cancel(CallbackToken token) = 0; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 151 | |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 152 | virtual void dump(std::string& result) const = 0; |
| 153 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 154 | protected: |
| 155 | VSyncDispatch() = default; |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 156 | |
| 157 | VSyncDispatch(const VSyncDispatch&) = delete; |
| 158 | VSyncDispatch& operator=(const VSyncDispatch&) = delete; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 161 | class VSyncCallbackRegistration { |
| 162 | public: |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 163 | VSyncCallbackRegistration(std::shared_ptr<VSyncDispatch>, VSyncDispatch::Callback, |
| 164 | std::string callbackName); |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 165 | ~VSyncCallbackRegistration(); |
| 166 | |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 167 | VSyncCallbackRegistration(VSyncCallbackRegistration&&); |
| 168 | VSyncCallbackRegistration& operator=(VSyncCallbackRegistration&&); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 169 | |
| 170 | // See documentation for VSyncDispatch::schedule. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 171 | ScheduleResult schedule(VSyncDispatch::ScheduleTiming scheduleTiming); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 172 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 173 | // See documentation for VSyncDispatch::update. |
| 174 | ScheduleResult update(VSyncDispatch::ScheduleTiming scheduleTiming); |
| 175 | |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 176 | // See documentation for VSyncDispatch::cancel. |
| 177 | CancelResult cancel(); |
| 178 | |
| 179 | private: |
Leon Scroggins III | 6c440ae | 2023-04-21 15:01:03 -0400 | [diff] [blame] | 180 | friend class VSyncCallbackRegistrationTest; |
| 181 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 182 | std::shared_ptr<VSyncDispatch> mDispatch; |
Leon Scroggins III | 6c440ae | 2023-04-21 15:01:03 -0400 | [diff] [blame] | 183 | std::optional<VSyncDispatch::CallbackToken> mToken; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | } // namespace android::scheduler |