blob: 448954fbf4ddc3d1183fa948e96f62421674e32b [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
17#include "Scheduler/TimeKeeper.h"
18#include "Scheduler/Timer.h"
19#include "Scheduler/VSyncDispatchTimerQueue.h"
20#include "Scheduler/VSyncTracker.h"
21
22#include <gmock/gmock.h>
23#include <gtest/gtest.h>
24#include <thread>
25
26using namespace testing;
27using namespace std::literals;
28
29namespace android::scheduler {
30
31template <typename Rep, typename Per>
32constexpr nsecs_t toNs(std::chrono::duration<Rep, Per> const& tp) {
33 return std::chrono::duration_cast<std::chrono::nanoseconds>(tp).count();
34}
35
36class FixedRateIdealStubTracker : public VSyncTracker {
37public:
38 FixedRateIdealStubTracker() : mPeriod{toNs(3ms)} {}
39
40 void addVsyncTimestamp(nsecs_t) final {}
41
42 nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t timePoint) const final {
43 auto const floor = timePoint % mPeriod;
44 if (floor == 0) {
45 return timePoint;
46 }
47 return timePoint - floor + mPeriod;
48 }
49
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080050 nsecs_t currentPeriod() const final { return mPeriod; }
51
Kevin DuBoiscc27b502019-11-13 09:40:07 -080052private:
53 nsecs_t const mPeriod;
54};
55
56class VRRStubTracker : public VSyncTracker {
57public:
58 VRRStubTracker(nsecs_t period) : mPeriod{period} {}
59
60 void addVsyncTimestamp(nsecs_t) final {}
61
62 nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t time_point) const final {
63 std::lock_guard<decltype(mMutex)> lk(mMutex);
64 auto const normalized_to_base = time_point - mBase;
65 auto const floor = (normalized_to_base) % mPeriod;
66 if (floor == 0) {
67 return time_point;
68 }
69 return normalized_to_base - floor + mPeriod + mBase;
70 }
71
72 void set_interval(nsecs_t interval, nsecs_t last_known) {
73 std::lock_guard<decltype(mMutex)> lk(mMutex);
74 mPeriod = interval;
75 mBase = last_known;
76 }
77
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080078 nsecs_t currentPeriod() const final {
79 std::lock_guard<decltype(mMutex)> lk(mMutex);
80 return mPeriod;
81 }
82
Kevin DuBoiscc27b502019-11-13 09:40:07 -080083private:
84 std::mutex mutable mMutex;
85 nsecs_t mPeriod;
86 nsecs_t mBase = 0;
87};
88
89struct VSyncDispatchRealtimeTest : testing::Test {
90 static nsecs_t constexpr mDispatchGroupThreshold = toNs(100us);
91 static size_t constexpr mIterations = 20;
92};
93
94class RepeatingCallbackReceiver {
95public:
96 RepeatingCallbackReceiver(VSyncDispatch& dispatch, nsecs_t wl)
97 : mWorkload(wl),
98 mCallback(
99 dispatch, [&](auto time) { callback_called(time); }, "repeat0") {}
100
101 void repeatedly_schedule(size_t iterations, std::function<void(nsecs_t)> const& onEachFrame) {
102 mCallbackTimes.reserve(iterations);
103 mCallback.schedule(mWorkload, systemTime(SYSTEM_TIME_MONOTONIC) + mWorkload);
104
105 for (auto i = 0u; i < iterations - 1; i++) {
106 std::unique_lock<decltype(mMutex)> lk(mMutex);
107 mCv.wait(lk, [&] { return mCalled; });
108 mCalled = false;
109 auto last = mLastTarget;
110 lk.unlock();
111
112 onEachFrame(last);
113
114 mCallback.schedule(mWorkload, last + mWorkload);
115 }
116
117 // wait for the last callback.
118 std::unique_lock<decltype(mMutex)> lk(mMutex);
119 mCv.wait(lk, [&] { return mCalled; });
120 }
121
122 void with_callback_times(std::function<void(std::vector<nsecs_t> const&)> const& fn) const {
123 fn(mCallbackTimes);
124 }
125
126private:
127 void callback_called(nsecs_t time) {
128 std::lock_guard<decltype(mMutex)> lk(mMutex);
129 mCallbackTimes.push_back(time);
130 mCalled = true;
131 mLastTarget = time;
132 mCv.notify_all();
133 }
134
135 nsecs_t const mWorkload;
136 VSyncCallbackRegistration mCallback;
137
138 std::mutex mMutex;
139 std::condition_variable mCv;
140 bool mCalled = false;
141 nsecs_t mLastTarget = 0;
142 std::vector<nsecs_t> mCallbackTimes;
143};
144
145TEST_F(VSyncDispatchRealtimeTest, triple_alarm) {
146 FixedRateIdealStubTracker tracker;
147 VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold);
148
149 static size_t constexpr num_clients = 3;
150 std::array<RepeatingCallbackReceiver, num_clients>
151 cb_receiver{RepeatingCallbackReceiver(dispatch, toNs(1500us)),
152 RepeatingCallbackReceiver(dispatch, toNs(0h)),
153 RepeatingCallbackReceiver(dispatch, toNs(1ms))};
154
155 auto const on_each_frame = [](nsecs_t) {};
156 std::array<std::thread, num_clients> threads{
157 std::thread([&] { cb_receiver[0].repeatedly_schedule(mIterations, on_each_frame); }),
158 std::thread([&] { cb_receiver[1].repeatedly_schedule(mIterations, on_each_frame); }),
159 std::thread([&] { cb_receiver[2].repeatedly_schedule(mIterations, on_each_frame); }),
160 };
161
162 for (auto it = threads.rbegin(); it != threads.rend(); it++) {
163 it->join();
164 }
165
166 for (auto const& cbs : cb_receiver) {
167 cbs.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
168 }
169}
170
171// starts at 333hz, slides down to 43hz
172TEST_F(VSyncDispatchRealtimeTest, vascillating_vrr) {
173 auto next_vsync_interval = toNs(3ms);
174 VRRStubTracker tracker(next_vsync_interval);
175 VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold);
176
177 RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms));
178
179 auto const on_each_frame = [&](nsecs_t last_known) {
180 tracker.set_interval(next_vsync_interval += toNs(1ms), last_known);
181 };
182
183 std::thread eventThread([&] { cb_receiver.repeatedly_schedule(mIterations, on_each_frame); });
184 eventThread.join();
185
186 cb_receiver.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
187}
188
189// starts at 333hz, jumps to 200hz at frame 10
190TEST_F(VSyncDispatchRealtimeTest, fixed_jump) {
191 VRRStubTracker tracker(toNs(3ms));
192 VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold);
193
194 RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms));
195
196 auto jump_frame_counter = 0u;
197 auto constexpr jump_frame_at = 10u;
198 auto const on_each_frame = [&](nsecs_t last_known) {
199 if (jump_frame_counter++ == jump_frame_at) {
200 tracker.set_interval(toNs(5ms), last_known);
201 }
202 };
203 std::thread eventThread([&] { cb_receiver.repeatedly_schedule(mIterations, on_each_frame); });
204 eventThread.join();
205
206 cb_receiver.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
207}
208} // namespace android::scheduler