blob: b4aec3688996150cc1b0aeae456b0148fd6e5818 [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,
tangrobin6753a022018-08-10 10:58:54 +0800660 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>,
661 NoPerFrameMetadataSupportVariant<SimpleHwcVirtualDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800662using WideColorP3ColorimetricDisplayCase =
663 Case<PrimaryDisplayVariant, WideColorP3ColorimetricSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700664 HdrNotSupportedVariant<PrimaryDisplayVariant>,
665 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800666using Hdr10DisplayCase =
667 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700668 Hdr10SupportedVariant<PrimaryDisplayVariant>,
669 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800670using HdrHlgDisplayCase =
671 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700672 HdrHlgSupportedVariant<PrimaryDisplayVariant>,
673 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800674using HdrDolbyVisionDisplayCase =
675 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700676 HdrDolbyVisionSupportedVariant<PrimaryDisplayVariant>,
677 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
678using HdrSmpte2086DisplayCase =
679 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
680 HdrNotSupportedVariant<PrimaryDisplayVariant>,
681 Smpte2086PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
682using HdrCta861_3_DisplayCase =
683 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
684 HdrNotSupportedVariant<PrimaryDisplayVariant>,
685 Cta861_3_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Dominik Laskowskif07b85b2018-06-11 12:49:15 -0700686
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800687/* ------------------------------------------------------------------------
Lloyd Pique6cf11032018-01-22 18:57:44 -0800688 *
689 * SurfaceFlinger::onHotplugReceived
690 */
691
692TEST_F(DisplayTransactionTest, hotplugEnqueuesEventsForDisplayTransaction) {
693 constexpr int currentSequenceId = 123;
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700694 constexpr hwc2_display_t hwcDisplayId1 = 456;
695 constexpr hwc2_display_t hwcDisplayId2 = 654;
Lloyd Pique6cf11032018-01-22 18:57:44 -0800696
697 // --------------------------------------------------------------------
698 // Preconditions
699
700 // Set the current sequence id for accepted events
701 mFlinger.mutableComposerSequenceId() = currentSequenceId;
702
703 // Set the main thread id so that the current thread does not appear to be
704 // the main thread.
705 mFlinger.mutableMainThreadId() = std::thread::id();
706
707 // --------------------------------------------------------------------
708 // Call Expectations
709
710 // We expect invalidate() to be invoked once to trigger display transaction
711 // processing.
712 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
713
714 // --------------------------------------------------------------------
715 // Invocation
716
717 // Simulate two hotplug events (a connect and a disconnect)
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700718 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId1, HWC2::Connection::Connected);
719 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId2, HWC2::Connection::Disconnected);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800720
721 // --------------------------------------------------------------------
722 // Postconditions
723
724 // The display transaction needed flag should be set.
725 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
726
727 // All events should be in the pending event queue.
728 const auto& pendingEvents = mFlinger.mutablePendingHotplugEvents();
729 ASSERT_EQ(2u, pendingEvents.size());
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700730 EXPECT_EQ(hwcDisplayId1, pendingEvents[0].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800731 EXPECT_EQ(HWC2::Connection::Connected, pendingEvents[0].connection);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700732 EXPECT_EQ(hwcDisplayId2, pendingEvents[1].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800733 EXPECT_EQ(HWC2::Connection::Disconnected, pendingEvents[1].connection);
734}
735
736TEST_F(DisplayTransactionTest, hotplugDiscardsUnexpectedEvents) {
737 constexpr int currentSequenceId = 123;
738 constexpr int otherSequenceId = 321;
739 constexpr hwc2_display_t displayId = 456;
740
741 // --------------------------------------------------------------------
742 // Preconditions
743
744 // Set the current sequence id for accepted events
745 mFlinger.mutableComposerSequenceId() = currentSequenceId;
746
747 // Set the main thread id so that the current thread does not appear to be
748 // the main thread.
749 mFlinger.mutableMainThreadId() = std::thread::id();
750
751 // --------------------------------------------------------------------
752 // Call Expectations
753
754 // We do not expect any calls to invalidate().
755 EXPECT_CALL(*mMessageQueue, invalidate()).Times(0);
756
757 // --------------------------------------------------------------------
758 // Invocation
759
760 // Call with an unexpected sequence id
761 mFlinger.onHotplugReceived(otherSequenceId, displayId, HWC2::Connection::Invalid);
762
763 // --------------------------------------------------------------------
764 // Postconditions
765
766 // The display transaction needed flag should not be set
767 EXPECT_FALSE(hasTransactionFlagSet(eDisplayTransactionNeeded));
768
769 // There should be no pending events
770 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
771}
772
773TEST_F(DisplayTransactionTest, hotplugProcessesEnqueuedEventsIfCalledOnMainThread) {
774 constexpr int currentSequenceId = 123;
775 constexpr hwc2_display_t displayId1 = 456;
776
777 // --------------------------------------------------------------------
778 // Note:
779 // --------------------------------------------------------------------
780 // This test case is a bit tricky. We want to verify that
781 // onHotplugReceived() calls processDisplayHotplugEventsLocked(), but we
782 // don't really want to provide coverage for everything the later function
783 // does as there are specific tests for it.
784 // --------------------------------------------------------------------
785
786 // --------------------------------------------------------------------
787 // Preconditions
788
789 // Set the current sequence id for accepted events
790 mFlinger.mutableComposerSequenceId() = currentSequenceId;
791
792 // Set the main thread id so that the current thread does appear to be the
793 // main thread.
794 mFlinger.mutableMainThreadId() = std::this_thread::get_id();
795
796 // --------------------------------------------------------------------
797 // Call Expectations
798
799 // We expect invalidate() to be invoked once to trigger display transaction
800 // processing.
801 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
802
803 // --------------------------------------------------------------------
804 // Invocation
805
806 // Simulate a disconnect on a display id that is not connected. This should
807 // be enqueued by onHotplugReceived(), and dequeued by
808 // processDisplayHotplugEventsLocked(), but then ignored as invalid.
809 mFlinger.onHotplugReceived(currentSequenceId, displayId1, HWC2::Connection::Disconnected);
810
811 // --------------------------------------------------------------------
812 // Postconditions
813
814 // The display transaction needed flag should be set.
815 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
816
817 // There should be no event queued on return, as it should have been
818 // processed.
819 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
820}
821
822/* ------------------------------------------------------------------------
Lloyd Piquea482f992018-01-22 19:00:34 -0800823 * SurfaceFlinger::createDisplay
824 */
825
826TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForNonsecureDisplay) {
827 const String8 name("virtual.test");
828
829 // --------------------------------------------------------------------
830 // Call Expectations
831
832 // The call should notify the interceptor that a display was created.
833 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
834
835 // --------------------------------------------------------------------
836 // Invocation
837
838 sp<IBinder> displayToken = mFlinger.createDisplay(name, false);
839
840 // --------------------------------------------------------------------
841 // Postconditions
842
843 // The display should have been added to the current state
844 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
845 const auto& display = getCurrentDisplayState(displayToken);
846 EXPECT_EQ(DisplayDevice::DISPLAY_VIRTUAL, display.type);
847 EXPECT_EQ(false, display.isSecure);
848 EXPECT_EQ(name.string(), display.displayName);
849
850 // --------------------------------------------------------------------
851 // Cleanup conditions
852
853 // Destroying the display invalidates the display state.
854 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
855}
856
857TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForSecureDisplay) {
858 const String8 name("virtual.test");
859
860 // --------------------------------------------------------------------
861 // Call Expectations
862
863 // The call should notify the interceptor that a display was created.
864 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
865
866 // --------------------------------------------------------------------
867 // Invocation
868
869 sp<IBinder> displayToken = mFlinger.createDisplay(name, true);
870
871 // --------------------------------------------------------------------
872 // Postconditions
873
874 // The display should have been added to the current state
875 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
876 const auto& display = getCurrentDisplayState(displayToken);
877 EXPECT_EQ(DisplayDevice::DISPLAY_VIRTUAL, display.type);
878 EXPECT_EQ(true, display.isSecure);
879 EXPECT_EQ(name.string(), display.displayName);
880
881 // --------------------------------------------------------------------
882 // Cleanup conditions
883
884 // Destroying the display invalidates the display state.
885 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
886}
887
888/* ------------------------------------------------------------------------
889 * SurfaceFlinger::destroyDisplay
890 */
891
892TEST_F(DisplayTransactionTest, destroyDisplayClearsCurrentStateForDisplay) {
893 using Case = NonHwcVirtualDisplayCase;
894
895 // --------------------------------------------------------------------
896 // Preconditions
897
898 // A virtual display exists
899 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
900 existing.inject();
901
902 // --------------------------------------------------------------------
903 // Call Expectations
904
905 // The call should notify the interceptor that a display was created.
906 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
907
908 // Destroying the display invalidates the display state.
909 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
910
911 // --------------------------------------------------------------------
912 // Invocation
913
914 mFlinger.destroyDisplay(existing.token());
915
916 // --------------------------------------------------------------------
917 // Postconditions
918
919 // The display should have been removed from the current state
920 EXPECT_FALSE(hasCurrentDisplayState(existing.token()));
921
922 // Ths display should still exist in the drawing state
923 EXPECT_TRUE(hasDrawingDisplayState(existing.token()));
924
925 // The display transaction needed flasg should be set
926 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
927}
928
929TEST_F(DisplayTransactionTest, destroyDisplayHandlesUnknownDisplay) {
930 // --------------------------------------------------------------------
931 // Preconditions
932
933 sp<BBinder> displayToken = new BBinder();
934
935 // --------------------------------------------------------------------
936 // Invocation
937
938 mFlinger.destroyDisplay(displayToken);
939}
940
941/* ------------------------------------------------------------------------
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -0800942 * SurfaceFlinger::resetDisplayState
943 */
944
945TEST_F(DisplayTransactionTest, resetDisplayStateClearsState) {
946 using Case = NonHwcVirtualDisplayCase;
947
948 // --------------------------------------------------------------------
949 // Preconditions
950
951 // vsync is enabled and available
952 mFlinger.mutablePrimaryHWVsyncEnabled() = true;
953 mFlinger.mutableHWVsyncAvailable() = true;
954
955 // A display exists
956 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
957 existing.inject();
958
959 // --------------------------------------------------------------------
960 // Call Expectations
961
962 // The call disable vsyncs
963 EXPECT_CALL(*mEventControlThread, setVsyncEnabled(false)).Times(1);
964
965 // The call clears the current render engine surface
966 EXPECT_CALL(*mRenderEngine, resetCurrentSurface());
967
Lloyd Pique41be5d22018-06-21 13:11:48 -0700968 // The call ends any display resyncs
969 EXPECT_CALL(*mPrimaryDispSync, endResync()).Times(1);
970
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -0800971 // --------------------------------------------------------------------
972 // Invocation
973
974 mFlinger.resetDisplayState();
975
976 // --------------------------------------------------------------------
977 // Postconditions
978
979 // vsyncs should be off and not available.
980 EXPECT_FALSE(mFlinger.mutablePrimaryHWVsyncEnabled());
981 EXPECT_FALSE(mFlinger.mutableHWVsyncAvailable());
982
983 // The display should have been removed from the display map.
984 EXPECT_FALSE(hasDisplayDevice(existing.token()));
985
986 // The display should still exist in the current state
987 EXPECT_TRUE(hasCurrentDisplayState(existing.token()));
988
989 // The display should have been removed from the drawing state
990 EXPECT_FALSE(hasDrawingDisplayState(existing.token()));
991}
992
993/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800994 * SurfaceFlinger::setupNewDisplayDeviceInternal
995 */
996
997class SetupNewDisplayDeviceInternalTest : public DisplayTransactionTest {
998public:
999 template <typename T>
1000 void setupNewDisplayDeviceInternalTest();
1001};
1002
1003template <typename Case>
1004void SetupNewDisplayDeviceInternalTest::setupNewDisplayDeviceInternalTest() {
1005 const sp<BBinder> displayToken = new BBinder();
1006 const sp<mock::DisplaySurface> displaySurface = new mock::DisplaySurface();
1007 const sp<mock::GraphicBufferProducer> producer = new mock::GraphicBufferProducer();
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001008
1009 // --------------------------------------------------------------------
1010 // Preconditions
1011
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001012 // Wide color displays support is configured appropriately
1013 Case::WideColorSupport::injectConfigChange(this);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001014
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001015 // The display is setup with the HWC.
1016 Case::Display::injectHwcDisplay(this);
1017
1018 // SurfaceFlinger will use a test-controlled factory for native window
1019 // surfaces.
1020 injectFakeNativeWindowSurfaceFactory();
1021
1022 // --------------------------------------------------------------------
1023 // Call Expectations
1024
1025 // Various native window calls will be made.
1026 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
Lloyd Pique3c085a02018-05-09 19:38:32 -07001027 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001028 Case::WideColorSupport::setupComposerCallExpectations(this);
1029 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001030 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001031
1032 // --------------------------------------------------------------------
1033 // Invocation
1034
1035 DisplayDeviceState state;
1036 state.type = Case::Display::TYPE;
1037 state.isSecure = static_cast<bool>(Case::Display::SECURE);
1038
1039 auto device = mFlinger.setupNewDisplayDeviceInternal(displayToken, Case::Display::TYPE, state,
1040 displaySurface, producer);
1041
1042 // --------------------------------------------------------------------
1043 // Postconditions
1044
1045 ASSERT_TRUE(device != nullptr);
1046 EXPECT_EQ(Case::Display::TYPE, device->getDisplayType());
1047 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
1048 EXPECT_EQ(Case::Display::WIDTH, device->getWidth());
1049 EXPECT_EQ(Case::Display::HEIGHT, device->getHeight());
1050 EXPECT_EQ(Case::WideColorSupport::WIDE_COLOR_SUPPORTED, device->hasWideColorGamut());
1051 EXPECT_EQ(Case::HdrSupport::HDR10_SUPPORTED, device->hasHDR10Support());
1052 EXPECT_EQ(Case::HdrSupport::HDR_HLG_SUPPORTED, device->hasHLGSupport());
1053 EXPECT_EQ(Case::HdrSupport::HDR_DOLBY_VISION_SUPPORTED, device->hasDolbyVisionSupport());
Lloyd Pique3c085a02018-05-09 19:38:32 -07001054 // Note: This is not Case::Display::HWC_ACTIVE_CONFIG_ID as the ids are
1055 // remapped, and the test only ever sets up one config. If there were an error
1056 // looking up the remapped index, device->getActiveConfig() would be -1 instead.
1057 EXPECT_EQ(0, device->getActiveConfig());
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001058 EXPECT_EQ(Case::PerFrameMetadataSupport::PER_FRAME_METADATA_KEYS,
1059 device->getSupportedPerFrameMetadata());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001060}
1061
1062TEST_F(SetupNewDisplayDeviceInternalTest, createSimplePrimaryDisplay) {
1063 setupNewDisplayDeviceInternalTest<SimplePrimaryDisplayCase>();
1064}
1065
1066TEST_F(SetupNewDisplayDeviceInternalTest, createSimpleExternalDisplay) {
1067 setupNewDisplayDeviceInternalTest<SimpleExternalDisplayCase>();
1068}
1069
1070TEST_F(SetupNewDisplayDeviceInternalTest, createNonHwcVirtualDisplay) {
1071 setupNewDisplayDeviceInternalTest<NonHwcVirtualDisplayCase>();
1072}
1073
1074TEST_F(SetupNewDisplayDeviceInternalTest, createHwcVirtualDisplay) {
1075 // We need to resize this so that the HWC thinks the virtual display
1076 // is something it created.
1077 mFlinger.mutableHwcDisplayData().resize(3);
1078
1079 setupNewDisplayDeviceInternalTest<HwcVirtualDisplayCase>();
1080}
1081
1082TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3Display) {
1083 setupNewDisplayDeviceInternalTest<WideColorP3ColorimetricDisplayCase>();
1084}
1085
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001086TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10Display) {
1087 setupNewDisplayDeviceInternalTest<Hdr10DisplayCase>();
1088}
1089
1090TEST_F(SetupNewDisplayDeviceInternalTest, createHdrHlgDisplay) {
1091 setupNewDisplayDeviceInternalTest<HdrHlgDisplayCase>();
1092}
1093
1094TEST_F(SetupNewDisplayDeviceInternalTest, createHdrDolbyVisionDisplay) {
1095 setupNewDisplayDeviceInternalTest<HdrDolbyVisionDisplayCase>();
1096}
1097
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001098TEST_F(SetupNewDisplayDeviceInternalTest, createHdrSmpte2086DisplayCase) {
1099 setupNewDisplayDeviceInternalTest<HdrSmpte2086DisplayCase>();
1100}
1101
1102TEST_F(SetupNewDisplayDeviceInternalTest, createHdrCta816_3_DisplayCase) {
1103 setupNewDisplayDeviceInternalTest<HdrCta861_3_DisplayCase>();
1104}
1105
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001106/* ------------------------------------------------------------------------
1107 * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded)
1108 */
1109
1110class HandleTransactionLockedTest : public DisplayTransactionTest {
1111public:
1112 template <typename Case>
1113 void setupCommonPreconditions();
1114
1115 template <typename Case>
1116 void setupCommonCallExpectationsForConnectProcessing();
1117
1118 template <typename Case>
1119 void setupCommonCallExpectationsForDisconnectProcessing();
1120
1121 template <typename Case>
1122 void processesHotplugConnectCommon();
1123
1124 template <typename Case>
1125 void ignoresHotplugConnectCommon();
1126
1127 template <typename Case>
1128 void processesHotplugDisconnectCommon();
1129
1130 template <typename Case>
1131 void verifyDisplayIsConnected(const sp<IBinder>& displayToken);
1132
1133 template <typename Case>
1134 void verifyPhysicalDisplayIsConnected();
1135
1136 void verifyDisplayIsNotConnected(const sp<IBinder>& displayToken);
1137};
1138
1139template <typename Case>
1140void HandleTransactionLockedTest::setupCommonPreconditions() {
1141 // Wide color displays support is configured appropriately
1142 Case::WideColorSupport::injectConfigChange(this);
1143
1144 // SurfaceFlinger will use a test-controlled factory for BufferQueues
1145 injectFakeBufferQueueFactory();
1146
1147 // SurfaceFlinger will use a test-controlled factory for native window
1148 // surfaces.
1149 injectFakeNativeWindowSurfaceFactory();
1150}
1151
1152template <typename Case>
1153void HandleTransactionLockedTest::setupCommonCallExpectationsForConnectProcessing() {
1154 Case::Display::setupHwcHotplugCallExpectations(this);
1155
1156 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1157 Case::Display::setupFramebufferProducerBufferQueueCallExpectations(this);
1158 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1159 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
1160
1161 Case::WideColorSupport::setupComposerCallExpectations(this);
1162 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001163 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001164
1165 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001166 EXPECT_CALL(*mEventThread,
1167 onHotplugReceived(Case::Display::TYPE == DisplayDevice::DISPLAY_PRIMARY
1168 ? EventThread::DisplayType::Primary
1169 : EventThread::DisplayType::External,
1170 true))
1171 .Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001172}
1173
1174template <typename Case>
1175void HandleTransactionLockedTest::setupCommonCallExpectationsForDisconnectProcessing() {
1176 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001177 EXPECT_CALL(*mEventThread,
1178 onHotplugReceived(Case::Display::TYPE == DisplayDevice::DISPLAY_PRIMARY
1179 ? EventThread::DisplayType::Primary
1180 : EventThread::DisplayType::External,
1181 false))
1182 .Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001183}
1184
1185template <typename Case>
1186void HandleTransactionLockedTest::verifyDisplayIsConnected(const sp<IBinder>& displayToken) {
1187 // The display device should have been set up in the list of displays.
1188 ASSERT_TRUE(hasDisplayDevice(displayToken));
1189 const auto& device = getDisplayDevice(displayToken);
1190 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
1191 EXPECT_EQ(Case::Display::TYPE == DisplayDevice::DISPLAY_PRIMARY, device->isPrimary());
1192
1193 // The display should have been set up in the current display state
1194 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1195 const auto& current = getCurrentDisplayState(displayToken);
1196 EXPECT_EQ(Case::Display::TYPE, current.type);
1197
1198 // The display should have been set up in the drawing display state
1199 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1200 const auto& draw = getDrawingDisplayState(displayToken);
1201 EXPECT_EQ(Case::Display::TYPE, draw.type);
1202}
1203
1204template <typename Case>
1205void HandleTransactionLockedTest::verifyPhysicalDisplayIsConnected() {
1206 // HWComposer should have an entry for the display
1207 EXPECT_TRUE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1208
1209 // The display should be set up as a built-in display.
1210 static_assert(0 <= Case::Display::TYPE &&
1211 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES,
1212 "Must use a valid physical display type index for the fixed-size array");
Dominik Laskowskieecd6592018-05-29 10:25:41 -07001213 auto& displayToken = mFlinger.mutableDisplayTokens()[Case::Display::TYPE];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001214 ASSERT_TRUE(displayToken != nullptr);
1215
1216 verifyDisplayIsConnected<Case>(displayToken);
1217}
1218
1219void HandleTransactionLockedTest::verifyDisplayIsNotConnected(const sp<IBinder>& displayToken) {
1220 EXPECT_FALSE(hasDisplayDevice(displayToken));
1221 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1222 EXPECT_FALSE(hasDrawingDisplayState(displayToken));
1223}
1224
1225template <typename Case>
1226void HandleTransactionLockedTest::processesHotplugConnectCommon() {
1227 // --------------------------------------------------------------------
1228 // Preconditions
1229
1230 setupCommonPreconditions<Case>();
1231
1232 // A hotplug connect event is enqueued for a display
1233 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001234
1235 // --------------------------------------------------------------------
1236 // Call Expectations
1237
1238 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillOnce(Return(false));
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001239
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001240 setupCommonCallExpectationsForConnectProcessing<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001241
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001242 // --------------------------------------------------------------------
1243 // Invocation
Lloyd Piquee39cad22017-12-20 17:01:29 -08001244
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001245 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
Lloyd Piquee39cad22017-12-20 17:01:29 -08001246
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001247 // --------------------------------------------------------------------
1248 // Postconditions
1249
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001250 verifyPhysicalDisplayIsConnected<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001251
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001252 // --------------------------------------------------------------------
1253 // Cleanup conditions
1254
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001255 EXPECT_CALL(*mComposer,
1256 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001257 .WillOnce(Return(Error::NONE));
1258 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Piquef58625d2017-12-19 13:22:33 -08001259}
1260
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001261template <typename Case>
1262void HandleTransactionLockedTest::ignoresHotplugConnectCommon() {
1263 // --------------------------------------------------------------------
1264 // Preconditions
1265
1266 setupCommonPreconditions<Case>();
1267
1268 // A hotplug connect event is enqueued for a display
1269 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1270
1271 // --------------------------------------------------------------------
1272 // Invocation
1273
1274 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1275
1276 // --------------------------------------------------------------------
1277 // Postconditions
1278
1279 // HWComposer should not have an entry for the display
1280 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1281}
1282
1283template <typename Case>
1284void HandleTransactionLockedTest::processesHotplugDisconnectCommon() {
1285 // --------------------------------------------------------------------
1286 // Preconditions
1287
1288 setupCommonPreconditions<Case>();
1289
1290 // A hotplug disconnect event is enqueued for a display
1291 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1292
1293 // The display is already completely set up.
1294 Case::Display::injectHwcDisplay(this);
1295 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1296 existing.inject();
1297
1298 // --------------------------------------------------------------------
1299 // Call Expectations
1300
1301 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1302
1303 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1304
1305 // --------------------------------------------------------------------
1306 // Invocation
1307
1308 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1309
1310 // --------------------------------------------------------------------
1311 // Postconditions
1312
1313 // HWComposer should not have an entry for the display
1314 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1315
1316 // The display should not be set up as a built-in display.
1317 ASSERT_TRUE(0 <= Case::Display::TYPE &&
1318 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES);
Dominik Laskowskieecd6592018-05-29 10:25:41 -07001319 auto displayToken = mFlinger.mutableDisplayTokens()[Case::Display::TYPE];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001320 EXPECT_TRUE(displayToken == nullptr);
1321
1322 // The existing token should have been removed
1323 verifyDisplayIsNotConnected(existing.token());
1324}
1325
1326TEST_F(HandleTransactionLockedTest, processesHotplugConnectPrimaryDisplay) {
1327 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1328}
1329
1330TEST_F(HandleTransactionLockedTest,
1331 processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected) {
1332 // Inject an external display.
1333 ExternalDisplayVariant::injectHwcDisplay(this);
1334
1335 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1336}
1337
1338TEST_F(HandleTransactionLockedTest, processesHotplugConnectExternalDisplay) {
1339 // Inject a primary display.
1340 PrimaryDisplayVariant::injectHwcDisplay(this);
1341
1342 processesHotplugConnectCommon<SimpleExternalDisplayCase>();
1343}
1344
1345TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected) {
1346 // Inject both a primary and external display.
1347 PrimaryDisplayVariant::injectHwcDisplay(this);
1348 ExternalDisplayVariant::injectHwcDisplay(this);
1349
1350 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1351
1352 ignoresHotplugConnectCommon<SimpleTertiaryDisplayCase>();
1353}
1354
1355TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfExternalForVrComposer) {
1356 // Inject a primary display.
1357 PrimaryDisplayVariant::injectHwcDisplay(this);
1358
1359 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(true));
1360
1361 ignoresHotplugConnectCommon<SimpleExternalDisplayCase>();
1362}
1363
1364TEST_F(HandleTransactionLockedTest, processHotplugDisconnectPrimaryDisplay) {
1365 processesHotplugDisconnectCommon<SimplePrimaryDisplayCase>();
1366}
1367
1368TEST_F(HandleTransactionLockedTest, processHotplugDisconnectExternalDisplay) {
1369 processesHotplugDisconnectCommon<SimpleExternalDisplayCase>();
1370}
1371
1372TEST_F(HandleTransactionLockedTest, processesHotplugConnectThenDisconnectPrimary) {
1373 using Case = SimplePrimaryDisplayCase;
1374
1375 // --------------------------------------------------------------------
1376 // Preconditions
1377
1378 setupCommonPreconditions<Case>();
1379
1380 // A hotplug connect event is enqueued for a display
1381 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1382 // A hotplug disconnect event is also enqueued for the same display
1383 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1384
1385 // --------------------------------------------------------------------
1386 // Call Expectations
1387
1388 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1389
1390 setupCommonCallExpectationsForConnectProcessing<Case>();
1391 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1392
1393 EXPECT_CALL(*mComposer,
1394 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1395 .WillOnce(Return(Error::NONE));
1396 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1397
1398 // --------------------------------------------------------------------
1399 // Invocation
1400
1401 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1402
1403 // --------------------------------------------------------------------
1404 // Postconditions
1405
1406 // HWComposer should not have an entry for the display
1407 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1408
1409 // The display should not be set up as a primary built-in display.
1410 ASSERT_TRUE(0 <= Case::Display::TYPE &&
1411 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES);
Dominik Laskowskieecd6592018-05-29 10:25:41 -07001412 auto displayToken = mFlinger.mutableDisplayTokens()[Case::Display::TYPE];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001413 EXPECT_TRUE(displayToken == nullptr);
1414}
1415
1416TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectThenConnectPrimary) {
1417 using Case = SimplePrimaryDisplayCase;
1418
1419 // --------------------------------------------------------------------
1420 // Preconditions
1421
1422 setupCommonPreconditions<Case>();
1423
1424 // The display is already completely set up.
1425 Case::Display::injectHwcDisplay(this);
1426 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1427 existing.inject();
1428
1429 // A hotplug disconnect event is enqueued for a display
1430 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1431 // A hotplug connect event is also enqueued for the same display
1432 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1433
1434 // --------------------------------------------------------------------
1435 // Call Expectations
1436
1437 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1438
1439 setupCommonCallExpectationsForConnectProcessing<Case>();
1440 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1441
1442 // --------------------------------------------------------------------
1443 // Invocation
1444
1445 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1446
1447 // --------------------------------------------------------------------
1448 // Postconditions
1449
1450 // The existing token should have been removed
1451 verifyDisplayIsNotConnected(existing.token());
1452 static_assert(0 <= Case::Display::TYPE &&
1453 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES,
1454 "Display type must be a built-in display");
Dominik Laskowskieecd6592018-05-29 10:25:41 -07001455 EXPECT_NE(existing.token(), mFlinger.mutableDisplayTokens()[Case::Display::TYPE]);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001456
1457 // A new display should be connected in its place
1458
1459 verifyPhysicalDisplayIsConnected<Case>();
1460
1461 // --------------------------------------------------------------------
1462 // Cleanup conditions
1463
1464 EXPECT_CALL(*mComposer,
1465 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1466 .WillOnce(Return(Error::NONE));
1467 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1468}
1469
1470TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAdded) {
1471 using Case = HwcVirtualDisplayCase;
1472
1473 // --------------------------------------------------------------------
1474 // Preconditions
1475
1476 // The HWC supports at least one virtual display
1477 injectMockComposer(1);
1478
1479 setupCommonPreconditions<Case>();
1480
1481 // A virtual display was added to the current state, and it has a
1482 // surface(producer)
1483 sp<BBinder> displayToken = new BBinder();
Lloyd Pique4c2ac022018-04-27 12:08:03 -07001484
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001485 DisplayDeviceState info;
1486 info.type = Case::Display::TYPE;
1487 info.isSecure = static_cast<bool>(Case::Display::SECURE);
1488
1489 sp<mock::GraphicBufferProducer> surface{new mock::GraphicBufferProducer()};
1490 info.surface = surface;
1491 mFlinger.mutableCurrentState().displays.add(displayToken, info);
1492
1493 // --------------------------------------------------------------------
1494 // Call Expectations
1495
1496 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1497 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1498
1499 EXPECT_CALL(*surface, query(NATIVE_WINDOW_WIDTH, _))
1500 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::WIDTH), Return(NO_ERROR)));
1501 EXPECT_CALL(*surface, query(NATIVE_WINDOW_HEIGHT, _))
1502 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::HEIGHT), Return(NO_ERROR)));
1503 EXPECT_CALL(*surface, query(NATIVE_WINDOW_FORMAT, _))
1504 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT),
1505 Return(NO_ERROR)));
1506 EXPECT_CALL(*surface, query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, _))
1507 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(NO_ERROR)));
1508
1509 EXPECT_CALL(*surface, setAsyncMode(true)).Times(1);
1510
1511 EXPECT_CALL(*mProducer, connect(_, _, _, _)).Times(1);
1512 EXPECT_CALL(*mProducer, disconnect(_, _)).Times(1);
1513
1514 Case::Display::setupHwcVirtualDisplayCreationCallExpectations(this);
1515 Case::WideColorSupport::setupComposerCallExpectations(this);
1516 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001517 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001518
1519 // --------------------------------------------------------------------
1520 // Invocation
1521
1522 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1523
1524 // --------------------------------------------------------------------
1525 // Postconditions
1526
1527 // The display device should have been set up in the list of displays.
1528 verifyDisplayIsConnected<Case>(displayToken);
1529
1530 // --------------------------------------------------------------------
1531 // Cleanup conditions
1532
1533 EXPECT_CALL(*mComposer, destroyVirtualDisplay(Case::Display::HWC_DISPLAY_ID))
1534 .WillOnce(Return(Error::NONE));
1535 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1536}
1537
1538TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAddedWithNoSurface) {
1539 using Case = HwcVirtualDisplayCase;
1540
1541 // --------------------------------------------------------------------
1542 // Preconditions
1543
1544 // The HWC supports at least one virtual display
1545 injectMockComposer(1);
1546
1547 setupCommonPreconditions<Case>();
1548
1549 // A virtual display was added to the current state, but it does not have a
1550 // surface.
1551 sp<BBinder> displayToken = new BBinder();
1552
1553 DisplayDeviceState info;
1554 info.type = Case::Display::TYPE;
Chia-I Wu0f509fb2018-06-21 15:52:50 +08001555 info.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001556
1557 mFlinger.mutableCurrentState().displays.add(displayToken, info);
1558
1559 // --------------------------------------------------------------------
1560 // Call Expectations
1561
1562 // --------------------------------------------------------------------
1563 // Invocation
1564
1565 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1566
1567 // --------------------------------------------------------------------
1568 // Postconditions
1569
1570 // There will not be a display device set up.
1571 EXPECT_FALSE(hasDisplayDevice(displayToken));
1572
1573 // The drawing display state will be set from the current display state.
1574 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1575 const auto& draw = getDrawingDisplayState(displayToken);
1576 EXPECT_EQ(Case::Display::TYPE, draw.type);
1577}
1578
1579TEST_F(HandleTransactionLockedTest, processesVirtualDisplayRemoval) {
1580 using Case = HwcVirtualDisplayCase;
1581
1582 // --------------------------------------------------------------------
1583 // Preconditions
1584
1585 // A virtual display is set up but is removed from the current state.
1586 mFlinger.mutableHwcDisplayData().resize(3);
1587 Case::Display::injectHwcDisplay(this);
1588 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1589 existing.inject();
1590 mFlinger.mutableCurrentState().displays.removeItem(existing.token());
1591
1592 // --------------------------------------------------------------------
1593 // Call Expectations
1594
1595 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1596
1597 // --------------------------------------------------------------------
1598 // Invocation
1599
1600 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1601
1602 // --------------------------------------------------------------------
1603 // Postconditions
1604
1605 // The existing token should have been removed
1606 verifyDisplayIsNotConnected(existing.token());
1607}
1608
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001609TEST_F(HandleTransactionLockedTest, processesDisplayLayerStackChanges) {
1610 using Case = NonHwcVirtualDisplayCase;
1611
1612 constexpr uint32_t oldLayerStack = 0u;
1613 constexpr uint32_t newLayerStack = 123u;
1614
1615 // --------------------------------------------------------------------
1616 // Preconditions
1617
1618 // A display is set up
1619 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1620 display.inject();
1621
1622 // There is a change to the layerStack state
1623 display.mutableDrawingDisplayState().layerStack = oldLayerStack;
1624 display.mutableCurrentDisplayState().layerStack = newLayerStack;
1625
1626 // --------------------------------------------------------------------
1627 // Invocation
1628
1629 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1630
1631 // --------------------------------------------------------------------
1632 // Postconditions
1633
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001634 EXPECT_EQ(newLayerStack, display.mutableDisplayDevice()->getLayerStack());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001635}
1636
1637TEST_F(HandleTransactionLockedTest, processesDisplayTransformChanges) {
1638 using Case = NonHwcVirtualDisplayCase;
1639
1640 constexpr int oldTransform = 0;
1641 constexpr int newTransform = 2;
1642
1643 // --------------------------------------------------------------------
1644 // Preconditions
1645
1646 // A display is set up
1647 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1648 display.inject();
1649
1650 // There is a change to the orientation state
1651 display.mutableDrawingDisplayState().orientation = oldTransform;
1652 display.mutableCurrentDisplayState().orientation = newTransform;
1653
1654 // --------------------------------------------------------------------
1655 // Invocation
1656
1657 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1658
1659 // --------------------------------------------------------------------
1660 // Postconditions
1661
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001662 EXPECT_EQ(newTransform, display.mutableDisplayDevice()->getOrientation());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001663}
1664
1665TEST_F(HandleTransactionLockedTest, processesDisplayViewportChanges) {
1666 using Case = NonHwcVirtualDisplayCase;
1667
1668 const Rect oldViewport(0, 0, 0, 0);
1669 const Rect newViewport(0, 0, 123, 456);
1670
1671 // --------------------------------------------------------------------
1672 // Preconditions
1673
1674 // A display is set up
1675 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1676 display.inject();
1677
1678 // There is a change to the viewport state
1679 display.mutableDrawingDisplayState().viewport = oldViewport;
1680 display.mutableCurrentDisplayState().viewport = newViewport;
1681
1682 // --------------------------------------------------------------------
1683 // Invocation
1684
1685 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1686
1687 // --------------------------------------------------------------------
1688 // Postconditions
1689
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001690 EXPECT_EQ(newViewport, display.mutableDisplayDevice()->getViewport());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001691}
1692
1693TEST_F(HandleTransactionLockedTest, processesDisplayFrameChanges) {
1694 using Case = NonHwcVirtualDisplayCase;
1695
1696 const Rect oldFrame(0, 0, 0, 0);
1697 const Rect newFrame(0, 0, 123, 456);
1698
1699 // --------------------------------------------------------------------
1700 // Preconditions
1701
1702 // A display is set up
1703 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1704 display.inject();
1705
1706 // There is a change to the viewport state
1707 display.mutableDrawingDisplayState().frame = oldFrame;
1708 display.mutableCurrentDisplayState().frame = newFrame;
1709
1710 // --------------------------------------------------------------------
1711 // Invocation
1712
1713 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1714
1715 // --------------------------------------------------------------------
1716 // Postconditions
1717
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001718 EXPECT_EQ(newFrame, display.mutableDisplayDevice()->getFrame());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001719}
1720
1721TEST_F(HandleTransactionLockedTest, processesDisplayWidthChanges) {
1722 using Case = NonHwcVirtualDisplayCase;
1723
1724 constexpr int oldWidth = 0;
1725 constexpr int oldHeight = 10;
1726 constexpr int newWidth = 123;
1727
1728 // --------------------------------------------------------------------
1729 // Preconditions
1730
1731 // A display is set up
1732 auto nativeWindow = new mock::NativeWindow();
1733 auto displaySurface = new mock::DisplaySurface();
1734 auto renderSurface = new RE::mock::Surface();
1735 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1736 display.setNativeWindow(nativeWindow);
1737 display.setDisplaySurface(displaySurface);
1738 display.setRenderSurface(std::unique_ptr<RE::Surface>(renderSurface));
1739 display.inject();
1740
1741 // There is a change to the viewport state
1742 display.mutableDrawingDisplayState().width = oldWidth;
1743 display.mutableDrawingDisplayState().height = oldHeight;
1744 display.mutableCurrentDisplayState().width = newWidth;
1745 display.mutableCurrentDisplayState().height = oldHeight;
1746
1747 // --------------------------------------------------------------------
1748 // Call Expectations
1749
1750 EXPECT_CALL(*renderSurface, setNativeWindow(nullptr)).Times(1);
1751 EXPECT_CALL(*displaySurface, resizeBuffers(newWidth, oldHeight)).Times(1);
1752 EXPECT_CALL(*renderSurface, setNativeWindow(nativeWindow)).Times(1);
1753 EXPECT_CALL(*renderSurface, queryWidth()).WillOnce(Return(newWidth));
1754 EXPECT_CALL(*renderSurface, queryHeight()).WillOnce(Return(oldHeight));
1755
1756 // --------------------------------------------------------------------
1757 // Invocation
1758
1759 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1760}
1761
1762TEST_F(HandleTransactionLockedTest, processesDisplayHeightChanges) {
1763 using Case = NonHwcVirtualDisplayCase;
1764
1765 constexpr int oldWidth = 0;
1766 constexpr int oldHeight = 10;
1767 constexpr int newHeight = 123;
1768
1769 // --------------------------------------------------------------------
1770 // Preconditions
1771
1772 // A display is set up
1773 auto nativeWindow = new mock::NativeWindow();
1774 auto displaySurface = new mock::DisplaySurface();
1775 auto renderSurface = new RE::mock::Surface();
1776 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1777 display.setNativeWindow(nativeWindow);
1778 display.setDisplaySurface(displaySurface);
1779 display.setRenderSurface(std::unique_ptr<RE::Surface>(renderSurface));
1780 display.inject();
1781
1782 // There is a change to the viewport state
1783 display.mutableDrawingDisplayState().width = oldWidth;
1784 display.mutableDrawingDisplayState().height = oldHeight;
1785 display.mutableCurrentDisplayState().width = oldWidth;
1786 display.mutableCurrentDisplayState().height = newHeight;
1787
1788 // --------------------------------------------------------------------
1789 // Call Expectations
1790
1791 EXPECT_CALL(*renderSurface, setNativeWindow(nullptr)).Times(1);
1792 EXPECT_CALL(*displaySurface, resizeBuffers(oldWidth, newHeight)).Times(1);
1793 EXPECT_CALL(*renderSurface, setNativeWindow(nativeWindow)).Times(1);
1794 EXPECT_CALL(*renderSurface, queryWidth()).WillOnce(Return(oldWidth));
1795 EXPECT_CALL(*renderSurface, queryHeight()).WillOnce(Return(newHeight));
1796
1797 // --------------------------------------------------------------------
1798 // Invocation
1799
1800 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1801}
1802
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001803/* ------------------------------------------------------------------------
1804 * SurfaceFlinger::setDisplayStateLocked
1805 */
1806
1807TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithUnknownDisplay) {
1808 // --------------------------------------------------------------------
1809 // Preconditions
1810
1811 // We have an unknown display token not associated with a known display
1812 sp<BBinder> displayToken = new BBinder();
1813
1814 // The requested display state references the unknown display.
1815 DisplayState state;
1816 state.what = DisplayState::eLayerStackChanged;
1817 state.token = displayToken;
1818 state.layerStack = 456;
1819
1820 // --------------------------------------------------------------------
1821 // Invocation
1822
1823 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1824
1825 // --------------------------------------------------------------------
1826 // Postconditions
1827
1828 // The returned flags are empty
1829 EXPECT_EQ(0u, flags);
1830
1831 // The display token still doesn't match anything known.
1832 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1833}
1834
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001835TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWhenNoChanges) {
1836 using Case = SimplePrimaryDisplayCase;
1837
1838 // --------------------------------------------------------------------
1839 // Preconditions
1840
1841 // A display is already set up
1842 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1843 display.inject();
1844
1845 // No changes are made to the display
1846 DisplayState state;
1847 state.what = 0;
1848 state.token = display.token();
1849
1850 // --------------------------------------------------------------------
1851 // Invocation
1852
1853 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1854
1855 // --------------------------------------------------------------------
1856 // Postconditions
1857
1858 // The returned flags are empty
1859 EXPECT_EQ(0u, flags);
1860}
1861
1862TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSurfaceDidNotChange) {
1863 using Case = SimplePrimaryDisplayCase;
1864
1865 // --------------------------------------------------------------------
1866 // Preconditions
1867
1868 // A display is already set up
1869 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1870 display.inject();
1871
1872 // There is a surface that can be set.
1873 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
1874
1875 // The current display state has the surface set
1876 display.mutableCurrentDisplayState().surface = surface;
1877
1878 // The incoming request sets the same surface
1879 DisplayState state;
1880 state.what = DisplayState::eSurfaceChanged;
1881 state.token = display.token();
1882 state.surface = surface;
1883
1884 // --------------------------------------------------------------------
1885 // Invocation
1886
1887 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1888
1889 // --------------------------------------------------------------------
1890 // Postconditions
1891
1892 // The returned flags are empty
1893 EXPECT_EQ(0u, flags);
1894
1895 // The current display state is unchanged.
1896 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
1897}
1898
1899TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfSurfaceChanged) {
1900 using Case = SimplePrimaryDisplayCase;
1901
1902 // --------------------------------------------------------------------
1903 // Preconditions
1904
1905 // A display is already set up
1906 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1907 display.inject();
1908
1909 // There is a surface that can be set.
1910 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
1911
1912 // The current display state does not have a surface
1913 display.mutableCurrentDisplayState().surface = nullptr;
1914
1915 // The incoming request sets a surface
1916 DisplayState state;
1917 state.what = DisplayState::eSurfaceChanged;
1918 state.token = display.token();
1919 state.surface = surface;
1920
1921 // --------------------------------------------------------------------
1922 // Invocation
1923
1924 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1925
1926 // --------------------------------------------------------------------
1927 // Postconditions
1928
1929 // The returned flags indicate a transaction is needed
1930 EXPECT_EQ(eDisplayTransactionNeeded, flags);
1931
1932 // The current display layer stack state is set to the new value
1933 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
1934}
1935
1936TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfLayerStackDidNotChange) {
1937 using Case = SimplePrimaryDisplayCase;
1938
1939 // --------------------------------------------------------------------
1940 // Preconditions
1941
1942 // A display is already set up
1943 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1944 display.inject();
1945
1946 // The display has a layer stack set
1947 display.mutableCurrentDisplayState().layerStack = 456u;
1948
1949 // The incoming request sets the same layer stack
1950 DisplayState state;
1951 state.what = DisplayState::eLayerStackChanged;
1952 state.token = display.token();
1953 state.layerStack = 456u;
1954
1955 // --------------------------------------------------------------------
1956 // Invocation
1957
1958 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1959
1960 // --------------------------------------------------------------------
1961 // Postconditions
1962
1963 // The returned flags are empty
1964 EXPECT_EQ(0u, flags);
1965
1966 // The current display state is unchanged
1967 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
1968}
1969
1970TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfLayerStackChanged) {
1971 using Case = SimplePrimaryDisplayCase;
1972
1973 // --------------------------------------------------------------------
1974 // Preconditions
1975
1976 // A display is set up
1977 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1978 display.inject();
1979
1980 // The display has a layer stack set
1981 display.mutableCurrentDisplayState().layerStack = 654u;
1982
1983 // The incoming request sets a different layer stack
1984 DisplayState state;
1985 state.what = DisplayState::eLayerStackChanged;
1986 state.token = display.token();
1987 state.layerStack = 456u;
1988
1989 // --------------------------------------------------------------------
1990 // Invocation
1991
1992 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1993
1994 // --------------------------------------------------------------------
1995 // Postconditions
1996
1997 // The returned flags indicate a transaction is needed
1998 EXPECT_EQ(eDisplayTransactionNeeded, flags);
1999
2000 // The desired display state has been set to the new value.
2001 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2002}
2003
2004TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfProjectionDidNotChange) {
2005 using Case = SimplePrimaryDisplayCase;
2006 constexpr int initialOrientation = 180;
2007 const Rect initialFrame = {1, 2, 3, 4};
2008 const Rect initialViewport = {5, 6, 7, 8};
2009
2010 // --------------------------------------------------------------------
2011 // Preconditions
2012
2013 // A display is set up
2014 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2015 display.inject();
2016
2017 // The current display state projection state is all set
2018 display.mutableCurrentDisplayState().orientation = initialOrientation;
2019 display.mutableCurrentDisplayState().frame = initialFrame;
2020 display.mutableCurrentDisplayState().viewport = initialViewport;
2021
2022 // The incoming request sets the same projection state
2023 DisplayState state;
2024 state.what = DisplayState::eDisplayProjectionChanged;
2025 state.token = display.token();
2026 state.orientation = initialOrientation;
2027 state.frame = initialFrame;
2028 state.viewport = initialViewport;
2029
2030 // --------------------------------------------------------------------
2031 // Invocation
2032
2033 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2034
2035 // --------------------------------------------------------------------
2036 // Postconditions
2037
2038 // The returned flags are empty
2039 EXPECT_EQ(0u, flags);
2040
2041 // The current display state is unchanged
2042 EXPECT_EQ(initialOrientation, display.getCurrentDisplayState().orientation);
2043
2044 EXPECT_EQ(initialFrame, display.getCurrentDisplayState().frame);
2045 EXPECT_EQ(initialViewport, display.getCurrentDisplayState().viewport);
2046}
2047
2048TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfOrientationChanged) {
2049 using Case = SimplePrimaryDisplayCase;
2050 constexpr int initialOrientation = 90;
2051 constexpr int desiredOrientation = 180;
2052
2053 // --------------------------------------------------------------------
2054 // Preconditions
2055
2056 // A display is set up
2057 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2058 display.inject();
2059
2060 // The current display state has an orientation set
2061 display.mutableCurrentDisplayState().orientation = initialOrientation;
2062
2063 // The incoming request sets a different orientation
2064 DisplayState state;
2065 state.what = DisplayState::eDisplayProjectionChanged;
2066 state.token = display.token();
2067 state.orientation = desiredOrientation;
2068
2069 // --------------------------------------------------------------------
2070 // Invocation
2071
2072 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2073
2074 // --------------------------------------------------------------------
2075 // Postconditions
2076
2077 // The returned flags indicate a transaction is needed
2078 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2079
2080 // The current display state has the new value.
2081 EXPECT_EQ(desiredOrientation, display.getCurrentDisplayState().orientation);
2082}
2083
2084TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfFrameChanged) {
2085 using Case = SimplePrimaryDisplayCase;
2086 const Rect initialFrame = {0, 0, 0, 0};
2087 const Rect desiredFrame = {5, 6, 7, 8};
2088
2089 // --------------------------------------------------------------------
2090 // Preconditions
2091
2092 // A display is set up
2093 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2094 display.inject();
2095
2096 // The current display state does not have a frame
2097 display.mutableCurrentDisplayState().frame = initialFrame;
2098
2099 // The incoming request sets a frame
2100 DisplayState state;
2101 state.what = DisplayState::eDisplayProjectionChanged;
2102 state.token = display.token();
2103 state.frame = desiredFrame;
2104
2105 // --------------------------------------------------------------------
2106 // Invocation
2107
2108 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2109
2110 // --------------------------------------------------------------------
2111 // Postconditions
2112
2113 // The returned flags indicate a transaction is needed
2114 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2115
2116 // The current display state has the new value.
2117 EXPECT_EQ(desiredFrame, display.getCurrentDisplayState().frame);
2118}
2119
2120TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfViewportChanged) {
2121 using Case = SimplePrimaryDisplayCase;
2122 const Rect initialViewport = {0, 0, 0, 0};
2123 const Rect desiredViewport = {5, 6, 7, 8};
2124
2125 // --------------------------------------------------------------------
2126 // Preconditions
2127
2128 // A display is set up
2129 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2130 display.inject();
2131
2132 // The current display state does not have a viewport
2133 display.mutableCurrentDisplayState().viewport = initialViewport;
2134
2135 // The incoming request sets a viewport
2136 DisplayState state;
2137 state.what = DisplayState::eDisplayProjectionChanged;
2138 state.token = display.token();
2139 state.viewport = desiredViewport;
2140
2141 // --------------------------------------------------------------------
2142 // Invocation
2143
2144 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2145
2146 // --------------------------------------------------------------------
2147 // Postconditions
2148
2149 // The returned flags indicate a transaction is needed
2150 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2151
2152 // The current display state has the new value.
2153 EXPECT_EQ(desiredViewport, display.getCurrentDisplayState().viewport);
2154}
2155
2156TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSizeDidNotChange) {
2157 using Case = SimplePrimaryDisplayCase;
2158 constexpr uint32_t initialWidth = 1024;
2159 constexpr uint32_t initialHeight = 768;
2160
2161 // --------------------------------------------------------------------
2162 // Preconditions
2163
2164 // A display is set up
2165 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2166 display.inject();
2167
2168 // The current display state has a size set
2169 display.mutableCurrentDisplayState().width = initialWidth;
2170 display.mutableCurrentDisplayState().height = initialHeight;
2171
2172 // The incoming request sets the same display size
2173 DisplayState state;
2174 state.what = DisplayState::eDisplaySizeChanged;
2175 state.token = display.token();
2176 state.width = initialWidth;
2177 state.height = initialHeight;
2178
2179 // --------------------------------------------------------------------
2180 // Invocation
2181
2182 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2183
2184 // --------------------------------------------------------------------
2185 // Postconditions
2186
2187 // The returned flags are empty
2188 EXPECT_EQ(0u, flags);
2189
2190 // The current display state is unchanged
2191 EXPECT_EQ(initialWidth, display.getCurrentDisplayState().width);
2192 EXPECT_EQ(initialHeight, display.getCurrentDisplayState().height);
2193}
2194
2195TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfWidthChanged) {
2196 using Case = SimplePrimaryDisplayCase;
2197 constexpr uint32_t initialWidth = 0;
2198 constexpr uint32_t desiredWidth = 1024;
2199
2200 // --------------------------------------------------------------------
2201 // Preconditions
2202
2203 // A display is set up
2204 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2205 display.inject();
2206
2207 // The display does not yet have a width
2208 display.mutableCurrentDisplayState().width = initialWidth;
2209
2210 // The incoming request sets a display width
2211 DisplayState state;
2212 state.what = DisplayState::eDisplaySizeChanged;
2213 state.token = display.token();
2214 state.width = desiredWidth;
2215
2216 // --------------------------------------------------------------------
2217 // Invocation
2218
2219 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2220
2221 // --------------------------------------------------------------------
2222 // Postconditions
2223
2224 // The returned flags indicate a transaction is needed
2225 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2226
2227 // The current display state has the new value.
2228 EXPECT_EQ(desiredWidth, display.getCurrentDisplayState().width);
2229}
2230
2231TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfHeightChanged) {
2232 using Case = SimplePrimaryDisplayCase;
2233 constexpr uint32_t initialHeight = 0;
2234 constexpr uint32_t desiredHeight = 768;
2235
2236 // --------------------------------------------------------------------
2237 // Preconditions
2238
2239 // A display is set up
2240 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2241 display.inject();
2242
2243 // The display does not yet have a height
2244 display.mutableCurrentDisplayState().height = initialHeight;
2245
2246 // The incoming request sets a display height
2247 DisplayState state;
2248 state.what = DisplayState::eDisplaySizeChanged;
2249 state.token = display.token();
2250 state.height = desiredHeight;
2251
2252 // --------------------------------------------------------------------
2253 // Invocation
2254
2255 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2256
2257 // --------------------------------------------------------------------
2258 // Postconditions
2259
2260 // The returned flags indicate a transaction is needed
2261 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2262
2263 // The current display state has the new value.
2264 EXPECT_EQ(desiredHeight, display.getCurrentDisplayState().height);
2265}
2266
Lloyd Pique86016da2018-03-01 16:09:38 -08002267/* ------------------------------------------------------------------------
2268 * SurfaceFlinger::onInitializeDisplays
2269 */
2270
2271TEST_F(DisplayTransactionTest, onInitializeDisplaysSetsUpPrimaryDisplay) {
2272 using Case = SimplePrimaryDisplayCase;
2273
2274 // --------------------------------------------------------------------
2275 // Preconditions
2276
2277 // A primary display is set up
2278 Case::Display::injectHwcDisplay(this);
2279 auto primaryDisplay = Case::Display::makeFakeExistingDisplayInjector(this);
2280 primaryDisplay.inject();
2281
2282 // --------------------------------------------------------------------
2283 // Call Expectations
2284
2285 // We expect the surface interceptor to possibly be used, but we treat it as
2286 // disabled since it is called as a side effect rather than directly by this
2287 // function.
2288 EXPECT_CALL(*mSurfaceInterceptor, isEnabled()).WillOnce(Return(false));
2289
2290 // We expect a call to get the active display config.
2291 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
2292
2293 // We expect invalidate() to be invoked once to trigger display transaction
2294 // processing.
2295 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
2296
2297 // --------------------------------------------------------------------
2298 // Invocation
2299
2300 mFlinger.onInitializeDisplays();
2301
2302 // --------------------------------------------------------------------
2303 // Postconditions
2304
2305 // The primary display should have a current state
2306 ASSERT_TRUE(hasCurrentDisplayState(primaryDisplay.token()));
2307 const auto& primaryDisplayState = getCurrentDisplayState(primaryDisplay.token());
2308 // The layer stack state should be set to zero
2309 EXPECT_EQ(0u, primaryDisplayState.layerStack);
2310 // The orientation state should be set to zero
2311 EXPECT_EQ(0, primaryDisplayState.orientation);
2312
2313 // The frame state should be set to INVALID
2314 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.frame);
2315
2316 // The viewport state should be set to INVALID
2317 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.viewport);
2318
2319 // The width and height should both be zero
2320 EXPECT_EQ(0u, primaryDisplayState.width);
2321 EXPECT_EQ(0u, primaryDisplayState.height);
2322
2323 // The display should be set to HWC_POWER_MODE_NORMAL
2324 ASSERT_TRUE(hasDisplayDevice(primaryDisplay.token()));
2325 auto displayDevice = primaryDisplay.mutableDisplayDevice();
2326 EXPECT_EQ(HWC_POWER_MODE_NORMAL, displayDevice->getPowerMode());
2327
2328 // The display refresh period should be set in the frame tracker.
2329 FrameStats stats;
2330 mFlinger.getAnimFrameTracker().getStats(&stats);
2331 EXPECT_EQ(DEFAULT_REFRESH_RATE, stats.refreshPeriodNano);
2332
2333 // The display transaction needed flag should be set.
2334 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
2335
2336 // The compositor timing should be set to default values
2337 const auto& compositorTiming = mFlinger.getCompositorTiming();
2338 EXPECT_EQ(-DEFAULT_REFRESH_RATE, compositorTiming.deadline);
2339 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.interval);
2340 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.presentLatency);
2341}
2342
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002343/* ------------------------------------------------------------------------
2344 * SurfaceFlinger::setPowerModeInternal
2345 */
2346
2347// Used when we simulate a display that supports doze.
2348struct DozeIsSupportedVariant {
2349 static constexpr bool DOZE_SUPPORTED = true;
2350 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2351 IComposerClient::PowerMode::DOZE;
2352 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2353 IComposerClient::PowerMode::DOZE_SUSPEND;
2354};
2355
2356// Used when we simulate a display that does not support doze.
2357struct DozeNotSupportedVariant {
2358 static constexpr bool DOZE_SUPPORTED = false;
2359 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2360 IComposerClient::PowerMode::ON;
2361 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2362 IComposerClient::PowerMode::ON;
2363};
2364
2365struct EventThreadBaseSupportedVariant {
2366 static void setupEventAndEventControlThreadNoCallExpectations(DisplayTransactionTest* test) {
2367 // The event control thread should not be notified.
2368 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(_)).Times(0);
2369
2370 // The event thread should not be notified.
2371 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(0);
2372 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(0);
2373 }
2374};
2375
2376struct EventThreadNotSupportedVariant : public EventThreadBaseSupportedVariant {
2377 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2378 // These calls are only expected for the primary display.
2379
2380 // Instead expect no calls.
2381 setupEventAndEventControlThreadNoCallExpectations(test);
2382 }
2383
2384 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2385 // These calls are only expected for the primary display.
2386
2387 // Instead expect no calls.
2388 setupEventAndEventControlThreadNoCallExpectations(test);
2389 }
2390};
2391
2392struct EventThreadIsSupportedVariant : public EventThreadBaseSupportedVariant {
2393 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2394 // The event control thread should be notified to enable vsyncs
2395 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(true)).Times(1);
2396
2397 // The event thread should be notified that the screen was acquired.
2398 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(1);
2399 }
2400
2401 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2402 // There should be a call to setVsyncEnabled(false)
2403 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(false)).Times(1);
2404
2405 // The event thread should not be notified that the screen was released.
2406 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(1);
2407 }
2408};
2409
Lloyd Pique41be5d22018-06-21 13:11:48 -07002410struct DispSyncIsSupportedVariant {
2411 static void setupBeginResyncCallExpectations(DisplayTransactionTest* test) {
2412 EXPECT_CALL(*test->mPrimaryDispSync, reset()).Times(1);
2413 EXPECT_CALL(*test->mPrimaryDispSync, setPeriod(DEFAULT_REFRESH_RATE)).Times(1);
2414 EXPECT_CALL(*test->mPrimaryDispSync, beginResync()).Times(1);
2415 }
2416
2417 static void setupEndResyncCallExpectations(DisplayTransactionTest* test) {
2418 EXPECT_CALL(*test->mPrimaryDispSync, endResync()).Times(1);
2419 }
2420};
2421
2422struct DispSyncNotSupportedVariant {
2423 static void setupBeginResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2424
2425 static void setupEndResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2426};
2427
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002428// --------------------------------------------------------------------
2429// Note:
2430//
2431// There are a large number of transitions we could test, however we only test a
2432// selected subset which provides complete test coverage of the implementation.
2433// --------------------------------------------------------------------
2434
2435template <int initialPowerMode, int targetPowerMode>
2436struct TransitionVariantCommon {
2437 static constexpr auto INITIAL_POWER_MODE = initialPowerMode;
2438 static constexpr auto TARGET_POWER_MODE = targetPowerMode;
2439
2440 static void verifyPostconditions(DisplayTransactionTest*) {}
2441};
2442
2443struct TransitionOffToOnVariant
2444 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_NORMAL> {
2445 template <typename Case>
2446 static void setupCallExpectations(DisplayTransactionTest* test) {
2447 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2448 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002449 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002450 Case::setupRepaintEverythingCallExpectations(test);
2451 }
2452
2453 static void verifyPostconditions(DisplayTransactionTest* test) {
2454 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2455 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2456 }
2457};
2458
2459struct TransitionOffToDozeSuspendVariant
2460 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_DOZE_SUSPEND> {
2461 template <typename Case>
2462 static void setupCallExpectations(DisplayTransactionTest* test) {
2463 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2464 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2465 Case::setupRepaintEverythingCallExpectations(test);
2466 }
2467
2468 static void verifyPostconditions(DisplayTransactionTest* test) {
2469 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2470 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2471 }
2472};
2473
2474struct TransitionOnToOffVariant
2475 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_OFF> {
2476 template <typename Case>
2477 static void setupCallExpectations(DisplayTransactionTest* test) {
2478 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002479 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002480 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2481 }
2482
2483 static void verifyPostconditions(DisplayTransactionTest* test) {
2484 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2485 }
2486};
2487
2488struct TransitionDozeSuspendToOffVariant
2489 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_OFF> {
2490 template <typename Case>
2491 static void setupCallExpectations(DisplayTransactionTest* test) {
2492 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2493 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2494 }
2495
2496 static void verifyPostconditions(DisplayTransactionTest* test) {
2497 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2498 }
2499};
2500
2501struct TransitionOnToDozeVariant
2502 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE> {
2503 template <typename Case>
2504 static void setupCallExpectations(DisplayTransactionTest* test) {
2505 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2506 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2507 }
2508};
2509
2510struct TransitionDozeSuspendToDozeVariant
2511 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_DOZE> {
2512 template <typename Case>
2513 static void setupCallExpectations(DisplayTransactionTest* test) {
2514 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002515 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002516 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2517 }
2518};
2519
2520struct TransitionDozeToOnVariant
2521 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE, HWC_POWER_MODE_NORMAL> {
2522 template <typename Case>
2523 static void setupCallExpectations(DisplayTransactionTest* test) {
2524 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2525 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2526 }
2527};
2528
2529struct TransitionDozeSuspendToOnVariant
2530 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_NORMAL> {
2531 template <typename Case>
2532 static void setupCallExpectations(DisplayTransactionTest* test) {
2533 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002534 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002535 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2536 }
2537};
2538
2539struct TransitionOnToDozeSuspendVariant
2540 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE_SUSPEND> {
2541 template <typename Case>
2542 static void setupCallExpectations(DisplayTransactionTest* test) {
2543 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002544 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002545 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2546 }
2547};
2548
2549struct TransitionOnToUnknownVariant
2550 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_LEET> {
2551 template <typename Case>
2552 static void setupCallExpectations(DisplayTransactionTest* test) {
2553 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2554 Case::setupNoComposerPowerModeCallExpectations(test);
2555 }
2556};
2557
2558// --------------------------------------------------------------------
2559// Note:
2560//
2561// Rather than testing the cartesian product of of
2562// DozeIsSupported/DozeNotSupported with all other options, we use one for one
2563// display type, and the other for another display type.
2564// --------------------------------------------------------------------
2565
2566template <typename DisplayVariant, typename DozeVariant, typename EventThreadVariant,
Lloyd Pique41be5d22018-06-21 13:11:48 -07002567 typename DispSyncVariant, typename TransitionVariant>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002568struct DisplayPowerCase {
2569 using Display = DisplayVariant;
2570 using Doze = DozeVariant;
2571 using EventThread = EventThreadVariant;
Lloyd Pique41be5d22018-06-21 13:11:48 -07002572 using DispSync = DispSyncVariant;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002573 using Transition = TransitionVariant;
2574
2575 static auto injectDisplayWithInitialPowerMode(DisplayTransactionTest* test, int mode) {
2576 Display::injectHwcDisplay(test);
2577 auto display = Display::makeFakeExistingDisplayInjector(test);
2578 display.inject();
2579 display.mutableDisplayDevice()->setPowerMode(mode);
2580 return display;
2581 }
2582
2583 static void setInitialPrimaryHWVsyncEnabled(DisplayTransactionTest* test, bool enabled) {
2584 test->mFlinger.mutablePrimaryHWVsyncEnabled() = enabled;
2585 }
2586
2587 static void setupRepaintEverythingCallExpectations(DisplayTransactionTest* test) {
2588 EXPECT_CALL(*test->mMessageQueue, invalidate()).Times(1);
2589 }
2590
2591 static void setupSurfaceInterceptorCallExpectations(DisplayTransactionTest* test, int mode) {
2592 EXPECT_CALL(*test->mSurfaceInterceptor, isEnabled()).WillOnce(Return(true));
2593 EXPECT_CALL(*test->mSurfaceInterceptor, savePowerModeUpdate(_, mode)).Times(1);
2594 }
2595
2596 static void setupComposerCallExpectations(DisplayTransactionTest* test,
2597 IComposerClient::PowerMode mode) {
2598 // Any calls to get the active config will return a default value.
2599 EXPECT_CALL(*test->mComposer, getActiveConfig(Display::HWC_DISPLAY_ID, _))
2600 .WillRepeatedly(DoAll(SetArgPointee<1>(Display::HWC_ACTIVE_CONFIG_ID),
2601 Return(Error::NONE)));
2602
2603 // Any calls to get whether the display supports dozing will return the value set by the
2604 // policy variant.
2605 EXPECT_CALL(*test->mComposer, getDozeSupport(Display::HWC_DISPLAY_ID, _))
2606 .WillRepeatedly(DoAll(SetArgPointee<1>(Doze::DOZE_SUPPORTED), Return(Error::NONE)));
2607
2608 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, mode)).Times(1);
2609 }
2610
2611 static void setupNoComposerPowerModeCallExpectations(DisplayTransactionTest* test) {
2612 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, _)).Times(0);
2613 }
2614};
2615
2616// A sample configuration for the primary display.
2617// In addition to having event thread support, we emulate doze support.
2618template <typename TransitionVariant>
2619using PrimaryDisplayPowerCase = DisplayPowerCase<PrimaryDisplayVariant, DozeIsSupportedVariant,
Lloyd Pique41be5d22018-06-21 13:11:48 -07002620 EventThreadIsSupportedVariant,
2621 DispSyncIsSupportedVariant, TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002622
2623// A sample configuration for the external display.
2624// In addition to not having event thread support, we emulate not having doze
2625// support.
2626template <typename TransitionVariant>
Lloyd Pique41be5d22018-06-21 13:11:48 -07002627using ExternalDisplayPowerCase = DisplayPowerCase<ExternalDisplayVariant, DozeNotSupportedVariant,
2628 EventThreadNotSupportedVariant,
2629 DispSyncNotSupportedVariant, TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002630
2631class SetPowerModeInternalTest : public DisplayTransactionTest {
2632public:
2633 template <typename Case>
2634 void transitionDisplayCommon();
2635};
2636
2637template <int PowerMode>
2638struct PowerModeInitialVSyncEnabled : public std::false_type {};
2639
2640template <>
2641struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_NORMAL> : public std::true_type {};
2642
2643template <>
2644struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_DOZE> : public std::true_type {};
2645
2646template <typename Case>
2647void SetPowerModeInternalTest::transitionDisplayCommon() {
2648 // --------------------------------------------------------------------
2649 // Preconditions
2650
2651 auto display =
2652 Case::injectDisplayWithInitialPowerMode(this, Case::Transition::INITIAL_POWER_MODE);
2653 Case::setInitialPrimaryHWVsyncEnabled(this,
2654 PowerModeInitialVSyncEnabled<
2655 Case::Transition::INITIAL_POWER_MODE>::value);
2656
2657 // --------------------------------------------------------------------
2658 // Call Expectations
2659
2660 Case::setupSurfaceInterceptorCallExpectations(this, Case::Transition::TARGET_POWER_MODE);
2661 Case::Transition::template setupCallExpectations<Case>(this);
2662
2663 // --------------------------------------------------------------------
2664 // Invocation
2665
2666 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(),
2667 Case::Transition::TARGET_POWER_MODE);
2668
2669 // --------------------------------------------------------------------
2670 // Postconditions
2671
2672 Case::Transition::verifyPostconditions(this);
2673}
2674
2675TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfNoChange) {
2676 using Case = SimplePrimaryDisplayCase;
2677
2678 // --------------------------------------------------------------------
2679 // Preconditions
2680
2681 // A primary display device is set up
2682 Case::Display::injectHwcDisplay(this);
2683 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2684 display.inject();
2685
Dominik Laskowskia2edf612018-06-01 13:15:16 -07002686 // The display is already set to HWC_POWER_MODE_NORMAL
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002687 display.mutableDisplayDevice()->setPowerMode(HWC_POWER_MODE_NORMAL);
2688
2689 // --------------------------------------------------------------------
2690 // Invocation
2691
2692 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_NORMAL);
2693
2694 // --------------------------------------------------------------------
2695 // Postconditions
2696
2697 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2698}
2699
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002700TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfVirtualDisplay) {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002701 using Case = HwcVirtualDisplayCase;
2702
2703 // --------------------------------------------------------------------
2704 // Preconditions
2705
2706 // We need to resize this so that the HWC thinks the virtual display
2707 // is something it created.
2708 mFlinger.mutableHwcDisplayData().resize(3);
2709
2710 // A virtual display device is set up
2711 Case::Display::injectHwcDisplay(this);
2712 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2713 display.inject();
2714
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002715 // The display is set to HWC_POWER_MODE_NORMAL
2716 getDisplayDevice(display.token())->setPowerMode(HWC_POWER_MODE_NORMAL);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002717
2718 // --------------------------------------------------------------------
2719 // Invocation
2720
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002721 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_OFF);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002722
2723 // --------------------------------------------------------------------
2724 // Postconditions
2725
2726 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2727}
2728
2729TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnPrimaryDisplay) {
2730 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToOnVariant>>();
2731}
2732
2733TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendPrimaryDisplay) {
2734 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
2735}
2736
2737TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffPrimaryDisplay) {
2738 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToOffVariant>>();
2739}
2740
2741TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffPrimaryDisplay) {
2742 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
2743}
2744
2745TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozePrimaryDisplay) {
2746 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeVariant>>();
2747}
2748
2749TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozePrimaryDisplay) {
2750 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
2751}
2752
2753TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnPrimaryDisplay) {
2754 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeToOnVariant>>();
2755}
2756
2757TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnPrimaryDisplay) {
2758 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
2759}
2760
2761TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendPrimaryDisplay) {
2762 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
2763}
2764
2765TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownPrimaryDisplay) {
2766 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToUnknownVariant>>();
2767}
2768
2769TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnExternalDisplay) {
2770 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToOnVariant>>();
2771}
2772
2773TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendExternalDisplay) {
2774 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
2775}
2776
2777TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffExternalDisplay) {
2778 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToOffVariant>>();
2779}
2780
2781TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffExternalDisplay) {
2782 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
2783}
2784
2785TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeExternalDisplay) {
2786 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeVariant>>();
2787}
2788
2789TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozeExternalDisplay) {
2790 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
2791}
2792
2793TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnExternalDisplay) {
2794 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeToOnVariant>>();
2795}
2796
2797TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnExternalDisplay) {
2798 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
2799}
2800
2801TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendExternalDisplay) {
2802 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
2803}
2804
2805TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownExternalDisplay) {
2806 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToUnknownVariant>>();
2807}
2808
Lloyd Piquef58625d2017-12-19 13:22:33 -08002809} // namespace
2810} // namespace android