blob: 409518b31a2e11375a2e944c3df4b40585c36c54 [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
Lloyd Piqued883d5a2018-04-27 19:32:30 -070048using android::Hwc2::ColorMode;
Lloyd Piquee39cad22017-12-20 17:01:29 -080049using android::Hwc2::Error;
Lloyd Piqued883d5a2018-04-27 19:32:30 -070050using android::Hwc2::Hdr;
Lloyd Piquee39cad22017-12-20 17:01:29 -080051using android::Hwc2::IComposer;
52using android::Hwc2::IComposerClient;
Lloyd Piqued883d5a2018-04-27 19:32:30 -070053using android::Hwc2::PerFrameMetadataKey;
54using android::Hwc2::RenderIntent;
Lloyd Piquee39cad22017-12-20 17:01:29 -080055
Lloyd Piquefc12f562018-01-22 18:44:59 -080056using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector;
57using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
Lloyd Pique1fa4d462018-01-22 18:03:16 -080058using HotplugEvent = TestableSurfaceFlinger::HotplugEvent;
Lloyd Piquefc12f562018-01-22 18:44:59 -080059using HWC2Display = TestableSurfaceFlinger::HWC2Display;
Lloyd Piquebc792092018-01-17 11:52:30 -080060
Lloyd Piquefc12f562018-01-22 18:44:59 -080061constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666;
Lloyd Piquee39cad22017-12-20 17:01:29 -080062constexpr int32_t DEFAULT_DPI = 320;
Lloyd Piquefc12f562018-01-22 18:44:59 -080063constexpr int DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT = HAL_PIXEL_FORMAT_RGB_565;
Lloyd Piquee39cad22017-12-20 17:01:29 -080064
Lloyd Piquefc12f562018-01-22 18:44:59 -080065/* ------------------------------------------------------------------------
66 * Boolean avoidance
67 *
68 * To make calls and template instantiations more readable, we define some
69 * local enums along with an implicit bool conversion.
70 */
71
72#define BOOL_SUBSTITUTE(TYPENAME) enum class TYPENAME : bool { FALSE = false, TRUE = true };
73
74BOOL_SUBSTITUTE(Critical);
75BOOL_SUBSTITUTE(Async);
76BOOL_SUBSTITUTE(Secure);
77
78/* ------------------------------------------------------------------------
79 *
80 */
Lloyd Pique1fa4d462018-01-22 18:03:16 -080081
Lloyd Piquef58625d2017-12-19 13:22:33 -080082class DisplayTransactionTest : public testing::Test {
Lloyd Piquefc12f562018-01-22 18:44:59 -080083public:
Lloyd Piquef58625d2017-12-19 13:22:33 -080084 DisplayTransactionTest();
85 ~DisplayTransactionTest() override;
86
Lloyd Pique1fa4d462018-01-22 18:03:16 -080087 // --------------------------------------------------------------------
Lloyd Piquefc12f562018-01-22 18:44:59 -080088 // Mock/Fake injection
Lloyd Piquef58625d2017-12-19 13:22:33 -080089
Lloyd Piquefc12f562018-01-22 18:44:59 -080090 void injectMockComposer(int virtualDisplayCount);
91 void injectFakeBufferQueueFactory();
92 void injectFakeNativeWindowSurfaceFactory();
Lloyd Pique1fa4d462018-01-22 18:03:16 -080093
94 // --------------------------------------------------------------------
95 // Postcondition helpers
96
Lloyd Piquefc12f562018-01-22 18:44:59 -080097 bool hasHwcDisplay(hwc2_display_t displayId);
Lloyd Pique1fa4d462018-01-22 18:03:16 -080098 bool hasTransactionFlagSet(int flag);
99 bool hasDisplayDevice(sp<IBinder> displayToken);
100 sp<DisplayDevice> getDisplayDevice(sp<IBinder> displayToken);
101 bool hasCurrentDisplayState(sp<IBinder> displayToken);
102 const DisplayDeviceState& getCurrentDisplayState(sp<IBinder> displayToken);
103 bool hasDrawingDisplayState(sp<IBinder> displayToken);
104 const DisplayDeviceState& getDrawingDisplayState(sp<IBinder> displayToken);
105
106 // --------------------------------------------------------------------
107 // Test instances
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800108
Lloyd Piquef58625d2017-12-19 13:22:33 -0800109 TestableSurfaceFlinger mFlinger;
Lloyd Piquee39cad22017-12-20 17:01:29 -0800110 mock::EventThread* mEventThread = new mock::EventThread();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800111 mock::EventControlThread* mEventControlThread = new mock::EventControlThread();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800112
113 // These mocks are created by the test, but are destroyed by SurfaceFlinger
114 // by virtue of being stored into a std::unique_ptr. However we still need
115 // to keep a reference to them for use in setting up call expectations.
116 RE::mock::RenderEngine* mRenderEngine = new RE::mock::RenderEngine();
Lloyd Piquefc12f562018-01-22 18:44:59 -0800117 Hwc2::mock::Composer* mComposer = nullptr;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800118 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
119 mock::SurfaceInterceptor* mSurfaceInterceptor = new mock::SurfaceInterceptor();
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800120
121 // These mocks are created only when expected to be created via a factory.
122 sp<mock::GraphicBufferConsumer> mConsumer;
123 sp<mock::GraphicBufferProducer> mProducer;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800124 mock::NativeWindowSurface* mNativeWindowSurface = nullptr;
125 sp<mock::NativeWindow> mNativeWindow;
126 RE::mock::Surface* mRenderSurface = nullptr;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800127};
128
129DisplayTransactionTest::DisplayTransactionTest() {
130 const ::testing::TestInfo* const test_info =
131 ::testing::UnitTest::GetInstance()->current_test_info();
132 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800133
Lloyd Piquefc12f562018-01-22 18:44:59 -0800134 // Default to no wide color display support configured
135 mFlinger.mutableHasWideColorDisplay() = false;
136 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
137
138 // Default to using HWC virtual displays
139 mFlinger.mutableUseHwcVirtualDisplays() = true;
140
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800141 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
142 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
143 });
144
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800145 mFlinger.setCreateNativeWindowSurface([](auto) {
146 ADD_FAILURE() << "Unexpected request to create a native window surface.";
147 return nullptr;
148 });
149
150 mFlinger.mutableEventControlThread().reset(mEventControlThread);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800151 mFlinger.mutableEventThread().reset(mEventThread);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800152 mFlinger.mutableEventQueue().reset(mMessageQueue);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800153 mFlinger.setupRenderEngine(std::unique_ptr<RE::RenderEngine>(mRenderEngine));
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800154 mFlinger.mutableInterceptor().reset(mSurfaceInterceptor);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800155
Lloyd Piquefc12f562018-01-22 18:44:59 -0800156 injectMockComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -0800157}
158
159DisplayTransactionTest::~DisplayTransactionTest() {
160 const ::testing::TestInfo* const test_info =
161 ::testing::UnitTest::GetInstance()->current_test_info();
162 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
163}
164
Lloyd Piquefc12f562018-01-22 18:44:59 -0800165void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
166 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800167 EXPECT_CALL(*mComposer, getCapabilities())
168 .WillOnce(Return(std::vector<IComposer::Capability>()));
169 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
170 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -0800171
Lloyd Piquee39cad22017-12-20 17:01:29 -0800172 Mock::VerifyAndClear(mComposer);
173}
174
Lloyd Piquefc12f562018-01-22 18:44:59 -0800175void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800176 // This setup is only expected once per test.
177 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
178
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800179 mConsumer = new mock::GraphicBufferConsumer();
180 mProducer = new mock::GraphicBufferProducer();
181
182 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
183 *outProducer = mProducer;
184 *outConsumer = mConsumer;
185 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800186}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800187
Lloyd Piquefc12f562018-01-22 18:44:59 -0800188void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800189 // This setup is only expected once per test.
190 ASSERT_TRUE(mNativeWindowSurface == nullptr);
191
192 mNativeWindowSurface = new mock::NativeWindowSurface();
193 mNativeWindow = new mock::NativeWindow();
194
195 mFlinger.setCreateNativeWindowSurface(
196 [this](auto) { return std::unique_ptr<NativeWindowSurface>(mNativeWindowSurface); });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800197}
198
Lloyd Piquefc12f562018-01-22 18:44:59 -0800199bool DisplayTransactionTest::hasHwcDisplay(hwc2_display_t displayId) {
200 return mFlinger.mutableHwcDisplaySlots().count(displayId) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800201}
202
203bool DisplayTransactionTest::hasTransactionFlagSet(int flag) {
204 return mFlinger.mutableTransactionFlags() & flag;
205}
206
207bool DisplayTransactionTest::hasDisplayDevice(sp<IBinder> displayToken) {
208 return mFlinger.mutableDisplays().indexOfKey(displayToken) >= 0;
209}
210
211sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) {
212 return mFlinger.mutableDisplays().valueFor(displayToken);
213}
214
215bool DisplayTransactionTest::hasCurrentDisplayState(sp<IBinder> displayToken) {
216 return mFlinger.mutableCurrentState().displays.indexOfKey(displayToken) >= 0;
217}
218
219const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(sp<IBinder> displayToken) {
220 return mFlinger.mutableCurrentState().displays.valueFor(displayToken);
221}
222
223bool DisplayTransactionTest::hasDrawingDisplayState(sp<IBinder> displayToken) {
224 return mFlinger.mutableDrawingState().displays.indexOfKey(displayToken) >= 0;
225}
226
227const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(sp<IBinder> displayToken) {
228 return mFlinger.mutableDrawingState().displays.valueFor(displayToken);
229}
230
231/* ------------------------------------------------------------------------
Lloyd Piquefc12f562018-01-22 18:44:59 -0800232 *
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800233 */
234
Lloyd Piquefc12f562018-01-22 18:44:59 -0800235template <DisplayDevice::DisplayType type, DisplayDevice::DisplayType hwcId, int width, int height,
236 Critical critical, Async async, Secure secure, int grallocUsage>
237struct DisplayVariant {
238 // The display width and height
239 static constexpr int WIDTH = width;
240 static constexpr int HEIGHT = height;
241
242 static constexpr int GRALLOC_USAGE = grallocUsage;
243
244 // The type for this display
245 static constexpr DisplayDevice::DisplayType TYPE = type;
246 static constexpr DisplayDevice::DisplayType HWCOMPOSER_ID = hwcId;
247
248 // When creating native window surfaces for the framebuffer, whether those should be critical
249 static constexpr Critical CRITICAL = critical;
250
251 // When creating native window surfaces for the framebuffer, whether those should be async
252 static constexpr Async ASYNC = async;
253
254 // Whether the display should be treated as secure
255 static constexpr Secure SECURE = secure;
256
257 static auto makeFakeExistingDisplayInjector(DisplayTransactionTest* test) {
258 auto injector = FakeDisplayDeviceInjector(test->mFlinger, TYPE, HWCOMPOSER_ID);
259 injector.setSecure(static_cast<bool>(SECURE));
260 return injector;
261 }
262
263 // Called by tests to set up any native window creation call expectations.
264 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
265 EXPECT_CALL(*test->mNativeWindowSurface, getNativeWindow())
266 .WillOnce(Return(test->mNativeWindow));
267 EXPECT_CALL(*test->mNativeWindow, perform(19)).WillRepeatedly(Return(NO_ERROR));
268
269 // For simplicity, we only expect to create a single render surface for
270 // each test.
271 ASSERT_TRUE(test->mRenderSurface == nullptr);
272 test->mRenderSurface = new RE::mock::Surface();
273 EXPECT_CALL(*test->mRenderEngine, createSurface())
274 .WillOnce(Return(ByMove(std::unique_ptr<RE::Surface>(test->mRenderSurface))));
275 EXPECT_CALL(*test->mRenderSurface, setAsync(static_cast<bool>(ASYNC))).Times(1);
276 EXPECT_CALL(*test->mRenderSurface, setCritical(static_cast<bool>(CRITICAL))).Times(1);
277 EXPECT_CALL(*test->mRenderSurface, setNativeWindow(test->mNativeWindow.get())).Times(1);
278 EXPECT_CALL(*test->mRenderSurface, queryWidth()).WillOnce(Return(WIDTH));
279 EXPECT_CALL(*test->mRenderSurface, queryHeight()).WillOnce(Return(HEIGHT));
280 }
281
282 static void setupFramebufferConsumerBufferQueueCallExpectations(DisplayTransactionTest* test) {
283 EXPECT_CALL(*test->mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR));
284 EXPECT_CALL(*test->mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR));
285 EXPECT_CALL(*test->mConsumer, setConsumerUsageBits(GRALLOC_USAGE))
286 .WillRepeatedly(Return(NO_ERROR));
287 EXPECT_CALL(*test->mConsumer, setDefaultBufferSize(WIDTH, HEIGHT))
288 .WillRepeatedly(Return(NO_ERROR));
289 EXPECT_CALL(*test->mConsumer, setMaxAcquiredBufferCount(_))
290 .WillRepeatedly(Return(NO_ERROR));
291 }
292
293 static void setupFramebufferProducerBufferQueueCallExpectations(DisplayTransactionTest* test) {
294 EXPECT_CALL(*test->mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return());
295 }
296};
297
298template <hwc2_display_t hwcDisplayId, HWC2::DisplayType hwcDisplayType, typename DisplayVariant>
299struct HwcDisplayVariant {
300 // The display id supplied by the HWC
301 static constexpr hwc2_display_t HWC_DISPLAY_ID = hwcDisplayId;
302
303 // The HWC display type
304 static constexpr HWC2::DisplayType HWC_DISPLAY_TYPE = hwcDisplayType;
305
306 // The HWC active configuration id
307 // TODO(b/69807179): SurfaceFlinger does not correctly get the active
308 // config. Once it does, change this to non-zero so that it is properly
309 // covered.
310 // static constexpr int HWC_ACTIVE_CONFIG_ID = 2001;
311 static constexpr int HWC_ACTIVE_CONFIG_ID = 0;
312
313 static void injectPendingHotplugEvent(DisplayTransactionTest* test,
314 HWC2::Connection connection) {
315 test->mFlinger.mutablePendingHotplugEvents().emplace_back(
316 HotplugEvent{HWC_DISPLAY_ID, connection});
317 }
318
319 // Called by tests to inject a HWC display setup
320 static void injectHwcDisplay(DisplayTransactionTest* test) {
321 FakeHwcDisplayInjector(DisplayVariant::TYPE, HWC_DISPLAY_TYPE)
322 .setHwcDisplayId(HWC_DISPLAY_ID)
323 .setWidth(DisplayVariant::WIDTH)
324 .setHeight(DisplayVariant::HEIGHT)
325 .setActiveConfig(HWC_ACTIVE_CONFIG_ID)
326 .inject(&test->mFlinger, test->mComposer);
327 }
328
329 static void setupHwcHotplugCallExpectations(DisplayTransactionTest* test) {
330 EXPECT_CALL(*test->mComposer, getDisplayType(HWC_DISPLAY_ID, _))
331 .WillOnce(DoAll(SetArgPointee<1>(static_cast<IComposerClient::DisplayType>(
332 HWC_DISPLAY_TYPE)),
333 Return(Error::NONE)));
334 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
335 EXPECT_CALL(*test->mComposer, getDisplayConfigs(HWC_DISPLAY_ID, _))
336 .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{HWC_ACTIVE_CONFIG_ID}),
337 Return(Error::NONE)));
338 EXPECT_CALL(*test->mComposer,
339 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
340 IComposerClient::Attribute::WIDTH, _))
341 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::WIDTH), Return(Error::NONE)));
342 EXPECT_CALL(*test->mComposer,
343 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
344 IComposerClient::Attribute::HEIGHT, _))
345 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::HEIGHT), Return(Error::NONE)));
346 EXPECT_CALL(*test->mComposer,
347 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
348 IComposerClient::Attribute::VSYNC_PERIOD, _))
349 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE)));
350 EXPECT_CALL(*test->mComposer,
351 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
352 IComposerClient::Attribute::DPI_X, _))
353 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
354 EXPECT_CALL(*test->mComposer,
355 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
356 IComposerClient::Attribute::DPI_Y, _))
357 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
358 }
359
360 // Called by tests to set up HWC call expectations
361 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
362 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY_ID, _))
363 .WillOnce(DoAll(SetArgPointee<1>(HWC_ACTIVE_CONFIG_ID), Return(Error::NONE)));
364 }
365};
366
367struct NonHwcDisplayVariant {
368 static constexpr int HWC_ACTIVE_CONFIG_ID = 0;
369
370 static void injectHwcDisplay(DisplayTransactionTest*) {}
371
372 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
373 EXPECT_CALL(*test->mComposer, getActiveConfig(_, _)).Times(0);
374 }
375};
376
377// Physical displays are expected to be synchronous, secure, and have a HWC display for output.
378constexpr uint32_t GRALLOC_USAGE_PHYSICAL_DISPLAY =
379 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
380
381template <hwc2_display_t hwcDisplayId, DisplayDevice::DisplayType type, int width, int height,
382 Critical critical>
383struct PhysicalDisplayVariant
384 : public DisplayVariant<type, type, width, height, critical, Async::FALSE, Secure::TRUE,
385 GRALLOC_USAGE_PHYSICAL_DISPLAY>,
386 public HwcDisplayVariant<hwcDisplayId, HWC2::DisplayType::Physical,
387 DisplayVariant<type, type, width, height, critical, Async::FALSE,
388 Secure::TRUE, GRALLOC_USAGE_PHYSICAL_DISPLAY>> {};
389
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800390// An invalid display
391using InvalidDisplayVariant =
392 DisplayVariant<DisplayDevice::DISPLAY_ID_INVALID, DisplayDevice::DISPLAY_ID_INVALID, 0, 0,
393 Critical::FALSE, Async::FALSE, Secure::FALSE, 0>;
394
Lloyd Piquefc12f562018-01-22 18:44:59 -0800395// A primary display is a physical display that is critical
396using PrimaryDisplayVariant =
397 PhysicalDisplayVariant<1001, DisplayDevice::DISPLAY_PRIMARY, 3840, 2160, Critical::TRUE>;
398
399// An external display is physical display that is not critical.
400using ExternalDisplayVariant =
401 PhysicalDisplayVariant<1002, DisplayDevice::DISPLAY_EXTERNAL, 1920, 1280, Critical::FALSE>;
402
403using TertiaryDisplayVariant =
404 PhysicalDisplayVariant<1003, DisplayDevice::DISPLAY_EXTERNAL, 1600, 1200, Critical::FALSE>;
405
406// A virtual display not supported by the HWC.
407constexpr uint32_t GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY = 0;
408
409template <int width, int height, Secure secure>
410struct NonHwcVirtualDisplayVariant
411 : public DisplayVariant<DisplayDevice::DISPLAY_VIRTUAL, DisplayDevice::DISPLAY_ID_INVALID,
412 width, height, Critical::FALSE, Async::TRUE, secure,
413 GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>,
414 public NonHwcDisplayVariant {
415 using Base = DisplayVariant<DisplayDevice::DISPLAY_VIRTUAL, DisplayDevice::DISPLAY_ID_INVALID,
416 width, height, Critical::FALSE, Async::TRUE, secure,
417 GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>;
418
419 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
420 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
421 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
422 }
423};
424
425// A virtual display supported by the HWC.
426constexpr uint32_t GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY = GRALLOC_USAGE_HW_COMPOSER;
427
428template <int width, int height, Secure secure>
429struct HwcVirtualDisplayVariant
430 : public DisplayVariant<DisplayDevice::DISPLAY_VIRTUAL, DisplayDevice::DISPLAY_VIRTUAL, width,
431 height, Critical::FALSE, Async::TRUE, secure,
432 GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>,
433 public HwcDisplayVariant<1010, HWC2::DisplayType::Virtual,
434 NonHwcVirtualDisplayVariant<width, height, secure>> {
435 using Base =
436 DisplayVariant<DisplayDevice::DISPLAY_VIRTUAL, DisplayDevice::DISPLAY_VIRTUAL, width,
437 height, Critical::FALSE, Async::TRUE, secure, GRALLOC_USAGE_HW_COMPOSER>;
438 using Self = HwcVirtualDisplayVariant<width, height, secure>;
439
440 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
441 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
442 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
443 }
444
445 static void setupHwcVirtualDisplayCreationCallExpectations(DisplayTransactionTest* test) {
446 EXPECT_CALL(*test->mComposer, createVirtualDisplay(Base::WIDTH, Base::HEIGHT, _, _))
447 .WillOnce(DoAll(SetArgPointee<3>(Self::HWC_DISPLAY_ID), Return(Error::NONE)));
448 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
449 }
450};
451
452// For this variant, SurfaceFlinger should not configure itself with wide
453// display support, so the display should not be configured for wide-color
454// support.
455struct WideColorSupportNotConfiguredVariant {
456 static constexpr bool WIDE_COLOR_SUPPORTED = false;
457
458 static void injectConfigChange(DisplayTransactionTest* test) {
459 test->mFlinger.mutableHasWideColorDisplay() = false;
460 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
461 }
462
463 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
464 EXPECT_CALL(*test->mComposer, getColorModes(_, _)).Times(0);
465 EXPECT_CALL(*test->mComposer, getRenderIntents(_, _, _)).Times(0);
466 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
467 }
468};
469
470// For this variant, SurfaceFlinger should configure itself with wide display
471// support, and the display should respond with an non-empty list of supported
472// color modes. Wide-color support should be configured.
473template <typename Display>
474struct WideColorP3ColorimetricSupportedVariant {
475 static constexpr bool WIDE_COLOR_SUPPORTED = true;
476
477 static void injectConfigChange(DisplayTransactionTest* test) {
478 test->mFlinger.mutableHasWideColorDisplay() = true;
479 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
480 }
481
482 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
483 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
484 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>({ColorMode::DISPLAY_P3})),
485 Return(Error::NONE)));
486 EXPECT_CALL(*test->mComposer,
487 getRenderIntents(Display::HWC_DISPLAY_ID, ColorMode::DISPLAY_P3, _))
488 .WillOnce(DoAll(SetArgPointee<2>(
489 std::vector<RenderIntent>({RenderIntent::COLORIMETRIC})),
490 Return(Error::NONE)));
491 EXPECT_CALL(*test->mComposer,
492 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::SRGB,
493 RenderIntent::COLORIMETRIC))
494 .WillOnce(Return(Error::NONE));
495 }
496};
497
Lloyd Piquefc12f562018-01-22 18:44:59 -0800498// For this variant, SurfaceFlinger should configure itself with wide display
499// support, but the display should respond with an empty list of supported color
500// modes. Wide-color support for the display should not be configured.
501template <typename Display>
502struct WideColorNotSupportedVariant {
503 static constexpr bool WIDE_COLOR_SUPPORTED = false;
504
505 static void injectConfigChange(DisplayTransactionTest* test) {
506 test->mFlinger.mutableHasWideColorDisplay() = true;
507 }
508
509 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
510 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
511 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>()), Return(Error::NONE)));
512 EXPECT_CALL(*test->mComposer,
513 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::NATIVE,
514 RenderIntent::COLORIMETRIC))
515 .WillOnce(Return(Error::NONE));
516 }
517};
518
519// For this variant, the display is not a HWC display, so no HDR support should
520// be configured.
521struct NonHwcDisplayHdrSupportVariant {
522 static constexpr bool HDR10_SUPPORTED = false;
523 static constexpr bool HDR_HLG_SUPPORTED = false;
524 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
525 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
526 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
527 }
528};
529
530// For this variant, the composer should respond with a non-empty list of HDR
531// modes containing HDR10, so HDR10 support should be configured.
532template <typename Display>
533struct Hdr10SupportedVariant {
534 static constexpr bool HDR10_SUPPORTED = true;
535 static constexpr bool HDR_HLG_SUPPORTED = false;
536 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
537 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
538 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
539 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HDR10})),
540 Return(Error::NONE)));
541 }
542};
543
544// For this variant, the composer should respond with a non-empty list of HDR
545// modes containing HLG, so HLG support should be configured.
546template <typename Display>
547struct HdrHlgSupportedVariant {
548 static constexpr bool HDR10_SUPPORTED = false;
549 static constexpr bool HDR_HLG_SUPPORTED = true;
550 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
551 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
552 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
553 .WillOnce(
554 DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HLG})), Return(Error::NONE)));
555 }
556};
557
558// For this variant, the composer should respond with a non-empty list of HDR
559// modes containing DOLBY_VISION, so DOLBY_VISION support should be configured.
560template <typename Display>
561struct HdrDolbyVisionSupportedVariant {
562 static constexpr bool HDR10_SUPPORTED = false;
563 static constexpr bool HDR_HLG_SUPPORTED = false;
564 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = true;
565 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
566 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
567 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::DOLBY_VISION})),
568 Return(Error::NONE)));
569 }
570};
571
572// For this variant, the composer should respond with am empty list of HDR
573// modes, so no HDR support should be configured.
574template <typename Display>
575struct HdrNotSupportedVariant {
576 static constexpr bool HDR10_SUPPORTED = false;
577 static constexpr bool HDR_HLG_SUPPORTED = false;
578 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
579 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
580 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
581 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
582 }
583};
584
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700585struct NonHwcPerFrameMetadataSupportVariant {
586 static constexpr int PER_FRAME_METADATA_KEYS = 0;
587 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
588 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(_, _)).Times(0);
589 }
590};
591
592template <typename Display>
593struct NoPerFrameMetadataSupportVariant {
594 static constexpr int PER_FRAME_METADATA_KEYS = 0;
595 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
596 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID, _))
597 .WillOnce(DoAll(SetArgPointee<1>(std::vector<PerFrameMetadataKey>()),
598 Return(Error::NONE)));
599 }
600};
601
602template <typename Display>
603struct Smpte2086PerFrameMetadataSupportVariant {
604 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::SMPTE2086;
605 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
606 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID, _))
607 .WillOnce(DoAll(SetArgPointee<1>(std::vector<PerFrameMetadataKey>({
608 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X,
609 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y,
610 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X,
611 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y,
612 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X,
613 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y,
614 PerFrameMetadataKey::WHITE_POINT_X,
615 PerFrameMetadataKey::WHITE_POINT_Y,
616 PerFrameMetadataKey::MAX_LUMINANCE,
617 PerFrameMetadataKey::MIN_LUMINANCE,
618 })),
619 Return(Error::NONE)));
620 }
621};
622
623template <typename Display>
624struct Cta861_3_PerFrameMetadataSupportVariant {
625 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::CTA861_3;
626 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
627 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID, _))
628 .WillOnce(DoAll(SetArgPointee<1>(std::vector<PerFrameMetadataKey>({
629 PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL,
630 PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL,
631 })),
632 Return(Error::NONE)));
633 }
634};
635
Lloyd Piquefc12f562018-01-22 18:44:59 -0800636/* ------------------------------------------------------------------------
637 * Typical display configurations to test
638 */
639
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700640template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy,
641 typename PerFrameMetadataSupportPolicy>
Lloyd Piquefc12f562018-01-22 18:44:59 -0800642struct Case {
643 using Display = DisplayPolicy;
644 using WideColorSupport = WideColorSupportPolicy;
645 using HdrSupport = HdrSupportPolicy;
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700646 using PerFrameMetadataSupport = PerFrameMetadataSupportPolicy;
Lloyd Piquefc12f562018-01-22 18:44:59 -0800647};
648
649using SimplePrimaryDisplayCase =
650 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700651 HdrNotSupportedVariant<PrimaryDisplayVariant>,
652 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquefc12f562018-01-22 18:44:59 -0800653using SimpleExternalDisplayCase =
654 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700655 HdrNotSupportedVariant<ExternalDisplayVariant>,
656 NoPerFrameMetadataSupportVariant<ExternalDisplayVariant>>;
Lloyd Piquefc12f562018-01-22 18:44:59 -0800657using SimpleTertiaryDisplayCase =
658 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700659 HdrNotSupportedVariant<TertiaryDisplayVariant>,
660 NoPerFrameMetadataSupportVariant<TertiaryDisplayVariant>>;
Lloyd Piquefc12f562018-01-22 18:44:59 -0800661using NonHwcVirtualDisplayCase =
662 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700663 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant,
664 NonHwcPerFrameMetadataSupportVariant>;
Lloyd Piquefc12f562018-01-22 18:44:59 -0800665using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
666using HwcVirtualDisplayCase =
667 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700668 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>,
669 NoPerFrameMetadataSupportVariant<SimpleHwcVirtualDisplayVariant>>;
Lloyd Piquefc12f562018-01-22 18:44:59 -0800670using WideColorP3ColorimetricDisplayCase =
671 Case<PrimaryDisplayVariant, WideColorP3ColorimetricSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700672 HdrNotSupportedVariant<PrimaryDisplayVariant>,
673 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquefc12f562018-01-22 18:44:59 -0800674using Hdr10DisplayCase =
675 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700676 Hdr10SupportedVariant<PrimaryDisplayVariant>,
677 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquefc12f562018-01-22 18:44:59 -0800678using HdrHlgDisplayCase =
679 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700680 HdrHlgSupportedVariant<PrimaryDisplayVariant>,
681 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquefc12f562018-01-22 18:44:59 -0800682using HdrDolbyVisionDisplayCase =
683 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700684 HdrDolbyVisionSupportedVariant<PrimaryDisplayVariant>,
685 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
686using HdrSmpte2086DisplayCase =
687 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
688 HdrNotSupportedVariant<PrimaryDisplayVariant>,
689 Smpte2086PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
690using HdrCta861_3_DisplayCase =
691 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
692 HdrNotSupportedVariant<PrimaryDisplayVariant>,
693 Cta861_3_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800694using InvalidDisplayCase = Case<InvalidDisplayVariant, WideColorSupportNotConfiguredVariant,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700695 NonHwcDisplayHdrSupportVariant,
696 NoPerFrameMetadataSupportVariant<InvalidDisplayVariant>>;
Lloyd Piquefc12f562018-01-22 18:44:59 -0800697/* ------------------------------------------------------------------------
Lloyd Pique6cf11032018-01-22 18:57:44 -0800698 *
699 * SurfaceFlinger::onHotplugReceived
700 */
701
702TEST_F(DisplayTransactionTest, hotplugEnqueuesEventsForDisplayTransaction) {
703 constexpr int currentSequenceId = 123;
704 constexpr hwc2_display_t displayId1 = 456;
705 constexpr hwc2_display_t displayId2 = 654;
706
707 // --------------------------------------------------------------------
708 // Preconditions
709
710 // Set the current sequence id for accepted events
711 mFlinger.mutableComposerSequenceId() = currentSequenceId;
712
713 // Set the main thread id so that the current thread does not appear to be
714 // the main thread.
715 mFlinger.mutableMainThreadId() = std::thread::id();
716
717 // --------------------------------------------------------------------
718 // Call Expectations
719
720 // We expect invalidate() to be invoked once to trigger display transaction
721 // processing.
722 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
723
724 // --------------------------------------------------------------------
725 // Invocation
726
727 // Simulate two hotplug events (a connect and a disconnect)
728 mFlinger.onHotplugReceived(currentSequenceId, displayId1, HWC2::Connection::Connected);
729 mFlinger.onHotplugReceived(currentSequenceId, displayId2, HWC2::Connection::Disconnected);
730
731 // --------------------------------------------------------------------
732 // Postconditions
733
734 // The display transaction needed flag should be set.
735 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
736
737 // All events should be in the pending event queue.
738 const auto& pendingEvents = mFlinger.mutablePendingHotplugEvents();
739 ASSERT_EQ(2u, pendingEvents.size());
740 EXPECT_EQ(displayId1, pendingEvents[0].display);
741 EXPECT_EQ(HWC2::Connection::Connected, pendingEvents[0].connection);
742 EXPECT_EQ(displayId2, pendingEvents[1].display);
743 EXPECT_EQ(HWC2::Connection::Disconnected, pendingEvents[1].connection);
744}
745
746TEST_F(DisplayTransactionTest, hotplugDiscardsUnexpectedEvents) {
747 constexpr int currentSequenceId = 123;
748 constexpr int otherSequenceId = 321;
749 constexpr hwc2_display_t displayId = 456;
750
751 // --------------------------------------------------------------------
752 // Preconditions
753
754 // Set the current sequence id for accepted events
755 mFlinger.mutableComposerSequenceId() = currentSequenceId;
756
757 // Set the main thread id so that the current thread does not appear to be
758 // the main thread.
759 mFlinger.mutableMainThreadId() = std::thread::id();
760
761 // --------------------------------------------------------------------
762 // Call Expectations
763
764 // We do not expect any calls to invalidate().
765 EXPECT_CALL(*mMessageQueue, invalidate()).Times(0);
766
767 // --------------------------------------------------------------------
768 // Invocation
769
770 // Call with an unexpected sequence id
771 mFlinger.onHotplugReceived(otherSequenceId, displayId, HWC2::Connection::Invalid);
772
773 // --------------------------------------------------------------------
774 // Postconditions
775
776 // The display transaction needed flag should not be set
777 EXPECT_FALSE(hasTransactionFlagSet(eDisplayTransactionNeeded));
778
779 // There should be no pending events
780 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
781}
782
783TEST_F(DisplayTransactionTest, hotplugProcessesEnqueuedEventsIfCalledOnMainThread) {
784 constexpr int currentSequenceId = 123;
785 constexpr hwc2_display_t displayId1 = 456;
786
787 // --------------------------------------------------------------------
788 // Note:
789 // --------------------------------------------------------------------
790 // This test case is a bit tricky. We want to verify that
791 // onHotplugReceived() calls processDisplayHotplugEventsLocked(), but we
792 // don't really want to provide coverage for everything the later function
793 // does as there are specific tests for it.
794 // --------------------------------------------------------------------
795
796 // --------------------------------------------------------------------
797 // Preconditions
798
799 // Set the current sequence id for accepted events
800 mFlinger.mutableComposerSequenceId() = currentSequenceId;
801
802 // Set the main thread id so that the current thread does appear to be the
803 // main thread.
804 mFlinger.mutableMainThreadId() = std::this_thread::get_id();
805
806 // --------------------------------------------------------------------
807 // Call Expectations
808
809 // We expect invalidate() to be invoked once to trigger display transaction
810 // processing.
811 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
812
813 // --------------------------------------------------------------------
814 // Invocation
815
816 // Simulate a disconnect on a display id that is not connected. This should
817 // be enqueued by onHotplugReceived(), and dequeued by
818 // processDisplayHotplugEventsLocked(), but then ignored as invalid.
819 mFlinger.onHotplugReceived(currentSequenceId, displayId1, HWC2::Connection::Disconnected);
820
821 // --------------------------------------------------------------------
822 // Postconditions
823
824 // The display transaction needed flag should be set.
825 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
826
827 // There should be no event queued on return, as it should have been
828 // processed.
829 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
830}
831
832/* ------------------------------------------------------------------------
Lloyd Piquea482f992018-01-22 19:00:34 -0800833 * SurfaceFlinger::createDisplay
834 */
835
836TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForNonsecureDisplay) {
837 const String8 name("virtual.test");
838
839 // --------------------------------------------------------------------
840 // Call Expectations
841
842 // The call should notify the interceptor that a display was created.
843 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
844
845 // --------------------------------------------------------------------
846 // Invocation
847
848 sp<IBinder> displayToken = mFlinger.createDisplay(name, false);
849
850 // --------------------------------------------------------------------
851 // Postconditions
852
853 // The display should have been added to the current state
854 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
855 const auto& display = getCurrentDisplayState(displayToken);
856 EXPECT_EQ(DisplayDevice::DISPLAY_VIRTUAL, display.type);
857 EXPECT_EQ(false, display.isSecure);
858 EXPECT_EQ(name.string(), display.displayName);
859
860 // --------------------------------------------------------------------
861 // Cleanup conditions
862
863 // Destroying the display invalidates the display state.
864 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
865}
866
867TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForSecureDisplay) {
868 const String8 name("virtual.test");
869
870 // --------------------------------------------------------------------
871 // Call Expectations
872
873 // The call should notify the interceptor that a display was created.
874 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
875
876 // --------------------------------------------------------------------
877 // Invocation
878
879 sp<IBinder> displayToken = mFlinger.createDisplay(name, true);
880
881 // --------------------------------------------------------------------
882 // Postconditions
883
884 // The display should have been added to the current state
885 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
886 const auto& display = getCurrentDisplayState(displayToken);
887 EXPECT_EQ(DisplayDevice::DISPLAY_VIRTUAL, display.type);
888 EXPECT_EQ(true, display.isSecure);
889 EXPECT_EQ(name.string(), display.displayName);
890
891 // --------------------------------------------------------------------
892 // Cleanup conditions
893
894 // Destroying the display invalidates the display state.
895 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
896}
897
898/* ------------------------------------------------------------------------
899 * SurfaceFlinger::destroyDisplay
900 */
901
902TEST_F(DisplayTransactionTest, destroyDisplayClearsCurrentStateForDisplay) {
903 using Case = NonHwcVirtualDisplayCase;
904
905 // --------------------------------------------------------------------
906 // Preconditions
907
908 // A virtual display exists
909 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
910 existing.inject();
911
912 // --------------------------------------------------------------------
913 // Call Expectations
914
915 // The call should notify the interceptor that a display was created.
916 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
917
918 // Destroying the display invalidates the display state.
919 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
920
921 // --------------------------------------------------------------------
922 // Invocation
923
924 mFlinger.destroyDisplay(existing.token());
925
926 // --------------------------------------------------------------------
927 // Postconditions
928
929 // The display should have been removed from the current state
930 EXPECT_FALSE(hasCurrentDisplayState(existing.token()));
931
932 // Ths display should still exist in the drawing state
933 EXPECT_TRUE(hasDrawingDisplayState(existing.token()));
934
935 // The display transaction needed flasg should be set
936 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
937}
938
939TEST_F(DisplayTransactionTest, destroyDisplayHandlesUnknownDisplay) {
940 // --------------------------------------------------------------------
941 // Preconditions
942
943 sp<BBinder> displayToken = new BBinder();
944
945 // --------------------------------------------------------------------
946 // Invocation
947
948 mFlinger.destroyDisplay(displayToken);
949}
950
951/* ------------------------------------------------------------------------
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -0800952 * SurfaceFlinger::resetDisplayState
953 */
954
955TEST_F(DisplayTransactionTest, resetDisplayStateClearsState) {
956 using Case = NonHwcVirtualDisplayCase;
957
958 // --------------------------------------------------------------------
959 // Preconditions
960
961 // vsync is enabled and available
962 mFlinger.mutablePrimaryHWVsyncEnabled() = true;
963 mFlinger.mutableHWVsyncAvailable() = true;
964
965 // A display exists
966 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
967 existing.inject();
968
969 // --------------------------------------------------------------------
970 // Call Expectations
971
972 // The call disable vsyncs
973 EXPECT_CALL(*mEventControlThread, setVsyncEnabled(false)).Times(1);
974
975 // The call clears the current render engine surface
976 EXPECT_CALL(*mRenderEngine, resetCurrentSurface());
977
978 // --------------------------------------------------------------------
979 // Invocation
980
981 mFlinger.resetDisplayState();
982
983 // --------------------------------------------------------------------
984 // Postconditions
985
986 // vsyncs should be off and not available.
987 EXPECT_FALSE(mFlinger.mutablePrimaryHWVsyncEnabled());
988 EXPECT_FALSE(mFlinger.mutableHWVsyncAvailable());
989
990 // The display should have been removed from the display map.
991 EXPECT_FALSE(hasDisplayDevice(existing.token()));
992
993 // The display should still exist in the current state
994 EXPECT_TRUE(hasCurrentDisplayState(existing.token()));
995
996 // The display should have been removed from the drawing state
997 EXPECT_FALSE(hasDrawingDisplayState(existing.token()));
998}
999
1000/* ------------------------------------------------------------------------
Lloyd Piquefc12f562018-01-22 18:44:59 -08001001 * SurfaceFlinger::setupNewDisplayDeviceInternal
1002 */
1003
1004class SetupNewDisplayDeviceInternalTest : public DisplayTransactionTest {
1005public:
1006 template <typename T>
1007 void setupNewDisplayDeviceInternalTest();
1008};
1009
1010template <typename Case>
1011void SetupNewDisplayDeviceInternalTest::setupNewDisplayDeviceInternalTest() {
1012 const sp<BBinder> displayToken = new BBinder();
1013 const sp<mock::DisplaySurface> displaySurface = new mock::DisplaySurface();
1014 const sp<mock::GraphicBufferProducer> producer = new mock::GraphicBufferProducer();
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001015
1016 // --------------------------------------------------------------------
1017 // Preconditions
1018
Lloyd Piquefc12f562018-01-22 18:44:59 -08001019 // Wide color displays support is configured appropriately
1020 Case::WideColorSupport::injectConfigChange(this);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001021
Lloyd Piquefc12f562018-01-22 18:44:59 -08001022 // The display is setup with the HWC.
1023 Case::Display::injectHwcDisplay(this);
1024
1025 // SurfaceFlinger will use a test-controlled factory for native window
1026 // surfaces.
1027 injectFakeNativeWindowSurfaceFactory();
1028
1029 // --------------------------------------------------------------------
1030 // Call Expectations
1031
1032 // Various native window calls will be made.
1033 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1034
1035 // TODO(b/69807179): SurfaceFlinger does not correctly get the active config.
1036 // Case::Display::setupHwcGetActiveConfigCallExpectations(this)
1037
1038 Case::WideColorSupport::setupComposerCallExpectations(this);
1039 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001040 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquefc12f562018-01-22 18:44:59 -08001041
1042 // --------------------------------------------------------------------
1043 // Invocation
1044
1045 auto state = DisplayDeviceState(Case::Display::TYPE, static_cast<bool>(Case::Display::SECURE));
1046 auto device = mFlinger.setupNewDisplayDeviceInternal(displayToken, Case::Display::TYPE, state,
1047 displaySurface, producer);
1048
1049 // --------------------------------------------------------------------
1050 // Postconditions
1051
1052 ASSERT_TRUE(device != nullptr);
1053 EXPECT_EQ(Case::Display::TYPE, device->getDisplayType());
1054 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
1055 EXPECT_EQ(Case::Display::WIDTH, device->getWidth());
1056 EXPECT_EQ(Case::Display::HEIGHT, device->getHeight());
1057 EXPECT_EQ(Case::WideColorSupport::WIDE_COLOR_SUPPORTED, device->hasWideColorGamut());
1058 EXPECT_EQ(Case::HdrSupport::HDR10_SUPPORTED, device->hasHDR10Support());
1059 EXPECT_EQ(Case::HdrSupport::HDR_HLG_SUPPORTED, device->hasHLGSupport());
1060 EXPECT_EQ(Case::HdrSupport::HDR_DOLBY_VISION_SUPPORTED, device->hasDolbyVisionSupport());
1061 EXPECT_EQ(Case::Display::HWC_ACTIVE_CONFIG_ID, device->getActiveConfig());
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001062 EXPECT_EQ(Case::PerFrameMetadataSupport::PER_FRAME_METADATA_KEYS,
1063 device->getSupportedPerFrameMetadata());
Lloyd Piquefc12f562018-01-22 18:44:59 -08001064}
1065
1066TEST_F(SetupNewDisplayDeviceInternalTest, createSimplePrimaryDisplay) {
1067 setupNewDisplayDeviceInternalTest<SimplePrimaryDisplayCase>();
1068}
1069
1070TEST_F(SetupNewDisplayDeviceInternalTest, createSimpleExternalDisplay) {
1071 setupNewDisplayDeviceInternalTest<SimpleExternalDisplayCase>();
1072}
1073
1074TEST_F(SetupNewDisplayDeviceInternalTest, createNonHwcVirtualDisplay) {
1075 setupNewDisplayDeviceInternalTest<NonHwcVirtualDisplayCase>();
1076}
1077
1078TEST_F(SetupNewDisplayDeviceInternalTest, createHwcVirtualDisplay) {
1079 // We need to resize this so that the HWC thinks the virtual display
1080 // is something it created.
1081 mFlinger.mutableHwcDisplayData().resize(3);
1082
1083 setupNewDisplayDeviceInternalTest<HwcVirtualDisplayCase>();
1084}
1085
1086TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3Display) {
1087 setupNewDisplayDeviceInternalTest<WideColorP3ColorimetricDisplayCase>();
1088}
1089
Lloyd Piquefc12f562018-01-22 18:44:59 -08001090TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10Display) {
1091 setupNewDisplayDeviceInternalTest<Hdr10DisplayCase>();
1092}
1093
1094TEST_F(SetupNewDisplayDeviceInternalTest, createHdrHlgDisplay) {
1095 setupNewDisplayDeviceInternalTest<HdrHlgDisplayCase>();
1096}
1097
1098TEST_F(SetupNewDisplayDeviceInternalTest, createHdrDolbyVisionDisplay) {
1099 setupNewDisplayDeviceInternalTest<HdrDolbyVisionDisplayCase>();
1100}
1101
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001102TEST_F(SetupNewDisplayDeviceInternalTest, createHdrSmpte2086DisplayCase) {
1103 setupNewDisplayDeviceInternalTest<HdrSmpte2086DisplayCase>();
1104}
1105
1106TEST_F(SetupNewDisplayDeviceInternalTest, createHdrCta816_3_DisplayCase) {
1107 setupNewDisplayDeviceInternalTest<HdrCta861_3_DisplayCase>();
1108}
1109
Lloyd Piquefc12f562018-01-22 18:44:59 -08001110/* ------------------------------------------------------------------------
1111 * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded)
1112 */
1113
1114class HandleTransactionLockedTest : public DisplayTransactionTest {
1115public:
1116 template <typename Case>
1117 void setupCommonPreconditions();
1118
1119 template <typename Case>
1120 void setupCommonCallExpectationsForConnectProcessing();
1121
1122 template <typename Case>
1123 void setupCommonCallExpectationsForDisconnectProcessing();
1124
1125 template <typename Case>
1126 void processesHotplugConnectCommon();
1127
1128 template <typename Case>
1129 void ignoresHotplugConnectCommon();
1130
1131 template <typename Case>
1132 void processesHotplugDisconnectCommon();
1133
1134 template <typename Case>
1135 void verifyDisplayIsConnected(const sp<IBinder>& displayToken);
1136
1137 template <typename Case>
1138 void verifyPhysicalDisplayIsConnected();
1139
1140 void verifyDisplayIsNotConnected(const sp<IBinder>& displayToken);
1141};
1142
1143template <typename Case>
1144void HandleTransactionLockedTest::setupCommonPreconditions() {
1145 // Wide color displays support is configured appropriately
1146 Case::WideColorSupport::injectConfigChange(this);
1147
1148 // SurfaceFlinger will use a test-controlled factory for BufferQueues
1149 injectFakeBufferQueueFactory();
1150
1151 // SurfaceFlinger will use a test-controlled factory for native window
1152 // surfaces.
1153 injectFakeNativeWindowSurfaceFactory();
1154}
1155
1156template <typename Case>
1157void HandleTransactionLockedTest::setupCommonCallExpectationsForConnectProcessing() {
1158 Case::Display::setupHwcHotplugCallExpectations(this);
1159
1160 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1161 Case::Display::setupFramebufferProducerBufferQueueCallExpectations(this);
1162 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1163 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
1164
1165 Case::WideColorSupport::setupComposerCallExpectations(this);
1166 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001167 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquefc12f562018-01-22 18:44:59 -08001168
1169 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
1170 EXPECT_CALL(*mEventThread, onHotplugReceived(Case::Display::TYPE, true)).Times(1);
1171}
1172
1173template <typename Case>
1174void HandleTransactionLockedTest::setupCommonCallExpectationsForDisconnectProcessing() {
1175 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
1176 EXPECT_CALL(*mEventThread, onHotplugReceived(Case::Display::TYPE, false)).Times(1);
1177}
1178
1179template <typename Case>
1180void HandleTransactionLockedTest::verifyDisplayIsConnected(const sp<IBinder>& displayToken) {
1181 // The display device should have been set up in the list of displays.
1182 ASSERT_TRUE(hasDisplayDevice(displayToken));
1183 const auto& device = getDisplayDevice(displayToken);
1184 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
1185 EXPECT_EQ(Case::Display::TYPE == DisplayDevice::DISPLAY_PRIMARY, device->isPrimary());
1186
1187 // The display should have been set up in the current display state
1188 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1189 const auto& current = getCurrentDisplayState(displayToken);
1190 EXPECT_EQ(Case::Display::TYPE, current.type);
1191
1192 // The display should have been set up in the drawing display state
1193 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1194 const auto& draw = getDrawingDisplayState(displayToken);
1195 EXPECT_EQ(Case::Display::TYPE, draw.type);
1196}
1197
1198template <typename Case>
1199void HandleTransactionLockedTest::verifyPhysicalDisplayIsConnected() {
1200 // HWComposer should have an entry for the display
1201 EXPECT_TRUE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1202
1203 // The display should be set up as a built-in display.
1204 static_assert(0 <= Case::Display::TYPE &&
1205 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES,
1206 "Must use a valid physical display type index for the fixed-size array");
1207 auto& displayToken = mFlinger.mutableBuiltinDisplays()[Case::Display::TYPE];
1208 ASSERT_TRUE(displayToken != nullptr);
1209
1210 verifyDisplayIsConnected<Case>(displayToken);
1211}
1212
1213void HandleTransactionLockedTest::verifyDisplayIsNotConnected(const sp<IBinder>& displayToken) {
1214 EXPECT_FALSE(hasDisplayDevice(displayToken));
1215 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1216 EXPECT_FALSE(hasDrawingDisplayState(displayToken));
1217}
1218
1219template <typename Case>
1220void HandleTransactionLockedTest::processesHotplugConnectCommon() {
1221 // --------------------------------------------------------------------
1222 // Preconditions
1223
1224 setupCommonPreconditions<Case>();
1225
1226 // A hotplug connect event is enqueued for a display
1227 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001228
1229 // --------------------------------------------------------------------
1230 // Call Expectations
1231
1232 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillOnce(Return(false));
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001233
Lloyd Piquefc12f562018-01-22 18:44:59 -08001234 setupCommonCallExpectationsForConnectProcessing<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001235
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001236 // --------------------------------------------------------------------
1237 // Invocation
Lloyd Piquee39cad22017-12-20 17:01:29 -08001238
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001239 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
Lloyd Piquee39cad22017-12-20 17:01:29 -08001240
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001241 // --------------------------------------------------------------------
1242 // Postconditions
1243
Lloyd Piquefc12f562018-01-22 18:44:59 -08001244 verifyPhysicalDisplayIsConnected<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001245
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001246 // --------------------------------------------------------------------
1247 // Cleanup conditions
1248
Lloyd Piquefc12f562018-01-22 18:44:59 -08001249 EXPECT_CALL(*mComposer,
1250 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001251 .WillOnce(Return(Error::NONE));
1252 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Piquef58625d2017-12-19 13:22:33 -08001253}
1254
Lloyd Piquefc12f562018-01-22 18:44:59 -08001255template <typename Case>
1256void HandleTransactionLockedTest::ignoresHotplugConnectCommon() {
1257 // --------------------------------------------------------------------
1258 // Preconditions
1259
1260 setupCommonPreconditions<Case>();
1261
1262 // A hotplug connect event is enqueued for a display
1263 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1264
1265 // --------------------------------------------------------------------
1266 // Invocation
1267
1268 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1269
1270 // --------------------------------------------------------------------
1271 // Postconditions
1272
1273 // HWComposer should not have an entry for the display
1274 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1275}
1276
1277template <typename Case>
1278void HandleTransactionLockedTest::processesHotplugDisconnectCommon() {
1279 // --------------------------------------------------------------------
1280 // Preconditions
1281
1282 setupCommonPreconditions<Case>();
1283
1284 // A hotplug disconnect event is enqueued for a display
1285 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1286
1287 // The display is already completely set up.
1288 Case::Display::injectHwcDisplay(this);
1289 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1290 existing.inject();
1291
1292 // --------------------------------------------------------------------
1293 // Call Expectations
1294
1295 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1296
1297 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1298
1299 // --------------------------------------------------------------------
1300 // Invocation
1301
1302 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1303
1304 // --------------------------------------------------------------------
1305 // Postconditions
1306
1307 // HWComposer should not have an entry for the display
1308 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1309
1310 // The display should not be set up as a built-in display.
1311 ASSERT_TRUE(0 <= Case::Display::TYPE &&
1312 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES);
1313 auto displayToken = mFlinger.mutableBuiltinDisplays()[Case::Display::TYPE];
1314 EXPECT_TRUE(displayToken == nullptr);
1315
1316 // The existing token should have been removed
1317 verifyDisplayIsNotConnected(existing.token());
1318}
1319
1320TEST_F(HandleTransactionLockedTest, processesHotplugConnectPrimaryDisplay) {
1321 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1322}
1323
1324TEST_F(HandleTransactionLockedTest,
1325 processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected) {
1326 // Inject an external display.
1327 ExternalDisplayVariant::injectHwcDisplay(this);
1328
1329 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1330}
1331
1332TEST_F(HandleTransactionLockedTest, processesHotplugConnectExternalDisplay) {
1333 // Inject a primary display.
1334 PrimaryDisplayVariant::injectHwcDisplay(this);
1335
1336 processesHotplugConnectCommon<SimpleExternalDisplayCase>();
1337}
1338
1339TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected) {
1340 // Inject both a primary and external display.
1341 PrimaryDisplayVariant::injectHwcDisplay(this);
1342 ExternalDisplayVariant::injectHwcDisplay(this);
1343
1344 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1345
1346 ignoresHotplugConnectCommon<SimpleTertiaryDisplayCase>();
1347}
1348
1349TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfExternalForVrComposer) {
1350 // Inject a primary display.
1351 PrimaryDisplayVariant::injectHwcDisplay(this);
1352
1353 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(true));
1354
1355 ignoresHotplugConnectCommon<SimpleExternalDisplayCase>();
1356}
1357
1358TEST_F(HandleTransactionLockedTest, processHotplugDisconnectPrimaryDisplay) {
1359 processesHotplugDisconnectCommon<SimplePrimaryDisplayCase>();
1360}
1361
1362TEST_F(HandleTransactionLockedTest, processHotplugDisconnectExternalDisplay) {
1363 processesHotplugDisconnectCommon<SimpleExternalDisplayCase>();
1364}
1365
1366TEST_F(HandleTransactionLockedTest, processesHotplugConnectThenDisconnectPrimary) {
1367 using Case = SimplePrimaryDisplayCase;
1368
1369 // --------------------------------------------------------------------
1370 // Preconditions
1371
1372 setupCommonPreconditions<Case>();
1373
1374 // A hotplug connect event is enqueued for a display
1375 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1376 // A hotplug disconnect event is also enqueued for the same display
1377 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1378
1379 // --------------------------------------------------------------------
1380 // Call Expectations
1381
1382 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1383
1384 setupCommonCallExpectationsForConnectProcessing<Case>();
1385 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1386
1387 EXPECT_CALL(*mComposer,
1388 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1389 .WillOnce(Return(Error::NONE));
1390 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1391
1392 // --------------------------------------------------------------------
1393 // Invocation
1394
1395 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1396
1397 // --------------------------------------------------------------------
1398 // Postconditions
1399
1400 // HWComposer should not have an entry for the display
1401 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1402
1403 // The display should not be set up as a primary built-in display.
1404 ASSERT_TRUE(0 <= Case::Display::TYPE &&
1405 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES);
1406 auto displayToken = mFlinger.mutableBuiltinDisplays()[Case::Display::TYPE];
1407 EXPECT_TRUE(displayToken == nullptr);
1408}
1409
1410TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectThenConnectPrimary) {
1411 using Case = SimplePrimaryDisplayCase;
1412
1413 // --------------------------------------------------------------------
1414 // Preconditions
1415
1416 setupCommonPreconditions<Case>();
1417
1418 // The display is already completely set up.
1419 Case::Display::injectHwcDisplay(this);
1420 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1421 existing.inject();
1422
1423 // A hotplug disconnect event is enqueued for a display
1424 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1425 // A hotplug connect event is also enqueued for the same display
1426 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1427
1428 // --------------------------------------------------------------------
1429 // Call Expectations
1430
1431 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1432
1433 setupCommonCallExpectationsForConnectProcessing<Case>();
1434 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1435
1436 // --------------------------------------------------------------------
1437 // Invocation
1438
1439 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1440
1441 // --------------------------------------------------------------------
1442 // Postconditions
1443
1444 // The existing token should have been removed
1445 verifyDisplayIsNotConnected(existing.token());
1446 static_assert(0 <= Case::Display::TYPE &&
1447 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES,
1448 "Display type must be a built-in display");
1449 EXPECT_NE(existing.token(), mFlinger.mutableBuiltinDisplays()[Case::Display::TYPE]);
1450
1451 // A new display should be connected in its place
1452
1453 verifyPhysicalDisplayIsConnected<Case>();
1454
1455 // --------------------------------------------------------------------
1456 // Cleanup conditions
1457
1458 EXPECT_CALL(*mComposer,
1459 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1460 .WillOnce(Return(Error::NONE));
1461 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1462}
1463
1464TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAdded) {
1465 using Case = HwcVirtualDisplayCase;
1466
1467 // --------------------------------------------------------------------
1468 // Preconditions
1469
1470 // The HWC supports at least one virtual display
1471 injectMockComposer(1);
1472
1473 setupCommonPreconditions<Case>();
1474
1475 // A virtual display was added to the current state, and it has a
1476 // surface(producer)
1477 sp<BBinder> displayToken = new BBinder();
1478 DisplayDeviceState info(Case::Display::TYPE, static_cast<bool>(Case::Display::SECURE));
1479 sp<mock::GraphicBufferProducer> surface{new mock::GraphicBufferProducer()};
1480 info.surface = surface;
1481 mFlinger.mutableCurrentState().displays.add(displayToken, info);
1482
1483 // --------------------------------------------------------------------
1484 // Call Expectations
1485
1486 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1487 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1488
1489 EXPECT_CALL(*surface, query(NATIVE_WINDOW_WIDTH, _))
1490 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::WIDTH), Return(NO_ERROR)));
1491 EXPECT_CALL(*surface, query(NATIVE_WINDOW_HEIGHT, _))
1492 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::HEIGHT), Return(NO_ERROR)));
1493 EXPECT_CALL(*surface, query(NATIVE_WINDOW_FORMAT, _))
1494 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT),
1495 Return(NO_ERROR)));
1496 EXPECT_CALL(*surface, query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, _))
1497 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(NO_ERROR)));
1498
1499 EXPECT_CALL(*surface, setAsyncMode(true)).Times(1);
1500
1501 EXPECT_CALL(*mProducer, connect(_, _, _, _)).Times(1);
1502 EXPECT_CALL(*mProducer, disconnect(_, _)).Times(1);
1503
1504 Case::Display::setupHwcVirtualDisplayCreationCallExpectations(this);
1505 Case::WideColorSupport::setupComposerCallExpectations(this);
1506 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001507 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquefc12f562018-01-22 18:44:59 -08001508
1509 // --------------------------------------------------------------------
1510 // Invocation
1511
1512 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1513
1514 // --------------------------------------------------------------------
1515 // Postconditions
1516
1517 // The display device should have been set up in the list of displays.
1518 verifyDisplayIsConnected<Case>(displayToken);
1519
1520 // --------------------------------------------------------------------
1521 // Cleanup conditions
1522
1523 EXPECT_CALL(*mComposer, destroyVirtualDisplay(Case::Display::HWC_DISPLAY_ID))
1524 .WillOnce(Return(Error::NONE));
1525 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1526}
1527
1528TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAddedWithNoSurface) {
1529 using Case = HwcVirtualDisplayCase;
1530
1531 // --------------------------------------------------------------------
1532 // Preconditions
1533
1534 // The HWC supports at least one virtual display
1535 injectMockComposer(1);
1536
1537 setupCommonPreconditions<Case>();
1538
1539 // A virtual display was added to the current state, but it does not have a
1540 // surface.
1541 sp<BBinder> displayToken = new BBinder();
1542 DisplayDeviceState info(Case::Display::TYPE, static_cast<bool>(Case::Display::SECURE));
1543 mFlinger.mutableCurrentState().displays.add(displayToken, info);
1544
1545 // --------------------------------------------------------------------
1546 // Call Expectations
1547
1548 // --------------------------------------------------------------------
1549 // Invocation
1550
1551 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1552
1553 // --------------------------------------------------------------------
1554 // Postconditions
1555
1556 // There will not be a display device set up.
1557 EXPECT_FALSE(hasDisplayDevice(displayToken));
1558
1559 // The drawing display state will be set from the current display state.
1560 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1561 const auto& draw = getDrawingDisplayState(displayToken);
1562 EXPECT_EQ(Case::Display::TYPE, draw.type);
1563}
1564
1565TEST_F(HandleTransactionLockedTest, processesVirtualDisplayRemoval) {
1566 using Case = HwcVirtualDisplayCase;
1567
1568 // --------------------------------------------------------------------
1569 // Preconditions
1570
1571 // A virtual display is set up but is removed from the current state.
1572 mFlinger.mutableHwcDisplayData().resize(3);
1573 Case::Display::injectHwcDisplay(this);
1574 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1575 existing.inject();
1576 mFlinger.mutableCurrentState().displays.removeItem(existing.token());
1577
1578 // --------------------------------------------------------------------
1579 // Call Expectations
1580
1581 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1582
1583 // --------------------------------------------------------------------
1584 // Invocation
1585
1586 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1587
1588 // --------------------------------------------------------------------
1589 // Postconditions
1590
1591 // The existing token should have been removed
1592 verifyDisplayIsNotConnected(existing.token());
1593}
1594
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001595TEST_F(HandleTransactionLockedTest, processesDisplayLayerStackChanges) {
1596 using Case = NonHwcVirtualDisplayCase;
1597
1598 constexpr uint32_t oldLayerStack = 0u;
1599 constexpr uint32_t newLayerStack = 123u;
1600
1601 // --------------------------------------------------------------------
1602 // Preconditions
1603
1604 // A display is set up
1605 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1606 display.inject();
1607
1608 // There is a change to the layerStack state
1609 display.mutableDrawingDisplayState().layerStack = oldLayerStack;
1610 display.mutableCurrentDisplayState().layerStack = newLayerStack;
1611
1612 // --------------------------------------------------------------------
1613 // Invocation
1614
1615 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1616
1617 // --------------------------------------------------------------------
1618 // Postconditions
1619
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001620 EXPECT_EQ(newLayerStack, display.mutableDisplayDevice()->getLayerStack());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001621}
1622
1623TEST_F(HandleTransactionLockedTest, processesDisplayTransformChanges) {
1624 using Case = NonHwcVirtualDisplayCase;
1625
1626 constexpr int oldTransform = 0;
1627 constexpr int newTransform = 2;
1628
1629 // --------------------------------------------------------------------
1630 // Preconditions
1631
1632 // A display is set up
1633 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1634 display.inject();
1635
1636 // There is a change to the orientation state
1637 display.mutableDrawingDisplayState().orientation = oldTransform;
1638 display.mutableCurrentDisplayState().orientation = newTransform;
1639
1640 // --------------------------------------------------------------------
1641 // Invocation
1642
1643 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1644
1645 // --------------------------------------------------------------------
1646 // Postconditions
1647
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001648 EXPECT_EQ(newTransform, display.mutableDisplayDevice()->getOrientation());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001649}
1650
1651TEST_F(HandleTransactionLockedTest, processesDisplayViewportChanges) {
1652 using Case = NonHwcVirtualDisplayCase;
1653
1654 const Rect oldViewport(0, 0, 0, 0);
1655 const Rect newViewport(0, 0, 123, 456);
1656
1657 // --------------------------------------------------------------------
1658 // Preconditions
1659
1660 // A display is set up
1661 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1662 display.inject();
1663
1664 // There is a change to the viewport state
1665 display.mutableDrawingDisplayState().viewport = oldViewport;
1666 display.mutableCurrentDisplayState().viewport = newViewport;
1667
1668 // --------------------------------------------------------------------
1669 // Invocation
1670
1671 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1672
1673 // --------------------------------------------------------------------
1674 // Postconditions
1675
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001676 EXPECT_EQ(newViewport, display.mutableDisplayDevice()->getViewport());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001677}
1678
1679TEST_F(HandleTransactionLockedTest, processesDisplayFrameChanges) {
1680 using Case = NonHwcVirtualDisplayCase;
1681
1682 const Rect oldFrame(0, 0, 0, 0);
1683 const Rect newFrame(0, 0, 123, 456);
1684
1685 // --------------------------------------------------------------------
1686 // Preconditions
1687
1688 // A display is set up
1689 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1690 display.inject();
1691
1692 // There is a change to the viewport state
1693 display.mutableDrawingDisplayState().frame = oldFrame;
1694 display.mutableCurrentDisplayState().frame = newFrame;
1695
1696 // --------------------------------------------------------------------
1697 // Invocation
1698
1699 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1700
1701 // --------------------------------------------------------------------
1702 // Postconditions
1703
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001704 EXPECT_EQ(newFrame, display.mutableDisplayDevice()->getFrame());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001705}
1706
1707TEST_F(HandleTransactionLockedTest, processesDisplayWidthChanges) {
1708 using Case = NonHwcVirtualDisplayCase;
1709
1710 constexpr int oldWidth = 0;
1711 constexpr int oldHeight = 10;
1712 constexpr int newWidth = 123;
1713
1714 // --------------------------------------------------------------------
1715 // Preconditions
1716
1717 // A display is set up
1718 auto nativeWindow = new mock::NativeWindow();
1719 auto displaySurface = new mock::DisplaySurface();
1720 auto renderSurface = new RE::mock::Surface();
1721 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1722 display.setNativeWindow(nativeWindow);
1723 display.setDisplaySurface(displaySurface);
1724 display.setRenderSurface(std::unique_ptr<RE::Surface>(renderSurface));
1725 display.inject();
1726
1727 // There is a change to the viewport state
1728 display.mutableDrawingDisplayState().width = oldWidth;
1729 display.mutableDrawingDisplayState().height = oldHeight;
1730 display.mutableCurrentDisplayState().width = newWidth;
1731 display.mutableCurrentDisplayState().height = oldHeight;
1732
1733 // --------------------------------------------------------------------
1734 // Call Expectations
1735
1736 EXPECT_CALL(*renderSurface, setNativeWindow(nullptr)).Times(1);
1737 EXPECT_CALL(*displaySurface, resizeBuffers(newWidth, oldHeight)).Times(1);
1738 EXPECT_CALL(*renderSurface, setNativeWindow(nativeWindow)).Times(1);
1739 EXPECT_CALL(*renderSurface, queryWidth()).WillOnce(Return(newWidth));
1740 EXPECT_CALL(*renderSurface, queryHeight()).WillOnce(Return(oldHeight));
1741
1742 // --------------------------------------------------------------------
1743 // Invocation
1744
1745 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1746}
1747
1748TEST_F(HandleTransactionLockedTest, processesDisplayHeightChanges) {
1749 using Case = NonHwcVirtualDisplayCase;
1750
1751 constexpr int oldWidth = 0;
1752 constexpr int oldHeight = 10;
1753 constexpr int newHeight = 123;
1754
1755 // --------------------------------------------------------------------
1756 // Preconditions
1757
1758 // A display is set up
1759 auto nativeWindow = new mock::NativeWindow();
1760 auto displaySurface = new mock::DisplaySurface();
1761 auto renderSurface = new RE::mock::Surface();
1762 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1763 display.setNativeWindow(nativeWindow);
1764 display.setDisplaySurface(displaySurface);
1765 display.setRenderSurface(std::unique_ptr<RE::Surface>(renderSurface));
1766 display.inject();
1767
1768 // There is a change to the viewport state
1769 display.mutableDrawingDisplayState().width = oldWidth;
1770 display.mutableDrawingDisplayState().height = oldHeight;
1771 display.mutableCurrentDisplayState().width = oldWidth;
1772 display.mutableCurrentDisplayState().height = newHeight;
1773
1774 // --------------------------------------------------------------------
1775 // Call Expectations
1776
1777 EXPECT_CALL(*renderSurface, setNativeWindow(nullptr)).Times(1);
1778 EXPECT_CALL(*displaySurface, resizeBuffers(oldWidth, newHeight)).Times(1);
1779 EXPECT_CALL(*renderSurface, setNativeWindow(nativeWindow)).Times(1);
1780 EXPECT_CALL(*renderSurface, queryWidth()).WillOnce(Return(oldWidth));
1781 EXPECT_CALL(*renderSurface, queryHeight()).WillOnce(Return(newHeight));
1782
1783 // --------------------------------------------------------------------
1784 // Invocation
1785
1786 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1787}
1788
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001789/* ------------------------------------------------------------------------
1790 * SurfaceFlinger::setDisplayStateLocked
1791 */
1792
1793TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithUnknownDisplay) {
1794 // --------------------------------------------------------------------
1795 // Preconditions
1796
1797 // We have an unknown display token not associated with a known display
1798 sp<BBinder> displayToken = new BBinder();
1799
1800 // The requested display state references the unknown display.
1801 DisplayState state;
1802 state.what = DisplayState::eLayerStackChanged;
1803 state.token = displayToken;
1804 state.layerStack = 456;
1805
1806 // --------------------------------------------------------------------
1807 // Invocation
1808
1809 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1810
1811 // --------------------------------------------------------------------
1812 // Postconditions
1813
1814 // The returned flags are empty
1815 EXPECT_EQ(0u, flags);
1816
1817 // The display token still doesn't match anything known.
1818 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1819}
1820
1821TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithInvalidDisplay) {
1822 using Case = InvalidDisplayCase;
1823
1824 // --------------------------------------------------------------------
1825 // Preconditions
1826
1827 // An invalid display is set up
1828 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1829 display.inject();
1830
1831 // The invalid display has some state
1832 display.mutableCurrentDisplayState().layerStack = 654u;
1833
1834 // The requested display state tries to change the display state.
1835 DisplayState state;
1836 state.what = DisplayState::eLayerStackChanged;
1837 state.token = display.token();
1838 state.layerStack = 456;
1839
1840 // --------------------------------------------------------------------
1841 // Invocation
1842
1843 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1844
1845 // --------------------------------------------------------------------
1846 // Postconditions
1847
1848 // The returned flags are empty
1849 EXPECT_EQ(0u, flags);
1850
1851 // The current display layer stack value is unchanged.
1852 EXPECT_EQ(654u, getCurrentDisplayState(display.token()).layerStack);
1853}
1854
1855TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWhenNoChanges) {
1856 using Case = SimplePrimaryDisplayCase;
1857
1858 // --------------------------------------------------------------------
1859 // Preconditions
1860
1861 // A display is already set up
1862 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1863 display.inject();
1864
1865 // No changes are made to the display
1866 DisplayState state;
1867 state.what = 0;
1868 state.token = display.token();
1869
1870 // --------------------------------------------------------------------
1871 // Invocation
1872
1873 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1874
1875 // --------------------------------------------------------------------
1876 // Postconditions
1877
1878 // The returned flags are empty
1879 EXPECT_EQ(0u, flags);
1880}
1881
1882TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSurfaceDidNotChange) {
1883 using Case = SimplePrimaryDisplayCase;
1884
1885 // --------------------------------------------------------------------
1886 // Preconditions
1887
1888 // A display is already set up
1889 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1890 display.inject();
1891
1892 // There is a surface that can be set.
1893 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
1894
1895 // The current display state has the surface set
1896 display.mutableCurrentDisplayState().surface = surface;
1897
1898 // The incoming request sets the same surface
1899 DisplayState state;
1900 state.what = DisplayState::eSurfaceChanged;
1901 state.token = display.token();
1902 state.surface = surface;
1903
1904 // --------------------------------------------------------------------
1905 // Invocation
1906
1907 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1908
1909 // --------------------------------------------------------------------
1910 // Postconditions
1911
1912 // The returned flags are empty
1913 EXPECT_EQ(0u, flags);
1914
1915 // The current display state is unchanged.
1916 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
1917}
1918
1919TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfSurfaceChanged) {
1920 using Case = SimplePrimaryDisplayCase;
1921
1922 // --------------------------------------------------------------------
1923 // Preconditions
1924
1925 // A display is already set up
1926 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1927 display.inject();
1928
1929 // There is a surface that can be set.
1930 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
1931
1932 // The current display state does not have a surface
1933 display.mutableCurrentDisplayState().surface = nullptr;
1934
1935 // The incoming request sets a surface
1936 DisplayState state;
1937 state.what = DisplayState::eSurfaceChanged;
1938 state.token = display.token();
1939 state.surface = surface;
1940
1941 // --------------------------------------------------------------------
1942 // Invocation
1943
1944 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1945
1946 // --------------------------------------------------------------------
1947 // Postconditions
1948
1949 // The returned flags indicate a transaction is needed
1950 EXPECT_EQ(eDisplayTransactionNeeded, flags);
1951
1952 // The current display layer stack state is set to the new value
1953 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
1954}
1955
1956TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfLayerStackDidNotChange) {
1957 using Case = SimplePrimaryDisplayCase;
1958
1959 // --------------------------------------------------------------------
1960 // Preconditions
1961
1962 // A display is already set up
1963 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1964 display.inject();
1965
1966 // The display has a layer stack set
1967 display.mutableCurrentDisplayState().layerStack = 456u;
1968
1969 // The incoming request sets the same layer stack
1970 DisplayState state;
1971 state.what = DisplayState::eLayerStackChanged;
1972 state.token = display.token();
1973 state.layerStack = 456u;
1974
1975 // --------------------------------------------------------------------
1976 // Invocation
1977
1978 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1979
1980 // --------------------------------------------------------------------
1981 // Postconditions
1982
1983 // The returned flags are empty
1984 EXPECT_EQ(0u, flags);
1985
1986 // The current display state is unchanged
1987 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
1988}
1989
1990TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfLayerStackChanged) {
1991 using Case = SimplePrimaryDisplayCase;
1992
1993 // --------------------------------------------------------------------
1994 // Preconditions
1995
1996 // A display is set up
1997 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1998 display.inject();
1999
2000 // The display has a layer stack set
2001 display.mutableCurrentDisplayState().layerStack = 654u;
2002
2003 // The incoming request sets a different layer stack
2004 DisplayState state;
2005 state.what = DisplayState::eLayerStackChanged;
2006 state.token = display.token();
2007 state.layerStack = 456u;
2008
2009 // --------------------------------------------------------------------
2010 // Invocation
2011
2012 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2013
2014 // --------------------------------------------------------------------
2015 // Postconditions
2016
2017 // The returned flags indicate a transaction is needed
2018 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2019
2020 // The desired display state has been set to the new value.
2021 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2022}
2023
2024TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfProjectionDidNotChange) {
2025 using Case = SimplePrimaryDisplayCase;
2026 constexpr int initialOrientation = 180;
2027 const Rect initialFrame = {1, 2, 3, 4};
2028 const Rect initialViewport = {5, 6, 7, 8};
2029
2030 // --------------------------------------------------------------------
2031 // Preconditions
2032
2033 // A display is set up
2034 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2035 display.inject();
2036
2037 // The current display state projection state is all set
2038 display.mutableCurrentDisplayState().orientation = initialOrientation;
2039 display.mutableCurrentDisplayState().frame = initialFrame;
2040 display.mutableCurrentDisplayState().viewport = initialViewport;
2041
2042 // The incoming request sets the same projection state
2043 DisplayState state;
2044 state.what = DisplayState::eDisplayProjectionChanged;
2045 state.token = display.token();
2046 state.orientation = initialOrientation;
2047 state.frame = initialFrame;
2048 state.viewport = initialViewport;
2049
2050 // --------------------------------------------------------------------
2051 // Invocation
2052
2053 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2054
2055 // --------------------------------------------------------------------
2056 // Postconditions
2057
2058 // The returned flags are empty
2059 EXPECT_EQ(0u, flags);
2060
2061 // The current display state is unchanged
2062 EXPECT_EQ(initialOrientation, display.getCurrentDisplayState().orientation);
2063
2064 EXPECT_EQ(initialFrame, display.getCurrentDisplayState().frame);
2065 EXPECT_EQ(initialViewport, display.getCurrentDisplayState().viewport);
2066}
2067
2068TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfOrientationChanged) {
2069 using Case = SimplePrimaryDisplayCase;
2070 constexpr int initialOrientation = 90;
2071 constexpr int desiredOrientation = 180;
2072
2073 // --------------------------------------------------------------------
2074 // Preconditions
2075
2076 // A display is set up
2077 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2078 display.inject();
2079
2080 // The current display state has an orientation set
2081 display.mutableCurrentDisplayState().orientation = initialOrientation;
2082
2083 // The incoming request sets a different orientation
2084 DisplayState state;
2085 state.what = DisplayState::eDisplayProjectionChanged;
2086 state.token = display.token();
2087 state.orientation = desiredOrientation;
2088
2089 // --------------------------------------------------------------------
2090 // Invocation
2091
2092 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2093
2094 // --------------------------------------------------------------------
2095 // Postconditions
2096
2097 // The returned flags indicate a transaction is needed
2098 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2099
2100 // The current display state has the new value.
2101 EXPECT_EQ(desiredOrientation, display.getCurrentDisplayState().orientation);
2102}
2103
2104TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfFrameChanged) {
2105 using Case = SimplePrimaryDisplayCase;
2106 const Rect initialFrame = {0, 0, 0, 0};
2107 const Rect desiredFrame = {5, 6, 7, 8};
2108
2109 // --------------------------------------------------------------------
2110 // Preconditions
2111
2112 // A display is set up
2113 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2114 display.inject();
2115
2116 // The current display state does not have a frame
2117 display.mutableCurrentDisplayState().frame = initialFrame;
2118
2119 // The incoming request sets a frame
2120 DisplayState state;
2121 state.what = DisplayState::eDisplayProjectionChanged;
2122 state.token = display.token();
2123 state.frame = desiredFrame;
2124
2125 // --------------------------------------------------------------------
2126 // Invocation
2127
2128 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2129
2130 // --------------------------------------------------------------------
2131 // Postconditions
2132
2133 // The returned flags indicate a transaction is needed
2134 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2135
2136 // The current display state has the new value.
2137 EXPECT_EQ(desiredFrame, display.getCurrentDisplayState().frame);
2138}
2139
2140TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfViewportChanged) {
2141 using Case = SimplePrimaryDisplayCase;
2142 const Rect initialViewport = {0, 0, 0, 0};
2143 const Rect desiredViewport = {5, 6, 7, 8};
2144
2145 // --------------------------------------------------------------------
2146 // Preconditions
2147
2148 // A display is set up
2149 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2150 display.inject();
2151
2152 // The current display state does not have a viewport
2153 display.mutableCurrentDisplayState().viewport = initialViewport;
2154
2155 // The incoming request sets a viewport
2156 DisplayState state;
2157 state.what = DisplayState::eDisplayProjectionChanged;
2158 state.token = display.token();
2159 state.viewport = desiredViewport;
2160
2161 // --------------------------------------------------------------------
2162 // Invocation
2163
2164 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2165
2166 // --------------------------------------------------------------------
2167 // Postconditions
2168
2169 // The returned flags indicate a transaction is needed
2170 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2171
2172 // The current display state has the new value.
2173 EXPECT_EQ(desiredViewport, display.getCurrentDisplayState().viewport);
2174}
2175
2176TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSizeDidNotChange) {
2177 using Case = SimplePrimaryDisplayCase;
2178 constexpr uint32_t initialWidth = 1024;
2179 constexpr uint32_t initialHeight = 768;
2180
2181 // --------------------------------------------------------------------
2182 // Preconditions
2183
2184 // A display is set up
2185 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2186 display.inject();
2187
2188 // The current display state has a size set
2189 display.mutableCurrentDisplayState().width = initialWidth;
2190 display.mutableCurrentDisplayState().height = initialHeight;
2191
2192 // The incoming request sets the same display size
2193 DisplayState state;
2194 state.what = DisplayState::eDisplaySizeChanged;
2195 state.token = display.token();
2196 state.width = initialWidth;
2197 state.height = initialHeight;
2198
2199 // --------------------------------------------------------------------
2200 // Invocation
2201
2202 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2203
2204 // --------------------------------------------------------------------
2205 // Postconditions
2206
2207 // The returned flags are empty
2208 EXPECT_EQ(0u, flags);
2209
2210 // The current display state is unchanged
2211 EXPECT_EQ(initialWidth, display.getCurrentDisplayState().width);
2212 EXPECT_EQ(initialHeight, display.getCurrentDisplayState().height);
2213}
2214
2215TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfWidthChanged) {
2216 using Case = SimplePrimaryDisplayCase;
2217 constexpr uint32_t initialWidth = 0;
2218 constexpr uint32_t desiredWidth = 1024;
2219
2220 // --------------------------------------------------------------------
2221 // Preconditions
2222
2223 // A display is set up
2224 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2225 display.inject();
2226
2227 // The display does not yet have a width
2228 display.mutableCurrentDisplayState().width = initialWidth;
2229
2230 // The incoming request sets a display width
2231 DisplayState state;
2232 state.what = DisplayState::eDisplaySizeChanged;
2233 state.token = display.token();
2234 state.width = desiredWidth;
2235
2236 // --------------------------------------------------------------------
2237 // Invocation
2238
2239 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2240
2241 // --------------------------------------------------------------------
2242 // Postconditions
2243
2244 // The returned flags indicate a transaction is needed
2245 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2246
2247 // The current display state has the new value.
2248 EXPECT_EQ(desiredWidth, display.getCurrentDisplayState().width);
2249}
2250
2251TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfHeightChanged) {
2252 using Case = SimplePrimaryDisplayCase;
2253 constexpr uint32_t initialHeight = 0;
2254 constexpr uint32_t desiredHeight = 768;
2255
2256 // --------------------------------------------------------------------
2257 // Preconditions
2258
2259 // A display is set up
2260 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2261 display.inject();
2262
2263 // The display does not yet have a height
2264 display.mutableCurrentDisplayState().height = initialHeight;
2265
2266 // The incoming request sets a display height
2267 DisplayState state;
2268 state.what = DisplayState::eDisplaySizeChanged;
2269 state.token = display.token();
2270 state.height = desiredHeight;
2271
2272 // --------------------------------------------------------------------
2273 // Invocation
2274
2275 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2276
2277 // --------------------------------------------------------------------
2278 // Postconditions
2279
2280 // The returned flags indicate a transaction is needed
2281 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2282
2283 // The current display state has the new value.
2284 EXPECT_EQ(desiredHeight, display.getCurrentDisplayState().height);
2285}
2286
Lloyd Pique86016da2018-03-01 16:09:38 -08002287/* ------------------------------------------------------------------------
2288 * SurfaceFlinger::onInitializeDisplays
2289 */
2290
2291TEST_F(DisplayTransactionTest, onInitializeDisplaysSetsUpPrimaryDisplay) {
2292 using Case = SimplePrimaryDisplayCase;
2293
2294 // --------------------------------------------------------------------
2295 // Preconditions
2296
2297 // A primary display is set up
2298 Case::Display::injectHwcDisplay(this);
2299 auto primaryDisplay = Case::Display::makeFakeExistingDisplayInjector(this);
2300 primaryDisplay.inject();
2301
2302 // --------------------------------------------------------------------
2303 // Call Expectations
2304
2305 // We expect the surface interceptor to possibly be used, but we treat it as
2306 // disabled since it is called as a side effect rather than directly by this
2307 // function.
2308 EXPECT_CALL(*mSurfaceInterceptor, isEnabled()).WillOnce(Return(false));
2309
2310 // We expect a call to get the active display config.
2311 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
2312
2313 // We expect invalidate() to be invoked once to trigger display transaction
2314 // processing.
2315 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
2316
2317 // --------------------------------------------------------------------
2318 // Invocation
2319
2320 mFlinger.onInitializeDisplays();
2321
2322 // --------------------------------------------------------------------
2323 // Postconditions
2324
2325 // The primary display should have a current state
2326 ASSERT_TRUE(hasCurrentDisplayState(primaryDisplay.token()));
2327 const auto& primaryDisplayState = getCurrentDisplayState(primaryDisplay.token());
2328 // The layer stack state should be set to zero
2329 EXPECT_EQ(0u, primaryDisplayState.layerStack);
2330 // The orientation state should be set to zero
2331 EXPECT_EQ(0, primaryDisplayState.orientation);
2332
2333 // The frame state should be set to INVALID
2334 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.frame);
2335
2336 // The viewport state should be set to INVALID
2337 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.viewport);
2338
2339 // The width and height should both be zero
2340 EXPECT_EQ(0u, primaryDisplayState.width);
2341 EXPECT_EQ(0u, primaryDisplayState.height);
2342
2343 // The display should be set to HWC_POWER_MODE_NORMAL
2344 ASSERT_TRUE(hasDisplayDevice(primaryDisplay.token()));
2345 auto displayDevice = primaryDisplay.mutableDisplayDevice();
2346 EXPECT_EQ(HWC_POWER_MODE_NORMAL, displayDevice->getPowerMode());
2347
2348 // The display refresh period should be set in the frame tracker.
2349 FrameStats stats;
2350 mFlinger.getAnimFrameTracker().getStats(&stats);
2351 EXPECT_EQ(DEFAULT_REFRESH_RATE, stats.refreshPeriodNano);
2352
2353 // The display transaction needed flag should be set.
2354 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
2355
2356 // The compositor timing should be set to default values
2357 const auto& compositorTiming = mFlinger.getCompositorTiming();
2358 EXPECT_EQ(-DEFAULT_REFRESH_RATE, compositorTiming.deadline);
2359 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.interval);
2360 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.presentLatency);
2361}
2362
Lloyd Piquef58625d2017-12-19 13:22:33 -08002363} // namespace
2364} // namespace android