blob: 61d4f47c21bb8b71b03b9eeea5722895ea3e7c33 [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>
22
23#include <log/log.h>
24
25#include <utils/Errors.h>
26
27#include "AsyncCallRecorder.h"
Ana Krulecfefcb582018-08-07 14:22:37 -070028#include "Scheduler/EventThread.h"
Ady Abraham2139f732019-11-13 18:56:40 -080029#include "Scheduler/HwcStrongTypes.h"
Lloyd Pique24b0a482018-03-09 18:52:26 -080030
31using namespace std::chrono_literals;
32using namespace std::placeholders;
33
34using testing::_;
35using testing::Invoke;
36
37namespace android {
Ady Abraham2139f732019-11-13 18:56:40 -080038
Lloyd Pique24b0a482018-03-09 18:52:26 -080039namespace {
40
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080041constexpr PhysicalDisplayId INTERNAL_DISPLAY_ID = 111;
42constexpr PhysicalDisplayId EXTERNAL_DISPLAY_ID = 222;
Ady Abrahamaf0ec272019-03-28 11:38:31 -070043constexpr PhysicalDisplayId DISPLAY_ID_64BIT = 0xabcd12349876fedcULL;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080044
Lloyd Pique24b0a482018-03-09 18:52:26 -080045class MockVSyncSource : public VSyncSource {
46public:
Dominik Laskowski6505f792019-09-18 11:10:05 -070047 const char* getName() const override { return "test"; }
48
Lloyd Pique24b0a482018-03-09 18:52:26 -080049 MOCK_METHOD1(setVSyncEnabled, void(bool));
50 MOCK_METHOD1(setCallback, void(VSyncSource::Callback*));
51 MOCK_METHOD1(setPhaseOffset, void(nsecs_t));
Ady Abrahamb838aed2019-02-12 15:30:16 -080052 MOCK_METHOD1(pauseVsyncCallback, void(bool));
Lloyd Pique24b0a482018-03-09 18:52:26 -080053};
54
55} // namespace
56
57class EventThreadTest : public testing::Test {
58protected:
Dominik Laskowskif654d572018-12-20 11:03:06 -080059 class MockEventThreadConnection : public EventThreadConnection {
Lloyd Pique24b0a482018-03-09 18:52:26 -080060 public:
Dominik Laskowski6505f792019-09-18 11:10:05 -070061 MockEventThreadConnection(impl::EventThread* eventThread, ResyncCallback&& resyncCallback,
Ady Abraham0f4a1b12019-06-04 16:04:04 -070062 ISurfaceComposer::ConfigChanged configChanged)
63 : EventThreadConnection(eventThread, std::move(resyncCallback), configChanged) {}
Lloyd Pique24b0a482018-03-09 18:52:26 -080064 MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event));
65 };
66
67 using ConnectionEventRecorder =
68 AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>;
69
70 EventThreadTest();
71 ~EventThreadTest() override;
72
Dominik Laskowski6505f792019-09-18 11:10:05 -070073 void createThread(std::unique_ptr<VSyncSource>);
Ady Abraham0f4a1b12019-06-04 16:04:04 -070074 sp<MockEventThreadConnection> createConnection(ConnectionEventRecorder& recorder,
75 ISurfaceComposer::ConfigChanged configChanged);
Lloyd Pique24b0a482018-03-09 18:52:26 -080076
77 void expectVSyncSetEnabledCallReceived(bool expectedState);
78 void expectVSyncSetPhaseOffsetCallReceived(nsecs_t expectedPhaseOffset);
79 VSyncSource::Callback* expectVSyncSetCallbackCallReceived();
80 void expectInterceptCallReceived(nsecs_t expectedTimestamp);
81 void expectVsyncEventReceivedByConnection(const char* name,
82 ConnectionEventRecorder& connectionEventRecorder,
83 nsecs_t expectedTimestamp, unsigned expectedCount);
84 void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080085 void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070086 bool expectedConnected);
Ady Abraham447052e2019-02-13 16:07:27 -080087 void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -070088 int32_t expectedConfigId,
89 nsecs_t expectedVsyncPeriod);
Lloyd Pique24b0a482018-03-09 18:52:26 -080090
91 AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder;
92 AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder;
93 AsyncCallRecorder<void (*)(nsecs_t)> mVSyncSetPhaseOffsetCallRecorder;
94 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
95 AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder;
96 ConnectionEventRecorder mConnectionEventCallRecorder{0};
97
Dominik Laskowski6505f792019-09-18 11:10:05 -070098 MockVSyncSource* mVSyncSource;
Dominik Laskowski029cc122019-01-23 19:52:06 -080099 VSyncSource::Callback* mCallback = nullptr;
Dominik Laskowski6505f792019-09-18 11:10:05 -0700100 std::unique_ptr<impl::EventThread> mThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800101 sp<MockEventThreadConnection> mConnection;
102};
103
104EventThreadTest::EventThreadTest() {
105 const ::testing::TestInfo* const test_info =
106 ::testing::UnitTest::GetInstance()->current_test_info();
107 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
108
Dominik Laskowski6505f792019-09-18 11:10:05 -0700109 auto vsyncSource = std::make_unique<MockVSyncSource>();
110 mVSyncSource = vsyncSource.get();
111
112 EXPECT_CALL(*mVSyncSource, setVSyncEnabled(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800113 .WillRepeatedly(Invoke(mVSyncSetEnabledCallRecorder.getInvocable()));
114
Dominik Laskowski6505f792019-09-18 11:10:05 -0700115 EXPECT_CALL(*mVSyncSource, setCallback(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800116 .WillRepeatedly(Invoke(mVSyncSetCallbackCallRecorder.getInvocable()));
117
Dominik Laskowski6505f792019-09-18 11:10:05 -0700118 EXPECT_CALL(*mVSyncSource, setPhaseOffset(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800119 .WillRepeatedly(Invoke(mVSyncSetPhaseOffsetCallRecorder.getInvocable()));
120
Dominik Laskowski6505f792019-09-18 11:10:05 -0700121 createThread(std::move(vsyncSource));
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700122 mConnection = createConnection(mConnectionEventCallRecorder,
123 ISurfaceComposer::eConfigChangedDispatch);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800124
125 // A display must be connected for VSYNC events to be delivered.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800126 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
127 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800128}
129
130EventThreadTest::~EventThreadTest() {
131 const ::testing::TestInfo* const test_info =
132 ::testing::UnitTest::GetInstance()->current_test_info();
133 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800134
135 // EventThread should unregister itself as VSyncSource callback.
Lloyd Pique0655b8e2019-01-31 18:20:27 -0800136 EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800137}
138
Dominik Laskowski6505f792019-09-18 11:10:05 -0700139void EventThreadTest::createThread(std::unique_ptr<VSyncSource> source) {
140 mThread = std::make_unique<impl::EventThread>(std::move(source),
141 mInterceptVSyncCallRecorder.getInvocable());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800142
143 // EventThread should register itself as VSyncSource callback.
144 mCallback = expectVSyncSetCallbackCallReceived();
145 ASSERT_TRUE(mCallback);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800146}
147
148sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700149 ConnectionEventRecorder& recorder, ISurfaceComposer::ConfigChanged configChanged) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800150 sp<MockEventThreadConnection> connection =
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700151 new MockEventThreadConnection(mThread.get(), mResyncCallRecorder.getInvocable(),
152 configChanged);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800153 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
154 return connection;
155}
156
157void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) {
158 auto args = mVSyncSetEnabledCallRecorder.waitForCall();
159 ASSERT_TRUE(args.has_value());
160 EXPECT_EQ(expectedState, std::get<0>(args.value()));
161}
162
163void EventThreadTest::expectVSyncSetPhaseOffsetCallReceived(nsecs_t expectedPhaseOffset) {
164 auto args = mVSyncSetPhaseOffsetCallRecorder.waitForCall();
165 ASSERT_TRUE(args.has_value());
166 EXPECT_EQ(expectedPhaseOffset, std::get<0>(args.value()));
167}
168
169VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() {
170 auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall();
171 return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr;
172}
173
174void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) {
175 auto args = mInterceptVSyncCallRecorder.waitForCall();
176 ASSERT_TRUE(args.has_value());
177 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
178}
179
180void EventThreadTest::expectVsyncEventReceivedByConnection(
181 const char* name, ConnectionEventRecorder& connectionEventRecorder,
182 nsecs_t expectedTimestamp, unsigned expectedCount) {
183 auto args = connectionEventRecorder.waitForCall();
184 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
185 << expectedTimestamp;
186 const auto& event = std::get<0>(args.value());
187 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
188 << name << " did not get the correct event for timestamp " << expectedTimestamp;
189 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
190 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
191 EXPECT_EQ(expectedCount, event.vsync.count)
192 << name << " did not get the expected count for timestamp " << expectedTimestamp;
193}
194
195void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
196 unsigned expectedCount) {
197 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
198 mConnectionEventCallRecorder, expectedTimestamp,
199 expectedCount);
200}
201
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800202void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
203 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800204 auto args = mConnectionEventCallRecorder.waitForCall();
205 ASSERT_TRUE(args.has_value());
206 const auto& event = std::get<0>(args.value());
207 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800208 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800209 EXPECT_EQ(expectedConnected, event.hotplug.connected);
210}
211
Ady Abraham447052e2019-02-13 16:07:27 -0800212void EventThreadTest::expectConfigChangedEventReceivedByConnection(
Alec Mouri60aee1c2019-10-28 16:18:59 -0700213 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId,
214 nsecs_t expectedVsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800215 auto args = mConnectionEventCallRecorder.waitForCall();
216 ASSERT_TRUE(args.has_value());
217 const auto& event = std::get<0>(args.value());
218 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED, event.header.type);
219 EXPECT_EQ(expectedDisplayId, event.header.displayId);
220 EXPECT_EQ(expectedConfigId, event.config.configId);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700221 EXPECT_EQ(expectedVsyncPeriod, event.config.vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800222}
223
Lloyd Pique24b0a482018-03-09 18:52:26 -0800224namespace {
225
226/* ------------------------------------------------------------------------
227 * Test cases
228 */
229
230TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
231 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
232 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
233 EXPECT_FALSE(mVSyncSetPhaseOffsetCallRecorder.waitForCall(0us).has_value());
234 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
235 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value());
236 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
237}
238
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800239TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800240 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
241 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800242
243 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700244 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800245
246 // EventThread should not enable vsync callbacks.
247 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800248}
249
Lloyd Pique24b0a482018-03-09 18:52:26 -0800250TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
251 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700252 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800253
Ady Abraham8532d012019-05-08 14:50:56 -0700254 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800255 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
256
Dominik Laskowski029cc122019-01-23 19:52:06 -0800257 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800258 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800259
260 // Use the received callback to signal a first vsync event.
261 // The interceptor should receive the event, as well as the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800262 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800263 expectInterceptCallReceived(123);
264 expectVsyncEventReceivedByConnection(123, 1u);
265
266 // Use the received callback to signal a second vsync event.
267 // The interceptor should receive the event, but the the connection should
268 // not as it was only interested in the first.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800269 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800270 expectInterceptCallReceived(456);
271 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
272
273 // EventThread should also detect that at this point that it does not need
274 // any more vsync events, and should disable their generation.
275 expectVSyncSetEnabledCallReceived(false);
276}
277
278TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
279 // Create a first connection, register it, and request a vsync rate of zero.
280 ConnectionEventRecorder firstConnectionEventRecorder{0};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700281 sp<MockEventThreadConnection> firstConnection =
282 createConnection(firstConnectionEventRecorder,
283 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800284 mThread->setVsyncRate(0, firstConnection);
285
286 // By itself, this should not enable vsync events
287 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
288 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
289
290 // However if there is another connection which wants events at a nonzero rate.....
291 ConnectionEventRecorder secondConnectionEventRecorder{0};
292 sp<MockEventThreadConnection> secondConnection =
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700293 createConnection(secondConnectionEventRecorder,
294 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800295 mThread->setVsyncRate(1, secondConnection);
296
Dominik Laskowski029cc122019-01-23 19:52:06 -0800297 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800298 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800299
300 // Send a vsync event. EventThread should then make a call to the
301 // interceptor, and the second connection. The first connection should not
302 // get the event.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800303 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800304 expectInterceptCallReceived(123);
305 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
306 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
307 1u);
308}
309
310TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
311 mThread->setVsyncRate(1, mConnection);
312
Dominik Laskowski029cc122019-01-23 19:52:06 -0800313 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800314 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800315
316 // Send a vsync event. EventThread should then make a call to the
317 // interceptor, and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800318 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800319 expectInterceptCallReceived(123);
320 expectVsyncEventReceivedByConnection(123, 1u);
321
322 // A second event should go to the same places.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800323 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800324 expectInterceptCallReceived(456);
325 expectVsyncEventReceivedByConnection(456, 2u);
326
327 // A third event should go to the same places.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800328 mCallback->onVSyncEvent(789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800329 expectInterceptCallReceived(789);
330 expectVsyncEventReceivedByConnection(789, 3u);
331}
332
333TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
334 mThread->setVsyncRate(2, mConnection);
335
Dominik Laskowski029cc122019-01-23 19:52:06 -0800336 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800337 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800338
339 // The first event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800340 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800341 expectInterceptCallReceived(123);
342 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
343
344 // The second event will be seen by the interceptor and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800345 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800346 expectInterceptCallReceived(456);
347 expectVsyncEventReceivedByConnection(456, 2u);
348
349 // The third event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800350 mCallback->onVSyncEvent(789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800351 expectInterceptCallReceived(789);
352 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
353
354 // The fourth event will be seen by the interceptor and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800355 mCallback->onVSyncEvent(101112);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800356 expectInterceptCallReceived(101112);
357 expectVsyncEventReceivedByConnection(101112, 4u);
358}
359
360TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
361 mThread->setVsyncRate(1, mConnection);
362
Dominik Laskowski029cc122019-01-23 19:52:06 -0800363 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800364 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800365
366 // Destroy the only (strong) reference to the connection.
367 mConnection = nullptr;
368
369 // The first event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800370 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800371 expectInterceptCallReceived(123);
372 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
373
374 // EventThread should disable vsync callbacks
375 expectVSyncSetEnabledCallReceived(false);
376}
377
378TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
379 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700380 sp<MockEventThreadConnection> errorConnection =
381 createConnection(errorConnectionEventRecorder,
382 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800383 mThread->setVsyncRate(1, errorConnection);
384
Dominik Laskowski029cc122019-01-23 19:52:06 -0800385 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800386 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800387
388 // The first event will be seen by the interceptor, and by the connection,
389 // which then returns an error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800390 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800391 expectInterceptCallReceived(123);
392 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
393
394 // A subsequent event will be seen by the interceptor and not by the
395 // connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800396 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800397 expectInterceptCallReceived(456);
398 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
399
400 // EventThread should disable vsync callbacks with the second event
401 expectVSyncSetEnabledCallReceived(false);
402}
403
404TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
405 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700406 sp<MockEventThreadConnection> errorConnection =
407 createConnection(errorConnectionEventRecorder,
408 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800409 mThread->setVsyncRate(1, errorConnection);
410
Dominik Laskowski029cc122019-01-23 19:52:06 -0800411 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800412 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800413
414 // The first event will be seen by the interceptor, and by the connection,
415 // which then returns an non-fatal error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800416 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800417 expectInterceptCallReceived(123);
418 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
419
420 // A subsequent event will be seen by the interceptor, and by the connection,
421 // which still then returns an non-fatal error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800422 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800423 expectInterceptCallReceived(456);
424 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
425
426 // EventThread will not disable vsync callbacks as the errors are non-fatal.
427 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
428}
429
430TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
431 mThread->setPhaseOffset(321);
432 expectVSyncSetPhaseOffsetCallReceived(321);
433}
434
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800435TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
436 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
437 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800438}
439
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800440TEST_F(EventThreadTest, postHotplugInternalConnect) {
441 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
442 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800443}
444
445TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800446 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
447 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800448}
449
450TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800451 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
452 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800453}
454
Ady Abraham447052e2019-02-13 16:07:27 -0800455TEST_F(EventThreadTest, postConfigChangedPrimary) {
Alec Mouri60aee1c2019-10-28 16:18:59 -0700456 mThread->onConfigChanged(INTERNAL_DISPLAY_ID, HwcConfigIndexType(7), 16666666);
457 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800458}
459
460TEST_F(EventThreadTest, postConfigChangedExternal) {
Alec Mouri60aee1c2019-10-28 16:18:59 -0700461 mThread->onConfigChanged(EXTERNAL_DISPLAY_ID, HwcConfigIndexType(5), 16666666);
462 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800463}
464
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700465TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
Alec Mouri60aee1c2019-10-28 16:18:59 -0700466 mThread->onConfigChanged(DISPLAY_ID_64BIT, HwcConfigIndexType(7), 16666666);
467 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, 16666666);
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700468}
469
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700470TEST_F(EventThreadTest, suppressConfigChanged) {
471 ConnectionEventRecorder suppressConnectionEventRecorder{0};
472 sp<MockEventThreadConnection> suppressConnection =
473 createConnection(suppressConnectionEventRecorder,
474 ISurfaceComposer::eConfigChangedSuppress);
475
Alec Mouri60aee1c2019-10-28 16:18:59 -0700476 mThread->onConfigChanged(INTERNAL_DISPLAY_ID, HwcConfigIndexType(9), 16666666);
477 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, 16666666);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700478
479 auto args = suppressConnectionEventRecorder.waitForCall();
480 ASSERT_FALSE(args.has_value());
481}
482
Lloyd Pique24b0a482018-03-09 18:52:26 -0800483} // namespace
484} // namespace android