Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 1 | /* |
| 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 Krulec | fefcb58 | 2018-08-07 14:22:37 -0700 | [diff] [blame] | 28 | #include "Scheduler/EventThread.h" |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 29 | |
| 30 | using namespace std::chrono_literals; |
| 31 | using namespace std::placeholders; |
| 32 | |
| 33 | using testing::_; |
| 34 | using testing::Invoke; |
| 35 | |
| 36 | namespace android { |
| 37 | namespace { |
| 38 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 39 | constexpr PhysicalDisplayId INTERNAL_DISPLAY_ID = 111; |
| 40 | constexpr PhysicalDisplayId EXTERNAL_DISPLAY_ID = 222; |
Ady Abraham | af0ec27 | 2019-03-28 11:38:31 -0700 | [diff] [blame] | 41 | constexpr PhysicalDisplayId DISPLAY_ID_64BIT = 0xabcd12349876fedcULL; |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 42 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 43 | class MockVSyncSource : public VSyncSource { |
| 44 | public: |
| 45 | MOCK_METHOD1(setVSyncEnabled, void(bool)); |
| 46 | MOCK_METHOD1(setCallback, void(VSyncSource::Callback*)); |
| 47 | MOCK_METHOD1(setPhaseOffset, void(nsecs_t)); |
Ady Abraham | b838aed | 2019-02-12 15:30:16 -0800 | [diff] [blame] | 48 | MOCK_METHOD1(pauseVsyncCallback, void(bool)); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | } // namespace |
| 52 | |
| 53 | class EventThreadTest : public testing::Test { |
| 54 | protected: |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 55 | class MockEventThreadConnection : public EventThreadConnection { |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 56 | public: |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 57 | MockEventThreadConnection(android::impl::EventThread* eventThread, |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame^] | 58 | ResyncCallback&& resyncCallback, |
| 59 | ISurfaceComposer::ConfigChanged configChanged) |
| 60 | : EventThreadConnection(eventThread, std::move(resyncCallback), configChanged) {} |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 61 | MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event)); |
| 62 | }; |
| 63 | |
| 64 | using ConnectionEventRecorder = |
| 65 | AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>; |
| 66 | |
| 67 | EventThreadTest(); |
| 68 | ~EventThreadTest() override; |
| 69 | |
| 70 | void createThread(); |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame^] | 71 | sp<MockEventThreadConnection> createConnection(ConnectionEventRecorder& recorder, |
| 72 | ISurfaceComposer::ConfigChanged configChanged); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 73 | |
| 74 | void expectVSyncSetEnabledCallReceived(bool expectedState); |
| 75 | void expectVSyncSetPhaseOffsetCallReceived(nsecs_t expectedPhaseOffset); |
| 76 | VSyncSource::Callback* expectVSyncSetCallbackCallReceived(); |
| 77 | void expectInterceptCallReceived(nsecs_t expectedTimestamp); |
| 78 | void expectVsyncEventReceivedByConnection(const char* name, |
| 79 | ConnectionEventRecorder& connectionEventRecorder, |
| 80 | nsecs_t expectedTimestamp, unsigned expectedCount); |
| 81 | void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 82 | void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 83 | bool expectedConnected); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 84 | void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, |
| 85 | int32_t expectedConfigId); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 86 | |
| 87 | AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder; |
| 88 | AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder; |
| 89 | AsyncCallRecorder<void (*)(nsecs_t)> mVSyncSetPhaseOffsetCallRecorder; |
| 90 | AsyncCallRecorder<void (*)()> mResyncCallRecorder; |
| 91 | AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder; |
| 92 | ConnectionEventRecorder mConnectionEventCallRecorder{0}; |
| 93 | |
| 94 | MockVSyncSource mVSyncSource; |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 95 | VSyncSource::Callback* mCallback = nullptr; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 96 | std::unique_ptr<android::impl::EventThread> mThread; |
| 97 | sp<MockEventThreadConnection> mConnection; |
| 98 | }; |
| 99 | |
| 100 | EventThreadTest::EventThreadTest() { |
| 101 | const ::testing::TestInfo* const test_info = |
| 102 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 103 | ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 104 | |
| 105 | EXPECT_CALL(mVSyncSource, setVSyncEnabled(_)) |
| 106 | .WillRepeatedly(Invoke(mVSyncSetEnabledCallRecorder.getInvocable())); |
| 107 | |
| 108 | EXPECT_CALL(mVSyncSource, setCallback(_)) |
| 109 | .WillRepeatedly(Invoke(mVSyncSetCallbackCallRecorder.getInvocable())); |
| 110 | |
| 111 | EXPECT_CALL(mVSyncSource, setPhaseOffset(_)) |
| 112 | .WillRepeatedly(Invoke(mVSyncSetPhaseOffsetCallRecorder.getInvocable())); |
| 113 | |
| 114 | createThread(); |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame^] | 115 | mConnection = createConnection(mConnectionEventCallRecorder, |
| 116 | ISurfaceComposer::eConfigChangedDispatch); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 117 | |
| 118 | // A display must be connected for VSYNC events to be delivered. |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 119 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true); |
| 120 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | EventThreadTest::~EventThreadTest() { |
| 124 | const ::testing::TestInfo* const test_info = |
| 125 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 126 | ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 127 | |
| 128 | // EventThread should unregister itself as VSyncSource callback. |
Lloyd Pique | 0655b8e | 2019-01-31 18:20:27 -0800 | [diff] [blame] | 129 | EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void EventThreadTest::createThread() { |
| 133 | mThread = |
| 134 | std::make_unique<android::impl::EventThread>(&mVSyncSource, |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 135 | mInterceptVSyncCallRecorder.getInvocable(), |
| 136 | "unit-test-event-thread"); |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 137 | |
| 138 | // EventThread should register itself as VSyncSource callback. |
| 139 | mCallback = expectVSyncSetCallbackCallReceived(); |
| 140 | ASSERT_TRUE(mCallback); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection( |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame^] | 144 | ConnectionEventRecorder& recorder, ISurfaceComposer::ConfigChanged configChanged) { |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 145 | sp<MockEventThreadConnection> connection = |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame^] | 146 | new MockEventThreadConnection(mThread.get(), mResyncCallRecorder.getInvocable(), |
| 147 | configChanged); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 148 | EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable())); |
| 149 | return connection; |
| 150 | } |
| 151 | |
| 152 | void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) { |
| 153 | auto args = mVSyncSetEnabledCallRecorder.waitForCall(); |
| 154 | ASSERT_TRUE(args.has_value()); |
| 155 | EXPECT_EQ(expectedState, std::get<0>(args.value())); |
| 156 | } |
| 157 | |
| 158 | void EventThreadTest::expectVSyncSetPhaseOffsetCallReceived(nsecs_t expectedPhaseOffset) { |
| 159 | auto args = mVSyncSetPhaseOffsetCallRecorder.waitForCall(); |
| 160 | ASSERT_TRUE(args.has_value()); |
| 161 | EXPECT_EQ(expectedPhaseOffset, std::get<0>(args.value())); |
| 162 | } |
| 163 | |
| 164 | VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() { |
| 165 | auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall(); |
| 166 | return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr; |
| 167 | } |
| 168 | |
| 169 | void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) { |
| 170 | auto args = mInterceptVSyncCallRecorder.waitForCall(); |
| 171 | ASSERT_TRUE(args.has_value()); |
| 172 | EXPECT_EQ(expectedTimestamp, std::get<0>(args.value())); |
| 173 | } |
| 174 | |
| 175 | void EventThreadTest::expectVsyncEventReceivedByConnection( |
| 176 | const char* name, ConnectionEventRecorder& connectionEventRecorder, |
| 177 | nsecs_t expectedTimestamp, unsigned expectedCount) { |
| 178 | auto args = connectionEventRecorder.waitForCall(); |
| 179 | ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp " |
| 180 | << expectedTimestamp; |
| 181 | const auto& event = std::get<0>(args.value()); |
| 182 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type) |
| 183 | << name << " did not get the correct event for timestamp " << expectedTimestamp; |
| 184 | EXPECT_EQ(expectedTimestamp, event.header.timestamp) |
| 185 | << name << " did not get the expected timestamp for timestamp " << expectedTimestamp; |
| 186 | EXPECT_EQ(expectedCount, event.vsync.count) |
| 187 | << name << " did not get the expected count for timestamp " << expectedTimestamp; |
| 188 | } |
| 189 | |
| 190 | void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, |
| 191 | unsigned expectedCount) { |
| 192 | expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder", |
| 193 | mConnectionEventCallRecorder, expectedTimestamp, |
| 194 | expectedCount); |
| 195 | } |
| 196 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 197 | void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, |
| 198 | bool expectedConnected) { |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 199 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 200 | ASSERT_TRUE(args.has_value()); |
| 201 | const auto& event = std::get<0>(args.value()); |
| 202 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 203 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 204 | EXPECT_EQ(expectedConnected, event.hotplug.connected); |
| 205 | } |
| 206 | |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 207 | void EventThreadTest::expectConfigChangedEventReceivedByConnection( |
| 208 | PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId) { |
| 209 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 210 | ASSERT_TRUE(args.has_value()); |
| 211 | const auto& event = std::get<0>(args.value()); |
| 212 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED, event.header.type); |
| 213 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
| 214 | EXPECT_EQ(expectedConfigId, event.config.configId); |
| 215 | } |
| 216 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 217 | namespace { |
| 218 | |
| 219 | /* ------------------------------------------------------------------------ |
| 220 | * Test cases |
| 221 | */ |
| 222 | |
| 223 | TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) { |
| 224 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 225 | EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value()); |
| 226 | EXPECT_FALSE(mVSyncSetPhaseOffsetCallRecorder.waitForCall(0us).has_value()); |
| 227 | EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value()); |
| 228 | EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value()); |
| 229 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value()); |
| 230 | } |
| 231 | |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 232 | TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 233 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false); |
| 234 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 235 | |
| 236 | // Signal that we want the next vsync event to be posted to the connection. |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 237 | mThread->requestNextVsync(mConnection); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 238 | |
| 239 | // EventThread should not enable vsync callbacks. |
| 240 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 243 | TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) { |
| 244 | // Signal that we want the next vsync event to be posted to the connection |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 245 | mThread->requestNextVsync(mConnection); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 246 | |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 247 | // EventThread should immediately request a resync. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 248 | EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value()); |
| 249 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 250 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 251 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 252 | |
| 253 | // Use the received callback to signal a first vsync event. |
| 254 | // The interceptor should receive the event, as well as the connection. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 255 | mCallback->onVSyncEvent(123); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 256 | expectInterceptCallReceived(123); |
| 257 | expectVsyncEventReceivedByConnection(123, 1u); |
| 258 | |
| 259 | // Use the received callback to signal a second vsync event. |
| 260 | // The interceptor should receive the event, but the the connection should |
| 261 | // not as it was only interested in the first. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 262 | mCallback->onVSyncEvent(456); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 263 | expectInterceptCallReceived(456); |
| 264 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 265 | |
| 266 | // EventThread should also detect that at this point that it does not need |
| 267 | // any more vsync events, and should disable their generation. |
| 268 | expectVSyncSetEnabledCallReceived(false); |
| 269 | } |
| 270 | |
| 271 | TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) { |
| 272 | // Create a first connection, register it, and request a vsync rate of zero. |
| 273 | ConnectionEventRecorder firstConnectionEventRecorder{0}; |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame^] | 274 | sp<MockEventThreadConnection> firstConnection = |
| 275 | createConnection(firstConnectionEventRecorder, |
| 276 | ISurfaceComposer::eConfigChangedSuppress); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 277 | mThread->setVsyncRate(0, firstConnection); |
| 278 | |
| 279 | // By itself, this should not enable vsync events |
| 280 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 281 | EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value()); |
| 282 | |
| 283 | // However if there is another connection which wants events at a nonzero rate..... |
| 284 | ConnectionEventRecorder secondConnectionEventRecorder{0}; |
| 285 | sp<MockEventThreadConnection> secondConnection = |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame^] | 286 | createConnection(secondConnectionEventRecorder, |
| 287 | ISurfaceComposer::eConfigChangedSuppress); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 288 | mThread->setVsyncRate(1, secondConnection); |
| 289 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 290 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 291 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 292 | |
| 293 | // Send a vsync event. EventThread should then make a call to the |
| 294 | // interceptor, and the second connection. The first connection should not |
| 295 | // get the event. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 296 | mCallback->onVSyncEvent(123); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 297 | expectInterceptCallReceived(123); |
| 298 | EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value()); |
| 299 | expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123, |
| 300 | 1u); |
| 301 | } |
| 302 | |
| 303 | TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) { |
| 304 | mThread->setVsyncRate(1, mConnection); |
| 305 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 306 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 307 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 308 | |
| 309 | // Send a vsync event. EventThread should then make a call to the |
| 310 | // interceptor, and the connection. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 311 | mCallback->onVSyncEvent(123); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 312 | expectInterceptCallReceived(123); |
| 313 | expectVsyncEventReceivedByConnection(123, 1u); |
| 314 | |
| 315 | // A second event should go to the same places. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 316 | mCallback->onVSyncEvent(456); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 317 | expectInterceptCallReceived(456); |
| 318 | expectVsyncEventReceivedByConnection(456, 2u); |
| 319 | |
| 320 | // A third event should go to the same places. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 321 | mCallback->onVSyncEvent(789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 322 | expectInterceptCallReceived(789); |
| 323 | expectVsyncEventReceivedByConnection(789, 3u); |
| 324 | } |
| 325 | |
| 326 | TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) { |
| 327 | mThread->setVsyncRate(2, mConnection); |
| 328 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 329 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 330 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 331 | |
| 332 | // The first event will be seen by the interceptor, and not the connection. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 333 | mCallback->onVSyncEvent(123); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 334 | expectInterceptCallReceived(123); |
| 335 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 336 | |
| 337 | // The second event will be seen by the interceptor and the connection. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 338 | mCallback->onVSyncEvent(456); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 339 | expectInterceptCallReceived(456); |
| 340 | expectVsyncEventReceivedByConnection(456, 2u); |
| 341 | |
| 342 | // The third event will be seen by the interceptor, and not the connection. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 343 | mCallback->onVSyncEvent(789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 344 | expectInterceptCallReceived(789); |
| 345 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 346 | |
| 347 | // The fourth event will be seen by the interceptor and the connection. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 348 | mCallback->onVSyncEvent(101112); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 349 | expectInterceptCallReceived(101112); |
| 350 | expectVsyncEventReceivedByConnection(101112, 4u); |
| 351 | } |
| 352 | |
| 353 | TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) { |
| 354 | mThread->setVsyncRate(1, mConnection); |
| 355 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 356 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 357 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 358 | |
| 359 | // Destroy the only (strong) reference to the connection. |
| 360 | mConnection = nullptr; |
| 361 | |
| 362 | // The first event will be seen by the interceptor, and not the connection. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 363 | mCallback->onVSyncEvent(123); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 364 | expectInterceptCallReceived(123); |
| 365 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 366 | |
| 367 | // EventThread should disable vsync callbacks |
| 368 | expectVSyncSetEnabledCallReceived(false); |
| 369 | } |
| 370 | |
| 371 | TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) { |
| 372 | ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY}; |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame^] | 373 | sp<MockEventThreadConnection> errorConnection = |
| 374 | createConnection(errorConnectionEventRecorder, |
| 375 | ISurfaceComposer::eConfigChangedSuppress); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 376 | mThread->setVsyncRate(1, errorConnection); |
| 377 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 378 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 379 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 380 | |
| 381 | // The first event will be seen by the interceptor, and by the connection, |
| 382 | // which then returns an error. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 383 | mCallback->onVSyncEvent(123); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 384 | expectInterceptCallReceived(123); |
| 385 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u); |
| 386 | |
| 387 | // A subsequent event will be seen by the interceptor and not by the |
| 388 | // connection. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 389 | mCallback->onVSyncEvent(456); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 390 | expectInterceptCallReceived(456); |
| 391 | EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value()); |
| 392 | |
| 393 | // EventThread should disable vsync callbacks with the second event |
| 394 | expectVSyncSetEnabledCallReceived(false); |
| 395 | } |
| 396 | |
| 397 | TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) { |
| 398 | ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK}; |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame^] | 399 | sp<MockEventThreadConnection> errorConnection = |
| 400 | createConnection(errorConnectionEventRecorder, |
| 401 | ISurfaceComposer::eConfigChangedSuppress); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 402 | mThread->setVsyncRate(1, errorConnection); |
| 403 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 404 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 405 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 406 | |
| 407 | // The first event will be seen by the interceptor, and by the connection, |
| 408 | // which then returns an non-fatal error. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 409 | mCallback->onVSyncEvent(123); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 410 | expectInterceptCallReceived(123); |
| 411 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u); |
| 412 | |
| 413 | // A subsequent event will be seen by the interceptor, and by the connection, |
| 414 | // which still then returns an non-fatal error. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 415 | mCallback->onVSyncEvent(456); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 416 | expectInterceptCallReceived(456); |
| 417 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u); |
| 418 | |
| 419 | // EventThread will not disable vsync callbacks as the errors are non-fatal. |
| 420 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 421 | } |
| 422 | |
| 423 | TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) { |
| 424 | mThread->setPhaseOffset(321); |
| 425 | expectVSyncSetPhaseOffsetCallReceived(321); |
| 426 | } |
| 427 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 428 | TEST_F(EventThreadTest, postHotplugInternalDisconnect) { |
| 429 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false); |
| 430 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 431 | } |
| 432 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 433 | TEST_F(EventThreadTest, postHotplugInternalConnect) { |
| 434 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true); |
| 435 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | TEST_F(EventThreadTest, postHotplugExternalDisconnect) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 439 | mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false); |
| 440 | expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | TEST_F(EventThreadTest, postHotplugExternalConnect) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 444 | mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true); |
| 445 | expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 448 | TEST_F(EventThreadTest, postConfigChangedPrimary) { |
| 449 | mThread->onConfigChanged(INTERNAL_DISPLAY_ID, 7); |
| 450 | expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7); |
| 451 | } |
| 452 | |
| 453 | TEST_F(EventThreadTest, postConfigChangedExternal) { |
| 454 | mThread->onConfigChanged(EXTERNAL_DISPLAY_ID, 5); |
| 455 | expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5); |
| 456 | } |
| 457 | |
Ady Abraham | af0ec27 | 2019-03-28 11:38:31 -0700 | [diff] [blame] | 458 | TEST_F(EventThreadTest, postConfigChangedPrimary64bit) { |
| 459 | mThread->onConfigChanged(DISPLAY_ID_64BIT, 7); |
| 460 | expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7); |
| 461 | } |
| 462 | |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame^] | 463 | TEST_F(EventThreadTest, suppressConfigChanged) { |
| 464 | ConnectionEventRecorder suppressConnectionEventRecorder{0}; |
| 465 | sp<MockEventThreadConnection> suppressConnection = |
| 466 | createConnection(suppressConnectionEventRecorder, |
| 467 | ISurfaceComposer::eConfigChangedSuppress); |
| 468 | |
| 469 | mThread->onConfigChanged(INTERNAL_DISPLAY_ID, 9); |
| 470 | expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9); |
| 471 | |
| 472 | auto args = suppressConnectionEventRecorder.waitForCall(); |
| 473 | ASSERT_FALSE(args.has_value()); |
| 474 | } |
| 475 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 476 | } // namespace |
| 477 | } // namespace android |