blob: 71986fe8048cfe205e0f9bdb31f00285fb502dcf [file] [log] [blame]
Lloyd Pique4603f3c2020-02-11 12:06:56 -08001/*
2 * Copyright 2020 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// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
21#undef LOG_TAG
22#define LOG_TAG "LibSurfaceFlingerUnittests"
23
24#include <vector>
25
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070026// StrictMock<T> derives from T and is not marked final, so the destructor of T is expected to be
27// virtual in case StrictMock<T> is used as a polymorphic base class. That is not the case here.
28#pragma clang diagnostic push
29#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
Lloyd Pique4603f3c2020-02-11 12:06:56 -080030#include <gmock/gmock.h>
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070031#pragma clang diagnostic pop
32
Lloyd Pique4603f3c2020-02-11 12:06:56 -080033#include <gui/LayerMetadata.h>
34#include <log/log.h>
35
Marin Shalamanov23c44202020-12-22 19:09:20 +010036#include "DisplayHardware/DisplayMode.h"
Lloyd Pique4603f3c2020-02-11 12:06:56 -080037#include "DisplayHardware/HWComposer.h"
38#include "mock/DisplayHardware/MockComposer.h"
39
40// TODO(b/129481165): remove the #pragma below and fix conversion issues
41#pragma clang diagnostic pop // ignored "-Wconversion"
42
43namespace android {
44namespace {
45
Marin Shalamanov6e840172020-12-14 22:13:28 +010046namespace V2_1 = hardware::graphics::composer::V2_1;
47namespace V2_4 = hardware::graphics::composer::V2_4;
48
49using Hwc2::Config;
Peiyong Line9d809e2020-04-14 13:10:48 -070050
Lloyd Pique4603f3c2020-02-11 12:06:56 -080051using ::testing::_;
52using ::testing::DoAll;
53using ::testing::ElementsAreArray;
54using ::testing::Return;
55using ::testing::SetArgPointee;
56using ::testing::StrictMock;
57
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070058struct MockHWC2ComposerCallback final : StrictMock<HWC2::ComposerCallback> {
59 MOCK_METHOD3(onHotplugReceived, void(int32_t sequenceId, hal::HWDisplayId, hal::Connection));
60 MOCK_METHOD2(onRefreshReceived, void(int32_t sequenceId, hal::HWDisplayId));
Lloyd Pique4603f3c2020-02-11 12:06:56 -080061 MOCK_METHOD4(onVsyncReceived,
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070062 void(int32_t sequenceId, hal::HWDisplayId, int64_t timestamp,
63 std::optional<hal::VsyncPeriodNanos>));
Lloyd Pique4603f3c2020-02-11 12:06:56 -080064 MOCK_METHOD3(onVsyncPeriodTimingChangedReceived,
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070065 void(int32_t sequenceId, hal::HWDisplayId, const hal::VsyncPeriodChangeTimeline&));
66 MOCK_METHOD2(onSeamlessPossible, void(int32_t sequenceId, hal::HWDisplayId));
Lloyd Pique4603f3c2020-02-11 12:06:56 -080067};
68
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070069struct HWComposerSetConfigurationTest : testing::Test {
Lloyd Pique4603f3c2020-02-11 12:06:56 -080070 Hwc2::mock::Composer* mHal = new StrictMock<Hwc2::mock::Composer>();
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070071 MockHWC2ComposerCallback mCallback;
Lloyd Pique4603f3c2020-02-11 12:06:56 -080072};
73
74TEST_F(HWComposerSetConfigurationTest, loadsLayerMetadataSupport) {
75 const std::string kMetadata1Name = "com.example.metadata.1";
76 constexpr bool kMetadata1Mandatory = false;
77 const std::string kMetadata2Name = "com.example.metadata.2";
78 constexpr bool kMetadata2Mandatory = true;
79
80 EXPECT_CALL(*mHal, getMaxVirtualDisplayCount()).WillOnce(Return(0));
Peiyong Line9d809e2020-04-14 13:10:48 -070081 EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<hal::Capability>{}));
Lloyd Pique4603f3c2020-02-11 12:06:56 -080082 EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_))
Peiyong Line9d809e2020-04-14 13:10:48 -070083 .WillOnce(DoAll(SetArgPointee<0>(std::vector<hal::LayerGenericMetadataKey>{
84 {kMetadata1Name, kMetadata1Mandatory},
85 {kMetadata2Name, kMetadata2Mandatory},
86 }),
Lloyd Pique4603f3c2020-02-11 12:06:56 -080087 Return(hardware::graphics::composer::V2_4::Error::NONE)));
88 EXPECT_CALL(*mHal, registerCallback(_));
89 EXPECT_CALL(*mHal, isVsyncPeriodSwitchSupported()).WillOnce(Return(false));
90
91 impl::HWComposer hwc{std::unique_ptr<Hwc2::Composer>(mHal)};
92 hwc.setConfiguration(&mCallback, 123);
93
94 const auto& supported = hwc.getSupportedLayerGenericMetadata();
95 EXPECT_EQ(2u, supported.size());
96 EXPECT_EQ(1u, supported.count(kMetadata1Name));
97 EXPECT_EQ(kMetadata1Mandatory, supported.find(kMetadata1Name)->second);
98 EXPECT_EQ(1u, supported.count(kMetadata2Name));
99 EXPECT_EQ(kMetadata2Mandatory, supported.find(kMetadata2Name)->second);
100}
101
102TEST_F(HWComposerSetConfigurationTest, handlesUnsupportedCallToGetLayerGenericMetadataKeys) {
103 EXPECT_CALL(*mHal, getMaxVirtualDisplayCount()).WillOnce(Return(0));
Peiyong Line9d809e2020-04-14 13:10:48 -0700104 EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<hal::Capability>{}));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800105 EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_))
106 .WillOnce(Return(hardware::graphics::composer::V2_4::Error::UNSUPPORTED));
107 EXPECT_CALL(*mHal, registerCallback(_));
108 EXPECT_CALL(*mHal, isVsyncPeriodSwitchSupported()).WillOnce(Return(false));
109
110 impl::HWComposer hwc{std::unique_ptr<Hwc2::Composer>(mHal)};
111 hwc.setConfiguration(&mCallback, 123);
112
113 const auto& supported = hwc.getSupportedLayerGenericMetadata();
114 EXPECT_EQ(0u, supported.size());
115}
116
117struct HWComposerLayerTest : public testing::Test {
Peiyong Line9d809e2020-04-14 13:10:48 -0700118 static constexpr hal::HWDisplayId kDisplayId = static_cast<hal::HWDisplayId>(1001);
119 static constexpr hal::HWLayerId kLayerId = static_cast<hal::HWLayerId>(1002);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800120
Peiyong Line9d809e2020-04-14 13:10:48 -0700121 HWComposerLayerTest(const std::unordered_set<hal::Capability>& capabilities)
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800122 : mCapabilies(capabilities) {}
123
124 ~HWComposerLayerTest() override { EXPECT_CALL(*mHal, destroyLayer(kDisplayId, kLayerId)); }
125
126 std::unique_ptr<Hwc2::mock::Composer> mHal{new StrictMock<Hwc2::mock::Composer>()};
Peiyong Line9d809e2020-04-14 13:10:48 -0700127 const std::unordered_set<hal::Capability> mCapabilies;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800128 HWC2::impl::Layer mLayer{*mHal, mCapabilies, kDisplayId, kLayerId};
129};
130
131struct HWComposerLayerGenericMetadataTest : public HWComposerLayerTest {
132 static const std::string kLayerGenericMetadata1Name;
133 static constexpr bool kLayerGenericMetadata1Mandatory = false;
134 static const std::vector<uint8_t> kLayerGenericMetadata1Value;
135 static const std::string kLayerGenericMetadata2Name;
136 static constexpr bool kLayerGenericMetadata2Mandatory = true;
137 static const std::vector<uint8_t> kLayerGenericMetadata2Value;
138
139 HWComposerLayerGenericMetadataTest() : HWComposerLayerTest({}) {}
140};
141
142const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Name =
143 "com.example.metadata.1";
144
145const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Value = {1u,
146 2u,
147 3u};
148
149const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Name =
150 "com.example.metadata.2";
151
152const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Value = {45u,
153 67u};
154
155TEST_F(HWComposerLayerGenericMetadataTest, forwardsSupportedMetadata) {
156 EXPECT_CALL(*mHal,
157 setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata1Name,
158 kLayerGenericMetadata1Mandatory,
159 kLayerGenericMetadata1Value))
160 .WillOnce(Return(hardware::graphics::composer::V2_4::Error::NONE));
161 auto result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata1Name,
162 kLayerGenericMetadata1Mandatory,
163 kLayerGenericMetadata1Value);
Peiyong Line9d809e2020-04-14 13:10:48 -0700164 EXPECT_EQ(hal::Error::NONE, result);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800165
166 EXPECT_CALL(*mHal,
167 setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata2Name,
168 kLayerGenericMetadata2Mandatory,
169 kLayerGenericMetadata2Value))
170 .WillOnce(Return(hardware::graphics::composer::V2_4::Error::UNSUPPORTED));
171 result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata2Name,
172 kLayerGenericMetadata2Mandatory,
173 kLayerGenericMetadata2Value);
Peiyong Line9d809e2020-04-14 13:10:48 -0700174 EXPECT_EQ(hal::Error::UNSUPPORTED, result);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800175}
176
Marin Shalamanov6e840172020-12-14 22:13:28 +0100177class HWComposerConfigsTest : public testing::Test {
178public:
179 Hwc2::mock::Composer* mHal = new StrictMock<Hwc2::mock::Composer>();
180 MockHWC2ComposerCallback mCallback;
181
182 void setActiveConfig(Config config) {
183 EXPECT_CALL(*mHal, getActiveConfig(_, _))
184 .WillRepeatedly(DoAll(SetArgPointee<1>(config), Return(V2_1::Error::NONE)));
185 }
186
187 void setDisplayConfigs(std::vector<Config> configs) {
188 EXPECT_CALL(*mHal, getDisplayConfigs(_, _))
189 .WillOnce(DoAll(SetArgPointee<1>(configs), Return(V2_1::Error::NONE)));
190 EXPECT_CALL(*mHal, getDisplayAttribute(_, _, _, _))
191 .WillRepeatedly(DoAll(SetArgPointee<3>(1), Return(V2_1::Error::NONE)));
192 }
193
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100194 void testSetActiveModeWithConstraintsCommon(bool isVsyncPeriodSwitchSupported);
Marin Shalamanov6e840172020-12-14 22:13:28 +0100195};
196
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100197void HWComposerConfigsTest::testSetActiveModeWithConstraintsCommon(
Marin Shalamanov6e840172020-12-14 22:13:28 +0100198 bool isVsyncPeriodSwitchSupported) {
199 EXPECT_CALL(*mHal, getMaxVirtualDisplayCount()).WillOnce(Return(0));
200 EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<hal::Capability>{}));
201 EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_)).WillOnce(Return(V2_4::Error::UNSUPPORTED));
202 EXPECT_CALL(*mHal, registerCallback(_));
203 EXPECT_CALL(*mHal, setVsyncEnabled(_, _)).WillRepeatedly(Return(V2_1::Error::NONE));
204 EXPECT_CALL(*mHal, getDisplayIdentificationData(_, _, _))
205 .WillRepeatedly(Return(V2_1::Error::UNSUPPORTED));
206 EXPECT_CALL(*mHal, setClientTargetSlotCount(_)).WillRepeatedly(Return(V2_1::Error::NONE));
207
208 EXPECT_CALL(*mHal, isVsyncPeriodSwitchSupported())
209 .WillRepeatedly(Return(isVsyncPeriodSwitchSupported));
210
211 if (isVsyncPeriodSwitchSupported) {
212 EXPECT_CALL(*mHal, setActiveConfigWithConstraints(_, _, _, _))
213 .WillRepeatedly(Return(V2_4::Error::NONE));
214 } else {
215 EXPECT_CALL(*mHal, setActiveConfig(_, _)).WillRepeatedly(Return(V2_1::Error::NONE));
216 }
217
218 impl::HWComposer hwc{std::unique_ptr<Hwc2::Composer>(mHal)};
219 hwc.setConfiguration(&mCallback, 123);
220
221 setDisplayConfigs({15});
222 setActiveConfig(15);
223
224 const auto physicalId = PhysicalDisplayId::fromPort(0);
225 const hal::HWDisplayId hwcId = 0;
226 hwc.allocatePhysicalDisplay(hwcId, physicalId);
227
228 hal::VsyncPeriodChangeConstraints constraints;
229 constraints.desiredTimeNanos = systemTime();
230 constraints.seamlessRequired = false;
231
232 hal::VsyncPeriodChangeTimeline timeline = {0, 0, 0};
Marin Shalamanov23c44202020-12-22 19:09:20 +0100233 constexpr DisplayModeId kConfigIndex(0);
Marin Shalamanov6e840172020-12-14 22:13:28 +0100234 const auto status =
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100235 hwc.setActiveModeWithConstraints(physicalId, kConfigIndex, constraints, &timeline);
Marin Shalamanov6e840172020-12-14 22:13:28 +0100236 EXPECT_EQ(NO_ERROR, status);
237
238 const std::vector<Config> kConfigs{7, 8, 9, 10, 11};
239 // Change the set of supported modes.
240 setDisplayConfigs(kConfigs);
241 setActiveConfig(11);
242 hwc.onHotplug(hwcId, hal::Connection::CONNECTED);
243 hwc.allocatePhysicalDisplay(hwcId, physicalId);
244
245 for (size_t configIndex = 0; configIndex < kConfigs.size(); configIndex++) {
Marin Shalamanov23c44202020-12-22 19:09:20 +0100246 const auto status = hwc.setActiveModeWithConstraints(physicalId, DisplayModeId(configIndex),
247 constraints, &timeline);
Marin Shalamanov6e840172020-12-14 22:13:28 +0100248 EXPECT_EQ(NO_ERROR, status) << "Error when switching to config " << configIndex;
249 }
250}
251
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100252TEST_F(HWComposerConfigsTest, setActiveModeWithConstraintsWithVsyncSwitchingSupported) {
253 testSetActiveModeWithConstraintsCommon(/*supported=*/true);
Marin Shalamanov6e840172020-12-14 22:13:28 +0100254}
255
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100256TEST_F(HWComposerConfigsTest, setActiveModeWithConstraintsWithVsyncSwitchingNotSupported) {
257 testSetActiveModeWithConstraintsCommon(/*supported=*/false);
Marin Shalamanov6e840172020-12-14 22:13:28 +0100258}
259
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800260} // namespace
Marin Shalamanov6e840172020-12-14 22:13:28 +0100261} // namespace android