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 | |
| 29 | enum class ScheduleResult { Scheduled, ReScheduled, CannotSchedule, Error }; |
| 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 | /* |
| 42 | * Registers a callback that will be called at designated points on the vsync timeline. |
| 43 | * The callback can be scheduled, rescheduled targeting vsync times, or cancelled. |
| 44 | * The token returned must be cleaned up via unregisterCallback. |
| 45 | * |
| 46 | * \param [in] callbackFn A function to schedule for callback. The resources needed to invoke |
| 47 | * callbackFn must have lifetimes encompassing the lifetime of the |
| 48 | * CallbackToken returned. |
| 49 | * \param [in] callbackName A human-readable, unique name to identify the callback. |
| 50 | * \return A token that can be used to schedule, reschedule, or cancel the |
| 51 | * invocation of callbackFn. |
| 52 | * |
| 53 | */ |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 54 | virtual CallbackToken registerCallback(std::function<void(nsecs_t)> const& callbackFn, |
| 55 | std::string callbackName) = 0; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | * Unregisters a callback. |
| 59 | * |
| 60 | * \param [in] token The callback to unregister. |
| 61 | * |
| 62 | */ |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 63 | virtual void unregisterCallback(CallbackToken token) = 0; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * Schedules the registered callback to be dispatched. |
| 67 | * |
| 68 | * The callback will be dispatched at 'workDuration' nanoseconds before a vsync event. |
| 69 | * |
| 70 | * The caller designates the earliest vsync event that should be targeted by the earliestVsync |
| 71 | * parameter. |
| 72 | * The callback will be scheduled at (workDuration - predictedVsync), where predictedVsync |
| 73 | * is the first vsync event time where ( predictedVsync >= earliestVsync ). |
| 74 | * |
| 75 | * If (workDuration - earliestVsync) is in the past, or if a callback has already been |
| 76 | * dispatched for the predictedVsync, an error will be returned. |
| 77 | * |
| 78 | * It is valid to reschedule a callback to a different time. |
| 79 | * |
| 80 | * \param [in] token The callback to schedule. |
| 81 | * \param [in] workDuration The time before the actual vsync time to invoke the callback |
| 82 | * associated with token. |
| 83 | * \param [in] earliestVsync The targeted display time. This will be snapped to the closest |
| 84 | * predicted vsync time after earliestVsync. |
| 85 | * \return A ScheduleResult::Scheduled if callback was scheduled. |
| 86 | * A ScheduleResult::ReScheduled if callback was rescheduled. |
| 87 | * A ScheduleResult::CannotSchedule |
| 88 | * if (workDuration - earliestVsync) is in the past, or |
| 89 | * if a callback was dispatched for the predictedVsync already. |
| 90 | * A ScheduleResult::Error if there was another error. |
| 91 | */ |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 92 | virtual ScheduleResult schedule(CallbackToken token, nsecs_t workDuration, |
| 93 | nsecs_t earliestVsync) = 0; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 94 | |
| 95 | /* Cancels a scheduled callback, if possible. |
| 96 | * |
| 97 | * \param [in] token The callback to cancel. |
| 98 | * \return A CancelResult::TooLate if the callback was already dispatched. |
| 99 | * A CancelResult::Cancelled if the callback was successfully cancelled. |
| 100 | * A CancelResult::Error if there was an pre-condition violation. |
| 101 | */ |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 102 | virtual CancelResult cancel(CallbackToken token) = 0; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 103 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 104 | protected: |
| 105 | VSyncDispatch() = default; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 106 | VSyncDispatch(VSyncDispatch const&) = delete; |
| 107 | VSyncDispatch& operator=(VSyncDispatch const&) = delete; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | /* |
| 111 | * Helper class to operate on registered callbacks. It is up to user of the class to ensure |
| 112 | * that VsyncDispatch lifetime exceeds the lifetime of VSyncCallbackRegistation. |
| 113 | */ |
| 114 | class VSyncCallbackRegistration { |
| 115 | public: |
| 116 | VSyncCallbackRegistration(VSyncDispatch&, std::function<void(nsecs_t)> const& callbackFn, |
| 117 | std::string const& callbackName); |
| 118 | VSyncCallbackRegistration(VSyncCallbackRegistration&&); |
| 119 | VSyncCallbackRegistration& operator=(VSyncCallbackRegistration&&); |
| 120 | ~VSyncCallbackRegistration(); |
| 121 | |
| 122 | // See documentation for VSyncDispatch::schedule. |
| 123 | ScheduleResult schedule(nsecs_t workDuration, nsecs_t earliestVsync); |
| 124 | |
| 125 | // See documentation for VSyncDispatch::cancel. |
| 126 | CancelResult cancel(); |
| 127 | |
| 128 | private: |
| 129 | VSyncCallbackRegistration(VSyncCallbackRegistration const&) = delete; |
| 130 | VSyncCallbackRegistration& operator=(VSyncCallbackRegistration const&) = delete; |
| 131 | |
| 132 | std::reference_wrapper<VSyncDispatch> mDispatch; |
| 133 | VSyncDispatch::CallbackToken mToken; |
| 134 | bool mValidToken; |
| 135 | }; |
| 136 | |
| 137 | } // namespace android::scheduler |