blob: 3b095545e90559b768c776512644989249b04633 [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 {}
Ady Abrahamee6365b2024-03-06 14:31:45 -080053 void setRenderRate(Fps, bool) final {}
Ady Abrahame9883032023-11-20 17:54:54 -080054 void onFrameBegin(TimePoint, TimePoint) final {}
55 void onFrameMissed(TimePoint) final {}
56 void dump(std::string&) const final {}
ramindanid17261e2024-03-27 17:50:25 -070057 bool isCurrentMode(const ftl::NonNull<DisplayModePtr>&) const final { return false; };
Ady Abrahame9883032023-11-20 17:54:54 -080058
59protected:
60 std::mutex mutable mMutex;
61 nsecs_t mPeriod;
62};
63
64class FixedRateIdealStubTracker : public StubTracker {
65public:
66 FixedRateIdealStubTracker() : StubTracker{toNs(3ms)} {}
67
Ady Abraham20024aa2024-03-05 01:32:49 +000068 nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t timePoint, std::optional<nsecs_t>) final {
Kevin DuBoiscc27b502019-11-13 09:40:07 -080069 auto const floor = timePoint % mPeriod;
70 if (floor == 0) {
71 return timePoint;
72 }
73 return timePoint - floor + mPeriod;
74 }
Kevin DuBoiscc27b502019-11-13 09:40:07 -080075};
76
Ady Abrahame9883032023-11-20 17:54:54 -080077class VRRStubTracker : public StubTracker {
Kevin DuBoiscc27b502019-11-13 09:40:07 -080078public:
Ady Abrahame9883032023-11-20 17:54:54 -080079 VRRStubTracker(nsecs_t period) : StubTracker(period) {}
Kevin DuBoiscc27b502019-11-13 09:40:07 -080080
Ady Abraham20024aa2024-03-05 01:32:49 +000081 nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t time_point, std::optional<nsecs_t>) final {
Ady Abraham8cb21882020-08-26 18:22:05 -070082 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -080083 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 Abraham8cb21882020-08-26 18:22:05 -070092 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -080093 mPeriod = interval;
94 mBase = last_known;
95 }
96
Kevin DuBoiscc27b502019-11-13 09:40:07 -080097private:
Kevin DuBoiscc27b502019-11-13 09:40:07 -080098 nsecs_t mBase = 0;
99};
100
101struct VSyncDispatchRealtimeTest : testing::Test {
102 static nsecs_t constexpr mDispatchGroupThreshold = toNs(100us);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800103 static nsecs_t constexpr mVsyncMoveThreshold = toNs(500us);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800104 static size_t constexpr mIterations = 20;
105};
106
107class RepeatingCallbackReceiver {
108public:
Leon Scroggins III67388622023-02-06 20:36:20 -0500109 RepeatingCallbackReceiver(std::shared_ptr<VSyncDispatch> dispatch, nsecs_t workload,
110 nsecs_t readyDuration)
Ady Abraham9c53ee72020-07-22 21:16:18 -0700111 : mWorkload(workload),
112 mReadyDuration(readyDuration),
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800113 mCallback(
Ady Abraham9c53ee72020-07-22 21:16:18 -0700114 dispatch, [&](auto time, auto, auto) { callback_called(time); }, "repeat0") {}
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800115
116 void repeatedly_schedule(size_t iterations, std::function<void(nsecs_t)> const& onEachFrame) {
117 mCallbackTimes.reserve(iterations);
Ady Abraham9c53ee72020-07-22 21:16:18 -0700118 mCallback.schedule(
119 {.workDuration = mWorkload,
120 .readyDuration = mReadyDuration,
Ady Abraham4335afd2023-12-18 19:10:47 -0800121 .lastVsync = systemTime(SYSTEM_TIME_MONOTONIC) + mWorkload + mReadyDuration});
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800122
123 for (auto i = 0u; i < iterations - 1; i++) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700124 std::unique_lock lock(mMutex);
125 mCv.wait(lock, [&] { return mCalled; });
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800126 mCalled = false;
127 auto last = mLastTarget;
Ady Abraham8cb21882020-08-26 18:22:05 -0700128 lock.unlock();
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800129
130 onEachFrame(last);
131
Ady Abraham9c53ee72020-07-22 21:16:18 -0700132 mCallback.schedule({.workDuration = mWorkload,
133 .readyDuration = mReadyDuration,
Ady Abraham4335afd2023-12-18 19:10:47 -0800134 .lastVsync = last + mWorkload + mReadyDuration});
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800135 }
136
137 // wait for the last callback.
Ady Abraham8cb21882020-08-26 18:22:05 -0700138 std::unique_lock lock(mMutex);
139 mCv.wait(lock, [&] { return mCalled; });
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800140 }
141
142 void with_callback_times(std::function<void(std::vector<nsecs_t> const&)> const& fn) const {
143 fn(mCallbackTimes);
144 }
145
146private:
147 void callback_called(nsecs_t time) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700148 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800149 mCallbackTimes.push_back(time);
150 mCalled = true;
151 mLastTarget = time;
152 mCv.notify_all();
153 }
154
155 nsecs_t const mWorkload;
Ady Abraham9c53ee72020-07-22 21:16:18 -0700156 nsecs_t const mReadyDuration;
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800157 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
166TEST_F(VSyncDispatchRealtimeTest, triple_alarm) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500167 auto tracker = std::make_shared<FixedRateIdealStubTracker>();
168 auto dispatch =
169 std::make_shared<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), tracker,
170 mDispatchGroupThreshold, mVsyncMoveThreshold);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800171
172 static size_t constexpr num_clients = 3;
173 std::array<RepeatingCallbackReceiver, num_clients>
Ady Abraham9c53ee72020-07-22 21:16:18 -0700174 cb_receiver{RepeatingCallbackReceiver(dispatch, toNs(1500us), toNs(2500us)),
175 RepeatingCallbackReceiver(dispatch, toNs(0h), toNs(0h)),
176 RepeatingCallbackReceiver(dispatch, toNs(1ms), toNs(3ms))};
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800177
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
195TEST_F(VSyncDispatchRealtimeTest, vascillating_vrr) {
196 auto next_vsync_interval = toNs(3ms);
Leon Scroggins III67388622023-02-06 20:36:20 -0500197 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 DuBoiscc27b502019-11-13 09:40:07 -0800201
Ady Abraham9c53ee72020-07-22 21:16:18 -0700202 RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms), toNs(5ms));
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800203
204 auto const on_each_frame = [&](nsecs_t last_known) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500205 tracker->set_interval(next_vsync_interval += toNs(1ms), last_known);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800206 };
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
215TEST_F(VSyncDispatchRealtimeTest, fixed_jump) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500216 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 DuBoiscc27b502019-11-13 09:40:07 -0800220
Ady Abraham9c53ee72020-07-22 21:16:18 -0700221 RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms), toNs(5ms));
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800222
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 III67388622023-02-06 20:36:20 -0500227 tracker->set_interval(toNs(5ms), last_known);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800228 }
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