| 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 |  | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 17 | #include <gmock/gmock.h> | 
|  | 18 | #include <gtest/gtest.h> | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 19 | #include <log/log.h> | 
|  | 20 |  | 
| Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 21 | #include <mutex> | 
|  | 22 |  | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 23 | #include "Scheduler/EventThread.h" | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 24 | #include "Scheduler/RefreshRateConfigs.h" | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 25 | #include "TestableScheduler.h" | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 26 | #include "TestableSurfaceFlinger.h" | 
| Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 27 | #include "mock/DisplayHardware/MockDisplay.h" | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 28 | #include "mock/MockEventThread.h" | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 29 | #include "mock/MockLayer.h" | 
| Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 30 | #include "mock/MockSchedulerCallback.h" | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 31 |  | 
|  | 32 | using testing::_; | 
|  | 33 | using testing::Return; | 
|  | 34 |  | 
|  | 35 | namespace android { | 
| Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 36 | namespace { | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 37 |  | 
| Marin Shalamanov | a524a09 | 2020-07-27 21:39:55 +0200 | [diff] [blame] | 38 | constexpr PhysicalDisplayId PHYSICAL_DISPLAY_ID(999); | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 39 |  | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 40 | class SchedulerTest : public testing::Test { | 
|  | 41 | protected: | 
| Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 42 | class MockEventThreadConnection : public android::EventThreadConnection { | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 43 | public: | 
| Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 44 | explicit MockEventThreadConnection(EventThread* eventThread) | 
| Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 45 | : EventThreadConnection(eventThread, ResyncCallback(), | 
|  | 46 | ISurfaceComposer::eConfigChangedSuppress) {} | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 47 | ~MockEventThreadConnection() = default; | 
|  | 48 |  | 
|  | 49 | MOCK_METHOD1(stealReceiveChannel, status_t(gui::BitTube* outChannel)); | 
|  | 50 | MOCK_METHOD1(setVsyncRate, status_t(uint32_t count)); | 
|  | 51 | MOCK_METHOD0(requestNextVsync, void()); | 
|  | 52 | }; | 
|  | 53 |  | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 54 | SchedulerTest(); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 55 |  | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 56 | Hwc2::mock::Display mDisplay; | 
|  | 57 | const scheduler::RefreshRateConfigs mConfigs{{HWC2::Display::Config::Builder(mDisplay, 0) | 
|  | 58 | .setVsyncPeriod(16'666'667) | 
|  | 59 | .setConfigGroup(0) | 
|  | 60 | .build()}, | 
|  | 61 | HwcConfigIndexType(0)}; | 
|  | 62 |  | 
| Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 63 | mock::SchedulerCallback mSchedulerCallback; | 
|  | 64 |  | 
|  | 65 | // The scheduler should initially disable VSYNC. | 
|  | 66 | struct ExpectDisableVsync { | 
|  | 67 | ExpectDisableVsync(mock::SchedulerCallback& callback) { | 
|  | 68 | EXPECT_CALL(callback, setVsyncEnabled(false)).Times(1); | 
|  | 69 | } | 
|  | 70 | } mExpectDisableVsync{mSchedulerCallback}; | 
|  | 71 |  | 
|  | 72 | static constexpr bool kUseContentDetectionV2 = false; | 
|  | 73 | TestableScheduler mScheduler{mConfigs, mSchedulerCallback, kUseContentDetectionV2}; | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 74 |  | 
|  | 75 | Scheduler::ConnectionHandle mConnectionHandle; | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 76 | mock::EventThread* mEventThread; | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 77 | sp<MockEventThreadConnection> mEventThreadConnection; | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 78 | }; | 
|  | 79 |  | 
|  | 80 | SchedulerTest::SchedulerTest() { | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 81 | auto eventThread = std::make_unique<mock::EventThread>(); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 82 | mEventThread = eventThread.get(); | 
| Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 83 | EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)).WillOnce(Return(0)); | 
|  | 84 |  | 
|  | 85 | mEventThreadConnection = new MockEventThreadConnection(mEventThread); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 86 |  | 
|  | 87 | // createConnection call to scheduler makes a createEventConnection call to EventThread. Make | 
|  | 88 | // sure that call gets executed and returns an EventThread::Connection object. | 
| Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 89 | EXPECT_CALL(*mEventThread, createEventConnection(_, _)) | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 90 | .WillRepeatedly(Return(mEventThreadConnection)); | 
|  | 91 |  | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 92 | mConnectionHandle = mScheduler.createConnection(std::move(eventThread)); | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 93 | EXPECT_TRUE(mConnectionHandle); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 94 | } | 
|  | 95 |  | 
| Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 96 | } // namespace | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 97 |  | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 98 | TEST_F(SchedulerTest, invalidConnectionHandle) { | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 99 | Scheduler::ConnectionHandle handle; | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 100 |  | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 101 | const sp<IDisplayEventConnection> connection = | 
|  | 102 | mScheduler.createDisplayEventConnection(handle, | 
|  | 103 | ISurfaceComposer::eConfigChangedSuppress); | 
|  | 104 |  | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 105 | EXPECT_FALSE(connection); | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 106 | EXPECT_FALSE(mScheduler.getEventConnection(handle)); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 107 |  | 
|  | 108 | // The EXPECT_CALLS make sure we don't call the functions on the subsequent event threads. | 
|  | 109 | EXPECT_CALL(*mEventThread, onHotplugReceived(_, _)).Times(0); | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 110 | mScheduler.onHotplugReceived(handle, PHYSICAL_DISPLAY_ID, false); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 111 |  | 
|  | 112 | EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(0); | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 113 | mScheduler.onScreenAcquired(handle); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 114 |  | 
|  | 115 | EXPECT_CALL(*mEventThread, onScreenReleased()).Times(0); | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 116 | mScheduler.onScreenReleased(handle); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 117 |  | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 118 | std::string output; | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 119 | EXPECT_CALL(*mEventThread, dump(_)).Times(0); | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 120 | mScheduler.dump(handle, output); | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 121 | EXPECT_TRUE(output.empty()); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 122 |  | 
| Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 123 | EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(0); | 
|  | 124 | mScheduler.setDuration(handle, 10ns, 20ns); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 125 | } | 
|  | 126 |  | 
|  | 127 | TEST_F(SchedulerTest, validConnectionHandle) { | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 128 | const sp<IDisplayEventConnection> connection = | 
|  | 129 | mScheduler.createDisplayEventConnection(mConnectionHandle, | 
|  | 130 | ISurfaceComposer::eConfigChangedSuppress); | 
|  | 131 |  | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 132 | ASSERT_EQ(mEventThreadConnection, connection); | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 133 | EXPECT_TRUE(mScheduler.getEventConnection(mConnectionHandle)); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 134 |  | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 135 | EXPECT_CALL(*mEventThread, onHotplugReceived(PHYSICAL_DISPLAY_ID, false)).Times(1); | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 136 | mScheduler.onHotplugReceived(mConnectionHandle, PHYSICAL_DISPLAY_ID, false); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 137 |  | 
|  | 138 | EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(1); | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 139 | mScheduler.onScreenAcquired(mConnectionHandle); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 140 |  | 
|  | 141 | EXPECT_CALL(*mEventThread, onScreenReleased()).Times(1); | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 142 | mScheduler.onScreenReleased(mConnectionHandle); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 143 |  | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 144 | std::string output("dump"); | 
|  | 145 | EXPECT_CALL(*mEventThread, dump(output)).Times(1); | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 146 | mScheduler.dump(mConnectionHandle, output); | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 147 | EXPECT_FALSE(output.empty()); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 148 |  | 
| Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 149 | EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(1); | 
|  | 150 | mScheduler.setDuration(mConnectionHandle, 10ns, 20ns); | 
| Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 151 |  | 
|  | 152 | static constexpr size_t kEventConnections = 5; | 
| Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 153 | EXPECT_CALL(*mEventThread, getEventThreadConnectionCount()).WillOnce(Return(kEventConnections)); | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 154 | EXPECT_EQ(kEventConnections, mScheduler.getEventThreadConnectionCount(mConnectionHandle)); | 
| Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 155 | } | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 156 |  | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 157 | TEST_F(SchedulerTest, noLayerHistory) { | 
|  | 158 | // Layer history should not be created if there is a single config. | 
|  | 159 | ASSERT_FALSE(mScheduler.hasLayerHistory()); | 
|  | 160 |  | 
|  | 161 | TestableSurfaceFlinger flinger; | 
|  | 162 | mock::MockLayer layer(flinger.flinger()); | 
|  | 163 |  | 
|  | 164 | // Content detection should be no-op. | 
|  | 165 | mScheduler.registerLayer(&layer); | 
|  | 166 | mScheduler.recordLayerHistory(&layer, 0, LayerHistory::LayerUpdateType::Buffer); | 
|  | 167 |  | 
|  | 168 | constexpr bool kPowerStateNormal = true; | 
|  | 169 | mScheduler.setDisplayPowerState(kPowerStateNormal); | 
|  | 170 |  | 
|  | 171 | constexpr uint32_t kDisplayArea = 999'999; | 
|  | 172 | mScheduler.onPrimaryDisplayAreaChanged(kDisplayArea); | 
|  | 173 |  | 
| Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 174 | EXPECT_CALL(mSchedulerCallback, changeRefreshRate(_, _)).Times(0); | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 175 | mScheduler.chooseRefreshRateForContent(); | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 176 | } | 
|  | 177 |  | 
| Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 178 | } // namespace android |