blob: 58d7a40fc82d80e00f8851165fedb2ba1bf9921d [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
ramindanif8c0f102023-10-09 12:42:57 -070082 void setDisplayData(HalDisplayId displayId, TimePoint lastExpectedPresentTimestamp,
83 Fps lastFrameInterval) {
ramindanib90711b2023-10-02 15:13:19 -070084 ASSERT_TRUE(mHwc.mDisplayData.find(displayId) != mHwc.mDisplayData.end());
85 auto& displayData = mHwc.mDisplayData.at(displayId);
ramindanif8c0f102023-10-09 12:42:57 -070086 std::scoped_lock lock{displayData.expectedPresentLock};
ramindanib90711b2023-10-02 15:13:19 -070087 displayData.lastExpectedPresentTimestamp = lastExpectedPresentTimestamp;
ramindanif8c0f102023-10-09 12:42:57 -070088 displayData.lastFrameInterval = lastFrameInterval;
ramindanib90711b2023-10-02 15:13:19 -070089 }
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070090};
Marin Shalamanov8b196592021-08-09 16:24:42 +020091
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -070092TEST_F(HWComposerTest, isHeadless) {
93 ASSERT_TRUE(mHwc.isHeadless());
94
95 constexpr hal::HWDisplayId kHwcDisplayId = 1;
96 expectHotplugConnect(kHwcDisplayId);
97
98 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
Marin Shalamanov8b196592021-08-09 16:24:42 +020099 ASSERT_TRUE(info);
Marin Shalamanov8b196592021-08-09 16:24:42 +0200100
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700101 ASSERT_FALSE(mHwc.isHeadless());
102
103 mHwc.disconnectDisplay(info->id);
104 ASSERT_TRUE(mHwc.isHeadless());
105}
106
107TEST_F(HWComposerTest, getActiveMode) {
108 // Unknown display.
109 EXPECT_EQ(mHwc.getActiveMode(PhysicalDisplayId::fromPort(0)), std::nullopt);
110
111 constexpr hal::HWDisplayId kHwcDisplayId = 2;
112 expectHotplugConnect(kHwcDisplayId);
113
114 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
115 ASSERT_TRUE(info);
116
117 {
118 // Display is known to SF but not HWC, e.g. the hotplug disconnect is pending.
119 EXPECT_CALL(*mHal, getActiveConfig(kHwcDisplayId, _))
120 .WillOnce(Return(HalError::BAD_DISPLAY));
121
122 EXPECT_EQ(mHwc.getActiveMode(info->id), std::nullopt);
123 }
124 {
125 constexpr hal::HWConfigId kConfigId = 42;
126 EXPECT_CALL(*mHal, getActiveConfig(kHwcDisplayId, _))
127 .WillOnce(DoAll(SetArgPointee<1>(kConfigId), Return(HalError::NONE)));
128
129 EXPECT_EQ(mHwc.getActiveMode(info->id), kConfigId);
130 }
Marin Shalamanov8b196592021-08-09 16:24:42 +0200131}
132
ramindani0cd1d8d2023-06-13 13:43:23 -0700133TEST_F(HWComposerTest, getModesWithLegacyDisplayConfigs) {
134 constexpr hal::HWDisplayId kHwcDisplayId = 2;
135 constexpr hal::HWConfigId kConfigId = 42;
ramindani263a3f12023-07-18 20:44:49 -0700136 constexpr int32_t kMaxFrameIntervalNs = 50000000; // 20Fps
ramindani0cd1d8d2023-06-13 13:43:23 -0700137
138 expectHotplugConnect(kHwcDisplayId);
139 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
140 ASSERT_TRUE(info);
141
142 EXPECT_CALL(*mHal, getDisplayConfigurationsSupported()).WillRepeatedly(Return(false));
143
144 {
145 EXPECT_CALL(*mHal, getDisplayConfigs(kHwcDisplayId, _))
146 .WillOnce(Return(HalError::BAD_DISPLAY));
ramindani263a3f12023-07-18 20:44:49 -0700147 EXPECT_TRUE(mHwc.getModes(info->id, kMaxFrameIntervalNs).empty());
ramindani0cd1d8d2023-06-13 13:43:23 -0700148 }
149 {
150 constexpr int32_t kWidth = 480;
151 constexpr int32_t kHeight = 720;
152 constexpr int32_t kConfigGroup = 1;
153 constexpr int32_t kVsyncPeriod = 16666667;
154
155 EXPECT_CALL(*mHal,
156 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::WIDTH,
157 _))
158 .WillRepeatedly(DoAll(SetArgPointee<3>(kWidth), Return(HalError::NONE)));
159 EXPECT_CALL(*mHal,
160 getDisplayAttribute(kHwcDisplayId, kConfigId,
161 IComposerClient::Attribute::HEIGHT, _))
162 .WillRepeatedly(DoAll(SetArgPointee<3>(kHeight), Return(HalError::NONE)));
163 EXPECT_CALL(*mHal,
164 getDisplayAttribute(kHwcDisplayId, kConfigId,
165 IComposerClient::Attribute::CONFIG_GROUP, _))
166 .WillRepeatedly(DoAll(SetArgPointee<3>(kConfigGroup), Return(HalError::NONE)));
167 EXPECT_CALL(*mHal,
168 getDisplayAttribute(kHwcDisplayId, kConfigId,
169 IComposerClient::Attribute::VSYNC_PERIOD, _))
170 .WillRepeatedly(DoAll(SetArgPointee<3>(kVsyncPeriod), Return(HalError::NONE)));
171
172 // Optional Parameters UNSUPPORTED
173 EXPECT_CALL(*mHal,
174 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_X,
175 _))
176 .WillOnce(Return(HalError::UNSUPPORTED));
177 EXPECT_CALL(*mHal,
178 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_Y,
179 _))
180 .WillOnce(Return(HalError::UNSUPPORTED));
181
182 EXPECT_CALL(*mHal, getDisplayConfigs(kHwcDisplayId, _))
183 .WillRepeatedly(DoAll(SetArgPointee<1>(std::vector<hal::HWConfigId>{kConfigId}),
184 Return(HalError::NONE)));
185
ramindani263a3f12023-07-18 20:44:49 -0700186 auto modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
ramindani0cd1d8d2023-06-13 13:43:23 -0700187 EXPECT_EQ(modes.size(), size_t{1});
188 EXPECT_EQ(modes.front().hwcId, kConfigId);
189 EXPECT_EQ(modes.front().width, kWidth);
190 EXPECT_EQ(modes.front().height, kHeight);
191 EXPECT_EQ(modes.front().configGroup, kConfigGroup);
192 EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
193 EXPECT_EQ(modes.front().dpiX, -1);
194 EXPECT_EQ(modes.front().dpiY, -1);
195
196 // Optional parameters are supported
197 constexpr int32_t kDpi = 320;
198 EXPECT_CALL(*mHal,
199 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_X,
200 _))
201 .WillOnce(DoAll(SetArgPointee<3>(kDpi), Return(HalError::NONE)));
202 EXPECT_CALL(*mHal,
203 getDisplayAttribute(kHwcDisplayId, kConfigId, IComposerClient::Attribute::DPI_Y,
204 _))
205 .WillOnce(DoAll(SetArgPointee<3>(kDpi), Return(HalError::NONE)));
206
ramindani263a3f12023-07-18 20:44:49 -0700207 modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
ramindani0cd1d8d2023-06-13 13:43:23 -0700208 EXPECT_EQ(modes.size(), size_t{1});
209 EXPECT_EQ(modes.front().hwcId, kConfigId);
210 EXPECT_EQ(modes.front().width, kWidth);
211 EXPECT_EQ(modes.front().height, kHeight);
212 EXPECT_EQ(modes.front().configGroup, kConfigGroup);
213 EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
214 // DPI values are scaled by 1000 in the legacy implementation.
215 EXPECT_EQ(modes.front().dpiX, kDpi / 1000.f);
216 EXPECT_EQ(modes.front().dpiY, kDpi / 1000.f);
217 }
218}
219
220TEST_F(HWComposerTest, getModesWithDisplayConfigurations) {
221 constexpr hal::HWDisplayId kHwcDisplayId = 2;
222 constexpr hal::HWConfigId kConfigId = 42;
ramindani263a3f12023-07-18 20:44:49 -0700223 constexpr int32_t kMaxFrameIntervalNs = 50000000; // 20Fps
ramindani0cd1d8d2023-06-13 13:43:23 -0700224 expectHotplugConnect(kHwcDisplayId);
225 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
226 ASSERT_TRUE(info);
227
228 EXPECT_CALL(*mHal, getDisplayConfigurationsSupported()).WillRepeatedly(Return(true));
229
230 {
ramindani263a3f12023-07-18 20:44:49 -0700231 EXPECT_CALL(*mHal, getDisplayConfigurations(kHwcDisplayId, _, _))
ramindani0cd1d8d2023-06-13 13:43:23 -0700232 .WillOnce(Return(HalError::BAD_DISPLAY));
ramindani263a3f12023-07-18 20:44:49 -0700233 EXPECT_TRUE(mHwc.getModes(info->id, kMaxFrameIntervalNs).empty());
ramindani0cd1d8d2023-06-13 13:43:23 -0700234 }
235 {
236 constexpr int32_t kWidth = 480;
237 constexpr int32_t kHeight = 720;
238 constexpr int32_t kConfigGroup = 1;
239 constexpr int32_t kVsyncPeriod = 16666667;
ramindanib90711b2023-10-02 15:13:19 -0700240 const hal::VrrConfig vrrConfig =
241 hal::VrrConfig{.minFrameIntervalNs = static_cast<Fps>(120_Hz).getPeriodNsecs(),
242 .notifyExpectedPresentConfig = hal::VrrConfig::
243 NotifyExpectedPresentConfig{.notifyExpectedPresentHeadsUpNs =
244 ms2ns(30),
245 .notifyExpectedPresentTimeoutNs =
246 ms2ns(30)}};
247 hal::DisplayConfiguration displayConfiguration{.configId = kConfigId,
248 .width = kWidth,
249 .height = kHeight,
250 .configGroup = kConfigGroup,
251 .vsyncPeriod = kVsyncPeriod,
252 .vrrConfig = vrrConfig};
ramindani0cd1d8d2023-06-13 13:43:23 -0700253
ramindani263a3f12023-07-18 20:44:49 -0700254 EXPECT_CALL(*mHal, getDisplayConfigurations(kHwcDisplayId, _, _))
255 .WillOnce(DoAll(SetArgPointee<2>(std::vector<hal::DisplayConfiguration>{
ramindani0cd1d8d2023-06-13 13:43:23 -0700256 displayConfiguration}),
257 Return(HalError::NONE)));
258
259 // Optional dpi not supported
ramindani263a3f12023-07-18 20:44:49 -0700260 auto modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
ramindani0cd1d8d2023-06-13 13:43:23 -0700261 EXPECT_EQ(modes.size(), size_t{1});
262 EXPECT_EQ(modes.front().hwcId, kConfigId);
263 EXPECT_EQ(modes.front().width, kWidth);
264 EXPECT_EQ(modes.front().height, kHeight);
265 EXPECT_EQ(modes.front().configGroup, kConfigGroup);
266 EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
ramindanib90711b2023-10-02 15:13:19 -0700267 EXPECT_EQ(modes.front().vrrConfig, vrrConfig);
ramindani0cd1d8d2023-06-13 13:43:23 -0700268 EXPECT_EQ(modes.front().dpiX, -1);
269 EXPECT_EQ(modes.front().dpiY, -1);
270
271 // Supports optional dpi parameter
272 constexpr int32_t kDpi = 320;
273 displayConfiguration.dpi = {kDpi, kDpi};
274
ramindani263a3f12023-07-18 20:44:49 -0700275 EXPECT_CALL(*mHal, getDisplayConfigurations(kHwcDisplayId, _, _))
276 .WillOnce(DoAll(SetArgPointee<2>(std::vector<hal::DisplayConfiguration>{
ramindani0cd1d8d2023-06-13 13:43:23 -0700277 displayConfiguration}),
278 Return(HalError::NONE)));
279
ramindani263a3f12023-07-18 20:44:49 -0700280 modes = mHwc.getModes(info->id, kMaxFrameIntervalNs);
ramindani0cd1d8d2023-06-13 13:43:23 -0700281 EXPECT_EQ(modes.size(), size_t{1});
282 EXPECT_EQ(modes.front().hwcId, kConfigId);
283 EXPECT_EQ(modes.front().width, kWidth);
284 EXPECT_EQ(modes.front().height, kHeight);
285 EXPECT_EQ(modes.front().configGroup, kConfigGroup);
286 EXPECT_EQ(modes.front().vsyncPeriod, kVsyncPeriod);
ramindanib90711b2023-10-02 15:13:19 -0700287 EXPECT_EQ(modes.front().vrrConfig, vrrConfig);
ramindani0cd1d8d2023-06-13 13:43:23 -0700288 EXPECT_EQ(modes.front().dpiX, kDpi);
289 EXPECT_EQ(modes.front().dpiY, kDpi);
290 }
291}
292
Leon Scroggins III959a7ff2023-02-07 11:24:25 -0500293TEST_F(HWComposerTest, onVsync) {
294 constexpr hal::HWDisplayId kHwcDisplayId = 1;
295 expectHotplugConnect(kHwcDisplayId);
296
297 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
298 ASSERT_TRUE(info);
299
300 const auto physicalDisplayId = info->id;
301
302 // Deliberately chosen not to match DisplayData.lastPresentTimestamp's
303 // initial value.
304 constexpr nsecs_t kTimestamp = 1;
305 auto displayIdOpt = mHwc.onVsync(kHwcDisplayId, kTimestamp);
306 ASSERT_TRUE(displayIdOpt);
307 EXPECT_EQ(physicalDisplayId, displayIdOpt);
308
309 // Attempt to send the same time stamp again.
310 displayIdOpt = mHwc.onVsync(kHwcDisplayId, kTimestamp);
311 EXPECT_FALSE(displayIdOpt);
312}
313
314TEST_F(HWComposerTest, onVsyncInvalid) {
315 constexpr hal::HWDisplayId kInvalidHwcDisplayId = 2;
316 constexpr nsecs_t kTimestamp = 1;
317 const auto displayIdOpt = mHwc.onVsync(kInvalidHwcDisplayId, kTimestamp);
318 EXPECT_FALSE(displayIdOpt);
319}
320
ramindanib90711b2023-10-02 15:13:19 -0700321TEST_F(HWComposerTest, notifyExpectedPresentTimeout) {
322 constexpr hal::HWDisplayId kHwcDisplayId = 2;
323 expectHotplugConnect(kHwcDisplayId);
324 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
325 ASSERT_TRUE(info);
326
327 auto expectedPresentTime = systemTime() + ms2ns(10);
ramindanif8c0f102023-10-09 12:42:57 -0700328 static constexpr Fps Fps60Hz = 60_Hz;
329 static constexpr int32_t kFrameInterval5HzNs = static_cast<Fps>(5_Hz).getPeriodNsecs();
330 static constexpr int32_t kFrameInterval60HzNs = Fps60Hz.getPeriodNsecs();
331 static constexpr int32_t kFrameInterval120HzNs = static_cast<Fps>(120_Hz).getPeriodNsecs();
332 static constexpr Period kVsyncPeriod =
333 Period::fromNs(static_cast<Fps>(240_Hz).getPeriodNsecs());
334 static constexpr Period kTimeoutNs = Period::fromNs(kFrameInterval5HzNs);
335 static constexpr auto kLastExpectedPresentTimestamp = TimePoint::fromNs(0);
ramindanib90711b2023-10-02 15:13:19 -0700336
ramindanif8c0f102023-10-09 12:42:57 -0700337 ASSERT_NO_FATAL_FAILURE(setDisplayData(info->id, kLastExpectedPresentTimestamp, Fps60Hz));
ramindanib90711b2023-10-02 15:13:19 -0700338
339 {
340 // Very first ExpectedPresent after idle, no previous timestamp
341 EXPECT_CALL(*mHal,
ramindanif8c0f102023-10-09 12:42:57 -0700342 notifyExpectedPresent(kHwcDisplayId, expectedPresentTime, kFrameInterval60HzNs))
ramindanib90711b2023-10-02 15:13:19 -0700343 .WillOnce(Return(HalError::NONE));
ramindanif8c0f102023-10-09 12:42:57 -0700344 mHwc.notifyExpectedPresentIfRequired(info->id, kVsyncPeriod,
345 TimePoint::fromNs(expectedPresentTime), Fps60Hz,
ramindanib90711b2023-10-02 15:13:19 -0700346 kTimeoutNs);
347 }
348 {
ramindanif8c0f102023-10-09 12:42:57 -0700349 // Absent timeoutNs
350 expectedPresentTime += 2 * kFrameInterval5HzNs;
351 EXPECT_CALL(*mHal, notifyExpectedPresent(kHwcDisplayId, _, _)).Times(0);
352 mHwc.notifyExpectedPresentIfRequired(info->id, kVsyncPeriod,
353 TimePoint::fromNs(expectedPresentTime), Fps60Hz,
354 /*timeoutOpt*/ std::nullopt);
355 }
356 {
357 // Timeout is 0
358 expectedPresentTime += kFrameInterval60HzNs;
ramindanib90711b2023-10-02 15:13:19 -0700359 EXPECT_CALL(*mHal,
ramindanif8c0f102023-10-09 12:42:57 -0700360 notifyExpectedPresent(kHwcDisplayId, expectedPresentTime, kFrameInterval60HzNs))
ramindanib90711b2023-10-02 15:13:19 -0700361 .WillOnce(Return(HalError::NONE));
ramindanif8c0f102023-10-09 12:42:57 -0700362 mHwc.notifyExpectedPresentIfRequired(info->id, kVsyncPeriod,
363 TimePoint::fromNs(expectedPresentTime), Fps60Hz,
364 Period::fromNs(0));
365 }
366 {
367 // ExpectedPresent is after the timeoutNs
368 expectedPresentTime += 2 * kFrameInterval5HzNs;
369 EXPECT_CALL(*mHal,
370 notifyExpectedPresent(kHwcDisplayId, expectedPresentTime, kFrameInterval60HzNs))
371 .WillOnce(Return(HalError::NONE));
372 mHwc.notifyExpectedPresentIfRequired(info->id, kVsyncPeriod,
373 TimePoint::fromNs(expectedPresentTime), Fps60Hz,
374 kTimeoutNs);
375 }
376 {
377 // ExpectedPresent has not changed
378 EXPECT_CALL(*mHal, notifyExpectedPresent(kHwcDisplayId, _, _)).Times(0);
379 mHwc.notifyExpectedPresentIfRequired(info->id, kVsyncPeriod,
380 TimePoint::fromNs(expectedPresentTime), Fps60Hz,
ramindanib90711b2023-10-02 15:13:19 -0700381 kTimeoutNs);
382 }
383 {
384 // ExpectedPresent is after the last reported ExpectedPresent.
ramindanif8c0f102023-10-09 12:42:57 -0700385 expectedPresentTime += kFrameInterval60HzNs;
ramindanib90711b2023-10-02 15:13:19 -0700386 EXPECT_CALL(*mHal, notifyExpectedPresent(kHwcDisplayId, _, _)).Times(0);
ramindanif8c0f102023-10-09 12:42:57 -0700387 mHwc.notifyExpectedPresentIfRequired(info->id, kVsyncPeriod,
388 TimePoint::fromNs(expectedPresentTime), Fps60Hz,
ramindanib90711b2023-10-02 15:13:19 -0700389 kTimeoutNs);
390 }
391 {
392 // ExpectedPresent is before the last reported ExpectedPresent but after the timeoutNs,
393 // representing we changed our decision and want to present earlier than previously
394 // reported.
ramindanif8c0f102023-10-09 12:42:57 -0700395 expectedPresentTime -= kFrameInterval120HzNs;
ramindanib90711b2023-10-02 15:13:19 -0700396 EXPECT_CALL(*mHal,
ramindanif8c0f102023-10-09 12:42:57 -0700397 notifyExpectedPresent(kHwcDisplayId, expectedPresentTime, kFrameInterval60HzNs))
ramindanib90711b2023-10-02 15:13:19 -0700398 .WillOnce(Return(HalError::NONE));
ramindanif8c0f102023-10-09 12:42:57 -0700399 mHwc.notifyExpectedPresentIfRequired(info->id, kVsyncPeriod,
400 TimePoint::fromNs(expectedPresentTime), Fps60Hz,
ramindanib90711b2023-10-02 15:13:19 -0700401 kTimeoutNs);
402 }
403}
404
ramindanif8c0f102023-10-09 12:42:57 -0700405TEST_F(HWComposerTest, notifyExpectedPresentRenderRateChanged) {
406 constexpr hal::HWDisplayId kHwcDisplayId = 2;
407 expectHotplugConnect(kHwcDisplayId);
408 const auto info = mHwc.onHotplug(kHwcDisplayId, hal::Connection::CONNECTED);
409 ASSERT_TRUE(info);
410
411 const auto now = systemTime();
412 auto expectedPresentTime = now;
413 static constexpr Period kTimeoutNs = Period::fromNs(static_cast<Fps>(1_Hz).getPeriodNsecs());
414
415 ASSERT_NO_FATAL_FAILURE(setDisplayData(info->id, TimePoint::fromNs(now), Fps::fromValue(0)));
416 static constexpr int32_t kFrameIntervalNs120Hz = static_cast<Fps>(120_Hz).getPeriodNsecs();
417 static constexpr int32_t kFrameIntervalNs96Hz = static_cast<Fps>(96_Hz).getPeriodNsecs();
418 static constexpr int32_t kFrameIntervalNs80Hz = static_cast<Fps>(80_Hz).getPeriodNsecs();
419 static constexpr int32_t kFrameIntervalNs60Hz = static_cast<Fps>(60_Hz).getPeriodNsecs();
420 static constexpr int32_t kFrameIntervalNs40Hz = static_cast<Fps>(40_Hz).getPeriodNsecs();
421 static constexpr int32_t kFrameIntervalNs30Hz = static_cast<Fps>(30_Hz).getPeriodNsecs();
422 static constexpr int32_t kFrameIntervalNs24Hz = static_cast<Fps>(24_Hz).getPeriodNsecs();
423 static constexpr int32_t kFrameIntervalNs20Hz = static_cast<Fps>(20_Hz).getPeriodNsecs();
424 static constexpr Period kVsyncPeriod =
425 Period::fromNs(static_cast<Fps>(240_Hz).getPeriodNsecs());
426
427 struct FrameRateIntervalTestData {
428 int32_t frameIntervalNs;
429 bool callExpectedPresent;
430 };
431 const std::vector<FrameRateIntervalTestData> frameIntervals = {
432 {kFrameIntervalNs60Hz, true}, {kFrameIntervalNs96Hz, true},
433 {kFrameIntervalNs80Hz, true}, {kFrameIntervalNs120Hz, true},
434 {kFrameIntervalNs80Hz, true}, {kFrameIntervalNs60Hz, true},
435 {kFrameIntervalNs60Hz, false}, {kFrameIntervalNs30Hz, false},
436 {kFrameIntervalNs24Hz, true}, {kFrameIntervalNs40Hz, true},
437 {kFrameIntervalNs20Hz, false}, {kFrameIntervalNs60Hz, true},
438 {kFrameIntervalNs20Hz, false}, {kFrameIntervalNs120Hz, true},
439 };
440
441 for (const auto& [frameIntervalNs, callExpectedPresent] : frameIntervals) {
442 {
443 expectedPresentTime += frameIntervalNs;
444 if (callExpectedPresent) {
445 EXPECT_CALL(*mHal,
446 notifyExpectedPresent(kHwcDisplayId, expectedPresentTime,
447 frameIntervalNs))
448 .WillOnce(Return(HalError::NONE));
449 } else {
450 EXPECT_CALL(*mHal, notifyExpectedPresent(kHwcDisplayId, _, _)).Times(0);
451 }
452 mHwc.notifyExpectedPresentIfRequired(info->id, kVsyncPeriod,
453 TimePoint::fromNs(expectedPresentTime),
454 Fps::fromPeriodNsecs(frameIntervalNs), kTimeoutNs);
455 }
456 }
457}
458
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700459struct MockHWC2ComposerCallback final : StrictMock<HWC2::ComposerCallback> {
Dominik Laskowski0deb06e2021-04-16 23:18:31 -0700460 MOCK_METHOD2(onComposerHalHotplug, void(hal::HWDisplayId, hal::Connection));
461 MOCK_METHOD1(onComposerHalRefresh, void(hal::HWDisplayId));
462 MOCK_METHOD3(onComposerHalVsync,
463 void(hal::HWDisplayId, int64_t timestamp, std::optional<hal::VsyncPeriodNanos>));
464 MOCK_METHOD2(onComposerHalVsyncPeriodTimingChanged,
465 void(hal::HWDisplayId, const hal::VsyncPeriodChangeTimeline&));
466 MOCK_METHOD1(onComposerHalSeamlessPossible, void(hal::HWDisplayId));
Yichi Chen1a417af2022-01-21 15:29:52 +0800467 MOCK_METHOD1(onComposerHalVsyncIdle, void(hal::HWDisplayId));
ramindani12bfe6b2023-02-03 13:29:19 -0800468 MOCK_METHOD(void, onRefreshRateChangedDebug, (const RefreshRateChangedDebugData&), (override));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800469};
470
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700471struct HWComposerSetCallbackTest : HWComposerTest {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700472 MockHWC2ComposerCallback mCallback;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800473};
474
Dominik Laskowski0deb06e2021-04-16 23:18:31 -0700475TEST_F(HWComposerSetCallbackTest, loadsLayerMetadataSupport) {
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800476 const std::string kMetadata1Name = "com.example.metadata.1";
477 constexpr bool kMetadata1Mandatory = false;
478 const std::string kMetadata2Name = "com.example.metadata.2";
479 constexpr bool kMetadata2Mandatory = true;
480
Ady Abrahamde549d42022-01-26 19:19:17 -0800481 EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<aidl::Capability>{}));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800482 EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_))
Peiyong Line9d809e2020-04-14 13:10:48 -0700483 .WillOnce(DoAll(SetArgPointee<0>(std::vector<hal::LayerGenericMetadataKey>{
484 {kMetadata1Name, kMetadata1Mandatory},
485 {kMetadata2Name, kMetadata2Mandatory},
486 }),
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800487 Return(hardware::graphics::composer::V2_4::Error::NONE)));
Sally Qibb866c12022-10-17 11:31:20 -0700488 EXPECT_CALL(*mHal, getOverlaySupport(_)).WillOnce(Return(HalError::NONE));
Kriti Dang674b9372022-11-18 10:58:44 +0100489 EXPECT_CALL(*mHal, getHdrConversionCapabilities(_)).WillOnce(Return(HalError::NONE));
Sally Qibb866c12022-10-17 11:31:20 -0700490
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800491 EXPECT_CALL(*mHal, registerCallback(_));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800492
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700493 mHwc.setCallback(mCallback);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800494
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700495 const auto& supported = mHwc.getSupportedLayerGenericMetadata();
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800496 EXPECT_EQ(2u, supported.size());
497 EXPECT_EQ(1u, supported.count(kMetadata1Name));
498 EXPECT_EQ(kMetadata1Mandatory, supported.find(kMetadata1Name)->second);
499 EXPECT_EQ(1u, supported.count(kMetadata2Name));
500 EXPECT_EQ(kMetadata2Mandatory, supported.find(kMetadata2Name)->second);
501}
502
Dominik Laskowski0deb06e2021-04-16 23:18:31 -0700503TEST_F(HWComposerSetCallbackTest, handlesUnsupportedCallToGetLayerGenericMetadataKeys) {
Ady Abrahamde549d42022-01-26 19:19:17 -0800504 EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<aidl::Capability>{}));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800505 EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_))
506 .WillOnce(Return(hardware::graphics::composer::V2_4::Error::UNSUPPORTED));
Sally Qibb866c12022-10-17 11:31:20 -0700507 EXPECT_CALL(*mHal, getOverlaySupport(_)).WillOnce(Return(HalError::UNSUPPORTED));
Kriti Dang674b9372022-11-18 10:58:44 +0100508 EXPECT_CALL(*mHal, getHdrConversionCapabilities(_)).WillOnce(Return(HalError::UNSUPPORTED));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800509 EXPECT_CALL(*mHal, registerCallback(_));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800510
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700511 mHwc.setCallback(mCallback);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800512
Dominik Laskowskie2c5b0a2022-08-10 14:53:53 -0700513 const auto& supported = mHwc.getSupportedLayerGenericMetadata();
514 EXPECT_TRUE(supported.empty());
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800515}
516
517struct HWComposerLayerTest : public testing::Test {
Peiyong Line9d809e2020-04-14 13:10:48 -0700518 static constexpr hal::HWDisplayId kDisplayId = static_cast<hal::HWDisplayId>(1001);
519 static constexpr hal::HWLayerId kLayerId = static_cast<hal::HWLayerId>(1002);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800520
Ady Abrahamde549d42022-01-26 19:19:17 -0800521 HWComposerLayerTest(const std::unordered_set<aidl::Capability>& capabilities)
Lloyd Piquea516c002021-05-07 14:36:58 -0700522 : mCapabilies(capabilities) {
523 EXPECT_CALL(mDisplay, getId()).WillRepeatedly(Return(kDisplayId));
524 }
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800525
Lloyd Piquea516c002021-05-07 14:36:58 -0700526 ~HWComposerLayerTest() override {
527 EXPECT_CALL(mDisplay, onLayerDestroyed(kLayerId));
528 EXPECT_CALL(*mHal, destroyLayer(kDisplayId, kLayerId));
529 }
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800530
531 std::unique_ptr<Hwc2::mock::Composer> mHal{new StrictMock<Hwc2::mock::Composer>()};
Ady Abrahamde549d42022-01-26 19:19:17 -0800532 const std::unordered_set<aidl::Capability> mCapabilies;
Lloyd Piquea516c002021-05-07 14:36:58 -0700533 StrictMock<HWC2::mock::Display> mDisplay;
534 HWC2::impl::Layer mLayer{*mHal, mCapabilies, mDisplay, kLayerId};
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800535};
536
537struct HWComposerLayerGenericMetadataTest : public HWComposerLayerTest {
538 static const std::string kLayerGenericMetadata1Name;
539 static constexpr bool kLayerGenericMetadata1Mandatory = false;
540 static const std::vector<uint8_t> kLayerGenericMetadata1Value;
541 static const std::string kLayerGenericMetadata2Name;
542 static constexpr bool kLayerGenericMetadata2Mandatory = true;
543 static const std::vector<uint8_t> kLayerGenericMetadata2Value;
544
545 HWComposerLayerGenericMetadataTest() : HWComposerLayerTest({}) {}
546};
547
548const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Name =
549 "com.example.metadata.1";
550
551const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Value = {1u,
552 2u,
553 3u};
554
555const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Name =
556 "com.example.metadata.2";
557
558const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Value = {45u,
559 67u};
560
561TEST_F(HWComposerLayerGenericMetadataTest, forwardsSupportedMetadata) {
562 EXPECT_CALL(*mHal,
563 setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata1Name,
564 kLayerGenericMetadata1Mandatory,
565 kLayerGenericMetadata1Value))
566 .WillOnce(Return(hardware::graphics::composer::V2_4::Error::NONE));
567 auto result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata1Name,
568 kLayerGenericMetadata1Mandatory,
569 kLayerGenericMetadata1Value);
Peiyong Line9d809e2020-04-14 13:10:48 -0700570 EXPECT_EQ(hal::Error::NONE, result);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800571
572 EXPECT_CALL(*mHal,
573 setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata2Name,
574 kLayerGenericMetadata2Mandatory,
575 kLayerGenericMetadata2Value))
576 .WillOnce(Return(hardware::graphics::composer::V2_4::Error::UNSUPPORTED));
577 result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata2Name,
578 kLayerGenericMetadata2Mandatory,
579 kLayerGenericMetadata2Value);
Peiyong Line9d809e2020-04-14 13:10:48 -0700580 EXPECT_EQ(hal::Error::UNSUPPORTED, result);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800581}
582
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800583} // namespace android