xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/tests/unittests/HWComposerTest.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
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 <optional>
25 #include <vector>
26 
27 // StrictMock<T> derives from T and is not marked final, so the destructor of T is expected to be
28 // virtual in case StrictMock<T> is used as a polymorphic base class. That is not the case here.
29 #pragma clang diagnostic push
30 #pragma clang diagnostic ignored "-Wnon-virtual-dtor"
31 #include <gmock/gmock.h>
32 #pragma clang diagnostic pop
33 
34 #include <common/FlagManager.h>
35 #include <gui/LayerMetadata.h>
36 #include <log/log.h>
37 #include <chrono>
38 
39 #include <common/test/FlagUtils.h>
40 #include "DisplayHardware/DisplayMode.h"
41 #include "DisplayHardware/HWComposer.h"
42 #include "DisplayHardware/Hal.h"
43 #include "DisplayIdentificationTestHelpers.h"
44 #include "mock/DisplayHardware/MockComposer.h"
45 #include "mock/DisplayHardware/MockHWC2.h"
46 
47 #include <com_android_graphics_surfaceflinger_flags.h>
48 
49 // TODO(b/129481165): remove the #pragma below and fix conversion issues
50 #pragma clang diagnostic pop // ignored "-Wconversion"
51 
52 namespace android {
53 
54 namespace V2_1 = hardware::graphics::composer::V2_1;
55 namespace V2_4 = hardware::graphics::composer::V2_4;
56 namespace aidl = aidl::android::hardware::graphics::composer3;
57 using namespace std::chrono_literals;
58 
59 using Hwc2::Config;
60 
61 using ::aidl::android::hardware::drm::HdcpLevels;
62 using ::aidl::android::hardware::graphics::common::DisplayHotplugEvent;
63 using ::aidl::android::hardware::graphics::composer3::RefreshRateChangedDebugData;
64 using hal::IComposerClient;
65 using ::testing::_;
66 using ::testing::DoAll;
67 using ::testing::Return;
68 using ::testing::SetArgPointee;
69 using ::testing::StrictMock;
70 
71 struct HWComposerTest : testing::Test {
72     using HalError = hal::Error;
73 
74     Hwc2::mock::Composer* const mHal = new StrictMock<Hwc2::mock::Composer>();
75     impl::HWComposer mHwc{std::unique_ptr<Hwc2::Composer>(mHal)};
76 
expectHotplugConnectandroid::HWComposerTest77     void expectHotplugConnect(hal::HWDisplayId hwcDisplayId) {
78         constexpr uint8_t kPort = 255;
79         EXPECT_CALL(*mHal, getDisplayIdentificationData(hwcDisplayId, _, _))
80                 .WillOnce(DoAll(SetArgPointee<1>(kPort),
81                                 SetArgPointee<2>(getExternalEdid()), Return(HalError::NONE)));
82 
83         EXPECT_CALL(*mHal, setClientTargetSlotCount(_));
84         EXPECT_CALL(*mHal, setVsyncEnabled(hwcDisplayId, Hwc2::IComposerClient::Vsync::DISABLE));
85         EXPECT_CALL(*mHal, onHotplugConnect(hwcDisplayId));
86     }
87 
setVrrTimeoutHintandroid::HWComposerTest88     void setVrrTimeoutHint(bool status) { mHwc.mEnableVrrTimeout = status; }
89 };
90 
TEST_F(HWComposerTest,isHeadless)91 TEST_F(HWComposerTest, isHeadless) {
92     ASSERT_TRUE(mHwc.isHeadless());
93 
94     constexpr hal::HWDisplayId kHwcDisplayId = 1;
95     expectHotplugConnect(kHwcDisplayId);
96 
97     const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
98     ASSERT_TRUE(info);
99 
100     ASSERT_FALSE(mHwc.isHeadless());
101 
102     mHwc.disconnectDisplay(info->id);
103     ASSERT_TRUE(mHwc.isHeadless());
104 }
105 
TEST_F(HWComposerTest,getDisplayConnectionType)106 TEST_F(HWComposerTest, getDisplayConnectionType) {
107     // Unknown display.
108     EXPECT_EQ(mHwc.getDisplayConnectionType(PhysicalDisplayId::fromPort(0)),
109               ui::DisplayConnectionType::Internal);
110 
111     constexpr hal::HWDisplayId kHwcDisplayId = 1;
112     expectHotplugConnect(kHwcDisplayId);
113 
114     const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
115     ASSERT_TRUE(info);
116 
117     EXPECT_CALL(*mHal, getDisplayConnectionType(kHwcDisplayId, _))
118             .WillOnce(DoAll(SetArgPointee<1>(IComposerClient::DisplayConnectionType::EXTERNAL),
119                             Return(V2_4::Error::NONE)));
120 
121     // The first call caches the connection type.
122     EXPECT_EQ(mHwc.getDisplayConnectionType(info->id), ui::DisplayConnectionType::External);
123 
124     // Subsequent calls return the cached connection type.
125     EXPECT_EQ(mHwc.getDisplayConnectionType(info->id), ui::DisplayConnectionType::External);
126     EXPECT_EQ(mHwc.getDisplayConnectionType(info->id), ui::DisplayConnectionType::External);
127 }
128 
TEST_F(HWComposerTest,getActiveMode)129 TEST_F(HWComposerTest, getActiveMode) {
130     // Unknown display.
131     EXPECT_EQ(mHwc.getActiveMode(PhysicalDisplayId::fromPort(0)), ftl::Unexpected(BAD_INDEX));
132 
133     constexpr hal::HWDisplayId kHwcDisplayId = 2;
134     expectHotplugConnect(kHwcDisplayId);
135 
136     const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
137     ASSERT_TRUE(info);
138 
139     {
140         // Display is known to SF but not HWC, e.g. the hotplug disconnect is pending.
141         EXPECT_CALL(*mHal, getActiveConfig(kHwcDisplayId, _))
142                 .WillOnce(Return(HalError::BAD_DISPLAY));
143 
144         EXPECT_EQ(mHwc.getActiveMode(info->id), ftl::Unexpected(UNKNOWN_ERROR));
145     }
146     {
147         EXPECT_CALL(*mHal, getActiveConfig(kHwcDisplayId, _))
148                 .WillOnce(Return(HalError::BAD_CONFIG));
149 
150         EXPECT_EQ(mHwc.getActiveMode(info->id), ftl::Unexpected(NO_INIT));
151     }
152     {
153         constexpr hal::HWConfigId kConfigId = 42;
154         EXPECT_CALL(*mHal, getActiveConfig(kHwcDisplayId, _))
155                 .WillOnce(DoAll(SetArgPointee<1>(kConfigId), Return(HalError::NONE)));
156 
157         EXPECT_EQ(mHwc.getActiveMode(info->id).value_opt(), kConfigId);
158     }
159 }
160 
TEST_F(HWComposerTest,getModesWithLegacyDisplayConfigs)161 TEST_F(HWComposerTest, getModesWithLegacyDisplayConfigs) {
162     constexpr hal::HWDisplayId kHwcDisplayId = 2;
163     constexpr hal::HWConfigId kConfigId = 42;
164     constexpr int32_t kMaxFrameIntervalNs = 50000000; // 20Fps
165 
166     expectHotplugConnect(kHwcDisplayId);
167     const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
168     ASSERT_TRUE(info);
169     ASSERT_TRUE(info->preferredDetailedTimingDescriptor.has_value());
170 
171     EXPECT_CALL(*mHal, isVrrSupported()).WillRepeatedly(Return(false));
172 
173     {
174         EXPECT_CALL(*mHal, getDisplayConfigs(kHwcDisplayId, _))
175                 .WillOnce(Return(HalError::BAD_DISPLAY));
176         EXPECT_TRUE(mHwc.getModes(info->id, kMaxFrameIntervalNs).empty());
177     }
178     {
179         constexpr int32_t kWidth = 480;
180         constexpr int32_t kHeight = 720;
181         constexpr int32_t kConfigGroup = 1;
182         constexpr int32_t kVsyncPeriod = 16666667;
183         constexpr float kMmPerInch = 25.4f;
184         const ui::Size size = info->preferredDetailedTimingDescriptor->physicalSizeInMm;
185         const float expectedDpiX = (kWidth * kMmPerInch / size.width);
186         const float expectedDpiY = (kHeight * kMmPerInch / size.height);
187 
188         EXPECT_CALL(*mHal,
189                     getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::WIDTH,
190                                         _))
191                 .WillRepeatedly(DoAll(SetArgPointee<3>(kWidth), Return(HalError::NONE)));
192         EXPECT_CALL(*mHal,
193                     getDisplayAttribute(kHwcDisplayId, kConfigId,
194                                         IComposerClient::Attribute::HEIGHT, _))
195                 .WillRepeatedly(DoAll(SetArgPointee<3>(kHeight), Return(HalError::NONE)));
196         EXPECT_CALL(*mHal,
197                     getDisplayAttribute(kHwcDisplayId, kConfigId,
198                                         IComposerClient::Attribute::CONFIG_GROUP, _))
199                 .WillRepeatedly(DoAll(SetArgPointee<3>(kConfigGroup), Return(HalError::NONE)));
200         EXPECT_CALL(*mHal,
201                     getDisplayAttribute(kHwcDisplayId, kConfigId,
202                                         IComposerClient::Attribute::VSYNC_PERIOD, _))
203                 .WillRepeatedly(DoAll(SetArgPointee<3>(kVsyncPeriod), Return(HalError::NONE)));
204 
205         // Optional Parameters UNSUPPORTED
206         EXPECT_CALL(*mHal,
207                     getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_X,
208                                         _))
209                 .WillOnce(Return(HalError::UNSUPPORTED));
210         EXPECT_CALL(*mHal,
211                     getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_Y,
212                                         _))
213                 .WillOnce(Return(HalError::UNSUPPORTED));
214 
215         EXPECT_CALL(*mHal, getDisplayConfigs(kHwcDisplayId, _))
216                 .WillRepeatedly(DoAll(SetArgPointee<1>(std::vector<hal::HWConfigId>{kConfigId}),
217                                       Return(HalError::NONE)));
218 
219         auto modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
220         EXPECT_EQ(modes.size(), size_t{1});
221         EXPECT_EQ(modes.front().hwcId, kConfigId);
222         EXPECT_EQ(modes.front().width, kWidth);
223         EXPECT_EQ(modes.front().height, kHeight);
224         EXPECT_EQ(modes.front().configGroup, kConfigGroup);
225         EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
226         if (!FlagManager::getInstance().correct_dpi_with_display_size()) {
227             EXPECT_EQ(modes.front().dpiX, -1);
228             EXPECT_EQ(modes.front().dpiY, -1);
229         } else {
230             EXPECT_EQ(modes.front().dpiX, expectedDpiX);
231             EXPECT_EQ(modes.front().dpiY, expectedDpiY);
232         }
233 
234         // Optional parameters are supported
235         constexpr int32_t kDpi = 320;
236         EXPECT_CALL(*mHal,
237                     getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_X,
238                                         _))
239                 .WillOnce(DoAll(SetArgPointee<3>(kDpi), Return(HalError::NONE)));
240         EXPECT_CALL(*mHal,
241                     getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_Y,
242                                         _))
243                 .WillOnce(DoAll(SetArgPointee<3>(kDpi), Return(HalError::NONE)));
244 
245         modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
246         EXPECT_EQ(modes.size(), size_t{1});
247         EXPECT_EQ(modes.front().hwcId, kConfigId);
248         EXPECT_EQ(modes.front().width, kWidth);
249         EXPECT_EQ(modes.front().height, kHeight);
250         EXPECT_EQ(modes.front().configGroup, kConfigGroup);
251         EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
252         // DPI values are scaled by 1000 in the legacy implementation.
253         EXPECT_EQ(modes.front().dpiX, kDpi / 1000.f);
254         EXPECT_EQ(modes.front().dpiY, kDpi / 1000.f);
255     }
256 }
257 
TEST_F(HWComposerTest,getModesWithDisplayConfigurations_VRR_OFF)258 TEST_F(HWComposerTest, getModesWithDisplayConfigurations_VRR_OFF) {
259     // if vrr_config is off, getDisplayConfigurationsSupported() is off as well
260     // then getModesWithLegacyDisplayConfigs should be called instead
261     SET_FLAG_FOR_TEST(com::android::graphics::surfaceflinger::flags::vrr_config, false);
262     ASSERT_FALSE(FlagManager::getInstance().vrr_config());
263 
264     constexpr hal::HWDisplayId kHwcDisplayId = 2;
265     constexpr hal::HWConfigId kConfigId = 42;
266     constexpr int32_t kMaxFrameIntervalNs = 50000000; // 20Fps
267 
268     expectHotplugConnect(kHwcDisplayId);
269     const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
270     ASSERT_TRUE(info);
271 
272     EXPECT_CALL(*mHal, isVrrSupported()).WillRepeatedly(Return(false));
273 
274     {
275         EXPECT_CALL(*mHal, getDisplayConfigs(kHwcDisplayId, _))
276                 .WillOnce(Return(HalError::BAD_DISPLAY));
277         EXPECT_TRUE(mHwc.getModes(info->id, kMaxFrameIntervalNs).empty());
278     }
279     {
280         constexpr int32_t kWidth = 480;
281         constexpr int32_t kHeight = 720;
282         constexpr int32_t kConfigGroup = 1;
283         constexpr int32_t kVsyncPeriod = 16666667;
284         constexpr float kMmPerInch = 25.4f;
285         const ui::Size size = info->preferredDetailedTimingDescriptor->physicalSizeInMm;
286         const float expectedDpiX = (kWidth * kMmPerInch / size.width);
287         const float expectedDpiY = (kHeight * kMmPerInch / size.height);
288 
289         EXPECT_CALL(*mHal,
290                     getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::WIDTH,
291                                         _))
292                 .WillRepeatedly(DoAll(SetArgPointee<3>(kWidth), Return(HalError::NONE)));
293         EXPECT_CALL(*mHal,
294                     getDisplayAttribute(kHwcDisplayId, kConfigId,
295                                         IComposerClient::Attribute::HEIGHT, _))
296                 .WillRepeatedly(DoAll(SetArgPointee<3>(kHeight), Return(HalError::NONE)));
297         EXPECT_CALL(*mHal,
298                     getDisplayAttribute(kHwcDisplayId, kConfigId,
299                                         IComposerClient::Attribute::CONFIG_GROUP, _))
300                 .WillRepeatedly(DoAll(SetArgPointee<3>(kConfigGroup), Return(HalError::NONE)));
301         EXPECT_CALL(*mHal,
302                     getDisplayAttribute(kHwcDisplayId, kConfigId,
303                                         IComposerClient::Attribute::VSYNC_PERIOD, _))
304                 .WillRepeatedly(DoAll(SetArgPointee<3>(kVsyncPeriod), Return(HalError::NONE)));
305 
306         // Optional Parameters UNSUPPORTED
307         EXPECT_CALL(*mHal,
308                     getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_X,
309                                         _))
310                 .WillOnce(Return(HalError::UNSUPPORTED));
311         EXPECT_CALL(*mHal,
312                     getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_Y,
313                                         _))
314                 .WillOnce(Return(HalError::UNSUPPORTED));
315 
316         EXPECT_CALL(*mHal, getDisplayConfigs(kHwcDisplayId, _))
317                 .WillRepeatedly(DoAll(SetArgPointee<1>(std::vector<hal::HWConfigId>{kConfigId}),
318                                       Return(HalError::NONE)));
319 
320         auto modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
321         EXPECT_EQ(modes.size(), size_t{1});
322         EXPECT_EQ(modes.front().hwcId, kConfigId);
323         EXPECT_EQ(modes.front().width, kWidth);
324         EXPECT_EQ(modes.front().height, kHeight);
325         EXPECT_EQ(modes.front().configGroup, kConfigGroup);
326         EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
327         if (!FlagManager::getInstance().correct_dpi_with_display_size()) {
328             EXPECT_EQ(modes.front().dpiX, -1);
329             EXPECT_EQ(modes.front().dpiY, -1);
330         } else {
331             EXPECT_EQ(modes.front().dpiX, expectedDpiX);
332             EXPECT_EQ(modes.front().dpiY, expectedDpiY);
333         }
334 
335         // Optional parameters are supported
336         constexpr int32_t kDpi = 320;
337         EXPECT_CALL(*mHal,
338                     getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_X,
339                                         _))
340                 .WillOnce(DoAll(SetArgPointee<3>(kDpi), Return(HalError::NONE)));
341         EXPECT_CALL(*mHal,
342                     getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_Y,
343                                         _))
344                 .WillOnce(DoAll(SetArgPointee<3>(kDpi), Return(HalError::NONE)));
345 
346         modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
347         EXPECT_EQ(modes.size(), size_t{1});
348         EXPECT_EQ(modes.front().hwcId, kConfigId);
349         EXPECT_EQ(modes.front().width, kWidth);
350         EXPECT_EQ(modes.front().height, kHeight);
351         EXPECT_EQ(modes.front().configGroup, kConfigGroup);
352         EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
353         // DPI values are scaled by 1000 in the legacy implementation.
354         EXPECT_EQ(modes.front().dpiX, kDpi / 1000.f);
355         EXPECT_EQ(modes.front().dpiY, kDpi / 1000.f);
356     }
357 }
358 
TEST_F(HWComposerTest,getModesWithDisplayConfigurations_VRR_ON)359 TEST_F(HWComposerTest, getModesWithDisplayConfigurations_VRR_ON) {
360     SET_FLAG_FOR_TEST(com::android::graphics::surfaceflinger::flags::vrr_config, true);
361     ASSERT_TRUE(FlagManager::getInstance().vrr_config());
362 
363     constexpr hal::HWDisplayId kHwcDisplayId = 2;
364     constexpr hal::HWConfigId kConfigId = 42;
365     constexpr int32_t kMaxFrameIntervalNs = 50000000; // 20Fps
366     expectHotplugConnect(kHwcDisplayId);
367     const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
368     ASSERT_TRUE(info);
369 
370     EXPECT_CALL(*mHal, isVrrSupported()).WillRepeatedly(Return(true));
371 
372     {
373         EXPECT_CALL(*mHal, getDisplayConfigurations(kHwcDisplayId, _, _))
374                 .WillOnce(Return(HalError::BAD_DISPLAY));
375         EXPECT_TRUE(mHwc.getModes(info->id, kMaxFrameIntervalNs).empty());
376     }
377     {
378         setVrrTimeoutHint(true);
379         constexpr int32_t kWidth = 480;
380         constexpr int32_t kHeight = 720;
381         constexpr int32_t kConfigGroup = 1;
382         constexpr int32_t kVsyncPeriod = 16666667;
383         constexpr float kMmPerInch = 25.4f;
384         const ui::Size size = info->preferredDetailedTimingDescriptor->physicalSizeInMm;
385         const float expectedDpiX = (kWidth * kMmPerInch / size.width);
386         const float expectedDpiY = (kHeight * kMmPerInch / size.height);
387         const OutputType hdrOutputType = OutputType::SYSTEM;
388         const hal::VrrConfig vrrConfig =
389                 hal::VrrConfig{.minFrameIntervalNs = static_cast<Fps>(120_Hz).getPeriodNsecs(),
390                                .notifyExpectedPresentConfig = hal::VrrConfig::
391                                        NotifyExpectedPresentConfig{.headsUpNs = ms2ns(30),
392                                                                    .timeoutNs = ms2ns(30)}};
393         hal::DisplayConfiguration displayConfiguration{.configId = kConfigId,
394                                                        .width = kWidth,
395                                                        .height = kHeight,
396                                                        .configGroup = kConfigGroup,
397                                                        .vsyncPeriod = kVsyncPeriod,
398                                                        .vrrConfig = vrrConfig,
399                                                        .hdrOutputType = hdrOutputType};
400 
401         EXPECT_CALL(*mHal, getDisplayConfigurations(kHwcDisplayId, _, _))
402                 .WillOnce(DoAll(SetArgPointee<2>(std::vector<hal::DisplayConfiguration>{
403                                         displayConfiguration}),
404                                 Return(HalError::NONE)));
405 
406         // Optional dpi not supported
407         auto modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
408         EXPECT_EQ(modes.size(), size_t{1});
409         EXPECT_EQ(modes.front().hwcId, kConfigId);
410         EXPECT_EQ(modes.front().width, kWidth);
411         EXPECT_EQ(modes.front().height, kHeight);
412         EXPECT_EQ(modes.front().configGroup, kConfigGroup);
413         EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
414         EXPECT_EQ(modes.front().vrrConfig, vrrConfig);
415         EXPECT_EQ(modes.front().hdrOutputType, hdrOutputType);
416         if (!FlagManager::getInstance().correct_dpi_with_display_size()) {
417             EXPECT_EQ(modes.front().dpiX, -1);
418             EXPECT_EQ(modes.front().dpiY, -1);
419         } else {
420             EXPECT_EQ(modes.front().dpiX, expectedDpiX);
421             EXPECT_EQ(modes.front().dpiY, expectedDpiY);
422         }
423 
424         // Supports optional dpi parameter
425         constexpr int32_t kDpi = 320;
426         displayConfiguration.dpi = {kDpi, kDpi};
427 
428         EXPECT_CALL(*mHal, getDisplayConfigurations(kHwcDisplayId, _, _))
429                 .WillRepeatedly(DoAll(SetArgPointee<2>(std::vector<hal::DisplayConfiguration>{
430                                               displayConfiguration}),
431                                       Return(HalError::NONE)));
432 
433         modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
434         EXPECT_EQ(modes.size(), size_t{1});
435         EXPECT_EQ(modes.front().hwcId, kConfigId);
436         EXPECT_EQ(modes.front().width, kWidth);
437         EXPECT_EQ(modes.front().height, kHeight);
438         EXPECT_EQ(modes.front().configGroup, kConfigGroup);
439         EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
440         EXPECT_EQ(modes.front().vrrConfig, vrrConfig);
441         EXPECT_EQ(modes.front().hdrOutputType, hdrOutputType);
442         EXPECT_EQ(modes.front().dpiX, kDpi);
443         EXPECT_EQ(modes.front().dpiY, kDpi);
444 
445         setVrrTimeoutHint(false);
446         modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
447         EXPECT_EQ(modes.front().vrrConfig->notifyExpectedPresentConfig, std::nullopt);
448     }
449 }
450 
TEST_F(HWComposerTest,onVsync)451 TEST_F(HWComposerTest, onVsync) {
452     constexpr hal::HWDisplayId kHwcDisplayId = 1;
453     expectHotplugConnect(kHwcDisplayId);
454 
455     const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
456     ASSERT_TRUE(info);
457 
458     const auto physicalDisplayId = info->id;
459 
460     // Deliberately chosen not to match DisplayData.lastPresentTimestamp's
461     // initial value.
462     constexpr nsecs_t kTimestamp = 1;
463     auto displayIdOpt = mHwc.onVsync(kHwcDisplayId, kTimestamp);
464     ASSERT_TRUE(displayIdOpt);
465     EXPECT_EQ(physicalDisplayId, displayIdOpt);
466 
467     // Attempt to send the same time stamp again.
468     displayIdOpt = mHwc.onVsync(kHwcDisplayId, kTimestamp);
469     EXPECT_FALSE(displayIdOpt);
470 }
471 
TEST_F(HWComposerTest,onVsyncInvalid)472 TEST_F(HWComposerTest, onVsyncInvalid) {
473     constexpr hal::HWDisplayId kInvalidHwcDisplayId = 2;
474     constexpr nsecs_t kTimestamp = 1;
475     const auto displayIdOpt = mHwc.onVsync(kInvalidHwcDisplayId, kTimestamp);
476     EXPECT_FALSE(displayIdOpt);
477 }
478 
479 struct MockHWC2ComposerCallback final : StrictMock<HWC2::ComposerCallback> {
480     MOCK_METHOD(void, onComposerHalHotplugEvent, (hal::HWDisplayId, DisplayHotplugEvent),
481                 (override));
482     MOCK_METHOD1(onComposerHalRefresh, void(hal::HWDisplayId));
483     MOCK_METHOD3(onComposerHalVsync,
484                  void(hal::HWDisplayId, int64_t timestamp, std::optional<hal::VsyncPeriodNanos>));
485     MOCK_METHOD2(onComposerHalVsyncPeriodTimingChanged,
486                  void(hal::HWDisplayId, const hal::VsyncPeriodChangeTimeline&));
487     MOCK_METHOD1(onComposerHalSeamlessPossible, void(hal::HWDisplayId));
488     MOCK_METHOD1(onComposerHalVsyncIdle, void(hal::HWDisplayId));
489     MOCK_METHOD(void, onRefreshRateChangedDebug, (const RefreshRateChangedDebugData&), (override));
490     MOCK_METHOD(void, onComposerHalHdcpLevelsChanged, (hal::HWDisplayId, const HdcpLevels&),
491                 (override));
492 };
493 
494 struct HWComposerSetCallbackTest : HWComposerTest {
495     MockHWC2ComposerCallback mCallback;
496 };
497 
TEST_F(HWComposerSetCallbackTest,loadsLayerMetadataSupport)498 TEST_F(HWComposerSetCallbackTest, loadsLayerMetadataSupport) {
499     const std::string kMetadata1Name = "com.example.metadata.1";
500     constexpr bool kMetadata1Mandatory = false;
501     const std::string kMetadata2Name = "com.example.metadata.2";
502     constexpr bool kMetadata2Mandatory = true;
503 
504     EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<aidl::Capability>{}));
505     EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_))
506             .WillOnce(DoAll(SetArgPointee<0>(std::vector<hal::LayerGenericMetadataKey>{
507                                     {kMetadata1Name, kMetadata1Mandatory},
508                                     {kMetadata2Name, kMetadata2Mandatory},
509                             }),
510                             Return(V2_4::Error::NONE)));
511     EXPECT_CALL(*mHal, getOverlaySupport(_)).WillOnce(Return(HalError::NONE));
512     EXPECT_CALL(*mHal, getHdrConversionCapabilities(_)).WillOnce(Return(HalError::NONE));
513 
514     EXPECT_CALL(*mHal, registerCallback(_));
515 
516     mHwc.setCallback(mCallback);
517 
518     const auto& supported = mHwc.getSupportedLayerGenericMetadata();
519     EXPECT_EQ(2u, supported.size());
520     EXPECT_EQ(1u, supported.count(kMetadata1Name));
521     EXPECT_EQ(kMetadata1Mandatory, supported.find(kMetadata1Name)->second);
522     EXPECT_EQ(1u, supported.count(kMetadata2Name));
523     EXPECT_EQ(kMetadata2Mandatory, supported.find(kMetadata2Name)->second);
524 }
525 
TEST_F(HWComposerSetCallbackTest,handlesUnsupportedCallToGetLayerGenericMetadataKeys)526 TEST_F(HWComposerSetCallbackTest, handlesUnsupportedCallToGetLayerGenericMetadataKeys) {
527     EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<aidl::Capability>{}));
528     EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_)).WillOnce(Return(V2_4::Error::UNSUPPORTED));
529     EXPECT_CALL(*mHal, getOverlaySupport(_)).WillOnce(Return(HalError::UNSUPPORTED));
530     EXPECT_CALL(*mHal, getHdrConversionCapabilities(_)).WillOnce(Return(HalError::UNSUPPORTED));
531     EXPECT_CALL(*mHal, registerCallback(_));
532 
533     mHwc.setCallback(mCallback);
534 
535     const auto& supported = mHwc.getSupportedLayerGenericMetadata();
536     EXPECT_TRUE(supported.empty());
537 }
538 
539 struct HWComposerLayerTest : public testing::Test {
540     static constexpr hal::HWDisplayId kDisplayId = static_cast<hal::HWDisplayId>(1001);
541     static constexpr hal::HWLayerId kLayerId = static_cast<hal::HWLayerId>(1002);
542 
HWComposerLayerTestandroid::HWComposerLayerTest543     HWComposerLayerTest(const std::unordered_set<aidl::Capability>& capabilities)
544           : mCapabilies(capabilities) {
545         EXPECT_CALL(mDisplay, getId()).WillRepeatedly(Return(kDisplayId));
546     }
547 
~HWComposerLayerTestandroid::HWComposerLayerTest548     ~HWComposerLayerTest() override {
549         EXPECT_CALL(mDisplay, onLayerDestroyed(kLayerId));
550         EXPECT_CALL(*mHal, destroyLayer(kDisplayId, kLayerId));
551     }
552 
553     std::unique_ptr<Hwc2::mock::Composer> mHal{new StrictMock<Hwc2::mock::Composer>()};
554     const std::unordered_set<aidl::Capability> mCapabilies;
555     StrictMock<HWC2::mock::Display> mDisplay;
556     HWC2::impl::Layer mLayer{*mHal, mCapabilies, mDisplay, kLayerId};
557 };
558 
559 struct HWComposerLayerGenericMetadataTest : public HWComposerLayerTest {
560     static const std::string kLayerGenericMetadata1Name;
561     static constexpr bool kLayerGenericMetadata1Mandatory = false;
562     static const std::vector<uint8_t> kLayerGenericMetadata1Value;
563     static const std::string kLayerGenericMetadata2Name;
564     static constexpr bool kLayerGenericMetadata2Mandatory = true;
565     static const std::vector<uint8_t> kLayerGenericMetadata2Value;
566 
HWComposerLayerGenericMetadataTestandroid::HWComposerLayerGenericMetadataTest567     HWComposerLayerGenericMetadataTest() : HWComposerLayerTest({}) {}
568 };
569 
570 const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Name =
571         "com.example.metadata.1";
572 
573 const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Value = {1u,
574                                                                                               2u,
575                                                                                               3u};
576 
577 const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Name =
578         "com.example.metadata.2";
579 
580 const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Value = {45u,
581                                                                                               67u};
582 
TEST_F(HWComposerLayerGenericMetadataTest,forwardsSupportedMetadata)583 TEST_F(HWComposerLayerGenericMetadataTest, forwardsSupportedMetadata) {
584     EXPECT_CALL(*mHal,
585                 setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata1Name,
586                                         kLayerGenericMetadata1Mandatory,
587                                         kLayerGenericMetadata1Value))
588             .WillOnce(Return(V2_4::Error::NONE));
589     auto result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata1Name,
590                                                  kLayerGenericMetadata1Mandatory,
591                                                  kLayerGenericMetadata1Value);
592     EXPECT_EQ(hal::Error::NONE, result);
593 
594     EXPECT_CALL(*mHal,
595                 setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata2Name,
596                                         kLayerGenericMetadata2Mandatory,
597                                         kLayerGenericMetadata2Value))
598             .WillOnce(Return(V2_4::Error::UNSUPPORTED));
599     result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata2Name,
600                                             kLayerGenericMetadata2Mandatory,
601                                             kLayerGenericMetadata2Value);
602     EXPECT_EQ(hal::Error::UNSUPPORTED, result);
603 }
604 
605 } // namespace android
606