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