blob: 3841209aa1924b5e01dacb0f864e19ceba3773b1 [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"
27#include "MockRenderEngine.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080028#include "TestableSurfaceFlinger.h"
29
30namespace android {
31namespace {
32
Lloyd Piquee39cad22017-12-20 17:01:29 -080033using testing::_;
34using testing::ByMove;
35using testing::DoAll;
36using testing::Mock;
37using testing::Return;
38using testing::SetArgPointee;
39
40using android::hardware::graphics::common::V1_0::Hdr;
41using android::Hwc2::Error;
42using android::Hwc2::IComposer;
43using android::Hwc2::IComposerClient;
44
45constexpr int32_t DEFAULT_REFRESH_RATE = 1666666666;
46constexpr int32_t DEFAULT_DPI = 320;
47
Lloyd Piquef58625d2017-12-19 13:22:33 -080048class DisplayTransactionTest : public testing::Test {
49protected:
50 DisplayTransactionTest();
51 ~DisplayTransactionTest() override;
52
53 void setupComposer(int virtualDisplayCount);
54 void setupPrimaryDisplay(int width, int height);
55
56 TestableSurfaceFlinger mFlinger;
Lloyd Piquee39cad22017-12-20 17:01:29 -080057 mock::EventThread* mEventThread = new mock::EventThread();
58
59 // These mocks are created by the test, but are destroyed by SurfaceFlinger
60 // by virtue of being stored into a std::unique_ptr. However we still need
61 // to keep a reference to them for use in setting up call expectations.
62 RE::mock::RenderEngine* mRenderEngine = new RE::mock::RenderEngine();
63 Hwc2::mock::Composer* mComposer = new Hwc2::mock::Composer();
Lloyd Piquef58625d2017-12-19 13:22:33 -080064};
65
66DisplayTransactionTest::DisplayTransactionTest() {
67 const ::testing::TestInfo* const test_info =
68 ::testing::UnitTest::GetInstance()->current_test_info();
69 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
Lloyd Piquee39cad22017-12-20 17:01:29 -080070
71 mFlinger.mutableEventThread().reset(mEventThread);
72 mFlinger.setupRenderEngine(std::unique_ptr<RE::RenderEngine>(mRenderEngine));
73
74 setupComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -080075}
76
77DisplayTransactionTest::~DisplayTransactionTest() {
78 const ::testing::TestInfo* const test_info =
79 ::testing::UnitTest::GetInstance()->current_test_info();
80 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
81}
82
Lloyd Piquee39cad22017-12-20 17:01:29 -080083void DisplayTransactionTest::setupComposer(int virtualDisplayCount) {
84 EXPECT_CALL(*mComposer, getCapabilities())
85 .WillOnce(Return(std::vector<IComposer::Capability>()));
86 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
87 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -080088
Lloyd Piquee39cad22017-12-20 17:01:29 -080089 Mock::VerifyAndClear(mComposer);
90}
91
92void DisplayTransactionTest::setupPrimaryDisplay(int width, int height) {
93 EXPECT_CALL(*mComposer, getDisplayType(DisplayDevice::DISPLAY_PRIMARY, _))
94 .WillOnce(DoAll(SetArgPointee<1>(IComposerClient::DisplayType::PHYSICAL),
95 Return(Error::NONE)));
96 EXPECT_CALL(*mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
97 EXPECT_CALL(*mComposer, getDisplayConfigs(_, _))
98 .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{0}), Return(Error::NONE)));
99 EXPECT_CALL(*mComposer,
100 getDisplayAttribute(DisplayDevice::DISPLAY_PRIMARY, 0,
101 IComposerClient::Attribute::WIDTH, _))
102 .WillOnce(DoAll(SetArgPointee<3>(width), Return(Error::NONE)));
103 EXPECT_CALL(*mComposer,
104 getDisplayAttribute(DisplayDevice::DISPLAY_PRIMARY, 0,
105 IComposerClient::Attribute::HEIGHT, _))
106 .WillOnce(DoAll(SetArgPointee<3>(height), Return(Error::NONE)));
107 EXPECT_CALL(*mComposer,
108 getDisplayAttribute(DisplayDevice::DISPLAY_PRIMARY, 0,
109 IComposerClient::Attribute::VSYNC_PERIOD, _))
110 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE)));
111 EXPECT_CALL(*mComposer,
112 getDisplayAttribute(DisplayDevice::DISPLAY_PRIMARY, 0,
113 IComposerClient::Attribute::DPI_X, _))
114 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
115 EXPECT_CALL(*mComposer,
116 getDisplayAttribute(DisplayDevice::DISPLAY_PRIMARY, 0,
117 IComposerClient::Attribute::DPI_Y, _))
118 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
119
120 mFlinger.setupPrimaryDisplay();
121
122 Mock::VerifyAndClear(mComposer);
123}
124
125TEST_F(DisplayTransactionTest, processDisplayChangesLockedProcessesPrimaryDisplayConnected) {
126 using android::hardware::graphics::common::V1_0::ColorMode;
127
128 setupPrimaryDisplay(1920, 1080);
129
130 sp<BBinder> token = new BBinder();
131 mFlinger.mutableCurrentState().displays.add(token, {DisplayDevice::DISPLAY_PRIMARY, true});
132
133 EXPECT_CALL(*mComposer, getActiveConfig(DisplayDevice::DISPLAY_PRIMARY, _))
134 .WillOnce(DoAll(SetArgPointee<1>(0), Return(Error::NONE)));
135 EXPECT_CALL(*mComposer, getColorModes(DisplayDevice::DISPLAY_PRIMARY, _))
136 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>({ColorMode::NATIVE})),
137 Return(Error::NONE)));
138
139 EXPECT_CALL(*mComposer, getHdrCapabilities(DisplayDevice::DISPLAY_PRIMARY, _, _, _, _))
140 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
141
142 auto reSurface = new RE::mock::Surface();
143 EXPECT_CALL(*mRenderEngine, createSurface())
144 .WillOnce(Return(ByMove(std::unique_ptr<RE::Surface>(reSurface))));
145 EXPECT_CALL(*reSurface, setAsync(false)).Times(1);
146 EXPECT_CALL(*reSurface, setCritical(true)).Times(1);
147 EXPECT_CALL(*reSurface, setNativeWindow(_)).Times(1);
148 EXPECT_CALL(*reSurface, queryWidth()).WillOnce(Return(1920));
149 EXPECT_CALL(*reSurface, queryHeight()).WillOnce(Return(1080));
150
151 EXPECT_CALL(*mEventThread, onHotplugReceived(DisplayDevice::DISPLAY_PRIMARY, true)).Times(1);
152
153 mFlinger.processDisplayChangesLocked();
154
155 ASSERT_TRUE(mFlinger.mutableDisplays().indexOfKey(token) >= 0);
156
157 const auto& device = mFlinger.mutableDisplays().valueFor(token);
158 ASSERT_TRUE(device.get());
159 EXPECT_TRUE(device->isSecure());
160 EXPECT_TRUE(device->isPrimary());
161
162 ssize_t i = mFlinger.mutableDrawingState().displays.indexOfKey(token);
163 ASSERT_GE(0, i);
164 const auto& draw = mFlinger.mutableDrawingState().displays[i];
165 EXPECT_EQ(DisplayDevice::DISPLAY_PRIMARY, draw.type);
166
167 EXPECT_CALL(*mComposer, setVsyncEnabled(0, IComposerClient::Vsync::DISABLE))
168 .WillOnce(Return(Error::NONE));
Lloyd Piquef58625d2017-12-19 13:22:33 -0800169}
170
171} // namespace
172} // namespace android