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