Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #undef LOG_TAG |
| 18 | #define LOG_TAG "LibSurfaceFlingerUnittests" |
| 19 | |
Marin Shalamanov | 07b1ff3 | 2020-10-07 16:57:22 +0200 | [diff] [blame] | 20 | #include "DisplayTransactionTestHelpers.h" |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 21 | |
| 22 | namespace android { |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 23 | |
Lloyd Pique | aad4ebf | 2019-10-03 17:58:30 -0700 | [diff] [blame] | 24 | using testing::AnyNumber; |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 25 | using testing::DoAll; |
| 26 | using testing::Mock; |
| 27 | using testing::Return; |
| 28 | using testing::SetArgPointee; |
| 29 | |
Marin Shalamanov | 07b1ff3 | 2020-10-07 16:57:22 +0200 | [diff] [blame] | 30 | using android::hardware::graphics::composer::hal::HWDisplayId; |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 31 | |
Dominik Laskowski | aee9a62 | 2023-02-11 14:24:19 -0500 | [diff] [blame] | 32 | DisplayTransactionTest::DisplayTransactionTest(bool withMockScheduler) { |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 33 | const ::testing::TestInfo* const test_info = |
| 34 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 35 | ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 36 | |
Lloyd Pique | 30db640 | 2023-06-26 18:56:51 +0000 | [diff] [blame] | 37 | mFlinger.mutableSupportsWideColor() = false; |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 38 | mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged; |
Lloyd Pique | c11e0d3 | 2018-01-22 18:44:59 -0800 | [diff] [blame] | 39 | |
Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 40 | mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) { |
| 41 | ADD_FAILURE() << "Unexpected request to create a buffer queue."; |
| 42 | }); |
| 43 | |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 44 | mFlinger.setCreateNativeWindowSurface([](auto) { |
| 45 | ADD_FAILURE() << "Unexpected request to create a native window surface."; |
| 46 | return nullptr; |
| 47 | }); |
| 48 | |
Dominik Laskowski | aee9a62 | 2023-02-11 14:24:19 -0500 | [diff] [blame] | 49 | if (withMockScheduler) { |
| 50 | injectMockScheduler(PhysicalDisplayId::fromPort(0)); |
| 51 | } |
| 52 | |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 53 | mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine)); |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 54 | |
Lloyd Pique | c11e0d3 | 2018-01-22 18:44:59 -0800 | [diff] [blame] | 55 | injectMockComposer(0); |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | DisplayTransactionTest::~DisplayTransactionTest() { |
| 59 | const ::testing::TestInfo* const test_info = |
| 60 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 61 | ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); |
Alec Mouri | cdf1679 | 2021-12-10 13:16:06 -0800 | [diff] [blame] | 62 | mFlinger.resetScheduler(nullptr); |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Dominik Laskowski | aee9a62 | 2023-02-11 14:24:19 -0500 | [diff] [blame] | 65 | void DisplayTransactionTest::injectMockScheduler(PhysicalDisplayId displayId) { |
| 66 | LOG_ALWAYS_FATAL_IF(mFlinger.scheduler()); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 67 | mFlinger.setupScheduler(std::make_unique<mock::VsyncController>(), |
| 68 | std::make_shared<mock::VSyncTracker>(), |
Dominik Laskowski | 7c9dbf9 | 2019-08-01 17:57:31 -0700 | [diff] [blame] | 69 | std::unique_ptr<EventThread>(mEventThread), |
Dominik Laskowski | 83bd771 | 2022-01-07 14:30:53 -0800 | [diff] [blame] | 70 | std::unique_ptr<EventThread>(mSFEventThread), |
Dominik Laskowski | aee9a62 | 2023-02-11 14:24:19 -0500 | [diff] [blame] | 71 | TestableSurfaceFlinger::DefaultDisplayMode{displayId}, |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame] | 72 | TestableSurfaceFlinger::SchedulerCallbackImpl::kMock); |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Lloyd Pique | c11e0d3 | 2018-01-22 18:44:59 -0800 | [diff] [blame] | 75 | void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) { |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 76 | if (mComposer) { |
| 77 | // If reinjecting, disable first to prevent the enable below from being a no-op. |
| 78 | mFlinger.enableHalVirtualDisplays(false); |
| 79 | } |
| 80 | |
Lloyd Pique | c11e0d3 | 2018-01-22 18:44:59 -0800 | [diff] [blame] | 81 | mComposer = new Hwc2::mock::Composer(); |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 82 | mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer)); |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 83 | |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 84 | EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount)); |
| 85 | mFlinger.enableHalVirtualDisplays(true); |
| 86 | |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 87 | Mock::VerifyAndClear(mComposer); |
| 88 | } |
| 89 | |
Lloyd Pique | c11e0d3 | 2018-01-22 18:44:59 -0800 | [diff] [blame] | 90 | void DisplayTransactionTest::injectFakeBufferQueueFactory() { |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 91 | // This setup is only expected once per test. |
| 92 | ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr); |
| 93 | |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 94 | mConsumer = sp<mock::GraphicBufferConsumer>::make(); |
| 95 | mProducer = sp<mock::GraphicBufferProducer>::make(); |
Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 96 | |
| 97 | mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) { |
| 98 | *outProducer = mProducer; |
| 99 | *outConsumer = mConsumer; |
| 100 | }); |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 101 | } |
Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 102 | |
Lloyd Pique | c11e0d3 | 2018-01-22 18:44:59 -0800 | [diff] [blame] | 103 | void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() { |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 104 | // This setup is only expected once per test. |
| 105 | ASSERT_TRUE(mNativeWindowSurface == nullptr); |
| 106 | |
Lloyd Pique | 2209836 | 2018-09-13 11:46:49 -0700 | [diff] [blame] | 107 | mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface(); |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 108 | |
Lloyd Pique | 2209836 | 2018-09-13 11:46:49 -0700 | [diff] [blame] | 109 | mFlinger.setCreateNativeWindowSurface([this](auto) { |
| 110 | return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface); |
| 111 | }); |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 114 | bool DisplayTransactionTest::hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId) const { |
Dominik Laskowski | bab5128 | 2022-08-12 09:28:55 -0700 | [diff] [blame] | 115 | const auto& map = mFlinger.hwcPhysicalDisplayIdMap(); |
| 116 | |
| 117 | const auto it = map.find(hwcDisplayId); |
| 118 | if (it == map.end()) return false; |
| 119 | |
| 120 | return mFlinger.hwcDisplayData().count(it->second) == 1; |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 123 | bool DisplayTransactionTest::hasTransactionFlagSet(int32_t flag) const { |
| 124 | return mFlinger.transactionFlags() & flag; |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 127 | bool DisplayTransactionTest::hasDisplayDevice(const sp<IBinder>& displayToken) const { |
| 128 | return mFlinger.displays().contains(displayToken); |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 131 | const DisplayDevice& DisplayTransactionTest::getDisplayDevice( |
| 132 | const sp<IBinder>& displayToken) const { |
| 133 | return *mFlinger.displays().get(displayToken)->get(); |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 136 | bool DisplayTransactionTest::hasCurrentDisplayState(const sp<IBinder>& displayToken) const { |
| 137 | return mFlinger.currentState().displays.indexOfKey(displayToken) >= 0; |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 140 | const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState( |
| 141 | const sp<IBinder>& displayToken) const { |
| 142 | return mFlinger.currentState().displays.valueFor(displayToken); |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 145 | bool DisplayTransactionTest::hasDrawingDisplayState(const sp<IBinder>& displayToken) const { |
| 146 | return mFlinger.drawingState().displays.indexOfKey(displayToken) >= 0; |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 149 | const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState( |
| 150 | const sp<IBinder>& displayToken) const { |
| 151 | return mFlinger.drawingState().displays.valueFor(displayToken); |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 154 | } // namespace android |