blob: c318e28ffe7e7bcbd1f03fe572d07cf4cf548a56 [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
Lloyd Piquec11e0d32018-01-22 18:44:59 -080039 // Default to no wide color display support configured
40 mFlinger.mutableHasWideColorDisplay() = false;
Lloyd Pique6a3b4462019-03-07 20:58:12 -080041 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080042
Lloyd Pique5b36f3f2018-01-17 11:57:07 -080043 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
44 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
45 });
46
Lloyd Pique1fa4d462018-01-22 18:03:16 -080047 mFlinger.setCreateNativeWindowSurface([](auto) {
48 ADD_FAILURE() << "Unexpected request to create a native window surface.";
49 return nullptr;
50 });
51
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070052 injectMockScheduler();
Peiyong Lin833074a2018-08-28 11:53:54 -070053 mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
Pablo Gamito5cfc6e52020-09-10 11:18:03 +000054 mFlinger.mutableInterceptor() = mSurfaceInterceptor;
Lloyd Piquee39cad22017-12-20 17:01:29 -080055
Lloyd Piquec11e0d32018-01-22 18:44:59 -080056 injectMockComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -080057}
58
59DisplayTransactionTest::~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 Mouricdf16792021-12-10 13:16:06 -080063 mFlinger.resetScheduler(nullptr);
Lloyd Piquef58625d2017-12-19 13:22:33 -080064}
65
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070066void DisplayTransactionTest::injectMockScheduler() {
Ana Krulecafb45842019-02-13 13:33:03 -080067 EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_));
Dominik Laskowski98041832019-08-01 18:35:59 -070068 EXPECT_CALL(*mEventThread, createEventConnection(_, _))
Ady Abraham62f216c2020-10-13 19:07:23 -070069 .WillOnce(Return(
70 new EventThreadConnection(mEventThread, /*callingUid=*/0, 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 Abraham62f216c2020-10-13 19:07:23 -070074 .WillOnce(Return(
75 new EventThreadConnection(mSFEventThread, /*callingUid=*/0, ResyncCallback())));
Ana Krulecafb45842019-02-13 13:33:03 -080076
Ady Abraham8cb21882020-08-26 18:22:05 -070077 mFlinger.setupScheduler(std::unique_ptr<scheduler::VsyncController>(mVsyncController),
78 std::unique_ptr<scheduler::VSyncTracker>(mVSyncTracker),
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070079 std::unique_ptr<EventThread>(mEventThread),
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070080 std::unique_ptr<EventThread>(mSFEventThread), &mSchedulerCallback);
Ana Krulecafb45842019-02-13 13:33:03 -080081}
82
Lloyd Piquec11e0d32018-01-22 18:44:59 -080083void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
Dominik Laskowski3dce4f42021-03-08 20:48:28 -080084 if (mComposer) {
85 // If reinjecting, disable first to prevent the enable below from being a no-op.
86 mFlinger.enableHalVirtualDisplays(false);
87 }
88
Lloyd Piquec11e0d32018-01-22 18:44:59 -080089 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -080090 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -080091
Dominik Laskowski3dce4f42021-03-08 20:48:28 -080092 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
93 mFlinger.enableHalVirtualDisplays(true);
94
Lloyd Piquee39cad22017-12-20 17:01:29 -080095 Mock::VerifyAndClear(mComposer);
96}
97
Lloyd Piquec11e0d32018-01-22 18:44:59 -080098void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -080099 // This setup is only expected once per test.
100 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
101
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800102 mConsumer = new mock::GraphicBufferConsumer();
103 mProducer = new mock::GraphicBufferProducer();
104
105 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
106 *outProducer = mProducer;
107 *outConsumer = mConsumer;
108 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800109}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800110
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800111void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800112 // This setup is only expected once per test.
113 ASSERT_TRUE(mNativeWindowSurface == nullptr);
114
Lloyd Pique22098362018-09-13 11:46:49 -0700115 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800116
Lloyd Pique22098362018-09-13 11:46:49 -0700117 mFlinger.setCreateNativeWindowSurface([this](auto) {
118 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
119 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800120}
121
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700122sp<DisplayDevice> DisplayTransactionTest::injectDefaultInternalDisplay(
123 std::function<void(FakeDisplayDeviceInjector&)> injectExtra) {
Dominik Laskowskif1833852021-03-23 15:06:50 -0700124 constexpr PhysicalDisplayId DEFAULT_DISPLAY_ID = PhysicalDisplayId::fromPort(255u);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700125 constexpr int DEFAULT_DISPLAY_WIDTH = 1080;
126 constexpr int DEFAULT_DISPLAY_HEIGHT = 1920;
Peiyong Line9d809e2020-04-14 13:10:48 -0700127 constexpr HWDisplayId DEFAULT_DISPLAY_HWC_DISPLAY_ID = 0;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700128
129 // The DisplayDevice is required to have a framebuffer (behind the
130 // ANativeWindow interface) which uses the actual hardware display
131 // size.
132 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
133 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_DISPLAY_WIDTH), Return(0)));
134 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
135 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_DISPLAY_HEIGHT), Return(0)));
136 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT));
137 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT));
138 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64));
139 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT)).Times(AnyNumber());
140
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800141 auto compositionDisplay =
142 compositionengine::impl::createDisplay(mFlinger.getCompositionEngine(),
143 compositionengine::DisplayCreationArgsBuilder()
144 .setId(DEFAULT_DISPLAY_ID)
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800145 .setPixels({DEFAULT_DISPLAY_WIDTH,
146 DEFAULT_DISPLAY_HEIGHT})
147 .setPowerAdvisor(&mPowerAdvisor)
148 .build());
149
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700150 constexpr bool kIsPrimary = true;
151 auto injector = FakeDisplayDeviceInjector(mFlinger, compositionDisplay,
152 ui::DisplayConnectionType::Internal,
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800153 DEFAULT_DISPLAY_HWC_DISPLAY_ID, kIsPrimary);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700154
155 injector.setNativeWindow(mNativeWindow);
156 if (injectExtra) {
157 injectExtra(injector);
158 }
159
160 auto displayDevice = injector.inject();
161
162 Mock::VerifyAndClear(mNativeWindow.get());
163
164 return displayDevice;
165}
166
Peiyong Line9d809e2020-04-14 13:10:48 -0700167bool DisplayTransactionTest::hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700168 return mFlinger.mutableHwcPhysicalDisplayIdMap().count(hwcDisplayId) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800169}
170
171bool DisplayTransactionTest::hasTransactionFlagSet(int flag) {
172 return mFlinger.mutableTransactionFlags() & flag;
173}
174
175bool DisplayTransactionTest::hasDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700176 return mFlinger.mutableDisplays().count(displayToken) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800177}
178
179sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700180 return mFlinger.mutableDisplays()[displayToken];
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800181}
182
183bool DisplayTransactionTest::hasCurrentDisplayState(sp<IBinder> displayToken) {
184 return mFlinger.mutableCurrentState().displays.indexOfKey(displayToken) >= 0;
185}
186
187const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(sp<IBinder> displayToken) {
188 return mFlinger.mutableCurrentState().displays.valueFor(displayToken);
189}
190
191bool DisplayTransactionTest::hasDrawingDisplayState(sp<IBinder> displayToken) {
192 return mFlinger.mutableDrawingState().displays.indexOfKey(displayToken) >= 0;
193}
194
195const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(sp<IBinder> displayToken) {
196 return mFlinger.mutableDrawingState().displays.valueFor(displayToken);
197}
198
Lloyd Piquef58625d2017-12-19 13:22:33 -0800199} // namespace android