blob: 223f4db8890287c40e2a4b1959fa512e4bf62dce [file] [log] [blame]
Marin Shalamanov07b1ff32020-10-07 16:57:22 +02001/*
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#pragma once
18
19// TODO(b/129481165): remove the #pragma below and fix conversion issues
20#pragma clang diagnostic push
21#pragma clang diagnostic ignored "-Wconversion"
Marin Shalamanovbed7fd32020-12-21 20:02:20 +010022#pragma clang diagnostic ignored "-Wextra"
Marin Shalamanov07b1ff32020-10-07 16:57:22 +020023
24#include <type_traits>
Alec Mouriff793872022-01-13 17:45:06 -080025#include "DisplayIdentificationTestHelpers.h"
Marin Shalamanov07b1ff32020-10-07 16:57:22 +020026
27#include <binder/IPCThreadState.h>
28#include <compositionengine/Display.h>
29#include <compositionengine/DisplayColorProfile.h>
30#include <compositionengine/impl/Display.h>
31#include <compositionengine/impl/OutputCompositionState.h>
32#include <compositionengine/mock/Display.h>
33#include <compositionengine/mock/DisplayColorProfile.h>
34#include <compositionengine/mock/DisplaySurface.h>
35#include <compositionengine/mock/RenderSurface.h>
36#include <gmock/gmock.h>
37#include <gtest/gtest.h>
38#include <gui/mock/GraphicBufferConsumer.h>
39#include <gui/mock/GraphicBufferProducer.h>
40#include <log/log.h>
41#include <private/android_filesystem_config.h>
42#include <renderengine/mock/RenderEngine.h>
43#include <ui/DebugUtils.h>
44
Dominik Laskowski327d6092022-10-11 18:05:08 -040045#include "FakeDisplayInjector.h"
Marin Shalamanov07b1ff32020-10-07 16:57:22 +020046#include "TestableScheduler.h"
47#include "TestableSurfaceFlinger.h"
48#include "mock/DisplayHardware/MockComposer.h"
Dominik Laskowskib0054a22022-03-03 09:03:06 -080049#include "mock/DisplayHardware/MockDisplayMode.h"
Marin Shalamanov07b1ff32020-10-07 16:57:22 +020050#include "mock/DisplayHardware/MockPowerAdvisor.h"
51#include "mock/MockEventThread.h"
Marin Shalamanov07b1ff32020-10-07 16:57:22 +020052#include "mock/MockNativeWindowSurface.h"
Marin Shalamanov07b1ff32020-10-07 16:57:22 +020053#include "mock/MockVsyncController.h"
54#include "mock/system/window/MockNativeWindow.h"
55
56namespace android {
57
58// TODO: Do not polute the android namespace
59namespace hal = android::hardware::graphics::composer::hal;
60
61using testing::_;
62using testing::AnyNumber;
63using testing::DoAll;
64using testing::Mock;
65using testing::ResultOf;
66using testing::Return;
67using testing::SetArgPointee;
68
Leon Scroggins III5967aec2021-12-29 11:14:22 -050069using aidl::android::hardware::graphics::composer3::DisplayCapability;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +020070using hal::ColorMode;
71using hal::Connection;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +020072using hal::DisplayType;
73using hal::Error;
74using hal::Hdr;
75using hal::HWDisplayId;
76using hal::IComposer;
77using hal::IComposerClient;
78using hal::PerFrameMetadataKey;
79using hal::PowerMode;
80
81class DisplayTransactionTest : public testing::Test {
82public:
83 ~DisplayTransactionTest() override;
84
85 // --------------------------------------------------------------------
86 // Mock/Fake injection
87
88 void injectMockScheduler();
89 void injectMockComposer(int virtualDisplayCount);
90 void injectFakeBufferQueueFactory();
91 void injectFakeNativeWindowSurfaceFactory();
Dominik Laskowski327d6092022-10-11 18:05:08 -040092
Marin Shalamanov07b1ff32020-10-07 16:57:22 +020093 sp<DisplayDevice> injectDefaultInternalDisplay(
Dominik Laskowski327d6092022-10-11 18:05:08 -040094 std::function<void(TestableSurfaceFlinger::FakeDisplayDeviceInjector&)> injectExtra) {
95 return mFakeDisplayInjector.injectInternalDisplay(injectExtra);
96 }
Marin Shalamanov07b1ff32020-10-07 16:57:22 +020097
98 // --------------------------------------------------------------------
99 // Postcondition helpers
100
Dominik Laskowskieb627312022-04-07 09:13:16 -0700101 bool hasPhysicalHwcDisplay(hal::HWDisplayId) const;
102 bool hasTransactionFlagSet(int32_t flag) const;
103
104 bool hasDisplayDevice(const sp<IBinder>& displayToken) const;
105 const DisplayDevice& getDisplayDevice(const sp<IBinder>& displayToken) const;
106
107 bool hasCurrentDisplayState(const sp<IBinder>& displayToken) const;
108 const DisplayDeviceState& getCurrentDisplayState(const sp<IBinder>& displayToken) const;
109
110 bool hasDrawingDisplayState(const sp<IBinder>& displayToken) const;
111 const DisplayDeviceState& getDrawingDisplayState(const sp<IBinder>& displayToken) const;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200112
113 // --------------------------------------------------------------------
114 // Test instances
115
116 TestableSurfaceFlinger mFlinger;
Ady Abrahamd11bade2022-08-01 16:18:03 -0700117 sp<mock::NativeWindow> mNativeWindow = sp<mock::NativeWindow>::make();
Brian Lindahlf5ab5ae2022-12-21 14:27:15 -0700118 sp<GraphicBuffer> mBuffer =
119 sp<GraphicBuffer>::make(1u, 1u, PIXEL_FORMAT_RGBA_8888,
120 GRALLOC_USAGE_SW_WRITE_OFTEN | GRALLOC_USAGE_SW_READ_OFTEN);
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200121 Hwc2::mock::PowerAdvisor mPowerAdvisor;
122
Dominik Laskowski327d6092022-10-11 18:05:08 -0400123 FakeDisplayInjector mFakeDisplayInjector{mFlinger, mPowerAdvisor, mNativeWindow};
124
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200125 // These mocks are created by the test, but are destroyed by SurfaceFlinger
126 // by virtue of being stored into a std::unique_ptr. However we still need
127 // to keep a reference to them for use in setting up call expectations.
128 renderengine::mock::RenderEngine* mRenderEngine = new renderengine::mock::RenderEngine();
129 Hwc2::mock::Composer* mComposer = nullptr;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200130
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500131 mock::VsyncController* mVsyncController = new mock::VsyncController;
132 mock::VSyncTracker* mVSyncTracker = new mock::VSyncTracker;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200133 mock::EventThread* mEventThread = new mock::EventThread;
134 mock::EventThread* mSFEventThread = new mock::EventThread;
135
136 // These mocks are created only when expected to be created via a factory.
137 sp<mock::GraphicBufferConsumer> mConsumer;
138 sp<mock::GraphicBufferProducer> mProducer;
139 surfaceflinger::mock::NativeWindowSurface* mNativeWindowSurface = nullptr;
140
141protected:
142 DisplayTransactionTest();
143};
144
Marin Shalamanova903d032020-12-29 20:35:13 +0100145constexpr int32_t DEFAULT_VSYNC_PERIOD = 16'666'667;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200146constexpr int32_t DEFAULT_DPI = 320;
147constexpr int DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT = HAL_PIXEL_FORMAT_RGB_565;
148
149constexpr int POWER_MODE_LEET = 1337; // An out of range power mode value
150
151/* ------------------------------------------------------------------------
152 * Boolean avoidance
153 *
154 * To make calls and template instantiations more readable, we define some
155 * local enums along with an implicit bool conversion.
156 */
157
158#define BOOL_SUBSTITUTE(TYPENAME) enum class TYPENAME : bool { FALSE = false, TRUE = true };
159
160BOOL_SUBSTITUTE(Async);
161BOOL_SUBSTITUTE(Critical);
162BOOL_SUBSTITUTE(Primary);
163BOOL_SUBSTITUTE(Secure);
164BOOL_SUBSTITUTE(Virtual);
165
166template <typename PhysicalDisplay>
167struct PhysicalDisplayIdType {};
168
169template <uint64_t displayId>
170using HalVirtualDisplayIdType = std::integral_constant<uint64_t, displayId>;
171
172struct GpuVirtualDisplayIdType {};
173
174template <typename>
175struct IsPhysicalDisplayId : std::bool_constant<false> {};
176
177template <typename PhysicalDisplay>
178struct IsPhysicalDisplayId<PhysicalDisplayIdType<PhysicalDisplay>> : std::bool_constant<true> {};
179
180template <typename>
181struct DisplayIdGetter;
182
183template <typename PhysicalDisplay>
184struct DisplayIdGetter<PhysicalDisplayIdType<PhysicalDisplay>> {
185 static PhysicalDisplayId get() {
186 if (!PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
187 return PhysicalDisplayId::fromPort(static_cast<bool>(PhysicalDisplay::PRIMARY)
188 ? LEGACY_DISPLAY_TYPE_PRIMARY
189 : LEGACY_DISPLAY_TYPE_EXTERNAL);
190 }
191
192 const auto info =
193 parseDisplayIdentificationData(PhysicalDisplay::PORT,
194 PhysicalDisplay::GET_IDENTIFICATION_DATA());
195 return info ? info->id : PhysicalDisplayId::fromPort(PhysicalDisplay::PORT);
196 }
197};
198
199template <uint64_t displayId>
200struct DisplayIdGetter<HalVirtualDisplayIdType<displayId>> {
201 static HalVirtualDisplayId get() { return HalVirtualDisplayId(displayId); }
202};
203
204template <>
205struct DisplayIdGetter<GpuVirtualDisplayIdType> {
206 static GpuVirtualDisplayId get() { return GpuVirtualDisplayId(0); }
207};
208
209template <typename>
210struct DisplayConnectionTypeGetter {
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100211 static constexpr std::optional<ui::DisplayConnectionType> value;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200212};
213
214template <typename PhysicalDisplay>
215struct DisplayConnectionTypeGetter<PhysicalDisplayIdType<PhysicalDisplay>> {
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100216 static constexpr std::optional<ui::DisplayConnectionType> value =
217 PhysicalDisplay::CONNECTION_TYPE;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200218};
219
220template <typename>
221struct HwcDisplayIdGetter {
222 static constexpr std::optional<HWDisplayId> value;
223};
224
225constexpr HWDisplayId HWC_VIRTUAL_DISPLAY_HWC_DISPLAY_ID = 1010;
226
227template <uint64_t displayId>
228struct HwcDisplayIdGetter<HalVirtualDisplayIdType<displayId>> {
229 static constexpr std::optional<HWDisplayId> value = HWC_VIRTUAL_DISPLAY_HWC_DISPLAY_ID;
230};
231
232template <typename PhysicalDisplay>
233struct HwcDisplayIdGetter<PhysicalDisplayIdType<PhysicalDisplay>> {
234 static constexpr std::optional<HWDisplayId> value = PhysicalDisplay::HWC_DISPLAY_ID;
235};
236
237// DisplayIdType can be:
238// 1) PhysicalDisplayIdType<...> for generated ID of physical display backed by HWC.
239// 2) HalVirtualDisplayIdType<...> for hard-coded ID of virtual display backed by HWC.
240// 3) GpuVirtualDisplayIdType for virtual display without HWC backing.
241template <typename DisplayIdType, int width, int height, Critical critical, Async async,
Vishnu Naira119aaa2021-09-24 07:19:35 -0700242 Secure secure, Primary primary, int grallocUsage, int displayFlags>
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200243struct DisplayVariant {
244 using DISPLAY_ID = DisplayIdGetter<DisplayIdType>;
245 using CONNECTION_TYPE = DisplayConnectionTypeGetter<DisplayIdType>;
246 using HWC_DISPLAY_ID_OPT = HwcDisplayIdGetter<DisplayIdType>;
247
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200248 static constexpr int WIDTH = width;
249 static constexpr int HEIGHT = height;
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800250 static constexpr ui::Size RESOLUTION{WIDTH, HEIGHT};
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200251
252 static constexpr int GRALLOC_USAGE = grallocUsage;
253
254 // Whether the display is virtual or physical
255 static constexpr Virtual VIRTUAL =
256 IsPhysicalDisplayId<DisplayIdType>{} ? Virtual::FALSE : Virtual::TRUE;
257
258 // When creating native window surfaces for the framebuffer, whether those should be critical
259 static constexpr Critical CRITICAL = critical;
260
261 // When creating native window surfaces for the framebuffer, whether those should be async
262 static constexpr Async ASYNC = async;
263
264 // Whether the display should be treated as secure
265 static constexpr Secure SECURE = secure;
266
267 // Whether the display is primary
268 static constexpr Primary PRIMARY = primary;
269
Vishnu Naira119aaa2021-09-24 07:19:35 -0700270 static constexpr int DISPLAY_FLAGS = displayFlags;
271
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200272 static auto makeFakeExistingDisplayInjector(DisplayTransactionTest* test) {
273 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder();
Dominik Laskowski13948602021-03-08 20:48:28 -0800274 ceDisplayArgs.setId(DISPLAY_ID::get())
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800275 .setPixels(RESOLUTION)
Dominik Laskowski13948602021-03-08 20:48:28 -0800276 .setPowerAdvisor(&test->mPowerAdvisor);
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200277
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200278 auto compositionDisplay =
279 compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
280 ceDisplayArgs.build());
281
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200282 auto injector =
283 TestableSurfaceFlinger::FakeDisplayDeviceInjector(test->mFlinger,
284 compositionDisplay,
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700285 CONNECTION_TYPE::value,
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200286 HWC_DISPLAY_ID_OPT::value,
287 static_cast<bool>(PRIMARY));
288
289 injector.setSecure(static_cast<bool>(SECURE));
290 injector.setNativeWindow(test->mNativeWindow);
291
292 // Creating a DisplayDevice requires getting default dimensions from the
293 // native window along with some other initial setup.
294 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
295 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
296 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
297 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
298 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT))
299 .WillRepeatedly(Return(0));
300 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT))
301 .WillRepeatedly(Return(0));
302 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64))
303 .WillRepeatedly(Return(0));
304 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT))
305 .WillRepeatedly(Return(0));
306
307 return injector;
308 }
309
310 // Called by tests to set up any native window creation call expectations.
311 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
312 EXPECT_CALL(*test->mNativeWindowSurface, getNativeWindow())
313 .WillOnce(Return(test->mNativeWindow));
314
315 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
316 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
317 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
318 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
319 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT))
320 .WillRepeatedly(Return(0));
321 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT))
322 .WillRepeatedly(Return(0));
323 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64))
324 .WillRepeatedly(Return(0));
325 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT))
326 .WillRepeatedly(Return(0));
327 }
328
329 static void setupFramebufferConsumerBufferQueueCallExpectations(DisplayTransactionTest* test) {
330 EXPECT_CALL(*test->mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR));
331 EXPECT_CALL(*test->mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR));
332 EXPECT_CALL(*test->mConsumer, setConsumerUsageBits(GRALLOC_USAGE))
333 .WillRepeatedly(Return(NO_ERROR));
334 EXPECT_CALL(*test->mConsumer, setDefaultBufferSize(WIDTH, HEIGHT))
335 .WillRepeatedly(Return(NO_ERROR));
336 EXPECT_CALL(*test->mConsumer, setMaxAcquiredBufferCount(_))
337 .WillRepeatedly(Return(NO_ERROR));
338 }
339
340 static void setupFramebufferProducerBufferQueueCallExpectations(DisplayTransactionTest* test) {
341 EXPECT_CALL(*test->mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return());
342 }
343};
344
345template <HWDisplayId hwcDisplayId, DisplayType hwcDisplayType, typename DisplayVariant,
346 typename PhysicalDisplay = void>
347struct HwcDisplayVariant {
348 // The display id supplied by the HWC
349 static constexpr HWDisplayId HWC_DISPLAY_ID = hwcDisplayId;
350
351 // The HWC display type
352 static constexpr DisplayType HWC_DISPLAY_TYPE = hwcDisplayType;
353
354 // The HWC active configuration id
Marin Shalamanov6e840172020-12-14 22:13:28 +0100355 static constexpr hal::HWConfigId HWC_ACTIVE_CONFIG_ID = 2001;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200356 static constexpr PowerMode INIT_POWER_MODE = hal::PowerMode::ON;
357
358 static void injectPendingHotplugEvent(DisplayTransactionTest* test, Connection connection) {
359 test->mFlinger.mutablePendingHotplugEvents().emplace_back(
360 TestableSurfaceFlinger::HotplugEvent{HWC_DISPLAY_ID, connection});
361 }
362
363 // Called by tests to inject a HWC display setup
Dominik Laskowskic132a152022-11-30 15:44:52 -0500364 template <bool kInitPowerMode = true>
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200365 static void injectHwcDisplayWithNoDefaultCapabilities(DisplayTransactionTest* test) {
366 const auto displayId = DisplayVariant::DISPLAY_ID::get();
367 ASSERT_FALSE(GpuVirtualDisplayId::tryCast(displayId));
368 TestableSurfaceFlinger::FakeHwcDisplayInjector(displayId, HWC_DISPLAY_TYPE,
369 static_cast<bool>(DisplayVariant::PRIMARY))
370 .setHwcDisplayId(HWC_DISPLAY_ID)
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800371 .setResolution(DisplayVariant::RESOLUTION)
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200372 .setActiveConfig(HWC_ACTIVE_CONFIG_ID)
Dominik Laskowskic132a152022-11-30 15:44:52 -0500373 .setPowerMode(kInitPowerMode ? std::make_optional(INIT_POWER_MODE) : std::nullopt)
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200374 .inject(&test->mFlinger, test->mComposer);
375 }
376
377 // Called by tests to inject a HWC display setup
Dominik Laskowskic132a152022-11-30 15:44:52 -0500378 template <bool kInitPowerMode = true>
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200379 static void injectHwcDisplay(DisplayTransactionTest* test) {
380 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(HWC_DISPLAY_ID, _))
381 .WillOnce(DoAll(SetArgPointee<1>(std::vector<DisplayCapability>({})),
382 Return(Error::NONE)));
Dominik Laskowskic132a152022-11-30 15:44:52 -0500383 if constexpr (kInitPowerMode) {
384 EXPECT_CALL(*test->mComposer, setPowerMode(HWC_DISPLAY_ID, INIT_POWER_MODE))
385 .WillOnce(Return(Error::NONE));
386 }
387 injectHwcDisplayWithNoDefaultCapabilities<kInitPowerMode>(test);
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200388 }
389
390 static std::shared_ptr<compositionengine::Display> injectCompositionDisplay(
391 DisplayTransactionTest* test) {
392 const ::testing::TestInfo* const test_info =
393 ::testing::UnitTest::GetInstance()->current_test_info();
394
395 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder()
Dominik Laskowski13948602021-03-08 20:48:28 -0800396 .setId(DisplayVariant::DISPLAY_ID::get())
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800397 .setPixels(DisplayVariant::RESOLUTION)
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200398 .setIsSecure(static_cast<bool>(DisplayVariant::SECURE))
399 .setPowerAdvisor(&test->mPowerAdvisor)
400 .setName(std::string("Injected display for ") +
401 test_info->test_case_name() + "." + test_info->name())
402 .build();
403
404 return compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
405 ceDisplayArgs);
406 }
407
Marin Shalamanov6e840172020-12-14 22:13:28 +0100408 static void setupHwcGetConfigsCallExpectations(DisplayTransactionTest* test) {
409 if (HWC_DISPLAY_TYPE == DisplayType::PHYSICAL) {
410 EXPECT_CALL(*test->mComposer, getDisplayConfigs(HWC_DISPLAY_ID, _))
411 .WillRepeatedly(DoAll(SetArgPointee<1>(std::vector<hal::HWConfigId>{
412 HWC_ACTIVE_CONFIG_ID}),
413 Return(Error::NONE)));
414 EXPECT_CALL(*test->mComposer,
415 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
416 IComposerClient::Attribute::WIDTH, _))
417 .WillRepeatedly(
418 DoAll(SetArgPointee<3>(DisplayVariant::WIDTH), Return(Error::NONE)));
419 EXPECT_CALL(*test->mComposer,
420 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
421 IComposerClient::Attribute::HEIGHT, _))
422 .WillRepeatedly(
423 DoAll(SetArgPointee<3>(DisplayVariant::HEIGHT), Return(Error::NONE)));
424 EXPECT_CALL(*test->mComposer,
425 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
426 IComposerClient::Attribute::VSYNC_PERIOD, _))
427 .WillRepeatedly(
Marin Shalamanova903d032020-12-29 20:35:13 +0100428 DoAll(SetArgPointee<3>(DEFAULT_VSYNC_PERIOD), Return(Error::NONE)));
Marin Shalamanov6e840172020-12-14 22:13:28 +0100429 EXPECT_CALL(*test->mComposer,
430 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
431 IComposerClient::Attribute::DPI_X, _))
432 .WillRepeatedly(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
433 EXPECT_CALL(*test->mComposer,
434 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
435 IComposerClient::Attribute::DPI_Y, _))
436 .WillRepeatedly(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
437 EXPECT_CALL(*test->mComposer,
438 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
439 IComposerClient::Attribute::CONFIG_GROUP, _))
440 .WillRepeatedly(DoAll(SetArgPointee<3>(-1), Return(Error::NONE)));
441 } else {
442 EXPECT_CALL(*test->mComposer, getDisplayConfigs(_, _)).Times(0);
443 EXPECT_CALL(*test->mComposer, getDisplayAttribute(_, _, _, _)).Times(0);
444 }
445 }
446
Dominik Laskowskibab51282022-08-12 09:28:55 -0700447 template <bool kFailedHotplug = false>
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200448 static void setupHwcHotplugCallExpectations(DisplayTransactionTest* test) {
Dominik Laskowskibab51282022-08-12 09:28:55 -0700449 if constexpr (!kFailedHotplug) {
450 constexpr auto CONNECTION_TYPE =
451 PhysicalDisplay::CONNECTION_TYPE == ui::DisplayConnectionType::Internal
452 ? IComposerClient::DisplayConnectionType::INTERNAL
453 : IComposerClient::DisplayConnectionType::EXTERNAL;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200454
Dominik Laskowskibab51282022-08-12 09:28:55 -0700455 EXPECT_CALL(*test->mComposer, getDisplayConnectionType(HWC_DISPLAY_ID, _))
456 .WillOnce(DoAll(SetArgPointee<1>(CONNECTION_TYPE),
457 Return(hal::V2_4::Error::NONE)));
458 }
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200459
460 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_))
461 .WillOnce(Return(hal::Error::NONE));
Marin Shalamanov6e840172020-12-14 22:13:28 +0100462
463 setupHwcGetConfigsCallExpectations(test);
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200464
465 if (PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
466 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
467 .WillOnce(DoAll(SetArgPointee<1>(PhysicalDisplay::PORT),
468 SetArgPointee<2>(PhysicalDisplay::GET_IDENTIFICATION_DATA()),
469 Return(Error::NONE)));
470 } else {
471 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
472 .WillOnce(Return(Error::UNSUPPORTED));
473 }
474 }
475
476 // Called by tests to set up HWC call expectations
477 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
478 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY_ID, _))
479 .WillRepeatedly(DoAll(SetArgPointee<1>(HWC_ACTIVE_CONFIG_ID), Return(Error::NONE)));
480 }
481};
482
483// Physical displays are expected to be synchronous, secure, and have a HWC display for output.
484constexpr uint32_t GRALLOC_USAGE_PHYSICAL_DISPLAY =
485 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
486
Vishnu Naira119aaa2021-09-24 07:19:35 -0700487constexpr int PHYSICAL_DISPLAY_FLAGS = 0x1;
488
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200489template <typename PhysicalDisplay, int width, int height, Critical critical>
490struct PhysicalDisplayVariant
491 : DisplayVariant<PhysicalDisplayIdType<PhysicalDisplay>, width, height, critical,
492 Async::FALSE, Secure::TRUE, PhysicalDisplay::PRIMARY,
Vishnu Naira119aaa2021-09-24 07:19:35 -0700493 GRALLOC_USAGE_PHYSICAL_DISPLAY, PHYSICAL_DISPLAY_FLAGS>,
494 HwcDisplayVariant<
495 PhysicalDisplay::HWC_DISPLAY_ID, DisplayType::PHYSICAL,
496 DisplayVariant<PhysicalDisplayIdType<PhysicalDisplay>, width, height, critical,
497 Async::FALSE, Secure::TRUE, PhysicalDisplay::PRIMARY,
498 GRALLOC_USAGE_PHYSICAL_DISPLAY, PHYSICAL_DISPLAY_FLAGS>,
499 PhysicalDisplay> {};
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200500
501template <bool hasIdentificationData>
502struct PrimaryDisplay {
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100503 static constexpr auto CONNECTION_TYPE = ui::DisplayConnectionType::Internal;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200504 static constexpr Primary PRIMARY = Primary::TRUE;
505 static constexpr uint8_t PORT = 255;
506 static constexpr HWDisplayId HWC_DISPLAY_ID = 1001;
507 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
508 static constexpr auto GET_IDENTIFICATION_DATA = getInternalEdid;
509};
510
511template <bool hasIdentificationData>
512struct ExternalDisplay {
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100513 static constexpr auto CONNECTION_TYPE = ui::DisplayConnectionType::External;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200514 static constexpr Primary PRIMARY = Primary::FALSE;
515 static constexpr uint8_t PORT = 254;
516 static constexpr HWDisplayId HWC_DISPLAY_ID = 1002;
517 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
518 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
519};
520
521struct TertiaryDisplay {
522 static constexpr Primary PRIMARY = Primary::FALSE;
523 static constexpr uint8_t PORT = 253;
524 static constexpr HWDisplayId HWC_DISPLAY_ID = 1003;
525 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
526};
527
528// A primary display is a physical display that is critical
529using PrimaryDisplayVariant =
530 PhysicalDisplayVariant<PrimaryDisplay<false>, 3840, 2160, Critical::TRUE>;
531
532// An external display is physical display that is not critical.
533using ExternalDisplayVariant =
534 PhysicalDisplayVariant<ExternalDisplay<false>, 1920, 1280, Critical::FALSE>;
535
536using TertiaryDisplayVariant = PhysicalDisplayVariant<TertiaryDisplay, 1600, 1200, Critical::FALSE>;
537
538// A virtual display not supported by the HWC.
539constexpr uint32_t GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY = 0;
540
Vishnu Naira119aaa2021-09-24 07:19:35 -0700541constexpr int VIRTUAL_DISPLAY_FLAGS = 0x0;
542
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200543template <int width, int height, Secure secure>
544struct NonHwcVirtualDisplayVariant
545 : DisplayVariant<GpuVirtualDisplayIdType, width, height, Critical::FALSE, Async::TRUE, secure,
Vishnu Naira119aaa2021-09-24 07:19:35 -0700546 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY,
547 VIRTUAL_DISPLAY_FLAGS> {
548 using Base = DisplayVariant<GpuVirtualDisplayIdType, width, height, Critical::FALSE,
549 Async::TRUE, secure, Primary::FALSE,
550 GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY, VIRTUAL_DISPLAY_FLAGS>;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200551
552 static void injectHwcDisplay(DisplayTransactionTest*) {}
553
554 static std::shared_ptr<compositionengine::Display> injectCompositionDisplay(
555 DisplayTransactionTest* test) {
556 const ::testing::TestInfo* const test_info =
557 ::testing::UnitTest::GetInstance()->current_test_info();
558
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200559 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder()
Dominik Laskowski13948602021-03-08 20:48:28 -0800560 .setId(Base::DISPLAY_ID::get())
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800561 .setPixels(Base::RESOLUTION)
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200562 .setIsSecure(static_cast<bool>(Base::SECURE))
563 .setPowerAdvisor(&test->mPowerAdvisor)
564 .setName(std::string("Injected display for ") +
565 test_info->test_case_name() + "." + test_info->name())
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200566 .build();
567
568 return compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
569 ceDisplayArgs);
570 }
571
Marin Shalamanov6e840172020-12-14 22:13:28 +0100572 static void setupHwcGetConfigsCallExpectations(DisplayTransactionTest* test) {
573 EXPECT_CALL(*test->mComposer, getDisplayConfigs(_, _)).Times(0);
574 EXPECT_CALL(*test->mComposer, getDisplayAttribute(_, _, _, _)).Times(0);
575 }
576
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200577 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
578 EXPECT_CALL(*test->mComposer, getActiveConfig(_, _)).Times(0);
579 }
580
581 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
582 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
583 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
584 }
585};
586
587// A virtual display supported by the HWC.
588constexpr uint32_t GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY = GRALLOC_USAGE_HW_COMPOSER;
589
590template <int width, int height, Secure secure>
591struct HwcVirtualDisplayVariant
592 : DisplayVariant<HalVirtualDisplayIdType<42>, width, height, Critical::FALSE, Async::TRUE,
Vishnu Naira119aaa2021-09-24 07:19:35 -0700593 secure, Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY,
594 VIRTUAL_DISPLAY_FLAGS>,
595 HwcDisplayVariant<
596 HWC_VIRTUAL_DISPLAY_HWC_DISPLAY_ID, DisplayType::VIRTUAL,
597 DisplayVariant<HalVirtualDisplayIdType<42>, width, height, Critical::FALSE,
598 Async::TRUE, secure, Primary::FALSE,
599 GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY, VIRTUAL_DISPLAY_FLAGS>> {
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200600 using Base = DisplayVariant<HalVirtualDisplayIdType<42>, width, height, Critical::FALSE,
Vishnu Naira119aaa2021-09-24 07:19:35 -0700601 Async::TRUE, secure, Primary::FALSE, GRALLOC_USAGE_HW_COMPOSER,
602 VIRTUAL_DISPLAY_FLAGS>;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200603 using Self = HwcVirtualDisplayVariant<width, height, secure>;
604
605 static std::shared_ptr<compositionengine::Display> injectCompositionDisplay(
606 DisplayTransactionTest* test) {
607 const ::testing::TestInfo* const test_info =
608 ::testing::UnitTest::GetInstance()->current_test_info();
609
Dominik Laskowski13948602021-03-08 20:48:28 -0800610 const auto displayId = Base::DISPLAY_ID::get();
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200611 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder()
Dominik Laskowski13948602021-03-08 20:48:28 -0800612 .setId(displayId)
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800613 .setPixels(Base::RESOLUTION)
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200614 .setIsSecure(static_cast<bool>(Base::SECURE))
615 .setPowerAdvisor(&test->mPowerAdvisor)
616 .setName(std::string("Injected display for ") +
617 test_info->test_case_name() + "." + test_info->name())
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200618 .build();
619
620 auto compositionDisplay =
621 compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
622 ceDisplayArgs);
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200623
624 // Insert display data so that the HWC thinks it created the virtual display.
Dominik Laskowski13948602021-03-08 20:48:28 -0800625 test->mFlinger.mutableHwcDisplayData().try_emplace(displayId);
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200626
627 return compositionDisplay;
628 }
629
630 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
631 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
632 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
633 }
634
635 static void setupHwcVirtualDisplayCreationCallExpectations(DisplayTransactionTest* test) {
Dominik Laskowski263eec42021-07-21 23:13:24 -0700636 EXPECT_CALL(*test->mComposer, createVirtualDisplay(Base::WIDTH, Base::HEIGHT, _, _))
637 .WillOnce(DoAll(SetArgPointee<3>(Self::HWC_DISPLAY_ID), Return(Error::NONE)));
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200638 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
639 }
640};
641
642// For this variant, the display is not a HWC display, so no HDR support should
643// be configured.
644struct NonHwcDisplayHdrSupportVariant {
645 static constexpr bool HDR10_PLUS_SUPPORTED = false;
646 static constexpr bool HDR10_SUPPORTED = false;
647 static constexpr bool HDR_HLG_SUPPORTED = false;
648 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
649 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
650 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
651 }
652};
653
654// For this variant, the composer should respond with am empty list of HDR
655// modes, so no HDR support should be configured.
656template <typename Display>
657struct HdrNotSupportedVariant {
658 static constexpr bool HDR10_PLUS_SUPPORTED = false;
659 static constexpr bool HDR10_SUPPORTED = false;
660 static constexpr bool HDR_HLG_SUPPORTED = false;
661 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
662 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
663 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
664 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
665 }
666};
667
668struct NonHwcPerFrameMetadataSupportVariant {
669 static constexpr int PER_FRAME_METADATA_KEYS = 0;
670 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
671 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(_)).Times(0);
672 }
673};
674
675template <typename Display>
676struct NoPerFrameMetadataSupportVariant {
677 static constexpr int PER_FRAME_METADATA_KEYS = 0;
678 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
679 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
680 .WillOnce(Return(std::vector<PerFrameMetadataKey>()));
681 }
682};
683
684// For this variant, SurfaceFlinger should configure itself with wide display
685// support, but the display should respond with an empty list of supported color
686// modes. Wide-color support for the display should not be configured.
687template <typename Display>
688struct WideColorNotSupportedVariant {
689 static constexpr bool WIDE_COLOR_SUPPORTED = false;
690
691 static void injectConfigChange(DisplayTransactionTest* test) {
Dominik Laskowski788945b2022-08-30 12:10:56 -0700692 test->mFlinger.mutableSupportsWideColor() = true;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200693 }
694
695 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200696 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
697 }
698};
699
700// For this variant, SurfaceFlinger should not configure itself with wide
701// display support, so the display should not be configured for wide-color
702// support.
703struct WideColorSupportNotConfiguredVariant {
704 static constexpr bool WIDE_COLOR_SUPPORTED = false;
705
706 static void injectConfigChange(DisplayTransactionTest* test) {
Dominik Laskowski788945b2022-08-30 12:10:56 -0700707 test->mFlinger.mutableSupportsWideColor() = false;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200708 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged;
709 }
710
711 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200712 EXPECT_CALL(*test->mComposer, getRenderIntents(_, _, _)).Times(0);
713 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
714 }
715};
716
717/* ------------------------------------------------------------------------
718 * Typical display configurations to test
719 */
720
721template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy,
722 typename PerFrameMetadataSupportPolicy>
723struct Case {
724 using Display = DisplayPolicy;
725 using WideColorSupport = WideColorSupportPolicy;
726 using HdrSupport = HdrSupportPolicy;
727 using PerFrameMetadataSupport = PerFrameMetadataSupportPolicy;
728};
729
730using SimplePrimaryDisplayCase =
731 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
732 HdrNotSupportedVariant<PrimaryDisplayVariant>,
733 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
734using SimpleExternalDisplayCase =
735 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
736 HdrNotSupportedVariant<ExternalDisplayVariant>,
737 NoPerFrameMetadataSupportVariant<ExternalDisplayVariant>>;
738using SimpleTertiaryDisplayCase =
739 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
740 HdrNotSupportedVariant<TertiaryDisplayVariant>,
741 NoPerFrameMetadataSupportVariant<TertiaryDisplayVariant>>;
742
743using NonHwcVirtualDisplayCase =
744 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
745 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant,
746 NonHwcPerFrameMetadataSupportVariant>;
747using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
748using HwcVirtualDisplayCase =
749 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
750 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>,
751 NoPerFrameMetadataSupportVariant<SimpleHwcVirtualDisplayVariant>>;
752
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800753inline DisplayModePtr createDisplayMode(DisplayModeId modeId, Fps refreshRate, int32_t group = 0,
754 ui::Size resolution = ui::Size(1920, 1080)) {
755 return mock::createDisplayMode(modeId, refreshRate, group, resolution,
756 PrimaryDisplayVariant::DISPLAY_ID::get());
757}
758
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200759} // namespace android
760
761// TODO(b/129481165): remove the #pragma below and fix conversion issues
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100762#pragma clang diagnostic pop // ignored "-Wconversion -Wextra"