blob: e64cb38b16493f7b14098d565fbf4b12eb6fb0f1 [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
Dominik Laskowskiaee9a622023-02-11 14:24:19 -050088 void injectMockScheduler(PhysicalDisplayId);
Marin Shalamanov07b1ff32020-10-07 16:57:22 +020089 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
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200131 mock::EventThread* mEventThread = new mock::EventThread;
132 mock::EventThread* mSFEventThread = new mock::EventThread;
133
134 // These mocks are created only when expected to be created via a factory.
135 sp<mock::GraphicBufferConsumer> mConsumer;
136 sp<mock::GraphicBufferProducer> mProducer;
137 surfaceflinger::mock::NativeWindowSurface* mNativeWindowSurface = nullptr;
138
139protected:
Dominik Laskowskiaee9a622023-02-11 14:24:19 -0500140 DisplayTransactionTest(bool withMockScheduler = true);
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200141};
142
Marin Shalamanova903d032020-12-29 20:35:13 +0100143constexpr int32_t DEFAULT_VSYNC_PERIOD = 16'666'667;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200144constexpr int32_t DEFAULT_DPI = 320;
145constexpr int DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT = HAL_PIXEL_FORMAT_RGB_565;
146
147constexpr int POWER_MODE_LEET = 1337; // An out of range power mode value
148
149/* ------------------------------------------------------------------------
150 * Boolean avoidance
151 *
152 * To make calls and template instantiations more readable, we define some
153 * local enums along with an implicit bool conversion.
154 */
155
156#define BOOL_SUBSTITUTE(TYPENAME) enum class TYPENAME : bool { FALSE = false, TRUE = true };
157
158BOOL_SUBSTITUTE(Async);
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200159BOOL_SUBSTITUTE(Primary);
160BOOL_SUBSTITUTE(Secure);
161BOOL_SUBSTITUTE(Virtual);
162
163template <typename PhysicalDisplay>
164struct PhysicalDisplayIdType {};
165
166template <uint64_t displayId>
167using HalVirtualDisplayIdType = std::integral_constant<uint64_t, displayId>;
168
169struct GpuVirtualDisplayIdType {};
170
171template <typename>
172struct IsPhysicalDisplayId : std::bool_constant<false> {};
173
174template <typename PhysicalDisplay>
175struct IsPhysicalDisplayId<PhysicalDisplayIdType<PhysicalDisplay>> : std::bool_constant<true> {};
176
177template <typename>
178struct DisplayIdGetter;
179
180template <typename PhysicalDisplay>
181struct DisplayIdGetter<PhysicalDisplayIdType<PhysicalDisplay>> {
182 static PhysicalDisplayId get() {
183 if (!PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
184 return PhysicalDisplayId::fromPort(static_cast<bool>(PhysicalDisplay::PRIMARY)
185 ? LEGACY_DISPLAY_TYPE_PRIMARY
186 : LEGACY_DISPLAY_TYPE_EXTERNAL);
187 }
188
189 const auto info =
190 parseDisplayIdentificationData(PhysicalDisplay::PORT,
191 PhysicalDisplay::GET_IDENTIFICATION_DATA());
192 return info ? info->id : PhysicalDisplayId::fromPort(PhysicalDisplay::PORT);
193 }
194};
195
196template <uint64_t displayId>
197struct DisplayIdGetter<HalVirtualDisplayIdType<displayId>> {
198 static HalVirtualDisplayId get() { return HalVirtualDisplayId(displayId); }
199};
200
201template <>
202struct DisplayIdGetter<GpuVirtualDisplayIdType> {
203 static GpuVirtualDisplayId get() { return GpuVirtualDisplayId(0); }
204};
205
206template <typename>
207struct DisplayConnectionTypeGetter {
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100208 static constexpr std::optional<ui::DisplayConnectionType> value;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200209};
210
211template <typename PhysicalDisplay>
212struct DisplayConnectionTypeGetter<PhysicalDisplayIdType<PhysicalDisplay>> {
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100213 static constexpr std::optional<ui::DisplayConnectionType> value =
214 PhysicalDisplay::CONNECTION_TYPE;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200215};
216
217template <typename>
218struct HwcDisplayIdGetter {
219 static constexpr std::optional<HWDisplayId> value;
220};
221
222constexpr HWDisplayId HWC_VIRTUAL_DISPLAY_HWC_DISPLAY_ID = 1010;
223
224template <uint64_t displayId>
225struct HwcDisplayIdGetter<HalVirtualDisplayIdType<displayId>> {
226 static constexpr std::optional<HWDisplayId> value = HWC_VIRTUAL_DISPLAY_HWC_DISPLAY_ID;
227};
228
229template <typename PhysicalDisplay>
230struct HwcDisplayIdGetter<PhysicalDisplayIdType<PhysicalDisplay>> {
231 static constexpr std::optional<HWDisplayId> value = PhysicalDisplay::HWC_DISPLAY_ID;
232};
233
234// DisplayIdType can be:
235// 1) PhysicalDisplayIdType<...> for generated ID of physical display backed by HWC.
236// 2) HalVirtualDisplayIdType<...> for hard-coded ID of virtual display backed by HWC.
237// 3) GpuVirtualDisplayIdType for virtual display without HWC backing.
Dominik Laskowskie80bd312023-02-11 16:23:34 -0500238template <typename DisplayIdType, int width, int height, Async async, Secure secure,
239 Primary primary, int grallocUsage, int displayFlags>
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200240struct DisplayVariant {
241 using DISPLAY_ID = DisplayIdGetter<DisplayIdType>;
242 using CONNECTION_TYPE = DisplayConnectionTypeGetter<DisplayIdType>;
243 using HWC_DISPLAY_ID_OPT = HwcDisplayIdGetter<DisplayIdType>;
244
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200245 static constexpr int WIDTH = width;
246 static constexpr int HEIGHT = height;
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800247 static constexpr ui::Size RESOLUTION{WIDTH, HEIGHT};
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200248
249 static constexpr int GRALLOC_USAGE = grallocUsage;
250
251 // Whether the display is virtual or physical
252 static constexpr Virtual VIRTUAL =
253 IsPhysicalDisplayId<DisplayIdType>{} ? Virtual::FALSE : Virtual::TRUE;
254
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200255 // When creating native window surfaces for the framebuffer, whether those should be async
256 static constexpr Async ASYNC = async;
257
258 // Whether the display should be treated as secure
259 static constexpr Secure SECURE = secure;
260
261 // Whether the display is primary
262 static constexpr Primary PRIMARY = primary;
263
Vishnu Naira119aaa2021-09-24 07:19:35 -0700264 static constexpr int DISPLAY_FLAGS = displayFlags;
265
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200266 static auto makeFakeExistingDisplayInjector(DisplayTransactionTest* test) {
267 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder();
Dominik Laskowski13948602021-03-08 20:48:28 -0800268 ceDisplayArgs.setId(DISPLAY_ID::get())
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800269 .setPixels(RESOLUTION)
Dominik Laskowski13948602021-03-08 20:48:28 -0800270 .setPowerAdvisor(&test->mPowerAdvisor);
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200271
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200272 auto compositionDisplay =
273 compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
274 ceDisplayArgs.build());
275
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200276 auto injector =
277 TestableSurfaceFlinger::FakeDisplayDeviceInjector(test->mFlinger,
278 compositionDisplay,
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700279 CONNECTION_TYPE::value,
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200280 HWC_DISPLAY_ID_OPT::value,
281 static_cast<bool>(PRIMARY));
282
283 injector.setSecure(static_cast<bool>(SECURE));
284 injector.setNativeWindow(test->mNativeWindow);
285
286 // Creating a DisplayDevice requires getting default dimensions from the
287 // native window along with some other initial setup.
288 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
289 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
290 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
291 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
292 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT))
293 .WillRepeatedly(Return(0));
294 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT))
295 .WillRepeatedly(Return(0));
296 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64))
297 .WillRepeatedly(Return(0));
298 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT))
299 .WillRepeatedly(Return(0));
300
301 return injector;
302 }
303
304 // Called by tests to set up any native window creation call expectations.
305 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
306 EXPECT_CALL(*test->mNativeWindowSurface, getNativeWindow())
307 .WillOnce(Return(test->mNativeWindow));
308
309 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
310 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
311 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
312 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
313 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT))
314 .WillRepeatedly(Return(0));
315 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT))
316 .WillRepeatedly(Return(0));
317 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64))
318 .WillRepeatedly(Return(0));
319 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT))
320 .WillRepeatedly(Return(0));
321 }
322
323 static void setupFramebufferConsumerBufferQueueCallExpectations(DisplayTransactionTest* test) {
324 EXPECT_CALL(*test->mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR));
325 EXPECT_CALL(*test->mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR));
326 EXPECT_CALL(*test->mConsumer, setConsumerUsageBits(GRALLOC_USAGE))
327 .WillRepeatedly(Return(NO_ERROR));
328 EXPECT_CALL(*test->mConsumer, setDefaultBufferSize(WIDTH, HEIGHT))
329 .WillRepeatedly(Return(NO_ERROR));
330 EXPECT_CALL(*test->mConsumer, setMaxAcquiredBufferCount(_))
331 .WillRepeatedly(Return(NO_ERROR));
332 }
333
334 static void setupFramebufferProducerBufferQueueCallExpectations(DisplayTransactionTest* test) {
335 EXPECT_CALL(*test->mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return());
336 }
337};
338
339template <HWDisplayId hwcDisplayId, DisplayType hwcDisplayType, typename DisplayVariant,
340 typename PhysicalDisplay = void>
341struct HwcDisplayVariant {
342 // The display id supplied by the HWC
343 static constexpr HWDisplayId HWC_DISPLAY_ID = hwcDisplayId;
344
345 // The HWC display type
346 static constexpr DisplayType HWC_DISPLAY_TYPE = hwcDisplayType;
347
348 // The HWC active configuration id
Marin Shalamanov6e840172020-12-14 22:13:28 +0100349 static constexpr hal::HWConfigId HWC_ACTIVE_CONFIG_ID = 2001;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200350 static constexpr PowerMode INIT_POWER_MODE = hal::PowerMode::ON;
351
352 static void injectPendingHotplugEvent(DisplayTransactionTest* test, Connection connection) {
353 test->mFlinger.mutablePendingHotplugEvents().emplace_back(
354 TestableSurfaceFlinger::HotplugEvent{HWC_DISPLAY_ID, connection});
355 }
356
357 // Called by tests to inject a HWC display setup
Dominik Laskowskic132a152022-11-30 15:44:52 -0500358 template <bool kInitPowerMode = true>
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200359 static void injectHwcDisplayWithNoDefaultCapabilities(DisplayTransactionTest* test) {
360 const auto displayId = DisplayVariant::DISPLAY_ID::get();
361 ASSERT_FALSE(GpuVirtualDisplayId::tryCast(displayId));
362 TestableSurfaceFlinger::FakeHwcDisplayInjector(displayId, HWC_DISPLAY_TYPE,
363 static_cast<bool>(DisplayVariant::PRIMARY))
364 .setHwcDisplayId(HWC_DISPLAY_ID)
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800365 .setResolution(DisplayVariant::RESOLUTION)
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200366 .setActiveConfig(HWC_ACTIVE_CONFIG_ID)
Dominik Laskowskic132a152022-11-30 15:44:52 -0500367 .setPowerMode(kInitPowerMode ? std::make_optional(INIT_POWER_MODE) : std::nullopt)
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200368 .inject(&test->mFlinger, test->mComposer);
369 }
370
371 // Called by tests to inject a HWC display setup
Dominik Laskowskic132a152022-11-30 15:44:52 -0500372 template <bool kInitPowerMode = true>
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200373 static void injectHwcDisplay(DisplayTransactionTest* test) {
374 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(HWC_DISPLAY_ID, _))
375 .WillOnce(DoAll(SetArgPointee<1>(std::vector<DisplayCapability>({})),
376 Return(Error::NONE)));
Dominik Laskowskic132a152022-11-30 15:44:52 -0500377 if constexpr (kInitPowerMode) {
378 EXPECT_CALL(*test->mComposer, setPowerMode(HWC_DISPLAY_ID, INIT_POWER_MODE))
379 .WillOnce(Return(Error::NONE));
380 }
381 injectHwcDisplayWithNoDefaultCapabilities<kInitPowerMode>(test);
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200382 }
383
384 static std::shared_ptr<compositionengine::Display> injectCompositionDisplay(
385 DisplayTransactionTest* test) {
386 const ::testing::TestInfo* const test_info =
387 ::testing::UnitTest::GetInstance()->current_test_info();
388
389 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder()
Dominik Laskowski13948602021-03-08 20:48:28 -0800390 .setId(DisplayVariant::DISPLAY_ID::get())
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800391 .setPixels(DisplayVariant::RESOLUTION)
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200392 .setIsSecure(static_cast<bool>(DisplayVariant::SECURE))
393 .setPowerAdvisor(&test->mPowerAdvisor)
394 .setName(std::string("Injected display for ") +
395 test_info->test_case_name() + "." + test_info->name())
396 .build();
397
398 return compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
399 ceDisplayArgs);
400 }
401
Marin Shalamanov6e840172020-12-14 22:13:28 +0100402 static void setupHwcGetConfigsCallExpectations(DisplayTransactionTest* test) {
403 if (HWC_DISPLAY_TYPE == DisplayType::PHYSICAL) {
404 EXPECT_CALL(*test->mComposer, getDisplayConfigs(HWC_DISPLAY_ID, _))
405 .WillRepeatedly(DoAll(SetArgPointee<1>(std::vector<hal::HWConfigId>{
406 HWC_ACTIVE_CONFIG_ID}),
407 Return(Error::NONE)));
408 EXPECT_CALL(*test->mComposer,
409 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
410 IComposerClient::Attribute::WIDTH, _))
411 .WillRepeatedly(
412 DoAll(SetArgPointee<3>(DisplayVariant::WIDTH), Return(Error::NONE)));
413 EXPECT_CALL(*test->mComposer,
414 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
415 IComposerClient::Attribute::HEIGHT, _))
416 .WillRepeatedly(
417 DoAll(SetArgPointee<3>(DisplayVariant::HEIGHT), Return(Error::NONE)));
418 EXPECT_CALL(*test->mComposer,
419 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
420 IComposerClient::Attribute::VSYNC_PERIOD, _))
421 .WillRepeatedly(
Marin Shalamanova903d032020-12-29 20:35:13 +0100422 DoAll(SetArgPointee<3>(DEFAULT_VSYNC_PERIOD), Return(Error::NONE)));
Marin Shalamanov6e840172020-12-14 22:13:28 +0100423 EXPECT_CALL(*test->mComposer,
424 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
425 IComposerClient::Attribute::DPI_X, _))
426 .WillRepeatedly(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
427 EXPECT_CALL(*test->mComposer,
428 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
429 IComposerClient::Attribute::DPI_Y, _))
430 .WillRepeatedly(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
431 EXPECT_CALL(*test->mComposer,
432 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
433 IComposerClient::Attribute::CONFIG_GROUP, _))
434 .WillRepeatedly(DoAll(SetArgPointee<3>(-1), Return(Error::NONE)));
435 } else {
436 EXPECT_CALL(*test->mComposer, getDisplayConfigs(_, _)).Times(0);
437 EXPECT_CALL(*test->mComposer, getDisplayAttribute(_, _, _, _)).Times(0);
438 }
439 }
440
Dominik Laskowskibab51282022-08-12 09:28:55 -0700441 template <bool kFailedHotplug = false>
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200442 static void setupHwcHotplugCallExpectations(DisplayTransactionTest* test) {
Dominik Laskowskibab51282022-08-12 09:28:55 -0700443 if constexpr (!kFailedHotplug) {
444 constexpr auto CONNECTION_TYPE =
445 PhysicalDisplay::CONNECTION_TYPE == ui::DisplayConnectionType::Internal
446 ? IComposerClient::DisplayConnectionType::INTERNAL
447 : IComposerClient::DisplayConnectionType::EXTERNAL;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200448
Dominik Laskowskibab51282022-08-12 09:28:55 -0700449 EXPECT_CALL(*test->mComposer, getDisplayConnectionType(HWC_DISPLAY_ID, _))
450 .WillOnce(DoAll(SetArgPointee<1>(CONNECTION_TYPE),
451 Return(hal::V2_4::Error::NONE)));
452 }
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200453
454 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_))
455 .WillOnce(Return(hal::Error::NONE));
Marin Shalamanov6e840172020-12-14 22:13:28 +0100456
457 setupHwcGetConfigsCallExpectations(test);
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200458
459 if (PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
460 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
461 .WillOnce(DoAll(SetArgPointee<1>(PhysicalDisplay::PORT),
462 SetArgPointee<2>(PhysicalDisplay::GET_IDENTIFICATION_DATA()),
463 Return(Error::NONE)));
464 } else {
465 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
466 .WillOnce(Return(Error::UNSUPPORTED));
467 }
468 }
469
470 // Called by tests to set up HWC call expectations
471 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
472 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY_ID, _))
473 .WillRepeatedly(DoAll(SetArgPointee<1>(HWC_ACTIVE_CONFIG_ID), Return(Error::NONE)));
474 }
475};
476
477// Physical displays are expected to be synchronous, secure, and have a HWC display for output.
478constexpr uint32_t GRALLOC_USAGE_PHYSICAL_DISPLAY =
479 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
480
Vishnu Naira119aaa2021-09-24 07:19:35 -0700481constexpr int PHYSICAL_DISPLAY_FLAGS = 0x1;
482
Dominik Laskowskie80bd312023-02-11 16:23:34 -0500483template <typename PhysicalDisplay, int width, int height>
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200484struct PhysicalDisplayVariant
Dominik Laskowskie80bd312023-02-11 16:23:34 -0500485 : DisplayVariant<PhysicalDisplayIdType<PhysicalDisplay>, width, height, Async::FALSE,
486 Secure::TRUE, PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY,
487 PHYSICAL_DISPLAY_FLAGS>,
488 HwcDisplayVariant<PhysicalDisplay::HWC_DISPLAY_ID, DisplayType::PHYSICAL,
489 DisplayVariant<PhysicalDisplayIdType<PhysicalDisplay>, width, height,
490 Async::FALSE, Secure::TRUE, PhysicalDisplay::PRIMARY,
491 GRALLOC_USAGE_PHYSICAL_DISPLAY, PHYSICAL_DISPLAY_FLAGS>,
492 PhysicalDisplay> {};
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200493
494template <bool hasIdentificationData>
495struct PrimaryDisplay {
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100496 static constexpr auto CONNECTION_TYPE = ui::DisplayConnectionType::Internal;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200497 static constexpr Primary PRIMARY = Primary::TRUE;
498 static constexpr uint8_t PORT = 255;
499 static constexpr HWDisplayId HWC_DISPLAY_ID = 1001;
500 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
501 static constexpr auto GET_IDENTIFICATION_DATA = getInternalEdid;
502};
503
Dominik Laskowski06e5db02023-02-11 16:55:47 -0500504template <ui::DisplayConnectionType connectionType, bool hasIdentificationData>
505struct SecondaryDisplay {
506 static constexpr auto CONNECTION_TYPE = connectionType;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200507 static constexpr Primary PRIMARY = Primary::FALSE;
508 static constexpr uint8_t PORT = 254;
509 static constexpr HWDisplayId HWC_DISPLAY_ID = 1002;
510 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
Dominik Laskowski06e5db02023-02-11 16:55:47 -0500511 static constexpr auto GET_IDENTIFICATION_DATA =
512 connectionType == ui::DisplayConnectionType::Internal ? getInternalEdid
513 : getExternalEdid;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200514};
515
516struct TertiaryDisplay {
517 static constexpr Primary PRIMARY = Primary::FALSE;
518 static constexpr uint8_t PORT = 253;
519 static constexpr HWDisplayId HWC_DISPLAY_ID = 1003;
520 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
521};
522
Dominik Laskowskie80bd312023-02-11 16:23:34 -0500523using PrimaryDisplayVariant = PhysicalDisplayVariant<PrimaryDisplay<false>, 3840, 2160>;
Dominik Laskowski06e5db02023-02-11 16:55:47 -0500524
525using InnerDisplayVariant = PhysicalDisplayVariant<PrimaryDisplay<true>, 1840, 2208>;
526using OuterDisplayVariant =
527 PhysicalDisplayVariant<SecondaryDisplay<ui::DisplayConnectionType::Internal, true>, 1080,
528 2092>;
529
530using ExternalDisplayVariant =
531 PhysicalDisplayVariant<SecondaryDisplay<ui::DisplayConnectionType::External, false>, 1920,
532 1280>;
533
Dominik Laskowskie80bd312023-02-11 16:23:34 -0500534using TertiaryDisplayVariant = PhysicalDisplayVariant<TertiaryDisplay, 1600, 1200>;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200535
536// A virtual display not supported by the HWC.
537constexpr uint32_t GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY = 0;
538
Vishnu Naira119aaa2021-09-24 07:19:35 -0700539constexpr int VIRTUAL_DISPLAY_FLAGS = 0x0;
540
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200541template <int width, int height, Secure secure>
542struct NonHwcVirtualDisplayVariant
Dominik Laskowskie80bd312023-02-11 16:23:34 -0500543 : DisplayVariant<GpuVirtualDisplayIdType, width, height, Async::TRUE, secure, Primary::FALSE,
544 GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY, VIRTUAL_DISPLAY_FLAGS> {
545 using Base = DisplayVariant<GpuVirtualDisplayIdType, width, height, Async::TRUE, secure,
546 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY,
547 VIRTUAL_DISPLAY_FLAGS>;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200548
549 static void injectHwcDisplay(DisplayTransactionTest*) {}
550
551 static std::shared_ptr<compositionengine::Display> injectCompositionDisplay(
552 DisplayTransactionTest* test) {
553 const ::testing::TestInfo* const test_info =
554 ::testing::UnitTest::GetInstance()->current_test_info();
555
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200556 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder()
Dominik Laskowski13948602021-03-08 20:48:28 -0800557 .setId(Base::DISPLAY_ID::get())
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800558 .setPixels(Base::RESOLUTION)
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200559 .setIsSecure(static_cast<bool>(Base::SECURE))
560 .setPowerAdvisor(&test->mPowerAdvisor)
561 .setName(std::string("Injected display for ") +
562 test_info->test_case_name() + "." + test_info->name())
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200563 .build();
564
565 return compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
566 ceDisplayArgs);
567 }
568
Marin Shalamanov6e840172020-12-14 22:13:28 +0100569 static void setupHwcGetConfigsCallExpectations(DisplayTransactionTest* test) {
570 EXPECT_CALL(*test->mComposer, getDisplayConfigs(_, _)).Times(0);
571 EXPECT_CALL(*test->mComposer, getDisplayAttribute(_, _, _, _)).Times(0);
572 }
573
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200574 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
575 EXPECT_CALL(*test->mComposer, getActiveConfig(_, _)).Times(0);
576 }
577
578 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
579 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
580 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
581 }
582};
583
584// A virtual display supported by the HWC.
585constexpr uint32_t GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY = GRALLOC_USAGE_HW_COMPOSER;
586
587template <int width, int height, Secure secure>
588struct HwcVirtualDisplayVariant
Dominik Laskowskie80bd312023-02-11 16:23:34 -0500589 : DisplayVariant<HalVirtualDisplayIdType<42>, width, height, Async::TRUE, secure,
590 Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY, VIRTUAL_DISPLAY_FLAGS>,
591 HwcDisplayVariant<HWC_VIRTUAL_DISPLAY_HWC_DISPLAY_ID, DisplayType::VIRTUAL,
592 DisplayVariant<HalVirtualDisplayIdType<42>, width, height, Async::TRUE,
593 secure, Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY,
594 VIRTUAL_DISPLAY_FLAGS>> {
595 using Base = DisplayVariant<HalVirtualDisplayIdType<42>, width, height, Async::TRUE, secure,
596 Primary::FALSE, GRALLOC_USAGE_HW_COMPOSER, VIRTUAL_DISPLAY_FLAGS>;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200597 using Self = HwcVirtualDisplayVariant<width, height, secure>;
598
599 static std::shared_ptr<compositionengine::Display> injectCompositionDisplay(
600 DisplayTransactionTest* test) {
601 const ::testing::TestInfo* const test_info =
602 ::testing::UnitTest::GetInstance()->current_test_info();
603
Dominik Laskowski13948602021-03-08 20:48:28 -0800604 const auto displayId = Base::DISPLAY_ID::get();
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200605 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder()
Dominik Laskowski13948602021-03-08 20:48:28 -0800606 .setId(displayId)
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800607 .setPixels(Base::RESOLUTION)
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200608 .setIsSecure(static_cast<bool>(Base::SECURE))
609 .setPowerAdvisor(&test->mPowerAdvisor)
610 .setName(std::string("Injected display for ") +
611 test_info->test_case_name() + "." + test_info->name())
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200612 .build();
613
614 auto compositionDisplay =
615 compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
616 ceDisplayArgs);
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200617
618 // Insert display data so that the HWC thinks it created the virtual display.
Dominik Laskowski13948602021-03-08 20:48:28 -0800619 test->mFlinger.mutableHwcDisplayData().try_emplace(displayId);
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200620
621 return compositionDisplay;
622 }
623
624 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
625 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
626 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
627 }
628
629 static void setupHwcVirtualDisplayCreationCallExpectations(DisplayTransactionTest* test) {
Dominik Laskowski263eec42021-07-21 23:13:24 -0700630 EXPECT_CALL(*test->mComposer, createVirtualDisplay(Base::WIDTH, Base::HEIGHT, _, _))
631 .WillOnce(DoAll(SetArgPointee<3>(Self::HWC_DISPLAY_ID), Return(Error::NONE)));
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200632 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
633 }
634};
635
636// For this variant, the display is not a HWC display, so no HDR support should
637// be configured.
638struct NonHwcDisplayHdrSupportVariant {
639 static constexpr bool HDR10_PLUS_SUPPORTED = false;
640 static constexpr bool HDR10_SUPPORTED = false;
641 static constexpr bool HDR_HLG_SUPPORTED = false;
642 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
643 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
644 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
645 }
646};
647
648// For this variant, the composer should respond with am empty list of HDR
649// modes, so no HDR support should be configured.
650template <typename Display>
651struct HdrNotSupportedVariant {
652 static constexpr bool HDR10_PLUS_SUPPORTED = false;
653 static constexpr bool HDR10_SUPPORTED = false;
654 static constexpr bool HDR_HLG_SUPPORTED = false;
655 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
656 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
657 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
658 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
659 }
660};
661
662struct NonHwcPerFrameMetadataSupportVariant {
663 static constexpr int PER_FRAME_METADATA_KEYS = 0;
664 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
665 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(_)).Times(0);
666 }
667};
668
669template <typename Display>
670struct NoPerFrameMetadataSupportVariant {
671 static constexpr int PER_FRAME_METADATA_KEYS = 0;
672 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
673 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
674 .WillOnce(Return(std::vector<PerFrameMetadataKey>()));
675 }
676};
677
678// For this variant, SurfaceFlinger should configure itself with wide display
679// support, but the display should respond with an empty list of supported color
680// modes. Wide-color support for the display should not be configured.
681template <typename Display>
682struct WideColorNotSupportedVariant {
683 static constexpr bool WIDE_COLOR_SUPPORTED = false;
684
685 static void injectConfigChange(DisplayTransactionTest* test) {
Dominik Laskowski788945b2022-08-30 12:10:56 -0700686 test->mFlinger.mutableSupportsWideColor() = true;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200687 }
688
689 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200690 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
691 }
692};
693
694// For this variant, SurfaceFlinger should not configure itself with wide
695// display support, so the display should not be configured for wide-color
696// support.
697struct WideColorSupportNotConfiguredVariant {
698 static constexpr bool WIDE_COLOR_SUPPORTED = false;
699
700 static void injectConfigChange(DisplayTransactionTest* test) {
Dominik Laskowski788945b2022-08-30 12:10:56 -0700701 test->mFlinger.mutableSupportsWideColor() = false;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200702 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged;
703 }
704
705 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200706 EXPECT_CALL(*test->mComposer, getRenderIntents(_, _, _)).Times(0);
707 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
708 }
709};
710
711/* ------------------------------------------------------------------------
712 * Typical display configurations to test
713 */
714
715template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy,
716 typename PerFrameMetadataSupportPolicy>
717struct Case {
718 using Display = DisplayPolicy;
719 using WideColorSupport = WideColorSupportPolicy;
720 using HdrSupport = HdrSupportPolicy;
721 using PerFrameMetadataSupport = PerFrameMetadataSupportPolicy;
722};
723
724using SimplePrimaryDisplayCase =
725 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
726 HdrNotSupportedVariant<PrimaryDisplayVariant>,
727 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
728using SimpleExternalDisplayCase =
729 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
730 HdrNotSupportedVariant<ExternalDisplayVariant>,
731 NoPerFrameMetadataSupportVariant<ExternalDisplayVariant>>;
732using SimpleTertiaryDisplayCase =
733 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
734 HdrNotSupportedVariant<TertiaryDisplayVariant>,
735 NoPerFrameMetadataSupportVariant<TertiaryDisplayVariant>>;
736
737using NonHwcVirtualDisplayCase =
738 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
739 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant,
740 NonHwcPerFrameMetadataSupportVariant>;
741using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
742using HwcVirtualDisplayCase =
743 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
744 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>,
745 NoPerFrameMetadataSupportVariant<SimpleHwcVirtualDisplayVariant>>;
746
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800747inline DisplayModePtr createDisplayMode(DisplayModeId modeId, Fps refreshRate, int32_t group = 0,
748 ui::Size resolution = ui::Size(1920, 1080)) {
749 return mock::createDisplayMode(modeId, refreshRate, group, resolution,
750 PrimaryDisplayVariant::DISPLAY_ID::get());
751}
752
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200753} // namespace android
754
755// TODO(b/129481165): remove the #pragma below and fix conversion issues
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100756#pragma clang diagnostic pop // ignored "-Wconversion -Wextra"