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 | |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wextra" |
| 20 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 21 | #undef LOG_TAG |
| 22 | #define LOG_TAG "LibSurfaceFlingerUnittests" |
| 23 | |
| 24 | #include <gmock/gmock.h> |
| 25 | #include <gtest/gtest.h> |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 26 | #include <log/log.h> |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 27 | #include <utils/Errors.h> |
| 28 | |
| 29 | #include "AsyncCallRecorder.h" |
Marin Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 30 | #include "DisplayHardware/DisplayMode.h" |
Ana Krulec | fefcb58 | 2018-08-07 14:22:37 -0700 | [diff] [blame] | 31 | #include "Scheduler/EventThread.h" |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 32 | |
| 33 | using namespace std::chrono_literals; |
| 34 | using namespace std::placeholders; |
| 35 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 36 | using namespace android::flag_operators; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 37 | using testing::_; |
| 38 | using testing::Invoke; |
| 39 | |
| 40 | namespace android { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 41 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 42 | namespace { |
| 43 | |
Marin Shalamanov | a524a09 | 2020-07-27 21:39:55 +0200 | [diff] [blame] | 44 | constexpr PhysicalDisplayId INTERNAL_DISPLAY_ID(111); |
| 45 | constexpr PhysicalDisplayId EXTERNAL_DISPLAY_ID(222); |
| 46 | constexpr PhysicalDisplayId DISPLAY_ID_64BIT(0xabcd12349876fedcULL); |
Jorim Jaggi | c0086af | 2021-02-12 18:18:11 +0100 | [diff] [blame] | 47 | constexpr std::chrono::duration VSYNC_PERIOD(16ms); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 48 | class MockVSyncSource : public VSyncSource { |
| 49 | public: |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 50 | const char* getName() const override { return "test"; } |
| 51 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 52 | MOCK_METHOD1(setVSyncEnabled, void(bool)); |
| 53 | MOCK_METHOD1(setCallback, void(VSyncSource::Callback*)); |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 54 | MOCK_METHOD2(setDuration, |
| 55 | void(std::chrono::nanoseconds workDuration, |
| 56 | std::chrono::nanoseconds readyDuration)); |
Ady Abraham | b838aed | 2019-02-12 15:30:16 -0800 | [diff] [blame] | 57 | MOCK_METHOD1(pauseVsyncCallback, void(bool)); |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 58 | MOCK_CONST_METHOD1(dump, void(std::string&)); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | } // namespace |
| 62 | |
| 63 | class EventThreadTest : public testing::Test { |
| 64 | protected: |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 65 | class MockEventThreadConnection : public EventThreadConnection { |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 66 | public: |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 67 | MockEventThreadConnection(impl::EventThread* eventThread, uid_t callingUid, |
| 68 | ResyncCallback&& resyncCallback, |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 69 | ISurfaceComposer::EventRegistrationFlags eventRegistration) |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 70 | : EventThreadConnection(eventThread, callingUid, std::move(resyncCallback), |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 71 | eventRegistration) {} |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 72 | MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event)); |
| 73 | }; |
| 74 | |
| 75 | using ConnectionEventRecorder = |
| 76 | AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>; |
| 77 | |
| 78 | EventThreadTest(); |
| 79 | ~EventThreadTest() override; |
| 80 | |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 81 | void createThread(std::unique_ptr<VSyncSource>); |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 82 | sp<MockEventThreadConnection> createConnection( |
| 83 | ConnectionEventRecorder& recorder, |
| 84 | ISurfaceComposer::EventRegistrationFlags eventRegistration = {}, |
| 85 | uid_t ownerUid = mConnectionUid); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 86 | |
| 87 | void expectVSyncSetEnabledCallReceived(bool expectedState); |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 88 | void expectVSyncSetDurationCallReceived(std::chrono::nanoseconds expectedDuration, |
| 89 | std::chrono::nanoseconds expectedReadyDuration); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 90 | VSyncSource::Callback* expectVSyncSetCallbackCallReceived(); |
| 91 | void expectInterceptCallReceived(nsecs_t expectedTimestamp); |
| 92 | void expectVsyncEventReceivedByConnection(const char* name, |
| 93 | ConnectionEventRecorder& connectionEventRecorder, |
| 94 | nsecs_t expectedTimestamp, unsigned expectedCount); |
| 95 | void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 96 | void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 97 | bool expectedConnected); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 98 | void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 99 | int32_t expectedConfigId, |
| 100 | nsecs_t expectedVsyncPeriod); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 101 | void expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t); |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 102 | void expectUidFrameRateMappingEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, |
| 103 | std::vector<FrameRateOverride>); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 104 | |
| 105 | AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder; |
| 106 | AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder; |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 107 | AsyncCallRecorder<void (*)(std::chrono::nanoseconds, std::chrono::nanoseconds)> |
| 108 | mVSyncSetDurationCallRecorder; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 109 | AsyncCallRecorder<void (*)()> mResyncCallRecorder; |
| 110 | AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 111 | AsyncCallRecorder<void (*)(nsecs_t, uid_t)> mThrottleVsyncCallRecorder; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 112 | ConnectionEventRecorder mConnectionEventCallRecorder{0}; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 113 | ConnectionEventRecorder mThrottledConnectionEventCallRecorder{0}; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 114 | |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 115 | MockVSyncSource* mVSyncSource; |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 116 | VSyncSource::Callback* mCallback = nullptr; |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 117 | std::unique_ptr<impl::EventThread> mThread; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 118 | sp<MockEventThreadConnection> mConnection; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 119 | sp<MockEventThreadConnection> mThrottledConnection; |
| 120 | |
| 121 | static constexpr uid_t mConnectionUid = 443; |
| 122 | static constexpr uid_t mThrottledConnectionUid = 177; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | EventThreadTest::EventThreadTest() { |
| 126 | const ::testing::TestInfo* const test_info = |
| 127 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 128 | ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 129 | |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 130 | auto vsyncSource = std::make_unique<MockVSyncSource>(); |
| 131 | mVSyncSource = vsyncSource.get(); |
| 132 | |
| 133 | EXPECT_CALL(*mVSyncSource, setVSyncEnabled(_)) |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 134 | .WillRepeatedly(Invoke(mVSyncSetEnabledCallRecorder.getInvocable())); |
| 135 | |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 136 | EXPECT_CALL(*mVSyncSource, setCallback(_)) |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 137 | .WillRepeatedly(Invoke(mVSyncSetCallbackCallRecorder.getInvocable())); |
| 138 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 139 | EXPECT_CALL(*mVSyncSource, setDuration(_, _)) |
| 140 | .WillRepeatedly(Invoke(mVSyncSetDurationCallRecorder.getInvocable())); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 141 | |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 142 | createThread(std::move(vsyncSource)); |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 143 | mConnection = createConnection(mConnectionEventCallRecorder, |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 144 | ISurfaceComposer::EventRegistration::modeChanged | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 145 | ISurfaceComposer::EventRegistration::frameRateOverride); |
| 146 | mThrottledConnection = createConnection(mThrottledConnectionEventCallRecorder, |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 147 | ISurfaceComposer::EventRegistration::modeChanged, |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 148 | mThrottledConnectionUid); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 149 | |
| 150 | // A display must be connected for VSYNC events to be delivered. |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 151 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true); |
| 152 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | EventThreadTest::~EventThreadTest() { |
| 156 | const ::testing::TestInfo* const test_info = |
| 157 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 158 | 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] | 159 | |
| 160 | // EventThread should unregister itself as VSyncSource callback. |
Lloyd Pique | 0655b8e | 2019-01-31 18:20:27 -0800 | [diff] [blame] | 161 | EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 164 | void EventThreadTest::createThread(std::unique_ptr<VSyncSource> source) { |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 165 | const auto throttleVsync = [&](nsecs_t expectedVsyncTimestamp, uid_t uid) { |
| 166 | mThrottleVsyncCallRecorder.getInvocable()(expectedVsyncTimestamp, uid); |
| 167 | return (uid == mThrottledConnectionUid); |
| 168 | }; |
Jorim Jaggi | c0086af | 2021-02-12 18:18:11 +0100 | [diff] [blame] | 169 | const auto getVsyncPeriod = [](uid_t uid) { |
| 170 | return VSYNC_PERIOD.count(); |
| 171 | }; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 172 | |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 173 | mThread = std::make_unique<impl::EventThread>(std::move(source), |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 174 | /*tokenManager=*/nullptr, |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 175 | mInterceptVSyncCallRecorder.getInvocable(), |
Jorim Jaggi | c0086af | 2021-02-12 18:18:11 +0100 | [diff] [blame] | 176 | throttleVsync, getVsyncPeriod); |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 177 | |
| 178 | // EventThread should register itself as VSyncSource callback. |
| 179 | mCallback = expectVSyncSetCallbackCallReceived(); |
| 180 | ASSERT_TRUE(mCallback); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection( |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 184 | ConnectionEventRecorder& recorder, |
| 185 | ISurfaceComposer::EventRegistrationFlags eventRegistration, uid_t ownerUid) { |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 186 | sp<MockEventThreadConnection> connection = |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 187 | new MockEventThreadConnection(mThread.get(), ownerUid, |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 188 | mResyncCallRecorder.getInvocable(), eventRegistration); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 189 | EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable())); |
| 190 | return connection; |
| 191 | } |
| 192 | |
| 193 | void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) { |
| 194 | auto args = mVSyncSetEnabledCallRecorder.waitForCall(); |
| 195 | ASSERT_TRUE(args.has_value()); |
| 196 | EXPECT_EQ(expectedState, std::get<0>(args.value())); |
| 197 | } |
| 198 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 199 | void EventThreadTest::expectVSyncSetDurationCallReceived( |
| 200 | std::chrono::nanoseconds expectedDuration, std::chrono::nanoseconds expectedReadyDuration) { |
| 201 | auto args = mVSyncSetDurationCallRecorder.waitForCall(); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 202 | ASSERT_TRUE(args.has_value()); |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 203 | EXPECT_EQ(expectedDuration, std::get<0>(args.value())); |
| 204 | EXPECT_EQ(expectedReadyDuration, std::get<1>(args.value())); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() { |
| 208 | auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall(); |
| 209 | return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr; |
| 210 | } |
| 211 | |
| 212 | void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) { |
| 213 | auto args = mInterceptVSyncCallRecorder.waitForCall(); |
| 214 | ASSERT_TRUE(args.has_value()); |
| 215 | EXPECT_EQ(expectedTimestamp, std::get<0>(args.value())); |
| 216 | } |
| 217 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 218 | void EventThreadTest::expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t uid) { |
| 219 | auto args = mThrottleVsyncCallRecorder.waitForCall(); |
| 220 | ASSERT_TRUE(args.has_value()); |
| 221 | EXPECT_EQ(expectedTimestamp, std::get<0>(args.value())); |
| 222 | EXPECT_EQ(uid, std::get<1>(args.value())); |
| 223 | } |
| 224 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 225 | void EventThreadTest::expectVsyncEventReceivedByConnection( |
| 226 | const char* name, ConnectionEventRecorder& connectionEventRecorder, |
| 227 | nsecs_t expectedTimestamp, unsigned expectedCount) { |
| 228 | auto args = connectionEventRecorder.waitForCall(); |
| 229 | ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp " |
| 230 | << expectedTimestamp; |
| 231 | const auto& event = std::get<0>(args.value()); |
| 232 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type) |
| 233 | << name << " did not get the correct event for timestamp " << expectedTimestamp; |
| 234 | EXPECT_EQ(expectedTimestamp, event.header.timestamp) |
| 235 | << name << " did not get the expected timestamp for timestamp " << expectedTimestamp; |
| 236 | EXPECT_EQ(expectedCount, event.vsync.count) |
| 237 | << name << " did not get the expected count for timestamp " << expectedTimestamp; |
| 238 | } |
| 239 | |
| 240 | void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, |
| 241 | unsigned expectedCount) { |
| 242 | expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder", |
| 243 | mConnectionEventCallRecorder, expectedTimestamp, |
| 244 | expectedCount); |
| 245 | } |
| 246 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 247 | void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, |
| 248 | bool expectedConnected) { |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 249 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 250 | ASSERT_TRUE(args.has_value()); |
| 251 | const auto& event = std::get<0>(args.value()); |
| 252 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 253 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 254 | EXPECT_EQ(expectedConnected, event.hotplug.connected); |
| 255 | } |
| 256 | |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 257 | void EventThreadTest::expectConfigChangedEventReceivedByConnection( |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 258 | PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId, |
| 259 | nsecs_t expectedVsyncPeriod) { |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 260 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 261 | ASSERT_TRUE(args.has_value()); |
| 262 | const auto& event = std::get<0>(args.value()); |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 263 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE, event.header.type); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 264 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 265 | EXPECT_EQ(expectedConfigId, event.modeChange.modeId); |
| 266 | EXPECT_EQ(expectedVsyncPeriod, event.modeChange.vsyncPeriod); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 269 | void EventThreadTest::expectUidFrameRateMappingEventReceivedByConnection( |
| 270 | PhysicalDisplayId expectedDisplayId, std::vector<FrameRateOverride> expectedOverrides) { |
| 271 | for (const auto [uid, frameRateHz] : expectedOverrides) { |
| 272 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 273 | ASSERT_TRUE(args.has_value()); |
| 274 | const auto& event = std::get<0>(args.value()); |
| 275 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE, event.header.type); |
| 276 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
| 277 | EXPECT_EQ(uid, event.frameRateOverride.uid); |
| 278 | EXPECT_EQ(frameRateHz, event.frameRateOverride.frameRateHz); |
| 279 | } |
| 280 | |
| 281 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 282 | ASSERT_TRUE(args.has_value()); |
| 283 | const auto& event = std::get<0>(args.value()); |
| 284 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH, event.header.type); |
| 285 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
| 286 | } |
| 287 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 288 | namespace { |
| 289 | |
| 290 | /* ------------------------------------------------------------------------ |
| 291 | * Test cases |
| 292 | */ |
| 293 | |
| 294 | TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) { |
| 295 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 296 | EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value()); |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 297 | EXPECT_FALSE(mVSyncSetDurationCallRecorder.waitForCall(0us).has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 298 | EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value()); |
| 299 | EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value()); |
| 300 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value()); |
| 301 | } |
| 302 | |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 303 | TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 304 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false); |
| 305 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 306 | |
| 307 | // 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] | 308 | mThread->requestNextVsync(mConnection); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 309 | |
| 310 | // EventThread should not enable vsync callbacks. |
| 311 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 314 | TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) { |
| 315 | // 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] | 316 | mThread->requestNextVsync(mConnection); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 317 | |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 318 | // EventThread should immediately request a resync. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 319 | EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value()); |
| 320 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 321 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 322 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 323 | |
| 324 | // Use the received callback to signal a first vsync event. |
| 325 | // The interceptor should receive the event, as well as the connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 326 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 327 | expectInterceptCallReceived(123); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 328 | expectThrottleVsyncReceived(456, mConnectionUid); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 329 | expectVsyncEventReceivedByConnection(123, 1u); |
| 330 | |
| 331 | // Use the received callback to signal a second vsync event. |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 332 | // The interceptor should receive the event, but the connection should |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 333 | // not as it was only interested in the first. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 334 | mCallback->onVSyncEvent(456, 123, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 335 | expectInterceptCallReceived(456); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 336 | EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 337 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 338 | |
| 339 | // EventThread should also detect that at this point that it does not need |
| 340 | // any more vsync events, and should disable their generation. |
| 341 | expectVSyncSetEnabledCallReceived(false); |
| 342 | } |
| 343 | |
| 344 | TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) { |
| 345 | // Create a first connection, register it, and request a vsync rate of zero. |
| 346 | ConnectionEventRecorder firstConnectionEventRecorder{0}; |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 347 | sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 348 | mThread->setVsyncRate(0, firstConnection); |
| 349 | |
| 350 | // By itself, this should not enable vsync events |
| 351 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 352 | EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value()); |
| 353 | |
| 354 | // However if there is another connection which wants events at a nonzero rate..... |
| 355 | ConnectionEventRecorder secondConnectionEventRecorder{0}; |
| 356 | sp<MockEventThreadConnection> secondConnection = |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 357 | createConnection(secondConnectionEventRecorder); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 358 | mThread->setVsyncRate(1, secondConnection); |
| 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 | // Send a vsync event. EventThread should then make a call to the |
| 364 | // interceptor, and the second connection. The first connection should not |
| 365 | // get the event. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 366 | mCallback->onVSyncEvent(123, 456, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 367 | expectInterceptCallReceived(123); |
| 368 | EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value()); |
| 369 | expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123, |
| 370 | 1u); |
| 371 | } |
| 372 | |
| 373 | TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) { |
| 374 | mThread->setVsyncRate(1, mConnection); |
| 375 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 376 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 377 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 378 | |
| 379 | // Send a vsync event. EventThread should then make a call to the |
| 380 | // interceptor, and the connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 381 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 382 | expectInterceptCallReceived(123); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 383 | expectThrottleVsyncReceived(456, mConnectionUid); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 384 | expectVsyncEventReceivedByConnection(123, 1u); |
| 385 | |
| 386 | // A second event should go to the same places. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 387 | mCallback->onVSyncEvent(456, 123, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 388 | expectInterceptCallReceived(456); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 389 | expectThrottleVsyncReceived(123, mConnectionUid); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 390 | expectVsyncEventReceivedByConnection(456, 2u); |
| 391 | |
| 392 | // A third event should go to the same places. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 393 | mCallback->onVSyncEvent(789, 777, 111); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 394 | expectInterceptCallReceived(789); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 395 | expectThrottleVsyncReceived(777, mConnectionUid); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 396 | expectVsyncEventReceivedByConnection(789, 3u); |
| 397 | } |
| 398 | |
| 399 | TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) { |
| 400 | mThread->setVsyncRate(2, mConnection); |
| 401 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 402 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 403 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 404 | |
| 405 | // The first event will be seen by the interceptor, and not the connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 406 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 407 | expectInterceptCallReceived(123); |
| 408 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 409 | EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 410 | |
| 411 | // The second event will be seen by the interceptor and the connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 412 | mCallback->onVSyncEvent(456, 123, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 413 | expectInterceptCallReceived(456); |
| 414 | expectVsyncEventReceivedByConnection(456, 2u); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 415 | EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 416 | |
| 417 | // The third event will be seen by the interceptor, and not the connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 418 | mCallback->onVSyncEvent(789, 777, 744); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 419 | expectInterceptCallReceived(789); |
| 420 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 421 | EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 422 | |
| 423 | // The fourth event will be seen by the interceptor and the connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 424 | mCallback->onVSyncEvent(101112, 7847, 86); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 425 | expectInterceptCallReceived(101112); |
| 426 | expectVsyncEventReceivedByConnection(101112, 4u); |
| 427 | } |
| 428 | |
| 429 | TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) { |
| 430 | mThread->setVsyncRate(1, mConnection); |
| 431 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 432 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 433 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 434 | |
| 435 | // Destroy the only (strong) reference to the connection. |
| 436 | mConnection = nullptr; |
| 437 | |
| 438 | // The first event will be seen by the interceptor, and not the connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 439 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 440 | expectInterceptCallReceived(123); |
| 441 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 442 | |
| 443 | // EventThread should disable vsync callbacks |
| 444 | expectVSyncSetEnabledCallReceived(false); |
| 445 | } |
| 446 | |
| 447 | TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) { |
| 448 | ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY}; |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 449 | sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 450 | mThread->setVsyncRate(1, errorConnection); |
| 451 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 452 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 453 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 454 | |
| 455 | // The first event will be seen by the interceptor, and by the connection, |
| 456 | // which then returns an error. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 457 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 458 | expectInterceptCallReceived(123); |
| 459 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u); |
| 460 | |
| 461 | // A subsequent event will be seen by the interceptor and not by the |
| 462 | // connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 463 | mCallback->onVSyncEvent(456, 123, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 464 | expectInterceptCallReceived(456); |
| 465 | EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value()); |
| 466 | |
| 467 | // EventThread should disable vsync callbacks with the second event |
| 468 | expectVSyncSetEnabledCallReceived(false); |
| 469 | } |
| 470 | |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 471 | TEST_F(EventThreadTest, tracksEventConnections) { |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 472 | EXPECT_EQ(2, mThread->getEventThreadConnectionCount()); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 473 | ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY}; |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 474 | sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 475 | mThread->setVsyncRate(1, errorConnection); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 476 | EXPECT_EQ(3, mThread->getEventThreadConnectionCount()); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 477 | ConnectionEventRecorder secondConnectionEventRecorder{0}; |
| 478 | sp<MockEventThreadConnection> secondConnection = |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 479 | createConnection(secondConnectionEventRecorder); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 480 | mThread->setVsyncRate(1, secondConnection); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 481 | EXPECT_EQ(4, mThread->getEventThreadConnectionCount()); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 482 | |
| 483 | // EventThread should enable vsync callbacks. |
| 484 | expectVSyncSetEnabledCallReceived(true); |
| 485 | |
| 486 | // The first event will be seen by the interceptor, and by the connection, |
| 487 | // which then returns an error. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 488 | mCallback->onVSyncEvent(123, 456, 789); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 489 | expectInterceptCallReceived(123); |
| 490 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u); |
| 491 | expectVsyncEventReceivedByConnection("successConnection", secondConnectionEventRecorder, 123, |
| 492 | 1u); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 493 | EXPECT_EQ(3, mThread->getEventThreadConnectionCount()); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 494 | } |
| 495 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 496 | TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) { |
| 497 | ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK}; |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 498 | sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 499 | mThread->setVsyncRate(1, errorConnection); |
| 500 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 501 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 502 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 503 | |
| 504 | // The first event will be seen by the interceptor, and by the connection, |
| 505 | // which then returns an non-fatal error. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 506 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 507 | expectInterceptCallReceived(123); |
| 508 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u); |
| 509 | |
| 510 | // A subsequent event will be seen by the interceptor, and by the connection, |
| 511 | // which still then returns an non-fatal error. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 512 | mCallback->onVSyncEvent(456, 123, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 513 | expectInterceptCallReceived(456); |
| 514 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u); |
| 515 | |
| 516 | // EventThread will not disable vsync callbacks as the errors are non-fatal. |
| 517 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 518 | } |
| 519 | |
| 520 | TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 521 | mThread->setDuration(321ns, 456ns); |
| 522 | expectVSyncSetDurationCallReceived(321ns, 456ns); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 525 | TEST_F(EventThreadTest, postHotplugInternalDisconnect) { |
| 526 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false); |
| 527 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 528 | } |
| 529 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 530 | TEST_F(EventThreadTest, postHotplugInternalConnect) { |
| 531 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true); |
| 532 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | TEST_F(EventThreadTest, postHotplugExternalDisconnect) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 536 | mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false); |
| 537 | expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | TEST_F(EventThreadTest, postHotplugExternalConnect) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 541 | mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true); |
| 542 | expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 543 | } |
| 544 | |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 545 | TEST_F(EventThreadTest, postConfigChangedPrimary) { |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 546 | mThread->onModeChanged(INTERNAL_DISPLAY_ID, DisplayModeId(7), 16666666); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 547 | expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, 16666666); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | TEST_F(EventThreadTest, postConfigChangedExternal) { |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 551 | mThread->onModeChanged(EXTERNAL_DISPLAY_ID, DisplayModeId(5), 16666666); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 552 | expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, 16666666); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 553 | } |
| 554 | |
Ady Abraham | af0ec27 | 2019-03-28 11:38:31 -0700 | [diff] [blame] | 555 | TEST_F(EventThreadTest, postConfigChangedPrimary64bit) { |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 556 | mThread->onModeChanged(DISPLAY_ID_64BIT, DisplayModeId(7), 16666666); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 557 | expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, 16666666); |
Ady Abraham | af0ec27 | 2019-03-28 11:38:31 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 560 | TEST_F(EventThreadTest, suppressConfigChanged) { |
| 561 | ConnectionEventRecorder suppressConnectionEventRecorder{0}; |
| 562 | sp<MockEventThreadConnection> suppressConnection = |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 563 | createConnection(suppressConnectionEventRecorder); |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 564 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 565 | mThread->onModeChanged(INTERNAL_DISPLAY_ID, DisplayModeId(9), 16666666); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 566 | expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, 16666666); |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 567 | |
| 568 | auto args = suppressConnectionEventRecorder.waitForCall(); |
| 569 | ASSERT_FALSE(args.has_value()); |
| 570 | } |
| 571 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 572 | TEST_F(EventThreadTest, postUidFrameRateMapping) { |
| 573 | const std::vector<FrameRateOverride> overrides = { |
| 574 | {.uid = 1, .frameRateHz = 20}, |
| 575 | {.uid = 3, .frameRateHz = 40}, |
| 576 | {.uid = 5, .frameRateHz = 60}, |
| 577 | }; |
| 578 | |
| 579 | mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides); |
| 580 | expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides); |
| 581 | } |
| 582 | |
| 583 | TEST_F(EventThreadTest, suppressUidFrameRateMapping) { |
| 584 | const std::vector<FrameRateOverride> overrides = { |
| 585 | {.uid = 1, .frameRateHz = 20}, |
| 586 | {.uid = 3, .frameRateHz = 40}, |
| 587 | {.uid = 5, .frameRateHz = 60}, |
| 588 | }; |
| 589 | |
| 590 | ConnectionEventRecorder suppressConnectionEventRecorder{0}; |
| 591 | sp<MockEventThreadConnection> suppressConnection = |
| 592 | createConnection(suppressConnectionEventRecorder); |
| 593 | |
| 594 | mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides); |
| 595 | expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides); |
| 596 | |
| 597 | auto args = suppressConnectionEventRecorder.waitForCall(); |
| 598 | ASSERT_FALSE(args.has_value()); |
| 599 | } |
| 600 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 601 | TEST_F(EventThreadTest, requestNextVsyncWithThrottleVsyncDoesntPostVSync) { |
| 602 | // Signal that we want the next vsync event to be posted to the throttled connection |
| 603 | mThread->requestNextVsync(mThrottledConnection); |
| 604 | |
| 605 | // EventThread should immediately request a resync. |
| 606 | EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value()); |
| 607 | |
| 608 | // EventThread should enable vsync callbacks. |
| 609 | expectVSyncSetEnabledCallReceived(true); |
| 610 | |
| 611 | // Use the received callback to signal a first vsync event. |
| 612 | // The interceptor should receive the event, but not the connection. |
| 613 | mCallback->onVSyncEvent(123, 456, 789); |
| 614 | expectInterceptCallReceived(123); |
| 615 | expectThrottleVsyncReceived(456, mThrottledConnectionUid); |
| 616 | mThrottledConnectionEventCallRecorder.waitForUnexpectedCall(); |
| 617 | |
| 618 | // Use the received callback to signal a second vsync event. |
| 619 | // The interceptor should receive the event, but the connection should |
| 620 | // not as it was only interested in the first. |
| 621 | mCallback->onVSyncEvent(456, 123, 0); |
| 622 | expectInterceptCallReceived(456); |
| 623 | expectThrottleVsyncReceived(123, mThrottledConnectionUid); |
| 624 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 625 | |
| 626 | // EventThread should not change the vsync state as it didn't send the event |
| 627 | // yet |
| 628 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 629 | } |
| 630 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 631 | } // namespace |
| 632 | } // namespace android |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 633 | |
| 634 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 635 | #pragma clang diagnostic pop // ignored "-Wextra" |