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
DisplayTransactionTest(bool withMockScheduler)32 DisplayTransactionTest::DisplayTransactionTest(bool withMockScheduler) {
33 const ::testing::TestInfo* const test_info =
34 ::testing::UnitTest::GetInstance()->current_test_info();
35 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
36
37 mFlinger.mutableSupportsWideColor() = false;
38 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged;
39
40 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
41 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
42 });
43
44 mFlinger.setCreateNativeWindowSurface([](auto) {
45 ADD_FAILURE() << "Unexpected request to create a native window surface.";
46 return nullptr;
47 });
48
49 if (withMockScheduler) {
50 injectMockScheduler(PhysicalDisplayId::fromPort(0));
51 }
52
53 mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
54
55 injectMockComposer(0);
56 }
57
~DisplayTransactionTest()58 DisplayTransactionTest::~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());
62 mFlinger.resetScheduler(nullptr);
63 }
64
injectMockScheduler(PhysicalDisplayId displayId)65 void DisplayTransactionTest::injectMockScheduler(PhysicalDisplayId displayId) {
66 LOG_ALWAYS_FATAL_IF(mFlinger.scheduler());
67
68 EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_));
69 EXPECT_CALL(*mEventThread, createEventConnection(_, _))
70 .WillOnce(Return(sp<EventThreadConnection>::make(mEventThread,
71 mock::EventThread::kCallingUid,
72 ResyncCallback())));
73
74 EXPECT_CALL(*mSFEventThread, registerDisplayEventConnection(_));
75 EXPECT_CALL(*mSFEventThread, createEventConnection(_, _))
76 .WillOnce(Return(sp<EventThreadConnection>::make(mSFEventThread,
77 mock::EventThread::kCallingUid,
78 ResyncCallback())));
79
80 mFlinger.setupScheduler(std::make_unique<mock::VsyncController>(),
81 std::make_shared<mock::VSyncTracker>(),
82 std::unique_ptr<EventThread>(mEventThread),
83 std::unique_ptr<EventThread>(mSFEventThread),
84 TestableSurfaceFlinger::DefaultDisplayMode{displayId},
85 TestableSurfaceFlinger::SchedulerCallbackImpl::kMock);
86 }
87
injectMockComposer(int virtualDisplayCount)88 void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
89 if (mComposer) {
90 // If reinjecting, disable first to prevent the enable below from being a no-op.
91 mFlinger.enableHalVirtualDisplays(false);
92 }
93
94 mComposer = new Hwc2::mock::Composer();
95 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
96
97 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
98 mFlinger.enableHalVirtualDisplays(true);
99
100 Mock::VerifyAndClear(mComposer);
101 }
102
injectFakeBufferQueueFactory()103 void DisplayTransactionTest::injectFakeBufferQueueFactory() {
104 // This setup is only expected once per test.
105 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
106
107 mConsumer = sp<mock::GraphicBufferConsumer>::make();
108 mProducer = sp<mock::GraphicBufferProducer>::make();
109
110 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
111 *outProducer = mProducer;
112 *outConsumer = mConsumer;
113 });
114 }
115
injectFakeNativeWindowSurfaceFactory()116 void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
117 // This setup is only expected once per test.
118 ASSERT_TRUE(mNativeWindowSurface == nullptr);
119
120 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
121
122 mFlinger.setCreateNativeWindowSurface([this](auto) {
123 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
124 });
125 }
126
hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId) const127 bool DisplayTransactionTest::hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId) const {
128 const auto& map = mFlinger.hwcPhysicalDisplayIdMap();
129
130 const auto it = map.find(hwcDisplayId);
131 if (it == map.end()) return false;
132
133 return mFlinger.hwcDisplayData().count(it->second) == 1;
134 }
135
hasTransactionFlagSet(int32_t flag) const136 bool DisplayTransactionTest::hasTransactionFlagSet(int32_t flag) const {
137 return mFlinger.transactionFlags() & flag;
138 }
139
hasDisplayDevice(const sp<IBinder> & displayToken) const140 bool DisplayTransactionTest::hasDisplayDevice(const sp<IBinder>& displayToken) const {
141 return mFlinger.displays().contains(displayToken);
142 }
143
getDisplayDevice(const sp<IBinder> & displayToken) const144 const DisplayDevice& DisplayTransactionTest::getDisplayDevice(
145 const sp<IBinder>& displayToken) const {
146 return *mFlinger.displays().get(displayToken)->get();
147 }
148
hasCurrentDisplayState(const sp<IBinder> & displayToken) const149 bool DisplayTransactionTest::hasCurrentDisplayState(const sp<IBinder>& displayToken) const {
150 return mFlinger.currentState().displays.indexOfKey(displayToken) >= 0;
151 }
152
getCurrentDisplayState(const sp<IBinder> & displayToken) const153 const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(
154 const sp<IBinder>& displayToken) const {
155 return mFlinger.currentState().displays.valueFor(displayToken);
156 }
157
hasDrawingDisplayState(const sp<IBinder> & displayToken) const158 bool DisplayTransactionTest::hasDrawingDisplayState(const sp<IBinder>& displayToken) const {
159 return mFlinger.drawingState().displays.indexOfKey(displayToken) >= 0;
160 }
161
getDrawingDisplayState(const sp<IBinder> & displayToken) const162 const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(
163 const sp<IBinder>& displayToken) const {
164 return mFlinger.drawingState().displays.valueFor(displayToken);
165 }
166
167 } // namespace android
168