blob: 8d18d76ffff3751ea0ac6ff689ebc08ecb6f5b9e [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
20#include <gmock/gmock.h>
21#include <gtest/gtest.h>
22
23#include <log/log.h>
24
25#include "TestableSurfaceFlinger.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080026#include "mock/DisplayHardware/MockComposer.h"
27#include "mock/DisplayHardware/MockDisplaySurface.h"
28#include "mock/MockEventControlThread.h"
29#include "mock/MockEventThread.h"
30#include "mock/MockMessageQueue.h"
31#include "mock/MockNativeWindowSurface.h"
32#include "mock/MockSurfaceInterceptor.h"
33#include "mock/RenderEngine/MockRenderEngine.h"
34#include "mock/gui/MockGraphicBufferConsumer.h"
35#include "mock/gui/MockGraphicBufferProducer.h"
36#include "mock/system/window/MockNativeWindow.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080037
38namespace android {
39namespace {
40
Lloyd Piquee39cad22017-12-20 17:01:29 -080041using testing::_;
42using testing::ByMove;
43using testing::DoAll;
44using testing::Mock;
45using testing::Return;
46using testing::SetArgPointee;
47
Lloyd Piqued883d5a2018-04-27 19:32:30 -070048using android::Hwc2::ColorMode;
Lloyd Piquee39cad22017-12-20 17:01:29 -080049using android::Hwc2::Error;
Lloyd Piqued883d5a2018-04-27 19:32:30 -070050using android::Hwc2::Hdr;
Lloyd Piquee39cad22017-12-20 17:01:29 -080051using android::Hwc2::IComposer;
52using android::Hwc2::IComposerClient;
Lloyd Piqued883d5a2018-04-27 19:32:30 -070053using android::Hwc2::PerFrameMetadataKey;
54using android::Hwc2::RenderIntent;
Lloyd Piquee39cad22017-12-20 17:01:29 -080055
Lloyd Piquec11e0d32018-01-22 18:44:59 -080056using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector;
57using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
Lloyd Pique1fa4d462018-01-22 18:03:16 -080058using HotplugEvent = TestableSurfaceFlinger::HotplugEvent;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080059using HWC2Display = TestableSurfaceFlinger::HWC2Display;
Lloyd Piquebc792092018-01-17 11:52:30 -080060
Lloyd Piquec11e0d32018-01-22 18:44:59 -080061constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666;
Lloyd Piquee39cad22017-12-20 17:01:29 -080062constexpr int32_t DEFAULT_DPI = 320;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080063constexpr int DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT = HAL_PIXEL_FORMAT_RGB_565;
Lloyd Piquee39cad22017-12-20 17:01:29 -080064
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -080065constexpr int HWC_POWER_MODE_LEET = 1337; // An out of range power mode value
66
Lloyd Piquec11e0d32018-01-22 18:44:59 -080067/* ------------------------------------------------------------------------
68 * Boolean avoidance
69 *
70 * To make calls and template instantiations more readable, we define some
71 * local enums along with an implicit bool conversion.
72 */
73
74#define BOOL_SUBSTITUTE(TYPENAME) enum class TYPENAME : bool { FALSE = false, TRUE = true };
75
76BOOL_SUBSTITUTE(Critical);
77BOOL_SUBSTITUTE(Async);
78BOOL_SUBSTITUTE(Secure);
79
80/* ------------------------------------------------------------------------
81 *
82 */
Lloyd Pique1fa4d462018-01-22 18:03:16 -080083
Lloyd Piquef58625d2017-12-19 13:22:33 -080084class DisplayTransactionTest : public testing::Test {
Lloyd Piquec11e0d32018-01-22 18:44:59 -080085public:
Lloyd Piquef58625d2017-12-19 13:22:33 -080086 DisplayTransactionTest();
87 ~DisplayTransactionTest() override;
88
Lloyd Pique1fa4d462018-01-22 18:03:16 -080089 // --------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -080090 // Mock/Fake injection
Lloyd Piquef58625d2017-12-19 13:22:33 -080091
Lloyd Piquec11e0d32018-01-22 18:44:59 -080092 void injectMockComposer(int virtualDisplayCount);
93 void injectFakeBufferQueueFactory();
94 void injectFakeNativeWindowSurfaceFactory();
Lloyd Pique1fa4d462018-01-22 18:03:16 -080095
96 // --------------------------------------------------------------------
97 // Postcondition helpers
98
Lloyd Piquec11e0d32018-01-22 18:44:59 -080099 bool hasHwcDisplay(hwc2_display_t displayId);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800100 bool hasTransactionFlagSet(int flag);
101 bool hasDisplayDevice(sp<IBinder> displayToken);
102 sp<DisplayDevice> getDisplayDevice(sp<IBinder> displayToken);
103 bool hasCurrentDisplayState(sp<IBinder> displayToken);
104 const DisplayDeviceState& getCurrentDisplayState(sp<IBinder> displayToken);
105 bool hasDrawingDisplayState(sp<IBinder> displayToken);
106 const DisplayDeviceState& getDrawingDisplayState(sp<IBinder> displayToken);
107
108 // --------------------------------------------------------------------
109 // Test instances
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800110
Lloyd Piquef58625d2017-12-19 13:22:33 -0800111 TestableSurfaceFlinger mFlinger;
Lloyd Piquee39cad22017-12-20 17:01:29 -0800112 mock::EventThread* mEventThread = new mock::EventThread();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800113 mock::EventControlThread* mEventControlThread = new mock::EventControlThread();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800114
115 // These mocks are created by the test, but are destroyed by SurfaceFlinger
116 // by virtue of being stored into a std::unique_ptr. However we still need
117 // to keep a reference to them for use in setting up call expectations.
118 RE::mock::RenderEngine* mRenderEngine = new RE::mock::RenderEngine();
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800119 Hwc2::mock::Composer* mComposer = nullptr;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800120 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
121 mock::SurfaceInterceptor* mSurfaceInterceptor = new mock::SurfaceInterceptor();
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800122
123 // These mocks are created only when expected to be created via a factory.
124 sp<mock::GraphicBufferConsumer> mConsumer;
125 sp<mock::GraphicBufferProducer> mProducer;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800126 mock::NativeWindowSurface* mNativeWindowSurface = nullptr;
127 sp<mock::NativeWindow> mNativeWindow;
128 RE::mock::Surface* mRenderSurface = nullptr;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800129};
130
131DisplayTransactionTest::DisplayTransactionTest() {
132 const ::testing::TestInfo* const test_info =
133 ::testing::UnitTest::GetInstance()->current_test_info();
134 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800135
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800136 // Default to no wide color display support configured
137 mFlinger.mutableHasWideColorDisplay() = false;
138 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
139
140 // Default to using HWC virtual displays
141 mFlinger.mutableUseHwcVirtualDisplays() = true;
142
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800143 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
144 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
145 });
146
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800147 mFlinger.setCreateNativeWindowSurface([](auto) {
148 ADD_FAILURE() << "Unexpected request to create a native window surface.";
149 return nullptr;
150 });
151
152 mFlinger.mutableEventControlThread().reset(mEventControlThread);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800153 mFlinger.mutableEventThread().reset(mEventThread);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800154 mFlinger.mutableEventQueue().reset(mMessageQueue);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800155 mFlinger.setupRenderEngine(std::unique_ptr<RE::RenderEngine>(mRenderEngine));
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800156 mFlinger.mutableInterceptor().reset(mSurfaceInterceptor);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800157
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800158 injectMockComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -0800159}
160
161DisplayTransactionTest::~DisplayTransactionTest() {
162 const ::testing::TestInfo* const test_info =
163 ::testing::UnitTest::GetInstance()->current_test_info();
164 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
165}
166
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800167void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
168 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800169 EXPECT_CALL(*mComposer, getCapabilities())
170 .WillOnce(Return(std::vector<IComposer::Capability>()));
171 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
172 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -0800173
Lloyd Piquee39cad22017-12-20 17:01:29 -0800174 Mock::VerifyAndClear(mComposer);
175}
176
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800177void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800178 // This setup is only expected once per test.
179 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
180
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800181 mConsumer = new mock::GraphicBufferConsumer();
182 mProducer = new mock::GraphicBufferProducer();
183
184 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
185 *outProducer = mProducer;
186 *outConsumer = mConsumer;
187 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800188}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800189
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800190void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800191 // This setup is only expected once per test.
192 ASSERT_TRUE(mNativeWindowSurface == nullptr);
193
194 mNativeWindowSurface = new mock::NativeWindowSurface();
195 mNativeWindow = new mock::NativeWindow();
196
197 mFlinger.setCreateNativeWindowSurface(
198 [this](auto) { return std::unique_ptr<NativeWindowSurface>(mNativeWindowSurface); });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800199}
200
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800201bool DisplayTransactionTest::hasHwcDisplay(hwc2_display_t displayId) {
202 return mFlinger.mutableHwcDisplaySlots().count(displayId) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800203}
204
205bool DisplayTransactionTest::hasTransactionFlagSet(int flag) {
206 return mFlinger.mutableTransactionFlags() & flag;
207}
208
209bool DisplayTransactionTest::hasDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700210 return mFlinger.mutableDisplays().count(displayToken) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800211}
212
213sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700214 return mFlinger.mutableDisplays()[displayToken];
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800215}
216
217bool DisplayTransactionTest::hasCurrentDisplayState(sp<IBinder> displayToken) {
218 return mFlinger.mutableCurrentState().displays.indexOfKey(displayToken) >= 0;
219}
220
221const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(sp<IBinder> displayToken) {
222 return mFlinger.mutableCurrentState().displays.valueFor(displayToken);
223}
224
225bool DisplayTransactionTest::hasDrawingDisplayState(sp<IBinder> displayToken) {
226 return mFlinger.mutableDrawingState().displays.indexOfKey(displayToken) >= 0;
227}
228
229const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(sp<IBinder> displayToken) {
230 return mFlinger.mutableDrawingState().displays.valueFor(displayToken);
231}
232
233/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800234 *
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800235 */
236
Dominik Laskowski7e045462018-05-30 13:02:02 -0700237template <DisplayDevice::DisplayType type, DisplayDevice::DisplayType displayId, int width,
238 int height, Critical critical, Async async, Secure secure, int grallocUsage>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800239struct DisplayVariant {
240 // The display width and height
241 static constexpr int WIDTH = width;
242 static constexpr int HEIGHT = height;
243
244 static constexpr int GRALLOC_USAGE = grallocUsage;
245
246 // The type for this display
247 static constexpr DisplayDevice::DisplayType TYPE = type;
Dominik Laskowski7e045462018-05-30 13:02:02 -0700248 static constexpr DisplayDevice::DisplayType DISPLAY_ID = displayId;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800249
250 // When creating native window surfaces for the framebuffer, whether those should be critical
251 static constexpr Critical CRITICAL = critical;
252
253 // When creating native window surfaces for the framebuffer, whether those should be async
254 static constexpr Async ASYNC = async;
255
256 // Whether the display should be treated as secure
257 static constexpr Secure SECURE = secure;
258
259 static auto makeFakeExistingDisplayInjector(DisplayTransactionTest* test) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700260 auto injector = FakeDisplayDeviceInjector(test->mFlinger, TYPE, DISPLAY_ID);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800261 injector.setSecure(static_cast<bool>(SECURE));
262 return injector;
263 }
264
265 // Called by tests to set up any native window creation call expectations.
266 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
267 EXPECT_CALL(*test->mNativeWindowSurface, getNativeWindow())
268 .WillOnce(Return(test->mNativeWindow));
269 EXPECT_CALL(*test->mNativeWindow, perform(19)).WillRepeatedly(Return(NO_ERROR));
270
271 // For simplicity, we only expect to create a single render surface for
272 // each test.
273 ASSERT_TRUE(test->mRenderSurface == nullptr);
274 test->mRenderSurface = new RE::mock::Surface();
275 EXPECT_CALL(*test->mRenderEngine, createSurface())
276 .WillOnce(Return(ByMove(std::unique_ptr<RE::Surface>(test->mRenderSurface))));
277 EXPECT_CALL(*test->mRenderSurface, setAsync(static_cast<bool>(ASYNC))).Times(1);
278 EXPECT_CALL(*test->mRenderSurface, setCritical(static_cast<bool>(CRITICAL))).Times(1);
279 EXPECT_CALL(*test->mRenderSurface, setNativeWindow(test->mNativeWindow.get())).Times(1);
280 EXPECT_CALL(*test->mRenderSurface, queryWidth()).WillOnce(Return(WIDTH));
281 EXPECT_CALL(*test->mRenderSurface, queryHeight()).WillOnce(Return(HEIGHT));
282 }
283
284 static void setupFramebufferConsumerBufferQueueCallExpectations(DisplayTransactionTest* test) {
285 EXPECT_CALL(*test->mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR));
286 EXPECT_CALL(*test->mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR));
287 EXPECT_CALL(*test->mConsumer, setConsumerUsageBits(GRALLOC_USAGE))
288 .WillRepeatedly(Return(NO_ERROR));
289 EXPECT_CALL(*test->mConsumer, setDefaultBufferSize(WIDTH, HEIGHT))
290 .WillRepeatedly(Return(NO_ERROR));
291 EXPECT_CALL(*test->mConsumer, setMaxAcquiredBufferCount(_))
292 .WillRepeatedly(Return(NO_ERROR));
293 }
294
295 static void setupFramebufferProducerBufferQueueCallExpectations(DisplayTransactionTest* test) {
296 EXPECT_CALL(*test->mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return());
297 }
298};
299
300template <hwc2_display_t hwcDisplayId, HWC2::DisplayType hwcDisplayType, typename DisplayVariant>
301struct HwcDisplayVariant {
302 // The display id supplied by the HWC
303 static constexpr hwc2_display_t HWC_DISPLAY_ID = hwcDisplayId;
304
305 // The HWC display type
306 static constexpr HWC2::DisplayType HWC_DISPLAY_TYPE = hwcDisplayType;
307
308 // The HWC active configuration id
Lloyd Pique3c085a02018-05-09 19:38:32 -0700309 static constexpr int HWC_ACTIVE_CONFIG_ID = 2001;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800310
311 static void injectPendingHotplugEvent(DisplayTransactionTest* test,
312 HWC2::Connection connection) {
313 test->mFlinger.mutablePendingHotplugEvents().emplace_back(
314 HotplugEvent{HWC_DISPLAY_ID, connection});
315 }
316
317 // Called by tests to inject a HWC display setup
318 static void injectHwcDisplay(DisplayTransactionTest* test) {
319 FakeHwcDisplayInjector(DisplayVariant::TYPE, HWC_DISPLAY_TYPE)
320 .setHwcDisplayId(HWC_DISPLAY_ID)
321 .setWidth(DisplayVariant::WIDTH)
322 .setHeight(DisplayVariant::HEIGHT)
323 .setActiveConfig(HWC_ACTIVE_CONFIG_ID)
324 .inject(&test->mFlinger, test->mComposer);
325 }
326
327 static void setupHwcHotplugCallExpectations(DisplayTransactionTest* test) {
328 EXPECT_CALL(*test->mComposer, getDisplayType(HWC_DISPLAY_ID, _))
329 .WillOnce(DoAll(SetArgPointee<1>(static_cast<IComposerClient::DisplayType>(
330 HWC_DISPLAY_TYPE)),
331 Return(Error::NONE)));
332 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
333 EXPECT_CALL(*test->mComposer, getDisplayConfigs(HWC_DISPLAY_ID, _))
334 .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{HWC_ACTIVE_CONFIG_ID}),
335 Return(Error::NONE)));
336 EXPECT_CALL(*test->mComposer,
337 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
338 IComposerClient::Attribute::WIDTH, _))
339 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::WIDTH), Return(Error::NONE)));
340 EXPECT_CALL(*test->mComposer,
341 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
342 IComposerClient::Attribute::HEIGHT, _))
343 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::HEIGHT), Return(Error::NONE)));
344 EXPECT_CALL(*test->mComposer,
345 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
346 IComposerClient::Attribute::VSYNC_PERIOD, _))
347 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE)));
348 EXPECT_CALL(*test->mComposer,
349 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
350 IComposerClient::Attribute::DPI_X, _))
351 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
352 EXPECT_CALL(*test->mComposer,
353 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
354 IComposerClient::Attribute::DPI_Y, _))
355 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700356 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
357 .WillRepeatedly(Return(Error::UNSUPPORTED));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800358 }
359
360 // Called by tests to set up HWC call expectations
361 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
362 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY_ID, _))
Lloyd Pique3c085a02018-05-09 19:38:32 -0700363 .WillRepeatedly(DoAll(SetArgPointee<1>(HWC_ACTIVE_CONFIG_ID), Return(Error::NONE)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800364 }
365};
366
367struct NonHwcDisplayVariant {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800368 static void injectHwcDisplay(DisplayTransactionTest*) {}
369
370 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
371 EXPECT_CALL(*test->mComposer, getActiveConfig(_, _)).Times(0);
372 }
373};
374
375// Physical displays are expected to be synchronous, secure, and have a HWC display for output.
376constexpr uint32_t GRALLOC_USAGE_PHYSICAL_DISPLAY =
377 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
378
379template <hwc2_display_t hwcDisplayId, DisplayDevice::DisplayType type, int width, int height,
380 Critical critical>
381struct PhysicalDisplayVariant
382 : public DisplayVariant<type, type, width, height, critical, Async::FALSE, Secure::TRUE,
383 GRALLOC_USAGE_PHYSICAL_DISPLAY>,
384 public HwcDisplayVariant<hwcDisplayId, HWC2::DisplayType::Physical,
385 DisplayVariant<type, type, width, height, critical, Async::FALSE,
386 Secure::TRUE, GRALLOC_USAGE_PHYSICAL_DISPLAY>> {};
387
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800388// An invalid display
389using InvalidDisplayVariant =
390 DisplayVariant<DisplayDevice::DISPLAY_ID_INVALID, DisplayDevice::DISPLAY_ID_INVALID, 0, 0,
391 Critical::FALSE, Async::FALSE, Secure::FALSE, 0>;
392
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800393// A primary display is a physical display that is critical
394using PrimaryDisplayVariant =
395 PhysicalDisplayVariant<1001, DisplayDevice::DISPLAY_PRIMARY, 3840, 2160, Critical::TRUE>;
396
397// An external display is physical display that is not critical.
398using ExternalDisplayVariant =
399 PhysicalDisplayVariant<1002, DisplayDevice::DISPLAY_EXTERNAL, 1920, 1280, Critical::FALSE>;
400
401using TertiaryDisplayVariant =
402 PhysicalDisplayVariant<1003, DisplayDevice::DISPLAY_EXTERNAL, 1600, 1200, Critical::FALSE>;
403
404// A virtual display not supported by the HWC.
405constexpr uint32_t GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY = 0;
406
407template <int width, int height, Secure secure>
408struct NonHwcVirtualDisplayVariant
409 : public DisplayVariant<DisplayDevice::DISPLAY_VIRTUAL, DisplayDevice::DISPLAY_ID_INVALID,
410 width, height, Critical::FALSE, Async::TRUE, secure,
411 GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>,
412 public NonHwcDisplayVariant {
413 using Base = DisplayVariant<DisplayDevice::DISPLAY_VIRTUAL, DisplayDevice::DISPLAY_ID_INVALID,
414 width, height, Critical::FALSE, Async::TRUE, secure,
415 GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>;
416
417 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
418 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
419 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
420 }
421};
422
423// A virtual display supported by the HWC.
424constexpr uint32_t GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY = GRALLOC_USAGE_HW_COMPOSER;
425
426template <int width, int height, Secure secure>
427struct HwcVirtualDisplayVariant
428 : public DisplayVariant<DisplayDevice::DISPLAY_VIRTUAL, DisplayDevice::DISPLAY_VIRTUAL, width,
429 height, Critical::FALSE, Async::TRUE, secure,
430 GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>,
431 public HwcDisplayVariant<1010, HWC2::DisplayType::Virtual,
432 NonHwcVirtualDisplayVariant<width, height, secure>> {
433 using Base =
434 DisplayVariant<DisplayDevice::DISPLAY_VIRTUAL, DisplayDevice::DISPLAY_VIRTUAL, width,
435 height, Critical::FALSE, Async::TRUE, secure, GRALLOC_USAGE_HW_COMPOSER>;
436 using Self = HwcVirtualDisplayVariant<width, height, secure>;
437
438 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
439 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
440 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
441 }
442
443 static void setupHwcVirtualDisplayCreationCallExpectations(DisplayTransactionTest* test) {
444 EXPECT_CALL(*test->mComposer, createVirtualDisplay(Base::WIDTH, Base::HEIGHT, _, _))
445 .WillOnce(DoAll(SetArgPointee<3>(Self::HWC_DISPLAY_ID), Return(Error::NONE)));
446 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
447 }
448};
449
450// For this variant, SurfaceFlinger should not configure itself with wide
451// display support, so the display should not be configured for wide-color
452// support.
453struct WideColorSupportNotConfiguredVariant {
454 static constexpr bool WIDE_COLOR_SUPPORTED = false;
455
456 static void injectConfigChange(DisplayTransactionTest* test) {
457 test->mFlinger.mutableHasWideColorDisplay() = false;
458 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
459 }
460
461 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
462 EXPECT_CALL(*test->mComposer, getColorModes(_, _)).Times(0);
463 EXPECT_CALL(*test->mComposer, getRenderIntents(_, _, _)).Times(0);
464 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
465 }
466};
467
468// For this variant, SurfaceFlinger should configure itself with wide display
469// support, and the display should respond with an non-empty list of supported
470// color modes. Wide-color support should be configured.
471template <typename Display>
472struct WideColorP3ColorimetricSupportedVariant {
473 static constexpr bool WIDE_COLOR_SUPPORTED = true;
474
475 static void injectConfigChange(DisplayTransactionTest* test) {
476 test->mFlinger.mutableHasWideColorDisplay() = true;
477 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
478 }
479
480 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
481 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
482 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>({ColorMode::DISPLAY_P3})),
483 Return(Error::NONE)));
484 EXPECT_CALL(*test->mComposer,
485 getRenderIntents(Display::HWC_DISPLAY_ID, ColorMode::DISPLAY_P3, _))
486 .WillOnce(DoAll(SetArgPointee<2>(
487 std::vector<RenderIntent>({RenderIntent::COLORIMETRIC})),
488 Return(Error::NONE)));
489 EXPECT_CALL(*test->mComposer,
490 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::SRGB,
491 RenderIntent::COLORIMETRIC))
492 .WillOnce(Return(Error::NONE));
493 }
494};
495
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800496// For this variant, SurfaceFlinger should configure itself with wide display
497// support, but the display should respond with an empty list of supported color
498// modes. Wide-color support for the display should not be configured.
499template <typename Display>
500struct WideColorNotSupportedVariant {
501 static constexpr bool WIDE_COLOR_SUPPORTED = false;
502
503 static void injectConfigChange(DisplayTransactionTest* test) {
504 test->mFlinger.mutableHasWideColorDisplay() = true;
505 }
506
507 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
508 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
509 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>()), Return(Error::NONE)));
Chia-I Wu614e1422018-05-23 02:17:03 -0700510 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800511 }
512};
513
514// For this variant, the display is not a HWC display, so no HDR support should
515// be configured.
516struct NonHwcDisplayHdrSupportVariant {
517 static constexpr bool HDR10_SUPPORTED = false;
518 static constexpr bool HDR_HLG_SUPPORTED = false;
519 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
520 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
521 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
522 }
523};
524
525// For this variant, the composer should respond with a non-empty list of HDR
526// modes containing HDR10, so HDR10 support should be configured.
527template <typename Display>
528struct Hdr10SupportedVariant {
529 static constexpr bool HDR10_SUPPORTED = true;
530 static constexpr bool HDR_HLG_SUPPORTED = false;
531 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
532 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
533 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
534 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HDR10})),
535 Return(Error::NONE)));
536 }
537};
538
539// For this variant, the composer should respond with a non-empty list of HDR
540// modes containing HLG, so HLG support should be configured.
541template <typename Display>
542struct HdrHlgSupportedVariant {
543 static constexpr bool HDR10_SUPPORTED = false;
544 static constexpr bool HDR_HLG_SUPPORTED = true;
545 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
546 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
547 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
548 .WillOnce(
549 DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HLG})), Return(Error::NONE)));
550 }
551};
552
553// For this variant, the composer should respond with a non-empty list of HDR
554// modes containing DOLBY_VISION, so DOLBY_VISION support should be configured.
555template <typename Display>
556struct HdrDolbyVisionSupportedVariant {
557 static constexpr bool HDR10_SUPPORTED = false;
558 static constexpr bool HDR_HLG_SUPPORTED = false;
559 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = true;
560 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
561 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
562 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::DOLBY_VISION})),
563 Return(Error::NONE)));
564 }
565};
566
567// For this variant, the composer should respond with am empty list of HDR
568// modes, so no HDR support should be configured.
569template <typename Display>
570struct HdrNotSupportedVariant {
571 static constexpr bool HDR10_SUPPORTED = false;
572 static constexpr bool HDR_HLG_SUPPORTED = false;
573 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
574 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
575 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
576 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
577 }
578};
579
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700580struct NonHwcPerFrameMetadataSupportVariant {
581 static constexpr int PER_FRAME_METADATA_KEYS = 0;
582 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
583 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(_, _)).Times(0);
584 }
585};
586
587template <typename Display>
588struct NoPerFrameMetadataSupportVariant {
589 static constexpr int PER_FRAME_METADATA_KEYS = 0;
590 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
591 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID, _))
592 .WillOnce(DoAll(SetArgPointee<1>(std::vector<PerFrameMetadataKey>()),
593 Return(Error::NONE)));
594 }
595};
596
597template <typename Display>
598struct Smpte2086PerFrameMetadataSupportVariant {
599 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::SMPTE2086;
600 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
601 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID, _))
602 .WillOnce(DoAll(SetArgPointee<1>(std::vector<PerFrameMetadataKey>({
603 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X,
604 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y,
605 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X,
606 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y,
607 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X,
608 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y,
609 PerFrameMetadataKey::WHITE_POINT_X,
610 PerFrameMetadataKey::WHITE_POINT_Y,
611 PerFrameMetadataKey::MAX_LUMINANCE,
612 PerFrameMetadataKey::MIN_LUMINANCE,
613 })),
614 Return(Error::NONE)));
615 }
616};
617
618template <typename Display>
619struct Cta861_3_PerFrameMetadataSupportVariant {
620 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::CTA861_3;
621 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
622 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID, _))
623 .WillOnce(DoAll(SetArgPointee<1>(std::vector<PerFrameMetadataKey>({
624 PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL,
625 PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL,
626 })),
627 Return(Error::NONE)));
628 }
629};
630
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800631/* ------------------------------------------------------------------------
632 * Typical display configurations to test
633 */
634
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700635template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy,
636 typename PerFrameMetadataSupportPolicy>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800637struct Case {
638 using Display = DisplayPolicy;
639 using WideColorSupport = WideColorSupportPolicy;
640 using HdrSupport = HdrSupportPolicy;
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700641 using PerFrameMetadataSupport = PerFrameMetadataSupportPolicy;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800642};
643
644using SimplePrimaryDisplayCase =
645 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700646 HdrNotSupportedVariant<PrimaryDisplayVariant>,
647 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800648using SimpleExternalDisplayCase =
649 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700650 HdrNotSupportedVariant<ExternalDisplayVariant>,
651 NoPerFrameMetadataSupportVariant<ExternalDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800652using SimpleTertiaryDisplayCase =
653 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700654 HdrNotSupportedVariant<TertiaryDisplayVariant>,
655 NoPerFrameMetadataSupportVariant<TertiaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800656using NonHwcVirtualDisplayCase =
657 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700658 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant,
659 NonHwcPerFrameMetadataSupportVariant>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800660using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
661using HwcVirtualDisplayCase =
662 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700663 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>,
664 NoPerFrameMetadataSupportVariant<SimpleHwcVirtualDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800665using WideColorP3ColorimetricDisplayCase =
666 Case<PrimaryDisplayVariant, WideColorP3ColorimetricSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700667 HdrNotSupportedVariant<PrimaryDisplayVariant>,
668 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800669using Hdr10DisplayCase =
670 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700671 Hdr10SupportedVariant<PrimaryDisplayVariant>,
672 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800673using HdrHlgDisplayCase =
674 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700675 HdrHlgSupportedVariant<PrimaryDisplayVariant>,
676 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800677using HdrDolbyVisionDisplayCase =
678 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700679 HdrDolbyVisionSupportedVariant<PrimaryDisplayVariant>,
680 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
681using HdrSmpte2086DisplayCase =
682 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
683 HdrNotSupportedVariant<PrimaryDisplayVariant>,
684 Smpte2086PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
685using HdrCta861_3_DisplayCase =
686 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
687 HdrNotSupportedVariant<PrimaryDisplayVariant>,
688 Cta861_3_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800689using InvalidDisplayCase = Case<InvalidDisplayVariant, WideColorSupportNotConfiguredVariant,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700690 NonHwcDisplayHdrSupportVariant,
691 NoPerFrameMetadataSupportVariant<InvalidDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800692/* ------------------------------------------------------------------------
Lloyd Pique6cf11032018-01-22 18:57:44 -0800693 *
694 * SurfaceFlinger::onHotplugReceived
695 */
696
697TEST_F(DisplayTransactionTest, hotplugEnqueuesEventsForDisplayTransaction) {
698 constexpr int currentSequenceId = 123;
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700699 constexpr hwc2_display_t hwcDisplayId1 = 456;
700 constexpr hwc2_display_t hwcDisplayId2 = 654;
Lloyd Pique6cf11032018-01-22 18:57:44 -0800701
702 // --------------------------------------------------------------------
703 // Preconditions
704
705 // Set the current sequence id for accepted events
706 mFlinger.mutableComposerSequenceId() = currentSequenceId;
707
708 // Set the main thread id so that the current thread does not appear to be
709 // the main thread.
710 mFlinger.mutableMainThreadId() = std::thread::id();
711
712 // --------------------------------------------------------------------
713 // Call Expectations
714
715 // We expect invalidate() to be invoked once to trigger display transaction
716 // processing.
717 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
718
719 // --------------------------------------------------------------------
720 // Invocation
721
722 // Simulate two hotplug events (a connect and a disconnect)
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700723 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId1, HWC2::Connection::Connected);
724 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId2, HWC2::Connection::Disconnected);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800725
726 // --------------------------------------------------------------------
727 // Postconditions
728
729 // The display transaction needed flag should be set.
730 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
731
732 // All events should be in the pending event queue.
733 const auto& pendingEvents = mFlinger.mutablePendingHotplugEvents();
734 ASSERT_EQ(2u, pendingEvents.size());
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700735 EXPECT_EQ(hwcDisplayId1, pendingEvents[0].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800736 EXPECT_EQ(HWC2::Connection::Connected, pendingEvents[0].connection);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700737 EXPECT_EQ(hwcDisplayId2, pendingEvents[1].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800738 EXPECT_EQ(HWC2::Connection::Disconnected, pendingEvents[1].connection);
739}
740
741TEST_F(DisplayTransactionTest, hotplugDiscardsUnexpectedEvents) {
742 constexpr int currentSequenceId = 123;
743 constexpr int otherSequenceId = 321;
744 constexpr hwc2_display_t displayId = 456;
745
746 // --------------------------------------------------------------------
747 // Preconditions
748
749 // Set the current sequence id for accepted events
750 mFlinger.mutableComposerSequenceId() = currentSequenceId;
751
752 // Set the main thread id so that the current thread does not appear to be
753 // the main thread.
754 mFlinger.mutableMainThreadId() = std::thread::id();
755
756 // --------------------------------------------------------------------
757 // Call Expectations
758
759 // We do not expect any calls to invalidate().
760 EXPECT_CALL(*mMessageQueue, invalidate()).Times(0);
761
762 // --------------------------------------------------------------------
763 // Invocation
764
765 // Call with an unexpected sequence id
766 mFlinger.onHotplugReceived(otherSequenceId, displayId, HWC2::Connection::Invalid);
767
768 // --------------------------------------------------------------------
769 // Postconditions
770
771 // The display transaction needed flag should not be set
772 EXPECT_FALSE(hasTransactionFlagSet(eDisplayTransactionNeeded));
773
774 // There should be no pending events
775 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
776}
777
778TEST_F(DisplayTransactionTest, hotplugProcessesEnqueuedEventsIfCalledOnMainThread) {
779 constexpr int currentSequenceId = 123;
780 constexpr hwc2_display_t displayId1 = 456;
781
782 // --------------------------------------------------------------------
783 // Note:
784 // --------------------------------------------------------------------
785 // This test case is a bit tricky. We want to verify that
786 // onHotplugReceived() calls processDisplayHotplugEventsLocked(), but we
787 // don't really want to provide coverage for everything the later function
788 // does as there are specific tests for it.
789 // --------------------------------------------------------------------
790
791 // --------------------------------------------------------------------
792 // Preconditions
793
794 // Set the current sequence id for accepted events
795 mFlinger.mutableComposerSequenceId() = currentSequenceId;
796
797 // Set the main thread id so that the current thread does appear to be the
798 // main thread.
799 mFlinger.mutableMainThreadId() = std::this_thread::get_id();
800
801 // --------------------------------------------------------------------
802 // Call Expectations
803
804 // We expect invalidate() to be invoked once to trigger display transaction
805 // processing.
806 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
807
808 // --------------------------------------------------------------------
809 // Invocation
810
811 // Simulate a disconnect on a display id that is not connected. This should
812 // be enqueued by onHotplugReceived(), and dequeued by
813 // processDisplayHotplugEventsLocked(), but then ignored as invalid.
814 mFlinger.onHotplugReceived(currentSequenceId, displayId1, HWC2::Connection::Disconnected);
815
816 // --------------------------------------------------------------------
817 // Postconditions
818
819 // The display transaction needed flag should be set.
820 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
821
822 // There should be no event queued on return, as it should have been
823 // processed.
824 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
825}
826
827/* ------------------------------------------------------------------------
Lloyd Piquea482f992018-01-22 19:00:34 -0800828 * SurfaceFlinger::createDisplay
829 */
830
831TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForNonsecureDisplay) {
832 const String8 name("virtual.test");
833
834 // --------------------------------------------------------------------
835 // Call Expectations
836
837 // The call should notify the interceptor that a display was created.
838 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
839
840 // --------------------------------------------------------------------
841 // Invocation
842
843 sp<IBinder> displayToken = mFlinger.createDisplay(name, false);
844
845 // --------------------------------------------------------------------
846 // Postconditions
847
848 // The display should have been added to the current state
849 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
850 const auto& display = getCurrentDisplayState(displayToken);
851 EXPECT_EQ(DisplayDevice::DISPLAY_VIRTUAL, display.type);
852 EXPECT_EQ(false, display.isSecure);
853 EXPECT_EQ(name.string(), display.displayName);
854
855 // --------------------------------------------------------------------
856 // Cleanup conditions
857
858 // Destroying the display invalidates the display state.
859 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
860}
861
862TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForSecureDisplay) {
863 const String8 name("virtual.test");
864
865 // --------------------------------------------------------------------
866 // Call Expectations
867
868 // The call should notify the interceptor that a display was created.
869 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
870
871 // --------------------------------------------------------------------
872 // Invocation
873
874 sp<IBinder> displayToken = mFlinger.createDisplay(name, true);
875
876 // --------------------------------------------------------------------
877 // Postconditions
878
879 // The display should have been added to the current state
880 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
881 const auto& display = getCurrentDisplayState(displayToken);
882 EXPECT_EQ(DisplayDevice::DISPLAY_VIRTUAL, display.type);
883 EXPECT_EQ(true, display.isSecure);
884 EXPECT_EQ(name.string(), display.displayName);
885
886 // --------------------------------------------------------------------
887 // Cleanup conditions
888
889 // Destroying the display invalidates the display state.
890 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
891}
892
893/* ------------------------------------------------------------------------
894 * SurfaceFlinger::destroyDisplay
895 */
896
897TEST_F(DisplayTransactionTest, destroyDisplayClearsCurrentStateForDisplay) {
898 using Case = NonHwcVirtualDisplayCase;
899
900 // --------------------------------------------------------------------
901 // Preconditions
902
903 // A virtual display exists
904 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
905 existing.inject();
906
907 // --------------------------------------------------------------------
908 // Call Expectations
909
910 // The call should notify the interceptor that a display was created.
911 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
912
913 // Destroying the display invalidates the display state.
914 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
915
916 // --------------------------------------------------------------------
917 // Invocation
918
919 mFlinger.destroyDisplay(existing.token());
920
921 // --------------------------------------------------------------------
922 // Postconditions
923
924 // The display should have been removed from the current state
925 EXPECT_FALSE(hasCurrentDisplayState(existing.token()));
926
927 // Ths display should still exist in the drawing state
928 EXPECT_TRUE(hasDrawingDisplayState(existing.token()));
929
930 // The display transaction needed flasg should be set
931 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
932}
933
934TEST_F(DisplayTransactionTest, destroyDisplayHandlesUnknownDisplay) {
935 // --------------------------------------------------------------------
936 // Preconditions
937
938 sp<BBinder> displayToken = new BBinder();
939
940 // --------------------------------------------------------------------
941 // Invocation
942
943 mFlinger.destroyDisplay(displayToken);
944}
945
946/* ------------------------------------------------------------------------
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -0800947 * SurfaceFlinger::resetDisplayState
948 */
949
950TEST_F(DisplayTransactionTest, resetDisplayStateClearsState) {
951 using Case = NonHwcVirtualDisplayCase;
952
953 // --------------------------------------------------------------------
954 // Preconditions
955
956 // vsync is enabled and available
957 mFlinger.mutablePrimaryHWVsyncEnabled() = true;
958 mFlinger.mutableHWVsyncAvailable() = true;
959
960 // A display exists
961 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
962 existing.inject();
963
964 // --------------------------------------------------------------------
965 // Call Expectations
966
967 // The call disable vsyncs
968 EXPECT_CALL(*mEventControlThread, setVsyncEnabled(false)).Times(1);
969
970 // The call clears the current render engine surface
971 EXPECT_CALL(*mRenderEngine, resetCurrentSurface());
972
973 // --------------------------------------------------------------------
974 // Invocation
975
976 mFlinger.resetDisplayState();
977
978 // --------------------------------------------------------------------
979 // Postconditions
980
981 // vsyncs should be off and not available.
982 EXPECT_FALSE(mFlinger.mutablePrimaryHWVsyncEnabled());
983 EXPECT_FALSE(mFlinger.mutableHWVsyncAvailable());
984
985 // The display should have been removed from the display map.
986 EXPECT_FALSE(hasDisplayDevice(existing.token()));
987
988 // The display should still exist in the current state
989 EXPECT_TRUE(hasCurrentDisplayState(existing.token()));
990
991 // The display should have been removed from the drawing state
992 EXPECT_FALSE(hasDrawingDisplayState(existing.token()));
993}
994
995/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800996 * SurfaceFlinger::setupNewDisplayDeviceInternal
997 */
998
999class SetupNewDisplayDeviceInternalTest : public DisplayTransactionTest {
1000public:
1001 template <typename T>
1002 void setupNewDisplayDeviceInternalTest();
1003};
1004
1005template <typename Case>
1006void SetupNewDisplayDeviceInternalTest::setupNewDisplayDeviceInternalTest() {
1007 const sp<BBinder> displayToken = new BBinder();
1008 const sp<mock::DisplaySurface> displaySurface = new mock::DisplaySurface();
1009 const sp<mock::GraphicBufferProducer> producer = new mock::GraphicBufferProducer();
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001010
1011 // --------------------------------------------------------------------
1012 // Preconditions
1013
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001014 // Wide color displays support is configured appropriately
1015 Case::WideColorSupport::injectConfigChange(this);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001016
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001017 // The display is setup with the HWC.
1018 Case::Display::injectHwcDisplay(this);
1019
1020 // SurfaceFlinger will use a test-controlled factory for native window
1021 // surfaces.
1022 injectFakeNativeWindowSurfaceFactory();
1023
1024 // --------------------------------------------------------------------
1025 // Call Expectations
1026
1027 // Various native window calls will be made.
1028 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
Lloyd Pique3c085a02018-05-09 19:38:32 -07001029 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001030 Case::WideColorSupport::setupComposerCallExpectations(this);
1031 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001032 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001033
1034 // --------------------------------------------------------------------
1035 // Invocation
1036
1037 DisplayDeviceState state;
1038 state.type = Case::Display::TYPE;
1039 state.isSecure = static_cast<bool>(Case::Display::SECURE);
1040
1041 auto device = mFlinger.setupNewDisplayDeviceInternal(displayToken, Case::Display::TYPE, state,
1042 displaySurface, producer);
1043
1044 // --------------------------------------------------------------------
1045 // Postconditions
1046
1047 ASSERT_TRUE(device != nullptr);
1048 EXPECT_EQ(Case::Display::TYPE, device->getDisplayType());
1049 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
1050 EXPECT_EQ(Case::Display::WIDTH, device->getWidth());
1051 EXPECT_EQ(Case::Display::HEIGHT, device->getHeight());
1052 EXPECT_EQ(Case::WideColorSupport::WIDE_COLOR_SUPPORTED, device->hasWideColorGamut());
1053 EXPECT_EQ(Case::HdrSupport::HDR10_SUPPORTED, device->hasHDR10Support());
1054 EXPECT_EQ(Case::HdrSupport::HDR_HLG_SUPPORTED, device->hasHLGSupport());
1055 EXPECT_EQ(Case::HdrSupport::HDR_DOLBY_VISION_SUPPORTED, device->hasDolbyVisionSupport());
Lloyd Pique3c085a02018-05-09 19:38:32 -07001056 // Note: This is not Case::Display::HWC_ACTIVE_CONFIG_ID as the ids are
1057 // remapped, and the test only ever sets up one config. If there were an error
1058 // looking up the remapped index, device->getActiveConfig() would be -1 instead.
1059 EXPECT_EQ(0, device->getActiveConfig());
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001060 EXPECT_EQ(Case::PerFrameMetadataSupport::PER_FRAME_METADATA_KEYS,
1061 device->getSupportedPerFrameMetadata());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001062}
1063
1064TEST_F(SetupNewDisplayDeviceInternalTest, createSimplePrimaryDisplay) {
1065 setupNewDisplayDeviceInternalTest<SimplePrimaryDisplayCase>();
1066}
1067
1068TEST_F(SetupNewDisplayDeviceInternalTest, createSimpleExternalDisplay) {
1069 setupNewDisplayDeviceInternalTest<SimpleExternalDisplayCase>();
1070}
1071
1072TEST_F(SetupNewDisplayDeviceInternalTest, createNonHwcVirtualDisplay) {
1073 setupNewDisplayDeviceInternalTest<NonHwcVirtualDisplayCase>();
1074}
1075
1076TEST_F(SetupNewDisplayDeviceInternalTest, createHwcVirtualDisplay) {
1077 // We need to resize this so that the HWC thinks the virtual display
1078 // is something it created.
1079 mFlinger.mutableHwcDisplayData().resize(3);
1080
1081 setupNewDisplayDeviceInternalTest<HwcVirtualDisplayCase>();
1082}
1083
1084TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3Display) {
1085 setupNewDisplayDeviceInternalTest<WideColorP3ColorimetricDisplayCase>();
1086}
1087
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001088TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10Display) {
1089 setupNewDisplayDeviceInternalTest<Hdr10DisplayCase>();
1090}
1091
1092TEST_F(SetupNewDisplayDeviceInternalTest, createHdrHlgDisplay) {
1093 setupNewDisplayDeviceInternalTest<HdrHlgDisplayCase>();
1094}
1095
1096TEST_F(SetupNewDisplayDeviceInternalTest, createHdrDolbyVisionDisplay) {
1097 setupNewDisplayDeviceInternalTest<HdrDolbyVisionDisplayCase>();
1098}
1099
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001100TEST_F(SetupNewDisplayDeviceInternalTest, createHdrSmpte2086DisplayCase) {
1101 setupNewDisplayDeviceInternalTest<HdrSmpte2086DisplayCase>();
1102}
1103
1104TEST_F(SetupNewDisplayDeviceInternalTest, createHdrCta816_3_DisplayCase) {
1105 setupNewDisplayDeviceInternalTest<HdrCta861_3_DisplayCase>();
1106}
1107
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001108/* ------------------------------------------------------------------------
1109 * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded)
1110 */
1111
1112class HandleTransactionLockedTest : public DisplayTransactionTest {
1113public:
1114 template <typename Case>
1115 void setupCommonPreconditions();
1116
1117 template <typename Case>
1118 void setupCommonCallExpectationsForConnectProcessing();
1119
1120 template <typename Case>
1121 void setupCommonCallExpectationsForDisconnectProcessing();
1122
1123 template <typename Case>
1124 void processesHotplugConnectCommon();
1125
1126 template <typename Case>
1127 void ignoresHotplugConnectCommon();
1128
1129 template <typename Case>
1130 void processesHotplugDisconnectCommon();
1131
1132 template <typename Case>
1133 void verifyDisplayIsConnected(const sp<IBinder>& displayToken);
1134
1135 template <typename Case>
1136 void verifyPhysicalDisplayIsConnected();
1137
1138 void verifyDisplayIsNotConnected(const sp<IBinder>& displayToken);
1139};
1140
1141template <typename Case>
1142void HandleTransactionLockedTest::setupCommonPreconditions() {
1143 // Wide color displays support is configured appropriately
1144 Case::WideColorSupport::injectConfigChange(this);
1145
1146 // SurfaceFlinger will use a test-controlled factory for BufferQueues
1147 injectFakeBufferQueueFactory();
1148
1149 // SurfaceFlinger will use a test-controlled factory for native window
1150 // surfaces.
1151 injectFakeNativeWindowSurfaceFactory();
1152}
1153
1154template <typename Case>
1155void HandleTransactionLockedTest::setupCommonCallExpectationsForConnectProcessing() {
1156 Case::Display::setupHwcHotplugCallExpectations(this);
1157
1158 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1159 Case::Display::setupFramebufferProducerBufferQueueCallExpectations(this);
1160 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1161 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
1162
1163 Case::WideColorSupport::setupComposerCallExpectations(this);
1164 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001165 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001166
1167 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001168 EXPECT_CALL(*mEventThread,
1169 onHotplugReceived(Case::Display::TYPE == DisplayDevice::DISPLAY_PRIMARY
1170 ? EventThread::DisplayType::Primary
1171 : EventThread::DisplayType::External,
1172 true))
1173 .Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001174}
1175
1176template <typename Case>
1177void HandleTransactionLockedTest::setupCommonCallExpectationsForDisconnectProcessing() {
1178 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001179 EXPECT_CALL(*mEventThread,
1180 onHotplugReceived(Case::Display::TYPE == DisplayDevice::DISPLAY_PRIMARY
1181 ? EventThread::DisplayType::Primary
1182 : EventThread::DisplayType::External,
1183 false))
1184 .Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001185}
1186
1187template <typename Case>
1188void HandleTransactionLockedTest::verifyDisplayIsConnected(const sp<IBinder>& displayToken) {
1189 // The display device should have been set up in the list of displays.
1190 ASSERT_TRUE(hasDisplayDevice(displayToken));
1191 const auto& device = getDisplayDevice(displayToken);
1192 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
1193 EXPECT_EQ(Case::Display::TYPE == DisplayDevice::DISPLAY_PRIMARY, device->isPrimary());
1194
1195 // The display should have been set up in the current display state
1196 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1197 const auto& current = getCurrentDisplayState(displayToken);
1198 EXPECT_EQ(Case::Display::TYPE, current.type);
1199
1200 // The display should have been set up in the drawing display state
1201 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1202 const auto& draw = getDrawingDisplayState(displayToken);
1203 EXPECT_EQ(Case::Display::TYPE, draw.type);
1204}
1205
1206template <typename Case>
1207void HandleTransactionLockedTest::verifyPhysicalDisplayIsConnected() {
1208 // HWComposer should have an entry for the display
1209 EXPECT_TRUE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1210
1211 // The display should be set up as a built-in display.
1212 static_assert(0 <= Case::Display::TYPE &&
1213 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES,
1214 "Must use a valid physical display type index for the fixed-size array");
Dominik Laskowskieecd6592018-05-29 10:25:41 -07001215 auto& displayToken = mFlinger.mutableDisplayTokens()[Case::Display::TYPE];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001216 ASSERT_TRUE(displayToken != nullptr);
1217
1218 verifyDisplayIsConnected<Case>(displayToken);
1219}
1220
1221void HandleTransactionLockedTest::verifyDisplayIsNotConnected(const sp<IBinder>& displayToken) {
1222 EXPECT_FALSE(hasDisplayDevice(displayToken));
1223 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1224 EXPECT_FALSE(hasDrawingDisplayState(displayToken));
1225}
1226
1227template <typename Case>
1228void HandleTransactionLockedTest::processesHotplugConnectCommon() {
1229 // --------------------------------------------------------------------
1230 // Preconditions
1231
1232 setupCommonPreconditions<Case>();
1233
1234 // A hotplug connect event is enqueued for a display
1235 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001236
1237 // --------------------------------------------------------------------
1238 // Call Expectations
1239
1240 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillOnce(Return(false));
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001241
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001242 setupCommonCallExpectationsForConnectProcessing<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001243
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001244 // --------------------------------------------------------------------
1245 // Invocation
Lloyd Piquee39cad22017-12-20 17:01:29 -08001246
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001247 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
Lloyd Piquee39cad22017-12-20 17:01:29 -08001248
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001249 // --------------------------------------------------------------------
1250 // Postconditions
1251
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001252 verifyPhysicalDisplayIsConnected<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001253
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001254 // --------------------------------------------------------------------
1255 // Cleanup conditions
1256
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001257 EXPECT_CALL(*mComposer,
1258 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001259 .WillOnce(Return(Error::NONE));
1260 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Piquef58625d2017-12-19 13:22:33 -08001261}
1262
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001263template <typename Case>
1264void HandleTransactionLockedTest::ignoresHotplugConnectCommon() {
1265 // --------------------------------------------------------------------
1266 // Preconditions
1267
1268 setupCommonPreconditions<Case>();
1269
1270 // A hotplug connect event is enqueued for a display
1271 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1272
1273 // --------------------------------------------------------------------
1274 // Invocation
1275
1276 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1277
1278 // --------------------------------------------------------------------
1279 // Postconditions
1280
1281 // HWComposer should not have an entry for the display
1282 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1283}
1284
1285template <typename Case>
1286void HandleTransactionLockedTest::processesHotplugDisconnectCommon() {
1287 // --------------------------------------------------------------------
1288 // Preconditions
1289
1290 setupCommonPreconditions<Case>();
1291
1292 // A hotplug disconnect event is enqueued for a display
1293 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1294
1295 // The display is already completely set up.
1296 Case::Display::injectHwcDisplay(this);
1297 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1298 existing.inject();
1299
1300 // --------------------------------------------------------------------
1301 // Call Expectations
1302
1303 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1304
1305 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1306
1307 // --------------------------------------------------------------------
1308 // Invocation
1309
1310 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1311
1312 // --------------------------------------------------------------------
1313 // Postconditions
1314
1315 // HWComposer should not have an entry for the display
1316 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1317
1318 // The display should not be set up as a built-in display.
1319 ASSERT_TRUE(0 <= Case::Display::TYPE &&
1320 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES);
Dominik Laskowskieecd6592018-05-29 10:25:41 -07001321 auto displayToken = mFlinger.mutableDisplayTokens()[Case::Display::TYPE];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001322 EXPECT_TRUE(displayToken == nullptr);
1323
1324 // The existing token should have been removed
1325 verifyDisplayIsNotConnected(existing.token());
1326}
1327
1328TEST_F(HandleTransactionLockedTest, processesHotplugConnectPrimaryDisplay) {
1329 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1330}
1331
1332TEST_F(HandleTransactionLockedTest,
1333 processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected) {
1334 // Inject an external display.
1335 ExternalDisplayVariant::injectHwcDisplay(this);
1336
1337 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1338}
1339
1340TEST_F(HandleTransactionLockedTest, processesHotplugConnectExternalDisplay) {
1341 // Inject a primary display.
1342 PrimaryDisplayVariant::injectHwcDisplay(this);
1343
1344 processesHotplugConnectCommon<SimpleExternalDisplayCase>();
1345}
1346
1347TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected) {
1348 // Inject both a primary and external display.
1349 PrimaryDisplayVariant::injectHwcDisplay(this);
1350 ExternalDisplayVariant::injectHwcDisplay(this);
1351
1352 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1353
1354 ignoresHotplugConnectCommon<SimpleTertiaryDisplayCase>();
1355}
1356
1357TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfExternalForVrComposer) {
1358 // Inject a primary display.
1359 PrimaryDisplayVariant::injectHwcDisplay(this);
1360
1361 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(true));
1362
1363 ignoresHotplugConnectCommon<SimpleExternalDisplayCase>();
1364}
1365
1366TEST_F(HandleTransactionLockedTest, processHotplugDisconnectPrimaryDisplay) {
1367 processesHotplugDisconnectCommon<SimplePrimaryDisplayCase>();
1368}
1369
1370TEST_F(HandleTransactionLockedTest, processHotplugDisconnectExternalDisplay) {
1371 processesHotplugDisconnectCommon<SimpleExternalDisplayCase>();
1372}
1373
1374TEST_F(HandleTransactionLockedTest, processesHotplugConnectThenDisconnectPrimary) {
1375 using Case = SimplePrimaryDisplayCase;
1376
1377 // --------------------------------------------------------------------
1378 // Preconditions
1379
1380 setupCommonPreconditions<Case>();
1381
1382 // A hotplug connect event is enqueued for a display
1383 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1384 // A hotplug disconnect event is also enqueued for the same display
1385 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1386
1387 // --------------------------------------------------------------------
1388 // Call Expectations
1389
1390 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1391
1392 setupCommonCallExpectationsForConnectProcessing<Case>();
1393 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1394
1395 EXPECT_CALL(*mComposer,
1396 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1397 .WillOnce(Return(Error::NONE));
1398 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1399
1400 // --------------------------------------------------------------------
1401 // Invocation
1402
1403 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1404
1405 // --------------------------------------------------------------------
1406 // Postconditions
1407
1408 // HWComposer should not have an entry for the display
1409 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1410
1411 // The display should not be set up as a primary built-in display.
1412 ASSERT_TRUE(0 <= Case::Display::TYPE &&
1413 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES);
Dominik Laskowskieecd6592018-05-29 10:25:41 -07001414 auto displayToken = mFlinger.mutableDisplayTokens()[Case::Display::TYPE];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001415 EXPECT_TRUE(displayToken == nullptr);
1416}
1417
1418TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectThenConnectPrimary) {
1419 using Case = SimplePrimaryDisplayCase;
1420
1421 // --------------------------------------------------------------------
1422 // Preconditions
1423
1424 setupCommonPreconditions<Case>();
1425
1426 // The display is already completely set up.
1427 Case::Display::injectHwcDisplay(this);
1428 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1429 existing.inject();
1430
1431 // A hotplug disconnect event is enqueued for a display
1432 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1433 // A hotplug connect event is also enqueued for the same display
1434 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1435
1436 // --------------------------------------------------------------------
1437 // Call Expectations
1438
1439 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1440
1441 setupCommonCallExpectationsForConnectProcessing<Case>();
1442 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1443
1444 // --------------------------------------------------------------------
1445 // Invocation
1446
1447 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1448
1449 // --------------------------------------------------------------------
1450 // Postconditions
1451
1452 // The existing token should have been removed
1453 verifyDisplayIsNotConnected(existing.token());
1454 static_assert(0 <= Case::Display::TYPE &&
1455 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES,
1456 "Display type must be a built-in display");
Dominik Laskowskieecd6592018-05-29 10:25:41 -07001457 EXPECT_NE(existing.token(), mFlinger.mutableDisplayTokens()[Case::Display::TYPE]);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001458
1459 // A new display should be connected in its place
1460
1461 verifyPhysicalDisplayIsConnected<Case>();
1462
1463 // --------------------------------------------------------------------
1464 // Cleanup conditions
1465
1466 EXPECT_CALL(*mComposer,
1467 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1468 .WillOnce(Return(Error::NONE));
1469 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1470}
1471
1472TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAdded) {
1473 using Case = HwcVirtualDisplayCase;
1474
1475 // --------------------------------------------------------------------
1476 // Preconditions
1477
1478 // The HWC supports at least one virtual display
1479 injectMockComposer(1);
1480
1481 setupCommonPreconditions<Case>();
1482
1483 // A virtual display was added to the current state, and it has a
1484 // surface(producer)
1485 sp<BBinder> displayToken = new BBinder();
Lloyd Pique4c2ac022018-04-27 12:08:03 -07001486
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001487 DisplayDeviceState info;
1488 info.type = Case::Display::TYPE;
1489 info.isSecure = static_cast<bool>(Case::Display::SECURE);
1490
1491 sp<mock::GraphicBufferProducer> surface{new mock::GraphicBufferProducer()};
1492 info.surface = surface;
1493 mFlinger.mutableCurrentState().displays.add(displayToken, info);
1494
1495 // --------------------------------------------------------------------
1496 // Call Expectations
1497
1498 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1499 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1500
1501 EXPECT_CALL(*surface, query(NATIVE_WINDOW_WIDTH, _))
1502 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::WIDTH), Return(NO_ERROR)));
1503 EXPECT_CALL(*surface, query(NATIVE_WINDOW_HEIGHT, _))
1504 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::HEIGHT), Return(NO_ERROR)));
1505 EXPECT_CALL(*surface, query(NATIVE_WINDOW_FORMAT, _))
1506 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT),
1507 Return(NO_ERROR)));
1508 EXPECT_CALL(*surface, query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, _))
1509 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(NO_ERROR)));
1510
1511 EXPECT_CALL(*surface, setAsyncMode(true)).Times(1);
1512
1513 EXPECT_CALL(*mProducer, connect(_, _, _, _)).Times(1);
1514 EXPECT_CALL(*mProducer, disconnect(_, _)).Times(1);
1515
1516 Case::Display::setupHwcVirtualDisplayCreationCallExpectations(this);
1517 Case::WideColorSupport::setupComposerCallExpectations(this);
1518 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001519 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001520
1521 // --------------------------------------------------------------------
1522 // Invocation
1523
1524 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1525
1526 // --------------------------------------------------------------------
1527 // Postconditions
1528
1529 // The display device should have been set up in the list of displays.
1530 verifyDisplayIsConnected<Case>(displayToken);
1531
1532 // --------------------------------------------------------------------
1533 // Cleanup conditions
1534
1535 EXPECT_CALL(*mComposer, destroyVirtualDisplay(Case::Display::HWC_DISPLAY_ID))
1536 .WillOnce(Return(Error::NONE));
1537 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1538}
1539
1540TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAddedWithNoSurface) {
1541 using Case = HwcVirtualDisplayCase;
1542
1543 // --------------------------------------------------------------------
1544 // Preconditions
1545
1546 // The HWC supports at least one virtual display
1547 injectMockComposer(1);
1548
1549 setupCommonPreconditions<Case>();
1550
1551 // A virtual display was added to the current state, but it does not have a
1552 // surface.
1553 sp<BBinder> displayToken = new BBinder();
1554
1555 DisplayDeviceState info;
1556 info.type = Case::Display::TYPE;
1557 info.isSecure = static_cast<bool>(Case::Display::SECURE);
1558
1559 mFlinger.mutableCurrentState().displays.add(displayToken, info);
1560
1561 // --------------------------------------------------------------------
1562 // Call Expectations
1563
1564 // --------------------------------------------------------------------
1565 // Invocation
1566
1567 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1568
1569 // --------------------------------------------------------------------
1570 // Postconditions
1571
1572 // There will not be a display device set up.
1573 EXPECT_FALSE(hasDisplayDevice(displayToken));
1574
1575 // The drawing display state will be set from the current display state.
1576 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1577 const auto& draw = getDrawingDisplayState(displayToken);
1578 EXPECT_EQ(Case::Display::TYPE, draw.type);
1579}
1580
1581TEST_F(HandleTransactionLockedTest, processesVirtualDisplayRemoval) {
1582 using Case = HwcVirtualDisplayCase;
1583
1584 // --------------------------------------------------------------------
1585 // Preconditions
1586
1587 // A virtual display is set up but is removed from the current state.
1588 mFlinger.mutableHwcDisplayData().resize(3);
1589 Case::Display::injectHwcDisplay(this);
1590 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1591 existing.inject();
1592 mFlinger.mutableCurrentState().displays.removeItem(existing.token());
1593
1594 // --------------------------------------------------------------------
1595 // Call Expectations
1596
1597 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1598
1599 // --------------------------------------------------------------------
1600 // Invocation
1601
1602 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1603
1604 // --------------------------------------------------------------------
1605 // Postconditions
1606
1607 // The existing token should have been removed
1608 verifyDisplayIsNotConnected(existing.token());
1609}
1610
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001611TEST_F(HandleTransactionLockedTest, processesDisplayLayerStackChanges) {
1612 using Case = NonHwcVirtualDisplayCase;
1613
1614 constexpr uint32_t oldLayerStack = 0u;
1615 constexpr uint32_t newLayerStack = 123u;
1616
1617 // --------------------------------------------------------------------
1618 // Preconditions
1619
1620 // A display is set up
1621 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1622 display.inject();
1623
1624 // There is a change to the layerStack state
1625 display.mutableDrawingDisplayState().layerStack = oldLayerStack;
1626 display.mutableCurrentDisplayState().layerStack = newLayerStack;
1627
1628 // --------------------------------------------------------------------
1629 // Invocation
1630
1631 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1632
1633 // --------------------------------------------------------------------
1634 // Postconditions
1635
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001636 EXPECT_EQ(newLayerStack, display.mutableDisplayDevice()->getLayerStack());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001637}
1638
1639TEST_F(HandleTransactionLockedTest, processesDisplayTransformChanges) {
1640 using Case = NonHwcVirtualDisplayCase;
1641
1642 constexpr int oldTransform = 0;
1643 constexpr int newTransform = 2;
1644
1645 // --------------------------------------------------------------------
1646 // Preconditions
1647
1648 // A display is set up
1649 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1650 display.inject();
1651
1652 // There is a change to the orientation state
1653 display.mutableDrawingDisplayState().orientation = oldTransform;
1654 display.mutableCurrentDisplayState().orientation = newTransform;
1655
1656 // --------------------------------------------------------------------
1657 // Invocation
1658
1659 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1660
1661 // --------------------------------------------------------------------
1662 // Postconditions
1663
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001664 EXPECT_EQ(newTransform, display.mutableDisplayDevice()->getOrientation());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001665}
1666
1667TEST_F(HandleTransactionLockedTest, processesDisplayViewportChanges) {
1668 using Case = NonHwcVirtualDisplayCase;
1669
1670 const Rect oldViewport(0, 0, 0, 0);
1671 const Rect newViewport(0, 0, 123, 456);
1672
1673 // --------------------------------------------------------------------
1674 // Preconditions
1675
1676 // A display is set up
1677 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1678 display.inject();
1679
1680 // There is a change to the viewport state
1681 display.mutableDrawingDisplayState().viewport = oldViewport;
1682 display.mutableCurrentDisplayState().viewport = newViewport;
1683
1684 // --------------------------------------------------------------------
1685 // Invocation
1686
1687 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1688
1689 // --------------------------------------------------------------------
1690 // Postconditions
1691
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001692 EXPECT_EQ(newViewport, display.mutableDisplayDevice()->getViewport());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001693}
1694
1695TEST_F(HandleTransactionLockedTest, processesDisplayFrameChanges) {
1696 using Case = NonHwcVirtualDisplayCase;
1697
1698 const Rect oldFrame(0, 0, 0, 0);
1699 const Rect newFrame(0, 0, 123, 456);
1700
1701 // --------------------------------------------------------------------
1702 // Preconditions
1703
1704 // A display is set up
1705 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1706 display.inject();
1707
1708 // There is a change to the viewport state
1709 display.mutableDrawingDisplayState().frame = oldFrame;
1710 display.mutableCurrentDisplayState().frame = newFrame;
1711
1712 // --------------------------------------------------------------------
1713 // Invocation
1714
1715 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1716
1717 // --------------------------------------------------------------------
1718 // Postconditions
1719
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001720 EXPECT_EQ(newFrame, display.mutableDisplayDevice()->getFrame());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001721}
1722
1723TEST_F(HandleTransactionLockedTest, processesDisplayWidthChanges) {
1724 using Case = NonHwcVirtualDisplayCase;
1725
1726 constexpr int oldWidth = 0;
1727 constexpr int oldHeight = 10;
1728 constexpr int newWidth = 123;
1729
1730 // --------------------------------------------------------------------
1731 // Preconditions
1732
1733 // A display is set up
1734 auto nativeWindow = new mock::NativeWindow();
1735 auto displaySurface = new mock::DisplaySurface();
1736 auto renderSurface = new RE::mock::Surface();
1737 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1738 display.setNativeWindow(nativeWindow);
1739 display.setDisplaySurface(displaySurface);
1740 display.setRenderSurface(std::unique_ptr<RE::Surface>(renderSurface));
1741 display.inject();
1742
1743 // There is a change to the viewport state
1744 display.mutableDrawingDisplayState().width = oldWidth;
1745 display.mutableDrawingDisplayState().height = oldHeight;
1746 display.mutableCurrentDisplayState().width = newWidth;
1747 display.mutableCurrentDisplayState().height = oldHeight;
1748
1749 // --------------------------------------------------------------------
1750 // Call Expectations
1751
1752 EXPECT_CALL(*renderSurface, setNativeWindow(nullptr)).Times(1);
1753 EXPECT_CALL(*displaySurface, resizeBuffers(newWidth, oldHeight)).Times(1);
1754 EXPECT_CALL(*renderSurface, setNativeWindow(nativeWindow)).Times(1);
1755 EXPECT_CALL(*renderSurface, queryWidth()).WillOnce(Return(newWidth));
1756 EXPECT_CALL(*renderSurface, queryHeight()).WillOnce(Return(oldHeight));
1757
1758 // --------------------------------------------------------------------
1759 // Invocation
1760
1761 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1762}
1763
1764TEST_F(HandleTransactionLockedTest, processesDisplayHeightChanges) {
1765 using Case = NonHwcVirtualDisplayCase;
1766
1767 constexpr int oldWidth = 0;
1768 constexpr int oldHeight = 10;
1769 constexpr int newHeight = 123;
1770
1771 // --------------------------------------------------------------------
1772 // Preconditions
1773
1774 // A display is set up
1775 auto nativeWindow = new mock::NativeWindow();
1776 auto displaySurface = new mock::DisplaySurface();
1777 auto renderSurface = new RE::mock::Surface();
1778 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1779 display.setNativeWindow(nativeWindow);
1780 display.setDisplaySurface(displaySurface);
1781 display.setRenderSurface(std::unique_ptr<RE::Surface>(renderSurface));
1782 display.inject();
1783
1784 // There is a change to the viewport state
1785 display.mutableDrawingDisplayState().width = oldWidth;
1786 display.mutableDrawingDisplayState().height = oldHeight;
1787 display.mutableCurrentDisplayState().width = oldWidth;
1788 display.mutableCurrentDisplayState().height = newHeight;
1789
1790 // --------------------------------------------------------------------
1791 // Call Expectations
1792
1793 EXPECT_CALL(*renderSurface, setNativeWindow(nullptr)).Times(1);
1794 EXPECT_CALL(*displaySurface, resizeBuffers(oldWidth, newHeight)).Times(1);
1795 EXPECT_CALL(*renderSurface, setNativeWindow(nativeWindow)).Times(1);
1796 EXPECT_CALL(*renderSurface, queryWidth()).WillOnce(Return(oldWidth));
1797 EXPECT_CALL(*renderSurface, queryHeight()).WillOnce(Return(newHeight));
1798
1799 // --------------------------------------------------------------------
1800 // Invocation
1801
1802 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1803}
1804
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001805/* ------------------------------------------------------------------------
1806 * SurfaceFlinger::setDisplayStateLocked
1807 */
1808
1809TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithUnknownDisplay) {
1810 // --------------------------------------------------------------------
1811 // Preconditions
1812
1813 // We have an unknown display token not associated with a known display
1814 sp<BBinder> displayToken = new BBinder();
1815
1816 // The requested display state references the unknown display.
1817 DisplayState state;
1818 state.what = DisplayState::eLayerStackChanged;
1819 state.token = displayToken;
1820 state.layerStack = 456;
1821
1822 // --------------------------------------------------------------------
1823 // Invocation
1824
1825 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1826
1827 // --------------------------------------------------------------------
1828 // Postconditions
1829
1830 // The returned flags are empty
1831 EXPECT_EQ(0u, flags);
1832
1833 // The display token still doesn't match anything known.
1834 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1835}
1836
1837TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithInvalidDisplay) {
1838 using Case = InvalidDisplayCase;
1839
1840 // --------------------------------------------------------------------
1841 // Preconditions
1842
1843 // An invalid display is set up
1844 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1845 display.inject();
1846
1847 // The invalid display has some state
1848 display.mutableCurrentDisplayState().layerStack = 654u;
1849
1850 // The requested display state tries to change the display state.
1851 DisplayState state;
1852 state.what = DisplayState::eLayerStackChanged;
1853 state.token = display.token();
1854 state.layerStack = 456;
1855
1856 // --------------------------------------------------------------------
1857 // Invocation
1858
1859 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1860
1861 // --------------------------------------------------------------------
1862 // Postconditions
1863
1864 // The returned flags are empty
1865 EXPECT_EQ(0u, flags);
1866
1867 // The current display layer stack value is unchanged.
1868 EXPECT_EQ(654u, getCurrentDisplayState(display.token()).layerStack);
1869}
1870
1871TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWhenNoChanges) {
1872 using Case = SimplePrimaryDisplayCase;
1873
1874 // --------------------------------------------------------------------
1875 // Preconditions
1876
1877 // A display is already set up
1878 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1879 display.inject();
1880
1881 // No changes are made to the display
1882 DisplayState state;
1883 state.what = 0;
1884 state.token = display.token();
1885
1886 // --------------------------------------------------------------------
1887 // Invocation
1888
1889 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1890
1891 // --------------------------------------------------------------------
1892 // Postconditions
1893
1894 // The returned flags are empty
1895 EXPECT_EQ(0u, flags);
1896}
1897
1898TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSurfaceDidNotChange) {
1899 using Case = SimplePrimaryDisplayCase;
1900
1901 // --------------------------------------------------------------------
1902 // Preconditions
1903
1904 // A display is already set up
1905 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1906 display.inject();
1907
1908 // There is a surface that can be set.
1909 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
1910
1911 // The current display state has the surface set
1912 display.mutableCurrentDisplayState().surface = surface;
1913
1914 // The incoming request sets the same surface
1915 DisplayState state;
1916 state.what = DisplayState::eSurfaceChanged;
1917 state.token = display.token();
1918 state.surface = surface;
1919
1920 // --------------------------------------------------------------------
1921 // Invocation
1922
1923 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1924
1925 // --------------------------------------------------------------------
1926 // Postconditions
1927
1928 // The returned flags are empty
1929 EXPECT_EQ(0u, flags);
1930
1931 // The current display state is unchanged.
1932 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
1933}
1934
1935TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfSurfaceChanged) {
1936 using Case = SimplePrimaryDisplayCase;
1937
1938 // --------------------------------------------------------------------
1939 // Preconditions
1940
1941 // A display is already set up
1942 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1943 display.inject();
1944
1945 // There is a surface that can be set.
1946 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
1947
1948 // The current display state does not have a surface
1949 display.mutableCurrentDisplayState().surface = nullptr;
1950
1951 // The incoming request sets a surface
1952 DisplayState state;
1953 state.what = DisplayState::eSurfaceChanged;
1954 state.token = display.token();
1955 state.surface = surface;
1956
1957 // --------------------------------------------------------------------
1958 // Invocation
1959
1960 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1961
1962 // --------------------------------------------------------------------
1963 // Postconditions
1964
1965 // The returned flags indicate a transaction is needed
1966 EXPECT_EQ(eDisplayTransactionNeeded, flags);
1967
1968 // The current display layer stack state is set to the new value
1969 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
1970}
1971
1972TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfLayerStackDidNotChange) {
1973 using Case = SimplePrimaryDisplayCase;
1974
1975 // --------------------------------------------------------------------
1976 // Preconditions
1977
1978 // A display is already set up
1979 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1980 display.inject();
1981
1982 // The display has a layer stack set
1983 display.mutableCurrentDisplayState().layerStack = 456u;
1984
1985 // The incoming request sets the same layer stack
1986 DisplayState state;
1987 state.what = DisplayState::eLayerStackChanged;
1988 state.token = display.token();
1989 state.layerStack = 456u;
1990
1991 // --------------------------------------------------------------------
1992 // Invocation
1993
1994 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1995
1996 // --------------------------------------------------------------------
1997 // Postconditions
1998
1999 // The returned flags are empty
2000 EXPECT_EQ(0u, flags);
2001
2002 // The current display state is unchanged
2003 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2004}
2005
2006TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfLayerStackChanged) {
2007 using Case = SimplePrimaryDisplayCase;
2008
2009 // --------------------------------------------------------------------
2010 // Preconditions
2011
2012 // A display is set up
2013 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2014 display.inject();
2015
2016 // The display has a layer stack set
2017 display.mutableCurrentDisplayState().layerStack = 654u;
2018
2019 // The incoming request sets a different layer stack
2020 DisplayState state;
2021 state.what = DisplayState::eLayerStackChanged;
2022 state.token = display.token();
2023 state.layerStack = 456u;
2024
2025 // --------------------------------------------------------------------
2026 // Invocation
2027
2028 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2029
2030 // --------------------------------------------------------------------
2031 // Postconditions
2032
2033 // The returned flags indicate a transaction is needed
2034 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2035
2036 // The desired display state has been set to the new value.
2037 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2038}
2039
2040TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfProjectionDidNotChange) {
2041 using Case = SimplePrimaryDisplayCase;
2042 constexpr int initialOrientation = 180;
2043 const Rect initialFrame = {1, 2, 3, 4};
2044 const Rect initialViewport = {5, 6, 7, 8};
2045
2046 // --------------------------------------------------------------------
2047 // Preconditions
2048
2049 // A display is set up
2050 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2051 display.inject();
2052
2053 // The current display state projection state is all set
2054 display.mutableCurrentDisplayState().orientation = initialOrientation;
2055 display.mutableCurrentDisplayState().frame = initialFrame;
2056 display.mutableCurrentDisplayState().viewport = initialViewport;
2057
2058 // The incoming request sets the same projection state
2059 DisplayState state;
2060 state.what = DisplayState::eDisplayProjectionChanged;
2061 state.token = display.token();
2062 state.orientation = initialOrientation;
2063 state.frame = initialFrame;
2064 state.viewport = initialViewport;
2065
2066 // --------------------------------------------------------------------
2067 // Invocation
2068
2069 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2070
2071 // --------------------------------------------------------------------
2072 // Postconditions
2073
2074 // The returned flags are empty
2075 EXPECT_EQ(0u, flags);
2076
2077 // The current display state is unchanged
2078 EXPECT_EQ(initialOrientation, display.getCurrentDisplayState().orientation);
2079
2080 EXPECT_EQ(initialFrame, display.getCurrentDisplayState().frame);
2081 EXPECT_EQ(initialViewport, display.getCurrentDisplayState().viewport);
2082}
2083
2084TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfOrientationChanged) {
2085 using Case = SimplePrimaryDisplayCase;
2086 constexpr int initialOrientation = 90;
2087 constexpr int desiredOrientation = 180;
2088
2089 // --------------------------------------------------------------------
2090 // Preconditions
2091
2092 // A display is set up
2093 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2094 display.inject();
2095
2096 // The current display state has an orientation set
2097 display.mutableCurrentDisplayState().orientation = initialOrientation;
2098
2099 // The incoming request sets a different orientation
2100 DisplayState state;
2101 state.what = DisplayState::eDisplayProjectionChanged;
2102 state.token = display.token();
2103 state.orientation = desiredOrientation;
2104
2105 // --------------------------------------------------------------------
2106 // Invocation
2107
2108 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2109
2110 // --------------------------------------------------------------------
2111 // Postconditions
2112
2113 // The returned flags indicate a transaction is needed
2114 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2115
2116 // The current display state has the new value.
2117 EXPECT_EQ(desiredOrientation, display.getCurrentDisplayState().orientation);
2118}
2119
2120TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfFrameChanged) {
2121 using Case = SimplePrimaryDisplayCase;
2122 const Rect initialFrame = {0, 0, 0, 0};
2123 const Rect desiredFrame = {5, 6, 7, 8};
2124
2125 // --------------------------------------------------------------------
2126 // Preconditions
2127
2128 // A display is set up
2129 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2130 display.inject();
2131
2132 // The current display state does not have a frame
2133 display.mutableCurrentDisplayState().frame = initialFrame;
2134
2135 // The incoming request sets a frame
2136 DisplayState state;
2137 state.what = DisplayState::eDisplayProjectionChanged;
2138 state.token = display.token();
2139 state.frame = desiredFrame;
2140
2141 // --------------------------------------------------------------------
2142 // Invocation
2143
2144 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2145
2146 // --------------------------------------------------------------------
2147 // Postconditions
2148
2149 // The returned flags indicate a transaction is needed
2150 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2151
2152 // The current display state has the new value.
2153 EXPECT_EQ(desiredFrame, display.getCurrentDisplayState().frame);
2154}
2155
2156TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfViewportChanged) {
2157 using Case = SimplePrimaryDisplayCase;
2158 const Rect initialViewport = {0, 0, 0, 0};
2159 const Rect desiredViewport = {5, 6, 7, 8};
2160
2161 // --------------------------------------------------------------------
2162 // Preconditions
2163
2164 // A display is set up
2165 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2166 display.inject();
2167
2168 // The current display state does not have a viewport
2169 display.mutableCurrentDisplayState().viewport = initialViewport;
2170
2171 // The incoming request sets a viewport
2172 DisplayState state;
2173 state.what = DisplayState::eDisplayProjectionChanged;
2174 state.token = display.token();
2175 state.viewport = desiredViewport;
2176
2177 // --------------------------------------------------------------------
2178 // Invocation
2179
2180 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2181
2182 // --------------------------------------------------------------------
2183 // Postconditions
2184
2185 // The returned flags indicate a transaction is needed
2186 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2187
2188 // The current display state has the new value.
2189 EXPECT_EQ(desiredViewport, display.getCurrentDisplayState().viewport);
2190}
2191
2192TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSizeDidNotChange) {
2193 using Case = SimplePrimaryDisplayCase;
2194 constexpr uint32_t initialWidth = 1024;
2195 constexpr uint32_t initialHeight = 768;
2196
2197 // --------------------------------------------------------------------
2198 // Preconditions
2199
2200 // A display is set up
2201 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2202 display.inject();
2203
2204 // The current display state has a size set
2205 display.mutableCurrentDisplayState().width = initialWidth;
2206 display.mutableCurrentDisplayState().height = initialHeight;
2207
2208 // The incoming request sets the same display size
2209 DisplayState state;
2210 state.what = DisplayState::eDisplaySizeChanged;
2211 state.token = display.token();
2212 state.width = initialWidth;
2213 state.height = initialHeight;
2214
2215 // --------------------------------------------------------------------
2216 // Invocation
2217
2218 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2219
2220 // --------------------------------------------------------------------
2221 // Postconditions
2222
2223 // The returned flags are empty
2224 EXPECT_EQ(0u, flags);
2225
2226 // The current display state is unchanged
2227 EXPECT_EQ(initialWidth, display.getCurrentDisplayState().width);
2228 EXPECT_EQ(initialHeight, display.getCurrentDisplayState().height);
2229}
2230
2231TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfWidthChanged) {
2232 using Case = SimplePrimaryDisplayCase;
2233 constexpr uint32_t initialWidth = 0;
2234 constexpr uint32_t desiredWidth = 1024;
2235
2236 // --------------------------------------------------------------------
2237 // Preconditions
2238
2239 // A display is set up
2240 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2241 display.inject();
2242
2243 // The display does not yet have a width
2244 display.mutableCurrentDisplayState().width = initialWidth;
2245
2246 // The incoming request sets a display width
2247 DisplayState state;
2248 state.what = DisplayState::eDisplaySizeChanged;
2249 state.token = display.token();
2250 state.width = desiredWidth;
2251
2252 // --------------------------------------------------------------------
2253 // Invocation
2254
2255 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2256
2257 // --------------------------------------------------------------------
2258 // Postconditions
2259
2260 // The returned flags indicate a transaction is needed
2261 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2262
2263 // The current display state has the new value.
2264 EXPECT_EQ(desiredWidth, display.getCurrentDisplayState().width);
2265}
2266
2267TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfHeightChanged) {
2268 using Case = SimplePrimaryDisplayCase;
2269 constexpr uint32_t initialHeight = 0;
2270 constexpr uint32_t desiredHeight = 768;
2271
2272 // --------------------------------------------------------------------
2273 // Preconditions
2274
2275 // A display is set up
2276 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2277 display.inject();
2278
2279 // The display does not yet have a height
2280 display.mutableCurrentDisplayState().height = initialHeight;
2281
2282 // The incoming request sets a display height
2283 DisplayState state;
2284 state.what = DisplayState::eDisplaySizeChanged;
2285 state.token = display.token();
2286 state.height = desiredHeight;
2287
2288 // --------------------------------------------------------------------
2289 // Invocation
2290
2291 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2292
2293 // --------------------------------------------------------------------
2294 // Postconditions
2295
2296 // The returned flags indicate a transaction is needed
2297 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2298
2299 // The current display state has the new value.
2300 EXPECT_EQ(desiredHeight, display.getCurrentDisplayState().height);
2301}
2302
Lloyd Pique86016da2018-03-01 16:09:38 -08002303/* ------------------------------------------------------------------------
2304 * SurfaceFlinger::onInitializeDisplays
2305 */
2306
2307TEST_F(DisplayTransactionTest, onInitializeDisplaysSetsUpPrimaryDisplay) {
2308 using Case = SimplePrimaryDisplayCase;
2309
2310 // --------------------------------------------------------------------
2311 // Preconditions
2312
2313 // A primary display is set up
2314 Case::Display::injectHwcDisplay(this);
2315 auto primaryDisplay = Case::Display::makeFakeExistingDisplayInjector(this);
2316 primaryDisplay.inject();
2317
2318 // --------------------------------------------------------------------
2319 // Call Expectations
2320
2321 // We expect the surface interceptor to possibly be used, but we treat it as
2322 // disabled since it is called as a side effect rather than directly by this
2323 // function.
2324 EXPECT_CALL(*mSurfaceInterceptor, isEnabled()).WillOnce(Return(false));
2325
2326 // We expect a call to get the active display config.
2327 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
2328
2329 // We expect invalidate() to be invoked once to trigger display transaction
2330 // processing.
2331 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
2332
2333 // --------------------------------------------------------------------
2334 // Invocation
2335
2336 mFlinger.onInitializeDisplays();
2337
2338 // --------------------------------------------------------------------
2339 // Postconditions
2340
2341 // The primary display should have a current state
2342 ASSERT_TRUE(hasCurrentDisplayState(primaryDisplay.token()));
2343 const auto& primaryDisplayState = getCurrentDisplayState(primaryDisplay.token());
2344 // The layer stack state should be set to zero
2345 EXPECT_EQ(0u, primaryDisplayState.layerStack);
2346 // The orientation state should be set to zero
2347 EXPECT_EQ(0, primaryDisplayState.orientation);
2348
2349 // The frame state should be set to INVALID
2350 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.frame);
2351
2352 // The viewport state should be set to INVALID
2353 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.viewport);
2354
2355 // The width and height should both be zero
2356 EXPECT_EQ(0u, primaryDisplayState.width);
2357 EXPECT_EQ(0u, primaryDisplayState.height);
2358
2359 // The display should be set to HWC_POWER_MODE_NORMAL
2360 ASSERT_TRUE(hasDisplayDevice(primaryDisplay.token()));
2361 auto displayDevice = primaryDisplay.mutableDisplayDevice();
2362 EXPECT_EQ(HWC_POWER_MODE_NORMAL, displayDevice->getPowerMode());
2363
2364 // The display refresh period should be set in the frame tracker.
2365 FrameStats stats;
2366 mFlinger.getAnimFrameTracker().getStats(&stats);
2367 EXPECT_EQ(DEFAULT_REFRESH_RATE, stats.refreshPeriodNano);
2368
2369 // The display transaction needed flag should be set.
2370 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
2371
2372 // The compositor timing should be set to default values
2373 const auto& compositorTiming = mFlinger.getCompositorTiming();
2374 EXPECT_EQ(-DEFAULT_REFRESH_RATE, compositorTiming.deadline);
2375 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.interval);
2376 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.presentLatency);
2377}
2378
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002379/* ------------------------------------------------------------------------
2380 * SurfaceFlinger::setPowerModeInternal
2381 */
2382
2383// Used when we simulate a display that supports doze.
2384struct DozeIsSupportedVariant {
2385 static constexpr bool DOZE_SUPPORTED = true;
2386 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2387 IComposerClient::PowerMode::DOZE;
2388 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2389 IComposerClient::PowerMode::DOZE_SUSPEND;
2390};
2391
2392// Used when we simulate a display that does not support doze.
2393struct DozeNotSupportedVariant {
2394 static constexpr bool DOZE_SUPPORTED = false;
2395 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2396 IComposerClient::PowerMode::ON;
2397 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2398 IComposerClient::PowerMode::ON;
2399};
2400
2401struct EventThreadBaseSupportedVariant {
2402 static void setupEventAndEventControlThreadNoCallExpectations(DisplayTransactionTest* test) {
2403 // The event control thread should not be notified.
2404 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(_)).Times(0);
2405
2406 // The event thread should not be notified.
2407 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(0);
2408 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(0);
2409 }
2410};
2411
2412struct EventThreadNotSupportedVariant : public EventThreadBaseSupportedVariant {
2413 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2414 // These calls are only expected for the primary display.
2415
2416 // Instead expect no calls.
2417 setupEventAndEventControlThreadNoCallExpectations(test);
2418 }
2419
2420 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2421 // These calls are only expected for the primary display.
2422
2423 // Instead expect no calls.
2424 setupEventAndEventControlThreadNoCallExpectations(test);
2425 }
2426};
2427
2428struct EventThreadIsSupportedVariant : public EventThreadBaseSupportedVariant {
2429 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2430 // The event control thread should be notified to enable vsyncs
2431 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(true)).Times(1);
2432
2433 // The event thread should be notified that the screen was acquired.
2434 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(1);
2435 }
2436
2437 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2438 // There should be a call to setVsyncEnabled(false)
2439 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(false)).Times(1);
2440
2441 // The event thread should not be notified that the screen was released.
2442 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(1);
2443 }
2444};
2445
2446// --------------------------------------------------------------------
2447// Note:
2448//
2449// There are a large number of transitions we could test, however we only test a
2450// selected subset which provides complete test coverage of the implementation.
2451// --------------------------------------------------------------------
2452
2453template <int initialPowerMode, int targetPowerMode>
2454struct TransitionVariantCommon {
2455 static constexpr auto INITIAL_POWER_MODE = initialPowerMode;
2456 static constexpr auto TARGET_POWER_MODE = targetPowerMode;
2457
2458 static void verifyPostconditions(DisplayTransactionTest*) {}
2459};
2460
2461struct TransitionOffToOnVariant
2462 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_NORMAL> {
2463 template <typename Case>
2464 static void setupCallExpectations(DisplayTransactionTest* test) {
2465 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2466 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
2467 Case::setupRepaintEverythingCallExpectations(test);
2468 }
2469
2470 static void verifyPostconditions(DisplayTransactionTest* test) {
2471 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2472 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2473 }
2474};
2475
2476struct TransitionOffToDozeSuspendVariant
2477 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_DOZE_SUSPEND> {
2478 template <typename Case>
2479 static void setupCallExpectations(DisplayTransactionTest* test) {
2480 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2481 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2482 Case::setupRepaintEverythingCallExpectations(test);
2483 }
2484
2485 static void verifyPostconditions(DisplayTransactionTest* test) {
2486 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2487 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2488 }
2489};
2490
2491struct TransitionOnToOffVariant
2492 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_OFF> {
2493 template <typename Case>
2494 static void setupCallExpectations(DisplayTransactionTest* test) {
2495 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
2496 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2497 }
2498
2499 static void verifyPostconditions(DisplayTransactionTest* test) {
2500 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2501 }
2502};
2503
2504struct TransitionDozeSuspendToOffVariant
2505 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_OFF> {
2506 template <typename Case>
2507 static void setupCallExpectations(DisplayTransactionTest* test) {
2508 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2509 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2510 }
2511
2512 static void verifyPostconditions(DisplayTransactionTest* test) {
2513 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2514 }
2515};
2516
2517struct TransitionOnToDozeVariant
2518 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE> {
2519 template <typename Case>
2520 static void setupCallExpectations(DisplayTransactionTest* test) {
2521 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2522 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2523 }
2524};
2525
2526struct TransitionDozeSuspendToDozeVariant
2527 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_DOZE> {
2528 template <typename Case>
2529 static void setupCallExpectations(DisplayTransactionTest* test) {
2530 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
2531 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2532 }
2533};
2534
2535struct TransitionDozeToOnVariant
2536 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE, HWC_POWER_MODE_NORMAL> {
2537 template <typename Case>
2538 static void setupCallExpectations(DisplayTransactionTest* test) {
2539 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2540 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2541 }
2542};
2543
2544struct TransitionDozeSuspendToOnVariant
2545 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_NORMAL> {
2546 template <typename Case>
2547 static void setupCallExpectations(DisplayTransactionTest* test) {
2548 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
2549 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2550 }
2551};
2552
2553struct TransitionOnToDozeSuspendVariant
2554 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE_SUSPEND> {
2555 template <typename Case>
2556 static void setupCallExpectations(DisplayTransactionTest* test) {
2557 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
2558 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2559 }
2560};
2561
2562struct TransitionOnToUnknownVariant
2563 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_LEET> {
2564 template <typename Case>
2565 static void setupCallExpectations(DisplayTransactionTest* test) {
2566 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2567 Case::setupNoComposerPowerModeCallExpectations(test);
2568 }
2569};
2570
2571// --------------------------------------------------------------------
2572// Note:
2573//
2574// Rather than testing the cartesian product of of
2575// DozeIsSupported/DozeNotSupported with all other options, we use one for one
2576// display type, and the other for another display type.
2577// --------------------------------------------------------------------
2578
2579template <typename DisplayVariant, typename DozeVariant, typename EventThreadVariant,
2580 typename TransitionVariant>
2581struct DisplayPowerCase {
2582 using Display = DisplayVariant;
2583 using Doze = DozeVariant;
2584 using EventThread = EventThreadVariant;
2585 using Transition = TransitionVariant;
2586
2587 static auto injectDisplayWithInitialPowerMode(DisplayTransactionTest* test, int mode) {
2588 Display::injectHwcDisplay(test);
2589 auto display = Display::makeFakeExistingDisplayInjector(test);
2590 display.inject();
2591 display.mutableDisplayDevice()->setPowerMode(mode);
2592 return display;
2593 }
2594
2595 static void setInitialPrimaryHWVsyncEnabled(DisplayTransactionTest* test, bool enabled) {
2596 test->mFlinger.mutablePrimaryHWVsyncEnabled() = enabled;
2597 }
2598
2599 static void setupRepaintEverythingCallExpectations(DisplayTransactionTest* test) {
2600 EXPECT_CALL(*test->mMessageQueue, invalidate()).Times(1);
2601 }
2602
2603 static void setupSurfaceInterceptorCallExpectations(DisplayTransactionTest* test, int mode) {
2604 EXPECT_CALL(*test->mSurfaceInterceptor, isEnabled()).WillOnce(Return(true));
2605 EXPECT_CALL(*test->mSurfaceInterceptor, savePowerModeUpdate(_, mode)).Times(1);
2606 }
2607
2608 static void setupComposerCallExpectations(DisplayTransactionTest* test,
2609 IComposerClient::PowerMode mode) {
2610 // Any calls to get the active config will return a default value.
2611 EXPECT_CALL(*test->mComposer, getActiveConfig(Display::HWC_DISPLAY_ID, _))
2612 .WillRepeatedly(DoAll(SetArgPointee<1>(Display::HWC_ACTIVE_CONFIG_ID),
2613 Return(Error::NONE)));
2614
2615 // Any calls to get whether the display supports dozing will return the value set by the
2616 // policy variant.
2617 EXPECT_CALL(*test->mComposer, getDozeSupport(Display::HWC_DISPLAY_ID, _))
2618 .WillRepeatedly(DoAll(SetArgPointee<1>(Doze::DOZE_SUPPORTED), Return(Error::NONE)));
2619
2620 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, mode)).Times(1);
2621 }
2622
2623 static void setupNoComposerPowerModeCallExpectations(DisplayTransactionTest* test) {
2624 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, _)).Times(0);
2625 }
2626};
2627
2628// A sample configuration for the primary display.
2629// In addition to having event thread support, we emulate doze support.
2630template <typename TransitionVariant>
2631using PrimaryDisplayPowerCase = DisplayPowerCase<PrimaryDisplayVariant, DozeIsSupportedVariant,
2632 EventThreadIsSupportedVariant, TransitionVariant>;
2633
2634// A sample configuration for the external display.
2635// In addition to not having event thread support, we emulate not having doze
2636// support.
2637template <typename TransitionVariant>
2638using ExternalDisplayPowerCase =
2639 DisplayPowerCase<ExternalDisplayVariant, DozeNotSupportedVariant,
2640 EventThreadNotSupportedVariant, TransitionVariant>;
2641
2642class SetPowerModeInternalTest : public DisplayTransactionTest {
2643public:
2644 template <typename Case>
2645 void transitionDisplayCommon();
2646};
2647
2648template <int PowerMode>
2649struct PowerModeInitialVSyncEnabled : public std::false_type {};
2650
2651template <>
2652struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_NORMAL> : public std::true_type {};
2653
2654template <>
2655struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_DOZE> : public std::true_type {};
2656
2657template <typename Case>
2658void SetPowerModeInternalTest::transitionDisplayCommon() {
2659 // --------------------------------------------------------------------
2660 // Preconditions
2661
2662 auto display =
2663 Case::injectDisplayWithInitialPowerMode(this, Case::Transition::INITIAL_POWER_MODE);
2664 Case::setInitialPrimaryHWVsyncEnabled(this,
2665 PowerModeInitialVSyncEnabled<
2666 Case::Transition::INITIAL_POWER_MODE>::value);
2667
2668 // --------------------------------------------------------------------
2669 // Call Expectations
2670
2671 Case::setupSurfaceInterceptorCallExpectations(this, Case::Transition::TARGET_POWER_MODE);
2672 Case::Transition::template setupCallExpectations<Case>(this);
2673
2674 // --------------------------------------------------------------------
2675 // Invocation
2676
2677 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(),
2678 Case::Transition::TARGET_POWER_MODE);
2679
2680 // --------------------------------------------------------------------
2681 // Postconditions
2682
2683 Case::Transition::verifyPostconditions(this);
2684}
2685
2686TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfNoChange) {
2687 using Case = SimplePrimaryDisplayCase;
2688
2689 // --------------------------------------------------------------------
2690 // Preconditions
2691
2692 // A primary display device is set up
2693 Case::Display::injectHwcDisplay(this);
2694 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2695 display.inject();
2696
Dominik Laskowskia2edf612018-06-01 13:15:16 -07002697 // The display is already set to HWC_POWER_MODE_NORMAL
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002698 display.mutableDisplayDevice()->setPowerMode(HWC_POWER_MODE_NORMAL);
2699
2700 // --------------------------------------------------------------------
2701 // Invocation
2702
2703 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_NORMAL);
2704
2705 // --------------------------------------------------------------------
2706 // Postconditions
2707
2708 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2709}
2710
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002711TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfVirtualDisplay) {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002712 using Case = HwcVirtualDisplayCase;
2713
2714 // --------------------------------------------------------------------
2715 // Preconditions
2716
2717 // We need to resize this so that the HWC thinks the virtual display
2718 // is something it created.
2719 mFlinger.mutableHwcDisplayData().resize(3);
2720
2721 // A virtual display device is set up
2722 Case::Display::injectHwcDisplay(this);
2723 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2724 display.inject();
2725
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002726 // The display is set to HWC_POWER_MODE_NORMAL
2727 getDisplayDevice(display.token())->setPowerMode(HWC_POWER_MODE_NORMAL);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002728
2729 // --------------------------------------------------------------------
2730 // Invocation
2731
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002732 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_OFF);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002733
2734 // --------------------------------------------------------------------
2735 // Postconditions
2736
2737 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2738}
2739
2740TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnPrimaryDisplay) {
2741 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToOnVariant>>();
2742}
2743
2744TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendPrimaryDisplay) {
2745 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
2746}
2747
2748TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffPrimaryDisplay) {
2749 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToOffVariant>>();
2750}
2751
2752TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffPrimaryDisplay) {
2753 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
2754}
2755
2756TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozePrimaryDisplay) {
2757 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeVariant>>();
2758}
2759
2760TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozePrimaryDisplay) {
2761 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
2762}
2763
2764TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnPrimaryDisplay) {
2765 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeToOnVariant>>();
2766}
2767
2768TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnPrimaryDisplay) {
2769 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
2770}
2771
2772TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendPrimaryDisplay) {
2773 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
2774}
2775
2776TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownPrimaryDisplay) {
2777 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToUnknownVariant>>();
2778}
2779
2780TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnExternalDisplay) {
2781 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToOnVariant>>();
2782}
2783
2784TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendExternalDisplay) {
2785 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
2786}
2787
2788TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffExternalDisplay) {
2789 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToOffVariant>>();
2790}
2791
2792TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffExternalDisplay) {
2793 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
2794}
2795
2796TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeExternalDisplay) {
2797 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeVariant>>();
2798}
2799
2800TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozeExternalDisplay) {
2801 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
2802}
2803
2804TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnExternalDisplay) {
2805 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeToOnVariant>>();
2806}
2807
2808TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnExternalDisplay) {
2809 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
2810}
2811
2812TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendExternalDisplay) {
2813 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
2814}
2815
2816TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownExternalDisplay) {
2817 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToUnknownVariant>>();
2818}
2819
Lloyd Piquef58625d2017-12-19 13:22:33 -08002820} // namespace
2821} // namespace android