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