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 <functional> |
| 20 | #include <memory> |
| 21 | #include <mutex> |
| 22 | #include <string> |
| 23 | #include <string_view> |
| 24 | #include <unordered_map> |
| 25 | |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 26 | #include <android-base/thread_annotations.h> |
| 27 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 28 | #include "VSyncDispatch.h" |
| 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 | class VSyncTracker; |
| 34 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 35 | // VSyncDispatchTimerQueueEntry is a helper class representing internal state for each entry in |
| 36 | // VSyncDispatchTimerQueue hoisted to public for unit testing. |
| 37 | class VSyncDispatchTimerQueueEntry { |
| 38 | public: |
| 39 | // This is the state of the entry. There are 3 states, armed, running, disarmed. |
| 40 | // Valid transition: disarmed -> armed ( when scheduled ) |
| 41 | // Valid transition: armed -> running -> disarmed ( when timer is called) |
| 42 | // Valid transition: armed -> disarmed ( when cancelled ) |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 43 | VSyncDispatchTimerQueueEntry(std::string name, VSyncDispatch::Callback, |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 44 | nsecs_t minVsyncDistance); |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 45 | std::string_view name() const; |
| 46 | |
| 47 | // Start: functions that are not threadsafe. |
| 48 | // Return the last vsync time this callback was invoked. |
| 49 | std::optional<nsecs_t> lastExecutedVsyncTarget() const; |
| 50 | |
| 51 | // This moves the state from disarmed->armed and will calculate the wakeupTime. |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 52 | ScheduleResult schedule(VSyncDispatch::ScheduleTiming, VSyncTracker&, nsecs_t now); |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 53 | // 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] | 54 | void update(VSyncTracker&, nsecs_t now); |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 55 | |
| 56 | // This will return empty if not armed, or the next calculated wakeup time if armed. |
| 57 | // It will not update the wakeupTime. |
| 58 | std::optional<nsecs_t> wakeupTime() const; |
| 59 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 60 | std::optional<nsecs_t> readyTime() const; |
| 61 | |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 62 | std::optional<nsecs_t> targetVsync() const; |
| 63 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 64 | // This moves state from armed->disarmed. |
| 65 | void disarm(); |
| 66 | |
| 67 | // This moves the state from armed->running. |
| 68 | // Store the timestamp that this was intended for as the last called timestamp. |
| 69 | nsecs_t executing(); |
Kevin DuBois | 5c18c1c | 2020-05-27 15:50:50 -0700 | [diff] [blame] | 70 | |
| 71 | // Adds a pending upload of the earliestVSync and workDuration that will be applied on the next |
| 72 | // call to update() |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 73 | void addPendingWorkloadUpdate(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: |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 87 | const std::string mName; |
| 88 | const VSyncDispatch::Callback mCallback; |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 89 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 90 | VSyncDispatch::ScheduleTiming mScheduleTiming; |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 91 | const nsecs_t mMinVsyncDistance; |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 92 | |
| 93 | struct ArmingInfo { |
| 94 | nsecs_t mActualWakeupTime; |
| 95 | nsecs_t mActualVsyncTime; |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 96 | nsecs_t mActualReadyTime; |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 97 | }; |
| 98 | std::optional<ArmingInfo> mArmedInfo; |
| 99 | std::optional<nsecs_t> mLastDispatchTime; |
| 100 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 101 | std::optional<VSyncDispatch::ScheduleTiming> mWorkloadUpdateInfo; |
Kevin DuBois | 5c18c1c | 2020-05-27 15:50:50 -0700 | [diff] [blame] | 102 | |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 103 | mutable std::mutex mRunningMutex; |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 104 | std::condition_variable mCv; |
| 105 | bool mRunning GUARDED_BY(mRunningMutex) = false; |
| 106 | }; |
| 107 | |
| 108 | /* |
| 109 | * VSyncDispatchTimerQueue is a class that will dispatch callbacks as per VSyncDispatch interface |
| 110 | * using a single timer queue. |
| 111 | */ |
| 112 | class VSyncDispatchTimerQueue : public VSyncDispatch { |
| 113 | public: |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 114 | // Constructs a VSyncDispatchTimerQueue. |
| 115 | // \param[in] tk A timekeeper. |
| 116 | // \param[in] tracker A tracker. |
| 117 | // \param[in] timerSlack The threshold at which different similarly timed callbacks |
| 118 | // should be grouped into one wakeup. |
| 119 | // \param[in] minVsyncDistance The minimum distance between two vsync estimates before the |
| 120 | // vsyncs are considered the same vsync event. |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 121 | VSyncDispatchTimerQueue(std::unique_ptr<TimeKeeper>, VSyncTracker&, nsecs_t timerSlack, |
| 122 | nsecs_t minVsyncDistance); |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 123 | ~VSyncDispatchTimerQueue(); |
| 124 | |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 125 | CallbackToken registerCallback(Callback, std::string callbackName) final; |
| 126 | void unregisterCallback(CallbackToken) final; |
| 127 | ScheduleResult schedule(CallbackToken, ScheduleTiming) final; |
| 128 | CancelResult cancel(CallbackToken) final; |
| 129 | void dump(std::string&) const final; |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 130 | |
| 131 | private: |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 132 | VSyncDispatchTimerQueue(const VSyncDispatchTimerQueue&) = delete; |
| 133 | VSyncDispatchTimerQueue& operator=(const VSyncDispatchTimerQueue&) = delete; |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 134 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 135 | using CallbackMap = |
| 136 | std::unordered_map<CallbackToken, std::shared_ptr<VSyncDispatchTimerQueueEntry>>; |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 137 | |
| 138 | void timerCallback(); |
| 139 | void setTimer(nsecs_t, nsecs_t) REQUIRES(mMutex); |
| 140 | void rearmTimer(nsecs_t now) REQUIRES(mMutex); |
| 141 | void rearmTimerSkippingUpdateFor(nsecs_t now, CallbackMap::iterator const& skipUpdate) |
| 142 | REQUIRES(mMutex); |
| 143 | void cancelTimer() REQUIRES(mMutex); |
| 144 | |
| 145 | static constexpr nsecs_t kInvalidTime = std::numeric_limits<int64_t>::max(); |
| 146 | std::unique_ptr<TimeKeeper> const mTimeKeeper; |
| 147 | VSyncTracker& mTracker; |
| 148 | nsecs_t const mTimerSlack; |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 149 | nsecs_t const mMinVsyncDistance; |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 150 | |
| 151 | std::mutex mutable mMutex; |
| 152 | size_t mCallbackToken GUARDED_BY(mMutex) = 0; |
| 153 | |
| 154 | CallbackMap mCallbacks GUARDED_BY(mMutex); |
| 155 | nsecs_t mIntendedWakeupTime GUARDED_BY(mMutex) = kInvalidTime; |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 156 | |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 157 | // For debugging purposes |
| 158 | nsecs_t mLastTimerCallback GUARDED_BY(mMutex) = kInvalidTime; |
| 159 | nsecs_t mLastTimerSchedule GUARDED_BY(mMutex) = kInvalidTime; |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | } // namespace android::scheduler |