blob: 2cff2f29294f1d29123d41d2771e7e3f3b4613d4 [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
Dominik Laskowski969cdcb2024-02-08 16:35:29 -0500105TEST_F(HWComposerTest, getDisplayConnectionType) {
106 // Unknown display.
107 EXPECT_EQ(mHwc.getDisplayConnectionType(PhysicalDisplayId::fromPort(0)),
108 ui::DisplayConnectionType::Internal);
109
110 constexpr hal::HWDisplayId kHwcDisplayId = 1;
111 expectHotplugConnect(kHwcDisplayId);
112
113 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
114 ASSERT_TRUE(info);
115
116 EXPECT_CALL(*mHal, getDisplayConnectionType(kHwcDisplayId, _))
117 .WillOnce(DoAll(SetArgPointee<1>(IComposerClient::DisplayConnectionType::EXTERNAL),
118 Return(V2_4::Error::NONE)));
119
120 // The first call caches the connection type.
121 EXPECT_EQ(mHwc.getDisplayConnectionType(info->id), ui::DisplayConnectionType::External);
122
123 // Subsequent calls return the cached connection type.
124 EXPECT_EQ(mHwc.getDisplayConnectionType(info->id), ui::DisplayConnectionType::External);
125 EXPECT_EQ(mHwc.getDisplayConnectionType(info->id), ui::DisplayConnectionType::External);
126}
127
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700128TEST_F(HWComposerTest, getActiveMode) {
129 // Unknown display.
Dominik Laskowskid940a012024-01-28 13:25:44 -0500130 EXPECT_EQ(mHwc.getActiveMode(PhysicalDisplayId::fromPort(0)), ftl::Unexpected(BAD_INDEX));
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700131
132 constexpr hal::HWDisplayId kHwcDisplayId = 2;
133 expectHotplugConnect(kHwcDisplayId);
134
135 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
136 ASSERT_TRUE(info);
137
138 {
139 // Display is known to SF but not HWC, e.g. the hotplug disconnect is pending.
140 EXPECT_CALL(*mHal, getActiveConfig(kHwcDisplayId, _))
141 .WillOnce(Return(HalError::BAD_DISPLAY));
142
Dominik Laskowskid940a012024-01-28 13:25:44 -0500143 EXPECT_EQ(mHwc.getActiveMode(info->id), ftl::Unexpected(UNKNOWN_ERROR));
144 }
145 {
146 EXPECT_CALL(*mHal, getActiveConfig(kHwcDisplayId, _))
147 .WillOnce(Return(HalError::BAD_CONFIG));
148
149 EXPECT_EQ(mHwc.getActiveMode(info->id), ftl::Unexpected(NO_INIT));
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700150 }
151 {
152 constexpr hal::HWConfigId kConfigId = 42;
153 EXPECT_CALL(*mHal, getActiveConfig(kHwcDisplayId, _))
154 .WillOnce(DoAll(SetArgPointee<1>(kConfigId), Return(HalError::NONE)));
155
Dominik Laskowskid940a012024-01-28 13:25:44 -0500156 EXPECT_EQ(mHwc.getActiveMode(info->id).value_opt(), kConfigId);
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700157 }
Marin Shalamanov8b196592021-08-09 16:24:42 +0200158}
159
ramindani0cd1d8d2023-06-13 13:43:23 -0700160TEST_F(HWComposerTest, getModesWithLegacyDisplayConfigs) {
161 constexpr hal::HWDisplayId kHwcDisplayId = 2;
162 constexpr hal::HWConfigId kConfigId = 42;
ramindani263a3f12023-07-18 20:44:49 -0700163 constexpr int32_t kMaxFrameIntervalNs = 50000000; // 20Fps
ramindani0cd1d8d2023-06-13 13:43:23 -0700164
165 expectHotplugConnect(kHwcDisplayId);
166 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
167 ASSERT_TRUE(info);
168
ramindani19919ff2023-12-07 11:27:06 -0800169 EXPECT_CALL(*mHal, isVrrSupported()).WillRepeatedly(Return(false));
ramindani0cd1d8d2023-06-13 13:43:23 -0700170
171 {
172 EXPECT_CALL(*mHal, getDisplayConfigs(kHwcDisplayId, _))
173 .WillOnce(Return(HalError::BAD_DISPLAY));
ramindani263a3f12023-07-18 20:44:49 -0700174 EXPECT_TRUE(mHwc.getModes(info->id, kMaxFrameIntervalNs).empty());
ramindani0cd1d8d2023-06-13 13:43:23 -0700175 }
176 {
177 constexpr int32_t kWidth = 480;
178 constexpr int32_t kHeight = 720;
179 constexpr int32_t kConfigGroup = 1;
180 constexpr int32_t kVsyncPeriod = 16666667;
181
182 EXPECT_CALL(*mHal,
183 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::WIDTH,
184 _))
185 .WillRepeatedly(DoAll(SetArgPointee<3>(kWidth), Return(HalError::NONE)));
186 EXPECT_CALL(*mHal,
187 getDisplayAttribute(kHwcDisplayId, kConfigId,
188 IComposerClient::Attribute::HEIGHT, _))
189 .WillRepeatedly(DoAll(SetArgPointee<3>(kHeight), Return(HalError::NONE)));
190 EXPECT_CALL(*mHal,
191 getDisplayAttribute(kHwcDisplayId, kConfigId,
192 IComposerClient::Attribute::CONFIG_GROUP, _))
193 .WillRepeatedly(DoAll(SetArgPointee<3>(kConfigGroup), Return(HalError::NONE)));
194 EXPECT_CALL(*mHal,
195 getDisplayAttribute(kHwcDisplayId, kConfigId,
196 IComposerClient::Attribute::VSYNC_PERIOD, _))
197 .WillRepeatedly(DoAll(SetArgPointee<3>(kVsyncPeriod), Return(HalError::NONE)));
198
199 // Optional Parameters UNSUPPORTED
200 EXPECT_CALL(*mHal,
201 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_X,
202 _))
203 .WillOnce(Return(HalError::UNSUPPORTED));
204 EXPECT_CALL(*mHal,
205 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_Y,
206 _))
207 .WillOnce(Return(HalError::UNSUPPORTED));
208
209 EXPECT_CALL(*mHal, getDisplayConfigs(kHwcDisplayId, _))
210 .WillRepeatedly(DoAll(SetArgPointee<1>(std::vector<hal::HWConfigId>{kConfigId}),
211 Return(HalError::NONE)));
212
ramindani263a3f12023-07-18 20:44:49 -0700213 auto modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
ramindani0cd1d8d2023-06-13 13:43:23 -0700214 EXPECT_EQ(modes.size(), size_t{1});
215 EXPECT_EQ(modes.front().hwcId, kConfigId);
216 EXPECT_EQ(modes.front().width, kWidth);
217 EXPECT_EQ(modes.front().height, kHeight);
218 EXPECT_EQ(modes.front().configGroup, kConfigGroup);
219 EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
220 EXPECT_EQ(modes.front().dpiX, -1);
221 EXPECT_EQ(modes.front().dpiY, -1);
222
223 // Optional parameters are supported
224 constexpr int32_t kDpi = 320;
225 EXPECT_CALL(*mHal,
226 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_X,
227 _))
228 .WillOnce(DoAll(SetArgPointee<3>(kDpi), Return(HalError::NONE)));
229 EXPECT_CALL(*mHal,
230 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_Y,
231 _))
232 .WillOnce(DoAll(SetArgPointee<3>(kDpi), Return(HalError::NONE)));
233
ramindani263a3f12023-07-18 20:44:49 -0700234 modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
ramindani0cd1d8d2023-06-13 13:43:23 -0700235 EXPECT_EQ(modes.size(), size_t{1});
236 EXPECT_EQ(modes.front().hwcId, kConfigId);
237 EXPECT_EQ(modes.front().width, kWidth);
238 EXPECT_EQ(modes.front().height, kHeight);
239 EXPECT_EQ(modes.front().configGroup, kConfigGroup);
240 EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
241 // DPI values are scaled by 1000 in the legacy implementation.
242 EXPECT_EQ(modes.front().dpiX, kDpi / 1000.f);
243 EXPECT_EQ(modes.front().dpiY, kDpi / 1000.f);
244 }
245}
246
Sally Qid57eb0d2023-11-07 16:46:15 -0800247TEST_F(HWComposerTest, getModesWithDisplayConfigurations_VRR_OFF) {
248 // if vrr_config is off, getDisplayConfigurationsSupported() is off as well
249 // then getModesWithLegacyDisplayConfigs should be called instead
250 SET_FLAG_FOR_TEST(com::android::graphics::surfaceflinger::flags::vrr_config, false);
251 ASSERT_FALSE(FlagManager::getInstance().vrr_config());
252
253 constexpr hal::HWDisplayId kHwcDisplayId = 2;
254 constexpr hal::HWConfigId kConfigId = 42;
255 constexpr int32_t kMaxFrameIntervalNs = 50000000; // 20Fps
256
257 expectHotplugConnect(kHwcDisplayId);
258 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
259 ASSERT_TRUE(info);
260
ramindani19919ff2023-12-07 11:27:06 -0800261 EXPECT_CALL(*mHal, isVrrSupported()).WillRepeatedly(Return(false));
Sally Qid57eb0d2023-11-07 16:46:15 -0800262
263 {
264 EXPECT_CALL(*mHal, getDisplayConfigs(kHwcDisplayId, _))
265 .WillOnce(Return(HalError::BAD_DISPLAY));
266 EXPECT_TRUE(mHwc.getModes(info->id, kMaxFrameIntervalNs).empty());
267 }
268 {
269 constexpr int32_t kWidth = 480;
270 constexpr int32_t kHeight = 720;
271 constexpr int32_t kConfigGroup = 1;
272 constexpr int32_t kVsyncPeriod = 16666667;
273
274 EXPECT_CALL(*mHal,
275 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::WIDTH,
276 _))
277 .WillRepeatedly(DoAll(SetArgPointee<3>(kWidth), Return(HalError::NONE)));
278 EXPECT_CALL(*mHal,
279 getDisplayAttribute(kHwcDisplayId, kConfigId,
280 IComposerClient::Attribute::HEIGHT, _))
281 .WillRepeatedly(DoAll(SetArgPointee<3>(kHeight), Return(HalError::NONE)));
282 EXPECT_CALL(*mHal,
283 getDisplayAttribute(kHwcDisplayId, kConfigId,
284 IComposerClient::Attribute::CONFIG_GROUP, _))
285 .WillRepeatedly(DoAll(SetArgPointee<3>(kConfigGroup), Return(HalError::NONE)));
286 EXPECT_CALL(*mHal,
287 getDisplayAttribute(kHwcDisplayId, kConfigId,
288 IComposerClient::Attribute::VSYNC_PERIOD, _))
289 .WillRepeatedly(DoAll(SetArgPointee<3>(kVsyncPeriod), Return(HalError::NONE)));
290
291 // Optional Parameters UNSUPPORTED
292 EXPECT_CALL(*mHal,
293 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_X,
294 _))
295 .WillOnce(Return(HalError::UNSUPPORTED));
296 EXPECT_CALL(*mHal,
297 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_Y,
298 _))
299 .WillOnce(Return(HalError::UNSUPPORTED));
300
301 EXPECT_CALL(*mHal, getDisplayConfigs(kHwcDisplayId, _))
302 .WillRepeatedly(DoAll(SetArgPointee<1>(std::vector<hal::HWConfigId>{kConfigId}),
303 Return(HalError::NONE)));
304
305 auto modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
306 EXPECT_EQ(modes.size(), size_t{1});
307 EXPECT_EQ(modes.front().hwcId, kConfigId);
308 EXPECT_EQ(modes.front().width, kWidth);
309 EXPECT_EQ(modes.front().height, kHeight);
310 EXPECT_EQ(modes.front().configGroup, kConfigGroup);
311 EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
312 EXPECT_EQ(modes.front().dpiX, -1);
313 EXPECT_EQ(modes.front().dpiY, -1);
314
315 // Optional parameters are supported
316 constexpr int32_t kDpi = 320;
317 EXPECT_CALL(*mHal,
318 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_X,
319 _))
320 .WillOnce(DoAll(SetArgPointee<3>(kDpi), Return(HalError::NONE)));
321 EXPECT_CALL(*mHal,
322 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_Y,
323 _))
324 .WillOnce(DoAll(SetArgPointee<3>(kDpi), Return(HalError::NONE)));
325
326 modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
327 EXPECT_EQ(modes.size(), size_t{1});
328 EXPECT_EQ(modes.front().hwcId, kConfigId);
329 EXPECT_EQ(modes.front().width, kWidth);
330 EXPECT_EQ(modes.front().height, kHeight);
331 EXPECT_EQ(modes.front().configGroup, kConfigGroup);
332 EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
333 // DPI values are scaled by 1000 in the legacy implementation.
334 EXPECT_EQ(modes.front().dpiX, kDpi / 1000.f);
335 EXPECT_EQ(modes.front().dpiY, kDpi / 1000.f);
336 }
337}
338
339TEST_F(HWComposerTest, getModesWithDisplayConfigurations_VRR_ON) {
340 SET_FLAG_FOR_TEST(com::android::graphics::surfaceflinger::flags::vrr_config, true);
341 ASSERT_TRUE(FlagManager::getInstance().vrr_config());
342
ramindani0cd1d8d2023-06-13 13:43:23 -0700343 constexpr hal::HWDisplayId kHwcDisplayId = 2;
344 constexpr hal::HWConfigId kConfigId = 42;
ramindani263a3f12023-07-18 20:44:49 -0700345 constexpr int32_t kMaxFrameIntervalNs = 50000000; // 20Fps
ramindani0cd1d8d2023-06-13 13:43:23 -0700346 expectHotplugConnect(kHwcDisplayId);
347 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
348 ASSERT_TRUE(info);
349
ramindani19919ff2023-12-07 11:27:06 -0800350 EXPECT_CALL(*mHal, isVrrSupported()).WillRepeatedly(Return(true));
ramindani0cd1d8d2023-06-13 13:43:23 -0700351
352 {
ramindani263a3f12023-07-18 20:44:49 -0700353 EXPECT_CALL(*mHal, getDisplayConfigurations(kHwcDisplayId, _, _))
ramindani0cd1d8d2023-06-13 13:43:23 -0700354 .WillOnce(Return(HalError::BAD_DISPLAY));
ramindani263a3f12023-07-18 20:44:49 -0700355 EXPECT_TRUE(mHwc.getModes(info->id, kMaxFrameIntervalNs).empty());
ramindani0cd1d8d2023-06-13 13:43:23 -0700356 }
357 {
ramindani355fca82023-12-18 12:07:34 -0800358 setVrrTimeoutHint(true);
ramindani0cd1d8d2023-06-13 13:43:23 -0700359 constexpr int32_t kWidth = 480;
360 constexpr int32_t kHeight = 720;
361 constexpr int32_t kConfigGroup = 1;
362 constexpr int32_t kVsyncPeriod = 16666667;
ramindanib90711b2023-10-02 15:13:19 -0700363 const hal::VrrConfig vrrConfig =
364 hal::VrrConfig{.minFrameIntervalNs = static_cast<Fps>(120_Hz).getPeriodNsecs(),
365 .notifyExpectedPresentConfig = hal::VrrConfig::
ramindanicbd7a6d2023-12-19 16:00:30 -0800366 NotifyExpectedPresentConfig{.headsUpNs = ms2ns(30),
367 .timeoutNs = ms2ns(30)}};
ramindanib90711b2023-10-02 15:13:19 -0700368 hal::DisplayConfiguration displayConfiguration{.configId = kConfigId,
369 .width = kWidth,
370 .height = kHeight,
371 .configGroup = kConfigGroup,
372 .vsyncPeriod = kVsyncPeriod,
373 .vrrConfig = vrrConfig};
ramindani0cd1d8d2023-06-13 13:43:23 -0700374
ramindani263a3f12023-07-18 20:44:49 -0700375 EXPECT_CALL(*mHal, getDisplayConfigurations(kHwcDisplayId, _, _))
376 .WillOnce(DoAll(SetArgPointee<2>(std::vector<hal::DisplayConfiguration>{
ramindani0cd1d8d2023-06-13 13:43:23 -0700377 displayConfiguration}),
378 Return(HalError::NONE)));
379
380 // Optional dpi not supported
ramindani263a3f12023-07-18 20:44:49 -0700381 auto modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
ramindani0cd1d8d2023-06-13 13:43:23 -0700382 EXPECT_EQ(modes.size(), size_t{1});
383 EXPECT_EQ(modes.front().hwcId, kConfigId);
384 EXPECT_EQ(modes.front().width, kWidth);
385 EXPECT_EQ(modes.front().height, kHeight);
386 EXPECT_EQ(modes.front().configGroup, kConfigGroup);
387 EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
ramindanib90711b2023-10-02 15:13:19 -0700388 EXPECT_EQ(modes.front().vrrConfig, vrrConfig);
ramindani0cd1d8d2023-06-13 13:43:23 -0700389 EXPECT_EQ(modes.front().dpiX, -1);
390 EXPECT_EQ(modes.front().dpiY, -1);
391
392 // Supports optional dpi parameter
393 constexpr int32_t kDpi = 320;
394 displayConfiguration.dpi = {kDpi, kDpi};
395
ramindani263a3f12023-07-18 20:44:49 -0700396 EXPECT_CALL(*mHal, getDisplayConfigurations(kHwcDisplayId, _, _))
ramindani355fca82023-12-18 12:07:34 -0800397 .WillRepeatedly(DoAll(SetArgPointee<2>(std::vector<hal::DisplayConfiguration>{
398 displayConfiguration}),
399 Return(HalError::NONE)));
ramindani0cd1d8d2023-06-13 13:43:23 -0700400
ramindani263a3f12023-07-18 20:44:49 -0700401 modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
ramindani0cd1d8d2023-06-13 13:43:23 -0700402 EXPECT_EQ(modes.size(), size_t{1});
403 EXPECT_EQ(modes.front().hwcId, kConfigId);
404 EXPECT_EQ(modes.front().width, kWidth);
405 EXPECT_EQ(modes.front().height, kHeight);
406 EXPECT_EQ(modes.front().configGroup, kConfigGroup);
407 EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
ramindanib90711b2023-10-02 15:13:19 -0700408 EXPECT_EQ(modes.front().vrrConfig, vrrConfig);
ramindani0cd1d8d2023-06-13 13:43:23 -0700409 EXPECT_EQ(modes.front().dpiX, kDpi);
410 EXPECT_EQ(modes.front().dpiY, kDpi);
ramindani355fca82023-12-18 12:07:34 -0800411
412 setVrrTimeoutHint(false);
413 modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
414 EXPECT_EQ(modes.front().vrrConfig->notifyExpectedPresentConfig, std::nullopt);
ramindani0cd1d8d2023-06-13 13:43:23 -0700415 }
416}
417
Leon Scroggins III959a7ff2023-02-07 11:24:25 -0500418TEST_F(HWComposerTest, onVsync) {
419 constexpr hal::HWDisplayId kHwcDisplayId = 1;
420 expectHotplugConnect(kHwcDisplayId);
421
422 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
423 ASSERT_TRUE(info);
424
425 const auto physicalDisplayId = info->id;
426
427 // Deliberately chosen not to match DisplayData.lastPresentTimestamp's
428 // initial value.
429 constexpr nsecs_t kTimestamp = 1;
430 auto displayIdOpt = mHwc.onVsync(kHwcDisplayId, kTimestamp);
431 ASSERT_TRUE(displayIdOpt);
432 EXPECT_EQ(physicalDisplayId, displayIdOpt);
433
434 // Attempt to send the same time stamp again.
435 displayIdOpt = mHwc.onVsync(kHwcDisplayId, kTimestamp);
436 EXPECT_FALSE(displayIdOpt);
437}
438
439TEST_F(HWComposerTest, onVsyncInvalid) {
440 constexpr hal::HWDisplayId kInvalidHwcDisplayId = 2;
441 constexpr nsecs_t kTimestamp = 1;
442 const auto displayIdOpt = mHwc.onVsync(kInvalidHwcDisplayId, kTimestamp);
443 EXPECT_FALSE(displayIdOpt);
444}
445
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700446struct MockHWC2ComposerCallback final : StrictMock<HWC2::ComposerCallback> {
Huihong Luo791bef92023-09-19 21:18:45 -0700447 MOCK_METHOD(void, onComposerHalHotplugEvent, (hal::HWDisplayId, DisplayHotplugEvent),
448 (override));
Dominik Laskowski0deb06e2021-04-16 23:18:31 -0700449 MOCK_METHOD1(onComposerHalRefresh, void(hal::HWDisplayId));
450 MOCK_METHOD3(onComposerHalVsync,
451 void(hal::HWDisplayId, int64_t timestamp, std::optional<hal::VsyncPeriodNanos>));
452 MOCK_METHOD2(onComposerHalVsyncPeriodTimingChanged,
453 void(hal::HWDisplayId, const hal::VsyncPeriodChangeTimeline&));
454 MOCK_METHOD1(onComposerHalSeamlessPossible, void(hal::HWDisplayId));
Yichi Chen1a417af2022-01-21 15:29:52 +0800455 MOCK_METHOD1(onComposerHalVsyncIdle, void(hal::HWDisplayId));
ramindani12bfe6b2023-02-03 13:29:19 -0800456 MOCK_METHOD(void, onRefreshRateChangedDebug, (const RefreshRateChangedDebugData&), (override));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800457};
458
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700459struct HWComposerSetCallbackTest : HWComposerTest {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700460 MockHWC2ComposerCallback mCallback;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800461};
462
Dominik Laskowski0deb06e2021-04-16 23:18:31 -0700463TEST_F(HWComposerSetCallbackTest, loadsLayerMetadataSupport) {
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800464 const std::string kMetadata1Name = "com.example.metadata.1";
465 constexpr bool kMetadata1Mandatory = false;
466 const std::string kMetadata2Name = "com.example.metadata.2";
467 constexpr bool kMetadata2Mandatory = true;
468
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(_))
Peiyong Line9d809e2020-04-14 13:10:48 -0700471 .WillOnce(DoAll(SetArgPointee<0>(std::vector<hal::LayerGenericMetadataKey>{
472 {kMetadata1Name, kMetadata1Mandatory},
473 {kMetadata2Name, kMetadata2Mandatory},
474 }),
Dominik Laskowski969cdcb2024-02-08 16:35:29 -0500475 Return(V2_4::Error::NONE)));
Sally Qibb866c12022-10-17 11:31:20 -0700476 EXPECT_CALL(*mHal, getOverlaySupport(_)).WillOnce(Return(HalError::NONE));
Kriti Dang674b9372022-11-18 10:58:44 +0100477 EXPECT_CALL(*mHal, getHdrConversionCapabilities(_)).WillOnce(Return(HalError::NONE));
Sally Qibb866c12022-10-17 11:31:20 -0700478
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800479 EXPECT_CALL(*mHal, registerCallback(_));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800480
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700481 mHwc.setCallback(mCallback);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800482
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700483 const auto& supported = mHwc.getSupportedLayerGenericMetadata();
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800484 EXPECT_EQ(2u, supported.size());
485 EXPECT_EQ(1u, supported.count(kMetadata1Name));
486 EXPECT_EQ(kMetadata1Mandatory, supported.find(kMetadata1Name)->second);
487 EXPECT_EQ(1u, supported.count(kMetadata2Name));
488 EXPECT_EQ(kMetadata2Mandatory, supported.find(kMetadata2Name)->second);
489}
490
Dominik Laskowski0deb06e2021-04-16 23:18:31 -0700491TEST_F(HWComposerSetCallbackTest, handlesUnsupportedCallToGetLayerGenericMetadataKeys) {
Ady Abrahamde549d42022-01-26 19:19:17 -0800492 EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<aidl::Capability>{}));
Dominik Laskowski969cdcb2024-02-08 16:35:29 -0500493 EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_)).WillOnce(Return(V2_4::Error::UNSUPPORTED));
Sally Qibb866c12022-10-17 11:31:20 -0700494 EXPECT_CALL(*mHal, getOverlaySupport(_)).WillOnce(Return(HalError::UNSUPPORTED));
Kriti Dang674b9372022-11-18 10:58:44 +0100495 EXPECT_CALL(*mHal, getHdrConversionCapabilities(_)).WillOnce(Return(HalError::UNSUPPORTED));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800496 EXPECT_CALL(*mHal, registerCallback(_));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800497
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700498 mHwc.setCallback(mCallback);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800499
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700500 const auto& supported = mHwc.getSupportedLayerGenericMetadata();
501 EXPECT_TRUE(supported.empty());
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800502}
503
504struct HWComposerLayerTest : public testing::Test {
Peiyong Line9d809e2020-04-14 13:10:48 -0700505 static constexpr hal::HWDisplayId kDisplayId = static_cast<hal::HWDisplayId>(1001);
506 static constexpr hal::HWLayerId kLayerId = static_cast<hal::HWLayerId>(1002);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800507
Ady Abrahamde549d42022-01-26 19:19:17 -0800508 HWComposerLayerTest(const std::unordered_set<aidl::Capability>& capabilities)
Lloyd Piquea516c002021-05-07 14:36:58 -0700509 : mCapabilies(capabilities) {
510 EXPECT_CALL(mDisplay, getId()).WillRepeatedly(Return(kDisplayId));
511 }
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800512
Lloyd Piquea516c002021-05-07 14:36:58 -0700513 ~HWComposerLayerTest() override {
514 EXPECT_CALL(mDisplay, onLayerDestroyed(kLayerId));
515 EXPECT_CALL(*mHal, destroyLayer(kDisplayId, kLayerId));
516 }
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800517
518 std::unique_ptr<Hwc2::mock::Composer> mHal{new StrictMock<Hwc2::mock::Composer>()};
Ady Abrahamde549d42022-01-26 19:19:17 -0800519 const std::unordered_set<aidl::Capability> mCapabilies;
Lloyd Piquea516c002021-05-07 14:36:58 -0700520 StrictMock<HWC2::mock::Display> mDisplay;
521 HWC2::impl::Layer mLayer{*mHal, mCapabilies, mDisplay, kLayerId};
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800522};
523
524struct HWComposerLayerGenericMetadataTest : public HWComposerLayerTest {
525 static const std::string kLayerGenericMetadata1Name;
526 static constexpr bool kLayerGenericMetadata1Mandatory = false;
527 static const std::vector<uint8_t> kLayerGenericMetadata1Value;
528 static const std::string kLayerGenericMetadata2Name;
529 static constexpr bool kLayerGenericMetadata2Mandatory = true;
530 static const std::vector<uint8_t> kLayerGenericMetadata2Value;
531
532 HWComposerLayerGenericMetadataTest() : HWComposerLayerTest({}) {}
533};
534
535const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Name =
536 "com.example.metadata.1";
537
538const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Value = {1u,
539 2u,
540 3u};
541
542const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Name =
543 "com.example.metadata.2";
544
545const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Value = {45u,
546 67u};
547
548TEST_F(HWComposerLayerGenericMetadataTest, forwardsSupportedMetadata) {
549 EXPECT_CALL(*mHal,
550 setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata1Name,
551 kLayerGenericMetadata1Mandatory,
552 kLayerGenericMetadata1Value))
Dominik Laskowski969cdcb2024-02-08 16:35:29 -0500553 .WillOnce(Return(V2_4::Error::NONE));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800554 auto result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata1Name,
555 kLayerGenericMetadata1Mandatory,
556 kLayerGenericMetadata1Value);
Peiyong Line9d809e2020-04-14 13:10:48 -0700557 EXPECT_EQ(hal::Error::NONE, result);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800558
559 EXPECT_CALL(*mHal,
560 setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata2Name,
561 kLayerGenericMetadata2Mandatory,
562 kLayerGenericMetadata2Value))
Dominik Laskowski969cdcb2024-02-08 16:35:29 -0500563 .WillOnce(Return(V2_4::Error::UNSUPPORTED));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800564 result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata2Name,
565 kLayerGenericMetadata2Mandatory,
566 kLayerGenericMetadata2Value);
Peiyong Line9d809e2020-04-14 13:10:48 -0700567 EXPECT_EQ(hal::Error::UNSUPPORTED, result);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800568}
569
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800570} // namespace android