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 | b5a094b | 2022-10-27 12:00:12 -0400 | [diff] [blame] | 346 | mScheduler->registerDisplay(kDisplayId1, |
| 347 | std::make_shared<RefreshRateSelector>(kDisplay1Modes, |
| 348 | kDisplay1Mode60->getId())); |
| 349 | mScheduler->registerDisplay(kDisplayId2, |
| 350 | std::make_shared<RefreshRateSelector>(kDisplay2Modes, |
| 351 | kDisplay2Mode60->getId())); |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 352 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 353 | using DisplayModeChoice = TestableScheduler::DisplayModeChoice; |
| 354 | TestableScheduler::DisplayModeChoiceMap expectedChoices; |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 355 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 356 | { |
| 357 | const GlobalSignals globalSignals = {.idle = true}; |
| 358 | expectedChoices = |
| 359 | ftl::init::map<const PhysicalDisplayId&, |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 360 | DisplayModeChoice>(kDisplayId1, |
| 361 | FrameRateMode{60_Hz, kDisplay1Mode60}, |
| 362 | globalSignals)(kDisplayId2, |
| 363 | FrameRateMode{60_Hz, |
| 364 | kDisplay2Mode60}, |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 365 | globalSignals); |
| 366 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 367 | std::vector<RefreshRateSelector::LayerRequirement> layers = {{.weight = 1.f}, |
| 368 | {.weight = 1.f}}; |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 369 | mScheduler->setContentRequirements(layers); |
| 370 | mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals); |
| 371 | |
| 372 | const auto actualChoices = mScheduler->chooseDisplayModes(); |
| 373 | EXPECT_EQ(expectedChoices, actualChoices); |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 374 | } |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 375 | { |
| 376 | const GlobalSignals globalSignals = {.idle = false}; |
| 377 | expectedChoices = |
| 378 | ftl::init::map<const PhysicalDisplayId&, |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 379 | DisplayModeChoice>(kDisplayId1, |
| 380 | FrameRateMode{120_Hz, kDisplay1Mode120}, |
| 381 | globalSignals)(kDisplayId2, |
| 382 | FrameRateMode{120_Hz, |
| 383 | kDisplay2Mode120}, |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 384 | globalSignals); |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 385 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 386 | mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals); |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 387 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 388 | const auto actualChoices = mScheduler->chooseDisplayModes(); |
| 389 | EXPECT_EQ(expectedChoices, actualChoices); |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 390 | } |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 391 | { |
| 392 | const GlobalSignals globalSignals = {.touch = true}; |
| 393 | mScheduler->replaceTouchTimer(10); |
| 394 | mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals); |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 395 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 396 | expectedChoices = |
| 397 | ftl::init::map<const PhysicalDisplayId&, |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 398 | DisplayModeChoice>(kDisplayId1, |
| 399 | FrameRateMode{120_Hz, kDisplay1Mode120}, |
| 400 | globalSignals)(kDisplayId2, |
| 401 | FrameRateMode{120_Hz, |
| 402 | kDisplay2Mode120}, |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 403 | globalSignals); |
| 404 | |
| 405 | const auto actualChoices = mScheduler->chooseDisplayModes(); |
| 406 | EXPECT_EQ(expectedChoices, actualChoices); |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 407 | } |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 408 | { |
ramindani | 22f2ead | 2023-04-21 10:27:11 -0700 | [diff] [blame] | 409 | // The kDisplayId3 does not support 120Hz, The pacesetter display rate is chosen to be 120 |
| 410 | // Hz. In this case only the display kDisplayId3 choose 60Hz as it does not support 120Hz. |
Dominik Laskowski | b5a094b | 2022-10-27 12:00:12 -0400 | [diff] [blame] | 411 | mScheduler |
| 412 | ->registerDisplay(kDisplayId3, |
| 413 | std::make_shared<RefreshRateSelector>(kDisplay3Modes, |
| 414 | kDisplay3Mode60->getId())); |
Dominik Laskowski | 327d609 | 2022-10-11 18:05:08 -0400 | [diff] [blame] | 415 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 416 | const GlobalSignals globalSignals = {.touch = true}; |
| 417 | mScheduler->replaceTouchTimer(10); |
| 418 | mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals); |
ramindani | 7c48728 | 2022-10-10 16:17:51 -0700 | [diff] [blame] | 419 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 420 | expectedChoices = ftl::init::map< |
| 421 | const PhysicalDisplayId&, |
ramindani | 22f2ead | 2023-04-21 10:27:11 -0700 | [diff] [blame] | 422 | DisplayModeChoice>(kDisplayId1, FrameRateMode{120_Hz, kDisplay1Mode120}, |
| 423 | globalSignals)(kDisplayId2, |
| 424 | FrameRateMode{120_Hz, kDisplay2Mode120}, |
| 425 | globalSignals)(kDisplayId3, |
| 426 | FrameRateMode{60_Hz, |
| 427 | kDisplay3Mode60}, |
| 428 | globalSignals); |
| 429 | |
| 430 | const auto actualChoices = mScheduler->chooseDisplayModes(); |
| 431 | EXPECT_EQ(expectedChoices, actualChoices); |
| 432 | } |
| 433 | { |
| 434 | // We should choose 60Hz despite the touch signal as pacesetter only supports 60Hz |
| 435 | mScheduler->setPacesetterDisplay(kDisplayId3); |
| 436 | const GlobalSignals globalSignals = {.touch = true}; |
| 437 | mScheduler->replaceTouchTimer(10); |
| 438 | mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals); |
| 439 | |
| 440 | expectedChoices = ftl::init::map< |
| 441 | const PhysicalDisplayId&, |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 442 | DisplayModeChoice>(kDisplayId1, FrameRateMode{60_Hz, kDisplay1Mode60}, |
| 443 | globalSignals)(kDisplayId2, |
| 444 | FrameRateMode{60_Hz, kDisplay2Mode60}, |
| 445 | globalSignals)(kDisplayId3, |
| 446 | FrameRateMode{60_Hz, |
| 447 | kDisplay3Mode60}, |
| 448 | globalSignals); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 449 | |
| 450 | const auto actualChoices = mScheduler->chooseDisplayModes(); |
| 451 | EXPECT_EQ(expectedChoices, actualChoices); |
ramindani | 7c48728 | 2022-10-10 16:17:51 -0700 | [diff] [blame] | 452 | } |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 455 | TEST_F(SchedulerTest, onFrameSignalMultipleDisplays) { |
| 456 | mScheduler->registerDisplay(kDisplayId1, |
| 457 | std::make_shared<RefreshRateSelector>(kDisplay1Modes, |
| 458 | kDisplay1Mode60->getId())); |
| 459 | mScheduler->registerDisplay(kDisplayId2, |
| 460 | std::make_shared<RefreshRateSelector>(kDisplay2Modes, |
| 461 | kDisplay2Mode60->getId())); |
| 462 | |
| 463 | using VsyncIds = std::vector<std::pair<PhysicalDisplayId, VsyncId>>; |
| 464 | |
| 465 | struct Compositor final : ICompositor { |
Dominik Laskowski | fb4b737 | 2023-11-22 09:56:54 -0500 | [diff] [blame] | 466 | explicit Compositor(TestableScheduler& scheduler) : scheduler(scheduler) {} |
| 467 | |
| 468 | TestableScheduler& scheduler; |
| 469 | |
| 470 | struct { |
| 471 | PhysicalDisplayId commit; |
| 472 | PhysicalDisplayId composite; |
| 473 | } pacesetterIds; |
| 474 | |
| 475 | struct { |
| 476 | VsyncIds commit; |
| 477 | VsyncIds composite; |
| 478 | } vsyncIds; |
| 479 | |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 480 | bool committed = true; |
Dominik Laskowski | fb4b737 | 2023-11-22 09:56:54 -0500 | [diff] [blame] | 481 | bool changePacesetter = false; |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 482 | |
| 483 | void configure() override {} |
| 484 | |
Dominik Laskowski | fb4b737 | 2023-11-22 09:56:54 -0500 | [diff] [blame] | 485 | bool commit(PhysicalDisplayId pacesetterId, |
| 486 | const scheduler::FrameTargets& targets) override { |
| 487 | pacesetterIds.commit = pacesetterId; |
| 488 | |
| 489 | vsyncIds.commit.clear(); |
| 490 | vsyncIds.composite.clear(); |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 491 | |
| 492 | for (const auto& [id, target] : targets) { |
Dominik Laskowski | fb4b737 | 2023-11-22 09:56:54 -0500 | [diff] [blame] | 493 | vsyncIds.commit.emplace_back(id, target->vsyncId()); |
| 494 | } |
| 495 | |
| 496 | if (changePacesetter) { |
| 497 | scheduler.setPacesetterDisplay(kDisplayId2); |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | return committed; |
| 501 | } |
| 502 | |
Dominik Laskowski | fb4b737 | 2023-11-22 09:56:54 -0500 | [diff] [blame] | 503 | CompositeResultsPerDisplay composite(PhysicalDisplayId pacesetterId, |
| 504 | const scheduler::FrameTargeters& targeters) override { |
| 505 | pacesetterIds.composite = pacesetterId; |
| 506 | |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 507 | CompositeResultsPerDisplay results; |
| 508 | |
Dominik Laskowski | fb4b737 | 2023-11-22 09:56:54 -0500 | [diff] [blame] | 509 | for (const auto& [id, targeter] : targeters) { |
| 510 | vsyncIds.composite.emplace_back(id, targeter->target().vsyncId()); |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 511 | results.try_emplace(id, |
| 512 | CompositeResult{.compositionCoverage = |
| 513 | CompositionCoverage::Hwc}); |
| 514 | } |
| 515 | |
| 516 | return results; |
| 517 | } |
| 518 | |
| 519 | void sample() override {} |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 520 | void sendNotifyExpectedPresentHint(PhysicalDisplayId) override {} |
Dominik Laskowski | fb4b737 | 2023-11-22 09:56:54 -0500 | [diff] [blame] | 521 | } compositor(*mScheduler); |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 522 | |
| 523 | mScheduler->doFrameSignal(compositor, VsyncId(42)); |
| 524 | |
Dominik Laskowski | fb4b737 | 2023-11-22 09:56:54 -0500 | [diff] [blame] | 525 | const auto makeVsyncIds = [](VsyncId vsyncId, bool swap = false) -> VsyncIds { |
| 526 | if (swap) { |
| 527 | return {{kDisplayId2, vsyncId}, {kDisplayId1, vsyncId}}; |
| 528 | } else { |
| 529 | return {{kDisplayId1, vsyncId}, {kDisplayId2, vsyncId}}; |
| 530 | } |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 531 | }; |
| 532 | |
Dominik Laskowski | fb4b737 | 2023-11-22 09:56:54 -0500 | [diff] [blame] | 533 | EXPECT_EQ(kDisplayId1, compositor.pacesetterIds.commit); |
| 534 | EXPECT_EQ(kDisplayId1, compositor.pacesetterIds.composite); |
| 535 | EXPECT_EQ(makeVsyncIds(VsyncId(42)), compositor.vsyncIds.commit); |
| 536 | EXPECT_EQ(makeVsyncIds(VsyncId(42)), compositor.vsyncIds.composite); |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 537 | |
Dominik Laskowski | fb4b737 | 2023-11-22 09:56:54 -0500 | [diff] [blame] | 538 | // FrameTargets should be updated despite the skipped commit. |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 539 | compositor.committed = false; |
| 540 | mScheduler->doFrameSignal(compositor, VsyncId(43)); |
| 541 | |
Dominik Laskowski | fb4b737 | 2023-11-22 09:56:54 -0500 | [diff] [blame] | 542 | EXPECT_EQ(kDisplayId1, compositor.pacesetterIds.commit); |
| 543 | EXPECT_EQ(kDisplayId1, compositor.pacesetterIds.composite); |
| 544 | EXPECT_EQ(makeVsyncIds(VsyncId(43)), compositor.vsyncIds.commit); |
| 545 | EXPECT_TRUE(compositor.vsyncIds.composite.empty()); |
| 546 | |
| 547 | // The pacesetter may change during commit. |
| 548 | compositor.committed = true; |
| 549 | compositor.changePacesetter = true; |
| 550 | mScheduler->doFrameSignal(compositor, VsyncId(44)); |
| 551 | |
| 552 | EXPECT_EQ(kDisplayId1, compositor.pacesetterIds.commit); |
| 553 | EXPECT_EQ(kDisplayId2, compositor.pacesetterIds.composite); |
| 554 | EXPECT_EQ(makeVsyncIds(VsyncId(44)), compositor.vsyncIds.commit); |
| 555 | EXPECT_EQ(makeVsyncIds(VsyncId(44), true), compositor.vsyncIds.composite); |
Dominik Laskowski | 50e2e4d | 2023-10-04 10:58:28 -0400 | [diff] [blame] | 556 | } |
| 557 | |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 558 | TEST_F(SchedulerTest, nextFrameIntervalTest) { |
| 559 | SET_FLAG_FOR_TEST(flags::vrr_config, true); |
| 560 | |
| 561 | static constexpr size_t kHistorySize = 10; |
| 562 | static constexpr size_t kMinimumSamplesForPrediction = 6; |
| 563 | static constexpr size_t kOutlierTolerancePercent = 25; |
| 564 | const auto refreshRate = Fps::fromPeriodNsecs(500); |
| 565 | auto frameRate = Fps::fromPeriodNsecs(1000); |
| 566 | |
| 567 | const ftl::NonNull<DisplayModePtr> kMode = ftl::as_non_null( |
| 568 | createVrrDisplayMode(DisplayModeId(0), refreshRate, |
| 569 | hal::VrrConfig{.minFrameIntervalNs = static_cast<int32_t>( |
| 570 | frameRate.getPeriodNsecs())})); |
| 571 | std::shared_ptr<VSyncPredictor> vrrTracker = |
Ady Abraham | 77b4fb1 | 2024-03-05 17:51:53 -0800 | [diff] [blame] | 572 | std::make_shared<VSyncPredictor>(std::make_unique<ZeroClock>(), kMode, kHistorySize, |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 573 | kMinimumSamplesForPrediction, |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 574 | kOutlierTolerancePercent); |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 575 | std::shared_ptr<RefreshRateSelector> vrrSelectorPtr = |
| 576 | std::make_shared<RefreshRateSelector>(makeModes(kMode), kMode->getId()); |
| 577 | TestableScheduler scheduler{std::make_unique<android::mock::VsyncController>(), |
| 578 | vrrTracker, |
| 579 | vrrSelectorPtr, |
Leon Scroggins III | 823d4ca | 2023-12-12 16:57:34 -0500 | [diff] [blame] | 580 | mFlinger.getFactory(), |
| 581 | mFlinger.getTimeStats(), |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 582 | mSchedulerCallback}; |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 583 | |
| 584 | scheduler.registerDisplay(kMode->getPhysicalDisplayId(), vrrSelectorPtr, vrrTracker); |
| 585 | vrrSelectorPtr->setActiveMode(kMode->getId(), frameRate); |
Ady Abraham | ee6365b | 2024-03-06 14:31:45 -0800 | [diff] [blame] | 586 | scheduler.setRenderRate(kMode->getPhysicalDisplayId(), frameRate, /*applyImmediately*/ false); |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 587 | vrrTracker->addVsyncTimestamp(0); |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 588 | // Set 1000 as vsync seq #0 |
| 589 | vrrTracker->nextAnticipatedVSyncTimeFrom(700); |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 590 | |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 591 | EXPECT_EQ(Fps::fromPeriodNsecs(1000), |
| 592 | scheduler.getNextFrameInterval(kMode->getPhysicalDisplayId(), |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 593 | TimePoint::fromNs(1000))); |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 594 | EXPECT_EQ(Fps::fromPeriodNsecs(1000), |
| 595 | scheduler.getNextFrameInterval(kMode->getPhysicalDisplayId(), |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 596 | TimePoint::fromNs(2000))); |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 597 | |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 598 | // Not crossing the min frame period |
Ady Abraham | e54ce10 | 2024-03-04 23:18:38 +0000 | [diff] [blame] | 599 | vrrTracker->onFrameBegin(TimePoint::fromNs(2000), TimePoint::fromNs(1500)); |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 600 | EXPECT_EQ(Fps::fromPeriodNsecs(1000), |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 601 | scheduler.getNextFrameInterval(kMode->getPhysicalDisplayId(), |
| 602 | TimePoint::fromNs(2500))); |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 603 | // Change render rate |
| 604 | frameRate = Fps::fromPeriodNsecs(2000); |
| 605 | vrrSelectorPtr->setActiveMode(kMode->getId(), frameRate); |
Ady Abraham | ee6365b | 2024-03-06 14:31:45 -0800 | [diff] [blame] | 606 | scheduler.setRenderRate(kMode->getPhysicalDisplayId(), frameRate, /*applyImmediately*/ false); |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 607 | |
| 608 | EXPECT_EQ(Fps::fromPeriodNsecs(2000), |
| 609 | scheduler.getNextFrameInterval(kMode->getPhysicalDisplayId(), |
Ady Abraham | fe15a35 | 2024-03-19 10:49:17 -0700 | [diff] [blame^] | 610 | TimePoint::fromNs(5500))); |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 611 | EXPECT_EQ(Fps::fromPeriodNsecs(2000), |
| 612 | scheduler.getNextFrameInterval(kMode->getPhysicalDisplayId(), |
Ady Abraham | fe15a35 | 2024-03-19 10:49:17 -0700 | [diff] [blame^] | 613 | TimePoint::fromNs(7500))); |
ramindani | 0491e64 | 2023-11-16 17:42:14 -0800 | [diff] [blame] | 614 | } |
| 615 | |
Leon Scroggins III | 792ea80 | 2023-11-27 17:32:51 -0500 | [diff] [blame] | 616 | TEST_F(SchedulerTest, resyncAllToHardwareVsync) FTL_FAKE_GUARD(kMainThreadContext) { |
| 617 | // resyncAllToHardwareVsync will result in requesting hardware VSYNC on both displays, since |
| 618 | // they are both on. |
| 619 | EXPECT_CALL(mScheduler->mockRequestHardwareVsync, Call(kDisplayId1, true)).Times(1); |
| 620 | EXPECT_CALL(mScheduler->mockRequestHardwareVsync, Call(kDisplayId2, true)).Times(1); |
| 621 | |
| 622 | mScheduler->registerDisplay(kDisplayId2, |
| 623 | std::make_shared<RefreshRateSelector>(kDisplay2Modes, |
| 624 | kDisplay2Mode60->getId())); |
| 625 | mScheduler->setDisplayPowerMode(kDisplayId1, hal::PowerMode::ON); |
| 626 | mScheduler->setDisplayPowerMode(kDisplayId2, hal::PowerMode::ON); |
| 627 | |
| 628 | static constexpr bool kDisallow = true; |
| 629 | mScheduler->disableHardwareVsync(kDisplayId1, kDisallow); |
| 630 | mScheduler->disableHardwareVsync(kDisplayId2, kDisallow); |
| 631 | |
| 632 | static constexpr bool kAllowToEnable = true; |
| 633 | mScheduler->resyncAllToHardwareVsync(kAllowToEnable); |
| 634 | } |
| 635 | |
| 636 | TEST_F(SchedulerTest, resyncAllDoNotAllow) FTL_FAKE_GUARD(kMainThreadContext) { |
| 637 | // Without setting allowToEnable to true, resyncAllToHardwareVsync does not |
| 638 | // result in requesting hardware VSYNC. |
| 639 | EXPECT_CALL(mScheduler->mockRequestHardwareVsync, Call(kDisplayId1, _)).Times(0); |
| 640 | |
| 641 | mScheduler->setDisplayPowerMode(kDisplayId1, hal::PowerMode::ON); |
| 642 | |
| 643 | static constexpr bool kDisallow = true; |
| 644 | mScheduler->disableHardwareVsync(kDisplayId1, kDisallow); |
| 645 | |
| 646 | static constexpr bool kAllowToEnable = false; |
| 647 | mScheduler->resyncAllToHardwareVsync(kAllowToEnable); |
| 648 | } |
| 649 | |
| 650 | TEST_F(SchedulerTest, resyncAllSkipsOffDisplays) FTL_FAKE_GUARD(kMainThreadContext) { |
| 651 | SET_FLAG_FOR_TEST(flags::multithreaded_present, true); |
| 652 | |
| 653 | // resyncAllToHardwareVsync will result in requesting hardware VSYNC on display 1, which is on, |
| 654 | // but not on display 2, which is off. |
| 655 | EXPECT_CALL(mScheduler->mockRequestHardwareVsync, Call(kDisplayId1, true)).Times(1); |
| 656 | EXPECT_CALL(mScheduler->mockRequestHardwareVsync, Call(kDisplayId2, _)).Times(0); |
| 657 | |
| 658 | mScheduler->setDisplayPowerMode(kDisplayId1, hal::PowerMode::ON); |
| 659 | |
| 660 | mScheduler->registerDisplay(kDisplayId2, |
| 661 | std::make_shared<RefreshRateSelector>(kDisplay2Modes, |
| 662 | kDisplay2Mode60->getId())); |
| 663 | ASSERT_EQ(hal::PowerMode::OFF, mScheduler->getDisplayPowerMode(kDisplayId2)); |
| 664 | |
| 665 | static constexpr bool kDisallow = true; |
| 666 | mScheduler->disableHardwareVsync(kDisplayId1, kDisallow); |
| 667 | mScheduler->disableHardwareVsync(kDisplayId2, kDisallow); |
| 668 | |
| 669 | static constexpr bool kAllowToEnable = true; |
| 670 | mScheduler->resyncAllToHardwareVsync(kAllowToEnable); |
| 671 | } |
| 672 | |
| 673 | TEST_F(SchedulerTest, resyncAllLegacyAppliesToOffDisplays) FTL_FAKE_GUARD(kMainThreadContext) { |
| 674 | SET_FLAG_FOR_TEST(flags::multithreaded_present, false); |
| 675 | |
| 676 | // In the legacy code, prior to the flag, resync applied to OFF displays. |
| 677 | EXPECT_CALL(mScheduler->mockRequestHardwareVsync, Call(kDisplayId1, true)).Times(1); |
| 678 | EXPECT_CALL(mScheduler->mockRequestHardwareVsync, Call(kDisplayId2, true)).Times(1); |
| 679 | |
| 680 | mScheduler->setDisplayPowerMode(kDisplayId1, hal::PowerMode::ON); |
| 681 | |
| 682 | mScheduler->registerDisplay(kDisplayId2, |
| 683 | std::make_shared<RefreshRateSelector>(kDisplay2Modes, |
| 684 | kDisplay2Mode60->getId())); |
| 685 | ASSERT_EQ(hal::PowerMode::OFF, mScheduler->getDisplayPowerMode(kDisplayId2)); |
| 686 | |
| 687 | static constexpr bool kDisallow = true; |
| 688 | mScheduler->disableHardwareVsync(kDisplayId1, kDisallow); |
| 689 | mScheduler->disableHardwareVsync(kDisplayId2, kDisallow); |
| 690 | |
| 691 | static constexpr bool kAllowToEnable = true; |
| 692 | mScheduler->resyncAllToHardwareVsync(kAllowToEnable); |
| 693 | } |
| 694 | |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 695 | class AttachedChoreographerTest : public SchedulerTest { |
| 696 | protected: |
| 697 | void frameRateTestScenario(Fps layerFps, int8_t frameRateCompatibility, Fps displayFps, |
| 698 | Fps expectedChoreographerFps); |
| 699 | }; |
| 700 | |
| 701 | TEST_F(AttachedChoreographerTest, registerSingle) { |
| 702 | EXPECT_TRUE(mScheduler->mutableAttachedChoreographers().empty()); |
| 703 | |
| 704 | const sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger()); |
| 705 | |
| 706 | EXPECT_CALL(mSchedulerCallback, onChoreographerAttached); |
| 707 | const sp<IDisplayEventConnection> connection = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 708 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, layer->getHandle()); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 709 | |
| 710 | EXPECT_EQ(1u, mScheduler->mutableAttachedChoreographers().size()); |
| 711 | ASSERT_EQ(1u, mScheduler->mutableAttachedChoreographers().count(layer->getSequence())); |
| 712 | EXPECT_EQ(1u, |
| 713 | mScheduler->mutableAttachedChoreographers()[layer->getSequence()].connections.size()); |
| 714 | EXPECT_FALSE( |
| 715 | mScheduler->mutableAttachedChoreographers()[layer->getSequence()].frameRate.isValid()); |
| 716 | } |
| 717 | |
| 718 | TEST_F(AttachedChoreographerTest, registerMultipleOnSameLayer) { |
| 719 | EXPECT_TRUE(mScheduler->mutableAttachedChoreographers().empty()); |
| 720 | |
| 721 | const sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger()); |
| 722 | const auto handle = layer->getHandle(); |
| 723 | |
| 724 | EXPECT_CALL(mSchedulerCallback, onChoreographerAttached).Times(2); |
| 725 | |
| 726 | EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)) |
| 727 | .WillOnce(Return(0)) |
| 728 | .WillOnce(Return(0)); |
| 729 | |
| 730 | const auto mockConnection1 = sp<MockEventThreadConnection>::make(mEventThread); |
| 731 | const auto mockConnection2 = sp<MockEventThreadConnection>::make(mEventThread); |
| 732 | EXPECT_CALL(*mEventThread, createEventConnection(_, _)) |
| 733 | .WillOnce(Return(mockConnection1)) |
| 734 | .WillOnce(Return(mockConnection2)); |
| 735 | |
| 736 | const sp<IDisplayEventConnection> connection1 = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 737 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, handle); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 738 | const sp<IDisplayEventConnection> connection2 = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 739 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, handle); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 740 | |
| 741 | EXPECT_EQ(1u, mScheduler->mutableAttachedChoreographers().size()); |
| 742 | ASSERT_EQ(1u, mScheduler->mutableAttachedChoreographers().count(layer->getSequence())); |
| 743 | EXPECT_EQ(2u, |
| 744 | mScheduler->mutableAttachedChoreographers()[layer->getSequence()].connections.size()); |
| 745 | EXPECT_FALSE( |
| 746 | mScheduler->mutableAttachedChoreographers()[layer->getSequence()].frameRate.isValid()); |
| 747 | } |
| 748 | |
| 749 | TEST_F(AttachedChoreographerTest, registerMultipleOnDifferentLayers) { |
| 750 | EXPECT_TRUE(mScheduler->mutableAttachedChoreographers().empty()); |
| 751 | |
| 752 | const sp<MockLayer> layer1 = sp<MockLayer>::make(mFlinger.flinger()); |
| 753 | const sp<MockLayer> layer2 = sp<MockLayer>::make(mFlinger.flinger()); |
| 754 | |
| 755 | EXPECT_CALL(mSchedulerCallback, onChoreographerAttached).Times(2); |
| 756 | const sp<IDisplayEventConnection> connection1 = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 757 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, layer1->getHandle()); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 758 | const sp<IDisplayEventConnection> connection2 = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 759 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, layer2->getHandle()); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 760 | |
| 761 | EXPECT_EQ(2u, mScheduler->mutableAttachedChoreographers().size()); |
| 762 | |
| 763 | ASSERT_EQ(1u, mScheduler->mutableAttachedChoreographers().count(layer1->getSequence())); |
| 764 | EXPECT_EQ(1u, |
| 765 | mScheduler->mutableAttachedChoreographers()[layer1->getSequence()] |
| 766 | .connections.size()); |
| 767 | EXPECT_FALSE( |
| 768 | mScheduler->mutableAttachedChoreographers()[layer1->getSequence()].frameRate.isValid()); |
| 769 | |
| 770 | ASSERT_EQ(1u, mScheduler->mutableAttachedChoreographers().count(layer2->getSequence())); |
| 771 | EXPECT_EQ(1u, |
| 772 | mScheduler->mutableAttachedChoreographers()[layer2->getSequence()] |
| 773 | .connections.size()); |
| 774 | EXPECT_FALSE( |
| 775 | mScheduler->mutableAttachedChoreographers()[layer2->getSequence()].frameRate.isValid()); |
| 776 | } |
| 777 | |
| 778 | TEST_F(AttachedChoreographerTest, removedWhenConnectionIsGone) { |
| 779 | EXPECT_TRUE(mScheduler->mutableAttachedChoreographers().empty()); |
| 780 | |
| 781 | const sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger()); |
| 782 | |
| 783 | EXPECT_CALL(mSchedulerCallback, onChoreographerAttached); |
| 784 | |
| 785 | sp<IDisplayEventConnection> connection = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 786 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, layer->getHandle()); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 787 | |
| 788 | ASSERT_EQ(1u, mScheduler->mutableAttachedChoreographers().count(layer->getSequence())); |
| 789 | EXPECT_EQ(1u, |
| 790 | mScheduler->mutableAttachedChoreographers()[layer->getSequence()].connections.size()); |
| 791 | |
| 792 | // The connection is used all over this test, so it is quite hard to release it from here. |
| 793 | // Instead, we just do a small shortcut. |
| 794 | { |
| 795 | EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)).WillOnce(Return(0)); |
| 796 | sp<MockEventThreadConnection> mockConnection = |
| 797 | sp<MockEventThreadConnection>::make(mEventThread); |
| 798 | mScheduler->mutableAttachedChoreographers()[layer->getSequence()].connections.clear(); |
| 799 | mScheduler->mutableAttachedChoreographers()[layer->getSequence()].connections.emplace( |
| 800 | mockConnection); |
| 801 | } |
| 802 | |
| 803 | RequestedLayerState layerState(LayerCreationArgs(layer->getSequence())); |
| 804 | LayerHierarchy hierarchy(&layerState); |
| 805 | mScheduler->updateAttachedChoreographers(hierarchy, 60_Hz); |
| 806 | EXPECT_TRUE(mScheduler->mutableAttachedChoreographers().empty()); |
| 807 | } |
| 808 | |
| 809 | TEST_F(AttachedChoreographerTest, removedWhenLayerIsGone) { |
| 810 | EXPECT_TRUE(mScheduler->mutableAttachedChoreographers().empty()); |
| 811 | |
| 812 | sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger()); |
| 813 | |
| 814 | EXPECT_CALL(mSchedulerCallback, onChoreographerAttached); |
| 815 | const sp<IDisplayEventConnection> connection = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 816 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, layer->getHandle()); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 817 | |
| 818 | layer.clear(); |
| 819 | mFlinger.mutableLayersPendingRemoval().clear(); |
| 820 | EXPECT_TRUE(mScheduler->mutableAttachedChoreographers().empty()); |
| 821 | } |
| 822 | |
| 823 | void AttachedChoreographerTest::frameRateTestScenario(Fps layerFps, int8_t frameRateCompatibility, |
| 824 | Fps displayFps, |
| 825 | Fps expectedChoreographerFps) { |
| 826 | const sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger()); |
| 827 | |
| 828 | EXPECT_CALL(mSchedulerCallback, onChoreographerAttached); |
| 829 | sp<IDisplayEventConnection> connection = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 830 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, layer->getHandle()); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 831 | |
| 832 | RequestedLayerState layerState(LayerCreationArgs(layer->getSequence())); |
| 833 | LayerHierarchy hierarchy(&layerState); |
| 834 | |
| 835 | layerState.frameRate = layerFps.getValue(); |
| 836 | layerState.frameRateCompatibility = frameRateCompatibility; |
| 837 | |
| 838 | mScheduler->updateAttachedChoreographers(hierarchy, displayFps); |
| 839 | |
| 840 | ASSERT_EQ(1u, mScheduler->mutableAttachedChoreographers().count(layer->getSequence())); |
| 841 | EXPECT_EQ(expectedChoreographerFps, |
| 842 | mScheduler->mutableAttachedChoreographers()[layer->getSequence()].frameRate); |
| 843 | EXPECT_EQ(expectedChoreographerFps, mEventThreadConnection->frameRate); |
| 844 | } |
| 845 | |
| 846 | TEST_F(AttachedChoreographerTest, setsFrameRateDefault) { |
| 847 | Fps layerFps = 30_Hz; |
| 848 | int8_t frameRateCompatibility = ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT; |
| 849 | Fps displayFps = 60_Hz; |
| 850 | Fps expectedChoreographerFps = 30_Hz; |
| 851 | |
| 852 | frameRateTestScenario(layerFps, frameRateCompatibility, displayFps, expectedChoreographerFps); |
| 853 | |
| 854 | layerFps = Fps::fromValue(32.7f); |
| 855 | frameRateTestScenario(layerFps, frameRateCompatibility, displayFps, expectedChoreographerFps); |
| 856 | } |
| 857 | |
| 858 | TEST_F(AttachedChoreographerTest, setsFrameRateExact) { |
| 859 | Fps layerFps = 30_Hz; |
| 860 | int8_t frameRateCompatibility = ANATIVEWINDOW_FRAME_RATE_EXACT; |
| 861 | Fps displayFps = 60_Hz; |
| 862 | Fps expectedChoreographerFps = 30_Hz; |
| 863 | |
| 864 | frameRateTestScenario(layerFps, frameRateCompatibility, displayFps, expectedChoreographerFps); |
| 865 | |
| 866 | layerFps = Fps::fromValue(32.7f); |
| 867 | expectedChoreographerFps = {}; |
| 868 | frameRateTestScenario(layerFps, frameRateCompatibility, displayFps, expectedChoreographerFps); |
| 869 | } |
| 870 | |
| 871 | TEST_F(AttachedChoreographerTest, setsFrameRateExactOrMultiple) { |
| 872 | Fps layerFps = 30_Hz; |
| 873 | int8_t frameRateCompatibility = ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE; |
| 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, setsFrameRateParent) { |
| 885 | const sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger()); |
| 886 | const sp<MockLayer> parent = sp<MockLayer>::make(mFlinger.flinger()); |
| 887 | |
| 888 | EXPECT_CALL(mSchedulerCallback, onChoreographerAttached); |
| 889 | sp<IDisplayEventConnection> connection = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 890 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, parent->getHandle()); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 891 | |
| 892 | RequestedLayerState parentState(LayerCreationArgs(parent->getSequence())); |
| 893 | LayerHierarchy parentHierarchy(&parentState); |
| 894 | |
| 895 | RequestedLayerState layerState(LayerCreationArgs(layer->getSequence())); |
| 896 | LayerHierarchy hierarchy(&layerState); |
| 897 | parentHierarchy.mChildren.push_back( |
| 898 | std::make_pair(&hierarchy, LayerHierarchy::Variant::Attached)); |
| 899 | |
| 900 | layerState.frameRate = (30_Hz).getValue(); |
| 901 | layerState.frameRateCompatibility = ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT; |
| 902 | |
| 903 | mScheduler->updateAttachedChoreographers(parentHierarchy, 120_Hz); |
| 904 | |
| 905 | ASSERT_EQ(1u, mScheduler->mutableAttachedChoreographers().count(parent->getSequence())); |
| 906 | |
| 907 | EXPECT_EQ(30_Hz, mScheduler->mutableAttachedChoreographers()[parent->getSequence()].frameRate); |
| 908 | } |
| 909 | |
| 910 | TEST_F(AttachedChoreographerTest, setsFrameRateParent2Children) { |
| 911 | const sp<MockLayer> layer1 = sp<MockLayer>::make(mFlinger.flinger()); |
| 912 | const sp<MockLayer> layer2 = sp<MockLayer>::make(mFlinger.flinger()); |
| 913 | const sp<MockLayer> parent = sp<MockLayer>::make(mFlinger.flinger()); |
| 914 | |
| 915 | EXPECT_CALL(mSchedulerCallback, onChoreographerAttached); |
| 916 | sp<IDisplayEventConnection> connection = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 917 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, parent->getHandle()); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 918 | |
| 919 | RequestedLayerState parentState(LayerCreationArgs(parent->getSequence())); |
| 920 | LayerHierarchy parentHierarchy(&parentState); |
| 921 | |
| 922 | RequestedLayerState layer1State(LayerCreationArgs(layer1->getSequence())); |
| 923 | LayerHierarchy layer1Hierarchy(&layer1State); |
| 924 | parentHierarchy.mChildren.push_back( |
| 925 | std::make_pair(&layer1Hierarchy, LayerHierarchy::Variant::Attached)); |
| 926 | |
| 927 | RequestedLayerState layer2State(LayerCreationArgs(layer1->getSequence())); |
| 928 | LayerHierarchy layer2Hierarchy(&layer2State); |
| 929 | parentHierarchy.mChildren.push_back( |
| 930 | std::make_pair(&layer2Hierarchy, LayerHierarchy::Variant::Attached)); |
| 931 | |
| 932 | layer1State.frameRate = (30_Hz).getValue(); |
| 933 | layer1State.frameRateCompatibility = ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT; |
| 934 | |
| 935 | layer2State.frameRate = (20_Hz).getValue(); |
| 936 | layer2State.frameRateCompatibility = ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT; |
| 937 | |
| 938 | mScheduler->updateAttachedChoreographers(parentHierarchy, 120_Hz); |
| 939 | |
| 940 | ASSERT_EQ(1u, mScheduler->mutableAttachedChoreographers().count(parent->getSequence())); |
| 941 | |
| 942 | EXPECT_EQ(60_Hz, mScheduler->mutableAttachedChoreographers()[parent->getSequence()].frameRate); |
| 943 | } |
| 944 | |
| 945 | TEST_F(AttachedChoreographerTest, setsFrameRateParentConflictingChildren) { |
| 946 | const sp<MockLayer> layer1 = sp<MockLayer>::make(mFlinger.flinger()); |
| 947 | const sp<MockLayer> layer2 = sp<MockLayer>::make(mFlinger.flinger()); |
| 948 | const sp<MockLayer> parent = sp<MockLayer>::make(mFlinger.flinger()); |
| 949 | |
| 950 | EXPECT_CALL(mSchedulerCallback, onChoreographerAttached); |
| 951 | sp<IDisplayEventConnection> connection = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 952 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, parent->getHandle()); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 953 | |
| 954 | RequestedLayerState parentState(LayerCreationArgs(parent->getSequence())); |
| 955 | LayerHierarchy parentHierarchy(&parentState); |
| 956 | |
| 957 | RequestedLayerState layer1State(LayerCreationArgs(layer1->getSequence())); |
| 958 | LayerHierarchy layer1Hierarchy(&layer1State); |
| 959 | parentHierarchy.mChildren.push_back( |
| 960 | std::make_pair(&layer1Hierarchy, LayerHierarchy::Variant::Attached)); |
| 961 | |
| 962 | RequestedLayerState layer2State(LayerCreationArgs(layer1->getSequence())); |
| 963 | LayerHierarchy layer2Hierarchy(&layer2State); |
| 964 | parentHierarchy.mChildren.push_back( |
| 965 | std::make_pair(&layer2Hierarchy, LayerHierarchy::Variant::Attached)); |
| 966 | |
| 967 | layer1State.frameRate = (30_Hz).getValue(); |
| 968 | layer1State.frameRateCompatibility = ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT; |
| 969 | |
| 970 | layer2State.frameRate = (25_Hz).getValue(); |
| 971 | layer2State.frameRateCompatibility = ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT; |
| 972 | |
| 973 | mScheduler->updateAttachedChoreographers(parentHierarchy, 120_Hz); |
| 974 | |
| 975 | ASSERT_EQ(1u, mScheduler->mutableAttachedChoreographers().count(parent->getSequence())); |
| 976 | |
| 977 | EXPECT_EQ(Fps(), mScheduler->mutableAttachedChoreographers()[parent->getSequence()].frameRate); |
| 978 | } |
| 979 | |
| 980 | TEST_F(AttachedChoreographerTest, setsFrameRateChild) { |
| 981 | const sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger()); |
| 982 | const sp<MockLayer> parent = sp<MockLayer>::make(mFlinger.flinger()); |
| 983 | |
| 984 | EXPECT_CALL(mSchedulerCallback, onChoreographerAttached); |
| 985 | sp<IDisplayEventConnection> connection = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 986 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, layer->getHandle()); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 987 | |
| 988 | RequestedLayerState parentState(LayerCreationArgs(parent->getSequence())); |
| 989 | LayerHierarchy parentHierarchy(&parentState); |
| 990 | |
| 991 | RequestedLayerState layerState(LayerCreationArgs(layer->getSequence())); |
| 992 | LayerHierarchy hierarchy(&layerState); |
| 993 | parentHierarchy.mChildren.push_back( |
| 994 | std::make_pair(&hierarchy, LayerHierarchy::Variant::Attached)); |
| 995 | |
| 996 | parentState.frameRate = (30_Hz).getValue(); |
| 997 | parentState.frameRateCompatibility = ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT; |
| 998 | |
| 999 | mScheduler->updateAttachedChoreographers(parentHierarchy, 120_Hz); |
| 1000 | |
| 1001 | ASSERT_EQ(1u, mScheduler->mutableAttachedChoreographers().count(layer->getSequence())); |
| 1002 | |
| 1003 | EXPECT_EQ(30_Hz, mScheduler->mutableAttachedChoreographers()[layer->getSequence()].frameRate); |
| 1004 | } |
| 1005 | |
| 1006 | TEST_F(AttachedChoreographerTest, setsFrameRateChildNotOverriddenByParent) { |
| 1007 | const sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger()); |
| 1008 | const sp<MockLayer> parent = sp<MockLayer>::make(mFlinger.flinger()); |
| 1009 | |
| 1010 | EXPECT_CALL(mSchedulerCallback, onChoreographerAttached); |
| 1011 | sp<IDisplayEventConnection> connection = |
Dominik Laskowski | 4babfc4 | 2024-02-16 12:28:40 -0500 | [diff] [blame] | 1012 | mScheduler->createDisplayEventConnection(Cycle::Render, {}, layer->getHandle()); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 1013 | |
| 1014 | RequestedLayerState parentState(LayerCreationArgs(parent->getSequence())); |
| 1015 | LayerHierarchy parentHierarchy(&parentState); |
| 1016 | |
| 1017 | RequestedLayerState layerState(LayerCreationArgs(layer->getSequence())); |
| 1018 | LayerHierarchy hierarchy(&layerState); |
| 1019 | parentHierarchy.mChildren.push_back( |
| 1020 | std::make_pair(&hierarchy, LayerHierarchy::Variant::Attached)); |
| 1021 | |
| 1022 | parentState.frameRate = (30_Hz).getValue(); |
| 1023 | parentState.frameRateCompatibility = ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT; |
| 1024 | |
| 1025 | layerState.frameRate = (60_Hz).getValue(); |
| 1026 | layerState.frameRateCompatibility = ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT; |
| 1027 | |
| 1028 | mScheduler->updateAttachedChoreographers(parentHierarchy, 120_Hz); |
| 1029 | |
| 1030 | ASSERT_EQ(1u, mScheduler->mutableAttachedChoreographers().count(layer->getSequence())); |
| 1031 | |
| 1032 | EXPECT_EQ(60_Hz, mScheduler->mutableAttachedChoreographers()[layer->getSequence()].frameRate); |
| 1033 | } |
| 1034 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 1035 | } // namespace android::scheduler |