blob: f0311bdaabfa292ec7052feae547bdeebde2a45e [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
44 // Default to using HWC virtual displays
45 mFlinger.mutableUseHwcVirtualDisplays() = true;
46
Lloyd Pique5b36f3f2018-01-17 11:57:07 -080047 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
48 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
49 });
50
Lloyd Pique1fa4d462018-01-22 18:03:16 -080051 mFlinger.setCreateNativeWindowSurface([](auto) {
52 ADD_FAILURE() << "Unexpected request to create a native window surface.";
53 return nullptr;
54 });
55
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070056 injectMockScheduler();
Lloyd Pique1fa4d462018-01-22 18:03:16 -080057 mFlinger.mutableEventQueue().reset(mMessageQueue);
Peiyong Lin833074a2018-08-28 11:53:54 -070058 mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
Pablo Gamito5cfc6e52020-09-10 11:18:03 +000059 mFlinger.mutableInterceptor() = mSurfaceInterceptor;
Lloyd Piquee39cad22017-12-20 17:01:29 -080060
Lloyd Piquec11e0d32018-01-22 18:44:59 -080061 injectMockComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -080062}
63
64DisplayTransactionTest::~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 Laskowski7c9dbf92019-08-01 17:57:31 -070070void DisplayTransactionTest::injectMockScheduler() {
Ana Krulecafb45842019-02-13 13:33:03 -080071 EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_));
Dominik Laskowski98041832019-08-01 18:35:59 -070072 EXPECT_CALL(*mEventThread, createEventConnection(_, _))
Ady Abraham0bb6a472020-10-12 10:22:13 -070073 .WillOnce(Return(new EventThreadConnection(mEventThread, /*callingUid=*/0,
74 ResyncCallback(),
Dominik Laskowski98041832019-08-01 18:35:59 -070075 ISurfaceComposer::eConfigChangedSuppress)));
76
Ana Krulecafb45842019-02-13 13:33:03 -080077 EXPECT_CALL(*mSFEventThread, registerDisplayEventConnection(_));
Dominik Laskowski98041832019-08-01 18:35:59 -070078 EXPECT_CALL(*mSFEventThread, createEventConnection(_, _))
Ady Abraham0bb6a472020-10-12 10:22:13 -070079 .WillOnce(Return(new EventThreadConnection(mSFEventThread, /*callingUid=*/0,
80 ResyncCallback(),
Dominik Laskowski98041832019-08-01 18:35:59 -070081 ISurfaceComposer::eConfigChangedSuppress)));
Ana Krulecafb45842019-02-13 13:33:03 -080082
Ady Abraham8cb21882020-08-26 18:22:05 -070083 mFlinger.setupScheduler(std::unique_ptr<scheduler::VsyncController>(mVsyncController),
84 std::unique_ptr<scheduler::VSyncTracker>(mVSyncTracker),
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070085 std::unique_ptr<EventThread>(mEventThread),
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070086 std::unique_ptr<EventThread>(mSFEventThread), &mSchedulerCallback);
Ana Krulecafb45842019-02-13 13:33:03 -080087}
88
Lloyd Piquec11e0d32018-01-22 18:44:59 -080089void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
90 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -080091 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
92 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -080093
Lloyd Piquee39cad22017-12-20 17:01:29 -080094 Mock::VerifyAndClear(mComposer);
95}
96
Lloyd Piquec11e0d32018-01-22 18:44:59 -080097void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -080098 // This setup is only expected once per test.
99 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
100
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800101 mConsumer = new mock::GraphicBufferConsumer();
102 mProducer = new mock::GraphicBufferProducer();
103
104 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
105 *outProducer = mProducer;
106 *outConsumer = mConsumer;
107 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800108}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800109
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800110void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800111 // This setup is only expected once per test.
112 ASSERT_TRUE(mNativeWindowSurface == nullptr);
113
Lloyd Pique22098362018-09-13 11:46:49 -0700114 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800115
Lloyd Pique22098362018-09-13 11:46:49 -0700116 mFlinger.setCreateNativeWindowSurface([this](auto) {
117 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
118 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800119}
120
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700121sp<DisplayDevice> DisplayTransactionTest::injectDefaultInternalDisplay(
122 std::function<void(FakeDisplayDeviceInjector&)> injectExtra) {
Marin Shalamanova524a092020-07-27 21:39:55 +0200123 constexpr PhysicalDisplayId DEFAULT_DISPLAY_ID(777);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700124 constexpr int DEFAULT_DISPLAY_WIDTH = 1080;
125 constexpr int DEFAULT_DISPLAY_HEIGHT = 1920;
Peiyong Line9d809e2020-04-14 13:10:48 -0700126 constexpr HWDisplayId DEFAULT_DISPLAY_HWC_DISPLAY_ID = 0;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700127
128 // The DisplayDevice is required to have a framebuffer (behind the
129 // ANativeWindow interface) which uses the actual hardware display
130 // size.
131 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
132 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_DISPLAY_WIDTH), Return(0)));
133 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
134 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_DISPLAY_HEIGHT), Return(0)));
135 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT));
136 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT));
137 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64));
138 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT)).Times(AnyNumber());
139
140 auto compositionDisplay = compositionengine::impl::
141 createDisplay(mFlinger.getCompositionEngine(),
142 compositionengine::DisplayCreationArgsBuilder()
143 .setPhysical(
144 {DEFAULT_DISPLAY_ID, DisplayConnectionType::Internal})
145 .setPixels({DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT})
146 .setPowerAdvisor(&mPowerAdvisor)
147 .build());
148
149 auto injector =
150 FakeDisplayDeviceInjector(mFlinger, compositionDisplay, DisplayConnectionType::Internal,
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100151 DEFAULT_DISPLAY_HWC_DISPLAY_ID, true /* isPrimary */);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700152
153 injector.setNativeWindow(mNativeWindow);
154 if (injectExtra) {
155 injectExtra(injector);
156 }
157
158 auto displayDevice = injector.inject();
159
160 Mock::VerifyAndClear(mNativeWindow.get());
161
162 return displayDevice;
163}
164
Peiyong Line9d809e2020-04-14 13:10:48 -0700165bool DisplayTransactionTest::hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700166 return mFlinger.mutableHwcPhysicalDisplayIdMap().count(hwcDisplayId) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800167}
168
169bool DisplayTransactionTest::hasTransactionFlagSet(int flag) {
170 return mFlinger.mutableTransactionFlags() & flag;
171}
172
173bool DisplayTransactionTest::hasDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700174 return mFlinger.mutableDisplays().count(displayToken) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800175}
176
177sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700178 return mFlinger.mutableDisplays()[displayToken];
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800179}
180
181bool DisplayTransactionTest::hasCurrentDisplayState(sp<IBinder> displayToken) {
182 return mFlinger.mutableCurrentState().displays.indexOfKey(displayToken) >= 0;
183}
184
185const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(sp<IBinder> displayToken) {
186 return mFlinger.mutableCurrentState().displays.valueFor(displayToken);
187}
188
189bool DisplayTransactionTest::hasDrawingDisplayState(sp<IBinder> displayToken) {
190 return mFlinger.mutableDrawingState().displays.indexOfKey(displayToken) >= 0;
191}
192
193const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(sp<IBinder> displayToken) {
194 return mFlinger.mutableDrawingState().displays.valueFor(displayToken);
195}
196
Lloyd Piquef58625d2017-12-19 13:22:33 -0800197} // namespace android