blob: a1eda949fd90bf650e38db0d8775c4fce26229a6 [file] [log] [blame]
Kevin DuBois305bef12019-10-09 13:23:27 -07001/*
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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
Marin Shalamanovbed7fd32020-12-21 20:02:20 +010020#pragma clang diagnostic ignored "-Wextra"
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080021
Kevin DuBois305bef12019-10-09 13:23:27 -070022#undef LOG_TAG
23#define LOG_TAG "LibSurfaceFlingerUnittests"
24#define LOG_NDEBUG 0
25
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080026#include <thread>
Kevin DuBois305bef12019-10-09 13:23:27 -070027
28#include <gmock/gmock.h>
29#include <gtest/gtest.h>
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080030
31#include <scheduler/TimeKeeper.h>
32
Ady Abraham529bd9f2023-10-05 14:55:30 -070033#include "FlagUtils.h"
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080034#include "Scheduler/VSyncDispatchTimerQueue.h"
35#include "Scheduler/VSyncTracker.h"
Kevin DuBois305bef12019-10-09 13:23:27 -070036
Ady Abraham529bd9f2023-10-05 14:55:30 -070037#include <com_android_graphics_surfaceflinger_flags.h>
38
Kevin DuBois305bef12019-10-09 13:23:27 -070039using namespace testing;
40using namespace std::literals;
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080041
Kevin DuBois305bef12019-10-09 13:23:27 -070042namespace android::scheduler {
Ady Abraham529bd9f2023-10-05 14:55:30 -070043using namespace com::android::graphics::surfaceflinger;
Kevin DuBois305bef12019-10-09 13:23:27 -070044
45class MockVSyncTracker : public VSyncTracker {
46public:
47 MockVSyncTracker(nsecs_t period) : mPeriod{period} {
48 ON_CALL(*this, nextAnticipatedVSyncTimeFrom(_))
49 .WillByDefault(Invoke(this, &MockVSyncTracker::nextVSyncTime));
Kevin DuBois02d5ed92020-01-27 11:05:46 -080050 ON_CALL(*this, addVsyncTimestamp(_)).WillByDefault(Return(true));
Ady Abraham3fcfd8b2022-07-12 12:31:00 -070051 ON_CALL(*this, currentPeriod())
52 .WillByDefault(Invoke(this, &MockVSyncTracker::getCurrentPeriod));
Kevin DuBois305bef12019-10-09 13:23:27 -070053 }
54
Kevin DuBois02d5ed92020-01-27 11:05:46 -080055 MOCK_METHOD1(addVsyncTimestamp, bool(nsecs_t));
Kevin DuBois305bef12019-10-09 13:23:27 -070056 MOCK_CONST_METHOD1(nextAnticipatedVSyncTimeFrom, nsecs_t(nsecs_t));
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080057 MOCK_CONST_METHOD0(currentPeriod, nsecs_t());
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -080058 MOCK_METHOD1(setPeriod, void(nsecs_t));
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080059 MOCK_METHOD0(resetModel, void());
Kevin DuBoisb818bfa2020-07-10 14:29:36 -070060 MOCK_CONST_METHOD0(needsMoreSamples, bool());
Ady Abraham5cc2e262021-03-25 13:09:17 -070061 MOCK_CONST_METHOD2(isVSyncInPhase, bool(nsecs_t, Fps));
Ady Abrahamfdc049c2023-02-17 14:52:05 +000062 MOCK_METHOD(void, setRenderRate, (Fps), (override));
Ady Abraham5e7371c2020-03-24 14:47:24 -070063 MOCK_CONST_METHOD1(dump, void(std::string&));
Kevin DuBois305bef12019-10-09 13:23:27 -070064
65 nsecs_t nextVSyncTime(nsecs_t timePoint) const {
66 if (timePoint % mPeriod == 0) {
67 return timePoint;
68 }
69 return (timePoint - (timePoint % mPeriod) + mPeriod);
70 }
71
Ady Abraham3fcfd8b2022-07-12 12:31:00 -070072 nsecs_t getCurrentPeriod() const { return mPeriod; }
73
Kevin DuBois305bef12019-10-09 13:23:27 -070074protected:
75 nsecs_t const mPeriod;
76};
77
78class ControllableClock : public TimeKeeper {
79public:
80 ControllableClock() {
Ady Abrahamb491c902020-08-15 15:47:56 -070081 ON_CALL(*this, alarmAt(_, _))
82 .WillByDefault(Invoke(this, &ControllableClock::alarmAtDefaultBehavior));
Kevin DuBois305bef12019-10-09 13:23:27 -070083 ON_CALL(*this, now()).WillByDefault(Invoke(this, &ControllableClock::fakeTime));
84 }
85
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080086 MOCK_METHOD(nsecs_t, now, (), (const));
87 MOCK_METHOD(void, alarmAt, (std::function<void()>, nsecs_t), (override));
88 MOCK_METHOD(void, alarmCancel, (), (override));
89 MOCK_METHOD(void, dump, (std::string&), (const, override));
Kevin DuBois305bef12019-10-09 13:23:27 -070090
Ady Abrahamb491c902020-08-15 15:47:56 -070091 void alarmAtDefaultBehavior(std::function<void()> const& callback, nsecs_t time) {
Kevin DuBois305bef12019-10-09 13:23:27 -070092 mCallback = callback;
Ady Abrahamb491c902020-08-15 15:47:56 -070093 mNextCallbackTime = time;
Kevin DuBois305bef12019-10-09 13:23:27 -070094 }
95
96 nsecs_t fakeTime() const { return mCurrentTime; }
97
98 void advanceToNextCallback() {
99 mCurrentTime = mNextCallbackTime;
100 if (mCallback) {
101 mCallback();
102 }
103 }
104
105 void advanceBy(nsecs_t advancement) {
106 mCurrentTime += advancement;
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700107 if (mCurrentTime >= (mNextCallbackTime + mLag) && mCallback) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700108 mCallback();
109 }
110 };
111
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700112 void setLag(nsecs_t lag) { mLag = lag; }
113
Kevin DuBois305bef12019-10-09 13:23:27 -0700114private:
115 std::function<void()> mCallback;
116 nsecs_t mNextCallbackTime = 0;
117 nsecs_t mCurrentTime = 0;
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700118 nsecs_t mLag = 0;
Kevin DuBois305bef12019-10-09 13:23:27 -0700119};
120
121class CountingCallback {
122public:
Leon Scroggins III67388622023-02-06 20:36:20 -0500123 CountingCallback(std::shared_ptr<VSyncDispatch> dispatch)
124 : mDispatch(std::move(dispatch)),
125 mToken(mDispatch->registerCallback(std::bind(&CountingCallback::counter, this,
126 std::placeholders::_1,
127 std::placeholders::_2,
128 std::placeholders::_3),
129 "test")) {}
130 ~CountingCallback() { mDispatch->unregisterCallback(mToken); }
Kevin DuBois305bef12019-10-09 13:23:27 -0700131
132 operator VSyncDispatch::CallbackToken() const { return mToken; }
133
Ady Abraham9c53ee72020-07-22 21:16:18 -0700134 void counter(nsecs_t time, nsecs_t wakeup_time, nsecs_t readyTime) {
Kevin DuBoisf9477832020-07-16 10:21:36 -0700135 mCalls.push_back(time);
136 mWakeupTime.push_back(wakeup_time);
Ady Abraham9c53ee72020-07-22 21:16:18 -0700137 mReadyTime.push_back(readyTime);
Kevin DuBoisf9477832020-07-16 10:21:36 -0700138 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700139
Leon Scroggins III67388622023-02-06 20:36:20 -0500140 std::shared_ptr<VSyncDispatch> mDispatch;
Kevin DuBois305bef12019-10-09 13:23:27 -0700141 VSyncDispatch::CallbackToken mToken;
142 std::vector<nsecs_t> mCalls;
Kevin DuBoisf9477832020-07-16 10:21:36 -0700143 std::vector<nsecs_t> mWakeupTime;
Ady Abraham9c53ee72020-07-22 21:16:18 -0700144 std::vector<nsecs_t> mReadyTime;
Kevin DuBois305bef12019-10-09 13:23:27 -0700145};
146
147class PausingCallback {
148public:
Leon Scroggins III67388622023-02-06 20:36:20 -0500149 PausingCallback(std::shared_ptr<VSyncDispatch> dispatch, std::chrono::milliseconds pauseAmount)
150 : mDispatch(std::move(dispatch)),
151 mToken(mDispatch->registerCallback(std::bind(&PausingCallback::pause, this,
152 std::placeholders::_1,
153 std::placeholders::_2),
154 "test")),
Kevin DuBois305bef12019-10-09 13:23:27 -0700155 mRegistered(true),
156 mPauseAmount(pauseAmount) {}
157 ~PausingCallback() { unregister(); }
158
159 operator VSyncDispatch::CallbackToken() const { return mToken; }
160
Kevin DuBois2968afc2020-01-14 09:48:50 -0800161 void pause(nsecs_t, nsecs_t) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700162 std::unique_lock lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700163 mPause = true;
164 mCv.notify_all();
165
Ady Abraham8cb21882020-08-26 18:22:05 -0700166 mCv.wait_for(lock, mPauseAmount, [this] { return !mPause; });
Kevin DuBois305bef12019-10-09 13:23:27 -0700167
168 mResourcePresent = (mResource.lock() != nullptr);
169 }
170
171 bool waitForPause() {
Ady Abraham8cb21882020-08-26 18:22:05 -0700172 std::unique_lock lock(mMutex);
173 auto waiting = mCv.wait_for(lock, 10s, [this] { return mPause; });
Kevin DuBois305bef12019-10-09 13:23:27 -0700174 return waiting;
175 }
176
177 void stashResource(std::weak_ptr<void> const& resource) { mResource = resource; }
178
179 bool resourcePresent() { return mResourcePresent; }
180
181 void unpause() {
Ady Abraham8cb21882020-08-26 18:22:05 -0700182 std::unique_lock lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700183 mPause = false;
184 mCv.notify_all();
185 }
186
187 void unregister() {
188 if (mRegistered) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500189 mDispatch->unregisterCallback(mToken);
Kevin DuBois305bef12019-10-09 13:23:27 -0700190 mRegistered = false;
191 }
192 }
193
Leon Scroggins III67388622023-02-06 20:36:20 -0500194 std::shared_ptr<VSyncDispatch> mDispatch;
Kevin DuBois305bef12019-10-09 13:23:27 -0700195 VSyncDispatch::CallbackToken mToken;
196 bool mRegistered = true;
197
198 std::mutex mMutex;
199 std::condition_variable mCv;
200 bool mPause = false;
201 std::weak_ptr<void> mResource;
202 bool mResourcePresent = false;
203 std::chrono::milliseconds const mPauseAmount;
204};
205
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800206class VSyncDispatchTimerQueueTest : public testing::Test {
Kevin DuBois305bef12019-10-09 13:23:27 -0700207protected:
208 std::unique_ptr<TimeKeeper> createTimeKeeper() {
209 class TimeKeeperWrapper : public TimeKeeper {
210 public:
211 TimeKeeperWrapper(TimeKeeper& control) : mControllableClock(control) {}
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -0800212
Kevin DuBois305bef12019-10-09 13:23:27 -0700213 nsecs_t now() const final { return mControllableClock.now(); }
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -0800214
215 void alarmAt(std::function<void()> callback, nsecs_t time) final {
216 mControllableClock.alarmAt(std::move(callback), time);
217 }
218
219 void alarmCancel() final { mControllableClock.alarmCancel(); }
Ady Abraham75398722020-04-07 14:08:45 -0700220 void dump(std::string&) const final {}
Kevin DuBois305bef12019-10-09 13:23:27 -0700221
222 private:
223 TimeKeeper& mControllableClock;
224 };
225 return std::make_unique<TimeKeeperWrapper>(mMockClock);
226 }
227
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800228 ~VSyncDispatchTimerQueueTest() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700229 // destructor of dispatch will cancelAlarm(). Ignore final cancel in common test.
230 Mock::VerifyAndClearExpectations(&mMockClock);
231 }
232
233 void advanceToNextCallback() { mMockClock.advanceToNextCallback(); }
234
235 NiceMock<ControllableClock> mMockClock;
236 static nsecs_t constexpr mDispatchGroupThreshold = 5;
237 nsecs_t const mPeriod = 1000;
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800238 nsecs_t const mVsyncMoveThreshold = 300;
Leon Scroggins III67388622023-02-06 20:36:20 -0500239 std::shared_ptr<NiceMock<MockVSyncTracker>> mStubTracker =
240 std::make_shared<NiceMock<MockVSyncTracker>>(mPeriod);
241 std::shared_ptr<VSyncDispatch> mDispatch =
242 std::make_shared<VSyncDispatchTimerQueue>(createTimeKeeper(), mStubTracker,
243 mDispatchGroupThreshold, mVsyncMoveThreshold);
Kevin DuBois305bef12019-10-09 13:23:27 -0700244};
245
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800246TEST_F(VSyncDispatchTimerQueueTest, unregistersSetAlarmOnDestruction) {
Ady Abrahamb491c902020-08-15 15:47:56 -0700247 EXPECT_CALL(mMockClock, alarmAt(_, 900));
Kevin DuBois305bef12019-10-09 13:23:27 -0700248 EXPECT_CALL(mMockClock, alarmCancel());
249 {
Leon Scroggins III67388622023-02-06 20:36:20 -0500250 std::shared_ptr<VSyncDispatch> mDispatch =
251 std::make_shared<VSyncDispatchTimerQueue>(createTimeKeeper(), mStubTracker,
252 mDispatchGroupThreshold,
253 mVsyncMoveThreshold);
Kevin DuBois305bef12019-10-09 13:23:27 -0700254 CountingCallback cb(mDispatch);
Leon Scroggins III67388622023-02-06 20:36:20 -0500255 const auto result = mDispatch->schedule(cb,
256 {.workDuration = 100,
257 .readyDuration = 0,
258 .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700259 EXPECT_TRUE(result.has_value());
260 EXPECT_EQ(900, *result);
Kevin DuBois305bef12019-10-09 13:23:27 -0700261 }
262}
263
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800264TEST_F(VSyncDispatchTimerQueueTest, basicAlarmSettingFuture) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700265 auto intended = mPeriod - 230;
Ady Abrahamb491c902020-08-15 15:47:56 -0700266 EXPECT_CALL(mMockClock, alarmAt(_, 900));
Kevin DuBois305bef12019-10-09 13:23:27 -0700267
268 CountingCallback cb(mDispatch);
Leon Scroggins III67388622023-02-06 20:36:20 -0500269 const auto result = mDispatch->schedule(cb,
270 {.workDuration = 100,
271 .readyDuration = 0,
272 .earliestVsync = intended});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700273 EXPECT_TRUE(result.has_value());
274 EXPECT_EQ(900, *result);
275
Kevin DuBois305bef12019-10-09 13:23:27 -0700276 advanceToNextCallback();
277
278 ASSERT_THAT(cb.mCalls.size(), Eq(1));
279 EXPECT_THAT(cb.mCalls[0], Eq(mPeriod));
280}
281
Ady Abraham011f8ba2022-11-22 15:09:07 -0800282TEST_F(VSyncDispatchTimerQueueTest, updateAlarmSettingFuture) {
283 auto intended = mPeriod - 230;
284 Sequence seq;
285 EXPECT_CALL(mMockClock, alarmAt(_, 900)).InSequence(seq);
286 EXPECT_CALL(mMockClock, alarmAt(_, 700)).InSequence(seq);
287
288 CountingCallback cb(mDispatch);
Leon Scroggins III67388622023-02-06 20:36:20 -0500289 auto result = mDispatch->schedule(cb,
290 {.workDuration = 100,
291 .readyDuration = 0,
292 .earliestVsync = intended});
Ady Abraham011f8ba2022-11-22 15:09:07 -0800293 EXPECT_TRUE(result.has_value());
294 EXPECT_EQ(900, *result);
295
Leon Scroggins III67388622023-02-06 20:36:20 -0500296 result =
297 mDispatch->update(cb,
Ady Abraham011f8ba2022-11-22 15:09:07 -0800298 {.workDuration = 300, .readyDuration = 0, .earliestVsync = intended});
299 EXPECT_TRUE(result.has_value());
300 EXPECT_EQ(700, *result);
301
302 advanceToNextCallback();
303
304 ASSERT_THAT(cb.mCalls.size(), Eq(1));
305 EXPECT_THAT(cb.mCalls[0], Eq(mPeriod));
306 EXPECT_THAT(cb.mWakeupTime[0], Eq(700));
307}
308
309TEST_F(VSyncDispatchTimerQueueTest, updateDoesntSchedule) {
310 auto intended = mPeriod - 230;
311 EXPECT_CALL(mMockClock, alarmAt(_, _)).Times(0);
312
313 CountingCallback cb(mDispatch);
314 const auto result =
Leon Scroggins III67388622023-02-06 20:36:20 -0500315 mDispatch->update(cb,
316 {.workDuration = 300, .readyDuration = 0, .earliestVsync = intended});
Ady Abraham011f8ba2022-11-22 15:09:07 -0800317 EXPECT_FALSE(result.has_value());
318}
319
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800320TEST_F(VSyncDispatchTimerQueueTest, basicAlarmSettingFutureWithAdjustmentToTrueVsync) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500321 EXPECT_CALL(*mStubTracker.get(), nextAnticipatedVSyncTimeFrom(1000)).WillOnce(Return(1150));
Ady Abrahamb491c902020-08-15 15:47:56 -0700322 EXPECT_CALL(mMockClock, alarmAt(_, 1050));
Kevin DuBois305bef12019-10-09 13:23:27 -0700323
324 CountingCallback cb(mDispatch);
Leon Scroggins III67388622023-02-06 20:36:20 -0500325 mDispatch->schedule(cb, {.workDuration = 100, .readyDuration = 0, .earliestVsync = mPeriod});
Kevin DuBois305bef12019-10-09 13:23:27 -0700326 advanceToNextCallback();
327
328 ASSERT_THAT(cb.mCalls.size(), Eq(1));
329 EXPECT_THAT(cb.mCalls[0], Eq(1150));
330}
331
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800332TEST_F(VSyncDispatchTimerQueueTest, basicAlarmSettingAdjustmentPast) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700333 auto const now = 234;
334 mMockClock.advanceBy(234);
335 auto const workDuration = 10 * mPeriod;
Leon Scroggins III67388622023-02-06 20:36:20 -0500336 EXPECT_CALL(*mStubTracker.get(), nextAnticipatedVSyncTimeFrom(now + workDuration))
Kevin DuBois305bef12019-10-09 13:23:27 -0700337 .WillOnce(Return(mPeriod * 11));
Ady Abrahamb491c902020-08-15 15:47:56 -0700338 EXPECT_CALL(mMockClock, alarmAt(_, mPeriod));
Kevin DuBois305bef12019-10-09 13:23:27 -0700339
340 CountingCallback cb(mDispatch);
Leon Scroggins III67388622023-02-06 20:36:20 -0500341 const auto result = mDispatch->schedule(cb,
342 {.workDuration = workDuration,
343 .readyDuration = 0,
344 .earliestVsync = mPeriod});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700345 EXPECT_TRUE(result.has_value());
346 EXPECT_EQ(mPeriod, *result);
Kevin DuBois305bef12019-10-09 13:23:27 -0700347}
348
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800349TEST_F(VSyncDispatchTimerQueueTest, basicAlarmCancel) {
Ady Abrahamb491c902020-08-15 15:47:56 -0700350 EXPECT_CALL(mMockClock, alarmAt(_, 900));
Kevin DuBois305bef12019-10-09 13:23:27 -0700351 EXPECT_CALL(mMockClock, alarmCancel());
352
353 CountingCallback cb(mDispatch);
Leon Scroggins III67388622023-02-06 20:36:20 -0500354 const auto result = mDispatch->schedule(cb,
355 {.workDuration = 100,
356 .readyDuration = 0,
357 .earliestVsync = mPeriod});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700358 EXPECT_TRUE(result.has_value());
359 EXPECT_EQ(mPeriod - 100, *result);
Leon Scroggins III67388622023-02-06 20:36:20 -0500360 EXPECT_EQ(mDispatch->cancel(cb), CancelResult::Cancelled);
Kevin DuBois305bef12019-10-09 13:23:27 -0700361}
362
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800363TEST_F(VSyncDispatchTimerQueueTest, basicAlarmCancelTooLate) {
Ady Abrahamb491c902020-08-15 15:47:56 -0700364 EXPECT_CALL(mMockClock, alarmAt(_, 900));
Kevin DuBois305bef12019-10-09 13:23:27 -0700365 EXPECT_CALL(mMockClock, alarmCancel());
366
367 CountingCallback cb(mDispatch);
Leon Scroggins III67388622023-02-06 20:36:20 -0500368 const auto result = mDispatch->schedule(cb,
369 {.workDuration = 100,
370 .readyDuration = 0,
371 .earliestVsync = mPeriod});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700372 EXPECT_TRUE(result.has_value());
373 EXPECT_EQ(mPeriod - 100, *result);
Kevin DuBois305bef12019-10-09 13:23:27 -0700374 mMockClock.advanceBy(950);
Leon Scroggins III67388622023-02-06 20:36:20 -0500375 EXPECT_EQ(mDispatch->cancel(cb), CancelResult::TooLate);
Kevin DuBois305bef12019-10-09 13:23:27 -0700376}
377
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800378TEST_F(VSyncDispatchTimerQueueTest, basicAlarmCancelTooLateWhenRunning) {
Ady Abrahamb491c902020-08-15 15:47:56 -0700379 EXPECT_CALL(mMockClock, alarmAt(_, 900));
Kevin DuBois305bef12019-10-09 13:23:27 -0700380 EXPECT_CALL(mMockClock, alarmCancel());
381
382 PausingCallback cb(mDispatch, std::chrono::duration_cast<std::chrono::milliseconds>(1s));
Leon Scroggins III67388622023-02-06 20:36:20 -0500383 const auto result = mDispatch->schedule(cb,
384 {.workDuration = 100,
385 .readyDuration = 0,
386 .earliestVsync = mPeriod});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700387 EXPECT_TRUE(result.has_value());
388 EXPECT_EQ(mPeriod - 100, *result);
Kevin DuBois305bef12019-10-09 13:23:27 -0700389
390 std::thread pausingThread([&] { mMockClock.advanceToNextCallback(); });
391 EXPECT_TRUE(cb.waitForPause());
Leon Scroggins III67388622023-02-06 20:36:20 -0500392 EXPECT_EQ(mDispatch->cancel(cb), CancelResult::TooLate);
Kevin DuBois305bef12019-10-09 13:23:27 -0700393 cb.unpause();
394 pausingThread.join();
395}
396
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800397TEST_F(VSyncDispatchTimerQueueTest, unregisterSynchronizes) {
Ady Abrahamb491c902020-08-15 15:47:56 -0700398 EXPECT_CALL(mMockClock, alarmAt(_, 900));
Kevin DuBois305bef12019-10-09 13:23:27 -0700399 EXPECT_CALL(mMockClock, alarmCancel());
400
401 auto resource = std::make_shared<int>(110);
402
403 PausingCallback cb(mDispatch, 50ms);
404 cb.stashResource(resource);
Leon Scroggins III67388622023-02-06 20:36:20 -0500405 const auto result = mDispatch->schedule(cb,
406 {.workDuration = 100,
407 .readyDuration = 0,
408 .earliestVsync = mPeriod});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700409 EXPECT_TRUE(result.has_value());
410 EXPECT_EQ(mPeriod - 100, *result);
Kevin DuBois305bef12019-10-09 13:23:27 -0700411
412 std::thread pausingThread([&] { mMockClock.advanceToNextCallback(); });
413 EXPECT_TRUE(cb.waitForPause());
414
415 cb.unregister();
416 resource.reset();
417
418 cb.unpause();
419 pausingThread.join();
420
421 EXPECT_TRUE(cb.resourcePresent());
422}
423
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800424TEST_F(VSyncDispatchTimerQueueTest, basicTwoAlarmSetting) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500425 EXPECT_CALL(*mStubTracker.get(), nextAnticipatedVSyncTimeFrom(1000))
Kevin DuBois305bef12019-10-09 13:23:27 -0700426 .Times(4)
427 .WillOnce(Return(1055))
428 .WillOnce(Return(1063))
429 .WillOnce(Return(1063))
430 .WillOnce(Return(1075));
431
432 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700433 EXPECT_CALL(mMockClock, alarmAt(_, 955)).InSequence(seq);
434 EXPECT_CALL(mMockClock, alarmAt(_, 813)).InSequence(seq);
435 EXPECT_CALL(mMockClock, alarmAt(_, 975)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700436
437 CountingCallback cb0(mDispatch);
438 CountingCallback cb1(mDispatch);
439
Leon Scroggins III67388622023-02-06 20:36:20 -0500440 mDispatch->schedule(cb0, {.workDuration = 100, .readyDuration = 0, .earliestVsync = mPeriod});
441 mDispatch->schedule(cb1, {.workDuration = 250, .readyDuration = 0, .earliestVsync = mPeriod});
Kevin DuBois305bef12019-10-09 13:23:27 -0700442
443 advanceToNextCallback();
444 advanceToNextCallback();
445
446 ASSERT_THAT(cb0.mCalls.size(), Eq(1));
447 EXPECT_THAT(cb0.mCalls[0], Eq(1075));
448 ASSERT_THAT(cb1.mCalls.size(), Eq(1));
449 EXPECT_THAT(cb1.mCalls[0], Eq(1063));
450}
451
Ady Abraham3fcfd8b2022-07-12 12:31:00 -0700452TEST_F(VSyncDispatchTimerQueueTest, noCloseCallbacksAfterPeriodChange) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500453 EXPECT_CALL(*mStubTracker.get(), nextAnticipatedVSyncTimeFrom(_))
Ady Abraham3fcfd8b2022-07-12 12:31:00 -0700454 .Times(4)
455 .WillOnce(Return(1000))
456 .WillOnce(Return(2000))
457 .WillOnce(Return(2500))
458 .WillOnce(Return(4000));
459
460 Sequence seq;
461 EXPECT_CALL(mMockClock, alarmAt(_, 900)).InSequence(seq);
462 EXPECT_CALL(mMockClock, alarmAt(_, 1900)).InSequence(seq);
463 EXPECT_CALL(mMockClock, alarmAt(_, 3900)).InSequence(seq);
464
465 CountingCallback cb(mDispatch);
466
Leon Scroggins III67388622023-02-06 20:36:20 -0500467 mDispatch->schedule(cb, {.workDuration = 100, .readyDuration = 0, .earliestVsync = 0});
Ady Abraham3fcfd8b2022-07-12 12:31:00 -0700468
469 advanceToNextCallback();
470
471 ASSERT_THAT(cb.mCalls.size(), Eq(1));
472 EXPECT_THAT(cb.mCalls[0], Eq(1000));
473
Leon Scroggins III67388622023-02-06 20:36:20 -0500474 mDispatch->schedule(cb, {.workDuration = 100, .readyDuration = 0, .earliestVsync = 1000});
Ady Abraham3fcfd8b2022-07-12 12:31:00 -0700475
476 advanceToNextCallback();
477
478 ASSERT_THAT(cb.mCalls.size(), Eq(2));
479 EXPECT_THAT(cb.mCalls[1], Eq(2000));
480
Leon Scroggins III67388622023-02-06 20:36:20 -0500481 mDispatch->schedule(cb, {.workDuration = 100, .readyDuration = 0, .earliestVsync = 2000});
Ady Abraham3fcfd8b2022-07-12 12:31:00 -0700482
483 advanceToNextCallback();
484
485 ASSERT_THAT(cb.mCalls.size(), Eq(3));
486 EXPECT_THAT(cb.mCalls[2], Eq(4000));
487}
488
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800489TEST_F(VSyncDispatchTimerQueueTest, rearmsFaroutTimeoutWhenCancellingCloseOne) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500490 EXPECT_CALL(*mStubTracker.get(), nextAnticipatedVSyncTimeFrom(_))
Kevin DuBois305bef12019-10-09 13:23:27 -0700491 .Times(4)
492 .WillOnce(Return(10000))
493 .WillOnce(Return(1000))
494 .WillOnce(Return(10000))
495 .WillOnce(Return(10000));
496
497 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700498 EXPECT_CALL(mMockClock, alarmAt(_, 9900)).InSequence(seq);
499 EXPECT_CALL(mMockClock, alarmAt(_, 750)).InSequence(seq);
500 EXPECT_CALL(mMockClock, alarmAt(_, 9900)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700501
502 CountingCallback cb0(mDispatch);
503 CountingCallback cb1(mDispatch);
504
Leon Scroggins III67388622023-02-06 20:36:20 -0500505 mDispatch->schedule(cb0,
506 {.workDuration = 100, .readyDuration = 0, .earliestVsync = mPeriod * 10});
507 mDispatch->schedule(cb1, {.workDuration = 250, .readyDuration = 0, .earliestVsync = mPeriod});
508 mDispatch->cancel(cb1);
Kevin DuBois305bef12019-10-09 13:23:27 -0700509}
510
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800511TEST_F(VSyncDispatchTimerQueueTest, noUnnecessaryRearmsWhenRescheduling) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700512 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700513 EXPECT_CALL(mMockClock, alarmAt(_, 600)).InSequence(seq);
514 EXPECT_CALL(mMockClock, alarmAt(_, 700)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700515
516 CountingCallback cb0(mDispatch);
517 CountingCallback cb1(mDispatch);
518
Leon Scroggins III67388622023-02-06 20:36:20 -0500519 mDispatch->schedule(cb0, {.workDuration = 400, .readyDuration = 0, .earliestVsync = 1000});
520 mDispatch->schedule(cb1, {.workDuration = 200, .readyDuration = 0, .earliestVsync = 1000});
521 mDispatch->schedule(cb1, {.workDuration = 300, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700522 advanceToNextCallback();
523}
524
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800525TEST_F(VSyncDispatchTimerQueueTest, necessaryRearmsWhenModifying) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700526 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700527 EXPECT_CALL(mMockClock, alarmAt(_, 600)).InSequence(seq);
528 EXPECT_CALL(mMockClock, alarmAt(_, 500)).InSequence(seq);
529 EXPECT_CALL(mMockClock, alarmAt(_, 600)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700530
531 CountingCallback cb0(mDispatch);
532 CountingCallback cb1(mDispatch);
533
Leon Scroggins III67388622023-02-06 20:36:20 -0500534 mDispatch->schedule(cb0, {.workDuration = 400, .readyDuration = 0, .earliestVsync = 1000});
535 mDispatch->schedule(cb1, {.workDuration = 200, .readyDuration = 0, .earliestVsync = 1000});
536 mDispatch->schedule(cb1, {.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700537 advanceToNextCallback();
538}
539
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800540TEST_F(VSyncDispatchTimerQueueTest, modifyIntoGroup) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700541 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700542 EXPECT_CALL(mMockClock, alarmAt(_, 600)).InSequence(seq);
543 EXPECT_CALL(mMockClock, alarmAt(_, 1600)).InSequence(seq);
544 EXPECT_CALL(mMockClock, alarmAt(_, 1590)).InSequence(seq);
545 EXPECT_CALL(mMockClock, alarmAt(_, 1600)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700546
547 auto offset = 400;
548 auto closeOffset = offset + mDispatchGroupThreshold - 1;
549 auto notCloseOffset = offset + 2 * mDispatchGroupThreshold;
550
551 CountingCallback cb0(mDispatch);
552 CountingCallback cb1(mDispatch);
553
Leon Scroggins III67388622023-02-06 20:36:20 -0500554 mDispatch->schedule(cb0, {.workDuration = 400, .readyDuration = 0, .earliestVsync = 1000});
555 mDispatch->schedule(cb1, {.workDuration = 200, .readyDuration = 0, .earliestVsync = 1000});
556 mDispatch->schedule(cb1,
557 {.workDuration = closeOffset, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700558
559 advanceToNextCallback();
560 ASSERT_THAT(cb0.mCalls.size(), Eq(1));
561 EXPECT_THAT(cb0.mCalls[0], Eq(mPeriod));
562 ASSERT_THAT(cb1.mCalls.size(), Eq(1));
563 EXPECT_THAT(cb1.mCalls[0], Eq(mPeriod));
564
Leon Scroggins III67388622023-02-06 20:36:20 -0500565 mDispatch->schedule(cb0, {.workDuration = 400, .readyDuration = 0, .earliestVsync = 2000});
566 mDispatch->schedule(cb1,
567 {.workDuration = notCloseOffset,
568 .readyDuration = 0,
569 .earliestVsync = 2000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700570 advanceToNextCallback();
571 ASSERT_THAT(cb1.mCalls.size(), Eq(2));
572 EXPECT_THAT(cb1.mCalls[1], Eq(2000));
573
574 advanceToNextCallback();
575 ASSERT_THAT(cb0.mCalls.size(), Eq(2));
576 EXPECT_THAT(cb0.mCalls[1], Eq(2000));
577}
578
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800579TEST_F(VSyncDispatchTimerQueueTest, rearmsWhenEndingAndDoesntCancel) {
Ady Abrahamb491c902020-08-15 15:47:56 -0700580 Sequence seq;
581 EXPECT_CALL(mMockClock, alarmAt(_, 900)).InSequence(seq);
582 EXPECT_CALL(mMockClock, alarmAt(_, 800)).InSequence(seq);
583 EXPECT_CALL(mMockClock, alarmAt(_, 900)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700584 EXPECT_CALL(mMockClock, alarmCancel());
585
586 CountingCallback cb0(mDispatch);
587 CountingCallback cb1(mDispatch);
588
Leon Scroggins III67388622023-02-06 20:36:20 -0500589 mDispatch->schedule(cb0, {.workDuration = 100, .readyDuration = 0, .earliestVsync = 1000});
590 mDispatch->schedule(cb1, {.workDuration = 200, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700591 advanceToNextCallback();
Leon Scroggins III67388622023-02-06 20:36:20 -0500592 EXPECT_EQ(mDispatch->cancel(cb0), CancelResult::Cancelled);
Kevin DuBois305bef12019-10-09 13:23:27 -0700593}
594
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800595TEST_F(VSyncDispatchTimerQueueTest, setAlarmCallsAtCorrectTimeWithChangingVsync) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500596 EXPECT_CALL(*mStubTracker.get(), nextAnticipatedVSyncTimeFrom(_))
Kevin DuBois305bef12019-10-09 13:23:27 -0700597 .Times(3)
598 .WillOnce(Return(950))
599 .WillOnce(Return(1975))
600 .WillOnce(Return(2950));
601
602 CountingCallback cb(mDispatch);
Leon Scroggins III67388622023-02-06 20:36:20 -0500603 mDispatch->schedule(cb, {.workDuration = 100, .readyDuration = 0, .earliestVsync = 920});
Kevin DuBois305bef12019-10-09 13:23:27 -0700604
605 mMockClock.advanceBy(850);
606 EXPECT_THAT(cb.mCalls.size(), Eq(1));
607
Leon Scroggins III67388622023-02-06 20:36:20 -0500608 mDispatch->schedule(cb, {.workDuration = 100, .readyDuration = 0, .earliestVsync = 1900});
Kevin DuBois305bef12019-10-09 13:23:27 -0700609 mMockClock.advanceBy(900);
610 EXPECT_THAT(cb.mCalls.size(), Eq(1));
611 mMockClock.advanceBy(125);
612 EXPECT_THAT(cb.mCalls.size(), Eq(2));
613
Leon Scroggins III67388622023-02-06 20:36:20 -0500614 mDispatch->schedule(cb, {.workDuration = 100, .readyDuration = 0, .earliestVsync = 2900});
Kevin DuBois305bef12019-10-09 13:23:27 -0700615 mMockClock.advanceBy(975);
616 EXPECT_THAT(cb.mCalls.size(), Eq(3));
617}
618
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800619TEST_F(VSyncDispatchTimerQueueTest, callbackReentrancy) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700620 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700621 EXPECT_CALL(mMockClock, alarmAt(_, 900)).InSequence(seq);
622 EXPECT_CALL(mMockClock, alarmAt(_, 1900)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700623
624 VSyncDispatch::CallbackToken tmp;
Leon Scroggins III67388622023-02-06 20:36:20 -0500625 tmp = mDispatch->registerCallback(
Ady Abraham9c53ee72020-07-22 21:16:18 -0700626 [&](auto, auto, auto) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500627 mDispatch->schedule(tmp,
628 {.workDuration = 100,
629 .readyDuration = 0,
630 .earliestVsync = 2000});
Ady Abraham9c53ee72020-07-22 21:16:18 -0700631 },
632 "o.o");
Kevin DuBois305bef12019-10-09 13:23:27 -0700633
Leon Scroggins III67388622023-02-06 20:36:20 -0500634 mDispatch->schedule(tmp, {.workDuration = 100, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700635 advanceToNextCallback();
636}
637
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800638TEST_F(VSyncDispatchTimerQueueTest, callbackReentrantWithPastWakeup) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700639 VSyncDispatch::CallbackToken tmp;
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800640 std::optional<nsecs_t> lastTarget;
Leon Scroggins III67388622023-02-06 20:36:20 -0500641 tmp = mDispatch->registerCallback(
Ady Abraham9c53ee72020-07-22 21:16:18 -0700642 [&](auto timestamp, auto, auto) {
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700643 auto result =
Leon Scroggins III67388622023-02-06 20:36:20 -0500644 mDispatch->schedule(tmp,
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700645 {.workDuration = 400,
646 .readyDuration = 0,
Leon Scroggins III67388622023-02-06 20:36:20 -0500647 .earliestVsync = timestamp - mVsyncMoveThreshold});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700648 EXPECT_TRUE(result.has_value());
649 EXPECT_EQ(mPeriod + timestamp - 400, *result);
Leon Scroggins III67388622023-02-06 20:36:20 -0500650 result = mDispatch->schedule(tmp,
651 {.workDuration = 400,
652 .readyDuration = 0,
653 .earliestVsync = timestamp});
654 EXPECT_TRUE(result.has_value());
655 EXPECT_EQ(mPeriod + timestamp - 400, *result);
656 result = mDispatch->schedule(tmp,
657 {.workDuration = 400,
658 .readyDuration = 0,
659 .earliestVsync = timestamp + mVsyncMoveThreshold});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700660 EXPECT_TRUE(result.has_value());
661 EXPECT_EQ(mPeriod + timestamp - 400, *result);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800662 lastTarget = timestamp;
Kevin DuBois305bef12019-10-09 13:23:27 -0700663 },
664 "oo");
665
Leon Scroggins III67388622023-02-06 20:36:20 -0500666 mDispatch->schedule(tmp, {.workDuration = 999, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700667 advanceToNextCallback();
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800668 EXPECT_THAT(lastTarget, Eq(1000));
669
670 advanceToNextCallback();
671 EXPECT_THAT(lastTarget, Eq(2000));
Kevin DuBois305bef12019-10-09 13:23:27 -0700672}
673
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800674TEST_F(VSyncDispatchTimerQueueTest, modificationsAroundVsyncTime) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700675 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700676 EXPECT_CALL(mMockClock, alarmAt(_, 1000)).InSequence(seq);
677 EXPECT_CALL(mMockClock, alarmAt(_, 950)).InSequence(seq);
678 EXPECT_CALL(mMockClock, alarmAt(_, 1950)).InSequence(seq);
679 EXPECT_CALL(mMockClock, alarmAt(_, 1900)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700680
681 CountingCallback cb(mDispatch);
Leon Scroggins III67388622023-02-06 20:36:20 -0500682 mDispatch->schedule(cb, {.workDuration = 0, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700683
684 mMockClock.advanceBy(750);
Leon Scroggins III67388622023-02-06 20:36:20 -0500685 mDispatch->schedule(cb, {.workDuration = 50, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700686
687 advanceToNextCallback();
Leon Scroggins III67388622023-02-06 20:36:20 -0500688 mDispatch->schedule(cb, {.workDuration = 50, .readyDuration = 0, .earliestVsync = 2000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700689
690 mMockClock.advanceBy(800);
Leon Scroggins III67388622023-02-06 20:36:20 -0500691 mDispatch->schedule(cb, {.workDuration = 100, .readyDuration = 0, .earliestVsync = 2000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700692}
693
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800694TEST_F(VSyncDispatchTimerQueueTest, lateModifications) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700695 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700696 EXPECT_CALL(mMockClock, alarmAt(_, 500)).InSequence(seq);
697 EXPECT_CALL(mMockClock, alarmAt(_, 900)).InSequence(seq);
698 EXPECT_CALL(mMockClock, alarmAt(_, 850)).InSequence(seq);
699 EXPECT_CALL(mMockClock, alarmAt(_, 1800)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700700
701 CountingCallback cb0(mDispatch);
702 CountingCallback cb1(mDispatch);
703
Leon Scroggins III67388622023-02-06 20:36:20 -0500704 mDispatch->schedule(cb0, {.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
705 mDispatch->schedule(cb1, {.workDuration = 100, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700706
707 advanceToNextCallback();
Leon Scroggins III67388622023-02-06 20:36:20 -0500708 mDispatch->schedule(cb0, {.workDuration = 200, .readyDuration = 0, .earliestVsync = 2000});
709 mDispatch->schedule(cb1, {.workDuration = 150, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700710
711 advanceToNextCallback();
712 advanceToNextCallback();
713}
714
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800715TEST_F(VSyncDispatchTimerQueueTest, doesntCancelPriorValidTimerForFutureMod) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700716 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700717 EXPECT_CALL(mMockClock, alarmAt(_, 500)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700718
719 CountingCallback cb0(mDispatch);
720 CountingCallback cb1(mDispatch);
Leon Scroggins III67388622023-02-06 20:36:20 -0500721 mDispatch->schedule(cb0, {.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
722 mDispatch->schedule(cb1, {.workDuration = 500, .readyDuration = 0, .earliestVsync = 20000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700723}
724
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800725TEST_F(VSyncDispatchTimerQueueTest, setsTimerAfterCancellation) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700726 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700727 EXPECT_CALL(mMockClock, alarmAt(_, 500)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700728 EXPECT_CALL(mMockClock, alarmCancel()).InSequence(seq);
Ady Abrahamb491c902020-08-15 15:47:56 -0700729 EXPECT_CALL(mMockClock, alarmAt(_, 900)).InSequence(seq);
Kevin DuBois305bef12019-10-09 13:23:27 -0700730
731 CountingCallback cb0(mDispatch);
Leon Scroggins III67388622023-02-06 20:36:20 -0500732 mDispatch->schedule(cb0, {.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
733 mDispatch->cancel(cb0);
734 mDispatch->schedule(cb0, {.workDuration = 100, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700735}
736
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800737TEST_F(VSyncDispatchTimerQueueTest, makingUpIdsError) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700738 VSyncDispatch::CallbackToken token(100);
Leon Scroggins III67388622023-02-06 20:36:20 -0500739 EXPECT_FALSE(
740 mDispatch
741 ->schedule(token,
742 {.workDuration = 100, .readyDuration = 0, .earliestVsync = 1000})
743 .has_value());
744 EXPECT_THAT(mDispatch->cancel(token), Eq(CancelResult::Error));
Kevin DuBois305bef12019-10-09 13:23:27 -0700745}
746
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800747TEST_F(VSyncDispatchTimerQueueTest, canMoveCallbackBackwardsInTime) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700748 CountingCallback cb0(mDispatch);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700749 auto result =
Leon Scroggins III67388622023-02-06 20:36:20 -0500750 mDispatch->schedule(cb0,
751 {.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700752 EXPECT_TRUE(result.has_value());
753 EXPECT_EQ(500, *result);
Leon Scroggins III67388622023-02-06 20:36:20 -0500754 result = mDispatch->schedule(cb0,
755 {.workDuration = 100, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700756 EXPECT_TRUE(result.has_value());
757 EXPECT_EQ(900, *result);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800758}
759
760// b/1450138150
761TEST_F(VSyncDispatchTimerQueueTest, doesNotMoveCallbackBackwardsAndSkipAScheduledTargetVSync) {
Ady Abrahamb491c902020-08-15 15:47:56 -0700762 EXPECT_CALL(mMockClock, alarmAt(_, 500));
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800763 CountingCallback cb(mDispatch);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700764 auto result =
Leon Scroggins III67388622023-02-06 20:36:20 -0500765 mDispatch->schedule(cb,
766 {.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700767 EXPECT_TRUE(result.has_value());
768 EXPECT_EQ(500, *result);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800769 mMockClock.advanceBy(400);
770
Leon Scroggins III67388622023-02-06 20:36:20 -0500771 result = mDispatch->schedule(cb,
772 {.workDuration = 800, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700773 EXPECT_TRUE(result.has_value());
774 EXPECT_EQ(1200, *result);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800775 advanceToNextCallback();
776 ASSERT_THAT(cb.mCalls.size(), Eq(1));
777}
778
779TEST_F(VSyncDispatchTimerQueueTest, targetOffsetMovingBackALittleCanStillSchedule) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500780 EXPECT_CALL(*mStubTracker.get(), nextAnticipatedVSyncTimeFrom(1000))
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800781 .Times(2)
782 .WillOnce(Return(1000))
783 .WillOnce(Return(1002));
784 CountingCallback cb(mDispatch);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700785 auto result =
Leon Scroggins III67388622023-02-06 20:36:20 -0500786 mDispatch->schedule(cb,
787 {.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700788 EXPECT_TRUE(result.has_value());
789 EXPECT_EQ(500, *result);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800790 mMockClock.advanceBy(400);
Leon Scroggins III67388622023-02-06 20:36:20 -0500791 result = mDispatch->schedule(cb,
792 {.workDuration = 400, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700793 EXPECT_TRUE(result.has_value());
794 EXPECT_EQ(602, *result);
Kevin DuBois305bef12019-10-09 13:23:27 -0700795}
796
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800797TEST_F(VSyncDispatchTimerQueueTest, canScheduleNegativeOffsetAgainstDifferentPeriods) {
798 CountingCallback cb0(mDispatch);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700799 auto result =
Leon Scroggins III67388622023-02-06 20:36:20 -0500800 mDispatch->schedule(cb0,
801 {.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700802 EXPECT_TRUE(result.has_value());
803 EXPECT_EQ(500, *result);
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800804 advanceToNextCallback();
Leon Scroggins III67388622023-02-06 20:36:20 -0500805 result = mDispatch->schedule(cb0,
806 {.workDuration = 1100, .readyDuration = 0, .earliestVsync = 2000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700807 EXPECT_TRUE(result.has_value());
808 EXPECT_EQ(900, *result);
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800809}
810
811TEST_F(VSyncDispatchTimerQueueTest, canScheduleLargeNegativeOffset) {
812 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700813 EXPECT_CALL(mMockClock, alarmAt(_, 500)).InSequence(seq);
814 EXPECT_CALL(mMockClock, alarmAt(_, 1100)).InSequence(seq);
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800815 CountingCallback cb0(mDispatch);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700816 auto result =
Leon Scroggins III67388622023-02-06 20:36:20 -0500817 mDispatch->schedule(cb0,
818 {.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700819 EXPECT_TRUE(result.has_value());
820 EXPECT_EQ(500, *result);
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800821 advanceToNextCallback();
Leon Scroggins III67388622023-02-06 20:36:20 -0500822 result = mDispatch->schedule(cb0,
823 {.workDuration = 1900, .readyDuration = 0, .earliestVsync = 2000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700824 EXPECT_TRUE(result.has_value());
825 EXPECT_EQ(1100, *result);
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800826}
827
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800828TEST_F(VSyncDispatchTimerQueueTest, scheduleUpdatesDoesNotAffectSchedulingState) {
Ady Abrahamb491c902020-08-15 15:47:56 -0700829 EXPECT_CALL(mMockClock, alarmAt(_, 600));
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800830
831 CountingCallback cb(mDispatch);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700832 auto result =
Leon Scroggins III67388622023-02-06 20:36:20 -0500833 mDispatch->schedule(cb,
834 {.workDuration = 400, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700835 EXPECT_TRUE(result.has_value());
836 EXPECT_EQ(600, *result);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800837
Leon Scroggins III67388622023-02-06 20:36:20 -0500838 result = mDispatch->schedule(cb,
839 {.workDuration = 1400, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700840 EXPECT_TRUE(result.has_value());
841 EXPECT_EQ(600, *result);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800842
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800843 advanceToNextCallback();
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800844}
845
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800846TEST_F(VSyncDispatchTimerQueueTest, helperMove) {
Ady Abrahamb491c902020-08-15 15:47:56 -0700847 EXPECT_CALL(mMockClock, alarmAt(_, 500)).Times(1);
Kevin DuBois305bef12019-10-09 13:23:27 -0700848 EXPECT_CALL(mMockClock, alarmCancel()).Times(1);
849
850 VSyncCallbackRegistration cb(
Ady Abraham9c53ee72020-07-22 21:16:18 -0700851 mDispatch, [](auto, auto, auto) {}, "");
Kevin DuBois305bef12019-10-09 13:23:27 -0700852 VSyncCallbackRegistration cb1(std::move(cb));
Ady Abraham9c53ee72020-07-22 21:16:18 -0700853 cb.schedule({.workDuration = 100, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700854 cb.cancel();
855
Ady Abraham9c53ee72020-07-22 21:16:18 -0700856 cb1.schedule({.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700857 cb1.cancel();
858}
859
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800860TEST_F(VSyncDispatchTimerQueueTest, helperMoveAssign) {
Ady Abrahamb491c902020-08-15 15:47:56 -0700861 EXPECT_CALL(mMockClock, alarmAt(_, 500)).Times(1);
Kevin DuBois305bef12019-10-09 13:23:27 -0700862 EXPECT_CALL(mMockClock, alarmCancel()).Times(1);
863
864 VSyncCallbackRegistration cb(
Ady Abraham9c53ee72020-07-22 21:16:18 -0700865 mDispatch, [](auto, auto, auto) {}, "");
Kevin DuBois305bef12019-10-09 13:23:27 -0700866 VSyncCallbackRegistration cb1(
Ady Abraham9c53ee72020-07-22 21:16:18 -0700867 mDispatch, [](auto, auto, auto) {}, "");
Kevin DuBois305bef12019-10-09 13:23:27 -0700868 cb1 = std::move(cb);
Ady Abraham9c53ee72020-07-22 21:16:18 -0700869 cb.schedule({.workDuration = 100, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700870 cb.cancel();
871
Ady Abraham9c53ee72020-07-22 21:16:18 -0700872 cb1.schedule({.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
Kevin DuBois305bef12019-10-09 13:23:27 -0700873 cb1.cancel();
874}
875
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700876// b/154303580
877TEST_F(VSyncDispatchTimerQueueTest, skipsSchedulingIfTimerReschedulingIsImminent) {
878 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700879 EXPECT_CALL(mMockClock, alarmAt(_, 600)).InSequence(seq);
880 EXPECT_CALL(mMockClock, alarmAt(_, 1900)).InSequence(seq);
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700881 CountingCallback cb1(mDispatch);
882 CountingCallback cb2(mDispatch);
883
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700884 auto result =
Leon Scroggins III67388622023-02-06 20:36:20 -0500885 mDispatch->schedule(cb1,
886 {.workDuration = 400, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700887 EXPECT_TRUE(result.has_value());
888 EXPECT_EQ(600, *result);
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700889
890 mMockClock.setLag(100);
891 mMockClock.advanceBy(620);
892
Leon Scroggins III67388622023-02-06 20:36:20 -0500893 result = mDispatch->schedule(cb2,
894 {.workDuration = 100, .readyDuration = 0, .earliestVsync = 2000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700895 EXPECT_TRUE(result.has_value());
896 EXPECT_EQ(1900, *result);
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700897 mMockClock.advanceBy(80);
898
899 EXPECT_THAT(cb1.mCalls.size(), Eq(1));
900 EXPECT_THAT(cb2.mCalls.size(), Eq(0));
901}
902
903// b/154303580.
904// If the same callback tries to reschedule itself after it's too late, timer opts to apply the
905// update later, as opposed to blocking the calling thread.
906TEST_F(VSyncDispatchTimerQueueTest, skipsSchedulingIfTimerReschedulingIsImminentSameCallback) {
907 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700908 EXPECT_CALL(mMockClock, alarmAt(_, 600)).InSequence(seq);
909 EXPECT_CALL(mMockClock, alarmAt(_, 1630)).InSequence(seq);
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700910 CountingCallback cb(mDispatch);
911
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700912 auto result =
Leon Scroggins III67388622023-02-06 20:36:20 -0500913 mDispatch->schedule(cb,
914 {.workDuration = 400, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700915 EXPECT_TRUE(result.has_value());
916 EXPECT_EQ(600, *result);
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700917
918 mMockClock.setLag(100);
919 mMockClock.advanceBy(620);
920
Leon Scroggins III67388622023-02-06 20:36:20 -0500921 result = mDispatch->schedule(cb,
922 {.workDuration = 370, .readyDuration = 0, .earliestVsync = 2000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700923 EXPECT_TRUE(result.has_value());
924 EXPECT_EQ(1630, *result);
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700925 mMockClock.advanceBy(80);
926
927 EXPECT_THAT(cb.mCalls.size(), Eq(1));
928}
929
Kevin DuBoisb340b732020-06-16 09:07:35 -0700930// b/154303580.
931TEST_F(VSyncDispatchTimerQueueTest, skipsRearmingWhenNotNextScheduled) {
932 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700933 EXPECT_CALL(mMockClock, alarmAt(_, 600)).InSequence(seq);
Kevin DuBoisb340b732020-06-16 09:07:35 -0700934 EXPECT_CALL(mMockClock, alarmCancel()).InSequence(seq);
935 CountingCallback cb1(mDispatch);
936 CountingCallback cb2(mDispatch);
937
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700938 auto result =
Leon Scroggins III67388622023-02-06 20:36:20 -0500939 mDispatch->schedule(cb1,
940 {.workDuration = 400, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700941 EXPECT_TRUE(result.has_value());
942 EXPECT_EQ(600, *result);
Leon Scroggins III67388622023-02-06 20:36:20 -0500943 result = mDispatch->schedule(cb2,
944 {.workDuration = 100, .readyDuration = 0, .earliestVsync = 2000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700945 EXPECT_TRUE(result.has_value());
946 EXPECT_EQ(1900, *result);
Kevin DuBoisb340b732020-06-16 09:07:35 -0700947
948 mMockClock.setLag(100);
949 mMockClock.advanceBy(620);
950
Leon Scroggins III67388622023-02-06 20:36:20 -0500951 EXPECT_EQ(mDispatch->cancel(cb2), CancelResult::Cancelled);
Kevin DuBoisb340b732020-06-16 09:07:35 -0700952
953 mMockClock.advanceBy(80);
954
955 EXPECT_THAT(cb1.mCalls.size(), Eq(1));
956 EXPECT_THAT(cb2.mCalls.size(), Eq(0));
957}
958
959TEST_F(VSyncDispatchTimerQueueTest, rearmsWhenCancelledAndIsNextScheduled) {
960 Sequence seq;
Ady Abrahamb491c902020-08-15 15:47:56 -0700961 EXPECT_CALL(mMockClock, alarmAt(_, 600)).InSequence(seq);
962 EXPECT_CALL(mMockClock, alarmAt(_, 1900)).InSequence(seq);
Kevin DuBoisb340b732020-06-16 09:07:35 -0700963 EXPECT_CALL(mMockClock, alarmCancel()).InSequence(seq);
964 CountingCallback cb1(mDispatch);
965 CountingCallback cb2(mDispatch);
966
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700967 auto result =
Leon Scroggins III67388622023-02-06 20:36:20 -0500968 mDispatch->schedule(cb1,
969 {.workDuration = 400, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700970 EXPECT_TRUE(result.has_value());
971 EXPECT_EQ(600, *result);
Leon Scroggins III67388622023-02-06 20:36:20 -0500972 result = mDispatch->schedule(cb2,
973 {.workDuration = 100, .readyDuration = 0, .earliestVsync = 2000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700974 EXPECT_TRUE(result.has_value());
975 EXPECT_EQ(1900, *result);
Kevin DuBoisb340b732020-06-16 09:07:35 -0700976
977 mMockClock.setLag(100);
978 mMockClock.advanceBy(620);
979
Leon Scroggins III67388622023-02-06 20:36:20 -0500980 EXPECT_EQ(mDispatch->cancel(cb1), CancelResult::Cancelled);
Kevin DuBoisb340b732020-06-16 09:07:35 -0700981
982 EXPECT_THAT(cb1.mCalls.size(), Eq(0));
983 EXPECT_THAT(cb2.mCalls.size(), Eq(0));
984 mMockClock.advanceToNextCallback();
985
986 EXPECT_THAT(cb1.mCalls.size(), Eq(0));
987 EXPECT_THAT(cb2.mCalls.size(), Eq(1));
988}
989
Kevin DuBoisf9477832020-07-16 10:21:36 -0700990TEST_F(VSyncDispatchTimerQueueTest, laggedTimerGroupsCallbacksWithinLag) {
991 CountingCallback cb1(mDispatch);
992 CountingCallback cb2(mDispatch);
993
994 Sequence seq;
Leon Scroggins III67388622023-02-06 20:36:20 -0500995 EXPECT_CALL(*mStubTracker.get(), nextAnticipatedVSyncTimeFrom(1000))
Kevin DuBoisf9477832020-07-16 10:21:36 -0700996 .InSequence(seq)
997 .WillOnce(Return(1000));
Ady Abrahamb491c902020-08-15 15:47:56 -0700998 EXPECT_CALL(mMockClock, alarmAt(_, 600)).InSequence(seq);
Leon Scroggins III67388622023-02-06 20:36:20 -0500999 EXPECT_CALL(*mStubTracker.get(), nextAnticipatedVSyncTimeFrom(1000))
Kevin DuBoisf9477832020-07-16 10:21:36 -07001000 .InSequence(seq)
1001 .WillOnce(Return(1000));
1002
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001003 auto result =
Leon Scroggins III67388622023-02-06 20:36:20 -05001004 mDispatch->schedule(cb1,
1005 {.workDuration = 400, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001006 EXPECT_TRUE(result.has_value());
1007 EXPECT_EQ(600, *result);
Leon Scroggins III67388622023-02-06 20:36:20 -05001008 result = mDispatch->schedule(cb2,
1009 {.workDuration = 390, .readyDuration = 0, .earliestVsync = 1000});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001010 EXPECT_TRUE(result.has_value());
1011 EXPECT_EQ(610, *result);
Kevin DuBoisf9477832020-07-16 10:21:36 -07001012
1013 mMockClock.setLag(100);
1014 mMockClock.advanceBy(700);
1015
1016 ASSERT_THAT(cb1.mWakeupTime.size(), Eq(1));
1017 EXPECT_THAT(cb1.mWakeupTime[0], Eq(600));
Ady Abraham9c53ee72020-07-22 21:16:18 -07001018 ASSERT_THAT(cb1.mReadyTime.size(), Eq(1));
1019 EXPECT_THAT(cb1.mReadyTime[0], Eq(1000));
Kevin DuBoisf9477832020-07-16 10:21:36 -07001020 ASSERT_THAT(cb2.mWakeupTime.size(), Eq(1));
1021 EXPECT_THAT(cb2.mWakeupTime[0], Eq(610));
Ady Abraham9c53ee72020-07-22 21:16:18 -07001022 ASSERT_THAT(cb2.mReadyTime.size(), Eq(1));
1023 EXPECT_THAT(cb2.mReadyTime[0], Eq(1000));
1024}
1025
1026TEST_F(VSyncDispatchTimerQueueTest, basicAlarmSettingFutureWithReadyDuration) {
1027 auto intended = mPeriod - 230;
1028 EXPECT_CALL(mMockClock, alarmAt(_, 900));
1029
1030 CountingCallback cb(mDispatch);
Leon Scroggins III67388622023-02-06 20:36:20 -05001031 const auto result = mDispatch->schedule(cb,
1032 {.workDuration = 70,
1033 .readyDuration = 30,
1034 .earliestVsync = intended});
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001035 EXPECT_TRUE(result.has_value());
1036 EXPECT_EQ(900, *result);
Ady Abraham9c53ee72020-07-22 21:16:18 -07001037 advanceToNextCallback();
1038
1039 ASSERT_THAT(cb.mCalls.size(), Eq(1));
1040 EXPECT_THAT(cb.mCalls[0], Eq(mPeriod));
1041 ASSERT_THAT(cb.mWakeupTime.size(), Eq(1));
1042 EXPECT_THAT(cb.mWakeupTime[0], 900);
1043 ASSERT_THAT(cb.mReadyTime.size(), Eq(1));
1044 EXPECT_THAT(cb.mReadyTime[0], 970);
Kevin DuBoisf9477832020-07-16 10:21:36 -07001045}
1046
Ady Abraham69b9e622021-07-19 12:24:31 -07001047TEST_F(VSyncDispatchTimerQueueTest, updatesVsyncTimeForCloseWakeupTime) {
1048 Sequence seq;
1049 EXPECT_CALL(mMockClock, alarmAt(_, 600)).InSequence(seq);
1050
1051 CountingCallback cb(mDispatch);
1052
Leon Scroggins III67388622023-02-06 20:36:20 -05001053 mDispatch->schedule(cb, {.workDuration = 400, .readyDuration = 0, .earliestVsync = 1000});
1054 mDispatch->schedule(cb, {.workDuration = 1400, .readyDuration = 0, .earliestVsync = 1000});
Ady Abraham69b9e622021-07-19 12:24:31 -07001055
1056 advanceToNextCallback();
1057
1058 advanceToNextCallback();
1059
1060 ASSERT_THAT(cb.mCalls.size(), Eq(1));
1061 EXPECT_THAT(cb.mCalls[0], Eq(2000));
1062 ASSERT_THAT(cb.mWakeupTime.size(), Eq(1));
1063 EXPECT_THAT(cb.mWakeupTime[0], Eq(600));
1064 ASSERT_THAT(cb.mReadyTime.size(), Eq(1));
1065 EXPECT_THAT(cb.mReadyTime[0], Eq(2000));
1066}
1067
Ady Abraham529bd9f2023-10-05 14:55:30 -07001068// TODO(b/304338314): Set the flag value instead of skipping the test
1069TEST_F(VSyncDispatchTimerQueueTest, skipAVsyc) {
1070 // SET_FLAG_FOR_TEST(flags::dont_skip_on_early, false);
1071 if (flags::dont_skip_on_early()) GTEST_SKIP();
1072
1073 EXPECT_CALL(mMockClock, alarmAt(_, 500));
1074 CountingCallback cb(mDispatch);
1075 auto result =
1076 mDispatch->schedule(cb,
1077 {.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
1078 EXPECT_TRUE(result.has_value());
1079 EXPECT_EQ(500, *result);
1080 mMockClock.advanceBy(300);
1081
1082 result = mDispatch->schedule(cb,
1083 {.workDuration = 800, .readyDuration = 0, .earliestVsync = 1000});
1084 EXPECT_TRUE(result.has_value());
1085 EXPECT_EQ(1200, *result);
1086
1087 advanceToNextCallback();
1088 ASSERT_THAT(cb.mCalls.size(), Eq(1));
1089}
1090
1091// TODO(b/304338314): Set the flag value instead of skipping the test
1092TEST_F(VSyncDispatchTimerQueueTest, dontskipAVsyc) {
1093 // SET_FLAG_FOR_TEST(flags::dont_skip_on_early, true);
1094 if (!flags::dont_skip_on_early()) GTEST_SKIP();
1095
1096 EXPECT_CALL(mMockClock, alarmAt(_, 500));
1097 CountingCallback cb(mDispatch);
1098 auto result =
1099 mDispatch->schedule(cb,
1100 {.workDuration = 500, .readyDuration = 0, .earliestVsync = 1000});
1101 EXPECT_TRUE(result.has_value());
1102 EXPECT_EQ(500, *result);
1103 mMockClock.advanceBy(300);
1104
1105 result = mDispatch->schedule(cb,
1106 {.workDuration = 800, .readyDuration = 0, .earliestVsync = 1000});
1107 EXPECT_TRUE(result.has_value());
1108 EXPECT_EQ(200, *result);
1109
1110 advanceToNextCallback();
1111 ASSERT_THAT(cb.mCalls.size(), Eq(1));
1112}
1113
Kevin DuBoise4f27a82019-11-12 11:41:41 -08001114class VSyncDispatchTimerQueueEntryTest : public testing::Test {
Kevin DuBois305bef12019-10-09 13:23:27 -07001115protected:
1116 nsecs_t const mPeriod = 1000;
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001117 nsecs_t const mVsyncMoveThreshold = 200;
Leon Scroggins III67388622023-02-06 20:36:20 -05001118 std::shared_ptr<NiceMock<MockVSyncTracker>> mStubTracker =
1119 std::make_shared<NiceMock<MockVSyncTracker>>(mPeriod);
Kevin DuBois305bef12019-10-09 13:23:27 -07001120};
1121
Kevin DuBoise4f27a82019-11-12 11:41:41 -08001122TEST_F(VSyncDispatchTimerQueueEntryTest, stateAfterInitialization) {
Kevin DuBois305bef12019-10-09 13:23:27 -07001123 std::string name("basicname");
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001124 VSyncDispatchTimerQueueEntry entry(
Ady Abraham9c53ee72020-07-22 21:16:18 -07001125 name, [](auto, auto, auto) {}, mVsyncMoveThreshold);
Kevin DuBois305bef12019-10-09 13:23:27 -07001126 EXPECT_THAT(entry.name(), Eq(name));
1127 EXPECT_FALSE(entry.lastExecutedVsyncTarget());
1128 EXPECT_FALSE(entry.wakeupTime());
1129}
1130
Kevin DuBoise4f27a82019-11-12 11:41:41 -08001131TEST_F(VSyncDispatchTimerQueueEntryTest, stateScheduling) {
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001132 VSyncDispatchTimerQueueEntry entry(
Ady Abraham9c53ee72020-07-22 21:16:18 -07001133 "test", [](auto, auto, auto) {}, mVsyncMoveThreshold);
Kevin DuBois305bef12019-10-09 13:23:27 -07001134
1135 EXPECT_FALSE(entry.wakeupTime());
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001136 EXPECT_TRUE(entry.schedule({.workDuration = 100, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins III67388622023-02-06 20:36:20 -05001137 *mStubTracker.get(), 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001138 .has_value());
Kevin DuBois2311b1a2019-11-18 16:19:08 -08001139 auto const wakeup = entry.wakeupTime();
1140 ASSERT_TRUE(wakeup);
1141 EXPECT_THAT(*wakeup, Eq(900));
Kevin DuBois305bef12019-10-09 13:23:27 -07001142
1143 entry.disarm();
1144 EXPECT_FALSE(entry.wakeupTime());
1145}
1146
Kevin DuBoise4f27a82019-11-12 11:41:41 -08001147TEST_F(VSyncDispatchTimerQueueEntryTest, stateSchedulingReallyLongWakeupLatency) {
Kevin DuBois305bef12019-10-09 13:23:27 -07001148 auto const duration = 500;
1149 auto const now = 8750;
1150
Leon Scroggins III67388622023-02-06 20:36:20 -05001151 EXPECT_CALL(*mStubTracker.get(), nextAnticipatedVSyncTimeFrom(now + duration))
Kevin DuBois305bef12019-10-09 13:23:27 -07001152 .Times(1)
1153 .WillOnce(Return(10000));
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001154 VSyncDispatchTimerQueueEntry entry(
Ady Abraham9c53ee72020-07-22 21:16:18 -07001155 "test", [](auto, auto, auto) {}, mVsyncMoveThreshold);
Kevin DuBois305bef12019-10-09 13:23:27 -07001156
1157 EXPECT_FALSE(entry.wakeupTime());
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001158 EXPECT_TRUE(entry.schedule({.workDuration = 500, .readyDuration = 0, .earliestVsync = 994},
Leon Scroggins III67388622023-02-06 20:36:20 -05001159 *mStubTracker.get(), now)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001160 .has_value());
Kevin DuBois2311b1a2019-11-18 16:19:08 -08001161 auto const wakeup = entry.wakeupTime();
1162 ASSERT_TRUE(wakeup);
1163 EXPECT_THAT(*wakeup, Eq(9500));
Kevin DuBois305bef12019-10-09 13:23:27 -07001164}
1165
Kevin DuBoise4f27a82019-11-12 11:41:41 -08001166TEST_F(VSyncDispatchTimerQueueEntryTest, runCallback) {
Kevin DuBois305bef12019-10-09 13:23:27 -07001167 auto callCount = 0;
Kevin DuBois2968afc2020-01-14 09:48:50 -08001168 auto vsyncCalledTime = 0;
1169 auto wakeupCalledTime = 0;
Ady Abraham9c53ee72020-07-22 21:16:18 -07001170 auto readyCalledTime = 0;
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001171 VSyncDispatchTimerQueueEntry entry(
1172 "test",
Ady Abraham9c53ee72020-07-22 21:16:18 -07001173 [&](auto vsyncTime, auto wakeupTime, auto readyTime) {
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001174 callCount++;
Kevin DuBois2968afc2020-01-14 09:48:50 -08001175 vsyncCalledTime = vsyncTime;
1176 wakeupCalledTime = wakeupTime;
Ady Abraham9c53ee72020-07-22 21:16:18 -07001177 readyCalledTime = readyTime;
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001178 },
1179 mVsyncMoveThreshold);
Kevin DuBois305bef12019-10-09 13:23:27 -07001180
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001181 EXPECT_TRUE(entry.schedule({.workDuration = 100, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins III67388622023-02-06 20:36:20 -05001182 *mStubTracker.get(), 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001183 .has_value());
Kevin DuBois2311b1a2019-11-18 16:19:08 -08001184 auto const wakeup = entry.wakeupTime();
1185 ASSERT_TRUE(wakeup);
1186 EXPECT_THAT(*wakeup, Eq(900));
Kevin DuBois305bef12019-10-09 13:23:27 -07001187
Ady Abraham9c53ee72020-07-22 21:16:18 -07001188 auto const ready = entry.readyTime();
1189 ASSERT_TRUE(ready);
1190 EXPECT_THAT(*ready, Eq(1000));
1191
1192 entry.callback(entry.executing(), *wakeup, *ready);
Kevin DuBois305bef12019-10-09 13:23:27 -07001193
1194 EXPECT_THAT(callCount, Eq(1));
Kevin DuBois2968afc2020-01-14 09:48:50 -08001195 EXPECT_THAT(vsyncCalledTime, Eq(mPeriod));
1196 EXPECT_THAT(wakeupCalledTime, Eq(*wakeup));
Kevin DuBois305bef12019-10-09 13:23:27 -07001197 EXPECT_FALSE(entry.wakeupTime());
1198 auto lastCalledTarget = entry.lastExecutedVsyncTarget();
1199 ASSERT_TRUE(lastCalledTarget);
1200 EXPECT_THAT(*lastCalledTarget, Eq(mPeriod));
1201}
1202
Kevin DuBoise4f27a82019-11-12 11:41:41 -08001203TEST_F(VSyncDispatchTimerQueueEntryTest, updateCallback) {
Leon Scroggins III67388622023-02-06 20:36:20 -05001204 EXPECT_CALL(*mStubTracker.get(), nextAnticipatedVSyncTimeFrom(_))
Kevin DuBois305bef12019-10-09 13:23:27 -07001205 .Times(2)
1206 .WillOnce(Return(1000))
1207 .WillOnce(Return(1020));
1208
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001209 VSyncDispatchTimerQueueEntry entry(
Ady Abraham9c53ee72020-07-22 21:16:18 -07001210 "test", [](auto, auto, auto) {}, mVsyncMoveThreshold);
Kevin DuBois305bef12019-10-09 13:23:27 -07001211
1212 EXPECT_FALSE(entry.wakeupTime());
Leon Scroggins III67388622023-02-06 20:36:20 -05001213 entry.update(*mStubTracker.get(), 0);
Kevin DuBois305bef12019-10-09 13:23:27 -07001214 EXPECT_FALSE(entry.wakeupTime());
1215
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001216 EXPECT_TRUE(entry.schedule({.workDuration = 100, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins III67388622023-02-06 20:36:20 -05001217 *mStubTracker.get(), 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001218 .has_value());
Kevin DuBois2311b1a2019-11-18 16:19:08 -08001219 auto wakeup = entry.wakeupTime();
1220 ASSERT_TRUE(wakeup);
Kevin DuBois305bef12019-10-09 13:23:27 -07001221 EXPECT_THAT(wakeup, Eq(900));
1222
Leon Scroggins III67388622023-02-06 20:36:20 -05001223 entry.update(*mStubTracker.get(), 0);
Kevin DuBois2311b1a2019-11-18 16:19:08 -08001224 wakeup = entry.wakeupTime();
1225 ASSERT_TRUE(wakeup);
1226 EXPECT_THAT(*wakeup, Eq(920));
Kevin DuBois305bef12019-10-09 13:23:27 -07001227}
1228
Kevin DuBoise4f27a82019-11-12 11:41:41 -08001229TEST_F(VSyncDispatchTimerQueueEntryTest, skipsUpdateIfJustScheduled) {
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001230 VSyncDispatchTimerQueueEntry entry(
Ady Abraham9c53ee72020-07-22 21:16:18 -07001231 "test", [](auto, auto, auto) {}, mVsyncMoveThreshold);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001232 EXPECT_TRUE(entry.schedule({.workDuration = 100, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins III67388622023-02-06 20:36:20 -05001233 *mStubTracker.get(), 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001234 .has_value());
Leon Scroggins III67388622023-02-06 20:36:20 -05001235 entry.update(*mStubTracker.get(), 0);
Kevin DuBois305bef12019-10-09 13:23:27 -07001236
Kevin DuBois2311b1a2019-11-18 16:19:08 -08001237 auto const wakeup = entry.wakeupTime();
1238 ASSERT_TRUE(wakeup);
1239 EXPECT_THAT(*wakeup, Eq(wakeup));
1240}
1241
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001242TEST_F(VSyncDispatchTimerQueueEntryTest, willSnapToNextTargettableVSync) {
1243 VSyncDispatchTimerQueueEntry entry(
Ady Abraham9c53ee72020-07-22 21:16:18 -07001244 "test", [](auto, auto, auto) {}, mVsyncMoveThreshold);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001245 EXPECT_TRUE(entry.schedule({.workDuration = 100, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins III67388622023-02-06 20:36:20 -05001246 *mStubTracker.get(), 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001247 .has_value());
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001248 entry.executing(); // 1000 is executing
1249 // had 1000 not been executing, this could have been scheduled for time 800.
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001250 EXPECT_TRUE(entry.schedule({.workDuration = 200, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins III67388622023-02-06 20:36:20 -05001251 *mStubTracker.get(), 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001252 .has_value());
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001253 EXPECT_THAT(*entry.wakeupTime(), Eq(1800));
Ady Abraham9c53ee72020-07-22 21:16:18 -07001254 EXPECT_THAT(*entry.readyTime(), Eq(2000));
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001255
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001256 EXPECT_TRUE(entry.schedule({.workDuration = 50, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins III67388622023-02-06 20:36:20 -05001257 *mStubTracker.get(), 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001258 .has_value());
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001259 EXPECT_THAT(*entry.wakeupTime(), Eq(1950));
Ady Abraham9c53ee72020-07-22 21:16:18 -07001260 EXPECT_THAT(*entry.readyTime(), Eq(2000));
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001261
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001262 EXPECT_TRUE(entry.schedule({.workDuration = 200, .readyDuration = 0, .earliestVsync = 1001},
Leon Scroggins III67388622023-02-06 20:36:20 -05001263 *mStubTracker.get(), 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001264 .has_value());
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001265 EXPECT_THAT(*entry.wakeupTime(), Eq(1800));
Ady Abraham9c53ee72020-07-22 21:16:18 -07001266 EXPECT_THAT(*entry.readyTime(), Eq(2000));
Kevin DuBois2311b1a2019-11-18 16:19:08 -08001267}
1268
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001269TEST_F(VSyncDispatchTimerQueueEntryTest,
1270 willRequestNextEstimateWhenSnappingToNextTargettableVSync) {
1271 VSyncDispatchTimerQueueEntry entry(
Ady Abraham9c53ee72020-07-22 21:16:18 -07001272 "test", [](auto, auto, auto) {}, mVsyncMoveThreshold);
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001273
1274 Sequence seq;
Leon Scroggins III67388622023-02-06 20:36:20 -05001275 EXPECT_CALL(*mStubTracker.get(), nextAnticipatedVSyncTimeFrom(500))
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001276 .InSequence(seq)
1277 .WillOnce(Return(1000));
Leon Scroggins III67388622023-02-06 20:36:20 -05001278 EXPECT_CALL(*mStubTracker.get(), nextAnticipatedVSyncTimeFrom(500))
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001279 .InSequence(seq)
1280 .WillOnce(Return(1000));
Leon Scroggins III67388622023-02-06 20:36:20 -05001281 EXPECT_CALL(*mStubTracker.get(), nextAnticipatedVSyncTimeFrom(1000 + mVsyncMoveThreshold))
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001282 .InSequence(seq)
1283 .WillOnce(Return(2000));
1284
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001285 EXPECT_TRUE(entry.schedule({.workDuration = 100, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins III67388622023-02-06 20:36:20 -05001286 *mStubTracker.get(), 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001287 .has_value());
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001288
1289 entry.executing(); // 1000 is executing
1290
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001291 EXPECT_TRUE(entry.schedule({.workDuration = 200, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins III67388622023-02-06 20:36:20 -05001292 *mStubTracker.get(), 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001293 .has_value());
Kevin DuBoisc94ca832019-11-26 12:56:24 -08001294}
1295
1296TEST_F(VSyncDispatchTimerQueueEntryTest, reportsScheduledIfStillTime) {
1297 VSyncDispatchTimerQueueEntry entry(
Ady Abraham9c53ee72020-07-22 21:16:18 -07001298 "test", [](auto, auto, auto) {}, mVsyncMoveThreshold);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001299 EXPECT_TRUE(entry.schedule({.workDuration = 100, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins III67388622023-02-06 20:36:20 -05001300 *mStubTracker.get(), 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001301 .has_value());
1302 EXPECT_TRUE(entry.schedule({.workDuration = 200, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins III67388622023-02-06 20:36:20 -05001303 *mStubTracker.get(), 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001304 .has_value());
1305 EXPECT_TRUE(entry.schedule({.workDuration = 50, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins III67388622023-02-06 20:36:20 -05001306 *mStubTracker.get(), 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001307 .has_value());
1308 EXPECT_TRUE(entry.schedule({.workDuration = 1200, .readyDuration = 0, .earliestVsync = 500},
Leon Scroggins III67388622023-02-06 20:36:20 -05001309 *mStubTracker.get(), 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001310 .has_value());
Kevin DuBois305bef12019-10-09 13:23:27 -07001311}
1312
Kevin DuBois5c18c1c2020-05-27 15:50:50 -07001313TEST_F(VSyncDispatchTimerQueueEntryTest, storesPendingUpdatesUntilUpdate) {
1314 static constexpr auto effectualOffset = 200;
1315 VSyncDispatchTimerQueueEntry entry(
Ady Abraham9c53ee72020-07-22 21:16:18 -07001316 "test", [](auto, auto, auto) {}, mVsyncMoveThreshold);
Kevin DuBois5c18c1c2020-05-27 15:50:50 -07001317 EXPECT_FALSE(entry.hasPendingWorkloadUpdate());
Ady Abraham9c53ee72020-07-22 21:16:18 -07001318 entry.addPendingWorkloadUpdate({.workDuration = 100, .readyDuration = 0, .earliestVsync = 400});
1319 entry.addPendingWorkloadUpdate(
1320 {.workDuration = effectualOffset, .readyDuration = 0, .earliestVsync = 400});
Kevin DuBois5c18c1c2020-05-27 15:50:50 -07001321 EXPECT_TRUE(entry.hasPendingWorkloadUpdate());
Leon Scroggins III67388622023-02-06 20:36:20 -05001322 entry.update(*mStubTracker.get(), 0);
Kevin DuBois5c18c1c2020-05-27 15:50:50 -07001323 EXPECT_FALSE(entry.hasPendingWorkloadUpdate());
1324 EXPECT_THAT(*entry.wakeupTime(), Eq(mPeriod - effectualOffset));
1325}
1326
Ady Abraham9c53ee72020-07-22 21:16:18 -07001327TEST_F(VSyncDispatchTimerQueueEntryTest, runCallbackWithReadyDuration) {
1328 auto callCount = 0;
1329 auto vsyncCalledTime = 0;
1330 auto wakeupCalledTime = 0;
1331 auto readyCalledTime = 0;
1332 VSyncDispatchTimerQueueEntry entry(
1333 "test",
1334 [&](auto vsyncTime, auto wakeupTime, auto readyTime) {
1335 callCount++;
1336 vsyncCalledTime = vsyncTime;
1337 wakeupCalledTime = wakeupTime;
1338 readyCalledTime = readyTime;
1339 },
1340 mVsyncMoveThreshold);
1341
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001342 EXPECT_TRUE(entry.schedule({.workDuration = 70, .readyDuration = 30, .earliestVsync = 500},
Leon Scroggins III67388622023-02-06 20:36:20 -05001343 *mStubTracker.get(), 0)
Ady Abrahamb5d3afa2021-05-07 11:22:23 -07001344 .has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -07001345 auto const wakeup = entry.wakeupTime();
1346 ASSERT_TRUE(wakeup);
1347 EXPECT_THAT(*wakeup, Eq(900));
1348
1349 auto const ready = entry.readyTime();
1350 ASSERT_TRUE(ready);
1351 EXPECT_THAT(*ready, Eq(970));
1352
1353 entry.callback(entry.executing(), *wakeup, *ready);
1354
1355 EXPECT_THAT(callCount, Eq(1));
1356 EXPECT_THAT(vsyncCalledTime, Eq(mPeriod));
1357 EXPECT_THAT(wakeupCalledTime, Eq(*wakeup));
1358 EXPECT_FALSE(entry.wakeupTime());
1359 auto lastCalledTarget = entry.lastExecutedVsyncTarget();
1360 ASSERT_TRUE(lastCalledTarget);
1361 EXPECT_THAT(*lastCalledTarget, Eq(mPeriod));
1362}
1363
Kevin DuBois305bef12019-10-09 13:23:27 -07001364} // namespace android::scheduler
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08001365
1366// TODO(b/129481165): remove the #pragma below and fix conversion issues
Marin Shalamanovbed7fd32020-12-21 20:02:20 +01001367#pragma clang diagnostic pop // ignored "-Wconversion -Wextra"