blob: ca2f92a375c0a98f3367b758e9f1e0b6af7094c8 [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
37class FixedRateIdealStubTracker : public VSyncTracker {
38public:
39 FixedRateIdealStubTracker() : mPeriod{toNs(3ms)} {}
40
Kevin DuBois02d5ed92020-01-27 11:05:46 -080041 bool addVsyncTimestamp(nsecs_t) final { return true; }
Kevin DuBoiscc27b502019-11-13 09:40:07 -080042
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 DuBois2fd3cea2019-11-14 08:52:45 -080051 nsecs_t currentPeriod() const final { return mPeriod; }
Ady Abraham3db8a3c2023-11-20 17:53:47 -080052 Period minFramePeriod() const final { return Period::fromNs(currentPeriod()); }
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080053
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080054 void resetModel() final {}
Kevin DuBoisb818bfa2020-07-10 14:29:36 -070055 bool needsMoreSamples() const final { return false; }
Ady Abraham5cc2e262021-03-25 13:09:17 -070056 bool isVSyncInPhase(nsecs_t, Fps) const final { return false; }
Ady Abrahamc585dba2023-11-15 18:41:35 -080057 void setDisplayModePtr(ftl::NonNull<DisplayModePtr>) final {}
58 void setRenderRate(Fps) final {}
Ady Abraham5e7371c2020-03-24 14:47:24 -070059 void dump(std::string&) const final {}
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -080060
Kevin DuBoiscc27b502019-11-13 09:40:07 -080061private:
62 nsecs_t const mPeriod;
63};
64
65class VRRStubTracker : public VSyncTracker {
66public:
67 VRRStubTracker(nsecs_t period) : mPeriod{period} {}
68
Kevin DuBois02d5ed92020-01-27 11:05:46 -080069 bool addVsyncTimestamp(nsecs_t) final { return true; }
Kevin DuBoiscc27b502019-11-13 09:40:07 -080070
71 nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t time_point) const final {
Ady Abraham8cb21882020-08-26 18:22:05 -070072 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -080073 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 Abraham8cb21882020-08-26 18:22:05 -070082 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -080083 mPeriod = interval;
84 mBase = last_known;
85 }
86
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080087 nsecs_t currentPeriod() const final {
Ady Abraham8cb21882020-08-26 18:22:05 -070088 std::lock_guard lock(mMutex);
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080089 return mPeriod;
90 }
91
Ady Abraham3db8a3c2023-11-20 17:53:47 -080092 Period minFramePeriod() const final { return Period::fromNs(currentPeriod()); }
93
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080094 void resetModel() final {}
Kevin DuBoisb818bfa2020-07-10 14:29:36 -070095 bool needsMoreSamples() const final { return false; }
Ady Abraham5cc2e262021-03-25 13:09:17 -070096 bool isVSyncInPhase(nsecs_t, Fps) const final { return false; }
Ady Abrahamc585dba2023-11-15 18:41:35 -080097 void setDisplayModePtr(ftl::NonNull<DisplayModePtr>) final {}
98 void setRenderRate(Fps) final {}
Ady Abraham5e7371c2020-03-24 14:47:24 -070099 void dump(std::string&) const final {}
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800100
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800101private:
102 std::mutex mutable mMutex;
103 nsecs_t mPeriod;
104 nsecs_t mBase = 0;
105};
106
107struct VSyncDispatchRealtimeTest : testing::Test {
108 static nsecs_t constexpr mDispatchGroupThreshold = toNs(100us);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800109 static nsecs_t constexpr mVsyncMoveThreshold = toNs(500us);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800110 static size_t constexpr mIterations = 20;
111};
112
113class RepeatingCallbackReceiver {
114public:
Leon Scroggins III67388622023-02-06 20:36:20 -0500115 RepeatingCallbackReceiver(std::shared_ptr<VSyncDispatch> dispatch, nsecs_t workload,
116 nsecs_t readyDuration)
Ady Abraham9c53ee72020-07-22 21:16:18 -0700117 : mWorkload(workload),
118 mReadyDuration(readyDuration),
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800119 mCallback(
Ady Abraham9c53ee72020-07-22 21:16:18 -0700120 dispatch, [&](auto time, auto, auto) { callback_called(time); }, "repeat0") {}
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800121
122 void repeatedly_schedule(size_t iterations, std::function<void(nsecs_t)> const& onEachFrame) {
123 mCallbackTimes.reserve(iterations);
Ady Abraham9c53ee72020-07-22 21:16:18 -0700124 mCallback.schedule(
125 {.workDuration = mWorkload,
126 .readyDuration = mReadyDuration,
127 .earliestVsync = systemTime(SYSTEM_TIME_MONOTONIC) + mWorkload + mReadyDuration});
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800128
129 for (auto i = 0u; i < iterations - 1; i++) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700130 std::unique_lock lock(mMutex);
131 mCv.wait(lock, [&] { return mCalled; });
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800132 mCalled = false;
133 auto last = mLastTarget;
Ady Abraham8cb21882020-08-26 18:22:05 -0700134 lock.unlock();
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800135
136 onEachFrame(last);
137
Ady Abraham9c53ee72020-07-22 21:16:18 -0700138 mCallback.schedule({.workDuration = mWorkload,
139 .readyDuration = mReadyDuration,
140 .earliestVsync = last + mWorkload + mReadyDuration});
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800141 }
142
143 // wait for the last callback.
Ady Abraham8cb21882020-08-26 18:22:05 -0700144 std::unique_lock lock(mMutex);
145 mCv.wait(lock, [&] { return mCalled; });
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800146 }
147
148 void with_callback_times(std::function<void(std::vector<nsecs_t> const&)> const& fn) const {
149 fn(mCallbackTimes);
150 }
151
152private:
153 void callback_called(nsecs_t time) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700154 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800155 mCallbackTimes.push_back(time);
156 mCalled = true;
157 mLastTarget = time;
158 mCv.notify_all();
159 }
160
161 nsecs_t const mWorkload;
Ady Abraham9c53ee72020-07-22 21:16:18 -0700162 nsecs_t const mReadyDuration;
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800163 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
172TEST_F(VSyncDispatchRealtimeTest, triple_alarm) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500173 auto tracker = std::make_shared<FixedRateIdealStubTracker>();
174 auto dispatch =
175 std::make_shared<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), tracker,
176 mDispatchGroupThreshold, mVsyncMoveThreshold);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800177
178 static size_t constexpr num_clients = 3;
179 std::array<RepeatingCallbackReceiver, num_clients>
Ady Abraham9c53ee72020-07-22 21:16:18 -0700180 cb_receiver{RepeatingCallbackReceiver(dispatch, toNs(1500us), toNs(2500us)),
181 RepeatingCallbackReceiver(dispatch, toNs(0h), toNs(0h)),
182 RepeatingCallbackReceiver(dispatch, toNs(1ms), toNs(3ms))};
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800183
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
201TEST_F(VSyncDispatchRealtimeTest, vascillating_vrr) {
202 auto next_vsync_interval = toNs(3ms);
Leon Scroggins III67388622023-02-06 20:36:20 -0500203 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 DuBoiscc27b502019-11-13 09:40:07 -0800207
Ady Abraham9c53ee72020-07-22 21:16:18 -0700208 RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms), toNs(5ms));
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800209
210 auto const on_each_frame = [&](nsecs_t last_known) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500211 tracker->set_interval(next_vsync_interval += toNs(1ms), last_known);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800212 };
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
221TEST_F(VSyncDispatchRealtimeTest, fixed_jump) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500222 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 DuBoiscc27b502019-11-13 09:40:07 -0800226
Ady Abraham9c53ee72020-07-22 21:16:18 -0700227 RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms), toNs(5ms));
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800228
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 III67388622023-02-06 20:36:20 -0500233 tracker->set_interval(toNs(5ms), last_known);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800234 }
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