blob: e8c889496b87c8549ce653cc354555c8cf2eece1 [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;
Alec Mouri05874642018-11-14 22:34:02 +0000138 renderengine::mock::Surface* mRenderSurface = nullptr;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800139};
140
141DisplayTransactionTest::DisplayTransactionTest() {
142 const ::testing::TestInfo* const test_info =
143 ::testing::UnitTest::GetInstance()->current_test_info();
144 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800145
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800146 // Default to no wide color display support configured
147 mFlinger.mutableHasWideColorDisplay() = false;
Peiyong Lin13effd12018-07-24 17:01:47 -0700148 mFlinger.mutableUseColorManagement() = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800149 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
150
151 // Default to using HWC virtual displays
152 mFlinger.mutableUseHwcVirtualDisplays() = true;
153
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800154 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
155 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
156 });
157
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800158 mFlinger.setCreateNativeWindowSurface([](auto) {
159 ADD_FAILURE() << "Unexpected request to create a native window surface.";
160 return nullptr;
161 });
162
163 mFlinger.mutableEventControlThread().reset(mEventControlThread);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800164 mFlinger.mutableEventThread().reset(mEventThread);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800165 mFlinger.mutableEventQueue().reset(mMessageQueue);
Peiyong Lin833074a2018-08-28 11:53:54 -0700166 mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800167 mFlinger.mutableInterceptor().reset(mSurfaceInterceptor);
Lloyd Pique41be5d22018-06-21 13:11:48 -0700168 mFlinger.mutablePrimaryDispSync().reset(mPrimaryDispSync);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800169
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800170 injectMockComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -0800171}
172
173DisplayTransactionTest::~DisplayTransactionTest() {
174 const ::testing::TestInfo* const test_info =
175 ::testing::UnitTest::GetInstance()->current_test_info();
176 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
177}
178
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800179void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
180 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800181 EXPECT_CALL(*mComposer, getCapabilities())
182 .WillOnce(Return(std::vector<IComposer::Capability>()));
183 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
184 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -0800185
Lloyd Piquee39cad22017-12-20 17:01:29 -0800186 Mock::VerifyAndClear(mComposer);
187}
188
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800189void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800190 // This setup is only expected once per test.
191 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
192
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800193 mConsumer = new mock::GraphicBufferConsumer();
194 mProducer = new mock::GraphicBufferProducer();
195
196 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
197 *outProducer = mProducer;
198 *outConsumer = mConsumer;
199 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800200}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800201
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800202void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800203 // This setup is only expected once per test.
204 ASSERT_TRUE(mNativeWindowSurface == nullptr);
205
Lloyd Pique22098362018-09-13 11:46:49 -0700206 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800207
Lloyd Pique22098362018-09-13 11:46:49 -0700208 mFlinger.setCreateNativeWindowSurface([this](auto) {
209 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
210 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800211}
212
Dominik Laskowski075d3172018-05-24 15:50:06 -0700213bool DisplayTransactionTest::hasPhysicalHwcDisplay(hwc2_display_t hwcDisplayId) {
214 return mFlinger.mutableHwcPhysicalDisplayIdMap().count(hwcDisplayId) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800215}
216
217bool DisplayTransactionTest::hasTransactionFlagSet(int flag) {
218 return mFlinger.mutableTransactionFlags() & flag;
219}
220
221bool DisplayTransactionTest::hasDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700222 return mFlinger.mutableDisplays().count(displayToken) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800223}
224
225sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700226 return mFlinger.mutableDisplays()[displayToken];
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800227}
228
229bool DisplayTransactionTest::hasCurrentDisplayState(sp<IBinder> displayToken) {
230 return mFlinger.mutableCurrentState().displays.indexOfKey(displayToken) >= 0;
231}
232
233const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(sp<IBinder> displayToken) {
234 return mFlinger.mutableCurrentState().displays.valueFor(displayToken);
235}
236
237bool DisplayTransactionTest::hasDrawingDisplayState(sp<IBinder> displayToken) {
238 return mFlinger.mutableDrawingState().displays.indexOfKey(displayToken) >= 0;
239}
240
241const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(sp<IBinder> displayToken) {
242 return mFlinger.mutableDrawingState().displays.valueFor(displayToken);
243}
244
245/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800246 *
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800247 */
248
Dominik Laskowski075d3172018-05-24 15:50:06 -0700249template <typename PhysicalDisplay>
250struct PhysicalDisplayId {};
251
Dominik Laskowski34157762018-10-31 13:07:19 -0700252template <DisplayId::Type displayId>
253using VirtualDisplayId = std::integral_constant<DisplayId::Type, displayId>;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700254
255struct NoDisplayId {};
256
257template <typename>
258struct IsPhysicalDisplayId : std::bool_constant<false> {};
259
260template <typename PhysicalDisplay>
261struct IsPhysicalDisplayId<PhysicalDisplayId<PhysicalDisplay>> : std::bool_constant<true> {};
262
263template <typename>
264struct DisplayIdGetter;
265
266template <typename PhysicalDisplay>
267struct DisplayIdGetter<PhysicalDisplayId<PhysicalDisplay>> {
268 static std::optional<DisplayId> get() {
269 if (!PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
270 return getFallbackDisplayId(static_cast<bool>(PhysicalDisplay::PRIMARY)
271 ? HWC_DISPLAY_PRIMARY
272 : HWC_DISPLAY_EXTERNAL);
273 }
274
275 const auto info =
276 parseDisplayIdentificationData(PhysicalDisplay::PORT,
277 PhysicalDisplay::GET_IDENTIFICATION_DATA());
278 return info ? std::make_optional(info->id) : std::nullopt;
279 }
280};
281
Dominik Laskowski34157762018-10-31 13:07:19 -0700282template <DisplayId::Type displayId>
Dominik Laskowski075d3172018-05-24 15:50:06 -0700283struct DisplayIdGetter<VirtualDisplayId<displayId>> {
Dominik Laskowski34157762018-10-31 13:07:19 -0700284 static std::optional<DisplayId> get() { return DisplayId{displayId}; }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700285};
286
287template <>
288struct DisplayIdGetter<NoDisplayId> {
289 static std::optional<DisplayId> get() { return {}; }
290};
291
292// DisplayIdType can be:
293// 1) PhysicalDisplayId<...> for generated ID of physical display backed by HWC.
294// 2) VirtualDisplayId<...> for hard-coded ID of virtual display backed by HWC.
295// 3) NoDisplayId for virtual display without HWC backing.
296template <typename DisplayIdType, int width, int height, Critical critical, Async async,
297 Secure secure, Primary primary, int grallocUsage>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800298struct DisplayVariant {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700299 using DISPLAY_ID = DisplayIdGetter<DisplayIdType>;
300
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800301 // The display width and height
302 static constexpr int WIDTH = width;
303 static constexpr int HEIGHT = height;
304
305 static constexpr int GRALLOC_USAGE = grallocUsage;
306
Dominik Laskowski075d3172018-05-24 15:50:06 -0700307 // Whether the display is virtual or physical
308 static constexpr Virtual VIRTUAL =
309 IsPhysicalDisplayId<DisplayIdType>{} ? Virtual::FALSE : Virtual::TRUE;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800310
311 // When creating native window surfaces for the framebuffer, whether those should be critical
312 static constexpr Critical CRITICAL = critical;
313
314 // When creating native window surfaces for the framebuffer, whether those should be async
315 static constexpr Async ASYNC = async;
316
317 // Whether the display should be treated as secure
318 static constexpr Secure SECURE = secure;
319
Dominik Laskowski075d3172018-05-24 15:50:06 -0700320 // Whether the display is primary
321 static constexpr Primary PRIMARY = primary;
322
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800323 static auto makeFakeExistingDisplayInjector(DisplayTransactionTest* test) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700324 auto injector =
325 FakeDisplayDeviceInjector(test->mFlinger, DISPLAY_ID::get(),
326 static_cast<bool>(VIRTUAL), static_cast<bool>(PRIMARY));
327
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800328 injector.setSecure(static_cast<bool>(SECURE));
Alec Mouriba013fa2018-10-16 12:43:11 -0700329 injector.setNativeWindow(test->mNativeWindow);
330
331 // Creating a DisplayDevice requires getting default dimensions from the
332 // native window.
333 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
334 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
335 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
336 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800337 return injector;
338 }
339
340 // Called by tests to set up any native window creation call expectations.
341 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
342 EXPECT_CALL(*test->mNativeWindowSurface, getNativeWindow())
343 .WillOnce(Return(test->mNativeWindow));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800344
Alec Mouri05874642018-11-14 22:34:02 +0000345 // For simplicity, we only expect to create a single render surface for
346 // each test.
347 ASSERT_TRUE(test->mRenderSurface == nullptr);
348 test->mRenderSurface = new renderengine::mock::Surface();
349 EXPECT_CALL(*test->mRenderEngine, createSurface())
350 .WillOnce(Return(ByMove(
351 std::unique_ptr<renderengine::Surface>(test->mRenderSurface))));
Alec Mouriba013fa2018-10-16 12:43:11 -0700352 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
353 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
354 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
355 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
Alec Mouri05874642018-11-14 22:34:02 +0000356
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800357 // Creating a DisplayDevice requires getting default dimensions from the
358 // native window.
Alec Mouri05874642018-11-14 22:34:02 +0000359 EXPECT_CALL(*test->mRenderSurface, setAsync(static_cast<bool>(ASYNC))).Times(1);
360 EXPECT_CALL(*test->mRenderSurface, setCritical(static_cast<bool>(CRITICAL))).Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800361 }
362
363 static void setupFramebufferConsumerBufferQueueCallExpectations(DisplayTransactionTest* test) {
364 EXPECT_CALL(*test->mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR));
365 EXPECT_CALL(*test->mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR));
366 EXPECT_CALL(*test->mConsumer, setConsumerUsageBits(GRALLOC_USAGE))
367 .WillRepeatedly(Return(NO_ERROR));
368 EXPECT_CALL(*test->mConsumer, setDefaultBufferSize(WIDTH, HEIGHT))
369 .WillRepeatedly(Return(NO_ERROR));
370 EXPECT_CALL(*test->mConsumer, setMaxAcquiredBufferCount(_))
371 .WillRepeatedly(Return(NO_ERROR));
372 }
373
374 static void setupFramebufferProducerBufferQueueCallExpectations(DisplayTransactionTest* test) {
375 EXPECT_CALL(*test->mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return());
376 }
377};
378
Dominik Laskowski075d3172018-05-24 15:50:06 -0700379template <hwc2_display_t hwcDisplayId, HWC2::DisplayType hwcDisplayType, typename DisplayVariant,
380 typename PhysicalDisplay = void>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800381struct HwcDisplayVariant {
382 // The display id supplied by the HWC
383 static constexpr hwc2_display_t HWC_DISPLAY_ID = hwcDisplayId;
384
385 // The HWC display type
386 static constexpr HWC2::DisplayType HWC_DISPLAY_TYPE = hwcDisplayType;
387
388 // The HWC active configuration id
Lloyd Pique3c085a02018-05-09 19:38:32 -0700389 static constexpr int HWC_ACTIVE_CONFIG_ID = 2001;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800390
391 static void injectPendingHotplugEvent(DisplayTransactionTest* test,
392 HWC2::Connection connection) {
393 test->mFlinger.mutablePendingHotplugEvents().emplace_back(
394 HotplugEvent{HWC_DISPLAY_ID, connection});
395 }
396
397 // Called by tests to inject a HWC display setup
398 static void injectHwcDisplay(DisplayTransactionTest* test) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700399 const auto displayId = DisplayVariant::DISPLAY_ID::get();
400 ASSERT_TRUE(displayId);
401 FakeHwcDisplayInjector(*displayId, HWC_DISPLAY_TYPE,
402 static_cast<bool>(DisplayVariant::PRIMARY))
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800403 .setHwcDisplayId(HWC_DISPLAY_ID)
404 .setWidth(DisplayVariant::WIDTH)
405 .setHeight(DisplayVariant::HEIGHT)
406 .setActiveConfig(HWC_ACTIVE_CONFIG_ID)
407 .inject(&test->mFlinger, test->mComposer);
408 }
409
410 static void setupHwcHotplugCallExpectations(DisplayTransactionTest* test) {
411 EXPECT_CALL(*test->mComposer, getDisplayType(HWC_DISPLAY_ID, _))
412 .WillOnce(DoAll(SetArgPointee<1>(static_cast<IComposerClient::DisplayType>(
413 HWC_DISPLAY_TYPE)),
414 Return(Error::NONE)));
415 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
416 EXPECT_CALL(*test->mComposer, getDisplayConfigs(HWC_DISPLAY_ID, _))
417 .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{HWC_ACTIVE_CONFIG_ID}),
418 Return(Error::NONE)));
419 EXPECT_CALL(*test->mComposer,
420 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
421 IComposerClient::Attribute::WIDTH, _))
422 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::WIDTH), Return(Error::NONE)));
423 EXPECT_CALL(*test->mComposer,
424 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
425 IComposerClient::Attribute::HEIGHT, _))
426 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::HEIGHT), Return(Error::NONE)));
427 EXPECT_CALL(*test->mComposer,
428 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
429 IComposerClient::Attribute::VSYNC_PERIOD, _))
430 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE)));
431 EXPECT_CALL(*test->mComposer,
432 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
433 IComposerClient::Attribute::DPI_X, _))
434 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
435 EXPECT_CALL(*test->mComposer,
436 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
437 IComposerClient::Attribute::DPI_Y, _))
438 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700439
440 if (PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
441 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
442 .WillOnce(DoAll(SetArgPointee<1>(PhysicalDisplay::PORT),
443 SetArgPointee<2>(PhysicalDisplay::GET_IDENTIFICATION_DATA()),
444 Return(Error::NONE)));
445 } else {
446 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
447 .WillOnce(Return(Error::UNSUPPORTED));
448 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800449 }
450
451 // Called by tests to set up HWC call expectations
452 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
453 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY_ID, _))
Lloyd Pique3c085a02018-05-09 19:38:32 -0700454 .WillRepeatedly(DoAll(SetArgPointee<1>(HWC_ACTIVE_CONFIG_ID), Return(Error::NONE)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800455 }
456};
457
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800458// Physical displays are expected to be synchronous, secure, and have a HWC display for output.
459constexpr uint32_t GRALLOC_USAGE_PHYSICAL_DISPLAY =
460 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
461
Dominik Laskowski075d3172018-05-24 15:50:06 -0700462template <hwc2_display_t hwcDisplayId, typename PhysicalDisplay, int width, int height,
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800463 Critical critical>
464struct PhysicalDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700465 : DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height, critical, Async::FALSE,
466 Secure::TRUE, PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
467 HwcDisplayVariant<hwcDisplayId, HWC2::DisplayType::Physical,
468 DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height,
469 critical, Async::FALSE, Secure::TRUE,
470 PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
471 PhysicalDisplay> {};
472
473template <bool hasIdentificationData>
474struct PrimaryDisplay {
475 static constexpr Primary PRIMARY = Primary::TRUE;
476 static constexpr uint8_t PORT = 255;
477 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
478 static constexpr auto GET_IDENTIFICATION_DATA = getInternalEdid;
479};
480
481template <bool hasIdentificationData>
482struct ExternalDisplay {
483 static constexpr Primary PRIMARY = Primary::FALSE;
484 static constexpr uint8_t PORT = 254;
485 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
486 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
487};
488
489struct TertiaryDisplay {
490 static constexpr Primary PRIMARY = Primary::FALSE;
491};
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800492
493// A primary display is a physical display that is critical
494using PrimaryDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700495 PhysicalDisplayVariant<1001, PrimaryDisplay<false>, 3840, 2160, Critical::TRUE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800496
497// An external display is physical display that is not critical.
498using ExternalDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700499 PhysicalDisplayVariant<1002, ExternalDisplay<false>, 1920, 1280, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800500
501using TertiaryDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700502 PhysicalDisplayVariant<1003, TertiaryDisplay, 1600, 1200, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800503
504// A virtual display not supported by the HWC.
505constexpr uint32_t GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY = 0;
506
507template <int width, int height, Secure secure>
508struct NonHwcVirtualDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700509 : DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
510 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY> {
511 using Base = DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
512 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>;
513
514 static void injectHwcDisplay(DisplayTransactionTest*) {}
515
516 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
517 EXPECT_CALL(*test->mComposer, getActiveConfig(_, _)).Times(0);
518 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800519
520 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
521 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
522 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
523 }
524};
525
526// A virtual display supported by the HWC.
527constexpr uint32_t GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY = GRALLOC_USAGE_HW_COMPOSER;
528
529template <int width, int height, Secure secure>
530struct HwcVirtualDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700531 : DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE, secure,
532 Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>,
533 HwcDisplayVariant<
534 1010, HWC2::DisplayType::Virtual,
535 DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
536 secure, Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>> {
537 using Base = DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
538 secure, Primary::FALSE, GRALLOC_USAGE_HW_COMPOSER>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800539 using Self = HwcVirtualDisplayVariant<width, height, secure>;
540
541 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
542 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
543 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
544 }
545
546 static void setupHwcVirtualDisplayCreationCallExpectations(DisplayTransactionTest* test) {
547 EXPECT_CALL(*test->mComposer, createVirtualDisplay(Base::WIDTH, Base::HEIGHT, _, _))
548 .WillOnce(DoAll(SetArgPointee<3>(Self::HWC_DISPLAY_ID), Return(Error::NONE)));
549 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
550 }
551};
552
553// For this variant, SurfaceFlinger should not configure itself with wide
554// display support, so the display should not be configured for wide-color
555// support.
556struct WideColorSupportNotConfiguredVariant {
557 static constexpr bool WIDE_COLOR_SUPPORTED = false;
558
559 static void injectConfigChange(DisplayTransactionTest* test) {
560 test->mFlinger.mutableHasWideColorDisplay() = false;
Peiyong Lin13effd12018-07-24 17:01:47 -0700561 test->mFlinger.mutableUseColorManagement() = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800562 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
563 }
564
565 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
566 EXPECT_CALL(*test->mComposer, getColorModes(_, _)).Times(0);
567 EXPECT_CALL(*test->mComposer, getRenderIntents(_, _, _)).Times(0);
568 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
569 }
570};
571
572// For this variant, SurfaceFlinger should configure itself with wide display
573// support, and the display should respond with an non-empty list of supported
574// color modes. Wide-color support should be configured.
575template <typename Display>
576struct WideColorP3ColorimetricSupportedVariant {
577 static constexpr bool WIDE_COLOR_SUPPORTED = true;
578
579 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700580 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800581 test->mFlinger.mutableHasWideColorDisplay() = true;
582 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
583 }
584
585 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
586 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
587 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>({ColorMode::DISPLAY_P3})),
588 Return(Error::NONE)));
589 EXPECT_CALL(*test->mComposer,
590 getRenderIntents(Display::HWC_DISPLAY_ID, ColorMode::DISPLAY_P3, _))
591 .WillOnce(DoAll(SetArgPointee<2>(
592 std::vector<RenderIntent>({RenderIntent::COLORIMETRIC})),
593 Return(Error::NONE)));
594 EXPECT_CALL(*test->mComposer,
595 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::SRGB,
596 RenderIntent::COLORIMETRIC))
597 .WillOnce(Return(Error::NONE));
598 }
599};
600
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800601// For this variant, SurfaceFlinger should configure itself with wide display
602// support, but the display should respond with an empty list of supported color
603// modes. Wide-color support for the display should not be configured.
604template <typename Display>
605struct WideColorNotSupportedVariant {
606 static constexpr bool WIDE_COLOR_SUPPORTED = false;
607
608 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700609 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800610 test->mFlinger.mutableHasWideColorDisplay() = true;
611 }
612
613 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
614 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
615 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>()), Return(Error::NONE)));
Chia-I Wu614e1422018-05-23 02:17:03 -0700616 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800617 }
618};
619
620// For this variant, the display is not a HWC display, so no HDR support should
621// be configured.
622struct NonHwcDisplayHdrSupportVariant {
623 static constexpr bool HDR10_SUPPORTED = false;
624 static constexpr bool HDR_HLG_SUPPORTED = false;
625 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
626 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
627 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
628 }
629};
630
631// For this variant, the composer should respond with a non-empty list of HDR
632// modes containing HDR10, so HDR10 support should be configured.
633template <typename Display>
634struct Hdr10SupportedVariant {
635 static constexpr bool HDR10_SUPPORTED = true;
636 static constexpr bool HDR_HLG_SUPPORTED = false;
637 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
638 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
639 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
640 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HDR10})),
641 Return(Error::NONE)));
642 }
643};
644
645// For this variant, the composer should respond with a non-empty list of HDR
646// modes containing HLG, so HLG support should be configured.
647template <typename Display>
648struct HdrHlgSupportedVariant {
649 static constexpr bool HDR10_SUPPORTED = false;
650 static constexpr bool HDR_HLG_SUPPORTED = true;
651 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
652 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
653 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
654 .WillOnce(
655 DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HLG})), Return(Error::NONE)));
656 }
657};
658
659// For this variant, the composer should respond with a non-empty list of HDR
660// modes containing DOLBY_VISION, so DOLBY_VISION support should be configured.
661template <typename Display>
662struct HdrDolbyVisionSupportedVariant {
663 static constexpr bool HDR10_SUPPORTED = false;
664 static constexpr bool HDR_HLG_SUPPORTED = false;
665 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = true;
666 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
667 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
668 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::DOLBY_VISION})),
669 Return(Error::NONE)));
670 }
671};
672
673// For this variant, the composer should respond with am empty list of HDR
674// modes, so no HDR support should be configured.
675template <typename Display>
676struct HdrNotSupportedVariant {
677 static constexpr bool HDR10_SUPPORTED = false;
678 static constexpr bool HDR_HLG_SUPPORTED = false;
679 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
680 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
681 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
682 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
683 }
684};
685
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700686struct NonHwcPerFrameMetadataSupportVariant {
687 static constexpr int PER_FRAME_METADATA_KEYS = 0;
688 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800689 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(_)).Times(0);
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700690 }
691};
692
693template <typename Display>
694struct NoPerFrameMetadataSupportVariant {
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(Display::HWC_DISPLAY_ID))
698 .WillOnce(Return(std::vector<PerFrameMetadataKey>()));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700699 }
700};
701
702template <typename Display>
703struct Smpte2086PerFrameMetadataSupportVariant {
704 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::SMPTE2086;
705 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800706 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
707 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700708 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X,
709 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y,
710 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X,
711 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y,
712 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X,
713 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y,
714 PerFrameMetadataKey::WHITE_POINT_X,
715 PerFrameMetadataKey::WHITE_POINT_Y,
716 PerFrameMetadataKey::MAX_LUMINANCE,
717 PerFrameMetadataKey::MIN_LUMINANCE,
Chia-I Wud7e01d72018-06-21 13:39:09 +0800718 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700719 }
720};
721
722template <typename Display>
723struct Cta861_3_PerFrameMetadataSupportVariant {
724 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::CTA861_3;
725 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800726 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
727 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700728 PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL,
729 PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL,
Chia-I Wud7e01d72018-06-21 13:39:09 +0800730 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700731 }
732};
733
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800734/* ------------------------------------------------------------------------
735 * Typical display configurations to test
736 */
737
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700738template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy,
739 typename PerFrameMetadataSupportPolicy>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800740struct Case {
741 using Display = DisplayPolicy;
742 using WideColorSupport = WideColorSupportPolicy;
743 using HdrSupport = HdrSupportPolicy;
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700744 using PerFrameMetadataSupport = PerFrameMetadataSupportPolicy;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800745};
746
747using SimplePrimaryDisplayCase =
748 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700749 HdrNotSupportedVariant<PrimaryDisplayVariant>,
750 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800751using SimpleExternalDisplayCase =
752 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700753 HdrNotSupportedVariant<ExternalDisplayVariant>,
754 NoPerFrameMetadataSupportVariant<ExternalDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800755using SimpleTertiaryDisplayCase =
756 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700757 HdrNotSupportedVariant<TertiaryDisplayVariant>,
758 NoPerFrameMetadataSupportVariant<TertiaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800759using NonHwcVirtualDisplayCase =
760 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700761 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant,
762 NonHwcPerFrameMetadataSupportVariant>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800763using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
764using HwcVirtualDisplayCase =
765 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
Lloyd Pique438e9e72018-09-04 18:06:08 -0700766 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>,
tangrobin6753a022018-08-10 10:58:54 +0800767 NoPerFrameMetadataSupportVariant<SimpleHwcVirtualDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800768using WideColorP3ColorimetricDisplayCase =
769 Case<PrimaryDisplayVariant, WideColorP3ColorimetricSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700770 HdrNotSupportedVariant<PrimaryDisplayVariant>,
771 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800772using Hdr10DisplayCase =
773 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700774 Hdr10SupportedVariant<PrimaryDisplayVariant>,
775 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800776using HdrHlgDisplayCase =
777 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700778 HdrHlgSupportedVariant<PrimaryDisplayVariant>,
779 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800780using HdrDolbyVisionDisplayCase =
781 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700782 HdrDolbyVisionSupportedVariant<PrimaryDisplayVariant>,
783 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
784using HdrSmpte2086DisplayCase =
785 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
786 HdrNotSupportedVariant<PrimaryDisplayVariant>,
787 Smpte2086PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
788using HdrCta861_3_DisplayCase =
789 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
790 HdrNotSupportedVariant<PrimaryDisplayVariant>,
791 Cta861_3_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Dominik Laskowskif07b85b2018-06-11 12:49:15 -0700792
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800793/* ------------------------------------------------------------------------
Lloyd Pique6cf11032018-01-22 18:57:44 -0800794 *
795 * SurfaceFlinger::onHotplugReceived
796 */
797
798TEST_F(DisplayTransactionTest, hotplugEnqueuesEventsForDisplayTransaction) {
799 constexpr int currentSequenceId = 123;
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700800 constexpr hwc2_display_t hwcDisplayId1 = 456;
801 constexpr hwc2_display_t hwcDisplayId2 = 654;
Lloyd Pique6cf11032018-01-22 18:57:44 -0800802
803 // --------------------------------------------------------------------
804 // Preconditions
805
806 // Set the current sequence id for accepted events
807 mFlinger.mutableComposerSequenceId() = currentSequenceId;
808
809 // Set the main thread id so that the current thread does not appear to be
810 // the main thread.
811 mFlinger.mutableMainThreadId() = std::thread::id();
812
813 // --------------------------------------------------------------------
814 // Call Expectations
815
816 // We expect invalidate() to be invoked once to trigger display transaction
817 // processing.
818 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
819
820 // --------------------------------------------------------------------
821 // Invocation
822
823 // Simulate two hotplug events (a connect and a disconnect)
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700824 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId1, HWC2::Connection::Connected);
825 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId2, HWC2::Connection::Disconnected);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800826
827 // --------------------------------------------------------------------
828 // Postconditions
829
830 // The display transaction needed flag should be set.
831 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
832
833 // All events should be in the pending event queue.
834 const auto& pendingEvents = mFlinger.mutablePendingHotplugEvents();
835 ASSERT_EQ(2u, pendingEvents.size());
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700836 EXPECT_EQ(hwcDisplayId1, pendingEvents[0].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800837 EXPECT_EQ(HWC2::Connection::Connected, pendingEvents[0].connection);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700838 EXPECT_EQ(hwcDisplayId2, pendingEvents[1].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800839 EXPECT_EQ(HWC2::Connection::Disconnected, pendingEvents[1].connection);
840}
841
842TEST_F(DisplayTransactionTest, hotplugDiscardsUnexpectedEvents) {
843 constexpr int currentSequenceId = 123;
844 constexpr int otherSequenceId = 321;
845 constexpr hwc2_display_t displayId = 456;
846
847 // --------------------------------------------------------------------
848 // Preconditions
849
850 // Set the current sequence id for accepted events
851 mFlinger.mutableComposerSequenceId() = currentSequenceId;
852
853 // Set the main thread id so that the current thread does not appear to be
854 // the main thread.
855 mFlinger.mutableMainThreadId() = std::thread::id();
856
857 // --------------------------------------------------------------------
858 // Call Expectations
859
860 // We do not expect any calls to invalidate().
861 EXPECT_CALL(*mMessageQueue, invalidate()).Times(0);
862
863 // --------------------------------------------------------------------
864 // Invocation
865
866 // Call with an unexpected sequence id
867 mFlinger.onHotplugReceived(otherSequenceId, displayId, HWC2::Connection::Invalid);
868
869 // --------------------------------------------------------------------
870 // Postconditions
871
872 // The display transaction needed flag should not be set
873 EXPECT_FALSE(hasTransactionFlagSet(eDisplayTransactionNeeded));
874
875 // There should be no pending events
876 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
877}
878
879TEST_F(DisplayTransactionTest, hotplugProcessesEnqueuedEventsIfCalledOnMainThread) {
880 constexpr int currentSequenceId = 123;
881 constexpr hwc2_display_t displayId1 = 456;
882
883 // --------------------------------------------------------------------
884 // Note:
885 // --------------------------------------------------------------------
886 // This test case is a bit tricky. We want to verify that
887 // onHotplugReceived() calls processDisplayHotplugEventsLocked(), but we
888 // don't really want to provide coverage for everything the later function
889 // does as there are specific tests for it.
890 // --------------------------------------------------------------------
891
892 // --------------------------------------------------------------------
893 // Preconditions
894
895 // Set the current sequence id for accepted events
896 mFlinger.mutableComposerSequenceId() = currentSequenceId;
897
898 // Set the main thread id so that the current thread does appear to be the
899 // main thread.
900 mFlinger.mutableMainThreadId() = std::this_thread::get_id();
901
902 // --------------------------------------------------------------------
903 // Call Expectations
904
905 // We expect invalidate() to be invoked once to trigger display transaction
906 // processing.
907 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
908
909 // --------------------------------------------------------------------
910 // Invocation
911
912 // Simulate a disconnect on a display id that is not connected. This should
913 // be enqueued by onHotplugReceived(), and dequeued by
914 // processDisplayHotplugEventsLocked(), but then ignored as invalid.
915 mFlinger.onHotplugReceived(currentSequenceId, displayId1, HWC2::Connection::Disconnected);
916
917 // --------------------------------------------------------------------
918 // Postconditions
919
920 // The display transaction needed flag should be set.
921 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
922
923 // There should be no event queued on return, as it should have been
924 // processed.
925 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
926}
927
928/* ------------------------------------------------------------------------
Lloyd Piquea482f992018-01-22 19:00:34 -0800929 * SurfaceFlinger::createDisplay
930 */
931
932TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForNonsecureDisplay) {
933 const String8 name("virtual.test");
934
935 // --------------------------------------------------------------------
936 // Call Expectations
937
938 // The call should notify the interceptor that a display was created.
939 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
940
941 // --------------------------------------------------------------------
942 // Invocation
943
944 sp<IBinder> displayToken = mFlinger.createDisplay(name, false);
945
946 // --------------------------------------------------------------------
947 // Postconditions
948
949 // The display should have been added to the current state
950 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
951 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700952 EXPECT_TRUE(display.isVirtual());
953 EXPECT_FALSE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -0800954 EXPECT_EQ(name.string(), display.displayName);
955
956 // --------------------------------------------------------------------
957 // Cleanup conditions
958
959 // Destroying the display invalidates the display state.
960 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
961}
962
963TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForSecureDisplay) {
964 const String8 name("virtual.test");
965
966 // --------------------------------------------------------------------
967 // Call Expectations
968
969 // The call should notify the interceptor that a display was created.
970 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
971
972 // --------------------------------------------------------------------
973 // Invocation
974
975 sp<IBinder> displayToken = mFlinger.createDisplay(name, true);
976
977 // --------------------------------------------------------------------
978 // Postconditions
979
980 // The display should have been added to the current state
981 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
982 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700983 EXPECT_TRUE(display.isVirtual());
984 EXPECT_TRUE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -0800985 EXPECT_EQ(name.string(), display.displayName);
986
987 // --------------------------------------------------------------------
988 // Cleanup conditions
989
990 // Destroying the display invalidates the display state.
991 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
992}
993
994/* ------------------------------------------------------------------------
995 * SurfaceFlinger::destroyDisplay
996 */
997
998TEST_F(DisplayTransactionTest, destroyDisplayClearsCurrentStateForDisplay) {
999 using Case = NonHwcVirtualDisplayCase;
1000
1001 // --------------------------------------------------------------------
1002 // Preconditions
1003
1004 // A virtual display exists
1005 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1006 existing.inject();
1007
1008 // --------------------------------------------------------------------
1009 // Call Expectations
1010
1011 // The call should notify the interceptor that a display was created.
1012 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
1013
1014 // Destroying the display invalidates the display state.
1015 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1016
1017 // --------------------------------------------------------------------
1018 // Invocation
1019
1020 mFlinger.destroyDisplay(existing.token());
1021
1022 // --------------------------------------------------------------------
1023 // Postconditions
1024
1025 // The display should have been removed from the current state
1026 EXPECT_FALSE(hasCurrentDisplayState(existing.token()));
1027
1028 // Ths display should still exist in the drawing state
1029 EXPECT_TRUE(hasDrawingDisplayState(existing.token()));
1030
1031 // The display transaction needed flasg should be set
1032 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1033}
1034
1035TEST_F(DisplayTransactionTest, destroyDisplayHandlesUnknownDisplay) {
1036 // --------------------------------------------------------------------
1037 // Preconditions
1038
1039 sp<BBinder> displayToken = new BBinder();
1040
1041 // --------------------------------------------------------------------
1042 // Invocation
1043
1044 mFlinger.destroyDisplay(displayToken);
1045}
1046
1047/* ------------------------------------------------------------------------
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001048 * SurfaceFlinger::resetDisplayState
1049 */
1050
1051TEST_F(DisplayTransactionTest, resetDisplayStateClearsState) {
1052 using Case = NonHwcVirtualDisplayCase;
1053
1054 // --------------------------------------------------------------------
1055 // Preconditions
1056
1057 // vsync is enabled and available
1058 mFlinger.mutablePrimaryHWVsyncEnabled() = true;
1059 mFlinger.mutableHWVsyncAvailable() = true;
1060
1061 // A display exists
1062 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1063 existing.inject();
1064
1065 // --------------------------------------------------------------------
1066 // Call Expectations
1067
1068 // The call disable vsyncs
1069 EXPECT_CALL(*mEventControlThread, setVsyncEnabled(false)).Times(1);
1070
Lloyd Pique41be5d22018-06-21 13:11:48 -07001071 // The call ends any display resyncs
1072 EXPECT_CALL(*mPrimaryDispSync, endResync()).Times(1);
1073
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001074 // --------------------------------------------------------------------
1075 // Invocation
1076
1077 mFlinger.resetDisplayState();
1078
1079 // --------------------------------------------------------------------
1080 // Postconditions
1081
1082 // vsyncs should be off and not available.
1083 EXPECT_FALSE(mFlinger.mutablePrimaryHWVsyncEnabled());
1084 EXPECT_FALSE(mFlinger.mutableHWVsyncAvailable());
1085
1086 // The display should have been removed from the display map.
1087 EXPECT_FALSE(hasDisplayDevice(existing.token()));
1088
1089 // The display should still exist in the current state
1090 EXPECT_TRUE(hasCurrentDisplayState(existing.token()));
1091
1092 // The display should have been removed from the drawing state
1093 EXPECT_FALSE(hasDrawingDisplayState(existing.token()));
1094}
1095
1096/* ------------------------------------------------------------------------
Valerie Hau9758ae02018-10-09 16:05:09 -07001097 * DisplayDevice::GetBestColorMode
1098 */
1099class GetBestColorModeTest : public DisplayTransactionTest {
1100public:
Dominik Laskowski34157762018-10-31 13:07:19 -07001101 static constexpr DisplayId DEFAULT_DISPLAY_ID = DisplayId{777};
Dominik Laskowski075d3172018-05-24 15:50:06 -07001102
Valerie Hau9758ae02018-10-09 16:05:09 -07001103 GetBestColorModeTest()
1104 : DisplayTransactionTest(),
Dominik Laskowski075d3172018-05-24 15:50:06 -07001105 mInjector(FakeDisplayDeviceInjector(mFlinger, DEFAULT_DISPLAY_ID, false /* isVirtual */,
1106 true /* isPrimary */)) {}
Valerie Hau9758ae02018-10-09 16:05:09 -07001107
1108 void setHasWideColorGamut(bool hasWideColorGamut) { mHasWideColorGamut = hasWideColorGamut; }
1109
1110 void addHwcColorModesMapping(ui::ColorMode colorMode,
1111 std::vector<ui::RenderIntent> renderIntents) {
1112 mHwcColorModes[colorMode] = renderIntents;
1113 }
1114
1115 void setInputDataspace(ui::Dataspace dataspace) { mInputDataspace = dataspace; }
1116
1117 void setInputRenderIntent(ui::RenderIntent renderIntent) { mInputRenderIntent = renderIntent; }
1118
1119 void getBestColorMode() {
1120 mInjector.setHwcColorModes(mHwcColorModes);
1121 mInjector.setHasWideColorGamut(mHasWideColorGamut);
Alec Mouriba013fa2018-10-16 12:43:11 -07001122 mInjector.setNativeWindow(mNativeWindow);
1123
1124 // Creating a DisplayDevice requires getting default dimensions from the
1125 // native window.
1126 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
1127 .WillRepeatedly(DoAll(SetArgPointee<1>(1080 /* arbitrary */), Return(0)));
1128 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
1129 .WillRepeatedly(DoAll(SetArgPointee<1>(1920 /* arbitrary */), Return(0)));
Alec Mouri4efb6c22018-11-16 13:07:47 -08001130 EXPECT_CALL(*mNativeWindow, perform(9)).Times(1);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001131 EXPECT_CALL(*mNativeWindow, perform(13)).Times(1);
Alec Mouri4efb6c22018-11-16 13:07:47 -08001132 EXPECT_CALL(*mNativeWindow, perform(30)).Times(1);
Valerie Hau9758ae02018-10-09 16:05:09 -07001133 auto displayDevice = mInjector.inject();
1134
1135 displayDevice->getBestColorMode(mInputDataspace, mInputRenderIntent, &mOutDataspace,
1136 &mOutColorMode, &mOutRenderIntent);
1137 }
1138
1139 ui::Dataspace mOutDataspace;
1140 ui::ColorMode mOutColorMode;
1141 ui::RenderIntent mOutRenderIntent;
1142
1143private:
1144 ui::Dataspace mInputDataspace;
1145 ui::RenderIntent mInputRenderIntent;
1146 bool mHasWideColorGamut = false;
1147 std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> mHwcColorModes;
1148 FakeDisplayDeviceInjector mInjector;
1149};
1150
1151TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeSRGB) {
1152 addHwcColorModesMapping(ui::ColorMode::SRGB,
1153 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1154 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1155 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1156 setHasWideColorGamut(true);
1157
1158 getBestColorMode();
1159
1160 ASSERT_EQ(ui::Dataspace::SRGB, mOutDataspace);
1161 ASSERT_EQ(ui::ColorMode::SRGB, mOutColorMode);
1162 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1163}
1164
1165TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDisplayP3) {
1166 addHwcColorModesMapping(ui::ColorMode::DISPLAY_P3,
1167 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1168 addHwcColorModesMapping(ui::ColorMode::SRGB,
1169 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1170 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1171 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1172 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1173 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1174 setHasWideColorGamut(true);
1175
1176 getBestColorMode();
1177
1178 ASSERT_EQ(ui::Dataspace::DISPLAY_P3, mOutDataspace);
1179 ASSERT_EQ(ui::ColorMode::DISPLAY_P3, mOutColorMode);
1180 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1181}
1182
1183TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDISPLAY_BT2020) {
1184 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1185 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1186 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1187 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1188 setHasWideColorGamut(true);
1189
1190 getBestColorMode();
1191
1192 ASSERT_EQ(ui::Dataspace::DISPLAY_BT2020, mOutDataspace);
1193 ASSERT_EQ(ui::ColorMode::DISPLAY_BT2020, mOutColorMode);
1194 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1195}
1196
1197/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001198 * SurfaceFlinger::setupNewDisplayDeviceInternal
1199 */
1200
1201class SetupNewDisplayDeviceInternalTest : public DisplayTransactionTest {
1202public:
1203 template <typename T>
1204 void setupNewDisplayDeviceInternalTest();
1205};
1206
1207template <typename Case>
1208void SetupNewDisplayDeviceInternalTest::setupNewDisplayDeviceInternalTest() {
1209 const sp<BBinder> displayToken = new BBinder();
1210 const sp<mock::DisplaySurface> displaySurface = new mock::DisplaySurface();
1211 const sp<mock::GraphicBufferProducer> producer = new mock::GraphicBufferProducer();
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001212
1213 // --------------------------------------------------------------------
1214 // Preconditions
1215
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001216 // Wide color displays support is configured appropriately
1217 Case::WideColorSupport::injectConfigChange(this);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001218
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001219 // The display is setup with the HWC.
1220 Case::Display::injectHwcDisplay(this);
1221
1222 // SurfaceFlinger will use a test-controlled factory for native window
1223 // surfaces.
1224 injectFakeNativeWindowSurfaceFactory();
1225
1226 // --------------------------------------------------------------------
1227 // Call Expectations
1228
1229 // Various native window calls will be made.
1230 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
Lloyd Pique3c085a02018-05-09 19:38:32 -07001231 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001232 Case::WideColorSupport::setupComposerCallExpectations(this);
1233 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001234 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001235
1236 // --------------------------------------------------------------------
1237 // Invocation
1238
1239 DisplayDeviceState state;
Dominik Laskowski075d3172018-05-24 15:50:06 -07001240 state.displayId = static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1241 : Case::Display::DISPLAY_ID::get();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001242 state.isSecure = static_cast<bool>(Case::Display::SECURE);
1243
Dominik Laskowski075d3172018-05-24 15:50:06 -07001244 auto device =
1245 mFlinger.setupNewDisplayDeviceInternal(displayToken, Case::Display::DISPLAY_ID::get(),
1246 state, displaySurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001247
1248 // --------------------------------------------------------------------
1249 // Postconditions
1250
1251 ASSERT_TRUE(device != nullptr);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001252 EXPECT_EQ(Case::Display::DISPLAY_ID::get(), device->getId());
1253 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), device->isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001254 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001255 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001256 EXPECT_EQ(Case::Display::WIDTH, device->getWidth());
1257 EXPECT_EQ(Case::Display::HEIGHT, device->getHeight());
1258 EXPECT_EQ(Case::WideColorSupport::WIDE_COLOR_SUPPORTED, device->hasWideColorGamut());
1259 EXPECT_EQ(Case::HdrSupport::HDR10_SUPPORTED, device->hasHDR10Support());
1260 EXPECT_EQ(Case::HdrSupport::HDR_HLG_SUPPORTED, device->hasHLGSupport());
1261 EXPECT_EQ(Case::HdrSupport::HDR_DOLBY_VISION_SUPPORTED, device->hasDolbyVisionSupport());
Lloyd Pique3c085a02018-05-09 19:38:32 -07001262 // Note: This is not Case::Display::HWC_ACTIVE_CONFIG_ID as the ids are
1263 // remapped, and the test only ever sets up one config. If there were an error
1264 // looking up the remapped index, device->getActiveConfig() would be -1 instead.
1265 EXPECT_EQ(0, device->getActiveConfig());
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001266 EXPECT_EQ(Case::PerFrameMetadataSupport::PER_FRAME_METADATA_KEYS,
1267 device->getSupportedPerFrameMetadata());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001268}
1269
1270TEST_F(SetupNewDisplayDeviceInternalTest, createSimplePrimaryDisplay) {
1271 setupNewDisplayDeviceInternalTest<SimplePrimaryDisplayCase>();
1272}
1273
1274TEST_F(SetupNewDisplayDeviceInternalTest, createSimpleExternalDisplay) {
1275 setupNewDisplayDeviceInternalTest<SimpleExternalDisplayCase>();
1276}
1277
1278TEST_F(SetupNewDisplayDeviceInternalTest, createNonHwcVirtualDisplay) {
1279 setupNewDisplayDeviceInternalTest<NonHwcVirtualDisplayCase>();
1280}
1281
1282TEST_F(SetupNewDisplayDeviceInternalTest, createHwcVirtualDisplay) {
Dominik Laskowski075d3172018-05-24 15:50:06 -07001283 using Case = HwcVirtualDisplayCase;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001284
Dominik Laskowski075d3172018-05-24 15:50:06 -07001285 // Insert display data so that the HWC thinks it created the virtual display.
1286 const auto displayId = Case::Display::DISPLAY_ID::get();
1287 ASSERT_TRUE(displayId);
1288 mFlinger.mutableHwcDisplayData()[*displayId] = {};
1289
1290 setupNewDisplayDeviceInternalTest<Case>();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001291}
1292
1293TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3Display) {
1294 setupNewDisplayDeviceInternalTest<WideColorP3ColorimetricDisplayCase>();
1295}
1296
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001297TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10Display) {
1298 setupNewDisplayDeviceInternalTest<Hdr10DisplayCase>();
1299}
1300
1301TEST_F(SetupNewDisplayDeviceInternalTest, createHdrHlgDisplay) {
1302 setupNewDisplayDeviceInternalTest<HdrHlgDisplayCase>();
1303}
1304
1305TEST_F(SetupNewDisplayDeviceInternalTest, createHdrDolbyVisionDisplay) {
1306 setupNewDisplayDeviceInternalTest<HdrDolbyVisionDisplayCase>();
1307}
1308
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001309TEST_F(SetupNewDisplayDeviceInternalTest, createHdrSmpte2086DisplayCase) {
1310 setupNewDisplayDeviceInternalTest<HdrSmpte2086DisplayCase>();
1311}
1312
1313TEST_F(SetupNewDisplayDeviceInternalTest, createHdrCta816_3_DisplayCase) {
1314 setupNewDisplayDeviceInternalTest<HdrCta861_3_DisplayCase>();
1315}
1316
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001317/* ------------------------------------------------------------------------
1318 * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded)
1319 */
1320
1321class HandleTransactionLockedTest : public DisplayTransactionTest {
1322public:
1323 template <typename Case>
1324 void setupCommonPreconditions();
1325
1326 template <typename Case>
1327 void setupCommonCallExpectationsForConnectProcessing();
1328
1329 template <typename Case>
1330 void setupCommonCallExpectationsForDisconnectProcessing();
1331
1332 template <typename Case>
1333 void processesHotplugConnectCommon();
1334
1335 template <typename Case>
1336 void ignoresHotplugConnectCommon();
1337
1338 template <typename Case>
1339 void processesHotplugDisconnectCommon();
1340
1341 template <typename Case>
1342 void verifyDisplayIsConnected(const sp<IBinder>& displayToken);
1343
1344 template <typename Case>
1345 void verifyPhysicalDisplayIsConnected();
1346
1347 void verifyDisplayIsNotConnected(const sp<IBinder>& displayToken);
1348};
1349
1350template <typename Case>
1351void HandleTransactionLockedTest::setupCommonPreconditions() {
1352 // Wide color displays support is configured appropriately
1353 Case::WideColorSupport::injectConfigChange(this);
1354
1355 // SurfaceFlinger will use a test-controlled factory for BufferQueues
1356 injectFakeBufferQueueFactory();
1357
1358 // SurfaceFlinger will use a test-controlled factory for native window
1359 // surfaces.
1360 injectFakeNativeWindowSurfaceFactory();
1361}
1362
1363template <typename Case>
1364void HandleTransactionLockedTest::setupCommonCallExpectationsForConnectProcessing() {
1365 Case::Display::setupHwcHotplugCallExpectations(this);
1366
1367 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1368 Case::Display::setupFramebufferProducerBufferQueueCallExpectations(this);
1369 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1370 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
1371
1372 Case::WideColorSupport::setupComposerCallExpectations(this);
1373 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001374 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001375
1376 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001377 EXPECT_CALL(*mEventThread,
Dominik Laskowski075d3172018-05-24 15:50:06 -07001378 onHotplugReceived(static_cast<bool>(Case::Display::PRIMARY)
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001379 ? EventThread::DisplayType::Primary
1380 : EventThread::DisplayType::External,
1381 true))
1382 .Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001383}
1384
1385template <typename Case>
1386void HandleTransactionLockedTest::setupCommonCallExpectationsForDisconnectProcessing() {
1387 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001388 EXPECT_CALL(*mEventThread,
Dominik Laskowski075d3172018-05-24 15:50:06 -07001389 onHotplugReceived(static_cast<bool>(Case::Display::PRIMARY)
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001390 ? EventThread::DisplayType::Primary
1391 : EventThread::DisplayType::External,
1392 false))
1393 .Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001394}
1395
1396template <typename Case>
1397void HandleTransactionLockedTest::verifyDisplayIsConnected(const sp<IBinder>& displayToken) {
1398 // The display device should have been set up in the list of displays.
1399 ASSERT_TRUE(hasDisplayDevice(displayToken));
1400 const auto& device = getDisplayDevice(displayToken);
1401 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001402 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001403
1404 // The display should have been set up in the current display state
1405 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1406 const auto& current = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001407 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), current.isVirtual());
1408 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1409 : Case::Display::DISPLAY_ID::get(),
1410 current.displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001411
1412 // The display should have been set up in the drawing display state
1413 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1414 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001415 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
1416 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1417 : Case::Display::DISPLAY_ID::get(),
1418 draw.displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001419}
1420
1421template <typename Case>
1422void HandleTransactionLockedTest::verifyPhysicalDisplayIsConnected() {
1423 // HWComposer should have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001424 EXPECT_TRUE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001425
Dominik Laskowski075d3172018-05-24 15:50:06 -07001426 // SF should have a display token.
1427 const auto displayId = Case::Display::DISPLAY_ID::get();
1428 ASSERT_TRUE(displayId);
1429 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1430 auto& displayToken = mFlinger.mutablePhysicalDisplayTokens()[*displayId];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001431
1432 verifyDisplayIsConnected<Case>(displayToken);
1433}
1434
1435void HandleTransactionLockedTest::verifyDisplayIsNotConnected(const sp<IBinder>& displayToken) {
1436 EXPECT_FALSE(hasDisplayDevice(displayToken));
1437 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1438 EXPECT_FALSE(hasDrawingDisplayState(displayToken));
1439}
1440
1441template <typename Case>
1442void HandleTransactionLockedTest::processesHotplugConnectCommon() {
1443 // --------------------------------------------------------------------
1444 // Preconditions
1445
1446 setupCommonPreconditions<Case>();
1447
1448 // A hotplug connect event is enqueued for a display
1449 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001450
1451 // --------------------------------------------------------------------
1452 // Call Expectations
1453
1454 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillOnce(Return(false));
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001455
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001456 setupCommonCallExpectationsForConnectProcessing<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001457
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001458 // --------------------------------------------------------------------
1459 // Invocation
Lloyd Piquee39cad22017-12-20 17:01:29 -08001460
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001461 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
Lloyd Piquee39cad22017-12-20 17:01:29 -08001462
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001463 // --------------------------------------------------------------------
1464 // Postconditions
1465
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001466 verifyPhysicalDisplayIsConnected<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001467
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001468 // --------------------------------------------------------------------
1469 // Cleanup conditions
1470
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001471 EXPECT_CALL(*mComposer,
1472 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001473 .WillOnce(Return(Error::NONE));
1474 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Piquef58625d2017-12-19 13:22:33 -08001475}
1476
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001477template <typename Case>
1478void HandleTransactionLockedTest::ignoresHotplugConnectCommon() {
1479 // --------------------------------------------------------------------
1480 // Preconditions
1481
1482 setupCommonPreconditions<Case>();
1483
1484 // A hotplug connect event is enqueued for a display
1485 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1486
1487 // --------------------------------------------------------------------
1488 // Invocation
1489
1490 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1491
1492 // --------------------------------------------------------------------
1493 // Postconditions
1494
1495 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001496 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001497}
1498
1499template <typename Case>
1500void HandleTransactionLockedTest::processesHotplugDisconnectCommon() {
1501 // --------------------------------------------------------------------
1502 // Preconditions
1503
1504 setupCommonPreconditions<Case>();
1505
1506 // A hotplug disconnect event is enqueued for a display
1507 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1508
1509 // The display is already completely set up.
1510 Case::Display::injectHwcDisplay(this);
1511 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1512 existing.inject();
1513
1514 // --------------------------------------------------------------------
1515 // Call Expectations
1516
1517 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
Lloyd Pique438e9e72018-09-04 18:06:08 -07001518 EXPECT_CALL(*mComposer, getDisplayIdentificationData(Case::Display::HWC_DISPLAY_ID, _, _))
1519 .Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001520
1521 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1522
1523 // --------------------------------------------------------------------
1524 // Invocation
1525
1526 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1527
1528 // --------------------------------------------------------------------
1529 // Postconditions
1530
1531 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001532 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001533
Dominik Laskowski075d3172018-05-24 15:50:06 -07001534 // SF should not have a display token.
1535 const auto displayId = Case::Display::DISPLAY_ID::get();
1536 ASSERT_TRUE(displayId);
1537 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001538
1539 // The existing token should have been removed
1540 verifyDisplayIsNotConnected(existing.token());
1541}
1542
1543TEST_F(HandleTransactionLockedTest, processesHotplugConnectPrimaryDisplay) {
1544 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1545}
1546
1547TEST_F(HandleTransactionLockedTest,
1548 processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected) {
1549 // Inject an external display.
1550 ExternalDisplayVariant::injectHwcDisplay(this);
1551
1552 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1553}
1554
1555TEST_F(HandleTransactionLockedTest, processesHotplugConnectExternalDisplay) {
1556 // Inject a primary display.
1557 PrimaryDisplayVariant::injectHwcDisplay(this);
1558
1559 processesHotplugConnectCommon<SimpleExternalDisplayCase>();
1560}
1561
1562TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected) {
1563 // Inject both a primary and external display.
1564 PrimaryDisplayVariant::injectHwcDisplay(this);
1565 ExternalDisplayVariant::injectHwcDisplay(this);
1566
1567 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1568
1569 ignoresHotplugConnectCommon<SimpleTertiaryDisplayCase>();
1570}
1571
1572TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfExternalForVrComposer) {
1573 // Inject a primary display.
1574 PrimaryDisplayVariant::injectHwcDisplay(this);
1575
1576 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(true));
1577
1578 ignoresHotplugConnectCommon<SimpleExternalDisplayCase>();
1579}
1580
1581TEST_F(HandleTransactionLockedTest, processHotplugDisconnectPrimaryDisplay) {
1582 processesHotplugDisconnectCommon<SimplePrimaryDisplayCase>();
1583}
1584
1585TEST_F(HandleTransactionLockedTest, processHotplugDisconnectExternalDisplay) {
1586 processesHotplugDisconnectCommon<SimpleExternalDisplayCase>();
1587}
1588
1589TEST_F(HandleTransactionLockedTest, processesHotplugConnectThenDisconnectPrimary) {
1590 using Case = SimplePrimaryDisplayCase;
1591
1592 // --------------------------------------------------------------------
1593 // Preconditions
1594
1595 setupCommonPreconditions<Case>();
1596
1597 // A hotplug connect event is enqueued for a display
1598 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1599 // A hotplug disconnect event is also enqueued for the same display
1600 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1601
1602 // --------------------------------------------------------------------
1603 // Call Expectations
1604
1605 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1606
1607 setupCommonCallExpectationsForConnectProcessing<Case>();
1608 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1609
1610 EXPECT_CALL(*mComposer,
1611 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1612 .WillOnce(Return(Error::NONE));
1613 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1614
1615 // --------------------------------------------------------------------
1616 // Invocation
1617
1618 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1619
1620 // --------------------------------------------------------------------
1621 // Postconditions
1622
1623 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001624 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001625
Dominik Laskowski075d3172018-05-24 15:50:06 -07001626 // SF should not have a display token.
1627 const auto displayId = Case::Display::DISPLAY_ID::get();
1628 ASSERT_TRUE(displayId);
1629 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001630}
1631
1632TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectThenConnectPrimary) {
1633 using Case = SimplePrimaryDisplayCase;
1634
1635 // --------------------------------------------------------------------
1636 // Preconditions
1637
1638 setupCommonPreconditions<Case>();
1639
1640 // The display is already completely set up.
1641 Case::Display::injectHwcDisplay(this);
1642 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1643 existing.inject();
1644
1645 // A hotplug disconnect event is enqueued for a display
1646 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1647 // A hotplug connect event is also enqueued for the same display
1648 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1649
1650 // --------------------------------------------------------------------
1651 // Call Expectations
1652
1653 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1654
1655 setupCommonCallExpectationsForConnectProcessing<Case>();
1656 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1657
1658 // --------------------------------------------------------------------
1659 // Invocation
1660
1661 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1662
1663 // --------------------------------------------------------------------
1664 // Postconditions
1665
1666 // The existing token should have been removed
1667 verifyDisplayIsNotConnected(existing.token());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001668 const auto displayId = Case::Display::DISPLAY_ID::get();
1669 ASSERT_TRUE(displayId);
1670 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1671 EXPECT_NE(existing.token(), mFlinger.mutablePhysicalDisplayTokens()[*displayId]);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001672
1673 // A new display should be connected in its place
1674
1675 verifyPhysicalDisplayIsConnected<Case>();
1676
1677 // --------------------------------------------------------------------
1678 // Cleanup conditions
1679
1680 EXPECT_CALL(*mComposer,
1681 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1682 .WillOnce(Return(Error::NONE));
1683 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1684}
1685
1686TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAdded) {
1687 using Case = HwcVirtualDisplayCase;
1688
1689 // --------------------------------------------------------------------
1690 // Preconditions
1691
1692 // The HWC supports at least one virtual display
1693 injectMockComposer(1);
1694
1695 setupCommonPreconditions<Case>();
1696
1697 // A virtual display was added to the current state, and it has a
1698 // surface(producer)
1699 sp<BBinder> displayToken = new BBinder();
Lloyd Pique4c2ac022018-04-27 12:08:03 -07001700
Dominik Laskowski075d3172018-05-24 15:50:06 -07001701 DisplayDeviceState state;
1702 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001703
1704 sp<mock::GraphicBufferProducer> surface{new mock::GraphicBufferProducer()};
Dominik Laskowski075d3172018-05-24 15:50:06 -07001705 state.surface = surface;
1706 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001707
1708 // --------------------------------------------------------------------
1709 // Call Expectations
1710
1711 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1712 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1713
1714 EXPECT_CALL(*surface, query(NATIVE_WINDOW_WIDTH, _))
1715 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::WIDTH), Return(NO_ERROR)));
1716 EXPECT_CALL(*surface, query(NATIVE_WINDOW_HEIGHT, _))
1717 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::HEIGHT), Return(NO_ERROR)));
1718 EXPECT_CALL(*surface, query(NATIVE_WINDOW_FORMAT, _))
1719 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT),
1720 Return(NO_ERROR)));
1721 EXPECT_CALL(*surface, query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, _))
1722 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(NO_ERROR)));
1723
1724 EXPECT_CALL(*surface, setAsyncMode(true)).Times(1);
1725
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001726 EXPECT_CALL(*mProducer, disconnect(_, _)).Times(1);
1727
1728 Case::Display::setupHwcVirtualDisplayCreationCallExpectations(this);
1729 Case::WideColorSupport::setupComposerCallExpectations(this);
1730 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001731 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001732
1733 // --------------------------------------------------------------------
1734 // Invocation
1735
1736 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1737
1738 // --------------------------------------------------------------------
1739 // Postconditions
1740
1741 // The display device should have been set up in the list of displays.
1742 verifyDisplayIsConnected<Case>(displayToken);
1743
1744 // --------------------------------------------------------------------
1745 // Cleanup conditions
1746
1747 EXPECT_CALL(*mComposer, destroyVirtualDisplay(Case::Display::HWC_DISPLAY_ID))
1748 .WillOnce(Return(Error::NONE));
1749 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1750}
1751
1752TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAddedWithNoSurface) {
1753 using Case = HwcVirtualDisplayCase;
1754
1755 // --------------------------------------------------------------------
1756 // Preconditions
1757
1758 // The HWC supports at least one virtual display
1759 injectMockComposer(1);
1760
1761 setupCommonPreconditions<Case>();
1762
1763 // A virtual display was added to the current state, but it does not have a
1764 // surface.
1765 sp<BBinder> displayToken = new BBinder();
1766
Dominik Laskowski075d3172018-05-24 15:50:06 -07001767 DisplayDeviceState state;
1768 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001769
Dominik Laskowski075d3172018-05-24 15:50:06 -07001770 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001771
1772 // --------------------------------------------------------------------
1773 // Call Expectations
1774
1775 // --------------------------------------------------------------------
1776 // Invocation
1777
1778 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1779
1780 // --------------------------------------------------------------------
1781 // Postconditions
1782
1783 // There will not be a display device set up.
1784 EXPECT_FALSE(hasDisplayDevice(displayToken));
1785
1786 // The drawing display state will be set from the current display state.
1787 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1788 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001789 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001790}
1791
1792TEST_F(HandleTransactionLockedTest, processesVirtualDisplayRemoval) {
1793 using Case = HwcVirtualDisplayCase;
1794
1795 // --------------------------------------------------------------------
1796 // Preconditions
1797
1798 // A virtual display is set up but is removed from the current state.
Dominik Laskowski075d3172018-05-24 15:50:06 -07001799 const auto displayId = Case::Display::DISPLAY_ID::get();
1800 ASSERT_TRUE(displayId);
1801 mFlinger.mutableHwcDisplayData()[*displayId] = {};
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001802 Case::Display::injectHwcDisplay(this);
1803 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1804 existing.inject();
1805 mFlinger.mutableCurrentState().displays.removeItem(existing.token());
1806
1807 // --------------------------------------------------------------------
1808 // Call Expectations
1809
1810 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1811
1812 // --------------------------------------------------------------------
1813 // Invocation
1814
1815 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1816
1817 // --------------------------------------------------------------------
1818 // Postconditions
1819
1820 // The existing token should have been removed
1821 verifyDisplayIsNotConnected(existing.token());
1822}
1823
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001824TEST_F(HandleTransactionLockedTest, processesDisplayLayerStackChanges) {
1825 using Case = NonHwcVirtualDisplayCase;
1826
1827 constexpr uint32_t oldLayerStack = 0u;
1828 constexpr uint32_t newLayerStack = 123u;
1829
1830 // --------------------------------------------------------------------
1831 // Preconditions
1832
1833 // A display is set up
1834 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1835 display.inject();
1836
1837 // There is a change to the layerStack state
1838 display.mutableDrawingDisplayState().layerStack = oldLayerStack;
1839 display.mutableCurrentDisplayState().layerStack = newLayerStack;
1840
1841 // --------------------------------------------------------------------
1842 // Invocation
1843
1844 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1845
1846 // --------------------------------------------------------------------
1847 // Postconditions
1848
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001849 EXPECT_EQ(newLayerStack, display.mutableDisplayDevice()->getLayerStack());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001850}
1851
1852TEST_F(HandleTransactionLockedTest, processesDisplayTransformChanges) {
1853 using Case = NonHwcVirtualDisplayCase;
1854
1855 constexpr int oldTransform = 0;
1856 constexpr int newTransform = 2;
1857
1858 // --------------------------------------------------------------------
1859 // Preconditions
1860
1861 // A display is set up
1862 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1863 display.inject();
1864
1865 // There is a change to the orientation state
1866 display.mutableDrawingDisplayState().orientation = oldTransform;
1867 display.mutableCurrentDisplayState().orientation = newTransform;
1868
1869 // --------------------------------------------------------------------
1870 // Invocation
1871
1872 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1873
1874 // --------------------------------------------------------------------
1875 // Postconditions
1876
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001877 EXPECT_EQ(newTransform, display.mutableDisplayDevice()->getOrientation());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001878}
1879
1880TEST_F(HandleTransactionLockedTest, processesDisplayViewportChanges) {
1881 using Case = NonHwcVirtualDisplayCase;
1882
1883 const Rect oldViewport(0, 0, 0, 0);
1884 const Rect newViewport(0, 0, 123, 456);
1885
1886 // --------------------------------------------------------------------
1887 // Preconditions
1888
1889 // A display is set up
1890 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1891 display.inject();
1892
1893 // There is a change to the viewport state
1894 display.mutableDrawingDisplayState().viewport = oldViewport;
1895 display.mutableCurrentDisplayState().viewport = newViewport;
1896
1897 // --------------------------------------------------------------------
1898 // Invocation
1899
1900 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1901
1902 // --------------------------------------------------------------------
1903 // Postconditions
1904
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001905 EXPECT_EQ(newViewport, display.mutableDisplayDevice()->getViewport());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001906}
1907
1908TEST_F(HandleTransactionLockedTest, processesDisplayFrameChanges) {
1909 using Case = NonHwcVirtualDisplayCase;
1910
1911 const Rect oldFrame(0, 0, 0, 0);
1912 const Rect newFrame(0, 0, 123, 456);
1913
1914 // --------------------------------------------------------------------
1915 // Preconditions
1916
1917 // A display is set up
1918 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1919 display.inject();
1920
1921 // There is a change to the viewport state
1922 display.mutableDrawingDisplayState().frame = oldFrame;
1923 display.mutableCurrentDisplayState().frame = newFrame;
1924
1925 // --------------------------------------------------------------------
1926 // Invocation
1927
1928 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1929
1930 // --------------------------------------------------------------------
1931 // Postconditions
1932
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001933 EXPECT_EQ(newFrame, display.mutableDisplayDevice()->getFrame());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001934}
1935
1936TEST_F(HandleTransactionLockedTest, processesDisplayWidthChanges) {
1937 using Case = NonHwcVirtualDisplayCase;
1938
1939 constexpr int oldWidth = 0;
1940 constexpr int oldHeight = 10;
1941 constexpr int newWidth = 123;
1942
1943 // --------------------------------------------------------------------
1944 // Preconditions
1945
1946 // A display is set up
1947 auto nativeWindow = new mock::NativeWindow();
1948 auto displaySurface = new mock::DisplaySurface();
Alec Mouri05874642018-11-14 22:34:02 +00001949 auto renderSurface = new renderengine::mock::Surface();
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001950 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001951 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1952 display.setNativeWindow(nativeWindow);
1953 display.setDisplaySurface(displaySurface);
Alec Mouri05874642018-11-14 22:34:02 +00001954 display.setRenderSurface(std::unique_ptr<renderengine::Surface>(renderSurface));
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001955 // Setup injection expections
1956 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
1957 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
1958 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
1959 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
Alec Mouri4efb6c22018-11-16 13:07:47 -08001960 EXPECT_CALL(*nativeWindow, perform(9)).Times(1);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001961 EXPECT_CALL(*nativeWindow, perform(13)).Times(1);
Alec Mouri4efb6c22018-11-16 13:07:47 -08001962 EXPECT_CALL(*nativeWindow, perform(30)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001963 display.inject();
1964
1965 // There is a change to the viewport state
1966 display.mutableDrawingDisplayState().width = oldWidth;
1967 display.mutableDrawingDisplayState().height = oldHeight;
1968 display.mutableCurrentDisplayState().width = newWidth;
1969 display.mutableCurrentDisplayState().height = oldHeight;
1970
1971 // --------------------------------------------------------------------
1972 // Call Expectations
1973
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001974 EXPECT_CALL(*displaySurface, resizeBuffers(newWidth, oldHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001975
1976 // --------------------------------------------------------------------
1977 // Invocation
1978
1979 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1980}
1981
1982TEST_F(HandleTransactionLockedTest, processesDisplayHeightChanges) {
1983 using Case = NonHwcVirtualDisplayCase;
1984
1985 constexpr int oldWidth = 0;
1986 constexpr int oldHeight = 10;
1987 constexpr int newHeight = 123;
1988
1989 // --------------------------------------------------------------------
1990 // Preconditions
1991
1992 // A display is set up
1993 auto nativeWindow = new mock::NativeWindow();
1994 auto displaySurface = new mock::DisplaySurface();
Alec Mouri05874642018-11-14 22:34:02 +00001995 auto renderSurface = new renderengine::mock::Surface();
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001996 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001997 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1998 display.setNativeWindow(nativeWindow);
1999 display.setDisplaySurface(displaySurface);
Alec Mouri05874642018-11-14 22:34:02 +00002000 display.setRenderSurface(std::unique_ptr<renderengine::Surface>(renderSurface));
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002001 // Setup injection expections
2002 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
2003 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
2004 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
2005 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
Alec Mouri4efb6c22018-11-16 13:07:47 -08002006 EXPECT_CALL(*nativeWindow, perform(9)).Times(1);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002007 EXPECT_CALL(*nativeWindow, perform(13)).Times(1);
Alec Mouri4efb6c22018-11-16 13:07:47 -08002008 EXPECT_CALL(*nativeWindow, perform(30)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002009 display.inject();
2010
2011 // There is a change to the viewport state
2012 display.mutableDrawingDisplayState().width = oldWidth;
2013 display.mutableDrawingDisplayState().height = oldHeight;
2014 display.mutableCurrentDisplayState().width = oldWidth;
2015 display.mutableCurrentDisplayState().height = newHeight;
2016
2017 // --------------------------------------------------------------------
2018 // Call Expectations
2019
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002020 EXPECT_CALL(*displaySurface, resizeBuffers(oldWidth, newHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002021
2022 // --------------------------------------------------------------------
2023 // Invocation
2024
2025 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2026}
2027
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002028/* ------------------------------------------------------------------------
2029 * SurfaceFlinger::setDisplayStateLocked
2030 */
2031
2032TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithUnknownDisplay) {
2033 // --------------------------------------------------------------------
2034 // Preconditions
2035
2036 // We have an unknown display token not associated with a known display
2037 sp<BBinder> displayToken = new BBinder();
2038
2039 // The requested display state references the unknown display.
2040 DisplayState state;
2041 state.what = DisplayState::eLayerStackChanged;
2042 state.token = displayToken;
2043 state.layerStack = 456;
2044
2045 // --------------------------------------------------------------------
2046 // Invocation
2047
2048 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2049
2050 // --------------------------------------------------------------------
2051 // Postconditions
2052
2053 // The returned flags are empty
2054 EXPECT_EQ(0u, flags);
2055
2056 // The display token still doesn't match anything known.
2057 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
2058}
2059
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002060TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWhenNoChanges) {
2061 using Case = SimplePrimaryDisplayCase;
2062
2063 // --------------------------------------------------------------------
2064 // Preconditions
2065
2066 // A display is already set up
2067 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2068 display.inject();
2069
2070 // No changes are made to the display
2071 DisplayState state;
2072 state.what = 0;
2073 state.token = display.token();
2074
2075 // --------------------------------------------------------------------
2076 // Invocation
2077
2078 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2079
2080 // --------------------------------------------------------------------
2081 // Postconditions
2082
2083 // The returned flags are empty
2084 EXPECT_EQ(0u, flags);
2085}
2086
2087TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSurfaceDidNotChange) {
2088 using Case = SimplePrimaryDisplayCase;
2089
2090 // --------------------------------------------------------------------
2091 // Preconditions
2092
2093 // A display is already set up
2094 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2095 display.inject();
2096
2097 // There is a surface that can be set.
2098 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2099
2100 // The current display state has the surface set
2101 display.mutableCurrentDisplayState().surface = surface;
2102
2103 // The incoming request sets the same surface
2104 DisplayState state;
2105 state.what = DisplayState::eSurfaceChanged;
2106 state.token = display.token();
2107 state.surface = surface;
2108
2109 // --------------------------------------------------------------------
2110 // Invocation
2111
2112 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2113
2114 // --------------------------------------------------------------------
2115 // Postconditions
2116
2117 // The returned flags are empty
2118 EXPECT_EQ(0u, flags);
2119
2120 // The current display state is unchanged.
2121 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2122}
2123
2124TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfSurfaceChanged) {
2125 using Case = SimplePrimaryDisplayCase;
2126
2127 // --------------------------------------------------------------------
2128 // Preconditions
2129
2130 // A display is already set up
2131 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2132 display.inject();
2133
2134 // There is a surface that can be set.
2135 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2136
2137 // The current display state does not have a surface
2138 display.mutableCurrentDisplayState().surface = nullptr;
2139
2140 // The incoming request sets a surface
2141 DisplayState state;
2142 state.what = DisplayState::eSurfaceChanged;
2143 state.token = display.token();
2144 state.surface = surface;
2145
2146 // --------------------------------------------------------------------
2147 // Invocation
2148
2149 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2150
2151 // --------------------------------------------------------------------
2152 // Postconditions
2153
2154 // The returned flags indicate a transaction is needed
2155 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2156
2157 // The current display layer stack state is set to the new value
2158 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2159}
2160
2161TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfLayerStackDidNotChange) {
2162 using Case = SimplePrimaryDisplayCase;
2163
2164 // --------------------------------------------------------------------
2165 // Preconditions
2166
2167 // A display is already set up
2168 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2169 display.inject();
2170
2171 // The display has a layer stack set
2172 display.mutableCurrentDisplayState().layerStack = 456u;
2173
2174 // The incoming request sets the same layer stack
2175 DisplayState state;
2176 state.what = DisplayState::eLayerStackChanged;
2177 state.token = display.token();
2178 state.layerStack = 456u;
2179
2180 // --------------------------------------------------------------------
2181 // Invocation
2182
2183 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2184
2185 // --------------------------------------------------------------------
2186 // Postconditions
2187
2188 // The returned flags are empty
2189 EXPECT_EQ(0u, flags);
2190
2191 // The current display state is unchanged
2192 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2193}
2194
2195TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfLayerStackChanged) {
2196 using Case = SimplePrimaryDisplayCase;
2197
2198 // --------------------------------------------------------------------
2199 // Preconditions
2200
2201 // A display is set up
2202 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2203 display.inject();
2204
2205 // The display has a layer stack set
2206 display.mutableCurrentDisplayState().layerStack = 654u;
2207
2208 // The incoming request sets a different layer stack
2209 DisplayState state;
2210 state.what = DisplayState::eLayerStackChanged;
2211 state.token = display.token();
2212 state.layerStack = 456u;
2213
2214 // --------------------------------------------------------------------
2215 // Invocation
2216
2217 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2218
2219 // --------------------------------------------------------------------
2220 // Postconditions
2221
2222 // The returned flags indicate a transaction is needed
2223 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2224
2225 // The desired display state has been set to the new value.
2226 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2227}
2228
2229TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfProjectionDidNotChange) {
2230 using Case = SimplePrimaryDisplayCase;
2231 constexpr int initialOrientation = 180;
2232 const Rect initialFrame = {1, 2, 3, 4};
2233 const Rect initialViewport = {5, 6, 7, 8};
2234
2235 // --------------------------------------------------------------------
2236 // Preconditions
2237
2238 // A display is set up
2239 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2240 display.inject();
2241
2242 // The current display state projection state is all set
2243 display.mutableCurrentDisplayState().orientation = initialOrientation;
2244 display.mutableCurrentDisplayState().frame = initialFrame;
2245 display.mutableCurrentDisplayState().viewport = initialViewport;
2246
2247 // The incoming request sets the same projection state
2248 DisplayState state;
2249 state.what = DisplayState::eDisplayProjectionChanged;
2250 state.token = display.token();
2251 state.orientation = initialOrientation;
2252 state.frame = initialFrame;
2253 state.viewport = initialViewport;
2254
2255 // --------------------------------------------------------------------
2256 // Invocation
2257
2258 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2259
2260 // --------------------------------------------------------------------
2261 // Postconditions
2262
2263 // The returned flags are empty
2264 EXPECT_EQ(0u, flags);
2265
2266 // The current display state is unchanged
2267 EXPECT_EQ(initialOrientation, display.getCurrentDisplayState().orientation);
2268
2269 EXPECT_EQ(initialFrame, display.getCurrentDisplayState().frame);
2270 EXPECT_EQ(initialViewport, display.getCurrentDisplayState().viewport);
2271}
2272
2273TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfOrientationChanged) {
2274 using Case = SimplePrimaryDisplayCase;
2275 constexpr int initialOrientation = 90;
2276 constexpr int desiredOrientation = 180;
2277
2278 // --------------------------------------------------------------------
2279 // Preconditions
2280
2281 // A display is set up
2282 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2283 display.inject();
2284
2285 // The current display state has an orientation set
2286 display.mutableCurrentDisplayState().orientation = initialOrientation;
2287
2288 // The incoming request sets a different orientation
2289 DisplayState state;
2290 state.what = DisplayState::eDisplayProjectionChanged;
2291 state.token = display.token();
2292 state.orientation = desiredOrientation;
2293
2294 // --------------------------------------------------------------------
2295 // Invocation
2296
2297 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2298
2299 // --------------------------------------------------------------------
2300 // Postconditions
2301
2302 // The returned flags indicate a transaction is needed
2303 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2304
2305 // The current display state has the new value.
2306 EXPECT_EQ(desiredOrientation, display.getCurrentDisplayState().orientation);
2307}
2308
2309TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfFrameChanged) {
2310 using Case = SimplePrimaryDisplayCase;
2311 const Rect initialFrame = {0, 0, 0, 0};
2312 const Rect desiredFrame = {5, 6, 7, 8};
2313
2314 // --------------------------------------------------------------------
2315 // Preconditions
2316
2317 // A display is set up
2318 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2319 display.inject();
2320
2321 // The current display state does not have a frame
2322 display.mutableCurrentDisplayState().frame = initialFrame;
2323
2324 // The incoming request sets a frame
2325 DisplayState state;
2326 state.what = DisplayState::eDisplayProjectionChanged;
2327 state.token = display.token();
2328 state.frame = desiredFrame;
2329
2330 // --------------------------------------------------------------------
2331 // Invocation
2332
2333 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2334
2335 // --------------------------------------------------------------------
2336 // Postconditions
2337
2338 // The returned flags indicate a transaction is needed
2339 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2340
2341 // The current display state has the new value.
2342 EXPECT_EQ(desiredFrame, display.getCurrentDisplayState().frame);
2343}
2344
2345TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfViewportChanged) {
2346 using Case = SimplePrimaryDisplayCase;
2347 const Rect initialViewport = {0, 0, 0, 0};
2348 const Rect desiredViewport = {5, 6, 7, 8};
2349
2350 // --------------------------------------------------------------------
2351 // Preconditions
2352
2353 // A display is set up
2354 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2355 display.inject();
2356
2357 // The current display state does not have a viewport
2358 display.mutableCurrentDisplayState().viewport = initialViewport;
2359
2360 // The incoming request sets a viewport
2361 DisplayState state;
2362 state.what = DisplayState::eDisplayProjectionChanged;
2363 state.token = display.token();
2364 state.viewport = desiredViewport;
2365
2366 // --------------------------------------------------------------------
2367 // Invocation
2368
2369 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2370
2371 // --------------------------------------------------------------------
2372 // Postconditions
2373
2374 // The returned flags indicate a transaction is needed
2375 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2376
2377 // The current display state has the new value.
2378 EXPECT_EQ(desiredViewport, display.getCurrentDisplayState().viewport);
2379}
2380
2381TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSizeDidNotChange) {
2382 using Case = SimplePrimaryDisplayCase;
2383 constexpr uint32_t initialWidth = 1024;
2384 constexpr uint32_t initialHeight = 768;
2385
2386 // --------------------------------------------------------------------
2387 // Preconditions
2388
2389 // A display is set up
2390 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2391 display.inject();
2392
2393 // The current display state has a size set
2394 display.mutableCurrentDisplayState().width = initialWidth;
2395 display.mutableCurrentDisplayState().height = initialHeight;
2396
2397 // The incoming request sets the same display size
2398 DisplayState state;
2399 state.what = DisplayState::eDisplaySizeChanged;
2400 state.token = display.token();
2401 state.width = initialWidth;
2402 state.height = initialHeight;
2403
2404 // --------------------------------------------------------------------
2405 // Invocation
2406
2407 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2408
2409 // --------------------------------------------------------------------
2410 // Postconditions
2411
2412 // The returned flags are empty
2413 EXPECT_EQ(0u, flags);
2414
2415 // The current display state is unchanged
2416 EXPECT_EQ(initialWidth, display.getCurrentDisplayState().width);
2417 EXPECT_EQ(initialHeight, display.getCurrentDisplayState().height);
2418}
2419
2420TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfWidthChanged) {
2421 using Case = SimplePrimaryDisplayCase;
2422 constexpr uint32_t initialWidth = 0;
2423 constexpr uint32_t desiredWidth = 1024;
2424
2425 // --------------------------------------------------------------------
2426 // Preconditions
2427
2428 // A display is set up
2429 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2430 display.inject();
2431
2432 // The display does not yet have a width
2433 display.mutableCurrentDisplayState().width = initialWidth;
2434
2435 // The incoming request sets a display width
2436 DisplayState state;
2437 state.what = DisplayState::eDisplaySizeChanged;
2438 state.token = display.token();
2439 state.width = desiredWidth;
2440
2441 // --------------------------------------------------------------------
2442 // Invocation
2443
2444 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2445
2446 // --------------------------------------------------------------------
2447 // Postconditions
2448
2449 // The returned flags indicate a transaction is needed
2450 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2451
2452 // The current display state has the new value.
2453 EXPECT_EQ(desiredWidth, display.getCurrentDisplayState().width);
2454}
2455
2456TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfHeightChanged) {
2457 using Case = SimplePrimaryDisplayCase;
2458 constexpr uint32_t initialHeight = 0;
2459 constexpr uint32_t desiredHeight = 768;
2460
2461 // --------------------------------------------------------------------
2462 // Preconditions
2463
2464 // A display is set up
2465 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2466 display.inject();
2467
2468 // The display does not yet have a height
2469 display.mutableCurrentDisplayState().height = initialHeight;
2470
2471 // The incoming request sets a display height
2472 DisplayState state;
2473 state.what = DisplayState::eDisplaySizeChanged;
2474 state.token = display.token();
2475 state.height = desiredHeight;
2476
2477 // --------------------------------------------------------------------
2478 // Invocation
2479
2480 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2481
2482 // --------------------------------------------------------------------
2483 // Postconditions
2484
2485 // The returned flags indicate a transaction is needed
2486 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2487
2488 // The current display state has the new value.
2489 EXPECT_EQ(desiredHeight, display.getCurrentDisplayState().height);
2490}
2491
Lloyd Pique86016da2018-03-01 16:09:38 -08002492/* ------------------------------------------------------------------------
2493 * SurfaceFlinger::onInitializeDisplays
2494 */
2495
2496TEST_F(DisplayTransactionTest, onInitializeDisplaysSetsUpPrimaryDisplay) {
2497 using Case = SimplePrimaryDisplayCase;
2498
2499 // --------------------------------------------------------------------
2500 // Preconditions
2501
2502 // A primary display is set up
2503 Case::Display::injectHwcDisplay(this);
2504 auto primaryDisplay = Case::Display::makeFakeExistingDisplayInjector(this);
2505 primaryDisplay.inject();
2506
2507 // --------------------------------------------------------------------
2508 // Call Expectations
2509
2510 // We expect the surface interceptor to possibly be used, but we treat it as
2511 // disabled since it is called as a side effect rather than directly by this
2512 // function.
2513 EXPECT_CALL(*mSurfaceInterceptor, isEnabled()).WillOnce(Return(false));
2514
2515 // We expect a call to get the active display config.
2516 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
2517
2518 // We expect invalidate() to be invoked once to trigger display transaction
2519 // processing.
2520 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
2521
2522 // --------------------------------------------------------------------
2523 // Invocation
2524
2525 mFlinger.onInitializeDisplays();
2526
2527 // --------------------------------------------------------------------
2528 // Postconditions
2529
2530 // The primary display should have a current state
2531 ASSERT_TRUE(hasCurrentDisplayState(primaryDisplay.token()));
2532 const auto& primaryDisplayState = getCurrentDisplayState(primaryDisplay.token());
2533 // The layer stack state should be set to zero
2534 EXPECT_EQ(0u, primaryDisplayState.layerStack);
2535 // The orientation state should be set to zero
2536 EXPECT_EQ(0, primaryDisplayState.orientation);
2537
2538 // The frame state should be set to INVALID
2539 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.frame);
2540
2541 // The viewport state should be set to INVALID
2542 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.viewport);
2543
2544 // The width and height should both be zero
2545 EXPECT_EQ(0u, primaryDisplayState.width);
2546 EXPECT_EQ(0u, primaryDisplayState.height);
2547
2548 // The display should be set to HWC_POWER_MODE_NORMAL
2549 ASSERT_TRUE(hasDisplayDevice(primaryDisplay.token()));
2550 auto displayDevice = primaryDisplay.mutableDisplayDevice();
2551 EXPECT_EQ(HWC_POWER_MODE_NORMAL, displayDevice->getPowerMode());
2552
2553 // The display refresh period should be set in the frame tracker.
2554 FrameStats stats;
2555 mFlinger.getAnimFrameTracker().getStats(&stats);
2556 EXPECT_EQ(DEFAULT_REFRESH_RATE, stats.refreshPeriodNano);
2557
2558 // The display transaction needed flag should be set.
2559 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
2560
2561 // The compositor timing should be set to default values
2562 const auto& compositorTiming = mFlinger.getCompositorTiming();
2563 EXPECT_EQ(-DEFAULT_REFRESH_RATE, compositorTiming.deadline);
2564 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.interval);
2565 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.presentLatency);
2566}
2567
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002568/* ------------------------------------------------------------------------
2569 * SurfaceFlinger::setPowerModeInternal
2570 */
2571
2572// Used when we simulate a display that supports doze.
2573struct DozeIsSupportedVariant {
2574 static constexpr bool DOZE_SUPPORTED = true;
2575 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2576 IComposerClient::PowerMode::DOZE;
2577 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2578 IComposerClient::PowerMode::DOZE_SUSPEND;
2579};
2580
2581// Used when we simulate a display that does not support doze.
2582struct DozeNotSupportedVariant {
2583 static constexpr bool DOZE_SUPPORTED = false;
2584 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2585 IComposerClient::PowerMode::ON;
2586 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2587 IComposerClient::PowerMode::ON;
2588};
2589
2590struct EventThreadBaseSupportedVariant {
2591 static void setupEventAndEventControlThreadNoCallExpectations(DisplayTransactionTest* test) {
2592 // The event control thread should not be notified.
2593 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(_)).Times(0);
2594
2595 // The event thread should not be notified.
2596 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(0);
2597 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(0);
2598 }
2599};
2600
2601struct EventThreadNotSupportedVariant : public EventThreadBaseSupportedVariant {
2602 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2603 // These calls are only expected for the primary display.
2604
2605 // Instead expect no calls.
2606 setupEventAndEventControlThreadNoCallExpectations(test);
2607 }
2608
2609 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2610 // These calls are only expected for the primary display.
2611
2612 // Instead expect no calls.
2613 setupEventAndEventControlThreadNoCallExpectations(test);
2614 }
2615};
2616
2617struct EventThreadIsSupportedVariant : public EventThreadBaseSupportedVariant {
2618 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2619 // The event control thread should be notified to enable vsyncs
2620 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(true)).Times(1);
2621
2622 // The event thread should be notified that the screen was acquired.
2623 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(1);
2624 }
2625
2626 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2627 // There should be a call to setVsyncEnabled(false)
2628 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(false)).Times(1);
2629
2630 // The event thread should not be notified that the screen was released.
2631 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(1);
2632 }
2633};
2634
Lloyd Pique41be5d22018-06-21 13:11:48 -07002635struct DispSyncIsSupportedVariant {
2636 static void setupBeginResyncCallExpectations(DisplayTransactionTest* test) {
2637 EXPECT_CALL(*test->mPrimaryDispSync, reset()).Times(1);
2638 EXPECT_CALL(*test->mPrimaryDispSync, setPeriod(DEFAULT_REFRESH_RATE)).Times(1);
2639 EXPECT_CALL(*test->mPrimaryDispSync, beginResync()).Times(1);
2640 }
2641
2642 static void setupEndResyncCallExpectations(DisplayTransactionTest* test) {
2643 EXPECT_CALL(*test->mPrimaryDispSync, endResync()).Times(1);
2644 }
2645};
2646
2647struct DispSyncNotSupportedVariant {
2648 static void setupBeginResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2649
2650 static void setupEndResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2651};
2652
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002653// --------------------------------------------------------------------
2654// Note:
2655//
2656// There are a large number of transitions we could test, however we only test a
2657// selected subset which provides complete test coverage of the implementation.
2658// --------------------------------------------------------------------
2659
2660template <int initialPowerMode, int targetPowerMode>
2661struct TransitionVariantCommon {
2662 static constexpr auto INITIAL_POWER_MODE = initialPowerMode;
2663 static constexpr auto TARGET_POWER_MODE = targetPowerMode;
2664
2665 static void verifyPostconditions(DisplayTransactionTest*) {}
2666};
2667
2668struct TransitionOffToOnVariant
2669 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_NORMAL> {
2670 template <typename Case>
2671 static void setupCallExpectations(DisplayTransactionTest* test) {
2672 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2673 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002674 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002675 Case::setupRepaintEverythingCallExpectations(test);
2676 }
2677
2678 static void verifyPostconditions(DisplayTransactionTest* test) {
2679 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2680 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2681 }
2682};
2683
2684struct TransitionOffToDozeSuspendVariant
2685 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_DOZE_SUSPEND> {
2686 template <typename Case>
2687 static void setupCallExpectations(DisplayTransactionTest* test) {
2688 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2689 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2690 Case::setupRepaintEverythingCallExpectations(test);
2691 }
2692
2693 static void verifyPostconditions(DisplayTransactionTest* test) {
2694 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2695 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2696 }
2697};
2698
2699struct TransitionOnToOffVariant
2700 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_OFF> {
2701 template <typename Case>
2702 static void setupCallExpectations(DisplayTransactionTest* test) {
2703 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002704 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002705 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2706 }
2707
2708 static void verifyPostconditions(DisplayTransactionTest* test) {
2709 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2710 }
2711};
2712
2713struct TransitionDozeSuspendToOffVariant
2714 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_OFF> {
2715 template <typename Case>
2716 static void setupCallExpectations(DisplayTransactionTest* test) {
2717 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2718 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2719 }
2720
2721 static void verifyPostconditions(DisplayTransactionTest* test) {
2722 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2723 }
2724};
2725
2726struct TransitionOnToDozeVariant
2727 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE> {
2728 template <typename Case>
2729 static void setupCallExpectations(DisplayTransactionTest* test) {
2730 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2731 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2732 }
2733};
2734
2735struct TransitionDozeSuspendToDozeVariant
2736 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_DOZE> {
2737 template <typename Case>
2738 static void setupCallExpectations(DisplayTransactionTest* test) {
2739 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002740 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002741 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2742 }
2743};
2744
2745struct TransitionDozeToOnVariant
2746 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE, HWC_POWER_MODE_NORMAL> {
2747 template <typename Case>
2748 static void setupCallExpectations(DisplayTransactionTest* test) {
2749 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2750 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2751 }
2752};
2753
2754struct TransitionDozeSuspendToOnVariant
2755 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_NORMAL> {
2756 template <typename Case>
2757 static void setupCallExpectations(DisplayTransactionTest* test) {
2758 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002759 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002760 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2761 }
2762};
2763
2764struct TransitionOnToDozeSuspendVariant
2765 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE_SUSPEND> {
2766 template <typename Case>
2767 static void setupCallExpectations(DisplayTransactionTest* test) {
2768 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002769 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002770 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2771 }
2772};
2773
2774struct TransitionOnToUnknownVariant
2775 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_LEET> {
2776 template <typename Case>
2777 static void setupCallExpectations(DisplayTransactionTest* test) {
2778 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2779 Case::setupNoComposerPowerModeCallExpectations(test);
2780 }
2781};
2782
2783// --------------------------------------------------------------------
2784// Note:
2785//
2786// Rather than testing the cartesian product of of
2787// DozeIsSupported/DozeNotSupported with all other options, we use one for one
2788// display type, and the other for another display type.
2789// --------------------------------------------------------------------
2790
2791template <typename DisplayVariant, typename DozeVariant, typename EventThreadVariant,
Lloyd Pique41be5d22018-06-21 13:11:48 -07002792 typename DispSyncVariant, typename TransitionVariant>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002793struct DisplayPowerCase {
2794 using Display = DisplayVariant;
2795 using Doze = DozeVariant;
2796 using EventThread = EventThreadVariant;
Lloyd Pique41be5d22018-06-21 13:11:48 -07002797 using DispSync = DispSyncVariant;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002798 using Transition = TransitionVariant;
2799
2800 static auto injectDisplayWithInitialPowerMode(DisplayTransactionTest* test, int mode) {
2801 Display::injectHwcDisplay(test);
2802 auto display = Display::makeFakeExistingDisplayInjector(test);
2803 display.inject();
2804 display.mutableDisplayDevice()->setPowerMode(mode);
2805 return display;
2806 }
2807
2808 static void setInitialPrimaryHWVsyncEnabled(DisplayTransactionTest* test, bool enabled) {
2809 test->mFlinger.mutablePrimaryHWVsyncEnabled() = enabled;
2810 }
2811
2812 static void setupRepaintEverythingCallExpectations(DisplayTransactionTest* test) {
2813 EXPECT_CALL(*test->mMessageQueue, invalidate()).Times(1);
2814 }
2815
2816 static void setupSurfaceInterceptorCallExpectations(DisplayTransactionTest* test, int mode) {
2817 EXPECT_CALL(*test->mSurfaceInterceptor, isEnabled()).WillOnce(Return(true));
2818 EXPECT_CALL(*test->mSurfaceInterceptor, savePowerModeUpdate(_, mode)).Times(1);
2819 }
2820
2821 static void setupComposerCallExpectations(DisplayTransactionTest* test,
2822 IComposerClient::PowerMode mode) {
2823 // Any calls to get the active config will return a default value.
2824 EXPECT_CALL(*test->mComposer, getActiveConfig(Display::HWC_DISPLAY_ID, _))
2825 .WillRepeatedly(DoAll(SetArgPointee<1>(Display::HWC_ACTIVE_CONFIG_ID),
2826 Return(Error::NONE)));
2827
2828 // Any calls to get whether the display supports dozing will return the value set by the
2829 // policy variant.
2830 EXPECT_CALL(*test->mComposer, getDozeSupport(Display::HWC_DISPLAY_ID, _))
2831 .WillRepeatedly(DoAll(SetArgPointee<1>(Doze::DOZE_SUPPORTED), Return(Error::NONE)));
2832
2833 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, mode)).Times(1);
2834 }
2835
2836 static void setupNoComposerPowerModeCallExpectations(DisplayTransactionTest* test) {
2837 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, _)).Times(0);
2838 }
2839};
2840
2841// A sample configuration for the primary display.
2842// In addition to having event thread support, we emulate doze support.
2843template <typename TransitionVariant>
2844using PrimaryDisplayPowerCase = DisplayPowerCase<PrimaryDisplayVariant, DozeIsSupportedVariant,
Lloyd Pique41be5d22018-06-21 13:11:48 -07002845 EventThreadIsSupportedVariant,
2846 DispSyncIsSupportedVariant, TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002847
2848// A sample configuration for the external display.
2849// In addition to not having event thread support, we emulate not having doze
2850// support.
2851template <typename TransitionVariant>
Lloyd Pique41be5d22018-06-21 13:11:48 -07002852using ExternalDisplayPowerCase = DisplayPowerCase<ExternalDisplayVariant, DozeNotSupportedVariant,
2853 EventThreadNotSupportedVariant,
2854 DispSyncNotSupportedVariant, TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002855
2856class SetPowerModeInternalTest : public DisplayTransactionTest {
2857public:
2858 template <typename Case>
2859 void transitionDisplayCommon();
2860};
2861
2862template <int PowerMode>
2863struct PowerModeInitialVSyncEnabled : public std::false_type {};
2864
2865template <>
2866struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_NORMAL> : public std::true_type {};
2867
2868template <>
2869struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_DOZE> : public std::true_type {};
2870
2871template <typename Case>
2872void SetPowerModeInternalTest::transitionDisplayCommon() {
2873 // --------------------------------------------------------------------
2874 // Preconditions
2875
2876 auto display =
2877 Case::injectDisplayWithInitialPowerMode(this, Case::Transition::INITIAL_POWER_MODE);
2878 Case::setInitialPrimaryHWVsyncEnabled(this,
2879 PowerModeInitialVSyncEnabled<
2880 Case::Transition::INITIAL_POWER_MODE>::value);
2881
2882 // --------------------------------------------------------------------
2883 // Call Expectations
2884
2885 Case::setupSurfaceInterceptorCallExpectations(this, Case::Transition::TARGET_POWER_MODE);
2886 Case::Transition::template setupCallExpectations<Case>(this);
2887
2888 // --------------------------------------------------------------------
2889 // Invocation
2890
2891 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(),
2892 Case::Transition::TARGET_POWER_MODE);
2893
2894 // --------------------------------------------------------------------
2895 // Postconditions
2896
2897 Case::Transition::verifyPostconditions(this);
2898}
2899
2900TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfNoChange) {
2901 using Case = SimplePrimaryDisplayCase;
2902
2903 // --------------------------------------------------------------------
2904 // Preconditions
2905
2906 // A primary display device is set up
2907 Case::Display::injectHwcDisplay(this);
2908 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2909 display.inject();
2910
Dominik Laskowskia2edf612018-06-01 13:15:16 -07002911 // The display is already set to HWC_POWER_MODE_NORMAL
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002912 display.mutableDisplayDevice()->setPowerMode(HWC_POWER_MODE_NORMAL);
2913
2914 // --------------------------------------------------------------------
2915 // Invocation
2916
2917 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_NORMAL);
2918
2919 // --------------------------------------------------------------------
2920 // Postconditions
2921
2922 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2923}
2924
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002925TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfVirtualDisplay) {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002926 using Case = HwcVirtualDisplayCase;
2927
2928 // --------------------------------------------------------------------
2929 // Preconditions
2930
Dominik Laskowski075d3172018-05-24 15:50:06 -07002931 // Insert display data so that the HWC thinks it created the virtual display.
2932 const auto displayId = Case::Display::DISPLAY_ID::get();
2933 ASSERT_TRUE(displayId);
2934 mFlinger.mutableHwcDisplayData()[*displayId] = {};
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002935
2936 // A virtual display device is set up
2937 Case::Display::injectHwcDisplay(this);
2938 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2939 display.inject();
2940
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002941 // The display is set to HWC_POWER_MODE_NORMAL
2942 getDisplayDevice(display.token())->setPowerMode(HWC_POWER_MODE_NORMAL);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002943
2944 // --------------------------------------------------------------------
2945 // Invocation
2946
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002947 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_OFF);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002948
2949 // --------------------------------------------------------------------
2950 // Postconditions
2951
2952 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2953}
2954
2955TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnPrimaryDisplay) {
2956 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToOnVariant>>();
2957}
2958
2959TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendPrimaryDisplay) {
2960 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
2961}
2962
2963TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffPrimaryDisplay) {
2964 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToOffVariant>>();
2965}
2966
2967TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffPrimaryDisplay) {
2968 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
2969}
2970
2971TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozePrimaryDisplay) {
2972 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeVariant>>();
2973}
2974
2975TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozePrimaryDisplay) {
2976 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
2977}
2978
2979TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnPrimaryDisplay) {
2980 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeToOnVariant>>();
2981}
2982
2983TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnPrimaryDisplay) {
2984 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
2985}
2986
2987TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendPrimaryDisplay) {
2988 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
2989}
2990
2991TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownPrimaryDisplay) {
2992 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToUnknownVariant>>();
2993}
2994
2995TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnExternalDisplay) {
2996 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToOnVariant>>();
2997}
2998
2999TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendExternalDisplay) {
3000 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
3001}
3002
3003TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffExternalDisplay) {
3004 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToOffVariant>>();
3005}
3006
3007TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffExternalDisplay) {
3008 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
3009}
3010
3011TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeExternalDisplay) {
3012 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeVariant>>();
3013}
3014
3015TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozeExternalDisplay) {
3016 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
3017}
3018
3019TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnExternalDisplay) {
3020 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeToOnVariant>>();
3021}
3022
3023TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnExternalDisplay) {
3024 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
3025}
3026
3027TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendExternalDisplay) {
3028 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
3029}
3030
3031TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownExternalDisplay) {
3032 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToUnknownVariant>>();
3033}
3034
Lloyd Piquef58625d2017-12-19 13:22:33 -08003035} // namespace
3036} // namespace android