blob: a8cd8672b6f659731e77dbbeb27c204920015a72 [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();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800123
124 // These mocks are created by the test, but are destroyed by SurfaceFlinger
125 // by virtue of being stored into a std::unique_ptr. However we still need
126 // to keep a reference to them for use in setting up call expectations.
Peiyong Lin833074a2018-08-28 11:53:54 -0700127 renderengine::mock::RenderEngine* mRenderEngine = new renderengine::mock::RenderEngine();
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800128 Hwc2::mock::Composer* mComposer = nullptr;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800129 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
130 mock::SurfaceInterceptor* mSurfaceInterceptor = new mock::SurfaceInterceptor();
Lloyd Pique41be5d22018-06-21 13:11:48 -0700131 mock::DispSync* mPrimaryDispSync = new mock::DispSync();
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800132
133 // These mocks are created only when expected to be created via a factory.
134 sp<mock::GraphicBufferConsumer> mConsumer;
135 sp<mock::GraphicBufferProducer> mProducer;
Lloyd Pique22098362018-09-13 11:46:49 -0700136 surfaceflinger::mock::NativeWindowSurface* mNativeWindowSurface = nullptr;
Peiyong Lin833074a2018-08-28 11:53:54 -0700137 renderengine::mock::Surface* mRenderSurface = nullptr;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800138};
139
140DisplayTransactionTest::DisplayTransactionTest() {
141 const ::testing::TestInfo* const test_info =
142 ::testing::UnitTest::GetInstance()->current_test_info();
143 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800144
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800145 // Default to no wide color display support configured
146 mFlinger.mutableHasWideColorDisplay() = false;
Peiyong Lin13effd12018-07-24 17:01:47 -0700147 mFlinger.mutableUseColorManagement() = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800148 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
149
150 // Default to using HWC virtual displays
151 mFlinger.mutableUseHwcVirtualDisplays() = true;
152
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800153 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
154 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
155 });
156
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800157 mFlinger.setCreateNativeWindowSurface([](auto) {
158 ADD_FAILURE() << "Unexpected request to create a native window surface.";
159 return nullptr;
160 });
161
162 mFlinger.mutableEventControlThread().reset(mEventControlThread);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800163 mFlinger.mutableEventThread().reset(mEventThread);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800164 mFlinger.mutableEventQueue().reset(mMessageQueue);
Peiyong Lin833074a2018-08-28 11:53:54 -0700165 mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800166 mFlinger.mutableInterceptor().reset(mSurfaceInterceptor);
Lloyd Pique41be5d22018-06-21 13:11:48 -0700167 mFlinger.mutablePrimaryDispSync().reset(mPrimaryDispSync);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800168
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800169 injectMockComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -0800170}
171
172DisplayTransactionTest::~DisplayTransactionTest() {
173 const ::testing::TestInfo* const test_info =
174 ::testing::UnitTest::GetInstance()->current_test_info();
175 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
176}
177
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800178void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
179 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800180 EXPECT_CALL(*mComposer, getCapabilities())
181 .WillOnce(Return(std::vector<IComposer::Capability>()));
182 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
183 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -0800184
Lloyd Piquee39cad22017-12-20 17:01:29 -0800185 Mock::VerifyAndClear(mComposer);
186}
187
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800188void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800189 // This setup is only expected once per test.
190 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
191
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800192 mConsumer = new mock::GraphicBufferConsumer();
193 mProducer = new mock::GraphicBufferProducer();
194
195 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
196 *outProducer = mProducer;
197 *outConsumer = mConsumer;
198 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800199}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800200
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800201void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800202 // This setup is only expected once per test.
203 ASSERT_TRUE(mNativeWindowSurface == nullptr);
204
Lloyd Pique22098362018-09-13 11:46:49 -0700205 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800206
Lloyd Pique22098362018-09-13 11:46:49 -0700207 mFlinger.setCreateNativeWindowSurface([this](auto) {
208 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
209 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800210}
211
Dominik Laskowski075d3172018-05-24 15:50:06 -0700212bool DisplayTransactionTest::hasPhysicalHwcDisplay(hwc2_display_t hwcDisplayId) {
213 return mFlinger.mutableHwcPhysicalDisplayIdMap().count(hwcDisplayId) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800214}
215
216bool DisplayTransactionTest::hasTransactionFlagSet(int flag) {
217 return mFlinger.mutableTransactionFlags() & flag;
218}
219
220bool DisplayTransactionTest::hasDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700221 return mFlinger.mutableDisplays().count(displayToken) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800222}
223
224sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700225 return mFlinger.mutableDisplays()[displayToken];
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800226}
227
228bool DisplayTransactionTest::hasCurrentDisplayState(sp<IBinder> displayToken) {
229 return mFlinger.mutableCurrentState().displays.indexOfKey(displayToken) >= 0;
230}
231
232const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(sp<IBinder> displayToken) {
233 return mFlinger.mutableCurrentState().displays.valueFor(displayToken);
234}
235
236bool DisplayTransactionTest::hasDrawingDisplayState(sp<IBinder> displayToken) {
237 return mFlinger.mutableDrawingState().displays.indexOfKey(displayToken) >= 0;
238}
239
240const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(sp<IBinder> displayToken) {
241 return mFlinger.mutableDrawingState().displays.valueFor(displayToken);
242}
243
244/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800245 *
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800246 */
247
Dominik Laskowski075d3172018-05-24 15:50:06 -0700248template <typename PhysicalDisplay>
249struct PhysicalDisplayId {};
250
251template <DisplayId displayId>
252using VirtualDisplayId = std::integral_constant<DisplayId, displayId>;
253
254struct NoDisplayId {};
255
256template <typename>
257struct IsPhysicalDisplayId : std::bool_constant<false> {};
258
259template <typename PhysicalDisplay>
260struct IsPhysicalDisplayId<PhysicalDisplayId<PhysicalDisplay>> : std::bool_constant<true> {};
261
262template <typename>
263struct DisplayIdGetter;
264
265template <typename PhysicalDisplay>
266struct DisplayIdGetter<PhysicalDisplayId<PhysicalDisplay>> {
267 static std::optional<DisplayId> get() {
268 if (!PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
269 return getFallbackDisplayId(static_cast<bool>(PhysicalDisplay::PRIMARY)
270 ? HWC_DISPLAY_PRIMARY
271 : HWC_DISPLAY_EXTERNAL);
272 }
273
274 const auto info =
275 parseDisplayIdentificationData(PhysicalDisplay::PORT,
276 PhysicalDisplay::GET_IDENTIFICATION_DATA());
277 return info ? std::make_optional(info->id) : std::nullopt;
278 }
279};
280
281template <DisplayId displayId>
282struct DisplayIdGetter<VirtualDisplayId<displayId>> {
283 static std::optional<DisplayId> get() { return displayId; }
284};
285
286template <>
287struct DisplayIdGetter<NoDisplayId> {
288 static std::optional<DisplayId> get() { return {}; }
289};
290
291// DisplayIdType can be:
292// 1) PhysicalDisplayId<...> for generated ID of physical display backed by HWC.
293// 2) VirtualDisplayId<...> for hard-coded ID of virtual display backed by HWC.
294// 3) NoDisplayId for virtual display without HWC backing.
295template <typename DisplayIdType, int width, int height, Critical critical, Async async,
296 Secure secure, Primary primary, int grallocUsage>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800297struct DisplayVariant {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700298 using DISPLAY_ID = DisplayIdGetter<DisplayIdType>;
299
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800300 // The display width and height
301 static constexpr int WIDTH = width;
302 static constexpr int HEIGHT = height;
303
304 static constexpr int GRALLOC_USAGE = grallocUsage;
305
Dominik Laskowski075d3172018-05-24 15:50:06 -0700306 // Whether the display is virtual or physical
307 static constexpr Virtual VIRTUAL =
308 IsPhysicalDisplayId<DisplayIdType>{} ? Virtual::FALSE : Virtual::TRUE;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800309
310 // When creating native window surfaces for the framebuffer, whether those should be critical
311 static constexpr Critical CRITICAL = critical;
312
313 // When creating native window surfaces for the framebuffer, whether those should be async
314 static constexpr Async ASYNC = async;
315
316 // Whether the display should be treated as secure
317 static constexpr Secure SECURE = secure;
318
Dominik Laskowski075d3172018-05-24 15:50:06 -0700319 // Whether the display is primary
320 static constexpr Primary PRIMARY = primary;
321
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800322 static auto makeFakeExistingDisplayInjector(DisplayTransactionTest* test) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700323 auto injector =
324 FakeDisplayDeviceInjector(test->mFlinger, DISPLAY_ID::get(),
325 static_cast<bool>(VIRTUAL), static_cast<bool>(PRIMARY));
326
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800327 injector.setSecure(static_cast<bool>(SECURE));
Alec Mouriba013fa2018-10-16 12:43:11 -0700328 injector.setNativeWindow(test->mNativeWindow);
329
330 // Creating a DisplayDevice requires getting default dimensions from the
331 // native window.
332 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
333 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
334 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
335 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800336 return injector;
337 }
338
339 // Called by tests to set up any native window creation call expectations.
340 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
341 EXPECT_CALL(*test->mNativeWindowSurface, getNativeWindow())
342 .WillOnce(Return(test->mNativeWindow));
343 EXPECT_CALL(*test->mNativeWindow, perform(19)).WillRepeatedly(Return(NO_ERROR));
344
345 // For simplicity, we only expect to create a single render surface for
346 // each test.
347 ASSERT_TRUE(test->mRenderSurface == nullptr);
Peiyong Lin833074a2018-08-28 11:53:54 -0700348 test->mRenderSurface = new renderengine::mock::Surface();
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800349 EXPECT_CALL(*test->mRenderEngine, createSurface())
Peiyong Lin833074a2018-08-28 11:53:54 -0700350 .WillOnce(Return(ByMove(
351 std::unique_ptr<renderengine::Surface>(test->mRenderSurface))));
Alec Mouriba013fa2018-10-16 12:43:11 -0700352
353 // Creating a DisplayDevice requires getting default dimensions from the
354 // native window.
355 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
356 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
357 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
358 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
359
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800360 EXPECT_CALL(*test->mRenderSurface, setAsync(static_cast<bool>(ASYNC))).Times(1);
361 EXPECT_CALL(*test->mRenderSurface, setCritical(static_cast<bool>(CRITICAL))).Times(1);
362 EXPECT_CALL(*test->mRenderSurface, setNativeWindow(test->mNativeWindow.get())).Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800363 }
364
365 static void setupFramebufferConsumerBufferQueueCallExpectations(DisplayTransactionTest* test) {
366 EXPECT_CALL(*test->mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR));
367 EXPECT_CALL(*test->mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR));
368 EXPECT_CALL(*test->mConsumer, setConsumerUsageBits(GRALLOC_USAGE))
369 .WillRepeatedly(Return(NO_ERROR));
370 EXPECT_CALL(*test->mConsumer, setDefaultBufferSize(WIDTH, HEIGHT))
371 .WillRepeatedly(Return(NO_ERROR));
372 EXPECT_CALL(*test->mConsumer, setMaxAcquiredBufferCount(_))
373 .WillRepeatedly(Return(NO_ERROR));
374 }
375
376 static void setupFramebufferProducerBufferQueueCallExpectations(DisplayTransactionTest* test) {
377 EXPECT_CALL(*test->mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return());
378 }
379};
380
Dominik Laskowski075d3172018-05-24 15:50:06 -0700381template <hwc2_display_t hwcDisplayId, HWC2::DisplayType hwcDisplayType, typename DisplayVariant,
382 typename PhysicalDisplay = void>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800383struct HwcDisplayVariant {
384 // The display id supplied by the HWC
385 static constexpr hwc2_display_t HWC_DISPLAY_ID = hwcDisplayId;
386
387 // The HWC display type
388 static constexpr HWC2::DisplayType HWC_DISPLAY_TYPE = hwcDisplayType;
389
390 // The HWC active configuration id
Lloyd Pique3c085a02018-05-09 19:38:32 -0700391 static constexpr int HWC_ACTIVE_CONFIG_ID = 2001;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800392
393 static void injectPendingHotplugEvent(DisplayTransactionTest* test,
394 HWC2::Connection connection) {
395 test->mFlinger.mutablePendingHotplugEvents().emplace_back(
396 HotplugEvent{HWC_DISPLAY_ID, connection});
397 }
398
399 // Called by tests to inject a HWC display setup
400 static void injectHwcDisplay(DisplayTransactionTest* test) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700401 const auto displayId = DisplayVariant::DISPLAY_ID::get();
402 ASSERT_TRUE(displayId);
403 FakeHwcDisplayInjector(*displayId, HWC_DISPLAY_TYPE,
404 static_cast<bool>(DisplayVariant::PRIMARY))
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800405 .setHwcDisplayId(HWC_DISPLAY_ID)
406 .setWidth(DisplayVariant::WIDTH)
407 .setHeight(DisplayVariant::HEIGHT)
408 .setActiveConfig(HWC_ACTIVE_CONFIG_ID)
409 .inject(&test->mFlinger, test->mComposer);
410 }
411
412 static void setupHwcHotplugCallExpectations(DisplayTransactionTest* test) {
413 EXPECT_CALL(*test->mComposer, getDisplayType(HWC_DISPLAY_ID, _))
414 .WillOnce(DoAll(SetArgPointee<1>(static_cast<IComposerClient::DisplayType>(
415 HWC_DISPLAY_TYPE)),
416 Return(Error::NONE)));
417 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
418 EXPECT_CALL(*test->mComposer, getDisplayConfigs(HWC_DISPLAY_ID, _))
419 .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{HWC_ACTIVE_CONFIG_ID}),
420 Return(Error::NONE)));
421 EXPECT_CALL(*test->mComposer,
422 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
423 IComposerClient::Attribute::WIDTH, _))
424 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::WIDTH), Return(Error::NONE)));
425 EXPECT_CALL(*test->mComposer,
426 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
427 IComposerClient::Attribute::HEIGHT, _))
428 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::HEIGHT), Return(Error::NONE)));
429 EXPECT_CALL(*test->mComposer,
430 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
431 IComposerClient::Attribute::VSYNC_PERIOD, _))
432 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE)));
433 EXPECT_CALL(*test->mComposer,
434 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
435 IComposerClient::Attribute::DPI_X, _))
436 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
437 EXPECT_CALL(*test->mComposer,
438 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
439 IComposerClient::Attribute::DPI_Y, _))
440 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700441
442 if (PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
443 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
444 .WillOnce(DoAll(SetArgPointee<1>(PhysicalDisplay::PORT),
445 SetArgPointee<2>(PhysicalDisplay::GET_IDENTIFICATION_DATA()),
446 Return(Error::NONE)));
447 } else {
448 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
449 .WillOnce(Return(Error::UNSUPPORTED));
450 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800451 }
452
453 // Called by tests to set up HWC call expectations
454 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
455 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY_ID, _))
Lloyd Pique3c085a02018-05-09 19:38:32 -0700456 .WillRepeatedly(DoAll(SetArgPointee<1>(HWC_ACTIVE_CONFIG_ID), Return(Error::NONE)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800457 }
458};
459
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800460// Physical displays are expected to be synchronous, secure, and have a HWC display for output.
461constexpr uint32_t GRALLOC_USAGE_PHYSICAL_DISPLAY =
462 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
463
Dominik Laskowski075d3172018-05-24 15:50:06 -0700464template <hwc2_display_t hwcDisplayId, typename PhysicalDisplay, int width, int height,
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800465 Critical critical>
466struct PhysicalDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700467 : DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height, critical, Async::FALSE,
468 Secure::TRUE, PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
469 HwcDisplayVariant<hwcDisplayId, HWC2::DisplayType::Physical,
470 DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height,
471 critical, Async::FALSE, Secure::TRUE,
472 PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
473 PhysicalDisplay> {};
474
475template <bool hasIdentificationData>
476struct PrimaryDisplay {
477 static constexpr Primary PRIMARY = Primary::TRUE;
478 static constexpr uint8_t PORT = 255;
479 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
480 static constexpr auto GET_IDENTIFICATION_DATA = getInternalEdid;
481};
482
483template <bool hasIdentificationData>
484struct ExternalDisplay {
485 static constexpr Primary PRIMARY = Primary::FALSE;
486 static constexpr uint8_t PORT = 254;
487 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
488 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
489};
490
491struct TertiaryDisplay {
492 static constexpr Primary PRIMARY = Primary::FALSE;
493};
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800494
495// A primary display is a physical display that is critical
496using PrimaryDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700497 PhysicalDisplayVariant<1001, PrimaryDisplay<false>, 3840, 2160, Critical::TRUE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800498
499// An external display is physical display that is not critical.
500using ExternalDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700501 PhysicalDisplayVariant<1002, ExternalDisplay<false>, 1920, 1280, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800502
503using TertiaryDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700504 PhysicalDisplayVariant<1003, TertiaryDisplay, 1600, 1200, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800505
506// A virtual display not supported by the HWC.
507constexpr uint32_t GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY = 0;
508
509template <int width, int height, Secure secure>
510struct NonHwcVirtualDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700511 : DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
512 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY> {
513 using Base = DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
514 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>;
515
516 static void injectHwcDisplay(DisplayTransactionTest*) {}
517
518 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
519 EXPECT_CALL(*test->mComposer, getActiveConfig(_, _)).Times(0);
520 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800521
522 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
523 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
524 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
525 }
526};
527
528// A virtual display supported by the HWC.
529constexpr uint32_t GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY = GRALLOC_USAGE_HW_COMPOSER;
530
531template <int width, int height, Secure secure>
532struct HwcVirtualDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700533 : DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE, secure,
534 Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>,
535 HwcDisplayVariant<
536 1010, HWC2::DisplayType::Virtual,
537 DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
538 secure, Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>> {
539 using Base = DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
540 secure, Primary::FALSE, GRALLOC_USAGE_HW_COMPOSER>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800541 using Self = HwcVirtualDisplayVariant<width, height, secure>;
542
543 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
544 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
545 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
546 }
547
548 static void setupHwcVirtualDisplayCreationCallExpectations(DisplayTransactionTest* test) {
549 EXPECT_CALL(*test->mComposer, createVirtualDisplay(Base::WIDTH, Base::HEIGHT, _, _))
550 .WillOnce(DoAll(SetArgPointee<3>(Self::HWC_DISPLAY_ID), Return(Error::NONE)));
551 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
552 }
553};
554
555// For this variant, SurfaceFlinger should not configure itself with wide
556// display support, so the display should not be configured for wide-color
557// support.
558struct WideColorSupportNotConfiguredVariant {
559 static constexpr bool WIDE_COLOR_SUPPORTED = false;
560
561 static void injectConfigChange(DisplayTransactionTest* test) {
562 test->mFlinger.mutableHasWideColorDisplay() = false;
Peiyong Lin13effd12018-07-24 17:01:47 -0700563 test->mFlinger.mutableUseColorManagement() = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800564 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
565 }
566
567 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
568 EXPECT_CALL(*test->mComposer, getColorModes(_, _)).Times(0);
569 EXPECT_CALL(*test->mComposer, getRenderIntents(_, _, _)).Times(0);
570 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
571 }
572};
573
574// For this variant, SurfaceFlinger should configure itself with wide display
575// support, and the display should respond with an non-empty list of supported
576// color modes. Wide-color support should be configured.
577template <typename Display>
578struct WideColorP3ColorimetricSupportedVariant {
579 static constexpr bool WIDE_COLOR_SUPPORTED = true;
580
581 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700582 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800583 test->mFlinger.mutableHasWideColorDisplay() = true;
584 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
585 }
586
587 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
588 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
589 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>({ColorMode::DISPLAY_P3})),
590 Return(Error::NONE)));
591 EXPECT_CALL(*test->mComposer,
592 getRenderIntents(Display::HWC_DISPLAY_ID, ColorMode::DISPLAY_P3, _))
593 .WillOnce(DoAll(SetArgPointee<2>(
594 std::vector<RenderIntent>({RenderIntent::COLORIMETRIC})),
595 Return(Error::NONE)));
596 EXPECT_CALL(*test->mComposer,
597 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::SRGB,
598 RenderIntent::COLORIMETRIC))
599 .WillOnce(Return(Error::NONE));
600 }
601};
602
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800603// For this variant, SurfaceFlinger should configure itself with wide display
604// support, but the display should respond with an empty list of supported color
605// modes. Wide-color support for the display should not be configured.
606template <typename Display>
607struct WideColorNotSupportedVariant {
608 static constexpr bool WIDE_COLOR_SUPPORTED = false;
609
610 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700611 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800612 test->mFlinger.mutableHasWideColorDisplay() = true;
613 }
614
615 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
616 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
617 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>()), Return(Error::NONE)));
Chia-I Wu614e1422018-05-23 02:17:03 -0700618 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800619 }
620};
621
622// For this variant, the display is not a HWC display, so no HDR support should
623// be configured.
624struct NonHwcDisplayHdrSupportVariant {
625 static constexpr bool HDR10_SUPPORTED = false;
626 static constexpr bool HDR_HLG_SUPPORTED = false;
627 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
628 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
629 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
630 }
631};
632
633// For this variant, the composer should respond with a non-empty list of HDR
634// modes containing HDR10, so HDR10 support should be configured.
635template <typename Display>
636struct Hdr10SupportedVariant {
637 static constexpr bool HDR10_SUPPORTED = true;
638 static constexpr bool HDR_HLG_SUPPORTED = false;
639 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
640 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
641 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
642 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HDR10})),
643 Return(Error::NONE)));
644 }
645};
646
647// For this variant, the composer should respond with a non-empty list of HDR
648// modes containing HLG, so HLG support should be configured.
649template <typename Display>
650struct HdrHlgSupportedVariant {
651 static constexpr bool HDR10_SUPPORTED = false;
652 static constexpr bool HDR_HLG_SUPPORTED = true;
653 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
654 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
655 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
656 .WillOnce(
657 DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HLG})), Return(Error::NONE)));
658 }
659};
660
661// For this variant, the composer should respond with a non-empty list of HDR
662// modes containing DOLBY_VISION, so DOLBY_VISION support should be configured.
663template <typename Display>
664struct HdrDolbyVisionSupportedVariant {
665 static constexpr bool HDR10_SUPPORTED = false;
666 static constexpr bool HDR_HLG_SUPPORTED = false;
667 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = true;
668 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
669 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
670 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::DOLBY_VISION})),
671 Return(Error::NONE)));
672 }
673};
674
675// For this variant, the composer should respond with am empty list of HDR
676// modes, so no HDR support should be configured.
677template <typename Display>
678struct HdrNotSupportedVariant {
679 static constexpr bool HDR10_SUPPORTED = false;
680 static constexpr bool HDR_HLG_SUPPORTED = false;
681 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
682 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
683 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
684 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
685 }
686};
687
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700688struct NonHwcPerFrameMetadataSupportVariant {
689 static constexpr int PER_FRAME_METADATA_KEYS = 0;
690 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800691 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(_)).Times(0);
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700692 }
693};
694
695template <typename Display>
696struct NoPerFrameMetadataSupportVariant {
697 static constexpr int PER_FRAME_METADATA_KEYS = 0;
698 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800699 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
700 .WillOnce(Return(std::vector<PerFrameMetadataKey>()));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700701 }
702};
703
704template <typename Display>
705struct Smpte2086PerFrameMetadataSupportVariant {
706 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::SMPTE2086;
707 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800708 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
709 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700710 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X,
711 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y,
712 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X,
713 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y,
714 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X,
715 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y,
716 PerFrameMetadataKey::WHITE_POINT_X,
717 PerFrameMetadataKey::WHITE_POINT_Y,
718 PerFrameMetadataKey::MAX_LUMINANCE,
719 PerFrameMetadataKey::MIN_LUMINANCE,
Chia-I Wud7e01d72018-06-21 13:39:09 +0800720 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700721 }
722};
723
724template <typename Display>
725struct Cta861_3_PerFrameMetadataSupportVariant {
726 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::CTA861_3;
727 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800728 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
729 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700730 PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL,
731 PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL,
Chia-I Wud7e01d72018-06-21 13:39:09 +0800732 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700733 }
734};
735
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800736/* ------------------------------------------------------------------------
737 * Typical display configurations to test
738 */
739
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700740template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy,
741 typename PerFrameMetadataSupportPolicy>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800742struct Case {
743 using Display = DisplayPolicy;
744 using WideColorSupport = WideColorSupportPolicy;
745 using HdrSupport = HdrSupportPolicy;
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700746 using PerFrameMetadataSupport = PerFrameMetadataSupportPolicy;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800747};
748
749using SimplePrimaryDisplayCase =
750 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700751 HdrNotSupportedVariant<PrimaryDisplayVariant>,
752 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800753using SimpleExternalDisplayCase =
754 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700755 HdrNotSupportedVariant<ExternalDisplayVariant>,
756 NoPerFrameMetadataSupportVariant<ExternalDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800757using SimpleTertiaryDisplayCase =
758 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700759 HdrNotSupportedVariant<TertiaryDisplayVariant>,
760 NoPerFrameMetadataSupportVariant<TertiaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800761using NonHwcVirtualDisplayCase =
762 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700763 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant,
764 NonHwcPerFrameMetadataSupportVariant>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800765using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
766using HwcVirtualDisplayCase =
767 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
Lloyd Pique438e9e72018-09-04 18:06:08 -0700768 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>,
tangrobin6753a022018-08-10 10:58:54 +0800769 NoPerFrameMetadataSupportVariant<SimpleHwcVirtualDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800770using WideColorP3ColorimetricDisplayCase =
771 Case<PrimaryDisplayVariant, WideColorP3ColorimetricSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700772 HdrNotSupportedVariant<PrimaryDisplayVariant>,
773 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800774using Hdr10DisplayCase =
775 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700776 Hdr10SupportedVariant<PrimaryDisplayVariant>,
777 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800778using HdrHlgDisplayCase =
779 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700780 HdrHlgSupportedVariant<PrimaryDisplayVariant>,
781 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800782using HdrDolbyVisionDisplayCase =
783 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700784 HdrDolbyVisionSupportedVariant<PrimaryDisplayVariant>,
785 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
786using HdrSmpte2086DisplayCase =
787 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
788 HdrNotSupportedVariant<PrimaryDisplayVariant>,
789 Smpte2086PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
790using HdrCta861_3_DisplayCase =
791 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
792 HdrNotSupportedVariant<PrimaryDisplayVariant>,
793 Cta861_3_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Dominik Laskowskif07b85b2018-06-11 12:49:15 -0700794
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800795/* ------------------------------------------------------------------------
Lloyd Pique6cf11032018-01-22 18:57:44 -0800796 *
797 * SurfaceFlinger::onHotplugReceived
798 */
799
800TEST_F(DisplayTransactionTest, hotplugEnqueuesEventsForDisplayTransaction) {
801 constexpr int currentSequenceId = 123;
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700802 constexpr hwc2_display_t hwcDisplayId1 = 456;
803 constexpr hwc2_display_t hwcDisplayId2 = 654;
Lloyd Pique6cf11032018-01-22 18:57:44 -0800804
805 // --------------------------------------------------------------------
806 // Preconditions
807
808 // Set the current sequence id for accepted events
809 mFlinger.mutableComposerSequenceId() = currentSequenceId;
810
811 // Set the main thread id so that the current thread does not appear to be
812 // the main thread.
813 mFlinger.mutableMainThreadId() = std::thread::id();
814
815 // --------------------------------------------------------------------
816 // Call Expectations
817
818 // We expect invalidate() to be invoked once to trigger display transaction
819 // processing.
820 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
821
822 // --------------------------------------------------------------------
823 // Invocation
824
825 // Simulate two hotplug events (a connect and a disconnect)
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700826 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId1, HWC2::Connection::Connected);
827 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId2, HWC2::Connection::Disconnected);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800828
829 // --------------------------------------------------------------------
830 // Postconditions
831
832 // The display transaction needed flag should be set.
833 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
834
835 // All events should be in the pending event queue.
836 const auto& pendingEvents = mFlinger.mutablePendingHotplugEvents();
837 ASSERT_EQ(2u, pendingEvents.size());
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700838 EXPECT_EQ(hwcDisplayId1, pendingEvents[0].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800839 EXPECT_EQ(HWC2::Connection::Connected, pendingEvents[0].connection);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700840 EXPECT_EQ(hwcDisplayId2, pendingEvents[1].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800841 EXPECT_EQ(HWC2::Connection::Disconnected, pendingEvents[1].connection);
842}
843
844TEST_F(DisplayTransactionTest, hotplugDiscardsUnexpectedEvents) {
845 constexpr int currentSequenceId = 123;
846 constexpr int otherSequenceId = 321;
847 constexpr hwc2_display_t displayId = 456;
848
849 // --------------------------------------------------------------------
850 // Preconditions
851
852 // Set the current sequence id for accepted events
853 mFlinger.mutableComposerSequenceId() = currentSequenceId;
854
855 // Set the main thread id so that the current thread does not appear to be
856 // the main thread.
857 mFlinger.mutableMainThreadId() = std::thread::id();
858
859 // --------------------------------------------------------------------
860 // Call Expectations
861
862 // We do not expect any calls to invalidate().
863 EXPECT_CALL(*mMessageQueue, invalidate()).Times(0);
864
865 // --------------------------------------------------------------------
866 // Invocation
867
868 // Call with an unexpected sequence id
869 mFlinger.onHotplugReceived(otherSequenceId, displayId, HWC2::Connection::Invalid);
870
871 // --------------------------------------------------------------------
872 // Postconditions
873
874 // The display transaction needed flag should not be set
875 EXPECT_FALSE(hasTransactionFlagSet(eDisplayTransactionNeeded));
876
877 // There should be no pending events
878 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
879}
880
881TEST_F(DisplayTransactionTest, hotplugProcessesEnqueuedEventsIfCalledOnMainThread) {
882 constexpr int currentSequenceId = 123;
883 constexpr hwc2_display_t displayId1 = 456;
884
885 // --------------------------------------------------------------------
886 // Note:
887 // --------------------------------------------------------------------
888 // This test case is a bit tricky. We want to verify that
889 // onHotplugReceived() calls processDisplayHotplugEventsLocked(), but we
890 // don't really want to provide coverage for everything the later function
891 // does as there are specific tests for it.
892 // --------------------------------------------------------------------
893
894 // --------------------------------------------------------------------
895 // Preconditions
896
897 // Set the current sequence id for accepted events
898 mFlinger.mutableComposerSequenceId() = currentSequenceId;
899
900 // Set the main thread id so that the current thread does appear to be the
901 // main thread.
902 mFlinger.mutableMainThreadId() = std::this_thread::get_id();
903
904 // --------------------------------------------------------------------
905 // Call Expectations
906
907 // We expect invalidate() to be invoked once to trigger display transaction
908 // processing.
909 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
910
911 // --------------------------------------------------------------------
912 // Invocation
913
914 // Simulate a disconnect on a display id that is not connected. This should
915 // be enqueued by onHotplugReceived(), and dequeued by
916 // processDisplayHotplugEventsLocked(), but then ignored as invalid.
917 mFlinger.onHotplugReceived(currentSequenceId, displayId1, HWC2::Connection::Disconnected);
918
919 // --------------------------------------------------------------------
920 // Postconditions
921
922 // The display transaction needed flag should be set.
923 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
924
925 // There should be no event queued on return, as it should have been
926 // processed.
927 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
928}
929
930/* ------------------------------------------------------------------------
Lloyd Piquea482f992018-01-22 19:00:34 -0800931 * SurfaceFlinger::createDisplay
932 */
933
934TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForNonsecureDisplay) {
935 const String8 name("virtual.test");
936
937 // --------------------------------------------------------------------
938 // Call Expectations
939
940 // The call should notify the interceptor that a display was created.
941 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
942
943 // --------------------------------------------------------------------
944 // Invocation
945
946 sp<IBinder> displayToken = mFlinger.createDisplay(name, false);
947
948 // --------------------------------------------------------------------
949 // Postconditions
950
951 // The display should have been added to the current state
952 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
953 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700954 EXPECT_TRUE(display.isVirtual());
955 EXPECT_FALSE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -0800956 EXPECT_EQ(name.string(), display.displayName);
957
958 // --------------------------------------------------------------------
959 // Cleanup conditions
960
961 // Destroying the display invalidates the display state.
962 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
963}
964
965TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForSecureDisplay) {
966 const String8 name("virtual.test");
967
968 // --------------------------------------------------------------------
969 // Call Expectations
970
971 // The call should notify the interceptor that a display was created.
972 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
973
974 // --------------------------------------------------------------------
975 // Invocation
976
977 sp<IBinder> displayToken = mFlinger.createDisplay(name, true);
978
979 // --------------------------------------------------------------------
980 // Postconditions
981
982 // The display should have been added to the current state
983 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
984 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700985 EXPECT_TRUE(display.isVirtual());
986 EXPECT_TRUE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -0800987 EXPECT_EQ(name.string(), display.displayName);
988
989 // --------------------------------------------------------------------
990 // Cleanup conditions
991
992 // Destroying the display invalidates the display state.
993 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
994}
995
996/* ------------------------------------------------------------------------
997 * SurfaceFlinger::destroyDisplay
998 */
999
1000TEST_F(DisplayTransactionTest, destroyDisplayClearsCurrentStateForDisplay) {
1001 using Case = NonHwcVirtualDisplayCase;
1002
1003 // --------------------------------------------------------------------
1004 // Preconditions
1005
1006 // A virtual display exists
1007 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1008 existing.inject();
1009
1010 // --------------------------------------------------------------------
1011 // Call Expectations
1012
1013 // The call should notify the interceptor that a display was created.
1014 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
1015
1016 // Destroying the display invalidates the display state.
1017 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1018
1019 // --------------------------------------------------------------------
1020 // Invocation
1021
1022 mFlinger.destroyDisplay(existing.token());
1023
1024 // --------------------------------------------------------------------
1025 // Postconditions
1026
1027 // The display should have been removed from the current state
1028 EXPECT_FALSE(hasCurrentDisplayState(existing.token()));
1029
1030 // Ths display should still exist in the drawing state
1031 EXPECT_TRUE(hasDrawingDisplayState(existing.token()));
1032
1033 // The display transaction needed flasg should be set
1034 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1035}
1036
1037TEST_F(DisplayTransactionTest, destroyDisplayHandlesUnknownDisplay) {
1038 // --------------------------------------------------------------------
1039 // Preconditions
1040
1041 sp<BBinder> displayToken = new BBinder();
1042
1043 // --------------------------------------------------------------------
1044 // Invocation
1045
1046 mFlinger.destroyDisplay(displayToken);
1047}
1048
1049/* ------------------------------------------------------------------------
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001050 * SurfaceFlinger::resetDisplayState
1051 */
1052
1053TEST_F(DisplayTransactionTest, resetDisplayStateClearsState) {
1054 using Case = NonHwcVirtualDisplayCase;
1055
1056 // --------------------------------------------------------------------
1057 // Preconditions
1058
1059 // vsync is enabled and available
1060 mFlinger.mutablePrimaryHWVsyncEnabled() = true;
1061 mFlinger.mutableHWVsyncAvailable() = true;
1062
1063 // A display exists
1064 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1065 existing.inject();
1066
1067 // --------------------------------------------------------------------
1068 // Call Expectations
1069
1070 // The call disable vsyncs
1071 EXPECT_CALL(*mEventControlThread, setVsyncEnabled(false)).Times(1);
1072
1073 // The call clears the current render engine surface
1074 EXPECT_CALL(*mRenderEngine, resetCurrentSurface());
1075
Lloyd Pique41be5d22018-06-21 13:11:48 -07001076 // The call ends any display resyncs
1077 EXPECT_CALL(*mPrimaryDispSync, endResync()).Times(1);
1078
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001079 // --------------------------------------------------------------------
1080 // Invocation
1081
1082 mFlinger.resetDisplayState();
1083
1084 // --------------------------------------------------------------------
1085 // Postconditions
1086
1087 // vsyncs should be off and not available.
1088 EXPECT_FALSE(mFlinger.mutablePrimaryHWVsyncEnabled());
1089 EXPECT_FALSE(mFlinger.mutableHWVsyncAvailable());
1090
1091 // The display should have been removed from the display map.
1092 EXPECT_FALSE(hasDisplayDevice(existing.token()));
1093
1094 // The display should still exist in the current state
1095 EXPECT_TRUE(hasCurrentDisplayState(existing.token()));
1096
1097 // The display should have been removed from the drawing state
1098 EXPECT_FALSE(hasDrawingDisplayState(existing.token()));
1099}
1100
1101/* ------------------------------------------------------------------------
Valerie Hau9758ae02018-10-09 16:05:09 -07001102 * DisplayDevice::GetBestColorMode
1103 */
1104class GetBestColorModeTest : public DisplayTransactionTest {
1105public:
Dominik Laskowski075d3172018-05-24 15:50:06 -07001106 static constexpr DisplayId DEFAULT_DISPLAY_ID = 777;
1107
Valerie Hau9758ae02018-10-09 16:05:09 -07001108 GetBestColorModeTest()
1109 : DisplayTransactionTest(),
Dominik Laskowski075d3172018-05-24 15:50:06 -07001110 mInjector(FakeDisplayDeviceInjector(mFlinger, DEFAULT_DISPLAY_ID, false /* isVirtual */,
1111 true /* isPrimary */)) {}
Valerie Hau9758ae02018-10-09 16:05:09 -07001112
1113 void setHasWideColorGamut(bool hasWideColorGamut) { mHasWideColorGamut = hasWideColorGamut; }
1114
1115 void addHwcColorModesMapping(ui::ColorMode colorMode,
1116 std::vector<ui::RenderIntent> renderIntents) {
1117 mHwcColorModes[colorMode] = renderIntents;
1118 }
1119
1120 void setInputDataspace(ui::Dataspace dataspace) { mInputDataspace = dataspace; }
1121
1122 void setInputRenderIntent(ui::RenderIntent renderIntent) { mInputRenderIntent = renderIntent; }
1123
1124 void getBestColorMode() {
1125 mInjector.setHwcColorModes(mHwcColorModes);
1126 mInjector.setHasWideColorGamut(mHasWideColorGamut);
Alec Mouriba013fa2018-10-16 12:43:11 -07001127 mInjector.setNativeWindow(mNativeWindow);
1128
1129 // Creating a DisplayDevice requires getting default dimensions from the
1130 // native window.
1131 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
1132 .WillRepeatedly(DoAll(SetArgPointee<1>(1080 /* arbitrary */), Return(0)));
1133 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
1134 .WillRepeatedly(DoAll(SetArgPointee<1>(1920 /* arbitrary */), Return(0)));
Valerie Hau9758ae02018-10-09 16:05:09 -07001135 auto displayDevice = mInjector.inject();
1136
1137 displayDevice->getBestColorMode(mInputDataspace, mInputRenderIntent, &mOutDataspace,
1138 &mOutColorMode, &mOutRenderIntent);
1139 }
1140
1141 ui::Dataspace mOutDataspace;
1142 ui::ColorMode mOutColorMode;
1143 ui::RenderIntent mOutRenderIntent;
1144
1145private:
1146 ui::Dataspace mInputDataspace;
1147 ui::RenderIntent mInputRenderIntent;
1148 bool mHasWideColorGamut = false;
1149 std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> mHwcColorModes;
1150 FakeDisplayDeviceInjector mInjector;
1151};
1152
1153TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeSRGB) {
1154 addHwcColorModesMapping(ui::ColorMode::SRGB,
1155 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1156 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1157 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1158 setHasWideColorGamut(true);
1159
1160 getBestColorMode();
1161
1162 ASSERT_EQ(ui::Dataspace::SRGB, mOutDataspace);
1163 ASSERT_EQ(ui::ColorMode::SRGB, mOutColorMode);
1164 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1165}
1166
1167TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDisplayP3) {
1168 addHwcColorModesMapping(ui::ColorMode::DISPLAY_P3,
1169 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1170 addHwcColorModesMapping(ui::ColorMode::SRGB,
1171 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1172 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1173 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1174 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1175 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1176 setHasWideColorGamut(true);
1177
1178 getBestColorMode();
1179
1180 ASSERT_EQ(ui::Dataspace::DISPLAY_P3, mOutDataspace);
1181 ASSERT_EQ(ui::ColorMode::DISPLAY_P3, mOutColorMode);
1182 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1183}
1184
1185TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDISPLAY_BT2020) {
1186 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1187 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1188 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1189 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1190 setHasWideColorGamut(true);
1191
1192 getBestColorMode();
1193
1194 ASSERT_EQ(ui::Dataspace::DISPLAY_BT2020, mOutDataspace);
1195 ASSERT_EQ(ui::ColorMode::DISPLAY_BT2020, mOutColorMode);
1196 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1197}
1198
1199/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001200 * SurfaceFlinger::setupNewDisplayDeviceInternal
1201 */
1202
1203class SetupNewDisplayDeviceInternalTest : public DisplayTransactionTest {
1204public:
1205 template <typename T>
1206 void setupNewDisplayDeviceInternalTest();
1207};
1208
1209template <typename Case>
1210void SetupNewDisplayDeviceInternalTest::setupNewDisplayDeviceInternalTest() {
1211 const sp<BBinder> displayToken = new BBinder();
1212 const sp<mock::DisplaySurface> displaySurface = new mock::DisplaySurface();
1213 const sp<mock::GraphicBufferProducer> producer = new mock::GraphicBufferProducer();
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001214
1215 // --------------------------------------------------------------------
1216 // Preconditions
1217
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001218 // Wide color displays support is configured appropriately
1219 Case::WideColorSupport::injectConfigChange(this);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001220
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001221 // The display is setup with the HWC.
1222 Case::Display::injectHwcDisplay(this);
1223
1224 // SurfaceFlinger will use a test-controlled factory for native window
1225 // surfaces.
1226 injectFakeNativeWindowSurfaceFactory();
1227
1228 // --------------------------------------------------------------------
1229 // Call Expectations
1230
1231 // Various native window calls will be made.
1232 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
Lloyd Pique3c085a02018-05-09 19:38:32 -07001233 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001234 Case::WideColorSupport::setupComposerCallExpectations(this);
1235 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001236 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001237
1238 // --------------------------------------------------------------------
1239 // Invocation
1240
1241 DisplayDeviceState state;
Dominik Laskowski075d3172018-05-24 15:50:06 -07001242 state.displayId = static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1243 : Case::Display::DISPLAY_ID::get();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001244 state.isSecure = static_cast<bool>(Case::Display::SECURE);
1245
Dominik Laskowski075d3172018-05-24 15:50:06 -07001246 auto device =
1247 mFlinger.setupNewDisplayDeviceInternal(displayToken, Case::Display::DISPLAY_ID::get(),
1248 state, displaySurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001249
1250 // --------------------------------------------------------------------
1251 // Postconditions
1252
1253 ASSERT_TRUE(device != nullptr);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001254 EXPECT_EQ(Case::Display::DISPLAY_ID::get(), device->getId());
1255 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), device->isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001256 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001257 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001258 EXPECT_EQ(Case::Display::WIDTH, device->getWidth());
1259 EXPECT_EQ(Case::Display::HEIGHT, device->getHeight());
1260 EXPECT_EQ(Case::WideColorSupport::WIDE_COLOR_SUPPORTED, device->hasWideColorGamut());
1261 EXPECT_EQ(Case::HdrSupport::HDR10_SUPPORTED, device->hasHDR10Support());
1262 EXPECT_EQ(Case::HdrSupport::HDR_HLG_SUPPORTED, device->hasHLGSupport());
1263 EXPECT_EQ(Case::HdrSupport::HDR_DOLBY_VISION_SUPPORTED, device->hasDolbyVisionSupport());
Lloyd Pique3c085a02018-05-09 19:38:32 -07001264 // Note: This is not Case::Display::HWC_ACTIVE_CONFIG_ID as the ids are
1265 // remapped, and the test only ever sets up one config. If there were an error
1266 // looking up the remapped index, device->getActiveConfig() would be -1 instead.
1267 EXPECT_EQ(0, device->getActiveConfig());
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001268 EXPECT_EQ(Case::PerFrameMetadataSupport::PER_FRAME_METADATA_KEYS,
1269 device->getSupportedPerFrameMetadata());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001270}
1271
1272TEST_F(SetupNewDisplayDeviceInternalTest, createSimplePrimaryDisplay) {
1273 setupNewDisplayDeviceInternalTest<SimplePrimaryDisplayCase>();
1274}
1275
1276TEST_F(SetupNewDisplayDeviceInternalTest, createSimpleExternalDisplay) {
1277 setupNewDisplayDeviceInternalTest<SimpleExternalDisplayCase>();
1278}
1279
1280TEST_F(SetupNewDisplayDeviceInternalTest, createNonHwcVirtualDisplay) {
1281 setupNewDisplayDeviceInternalTest<NonHwcVirtualDisplayCase>();
1282}
1283
1284TEST_F(SetupNewDisplayDeviceInternalTest, createHwcVirtualDisplay) {
Dominik Laskowski075d3172018-05-24 15:50:06 -07001285 using Case = HwcVirtualDisplayCase;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001286
Dominik Laskowski075d3172018-05-24 15:50:06 -07001287 // Insert display data so that the HWC thinks it created the virtual display.
1288 const auto displayId = Case::Display::DISPLAY_ID::get();
1289 ASSERT_TRUE(displayId);
1290 mFlinger.mutableHwcDisplayData()[*displayId] = {};
1291
1292 setupNewDisplayDeviceInternalTest<Case>();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001293}
1294
1295TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3Display) {
1296 setupNewDisplayDeviceInternalTest<WideColorP3ColorimetricDisplayCase>();
1297}
1298
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001299TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10Display) {
1300 setupNewDisplayDeviceInternalTest<Hdr10DisplayCase>();
1301}
1302
1303TEST_F(SetupNewDisplayDeviceInternalTest, createHdrHlgDisplay) {
1304 setupNewDisplayDeviceInternalTest<HdrHlgDisplayCase>();
1305}
1306
1307TEST_F(SetupNewDisplayDeviceInternalTest, createHdrDolbyVisionDisplay) {
1308 setupNewDisplayDeviceInternalTest<HdrDolbyVisionDisplayCase>();
1309}
1310
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001311TEST_F(SetupNewDisplayDeviceInternalTest, createHdrSmpte2086DisplayCase) {
1312 setupNewDisplayDeviceInternalTest<HdrSmpte2086DisplayCase>();
1313}
1314
1315TEST_F(SetupNewDisplayDeviceInternalTest, createHdrCta816_3_DisplayCase) {
1316 setupNewDisplayDeviceInternalTest<HdrCta861_3_DisplayCase>();
1317}
1318
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001319/* ------------------------------------------------------------------------
1320 * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded)
1321 */
1322
1323class HandleTransactionLockedTest : public DisplayTransactionTest {
1324public:
1325 template <typename Case>
1326 void setupCommonPreconditions();
1327
1328 template <typename Case>
1329 void setupCommonCallExpectationsForConnectProcessing();
1330
1331 template <typename Case>
1332 void setupCommonCallExpectationsForDisconnectProcessing();
1333
1334 template <typename Case>
1335 void processesHotplugConnectCommon();
1336
1337 template <typename Case>
1338 void ignoresHotplugConnectCommon();
1339
1340 template <typename Case>
1341 void processesHotplugDisconnectCommon();
1342
1343 template <typename Case>
1344 void verifyDisplayIsConnected(const sp<IBinder>& displayToken);
1345
1346 template <typename Case>
1347 void verifyPhysicalDisplayIsConnected();
1348
1349 void verifyDisplayIsNotConnected(const sp<IBinder>& displayToken);
1350};
1351
1352template <typename Case>
1353void HandleTransactionLockedTest::setupCommonPreconditions() {
1354 // Wide color displays support is configured appropriately
1355 Case::WideColorSupport::injectConfigChange(this);
1356
1357 // SurfaceFlinger will use a test-controlled factory for BufferQueues
1358 injectFakeBufferQueueFactory();
1359
1360 // SurfaceFlinger will use a test-controlled factory for native window
1361 // surfaces.
1362 injectFakeNativeWindowSurfaceFactory();
1363}
1364
1365template <typename Case>
1366void HandleTransactionLockedTest::setupCommonCallExpectationsForConnectProcessing() {
1367 Case::Display::setupHwcHotplugCallExpectations(this);
1368
1369 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1370 Case::Display::setupFramebufferProducerBufferQueueCallExpectations(this);
1371 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1372 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
1373
1374 Case::WideColorSupport::setupComposerCallExpectations(this);
1375 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001376 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001377
1378 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001379 EXPECT_CALL(*mEventThread,
Dominik Laskowski075d3172018-05-24 15:50:06 -07001380 onHotplugReceived(static_cast<bool>(Case::Display::PRIMARY)
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001381 ? EventThread::DisplayType::Primary
1382 : EventThread::DisplayType::External,
1383 true))
1384 .Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001385}
1386
1387template <typename Case>
1388void HandleTransactionLockedTest::setupCommonCallExpectationsForDisconnectProcessing() {
1389 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001390 EXPECT_CALL(*mEventThread,
Dominik Laskowski075d3172018-05-24 15:50:06 -07001391 onHotplugReceived(static_cast<bool>(Case::Display::PRIMARY)
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001392 ? EventThread::DisplayType::Primary
1393 : EventThread::DisplayType::External,
1394 false))
1395 .Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001396}
1397
1398template <typename Case>
1399void HandleTransactionLockedTest::verifyDisplayIsConnected(const sp<IBinder>& displayToken) {
1400 // The display device should have been set up in the list of displays.
1401 ASSERT_TRUE(hasDisplayDevice(displayToken));
1402 const auto& device = getDisplayDevice(displayToken);
1403 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001404 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001405
1406 // The display should have been set up in the current display state
1407 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1408 const auto& current = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001409 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), current.isVirtual());
1410 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1411 : Case::Display::DISPLAY_ID::get(),
1412 current.displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001413
1414 // The display should have been set up in the drawing display state
1415 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1416 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001417 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
1418 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1419 : Case::Display::DISPLAY_ID::get(),
1420 draw.displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001421}
1422
1423template <typename Case>
1424void HandleTransactionLockedTest::verifyPhysicalDisplayIsConnected() {
1425 // HWComposer should have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001426 EXPECT_TRUE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001427
Dominik Laskowski075d3172018-05-24 15:50:06 -07001428 // SF should have a display token.
1429 const auto displayId = Case::Display::DISPLAY_ID::get();
1430 ASSERT_TRUE(displayId);
1431 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1432 auto& displayToken = mFlinger.mutablePhysicalDisplayTokens()[*displayId];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001433
1434 verifyDisplayIsConnected<Case>(displayToken);
1435}
1436
1437void HandleTransactionLockedTest::verifyDisplayIsNotConnected(const sp<IBinder>& displayToken) {
1438 EXPECT_FALSE(hasDisplayDevice(displayToken));
1439 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1440 EXPECT_FALSE(hasDrawingDisplayState(displayToken));
1441}
1442
1443template <typename Case>
1444void HandleTransactionLockedTest::processesHotplugConnectCommon() {
1445 // --------------------------------------------------------------------
1446 // Preconditions
1447
1448 setupCommonPreconditions<Case>();
1449
1450 // A hotplug connect event is enqueued for a display
1451 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001452
1453 // --------------------------------------------------------------------
1454 // Call Expectations
1455
1456 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillOnce(Return(false));
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001457
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001458 setupCommonCallExpectationsForConnectProcessing<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001459
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001460 // --------------------------------------------------------------------
1461 // Invocation
Lloyd Piquee39cad22017-12-20 17:01:29 -08001462
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001463 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
Lloyd Piquee39cad22017-12-20 17:01:29 -08001464
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001465 // --------------------------------------------------------------------
1466 // Postconditions
1467
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001468 verifyPhysicalDisplayIsConnected<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001469
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001470 // --------------------------------------------------------------------
1471 // Cleanup conditions
1472
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001473 EXPECT_CALL(*mComposer,
1474 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001475 .WillOnce(Return(Error::NONE));
1476 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Piquef58625d2017-12-19 13:22:33 -08001477}
1478
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001479template <typename Case>
1480void HandleTransactionLockedTest::ignoresHotplugConnectCommon() {
1481 // --------------------------------------------------------------------
1482 // Preconditions
1483
1484 setupCommonPreconditions<Case>();
1485
1486 // A hotplug connect event is enqueued for a display
1487 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1488
1489 // --------------------------------------------------------------------
1490 // Invocation
1491
1492 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1493
1494 // --------------------------------------------------------------------
1495 // Postconditions
1496
1497 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001498 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001499}
1500
1501template <typename Case>
1502void HandleTransactionLockedTest::processesHotplugDisconnectCommon() {
1503 // --------------------------------------------------------------------
1504 // Preconditions
1505
1506 setupCommonPreconditions<Case>();
1507
1508 // A hotplug disconnect event is enqueued for a display
1509 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1510
1511 // The display is already completely set up.
1512 Case::Display::injectHwcDisplay(this);
1513 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1514 existing.inject();
1515
1516 // --------------------------------------------------------------------
1517 // Call Expectations
1518
1519 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
Lloyd Pique438e9e72018-09-04 18:06:08 -07001520 EXPECT_CALL(*mComposer, getDisplayIdentificationData(Case::Display::HWC_DISPLAY_ID, _, _))
1521 .Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001522
1523 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1524
1525 // --------------------------------------------------------------------
1526 // Invocation
1527
1528 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1529
1530 // --------------------------------------------------------------------
1531 // Postconditions
1532
1533 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001534 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001535
Dominik Laskowski075d3172018-05-24 15:50:06 -07001536 // SF should not have a display token.
1537 const auto displayId = Case::Display::DISPLAY_ID::get();
1538 ASSERT_TRUE(displayId);
1539 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001540
1541 // The existing token should have been removed
1542 verifyDisplayIsNotConnected(existing.token());
1543}
1544
1545TEST_F(HandleTransactionLockedTest, processesHotplugConnectPrimaryDisplay) {
1546 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1547}
1548
1549TEST_F(HandleTransactionLockedTest,
1550 processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected) {
1551 // Inject an external display.
1552 ExternalDisplayVariant::injectHwcDisplay(this);
1553
1554 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1555}
1556
1557TEST_F(HandleTransactionLockedTest, processesHotplugConnectExternalDisplay) {
1558 // Inject a primary display.
1559 PrimaryDisplayVariant::injectHwcDisplay(this);
1560
1561 processesHotplugConnectCommon<SimpleExternalDisplayCase>();
1562}
1563
1564TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected) {
1565 // Inject both a primary and external display.
1566 PrimaryDisplayVariant::injectHwcDisplay(this);
1567 ExternalDisplayVariant::injectHwcDisplay(this);
1568
1569 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1570
1571 ignoresHotplugConnectCommon<SimpleTertiaryDisplayCase>();
1572}
1573
1574TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfExternalForVrComposer) {
1575 // Inject a primary display.
1576 PrimaryDisplayVariant::injectHwcDisplay(this);
1577
1578 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(true));
1579
1580 ignoresHotplugConnectCommon<SimpleExternalDisplayCase>();
1581}
1582
1583TEST_F(HandleTransactionLockedTest, processHotplugDisconnectPrimaryDisplay) {
1584 processesHotplugDisconnectCommon<SimplePrimaryDisplayCase>();
1585}
1586
1587TEST_F(HandleTransactionLockedTest, processHotplugDisconnectExternalDisplay) {
1588 processesHotplugDisconnectCommon<SimpleExternalDisplayCase>();
1589}
1590
1591TEST_F(HandleTransactionLockedTest, processesHotplugConnectThenDisconnectPrimary) {
1592 using Case = SimplePrimaryDisplayCase;
1593
1594 // --------------------------------------------------------------------
1595 // Preconditions
1596
1597 setupCommonPreconditions<Case>();
1598
1599 // A hotplug connect event is enqueued for a display
1600 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1601 // A hotplug disconnect event is also enqueued for the same display
1602 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1603
1604 // --------------------------------------------------------------------
1605 // Call Expectations
1606
1607 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1608
1609 setupCommonCallExpectationsForConnectProcessing<Case>();
1610 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1611
1612 EXPECT_CALL(*mComposer,
1613 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1614 .WillOnce(Return(Error::NONE));
1615 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1616
1617 // --------------------------------------------------------------------
1618 // Invocation
1619
1620 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1621
1622 // --------------------------------------------------------------------
1623 // Postconditions
1624
1625 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001626 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001627
Dominik Laskowski075d3172018-05-24 15:50:06 -07001628 // SF should not have a display token.
1629 const auto displayId = Case::Display::DISPLAY_ID::get();
1630 ASSERT_TRUE(displayId);
1631 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001632}
1633
1634TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectThenConnectPrimary) {
1635 using Case = SimplePrimaryDisplayCase;
1636
1637 // --------------------------------------------------------------------
1638 // Preconditions
1639
1640 setupCommonPreconditions<Case>();
1641
1642 // The display is already completely set up.
1643 Case::Display::injectHwcDisplay(this);
1644 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1645 existing.inject();
1646
1647 // A hotplug disconnect event is enqueued for a display
1648 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1649 // A hotplug connect event is also enqueued for the same display
1650 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1651
1652 // --------------------------------------------------------------------
1653 // Call Expectations
1654
1655 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1656
1657 setupCommonCallExpectationsForConnectProcessing<Case>();
1658 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1659
1660 // --------------------------------------------------------------------
1661 // Invocation
1662
1663 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1664
1665 // --------------------------------------------------------------------
1666 // Postconditions
1667
1668 // The existing token should have been removed
1669 verifyDisplayIsNotConnected(existing.token());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001670 const auto displayId = Case::Display::DISPLAY_ID::get();
1671 ASSERT_TRUE(displayId);
1672 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1673 EXPECT_NE(existing.token(), mFlinger.mutablePhysicalDisplayTokens()[*displayId]);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001674
1675 // A new display should be connected in its place
1676
1677 verifyPhysicalDisplayIsConnected<Case>();
1678
1679 // --------------------------------------------------------------------
1680 // Cleanup conditions
1681
1682 EXPECT_CALL(*mComposer,
1683 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1684 .WillOnce(Return(Error::NONE));
1685 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1686}
1687
1688TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAdded) {
1689 using Case = HwcVirtualDisplayCase;
1690
1691 // --------------------------------------------------------------------
1692 // Preconditions
1693
1694 // The HWC supports at least one virtual display
1695 injectMockComposer(1);
1696
1697 setupCommonPreconditions<Case>();
1698
1699 // A virtual display was added to the current state, and it has a
1700 // surface(producer)
1701 sp<BBinder> displayToken = new BBinder();
Lloyd Pique4c2ac022018-04-27 12:08:03 -07001702
Dominik Laskowski075d3172018-05-24 15:50:06 -07001703 DisplayDeviceState state;
1704 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001705
1706 sp<mock::GraphicBufferProducer> surface{new mock::GraphicBufferProducer()};
Dominik Laskowski075d3172018-05-24 15:50:06 -07001707 state.surface = surface;
1708 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001709
1710 // --------------------------------------------------------------------
1711 // Call Expectations
1712
1713 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1714 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1715
1716 EXPECT_CALL(*surface, query(NATIVE_WINDOW_WIDTH, _))
1717 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::WIDTH), Return(NO_ERROR)));
1718 EXPECT_CALL(*surface, query(NATIVE_WINDOW_HEIGHT, _))
1719 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::HEIGHT), Return(NO_ERROR)));
1720 EXPECT_CALL(*surface, query(NATIVE_WINDOW_FORMAT, _))
1721 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT),
1722 Return(NO_ERROR)));
1723 EXPECT_CALL(*surface, query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, _))
1724 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(NO_ERROR)));
1725
1726 EXPECT_CALL(*surface, setAsyncMode(true)).Times(1);
1727
1728 EXPECT_CALL(*mProducer, connect(_, _, _, _)).Times(1);
1729 EXPECT_CALL(*mProducer, disconnect(_, _)).Times(1);
1730
1731 Case::Display::setupHwcVirtualDisplayCreationCallExpectations(this);
1732 Case::WideColorSupport::setupComposerCallExpectations(this);
1733 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001734 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001735
1736 // --------------------------------------------------------------------
1737 // Invocation
1738
1739 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1740
1741 // --------------------------------------------------------------------
1742 // Postconditions
1743
1744 // The display device should have been set up in the list of displays.
1745 verifyDisplayIsConnected<Case>(displayToken);
1746
1747 // --------------------------------------------------------------------
1748 // Cleanup conditions
1749
1750 EXPECT_CALL(*mComposer, destroyVirtualDisplay(Case::Display::HWC_DISPLAY_ID))
1751 .WillOnce(Return(Error::NONE));
1752 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1753}
1754
1755TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAddedWithNoSurface) {
1756 using Case = HwcVirtualDisplayCase;
1757
1758 // --------------------------------------------------------------------
1759 // Preconditions
1760
1761 // The HWC supports at least one virtual display
1762 injectMockComposer(1);
1763
1764 setupCommonPreconditions<Case>();
1765
1766 // A virtual display was added to the current state, but it does not have a
1767 // surface.
1768 sp<BBinder> displayToken = new BBinder();
1769
Dominik Laskowski075d3172018-05-24 15:50:06 -07001770 DisplayDeviceState state;
1771 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001772
Dominik Laskowski075d3172018-05-24 15:50:06 -07001773 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001774
1775 // --------------------------------------------------------------------
1776 // Call Expectations
1777
1778 // --------------------------------------------------------------------
1779 // Invocation
1780
1781 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1782
1783 // --------------------------------------------------------------------
1784 // Postconditions
1785
1786 // There will not be a display device set up.
1787 EXPECT_FALSE(hasDisplayDevice(displayToken));
1788
1789 // The drawing display state will be set from the current display state.
1790 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1791 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001792 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001793}
1794
1795TEST_F(HandleTransactionLockedTest, processesVirtualDisplayRemoval) {
1796 using Case = HwcVirtualDisplayCase;
1797
1798 // --------------------------------------------------------------------
1799 // Preconditions
1800
1801 // A virtual display is set up but is removed from the current state.
Dominik Laskowski075d3172018-05-24 15:50:06 -07001802 const auto displayId = Case::Display::DISPLAY_ID::get();
1803 ASSERT_TRUE(displayId);
1804 mFlinger.mutableHwcDisplayData()[*displayId] = {};
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001805 Case::Display::injectHwcDisplay(this);
1806 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1807 existing.inject();
1808 mFlinger.mutableCurrentState().displays.removeItem(existing.token());
1809
1810 // --------------------------------------------------------------------
1811 // Call Expectations
1812
1813 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1814
1815 // --------------------------------------------------------------------
1816 // Invocation
1817
1818 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1819
1820 // --------------------------------------------------------------------
1821 // Postconditions
1822
1823 // The existing token should have been removed
1824 verifyDisplayIsNotConnected(existing.token());
1825}
1826
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001827TEST_F(HandleTransactionLockedTest, processesDisplayLayerStackChanges) {
1828 using Case = NonHwcVirtualDisplayCase;
1829
1830 constexpr uint32_t oldLayerStack = 0u;
1831 constexpr uint32_t newLayerStack = 123u;
1832
1833 // --------------------------------------------------------------------
1834 // Preconditions
1835
1836 // A display is set up
1837 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1838 display.inject();
1839
1840 // There is a change to the layerStack state
1841 display.mutableDrawingDisplayState().layerStack = oldLayerStack;
1842 display.mutableCurrentDisplayState().layerStack = newLayerStack;
1843
1844 // --------------------------------------------------------------------
1845 // Invocation
1846
1847 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1848
1849 // --------------------------------------------------------------------
1850 // Postconditions
1851
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001852 EXPECT_EQ(newLayerStack, display.mutableDisplayDevice()->getLayerStack());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001853}
1854
1855TEST_F(HandleTransactionLockedTest, processesDisplayTransformChanges) {
1856 using Case = NonHwcVirtualDisplayCase;
1857
1858 constexpr int oldTransform = 0;
1859 constexpr int newTransform = 2;
1860
1861 // --------------------------------------------------------------------
1862 // Preconditions
1863
1864 // A display is set up
1865 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1866 display.inject();
1867
1868 // There is a change to the orientation state
1869 display.mutableDrawingDisplayState().orientation = oldTransform;
1870 display.mutableCurrentDisplayState().orientation = newTransform;
1871
1872 // --------------------------------------------------------------------
1873 // Invocation
1874
1875 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1876
1877 // --------------------------------------------------------------------
1878 // Postconditions
1879
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001880 EXPECT_EQ(newTransform, display.mutableDisplayDevice()->getOrientation());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001881}
1882
1883TEST_F(HandleTransactionLockedTest, processesDisplayViewportChanges) {
1884 using Case = NonHwcVirtualDisplayCase;
1885
1886 const Rect oldViewport(0, 0, 0, 0);
1887 const Rect newViewport(0, 0, 123, 456);
1888
1889 // --------------------------------------------------------------------
1890 // Preconditions
1891
1892 // A display is set up
1893 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1894 display.inject();
1895
1896 // There is a change to the viewport state
1897 display.mutableDrawingDisplayState().viewport = oldViewport;
1898 display.mutableCurrentDisplayState().viewport = newViewport;
1899
1900 // --------------------------------------------------------------------
1901 // Invocation
1902
1903 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1904
1905 // --------------------------------------------------------------------
1906 // Postconditions
1907
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001908 EXPECT_EQ(newViewport, display.mutableDisplayDevice()->getViewport());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001909}
1910
1911TEST_F(HandleTransactionLockedTest, processesDisplayFrameChanges) {
1912 using Case = NonHwcVirtualDisplayCase;
1913
1914 const Rect oldFrame(0, 0, 0, 0);
1915 const Rect newFrame(0, 0, 123, 456);
1916
1917 // --------------------------------------------------------------------
1918 // Preconditions
1919
1920 // A display is set up
1921 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1922 display.inject();
1923
1924 // There is a change to the viewport state
1925 display.mutableDrawingDisplayState().frame = oldFrame;
1926 display.mutableCurrentDisplayState().frame = newFrame;
1927
1928 // --------------------------------------------------------------------
1929 // Invocation
1930
1931 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1932
1933 // --------------------------------------------------------------------
1934 // Postconditions
1935
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001936 EXPECT_EQ(newFrame, display.mutableDisplayDevice()->getFrame());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001937}
1938
1939TEST_F(HandleTransactionLockedTest, processesDisplayWidthChanges) {
1940 using Case = NonHwcVirtualDisplayCase;
1941
1942 constexpr int oldWidth = 0;
1943 constexpr int oldHeight = 10;
1944 constexpr int newWidth = 123;
1945
1946 // --------------------------------------------------------------------
1947 // Preconditions
1948
1949 // A display is set up
1950 auto nativeWindow = new mock::NativeWindow();
1951 auto displaySurface = new mock::DisplaySurface();
Peiyong Lin833074a2018-08-28 11:53:54 -07001952 auto renderSurface = new renderengine::mock::Surface();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001953 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1954 display.setNativeWindow(nativeWindow);
1955 display.setDisplaySurface(displaySurface);
Peiyong Lin833074a2018-08-28 11:53:54 -07001956 display.setRenderSurface(std::unique_ptr<renderengine::Surface>(renderSurface));
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001957 display.inject();
1958
1959 // There is a change to the viewport state
1960 display.mutableDrawingDisplayState().width = oldWidth;
1961 display.mutableDrawingDisplayState().height = oldHeight;
1962 display.mutableCurrentDisplayState().width = newWidth;
1963 display.mutableCurrentDisplayState().height = oldHeight;
1964
1965 // --------------------------------------------------------------------
1966 // Call Expectations
1967
1968 EXPECT_CALL(*renderSurface, setNativeWindow(nullptr)).Times(1);
1969 EXPECT_CALL(*displaySurface, resizeBuffers(newWidth, oldHeight)).Times(1);
1970 EXPECT_CALL(*renderSurface, setNativeWindow(nativeWindow)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001971
1972 // --------------------------------------------------------------------
1973 // Invocation
1974
1975 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1976}
1977
1978TEST_F(HandleTransactionLockedTest, processesDisplayHeightChanges) {
1979 using Case = NonHwcVirtualDisplayCase;
1980
1981 constexpr int oldWidth = 0;
1982 constexpr int oldHeight = 10;
1983 constexpr int newHeight = 123;
1984
1985 // --------------------------------------------------------------------
1986 // Preconditions
1987
1988 // A display is set up
1989 auto nativeWindow = new mock::NativeWindow();
1990 auto displaySurface = new mock::DisplaySurface();
Peiyong Lin833074a2018-08-28 11:53:54 -07001991 auto renderSurface = new renderengine::mock::Surface();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001992 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1993 display.setNativeWindow(nativeWindow);
1994 display.setDisplaySurface(displaySurface);
Peiyong Lin833074a2018-08-28 11:53:54 -07001995 display.setRenderSurface(std::unique_ptr<renderengine::Surface>(renderSurface));
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001996 display.inject();
1997
1998 // There is a change to the viewport state
1999 display.mutableDrawingDisplayState().width = oldWidth;
2000 display.mutableDrawingDisplayState().height = oldHeight;
2001 display.mutableCurrentDisplayState().width = oldWidth;
2002 display.mutableCurrentDisplayState().height = newHeight;
2003
2004 // --------------------------------------------------------------------
2005 // Call Expectations
2006
2007 EXPECT_CALL(*renderSurface, setNativeWindow(nullptr)).Times(1);
2008 EXPECT_CALL(*displaySurface, resizeBuffers(oldWidth, newHeight)).Times(1);
2009 EXPECT_CALL(*renderSurface, setNativeWindow(nativeWindow)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002010
2011 // --------------------------------------------------------------------
2012 // Invocation
2013
2014 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2015}
2016
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002017/* ------------------------------------------------------------------------
2018 * SurfaceFlinger::setDisplayStateLocked
2019 */
2020
2021TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithUnknownDisplay) {
2022 // --------------------------------------------------------------------
2023 // Preconditions
2024
2025 // We have an unknown display token not associated with a known display
2026 sp<BBinder> displayToken = new BBinder();
2027
2028 // The requested display state references the unknown display.
2029 DisplayState state;
2030 state.what = DisplayState::eLayerStackChanged;
2031 state.token = displayToken;
2032 state.layerStack = 456;
2033
2034 // --------------------------------------------------------------------
2035 // Invocation
2036
2037 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2038
2039 // --------------------------------------------------------------------
2040 // Postconditions
2041
2042 // The returned flags are empty
2043 EXPECT_EQ(0u, flags);
2044
2045 // The display token still doesn't match anything known.
2046 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
2047}
2048
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002049TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWhenNoChanges) {
2050 using Case = SimplePrimaryDisplayCase;
2051
2052 // --------------------------------------------------------------------
2053 // Preconditions
2054
2055 // A display is already set up
2056 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2057 display.inject();
2058
2059 // No changes are made to the display
2060 DisplayState state;
2061 state.what = 0;
2062 state.token = display.token();
2063
2064 // --------------------------------------------------------------------
2065 // Invocation
2066
2067 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2068
2069 // --------------------------------------------------------------------
2070 // Postconditions
2071
2072 // The returned flags are empty
2073 EXPECT_EQ(0u, flags);
2074}
2075
2076TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSurfaceDidNotChange) {
2077 using Case = SimplePrimaryDisplayCase;
2078
2079 // --------------------------------------------------------------------
2080 // Preconditions
2081
2082 // A display is already set up
2083 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2084 display.inject();
2085
2086 // There is a surface that can be set.
2087 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2088
2089 // The current display state has the surface set
2090 display.mutableCurrentDisplayState().surface = surface;
2091
2092 // The incoming request sets the same surface
2093 DisplayState state;
2094 state.what = DisplayState::eSurfaceChanged;
2095 state.token = display.token();
2096 state.surface = surface;
2097
2098 // --------------------------------------------------------------------
2099 // Invocation
2100
2101 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2102
2103 // --------------------------------------------------------------------
2104 // Postconditions
2105
2106 // The returned flags are empty
2107 EXPECT_EQ(0u, flags);
2108
2109 // The current display state is unchanged.
2110 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2111}
2112
2113TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfSurfaceChanged) {
2114 using Case = SimplePrimaryDisplayCase;
2115
2116 // --------------------------------------------------------------------
2117 // Preconditions
2118
2119 // A display is already set up
2120 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2121 display.inject();
2122
2123 // There is a surface that can be set.
2124 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2125
2126 // The current display state does not have a surface
2127 display.mutableCurrentDisplayState().surface = nullptr;
2128
2129 // The incoming request sets a surface
2130 DisplayState state;
2131 state.what = DisplayState::eSurfaceChanged;
2132 state.token = display.token();
2133 state.surface = surface;
2134
2135 // --------------------------------------------------------------------
2136 // Invocation
2137
2138 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2139
2140 // --------------------------------------------------------------------
2141 // Postconditions
2142
2143 // The returned flags indicate a transaction is needed
2144 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2145
2146 // The current display layer stack state is set to the new value
2147 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2148}
2149
2150TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfLayerStackDidNotChange) {
2151 using Case = SimplePrimaryDisplayCase;
2152
2153 // --------------------------------------------------------------------
2154 // Preconditions
2155
2156 // A display is already set up
2157 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2158 display.inject();
2159
2160 // The display has a layer stack set
2161 display.mutableCurrentDisplayState().layerStack = 456u;
2162
2163 // The incoming request sets the same layer stack
2164 DisplayState state;
2165 state.what = DisplayState::eLayerStackChanged;
2166 state.token = display.token();
2167 state.layerStack = 456u;
2168
2169 // --------------------------------------------------------------------
2170 // Invocation
2171
2172 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2173
2174 // --------------------------------------------------------------------
2175 // Postconditions
2176
2177 // The returned flags are empty
2178 EXPECT_EQ(0u, flags);
2179
2180 // The current display state is unchanged
2181 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2182}
2183
2184TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfLayerStackChanged) {
2185 using Case = SimplePrimaryDisplayCase;
2186
2187 // --------------------------------------------------------------------
2188 // Preconditions
2189
2190 // A display is set up
2191 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2192 display.inject();
2193
2194 // The display has a layer stack set
2195 display.mutableCurrentDisplayState().layerStack = 654u;
2196
2197 // The incoming request sets a different layer stack
2198 DisplayState state;
2199 state.what = DisplayState::eLayerStackChanged;
2200 state.token = display.token();
2201 state.layerStack = 456u;
2202
2203 // --------------------------------------------------------------------
2204 // Invocation
2205
2206 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2207
2208 // --------------------------------------------------------------------
2209 // Postconditions
2210
2211 // The returned flags indicate a transaction is needed
2212 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2213
2214 // The desired display state has been set to the new value.
2215 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2216}
2217
2218TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfProjectionDidNotChange) {
2219 using Case = SimplePrimaryDisplayCase;
2220 constexpr int initialOrientation = 180;
2221 const Rect initialFrame = {1, 2, 3, 4};
2222 const Rect initialViewport = {5, 6, 7, 8};
2223
2224 // --------------------------------------------------------------------
2225 // Preconditions
2226
2227 // A display is set up
2228 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2229 display.inject();
2230
2231 // The current display state projection state is all set
2232 display.mutableCurrentDisplayState().orientation = initialOrientation;
2233 display.mutableCurrentDisplayState().frame = initialFrame;
2234 display.mutableCurrentDisplayState().viewport = initialViewport;
2235
2236 // The incoming request sets the same projection state
2237 DisplayState state;
2238 state.what = DisplayState::eDisplayProjectionChanged;
2239 state.token = display.token();
2240 state.orientation = initialOrientation;
2241 state.frame = initialFrame;
2242 state.viewport = initialViewport;
2243
2244 // --------------------------------------------------------------------
2245 // Invocation
2246
2247 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2248
2249 // --------------------------------------------------------------------
2250 // Postconditions
2251
2252 // The returned flags are empty
2253 EXPECT_EQ(0u, flags);
2254
2255 // The current display state is unchanged
2256 EXPECT_EQ(initialOrientation, display.getCurrentDisplayState().orientation);
2257
2258 EXPECT_EQ(initialFrame, display.getCurrentDisplayState().frame);
2259 EXPECT_EQ(initialViewport, display.getCurrentDisplayState().viewport);
2260}
2261
2262TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfOrientationChanged) {
2263 using Case = SimplePrimaryDisplayCase;
2264 constexpr int initialOrientation = 90;
2265 constexpr int desiredOrientation = 180;
2266
2267 // --------------------------------------------------------------------
2268 // Preconditions
2269
2270 // A display is set up
2271 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2272 display.inject();
2273
2274 // The current display state has an orientation set
2275 display.mutableCurrentDisplayState().orientation = initialOrientation;
2276
2277 // The incoming request sets a different orientation
2278 DisplayState state;
2279 state.what = DisplayState::eDisplayProjectionChanged;
2280 state.token = display.token();
2281 state.orientation = desiredOrientation;
2282
2283 // --------------------------------------------------------------------
2284 // Invocation
2285
2286 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2287
2288 // --------------------------------------------------------------------
2289 // Postconditions
2290
2291 // The returned flags indicate a transaction is needed
2292 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2293
2294 // The current display state has the new value.
2295 EXPECT_EQ(desiredOrientation, display.getCurrentDisplayState().orientation);
2296}
2297
2298TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfFrameChanged) {
2299 using Case = SimplePrimaryDisplayCase;
2300 const Rect initialFrame = {0, 0, 0, 0};
2301 const Rect desiredFrame = {5, 6, 7, 8};
2302
2303 // --------------------------------------------------------------------
2304 // Preconditions
2305
2306 // A display is set up
2307 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2308 display.inject();
2309
2310 // The current display state does not have a frame
2311 display.mutableCurrentDisplayState().frame = initialFrame;
2312
2313 // The incoming request sets a frame
2314 DisplayState state;
2315 state.what = DisplayState::eDisplayProjectionChanged;
2316 state.token = display.token();
2317 state.frame = desiredFrame;
2318
2319 // --------------------------------------------------------------------
2320 // Invocation
2321
2322 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2323
2324 // --------------------------------------------------------------------
2325 // Postconditions
2326
2327 // The returned flags indicate a transaction is needed
2328 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2329
2330 // The current display state has the new value.
2331 EXPECT_EQ(desiredFrame, display.getCurrentDisplayState().frame);
2332}
2333
2334TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfViewportChanged) {
2335 using Case = SimplePrimaryDisplayCase;
2336 const Rect initialViewport = {0, 0, 0, 0};
2337 const Rect desiredViewport = {5, 6, 7, 8};
2338
2339 // --------------------------------------------------------------------
2340 // Preconditions
2341
2342 // A display is set up
2343 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2344 display.inject();
2345
2346 // The current display state does not have a viewport
2347 display.mutableCurrentDisplayState().viewport = initialViewport;
2348
2349 // The incoming request sets a viewport
2350 DisplayState state;
2351 state.what = DisplayState::eDisplayProjectionChanged;
2352 state.token = display.token();
2353 state.viewport = desiredViewport;
2354
2355 // --------------------------------------------------------------------
2356 // Invocation
2357
2358 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2359
2360 // --------------------------------------------------------------------
2361 // Postconditions
2362
2363 // The returned flags indicate a transaction is needed
2364 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2365
2366 // The current display state has the new value.
2367 EXPECT_EQ(desiredViewport, display.getCurrentDisplayState().viewport);
2368}
2369
2370TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSizeDidNotChange) {
2371 using Case = SimplePrimaryDisplayCase;
2372 constexpr uint32_t initialWidth = 1024;
2373 constexpr uint32_t initialHeight = 768;
2374
2375 // --------------------------------------------------------------------
2376 // Preconditions
2377
2378 // A display is set up
2379 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2380 display.inject();
2381
2382 // The current display state has a size set
2383 display.mutableCurrentDisplayState().width = initialWidth;
2384 display.mutableCurrentDisplayState().height = initialHeight;
2385
2386 // The incoming request sets the same display size
2387 DisplayState state;
2388 state.what = DisplayState::eDisplaySizeChanged;
2389 state.token = display.token();
2390 state.width = initialWidth;
2391 state.height = initialHeight;
2392
2393 // --------------------------------------------------------------------
2394 // Invocation
2395
2396 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2397
2398 // --------------------------------------------------------------------
2399 // Postconditions
2400
2401 // The returned flags are empty
2402 EXPECT_EQ(0u, flags);
2403
2404 // The current display state is unchanged
2405 EXPECT_EQ(initialWidth, display.getCurrentDisplayState().width);
2406 EXPECT_EQ(initialHeight, display.getCurrentDisplayState().height);
2407}
2408
2409TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfWidthChanged) {
2410 using Case = SimplePrimaryDisplayCase;
2411 constexpr uint32_t initialWidth = 0;
2412 constexpr uint32_t desiredWidth = 1024;
2413
2414 // --------------------------------------------------------------------
2415 // Preconditions
2416
2417 // A display is set up
2418 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2419 display.inject();
2420
2421 // The display does not yet have a width
2422 display.mutableCurrentDisplayState().width = initialWidth;
2423
2424 // The incoming request sets a display width
2425 DisplayState state;
2426 state.what = DisplayState::eDisplaySizeChanged;
2427 state.token = display.token();
2428 state.width = desiredWidth;
2429
2430 // --------------------------------------------------------------------
2431 // Invocation
2432
2433 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2434
2435 // --------------------------------------------------------------------
2436 // Postconditions
2437
2438 // The returned flags indicate a transaction is needed
2439 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2440
2441 // The current display state has the new value.
2442 EXPECT_EQ(desiredWidth, display.getCurrentDisplayState().width);
2443}
2444
2445TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfHeightChanged) {
2446 using Case = SimplePrimaryDisplayCase;
2447 constexpr uint32_t initialHeight = 0;
2448 constexpr uint32_t desiredHeight = 768;
2449
2450 // --------------------------------------------------------------------
2451 // Preconditions
2452
2453 // A display is set up
2454 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2455 display.inject();
2456
2457 // The display does not yet have a height
2458 display.mutableCurrentDisplayState().height = initialHeight;
2459
2460 // The incoming request sets a display height
2461 DisplayState state;
2462 state.what = DisplayState::eDisplaySizeChanged;
2463 state.token = display.token();
2464 state.height = desiredHeight;
2465
2466 // --------------------------------------------------------------------
2467 // Invocation
2468
2469 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2470
2471 // --------------------------------------------------------------------
2472 // Postconditions
2473
2474 // The returned flags indicate a transaction is needed
2475 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2476
2477 // The current display state has the new value.
2478 EXPECT_EQ(desiredHeight, display.getCurrentDisplayState().height);
2479}
2480
Lloyd Pique86016da2018-03-01 16:09:38 -08002481/* ------------------------------------------------------------------------
2482 * SurfaceFlinger::onInitializeDisplays
2483 */
2484
2485TEST_F(DisplayTransactionTest, onInitializeDisplaysSetsUpPrimaryDisplay) {
2486 using Case = SimplePrimaryDisplayCase;
2487
2488 // --------------------------------------------------------------------
2489 // Preconditions
2490
2491 // A primary display is set up
2492 Case::Display::injectHwcDisplay(this);
2493 auto primaryDisplay = Case::Display::makeFakeExistingDisplayInjector(this);
2494 primaryDisplay.inject();
2495
2496 // --------------------------------------------------------------------
2497 // Call Expectations
2498
2499 // We expect the surface interceptor to possibly be used, but we treat it as
2500 // disabled since it is called as a side effect rather than directly by this
2501 // function.
2502 EXPECT_CALL(*mSurfaceInterceptor, isEnabled()).WillOnce(Return(false));
2503
2504 // We expect a call to get the active display config.
2505 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
2506
2507 // We expect invalidate() to be invoked once to trigger display transaction
2508 // processing.
2509 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
2510
2511 // --------------------------------------------------------------------
2512 // Invocation
2513
2514 mFlinger.onInitializeDisplays();
2515
2516 // --------------------------------------------------------------------
2517 // Postconditions
2518
2519 // The primary display should have a current state
2520 ASSERT_TRUE(hasCurrentDisplayState(primaryDisplay.token()));
2521 const auto& primaryDisplayState = getCurrentDisplayState(primaryDisplay.token());
2522 // The layer stack state should be set to zero
2523 EXPECT_EQ(0u, primaryDisplayState.layerStack);
2524 // The orientation state should be set to zero
2525 EXPECT_EQ(0, primaryDisplayState.orientation);
2526
2527 // The frame state should be set to INVALID
2528 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.frame);
2529
2530 // The viewport state should be set to INVALID
2531 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.viewport);
2532
2533 // The width and height should both be zero
2534 EXPECT_EQ(0u, primaryDisplayState.width);
2535 EXPECT_EQ(0u, primaryDisplayState.height);
2536
2537 // The display should be set to HWC_POWER_MODE_NORMAL
2538 ASSERT_TRUE(hasDisplayDevice(primaryDisplay.token()));
2539 auto displayDevice = primaryDisplay.mutableDisplayDevice();
2540 EXPECT_EQ(HWC_POWER_MODE_NORMAL, displayDevice->getPowerMode());
2541
2542 // The display refresh period should be set in the frame tracker.
2543 FrameStats stats;
2544 mFlinger.getAnimFrameTracker().getStats(&stats);
2545 EXPECT_EQ(DEFAULT_REFRESH_RATE, stats.refreshPeriodNano);
2546
2547 // The display transaction needed flag should be set.
2548 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
2549
2550 // The compositor timing should be set to default values
2551 const auto& compositorTiming = mFlinger.getCompositorTiming();
2552 EXPECT_EQ(-DEFAULT_REFRESH_RATE, compositorTiming.deadline);
2553 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.interval);
2554 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.presentLatency);
2555}
2556
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002557/* ------------------------------------------------------------------------
2558 * SurfaceFlinger::setPowerModeInternal
2559 */
2560
2561// Used when we simulate a display that supports doze.
2562struct DozeIsSupportedVariant {
2563 static constexpr bool DOZE_SUPPORTED = true;
2564 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2565 IComposerClient::PowerMode::DOZE;
2566 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2567 IComposerClient::PowerMode::DOZE_SUSPEND;
2568};
2569
2570// Used when we simulate a display that does not support doze.
2571struct DozeNotSupportedVariant {
2572 static constexpr bool DOZE_SUPPORTED = false;
2573 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2574 IComposerClient::PowerMode::ON;
2575 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2576 IComposerClient::PowerMode::ON;
2577};
2578
2579struct EventThreadBaseSupportedVariant {
2580 static void setupEventAndEventControlThreadNoCallExpectations(DisplayTransactionTest* test) {
2581 // The event control thread should not be notified.
2582 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(_)).Times(0);
2583
2584 // The event thread should not be notified.
2585 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(0);
2586 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(0);
2587 }
2588};
2589
2590struct EventThreadNotSupportedVariant : public EventThreadBaseSupportedVariant {
2591 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2592 // These calls are only expected for the primary display.
2593
2594 // Instead expect no calls.
2595 setupEventAndEventControlThreadNoCallExpectations(test);
2596 }
2597
2598 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2599 // These calls are only expected for the primary display.
2600
2601 // Instead expect no calls.
2602 setupEventAndEventControlThreadNoCallExpectations(test);
2603 }
2604};
2605
2606struct EventThreadIsSupportedVariant : public EventThreadBaseSupportedVariant {
2607 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2608 // The event control thread should be notified to enable vsyncs
2609 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(true)).Times(1);
2610
2611 // The event thread should be notified that the screen was acquired.
2612 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(1);
2613 }
2614
2615 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2616 // There should be a call to setVsyncEnabled(false)
2617 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(false)).Times(1);
2618
2619 // The event thread should not be notified that the screen was released.
2620 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(1);
2621 }
2622};
2623
Lloyd Pique41be5d22018-06-21 13:11:48 -07002624struct DispSyncIsSupportedVariant {
2625 static void setupBeginResyncCallExpectations(DisplayTransactionTest* test) {
2626 EXPECT_CALL(*test->mPrimaryDispSync, reset()).Times(1);
2627 EXPECT_CALL(*test->mPrimaryDispSync, setPeriod(DEFAULT_REFRESH_RATE)).Times(1);
2628 EXPECT_CALL(*test->mPrimaryDispSync, beginResync()).Times(1);
2629 }
2630
2631 static void setupEndResyncCallExpectations(DisplayTransactionTest* test) {
2632 EXPECT_CALL(*test->mPrimaryDispSync, endResync()).Times(1);
2633 }
2634};
2635
2636struct DispSyncNotSupportedVariant {
2637 static void setupBeginResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2638
2639 static void setupEndResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2640};
2641
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002642// --------------------------------------------------------------------
2643// Note:
2644//
2645// There are a large number of transitions we could test, however we only test a
2646// selected subset which provides complete test coverage of the implementation.
2647// --------------------------------------------------------------------
2648
2649template <int initialPowerMode, int targetPowerMode>
2650struct TransitionVariantCommon {
2651 static constexpr auto INITIAL_POWER_MODE = initialPowerMode;
2652 static constexpr auto TARGET_POWER_MODE = targetPowerMode;
2653
2654 static void verifyPostconditions(DisplayTransactionTest*) {}
2655};
2656
2657struct TransitionOffToOnVariant
2658 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_NORMAL> {
2659 template <typename Case>
2660 static void setupCallExpectations(DisplayTransactionTest* test) {
2661 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2662 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002663 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002664 Case::setupRepaintEverythingCallExpectations(test);
2665 }
2666
2667 static void verifyPostconditions(DisplayTransactionTest* test) {
2668 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2669 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2670 }
2671};
2672
2673struct TransitionOffToDozeSuspendVariant
2674 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_DOZE_SUSPEND> {
2675 template <typename Case>
2676 static void setupCallExpectations(DisplayTransactionTest* test) {
2677 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2678 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2679 Case::setupRepaintEverythingCallExpectations(test);
2680 }
2681
2682 static void verifyPostconditions(DisplayTransactionTest* test) {
2683 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2684 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2685 }
2686};
2687
2688struct TransitionOnToOffVariant
2689 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_OFF> {
2690 template <typename Case>
2691 static void setupCallExpectations(DisplayTransactionTest* test) {
2692 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002693 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002694 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2695 }
2696
2697 static void verifyPostconditions(DisplayTransactionTest* test) {
2698 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2699 }
2700};
2701
2702struct TransitionDozeSuspendToOffVariant
2703 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_OFF> {
2704 template <typename Case>
2705 static void setupCallExpectations(DisplayTransactionTest* test) {
2706 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2707 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2708 }
2709
2710 static void verifyPostconditions(DisplayTransactionTest* test) {
2711 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2712 }
2713};
2714
2715struct TransitionOnToDozeVariant
2716 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE> {
2717 template <typename Case>
2718 static void setupCallExpectations(DisplayTransactionTest* test) {
2719 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2720 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2721 }
2722};
2723
2724struct TransitionDozeSuspendToDozeVariant
2725 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_DOZE> {
2726 template <typename Case>
2727 static void setupCallExpectations(DisplayTransactionTest* test) {
2728 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002729 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002730 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2731 }
2732};
2733
2734struct TransitionDozeToOnVariant
2735 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE, HWC_POWER_MODE_NORMAL> {
2736 template <typename Case>
2737 static void setupCallExpectations(DisplayTransactionTest* test) {
2738 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2739 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2740 }
2741};
2742
2743struct TransitionDozeSuspendToOnVariant
2744 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_NORMAL> {
2745 template <typename Case>
2746 static void setupCallExpectations(DisplayTransactionTest* test) {
2747 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002748 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002749 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2750 }
2751};
2752
2753struct TransitionOnToDozeSuspendVariant
2754 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE_SUSPEND> {
2755 template <typename Case>
2756 static void setupCallExpectations(DisplayTransactionTest* test) {
2757 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002758 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002759 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2760 }
2761};
2762
2763struct TransitionOnToUnknownVariant
2764 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_LEET> {
2765 template <typename Case>
2766 static void setupCallExpectations(DisplayTransactionTest* test) {
2767 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2768 Case::setupNoComposerPowerModeCallExpectations(test);
2769 }
2770};
2771
2772// --------------------------------------------------------------------
2773// Note:
2774//
2775// Rather than testing the cartesian product of of
2776// DozeIsSupported/DozeNotSupported with all other options, we use one for one
2777// display type, and the other for another display type.
2778// --------------------------------------------------------------------
2779
2780template <typename DisplayVariant, typename DozeVariant, typename EventThreadVariant,
Lloyd Pique41be5d22018-06-21 13:11:48 -07002781 typename DispSyncVariant, typename TransitionVariant>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002782struct DisplayPowerCase {
2783 using Display = DisplayVariant;
2784 using Doze = DozeVariant;
2785 using EventThread = EventThreadVariant;
Lloyd Pique41be5d22018-06-21 13:11:48 -07002786 using DispSync = DispSyncVariant;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002787 using Transition = TransitionVariant;
2788
2789 static auto injectDisplayWithInitialPowerMode(DisplayTransactionTest* test, int mode) {
2790 Display::injectHwcDisplay(test);
2791 auto display = Display::makeFakeExistingDisplayInjector(test);
2792 display.inject();
2793 display.mutableDisplayDevice()->setPowerMode(mode);
2794 return display;
2795 }
2796
2797 static void setInitialPrimaryHWVsyncEnabled(DisplayTransactionTest* test, bool enabled) {
2798 test->mFlinger.mutablePrimaryHWVsyncEnabled() = enabled;
2799 }
2800
2801 static void setupRepaintEverythingCallExpectations(DisplayTransactionTest* test) {
2802 EXPECT_CALL(*test->mMessageQueue, invalidate()).Times(1);
2803 }
2804
2805 static void setupSurfaceInterceptorCallExpectations(DisplayTransactionTest* test, int mode) {
2806 EXPECT_CALL(*test->mSurfaceInterceptor, isEnabled()).WillOnce(Return(true));
2807 EXPECT_CALL(*test->mSurfaceInterceptor, savePowerModeUpdate(_, mode)).Times(1);
2808 }
2809
2810 static void setupComposerCallExpectations(DisplayTransactionTest* test,
2811 IComposerClient::PowerMode mode) {
2812 // Any calls to get the active config will return a default value.
2813 EXPECT_CALL(*test->mComposer, getActiveConfig(Display::HWC_DISPLAY_ID, _))
2814 .WillRepeatedly(DoAll(SetArgPointee<1>(Display::HWC_ACTIVE_CONFIG_ID),
2815 Return(Error::NONE)));
2816
2817 // Any calls to get whether the display supports dozing will return the value set by the
2818 // policy variant.
2819 EXPECT_CALL(*test->mComposer, getDozeSupport(Display::HWC_DISPLAY_ID, _))
2820 .WillRepeatedly(DoAll(SetArgPointee<1>(Doze::DOZE_SUPPORTED), Return(Error::NONE)));
2821
2822 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, mode)).Times(1);
2823 }
2824
2825 static void setupNoComposerPowerModeCallExpectations(DisplayTransactionTest* test) {
2826 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, _)).Times(0);
2827 }
2828};
2829
2830// A sample configuration for the primary display.
2831// In addition to having event thread support, we emulate doze support.
2832template <typename TransitionVariant>
2833using PrimaryDisplayPowerCase = DisplayPowerCase<PrimaryDisplayVariant, DozeIsSupportedVariant,
Lloyd Pique41be5d22018-06-21 13:11:48 -07002834 EventThreadIsSupportedVariant,
2835 DispSyncIsSupportedVariant, TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002836
2837// A sample configuration for the external display.
2838// In addition to not having event thread support, we emulate not having doze
2839// support.
2840template <typename TransitionVariant>
Lloyd Pique41be5d22018-06-21 13:11:48 -07002841using ExternalDisplayPowerCase = DisplayPowerCase<ExternalDisplayVariant, DozeNotSupportedVariant,
2842 EventThreadNotSupportedVariant,
2843 DispSyncNotSupportedVariant, TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002844
2845class SetPowerModeInternalTest : public DisplayTransactionTest {
2846public:
2847 template <typename Case>
2848 void transitionDisplayCommon();
2849};
2850
2851template <int PowerMode>
2852struct PowerModeInitialVSyncEnabled : public std::false_type {};
2853
2854template <>
2855struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_NORMAL> : public std::true_type {};
2856
2857template <>
2858struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_DOZE> : public std::true_type {};
2859
2860template <typename Case>
2861void SetPowerModeInternalTest::transitionDisplayCommon() {
2862 // --------------------------------------------------------------------
2863 // Preconditions
2864
2865 auto display =
2866 Case::injectDisplayWithInitialPowerMode(this, Case::Transition::INITIAL_POWER_MODE);
2867 Case::setInitialPrimaryHWVsyncEnabled(this,
2868 PowerModeInitialVSyncEnabled<
2869 Case::Transition::INITIAL_POWER_MODE>::value);
2870
2871 // --------------------------------------------------------------------
2872 // Call Expectations
2873
2874 Case::setupSurfaceInterceptorCallExpectations(this, Case::Transition::TARGET_POWER_MODE);
2875 Case::Transition::template setupCallExpectations<Case>(this);
2876
2877 // --------------------------------------------------------------------
2878 // Invocation
2879
2880 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(),
2881 Case::Transition::TARGET_POWER_MODE);
2882
2883 // --------------------------------------------------------------------
2884 // Postconditions
2885
2886 Case::Transition::verifyPostconditions(this);
2887}
2888
2889TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfNoChange) {
2890 using Case = SimplePrimaryDisplayCase;
2891
2892 // --------------------------------------------------------------------
2893 // Preconditions
2894
2895 // A primary display device is set up
2896 Case::Display::injectHwcDisplay(this);
2897 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2898 display.inject();
2899
Dominik Laskowskia2edf612018-06-01 13:15:16 -07002900 // The display is already set to HWC_POWER_MODE_NORMAL
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002901 display.mutableDisplayDevice()->setPowerMode(HWC_POWER_MODE_NORMAL);
2902
2903 // --------------------------------------------------------------------
2904 // Invocation
2905
2906 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_NORMAL);
2907
2908 // --------------------------------------------------------------------
2909 // Postconditions
2910
2911 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2912}
2913
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002914TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfVirtualDisplay) {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002915 using Case = HwcVirtualDisplayCase;
2916
2917 // --------------------------------------------------------------------
2918 // Preconditions
2919
Dominik Laskowski075d3172018-05-24 15:50:06 -07002920 // Insert display data so that the HWC thinks it created the virtual display.
2921 const auto displayId = Case::Display::DISPLAY_ID::get();
2922 ASSERT_TRUE(displayId);
2923 mFlinger.mutableHwcDisplayData()[*displayId] = {};
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002924
2925 // A virtual display device is set up
2926 Case::Display::injectHwcDisplay(this);
2927 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2928 display.inject();
2929
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002930 // The display is set to HWC_POWER_MODE_NORMAL
2931 getDisplayDevice(display.token())->setPowerMode(HWC_POWER_MODE_NORMAL);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002932
2933 // --------------------------------------------------------------------
2934 // Invocation
2935
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002936 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_OFF);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002937
2938 // --------------------------------------------------------------------
2939 // Postconditions
2940
2941 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2942}
2943
2944TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnPrimaryDisplay) {
2945 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToOnVariant>>();
2946}
2947
2948TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendPrimaryDisplay) {
2949 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
2950}
2951
2952TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffPrimaryDisplay) {
2953 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToOffVariant>>();
2954}
2955
2956TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffPrimaryDisplay) {
2957 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
2958}
2959
2960TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozePrimaryDisplay) {
2961 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeVariant>>();
2962}
2963
2964TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozePrimaryDisplay) {
2965 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
2966}
2967
2968TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnPrimaryDisplay) {
2969 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeToOnVariant>>();
2970}
2971
2972TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnPrimaryDisplay) {
2973 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
2974}
2975
2976TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendPrimaryDisplay) {
2977 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
2978}
2979
2980TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownPrimaryDisplay) {
2981 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToUnknownVariant>>();
2982}
2983
2984TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnExternalDisplay) {
2985 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToOnVariant>>();
2986}
2987
2988TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendExternalDisplay) {
2989 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
2990}
2991
2992TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffExternalDisplay) {
2993 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToOffVariant>>();
2994}
2995
2996TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffExternalDisplay) {
2997 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
2998}
2999
3000TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeExternalDisplay) {
3001 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeVariant>>();
3002}
3003
3004TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozeExternalDisplay) {
3005 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
3006}
3007
3008TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnExternalDisplay) {
3009 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeToOnVariant>>();
3010}
3011
3012TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnExternalDisplay) {
3013 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
3014}
3015
3016TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendExternalDisplay) {
3017 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
3018}
3019
3020TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownExternalDisplay) {
3021 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToUnknownVariant>>();
3022}
3023
Lloyd Piquef58625d2017-12-19 13:22:33 -08003024} // namespace
3025} // namespace android