blob: c22deaba720d517d8174e5ece919b94af58838fc [file] [log] [blame]
Kevin DuBoiscc27b502019-11-13 09:40:07 -08001/*
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 Laskowski4e0d20d2021-12-06 11:31:02 -080017#include <thread>
Kevin DuBoiscc27b502019-11-13 09:40:07 -080018
19#include <gmock/gmock.h>
20#include <gtest/gtest.h>
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080021
22#include <scheduler/Timer.h>
23
24#include "Scheduler/VSyncDispatchTimerQueue.h"
25#include "Scheduler/VSyncTracker.h"
Kevin DuBoiscc27b502019-11-13 09:40:07 -080026
27using namespace testing;
28using namespace std::literals;
29
30namespace android::scheduler {
31
32template <typename Rep, typename Per>
33constexpr nsecs_t toNs(std::chrono::duration<Rep, Per> const& tp) {
34 return std::chrono::duration_cast<std::chrono::nanoseconds>(tp).count();
35}
36
Ady Abrahame9883032023-11-20 17:54:54 -080037class StubTracker : public VSyncTracker {
Kevin DuBoiscc27b502019-11-13 09:40:07 -080038public:
Ady Abrahame9883032023-11-20 17:54:54 -080039 StubTracker(nsecs_t period) : mPeriod(period) {}
Kevin DuBoiscc27b502019-11-13 09:40:07 -080040
Kevin DuBois02d5ed92020-01-27 11:05:46 -080041 bool addVsyncTimestamp(nsecs_t) final { return true; }
Kevin DuBoiscc27b502019-11-13 09:40:07 -080042
Ady Abrahame9883032023-11-20 17:54:54 -080043 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 Abraham20024aa2024-03-05 01:32:49 +000051 bool isVSyncInPhase(nsecs_t, Fps) final { return false; }
Ady Abrahame9883032023-11-20 17:54:54 -080052 void setDisplayModePtr(ftl::NonNull<DisplayModePtr>) final {}
53 void setRenderRate(Fps) final {}
54 void onFrameBegin(TimePoint, TimePoint) final {}
55 void onFrameMissed(TimePoint) final {}
56 void dump(std::string&) const final {}
57
58protected:
59 std::mutex mutable mMutex;
60 nsecs_t mPeriod;
61};
62
63class FixedRateIdealStubTracker : public StubTracker {
64public:
65 FixedRateIdealStubTracker() : StubTracker{toNs(3ms)} {}
66
Ady Abraham20024aa2024-03-05 01:32:49 +000067 nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t timePoint, std::optional<nsecs_t>) final {
Kevin DuBoiscc27b502019-11-13 09:40:07 -080068 auto const floor = timePoint % mPeriod;
69 if (floor == 0) {
70 return timePoint;
71 }
72 return timePoint - floor + mPeriod;
73 }
Kevin DuBoiscc27b502019-11-13 09:40:07 -080074};
75
Ady Abrahame9883032023-11-20 17:54:54 -080076class VRRStubTracker : public StubTracker {
Kevin DuBoiscc27b502019-11-13 09:40:07 -080077public:
Ady Abrahame9883032023-11-20 17:54:54 -080078 VRRStubTracker(nsecs_t period) : StubTracker(period) {}
Kevin DuBoiscc27b502019-11-13 09:40:07 -080079
Ady Abraham20024aa2024-03-05 01:32:49 +000080 nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t time_point, std::optional<nsecs_t>) final {
Ady Abraham8cb21882020-08-26 18:22:05 -070081 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -080082 auto const normalized_to_base = time_point - mBase;
83 auto const floor = (normalized_to_base) % mPeriod;
84 if (floor == 0) {
85 return time_point;
86 }
87 return normalized_to_base - floor + mPeriod + mBase;
88 }
89
90 void set_interval(nsecs_t interval, nsecs_t last_known) {
Ady Abraham8cb21882020-08-26 18:22:05 -070091 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -080092 mPeriod = interval;
93 mBase = last_known;
94 }
95
Kevin DuBoiscc27b502019-11-13 09:40:07 -080096private:
Kevin DuBoiscc27b502019-11-13 09:40:07 -080097 nsecs_t mBase = 0;
98};
99
100struct VSyncDispatchRealtimeTest : testing::Test {
101 static nsecs_t constexpr mDispatchGroupThreshold = toNs(100us);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800102 static nsecs_t constexpr mVsyncMoveThreshold = toNs(500us);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800103 static size_t constexpr mIterations = 20;
104};
105
106class RepeatingCallbackReceiver {
107public:
Leon Scroggins III67388622023-02-06 20:36:20 -0500108 RepeatingCallbackReceiver(std::shared_ptr<VSyncDispatch> dispatch, nsecs_t workload,
109 nsecs_t readyDuration)
Ady Abraham9c53ee72020-07-22 21:16:18 -0700110 : mWorkload(workload),
111 mReadyDuration(readyDuration),
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800112 mCallback(
Ady Abraham9c53ee72020-07-22 21:16:18 -0700113 dispatch, [&](auto time, auto, auto) { callback_called(time); }, "repeat0") {}
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800114
115 void repeatedly_schedule(size_t iterations, std::function<void(nsecs_t)> const& onEachFrame) {
116 mCallbackTimes.reserve(iterations);
Ady Abraham9c53ee72020-07-22 21:16:18 -0700117 mCallback.schedule(
118 {.workDuration = mWorkload,
119 .readyDuration = mReadyDuration,
Ady Abraham4335afd2023-12-18 19:10:47 -0800120 .lastVsync = systemTime(SYSTEM_TIME_MONOTONIC) + mWorkload + mReadyDuration});
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800121
122 for (auto i = 0u; i < iterations - 1; i++) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700123 std::unique_lock lock(mMutex);
124 mCv.wait(lock, [&] { return mCalled; });
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800125 mCalled = false;
126 auto last = mLastTarget;
Ady Abraham8cb21882020-08-26 18:22:05 -0700127 lock.unlock();
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800128
129 onEachFrame(last);
130
Ady Abraham9c53ee72020-07-22 21:16:18 -0700131 mCallback.schedule({.workDuration = mWorkload,
132 .readyDuration = mReadyDuration,
Ady Abraham4335afd2023-12-18 19:10:47 -0800133 .lastVsync = last + mWorkload + mReadyDuration});
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800134 }
135
136 // wait for the last callback.
Ady Abraham8cb21882020-08-26 18:22:05 -0700137 std::unique_lock lock(mMutex);
138 mCv.wait(lock, [&] { return mCalled; });
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800139 }
140
141 void with_callback_times(std::function<void(std::vector<nsecs_t> const&)> const& fn) const {
142 fn(mCallbackTimes);
143 }
144
145private:
146 void callback_called(nsecs_t time) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700147 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800148 mCallbackTimes.push_back(time);
149 mCalled = true;
150 mLastTarget = time;
151 mCv.notify_all();
152 }
153
154 nsecs_t const mWorkload;
Ady Abraham9c53ee72020-07-22 21:16:18 -0700155 nsecs_t const mReadyDuration;
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800156 VSyncCallbackRegistration mCallback;
157
158 std::mutex mMutex;
159 std::condition_variable mCv;
160 bool mCalled = false;
161 nsecs_t mLastTarget = 0;
162 std::vector<nsecs_t> mCallbackTimes;
163};
164
165TEST_F(VSyncDispatchRealtimeTest, triple_alarm) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500166 auto tracker = std::make_shared<FixedRateIdealStubTracker>();
167 auto dispatch =
168 std::make_shared<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), tracker,
169 mDispatchGroupThreshold, mVsyncMoveThreshold);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800170
171 static size_t constexpr num_clients = 3;
172 std::array<RepeatingCallbackReceiver, num_clients>
Ady Abraham9c53ee72020-07-22 21:16:18 -0700173 cb_receiver{RepeatingCallbackReceiver(dispatch, toNs(1500us), toNs(2500us)),
174 RepeatingCallbackReceiver(dispatch, toNs(0h), toNs(0h)),
175 RepeatingCallbackReceiver(dispatch, toNs(1ms), toNs(3ms))};
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800176
177 auto const on_each_frame = [](nsecs_t) {};
178 std::array<std::thread, num_clients> threads{
179 std::thread([&] { cb_receiver[0].repeatedly_schedule(mIterations, on_each_frame); }),
180 std::thread([&] { cb_receiver[1].repeatedly_schedule(mIterations, on_each_frame); }),
181 std::thread([&] { cb_receiver[2].repeatedly_schedule(mIterations, on_each_frame); }),
182 };
183
184 for (auto it = threads.rbegin(); it != threads.rend(); it++) {
185 it->join();
186 }
187
188 for (auto const& cbs : cb_receiver) {
189 cbs.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
190 }
191}
192
193// starts at 333hz, slides down to 43hz
194TEST_F(VSyncDispatchRealtimeTest, vascillating_vrr) {
195 auto next_vsync_interval = toNs(3ms);
Leon Scroggins III67388622023-02-06 20:36:20 -0500196 auto tracker = std::make_shared<VRRStubTracker>(next_vsync_interval);
197 auto dispatch =
198 std::make_shared<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), tracker,
199 mDispatchGroupThreshold, mVsyncMoveThreshold);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800200
Ady Abraham9c53ee72020-07-22 21:16:18 -0700201 RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms), toNs(5ms));
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800202
203 auto const on_each_frame = [&](nsecs_t last_known) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500204 tracker->set_interval(next_vsync_interval += toNs(1ms), last_known);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800205 };
206
207 std::thread eventThread([&] { cb_receiver.repeatedly_schedule(mIterations, on_each_frame); });
208 eventThread.join();
209
210 cb_receiver.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
211}
212
213// starts at 333hz, jumps to 200hz at frame 10
214TEST_F(VSyncDispatchRealtimeTest, fixed_jump) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500215 auto tracker = std::make_shared<VRRStubTracker>(toNs(3ms));
216 auto dispatch =
217 std::make_shared<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), tracker,
218 mDispatchGroupThreshold, mVsyncMoveThreshold);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800219
Ady Abraham9c53ee72020-07-22 21:16:18 -0700220 RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms), toNs(5ms));
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800221
222 auto jump_frame_counter = 0u;
223 auto constexpr jump_frame_at = 10u;
224 auto const on_each_frame = [&](nsecs_t last_known) {
225 if (jump_frame_counter++ == jump_frame_at) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500226 tracker->set_interval(toNs(5ms), last_known);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800227 }
228 };
229 std::thread eventThread([&] { cb_receiver.repeatedly_schedule(mIterations, on_each_frame); });
230 eventThread.join();
231
232 cb_receiver.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
233}
234} // namespace android::scheduler