blob: 06c0f8ed3043a657d8044c2443de7fe3015a091a [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
139constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'667;
140constexpr 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
368 static constexpr int HWC_ACTIVE_CONFIG_ID = 2001;
369 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
419 static void setupHwcHotplugCallExpectations(DisplayTransactionTest* test) {
420 constexpr auto CONNECTION_TYPE =
421 PhysicalDisplay::CONNECTION_TYPE == DisplayConnectionType::Internal
422 ? IComposerClient::DisplayConnectionType::INTERNAL
423 : IComposerClient::DisplayConnectionType::EXTERNAL;
424
425 EXPECT_CALL(*test->mComposer, getDisplayConnectionType(HWC_DISPLAY_ID, _))
426 .WillOnce(DoAll(SetArgPointee<1>(CONNECTION_TYPE), Return(hal::V2_4::Error::NONE)));
427
428 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_))
429 .WillOnce(Return(hal::Error::NONE));
430 EXPECT_CALL(*test->mComposer, getDisplayConfigs(HWC_DISPLAY_ID, _))
431 .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{HWC_ACTIVE_CONFIG_ID}),
432 Return(Error::NONE)));
433 EXPECT_CALL(*test->mComposer,
434 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
435 IComposerClient::Attribute::WIDTH, _))
436 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::WIDTH), Return(Error::NONE)));
437 EXPECT_CALL(*test->mComposer,
438 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
439 IComposerClient::Attribute::HEIGHT, _))
440 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::HEIGHT), Return(Error::NONE)));
441 EXPECT_CALL(*test->mComposer,
442 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
443 IComposerClient::Attribute::VSYNC_PERIOD, _))
444 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE)));
445 EXPECT_CALL(*test->mComposer,
446 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
447 IComposerClient::Attribute::DPI_X, _))
448 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
449 EXPECT_CALL(*test->mComposer,
450 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
451 IComposerClient::Attribute::DPI_Y, _))
452 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
453 EXPECT_CALL(*test->mComposer,
454 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
455 IComposerClient::Attribute::CONFIG_GROUP, _))
456 .WillOnce(DoAll(SetArgPointee<3>(-1), Return(Error::NONE)));
457
458 if (PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
459 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
460 .WillOnce(DoAll(SetArgPointee<1>(PhysicalDisplay::PORT),
461 SetArgPointee<2>(PhysicalDisplay::GET_IDENTIFICATION_DATA()),
462 Return(Error::NONE)));
463 } else {
464 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
465 .WillOnce(Return(Error::UNSUPPORTED));
466 }
467 }
468
469 // Called by tests to set up HWC call expectations
470 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
471 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY_ID, _))
472 .WillRepeatedly(DoAll(SetArgPointee<1>(HWC_ACTIVE_CONFIG_ID), Return(Error::NONE)));
473 }
474};
475
476// Physical displays are expected to be synchronous, secure, and have a HWC display for output.
477constexpr uint32_t GRALLOC_USAGE_PHYSICAL_DISPLAY =
478 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
479
480template <typename PhysicalDisplay, int width, int height, Critical critical>
481struct PhysicalDisplayVariant
482 : DisplayVariant<PhysicalDisplayIdType<PhysicalDisplay>, width, height, critical,
483 Async::FALSE, Secure::TRUE, PhysicalDisplay::PRIMARY,
484 GRALLOC_USAGE_PHYSICAL_DISPLAY>,
485 HwcDisplayVariant<PhysicalDisplay::HWC_DISPLAY_ID, DisplayType::PHYSICAL,
486 DisplayVariant<PhysicalDisplayIdType<PhysicalDisplay>, width, height,
487 critical, Async::FALSE, Secure::TRUE,
488 PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
489 PhysicalDisplay> {};
490
491template <bool hasIdentificationData>
492struct PrimaryDisplay {
493 static constexpr auto CONNECTION_TYPE = DisplayConnectionType::Internal;
494 static constexpr Primary PRIMARY = Primary::TRUE;
495 static constexpr uint8_t PORT = 255;
496 static constexpr HWDisplayId HWC_DISPLAY_ID = 1001;
497 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
498 static constexpr auto GET_IDENTIFICATION_DATA = getInternalEdid;
499};
500
501template <bool hasIdentificationData>
502struct ExternalDisplay {
503 static constexpr auto CONNECTION_TYPE = DisplayConnectionType::External;
504 static constexpr Primary PRIMARY = Primary::FALSE;
505 static constexpr uint8_t PORT = 254;
506 static constexpr HWDisplayId HWC_DISPLAY_ID = 1002;
507 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
508 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
509};
510
511struct TertiaryDisplay {
512 static constexpr Primary PRIMARY = Primary::FALSE;
513 static constexpr uint8_t PORT = 253;
514 static constexpr HWDisplayId HWC_DISPLAY_ID = 1003;
515 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
516};
517
518// A primary display is a physical display that is critical
519using PrimaryDisplayVariant =
520 PhysicalDisplayVariant<PrimaryDisplay<false>, 3840, 2160, Critical::TRUE>;
521
522// An external display is physical display that is not critical.
523using ExternalDisplayVariant =
524 PhysicalDisplayVariant<ExternalDisplay<false>, 1920, 1280, Critical::FALSE>;
525
526using TertiaryDisplayVariant = PhysicalDisplayVariant<TertiaryDisplay, 1600, 1200, Critical::FALSE>;
527
528// A virtual display not supported by the HWC.
529constexpr uint32_t GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY = 0;
530
531template <int width, int height, Secure secure>
532struct NonHwcVirtualDisplayVariant
533 : DisplayVariant<GpuVirtualDisplayIdType, width, height, Critical::FALSE, Async::TRUE, secure,
534 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY> {
535 using Base =
536 DisplayVariant<GpuVirtualDisplayIdType, width, height, Critical::FALSE, Async::TRUE,
537 secure, Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>;
538
539 static void injectHwcDisplay(DisplayTransactionTest*) {}
540
541 static std::shared_ptr<compositionengine::Display> injectCompositionDisplay(
542 DisplayTransactionTest* test) {
543 const ::testing::TestInfo* const test_info =
544 ::testing::UnitTest::GetInstance()->current_test_info();
545
546 ON_CALL(test->mFlinger.gpuVirtualDisplayIdGenerator(), nextId())
547 .WillByDefault(Return(Base::DISPLAY_ID::get()));
548
549 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder()
550 .setPixels({Base::WIDTH, Base::HEIGHT})
551 .setIsSecure(static_cast<bool>(Base::SECURE))
552 .setPowerAdvisor(&test->mPowerAdvisor)
553 .setName(std::string("Injected display for ") +
554 test_info->test_case_name() + "." + test_info->name())
555 .setGpuVirtualDisplayIdGenerator(
556 test->mFlinger.gpuVirtualDisplayIdGenerator())
557 .build();
558
559 return compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
560 ceDisplayArgs);
561 }
562
563 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
564 EXPECT_CALL(*test->mComposer, getActiveConfig(_, _)).Times(0);
565 }
566
567 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
568 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
569 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
570 }
571};
572
573// A virtual display supported by the HWC.
574constexpr uint32_t GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY = GRALLOC_USAGE_HW_COMPOSER;
575
576template <int width, int height, Secure secure>
577struct HwcVirtualDisplayVariant
578 : DisplayVariant<HalVirtualDisplayIdType<42>, width, height, Critical::FALSE, Async::TRUE,
579 secure, Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>,
580 HwcDisplayVariant<HWC_VIRTUAL_DISPLAY_HWC_DISPLAY_ID, DisplayType::VIRTUAL,
581 DisplayVariant<HalVirtualDisplayIdType<42>, width, height,
582 Critical::FALSE, Async::TRUE, secure, Primary::FALSE,
583 GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>> {
584 using Base = DisplayVariant<HalVirtualDisplayIdType<42>, width, height, Critical::FALSE,
585 Async::TRUE, secure, Primary::FALSE, GRALLOC_USAGE_HW_COMPOSER>;
586 using Self = HwcVirtualDisplayVariant<width, height, secure>;
587
588 static std::shared_ptr<compositionengine::Display> injectCompositionDisplay(
589 DisplayTransactionTest* test) {
590 const ::testing::TestInfo* const test_info =
591 ::testing::UnitTest::GetInstance()->current_test_info();
592
593 // In order to prevent compostition engine calling into HWComposer, we
594 // 1. turn off the use of HWC virtual displays,
595 // 2. provide a GpuVirtualDisplayIdGenerator which always returns some fake ID
596 // 3. override the ID by calling setDisplayIdForTesting()
597
598 ON_CALL(test->mFlinger.gpuVirtualDisplayIdGenerator(), nextId())
599 .WillByDefault(Return(GpuVirtualDisplayId(0)));
600
601 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder()
602 .setUseHwcVirtualDisplays(false)
603 .setPixels({Base::WIDTH, Base::HEIGHT})
604 .setIsSecure(static_cast<bool>(Base::SECURE))
605 .setPowerAdvisor(&test->mPowerAdvisor)
606 .setName(std::string("Injected display for ") +
607 test_info->test_case_name() + "." + test_info->name())
608 .setGpuVirtualDisplayIdGenerator(
609 test->mFlinger.gpuVirtualDisplayIdGenerator())
610 .build();
611
612 auto compositionDisplay =
613 compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
614 ceDisplayArgs);
615 compositionDisplay->setDisplayIdForTesting(Base::DISPLAY_ID::get());
616
617 // Insert display data so that the HWC thinks it created the virtual display.
618 if (const auto displayId = Base::DISPLAY_ID::get();
619 HalVirtualDisplayId::tryCast(displayId)) {
620 test->mFlinger.mutableHwcDisplayData().try_emplace(displayId);
621 }
622
623 return compositionDisplay;
624 }
625
626 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
627 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
628 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
629 }
630
631 static void setupHwcVirtualDisplayCreationCallExpectations(DisplayTransactionTest* test) {
632 EXPECT_CALL(*test->mComposer, createVirtualDisplay(Base::WIDTH, Base::HEIGHT, _, _))
633 .WillOnce(DoAll(SetArgPointee<3>(Self::HWC_DISPLAY_ID), Return(Error::NONE)));
634 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
635 }
636};
637
638// For this variant, the display is not a HWC display, so no HDR support should
639// be configured.
640struct NonHwcDisplayHdrSupportVariant {
641 static constexpr bool HDR10_PLUS_SUPPORTED = false;
642 static constexpr bool HDR10_SUPPORTED = false;
643 static constexpr bool HDR_HLG_SUPPORTED = false;
644 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
645 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
646 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
647 }
648};
649
650// For this variant, the composer should respond with am empty list of HDR
651// modes, so no HDR support should be configured.
652template <typename Display>
653struct HdrNotSupportedVariant {
654 static constexpr bool HDR10_PLUS_SUPPORTED = false;
655 static constexpr bool HDR10_SUPPORTED = false;
656 static constexpr bool HDR_HLG_SUPPORTED = false;
657 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
658 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
659 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
660 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
661 }
662};
663
664struct NonHwcPerFrameMetadataSupportVariant {
665 static constexpr int PER_FRAME_METADATA_KEYS = 0;
666 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
667 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(_)).Times(0);
668 }
669};
670
671template <typename Display>
672struct NoPerFrameMetadataSupportVariant {
673 static constexpr int PER_FRAME_METADATA_KEYS = 0;
674 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
675 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
676 .WillOnce(Return(std::vector<PerFrameMetadataKey>()));
677 }
678};
679
680// For this variant, SurfaceFlinger should configure itself with wide display
681// support, but the display should respond with an empty list of supported color
682// modes. Wide-color support for the display should not be configured.
683template <typename Display>
684struct WideColorNotSupportedVariant {
685 static constexpr bool WIDE_COLOR_SUPPORTED = false;
686
687 static void injectConfigChange(DisplayTransactionTest* test) {
688 test->mFlinger.mutableUseColorManagement() = true;
689 test->mFlinger.mutableHasWideColorDisplay() = true;
690 }
691
692 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
693 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
694 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>()), Return(Error::NONE)));
695 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
696 }
697};
698
699// For this variant, SurfaceFlinger should not configure itself with wide
700// display support, so the display should not be configured for wide-color
701// support.
702struct WideColorSupportNotConfiguredVariant {
703 static constexpr bool WIDE_COLOR_SUPPORTED = false;
704
705 static void injectConfigChange(DisplayTransactionTest* test) {
706 test->mFlinger.mutableHasWideColorDisplay() = false;
707 test->mFlinger.mutableUseColorManagement() = false;
708 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged;
709 }
710
711 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
712 EXPECT_CALL(*test->mComposer, getColorModes(_, _)).Times(0);
713 EXPECT_CALL(*test->mComposer, getRenderIntents(_, _, _)).Times(0);
714 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
715 }
716};
717
718/* ------------------------------------------------------------------------
719 * Typical display configurations to test
720 */
721
722template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy,
723 typename PerFrameMetadataSupportPolicy>
724struct Case {
725 using Display = DisplayPolicy;
726 using WideColorSupport = WideColorSupportPolicy;
727 using HdrSupport = HdrSupportPolicy;
728 using PerFrameMetadataSupport = PerFrameMetadataSupportPolicy;
729};
730
731using SimplePrimaryDisplayCase =
732 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
733 HdrNotSupportedVariant<PrimaryDisplayVariant>,
734 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
735using SimpleExternalDisplayCase =
736 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
737 HdrNotSupportedVariant<ExternalDisplayVariant>,
738 NoPerFrameMetadataSupportVariant<ExternalDisplayVariant>>;
739using SimpleTertiaryDisplayCase =
740 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
741 HdrNotSupportedVariant<TertiaryDisplayVariant>,
742 NoPerFrameMetadataSupportVariant<TertiaryDisplayVariant>>;
743
744using NonHwcVirtualDisplayCase =
745 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
746 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant,
747 NonHwcPerFrameMetadataSupportVariant>;
748using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
749using HwcVirtualDisplayCase =
750 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
751 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>,
752 NoPerFrameMetadataSupportVariant<SimpleHwcVirtualDisplayVariant>>;
753
754} // namespace android
755
756// TODO(b/129481165): remove the #pragma below and fix conversion issues
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100757#pragma clang diagnostic pop // ignored "-Wconversion -Wextra"