| Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2019 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
|  | 18 | #pragma clang diagnostic push | 
|  | 19 | #pragma clang diagnostic ignored "-Wconversion" | 
|  | 20 |  | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 21 | #undef LOG_TAG | 
|  | 22 | #define LOG_TAG "SchedulerUnittests" | 
|  | 23 |  | 
|  | 24 | #include <gmock/gmock.h> | 
|  | 25 | #include <gtest/gtest.h> | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 26 | #include <log/log.h> | 
|  | 27 |  | 
| Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 28 | #include <mutex> | 
|  | 29 |  | 
| Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 30 | #include "Scheduler/EventControlThread.h" | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 31 | #include "Scheduler/EventThread.h" | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 32 | #include "Scheduler/RefreshRateConfigs.h" | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 33 | #include "TestableScheduler.h" | 
| Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 34 | #include "mock/DisplayHardware/MockDisplay.h" | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 35 | #include "mock/MockEventThread.h" | 
|  | 36 |  | 
|  | 37 | using testing::_; | 
|  | 38 | using testing::Return; | 
|  | 39 |  | 
|  | 40 | namespace android { | 
|  | 41 |  | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 42 | constexpr PhysicalDisplayId PHYSICAL_DISPLAY_ID = 999; | 
|  | 43 |  | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 44 | class SchedulerTest : public testing::Test { | 
|  | 45 | protected: | 
| Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 46 | class MockEventThreadConnection : public android::EventThreadConnection { | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 47 | public: | 
| Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 48 | explicit MockEventThreadConnection(EventThread* eventThread) | 
| Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 49 | : EventThreadConnection(eventThread, ResyncCallback(), | 
|  | 50 | ISurfaceComposer::eConfigChangedSuppress) {} | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 51 | ~MockEventThreadConnection() = default; | 
|  | 52 |  | 
|  | 53 | MOCK_METHOD1(stealReceiveChannel, status_t(gui::BitTube* outChannel)); | 
|  | 54 | MOCK_METHOD1(setVsyncRate, status_t(uint32_t count)); | 
|  | 55 | MOCK_METHOD0(requestNextVsync, void()); | 
|  | 56 | }; | 
|  | 57 |  | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 58 | SchedulerTest(); | 
|  | 59 | ~SchedulerTest() override; | 
|  | 60 |  | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 61 | std::unique_ptr<scheduler::RefreshRateConfigs> mRefreshRateConfigs; | 
|  | 62 | std::unique_ptr<TestableScheduler> mScheduler; | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 63 |  | 
|  | 64 | Scheduler::ConnectionHandle mConnectionHandle; | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 65 | mock::EventThread* mEventThread; | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 66 | sp<MockEventThreadConnection> mEventThreadConnection; | 
| Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 67 | Hwc2::mock::Display mDisplay; | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 68 | }; | 
|  | 69 |  | 
|  | 70 | SchedulerTest::SchedulerTest() { | 
|  | 71 | const ::testing::TestInfo* const test_info = | 
|  | 72 | ::testing::UnitTest::GetInstance()->current_test_info(); | 
|  | 73 | ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); | 
|  | 74 |  | 
| Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 75 | std::vector<std::shared_ptr<const HWC2::Display::Config>> configs{ | 
|  | 76 | HWC2::Display::Config::Builder(mDisplay, 0) | 
|  | 77 | .setVsyncPeriod(int32_t(16666667)) | 
|  | 78 | .setConfigGroup(0) | 
|  | 79 | .build()}; | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 80 | mRefreshRateConfigs = std::make_unique< | 
| Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 81 | scheduler::RefreshRateConfigs>(configs, /*currentConfig=*/HwcConfigIndexType(0)); | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 82 |  | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 83 | mScheduler = std::make_unique<TestableScheduler>(*mRefreshRateConfigs, false); | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 84 |  | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 85 | auto eventThread = std::make_unique<mock::EventThread>(); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 86 | mEventThread = eventThread.get(); | 
| Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 87 | EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)).WillOnce(Return(0)); | 
|  | 88 |  | 
|  | 89 | mEventThreadConnection = new MockEventThreadConnection(mEventThread); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 90 |  | 
|  | 91 | // createConnection call to scheduler makes a createEventConnection call to EventThread. Make | 
|  | 92 | // sure that call gets executed and returns an EventThread::Connection object. | 
| Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 93 | EXPECT_CALL(*mEventThread, createEventConnection(_, _)) | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 94 | .WillRepeatedly(Return(mEventThreadConnection)); | 
|  | 95 |  | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 96 | mConnectionHandle = mScheduler->createConnection(std::move(eventThread)); | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 97 | EXPECT_TRUE(mConnectionHandle); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 98 | } | 
|  | 99 |  | 
|  | 100 | SchedulerTest::~SchedulerTest() { | 
|  | 101 | const ::testing::TestInfo* const test_info = | 
|  | 102 | ::testing::UnitTest::GetInstance()->current_test_info(); | 
|  | 103 | ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | namespace { | 
|  | 107 | /* ------------------------------------------------------------------------ | 
|  | 108 | * Test cases | 
|  | 109 | */ | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 110 |  | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 111 | TEST_F(SchedulerTest, invalidConnectionHandle) { | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 112 | Scheduler::ConnectionHandle handle; | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 113 |  | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 114 | sp<IDisplayEventConnection> connection; | 
| Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 115 | ASSERT_NO_FATAL_FAILURE( | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 116 | connection = mScheduler->createDisplayEventConnection(handle, | 
|  | 117 | ISurfaceComposer:: | 
|  | 118 | eConfigChangedSuppress)); | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 119 | EXPECT_FALSE(connection); | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 120 | EXPECT_FALSE(mScheduler->getEventConnection(handle)); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 121 |  | 
|  | 122 | // The EXPECT_CALLS make sure we don't call the functions on the subsequent event threads. | 
|  | 123 | EXPECT_CALL(*mEventThread, onHotplugReceived(_, _)).Times(0); | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 124 | ASSERT_NO_FATAL_FAILURE(mScheduler->onHotplugReceived(handle, PHYSICAL_DISPLAY_ID, false)); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 125 |  | 
|  | 126 | EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(0); | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 127 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenAcquired(handle)); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 128 |  | 
|  | 129 | EXPECT_CALL(*mEventThread, onScreenReleased()).Times(0); | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 130 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenReleased(handle)); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 131 |  | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 132 | std::string output; | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 133 | EXPECT_CALL(*mEventThread, dump(_)).Times(0); | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 134 | ASSERT_NO_FATAL_FAILURE(mScheduler->dump(handle, output)); | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 135 | EXPECT_TRUE(output.empty()); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 136 |  | 
|  | 137 | EXPECT_CALL(*mEventThread, setPhaseOffset(_)).Times(0); | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 138 | ASSERT_NO_FATAL_FAILURE(mScheduler->setPhaseOffset(handle, 10)); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 139 | } | 
|  | 140 |  | 
|  | 141 | TEST_F(SchedulerTest, validConnectionHandle) { | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 142 | sp<IDisplayEventConnection> connection; | 
| Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 143 | ASSERT_NO_FATAL_FAILURE( | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 144 | connection = mScheduler->createDisplayEventConnection(mConnectionHandle, | 
|  | 145 | ISurfaceComposer:: | 
|  | 146 | eConfigChangedSuppress)); | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 147 | ASSERT_EQ(mEventThreadConnection, connection); | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 148 | EXPECT_TRUE(mScheduler->getEventConnection(mConnectionHandle)); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 149 |  | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 150 | EXPECT_CALL(*mEventThread, onHotplugReceived(PHYSICAL_DISPLAY_ID, false)).Times(1); | 
|  | 151 | ASSERT_NO_FATAL_FAILURE( | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 152 | mScheduler->onHotplugReceived(mConnectionHandle, PHYSICAL_DISPLAY_ID, false)); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 153 |  | 
|  | 154 | EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(1); | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 155 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenAcquired(mConnectionHandle)); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 156 |  | 
|  | 157 | EXPECT_CALL(*mEventThread, onScreenReleased()).Times(1); | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 158 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenReleased(mConnectionHandle)); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 159 |  | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 160 | std::string output("dump"); | 
|  | 161 | EXPECT_CALL(*mEventThread, dump(output)).Times(1); | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 162 | ASSERT_NO_FATAL_FAILURE(mScheduler->dump(mConnectionHandle, output)); | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 163 | EXPECT_FALSE(output.empty()); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 164 |  | 
|  | 165 | EXPECT_CALL(*mEventThread, setPhaseOffset(10)).Times(1); | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 166 | ASSERT_NO_FATAL_FAILURE(mScheduler->setPhaseOffset(mConnectionHandle, 10)); | 
| Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 167 |  | 
|  | 168 | static constexpr size_t kEventConnections = 5; | 
|  | 169 | ON_CALL(*mEventThread, getEventThreadConnectionCount()) | 
|  | 170 | .WillByDefault(Return(kEventConnections)); | 
|  | 171 | EXPECT_EQ(kEventConnections, mScheduler->getEventThreadConnectionCount(mConnectionHandle)); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 172 | } | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 173 |  | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 174 | } // namespace | 
| Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 175 | } // namespace android | 
| Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 176 |  | 
|  | 177 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
|  | 178 | #pragma clang diagnostic pop // ignored "-Wconversion" |