blob: b63f29990ed60c3f151dc098a287eb844c0f3b77 [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
ramindani7b32b3a2024-07-02 10:17:47 -070022#include <scheduler/FrameTime.h>
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080023#include <scheduler/Timer.h>
24
25#include "Scheduler/VSyncDispatchTimerQueue.h"
26#include "Scheduler/VSyncTracker.h"
Kevin DuBoiscc27b502019-11-13 09:40:07 -080027
28using namespace testing;
29using namespace std::literals;
30
31namespace android::scheduler {
32
33template <typename Rep, typename Per>
34constexpr nsecs_t toNs(std::chrono::duration<Rep, Per> const& tp) {
35 return std::chrono::duration_cast<std::chrono::nanoseconds>(tp).count();
36}
37
Ady Abrahame9883032023-11-20 17:54:54 -080038class StubTracker : public VSyncTracker {
Kevin DuBoiscc27b502019-11-13 09:40:07 -080039public:
Ady Abrahame9883032023-11-20 17:54:54 -080040 StubTracker(nsecs_t period) : mPeriod(period) {}
Kevin DuBoiscc27b502019-11-13 09:40:07 -080041
Kevin DuBois02d5ed92020-01-27 11:05:46 -080042 bool addVsyncTimestamp(nsecs_t) final { return true; }
Kevin DuBoiscc27b502019-11-13 09:40:07 -080043
Ady Abrahame9883032023-11-20 17:54:54 -080044 nsecs_t currentPeriod() const final {
45 std::lock_guard lock(mMutex);
46 return mPeriod;
47 }
48
49 Period minFramePeriod() const final { return Period::fromNs(currentPeriod()); }
50 void resetModel() final {}
51 bool needsMoreSamples() const final { return false; }
Ady Abraham20024aa2024-03-05 01:32:49 +000052 bool isVSyncInPhase(nsecs_t, Fps) final { return false; }
Ady Abrahame9883032023-11-20 17:54:54 -080053 void setDisplayModePtr(ftl::NonNull<DisplayModePtr>) final {}
Ady Abrahamee6365b2024-03-06 14:31:45 -080054 void setRenderRate(Fps, bool) final {}
ramindani7b32b3a2024-07-02 10:17:47 -070055 void onFrameBegin(TimePoint, scheduler::FrameTime) final {}
Ady Abrahame9883032023-11-20 17:54:54 -080056 void onFrameMissed(TimePoint) final {}
57 void dump(std::string&) const final {}
ramindanid17261e2024-03-27 17:50:25 -070058 bool isCurrentMode(const ftl::NonNull<DisplayModePtr>&) const final { return false; };
Ady Abrahame9883032023-11-20 17:54:54 -080059
60protected:
61 std::mutex mutable mMutex;
62 nsecs_t mPeriod;
63};
64
65class FixedRateIdealStubTracker : public StubTracker {
66public:
67 FixedRateIdealStubTracker() : StubTracker{toNs(3ms)} {}
68
Ady Abraham20024aa2024-03-05 01:32:49 +000069 nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t timePoint, std::optional<nsecs_t>) final {
Kevin DuBoiscc27b502019-11-13 09:40:07 -080070 auto const floor = timePoint % mPeriod;
71 if (floor == 0) {
72 return timePoint;
73 }
74 return timePoint - floor + mPeriod;
75 }
Kevin DuBoiscc27b502019-11-13 09:40:07 -080076};
77
Ady Abrahame9883032023-11-20 17:54:54 -080078class VRRStubTracker : public StubTracker {
Kevin DuBoiscc27b502019-11-13 09:40:07 -080079public:
Ady Abrahame9883032023-11-20 17:54:54 -080080 VRRStubTracker(nsecs_t period) : StubTracker(period) {}
Kevin DuBoiscc27b502019-11-13 09:40:07 -080081
Ady Abraham20024aa2024-03-05 01:32:49 +000082 nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t time_point, std::optional<nsecs_t>) final {
Ady Abraham8cb21882020-08-26 18:22:05 -070083 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -080084 auto const normalized_to_base = time_point - mBase;
85 auto const floor = (normalized_to_base) % mPeriod;
86 if (floor == 0) {
87 return time_point;
88 }
89 return normalized_to_base - floor + mPeriod + mBase;
90 }
91
92 void set_interval(nsecs_t interval, nsecs_t last_known) {
Ady Abraham8cb21882020-08-26 18:22:05 -070093 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -080094 mPeriod = interval;
95 mBase = last_known;
96 }
97
Kevin DuBoiscc27b502019-11-13 09:40:07 -080098private:
Kevin DuBoiscc27b502019-11-13 09:40:07 -080099 nsecs_t mBase = 0;
100};
101
102struct VSyncDispatchRealtimeTest : testing::Test {
103 static nsecs_t constexpr mDispatchGroupThreshold = toNs(100us);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800104 static nsecs_t constexpr mVsyncMoveThreshold = toNs(500us);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800105 static size_t constexpr mIterations = 20;
106};
107
108class RepeatingCallbackReceiver {
109public:
Leon Scroggins III67388622023-02-06 20:36:20 -0500110 RepeatingCallbackReceiver(std::shared_ptr<VSyncDispatch> dispatch, nsecs_t workload,
111 nsecs_t readyDuration)
Ady Abraham9c53ee72020-07-22 21:16:18 -0700112 : mWorkload(workload),
113 mReadyDuration(readyDuration),
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800114 mCallback(
Ady Abraham9c53ee72020-07-22 21:16:18 -0700115 dispatch, [&](auto time, auto, auto) { callback_called(time); }, "repeat0") {}
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800116
117 void repeatedly_schedule(size_t iterations, std::function<void(nsecs_t)> const& onEachFrame) {
118 mCallbackTimes.reserve(iterations);
Ady Abraham9c53ee72020-07-22 21:16:18 -0700119 mCallback.schedule(
120 {.workDuration = mWorkload,
121 .readyDuration = mReadyDuration,
Ady Abraham4335afd2023-12-18 19:10:47 -0800122 .lastVsync = systemTime(SYSTEM_TIME_MONOTONIC) + mWorkload + mReadyDuration});
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800123
124 for (auto i = 0u; i < iterations - 1; i++) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700125 std::unique_lock lock(mMutex);
126 mCv.wait(lock, [&] { return mCalled; });
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800127 mCalled = false;
128 auto last = mLastTarget;
Ady Abraham8cb21882020-08-26 18:22:05 -0700129 lock.unlock();
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800130
131 onEachFrame(last);
132
Ady Abraham9c53ee72020-07-22 21:16:18 -0700133 mCallback.schedule({.workDuration = mWorkload,
134 .readyDuration = mReadyDuration,
Ady Abraham4335afd2023-12-18 19:10:47 -0800135 .lastVsync = last + mWorkload + mReadyDuration});
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800136 }
137
138 // wait for the last callback.
Ady Abraham8cb21882020-08-26 18:22:05 -0700139 std::unique_lock lock(mMutex);
140 mCv.wait(lock, [&] { return mCalled; });
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800141 }
142
143 void with_callback_times(std::function<void(std::vector<nsecs_t> const&)> const& fn) const {
144 fn(mCallbackTimes);
145 }
146
147private:
148 void callback_called(nsecs_t time) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700149 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800150 mCallbackTimes.push_back(time);
151 mCalled = true;
152 mLastTarget = time;
153 mCv.notify_all();
154 }
155
156 nsecs_t const mWorkload;
Ady Abraham9c53ee72020-07-22 21:16:18 -0700157 nsecs_t const mReadyDuration;
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800158 VSyncCallbackRegistration mCallback;
159
160 std::mutex mMutex;
161 std::condition_variable mCv;
162 bool mCalled = false;
163 nsecs_t mLastTarget = 0;
164 std::vector<nsecs_t> mCallbackTimes;
165};
166
167TEST_F(VSyncDispatchRealtimeTest, triple_alarm) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500168 auto tracker = std::make_shared<FixedRateIdealStubTracker>();
169 auto dispatch =
170 std::make_shared<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), tracker,
171 mDispatchGroupThreshold, mVsyncMoveThreshold);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800172
173 static size_t constexpr num_clients = 3;
174 std::array<RepeatingCallbackReceiver, num_clients>
Ady Abraham9c53ee72020-07-22 21:16:18 -0700175 cb_receiver{RepeatingCallbackReceiver(dispatch, toNs(1500us), toNs(2500us)),
176 RepeatingCallbackReceiver(dispatch, toNs(0h), toNs(0h)),
177 RepeatingCallbackReceiver(dispatch, toNs(1ms), toNs(3ms))};
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800178
179 auto const on_each_frame = [](nsecs_t) {};
180 std::array<std::thread, num_clients> threads{
181 std::thread([&] { cb_receiver[0].repeatedly_schedule(mIterations, on_each_frame); }),
182 std::thread([&] { cb_receiver[1].repeatedly_schedule(mIterations, on_each_frame); }),
183 std::thread([&] { cb_receiver[2].repeatedly_schedule(mIterations, on_each_frame); }),
184 };
185
186 for (auto it = threads.rbegin(); it != threads.rend(); it++) {
187 it->join();
188 }
189
190 for (auto const& cbs : cb_receiver) {
191 cbs.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
192 }
193}
194
195// starts at 333hz, slides down to 43hz
196TEST_F(VSyncDispatchRealtimeTest, vascillating_vrr) {
197 auto next_vsync_interval = toNs(3ms);
Leon Scroggins III67388622023-02-06 20:36:20 -0500198 auto tracker = std::make_shared<VRRStubTracker>(next_vsync_interval);
199 auto dispatch =
200 std::make_shared<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), tracker,
201 mDispatchGroupThreshold, mVsyncMoveThreshold);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800202
Ady Abraham9c53ee72020-07-22 21:16:18 -0700203 RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms), toNs(5ms));
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800204
205 auto const on_each_frame = [&](nsecs_t last_known) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500206 tracker->set_interval(next_vsync_interval += toNs(1ms), last_known);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800207 };
208
209 std::thread eventThread([&] { cb_receiver.repeatedly_schedule(mIterations, on_each_frame); });
210 eventThread.join();
211
212 cb_receiver.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
213}
214
215// starts at 333hz, jumps to 200hz at frame 10
216TEST_F(VSyncDispatchRealtimeTest, fixed_jump) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500217 auto tracker = std::make_shared<VRRStubTracker>(toNs(3ms));
218 auto dispatch =
219 std::make_shared<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), tracker,
220 mDispatchGroupThreshold, mVsyncMoveThreshold);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800221
Ady Abraham9c53ee72020-07-22 21:16:18 -0700222 RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms), toNs(5ms));
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800223
224 auto jump_frame_counter = 0u;
225 auto constexpr jump_frame_at = 10u;
226 auto const on_each_frame = [&](nsecs_t last_known) {
227 if (jump_frame_counter++ == jump_frame_at) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500228 tracker->set_interval(toNs(5ms), last_known);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800229 }
230 };
231 std::thread eventThread([&] { cb_receiver.repeatedly_schedule(mIterations, on_each_frame); });
232 eventThread.join();
233
234 cb_receiver.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
235}
236} // namespace android::scheduler