blob: a25804c909ec0dd0a9bdf48a81f609ded1f71340 [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
ramindani355fca82023-12-18 12:07:34 -080024#include <optional>
Lloyd Pique4603f3c2020-02-11 12:06:56 -080025#include <vector>
26
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070027// 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"
Lloyd Pique4603f3c2020-02-11 12:06:56 -080031#include <gmock/gmock.h>
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070032#pragma clang diagnostic pop
33
Alec Mouri9b133ca2023-11-14 19:00:01 +000034#include <common/FlagManager.h>
Lloyd Pique4603f3c2020-02-11 12:06:56 -080035#include <gui/LayerMetadata.h>
36#include <log/log.h>
ramindanib90711b2023-10-02 15:13:19 -070037#include <chrono>
Lloyd Pique4603f3c2020-02-11 12:06:56 -080038
Ady Abraham3db8a3c2023-11-20 17:53:47 -080039#include <common/test/FlagUtils.h>
Marin Shalamanov23c44202020-12-22 19:09:20 +010040#include "DisplayHardware/DisplayMode.h"
Lloyd Pique4603f3c2020-02-11 12:06:56 -080041#include "DisplayHardware/HWComposer.h"
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +010042#include "DisplayHardware/Hal.h"
Alec Mouriff793872022-01-13 17:45:06 -080043#include "DisplayIdentificationTestHelpers.h"
Lloyd Pique4603f3c2020-02-11 12:06:56 -080044#include "mock/DisplayHardware/MockComposer.h"
Lloyd Piquea516c002021-05-07 14:36:58 -070045#include "mock/DisplayHardware/MockHWC2.h"
Lloyd Pique4603f3c2020-02-11 12:06:56 -080046
Sally Qid57eb0d2023-11-07 16:46:15 -080047#include <com_android_graphics_surfaceflinger_flags.h>
48
Lloyd Pique4603f3c2020-02-11 12:06:56 -080049// TODO(b/129481165): remove the #pragma below and fix conversion issues
50#pragma clang diagnostic pop // ignored "-Wconversion"
51
52namespace android {
Lloyd Pique4603f3c2020-02-11 12:06:56 -080053
Marin Shalamanov6e840172020-12-14 22:13:28 +010054namespace V2_1 = hardware::graphics::composer::V2_1;
55namespace V2_4 = hardware::graphics::composer::V2_4;
Ady Abrahamde549d42022-01-26 19:19:17 -080056namespace aidl = aidl::android::hardware::graphics::composer3;
ramindanib90711b2023-10-02 15:13:19 -070057using namespace std::chrono_literals;
Marin Shalamanov6e840172020-12-14 22:13:28 +010058
59using Hwc2::Config;
Peiyong Line9d809e2020-04-14 13:10:48 -070060
Huihong Luo791bef92023-09-19 21:18:45 -070061using ::aidl::android::hardware::graphics::common::DisplayHotplugEvent;
ramindani12bfe6b2023-02-03 13:29:19 -080062using ::aidl::android::hardware::graphics::composer3::RefreshRateChangedDebugData;
ramindani0cd1d8d2023-06-13 13:43:23 -070063using hal::IComposerClient;
Lloyd Pique4603f3c2020-02-11 12:06:56 -080064using ::testing::_;
65using ::testing::DoAll;
Lloyd Pique4603f3c2020-02-11 12:06:56 -080066using ::testing::Return;
67using ::testing::SetArgPointee;
68using ::testing::StrictMock;
69
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070070struct HWComposerTest : testing::Test {
71 using HalError = hardware::graphics::composer::V2_1::Error;
Marin Shalamanov8b196592021-08-09 16:24:42 +020072
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070073 Hwc2::mock::Composer* const mHal = new StrictMock<Hwc2::mock::Composer>();
74 impl::HWComposer mHwc{std::unique_ptr<Hwc2::Composer>(mHal)};
Marin Shalamanov8b196592021-08-09 16:24:42 +020075
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070076 void expectHotplugConnect(hal::HWDisplayId hwcDisplayId) {
77 constexpr uint8_t kPort = 255;
78 EXPECT_CALL(*mHal, getDisplayIdentificationData(hwcDisplayId, _, _))
79 .WillOnce(DoAll(SetArgPointee<1>(kPort),
80 SetArgPointee<2>(getExternalEdid()), Return(HalError::NONE)));
Marin Shalamanov8b196592021-08-09 16:24:42 +020081
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070082 EXPECT_CALL(*mHal, setClientTargetSlotCount(_));
83 EXPECT_CALL(*mHal, setVsyncEnabled(hwcDisplayId, Hwc2::IComposerClient::Vsync::DISABLE));
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -040084 EXPECT_CALL(*mHal, onHotplugConnect(hwcDisplayId));
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070085 }
ramindani355fca82023-12-18 12:07:34 -080086
87 void setVrrTimeoutHint(bool status) { mHwc.mEnableVrrTimeout = status; }
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070088};
Marin Shalamanov8b196592021-08-09 16:24:42 +020089
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070090TEST_F(HWComposerTest, isHeadless) {
91 ASSERT_TRUE(mHwc.isHeadless());
92
93 constexpr hal::HWDisplayId kHwcDisplayId = 1;
94 expectHotplugConnect(kHwcDisplayId);
95
96 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
Marin Shalamanov8b196592021-08-09 16:24:42 +020097 ASSERT_TRUE(info);
Marin Shalamanov8b196592021-08-09 16:24:42 +020098
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070099 ASSERT_FALSE(mHwc.isHeadless());
100
101 mHwc.disconnectDisplay(info->id);
102 ASSERT_TRUE(mHwc.isHeadless());
103}
104
105TEST_F(HWComposerTest, getActiveMode) {
106 // Unknown display.
Dominik Laskowskid940a012024-01-28 13:25:44 -0500107 EXPECT_EQ(mHwc.getActiveMode(PhysicalDisplayId::fromPort(0)), ftl::Unexpected(BAD_INDEX));
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700108
109 constexpr hal::HWDisplayId kHwcDisplayId = 2;
110 expectHotplugConnect(kHwcDisplayId);
111
112 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
113 ASSERT_TRUE(info);
114
115 {
116 // Display is known to SF but not HWC, e.g. the hotplug disconnect is pending.
117 EXPECT_CALL(*mHal, getActiveConfig(kHwcDisplayId, _))
118 .WillOnce(Return(HalError::BAD_DISPLAY));
119
Dominik Laskowskid940a012024-01-28 13:25:44 -0500120 EXPECT_EQ(mHwc.getActiveMode(info->id), ftl::Unexpected(UNKNOWN_ERROR));
121 }
122 {
123 EXPECT_CALL(*mHal, getActiveConfig(kHwcDisplayId, _))
124 .WillOnce(Return(HalError::BAD_CONFIG));
125
126 EXPECT_EQ(mHwc.getActiveMode(info->id), ftl::Unexpected(NO_INIT));
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700127 }
128 {
129 constexpr hal::HWConfigId kConfigId = 42;
130 EXPECT_CALL(*mHal, getActiveConfig(kHwcDisplayId, _))
131 .WillOnce(DoAll(SetArgPointee<1>(kConfigId), Return(HalError::NONE)));
132
Dominik Laskowskid940a012024-01-28 13:25:44 -0500133 EXPECT_EQ(mHwc.getActiveMode(info->id).value_opt(), kConfigId);
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700134 }
Marin Shalamanov8b196592021-08-09 16:24:42 +0200135}
136
ramindani0cd1d8d2023-06-13 13:43:23 -0700137TEST_F(HWComposerTest, getModesWithLegacyDisplayConfigs) {
138 constexpr hal::HWDisplayId kHwcDisplayId = 2;
139 constexpr hal::HWConfigId kConfigId = 42;
ramindani263a3f12023-07-18 20:44:49 -0700140 constexpr int32_t kMaxFrameIntervalNs = 50000000; // 20Fps
ramindani0cd1d8d2023-06-13 13:43:23 -0700141
142 expectHotplugConnect(kHwcDisplayId);
143 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
144 ASSERT_TRUE(info);
145
ramindani19919ff2023-12-07 11:27:06 -0800146 EXPECT_CALL(*mHal, isVrrSupported()).WillRepeatedly(Return(false));
ramindani0cd1d8d2023-06-13 13:43:23 -0700147
148 {
149 EXPECT_CALL(*mHal, getDisplayConfigs(kHwcDisplayId, _))
150 .WillOnce(Return(HalError::BAD_DISPLAY));
ramindani263a3f12023-07-18 20:44:49 -0700151 EXPECT_TRUE(mHwc.getModes(info->id, kMaxFrameIntervalNs).empty());
ramindani0cd1d8d2023-06-13 13:43:23 -0700152 }
153 {
154 constexpr int32_t kWidth = 480;
155 constexpr int32_t kHeight = 720;
156 constexpr int32_t kConfigGroup = 1;
157 constexpr int32_t kVsyncPeriod = 16666667;
158
159 EXPECT_CALL(*mHal,
160 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::WIDTH,
161 _))
162 .WillRepeatedly(DoAll(SetArgPointee<3>(kWidth), Return(HalError::NONE)));
163 EXPECT_CALL(*mHal,
164 getDisplayAttribute(kHwcDisplayId, kConfigId,
165 IComposerClient::Attribute::HEIGHT, _))
166 .WillRepeatedly(DoAll(SetArgPointee<3>(kHeight), Return(HalError::NONE)));
167 EXPECT_CALL(*mHal,
168 getDisplayAttribute(kHwcDisplayId, kConfigId,
169 IComposerClient::Attribute::CONFIG_GROUP, _))
170 .WillRepeatedly(DoAll(SetArgPointee<3>(kConfigGroup), Return(HalError::NONE)));
171 EXPECT_CALL(*mHal,
172 getDisplayAttribute(kHwcDisplayId, kConfigId,
173 IComposerClient::Attribute::VSYNC_PERIOD, _))
174 .WillRepeatedly(DoAll(SetArgPointee<3>(kVsyncPeriod), Return(HalError::NONE)));
175
176 // Optional Parameters UNSUPPORTED
177 EXPECT_CALL(*mHal,
178 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_X,
179 _))
180 .WillOnce(Return(HalError::UNSUPPORTED));
181 EXPECT_CALL(*mHal,
182 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_Y,
183 _))
184 .WillOnce(Return(HalError::UNSUPPORTED));
185
186 EXPECT_CALL(*mHal, getDisplayConfigs(kHwcDisplayId, _))
187 .WillRepeatedly(DoAll(SetArgPointee<1>(std::vector<hal::HWConfigId>{kConfigId}),
188 Return(HalError::NONE)));
189
ramindani263a3f12023-07-18 20:44:49 -0700190 auto modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
ramindani0cd1d8d2023-06-13 13:43:23 -0700191 EXPECT_EQ(modes.size(), size_t{1});
192 EXPECT_EQ(modes.front().hwcId, kConfigId);
193 EXPECT_EQ(modes.front().width, kWidth);
194 EXPECT_EQ(modes.front().height, kHeight);
195 EXPECT_EQ(modes.front().configGroup, kConfigGroup);
196 EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
197 EXPECT_EQ(modes.front().dpiX, -1);
198 EXPECT_EQ(modes.front().dpiY, -1);
199
200 // Optional parameters are supported
201 constexpr int32_t kDpi = 320;
202 EXPECT_CALL(*mHal,
203 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_X,
204 _))
205 .WillOnce(DoAll(SetArgPointee<3>(kDpi), Return(HalError::NONE)));
206 EXPECT_CALL(*mHal,
207 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_Y,
208 _))
209 .WillOnce(DoAll(SetArgPointee<3>(kDpi), Return(HalError::NONE)));
210
ramindani263a3f12023-07-18 20:44:49 -0700211 modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
ramindani0cd1d8d2023-06-13 13:43:23 -0700212 EXPECT_EQ(modes.size(), size_t{1});
213 EXPECT_EQ(modes.front().hwcId, kConfigId);
214 EXPECT_EQ(modes.front().width, kWidth);
215 EXPECT_EQ(modes.front().height, kHeight);
216 EXPECT_EQ(modes.front().configGroup, kConfigGroup);
217 EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
218 // DPI values are scaled by 1000 in the legacy implementation.
219 EXPECT_EQ(modes.front().dpiX, kDpi / 1000.f);
220 EXPECT_EQ(modes.front().dpiY, kDpi / 1000.f);
221 }
222}
223
Sally Qid57eb0d2023-11-07 16:46:15 -0800224TEST_F(HWComposerTest, getModesWithDisplayConfigurations_VRR_OFF) {
225 // if vrr_config is off, getDisplayConfigurationsSupported() is off as well
226 // then getModesWithLegacyDisplayConfigs should be called instead
227 SET_FLAG_FOR_TEST(com::android::graphics::surfaceflinger::flags::vrr_config, false);
228 ASSERT_FALSE(FlagManager::getInstance().vrr_config());
229
230 constexpr hal::HWDisplayId kHwcDisplayId = 2;
231 constexpr hal::HWConfigId kConfigId = 42;
232 constexpr int32_t kMaxFrameIntervalNs = 50000000; // 20Fps
233
234 expectHotplugConnect(kHwcDisplayId);
235 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
236 ASSERT_TRUE(info);
237
ramindani19919ff2023-12-07 11:27:06 -0800238 EXPECT_CALL(*mHal, isVrrSupported()).WillRepeatedly(Return(false));
Sally Qid57eb0d2023-11-07 16:46:15 -0800239
240 {
241 EXPECT_CALL(*mHal, getDisplayConfigs(kHwcDisplayId, _))
242 .WillOnce(Return(HalError::BAD_DISPLAY));
243 EXPECT_TRUE(mHwc.getModes(info->id, kMaxFrameIntervalNs).empty());
244 }
245 {
246 constexpr int32_t kWidth = 480;
247 constexpr int32_t kHeight = 720;
248 constexpr int32_t kConfigGroup = 1;
249 constexpr int32_t kVsyncPeriod = 16666667;
250
251 EXPECT_CALL(*mHal,
252 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::WIDTH,
253 _))
254 .WillRepeatedly(DoAll(SetArgPointee<3>(kWidth), Return(HalError::NONE)));
255 EXPECT_CALL(*mHal,
256 getDisplayAttribute(kHwcDisplayId, kConfigId,
257 IComposerClient::Attribute::HEIGHT, _))
258 .WillRepeatedly(DoAll(SetArgPointee<3>(kHeight), Return(HalError::NONE)));
259 EXPECT_CALL(*mHal,
260 getDisplayAttribute(kHwcDisplayId, kConfigId,
261 IComposerClient::Attribute::CONFIG_GROUP, _))
262 .WillRepeatedly(DoAll(SetArgPointee<3>(kConfigGroup), Return(HalError::NONE)));
263 EXPECT_CALL(*mHal,
264 getDisplayAttribute(kHwcDisplayId, kConfigId,
265 IComposerClient::Attribute::VSYNC_PERIOD, _))
266 .WillRepeatedly(DoAll(SetArgPointee<3>(kVsyncPeriod), Return(HalError::NONE)));
267
268 // Optional Parameters UNSUPPORTED
269 EXPECT_CALL(*mHal,
270 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_X,
271 _))
272 .WillOnce(Return(HalError::UNSUPPORTED));
273 EXPECT_CALL(*mHal,
274 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_Y,
275 _))
276 .WillOnce(Return(HalError::UNSUPPORTED));
277
278 EXPECT_CALL(*mHal, getDisplayConfigs(kHwcDisplayId, _))
279 .WillRepeatedly(DoAll(SetArgPointee<1>(std::vector<hal::HWConfigId>{kConfigId}),
280 Return(HalError::NONE)));
281
282 auto modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
283 EXPECT_EQ(modes.size(), size_t{1});
284 EXPECT_EQ(modes.front().hwcId, kConfigId);
285 EXPECT_EQ(modes.front().width, kWidth);
286 EXPECT_EQ(modes.front().height, kHeight);
287 EXPECT_EQ(modes.front().configGroup, kConfigGroup);
288 EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
289 EXPECT_EQ(modes.front().dpiX, -1);
290 EXPECT_EQ(modes.front().dpiY, -1);
291
292 // Optional parameters are supported
293 constexpr int32_t kDpi = 320;
294 EXPECT_CALL(*mHal,
295 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_X,
296 _))
297 .WillOnce(DoAll(SetArgPointee<3>(kDpi), Return(HalError::NONE)));
298 EXPECT_CALL(*mHal,
299 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_Y,
300 _))
301 .WillOnce(DoAll(SetArgPointee<3>(kDpi), Return(HalError::NONE)));
302
303 modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
304 EXPECT_EQ(modes.size(), size_t{1});
305 EXPECT_EQ(modes.front().hwcId, kConfigId);
306 EXPECT_EQ(modes.front().width, kWidth);
307 EXPECT_EQ(modes.front().height, kHeight);
308 EXPECT_EQ(modes.front().configGroup, kConfigGroup);
309 EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
310 // DPI values are scaled by 1000 in the legacy implementation.
311 EXPECT_EQ(modes.front().dpiX, kDpi / 1000.f);
312 EXPECT_EQ(modes.front().dpiY, kDpi / 1000.f);
313 }
314}
315
316TEST_F(HWComposerTest, getModesWithDisplayConfigurations_VRR_ON) {
317 SET_FLAG_FOR_TEST(com::android::graphics::surfaceflinger::flags::vrr_config, true);
318 ASSERT_TRUE(FlagManager::getInstance().vrr_config());
319
ramindani0cd1d8d2023-06-13 13:43:23 -0700320 constexpr hal::HWDisplayId kHwcDisplayId = 2;
321 constexpr hal::HWConfigId kConfigId = 42;
ramindani263a3f12023-07-18 20:44:49 -0700322 constexpr int32_t kMaxFrameIntervalNs = 50000000; // 20Fps
ramindani0cd1d8d2023-06-13 13:43:23 -0700323 expectHotplugConnect(kHwcDisplayId);
324 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
325 ASSERT_TRUE(info);
326
ramindani19919ff2023-12-07 11:27:06 -0800327 EXPECT_CALL(*mHal, isVrrSupported()).WillRepeatedly(Return(true));
ramindani0cd1d8d2023-06-13 13:43:23 -0700328
329 {
ramindani263a3f12023-07-18 20:44:49 -0700330 EXPECT_CALL(*mHal, getDisplayConfigurations(kHwcDisplayId, _, _))
ramindani0cd1d8d2023-06-13 13:43:23 -0700331 .WillOnce(Return(HalError::BAD_DISPLAY));
ramindani263a3f12023-07-18 20:44:49 -0700332 EXPECT_TRUE(mHwc.getModes(info->id, kMaxFrameIntervalNs).empty());
ramindani0cd1d8d2023-06-13 13:43:23 -0700333 }
334 {
ramindani355fca82023-12-18 12:07:34 -0800335 setVrrTimeoutHint(true);
ramindani0cd1d8d2023-06-13 13:43:23 -0700336 constexpr int32_t kWidth = 480;
337 constexpr int32_t kHeight = 720;
338 constexpr int32_t kConfigGroup = 1;
339 constexpr int32_t kVsyncPeriod = 16666667;
ramindanib90711b2023-10-02 15:13:19 -0700340 const hal::VrrConfig vrrConfig =
341 hal::VrrConfig{.minFrameIntervalNs = static_cast<Fps>(120_Hz).getPeriodNsecs(),
342 .notifyExpectedPresentConfig = hal::VrrConfig::
ramindanicbd7a6d2023-12-19 16:00:30 -0800343 NotifyExpectedPresentConfig{.headsUpNs = ms2ns(30),
344 .timeoutNs = ms2ns(30)}};
ramindanib90711b2023-10-02 15:13:19 -0700345 hal::DisplayConfiguration displayConfiguration{.configId = kConfigId,
346 .width = kWidth,
347 .height = kHeight,
348 .configGroup = kConfigGroup,
349 .vsyncPeriod = kVsyncPeriod,
350 .vrrConfig = vrrConfig};
ramindani0cd1d8d2023-06-13 13:43:23 -0700351
ramindani263a3f12023-07-18 20:44:49 -0700352 EXPECT_CALL(*mHal, getDisplayConfigurations(kHwcDisplayId, _, _))
353 .WillOnce(DoAll(SetArgPointee<2>(std::vector<hal::DisplayConfiguration>{
ramindani0cd1d8d2023-06-13 13:43:23 -0700354 displayConfiguration}),
355 Return(HalError::NONE)));
356
357 // Optional dpi not supported
ramindani263a3f12023-07-18 20:44:49 -0700358 auto modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
ramindani0cd1d8d2023-06-13 13:43:23 -0700359 EXPECT_EQ(modes.size(), size_t{1});
360 EXPECT_EQ(modes.front().hwcId, kConfigId);
361 EXPECT_EQ(modes.front().width, kWidth);
362 EXPECT_EQ(modes.front().height, kHeight);
363 EXPECT_EQ(modes.front().configGroup, kConfigGroup);
364 EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
ramindanib90711b2023-10-02 15:13:19 -0700365 EXPECT_EQ(modes.front().vrrConfig, vrrConfig);
ramindani0cd1d8d2023-06-13 13:43:23 -0700366 EXPECT_EQ(modes.front().dpiX, -1);
367 EXPECT_EQ(modes.front().dpiY, -1);
368
369 // Supports optional dpi parameter
370 constexpr int32_t kDpi = 320;
371 displayConfiguration.dpi = {kDpi, kDpi};
372
ramindani263a3f12023-07-18 20:44:49 -0700373 EXPECT_CALL(*mHal, getDisplayConfigurations(kHwcDisplayId, _, _))
ramindani355fca82023-12-18 12:07:34 -0800374 .WillRepeatedly(DoAll(SetArgPointee<2>(std::vector<hal::DisplayConfiguration>{
375 displayConfiguration}),
376 Return(HalError::NONE)));
ramindani0cd1d8d2023-06-13 13:43:23 -0700377
ramindani263a3f12023-07-18 20:44:49 -0700378 modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
ramindani0cd1d8d2023-06-13 13:43:23 -0700379 EXPECT_EQ(modes.size(), size_t{1});
380 EXPECT_EQ(modes.front().hwcId, kConfigId);
381 EXPECT_EQ(modes.front().width, kWidth);
382 EXPECT_EQ(modes.front().height, kHeight);
383 EXPECT_EQ(modes.front().configGroup, kConfigGroup);
384 EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
ramindanib90711b2023-10-02 15:13:19 -0700385 EXPECT_EQ(modes.front().vrrConfig, vrrConfig);
ramindani0cd1d8d2023-06-13 13:43:23 -0700386 EXPECT_EQ(modes.front().dpiX, kDpi);
387 EXPECT_EQ(modes.front().dpiY, kDpi);
ramindani355fca82023-12-18 12:07:34 -0800388
389 setVrrTimeoutHint(false);
390 modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
391 EXPECT_EQ(modes.front().vrrConfig->notifyExpectedPresentConfig, std::nullopt);
ramindani0cd1d8d2023-06-13 13:43:23 -0700392 }
393}
394
Leon Scroggins III959a7ff2023-02-07 11:24:25 -0500395TEST_F(HWComposerTest, onVsync) {
396 constexpr hal::HWDisplayId kHwcDisplayId = 1;
397 expectHotplugConnect(kHwcDisplayId);
398
399 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
400 ASSERT_TRUE(info);
401
402 const auto physicalDisplayId = info->id;
403
404 // Deliberately chosen not to match DisplayData.lastPresentTimestamp's
405 // initial value.
406 constexpr nsecs_t kTimestamp = 1;
407 auto displayIdOpt = mHwc.onVsync(kHwcDisplayId, kTimestamp);
408 ASSERT_TRUE(displayIdOpt);
409 EXPECT_EQ(physicalDisplayId, displayIdOpt);
410
411 // Attempt to send the same time stamp again.
412 displayIdOpt = mHwc.onVsync(kHwcDisplayId, kTimestamp);
413 EXPECT_FALSE(displayIdOpt);
414}
415
416TEST_F(HWComposerTest, onVsyncInvalid) {
417 constexpr hal::HWDisplayId kInvalidHwcDisplayId = 2;
418 constexpr nsecs_t kTimestamp = 1;
419 const auto displayIdOpt = mHwc.onVsync(kInvalidHwcDisplayId, kTimestamp);
420 EXPECT_FALSE(displayIdOpt);
421}
422
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700423struct MockHWC2ComposerCallback final : StrictMock<HWC2::ComposerCallback> {
Huihong Luo791bef92023-09-19 21:18:45 -0700424 MOCK_METHOD(void, onComposerHalHotplugEvent, (hal::HWDisplayId, DisplayHotplugEvent),
425 (override));
Dominik Laskowski0deb06e2021-04-16 23:18:31 -0700426 MOCK_METHOD1(onComposerHalRefresh, void(hal::HWDisplayId));
427 MOCK_METHOD3(onComposerHalVsync,
428 void(hal::HWDisplayId, int64_t timestamp, std::optional<hal::VsyncPeriodNanos>));
429 MOCK_METHOD2(onComposerHalVsyncPeriodTimingChanged,
430 void(hal::HWDisplayId, const hal::VsyncPeriodChangeTimeline&));
431 MOCK_METHOD1(onComposerHalSeamlessPossible, void(hal::HWDisplayId));
Yichi Chen1a417af2022-01-21 15:29:52 +0800432 MOCK_METHOD1(onComposerHalVsyncIdle, void(hal::HWDisplayId));
ramindani12bfe6b2023-02-03 13:29:19 -0800433 MOCK_METHOD(void, onRefreshRateChangedDebug, (const RefreshRateChangedDebugData&), (override));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800434};
435
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700436struct HWComposerSetCallbackTest : HWComposerTest {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700437 MockHWC2ComposerCallback mCallback;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800438};
439
Dominik Laskowski0deb06e2021-04-16 23:18:31 -0700440TEST_F(HWComposerSetCallbackTest, loadsLayerMetadataSupport) {
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800441 const std::string kMetadata1Name = "com.example.metadata.1";
442 constexpr bool kMetadata1Mandatory = false;
443 const std::string kMetadata2Name = "com.example.metadata.2";
444 constexpr bool kMetadata2Mandatory = true;
445
Ady Abrahamde549d42022-01-26 19:19:17 -0800446 EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<aidl::Capability>{}));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800447 EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_))
Peiyong Line9d809e2020-04-14 13:10:48 -0700448 .WillOnce(DoAll(SetArgPointee<0>(std::vector<hal::LayerGenericMetadataKey>{
449 {kMetadata1Name, kMetadata1Mandatory},
450 {kMetadata2Name, kMetadata2Mandatory},
451 }),
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800452 Return(hardware::graphics::composer::V2_4::Error::NONE)));
Sally Qibb866c12022-10-17 11:31:20 -0700453 EXPECT_CALL(*mHal, getOverlaySupport(_)).WillOnce(Return(HalError::NONE));
Kriti Dang674b9372022-11-18 10:58:44 +0100454 EXPECT_CALL(*mHal, getHdrConversionCapabilities(_)).WillOnce(Return(HalError::NONE));
Sally Qibb866c12022-10-17 11:31:20 -0700455
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800456 EXPECT_CALL(*mHal, registerCallback(_));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800457
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700458 mHwc.setCallback(mCallback);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800459
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700460 const auto& supported = mHwc.getSupportedLayerGenericMetadata();
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800461 EXPECT_EQ(2u, supported.size());
462 EXPECT_EQ(1u, supported.count(kMetadata1Name));
463 EXPECT_EQ(kMetadata1Mandatory, supported.find(kMetadata1Name)->second);
464 EXPECT_EQ(1u, supported.count(kMetadata2Name));
465 EXPECT_EQ(kMetadata2Mandatory, supported.find(kMetadata2Name)->second);
466}
467
Dominik Laskowski0deb06e2021-04-16 23:18:31 -0700468TEST_F(HWComposerSetCallbackTest, handlesUnsupportedCallToGetLayerGenericMetadataKeys) {
Ady Abrahamde549d42022-01-26 19:19:17 -0800469 EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<aidl::Capability>{}));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800470 EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_))
471 .WillOnce(Return(hardware::graphics::composer::V2_4::Error::UNSUPPORTED));
Sally Qibb866c12022-10-17 11:31:20 -0700472 EXPECT_CALL(*mHal, getOverlaySupport(_)).WillOnce(Return(HalError::UNSUPPORTED));
Kriti Dang674b9372022-11-18 10:58:44 +0100473 EXPECT_CALL(*mHal, getHdrConversionCapabilities(_)).WillOnce(Return(HalError::UNSUPPORTED));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800474 EXPECT_CALL(*mHal, registerCallback(_));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800475
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700476 mHwc.setCallback(mCallback);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800477
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700478 const auto& supported = mHwc.getSupportedLayerGenericMetadata();
479 EXPECT_TRUE(supported.empty());
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800480}
481
482struct HWComposerLayerTest : public testing::Test {
Peiyong Line9d809e2020-04-14 13:10:48 -0700483 static constexpr hal::HWDisplayId kDisplayId = static_cast<hal::HWDisplayId>(1001);
484 static constexpr hal::HWLayerId kLayerId = static_cast<hal::HWLayerId>(1002);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800485
Ady Abrahamde549d42022-01-26 19:19:17 -0800486 HWComposerLayerTest(const std::unordered_set<aidl::Capability>& capabilities)
Lloyd Piquea516c002021-05-07 14:36:58 -0700487 : mCapabilies(capabilities) {
488 EXPECT_CALL(mDisplay, getId()).WillRepeatedly(Return(kDisplayId));
489 }
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800490
Lloyd Piquea516c002021-05-07 14:36:58 -0700491 ~HWComposerLayerTest() override {
492 EXPECT_CALL(mDisplay, onLayerDestroyed(kLayerId));
493 EXPECT_CALL(*mHal, destroyLayer(kDisplayId, kLayerId));
494 }
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800495
496 std::unique_ptr<Hwc2::mock::Composer> mHal{new StrictMock<Hwc2::mock::Composer>()};
Ady Abrahamde549d42022-01-26 19:19:17 -0800497 const std::unordered_set<aidl::Capability> mCapabilies;
Lloyd Piquea516c002021-05-07 14:36:58 -0700498 StrictMock<HWC2::mock::Display> mDisplay;
499 HWC2::impl::Layer mLayer{*mHal, mCapabilies, mDisplay, kLayerId};
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800500};
501
502struct HWComposerLayerGenericMetadataTest : public HWComposerLayerTest {
503 static const std::string kLayerGenericMetadata1Name;
504 static constexpr bool kLayerGenericMetadata1Mandatory = false;
505 static const std::vector<uint8_t> kLayerGenericMetadata1Value;
506 static const std::string kLayerGenericMetadata2Name;
507 static constexpr bool kLayerGenericMetadata2Mandatory = true;
508 static const std::vector<uint8_t> kLayerGenericMetadata2Value;
509
510 HWComposerLayerGenericMetadataTest() : HWComposerLayerTest({}) {}
511};
512
513const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Name =
514 "com.example.metadata.1";
515
516const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Value = {1u,
517 2u,
518 3u};
519
520const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Name =
521 "com.example.metadata.2";
522
523const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Value = {45u,
524 67u};
525
526TEST_F(HWComposerLayerGenericMetadataTest, forwardsSupportedMetadata) {
527 EXPECT_CALL(*mHal,
528 setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata1Name,
529 kLayerGenericMetadata1Mandatory,
530 kLayerGenericMetadata1Value))
531 .WillOnce(Return(hardware::graphics::composer::V2_4::Error::NONE));
532 auto result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata1Name,
533 kLayerGenericMetadata1Mandatory,
534 kLayerGenericMetadata1Value);
Peiyong Line9d809e2020-04-14 13:10:48 -0700535 EXPECT_EQ(hal::Error::NONE, result);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800536
537 EXPECT_CALL(*mHal,
538 setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata2Name,
539 kLayerGenericMetadata2Mandatory,
540 kLayerGenericMetadata2Value))
541 .WillOnce(Return(hardware::graphics::composer::V2_4::Error::UNSUPPORTED));
542 result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata2Name,
543 kLayerGenericMetadata2Mandatory,
544 kLayerGenericMetadata2Value);
Peiyong Line9d809e2020-04-14 13:10:48 -0700545 EXPECT_EQ(hal::Error::UNSUPPORTED, result);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800546}
547
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800548} // namespace android