Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [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 | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 19 | #include <memory> |
| 20 | #include <mutex> |
| 21 | #include <string> |
| 22 | #include <string_view> |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 23 | |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 24 | #include <android-base/thread_annotations.h> |
Dominik Laskowski | 0f3e5b9 | 2023-11-17 18:03:41 -0500 | [diff] [blame] | 25 | #include <ftl/small_map.h> |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 26 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 27 | #include "VSyncDispatch.h" |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 28 | #include "VsyncSchedule.h" |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 29 | |
| 30 | namespace android::scheduler { |
| 31 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 32 | class TimeKeeper; |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 33 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 34 | // VSyncDispatchTimerQueueEntry is a helper class representing internal state for each entry in |
| 35 | // VSyncDispatchTimerQueue hoisted to public for unit testing. |
| 36 | class VSyncDispatchTimerQueueEntry { |
| 37 | public: |
| 38 | // This is the state of the entry. There are 3 states, armed, running, disarmed. |
| 39 | // Valid transition: disarmed -> armed ( when scheduled ) |
| 40 | // Valid transition: armed -> running -> disarmed ( when timer is called) |
| 41 | // Valid transition: armed -> disarmed ( when cancelled ) |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 42 | VSyncDispatchTimerQueueEntry(std::string name, VSyncDispatch::Callback, |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 43 | nsecs_t minVsyncDistance); |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 44 | std::string_view name() const; |
| 45 | |
| 46 | // Start: functions that are not threadsafe. |
| 47 | // Return the last vsync time this callback was invoked. |
| 48 | std::optional<nsecs_t> lastExecutedVsyncTarget() const; |
| 49 | |
| 50 | // This moves the state from disarmed->armed and will calculate the wakeupTime. |
ramindani | 558f4a9 | 2024-02-16 15:49:23 -0800 | [diff] [blame] | 51 | ScheduleResult schedule(VSyncDispatch::ScheduleTiming, VSyncTracker&, nsecs_t now); |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 52 | // This will update armed entries with the latest vsync information. Entry remains armed. |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 53 | void update(VSyncTracker&, nsecs_t now); |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 54 | |
| 55 | // This will return empty if not armed, or the next calculated wakeup time if armed. |
| 56 | // It will not update the wakeupTime. |
| 57 | std::optional<nsecs_t> wakeupTime() const; |
| 58 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 59 | std::optional<nsecs_t> readyTime() const; |
| 60 | |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 61 | std::optional<nsecs_t> targetVsync() const; |
| 62 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 63 | // This moves state from armed->disarmed. |
| 64 | void disarm(); |
| 65 | |
| 66 | // This moves the state from armed->running. |
| 67 | // Store the timestamp that this was intended for as the last called timestamp. |
| 68 | nsecs_t executing(); |
Kevin DuBois | 5c18c1c | 2020-05-27 15:50:50 -0700 | [diff] [blame] | 69 | |
| 70 | // Adds a pending upload of the earliestVSync and workDuration that will be applied on the next |
| 71 | // call to update() |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 72 | ScheduleResult addPendingWorkloadUpdate(VSyncTracker&, nsecs_t now, |
| 73 | VSyncDispatch::ScheduleTiming); |
Kevin DuBois | 5c18c1c | 2020-05-27 15:50:50 -0700 | [diff] [blame] | 74 | |
| 75 | // Checks if there is a pending update to the workload, returning true if so. |
| 76 | bool hasPendingWorkloadUpdate() const; |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 77 | // End: functions that are not threadsafe. |
| 78 | |
Kevin DuBois | 2968afc | 2020-01-14 09:48:50 -0800 | [diff] [blame] | 79 | // Invoke the callback with the two given timestamps, moving the state from running->disarmed. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 80 | void callback(nsecs_t vsyncTimestamp, nsecs_t wakeupTimestamp, nsecs_t deadlineTimestamp); |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 81 | // Block calling thread while the callback is executing. |
| 82 | void ensureNotRunning(); |
| 83 | |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 84 | void dump(std::string& result) const; |
| 85 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 86 | private: |
Ady Abraham | da8af4c | 2024-02-14 00:24:34 +0000 | [diff] [blame] | 87 | struct ArmingInfo { |
| 88 | nsecs_t mActualWakeupTime; |
| 89 | nsecs_t mActualVsyncTime; |
| 90 | nsecs_t mActualReadyTime; |
| 91 | }; |
| 92 | |
Ady Abraham | 3fcfd8b | 2022-07-12 12:31:00 -0700 | [diff] [blame] | 93 | nsecs_t adjustVsyncIfNeeded(VSyncTracker& tracker, nsecs_t nextVsyncTime) const; |
Ady Abraham | b6c7f88 | 2024-01-29 14:43:29 -0800 | [diff] [blame^] | 94 | ArmingInfo getArmedInfo(VSyncTracker&, nsecs_t now, VSyncDispatch::ScheduleTiming, |
| 95 | std::optional<ArmingInfo>) const; |
Ady Abraham | 3fcfd8b | 2022-07-12 12:31:00 -0700 | [diff] [blame] | 96 | |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 97 | const std::string mName; |
| 98 | const VSyncDispatch::Callback mCallback; |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 99 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 100 | VSyncDispatch::ScheduleTiming mScheduleTiming; |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 101 | const nsecs_t mMinVsyncDistance; |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 102 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 103 | std::optional<ArmingInfo> mArmedInfo; |
| 104 | std::optional<nsecs_t> mLastDispatchTime; |
| 105 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 106 | std::optional<VSyncDispatch::ScheduleTiming> mWorkloadUpdateInfo; |
Kevin DuBois | 5c18c1c | 2020-05-27 15:50:50 -0700 | [diff] [blame] | 107 | |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 108 | mutable std::mutex mRunningMutex; |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 109 | std::condition_variable mCv; |
| 110 | bool mRunning GUARDED_BY(mRunningMutex) = false; |
| 111 | }; |
| 112 | |
| 113 | /* |
| 114 | * VSyncDispatchTimerQueue is a class that will dispatch callbacks as per VSyncDispatch interface |
| 115 | * using a single timer queue. |
| 116 | */ |
| 117 | class VSyncDispatchTimerQueue : public VSyncDispatch { |
| 118 | public: |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 119 | // Constructs a VSyncDispatchTimerQueue. |
| 120 | // \param[in] tk A timekeeper. |
| 121 | // \param[in] tracker A tracker. |
| 122 | // \param[in] timerSlack The threshold at which different similarly timed callbacks |
| 123 | // should be grouped into one wakeup. |
| 124 | // \param[in] minVsyncDistance The minimum distance between two vsync estimates before the |
| 125 | // vsyncs are considered the same vsync event. |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 126 | VSyncDispatchTimerQueue(std::unique_ptr<TimeKeeper>, VsyncSchedule::TrackerPtr, |
| 127 | nsecs_t timerSlack, nsecs_t minVsyncDistance); |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 128 | ~VSyncDispatchTimerQueue(); |
| 129 | |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 130 | CallbackToken registerCallback(Callback, std::string callbackName) final; |
| 131 | void unregisterCallback(CallbackToken) final; |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 132 | std::optional<ScheduleResult> schedule(CallbackToken, ScheduleTiming) final; |
| 133 | std::optional<ScheduleResult> update(CallbackToken, ScheduleTiming) final; |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 134 | CancelResult cancel(CallbackToken) final; |
| 135 | void dump(std::string&) const final; |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 136 | |
| 137 | private: |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 138 | VSyncDispatchTimerQueue(const VSyncDispatchTimerQueue&) = delete; |
| 139 | VSyncDispatchTimerQueue& operator=(const VSyncDispatchTimerQueue&) = delete; |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 140 | |
Dominik Laskowski | 0f3e5b9 | 2023-11-17 18:03:41 -0500 | [diff] [blame] | 141 | // The static capacity was chosen to exceed the expected number of callbacks. |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 142 | using CallbackMap = |
Dominik Laskowski | 0f3e5b9 | 2023-11-17 18:03:41 -0500 | [diff] [blame] | 143 | ftl::SmallMap<CallbackToken, std::shared_ptr<VSyncDispatchTimerQueueEntry>, 5>; |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 144 | |
| 145 | void timerCallback(); |
| 146 | void setTimer(nsecs_t, nsecs_t) REQUIRES(mMutex); |
| 147 | void rearmTimer(nsecs_t now) REQUIRES(mMutex); |
Dominik Laskowski | 0f3e5b9 | 2023-11-17 18:03:41 -0500 | [diff] [blame] | 148 | void rearmTimerSkippingUpdateFor(nsecs_t now, CallbackMap::const_iterator skipUpdate) |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 149 | REQUIRES(mMutex); |
| 150 | void cancelTimer() REQUIRES(mMutex); |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 151 | std::optional<ScheduleResult> scheduleLocked(CallbackToken, ScheduleTiming) REQUIRES(mMutex); |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 152 | |
Ray Chin | 7a1e742 | 2023-07-17 16:39:22 +0800 | [diff] [blame] | 153 | std::mutex mutable mMutex; |
| 154 | |
en.liu | 07e0e29 | 2023-07-05 19:01:52 +0800 | [diff] [blame] | 155 | // During VSyncDispatchTimerQueue deconstruction, skip timerCallback to |
| 156 | // avoid crash |
| 157 | bool mRunning = true; |
| 158 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 159 | static constexpr nsecs_t kInvalidTime = std::numeric_limits<int64_t>::max(); |
| 160 | std::unique_ptr<TimeKeeper> const mTimeKeeper; |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 161 | VsyncSchedule::TrackerPtr mTracker; |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 162 | nsecs_t const mTimerSlack; |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 163 | nsecs_t const mMinVsyncDistance; |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 164 | |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 165 | CallbackToken mCallbackToken GUARDED_BY(mMutex); |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 166 | |
| 167 | CallbackMap mCallbacks GUARDED_BY(mMutex); |
| 168 | nsecs_t mIntendedWakeupTime GUARDED_BY(mMutex) = kInvalidTime; |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 169 | |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 170 | // For debugging purposes |
| 171 | nsecs_t mLastTimerCallback GUARDED_BY(mMutex) = kInvalidTime; |
| 172 | nsecs_t mLastTimerSchedule GUARDED_BY(mMutex) = kInvalidTime; |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | } // namespace android::scheduler |