blob: 3a9cfd237d7d8557625c21e299b4b6decf8c6a89 [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/* ------------------------------------------------------------------------
650 * SurfaceFlinger::setupNewDisplayDeviceInternal
651 */
652
653class SetupNewDisplayDeviceInternalTest : public DisplayTransactionTest {
654public:
655 template <typename T>
656 void setupNewDisplayDeviceInternalTest();
657};
658
659template <typename Case>
660void SetupNewDisplayDeviceInternalTest::setupNewDisplayDeviceInternalTest() {
661 const sp<BBinder> displayToken = new BBinder();
662 const sp<mock::DisplaySurface> displaySurface = new mock::DisplaySurface();
663 const sp<mock::GraphicBufferProducer> producer = new mock::GraphicBufferProducer();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800664
665 // --------------------------------------------------------------------
666 // Preconditions
667
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800668 // Wide color displays support is configured appropriately
669 Case::WideColorSupport::injectConfigChange(this);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800670
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800671 // The display is setup with the HWC.
672 Case::Display::injectHwcDisplay(this);
673
674 // SurfaceFlinger will use a test-controlled factory for native window
675 // surfaces.
676 injectFakeNativeWindowSurfaceFactory();
677
678 // --------------------------------------------------------------------
679 // Call Expectations
680
681 // Various native window calls will be made.
682 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
683
684 // TODO(b/69807179): SurfaceFlinger does not correctly get the active config.
685 // Case::Display::setupHwcGetActiveConfigCallExpectations(this)
686
687 Case::WideColorSupport::setupComposerCallExpectations(this);
688 Case::HdrSupport::setupComposerCallExpectations(this);
689
690 // --------------------------------------------------------------------
691 // Invocation
692
693 DisplayDeviceState state;
694 state.type = Case::Display::TYPE;
695 state.isSecure = static_cast<bool>(Case::Display::SECURE);
696
697 auto device = mFlinger.setupNewDisplayDeviceInternal(displayToken, Case::Display::TYPE, state,
698 displaySurface, producer);
699
700 // --------------------------------------------------------------------
701 // Postconditions
702
703 ASSERT_TRUE(device != nullptr);
704 EXPECT_EQ(Case::Display::TYPE, device->getDisplayType());
705 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
706 EXPECT_EQ(Case::Display::WIDTH, device->getWidth());
707 EXPECT_EQ(Case::Display::HEIGHT, device->getHeight());
708 EXPECT_EQ(Case::WideColorSupport::WIDE_COLOR_SUPPORTED, device->hasWideColorGamut());
709 EXPECT_EQ(Case::HdrSupport::HDR10_SUPPORTED, device->hasHDR10Support());
710 EXPECT_EQ(Case::HdrSupport::HDR_HLG_SUPPORTED, device->hasHLGSupport());
711 EXPECT_EQ(Case::HdrSupport::HDR_DOLBY_VISION_SUPPORTED, device->hasDolbyVisionSupport());
712 EXPECT_EQ(Case::Display::HWC_ACTIVE_CONFIG_ID, device->getActiveConfig());
713}
714
715TEST_F(SetupNewDisplayDeviceInternalTest, createSimplePrimaryDisplay) {
716 setupNewDisplayDeviceInternalTest<SimplePrimaryDisplayCase>();
717}
718
719TEST_F(SetupNewDisplayDeviceInternalTest, createSimpleExternalDisplay) {
720 setupNewDisplayDeviceInternalTest<SimpleExternalDisplayCase>();
721}
722
723TEST_F(SetupNewDisplayDeviceInternalTest, createNonHwcVirtualDisplay) {
724 setupNewDisplayDeviceInternalTest<NonHwcVirtualDisplayCase>();
725}
726
727TEST_F(SetupNewDisplayDeviceInternalTest, createHwcVirtualDisplay) {
728 // We need to resize this so that the HWC thinks the virtual display
729 // is something it created.
730 mFlinger.mutableHwcDisplayData().resize(3);
731
732 setupNewDisplayDeviceInternalTest<HwcVirtualDisplayCase>();
733}
734
735TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3Display) {
736 setupNewDisplayDeviceInternalTest<WideColorP3ColorimetricDisplayCase>();
737}
738
739TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3EnhanceDisplay) {
740 setupNewDisplayDeviceInternalTest<WideColorP3EnhanceDisplayCase>();
741}
742
743TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10Display) {
744 setupNewDisplayDeviceInternalTest<Hdr10DisplayCase>();
745}
746
747TEST_F(SetupNewDisplayDeviceInternalTest, createHdrHlgDisplay) {
748 setupNewDisplayDeviceInternalTest<HdrHlgDisplayCase>();
749}
750
751TEST_F(SetupNewDisplayDeviceInternalTest, createHdrDolbyVisionDisplay) {
752 setupNewDisplayDeviceInternalTest<HdrDolbyVisionDisplayCase>();
753}
754
755/* ------------------------------------------------------------------------
756 * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded)
757 */
758
759class HandleTransactionLockedTest : public DisplayTransactionTest {
760public:
761 template <typename Case>
762 void setupCommonPreconditions();
763
764 template <typename Case>
765 void setupCommonCallExpectationsForConnectProcessing();
766
767 template <typename Case>
768 void setupCommonCallExpectationsForDisconnectProcessing();
769
770 template <typename Case>
771 void processesHotplugConnectCommon();
772
773 template <typename Case>
774 void ignoresHotplugConnectCommon();
775
776 template <typename Case>
777 void processesHotplugDisconnectCommon();
778
779 template <typename Case>
780 void verifyDisplayIsConnected(const sp<IBinder>& displayToken);
781
782 template <typename Case>
783 void verifyPhysicalDisplayIsConnected();
784
785 void verifyDisplayIsNotConnected(const sp<IBinder>& displayToken);
786};
787
788template <typename Case>
789void HandleTransactionLockedTest::setupCommonPreconditions() {
790 // Wide color displays support is configured appropriately
791 Case::WideColorSupport::injectConfigChange(this);
792
793 // SurfaceFlinger will use a test-controlled factory for BufferQueues
794 injectFakeBufferQueueFactory();
795
796 // SurfaceFlinger will use a test-controlled factory for native window
797 // surfaces.
798 injectFakeNativeWindowSurfaceFactory();
799}
800
801template <typename Case>
802void HandleTransactionLockedTest::setupCommonCallExpectationsForConnectProcessing() {
803 Case::Display::setupHwcHotplugCallExpectations(this);
804
805 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
806 Case::Display::setupFramebufferProducerBufferQueueCallExpectations(this);
807 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
808 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
809
810 Case::WideColorSupport::setupComposerCallExpectations(this);
811 Case::HdrSupport::setupComposerCallExpectations(this);
812
813 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
814 EXPECT_CALL(*mEventThread, onHotplugReceived(Case::Display::TYPE, true)).Times(1);
815}
816
817template <typename Case>
818void HandleTransactionLockedTest::setupCommonCallExpectationsForDisconnectProcessing() {
819 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
820 EXPECT_CALL(*mEventThread, onHotplugReceived(Case::Display::TYPE, false)).Times(1);
821}
822
823template <typename Case>
824void HandleTransactionLockedTest::verifyDisplayIsConnected(const sp<IBinder>& displayToken) {
825 // The display device should have been set up in the list of displays.
826 ASSERT_TRUE(hasDisplayDevice(displayToken));
827 const auto& device = getDisplayDevice(displayToken);
828 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
829 EXPECT_EQ(Case::Display::TYPE == DisplayDevice::DISPLAY_PRIMARY, device->isPrimary());
830
831 // The display should have been set up in the current display state
832 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
833 const auto& current = getCurrentDisplayState(displayToken);
834 EXPECT_EQ(Case::Display::TYPE, current.type);
835
836 // The display should have been set up in the drawing display state
837 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
838 const auto& draw = getDrawingDisplayState(displayToken);
839 EXPECT_EQ(Case::Display::TYPE, draw.type);
840}
841
842template <typename Case>
843void HandleTransactionLockedTest::verifyPhysicalDisplayIsConnected() {
844 // HWComposer should have an entry for the display
845 EXPECT_TRUE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
846
847 // The display should be set up as a built-in display.
848 static_assert(0 <= Case::Display::TYPE &&
849 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES,
850 "Must use a valid physical display type index for the fixed-size array");
851 auto& displayToken = mFlinger.mutableBuiltinDisplays()[Case::Display::TYPE];
852 ASSERT_TRUE(displayToken != nullptr);
853
854 verifyDisplayIsConnected<Case>(displayToken);
855}
856
857void HandleTransactionLockedTest::verifyDisplayIsNotConnected(const sp<IBinder>& displayToken) {
858 EXPECT_FALSE(hasDisplayDevice(displayToken));
859 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
860 EXPECT_FALSE(hasDrawingDisplayState(displayToken));
861}
862
863template <typename Case>
864void HandleTransactionLockedTest::processesHotplugConnectCommon() {
865 // --------------------------------------------------------------------
866 // Preconditions
867
868 setupCommonPreconditions<Case>();
869
870 // A hotplug connect event is enqueued for a display
871 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800872
873 // --------------------------------------------------------------------
874 // Call Expectations
875
876 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillOnce(Return(false));
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800877
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800878 setupCommonCallExpectationsForConnectProcessing<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800879
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800880 // --------------------------------------------------------------------
881 // Invocation
Lloyd Piquee39cad22017-12-20 17:01:29 -0800882
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800883 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800884
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800885 // --------------------------------------------------------------------
886 // Postconditions
887
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800888 verifyPhysicalDisplayIsConnected<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800889
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800890 // --------------------------------------------------------------------
891 // Cleanup conditions
892
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800893 EXPECT_CALL(*mComposer,
894 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800895 .WillOnce(Return(Error::NONE));
896 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Piquef58625d2017-12-19 13:22:33 -0800897}
898
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800899template <typename Case>
900void HandleTransactionLockedTest::ignoresHotplugConnectCommon() {
901 // --------------------------------------------------------------------
902 // Preconditions
903
904 setupCommonPreconditions<Case>();
905
906 // A hotplug connect event is enqueued for a display
907 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
908
909 // --------------------------------------------------------------------
910 // Invocation
911
912 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
913
914 // --------------------------------------------------------------------
915 // Postconditions
916
917 // HWComposer should not have an entry for the display
918 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
919}
920
921template <typename Case>
922void HandleTransactionLockedTest::processesHotplugDisconnectCommon() {
923 // --------------------------------------------------------------------
924 // Preconditions
925
926 setupCommonPreconditions<Case>();
927
928 // A hotplug disconnect event is enqueued for a display
929 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
930
931 // The display is already completely set up.
932 Case::Display::injectHwcDisplay(this);
933 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
934 existing.inject();
935
936 // --------------------------------------------------------------------
937 // Call Expectations
938
939 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
940
941 setupCommonCallExpectationsForDisconnectProcessing<Case>();
942
943 // --------------------------------------------------------------------
944 // Invocation
945
946 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
947
948 // --------------------------------------------------------------------
949 // Postconditions
950
951 // HWComposer should not have an entry for the display
952 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
953
954 // The display should not be set up as a built-in display.
955 ASSERT_TRUE(0 <= Case::Display::TYPE &&
956 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES);
957 auto displayToken = mFlinger.mutableBuiltinDisplays()[Case::Display::TYPE];
958 EXPECT_TRUE(displayToken == nullptr);
959
960 // The existing token should have been removed
961 verifyDisplayIsNotConnected(existing.token());
962}
963
964TEST_F(HandleTransactionLockedTest, processesHotplugConnectPrimaryDisplay) {
965 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
966}
967
968TEST_F(HandleTransactionLockedTest,
969 processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected) {
970 // Inject an external display.
971 ExternalDisplayVariant::injectHwcDisplay(this);
972
973 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
974}
975
976TEST_F(HandleTransactionLockedTest, processesHotplugConnectExternalDisplay) {
977 // Inject a primary display.
978 PrimaryDisplayVariant::injectHwcDisplay(this);
979
980 processesHotplugConnectCommon<SimpleExternalDisplayCase>();
981}
982
983TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected) {
984 // Inject both a primary and external display.
985 PrimaryDisplayVariant::injectHwcDisplay(this);
986 ExternalDisplayVariant::injectHwcDisplay(this);
987
988 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
989
990 ignoresHotplugConnectCommon<SimpleTertiaryDisplayCase>();
991}
992
993TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfExternalForVrComposer) {
994 // Inject a primary display.
995 PrimaryDisplayVariant::injectHwcDisplay(this);
996
997 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(true));
998
999 ignoresHotplugConnectCommon<SimpleExternalDisplayCase>();
1000}
1001
1002TEST_F(HandleTransactionLockedTest, processHotplugDisconnectPrimaryDisplay) {
1003 processesHotplugDisconnectCommon<SimplePrimaryDisplayCase>();
1004}
1005
1006TEST_F(HandleTransactionLockedTest, processHotplugDisconnectExternalDisplay) {
1007 processesHotplugDisconnectCommon<SimpleExternalDisplayCase>();
1008}
1009
1010TEST_F(HandleTransactionLockedTest, processesHotplugConnectThenDisconnectPrimary) {
1011 using Case = SimplePrimaryDisplayCase;
1012
1013 // --------------------------------------------------------------------
1014 // Preconditions
1015
1016 setupCommonPreconditions<Case>();
1017
1018 // A hotplug connect event is enqueued for a display
1019 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1020 // A hotplug disconnect event is also enqueued for the same display
1021 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1022
1023 // --------------------------------------------------------------------
1024 // Call Expectations
1025
1026 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1027
1028 setupCommonCallExpectationsForConnectProcessing<Case>();
1029 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1030
1031 EXPECT_CALL(*mComposer,
1032 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1033 .WillOnce(Return(Error::NONE));
1034 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1035
1036 // --------------------------------------------------------------------
1037 // Invocation
1038
1039 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1040
1041 // --------------------------------------------------------------------
1042 // Postconditions
1043
1044 // HWComposer should not have an entry for the display
1045 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1046
1047 // The display should not be set up as a primary built-in display.
1048 ASSERT_TRUE(0 <= Case::Display::TYPE &&
1049 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES);
1050 auto displayToken = mFlinger.mutableBuiltinDisplays()[Case::Display::TYPE];
1051 EXPECT_TRUE(displayToken == nullptr);
1052}
1053
1054TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectThenConnectPrimary) {
1055 using Case = SimplePrimaryDisplayCase;
1056
1057 // --------------------------------------------------------------------
1058 // Preconditions
1059
1060 setupCommonPreconditions<Case>();
1061
1062 // The display is already completely set up.
1063 Case::Display::injectHwcDisplay(this);
1064 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1065 existing.inject();
1066
1067 // A hotplug disconnect event is enqueued for a display
1068 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1069 // A hotplug connect event is also enqueued for the same display
1070 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1071
1072 // --------------------------------------------------------------------
1073 // Call Expectations
1074
1075 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1076
1077 setupCommonCallExpectationsForConnectProcessing<Case>();
1078 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1079
1080 // --------------------------------------------------------------------
1081 // Invocation
1082
1083 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1084
1085 // --------------------------------------------------------------------
1086 // Postconditions
1087
1088 // The existing token should have been removed
1089 verifyDisplayIsNotConnected(existing.token());
1090 static_assert(0 <= Case::Display::TYPE &&
1091 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES,
1092 "Display type must be a built-in display");
1093 EXPECT_NE(existing.token(), mFlinger.mutableBuiltinDisplays()[Case::Display::TYPE]);
1094
1095 // A new display should be connected in its place
1096
1097 verifyPhysicalDisplayIsConnected<Case>();
1098
1099 // --------------------------------------------------------------------
1100 // Cleanup conditions
1101
1102 EXPECT_CALL(*mComposer,
1103 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1104 .WillOnce(Return(Error::NONE));
1105 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1106}
1107
1108TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAdded) {
1109 using Case = HwcVirtualDisplayCase;
1110
1111 // --------------------------------------------------------------------
1112 // Preconditions
1113
1114 // The HWC supports at least one virtual display
1115 injectMockComposer(1);
1116
1117 setupCommonPreconditions<Case>();
1118
1119 // A virtual display was added to the current state, and it has a
1120 // surface(producer)
1121 sp<BBinder> displayToken = new BBinder();
1122 DisplayDeviceState info;
1123 info.type = Case::Display::TYPE;
1124 info.isSecure = static_cast<bool>(Case::Display::SECURE);
1125
1126 sp<mock::GraphicBufferProducer> surface{new mock::GraphicBufferProducer()};
1127 info.surface = surface;
1128 mFlinger.mutableCurrentState().displays.add(displayToken, info);
1129
1130 // --------------------------------------------------------------------
1131 // Call Expectations
1132
1133 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1134 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1135
1136 EXPECT_CALL(*surface, query(NATIVE_WINDOW_WIDTH, _))
1137 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::WIDTH), Return(NO_ERROR)));
1138 EXPECT_CALL(*surface, query(NATIVE_WINDOW_HEIGHT, _))
1139 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::HEIGHT), Return(NO_ERROR)));
1140 EXPECT_CALL(*surface, query(NATIVE_WINDOW_FORMAT, _))
1141 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT),
1142 Return(NO_ERROR)));
1143 EXPECT_CALL(*surface, query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, _))
1144 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(NO_ERROR)));
1145
1146 EXPECT_CALL(*surface, setAsyncMode(true)).Times(1);
1147
1148 EXPECT_CALL(*mProducer, connect(_, _, _, _)).Times(1);
1149 EXPECT_CALL(*mProducer, disconnect(_, _)).Times(1);
1150
1151 Case::Display::setupHwcVirtualDisplayCreationCallExpectations(this);
1152 Case::WideColorSupport::setupComposerCallExpectations(this);
1153 Case::HdrSupport::setupComposerCallExpectations(this);
1154
1155 // --------------------------------------------------------------------
1156 // Invocation
1157
1158 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1159
1160 // --------------------------------------------------------------------
1161 // Postconditions
1162
1163 // The display device should have been set up in the list of displays.
1164 verifyDisplayIsConnected<Case>(displayToken);
1165
1166 // --------------------------------------------------------------------
1167 // Cleanup conditions
1168
1169 EXPECT_CALL(*mComposer, destroyVirtualDisplay(Case::Display::HWC_DISPLAY_ID))
1170 .WillOnce(Return(Error::NONE));
1171 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1172}
1173
1174TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAddedWithNoSurface) {
1175 using Case = HwcVirtualDisplayCase;
1176
1177 // --------------------------------------------------------------------
1178 // Preconditions
1179
1180 // The HWC supports at least one virtual display
1181 injectMockComposer(1);
1182
1183 setupCommonPreconditions<Case>();
1184
1185 // A virtual display was added to the current state, but it does not have a
1186 // surface.
1187 sp<BBinder> displayToken = new BBinder();
1188
1189 DisplayDeviceState info;
1190 info.type = Case::Display::TYPE;
1191 info.isSecure = static_cast<bool>(Case::Display::SECURE);
1192
1193 mFlinger.mutableCurrentState().displays.add(displayToken, info);
1194
1195 // --------------------------------------------------------------------
1196 // Call Expectations
1197
1198 // --------------------------------------------------------------------
1199 // Invocation
1200
1201 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1202
1203 // --------------------------------------------------------------------
1204 // Postconditions
1205
1206 // There will not be a display device set up.
1207 EXPECT_FALSE(hasDisplayDevice(displayToken));
1208
1209 // The drawing display state will be set from the current display state.
1210 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1211 const auto& draw = getDrawingDisplayState(displayToken);
1212 EXPECT_EQ(Case::Display::TYPE, draw.type);
1213}
1214
1215TEST_F(HandleTransactionLockedTest, processesVirtualDisplayRemoval) {
1216 using Case = HwcVirtualDisplayCase;
1217
1218 // --------------------------------------------------------------------
1219 // Preconditions
1220
1221 // A virtual display is set up but is removed from the current state.
1222 mFlinger.mutableHwcDisplayData().resize(3);
1223 Case::Display::injectHwcDisplay(this);
1224 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1225 existing.inject();
1226 mFlinger.mutableCurrentState().displays.removeItem(existing.token());
1227
1228 // --------------------------------------------------------------------
1229 // Call Expectations
1230
1231 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1232
1233 // --------------------------------------------------------------------
1234 // Invocation
1235
1236 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1237
1238 // --------------------------------------------------------------------
1239 // Postconditions
1240
1241 // The existing token should have been removed
1242 verifyDisplayIsNotConnected(existing.token());
1243}
1244
Lloyd Piquef58625d2017-12-19 13:22:33 -08001245} // namespace
1246} // namespace android