blob: ec9866168f384f468df7548539590c2c76e5eb8a [file] [log] [blame]
Lloyd Piquef58625d2017-12-19 13:22:33 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#undef LOG_TAG
18#define LOG_TAG "LibSurfaceFlingerUnittests"
19
20#include <gmock/gmock.h>
21#include <gtest/gtest.h>
22
23#include <log/log.h>
24
25#include "TestableSurfaceFlinger.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080026#include "mock/DisplayHardware/MockComposer.h"
27#include "mock/DisplayHardware/MockDisplaySurface.h"
28#include "mock/MockEventControlThread.h"
29#include "mock/MockEventThread.h"
30#include "mock/MockMessageQueue.h"
31#include "mock/MockNativeWindowSurface.h"
32#include "mock/MockSurfaceInterceptor.h"
33#include "mock/RenderEngine/MockRenderEngine.h"
34#include "mock/gui/MockGraphicBufferConsumer.h"
35#include "mock/gui/MockGraphicBufferProducer.h"
36#include "mock/system/window/MockNativeWindow.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080037
38namespace android {
39namespace {
40
Lloyd Piquee39cad22017-12-20 17:01:29 -080041using testing::_;
42using testing::ByMove;
43using testing::DoAll;
44using testing::Mock;
45using testing::Return;
46using testing::SetArgPointee;
47
Lloyd Piqued883d5a2018-04-27 19:32:30 -070048using android::Hwc2::ColorMode;
Lloyd Piquee39cad22017-12-20 17:01:29 -080049using android::Hwc2::Error;
Lloyd Piqued883d5a2018-04-27 19:32:30 -070050using android::Hwc2::Hdr;
Lloyd Piquee39cad22017-12-20 17:01:29 -080051using android::Hwc2::IComposer;
52using android::Hwc2::IComposerClient;
Lloyd Piqued883d5a2018-04-27 19:32:30 -070053using android::Hwc2::PerFrameMetadataKey;
54using android::Hwc2::RenderIntent;
Lloyd Piquee39cad22017-12-20 17:01:29 -080055
Lloyd Piquec11e0d32018-01-22 18:44:59 -080056using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector;
57using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
Lloyd Pique1fa4d462018-01-22 18:03:16 -080058using HotplugEvent = TestableSurfaceFlinger::HotplugEvent;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080059using HWC2Display = TestableSurfaceFlinger::HWC2Display;
Lloyd Piquebc792092018-01-17 11:52:30 -080060
Lloyd Piquec11e0d32018-01-22 18:44:59 -080061constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666;
Lloyd Piquee39cad22017-12-20 17:01:29 -080062constexpr int32_t DEFAULT_DPI = 320;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080063constexpr int DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT = HAL_PIXEL_FORMAT_RGB_565;
Lloyd Piquee39cad22017-12-20 17:01:29 -080064
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -080065constexpr int HWC_POWER_MODE_LEET = 1337; // An out of range power mode value
66
Lloyd Piquec11e0d32018-01-22 18:44:59 -080067/* ------------------------------------------------------------------------
68 * Boolean avoidance
69 *
70 * To make calls and template instantiations more readable, we define some
71 * local enums along with an implicit bool conversion.
72 */
73
74#define BOOL_SUBSTITUTE(TYPENAME) enum class TYPENAME : bool { FALSE = false, TRUE = true };
75
76BOOL_SUBSTITUTE(Critical);
77BOOL_SUBSTITUTE(Async);
78BOOL_SUBSTITUTE(Secure);
79
80/* ------------------------------------------------------------------------
81 *
82 */
Lloyd Pique1fa4d462018-01-22 18:03:16 -080083
Lloyd Piquef58625d2017-12-19 13:22:33 -080084class DisplayTransactionTest : public testing::Test {
Lloyd Piquec11e0d32018-01-22 18:44:59 -080085public:
Lloyd Piquef58625d2017-12-19 13:22:33 -080086 DisplayTransactionTest();
87 ~DisplayTransactionTest() override;
88
Lloyd Pique1fa4d462018-01-22 18:03:16 -080089 // --------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -080090 // Mock/Fake injection
Lloyd Piquef58625d2017-12-19 13:22:33 -080091
Lloyd Piquec11e0d32018-01-22 18:44:59 -080092 void injectMockComposer(int virtualDisplayCount);
93 void injectFakeBufferQueueFactory();
94 void injectFakeNativeWindowSurfaceFactory();
Lloyd Pique1fa4d462018-01-22 18:03:16 -080095
96 // --------------------------------------------------------------------
97 // Postcondition helpers
98
Lloyd Piquec11e0d32018-01-22 18:44:59 -080099 bool hasHwcDisplay(hwc2_display_t displayId);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800100 bool hasTransactionFlagSet(int flag);
101 bool hasDisplayDevice(sp<IBinder> displayToken);
102 sp<DisplayDevice> getDisplayDevice(sp<IBinder> displayToken);
103 bool hasCurrentDisplayState(sp<IBinder> displayToken);
104 const DisplayDeviceState& getCurrentDisplayState(sp<IBinder> displayToken);
105 bool hasDrawingDisplayState(sp<IBinder> displayToken);
106 const DisplayDeviceState& getDrawingDisplayState(sp<IBinder> displayToken);
107
108 // --------------------------------------------------------------------
109 // Test instances
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800110
Lloyd Piquef58625d2017-12-19 13:22:33 -0800111 TestableSurfaceFlinger mFlinger;
Lloyd Piquee39cad22017-12-20 17:01:29 -0800112 mock::EventThread* mEventThread = new mock::EventThread();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800113 mock::EventControlThread* mEventControlThread = new mock::EventControlThread();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800114
115 // These mocks are created by the test, but are destroyed by SurfaceFlinger
116 // by virtue of being stored into a std::unique_ptr. However we still need
117 // to keep a reference to them for use in setting up call expectations.
118 RE::mock::RenderEngine* mRenderEngine = new RE::mock::RenderEngine();
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800119 Hwc2::mock::Composer* mComposer = nullptr;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800120 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
121 mock::SurfaceInterceptor* mSurfaceInterceptor = new mock::SurfaceInterceptor();
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800122
123 // These mocks are created only when expected to be created via a factory.
124 sp<mock::GraphicBufferConsumer> mConsumer;
125 sp<mock::GraphicBufferProducer> mProducer;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800126 mock::NativeWindowSurface* mNativeWindowSurface = nullptr;
127 sp<mock::NativeWindow> mNativeWindow;
128 RE::mock::Surface* mRenderSurface = nullptr;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800129};
130
131DisplayTransactionTest::DisplayTransactionTest() {
132 const ::testing::TestInfo* const test_info =
133 ::testing::UnitTest::GetInstance()->current_test_info();
134 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800135
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800136 // Default to no wide color display support configured
137 mFlinger.mutableHasWideColorDisplay() = false;
138 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
139
140 // Default to using HWC virtual displays
141 mFlinger.mutableUseHwcVirtualDisplays() = true;
142
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800143 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
144 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
145 });
146
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800147 mFlinger.setCreateNativeWindowSurface([](auto) {
148 ADD_FAILURE() << "Unexpected request to create a native window surface.";
149 return nullptr;
150 });
151
152 mFlinger.mutableEventControlThread().reset(mEventControlThread);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800153 mFlinger.mutableEventThread().reset(mEventThread);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800154 mFlinger.mutableEventQueue().reset(mMessageQueue);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800155 mFlinger.setupRenderEngine(std::unique_ptr<RE::RenderEngine>(mRenderEngine));
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800156 mFlinger.mutableInterceptor().reset(mSurfaceInterceptor);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800157
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800158 injectMockComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -0800159}
160
161DisplayTransactionTest::~DisplayTransactionTest() {
162 const ::testing::TestInfo* const test_info =
163 ::testing::UnitTest::GetInstance()->current_test_info();
164 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
165}
166
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800167void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
168 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800169 EXPECT_CALL(*mComposer, getCapabilities())
170 .WillOnce(Return(std::vector<IComposer::Capability>()));
171 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
172 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -0800173
Lloyd Piquee39cad22017-12-20 17:01:29 -0800174 Mock::VerifyAndClear(mComposer);
175}
176
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800177void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800178 // This setup is only expected once per test.
179 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
180
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800181 mConsumer = new mock::GraphicBufferConsumer();
182 mProducer = new mock::GraphicBufferProducer();
183
184 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
185 *outProducer = mProducer;
186 *outConsumer = mConsumer;
187 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800188}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800189
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800190void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800191 // This setup is only expected once per test.
192 ASSERT_TRUE(mNativeWindowSurface == nullptr);
193
194 mNativeWindowSurface = new mock::NativeWindowSurface();
195 mNativeWindow = new mock::NativeWindow();
196
197 mFlinger.setCreateNativeWindowSurface(
198 [this](auto) { return std::unique_ptr<NativeWindowSurface>(mNativeWindowSurface); });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800199}
200
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800201bool DisplayTransactionTest::hasHwcDisplay(hwc2_display_t displayId) {
202 return mFlinger.mutableHwcDisplaySlots().count(displayId) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800203}
204
205bool DisplayTransactionTest::hasTransactionFlagSet(int flag) {
206 return mFlinger.mutableTransactionFlags() & flag;
207}
208
209bool DisplayTransactionTest::hasDisplayDevice(sp<IBinder> displayToken) {
210 return mFlinger.mutableDisplays().indexOfKey(displayToken) >= 0;
211}
212
213sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) {
214 return mFlinger.mutableDisplays().valueFor(displayToken);
215}
216
217bool DisplayTransactionTest::hasCurrentDisplayState(sp<IBinder> displayToken) {
218 return mFlinger.mutableCurrentState().displays.indexOfKey(displayToken) >= 0;
219}
220
221const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(sp<IBinder> displayToken) {
222 return mFlinger.mutableCurrentState().displays.valueFor(displayToken);
223}
224
225bool DisplayTransactionTest::hasDrawingDisplayState(sp<IBinder> displayToken) {
226 return mFlinger.mutableDrawingState().displays.indexOfKey(displayToken) >= 0;
227}
228
229const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(sp<IBinder> displayToken) {
230 return mFlinger.mutableDrawingState().displays.valueFor(displayToken);
231}
232
233/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800234 *
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800235 */
236
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800237template <DisplayDevice::DisplayType type, DisplayDevice::DisplayType hwcId, int width, int height,
238 Critical critical, Async async, Secure secure, int grallocUsage>
239struct DisplayVariant {
240 // The display width and height
241 static constexpr int WIDTH = width;
242 static constexpr int HEIGHT = height;
243
244 static constexpr int GRALLOC_USAGE = grallocUsage;
245
246 // The type for this display
247 static constexpr DisplayDevice::DisplayType TYPE = type;
248 static constexpr DisplayDevice::DisplayType HWCOMPOSER_ID = hwcId;
249
250 // When creating native window surfaces for the framebuffer, whether those should be critical
251 static constexpr Critical CRITICAL = critical;
252
253 // When creating native window surfaces for the framebuffer, whether those should be async
254 static constexpr Async ASYNC = async;
255
256 // Whether the display should be treated as secure
257 static constexpr Secure SECURE = secure;
258
259 static auto makeFakeExistingDisplayInjector(DisplayTransactionTest* test) {
260 auto injector = FakeDisplayDeviceInjector(test->mFlinger, TYPE, HWCOMPOSER_ID);
261 injector.setSecure(static_cast<bool>(SECURE));
262 return injector;
263 }
264
265 // Called by tests to set up any native window creation call expectations.
266 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
267 EXPECT_CALL(*test->mNativeWindowSurface, getNativeWindow())
268 .WillOnce(Return(test->mNativeWindow));
269 EXPECT_CALL(*test->mNativeWindow, perform(19)).WillRepeatedly(Return(NO_ERROR));
270
271 // For simplicity, we only expect to create a single render surface for
272 // each test.
273 ASSERT_TRUE(test->mRenderSurface == nullptr);
274 test->mRenderSurface = new RE::mock::Surface();
275 EXPECT_CALL(*test->mRenderEngine, createSurface())
276 .WillOnce(Return(ByMove(std::unique_ptr<RE::Surface>(test->mRenderSurface))));
277 EXPECT_CALL(*test->mRenderSurface, setAsync(static_cast<bool>(ASYNC))).Times(1);
278 EXPECT_CALL(*test->mRenderSurface, setCritical(static_cast<bool>(CRITICAL))).Times(1);
279 EXPECT_CALL(*test->mRenderSurface, setNativeWindow(test->mNativeWindow.get())).Times(1);
280 EXPECT_CALL(*test->mRenderSurface, queryWidth()).WillOnce(Return(WIDTH));
281 EXPECT_CALL(*test->mRenderSurface, queryHeight()).WillOnce(Return(HEIGHT));
282 }
283
284 static void setupFramebufferConsumerBufferQueueCallExpectations(DisplayTransactionTest* test) {
285 EXPECT_CALL(*test->mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR));
286 EXPECT_CALL(*test->mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR));
287 EXPECT_CALL(*test->mConsumer, setConsumerUsageBits(GRALLOC_USAGE))
288 .WillRepeatedly(Return(NO_ERROR));
289 EXPECT_CALL(*test->mConsumer, setDefaultBufferSize(WIDTH, HEIGHT))
290 .WillRepeatedly(Return(NO_ERROR));
291 EXPECT_CALL(*test->mConsumer, setMaxAcquiredBufferCount(_))
292 .WillRepeatedly(Return(NO_ERROR));
293 }
294
295 static void setupFramebufferProducerBufferQueueCallExpectations(DisplayTransactionTest* test) {
296 EXPECT_CALL(*test->mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return());
297 }
298};
299
300template <hwc2_display_t hwcDisplayId, HWC2::DisplayType hwcDisplayType, typename DisplayVariant>
301struct HwcDisplayVariant {
302 // The display id supplied by the HWC
303 static constexpr hwc2_display_t HWC_DISPLAY_ID = hwcDisplayId;
304
305 // The HWC display type
306 static constexpr HWC2::DisplayType HWC_DISPLAY_TYPE = hwcDisplayType;
307
308 // The HWC active configuration id
Lloyd Pique3c085a02018-05-09 19:38:32 -0700309 static constexpr int HWC_ACTIVE_CONFIG_ID = 2001;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800310
311 static void injectPendingHotplugEvent(DisplayTransactionTest* test,
312 HWC2::Connection connection) {
313 test->mFlinger.mutablePendingHotplugEvents().emplace_back(
314 HotplugEvent{HWC_DISPLAY_ID, connection});
315 }
316
317 // Called by tests to inject a HWC display setup
318 static void injectHwcDisplay(DisplayTransactionTest* test) {
319 FakeHwcDisplayInjector(DisplayVariant::TYPE, HWC_DISPLAY_TYPE)
320 .setHwcDisplayId(HWC_DISPLAY_ID)
321 .setWidth(DisplayVariant::WIDTH)
322 .setHeight(DisplayVariant::HEIGHT)
323 .setActiveConfig(HWC_ACTIVE_CONFIG_ID)
324 .inject(&test->mFlinger, test->mComposer);
325 }
326
327 static void setupHwcHotplugCallExpectations(DisplayTransactionTest* test) {
328 EXPECT_CALL(*test->mComposer, getDisplayType(HWC_DISPLAY_ID, _))
329 .WillOnce(DoAll(SetArgPointee<1>(static_cast<IComposerClient::DisplayType>(
330 HWC_DISPLAY_TYPE)),
331 Return(Error::NONE)));
332 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
333 EXPECT_CALL(*test->mComposer, getDisplayConfigs(HWC_DISPLAY_ID, _))
334 .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{HWC_ACTIVE_CONFIG_ID}),
335 Return(Error::NONE)));
336 EXPECT_CALL(*test->mComposer,
337 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
338 IComposerClient::Attribute::WIDTH, _))
339 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::WIDTH), Return(Error::NONE)));
340 EXPECT_CALL(*test->mComposer,
341 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
342 IComposerClient::Attribute::HEIGHT, _))
343 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::HEIGHT), Return(Error::NONE)));
344 EXPECT_CALL(*test->mComposer,
345 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
346 IComposerClient::Attribute::VSYNC_PERIOD, _))
347 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE)));
348 EXPECT_CALL(*test->mComposer,
349 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
350 IComposerClient::Attribute::DPI_X, _))
351 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
352 EXPECT_CALL(*test->mComposer,
353 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
354 IComposerClient::Attribute::DPI_Y, _))
355 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700356 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
357 .WillRepeatedly(Return(Error::UNSUPPORTED));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800358 }
359
360 // Called by tests to set up HWC call expectations
361 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
362 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY_ID, _))
Lloyd Pique3c085a02018-05-09 19:38:32 -0700363 .WillRepeatedly(DoAll(SetArgPointee<1>(HWC_ACTIVE_CONFIG_ID), Return(Error::NONE)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800364 }
365};
366
367struct NonHwcDisplayVariant {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800368 static void injectHwcDisplay(DisplayTransactionTest*) {}
369
370 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
371 EXPECT_CALL(*test->mComposer, getActiveConfig(_, _)).Times(0);
372 }
373};
374
375// Physical displays are expected to be synchronous, secure, and have a HWC display for output.
376constexpr uint32_t GRALLOC_USAGE_PHYSICAL_DISPLAY =
377 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
378
379template <hwc2_display_t hwcDisplayId, DisplayDevice::DisplayType type, int width, int height,
380 Critical critical>
381struct PhysicalDisplayVariant
382 : public DisplayVariant<type, type, width, height, critical, Async::FALSE, Secure::TRUE,
383 GRALLOC_USAGE_PHYSICAL_DISPLAY>,
384 public HwcDisplayVariant<hwcDisplayId, HWC2::DisplayType::Physical,
385 DisplayVariant<type, type, width, height, critical, Async::FALSE,
386 Secure::TRUE, GRALLOC_USAGE_PHYSICAL_DISPLAY>> {};
387
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800388// An invalid display
389using InvalidDisplayVariant =
390 DisplayVariant<DisplayDevice::DISPLAY_ID_INVALID, DisplayDevice::DISPLAY_ID_INVALID, 0, 0,
391 Critical::FALSE, Async::FALSE, Secure::FALSE, 0>;
392
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800393// A primary display is a physical display that is critical
394using PrimaryDisplayVariant =
395 PhysicalDisplayVariant<1001, DisplayDevice::DISPLAY_PRIMARY, 3840, 2160, Critical::TRUE>;
396
397// An external display is physical display that is not critical.
398using ExternalDisplayVariant =
399 PhysicalDisplayVariant<1002, DisplayDevice::DISPLAY_EXTERNAL, 1920, 1280, Critical::FALSE>;
400
401using TertiaryDisplayVariant =
402 PhysicalDisplayVariant<1003, DisplayDevice::DISPLAY_EXTERNAL, 1600, 1200, Critical::FALSE>;
403
404// A virtual display not supported by the HWC.
405constexpr uint32_t GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY = 0;
406
407template <int width, int height, Secure secure>
408struct NonHwcVirtualDisplayVariant
409 : public DisplayVariant<DisplayDevice::DISPLAY_VIRTUAL, DisplayDevice::DISPLAY_ID_INVALID,
410 width, height, Critical::FALSE, Async::TRUE, secure,
411 GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>,
412 public NonHwcDisplayVariant {
413 using Base = DisplayVariant<DisplayDevice::DISPLAY_VIRTUAL, DisplayDevice::DISPLAY_ID_INVALID,
414 width, height, Critical::FALSE, Async::TRUE, secure,
415 GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>;
416
417 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
418 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
419 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
420 }
421};
422
423// A virtual display supported by the HWC.
424constexpr uint32_t GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY = GRALLOC_USAGE_HW_COMPOSER;
425
426template <int width, int height, Secure secure>
427struct HwcVirtualDisplayVariant
428 : public DisplayVariant<DisplayDevice::DISPLAY_VIRTUAL, DisplayDevice::DISPLAY_VIRTUAL, width,
429 height, Critical::FALSE, Async::TRUE, secure,
430 GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>,
431 public HwcDisplayVariant<1010, HWC2::DisplayType::Virtual,
432 NonHwcVirtualDisplayVariant<width, height, secure>> {
433 using Base =
434 DisplayVariant<DisplayDevice::DISPLAY_VIRTUAL, DisplayDevice::DISPLAY_VIRTUAL, width,
435 height, Critical::FALSE, Async::TRUE, secure, GRALLOC_USAGE_HW_COMPOSER>;
436 using Self = HwcVirtualDisplayVariant<width, height, secure>;
437
438 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
439 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
440 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
441 }
442
443 static void setupHwcVirtualDisplayCreationCallExpectations(DisplayTransactionTest* test) {
444 EXPECT_CALL(*test->mComposer, createVirtualDisplay(Base::WIDTH, Base::HEIGHT, _, _))
445 .WillOnce(DoAll(SetArgPointee<3>(Self::HWC_DISPLAY_ID), Return(Error::NONE)));
446 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
447 }
448};
449
450// For this variant, SurfaceFlinger should not configure itself with wide
451// display support, so the display should not be configured for wide-color
452// support.
453struct WideColorSupportNotConfiguredVariant {
454 static constexpr bool WIDE_COLOR_SUPPORTED = false;
455
456 static void injectConfigChange(DisplayTransactionTest* test) {
457 test->mFlinger.mutableHasWideColorDisplay() = false;
458 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
459 }
460
461 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
462 EXPECT_CALL(*test->mComposer, getColorModes(_, _)).Times(0);
463 EXPECT_CALL(*test->mComposer, getRenderIntents(_, _, _)).Times(0);
464 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
465 }
466};
467
468// For this variant, SurfaceFlinger should configure itself with wide display
469// support, and the display should respond with an non-empty list of supported
470// color modes. Wide-color support should be configured.
471template <typename Display>
472struct WideColorP3ColorimetricSupportedVariant {
473 static constexpr bool WIDE_COLOR_SUPPORTED = true;
474
475 static void injectConfigChange(DisplayTransactionTest* test) {
476 test->mFlinger.mutableHasWideColorDisplay() = true;
477 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
478 }
479
480 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
481 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
482 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>({ColorMode::DISPLAY_P3})),
483 Return(Error::NONE)));
484 EXPECT_CALL(*test->mComposer,
485 getRenderIntents(Display::HWC_DISPLAY_ID, ColorMode::DISPLAY_P3, _))
486 .WillOnce(DoAll(SetArgPointee<2>(
487 std::vector<RenderIntent>({RenderIntent::COLORIMETRIC})),
488 Return(Error::NONE)));
489 EXPECT_CALL(*test->mComposer,
490 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::SRGB,
491 RenderIntent::COLORIMETRIC))
492 .WillOnce(Return(Error::NONE));
493 }
494};
495
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800496// For this variant, SurfaceFlinger should configure itself with wide display
497// support, but the display should respond with an empty list of supported color
498// modes. Wide-color support for the display should not be configured.
499template <typename Display>
500struct WideColorNotSupportedVariant {
501 static constexpr bool WIDE_COLOR_SUPPORTED = false;
502
503 static void injectConfigChange(DisplayTransactionTest* test) {
504 test->mFlinger.mutableHasWideColorDisplay() = true;
505 }
506
507 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
508 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
509 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>()), Return(Error::NONE)));
510 EXPECT_CALL(*test->mComposer,
511 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::NATIVE,
512 RenderIntent::COLORIMETRIC))
513 .WillOnce(Return(Error::NONE));
514 }
515};
516
517// For this variant, the display is not a HWC display, so no HDR support should
518// be configured.
519struct NonHwcDisplayHdrSupportVariant {
520 static constexpr bool HDR10_SUPPORTED = false;
521 static constexpr bool HDR_HLG_SUPPORTED = false;
522 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
523 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
524 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
525 }
526};
527
528// For this variant, the composer should respond with a non-empty list of HDR
529// modes containing HDR10, so HDR10 support should be configured.
530template <typename Display>
531struct Hdr10SupportedVariant {
532 static constexpr bool HDR10_SUPPORTED = true;
533 static constexpr bool HDR_HLG_SUPPORTED = false;
534 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
535 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
536 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
537 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HDR10})),
538 Return(Error::NONE)));
539 }
540};
541
542// For this variant, the composer should respond with a non-empty list of HDR
543// modes containing HLG, so HLG support should be configured.
544template <typename Display>
545struct HdrHlgSupportedVariant {
546 static constexpr bool HDR10_SUPPORTED = false;
547 static constexpr bool HDR_HLG_SUPPORTED = true;
548 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
549 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
550 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
551 .WillOnce(
552 DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HLG})), Return(Error::NONE)));
553 }
554};
555
556// For this variant, the composer should respond with a non-empty list of HDR
557// modes containing DOLBY_VISION, so DOLBY_VISION support should be configured.
558template <typename Display>
559struct HdrDolbyVisionSupportedVariant {
560 static constexpr bool HDR10_SUPPORTED = false;
561 static constexpr bool HDR_HLG_SUPPORTED = false;
562 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = true;
563 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
564 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
565 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::DOLBY_VISION})),
566 Return(Error::NONE)));
567 }
568};
569
570// For this variant, the composer should respond with am empty list of HDR
571// modes, so no HDR support should be configured.
572template <typename Display>
573struct HdrNotSupportedVariant {
574 static constexpr bool HDR10_SUPPORTED = false;
575 static constexpr bool HDR_HLG_SUPPORTED = false;
576 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
577 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
578 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
579 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
580 }
581};
582
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700583struct NonHwcPerFrameMetadataSupportVariant {
584 static constexpr int PER_FRAME_METADATA_KEYS = 0;
585 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
586 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(_, _)).Times(0);
587 }
588};
589
590template <typename Display>
591struct NoPerFrameMetadataSupportVariant {
592 static constexpr int PER_FRAME_METADATA_KEYS = 0;
593 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
594 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID, _))
595 .WillOnce(DoAll(SetArgPointee<1>(std::vector<PerFrameMetadataKey>()),
596 Return(Error::NONE)));
597 }
598};
599
600template <typename Display>
601struct Smpte2086PerFrameMetadataSupportVariant {
602 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::SMPTE2086;
603 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
604 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID, _))
605 .WillOnce(DoAll(SetArgPointee<1>(std::vector<PerFrameMetadataKey>({
606 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X,
607 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y,
608 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X,
609 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y,
610 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X,
611 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y,
612 PerFrameMetadataKey::WHITE_POINT_X,
613 PerFrameMetadataKey::WHITE_POINT_Y,
614 PerFrameMetadataKey::MAX_LUMINANCE,
615 PerFrameMetadataKey::MIN_LUMINANCE,
616 })),
617 Return(Error::NONE)));
618 }
619};
620
621template <typename Display>
622struct Cta861_3_PerFrameMetadataSupportVariant {
623 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::CTA861_3;
624 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
625 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID, _))
626 .WillOnce(DoAll(SetArgPointee<1>(std::vector<PerFrameMetadataKey>({
627 PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL,
628 PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL,
629 })),
630 Return(Error::NONE)));
631 }
632};
633
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800634/* ------------------------------------------------------------------------
635 * Typical display configurations to test
636 */
637
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700638template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy,
639 typename PerFrameMetadataSupportPolicy>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800640struct Case {
641 using Display = DisplayPolicy;
642 using WideColorSupport = WideColorSupportPolicy;
643 using HdrSupport = HdrSupportPolicy;
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700644 using PerFrameMetadataSupport = PerFrameMetadataSupportPolicy;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800645};
646
647using SimplePrimaryDisplayCase =
648 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700649 HdrNotSupportedVariant<PrimaryDisplayVariant>,
650 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800651using SimpleExternalDisplayCase =
652 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700653 HdrNotSupportedVariant<ExternalDisplayVariant>,
654 NoPerFrameMetadataSupportVariant<ExternalDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800655using SimpleTertiaryDisplayCase =
656 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700657 HdrNotSupportedVariant<TertiaryDisplayVariant>,
658 NoPerFrameMetadataSupportVariant<TertiaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800659using NonHwcVirtualDisplayCase =
660 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700661 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant,
662 NonHwcPerFrameMetadataSupportVariant>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800663using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
664using HwcVirtualDisplayCase =
665 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700666 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>,
667 NoPerFrameMetadataSupportVariant<SimpleHwcVirtualDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800668using WideColorP3ColorimetricDisplayCase =
669 Case<PrimaryDisplayVariant, WideColorP3ColorimetricSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700670 HdrNotSupportedVariant<PrimaryDisplayVariant>,
671 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800672using Hdr10DisplayCase =
673 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700674 Hdr10SupportedVariant<PrimaryDisplayVariant>,
675 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800676using HdrHlgDisplayCase =
677 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700678 HdrHlgSupportedVariant<PrimaryDisplayVariant>,
679 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800680using HdrDolbyVisionDisplayCase =
681 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700682 HdrDolbyVisionSupportedVariant<PrimaryDisplayVariant>,
683 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
684using HdrSmpte2086DisplayCase =
685 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
686 HdrNotSupportedVariant<PrimaryDisplayVariant>,
687 Smpte2086PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
688using HdrCta861_3_DisplayCase =
689 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
690 HdrNotSupportedVariant<PrimaryDisplayVariant>,
691 Cta861_3_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800692using InvalidDisplayCase = Case<InvalidDisplayVariant, WideColorSupportNotConfiguredVariant,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700693 NonHwcDisplayHdrSupportVariant,
694 NoPerFrameMetadataSupportVariant<InvalidDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800695/* ------------------------------------------------------------------------
Lloyd Pique6cf11032018-01-22 18:57:44 -0800696 *
697 * SurfaceFlinger::onHotplugReceived
698 */
699
700TEST_F(DisplayTransactionTest, hotplugEnqueuesEventsForDisplayTransaction) {
701 constexpr int currentSequenceId = 123;
702 constexpr hwc2_display_t displayId1 = 456;
703 constexpr hwc2_display_t displayId2 = 654;
704
705 // --------------------------------------------------------------------
706 // Preconditions
707
708 // Set the current sequence id for accepted events
709 mFlinger.mutableComposerSequenceId() = currentSequenceId;
710
711 // Set the main thread id so that the current thread does not appear to be
712 // the main thread.
713 mFlinger.mutableMainThreadId() = std::thread::id();
714
715 // --------------------------------------------------------------------
716 // Call Expectations
717
718 // We expect invalidate() to be invoked once to trigger display transaction
719 // processing.
720 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
721
722 // --------------------------------------------------------------------
723 // Invocation
724
725 // Simulate two hotplug events (a connect and a disconnect)
726 mFlinger.onHotplugReceived(currentSequenceId, displayId1, HWC2::Connection::Connected);
727 mFlinger.onHotplugReceived(currentSequenceId, displayId2, HWC2::Connection::Disconnected);
728
729 // --------------------------------------------------------------------
730 // Postconditions
731
732 // The display transaction needed flag should be set.
733 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
734
735 // All events should be in the pending event queue.
736 const auto& pendingEvents = mFlinger.mutablePendingHotplugEvents();
737 ASSERT_EQ(2u, pendingEvents.size());
738 EXPECT_EQ(displayId1, pendingEvents[0].display);
739 EXPECT_EQ(HWC2::Connection::Connected, pendingEvents[0].connection);
740 EXPECT_EQ(displayId2, pendingEvents[1].display);
741 EXPECT_EQ(HWC2::Connection::Disconnected, pendingEvents[1].connection);
742}
743
744TEST_F(DisplayTransactionTest, hotplugDiscardsUnexpectedEvents) {
745 constexpr int currentSequenceId = 123;
746 constexpr int otherSequenceId = 321;
747 constexpr hwc2_display_t displayId = 456;
748
749 // --------------------------------------------------------------------
750 // Preconditions
751
752 // Set the current sequence id for accepted events
753 mFlinger.mutableComposerSequenceId() = currentSequenceId;
754
755 // Set the main thread id so that the current thread does not appear to be
756 // the main thread.
757 mFlinger.mutableMainThreadId() = std::thread::id();
758
759 // --------------------------------------------------------------------
760 // Call Expectations
761
762 // We do not expect any calls to invalidate().
763 EXPECT_CALL(*mMessageQueue, invalidate()).Times(0);
764
765 // --------------------------------------------------------------------
766 // Invocation
767
768 // Call with an unexpected sequence id
769 mFlinger.onHotplugReceived(otherSequenceId, displayId, HWC2::Connection::Invalid);
770
771 // --------------------------------------------------------------------
772 // Postconditions
773
774 // The display transaction needed flag should not be set
775 EXPECT_FALSE(hasTransactionFlagSet(eDisplayTransactionNeeded));
776
777 // There should be no pending events
778 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
779}
780
781TEST_F(DisplayTransactionTest, hotplugProcessesEnqueuedEventsIfCalledOnMainThread) {
782 constexpr int currentSequenceId = 123;
783 constexpr hwc2_display_t displayId1 = 456;
784
785 // --------------------------------------------------------------------
786 // Note:
787 // --------------------------------------------------------------------
788 // This test case is a bit tricky. We want to verify that
789 // onHotplugReceived() calls processDisplayHotplugEventsLocked(), but we
790 // don't really want to provide coverage for everything the later function
791 // does as there are specific tests for it.
792 // --------------------------------------------------------------------
793
794 // --------------------------------------------------------------------
795 // Preconditions
796
797 // Set the current sequence id for accepted events
798 mFlinger.mutableComposerSequenceId() = currentSequenceId;
799
800 // Set the main thread id so that the current thread does appear to be the
801 // main thread.
802 mFlinger.mutableMainThreadId() = std::this_thread::get_id();
803
804 // --------------------------------------------------------------------
805 // Call Expectations
806
807 // We expect invalidate() to be invoked once to trigger display transaction
808 // processing.
809 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
810
811 // --------------------------------------------------------------------
812 // Invocation
813
814 // Simulate a disconnect on a display id that is not connected. This should
815 // be enqueued by onHotplugReceived(), and dequeued by
816 // processDisplayHotplugEventsLocked(), but then ignored as invalid.
817 mFlinger.onHotplugReceived(currentSequenceId, displayId1, HWC2::Connection::Disconnected);
818
819 // --------------------------------------------------------------------
820 // Postconditions
821
822 // The display transaction needed flag should be set.
823 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
824
825 // There should be no event queued on return, as it should have been
826 // processed.
827 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
828}
829
830/* ------------------------------------------------------------------------
Lloyd Piquea482f992018-01-22 19:00:34 -0800831 * SurfaceFlinger::createDisplay
832 */
833
834TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForNonsecureDisplay) {
835 const String8 name("virtual.test");
836
837 // --------------------------------------------------------------------
838 // Call Expectations
839
840 // The call should notify the interceptor that a display was created.
841 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
842
843 // --------------------------------------------------------------------
844 // Invocation
845
846 sp<IBinder> displayToken = mFlinger.createDisplay(name, false);
847
848 // --------------------------------------------------------------------
849 // Postconditions
850
851 // The display should have been added to the current state
852 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
853 const auto& display = getCurrentDisplayState(displayToken);
854 EXPECT_EQ(DisplayDevice::DISPLAY_VIRTUAL, display.type);
855 EXPECT_EQ(false, display.isSecure);
856 EXPECT_EQ(name.string(), display.displayName);
857
858 // --------------------------------------------------------------------
859 // Cleanup conditions
860
861 // Destroying the display invalidates the display state.
862 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
863}
864
865TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForSecureDisplay) {
866 const String8 name("virtual.test");
867
868 // --------------------------------------------------------------------
869 // Call Expectations
870
871 // The call should notify the interceptor that a display was created.
872 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
873
874 // --------------------------------------------------------------------
875 // Invocation
876
877 sp<IBinder> displayToken = mFlinger.createDisplay(name, true);
878
879 // --------------------------------------------------------------------
880 // Postconditions
881
882 // The display should have been added to the current state
883 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
884 const auto& display = getCurrentDisplayState(displayToken);
885 EXPECT_EQ(DisplayDevice::DISPLAY_VIRTUAL, display.type);
886 EXPECT_EQ(true, display.isSecure);
887 EXPECT_EQ(name.string(), display.displayName);
888
889 // --------------------------------------------------------------------
890 // Cleanup conditions
891
892 // Destroying the display invalidates the display state.
893 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
894}
895
896/* ------------------------------------------------------------------------
897 * SurfaceFlinger::destroyDisplay
898 */
899
900TEST_F(DisplayTransactionTest, destroyDisplayClearsCurrentStateForDisplay) {
901 using Case = NonHwcVirtualDisplayCase;
902
903 // --------------------------------------------------------------------
904 // Preconditions
905
906 // A virtual display exists
907 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
908 existing.inject();
909
910 // --------------------------------------------------------------------
911 // Call Expectations
912
913 // The call should notify the interceptor that a display was created.
914 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
915
916 // Destroying the display invalidates the display state.
917 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
918
919 // --------------------------------------------------------------------
920 // Invocation
921
922 mFlinger.destroyDisplay(existing.token());
923
924 // --------------------------------------------------------------------
925 // Postconditions
926
927 // The display should have been removed from the current state
928 EXPECT_FALSE(hasCurrentDisplayState(existing.token()));
929
930 // Ths display should still exist in the drawing state
931 EXPECT_TRUE(hasDrawingDisplayState(existing.token()));
932
933 // The display transaction needed flasg should be set
934 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
935}
936
937TEST_F(DisplayTransactionTest, destroyDisplayHandlesUnknownDisplay) {
938 // --------------------------------------------------------------------
939 // Preconditions
940
941 sp<BBinder> displayToken = new BBinder();
942
943 // --------------------------------------------------------------------
944 // Invocation
945
946 mFlinger.destroyDisplay(displayToken);
947}
948
949/* ------------------------------------------------------------------------
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -0800950 * SurfaceFlinger::resetDisplayState
951 */
952
953TEST_F(DisplayTransactionTest, resetDisplayStateClearsState) {
954 using Case = NonHwcVirtualDisplayCase;
955
956 // --------------------------------------------------------------------
957 // Preconditions
958
959 // vsync is enabled and available
960 mFlinger.mutablePrimaryHWVsyncEnabled() = true;
961 mFlinger.mutableHWVsyncAvailable() = true;
962
963 // A display exists
964 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
965 existing.inject();
966
967 // --------------------------------------------------------------------
968 // Call Expectations
969
970 // The call disable vsyncs
971 EXPECT_CALL(*mEventControlThread, setVsyncEnabled(false)).Times(1);
972
973 // The call clears the current render engine surface
974 EXPECT_CALL(*mRenderEngine, resetCurrentSurface());
975
976 // --------------------------------------------------------------------
977 // Invocation
978
979 mFlinger.resetDisplayState();
980
981 // --------------------------------------------------------------------
982 // Postconditions
983
984 // vsyncs should be off and not available.
985 EXPECT_FALSE(mFlinger.mutablePrimaryHWVsyncEnabled());
986 EXPECT_FALSE(mFlinger.mutableHWVsyncAvailable());
987
988 // The display should have been removed from the display map.
989 EXPECT_FALSE(hasDisplayDevice(existing.token()));
990
991 // The display should still exist in the current state
992 EXPECT_TRUE(hasCurrentDisplayState(existing.token()));
993
994 // The display should have been removed from the drawing state
995 EXPECT_FALSE(hasDrawingDisplayState(existing.token()));
996}
997
998/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800999 * SurfaceFlinger::setupNewDisplayDeviceInternal
1000 */
1001
1002class SetupNewDisplayDeviceInternalTest : public DisplayTransactionTest {
1003public:
1004 template <typename T>
1005 void setupNewDisplayDeviceInternalTest();
1006};
1007
1008template <typename Case>
1009void SetupNewDisplayDeviceInternalTest::setupNewDisplayDeviceInternalTest() {
1010 const sp<BBinder> displayToken = new BBinder();
1011 const sp<mock::DisplaySurface> displaySurface = new mock::DisplaySurface();
1012 const sp<mock::GraphicBufferProducer> producer = new mock::GraphicBufferProducer();
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001013
1014 // --------------------------------------------------------------------
1015 // Preconditions
1016
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001017 // Wide color displays support is configured appropriately
1018 Case::WideColorSupport::injectConfigChange(this);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001019
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001020 // The display is setup with the HWC.
1021 Case::Display::injectHwcDisplay(this);
1022
1023 // SurfaceFlinger will use a test-controlled factory for native window
1024 // surfaces.
1025 injectFakeNativeWindowSurfaceFactory();
1026
1027 // --------------------------------------------------------------------
1028 // Call Expectations
1029
1030 // Various native window calls will be made.
1031 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
Lloyd Pique3c085a02018-05-09 19:38:32 -07001032 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001033 Case::WideColorSupport::setupComposerCallExpectations(this);
1034 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001035 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001036
1037 // --------------------------------------------------------------------
1038 // Invocation
1039
1040 DisplayDeviceState state;
1041 state.type = Case::Display::TYPE;
1042 state.isSecure = static_cast<bool>(Case::Display::SECURE);
1043
1044 auto device = mFlinger.setupNewDisplayDeviceInternal(displayToken, Case::Display::TYPE, state,
1045 displaySurface, producer);
1046
1047 // --------------------------------------------------------------------
1048 // Postconditions
1049
1050 ASSERT_TRUE(device != nullptr);
1051 EXPECT_EQ(Case::Display::TYPE, device->getDisplayType());
1052 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
1053 EXPECT_EQ(Case::Display::WIDTH, device->getWidth());
1054 EXPECT_EQ(Case::Display::HEIGHT, device->getHeight());
1055 EXPECT_EQ(Case::WideColorSupport::WIDE_COLOR_SUPPORTED, device->hasWideColorGamut());
1056 EXPECT_EQ(Case::HdrSupport::HDR10_SUPPORTED, device->hasHDR10Support());
1057 EXPECT_EQ(Case::HdrSupport::HDR_HLG_SUPPORTED, device->hasHLGSupport());
1058 EXPECT_EQ(Case::HdrSupport::HDR_DOLBY_VISION_SUPPORTED, device->hasDolbyVisionSupport());
Lloyd Pique3c085a02018-05-09 19:38:32 -07001059 // Note: This is not Case::Display::HWC_ACTIVE_CONFIG_ID as the ids are
1060 // remapped, and the test only ever sets up one config. If there were an error
1061 // looking up the remapped index, device->getActiveConfig() would be -1 instead.
1062 EXPECT_EQ(0, device->getActiveConfig());
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001063 EXPECT_EQ(Case::PerFrameMetadataSupport::PER_FRAME_METADATA_KEYS,
1064 device->getSupportedPerFrameMetadata());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001065}
1066
1067TEST_F(SetupNewDisplayDeviceInternalTest, createSimplePrimaryDisplay) {
1068 setupNewDisplayDeviceInternalTest<SimplePrimaryDisplayCase>();
1069}
1070
1071TEST_F(SetupNewDisplayDeviceInternalTest, createSimpleExternalDisplay) {
1072 setupNewDisplayDeviceInternalTest<SimpleExternalDisplayCase>();
1073}
1074
1075TEST_F(SetupNewDisplayDeviceInternalTest, createNonHwcVirtualDisplay) {
1076 setupNewDisplayDeviceInternalTest<NonHwcVirtualDisplayCase>();
1077}
1078
1079TEST_F(SetupNewDisplayDeviceInternalTest, createHwcVirtualDisplay) {
1080 // We need to resize this so that the HWC thinks the virtual display
1081 // is something it created.
1082 mFlinger.mutableHwcDisplayData().resize(3);
1083
1084 setupNewDisplayDeviceInternalTest<HwcVirtualDisplayCase>();
1085}
1086
1087TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3Display) {
1088 setupNewDisplayDeviceInternalTest<WideColorP3ColorimetricDisplayCase>();
1089}
1090
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001091TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10Display) {
1092 setupNewDisplayDeviceInternalTest<Hdr10DisplayCase>();
1093}
1094
1095TEST_F(SetupNewDisplayDeviceInternalTest, createHdrHlgDisplay) {
1096 setupNewDisplayDeviceInternalTest<HdrHlgDisplayCase>();
1097}
1098
1099TEST_F(SetupNewDisplayDeviceInternalTest, createHdrDolbyVisionDisplay) {
1100 setupNewDisplayDeviceInternalTest<HdrDolbyVisionDisplayCase>();
1101}
1102
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001103TEST_F(SetupNewDisplayDeviceInternalTest, createHdrSmpte2086DisplayCase) {
1104 setupNewDisplayDeviceInternalTest<HdrSmpte2086DisplayCase>();
1105}
1106
1107TEST_F(SetupNewDisplayDeviceInternalTest, createHdrCta816_3_DisplayCase) {
1108 setupNewDisplayDeviceInternalTest<HdrCta861_3_DisplayCase>();
1109}
1110
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001111/* ------------------------------------------------------------------------
1112 * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded)
1113 */
1114
1115class HandleTransactionLockedTest : public DisplayTransactionTest {
1116public:
1117 template <typename Case>
1118 void setupCommonPreconditions();
1119
1120 template <typename Case>
1121 void setupCommonCallExpectationsForConnectProcessing();
1122
1123 template <typename Case>
1124 void setupCommonCallExpectationsForDisconnectProcessing();
1125
1126 template <typename Case>
1127 void processesHotplugConnectCommon();
1128
1129 template <typename Case>
1130 void ignoresHotplugConnectCommon();
1131
1132 template <typename Case>
1133 void processesHotplugDisconnectCommon();
1134
1135 template <typename Case>
1136 void verifyDisplayIsConnected(const sp<IBinder>& displayToken);
1137
1138 template <typename Case>
1139 void verifyPhysicalDisplayIsConnected();
1140
1141 void verifyDisplayIsNotConnected(const sp<IBinder>& displayToken);
1142};
1143
1144template <typename Case>
1145void HandleTransactionLockedTest::setupCommonPreconditions() {
1146 // Wide color displays support is configured appropriately
1147 Case::WideColorSupport::injectConfigChange(this);
1148
1149 // SurfaceFlinger will use a test-controlled factory for BufferQueues
1150 injectFakeBufferQueueFactory();
1151
1152 // SurfaceFlinger will use a test-controlled factory for native window
1153 // surfaces.
1154 injectFakeNativeWindowSurfaceFactory();
1155}
1156
1157template <typename Case>
1158void HandleTransactionLockedTest::setupCommonCallExpectationsForConnectProcessing() {
1159 Case::Display::setupHwcHotplugCallExpectations(this);
1160
1161 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1162 Case::Display::setupFramebufferProducerBufferQueueCallExpectations(this);
1163 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1164 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
1165
1166 Case::WideColorSupport::setupComposerCallExpectations(this);
1167 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001168 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001169
1170 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
1171 EXPECT_CALL(*mEventThread, onHotplugReceived(Case::Display::TYPE, true)).Times(1);
1172}
1173
1174template <typename Case>
1175void HandleTransactionLockedTest::setupCommonCallExpectationsForDisconnectProcessing() {
1176 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
1177 EXPECT_CALL(*mEventThread, onHotplugReceived(Case::Display::TYPE, false)).Times(1);
1178}
1179
1180template <typename Case>
1181void HandleTransactionLockedTest::verifyDisplayIsConnected(const sp<IBinder>& displayToken) {
1182 // The display device should have been set up in the list of displays.
1183 ASSERT_TRUE(hasDisplayDevice(displayToken));
1184 const auto& device = getDisplayDevice(displayToken);
1185 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
1186 EXPECT_EQ(Case::Display::TYPE == DisplayDevice::DISPLAY_PRIMARY, device->isPrimary());
1187
1188 // The display should have been set up in the current display state
1189 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1190 const auto& current = getCurrentDisplayState(displayToken);
1191 EXPECT_EQ(Case::Display::TYPE, current.type);
1192
1193 // The display should have been set up in the drawing display state
1194 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1195 const auto& draw = getDrawingDisplayState(displayToken);
1196 EXPECT_EQ(Case::Display::TYPE, draw.type);
1197}
1198
1199template <typename Case>
1200void HandleTransactionLockedTest::verifyPhysicalDisplayIsConnected() {
1201 // HWComposer should have an entry for the display
1202 EXPECT_TRUE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1203
1204 // The display should be set up as a built-in display.
1205 static_assert(0 <= Case::Display::TYPE &&
1206 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES,
1207 "Must use a valid physical display type index for the fixed-size array");
1208 auto& displayToken = mFlinger.mutableBuiltinDisplays()[Case::Display::TYPE];
1209 ASSERT_TRUE(displayToken != nullptr);
1210
1211 verifyDisplayIsConnected<Case>(displayToken);
1212}
1213
1214void HandleTransactionLockedTest::verifyDisplayIsNotConnected(const sp<IBinder>& displayToken) {
1215 EXPECT_FALSE(hasDisplayDevice(displayToken));
1216 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1217 EXPECT_FALSE(hasDrawingDisplayState(displayToken));
1218}
1219
1220template <typename Case>
1221void HandleTransactionLockedTest::processesHotplugConnectCommon() {
1222 // --------------------------------------------------------------------
1223 // Preconditions
1224
1225 setupCommonPreconditions<Case>();
1226
1227 // A hotplug connect event is enqueued for a display
1228 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001229
1230 // --------------------------------------------------------------------
1231 // Call Expectations
1232
1233 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillOnce(Return(false));
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001234
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001235 setupCommonCallExpectationsForConnectProcessing<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001236
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001237 // --------------------------------------------------------------------
1238 // Invocation
Lloyd Piquee39cad22017-12-20 17:01:29 -08001239
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001240 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
Lloyd Piquee39cad22017-12-20 17:01:29 -08001241
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001242 // --------------------------------------------------------------------
1243 // Postconditions
1244
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001245 verifyPhysicalDisplayIsConnected<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001246
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001247 // --------------------------------------------------------------------
1248 // Cleanup conditions
1249
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001250 EXPECT_CALL(*mComposer,
1251 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001252 .WillOnce(Return(Error::NONE));
1253 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Piquef58625d2017-12-19 13:22:33 -08001254}
1255
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001256template <typename Case>
1257void HandleTransactionLockedTest::ignoresHotplugConnectCommon() {
1258 // --------------------------------------------------------------------
1259 // Preconditions
1260
1261 setupCommonPreconditions<Case>();
1262
1263 // A hotplug connect event is enqueued for a display
1264 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1265
1266 // --------------------------------------------------------------------
1267 // Invocation
1268
1269 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1270
1271 // --------------------------------------------------------------------
1272 // Postconditions
1273
1274 // HWComposer should not have an entry for the display
1275 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1276}
1277
1278template <typename Case>
1279void HandleTransactionLockedTest::processesHotplugDisconnectCommon() {
1280 // --------------------------------------------------------------------
1281 // Preconditions
1282
1283 setupCommonPreconditions<Case>();
1284
1285 // A hotplug disconnect event is enqueued for a display
1286 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1287
1288 // The display is already completely set up.
1289 Case::Display::injectHwcDisplay(this);
1290 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1291 existing.inject();
1292
1293 // --------------------------------------------------------------------
1294 // Call Expectations
1295
1296 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1297
1298 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1299
1300 // --------------------------------------------------------------------
1301 // Invocation
1302
1303 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1304
1305 // --------------------------------------------------------------------
1306 // Postconditions
1307
1308 // HWComposer should not have an entry for the display
1309 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1310
1311 // The display should not be set up as a built-in display.
1312 ASSERT_TRUE(0 <= Case::Display::TYPE &&
1313 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES);
1314 auto displayToken = mFlinger.mutableBuiltinDisplays()[Case::Display::TYPE];
1315 EXPECT_TRUE(displayToken == nullptr);
1316
1317 // The existing token should have been removed
1318 verifyDisplayIsNotConnected(existing.token());
1319}
1320
1321TEST_F(HandleTransactionLockedTest, processesHotplugConnectPrimaryDisplay) {
1322 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1323}
1324
1325TEST_F(HandleTransactionLockedTest,
1326 processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected) {
1327 // Inject an external display.
1328 ExternalDisplayVariant::injectHwcDisplay(this);
1329
1330 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1331}
1332
1333TEST_F(HandleTransactionLockedTest, processesHotplugConnectExternalDisplay) {
1334 // Inject a primary display.
1335 PrimaryDisplayVariant::injectHwcDisplay(this);
1336
1337 processesHotplugConnectCommon<SimpleExternalDisplayCase>();
1338}
1339
1340TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected) {
1341 // Inject both a primary and external display.
1342 PrimaryDisplayVariant::injectHwcDisplay(this);
1343 ExternalDisplayVariant::injectHwcDisplay(this);
1344
1345 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1346
1347 ignoresHotplugConnectCommon<SimpleTertiaryDisplayCase>();
1348}
1349
1350TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfExternalForVrComposer) {
1351 // Inject a primary display.
1352 PrimaryDisplayVariant::injectHwcDisplay(this);
1353
1354 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(true));
1355
1356 ignoresHotplugConnectCommon<SimpleExternalDisplayCase>();
1357}
1358
1359TEST_F(HandleTransactionLockedTest, processHotplugDisconnectPrimaryDisplay) {
1360 processesHotplugDisconnectCommon<SimplePrimaryDisplayCase>();
1361}
1362
1363TEST_F(HandleTransactionLockedTest, processHotplugDisconnectExternalDisplay) {
1364 processesHotplugDisconnectCommon<SimpleExternalDisplayCase>();
1365}
1366
1367TEST_F(HandleTransactionLockedTest, processesHotplugConnectThenDisconnectPrimary) {
1368 using Case = SimplePrimaryDisplayCase;
1369
1370 // --------------------------------------------------------------------
1371 // Preconditions
1372
1373 setupCommonPreconditions<Case>();
1374
1375 // A hotplug connect event is enqueued for a display
1376 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1377 // A hotplug disconnect event is also enqueued for the same display
1378 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1379
1380 // --------------------------------------------------------------------
1381 // Call Expectations
1382
1383 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1384
1385 setupCommonCallExpectationsForConnectProcessing<Case>();
1386 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1387
1388 EXPECT_CALL(*mComposer,
1389 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1390 .WillOnce(Return(Error::NONE));
1391 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1392
1393 // --------------------------------------------------------------------
1394 // Invocation
1395
1396 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1397
1398 // --------------------------------------------------------------------
1399 // Postconditions
1400
1401 // HWComposer should not have an entry for the display
1402 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1403
1404 // The display should not be set up as a primary built-in display.
1405 ASSERT_TRUE(0 <= Case::Display::TYPE &&
1406 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES);
1407 auto displayToken = mFlinger.mutableBuiltinDisplays()[Case::Display::TYPE];
1408 EXPECT_TRUE(displayToken == nullptr);
1409}
1410
1411TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectThenConnectPrimary) {
1412 using Case = SimplePrimaryDisplayCase;
1413
1414 // --------------------------------------------------------------------
1415 // Preconditions
1416
1417 setupCommonPreconditions<Case>();
1418
1419 // The display is already completely set up.
1420 Case::Display::injectHwcDisplay(this);
1421 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1422 existing.inject();
1423
1424 // A hotplug disconnect event is enqueued for a display
1425 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1426 // A hotplug connect event is also enqueued for the same display
1427 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1428
1429 // --------------------------------------------------------------------
1430 // Call Expectations
1431
1432 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1433
1434 setupCommonCallExpectationsForConnectProcessing<Case>();
1435 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1436
1437 // --------------------------------------------------------------------
1438 // Invocation
1439
1440 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1441
1442 // --------------------------------------------------------------------
1443 // Postconditions
1444
1445 // The existing token should have been removed
1446 verifyDisplayIsNotConnected(existing.token());
1447 static_assert(0 <= Case::Display::TYPE &&
1448 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES,
1449 "Display type must be a built-in display");
1450 EXPECT_NE(existing.token(), mFlinger.mutableBuiltinDisplays()[Case::Display::TYPE]);
1451
1452 // A new display should be connected in its place
1453
1454 verifyPhysicalDisplayIsConnected<Case>();
1455
1456 // --------------------------------------------------------------------
1457 // Cleanup conditions
1458
1459 EXPECT_CALL(*mComposer,
1460 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1461 .WillOnce(Return(Error::NONE));
1462 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1463}
1464
1465TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAdded) {
1466 using Case = HwcVirtualDisplayCase;
1467
1468 // --------------------------------------------------------------------
1469 // Preconditions
1470
1471 // The HWC supports at least one virtual display
1472 injectMockComposer(1);
1473
1474 setupCommonPreconditions<Case>();
1475
1476 // A virtual display was added to the current state, and it has a
1477 // surface(producer)
1478 sp<BBinder> displayToken = new BBinder();
Lloyd Pique4c2ac022018-04-27 12:08:03 -07001479
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001480 DisplayDeviceState info;
1481 info.type = Case::Display::TYPE;
1482 info.isSecure = static_cast<bool>(Case::Display::SECURE);
1483
1484 sp<mock::GraphicBufferProducer> surface{new mock::GraphicBufferProducer()};
1485 info.surface = surface;
1486 mFlinger.mutableCurrentState().displays.add(displayToken, info);
1487
1488 // --------------------------------------------------------------------
1489 // Call Expectations
1490
1491 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1492 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1493
1494 EXPECT_CALL(*surface, query(NATIVE_WINDOW_WIDTH, _))
1495 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::WIDTH), Return(NO_ERROR)));
1496 EXPECT_CALL(*surface, query(NATIVE_WINDOW_HEIGHT, _))
1497 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::HEIGHT), Return(NO_ERROR)));
1498 EXPECT_CALL(*surface, query(NATIVE_WINDOW_FORMAT, _))
1499 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT),
1500 Return(NO_ERROR)));
1501 EXPECT_CALL(*surface, query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, _))
1502 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(NO_ERROR)));
1503
1504 EXPECT_CALL(*surface, setAsyncMode(true)).Times(1);
1505
1506 EXPECT_CALL(*mProducer, connect(_, _, _, _)).Times(1);
1507 EXPECT_CALL(*mProducer, disconnect(_, _)).Times(1);
1508
1509 Case::Display::setupHwcVirtualDisplayCreationCallExpectations(this);
1510 Case::WideColorSupport::setupComposerCallExpectations(this);
1511 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001512 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001513
1514 // --------------------------------------------------------------------
1515 // Invocation
1516
1517 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1518
1519 // --------------------------------------------------------------------
1520 // Postconditions
1521
1522 // The display device should have been set up in the list of displays.
1523 verifyDisplayIsConnected<Case>(displayToken);
1524
1525 // --------------------------------------------------------------------
1526 // Cleanup conditions
1527
1528 EXPECT_CALL(*mComposer, destroyVirtualDisplay(Case::Display::HWC_DISPLAY_ID))
1529 .WillOnce(Return(Error::NONE));
1530 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1531}
1532
1533TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAddedWithNoSurface) {
1534 using Case = HwcVirtualDisplayCase;
1535
1536 // --------------------------------------------------------------------
1537 // Preconditions
1538
1539 // The HWC supports at least one virtual display
1540 injectMockComposer(1);
1541
1542 setupCommonPreconditions<Case>();
1543
1544 // A virtual display was added to the current state, but it does not have a
1545 // surface.
1546 sp<BBinder> displayToken = new BBinder();
1547
1548 DisplayDeviceState info;
1549 info.type = Case::Display::TYPE;
1550 info.isSecure = static_cast<bool>(Case::Display::SECURE);
1551
1552 mFlinger.mutableCurrentState().displays.add(displayToken, info);
1553
1554 // --------------------------------------------------------------------
1555 // Call Expectations
1556
1557 // --------------------------------------------------------------------
1558 // Invocation
1559
1560 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1561
1562 // --------------------------------------------------------------------
1563 // Postconditions
1564
1565 // There will not be a display device set up.
1566 EXPECT_FALSE(hasDisplayDevice(displayToken));
1567
1568 // The drawing display state will be set from the current display state.
1569 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1570 const auto& draw = getDrawingDisplayState(displayToken);
1571 EXPECT_EQ(Case::Display::TYPE, draw.type);
1572}
1573
1574TEST_F(HandleTransactionLockedTest, processesVirtualDisplayRemoval) {
1575 using Case = HwcVirtualDisplayCase;
1576
1577 // --------------------------------------------------------------------
1578 // Preconditions
1579
1580 // A virtual display is set up but is removed from the current state.
1581 mFlinger.mutableHwcDisplayData().resize(3);
1582 Case::Display::injectHwcDisplay(this);
1583 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1584 existing.inject();
1585 mFlinger.mutableCurrentState().displays.removeItem(existing.token());
1586
1587 // --------------------------------------------------------------------
1588 // Call Expectations
1589
1590 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1591
1592 // --------------------------------------------------------------------
1593 // Invocation
1594
1595 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1596
1597 // --------------------------------------------------------------------
1598 // Postconditions
1599
1600 // The existing token should have been removed
1601 verifyDisplayIsNotConnected(existing.token());
1602}
1603
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001604TEST_F(HandleTransactionLockedTest, processesDisplayLayerStackChanges) {
1605 using Case = NonHwcVirtualDisplayCase;
1606
1607 constexpr uint32_t oldLayerStack = 0u;
1608 constexpr uint32_t newLayerStack = 123u;
1609
1610 // --------------------------------------------------------------------
1611 // Preconditions
1612
1613 // A display is set up
1614 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1615 display.inject();
1616
1617 // There is a change to the layerStack state
1618 display.mutableDrawingDisplayState().layerStack = oldLayerStack;
1619 display.mutableCurrentDisplayState().layerStack = newLayerStack;
1620
1621 // --------------------------------------------------------------------
1622 // Invocation
1623
1624 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1625
1626 // --------------------------------------------------------------------
1627 // Postconditions
1628
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001629 EXPECT_EQ(newLayerStack, display.mutableDisplayDevice()->getLayerStack());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001630}
1631
1632TEST_F(HandleTransactionLockedTest, processesDisplayTransformChanges) {
1633 using Case = NonHwcVirtualDisplayCase;
1634
1635 constexpr int oldTransform = 0;
1636 constexpr int newTransform = 2;
1637
1638 // --------------------------------------------------------------------
1639 // Preconditions
1640
1641 // A display is set up
1642 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1643 display.inject();
1644
1645 // There is a change to the orientation state
1646 display.mutableDrawingDisplayState().orientation = oldTransform;
1647 display.mutableCurrentDisplayState().orientation = newTransform;
1648
1649 // --------------------------------------------------------------------
1650 // Invocation
1651
1652 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1653
1654 // --------------------------------------------------------------------
1655 // Postconditions
1656
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001657 EXPECT_EQ(newTransform, display.mutableDisplayDevice()->getOrientation());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001658}
1659
1660TEST_F(HandleTransactionLockedTest, processesDisplayViewportChanges) {
1661 using Case = NonHwcVirtualDisplayCase;
1662
1663 const Rect oldViewport(0, 0, 0, 0);
1664 const Rect newViewport(0, 0, 123, 456);
1665
1666 // --------------------------------------------------------------------
1667 // Preconditions
1668
1669 // A display is set up
1670 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1671 display.inject();
1672
1673 // There is a change to the viewport state
1674 display.mutableDrawingDisplayState().viewport = oldViewport;
1675 display.mutableCurrentDisplayState().viewport = newViewport;
1676
1677 // --------------------------------------------------------------------
1678 // Invocation
1679
1680 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1681
1682 // --------------------------------------------------------------------
1683 // Postconditions
1684
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001685 EXPECT_EQ(newViewport, display.mutableDisplayDevice()->getViewport());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001686}
1687
1688TEST_F(HandleTransactionLockedTest, processesDisplayFrameChanges) {
1689 using Case = NonHwcVirtualDisplayCase;
1690
1691 const Rect oldFrame(0, 0, 0, 0);
1692 const Rect newFrame(0, 0, 123, 456);
1693
1694 // --------------------------------------------------------------------
1695 // Preconditions
1696
1697 // A display is set up
1698 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1699 display.inject();
1700
1701 // There is a change to the viewport state
1702 display.mutableDrawingDisplayState().frame = oldFrame;
1703 display.mutableCurrentDisplayState().frame = newFrame;
1704
1705 // --------------------------------------------------------------------
1706 // Invocation
1707
1708 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1709
1710 // --------------------------------------------------------------------
1711 // Postconditions
1712
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001713 EXPECT_EQ(newFrame, display.mutableDisplayDevice()->getFrame());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001714}
1715
1716TEST_F(HandleTransactionLockedTest, processesDisplayWidthChanges) {
1717 using Case = NonHwcVirtualDisplayCase;
1718
1719 constexpr int oldWidth = 0;
1720 constexpr int oldHeight = 10;
1721 constexpr int newWidth = 123;
1722
1723 // --------------------------------------------------------------------
1724 // Preconditions
1725
1726 // A display is set up
1727 auto nativeWindow = new mock::NativeWindow();
1728 auto displaySurface = new mock::DisplaySurface();
1729 auto renderSurface = new RE::mock::Surface();
1730 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1731 display.setNativeWindow(nativeWindow);
1732 display.setDisplaySurface(displaySurface);
1733 display.setRenderSurface(std::unique_ptr<RE::Surface>(renderSurface));
1734 display.inject();
1735
1736 // There is a change to the viewport state
1737 display.mutableDrawingDisplayState().width = oldWidth;
1738 display.mutableDrawingDisplayState().height = oldHeight;
1739 display.mutableCurrentDisplayState().width = newWidth;
1740 display.mutableCurrentDisplayState().height = oldHeight;
1741
1742 // --------------------------------------------------------------------
1743 // Call Expectations
1744
1745 EXPECT_CALL(*renderSurface, setNativeWindow(nullptr)).Times(1);
1746 EXPECT_CALL(*displaySurface, resizeBuffers(newWidth, oldHeight)).Times(1);
1747 EXPECT_CALL(*renderSurface, setNativeWindow(nativeWindow)).Times(1);
1748 EXPECT_CALL(*renderSurface, queryWidth()).WillOnce(Return(newWidth));
1749 EXPECT_CALL(*renderSurface, queryHeight()).WillOnce(Return(oldHeight));
1750
1751 // --------------------------------------------------------------------
1752 // Invocation
1753
1754 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1755}
1756
1757TEST_F(HandleTransactionLockedTest, processesDisplayHeightChanges) {
1758 using Case = NonHwcVirtualDisplayCase;
1759
1760 constexpr int oldWidth = 0;
1761 constexpr int oldHeight = 10;
1762 constexpr int newHeight = 123;
1763
1764 // --------------------------------------------------------------------
1765 // Preconditions
1766
1767 // A display is set up
1768 auto nativeWindow = new mock::NativeWindow();
1769 auto displaySurface = new mock::DisplaySurface();
1770 auto renderSurface = new RE::mock::Surface();
1771 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1772 display.setNativeWindow(nativeWindow);
1773 display.setDisplaySurface(displaySurface);
1774 display.setRenderSurface(std::unique_ptr<RE::Surface>(renderSurface));
1775 display.inject();
1776
1777 // There is a change to the viewport state
1778 display.mutableDrawingDisplayState().width = oldWidth;
1779 display.mutableDrawingDisplayState().height = oldHeight;
1780 display.mutableCurrentDisplayState().width = oldWidth;
1781 display.mutableCurrentDisplayState().height = newHeight;
1782
1783 // --------------------------------------------------------------------
1784 // Call Expectations
1785
1786 EXPECT_CALL(*renderSurface, setNativeWindow(nullptr)).Times(1);
1787 EXPECT_CALL(*displaySurface, resizeBuffers(oldWidth, newHeight)).Times(1);
1788 EXPECT_CALL(*renderSurface, setNativeWindow(nativeWindow)).Times(1);
1789 EXPECT_CALL(*renderSurface, queryWidth()).WillOnce(Return(oldWidth));
1790 EXPECT_CALL(*renderSurface, queryHeight()).WillOnce(Return(newHeight));
1791
1792 // --------------------------------------------------------------------
1793 // Invocation
1794
1795 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1796}
1797
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001798/* ------------------------------------------------------------------------
1799 * SurfaceFlinger::setDisplayStateLocked
1800 */
1801
1802TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithUnknownDisplay) {
1803 // --------------------------------------------------------------------
1804 // Preconditions
1805
1806 // We have an unknown display token not associated with a known display
1807 sp<BBinder> displayToken = new BBinder();
1808
1809 // The requested display state references the unknown display.
1810 DisplayState state;
1811 state.what = DisplayState::eLayerStackChanged;
1812 state.token = displayToken;
1813 state.layerStack = 456;
1814
1815 // --------------------------------------------------------------------
1816 // Invocation
1817
1818 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1819
1820 // --------------------------------------------------------------------
1821 // Postconditions
1822
1823 // The returned flags are empty
1824 EXPECT_EQ(0u, flags);
1825
1826 // The display token still doesn't match anything known.
1827 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1828}
1829
1830TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithInvalidDisplay) {
1831 using Case = InvalidDisplayCase;
1832
1833 // --------------------------------------------------------------------
1834 // Preconditions
1835
1836 // An invalid display is set up
1837 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1838 display.inject();
1839
1840 // The invalid display has some state
1841 display.mutableCurrentDisplayState().layerStack = 654u;
1842
1843 // The requested display state tries to change the display state.
1844 DisplayState state;
1845 state.what = DisplayState::eLayerStackChanged;
1846 state.token = display.token();
1847 state.layerStack = 456;
1848
1849 // --------------------------------------------------------------------
1850 // Invocation
1851
1852 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1853
1854 // --------------------------------------------------------------------
1855 // Postconditions
1856
1857 // The returned flags are empty
1858 EXPECT_EQ(0u, flags);
1859
1860 // The current display layer stack value is unchanged.
1861 EXPECT_EQ(654u, getCurrentDisplayState(display.token()).layerStack);
1862}
1863
1864TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWhenNoChanges) {
1865 using Case = SimplePrimaryDisplayCase;
1866
1867 // --------------------------------------------------------------------
1868 // Preconditions
1869
1870 // A display is already set up
1871 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1872 display.inject();
1873
1874 // No changes are made to the display
1875 DisplayState state;
1876 state.what = 0;
1877 state.token = display.token();
1878
1879 // --------------------------------------------------------------------
1880 // Invocation
1881
1882 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1883
1884 // --------------------------------------------------------------------
1885 // Postconditions
1886
1887 // The returned flags are empty
1888 EXPECT_EQ(0u, flags);
1889}
1890
1891TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSurfaceDidNotChange) {
1892 using Case = SimplePrimaryDisplayCase;
1893
1894 // --------------------------------------------------------------------
1895 // Preconditions
1896
1897 // A display is already set up
1898 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1899 display.inject();
1900
1901 // There is a surface that can be set.
1902 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
1903
1904 // The current display state has the surface set
1905 display.mutableCurrentDisplayState().surface = surface;
1906
1907 // The incoming request sets the same surface
1908 DisplayState state;
1909 state.what = DisplayState::eSurfaceChanged;
1910 state.token = display.token();
1911 state.surface = surface;
1912
1913 // --------------------------------------------------------------------
1914 // Invocation
1915
1916 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1917
1918 // --------------------------------------------------------------------
1919 // Postconditions
1920
1921 // The returned flags are empty
1922 EXPECT_EQ(0u, flags);
1923
1924 // The current display state is unchanged.
1925 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
1926}
1927
1928TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfSurfaceChanged) {
1929 using Case = SimplePrimaryDisplayCase;
1930
1931 // --------------------------------------------------------------------
1932 // Preconditions
1933
1934 // A display is already set up
1935 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1936 display.inject();
1937
1938 // There is a surface that can be set.
1939 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
1940
1941 // The current display state does not have a surface
1942 display.mutableCurrentDisplayState().surface = nullptr;
1943
1944 // The incoming request sets a surface
1945 DisplayState state;
1946 state.what = DisplayState::eSurfaceChanged;
1947 state.token = display.token();
1948 state.surface = surface;
1949
1950 // --------------------------------------------------------------------
1951 // Invocation
1952
1953 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1954
1955 // --------------------------------------------------------------------
1956 // Postconditions
1957
1958 // The returned flags indicate a transaction is needed
1959 EXPECT_EQ(eDisplayTransactionNeeded, flags);
1960
1961 // The current display layer stack state is set to the new value
1962 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
1963}
1964
1965TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfLayerStackDidNotChange) {
1966 using Case = SimplePrimaryDisplayCase;
1967
1968 // --------------------------------------------------------------------
1969 // Preconditions
1970
1971 // A display is already set up
1972 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1973 display.inject();
1974
1975 // The display has a layer stack set
1976 display.mutableCurrentDisplayState().layerStack = 456u;
1977
1978 // The incoming request sets the same layer stack
1979 DisplayState state;
1980 state.what = DisplayState::eLayerStackChanged;
1981 state.token = display.token();
1982 state.layerStack = 456u;
1983
1984 // --------------------------------------------------------------------
1985 // Invocation
1986
1987 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1988
1989 // --------------------------------------------------------------------
1990 // Postconditions
1991
1992 // The returned flags are empty
1993 EXPECT_EQ(0u, flags);
1994
1995 // The current display state is unchanged
1996 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
1997}
1998
1999TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfLayerStackChanged) {
2000 using Case = SimplePrimaryDisplayCase;
2001
2002 // --------------------------------------------------------------------
2003 // Preconditions
2004
2005 // A display is set up
2006 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2007 display.inject();
2008
2009 // The display has a layer stack set
2010 display.mutableCurrentDisplayState().layerStack = 654u;
2011
2012 // The incoming request sets a different layer stack
2013 DisplayState state;
2014 state.what = DisplayState::eLayerStackChanged;
2015 state.token = display.token();
2016 state.layerStack = 456u;
2017
2018 // --------------------------------------------------------------------
2019 // Invocation
2020
2021 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2022
2023 // --------------------------------------------------------------------
2024 // Postconditions
2025
2026 // The returned flags indicate a transaction is needed
2027 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2028
2029 // The desired display state has been set to the new value.
2030 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2031}
2032
2033TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfProjectionDidNotChange) {
2034 using Case = SimplePrimaryDisplayCase;
2035 constexpr int initialOrientation = 180;
2036 const Rect initialFrame = {1, 2, 3, 4};
2037 const Rect initialViewport = {5, 6, 7, 8};
2038
2039 // --------------------------------------------------------------------
2040 // Preconditions
2041
2042 // A display is set up
2043 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2044 display.inject();
2045
2046 // The current display state projection state is all set
2047 display.mutableCurrentDisplayState().orientation = initialOrientation;
2048 display.mutableCurrentDisplayState().frame = initialFrame;
2049 display.mutableCurrentDisplayState().viewport = initialViewport;
2050
2051 // The incoming request sets the same projection state
2052 DisplayState state;
2053 state.what = DisplayState::eDisplayProjectionChanged;
2054 state.token = display.token();
2055 state.orientation = initialOrientation;
2056 state.frame = initialFrame;
2057 state.viewport = initialViewport;
2058
2059 // --------------------------------------------------------------------
2060 // Invocation
2061
2062 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2063
2064 // --------------------------------------------------------------------
2065 // Postconditions
2066
2067 // The returned flags are empty
2068 EXPECT_EQ(0u, flags);
2069
2070 // The current display state is unchanged
2071 EXPECT_EQ(initialOrientation, display.getCurrentDisplayState().orientation);
2072
2073 EXPECT_EQ(initialFrame, display.getCurrentDisplayState().frame);
2074 EXPECT_EQ(initialViewport, display.getCurrentDisplayState().viewport);
2075}
2076
2077TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfOrientationChanged) {
2078 using Case = SimplePrimaryDisplayCase;
2079 constexpr int initialOrientation = 90;
2080 constexpr int desiredOrientation = 180;
2081
2082 // --------------------------------------------------------------------
2083 // Preconditions
2084
2085 // A display is set up
2086 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2087 display.inject();
2088
2089 // The current display state has an orientation set
2090 display.mutableCurrentDisplayState().orientation = initialOrientation;
2091
2092 // The incoming request sets a different orientation
2093 DisplayState state;
2094 state.what = DisplayState::eDisplayProjectionChanged;
2095 state.token = display.token();
2096 state.orientation = desiredOrientation;
2097
2098 // --------------------------------------------------------------------
2099 // Invocation
2100
2101 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2102
2103 // --------------------------------------------------------------------
2104 // Postconditions
2105
2106 // The returned flags indicate a transaction is needed
2107 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2108
2109 // The current display state has the new value.
2110 EXPECT_EQ(desiredOrientation, display.getCurrentDisplayState().orientation);
2111}
2112
2113TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfFrameChanged) {
2114 using Case = SimplePrimaryDisplayCase;
2115 const Rect initialFrame = {0, 0, 0, 0};
2116 const Rect desiredFrame = {5, 6, 7, 8};
2117
2118 // --------------------------------------------------------------------
2119 // Preconditions
2120
2121 // A display is set up
2122 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2123 display.inject();
2124
2125 // The current display state does not have a frame
2126 display.mutableCurrentDisplayState().frame = initialFrame;
2127
2128 // The incoming request sets a frame
2129 DisplayState state;
2130 state.what = DisplayState::eDisplayProjectionChanged;
2131 state.token = display.token();
2132 state.frame = desiredFrame;
2133
2134 // --------------------------------------------------------------------
2135 // Invocation
2136
2137 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2138
2139 // --------------------------------------------------------------------
2140 // Postconditions
2141
2142 // The returned flags indicate a transaction is needed
2143 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2144
2145 // The current display state has the new value.
2146 EXPECT_EQ(desiredFrame, display.getCurrentDisplayState().frame);
2147}
2148
2149TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfViewportChanged) {
2150 using Case = SimplePrimaryDisplayCase;
2151 const Rect initialViewport = {0, 0, 0, 0};
2152 const Rect desiredViewport = {5, 6, 7, 8};
2153
2154 // --------------------------------------------------------------------
2155 // Preconditions
2156
2157 // A display is set up
2158 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2159 display.inject();
2160
2161 // The current display state does not have a viewport
2162 display.mutableCurrentDisplayState().viewport = initialViewport;
2163
2164 // The incoming request sets a viewport
2165 DisplayState state;
2166 state.what = DisplayState::eDisplayProjectionChanged;
2167 state.token = display.token();
2168 state.viewport = desiredViewport;
2169
2170 // --------------------------------------------------------------------
2171 // Invocation
2172
2173 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2174
2175 // --------------------------------------------------------------------
2176 // Postconditions
2177
2178 // The returned flags indicate a transaction is needed
2179 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2180
2181 // The current display state has the new value.
2182 EXPECT_EQ(desiredViewport, display.getCurrentDisplayState().viewport);
2183}
2184
2185TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSizeDidNotChange) {
2186 using Case = SimplePrimaryDisplayCase;
2187 constexpr uint32_t initialWidth = 1024;
2188 constexpr uint32_t initialHeight = 768;
2189
2190 // --------------------------------------------------------------------
2191 // Preconditions
2192
2193 // A display is set up
2194 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2195 display.inject();
2196
2197 // The current display state has a size set
2198 display.mutableCurrentDisplayState().width = initialWidth;
2199 display.mutableCurrentDisplayState().height = initialHeight;
2200
2201 // The incoming request sets the same display size
2202 DisplayState state;
2203 state.what = DisplayState::eDisplaySizeChanged;
2204 state.token = display.token();
2205 state.width = initialWidth;
2206 state.height = initialHeight;
2207
2208 // --------------------------------------------------------------------
2209 // Invocation
2210
2211 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2212
2213 // --------------------------------------------------------------------
2214 // Postconditions
2215
2216 // The returned flags are empty
2217 EXPECT_EQ(0u, flags);
2218
2219 // The current display state is unchanged
2220 EXPECT_EQ(initialWidth, display.getCurrentDisplayState().width);
2221 EXPECT_EQ(initialHeight, display.getCurrentDisplayState().height);
2222}
2223
2224TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfWidthChanged) {
2225 using Case = SimplePrimaryDisplayCase;
2226 constexpr uint32_t initialWidth = 0;
2227 constexpr uint32_t desiredWidth = 1024;
2228
2229 // --------------------------------------------------------------------
2230 // Preconditions
2231
2232 // A display is set up
2233 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2234 display.inject();
2235
2236 // The display does not yet have a width
2237 display.mutableCurrentDisplayState().width = initialWidth;
2238
2239 // The incoming request sets a display width
2240 DisplayState state;
2241 state.what = DisplayState::eDisplaySizeChanged;
2242 state.token = display.token();
2243 state.width = desiredWidth;
2244
2245 // --------------------------------------------------------------------
2246 // Invocation
2247
2248 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2249
2250 // --------------------------------------------------------------------
2251 // Postconditions
2252
2253 // The returned flags indicate a transaction is needed
2254 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2255
2256 // The current display state has the new value.
2257 EXPECT_EQ(desiredWidth, display.getCurrentDisplayState().width);
2258}
2259
2260TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfHeightChanged) {
2261 using Case = SimplePrimaryDisplayCase;
2262 constexpr uint32_t initialHeight = 0;
2263 constexpr uint32_t desiredHeight = 768;
2264
2265 // --------------------------------------------------------------------
2266 // Preconditions
2267
2268 // A display is set up
2269 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2270 display.inject();
2271
2272 // The display does not yet have a height
2273 display.mutableCurrentDisplayState().height = initialHeight;
2274
2275 // The incoming request sets a display height
2276 DisplayState state;
2277 state.what = DisplayState::eDisplaySizeChanged;
2278 state.token = display.token();
2279 state.height = desiredHeight;
2280
2281 // --------------------------------------------------------------------
2282 // Invocation
2283
2284 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2285
2286 // --------------------------------------------------------------------
2287 // Postconditions
2288
2289 // The returned flags indicate a transaction is needed
2290 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2291
2292 // The current display state has the new value.
2293 EXPECT_EQ(desiredHeight, display.getCurrentDisplayState().height);
2294}
2295
Lloyd Pique86016da2018-03-01 16:09:38 -08002296/* ------------------------------------------------------------------------
2297 * SurfaceFlinger::onInitializeDisplays
2298 */
2299
2300TEST_F(DisplayTransactionTest, onInitializeDisplaysSetsUpPrimaryDisplay) {
2301 using Case = SimplePrimaryDisplayCase;
2302
2303 // --------------------------------------------------------------------
2304 // Preconditions
2305
2306 // A primary display is set up
2307 Case::Display::injectHwcDisplay(this);
2308 auto primaryDisplay = Case::Display::makeFakeExistingDisplayInjector(this);
2309 primaryDisplay.inject();
2310
2311 // --------------------------------------------------------------------
2312 // Call Expectations
2313
2314 // We expect the surface interceptor to possibly be used, but we treat it as
2315 // disabled since it is called as a side effect rather than directly by this
2316 // function.
2317 EXPECT_CALL(*mSurfaceInterceptor, isEnabled()).WillOnce(Return(false));
2318
2319 // We expect a call to get the active display config.
2320 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
2321
2322 // We expect invalidate() to be invoked once to trigger display transaction
2323 // processing.
2324 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
2325
2326 // --------------------------------------------------------------------
2327 // Invocation
2328
2329 mFlinger.onInitializeDisplays();
2330
2331 // --------------------------------------------------------------------
2332 // Postconditions
2333
2334 // The primary display should have a current state
2335 ASSERT_TRUE(hasCurrentDisplayState(primaryDisplay.token()));
2336 const auto& primaryDisplayState = getCurrentDisplayState(primaryDisplay.token());
2337 // The layer stack state should be set to zero
2338 EXPECT_EQ(0u, primaryDisplayState.layerStack);
2339 // The orientation state should be set to zero
2340 EXPECT_EQ(0, primaryDisplayState.orientation);
2341
2342 // The frame state should be set to INVALID
2343 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.frame);
2344
2345 // The viewport state should be set to INVALID
2346 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.viewport);
2347
2348 // The width and height should both be zero
2349 EXPECT_EQ(0u, primaryDisplayState.width);
2350 EXPECT_EQ(0u, primaryDisplayState.height);
2351
2352 // The display should be set to HWC_POWER_MODE_NORMAL
2353 ASSERT_TRUE(hasDisplayDevice(primaryDisplay.token()));
2354 auto displayDevice = primaryDisplay.mutableDisplayDevice();
2355 EXPECT_EQ(HWC_POWER_MODE_NORMAL, displayDevice->getPowerMode());
2356
2357 // The display refresh period should be set in the frame tracker.
2358 FrameStats stats;
2359 mFlinger.getAnimFrameTracker().getStats(&stats);
2360 EXPECT_EQ(DEFAULT_REFRESH_RATE, stats.refreshPeriodNano);
2361
2362 // The display transaction needed flag should be set.
2363 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
2364
2365 // The compositor timing should be set to default values
2366 const auto& compositorTiming = mFlinger.getCompositorTiming();
2367 EXPECT_EQ(-DEFAULT_REFRESH_RATE, compositorTiming.deadline);
2368 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.interval);
2369 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.presentLatency);
2370}
2371
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002372/* ------------------------------------------------------------------------
2373 * SurfaceFlinger::setPowerModeInternal
2374 */
2375
2376// Used when we simulate a display that supports doze.
2377struct DozeIsSupportedVariant {
2378 static constexpr bool DOZE_SUPPORTED = true;
2379 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2380 IComposerClient::PowerMode::DOZE;
2381 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2382 IComposerClient::PowerMode::DOZE_SUSPEND;
2383};
2384
2385// Used when we simulate a display that does not support doze.
2386struct DozeNotSupportedVariant {
2387 static constexpr bool DOZE_SUPPORTED = false;
2388 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2389 IComposerClient::PowerMode::ON;
2390 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2391 IComposerClient::PowerMode::ON;
2392};
2393
2394struct EventThreadBaseSupportedVariant {
2395 static void setupEventAndEventControlThreadNoCallExpectations(DisplayTransactionTest* test) {
2396 // The event control thread should not be notified.
2397 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(_)).Times(0);
2398
2399 // The event thread should not be notified.
2400 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(0);
2401 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(0);
2402 }
2403};
2404
2405struct EventThreadNotSupportedVariant : public EventThreadBaseSupportedVariant {
2406 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2407 // These calls are only expected for the primary display.
2408
2409 // Instead expect no calls.
2410 setupEventAndEventControlThreadNoCallExpectations(test);
2411 }
2412
2413 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2414 // These calls are only expected for the primary display.
2415
2416 // Instead expect no calls.
2417 setupEventAndEventControlThreadNoCallExpectations(test);
2418 }
2419};
2420
2421struct EventThreadIsSupportedVariant : public EventThreadBaseSupportedVariant {
2422 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2423 // The event control thread should be notified to enable vsyncs
2424 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(true)).Times(1);
2425
2426 // The event thread should be notified that the screen was acquired.
2427 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(1);
2428 }
2429
2430 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2431 // There should be a call to setVsyncEnabled(false)
2432 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(false)).Times(1);
2433
2434 // The event thread should not be notified that the screen was released.
2435 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(1);
2436 }
2437};
2438
2439// --------------------------------------------------------------------
2440// Note:
2441//
2442// There are a large number of transitions we could test, however we only test a
2443// selected subset which provides complete test coverage of the implementation.
2444// --------------------------------------------------------------------
2445
2446template <int initialPowerMode, int targetPowerMode>
2447struct TransitionVariantCommon {
2448 static constexpr auto INITIAL_POWER_MODE = initialPowerMode;
2449 static constexpr auto TARGET_POWER_MODE = targetPowerMode;
2450
2451 static void verifyPostconditions(DisplayTransactionTest*) {}
2452};
2453
2454struct TransitionOffToOnVariant
2455 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_NORMAL> {
2456 template <typename Case>
2457 static void setupCallExpectations(DisplayTransactionTest* test) {
2458 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2459 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
2460 Case::setupRepaintEverythingCallExpectations(test);
2461 }
2462
2463 static void verifyPostconditions(DisplayTransactionTest* test) {
2464 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2465 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2466 }
2467};
2468
2469struct TransitionOffToDozeSuspendVariant
2470 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_DOZE_SUSPEND> {
2471 template <typename Case>
2472 static void setupCallExpectations(DisplayTransactionTest* test) {
2473 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2474 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2475 Case::setupRepaintEverythingCallExpectations(test);
2476 }
2477
2478 static void verifyPostconditions(DisplayTransactionTest* test) {
2479 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2480 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2481 }
2482};
2483
2484struct TransitionOnToOffVariant
2485 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_OFF> {
2486 template <typename Case>
2487 static void setupCallExpectations(DisplayTransactionTest* test) {
2488 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
2489 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2490 }
2491
2492 static void verifyPostconditions(DisplayTransactionTest* test) {
2493 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2494 }
2495};
2496
2497struct TransitionDozeSuspendToOffVariant
2498 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_OFF> {
2499 template <typename Case>
2500 static void setupCallExpectations(DisplayTransactionTest* test) {
2501 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2502 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2503 }
2504
2505 static void verifyPostconditions(DisplayTransactionTest* test) {
2506 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2507 }
2508};
2509
2510struct TransitionOnToDozeVariant
2511 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE> {
2512 template <typename Case>
2513 static void setupCallExpectations(DisplayTransactionTest* test) {
2514 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2515 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2516 }
2517};
2518
2519struct TransitionDozeSuspendToDozeVariant
2520 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_DOZE> {
2521 template <typename Case>
2522 static void setupCallExpectations(DisplayTransactionTest* test) {
2523 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
2524 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2525 }
2526};
2527
2528struct TransitionDozeToOnVariant
2529 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE, HWC_POWER_MODE_NORMAL> {
2530 template <typename Case>
2531 static void setupCallExpectations(DisplayTransactionTest* test) {
2532 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2533 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2534 }
2535};
2536
2537struct TransitionDozeSuspendToOnVariant
2538 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_NORMAL> {
2539 template <typename Case>
2540 static void setupCallExpectations(DisplayTransactionTest* test) {
2541 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
2542 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2543 }
2544};
2545
2546struct TransitionOnToDozeSuspendVariant
2547 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE_SUSPEND> {
2548 template <typename Case>
2549 static void setupCallExpectations(DisplayTransactionTest* test) {
2550 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
2551 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2552 }
2553};
2554
2555struct TransitionOnToUnknownVariant
2556 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_LEET> {
2557 template <typename Case>
2558 static void setupCallExpectations(DisplayTransactionTest* test) {
2559 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2560 Case::setupNoComposerPowerModeCallExpectations(test);
2561 }
2562};
2563
2564// --------------------------------------------------------------------
2565// Note:
2566//
2567// Rather than testing the cartesian product of of
2568// DozeIsSupported/DozeNotSupported with all other options, we use one for one
2569// display type, and the other for another display type.
2570// --------------------------------------------------------------------
2571
2572template <typename DisplayVariant, typename DozeVariant, typename EventThreadVariant,
2573 typename TransitionVariant>
2574struct DisplayPowerCase {
2575 using Display = DisplayVariant;
2576 using Doze = DozeVariant;
2577 using EventThread = EventThreadVariant;
2578 using Transition = TransitionVariant;
2579
2580 static auto injectDisplayWithInitialPowerMode(DisplayTransactionTest* test, int mode) {
2581 Display::injectHwcDisplay(test);
2582 auto display = Display::makeFakeExistingDisplayInjector(test);
2583 display.inject();
2584 display.mutableDisplayDevice()->setPowerMode(mode);
2585 return display;
2586 }
2587
2588 static void setInitialPrimaryHWVsyncEnabled(DisplayTransactionTest* test, bool enabled) {
2589 test->mFlinger.mutablePrimaryHWVsyncEnabled() = enabled;
2590 }
2591
2592 static void setupRepaintEverythingCallExpectations(DisplayTransactionTest* test) {
2593 EXPECT_CALL(*test->mMessageQueue, invalidate()).Times(1);
2594 }
2595
2596 static void setupSurfaceInterceptorCallExpectations(DisplayTransactionTest* test, int mode) {
2597 EXPECT_CALL(*test->mSurfaceInterceptor, isEnabled()).WillOnce(Return(true));
2598 EXPECT_CALL(*test->mSurfaceInterceptor, savePowerModeUpdate(_, mode)).Times(1);
2599 }
2600
2601 static void setupComposerCallExpectations(DisplayTransactionTest* test,
2602 IComposerClient::PowerMode mode) {
2603 // Any calls to get the active config will return a default value.
2604 EXPECT_CALL(*test->mComposer, getActiveConfig(Display::HWC_DISPLAY_ID, _))
2605 .WillRepeatedly(DoAll(SetArgPointee<1>(Display::HWC_ACTIVE_CONFIG_ID),
2606 Return(Error::NONE)));
2607
2608 // Any calls to get whether the display supports dozing will return the value set by the
2609 // policy variant.
2610 EXPECT_CALL(*test->mComposer, getDozeSupport(Display::HWC_DISPLAY_ID, _))
2611 .WillRepeatedly(DoAll(SetArgPointee<1>(Doze::DOZE_SUPPORTED), Return(Error::NONE)));
2612
2613 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, mode)).Times(1);
2614 }
2615
2616 static void setupNoComposerPowerModeCallExpectations(DisplayTransactionTest* test) {
2617 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, _)).Times(0);
2618 }
2619};
2620
2621// A sample configuration for the primary display.
2622// In addition to having event thread support, we emulate doze support.
2623template <typename TransitionVariant>
2624using PrimaryDisplayPowerCase = DisplayPowerCase<PrimaryDisplayVariant, DozeIsSupportedVariant,
2625 EventThreadIsSupportedVariant, TransitionVariant>;
2626
2627// A sample configuration for the external display.
2628// In addition to not having event thread support, we emulate not having doze
2629// support.
2630template <typename TransitionVariant>
2631using ExternalDisplayPowerCase =
2632 DisplayPowerCase<ExternalDisplayVariant, DozeNotSupportedVariant,
2633 EventThreadNotSupportedVariant, TransitionVariant>;
2634
2635class SetPowerModeInternalTest : public DisplayTransactionTest {
2636public:
2637 template <typename Case>
2638 void transitionDisplayCommon();
2639};
2640
2641template <int PowerMode>
2642struct PowerModeInitialVSyncEnabled : public std::false_type {};
2643
2644template <>
2645struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_NORMAL> : public std::true_type {};
2646
2647template <>
2648struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_DOZE> : public std::true_type {};
2649
2650template <typename Case>
2651void SetPowerModeInternalTest::transitionDisplayCommon() {
2652 // --------------------------------------------------------------------
2653 // Preconditions
2654
2655 auto display =
2656 Case::injectDisplayWithInitialPowerMode(this, Case::Transition::INITIAL_POWER_MODE);
2657 Case::setInitialPrimaryHWVsyncEnabled(this,
2658 PowerModeInitialVSyncEnabled<
2659 Case::Transition::INITIAL_POWER_MODE>::value);
2660
2661 // --------------------------------------------------------------------
2662 // Call Expectations
2663
2664 Case::setupSurfaceInterceptorCallExpectations(this, Case::Transition::TARGET_POWER_MODE);
2665 Case::Transition::template setupCallExpectations<Case>(this);
2666
2667 // --------------------------------------------------------------------
2668 // Invocation
2669
2670 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(),
2671 Case::Transition::TARGET_POWER_MODE);
2672
2673 // --------------------------------------------------------------------
2674 // Postconditions
2675
2676 Case::Transition::verifyPostconditions(this);
2677}
2678
2679TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfNoChange) {
2680 using Case = SimplePrimaryDisplayCase;
2681
2682 // --------------------------------------------------------------------
2683 // Preconditions
2684
2685 // A primary display device is set up
2686 Case::Display::injectHwcDisplay(this);
2687 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2688 display.inject();
2689
2690 // The diplay is already set to HWC_POWER_MODE_NORMAL
2691 display.mutableDisplayDevice()->setPowerMode(HWC_POWER_MODE_NORMAL);
2692
2693 // --------------------------------------------------------------------
2694 // Invocation
2695
2696 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_NORMAL);
2697
2698 // --------------------------------------------------------------------
2699 // Postconditions
2700
2701 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2702}
2703
2704TEST_F(SetPowerModeInternalTest, setPowerModeInternalJustSetsInternalStateIfVirtualDisplay) {
2705 using Case = HwcVirtualDisplayCase;
2706
2707 // --------------------------------------------------------------------
2708 // Preconditions
2709
2710 // We need to resize this so that the HWC thinks the virtual display
2711 // is something it created.
2712 mFlinger.mutableHwcDisplayData().resize(3);
2713
2714 // A virtual display device is set up
2715 Case::Display::injectHwcDisplay(this);
2716 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2717 display.inject();
2718
2719 // The display is set to HWC_POWER_MODE_OFF
2720 getDisplayDevice(display.token())->setPowerMode(HWC_POWER_MODE_OFF);
2721
2722 // --------------------------------------------------------------------
2723 // Invocation
2724
2725 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_NORMAL);
2726
2727 // --------------------------------------------------------------------
2728 // Postconditions
2729
2730 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2731}
2732
2733TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnPrimaryDisplay) {
2734 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToOnVariant>>();
2735}
2736
2737TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendPrimaryDisplay) {
2738 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
2739}
2740
2741TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffPrimaryDisplay) {
2742 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToOffVariant>>();
2743}
2744
2745TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffPrimaryDisplay) {
2746 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
2747}
2748
2749TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozePrimaryDisplay) {
2750 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeVariant>>();
2751}
2752
2753TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozePrimaryDisplay) {
2754 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
2755}
2756
2757TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnPrimaryDisplay) {
2758 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeToOnVariant>>();
2759}
2760
2761TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnPrimaryDisplay) {
2762 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
2763}
2764
2765TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendPrimaryDisplay) {
2766 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
2767}
2768
2769TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownPrimaryDisplay) {
2770 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToUnknownVariant>>();
2771}
2772
2773TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnExternalDisplay) {
2774 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToOnVariant>>();
2775}
2776
2777TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendExternalDisplay) {
2778 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
2779}
2780
2781TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffExternalDisplay) {
2782 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToOffVariant>>();
2783}
2784
2785TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffExternalDisplay) {
2786 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
2787}
2788
2789TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeExternalDisplay) {
2790 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeVariant>>();
2791}
2792
2793TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozeExternalDisplay) {
2794 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
2795}
2796
2797TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnExternalDisplay) {
2798 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeToOnVariant>>();
2799}
2800
2801TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnExternalDisplay) {
2802 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
2803}
2804
2805TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendExternalDisplay) {
2806 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
2807}
2808
2809TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownExternalDisplay) {
2810 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToUnknownVariant>>();
2811}
2812
Lloyd Piquef58625d2017-12-19 13:22:33 -08002813} // namespace
2814} // namespace android