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