xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/tests/unittests/DisplayTransactionTest.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
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     mFlinger.setupScheduler(std::make_unique<mock::VsyncController>(),
68                             std::make_shared<mock::VSyncTracker>(),
69                             std::unique_ptr<EventThread>(mEventThread),
70                             std::unique_ptr<EventThread>(mSFEventThread),
71                             TestableSurfaceFlinger::DefaultDisplayMode{displayId},
72                             TestableSurfaceFlinger::SchedulerCallbackImpl::kMock);
73 }
74 
injectMockComposer(int virtualDisplayCount)75 void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
76     if (mComposer) {
77         // If reinjecting, disable first to prevent the enable below from being a no-op.
78         mFlinger.enableHalVirtualDisplays(false);
79     }
80 
81     mComposer = new Hwc2::mock::Composer();
82     mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
83 
84     EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
85     mFlinger.enableHalVirtualDisplays(true);
86 
87     Mock::VerifyAndClear(mComposer);
88 }
89 
injectFakeBufferQueueFactory()90 void DisplayTransactionTest::injectFakeBufferQueueFactory() {
91     // This setup is only expected once per test.
92     ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
93 
94     mConsumer = sp<mock::GraphicBufferConsumer>::make();
95     mProducer = sp<mock::GraphicBufferProducer>::make();
96 
97     mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
98         *outProducer = mProducer;
99         *outConsumer = mConsumer;
100     });
101 }
102 
injectFakeNativeWindowSurfaceFactory()103 void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
104     // This setup is only expected once per test.
105     ASSERT_TRUE(mNativeWindowSurface == nullptr);
106 
107     mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
108 
109     mFlinger.setCreateNativeWindowSurface([this](auto) {
110         return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
111     });
112 }
113 
hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId) const114 bool DisplayTransactionTest::hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId) const {
115     const auto& map = mFlinger.hwcPhysicalDisplayIdMap();
116 
117     const auto it = map.find(hwcDisplayId);
118     if (it == map.end()) return false;
119 
120     return mFlinger.hwcDisplayData().count(it->second) == 1;
121 }
122 
hasTransactionFlagSet(int32_t flag) const123 bool DisplayTransactionTest::hasTransactionFlagSet(int32_t flag) const {
124     return mFlinger.transactionFlags() & flag;
125 }
126 
hasDisplayDevice(const sp<IBinder> & displayToken) const127 bool DisplayTransactionTest::hasDisplayDevice(const sp<IBinder>& displayToken) const {
128     return mFlinger.displays().contains(displayToken);
129 }
130 
getDisplayDevice(const sp<IBinder> & displayToken) const131 const DisplayDevice& DisplayTransactionTest::getDisplayDevice(
132         const sp<IBinder>& displayToken) const {
133     return *mFlinger.displays().get(displayToken)->get();
134 }
135 
hasCurrentDisplayState(const sp<IBinder> & displayToken) const136 bool DisplayTransactionTest::hasCurrentDisplayState(const sp<IBinder>& displayToken) const {
137     return mFlinger.currentState().displays.indexOfKey(displayToken) >= 0;
138 }
139 
getCurrentDisplayState(const sp<IBinder> & displayToken) const140 const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(
141         const sp<IBinder>& displayToken) const {
142     return mFlinger.currentState().displays.valueFor(displayToken);
143 }
144 
hasDrawingDisplayState(const sp<IBinder> & displayToken) const145 bool DisplayTransactionTest::hasDrawingDisplayState(const sp<IBinder>& displayToken) const {
146     return mFlinger.drawingState().displays.indexOfKey(displayToken) >= 0;
147 }
148 
getDrawingDisplayState(const sp<IBinder> & displayToken) const149 const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(
150         const sp<IBinder>& displayToken) const {
151     return mFlinger.drawingState().displays.valueFor(displayToken);
152 }
153 
154 } // namespace android
155