1 /*
2 * Copyright (C) 2018 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 #undef LOG_TAG
18 #define LOG_TAG "LibSurfaceFlingerUnittests"
19
20 #include <type_traits>
21
22 #include <compositionengine/Display.h>
23 #include <compositionengine/DisplayColorProfile.h>
24 #include <compositionengine/mock/DisplaySurface.h>
25 #include <gmock/gmock.h>
26 #include <gtest/gtest.h>
27 #include <log/log.h>
28 #include <renderengine/mock/RenderEngine.h>
29 #include <ui/DebugUtils.h>
30
31 #include "DisplayIdentificationTest.h"
32 #include "Scheduler/RefreshRateConfigs.h"
33 #include "TestableScheduler.h"
34 #include "TestableSurfaceFlinger.h"
35 #include "mock/DisplayHardware/MockComposer.h"
36 #include "mock/MockDispSync.h"
37 #include "mock/MockEventControlThread.h"
38 #include "mock/MockEventThread.h"
39 #include "mock/MockMessageQueue.h"
40 #include "mock/MockNativeWindowSurface.h"
41 #include "mock/MockSurfaceInterceptor.h"
42 #include "mock/gui/MockGraphicBufferConsumer.h"
43 #include "mock/gui/MockGraphicBufferProducer.h"
44 #include "mock/system/window/MockNativeWindow.h"
45
46 namespace android {
47 namespace {
48
49 using testing::_;
50 using testing::DoAll;
51 using testing::Mock;
52 using testing::ResultOf;
53 using testing::Return;
54 using testing::SetArgPointee;
55
56 using android::Hwc2::ColorMode;
57 using android::Hwc2::Error;
58 using android::Hwc2::Hdr;
59 using android::Hwc2::IComposer;
60 using android::Hwc2::IComposerClient;
61 using android::Hwc2::PerFrameMetadataKey;
62 using android::Hwc2::RenderIntent;
63
64 using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector;
65 using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
66 using HotplugEvent = TestableSurfaceFlinger::HotplugEvent;
67 using HWC2Display = TestableSurfaceFlinger::HWC2Display;
68
69 constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666;
70 constexpr int32_t DEFAULT_DPI = 320;
71 constexpr int DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT = HAL_PIXEL_FORMAT_RGB_565;
72
73 constexpr int HWC_POWER_MODE_LEET = 1337; // An out of range power mode value
74
75 /* ------------------------------------------------------------------------
76 * Boolean avoidance
77 *
78 * To make calls and template instantiations more readable, we define some
79 * local enums along with an implicit bool conversion.
80 */
81
82 #define BOOL_SUBSTITUTE(TYPENAME) enum class TYPENAME : bool { FALSE = false, TRUE = true };
83
84 BOOL_SUBSTITUTE(Async);
85 BOOL_SUBSTITUTE(Critical);
86 BOOL_SUBSTITUTE(Primary);
87 BOOL_SUBSTITUTE(Secure);
88 BOOL_SUBSTITUTE(Virtual);
89
90 /* ------------------------------------------------------------------------
91 *
92 */
93
94 class DisplayTransactionTest : public testing::Test {
95 public:
96 DisplayTransactionTest();
97 ~DisplayTransactionTest() override;
98
99 void setupScheduler();
100
101 // --------------------------------------------------------------------
102 // Mock/Fake injection
103
104 void injectMockComposer(int virtualDisplayCount);
105 void injectFakeBufferQueueFactory();
106 void injectFakeNativeWindowSurfaceFactory();
107
108 // --------------------------------------------------------------------
109 // Postcondition helpers
110
111 bool hasPhysicalHwcDisplay(hwc2_display_t hwcDisplayId);
112 bool hasTransactionFlagSet(int flag);
113 bool hasDisplayDevice(sp<IBinder> displayToken);
114 sp<DisplayDevice> getDisplayDevice(sp<IBinder> displayToken);
115 bool hasCurrentDisplayState(sp<IBinder> displayToken);
116 const DisplayDeviceState& getCurrentDisplayState(sp<IBinder> displayToken);
117 bool hasDrawingDisplayState(sp<IBinder> displayToken);
118 const DisplayDeviceState& getDrawingDisplayState(sp<IBinder> displayToken);
119
120 // --------------------------------------------------------------------
121 // Test instances
122
123 TestableScheduler* mScheduler;
124 TestableSurfaceFlinger mFlinger;
125 mock::EventThread* mEventThread = new mock::EventThread();
126 mock::EventThread* mSFEventThread = new mock::EventThread();
127 mock::EventControlThread* mEventControlThread = new mock::EventControlThread();
128 sp<mock::NativeWindow> mNativeWindow = new mock::NativeWindow();
129 sp<GraphicBuffer> mBuffer = new GraphicBuffer();
130
131 // These mocks are created by the test, but are destroyed by SurfaceFlinger
132 // by virtue of being stored into a std::unique_ptr. However we still need
133 // to keep a reference to them for use in setting up call expectations.
134 renderengine::mock::RenderEngine* mRenderEngine = new renderengine::mock::RenderEngine();
135 Hwc2::mock::Composer* mComposer = nullptr;
136 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
137 mock::SurfaceInterceptor* mSurfaceInterceptor = new mock::SurfaceInterceptor();
138 mock::DispSync* mPrimaryDispSync = new mock::DispSync();
139
140 // These mocks are created only when expected to be created via a factory.
141 sp<mock::GraphicBufferConsumer> mConsumer;
142 sp<mock::GraphicBufferProducer> mProducer;
143 surfaceflinger::mock::NativeWindowSurface* mNativeWindowSurface = nullptr;
144 };
145
DisplayTransactionTest()146 DisplayTransactionTest::DisplayTransactionTest() {
147 const ::testing::TestInfo* const test_info =
148 ::testing::UnitTest::GetInstance()->current_test_info();
149 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
150
151 // Default to no wide color display support configured
152 mFlinger.mutableHasWideColorDisplay() = false;
153 mFlinger.mutableUseColorManagement() = false;
154 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
155
156 // Default to using HWC virtual displays
157 mFlinger.mutableUseHwcVirtualDisplays() = true;
158
159 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
160 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
161 });
162
163 mFlinger.setCreateNativeWindowSurface([](auto) {
164 ADD_FAILURE() << "Unexpected request to create a native window surface.";
165 return nullptr;
166 });
167
168 setupScheduler();
169 mFlinger.mutableEventQueue().reset(mMessageQueue);
170 mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
171 mFlinger.mutableInterceptor().reset(mSurfaceInterceptor);
172
173 injectMockComposer(0);
174 }
175
~DisplayTransactionTest()176 DisplayTransactionTest::~DisplayTransactionTest() {
177 const ::testing::TestInfo* const test_info =
178 ::testing::UnitTest::GetInstance()->current_test_info();
179 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
180 }
181
setupScheduler()182 void DisplayTransactionTest::setupScheduler() {
183 std::vector<scheduler::RefreshRateConfigs::InputConfig> configs{{/*hwcId=*/0, 16666667}};
184 mFlinger.mutableRefreshRateConfigs() =
185 std::make_unique<scheduler::RefreshRateConfigs>(/*refreshRateSwitching=*/false, configs,
186 /*currentConfig=*/0);
187 mFlinger.mutableRefreshRateStats() =
188 std::make_unique<scheduler::RefreshRateStats>(*mFlinger.mutableRefreshRateConfigs(),
189 *mFlinger.mutableTimeStats(),
190 /*currentConfig=*/0,
191 /*powerMode=*/HWC_POWER_MODE_OFF);
192 mScheduler = new TestableScheduler(*mFlinger.mutableRefreshRateConfigs());
193 mScheduler->mutableEventControlThread().reset(mEventControlThread);
194 mScheduler->mutablePrimaryDispSync().reset(mPrimaryDispSync);
195 EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_));
196 EXPECT_CALL(*mSFEventThread, registerDisplayEventConnection(_));
197
198 sp<Scheduler::ConnectionHandle> sfConnectionHandle =
199 mScheduler->addConnection(std::unique_ptr<EventThread>(mSFEventThread));
200 mFlinger.mutableSfConnectionHandle() = std::move(sfConnectionHandle);
201 sp<Scheduler::ConnectionHandle> appConnectionHandle =
202 mScheduler->addConnection(std::unique_ptr<EventThread>(mEventThread));
203 mFlinger.mutableAppConnectionHandle() = std::move(appConnectionHandle);
204 mFlinger.mutableScheduler().reset(mScheduler);
205 }
206
injectMockComposer(int virtualDisplayCount)207 void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
208 mComposer = new Hwc2::mock::Composer();
209 EXPECT_CALL(*mComposer, getCapabilities())
210 .WillOnce(Return(std::vector<IComposer::Capability>()));
211 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
212 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
213
214 Mock::VerifyAndClear(mComposer);
215 }
216
injectFakeBufferQueueFactory()217 void DisplayTransactionTest::injectFakeBufferQueueFactory() {
218 // This setup is only expected once per test.
219 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
220
221 mConsumer = new mock::GraphicBufferConsumer();
222 mProducer = new mock::GraphicBufferProducer();
223
224 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
225 *outProducer = mProducer;
226 *outConsumer = mConsumer;
227 });
228 }
229
injectFakeNativeWindowSurfaceFactory()230 void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
231 // This setup is only expected once per test.
232 ASSERT_TRUE(mNativeWindowSurface == nullptr);
233
234 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
235
236 mFlinger.setCreateNativeWindowSurface([this](auto) {
237 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
238 });
239 }
240
hasPhysicalHwcDisplay(hwc2_display_t hwcDisplayId)241 bool DisplayTransactionTest::hasPhysicalHwcDisplay(hwc2_display_t hwcDisplayId) {
242 return mFlinger.mutableHwcPhysicalDisplayIdMap().count(hwcDisplayId) == 1;
243 }
244
hasTransactionFlagSet(int flag)245 bool DisplayTransactionTest::hasTransactionFlagSet(int flag) {
246 return mFlinger.mutableTransactionFlags() & flag;
247 }
248
hasDisplayDevice(sp<IBinder> displayToken)249 bool DisplayTransactionTest::hasDisplayDevice(sp<IBinder> displayToken) {
250 return mFlinger.mutableDisplays().count(displayToken) == 1;
251 }
252
getDisplayDevice(sp<IBinder> displayToken)253 sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) {
254 return mFlinger.mutableDisplays()[displayToken];
255 }
256
hasCurrentDisplayState(sp<IBinder> displayToken)257 bool DisplayTransactionTest::hasCurrentDisplayState(sp<IBinder> displayToken) {
258 return mFlinger.mutableCurrentState().displays.indexOfKey(displayToken) >= 0;
259 }
260
getCurrentDisplayState(sp<IBinder> displayToken)261 const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(sp<IBinder> displayToken) {
262 return mFlinger.mutableCurrentState().displays.valueFor(displayToken);
263 }
264
hasDrawingDisplayState(sp<IBinder> displayToken)265 bool DisplayTransactionTest::hasDrawingDisplayState(sp<IBinder> displayToken) {
266 return mFlinger.mutableDrawingState().displays.indexOfKey(displayToken) >= 0;
267 }
268
getDrawingDisplayState(sp<IBinder> displayToken)269 const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(sp<IBinder> displayToken) {
270 return mFlinger.mutableDrawingState().displays.valueFor(displayToken);
271 }
272
273 /* ------------------------------------------------------------------------
274 *
275 */
276
277 template <typename PhysicalDisplay>
278 struct PhysicalDisplayId {};
279
280 template <DisplayId::Type displayId>
281 using VirtualDisplayId = std::integral_constant<DisplayId::Type, displayId>;
282
283 struct NoDisplayId {};
284
285 template <typename>
286 struct IsPhysicalDisplayId : std::bool_constant<false> {};
287
288 template <typename PhysicalDisplay>
289 struct IsPhysicalDisplayId<PhysicalDisplayId<PhysicalDisplay>> : std::bool_constant<true> {};
290
291 template <typename>
292 struct DisplayIdGetter;
293
294 template <typename PhysicalDisplay>
295 struct DisplayIdGetter<PhysicalDisplayId<PhysicalDisplay>> {
getandroid::__anon968546880111::DisplayIdGetter296 static std::optional<DisplayId> get() {
297 if (!PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
298 return getFallbackDisplayId(static_cast<bool>(PhysicalDisplay::PRIMARY)
299 ? HWC_DISPLAY_PRIMARY
300 : HWC_DISPLAY_EXTERNAL);
301 }
302
303 const auto info =
304 parseDisplayIdentificationData(PhysicalDisplay::PORT,
305 PhysicalDisplay::GET_IDENTIFICATION_DATA());
306 return info ? std::make_optional(info->id) : std::nullopt;
307 }
308 };
309
310 template <DisplayId::Type displayId>
311 struct DisplayIdGetter<VirtualDisplayId<displayId>> {
getandroid::__anon968546880111::DisplayIdGetter312 static std::optional<DisplayId> get() { return DisplayId{displayId}; }
313 };
314
315 template <>
316 struct DisplayIdGetter<NoDisplayId> {
getandroid::__anon968546880111::DisplayIdGetter317 static std::optional<DisplayId> get() { return {}; }
318 };
319
320 // DisplayIdType can be:
321 // 1) PhysicalDisplayId<...> for generated ID of physical display backed by HWC.
322 // 2) VirtualDisplayId<...> for hard-coded ID of virtual display backed by HWC.
323 // 3) NoDisplayId for virtual display without HWC backing.
324 template <typename DisplayIdType, int width, int height, Critical critical, Async async,
325 Secure secure, Primary primary, int grallocUsage>
326 struct DisplayVariant {
327 using DISPLAY_ID = DisplayIdGetter<DisplayIdType>;
328
329 // The display width and height
330 static constexpr int WIDTH = width;
331 static constexpr int HEIGHT = height;
332
333 static constexpr int GRALLOC_USAGE = grallocUsage;
334
335 // Whether the display is virtual or physical
336 static constexpr Virtual VIRTUAL =
337 IsPhysicalDisplayId<DisplayIdType>{} ? Virtual::FALSE : Virtual::TRUE;
338
339 // When creating native window surfaces for the framebuffer, whether those should be critical
340 static constexpr Critical CRITICAL = critical;
341
342 // When creating native window surfaces for the framebuffer, whether those should be async
343 static constexpr Async ASYNC = async;
344
345 // Whether the display should be treated as secure
346 static constexpr Secure SECURE = secure;
347
348 // Whether the display is primary
349 static constexpr Primary PRIMARY = primary;
350
makeFakeExistingDisplayInjectorandroid::__anon968546880111::DisplayVariant351 static auto makeFakeExistingDisplayInjector(DisplayTransactionTest* test) {
352 auto injector =
353 FakeDisplayDeviceInjector(test->mFlinger, DISPLAY_ID::get(),
354 static_cast<bool>(VIRTUAL), static_cast<bool>(PRIMARY));
355
356 injector.setSecure(static_cast<bool>(SECURE));
357 injector.setNativeWindow(test->mNativeWindow);
358
359 // Creating a DisplayDevice requires getting default dimensions from the
360 // native window along with some other initial setup.
361 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
362 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
363 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
364 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
365 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT))
366 .WillRepeatedly(Return(0));
367 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT))
368 .WillRepeatedly(Return(0));
369 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64))
370 .WillRepeatedly(Return(0));
371 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT))
372 .WillRepeatedly(Return(0));
373
374 return injector;
375 }
376
377 // Called by tests to set up any native window creation call expectations.
setupNativeWindowSurfaceCreationCallExpectationsandroid::__anon968546880111::DisplayVariant378 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
379 EXPECT_CALL(*test->mNativeWindowSurface, getNativeWindow())
380 .WillOnce(Return(test->mNativeWindow));
381
382 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
383 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
384 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
385 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
386 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT))
387 .WillRepeatedly(Return(0));
388 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT))
389 .WillRepeatedly(Return(0));
390 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64))
391 .WillRepeatedly(Return(0));
392 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT))
393 .WillRepeatedly(Return(0));
394 }
395
setupFramebufferConsumerBufferQueueCallExpectationsandroid::__anon968546880111::DisplayVariant396 static void setupFramebufferConsumerBufferQueueCallExpectations(DisplayTransactionTest* test) {
397 EXPECT_CALL(*test->mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR));
398 EXPECT_CALL(*test->mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR));
399 EXPECT_CALL(*test->mConsumer, setConsumerUsageBits(GRALLOC_USAGE))
400 .WillRepeatedly(Return(NO_ERROR));
401 EXPECT_CALL(*test->mConsumer, setDefaultBufferSize(WIDTH, HEIGHT))
402 .WillRepeatedly(Return(NO_ERROR));
403 EXPECT_CALL(*test->mConsumer, setMaxAcquiredBufferCount(_))
404 .WillRepeatedly(Return(NO_ERROR));
405 }
406
setupFramebufferProducerBufferQueueCallExpectationsandroid::__anon968546880111::DisplayVariant407 static void setupFramebufferProducerBufferQueueCallExpectations(DisplayTransactionTest* test) {
408 EXPECT_CALL(*test->mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return());
409 }
410 };
411
412 template <hwc2_display_t hwcDisplayId, HWC2::DisplayType hwcDisplayType, typename DisplayVariant,
413 typename PhysicalDisplay = void>
414 struct HwcDisplayVariant {
415 // The display id supplied by the HWC
416 static constexpr hwc2_display_t HWC_DISPLAY_ID = hwcDisplayId;
417
418 // The HWC display type
419 static constexpr HWC2::DisplayType HWC_DISPLAY_TYPE = hwcDisplayType;
420
421 // The HWC active configuration id
422 static constexpr int HWC_ACTIVE_CONFIG_ID = 2001;
423 static constexpr int INIT_POWER_MODE = HWC_POWER_MODE_NORMAL;
424
injectPendingHotplugEventandroid::__anon968546880111::HwcDisplayVariant425 static void injectPendingHotplugEvent(DisplayTransactionTest* test,
426 HWC2::Connection connection) {
427 test->mFlinger.mutablePendingHotplugEvents().emplace_back(
428 HotplugEvent{HWC_DISPLAY_ID, connection});
429 }
430
431 // Called by tests to inject a HWC display setup
injectHwcDisplayWithNoDefaultCapabilitiesandroid::__anon968546880111::HwcDisplayVariant432 static void injectHwcDisplayWithNoDefaultCapabilities(DisplayTransactionTest* test) {
433 const auto displayId = DisplayVariant::DISPLAY_ID::get();
434 ASSERT_TRUE(displayId);
435 FakeHwcDisplayInjector(*displayId, HWC_DISPLAY_TYPE,
436 static_cast<bool>(DisplayVariant::PRIMARY))
437 .setHwcDisplayId(HWC_DISPLAY_ID)
438 .setWidth(DisplayVariant::WIDTH)
439 .setHeight(DisplayVariant::HEIGHT)
440 .setActiveConfig(HWC_ACTIVE_CONFIG_ID)
441 .setPowerMode(INIT_POWER_MODE)
442 .inject(&test->mFlinger, test->mComposer);
443 }
444
445 // Called by tests to inject a HWC display setup
injectHwcDisplayandroid::__anon968546880111::HwcDisplayVariant446 static void injectHwcDisplay(DisplayTransactionTest* test) {
447 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(HWC_DISPLAY_ID, _))
448 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hwc2::DisplayCapability>({})),
449 Return(Error::NONE)));
450 EXPECT_CALL(*test->mComposer,
451 setPowerMode(HWC_DISPLAY_ID,
452 static_cast<Hwc2::IComposerClient::PowerMode>(INIT_POWER_MODE)))
453 .WillOnce(Return(Error::NONE));
454 injectHwcDisplayWithNoDefaultCapabilities(test);
455 }
456
setupHwcHotplugCallExpectationsandroid::__anon968546880111::HwcDisplayVariant457 static void setupHwcHotplugCallExpectations(DisplayTransactionTest* test) {
458 EXPECT_CALL(*test->mComposer, getDisplayType(HWC_DISPLAY_ID, _))
459 .WillOnce(DoAll(SetArgPointee<1>(static_cast<IComposerClient::DisplayType>(
460 HWC_DISPLAY_TYPE)),
461 Return(Error::NONE)));
462 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
463 EXPECT_CALL(*test->mComposer, getDisplayConfigs(HWC_DISPLAY_ID, _))
464 .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{HWC_ACTIVE_CONFIG_ID}),
465 Return(Error::NONE)));
466 EXPECT_CALL(*test->mComposer,
467 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
468 IComposerClient::Attribute::WIDTH, _))
469 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::WIDTH), Return(Error::NONE)));
470 EXPECT_CALL(*test->mComposer,
471 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
472 IComposerClient::Attribute::HEIGHT, _))
473 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::HEIGHT), Return(Error::NONE)));
474 EXPECT_CALL(*test->mComposer,
475 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
476 IComposerClient::Attribute::VSYNC_PERIOD, _))
477 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE)));
478 EXPECT_CALL(*test->mComposer,
479 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
480 IComposerClient::Attribute::DPI_X, _))
481 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
482 EXPECT_CALL(*test->mComposer,
483 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
484 IComposerClient::Attribute::DPI_Y, _))
485 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
486
487 if (PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
488 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
489 .WillOnce(DoAll(SetArgPointee<1>(PhysicalDisplay::PORT),
490 SetArgPointee<2>(PhysicalDisplay::GET_IDENTIFICATION_DATA()),
491 Return(Error::NONE)));
492 } else {
493 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
494 .WillOnce(Return(Error::UNSUPPORTED));
495 }
496 }
497
498 // Called by tests to set up HWC call expectations
setupHwcGetActiveConfigCallExpectationsandroid::__anon968546880111::HwcDisplayVariant499 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
500 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY_ID, _))
501 .WillRepeatedly(DoAll(SetArgPointee<1>(HWC_ACTIVE_CONFIG_ID), Return(Error::NONE)));
502 }
503 };
504
505 // Physical displays are expected to be synchronous, secure, and have a HWC display for output.
506 constexpr uint32_t GRALLOC_USAGE_PHYSICAL_DISPLAY =
507 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
508
509 template <hwc2_display_t hwcDisplayId, typename PhysicalDisplay, int width, int height,
510 Critical critical>
511 struct PhysicalDisplayVariant
512 : DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height, critical, Async::FALSE,
513 Secure::TRUE, PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
514 HwcDisplayVariant<hwcDisplayId, HWC2::DisplayType::Physical,
515 DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height,
516 critical, Async::FALSE, Secure::TRUE,
517 PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
518 PhysicalDisplay> {};
519
520 template <bool hasIdentificationData>
521 struct PrimaryDisplay {
522 static constexpr Primary PRIMARY = Primary::TRUE;
523 static constexpr uint8_t PORT = 255;
524 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
525 static constexpr auto GET_IDENTIFICATION_DATA = getInternalEdid;
526 };
527
528 template <bool hasIdentificationData>
529 struct ExternalDisplay {
530 static constexpr Primary PRIMARY = Primary::FALSE;
531 static constexpr uint8_t PORT = 254;
532 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
533 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
534 };
535
536 struct TertiaryDisplay {
537 static constexpr Primary PRIMARY = Primary::FALSE;
538 static constexpr uint8_t PORT = 253;
539 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
540 };
541
542 // A primary display is a physical display that is critical
543 using PrimaryDisplayVariant =
544 PhysicalDisplayVariant<1001, PrimaryDisplay<false>, 3840, 2160, Critical::TRUE>;
545
546 // An external display is physical display that is not critical.
547 using ExternalDisplayVariant =
548 PhysicalDisplayVariant<1002, ExternalDisplay<false>, 1920, 1280, Critical::FALSE>;
549
550 using TertiaryDisplayVariant =
551 PhysicalDisplayVariant<1003, TertiaryDisplay, 1600, 1200, Critical::FALSE>;
552
553 // A virtual display not supported by the HWC.
554 constexpr uint32_t GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY = 0;
555
556 template <int width, int height, Secure secure>
557 struct NonHwcVirtualDisplayVariant
558 : DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
559 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY> {
560 using Base = DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
561 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>;
562
injectHwcDisplayandroid::__anon968546880111::NonHwcVirtualDisplayVariant563 static void injectHwcDisplay(DisplayTransactionTest*) {}
564
setupHwcGetActiveConfigCallExpectationsandroid::__anon968546880111::NonHwcVirtualDisplayVariant565 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
566 EXPECT_CALL(*test->mComposer, getActiveConfig(_, _)).Times(0);
567 }
568
setupNativeWindowSurfaceCreationCallExpectationsandroid::__anon968546880111::NonHwcVirtualDisplayVariant569 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
570 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
571 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
572 }
573 };
574
575 // A virtual display supported by the HWC.
576 constexpr uint32_t GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY = GRALLOC_USAGE_HW_COMPOSER;
577
578 template <int width, int height, Secure secure>
579 struct HwcVirtualDisplayVariant
580 : DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE, secure,
581 Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>,
582 HwcDisplayVariant<
583 1010, HWC2::DisplayType::Virtual,
584 DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
585 secure, Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>> {
586 using Base = DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
587 secure, Primary::FALSE, GRALLOC_USAGE_HW_COMPOSER>;
588 using Self = HwcVirtualDisplayVariant<width, height, secure>;
589
setupNativeWindowSurfaceCreationCallExpectationsandroid::__anon968546880111::HwcVirtualDisplayVariant590 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
591 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
592 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
593 }
594
setupHwcVirtualDisplayCreationCallExpectationsandroid::__anon968546880111::HwcVirtualDisplayVariant595 static void setupHwcVirtualDisplayCreationCallExpectations(DisplayTransactionTest* test) {
596 EXPECT_CALL(*test->mComposer, createVirtualDisplay(Base::WIDTH, Base::HEIGHT, _, _))
597 .WillOnce(DoAll(SetArgPointee<3>(Self::HWC_DISPLAY_ID), Return(Error::NONE)));
598 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
599 }
600 };
601
602 // For this variant, SurfaceFlinger should not configure itself with wide
603 // display support, so the display should not be configured for wide-color
604 // support.
605 struct WideColorSupportNotConfiguredVariant {
606 static constexpr bool WIDE_COLOR_SUPPORTED = false;
607
injectConfigChangeandroid::__anon968546880111::WideColorSupportNotConfiguredVariant608 static void injectConfigChange(DisplayTransactionTest* test) {
609 test->mFlinger.mutableHasWideColorDisplay() = false;
610 test->mFlinger.mutableUseColorManagement() = false;
611 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
612 }
613
setupComposerCallExpectationsandroid::__anon968546880111::WideColorSupportNotConfiguredVariant614 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
615 EXPECT_CALL(*test->mComposer, getColorModes(_, _)).Times(0);
616 EXPECT_CALL(*test->mComposer, getRenderIntents(_, _, _)).Times(0);
617 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
618 }
619 };
620
621 // For this variant, SurfaceFlinger should configure itself with wide display
622 // support, and the display should respond with an non-empty list of supported
623 // color modes. Wide-color support should be configured.
624 template <typename Display>
625 struct WideColorP3ColorimetricSupportedVariant {
626 static constexpr bool WIDE_COLOR_SUPPORTED = true;
627
injectConfigChangeandroid::__anon968546880111::WideColorP3ColorimetricSupportedVariant628 static void injectConfigChange(DisplayTransactionTest* test) {
629 test->mFlinger.mutableUseColorManagement() = true;
630 test->mFlinger.mutableHasWideColorDisplay() = true;
631 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
632 }
633
setupComposerCallExpectationsandroid::__anon968546880111::WideColorP3ColorimetricSupportedVariant634 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
635 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_DATASPACE)).Times(1);
636
637 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
638 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>({ColorMode::DISPLAY_P3})),
639 Return(Error::NONE)));
640 EXPECT_CALL(*test->mComposer,
641 getRenderIntents(Display::HWC_DISPLAY_ID, ColorMode::DISPLAY_P3, _))
642 .WillOnce(DoAll(SetArgPointee<2>(
643 std::vector<RenderIntent>({RenderIntent::COLORIMETRIC})),
644 Return(Error::NONE)));
645 EXPECT_CALL(*test->mComposer,
646 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::SRGB,
647 RenderIntent::COLORIMETRIC))
648 .WillOnce(Return(Error::NONE));
649 }
650 };
651
652 // For this variant, SurfaceFlinger should configure itself with wide display
653 // support, but the display should respond with an empty list of supported color
654 // modes. Wide-color support for the display should not be configured.
655 template <typename Display>
656 struct WideColorNotSupportedVariant {
657 static constexpr bool WIDE_COLOR_SUPPORTED = false;
658
injectConfigChangeandroid::__anon968546880111::WideColorNotSupportedVariant659 static void injectConfigChange(DisplayTransactionTest* test) {
660 test->mFlinger.mutableUseColorManagement() = true;
661 test->mFlinger.mutableHasWideColorDisplay() = true;
662 }
663
setupComposerCallExpectationsandroid::__anon968546880111::WideColorNotSupportedVariant664 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
665 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
666 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>()), Return(Error::NONE)));
667 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
668 }
669 };
670
671 // For this variant, the display is not a HWC display, so no HDR support should
672 // be configured.
673 struct NonHwcDisplayHdrSupportVariant {
674 static constexpr bool HDR10_PLUS_SUPPORTED = false;
675 static constexpr bool HDR10_SUPPORTED = false;
676 static constexpr bool HDR_HLG_SUPPORTED = false;
677 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
setupComposerCallExpectationsandroid::__anon968546880111::NonHwcDisplayHdrSupportVariant678 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
679 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
680 }
681 };
682
683 template <typename Display>
684 struct Hdr10PlusSupportedVariant {
685 static constexpr bool HDR10_PLUS_SUPPORTED = true;
686 static constexpr bool HDR10_SUPPORTED = true;
687 static constexpr bool HDR_HLG_SUPPORTED = false;
688 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
setupComposerCallExpectationsandroid::__anon968546880111::Hdr10PlusSupportedVariant689 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
690 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _))
691 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({
692 Hdr::HDR10_PLUS,
693 Hdr::HDR10,
694 })),
695 Return(Error::NONE)));
696 }
697 };
698
699 // For this variant, the composer should respond with a non-empty list of HDR
700 // modes containing HDR10, so HDR10 support should be configured.
701 template <typename Display>
702 struct Hdr10SupportedVariant {
703 static constexpr bool HDR10_PLUS_SUPPORTED = false;
704 static constexpr bool HDR10_SUPPORTED = true;
705 static constexpr bool HDR_HLG_SUPPORTED = false;
706 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
setupComposerCallExpectationsandroid::__anon968546880111::Hdr10SupportedVariant707 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
708 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
709 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HDR10})),
710 Return(Error::NONE)));
711 }
712 };
713
714 // For this variant, the composer should respond with a non-empty list of HDR
715 // modes containing HLG, so HLG support should be configured.
716 template <typename Display>
717 struct HdrHlgSupportedVariant {
718 static constexpr bool HDR10_PLUS_SUPPORTED = false;
719 static constexpr bool HDR10_SUPPORTED = false;
720 static constexpr bool HDR_HLG_SUPPORTED = true;
721 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
setupComposerCallExpectationsandroid::__anon968546880111::HdrHlgSupportedVariant722 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
723 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
724 .WillOnce(
725 DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HLG})), Return(Error::NONE)));
726 }
727 };
728
729 // For this variant, the composer should respond with a non-empty list of HDR
730 // modes containing DOLBY_VISION, so DOLBY_VISION support should be configured.
731 template <typename Display>
732 struct HdrDolbyVisionSupportedVariant {
733 static constexpr bool HDR10_PLUS_SUPPORTED = false;
734 static constexpr bool HDR10_SUPPORTED = false;
735 static constexpr bool HDR_HLG_SUPPORTED = false;
736 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = true;
setupComposerCallExpectationsandroid::__anon968546880111::HdrDolbyVisionSupportedVariant737 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
738 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
739 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::DOLBY_VISION})),
740 Return(Error::NONE)));
741 }
742 };
743
744 // For this variant, the composer should respond with am empty list of HDR
745 // modes, so no HDR support should be configured.
746 template <typename Display>
747 struct HdrNotSupportedVariant {
748 static constexpr bool HDR10_PLUS_SUPPORTED = false;
749 static constexpr bool HDR10_SUPPORTED = false;
750 static constexpr bool HDR_HLG_SUPPORTED = false;
751 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
setupComposerCallExpectationsandroid::__anon968546880111::HdrNotSupportedVariant752 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
753 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
754 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
755 }
756 };
757
758 struct NonHwcPerFrameMetadataSupportVariant {
759 static constexpr int PER_FRAME_METADATA_KEYS = 0;
setupComposerCallExpectationsandroid::__anon968546880111::NonHwcPerFrameMetadataSupportVariant760 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
761 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(_)).Times(0);
762 }
763 };
764
765 template <typename Display>
766 struct NoPerFrameMetadataSupportVariant {
767 static constexpr int PER_FRAME_METADATA_KEYS = 0;
setupComposerCallExpectationsandroid::__anon968546880111::NoPerFrameMetadataSupportVariant768 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
769 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
770 .WillOnce(Return(std::vector<PerFrameMetadataKey>()));
771 }
772 };
773
774 template <typename Display>
775 struct Smpte2086PerFrameMetadataSupportVariant {
776 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::SMPTE2086;
setupComposerCallExpectationsandroid::__anon968546880111::Smpte2086PerFrameMetadataSupportVariant777 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
778 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
779 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
780 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X,
781 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y,
782 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X,
783 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y,
784 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X,
785 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y,
786 PerFrameMetadataKey::WHITE_POINT_X,
787 PerFrameMetadataKey::WHITE_POINT_Y,
788 PerFrameMetadataKey::MAX_LUMINANCE,
789 PerFrameMetadataKey::MIN_LUMINANCE,
790 })));
791 }
792 };
793
794 template <typename Display>
795 struct Cta861_3_PerFrameMetadataSupportVariant {
796 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::CTA861_3;
setupComposerCallExpectationsandroid::__anon968546880111::Cta861_3_PerFrameMetadataSupportVariant797 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
798 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
799 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
800 PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL,
801 PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL,
802 })));
803 }
804 };
805
806 template <typename Display>
807 struct Hdr10_Plus_PerFrameMetadataSupportVariant {
808 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::HDR10PLUS;
setupComposerCallExpectationsandroid::__anon968546880111::Hdr10_Plus_PerFrameMetadataSupportVariant809 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
810 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
811 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
812 PerFrameMetadataKey::HDR10_PLUS_SEI,
813 })));
814 }
815 };
816 /* ------------------------------------------------------------------------
817 * Typical display configurations to test
818 */
819
820 template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy,
821 typename PerFrameMetadataSupportPolicy>
822 struct Case {
823 using Display = DisplayPolicy;
824 using WideColorSupport = WideColorSupportPolicy;
825 using HdrSupport = HdrSupportPolicy;
826 using PerFrameMetadataSupport = PerFrameMetadataSupportPolicy;
827 };
828
829 using SimplePrimaryDisplayCase =
830 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
831 HdrNotSupportedVariant<PrimaryDisplayVariant>,
832 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
833 using SimpleExternalDisplayCase =
834 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
835 HdrNotSupportedVariant<ExternalDisplayVariant>,
836 NoPerFrameMetadataSupportVariant<ExternalDisplayVariant>>;
837 using SimpleTertiaryDisplayCase =
838 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
839 HdrNotSupportedVariant<TertiaryDisplayVariant>,
840 NoPerFrameMetadataSupportVariant<TertiaryDisplayVariant>>;
841 using NonHwcVirtualDisplayCase =
842 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
843 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant,
844 NonHwcPerFrameMetadataSupportVariant>;
845 using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
846 using HwcVirtualDisplayCase =
847 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
848 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>,
849 NoPerFrameMetadataSupportVariant<SimpleHwcVirtualDisplayVariant>>;
850 using WideColorP3ColorimetricDisplayCase =
851 Case<PrimaryDisplayVariant, WideColorP3ColorimetricSupportedVariant<PrimaryDisplayVariant>,
852 HdrNotSupportedVariant<PrimaryDisplayVariant>,
853 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
854 using Hdr10PlusDisplayCase =
855 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
856 Hdr10SupportedVariant<PrimaryDisplayVariant>,
857 Hdr10_Plus_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
858 using Hdr10DisplayCase =
859 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
860 Hdr10SupportedVariant<PrimaryDisplayVariant>,
861 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
862 using HdrHlgDisplayCase =
863 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
864 HdrHlgSupportedVariant<PrimaryDisplayVariant>,
865 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
866 using HdrDolbyVisionDisplayCase =
867 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
868 HdrDolbyVisionSupportedVariant<PrimaryDisplayVariant>,
869 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
870 using HdrSmpte2086DisplayCase =
871 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
872 HdrNotSupportedVariant<PrimaryDisplayVariant>,
873 Smpte2086PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
874 using HdrCta861_3_DisplayCase =
875 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
876 HdrNotSupportedVariant<PrimaryDisplayVariant>,
877 Cta861_3_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
878
879 /* ------------------------------------------------------------------------
880 *
881 * SurfaceFlinger::onHotplugReceived
882 */
883
TEST_F(DisplayTransactionTest,hotplugEnqueuesEventsForDisplayTransaction)884 TEST_F(DisplayTransactionTest, hotplugEnqueuesEventsForDisplayTransaction) {
885 constexpr int currentSequenceId = 123;
886 constexpr hwc2_display_t hwcDisplayId1 = 456;
887 constexpr hwc2_display_t hwcDisplayId2 = 654;
888
889 // --------------------------------------------------------------------
890 // Preconditions
891
892 // Set the current sequence id for accepted events
893 mFlinger.mutableComposerSequenceId() = currentSequenceId;
894
895 // Set the main thread id so that the current thread does not appear to be
896 // the main thread.
897 mFlinger.mutableMainThreadId() = std::thread::id();
898
899 // --------------------------------------------------------------------
900 // Call Expectations
901
902 // We expect invalidate() to be invoked once to trigger display transaction
903 // processing.
904 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
905
906 // --------------------------------------------------------------------
907 // Invocation
908
909 // Simulate two hotplug events (a connect and a disconnect)
910 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId1, HWC2::Connection::Connected);
911 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId2, HWC2::Connection::Disconnected);
912
913 // --------------------------------------------------------------------
914 // Postconditions
915
916 // The display transaction needed flag should be set.
917 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
918
919 // All events should be in the pending event queue.
920 const auto& pendingEvents = mFlinger.mutablePendingHotplugEvents();
921 ASSERT_EQ(2u, pendingEvents.size());
922 EXPECT_EQ(hwcDisplayId1, pendingEvents[0].hwcDisplayId);
923 EXPECT_EQ(HWC2::Connection::Connected, pendingEvents[0].connection);
924 EXPECT_EQ(hwcDisplayId2, pendingEvents[1].hwcDisplayId);
925 EXPECT_EQ(HWC2::Connection::Disconnected, pendingEvents[1].connection);
926 }
927
TEST_F(DisplayTransactionTest,hotplugDiscardsUnexpectedEvents)928 TEST_F(DisplayTransactionTest, hotplugDiscardsUnexpectedEvents) {
929 constexpr int currentSequenceId = 123;
930 constexpr int otherSequenceId = 321;
931 constexpr hwc2_display_t displayId = 456;
932
933 // --------------------------------------------------------------------
934 // Preconditions
935
936 // Set the current sequence id for accepted events
937 mFlinger.mutableComposerSequenceId() = currentSequenceId;
938
939 // Set the main thread id so that the current thread does not appear to be
940 // the main thread.
941 mFlinger.mutableMainThreadId() = std::thread::id();
942
943 // --------------------------------------------------------------------
944 // Call Expectations
945
946 // We do not expect any calls to invalidate().
947 EXPECT_CALL(*mMessageQueue, invalidate()).Times(0);
948
949 // --------------------------------------------------------------------
950 // Invocation
951
952 // Call with an unexpected sequence id
953 mFlinger.onHotplugReceived(otherSequenceId, displayId, HWC2::Connection::Invalid);
954
955 // --------------------------------------------------------------------
956 // Postconditions
957
958 // The display transaction needed flag should not be set
959 EXPECT_FALSE(hasTransactionFlagSet(eDisplayTransactionNeeded));
960
961 // There should be no pending events
962 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
963 }
964
TEST_F(DisplayTransactionTest,hotplugProcessesEnqueuedEventsIfCalledOnMainThread)965 TEST_F(DisplayTransactionTest, hotplugProcessesEnqueuedEventsIfCalledOnMainThread) {
966 constexpr int currentSequenceId = 123;
967 constexpr hwc2_display_t displayId1 = 456;
968
969 // --------------------------------------------------------------------
970 // Note:
971 // --------------------------------------------------------------------
972 // This test case is a bit tricky. We want to verify that
973 // onHotplugReceived() calls processDisplayHotplugEventsLocked(), but we
974 // don't really want to provide coverage for everything the later function
975 // does as there are specific tests for it.
976 // --------------------------------------------------------------------
977
978 // --------------------------------------------------------------------
979 // Preconditions
980
981 // Set the current sequence id for accepted events
982 mFlinger.mutableComposerSequenceId() = currentSequenceId;
983
984 // Set the main thread id so that the current thread does appear to be the
985 // main thread.
986 mFlinger.mutableMainThreadId() = std::this_thread::get_id();
987
988 // --------------------------------------------------------------------
989 // Call Expectations
990
991 // We expect invalidate() to be invoked once to trigger display transaction
992 // processing.
993 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
994
995 // --------------------------------------------------------------------
996 // Invocation
997
998 // Simulate a disconnect on a display id that is not connected. This should
999 // be enqueued by onHotplugReceived(), and dequeued by
1000 // processDisplayHotplugEventsLocked(), but then ignored as invalid.
1001 mFlinger.onHotplugReceived(currentSequenceId, displayId1, HWC2::Connection::Disconnected);
1002
1003 // --------------------------------------------------------------------
1004 // Postconditions
1005
1006 // The display transaction needed flag should be set.
1007 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1008
1009 // There should be no event queued on return, as it should have been
1010 // processed.
1011 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
1012 }
1013
1014 /* ------------------------------------------------------------------------
1015 * SurfaceFlinger::createDisplay
1016 */
1017
TEST_F(DisplayTransactionTest,createDisplaySetsCurrentStateForNonsecureDisplay)1018 TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForNonsecureDisplay) {
1019 const String8 name("virtual.test");
1020
1021 // --------------------------------------------------------------------
1022 // Call Expectations
1023
1024 // The call should notify the interceptor that a display was created.
1025 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
1026
1027 // --------------------------------------------------------------------
1028 // Invocation
1029
1030 sp<IBinder> displayToken = mFlinger.createDisplay(name, false);
1031
1032 // --------------------------------------------------------------------
1033 // Postconditions
1034
1035 // The display should have been added to the current state
1036 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1037 const auto& display = getCurrentDisplayState(displayToken);
1038 EXPECT_TRUE(display.isVirtual());
1039 EXPECT_FALSE(display.isSecure);
1040 EXPECT_EQ(name.string(), display.displayName);
1041
1042 // --------------------------------------------------------------------
1043 // Cleanup conditions
1044
1045 // Destroying the display invalidates the display state.
1046 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1047 }
1048
TEST_F(DisplayTransactionTest,createDisplaySetsCurrentStateForSecureDisplay)1049 TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForSecureDisplay) {
1050 const String8 name("virtual.test");
1051
1052 // --------------------------------------------------------------------
1053 // Call Expectations
1054
1055 // The call should notify the interceptor that a display was created.
1056 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
1057
1058 // --------------------------------------------------------------------
1059 // Invocation
1060
1061 sp<IBinder> displayToken = mFlinger.createDisplay(name, true);
1062
1063 // --------------------------------------------------------------------
1064 // Postconditions
1065
1066 // The display should have been added to the current state
1067 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1068 const auto& display = getCurrentDisplayState(displayToken);
1069 EXPECT_TRUE(display.isVirtual());
1070 EXPECT_TRUE(display.isSecure);
1071 EXPECT_EQ(name.string(), display.displayName);
1072
1073 // --------------------------------------------------------------------
1074 // Cleanup conditions
1075
1076 // Destroying the display invalidates the display state.
1077 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1078 }
1079
1080 /* ------------------------------------------------------------------------
1081 * SurfaceFlinger::destroyDisplay
1082 */
1083
TEST_F(DisplayTransactionTest,destroyDisplayClearsCurrentStateForDisplay)1084 TEST_F(DisplayTransactionTest, destroyDisplayClearsCurrentStateForDisplay) {
1085 using Case = NonHwcVirtualDisplayCase;
1086
1087 // --------------------------------------------------------------------
1088 // Preconditions
1089
1090 // A virtual display exists
1091 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1092 existing.inject();
1093
1094 // --------------------------------------------------------------------
1095 // Call Expectations
1096
1097 // The call should notify the interceptor that a display was created.
1098 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
1099
1100 // Destroying the display invalidates the display state.
1101 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1102
1103 // --------------------------------------------------------------------
1104 // Invocation
1105
1106 mFlinger.destroyDisplay(existing.token());
1107
1108 // --------------------------------------------------------------------
1109 // Postconditions
1110
1111 // The display should have been removed from the current state
1112 EXPECT_FALSE(hasCurrentDisplayState(existing.token()));
1113
1114 // Ths display should still exist in the drawing state
1115 EXPECT_TRUE(hasDrawingDisplayState(existing.token()));
1116
1117 // The display transaction needed flasg should be set
1118 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1119 }
1120
TEST_F(DisplayTransactionTest,destroyDisplayHandlesUnknownDisplay)1121 TEST_F(DisplayTransactionTest, destroyDisplayHandlesUnknownDisplay) {
1122 // --------------------------------------------------------------------
1123 // Preconditions
1124
1125 sp<BBinder> displayToken = new BBinder();
1126
1127 // --------------------------------------------------------------------
1128 // Invocation
1129
1130 mFlinger.destroyDisplay(displayToken);
1131 }
1132
1133 /* ------------------------------------------------------------------------
1134 * SurfaceFlinger::resetDisplayState
1135 */
1136
TEST_F(DisplayTransactionTest,resetDisplayStateClearsState)1137 TEST_F(DisplayTransactionTest, resetDisplayStateClearsState) {
1138 using Case = NonHwcVirtualDisplayCase;
1139
1140 // --------------------------------------------------------------------
1141 // Preconditions
1142
1143 // vsync is enabled and available
1144 mScheduler->mutablePrimaryHWVsyncEnabled() = true;
1145 mScheduler->mutableHWVsyncAvailable() = true;
1146
1147 // A display exists
1148 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1149 existing.inject();
1150
1151 // --------------------------------------------------------------------
1152 // Call Expectations
1153
1154 // The call disable vsyncs
1155 EXPECT_CALL(*mEventControlThread, setVsyncEnabled(false)).Times(1);
1156
1157 // The call ends any display resyncs
1158 EXPECT_CALL(*mPrimaryDispSync, endResync()).Times(1);
1159
1160 // --------------------------------------------------------------------
1161 // Invocation
1162
1163 mFlinger.resetDisplayState();
1164
1165 // --------------------------------------------------------------------
1166 // Postconditions
1167
1168 // vsyncs should be off and not available.
1169 EXPECT_FALSE(mScheduler->mutablePrimaryHWVsyncEnabled());
1170 EXPECT_FALSE(mScheduler->mutableHWVsyncAvailable());
1171
1172 // The display should have been removed from the display map.
1173 EXPECT_FALSE(hasDisplayDevice(existing.token()));
1174
1175 // The display should still exist in the current state
1176 EXPECT_TRUE(hasCurrentDisplayState(existing.token()));
1177
1178 // The display should have been removed from the drawing state
1179 EXPECT_FALSE(hasDrawingDisplayState(existing.token()));
1180 }
1181
1182 /* ------------------------------------------------------------------------
1183 * DisplayDevice::GetBestColorMode
1184 */
1185 class GetBestColorModeTest : public DisplayTransactionTest {
1186 public:
1187 static constexpr DisplayId DEFAULT_DISPLAY_ID = DisplayId{777};
1188
GetBestColorModeTest()1189 GetBestColorModeTest()
1190 : DisplayTransactionTest(),
1191 mInjector(FakeDisplayDeviceInjector(mFlinger, DEFAULT_DISPLAY_ID, false /* isVirtual */,
1192 true /* isPrimary */)) {}
1193
setHasWideColorGamut(bool hasWideColorGamut)1194 void setHasWideColorGamut(bool hasWideColorGamut) { mHasWideColorGamut = hasWideColorGamut; }
1195
addHwcColorModesMapping(ui::ColorMode colorMode,std::vector<ui::RenderIntent> renderIntents)1196 void addHwcColorModesMapping(ui::ColorMode colorMode,
1197 std::vector<ui::RenderIntent> renderIntents) {
1198 mHwcColorModes[colorMode] = renderIntents;
1199 }
1200
setInputDataspace(ui::Dataspace dataspace)1201 void setInputDataspace(ui::Dataspace dataspace) { mInputDataspace = dataspace; }
1202
setInputRenderIntent(ui::RenderIntent renderIntent)1203 void setInputRenderIntent(ui::RenderIntent renderIntent) { mInputRenderIntent = renderIntent; }
1204
getBestColorMode()1205 void getBestColorMode() {
1206 mInjector.setHwcColorModes(mHwcColorModes);
1207 mInjector.setHasWideColorGamut(mHasWideColorGamut);
1208 mInjector.setNativeWindow(mNativeWindow);
1209
1210 // Creating a DisplayDevice requires getting default dimensions from the
1211 // native window.
1212 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
1213 .WillRepeatedly(DoAll(SetArgPointee<1>(1080 /* arbitrary */), Return(0)));
1214 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
1215 .WillRepeatedly(DoAll(SetArgPointee<1>(1920 /* arbitrary */), Return(0)));
1216 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT)).Times(1);
1217 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT)).Times(1);
1218 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64)).Times(1);
1219 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT)).Times(1);
1220 auto displayDevice = mInjector.inject();
1221
1222 displayDevice->getCompositionDisplay()
1223 ->getDisplayColorProfile()
1224 ->getBestColorMode(mInputDataspace, mInputRenderIntent, &mOutDataspace,
1225 &mOutColorMode, &mOutRenderIntent);
1226 }
1227
1228 ui::Dataspace mOutDataspace;
1229 ui::ColorMode mOutColorMode;
1230 ui::RenderIntent mOutRenderIntent;
1231
1232 private:
1233 ui::Dataspace mInputDataspace;
1234 ui::RenderIntent mInputRenderIntent;
1235 bool mHasWideColorGamut = false;
1236 std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> mHwcColorModes;
1237 FakeDisplayDeviceInjector mInjector;
1238 };
1239
TEST_F(GetBestColorModeTest,DataspaceDisplayP3_ColorModeSRGB)1240 TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeSRGB) {
1241 addHwcColorModesMapping(ui::ColorMode::SRGB,
1242 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1243 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1244 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1245 setHasWideColorGamut(true);
1246
1247 getBestColorMode();
1248
1249 ASSERT_EQ(ui::Dataspace::V0_SRGB, mOutDataspace);
1250 ASSERT_EQ(ui::ColorMode::SRGB, mOutColorMode);
1251 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1252 }
1253
TEST_F(GetBestColorModeTest,DataspaceDisplayP3_ColorModeDisplayP3)1254 TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDisplayP3) {
1255 addHwcColorModesMapping(ui::ColorMode::DISPLAY_P3,
1256 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1257 addHwcColorModesMapping(ui::ColorMode::SRGB,
1258 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1259 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1260 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1261 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1262 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1263 setHasWideColorGamut(true);
1264
1265 getBestColorMode();
1266
1267 ASSERT_EQ(ui::Dataspace::DISPLAY_P3, mOutDataspace);
1268 ASSERT_EQ(ui::ColorMode::DISPLAY_P3, mOutColorMode);
1269 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1270 }
1271
TEST_F(GetBestColorModeTest,DataspaceDisplayP3_ColorModeDISPLAY_BT2020)1272 TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDISPLAY_BT2020) {
1273 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1274 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1275 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1276 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1277 setHasWideColorGamut(true);
1278
1279 getBestColorMode();
1280
1281 ASSERT_EQ(ui::Dataspace::DISPLAY_BT2020, mOutDataspace);
1282 ASSERT_EQ(ui::ColorMode::DISPLAY_BT2020, mOutColorMode);
1283 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1284 }
1285
1286 /* ------------------------------------------------------------------------
1287 * SurfaceFlinger::getDisplayNativePrimaries
1288 */
1289
1290 class GetDisplayNativePrimaries : public DisplayTransactionTest {
1291 public:
1292 GetDisplayNativePrimaries();
1293 void populateDummyDisplayNativePrimaries(ui::DisplayPrimaries& primaries);
1294 void checkDummyDisplayNativePrimaries(const ui::DisplayPrimaries& primaries);
1295
1296 private:
1297 static constexpr float mStartingTestValue = 1.0f;
1298 };
1299
GetDisplayNativePrimaries()1300 GetDisplayNativePrimaries::GetDisplayNativePrimaries() {
1301 SimplePrimaryDisplayCase::Display::injectHwcDisplay(this);
1302 injectFakeNativeWindowSurfaceFactory();
1303 }
1304
populateDummyDisplayNativePrimaries(ui::DisplayPrimaries & primaries)1305 void GetDisplayNativePrimaries::populateDummyDisplayNativePrimaries(
1306 ui::DisplayPrimaries& primaries) {
1307 float startingVal = mStartingTestValue;
1308 primaries.red.X = startingVal++;
1309 primaries.red.Y = startingVal++;
1310 primaries.red.Z = startingVal++;
1311 primaries.green.X = startingVal++;
1312 primaries.green.Y = startingVal++;
1313 primaries.green.Z = startingVal++;
1314 primaries.blue.X = startingVal++;
1315 primaries.blue.Y = startingVal++;
1316 primaries.blue.Z = startingVal++;
1317 primaries.white.X = startingVal++;
1318 primaries.white.Y = startingVal++;
1319 primaries.white.Z = startingVal++;
1320 }
1321
checkDummyDisplayNativePrimaries(const ui::DisplayPrimaries & primaries)1322 void GetDisplayNativePrimaries::checkDummyDisplayNativePrimaries(
1323 const ui::DisplayPrimaries& primaries) {
1324 float startingVal = mStartingTestValue;
1325 EXPECT_EQ(primaries.red.X, startingVal++);
1326 EXPECT_EQ(primaries.red.Y, startingVal++);
1327 EXPECT_EQ(primaries.red.Z, startingVal++);
1328 EXPECT_EQ(primaries.green.X, startingVal++);
1329 EXPECT_EQ(primaries.green.Y, startingVal++);
1330 EXPECT_EQ(primaries.green.Z, startingVal++);
1331 EXPECT_EQ(primaries.blue.X, startingVal++);
1332 EXPECT_EQ(primaries.blue.Y, startingVal++);
1333 EXPECT_EQ(primaries.blue.Z, startingVal++);
1334 EXPECT_EQ(primaries.white.X, startingVal++);
1335 EXPECT_EQ(primaries.white.Y, startingVal++);
1336 EXPECT_EQ(primaries.white.Z, startingVal++);
1337 }
1338
TEST_F(GetDisplayNativePrimaries,nullDisplayToken)1339 TEST_F(GetDisplayNativePrimaries, nullDisplayToken) {
1340 ui::DisplayPrimaries primaries;
1341 EXPECT_EQ(BAD_VALUE, mFlinger.getDisplayNativePrimaries(nullptr, primaries));
1342 }
1343
TEST_F(GetDisplayNativePrimaries,internalDisplayWithPrimariesData)1344 TEST_F(GetDisplayNativePrimaries, internalDisplayWithPrimariesData) {
1345 auto injector = SimplePrimaryDisplayCase::Display::makeFakeExistingDisplayInjector(this);
1346 injector.inject();
1347 auto internalDisplayToken = injector.token();
1348
1349 ui::DisplayPrimaries expectedPrimaries;
1350 populateDummyDisplayNativePrimaries(expectedPrimaries);
1351 mFlinger.setInternalDisplayPrimaries(expectedPrimaries);
1352
1353 ui::DisplayPrimaries primaries;
1354 EXPECT_EQ(NO_ERROR, mFlinger.getDisplayNativePrimaries(internalDisplayToken, primaries));
1355
1356 checkDummyDisplayNativePrimaries(primaries);
1357 }
1358
TEST_F(GetDisplayNativePrimaries,notInternalDisplayToken)1359 TEST_F(GetDisplayNativePrimaries, notInternalDisplayToken) {
1360 sp<BBinder> notInternalDisplayToken = new BBinder();
1361
1362 ui::DisplayPrimaries primaries;
1363 populateDummyDisplayNativePrimaries(primaries);
1364 EXPECT_EQ(BAD_VALUE, mFlinger.getDisplayNativePrimaries(notInternalDisplayToken, primaries));
1365
1366 // Check primaries argument wasn't modified in case of failure
1367 checkDummyDisplayNativePrimaries(primaries);
1368 }
1369
1370 /* ------------------------------------------------------------------------
1371 * SurfaceFlinger::setupNewDisplayDeviceInternal
1372 */
1373
1374 class SetupNewDisplayDeviceInternalTest : public DisplayTransactionTest {
1375 public:
1376 template <typename T>
1377 void setupNewDisplayDeviceInternalTest();
1378 };
1379
1380 template <typename Case>
setupNewDisplayDeviceInternalTest()1381 void SetupNewDisplayDeviceInternalTest::setupNewDisplayDeviceInternalTest() {
1382 const sp<BBinder> displayToken = new BBinder();
1383 const sp<compositionengine::mock::DisplaySurface> displaySurface =
1384 new compositionengine::mock::DisplaySurface();
1385 const sp<mock::GraphicBufferProducer> producer = new mock::GraphicBufferProducer();
1386
1387 // --------------------------------------------------------------------
1388 // Preconditions
1389
1390 // Wide color displays support is configured appropriately
1391 Case::WideColorSupport::injectConfigChange(this);
1392
1393 // The display is setup with the HWC.
1394 Case::Display::injectHwcDisplay(this);
1395
1396 // SurfaceFlinger will use a test-controlled factory for native window
1397 // surfaces.
1398 injectFakeNativeWindowSurfaceFactory();
1399
1400 // --------------------------------------------------------------------
1401 // Call Expectations
1402
1403 // Various native window calls will be made.
1404 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1405 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
1406 Case::WideColorSupport::setupComposerCallExpectations(this);
1407 Case::HdrSupport::setupComposerCallExpectations(this);
1408 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
1409
1410 // --------------------------------------------------------------------
1411 // Invocation
1412
1413 DisplayDeviceState state;
1414 state.displayId = static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1415 : Case::Display::DISPLAY_ID::get();
1416 state.isSecure = static_cast<bool>(Case::Display::SECURE);
1417
1418 auto device =
1419 mFlinger.setupNewDisplayDeviceInternal(displayToken, Case::Display::DISPLAY_ID::get(),
1420 state, displaySurface, producer);
1421
1422 // --------------------------------------------------------------------
1423 // Postconditions
1424
1425 ASSERT_TRUE(device != nullptr);
1426 EXPECT_EQ(Case::Display::DISPLAY_ID::get(), device->getId());
1427 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), device->isVirtual());
1428 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
1429 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
1430 EXPECT_EQ(Case::Display::WIDTH, device->getWidth());
1431 EXPECT_EQ(Case::Display::HEIGHT, device->getHeight());
1432 EXPECT_EQ(Case::WideColorSupport::WIDE_COLOR_SUPPORTED, device->hasWideColorGamut());
1433 EXPECT_EQ(Case::HdrSupport::HDR10_PLUS_SUPPORTED, device->hasHDR10PlusSupport());
1434 EXPECT_EQ(Case::HdrSupport::HDR10_SUPPORTED, device->hasHDR10Support());
1435 EXPECT_EQ(Case::HdrSupport::HDR_HLG_SUPPORTED, device->hasHLGSupport());
1436 EXPECT_EQ(Case::HdrSupport::HDR_DOLBY_VISION_SUPPORTED, device->hasDolbyVisionSupport());
1437 // Note: This is not Case::Display::HWC_ACTIVE_CONFIG_ID as the ids are
1438 // remapped, and the test only ever sets up one config. If there were an error
1439 // looking up the remapped index, device->getActiveConfig() would be -1 instead.
1440 EXPECT_EQ(0, device->getActiveConfig());
1441 EXPECT_EQ(Case::PerFrameMetadataSupport::PER_FRAME_METADATA_KEYS,
1442 device->getSupportedPerFrameMetadata());
1443 }
1444
TEST_F(SetupNewDisplayDeviceInternalTest,createSimplePrimaryDisplay)1445 TEST_F(SetupNewDisplayDeviceInternalTest, createSimplePrimaryDisplay) {
1446 setupNewDisplayDeviceInternalTest<SimplePrimaryDisplayCase>();
1447 }
1448
TEST_F(SetupNewDisplayDeviceInternalTest,createSimpleExternalDisplay)1449 TEST_F(SetupNewDisplayDeviceInternalTest, createSimpleExternalDisplay) {
1450 setupNewDisplayDeviceInternalTest<SimpleExternalDisplayCase>();
1451 }
1452
TEST_F(SetupNewDisplayDeviceInternalTest,createNonHwcVirtualDisplay)1453 TEST_F(SetupNewDisplayDeviceInternalTest, createNonHwcVirtualDisplay) {
1454 setupNewDisplayDeviceInternalTest<NonHwcVirtualDisplayCase>();
1455 }
1456
TEST_F(SetupNewDisplayDeviceInternalTest,createHwcVirtualDisplay)1457 TEST_F(SetupNewDisplayDeviceInternalTest, createHwcVirtualDisplay) {
1458 using Case = HwcVirtualDisplayCase;
1459
1460 // Insert display data so that the HWC thinks it created the virtual display.
1461 const auto displayId = Case::Display::DISPLAY_ID::get();
1462 ASSERT_TRUE(displayId);
1463 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
1464
1465 setupNewDisplayDeviceInternalTest<Case>();
1466 }
1467
TEST_F(SetupNewDisplayDeviceInternalTest,createWideColorP3Display)1468 TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3Display) {
1469 setupNewDisplayDeviceInternalTest<WideColorP3ColorimetricDisplayCase>();
1470 }
1471
TEST_F(SetupNewDisplayDeviceInternalTest,createHdr10PlusDisplay)1472 TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10PlusDisplay) {
1473 setupNewDisplayDeviceInternalTest<Hdr10PlusDisplayCase>();
1474 }
1475
TEST_F(SetupNewDisplayDeviceInternalTest,createHdr10Display)1476 TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10Display) {
1477 setupNewDisplayDeviceInternalTest<Hdr10DisplayCase>();
1478 }
1479
TEST_F(SetupNewDisplayDeviceInternalTest,createHdrHlgDisplay)1480 TEST_F(SetupNewDisplayDeviceInternalTest, createHdrHlgDisplay) {
1481 setupNewDisplayDeviceInternalTest<HdrHlgDisplayCase>();
1482 }
1483
TEST_F(SetupNewDisplayDeviceInternalTest,createHdrDolbyVisionDisplay)1484 TEST_F(SetupNewDisplayDeviceInternalTest, createHdrDolbyVisionDisplay) {
1485 setupNewDisplayDeviceInternalTest<HdrDolbyVisionDisplayCase>();
1486 }
1487
TEST_F(SetupNewDisplayDeviceInternalTest,createHdrSmpte2086DisplayCase)1488 TEST_F(SetupNewDisplayDeviceInternalTest, createHdrSmpte2086DisplayCase) {
1489 setupNewDisplayDeviceInternalTest<HdrSmpte2086DisplayCase>();
1490 }
1491
TEST_F(SetupNewDisplayDeviceInternalTest,createHdrCta816_3_DisplayCase)1492 TEST_F(SetupNewDisplayDeviceInternalTest, createHdrCta816_3_DisplayCase) {
1493 setupNewDisplayDeviceInternalTest<HdrCta861_3_DisplayCase>();
1494 }
1495
1496 /* ------------------------------------------------------------------------
1497 * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded)
1498 */
1499
1500 class HandleTransactionLockedTest : public DisplayTransactionTest {
1501 public:
1502 template <typename Case>
1503 void setupCommonPreconditions();
1504
1505 template <typename Case, bool connected>
1506 static void expectHotplugReceived(mock::EventThread*);
1507
1508 template <typename Case>
1509 void setupCommonCallExpectationsForConnectProcessing();
1510
1511 template <typename Case>
1512 void setupCommonCallExpectationsForDisconnectProcessing();
1513
1514 template <typename Case>
1515 void processesHotplugConnectCommon();
1516
1517 template <typename Case>
1518 void ignoresHotplugConnectCommon();
1519
1520 template <typename Case>
1521 void processesHotplugDisconnectCommon();
1522
1523 template <typename Case>
1524 void verifyDisplayIsConnected(const sp<IBinder>& displayToken);
1525
1526 template <typename Case>
1527 void verifyPhysicalDisplayIsConnected();
1528
1529 void verifyDisplayIsNotConnected(const sp<IBinder>& displayToken);
1530 };
1531
1532 template <typename Case>
setupCommonPreconditions()1533 void HandleTransactionLockedTest::setupCommonPreconditions() {
1534 // Wide color displays support is configured appropriately
1535 Case::WideColorSupport::injectConfigChange(this);
1536
1537 // SurfaceFlinger will use a test-controlled factory for BufferQueues
1538 injectFakeBufferQueueFactory();
1539
1540 // SurfaceFlinger will use a test-controlled factory for native window
1541 // surfaces.
1542 injectFakeNativeWindowSurfaceFactory();
1543 }
1544
1545 template <typename Case, bool connected>
expectHotplugReceived(mock::EventThread * eventThread)1546 void HandleTransactionLockedTest::expectHotplugReceived(mock::EventThread* eventThread) {
1547 const auto convert = [](auto physicalDisplayId) {
1548 return std::make_optional(DisplayId{physicalDisplayId});
1549 };
1550
1551 EXPECT_CALL(*eventThread,
1552 onHotplugReceived(ResultOf(convert, Case::Display::DISPLAY_ID::get()), connected))
1553 .Times(1);
1554 }
1555
1556 template <typename Case>
setupCommonCallExpectationsForConnectProcessing()1557 void HandleTransactionLockedTest::setupCommonCallExpectationsForConnectProcessing() {
1558 Case::Display::setupHwcHotplugCallExpectations(this);
1559
1560 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1561 Case::Display::setupFramebufferProducerBufferQueueCallExpectations(this);
1562 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1563 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
1564
1565 Case::WideColorSupport::setupComposerCallExpectations(this);
1566 Case::HdrSupport::setupComposerCallExpectations(this);
1567 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
1568
1569 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
1570 expectHotplugReceived<Case, true>(mEventThread);
1571 expectHotplugReceived<Case, true>(mSFEventThread);
1572 }
1573
1574 template <typename Case>
setupCommonCallExpectationsForDisconnectProcessing()1575 void HandleTransactionLockedTest::setupCommonCallExpectationsForDisconnectProcessing() {
1576 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
1577
1578 expectHotplugReceived<Case, false>(mEventThread);
1579 expectHotplugReceived<Case, false>(mSFEventThread);
1580 }
1581
1582 template <typename Case>
verifyDisplayIsConnected(const sp<IBinder> & displayToken)1583 void HandleTransactionLockedTest::verifyDisplayIsConnected(const sp<IBinder>& displayToken) {
1584 // The display device should have been set up in the list of displays.
1585 ASSERT_TRUE(hasDisplayDevice(displayToken));
1586 const auto& device = getDisplayDevice(displayToken);
1587 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
1588 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
1589
1590 // The display should have been set up in the current display state
1591 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1592 const auto& current = getCurrentDisplayState(displayToken);
1593 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), current.isVirtual());
1594 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1595 : Case::Display::DISPLAY_ID::get(),
1596 current.displayId);
1597
1598 // The display should have been set up in the drawing display state
1599 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1600 const auto& draw = getDrawingDisplayState(displayToken);
1601 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
1602 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1603 : Case::Display::DISPLAY_ID::get(),
1604 draw.displayId);
1605 }
1606
1607 template <typename Case>
verifyPhysicalDisplayIsConnected()1608 void HandleTransactionLockedTest::verifyPhysicalDisplayIsConnected() {
1609 // HWComposer should have an entry for the display
1610 EXPECT_TRUE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1611
1612 // SF should have a display token.
1613 const auto displayId = Case::Display::DISPLAY_ID::get();
1614 ASSERT_TRUE(displayId);
1615 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1616 auto& displayToken = mFlinger.mutablePhysicalDisplayTokens()[*displayId];
1617
1618 verifyDisplayIsConnected<Case>(displayToken);
1619 }
1620
verifyDisplayIsNotConnected(const sp<IBinder> & displayToken)1621 void HandleTransactionLockedTest::verifyDisplayIsNotConnected(const sp<IBinder>& displayToken) {
1622 EXPECT_FALSE(hasDisplayDevice(displayToken));
1623 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1624 EXPECT_FALSE(hasDrawingDisplayState(displayToken));
1625 }
1626
1627 template <typename Case>
processesHotplugConnectCommon()1628 void HandleTransactionLockedTest::processesHotplugConnectCommon() {
1629 // --------------------------------------------------------------------
1630 // Preconditions
1631
1632 setupCommonPreconditions<Case>();
1633
1634 // A hotplug connect event is enqueued for a display
1635 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1636
1637 // --------------------------------------------------------------------
1638 // Call Expectations
1639
1640 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillOnce(Return(false));
1641
1642 setupCommonCallExpectationsForConnectProcessing<Case>();
1643
1644 // --------------------------------------------------------------------
1645 // Invocation
1646
1647 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1648
1649 // --------------------------------------------------------------------
1650 // Postconditions
1651
1652 verifyPhysicalDisplayIsConnected<Case>();
1653
1654 // --------------------------------------------------------------------
1655 // Cleanup conditions
1656
1657 EXPECT_CALL(*mComposer,
1658 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1659 .WillOnce(Return(Error::NONE));
1660 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1661 }
1662
1663 template <typename Case>
ignoresHotplugConnectCommon()1664 void HandleTransactionLockedTest::ignoresHotplugConnectCommon() {
1665 // --------------------------------------------------------------------
1666 // Preconditions
1667
1668 setupCommonPreconditions<Case>();
1669
1670 // A hotplug connect event is enqueued for a display
1671 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1672
1673 // --------------------------------------------------------------------
1674 // Invocation
1675
1676 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1677
1678 // --------------------------------------------------------------------
1679 // Postconditions
1680
1681 // HWComposer should not have an entry for the display
1682 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1683 }
1684
1685 template <typename Case>
processesHotplugDisconnectCommon()1686 void HandleTransactionLockedTest::processesHotplugDisconnectCommon() {
1687 // --------------------------------------------------------------------
1688 // Preconditions
1689
1690 setupCommonPreconditions<Case>();
1691
1692 // A hotplug disconnect event is enqueued for a display
1693 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1694
1695 // The display is already completely set up.
1696 Case::Display::injectHwcDisplay(this);
1697 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1698 existing.inject();
1699
1700 // --------------------------------------------------------------------
1701 // Call Expectations
1702
1703 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1704 EXPECT_CALL(*mComposer, getDisplayIdentificationData(Case::Display::HWC_DISPLAY_ID, _, _))
1705 .Times(0);
1706
1707 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1708
1709 // --------------------------------------------------------------------
1710 // Invocation
1711
1712 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1713
1714 // --------------------------------------------------------------------
1715 // Postconditions
1716
1717 // HWComposer should not have an entry for the display
1718 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1719
1720 // SF should not have a display token.
1721 const auto displayId = Case::Display::DISPLAY_ID::get();
1722 ASSERT_TRUE(displayId);
1723 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
1724
1725 // The existing token should have been removed
1726 verifyDisplayIsNotConnected(existing.token());
1727 }
1728
TEST_F(HandleTransactionLockedTest,processesHotplugConnectPrimaryDisplay)1729 TEST_F(HandleTransactionLockedTest, processesHotplugConnectPrimaryDisplay) {
1730 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1731 }
1732
TEST_F(HandleTransactionLockedTest,processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected)1733 TEST_F(HandleTransactionLockedTest,
1734 processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected) {
1735 // Inject an external display.
1736 ExternalDisplayVariant::injectHwcDisplay(this);
1737
1738 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1739 }
1740
TEST_F(HandleTransactionLockedTest,processesHotplugConnectExternalDisplay)1741 TEST_F(HandleTransactionLockedTest, processesHotplugConnectExternalDisplay) {
1742 // Inject a primary display.
1743 PrimaryDisplayVariant::injectHwcDisplay(this);
1744
1745 processesHotplugConnectCommon<SimpleExternalDisplayCase>();
1746 }
1747
TEST_F(HandleTransactionLockedTest,ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected)1748 TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected) {
1749 // Inject both a primary and external display.
1750 PrimaryDisplayVariant::injectHwcDisplay(this);
1751 ExternalDisplayVariant::injectHwcDisplay(this);
1752
1753 // TODO: This is an unnecessary call.
1754 EXPECT_CALL(*mComposer,
1755 getDisplayIdentificationData(TertiaryDisplayVariant::HWC_DISPLAY_ID, _, _))
1756 .WillOnce(DoAll(SetArgPointee<1>(TertiaryDisplay::PORT),
1757 SetArgPointee<2>(TertiaryDisplay::GET_IDENTIFICATION_DATA()),
1758 Return(Error::NONE)));
1759
1760 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1761
1762 ignoresHotplugConnectCommon<SimpleTertiaryDisplayCase>();
1763 }
1764
TEST_F(HandleTransactionLockedTest,ignoresHotplugConnectIfExternalForVrComposer)1765 TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfExternalForVrComposer) {
1766 // Inject a primary display.
1767 PrimaryDisplayVariant::injectHwcDisplay(this);
1768
1769 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(true));
1770
1771 ignoresHotplugConnectCommon<SimpleExternalDisplayCase>();
1772 }
1773
TEST_F(HandleTransactionLockedTest,processHotplugDisconnectPrimaryDisplay)1774 TEST_F(HandleTransactionLockedTest, processHotplugDisconnectPrimaryDisplay) {
1775 processesHotplugDisconnectCommon<SimplePrimaryDisplayCase>();
1776 }
1777
TEST_F(HandleTransactionLockedTest,processHotplugDisconnectExternalDisplay)1778 TEST_F(HandleTransactionLockedTest, processHotplugDisconnectExternalDisplay) {
1779 processesHotplugDisconnectCommon<SimpleExternalDisplayCase>();
1780 }
1781
TEST_F(HandleTransactionLockedTest,processesHotplugConnectThenDisconnectPrimary)1782 TEST_F(HandleTransactionLockedTest, processesHotplugConnectThenDisconnectPrimary) {
1783 using Case = SimplePrimaryDisplayCase;
1784
1785 // --------------------------------------------------------------------
1786 // Preconditions
1787
1788 setupCommonPreconditions<Case>();
1789
1790 // A hotplug connect event is enqueued for a display
1791 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1792 // A hotplug disconnect event is also enqueued for the same display
1793 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1794
1795 // --------------------------------------------------------------------
1796 // Call Expectations
1797
1798 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1799
1800 setupCommonCallExpectationsForConnectProcessing<Case>();
1801 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1802
1803 EXPECT_CALL(*mComposer,
1804 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1805 .WillOnce(Return(Error::NONE));
1806 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1807
1808 // --------------------------------------------------------------------
1809 // Invocation
1810
1811 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1812
1813 // --------------------------------------------------------------------
1814 // Postconditions
1815
1816 // HWComposer should not have an entry for the display
1817 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1818
1819 // SF should not have a display token.
1820 const auto displayId = Case::Display::DISPLAY_ID::get();
1821 ASSERT_TRUE(displayId);
1822 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
1823 }
1824
TEST_F(HandleTransactionLockedTest,processesHotplugDisconnectThenConnectPrimary)1825 TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectThenConnectPrimary) {
1826 using Case = SimplePrimaryDisplayCase;
1827
1828 // --------------------------------------------------------------------
1829 // Preconditions
1830
1831 setupCommonPreconditions<Case>();
1832
1833 // The display is already completely set up.
1834 Case::Display::injectHwcDisplay(this);
1835 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1836 existing.inject();
1837
1838 // A hotplug disconnect event is enqueued for a display
1839 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1840 // A hotplug connect event is also enqueued for the same display
1841 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1842
1843 // --------------------------------------------------------------------
1844 // Call Expectations
1845
1846 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1847
1848 setupCommonCallExpectationsForConnectProcessing<Case>();
1849 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1850
1851 // --------------------------------------------------------------------
1852 // Invocation
1853
1854 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1855
1856 // --------------------------------------------------------------------
1857 // Postconditions
1858
1859 // The existing token should have been removed
1860 verifyDisplayIsNotConnected(existing.token());
1861 const auto displayId = Case::Display::DISPLAY_ID::get();
1862 ASSERT_TRUE(displayId);
1863 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1864 EXPECT_NE(existing.token(), mFlinger.mutablePhysicalDisplayTokens()[*displayId]);
1865
1866 // A new display should be connected in its place
1867
1868 verifyPhysicalDisplayIsConnected<Case>();
1869
1870 // --------------------------------------------------------------------
1871 // Cleanup conditions
1872
1873 EXPECT_CALL(*mComposer,
1874 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1875 .WillOnce(Return(Error::NONE));
1876 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1877 }
1878
TEST_F(HandleTransactionLockedTest,processesVirtualDisplayAdded)1879 TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAdded) {
1880 using Case = HwcVirtualDisplayCase;
1881
1882 // --------------------------------------------------------------------
1883 // Preconditions
1884
1885 // The HWC supports at least one virtual display
1886 injectMockComposer(1);
1887
1888 setupCommonPreconditions<Case>();
1889
1890 // A virtual display was added to the current state, and it has a
1891 // surface(producer)
1892 sp<BBinder> displayToken = new BBinder();
1893
1894 DisplayDeviceState state;
1895 state.isSecure = static_cast<bool>(Case::Display::SECURE);
1896
1897 sp<mock::GraphicBufferProducer> surface{new mock::GraphicBufferProducer()};
1898 state.surface = surface;
1899 mFlinger.mutableCurrentState().displays.add(displayToken, state);
1900
1901 // --------------------------------------------------------------------
1902 // Call Expectations
1903
1904 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1905 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1906
1907 EXPECT_CALL(*surface, query(NATIVE_WINDOW_WIDTH, _))
1908 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::WIDTH), Return(NO_ERROR)));
1909 EXPECT_CALL(*surface, query(NATIVE_WINDOW_HEIGHT, _))
1910 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::HEIGHT), Return(NO_ERROR)));
1911 EXPECT_CALL(*surface, query(NATIVE_WINDOW_FORMAT, _))
1912 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT),
1913 Return(NO_ERROR)));
1914 EXPECT_CALL(*surface, query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, _))
1915 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(NO_ERROR)));
1916
1917 EXPECT_CALL(*surface, setAsyncMode(true)).Times(1);
1918
1919 EXPECT_CALL(*mProducer, connect(_, NATIVE_WINDOW_API_EGL, false, _)).Times(1);
1920 EXPECT_CALL(*mProducer, disconnect(_, _)).Times(1);
1921
1922 Case::Display::setupHwcVirtualDisplayCreationCallExpectations(this);
1923 Case::WideColorSupport::setupComposerCallExpectations(this);
1924 Case::HdrSupport::setupComposerCallExpectations(this);
1925 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
1926
1927 // --------------------------------------------------------------------
1928 // Invocation
1929
1930 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1931
1932 // --------------------------------------------------------------------
1933 // Postconditions
1934
1935 // The display device should have been set up in the list of displays.
1936 verifyDisplayIsConnected<Case>(displayToken);
1937
1938 // --------------------------------------------------------------------
1939 // Cleanup conditions
1940
1941 EXPECT_CALL(*mComposer, destroyVirtualDisplay(Case::Display::HWC_DISPLAY_ID))
1942 .WillOnce(Return(Error::NONE));
1943 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1944
1945 // Cleanup
1946 mFlinger.mutableCurrentState().displays.removeItem(displayToken);
1947 mFlinger.mutableDrawingState().displays.removeItem(displayToken);
1948 }
1949
TEST_F(HandleTransactionLockedTest,processesVirtualDisplayAddedWithNoSurface)1950 TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAddedWithNoSurface) {
1951 using Case = HwcVirtualDisplayCase;
1952
1953 // --------------------------------------------------------------------
1954 // Preconditions
1955
1956 // The HWC supports at least one virtual display
1957 injectMockComposer(1);
1958
1959 setupCommonPreconditions<Case>();
1960
1961 // A virtual display was added to the current state, but it does not have a
1962 // surface.
1963 sp<BBinder> displayToken = new BBinder();
1964
1965 DisplayDeviceState state;
1966 state.isSecure = static_cast<bool>(Case::Display::SECURE);
1967
1968 mFlinger.mutableCurrentState().displays.add(displayToken, state);
1969
1970 // --------------------------------------------------------------------
1971 // Call Expectations
1972
1973 // --------------------------------------------------------------------
1974 // Invocation
1975
1976 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1977
1978 // --------------------------------------------------------------------
1979 // Postconditions
1980
1981 // There will not be a display device set up.
1982 EXPECT_FALSE(hasDisplayDevice(displayToken));
1983
1984 // The drawing display state will be set from the current display state.
1985 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1986 const auto& draw = getDrawingDisplayState(displayToken);
1987 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
1988 }
1989
TEST_F(HandleTransactionLockedTest,processesVirtualDisplayRemoval)1990 TEST_F(HandleTransactionLockedTest, processesVirtualDisplayRemoval) {
1991 using Case = HwcVirtualDisplayCase;
1992
1993 // --------------------------------------------------------------------
1994 // Preconditions
1995
1996 // A virtual display is set up but is removed from the current state.
1997 const auto displayId = Case::Display::DISPLAY_ID::get();
1998 ASSERT_TRUE(displayId);
1999 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
2000 Case::Display::injectHwcDisplay(this);
2001 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
2002 existing.inject();
2003 mFlinger.mutableCurrentState().displays.removeItem(existing.token());
2004
2005 // --------------------------------------------------------------------
2006 // Call Expectations
2007
2008 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
2009
2010 // --------------------------------------------------------------------
2011 // Invocation
2012
2013 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2014
2015 // --------------------------------------------------------------------
2016 // Postconditions
2017
2018 // The existing token should have been removed
2019 verifyDisplayIsNotConnected(existing.token());
2020 }
2021
TEST_F(HandleTransactionLockedTest,processesDisplayLayerStackChanges)2022 TEST_F(HandleTransactionLockedTest, processesDisplayLayerStackChanges) {
2023 using Case = NonHwcVirtualDisplayCase;
2024
2025 constexpr uint32_t oldLayerStack = 0u;
2026 constexpr uint32_t newLayerStack = 123u;
2027
2028 // --------------------------------------------------------------------
2029 // Preconditions
2030
2031 // A display is set up
2032 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2033 display.inject();
2034
2035 // There is a change to the layerStack state
2036 display.mutableDrawingDisplayState().layerStack = oldLayerStack;
2037 display.mutableCurrentDisplayState().layerStack = newLayerStack;
2038
2039 // --------------------------------------------------------------------
2040 // Invocation
2041
2042 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2043
2044 // --------------------------------------------------------------------
2045 // Postconditions
2046
2047 EXPECT_EQ(newLayerStack, display.mutableDisplayDevice()->getLayerStack());
2048 }
2049
TEST_F(HandleTransactionLockedTest,processesDisplayTransformChanges)2050 TEST_F(HandleTransactionLockedTest, processesDisplayTransformChanges) {
2051 using Case = NonHwcVirtualDisplayCase;
2052
2053 constexpr int oldTransform = 0;
2054 constexpr int newTransform = 2;
2055
2056 // --------------------------------------------------------------------
2057 // Preconditions
2058
2059 // A display is set up
2060 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2061 display.inject();
2062
2063 // There is a change to the orientation state
2064 display.mutableDrawingDisplayState().orientation = oldTransform;
2065 display.mutableCurrentDisplayState().orientation = newTransform;
2066
2067 // --------------------------------------------------------------------
2068 // Invocation
2069
2070 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2071
2072 // --------------------------------------------------------------------
2073 // Postconditions
2074
2075 EXPECT_EQ(newTransform, display.mutableDisplayDevice()->getOrientation());
2076 }
2077
TEST_F(HandleTransactionLockedTest,processesDisplayViewportChanges)2078 TEST_F(HandleTransactionLockedTest, processesDisplayViewportChanges) {
2079 using Case = NonHwcVirtualDisplayCase;
2080
2081 const Rect oldViewport(0, 0, 0, 0);
2082 const Rect newViewport(0, 0, 123, 456);
2083
2084 // --------------------------------------------------------------------
2085 // Preconditions
2086
2087 // A display is set up
2088 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2089 display.inject();
2090
2091 // There is a change to the viewport state
2092 display.mutableDrawingDisplayState().viewport = oldViewport;
2093 display.mutableCurrentDisplayState().viewport = newViewport;
2094
2095 // --------------------------------------------------------------------
2096 // Invocation
2097
2098 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2099
2100 // --------------------------------------------------------------------
2101 // Postconditions
2102
2103 EXPECT_EQ(newViewport, display.mutableDisplayDevice()->getViewport());
2104 }
2105
TEST_F(HandleTransactionLockedTest,processesDisplayFrameChanges)2106 TEST_F(HandleTransactionLockedTest, processesDisplayFrameChanges) {
2107 using Case = NonHwcVirtualDisplayCase;
2108
2109 const Rect oldFrame(0, 0, 0, 0);
2110 const Rect newFrame(0, 0, 123, 456);
2111
2112 // --------------------------------------------------------------------
2113 // Preconditions
2114
2115 // A display is set up
2116 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2117 display.inject();
2118
2119 // There is a change to the viewport state
2120 display.mutableDrawingDisplayState().frame = oldFrame;
2121 display.mutableCurrentDisplayState().frame = newFrame;
2122
2123 // --------------------------------------------------------------------
2124 // Invocation
2125
2126 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2127
2128 // --------------------------------------------------------------------
2129 // Postconditions
2130
2131 EXPECT_EQ(newFrame, display.mutableDisplayDevice()->getFrame());
2132 }
2133
TEST_F(HandleTransactionLockedTest,processesDisplayWidthChanges)2134 TEST_F(HandleTransactionLockedTest, processesDisplayWidthChanges) {
2135 using Case = NonHwcVirtualDisplayCase;
2136
2137 constexpr int oldWidth = 0;
2138 constexpr int oldHeight = 10;
2139 constexpr int newWidth = 123;
2140
2141 // --------------------------------------------------------------------
2142 // Preconditions
2143
2144 // A display is set up
2145 auto nativeWindow = new mock::NativeWindow();
2146 auto displaySurface = new compositionengine::mock::DisplaySurface();
2147 sp<GraphicBuffer> buf = new GraphicBuffer();
2148 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2149 display.setNativeWindow(nativeWindow);
2150 display.setDisplaySurface(displaySurface);
2151 // Setup injection expections
2152 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
2153 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
2154 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
2155 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
2156 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT)).Times(1);
2157 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_API_CONNECT)).Times(1);
2158 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_SET_USAGE64)).Times(1);
2159 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT)).Times(1);
2160 display.inject();
2161
2162 // There is a change to the viewport state
2163 display.mutableDrawingDisplayState().width = oldWidth;
2164 display.mutableDrawingDisplayState().height = oldHeight;
2165 display.mutableCurrentDisplayState().width = newWidth;
2166 display.mutableCurrentDisplayState().height = oldHeight;
2167
2168 // --------------------------------------------------------------------
2169 // Call Expectations
2170
2171 EXPECT_CALL(*displaySurface, resizeBuffers(newWidth, oldHeight)).Times(1);
2172
2173 // --------------------------------------------------------------------
2174 // Invocation
2175
2176 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2177 }
2178
TEST_F(HandleTransactionLockedTest,processesDisplayHeightChanges)2179 TEST_F(HandleTransactionLockedTest, processesDisplayHeightChanges) {
2180 using Case = NonHwcVirtualDisplayCase;
2181
2182 constexpr int oldWidth = 0;
2183 constexpr int oldHeight = 10;
2184 constexpr int newHeight = 123;
2185
2186 // --------------------------------------------------------------------
2187 // Preconditions
2188
2189 // A display is set up
2190 auto nativeWindow = new mock::NativeWindow();
2191 auto displaySurface = new compositionengine::mock::DisplaySurface();
2192 sp<GraphicBuffer> buf = new GraphicBuffer();
2193 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2194 display.setNativeWindow(nativeWindow);
2195 display.setDisplaySurface(displaySurface);
2196 // Setup injection expections
2197 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
2198 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
2199 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
2200 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
2201 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT)).Times(1);
2202 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_API_CONNECT)).Times(1);
2203 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_SET_USAGE64)).Times(1);
2204 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT)).Times(1);
2205 display.inject();
2206
2207 // There is a change to the viewport state
2208 display.mutableDrawingDisplayState().width = oldWidth;
2209 display.mutableDrawingDisplayState().height = oldHeight;
2210 display.mutableCurrentDisplayState().width = oldWidth;
2211 display.mutableCurrentDisplayState().height = newHeight;
2212
2213 // --------------------------------------------------------------------
2214 // Call Expectations
2215
2216 EXPECT_CALL(*displaySurface, resizeBuffers(oldWidth, newHeight)).Times(1);
2217
2218 // --------------------------------------------------------------------
2219 // Invocation
2220
2221 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2222 }
2223
2224 /* ------------------------------------------------------------------------
2225 * SurfaceFlinger::setDisplayStateLocked
2226 */
2227
TEST_F(DisplayTransactionTest,setDisplayStateLockedDoesNothingWithUnknownDisplay)2228 TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithUnknownDisplay) {
2229 // --------------------------------------------------------------------
2230 // Preconditions
2231
2232 // We have an unknown display token not associated with a known display
2233 sp<BBinder> displayToken = new BBinder();
2234
2235 // The requested display state references the unknown display.
2236 DisplayState state;
2237 state.what = DisplayState::eLayerStackChanged;
2238 state.token = displayToken;
2239 state.layerStack = 456;
2240
2241 // --------------------------------------------------------------------
2242 // Invocation
2243
2244 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2245
2246 // --------------------------------------------------------------------
2247 // Postconditions
2248
2249 // The returned flags are empty
2250 EXPECT_EQ(0u, flags);
2251
2252 // The display token still doesn't match anything known.
2253 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
2254 }
2255
TEST_F(DisplayTransactionTest,setDisplayStateLockedDoesNothingWhenNoChanges)2256 TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWhenNoChanges) {
2257 using Case = SimplePrimaryDisplayCase;
2258
2259 // --------------------------------------------------------------------
2260 // Preconditions
2261
2262 // A display is already set up
2263 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2264 display.inject();
2265
2266 // No changes are made to the display
2267 DisplayState state;
2268 state.what = 0;
2269 state.token = display.token();
2270
2271 // --------------------------------------------------------------------
2272 // Invocation
2273
2274 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2275
2276 // --------------------------------------------------------------------
2277 // Postconditions
2278
2279 // The returned flags are empty
2280 EXPECT_EQ(0u, flags);
2281 }
2282
TEST_F(DisplayTransactionTest,setDisplayStateLockedDoesNothingIfSurfaceDidNotChange)2283 TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSurfaceDidNotChange) {
2284 using Case = SimplePrimaryDisplayCase;
2285
2286 // --------------------------------------------------------------------
2287 // Preconditions
2288
2289 // A display is already set up
2290 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2291 display.inject();
2292
2293 // There is a surface that can be set.
2294 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2295
2296 // The current display state has the surface set
2297 display.mutableCurrentDisplayState().surface = surface;
2298
2299 // The incoming request sets the same surface
2300 DisplayState state;
2301 state.what = DisplayState::eSurfaceChanged;
2302 state.token = display.token();
2303 state.surface = surface;
2304
2305 // --------------------------------------------------------------------
2306 // Invocation
2307
2308 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2309
2310 // --------------------------------------------------------------------
2311 // Postconditions
2312
2313 // The returned flags are empty
2314 EXPECT_EQ(0u, flags);
2315
2316 // The current display state is unchanged.
2317 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2318 }
2319
TEST_F(DisplayTransactionTest,setDisplayStateLockedRequestsUpdateIfSurfaceChanged)2320 TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfSurfaceChanged) {
2321 using Case = SimplePrimaryDisplayCase;
2322
2323 // --------------------------------------------------------------------
2324 // Preconditions
2325
2326 // A display is already set up
2327 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2328 display.inject();
2329
2330 // There is a surface that can be set.
2331 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2332
2333 // The current display state does not have a surface
2334 display.mutableCurrentDisplayState().surface = nullptr;
2335
2336 // The incoming request sets a surface
2337 DisplayState state;
2338 state.what = DisplayState::eSurfaceChanged;
2339 state.token = display.token();
2340 state.surface = surface;
2341
2342 // --------------------------------------------------------------------
2343 // Invocation
2344
2345 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2346
2347 // --------------------------------------------------------------------
2348 // Postconditions
2349
2350 // The returned flags indicate a transaction is needed
2351 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2352
2353 // The current display layer stack state is set to the new value
2354 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2355 }
2356
TEST_F(DisplayTransactionTest,setDisplayStateLockedDoesNothingIfLayerStackDidNotChange)2357 TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfLayerStackDidNotChange) {
2358 using Case = SimplePrimaryDisplayCase;
2359
2360 // --------------------------------------------------------------------
2361 // Preconditions
2362
2363 // A display is already set up
2364 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2365 display.inject();
2366
2367 // The display has a layer stack set
2368 display.mutableCurrentDisplayState().layerStack = 456u;
2369
2370 // The incoming request sets the same layer stack
2371 DisplayState state;
2372 state.what = DisplayState::eLayerStackChanged;
2373 state.token = display.token();
2374 state.layerStack = 456u;
2375
2376 // --------------------------------------------------------------------
2377 // Invocation
2378
2379 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2380
2381 // --------------------------------------------------------------------
2382 // Postconditions
2383
2384 // The returned flags are empty
2385 EXPECT_EQ(0u, flags);
2386
2387 // The current display state is unchanged
2388 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2389 }
2390
TEST_F(DisplayTransactionTest,setDisplayStateLockedRequestsUpdateIfLayerStackChanged)2391 TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfLayerStackChanged) {
2392 using Case = SimplePrimaryDisplayCase;
2393
2394 // --------------------------------------------------------------------
2395 // Preconditions
2396
2397 // A display is set up
2398 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2399 display.inject();
2400
2401 // The display has a layer stack set
2402 display.mutableCurrentDisplayState().layerStack = 654u;
2403
2404 // The incoming request sets a different layer stack
2405 DisplayState state;
2406 state.what = DisplayState::eLayerStackChanged;
2407 state.token = display.token();
2408 state.layerStack = 456u;
2409
2410 // --------------------------------------------------------------------
2411 // Invocation
2412
2413 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2414
2415 // --------------------------------------------------------------------
2416 // Postconditions
2417
2418 // The returned flags indicate a transaction is needed
2419 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2420
2421 // The desired display state has been set to the new value.
2422 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2423 }
2424
TEST_F(DisplayTransactionTest,setDisplayStateLockedDoesNothingIfProjectionDidNotChange)2425 TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfProjectionDidNotChange) {
2426 using Case = SimplePrimaryDisplayCase;
2427 constexpr int initialOrientation = 180;
2428 const Rect initialFrame = {1, 2, 3, 4};
2429 const Rect initialViewport = {5, 6, 7, 8};
2430
2431 // --------------------------------------------------------------------
2432 // Preconditions
2433
2434 // A display is set up
2435 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2436 display.inject();
2437
2438 // The current display state projection state is all set
2439 display.mutableCurrentDisplayState().orientation = initialOrientation;
2440 display.mutableCurrentDisplayState().frame = initialFrame;
2441 display.mutableCurrentDisplayState().viewport = initialViewport;
2442
2443 // The incoming request sets the same projection state
2444 DisplayState state;
2445 state.what = DisplayState::eDisplayProjectionChanged;
2446 state.token = display.token();
2447 state.orientation = initialOrientation;
2448 state.frame = initialFrame;
2449 state.viewport = initialViewport;
2450
2451 // --------------------------------------------------------------------
2452 // Invocation
2453
2454 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2455
2456 // --------------------------------------------------------------------
2457 // Postconditions
2458
2459 // The returned flags are empty
2460 EXPECT_EQ(0u, flags);
2461
2462 // The current display state is unchanged
2463 EXPECT_EQ(initialOrientation, display.getCurrentDisplayState().orientation);
2464
2465 EXPECT_EQ(initialFrame, display.getCurrentDisplayState().frame);
2466 EXPECT_EQ(initialViewport, display.getCurrentDisplayState().viewport);
2467 }
2468
TEST_F(DisplayTransactionTest,setDisplayStateLockedRequestsUpdateIfOrientationChanged)2469 TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfOrientationChanged) {
2470 using Case = SimplePrimaryDisplayCase;
2471 constexpr int initialOrientation = 90;
2472 constexpr int desiredOrientation = 180;
2473
2474 // --------------------------------------------------------------------
2475 // Preconditions
2476
2477 // A display is set up
2478 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2479 display.inject();
2480
2481 // The current display state has an orientation set
2482 display.mutableCurrentDisplayState().orientation = initialOrientation;
2483
2484 // The incoming request sets a different orientation
2485 DisplayState state;
2486 state.what = DisplayState::eDisplayProjectionChanged;
2487 state.token = display.token();
2488 state.orientation = desiredOrientation;
2489
2490 // --------------------------------------------------------------------
2491 // Invocation
2492
2493 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2494
2495 // --------------------------------------------------------------------
2496 // Postconditions
2497
2498 // The returned flags indicate a transaction is needed
2499 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2500
2501 // The current display state has the new value.
2502 EXPECT_EQ(desiredOrientation, display.getCurrentDisplayState().orientation);
2503 }
2504
TEST_F(DisplayTransactionTest,setDisplayStateLockedRequestsUpdateIfFrameChanged)2505 TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfFrameChanged) {
2506 using Case = SimplePrimaryDisplayCase;
2507 const Rect initialFrame = {0, 0, 0, 0};
2508 const Rect desiredFrame = {5, 6, 7, 8};
2509
2510 // --------------------------------------------------------------------
2511 // Preconditions
2512
2513 // A display is set up
2514 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2515 display.inject();
2516
2517 // The current display state does not have a frame
2518 display.mutableCurrentDisplayState().frame = initialFrame;
2519
2520 // The incoming request sets a frame
2521 DisplayState state;
2522 state.what = DisplayState::eDisplayProjectionChanged;
2523 state.token = display.token();
2524 state.frame = desiredFrame;
2525
2526 // --------------------------------------------------------------------
2527 // Invocation
2528
2529 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2530
2531 // --------------------------------------------------------------------
2532 // Postconditions
2533
2534 // The returned flags indicate a transaction is needed
2535 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2536
2537 // The current display state has the new value.
2538 EXPECT_EQ(desiredFrame, display.getCurrentDisplayState().frame);
2539 }
2540
TEST_F(DisplayTransactionTest,setDisplayStateLockedRequestsUpdateIfViewportChanged)2541 TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfViewportChanged) {
2542 using Case = SimplePrimaryDisplayCase;
2543 const Rect initialViewport = {0, 0, 0, 0};
2544 const Rect desiredViewport = {5, 6, 7, 8};
2545
2546 // --------------------------------------------------------------------
2547 // Preconditions
2548
2549 // A display is set up
2550 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2551 display.inject();
2552
2553 // The current display state does not have a viewport
2554 display.mutableCurrentDisplayState().viewport = initialViewport;
2555
2556 // The incoming request sets a viewport
2557 DisplayState state;
2558 state.what = DisplayState::eDisplayProjectionChanged;
2559 state.token = display.token();
2560 state.viewport = desiredViewport;
2561
2562 // --------------------------------------------------------------------
2563 // Invocation
2564
2565 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2566
2567 // --------------------------------------------------------------------
2568 // Postconditions
2569
2570 // The returned flags indicate a transaction is needed
2571 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2572
2573 // The current display state has the new value.
2574 EXPECT_EQ(desiredViewport, display.getCurrentDisplayState().viewport);
2575 }
2576
TEST_F(DisplayTransactionTest,setDisplayStateLockedDoesNothingIfSizeDidNotChange)2577 TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSizeDidNotChange) {
2578 using Case = SimplePrimaryDisplayCase;
2579 constexpr uint32_t initialWidth = 1024;
2580 constexpr uint32_t initialHeight = 768;
2581
2582 // --------------------------------------------------------------------
2583 // Preconditions
2584
2585 // A display is set up
2586 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2587 display.inject();
2588
2589 // The current display state has a size set
2590 display.mutableCurrentDisplayState().width = initialWidth;
2591 display.mutableCurrentDisplayState().height = initialHeight;
2592
2593 // The incoming request sets the same display size
2594 DisplayState state;
2595 state.what = DisplayState::eDisplaySizeChanged;
2596 state.token = display.token();
2597 state.width = initialWidth;
2598 state.height = initialHeight;
2599
2600 // --------------------------------------------------------------------
2601 // Invocation
2602
2603 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2604
2605 // --------------------------------------------------------------------
2606 // Postconditions
2607
2608 // The returned flags are empty
2609 EXPECT_EQ(0u, flags);
2610
2611 // The current display state is unchanged
2612 EXPECT_EQ(initialWidth, display.getCurrentDisplayState().width);
2613 EXPECT_EQ(initialHeight, display.getCurrentDisplayState().height);
2614 }
2615
TEST_F(DisplayTransactionTest,setDisplayStateLockedRequestsUpdateIfWidthChanged)2616 TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfWidthChanged) {
2617 using Case = SimplePrimaryDisplayCase;
2618 constexpr uint32_t initialWidth = 0;
2619 constexpr uint32_t desiredWidth = 1024;
2620
2621 // --------------------------------------------------------------------
2622 // Preconditions
2623
2624 // A display is set up
2625 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2626 display.inject();
2627
2628 // The display does not yet have a width
2629 display.mutableCurrentDisplayState().width = initialWidth;
2630
2631 // The incoming request sets a display width
2632 DisplayState state;
2633 state.what = DisplayState::eDisplaySizeChanged;
2634 state.token = display.token();
2635 state.width = desiredWidth;
2636
2637 // --------------------------------------------------------------------
2638 // Invocation
2639
2640 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2641
2642 // --------------------------------------------------------------------
2643 // Postconditions
2644
2645 // The returned flags indicate a transaction is needed
2646 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2647
2648 // The current display state has the new value.
2649 EXPECT_EQ(desiredWidth, display.getCurrentDisplayState().width);
2650 }
2651
TEST_F(DisplayTransactionTest,setDisplayStateLockedRequestsUpdateIfHeightChanged)2652 TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfHeightChanged) {
2653 using Case = SimplePrimaryDisplayCase;
2654 constexpr uint32_t initialHeight = 0;
2655 constexpr uint32_t desiredHeight = 768;
2656
2657 // --------------------------------------------------------------------
2658 // Preconditions
2659
2660 // A display is set up
2661 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2662 display.inject();
2663
2664 // The display does not yet have a height
2665 display.mutableCurrentDisplayState().height = initialHeight;
2666
2667 // The incoming request sets a display height
2668 DisplayState state;
2669 state.what = DisplayState::eDisplaySizeChanged;
2670 state.token = display.token();
2671 state.height = desiredHeight;
2672
2673 // --------------------------------------------------------------------
2674 // Invocation
2675
2676 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2677
2678 // --------------------------------------------------------------------
2679 // Postconditions
2680
2681 // The returned flags indicate a transaction is needed
2682 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2683
2684 // The current display state has the new value.
2685 EXPECT_EQ(desiredHeight, display.getCurrentDisplayState().height);
2686 }
2687
2688 /* ------------------------------------------------------------------------
2689 * SurfaceFlinger::onInitializeDisplays
2690 */
2691
TEST_F(DisplayTransactionTest,onInitializeDisplaysSetsUpPrimaryDisplay)2692 TEST_F(DisplayTransactionTest, onInitializeDisplaysSetsUpPrimaryDisplay) {
2693 using Case = SimplePrimaryDisplayCase;
2694
2695 // --------------------------------------------------------------------
2696 // Preconditions
2697
2698 // A primary display is set up
2699 Case::Display::injectHwcDisplay(this);
2700 auto primaryDisplay = Case::Display::makeFakeExistingDisplayInjector(this);
2701 primaryDisplay.inject();
2702
2703 // --------------------------------------------------------------------
2704 // Call Expectations
2705
2706 // We expect the surface interceptor to possibly be used, but we treat it as
2707 // disabled since it is called as a side effect rather than directly by this
2708 // function.
2709 EXPECT_CALL(*mSurfaceInterceptor, isEnabled()).WillOnce(Return(false));
2710
2711 // We expect a call to get the active display config.
2712 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
2713
2714 // We expect invalidate() to be invoked once to trigger display transaction
2715 // processing.
2716 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
2717
2718 EXPECT_CALL(*mPrimaryDispSync, expectedPresentTime()).WillRepeatedly(Return(0));
2719
2720 // --------------------------------------------------------------------
2721 // Invocation
2722
2723 mFlinger.onInitializeDisplays();
2724
2725 // --------------------------------------------------------------------
2726 // Postconditions
2727
2728 // The primary display should have a current state
2729 ASSERT_TRUE(hasCurrentDisplayState(primaryDisplay.token()));
2730 const auto& primaryDisplayState = getCurrentDisplayState(primaryDisplay.token());
2731 // The layer stack state should be set to zero
2732 EXPECT_EQ(0u, primaryDisplayState.layerStack);
2733 // The orientation state should be set to zero
2734 EXPECT_EQ(0, primaryDisplayState.orientation);
2735
2736 // The frame state should be set to INVALID
2737 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.frame);
2738
2739 // The viewport state should be set to INVALID
2740 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.viewport);
2741
2742 // The width and height should both be zero
2743 EXPECT_EQ(0u, primaryDisplayState.width);
2744 EXPECT_EQ(0u, primaryDisplayState.height);
2745
2746 // The display should be set to HWC_POWER_MODE_NORMAL
2747 ASSERT_TRUE(hasDisplayDevice(primaryDisplay.token()));
2748 auto displayDevice = primaryDisplay.mutableDisplayDevice();
2749 EXPECT_EQ(HWC_POWER_MODE_NORMAL, displayDevice->getPowerMode());
2750
2751 // The display refresh period should be set in the frame tracker.
2752 FrameStats stats;
2753 mFlinger.getAnimFrameTracker().getStats(&stats);
2754 EXPECT_EQ(DEFAULT_REFRESH_RATE, stats.refreshPeriodNano);
2755
2756 // The display transaction needed flag should be set.
2757 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
2758
2759 // The compositor timing should be set to default values
2760 const auto& compositorTiming = mFlinger.getCompositorTiming();
2761 EXPECT_EQ(-DEFAULT_REFRESH_RATE, compositorTiming.deadline);
2762 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.interval);
2763 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.presentLatency);
2764 }
2765
2766 /* ------------------------------------------------------------------------
2767 * SurfaceFlinger::setPowerModeInternal
2768 */
2769
2770 // Used when we simulate a display that supports doze.
2771 template <typename Display>
2772 struct DozeIsSupportedVariant {
2773 static constexpr bool DOZE_SUPPORTED = true;
2774 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2775 IComposerClient::PowerMode::DOZE;
2776 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2777 IComposerClient::PowerMode::DOZE_SUSPEND;
2778
setupComposerCallExpectationsandroid::__anon968546880111::DozeIsSupportedVariant2779 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
2780 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(Display::HWC_DISPLAY_ID, _))
2781 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hwc2::DisplayCapability>(
2782 {Hwc2::DisplayCapability::DOZE})),
2783 Return(Error::NONE)));
2784 }
2785 };
2786
2787 template <typename Display>
2788 // Used when we simulate a display that does not support doze.
2789 struct DozeNotSupportedVariant {
2790 static constexpr bool DOZE_SUPPORTED = false;
2791 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2792 IComposerClient::PowerMode::ON;
2793 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2794 IComposerClient::PowerMode::ON;
2795
setupComposerCallExpectationsandroid::__anon968546880111::DozeNotSupportedVariant2796 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
2797 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(Display::HWC_DISPLAY_ID, _))
2798 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hwc2::DisplayCapability>({})),
2799 Return(Error::NONE)));
2800 }
2801 };
2802
2803 struct EventThreadBaseSupportedVariant {
setupEventAndEventControlThreadNoCallExpectationsandroid::__anon968546880111::EventThreadBaseSupportedVariant2804 static void setupEventAndEventControlThreadNoCallExpectations(DisplayTransactionTest* test) {
2805 // The event control thread should not be notified.
2806 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(_)).Times(0);
2807
2808 // The event thread should not be notified.
2809 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(0);
2810 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(0);
2811 }
2812 };
2813
2814 struct EventThreadNotSupportedVariant : public EventThreadBaseSupportedVariant {
setupAcquireAndEnableVsyncCallExpectationsandroid::__anon968546880111::EventThreadNotSupportedVariant2815 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2816 // These calls are only expected for the primary display.
2817
2818 // Instead expect no calls.
2819 setupEventAndEventControlThreadNoCallExpectations(test);
2820 }
2821
setupReleaseAndDisableVsyncCallExpectationsandroid::__anon968546880111::EventThreadNotSupportedVariant2822 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2823 // These calls are only expected for the primary display.
2824
2825 // Instead expect no calls.
2826 setupEventAndEventControlThreadNoCallExpectations(test);
2827 }
2828 };
2829
2830 struct EventThreadIsSupportedVariant : public EventThreadBaseSupportedVariant {
setupAcquireAndEnableVsyncCallExpectationsandroid::__anon968546880111::EventThreadIsSupportedVariant2831 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2832 // The event control thread should be notified to enable vsyncs
2833 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(true)).Times(1);
2834
2835 // The event thread should be notified that the screen was acquired.
2836 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(1);
2837 }
2838
setupReleaseAndDisableVsyncCallExpectationsandroid::__anon968546880111::EventThreadIsSupportedVariant2839 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2840 // There should be a call to setVsyncEnabled(false)
2841 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(false)).Times(1);
2842
2843 // The event thread should not be notified that the screen was released.
2844 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(1);
2845 }
2846 };
2847
2848 struct DispSyncIsSupportedVariant {
setupBeginResyncCallExpectationsandroid::__anon968546880111::DispSyncIsSupportedVariant2849 static void setupBeginResyncCallExpectations(DisplayTransactionTest* test) {
2850 EXPECT_CALL(*test->mPrimaryDispSync, setPeriod(DEFAULT_REFRESH_RATE)).Times(1);
2851 EXPECT_CALL(*test->mPrimaryDispSync, beginResync()).Times(1);
2852 }
2853
setupEndResyncCallExpectationsandroid::__anon968546880111::DispSyncIsSupportedVariant2854 static void setupEndResyncCallExpectations(DisplayTransactionTest* test) {
2855 EXPECT_CALL(*test->mPrimaryDispSync, endResync()).Times(1);
2856 }
2857 };
2858
2859 struct DispSyncNotSupportedVariant {
setupBeginResyncCallExpectationsandroid::__anon968546880111::DispSyncNotSupportedVariant2860 static void setupBeginResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2861
setupEndResyncCallExpectationsandroid::__anon968546880111::DispSyncNotSupportedVariant2862 static void setupEndResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2863 };
2864
2865 // --------------------------------------------------------------------
2866 // Note:
2867 //
2868 // There are a large number of transitions we could test, however we only test a
2869 // selected subset which provides complete test coverage of the implementation.
2870 // --------------------------------------------------------------------
2871
2872 template <int initialPowerMode, int targetPowerMode>
2873 struct TransitionVariantCommon {
2874 static constexpr auto INITIAL_POWER_MODE = initialPowerMode;
2875 static constexpr auto TARGET_POWER_MODE = targetPowerMode;
2876
verifyPostconditionsandroid::__anon968546880111::TransitionVariantCommon2877 static void verifyPostconditions(DisplayTransactionTest*) {}
2878 };
2879
2880 struct TransitionOffToOnVariant
2881 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_NORMAL> {
2882 template <typename Case>
setupCallExpectationsandroid::__anon968546880111::TransitionOffToOnVariant2883 static void setupCallExpectations(DisplayTransactionTest* test) {
2884 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2885 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
2886 Case::DispSync::setupBeginResyncCallExpectations(test);
2887 Case::setupRepaintEverythingCallExpectations(test);
2888 }
2889
verifyPostconditionsandroid::__anon968546880111::TransitionOffToOnVariant2890 static void verifyPostconditions(DisplayTransactionTest* test) {
2891 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2892 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2893 }
2894 };
2895
2896 struct TransitionOffToDozeSuspendVariant
2897 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_DOZE_SUSPEND> {
2898 template <typename Case>
setupCallExpectationsandroid::__anon968546880111::TransitionOffToDozeSuspendVariant2899 static void setupCallExpectations(DisplayTransactionTest* test) {
2900 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2901 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2902 Case::setupRepaintEverythingCallExpectations(test);
2903 }
2904
verifyPostconditionsandroid::__anon968546880111::TransitionOffToDozeSuspendVariant2905 static void verifyPostconditions(DisplayTransactionTest* test) {
2906 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2907 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2908 }
2909 };
2910
2911 struct TransitionOnToOffVariant
2912 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_OFF> {
2913 template <typename Case>
setupCallExpectationsandroid::__anon968546880111::TransitionOnToOffVariant2914 static void setupCallExpectations(DisplayTransactionTest* test) {
2915 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
2916 Case::DispSync::setupEndResyncCallExpectations(test);
2917 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2918 }
2919
verifyPostconditionsandroid::__anon968546880111::TransitionOnToOffVariant2920 static void verifyPostconditions(DisplayTransactionTest* test) {
2921 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2922 }
2923 };
2924
2925 struct TransitionDozeSuspendToOffVariant
2926 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_OFF> {
2927 template <typename Case>
setupCallExpectationsandroid::__anon968546880111::TransitionDozeSuspendToOffVariant2928 static void setupCallExpectations(DisplayTransactionTest* test) {
2929 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2930 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2931 }
2932
verifyPostconditionsandroid::__anon968546880111::TransitionDozeSuspendToOffVariant2933 static void verifyPostconditions(DisplayTransactionTest* test) {
2934 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2935 }
2936 };
2937
2938 struct TransitionOnToDozeVariant
2939 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE> {
2940 template <typename Case>
setupCallExpectationsandroid::__anon968546880111::TransitionOnToDozeVariant2941 static void setupCallExpectations(DisplayTransactionTest* test) {
2942 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2943 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2944 }
2945 };
2946
2947 struct TransitionDozeSuspendToDozeVariant
2948 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_DOZE> {
2949 template <typename Case>
setupCallExpectationsandroid::__anon968546880111::TransitionDozeSuspendToDozeVariant2950 static void setupCallExpectations(DisplayTransactionTest* test) {
2951 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
2952 Case::DispSync::setupBeginResyncCallExpectations(test);
2953 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2954 }
2955 };
2956
2957 struct TransitionDozeToOnVariant
2958 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE, HWC_POWER_MODE_NORMAL> {
2959 template <typename Case>
setupCallExpectationsandroid::__anon968546880111::TransitionDozeToOnVariant2960 static void setupCallExpectations(DisplayTransactionTest* test) {
2961 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2962 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2963 }
2964 };
2965
2966 struct TransitionDozeSuspendToOnVariant
2967 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_NORMAL> {
2968 template <typename Case>
setupCallExpectationsandroid::__anon968546880111::TransitionDozeSuspendToOnVariant2969 static void setupCallExpectations(DisplayTransactionTest* test) {
2970 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
2971 Case::DispSync::setupBeginResyncCallExpectations(test);
2972 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2973 }
2974 };
2975
2976 struct TransitionOnToDozeSuspendVariant
2977 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE_SUSPEND> {
2978 template <typename Case>
setupCallExpectationsandroid::__anon968546880111::TransitionOnToDozeSuspendVariant2979 static void setupCallExpectations(DisplayTransactionTest* test) {
2980 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
2981 Case::DispSync::setupEndResyncCallExpectations(test);
2982 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2983 }
2984 };
2985
2986 struct TransitionOnToUnknownVariant
2987 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_LEET> {
2988 template <typename Case>
setupCallExpectationsandroid::__anon968546880111::TransitionOnToUnknownVariant2989 static void setupCallExpectations(DisplayTransactionTest* test) {
2990 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2991 Case::setupNoComposerPowerModeCallExpectations(test);
2992 }
2993 };
2994
2995 // --------------------------------------------------------------------
2996 // Note:
2997 //
2998 // Rather than testing the cartesian product of of
2999 // DozeIsSupported/DozeNotSupported with all other options, we use one for one
3000 // display type, and the other for another display type.
3001 // --------------------------------------------------------------------
3002
3003 template <typename DisplayVariant, typename DozeVariant, typename EventThreadVariant,
3004 typename DispSyncVariant, typename TransitionVariant>
3005 struct DisplayPowerCase {
3006 using Display = DisplayVariant;
3007 using Doze = DozeVariant;
3008 using EventThread = EventThreadVariant;
3009 using DispSync = DispSyncVariant;
3010 using Transition = TransitionVariant;
3011
injectDisplayWithInitialPowerModeandroid::__anon968546880111::DisplayPowerCase3012 static auto injectDisplayWithInitialPowerMode(DisplayTransactionTest* test, int mode) {
3013 Display::injectHwcDisplayWithNoDefaultCapabilities(test);
3014 auto display = Display::makeFakeExistingDisplayInjector(test);
3015 display.inject();
3016 display.mutableDisplayDevice()->setPowerMode(mode);
3017 return display;
3018 }
3019
setInitialPrimaryHWVsyncEnabledandroid::__anon968546880111::DisplayPowerCase3020 static void setInitialPrimaryHWVsyncEnabled(DisplayTransactionTest* test, bool enabled) {
3021 test->mScheduler->mutablePrimaryHWVsyncEnabled() = enabled;
3022 }
3023
setupRepaintEverythingCallExpectationsandroid::__anon968546880111::DisplayPowerCase3024 static void setupRepaintEverythingCallExpectations(DisplayTransactionTest* test) {
3025 EXPECT_CALL(*test->mMessageQueue, invalidate()).Times(1);
3026 }
3027
setupSurfaceInterceptorCallExpectationsandroid::__anon968546880111::DisplayPowerCase3028 static void setupSurfaceInterceptorCallExpectations(DisplayTransactionTest* test, int mode) {
3029 EXPECT_CALL(*test->mSurfaceInterceptor, isEnabled()).WillOnce(Return(true));
3030 EXPECT_CALL(*test->mSurfaceInterceptor, savePowerModeUpdate(_, mode)).Times(1);
3031 }
3032
setupComposerCallExpectationsandroid::__anon968546880111::DisplayPowerCase3033 static void setupComposerCallExpectations(DisplayTransactionTest* test,
3034 IComposerClient::PowerMode mode) {
3035 // Any calls to get the active config will return a default value.
3036 EXPECT_CALL(*test->mComposer, getActiveConfig(Display::HWC_DISPLAY_ID, _))
3037 .WillRepeatedly(DoAll(SetArgPointee<1>(Display::HWC_ACTIVE_CONFIG_ID),
3038 Return(Error::NONE)));
3039
3040 // Any calls to get whether the display supports dozing will return the value set by the
3041 // policy variant.
3042 EXPECT_CALL(*test->mComposer, getDozeSupport(Display::HWC_DISPLAY_ID, _))
3043 .WillRepeatedly(DoAll(SetArgPointee<1>(Doze::DOZE_SUPPORTED), Return(Error::NONE)));
3044
3045 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, mode)).Times(1);
3046 }
3047
setupNoComposerPowerModeCallExpectationsandroid::__anon968546880111::DisplayPowerCase3048 static void setupNoComposerPowerModeCallExpectations(DisplayTransactionTest* test) {
3049 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, _)).Times(0);
3050 }
3051 };
3052
3053 // A sample configuration for the primary display.
3054 // In addition to having event thread support, we emulate doze support.
3055 template <typename TransitionVariant>
3056 using PrimaryDisplayPowerCase =
3057 DisplayPowerCase<PrimaryDisplayVariant, DozeIsSupportedVariant<PrimaryDisplayVariant>,
3058 EventThreadIsSupportedVariant, DispSyncIsSupportedVariant,
3059 TransitionVariant>;
3060
3061 // A sample configuration for the external display.
3062 // In addition to not having event thread support, we emulate not having doze
3063 // support.
3064 template <typename TransitionVariant>
3065 using ExternalDisplayPowerCase =
3066 DisplayPowerCase<ExternalDisplayVariant, DozeNotSupportedVariant<ExternalDisplayVariant>,
3067 EventThreadNotSupportedVariant, DispSyncNotSupportedVariant,
3068 TransitionVariant>;
3069
3070 class SetPowerModeInternalTest : public DisplayTransactionTest {
3071 public:
3072 template <typename Case>
3073 void transitionDisplayCommon();
3074 };
3075
3076 template <int PowerMode>
3077 struct PowerModeInitialVSyncEnabled : public std::false_type {};
3078
3079 template <>
3080 struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_NORMAL> : public std::true_type {};
3081
3082 template <>
3083 struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_DOZE> : public std::true_type {};
3084
3085 template <typename Case>
transitionDisplayCommon()3086 void SetPowerModeInternalTest::transitionDisplayCommon() {
3087 // --------------------------------------------------------------------
3088 // Preconditions
3089
3090 Case::Doze::setupComposerCallExpectations(this);
3091 auto display =
3092 Case::injectDisplayWithInitialPowerMode(this, Case::Transition::INITIAL_POWER_MODE);
3093 Case::setInitialPrimaryHWVsyncEnabled(this,
3094 PowerModeInitialVSyncEnabled<
3095 Case::Transition::INITIAL_POWER_MODE>::value);
3096
3097 // --------------------------------------------------------------------
3098 // Call Expectations
3099
3100 Case::setupSurfaceInterceptorCallExpectations(this, Case::Transition::TARGET_POWER_MODE);
3101 Case::Transition::template setupCallExpectations<Case>(this);
3102
3103 // --------------------------------------------------------------------
3104 // Invocation
3105
3106 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(),
3107 Case::Transition::TARGET_POWER_MODE);
3108
3109 // --------------------------------------------------------------------
3110 // Postconditions
3111
3112 Case::Transition::verifyPostconditions(this);
3113 }
3114
TEST_F(SetPowerModeInternalTest,setPowerModeInternalDoesNothingIfNoChange)3115 TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfNoChange) {
3116 using Case = SimplePrimaryDisplayCase;
3117
3118 // --------------------------------------------------------------------
3119 // Preconditions
3120
3121 // A primary display device is set up
3122 Case::Display::injectHwcDisplay(this);
3123 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
3124 display.inject();
3125
3126 // The display is already set to HWC_POWER_MODE_NORMAL
3127 display.mutableDisplayDevice()->setPowerMode(HWC_POWER_MODE_NORMAL);
3128
3129 // --------------------------------------------------------------------
3130 // Invocation
3131
3132 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_NORMAL);
3133
3134 // --------------------------------------------------------------------
3135 // Postconditions
3136
3137 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
3138 }
3139
TEST_F(SetPowerModeInternalTest,setPowerModeInternalDoesNothingIfVirtualDisplay)3140 TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfVirtualDisplay) {
3141 using Case = HwcVirtualDisplayCase;
3142
3143 // --------------------------------------------------------------------
3144 // Preconditions
3145
3146 // Insert display data so that the HWC thinks it created the virtual display.
3147 const auto displayId = Case::Display::DISPLAY_ID::get();
3148 ASSERT_TRUE(displayId);
3149 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
3150
3151 // A virtual display device is set up
3152 Case::Display::injectHwcDisplay(this);
3153 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
3154 display.inject();
3155
3156 // The display is set to HWC_POWER_MODE_NORMAL
3157 getDisplayDevice(display.token())->setPowerMode(HWC_POWER_MODE_NORMAL);
3158
3159 // --------------------------------------------------------------------
3160 // Invocation
3161
3162 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_OFF);
3163
3164 // --------------------------------------------------------------------
3165 // Postconditions
3166
3167 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
3168 }
3169
TEST_F(SetPowerModeInternalTest,transitionsDisplayFromOffToOnPrimaryDisplay)3170 TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnPrimaryDisplay) {
3171 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToOnVariant>>();
3172 }
3173
TEST_F(SetPowerModeInternalTest,transitionsDisplayFromOffToDozeSuspendPrimaryDisplay)3174 TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendPrimaryDisplay) {
3175 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
3176 }
3177
TEST_F(SetPowerModeInternalTest,transitionsDisplayFromOnToOffPrimaryDisplay)3178 TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffPrimaryDisplay) {
3179 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToOffVariant>>();
3180 }
3181
TEST_F(SetPowerModeInternalTest,transitionsDisplayFromDozeSuspendToOffPrimaryDisplay)3182 TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffPrimaryDisplay) {
3183 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
3184 }
3185
TEST_F(SetPowerModeInternalTest,transitionsDisplayFromOnToDozePrimaryDisplay)3186 TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozePrimaryDisplay) {
3187 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeVariant>>();
3188 }
3189
TEST_F(SetPowerModeInternalTest,transitionsDisplayFromDozeSuspendToDozePrimaryDisplay)3190 TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozePrimaryDisplay) {
3191 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
3192 }
3193
TEST_F(SetPowerModeInternalTest,transitionsDisplayFromDozeToOnPrimaryDisplay)3194 TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnPrimaryDisplay) {
3195 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeToOnVariant>>();
3196 }
3197
TEST_F(SetPowerModeInternalTest,transitionsDisplayFromDozeSuspendToOnPrimaryDisplay)3198 TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnPrimaryDisplay) {
3199 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
3200 }
3201
TEST_F(SetPowerModeInternalTest,transitionsDisplayFromOnToDozeSuspendPrimaryDisplay)3202 TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendPrimaryDisplay) {
3203 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
3204 }
3205
TEST_F(SetPowerModeInternalTest,transitionsDisplayFromOnToUnknownPrimaryDisplay)3206 TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownPrimaryDisplay) {
3207 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToUnknownVariant>>();
3208 }
3209
TEST_F(SetPowerModeInternalTest,transitionsDisplayFromOffToOnExternalDisplay)3210 TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnExternalDisplay) {
3211 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToOnVariant>>();
3212 }
3213
TEST_F(SetPowerModeInternalTest,transitionsDisplayFromOffToDozeSuspendExternalDisplay)3214 TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendExternalDisplay) {
3215 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
3216 }
3217
TEST_F(SetPowerModeInternalTest,transitionsDisplayFromOnToOffExternalDisplay)3218 TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffExternalDisplay) {
3219 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToOffVariant>>();
3220 }
3221
TEST_F(SetPowerModeInternalTest,transitionsDisplayFromDozeSuspendToOffExternalDisplay)3222 TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffExternalDisplay) {
3223 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
3224 }
3225
TEST_F(SetPowerModeInternalTest,transitionsDisplayFromOnToDozeExternalDisplay)3226 TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeExternalDisplay) {
3227 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeVariant>>();
3228 }
3229
TEST_F(SetPowerModeInternalTest,transitionsDisplayFromDozeSuspendToDozeExternalDisplay)3230 TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozeExternalDisplay) {
3231 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
3232 }
3233
TEST_F(SetPowerModeInternalTest,transitionsDisplayFromDozeToOnExternalDisplay)3234 TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnExternalDisplay) {
3235 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeToOnVariant>>();
3236 }
3237
TEST_F(SetPowerModeInternalTest,transitionsDisplayFromDozeSuspendToOnExternalDisplay)3238 TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnExternalDisplay) {
3239 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
3240 }
3241
TEST_F(SetPowerModeInternalTest,transitionsDisplayFromOnToDozeSuspendExternalDisplay)3242 TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendExternalDisplay) {
3243 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
3244 }
3245
TEST_F(SetPowerModeInternalTest,transitionsDisplayFromOnToUnknownExternalDisplay)3246 TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownExternalDisplay) {
3247 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToUnknownVariant>>();
3248 }
3249
3250 } // namespace
3251 } // namespace android
3252