Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -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 | |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 17 | #include <thread> |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 18 | |
| 19 | #include <gmock/gmock.h> |
| 20 | #include <gtest/gtest.h> |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 21 | |
| 22 | #include <scheduler/Timer.h> |
| 23 | |
| 24 | #include "Scheduler/VSyncDispatchTimerQueue.h" |
| 25 | #include "Scheduler/VSyncTracker.h" |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 26 | |
| 27 | using namespace testing; |
| 28 | using namespace std::literals; |
| 29 | |
| 30 | namespace android::scheduler { |
| 31 | |
| 32 | template <typename Rep, typename Per> |
| 33 | constexpr nsecs_t toNs(std::chrono::duration<Rep, Per> const& tp) { |
| 34 | return std::chrono::duration_cast<std::chrono::nanoseconds>(tp).count(); |
| 35 | } |
| 36 | |
| 37 | class FixedRateIdealStubTracker : public VSyncTracker { |
| 38 | public: |
| 39 | FixedRateIdealStubTracker() : mPeriod{toNs(3ms)} {} |
| 40 | |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 41 | bool addVsyncTimestamp(nsecs_t) final { return true; } |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 42 | |
| 43 | nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t timePoint) const final { |
| 44 | auto const floor = timePoint % mPeriod; |
| 45 | if (floor == 0) { |
| 46 | return timePoint; |
| 47 | } |
| 48 | return timePoint - floor + mPeriod; |
| 49 | } |
| 50 | |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 51 | nsecs_t currentPeriod() const final { return mPeriod; } |
Ady Abraham | 3db8a3c | 2023-11-20 17:53:47 -0800 | [diff] [blame] | 52 | Period minFramePeriod() const final { return Period::fromNs(currentPeriod()); } |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 53 | |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 54 | void resetModel() final {} |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 55 | bool needsMoreSamples() const final { return false; } |
Ady Abraham | 5cc2e26 | 2021-03-25 13:09:17 -0700 | [diff] [blame] | 56 | bool isVSyncInPhase(nsecs_t, Fps) const final { return false; } |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 57 | void setDisplayModePtr(ftl::NonNull<DisplayModePtr>) final {} |
| 58 | void setRenderRate(Fps) final {} |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 59 | void dump(std::string&) const final {} |
Kevin DuBois | ee2ad9f | 2019-11-21 11:10:57 -0800 | [diff] [blame] | 60 | |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 61 | private: |
| 62 | nsecs_t const mPeriod; |
| 63 | }; |
| 64 | |
| 65 | class VRRStubTracker : public VSyncTracker { |
| 66 | public: |
| 67 | VRRStubTracker(nsecs_t period) : mPeriod{period} {} |
| 68 | |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 69 | bool addVsyncTimestamp(nsecs_t) final { return true; } |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 70 | |
| 71 | nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t time_point) const final { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 72 | std::lock_guard lock(mMutex); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 73 | auto const normalized_to_base = time_point - mBase; |
| 74 | auto const floor = (normalized_to_base) % mPeriod; |
| 75 | if (floor == 0) { |
| 76 | return time_point; |
| 77 | } |
| 78 | return normalized_to_base - floor + mPeriod + mBase; |
| 79 | } |
| 80 | |
| 81 | void set_interval(nsecs_t interval, nsecs_t last_known) { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 82 | std::lock_guard lock(mMutex); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 83 | mPeriod = interval; |
| 84 | mBase = last_known; |
| 85 | } |
| 86 | |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 87 | nsecs_t currentPeriod() const final { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 88 | std::lock_guard lock(mMutex); |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 89 | return mPeriod; |
| 90 | } |
| 91 | |
Ady Abraham | 3db8a3c | 2023-11-20 17:53:47 -0800 | [diff] [blame] | 92 | Period minFramePeriod() const final { return Period::fromNs(currentPeriod()); } |
| 93 | |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 94 | void resetModel() final {} |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 95 | bool needsMoreSamples() const final { return false; } |
Ady Abraham | 5cc2e26 | 2021-03-25 13:09:17 -0700 | [diff] [blame] | 96 | bool isVSyncInPhase(nsecs_t, Fps) const final { return false; } |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 97 | void setDisplayModePtr(ftl::NonNull<DisplayModePtr>) final {} |
| 98 | void setRenderRate(Fps) final {} |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 99 | void dump(std::string&) const final {} |
Kevin DuBois | ee2ad9f | 2019-11-21 11:10:57 -0800 | [diff] [blame] | 100 | |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 101 | private: |
| 102 | std::mutex mutable mMutex; |
| 103 | nsecs_t mPeriod; |
| 104 | nsecs_t mBase = 0; |
| 105 | }; |
| 106 | |
| 107 | struct VSyncDispatchRealtimeTest : testing::Test { |
| 108 | static nsecs_t constexpr mDispatchGroupThreshold = toNs(100us); |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 109 | static nsecs_t constexpr mVsyncMoveThreshold = toNs(500us); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 110 | static size_t constexpr mIterations = 20; |
| 111 | }; |
| 112 | |
| 113 | class RepeatingCallbackReceiver { |
| 114 | public: |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 115 | RepeatingCallbackReceiver(std::shared_ptr<VSyncDispatch> dispatch, nsecs_t workload, |
| 116 | nsecs_t readyDuration) |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 117 | : mWorkload(workload), |
| 118 | mReadyDuration(readyDuration), |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 119 | mCallback( |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 120 | dispatch, [&](auto time, auto, auto) { callback_called(time); }, "repeat0") {} |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 121 | |
| 122 | void repeatedly_schedule(size_t iterations, std::function<void(nsecs_t)> const& onEachFrame) { |
| 123 | mCallbackTimes.reserve(iterations); |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 124 | mCallback.schedule( |
| 125 | {.workDuration = mWorkload, |
| 126 | .readyDuration = mReadyDuration, |
| 127 | .earliestVsync = systemTime(SYSTEM_TIME_MONOTONIC) + mWorkload + mReadyDuration}); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 128 | |
| 129 | for (auto i = 0u; i < iterations - 1; i++) { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 130 | std::unique_lock lock(mMutex); |
| 131 | mCv.wait(lock, [&] { return mCalled; }); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 132 | mCalled = false; |
| 133 | auto last = mLastTarget; |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 134 | lock.unlock(); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 135 | |
| 136 | onEachFrame(last); |
| 137 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 138 | mCallback.schedule({.workDuration = mWorkload, |
| 139 | .readyDuration = mReadyDuration, |
| 140 | .earliestVsync = last + mWorkload + mReadyDuration}); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // wait for the last callback. |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 144 | std::unique_lock lock(mMutex); |
| 145 | mCv.wait(lock, [&] { return mCalled; }); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void with_callback_times(std::function<void(std::vector<nsecs_t> const&)> const& fn) const { |
| 149 | fn(mCallbackTimes); |
| 150 | } |
| 151 | |
| 152 | private: |
| 153 | void callback_called(nsecs_t time) { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 154 | std::lock_guard lock(mMutex); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 155 | mCallbackTimes.push_back(time); |
| 156 | mCalled = true; |
| 157 | mLastTarget = time; |
| 158 | mCv.notify_all(); |
| 159 | } |
| 160 | |
| 161 | nsecs_t const mWorkload; |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 162 | nsecs_t const mReadyDuration; |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 163 | VSyncCallbackRegistration mCallback; |
| 164 | |
| 165 | std::mutex mMutex; |
| 166 | std::condition_variable mCv; |
| 167 | bool mCalled = false; |
| 168 | nsecs_t mLastTarget = 0; |
| 169 | std::vector<nsecs_t> mCallbackTimes; |
| 170 | }; |
| 171 | |
| 172 | TEST_F(VSyncDispatchRealtimeTest, triple_alarm) { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 173 | auto tracker = std::make_shared<FixedRateIdealStubTracker>(); |
| 174 | auto dispatch = |
| 175 | std::make_shared<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), tracker, |
| 176 | mDispatchGroupThreshold, mVsyncMoveThreshold); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 177 | |
| 178 | static size_t constexpr num_clients = 3; |
| 179 | std::array<RepeatingCallbackReceiver, num_clients> |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 180 | cb_receiver{RepeatingCallbackReceiver(dispatch, toNs(1500us), toNs(2500us)), |
| 181 | RepeatingCallbackReceiver(dispatch, toNs(0h), toNs(0h)), |
| 182 | RepeatingCallbackReceiver(dispatch, toNs(1ms), toNs(3ms))}; |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 183 | |
| 184 | auto const on_each_frame = [](nsecs_t) {}; |
| 185 | std::array<std::thread, num_clients> threads{ |
| 186 | std::thread([&] { cb_receiver[0].repeatedly_schedule(mIterations, on_each_frame); }), |
| 187 | std::thread([&] { cb_receiver[1].repeatedly_schedule(mIterations, on_each_frame); }), |
| 188 | std::thread([&] { cb_receiver[2].repeatedly_schedule(mIterations, on_each_frame); }), |
| 189 | }; |
| 190 | |
| 191 | for (auto it = threads.rbegin(); it != threads.rend(); it++) { |
| 192 | it->join(); |
| 193 | } |
| 194 | |
| 195 | for (auto const& cbs : cb_receiver) { |
| 196 | cbs.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); }); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // starts at 333hz, slides down to 43hz |
| 201 | TEST_F(VSyncDispatchRealtimeTest, vascillating_vrr) { |
| 202 | auto next_vsync_interval = toNs(3ms); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 203 | auto tracker = std::make_shared<VRRStubTracker>(next_vsync_interval); |
| 204 | auto dispatch = |
| 205 | std::make_shared<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), tracker, |
| 206 | mDispatchGroupThreshold, mVsyncMoveThreshold); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 207 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 208 | RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms), toNs(5ms)); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 209 | |
| 210 | auto const on_each_frame = [&](nsecs_t last_known) { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 211 | tracker->set_interval(next_vsync_interval += toNs(1ms), last_known); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | std::thread eventThread([&] { cb_receiver.repeatedly_schedule(mIterations, on_each_frame); }); |
| 215 | eventThread.join(); |
| 216 | |
| 217 | cb_receiver.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); }); |
| 218 | } |
| 219 | |
| 220 | // starts at 333hz, jumps to 200hz at frame 10 |
| 221 | TEST_F(VSyncDispatchRealtimeTest, fixed_jump) { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 222 | auto tracker = std::make_shared<VRRStubTracker>(toNs(3ms)); |
| 223 | auto dispatch = |
| 224 | std::make_shared<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), tracker, |
| 225 | mDispatchGroupThreshold, mVsyncMoveThreshold); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 226 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 227 | RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms), toNs(5ms)); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 228 | |
| 229 | auto jump_frame_counter = 0u; |
| 230 | auto constexpr jump_frame_at = 10u; |
| 231 | auto const on_each_frame = [&](nsecs_t last_known) { |
| 232 | if (jump_frame_counter++ == jump_frame_at) { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 233 | tracker->set_interval(toNs(5ms), last_known); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 234 | } |
| 235 | }; |
| 236 | std::thread eventThread([&] { cb_receiver.repeatedly_schedule(mIterations, on_each_frame); }); |
| 237 | eventThread.join(); |
| 238 | |
| 239 | cb_receiver.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); }); |
| 240 | } |
| 241 | } // namespace android::scheduler |