blob: fa31643df11919e87474e86039799229f6fe6e3a [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 Abrahamf2851612023-09-25 17:19:00 -070070 .WillOnce(Return(
71 sp<EventThreadConnection>::make(mEventThread, mock::EventThread::kCallingUid)));
Dominik Laskowski98041832019-08-01 18:35:59 -070072
Ana Krulecafb45842019-02-13 13:33:03 -080073 EXPECT_CALL(*mSFEventThread, registerDisplayEventConnection(_));
Dominik Laskowski98041832019-08-01 18:35:59 -070074 EXPECT_CALL(*mSFEventThread, createEventConnection(_, _))
Ady Abrahamd11bade2022-08-01 16:18:03 -070075 .WillOnce(Return(sp<EventThreadConnection>::make(mSFEventThread,
Ady Abrahamf2851612023-09-25 17:19:00 -070076 mock::EventThread::kCallingUid)));
Ana Krulecafb45842019-02-13 13:33:03 -080077
Leon Scroggins III67388622023-02-06 20:36:20 -050078 mFlinger.setupScheduler(std::make_unique<mock::VsyncController>(),
79 std::make_shared<mock::VSyncTracker>(),
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070080 std::unique_ptr<EventThread>(mEventThread),
Dominik Laskowski83bd7712022-01-07 14:30:53 -080081 std::unique_ptr<EventThread>(mSFEventThread),
Dominik Laskowskiaee9a622023-02-11 14:24:19 -050082 TestableSurfaceFlinger::DefaultDisplayMode{displayId},
ramindaniae645822024-01-11 10:57:29 -080083 TestableSurfaceFlinger::SchedulerCallbackImpl::kMock);
Ana Krulecafb45842019-02-13 13:33:03 -080084}
85
Lloyd Piquec11e0d32018-01-22 18:44:59 -080086void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
Dominik Laskowski3dce4f42021-03-08 20:48:28 -080087 if (mComposer) {
88 // If reinjecting, disable first to prevent the enable below from being a no-op.
89 mFlinger.enableHalVirtualDisplays(false);
90 }
91
Lloyd Piquec11e0d32018-01-22 18:44:59 -080092 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -080093 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -080094
Dominik Laskowski3dce4f42021-03-08 20:48:28 -080095 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
96 mFlinger.enableHalVirtualDisplays(true);
97
Lloyd Piquee39cad22017-12-20 17:01:29 -080098 Mock::VerifyAndClear(mComposer);
99}
100
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800101void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800102 // This setup is only expected once per test.
103 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
104
Ady Abrahamd11bade2022-08-01 16:18:03 -0700105 mConsumer = sp<mock::GraphicBufferConsumer>::make();
106 mProducer = sp<mock::GraphicBufferProducer>::make();
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800107
108 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
109 *outProducer = mProducer;
110 *outConsumer = mConsumer;
111 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800112}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800113
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800114void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800115 // This setup is only expected once per test.
116 ASSERT_TRUE(mNativeWindowSurface == nullptr);
117
Lloyd Pique22098362018-09-13 11:46:49 -0700118 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800119
Lloyd Pique22098362018-09-13 11:46:49 -0700120 mFlinger.setCreateNativeWindowSurface([this](auto) {
121 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
122 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800123}
124
Dominik Laskowskieb627312022-04-07 09:13:16 -0700125bool DisplayTransactionTest::hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId) const {
Dominik Laskowskibab51282022-08-12 09:28:55 -0700126 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;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800132}
133
Dominik Laskowskieb627312022-04-07 09:13:16 -0700134bool DisplayTransactionTest::hasTransactionFlagSet(int32_t flag) const {
135 return mFlinger.transactionFlags() & flag;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800136}
137
Dominik Laskowskieb627312022-04-07 09:13:16 -0700138bool DisplayTransactionTest::hasDisplayDevice(const sp<IBinder>& displayToken) const {
139 return mFlinger.displays().contains(displayToken);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800140}
141
Dominik Laskowskieb627312022-04-07 09:13:16 -0700142const DisplayDevice& DisplayTransactionTest::getDisplayDevice(
143 const sp<IBinder>& displayToken) const {
144 return *mFlinger.displays().get(displayToken)->get();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800145}
146
Dominik Laskowskieb627312022-04-07 09:13:16 -0700147bool DisplayTransactionTest::hasCurrentDisplayState(const sp<IBinder>& displayToken) const {
148 return mFlinger.currentState().displays.indexOfKey(displayToken) >= 0;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800149}
150
Dominik Laskowskieb627312022-04-07 09:13:16 -0700151const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(
152 const sp<IBinder>& displayToken) const {
153 return mFlinger.currentState().displays.valueFor(displayToken);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800154}
155
Dominik Laskowskieb627312022-04-07 09:13:16 -0700156bool DisplayTransactionTest::hasDrawingDisplayState(const sp<IBinder>& displayToken) const {
157 return mFlinger.drawingState().displays.indexOfKey(displayToken) >= 0;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800158}
159
Dominik Laskowskieb627312022-04-07 09:13:16 -0700160const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(
161 const sp<IBinder>& displayToken) const {
162 return mFlinger.drawingState().displays.valueFor(displayToken);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800163}
164
Lloyd Piquef58625d2017-12-19 13:22:33 -0800165} // namespace android