blob: ee56178169934e6edac65486cf22decbe7ac53d8 [file] [log] [blame]
Lloyd Pique24b0a482018-03-09 18:52:26 -08001/*
2 * Copyright (C) 2018 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#undef LOG_TAG
18#define LOG_TAG "LibSurfaceFlingerUnittests"
19
20#include <gmock/gmock.h>
21#include <gtest/gtest.h>
Lloyd Pique24b0a482018-03-09 18:52:26 -080022#include <log/log.h>
Lloyd Pique24b0a482018-03-09 18:52:26 -080023#include <utils/Errors.h>
24
25#include "AsyncCallRecorder.h"
Ana Krulecfefcb582018-08-07 14:22:37 -070026#include "Scheduler/EventThread.h"
Ady Abraham2139f732019-11-13 18:56:40 -080027#include "Scheduler/HwcStrongTypes.h"
Lloyd Pique24b0a482018-03-09 18:52:26 -080028
29using namespace std::chrono_literals;
30using namespace std::placeholders;
31
Ady Abraham62f216c2020-10-13 19:07:23 -070032using namespace android::flag_operators;
Lloyd Pique24b0a482018-03-09 18:52:26 -080033using testing::_;
34using testing::Invoke;
35
36namespace android {
Ady Abraham2139f732019-11-13 18:56:40 -080037
Lloyd Pique24b0a482018-03-09 18:52:26 -080038namespace {
39
Marin Shalamanova524a092020-07-27 21:39:55 +020040constexpr PhysicalDisplayId INTERNAL_DISPLAY_ID(111);
41constexpr PhysicalDisplayId EXTERNAL_DISPLAY_ID(222);
42constexpr PhysicalDisplayId DISPLAY_ID_64BIT(0xabcd12349876fedcULL);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080043
Lloyd Pique24b0a482018-03-09 18:52:26 -080044class MockVSyncSource : public VSyncSource {
45public:
Dominik Laskowski6505f792019-09-18 11:10:05 -070046 const char* getName() const override { return "test"; }
47
Lloyd Pique24b0a482018-03-09 18:52:26 -080048 MOCK_METHOD1(setVSyncEnabled, void(bool));
49 MOCK_METHOD1(setCallback, void(VSyncSource::Callback*));
Ady Abraham9c53ee72020-07-22 21:16:18 -070050 MOCK_METHOD2(setDuration,
51 void(std::chrono::nanoseconds workDuration,
52 std::chrono::nanoseconds readyDuration));
Ady Abrahamb838aed2019-02-12 15:30:16 -080053 MOCK_METHOD1(pauseVsyncCallback, void(bool));
Ady Abraham5e7371c2020-03-24 14:47:24 -070054 MOCK_CONST_METHOD1(dump, void(std::string&));
Lloyd Pique24b0a482018-03-09 18:52:26 -080055};
56
57} // namespace
58
59class EventThreadTest : public testing::Test {
60protected:
Dominik Laskowskif654d572018-12-20 11:03:06 -080061 class MockEventThreadConnection : public EventThreadConnection {
Lloyd Pique24b0a482018-03-09 18:52:26 -080062 public:
Ady Abraham0bb6a472020-10-12 10:22:13 -070063 MockEventThreadConnection(impl::EventThread* eventThread, uid_t callingUid,
64 ResyncCallback&& resyncCallback,
Ady Abraham62f216c2020-10-13 19:07:23 -070065 ISurfaceComposer::EventRegistrationFlags eventRegistration)
Ady Abraham0bb6a472020-10-12 10:22:13 -070066 : EventThreadConnection(eventThread, callingUid, std::move(resyncCallback),
Ady Abraham62f216c2020-10-13 19:07:23 -070067 eventRegistration) {}
Lloyd Pique24b0a482018-03-09 18:52:26 -080068 MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event));
69 };
70
71 using ConnectionEventRecorder =
72 AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>;
73
74 EventThreadTest();
75 ~EventThreadTest() override;
76
Dominik Laskowski6505f792019-09-18 11:10:05 -070077 void createThread(std::unique_ptr<VSyncSource>);
Ady Abraham62f216c2020-10-13 19:07:23 -070078 sp<MockEventThreadConnection> createConnection(
79 ConnectionEventRecorder& recorder,
80 ISurfaceComposer::EventRegistrationFlags eventRegistration = {},
81 uid_t ownerUid = mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -080082
83 void expectVSyncSetEnabledCallReceived(bool expectedState);
Ady Abraham9c53ee72020-07-22 21:16:18 -070084 void expectVSyncSetDurationCallReceived(std::chrono::nanoseconds expectedDuration,
85 std::chrono::nanoseconds expectedReadyDuration);
Lloyd Pique24b0a482018-03-09 18:52:26 -080086 VSyncSource::Callback* expectVSyncSetCallbackCallReceived();
87 void expectInterceptCallReceived(nsecs_t expectedTimestamp);
88 void expectVsyncEventReceivedByConnection(const char* name,
89 ConnectionEventRecorder& connectionEventRecorder,
90 nsecs_t expectedTimestamp, unsigned expectedCount);
91 void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080092 void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070093 bool expectedConnected);
Ady Abraham447052e2019-02-13 16:07:27 -080094 void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -070095 int32_t expectedConfigId,
96 nsecs_t expectedVsyncPeriod);
Ady Abraham0bb6a472020-10-12 10:22:13 -070097 void expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t);
Ady Abraham62f216c2020-10-13 19:07:23 -070098 void expectUidFrameRateMappingEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
99 std::vector<FrameRateOverride>);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800100
101 AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder;
102 AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder;
Ady Abraham9c53ee72020-07-22 21:16:18 -0700103 AsyncCallRecorder<void (*)(std::chrono::nanoseconds, std::chrono::nanoseconds)>
104 mVSyncSetDurationCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800105 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
106 AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700107 AsyncCallRecorder<void (*)(nsecs_t, uid_t)> mThrottleVsyncCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800108 ConnectionEventRecorder mConnectionEventCallRecorder{0};
Ady Abraham0bb6a472020-10-12 10:22:13 -0700109 ConnectionEventRecorder mThrottledConnectionEventCallRecorder{0};
Lloyd Pique24b0a482018-03-09 18:52:26 -0800110
Dominik Laskowski6505f792019-09-18 11:10:05 -0700111 MockVSyncSource* mVSyncSource;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800112 VSyncSource::Callback* mCallback = nullptr;
Dominik Laskowski6505f792019-09-18 11:10:05 -0700113 std::unique_ptr<impl::EventThread> mThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800114 sp<MockEventThreadConnection> mConnection;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700115 sp<MockEventThreadConnection> mThrottledConnection;
116
117 static constexpr uid_t mConnectionUid = 443;
118 static constexpr uid_t mThrottledConnectionUid = 177;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800119};
120
121EventThreadTest::EventThreadTest() {
122 const ::testing::TestInfo* const test_info =
123 ::testing::UnitTest::GetInstance()->current_test_info();
124 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
125
Dominik Laskowski6505f792019-09-18 11:10:05 -0700126 auto vsyncSource = std::make_unique<MockVSyncSource>();
127 mVSyncSource = vsyncSource.get();
128
129 EXPECT_CALL(*mVSyncSource, setVSyncEnabled(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800130 .WillRepeatedly(Invoke(mVSyncSetEnabledCallRecorder.getInvocable()));
131
Dominik Laskowski6505f792019-09-18 11:10:05 -0700132 EXPECT_CALL(*mVSyncSource, setCallback(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800133 .WillRepeatedly(Invoke(mVSyncSetCallbackCallRecorder.getInvocable()));
134
Ady Abraham9c53ee72020-07-22 21:16:18 -0700135 EXPECT_CALL(*mVSyncSource, setDuration(_, _))
136 .WillRepeatedly(Invoke(mVSyncSetDurationCallRecorder.getInvocable()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800137
Dominik Laskowski6505f792019-09-18 11:10:05 -0700138 createThread(std::move(vsyncSource));
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700139 mConnection = createConnection(mConnectionEventCallRecorder,
Ady Abraham62f216c2020-10-13 19:07:23 -0700140 ISurfaceComposer::EventRegistration::configChanged |
141 ISurfaceComposer::EventRegistration::frameRateOverride);
142 mThrottledConnection = createConnection(mThrottledConnectionEventCallRecorder,
143 ISurfaceComposer::EventRegistration::configChanged,
144 mThrottledConnectionUid);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800145
146 // A display must be connected for VSYNC events to be delivered.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800147 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
148 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800149}
150
151EventThreadTest::~EventThreadTest() {
152 const ::testing::TestInfo* const test_info =
153 ::testing::UnitTest::GetInstance()->current_test_info();
154 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800155
156 // EventThread should unregister itself as VSyncSource callback.
Lloyd Pique0655b8e2019-01-31 18:20:27 -0800157 EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800158}
159
Dominik Laskowski6505f792019-09-18 11:10:05 -0700160void EventThreadTest::createThread(std::unique_ptr<VSyncSource> source) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700161 const auto throttleVsync = [&](nsecs_t expectedVsyncTimestamp, uid_t uid) {
162 mThrottleVsyncCallRecorder.getInvocable()(expectedVsyncTimestamp, uid);
163 return (uid == mThrottledConnectionUid);
164 };
165
Dominik Laskowski6505f792019-09-18 11:10:05 -0700166 mThread = std::make_unique<impl::EventThread>(std::move(source),
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700167 /*tokenManager=*/nullptr,
Ady Abraham0bb6a472020-10-12 10:22:13 -0700168 mInterceptVSyncCallRecorder.getInvocable(),
169 throttleVsync);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800170
171 // EventThread should register itself as VSyncSource callback.
172 mCallback = expectVSyncSetCallbackCallReceived();
173 ASSERT_TRUE(mCallback);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800174}
175
176sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
Ady Abraham62f216c2020-10-13 19:07:23 -0700177 ConnectionEventRecorder& recorder,
178 ISurfaceComposer::EventRegistrationFlags eventRegistration, uid_t ownerUid) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800179 sp<MockEventThreadConnection> connection =
Ady Abraham0bb6a472020-10-12 10:22:13 -0700180 new MockEventThreadConnection(mThread.get(), ownerUid,
Ady Abraham62f216c2020-10-13 19:07:23 -0700181 mResyncCallRecorder.getInvocable(), eventRegistration);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800182 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
183 return connection;
184}
185
186void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) {
187 auto args = mVSyncSetEnabledCallRecorder.waitForCall();
188 ASSERT_TRUE(args.has_value());
189 EXPECT_EQ(expectedState, std::get<0>(args.value()));
190}
191
Ady Abraham9c53ee72020-07-22 21:16:18 -0700192void EventThreadTest::expectVSyncSetDurationCallReceived(
193 std::chrono::nanoseconds expectedDuration, std::chrono::nanoseconds expectedReadyDuration) {
194 auto args = mVSyncSetDurationCallRecorder.waitForCall();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800195 ASSERT_TRUE(args.has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700196 EXPECT_EQ(expectedDuration, std::get<0>(args.value()));
197 EXPECT_EQ(expectedReadyDuration, std::get<1>(args.value()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800198}
199
200VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() {
201 auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall();
202 return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr;
203}
204
205void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) {
206 auto args = mInterceptVSyncCallRecorder.waitForCall();
207 ASSERT_TRUE(args.has_value());
208 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
209}
210
Ady Abraham0bb6a472020-10-12 10:22:13 -0700211void EventThreadTest::expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t uid) {
212 auto args = mThrottleVsyncCallRecorder.waitForCall();
213 ASSERT_TRUE(args.has_value());
214 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
215 EXPECT_EQ(uid, std::get<1>(args.value()));
216}
217
Lloyd Pique24b0a482018-03-09 18:52:26 -0800218void EventThreadTest::expectVsyncEventReceivedByConnection(
219 const char* name, ConnectionEventRecorder& connectionEventRecorder,
220 nsecs_t expectedTimestamp, unsigned expectedCount) {
221 auto args = connectionEventRecorder.waitForCall();
222 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
223 << expectedTimestamp;
224 const auto& event = std::get<0>(args.value());
225 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
226 << name << " did not get the correct event for timestamp " << expectedTimestamp;
227 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
228 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
229 EXPECT_EQ(expectedCount, event.vsync.count)
230 << name << " did not get the expected count for timestamp " << expectedTimestamp;
231}
232
233void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
234 unsigned expectedCount) {
235 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
236 mConnectionEventCallRecorder, expectedTimestamp,
237 expectedCount);
238}
239
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800240void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
241 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800242 auto args = mConnectionEventCallRecorder.waitForCall();
243 ASSERT_TRUE(args.has_value());
244 const auto& event = std::get<0>(args.value());
245 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800246 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800247 EXPECT_EQ(expectedConnected, event.hotplug.connected);
248}
249
Ady Abraham447052e2019-02-13 16:07:27 -0800250void EventThreadTest::expectConfigChangedEventReceivedByConnection(
Alec Mouri60aee1c2019-10-28 16:18:59 -0700251 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId,
252 nsecs_t expectedVsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800253 auto args = mConnectionEventCallRecorder.waitForCall();
254 ASSERT_TRUE(args.has_value());
255 const auto& event = std::get<0>(args.value());
256 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED, event.header.type);
257 EXPECT_EQ(expectedDisplayId, event.header.displayId);
258 EXPECT_EQ(expectedConfigId, event.config.configId);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700259 EXPECT_EQ(expectedVsyncPeriod, event.config.vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800260}
261
Ady Abraham62f216c2020-10-13 19:07:23 -0700262void EventThreadTest::expectUidFrameRateMappingEventReceivedByConnection(
263 PhysicalDisplayId expectedDisplayId, std::vector<FrameRateOverride> expectedOverrides) {
264 for (const auto [uid, frameRateHz] : expectedOverrides) {
265 auto args = mConnectionEventCallRecorder.waitForCall();
266 ASSERT_TRUE(args.has_value());
267 const auto& event = std::get<0>(args.value());
268 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE, event.header.type);
269 EXPECT_EQ(expectedDisplayId, event.header.displayId);
270 EXPECT_EQ(uid, event.frameRateOverride.uid);
271 EXPECT_EQ(frameRateHz, event.frameRateOverride.frameRateHz);
272 }
273
274 auto args = mConnectionEventCallRecorder.waitForCall();
275 ASSERT_TRUE(args.has_value());
276 const auto& event = std::get<0>(args.value());
277 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH, event.header.type);
278 EXPECT_EQ(expectedDisplayId, event.header.displayId);
279}
280
Lloyd Pique24b0a482018-03-09 18:52:26 -0800281namespace {
282
283/* ------------------------------------------------------------------------
284 * Test cases
285 */
286
287TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
288 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
289 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700290 EXPECT_FALSE(mVSyncSetDurationCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800291 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
292 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value());
293 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
294}
295
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800296TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800297 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
298 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800299
300 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700301 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800302
303 // EventThread should not enable vsync callbacks.
304 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800305}
306
Lloyd Pique24b0a482018-03-09 18:52:26 -0800307TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
308 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700309 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800310
Ady Abraham8532d012019-05-08 14:50:56 -0700311 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800312 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
313
Dominik Laskowski029cc122019-01-23 19:52:06 -0800314 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800315 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800316
317 // Use the received callback to signal a first vsync event.
318 // The interceptor should receive the event, as well as the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700319 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800320 expectInterceptCallReceived(123);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700321 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800322 expectVsyncEventReceivedByConnection(123, 1u);
323
324 // Use the received callback to signal a second vsync event.
Ady Abraham0bb6a472020-10-12 10:22:13 -0700325 // The interceptor should receive the event, but the connection should
Lloyd Pique24b0a482018-03-09 18:52:26 -0800326 // not as it was only interested in the first.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700327 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800328 expectInterceptCallReceived(456);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700329 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800330 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
331
332 // EventThread should also detect that at this point that it does not need
333 // any more vsync events, and should disable their generation.
334 expectVSyncSetEnabledCallReceived(false);
335}
336
337TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
338 // Create a first connection, register it, and request a vsync rate of zero.
339 ConnectionEventRecorder firstConnectionEventRecorder{0};
Ady Abraham62f216c2020-10-13 19:07:23 -0700340 sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800341 mThread->setVsyncRate(0, firstConnection);
342
343 // By itself, this should not enable vsync events
344 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
345 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
346
347 // However if there is another connection which wants events at a nonzero rate.....
348 ConnectionEventRecorder secondConnectionEventRecorder{0};
349 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700350 createConnection(secondConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800351 mThread->setVsyncRate(1, secondConnection);
352
Dominik Laskowski029cc122019-01-23 19:52:06 -0800353 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800354 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800355
356 // Send a vsync event. EventThread should then make a call to the
357 // interceptor, and the second connection. The first connection should not
358 // get the event.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700359 mCallback->onVSyncEvent(123, 456, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800360 expectInterceptCallReceived(123);
361 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
362 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
363 1u);
364}
365
366TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
367 mThread->setVsyncRate(1, mConnection);
368
Dominik Laskowski029cc122019-01-23 19:52:06 -0800369 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800370 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800371
372 // Send a vsync event. EventThread should then make a call to the
373 // interceptor, and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700374 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800375 expectInterceptCallReceived(123);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700376 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800377 expectVsyncEventReceivedByConnection(123, 1u);
378
379 // A second event should go to the same places.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700380 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800381 expectInterceptCallReceived(456);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700382 expectThrottleVsyncReceived(123, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800383 expectVsyncEventReceivedByConnection(456, 2u);
384
385 // A third event should go to the same places.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700386 mCallback->onVSyncEvent(789, 777, 111);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800387 expectInterceptCallReceived(789);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700388 expectThrottleVsyncReceived(777, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800389 expectVsyncEventReceivedByConnection(789, 3u);
390}
391
392TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
393 mThread->setVsyncRate(2, mConnection);
394
Dominik Laskowski029cc122019-01-23 19:52:06 -0800395 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800396 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800397
398 // The first event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700399 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800400 expectInterceptCallReceived(123);
401 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700402 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800403
404 // The second event will be seen by the interceptor and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700405 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800406 expectInterceptCallReceived(456);
407 expectVsyncEventReceivedByConnection(456, 2u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700408 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800409
410 // The third event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700411 mCallback->onVSyncEvent(789, 777, 744);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800412 expectInterceptCallReceived(789);
413 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700414 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800415
416 // The fourth event will be seen by the interceptor and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700417 mCallback->onVSyncEvent(101112, 7847, 86);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800418 expectInterceptCallReceived(101112);
419 expectVsyncEventReceivedByConnection(101112, 4u);
420}
421
422TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
423 mThread->setVsyncRate(1, mConnection);
424
Dominik Laskowski029cc122019-01-23 19:52:06 -0800425 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800426 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800427
428 // Destroy the only (strong) reference to the connection.
429 mConnection = nullptr;
430
431 // The first event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700432 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800433 expectInterceptCallReceived(123);
434 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
435
436 // EventThread should disable vsync callbacks
437 expectVSyncSetEnabledCallReceived(false);
438}
439
440TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
441 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700442 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800443 mThread->setVsyncRate(1, errorConnection);
444
Dominik Laskowski029cc122019-01-23 19:52:06 -0800445 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800446 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800447
448 // The first event will be seen by the interceptor, and by the connection,
449 // which then returns an error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700450 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800451 expectInterceptCallReceived(123);
452 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
453
454 // A subsequent event will be seen by the interceptor and not by the
455 // connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700456 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800457 expectInterceptCallReceived(456);
458 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
459
460 // EventThread should disable vsync callbacks with the second event
461 expectVSyncSetEnabledCallReceived(false);
462}
463
Alec Mouri717bcb62020-02-10 17:07:19 -0800464TEST_F(EventThreadTest, tracksEventConnections) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700465 EXPECT_EQ(2, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800466 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700467 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800468 mThread->setVsyncRate(1, errorConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700469 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800470 ConnectionEventRecorder secondConnectionEventRecorder{0};
471 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700472 createConnection(secondConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800473 mThread->setVsyncRate(1, secondConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700474 EXPECT_EQ(4, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800475
476 // EventThread should enable vsync callbacks.
477 expectVSyncSetEnabledCallReceived(true);
478
479 // The first event will be seen by the interceptor, and by the connection,
480 // which then returns an error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700481 mCallback->onVSyncEvent(123, 456, 789);
Alec Mouri717bcb62020-02-10 17:07:19 -0800482 expectInterceptCallReceived(123);
483 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
484 expectVsyncEventReceivedByConnection("successConnection", secondConnectionEventRecorder, 123,
485 1u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700486 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800487}
488
Lloyd Pique24b0a482018-03-09 18:52:26 -0800489TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
490 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham62f216c2020-10-13 19:07:23 -0700491 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800492 mThread->setVsyncRate(1, errorConnection);
493
Dominik Laskowski029cc122019-01-23 19:52:06 -0800494 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800495 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800496
497 // The first event will be seen by the interceptor, and by the connection,
498 // which then returns an non-fatal error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700499 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800500 expectInterceptCallReceived(123);
501 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
502
503 // A subsequent event will be seen by the interceptor, and by the connection,
504 // which still then returns an non-fatal error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700505 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800506 expectInterceptCallReceived(456);
507 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
508
509 // EventThread will not disable vsync callbacks as the errors are non-fatal.
510 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
511}
512
513TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700514 mThread->setDuration(321ns, 456ns);
515 expectVSyncSetDurationCallReceived(321ns, 456ns);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800516}
517
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800518TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
519 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
520 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800521}
522
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800523TEST_F(EventThreadTest, postHotplugInternalConnect) {
524 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
525 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800526}
527
528TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800529 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
530 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800531}
532
533TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800534 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
535 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800536}
537
Ady Abraham447052e2019-02-13 16:07:27 -0800538TEST_F(EventThreadTest, postConfigChangedPrimary) {
Alec Mouri60aee1c2019-10-28 16:18:59 -0700539 mThread->onConfigChanged(INTERNAL_DISPLAY_ID, HwcConfigIndexType(7), 16666666);
540 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800541}
542
543TEST_F(EventThreadTest, postConfigChangedExternal) {
Alec Mouri60aee1c2019-10-28 16:18:59 -0700544 mThread->onConfigChanged(EXTERNAL_DISPLAY_ID, HwcConfigIndexType(5), 16666666);
545 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800546}
547
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700548TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
Alec Mouri60aee1c2019-10-28 16:18:59 -0700549 mThread->onConfigChanged(DISPLAY_ID_64BIT, HwcConfigIndexType(7), 16666666);
550 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, 16666666);
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700551}
552
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700553TEST_F(EventThreadTest, suppressConfigChanged) {
554 ConnectionEventRecorder suppressConnectionEventRecorder{0};
555 sp<MockEventThreadConnection> suppressConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700556 createConnection(suppressConnectionEventRecorder);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700557
Alec Mouri60aee1c2019-10-28 16:18:59 -0700558 mThread->onConfigChanged(INTERNAL_DISPLAY_ID, HwcConfigIndexType(9), 16666666);
559 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, 16666666);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700560
561 auto args = suppressConnectionEventRecorder.waitForCall();
562 ASSERT_FALSE(args.has_value());
563}
564
Ady Abraham62f216c2020-10-13 19:07:23 -0700565TEST_F(EventThreadTest, postUidFrameRateMapping) {
566 const std::vector<FrameRateOverride> overrides = {
567 {.uid = 1, .frameRateHz = 20},
568 {.uid = 3, .frameRateHz = 40},
569 {.uid = 5, .frameRateHz = 60},
570 };
571
572 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
573 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
574}
575
576TEST_F(EventThreadTest, suppressUidFrameRateMapping) {
577 const std::vector<FrameRateOverride> overrides = {
578 {.uid = 1, .frameRateHz = 20},
579 {.uid = 3, .frameRateHz = 40},
580 {.uid = 5, .frameRateHz = 60},
581 };
582
583 ConnectionEventRecorder suppressConnectionEventRecorder{0};
584 sp<MockEventThreadConnection> suppressConnection =
585 createConnection(suppressConnectionEventRecorder);
586
587 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
588 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
589
590 auto args = suppressConnectionEventRecorder.waitForCall();
591 ASSERT_FALSE(args.has_value());
592}
593
Ady Abraham0bb6a472020-10-12 10:22:13 -0700594TEST_F(EventThreadTest, requestNextVsyncWithThrottleVsyncDoesntPostVSync) {
595 // Signal that we want the next vsync event to be posted to the throttled connection
596 mThread->requestNextVsync(mThrottledConnection);
597
598 // EventThread should immediately request a resync.
599 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
600
601 // EventThread should enable vsync callbacks.
602 expectVSyncSetEnabledCallReceived(true);
603
604 // Use the received callback to signal a first vsync event.
605 // The interceptor should receive the event, but not the connection.
606 mCallback->onVSyncEvent(123, 456, 789);
607 expectInterceptCallReceived(123);
608 expectThrottleVsyncReceived(456, mThrottledConnectionUid);
609 mThrottledConnectionEventCallRecorder.waitForUnexpectedCall();
610
611 // Use the received callback to signal a second vsync event.
612 // The interceptor should receive the event, but the connection should
613 // not as it was only interested in the first.
614 mCallback->onVSyncEvent(456, 123, 0);
615 expectInterceptCallReceived(456);
616 expectThrottleVsyncReceived(123, mThrottledConnectionUid);
617 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
618
619 // EventThread should not change the vsync state as it didn't send the event
620 // yet
621 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
622}
623
Lloyd Pique24b0a482018-03-09 18:52:26 -0800624} // namespace
625} // namespace android