blob: 48b9e927c0d6486a2c47f8ffbf453277382ad763 [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
Lloyd Piquebc792092018-01-17 11:52:30 -080047using HWC2Display = TestableSurfaceFlinger::HWC2Display;
48
Lloyd Piquee39cad22017-12-20 17:01:29 -080049constexpr int32_t DEFAULT_REFRESH_RATE = 1666666666;
50constexpr int32_t DEFAULT_DPI = 320;
51
Lloyd Piquef58625d2017-12-19 13:22:33 -080052class DisplayTransactionTest : public testing::Test {
53protected:
54 DisplayTransactionTest();
55 ~DisplayTransactionTest() override;
56
57 void setupComposer(int virtualDisplayCount);
58 void setupPrimaryDisplay(int width, int height);
59
Lloyd Pique5b36f3f2018-01-17 11:57:07 -080060 void expectFramebufferQueuePairCreation(int width, int height);
61
Lloyd Piquebc792092018-01-17 11:52:30 -080062 std::unordered_set<HWC2::Capability> mCapabilities;
63
Lloyd Piquef58625d2017-12-19 13:22:33 -080064 TestableSurfaceFlinger mFlinger;
Lloyd Piquee39cad22017-12-20 17:01:29 -080065 mock::EventThread* mEventThread = new mock::EventThread();
66
67 // These mocks are created by the test, but are destroyed by SurfaceFlinger
68 // by virtue of being stored into a std::unique_ptr. However we still need
69 // to keep a reference to them for use in setting up call expectations.
70 RE::mock::RenderEngine* mRenderEngine = new RE::mock::RenderEngine();
71 Hwc2::mock::Composer* mComposer = new Hwc2::mock::Composer();
Lloyd Pique5b36f3f2018-01-17 11:57:07 -080072
73 // These mocks are created only when expected to be created via a factory.
74 sp<mock::GraphicBufferConsumer> mConsumer;
75 sp<mock::GraphicBufferProducer> mProducer;
Lloyd Piquebc792092018-01-17 11:52:30 -080076 std::unique_ptr<HWC2Display> mDisplay;
Lloyd Piquef58625d2017-12-19 13:22:33 -080077};
78
79DisplayTransactionTest::DisplayTransactionTest() {
80 const ::testing::TestInfo* const test_info =
81 ::testing::UnitTest::GetInstance()->current_test_info();
82 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
Lloyd Piquee39cad22017-12-20 17:01:29 -080083
Lloyd Pique5b36f3f2018-01-17 11:57:07 -080084 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
85 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
86 });
87
Lloyd Piquee39cad22017-12-20 17:01:29 -080088 mFlinger.mutableEventThread().reset(mEventThread);
89 mFlinger.setupRenderEngine(std::unique_ptr<RE::RenderEngine>(mRenderEngine));
90
91 setupComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -080092}
93
94DisplayTransactionTest::~DisplayTransactionTest() {
95 const ::testing::TestInfo* const test_info =
96 ::testing::UnitTest::GetInstance()->current_test_info();
97 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
98}
99
Lloyd Piquee39cad22017-12-20 17:01:29 -0800100void DisplayTransactionTest::setupComposer(int virtualDisplayCount) {
101 EXPECT_CALL(*mComposer, getCapabilities())
102 .WillOnce(Return(std::vector<IComposer::Capability>()));
103 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
104 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -0800105
Lloyd Piquee39cad22017-12-20 17:01:29 -0800106 Mock::VerifyAndClear(mComposer);
107}
108
109void DisplayTransactionTest::setupPrimaryDisplay(int width, int height) {
Lloyd Piquebc792092018-01-17 11:52:30 -0800110 mDisplay = std::make_unique<HWC2Display>(*mComposer, mCapabilities, 0,
111 HWC2::DisplayType::Physical);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800112
Lloyd Piquebc792092018-01-17 11:52:30 -0800113 mDisplay->mutableIsConnected() = true;
114 mDisplay->mutableConfigs().emplace(0,
115 HWC2::Display::Config::Builder(*mDisplay, 0)
116 .setWidth(width)
117 .setHeight(height)
118 .setVsyncPeriod(DEFAULT_REFRESH_RATE)
119 .setDpiX(DEFAULT_DPI)
120 .setDpiY(DEFAULT_DPI)
121 .build());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800122
Lloyd Piquebc792092018-01-17 11:52:30 -0800123 mFlinger.mutableHwcDisplayData()[DisplayDevice::DISPLAY_PRIMARY].reset();
124 mFlinger.mutableHwcDisplayData()[DisplayDevice::DISPLAY_PRIMARY].hwcDisplay = mDisplay.get();
125 mFlinger.mutableHwcDisplaySlots().emplace(0, DisplayDevice::DISPLAY_PRIMARY);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800126}
127
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800128void DisplayTransactionTest::expectFramebufferQueuePairCreation(int width, int height) {
129 mConsumer = new mock::GraphicBufferConsumer();
130 mProducer = new mock::GraphicBufferProducer();
131
132 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
133 *outProducer = mProducer;
134 *outConsumer = mConsumer;
135 });
136
137 EXPECT_CALL(*mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR));
138 EXPECT_CALL(*mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR));
139 EXPECT_CALL(*mConsumer,
140 setConsumerUsageBits(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER |
141 GRALLOC_USAGE_HW_FB))
142 .WillRepeatedly(Return(NO_ERROR));
143 EXPECT_CALL(*mConsumer, setDefaultBufferSize(width, height)).WillRepeatedly(Return(NO_ERROR));
144 EXPECT_CALL(*mConsumer, setMaxAcquiredBufferCount(_)).WillRepeatedly(Return(NO_ERROR));
145
146 EXPECT_CALL(*mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return());
147}
148
Lloyd Piquee39cad22017-12-20 17:01:29 -0800149TEST_F(DisplayTransactionTest, processDisplayChangesLockedProcessesPrimaryDisplayConnected) {
150 using android::hardware::graphics::common::V1_0::ColorMode;
151
152 setupPrimaryDisplay(1920, 1080);
153
154 sp<BBinder> token = new BBinder();
155 mFlinger.mutableCurrentState().displays.add(token, {DisplayDevice::DISPLAY_PRIMARY, true});
156
157 EXPECT_CALL(*mComposer, getActiveConfig(DisplayDevice::DISPLAY_PRIMARY, _))
158 .WillOnce(DoAll(SetArgPointee<1>(0), Return(Error::NONE)));
Lloyd Pique926a5fc2018-03-01 13:52:37 -0800159 EXPECT_CALL(*mComposer, getColorModes(DisplayDevice::DISPLAY_PRIMARY, _)).Times(0);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800160 EXPECT_CALL(*mComposer, getHdrCapabilities(DisplayDevice::DISPLAY_PRIMARY, _, _, _, _))
161 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
162
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800163 expectFramebufferQueuePairCreation(1920, 1080);
164
Lloyd Piquee39cad22017-12-20 17:01:29 -0800165 auto reSurface = new RE::mock::Surface();
166 EXPECT_CALL(*mRenderEngine, createSurface())
167 .WillOnce(Return(ByMove(std::unique_ptr<RE::Surface>(reSurface))));
168 EXPECT_CALL(*reSurface, setAsync(false)).Times(1);
169 EXPECT_CALL(*reSurface, setCritical(true)).Times(1);
170 EXPECT_CALL(*reSurface, setNativeWindow(_)).Times(1);
171 EXPECT_CALL(*reSurface, queryWidth()).WillOnce(Return(1920));
172 EXPECT_CALL(*reSurface, queryHeight()).WillOnce(Return(1080));
173
174 EXPECT_CALL(*mEventThread, onHotplugReceived(DisplayDevice::DISPLAY_PRIMARY, true)).Times(1);
175
176 mFlinger.processDisplayChangesLocked();
177
178 ASSERT_TRUE(mFlinger.mutableDisplays().indexOfKey(token) >= 0);
179
180 const auto& device = mFlinger.mutableDisplays().valueFor(token);
181 ASSERT_TRUE(device.get());
182 EXPECT_TRUE(device->isSecure());
183 EXPECT_TRUE(device->isPrimary());
184
185 ssize_t i = mFlinger.mutableDrawingState().displays.indexOfKey(token);
186 ASSERT_GE(0, i);
187 const auto& draw = mFlinger.mutableDrawingState().displays[i];
188 EXPECT_EQ(DisplayDevice::DISPLAY_PRIMARY, draw.type);
189
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800190 EXPECT_CALL(*mConsumer, consumerDisconnect()).Times(1);
Lloyd Piquef58625d2017-12-19 13:22:33 -0800191}
192
193} // namespace
194} // namespace android