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