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