blob: e32cf8863b850a86d824e912981f9af3c0ab8026 [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());
67
Ana Krulecafb45842019-02-13 13:33:03 -080068 EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_));
Dominik Laskowski98041832019-08-01 18:35:59 -070069 EXPECT_CALL(*mEventThread, createEventConnection(_, _))
Ady Abrahamd11bade2022-08-01 16:18:03 -070070 .WillOnce(Return(sp<EventThreadConnection>::make(mEventThread,
71 mock::EventThread::kCallingUid,
72 ResyncCallback())));
Dominik Laskowski98041832019-08-01 18:35:59 -070073
Ana Krulecafb45842019-02-13 13:33:03 -080074 EXPECT_CALL(*mSFEventThread, registerDisplayEventConnection(_));
Dominik Laskowski98041832019-08-01 18:35:59 -070075 EXPECT_CALL(*mSFEventThread, createEventConnection(_, _))
Ady Abrahamd11bade2022-08-01 16:18:03 -070076 .WillOnce(Return(sp<EventThreadConnection>::make(mSFEventThread,
77 mock::EventThread::kCallingUid,
78 ResyncCallback())));
Ana Krulecafb45842019-02-13 13:33:03 -080079
Leon Scroggins III67388622023-02-06 20:36:20 -050080 mFlinger.setupScheduler(std::make_unique<mock::VsyncController>(),
81 std::make_shared<mock::VSyncTracker>(),
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070082 std::unique_ptr<EventThread>(mEventThread),
Dominik Laskowski83bd7712022-01-07 14:30:53 -080083 std::unique_ptr<EventThread>(mSFEventThread),
Dominik Laskowskiaee9a622023-02-11 14:24:19 -050084 TestableSurfaceFlinger::DefaultDisplayMode{displayId},
Dominik Laskowski83bd7712022-01-07 14:30:53 -080085 TestableSurfaceFlinger::SchedulerCallbackImpl::kMock);
Ana Krulecafb45842019-02-13 13:33:03 -080086}
87
Lloyd Piquec11e0d32018-01-22 18:44:59 -080088void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
Dominik Laskowski3dce4f42021-03-08 20:48:28 -080089 if (mComposer) {
90 // If reinjecting, disable first to prevent the enable below from being a no-op.
91 mFlinger.enableHalVirtualDisplays(false);
92 }
93
Lloyd Piquec11e0d32018-01-22 18:44:59 -080094 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -080095 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -080096
Dominik Laskowski3dce4f42021-03-08 20:48:28 -080097 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
98 mFlinger.enableHalVirtualDisplays(true);
99
Lloyd Piquee39cad22017-12-20 17:01:29 -0800100 Mock::VerifyAndClear(mComposer);
101}
102
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800103void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800104 // This setup is only expected once per test.
105 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
106
Ady Abrahamd11bade2022-08-01 16:18:03 -0700107 mConsumer = sp<mock::GraphicBufferConsumer>::make();
108 mProducer = sp<mock::GraphicBufferProducer>::make();
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800109
110 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
111 *outProducer = mProducer;
112 *outConsumer = mConsumer;
113 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800114}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800115
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800116void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800117 // This setup is only expected once per test.
118 ASSERT_TRUE(mNativeWindowSurface == nullptr);
119
Lloyd Pique22098362018-09-13 11:46:49 -0700120 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800121
Lloyd Pique22098362018-09-13 11:46:49 -0700122 mFlinger.setCreateNativeWindowSurface([this](auto) {
123 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
124 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800125}
126
Dominik Laskowskieb627312022-04-07 09:13:16 -0700127bool DisplayTransactionTest::hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId) const {
Dominik Laskowskibab51282022-08-12 09:28:55 -0700128 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;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800134}
135
Dominik Laskowskieb627312022-04-07 09:13:16 -0700136bool DisplayTransactionTest::hasTransactionFlagSet(int32_t flag) const {
137 return mFlinger.transactionFlags() & flag;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800138}
139
Dominik Laskowskieb627312022-04-07 09:13:16 -0700140bool DisplayTransactionTest::hasDisplayDevice(const sp<IBinder>& displayToken) const {
141 return mFlinger.displays().contains(displayToken);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800142}
143
Dominik Laskowskieb627312022-04-07 09:13:16 -0700144const DisplayDevice& DisplayTransactionTest::getDisplayDevice(
145 const sp<IBinder>& displayToken) const {
146 return *mFlinger.displays().get(displayToken)->get();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800147}
148
Dominik Laskowskieb627312022-04-07 09:13:16 -0700149bool DisplayTransactionTest::hasCurrentDisplayState(const sp<IBinder>& displayToken) const {
150 return mFlinger.currentState().displays.indexOfKey(displayToken) >= 0;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800151}
152
Dominik Laskowskieb627312022-04-07 09:13:16 -0700153const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(
154 const sp<IBinder>& displayToken) const {
155 return mFlinger.currentState().displays.valueFor(displayToken);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800156}
157
Dominik Laskowskieb627312022-04-07 09:13:16 -0700158bool DisplayTransactionTest::hasDrawingDisplayState(const sp<IBinder>& displayToken) const {
159 return mFlinger.drawingState().displays.indexOfKey(displayToken) >= 0;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800160}
161
Dominik Laskowskieb627312022-04-07 09:13:16 -0700162const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(
163 const sp<IBinder>& displayToken) const {
164 return mFlinger.drawingState().displays.valueFor(displayToken);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800165}
166
Lloyd Piquef58625d2017-12-19 13:22:33 -0800167} // namespace android