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