blob: 13796650c8191a71b3ed2c8ba1a90b09361c4801 [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},
ramindanid4354a92023-10-02 15:11:09 -070083 TestableSurfaceFlinger::SchedulerCallbackImpl::kMock,
84 TestableSurfaceFlinger::VsyncTrackerCallbackImpl::kMock);
Ana Krulecafb45842019-02-13 13:33:03 -080085}
86
Lloyd Piquec11e0d32018-01-22 18:44:59 -080087void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
Dominik Laskowski3dce4f42021-03-08 20:48:28 -080088 if (mComposer) {
89 // If reinjecting, disable first to prevent the enable below from being a no-op.
90 mFlinger.enableHalVirtualDisplays(false);
91 }
92
Lloyd Piquec11e0d32018-01-22 18:44:59 -080093 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -080094 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -080095
Dominik Laskowski3dce4f42021-03-08 20:48:28 -080096 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
97 mFlinger.enableHalVirtualDisplays(true);
98
Lloyd Piquee39cad22017-12-20 17:01:29 -080099 Mock::VerifyAndClear(mComposer);
100}
101
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800102void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800103 // This setup is only expected once per test.
104 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
105
Ady Abrahamd11bade2022-08-01 16:18:03 -0700106 mConsumer = sp<mock::GraphicBufferConsumer>::make();
107 mProducer = sp<mock::GraphicBufferProducer>::make();
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800108
109 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
110 *outProducer = mProducer;
111 *outConsumer = mConsumer;
112 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800113}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800114
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800115void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800116 // This setup is only expected once per test.
117 ASSERT_TRUE(mNativeWindowSurface == nullptr);
118
Lloyd Pique22098362018-09-13 11:46:49 -0700119 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800120
Lloyd Pique22098362018-09-13 11:46:49 -0700121 mFlinger.setCreateNativeWindowSurface([this](auto) {
122 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
123 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800124}
125
Dominik Laskowskieb627312022-04-07 09:13:16 -0700126bool DisplayTransactionTest::hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId) const {
Dominik Laskowskibab51282022-08-12 09:28:55 -0700127 const auto& map = mFlinger.hwcPhysicalDisplayIdMap();
128
129 const auto it = map.find(hwcDisplayId);
130 if (it == map.end()) return false;
131
132 return mFlinger.hwcDisplayData().count(it->second) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800133}
134
Dominik Laskowskieb627312022-04-07 09:13:16 -0700135bool DisplayTransactionTest::hasTransactionFlagSet(int32_t flag) const {
136 return mFlinger.transactionFlags() & flag;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800137}
138
Dominik Laskowskieb627312022-04-07 09:13:16 -0700139bool DisplayTransactionTest::hasDisplayDevice(const sp<IBinder>& displayToken) const {
140 return mFlinger.displays().contains(displayToken);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800141}
142
Dominik Laskowskieb627312022-04-07 09:13:16 -0700143const DisplayDevice& DisplayTransactionTest::getDisplayDevice(
144 const sp<IBinder>& displayToken) const {
145 return *mFlinger.displays().get(displayToken)->get();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800146}
147
Dominik Laskowskieb627312022-04-07 09:13:16 -0700148bool DisplayTransactionTest::hasCurrentDisplayState(const sp<IBinder>& displayToken) const {
149 return mFlinger.currentState().displays.indexOfKey(displayToken) >= 0;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800150}
151
Dominik Laskowskieb627312022-04-07 09:13:16 -0700152const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(
153 const sp<IBinder>& displayToken) const {
154 return mFlinger.currentState().displays.valueFor(displayToken);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800155}
156
Dominik Laskowskieb627312022-04-07 09:13:16 -0700157bool DisplayTransactionTest::hasDrawingDisplayState(const sp<IBinder>& displayToken) const {
158 return mFlinger.drawingState().displays.indexOfKey(displayToken) >= 0;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800159}
160
Dominik Laskowskieb627312022-04-07 09:13:16 -0700161const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(
162 const sp<IBinder>& displayToken) const {
163 return mFlinger.drawingState().displays.valueFor(displayToken);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800164}
165
Lloyd Piquef58625d2017-12-19 13:22:33 -0800166} // namespace android