blob: ca1cb57edb42264430124016f136256ed2b2128e [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"
28#include "mock/MockEventControlThread.h"
29#include "mock/MockEventThread.h"
30#include "mock/MockMessageQueue.h"
31#include "mock/MockNativeWindowSurface.h"
32#include "mock/MockSurfaceInterceptor.h"
33#include "mock/RenderEngine/MockRenderEngine.h"
34#include "mock/gui/MockGraphicBufferConsumer.h"
35#include "mock/gui/MockGraphicBufferProducer.h"
36#include "mock/system/window/MockNativeWindow.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080037
38namespace android {
39namespace {
40
Lloyd Piquee39cad22017-12-20 17:01:29 -080041using testing::_;
42using testing::ByMove;
43using testing::DoAll;
44using testing::Mock;
45using testing::Return;
46using testing::SetArgPointee;
47
48using android::hardware::graphics::common::V1_0::Hdr;
Peiyong Lin0e7a7912018-04-05 14:36:36 -070049using android::hardware::graphics::common::V1_1::ColorMode;
Lloyd Piquee39cad22017-12-20 17:01:29 -080050using android::Hwc2::Error;
51using android::Hwc2::IComposer;
52using android::Hwc2::IComposerClient;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080053using android::Hwc2::RenderIntent;
Lloyd Piquee39cad22017-12-20 17:01:29 -080054
Lloyd Piquec11e0d32018-01-22 18:44:59 -080055using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector;
56using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
Lloyd Pique1fa4d462018-01-22 18:03:16 -080057using HotplugEvent = TestableSurfaceFlinger::HotplugEvent;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080058using HWC2Display = TestableSurfaceFlinger::HWC2Display;
Lloyd Piquebc792092018-01-17 11:52:30 -080059
Lloyd Piquec11e0d32018-01-22 18:44:59 -080060constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666;
Lloyd Piquee39cad22017-12-20 17:01:29 -080061constexpr int32_t DEFAULT_DPI = 320;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080062constexpr int DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT = HAL_PIXEL_FORMAT_RGB_565;
Lloyd Piquee39cad22017-12-20 17:01:29 -080063
Lloyd Piquec11e0d32018-01-22 18:44:59 -080064/* ------------------------------------------------------------------------
65 * Boolean avoidance
66 *
67 * To make calls and template instantiations more readable, we define some
68 * local enums along with an implicit bool conversion.
69 */
70
71#define BOOL_SUBSTITUTE(TYPENAME) enum class TYPENAME : bool { FALSE = false, TRUE = true };
72
73BOOL_SUBSTITUTE(Critical);
74BOOL_SUBSTITUTE(Async);
75BOOL_SUBSTITUTE(Secure);
76
77/* ------------------------------------------------------------------------
78 *
79 */
Lloyd Pique1fa4d462018-01-22 18:03:16 -080080
Lloyd Piquef58625d2017-12-19 13:22:33 -080081class DisplayTransactionTest : public testing::Test {
Lloyd Piquec11e0d32018-01-22 18:44:59 -080082public:
Lloyd Piquef58625d2017-12-19 13:22:33 -080083 DisplayTransactionTest();
84 ~DisplayTransactionTest() override;
85
Lloyd Pique1fa4d462018-01-22 18:03:16 -080086 // --------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -080087 // Mock/Fake injection
Lloyd Piquef58625d2017-12-19 13:22:33 -080088
Lloyd Piquec11e0d32018-01-22 18:44:59 -080089 void injectMockComposer(int virtualDisplayCount);
90 void injectFakeBufferQueueFactory();
91 void injectFakeNativeWindowSurfaceFactory();
Lloyd Pique1fa4d462018-01-22 18:03:16 -080092
93 // --------------------------------------------------------------------
94 // Postcondition helpers
95
Lloyd Piquec11e0d32018-01-22 18:44:59 -080096 bool hasHwcDisplay(hwc2_display_t displayId);
Lloyd Pique1fa4d462018-01-22 18:03:16 -080097 bool hasTransactionFlagSet(int flag);
98 bool hasDisplayDevice(sp<IBinder> displayToken);
99 sp<DisplayDevice> getDisplayDevice(sp<IBinder> displayToken);
100 bool hasCurrentDisplayState(sp<IBinder> displayToken);
101 const DisplayDeviceState& getCurrentDisplayState(sp<IBinder> displayToken);
102 bool hasDrawingDisplayState(sp<IBinder> displayToken);
103 const DisplayDeviceState& getDrawingDisplayState(sp<IBinder> displayToken);
104
105 // --------------------------------------------------------------------
106 // Test instances
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800107
Lloyd Piquef58625d2017-12-19 13:22:33 -0800108 TestableSurfaceFlinger mFlinger;
Lloyd Piquee39cad22017-12-20 17:01:29 -0800109 mock::EventThread* mEventThread = new mock::EventThread();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800110 mock::EventControlThread* mEventControlThread = new mock::EventControlThread();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800111
112 // These mocks are created by the test, but are destroyed by SurfaceFlinger
113 // by virtue of being stored into a std::unique_ptr. However we still need
114 // to keep a reference to them for use in setting up call expectations.
115 RE::mock::RenderEngine* mRenderEngine = new RE::mock::RenderEngine();
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800116 Hwc2::mock::Composer* mComposer = nullptr;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800117 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
118 mock::SurfaceInterceptor* mSurfaceInterceptor = new mock::SurfaceInterceptor();
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800119
120 // These mocks are created only when expected to be created via a factory.
121 sp<mock::GraphicBufferConsumer> mConsumer;
122 sp<mock::GraphicBufferProducer> mProducer;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800123 mock::NativeWindowSurface* mNativeWindowSurface = nullptr;
124 sp<mock::NativeWindow> mNativeWindow;
125 RE::mock::Surface* mRenderSurface = nullptr;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800126};
127
128DisplayTransactionTest::DisplayTransactionTest() {
129 const ::testing::TestInfo* const test_info =
130 ::testing::UnitTest::GetInstance()->current_test_info();
131 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800132
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800133 // Default to no wide color display support configured
134 mFlinger.mutableHasWideColorDisplay() = false;
135 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
136
137 // Default to using HWC virtual displays
138 mFlinger.mutableUseHwcVirtualDisplays() = true;
139
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800140 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
141 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
142 });
143
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800144 mFlinger.setCreateNativeWindowSurface([](auto) {
145 ADD_FAILURE() << "Unexpected request to create a native window surface.";
146 return nullptr;
147 });
148
149 mFlinger.mutableEventControlThread().reset(mEventControlThread);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800150 mFlinger.mutableEventThread().reset(mEventThread);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800151 mFlinger.mutableEventQueue().reset(mMessageQueue);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800152 mFlinger.setupRenderEngine(std::unique_ptr<RE::RenderEngine>(mRenderEngine));
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800153 mFlinger.mutableInterceptor().reset(mSurfaceInterceptor);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800154
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800155 injectMockComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -0800156}
157
158DisplayTransactionTest::~DisplayTransactionTest() {
159 const ::testing::TestInfo* const test_info =
160 ::testing::UnitTest::GetInstance()->current_test_info();
161 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
162}
163
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800164void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
165 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800166 EXPECT_CALL(*mComposer, getCapabilities())
167 .WillOnce(Return(std::vector<IComposer::Capability>()));
168 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
169 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -0800170
Lloyd Piquee39cad22017-12-20 17:01:29 -0800171 Mock::VerifyAndClear(mComposer);
172}
173
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800174void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800175 // This setup is only expected once per test.
176 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
177
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800178 mConsumer = new mock::GraphicBufferConsumer();
179 mProducer = new mock::GraphicBufferProducer();
180
181 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
182 *outProducer = mProducer;
183 *outConsumer = mConsumer;
184 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800185}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800186
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800187void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800188 // This setup is only expected once per test.
189 ASSERT_TRUE(mNativeWindowSurface == nullptr);
190
191 mNativeWindowSurface = new mock::NativeWindowSurface();
192 mNativeWindow = new mock::NativeWindow();
193
194 mFlinger.setCreateNativeWindowSurface(
195 [this](auto) { return std::unique_ptr<NativeWindowSurface>(mNativeWindowSurface); });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800196}
197
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800198bool DisplayTransactionTest::hasHwcDisplay(hwc2_display_t displayId) {
199 return mFlinger.mutableHwcDisplaySlots().count(displayId) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800200}
201
202bool DisplayTransactionTest::hasTransactionFlagSet(int flag) {
203 return mFlinger.mutableTransactionFlags() & flag;
204}
205
206bool DisplayTransactionTest::hasDisplayDevice(sp<IBinder> displayToken) {
207 return mFlinger.mutableDisplays().indexOfKey(displayToken) >= 0;
208}
209
210sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) {
211 return mFlinger.mutableDisplays().valueFor(displayToken);
212}
213
214bool DisplayTransactionTest::hasCurrentDisplayState(sp<IBinder> displayToken) {
215 return mFlinger.mutableCurrentState().displays.indexOfKey(displayToken) >= 0;
216}
217
218const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(sp<IBinder> displayToken) {
219 return mFlinger.mutableCurrentState().displays.valueFor(displayToken);
220}
221
222bool DisplayTransactionTest::hasDrawingDisplayState(sp<IBinder> displayToken) {
223 return mFlinger.mutableDrawingState().displays.indexOfKey(displayToken) >= 0;
224}
225
226const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(sp<IBinder> displayToken) {
227 return mFlinger.mutableDrawingState().displays.valueFor(displayToken);
228}
229
230/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800231 *
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800232 */
233
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800234template <DisplayDevice::DisplayType type, DisplayDevice::DisplayType hwcId, int width, int height,
235 Critical critical, Async async, Secure secure, int grallocUsage>
236struct DisplayVariant {
237 // The display width and height
238 static constexpr int WIDTH = width;
239 static constexpr int HEIGHT = height;
240
241 static constexpr int GRALLOC_USAGE = grallocUsage;
242
243 // The type for this display
244 static constexpr DisplayDevice::DisplayType TYPE = type;
245 static constexpr DisplayDevice::DisplayType HWCOMPOSER_ID = hwcId;
246
247 // When creating native window surfaces for the framebuffer, whether those should be critical
248 static constexpr Critical CRITICAL = critical;
249
250 // When creating native window surfaces for the framebuffer, whether those should be async
251 static constexpr Async ASYNC = async;
252
253 // Whether the display should be treated as secure
254 static constexpr Secure SECURE = secure;
255
256 static auto makeFakeExistingDisplayInjector(DisplayTransactionTest* test) {
257 auto injector = FakeDisplayDeviceInjector(test->mFlinger, TYPE, HWCOMPOSER_ID);
258 injector.setSecure(static_cast<bool>(SECURE));
259 return injector;
260 }
261
262 // Called by tests to set up any native window creation call expectations.
263 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
264 EXPECT_CALL(*test->mNativeWindowSurface, getNativeWindow())
265 .WillOnce(Return(test->mNativeWindow));
266 EXPECT_CALL(*test->mNativeWindow, perform(19)).WillRepeatedly(Return(NO_ERROR));
267
268 // For simplicity, we only expect to create a single render surface for
269 // each test.
270 ASSERT_TRUE(test->mRenderSurface == nullptr);
271 test->mRenderSurface = new RE::mock::Surface();
272 EXPECT_CALL(*test->mRenderEngine, createSurface())
273 .WillOnce(Return(ByMove(std::unique_ptr<RE::Surface>(test->mRenderSurface))));
274 EXPECT_CALL(*test->mRenderSurface, setAsync(static_cast<bool>(ASYNC))).Times(1);
275 EXPECT_CALL(*test->mRenderSurface, setCritical(static_cast<bool>(CRITICAL))).Times(1);
276 EXPECT_CALL(*test->mRenderSurface, setNativeWindow(test->mNativeWindow.get())).Times(1);
277 EXPECT_CALL(*test->mRenderSurface, queryWidth()).WillOnce(Return(WIDTH));
278 EXPECT_CALL(*test->mRenderSurface, queryHeight()).WillOnce(Return(HEIGHT));
279 }
280
281 static void setupFramebufferConsumerBufferQueueCallExpectations(DisplayTransactionTest* test) {
282 EXPECT_CALL(*test->mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR));
283 EXPECT_CALL(*test->mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR));
284 EXPECT_CALL(*test->mConsumer, setConsumerUsageBits(GRALLOC_USAGE))
285 .WillRepeatedly(Return(NO_ERROR));
286 EXPECT_CALL(*test->mConsumer, setDefaultBufferSize(WIDTH, HEIGHT))
287 .WillRepeatedly(Return(NO_ERROR));
288 EXPECT_CALL(*test->mConsumer, setMaxAcquiredBufferCount(_))
289 .WillRepeatedly(Return(NO_ERROR));
290 }
291
292 static void setupFramebufferProducerBufferQueueCallExpectations(DisplayTransactionTest* test) {
293 EXPECT_CALL(*test->mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return());
294 }
295};
296
297template <hwc2_display_t hwcDisplayId, HWC2::DisplayType hwcDisplayType, typename DisplayVariant>
298struct HwcDisplayVariant {
299 // The display id supplied by the HWC
300 static constexpr hwc2_display_t HWC_DISPLAY_ID = hwcDisplayId;
301
302 // The HWC display type
303 static constexpr HWC2::DisplayType HWC_DISPLAY_TYPE = hwcDisplayType;
304
305 // The HWC active configuration id
306 // TODO(b/69807179): SurfaceFlinger does not correctly get the active
307 // config. Once it does, change this to non-zero so that it is properly
308 // covered.
309 // static constexpr int HWC_ACTIVE_CONFIG_ID = 2001;
310 static constexpr int HWC_ACTIVE_CONFIG_ID = 0;
311
312 static void injectPendingHotplugEvent(DisplayTransactionTest* test,
313 HWC2::Connection connection) {
314 test->mFlinger.mutablePendingHotplugEvents().emplace_back(
315 HotplugEvent{HWC_DISPLAY_ID, connection});
316 }
317
318 // Called by tests to inject a HWC display setup
319 static void injectHwcDisplay(DisplayTransactionTest* test) {
320 FakeHwcDisplayInjector(DisplayVariant::TYPE, HWC_DISPLAY_TYPE)
321 .setHwcDisplayId(HWC_DISPLAY_ID)
322 .setWidth(DisplayVariant::WIDTH)
323 .setHeight(DisplayVariant::HEIGHT)
324 .setActiveConfig(HWC_ACTIVE_CONFIG_ID)
325 .inject(&test->mFlinger, test->mComposer);
326 }
327
328 static void setupHwcHotplugCallExpectations(DisplayTransactionTest* test) {
329 EXPECT_CALL(*test->mComposer, getDisplayType(HWC_DISPLAY_ID, _))
330 .WillOnce(DoAll(SetArgPointee<1>(static_cast<IComposerClient::DisplayType>(
331 HWC_DISPLAY_TYPE)),
332 Return(Error::NONE)));
333 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
334 EXPECT_CALL(*test->mComposer, getDisplayConfigs(HWC_DISPLAY_ID, _))
335 .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{HWC_ACTIVE_CONFIG_ID}),
336 Return(Error::NONE)));
337 EXPECT_CALL(*test->mComposer,
338 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
339 IComposerClient::Attribute::WIDTH, _))
340 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::WIDTH), Return(Error::NONE)));
341 EXPECT_CALL(*test->mComposer,
342 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
343 IComposerClient::Attribute::HEIGHT, _))
344 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::HEIGHT), Return(Error::NONE)));
345 EXPECT_CALL(*test->mComposer,
346 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
347 IComposerClient::Attribute::VSYNC_PERIOD, _))
348 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE)));
349 EXPECT_CALL(*test->mComposer,
350 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
351 IComposerClient::Attribute::DPI_X, _))
352 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
353 EXPECT_CALL(*test->mComposer,
354 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
355 IComposerClient::Attribute::DPI_Y, _))
356 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
357 }
358
359 // Called by tests to set up HWC call expectations
360 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
361 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY_ID, _))
362 .WillOnce(DoAll(SetArgPointee<1>(HWC_ACTIVE_CONFIG_ID), Return(Error::NONE)));
363 }
364};
365
366struct NonHwcDisplayVariant {
367 static constexpr int HWC_ACTIVE_CONFIG_ID = 0;
368
369 static void injectHwcDisplay(DisplayTransactionTest*) {}
370
371 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
372 EXPECT_CALL(*test->mComposer, getActiveConfig(_, _)).Times(0);
373 }
374};
375
376// Physical displays are expected to be synchronous, secure, and have a HWC display for output.
377constexpr uint32_t GRALLOC_USAGE_PHYSICAL_DISPLAY =
378 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
379
380template <hwc2_display_t hwcDisplayId, DisplayDevice::DisplayType type, int width, int height,
381 Critical critical>
382struct PhysicalDisplayVariant
383 : public DisplayVariant<type, type, width, height, critical, Async::FALSE, Secure::TRUE,
384 GRALLOC_USAGE_PHYSICAL_DISPLAY>,
385 public HwcDisplayVariant<hwcDisplayId, HWC2::DisplayType::Physical,
386 DisplayVariant<type, type, width, height, critical, Async::FALSE,
387 Secure::TRUE, GRALLOC_USAGE_PHYSICAL_DISPLAY>> {};
388
389// A primary display is a physical display that is critical
390using PrimaryDisplayVariant =
391 PhysicalDisplayVariant<1001, DisplayDevice::DISPLAY_PRIMARY, 3840, 2160, Critical::TRUE>;
392
393// An external display is physical display that is not critical.
394using ExternalDisplayVariant =
395 PhysicalDisplayVariant<1002, DisplayDevice::DISPLAY_EXTERNAL, 1920, 1280, Critical::FALSE>;
396
397using TertiaryDisplayVariant =
398 PhysicalDisplayVariant<1003, DisplayDevice::DISPLAY_EXTERNAL, 1600, 1200, Critical::FALSE>;
399
400// A virtual display not supported by the HWC.
401constexpr uint32_t GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY = 0;
402
403template <int width, int height, Secure secure>
404struct NonHwcVirtualDisplayVariant
405 : public DisplayVariant<DisplayDevice::DISPLAY_VIRTUAL, DisplayDevice::DISPLAY_ID_INVALID,
406 width, height, Critical::FALSE, Async::TRUE, secure,
407 GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>,
408 public NonHwcDisplayVariant {
409 using Base = DisplayVariant<DisplayDevice::DISPLAY_VIRTUAL, DisplayDevice::DISPLAY_ID_INVALID,
410 width, height, Critical::FALSE, Async::TRUE, secure,
411 GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>;
412
413 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
414 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
415 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
416 }
417};
418
419// A virtual display supported by the HWC.
420constexpr uint32_t GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY = GRALLOC_USAGE_HW_COMPOSER;
421
422template <int width, int height, Secure secure>
423struct HwcVirtualDisplayVariant
424 : public DisplayVariant<DisplayDevice::DISPLAY_VIRTUAL, DisplayDevice::DISPLAY_VIRTUAL, width,
425 height, Critical::FALSE, Async::TRUE, secure,
426 GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>,
427 public HwcDisplayVariant<1010, HWC2::DisplayType::Virtual,
428 NonHwcVirtualDisplayVariant<width, height, secure>> {
429 using Base =
430 DisplayVariant<DisplayDevice::DISPLAY_VIRTUAL, DisplayDevice::DISPLAY_VIRTUAL, width,
431 height, Critical::FALSE, Async::TRUE, secure, GRALLOC_USAGE_HW_COMPOSER>;
432 using Self = HwcVirtualDisplayVariant<width, height, secure>;
433
434 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
435 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
436 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
437 }
438
439 static void setupHwcVirtualDisplayCreationCallExpectations(DisplayTransactionTest* test) {
440 EXPECT_CALL(*test->mComposer, createVirtualDisplay(Base::WIDTH, Base::HEIGHT, _, _))
441 .WillOnce(DoAll(SetArgPointee<3>(Self::HWC_DISPLAY_ID), Return(Error::NONE)));
442 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
443 }
444};
445
446// For this variant, SurfaceFlinger should not configure itself with wide
447// display support, so the display should not be configured for wide-color
448// support.
449struct WideColorSupportNotConfiguredVariant {
450 static constexpr bool WIDE_COLOR_SUPPORTED = false;
451
452 static void injectConfigChange(DisplayTransactionTest* test) {
453 test->mFlinger.mutableHasWideColorDisplay() = false;
454 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
455 }
456
457 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
458 EXPECT_CALL(*test->mComposer, getColorModes(_, _)).Times(0);
459 EXPECT_CALL(*test->mComposer, getRenderIntents(_, _, _)).Times(0);
460 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
461 }
462};
463
464// For this variant, SurfaceFlinger should configure itself with wide display
465// support, and the display should respond with an non-empty list of supported
466// color modes. Wide-color support should be configured.
467template <typename Display>
468struct WideColorP3ColorimetricSupportedVariant {
469 static constexpr bool WIDE_COLOR_SUPPORTED = true;
470
471 static void injectConfigChange(DisplayTransactionTest* test) {
472 test->mFlinger.mutableHasWideColorDisplay() = true;
473 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
474 }
475
476 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
477 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
478 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>({ColorMode::DISPLAY_P3})),
479 Return(Error::NONE)));
480 EXPECT_CALL(*test->mComposer,
481 getRenderIntents(Display::HWC_DISPLAY_ID, ColorMode::DISPLAY_P3, _))
482 .WillOnce(DoAll(SetArgPointee<2>(
483 std::vector<RenderIntent>({RenderIntent::COLORIMETRIC})),
484 Return(Error::NONE)));
485 EXPECT_CALL(*test->mComposer,
486 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::SRGB,
487 RenderIntent::COLORIMETRIC))
488 .WillOnce(Return(Error::NONE));
489 }
490};
491
492// For this variant, SurfaceFlinger should configure itself with wide color
493// display support, and the display should respond with an non-empty list of
494// supported color modes.
495template <typename Display>
496struct WideColorP3EnhanceSupportedVariant {
497 static constexpr bool WIDE_COLOR_SUPPORTED = true;
498
499 static void injectConfigChange(DisplayTransactionTest* test) {
500 test->mFlinger.mutableHasWideColorDisplay() = true;
501 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::ENHANCED;
502 }
503
504 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
505 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
506 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>({ColorMode::DISPLAY_P3})),
507 Return(Error::NONE)));
508 EXPECT_CALL(*test->mComposer,
509 getRenderIntents(Display::HWC_DISPLAY_ID, ColorMode::DISPLAY_P3, _))
510 .WillOnce(
511 DoAll(SetArgPointee<2>(std::vector<RenderIntent>({RenderIntent::ENHANCE})),
512 Return(Error::NONE)));
513 EXPECT_CALL(*test->mComposer,
514 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::SRGB, RenderIntent::ENHANCE))
515 .WillOnce(Return(Error::NONE));
516 }
517};
518
519// For this variant, SurfaceFlinger should configure itself with wide display
520// support, but the display should respond with an empty list of supported color
521// modes. Wide-color support for the display should not be configured.
522template <typename Display>
523struct WideColorNotSupportedVariant {
524 static constexpr bool WIDE_COLOR_SUPPORTED = false;
525
526 static void injectConfigChange(DisplayTransactionTest* test) {
527 test->mFlinger.mutableHasWideColorDisplay() = true;
528 }
529
530 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
531 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
532 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>()), Return(Error::NONE)));
533 EXPECT_CALL(*test->mComposer,
534 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::NATIVE,
535 RenderIntent::COLORIMETRIC))
536 .WillOnce(Return(Error::NONE));
537 }
538};
539
540// For this variant, the display is not a HWC display, so no HDR support should
541// be configured.
542struct NonHwcDisplayHdrSupportVariant {
543 static constexpr bool HDR10_SUPPORTED = false;
544 static constexpr bool HDR_HLG_SUPPORTED = false;
545 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
546 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
547 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
548 }
549};
550
551// For this variant, the composer should respond with a non-empty list of HDR
552// modes containing HDR10, so HDR10 support should be configured.
553template <typename Display>
554struct Hdr10SupportedVariant {
555 static constexpr bool HDR10_SUPPORTED = true;
556 static constexpr bool HDR_HLG_SUPPORTED = false;
557 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
558 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
559 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
560 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HDR10})),
561 Return(Error::NONE)));
562 }
563};
564
565// For this variant, the composer should respond with a non-empty list of HDR
566// modes containing HLG, so HLG support should be configured.
567template <typename Display>
568struct HdrHlgSupportedVariant {
569 static constexpr bool HDR10_SUPPORTED = false;
570 static constexpr bool HDR_HLG_SUPPORTED = true;
571 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
572 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
573 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
574 .WillOnce(
575 DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HLG})), Return(Error::NONE)));
576 }
577};
578
579// For this variant, the composer should respond with a non-empty list of HDR
580// modes containing DOLBY_VISION, so DOLBY_VISION support should be configured.
581template <typename Display>
582struct HdrDolbyVisionSupportedVariant {
583 static constexpr bool HDR10_SUPPORTED = false;
584 static constexpr bool HDR_HLG_SUPPORTED = false;
585 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = true;
586 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
587 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
588 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::DOLBY_VISION})),
589 Return(Error::NONE)));
590 }
591};
592
593// For this variant, the composer should respond with am empty list of HDR
594// modes, so no HDR support should be configured.
595template <typename Display>
596struct HdrNotSupportedVariant {
597 static constexpr bool HDR10_SUPPORTED = false;
598 static constexpr bool HDR_HLG_SUPPORTED = false;
599 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
600 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
601 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
602 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
603 }
604};
605
606/* ------------------------------------------------------------------------
607 * Typical display configurations to test
608 */
609
610template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy>
611struct Case {
612 using Display = DisplayPolicy;
613 using WideColorSupport = WideColorSupportPolicy;
614 using HdrSupport = HdrSupportPolicy;
615};
616
617using SimplePrimaryDisplayCase =
618 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
619 HdrNotSupportedVariant<PrimaryDisplayVariant>>;
620using SimpleExternalDisplayCase =
621 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
622 HdrNotSupportedVariant<ExternalDisplayVariant>>;
623using SimpleTertiaryDisplayCase =
624 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
625 HdrNotSupportedVariant<TertiaryDisplayVariant>>;
626using NonHwcVirtualDisplayCase =
627 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
628 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant>;
629using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
630using HwcVirtualDisplayCase =
631 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
632 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>>;
633using WideColorP3ColorimetricDisplayCase =
634 Case<PrimaryDisplayVariant, WideColorP3ColorimetricSupportedVariant<PrimaryDisplayVariant>,
635 HdrNotSupportedVariant<PrimaryDisplayVariant>>;
636using WideColorP3EnhanceDisplayCase =
637 Case<PrimaryDisplayVariant, WideColorP3EnhanceSupportedVariant<PrimaryDisplayVariant>,
638 HdrNotSupportedVariant<PrimaryDisplayVariant>>;
639using Hdr10DisplayCase =
640 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
641 Hdr10SupportedVariant<PrimaryDisplayVariant>>;
642using HdrHlgDisplayCase =
643 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
644 HdrHlgSupportedVariant<PrimaryDisplayVariant>>;
645using HdrDolbyVisionDisplayCase =
646 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
647 HdrDolbyVisionSupportedVariant<PrimaryDisplayVariant>>;
648
649/* ------------------------------------------------------------------------
Lloyd Pique6cf11032018-01-22 18:57:44 -0800650 *
651 * SurfaceFlinger::onHotplugReceived
652 */
653
654TEST_F(DisplayTransactionTest, hotplugEnqueuesEventsForDisplayTransaction) {
655 constexpr int currentSequenceId = 123;
656 constexpr hwc2_display_t displayId1 = 456;
657 constexpr hwc2_display_t displayId2 = 654;
658
659 // --------------------------------------------------------------------
660 // Preconditions
661
662 // Set the current sequence id for accepted events
663 mFlinger.mutableComposerSequenceId() = currentSequenceId;
664
665 // Set the main thread id so that the current thread does not appear to be
666 // the main thread.
667 mFlinger.mutableMainThreadId() = std::thread::id();
668
669 // --------------------------------------------------------------------
670 // Call Expectations
671
672 // We expect invalidate() to be invoked once to trigger display transaction
673 // processing.
674 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
675
676 // --------------------------------------------------------------------
677 // Invocation
678
679 // Simulate two hotplug events (a connect and a disconnect)
680 mFlinger.onHotplugReceived(currentSequenceId, displayId1, HWC2::Connection::Connected);
681 mFlinger.onHotplugReceived(currentSequenceId, displayId2, HWC2::Connection::Disconnected);
682
683 // --------------------------------------------------------------------
684 // Postconditions
685
686 // The display transaction needed flag should be set.
687 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
688
689 // All events should be in the pending event queue.
690 const auto& pendingEvents = mFlinger.mutablePendingHotplugEvents();
691 ASSERT_EQ(2u, pendingEvents.size());
692 EXPECT_EQ(displayId1, pendingEvents[0].display);
693 EXPECT_EQ(HWC2::Connection::Connected, pendingEvents[0].connection);
694 EXPECT_EQ(displayId2, pendingEvents[1].display);
695 EXPECT_EQ(HWC2::Connection::Disconnected, pendingEvents[1].connection);
696}
697
698TEST_F(DisplayTransactionTest, hotplugDiscardsUnexpectedEvents) {
699 constexpr int currentSequenceId = 123;
700 constexpr int otherSequenceId = 321;
701 constexpr hwc2_display_t displayId = 456;
702
703 // --------------------------------------------------------------------
704 // Preconditions
705
706 // Set the current sequence id for accepted events
707 mFlinger.mutableComposerSequenceId() = currentSequenceId;
708
709 // Set the main thread id so that the current thread does not appear to be
710 // the main thread.
711 mFlinger.mutableMainThreadId() = std::thread::id();
712
713 // --------------------------------------------------------------------
714 // Call Expectations
715
716 // We do not expect any calls to invalidate().
717 EXPECT_CALL(*mMessageQueue, invalidate()).Times(0);
718
719 // --------------------------------------------------------------------
720 // Invocation
721
722 // Call with an unexpected sequence id
723 mFlinger.onHotplugReceived(otherSequenceId, displayId, HWC2::Connection::Invalid);
724
725 // --------------------------------------------------------------------
726 // Postconditions
727
728 // The display transaction needed flag should not be set
729 EXPECT_FALSE(hasTransactionFlagSet(eDisplayTransactionNeeded));
730
731 // There should be no pending events
732 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
733}
734
735TEST_F(DisplayTransactionTest, hotplugProcessesEnqueuedEventsIfCalledOnMainThread) {
736 constexpr int currentSequenceId = 123;
737 constexpr hwc2_display_t displayId1 = 456;
738
739 // --------------------------------------------------------------------
740 // Note:
741 // --------------------------------------------------------------------
742 // This test case is a bit tricky. We want to verify that
743 // onHotplugReceived() calls processDisplayHotplugEventsLocked(), but we
744 // don't really want to provide coverage for everything the later function
745 // does as there are specific tests for it.
746 // --------------------------------------------------------------------
747
748 // --------------------------------------------------------------------
749 // Preconditions
750
751 // Set the current sequence id for accepted events
752 mFlinger.mutableComposerSequenceId() = currentSequenceId;
753
754 // Set the main thread id so that the current thread does appear to be the
755 // main thread.
756 mFlinger.mutableMainThreadId() = std::this_thread::get_id();
757
758 // --------------------------------------------------------------------
759 // Call Expectations
760
761 // We expect invalidate() to be invoked once to trigger display transaction
762 // processing.
763 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
764
765 // --------------------------------------------------------------------
766 // Invocation
767
768 // Simulate a disconnect on a display id that is not connected. This should
769 // be enqueued by onHotplugReceived(), and dequeued by
770 // processDisplayHotplugEventsLocked(), but then ignored as invalid.
771 mFlinger.onHotplugReceived(currentSequenceId, displayId1, HWC2::Connection::Disconnected);
772
773 // --------------------------------------------------------------------
774 // Postconditions
775
776 // The display transaction needed flag should be set.
777 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
778
779 // There should be no event queued on return, as it should have been
780 // processed.
781 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
782}
783
784/* ------------------------------------------------------------------------
Lloyd Piquea482f992018-01-22 19:00:34 -0800785 * SurfaceFlinger::createDisplay
786 */
787
788TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForNonsecureDisplay) {
789 const String8 name("virtual.test");
790
791 // --------------------------------------------------------------------
792 // Call Expectations
793
794 // The call should notify the interceptor that a display was created.
795 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
796
797 // --------------------------------------------------------------------
798 // Invocation
799
800 sp<IBinder> displayToken = mFlinger.createDisplay(name, false);
801
802 // --------------------------------------------------------------------
803 // Postconditions
804
805 // The display should have been added to the current state
806 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
807 const auto& display = getCurrentDisplayState(displayToken);
808 EXPECT_EQ(DisplayDevice::DISPLAY_VIRTUAL, display.type);
809 EXPECT_EQ(false, display.isSecure);
810 EXPECT_EQ(name.string(), display.displayName);
811
812 // --------------------------------------------------------------------
813 // Cleanup conditions
814
815 // Destroying the display invalidates the display state.
816 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
817}
818
819TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForSecureDisplay) {
820 const String8 name("virtual.test");
821
822 // --------------------------------------------------------------------
823 // Call Expectations
824
825 // The call should notify the interceptor that a display was created.
826 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
827
828 // --------------------------------------------------------------------
829 // Invocation
830
831 sp<IBinder> displayToken = mFlinger.createDisplay(name, true);
832
833 // --------------------------------------------------------------------
834 // Postconditions
835
836 // The display should have been added to the current state
837 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
838 const auto& display = getCurrentDisplayState(displayToken);
839 EXPECT_EQ(DisplayDevice::DISPLAY_VIRTUAL, display.type);
840 EXPECT_EQ(true, display.isSecure);
841 EXPECT_EQ(name.string(), display.displayName);
842
843 // --------------------------------------------------------------------
844 // Cleanup conditions
845
846 // Destroying the display invalidates the display state.
847 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
848}
849
850/* ------------------------------------------------------------------------
851 * SurfaceFlinger::destroyDisplay
852 */
853
854TEST_F(DisplayTransactionTest, destroyDisplayClearsCurrentStateForDisplay) {
855 using Case = NonHwcVirtualDisplayCase;
856
857 // --------------------------------------------------------------------
858 // Preconditions
859
860 // A virtual display exists
861 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
862 existing.inject();
863
864 // --------------------------------------------------------------------
865 // Call Expectations
866
867 // The call should notify the interceptor that a display was created.
868 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
869
870 // Destroying the display invalidates the display state.
871 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
872
873 // --------------------------------------------------------------------
874 // Invocation
875
876 mFlinger.destroyDisplay(existing.token());
877
878 // --------------------------------------------------------------------
879 // Postconditions
880
881 // The display should have been removed from the current state
882 EXPECT_FALSE(hasCurrentDisplayState(existing.token()));
883
884 // Ths display should still exist in the drawing state
885 EXPECT_TRUE(hasDrawingDisplayState(existing.token()));
886
887 // The display transaction needed flasg should be set
888 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
889}
890
891TEST_F(DisplayTransactionTest, destroyDisplayHandlesUnknownDisplay) {
892 // --------------------------------------------------------------------
893 // Preconditions
894
895 sp<BBinder> displayToken = new BBinder();
896
897 // --------------------------------------------------------------------
898 // Invocation
899
900 mFlinger.destroyDisplay(displayToken);
901}
902
903/* ------------------------------------------------------------------------
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -0800904 * SurfaceFlinger::resetDisplayState
905 */
906
907TEST_F(DisplayTransactionTest, resetDisplayStateClearsState) {
908 using Case = NonHwcVirtualDisplayCase;
909
910 // --------------------------------------------------------------------
911 // Preconditions
912
913 // vsync is enabled and available
914 mFlinger.mutablePrimaryHWVsyncEnabled() = true;
915 mFlinger.mutableHWVsyncAvailable() = true;
916
917 // A display exists
918 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
919 existing.inject();
920
921 // --------------------------------------------------------------------
922 // Call Expectations
923
924 // The call disable vsyncs
925 EXPECT_CALL(*mEventControlThread, setVsyncEnabled(false)).Times(1);
926
927 // The call clears the current render engine surface
928 EXPECT_CALL(*mRenderEngine, resetCurrentSurface());
929
930 // --------------------------------------------------------------------
931 // Invocation
932
933 mFlinger.resetDisplayState();
934
935 // --------------------------------------------------------------------
936 // Postconditions
937
938 // vsyncs should be off and not available.
939 EXPECT_FALSE(mFlinger.mutablePrimaryHWVsyncEnabled());
940 EXPECT_FALSE(mFlinger.mutableHWVsyncAvailable());
941
942 // The display should have been removed from the display map.
943 EXPECT_FALSE(hasDisplayDevice(existing.token()));
944
945 // The display should still exist in the current state
946 EXPECT_TRUE(hasCurrentDisplayState(existing.token()));
947
948 // The display should have been removed from the drawing state
949 EXPECT_FALSE(hasDrawingDisplayState(existing.token()));
950}
951
952/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800953 * SurfaceFlinger::setupNewDisplayDeviceInternal
954 */
955
956class SetupNewDisplayDeviceInternalTest : public DisplayTransactionTest {
957public:
958 template <typename T>
959 void setupNewDisplayDeviceInternalTest();
960};
961
962template <typename Case>
963void SetupNewDisplayDeviceInternalTest::setupNewDisplayDeviceInternalTest() {
964 const sp<BBinder> displayToken = new BBinder();
965 const sp<mock::DisplaySurface> displaySurface = new mock::DisplaySurface();
966 const sp<mock::GraphicBufferProducer> producer = new mock::GraphicBufferProducer();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800967
968 // --------------------------------------------------------------------
969 // Preconditions
970
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800971 // Wide color displays support is configured appropriately
972 Case::WideColorSupport::injectConfigChange(this);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800973
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800974 // The display is setup with the HWC.
975 Case::Display::injectHwcDisplay(this);
976
977 // SurfaceFlinger will use a test-controlled factory for native window
978 // surfaces.
979 injectFakeNativeWindowSurfaceFactory();
980
981 // --------------------------------------------------------------------
982 // Call Expectations
983
984 // Various native window calls will be made.
985 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
986
987 // TODO(b/69807179): SurfaceFlinger does not correctly get the active config.
988 // Case::Display::setupHwcGetActiveConfigCallExpectations(this)
989
990 Case::WideColorSupport::setupComposerCallExpectations(this);
991 Case::HdrSupport::setupComposerCallExpectations(this);
992
993 // --------------------------------------------------------------------
994 // Invocation
995
996 DisplayDeviceState state;
997 state.type = Case::Display::TYPE;
998 state.isSecure = static_cast<bool>(Case::Display::SECURE);
999
1000 auto device = mFlinger.setupNewDisplayDeviceInternal(displayToken, Case::Display::TYPE, state,
1001 displaySurface, producer);
1002
1003 // --------------------------------------------------------------------
1004 // Postconditions
1005
1006 ASSERT_TRUE(device != nullptr);
1007 EXPECT_EQ(Case::Display::TYPE, device->getDisplayType());
1008 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
1009 EXPECT_EQ(Case::Display::WIDTH, device->getWidth());
1010 EXPECT_EQ(Case::Display::HEIGHT, device->getHeight());
1011 EXPECT_EQ(Case::WideColorSupport::WIDE_COLOR_SUPPORTED, device->hasWideColorGamut());
1012 EXPECT_EQ(Case::HdrSupport::HDR10_SUPPORTED, device->hasHDR10Support());
1013 EXPECT_EQ(Case::HdrSupport::HDR_HLG_SUPPORTED, device->hasHLGSupport());
1014 EXPECT_EQ(Case::HdrSupport::HDR_DOLBY_VISION_SUPPORTED, device->hasDolbyVisionSupport());
1015 EXPECT_EQ(Case::Display::HWC_ACTIVE_CONFIG_ID, device->getActiveConfig());
1016}
1017
1018TEST_F(SetupNewDisplayDeviceInternalTest, createSimplePrimaryDisplay) {
1019 setupNewDisplayDeviceInternalTest<SimplePrimaryDisplayCase>();
1020}
1021
1022TEST_F(SetupNewDisplayDeviceInternalTest, createSimpleExternalDisplay) {
1023 setupNewDisplayDeviceInternalTest<SimpleExternalDisplayCase>();
1024}
1025
1026TEST_F(SetupNewDisplayDeviceInternalTest, createNonHwcVirtualDisplay) {
1027 setupNewDisplayDeviceInternalTest<NonHwcVirtualDisplayCase>();
1028}
1029
1030TEST_F(SetupNewDisplayDeviceInternalTest, createHwcVirtualDisplay) {
1031 // We need to resize this so that the HWC thinks the virtual display
1032 // is something it created.
1033 mFlinger.mutableHwcDisplayData().resize(3);
1034
1035 setupNewDisplayDeviceInternalTest<HwcVirtualDisplayCase>();
1036}
1037
1038TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3Display) {
1039 setupNewDisplayDeviceInternalTest<WideColorP3ColorimetricDisplayCase>();
1040}
1041
1042TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3EnhanceDisplay) {
1043 setupNewDisplayDeviceInternalTest<WideColorP3EnhanceDisplayCase>();
1044}
1045
1046TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10Display) {
1047 setupNewDisplayDeviceInternalTest<Hdr10DisplayCase>();
1048}
1049
1050TEST_F(SetupNewDisplayDeviceInternalTest, createHdrHlgDisplay) {
1051 setupNewDisplayDeviceInternalTest<HdrHlgDisplayCase>();
1052}
1053
1054TEST_F(SetupNewDisplayDeviceInternalTest, createHdrDolbyVisionDisplay) {
1055 setupNewDisplayDeviceInternalTest<HdrDolbyVisionDisplayCase>();
1056}
1057
1058/* ------------------------------------------------------------------------
1059 * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded)
1060 */
1061
1062class HandleTransactionLockedTest : public DisplayTransactionTest {
1063public:
1064 template <typename Case>
1065 void setupCommonPreconditions();
1066
1067 template <typename Case>
1068 void setupCommonCallExpectationsForConnectProcessing();
1069
1070 template <typename Case>
1071 void setupCommonCallExpectationsForDisconnectProcessing();
1072
1073 template <typename Case>
1074 void processesHotplugConnectCommon();
1075
1076 template <typename Case>
1077 void ignoresHotplugConnectCommon();
1078
1079 template <typename Case>
1080 void processesHotplugDisconnectCommon();
1081
1082 template <typename Case>
1083 void verifyDisplayIsConnected(const sp<IBinder>& displayToken);
1084
1085 template <typename Case>
1086 void verifyPhysicalDisplayIsConnected();
1087
1088 void verifyDisplayIsNotConnected(const sp<IBinder>& displayToken);
1089};
1090
1091template <typename Case>
1092void HandleTransactionLockedTest::setupCommonPreconditions() {
1093 // Wide color displays support is configured appropriately
1094 Case::WideColorSupport::injectConfigChange(this);
1095
1096 // SurfaceFlinger will use a test-controlled factory for BufferQueues
1097 injectFakeBufferQueueFactory();
1098
1099 // SurfaceFlinger will use a test-controlled factory for native window
1100 // surfaces.
1101 injectFakeNativeWindowSurfaceFactory();
1102}
1103
1104template <typename Case>
1105void HandleTransactionLockedTest::setupCommonCallExpectationsForConnectProcessing() {
1106 Case::Display::setupHwcHotplugCallExpectations(this);
1107
1108 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1109 Case::Display::setupFramebufferProducerBufferQueueCallExpectations(this);
1110 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1111 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
1112
1113 Case::WideColorSupport::setupComposerCallExpectations(this);
1114 Case::HdrSupport::setupComposerCallExpectations(this);
1115
1116 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
1117 EXPECT_CALL(*mEventThread, onHotplugReceived(Case::Display::TYPE, true)).Times(1);
1118}
1119
1120template <typename Case>
1121void HandleTransactionLockedTest::setupCommonCallExpectationsForDisconnectProcessing() {
1122 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
1123 EXPECT_CALL(*mEventThread, onHotplugReceived(Case::Display::TYPE, false)).Times(1);
1124}
1125
1126template <typename Case>
1127void HandleTransactionLockedTest::verifyDisplayIsConnected(const sp<IBinder>& displayToken) {
1128 // The display device should have been set up in the list of displays.
1129 ASSERT_TRUE(hasDisplayDevice(displayToken));
1130 const auto& device = getDisplayDevice(displayToken);
1131 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
1132 EXPECT_EQ(Case::Display::TYPE == DisplayDevice::DISPLAY_PRIMARY, device->isPrimary());
1133
1134 // The display should have been set up in the current display state
1135 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1136 const auto& current = getCurrentDisplayState(displayToken);
1137 EXPECT_EQ(Case::Display::TYPE, current.type);
1138
1139 // The display should have been set up in the drawing display state
1140 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1141 const auto& draw = getDrawingDisplayState(displayToken);
1142 EXPECT_EQ(Case::Display::TYPE, draw.type);
1143}
1144
1145template <typename Case>
1146void HandleTransactionLockedTest::verifyPhysicalDisplayIsConnected() {
1147 // HWComposer should have an entry for the display
1148 EXPECT_TRUE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1149
1150 // The display should be set up as a built-in display.
1151 static_assert(0 <= Case::Display::TYPE &&
1152 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES,
1153 "Must use a valid physical display type index for the fixed-size array");
1154 auto& displayToken = mFlinger.mutableBuiltinDisplays()[Case::Display::TYPE];
1155 ASSERT_TRUE(displayToken != nullptr);
1156
1157 verifyDisplayIsConnected<Case>(displayToken);
1158}
1159
1160void HandleTransactionLockedTest::verifyDisplayIsNotConnected(const sp<IBinder>& displayToken) {
1161 EXPECT_FALSE(hasDisplayDevice(displayToken));
1162 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1163 EXPECT_FALSE(hasDrawingDisplayState(displayToken));
1164}
1165
1166template <typename Case>
1167void HandleTransactionLockedTest::processesHotplugConnectCommon() {
1168 // --------------------------------------------------------------------
1169 // Preconditions
1170
1171 setupCommonPreconditions<Case>();
1172
1173 // A hotplug connect event is enqueued for a display
1174 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001175
1176 // --------------------------------------------------------------------
1177 // Call Expectations
1178
1179 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillOnce(Return(false));
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001180
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001181 setupCommonCallExpectationsForConnectProcessing<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001182
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001183 // --------------------------------------------------------------------
1184 // Invocation
Lloyd Piquee39cad22017-12-20 17:01:29 -08001185
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001186 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
Lloyd Piquee39cad22017-12-20 17:01:29 -08001187
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001188 // --------------------------------------------------------------------
1189 // Postconditions
1190
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001191 verifyPhysicalDisplayIsConnected<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001192
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001193 // --------------------------------------------------------------------
1194 // Cleanup conditions
1195
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001196 EXPECT_CALL(*mComposer,
1197 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001198 .WillOnce(Return(Error::NONE));
1199 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Piquef58625d2017-12-19 13:22:33 -08001200}
1201
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001202template <typename Case>
1203void HandleTransactionLockedTest::ignoresHotplugConnectCommon() {
1204 // --------------------------------------------------------------------
1205 // Preconditions
1206
1207 setupCommonPreconditions<Case>();
1208
1209 // A hotplug connect event is enqueued for a display
1210 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1211
1212 // --------------------------------------------------------------------
1213 // Invocation
1214
1215 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1216
1217 // --------------------------------------------------------------------
1218 // Postconditions
1219
1220 // HWComposer should not have an entry for the display
1221 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1222}
1223
1224template <typename Case>
1225void HandleTransactionLockedTest::processesHotplugDisconnectCommon() {
1226 // --------------------------------------------------------------------
1227 // Preconditions
1228
1229 setupCommonPreconditions<Case>();
1230
1231 // A hotplug disconnect event is enqueued for a display
1232 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1233
1234 // The display is already completely set up.
1235 Case::Display::injectHwcDisplay(this);
1236 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1237 existing.inject();
1238
1239 // --------------------------------------------------------------------
1240 // Call Expectations
1241
1242 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1243
1244 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1245
1246 // --------------------------------------------------------------------
1247 // Invocation
1248
1249 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1250
1251 // --------------------------------------------------------------------
1252 // Postconditions
1253
1254 // HWComposer should not have an entry for the display
1255 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1256
1257 // The display should not be set up as a built-in display.
1258 ASSERT_TRUE(0 <= Case::Display::TYPE &&
1259 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES);
1260 auto displayToken = mFlinger.mutableBuiltinDisplays()[Case::Display::TYPE];
1261 EXPECT_TRUE(displayToken == nullptr);
1262
1263 // The existing token should have been removed
1264 verifyDisplayIsNotConnected(existing.token());
1265}
1266
1267TEST_F(HandleTransactionLockedTest, processesHotplugConnectPrimaryDisplay) {
1268 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1269}
1270
1271TEST_F(HandleTransactionLockedTest,
1272 processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected) {
1273 // Inject an external display.
1274 ExternalDisplayVariant::injectHwcDisplay(this);
1275
1276 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1277}
1278
1279TEST_F(HandleTransactionLockedTest, processesHotplugConnectExternalDisplay) {
1280 // Inject a primary display.
1281 PrimaryDisplayVariant::injectHwcDisplay(this);
1282
1283 processesHotplugConnectCommon<SimpleExternalDisplayCase>();
1284}
1285
1286TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected) {
1287 // Inject both a primary and external display.
1288 PrimaryDisplayVariant::injectHwcDisplay(this);
1289 ExternalDisplayVariant::injectHwcDisplay(this);
1290
1291 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1292
1293 ignoresHotplugConnectCommon<SimpleTertiaryDisplayCase>();
1294}
1295
1296TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfExternalForVrComposer) {
1297 // Inject a primary display.
1298 PrimaryDisplayVariant::injectHwcDisplay(this);
1299
1300 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(true));
1301
1302 ignoresHotplugConnectCommon<SimpleExternalDisplayCase>();
1303}
1304
1305TEST_F(HandleTransactionLockedTest, processHotplugDisconnectPrimaryDisplay) {
1306 processesHotplugDisconnectCommon<SimplePrimaryDisplayCase>();
1307}
1308
1309TEST_F(HandleTransactionLockedTest, processHotplugDisconnectExternalDisplay) {
1310 processesHotplugDisconnectCommon<SimpleExternalDisplayCase>();
1311}
1312
1313TEST_F(HandleTransactionLockedTest, processesHotplugConnectThenDisconnectPrimary) {
1314 using Case = SimplePrimaryDisplayCase;
1315
1316 // --------------------------------------------------------------------
1317 // Preconditions
1318
1319 setupCommonPreconditions<Case>();
1320
1321 // A hotplug connect event is enqueued for a display
1322 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1323 // A hotplug disconnect event is also enqueued for the same display
1324 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1325
1326 // --------------------------------------------------------------------
1327 // Call Expectations
1328
1329 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1330
1331 setupCommonCallExpectationsForConnectProcessing<Case>();
1332 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1333
1334 EXPECT_CALL(*mComposer,
1335 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1336 .WillOnce(Return(Error::NONE));
1337 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1338
1339 // --------------------------------------------------------------------
1340 // Invocation
1341
1342 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1343
1344 // --------------------------------------------------------------------
1345 // Postconditions
1346
1347 // HWComposer should not have an entry for the display
1348 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1349
1350 // The display should not be set up as a primary built-in display.
1351 ASSERT_TRUE(0 <= Case::Display::TYPE &&
1352 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES);
1353 auto displayToken = mFlinger.mutableBuiltinDisplays()[Case::Display::TYPE];
1354 EXPECT_TRUE(displayToken == nullptr);
1355}
1356
1357TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectThenConnectPrimary) {
1358 using Case = SimplePrimaryDisplayCase;
1359
1360 // --------------------------------------------------------------------
1361 // Preconditions
1362
1363 setupCommonPreconditions<Case>();
1364
1365 // The display is already completely set up.
1366 Case::Display::injectHwcDisplay(this);
1367 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1368 existing.inject();
1369
1370 // A hotplug disconnect event is enqueued for a display
1371 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1372 // A hotplug connect event is also enqueued for the same display
1373 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1374
1375 // --------------------------------------------------------------------
1376 // Call Expectations
1377
1378 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1379
1380 setupCommonCallExpectationsForConnectProcessing<Case>();
1381 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1382
1383 // --------------------------------------------------------------------
1384 // Invocation
1385
1386 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1387
1388 // --------------------------------------------------------------------
1389 // Postconditions
1390
1391 // The existing token should have been removed
1392 verifyDisplayIsNotConnected(existing.token());
1393 static_assert(0 <= Case::Display::TYPE &&
1394 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES,
1395 "Display type must be a built-in display");
1396 EXPECT_NE(existing.token(), mFlinger.mutableBuiltinDisplays()[Case::Display::TYPE]);
1397
1398 // A new display should be connected in its place
1399
1400 verifyPhysicalDisplayIsConnected<Case>();
1401
1402 // --------------------------------------------------------------------
1403 // Cleanup conditions
1404
1405 EXPECT_CALL(*mComposer,
1406 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1407 .WillOnce(Return(Error::NONE));
1408 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1409}
1410
1411TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAdded) {
1412 using Case = HwcVirtualDisplayCase;
1413
1414 // --------------------------------------------------------------------
1415 // Preconditions
1416
1417 // The HWC supports at least one virtual display
1418 injectMockComposer(1);
1419
1420 setupCommonPreconditions<Case>();
1421
1422 // A virtual display was added to the current state, and it has a
1423 // surface(producer)
1424 sp<BBinder> displayToken = new BBinder();
Lloyd Pique4c2ac022018-04-27 12:08:03 -07001425
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001426 DisplayDeviceState info;
1427 info.type = Case::Display::TYPE;
1428 info.isSecure = static_cast<bool>(Case::Display::SECURE);
1429
1430 sp<mock::GraphicBufferProducer> surface{new mock::GraphicBufferProducer()};
1431 info.surface = surface;
1432 mFlinger.mutableCurrentState().displays.add(displayToken, info);
1433
1434 // --------------------------------------------------------------------
1435 // Call Expectations
1436
1437 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1438 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1439
1440 EXPECT_CALL(*surface, query(NATIVE_WINDOW_WIDTH, _))
1441 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::WIDTH), Return(NO_ERROR)));
1442 EXPECT_CALL(*surface, query(NATIVE_WINDOW_HEIGHT, _))
1443 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::HEIGHT), Return(NO_ERROR)));
1444 EXPECT_CALL(*surface, query(NATIVE_WINDOW_FORMAT, _))
1445 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT),
1446 Return(NO_ERROR)));
1447 EXPECT_CALL(*surface, query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, _))
1448 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(NO_ERROR)));
1449
1450 EXPECT_CALL(*surface, setAsyncMode(true)).Times(1);
1451
1452 EXPECT_CALL(*mProducer, connect(_, _, _, _)).Times(1);
1453 EXPECT_CALL(*mProducer, disconnect(_, _)).Times(1);
1454
1455 Case::Display::setupHwcVirtualDisplayCreationCallExpectations(this);
1456 Case::WideColorSupport::setupComposerCallExpectations(this);
1457 Case::HdrSupport::setupComposerCallExpectations(this);
1458
1459 // --------------------------------------------------------------------
1460 // Invocation
1461
1462 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1463
1464 // --------------------------------------------------------------------
1465 // Postconditions
1466
1467 // The display device should have been set up in the list of displays.
1468 verifyDisplayIsConnected<Case>(displayToken);
1469
1470 // --------------------------------------------------------------------
1471 // Cleanup conditions
1472
1473 EXPECT_CALL(*mComposer, destroyVirtualDisplay(Case::Display::HWC_DISPLAY_ID))
1474 .WillOnce(Return(Error::NONE));
1475 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1476}
1477
1478TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAddedWithNoSurface) {
1479 using Case = HwcVirtualDisplayCase;
1480
1481 // --------------------------------------------------------------------
1482 // Preconditions
1483
1484 // The HWC supports at least one virtual display
1485 injectMockComposer(1);
1486
1487 setupCommonPreconditions<Case>();
1488
1489 // A virtual display was added to the current state, but it does not have a
1490 // surface.
1491 sp<BBinder> displayToken = new BBinder();
1492
1493 DisplayDeviceState info;
1494 info.type = Case::Display::TYPE;
1495 info.isSecure = static_cast<bool>(Case::Display::SECURE);
1496
1497 mFlinger.mutableCurrentState().displays.add(displayToken, info);
1498
1499 // --------------------------------------------------------------------
1500 // Call Expectations
1501
1502 // --------------------------------------------------------------------
1503 // Invocation
1504
1505 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1506
1507 // --------------------------------------------------------------------
1508 // Postconditions
1509
1510 // There will not be a display device set up.
1511 EXPECT_FALSE(hasDisplayDevice(displayToken));
1512
1513 // The drawing display state will be set from the current display state.
1514 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1515 const auto& draw = getDrawingDisplayState(displayToken);
1516 EXPECT_EQ(Case::Display::TYPE, draw.type);
1517}
1518
1519TEST_F(HandleTransactionLockedTest, processesVirtualDisplayRemoval) {
1520 using Case = HwcVirtualDisplayCase;
1521
1522 // --------------------------------------------------------------------
1523 // Preconditions
1524
1525 // A virtual display is set up but is removed from the current state.
1526 mFlinger.mutableHwcDisplayData().resize(3);
1527 Case::Display::injectHwcDisplay(this);
1528 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1529 existing.inject();
1530 mFlinger.mutableCurrentState().displays.removeItem(existing.token());
1531
1532 // --------------------------------------------------------------------
1533 // Call Expectations
1534
1535 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1536
1537 // --------------------------------------------------------------------
1538 // Invocation
1539
1540 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1541
1542 // --------------------------------------------------------------------
1543 // Postconditions
1544
1545 // The existing token should have been removed
1546 verifyDisplayIsNotConnected(existing.token());
1547}
1548
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001549TEST_F(HandleTransactionLockedTest, processesDisplayLayerStackChanges) {
1550 using Case = NonHwcVirtualDisplayCase;
1551
1552 constexpr uint32_t oldLayerStack = 0u;
1553 constexpr uint32_t newLayerStack = 123u;
1554
1555 // --------------------------------------------------------------------
1556 // Preconditions
1557
1558 // A display is set up
1559 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1560 display.inject();
1561
1562 // There is a change to the layerStack state
1563 display.mutableDrawingDisplayState().layerStack = oldLayerStack;
1564 display.mutableCurrentDisplayState().layerStack = newLayerStack;
1565
1566 // --------------------------------------------------------------------
1567 // Invocation
1568
1569 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1570
1571 // --------------------------------------------------------------------
1572 // Postconditions
1573
1574 EXPECT_EQ(newLayerStack, getDisplayDevice(display.token())->getLayerStack());
1575}
1576
1577TEST_F(HandleTransactionLockedTest, processesDisplayTransformChanges) {
1578 using Case = NonHwcVirtualDisplayCase;
1579
1580 constexpr int oldTransform = 0;
1581 constexpr int newTransform = 2;
1582
1583 // --------------------------------------------------------------------
1584 // Preconditions
1585
1586 // A display is set up
1587 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1588 display.inject();
1589
1590 // There is a change to the orientation state
1591 display.mutableDrawingDisplayState().orientation = oldTransform;
1592 display.mutableCurrentDisplayState().orientation = newTransform;
1593
1594 // --------------------------------------------------------------------
1595 // Invocation
1596
1597 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1598
1599 // --------------------------------------------------------------------
1600 // Postconditions
1601
1602 EXPECT_EQ(newTransform, getDisplayDevice(display.token())->getOrientation());
1603}
1604
1605TEST_F(HandleTransactionLockedTest, processesDisplayViewportChanges) {
1606 using Case = NonHwcVirtualDisplayCase;
1607
1608 const Rect oldViewport(0, 0, 0, 0);
1609 const Rect newViewport(0, 0, 123, 456);
1610
1611 // --------------------------------------------------------------------
1612 // Preconditions
1613
1614 // A display is set up
1615 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1616 display.inject();
1617
1618 // There is a change to the viewport state
1619 display.mutableDrawingDisplayState().viewport = oldViewport;
1620 display.mutableCurrentDisplayState().viewport = newViewport;
1621
1622 // --------------------------------------------------------------------
1623 // Invocation
1624
1625 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1626
1627 // --------------------------------------------------------------------
1628 // Postconditions
1629
1630 EXPECT_EQ(newViewport, getDisplayDevice(display.token())->getViewport());
1631}
1632
1633TEST_F(HandleTransactionLockedTest, processesDisplayFrameChanges) {
1634 using Case = NonHwcVirtualDisplayCase;
1635
1636 const Rect oldFrame(0, 0, 0, 0);
1637 const Rect newFrame(0, 0, 123, 456);
1638
1639 // --------------------------------------------------------------------
1640 // Preconditions
1641
1642 // A display is set up
1643 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1644 display.inject();
1645
1646 // There is a change to the viewport state
1647 display.mutableDrawingDisplayState().frame = oldFrame;
1648 display.mutableCurrentDisplayState().frame = newFrame;
1649
1650 // --------------------------------------------------------------------
1651 // Invocation
1652
1653 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1654
1655 // --------------------------------------------------------------------
1656 // Postconditions
1657
1658 EXPECT_EQ(newFrame, getDisplayDevice(display.token())->getFrame());
1659}
1660
1661TEST_F(HandleTransactionLockedTest, processesDisplayWidthChanges) {
1662 using Case = NonHwcVirtualDisplayCase;
1663
1664 constexpr int oldWidth = 0;
1665 constexpr int oldHeight = 10;
1666 constexpr int newWidth = 123;
1667
1668 // --------------------------------------------------------------------
1669 // Preconditions
1670
1671 // A display is set up
1672 auto nativeWindow = new mock::NativeWindow();
1673 auto displaySurface = new mock::DisplaySurface();
1674 auto renderSurface = new RE::mock::Surface();
1675 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1676 display.setNativeWindow(nativeWindow);
1677 display.setDisplaySurface(displaySurface);
1678 display.setRenderSurface(std::unique_ptr<RE::Surface>(renderSurface));
1679 display.inject();
1680
1681 // There is a change to the viewport state
1682 display.mutableDrawingDisplayState().width = oldWidth;
1683 display.mutableDrawingDisplayState().height = oldHeight;
1684 display.mutableCurrentDisplayState().width = newWidth;
1685 display.mutableCurrentDisplayState().height = oldHeight;
1686
1687 // --------------------------------------------------------------------
1688 // Call Expectations
1689
1690 EXPECT_CALL(*renderSurface, setNativeWindow(nullptr)).Times(1);
1691 EXPECT_CALL(*displaySurface, resizeBuffers(newWidth, oldHeight)).Times(1);
1692 EXPECT_CALL(*renderSurface, setNativeWindow(nativeWindow)).Times(1);
1693 EXPECT_CALL(*renderSurface, queryWidth()).WillOnce(Return(newWidth));
1694 EXPECT_CALL(*renderSurface, queryHeight()).WillOnce(Return(oldHeight));
1695
1696 // --------------------------------------------------------------------
1697 // Invocation
1698
1699 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1700}
1701
1702TEST_F(HandleTransactionLockedTest, processesDisplayHeightChanges) {
1703 using Case = NonHwcVirtualDisplayCase;
1704
1705 constexpr int oldWidth = 0;
1706 constexpr int oldHeight = 10;
1707 constexpr int newHeight = 123;
1708
1709 // --------------------------------------------------------------------
1710 // Preconditions
1711
1712 // A display is set up
1713 auto nativeWindow = new mock::NativeWindow();
1714 auto displaySurface = new mock::DisplaySurface();
1715 auto renderSurface = new RE::mock::Surface();
1716 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1717 display.setNativeWindow(nativeWindow);
1718 display.setDisplaySurface(displaySurface);
1719 display.setRenderSurface(std::unique_ptr<RE::Surface>(renderSurface));
1720 display.inject();
1721
1722 // There is a change to the viewport state
1723 display.mutableDrawingDisplayState().width = oldWidth;
1724 display.mutableDrawingDisplayState().height = oldHeight;
1725 display.mutableCurrentDisplayState().width = oldWidth;
1726 display.mutableCurrentDisplayState().height = newHeight;
1727
1728 // --------------------------------------------------------------------
1729 // Call Expectations
1730
1731 EXPECT_CALL(*renderSurface, setNativeWindow(nullptr)).Times(1);
1732 EXPECT_CALL(*displaySurface, resizeBuffers(oldWidth, newHeight)).Times(1);
1733 EXPECT_CALL(*renderSurface, setNativeWindow(nativeWindow)).Times(1);
1734 EXPECT_CALL(*renderSurface, queryWidth()).WillOnce(Return(oldWidth));
1735 EXPECT_CALL(*renderSurface, queryHeight()).WillOnce(Return(newHeight));
1736
1737 // --------------------------------------------------------------------
1738 // Invocation
1739
1740 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1741}
1742
Lloyd Piquef58625d2017-12-19 13:22:33 -08001743} // namespace
1744} // namespace android