blob: 4f545a9ef3864b512918036f7e6a3d907602d792 [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>
ramindanib90711b2023-10-02 15:13:19 -070035#include <chrono>
Lloyd Pique4603f3c2020-02-11 12:06:56 -080036
Marin Shalamanov23c44202020-12-22 19:09:20 +010037#include "DisplayHardware/DisplayMode.h"
Lloyd Pique4603f3c2020-02-11 12:06:56 -080038#include "DisplayHardware/HWComposer.h"
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +010039#include "DisplayHardware/Hal.h"
Alec Mouriff793872022-01-13 17:45:06 -080040#include "DisplayIdentificationTestHelpers.h"
Lloyd Pique4603f3c2020-02-11 12:06:56 -080041#include "mock/DisplayHardware/MockComposer.h"
Lloyd Piquea516c002021-05-07 14:36:58 -070042#include "mock/DisplayHardware/MockHWC2.h"
Lloyd Pique4603f3c2020-02-11 12:06:56 -080043
44// TODO(b/129481165): remove the #pragma below and fix conversion issues
45#pragma clang diagnostic pop // ignored "-Wconversion"
46
47namespace android {
Lloyd Pique4603f3c2020-02-11 12:06:56 -080048
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;
ramindanib90711b2023-10-02 15:13:19 -070052using namespace std::chrono_literals;
Marin Shalamanov6e840172020-12-14 22:13:28 +010053
54using Hwc2::Config;
Peiyong Line9d809e2020-04-14 13:10:48 -070055
ramindani12bfe6b2023-02-03 13:29:19 -080056using ::aidl::android::hardware::graphics::composer3::RefreshRateChangedDebugData;
ramindani0cd1d8d2023-06-13 13:43:23 -070057using hal::IComposerClient;
Lloyd Pique4603f3c2020-02-11 12:06:56 -080058using ::testing::_;
59using ::testing::DoAll;
60using ::testing::ElementsAreArray;
61using ::testing::Return;
62using ::testing::SetArgPointee;
63using ::testing::StrictMock;
64
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070065struct HWComposerTest : testing::Test {
66 using HalError = hardware::graphics::composer::V2_1::Error;
Marin Shalamanov8b196592021-08-09 16:24:42 +020067
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070068 Hwc2::mock::Composer* const mHal = new StrictMock<Hwc2::mock::Composer>();
69 impl::HWComposer mHwc{std::unique_ptr<Hwc2::Composer>(mHal)};
Marin Shalamanov8b196592021-08-09 16:24:42 +020070
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070071 void expectHotplugConnect(hal::HWDisplayId hwcDisplayId) {
72 constexpr uint8_t kPort = 255;
73 EXPECT_CALL(*mHal, getDisplayIdentificationData(hwcDisplayId, _, _))
74 .WillOnce(DoAll(SetArgPointee<1>(kPort),
75 SetArgPointee<2>(getExternalEdid()), Return(HalError::NONE)));
Marin Shalamanov8b196592021-08-09 16:24:42 +020076
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070077 EXPECT_CALL(*mHal, setClientTargetSlotCount(_));
78 EXPECT_CALL(*mHal, setVsyncEnabled(hwcDisplayId, Hwc2::IComposerClient::Vsync::DISABLE));
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -040079 EXPECT_CALL(*mHal, onHotplugConnect(hwcDisplayId));
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070080 }
ramindanib90711b2023-10-02 15:13:19 -070081
82 void setDisplayData(HalDisplayId displayId, nsecs_t lastExpectedPresentTimestamp) {
83 ASSERT_TRUE(mHwc.mDisplayData.find(displayId) != mHwc.mDisplayData.end());
84 auto& displayData = mHwc.mDisplayData.at(displayId);
85 displayData.lastExpectedPresentTimestamp = lastExpectedPresentTimestamp;
86 }
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070087};
Marin Shalamanov8b196592021-08-09 16:24:42 +020088
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070089TEST_F(HWComposerTest, isHeadless) {
90 ASSERT_TRUE(mHwc.isHeadless());
91
92 constexpr hal::HWDisplayId kHwcDisplayId = 1;
93 expectHotplugConnect(kHwcDisplayId);
94
95 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
Marin Shalamanov8b196592021-08-09 16:24:42 +020096 ASSERT_TRUE(info);
Marin Shalamanov8b196592021-08-09 16:24:42 +020097
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070098 ASSERT_FALSE(mHwc.isHeadless());
99
100 mHwc.disconnectDisplay(info->id);
101 ASSERT_TRUE(mHwc.isHeadless());
102}
103
104TEST_F(HWComposerTest, getActiveMode) {
105 // Unknown display.
106 EXPECT_EQ(mHwc.getActiveMode(PhysicalDisplayId::fromPort(0)), std::nullopt);
107
108 constexpr hal::HWDisplayId kHwcDisplayId = 2;
109 expectHotplugConnect(kHwcDisplayId);
110
111 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
112 ASSERT_TRUE(info);
113
114 {
115 // Display is known to SF but not HWC, e.g. the hotplug disconnect is pending.
116 EXPECT_CALL(*mHal, getActiveConfig(kHwcDisplayId, _))
117 .WillOnce(Return(HalError::BAD_DISPLAY));
118
119 EXPECT_EQ(mHwc.getActiveMode(info->id), std::nullopt);
120 }
121 {
122 constexpr hal::HWConfigId kConfigId = 42;
123 EXPECT_CALL(*mHal, getActiveConfig(kHwcDisplayId, _))
124 .WillOnce(DoAll(SetArgPointee<1>(kConfigId), Return(HalError::NONE)));
125
126 EXPECT_EQ(mHwc.getActiveMode(info->id), kConfigId);
127 }
Marin Shalamanov8b196592021-08-09 16:24:42 +0200128}
129
ramindani0cd1d8d2023-06-13 13:43:23 -0700130TEST_F(HWComposerTest, getModesWithLegacyDisplayConfigs) {
131 constexpr hal::HWDisplayId kHwcDisplayId = 2;
132 constexpr hal::HWConfigId kConfigId = 42;
ramindani263a3f12023-07-18 20:44:49 -0700133 constexpr int32_t kMaxFrameIntervalNs = 50000000; // 20Fps
ramindani0cd1d8d2023-06-13 13:43:23 -0700134
135 expectHotplugConnect(kHwcDisplayId);
136 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
137 ASSERT_TRUE(info);
138
139 EXPECT_CALL(*mHal, getDisplayConfigurationsSupported()).WillRepeatedly(Return(false));
140
141 {
142 EXPECT_CALL(*mHal, getDisplayConfigs(kHwcDisplayId, _))
143 .WillOnce(Return(HalError::BAD_DISPLAY));
ramindani263a3f12023-07-18 20:44:49 -0700144 EXPECT_TRUE(mHwc.getModes(info->id, kMaxFrameIntervalNs).empty());
ramindani0cd1d8d2023-06-13 13:43:23 -0700145 }
146 {
147 constexpr int32_t kWidth = 480;
148 constexpr int32_t kHeight = 720;
149 constexpr int32_t kConfigGroup = 1;
150 constexpr int32_t kVsyncPeriod = 16666667;
151
152 EXPECT_CALL(*mHal,
153 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::WIDTH,
154 _))
155 .WillRepeatedly(DoAll(SetArgPointee<3>(kWidth), Return(HalError::NONE)));
156 EXPECT_CALL(*mHal,
157 getDisplayAttribute(kHwcDisplayId, kConfigId,
158 IComposerClient::Attribute::HEIGHT, _))
159 .WillRepeatedly(DoAll(SetArgPointee<3>(kHeight), Return(HalError::NONE)));
160 EXPECT_CALL(*mHal,
161 getDisplayAttribute(kHwcDisplayId, kConfigId,
162 IComposerClient::Attribute::CONFIG_GROUP, _))
163 .WillRepeatedly(DoAll(SetArgPointee<3>(kConfigGroup), Return(HalError::NONE)));
164 EXPECT_CALL(*mHal,
165 getDisplayAttribute(kHwcDisplayId, kConfigId,
166 IComposerClient::Attribute::VSYNC_PERIOD, _))
167 .WillRepeatedly(DoAll(SetArgPointee<3>(kVsyncPeriod), Return(HalError::NONE)));
168
169 // Optional Parameters UNSUPPORTED
170 EXPECT_CALL(*mHal,
171 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_X,
172 _))
173 .WillOnce(Return(HalError::UNSUPPORTED));
174 EXPECT_CALL(*mHal,
175 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_Y,
176 _))
177 .WillOnce(Return(HalError::UNSUPPORTED));
178
179 EXPECT_CALL(*mHal, getDisplayConfigs(kHwcDisplayId, _))
180 .WillRepeatedly(DoAll(SetArgPointee<1>(std::vector<hal::HWConfigId>{kConfigId}),
181 Return(HalError::NONE)));
182
ramindani263a3f12023-07-18 20:44:49 -0700183 auto modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
ramindani0cd1d8d2023-06-13 13:43:23 -0700184 EXPECT_EQ(modes.size(), size_t{1});
185 EXPECT_EQ(modes.front().hwcId, kConfigId);
186 EXPECT_EQ(modes.front().width, kWidth);
187 EXPECT_EQ(modes.front().height, kHeight);
188 EXPECT_EQ(modes.front().configGroup, kConfigGroup);
189 EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
190 EXPECT_EQ(modes.front().dpiX, -1);
191 EXPECT_EQ(modes.front().dpiY, -1);
192
193 // Optional parameters are supported
194 constexpr int32_t kDpi = 320;
195 EXPECT_CALL(*mHal,
196 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_X,
197 _))
198 .WillOnce(DoAll(SetArgPointee<3>(kDpi), Return(HalError::NONE)));
199 EXPECT_CALL(*mHal,
200 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_Y,
201 _))
202 .WillOnce(DoAll(SetArgPointee<3>(kDpi), Return(HalError::NONE)));
203
ramindani263a3f12023-07-18 20:44:49 -0700204 modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
ramindani0cd1d8d2023-06-13 13:43:23 -0700205 EXPECT_EQ(modes.size(), size_t{1});
206 EXPECT_EQ(modes.front().hwcId, kConfigId);
207 EXPECT_EQ(modes.front().width, kWidth);
208 EXPECT_EQ(modes.front().height, kHeight);
209 EXPECT_EQ(modes.front().configGroup, kConfigGroup);
210 EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
211 // DPI values are scaled by 1000 in the legacy implementation.
212 EXPECT_EQ(modes.front().dpiX, kDpi / 1000.f);
213 EXPECT_EQ(modes.front().dpiY, kDpi / 1000.f);
214 }
215}
216
217TEST_F(HWComposerTest, getModesWithDisplayConfigurations) {
218 constexpr hal::HWDisplayId kHwcDisplayId = 2;
219 constexpr hal::HWConfigId kConfigId = 42;
ramindani263a3f12023-07-18 20:44:49 -0700220 constexpr int32_t kMaxFrameIntervalNs = 50000000; // 20Fps
ramindani0cd1d8d2023-06-13 13:43:23 -0700221 expectHotplugConnect(kHwcDisplayId);
222 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
223 ASSERT_TRUE(info);
224
225 EXPECT_CALL(*mHal, getDisplayConfigurationsSupported()).WillRepeatedly(Return(true));
226
227 {
ramindani263a3f12023-07-18 20:44:49 -0700228 EXPECT_CALL(*mHal, getDisplayConfigurations(kHwcDisplayId, _, _))
ramindani0cd1d8d2023-06-13 13:43:23 -0700229 .WillOnce(Return(HalError::BAD_DISPLAY));
ramindani263a3f12023-07-18 20:44:49 -0700230 EXPECT_TRUE(mHwc.getModes(info->id, kMaxFrameIntervalNs).empty());
ramindani0cd1d8d2023-06-13 13:43:23 -0700231 }
232 {
233 constexpr int32_t kWidth = 480;
234 constexpr int32_t kHeight = 720;
235 constexpr int32_t kConfigGroup = 1;
236 constexpr int32_t kVsyncPeriod = 16666667;
ramindanib90711b2023-10-02 15:13:19 -0700237 const hal::VrrConfig vrrConfig =
238 hal::VrrConfig{.minFrameIntervalNs = static_cast<Fps>(120_Hz).getPeriodNsecs(),
239 .notifyExpectedPresentConfig = hal::VrrConfig::
240 NotifyExpectedPresentConfig{.notifyExpectedPresentHeadsUpNs =
241 ms2ns(30),
242 .notifyExpectedPresentTimeoutNs =
243 ms2ns(30)}};
244 hal::DisplayConfiguration displayConfiguration{.configId = kConfigId,
245 .width = kWidth,
246 .height = kHeight,
247 .configGroup = kConfigGroup,
248 .vsyncPeriod = kVsyncPeriod,
249 .vrrConfig = vrrConfig};
ramindani0cd1d8d2023-06-13 13:43:23 -0700250
ramindani263a3f12023-07-18 20:44:49 -0700251 EXPECT_CALL(*mHal, getDisplayConfigurations(kHwcDisplayId, _, _))
252 .WillOnce(DoAll(SetArgPointee<2>(std::vector<hal::DisplayConfiguration>{
ramindani0cd1d8d2023-06-13 13:43:23 -0700253 displayConfiguration}),
254 Return(HalError::NONE)));
255
256 // Optional dpi not supported
ramindani263a3f12023-07-18 20:44:49 -0700257 auto modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
ramindani0cd1d8d2023-06-13 13:43:23 -0700258 EXPECT_EQ(modes.size(), size_t{1});
259 EXPECT_EQ(modes.front().hwcId, kConfigId);
260 EXPECT_EQ(modes.front().width, kWidth);
261 EXPECT_EQ(modes.front().height, kHeight);
262 EXPECT_EQ(modes.front().configGroup, kConfigGroup);
263 EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
ramindanib90711b2023-10-02 15:13:19 -0700264 EXPECT_EQ(modes.front().vrrConfig, vrrConfig);
ramindani0cd1d8d2023-06-13 13:43:23 -0700265 EXPECT_EQ(modes.front().dpiX, -1);
266 EXPECT_EQ(modes.front().dpiY, -1);
267
268 // Supports optional dpi parameter
269 constexpr int32_t kDpi = 320;
270 displayConfiguration.dpi = {kDpi, kDpi};
271
ramindani263a3f12023-07-18 20:44:49 -0700272 EXPECT_CALL(*mHal, getDisplayConfigurations(kHwcDisplayId, _, _))
273 .WillOnce(DoAll(SetArgPointee<2>(std::vector<hal::DisplayConfiguration>{
ramindani0cd1d8d2023-06-13 13:43:23 -0700274 displayConfiguration}),
275 Return(HalError::NONE)));
276
ramindani263a3f12023-07-18 20:44:49 -0700277 modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
ramindani0cd1d8d2023-06-13 13:43:23 -0700278 EXPECT_EQ(modes.size(), size_t{1});
279 EXPECT_EQ(modes.front().hwcId, kConfigId);
280 EXPECT_EQ(modes.front().width, kWidth);
281 EXPECT_EQ(modes.front().height, kHeight);
282 EXPECT_EQ(modes.front().configGroup, kConfigGroup);
283 EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
ramindanib90711b2023-10-02 15:13:19 -0700284 EXPECT_EQ(modes.front().vrrConfig, vrrConfig);
ramindani0cd1d8d2023-06-13 13:43:23 -0700285 EXPECT_EQ(modes.front().dpiX, kDpi);
286 EXPECT_EQ(modes.front().dpiY, kDpi);
287 }
288}
289
Leon Scroggins III959a7ff2023-02-07 11:24:25 -0500290TEST_F(HWComposerTest, onVsync) {
291 constexpr hal::HWDisplayId kHwcDisplayId = 1;
292 expectHotplugConnect(kHwcDisplayId);
293
294 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
295 ASSERT_TRUE(info);
296
297 const auto physicalDisplayId = info->id;
298
299 // Deliberately chosen not to match DisplayData.lastPresentTimestamp's
300 // initial value.
301 constexpr nsecs_t kTimestamp = 1;
302 auto displayIdOpt = mHwc.onVsync(kHwcDisplayId, kTimestamp);
303 ASSERT_TRUE(displayIdOpt);
304 EXPECT_EQ(physicalDisplayId, displayIdOpt);
305
306 // Attempt to send the same time stamp again.
307 displayIdOpt = mHwc.onVsync(kHwcDisplayId, kTimestamp);
308 EXPECT_FALSE(displayIdOpt);
309}
310
311TEST_F(HWComposerTest, onVsyncInvalid) {
312 constexpr hal::HWDisplayId kInvalidHwcDisplayId = 2;
313 constexpr nsecs_t kTimestamp = 1;
314 const auto displayIdOpt = mHwc.onVsync(kInvalidHwcDisplayId, kTimestamp);
315 EXPECT_FALSE(displayIdOpt);
316}
317
ramindanib90711b2023-10-02 15:13:19 -0700318TEST_F(HWComposerTest, notifyExpectedPresentTimeout) {
319 constexpr hal::HWDisplayId kHwcDisplayId = 2;
320 expectHotplugConnect(kHwcDisplayId);
321 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
322 ASSERT_TRUE(info);
323
324 auto expectedPresentTime = systemTime() + ms2ns(10);
325 const int32_t frameIntervalNs = static_cast<Fps>(60_Hz).getPeriodNsecs();
326 static constexpr nsecs_t kTimeoutNs = ms2ns(30);
327
328 ASSERT_NO_FATAL_FAILURE(setDisplayData(info->id, /* lastExpectedPresentTimestamp= */ 0));
329
330 {
331 // Very first ExpectedPresent after idle, no previous timestamp
332 EXPECT_CALL(*mHal,
333 notifyExpectedPresent(kHwcDisplayId, expectedPresentTime, frameIntervalNs))
334 .WillOnce(Return(HalError::NONE));
335 mHwc.notifyExpectedPresentIfRequired(info->id, expectedPresentTime, frameIntervalNs,
336 kTimeoutNs);
337 }
338 {
339 // ExpectedPresent is after the timeoutNs
340 expectedPresentTime += ms2ns(50);
341 EXPECT_CALL(*mHal,
342 notifyExpectedPresent(kHwcDisplayId, expectedPresentTime, frameIntervalNs))
343 .WillOnce(Return(HalError::NONE));
344 mHwc.notifyExpectedPresentIfRequired(info->id, expectedPresentTime, frameIntervalNs,
345 kTimeoutNs);
346 }
347 {
348 // ExpectedPresent is after the last reported ExpectedPresent.
349 expectedPresentTime += ms2ns(10);
350 EXPECT_CALL(*mHal, notifyExpectedPresent(kHwcDisplayId, _, _)).Times(0);
351 mHwc.notifyExpectedPresentIfRequired(info->id, expectedPresentTime, frameIntervalNs,
352 kTimeoutNs);
353 }
354 {
355 // ExpectedPresent is before the last reported ExpectedPresent but after the timeoutNs,
356 // representing we changed our decision and want to present earlier than previously
357 // reported.
358 expectedPresentTime -= ms2ns(20);
359 EXPECT_CALL(*mHal,
360 notifyExpectedPresent(kHwcDisplayId, expectedPresentTime, frameIntervalNs))
361 .WillOnce(Return(HalError::NONE));
362 mHwc.notifyExpectedPresentIfRequired(info->id, expectedPresentTime, frameIntervalNs,
363 kTimeoutNs);
364 }
365}
366
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700367struct MockHWC2ComposerCallback final : StrictMock<HWC2::ComposerCallback> {
Dominik Laskowski0deb06e2021-04-16 23:18:31 -0700368 MOCK_METHOD2(onComposerHalHotplug, void(hal::HWDisplayId, hal::Connection));
369 MOCK_METHOD1(onComposerHalRefresh, void(hal::HWDisplayId));
370 MOCK_METHOD3(onComposerHalVsync,
371 void(hal::HWDisplayId, int64_t timestamp, std::optional<hal::VsyncPeriodNanos>));
372 MOCK_METHOD2(onComposerHalVsyncPeriodTimingChanged,
373 void(hal::HWDisplayId, const hal::VsyncPeriodChangeTimeline&));
374 MOCK_METHOD1(onComposerHalSeamlessPossible, void(hal::HWDisplayId));
Yichi Chen1a417af2022-01-21 15:29:52 +0800375 MOCK_METHOD1(onComposerHalVsyncIdle, void(hal::HWDisplayId));
ramindani12bfe6b2023-02-03 13:29:19 -0800376 MOCK_METHOD(void, onRefreshRateChangedDebug, (const RefreshRateChangedDebugData&), (override));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800377};
378
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700379struct HWComposerSetCallbackTest : HWComposerTest {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700380 MockHWC2ComposerCallback mCallback;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800381};
382
Dominik Laskowski0deb06e2021-04-16 23:18:31 -0700383TEST_F(HWComposerSetCallbackTest, loadsLayerMetadataSupport) {
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800384 const std::string kMetadata1Name = "com.example.metadata.1";
385 constexpr bool kMetadata1Mandatory = false;
386 const std::string kMetadata2Name = "com.example.metadata.2";
387 constexpr bool kMetadata2Mandatory = true;
388
Ady Abrahamde549d42022-01-26 19:19:17 -0800389 EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<aidl::Capability>{}));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800390 EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_))
Peiyong Line9d809e2020-04-14 13:10:48 -0700391 .WillOnce(DoAll(SetArgPointee<0>(std::vector<hal::LayerGenericMetadataKey>{
392 {kMetadata1Name, kMetadata1Mandatory},
393 {kMetadata2Name, kMetadata2Mandatory},
394 }),
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800395 Return(hardware::graphics::composer::V2_4::Error::NONE)));
Sally Qibb866c12022-10-17 11:31:20 -0700396 EXPECT_CALL(*mHal, getOverlaySupport(_)).WillOnce(Return(HalError::NONE));
Kriti Dang674b9372022-11-18 10:58:44 +0100397 EXPECT_CALL(*mHal, getHdrConversionCapabilities(_)).WillOnce(Return(HalError::NONE));
Sally Qibb866c12022-10-17 11:31:20 -0700398
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800399 EXPECT_CALL(*mHal, registerCallback(_));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800400
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700401 mHwc.setCallback(mCallback);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800402
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700403 const auto& supported = mHwc.getSupportedLayerGenericMetadata();
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800404 EXPECT_EQ(2u, supported.size());
405 EXPECT_EQ(1u, supported.count(kMetadata1Name));
406 EXPECT_EQ(kMetadata1Mandatory, supported.find(kMetadata1Name)->second);
407 EXPECT_EQ(1u, supported.count(kMetadata2Name));
408 EXPECT_EQ(kMetadata2Mandatory, supported.find(kMetadata2Name)->second);
409}
410
Dominik Laskowski0deb06e2021-04-16 23:18:31 -0700411TEST_F(HWComposerSetCallbackTest, handlesUnsupportedCallToGetLayerGenericMetadataKeys) {
Ady Abrahamde549d42022-01-26 19:19:17 -0800412 EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<aidl::Capability>{}));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800413 EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_))
414 .WillOnce(Return(hardware::graphics::composer::V2_4::Error::UNSUPPORTED));
Sally Qibb866c12022-10-17 11:31:20 -0700415 EXPECT_CALL(*mHal, getOverlaySupport(_)).WillOnce(Return(HalError::UNSUPPORTED));
Kriti Dang674b9372022-11-18 10:58:44 +0100416 EXPECT_CALL(*mHal, getHdrConversionCapabilities(_)).WillOnce(Return(HalError::UNSUPPORTED));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800417 EXPECT_CALL(*mHal, registerCallback(_));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800418
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700419 mHwc.setCallback(mCallback);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800420
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700421 const auto& supported = mHwc.getSupportedLayerGenericMetadata();
422 EXPECT_TRUE(supported.empty());
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800423}
424
425struct HWComposerLayerTest : public testing::Test {
Peiyong Line9d809e2020-04-14 13:10:48 -0700426 static constexpr hal::HWDisplayId kDisplayId = static_cast<hal::HWDisplayId>(1001);
427 static constexpr hal::HWLayerId kLayerId = static_cast<hal::HWLayerId>(1002);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800428
Ady Abrahamde549d42022-01-26 19:19:17 -0800429 HWComposerLayerTest(const std::unordered_set<aidl::Capability>& capabilities)
Lloyd Piquea516c002021-05-07 14:36:58 -0700430 : mCapabilies(capabilities) {
431 EXPECT_CALL(mDisplay, getId()).WillRepeatedly(Return(kDisplayId));
432 }
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800433
Lloyd Piquea516c002021-05-07 14:36:58 -0700434 ~HWComposerLayerTest() override {
435 EXPECT_CALL(mDisplay, onLayerDestroyed(kLayerId));
436 EXPECT_CALL(*mHal, destroyLayer(kDisplayId, kLayerId));
437 }
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800438
439 std::unique_ptr<Hwc2::mock::Composer> mHal{new StrictMock<Hwc2::mock::Composer>()};
Ady Abrahamde549d42022-01-26 19:19:17 -0800440 const std::unordered_set<aidl::Capability> mCapabilies;
Lloyd Piquea516c002021-05-07 14:36:58 -0700441 StrictMock<HWC2::mock::Display> mDisplay;
442 HWC2::impl::Layer mLayer{*mHal, mCapabilies, mDisplay, kLayerId};
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800443};
444
445struct HWComposerLayerGenericMetadataTest : public HWComposerLayerTest {
446 static const std::string kLayerGenericMetadata1Name;
447 static constexpr bool kLayerGenericMetadata1Mandatory = false;
448 static const std::vector<uint8_t> kLayerGenericMetadata1Value;
449 static const std::string kLayerGenericMetadata2Name;
450 static constexpr bool kLayerGenericMetadata2Mandatory = true;
451 static const std::vector<uint8_t> kLayerGenericMetadata2Value;
452
453 HWComposerLayerGenericMetadataTest() : HWComposerLayerTest({}) {}
454};
455
456const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Name =
457 "com.example.metadata.1";
458
459const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Value = {1u,
460 2u,
461 3u};
462
463const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Name =
464 "com.example.metadata.2";
465
466const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Value = {45u,
467 67u};
468
469TEST_F(HWComposerLayerGenericMetadataTest, forwardsSupportedMetadata) {
470 EXPECT_CALL(*mHal,
471 setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata1Name,
472 kLayerGenericMetadata1Mandatory,
473 kLayerGenericMetadata1Value))
474 .WillOnce(Return(hardware::graphics::composer::V2_4::Error::NONE));
475 auto result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata1Name,
476 kLayerGenericMetadata1Mandatory,
477 kLayerGenericMetadata1Value);
Peiyong Line9d809e2020-04-14 13:10:48 -0700478 EXPECT_EQ(hal::Error::NONE, result);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800479
480 EXPECT_CALL(*mHal,
481 setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata2Name,
482 kLayerGenericMetadata2Mandatory,
483 kLayerGenericMetadata2Value))
484 .WillOnce(Return(hardware::graphics::composer::V2_4::Error::UNSUPPORTED));
485 result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata2Name,
486 kLayerGenericMetadata2Mandatory,
487 kLayerGenericMetadata2Value);
Peiyong Line9d809e2020-04-14 13:10:48 -0700488 EXPECT_EQ(hal::Error::UNSUPPORTED, result);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800489}
490
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800491} // namespace android