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