blob: da00377cae4bb3ab2bd87f9e7e552a1d555712d6 [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
ramindani12bfe6b2023-02-03 13:29:19 -080055using ::aidl::android::hardware::graphics::composer3::RefreshRateChangedDebugData;
Lloyd Pique4603f3c2020-02-11 12:06:56 -080056using ::testing::_;
57using ::testing::DoAll;
58using ::testing::ElementsAreArray;
59using ::testing::Return;
60using ::testing::SetArgPointee;
61using ::testing::StrictMock;
62
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070063struct HWComposerTest : testing::Test {
64 using HalError = hardware::graphics::composer::V2_1::Error;
Marin Shalamanov8b196592021-08-09 16:24:42 +020065
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070066 Hwc2::mock::Composer* const mHal = new StrictMock<Hwc2::mock::Composer>();
67 impl::HWComposer mHwc{std::unique_ptr<Hwc2::Composer>(mHal)};
Marin Shalamanov8b196592021-08-09 16:24:42 +020068
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070069 void expectHotplugConnect(hal::HWDisplayId hwcDisplayId) {
70 constexpr uint8_t kPort = 255;
71 EXPECT_CALL(*mHal, getDisplayIdentificationData(hwcDisplayId, _, _))
72 .WillOnce(DoAll(SetArgPointee<1>(kPort),
73 SetArgPointee<2>(getExternalEdid()), Return(HalError::NONE)));
Marin Shalamanov8b196592021-08-09 16:24:42 +020074
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070075 EXPECT_CALL(*mHal, setClientTargetSlotCount(_));
76 EXPECT_CALL(*mHal, setVsyncEnabled(hwcDisplayId, Hwc2::IComposerClient::Vsync::DISABLE));
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -040077 EXPECT_CALL(*mHal, onHotplugConnect(hwcDisplayId));
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070078 }
79};
Marin Shalamanov8b196592021-08-09 16:24:42 +020080
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070081TEST_F(HWComposerTest, isHeadless) {
82 ASSERT_TRUE(mHwc.isHeadless());
83
84 constexpr hal::HWDisplayId kHwcDisplayId = 1;
85 expectHotplugConnect(kHwcDisplayId);
86
87 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
Marin Shalamanov8b196592021-08-09 16:24:42 +020088 ASSERT_TRUE(info);
Marin Shalamanov8b196592021-08-09 16:24:42 +020089
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070090 ASSERT_FALSE(mHwc.isHeadless());
91
92 mHwc.disconnectDisplay(info->id);
93 ASSERT_TRUE(mHwc.isHeadless());
94}
95
96TEST_F(HWComposerTest, getActiveMode) {
97 // Unknown display.
98 EXPECT_EQ(mHwc.getActiveMode(PhysicalDisplayId::fromPort(0)), std::nullopt);
99
100 constexpr hal::HWDisplayId kHwcDisplayId = 2;
101 expectHotplugConnect(kHwcDisplayId);
102
103 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
104 ASSERT_TRUE(info);
105
106 {
107 // Display is known to SF but not HWC, e.g. the hotplug disconnect is pending.
108 EXPECT_CALL(*mHal, getActiveConfig(kHwcDisplayId, _))
109 .WillOnce(Return(HalError::BAD_DISPLAY));
110
111 EXPECT_EQ(mHwc.getActiveMode(info->id), std::nullopt);
112 }
113 {
114 constexpr hal::HWConfigId kConfigId = 42;
115 EXPECT_CALL(*mHal, getActiveConfig(kHwcDisplayId, _))
116 .WillOnce(DoAll(SetArgPointee<1>(kConfigId), Return(HalError::NONE)));
117
118 EXPECT_EQ(mHwc.getActiveMode(info->id), kConfigId);
119 }
Marin Shalamanov8b196592021-08-09 16:24:42 +0200120}
121
Leon Scroggins III959a7ff2023-02-07 11:24:25 -0500122TEST_F(HWComposerTest, onVsync) {
123 constexpr hal::HWDisplayId kHwcDisplayId = 1;
124 expectHotplugConnect(kHwcDisplayId);
125
126 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
127 ASSERT_TRUE(info);
128
129 const auto physicalDisplayId = info->id;
130
131 // Deliberately chosen not to match DisplayData.lastPresentTimestamp's
132 // initial value.
133 constexpr nsecs_t kTimestamp = 1;
134 auto displayIdOpt = mHwc.onVsync(kHwcDisplayId, kTimestamp);
135 ASSERT_TRUE(displayIdOpt);
136 EXPECT_EQ(physicalDisplayId, displayIdOpt);
137
138 // Attempt to send the same time stamp again.
139 displayIdOpt = mHwc.onVsync(kHwcDisplayId, kTimestamp);
140 EXPECT_FALSE(displayIdOpt);
141}
142
143TEST_F(HWComposerTest, onVsyncInvalid) {
144 constexpr hal::HWDisplayId kInvalidHwcDisplayId = 2;
145 constexpr nsecs_t kTimestamp = 1;
146 const auto displayIdOpt = mHwc.onVsync(kInvalidHwcDisplayId, kTimestamp);
147 EXPECT_FALSE(displayIdOpt);
148}
149
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700150struct MockHWC2ComposerCallback final : StrictMock<HWC2::ComposerCallback> {
Dominik Laskowski0deb06e2021-04-16 23:18:31 -0700151 MOCK_METHOD2(onComposerHalHotplug, void(hal::HWDisplayId, hal::Connection));
152 MOCK_METHOD1(onComposerHalRefresh, void(hal::HWDisplayId));
153 MOCK_METHOD3(onComposerHalVsync,
154 void(hal::HWDisplayId, int64_t timestamp, std::optional<hal::VsyncPeriodNanos>));
155 MOCK_METHOD2(onComposerHalVsyncPeriodTimingChanged,
156 void(hal::HWDisplayId, const hal::VsyncPeriodChangeTimeline&));
157 MOCK_METHOD1(onComposerHalSeamlessPossible, void(hal::HWDisplayId));
Yichi Chen1a417af2022-01-21 15:29:52 +0800158 MOCK_METHOD1(onComposerHalVsyncIdle, void(hal::HWDisplayId));
ramindani12bfe6b2023-02-03 13:29:19 -0800159 MOCK_METHOD(void, onRefreshRateChangedDebug, (const RefreshRateChangedDebugData&), (override));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800160};
161
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700162struct HWComposerSetCallbackTest : HWComposerTest {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700163 MockHWC2ComposerCallback mCallback;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800164};
165
Dominik Laskowski0deb06e2021-04-16 23:18:31 -0700166TEST_F(HWComposerSetCallbackTest, loadsLayerMetadataSupport) {
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800167 const std::string kMetadata1Name = "com.example.metadata.1";
168 constexpr bool kMetadata1Mandatory = false;
169 const std::string kMetadata2Name = "com.example.metadata.2";
170 constexpr bool kMetadata2Mandatory = true;
171
Ady Abrahamde549d42022-01-26 19:19:17 -0800172 EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<aidl::Capability>{}));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800173 EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_))
Peiyong Line9d809e2020-04-14 13:10:48 -0700174 .WillOnce(DoAll(SetArgPointee<0>(std::vector<hal::LayerGenericMetadataKey>{
175 {kMetadata1Name, kMetadata1Mandatory},
176 {kMetadata2Name, kMetadata2Mandatory},
177 }),
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800178 Return(hardware::graphics::composer::V2_4::Error::NONE)));
Sally Qibb866c12022-10-17 11:31:20 -0700179 EXPECT_CALL(*mHal, getOverlaySupport(_)).WillOnce(Return(HalError::NONE));
Kriti Dang674b9372022-11-18 10:58:44 +0100180 EXPECT_CALL(*mHal, getHdrConversionCapabilities(_)).WillOnce(Return(HalError::NONE));
Sally Qibb866c12022-10-17 11:31:20 -0700181
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800182 EXPECT_CALL(*mHal, registerCallback(_));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800183
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700184 mHwc.setCallback(mCallback);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800185
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700186 const auto& supported = mHwc.getSupportedLayerGenericMetadata();
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800187 EXPECT_EQ(2u, supported.size());
188 EXPECT_EQ(1u, supported.count(kMetadata1Name));
189 EXPECT_EQ(kMetadata1Mandatory, supported.find(kMetadata1Name)->second);
190 EXPECT_EQ(1u, supported.count(kMetadata2Name));
191 EXPECT_EQ(kMetadata2Mandatory, supported.find(kMetadata2Name)->second);
192}
193
Dominik Laskowski0deb06e2021-04-16 23:18:31 -0700194TEST_F(HWComposerSetCallbackTest, handlesUnsupportedCallToGetLayerGenericMetadataKeys) {
Ady Abrahamde549d42022-01-26 19:19:17 -0800195 EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<aidl::Capability>{}));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800196 EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_))
197 .WillOnce(Return(hardware::graphics::composer::V2_4::Error::UNSUPPORTED));
Sally Qibb866c12022-10-17 11:31:20 -0700198 EXPECT_CALL(*mHal, getOverlaySupport(_)).WillOnce(Return(HalError::UNSUPPORTED));
Kriti Dang674b9372022-11-18 10:58:44 +0100199 EXPECT_CALL(*mHal, getHdrConversionCapabilities(_)).WillOnce(Return(HalError::UNSUPPORTED));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800200 EXPECT_CALL(*mHal, registerCallback(_));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800201
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700202 mHwc.setCallback(mCallback);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800203
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700204 const auto& supported = mHwc.getSupportedLayerGenericMetadata();
205 EXPECT_TRUE(supported.empty());
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800206}
207
208struct HWComposerLayerTest : public testing::Test {
Peiyong Line9d809e2020-04-14 13:10:48 -0700209 static constexpr hal::HWDisplayId kDisplayId = static_cast<hal::HWDisplayId>(1001);
210 static constexpr hal::HWLayerId kLayerId = static_cast<hal::HWLayerId>(1002);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800211
Ady Abrahamde549d42022-01-26 19:19:17 -0800212 HWComposerLayerTest(const std::unordered_set<aidl::Capability>& capabilities)
Lloyd Piquea516c002021-05-07 14:36:58 -0700213 : mCapabilies(capabilities) {
214 EXPECT_CALL(mDisplay, getId()).WillRepeatedly(Return(kDisplayId));
215 }
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800216
Lloyd Piquea516c002021-05-07 14:36:58 -0700217 ~HWComposerLayerTest() override {
218 EXPECT_CALL(mDisplay, onLayerDestroyed(kLayerId));
219 EXPECT_CALL(*mHal, destroyLayer(kDisplayId, kLayerId));
220 }
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800221
222 std::unique_ptr<Hwc2::mock::Composer> mHal{new StrictMock<Hwc2::mock::Composer>()};
Ady Abrahamde549d42022-01-26 19:19:17 -0800223 const std::unordered_set<aidl::Capability> mCapabilies;
Lloyd Piquea516c002021-05-07 14:36:58 -0700224 StrictMock<HWC2::mock::Display> mDisplay;
225 HWC2::impl::Layer mLayer{*mHal, mCapabilies, mDisplay, kLayerId};
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800226};
227
228struct HWComposerLayerGenericMetadataTest : public HWComposerLayerTest {
229 static const std::string kLayerGenericMetadata1Name;
230 static constexpr bool kLayerGenericMetadata1Mandatory = false;
231 static const std::vector<uint8_t> kLayerGenericMetadata1Value;
232 static const std::string kLayerGenericMetadata2Name;
233 static constexpr bool kLayerGenericMetadata2Mandatory = true;
234 static const std::vector<uint8_t> kLayerGenericMetadata2Value;
235
236 HWComposerLayerGenericMetadataTest() : HWComposerLayerTest({}) {}
237};
238
239const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Name =
240 "com.example.metadata.1";
241
242const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Value = {1u,
243 2u,
244 3u};
245
246const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Name =
247 "com.example.metadata.2";
248
249const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Value = {45u,
250 67u};
251
252TEST_F(HWComposerLayerGenericMetadataTest, forwardsSupportedMetadata) {
253 EXPECT_CALL(*mHal,
254 setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata1Name,
255 kLayerGenericMetadata1Mandatory,
256 kLayerGenericMetadata1Value))
257 .WillOnce(Return(hardware::graphics::composer::V2_4::Error::NONE));
258 auto result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata1Name,
259 kLayerGenericMetadata1Mandatory,
260 kLayerGenericMetadata1Value);
Peiyong Line9d809e2020-04-14 13:10:48 -0700261 EXPECT_EQ(hal::Error::NONE, result);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800262
263 EXPECT_CALL(*mHal,
264 setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata2Name,
265 kLayerGenericMetadata2Mandatory,
266 kLayerGenericMetadata2Value))
267 .WillOnce(Return(hardware::graphics::composer::V2_4::Error::UNSUPPORTED));
268 result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata2Name,
269 kLayerGenericMetadata2Mandatory,
270 kLayerGenericMetadata2Value);
Peiyong Line9d809e2020-04-14 13:10:48 -0700271 EXPECT_EQ(hal::Error::UNSUPPORTED, result);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800272}
273
274} // namespace
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800275} // namespace android