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
20 #include "DisplayTransactionTestHelpers.h"
21
22 namespace android {
23
24 using testing::AnyNumber;
25 using testing::DoAll;
26 using testing::Mock;
27 using testing::Return;
28 using testing::SetArgPointee;
29
30 using android::hardware::graphics::composer::hal::HWDisplayId;
31
32 using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector;
33
DisplayTransactionTest()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());
38
39 // Default to no wide color display support configured
40 mFlinger.mutableHasWideColorDisplay() = false;
41 mFlinger.mutableUseColorManagement() = false;
42 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged;
43
44 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
45 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
46 });
47
48 mFlinger.setCreateNativeWindowSurface([](auto) {
49 ADD_FAILURE() << "Unexpected request to create a native window surface.";
50 return nullptr;
51 });
52
53 injectMockScheduler();
54 mFlinger.mutableEventQueue().reset(mMessageQueue);
55 mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
56 mFlinger.mutableInterceptor() = mSurfaceInterceptor;
57
58 injectMockComposer(0);
59 }
60
~DisplayTransactionTest()61 DisplayTransactionTest::~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
injectMockScheduler()67 void DisplayTransactionTest::injectMockScheduler() {
68 EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_));
69 EXPECT_CALL(*mEventThread, createEventConnection(_, _))
70 .WillOnce(Return(
71 new EventThreadConnection(mEventThread, /*callingUid=*/0, ResyncCallback())));
72
73 EXPECT_CALL(*mSFEventThread, registerDisplayEventConnection(_));
74 EXPECT_CALL(*mSFEventThread, createEventConnection(_, _))
75 .WillOnce(Return(
76 new EventThreadConnection(mSFEventThread, /*callingUid=*/0, ResyncCallback())));
77
78 mFlinger.setupScheduler(std::unique_ptr<scheduler::VsyncController>(mVsyncController),
79 std::unique_ptr<scheduler::VSyncTracker>(mVSyncTracker),
80 std::unique_ptr<EventThread>(mEventThread),
81 std::unique_ptr<EventThread>(mSFEventThread), &mSchedulerCallback);
82 }
83
injectMockComposer(int virtualDisplayCount)84 void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
85 if (mComposer) {
86 // If reinjecting, disable first to prevent the enable below from being a no-op.
87 mFlinger.enableHalVirtualDisplays(false);
88 }
89
90 mComposer = new Hwc2::mock::Composer();
91 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
92
93 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
94 mFlinger.enableHalVirtualDisplays(true);
95
96 Mock::VerifyAndClear(mComposer);
97 }
98
injectFakeBufferQueueFactory()99 void DisplayTransactionTest::injectFakeBufferQueueFactory() {
100 // This setup is only expected once per test.
101 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
102
103 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 });
110 }
111
injectFakeNativeWindowSurfaceFactory()112 void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
113 // This setup is only expected once per test.
114 ASSERT_TRUE(mNativeWindowSurface == nullptr);
115
116 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
117
118 mFlinger.setCreateNativeWindowSurface([this](auto) {
119 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
120 });
121 }
122
injectDefaultInternalDisplay(std::function<void (FakeDisplayDeviceInjector &)> injectExtra)123 sp<DisplayDevice> DisplayTransactionTest::injectDefaultInternalDisplay(
124 std::function<void(FakeDisplayDeviceInjector&)> injectExtra) {
125 constexpr PhysicalDisplayId DEFAULT_DISPLAY_ID(777);
126 constexpr int DEFAULT_DISPLAY_WIDTH = 1080;
127 constexpr int DEFAULT_DISPLAY_HEIGHT = 1920;
128 constexpr HWDisplayId DEFAULT_DISPLAY_HWC_DISPLAY_ID = 0;
129
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
142 constexpr auto kConnectionType = ui::DisplayConnectionType::Internal;
143 constexpr bool kIsPrimary = true;
144
145 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);
157
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
hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId)170 bool DisplayTransactionTest::hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId) {
171 return mFlinger.mutableHwcPhysicalDisplayIdMap().count(hwcDisplayId) == 1;
172 }
173
hasTransactionFlagSet(int flag)174 bool DisplayTransactionTest::hasTransactionFlagSet(int flag) {
175 return mFlinger.mutableTransactionFlags() & flag;
176 }
177
hasDisplayDevice(sp<IBinder> displayToken)178 bool DisplayTransactionTest::hasDisplayDevice(sp<IBinder> displayToken) {
179 return mFlinger.mutableDisplays().count(displayToken) == 1;
180 }
181
getDisplayDevice(sp<IBinder> displayToken)182 sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) {
183 return mFlinger.mutableDisplays()[displayToken];
184 }
185
hasCurrentDisplayState(sp<IBinder> displayToken)186 bool DisplayTransactionTest::hasCurrentDisplayState(sp<IBinder> displayToken) {
187 return mFlinger.mutableCurrentState().displays.indexOfKey(displayToken) >= 0;
188 }
189
getCurrentDisplayState(sp<IBinder> displayToken)190 const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(sp<IBinder> displayToken) {
191 return mFlinger.mutableCurrentState().displays.valueFor(displayToken);
192 }
193
hasDrawingDisplayState(sp<IBinder> displayToken)194 bool DisplayTransactionTest::hasDrawingDisplayState(sp<IBinder> displayToken) {
195 return mFlinger.mutableDrawingState().displays.indexOfKey(displayToken) >= 0;
196 }
197
getDrawingDisplayState(sp<IBinder> displayToken)198 const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(sp<IBinder> displayToken) {
199 return mFlinger.mutableDrawingState().displays.valueFor(displayToken);
200 }
201
202 } // namespace android
203