blob: abe32c9794db2e082013a1ac1c5c1e702d43bd99 [file] [log] [blame]
Lloyd Piquef58625d2017-12-19 13:22:33 -08001/*
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 Shalamanov07b1ff32020-10-07 16:57:22 +020020#include "DisplayTransactionTestHelpers.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080021
22namespace android {
Lloyd Piquef58625d2017-12-19 13:22:33 -080023
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070024using testing::AnyNumber;
Lloyd Piquee39cad22017-12-20 17:01:29 -080025using testing::DoAll;
26using testing::Mock;
27using testing::Return;
28using testing::SetArgPointee;
29
Marin Shalamanov07b1ff32020-10-07 16:57:22 +020030using android::hardware::graphics::composer::hal::HWDisplayId;
Lloyd Piquee39cad22017-12-20 17:01:29 -080031
Lloyd Piquec11e0d32018-01-22 18:44:59 -080032using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector;
Lloyd Piquef58625d2017-12-19 13:22:33 -080033
34DisplayTransactionTest::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 Piquee39cad22017-12-20 17:01:29 -080038
Dominik Laskowski788945b2022-08-30 12:10:56 -070039 mFlinger.mutableSupportsWideColor() = false;
Lloyd Pique6a3b4462019-03-07 20:58:12 -080040 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080041
Lloyd Pique5b36f3f2018-01-17 11:57:07 -080042 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
43 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
44 });
45
Lloyd Pique1fa4d462018-01-22 18:03:16 -080046 mFlinger.setCreateNativeWindowSurface([](auto) {
47 ADD_FAILURE() << "Unexpected request to create a native window surface.";
48 return nullptr;
49 });
50
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070051 injectMockScheduler();
Peiyong Lin833074a2018-08-28 11:53:54 -070052 mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
Pablo Gamito5cfc6e52020-09-10 11:18:03 +000053 mFlinger.mutableInterceptor() = mSurfaceInterceptor;
Lloyd Piquee39cad22017-12-20 17:01:29 -080054
Lloyd Piquec11e0d32018-01-22 18:44:59 -080055 injectMockComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -080056}
57
58DisplayTransactionTest::~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 Mouricdf16792021-12-10 13:16:06 -080062 mFlinger.resetScheduler(nullptr);
Lloyd Piquef58625d2017-12-19 13:22:33 -080063}
64
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070065void DisplayTransactionTest::injectMockScheduler() {
Ana Krulecafb45842019-02-13 13:33:03 -080066 EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_));
Dominik Laskowski98041832019-08-01 18:35:59 -070067 EXPECT_CALL(*mEventThread, createEventConnection(_, _))
Ady Abrahamd11bade2022-08-01 16:18:03 -070068 .WillOnce(Return(sp<EventThreadConnection>::make(mEventThread,
69 mock::EventThread::kCallingUid,
70 ResyncCallback())));
Dominik Laskowski98041832019-08-01 18:35:59 -070071
Ana Krulecafb45842019-02-13 13:33:03 -080072 EXPECT_CALL(*mSFEventThread, registerDisplayEventConnection(_));
Dominik Laskowski98041832019-08-01 18:35:59 -070073 EXPECT_CALL(*mSFEventThread, createEventConnection(_, _))
Ady Abrahamd11bade2022-08-01 16:18:03 -070074 .WillOnce(Return(sp<EventThreadConnection>::make(mSFEventThread,
75 mock::EventThread::kCallingUid,
76 ResyncCallback())));
Ana Krulecafb45842019-02-13 13:33:03 -080077
Ady Abraham8cb21882020-08-26 18:22:05 -070078 mFlinger.setupScheduler(std::unique_ptr<scheduler::VsyncController>(mVsyncController),
79 std::unique_ptr<scheduler::VSyncTracker>(mVSyncTracker),
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070080 std::unique_ptr<EventThread>(mEventThread),
Dominik Laskowski83bd7712022-01-07 14:30:53 -080081 std::unique_ptr<EventThread>(mSFEventThread),
82 TestableSurfaceFlinger::SchedulerCallbackImpl::kMock);
Ana Krulecafb45842019-02-13 13:33:03 -080083}
84
Lloyd Piquec11e0d32018-01-22 18:44:59 -080085void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
Dominik Laskowski3dce4f42021-03-08 20:48:28 -080086 if (mComposer) {
87 // If reinjecting, disable first to prevent the enable below from being a no-op.
88 mFlinger.enableHalVirtualDisplays(false);
89 }
90
Lloyd Piquec11e0d32018-01-22 18:44:59 -080091 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -080092 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -080093
Dominik Laskowski3dce4f42021-03-08 20:48:28 -080094 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
95 mFlinger.enableHalVirtualDisplays(true);
96
Lloyd Piquee39cad22017-12-20 17:01:29 -080097 Mock::VerifyAndClear(mComposer);
98}
99
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800100void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800101 // This setup is only expected once per test.
102 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
103
Ady Abrahamd11bade2022-08-01 16:18:03 -0700104 mConsumer = sp<mock::GraphicBufferConsumer>::make();
105 mProducer = sp<mock::GraphicBufferProducer>::make();
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800106
107 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
108 *outProducer = mProducer;
109 *outConsumer = mConsumer;
110 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800111}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800112
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800113void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800114 // This setup is only expected once per test.
115 ASSERT_TRUE(mNativeWindowSurface == nullptr);
116
Lloyd Pique22098362018-09-13 11:46:49 -0700117 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800118
Lloyd Pique22098362018-09-13 11:46:49 -0700119 mFlinger.setCreateNativeWindowSurface([this](auto) {
120 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
121 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800122}
123
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700124sp<DisplayDevice> DisplayTransactionTest::injectDefaultInternalDisplay(
125 std::function<void(FakeDisplayDeviceInjector&)> injectExtra) {
Dominik Laskowskif1833852021-03-23 15:06:50 -0700126 constexpr PhysicalDisplayId DEFAULT_DISPLAY_ID = PhysicalDisplayId::fromPort(255u);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700127 constexpr int DEFAULT_DISPLAY_WIDTH = 1080;
128 constexpr int DEFAULT_DISPLAY_HEIGHT = 1920;
Peiyong Line9d809e2020-04-14 13:10:48 -0700129 constexpr HWDisplayId DEFAULT_DISPLAY_HWC_DISPLAY_ID = 0;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700130
131 // The DisplayDevice is required to have a framebuffer (behind the
132 // ANativeWindow interface) which uses the actual hardware display
133 // size.
134 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
135 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_DISPLAY_WIDTH), Return(0)));
136 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
137 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_DISPLAY_HEIGHT), Return(0)));
138 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT));
139 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT));
140 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64));
141 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT)).Times(AnyNumber());
142
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800143 auto compositionDisplay =
144 compositionengine::impl::createDisplay(mFlinger.getCompositionEngine(),
145 compositionengine::DisplayCreationArgsBuilder()
146 .setId(DEFAULT_DISPLAY_ID)
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800147 .setPixels({DEFAULT_DISPLAY_WIDTH,
148 DEFAULT_DISPLAY_HEIGHT})
149 .setPowerAdvisor(&mPowerAdvisor)
150 .build());
151
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700152 constexpr bool kIsPrimary = true;
153 auto injector = FakeDisplayDeviceInjector(mFlinger, compositionDisplay,
154 ui::DisplayConnectionType::Internal,
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800155 DEFAULT_DISPLAY_HWC_DISPLAY_ID, kIsPrimary);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700156
157 injector.setNativeWindow(mNativeWindow);
158 if (injectExtra) {
159 injectExtra(injector);
160 }
161
162 auto displayDevice = injector.inject();
163
164 Mock::VerifyAndClear(mNativeWindow.get());
165
166 return displayDevice;
167}
168
Dominik Laskowskieb627312022-04-07 09:13:16 -0700169bool DisplayTransactionTest::hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId) const {
Dominik Laskowskibab51282022-08-12 09:28:55 -0700170 const auto& map = mFlinger.hwcPhysicalDisplayIdMap();
171
172 const auto it = map.find(hwcDisplayId);
173 if (it == map.end()) return false;
174
175 return mFlinger.hwcDisplayData().count(it->second) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800176}
177
Dominik Laskowskieb627312022-04-07 09:13:16 -0700178bool DisplayTransactionTest::hasTransactionFlagSet(int32_t flag) const {
179 return mFlinger.transactionFlags() & flag;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800180}
181
Dominik Laskowskieb627312022-04-07 09:13:16 -0700182bool DisplayTransactionTest::hasDisplayDevice(const sp<IBinder>& displayToken) const {
183 return mFlinger.displays().contains(displayToken);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800184}
185
Dominik Laskowskieb627312022-04-07 09:13:16 -0700186const DisplayDevice& DisplayTransactionTest::getDisplayDevice(
187 const sp<IBinder>& displayToken) const {
188 return *mFlinger.displays().get(displayToken)->get();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800189}
190
Dominik Laskowskieb627312022-04-07 09:13:16 -0700191bool DisplayTransactionTest::hasCurrentDisplayState(const sp<IBinder>& displayToken) const {
192 return mFlinger.currentState().displays.indexOfKey(displayToken) >= 0;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800193}
194
Dominik Laskowskieb627312022-04-07 09:13:16 -0700195const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(
196 const sp<IBinder>& displayToken) const {
197 return mFlinger.currentState().displays.valueFor(displayToken);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800198}
199
Dominik Laskowskieb627312022-04-07 09:13:16 -0700200bool DisplayTransactionTest::hasDrawingDisplayState(const sp<IBinder>& displayToken) const {
201 return mFlinger.drawingState().displays.indexOfKey(displayToken) >= 0;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800202}
203
Dominik Laskowskieb627312022-04-07 09:13:16 -0700204const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(
205 const sp<IBinder>& displayToken) const {
206 return mFlinger.drawingState().displays.valueFor(displayToken);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800207}
208
Lloyd Piquef58625d2017-12-19 13:22:33 -0800209} // namespace android