blob: 166430193d8e6b79d48fc0b7abae54ac38208781 [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>
25#include "DisplayIdentificationTest.h"
26
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
45#include "TestableScheduler.h"
46#include "TestableSurfaceFlinger.h"
47#include "mock/DisplayHardware/MockComposer.h"
48#include "mock/DisplayHardware/MockPowerAdvisor.h"
49#include "mock/MockEventThread.h"
50#include "mock/MockMessageQueue.h"
51#include "mock/MockNativeWindowSurface.h"
52#include "mock/MockSchedulerCallback.h"
53#include "mock/MockSurfaceInterceptor.h"
54#include "mock/MockVsyncController.h"
55#include "mock/system/window/MockNativeWindow.h"
56
57namespace android {
58
59// TODO: Do not polute the android namespace
60namespace hal = android::hardware::graphics::composer::hal;
61
62using testing::_;
63using testing::AnyNumber;
64using testing::DoAll;
65using testing::Mock;
66using testing::ResultOf;
67using testing::Return;
68using testing::SetArgPointee;
69
70using hal::ColorMode;
71using hal::Connection;
72using hal::DisplayCapability;
73using hal::DisplayType;
74using hal::Error;
75using hal::Hdr;
76using hal::HWDisplayId;
77using hal::IComposer;
78using hal::IComposerClient;
79using hal::PerFrameMetadataKey;
80using hal::PowerMode;
81
82class DisplayTransactionTest : public testing::Test {
83public:
84 ~DisplayTransactionTest() override;
85
86 // --------------------------------------------------------------------
87 // Mock/Fake injection
88
89 void injectMockScheduler();
90 void injectMockComposer(int virtualDisplayCount);
91 void injectFakeBufferQueueFactory();
92 void injectFakeNativeWindowSurfaceFactory();
93 sp<DisplayDevice> injectDefaultInternalDisplay(
94 std::function<void(TestableSurfaceFlinger::FakeDisplayDeviceInjector&)>);
95
96 // --------------------------------------------------------------------
97 // Postcondition helpers
98
99 bool hasPhysicalHwcDisplay(hal::HWDisplayId hwcDisplayId);
100 bool hasTransactionFlagSet(int flag);
101 bool hasDisplayDevice(sp<IBinder> displayToken);
102 sp<DisplayDevice> getDisplayDevice(sp<IBinder> displayToken);
103 bool hasCurrentDisplayState(sp<IBinder> displayToken);
104 const DisplayDeviceState& getCurrentDisplayState(sp<IBinder> displayToken);
105 bool hasDrawingDisplayState(sp<IBinder> displayToken);
106 const DisplayDeviceState& getDrawingDisplayState(sp<IBinder> displayToken);
107
108 // --------------------------------------------------------------------
109 // Test instances
110
111 TestableSurfaceFlinger mFlinger;
112 sp<mock::NativeWindow> mNativeWindow = new mock::NativeWindow();
113 sp<GraphicBuffer> mBuffer = new GraphicBuffer();
114 Hwc2::mock::PowerAdvisor mPowerAdvisor;
115
116 // These mocks are created by the test, but are destroyed by SurfaceFlinger
117 // by virtue of being stored into a std::unique_ptr. However we still need
118 // to keep a reference to them for use in setting up call expectations.
119 renderengine::mock::RenderEngine* mRenderEngine = new renderengine::mock::RenderEngine();
120 Hwc2::mock::Composer* mComposer = nullptr;
121 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
122 sp<mock::SurfaceInterceptor> mSurfaceInterceptor = new mock::SurfaceInterceptor;
123
124 mock::VsyncController* mVsyncController = new mock::VsyncController;
125 mock::VSyncTracker* mVSyncTracker = new mock::VSyncTracker;
126 mock::SchedulerCallback mSchedulerCallback;
127 mock::EventThread* mEventThread = new mock::EventThread;
128 mock::EventThread* mSFEventThread = new mock::EventThread;
129
130 // These mocks are created only when expected to be created via a factory.
131 sp<mock::GraphicBufferConsumer> mConsumer;
132 sp<mock::GraphicBufferProducer> mProducer;
133 surfaceflinger::mock::NativeWindowSurface* mNativeWindowSurface = nullptr;
134
135protected:
136 DisplayTransactionTest();
137};
138
Marin Shalamanova903d032020-12-29 20:35:13 +0100139constexpr int32_t DEFAULT_VSYNC_PERIOD = 16'666'667;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200140constexpr int32_t DEFAULT_DPI = 320;
141constexpr int DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT = HAL_PIXEL_FORMAT_RGB_565;
142
143constexpr int POWER_MODE_LEET = 1337; // An out of range power mode value
144
145/* ------------------------------------------------------------------------
146 * Boolean avoidance
147 *
148 * To make calls and template instantiations more readable, we define some
149 * local enums along with an implicit bool conversion.
150 */
151
152#define BOOL_SUBSTITUTE(TYPENAME) enum class TYPENAME : bool { FALSE = false, TRUE = true };
153
154BOOL_SUBSTITUTE(Async);
155BOOL_SUBSTITUTE(Critical);
156BOOL_SUBSTITUTE(Primary);
157BOOL_SUBSTITUTE(Secure);
158BOOL_SUBSTITUTE(Virtual);
159
160template <typename PhysicalDisplay>
161struct PhysicalDisplayIdType {};
162
163template <uint64_t displayId>
164using HalVirtualDisplayIdType = std::integral_constant<uint64_t, displayId>;
165
166struct GpuVirtualDisplayIdType {};
167
168template <typename>
169struct IsPhysicalDisplayId : std::bool_constant<false> {};
170
171template <typename PhysicalDisplay>
172struct IsPhysicalDisplayId<PhysicalDisplayIdType<PhysicalDisplay>> : std::bool_constant<true> {};
173
174template <typename>
175struct DisplayIdGetter;
176
177template <typename PhysicalDisplay>
178struct DisplayIdGetter<PhysicalDisplayIdType<PhysicalDisplay>> {
179 static PhysicalDisplayId get() {
180 if (!PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
181 return PhysicalDisplayId::fromPort(static_cast<bool>(PhysicalDisplay::PRIMARY)
182 ? LEGACY_DISPLAY_TYPE_PRIMARY
183 : LEGACY_DISPLAY_TYPE_EXTERNAL);
184 }
185
186 const auto info =
187 parseDisplayIdentificationData(PhysicalDisplay::PORT,
188 PhysicalDisplay::GET_IDENTIFICATION_DATA());
189 return info ? info->id : PhysicalDisplayId::fromPort(PhysicalDisplay::PORT);
190 }
191};
192
193template <uint64_t displayId>
194struct DisplayIdGetter<HalVirtualDisplayIdType<displayId>> {
195 static HalVirtualDisplayId get() { return HalVirtualDisplayId(displayId); }
196};
197
198template <>
199struct DisplayIdGetter<GpuVirtualDisplayIdType> {
200 static GpuVirtualDisplayId get() { return GpuVirtualDisplayId(0); }
201};
202
203template <typename>
204struct DisplayConnectionTypeGetter {
205 static constexpr std::optional<DisplayConnectionType> value;
206};
207
208template <typename PhysicalDisplay>
209struct DisplayConnectionTypeGetter<PhysicalDisplayIdType<PhysicalDisplay>> {
210 static constexpr std::optional<DisplayConnectionType> value = PhysicalDisplay::CONNECTION_TYPE;
211};
212
213template <typename>
214struct HwcDisplayIdGetter {
215 static constexpr std::optional<HWDisplayId> value;
216};
217
218constexpr HWDisplayId HWC_VIRTUAL_DISPLAY_HWC_DISPLAY_ID = 1010;
219
220template <uint64_t displayId>
221struct HwcDisplayIdGetter<HalVirtualDisplayIdType<displayId>> {
222 static constexpr std::optional<HWDisplayId> value = HWC_VIRTUAL_DISPLAY_HWC_DISPLAY_ID;
223};
224
225template <typename PhysicalDisplay>
226struct HwcDisplayIdGetter<PhysicalDisplayIdType<PhysicalDisplay>> {
227 static constexpr std::optional<HWDisplayId> value = PhysicalDisplay::HWC_DISPLAY_ID;
228};
229
230// DisplayIdType can be:
231// 1) PhysicalDisplayIdType<...> for generated ID of physical display backed by HWC.
232// 2) HalVirtualDisplayIdType<...> for hard-coded ID of virtual display backed by HWC.
233// 3) GpuVirtualDisplayIdType for virtual display without HWC backing.
234template <typename DisplayIdType, int width, int height, Critical critical, Async async,
235 Secure secure, Primary primary, int grallocUsage>
236struct DisplayVariant {
237 using DISPLAY_ID = DisplayIdGetter<DisplayIdType>;
238 using CONNECTION_TYPE = DisplayConnectionTypeGetter<DisplayIdType>;
239 using HWC_DISPLAY_ID_OPT = HwcDisplayIdGetter<DisplayIdType>;
240
241 // The display width and height
242 static constexpr int WIDTH = width;
243 static constexpr int HEIGHT = height;
244
245 static constexpr int GRALLOC_USAGE = grallocUsage;
246
247 // Whether the display is virtual or physical
248 static constexpr Virtual VIRTUAL =
249 IsPhysicalDisplayId<DisplayIdType>{} ? Virtual::FALSE : Virtual::TRUE;
250
251 // When creating native window surfaces for the framebuffer, whether those should be critical
252 static constexpr Critical CRITICAL = critical;
253
254 // When creating native window surfaces for the framebuffer, whether those should be async
255 static constexpr Async ASYNC = async;
256
257 // Whether the display should be treated as secure
258 static constexpr Secure SECURE = secure;
259
260 // Whether the display is primary
261 static constexpr Primary PRIMARY = primary;
262
263 static auto makeFakeExistingDisplayInjector(DisplayTransactionTest* test) {
264 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder();
265 if (auto displayId = PhysicalDisplayId::tryCast(DISPLAY_ID::get())) {
266 ceDisplayArgs.setPhysical({*displayId, DisplayConnectionType::Internal});
267 } else {
268 // We turn off the use of HwcVirtualDisplays, to prevent Composition Engine
269 // from calling into HWComposer. This way all virtual displays will get
270 // a GpuVirtualDisplayId, even if we are in the HwcVirtualDisplayVariant.
271 // In this case we later override it by calling display.setDisplayIdForTesting().
272 ceDisplayArgs.setUseHwcVirtualDisplays(false);
273
274 GpuVirtualDisplayId desiredDisplayId = GpuVirtualDisplayId::tryCast(DISPLAY_ID::get())
275 .value_or(GpuVirtualDisplayId(0));
276
277 ON_CALL(test->mFlinger.gpuVirtualDisplayIdGenerator(), nextId())
278 .WillByDefault(Return(desiredDisplayId));
279
280 auto& generator = test->mFlinger.gpuVirtualDisplayIdGenerator();
281 ceDisplayArgs.setGpuVirtualDisplayIdGenerator(generator);
282 }
283 ceDisplayArgs.setPixels({WIDTH, HEIGHT}).setPowerAdvisor(&test->mPowerAdvisor);
284
285 auto compositionDisplay =
286 compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
287 ceDisplayArgs.build());
288
289 if (HalVirtualDisplayId::tryCast(DISPLAY_ID::get())) {
290 // CompositionEngine has assigned a placeholder GpuVirtualDisplayId and we need to
291 // override it with the correct HalVirtualDisplayId.
292 compositionDisplay->setDisplayIdForTesting(DISPLAY_ID::get());
293 }
294
295 auto injector =
296 TestableSurfaceFlinger::FakeDisplayDeviceInjector(test->mFlinger,
297 compositionDisplay,
298 CONNECTION_TYPE::value,
299 HWC_DISPLAY_ID_OPT::value,
300 static_cast<bool>(PRIMARY));
301
302 injector.setSecure(static_cast<bool>(SECURE));
303 injector.setNativeWindow(test->mNativeWindow);
304
305 // Creating a DisplayDevice requires getting default dimensions from the
306 // native window along with some other initial setup.
307 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
308 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
309 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
310 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
311 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT))
312 .WillRepeatedly(Return(0));
313 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT))
314 .WillRepeatedly(Return(0));
315 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64))
316 .WillRepeatedly(Return(0));
317 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT))
318 .WillRepeatedly(Return(0));
319
320 return injector;
321 }
322
323 // Called by tests to set up any native window creation call expectations.
324 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
325 EXPECT_CALL(*test->mNativeWindowSurface, getNativeWindow())
326 .WillOnce(Return(test->mNativeWindow));
327
328 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
329 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
330 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
331 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
332 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT))
333 .WillRepeatedly(Return(0));
334 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT))
335 .WillRepeatedly(Return(0));
336 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64))
337 .WillRepeatedly(Return(0));
338 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT))
339 .WillRepeatedly(Return(0));
340 }
341
342 static void setupFramebufferConsumerBufferQueueCallExpectations(DisplayTransactionTest* test) {
343 EXPECT_CALL(*test->mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR));
344 EXPECT_CALL(*test->mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR));
345 EXPECT_CALL(*test->mConsumer, setConsumerUsageBits(GRALLOC_USAGE))
346 .WillRepeatedly(Return(NO_ERROR));
347 EXPECT_CALL(*test->mConsumer, setDefaultBufferSize(WIDTH, HEIGHT))
348 .WillRepeatedly(Return(NO_ERROR));
349 EXPECT_CALL(*test->mConsumer, setMaxAcquiredBufferCount(_))
350 .WillRepeatedly(Return(NO_ERROR));
351 }
352
353 static void setupFramebufferProducerBufferQueueCallExpectations(DisplayTransactionTest* test) {
354 EXPECT_CALL(*test->mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return());
355 }
356};
357
358template <HWDisplayId hwcDisplayId, DisplayType hwcDisplayType, typename DisplayVariant,
359 typename PhysicalDisplay = void>
360struct HwcDisplayVariant {
361 // The display id supplied by the HWC
362 static constexpr HWDisplayId HWC_DISPLAY_ID = hwcDisplayId;
363
364 // The HWC display type
365 static constexpr DisplayType HWC_DISPLAY_TYPE = hwcDisplayType;
366
367 // The HWC active configuration id
Marin Shalamanov6e840172020-12-14 22:13:28 +0100368 static constexpr hal::HWConfigId HWC_ACTIVE_CONFIG_ID = 2001;
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200369 static constexpr PowerMode INIT_POWER_MODE = hal::PowerMode::ON;
370
371 static void injectPendingHotplugEvent(DisplayTransactionTest* test, Connection connection) {
372 test->mFlinger.mutablePendingHotplugEvents().emplace_back(
373 TestableSurfaceFlinger::HotplugEvent{HWC_DISPLAY_ID, connection});
374 }
375
376 // Called by tests to inject a HWC display setup
377 static void injectHwcDisplayWithNoDefaultCapabilities(DisplayTransactionTest* test) {
378 const auto displayId = DisplayVariant::DISPLAY_ID::get();
379 ASSERT_FALSE(GpuVirtualDisplayId::tryCast(displayId));
380 TestableSurfaceFlinger::FakeHwcDisplayInjector(displayId, HWC_DISPLAY_TYPE,
381 static_cast<bool>(DisplayVariant::PRIMARY))
382 .setHwcDisplayId(HWC_DISPLAY_ID)
383 .setWidth(DisplayVariant::WIDTH)
384 .setHeight(DisplayVariant::HEIGHT)
385 .setActiveConfig(HWC_ACTIVE_CONFIG_ID)
386 .setPowerMode(INIT_POWER_MODE)
387 .inject(&test->mFlinger, test->mComposer);
388 }
389
390 // Called by tests to inject a HWC display setup
391 static void injectHwcDisplay(DisplayTransactionTest* test) {
392 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(HWC_DISPLAY_ID, _))
393 .WillOnce(DoAll(SetArgPointee<1>(std::vector<DisplayCapability>({})),
394 Return(Error::NONE)));
395 EXPECT_CALL(*test->mComposer, setPowerMode(HWC_DISPLAY_ID, INIT_POWER_MODE))
396 .WillOnce(Return(Error::NONE));
397 injectHwcDisplayWithNoDefaultCapabilities(test);
398 }
399
400 static std::shared_ptr<compositionengine::Display> injectCompositionDisplay(
401 DisplayTransactionTest* test) {
402 const ::testing::TestInfo* const test_info =
403 ::testing::UnitTest::GetInstance()->current_test_info();
404
405 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder()
406 .setPhysical({DisplayVariant::DISPLAY_ID::get(),
407 PhysicalDisplay::CONNECTION_TYPE})
408 .setPixels({DisplayVariant::WIDTH, DisplayVariant::HEIGHT})
409 .setIsSecure(static_cast<bool>(DisplayVariant::SECURE))
410 .setPowerAdvisor(&test->mPowerAdvisor)
411 .setName(std::string("Injected display for ") +
412 test_info->test_case_name() + "." + test_info->name())
413 .build();
414
415 return compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
416 ceDisplayArgs);
417 }
418
Marin Shalamanov6e840172020-12-14 22:13:28 +0100419 static void setupHwcGetConfigsCallExpectations(DisplayTransactionTest* test) {
420 if (HWC_DISPLAY_TYPE == DisplayType::PHYSICAL) {
421 EXPECT_CALL(*test->mComposer, getDisplayConfigs(HWC_DISPLAY_ID, _))
422 .WillRepeatedly(DoAll(SetArgPointee<1>(std::vector<hal::HWConfigId>{
423 HWC_ACTIVE_CONFIG_ID}),
424 Return(Error::NONE)));
425 EXPECT_CALL(*test->mComposer,
426 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
427 IComposerClient::Attribute::WIDTH, _))
428 .WillRepeatedly(
429 DoAll(SetArgPointee<3>(DisplayVariant::WIDTH), Return(Error::NONE)));
430 EXPECT_CALL(*test->mComposer,
431 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
432 IComposerClient::Attribute::HEIGHT, _))
433 .WillRepeatedly(
434 DoAll(SetArgPointee<3>(DisplayVariant::HEIGHT), Return(Error::NONE)));
435 EXPECT_CALL(*test->mComposer,
436 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
437 IComposerClient::Attribute::VSYNC_PERIOD, _))
438 .WillRepeatedly(
Marin Shalamanova903d032020-12-29 20:35:13 +0100439 DoAll(SetArgPointee<3>(DEFAULT_VSYNC_PERIOD), Return(Error::NONE)));
Marin Shalamanov6e840172020-12-14 22:13:28 +0100440 EXPECT_CALL(*test->mComposer,
441 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
442 IComposerClient::Attribute::DPI_X, _))
443 .WillRepeatedly(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
444 EXPECT_CALL(*test->mComposer,
445 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
446 IComposerClient::Attribute::DPI_Y, _))
447 .WillRepeatedly(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
448 EXPECT_CALL(*test->mComposer,
449 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
450 IComposerClient::Attribute::CONFIG_GROUP, _))
451 .WillRepeatedly(DoAll(SetArgPointee<3>(-1), Return(Error::NONE)));
452 } else {
453 EXPECT_CALL(*test->mComposer, getDisplayConfigs(_, _)).Times(0);
454 EXPECT_CALL(*test->mComposer, getDisplayAttribute(_, _, _, _)).Times(0);
455 }
456 }
457
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200458 static void setupHwcHotplugCallExpectations(DisplayTransactionTest* test) {
459 constexpr auto CONNECTION_TYPE =
460 PhysicalDisplay::CONNECTION_TYPE == DisplayConnectionType::Internal
461 ? IComposerClient::DisplayConnectionType::INTERNAL
462 : IComposerClient::DisplayConnectionType::EXTERNAL;
463
464 EXPECT_CALL(*test->mComposer, getDisplayConnectionType(HWC_DISPLAY_ID, _))
465 .WillOnce(DoAll(SetArgPointee<1>(CONNECTION_TYPE), Return(hal::V2_4::Error::NONE)));
466
467 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_))
468 .WillOnce(Return(hal::Error::NONE));
Marin Shalamanov6e840172020-12-14 22:13:28 +0100469
470 setupHwcGetConfigsCallExpectations(test);
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200471
472 if (PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
473 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
474 .WillOnce(DoAll(SetArgPointee<1>(PhysicalDisplay::PORT),
475 SetArgPointee<2>(PhysicalDisplay::GET_IDENTIFICATION_DATA()),
476 Return(Error::NONE)));
477 } else {
478 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
479 .WillOnce(Return(Error::UNSUPPORTED));
480 }
481 }
482
483 // Called by tests to set up HWC call expectations
484 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
485 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY_ID, _))
486 .WillRepeatedly(DoAll(SetArgPointee<1>(HWC_ACTIVE_CONFIG_ID), Return(Error::NONE)));
487 }
488};
489
490// Physical displays are expected to be synchronous, secure, and have a HWC display for output.
491constexpr uint32_t GRALLOC_USAGE_PHYSICAL_DISPLAY =
492 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
493
494template <typename PhysicalDisplay, int width, int height, Critical critical>
495struct PhysicalDisplayVariant
496 : DisplayVariant<PhysicalDisplayIdType<PhysicalDisplay>, width, height, critical,
497 Async::FALSE, Secure::TRUE, PhysicalDisplay::PRIMARY,
498 GRALLOC_USAGE_PHYSICAL_DISPLAY>,
499 HwcDisplayVariant<PhysicalDisplay::HWC_DISPLAY_ID, DisplayType::PHYSICAL,
500 DisplayVariant<PhysicalDisplayIdType<PhysicalDisplay>, width, height,
501 critical, Async::FALSE, Secure::TRUE,
502 PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
503 PhysicalDisplay> {};
504
505template <bool hasIdentificationData>
506struct PrimaryDisplay {
507 static constexpr auto CONNECTION_TYPE = DisplayConnectionType::Internal;
508 static constexpr Primary PRIMARY = Primary::TRUE;
509 static constexpr uint8_t PORT = 255;
510 static constexpr HWDisplayId HWC_DISPLAY_ID = 1001;
511 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
512 static constexpr auto GET_IDENTIFICATION_DATA = getInternalEdid;
513};
514
515template <bool hasIdentificationData>
516struct ExternalDisplay {
517 static constexpr auto CONNECTION_TYPE = DisplayConnectionType::External;
518 static constexpr Primary PRIMARY = Primary::FALSE;
519 static constexpr uint8_t PORT = 254;
520 static constexpr HWDisplayId HWC_DISPLAY_ID = 1002;
521 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
522 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
523};
524
525struct TertiaryDisplay {
526 static constexpr Primary PRIMARY = Primary::FALSE;
527 static constexpr uint8_t PORT = 253;
528 static constexpr HWDisplayId HWC_DISPLAY_ID = 1003;
529 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
530};
531
532// A primary display is a physical display that is critical
533using PrimaryDisplayVariant =
534 PhysicalDisplayVariant<PrimaryDisplay<false>, 3840, 2160, Critical::TRUE>;
535
536// An external display is physical display that is not critical.
537using ExternalDisplayVariant =
538 PhysicalDisplayVariant<ExternalDisplay<false>, 1920, 1280, Critical::FALSE>;
539
540using TertiaryDisplayVariant = PhysicalDisplayVariant<TertiaryDisplay, 1600, 1200, Critical::FALSE>;
541
542// A virtual display not supported by the HWC.
543constexpr uint32_t GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY = 0;
544
545template <int width, int height, Secure secure>
546struct NonHwcVirtualDisplayVariant
547 : DisplayVariant<GpuVirtualDisplayIdType, width, height, Critical::FALSE, Async::TRUE, secure,
548 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY> {
549 using Base =
550 DisplayVariant<GpuVirtualDisplayIdType, width, height, Critical::FALSE, Async::TRUE,
551 secure, Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>;
552
553 static void injectHwcDisplay(DisplayTransactionTest*) {}
554
555 static std::shared_ptr<compositionengine::Display> injectCompositionDisplay(
556 DisplayTransactionTest* test) {
557 const ::testing::TestInfo* const test_info =
558 ::testing::UnitTest::GetInstance()->current_test_info();
559
560 ON_CALL(test->mFlinger.gpuVirtualDisplayIdGenerator(), nextId())
561 .WillByDefault(Return(Base::DISPLAY_ID::get()));
562
563 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder()
564 .setPixels({Base::WIDTH, Base::HEIGHT})
565 .setIsSecure(static_cast<bool>(Base::SECURE))
566 .setPowerAdvisor(&test->mPowerAdvisor)
567 .setName(std::string("Injected display for ") +
568 test_info->test_case_name() + "." + test_info->name())
569 .setGpuVirtualDisplayIdGenerator(
570 test->mFlinger.gpuVirtualDisplayIdGenerator())
571 .build();
572
573 return compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
574 ceDisplayArgs);
575 }
576
Marin Shalamanov6e840172020-12-14 22:13:28 +0100577 static void setupHwcGetConfigsCallExpectations(DisplayTransactionTest* test) {
578 EXPECT_CALL(*test->mComposer, getDisplayConfigs(_, _)).Times(0);
579 EXPECT_CALL(*test->mComposer, getDisplayAttribute(_, _, _, _)).Times(0);
580 }
581
Marin Shalamanov07b1ff32020-10-07 16:57:22 +0200582 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
583 EXPECT_CALL(*test->mComposer, getActiveConfig(_, _)).Times(0);
584 }
585
586 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
587 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
588 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
589 }
590};
591
592// A virtual display supported by the HWC.
593constexpr uint32_t GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY = GRALLOC_USAGE_HW_COMPOSER;
594
595template <int width, int height, Secure secure>
596struct HwcVirtualDisplayVariant
597 : DisplayVariant<HalVirtualDisplayIdType<42>, width, height, Critical::FALSE, Async::TRUE,
598 secure, Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>,
599 HwcDisplayVariant<HWC_VIRTUAL_DISPLAY_HWC_DISPLAY_ID, DisplayType::VIRTUAL,
600 DisplayVariant<HalVirtualDisplayIdType<42>, width, height,
601 Critical::FALSE, Async::TRUE, secure, Primary::FALSE,
602 GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>> {
603 using Base = DisplayVariant<HalVirtualDisplayIdType<42>, width, height, Critical::FALSE,
604 Async::TRUE, secure, Primary::FALSE, GRALLOC_USAGE_HW_COMPOSER>;
605 using Self = HwcVirtualDisplayVariant<width, height, secure>;
606
607 static std::shared_ptr<compositionengine::Display> injectCompositionDisplay(
608 DisplayTransactionTest* test) {
609 const ::testing::TestInfo* const test_info =
610 ::testing::UnitTest::GetInstance()->current_test_info();
611
612 // In order to prevent compostition engine calling into HWComposer, we
613 // 1. turn off the use of HWC virtual displays,
614 // 2. provide a GpuVirtualDisplayIdGenerator which always returns some fake ID
615 // 3. override the ID by calling setDisplayIdForTesting()
616
617 ON_CALL(test->mFlinger.gpuVirtualDisplayIdGenerator(), nextId())
618 .WillByDefault(Return(GpuVirtualDisplayId(0)));
619
620 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder()
621 .setUseHwcVirtualDisplays(false)
622 .setPixels({Base::WIDTH, Base::HEIGHT})
623 .setIsSecure(static_cast<bool>(Base::SECURE))
624 .setPowerAdvisor(&test->mPowerAdvisor)
625 .setName(std::string("Injected display for ") +
626 test_info->test_case_name() + "." + test_info->name())
627 .setGpuVirtualDisplayIdGenerator(
628 test->mFlinger.gpuVirtualDisplayIdGenerator())
629 .build();
630
631 auto compositionDisplay =
632 compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
633 ceDisplayArgs);
634 compositionDisplay->setDisplayIdForTesting(Base::DISPLAY_ID::get());
635
636 // Insert display data so that the HWC thinks it created the virtual display.
637 if (const auto displayId = Base::DISPLAY_ID::get();
638 HalVirtualDisplayId::tryCast(displayId)) {
639 test->mFlinger.mutableHwcDisplayData().try_emplace(displayId);
640 }
641
642 return compositionDisplay;
643 }
644
645 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
646 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
647 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
648 }
649
650 static void setupHwcVirtualDisplayCreationCallExpectations(DisplayTransactionTest* test) {
651 EXPECT_CALL(*test->mComposer, createVirtualDisplay(Base::WIDTH, Base::HEIGHT, _, _))
652 .WillOnce(DoAll(SetArgPointee<3>(Self::HWC_DISPLAY_ID), Return(Error::NONE)));
653 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
654 }
655};
656
657// For this variant, the display is not a HWC display, so no HDR support should
658// be configured.
659struct NonHwcDisplayHdrSupportVariant {
660 static constexpr bool HDR10_PLUS_SUPPORTED = false;
661 static constexpr bool HDR10_SUPPORTED = false;
662 static constexpr bool HDR_HLG_SUPPORTED = false;
663 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
664 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
665 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
666 }
667};
668
669// For this variant, the composer should respond with am empty list of HDR
670// modes, so no HDR support should be configured.
671template <typename Display>
672struct HdrNotSupportedVariant {
673 static constexpr bool HDR10_PLUS_SUPPORTED = false;
674 static constexpr bool HDR10_SUPPORTED = false;
675 static constexpr bool HDR_HLG_SUPPORTED = false;
676 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
677 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
678 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
679 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
680 }
681};
682
683struct NonHwcPerFrameMetadataSupportVariant {
684 static constexpr int PER_FRAME_METADATA_KEYS = 0;
685 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
686 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(_)).Times(0);
687 }
688};
689
690template <typename Display>
691struct NoPerFrameMetadataSupportVariant {
692 static constexpr int PER_FRAME_METADATA_KEYS = 0;
693 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
694 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
695 .WillOnce(Return(std::vector<PerFrameMetadataKey>()));
696 }
697};
698
699// For this variant, SurfaceFlinger should configure itself with wide display
700// support, but the display should respond with an empty list of supported color
701// modes. Wide-color support for the display should not be configured.
702template <typename Display>
703struct WideColorNotSupportedVariant {
704 static constexpr bool WIDE_COLOR_SUPPORTED = false;
705
706 static void injectConfigChange(DisplayTransactionTest* test) {
707 test->mFlinger.mutableUseColorManagement() = true;
708 test->mFlinger.mutableHasWideColorDisplay() = true;
709 }
710
711 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
712 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
713 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>()), Return(Error::NONE)));
714 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
715 }
716};
717
718// For this variant, SurfaceFlinger should not configure itself with wide
719// display support, so the display should not be configured for wide-color
720// support.
721struct WideColorSupportNotConfiguredVariant {
722 static constexpr bool WIDE_COLOR_SUPPORTED = false;
723
724 static void injectConfigChange(DisplayTransactionTest* test) {
725 test->mFlinger.mutableHasWideColorDisplay() = false;
726 test->mFlinger.mutableUseColorManagement() = false;
727 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged;
728 }
729
730 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
731 EXPECT_CALL(*test->mComposer, getColorModes(_, _)).Times(0);
732 EXPECT_CALL(*test->mComposer, getRenderIntents(_, _, _)).Times(0);
733 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
734 }
735};
736
737/* ------------------------------------------------------------------------
738 * Typical display configurations to test
739 */
740
741template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy,
742 typename PerFrameMetadataSupportPolicy>
743struct Case {
744 using Display = DisplayPolicy;
745 using WideColorSupport = WideColorSupportPolicy;
746 using HdrSupport = HdrSupportPolicy;
747 using PerFrameMetadataSupport = PerFrameMetadataSupportPolicy;
748};
749
750using SimplePrimaryDisplayCase =
751 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
752 HdrNotSupportedVariant<PrimaryDisplayVariant>,
753 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
754using SimpleExternalDisplayCase =
755 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
756 HdrNotSupportedVariant<ExternalDisplayVariant>,
757 NoPerFrameMetadataSupportVariant<ExternalDisplayVariant>>;
758using SimpleTertiaryDisplayCase =
759 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
760 HdrNotSupportedVariant<TertiaryDisplayVariant>,
761 NoPerFrameMetadataSupportVariant<TertiaryDisplayVariant>>;
762
763using NonHwcVirtualDisplayCase =
764 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
765 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant,
766 NonHwcPerFrameMetadataSupportVariant>;
767using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
768using HwcVirtualDisplayCase =
769 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
770 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>,
771 NoPerFrameMetadataSupportVariant<SimpleHwcVirtualDisplayVariant>>;
772
773} // namespace android
774
775// TODO(b/129481165): remove the #pragma below and fix conversion issues
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100776#pragma clang diagnostic pop // ignored "-Wconversion -Wextra"