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