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 | |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 17 | #include <common/test/FlagUtils.h> |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 18 | #include <ftl/fake_guard.h> |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 19 | #include <gmock/gmock.h> |
| 20 | #include <gtest/gtest.h> |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 21 | #include <log/log.h> |
| 22 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 23 | #include <mutex> |
| 24 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 25 | #include "Scheduler/EventThread.h" |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 26 | #include "Scheduler/RefreshRateSelector.h" |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 27 | #include "Scheduler/VSyncPredictor.h" |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 28 | #include "Scheduler/VSyncReactor.h" |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 29 | #include "TestableScheduler.h" |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 30 | #include "TestableSurfaceFlinger.h" |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 31 | #include "mock/DisplayHardware/MockDisplayMode.h" |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 32 | #include "mock/MockEventThread.h" |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 33 | #include "mock/MockLayer.h" |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 34 | #include "mock/MockSchedulerCallback.h" |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 35 | |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 36 | #include <FrontEnd/LayerHierarchy.h> |
ramindani | 7b32b3a | 2024-07-02 10:17:47 -0700 | [diff] [blame^] | 37 | #include <scheduler/FrameTime.h> |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 38 | |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 39 | #include <com_android_graphics_surfaceflinger_flags.h> |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 40 | #include "FpsOps.h" |
| 41 | |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 42 | using namespace com::android::graphics::surfaceflinger; |
| 43 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 44 | namespace android::scheduler { |
| 45 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 46 | using android::mock::createDisplayMode; |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 47 | using android::mock::createVrrDisplayMode; |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 48 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 49 | using testing::_; |
| 50 | using testing::Return; |
| 51 | |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 52 | namespace { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 53 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 54 | using MockEventThread = android::mock::EventThread; |
| 55 | using MockLayer = android::mock::MockLayer; |
| 56 | |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 57 | using LayerHierarchy = surfaceflinger::frontend::LayerHierarchy; |
| 58 | using LayerHierarchyBuilder = surfaceflinger::frontend::LayerHierarchyBuilder; |
| 59 | using RequestedLayerState = surfaceflinger::frontend::RequestedLayerState; |
| 60 | |
Ady Abraham | 77b4fb1 | 2024-03-05 17:51:53 -0800 | [diff] [blame] | 61 | class ZeroClock : public Clock { |
| 62 | public: |
| 63 | nsecs_t now() const override { return 0; } |
| 64 | }; |
| 65 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 66 | class SchedulerTest : public testing::Test { |
| 67 | protected: |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 68 | class MockEventThreadConnection : public android::EventThreadConnection { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 69 | public: |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 70 | explicit MockEventThreadConnection(EventThread* eventThread) |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 71 | : EventThreadConnection(eventThread, /*callingUid*/ static_cast<uid_t>(0)) {} |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 72 | ~MockEventThreadConnection() = default; |
| 73 | |
Huihong Luo | 6fac523 | 2021-11-22 16:05:23 -0800 | [diff] [blame] | 74 | MOCK_METHOD1(stealReceiveChannel, binder::Status(gui::BitTube* outChannel)); |
| 75 | MOCK_METHOD1(setVsyncRate, binder::Status(int count)); |
| 76 | MOCK_METHOD0(requestNextVsync, binder::Status()); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 79 | SchedulerTest(); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 80 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 81 | static constexpr PhysicalDisplayId kDisplayId1 = PhysicalDisplayId::fromPort(255u); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 82 | static inline const ftl::NonNull<DisplayModePtr> kDisplay1Mode60 = |
| 83 | ftl::as_non_null(createDisplayMode(kDisplayId1, DisplayModeId(0), 60_Hz)); |
| 84 | static inline const ftl::NonNull<DisplayModePtr> kDisplay1Mode120 = |
| 85 | ftl::as_non_null(createDisplayMode(kDisplayId1, DisplayModeId(1), 120_Hz)); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 86 | static inline const DisplayModes kDisplay1Modes = makeModes(kDisplay1Mode60, kDisplay1Mode120); |
| 87 | |
| 88 | static constexpr PhysicalDisplayId kDisplayId2 = PhysicalDisplayId::fromPort(254u); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 89 | static inline const ftl::NonNull<DisplayModePtr> kDisplay2Mode60 = |
| 90 | ftl::as_non_null(createDisplayMode(kDisplayId2, DisplayModeId(0), 60_Hz)); |
| 91 | static inline const ftl::NonNull<DisplayModePtr> kDisplay2Mode120 = |
| 92 | ftl::as_non_null(createDisplayMode(kDisplayId2, DisplayModeId(1), 120_Hz)); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 93 | static inline const DisplayModes kDisplay2Modes = makeModes(kDisplay2Mode60, kDisplay2Mode120); |
| 94 | |
| 95 | static constexpr PhysicalDisplayId kDisplayId3 = PhysicalDisplayId::fromPort(253u); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 96 | static inline const ftl::NonNull<DisplayModePtr> kDisplay3Mode60 = |
| 97 | ftl::as_non_null(createDisplayMode(kDisplayId3, DisplayModeId(0), 60_Hz)); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 98 | static inline const DisplayModes kDisplay3Modes = makeModes(kDisplay3Mode60); |
Marin Shalamanov | 2cde100 | 2021-06-08 19:50:10 +0200 | [diff] [blame] | 99 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 100 | std::shared_ptr<RefreshRateSelector> mSelector = |
| 101 | std::make_shared<RefreshRateSelector>(makeModes(kDisplay1Mode60), |
| 102 | kDisplay1Mode60->getId()); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 103 | |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 104 | mock::SchedulerCallback mSchedulerCallback; |
Leon Scroggins III | 823d4ca | 2023-12-12 16:57:34 -0500 | [diff] [blame] | 105 | TestableSurfaceFlinger mFlinger; |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 106 | TestableScheduler* mScheduler = new TestableScheduler{mSelector, mFlinger, mSchedulerCallback}; |
Vishnu Nair | a029228 | 2023-12-16 14:32:00 -0800 | [diff] [blame] | 107 | surfaceflinger::frontend::LayerHierarchyBuilder mLayerHierarchyBuilder; |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 108 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 109 | MockEventThread* mEventThread; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 110 | sp<MockEventThreadConnection> mEventThreadConnection; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | SchedulerTest::SchedulerTest() { |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 114 | auto eventThread = std::make_unique<MockEventThread>(); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 115 | mEventThread = eventThread.get(); |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 116 | EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)).WillOnce(Return(0)); |
| 117 | |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 118 | mEventThreadConnection = sp<MockEventThreadConnection>::make(mEventThread); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 119 | |
| 120 | // createConnection call to scheduler makes a createEventConnection call to EventThread. Make |
| 121 | // sure that call gets executed and returns an EventThread::Connection object. |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 122 | EXPECT_CALL(*mEventThread, createEventConnection(_, _)) |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 123 | .WillRepeatedly(Return(mEventThreadConnection)); |
| 124 | |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 125 | mScheduler->setEventThread(Cycle::Render, std::move(eventThread)); |
Ady Abraham | a0a1627 | 2021-03-03 15:23:35 -0800 | [diff] [blame] | 126 | |
| 127 | mFlinger.resetScheduler(mScheduler); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 130 | } // namespace |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 131 | |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 132 | TEST_F(SchedulerTest, registerDisplay) FTL_FAKE_GUARD(kMainThreadContext) { |
Dominik Laskowski | 008bec0 | 2023-03-14 12:04:58 -0400 | [diff] [blame] | 133 | // Hardware VSYNC should not change if the display is already registered. |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 134 | EXPECT_CALL(mSchedulerCallback, requestHardwareVsync(kDisplayId1, false)).Times(0); |
Dominik Laskowski | 008bec0 | 2023-03-14 12:04:58 -0400 | [diff] [blame] | 135 | mScheduler->registerDisplay(kDisplayId1, |
| 136 | std::make_shared<RefreshRateSelector>(kDisplay1Modes, |
| 137 | kDisplay1Mode60->getId())); |
| 138 | |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 139 | // TODO(b/241285191): Restore once VsyncSchedule::getPendingHardwareVsyncState is called by |
| 140 | // Scheduler::setDisplayPowerMode rather than SF::setPowerModeInternal. |
| 141 | #if 0 |
Dominik Laskowski | 008bec0 | 2023-03-14 12:04:58 -0400 | [diff] [blame] | 142 | // Hardware VSYNC should be disabled for newly registered displays. |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 143 | EXPECT_CALL(mSchedulerCallback, requestHardwareVsync(kDisplayId2, false)).Times(1); |
| 144 | EXPECT_CALL(mSchedulerCallback, requestHardwareVsync(kDisplayId3, false)).Times(1); |
| 145 | #endif |
Dominik Laskowski | 008bec0 | 2023-03-14 12:04:58 -0400 | [diff] [blame] | 146 | |
| 147 | mScheduler->registerDisplay(kDisplayId2, |
| 148 | std::make_shared<RefreshRateSelector>(kDisplay2Modes, |
| 149 | kDisplay2Mode60->getId())); |
| 150 | mScheduler->registerDisplay(kDisplayId3, |
| 151 | std::make_shared<RefreshRateSelector>(kDisplay3Modes, |
| 152 | kDisplay3Mode60->getId())); |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 153 | |
| 154 | EXPECT_FALSE(mScheduler->getVsyncSchedule(kDisplayId1)->getPendingHardwareVsyncState()); |
| 155 | EXPECT_FALSE(mScheduler->getVsyncSchedule(kDisplayId2)->getPendingHardwareVsyncState()); |
| 156 | EXPECT_FALSE(mScheduler->getVsyncSchedule(kDisplayId3)->getPendingHardwareVsyncState()); |
Dominik Laskowski | 008bec0 | 2023-03-14 12:04:58 -0400 | [diff] [blame] | 157 | } |
| 158 | |
Marin Shalamanov | 2cde100 | 2021-06-08 19:50:10 +0200 | [diff] [blame] | 159 | TEST_F(SchedulerTest, chooseRefreshRateForContentIsNoopWhenModeSwitchingIsNotSupported) { |
| 160 | // The layer is registered at creation time and deregistered at destruction time. |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 161 | sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger()); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 162 | |
Marin Shalamanov | 2cde100 | 2021-06-08 19:50:10 +0200 | [diff] [blame] | 163 | // recordLayerHistory should be a noop |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 164 | ASSERT_EQ(0u, mScheduler->getNumActiveLayers()); |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 165 | mScheduler->recordLayerHistory(layer->getSequence(), layer->getLayerProps(), 0, 0, |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 166 | LayerHistory::LayerUpdateType::Buffer); |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 167 | ASSERT_EQ(0u, mScheduler->getNumActiveLayers()); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 168 | |
Rachel Lee | 6a9731d | 2022-06-06 17:08:14 -0700 | [diff] [blame] | 169 | constexpr hal::PowerMode kPowerModeOn = hal::PowerMode::ON; |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 170 | FTL_FAKE_GUARD(kMainThreadContext, mScheduler->setDisplayPowerMode(kDisplayId1, kPowerModeOn)); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 171 | |
| 172 | constexpr uint32_t kDisplayArea = 999'999; |
Ady Abraham | ed3290f | 2021-05-17 15:12:14 -0700 | [diff] [blame] | 173 | mScheduler->onActiveDisplayAreaChanged(kDisplayArea); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 174 | |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 175 | EXPECT_CALL(mSchedulerCallback, requestDisplayModes(_)).Times(0); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 176 | mScheduler->chooseRefreshRateForContent(/*LayerHierarchy*/ nullptr, |
| 177 | /*updateAttachedChoreographer*/ false); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Marin Shalamanov | 2cde100 | 2021-06-08 19:50:10 +0200 | [diff] [blame] | 180 | TEST_F(SchedulerTest, updateDisplayModes) { |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 181 | ASSERT_EQ(0u, mScheduler->layerHistorySize()); |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 182 | sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger()); |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 183 | ASSERT_EQ(1u, mScheduler->layerHistorySize()); |
Marin Shalamanov | 2cde100 | 2021-06-08 19:50:10 +0200 | [diff] [blame] | 184 | |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 185 | // Replace `mSelector` with a new `RefreshRateSelector` that has different display modes. |
| 186 | mScheduler->registerDisplay(kDisplayId1, |
| 187 | std::make_shared<RefreshRateSelector>(kDisplay1Modes, |
| 188 | kDisplay1Mode60->getId())); |
Marin Shalamanov | 2cde100 | 2021-06-08 19:50:10 +0200 | [diff] [blame] | 189 | |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 190 | ASSERT_EQ(0u, mScheduler->getNumActiveLayers()); |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 191 | mScheduler->recordLayerHistory(layer->getSequence(), layer->getLayerProps(), 0, 0, |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 192 | LayerHistory::LayerUpdateType::Buffer); |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 193 | ASSERT_EQ(1u, mScheduler->getNumActiveLayers()); |
Marin Shalamanov | 2cde100 | 2021-06-08 19:50:10 +0200 | [diff] [blame] | 194 | } |
| 195 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 196 | TEST_F(SchedulerTest, dispatchCachedReportedMode) { |
| 197 | mScheduler->clearCachedReportedMode(); |
| 198 | |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 199 | EXPECT_CALL(*mEventThread, onModeChanged(_)).Times(0); |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 200 | EXPECT_NO_FATAL_FAILURE(mScheduler->dispatchCachedReportedMode()); |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 203 | TEST_F(SchedulerTest, calculateMaxAcquiredBufferCount) { |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 204 | EXPECT_EQ(1, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 30ms)); |
| 205 | EXPECT_EQ(2, mFlinger.calculateMaxAcquiredBufferCount(90_Hz, 30ms)); |
| 206 | EXPECT_EQ(3, mFlinger.calculateMaxAcquiredBufferCount(120_Hz, 30ms)); |
Ady Abraham | 564f9de | 2021-02-03 18:34:33 -0800 | [diff] [blame] | 207 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 208 | EXPECT_EQ(2, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 40ms)); |
Ady Abraham | 564f9de | 2021-02-03 18:34:33 -0800 | [diff] [blame] | 209 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 210 | EXPECT_EQ(1, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 10ms)); |
Lloyd Pique | a1456c1 | 2023-05-17 12:11:15 -0700 | [diff] [blame] | 211 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 212 | const auto savedMinAcquiredBuffers = mFlinger.mutableMinAcquiredBuffers(); |
| 213 | mFlinger.mutableMinAcquiredBuffers() = 2; |
Lloyd Pique | a1456c1 | 2023-05-17 12:11:15 -0700 | [diff] [blame] | 214 | EXPECT_EQ(2, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 10ms)); |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 215 | mFlinger.mutableMinAcquiredBuffers() = savedMinAcquiredBuffers; |
Ady Abraham | 564f9de | 2021-02-03 18:34:33 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Marin Shalamanov | 4c7831e | 2021-06-08 20:44:06 +0200 | [diff] [blame] | 218 | MATCHER(Is120Hz, "") { |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 219 | return isApproxEqual(arg.front().mode.fps, 120_Hz); |
Marin Shalamanov | 4c7831e | 2021-06-08 20:44:06 +0200 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | TEST_F(SchedulerTest, chooseRefreshRateForContentSelectsMaxRefreshRate) { |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 223 | mScheduler->registerDisplay(kDisplayId1, |
| 224 | std::make_shared<RefreshRateSelector>(kDisplay1Modes, |
| 225 | kDisplay1Mode60->getId())); |
Marin Shalamanov | 4c7831e | 2021-06-08 20:44:06 +0200 | [diff] [blame] | 226 | |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 227 | const sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger()); |
| 228 | EXPECT_CALL(*layer, isVisible()).WillOnce(Return(true)); |
Marin Shalamanov | 4c7831e | 2021-06-08 20:44:06 +0200 | [diff] [blame] | 229 | |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 230 | mScheduler->recordLayerHistory(layer->getSequence(), layer->getLayerProps(), 0, systemTime(), |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 231 | LayerHistory::LayerUpdateType::Buffer); |
Marin Shalamanov | 4c7831e | 2021-06-08 20:44:06 +0200 | [diff] [blame] | 232 | |
Rachel Lee | 6a9731d | 2022-06-06 17:08:14 -0700 | [diff] [blame] | 233 | constexpr hal::PowerMode kPowerModeOn = hal::PowerMode::ON; |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 234 | FTL_FAKE_GUARD(kMainThreadContext, mScheduler->setDisplayPowerMode(kDisplayId1, kPowerModeOn)); |
Marin Shalamanov | 4c7831e | 2021-06-08 20:44:06 +0200 | [diff] [blame] | 235 | |
| 236 | constexpr uint32_t kDisplayArea = 999'999; |
Ady Abraham | ed3290f | 2021-05-17 15:12:14 -0700 | [diff] [blame] | 237 | mScheduler->onActiveDisplayAreaChanged(kDisplayArea); |
Marin Shalamanov | 4c7831e | 2021-06-08 20:44:06 +0200 | [diff] [blame] | 238 | |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 239 | EXPECT_CALL(mSchedulerCallback, requestDisplayModes(Is120Hz())).Times(1); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 240 | mScheduler->chooseRefreshRateForContent(/*LayerHierarchy*/ nullptr, |
| 241 | /*updateAttachedChoreographer*/ false); |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 242 | |
| 243 | // No-op if layer requirements have not changed. |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 244 | EXPECT_CALL(mSchedulerCallback, requestDisplayModes(_)).Times(0); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 245 | mScheduler->chooseRefreshRateForContent(/*LayerHierarchy*/ nullptr, |
| 246 | /*updateAttachedChoreographer*/ false); |
Marin Shalamanov | 4c7831e | 2021-06-08 20:44:06 +0200 | [diff] [blame] | 247 | } |
| 248 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 249 | TEST_F(SchedulerTest, chooseDisplayModesSingleDisplay) { |
Dominik Laskowski | b5a094b | 2022-10-27 12:00:12 -0400 | [diff] [blame] | 250 | mScheduler->registerDisplay(kDisplayId1, |
| 251 | std::make_shared<RefreshRateSelector>(kDisplay1Modes, |
| 252 | kDisplay1Mode60->getId())); |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 253 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 254 | std::vector<RefreshRateSelector::LayerRequirement> layers = |
| 255 | std::vector<RefreshRateSelector::LayerRequirement>({{.weight = 1.f}, {.weight = 1.f}}); |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 256 | mScheduler->setContentRequirements(layers); |
| 257 | GlobalSignals globalSignals = {.idle = true}; |
| 258 | mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals); |
| 259 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 260 | using DisplayModeChoice = TestableScheduler::DisplayModeChoice; |
| 261 | |
| 262 | auto modeChoices = mScheduler->chooseDisplayModes(); |
| 263 | ASSERT_EQ(1u, modeChoices.size()); |
| 264 | |
| 265 | auto choice = modeChoices.get(kDisplayId1); |
| 266 | ASSERT_TRUE(choice); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 267 | EXPECT_EQ(choice->get(), DisplayModeChoice({60_Hz, kDisplay1Mode60}, globalSignals)); |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 268 | |
| 269 | globalSignals = {.idle = false}; |
| 270 | mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 271 | |
| 272 | modeChoices = mScheduler->chooseDisplayModes(); |
| 273 | ASSERT_EQ(1u, modeChoices.size()); |
| 274 | |
| 275 | choice = modeChoices.get(kDisplayId1); |
| 276 | ASSERT_TRUE(choice); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 277 | EXPECT_EQ(choice->get(), DisplayModeChoice({120_Hz, kDisplay1Mode120}, globalSignals)); |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 278 | |
| 279 | globalSignals = {.touch = true}; |
| 280 | mScheduler->replaceTouchTimer(10); |
| 281 | mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals); |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 282 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 283 | modeChoices = mScheduler->chooseDisplayModes(); |
| 284 | ASSERT_EQ(1u, modeChoices.size()); |
| 285 | |
| 286 | choice = modeChoices.get(kDisplayId1); |
| 287 | ASSERT_TRUE(choice); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 288 | EXPECT_EQ(choice->get(), DisplayModeChoice({120_Hz, kDisplay1Mode120}, globalSignals)); |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Rachel Lee | 6ed96c9 | 2024-02-07 14:37:38 -0800 | [diff] [blame] | 291 | TEST_F(SchedulerTest, chooseDisplayModesSingleDisplayHighHintTouchSignal) { |
| 292 | mScheduler->registerDisplay(kDisplayId1, |
| 293 | std::make_shared<RefreshRateSelector>(kDisplay1Modes, |
| 294 | kDisplay1Mode60->getId())); |
| 295 | |
| 296 | using DisplayModeChoice = TestableScheduler::DisplayModeChoice; |
| 297 | |
| 298 | std::vector<RefreshRateSelector::LayerRequirement> layers = |
| 299 | std::vector<RefreshRateSelector::LayerRequirement>({{.weight = 1.f}, {.weight = 1.f}}); |
| 300 | auto& lr1 = layers[0]; |
| 301 | auto& lr2 = layers[1]; |
| 302 | |
| 303 | // Scenario that is similar to game. Expects no touch boost. |
| 304 | lr1.vote = RefreshRateSelector::LayerVoteType::ExplicitCategory; |
| 305 | lr1.frameRateCategory = FrameRateCategory::HighHint; |
| 306 | lr1.name = "ExplicitCategory HighHint"; |
| 307 | lr2.vote = RefreshRateSelector::LayerVoteType::ExplicitDefault; |
| 308 | lr2.desiredRefreshRate = 30_Hz; |
| 309 | lr2.name = "30Hz ExplicitDefault"; |
| 310 | mScheduler->setContentRequirements(layers); |
| 311 | auto modeChoices = mScheduler->chooseDisplayModes(); |
| 312 | ASSERT_EQ(1u, modeChoices.size()); |
| 313 | auto choice = modeChoices.get(kDisplayId1); |
| 314 | ASSERT_TRUE(choice); |
| 315 | EXPECT_EQ(choice->get(), DisplayModeChoice({60_Hz, kDisplay1Mode60}, {.touch = false})); |
| 316 | |
| 317 | // Scenario that is similar to video playback and interaction. Expects touch boost. |
| 318 | lr1.vote = RefreshRateSelector::LayerVoteType::ExplicitCategory; |
| 319 | lr1.frameRateCategory = FrameRateCategory::HighHint; |
| 320 | lr1.name = "ExplicitCategory HighHint"; |
| 321 | lr2.vote = RefreshRateSelector::LayerVoteType::ExplicitExactOrMultiple; |
| 322 | lr2.desiredRefreshRate = 30_Hz; |
| 323 | lr2.name = "30Hz ExplicitExactOrMultiple"; |
| 324 | mScheduler->setContentRequirements(layers); |
| 325 | modeChoices = mScheduler->chooseDisplayModes(); |
| 326 | ASSERT_EQ(1u, modeChoices.size()); |
| 327 | choice = modeChoices.get(kDisplayId1); |
| 328 | ASSERT_TRUE(choice); |
| 329 | EXPECT_EQ(choice->get(), DisplayModeChoice({120_Hz, kDisplay1Mode120}, {.touch = true})); |
| 330 | |
| 331 | // Scenario with explicit category and HighHint. Expects touch boost. |
| 332 | lr1.vote = RefreshRateSelector::LayerVoteType::ExplicitCategory; |
| 333 | lr1.frameRateCategory = FrameRateCategory::HighHint; |
| 334 | lr1.name = "ExplicitCategory HighHint"; |
| 335 | lr2.vote = RefreshRateSelector::LayerVoteType::ExplicitCategory; |
| 336 | lr2.frameRateCategory = FrameRateCategory::Low; |
| 337 | lr2.name = "ExplicitCategory Low"; |
| 338 | mScheduler->setContentRequirements(layers); |
| 339 | modeChoices = mScheduler->chooseDisplayModes(); |
| 340 | ASSERT_EQ(1u, modeChoices.size()); |
| 341 | choice = modeChoices.get(kDisplayId1); |
| 342 | ASSERT_TRUE(choice); |
| 343 | EXPECT_EQ(choice->get(), DisplayModeChoice({120_Hz, kDisplay1Mode120}, {.touch = true})); |
| 344 | } |
| 345 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 346 | TEST_F(SchedulerTest, chooseDisplayModesMultipleDisplays) { |
Dominik Laskowski | 3ff44c7 | 2024-06-17 11:03:30 -0400 | [diff] [blame] | 347 | constexpr PhysicalDisplayId kActiveDisplayId = kDisplayId1; |
Dominik Laskowski | b5a094b | 2022-10-27 12:00:12 -0400 | [diff] [blame] | 348 | mScheduler->registerDisplay(kDisplayId1, |
| 349 | std::make_shared<RefreshRateSelector>(kDisplay1Modes, |
Dominik Laskowski | 3ff44c7 | 2024-06-17 11:03:30 -0400 | [diff] [blame] | 350 | kDisplay1Mode60->getId()), |
| 351 | kActiveDisplayId); |
Dominik Laskowski | b5a094b | 2022-10-27 12:00:12 -0400 | [diff] [blame] | 352 | mScheduler->registerDisplay(kDisplayId2, |
| 353 | std::make_shared<RefreshRateSelector>(kDisplay2Modes, |
Dominik Laskowski | 3ff44c7 | 2024-06-17 11:03:30 -0400 | [diff] [blame] | 354 | kDisplay2Mode60->getId()), |
| 355 | kActiveDisplayId); |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 356 | |
Dominik Laskowski | 086507b | 2024-05-16 15:33:16 -0400 | [diff] [blame] | 357 | mScheduler->setDisplayPowerMode(kDisplayId1, hal::PowerMode::ON); |
| 358 | mScheduler->setDisplayPowerMode(kDisplayId2, hal::PowerMode::ON); |
| 359 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 360 | using DisplayModeChoice = TestableScheduler::DisplayModeChoice; |
| 361 | TestableScheduler::DisplayModeChoiceMap expectedChoices; |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 362 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 363 | { |
| 364 | const GlobalSignals globalSignals = {.idle = true}; |
| 365 | expectedChoices = |
| 366 | ftl::init::map<const PhysicalDisplayId&, |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 367 | DisplayModeChoice>(kDisplayId1, |
| 368 | FrameRateMode{60_Hz, kDisplay1Mode60}, |
| 369 | globalSignals)(kDisplayId2, |
| 370 | FrameRateMode{60_Hz, |
| 371 | kDisplay2Mode60}, |
Dominik Laskowski | 9e88d62 | 2024-03-06 17:42:39 -0500 | [diff] [blame] | 372 | GlobalSignals{}); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 373 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 374 | std::vector<RefreshRateSelector::LayerRequirement> layers = {{.weight = 1.f}, |
| 375 | {.weight = 1.f}}; |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 376 | mScheduler->setContentRequirements(layers); |
| 377 | mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals); |
| 378 | |
| 379 | const auto actualChoices = mScheduler->chooseDisplayModes(); |
| 380 | EXPECT_EQ(expectedChoices, actualChoices); |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 381 | } |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 382 | { |
| 383 | const GlobalSignals globalSignals = {.idle = false}; |
| 384 | expectedChoices = |
| 385 | ftl::init::map<const PhysicalDisplayId&, |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 386 | DisplayModeChoice>(kDisplayId1, |
| 387 | FrameRateMode{120_Hz, kDisplay1Mode120}, |
| 388 | globalSignals)(kDisplayId2, |
| 389 | FrameRateMode{120_Hz, |
| 390 | kDisplay2Mode120}, |
Dominik Laskowski | 9e88d62 | 2024-03-06 17:42:39 -0500 | [diff] [blame] | 391 | GlobalSignals{}); |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 392 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 393 | mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals); |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 394 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 395 | const auto actualChoices = mScheduler->chooseDisplayModes(); |
| 396 | EXPECT_EQ(expectedChoices, actualChoices); |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 397 | } |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 398 | { |
| 399 | const GlobalSignals globalSignals = {.touch = true}; |
| 400 | mScheduler->replaceTouchTimer(10); |
| 401 | mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals); |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 402 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 403 | expectedChoices = |
| 404 | ftl::init::map<const PhysicalDisplayId&, |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 405 | DisplayModeChoice>(kDisplayId1, |
| 406 | FrameRateMode{120_Hz, kDisplay1Mode120}, |
| 407 | globalSignals)(kDisplayId2, |
| 408 | FrameRateMode{120_Hz, |
| 409 | kDisplay2Mode120}, |
Dominik Laskowski | 9e88d62 | 2024-03-06 17:42:39 -0500 | [diff] [blame] | 410 | GlobalSignals{}); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 411 | |
| 412 | const auto actualChoices = mScheduler->chooseDisplayModes(); |
| 413 | EXPECT_EQ(expectedChoices, actualChoices); |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 414 | } |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 415 | { |
ramindani | 22f2ead | 2023-04-21 10:27:11 -0700 | [diff] [blame] | 416 | // The kDisplayId3 does not support 120Hz, The pacesetter display rate is chosen to be 120 |
| 417 | // Hz. In this case only the display kDisplayId3 choose 60Hz as it does not support 120Hz. |
Dominik Laskowski | 3ff44c7 | 2024-06-17 11:03:30 -0400 | [diff] [blame] | 418 | mScheduler->registerDisplay(kDisplayId3, |
| 419 | std::make_shared<RefreshRateSelector>(kDisplay3Modes, |
| 420 | kDisplay3Mode60->getId()), |
| 421 | kActiveDisplayId); |
Dominik Laskowski | 086507b | 2024-05-16 15:33:16 -0400 | [diff] [blame] | 422 | mScheduler->setDisplayPowerMode(kDisplayId3, hal::PowerMode::ON); |
Dominik Laskowski | 327d609 | 2022-10-11 18:05:08 -0400 | [diff] [blame] | 423 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 424 | const GlobalSignals globalSignals = {.touch = true}; |
| 425 | mScheduler->replaceTouchTimer(10); |
| 426 | mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals); |
ramindani | 7c48728 | 2022-10-10 16:17:51 -0700 | [diff] [blame] | 427 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 428 | expectedChoices = ftl::init::map< |
| 429 | const PhysicalDisplayId&, |
ramindani | 22f2ead | 2023-04-21 10:27:11 -0700 | [diff] [blame] | 430 | DisplayModeChoice>(kDisplayId1, FrameRateMode{120_Hz, kDisplay1Mode120}, |
| 431 | globalSignals)(kDisplayId2, |
| 432 | FrameRateMode{120_Hz, kDisplay2Mode120}, |
Dominik Laskowski | 9e88d62 | 2024-03-06 17:42:39 -0500 | [diff] [blame] | 433 | GlobalSignals{})(kDisplayId3, |
| 434 | FrameRateMode{60_Hz, |
| 435 | kDisplay3Mode60}, |
| 436 | GlobalSignals{}); |
ramindani | 22f2ead | 2023-04-21 10:27:11 -0700 | [diff] [blame] | 437 | |
| 438 | const auto actualChoices = mScheduler->chooseDisplayModes(); |
| 439 | EXPECT_EQ(expectedChoices, actualChoices); |
| 440 | } |
| 441 | { |
| 442 | // We should choose 60Hz despite the touch signal as pacesetter only supports 60Hz |
| 443 | mScheduler->setPacesetterDisplay(kDisplayId3); |
| 444 | const GlobalSignals globalSignals = {.touch = true}; |
| 445 | mScheduler->replaceTouchTimer(10); |
| 446 | mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals); |
| 447 | |
| 448 | expectedChoices = ftl::init::map< |
| 449 | const PhysicalDisplayId&, |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 450 | DisplayModeChoice>(kDisplayId1, FrameRateMode{60_Hz, kDisplay1Mode60}, |
Dominik Laskowski | 9e88d62 | 2024-03-06 17:42:39 -0500 | [diff] [blame] | 451 | GlobalSignals{})(kDisplayId2, |
| 452 | FrameRateMode{60_Hz, kDisplay2Mode60}, |
| 453 | GlobalSignals{})(kDisplayId3, |
| 454 | FrameRateMode{60_Hz, |
| 455 | kDisplay3Mode60}, |
| 456 | globalSignals); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 457 | |
| 458 | const auto actualChoices = mScheduler->chooseDisplayModes(); |
| 459 | EXPECT_EQ(expectedChoices, actualChoices); |
ramindani | 7c48728 | 2022-10-10 16:17:51 -0700 | [diff] [blame] | 460 | } |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 463 | TEST_F(SchedulerTest, onFrameSignalMultipleDisplays) { |
Dominik Laskowski | 3ff44c7 | 2024-06-17 11:03:30 -0400 | [diff] [blame] | 464 | constexpr PhysicalDisplayId kActiveDisplayId = kDisplayId1; |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 465 | mScheduler->registerDisplay(kDisplayId1, |
| 466 | std::make_shared<RefreshRateSelector>(kDisplay1Modes, |
Dominik Laskowski | 3ff44c7 | 2024-06-17 11:03:30 -0400 | [diff] [blame] | 467 | kDisplay1Mode60->getId()), |
| 468 | kActiveDisplayId); |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 469 | mScheduler->registerDisplay(kDisplayId2, |
| 470 | std::make_shared<RefreshRateSelector>(kDisplay2Modes, |
Dominik Laskowski | 3ff44c7 | 2024-06-17 11:03:30 -0400 | [diff] [blame] | 471 | kDisplay2Mode60->getId()), |
| 472 | kActiveDisplayId); |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 473 | |
| 474 | using VsyncIds = std::vector<std::pair<PhysicalDisplayId, VsyncId>>; |
| 475 | |
| 476 | struct Compositor final : ICompositor { |
Dominik Laskowski | fb4b737 | 2023-11-22 09:56:54 -0500 | [diff] [blame] | 477 | explicit Compositor(TestableScheduler& scheduler) : scheduler(scheduler) {} |
| 478 | |
| 479 | TestableScheduler& scheduler; |
| 480 | |
| 481 | struct { |
| 482 | PhysicalDisplayId commit; |
| 483 | PhysicalDisplayId composite; |
| 484 | } pacesetterIds; |
| 485 | |
| 486 | struct { |
| 487 | VsyncIds commit; |
| 488 | VsyncIds composite; |
| 489 | } vsyncIds; |
| 490 | |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 491 | bool committed = true; |
Dominik Laskowski | fb4b737 | 2023-11-22 09:56:54 -0500 | [diff] [blame] | 492 | bool changePacesetter = false; |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 493 | |
| 494 | void configure() override {} |
| 495 | |
Dominik Laskowski | fb4b737 | 2023-11-22 09:56:54 -0500 | [diff] [blame] | 496 | bool commit(PhysicalDisplayId pacesetterId, |
| 497 | const scheduler::FrameTargets& targets) override { |
| 498 | pacesetterIds.commit = pacesetterId; |
| 499 | |
| 500 | vsyncIds.commit.clear(); |
| 501 | vsyncIds.composite.clear(); |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 502 | |
| 503 | for (const auto& [id, target] : targets) { |
Dominik Laskowski | fb4b737 | 2023-11-22 09:56:54 -0500 | [diff] [blame] | 504 | vsyncIds.commit.emplace_back(id, target->vsyncId()); |
| 505 | } |
| 506 | |
| 507 | if (changePacesetter) { |
| 508 | scheduler.setPacesetterDisplay(kDisplayId2); |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | return committed; |
| 512 | } |
| 513 | |
Dominik Laskowski | fb4b737 | 2023-11-22 09:56:54 -0500 | [diff] [blame] | 514 | CompositeResultsPerDisplay composite(PhysicalDisplayId pacesetterId, |
| 515 | const scheduler::FrameTargeters& targeters) override { |
| 516 | pacesetterIds.composite = pacesetterId; |
| 517 | |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 518 | CompositeResultsPerDisplay results; |
| 519 | |
Dominik Laskowski | fb4b737 | 2023-11-22 09:56:54 -0500 | [diff] [blame] | 520 | for (const auto& [id, targeter] : targeters) { |
| 521 | vsyncIds.composite.emplace_back(id, targeter->target().vsyncId()); |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 522 | results.try_emplace(id, |
| 523 | CompositeResult{.compositionCoverage = |
| 524 | CompositionCoverage::Hwc}); |
| 525 | } |
| 526 | |
| 527 | return results; |
| 528 | } |
| 529 | |
| 530 | void sample() override {} |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 531 | void sendNotifyExpectedPresentHint(PhysicalDisplayId) override {} |
Dominik Laskowski | fb4b737 | 2023-11-22 09:56:54 -0500 | [diff] [blame] | 532 | } compositor(*mScheduler); |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 533 | |
| 534 | mScheduler->doFrameSignal(compositor, VsyncId(42)); |
| 535 | |
Dominik Laskowski | fb4b737 | 2023-11-22 09:56:54 -0500 | [diff] [blame] | 536 | const auto makeVsyncIds = [](VsyncId vsyncId, bool swap = false) -> VsyncIds { |
| 537 | if (swap) { |
| 538 | return {{kDisplayId2, vsyncId}, {kDisplayId1, vsyncId}}; |
| 539 | } else { |
| 540 | return {{kDisplayId1, vsyncId}, {kDisplayId2, vsyncId}}; |
| 541 | } |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 542 | }; |
| 543 | |
Dominik Laskowski | fb4b737 | 2023-11-22 09:56:54 -0500 | [diff] [blame] | 544 | EXPECT_EQ(kDisplayId1, compositor.pacesetterIds.commit); |
| 545 | EXPECT_EQ(kDisplayId1, compositor.pacesetterIds.composite); |
| 546 | EXPECT_EQ(makeVsyncIds(VsyncId(42)), compositor.vsyncIds.commit); |
| 547 | EXPECT_EQ(makeVsyncIds(VsyncId(42)), compositor.vsyncIds.composite); |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 548 | |
Dominik Laskowski | fb4b737 | 2023-11-22 09:56:54 -0500 | [diff] [blame] | 549 | // FrameTargets should be updated despite the skipped commit. |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 550 | compositor.committed = false; |
| 551 | mScheduler->doFrameSignal(compositor, VsyncId(43)); |
| 552 | |
Dominik Laskowski | fb4b737 | 2023-11-22 09:56:54 -0500 | [diff] [blame] | 553 | EXPECT_EQ(kDisplayId1, compositor.pacesetterIds.commit); |
| 554 | EXPECT_EQ(kDisplayId1, compositor.pacesetterIds.composite); |
| 555 | EXPECT_EQ(makeVsyncIds(VsyncId(43)), compositor.vsyncIds.commit); |
| 556 | EXPECT_TRUE(compositor.vsyncIds.composite.empty()); |
| 557 | |
| 558 | // The pacesetter may change during commit. |
| 559 | compositor.committed = true; |
| 560 | compositor.changePacesetter = true; |
| 561 | mScheduler->doFrameSignal(compositor, VsyncId(44)); |
| 562 | |
| 563 | EXPECT_EQ(kDisplayId1, compositor.pacesetterIds.commit); |
| 564 | EXPECT_EQ(kDisplayId2, compositor.pacesetterIds.composite); |
| 565 | EXPECT_EQ(makeVsyncIds(VsyncId(44)), compositor.vsyncIds.commit); |
| 566 | EXPECT_EQ(makeVsyncIds(VsyncId(44), true), compositor.vsyncIds.composite); |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 567 | } |
| 568 | |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 569 | TEST_F(SchedulerTest, nextFrameIntervalTest) { |
| 570 | SET_FLAG_FOR_TEST(flags::vrr_config, true); |
| 571 | |
| 572 | static constexpr size_t kHistorySize = 10; |
| 573 | static constexpr size_t kMinimumSamplesForPrediction = 6; |
| 574 | static constexpr size_t kOutlierTolerancePercent = 25; |
| 575 | const auto refreshRate = Fps::fromPeriodNsecs(500); |
| 576 | auto frameRate = Fps::fromPeriodNsecs(1000); |
| 577 | |
| 578 | const ftl::NonNull<DisplayModePtr> kMode = ftl::as_non_null( |
| 579 | createVrrDisplayMode(DisplayModeId(0), refreshRate, |
| 580 | hal::VrrConfig{.minFrameIntervalNs = static_cast<int32_t>( |
| 581 | frameRate.getPeriodNsecs())})); |
| 582 | std::shared_ptr<VSyncPredictor> vrrTracker = |
Ady Abraham | 77b4fb1 | 2024-03-05 17:51:53 -0800 | [diff] [blame] | 583 | std::make_shared<VSyncPredictor>(std::make_unique<ZeroClock>(), kMode, kHistorySize, |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 584 | kMinimumSamplesForPrediction, |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 585 | kOutlierTolerancePercent); |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 586 | std::shared_ptr<RefreshRateSelector> vrrSelectorPtr = |
| 587 | std::make_shared<RefreshRateSelector>(makeModes(kMode), kMode->getId()); |
| 588 | TestableScheduler scheduler{std::make_unique<android::mock::VsyncController>(), |
| 589 | vrrTracker, |
| 590 | vrrSelectorPtr, |
Leon Scroggins III | 823d4ca | 2023-12-12 16:57:34 -0500 | [diff] [blame] | 591 | mFlinger.getFactory(), |
| 592 | mFlinger.getTimeStats(), |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 593 | mSchedulerCallback}; |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 594 | |
Dominik Laskowski | 3ff44c7 | 2024-06-17 11:03:30 -0400 | [diff] [blame] | 595 | scheduler.registerDisplay(kMode->getPhysicalDisplayId(), vrrSelectorPtr, std::nullopt, |
| 596 | vrrTracker); |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 597 | vrrSelectorPtr->setActiveMode(kMode->getId(), frameRate); |
Ady Abraham | ee6365b | 2024-03-06 14:31:45 -0800 | [diff] [blame] | 598 | scheduler.setRenderRate(kMode->getPhysicalDisplayId(), frameRate, /*applyImmediately*/ false); |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 599 | vrrTracker->addVsyncTimestamp(0); |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 600 | // Set 1000 as vsync seq #0 |
| 601 | vrrTracker->nextAnticipatedVSyncTimeFrom(700); |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 602 | |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 603 | EXPECT_EQ(Fps::fromPeriodNsecs(1000), |
| 604 | scheduler.getNextFrameInterval(kMode->getPhysicalDisplayId(), |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 605 | TimePoint::fromNs(1000))); |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 606 | EXPECT_EQ(Fps::fromPeriodNsecs(1000), |
| 607 | scheduler.getNextFrameInterval(kMode->getPhysicalDisplayId(), |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 608 | TimePoint::fromNs(2000))); |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 609 | |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 610 | // Not crossing the min frame period |
ramindani | 7b32b3a | 2024-07-02 10:17:47 -0700 | [diff] [blame^] | 611 | vrrTracker->onFrameBegin(TimePoint::fromNs(2000), |
| 612 | {TimePoint::fromNs(1500), TimePoint::fromNs(1500)}); |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 613 | EXPECT_EQ(Fps::fromPeriodNsecs(1000), |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 614 | scheduler.getNextFrameInterval(kMode->getPhysicalDisplayId(), |
| 615 | TimePoint::fromNs(2500))); |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 616 | // Change render rate |
| 617 | frameRate = Fps::fromPeriodNsecs(2000); |
| 618 | vrrSelectorPtr->setActiveMode(kMode->getId(), frameRate); |
Ady Abraham | ee6365b | 2024-03-06 14:31:45 -0800 | [diff] [blame] | 619 | scheduler.setRenderRate(kMode->getPhysicalDisplayId(), frameRate, /*applyImmediately*/ false); |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 620 | |
| 621 | EXPECT_EQ(Fps::fromPeriodNsecs(2000), |
| 622 | scheduler.getNextFrameInterval(kMode->getPhysicalDisplayId(), |
Ady Abraham | fe15a35 | 2024-03-19 10:49:17 -0700 | [diff] [blame] | 623 | TimePoint::fromNs(5500))); |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 624 | EXPECT_EQ(Fps::fromPeriodNsecs(2000), |
| 625 | scheduler.getNextFrameInterval(kMode->getPhysicalDisplayId(), |
Ady Abraham | fe15a35 | 2024-03-19 10:49:17 -0700 | [diff] [blame] | 626 | TimePoint::fromNs(7500))); |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 627 | } |
| 628 | |
Leon Scroggins III | 792ea80 | 2023-11-27 17:32:51 -0500 | [diff] [blame] | 629 | TEST_F(SchedulerTest, resyncAllToHardwareVsync) FTL_FAKE_GUARD(kMainThreadContext) { |
| 630 | // resyncAllToHardwareVsync will result in requesting hardware VSYNC on both displays, since |
| 631 | // they are both on. |
| 632 | EXPECT_CALL(mScheduler->mockRequestHardwareVsync, Call(kDisplayId1, true)).Times(1); |
| 633 | EXPECT_CALL(mScheduler->mockRequestHardwareVsync, Call(kDisplayId2, true)).Times(1); |
| 634 | |
| 635 | mScheduler->registerDisplay(kDisplayId2, |
| 636 | std::make_shared<RefreshRateSelector>(kDisplay2Modes, |
| 637 | kDisplay2Mode60->getId())); |
| 638 | mScheduler->setDisplayPowerMode(kDisplayId1, hal::PowerMode::ON); |
| 639 | mScheduler->setDisplayPowerMode(kDisplayId2, hal::PowerMode::ON); |
| 640 | |
| 641 | static constexpr bool kDisallow = true; |
| 642 | mScheduler->disableHardwareVsync(kDisplayId1, kDisallow); |
| 643 | mScheduler->disableHardwareVsync(kDisplayId2, kDisallow); |
| 644 | |
| 645 | static constexpr bool kAllowToEnable = true; |
| 646 | mScheduler->resyncAllToHardwareVsync(kAllowToEnable); |
| 647 | } |
| 648 | |
| 649 | TEST_F(SchedulerTest, resyncAllDoNotAllow) FTL_FAKE_GUARD(kMainThreadContext) { |
| 650 | // Without setting allowToEnable to true, resyncAllToHardwareVsync does not |
| 651 | // result in requesting hardware VSYNC. |
| 652 | EXPECT_CALL(mScheduler->mockRequestHardwareVsync, Call(kDisplayId1, _)).Times(0); |
| 653 | |
| 654 | mScheduler->setDisplayPowerMode(kDisplayId1, hal::PowerMode::ON); |
| 655 | |
| 656 | static constexpr bool kDisallow = true; |
| 657 | mScheduler->disableHardwareVsync(kDisplayId1, kDisallow); |
| 658 | |
| 659 | static constexpr bool kAllowToEnable = false; |
| 660 | mScheduler->resyncAllToHardwareVsync(kAllowToEnable); |
| 661 | } |
| 662 | |
| 663 | TEST_F(SchedulerTest, resyncAllSkipsOffDisplays) FTL_FAKE_GUARD(kMainThreadContext) { |
| 664 | SET_FLAG_FOR_TEST(flags::multithreaded_present, true); |
| 665 | |
| 666 | // resyncAllToHardwareVsync will result in requesting hardware VSYNC on display 1, which is on, |
| 667 | // but not on display 2, which is off. |
| 668 | EXPECT_CALL(mScheduler->mockRequestHardwareVsync, Call(kDisplayId1, true)).Times(1); |
| 669 | EXPECT_CALL(mScheduler->mockRequestHardwareVsync, Call(kDisplayId2, _)).Times(0); |
| 670 | |
| 671 | mScheduler->setDisplayPowerMode(kDisplayId1, hal::PowerMode::ON); |
| 672 | |
| 673 | mScheduler->registerDisplay(kDisplayId2, |
| 674 | std::make_shared<RefreshRateSelector>(kDisplay2Modes, |
| 675 | kDisplay2Mode60->getId())); |
| 676 | ASSERT_EQ(hal::PowerMode::OFF, mScheduler->getDisplayPowerMode(kDisplayId2)); |
| 677 | |
| 678 | static constexpr bool kDisallow = true; |
| 679 | mScheduler->disableHardwareVsync(kDisplayId1, kDisallow); |
| 680 | mScheduler->disableHardwareVsync(kDisplayId2, kDisallow); |
| 681 | |
| 682 | static constexpr bool kAllowToEnable = true; |
| 683 | mScheduler->resyncAllToHardwareVsync(kAllowToEnable); |
| 684 | } |
| 685 | |
| 686 | TEST_F(SchedulerTest, resyncAllLegacyAppliesToOffDisplays) FTL_FAKE_GUARD(kMainThreadContext) { |
| 687 | SET_FLAG_FOR_TEST(flags::multithreaded_present, false); |
| 688 | |
| 689 | // In the legacy code, prior to the flag, resync applied to OFF displays. |
| 690 | EXPECT_CALL(mScheduler->mockRequestHardwareVsync, Call(kDisplayId1, true)).Times(1); |
| 691 | EXPECT_CALL(mScheduler->mockRequestHardwareVsync, Call(kDisplayId2, true)).Times(1); |
| 692 | |
| 693 | mScheduler->setDisplayPowerMode(kDisplayId1, hal::PowerMode::ON); |
| 694 | |
| 695 | mScheduler->registerDisplay(kDisplayId2, |
| 696 | std::make_shared<RefreshRateSelector>(kDisplay2Modes, |
| 697 | kDisplay2Mode60->getId())); |
| 698 | ASSERT_EQ(hal::PowerMode::OFF, mScheduler->getDisplayPowerMode(kDisplayId2)); |
| 699 | |
| 700 | static constexpr bool kDisallow = true; |
| 701 | mScheduler->disableHardwareVsync(kDisplayId1, kDisallow); |
| 702 | mScheduler->disableHardwareVsync(kDisplayId2, kDisallow); |
| 703 | |
| 704 | static constexpr bool kAllowToEnable = true; |
| 705 | mScheduler->resyncAllToHardwareVsync(kAllowToEnable); |
| 706 | } |
| 707 | |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 708 | class AttachedChoreographerTest : public SchedulerTest { |
| 709 | protected: |
| 710 | void frameRateTestScenario(Fps layerFps, int8_t frameRateCompatibility, Fps displayFps, |
| 711 | Fps expectedChoreographerFps); |
| 712 | }; |
| 713 | |
| 714 | TEST_F(AttachedChoreographerTest, registerSingle) { |
| 715 | EXPECT_TRUE(mScheduler->mutableAttachedChoreographers().empty()); |
| 716 | |
| 717 | const sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger()); |
| 718 | |
| 719 | EXPECT_CALL(mSchedulerCallback, onChoreographerAttached); |
| 720 | const sp<IDisplayEventConnection> connection = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 721 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, layer->getHandle()); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 722 | |
| 723 | EXPECT_EQ(1u, mScheduler->mutableAttachedChoreographers().size()); |
| 724 | ASSERT_EQ(1u, mScheduler->mutableAttachedChoreographers().count(layer->getSequence())); |
| 725 | EXPECT_EQ(1u, |
| 726 | mScheduler->mutableAttachedChoreographers()[layer->getSequence()].connections.size()); |
| 727 | EXPECT_FALSE( |
| 728 | mScheduler->mutableAttachedChoreographers()[layer->getSequence()].frameRate.isValid()); |
| 729 | } |
| 730 | |
| 731 | TEST_F(AttachedChoreographerTest, registerMultipleOnSameLayer) { |
| 732 | EXPECT_TRUE(mScheduler->mutableAttachedChoreographers().empty()); |
| 733 | |
| 734 | const sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger()); |
| 735 | const auto handle = layer->getHandle(); |
| 736 | |
| 737 | EXPECT_CALL(mSchedulerCallback, onChoreographerAttached).Times(2); |
| 738 | |
| 739 | EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)) |
| 740 | .WillOnce(Return(0)) |
| 741 | .WillOnce(Return(0)); |
| 742 | |
| 743 | const auto mockConnection1 = sp<MockEventThreadConnection>::make(mEventThread); |
| 744 | const auto mockConnection2 = sp<MockEventThreadConnection>::make(mEventThread); |
| 745 | EXPECT_CALL(*mEventThread, createEventConnection(_, _)) |
| 746 | .WillOnce(Return(mockConnection1)) |
| 747 | .WillOnce(Return(mockConnection2)); |
| 748 | |
| 749 | const sp<IDisplayEventConnection> connection1 = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 750 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, handle); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 751 | const sp<IDisplayEventConnection> connection2 = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 752 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, handle); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 753 | |
| 754 | EXPECT_EQ(1u, mScheduler->mutableAttachedChoreographers().size()); |
| 755 | ASSERT_EQ(1u, mScheduler->mutableAttachedChoreographers().count(layer->getSequence())); |
| 756 | EXPECT_EQ(2u, |
| 757 | mScheduler->mutableAttachedChoreographers()[layer->getSequence()].connections.size()); |
| 758 | EXPECT_FALSE( |
| 759 | mScheduler->mutableAttachedChoreographers()[layer->getSequence()].frameRate.isValid()); |
| 760 | } |
| 761 | |
| 762 | TEST_F(AttachedChoreographerTest, registerMultipleOnDifferentLayers) { |
| 763 | EXPECT_TRUE(mScheduler->mutableAttachedChoreographers().empty()); |
| 764 | |
| 765 | const sp<MockLayer> layer1 = sp<MockLayer>::make(mFlinger.flinger()); |
| 766 | const sp<MockLayer> layer2 = sp<MockLayer>::make(mFlinger.flinger()); |
| 767 | |
| 768 | EXPECT_CALL(mSchedulerCallback, onChoreographerAttached).Times(2); |
| 769 | const sp<IDisplayEventConnection> connection1 = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 770 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, layer1->getHandle()); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 771 | const sp<IDisplayEventConnection> connection2 = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 772 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, layer2->getHandle()); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 773 | |
| 774 | EXPECT_EQ(2u, mScheduler->mutableAttachedChoreographers().size()); |
| 775 | |
| 776 | ASSERT_EQ(1u, mScheduler->mutableAttachedChoreographers().count(layer1->getSequence())); |
| 777 | EXPECT_EQ(1u, |
| 778 | mScheduler->mutableAttachedChoreographers()[layer1->getSequence()] |
| 779 | .connections.size()); |
| 780 | EXPECT_FALSE( |
| 781 | mScheduler->mutableAttachedChoreographers()[layer1->getSequence()].frameRate.isValid()); |
| 782 | |
| 783 | ASSERT_EQ(1u, mScheduler->mutableAttachedChoreographers().count(layer2->getSequence())); |
| 784 | EXPECT_EQ(1u, |
| 785 | mScheduler->mutableAttachedChoreographers()[layer2->getSequence()] |
| 786 | .connections.size()); |
| 787 | EXPECT_FALSE( |
| 788 | mScheduler->mutableAttachedChoreographers()[layer2->getSequence()].frameRate.isValid()); |
| 789 | } |
| 790 | |
| 791 | TEST_F(AttachedChoreographerTest, removedWhenConnectionIsGone) { |
| 792 | EXPECT_TRUE(mScheduler->mutableAttachedChoreographers().empty()); |
| 793 | |
| 794 | const sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger()); |
| 795 | |
| 796 | EXPECT_CALL(mSchedulerCallback, onChoreographerAttached); |
| 797 | |
| 798 | sp<IDisplayEventConnection> connection = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 799 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, layer->getHandle()); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 800 | |
| 801 | ASSERT_EQ(1u, mScheduler->mutableAttachedChoreographers().count(layer->getSequence())); |
| 802 | EXPECT_EQ(1u, |
| 803 | mScheduler->mutableAttachedChoreographers()[layer->getSequence()].connections.size()); |
| 804 | |
| 805 | // The connection is used all over this test, so it is quite hard to release it from here. |
| 806 | // Instead, we just do a small shortcut. |
| 807 | { |
| 808 | EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)).WillOnce(Return(0)); |
| 809 | sp<MockEventThreadConnection> mockConnection = |
| 810 | sp<MockEventThreadConnection>::make(mEventThread); |
| 811 | mScheduler->mutableAttachedChoreographers()[layer->getSequence()].connections.clear(); |
| 812 | mScheduler->mutableAttachedChoreographers()[layer->getSequence()].connections.emplace( |
| 813 | mockConnection); |
| 814 | } |
| 815 | |
| 816 | RequestedLayerState layerState(LayerCreationArgs(layer->getSequence())); |
| 817 | LayerHierarchy hierarchy(&layerState); |
| 818 | mScheduler->updateAttachedChoreographers(hierarchy, 60_Hz); |
| 819 | EXPECT_TRUE(mScheduler->mutableAttachedChoreographers().empty()); |
| 820 | } |
| 821 | |
| 822 | TEST_F(AttachedChoreographerTest, removedWhenLayerIsGone) { |
| 823 | EXPECT_TRUE(mScheduler->mutableAttachedChoreographers().empty()); |
| 824 | |
| 825 | sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger()); |
| 826 | |
| 827 | EXPECT_CALL(mSchedulerCallback, onChoreographerAttached); |
| 828 | const sp<IDisplayEventConnection> connection = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 829 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, layer->getHandle()); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 830 | |
| 831 | layer.clear(); |
| 832 | mFlinger.mutableLayersPendingRemoval().clear(); |
| 833 | EXPECT_TRUE(mScheduler->mutableAttachedChoreographers().empty()); |
| 834 | } |
| 835 | |
| 836 | void AttachedChoreographerTest::frameRateTestScenario(Fps layerFps, int8_t frameRateCompatibility, |
| 837 | Fps displayFps, |
| 838 | Fps expectedChoreographerFps) { |
| 839 | const sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger()); |
| 840 | |
| 841 | EXPECT_CALL(mSchedulerCallback, onChoreographerAttached); |
| 842 | sp<IDisplayEventConnection> connection = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 843 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, layer->getHandle()); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 844 | |
| 845 | RequestedLayerState layerState(LayerCreationArgs(layer->getSequence())); |
| 846 | LayerHierarchy hierarchy(&layerState); |
| 847 | |
| 848 | layerState.frameRate = layerFps.getValue(); |
| 849 | layerState.frameRateCompatibility = frameRateCompatibility; |
| 850 | |
| 851 | mScheduler->updateAttachedChoreographers(hierarchy, displayFps); |
| 852 | |
| 853 | ASSERT_EQ(1u, mScheduler->mutableAttachedChoreographers().count(layer->getSequence())); |
| 854 | EXPECT_EQ(expectedChoreographerFps, |
| 855 | mScheduler->mutableAttachedChoreographers()[layer->getSequence()].frameRate); |
| 856 | EXPECT_EQ(expectedChoreographerFps, mEventThreadConnection->frameRate); |
| 857 | } |
| 858 | |
| 859 | TEST_F(AttachedChoreographerTest, setsFrameRateDefault) { |
| 860 | Fps layerFps = 30_Hz; |
| 861 | int8_t frameRateCompatibility = ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT; |
| 862 | Fps displayFps = 60_Hz; |
| 863 | Fps expectedChoreographerFps = 30_Hz; |
| 864 | |
| 865 | frameRateTestScenario(layerFps, frameRateCompatibility, displayFps, expectedChoreographerFps); |
| 866 | |
| 867 | layerFps = Fps::fromValue(32.7f); |
| 868 | frameRateTestScenario(layerFps, frameRateCompatibility, displayFps, expectedChoreographerFps); |
| 869 | } |
| 870 | |
| 871 | TEST_F(AttachedChoreographerTest, setsFrameRateExact) { |
| 872 | Fps layerFps = 30_Hz; |
| 873 | int8_t frameRateCompatibility = ANATIVEWINDOW_FRAME_RATE_EXACT; |
| 874 | Fps displayFps = 60_Hz; |
| 875 | Fps expectedChoreographerFps = 30_Hz; |
| 876 | |
| 877 | frameRateTestScenario(layerFps, frameRateCompatibility, displayFps, expectedChoreographerFps); |
| 878 | |
| 879 | layerFps = Fps::fromValue(32.7f); |
| 880 | expectedChoreographerFps = {}; |
| 881 | frameRateTestScenario(layerFps, frameRateCompatibility, displayFps, expectedChoreographerFps); |
| 882 | } |
| 883 | |
| 884 | TEST_F(AttachedChoreographerTest, setsFrameRateExactOrMultiple) { |
| 885 | Fps layerFps = 30_Hz; |
| 886 | int8_t frameRateCompatibility = ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE; |
| 887 | Fps displayFps = 60_Hz; |
| 888 | Fps expectedChoreographerFps = 30_Hz; |
| 889 | |
| 890 | frameRateTestScenario(layerFps, frameRateCompatibility, displayFps, expectedChoreographerFps); |
| 891 | |
| 892 | layerFps = Fps::fromValue(32.7f); |
| 893 | expectedChoreographerFps = {}; |
| 894 | frameRateTestScenario(layerFps, frameRateCompatibility, displayFps, expectedChoreographerFps); |
| 895 | } |
| 896 | |
| 897 | TEST_F(AttachedChoreographerTest, setsFrameRateParent) { |
| 898 | const sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger()); |
| 899 | const sp<MockLayer> parent = sp<MockLayer>::make(mFlinger.flinger()); |
| 900 | |
| 901 | EXPECT_CALL(mSchedulerCallback, onChoreographerAttached); |
| 902 | sp<IDisplayEventConnection> connection = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 903 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, parent->getHandle()); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 904 | |
| 905 | RequestedLayerState parentState(LayerCreationArgs(parent->getSequence())); |
| 906 | LayerHierarchy parentHierarchy(&parentState); |
| 907 | |
| 908 | RequestedLayerState layerState(LayerCreationArgs(layer->getSequence())); |
| 909 | LayerHierarchy hierarchy(&layerState); |
| 910 | parentHierarchy.mChildren.push_back( |
| 911 | std::make_pair(&hierarchy, LayerHierarchy::Variant::Attached)); |
| 912 | |
| 913 | layerState.frameRate = (30_Hz).getValue(); |
| 914 | layerState.frameRateCompatibility = ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT; |
| 915 | |
| 916 | mScheduler->updateAttachedChoreographers(parentHierarchy, 120_Hz); |
| 917 | |
| 918 | ASSERT_EQ(1u, mScheduler->mutableAttachedChoreographers().count(parent->getSequence())); |
| 919 | |
| 920 | EXPECT_EQ(30_Hz, mScheduler->mutableAttachedChoreographers()[parent->getSequence()].frameRate); |
| 921 | } |
| 922 | |
| 923 | TEST_F(AttachedChoreographerTest, setsFrameRateParent2Children) { |
| 924 | const sp<MockLayer> layer1 = sp<MockLayer>::make(mFlinger.flinger()); |
| 925 | const sp<MockLayer> layer2 = sp<MockLayer>::make(mFlinger.flinger()); |
| 926 | const sp<MockLayer> parent = sp<MockLayer>::make(mFlinger.flinger()); |
| 927 | |
| 928 | EXPECT_CALL(mSchedulerCallback, onChoreographerAttached); |
| 929 | sp<IDisplayEventConnection> connection = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 930 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, parent->getHandle()); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 931 | |
| 932 | RequestedLayerState parentState(LayerCreationArgs(parent->getSequence())); |
| 933 | LayerHierarchy parentHierarchy(&parentState); |
| 934 | |
| 935 | RequestedLayerState layer1State(LayerCreationArgs(layer1->getSequence())); |
| 936 | LayerHierarchy layer1Hierarchy(&layer1State); |
| 937 | parentHierarchy.mChildren.push_back( |
| 938 | std::make_pair(&layer1Hierarchy, LayerHierarchy::Variant::Attached)); |
| 939 | |
| 940 | RequestedLayerState layer2State(LayerCreationArgs(layer1->getSequence())); |
| 941 | LayerHierarchy layer2Hierarchy(&layer2State); |
| 942 | parentHierarchy.mChildren.push_back( |
| 943 | std::make_pair(&layer2Hierarchy, LayerHierarchy::Variant::Attached)); |
| 944 | |
| 945 | layer1State.frameRate = (30_Hz).getValue(); |
| 946 | layer1State.frameRateCompatibility = ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT; |
| 947 | |
| 948 | layer2State.frameRate = (20_Hz).getValue(); |
| 949 | layer2State.frameRateCompatibility = ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT; |
| 950 | |
| 951 | mScheduler->updateAttachedChoreographers(parentHierarchy, 120_Hz); |
| 952 | |
| 953 | ASSERT_EQ(1u, mScheduler->mutableAttachedChoreographers().count(parent->getSequence())); |
| 954 | |
| 955 | EXPECT_EQ(60_Hz, mScheduler->mutableAttachedChoreographers()[parent->getSequence()].frameRate); |
| 956 | } |
| 957 | |
| 958 | TEST_F(AttachedChoreographerTest, setsFrameRateParentConflictingChildren) { |
| 959 | const sp<MockLayer> layer1 = sp<MockLayer>::make(mFlinger.flinger()); |
| 960 | const sp<MockLayer> layer2 = sp<MockLayer>::make(mFlinger.flinger()); |
| 961 | const sp<MockLayer> parent = sp<MockLayer>::make(mFlinger.flinger()); |
| 962 | |
| 963 | EXPECT_CALL(mSchedulerCallback, onChoreographerAttached); |
| 964 | sp<IDisplayEventConnection> connection = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 965 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, parent->getHandle()); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 966 | |
| 967 | RequestedLayerState parentState(LayerCreationArgs(parent->getSequence())); |
| 968 | LayerHierarchy parentHierarchy(&parentState); |
| 969 | |
| 970 | RequestedLayerState layer1State(LayerCreationArgs(layer1->getSequence())); |
| 971 | LayerHierarchy layer1Hierarchy(&layer1State); |
| 972 | parentHierarchy.mChildren.push_back( |
| 973 | std::make_pair(&layer1Hierarchy, LayerHierarchy::Variant::Attached)); |
| 974 | |
| 975 | RequestedLayerState layer2State(LayerCreationArgs(layer1->getSequence())); |
| 976 | LayerHierarchy layer2Hierarchy(&layer2State); |
| 977 | parentHierarchy.mChildren.push_back( |
| 978 | std::make_pair(&layer2Hierarchy, LayerHierarchy::Variant::Attached)); |
| 979 | |
| 980 | layer1State.frameRate = (30_Hz).getValue(); |
| 981 | layer1State.frameRateCompatibility = ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT; |
| 982 | |
| 983 | layer2State.frameRate = (25_Hz).getValue(); |
| 984 | layer2State.frameRateCompatibility = ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT; |
| 985 | |
| 986 | mScheduler->updateAttachedChoreographers(parentHierarchy, 120_Hz); |
| 987 | |
| 988 | ASSERT_EQ(1u, mScheduler->mutableAttachedChoreographers().count(parent->getSequence())); |
| 989 | |
| 990 | EXPECT_EQ(Fps(), mScheduler->mutableAttachedChoreographers()[parent->getSequence()].frameRate); |
| 991 | } |
| 992 | |
| 993 | TEST_F(AttachedChoreographerTest, setsFrameRateChild) { |
| 994 | const sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger()); |
| 995 | const sp<MockLayer> parent = sp<MockLayer>::make(mFlinger.flinger()); |
| 996 | |
| 997 | EXPECT_CALL(mSchedulerCallback, onChoreographerAttached); |
| 998 | sp<IDisplayEventConnection> connection = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 999 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, layer->getHandle()); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 1000 | |
| 1001 | RequestedLayerState parentState(LayerCreationArgs(parent->getSequence())); |
| 1002 | LayerHierarchy parentHierarchy(&parentState); |
| 1003 | |
| 1004 | RequestedLayerState layerState(LayerCreationArgs(layer->getSequence())); |
| 1005 | LayerHierarchy hierarchy(&layerState); |
| 1006 | parentHierarchy.mChildren.push_back( |
| 1007 | std::make_pair(&hierarchy, LayerHierarchy::Variant::Attached)); |
| 1008 | |
| 1009 | parentState.frameRate = (30_Hz).getValue(); |
| 1010 | parentState.frameRateCompatibility = ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT; |
| 1011 | |
| 1012 | mScheduler->updateAttachedChoreographers(parentHierarchy, 120_Hz); |
| 1013 | |
| 1014 | ASSERT_EQ(1u, mScheduler->mutableAttachedChoreographers().count(layer->getSequence())); |
| 1015 | |
| 1016 | EXPECT_EQ(30_Hz, mScheduler->mutableAttachedChoreographers()[layer->getSequence()].frameRate); |
| 1017 | } |
| 1018 | |
| 1019 | TEST_F(AttachedChoreographerTest, setsFrameRateChildNotOverriddenByParent) { |
| 1020 | const sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger()); |
| 1021 | const sp<MockLayer> parent = sp<MockLayer>::make(mFlinger.flinger()); |
| 1022 | |
| 1023 | EXPECT_CALL(mSchedulerCallback, onChoreographerAttached); |
| 1024 | sp<IDisplayEventConnection> connection = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 1025 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, layer->getHandle()); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 1026 | |
| 1027 | RequestedLayerState parentState(LayerCreationArgs(parent->getSequence())); |
| 1028 | LayerHierarchy parentHierarchy(&parentState); |
| 1029 | |
| 1030 | RequestedLayerState layerState(LayerCreationArgs(layer->getSequence())); |
| 1031 | LayerHierarchy hierarchy(&layerState); |
| 1032 | parentHierarchy.mChildren.push_back( |
| 1033 | std::make_pair(&hierarchy, LayerHierarchy::Variant::Attached)); |
| 1034 | |
| 1035 | parentState.frameRate = (30_Hz).getValue(); |
| 1036 | parentState.frameRateCompatibility = ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT; |
| 1037 | |
| 1038 | layerState.frameRate = (60_Hz).getValue(); |
| 1039 | layerState.frameRateCompatibility = ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT; |
| 1040 | |
| 1041 | mScheduler->updateAttachedChoreographers(parentHierarchy, 120_Hz); |
| 1042 | |
| 1043 | ASSERT_EQ(1u, mScheduler->mutableAttachedChoreographers().count(layer->getSequence())); |
| 1044 | |
| 1045 | EXPECT_EQ(60_Hz, mScheduler->mutableAttachedChoreographers()[layer->getSequence()].frameRate); |
| 1046 | } |
| 1047 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 1048 | } // namespace android::scheduler |