| 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 |  | 
| Dominik Laskowski | 788945b | 2022-08-30 12:10:56 -0700 | [diff] [blame] | 39 | mFlinger.mutableSupportsWideColor() = false; | 
| Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 40 | mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged; | 
| Lloyd Pique | c11e0d3 | 2018-01-22 18:44:59 -0800 | [diff] [blame] | 41 |  | 
| Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 42 | mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) { | 
|  | 43 | ADD_FAILURE() << "Unexpected request to create a buffer queue."; | 
|  | 44 | }); | 
|  | 45 |  | 
| Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 46 | mFlinger.setCreateNativeWindowSurface([](auto) { | 
|  | 47 | ADD_FAILURE() << "Unexpected request to create a native window surface."; | 
|  | 48 | return nullptr; | 
|  | 49 | }); | 
|  | 50 |  | 
| Dominik Laskowski | 7c9dbf9 | 2019-08-01 17:57:31 -0700 | [diff] [blame] | 51 | injectMockScheduler(); | 
| Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 52 | mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine)); | 
| Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 53 |  | 
| Lloyd Pique | c11e0d3 | 2018-01-22 18:44:59 -0800 | [diff] [blame] | 54 | injectMockComposer(0); | 
| Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 55 | } | 
|  | 56 |  | 
|  | 57 | DisplayTransactionTest::~DisplayTransactionTest() { | 
|  | 58 | const ::testing::TestInfo* const test_info = | 
|  | 59 | ::testing::UnitTest::GetInstance()->current_test_info(); | 
|  | 60 | 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] | 61 | mFlinger.resetScheduler(nullptr); | 
| Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 62 | } | 
|  | 63 |  | 
| Dominik Laskowski | 7c9dbf9 | 2019-08-01 17:57:31 -0700 | [diff] [blame] | 64 | void DisplayTransactionTest::injectMockScheduler() { | 
| Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 65 | EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)); | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 66 | EXPECT_CALL(*mEventThread, createEventConnection(_, _)) | 
| Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 67 | .WillOnce(Return(sp<EventThreadConnection>::make(mEventThread, | 
|  | 68 | mock::EventThread::kCallingUid, | 
|  | 69 | ResyncCallback()))); | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 70 |  | 
| Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 71 | EXPECT_CALL(*mSFEventThread, registerDisplayEventConnection(_)); | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 72 | EXPECT_CALL(*mSFEventThread, createEventConnection(_, _)) | 
| Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 73 | .WillOnce(Return(sp<EventThreadConnection>::make(mSFEventThread, | 
|  | 74 | mock::EventThread::kCallingUid, | 
|  | 75 | ResyncCallback()))); | 
| Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 76 |  | 
| Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 77 | mFlinger.setupScheduler(std::unique_ptr<scheduler::VsyncController>(mVsyncController), | 
|  | 78 | std::unique_ptr<scheduler::VSyncTracker>(mVSyncTracker), | 
| Dominik Laskowski | 7c9dbf9 | 2019-08-01 17:57:31 -0700 | [diff] [blame] | 79 | std::unique_ptr<EventThread>(mEventThread), | 
| Dominik Laskowski | 83bd771 | 2022-01-07 14:30:53 -0800 | [diff] [blame] | 80 | std::unique_ptr<EventThread>(mSFEventThread), | 
|  | 81 | TestableSurfaceFlinger::SchedulerCallbackImpl::kMock); | 
| Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
| Lloyd Pique | c11e0d3 | 2018-01-22 18:44:59 -0800 | [diff] [blame] | 84 | void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) { | 
| Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 85 | if (mComposer) { | 
|  | 86 | // If reinjecting, disable first to prevent the enable below from being a no-op. | 
|  | 87 | mFlinger.enableHalVirtualDisplays(false); | 
|  | 88 | } | 
|  | 89 |  | 
| Lloyd Pique | c11e0d3 | 2018-01-22 18:44:59 -0800 | [diff] [blame] | 90 | mComposer = new Hwc2::mock::Composer(); | 
| Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 91 | mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer)); | 
| Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 92 |  | 
| Dominik Laskowski | 3dce4f4 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 93 | EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount)); | 
|  | 94 | mFlinger.enableHalVirtualDisplays(true); | 
|  | 95 |  | 
| Lloyd Pique | e39cad2 | 2017-12-20 17:01:29 -0800 | [diff] [blame] | 96 | Mock::VerifyAndClear(mComposer); | 
|  | 97 | } | 
|  | 98 |  | 
| Lloyd Pique | c11e0d3 | 2018-01-22 18:44:59 -0800 | [diff] [blame] | 99 | void DisplayTransactionTest::injectFakeBufferQueueFactory() { | 
| Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 100 | // This setup is only expected once per test. | 
|  | 101 | ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr); | 
|  | 102 |  | 
| Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 103 | mConsumer = sp<mock::GraphicBufferConsumer>::make(); | 
|  | 104 | mProducer = sp<mock::GraphicBufferProducer>::make(); | 
| Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 105 |  | 
|  | 106 | mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) { | 
|  | 107 | *outProducer = mProducer; | 
|  | 108 | *outConsumer = mConsumer; | 
|  | 109 | }); | 
| Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 110 | } | 
| Lloyd Pique | 5b36f3f | 2018-01-17 11:57:07 -0800 | [diff] [blame] | 111 |  | 
| Lloyd Pique | c11e0d3 | 2018-01-22 18:44:59 -0800 | [diff] [blame] | 112 | void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() { | 
| Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 113 | // This setup is only expected once per test. | 
|  | 114 | ASSERT_TRUE(mNativeWindowSurface == nullptr); | 
|  | 115 |  | 
| Lloyd Pique | 2209836 | 2018-09-13 11:46:49 -0700 | [diff] [blame] | 116 | mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface(); | 
| Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 117 |  | 
| Lloyd Pique | 2209836 | 2018-09-13 11:46:49 -0700 | [diff] [blame] | 118 | mFlinger.setCreateNativeWindowSurface([this](auto) { | 
|  | 119 | return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface); | 
|  | 120 | }); | 
| 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::hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId) const { | 
| Dominik Laskowski | bab5128 | 2022-08-12 09:28:55 -0700 | [diff] [blame] | 124 | const auto& map = mFlinger.hwcPhysicalDisplayIdMap(); | 
|  | 125 |  | 
|  | 126 | const auto it = map.find(hwcDisplayId); | 
|  | 127 | if (it == map.end()) return false; | 
|  | 128 |  | 
|  | 129 | return mFlinger.hwcDisplayData().count(it->second) == 1; | 
| Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 130 | } | 
|  | 131 |  | 
| Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 132 | bool DisplayTransactionTest::hasTransactionFlagSet(int32_t flag) const { | 
|  | 133 | return mFlinger.transactionFlags() & flag; | 
| 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::hasDisplayDevice(const sp<IBinder>& displayToken) const { | 
|  | 137 | return mFlinger.displays().contains(displayToken); | 
| 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 DisplayDevice& DisplayTransactionTest::getDisplayDevice( | 
|  | 141 | const sp<IBinder>& displayToken) const { | 
|  | 142 | return *mFlinger.displays().get(displayToken)->get(); | 
| 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::hasCurrentDisplayState(const sp<IBinder>& displayToken) const { | 
|  | 146 | return mFlinger.currentState().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::getCurrentDisplayState( | 
|  | 150 | const sp<IBinder>& displayToken) const { | 
|  | 151 | return mFlinger.currentState().displays.valueFor(displayToken); | 
| Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 152 | } | 
|  | 153 |  | 
| Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 154 | bool DisplayTransactionTest::hasDrawingDisplayState(const sp<IBinder>& displayToken) const { | 
|  | 155 | return mFlinger.drawingState().displays.indexOfKey(displayToken) >= 0; | 
| Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 156 | } | 
|  | 157 |  | 
| Dominik Laskowski | eb62731 | 2022-04-07 09:13:16 -0700 | [diff] [blame] | 158 | const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState( | 
|  | 159 | const sp<IBinder>& displayToken) const { | 
|  | 160 | return mFlinger.drawingState().displays.valueFor(displayToken); | 
| Lloyd Pique | 1fa4d46 | 2018-01-22 18:03:16 -0800 | [diff] [blame] | 161 | } | 
|  | 162 |  | 
| Lloyd Pique | f58625d | 2017-12-19 13:22:33 -0800 | [diff] [blame] | 163 | } // namespace android |