blob: 45bc29cabba130cb4e6c7abf4a04835282adf143 [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
Marin Shalamanovbed7fd32020-12-21 20:02:20 +010017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wextra"
20
Lloyd Pique24b0a482018-03-09 18:52:26 -080021#undef LOG_TAG
22#define LOG_TAG "LibSurfaceFlingerUnittests"
23
24#include <gmock/gmock.h>
25#include <gtest/gtest.h>
Lloyd Pique24b0a482018-03-09 18:52:26 -080026#include <log/log.h>
Lloyd Pique24b0a482018-03-09 18:52:26 -080027#include <utils/Errors.h>
28
29#include "AsyncCallRecorder.h"
Marin Shalamanov23c44202020-12-22 19:09:20 +010030#include "DisplayHardware/DisplayMode.h"
Ana Krulecfefcb582018-08-07 14:22:37 -070031#include "Scheduler/EventThread.h"
Lloyd Pique24b0a482018-03-09 18:52:26 -080032
33using namespace std::chrono_literals;
34using namespace std::placeholders;
35
Ady Abraham62f216c2020-10-13 19:07:23 -070036using namespace android::flag_operators;
Lloyd Pique24b0a482018-03-09 18:52:26 -080037using testing::_;
38using testing::Invoke;
39
40namespace android {
Ady Abraham2139f732019-11-13 18:56:40 -080041
Lloyd Pique24b0a482018-03-09 18:52:26 -080042namespace {
43
Marin Shalamanova524a092020-07-27 21:39:55 +020044constexpr PhysicalDisplayId INTERNAL_DISPLAY_ID(111);
45constexpr PhysicalDisplayId EXTERNAL_DISPLAY_ID(222);
46constexpr PhysicalDisplayId DISPLAY_ID_64BIT(0xabcd12349876fedcULL);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080047
Lloyd Pique24b0a482018-03-09 18:52:26 -080048class MockVSyncSource : public VSyncSource {
49public:
Dominik Laskowski6505f792019-09-18 11:10:05 -070050 const char* getName() const override { return "test"; }
51
Lloyd Pique24b0a482018-03-09 18:52:26 -080052 MOCK_METHOD1(setVSyncEnabled, void(bool));
53 MOCK_METHOD1(setCallback, void(VSyncSource::Callback*));
Ady Abraham9c53ee72020-07-22 21:16:18 -070054 MOCK_METHOD2(setDuration,
55 void(std::chrono::nanoseconds workDuration,
56 std::chrono::nanoseconds readyDuration));
Ady Abrahamb838aed2019-02-12 15:30:16 -080057 MOCK_METHOD1(pauseVsyncCallback, void(bool));
Ady Abraham5e7371c2020-03-24 14:47:24 -070058 MOCK_CONST_METHOD1(dump, void(std::string&));
Lloyd Pique24b0a482018-03-09 18:52:26 -080059};
60
61} // namespace
62
63class EventThreadTest : public testing::Test {
64protected:
Dominik Laskowskif654d572018-12-20 11:03:06 -080065 class MockEventThreadConnection : public EventThreadConnection {
Lloyd Pique24b0a482018-03-09 18:52:26 -080066 public:
Ady Abraham0bb6a472020-10-12 10:22:13 -070067 MockEventThreadConnection(impl::EventThread* eventThread, uid_t callingUid,
68 ResyncCallback&& resyncCallback,
Ady Abraham62f216c2020-10-13 19:07:23 -070069 ISurfaceComposer::EventRegistrationFlags eventRegistration)
Ady Abraham0bb6a472020-10-12 10:22:13 -070070 : EventThreadConnection(eventThread, callingUid, std::move(resyncCallback),
Ady Abraham62f216c2020-10-13 19:07:23 -070071 eventRegistration) {}
Lloyd Pique24b0a482018-03-09 18:52:26 -080072 MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event));
73 };
74
75 using ConnectionEventRecorder =
76 AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>;
77
78 EventThreadTest();
79 ~EventThreadTest() override;
80
Dominik Laskowski6505f792019-09-18 11:10:05 -070081 void createThread(std::unique_ptr<VSyncSource>);
Ady Abraham62f216c2020-10-13 19:07:23 -070082 sp<MockEventThreadConnection> createConnection(
83 ConnectionEventRecorder& recorder,
84 ISurfaceComposer::EventRegistrationFlags eventRegistration = {},
85 uid_t ownerUid = mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -080086
87 void expectVSyncSetEnabledCallReceived(bool expectedState);
Ady Abraham9c53ee72020-07-22 21:16:18 -070088 void expectVSyncSetDurationCallReceived(std::chrono::nanoseconds expectedDuration,
89 std::chrono::nanoseconds expectedReadyDuration);
Lloyd Pique24b0a482018-03-09 18:52:26 -080090 VSyncSource::Callback* expectVSyncSetCallbackCallReceived();
91 void expectInterceptCallReceived(nsecs_t expectedTimestamp);
92 void expectVsyncEventReceivedByConnection(const char* name,
93 ConnectionEventRecorder& connectionEventRecorder,
94 nsecs_t expectedTimestamp, unsigned expectedCount);
95 void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080096 void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070097 bool expectedConnected);
Ady Abraham447052e2019-02-13 16:07:27 -080098 void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -070099 int32_t expectedConfigId,
100 nsecs_t expectedVsyncPeriod);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700101 void expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t);
Ady Abraham62f216c2020-10-13 19:07:23 -0700102 void expectUidFrameRateMappingEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
103 std::vector<FrameRateOverride>);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800104
105 AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder;
106 AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder;
Ady Abraham9c53ee72020-07-22 21:16:18 -0700107 AsyncCallRecorder<void (*)(std::chrono::nanoseconds, std::chrono::nanoseconds)>
108 mVSyncSetDurationCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800109 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
110 AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700111 AsyncCallRecorder<void (*)(nsecs_t, uid_t)> mThrottleVsyncCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800112 ConnectionEventRecorder mConnectionEventCallRecorder{0};
Ady Abraham0bb6a472020-10-12 10:22:13 -0700113 ConnectionEventRecorder mThrottledConnectionEventCallRecorder{0};
Lloyd Pique24b0a482018-03-09 18:52:26 -0800114
Dominik Laskowski6505f792019-09-18 11:10:05 -0700115 MockVSyncSource* mVSyncSource;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800116 VSyncSource::Callback* mCallback = nullptr;
Dominik Laskowski6505f792019-09-18 11:10:05 -0700117 std::unique_ptr<impl::EventThread> mThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800118 sp<MockEventThreadConnection> mConnection;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700119 sp<MockEventThreadConnection> mThrottledConnection;
120
121 static constexpr uid_t mConnectionUid = 443;
122 static constexpr uid_t mThrottledConnectionUid = 177;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800123};
124
125EventThreadTest::EventThreadTest() {
126 const ::testing::TestInfo* const test_info =
127 ::testing::UnitTest::GetInstance()->current_test_info();
128 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
129
Dominik Laskowski6505f792019-09-18 11:10:05 -0700130 auto vsyncSource = std::make_unique<MockVSyncSource>();
131 mVSyncSource = vsyncSource.get();
132
133 EXPECT_CALL(*mVSyncSource, setVSyncEnabled(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800134 .WillRepeatedly(Invoke(mVSyncSetEnabledCallRecorder.getInvocable()));
135
Dominik Laskowski6505f792019-09-18 11:10:05 -0700136 EXPECT_CALL(*mVSyncSource, setCallback(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800137 .WillRepeatedly(Invoke(mVSyncSetCallbackCallRecorder.getInvocable()));
138
Ady Abraham9c53ee72020-07-22 21:16:18 -0700139 EXPECT_CALL(*mVSyncSource, setDuration(_, _))
140 .WillRepeatedly(Invoke(mVSyncSetDurationCallRecorder.getInvocable()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800141
Dominik Laskowski6505f792019-09-18 11:10:05 -0700142 createThread(std::move(vsyncSource));
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700143 mConnection = createConnection(mConnectionEventCallRecorder,
Ady Abraham62f216c2020-10-13 19:07:23 -0700144 ISurfaceComposer::EventRegistration::configChanged |
145 ISurfaceComposer::EventRegistration::frameRateOverride);
146 mThrottledConnection = createConnection(mThrottledConnectionEventCallRecorder,
147 ISurfaceComposer::EventRegistration::configChanged,
148 mThrottledConnectionUid);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800149
150 // A display must be connected for VSYNC events to be delivered.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800151 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
152 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800153}
154
155EventThreadTest::~EventThreadTest() {
156 const ::testing::TestInfo* const test_info =
157 ::testing::UnitTest::GetInstance()->current_test_info();
158 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800159
160 // EventThread should unregister itself as VSyncSource callback.
Lloyd Pique0655b8e2019-01-31 18:20:27 -0800161 EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800162}
163
Dominik Laskowski6505f792019-09-18 11:10:05 -0700164void EventThreadTest::createThread(std::unique_ptr<VSyncSource> source) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700165 const auto throttleVsync = [&](nsecs_t expectedVsyncTimestamp, uid_t uid) {
166 mThrottleVsyncCallRecorder.getInvocable()(expectedVsyncTimestamp, uid);
167 return (uid == mThrottledConnectionUid);
168 };
169
Dominik Laskowski6505f792019-09-18 11:10:05 -0700170 mThread = std::make_unique<impl::EventThread>(std::move(source),
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700171 /*tokenManager=*/nullptr,
Ady Abraham0bb6a472020-10-12 10:22:13 -0700172 mInterceptVSyncCallRecorder.getInvocable(),
173 throttleVsync);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800174
175 // EventThread should register itself as VSyncSource callback.
176 mCallback = expectVSyncSetCallbackCallReceived();
177 ASSERT_TRUE(mCallback);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800178}
179
180sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
Ady Abraham62f216c2020-10-13 19:07:23 -0700181 ConnectionEventRecorder& recorder,
182 ISurfaceComposer::EventRegistrationFlags eventRegistration, uid_t ownerUid) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800183 sp<MockEventThreadConnection> connection =
Ady Abraham0bb6a472020-10-12 10:22:13 -0700184 new MockEventThreadConnection(mThread.get(), ownerUid,
Ady Abraham62f216c2020-10-13 19:07:23 -0700185 mResyncCallRecorder.getInvocable(), eventRegistration);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800186 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
187 return connection;
188}
189
190void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) {
191 auto args = mVSyncSetEnabledCallRecorder.waitForCall();
192 ASSERT_TRUE(args.has_value());
193 EXPECT_EQ(expectedState, std::get<0>(args.value()));
194}
195
Ady Abraham9c53ee72020-07-22 21:16:18 -0700196void EventThreadTest::expectVSyncSetDurationCallReceived(
197 std::chrono::nanoseconds expectedDuration, std::chrono::nanoseconds expectedReadyDuration) {
198 auto args = mVSyncSetDurationCallRecorder.waitForCall();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800199 ASSERT_TRUE(args.has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700200 EXPECT_EQ(expectedDuration, std::get<0>(args.value()));
201 EXPECT_EQ(expectedReadyDuration, std::get<1>(args.value()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800202}
203
204VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() {
205 auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall();
206 return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr;
207}
208
209void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) {
210 auto args = mInterceptVSyncCallRecorder.waitForCall();
211 ASSERT_TRUE(args.has_value());
212 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
213}
214
Ady Abraham0bb6a472020-10-12 10:22:13 -0700215void EventThreadTest::expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t uid) {
216 auto args = mThrottleVsyncCallRecorder.waitForCall();
217 ASSERT_TRUE(args.has_value());
218 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
219 EXPECT_EQ(uid, std::get<1>(args.value()));
220}
221
Lloyd Pique24b0a482018-03-09 18:52:26 -0800222void EventThreadTest::expectVsyncEventReceivedByConnection(
223 const char* name, ConnectionEventRecorder& connectionEventRecorder,
224 nsecs_t expectedTimestamp, unsigned expectedCount) {
225 auto args = connectionEventRecorder.waitForCall();
226 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
227 << expectedTimestamp;
228 const auto& event = std::get<0>(args.value());
229 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
230 << name << " did not get the correct event for timestamp " << expectedTimestamp;
231 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
232 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
233 EXPECT_EQ(expectedCount, event.vsync.count)
234 << name << " did not get the expected count for timestamp " << expectedTimestamp;
235}
236
237void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
238 unsigned expectedCount) {
239 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
240 mConnectionEventCallRecorder, expectedTimestamp,
241 expectedCount);
242}
243
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800244void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
245 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800246 auto args = mConnectionEventCallRecorder.waitForCall();
247 ASSERT_TRUE(args.has_value());
248 const auto& event = std::get<0>(args.value());
249 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800250 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800251 EXPECT_EQ(expectedConnected, event.hotplug.connected);
252}
253
Ady Abraham447052e2019-02-13 16:07:27 -0800254void EventThreadTest::expectConfigChangedEventReceivedByConnection(
Alec Mouri60aee1c2019-10-28 16:18:59 -0700255 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId,
256 nsecs_t expectedVsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800257 auto args = mConnectionEventCallRecorder.waitForCall();
258 ASSERT_TRUE(args.has_value());
259 const auto& event = std::get<0>(args.value());
260 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED, event.header.type);
261 EXPECT_EQ(expectedDisplayId, event.header.displayId);
262 EXPECT_EQ(expectedConfigId, event.config.configId);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700263 EXPECT_EQ(expectedVsyncPeriod, event.config.vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800264}
265
Ady Abraham62f216c2020-10-13 19:07:23 -0700266void EventThreadTest::expectUidFrameRateMappingEventReceivedByConnection(
267 PhysicalDisplayId expectedDisplayId, std::vector<FrameRateOverride> expectedOverrides) {
268 for (const auto [uid, frameRateHz] : expectedOverrides) {
269 auto args = mConnectionEventCallRecorder.waitForCall();
270 ASSERT_TRUE(args.has_value());
271 const auto& event = std::get<0>(args.value());
272 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE, event.header.type);
273 EXPECT_EQ(expectedDisplayId, event.header.displayId);
274 EXPECT_EQ(uid, event.frameRateOverride.uid);
275 EXPECT_EQ(frameRateHz, event.frameRateOverride.frameRateHz);
276 }
277
278 auto args = mConnectionEventCallRecorder.waitForCall();
279 ASSERT_TRUE(args.has_value());
280 const auto& event = std::get<0>(args.value());
281 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH, event.header.type);
282 EXPECT_EQ(expectedDisplayId, event.header.displayId);
283}
284
Lloyd Pique24b0a482018-03-09 18:52:26 -0800285namespace {
286
287/* ------------------------------------------------------------------------
288 * Test cases
289 */
290
291TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
292 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
293 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700294 EXPECT_FALSE(mVSyncSetDurationCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800295 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
296 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value());
297 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
298}
299
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800300TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800301 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
302 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800303
304 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700305 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800306
307 // EventThread should not enable vsync callbacks.
308 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800309}
310
Lloyd Pique24b0a482018-03-09 18:52:26 -0800311TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
312 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700313 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800314
Ady Abraham8532d012019-05-08 14:50:56 -0700315 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800316 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
317
Dominik Laskowski029cc122019-01-23 19:52:06 -0800318 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800319 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800320
321 // Use the received callback to signal a first vsync event.
322 // The interceptor should receive the event, as well as the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700323 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800324 expectInterceptCallReceived(123);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700325 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800326 expectVsyncEventReceivedByConnection(123, 1u);
327
328 // Use the received callback to signal a second vsync event.
Ady Abraham0bb6a472020-10-12 10:22:13 -0700329 // The interceptor should receive the event, but the connection should
Lloyd Pique24b0a482018-03-09 18:52:26 -0800330 // not as it was only interested in the first.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700331 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800332 expectInterceptCallReceived(456);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700333 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800334 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
335
336 // EventThread should also detect that at this point that it does not need
337 // any more vsync events, and should disable their generation.
338 expectVSyncSetEnabledCallReceived(false);
339}
340
341TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
342 // Create a first connection, register it, and request a vsync rate of zero.
343 ConnectionEventRecorder firstConnectionEventRecorder{0};
Ady Abraham62f216c2020-10-13 19:07:23 -0700344 sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800345 mThread->setVsyncRate(0, firstConnection);
346
347 // By itself, this should not enable vsync events
348 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
349 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
350
351 // However if there is another connection which wants events at a nonzero rate.....
352 ConnectionEventRecorder secondConnectionEventRecorder{0};
353 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700354 createConnection(secondConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800355 mThread->setVsyncRate(1, secondConnection);
356
Dominik Laskowski029cc122019-01-23 19:52:06 -0800357 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800358 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800359
360 // Send a vsync event. EventThread should then make a call to the
361 // interceptor, and the second connection. The first connection should not
362 // get the event.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700363 mCallback->onVSyncEvent(123, 456, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800364 expectInterceptCallReceived(123);
365 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
366 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
367 1u);
368}
369
370TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
371 mThread->setVsyncRate(1, mConnection);
372
Dominik Laskowski029cc122019-01-23 19:52:06 -0800373 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800374 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800375
376 // Send a vsync event. EventThread should then make a call to the
377 // interceptor, and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700378 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800379 expectInterceptCallReceived(123);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700380 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800381 expectVsyncEventReceivedByConnection(123, 1u);
382
383 // A second event should go to the same places.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700384 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800385 expectInterceptCallReceived(456);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700386 expectThrottleVsyncReceived(123, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800387 expectVsyncEventReceivedByConnection(456, 2u);
388
389 // A third event should go to the same places.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700390 mCallback->onVSyncEvent(789, 777, 111);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800391 expectInterceptCallReceived(789);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700392 expectThrottleVsyncReceived(777, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800393 expectVsyncEventReceivedByConnection(789, 3u);
394}
395
396TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
397 mThread->setVsyncRate(2, mConnection);
398
Dominik Laskowski029cc122019-01-23 19:52:06 -0800399 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800400 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800401
402 // The first event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700403 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800404 expectInterceptCallReceived(123);
405 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700406 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800407
408 // The second event will be seen by the interceptor and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700409 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800410 expectInterceptCallReceived(456);
411 expectVsyncEventReceivedByConnection(456, 2u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700412 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800413
414 // The third event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700415 mCallback->onVSyncEvent(789, 777, 744);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800416 expectInterceptCallReceived(789);
417 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700418 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800419
420 // The fourth event will be seen by the interceptor and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700421 mCallback->onVSyncEvent(101112, 7847, 86);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800422 expectInterceptCallReceived(101112);
423 expectVsyncEventReceivedByConnection(101112, 4u);
424}
425
426TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
427 mThread->setVsyncRate(1, mConnection);
428
Dominik Laskowski029cc122019-01-23 19:52:06 -0800429 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800430 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800431
432 // Destroy the only (strong) reference to the connection.
433 mConnection = nullptr;
434
435 // The first event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700436 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800437 expectInterceptCallReceived(123);
438 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
439
440 // EventThread should disable vsync callbacks
441 expectVSyncSetEnabledCallReceived(false);
442}
443
444TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
445 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700446 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800447 mThread->setVsyncRate(1, errorConnection);
448
Dominik Laskowski029cc122019-01-23 19:52:06 -0800449 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800450 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800451
452 // The first event will be seen by the interceptor, and by the connection,
453 // which then returns an error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700454 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800455 expectInterceptCallReceived(123);
456 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
457
458 // A subsequent event will be seen by the interceptor and not by the
459 // connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700460 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800461 expectInterceptCallReceived(456);
462 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
463
464 // EventThread should disable vsync callbacks with the second event
465 expectVSyncSetEnabledCallReceived(false);
466}
467
Alec Mouri717bcb62020-02-10 17:07:19 -0800468TEST_F(EventThreadTest, tracksEventConnections) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700469 EXPECT_EQ(2, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800470 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700471 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800472 mThread->setVsyncRate(1, errorConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700473 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800474 ConnectionEventRecorder secondConnectionEventRecorder{0};
475 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700476 createConnection(secondConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800477 mThread->setVsyncRate(1, secondConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700478 EXPECT_EQ(4, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800479
480 // EventThread should enable vsync callbacks.
481 expectVSyncSetEnabledCallReceived(true);
482
483 // The first event will be seen by the interceptor, and by the connection,
484 // which then returns an error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700485 mCallback->onVSyncEvent(123, 456, 789);
Alec Mouri717bcb62020-02-10 17:07:19 -0800486 expectInterceptCallReceived(123);
487 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
488 expectVsyncEventReceivedByConnection("successConnection", secondConnectionEventRecorder, 123,
489 1u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700490 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800491}
492
Lloyd Pique24b0a482018-03-09 18:52:26 -0800493TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
494 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham62f216c2020-10-13 19:07:23 -0700495 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800496 mThread->setVsyncRate(1, errorConnection);
497
Dominik Laskowski029cc122019-01-23 19:52:06 -0800498 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800499 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800500
501 // The first event will be seen by the interceptor, and by the connection,
502 // which then returns an non-fatal error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700503 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800504 expectInterceptCallReceived(123);
505 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
506
507 // A subsequent event will be seen by the interceptor, and by the connection,
508 // which still then returns an non-fatal error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700509 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800510 expectInterceptCallReceived(456);
511 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
512
513 // EventThread will not disable vsync callbacks as the errors are non-fatal.
514 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
515}
516
517TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700518 mThread->setDuration(321ns, 456ns);
519 expectVSyncSetDurationCallReceived(321ns, 456ns);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800520}
521
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800522TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
523 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
524 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800525}
526
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800527TEST_F(EventThreadTest, postHotplugInternalConnect) {
528 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
529 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800530}
531
532TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800533 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
534 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800535}
536
537TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800538 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
539 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800540}
541
Ady Abraham447052e2019-02-13 16:07:27 -0800542TEST_F(EventThreadTest, postConfigChangedPrimary) {
Marin Shalamanov23c44202020-12-22 19:09:20 +0100543 mThread->onConfigChanged(INTERNAL_DISPLAY_ID, DisplayModeId(7), 16666666);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700544 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800545}
546
547TEST_F(EventThreadTest, postConfigChangedExternal) {
Marin Shalamanov23c44202020-12-22 19:09:20 +0100548 mThread->onConfigChanged(EXTERNAL_DISPLAY_ID, DisplayModeId(5), 16666666);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700549 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800550}
551
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700552TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
Marin Shalamanov23c44202020-12-22 19:09:20 +0100553 mThread->onConfigChanged(DISPLAY_ID_64BIT, DisplayModeId(7), 16666666);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700554 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, 16666666);
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700555}
556
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700557TEST_F(EventThreadTest, suppressConfigChanged) {
558 ConnectionEventRecorder suppressConnectionEventRecorder{0};
559 sp<MockEventThreadConnection> suppressConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700560 createConnection(suppressConnectionEventRecorder);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700561
Marin Shalamanov23c44202020-12-22 19:09:20 +0100562 mThread->onConfigChanged(INTERNAL_DISPLAY_ID, DisplayModeId(9), 16666666);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700563 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, 16666666);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700564
565 auto args = suppressConnectionEventRecorder.waitForCall();
566 ASSERT_FALSE(args.has_value());
567}
568
Ady Abraham62f216c2020-10-13 19:07:23 -0700569TEST_F(EventThreadTest, postUidFrameRateMapping) {
570 const std::vector<FrameRateOverride> overrides = {
571 {.uid = 1, .frameRateHz = 20},
572 {.uid = 3, .frameRateHz = 40},
573 {.uid = 5, .frameRateHz = 60},
574 };
575
576 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
577 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
578}
579
580TEST_F(EventThreadTest, suppressUidFrameRateMapping) {
581 const std::vector<FrameRateOverride> overrides = {
582 {.uid = 1, .frameRateHz = 20},
583 {.uid = 3, .frameRateHz = 40},
584 {.uid = 5, .frameRateHz = 60},
585 };
586
587 ConnectionEventRecorder suppressConnectionEventRecorder{0};
588 sp<MockEventThreadConnection> suppressConnection =
589 createConnection(suppressConnectionEventRecorder);
590
591 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
592 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
593
594 auto args = suppressConnectionEventRecorder.waitForCall();
595 ASSERT_FALSE(args.has_value());
596}
597
Ady Abraham0bb6a472020-10-12 10:22:13 -0700598TEST_F(EventThreadTest, requestNextVsyncWithThrottleVsyncDoesntPostVSync) {
599 // Signal that we want the next vsync event to be posted to the throttled connection
600 mThread->requestNextVsync(mThrottledConnection);
601
602 // EventThread should immediately request a resync.
603 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
604
605 // EventThread should enable vsync callbacks.
606 expectVSyncSetEnabledCallReceived(true);
607
608 // Use the received callback to signal a first vsync event.
609 // The interceptor should receive the event, but not the connection.
610 mCallback->onVSyncEvent(123, 456, 789);
611 expectInterceptCallReceived(123);
612 expectThrottleVsyncReceived(456, mThrottledConnectionUid);
613 mThrottledConnectionEventCallRecorder.waitForUnexpectedCall();
614
615 // Use the received callback to signal a second vsync event.
616 // The interceptor should receive the event, but the connection should
617 // not as it was only interested in the first.
618 mCallback->onVSyncEvent(456, 123, 0);
619 expectInterceptCallReceived(456);
620 expectThrottleVsyncReceived(123, mThrottledConnectionUid);
621 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
622
623 // EventThread should not change the vsync state as it didn't send the event
624 // yet
625 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
626}
627
Lloyd Pique24b0a482018-03-09 18:52:26 -0800628} // namespace
629} // namespace android
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100630
631// TODO(b/129481165): remove the #pragma below and fix conversion issues
632#pragma clang diagnostic pop // ignored "-Wextra"