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