blob: 9b10c94bcd6e7fdd145135a5d72867113fd1131c [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
Dominik Laskowskiaee9a622023-02-11 14:24:19 -050032DisplayTransactionTest::DisplayTransactionTest(bool withMockScheduler) {
Lloyd Piquef58625d2017-12-19 13:22:33 -080033 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());
Lloyd Piquee39cad22017-12-20 17:01:29 -080036
Lloyd Pique30db6402023-06-26 18:56:51 +000037 mFlinger.mutableSupportsWideColor() = false;
Lloyd Pique6a3b4462019-03-07 20:58:12 -080038 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080039
Lloyd Pique5b36f3f2018-01-17 11:57:07 -080040 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
41 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
42 });
43
Lloyd Pique1fa4d462018-01-22 18:03:16 -080044 mFlinger.setCreateNativeWindowSurface([](auto) {
45 ADD_FAILURE() << "Unexpected request to create a native window surface.";
46 return nullptr;
47 });
48
Dominik Laskowskiaee9a622023-02-11 14:24:19 -050049 if (withMockScheduler) {
50 injectMockScheduler(PhysicalDisplayId::fromPort(0));
51 }
52
Peiyong Lin833074a2018-08-28 11:53:54 -070053 mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
Lloyd Piquee39cad22017-12-20 17:01:29 -080054
Lloyd Piquec11e0d32018-01-22 18:44:59 -080055 injectMockComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -080056}
57
58DisplayTransactionTest::~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());
Alec Mouricdf16792021-12-10 13:16:06 -080062 mFlinger.resetScheduler(nullptr);
Lloyd Piquef58625d2017-12-19 13:22:33 -080063}
64
Dominik Laskowskiaee9a622023-02-11 14:24:19 -050065void DisplayTransactionTest::injectMockScheduler(PhysicalDisplayId displayId) {
66 LOG_ALWAYS_FATAL_IF(mFlinger.scheduler());
Leon Scroggins III67388622023-02-06 20:36:20 -050067 mFlinger.setupScheduler(std::make_unique<mock::VsyncController>(),
68 std::make_shared<mock::VSyncTracker>(),
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070069 std::unique_ptr<EventThread>(mEventThread),
Dominik Laskowski83bd7712022-01-07 14:30:53 -080070 std::unique_ptr<EventThread>(mSFEventThread),
Dominik Laskowskiaee9a622023-02-11 14:24:19 -050071 TestableSurfaceFlinger::DefaultDisplayMode{displayId},
ramindaniae645822024-01-11 10:57:29 -080072 TestableSurfaceFlinger::SchedulerCallbackImpl::kMock);
Ana Krulecafb45842019-02-13 13:33:03 -080073}
74
Lloyd Piquec11e0d32018-01-22 18:44:59 -080075void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
Dominik Laskowski3dce4f42021-03-08 20:48:28 -080076 if (mComposer) {
77 // If reinjecting, disable first to prevent the enable below from being a no-op.
78 mFlinger.enableHalVirtualDisplays(false);
79 }
80
Lloyd Piquec11e0d32018-01-22 18:44:59 -080081 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -080082 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -080083
Dominik Laskowski3dce4f42021-03-08 20:48:28 -080084 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
85 mFlinger.enableHalVirtualDisplays(true);
86
Lloyd Piquee39cad22017-12-20 17:01:29 -080087 Mock::VerifyAndClear(mComposer);
88}
89
Lloyd Piquec11e0d32018-01-22 18:44:59 -080090void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -080091 // This setup is only expected once per test.
92 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
93
Ady Abrahamd11bade2022-08-01 16:18:03 -070094 mConsumer = sp<mock::GraphicBufferConsumer>::make();
95 mProducer = sp<mock::GraphicBufferProducer>::make();
Lloyd Pique5b36f3f2018-01-17 11:57:07 -080096
97 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
98 *outProducer = mProducer;
99 *outConsumer = mConsumer;
100 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800101}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800102
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800103void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800104 // This setup is only expected once per test.
105 ASSERT_TRUE(mNativeWindowSurface == nullptr);
106
Lloyd Pique22098362018-09-13 11:46:49 -0700107 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800108
Lloyd Pique22098362018-09-13 11:46:49 -0700109 mFlinger.setCreateNativeWindowSurface([this](auto) {
110 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
111 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800112}
113
Dominik Laskowskieb627312022-04-07 09:13:16 -0700114bool DisplayTransactionTest::hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId) const {
Dominik Laskowskibab51282022-08-12 09:28:55 -0700115 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;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800121}
122
Dominik Laskowskieb627312022-04-07 09:13:16 -0700123bool DisplayTransactionTest::hasTransactionFlagSet(int32_t flag) const {
124 return mFlinger.transactionFlags() & flag;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800125}
126
Dominik Laskowskieb627312022-04-07 09:13:16 -0700127bool DisplayTransactionTest::hasDisplayDevice(const sp<IBinder>& displayToken) const {
128 return mFlinger.displays().contains(displayToken);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800129}
130
Dominik Laskowskieb627312022-04-07 09:13:16 -0700131const DisplayDevice& DisplayTransactionTest::getDisplayDevice(
132 const sp<IBinder>& displayToken) const {
133 return *mFlinger.displays().get(displayToken)->get();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800134}
135
Dominik Laskowskieb627312022-04-07 09:13:16 -0700136bool DisplayTransactionTest::hasCurrentDisplayState(const sp<IBinder>& displayToken) const {
137 return mFlinger.currentState().displays.indexOfKey(displayToken) >= 0;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800138}
139
Dominik Laskowskieb627312022-04-07 09:13:16 -0700140const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(
141 const sp<IBinder>& displayToken) const {
142 return mFlinger.currentState().displays.valueFor(displayToken);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800143}
144
Dominik Laskowskieb627312022-04-07 09:13:16 -0700145bool DisplayTransactionTest::hasDrawingDisplayState(const sp<IBinder>& displayToken) const {
146 return mFlinger.drawingState().displays.indexOfKey(displayToken) >= 0;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800147}
148
Dominik Laskowskieb627312022-04-07 09:13:16 -0700149const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(
150 const sp<IBinder>& displayToken) const {
151 return mFlinger.drawingState().displays.valueFor(displayToken);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800152}
153
Lloyd Piquef58625d2017-12-19 13:22:33 -0800154} // namespace android