blob: f680bdbf1bec41c4aecf505ee5aa49d8ceb09091 [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
32using testing::_;
33using testing::Invoke;
34
35namespace android {
Ady Abraham2139f732019-11-13 18:56:40 -080036
Lloyd Pique24b0a482018-03-09 18:52:26 -080037namespace {
38
Marin Shalamanova524a092020-07-27 21:39:55 +020039constexpr PhysicalDisplayId INTERNAL_DISPLAY_ID(111);
40constexpr PhysicalDisplayId EXTERNAL_DISPLAY_ID(222);
41constexpr PhysicalDisplayId DISPLAY_ID_64BIT(0xabcd12349876fedcULL);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080042
Lloyd Pique24b0a482018-03-09 18:52:26 -080043class MockVSyncSource : public VSyncSource {
44public:
Dominik Laskowski6505f792019-09-18 11:10:05 -070045 const char* getName() const override { return "test"; }
46
Lloyd Pique24b0a482018-03-09 18:52:26 -080047 MOCK_METHOD1(setVSyncEnabled, void(bool));
48 MOCK_METHOD1(setCallback, void(VSyncSource::Callback*));
Ady Abraham9c53ee72020-07-22 21:16:18 -070049 MOCK_METHOD2(setDuration,
50 void(std::chrono::nanoseconds workDuration,
51 std::chrono::nanoseconds readyDuration));
Ady Abrahamb838aed2019-02-12 15:30:16 -080052 MOCK_METHOD1(pauseVsyncCallback, void(bool));
Ady Abraham5e7371c2020-03-24 14:47:24 -070053 MOCK_CONST_METHOD1(dump, void(std::string&));
Lloyd Pique24b0a482018-03-09 18:52:26 -080054};
55
56} // namespace
57
58class EventThreadTest : public testing::Test {
59protected:
Dominik Laskowskif654d572018-12-20 11:03:06 -080060 class MockEventThreadConnection : public EventThreadConnection {
Lloyd Pique24b0a482018-03-09 18:52:26 -080061 public:
Dominik Laskowski6505f792019-09-18 11:10:05 -070062 MockEventThreadConnection(impl::EventThread* eventThread, ResyncCallback&& resyncCallback,
Ady Abraham0f4a1b12019-06-04 16:04:04 -070063 ISurfaceComposer::ConfigChanged configChanged)
64 : EventThreadConnection(eventThread, std::move(resyncCallback), configChanged) {}
Lloyd Pique24b0a482018-03-09 18:52:26 -080065 MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event));
66 };
67
68 using ConnectionEventRecorder =
69 AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>;
70
71 EventThreadTest();
72 ~EventThreadTest() override;
73
Dominik Laskowski6505f792019-09-18 11:10:05 -070074 void createThread(std::unique_ptr<VSyncSource>);
Ady Abraham0f4a1b12019-06-04 16:04:04 -070075 sp<MockEventThreadConnection> createConnection(ConnectionEventRecorder& recorder,
76 ISurfaceComposer::ConfigChanged configChanged);
Lloyd Pique24b0a482018-03-09 18:52:26 -080077
78 void expectVSyncSetEnabledCallReceived(bool expectedState);
Ady Abraham9c53ee72020-07-22 21:16:18 -070079 void expectVSyncSetDurationCallReceived(std::chrono::nanoseconds expectedDuration,
80 std::chrono::nanoseconds expectedReadyDuration);
Lloyd Pique24b0a482018-03-09 18:52:26 -080081 VSyncSource::Callback* expectVSyncSetCallbackCallReceived();
82 void expectInterceptCallReceived(nsecs_t expectedTimestamp);
83 void expectVsyncEventReceivedByConnection(const char* name,
84 ConnectionEventRecorder& connectionEventRecorder,
85 nsecs_t expectedTimestamp, unsigned expectedCount);
86 void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080087 void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070088 bool expectedConnected);
Ady Abraham447052e2019-02-13 16:07:27 -080089 void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -070090 int32_t expectedConfigId,
91 nsecs_t expectedVsyncPeriod);
Lloyd Pique24b0a482018-03-09 18:52:26 -080092
93 AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder;
94 AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder;
Ady Abraham9c53ee72020-07-22 21:16:18 -070095 AsyncCallRecorder<void (*)(std::chrono::nanoseconds, std::chrono::nanoseconds)>
96 mVSyncSetDurationCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -080097 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
98 AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder;
99 ConnectionEventRecorder mConnectionEventCallRecorder{0};
100
Dominik Laskowski6505f792019-09-18 11:10:05 -0700101 MockVSyncSource* mVSyncSource;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800102 VSyncSource::Callback* mCallback = nullptr;
Dominik Laskowski6505f792019-09-18 11:10:05 -0700103 std::unique_ptr<impl::EventThread> mThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800104 sp<MockEventThreadConnection> mConnection;
105};
106
107EventThreadTest::EventThreadTest() {
108 const ::testing::TestInfo* const test_info =
109 ::testing::UnitTest::GetInstance()->current_test_info();
110 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
111
Dominik Laskowski6505f792019-09-18 11:10:05 -0700112 auto vsyncSource = std::make_unique<MockVSyncSource>();
113 mVSyncSource = vsyncSource.get();
114
115 EXPECT_CALL(*mVSyncSource, setVSyncEnabled(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800116 .WillRepeatedly(Invoke(mVSyncSetEnabledCallRecorder.getInvocable()));
117
Dominik Laskowski6505f792019-09-18 11:10:05 -0700118 EXPECT_CALL(*mVSyncSource, setCallback(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800119 .WillRepeatedly(Invoke(mVSyncSetCallbackCallRecorder.getInvocable()));
120
Ady Abraham9c53ee72020-07-22 21:16:18 -0700121 EXPECT_CALL(*mVSyncSource, setDuration(_, _))
122 .WillRepeatedly(Invoke(mVSyncSetDurationCallRecorder.getInvocable()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800123
Dominik Laskowski6505f792019-09-18 11:10:05 -0700124 createThread(std::move(vsyncSource));
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700125 mConnection = createConnection(mConnectionEventCallRecorder,
126 ISurfaceComposer::eConfigChangedDispatch);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800127
128 // A display must be connected for VSYNC events to be delivered.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800129 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
130 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800131}
132
133EventThreadTest::~EventThreadTest() {
134 const ::testing::TestInfo* const test_info =
135 ::testing::UnitTest::GetInstance()->current_test_info();
136 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800137
138 // EventThread should unregister itself as VSyncSource callback.
Lloyd Pique0655b8e2019-01-31 18:20:27 -0800139 EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800140}
141
Dominik Laskowski6505f792019-09-18 11:10:05 -0700142void EventThreadTest::createThread(std::unique_ptr<VSyncSource> source) {
143 mThread = std::make_unique<impl::EventThread>(std::move(source),
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700144 /*tokenManager=*/nullptr,
Dominik Laskowski6505f792019-09-18 11:10:05 -0700145 mInterceptVSyncCallRecorder.getInvocable());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800146
147 // EventThread should register itself as VSyncSource callback.
148 mCallback = expectVSyncSetCallbackCallReceived();
149 ASSERT_TRUE(mCallback);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800150}
151
152sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700153 ConnectionEventRecorder& recorder, ISurfaceComposer::ConfigChanged configChanged) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800154 sp<MockEventThreadConnection> connection =
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700155 new MockEventThreadConnection(mThread.get(), mResyncCallRecorder.getInvocable(),
156 configChanged);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800157 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
158 return connection;
159}
160
161void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) {
162 auto args = mVSyncSetEnabledCallRecorder.waitForCall();
163 ASSERT_TRUE(args.has_value());
164 EXPECT_EQ(expectedState, std::get<0>(args.value()));
165}
166
Ady Abraham9c53ee72020-07-22 21:16:18 -0700167void EventThreadTest::expectVSyncSetDurationCallReceived(
168 std::chrono::nanoseconds expectedDuration, std::chrono::nanoseconds expectedReadyDuration) {
169 auto args = mVSyncSetDurationCallRecorder.waitForCall();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800170 ASSERT_TRUE(args.has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700171 EXPECT_EQ(expectedDuration, std::get<0>(args.value()));
172 EXPECT_EQ(expectedReadyDuration, std::get<1>(args.value()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800173}
174
175VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() {
176 auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall();
177 return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr;
178}
179
180void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) {
181 auto args = mInterceptVSyncCallRecorder.waitForCall();
182 ASSERT_TRUE(args.has_value());
183 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
184}
185
186void EventThreadTest::expectVsyncEventReceivedByConnection(
187 const char* name, ConnectionEventRecorder& connectionEventRecorder,
188 nsecs_t expectedTimestamp, unsigned expectedCount) {
189 auto args = connectionEventRecorder.waitForCall();
190 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
191 << expectedTimestamp;
192 const auto& event = std::get<0>(args.value());
193 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
194 << name << " did not get the correct event for timestamp " << expectedTimestamp;
195 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
196 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
197 EXPECT_EQ(expectedCount, event.vsync.count)
198 << name << " did not get the expected count for timestamp " << expectedTimestamp;
199}
200
201void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
202 unsigned expectedCount) {
203 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
204 mConnectionEventCallRecorder, expectedTimestamp,
205 expectedCount);
206}
207
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800208void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
209 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800210 auto args = mConnectionEventCallRecorder.waitForCall();
211 ASSERT_TRUE(args.has_value());
212 const auto& event = std::get<0>(args.value());
213 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800214 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800215 EXPECT_EQ(expectedConnected, event.hotplug.connected);
216}
217
Ady Abraham447052e2019-02-13 16:07:27 -0800218void EventThreadTest::expectConfigChangedEventReceivedByConnection(
Alec Mouri60aee1c2019-10-28 16:18:59 -0700219 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId,
220 nsecs_t expectedVsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800221 auto args = mConnectionEventCallRecorder.waitForCall();
222 ASSERT_TRUE(args.has_value());
223 const auto& event = std::get<0>(args.value());
224 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED, event.header.type);
225 EXPECT_EQ(expectedDisplayId, event.header.displayId);
226 EXPECT_EQ(expectedConfigId, event.config.configId);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700227 EXPECT_EQ(expectedVsyncPeriod, event.config.vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800228}
229
Lloyd Pique24b0a482018-03-09 18:52:26 -0800230namespace {
231
232/* ------------------------------------------------------------------------
233 * Test cases
234 */
235
236TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
237 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
238 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700239 EXPECT_FALSE(mVSyncSetDurationCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800240 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
241 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value());
242 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
243}
244
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800245TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800246 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
247 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800248
249 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700250 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800251
252 // EventThread should not enable vsync callbacks.
253 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800254}
255
Lloyd Pique24b0a482018-03-09 18:52:26 -0800256TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
257 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700258 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800259
Ady Abraham8532d012019-05-08 14:50:56 -0700260 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800261 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
262
Dominik Laskowski029cc122019-01-23 19:52:06 -0800263 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800264 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800265
266 // Use the received callback to signal a first vsync event.
267 // The interceptor should receive the event, as well as the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700268 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800269 expectInterceptCallReceived(123);
270 expectVsyncEventReceivedByConnection(123, 1u);
271
272 // Use the received callback to signal a second vsync event.
273 // The interceptor should receive the event, but the the connection should
274 // not as it was only interested in the first.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700275 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800276 expectInterceptCallReceived(456);
277 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
278
279 // EventThread should also detect that at this point that it does not need
280 // any more vsync events, and should disable their generation.
281 expectVSyncSetEnabledCallReceived(false);
282}
283
284TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
285 // Create a first connection, register it, and request a vsync rate of zero.
286 ConnectionEventRecorder firstConnectionEventRecorder{0};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700287 sp<MockEventThreadConnection> firstConnection =
288 createConnection(firstConnectionEventRecorder,
289 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800290 mThread->setVsyncRate(0, firstConnection);
291
292 // By itself, this should not enable vsync events
293 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
294 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
295
296 // However if there is another connection which wants events at a nonzero rate.....
297 ConnectionEventRecorder secondConnectionEventRecorder{0};
298 sp<MockEventThreadConnection> secondConnection =
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700299 createConnection(secondConnectionEventRecorder,
300 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800301 mThread->setVsyncRate(1, secondConnection);
302
Dominik Laskowski029cc122019-01-23 19:52:06 -0800303 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800304 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800305
306 // Send a vsync event. EventThread should then make a call to the
307 // interceptor, and the second connection. The first connection should not
308 // get the event.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700309 mCallback->onVSyncEvent(123, 456, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800310 expectInterceptCallReceived(123);
311 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
312 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
313 1u);
314}
315
316TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
317 mThread->setVsyncRate(1, mConnection);
318
Dominik Laskowski029cc122019-01-23 19:52:06 -0800319 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800320 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800321
322 // Send a vsync event. EventThread should then make a call to the
323 // interceptor, and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700324 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800325 expectInterceptCallReceived(123);
326 expectVsyncEventReceivedByConnection(123, 1u);
327
328 // A second event should go to the same places.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700329 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800330 expectInterceptCallReceived(456);
331 expectVsyncEventReceivedByConnection(456, 2u);
332
333 // A third event should go to the same places.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700334 mCallback->onVSyncEvent(789, 777, 111);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800335 expectInterceptCallReceived(789);
336 expectVsyncEventReceivedByConnection(789, 3u);
337}
338
339TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
340 mThread->setVsyncRate(2, mConnection);
341
Dominik Laskowski029cc122019-01-23 19:52:06 -0800342 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800343 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800344
345 // The first event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700346 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800347 expectInterceptCallReceived(123);
348 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
349
350 // The second event will be seen by the interceptor and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700351 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800352 expectInterceptCallReceived(456);
353 expectVsyncEventReceivedByConnection(456, 2u);
354
355 // The third event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700356 mCallback->onVSyncEvent(789, 777, 744);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800357 expectInterceptCallReceived(789);
358 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
359
360 // The fourth event will be seen by the interceptor and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700361 mCallback->onVSyncEvent(101112, 7847, 86);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800362 expectInterceptCallReceived(101112);
363 expectVsyncEventReceivedByConnection(101112, 4u);
364}
365
366TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
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 // Destroy the only (strong) reference to the connection.
373 mConnection = nullptr;
374
375 // The first event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700376 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800377 expectInterceptCallReceived(123);
378 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
379
380 // EventThread should disable vsync callbacks
381 expectVSyncSetEnabledCallReceived(false);
382}
383
384TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
385 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700386 sp<MockEventThreadConnection> errorConnection =
387 createConnection(errorConnectionEventRecorder,
388 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800389 mThread->setVsyncRate(1, errorConnection);
390
Dominik Laskowski029cc122019-01-23 19:52:06 -0800391 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800392 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800393
394 // The first event will be seen by the interceptor, and by the connection,
395 // which then returns an error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700396 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800397 expectInterceptCallReceived(123);
398 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
399
400 // A subsequent event will be seen by the interceptor and not by the
401 // connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700402 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800403 expectInterceptCallReceived(456);
404 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
405
406 // EventThread should disable vsync callbacks with the second event
407 expectVSyncSetEnabledCallReceived(false);
408}
409
Alec Mouri717bcb62020-02-10 17:07:19 -0800410TEST_F(EventThreadTest, tracksEventConnections) {
411 EXPECT_EQ(1, mThread->getEventThreadConnectionCount());
412 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
413 sp<MockEventThreadConnection> errorConnection =
414 createConnection(errorConnectionEventRecorder,
415 ISurfaceComposer::eConfigChangedSuppress);
416 mThread->setVsyncRate(1, errorConnection);
417 EXPECT_EQ(2, mThread->getEventThreadConnectionCount());
418 ConnectionEventRecorder secondConnectionEventRecorder{0};
419 sp<MockEventThreadConnection> secondConnection =
420 createConnection(secondConnectionEventRecorder,
421 ISurfaceComposer::eConfigChangedSuppress);
422 mThread->setVsyncRate(1, secondConnection);
423 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
424
425 // EventThread should enable vsync callbacks.
426 expectVSyncSetEnabledCallReceived(true);
427
428 // The first event will be seen by the interceptor, and by the connection,
429 // which then returns an error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700430 mCallback->onVSyncEvent(123, 456, 789);
Alec Mouri717bcb62020-02-10 17:07:19 -0800431 expectInterceptCallReceived(123);
432 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
433 expectVsyncEventReceivedByConnection("successConnection", secondConnectionEventRecorder, 123,
434 1u);
435 EXPECT_EQ(2, mThread->getEventThreadConnectionCount());
436}
437
Lloyd Pique24b0a482018-03-09 18:52:26 -0800438TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
439 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700440 sp<MockEventThreadConnection> errorConnection =
441 createConnection(errorConnectionEventRecorder,
442 ISurfaceComposer::eConfigChangedSuppress);
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 non-fatal 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 by the connection,
455 // which still then returns an non-fatal error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700456 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800457 expectInterceptCallReceived(456);
458 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
459
460 // EventThread will not disable vsync callbacks as the errors are non-fatal.
461 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
462}
463
464TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700465 mThread->setDuration(321ns, 456ns);
466 expectVSyncSetDurationCallReceived(321ns, 456ns);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800467}
468
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800469TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
470 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
471 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800472}
473
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800474TEST_F(EventThreadTest, postHotplugInternalConnect) {
475 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
476 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800477}
478
479TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800480 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
481 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800482}
483
484TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800485 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
486 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800487}
488
Ady Abraham447052e2019-02-13 16:07:27 -0800489TEST_F(EventThreadTest, postConfigChangedPrimary) {
Alec Mouri60aee1c2019-10-28 16:18:59 -0700490 mThread->onConfigChanged(INTERNAL_DISPLAY_ID, HwcConfigIndexType(7), 16666666);
491 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800492}
493
494TEST_F(EventThreadTest, postConfigChangedExternal) {
Alec Mouri60aee1c2019-10-28 16:18:59 -0700495 mThread->onConfigChanged(EXTERNAL_DISPLAY_ID, HwcConfigIndexType(5), 16666666);
496 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800497}
498
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700499TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
Alec Mouri60aee1c2019-10-28 16:18:59 -0700500 mThread->onConfigChanged(DISPLAY_ID_64BIT, HwcConfigIndexType(7), 16666666);
501 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, 16666666);
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700502}
503
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700504TEST_F(EventThreadTest, suppressConfigChanged) {
505 ConnectionEventRecorder suppressConnectionEventRecorder{0};
506 sp<MockEventThreadConnection> suppressConnection =
507 createConnection(suppressConnectionEventRecorder,
508 ISurfaceComposer::eConfigChangedSuppress);
509
Alec Mouri60aee1c2019-10-28 16:18:59 -0700510 mThread->onConfigChanged(INTERNAL_DISPLAY_ID, HwcConfigIndexType(9), 16666666);
511 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, 16666666);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700512
513 auto args = suppressConnectionEventRecorder.waitForCall();
514 ASSERT_FALSE(args.has_value());
515}
516
Lloyd Pique24b0a482018-03-09 18:52:26 -0800517} // namespace
518} // namespace android