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(
71                     sp<EventThreadConnection>::make(mEventThread, mock::EventThread::kCallingUid)));
72 
73     EXPECT_CALL(*mSFEventThread, registerDisplayEventConnection(_));
74     EXPECT_CALL(*mSFEventThread, createEventConnection(_, _))
75             .WillOnce(Return(sp<EventThreadConnection>::make(mSFEventThread,
76                                                              mock::EventThread::kCallingUid)));
77 
78     mFlinger.setupScheduler(std::make_unique<mock::VsyncController>(),
79                             std::make_shared<mock::VSyncTracker>(),
80                             std::unique_ptr<EventThread>(mEventThread),
81                             std::unique_ptr<EventThread>(mSFEventThread),
82                             TestableSurfaceFlinger::DefaultDisplayMode{displayId},
83                             TestableSurfaceFlinger::SchedulerCallbackImpl::kMock);
84 }
85 
injectMockComposer(int virtualDisplayCount)86 void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
87     if (mComposer) {
88         // If reinjecting, disable first to prevent the enable below from being a no-op.
89         mFlinger.enableHalVirtualDisplays(false);
90     }
91 
92     mComposer = new Hwc2::mock::Composer();
93     mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
94 
95     EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
96     mFlinger.enableHalVirtualDisplays(true);
97 
98     Mock::VerifyAndClear(mComposer);
99 }
100 
injectFakeBufferQueueFactory()101 void DisplayTransactionTest::injectFakeBufferQueueFactory() {
102     // This setup is only expected once per test.
103     ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
104 
105     mConsumer = sp<mock::GraphicBufferConsumer>::make();
106     mProducer = sp<mock::GraphicBufferProducer>::make();
107 
108     mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
109         *outProducer = mProducer;
110         *outConsumer = mConsumer;
111     });
112 }
113 
injectFakeNativeWindowSurfaceFactory()114 void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
115     // This setup is only expected once per test.
116     ASSERT_TRUE(mNativeWindowSurface == nullptr);
117 
118     mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
119 
120     mFlinger.setCreateNativeWindowSurface([this](auto) {
121         return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
122     });
123 }
124 
hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId) const125 bool DisplayTransactionTest::hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId) const {
126     const auto& map = mFlinger.hwcPhysicalDisplayIdMap();
127 
128     const auto it = map.find(hwcDisplayId);
129     if (it == map.end()) return false;
130 
131     return mFlinger.hwcDisplayData().count(it->second) == 1;
132 }
133 
hasTransactionFlagSet(int32_t flag) const134 bool DisplayTransactionTest::hasTransactionFlagSet(int32_t flag) const {
135     return mFlinger.transactionFlags() & flag;
136 }
137 
hasDisplayDevice(const sp<IBinder> & displayToken) const138 bool DisplayTransactionTest::hasDisplayDevice(const sp<IBinder>& displayToken) const {
139     return mFlinger.displays().contains(displayToken);
140 }
141 
getDisplayDevice(const sp<IBinder> & displayToken) const142 const DisplayDevice& DisplayTransactionTest::getDisplayDevice(
143         const sp<IBinder>& displayToken) const {
144     return *mFlinger.displays().get(displayToken)->get();
145 }
146 
hasCurrentDisplayState(const sp<IBinder> & displayToken) const147 bool DisplayTransactionTest::hasCurrentDisplayState(const sp<IBinder>& displayToken) const {
148     return mFlinger.currentState().displays.indexOfKey(displayToken) >= 0;
149 }
150 
getCurrentDisplayState(const sp<IBinder> & displayToken) const151 const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(
152         const sp<IBinder>& displayToken) const {
153     return mFlinger.currentState().displays.valueFor(displayToken);
154 }
155 
hasDrawingDisplayState(const sp<IBinder> & displayToken) const156 bool DisplayTransactionTest::hasDrawingDisplayState(const sp<IBinder>& displayToken) const {
157     return mFlinger.drawingState().displays.indexOfKey(displayToken) >= 0;
158 }
159 
getDrawingDisplayState(const sp<IBinder> & displayToken) const160 const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(
161         const sp<IBinder>& displayToken) const {
162     return mFlinger.drawingState().displays.valueFor(displayToken);
163 }
164 
165 } // namespace android
166