blob: 9534f3b548cac8d206ef6a89679e440fe2244c04 [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"
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +010038#include "DisplayHardware/Hal.h"
Alec Mouriff793872022-01-13 17:45:06 -080039#include "DisplayIdentificationTestHelpers.h"
Lloyd Pique4603f3c2020-02-11 12:06:56 -080040#include "mock/DisplayHardware/MockComposer.h"
Lloyd Piquea516c002021-05-07 14:36:58 -070041#include "mock/DisplayHardware/MockHWC2.h"
Lloyd Pique4603f3c2020-02-11 12:06:56 -080042
43// TODO(b/129481165): remove the #pragma below and fix conversion issues
44#pragma clang diagnostic pop // ignored "-Wconversion"
45
46namespace android {
47namespace {
48
Marin Shalamanov6e840172020-12-14 22:13:28 +010049namespace V2_1 = hardware::graphics::composer::V2_1;
50namespace V2_4 = hardware::graphics::composer::V2_4;
Ady Abrahamde549d42022-01-26 19:19:17 -080051namespace aidl = aidl::android::hardware::graphics::composer3;
Marin Shalamanov6e840172020-12-14 22:13:28 +010052
53using Hwc2::Config;
Peiyong Line9d809e2020-04-14 13:10:48 -070054
Lloyd Pique4603f3c2020-02-11 12:06:56 -080055using ::testing::_;
56using ::testing::DoAll;
57using ::testing::ElementsAreArray;
58using ::testing::Return;
59using ::testing::SetArgPointee;
60using ::testing::StrictMock;
61
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070062struct HWComposerTest : testing::Test {
63 using HalError = hardware::graphics::composer::V2_1::Error;
Marin Shalamanov8b196592021-08-09 16:24:42 +020064
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070065 Hwc2::mock::Composer* const mHal = new StrictMock<Hwc2::mock::Composer>();
66 impl::HWComposer mHwc{std::unique_ptr<Hwc2::Composer>(mHal)};
Marin Shalamanov8b196592021-08-09 16:24:42 +020067
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070068 void expectHotplugConnect(hal::HWDisplayId hwcDisplayId) {
69 constexpr uint8_t kPort = 255;
70 EXPECT_CALL(*mHal, getDisplayIdentificationData(hwcDisplayId, _, _))
71 .WillOnce(DoAll(SetArgPointee<1>(kPort),
72 SetArgPointee<2>(getExternalEdid()), Return(HalError::NONE)));
Marin Shalamanov8b196592021-08-09 16:24:42 +020073
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070074 EXPECT_CALL(*mHal, setClientTargetSlotCount(_));
75 EXPECT_CALL(*mHal, setVsyncEnabled(hwcDisplayId, Hwc2::IComposerClient::Vsync::DISABLE));
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -040076 EXPECT_CALL(*mHal, onHotplugConnect(hwcDisplayId));
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070077 }
78};
Marin Shalamanov8b196592021-08-09 16:24:42 +020079
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070080TEST_F(HWComposerTest, isHeadless) {
81 ASSERT_TRUE(mHwc.isHeadless());
82
83 constexpr hal::HWDisplayId kHwcDisplayId = 1;
84 expectHotplugConnect(kHwcDisplayId);
85
86 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
Marin Shalamanov8b196592021-08-09 16:24:42 +020087 ASSERT_TRUE(info);
Marin Shalamanov8b196592021-08-09 16:24:42 +020088
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070089 ASSERT_FALSE(mHwc.isHeadless());
90
91 mHwc.disconnectDisplay(info->id);
92 ASSERT_TRUE(mHwc.isHeadless());
93}
94
95TEST_F(HWComposerTest, getActiveMode) {
96 // Unknown display.
97 EXPECT_EQ(mHwc.getActiveMode(PhysicalDisplayId::fromPort(0)), std::nullopt);
98
99 constexpr hal::HWDisplayId kHwcDisplayId = 2;
100 expectHotplugConnect(kHwcDisplayId);
101
102 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
103 ASSERT_TRUE(info);
104
105 {
106 // Display is known to SF but not HWC, e.g. the hotplug disconnect is pending.
107 EXPECT_CALL(*mHal, getActiveConfig(kHwcDisplayId, _))
108 .WillOnce(Return(HalError::BAD_DISPLAY));
109
110 EXPECT_EQ(mHwc.getActiveMode(info->id), std::nullopt);
111 }
112 {
113 constexpr hal::HWConfigId kConfigId = 42;
114 EXPECT_CALL(*mHal, getActiveConfig(kHwcDisplayId, _))
115 .WillOnce(DoAll(SetArgPointee<1>(kConfigId), Return(HalError::NONE)));
116
117 EXPECT_EQ(mHwc.getActiveMode(info->id), kConfigId);
118 }
Marin Shalamanov8b196592021-08-09 16:24:42 +0200119}
120
Leon Scroggins III959a7ff2023-02-07 11:24:25 -0500121TEST_F(HWComposerTest, onVsync) {
122 constexpr hal::HWDisplayId kHwcDisplayId = 1;
123 expectHotplugConnect(kHwcDisplayId);
124
125 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
126 ASSERT_TRUE(info);
127
128 const auto physicalDisplayId = info->id;
129
130 // Deliberately chosen not to match DisplayData.lastPresentTimestamp's
131 // initial value.
132 constexpr nsecs_t kTimestamp = 1;
133 auto displayIdOpt = mHwc.onVsync(kHwcDisplayId, kTimestamp);
134 ASSERT_TRUE(displayIdOpt);
135 EXPECT_EQ(physicalDisplayId, displayIdOpt);
136
137 // Attempt to send the same time stamp again.
138 displayIdOpt = mHwc.onVsync(kHwcDisplayId, kTimestamp);
139 EXPECT_FALSE(displayIdOpt);
140}
141
142TEST_F(HWComposerTest, onVsyncInvalid) {
143 constexpr hal::HWDisplayId kInvalidHwcDisplayId = 2;
144 constexpr nsecs_t kTimestamp = 1;
145 const auto displayIdOpt = mHwc.onVsync(kInvalidHwcDisplayId, kTimestamp);
146 EXPECT_FALSE(displayIdOpt);
147}
148
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700149struct MockHWC2ComposerCallback final : StrictMock<HWC2::ComposerCallback> {
Dominik Laskowski0deb06e2021-04-16 23:18:31 -0700150 MOCK_METHOD2(onComposerHalHotplug, void(hal::HWDisplayId, hal::Connection));
151 MOCK_METHOD1(onComposerHalRefresh, void(hal::HWDisplayId));
152 MOCK_METHOD3(onComposerHalVsync,
153 void(hal::HWDisplayId, int64_t timestamp, std::optional<hal::VsyncPeriodNanos>));
154 MOCK_METHOD2(onComposerHalVsyncPeriodTimingChanged,
155 void(hal::HWDisplayId, const hal::VsyncPeriodChangeTimeline&));
156 MOCK_METHOD1(onComposerHalSeamlessPossible, void(hal::HWDisplayId));
Yichi Chen1a417af2022-01-21 15:29:52 +0800157 MOCK_METHOD1(onComposerHalVsyncIdle, void(hal::HWDisplayId));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800158};
159
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700160struct HWComposerSetCallbackTest : HWComposerTest {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700161 MockHWC2ComposerCallback mCallback;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800162};
163
Dominik Laskowski0deb06e2021-04-16 23:18:31 -0700164TEST_F(HWComposerSetCallbackTest, loadsLayerMetadataSupport) {
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800165 const std::string kMetadata1Name = "com.example.metadata.1";
166 constexpr bool kMetadata1Mandatory = false;
167 const std::string kMetadata2Name = "com.example.metadata.2";
168 constexpr bool kMetadata2Mandatory = true;
169
Ady Abrahamde549d42022-01-26 19:19:17 -0800170 EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<aidl::Capability>{}));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800171 EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_))
Peiyong Line9d809e2020-04-14 13:10:48 -0700172 .WillOnce(DoAll(SetArgPointee<0>(std::vector<hal::LayerGenericMetadataKey>{
173 {kMetadata1Name, kMetadata1Mandatory},
174 {kMetadata2Name, kMetadata2Mandatory},
175 }),
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800176 Return(hardware::graphics::composer::V2_4::Error::NONE)));
Sally Qibb866c12022-10-17 11:31:20 -0700177 EXPECT_CALL(*mHal, getOverlaySupport(_)).WillOnce(Return(HalError::NONE));
Kriti Dang674b9372022-11-18 10:58:44 +0100178 EXPECT_CALL(*mHal, getHdrConversionCapabilities(_)).WillOnce(Return(HalError::NONE));
Sally Qibb866c12022-10-17 11:31:20 -0700179
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800180 EXPECT_CALL(*mHal, registerCallback(_));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800181
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700182 mHwc.setCallback(mCallback);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800183
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700184 const auto& supported = mHwc.getSupportedLayerGenericMetadata();
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800185 EXPECT_EQ(2u, supported.size());
186 EXPECT_EQ(1u, supported.count(kMetadata1Name));
187 EXPECT_EQ(kMetadata1Mandatory, supported.find(kMetadata1Name)->second);
188 EXPECT_EQ(1u, supported.count(kMetadata2Name));
189 EXPECT_EQ(kMetadata2Mandatory, supported.find(kMetadata2Name)->second);
190}
191
Dominik Laskowski0deb06e2021-04-16 23:18:31 -0700192TEST_F(HWComposerSetCallbackTest, handlesUnsupportedCallToGetLayerGenericMetadataKeys) {
Ady Abrahamde549d42022-01-26 19:19:17 -0800193 EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<aidl::Capability>{}));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800194 EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_))
195 .WillOnce(Return(hardware::graphics::composer::V2_4::Error::UNSUPPORTED));
Sally Qibb866c12022-10-17 11:31:20 -0700196 EXPECT_CALL(*mHal, getOverlaySupport(_)).WillOnce(Return(HalError::UNSUPPORTED));
Kriti Dang674b9372022-11-18 10:58:44 +0100197 EXPECT_CALL(*mHal, getHdrConversionCapabilities(_)).WillOnce(Return(HalError::UNSUPPORTED));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800198 EXPECT_CALL(*mHal, registerCallback(_));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800199
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700200 mHwc.setCallback(mCallback);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800201
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700202 const auto& supported = mHwc.getSupportedLayerGenericMetadata();
203 EXPECT_TRUE(supported.empty());
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800204}
205
206struct HWComposerLayerTest : public testing::Test {
Peiyong Line9d809e2020-04-14 13:10:48 -0700207 static constexpr hal::HWDisplayId kDisplayId = static_cast<hal::HWDisplayId>(1001);
208 static constexpr hal::HWLayerId kLayerId = static_cast<hal::HWLayerId>(1002);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800209
Ady Abrahamde549d42022-01-26 19:19:17 -0800210 HWComposerLayerTest(const std::unordered_set<aidl::Capability>& capabilities)
Lloyd Piquea516c002021-05-07 14:36:58 -0700211 : mCapabilies(capabilities) {
212 EXPECT_CALL(mDisplay, getId()).WillRepeatedly(Return(kDisplayId));
213 }
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800214
Lloyd Piquea516c002021-05-07 14:36:58 -0700215 ~HWComposerLayerTest() override {
216 EXPECT_CALL(mDisplay, onLayerDestroyed(kLayerId));
217 EXPECT_CALL(*mHal, destroyLayer(kDisplayId, kLayerId));
218 }
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800219
220 std::unique_ptr<Hwc2::mock::Composer> mHal{new StrictMock<Hwc2::mock::Composer>()};
Ady Abrahamde549d42022-01-26 19:19:17 -0800221 const std::unordered_set<aidl::Capability> mCapabilies;
Lloyd Piquea516c002021-05-07 14:36:58 -0700222 StrictMock<HWC2::mock::Display> mDisplay;
223 HWC2::impl::Layer mLayer{*mHal, mCapabilies, mDisplay, kLayerId};
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800224};
225
226struct HWComposerLayerGenericMetadataTest : public HWComposerLayerTest {
227 static const std::string kLayerGenericMetadata1Name;
228 static constexpr bool kLayerGenericMetadata1Mandatory = false;
229 static const std::vector<uint8_t> kLayerGenericMetadata1Value;
230 static const std::string kLayerGenericMetadata2Name;
231 static constexpr bool kLayerGenericMetadata2Mandatory = true;
232 static const std::vector<uint8_t> kLayerGenericMetadata2Value;
233
234 HWComposerLayerGenericMetadataTest() : HWComposerLayerTest({}) {}
235};
236
237const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Name =
238 "com.example.metadata.1";
239
240const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Value = {1u,
241 2u,
242 3u};
243
244const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Name =
245 "com.example.metadata.2";
246
247const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Value = {45u,
248 67u};
249
250TEST_F(HWComposerLayerGenericMetadataTest, forwardsSupportedMetadata) {
251 EXPECT_CALL(*mHal,
252 setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata1Name,
253 kLayerGenericMetadata1Mandatory,
254 kLayerGenericMetadata1Value))
255 .WillOnce(Return(hardware::graphics::composer::V2_4::Error::NONE));
256 auto result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata1Name,
257 kLayerGenericMetadata1Mandatory,
258 kLayerGenericMetadata1Value);
Peiyong Line9d809e2020-04-14 13:10:48 -0700259 EXPECT_EQ(hal::Error::NONE, result);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800260
261 EXPECT_CALL(*mHal,
262 setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata2Name,
263 kLayerGenericMetadata2Mandatory,
264 kLayerGenericMetadata2Value))
265 .WillOnce(Return(hardware::graphics::composer::V2_4::Error::UNSUPPORTED));
266 result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata2Name,
267 kLayerGenericMetadata2Mandatory,
268 kLayerGenericMetadata2Value);
Peiyong Line9d809e2020-04-14 13:10:48 -0700269 EXPECT_EQ(hal::Error::UNSUPPORTED, result);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800270}
271
272} // namespace
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800273} // namespace android