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 | |
Lloyd Pique | c11e0d3 | 2018-01-22 18:44:59 -0800 | [diff] [blame] | 32 | using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector; |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 33 | |
| 34 | DisplayTransactionTest::DisplayTransactionTest() { |
| 35 | const ::testing::TestInfo* const test_info = |
| 36 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 37 | 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] | 38 | |
Lloyd Pique | c11e0d3 | 2018-01-22 18:44:59 -0800 | [diff] [blame] | 39 | // Default to no wide color display support configured |
| 40 | mFlinger.mutableHasWideColorDisplay() = false; |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 41 | mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged; |
Lloyd Pique | c11e0d3 | 2018-01-22 18:44:59 -0800 | [diff] [blame] | 42 | |
Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 43 | mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) { |
| 44 | ADD_FAILURE() << "Unexpected request to create a buffer queue."; |
| 45 | }); |
| 46 | |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 47 | mFlinger.setCreateNativeWindowSurface([](auto) { |
| 48 | ADD_FAILURE() << "Unexpected request to create a native window surface."; |
| 49 | return nullptr; |
| 50 | }); |
| 51 | |
Dominik Laskowski | 7c9dbf9 | 2019-08-01 17:57:31 -0700 | [diff] [blame] | 52 | injectMockScheduler(); |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 53 | mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine)); |
Pablo Gamito | 5cfc6e5 | 2020-09-10 11:18:03 +0000 | [diff] [blame] | 54 | mFlinger.mutableInterceptor() = mSurfaceInterceptor; |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 55 | |
Lloyd Pique | c11e0d3 | 2018-01-22 18:44:59 -0800 | [diff] [blame] | 56 | injectMockComposer(0); |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | DisplayTransactionTest::~DisplayTransactionTest() { |
| 60 | const ::testing::TestInfo* const test_info = |
| 61 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 62 | 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] | 63 | mFlinger.resetScheduler(nullptr); |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Dominik Laskowski | 7c9dbf9 | 2019-08-01 17:57:31 -0700 | [diff] [blame] | 66 | void DisplayTransactionTest::injectMockScheduler() { |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 67 | EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 68 | EXPECT_CALL(*mEventThread, createEventConnection(_, _)) |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame^] | 69 | .WillOnce(Return(sp<EventThreadConnection>::make(mEventThread, |
| 70 | mock::EventThread::kCallingUid, |
| 71 | ResyncCallback()))); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 72 | |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 73 | EXPECT_CALL(*mSFEventThread, registerDisplayEventConnection(_)); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 74 | EXPECT_CALL(*mSFEventThread, createEventConnection(_, _)) |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame^] | 75 | .WillOnce(Return(sp<EventThreadConnection>::make(mSFEventThread, |
| 76 | mock::EventThread::kCallingUid, |
| 77 | ResyncCallback()))); |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 78 | |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 79 | mFlinger.setupScheduler(std::unique_ptr<scheduler::VsyncController>(mVsyncController), |
| 80 | std::unique_ptr<scheduler::VSyncTracker>(mVSyncTracker), |
Dominik Laskowski | 7c9dbf9 | 2019-08-01 17:57:31 -0700 | [diff] [blame] | 81 | std::unique_ptr<EventThread>(mEventThread), |
Dominik Laskowski | 83bd771 | 2022-01-07 14:30:53 -0800 | [diff] [blame] | 82 | std::unique_ptr<EventThread>(mSFEventThread), |
| 83 | TestableSurfaceFlinger::SchedulerCallbackImpl::kMock); |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Lloyd Pique | c11e0d3 | 2018-01-22 18:44:59 -0800 | [diff] [blame] | 86 | void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) { |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 87 | if (mComposer) { |
| 88 | // If reinjecting, disable first to prevent the enable below from being a no-op. |
| 89 | mFlinger.enableHalVirtualDisplays(false); |
| 90 | } |
| 91 | |
Lloyd Pique | c11e0d3 | 2018-01-22 18:44:59 -0800 | [diff] [blame] | 92 | mComposer = new Hwc2::mock::Composer(); |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 93 | mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer)); |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 94 | |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 95 | EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount)); |
| 96 | mFlinger.enableHalVirtualDisplays(true); |
| 97 | |
Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 98 | Mock::VerifyAndClear(mComposer); |
| 99 | } |
| 100 | |
Lloyd Pique | c11e0d3 | 2018-01-22 18:44:59 -0800 | [diff] [blame] | 101 | void DisplayTransactionTest::injectFakeBufferQueueFactory() { |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 102 | // This setup is only expected once per test. |
| 103 | ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr); |
| 104 | |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame^] | 105 | mConsumer = sp<mock::GraphicBufferConsumer>::make(); |
| 106 | mProducer = sp<mock::GraphicBufferProducer>::make(); |
Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 107 | |
| 108 | mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) { |
| 109 | *outProducer = mProducer; |
| 110 | *outConsumer = mConsumer; |
| 111 | }); |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 112 | } |
Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 113 | |
Lloyd Pique | c11e0d3 | 2018-01-22 18:44:59 -0800 | [diff] [blame] | 114 | void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() { |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 115 | // This setup is only expected once per test. |
| 116 | ASSERT_TRUE(mNativeWindowSurface == nullptr); |
| 117 | |
Lloyd Pique | 2209836 | 2018-09-13 11:46:49 -0700 | [diff] [blame] | 118 | mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface(); |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 119 | |
Lloyd Pique | 2209836 | 2018-09-13 11:46:49 -0700 | [diff] [blame] | 120 | mFlinger.setCreateNativeWindowSurface([this](auto) { |
| 121 | return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface); |
| 122 | }); |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Lloyd Pique | aad4ebf | 2019-10-03 17:58:30 -0700 | [diff] [blame] | 125 | sp<DisplayDevice> DisplayTransactionTest::injectDefaultInternalDisplay( |
| 126 | std::function<void(FakeDisplayDeviceInjector&)> injectExtra) { |
Dominik Laskowski | f183385 | 2021-03-23 15:06:50 -0700 | [diff] [blame] | 127 | constexpr PhysicalDisplayId DEFAULT_DISPLAY_ID = PhysicalDisplayId::fromPort(255u); |
Lloyd Pique | aad4ebf | 2019-10-03 17:58:30 -0700 | [diff] [blame] | 128 | constexpr int DEFAULT_DISPLAY_WIDTH = 1080; |
| 129 | constexpr int DEFAULT_DISPLAY_HEIGHT = 1920; |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 130 | constexpr HWDisplayId DEFAULT_DISPLAY_HWC_DISPLAY_ID = 0; |
Lloyd Pique | aad4ebf | 2019-10-03 17:58:30 -0700 | [diff] [blame] | 131 | |
| 132 | // The DisplayDevice is required to have a framebuffer (behind the |
| 133 | // ANativeWindow interface) which uses the actual hardware display |
| 134 | // size. |
| 135 | EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_WIDTH, _)) |
| 136 | .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_DISPLAY_WIDTH), Return(0))); |
| 137 | EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _)) |
| 138 | .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_DISPLAY_HEIGHT), Return(0))); |
| 139 | EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT)); |
| 140 | EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT)); |
| 141 | EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64)); |
| 142 | EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT)).Times(AnyNumber()); |
| 143 | |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 144 | auto compositionDisplay = |
| 145 | compositionengine::impl::createDisplay(mFlinger.getCompositionEngine(), |
| 146 | compositionengine::DisplayCreationArgsBuilder() |
| 147 | .setId(DEFAULT_DISPLAY_ID) |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 148 | .setPixels({DEFAULT_DISPLAY_WIDTH, |
| 149 | DEFAULT_DISPLAY_HEIGHT}) |
| 150 | .setPowerAdvisor(&mPowerAdvisor) |
| 151 | .build()); |
| 152 | |
Dominik Laskowski | 29fa146 | 2021-04-27 15:51:50 -0700 | [diff] [blame] | 153 | constexpr bool kIsPrimary = true; |
| 154 | auto injector = FakeDisplayDeviceInjector(mFlinger, compositionDisplay, |
| 155 | ui::DisplayConnectionType::Internal, |
Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 156 | DEFAULT_DISPLAY_HWC_DISPLAY_ID, kIsPrimary); |
Lloyd Pique | aad4ebf | 2019-10-03 17:58:30 -0700 | [diff] [blame] | 157 | |
| 158 | injector.setNativeWindow(mNativeWindow); |
| 159 | if (injectExtra) { |
| 160 | injectExtra(injector); |
| 161 | } |
| 162 | |
| 163 | auto displayDevice = injector.inject(); |
| 164 | |
| 165 | Mock::VerifyAndClear(mNativeWindow.get()); |
| 166 | |
| 167 | return displayDevice; |
| 168 | } |
| 169 | |
Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 170 | bool DisplayTransactionTest::hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId) const { |
| 171 | return mFlinger.hwcPhysicalDisplayIdMap().count(hwcDisplayId) == 1; |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 174 | bool DisplayTransactionTest::hasTransactionFlagSet(int32_t flag) const { |
| 175 | return mFlinger.transactionFlags() & flag; |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 178 | bool DisplayTransactionTest::hasDisplayDevice(const sp<IBinder>& displayToken) const { |
| 179 | return mFlinger.displays().contains(displayToken); |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 182 | const DisplayDevice& DisplayTransactionTest::getDisplayDevice( |
| 183 | const sp<IBinder>& displayToken) const { |
| 184 | return *mFlinger.displays().get(displayToken)->get(); |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 187 | bool DisplayTransactionTest::hasCurrentDisplayState(const sp<IBinder>& displayToken) const { |
| 188 | return mFlinger.currentState().displays.indexOfKey(displayToken) >= 0; |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 191 | const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState( |
| 192 | const sp<IBinder>& displayToken) const { |
| 193 | return mFlinger.currentState().displays.valueFor(displayToken); |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 196 | bool DisplayTransactionTest::hasDrawingDisplayState(const sp<IBinder>& displayToken) const { |
| 197 | return mFlinger.drawingState().displays.indexOfKey(displayToken) >= 0; |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 200 | const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState( |
| 201 | const sp<IBinder>& displayToken) const { |
| 202 | return mFlinger.drawingState().displays.valueFor(displayToken); |
Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 205 | } // namespace android |