blob: d9765e0b5691d8d7355cc1cec9effa93e0a092e4 [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)));
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700357 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
358 .WillRepeatedly(Return(Error::UNSUPPORTED));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800359 }
360
361 // Called by tests to set up HWC call expectations
362 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
363 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY_ID, _))
364 .WillOnce(DoAll(SetArgPointee<1>(HWC_ACTIVE_CONFIG_ID), Return(Error::NONE)));
365 }
366};
367
368struct NonHwcDisplayVariant {
369 static constexpr int HWC_ACTIVE_CONFIG_ID = 0;
370
371 static void injectHwcDisplay(DisplayTransactionTest*) {}
372
373 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
374 EXPECT_CALL(*test->mComposer, getActiveConfig(_, _)).Times(0);
375 }
376};
377
378// Physical displays are expected to be synchronous, secure, and have a HWC display for output.
379constexpr uint32_t GRALLOC_USAGE_PHYSICAL_DISPLAY =
380 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
381
382template <hwc2_display_t hwcDisplayId, DisplayDevice::DisplayType type, int width, int height,
383 Critical critical>
384struct PhysicalDisplayVariant
385 : public DisplayVariant<type, type, width, height, critical, Async::FALSE, Secure::TRUE,
386 GRALLOC_USAGE_PHYSICAL_DISPLAY>,
387 public HwcDisplayVariant<hwcDisplayId, HWC2::DisplayType::Physical,
388 DisplayVariant<type, type, width, height, critical, Async::FALSE,
389 Secure::TRUE, GRALLOC_USAGE_PHYSICAL_DISPLAY>> {};
390
391// A primary display is a physical display that is critical
392using PrimaryDisplayVariant =
393 PhysicalDisplayVariant<1001, DisplayDevice::DISPLAY_PRIMARY, 3840, 2160, Critical::TRUE>;
394
395// An external display is physical display that is not critical.
396using ExternalDisplayVariant =
397 PhysicalDisplayVariant<1002, DisplayDevice::DISPLAY_EXTERNAL, 1920, 1280, Critical::FALSE>;
398
399using TertiaryDisplayVariant =
400 PhysicalDisplayVariant<1003, DisplayDevice::DISPLAY_EXTERNAL, 1600, 1200, Critical::FALSE>;
401
402// A virtual display not supported by the HWC.
403constexpr uint32_t GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY = 0;
404
405template <int width, int height, Secure secure>
406struct NonHwcVirtualDisplayVariant
407 : public DisplayVariant<DisplayDevice::DISPLAY_VIRTUAL, DisplayDevice::DISPLAY_ID_INVALID,
408 width, height, Critical::FALSE, Async::TRUE, secure,
409 GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>,
410 public NonHwcDisplayVariant {
411 using Base = DisplayVariant<DisplayDevice::DISPLAY_VIRTUAL, DisplayDevice::DISPLAY_ID_INVALID,
412 width, height, Critical::FALSE, Async::TRUE, secure,
413 GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>;
414
415 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
416 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
417 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
418 }
419};
420
421// A virtual display supported by the HWC.
422constexpr uint32_t GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY = GRALLOC_USAGE_HW_COMPOSER;
423
424template <int width, int height, Secure secure>
425struct HwcVirtualDisplayVariant
426 : public DisplayVariant<DisplayDevice::DISPLAY_VIRTUAL, DisplayDevice::DISPLAY_VIRTUAL, width,
427 height, Critical::FALSE, Async::TRUE, secure,
428 GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>,
429 public HwcDisplayVariant<1010, HWC2::DisplayType::Virtual,
430 NonHwcVirtualDisplayVariant<width, height, secure>> {
431 using Base =
432 DisplayVariant<DisplayDevice::DISPLAY_VIRTUAL, DisplayDevice::DISPLAY_VIRTUAL, width,
433 height, Critical::FALSE, Async::TRUE, secure, GRALLOC_USAGE_HW_COMPOSER>;
434 using Self = HwcVirtualDisplayVariant<width, height, secure>;
435
436 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
437 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
438 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
439 }
440
441 static void setupHwcVirtualDisplayCreationCallExpectations(DisplayTransactionTest* test) {
442 EXPECT_CALL(*test->mComposer, createVirtualDisplay(Base::WIDTH, Base::HEIGHT, _, _))
443 .WillOnce(DoAll(SetArgPointee<3>(Self::HWC_DISPLAY_ID), Return(Error::NONE)));
444 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
445 }
446};
447
448// For this variant, SurfaceFlinger should not configure itself with wide
449// display support, so the display should not be configured for wide-color
450// support.
451struct WideColorSupportNotConfiguredVariant {
452 static constexpr bool WIDE_COLOR_SUPPORTED = false;
453
454 static void injectConfigChange(DisplayTransactionTest* test) {
455 test->mFlinger.mutableHasWideColorDisplay() = false;
456 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
457 }
458
459 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
460 EXPECT_CALL(*test->mComposer, getColorModes(_, _)).Times(0);
461 EXPECT_CALL(*test->mComposer, getRenderIntents(_, _, _)).Times(0);
462 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
463 }
464};
465
466// For this variant, SurfaceFlinger should configure itself with wide display
467// support, and the display should respond with an non-empty list of supported
468// color modes. Wide-color support should be configured.
469template <typename Display>
470struct WideColorP3ColorimetricSupportedVariant {
471 static constexpr bool WIDE_COLOR_SUPPORTED = true;
472
473 static void injectConfigChange(DisplayTransactionTest* test) {
474 test->mFlinger.mutableHasWideColorDisplay() = true;
475 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
476 }
477
478 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
479 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
480 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>({ColorMode::DISPLAY_P3})),
481 Return(Error::NONE)));
482 EXPECT_CALL(*test->mComposer,
483 getRenderIntents(Display::HWC_DISPLAY_ID, ColorMode::DISPLAY_P3, _))
484 .WillOnce(DoAll(SetArgPointee<2>(
485 std::vector<RenderIntent>({RenderIntent::COLORIMETRIC})),
486 Return(Error::NONE)));
487 EXPECT_CALL(*test->mComposer,
488 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::SRGB,
489 RenderIntent::COLORIMETRIC))
490 .WillOnce(Return(Error::NONE));
491 }
492};
493
494// For this variant, SurfaceFlinger should configure itself with wide color
495// display support, and the display should respond with an non-empty list of
496// supported color modes.
497template <typename Display>
498struct WideColorP3EnhanceSupportedVariant {
499 static constexpr bool WIDE_COLOR_SUPPORTED = true;
500
501 static void injectConfigChange(DisplayTransactionTest* test) {
502 test->mFlinger.mutableHasWideColorDisplay() = true;
503 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::ENHANCED;
504 }
505
506 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
507 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
508 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>({ColorMode::DISPLAY_P3})),
509 Return(Error::NONE)));
510 EXPECT_CALL(*test->mComposer,
511 getRenderIntents(Display::HWC_DISPLAY_ID, ColorMode::DISPLAY_P3, _))
512 .WillOnce(
513 DoAll(SetArgPointee<2>(std::vector<RenderIntent>({RenderIntent::ENHANCE})),
514 Return(Error::NONE)));
515 EXPECT_CALL(*test->mComposer,
516 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::SRGB, RenderIntent::ENHANCE))
517 .WillOnce(Return(Error::NONE));
518 }
519};
520
521// For this variant, SurfaceFlinger should configure itself with wide display
522// support, but the display should respond with an empty list of supported color
523// modes. Wide-color support for the display should not be configured.
524template <typename Display>
525struct WideColorNotSupportedVariant {
526 static constexpr bool WIDE_COLOR_SUPPORTED = false;
527
528 static void injectConfigChange(DisplayTransactionTest* test) {
529 test->mFlinger.mutableHasWideColorDisplay() = true;
530 }
531
532 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
533 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
534 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>()), Return(Error::NONE)));
535 EXPECT_CALL(*test->mComposer,
536 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::NATIVE,
537 RenderIntent::COLORIMETRIC))
538 .WillOnce(Return(Error::NONE));
539 }
540};
541
542// For this variant, the display is not a HWC display, so no HDR support should
543// be configured.
544struct NonHwcDisplayHdrSupportVariant {
545 static constexpr bool HDR10_SUPPORTED = false;
546 static constexpr bool HDR_HLG_SUPPORTED = false;
547 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
548 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
549 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
550 }
551};
552
553// For this variant, the composer should respond with a non-empty list of HDR
554// modes containing HDR10, so HDR10 support should be configured.
555template <typename Display>
556struct Hdr10SupportedVariant {
557 static constexpr bool HDR10_SUPPORTED = true;
558 static constexpr bool HDR_HLG_SUPPORTED = false;
559 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
560 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
561 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
562 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HDR10})),
563 Return(Error::NONE)));
564 }
565};
566
567// For this variant, the composer should respond with a non-empty list of HDR
568// modes containing HLG, so HLG support should be configured.
569template <typename Display>
570struct HdrHlgSupportedVariant {
571 static constexpr bool HDR10_SUPPORTED = false;
572 static constexpr bool HDR_HLG_SUPPORTED = true;
573 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
574 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
575 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
576 .WillOnce(
577 DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HLG})), Return(Error::NONE)));
578 }
579};
580
581// For this variant, the composer should respond with a non-empty list of HDR
582// modes containing DOLBY_VISION, so DOLBY_VISION support should be configured.
583template <typename Display>
584struct HdrDolbyVisionSupportedVariant {
585 static constexpr bool HDR10_SUPPORTED = false;
586 static constexpr bool HDR_HLG_SUPPORTED = false;
587 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = true;
588 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
589 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
590 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::DOLBY_VISION})),
591 Return(Error::NONE)));
592 }
593};
594
595// For this variant, the composer should respond with am empty list of HDR
596// modes, so no HDR support should be configured.
597template <typename Display>
598struct HdrNotSupportedVariant {
599 static constexpr bool HDR10_SUPPORTED = false;
600 static constexpr bool HDR_HLG_SUPPORTED = false;
601 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
602 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
603 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
604 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
605 }
606};
607
608/* ------------------------------------------------------------------------
609 * Typical display configurations to test
610 */
611
612template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy>
613struct Case {
614 using Display = DisplayPolicy;
615 using WideColorSupport = WideColorSupportPolicy;
616 using HdrSupport = HdrSupportPolicy;
617};
618
619using SimplePrimaryDisplayCase =
620 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
621 HdrNotSupportedVariant<PrimaryDisplayVariant>>;
622using SimpleExternalDisplayCase =
623 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
624 HdrNotSupportedVariant<ExternalDisplayVariant>>;
625using SimpleTertiaryDisplayCase =
626 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
627 HdrNotSupportedVariant<TertiaryDisplayVariant>>;
628using NonHwcVirtualDisplayCase =
629 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
630 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant>;
631using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
632using HwcVirtualDisplayCase =
633 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
634 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>>;
635using WideColorP3ColorimetricDisplayCase =
636 Case<PrimaryDisplayVariant, WideColorP3ColorimetricSupportedVariant<PrimaryDisplayVariant>,
637 HdrNotSupportedVariant<PrimaryDisplayVariant>>;
638using WideColorP3EnhanceDisplayCase =
639 Case<PrimaryDisplayVariant, WideColorP3EnhanceSupportedVariant<PrimaryDisplayVariant>,
640 HdrNotSupportedVariant<PrimaryDisplayVariant>>;
641using Hdr10DisplayCase =
642 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
643 Hdr10SupportedVariant<PrimaryDisplayVariant>>;
644using HdrHlgDisplayCase =
645 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
646 HdrHlgSupportedVariant<PrimaryDisplayVariant>>;
647using HdrDolbyVisionDisplayCase =
648 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
649 HdrDolbyVisionSupportedVariant<PrimaryDisplayVariant>>;
650
651/* ------------------------------------------------------------------------
652 * SurfaceFlinger::setupNewDisplayDeviceInternal
653 */
654
655class SetupNewDisplayDeviceInternalTest : public DisplayTransactionTest {
656public:
657 template <typename T>
658 void setupNewDisplayDeviceInternalTest();
659};
660
661template <typename Case>
662void SetupNewDisplayDeviceInternalTest::setupNewDisplayDeviceInternalTest() {
663 const sp<BBinder> displayToken = new BBinder();
664 const sp<mock::DisplaySurface> displaySurface = new mock::DisplaySurface();
665 const sp<mock::GraphicBufferProducer> producer = new mock::GraphicBufferProducer();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800666
667 // --------------------------------------------------------------------
668 // Preconditions
669
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800670 // Wide color displays support is configured appropriately
671 Case::WideColorSupport::injectConfigChange(this);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800672
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800673 // The display is setup with the HWC.
674 Case::Display::injectHwcDisplay(this);
675
676 // SurfaceFlinger will use a test-controlled factory for native window
677 // surfaces.
678 injectFakeNativeWindowSurfaceFactory();
679
680 // --------------------------------------------------------------------
681 // Call Expectations
682
683 // Various native window calls will be made.
684 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
685
686 // TODO(b/69807179): SurfaceFlinger does not correctly get the active config.
687 // Case::Display::setupHwcGetActiveConfigCallExpectations(this)
688
689 Case::WideColorSupport::setupComposerCallExpectations(this);
690 Case::HdrSupport::setupComposerCallExpectations(this);
691
692 // --------------------------------------------------------------------
693 // Invocation
694
695 DisplayDeviceState state;
696 state.type = Case::Display::TYPE;
697 state.isSecure = static_cast<bool>(Case::Display::SECURE);
698
699 auto device = mFlinger.setupNewDisplayDeviceInternal(displayToken, Case::Display::TYPE, state,
700 displaySurface, producer);
701
702 // --------------------------------------------------------------------
703 // Postconditions
704
705 ASSERT_TRUE(device != nullptr);
706 EXPECT_EQ(Case::Display::TYPE, device->getDisplayType());
707 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
708 EXPECT_EQ(Case::Display::WIDTH, device->getWidth());
709 EXPECT_EQ(Case::Display::HEIGHT, device->getHeight());
710 EXPECT_EQ(Case::WideColorSupport::WIDE_COLOR_SUPPORTED, device->hasWideColorGamut());
711 EXPECT_EQ(Case::HdrSupport::HDR10_SUPPORTED, device->hasHDR10Support());
712 EXPECT_EQ(Case::HdrSupport::HDR_HLG_SUPPORTED, device->hasHLGSupport());
713 EXPECT_EQ(Case::HdrSupport::HDR_DOLBY_VISION_SUPPORTED, device->hasDolbyVisionSupport());
714 EXPECT_EQ(Case::Display::HWC_ACTIVE_CONFIG_ID, device->getActiveConfig());
715}
716
717TEST_F(SetupNewDisplayDeviceInternalTest, createSimplePrimaryDisplay) {
718 setupNewDisplayDeviceInternalTest<SimplePrimaryDisplayCase>();
719}
720
721TEST_F(SetupNewDisplayDeviceInternalTest, createSimpleExternalDisplay) {
722 setupNewDisplayDeviceInternalTest<SimpleExternalDisplayCase>();
723}
724
725TEST_F(SetupNewDisplayDeviceInternalTest, createNonHwcVirtualDisplay) {
726 setupNewDisplayDeviceInternalTest<NonHwcVirtualDisplayCase>();
727}
728
729TEST_F(SetupNewDisplayDeviceInternalTest, createHwcVirtualDisplay) {
730 // We need to resize this so that the HWC thinks the virtual display
731 // is something it created.
732 mFlinger.mutableHwcDisplayData().resize(3);
733
734 setupNewDisplayDeviceInternalTest<HwcVirtualDisplayCase>();
735}
736
737TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3Display) {
738 setupNewDisplayDeviceInternalTest<WideColorP3ColorimetricDisplayCase>();
739}
740
741TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3EnhanceDisplay) {
742 setupNewDisplayDeviceInternalTest<WideColorP3EnhanceDisplayCase>();
743}
744
745TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10Display) {
746 setupNewDisplayDeviceInternalTest<Hdr10DisplayCase>();
747}
748
749TEST_F(SetupNewDisplayDeviceInternalTest, createHdrHlgDisplay) {
750 setupNewDisplayDeviceInternalTest<HdrHlgDisplayCase>();
751}
752
753TEST_F(SetupNewDisplayDeviceInternalTest, createHdrDolbyVisionDisplay) {
754 setupNewDisplayDeviceInternalTest<HdrDolbyVisionDisplayCase>();
755}
756
757/* ------------------------------------------------------------------------
758 * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded)
759 */
760
761class HandleTransactionLockedTest : public DisplayTransactionTest {
762public:
763 template <typename Case>
764 void setupCommonPreconditions();
765
766 template <typename Case>
767 void setupCommonCallExpectationsForConnectProcessing();
768
769 template <typename Case>
770 void setupCommonCallExpectationsForDisconnectProcessing();
771
772 template <typename Case>
773 void processesHotplugConnectCommon();
774
775 template <typename Case>
776 void ignoresHotplugConnectCommon();
777
778 template <typename Case>
779 void processesHotplugDisconnectCommon();
780
781 template <typename Case>
782 void verifyDisplayIsConnected(const sp<IBinder>& displayToken);
783
784 template <typename Case>
785 void verifyPhysicalDisplayIsConnected();
786
787 void verifyDisplayIsNotConnected(const sp<IBinder>& displayToken);
788};
789
790template <typename Case>
791void HandleTransactionLockedTest::setupCommonPreconditions() {
792 // Wide color displays support is configured appropriately
793 Case::WideColorSupport::injectConfigChange(this);
794
795 // SurfaceFlinger will use a test-controlled factory for BufferQueues
796 injectFakeBufferQueueFactory();
797
798 // SurfaceFlinger will use a test-controlled factory for native window
799 // surfaces.
800 injectFakeNativeWindowSurfaceFactory();
801}
802
803template <typename Case>
804void HandleTransactionLockedTest::setupCommonCallExpectationsForConnectProcessing() {
805 Case::Display::setupHwcHotplugCallExpectations(this);
806
807 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
808 Case::Display::setupFramebufferProducerBufferQueueCallExpectations(this);
809 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
810 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
811
812 Case::WideColorSupport::setupComposerCallExpectations(this);
813 Case::HdrSupport::setupComposerCallExpectations(this);
814
815 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
816 EXPECT_CALL(*mEventThread, onHotplugReceived(Case::Display::TYPE, true)).Times(1);
817}
818
819template <typename Case>
820void HandleTransactionLockedTest::setupCommonCallExpectationsForDisconnectProcessing() {
821 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
822 EXPECT_CALL(*mEventThread, onHotplugReceived(Case::Display::TYPE, false)).Times(1);
823}
824
825template <typename Case>
826void HandleTransactionLockedTest::verifyDisplayIsConnected(const sp<IBinder>& displayToken) {
827 // The display device should have been set up in the list of displays.
828 ASSERT_TRUE(hasDisplayDevice(displayToken));
829 const auto& device = getDisplayDevice(displayToken);
830 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
831 EXPECT_EQ(Case::Display::TYPE == DisplayDevice::DISPLAY_PRIMARY, device->isPrimary());
832
833 // The display should have been set up in the current display state
834 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
835 const auto& current = getCurrentDisplayState(displayToken);
836 EXPECT_EQ(Case::Display::TYPE, current.type);
837
838 // The display should have been set up in the drawing display state
839 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
840 const auto& draw = getDrawingDisplayState(displayToken);
841 EXPECT_EQ(Case::Display::TYPE, draw.type);
842}
843
844template <typename Case>
845void HandleTransactionLockedTest::verifyPhysicalDisplayIsConnected() {
846 // HWComposer should have an entry for the display
847 EXPECT_TRUE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
848
849 // The display should be set up as a built-in display.
850 static_assert(0 <= Case::Display::TYPE &&
851 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES,
852 "Must use a valid physical display type index for the fixed-size array");
853 auto& displayToken = mFlinger.mutableBuiltinDisplays()[Case::Display::TYPE];
854 ASSERT_TRUE(displayToken != nullptr);
855
856 verifyDisplayIsConnected<Case>(displayToken);
857}
858
859void HandleTransactionLockedTest::verifyDisplayIsNotConnected(const sp<IBinder>& displayToken) {
860 EXPECT_FALSE(hasDisplayDevice(displayToken));
861 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
862 EXPECT_FALSE(hasDrawingDisplayState(displayToken));
863}
864
865template <typename Case>
866void HandleTransactionLockedTest::processesHotplugConnectCommon() {
867 // --------------------------------------------------------------------
868 // Preconditions
869
870 setupCommonPreconditions<Case>();
871
872 // A hotplug connect event is enqueued for a display
873 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800874
875 // --------------------------------------------------------------------
876 // Call Expectations
877
878 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillOnce(Return(false));
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800879
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800880 setupCommonCallExpectationsForConnectProcessing<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800881
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800882 // --------------------------------------------------------------------
883 // Invocation
Lloyd Piquee39cad22017-12-20 17:01:29 -0800884
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800885 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800886
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800887 // --------------------------------------------------------------------
888 // Postconditions
889
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800890 verifyPhysicalDisplayIsConnected<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800891
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800892 // --------------------------------------------------------------------
893 // Cleanup conditions
894
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800895 EXPECT_CALL(*mComposer,
896 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800897 .WillOnce(Return(Error::NONE));
898 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Piquef58625d2017-12-19 13:22:33 -0800899}
900
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800901template <typename Case>
902void HandleTransactionLockedTest::ignoresHotplugConnectCommon() {
903 // --------------------------------------------------------------------
904 // Preconditions
905
906 setupCommonPreconditions<Case>();
907
908 // A hotplug connect event is enqueued for a display
909 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
910
911 // --------------------------------------------------------------------
912 // Invocation
913
914 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
915
916 // --------------------------------------------------------------------
917 // Postconditions
918
919 // HWComposer should not have an entry for the display
920 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
921}
922
923template <typename Case>
924void HandleTransactionLockedTest::processesHotplugDisconnectCommon() {
925 // --------------------------------------------------------------------
926 // Preconditions
927
928 setupCommonPreconditions<Case>();
929
930 // A hotplug disconnect event is enqueued for a display
931 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
932
933 // The display is already completely set up.
934 Case::Display::injectHwcDisplay(this);
935 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
936 existing.inject();
937
938 // --------------------------------------------------------------------
939 // Call Expectations
940
941 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
942
943 setupCommonCallExpectationsForDisconnectProcessing<Case>();
944
945 // --------------------------------------------------------------------
946 // Invocation
947
948 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
949
950 // --------------------------------------------------------------------
951 // Postconditions
952
953 // HWComposer should not have an entry for the display
954 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
955
956 // The display should not be set up as a built-in display.
957 ASSERT_TRUE(0 <= Case::Display::TYPE &&
958 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES);
959 auto displayToken = mFlinger.mutableBuiltinDisplays()[Case::Display::TYPE];
960 EXPECT_TRUE(displayToken == nullptr);
961
962 // The existing token should have been removed
963 verifyDisplayIsNotConnected(existing.token());
964}
965
966TEST_F(HandleTransactionLockedTest, processesHotplugConnectPrimaryDisplay) {
967 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
968}
969
970TEST_F(HandleTransactionLockedTest,
971 processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected) {
972 // Inject an external display.
973 ExternalDisplayVariant::injectHwcDisplay(this);
974
975 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
976}
977
978TEST_F(HandleTransactionLockedTest, processesHotplugConnectExternalDisplay) {
979 // Inject a primary display.
980 PrimaryDisplayVariant::injectHwcDisplay(this);
981
982 processesHotplugConnectCommon<SimpleExternalDisplayCase>();
983}
984
985TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected) {
986 // Inject both a primary and external display.
987 PrimaryDisplayVariant::injectHwcDisplay(this);
988 ExternalDisplayVariant::injectHwcDisplay(this);
989
990 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
991
992 ignoresHotplugConnectCommon<SimpleTertiaryDisplayCase>();
993}
994
995TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfExternalForVrComposer) {
996 // Inject a primary display.
997 PrimaryDisplayVariant::injectHwcDisplay(this);
998
999 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(true));
1000
1001 ignoresHotplugConnectCommon<SimpleExternalDisplayCase>();
1002}
1003
1004TEST_F(HandleTransactionLockedTest, processHotplugDisconnectPrimaryDisplay) {
1005 processesHotplugDisconnectCommon<SimplePrimaryDisplayCase>();
1006}
1007
1008TEST_F(HandleTransactionLockedTest, processHotplugDisconnectExternalDisplay) {
1009 processesHotplugDisconnectCommon<SimpleExternalDisplayCase>();
1010}
1011
1012TEST_F(HandleTransactionLockedTest, processesHotplugConnectThenDisconnectPrimary) {
1013 using Case = SimplePrimaryDisplayCase;
1014
1015 // --------------------------------------------------------------------
1016 // Preconditions
1017
1018 setupCommonPreconditions<Case>();
1019
1020 // A hotplug connect event is enqueued for a display
1021 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1022 // A hotplug disconnect event is also enqueued for the same display
1023 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1024
1025 // --------------------------------------------------------------------
1026 // Call Expectations
1027
1028 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1029
1030 setupCommonCallExpectationsForConnectProcessing<Case>();
1031 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1032
1033 EXPECT_CALL(*mComposer,
1034 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1035 .WillOnce(Return(Error::NONE));
1036 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1037
1038 // --------------------------------------------------------------------
1039 // Invocation
1040
1041 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1042
1043 // --------------------------------------------------------------------
1044 // Postconditions
1045
1046 // HWComposer should not have an entry for the display
1047 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1048
1049 // The display should not be set up as a primary built-in display.
1050 ASSERT_TRUE(0 <= Case::Display::TYPE &&
1051 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES);
1052 auto displayToken = mFlinger.mutableBuiltinDisplays()[Case::Display::TYPE];
1053 EXPECT_TRUE(displayToken == nullptr);
1054}
1055
1056TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectThenConnectPrimary) {
1057 using Case = SimplePrimaryDisplayCase;
1058
1059 // --------------------------------------------------------------------
1060 // Preconditions
1061
1062 setupCommonPreconditions<Case>();
1063
1064 // The display is already completely set up.
1065 Case::Display::injectHwcDisplay(this);
1066 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1067 existing.inject();
1068
1069 // A hotplug disconnect event is enqueued for a display
1070 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1071 // A hotplug connect event is also enqueued for the same display
1072 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1073
1074 // --------------------------------------------------------------------
1075 // Call Expectations
1076
1077 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1078
1079 setupCommonCallExpectationsForConnectProcessing<Case>();
1080 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1081
1082 // --------------------------------------------------------------------
1083 // Invocation
1084
1085 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1086
1087 // --------------------------------------------------------------------
1088 // Postconditions
1089
1090 // The existing token should have been removed
1091 verifyDisplayIsNotConnected(existing.token());
1092 static_assert(0 <= Case::Display::TYPE &&
1093 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES,
1094 "Display type must be a built-in display");
1095 EXPECT_NE(existing.token(), mFlinger.mutableBuiltinDisplays()[Case::Display::TYPE]);
1096
1097 // A new display should be connected in its place
1098
1099 verifyPhysicalDisplayIsConnected<Case>();
1100
1101 // --------------------------------------------------------------------
1102 // Cleanup conditions
1103
1104 EXPECT_CALL(*mComposer,
1105 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1106 .WillOnce(Return(Error::NONE));
1107 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1108}
1109
1110TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAdded) {
1111 using Case = HwcVirtualDisplayCase;
1112
1113 // --------------------------------------------------------------------
1114 // Preconditions
1115
1116 // The HWC supports at least one virtual display
1117 injectMockComposer(1);
1118
1119 setupCommonPreconditions<Case>();
1120
1121 // A virtual display was added to the current state, and it has a
1122 // surface(producer)
1123 sp<BBinder> displayToken = new BBinder();
1124 DisplayDeviceState info;
1125 info.type = Case::Display::TYPE;
1126 info.isSecure = static_cast<bool>(Case::Display::SECURE);
1127
1128 sp<mock::GraphicBufferProducer> surface{new mock::GraphicBufferProducer()};
1129 info.surface = surface;
1130 mFlinger.mutableCurrentState().displays.add(displayToken, info);
1131
1132 // --------------------------------------------------------------------
1133 // Call Expectations
1134
1135 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1136 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1137
1138 EXPECT_CALL(*surface, query(NATIVE_WINDOW_WIDTH, _))
1139 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::WIDTH), Return(NO_ERROR)));
1140 EXPECT_CALL(*surface, query(NATIVE_WINDOW_HEIGHT, _))
1141 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::HEIGHT), Return(NO_ERROR)));
1142 EXPECT_CALL(*surface, query(NATIVE_WINDOW_FORMAT, _))
1143 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT),
1144 Return(NO_ERROR)));
1145 EXPECT_CALL(*surface, query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, _))
1146 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(NO_ERROR)));
1147
1148 EXPECT_CALL(*surface, setAsyncMode(true)).Times(1);
1149
1150 EXPECT_CALL(*mProducer, connect(_, _, _, _)).Times(1);
1151 EXPECT_CALL(*mProducer, disconnect(_, _)).Times(1);
1152
1153 Case::Display::setupHwcVirtualDisplayCreationCallExpectations(this);
1154 Case::WideColorSupport::setupComposerCallExpectations(this);
1155 Case::HdrSupport::setupComposerCallExpectations(this);
1156
1157 // --------------------------------------------------------------------
1158 // Invocation
1159
1160 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1161
1162 // --------------------------------------------------------------------
1163 // Postconditions
1164
1165 // The display device should have been set up in the list of displays.
1166 verifyDisplayIsConnected<Case>(displayToken);
1167
1168 // --------------------------------------------------------------------
1169 // Cleanup conditions
1170
1171 EXPECT_CALL(*mComposer, destroyVirtualDisplay(Case::Display::HWC_DISPLAY_ID))
1172 .WillOnce(Return(Error::NONE));
1173 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1174}
1175
1176TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAddedWithNoSurface) {
1177 using Case = HwcVirtualDisplayCase;
1178
1179 // --------------------------------------------------------------------
1180 // Preconditions
1181
1182 // The HWC supports at least one virtual display
1183 injectMockComposer(1);
1184
1185 setupCommonPreconditions<Case>();
1186
1187 // A virtual display was added to the current state, but it does not have a
1188 // surface.
1189 sp<BBinder> displayToken = new BBinder();
1190
1191 DisplayDeviceState info;
1192 info.type = Case::Display::TYPE;
1193 info.isSecure = static_cast<bool>(Case::Display::SECURE);
1194
1195 mFlinger.mutableCurrentState().displays.add(displayToken, info);
1196
1197 // --------------------------------------------------------------------
1198 // Call Expectations
1199
1200 // --------------------------------------------------------------------
1201 // Invocation
1202
1203 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1204
1205 // --------------------------------------------------------------------
1206 // Postconditions
1207
1208 // There will not be a display device set up.
1209 EXPECT_FALSE(hasDisplayDevice(displayToken));
1210
1211 // The drawing display state will be set from the current display state.
1212 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1213 const auto& draw = getDrawingDisplayState(displayToken);
1214 EXPECT_EQ(Case::Display::TYPE, draw.type);
1215}
1216
1217TEST_F(HandleTransactionLockedTest, processesVirtualDisplayRemoval) {
1218 using Case = HwcVirtualDisplayCase;
1219
1220 // --------------------------------------------------------------------
1221 // Preconditions
1222
1223 // A virtual display is set up but is removed from the current state.
1224 mFlinger.mutableHwcDisplayData().resize(3);
1225 Case::Display::injectHwcDisplay(this);
1226 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1227 existing.inject();
1228 mFlinger.mutableCurrentState().displays.removeItem(existing.token());
1229
1230 // --------------------------------------------------------------------
1231 // Call Expectations
1232
1233 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1234
1235 // --------------------------------------------------------------------
1236 // Invocation
1237
1238 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1239
1240 // --------------------------------------------------------------------
1241 // Postconditions
1242
1243 // The existing token should have been removed
1244 verifyDisplayIsNotConnected(existing.token());
1245}
1246
Lloyd Piquef58625d2017-12-19 13:22:33 -08001247} // namespace
1248} // namespace android