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