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