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