blob: 21590dfffc04abbd2ec2fa08382d44502a001340 [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
20#include <gmock/gmock.h>
21#include <gtest/gtest.h>
22
23#include <log/log.h>
24
Lloyd Piquee39cad22017-12-20 17:01:29 -080025#include "MockComposer.h"
26#include "MockEventThread.h"
Lloyd Pique5b36f3f2018-01-17 11:57:07 -080027#include "MockGraphicBufferConsumer.h"
28#include "MockGraphicBufferProducer.h"
Lloyd Piquee39cad22017-12-20 17:01:29 -080029#include "MockRenderEngine.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080030#include "TestableSurfaceFlinger.h"
31
32namespace android {
33namespace {
34
Lloyd Piquee39cad22017-12-20 17:01:29 -080035using testing::_;
36using testing::ByMove;
37using testing::DoAll;
38using testing::Mock;
39using testing::Return;
40using testing::SetArgPointee;
41
42using android::hardware::graphics::common::V1_0::Hdr;
43using android::Hwc2::Error;
44using android::Hwc2::IComposer;
45using android::Hwc2::IComposerClient;
46
47constexpr int32_t DEFAULT_REFRESH_RATE = 1666666666;
48constexpr int32_t DEFAULT_DPI = 320;
49
Lloyd Piquef58625d2017-12-19 13:22:33 -080050class DisplayTransactionTest : public testing::Test {
51protected:
52 DisplayTransactionTest();
53 ~DisplayTransactionTest() override;
54
55 void setupComposer(int virtualDisplayCount);
56 void setupPrimaryDisplay(int width, int height);
57
Lloyd Pique5b36f3f2018-01-17 11:57:07 -080058 void expectFramebufferQueuePairCreation(int width, int height);
59
Lloyd Piquef58625d2017-12-19 13:22:33 -080060 TestableSurfaceFlinger mFlinger;
Lloyd Piquee39cad22017-12-20 17:01:29 -080061 mock::EventThread* mEventThread = new mock::EventThread();
62
63 // These mocks are created by the test, but are destroyed by SurfaceFlinger
64 // by virtue of being stored into a std::unique_ptr. However we still need
65 // to keep a reference to them for use in setting up call expectations.
66 RE::mock::RenderEngine* mRenderEngine = new RE::mock::RenderEngine();
67 Hwc2::mock::Composer* mComposer = new Hwc2::mock::Composer();
Lloyd Pique5b36f3f2018-01-17 11:57:07 -080068
69 // These mocks are created only when expected to be created via a factory.
70 sp<mock::GraphicBufferConsumer> mConsumer;
71 sp<mock::GraphicBufferProducer> mProducer;
Lloyd Piquef58625d2017-12-19 13:22:33 -080072};
73
74DisplayTransactionTest::DisplayTransactionTest() {
75 const ::testing::TestInfo* const test_info =
76 ::testing::UnitTest::GetInstance()->current_test_info();
77 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
Lloyd Piquee39cad22017-12-20 17:01:29 -080078
Lloyd Pique5b36f3f2018-01-17 11:57:07 -080079 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
80 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
81 });
82
Lloyd Piquee39cad22017-12-20 17:01:29 -080083 mFlinger.mutableEventThread().reset(mEventThread);
84 mFlinger.setupRenderEngine(std::unique_ptr<RE::RenderEngine>(mRenderEngine));
85
86 setupComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -080087}
88
89DisplayTransactionTest::~DisplayTransactionTest() {
90 const ::testing::TestInfo* const test_info =
91 ::testing::UnitTest::GetInstance()->current_test_info();
92 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
93}
94
Lloyd Piquee39cad22017-12-20 17:01:29 -080095void DisplayTransactionTest::setupComposer(int virtualDisplayCount) {
96 EXPECT_CALL(*mComposer, getCapabilities())
97 .WillOnce(Return(std::vector<IComposer::Capability>()));
98 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
99 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -0800100
Lloyd Piquee39cad22017-12-20 17:01:29 -0800101 Mock::VerifyAndClear(mComposer);
102}
103
104void DisplayTransactionTest::setupPrimaryDisplay(int width, int height) {
105 EXPECT_CALL(*mComposer, getDisplayType(DisplayDevice::DISPLAY_PRIMARY, _))
106 .WillOnce(DoAll(SetArgPointee<1>(IComposerClient::DisplayType::PHYSICAL),
107 Return(Error::NONE)));
108 EXPECT_CALL(*mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
109 EXPECT_CALL(*mComposer, getDisplayConfigs(_, _))
110 .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{0}), Return(Error::NONE)));
111 EXPECT_CALL(*mComposer,
112 getDisplayAttribute(DisplayDevice::DISPLAY_PRIMARY, 0,
113 IComposerClient::Attribute::WIDTH, _))
114 .WillOnce(DoAll(SetArgPointee<3>(width), Return(Error::NONE)));
115 EXPECT_CALL(*mComposer,
116 getDisplayAttribute(DisplayDevice::DISPLAY_PRIMARY, 0,
117 IComposerClient::Attribute::HEIGHT, _))
118 .WillOnce(DoAll(SetArgPointee<3>(height), Return(Error::NONE)));
119 EXPECT_CALL(*mComposer,
120 getDisplayAttribute(DisplayDevice::DISPLAY_PRIMARY, 0,
121 IComposerClient::Attribute::VSYNC_PERIOD, _))
122 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE)));
123 EXPECT_CALL(*mComposer,
124 getDisplayAttribute(DisplayDevice::DISPLAY_PRIMARY, 0,
125 IComposerClient::Attribute::DPI_X, _))
126 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
127 EXPECT_CALL(*mComposer,
128 getDisplayAttribute(DisplayDevice::DISPLAY_PRIMARY, 0,
129 IComposerClient::Attribute::DPI_Y, _))
130 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
131
132 mFlinger.setupPrimaryDisplay();
133
134 Mock::VerifyAndClear(mComposer);
135}
136
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800137void DisplayTransactionTest::expectFramebufferQueuePairCreation(int width, int height) {
138 mConsumer = new mock::GraphicBufferConsumer();
139 mProducer = new mock::GraphicBufferProducer();
140
141 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
142 *outProducer = mProducer;
143 *outConsumer = mConsumer;
144 });
145
146 EXPECT_CALL(*mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR));
147 EXPECT_CALL(*mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR));
148 EXPECT_CALL(*mConsumer,
149 setConsumerUsageBits(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER |
150 GRALLOC_USAGE_HW_FB))
151 .WillRepeatedly(Return(NO_ERROR));
152 EXPECT_CALL(*mConsumer, setDefaultBufferSize(width, height)).WillRepeatedly(Return(NO_ERROR));
153 EXPECT_CALL(*mConsumer, setMaxAcquiredBufferCount(_)).WillRepeatedly(Return(NO_ERROR));
154
155 EXPECT_CALL(*mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return());
156}
157
Lloyd Piquee39cad22017-12-20 17:01:29 -0800158TEST_F(DisplayTransactionTest, processDisplayChangesLockedProcessesPrimaryDisplayConnected) {
159 using android::hardware::graphics::common::V1_0::ColorMode;
160
161 setupPrimaryDisplay(1920, 1080);
162
163 sp<BBinder> token = new BBinder();
164 mFlinger.mutableCurrentState().displays.add(token, {DisplayDevice::DISPLAY_PRIMARY, true});
165
166 EXPECT_CALL(*mComposer, getActiveConfig(DisplayDevice::DISPLAY_PRIMARY, _))
167 .WillOnce(DoAll(SetArgPointee<1>(0), Return(Error::NONE)));
Lloyd Pique926a5fc2018-03-01 13:52:37 -0800168 EXPECT_CALL(*mComposer, getColorModes(DisplayDevice::DISPLAY_PRIMARY, _)).Times(0);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800169 EXPECT_CALL(*mComposer, getHdrCapabilities(DisplayDevice::DISPLAY_PRIMARY, _, _, _, _))
170 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
171
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800172 expectFramebufferQueuePairCreation(1920, 1080);
173
Lloyd Piquee39cad22017-12-20 17:01:29 -0800174 auto reSurface = new RE::mock::Surface();
175 EXPECT_CALL(*mRenderEngine, createSurface())
176 .WillOnce(Return(ByMove(std::unique_ptr<RE::Surface>(reSurface))));
177 EXPECT_CALL(*reSurface, setAsync(false)).Times(1);
178 EXPECT_CALL(*reSurface, setCritical(true)).Times(1);
179 EXPECT_CALL(*reSurface, setNativeWindow(_)).Times(1);
180 EXPECT_CALL(*reSurface, queryWidth()).WillOnce(Return(1920));
181 EXPECT_CALL(*reSurface, queryHeight()).WillOnce(Return(1080));
182
183 EXPECT_CALL(*mEventThread, onHotplugReceived(DisplayDevice::DISPLAY_PRIMARY, true)).Times(1);
184
185 mFlinger.processDisplayChangesLocked();
186
187 ASSERT_TRUE(mFlinger.mutableDisplays().indexOfKey(token) >= 0);
188
189 const auto& device = mFlinger.mutableDisplays().valueFor(token);
190 ASSERT_TRUE(device.get());
191 EXPECT_TRUE(device->isSecure());
192 EXPECT_TRUE(device->isPrimary());
193
194 ssize_t i = mFlinger.mutableDrawingState().displays.indexOfKey(token);
195 ASSERT_GE(0, i);
196 const auto& draw = mFlinger.mutableDrawingState().displays[i];
197 EXPECT_EQ(DisplayDevice::DISPLAY_PRIMARY, draw.type);
198
199 EXPECT_CALL(*mComposer, setVsyncEnabled(0, IComposerClient::Vsync::DISABLE))
200 .WillOnce(Return(Error::NONE));
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800201
202 EXPECT_CALL(*mConsumer, consumerDisconnect()).Times(1);
Lloyd Piquef58625d2017-12-19 13:22:33 -0800203}
204
205} // namespace
206} // namespace android