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