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); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 47 | |
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, |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 144 | ISurfaceComposer::EventRegistration::configChanged | |
| 145 | ISurfaceComposer::EventRegistration::frameRateOverride); |
| 146 | mThrottledConnection = createConnection(mThrottledConnectionEventCallRecorder, |
| 147 | ISurfaceComposer::EventRegistration::configChanged, |
| 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 | }; |
| 169 | |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 170 | mThread = std::make_unique<impl::EventThread>(std::move(source), |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 171 | /*tokenManager=*/nullptr, |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 172 | mInterceptVSyncCallRecorder.getInvocable(), |
| 173 | throttleVsync); |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 174 | |
| 175 | // EventThread should register itself as VSyncSource callback. |
| 176 | mCallback = expectVSyncSetCallbackCallReceived(); |
| 177 | ASSERT_TRUE(mCallback); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection( |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 181 | ConnectionEventRecorder& recorder, |
| 182 | ISurfaceComposer::EventRegistrationFlags eventRegistration, uid_t ownerUid) { |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 183 | sp<MockEventThreadConnection> connection = |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 184 | new MockEventThreadConnection(mThread.get(), ownerUid, |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 185 | mResyncCallRecorder.getInvocable(), eventRegistration); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 186 | EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable())); |
| 187 | return connection; |
| 188 | } |
| 189 | |
| 190 | void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) { |
| 191 | auto args = mVSyncSetEnabledCallRecorder.waitForCall(); |
| 192 | ASSERT_TRUE(args.has_value()); |
| 193 | EXPECT_EQ(expectedState, std::get<0>(args.value())); |
| 194 | } |
| 195 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 196 | void EventThreadTest::expectVSyncSetDurationCallReceived( |
| 197 | std::chrono::nanoseconds expectedDuration, std::chrono::nanoseconds expectedReadyDuration) { |
| 198 | auto args = mVSyncSetDurationCallRecorder.waitForCall(); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 199 | ASSERT_TRUE(args.has_value()); |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 200 | EXPECT_EQ(expectedDuration, std::get<0>(args.value())); |
| 201 | EXPECT_EQ(expectedReadyDuration, std::get<1>(args.value())); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() { |
| 205 | auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall(); |
| 206 | return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr; |
| 207 | } |
| 208 | |
| 209 | void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) { |
| 210 | auto args = mInterceptVSyncCallRecorder.waitForCall(); |
| 211 | ASSERT_TRUE(args.has_value()); |
| 212 | EXPECT_EQ(expectedTimestamp, std::get<0>(args.value())); |
| 213 | } |
| 214 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 215 | void EventThreadTest::expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t uid) { |
| 216 | auto args = mThrottleVsyncCallRecorder.waitForCall(); |
| 217 | ASSERT_TRUE(args.has_value()); |
| 218 | EXPECT_EQ(expectedTimestamp, std::get<0>(args.value())); |
| 219 | EXPECT_EQ(uid, std::get<1>(args.value())); |
| 220 | } |
| 221 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 222 | void EventThreadTest::expectVsyncEventReceivedByConnection( |
| 223 | const char* name, ConnectionEventRecorder& connectionEventRecorder, |
| 224 | nsecs_t expectedTimestamp, unsigned expectedCount) { |
| 225 | auto args = connectionEventRecorder.waitForCall(); |
| 226 | ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp " |
| 227 | << expectedTimestamp; |
| 228 | const auto& event = std::get<0>(args.value()); |
| 229 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type) |
| 230 | << name << " did not get the correct event for timestamp " << expectedTimestamp; |
| 231 | EXPECT_EQ(expectedTimestamp, event.header.timestamp) |
| 232 | << name << " did not get the expected timestamp for timestamp " << expectedTimestamp; |
| 233 | EXPECT_EQ(expectedCount, event.vsync.count) |
| 234 | << name << " did not get the expected count for timestamp " << expectedTimestamp; |
| 235 | } |
| 236 | |
| 237 | void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, |
| 238 | unsigned expectedCount) { |
| 239 | expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder", |
| 240 | mConnectionEventCallRecorder, expectedTimestamp, |
| 241 | expectedCount); |
| 242 | } |
| 243 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 244 | void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, |
| 245 | bool expectedConnected) { |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 246 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 247 | ASSERT_TRUE(args.has_value()); |
| 248 | const auto& event = std::get<0>(args.value()); |
| 249 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 250 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 251 | EXPECT_EQ(expectedConnected, event.hotplug.connected); |
| 252 | } |
| 253 | |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 254 | void EventThreadTest::expectConfigChangedEventReceivedByConnection( |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 255 | PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId, |
| 256 | nsecs_t expectedVsyncPeriod) { |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 257 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 258 | ASSERT_TRUE(args.has_value()); |
| 259 | const auto& event = std::get<0>(args.value()); |
| 260 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED, event.header.type); |
| 261 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
| 262 | EXPECT_EQ(expectedConfigId, event.config.configId); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 263 | EXPECT_EQ(expectedVsyncPeriod, event.config.vsyncPeriod); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 266 | void EventThreadTest::expectUidFrameRateMappingEventReceivedByConnection( |
| 267 | PhysicalDisplayId expectedDisplayId, std::vector<FrameRateOverride> expectedOverrides) { |
| 268 | for (const auto [uid, frameRateHz] : expectedOverrides) { |
| 269 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 270 | ASSERT_TRUE(args.has_value()); |
| 271 | const auto& event = std::get<0>(args.value()); |
| 272 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE, event.header.type); |
| 273 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
| 274 | EXPECT_EQ(uid, event.frameRateOverride.uid); |
| 275 | EXPECT_EQ(frameRateHz, event.frameRateOverride.frameRateHz); |
| 276 | } |
| 277 | |
| 278 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 279 | ASSERT_TRUE(args.has_value()); |
| 280 | const auto& event = std::get<0>(args.value()); |
| 281 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH, event.header.type); |
| 282 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
| 283 | } |
| 284 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 285 | namespace { |
| 286 | |
| 287 | /* ------------------------------------------------------------------------ |
| 288 | * Test cases |
| 289 | */ |
| 290 | |
| 291 | TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) { |
| 292 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 293 | EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value()); |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 294 | EXPECT_FALSE(mVSyncSetDurationCallRecorder.waitForCall(0us).has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 295 | EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value()); |
| 296 | EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value()); |
| 297 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value()); |
| 298 | } |
| 299 | |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 300 | TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 301 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false); |
| 302 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 303 | |
| 304 | // 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] | 305 | mThread->requestNextVsync(mConnection); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 306 | |
| 307 | // EventThread should not enable vsync callbacks. |
| 308 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 311 | TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) { |
| 312 | // 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] | 313 | mThread->requestNextVsync(mConnection); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 314 | |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 315 | // EventThread should immediately request a resync. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 316 | EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value()); |
| 317 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 318 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 319 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 320 | |
| 321 | // Use the received callback to signal a first vsync event. |
| 322 | // The interceptor should receive the event, as well as the connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 323 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 324 | expectInterceptCallReceived(123); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 325 | expectThrottleVsyncReceived(456, mConnectionUid); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 326 | expectVsyncEventReceivedByConnection(123, 1u); |
| 327 | |
| 328 | // Use the received callback to signal a second vsync event. |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 329 | // The interceptor should receive the event, but the connection should |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 330 | // not as it was only interested in the first. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 331 | mCallback->onVSyncEvent(456, 123, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 332 | expectInterceptCallReceived(456); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 333 | EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 334 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 335 | |
| 336 | // EventThread should also detect that at this point that it does not need |
| 337 | // any more vsync events, and should disable their generation. |
| 338 | expectVSyncSetEnabledCallReceived(false); |
| 339 | } |
| 340 | |
| 341 | TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) { |
| 342 | // Create a first connection, register it, and request a vsync rate of zero. |
| 343 | ConnectionEventRecorder firstConnectionEventRecorder{0}; |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 344 | sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 345 | mThread->setVsyncRate(0, firstConnection); |
| 346 | |
| 347 | // By itself, this should not enable vsync events |
| 348 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 349 | EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value()); |
| 350 | |
| 351 | // However if there is another connection which wants events at a nonzero rate..... |
| 352 | ConnectionEventRecorder secondConnectionEventRecorder{0}; |
| 353 | sp<MockEventThreadConnection> secondConnection = |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 354 | createConnection(secondConnectionEventRecorder); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 355 | mThread->setVsyncRate(1, secondConnection); |
| 356 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 357 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 358 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 359 | |
| 360 | // Send a vsync event. EventThread should then make a call to the |
| 361 | // interceptor, and the second connection. The first connection should not |
| 362 | // get the event. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 363 | mCallback->onVSyncEvent(123, 456, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 364 | expectInterceptCallReceived(123); |
| 365 | EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value()); |
| 366 | expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123, |
| 367 | 1u); |
| 368 | } |
| 369 | |
| 370 | TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) { |
| 371 | mThread->setVsyncRate(1, mConnection); |
| 372 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 373 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 374 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 375 | |
| 376 | // Send a vsync event. EventThread should then make a call to the |
| 377 | // interceptor, and the connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 378 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 379 | expectInterceptCallReceived(123); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 380 | expectThrottleVsyncReceived(456, mConnectionUid); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 381 | expectVsyncEventReceivedByConnection(123, 1u); |
| 382 | |
| 383 | // A second event should go to the same places. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 384 | mCallback->onVSyncEvent(456, 123, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 385 | expectInterceptCallReceived(456); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 386 | expectThrottleVsyncReceived(123, mConnectionUid); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 387 | expectVsyncEventReceivedByConnection(456, 2u); |
| 388 | |
| 389 | // A third event should go to the same places. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 390 | mCallback->onVSyncEvent(789, 777, 111); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 391 | expectInterceptCallReceived(789); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 392 | expectThrottleVsyncReceived(777, mConnectionUid); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 393 | expectVsyncEventReceivedByConnection(789, 3u); |
| 394 | } |
| 395 | |
| 396 | TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) { |
| 397 | mThread->setVsyncRate(2, mConnection); |
| 398 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 399 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 400 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 401 | |
| 402 | // 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] | 403 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 404 | expectInterceptCallReceived(123); |
| 405 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 406 | EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 407 | |
| 408 | // The second event will be seen by the interceptor and the connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 409 | mCallback->onVSyncEvent(456, 123, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 410 | expectInterceptCallReceived(456); |
| 411 | expectVsyncEventReceivedByConnection(456, 2u); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 412 | EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 413 | |
| 414 | // 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] | 415 | mCallback->onVSyncEvent(789, 777, 744); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 416 | expectInterceptCallReceived(789); |
| 417 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 418 | EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 419 | |
| 420 | // The fourth event will be seen by the interceptor and the connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 421 | mCallback->onVSyncEvent(101112, 7847, 86); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 422 | expectInterceptCallReceived(101112); |
| 423 | expectVsyncEventReceivedByConnection(101112, 4u); |
| 424 | } |
| 425 | |
| 426 | TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) { |
| 427 | mThread->setVsyncRate(1, mConnection); |
| 428 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 429 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 430 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 431 | |
| 432 | // Destroy the only (strong) reference to the connection. |
| 433 | mConnection = nullptr; |
| 434 | |
| 435 | // 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] | 436 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 437 | expectInterceptCallReceived(123); |
| 438 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 439 | |
| 440 | // EventThread should disable vsync callbacks |
| 441 | expectVSyncSetEnabledCallReceived(false); |
| 442 | } |
| 443 | |
| 444 | TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) { |
| 445 | ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY}; |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 446 | sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 447 | mThread->setVsyncRate(1, errorConnection); |
| 448 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 449 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 450 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 451 | |
| 452 | // The first event will be seen by the interceptor, and by the connection, |
| 453 | // which then returns an error. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 454 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 455 | expectInterceptCallReceived(123); |
| 456 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u); |
| 457 | |
| 458 | // A subsequent event will be seen by the interceptor and not by the |
| 459 | // connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 460 | mCallback->onVSyncEvent(456, 123, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 461 | expectInterceptCallReceived(456); |
| 462 | EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value()); |
| 463 | |
| 464 | // EventThread should disable vsync callbacks with the second event |
| 465 | expectVSyncSetEnabledCallReceived(false); |
| 466 | } |
| 467 | |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 468 | TEST_F(EventThreadTest, tracksEventConnections) { |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 469 | EXPECT_EQ(2, mThread->getEventThreadConnectionCount()); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 470 | ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY}; |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 471 | sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 472 | mThread->setVsyncRate(1, errorConnection); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 473 | EXPECT_EQ(3, mThread->getEventThreadConnectionCount()); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 474 | ConnectionEventRecorder secondConnectionEventRecorder{0}; |
| 475 | sp<MockEventThreadConnection> secondConnection = |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 476 | createConnection(secondConnectionEventRecorder); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 477 | mThread->setVsyncRate(1, secondConnection); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 478 | EXPECT_EQ(4, mThread->getEventThreadConnectionCount()); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 479 | |
| 480 | // EventThread should enable vsync callbacks. |
| 481 | expectVSyncSetEnabledCallReceived(true); |
| 482 | |
| 483 | // The first event will be seen by the interceptor, and by the connection, |
| 484 | // which then returns an error. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 485 | mCallback->onVSyncEvent(123, 456, 789); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 486 | expectInterceptCallReceived(123); |
| 487 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u); |
| 488 | expectVsyncEventReceivedByConnection("successConnection", secondConnectionEventRecorder, 123, |
| 489 | 1u); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 490 | EXPECT_EQ(3, mThread->getEventThreadConnectionCount()); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 491 | } |
| 492 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 493 | TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) { |
| 494 | ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK}; |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 495 | sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 496 | mThread->setVsyncRate(1, errorConnection); |
| 497 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 498 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 499 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 500 | |
| 501 | // The first event will be seen by the interceptor, and by the connection, |
| 502 | // which then returns an non-fatal error. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 503 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 504 | expectInterceptCallReceived(123); |
| 505 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u); |
| 506 | |
| 507 | // A subsequent event will be seen by the interceptor, and by the connection, |
| 508 | // which still then returns an non-fatal error. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 509 | mCallback->onVSyncEvent(456, 123, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 510 | expectInterceptCallReceived(456); |
| 511 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u); |
| 512 | |
| 513 | // EventThread will not disable vsync callbacks as the errors are non-fatal. |
| 514 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 515 | } |
| 516 | |
| 517 | TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 518 | mThread->setDuration(321ns, 456ns); |
| 519 | expectVSyncSetDurationCallReceived(321ns, 456ns); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 520 | } |
| 521 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 522 | TEST_F(EventThreadTest, postHotplugInternalDisconnect) { |
| 523 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false); |
| 524 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 525 | } |
| 526 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 527 | TEST_F(EventThreadTest, postHotplugInternalConnect) { |
| 528 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true); |
| 529 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | TEST_F(EventThreadTest, postHotplugExternalDisconnect) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 533 | mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false); |
| 534 | expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | TEST_F(EventThreadTest, postHotplugExternalConnect) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 538 | mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true); |
| 539 | expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 540 | } |
| 541 | |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 542 | TEST_F(EventThreadTest, postConfigChangedPrimary) { |
Marin Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 543 | mThread->onConfigChanged(INTERNAL_DISPLAY_ID, DisplayModeId(7), 16666666); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 544 | expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, 16666666); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | TEST_F(EventThreadTest, postConfigChangedExternal) { |
Marin Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 548 | mThread->onConfigChanged(EXTERNAL_DISPLAY_ID, DisplayModeId(5), 16666666); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 549 | expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, 16666666); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 550 | } |
| 551 | |
Ady Abraham | af0ec27 | 2019-03-28 11:38:31 -0700 | [diff] [blame] | 552 | TEST_F(EventThreadTest, postConfigChangedPrimary64bit) { |
Marin Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 553 | mThread->onConfigChanged(DISPLAY_ID_64BIT, DisplayModeId(7), 16666666); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 554 | expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, 16666666); |
Ady Abraham | af0ec27 | 2019-03-28 11:38:31 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 557 | TEST_F(EventThreadTest, suppressConfigChanged) { |
| 558 | ConnectionEventRecorder suppressConnectionEventRecorder{0}; |
| 559 | sp<MockEventThreadConnection> suppressConnection = |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 560 | createConnection(suppressConnectionEventRecorder); |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 561 | |
Marin Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 562 | mThread->onConfigChanged(INTERNAL_DISPLAY_ID, DisplayModeId(9), 16666666); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 563 | expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, 16666666); |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 564 | |
| 565 | auto args = suppressConnectionEventRecorder.waitForCall(); |
| 566 | ASSERT_FALSE(args.has_value()); |
| 567 | } |
| 568 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 569 | TEST_F(EventThreadTest, postUidFrameRateMapping) { |
| 570 | const std::vector<FrameRateOverride> overrides = { |
| 571 | {.uid = 1, .frameRateHz = 20}, |
| 572 | {.uid = 3, .frameRateHz = 40}, |
| 573 | {.uid = 5, .frameRateHz = 60}, |
| 574 | }; |
| 575 | |
| 576 | mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides); |
| 577 | expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides); |
| 578 | } |
| 579 | |
| 580 | TEST_F(EventThreadTest, suppressUidFrameRateMapping) { |
| 581 | const std::vector<FrameRateOverride> overrides = { |
| 582 | {.uid = 1, .frameRateHz = 20}, |
| 583 | {.uid = 3, .frameRateHz = 40}, |
| 584 | {.uid = 5, .frameRateHz = 60}, |
| 585 | }; |
| 586 | |
| 587 | ConnectionEventRecorder suppressConnectionEventRecorder{0}; |
| 588 | sp<MockEventThreadConnection> suppressConnection = |
| 589 | createConnection(suppressConnectionEventRecorder); |
| 590 | |
| 591 | mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides); |
| 592 | expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides); |
| 593 | |
| 594 | auto args = suppressConnectionEventRecorder.waitForCall(); |
| 595 | ASSERT_FALSE(args.has_value()); |
| 596 | } |
| 597 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 598 | TEST_F(EventThreadTest, requestNextVsyncWithThrottleVsyncDoesntPostVSync) { |
| 599 | // Signal that we want the next vsync event to be posted to the throttled connection |
| 600 | mThread->requestNextVsync(mThrottledConnection); |
| 601 | |
| 602 | // EventThread should immediately request a resync. |
| 603 | EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value()); |
| 604 | |
| 605 | // EventThread should enable vsync callbacks. |
| 606 | expectVSyncSetEnabledCallReceived(true); |
| 607 | |
| 608 | // Use the received callback to signal a first vsync event. |
| 609 | // The interceptor should receive the event, but not the connection. |
| 610 | mCallback->onVSyncEvent(123, 456, 789); |
| 611 | expectInterceptCallReceived(123); |
| 612 | expectThrottleVsyncReceived(456, mThrottledConnectionUid); |
| 613 | mThrottledConnectionEventCallRecorder.waitForUnexpectedCall(); |
| 614 | |
| 615 | // Use the received callback to signal a second vsync event. |
| 616 | // The interceptor should receive the event, but the connection should |
| 617 | // not as it was only interested in the first. |
| 618 | mCallback->onVSyncEvent(456, 123, 0); |
| 619 | expectInterceptCallReceived(456); |
| 620 | expectThrottleVsyncReceived(123, mThrottledConnectionUid); |
| 621 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 622 | |
| 623 | // EventThread should not change the vsync state as it didn't send the event |
| 624 | // yet |
| 625 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 626 | } |
| 627 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 628 | } // namespace |
| 629 | } // namespace android |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 630 | |
| 631 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 632 | #pragma clang diagnostic pop // ignored "-Wextra" |