blob: bd9b1405283d6d9aceddb757ecce5d7e91c83e4f [file] [log] [blame]
Lloyd Piquef58625d2017-12-19 13:22:33 -08001/*
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
Dominik Laskowski075d3172018-05-24 15:50:06 -070020#include <type_traits>
21
Lloyd Piquef58625d2017-12-19 13:22:33 -080022#include <gmock/gmock.h>
23#include <gtest/gtest.h>
24
25#include <log/log.h>
26
Valerie Hau9758ae02018-10-09 16:05:09 -070027#include <ui/DebugUtils.h>
Dominik Laskowski075d3172018-05-24 15:50:06 -070028
29#include "DisplayIdentificationTest.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080030#include "TestableSurfaceFlinger.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080031#include "mock/DisplayHardware/MockComposer.h"
32#include "mock/DisplayHardware/MockDisplaySurface.h"
Lloyd Pique41be5d22018-06-21 13:11:48 -070033#include "mock/MockDispSync.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080034#include "mock/MockEventControlThread.h"
35#include "mock/MockEventThread.h"
36#include "mock/MockMessageQueue.h"
37#include "mock/MockNativeWindowSurface.h"
38#include "mock/MockSurfaceInterceptor.h"
39#include "mock/RenderEngine/MockRenderEngine.h"
40#include "mock/gui/MockGraphicBufferConsumer.h"
41#include "mock/gui/MockGraphicBufferProducer.h"
42#include "mock/system/window/MockNativeWindow.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080043
44namespace android {
45namespace {
46
Lloyd Piquee39cad22017-12-20 17:01:29 -080047using testing::_;
48using testing::ByMove;
49using testing::DoAll;
50using testing::Mock;
51using testing::Return;
52using testing::SetArgPointee;
53
Lloyd Piqued883d5a2018-04-27 19:32:30 -070054using android::Hwc2::ColorMode;
Lloyd Piquee39cad22017-12-20 17:01:29 -080055using android::Hwc2::Error;
Lloyd Piqued883d5a2018-04-27 19:32:30 -070056using android::Hwc2::Hdr;
Lloyd Piquee39cad22017-12-20 17:01:29 -080057using android::Hwc2::IComposer;
58using android::Hwc2::IComposerClient;
Lloyd Piqued883d5a2018-04-27 19:32:30 -070059using android::Hwc2::PerFrameMetadataKey;
60using android::Hwc2::RenderIntent;
Lloyd Piquee39cad22017-12-20 17:01:29 -080061
Lloyd Piquec11e0d32018-01-22 18:44:59 -080062using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector;
63using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
Lloyd Pique1fa4d462018-01-22 18:03:16 -080064using HotplugEvent = TestableSurfaceFlinger::HotplugEvent;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080065using HWC2Display = TestableSurfaceFlinger::HWC2Display;
Lloyd Piquebc792092018-01-17 11:52:30 -080066
Lloyd Piquec11e0d32018-01-22 18:44:59 -080067constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666;
Lloyd Piquee39cad22017-12-20 17:01:29 -080068constexpr int32_t DEFAULT_DPI = 320;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080069constexpr int DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT = HAL_PIXEL_FORMAT_RGB_565;
Lloyd Piquee39cad22017-12-20 17:01:29 -080070
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -080071constexpr int HWC_POWER_MODE_LEET = 1337; // An out of range power mode value
72
Lloyd Piquec11e0d32018-01-22 18:44:59 -080073/* ------------------------------------------------------------------------
74 * Boolean avoidance
75 *
76 * To make calls and template instantiations more readable, we define some
77 * local enums along with an implicit bool conversion.
78 */
79
80#define BOOL_SUBSTITUTE(TYPENAME) enum class TYPENAME : bool { FALSE = false, TRUE = true };
81
Lloyd Piquec11e0d32018-01-22 18:44:59 -080082BOOL_SUBSTITUTE(Async);
Dominik Laskowski075d3172018-05-24 15:50:06 -070083BOOL_SUBSTITUTE(Critical);
84BOOL_SUBSTITUTE(Primary);
Lloyd Piquec11e0d32018-01-22 18:44:59 -080085BOOL_SUBSTITUTE(Secure);
Dominik Laskowski075d3172018-05-24 15:50:06 -070086BOOL_SUBSTITUTE(Virtual);
Lloyd Piquec11e0d32018-01-22 18:44:59 -080087
88/* ------------------------------------------------------------------------
89 *
90 */
Lloyd Pique1fa4d462018-01-22 18:03:16 -080091
Lloyd Piquef58625d2017-12-19 13:22:33 -080092class DisplayTransactionTest : public testing::Test {
Lloyd Piquec11e0d32018-01-22 18:44:59 -080093public:
Lloyd Piquef58625d2017-12-19 13:22:33 -080094 DisplayTransactionTest();
95 ~DisplayTransactionTest() override;
96
Lloyd Pique1fa4d462018-01-22 18:03:16 -080097 // --------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -080098 // Mock/Fake injection
Lloyd Piquef58625d2017-12-19 13:22:33 -080099
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800100 void injectMockComposer(int virtualDisplayCount);
101 void injectFakeBufferQueueFactory();
102 void injectFakeNativeWindowSurfaceFactory();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800103
104 // --------------------------------------------------------------------
105 // Postcondition helpers
106
Dominik Laskowski075d3172018-05-24 15:50:06 -0700107 bool hasPhysicalHwcDisplay(hwc2_display_t hwcDisplayId);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800108 bool hasTransactionFlagSet(int flag);
109 bool hasDisplayDevice(sp<IBinder> displayToken);
110 sp<DisplayDevice> getDisplayDevice(sp<IBinder> displayToken);
111 bool hasCurrentDisplayState(sp<IBinder> displayToken);
112 const DisplayDeviceState& getCurrentDisplayState(sp<IBinder> displayToken);
113 bool hasDrawingDisplayState(sp<IBinder> displayToken);
114 const DisplayDeviceState& getDrawingDisplayState(sp<IBinder> displayToken);
115
116 // --------------------------------------------------------------------
117 // Test instances
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800118
Lloyd Piquef58625d2017-12-19 13:22:33 -0800119 TestableSurfaceFlinger mFlinger;
Lloyd Piquee39cad22017-12-20 17:01:29 -0800120 mock::EventThread* mEventThread = new mock::EventThread();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800121 mock::EventControlThread* mEventControlThread = new mock::EventControlThread();
Alec Mouriba013fa2018-10-16 12:43:11 -0700122 sp<mock::NativeWindow> mNativeWindow = new mock::NativeWindow();
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800123 sp<GraphicBuffer> mBuffer = new GraphicBuffer();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800124
125 // These mocks are created by the test, but are destroyed by SurfaceFlinger
126 // by virtue of being stored into a std::unique_ptr. However we still need
127 // to keep a reference to them for use in setting up call expectations.
Peiyong Lin833074a2018-08-28 11:53:54 -0700128 renderengine::mock::RenderEngine* mRenderEngine = new renderengine::mock::RenderEngine();
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800129 Hwc2::mock::Composer* mComposer = nullptr;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800130 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
131 mock::SurfaceInterceptor* mSurfaceInterceptor = new mock::SurfaceInterceptor();
Lloyd Pique41be5d22018-06-21 13:11:48 -0700132 mock::DispSync* mPrimaryDispSync = new mock::DispSync();
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800133
134 // These mocks are created only when expected to be created via a factory.
135 sp<mock::GraphicBufferConsumer> mConsumer;
136 sp<mock::GraphicBufferProducer> mProducer;
Lloyd Pique22098362018-09-13 11:46:49 -0700137 surfaceflinger::mock::NativeWindowSurface* mNativeWindowSurface = nullptr;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800138};
139
140DisplayTransactionTest::DisplayTransactionTest() {
141 const ::testing::TestInfo* const test_info =
142 ::testing::UnitTest::GetInstance()->current_test_info();
143 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800144
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800145 // Default to no wide color display support configured
146 mFlinger.mutableHasWideColorDisplay() = false;
Peiyong Lin13effd12018-07-24 17:01:47 -0700147 mFlinger.mutableUseColorManagement() = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800148 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
149
150 // Default to using HWC virtual displays
151 mFlinger.mutableUseHwcVirtualDisplays() = true;
152
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800153 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
154 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
155 });
156
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800157 mFlinger.setCreateNativeWindowSurface([](auto) {
158 ADD_FAILURE() << "Unexpected request to create a native window surface.";
159 return nullptr;
160 });
161
162 mFlinger.mutableEventControlThread().reset(mEventControlThread);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800163 mFlinger.mutableEventThread().reset(mEventThread);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800164 mFlinger.mutableEventQueue().reset(mMessageQueue);
Peiyong Lin833074a2018-08-28 11:53:54 -0700165 mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800166 mFlinger.mutableInterceptor().reset(mSurfaceInterceptor);
Lloyd Pique41be5d22018-06-21 13:11:48 -0700167 mFlinger.mutablePrimaryDispSync().reset(mPrimaryDispSync);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800168
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800169 injectMockComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -0800170}
171
172DisplayTransactionTest::~DisplayTransactionTest() {
173 const ::testing::TestInfo* const test_info =
174 ::testing::UnitTest::GetInstance()->current_test_info();
175 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
176}
177
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800178void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
179 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800180 EXPECT_CALL(*mComposer, getCapabilities())
181 .WillOnce(Return(std::vector<IComposer::Capability>()));
182 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
183 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -0800184
Lloyd Piquee39cad22017-12-20 17:01:29 -0800185 Mock::VerifyAndClear(mComposer);
186}
187
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800188void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800189 // This setup is only expected once per test.
190 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
191
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800192 mConsumer = new mock::GraphicBufferConsumer();
193 mProducer = new mock::GraphicBufferProducer();
194
195 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
196 *outProducer = mProducer;
197 *outConsumer = mConsumer;
198 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800199}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800200
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800201void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800202 // This setup is only expected once per test.
203 ASSERT_TRUE(mNativeWindowSurface == nullptr);
204
Lloyd Pique22098362018-09-13 11:46:49 -0700205 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800206
Lloyd Pique22098362018-09-13 11:46:49 -0700207 mFlinger.setCreateNativeWindowSurface([this](auto) {
208 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
209 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800210}
211
Dominik Laskowski075d3172018-05-24 15:50:06 -0700212bool DisplayTransactionTest::hasPhysicalHwcDisplay(hwc2_display_t hwcDisplayId) {
213 return mFlinger.mutableHwcPhysicalDisplayIdMap().count(hwcDisplayId) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800214}
215
216bool DisplayTransactionTest::hasTransactionFlagSet(int flag) {
217 return mFlinger.mutableTransactionFlags() & flag;
218}
219
220bool DisplayTransactionTest::hasDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700221 return mFlinger.mutableDisplays().count(displayToken) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800222}
223
224sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700225 return mFlinger.mutableDisplays()[displayToken];
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800226}
227
228bool DisplayTransactionTest::hasCurrentDisplayState(sp<IBinder> displayToken) {
229 return mFlinger.mutableCurrentState().displays.indexOfKey(displayToken) >= 0;
230}
231
232const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(sp<IBinder> displayToken) {
233 return mFlinger.mutableCurrentState().displays.valueFor(displayToken);
234}
235
236bool DisplayTransactionTest::hasDrawingDisplayState(sp<IBinder> displayToken) {
237 return mFlinger.mutableDrawingState().displays.indexOfKey(displayToken) >= 0;
238}
239
240const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(sp<IBinder> displayToken) {
241 return mFlinger.mutableDrawingState().displays.valueFor(displayToken);
242}
243
244/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800245 *
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800246 */
247
Dominik Laskowski075d3172018-05-24 15:50:06 -0700248template <typename PhysicalDisplay>
249struct PhysicalDisplayId {};
250
Dominik Laskowski34157762018-10-31 13:07:19 -0700251template <DisplayId::Type displayId>
252using VirtualDisplayId = std::integral_constant<DisplayId::Type, displayId>;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700253
254struct NoDisplayId {};
255
256template <typename>
257struct IsPhysicalDisplayId : std::bool_constant<false> {};
258
259template <typename PhysicalDisplay>
260struct IsPhysicalDisplayId<PhysicalDisplayId<PhysicalDisplay>> : std::bool_constant<true> {};
261
262template <typename>
263struct DisplayIdGetter;
264
265template <typename PhysicalDisplay>
266struct DisplayIdGetter<PhysicalDisplayId<PhysicalDisplay>> {
267 static std::optional<DisplayId> get() {
268 if (!PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
269 return getFallbackDisplayId(static_cast<bool>(PhysicalDisplay::PRIMARY)
270 ? HWC_DISPLAY_PRIMARY
271 : HWC_DISPLAY_EXTERNAL);
272 }
273
274 const auto info =
275 parseDisplayIdentificationData(PhysicalDisplay::PORT,
276 PhysicalDisplay::GET_IDENTIFICATION_DATA());
277 return info ? std::make_optional(info->id) : std::nullopt;
278 }
279};
280
Dominik Laskowski34157762018-10-31 13:07:19 -0700281template <DisplayId::Type displayId>
Dominik Laskowski075d3172018-05-24 15:50:06 -0700282struct DisplayIdGetter<VirtualDisplayId<displayId>> {
Dominik Laskowski34157762018-10-31 13:07:19 -0700283 static std::optional<DisplayId> get() { return DisplayId{displayId}; }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700284};
285
286template <>
287struct DisplayIdGetter<NoDisplayId> {
288 static std::optional<DisplayId> get() { return {}; }
289};
290
291// DisplayIdType can be:
292// 1) PhysicalDisplayId<...> for generated ID of physical display backed by HWC.
293// 2) VirtualDisplayId<...> for hard-coded ID of virtual display backed by HWC.
294// 3) NoDisplayId for virtual display without HWC backing.
295template <typename DisplayIdType, int width, int height, Critical critical, Async async,
296 Secure secure, Primary primary, int grallocUsage>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800297struct DisplayVariant {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700298 using DISPLAY_ID = DisplayIdGetter<DisplayIdType>;
299
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800300 // The display width and height
301 static constexpr int WIDTH = width;
302 static constexpr int HEIGHT = height;
303
304 static constexpr int GRALLOC_USAGE = grallocUsage;
305
Dominik Laskowski075d3172018-05-24 15:50:06 -0700306 // Whether the display is virtual or physical
307 static constexpr Virtual VIRTUAL =
308 IsPhysicalDisplayId<DisplayIdType>{} ? Virtual::FALSE : Virtual::TRUE;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800309
310 // When creating native window surfaces for the framebuffer, whether those should be critical
311 static constexpr Critical CRITICAL = critical;
312
313 // When creating native window surfaces for the framebuffer, whether those should be async
314 static constexpr Async ASYNC = async;
315
316 // Whether the display should be treated as secure
317 static constexpr Secure SECURE = secure;
318
Dominik Laskowski075d3172018-05-24 15:50:06 -0700319 // Whether the display is primary
320 static constexpr Primary PRIMARY = primary;
321
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800322 static auto makeFakeExistingDisplayInjector(DisplayTransactionTest* test) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700323 auto injector =
324 FakeDisplayDeviceInjector(test->mFlinger, DISPLAY_ID::get(),
325 static_cast<bool>(VIRTUAL), static_cast<bool>(PRIMARY));
326
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800327 injector.setSecure(static_cast<bool>(SECURE));
Alec Mouriba013fa2018-10-16 12:43:11 -0700328 injector.setNativeWindow(test->mNativeWindow);
329
330 // Creating a DisplayDevice requires getting default dimensions from the
331 // native window.
332 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
333 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
334 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
335 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800336 return injector;
337 }
338
339 // Called by tests to set up any native window creation call expectations.
340 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
341 EXPECT_CALL(*test->mNativeWindowSurface, getNativeWindow())
342 .WillOnce(Return(test->mNativeWindow));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800343
Alec Mouriba013fa2018-10-16 12:43:11 -0700344 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
345 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
346 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
347 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800348 }
349
350 static void setupFramebufferConsumerBufferQueueCallExpectations(DisplayTransactionTest* test) {
351 EXPECT_CALL(*test->mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR));
352 EXPECT_CALL(*test->mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR));
353 EXPECT_CALL(*test->mConsumer, setConsumerUsageBits(GRALLOC_USAGE))
354 .WillRepeatedly(Return(NO_ERROR));
355 EXPECT_CALL(*test->mConsumer, setDefaultBufferSize(WIDTH, HEIGHT))
356 .WillRepeatedly(Return(NO_ERROR));
357 EXPECT_CALL(*test->mConsumer, setMaxAcquiredBufferCount(_))
358 .WillRepeatedly(Return(NO_ERROR));
359 }
360
361 static void setupFramebufferProducerBufferQueueCallExpectations(DisplayTransactionTest* test) {
362 EXPECT_CALL(*test->mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return());
363 }
364};
365
Dominik Laskowski075d3172018-05-24 15:50:06 -0700366template <hwc2_display_t hwcDisplayId, HWC2::DisplayType hwcDisplayType, typename DisplayVariant,
367 typename PhysicalDisplay = void>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800368struct HwcDisplayVariant {
369 // The display id supplied by the HWC
370 static constexpr hwc2_display_t HWC_DISPLAY_ID = hwcDisplayId;
371
372 // The HWC display type
373 static constexpr HWC2::DisplayType HWC_DISPLAY_TYPE = hwcDisplayType;
374
375 // The HWC active configuration id
Lloyd Pique3c085a02018-05-09 19:38:32 -0700376 static constexpr int HWC_ACTIVE_CONFIG_ID = 2001;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800377
378 static void injectPendingHotplugEvent(DisplayTransactionTest* test,
379 HWC2::Connection connection) {
380 test->mFlinger.mutablePendingHotplugEvents().emplace_back(
381 HotplugEvent{HWC_DISPLAY_ID, connection});
382 }
383
384 // Called by tests to inject a HWC display setup
385 static void injectHwcDisplay(DisplayTransactionTest* test) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700386 const auto displayId = DisplayVariant::DISPLAY_ID::get();
387 ASSERT_TRUE(displayId);
388 FakeHwcDisplayInjector(*displayId, HWC_DISPLAY_TYPE,
389 static_cast<bool>(DisplayVariant::PRIMARY))
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800390 .setHwcDisplayId(HWC_DISPLAY_ID)
391 .setWidth(DisplayVariant::WIDTH)
392 .setHeight(DisplayVariant::HEIGHT)
393 .setActiveConfig(HWC_ACTIVE_CONFIG_ID)
394 .inject(&test->mFlinger, test->mComposer);
395 }
396
397 static void setupHwcHotplugCallExpectations(DisplayTransactionTest* test) {
398 EXPECT_CALL(*test->mComposer, getDisplayType(HWC_DISPLAY_ID, _))
399 .WillOnce(DoAll(SetArgPointee<1>(static_cast<IComposerClient::DisplayType>(
400 HWC_DISPLAY_TYPE)),
401 Return(Error::NONE)));
402 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
403 EXPECT_CALL(*test->mComposer, getDisplayConfigs(HWC_DISPLAY_ID, _))
404 .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{HWC_ACTIVE_CONFIG_ID}),
405 Return(Error::NONE)));
406 EXPECT_CALL(*test->mComposer,
407 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
408 IComposerClient::Attribute::WIDTH, _))
409 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::WIDTH), Return(Error::NONE)));
410 EXPECT_CALL(*test->mComposer,
411 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
412 IComposerClient::Attribute::HEIGHT, _))
413 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::HEIGHT), Return(Error::NONE)));
414 EXPECT_CALL(*test->mComposer,
415 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
416 IComposerClient::Attribute::VSYNC_PERIOD, _))
417 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE)));
418 EXPECT_CALL(*test->mComposer,
419 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
420 IComposerClient::Attribute::DPI_X, _))
421 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
422 EXPECT_CALL(*test->mComposer,
423 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
424 IComposerClient::Attribute::DPI_Y, _))
425 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700426
427 if (PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
428 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
429 .WillOnce(DoAll(SetArgPointee<1>(PhysicalDisplay::PORT),
430 SetArgPointee<2>(PhysicalDisplay::GET_IDENTIFICATION_DATA()),
431 Return(Error::NONE)));
432 } else {
433 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
434 .WillOnce(Return(Error::UNSUPPORTED));
435 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800436 }
437
438 // Called by tests to set up HWC call expectations
439 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
440 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY_ID, _))
Lloyd Pique3c085a02018-05-09 19:38:32 -0700441 .WillRepeatedly(DoAll(SetArgPointee<1>(HWC_ACTIVE_CONFIG_ID), Return(Error::NONE)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800442 }
443};
444
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800445// Physical displays are expected to be synchronous, secure, and have a HWC display for output.
446constexpr uint32_t GRALLOC_USAGE_PHYSICAL_DISPLAY =
447 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
448
Dominik Laskowski075d3172018-05-24 15:50:06 -0700449template <hwc2_display_t hwcDisplayId, typename PhysicalDisplay, int width, int height,
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800450 Critical critical>
451struct PhysicalDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700452 : DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height, critical, Async::FALSE,
453 Secure::TRUE, PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
454 HwcDisplayVariant<hwcDisplayId, HWC2::DisplayType::Physical,
455 DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height,
456 critical, Async::FALSE, Secure::TRUE,
457 PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
458 PhysicalDisplay> {};
459
460template <bool hasIdentificationData>
461struct PrimaryDisplay {
462 static constexpr Primary PRIMARY = Primary::TRUE;
463 static constexpr uint8_t PORT = 255;
464 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
465 static constexpr auto GET_IDENTIFICATION_DATA = getInternalEdid;
466};
467
468template <bool hasIdentificationData>
469struct ExternalDisplay {
470 static constexpr Primary PRIMARY = Primary::FALSE;
471 static constexpr uint8_t PORT = 254;
472 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
473 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
474};
475
476struct TertiaryDisplay {
477 static constexpr Primary PRIMARY = Primary::FALSE;
478};
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800479
480// A primary display is a physical display that is critical
481using PrimaryDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700482 PhysicalDisplayVariant<1001, PrimaryDisplay<false>, 3840, 2160, Critical::TRUE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800483
484// An external display is physical display that is not critical.
485using ExternalDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700486 PhysicalDisplayVariant<1002, ExternalDisplay<false>, 1920, 1280, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800487
488using TertiaryDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700489 PhysicalDisplayVariant<1003, TertiaryDisplay, 1600, 1200, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800490
491// A virtual display not supported by the HWC.
492constexpr uint32_t GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY = 0;
493
494template <int width, int height, Secure secure>
495struct NonHwcVirtualDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700496 : DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
497 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY> {
498 using Base = DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
499 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>;
500
501 static void injectHwcDisplay(DisplayTransactionTest*) {}
502
503 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
504 EXPECT_CALL(*test->mComposer, getActiveConfig(_, _)).Times(0);
505 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800506
507 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
508 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
509 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
510 }
511};
512
513// A virtual display supported by the HWC.
514constexpr uint32_t GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY = GRALLOC_USAGE_HW_COMPOSER;
515
516template <int width, int height, Secure secure>
517struct HwcVirtualDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700518 : DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE, secure,
519 Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>,
520 HwcDisplayVariant<
521 1010, HWC2::DisplayType::Virtual,
522 DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
523 secure, Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>> {
524 using Base = DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
525 secure, Primary::FALSE, GRALLOC_USAGE_HW_COMPOSER>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800526 using Self = HwcVirtualDisplayVariant<width, height, secure>;
527
528 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
529 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
530 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
531 }
532
533 static void setupHwcVirtualDisplayCreationCallExpectations(DisplayTransactionTest* test) {
534 EXPECT_CALL(*test->mComposer, createVirtualDisplay(Base::WIDTH, Base::HEIGHT, _, _))
535 .WillOnce(DoAll(SetArgPointee<3>(Self::HWC_DISPLAY_ID), Return(Error::NONE)));
536 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
537 }
538};
539
540// For this variant, SurfaceFlinger should not configure itself with wide
541// display support, so the display should not be configured for wide-color
542// support.
543struct WideColorSupportNotConfiguredVariant {
544 static constexpr bool WIDE_COLOR_SUPPORTED = false;
545
546 static void injectConfigChange(DisplayTransactionTest* test) {
547 test->mFlinger.mutableHasWideColorDisplay() = false;
Peiyong Lin13effd12018-07-24 17:01:47 -0700548 test->mFlinger.mutableUseColorManagement() = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800549 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
550 }
551
552 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
553 EXPECT_CALL(*test->mComposer, getColorModes(_, _)).Times(0);
554 EXPECT_CALL(*test->mComposer, getRenderIntents(_, _, _)).Times(0);
555 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
556 }
557};
558
559// For this variant, SurfaceFlinger should configure itself with wide display
560// support, and the display should respond with an non-empty list of supported
561// color modes. Wide-color support should be configured.
562template <typename Display>
563struct WideColorP3ColorimetricSupportedVariant {
564 static constexpr bool WIDE_COLOR_SUPPORTED = true;
565
566 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700567 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800568 test->mFlinger.mutableHasWideColorDisplay() = true;
569 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
570 }
571
572 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
573 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
574 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>({ColorMode::DISPLAY_P3})),
575 Return(Error::NONE)));
576 EXPECT_CALL(*test->mComposer,
577 getRenderIntents(Display::HWC_DISPLAY_ID, ColorMode::DISPLAY_P3, _))
578 .WillOnce(DoAll(SetArgPointee<2>(
579 std::vector<RenderIntent>({RenderIntent::COLORIMETRIC})),
580 Return(Error::NONE)));
581 EXPECT_CALL(*test->mComposer,
582 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::SRGB,
583 RenderIntent::COLORIMETRIC))
584 .WillOnce(Return(Error::NONE));
585 }
586};
587
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800588// For this variant, SurfaceFlinger should configure itself with wide display
589// support, but the display should respond with an empty list of supported color
590// modes. Wide-color support for the display should not be configured.
591template <typename Display>
592struct WideColorNotSupportedVariant {
593 static constexpr bool WIDE_COLOR_SUPPORTED = false;
594
595 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700596 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800597 test->mFlinger.mutableHasWideColorDisplay() = true;
598 }
599
600 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
601 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
602 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>()), Return(Error::NONE)));
Chia-I Wu614e1422018-05-23 02:17:03 -0700603 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800604 }
605};
606
607// For this variant, the display is not a HWC display, so no HDR support should
608// be configured.
609struct NonHwcDisplayHdrSupportVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800610 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800611 static constexpr bool HDR10_SUPPORTED = false;
612 static constexpr bool HDR_HLG_SUPPORTED = false;
613 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
614 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
615 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
616 }
617};
618
Valerie Haue9e843a2018-12-18 13:39:23 -0800619template <typename Display>
620struct Hdr10PlusSupportedVariant {
621 static constexpr bool HDR10_PLUS_SUPPORTED = true;
622 static constexpr bool HDR10_SUPPORTED = true;
623 static constexpr bool HDR_HLG_SUPPORTED = false;
624 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
625 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
626 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _))
627 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({
628 Hdr::HDR10_PLUS,
629 Hdr::HDR10,
630 })),
631 Return(Error::NONE)));
632 }
633};
634
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800635// For this variant, the composer should respond with a non-empty list of HDR
636// modes containing HDR10, so HDR10 support should be configured.
637template <typename Display>
638struct Hdr10SupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800639 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800640 static constexpr bool HDR10_SUPPORTED = true;
641 static constexpr bool HDR_HLG_SUPPORTED = false;
642 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
643 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
644 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
645 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HDR10})),
646 Return(Error::NONE)));
647 }
648};
649
650// For this variant, the composer should respond with a non-empty list of HDR
651// modes containing HLG, so HLG support should be configured.
652template <typename Display>
653struct HdrHlgSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800654 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800655 static constexpr bool HDR10_SUPPORTED = false;
656 static constexpr bool HDR_HLG_SUPPORTED = true;
657 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
658 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
659 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
660 .WillOnce(
661 DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HLG})), Return(Error::NONE)));
662 }
663};
664
665// For this variant, the composer should respond with a non-empty list of HDR
666// modes containing DOLBY_VISION, so DOLBY_VISION support should be configured.
667template <typename Display>
668struct HdrDolbyVisionSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800669 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800670 static constexpr bool HDR10_SUPPORTED = false;
671 static constexpr bool HDR_HLG_SUPPORTED = false;
672 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = true;
673 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
674 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
675 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::DOLBY_VISION})),
676 Return(Error::NONE)));
677 }
678};
679
680// For this variant, the composer should respond with am empty list of HDR
681// modes, so no HDR support should be configured.
682template <typename Display>
683struct HdrNotSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800684 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800685 static constexpr bool HDR10_SUPPORTED = false;
686 static constexpr bool HDR_HLG_SUPPORTED = false;
687 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
688 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
689 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
690 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
691 }
692};
693
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700694struct NonHwcPerFrameMetadataSupportVariant {
695 static constexpr int PER_FRAME_METADATA_KEYS = 0;
696 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800697 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(_)).Times(0);
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700698 }
699};
700
701template <typename Display>
702struct NoPerFrameMetadataSupportVariant {
703 static constexpr int PER_FRAME_METADATA_KEYS = 0;
704 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800705 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
706 .WillOnce(Return(std::vector<PerFrameMetadataKey>()));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700707 }
708};
709
710template <typename Display>
711struct Smpte2086PerFrameMetadataSupportVariant {
712 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::SMPTE2086;
713 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800714 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
715 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Valerie Haue9e843a2018-12-18 13:39:23 -0800716 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X,
717 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y,
718 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X,
719 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y,
720 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X,
721 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y,
722 PerFrameMetadataKey::WHITE_POINT_X,
723 PerFrameMetadataKey::WHITE_POINT_Y,
724 PerFrameMetadataKey::MAX_LUMINANCE,
725 PerFrameMetadataKey::MIN_LUMINANCE,
726 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700727 }
728};
729
730template <typename Display>
731struct Cta861_3_PerFrameMetadataSupportVariant {
732 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::CTA861_3;
733 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800734 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
735 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Valerie Haue9e843a2018-12-18 13:39:23 -0800736 PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL,
737 PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL,
738 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700739 }
740};
741
Valerie Haue9e843a2018-12-18 13:39:23 -0800742template <typename Display>
743struct Hdr10_Plus_PerFrameMetadataSupportVariant {
744 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::HDR10PLUS;
745 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
746 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
747 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
748 PerFrameMetadataKey::HDR10_PLUS_SEI,
749 })));
750 }
751};
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800752/* ------------------------------------------------------------------------
753 * Typical display configurations to test
754 */
755
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700756template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy,
757 typename PerFrameMetadataSupportPolicy>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800758struct Case {
759 using Display = DisplayPolicy;
760 using WideColorSupport = WideColorSupportPolicy;
761 using HdrSupport = HdrSupportPolicy;
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700762 using PerFrameMetadataSupport = PerFrameMetadataSupportPolicy;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800763};
764
765using SimplePrimaryDisplayCase =
766 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700767 HdrNotSupportedVariant<PrimaryDisplayVariant>,
768 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800769using SimpleExternalDisplayCase =
770 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700771 HdrNotSupportedVariant<ExternalDisplayVariant>,
772 NoPerFrameMetadataSupportVariant<ExternalDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800773using SimpleTertiaryDisplayCase =
774 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700775 HdrNotSupportedVariant<TertiaryDisplayVariant>,
776 NoPerFrameMetadataSupportVariant<TertiaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800777using NonHwcVirtualDisplayCase =
778 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700779 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant,
780 NonHwcPerFrameMetadataSupportVariant>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800781using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
782using HwcVirtualDisplayCase =
783 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
Lloyd Pique438e9e72018-09-04 18:06:08 -0700784 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>,
tangrobin6753a022018-08-10 10:58:54 +0800785 NoPerFrameMetadataSupportVariant<SimpleHwcVirtualDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800786using WideColorP3ColorimetricDisplayCase =
787 Case<PrimaryDisplayVariant, WideColorP3ColorimetricSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700788 HdrNotSupportedVariant<PrimaryDisplayVariant>,
789 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Valerie Haue9e843a2018-12-18 13:39:23 -0800790using Hdr10PlusDisplayCase =
791 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
792 Hdr10SupportedVariant<PrimaryDisplayVariant>,
793 Hdr10_Plus_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800794using Hdr10DisplayCase =
795 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700796 Hdr10SupportedVariant<PrimaryDisplayVariant>,
797 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800798using HdrHlgDisplayCase =
799 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700800 HdrHlgSupportedVariant<PrimaryDisplayVariant>,
801 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800802using HdrDolbyVisionDisplayCase =
803 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700804 HdrDolbyVisionSupportedVariant<PrimaryDisplayVariant>,
805 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
806using HdrSmpte2086DisplayCase =
807 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
808 HdrNotSupportedVariant<PrimaryDisplayVariant>,
809 Smpte2086PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
810using HdrCta861_3_DisplayCase =
811 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
812 HdrNotSupportedVariant<PrimaryDisplayVariant>,
813 Cta861_3_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Dominik Laskowskif07b85b2018-06-11 12:49:15 -0700814
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800815/* ------------------------------------------------------------------------
Lloyd Pique6cf11032018-01-22 18:57:44 -0800816 *
817 * SurfaceFlinger::onHotplugReceived
818 */
819
820TEST_F(DisplayTransactionTest, hotplugEnqueuesEventsForDisplayTransaction) {
821 constexpr int currentSequenceId = 123;
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700822 constexpr hwc2_display_t hwcDisplayId1 = 456;
823 constexpr hwc2_display_t hwcDisplayId2 = 654;
Lloyd Pique6cf11032018-01-22 18:57:44 -0800824
825 // --------------------------------------------------------------------
826 // Preconditions
827
828 // Set the current sequence id for accepted events
829 mFlinger.mutableComposerSequenceId() = currentSequenceId;
830
831 // Set the main thread id so that the current thread does not appear to be
832 // the main thread.
833 mFlinger.mutableMainThreadId() = std::thread::id();
834
835 // --------------------------------------------------------------------
836 // Call Expectations
837
838 // We expect invalidate() to be invoked once to trigger display transaction
839 // processing.
840 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
841
842 // --------------------------------------------------------------------
843 // Invocation
844
845 // Simulate two hotplug events (a connect and a disconnect)
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700846 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId1, HWC2::Connection::Connected);
847 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId2, HWC2::Connection::Disconnected);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800848
849 // --------------------------------------------------------------------
850 // Postconditions
851
852 // The display transaction needed flag should be set.
853 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
854
855 // All events should be in the pending event queue.
856 const auto& pendingEvents = mFlinger.mutablePendingHotplugEvents();
857 ASSERT_EQ(2u, pendingEvents.size());
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700858 EXPECT_EQ(hwcDisplayId1, pendingEvents[0].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800859 EXPECT_EQ(HWC2::Connection::Connected, pendingEvents[0].connection);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700860 EXPECT_EQ(hwcDisplayId2, pendingEvents[1].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800861 EXPECT_EQ(HWC2::Connection::Disconnected, pendingEvents[1].connection);
862}
863
864TEST_F(DisplayTransactionTest, hotplugDiscardsUnexpectedEvents) {
865 constexpr int currentSequenceId = 123;
866 constexpr int otherSequenceId = 321;
867 constexpr hwc2_display_t displayId = 456;
868
869 // --------------------------------------------------------------------
870 // Preconditions
871
872 // Set the current sequence id for accepted events
873 mFlinger.mutableComposerSequenceId() = currentSequenceId;
874
875 // Set the main thread id so that the current thread does not appear to be
876 // the main thread.
877 mFlinger.mutableMainThreadId() = std::thread::id();
878
879 // --------------------------------------------------------------------
880 // Call Expectations
881
882 // We do not expect any calls to invalidate().
883 EXPECT_CALL(*mMessageQueue, invalidate()).Times(0);
884
885 // --------------------------------------------------------------------
886 // Invocation
887
888 // Call with an unexpected sequence id
889 mFlinger.onHotplugReceived(otherSequenceId, displayId, HWC2::Connection::Invalid);
890
891 // --------------------------------------------------------------------
892 // Postconditions
893
894 // The display transaction needed flag should not be set
895 EXPECT_FALSE(hasTransactionFlagSet(eDisplayTransactionNeeded));
896
897 // There should be no pending events
898 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
899}
900
901TEST_F(DisplayTransactionTest, hotplugProcessesEnqueuedEventsIfCalledOnMainThread) {
902 constexpr int currentSequenceId = 123;
903 constexpr hwc2_display_t displayId1 = 456;
904
905 // --------------------------------------------------------------------
906 // Note:
907 // --------------------------------------------------------------------
908 // This test case is a bit tricky. We want to verify that
909 // onHotplugReceived() calls processDisplayHotplugEventsLocked(), but we
910 // don't really want to provide coverage for everything the later function
911 // does as there are specific tests for it.
912 // --------------------------------------------------------------------
913
914 // --------------------------------------------------------------------
915 // Preconditions
916
917 // Set the current sequence id for accepted events
918 mFlinger.mutableComposerSequenceId() = currentSequenceId;
919
920 // Set the main thread id so that the current thread does appear to be the
921 // main thread.
922 mFlinger.mutableMainThreadId() = std::this_thread::get_id();
923
924 // --------------------------------------------------------------------
925 // Call Expectations
926
927 // We expect invalidate() to be invoked once to trigger display transaction
928 // processing.
929 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
930
931 // --------------------------------------------------------------------
932 // Invocation
933
934 // Simulate a disconnect on a display id that is not connected. This should
935 // be enqueued by onHotplugReceived(), and dequeued by
936 // processDisplayHotplugEventsLocked(), but then ignored as invalid.
937 mFlinger.onHotplugReceived(currentSequenceId, displayId1, HWC2::Connection::Disconnected);
938
939 // --------------------------------------------------------------------
940 // Postconditions
941
942 // The display transaction needed flag should be set.
943 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
944
945 // There should be no event queued on return, as it should have been
946 // processed.
947 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
948}
949
950/* ------------------------------------------------------------------------
Lloyd Piquea482f992018-01-22 19:00:34 -0800951 * SurfaceFlinger::createDisplay
952 */
953
954TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForNonsecureDisplay) {
955 const String8 name("virtual.test");
956
957 // --------------------------------------------------------------------
958 // Call Expectations
959
960 // The call should notify the interceptor that a display was created.
961 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
962
963 // --------------------------------------------------------------------
964 // Invocation
965
966 sp<IBinder> displayToken = mFlinger.createDisplay(name, false);
967
968 // --------------------------------------------------------------------
969 // Postconditions
970
971 // The display should have been added to the current state
972 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
973 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700974 EXPECT_TRUE(display.isVirtual());
975 EXPECT_FALSE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -0800976 EXPECT_EQ(name.string(), display.displayName);
977
978 // --------------------------------------------------------------------
979 // Cleanup conditions
980
981 // Destroying the display invalidates the display state.
982 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
983}
984
985TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForSecureDisplay) {
986 const String8 name("virtual.test");
987
988 // --------------------------------------------------------------------
989 // Call Expectations
990
991 // The call should notify the interceptor that a display was created.
992 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
993
994 // --------------------------------------------------------------------
995 // Invocation
996
997 sp<IBinder> displayToken = mFlinger.createDisplay(name, true);
998
999 // --------------------------------------------------------------------
1000 // Postconditions
1001
1002 // The display should have been added to the current state
1003 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1004 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001005 EXPECT_TRUE(display.isVirtual());
1006 EXPECT_TRUE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -08001007 EXPECT_EQ(name.string(), display.displayName);
1008
1009 // --------------------------------------------------------------------
1010 // Cleanup conditions
1011
1012 // Destroying the display invalidates the display state.
1013 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1014}
1015
1016/* ------------------------------------------------------------------------
1017 * SurfaceFlinger::destroyDisplay
1018 */
1019
1020TEST_F(DisplayTransactionTest, destroyDisplayClearsCurrentStateForDisplay) {
1021 using Case = NonHwcVirtualDisplayCase;
1022
1023 // --------------------------------------------------------------------
1024 // Preconditions
1025
1026 // A virtual display exists
1027 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1028 existing.inject();
1029
1030 // --------------------------------------------------------------------
1031 // Call Expectations
1032
1033 // The call should notify the interceptor that a display was created.
1034 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
1035
1036 // Destroying the display invalidates the display state.
1037 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1038
1039 // --------------------------------------------------------------------
1040 // Invocation
1041
1042 mFlinger.destroyDisplay(existing.token());
1043
1044 // --------------------------------------------------------------------
1045 // Postconditions
1046
1047 // The display should have been removed from the current state
1048 EXPECT_FALSE(hasCurrentDisplayState(existing.token()));
1049
1050 // Ths display should still exist in the drawing state
1051 EXPECT_TRUE(hasDrawingDisplayState(existing.token()));
1052
1053 // The display transaction needed flasg should be set
1054 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1055}
1056
1057TEST_F(DisplayTransactionTest, destroyDisplayHandlesUnknownDisplay) {
1058 // --------------------------------------------------------------------
1059 // Preconditions
1060
1061 sp<BBinder> displayToken = new BBinder();
1062
1063 // --------------------------------------------------------------------
1064 // Invocation
1065
1066 mFlinger.destroyDisplay(displayToken);
1067}
1068
1069/* ------------------------------------------------------------------------
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001070 * SurfaceFlinger::resetDisplayState
1071 */
1072
1073TEST_F(DisplayTransactionTest, resetDisplayStateClearsState) {
1074 using Case = NonHwcVirtualDisplayCase;
1075
1076 // --------------------------------------------------------------------
1077 // Preconditions
1078
1079 // vsync is enabled and available
1080 mFlinger.mutablePrimaryHWVsyncEnabled() = true;
1081 mFlinger.mutableHWVsyncAvailable() = true;
1082
1083 // A display exists
1084 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1085 existing.inject();
1086
1087 // --------------------------------------------------------------------
1088 // Call Expectations
1089
1090 // The call disable vsyncs
1091 EXPECT_CALL(*mEventControlThread, setVsyncEnabled(false)).Times(1);
1092
Lloyd Pique41be5d22018-06-21 13:11:48 -07001093 // The call ends any display resyncs
1094 EXPECT_CALL(*mPrimaryDispSync, endResync()).Times(1);
1095
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001096 // --------------------------------------------------------------------
1097 // Invocation
1098
1099 mFlinger.resetDisplayState();
1100
1101 // --------------------------------------------------------------------
1102 // Postconditions
1103
1104 // vsyncs should be off and not available.
1105 EXPECT_FALSE(mFlinger.mutablePrimaryHWVsyncEnabled());
1106 EXPECT_FALSE(mFlinger.mutableHWVsyncAvailable());
1107
1108 // The display should have been removed from the display map.
1109 EXPECT_FALSE(hasDisplayDevice(existing.token()));
1110
1111 // The display should still exist in the current state
1112 EXPECT_TRUE(hasCurrentDisplayState(existing.token()));
1113
1114 // The display should have been removed from the drawing state
1115 EXPECT_FALSE(hasDrawingDisplayState(existing.token()));
1116}
1117
1118/* ------------------------------------------------------------------------
Valerie Hau9758ae02018-10-09 16:05:09 -07001119 * DisplayDevice::GetBestColorMode
1120 */
1121class GetBestColorModeTest : public DisplayTransactionTest {
1122public:
Dominik Laskowski34157762018-10-31 13:07:19 -07001123 static constexpr DisplayId DEFAULT_DISPLAY_ID = DisplayId{777};
Dominik Laskowski075d3172018-05-24 15:50:06 -07001124
Valerie Hau9758ae02018-10-09 16:05:09 -07001125 GetBestColorModeTest()
1126 : DisplayTransactionTest(),
Dominik Laskowski075d3172018-05-24 15:50:06 -07001127 mInjector(FakeDisplayDeviceInjector(mFlinger, DEFAULT_DISPLAY_ID, false /* isVirtual */,
1128 true /* isPrimary */)) {}
Valerie Hau9758ae02018-10-09 16:05:09 -07001129
1130 void setHasWideColorGamut(bool hasWideColorGamut) { mHasWideColorGamut = hasWideColorGamut; }
1131
1132 void addHwcColorModesMapping(ui::ColorMode colorMode,
1133 std::vector<ui::RenderIntent> renderIntents) {
1134 mHwcColorModes[colorMode] = renderIntents;
1135 }
1136
1137 void setInputDataspace(ui::Dataspace dataspace) { mInputDataspace = dataspace; }
1138
1139 void setInputRenderIntent(ui::RenderIntent renderIntent) { mInputRenderIntent = renderIntent; }
1140
1141 void getBestColorMode() {
1142 mInjector.setHwcColorModes(mHwcColorModes);
1143 mInjector.setHasWideColorGamut(mHasWideColorGamut);
Alec Mouriba013fa2018-10-16 12:43:11 -07001144 mInjector.setNativeWindow(mNativeWindow);
1145
1146 // Creating a DisplayDevice requires getting default dimensions from the
1147 // native window.
1148 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
1149 .WillRepeatedly(DoAll(SetArgPointee<1>(1080 /* arbitrary */), Return(0)));
1150 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
1151 .WillRepeatedly(DoAll(SetArgPointee<1>(1920 /* arbitrary */), Return(0)));
Alec Mouri4efb6c22018-11-16 13:07:47 -08001152 EXPECT_CALL(*mNativeWindow, perform(9)).Times(1);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001153 EXPECT_CALL(*mNativeWindow, perform(13)).Times(1);
Alec Mouri4efb6c22018-11-16 13:07:47 -08001154 EXPECT_CALL(*mNativeWindow, perform(30)).Times(1);
Valerie Hau9758ae02018-10-09 16:05:09 -07001155 auto displayDevice = mInjector.inject();
1156
1157 displayDevice->getBestColorMode(mInputDataspace, mInputRenderIntent, &mOutDataspace,
1158 &mOutColorMode, &mOutRenderIntent);
1159 }
1160
1161 ui::Dataspace mOutDataspace;
1162 ui::ColorMode mOutColorMode;
1163 ui::RenderIntent mOutRenderIntent;
1164
1165private:
1166 ui::Dataspace mInputDataspace;
1167 ui::RenderIntent mInputRenderIntent;
1168 bool mHasWideColorGamut = false;
1169 std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> mHwcColorModes;
1170 FakeDisplayDeviceInjector mInjector;
1171};
1172
1173TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeSRGB) {
1174 addHwcColorModesMapping(ui::ColorMode::SRGB,
1175 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1176 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1177 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1178 setHasWideColorGamut(true);
1179
1180 getBestColorMode();
1181
Peiyong Lin14724e62018-12-05 07:27:30 -08001182 ASSERT_EQ(ui::Dataspace::V0_SRGB, mOutDataspace);
Valerie Hau9758ae02018-10-09 16:05:09 -07001183 ASSERT_EQ(ui::ColorMode::SRGB, mOutColorMode);
1184 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1185}
1186
1187TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDisplayP3) {
1188 addHwcColorModesMapping(ui::ColorMode::DISPLAY_P3,
1189 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1190 addHwcColorModesMapping(ui::ColorMode::SRGB,
1191 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1192 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1193 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1194 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1195 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1196 setHasWideColorGamut(true);
1197
1198 getBestColorMode();
1199
1200 ASSERT_EQ(ui::Dataspace::DISPLAY_P3, mOutDataspace);
1201 ASSERT_EQ(ui::ColorMode::DISPLAY_P3, mOutColorMode);
1202 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1203}
1204
1205TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDISPLAY_BT2020) {
1206 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1207 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1208 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1209 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1210 setHasWideColorGamut(true);
1211
1212 getBestColorMode();
1213
1214 ASSERT_EQ(ui::Dataspace::DISPLAY_BT2020, mOutDataspace);
1215 ASSERT_EQ(ui::ColorMode::DISPLAY_BT2020, mOutColorMode);
1216 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1217}
1218
1219/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001220 * SurfaceFlinger::setupNewDisplayDeviceInternal
1221 */
1222
1223class SetupNewDisplayDeviceInternalTest : public DisplayTransactionTest {
1224public:
1225 template <typename T>
1226 void setupNewDisplayDeviceInternalTest();
1227};
1228
1229template <typename Case>
1230void SetupNewDisplayDeviceInternalTest::setupNewDisplayDeviceInternalTest() {
1231 const sp<BBinder> displayToken = new BBinder();
1232 const sp<mock::DisplaySurface> displaySurface = new mock::DisplaySurface();
1233 const sp<mock::GraphicBufferProducer> producer = new mock::GraphicBufferProducer();
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001234
1235 // --------------------------------------------------------------------
1236 // Preconditions
1237
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001238 // Wide color displays support is configured appropriately
1239 Case::WideColorSupport::injectConfigChange(this);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001240
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001241 // The display is setup with the HWC.
1242 Case::Display::injectHwcDisplay(this);
1243
1244 // SurfaceFlinger will use a test-controlled factory for native window
1245 // surfaces.
1246 injectFakeNativeWindowSurfaceFactory();
1247
1248 // --------------------------------------------------------------------
1249 // Call Expectations
1250
1251 // Various native window calls will be made.
1252 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
Lloyd Pique3c085a02018-05-09 19:38:32 -07001253 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001254 Case::WideColorSupport::setupComposerCallExpectations(this);
1255 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001256 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001257
1258 // --------------------------------------------------------------------
1259 // Invocation
1260
1261 DisplayDeviceState state;
Dominik Laskowski075d3172018-05-24 15:50:06 -07001262 state.displayId = static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1263 : Case::Display::DISPLAY_ID::get();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001264 state.isSecure = static_cast<bool>(Case::Display::SECURE);
1265
Dominik Laskowski075d3172018-05-24 15:50:06 -07001266 auto device =
1267 mFlinger.setupNewDisplayDeviceInternal(displayToken, Case::Display::DISPLAY_ID::get(),
1268 state, displaySurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001269
1270 // --------------------------------------------------------------------
1271 // Postconditions
1272
1273 ASSERT_TRUE(device != nullptr);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001274 EXPECT_EQ(Case::Display::DISPLAY_ID::get(), device->getId());
1275 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), device->isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001276 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001277 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001278 EXPECT_EQ(Case::Display::WIDTH, device->getWidth());
1279 EXPECT_EQ(Case::Display::HEIGHT, device->getHeight());
1280 EXPECT_EQ(Case::WideColorSupport::WIDE_COLOR_SUPPORTED, device->hasWideColorGamut());
Valerie Haue9e843a2018-12-18 13:39:23 -08001281 EXPECT_EQ(Case::HdrSupport::HDR10_PLUS_SUPPORTED, device->hasHDR10PlusSupport());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001282 EXPECT_EQ(Case::HdrSupport::HDR10_SUPPORTED, device->hasHDR10Support());
1283 EXPECT_EQ(Case::HdrSupport::HDR_HLG_SUPPORTED, device->hasHLGSupport());
1284 EXPECT_EQ(Case::HdrSupport::HDR_DOLBY_VISION_SUPPORTED, device->hasDolbyVisionSupport());
Lloyd Pique3c085a02018-05-09 19:38:32 -07001285 // Note: This is not Case::Display::HWC_ACTIVE_CONFIG_ID as the ids are
1286 // remapped, and the test only ever sets up one config. If there were an error
1287 // looking up the remapped index, device->getActiveConfig() would be -1 instead.
1288 EXPECT_EQ(0, device->getActiveConfig());
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001289 EXPECT_EQ(Case::PerFrameMetadataSupport::PER_FRAME_METADATA_KEYS,
1290 device->getSupportedPerFrameMetadata());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001291}
1292
1293TEST_F(SetupNewDisplayDeviceInternalTest, createSimplePrimaryDisplay) {
1294 setupNewDisplayDeviceInternalTest<SimplePrimaryDisplayCase>();
1295}
1296
1297TEST_F(SetupNewDisplayDeviceInternalTest, createSimpleExternalDisplay) {
1298 setupNewDisplayDeviceInternalTest<SimpleExternalDisplayCase>();
1299}
1300
1301TEST_F(SetupNewDisplayDeviceInternalTest, createNonHwcVirtualDisplay) {
1302 setupNewDisplayDeviceInternalTest<NonHwcVirtualDisplayCase>();
1303}
1304
1305TEST_F(SetupNewDisplayDeviceInternalTest, createHwcVirtualDisplay) {
Dominik Laskowski075d3172018-05-24 15:50:06 -07001306 using Case = HwcVirtualDisplayCase;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001307
Dominik Laskowski075d3172018-05-24 15:50:06 -07001308 // Insert display data so that the HWC thinks it created the virtual display.
1309 const auto displayId = Case::Display::DISPLAY_ID::get();
1310 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08001311 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001312
1313 setupNewDisplayDeviceInternalTest<Case>();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001314}
1315
1316TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3Display) {
1317 setupNewDisplayDeviceInternalTest<WideColorP3ColorimetricDisplayCase>();
1318}
1319
Valerie Haue9e843a2018-12-18 13:39:23 -08001320TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10PlusDisplay) {
1321 setupNewDisplayDeviceInternalTest<Hdr10PlusDisplayCase>();
1322}
1323
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001324TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10Display) {
1325 setupNewDisplayDeviceInternalTest<Hdr10DisplayCase>();
1326}
1327
1328TEST_F(SetupNewDisplayDeviceInternalTest, createHdrHlgDisplay) {
1329 setupNewDisplayDeviceInternalTest<HdrHlgDisplayCase>();
1330}
1331
1332TEST_F(SetupNewDisplayDeviceInternalTest, createHdrDolbyVisionDisplay) {
1333 setupNewDisplayDeviceInternalTest<HdrDolbyVisionDisplayCase>();
1334}
1335
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001336TEST_F(SetupNewDisplayDeviceInternalTest, createHdrSmpte2086DisplayCase) {
1337 setupNewDisplayDeviceInternalTest<HdrSmpte2086DisplayCase>();
1338}
1339
1340TEST_F(SetupNewDisplayDeviceInternalTest, createHdrCta816_3_DisplayCase) {
1341 setupNewDisplayDeviceInternalTest<HdrCta861_3_DisplayCase>();
1342}
1343
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001344/* ------------------------------------------------------------------------
1345 * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded)
1346 */
1347
1348class HandleTransactionLockedTest : public DisplayTransactionTest {
1349public:
1350 template <typename Case>
1351 void setupCommonPreconditions();
1352
1353 template <typename Case>
1354 void setupCommonCallExpectationsForConnectProcessing();
1355
1356 template <typename Case>
1357 void setupCommonCallExpectationsForDisconnectProcessing();
1358
1359 template <typename Case>
1360 void processesHotplugConnectCommon();
1361
1362 template <typename Case>
1363 void ignoresHotplugConnectCommon();
1364
1365 template <typename Case>
1366 void processesHotplugDisconnectCommon();
1367
1368 template <typename Case>
1369 void verifyDisplayIsConnected(const sp<IBinder>& displayToken);
1370
1371 template <typename Case>
1372 void verifyPhysicalDisplayIsConnected();
1373
1374 void verifyDisplayIsNotConnected(const sp<IBinder>& displayToken);
1375};
1376
1377template <typename Case>
1378void HandleTransactionLockedTest::setupCommonPreconditions() {
1379 // Wide color displays support is configured appropriately
1380 Case::WideColorSupport::injectConfigChange(this);
1381
1382 // SurfaceFlinger will use a test-controlled factory for BufferQueues
1383 injectFakeBufferQueueFactory();
1384
1385 // SurfaceFlinger will use a test-controlled factory for native window
1386 // surfaces.
1387 injectFakeNativeWindowSurfaceFactory();
1388}
1389
1390template <typename Case>
1391void HandleTransactionLockedTest::setupCommonCallExpectationsForConnectProcessing() {
1392 Case::Display::setupHwcHotplugCallExpectations(this);
1393
1394 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1395 Case::Display::setupFramebufferProducerBufferQueueCallExpectations(this);
1396 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1397 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
1398
1399 Case::WideColorSupport::setupComposerCallExpectations(this);
1400 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001401 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001402
1403 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001404 EXPECT_CALL(*mEventThread,
Dominik Laskowski075d3172018-05-24 15:50:06 -07001405 onHotplugReceived(static_cast<bool>(Case::Display::PRIMARY)
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001406 ? EventThread::DisplayType::Primary
1407 : EventThread::DisplayType::External,
1408 true))
1409 .Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001410}
1411
1412template <typename Case>
1413void HandleTransactionLockedTest::setupCommonCallExpectationsForDisconnectProcessing() {
1414 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001415 EXPECT_CALL(*mEventThread,
Dominik Laskowski075d3172018-05-24 15:50:06 -07001416 onHotplugReceived(static_cast<bool>(Case::Display::PRIMARY)
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001417 ? EventThread::DisplayType::Primary
1418 : EventThread::DisplayType::External,
1419 false))
1420 .Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001421}
1422
1423template <typename Case>
1424void HandleTransactionLockedTest::verifyDisplayIsConnected(const sp<IBinder>& displayToken) {
1425 // The display device should have been set up in the list of displays.
1426 ASSERT_TRUE(hasDisplayDevice(displayToken));
1427 const auto& device = getDisplayDevice(displayToken);
1428 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001429 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001430
1431 // The display should have been set up in the current display state
1432 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1433 const auto& current = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001434 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), current.isVirtual());
1435 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1436 : Case::Display::DISPLAY_ID::get(),
1437 current.displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001438
1439 // The display should have been set up in the drawing display state
1440 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1441 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001442 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
1443 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1444 : Case::Display::DISPLAY_ID::get(),
1445 draw.displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001446}
1447
1448template <typename Case>
1449void HandleTransactionLockedTest::verifyPhysicalDisplayIsConnected() {
1450 // HWComposer should have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001451 EXPECT_TRUE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001452
Dominik Laskowski075d3172018-05-24 15:50:06 -07001453 // SF should have a display token.
1454 const auto displayId = Case::Display::DISPLAY_ID::get();
1455 ASSERT_TRUE(displayId);
1456 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1457 auto& displayToken = mFlinger.mutablePhysicalDisplayTokens()[*displayId];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001458
1459 verifyDisplayIsConnected<Case>(displayToken);
1460}
1461
1462void HandleTransactionLockedTest::verifyDisplayIsNotConnected(const sp<IBinder>& displayToken) {
1463 EXPECT_FALSE(hasDisplayDevice(displayToken));
1464 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1465 EXPECT_FALSE(hasDrawingDisplayState(displayToken));
1466}
1467
1468template <typename Case>
1469void HandleTransactionLockedTest::processesHotplugConnectCommon() {
1470 // --------------------------------------------------------------------
1471 // Preconditions
1472
1473 setupCommonPreconditions<Case>();
1474
1475 // A hotplug connect event is enqueued for a display
1476 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001477
1478 // --------------------------------------------------------------------
1479 // Call Expectations
1480
1481 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillOnce(Return(false));
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001482
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001483 setupCommonCallExpectationsForConnectProcessing<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001484
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001485 // --------------------------------------------------------------------
1486 // Invocation
Lloyd Piquee39cad22017-12-20 17:01:29 -08001487
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001488 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
Lloyd Piquee39cad22017-12-20 17:01:29 -08001489
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001490 // --------------------------------------------------------------------
1491 // Postconditions
1492
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001493 verifyPhysicalDisplayIsConnected<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001494
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001495 // --------------------------------------------------------------------
1496 // Cleanup conditions
1497
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001498 EXPECT_CALL(*mComposer,
1499 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001500 .WillOnce(Return(Error::NONE));
1501 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Piquef58625d2017-12-19 13:22:33 -08001502}
1503
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001504template <typename Case>
1505void HandleTransactionLockedTest::ignoresHotplugConnectCommon() {
1506 // --------------------------------------------------------------------
1507 // Preconditions
1508
1509 setupCommonPreconditions<Case>();
1510
1511 // A hotplug connect event is enqueued for a display
1512 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1513
1514 // --------------------------------------------------------------------
1515 // Invocation
1516
1517 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1518
1519 // --------------------------------------------------------------------
1520 // Postconditions
1521
1522 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001523 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001524}
1525
1526template <typename Case>
1527void HandleTransactionLockedTest::processesHotplugDisconnectCommon() {
1528 // --------------------------------------------------------------------
1529 // Preconditions
1530
1531 setupCommonPreconditions<Case>();
1532
1533 // A hotplug disconnect event is enqueued for a display
1534 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1535
1536 // The display is already completely set up.
1537 Case::Display::injectHwcDisplay(this);
1538 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1539 existing.inject();
1540
1541 // --------------------------------------------------------------------
1542 // Call Expectations
1543
1544 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
Lloyd Pique438e9e72018-09-04 18:06:08 -07001545 EXPECT_CALL(*mComposer, getDisplayIdentificationData(Case::Display::HWC_DISPLAY_ID, _, _))
1546 .Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001547
1548 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1549
1550 // --------------------------------------------------------------------
1551 // Invocation
1552
1553 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1554
1555 // --------------------------------------------------------------------
1556 // Postconditions
1557
1558 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001559 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001560
Dominik Laskowski075d3172018-05-24 15:50:06 -07001561 // SF should not have a display token.
1562 const auto displayId = Case::Display::DISPLAY_ID::get();
1563 ASSERT_TRUE(displayId);
1564 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001565
1566 // The existing token should have been removed
1567 verifyDisplayIsNotConnected(existing.token());
1568}
1569
1570TEST_F(HandleTransactionLockedTest, processesHotplugConnectPrimaryDisplay) {
1571 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1572}
1573
1574TEST_F(HandleTransactionLockedTest,
1575 processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected) {
1576 // Inject an external display.
1577 ExternalDisplayVariant::injectHwcDisplay(this);
1578
1579 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1580}
1581
1582TEST_F(HandleTransactionLockedTest, processesHotplugConnectExternalDisplay) {
1583 // Inject a primary display.
1584 PrimaryDisplayVariant::injectHwcDisplay(this);
1585
1586 processesHotplugConnectCommon<SimpleExternalDisplayCase>();
1587}
1588
1589TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected) {
1590 // Inject both a primary and external display.
1591 PrimaryDisplayVariant::injectHwcDisplay(this);
1592 ExternalDisplayVariant::injectHwcDisplay(this);
1593
1594 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1595
1596 ignoresHotplugConnectCommon<SimpleTertiaryDisplayCase>();
1597}
1598
1599TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfExternalForVrComposer) {
1600 // Inject a primary display.
1601 PrimaryDisplayVariant::injectHwcDisplay(this);
1602
1603 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(true));
1604
1605 ignoresHotplugConnectCommon<SimpleExternalDisplayCase>();
1606}
1607
1608TEST_F(HandleTransactionLockedTest, processHotplugDisconnectPrimaryDisplay) {
1609 processesHotplugDisconnectCommon<SimplePrimaryDisplayCase>();
1610}
1611
1612TEST_F(HandleTransactionLockedTest, processHotplugDisconnectExternalDisplay) {
1613 processesHotplugDisconnectCommon<SimpleExternalDisplayCase>();
1614}
1615
1616TEST_F(HandleTransactionLockedTest, processesHotplugConnectThenDisconnectPrimary) {
1617 using Case = SimplePrimaryDisplayCase;
1618
1619 // --------------------------------------------------------------------
1620 // Preconditions
1621
1622 setupCommonPreconditions<Case>();
1623
1624 // A hotplug connect event is enqueued for a display
1625 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1626 // A hotplug disconnect event is also enqueued for the same display
1627 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1628
1629 // --------------------------------------------------------------------
1630 // Call Expectations
1631
1632 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1633
1634 setupCommonCallExpectationsForConnectProcessing<Case>();
1635 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1636
1637 EXPECT_CALL(*mComposer,
1638 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1639 .WillOnce(Return(Error::NONE));
1640 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1641
1642 // --------------------------------------------------------------------
1643 // Invocation
1644
1645 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1646
1647 // --------------------------------------------------------------------
1648 // Postconditions
1649
1650 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001651 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001652
Dominik Laskowski075d3172018-05-24 15:50:06 -07001653 // SF should not have a display token.
1654 const auto displayId = Case::Display::DISPLAY_ID::get();
1655 ASSERT_TRUE(displayId);
1656 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001657}
1658
1659TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectThenConnectPrimary) {
1660 using Case = SimplePrimaryDisplayCase;
1661
1662 // --------------------------------------------------------------------
1663 // Preconditions
1664
1665 setupCommonPreconditions<Case>();
1666
1667 // The display is already completely set up.
1668 Case::Display::injectHwcDisplay(this);
1669 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1670 existing.inject();
1671
1672 // A hotplug disconnect event is enqueued for a display
1673 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1674 // A hotplug connect event is also enqueued for the same display
1675 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1676
1677 // --------------------------------------------------------------------
1678 // Call Expectations
1679
1680 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1681
1682 setupCommonCallExpectationsForConnectProcessing<Case>();
1683 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1684
1685 // --------------------------------------------------------------------
1686 // Invocation
1687
1688 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1689
1690 // --------------------------------------------------------------------
1691 // Postconditions
1692
1693 // The existing token should have been removed
1694 verifyDisplayIsNotConnected(existing.token());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001695 const auto displayId = Case::Display::DISPLAY_ID::get();
1696 ASSERT_TRUE(displayId);
1697 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1698 EXPECT_NE(existing.token(), mFlinger.mutablePhysicalDisplayTokens()[*displayId]);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001699
1700 // A new display should be connected in its place
1701
1702 verifyPhysicalDisplayIsConnected<Case>();
1703
1704 // --------------------------------------------------------------------
1705 // Cleanup conditions
1706
1707 EXPECT_CALL(*mComposer,
1708 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1709 .WillOnce(Return(Error::NONE));
1710 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1711}
1712
1713TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAdded) {
1714 using Case = HwcVirtualDisplayCase;
1715
1716 // --------------------------------------------------------------------
1717 // Preconditions
1718
1719 // The HWC supports at least one virtual display
1720 injectMockComposer(1);
1721
1722 setupCommonPreconditions<Case>();
1723
1724 // A virtual display was added to the current state, and it has a
1725 // surface(producer)
1726 sp<BBinder> displayToken = new BBinder();
Lloyd Pique4c2ac022018-04-27 12:08:03 -07001727
Dominik Laskowski075d3172018-05-24 15:50:06 -07001728 DisplayDeviceState state;
1729 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001730
1731 sp<mock::GraphicBufferProducer> surface{new mock::GraphicBufferProducer()};
Dominik Laskowski075d3172018-05-24 15:50:06 -07001732 state.surface = surface;
1733 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001734
1735 // --------------------------------------------------------------------
1736 // Call Expectations
1737
1738 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1739 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1740
1741 EXPECT_CALL(*surface, query(NATIVE_WINDOW_WIDTH, _))
1742 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::WIDTH), Return(NO_ERROR)));
1743 EXPECT_CALL(*surface, query(NATIVE_WINDOW_HEIGHT, _))
1744 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::HEIGHT), Return(NO_ERROR)));
1745 EXPECT_CALL(*surface, query(NATIVE_WINDOW_FORMAT, _))
1746 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT),
1747 Return(NO_ERROR)));
1748 EXPECT_CALL(*surface, query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, _))
1749 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(NO_ERROR)));
1750
1751 EXPECT_CALL(*surface, setAsyncMode(true)).Times(1);
1752
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001753 EXPECT_CALL(*mProducer, disconnect(_, _)).Times(1);
1754
1755 Case::Display::setupHwcVirtualDisplayCreationCallExpectations(this);
1756 Case::WideColorSupport::setupComposerCallExpectations(this);
1757 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001758 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001759
1760 // --------------------------------------------------------------------
1761 // Invocation
1762
1763 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1764
1765 // --------------------------------------------------------------------
1766 // Postconditions
1767
1768 // The display device should have been set up in the list of displays.
1769 verifyDisplayIsConnected<Case>(displayToken);
1770
1771 // --------------------------------------------------------------------
1772 // Cleanup conditions
1773
1774 EXPECT_CALL(*mComposer, destroyVirtualDisplay(Case::Display::HWC_DISPLAY_ID))
1775 .WillOnce(Return(Error::NONE));
1776 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1777}
1778
1779TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAddedWithNoSurface) {
1780 using Case = HwcVirtualDisplayCase;
1781
1782 // --------------------------------------------------------------------
1783 // Preconditions
1784
1785 // The HWC supports at least one virtual display
1786 injectMockComposer(1);
1787
1788 setupCommonPreconditions<Case>();
1789
1790 // A virtual display was added to the current state, but it does not have a
1791 // surface.
1792 sp<BBinder> displayToken = new BBinder();
1793
Dominik Laskowski075d3172018-05-24 15:50:06 -07001794 DisplayDeviceState state;
1795 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001796
Dominik Laskowski075d3172018-05-24 15:50:06 -07001797 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001798
1799 // --------------------------------------------------------------------
1800 // Call Expectations
1801
1802 // --------------------------------------------------------------------
1803 // Invocation
1804
1805 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1806
1807 // --------------------------------------------------------------------
1808 // Postconditions
1809
1810 // There will not be a display device set up.
1811 EXPECT_FALSE(hasDisplayDevice(displayToken));
1812
1813 // The drawing display state will be set from the current display state.
1814 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1815 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001816 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001817}
1818
1819TEST_F(HandleTransactionLockedTest, processesVirtualDisplayRemoval) {
1820 using Case = HwcVirtualDisplayCase;
1821
1822 // --------------------------------------------------------------------
1823 // Preconditions
1824
1825 // A virtual display is set up but is removed from the current state.
Dominik Laskowski075d3172018-05-24 15:50:06 -07001826 const auto displayId = Case::Display::DISPLAY_ID::get();
1827 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08001828 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001829 Case::Display::injectHwcDisplay(this);
1830 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1831 existing.inject();
1832 mFlinger.mutableCurrentState().displays.removeItem(existing.token());
1833
1834 // --------------------------------------------------------------------
1835 // Call Expectations
1836
1837 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1838
1839 // --------------------------------------------------------------------
1840 // Invocation
1841
1842 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1843
1844 // --------------------------------------------------------------------
1845 // Postconditions
1846
1847 // The existing token should have been removed
1848 verifyDisplayIsNotConnected(existing.token());
1849}
1850
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001851TEST_F(HandleTransactionLockedTest, processesDisplayLayerStackChanges) {
1852 using Case = NonHwcVirtualDisplayCase;
1853
1854 constexpr uint32_t oldLayerStack = 0u;
1855 constexpr uint32_t newLayerStack = 123u;
1856
1857 // --------------------------------------------------------------------
1858 // Preconditions
1859
1860 // A display is set up
1861 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1862 display.inject();
1863
1864 // There is a change to the layerStack state
1865 display.mutableDrawingDisplayState().layerStack = oldLayerStack;
1866 display.mutableCurrentDisplayState().layerStack = newLayerStack;
1867
1868 // --------------------------------------------------------------------
1869 // Invocation
1870
1871 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1872
1873 // --------------------------------------------------------------------
1874 // Postconditions
1875
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001876 EXPECT_EQ(newLayerStack, display.mutableDisplayDevice()->getLayerStack());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001877}
1878
1879TEST_F(HandleTransactionLockedTest, processesDisplayTransformChanges) {
1880 using Case = NonHwcVirtualDisplayCase;
1881
1882 constexpr int oldTransform = 0;
1883 constexpr int newTransform = 2;
1884
1885 // --------------------------------------------------------------------
1886 // Preconditions
1887
1888 // A display is set up
1889 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1890 display.inject();
1891
1892 // There is a change to the orientation state
1893 display.mutableDrawingDisplayState().orientation = oldTransform;
1894 display.mutableCurrentDisplayState().orientation = newTransform;
1895
1896 // --------------------------------------------------------------------
1897 // Invocation
1898
1899 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1900
1901 // --------------------------------------------------------------------
1902 // Postconditions
1903
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001904 EXPECT_EQ(newTransform, display.mutableDisplayDevice()->getOrientation());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001905}
1906
1907TEST_F(HandleTransactionLockedTest, processesDisplayViewportChanges) {
1908 using Case = NonHwcVirtualDisplayCase;
1909
1910 const Rect oldViewport(0, 0, 0, 0);
1911 const Rect newViewport(0, 0, 123, 456);
1912
1913 // --------------------------------------------------------------------
1914 // Preconditions
1915
1916 // A display is set up
1917 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1918 display.inject();
1919
1920 // There is a change to the viewport state
1921 display.mutableDrawingDisplayState().viewport = oldViewport;
1922 display.mutableCurrentDisplayState().viewport = newViewport;
1923
1924 // --------------------------------------------------------------------
1925 // Invocation
1926
1927 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1928
1929 // --------------------------------------------------------------------
1930 // Postconditions
1931
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001932 EXPECT_EQ(newViewport, display.mutableDisplayDevice()->getViewport());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001933}
1934
1935TEST_F(HandleTransactionLockedTest, processesDisplayFrameChanges) {
1936 using Case = NonHwcVirtualDisplayCase;
1937
1938 const Rect oldFrame(0, 0, 0, 0);
1939 const Rect newFrame(0, 0, 123, 456);
1940
1941 // --------------------------------------------------------------------
1942 // Preconditions
1943
1944 // A display is set up
1945 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1946 display.inject();
1947
1948 // There is a change to the viewport state
1949 display.mutableDrawingDisplayState().frame = oldFrame;
1950 display.mutableCurrentDisplayState().frame = newFrame;
1951
1952 // --------------------------------------------------------------------
1953 // Invocation
1954
1955 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1956
1957 // --------------------------------------------------------------------
1958 // Postconditions
1959
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001960 EXPECT_EQ(newFrame, display.mutableDisplayDevice()->getFrame());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001961}
1962
1963TEST_F(HandleTransactionLockedTest, processesDisplayWidthChanges) {
1964 using Case = NonHwcVirtualDisplayCase;
1965
1966 constexpr int oldWidth = 0;
1967 constexpr int oldHeight = 10;
1968 constexpr int newWidth = 123;
1969
1970 // --------------------------------------------------------------------
1971 // Preconditions
1972
1973 // A display is set up
1974 auto nativeWindow = new mock::NativeWindow();
1975 auto displaySurface = new mock::DisplaySurface();
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001976 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001977 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1978 display.setNativeWindow(nativeWindow);
1979 display.setDisplaySurface(displaySurface);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001980 // Setup injection expections
1981 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
1982 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
1983 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
1984 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
Alec Mouri4efb6c22018-11-16 13:07:47 -08001985 EXPECT_CALL(*nativeWindow, perform(9)).Times(1);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001986 EXPECT_CALL(*nativeWindow, perform(13)).Times(1);
Alec Mouri4efb6c22018-11-16 13:07:47 -08001987 EXPECT_CALL(*nativeWindow, perform(30)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001988 display.inject();
1989
1990 // There is a change to the viewport state
1991 display.mutableDrawingDisplayState().width = oldWidth;
1992 display.mutableDrawingDisplayState().height = oldHeight;
1993 display.mutableCurrentDisplayState().width = newWidth;
1994 display.mutableCurrentDisplayState().height = oldHeight;
1995
1996 // --------------------------------------------------------------------
1997 // Call Expectations
1998
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001999 EXPECT_CALL(*displaySurface, resizeBuffers(newWidth, oldHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002000
2001 // --------------------------------------------------------------------
2002 // Invocation
2003
2004 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2005}
2006
2007TEST_F(HandleTransactionLockedTest, processesDisplayHeightChanges) {
2008 using Case = NonHwcVirtualDisplayCase;
2009
2010 constexpr int oldWidth = 0;
2011 constexpr int oldHeight = 10;
2012 constexpr int newHeight = 123;
2013
2014 // --------------------------------------------------------------------
2015 // Preconditions
2016
2017 // A display is set up
2018 auto nativeWindow = new mock::NativeWindow();
2019 auto displaySurface = new mock::DisplaySurface();
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002020 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002021 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2022 display.setNativeWindow(nativeWindow);
2023 display.setDisplaySurface(displaySurface);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002024 // Setup injection expections
2025 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
2026 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
2027 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
2028 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
Alec Mouri4efb6c22018-11-16 13:07:47 -08002029 EXPECT_CALL(*nativeWindow, perform(9)).Times(1);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002030 EXPECT_CALL(*nativeWindow, perform(13)).Times(1);
Alec Mouri4efb6c22018-11-16 13:07:47 -08002031 EXPECT_CALL(*nativeWindow, perform(30)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002032 display.inject();
2033
2034 // There is a change to the viewport state
2035 display.mutableDrawingDisplayState().width = oldWidth;
2036 display.mutableDrawingDisplayState().height = oldHeight;
2037 display.mutableCurrentDisplayState().width = oldWidth;
2038 display.mutableCurrentDisplayState().height = newHeight;
2039
2040 // --------------------------------------------------------------------
2041 // Call Expectations
2042
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002043 EXPECT_CALL(*displaySurface, resizeBuffers(oldWidth, newHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002044
2045 // --------------------------------------------------------------------
2046 // Invocation
2047
2048 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2049}
2050
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002051/* ------------------------------------------------------------------------
2052 * SurfaceFlinger::setDisplayStateLocked
2053 */
2054
2055TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithUnknownDisplay) {
2056 // --------------------------------------------------------------------
2057 // Preconditions
2058
2059 // We have an unknown display token not associated with a known display
2060 sp<BBinder> displayToken = new BBinder();
2061
2062 // The requested display state references the unknown display.
2063 DisplayState state;
2064 state.what = DisplayState::eLayerStackChanged;
2065 state.token = displayToken;
2066 state.layerStack = 456;
2067
2068 // --------------------------------------------------------------------
2069 // Invocation
2070
2071 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2072
2073 // --------------------------------------------------------------------
2074 // Postconditions
2075
2076 // The returned flags are empty
2077 EXPECT_EQ(0u, flags);
2078
2079 // The display token still doesn't match anything known.
2080 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
2081}
2082
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002083TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWhenNoChanges) {
2084 using Case = SimplePrimaryDisplayCase;
2085
2086 // --------------------------------------------------------------------
2087 // Preconditions
2088
2089 // A display is already set up
2090 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2091 display.inject();
2092
2093 // No changes are made to the display
2094 DisplayState state;
2095 state.what = 0;
2096 state.token = display.token();
2097
2098 // --------------------------------------------------------------------
2099 // Invocation
2100
2101 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2102
2103 // --------------------------------------------------------------------
2104 // Postconditions
2105
2106 // The returned flags are empty
2107 EXPECT_EQ(0u, flags);
2108}
2109
2110TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSurfaceDidNotChange) {
2111 using Case = SimplePrimaryDisplayCase;
2112
2113 // --------------------------------------------------------------------
2114 // Preconditions
2115
2116 // A display is already set up
2117 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2118 display.inject();
2119
2120 // There is a surface that can be set.
2121 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2122
2123 // The current display state has the surface set
2124 display.mutableCurrentDisplayState().surface = surface;
2125
2126 // The incoming request sets the same surface
2127 DisplayState state;
2128 state.what = DisplayState::eSurfaceChanged;
2129 state.token = display.token();
2130 state.surface = surface;
2131
2132 // --------------------------------------------------------------------
2133 // Invocation
2134
2135 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2136
2137 // --------------------------------------------------------------------
2138 // Postconditions
2139
2140 // The returned flags are empty
2141 EXPECT_EQ(0u, flags);
2142
2143 // The current display state is unchanged.
2144 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2145}
2146
2147TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfSurfaceChanged) {
2148 using Case = SimplePrimaryDisplayCase;
2149
2150 // --------------------------------------------------------------------
2151 // Preconditions
2152
2153 // A display is already set up
2154 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2155 display.inject();
2156
2157 // There is a surface that can be set.
2158 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2159
2160 // The current display state does not have a surface
2161 display.mutableCurrentDisplayState().surface = nullptr;
2162
2163 // The incoming request sets a surface
2164 DisplayState state;
2165 state.what = DisplayState::eSurfaceChanged;
2166 state.token = display.token();
2167 state.surface = surface;
2168
2169 // --------------------------------------------------------------------
2170 // Invocation
2171
2172 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2173
2174 // --------------------------------------------------------------------
2175 // Postconditions
2176
2177 // The returned flags indicate a transaction is needed
2178 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2179
2180 // The current display layer stack state is set to the new value
2181 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2182}
2183
2184TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfLayerStackDidNotChange) {
2185 using Case = SimplePrimaryDisplayCase;
2186
2187 // --------------------------------------------------------------------
2188 // Preconditions
2189
2190 // A display is already set up
2191 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2192 display.inject();
2193
2194 // The display has a layer stack set
2195 display.mutableCurrentDisplayState().layerStack = 456u;
2196
2197 // The incoming request sets the same layer stack
2198 DisplayState state;
2199 state.what = DisplayState::eLayerStackChanged;
2200 state.token = display.token();
2201 state.layerStack = 456u;
2202
2203 // --------------------------------------------------------------------
2204 // Invocation
2205
2206 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2207
2208 // --------------------------------------------------------------------
2209 // Postconditions
2210
2211 // The returned flags are empty
2212 EXPECT_EQ(0u, flags);
2213
2214 // The current display state is unchanged
2215 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2216}
2217
2218TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfLayerStackChanged) {
2219 using Case = SimplePrimaryDisplayCase;
2220
2221 // --------------------------------------------------------------------
2222 // Preconditions
2223
2224 // A display is set up
2225 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2226 display.inject();
2227
2228 // The display has a layer stack set
2229 display.mutableCurrentDisplayState().layerStack = 654u;
2230
2231 // The incoming request sets a different layer stack
2232 DisplayState state;
2233 state.what = DisplayState::eLayerStackChanged;
2234 state.token = display.token();
2235 state.layerStack = 456u;
2236
2237 // --------------------------------------------------------------------
2238 // Invocation
2239
2240 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2241
2242 // --------------------------------------------------------------------
2243 // Postconditions
2244
2245 // The returned flags indicate a transaction is needed
2246 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2247
2248 // The desired display state has been set to the new value.
2249 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2250}
2251
2252TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfProjectionDidNotChange) {
2253 using Case = SimplePrimaryDisplayCase;
2254 constexpr int initialOrientation = 180;
2255 const Rect initialFrame = {1, 2, 3, 4};
2256 const Rect initialViewport = {5, 6, 7, 8};
2257
2258 // --------------------------------------------------------------------
2259 // Preconditions
2260
2261 // A display is set up
2262 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2263 display.inject();
2264
2265 // The current display state projection state is all set
2266 display.mutableCurrentDisplayState().orientation = initialOrientation;
2267 display.mutableCurrentDisplayState().frame = initialFrame;
2268 display.mutableCurrentDisplayState().viewport = initialViewport;
2269
2270 // The incoming request sets the same projection state
2271 DisplayState state;
2272 state.what = DisplayState::eDisplayProjectionChanged;
2273 state.token = display.token();
2274 state.orientation = initialOrientation;
2275 state.frame = initialFrame;
2276 state.viewport = initialViewport;
2277
2278 // --------------------------------------------------------------------
2279 // Invocation
2280
2281 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2282
2283 // --------------------------------------------------------------------
2284 // Postconditions
2285
2286 // The returned flags are empty
2287 EXPECT_EQ(0u, flags);
2288
2289 // The current display state is unchanged
2290 EXPECT_EQ(initialOrientation, display.getCurrentDisplayState().orientation);
2291
2292 EXPECT_EQ(initialFrame, display.getCurrentDisplayState().frame);
2293 EXPECT_EQ(initialViewport, display.getCurrentDisplayState().viewport);
2294}
2295
2296TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfOrientationChanged) {
2297 using Case = SimplePrimaryDisplayCase;
2298 constexpr int initialOrientation = 90;
2299 constexpr int desiredOrientation = 180;
2300
2301 // --------------------------------------------------------------------
2302 // Preconditions
2303
2304 // A display is set up
2305 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2306 display.inject();
2307
2308 // The current display state has an orientation set
2309 display.mutableCurrentDisplayState().orientation = initialOrientation;
2310
2311 // The incoming request sets a different orientation
2312 DisplayState state;
2313 state.what = DisplayState::eDisplayProjectionChanged;
2314 state.token = display.token();
2315 state.orientation = desiredOrientation;
2316
2317 // --------------------------------------------------------------------
2318 // Invocation
2319
2320 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2321
2322 // --------------------------------------------------------------------
2323 // Postconditions
2324
2325 // The returned flags indicate a transaction is needed
2326 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2327
2328 // The current display state has the new value.
2329 EXPECT_EQ(desiredOrientation, display.getCurrentDisplayState().orientation);
2330}
2331
2332TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfFrameChanged) {
2333 using Case = SimplePrimaryDisplayCase;
2334 const Rect initialFrame = {0, 0, 0, 0};
2335 const Rect desiredFrame = {5, 6, 7, 8};
2336
2337 // --------------------------------------------------------------------
2338 // Preconditions
2339
2340 // A display is set up
2341 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2342 display.inject();
2343
2344 // The current display state does not have a frame
2345 display.mutableCurrentDisplayState().frame = initialFrame;
2346
2347 // The incoming request sets a frame
2348 DisplayState state;
2349 state.what = DisplayState::eDisplayProjectionChanged;
2350 state.token = display.token();
2351 state.frame = desiredFrame;
2352
2353 // --------------------------------------------------------------------
2354 // Invocation
2355
2356 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2357
2358 // --------------------------------------------------------------------
2359 // Postconditions
2360
2361 // The returned flags indicate a transaction is needed
2362 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2363
2364 // The current display state has the new value.
2365 EXPECT_EQ(desiredFrame, display.getCurrentDisplayState().frame);
2366}
2367
2368TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfViewportChanged) {
2369 using Case = SimplePrimaryDisplayCase;
2370 const Rect initialViewport = {0, 0, 0, 0};
2371 const Rect desiredViewport = {5, 6, 7, 8};
2372
2373 // --------------------------------------------------------------------
2374 // Preconditions
2375
2376 // A display is set up
2377 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2378 display.inject();
2379
2380 // The current display state does not have a viewport
2381 display.mutableCurrentDisplayState().viewport = initialViewport;
2382
2383 // The incoming request sets a viewport
2384 DisplayState state;
2385 state.what = DisplayState::eDisplayProjectionChanged;
2386 state.token = display.token();
2387 state.viewport = desiredViewport;
2388
2389 // --------------------------------------------------------------------
2390 // Invocation
2391
2392 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2393
2394 // --------------------------------------------------------------------
2395 // Postconditions
2396
2397 // The returned flags indicate a transaction is needed
2398 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2399
2400 // The current display state has the new value.
2401 EXPECT_EQ(desiredViewport, display.getCurrentDisplayState().viewport);
2402}
2403
2404TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSizeDidNotChange) {
2405 using Case = SimplePrimaryDisplayCase;
2406 constexpr uint32_t initialWidth = 1024;
2407 constexpr uint32_t initialHeight = 768;
2408
2409 // --------------------------------------------------------------------
2410 // Preconditions
2411
2412 // A display is set up
2413 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2414 display.inject();
2415
2416 // The current display state has a size set
2417 display.mutableCurrentDisplayState().width = initialWidth;
2418 display.mutableCurrentDisplayState().height = initialHeight;
2419
2420 // The incoming request sets the same display size
2421 DisplayState state;
2422 state.what = DisplayState::eDisplaySizeChanged;
2423 state.token = display.token();
2424 state.width = initialWidth;
2425 state.height = initialHeight;
2426
2427 // --------------------------------------------------------------------
2428 // Invocation
2429
2430 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2431
2432 // --------------------------------------------------------------------
2433 // Postconditions
2434
2435 // The returned flags are empty
2436 EXPECT_EQ(0u, flags);
2437
2438 // The current display state is unchanged
2439 EXPECT_EQ(initialWidth, display.getCurrentDisplayState().width);
2440 EXPECT_EQ(initialHeight, display.getCurrentDisplayState().height);
2441}
2442
2443TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfWidthChanged) {
2444 using Case = SimplePrimaryDisplayCase;
2445 constexpr uint32_t initialWidth = 0;
2446 constexpr uint32_t desiredWidth = 1024;
2447
2448 // --------------------------------------------------------------------
2449 // Preconditions
2450
2451 // A display is set up
2452 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2453 display.inject();
2454
2455 // The display does not yet have a width
2456 display.mutableCurrentDisplayState().width = initialWidth;
2457
2458 // The incoming request sets a display width
2459 DisplayState state;
2460 state.what = DisplayState::eDisplaySizeChanged;
2461 state.token = display.token();
2462 state.width = desiredWidth;
2463
2464 // --------------------------------------------------------------------
2465 // Invocation
2466
2467 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2468
2469 // --------------------------------------------------------------------
2470 // Postconditions
2471
2472 // The returned flags indicate a transaction is needed
2473 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2474
2475 // The current display state has the new value.
2476 EXPECT_EQ(desiredWidth, display.getCurrentDisplayState().width);
2477}
2478
2479TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfHeightChanged) {
2480 using Case = SimplePrimaryDisplayCase;
2481 constexpr uint32_t initialHeight = 0;
2482 constexpr uint32_t desiredHeight = 768;
2483
2484 // --------------------------------------------------------------------
2485 // Preconditions
2486
2487 // A display is set up
2488 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2489 display.inject();
2490
2491 // The display does not yet have a height
2492 display.mutableCurrentDisplayState().height = initialHeight;
2493
2494 // The incoming request sets a display height
2495 DisplayState state;
2496 state.what = DisplayState::eDisplaySizeChanged;
2497 state.token = display.token();
2498 state.height = desiredHeight;
2499
2500 // --------------------------------------------------------------------
2501 // Invocation
2502
2503 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2504
2505 // --------------------------------------------------------------------
2506 // Postconditions
2507
2508 // The returned flags indicate a transaction is needed
2509 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2510
2511 // The current display state has the new value.
2512 EXPECT_EQ(desiredHeight, display.getCurrentDisplayState().height);
2513}
2514
Lloyd Pique86016da2018-03-01 16:09:38 -08002515/* ------------------------------------------------------------------------
2516 * SurfaceFlinger::onInitializeDisplays
2517 */
2518
2519TEST_F(DisplayTransactionTest, onInitializeDisplaysSetsUpPrimaryDisplay) {
2520 using Case = SimplePrimaryDisplayCase;
2521
2522 // --------------------------------------------------------------------
2523 // Preconditions
2524
2525 // A primary display is set up
2526 Case::Display::injectHwcDisplay(this);
2527 auto primaryDisplay = Case::Display::makeFakeExistingDisplayInjector(this);
2528 primaryDisplay.inject();
2529
2530 // --------------------------------------------------------------------
2531 // Call Expectations
2532
2533 // We expect the surface interceptor to possibly be used, but we treat it as
2534 // disabled since it is called as a side effect rather than directly by this
2535 // function.
2536 EXPECT_CALL(*mSurfaceInterceptor, isEnabled()).WillOnce(Return(false));
2537
2538 // We expect a call to get the active display config.
2539 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
2540
2541 // We expect invalidate() to be invoked once to trigger display transaction
2542 // processing.
2543 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
2544
2545 // --------------------------------------------------------------------
2546 // Invocation
2547
2548 mFlinger.onInitializeDisplays();
2549
2550 // --------------------------------------------------------------------
2551 // Postconditions
2552
2553 // The primary display should have a current state
2554 ASSERT_TRUE(hasCurrentDisplayState(primaryDisplay.token()));
2555 const auto& primaryDisplayState = getCurrentDisplayState(primaryDisplay.token());
2556 // The layer stack state should be set to zero
2557 EXPECT_EQ(0u, primaryDisplayState.layerStack);
2558 // The orientation state should be set to zero
2559 EXPECT_EQ(0, primaryDisplayState.orientation);
2560
2561 // The frame state should be set to INVALID
2562 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.frame);
2563
2564 // The viewport state should be set to INVALID
2565 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.viewport);
2566
2567 // The width and height should both be zero
2568 EXPECT_EQ(0u, primaryDisplayState.width);
2569 EXPECT_EQ(0u, primaryDisplayState.height);
2570
2571 // The display should be set to HWC_POWER_MODE_NORMAL
2572 ASSERT_TRUE(hasDisplayDevice(primaryDisplay.token()));
2573 auto displayDevice = primaryDisplay.mutableDisplayDevice();
2574 EXPECT_EQ(HWC_POWER_MODE_NORMAL, displayDevice->getPowerMode());
2575
2576 // The display refresh period should be set in the frame tracker.
2577 FrameStats stats;
2578 mFlinger.getAnimFrameTracker().getStats(&stats);
2579 EXPECT_EQ(DEFAULT_REFRESH_RATE, stats.refreshPeriodNano);
2580
2581 // The display transaction needed flag should be set.
2582 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
2583
2584 // The compositor timing should be set to default values
2585 const auto& compositorTiming = mFlinger.getCompositorTiming();
2586 EXPECT_EQ(-DEFAULT_REFRESH_RATE, compositorTiming.deadline);
2587 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.interval);
2588 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.presentLatency);
2589}
2590
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002591/* ------------------------------------------------------------------------
2592 * SurfaceFlinger::setPowerModeInternal
2593 */
2594
2595// Used when we simulate a display that supports doze.
Peiyong Lined531a32018-10-26 18:27:56 -07002596template <typename Display>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002597struct DozeIsSupportedVariant {
2598 static constexpr bool DOZE_SUPPORTED = true;
2599 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2600 IComposerClient::PowerMode::DOZE;
2601 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2602 IComposerClient::PowerMode::DOZE_SUSPEND;
Peiyong Lined531a32018-10-26 18:27:56 -07002603
2604 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
2605 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(Display::HWC_DISPLAY_ID, _))
2606 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hwc2::DisplayCapability>(
2607 {Hwc2::DisplayCapability::DOZE})),
2608 Return(Error::NONE)));
2609 }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002610};
2611
Peiyong Lined531a32018-10-26 18:27:56 -07002612template <typename Display>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002613// Used when we simulate a display that does not support doze.
2614struct DozeNotSupportedVariant {
2615 static constexpr bool DOZE_SUPPORTED = false;
2616 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2617 IComposerClient::PowerMode::ON;
2618 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2619 IComposerClient::PowerMode::ON;
Peiyong Lined531a32018-10-26 18:27:56 -07002620
2621 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
2622 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(Display::HWC_DISPLAY_ID, _))
2623 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hwc2::DisplayCapability>({})),
2624 Return(Error::NONE)));
2625 }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002626};
2627
2628struct EventThreadBaseSupportedVariant {
2629 static void setupEventAndEventControlThreadNoCallExpectations(DisplayTransactionTest* test) {
2630 // The event control thread should not be notified.
2631 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(_)).Times(0);
2632
2633 // The event thread should not be notified.
2634 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(0);
2635 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(0);
2636 }
2637};
2638
2639struct EventThreadNotSupportedVariant : public EventThreadBaseSupportedVariant {
2640 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2641 // These calls are only expected for the primary display.
2642
2643 // Instead expect no calls.
2644 setupEventAndEventControlThreadNoCallExpectations(test);
2645 }
2646
2647 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2648 // These calls are only expected for the primary display.
2649
2650 // Instead expect no calls.
2651 setupEventAndEventControlThreadNoCallExpectations(test);
2652 }
2653};
2654
2655struct EventThreadIsSupportedVariant : public EventThreadBaseSupportedVariant {
2656 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2657 // The event control thread should be notified to enable vsyncs
2658 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(true)).Times(1);
2659
2660 // The event thread should be notified that the screen was acquired.
2661 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(1);
2662 }
2663
2664 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2665 // There should be a call to setVsyncEnabled(false)
2666 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(false)).Times(1);
2667
2668 // The event thread should not be notified that the screen was released.
2669 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(1);
2670 }
2671};
2672
Lloyd Pique41be5d22018-06-21 13:11:48 -07002673struct DispSyncIsSupportedVariant {
2674 static void setupBeginResyncCallExpectations(DisplayTransactionTest* test) {
2675 EXPECT_CALL(*test->mPrimaryDispSync, reset()).Times(1);
2676 EXPECT_CALL(*test->mPrimaryDispSync, setPeriod(DEFAULT_REFRESH_RATE)).Times(1);
2677 EXPECT_CALL(*test->mPrimaryDispSync, beginResync()).Times(1);
2678 }
2679
2680 static void setupEndResyncCallExpectations(DisplayTransactionTest* test) {
2681 EXPECT_CALL(*test->mPrimaryDispSync, endResync()).Times(1);
2682 }
2683};
2684
2685struct DispSyncNotSupportedVariant {
2686 static void setupBeginResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2687
2688 static void setupEndResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2689};
2690
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002691// --------------------------------------------------------------------
2692// Note:
2693//
2694// There are a large number of transitions we could test, however we only test a
2695// selected subset which provides complete test coverage of the implementation.
2696// --------------------------------------------------------------------
2697
2698template <int initialPowerMode, int targetPowerMode>
2699struct TransitionVariantCommon {
2700 static constexpr auto INITIAL_POWER_MODE = initialPowerMode;
2701 static constexpr auto TARGET_POWER_MODE = targetPowerMode;
2702
2703 static void verifyPostconditions(DisplayTransactionTest*) {}
2704};
2705
2706struct TransitionOffToOnVariant
2707 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_NORMAL> {
2708 template <typename Case>
2709 static void setupCallExpectations(DisplayTransactionTest* test) {
2710 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2711 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002712 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002713 Case::setupRepaintEverythingCallExpectations(test);
2714 }
2715
2716 static void verifyPostconditions(DisplayTransactionTest* test) {
2717 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2718 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2719 }
2720};
2721
2722struct TransitionOffToDozeSuspendVariant
2723 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_DOZE_SUSPEND> {
2724 template <typename Case>
2725 static void setupCallExpectations(DisplayTransactionTest* test) {
2726 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2727 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2728 Case::setupRepaintEverythingCallExpectations(test);
2729 }
2730
2731 static void verifyPostconditions(DisplayTransactionTest* test) {
2732 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2733 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2734 }
2735};
2736
2737struct TransitionOnToOffVariant
2738 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_OFF> {
2739 template <typename Case>
2740 static void setupCallExpectations(DisplayTransactionTest* test) {
2741 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002742 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002743 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2744 }
2745
2746 static void verifyPostconditions(DisplayTransactionTest* test) {
2747 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2748 }
2749};
2750
2751struct TransitionDozeSuspendToOffVariant
2752 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_OFF> {
2753 template <typename Case>
2754 static void setupCallExpectations(DisplayTransactionTest* test) {
2755 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2756 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2757 }
2758
2759 static void verifyPostconditions(DisplayTransactionTest* test) {
2760 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2761 }
2762};
2763
2764struct TransitionOnToDozeVariant
2765 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE> {
2766 template <typename Case>
2767 static void setupCallExpectations(DisplayTransactionTest* test) {
2768 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2769 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2770 }
2771};
2772
2773struct TransitionDozeSuspendToDozeVariant
2774 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_DOZE> {
2775 template <typename Case>
2776 static void setupCallExpectations(DisplayTransactionTest* test) {
2777 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002778 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002779 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2780 }
2781};
2782
2783struct TransitionDozeToOnVariant
2784 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE, HWC_POWER_MODE_NORMAL> {
2785 template <typename Case>
2786 static void setupCallExpectations(DisplayTransactionTest* test) {
2787 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2788 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2789 }
2790};
2791
2792struct TransitionDozeSuspendToOnVariant
2793 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_NORMAL> {
2794 template <typename Case>
2795 static void setupCallExpectations(DisplayTransactionTest* test) {
2796 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002797 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002798 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2799 }
2800};
2801
2802struct TransitionOnToDozeSuspendVariant
2803 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE_SUSPEND> {
2804 template <typename Case>
2805 static void setupCallExpectations(DisplayTransactionTest* test) {
2806 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002807 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002808 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2809 }
2810};
2811
2812struct TransitionOnToUnknownVariant
2813 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_LEET> {
2814 template <typename Case>
2815 static void setupCallExpectations(DisplayTransactionTest* test) {
2816 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2817 Case::setupNoComposerPowerModeCallExpectations(test);
2818 }
2819};
2820
2821// --------------------------------------------------------------------
2822// Note:
2823//
2824// Rather than testing the cartesian product of of
2825// DozeIsSupported/DozeNotSupported with all other options, we use one for one
2826// display type, and the other for another display type.
2827// --------------------------------------------------------------------
2828
2829template <typename DisplayVariant, typename DozeVariant, typename EventThreadVariant,
Lloyd Pique41be5d22018-06-21 13:11:48 -07002830 typename DispSyncVariant, typename TransitionVariant>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002831struct DisplayPowerCase {
2832 using Display = DisplayVariant;
2833 using Doze = DozeVariant;
2834 using EventThread = EventThreadVariant;
Lloyd Pique41be5d22018-06-21 13:11:48 -07002835 using DispSync = DispSyncVariant;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002836 using Transition = TransitionVariant;
2837
2838 static auto injectDisplayWithInitialPowerMode(DisplayTransactionTest* test, int mode) {
2839 Display::injectHwcDisplay(test);
2840 auto display = Display::makeFakeExistingDisplayInjector(test);
2841 display.inject();
2842 display.mutableDisplayDevice()->setPowerMode(mode);
2843 return display;
2844 }
2845
2846 static void setInitialPrimaryHWVsyncEnabled(DisplayTransactionTest* test, bool enabled) {
2847 test->mFlinger.mutablePrimaryHWVsyncEnabled() = enabled;
2848 }
2849
2850 static void setupRepaintEverythingCallExpectations(DisplayTransactionTest* test) {
2851 EXPECT_CALL(*test->mMessageQueue, invalidate()).Times(1);
2852 }
2853
2854 static void setupSurfaceInterceptorCallExpectations(DisplayTransactionTest* test, int mode) {
2855 EXPECT_CALL(*test->mSurfaceInterceptor, isEnabled()).WillOnce(Return(true));
2856 EXPECT_CALL(*test->mSurfaceInterceptor, savePowerModeUpdate(_, mode)).Times(1);
2857 }
2858
2859 static void setupComposerCallExpectations(DisplayTransactionTest* test,
2860 IComposerClient::PowerMode mode) {
2861 // Any calls to get the active config will return a default value.
2862 EXPECT_CALL(*test->mComposer, getActiveConfig(Display::HWC_DISPLAY_ID, _))
2863 .WillRepeatedly(DoAll(SetArgPointee<1>(Display::HWC_ACTIVE_CONFIG_ID),
2864 Return(Error::NONE)));
2865
2866 // Any calls to get whether the display supports dozing will return the value set by the
2867 // policy variant.
2868 EXPECT_CALL(*test->mComposer, getDozeSupport(Display::HWC_DISPLAY_ID, _))
2869 .WillRepeatedly(DoAll(SetArgPointee<1>(Doze::DOZE_SUPPORTED), Return(Error::NONE)));
2870
2871 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, mode)).Times(1);
2872 }
2873
2874 static void setupNoComposerPowerModeCallExpectations(DisplayTransactionTest* test) {
2875 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, _)).Times(0);
2876 }
2877};
2878
2879// A sample configuration for the primary display.
2880// In addition to having event thread support, we emulate doze support.
2881template <typename TransitionVariant>
Peiyong Lined531a32018-10-26 18:27:56 -07002882using PrimaryDisplayPowerCase =
2883 DisplayPowerCase<PrimaryDisplayVariant, DozeIsSupportedVariant<PrimaryDisplayVariant>,
2884 EventThreadIsSupportedVariant, DispSyncIsSupportedVariant,
2885 TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002886
2887// A sample configuration for the external display.
2888// In addition to not having event thread support, we emulate not having doze
2889// support.
2890template <typename TransitionVariant>
Peiyong Lined531a32018-10-26 18:27:56 -07002891using ExternalDisplayPowerCase =
2892 DisplayPowerCase<ExternalDisplayVariant, DozeNotSupportedVariant<ExternalDisplayVariant>,
2893 EventThreadNotSupportedVariant, DispSyncNotSupportedVariant,
2894 TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002895
2896class SetPowerModeInternalTest : public DisplayTransactionTest {
2897public:
2898 template <typename Case>
2899 void transitionDisplayCommon();
2900};
2901
2902template <int PowerMode>
2903struct PowerModeInitialVSyncEnabled : public std::false_type {};
2904
2905template <>
2906struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_NORMAL> : public std::true_type {};
2907
2908template <>
2909struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_DOZE> : public std::true_type {};
2910
2911template <typename Case>
2912void SetPowerModeInternalTest::transitionDisplayCommon() {
2913 // --------------------------------------------------------------------
2914 // Preconditions
2915
Peiyong Lined531a32018-10-26 18:27:56 -07002916 Case::Doze::setupComposerCallExpectations(this);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002917 auto display =
2918 Case::injectDisplayWithInitialPowerMode(this, Case::Transition::INITIAL_POWER_MODE);
2919 Case::setInitialPrimaryHWVsyncEnabled(this,
2920 PowerModeInitialVSyncEnabled<
2921 Case::Transition::INITIAL_POWER_MODE>::value);
2922
2923 // --------------------------------------------------------------------
2924 // Call Expectations
2925
2926 Case::setupSurfaceInterceptorCallExpectations(this, Case::Transition::TARGET_POWER_MODE);
2927 Case::Transition::template setupCallExpectations<Case>(this);
2928
2929 // --------------------------------------------------------------------
2930 // Invocation
2931
2932 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(),
2933 Case::Transition::TARGET_POWER_MODE);
2934
2935 // --------------------------------------------------------------------
2936 // Postconditions
2937
2938 Case::Transition::verifyPostconditions(this);
2939}
2940
2941TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfNoChange) {
2942 using Case = SimplePrimaryDisplayCase;
2943
2944 // --------------------------------------------------------------------
2945 // Preconditions
2946
2947 // A primary display device is set up
2948 Case::Display::injectHwcDisplay(this);
2949 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2950 display.inject();
2951
Dominik Laskowskia2edf612018-06-01 13:15:16 -07002952 // The display is already set to HWC_POWER_MODE_NORMAL
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002953 display.mutableDisplayDevice()->setPowerMode(HWC_POWER_MODE_NORMAL);
2954
2955 // --------------------------------------------------------------------
2956 // Invocation
2957
2958 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_NORMAL);
2959
2960 // --------------------------------------------------------------------
2961 // Postconditions
2962
2963 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2964}
2965
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002966TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfVirtualDisplay) {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002967 using Case = HwcVirtualDisplayCase;
2968
2969 // --------------------------------------------------------------------
2970 // Preconditions
2971
Dominik Laskowski075d3172018-05-24 15:50:06 -07002972 // Insert display data so that the HWC thinks it created the virtual display.
2973 const auto displayId = Case::Display::DISPLAY_ID::get();
2974 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08002975 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002976
2977 // A virtual display device is set up
2978 Case::Display::injectHwcDisplay(this);
2979 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2980 display.inject();
2981
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002982 // The display is set to HWC_POWER_MODE_NORMAL
2983 getDisplayDevice(display.token())->setPowerMode(HWC_POWER_MODE_NORMAL);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002984
2985 // --------------------------------------------------------------------
2986 // Invocation
2987
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002988 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_OFF);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002989
2990 // --------------------------------------------------------------------
2991 // Postconditions
2992
2993 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2994}
2995
2996TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnPrimaryDisplay) {
2997 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToOnVariant>>();
2998}
2999
3000TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendPrimaryDisplay) {
3001 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
3002}
3003
3004TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffPrimaryDisplay) {
3005 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToOffVariant>>();
3006}
3007
3008TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffPrimaryDisplay) {
3009 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
3010}
3011
3012TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozePrimaryDisplay) {
3013 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeVariant>>();
3014}
3015
3016TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozePrimaryDisplay) {
3017 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
3018}
3019
3020TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnPrimaryDisplay) {
3021 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeToOnVariant>>();
3022}
3023
3024TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnPrimaryDisplay) {
3025 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
3026}
3027
3028TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendPrimaryDisplay) {
3029 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
3030}
3031
3032TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownPrimaryDisplay) {
3033 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToUnknownVariant>>();
3034}
3035
3036TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnExternalDisplay) {
3037 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToOnVariant>>();
3038}
3039
3040TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendExternalDisplay) {
3041 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
3042}
3043
3044TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffExternalDisplay) {
3045 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToOffVariant>>();
3046}
3047
3048TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffExternalDisplay) {
3049 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
3050}
3051
3052TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeExternalDisplay) {
3053 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeVariant>>();
3054}
3055
3056TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozeExternalDisplay) {
3057 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
3058}
3059
3060TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnExternalDisplay) {
3061 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeToOnVariant>>();
3062}
3063
3064TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnExternalDisplay) {
3065 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
3066}
3067
3068TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendExternalDisplay) {
3069 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
3070}
3071
3072TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownExternalDisplay) {
3073 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToUnknownVariant>>();
3074}
3075
Lloyd Piquef58625d2017-12-19 13:22:33 -08003076} // namespace
3077} // namespace android