blob: 508875de407edac1810e2bdf0f6c8032495eaaef [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"
Lloyd Pique41be5d22018-06-21 13:11:48 -070028#include "mock/MockDispSync.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080029#include "mock/MockEventControlThread.h"
30#include "mock/MockEventThread.h"
31#include "mock/MockMessageQueue.h"
32#include "mock/MockNativeWindowSurface.h"
33#include "mock/MockSurfaceInterceptor.h"
34#include "mock/RenderEngine/MockRenderEngine.h"
35#include "mock/gui/MockGraphicBufferConsumer.h"
36#include "mock/gui/MockGraphicBufferProducer.h"
37#include "mock/system/window/MockNativeWindow.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080038
39namespace android {
40namespace {
41
Lloyd Piquee39cad22017-12-20 17:01:29 -080042using testing::_;
43using testing::ByMove;
44using testing::DoAll;
45using testing::Mock;
46using testing::Return;
47using testing::SetArgPointee;
48
Lloyd Piqued883d5a2018-04-27 19:32:30 -070049using android::Hwc2::ColorMode;
Lloyd Piquee39cad22017-12-20 17:01:29 -080050using android::Hwc2::Error;
Lloyd Piqued883d5a2018-04-27 19:32:30 -070051using android::Hwc2::Hdr;
Lloyd Piquee39cad22017-12-20 17:01:29 -080052using android::Hwc2::IComposer;
53using android::Hwc2::IComposerClient;
Lloyd Piqued883d5a2018-04-27 19:32:30 -070054using android::Hwc2::PerFrameMetadataKey;
55using android::Hwc2::RenderIntent;
Lloyd Piquee39cad22017-12-20 17:01:29 -080056
Lloyd Piquec11e0d32018-01-22 18:44:59 -080057using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector;
58using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
Lloyd Pique1fa4d462018-01-22 18:03:16 -080059using HotplugEvent = TestableSurfaceFlinger::HotplugEvent;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080060using HWC2Display = TestableSurfaceFlinger::HWC2Display;
Lloyd Piquebc792092018-01-17 11:52:30 -080061
Lloyd Piquec11e0d32018-01-22 18:44:59 -080062constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666;
Lloyd Piquee39cad22017-12-20 17:01:29 -080063constexpr int32_t DEFAULT_DPI = 320;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080064constexpr int DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT = HAL_PIXEL_FORMAT_RGB_565;
Lloyd Piquee39cad22017-12-20 17:01:29 -080065
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -080066constexpr int HWC_POWER_MODE_LEET = 1337; // An out of range power mode value
67
Lloyd Piquec11e0d32018-01-22 18:44:59 -080068/* ------------------------------------------------------------------------
69 * Boolean avoidance
70 *
71 * To make calls and template instantiations more readable, we define some
72 * local enums along with an implicit bool conversion.
73 */
74
75#define BOOL_SUBSTITUTE(TYPENAME) enum class TYPENAME : bool { FALSE = false, TRUE = true };
76
77BOOL_SUBSTITUTE(Critical);
78BOOL_SUBSTITUTE(Async);
79BOOL_SUBSTITUTE(Secure);
80
81/* ------------------------------------------------------------------------
82 *
83 */
Lloyd Pique1fa4d462018-01-22 18:03:16 -080084
Lloyd Piquef58625d2017-12-19 13:22:33 -080085class DisplayTransactionTest : public testing::Test {
Lloyd Piquec11e0d32018-01-22 18:44:59 -080086public:
Lloyd Piquef58625d2017-12-19 13:22:33 -080087 DisplayTransactionTest();
88 ~DisplayTransactionTest() override;
89
Lloyd Pique1fa4d462018-01-22 18:03:16 -080090 // --------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -080091 // Mock/Fake injection
Lloyd Piquef58625d2017-12-19 13:22:33 -080092
Lloyd Piquec11e0d32018-01-22 18:44:59 -080093 void injectMockComposer(int virtualDisplayCount);
94 void injectFakeBufferQueueFactory();
95 void injectFakeNativeWindowSurfaceFactory();
Lloyd Pique1fa4d462018-01-22 18:03:16 -080096
97 // --------------------------------------------------------------------
98 // Postcondition helpers
99
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800100 bool hasHwcDisplay(hwc2_display_t displayId);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800101 bool hasTransactionFlagSet(int flag);
102 bool hasDisplayDevice(sp<IBinder> displayToken);
103 sp<DisplayDevice> getDisplayDevice(sp<IBinder> displayToken);
104 bool hasCurrentDisplayState(sp<IBinder> displayToken);
105 const DisplayDeviceState& getCurrentDisplayState(sp<IBinder> displayToken);
106 bool hasDrawingDisplayState(sp<IBinder> displayToken);
107 const DisplayDeviceState& getDrawingDisplayState(sp<IBinder> displayToken);
108
109 // --------------------------------------------------------------------
110 // Test instances
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800111
Lloyd Piquef58625d2017-12-19 13:22:33 -0800112 TestableSurfaceFlinger mFlinger;
Lloyd Piquee39cad22017-12-20 17:01:29 -0800113 mock::EventThread* mEventThread = new mock::EventThread();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800114 mock::EventControlThread* mEventControlThread = new mock::EventControlThread();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800115
116 // These mocks are created by the test, but are destroyed by SurfaceFlinger
117 // by virtue of being stored into a std::unique_ptr. However we still need
118 // to keep a reference to them for use in setting up call expectations.
119 RE::mock::RenderEngine* mRenderEngine = new RE::mock::RenderEngine();
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800120 Hwc2::mock::Composer* mComposer = nullptr;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800121 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
122 mock::SurfaceInterceptor* mSurfaceInterceptor = new mock::SurfaceInterceptor();
Lloyd Pique41be5d22018-06-21 13:11:48 -0700123 mock::DispSync* mPrimaryDispSync = new mock::DispSync();
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800124
125 // These mocks are created only when expected to be created via a factory.
126 sp<mock::GraphicBufferConsumer> mConsumer;
127 sp<mock::GraphicBufferProducer> mProducer;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800128 mock::NativeWindowSurface* mNativeWindowSurface = nullptr;
129 sp<mock::NativeWindow> mNativeWindow;
130 RE::mock::Surface* mRenderSurface = nullptr;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800131};
132
133DisplayTransactionTest::DisplayTransactionTest() {
134 const ::testing::TestInfo* const test_info =
135 ::testing::UnitTest::GetInstance()->current_test_info();
136 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800137
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800138 // Default to no wide color display support configured
139 mFlinger.mutableHasWideColorDisplay() = false;
140 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
141
142 // Default to using HWC virtual displays
143 mFlinger.mutableUseHwcVirtualDisplays() = true;
144
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800145 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
146 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
147 });
148
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800149 mFlinger.setCreateNativeWindowSurface([](auto) {
150 ADD_FAILURE() << "Unexpected request to create a native window surface.";
151 return nullptr;
152 });
153
154 mFlinger.mutableEventControlThread().reset(mEventControlThread);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800155 mFlinger.mutableEventThread().reset(mEventThread);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800156 mFlinger.mutableEventQueue().reset(mMessageQueue);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800157 mFlinger.setupRenderEngine(std::unique_ptr<RE::RenderEngine>(mRenderEngine));
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800158 mFlinger.mutableInterceptor().reset(mSurfaceInterceptor);
Lloyd Pique41be5d22018-06-21 13:11:48 -0700159 mFlinger.mutablePrimaryDispSync().reset(mPrimaryDispSync);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800160
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800161 injectMockComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -0800162}
163
164DisplayTransactionTest::~DisplayTransactionTest() {
165 const ::testing::TestInfo* const test_info =
166 ::testing::UnitTest::GetInstance()->current_test_info();
167 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
168}
169
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800170void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
171 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800172 EXPECT_CALL(*mComposer, getCapabilities())
173 .WillOnce(Return(std::vector<IComposer::Capability>()));
174 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
175 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -0800176
Lloyd Piquee39cad22017-12-20 17:01:29 -0800177 Mock::VerifyAndClear(mComposer);
178}
179
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800180void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800181 // This setup is only expected once per test.
182 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
183
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800184 mConsumer = new mock::GraphicBufferConsumer();
185 mProducer = new mock::GraphicBufferProducer();
186
187 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
188 *outProducer = mProducer;
189 *outConsumer = mConsumer;
190 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800191}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800192
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800193void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800194 // This setup is only expected once per test.
195 ASSERT_TRUE(mNativeWindowSurface == nullptr);
196
197 mNativeWindowSurface = new mock::NativeWindowSurface();
198 mNativeWindow = new mock::NativeWindow();
199
200 mFlinger.setCreateNativeWindowSurface(
201 [this](auto) { return std::unique_ptr<NativeWindowSurface>(mNativeWindowSurface); });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800202}
203
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800204bool DisplayTransactionTest::hasHwcDisplay(hwc2_display_t displayId) {
205 return mFlinger.mutableHwcDisplaySlots().count(displayId) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800206}
207
208bool DisplayTransactionTest::hasTransactionFlagSet(int flag) {
209 return mFlinger.mutableTransactionFlags() & flag;
210}
211
212bool DisplayTransactionTest::hasDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700213 return mFlinger.mutableDisplays().count(displayToken) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800214}
215
216sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700217 return mFlinger.mutableDisplays()[displayToken];
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800218}
219
220bool DisplayTransactionTest::hasCurrentDisplayState(sp<IBinder> displayToken) {
221 return mFlinger.mutableCurrentState().displays.indexOfKey(displayToken) >= 0;
222}
223
224const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(sp<IBinder> displayToken) {
225 return mFlinger.mutableCurrentState().displays.valueFor(displayToken);
226}
227
228bool DisplayTransactionTest::hasDrawingDisplayState(sp<IBinder> displayToken) {
229 return mFlinger.mutableDrawingState().displays.indexOfKey(displayToken) >= 0;
230}
231
232const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(sp<IBinder> displayToken) {
233 return mFlinger.mutableDrawingState().displays.valueFor(displayToken);
234}
235
236/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800237 *
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800238 */
239
Dominik Laskowski7e045462018-05-30 13:02:02 -0700240template <DisplayDevice::DisplayType type, DisplayDevice::DisplayType displayId, int width,
241 int height, Critical critical, Async async, Secure secure, int grallocUsage>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800242struct DisplayVariant {
243 // The display width and height
244 static constexpr int WIDTH = width;
245 static constexpr int HEIGHT = height;
246
247 static constexpr int GRALLOC_USAGE = grallocUsage;
248
249 // The type for this display
250 static constexpr DisplayDevice::DisplayType TYPE = type;
Dominik Laskowskif07b85b2018-06-11 12:49:15 -0700251 static_assert(TYPE != DisplayDevice::DISPLAY_ID_INVALID);
252
Dominik Laskowski7e045462018-05-30 13:02:02 -0700253 static constexpr DisplayDevice::DisplayType DISPLAY_ID = displayId;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800254
255 // When creating native window surfaces for the framebuffer, whether those should be critical
256 static constexpr Critical CRITICAL = critical;
257
258 // When creating native window surfaces for the framebuffer, whether those should be async
259 static constexpr Async ASYNC = async;
260
261 // Whether the display should be treated as secure
262 static constexpr Secure SECURE = secure;
263
264 static auto makeFakeExistingDisplayInjector(DisplayTransactionTest* test) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700265 auto injector = FakeDisplayDeviceInjector(test->mFlinger, TYPE, DISPLAY_ID);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800266 injector.setSecure(static_cast<bool>(SECURE));
267 return injector;
268 }
269
270 // Called by tests to set up any native window creation call expectations.
271 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
272 EXPECT_CALL(*test->mNativeWindowSurface, getNativeWindow())
273 .WillOnce(Return(test->mNativeWindow));
274 EXPECT_CALL(*test->mNativeWindow, perform(19)).WillRepeatedly(Return(NO_ERROR));
275
276 // For simplicity, we only expect to create a single render surface for
277 // each test.
278 ASSERT_TRUE(test->mRenderSurface == nullptr);
279 test->mRenderSurface = new RE::mock::Surface();
280 EXPECT_CALL(*test->mRenderEngine, createSurface())
281 .WillOnce(Return(ByMove(std::unique_ptr<RE::Surface>(test->mRenderSurface))));
282 EXPECT_CALL(*test->mRenderSurface, setAsync(static_cast<bool>(ASYNC))).Times(1);
283 EXPECT_CALL(*test->mRenderSurface, setCritical(static_cast<bool>(CRITICAL))).Times(1);
284 EXPECT_CALL(*test->mRenderSurface, setNativeWindow(test->mNativeWindow.get())).Times(1);
285 EXPECT_CALL(*test->mRenderSurface, queryWidth()).WillOnce(Return(WIDTH));
286 EXPECT_CALL(*test->mRenderSurface, queryHeight()).WillOnce(Return(HEIGHT));
287 }
288
289 static void setupFramebufferConsumerBufferQueueCallExpectations(DisplayTransactionTest* test) {
290 EXPECT_CALL(*test->mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR));
291 EXPECT_CALL(*test->mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR));
292 EXPECT_CALL(*test->mConsumer, setConsumerUsageBits(GRALLOC_USAGE))
293 .WillRepeatedly(Return(NO_ERROR));
294 EXPECT_CALL(*test->mConsumer, setDefaultBufferSize(WIDTH, HEIGHT))
295 .WillRepeatedly(Return(NO_ERROR));
296 EXPECT_CALL(*test->mConsumer, setMaxAcquiredBufferCount(_))
297 .WillRepeatedly(Return(NO_ERROR));
298 }
299
300 static void setupFramebufferProducerBufferQueueCallExpectations(DisplayTransactionTest* test) {
301 EXPECT_CALL(*test->mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return());
302 }
303};
304
305template <hwc2_display_t hwcDisplayId, HWC2::DisplayType hwcDisplayType, typename DisplayVariant>
306struct HwcDisplayVariant {
307 // The display id supplied by the HWC
308 static constexpr hwc2_display_t HWC_DISPLAY_ID = hwcDisplayId;
309
310 // The HWC display type
311 static constexpr HWC2::DisplayType HWC_DISPLAY_TYPE = hwcDisplayType;
312
313 // The HWC active configuration id
Lloyd Pique3c085a02018-05-09 19:38:32 -0700314 static constexpr int HWC_ACTIVE_CONFIG_ID = 2001;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800315
316 static void injectPendingHotplugEvent(DisplayTransactionTest* test,
317 HWC2::Connection connection) {
318 test->mFlinger.mutablePendingHotplugEvents().emplace_back(
319 HotplugEvent{HWC_DISPLAY_ID, connection});
320 }
321
322 // Called by tests to inject a HWC display setup
323 static void injectHwcDisplay(DisplayTransactionTest* test) {
324 FakeHwcDisplayInjector(DisplayVariant::TYPE, HWC_DISPLAY_TYPE)
325 .setHwcDisplayId(HWC_DISPLAY_ID)
326 .setWidth(DisplayVariant::WIDTH)
327 .setHeight(DisplayVariant::HEIGHT)
328 .setActiveConfig(HWC_ACTIVE_CONFIG_ID)
329 .inject(&test->mFlinger, test->mComposer);
330 }
331
332 static void setupHwcHotplugCallExpectations(DisplayTransactionTest* test) {
333 EXPECT_CALL(*test->mComposer, getDisplayType(HWC_DISPLAY_ID, _))
334 .WillOnce(DoAll(SetArgPointee<1>(static_cast<IComposerClient::DisplayType>(
335 HWC_DISPLAY_TYPE)),
336 Return(Error::NONE)));
337 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
338 EXPECT_CALL(*test->mComposer, getDisplayConfigs(HWC_DISPLAY_ID, _))
339 .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{HWC_ACTIVE_CONFIG_ID}),
340 Return(Error::NONE)));
341 EXPECT_CALL(*test->mComposer,
342 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
343 IComposerClient::Attribute::WIDTH, _))
344 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::WIDTH), Return(Error::NONE)));
345 EXPECT_CALL(*test->mComposer,
346 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
347 IComposerClient::Attribute::HEIGHT, _))
348 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::HEIGHT), Return(Error::NONE)));
349 EXPECT_CALL(*test->mComposer,
350 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
351 IComposerClient::Attribute::VSYNC_PERIOD, _))
352 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE)));
353 EXPECT_CALL(*test->mComposer,
354 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
355 IComposerClient::Attribute::DPI_X, _))
356 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
357 EXPECT_CALL(*test->mComposer,
358 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
359 IComposerClient::Attribute::DPI_Y, _))
360 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700361 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
362 .WillRepeatedly(Return(Error::UNSUPPORTED));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800363 }
364
365 // Called by tests to set up HWC call expectations
366 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
367 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY_ID, _))
Lloyd Pique3c085a02018-05-09 19:38:32 -0700368 .WillRepeatedly(DoAll(SetArgPointee<1>(HWC_ACTIVE_CONFIG_ID), Return(Error::NONE)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800369 }
370};
371
372struct NonHwcDisplayVariant {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800373 static void injectHwcDisplay(DisplayTransactionTest*) {}
374
375 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
376 EXPECT_CALL(*test->mComposer, getActiveConfig(_, _)).Times(0);
377 }
378};
379
380// Physical displays are expected to be synchronous, secure, and have a HWC display for output.
381constexpr uint32_t GRALLOC_USAGE_PHYSICAL_DISPLAY =
382 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
383
384template <hwc2_display_t hwcDisplayId, DisplayDevice::DisplayType type, int width, int height,
385 Critical critical>
386struct PhysicalDisplayVariant
387 : public DisplayVariant<type, type, width, height, critical, Async::FALSE, Secure::TRUE,
388 GRALLOC_USAGE_PHYSICAL_DISPLAY>,
389 public HwcDisplayVariant<hwcDisplayId, HWC2::DisplayType::Physical,
390 DisplayVariant<type, type, width, height, critical, Async::FALSE,
391 Secure::TRUE, GRALLOC_USAGE_PHYSICAL_DISPLAY>> {};
392
393// 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) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800583 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(_)).Times(0);
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700584 }
585};
586
587template <typename Display>
588struct NoPerFrameMetadataSupportVariant {
589 static constexpr int PER_FRAME_METADATA_KEYS = 0;
590 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800591 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
592 .WillOnce(Return(std::vector<PerFrameMetadataKey>()));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700593 }
594};
595
596template <typename Display>
597struct Smpte2086PerFrameMetadataSupportVariant {
598 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::SMPTE2086;
599 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800600 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
601 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700602 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X,
603 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y,
604 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X,
605 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y,
606 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X,
607 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y,
608 PerFrameMetadataKey::WHITE_POINT_X,
609 PerFrameMetadataKey::WHITE_POINT_Y,
610 PerFrameMetadataKey::MAX_LUMINANCE,
611 PerFrameMetadataKey::MIN_LUMINANCE,
Chia-I Wud7e01d72018-06-21 13:39:09 +0800612 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700613 }
614};
615
616template <typename Display>
617struct Cta861_3_PerFrameMetadataSupportVariant {
618 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::CTA861_3;
619 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800620 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
621 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700622 PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL,
623 PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL,
Chia-I Wud7e01d72018-06-21 13:39:09 +0800624 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700625 }
626};
627
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800628/* ------------------------------------------------------------------------
629 * Typical display configurations to test
630 */
631
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700632template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy,
633 typename PerFrameMetadataSupportPolicy>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800634struct Case {
635 using Display = DisplayPolicy;
636 using WideColorSupport = WideColorSupportPolicy;
637 using HdrSupport = HdrSupportPolicy;
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700638 using PerFrameMetadataSupport = PerFrameMetadataSupportPolicy;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800639};
640
641using SimplePrimaryDisplayCase =
642 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700643 HdrNotSupportedVariant<PrimaryDisplayVariant>,
644 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800645using SimpleExternalDisplayCase =
646 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700647 HdrNotSupportedVariant<ExternalDisplayVariant>,
648 NoPerFrameMetadataSupportVariant<ExternalDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800649using SimpleTertiaryDisplayCase =
650 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700651 HdrNotSupportedVariant<TertiaryDisplayVariant>,
652 NoPerFrameMetadataSupportVariant<TertiaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800653using NonHwcVirtualDisplayCase =
654 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700655 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant,
656 NonHwcPerFrameMetadataSupportVariant>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800657using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
658using HwcVirtualDisplayCase =
659 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
Chia-I Wu0f509fb2018-06-21 15:52:50 +0800660 NonHwcDisplayHdrSupportVariant, NonHwcPerFrameMetadataSupportVariant>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800661using WideColorP3ColorimetricDisplayCase =
662 Case<PrimaryDisplayVariant, WideColorP3ColorimetricSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700663 HdrNotSupportedVariant<PrimaryDisplayVariant>,
664 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800665using Hdr10DisplayCase =
666 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700667 Hdr10SupportedVariant<PrimaryDisplayVariant>,
668 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800669using HdrHlgDisplayCase =
670 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700671 HdrHlgSupportedVariant<PrimaryDisplayVariant>,
672 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800673using HdrDolbyVisionDisplayCase =
674 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700675 HdrDolbyVisionSupportedVariant<PrimaryDisplayVariant>,
676 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
677using HdrSmpte2086DisplayCase =
678 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
679 HdrNotSupportedVariant<PrimaryDisplayVariant>,
680 Smpte2086PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
681using HdrCta861_3_DisplayCase =
682 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
683 HdrNotSupportedVariant<PrimaryDisplayVariant>,
684 Cta861_3_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Dominik Laskowskif07b85b2018-06-11 12:49:15 -0700685
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800686/* ------------------------------------------------------------------------
Lloyd Pique6cf11032018-01-22 18:57:44 -0800687 *
688 * SurfaceFlinger::onHotplugReceived
689 */
690
691TEST_F(DisplayTransactionTest, hotplugEnqueuesEventsForDisplayTransaction) {
692 constexpr int currentSequenceId = 123;
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700693 constexpr hwc2_display_t hwcDisplayId1 = 456;
694 constexpr hwc2_display_t hwcDisplayId2 = 654;
Lloyd Pique6cf11032018-01-22 18:57:44 -0800695
696 // --------------------------------------------------------------------
697 // Preconditions
698
699 // Set the current sequence id for accepted events
700 mFlinger.mutableComposerSequenceId() = currentSequenceId;
701
702 // Set the main thread id so that the current thread does not appear to be
703 // the main thread.
704 mFlinger.mutableMainThreadId() = std::thread::id();
705
706 // --------------------------------------------------------------------
707 // Call Expectations
708
709 // We expect invalidate() to be invoked once to trigger display transaction
710 // processing.
711 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
712
713 // --------------------------------------------------------------------
714 // Invocation
715
716 // Simulate two hotplug events (a connect and a disconnect)
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700717 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId1, HWC2::Connection::Connected);
718 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId2, HWC2::Connection::Disconnected);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800719
720 // --------------------------------------------------------------------
721 // Postconditions
722
723 // The display transaction needed flag should be set.
724 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
725
726 // All events should be in the pending event queue.
727 const auto& pendingEvents = mFlinger.mutablePendingHotplugEvents();
728 ASSERT_EQ(2u, pendingEvents.size());
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700729 EXPECT_EQ(hwcDisplayId1, pendingEvents[0].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800730 EXPECT_EQ(HWC2::Connection::Connected, pendingEvents[0].connection);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700731 EXPECT_EQ(hwcDisplayId2, pendingEvents[1].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800732 EXPECT_EQ(HWC2::Connection::Disconnected, pendingEvents[1].connection);
733}
734
735TEST_F(DisplayTransactionTest, hotplugDiscardsUnexpectedEvents) {
736 constexpr int currentSequenceId = 123;
737 constexpr int otherSequenceId = 321;
738 constexpr hwc2_display_t displayId = 456;
739
740 // --------------------------------------------------------------------
741 // Preconditions
742
743 // Set the current sequence id for accepted events
744 mFlinger.mutableComposerSequenceId() = currentSequenceId;
745
746 // Set the main thread id so that the current thread does not appear to be
747 // the main thread.
748 mFlinger.mutableMainThreadId() = std::thread::id();
749
750 // --------------------------------------------------------------------
751 // Call Expectations
752
753 // We do not expect any calls to invalidate().
754 EXPECT_CALL(*mMessageQueue, invalidate()).Times(0);
755
756 // --------------------------------------------------------------------
757 // Invocation
758
759 // Call with an unexpected sequence id
760 mFlinger.onHotplugReceived(otherSequenceId, displayId, HWC2::Connection::Invalid);
761
762 // --------------------------------------------------------------------
763 // Postconditions
764
765 // The display transaction needed flag should not be set
766 EXPECT_FALSE(hasTransactionFlagSet(eDisplayTransactionNeeded));
767
768 // There should be no pending events
769 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
770}
771
772TEST_F(DisplayTransactionTest, hotplugProcessesEnqueuedEventsIfCalledOnMainThread) {
773 constexpr int currentSequenceId = 123;
774 constexpr hwc2_display_t displayId1 = 456;
775
776 // --------------------------------------------------------------------
777 // Note:
778 // --------------------------------------------------------------------
779 // This test case is a bit tricky. We want to verify that
780 // onHotplugReceived() calls processDisplayHotplugEventsLocked(), but we
781 // don't really want to provide coverage for everything the later function
782 // does as there are specific tests for it.
783 // --------------------------------------------------------------------
784
785 // --------------------------------------------------------------------
786 // Preconditions
787
788 // Set the current sequence id for accepted events
789 mFlinger.mutableComposerSequenceId() = currentSequenceId;
790
791 // Set the main thread id so that the current thread does appear to be the
792 // main thread.
793 mFlinger.mutableMainThreadId() = std::this_thread::get_id();
794
795 // --------------------------------------------------------------------
796 // Call Expectations
797
798 // We expect invalidate() to be invoked once to trigger display transaction
799 // processing.
800 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
801
802 // --------------------------------------------------------------------
803 // Invocation
804
805 // Simulate a disconnect on a display id that is not connected. This should
806 // be enqueued by onHotplugReceived(), and dequeued by
807 // processDisplayHotplugEventsLocked(), but then ignored as invalid.
808 mFlinger.onHotplugReceived(currentSequenceId, displayId1, HWC2::Connection::Disconnected);
809
810 // --------------------------------------------------------------------
811 // Postconditions
812
813 // The display transaction needed flag should be set.
814 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
815
816 // There should be no event queued on return, as it should have been
817 // processed.
818 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
819}
820
821/* ------------------------------------------------------------------------
Lloyd Piquea482f992018-01-22 19:00:34 -0800822 * SurfaceFlinger::createDisplay
823 */
824
825TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForNonsecureDisplay) {
826 const String8 name("virtual.test");
827
828 // --------------------------------------------------------------------
829 // Call Expectations
830
831 // The call should notify the interceptor that a display was created.
832 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
833
834 // --------------------------------------------------------------------
835 // Invocation
836
837 sp<IBinder> displayToken = mFlinger.createDisplay(name, false);
838
839 // --------------------------------------------------------------------
840 // Postconditions
841
842 // The display should have been added to the current state
843 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
844 const auto& display = getCurrentDisplayState(displayToken);
845 EXPECT_EQ(DisplayDevice::DISPLAY_VIRTUAL, display.type);
846 EXPECT_EQ(false, display.isSecure);
847 EXPECT_EQ(name.string(), display.displayName);
848
849 // --------------------------------------------------------------------
850 // Cleanup conditions
851
852 // Destroying the display invalidates the display state.
853 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
854}
855
856TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForSecureDisplay) {
857 const String8 name("virtual.test");
858
859 // --------------------------------------------------------------------
860 // Call Expectations
861
862 // The call should notify the interceptor that a display was created.
863 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
864
865 // --------------------------------------------------------------------
866 // Invocation
867
868 sp<IBinder> displayToken = mFlinger.createDisplay(name, true);
869
870 // --------------------------------------------------------------------
871 // Postconditions
872
873 // The display should have been added to the current state
874 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
875 const auto& display = getCurrentDisplayState(displayToken);
876 EXPECT_EQ(DisplayDevice::DISPLAY_VIRTUAL, display.type);
877 EXPECT_EQ(true, display.isSecure);
878 EXPECT_EQ(name.string(), display.displayName);
879
880 // --------------------------------------------------------------------
881 // Cleanup conditions
882
883 // Destroying the display invalidates the display state.
884 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
885}
886
887/* ------------------------------------------------------------------------
888 * SurfaceFlinger::destroyDisplay
889 */
890
891TEST_F(DisplayTransactionTest, destroyDisplayClearsCurrentStateForDisplay) {
892 using Case = NonHwcVirtualDisplayCase;
893
894 // --------------------------------------------------------------------
895 // Preconditions
896
897 // A virtual display exists
898 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
899 existing.inject();
900
901 // --------------------------------------------------------------------
902 // Call Expectations
903
904 // The call should notify the interceptor that a display was created.
905 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
906
907 // Destroying the display invalidates the display state.
908 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
909
910 // --------------------------------------------------------------------
911 // Invocation
912
913 mFlinger.destroyDisplay(existing.token());
914
915 // --------------------------------------------------------------------
916 // Postconditions
917
918 // The display should have been removed from the current state
919 EXPECT_FALSE(hasCurrentDisplayState(existing.token()));
920
921 // Ths display should still exist in the drawing state
922 EXPECT_TRUE(hasDrawingDisplayState(existing.token()));
923
924 // The display transaction needed flasg should be set
925 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
926}
927
928TEST_F(DisplayTransactionTest, destroyDisplayHandlesUnknownDisplay) {
929 // --------------------------------------------------------------------
930 // Preconditions
931
932 sp<BBinder> displayToken = new BBinder();
933
934 // --------------------------------------------------------------------
935 // Invocation
936
937 mFlinger.destroyDisplay(displayToken);
938}
939
940/* ------------------------------------------------------------------------
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -0800941 * SurfaceFlinger::resetDisplayState
942 */
943
944TEST_F(DisplayTransactionTest, resetDisplayStateClearsState) {
945 using Case = NonHwcVirtualDisplayCase;
946
947 // --------------------------------------------------------------------
948 // Preconditions
949
950 // vsync is enabled and available
951 mFlinger.mutablePrimaryHWVsyncEnabled() = true;
952 mFlinger.mutableHWVsyncAvailable() = true;
953
954 // A display exists
955 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
956 existing.inject();
957
958 // --------------------------------------------------------------------
959 // Call Expectations
960
961 // The call disable vsyncs
962 EXPECT_CALL(*mEventControlThread, setVsyncEnabled(false)).Times(1);
963
964 // The call clears the current render engine surface
965 EXPECT_CALL(*mRenderEngine, resetCurrentSurface());
966
Lloyd Pique41be5d22018-06-21 13:11:48 -0700967 // The call ends any display resyncs
968 EXPECT_CALL(*mPrimaryDispSync, endResync()).Times(1);
969
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -0800970 // --------------------------------------------------------------------
971 // Invocation
972
973 mFlinger.resetDisplayState();
974
975 // --------------------------------------------------------------------
976 // Postconditions
977
978 // vsyncs should be off and not available.
979 EXPECT_FALSE(mFlinger.mutablePrimaryHWVsyncEnabled());
980 EXPECT_FALSE(mFlinger.mutableHWVsyncAvailable());
981
982 // The display should have been removed from the display map.
983 EXPECT_FALSE(hasDisplayDevice(existing.token()));
984
985 // The display should still exist in the current state
986 EXPECT_TRUE(hasCurrentDisplayState(existing.token()));
987
988 // The display should have been removed from the drawing state
989 EXPECT_FALSE(hasDrawingDisplayState(existing.token()));
990}
991
992/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800993 * SurfaceFlinger::setupNewDisplayDeviceInternal
994 */
995
996class SetupNewDisplayDeviceInternalTest : public DisplayTransactionTest {
997public:
998 template <typename T>
999 void setupNewDisplayDeviceInternalTest();
1000};
1001
1002template <typename Case>
1003void SetupNewDisplayDeviceInternalTest::setupNewDisplayDeviceInternalTest() {
1004 const sp<BBinder> displayToken = new BBinder();
1005 const sp<mock::DisplaySurface> displaySurface = new mock::DisplaySurface();
1006 const sp<mock::GraphicBufferProducer> producer = new mock::GraphicBufferProducer();
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001007
1008 // --------------------------------------------------------------------
1009 // Preconditions
1010
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001011 // Wide color displays support is configured appropriately
1012 Case::WideColorSupport::injectConfigChange(this);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001013
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001014 // The display is setup with the HWC.
1015 Case::Display::injectHwcDisplay(this);
1016
1017 // SurfaceFlinger will use a test-controlled factory for native window
1018 // surfaces.
1019 injectFakeNativeWindowSurfaceFactory();
1020
1021 // --------------------------------------------------------------------
1022 // Call Expectations
1023
1024 // Various native window calls will be made.
1025 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
Lloyd Pique3c085a02018-05-09 19:38:32 -07001026 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001027 Case::WideColorSupport::setupComposerCallExpectations(this);
1028 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001029 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001030
1031 // --------------------------------------------------------------------
1032 // Invocation
1033
1034 DisplayDeviceState state;
1035 state.type = Case::Display::TYPE;
1036 state.isSecure = static_cast<bool>(Case::Display::SECURE);
1037
1038 auto device = mFlinger.setupNewDisplayDeviceInternal(displayToken, Case::Display::TYPE, state,
1039 displaySurface, producer);
1040
1041 // --------------------------------------------------------------------
1042 // Postconditions
1043
1044 ASSERT_TRUE(device != nullptr);
1045 EXPECT_EQ(Case::Display::TYPE, device->getDisplayType());
1046 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
1047 EXPECT_EQ(Case::Display::WIDTH, device->getWidth());
1048 EXPECT_EQ(Case::Display::HEIGHT, device->getHeight());
1049 EXPECT_EQ(Case::WideColorSupport::WIDE_COLOR_SUPPORTED, device->hasWideColorGamut());
1050 EXPECT_EQ(Case::HdrSupport::HDR10_SUPPORTED, device->hasHDR10Support());
1051 EXPECT_EQ(Case::HdrSupport::HDR_HLG_SUPPORTED, device->hasHLGSupport());
1052 EXPECT_EQ(Case::HdrSupport::HDR_DOLBY_VISION_SUPPORTED, device->hasDolbyVisionSupport());
Lloyd Pique3c085a02018-05-09 19:38:32 -07001053 // Note: This is not Case::Display::HWC_ACTIVE_CONFIG_ID as the ids are
1054 // remapped, and the test only ever sets up one config. If there were an error
1055 // looking up the remapped index, device->getActiveConfig() would be -1 instead.
1056 EXPECT_EQ(0, device->getActiveConfig());
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001057 EXPECT_EQ(Case::PerFrameMetadataSupport::PER_FRAME_METADATA_KEYS,
1058 device->getSupportedPerFrameMetadata());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001059}
1060
1061TEST_F(SetupNewDisplayDeviceInternalTest, createSimplePrimaryDisplay) {
1062 setupNewDisplayDeviceInternalTest<SimplePrimaryDisplayCase>();
1063}
1064
1065TEST_F(SetupNewDisplayDeviceInternalTest, createSimpleExternalDisplay) {
1066 setupNewDisplayDeviceInternalTest<SimpleExternalDisplayCase>();
1067}
1068
1069TEST_F(SetupNewDisplayDeviceInternalTest, createNonHwcVirtualDisplay) {
1070 setupNewDisplayDeviceInternalTest<NonHwcVirtualDisplayCase>();
1071}
1072
1073TEST_F(SetupNewDisplayDeviceInternalTest, createHwcVirtualDisplay) {
1074 // We need to resize this so that the HWC thinks the virtual display
1075 // is something it created.
1076 mFlinger.mutableHwcDisplayData().resize(3);
1077
1078 setupNewDisplayDeviceInternalTest<HwcVirtualDisplayCase>();
1079}
1080
1081TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3Display) {
1082 setupNewDisplayDeviceInternalTest<WideColorP3ColorimetricDisplayCase>();
1083}
1084
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001085TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10Display) {
1086 setupNewDisplayDeviceInternalTest<Hdr10DisplayCase>();
1087}
1088
1089TEST_F(SetupNewDisplayDeviceInternalTest, createHdrHlgDisplay) {
1090 setupNewDisplayDeviceInternalTest<HdrHlgDisplayCase>();
1091}
1092
1093TEST_F(SetupNewDisplayDeviceInternalTest, createHdrDolbyVisionDisplay) {
1094 setupNewDisplayDeviceInternalTest<HdrDolbyVisionDisplayCase>();
1095}
1096
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001097TEST_F(SetupNewDisplayDeviceInternalTest, createHdrSmpte2086DisplayCase) {
1098 setupNewDisplayDeviceInternalTest<HdrSmpte2086DisplayCase>();
1099}
1100
1101TEST_F(SetupNewDisplayDeviceInternalTest, createHdrCta816_3_DisplayCase) {
1102 setupNewDisplayDeviceInternalTest<HdrCta861_3_DisplayCase>();
1103}
1104
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001105/* ------------------------------------------------------------------------
1106 * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded)
1107 */
1108
1109class HandleTransactionLockedTest : public DisplayTransactionTest {
1110public:
1111 template <typename Case>
1112 void setupCommonPreconditions();
1113
1114 template <typename Case>
1115 void setupCommonCallExpectationsForConnectProcessing();
1116
1117 template <typename Case>
1118 void setupCommonCallExpectationsForDisconnectProcessing();
1119
1120 template <typename Case>
1121 void processesHotplugConnectCommon();
1122
1123 template <typename Case>
1124 void ignoresHotplugConnectCommon();
1125
1126 template <typename Case>
1127 void processesHotplugDisconnectCommon();
1128
1129 template <typename Case>
1130 void verifyDisplayIsConnected(const sp<IBinder>& displayToken);
1131
1132 template <typename Case>
1133 void verifyPhysicalDisplayIsConnected();
1134
1135 void verifyDisplayIsNotConnected(const sp<IBinder>& displayToken);
1136};
1137
1138template <typename Case>
1139void HandleTransactionLockedTest::setupCommonPreconditions() {
1140 // Wide color displays support is configured appropriately
1141 Case::WideColorSupport::injectConfigChange(this);
1142
1143 // SurfaceFlinger will use a test-controlled factory for BufferQueues
1144 injectFakeBufferQueueFactory();
1145
1146 // SurfaceFlinger will use a test-controlled factory for native window
1147 // surfaces.
1148 injectFakeNativeWindowSurfaceFactory();
1149}
1150
1151template <typename Case>
1152void HandleTransactionLockedTest::setupCommonCallExpectationsForConnectProcessing() {
1153 Case::Display::setupHwcHotplugCallExpectations(this);
1154
1155 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1156 Case::Display::setupFramebufferProducerBufferQueueCallExpectations(this);
1157 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1158 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
1159
1160 Case::WideColorSupport::setupComposerCallExpectations(this);
1161 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001162 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001163
1164 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001165 EXPECT_CALL(*mEventThread,
1166 onHotplugReceived(Case::Display::TYPE == DisplayDevice::DISPLAY_PRIMARY
1167 ? EventThread::DisplayType::Primary
1168 : EventThread::DisplayType::External,
1169 true))
1170 .Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001171}
1172
1173template <typename Case>
1174void HandleTransactionLockedTest::setupCommonCallExpectationsForDisconnectProcessing() {
1175 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001176 EXPECT_CALL(*mEventThread,
1177 onHotplugReceived(Case::Display::TYPE == DisplayDevice::DISPLAY_PRIMARY
1178 ? EventThread::DisplayType::Primary
1179 : EventThread::DisplayType::External,
1180 false))
1181 .Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001182}
1183
1184template <typename Case>
1185void HandleTransactionLockedTest::verifyDisplayIsConnected(const sp<IBinder>& displayToken) {
1186 // The display device should have been set up in the list of displays.
1187 ASSERT_TRUE(hasDisplayDevice(displayToken));
1188 const auto& device = getDisplayDevice(displayToken);
1189 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
1190 EXPECT_EQ(Case::Display::TYPE == DisplayDevice::DISPLAY_PRIMARY, device->isPrimary());
1191
1192 // The display should have been set up in the current display state
1193 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1194 const auto& current = getCurrentDisplayState(displayToken);
1195 EXPECT_EQ(Case::Display::TYPE, current.type);
1196
1197 // The display should have been set up in the drawing display state
1198 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1199 const auto& draw = getDrawingDisplayState(displayToken);
1200 EXPECT_EQ(Case::Display::TYPE, draw.type);
1201}
1202
1203template <typename Case>
1204void HandleTransactionLockedTest::verifyPhysicalDisplayIsConnected() {
1205 // HWComposer should have an entry for the display
1206 EXPECT_TRUE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1207
1208 // The display should be set up as a built-in display.
1209 static_assert(0 <= Case::Display::TYPE &&
1210 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES,
1211 "Must use a valid physical display type index for the fixed-size array");
Dominik Laskowskieecd6592018-05-29 10:25:41 -07001212 auto& displayToken = mFlinger.mutableDisplayTokens()[Case::Display::TYPE];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001213 ASSERT_TRUE(displayToken != nullptr);
1214
1215 verifyDisplayIsConnected<Case>(displayToken);
1216}
1217
1218void HandleTransactionLockedTest::verifyDisplayIsNotConnected(const sp<IBinder>& displayToken) {
1219 EXPECT_FALSE(hasDisplayDevice(displayToken));
1220 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1221 EXPECT_FALSE(hasDrawingDisplayState(displayToken));
1222}
1223
1224template <typename Case>
1225void HandleTransactionLockedTest::processesHotplugConnectCommon() {
1226 // --------------------------------------------------------------------
1227 // Preconditions
1228
1229 setupCommonPreconditions<Case>();
1230
1231 // A hotplug connect event is enqueued for a display
1232 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001233
1234 // --------------------------------------------------------------------
1235 // Call Expectations
1236
1237 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillOnce(Return(false));
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001238
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001239 setupCommonCallExpectationsForConnectProcessing<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001240
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001241 // --------------------------------------------------------------------
1242 // Invocation
Lloyd Piquee39cad22017-12-20 17:01:29 -08001243
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001244 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
Lloyd Piquee39cad22017-12-20 17:01:29 -08001245
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001246 // --------------------------------------------------------------------
1247 // Postconditions
1248
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001249 verifyPhysicalDisplayIsConnected<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001250
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001251 // --------------------------------------------------------------------
1252 // Cleanup conditions
1253
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001254 EXPECT_CALL(*mComposer,
1255 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001256 .WillOnce(Return(Error::NONE));
1257 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Piquef58625d2017-12-19 13:22:33 -08001258}
1259
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001260template <typename Case>
1261void HandleTransactionLockedTest::ignoresHotplugConnectCommon() {
1262 // --------------------------------------------------------------------
1263 // Preconditions
1264
1265 setupCommonPreconditions<Case>();
1266
1267 // A hotplug connect event is enqueued for a display
1268 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1269
1270 // --------------------------------------------------------------------
1271 // Invocation
1272
1273 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1274
1275 // --------------------------------------------------------------------
1276 // Postconditions
1277
1278 // HWComposer should not have an entry for the display
1279 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1280}
1281
1282template <typename Case>
1283void HandleTransactionLockedTest::processesHotplugDisconnectCommon() {
1284 // --------------------------------------------------------------------
1285 // Preconditions
1286
1287 setupCommonPreconditions<Case>();
1288
1289 // A hotplug disconnect event is enqueued for a display
1290 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1291
1292 // The display is already completely set up.
1293 Case::Display::injectHwcDisplay(this);
1294 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1295 existing.inject();
1296
1297 // --------------------------------------------------------------------
1298 // Call Expectations
1299
1300 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1301
1302 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1303
1304 // --------------------------------------------------------------------
1305 // Invocation
1306
1307 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1308
1309 // --------------------------------------------------------------------
1310 // Postconditions
1311
1312 // HWComposer should not have an entry for the display
1313 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1314
1315 // The display should not be set up as a built-in display.
1316 ASSERT_TRUE(0 <= Case::Display::TYPE &&
1317 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES);
Dominik Laskowskieecd6592018-05-29 10:25:41 -07001318 auto displayToken = mFlinger.mutableDisplayTokens()[Case::Display::TYPE];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001319 EXPECT_TRUE(displayToken == nullptr);
1320
1321 // The existing token should have been removed
1322 verifyDisplayIsNotConnected(existing.token());
1323}
1324
1325TEST_F(HandleTransactionLockedTest, processesHotplugConnectPrimaryDisplay) {
1326 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1327}
1328
1329TEST_F(HandleTransactionLockedTest,
1330 processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected) {
1331 // Inject an external display.
1332 ExternalDisplayVariant::injectHwcDisplay(this);
1333
1334 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1335}
1336
1337TEST_F(HandleTransactionLockedTest, processesHotplugConnectExternalDisplay) {
1338 // Inject a primary display.
1339 PrimaryDisplayVariant::injectHwcDisplay(this);
1340
1341 processesHotplugConnectCommon<SimpleExternalDisplayCase>();
1342}
1343
1344TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected) {
1345 // Inject both a primary and external display.
1346 PrimaryDisplayVariant::injectHwcDisplay(this);
1347 ExternalDisplayVariant::injectHwcDisplay(this);
1348
1349 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1350
1351 ignoresHotplugConnectCommon<SimpleTertiaryDisplayCase>();
1352}
1353
1354TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfExternalForVrComposer) {
1355 // Inject a primary display.
1356 PrimaryDisplayVariant::injectHwcDisplay(this);
1357
1358 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(true));
1359
1360 ignoresHotplugConnectCommon<SimpleExternalDisplayCase>();
1361}
1362
1363TEST_F(HandleTransactionLockedTest, processHotplugDisconnectPrimaryDisplay) {
1364 processesHotplugDisconnectCommon<SimplePrimaryDisplayCase>();
1365}
1366
1367TEST_F(HandleTransactionLockedTest, processHotplugDisconnectExternalDisplay) {
1368 processesHotplugDisconnectCommon<SimpleExternalDisplayCase>();
1369}
1370
1371TEST_F(HandleTransactionLockedTest, processesHotplugConnectThenDisconnectPrimary) {
1372 using Case = SimplePrimaryDisplayCase;
1373
1374 // --------------------------------------------------------------------
1375 // Preconditions
1376
1377 setupCommonPreconditions<Case>();
1378
1379 // A hotplug connect event is enqueued for a display
1380 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1381 // A hotplug disconnect event is also enqueued for the same display
1382 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1383
1384 // --------------------------------------------------------------------
1385 // Call Expectations
1386
1387 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1388
1389 setupCommonCallExpectationsForConnectProcessing<Case>();
1390 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1391
1392 EXPECT_CALL(*mComposer,
1393 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1394 .WillOnce(Return(Error::NONE));
1395 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1396
1397 // --------------------------------------------------------------------
1398 // Invocation
1399
1400 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1401
1402 // --------------------------------------------------------------------
1403 // Postconditions
1404
1405 // HWComposer should not have an entry for the display
1406 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1407
1408 // The display should not be set up as a primary built-in display.
1409 ASSERT_TRUE(0 <= Case::Display::TYPE &&
1410 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES);
Dominik Laskowskieecd6592018-05-29 10:25:41 -07001411 auto displayToken = mFlinger.mutableDisplayTokens()[Case::Display::TYPE];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001412 EXPECT_TRUE(displayToken == nullptr);
1413}
1414
1415TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectThenConnectPrimary) {
1416 using Case = SimplePrimaryDisplayCase;
1417
1418 // --------------------------------------------------------------------
1419 // Preconditions
1420
1421 setupCommonPreconditions<Case>();
1422
1423 // The display is already completely set up.
1424 Case::Display::injectHwcDisplay(this);
1425 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1426 existing.inject();
1427
1428 // A hotplug disconnect event is enqueued for a display
1429 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1430 // A hotplug connect event is also enqueued for the same display
1431 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1432
1433 // --------------------------------------------------------------------
1434 // Call Expectations
1435
1436 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1437
1438 setupCommonCallExpectationsForConnectProcessing<Case>();
1439 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1440
1441 // --------------------------------------------------------------------
1442 // Invocation
1443
1444 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1445
1446 // --------------------------------------------------------------------
1447 // Postconditions
1448
1449 // The existing token should have been removed
1450 verifyDisplayIsNotConnected(existing.token());
1451 static_assert(0 <= Case::Display::TYPE &&
1452 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES,
1453 "Display type must be a built-in display");
Dominik Laskowskieecd6592018-05-29 10:25:41 -07001454 EXPECT_NE(existing.token(), mFlinger.mutableDisplayTokens()[Case::Display::TYPE]);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001455
1456 // A new display should be connected in its place
1457
1458 verifyPhysicalDisplayIsConnected<Case>();
1459
1460 // --------------------------------------------------------------------
1461 // Cleanup conditions
1462
1463 EXPECT_CALL(*mComposer,
1464 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1465 .WillOnce(Return(Error::NONE));
1466 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1467}
1468
1469TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAdded) {
1470 using Case = HwcVirtualDisplayCase;
1471
1472 // --------------------------------------------------------------------
1473 // Preconditions
1474
1475 // The HWC supports at least one virtual display
1476 injectMockComposer(1);
1477
1478 setupCommonPreconditions<Case>();
1479
1480 // A virtual display was added to the current state, and it has a
1481 // surface(producer)
1482 sp<BBinder> displayToken = new BBinder();
Lloyd Pique4c2ac022018-04-27 12:08:03 -07001483
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001484 DisplayDeviceState info;
1485 info.type = Case::Display::TYPE;
1486 info.isSecure = static_cast<bool>(Case::Display::SECURE);
1487
1488 sp<mock::GraphicBufferProducer> surface{new mock::GraphicBufferProducer()};
1489 info.surface = surface;
1490 mFlinger.mutableCurrentState().displays.add(displayToken, info);
1491
1492 // --------------------------------------------------------------------
1493 // Call Expectations
1494
1495 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1496 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1497
1498 EXPECT_CALL(*surface, query(NATIVE_WINDOW_WIDTH, _))
1499 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::WIDTH), Return(NO_ERROR)));
1500 EXPECT_CALL(*surface, query(NATIVE_WINDOW_HEIGHT, _))
1501 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::HEIGHT), Return(NO_ERROR)));
1502 EXPECT_CALL(*surface, query(NATIVE_WINDOW_FORMAT, _))
1503 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT),
1504 Return(NO_ERROR)));
1505 EXPECT_CALL(*surface, query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, _))
1506 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(NO_ERROR)));
1507
1508 EXPECT_CALL(*surface, setAsyncMode(true)).Times(1);
1509
1510 EXPECT_CALL(*mProducer, connect(_, _, _, _)).Times(1);
1511 EXPECT_CALL(*mProducer, disconnect(_, _)).Times(1);
1512
1513 Case::Display::setupHwcVirtualDisplayCreationCallExpectations(this);
1514 Case::WideColorSupport::setupComposerCallExpectations(this);
1515 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001516 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001517
1518 // --------------------------------------------------------------------
1519 // Invocation
1520
1521 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1522
1523 // --------------------------------------------------------------------
1524 // Postconditions
1525
1526 // The display device should have been set up in the list of displays.
1527 verifyDisplayIsConnected<Case>(displayToken);
1528
1529 // --------------------------------------------------------------------
1530 // Cleanup conditions
1531
1532 EXPECT_CALL(*mComposer, destroyVirtualDisplay(Case::Display::HWC_DISPLAY_ID))
1533 .WillOnce(Return(Error::NONE));
1534 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1535}
1536
1537TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAddedWithNoSurface) {
1538 using Case = HwcVirtualDisplayCase;
1539
1540 // --------------------------------------------------------------------
1541 // Preconditions
1542
1543 // The HWC supports at least one virtual display
1544 injectMockComposer(1);
1545
1546 setupCommonPreconditions<Case>();
1547
1548 // A virtual display was added to the current state, but it does not have a
1549 // surface.
1550 sp<BBinder> displayToken = new BBinder();
1551
1552 DisplayDeviceState info;
1553 info.type = Case::Display::TYPE;
Chia-I Wu0f509fb2018-06-21 15:52:50 +08001554 info.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001555
1556 mFlinger.mutableCurrentState().displays.add(displayToken, info);
1557
1558 // --------------------------------------------------------------------
1559 // Call Expectations
1560
1561 // --------------------------------------------------------------------
1562 // Invocation
1563
1564 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1565
1566 // --------------------------------------------------------------------
1567 // Postconditions
1568
1569 // There will not be a display device set up.
1570 EXPECT_FALSE(hasDisplayDevice(displayToken));
1571
1572 // The drawing display state will be set from the current display state.
1573 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1574 const auto& draw = getDrawingDisplayState(displayToken);
1575 EXPECT_EQ(Case::Display::TYPE, draw.type);
1576}
1577
1578TEST_F(HandleTransactionLockedTest, processesVirtualDisplayRemoval) {
1579 using Case = HwcVirtualDisplayCase;
1580
1581 // --------------------------------------------------------------------
1582 // Preconditions
1583
1584 // A virtual display is set up but is removed from the current state.
1585 mFlinger.mutableHwcDisplayData().resize(3);
1586 Case::Display::injectHwcDisplay(this);
1587 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1588 existing.inject();
1589 mFlinger.mutableCurrentState().displays.removeItem(existing.token());
1590
1591 // --------------------------------------------------------------------
1592 // Call Expectations
1593
1594 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1595
1596 // --------------------------------------------------------------------
1597 // Invocation
1598
1599 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1600
1601 // --------------------------------------------------------------------
1602 // Postconditions
1603
1604 // The existing token should have been removed
1605 verifyDisplayIsNotConnected(existing.token());
1606}
1607
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001608TEST_F(HandleTransactionLockedTest, processesDisplayLayerStackChanges) {
1609 using Case = NonHwcVirtualDisplayCase;
1610
1611 constexpr uint32_t oldLayerStack = 0u;
1612 constexpr uint32_t newLayerStack = 123u;
1613
1614 // --------------------------------------------------------------------
1615 // Preconditions
1616
1617 // A display is set up
1618 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1619 display.inject();
1620
1621 // There is a change to the layerStack state
1622 display.mutableDrawingDisplayState().layerStack = oldLayerStack;
1623 display.mutableCurrentDisplayState().layerStack = newLayerStack;
1624
1625 // --------------------------------------------------------------------
1626 // Invocation
1627
1628 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1629
1630 // --------------------------------------------------------------------
1631 // Postconditions
1632
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001633 EXPECT_EQ(newLayerStack, display.mutableDisplayDevice()->getLayerStack());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001634}
1635
1636TEST_F(HandleTransactionLockedTest, processesDisplayTransformChanges) {
1637 using Case = NonHwcVirtualDisplayCase;
1638
1639 constexpr int oldTransform = 0;
1640 constexpr int newTransform = 2;
1641
1642 // --------------------------------------------------------------------
1643 // Preconditions
1644
1645 // A display is set up
1646 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1647 display.inject();
1648
1649 // There is a change to the orientation state
1650 display.mutableDrawingDisplayState().orientation = oldTransform;
1651 display.mutableCurrentDisplayState().orientation = newTransform;
1652
1653 // --------------------------------------------------------------------
1654 // Invocation
1655
1656 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1657
1658 // --------------------------------------------------------------------
1659 // Postconditions
1660
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001661 EXPECT_EQ(newTransform, display.mutableDisplayDevice()->getOrientation());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001662}
1663
1664TEST_F(HandleTransactionLockedTest, processesDisplayViewportChanges) {
1665 using Case = NonHwcVirtualDisplayCase;
1666
1667 const Rect oldViewport(0, 0, 0, 0);
1668 const Rect newViewport(0, 0, 123, 456);
1669
1670 // --------------------------------------------------------------------
1671 // Preconditions
1672
1673 // A display is set up
1674 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1675 display.inject();
1676
1677 // There is a change to the viewport state
1678 display.mutableDrawingDisplayState().viewport = oldViewport;
1679 display.mutableCurrentDisplayState().viewport = newViewport;
1680
1681 // --------------------------------------------------------------------
1682 // Invocation
1683
1684 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1685
1686 // --------------------------------------------------------------------
1687 // Postconditions
1688
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001689 EXPECT_EQ(newViewport, display.mutableDisplayDevice()->getViewport());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001690}
1691
1692TEST_F(HandleTransactionLockedTest, processesDisplayFrameChanges) {
1693 using Case = NonHwcVirtualDisplayCase;
1694
1695 const Rect oldFrame(0, 0, 0, 0);
1696 const Rect newFrame(0, 0, 123, 456);
1697
1698 // --------------------------------------------------------------------
1699 // Preconditions
1700
1701 // A display is set up
1702 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1703 display.inject();
1704
1705 // There is a change to the viewport state
1706 display.mutableDrawingDisplayState().frame = oldFrame;
1707 display.mutableCurrentDisplayState().frame = newFrame;
1708
1709 // --------------------------------------------------------------------
1710 // Invocation
1711
1712 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1713
1714 // --------------------------------------------------------------------
1715 // Postconditions
1716
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001717 EXPECT_EQ(newFrame, display.mutableDisplayDevice()->getFrame());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001718}
1719
1720TEST_F(HandleTransactionLockedTest, processesDisplayWidthChanges) {
1721 using Case = NonHwcVirtualDisplayCase;
1722
1723 constexpr int oldWidth = 0;
1724 constexpr int oldHeight = 10;
1725 constexpr int newWidth = 123;
1726
1727 // --------------------------------------------------------------------
1728 // Preconditions
1729
1730 // A display is set up
1731 auto nativeWindow = new mock::NativeWindow();
1732 auto displaySurface = new mock::DisplaySurface();
1733 auto renderSurface = new RE::mock::Surface();
1734 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1735 display.setNativeWindow(nativeWindow);
1736 display.setDisplaySurface(displaySurface);
1737 display.setRenderSurface(std::unique_ptr<RE::Surface>(renderSurface));
1738 display.inject();
1739
1740 // There is a change to the viewport state
1741 display.mutableDrawingDisplayState().width = oldWidth;
1742 display.mutableDrawingDisplayState().height = oldHeight;
1743 display.mutableCurrentDisplayState().width = newWidth;
1744 display.mutableCurrentDisplayState().height = oldHeight;
1745
1746 // --------------------------------------------------------------------
1747 // Call Expectations
1748
1749 EXPECT_CALL(*renderSurface, setNativeWindow(nullptr)).Times(1);
1750 EXPECT_CALL(*displaySurface, resizeBuffers(newWidth, oldHeight)).Times(1);
1751 EXPECT_CALL(*renderSurface, setNativeWindow(nativeWindow)).Times(1);
1752 EXPECT_CALL(*renderSurface, queryWidth()).WillOnce(Return(newWidth));
1753 EXPECT_CALL(*renderSurface, queryHeight()).WillOnce(Return(oldHeight));
1754
1755 // --------------------------------------------------------------------
1756 // Invocation
1757
1758 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1759}
1760
1761TEST_F(HandleTransactionLockedTest, processesDisplayHeightChanges) {
1762 using Case = NonHwcVirtualDisplayCase;
1763
1764 constexpr int oldWidth = 0;
1765 constexpr int oldHeight = 10;
1766 constexpr int newHeight = 123;
1767
1768 // --------------------------------------------------------------------
1769 // Preconditions
1770
1771 // A display is set up
1772 auto nativeWindow = new mock::NativeWindow();
1773 auto displaySurface = new mock::DisplaySurface();
1774 auto renderSurface = new RE::mock::Surface();
1775 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1776 display.setNativeWindow(nativeWindow);
1777 display.setDisplaySurface(displaySurface);
1778 display.setRenderSurface(std::unique_ptr<RE::Surface>(renderSurface));
1779 display.inject();
1780
1781 // There is a change to the viewport state
1782 display.mutableDrawingDisplayState().width = oldWidth;
1783 display.mutableDrawingDisplayState().height = oldHeight;
1784 display.mutableCurrentDisplayState().width = oldWidth;
1785 display.mutableCurrentDisplayState().height = newHeight;
1786
1787 // --------------------------------------------------------------------
1788 // Call Expectations
1789
1790 EXPECT_CALL(*renderSurface, setNativeWindow(nullptr)).Times(1);
1791 EXPECT_CALL(*displaySurface, resizeBuffers(oldWidth, newHeight)).Times(1);
1792 EXPECT_CALL(*renderSurface, setNativeWindow(nativeWindow)).Times(1);
1793 EXPECT_CALL(*renderSurface, queryWidth()).WillOnce(Return(oldWidth));
1794 EXPECT_CALL(*renderSurface, queryHeight()).WillOnce(Return(newHeight));
1795
1796 // --------------------------------------------------------------------
1797 // Invocation
1798
1799 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1800}
1801
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001802/* ------------------------------------------------------------------------
1803 * SurfaceFlinger::setDisplayStateLocked
1804 */
1805
1806TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithUnknownDisplay) {
1807 // --------------------------------------------------------------------
1808 // Preconditions
1809
1810 // We have an unknown display token not associated with a known display
1811 sp<BBinder> displayToken = new BBinder();
1812
1813 // The requested display state references the unknown display.
1814 DisplayState state;
1815 state.what = DisplayState::eLayerStackChanged;
1816 state.token = displayToken;
1817 state.layerStack = 456;
1818
1819 // --------------------------------------------------------------------
1820 // Invocation
1821
1822 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1823
1824 // --------------------------------------------------------------------
1825 // Postconditions
1826
1827 // The returned flags are empty
1828 EXPECT_EQ(0u, flags);
1829
1830 // The display token still doesn't match anything known.
1831 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1832}
1833
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001834TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWhenNoChanges) {
1835 using Case = SimplePrimaryDisplayCase;
1836
1837 // --------------------------------------------------------------------
1838 // Preconditions
1839
1840 // A display is already set up
1841 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1842 display.inject();
1843
1844 // No changes are made to the display
1845 DisplayState state;
1846 state.what = 0;
1847 state.token = display.token();
1848
1849 // --------------------------------------------------------------------
1850 // Invocation
1851
1852 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1853
1854 // --------------------------------------------------------------------
1855 // Postconditions
1856
1857 // The returned flags are empty
1858 EXPECT_EQ(0u, flags);
1859}
1860
1861TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSurfaceDidNotChange) {
1862 using Case = SimplePrimaryDisplayCase;
1863
1864 // --------------------------------------------------------------------
1865 // Preconditions
1866
1867 // A display is already set up
1868 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1869 display.inject();
1870
1871 // There is a surface that can be set.
1872 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
1873
1874 // The current display state has the surface set
1875 display.mutableCurrentDisplayState().surface = surface;
1876
1877 // The incoming request sets the same surface
1878 DisplayState state;
1879 state.what = DisplayState::eSurfaceChanged;
1880 state.token = display.token();
1881 state.surface = surface;
1882
1883 // --------------------------------------------------------------------
1884 // Invocation
1885
1886 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1887
1888 // --------------------------------------------------------------------
1889 // Postconditions
1890
1891 // The returned flags are empty
1892 EXPECT_EQ(0u, flags);
1893
1894 // The current display state is unchanged.
1895 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
1896}
1897
1898TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfSurfaceChanged) {
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 does not have a surface
1912 display.mutableCurrentDisplayState().surface = nullptr;
1913
1914 // The incoming request sets a 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 indicate a transaction is needed
1929 EXPECT_EQ(eDisplayTransactionNeeded, flags);
1930
1931 // The current display layer stack state is set to the new value
1932 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
1933}
1934
1935TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfLayerStackDidNotChange) {
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 // The display has a layer stack set
1946 display.mutableCurrentDisplayState().layerStack = 456u;
1947
1948 // The incoming request sets the same layer stack
1949 DisplayState state;
1950 state.what = DisplayState::eLayerStackChanged;
1951 state.token = display.token();
1952 state.layerStack = 456u;
1953
1954 // --------------------------------------------------------------------
1955 // Invocation
1956
1957 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1958
1959 // --------------------------------------------------------------------
1960 // Postconditions
1961
1962 // The returned flags are empty
1963 EXPECT_EQ(0u, flags);
1964
1965 // The current display state is unchanged
1966 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
1967}
1968
1969TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfLayerStackChanged) {
1970 using Case = SimplePrimaryDisplayCase;
1971
1972 // --------------------------------------------------------------------
1973 // Preconditions
1974
1975 // A display is set up
1976 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1977 display.inject();
1978
1979 // The display has a layer stack set
1980 display.mutableCurrentDisplayState().layerStack = 654u;
1981
1982 // The incoming request sets a different layer stack
1983 DisplayState state;
1984 state.what = DisplayState::eLayerStackChanged;
1985 state.token = display.token();
1986 state.layerStack = 456u;
1987
1988 // --------------------------------------------------------------------
1989 // Invocation
1990
1991 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1992
1993 // --------------------------------------------------------------------
1994 // Postconditions
1995
1996 // The returned flags indicate a transaction is needed
1997 EXPECT_EQ(eDisplayTransactionNeeded, flags);
1998
1999 // The desired display state has been set to the new value.
2000 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2001}
2002
2003TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfProjectionDidNotChange) {
2004 using Case = SimplePrimaryDisplayCase;
2005 constexpr int initialOrientation = 180;
2006 const Rect initialFrame = {1, 2, 3, 4};
2007 const Rect initialViewport = {5, 6, 7, 8};
2008
2009 // --------------------------------------------------------------------
2010 // Preconditions
2011
2012 // A display is set up
2013 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2014 display.inject();
2015
2016 // The current display state projection state is all set
2017 display.mutableCurrentDisplayState().orientation = initialOrientation;
2018 display.mutableCurrentDisplayState().frame = initialFrame;
2019 display.mutableCurrentDisplayState().viewport = initialViewport;
2020
2021 // The incoming request sets the same projection state
2022 DisplayState state;
2023 state.what = DisplayState::eDisplayProjectionChanged;
2024 state.token = display.token();
2025 state.orientation = initialOrientation;
2026 state.frame = initialFrame;
2027 state.viewport = initialViewport;
2028
2029 // --------------------------------------------------------------------
2030 // Invocation
2031
2032 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2033
2034 // --------------------------------------------------------------------
2035 // Postconditions
2036
2037 // The returned flags are empty
2038 EXPECT_EQ(0u, flags);
2039
2040 // The current display state is unchanged
2041 EXPECT_EQ(initialOrientation, display.getCurrentDisplayState().orientation);
2042
2043 EXPECT_EQ(initialFrame, display.getCurrentDisplayState().frame);
2044 EXPECT_EQ(initialViewport, display.getCurrentDisplayState().viewport);
2045}
2046
2047TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfOrientationChanged) {
2048 using Case = SimplePrimaryDisplayCase;
2049 constexpr int initialOrientation = 90;
2050 constexpr int desiredOrientation = 180;
2051
2052 // --------------------------------------------------------------------
2053 // Preconditions
2054
2055 // A display is set up
2056 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2057 display.inject();
2058
2059 // The current display state has an orientation set
2060 display.mutableCurrentDisplayState().orientation = initialOrientation;
2061
2062 // The incoming request sets a different orientation
2063 DisplayState state;
2064 state.what = DisplayState::eDisplayProjectionChanged;
2065 state.token = display.token();
2066 state.orientation = desiredOrientation;
2067
2068 // --------------------------------------------------------------------
2069 // Invocation
2070
2071 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2072
2073 // --------------------------------------------------------------------
2074 // Postconditions
2075
2076 // The returned flags indicate a transaction is needed
2077 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2078
2079 // The current display state has the new value.
2080 EXPECT_EQ(desiredOrientation, display.getCurrentDisplayState().orientation);
2081}
2082
2083TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfFrameChanged) {
2084 using Case = SimplePrimaryDisplayCase;
2085 const Rect initialFrame = {0, 0, 0, 0};
2086 const Rect desiredFrame = {5, 6, 7, 8};
2087
2088 // --------------------------------------------------------------------
2089 // Preconditions
2090
2091 // A display is set up
2092 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2093 display.inject();
2094
2095 // The current display state does not have a frame
2096 display.mutableCurrentDisplayState().frame = initialFrame;
2097
2098 // The incoming request sets a frame
2099 DisplayState state;
2100 state.what = DisplayState::eDisplayProjectionChanged;
2101 state.token = display.token();
2102 state.frame = desiredFrame;
2103
2104 // --------------------------------------------------------------------
2105 // Invocation
2106
2107 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2108
2109 // --------------------------------------------------------------------
2110 // Postconditions
2111
2112 // The returned flags indicate a transaction is needed
2113 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2114
2115 // The current display state has the new value.
2116 EXPECT_EQ(desiredFrame, display.getCurrentDisplayState().frame);
2117}
2118
2119TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfViewportChanged) {
2120 using Case = SimplePrimaryDisplayCase;
2121 const Rect initialViewport = {0, 0, 0, 0};
2122 const Rect desiredViewport = {5, 6, 7, 8};
2123
2124 // --------------------------------------------------------------------
2125 // Preconditions
2126
2127 // A display is set up
2128 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2129 display.inject();
2130
2131 // The current display state does not have a viewport
2132 display.mutableCurrentDisplayState().viewport = initialViewport;
2133
2134 // The incoming request sets a viewport
2135 DisplayState state;
2136 state.what = DisplayState::eDisplayProjectionChanged;
2137 state.token = display.token();
2138 state.viewport = desiredViewport;
2139
2140 // --------------------------------------------------------------------
2141 // Invocation
2142
2143 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2144
2145 // --------------------------------------------------------------------
2146 // Postconditions
2147
2148 // The returned flags indicate a transaction is needed
2149 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2150
2151 // The current display state has the new value.
2152 EXPECT_EQ(desiredViewport, display.getCurrentDisplayState().viewport);
2153}
2154
2155TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSizeDidNotChange) {
2156 using Case = SimplePrimaryDisplayCase;
2157 constexpr uint32_t initialWidth = 1024;
2158 constexpr uint32_t initialHeight = 768;
2159
2160 // --------------------------------------------------------------------
2161 // Preconditions
2162
2163 // A display is set up
2164 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2165 display.inject();
2166
2167 // The current display state has a size set
2168 display.mutableCurrentDisplayState().width = initialWidth;
2169 display.mutableCurrentDisplayState().height = initialHeight;
2170
2171 // The incoming request sets the same display size
2172 DisplayState state;
2173 state.what = DisplayState::eDisplaySizeChanged;
2174 state.token = display.token();
2175 state.width = initialWidth;
2176 state.height = initialHeight;
2177
2178 // --------------------------------------------------------------------
2179 // Invocation
2180
2181 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2182
2183 // --------------------------------------------------------------------
2184 // Postconditions
2185
2186 // The returned flags are empty
2187 EXPECT_EQ(0u, flags);
2188
2189 // The current display state is unchanged
2190 EXPECT_EQ(initialWidth, display.getCurrentDisplayState().width);
2191 EXPECT_EQ(initialHeight, display.getCurrentDisplayState().height);
2192}
2193
2194TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfWidthChanged) {
2195 using Case = SimplePrimaryDisplayCase;
2196 constexpr uint32_t initialWidth = 0;
2197 constexpr uint32_t desiredWidth = 1024;
2198
2199 // --------------------------------------------------------------------
2200 // Preconditions
2201
2202 // A display is set up
2203 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2204 display.inject();
2205
2206 // The display does not yet have a width
2207 display.mutableCurrentDisplayState().width = initialWidth;
2208
2209 // The incoming request sets a display width
2210 DisplayState state;
2211 state.what = DisplayState::eDisplaySizeChanged;
2212 state.token = display.token();
2213 state.width = desiredWidth;
2214
2215 // --------------------------------------------------------------------
2216 // Invocation
2217
2218 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2219
2220 // --------------------------------------------------------------------
2221 // Postconditions
2222
2223 // The returned flags indicate a transaction is needed
2224 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2225
2226 // The current display state has the new value.
2227 EXPECT_EQ(desiredWidth, display.getCurrentDisplayState().width);
2228}
2229
2230TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfHeightChanged) {
2231 using Case = SimplePrimaryDisplayCase;
2232 constexpr uint32_t initialHeight = 0;
2233 constexpr uint32_t desiredHeight = 768;
2234
2235 // --------------------------------------------------------------------
2236 // Preconditions
2237
2238 // A display is set up
2239 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2240 display.inject();
2241
2242 // The display does not yet have a height
2243 display.mutableCurrentDisplayState().height = initialHeight;
2244
2245 // The incoming request sets a display height
2246 DisplayState state;
2247 state.what = DisplayState::eDisplaySizeChanged;
2248 state.token = display.token();
2249 state.height = desiredHeight;
2250
2251 // --------------------------------------------------------------------
2252 // Invocation
2253
2254 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2255
2256 // --------------------------------------------------------------------
2257 // Postconditions
2258
2259 // The returned flags indicate a transaction is needed
2260 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2261
2262 // The current display state has the new value.
2263 EXPECT_EQ(desiredHeight, display.getCurrentDisplayState().height);
2264}
2265
Lloyd Pique86016da2018-03-01 16:09:38 -08002266/* ------------------------------------------------------------------------
2267 * SurfaceFlinger::onInitializeDisplays
2268 */
2269
2270TEST_F(DisplayTransactionTest, onInitializeDisplaysSetsUpPrimaryDisplay) {
2271 using Case = SimplePrimaryDisplayCase;
2272
2273 // --------------------------------------------------------------------
2274 // Preconditions
2275
2276 // A primary display is set up
2277 Case::Display::injectHwcDisplay(this);
2278 auto primaryDisplay = Case::Display::makeFakeExistingDisplayInjector(this);
2279 primaryDisplay.inject();
2280
2281 // --------------------------------------------------------------------
2282 // Call Expectations
2283
2284 // We expect the surface interceptor to possibly be used, but we treat it as
2285 // disabled since it is called as a side effect rather than directly by this
2286 // function.
2287 EXPECT_CALL(*mSurfaceInterceptor, isEnabled()).WillOnce(Return(false));
2288
2289 // We expect a call to get the active display config.
2290 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
2291
2292 // We expect invalidate() to be invoked once to trigger display transaction
2293 // processing.
2294 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
2295
2296 // --------------------------------------------------------------------
2297 // Invocation
2298
2299 mFlinger.onInitializeDisplays();
2300
2301 // --------------------------------------------------------------------
2302 // Postconditions
2303
2304 // The primary display should have a current state
2305 ASSERT_TRUE(hasCurrentDisplayState(primaryDisplay.token()));
2306 const auto& primaryDisplayState = getCurrentDisplayState(primaryDisplay.token());
2307 // The layer stack state should be set to zero
2308 EXPECT_EQ(0u, primaryDisplayState.layerStack);
2309 // The orientation state should be set to zero
2310 EXPECT_EQ(0, primaryDisplayState.orientation);
2311
2312 // The frame state should be set to INVALID
2313 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.frame);
2314
2315 // The viewport state should be set to INVALID
2316 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.viewport);
2317
2318 // The width and height should both be zero
2319 EXPECT_EQ(0u, primaryDisplayState.width);
2320 EXPECT_EQ(0u, primaryDisplayState.height);
2321
2322 // The display should be set to HWC_POWER_MODE_NORMAL
2323 ASSERT_TRUE(hasDisplayDevice(primaryDisplay.token()));
2324 auto displayDevice = primaryDisplay.mutableDisplayDevice();
2325 EXPECT_EQ(HWC_POWER_MODE_NORMAL, displayDevice->getPowerMode());
2326
2327 // The display refresh period should be set in the frame tracker.
2328 FrameStats stats;
2329 mFlinger.getAnimFrameTracker().getStats(&stats);
2330 EXPECT_EQ(DEFAULT_REFRESH_RATE, stats.refreshPeriodNano);
2331
2332 // The display transaction needed flag should be set.
2333 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
2334
2335 // The compositor timing should be set to default values
2336 const auto& compositorTiming = mFlinger.getCompositorTiming();
2337 EXPECT_EQ(-DEFAULT_REFRESH_RATE, compositorTiming.deadline);
2338 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.interval);
2339 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.presentLatency);
2340}
2341
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002342/* ------------------------------------------------------------------------
2343 * SurfaceFlinger::setPowerModeInternal
2344 */
2345
2346// Used when we simulate a display that supports doze.
2347struct DozeIsSupportedVariant {
2348 static constexpr bool DOZE_SUPPORTED = true;
2349 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2350 IComposerClient::PowerMode::DOZE;
2351 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2352 IComposerClient::PowerMode::DOZE_SUSPEND;
2353};
2354
2355// Used when we simulate a display that does not support doze.
2356struct DozeNotSupportedVariant {
2357 static constexpr bool DOZE_SUPPORTED = false;
2358 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2359 IComposerClient::PowerMode::ON;
2360 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2361 IComposerClient::PowerMode::ON;
2362};
2363
2364struct EventThreadBaseSupportedVariant {
2365 static void setupEventAndEventControlThreadNoCallExpectations(DisplayTransactionTest* test) {
2366 // The event control thread should not be notified.
2367 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(_)).Times(0);
2368
2369 // The event thread should not be notified.
2370 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(0);
2371 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(0);
2372 }
2373};
2374
2375struct EventThreadNotSupportedVariant : public EventThreadBaseSupportedVariant {
2376 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2377 // These calls are only expected for the primary display.
2378
2379 // Instead expect no calls.
2380 setupEventAndEventControlThreadNoCallExpectations(test);
2381 }
2382
2383 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2384 // These calls are only expected for the primary display.
2385
2386 // Instead expect no calls.
2387 setupEventAndEventControlThreadNoCallExpectations(test);
2388 }
2389};
2390
2391struct EventThreadIsSupportedVariant : public EventThreadBaseSupportedVariant {
2392 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2393 // The event control thread should be notified to enable vsyncs
2394 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(true)).Times(1);
2395
2396 // The event thread should be notified that the screen was acquired.
2397 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(1);
2398 }
2399
2400 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2401 // There should be a call to setVsyncEnabled(false)
2402 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(false)).Times(1);
2403
2404 // The event thread should not be notified that the screen was released.
2405 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(1);
2406 }
2407};
2408
Lloyd Pique41be5d22018-06-21 13:11:48 -07002409struct DispSyncIsSupportedVariant {
2410 static void setupBeginResyncCallExpectations(DisplayTransactionTest* test) {
2411 EXPECT_CALL(*test->mPrimaryDispSync, reset()).Times(1);
2412 EXPECT_CALL(*test->mPrimaryDispSync, setPeriod(DEFAULT_REFRESH_RATE)).Times(1);
2413 EXPECT_CALL(*test->mPrimaryDispSync, beginResync()).Times(1);
2414 }
2415
2416 static void setupEndResyncCallExpectations(DisplayTransactionTest* test) {
2417 EXPECT_CALL(*test->mPrimaryDispSync, endResync()).Times(1);
2418 }
2419};
2420
2421struct DispSyncNotSupportedVariant {
2422 static void setupBeginResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2423
2424 static void setupEndResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2425};
2426
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002427// --------------------------------------------------------------------
2428// Note:
2429//
2430// There are a large number of transitions we could test, however we only test a
2431// selected subset which provides complete test coverage of the implementation.
2432// --------------------------------------------------------------------
2433
2434template <int initialPowerMode, int targetPowerMode>
2435struct TransitionVariantCommon {
2436 static constexpr auto INITIAL_POWER_MODE = initialPowerMode;
2437 static constexpr auto TARGET_POWER_MODE = targetPowerMode;
2438
2439 static void verifyPostconditions(DisplayTransactionTest*) {}
2440};
2441
2442struct TransitionOffToOnVariant
2443 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_NORMAL> {
2444 template <typename Case>
2445 static void setupCallExpectations(DisplayTransactionTest* test) {
2446 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2447 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002448 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002449 Case::setupRepaintEverythingCallExpectations(test);
2450 }
2451
2452 static void verifyPostconditions(DisplayTransactionTest* test) {
2453 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2454 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2455 }
2456};
2457
2458struct TransitionOffToDozeSuspendVariant
2459 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_DOZE_SUSPEND> {
2460 template <typename Case>
2461 static void setupCallExpectations(DisplayTransactionTest* test) {
2462 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2463 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2464 Case::setupRepaintEverythingCallExpectations(test);
2465 }
2466
2467 static void verifyPostconditions(DisplayTransactionTest* test) {
2468 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2469 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2470 }
2471};
2472
2473struct TransitionOnToOffVariant
2474 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_OFF> {
2475 template <typename Case>
2476 static void setupCallExpectations(DisplayTransactionTest* test) {
2477 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002478 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002479 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2480 }
2481
2482 static void verifyPostconditions(DisplayTransactionTest* test) {
2483 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2484 }
2485};
2486
2487struct TransitionDozeSuspendToOffVariant
2488 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_OFF> {
2489 template <typename Case>
2490 static void setupCallExpectations(DisplayTransactionTest* test) {
2491 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2492 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2493 }
2494
2495 static void verifyPostconditions(DisplayTransactionTest* test) {
2496 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2497 }
2498};
2499
2500struct TransitionOnToDozeVariant
2501 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE> {
2502 template <typename Case>
2503 static void setupCallExpectations(DisplayTransactionTest* test) {
2504 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2505 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2506 }
2507};
2508
2509struct TransitionDozeSuspendToDozeVariant
2510 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_DOZE> {
2511 template <typename Case>
2512 static void setupCallExpectations(DisplayTransactionTest* test) {
2513 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002514 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002515 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2516 }
2517};
2518
2519struct TransitionDozeToOnVariant
2520 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE, HWC_POWER_MODE_NORMAL> {
2521 template <typename Case>
2522 static void setupCallExpectations(DisplayTransactionTest* test) {
2523 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2524 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2525 }
2526};
2527
2528struct TransitionDozeSuspendToOnVariant
2529 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_NORMAL> {
2530 template <typename Case>
2531 static void setupCallExpectations(DisplayTransactionTest* test) {
2532 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002533 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002534 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2535 }
2536};
2537
2538struct TransitionOnToDozeSuspendVariant
2539 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE_SUSPEND> {
2540 template <typename Case>
2541 static void setupCallExpectations(DisplayTransactionTest* test) {
2542 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002543 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002544 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2545 }
2546};
2547
2548struct TransitionOnToUnknownVariant
2549 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_LEET> {
2550 template <typename Case>
2551 static void setupCallExpectations(DisplayTransactionTest* test) {
2552 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2553 Case::setupNoComposerPowerModeCallExpectations(test);
2554 }
2555};
2556
2557// --------------------------------------------------------------------
2558// Note:
2559//
2560// Rather than testing the cartesian product of of
2561// DozeIsSupported/DozeNotSupported with all other options, we use one for one
2562// display type, and the other for another display type.
2563// --------------------------------------------------------------------
2564
2565template <typename DisplayVariant, typename DozeVariant, typename EventThreadVariant,
Lloyd Pique41be5d22018-06-21 13:11:48 -07002566 typename DispSyncVariant, typename TransitionVariant>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002567struct DisplayPowerCase {
2568 using Display = DisplayVariant;
2569 using Doze = DozeVariant;
2570 using EventThread = EventThreadVariant;
Lloyd Pique41be5d22018-06-21 13:11:48 -07002571 using DispSync = DispSyncVariant;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002572 using Transition = TransitionVariant;
2573
2574 static auto injectDisplayWithInitialPowerMode(DisplayTransactionTest* test, int mode) {
2575 Display::injectHwcDisplay(test);
2576 auto display = Display::makeFakeExistingDisplayInjector(test);
2577 display.inject();
2578 display.mutableDisplayDevice()->setPowerMode(mode);
2579 return display;
2580 }
2581
2582 static void setInitialPrimaryHWVsyncEnabled(DisplayTransactionTest* test, bool enabled) {
2583 test->mFlinger.mutablePrimaryHWVsyncEnabled() = enabled;
2584 }
2585
2586 static void setupRepaintEverythingCallExpectations(DisplayTransactionTest* test) {
2587 EXPECT_CALL(*test->mMessageQueue, invalidate()).Times(1);
2588 }
2589
2590 static void setupSurfaceInterceptorCallExpectations(DisplayTransactionTest* test, int mode) {
2591 EXPECT_CALL(*test->mSurfaceInterceptor, isEnabled()).WillOnce(Return(true));
2592 EXPECT_CALL(*test->mSurfaceInterceptor, savePowerModeUpdate(_, mode)).Times(1);
2593 }
2594
2595 static void setupComposerCallExpectations(DisplayTransactionTest* test,
2596 IComposerClient::PowerMode mode) {
2597 // Any calls to get the active config will return a default value.
2598 EXPECT_CALL(*test->mComposer, getActiveConfig(Display::HWC_DISPLAY_ID, _))
2599 .WillRepeatedly(DoAll(SetArgPointee<1>(Display::HWC_ACTIVE_CONFIG_ID),
2600 Return(Error::NONE)));
2601
2602 // Any calls to get whether the display supports dozing will return the value set by the
2603 // policy variant.
2604 EXPECT_CALL(*test->mComposer, getDozeSupport(Display::HWC_DISPLAY_ID, _))
2605 .WillRepeatedly(DoAll(SetArgPointee<1>(Doze::DOZE_SUPPORTED), Return(Error::NONE)));
2606
2607 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, mode)).Times(1);
2608 }
2609
2610 static void setupNoComposerPowerModeCallExpectations(DisplayTransactionTest* test) {
2611 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, _)).Times(0);
2612 }
2613};
2614
2615// A sample configuration for the primary display.
2616// In addition to having event thread support, we emulate doze support.
2617template <typename TransitionVariant>
2618using PrimaryDisplayPowerCase = DisplayPowerCase<PrimaryDisplayVariant, DozeIsSupportedVariant,
Lloyd Pique41be5d22018-06-21 13:11:48 -07002619 EventThreadIsSupportedVariant,
2620 DispSyncIsSupportedVariant, TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002621
2622// A sample configuration for the external display.
2623// In addition to not having event thread support, we emulate not having doze
2624// support.
2625template <typename TransitionVariant>
Lloyd Pique41be5d22018-06-21 13:11:48 -07002626using ExternalDisplayPowerCase = DisplayPowerCase<ExternalDisplayVariant, DozeNotSupportedVariant,
2627 EventThreadNotSupportedVariant,
2628 DispSyncNotSupportedVariant, TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002629
2630class SetPowerModeInternalTest : public DisplayTransactionTest {
2631public:
2632 template <typename Case>
2633 void transitionDisplayCommon();
2634};
2635
2636template <int PowerMode>
2637struct PowerModeInitialVSyncEnabled : public std::false_type {};
2638
2639template <>
2640struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_NORMAL> : public std::true_type {};
2641
2642template <>
2643struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_DOZE> : public std::true_type {};
2644
2645template <typename Case>
2646void SetPowerModeInternalTest::transitionDisplayCommon() {
2647 // --------------------------------------------------------------------
2648 // Preconditions
2649
2650 auto display =
2651 Case::injectDisplayWithInitialPowerMode(this, Case::Transition::INITIAL_POWER_MODE);
2652 Case::setInitialPrimaryHWVsyncEnabled(this,
2653 PowerModeInitialVSyncEnabled<
2654 Case::Transition::INITIAL_POWER_MODE>::value);
2655
2656 // --------------------------------------------------------------------
2657 // Call Expectations
2658
2659 Case::setupSurfaceInterceptorCallExpectations(this, Case::Transition::TARGET_POWER_MODE);
2660 Case::Transition::template setupCallExpectations<Case>(this);
2661
2662 // --------------------------------------------------------------------
2663 // Invocation
2664
2665 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(),
2666 Case::Transition::TARGET_POWER_MODE);
2667
2668 // --------------------------------------------------------------------
2669 // Postconditions
2670
2671 Case::Transition::verifyPostconditions(this);
2672}
2673
2674TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfNoChange) {
2675 using Case = SimplePrimaryDisplayCase;
2676
2677 // --------------------------------------------------------------------
2678 // Preconditions
2679
2680 // A primary display device is set up
2681 Case::Display::injectHwcDisplay(this);
2682 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2683 display.inject();
2684
Dominik Laskowskia2edf612018-06-01 13:15:16 -07002685 // The display is already set to HWC_POWER_MODE_NORMAL
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002686 display.mutableDisplayDevice()->setPowerMode(HWC_POWER_MODE_NORMAL);
2687
2688 // --------------------------------------------------------------------
2689 // Invocation
2690
2691 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_NORMAL);
2692
2693 // --------------------------------------------------------------------
2694 // Postconditions
2695
2696 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2697}
2698
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002699TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfVirtualDisplay) {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002700 using Case = HwcVirtualDisplayCase;
2701
2702 // --------------------------------------------------------------------
2703 // Preconditions
2704
2705 // We need to resize this so that the HWC thinks the virtual display
2706 // is something it created.
2707 mFlinger.mutableHwcDisplayData().resize(3);
2708
2709 // A virtual display device is set up
2710 Case::Display::injectHwcDisplay(this);
2711 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2712 display.inject();
2713
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002714 // The display is set to HWC_POWER_MODE_NORMAL
2715 getDisplayDevice(display.token())->setPowerMode(HWC_POWER_MODE_NORMAL);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002716
2717 // --------------------------------------------------------------------
2718 // Invocation
2719
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002720 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_OFF);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002721
2722 // --------------------------------------------------------------------
2723 // Postconditions
2724
2725 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2726}
2727
2728TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnPrimaryDisplay) {
2729 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToOnVariant>>();
2730}
2731
2732TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendPrimaryDisplay) {
2733 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
2734}
2735
2736TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffPrimaryDisplay) {
2737 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToOffVariant>>();
2738}
2739
2740TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffPrimaryDisplay) {
2741 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
2742}
2743
2744TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozePrimaryDisplay) {
2745 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeVariant>>();
2746}
2747
2748TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozePrimaryDisplay) {
2749 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
2750}
2751
2752TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnPrimaryDisplay) {
2753 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeToOnVariant>>();
2754}
2755
2756TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnPrimaryDisplay) {
2757 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
2758}
2759
2760TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendPrimaryDisplay) {
2761 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
2762}
2763
2764TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownPrimaryDisplay) {
2765 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToUnknownVariant>>();
2766}
2767
2768TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnExternalDisplay) {
2769 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToOnVariant>>();
2770}
2771
2772TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendExternalDisplay) {
2773 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
2774}
2775
2776TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffExternalDisplay) {
2777 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToOffVariant>>();
2778}
2779
2780TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffExternalDisplay) {
2781 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
2782}
2783
2784TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeExternalDisplay) {
2785 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeVariant>>();
2786}
2787
2788TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozeExternalDisplay) {
2789 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
2790}
2791
2792TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnExternalDisplay) {
2793 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeToOnVariant>>();
2794}
2795
2796TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnExternalDisplay) {
2797 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
2798}
2799
2800TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendExternalDisplay) {
2801 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
2802}
2803
2804TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownExternalDisplay) {
2805 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToUnknownVariant>>();
2806}
2807
Lloyd Piquef58625d2017-12-19 13:22:33 -08002808} // namespace
2809} // namespace android