blob: 60b0f5334cc34fd81477b94cc8609c0241c7c1b3 [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;
Peiyong Lin13effd12018-07-24 17:01:47 -070041 mFlinger.mutableUseColorManagement() = false;
Lloyd Pique6a3b4462019-03-07 20:58:12 -080042 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080043
Lloyd Pique5b36f3f2018-01-17 11:57:07 -080044 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
45 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
46 });
47
Lloyd Pique1fa4d462018-01-22 18:03:16 -080048 mFlinger.setCreateNativeWindowSurface([](auto) {
49 ADD_FAILURE() << "Unexpected request to create a native window surface.";
50 return nullptr;
51 });
52
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070053 injectMockScheduler();
Lloyd Pique1fa4d462018-01-22 18:03:16 -080054 mFlinger.mutableEventQueue().reset(mMessageQueue);
Peiyong Lin833074a2018-08-28 11:53:54 -070055 mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
Pablo Gamito5cfc6e52020-09-10 11:18:03 +000056 mFlinger.mutableInterceptor() = mSurfaceInterceptor;
Lloyd Piquee39cad22017-12-20 17:01:29 -080057
Lloyd Piquec11e0d32018-01-22 18:44:59 -080058 injectMockComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -080059}
60
61DisplayTransactionTest::~DisplayTransactionTest() {
62 const ::testing::TestInfo* const test_info =
63 ::testing::UnitTest::GetInstance()->current_test_info();
64 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
65}
66
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070067void DisplayTransactionTest::injectMockScheduler() {
Ana Krulecafb45842019-02-13 13:33:03 -080068 EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_));
Dominik Laskowski98041832019-08-01 18:35:59 -070069 EXPECT_CALL(*mEventThread, createEventConnection(_, _))
Ady Abraham62f216c2020-10-13 19:07:23 -070070 .WillOnce(Return(
71 new EventThreadConnection(mEventThread, /*callingUid=*/0, ResyncCallback())));
Dominik Laskowski98041832019-08-01 18:35:59 -070072
Ana Krulecafb45842019-02-13 13:33:03 -080073 EXPECT_CALL(*mSFEventThread, registerDisplayEventConnection(_));
Dominik Laskowski98041832019-08-01 18:35:59 -070074 EXPECT_CALL(*mSFEventThread, createEventConnection(_, _))
Ady Abraham62f216c2020-10-13 19:07:23 -070075 .WillOnce(Return(
76 new EventThreadConnection(mSFEventThread, /*callingUid=*/0, 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 Laskowski8b01cc02020-07-14 19:02:41 -070081 std::unique_ptr<EventThread>(mSFEventThread), &mSchedulerCallback);
Ana Krulecafb45842019-02-13 13:33:03 -080082}
83
Lloyd Piquec11e0d32018-01-22 18:44:59 -080084void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
Dominik Laskowski3dce4f42021-03-08 20:48:28 -080085 if (mComposer) {
86 // If reinjecting, disable first to prevent the enable below from being a no-op.
87 mFlinger.enableHalVirtualDisplays(false);
88 }
89
Lloyd Piquec11e0d32018-01-22 18:44:59 -080090 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -080091 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -080092
Dominik Laskowski3dce4f42021-03-08 20:48:28 -080093 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
94 mFlinger.enableHalVirtualDisplays(true);
95
Lloyd Piquee39cad22017-12-20 17:01:29 -080096 Mock::VerifyAndClear(mComposer);
97}
98
Lloyd Piquec11e0d32018-01-22 18:44:59 -080099void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800100 // This setup is only expected once per test.
101 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
102
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800103 mConsumer = new mock::GraphicBufferConsumer();
104 mProducer = new mock::GraphicBufferProducer();
105
106 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
107 *outProducer = mProducer;
108 *outConsumer = mConsumer;
109 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800110}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800111
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800112void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800113 // This setup is only expected once per test.
114 ASSERT_TRUE(mNativeWindowSurface == nullptr);
115
Lloyd Pique22098362018-09-13 11:46:49 -0700116 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800117
Lloyd Pique22098362018-09-13 11:46:49 -0700118 mFlinger.setCreateNativeWindowSurface([this](auto) {
119 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
120 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800121}
122
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700123sp<DisplayDevice> DisplayTransactionTest::injectDefaultInternalDisplay(
124 std::function<void(FakeDisplayDeviceInjector&)> injectExtra) {
Dominik Laskowskif1833852021-03-23 15:06:50 -0700125 constexpr PhysicalDisplayId DEFAULT_DISPLAY_ID = PhysicalDisplayId::fromPort(255u);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700126 constexpr int DEFAULT_DISPLAY_WIDTH = 1080;
127 constexpr int DEFAULT_DISPLAY_HEIGHT = 1920;
Peiyong Line9d809e2020-04-14 13:10:48 -0700128 constexpr HWDisplayId DEFAULT_DISPLAY_HWC_DISPLAY_ID = 0;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700129
130 // The DisplayDevice is required to have a framebuffer (behind the
131 // ANativeWindow interface) which uses the actual hardware display
132 // size.
133 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
134 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_DISPLAY_WIDTH), Return(0)));
135 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
136 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_DISPLAY_HEIGHT), Return(0)));
137 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT));
138 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT));
139 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64));
140 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT)).Times(AnyNumber());
141
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800142 constexpr auto kConnectionType = ui::DisplayConnectionType::Internal;
143 constexpr bool kIsPrimary = true;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700144
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800145 auto compositionDisplay =
146 compositionengine::impl::createDisplay(mFlinger.getCompositionEngine(),
147 compositionengine::DisplayCreationArgsBuilder()
148 .setId(DEFAULT_DISPLAY_ID)
149 .setConnectionType(kConnectionType)
150 .setPixels({DEFAULT_DISPLAY_WIDTH,
151 DEFAULT_DISPLAY_HEIGHT})
152 .setPowerAdvisor(&mPowerAdvisor)
153 .build());
154
155 auto injector = FakeDisplayDeviceInjector(mFlinger, compositionDisplay, kConnectionType,
156 DEFAULT_DISPLAY_HWC_DISPLAY_ID, kIsPrimary);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700157
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
Peiyong Line9d809e2020-04-14 13:10:48 -0700170bool DisplayTransactionTest::hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700171 return mFlinger.mutableHwcPhysicalDisplayIdMap().count(hwcDisplayId) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800172}
173
174bool DisplayTransactionTest::hasTransactionFlagSet(int flag) {
175 return mFlinger.mutableTransactionFlags() & flag;
176}
177
178bool DisplayTransactionTest::hasDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700179 return mFlinger.mutableDisplays().count(displayToken) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800180}
181
182sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700183 return mFlinger.mutableDisplays()[displayToken];
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800184}
185
186bool DisplayTransactionTest::hasCurrentDisplayState(sp<IBinder> displayToken) {
187 return mFlinger.mutableCurrentState().displays.indexOfKey(displayToken) >= 0;
188}
189
190const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(sp<IBinder> displayToken) {
191 return mFlinger.mutableCurrentState().displays.valueFor(displayToken);
192}
193
194bool DisplayTransactionTest::hasDrawingDisplayState(sp<IBinder> displayToken) {
195 return mFlinger.mutableDrawingState().displays.indexOfKey(displayToken) >= 0;
196}
197
198const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(sp<IBinder> displayToken) {
199 return mFlinger.mutableDrawingState().displays.valueFor(displayToken);
200}
201
Lloyd Piquef58625d2017-12-19 13:22:33 -0800202} // namespace android