blob: 214b02888fa9a8af9410474ca211dd0370803de4 [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
Lloyd Piquec11e0d32018-01-22 18:44:59 -080032using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector;
Lloyd Piquef58625d2017-12-19 13:22:33 -080033
34DisplayTransactionTest::DisplayTransactionTest() {
35 const ::testing::TestInfo* const test_info =
36 ::testing::UnitTest::GetInstance()->current_test_info();
37 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
Lloyd Piquee39cad22017-12-20 17:01:29 -080038
Dominik Laskowski788945b2022-08-30 12:10:56 -070039 mFlinger.mutableSupportsWideColor() = false;
Lloyd Pique6a3b4462019-03-07 20:58:12 -080040 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080041
Lloyd Pique5b36f3f2018-01-17 11:57:07 -080042 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
43 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
44 });
45
Lloyd Pique1fa4d462018-01-22 18:03:16 -080046 mFlinger.setCreateNativeWindowSurface([](auto) {
47 ADD_FAILURE() << "Unexpected request to create a native window surface.";
48 return nullptr;
49 });
50
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070051 injectMockScheduler();
Peiyong Lin833074a2018-08-28 11:53:54 -070052 mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
Lloyd Piquee39cad22017-12-20 17:01:29 -080053
Lloyd Piquec11e0d32018-01-22 18:44:59 -080054 injectMockComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -080055}
56
57DisplayTransactionTest::~DisplayTransactionTest() {
58 const ::testing::TestInfo* const test_info =
59 ::testing::UnitTest::GetInstance()->current_test_info();
60 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Alec Mouricdf16792021-12-10 13:16:06 -080061 mFlinger.resetScheduler(nullptr);
Lloyd Piquef58625d2017-12-19 13:22:33 -080062}
63
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070064void DisplayTransactionTest::injectMockScheduler() {
Ana Krulecafb45842019-02-13 13:33:03 -080065 EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_));
Dominik Laskowski98041832019-08-01 18:35:59 -070066 EXPECT_CALL(*mEventThread, createEventConnection(_, _))
Ady Abrahamd11bade2022-08-01 16:18:03 -070067 .WillOnce(Return(sp<EventThreadConnection>::make(mEventThread,
68 mock::EventThread::kCallingUid,
69 ResyncCallback())));
Dominik Laskowski98041832019-08-01 18:35:59 -070070
Ana Krulecafb45842019-02-13 13:33:03 -080071 EXPECT_CALL(*mSFEventThread, registerDisplayEventConnection(_));
Dominik Laskowski98041832019-08-01 18:35:59 -070072 EXPECT_CALL(*mSFEventThread, createEventConnection(_, _))
Ady Abrahamd11bade2022-08-01 16:18:03 -070073 .WillOnce(Return(sp<EventThreadConnection>::make(mSFEventThread,
74 mock::EventThread::kCallingUid,
75 ResyncCallback())));
Ana Krulecafb45842019-02-13 13:33:03 -080076
Leon Scroggins III31d41412022-11-18 16:42:53 -050077 mFlinger.setupScheduler(std::make_unique<mock::VsyncController>(),
78 std::make_shared<mock::VSyncTracker>(),
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -070079 std::unique_ptr<EventThread>(mEventThread),
Dominik Laskowski83bd7712022-01-07 14:30:53 -080080 std::unique_ptr<EventThread>(mSFEventThread),
81 TestableSurfaceFlinger::SchedulerCallbackImpl::kMock);
Ana Krulecafb45842019-02-13 13:33:03 -080082}
83
Lloyd Piquec11e0d32018-01-22 18:44:59 -080084void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
Dominik Laskowski3dce4f42021-03-08 20:48:28 -080085 if (mComposer) {
86 // If reinjecting, disable first to prevent the enable below from being a no-op.
87 mFlinger.enableHalVirtualDisplays(false);
88 }
89
Lloyd Piquec11e0d32018-01-22 18:44:59 -080090 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -080091 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -080092
Dominik Laskowski3dce4f42021-03-08 20:48:28 -080093 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
94 mFlinger.enableHalVirtualDisplays(true);
95
Lloyd Piquee39cad22017-12-20 17:01:29 -080096 Mock::VerifyAndClear(mComposer);
97}
98
Lloyd Piquec11e0d32018-01-22 18:44:59 -080099void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800100 // This setup is only expected once per test.
101 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
102
Ady Abrahamd11bade2022-08-01 16:18:03 -0700103 mConsumer = sp<mock::GraphicBufferConsumer>::make();
104 mProducer = sp<mock::GraphicBufferProducer>::make();
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800105
106 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
107 *outProducer = mProducer;
108 *outConsumer = mConsumer;
109 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800110}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800111
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800112void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800113 // This setup is only expected once per test.
114 ASSERT_TRUE(mNativeWindowSurface == nullptr);
115
Lloyd Pique22098362018-09-13 11:46:49 -0700116 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800117
Lloyd Pique22098362018-09-13 11:46:49 -0700118 mFlinger.setCreateNativeWindowSurface([this](auto) {
119 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
120 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800121}
122
Dominik Laskowskieb627312022-04-07 09:13:16 -0700123bool DisplayTransactionTest::hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId) const {
Dominik Laskowskibab51282022-08-12 09:28:55 -0700124 const auto& map = mFlinger.hwcPhysicalDisplayIdMap();
125
126 const auto it = map.find(hwcDisplayId);
127 if (it == map.end()) return false;
128
129 return mFlinger.hwcDisplayData().count(it->second) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800130}
131
Dominik Laskowskieb627312022-04-07 09:13:16 -0700132bool DisplayTransactionTest::hasTransactionFlagSet(int32_t flag) const {
133 return mFlinger.transactionFlags() & flag;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800134}
135
Dominik Laskowskieb627312022-04-07 09:13:16 -0700136bool DisplayTransactionTest::hasDisplayDevice(const sp<IBinder>& displayToken) const {
137 return mFlinger.displays().contains(displayToken);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800138}
139
Dominik Laskowskieb627312022-04-07 09:13:16 -0700140const DisplayDevice& DisplayTransactionTest::getDisplayDevice(
141 const sp<IBinder>& displayToken) const {
142 return *mFlinger.displays().get(displayToken)->get();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800143}
144
Dominik Laskowskieb627312022-04-07 09:13:16 -0700145bool DisplayTransactionTest::hasCurrentDisplayState(const sp<IBinder>& displayToken) const {
146 return mFlinger.currentState().displays.indexOfKey(displayToken) >= 0;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800147}
148
Dominik Laskowskieb627312022-04-07 09:13:16 -0700149const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(
150 const sp<IBinder>& displayToken) const {
151 return mFlinger.currentState().displays.valueFor(displayToken);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800152}
153
Dominik Laskowskieb627312022-04-07 09:13:16 -0700154bool DisplayTransactionTest::hasDrawingDisplayState(const sp<IBinder>& displayToken) const {
155 return mFlinger.drawingState().displays.indexOfKey(displayToken) >= 0;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800156}
157
Dominik Laskowskieb627312022-04-07 09:13:16 -0700158const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(
159 const sp<IBinder>& displayToken) const {
160 return mFlinger.drawingState().displays.valueFor(displayToken);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800161}
162
Lloyd Piquef58625d2017-12-19 13:22:33 -0800163} // namespace android