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 <utils/Timers.h> |
| 20 | #include <functional> |
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 | |
| 23 | #include "StrongTyping.h" |
| 24 | |
| 25 | namespace android::scheduler { |
| 26 | class TimeKeeper; |
| 27 | class VSyncTracker; |
| 28 | |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 29 | enum class ScheduleResult { Scheduled, CannotSchedule, Error }; |
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: |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 37 | using CallbackToken = StrongTyping<size_t, class CallbackTokenTag, Compare, Hash>; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 38 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 39 | virtual ~VSyncDispatch(); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 40 | |
| 41 | /* |
Kevin DuBois | 2968afc | 2020-01-14 09:48:50 -0800 | [diff] [blame] | 42 | * 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^] | 43 | * \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 DuBois | 2968afc | 2020-01-14 09:48:50 -0800 | [diff] [blame] | 47 | */ |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame^] | 48 | using Callback = |
| 49 | std::function<void(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime)>; |
Kevin DuBois | 2968afc | 2020-01-14 09:48:50 -0800 | [diff] [blame] | 50 | |
| 51 | /* |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 52 | * 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 DuBois | 2968afc | 2020-01-14 09:48:50 -0800 | [diff] [blame] | 64 | virtual CallbackToken registerCallback(Callback const& callbackFn, |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 65 | std::string callbackName) = 0; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | * Unregisters a callback. |
| 69 | * |
| 70 | * \param [in] token The callback to unregister. |
| 71 | * |
| 72 | */ |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 73 | virtual void unregisterCallback(CallbackToken token) = 0; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 74 | |
| 75 | /* |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame^] | 76 | * 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; |
| 97 | }; |
| 98 | |
| 99 | /* |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 100 | * Schedules the registered callback to be dispatched. |
| 101 | * |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame^] | 102 | * The callback will be dispatched at 'workDuration + readyDuration' nanoseconds before a vsync |
| 103 | * event. |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 104 | * |
| 105 | * The caller designates the earliest vsync event that should be targeted by the earliestVsync |
| 106 | * parameter. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame^] | 107 | * The callback will be scheduled at (workDuration + readyDuration - predictedVsync), where |
| 108 | * predictedVsync is the first vsync event time where ( predictedVsync >= earliestVsync ). |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 109 | * |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame^] | 110 | * If (workDuration + readyDuration - earliestVsync) is in the past, or if a callback has |
| 111 | * already been dispatched for the predictedVsync, an error will be returned. |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 112 | * |
| 113 | * It is valid to reschedule a callback to a different time. |
| 114 | * |
| 115 | * \param [in] token The callback to schedule. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame^] | 116 | * \param [in] scheduleTiming The timing information for this schedule call |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 117 | * \return A ScheduleResult::Scheduled if callback was scheduled. |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 118 | * A ScheduleResult::CannotSchedule |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame^] | 119 | * if (workDuration + readyDuration - earliestVsync) is in the past, |
| 120 | * or if a callback was dispatched for the predictedVsync already. A ScheduleResult::Error if |
| 121 | * there was another error. |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 122 | */ |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame^] | 123 | virtual ScheduleResult schedule(CallbackToken token, ScheduleTiming scheduleTiming) = 0; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 124 | |
| 125 | /* Cancels a scheduled callback, if possible. |
| 126 | * |
| 127 | * \param [in] token The callback to cancel. |
| 128 | * \return A CancelResult::TooLate if the callback was already dispatched. |
| 129 | * A CancelResult::Cancelled if the callback was successfully cancelled. |
| 130 | * A CancelResult::Error if there was an pre-condition violation. |
| 131 | */ |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 132 | virtual CancelResult cancel(CallbackToken token) = 0; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 133 | |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 134 | virtual void dump(std::string& result) const = 0; |
| 135 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 136 | protected: |
| 137 | VSyncDispatch() = default; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 138 | VSyncDispatch(VSyncDispatch const&) = delete; |
| 139 | VSyncDispatch& operator=(VSyncDispatch const&) = delete; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | /* |
| 143 | * Helper class to operate on registered callbacks. It is up to user of the class to ensure |
| 144 | * that VsyncDispatch lifetime exceeds the lifetime of VSyncCallbackRegistation. |
| 145 | */ |
| 146 | class VSyncCallbackRegistration { |
| 147 | public: |
Kevin DuBois | 2968afc | 2020-01-14 09:48:50 -0800 | [diff] [blame] | 148 | VSyncCallbackRegistration(VSyncDispatch&, VSyncDispatch::Callback const& callbackFn, |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 149 | std::string const& callbackName); |
| 150 | VSyncCallbackRegistration(VSyncCallbackRegistration&&); |
| 151 | VSyncCallbackRegistration& operator=(VSyncCallbackRegistration&&); |
| 152 | ~VSyncCallbackRegistration(); |
| 153 | |
| 154 | // See documentation for VSyncDispatch::schedule. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame^] | 155 | ScheduleResult schedule(VSyncDispatch::ScheduleTiming scheduleTiming); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 156 | |
| 157 | // See documentation for VSyncDispatch::cancel. |
| 158 | CancelResult cancel(); |
| 159 | |
| 160 | private: |
| 161 | VSyncCallbackRegistration(VSyncCallbackRegistration const&) = delete; |
| 162 | VSyncCallbackRegistration& operator=(VSyncCallbackRegistration const&) = delete; |
| 163 | |
| 164 | std::reference_wrapper<VSyncDispatch> mDispatch; |
| 165 | VSyncDispatch::CallbackToken mToken; |
| 166 | bool mValidToken; |
| 167 | }; |
| 168 | |
| 169 | } // namespace android::scheduler |