blob: 7928cba10879de7185839730e6dc08ec2b8f8705 [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) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700210 return mFlinger.mutableDisplays().count(displayToken) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800211}
212
213sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700214 return mFlinger.mutableDisplays()[displayToken];
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800215}
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
Dominik Laskowski7e045462018-05-30 13:02:02 -0700237template <DisplayDevice::DisplayType type, DisplayDevice::DisplayType displayId, int width,
238 int height, Critical critical, Async async, Secure secure, int grallocUsage>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800239struct 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;
Dominik Laskowski7e045462018-05-30 13:02:02 -0700248 static constexpr DisplayDevice::DisplayType DISPLAY_ID = displayId;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800249
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) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700260 auto injector = FakeDisplayDeviceInjector(test->mFlinger, TYPE, DISPLAY_ID);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800261 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)));
Chia-I Wu614e1422018-05-23 02:17:03 -0700510 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800511 }
512};
513
514// For this variant, the display is not a HWC display, so no HDR support should
515// be configured.
516struct NonHwcDisplayHdrSupportVariant {
517 static constexpr bool HDR10_SUPPORTED = false;
518 static constexpr bool HDR_HLG_SUPPORTED = false;
519 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
520 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
521 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
522 }
523};
524
525// For this variant, the composer should respond with a non-empty list of HDR
526// modes containing HDR10, so HDR10 support should be configured.
527template <typename Display>
528struct Hdr10SupportedVariant {
529 static constexpr bool HDR10_SUPPORTED = true;
530 static constexpr bool HDR_HLG_SUPPORTED = false;
531 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
532 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
533 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
534 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HDR10})),
535 Return(Error::NONE)));
536 }
537};
538
539// For this variant, the composer should respond with a non-empty list of HDR
540// modes containing HLG, so HLG support should be configured.
541template <typename Display>
542struct HdrHlgSupportedVariant {
543 static constexpr bool HDR10_SUPPORTED = false;
544 static constexpr bool HDR_HLG_SUPPORTED = true;
545 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
546 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
547 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
548 .WillOnce(
549 DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HLG})), Return(Error::NONE)));
550 }
551};
552
553// For this variant, the composer should respond with a non-empty list of HDR
554// modes containing DOLBY_VISION, so DOLBY_VISION support should be configured.
555template <typename Display>
556struct HdrDolbyVisionSupportedVariant {
557 static constexpr bool HDR10_SUPPORTED = false;
558 static constexpr bool HDR_HLG_SUPPORTED = false;
559 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = true;
560 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
561 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
562 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::DOLBY_VISION})),
563 Return(Error::NONE)));
564 }
565};
566
567// For this variant, the composer should respond with am empty list of HDR
568// modes, so no HDR support should be configured.
569template <typename Display>
570struct HdrNotSupportedVariant {
571 static constexpr bool HDR10_SUPPORTED = false;
572 static constexpr bool HDR_HLG_SUPPORTED = false;
573 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
574 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
575 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
576 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
577 }
578};
579
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700580struct NonHwcPerFrameMetadataSupportVariant {
581 static constexpr int PER_FRAME_METADATA_KEYS = 0;
582 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
583 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(_, _)).Times(0);
584 }
585};
586
587template <typename Display>
588struct NoPerFrameMetadataSupportVariant {
589 static constexpr int PER_FRAME_METADATA_KEYS = 0;
590 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
591 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID, _))
592 .WillOnce(DoAll(SetArgPointee<1>(std::vector<PerFrameMetadataKey>()),
593 Return(Error::NONE)));
594 }
595};
596
597template <typename Display>
598struct Smpte2086PerFrameMetadataSupportVariant {
599 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::SMPTE2086;
600 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
601 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID, _))
602 .WillOnce(DoAll(SetArgPointee<1>(std::vector<PerFrameMetadataKey>({
603 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X,
604 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y,
605 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X,
606 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y,
607 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X,
608 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y,
609 PerFrameMetadataKey::WHITE_POINT_X,
610 PerFrameMetadataKey::WHITE_POINT_Y,
611 PerFrameMetadataKey::MAX_LUMINANCE,
612 PerFrameMetadataKey::MIN_LUMINANCE,
613 })),
614 Return(Error::NONE)));
615 }
616};
617
618template <typename Display>
619struct Cta861_3_PerFrameMetadataSupportVariant {
620 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::CTA861_3;
621 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
622 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID, _))
623 .WillOnce(DoAll(SetArgPointee<1>(std::vector<PerFrameMetadataKey>({
624 PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL,
625 PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL,
626 })),
627 Return(Error::NONE)));
628 }
629};
630
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800631/* ------------------------------------------------------------------------
632 * Typical display configurations to test
633 */
634
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700635template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy,
636 typename PerFrameMetadataSupportPolicy>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800637struct Case {
638 using Display = DisplayPolicy;
639 using WideColorSupport = WideColorSupportPolicy;
640 using HdrSupport = HdrSupportPolicy;
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700641 using PerFrameMetadataSupport = PerFrameMetadataSupportPolicy;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800642};
643
644using SimplePrimaryDisplayCase =
645 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700646 HdrNotSupportedVariant<PrimaryDisplayVariant>,
647 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800648using SimpleExternalDisplayCase =
649 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700650 HdrNotSupportedVariant<ExternalDisplayVariant>,
651 NoPerFrameMetadataSupportVariant<ExternalDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800652using SimpleTertiaryDisplayCase =
653 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700654 HdrNotSupportedVariant<TertiaryDisplayVariant>,
655 NoPerFrameMetadataSupportVariant<TertiaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800656using NonHwcVirtualDisplayCase =
657 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700658 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant,
659 NonHwcPerFrameMetadataSupportVariant>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800660using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
661using HwcVirtualDisplayCase =
662 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700663 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>,
664 NoPerFrameMetadataSupportVariant<SimpleHwcVirtualDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800665using WideColorP3ColorimetricDisplayCase =
666 Case<PrimaryDisplayVariant, WideColorP3ColorimetricSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700667 HdrNotSupportedVariant<PrimaryDisplayVariant>,
668 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800669using Hdr10DisplayCase =
670 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700671 Hdr10SupportedVariant<PrimaryDisplayVariant>,
672 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800673using HdrHlgDisplayCase =
674 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700675 HdrHlgSupportedVariant<PrimaryDisplayVariant>,
676 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800677using HdrDolbyVisionDisplayCase =
678 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700679 HdrDolbyVisionSupportedVariant<PrimaryDisplayVariant>,
680 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
681using HdrSmpte2086DisplayCase =
682 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
683 HdrNotSupportedVariant<PrimaryDisplayVariant>,
684 Smpte2086PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
685using HdrCta861_3_DisplayCase =
686 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
687 HdrNotSupportedVariant<PrimaryDisplayVariant>,
688 Cta861_3_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Pique9d9cf402018-02-16 17:47:13 -0800689using InvalidDisplayCase = Case<InvalidDisplayVariant, WideColorSupportNotConfiguredVariant,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700690 NonHwcDisplayHdrSupportVariant,
691 NoPerFrameMetadataSupportVariant<InvalidDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800692/* ------------------------------------------------------------------------
Lloyd Pique6cf11032018-01-22 18:57:44 -0800693 *
694 * SurfaceFlinger::onHotplugReceived
695 */
696
697TEST_F(DisplayTransactionTest, hotplugEnqueuesEventsForDisplayTransaction) {
698 constexpr int currentSequenceId = 123;
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700699 constexpr hwc2_display_t hwcDisplayId1 = 456;
700 constexpr hwc2_display_t hwcDisplayId2 = 654;
Lloyd Pique6cf11032018-01-22 18:57:44 -0800701
702 // --------------------------------------------------------------------
703 // Preconditions
704
705 // Set the current sequence id for accepted events
706 mFlinger.mutableComposerSequenceId() = currentSequenceId;
707
708 // Set the main thread id so that the current thread does not appear to be
709 // the main thread.
710 mFlinger.mutableMainThreadId() = std::thread::id();
711
712 // --------------------------------------------------------------------
713 // Call Expectations
714
715 // We expect invalidate() to be invoked once to trigger display transaction
716 // processing.
717 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
718
719 // --------------------------------------------------------------------
720 // Invocation
721
722 // Simulate two hotplug events (a connect and a disconnect)
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700723 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId1, HWC2::Connection::Connected);
724 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId2, HWC2::Connection::Disconnected);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800725
726 // --------------------------------------------------------------------
727 // Postconditions
728
729 // The display transaction needed flag should be set.
730 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
731
732 // All events should be in the pending event queue.
733 const auto& pendingEvents = mFlinger.mutablePendingHotplugEvents();
734 ASSERT_EQ(2u, pendingEvents.size());
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700735 EXPECT_EQ(hwcDisplayId1, pendingEvents[0].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800736 EXPECT_EQ(HWC2::Connection::Connected, pendingEvents[0].connection);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700737 EXPECT_EQ(hwcDisplayId2, pendingEvents[1].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800738 EXPECT_EQ(HWC2::Connection::Disconnected, pendingEvents[1].connection);
739}
740
741TEST_F(DisplayTransactionTest, hotplugDiscardsUnexpectedEvents) {
742 constexpr int currentSequenceId = 123;
743 constexpr int otherSequenceId = 321;
744 constexpr hwc2_display_t displayId = 456;
745
746 // --------------------------------------------------------------------
747 // Preconditions
748
749 // Set the current sequence id for accepted events
750 mFlinger.mutableComposerSequenceId() = currentSequenceId;
751
752 // Set the main thread id so that the current thread does not appear to be
753 // the main thread.
754 mFlinger.mutableMainThreadId() = std::thread::id();
755
756 // --------------------------------------------------------------------
757 // Call Expectations
758
759 // We do not expect any calls to invalidate().
760 EXPECT_CALL(*mMessageQueue, invalidate()).Times(0);
761
762 // --------------------------------------------------------------------
763 // Invocation
764
765 // Call with an unexpected sequence id
766 mFlinger.onHotplugReceived(otherSequenceId, displayId, HWC2::Connection::Invalid);
767
768 // --------------------------------------------------------------------
769 // Postconditions
770
771 // The display transaction needed flag should not be set
772 EXPECT_FALSE(hasTransactionFlagSet(eDisplayTransactionNeeded));
773
774 // There should be no pending events
775 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
776}
777
778TEST_F(DisplayTransactionTest, hotplugProcessesEnqueuedEventsIfCalledOnMainThread) {
779 constexpr int currentSequenceId = 123;
780 constexpr hwc2_display_t displayId1 = 456;
781
782 // --------------------------------------------------------------------
783 // Note:
784 // --------------------------------------------------------------------
785 // This test case is a bit tricky. We want to verify that
786 // onHotplugReceived() calls processDisplayHotplugEventsLocked(), but we
787 // don't really want to provide coverage for everything the later function
788 // does as there are specific tests for it.
789 // --------------------------------------------------------------------
790
791 // --------------------------------------------------------------------
792 // Preconditions
793
794 // Set the current sequence id for accepted events
795 mFlinger.mutableComposerSequenceId() = currentSequenceId;
796
797 // Set the main thread id so that the current thread does appear to be the
798 // main thread.
799 mFlinger.mutableMainThreadId() = std::this_thread::get_id();
800
801 // --------------------------------------------------------------------
802 // Call Expectations
803
804 // We expect invalidate() to be invoked once to trigger display transaction
805 // processing.
806 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
807
808 // --------------------------------------------------------------------
809 // Invocation
810
811 // Simulate a disconnect on a display id that is not connected. This should
812 // be enqueued by onHotplugReceived(), and dequeued by
813 // processDisplayHotplugEventsLocked(), but then ignored as invalid.
814 mFlinger.onHotplugReceived(currentSequenceId, displayId1, HWC2::Connection::Disconnected);
815
816 // --------------------------------------------------------------------
817 // Postconditions
818
819 // The display transaction needed flag should be set.
820 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
821
822 // There should be no event queued on return, as it should have been
823 // processed.
824 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
825}
826
827/* ------------------------------------------------------------------------
Lloyd Piquea482f992018-01-22 19:00:34 -0800828 * SurfaceFlinger::createDisplay
829 */
830
831TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForNonsecureDisplay) {
832 const String8 name("virtual.test");
833
834 // --------------------------------------------------------------------
835 // Call Expectations
836
837 // The call should notify the interceptor that a display was created.
838 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
839
840 // --------------------------------------------------------------------
841 // Invocation
842
843 sp<IBinder> displayToken = mFlinger.createDisplay(name, false);
844
845 // --------------------------------------------------------------------
846 // Postconditions
847
848 // The display should have been added to the current state
849 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
850 const auto& display = getCurrentDisplayState(displayToken);
851 EXPECT_EQ(DisplayDevice::DISPLAY_VIRTUAL, display.type);
852 EXPECT_EQ(false, display.isSecure);
853 EXPECT_EQ(name.string(), display.displayName);
854
855 // --------------------------------------------------------------------
856 // Cleanup conditions
857
858 // Destroying the display invalidates the display state.
859 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
860}
861
862TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForSecureDisplay) {
863 const String8 name("virtual.test");
864
865 // --------------------------------------------------------------------
866 // Call Expectations
867
868 // The call should notify the interceptor that a display was created.
869 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
870
871 // --------------------------------------------------------------------
872 // Invocation
873
874 sp<IBinder> displayToken = mFlinger.createDisplay(name, true);
875
876 // --------------------------------------------------------------------
877 // Postconditions
878
879 // The display should have been added to the current state
880 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
881 const auto& display = getCurrentDisplayState(displayToken);
882 EXPECT_EQ(DisplayDevice::DISPLAY_VIRTUAL, display.type);
883 EXPECT_EQ(true, display.isSecure);
884 EXPECT_EQ(name.string(), display.displayName);
885
886 // --------------------------------------------------------------------
887 // Cleanup conditions
888
889 // Destroying the display invalidates the display state.
890 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
891}
892
893/* ------------------------------------------------------------------------
894 * SurfaceFlinger::destroyDisplay
895 */
896
897TEST_F(DisplayTransactionTest, destroyDisplayClearsCurrentStateForDisplay) {
898 using Case = NonHwcVirtualDisplayCase;
899
900 // --------------------------------------------------------------------
901 // Preconditions
902
903 // A virtual display exists
904 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
905 existing.inject();
906
907 // --------------------------------------------------------------------
908 // Call Expectations
909
910 // The call should notify the interceptor that a display was created.
911 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
912
913 // Destroying the display invalidates the display state.
914 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
915
916 // --------------------------------------------------------------------
917 // Invocation
918
919 mFlinger.destroyDisplay(existing.token());
920
921 // --------------------------------------------------------------------
922 // Postconditions
923
924 // The display should have been removed from the current state
925 EXPECT_FALSE(hasCurrentDisplayState(existing.token()));
926
927 // Ths display should still exist in the drawing state
928 EXPECT_TRUE(hasDrawingDisplayState(existing.token()));
929
930 // The display transaction needed flasg should be set
931 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
932}
933
934TEST_F(DisplayTransactionTest, destroyDisplayHandlesUnknownDisplay) {
935 // --------------------------------------------------------------------
936 // Preconditions
937
938 sp<BBinder> displayToken = new BBinder();
939
940 // --------------------------------------------------------------------
941 // Invocation
942
943 mFlinger.destroyDisplay(displayToken);
944}
945
946/* ------------------------------------------------------------------------
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -0800947 * SurfaceFlinger::resetDisplayState
948 */
949
950TEST_F(DisplayTransactionTest, resetDisplayStateClearsState) {
951 using Case = NonHwcVirtualDisplayCase;
952
953 // --------------------------------------------------------------------
954 // Preconditions
955
956 // vsync is enabled and available
957 mFlinger.mutablePrimaryHWVsyncEnabled() = true;
958 mFlinger.mutableHWVsyncAvailable() = true;
959
960 // A display exists
961 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
962 existing.inject();
963
964 // --------------------------------------------------------------------
965 // Call Expectations
966
967 // The call disable vsyncs
968 EXPECT_CALL(*mEventControlThread, setVsyncEnabled(false)).Times(1);
969
970 // The call clears the current render engine surface
971 EXPECT_CALL(*mRenderEngine, resetCurrentSurface());
972
973 // --------------------------------------------------------------------
974 // Invocation
975
976 mFlinger.resetDisplayState();
977
978 // --------------------------------------------------------------------
979 // Postconditions
980
981 // vsyncs should be off and not available.
982 EXPECT_FALSE(mFlinger.mutablePrimaryHWVsyncEnabled());
983 EXPECT_FALSE(mFlinger.mutableHWVsyncAvailable());
984
985 // The display should have been removed from the display map.
986 EXPECT_FALSE(hasDisplayDevice(existing.token()));
987
988 // The display should still exist in the current state
989 EXPECT_TRUE(hasCurrentDisplayState(existing.token()));
990
991 // The display should have been removed from the drawing state
992 EXPECT_FALSE(hasDrawingDisplayState(existing.token()));
993}
994
995/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800996 * SurfaceFlinger::setupNewDisplayDeviceInternal
997 */
998
999class SetupNewDisplayDeviceInternalTest : public DisplayTransactionTest {
1000public:
1001 template <typename T>
1002 void setupNewDisplayDeviceInternalTest();
1003};
1004
1005template <typename Case>
1006void SetupNewDisplayDeviceInternalTest::setupNewDisplayDeviceInternalTest() {
1007 const sp<BBinder> displayToken = new BBinder();
1008 const sp<mock::DisplaySurface> displaySurface = new mock::DisplaySurface();
1009 const sp<mock::GraphicBufferProducer> producer = new mock::GraphicBufferProducer();
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001010
1011 // --------------------------------------------------------------------
1012 // Preconditions
1013
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001014 // Wide color displays support is configured appropriately
1015 Case::WideColorSupport::injectConfigChange(this);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001016
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001017 // The display is setup with the HWC.
1018 Case::Display::injectHwcDisplay(this);
1019
1020 // SurfaceFlinger will use a test-controlled factory for native window
1021 // surfaces.
1022 injectFakeNativeWindowSurfaceFactory();
1023
1024 // --------------------------------------------------------------------
1025 // Call Expectations
1026
1027 // Various native window calls will be made.
1028 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
Lloyd Pique3c085a02018-05-09 19:38:32 -07001029 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001030 Case::WideColorSupport::setupComposerCallExpectations(this);
1031 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001032 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001033
1034 // --------------------------------------------------------------------
1035 // Invocation
1036
1037 DisplayDeviceState state;
1038 state.type = Case::Display::TYPE;
1039 state.isSecure = static_cast<bool>(Case::Display::SECURE);
1040
1041 auto device = mFlinger.setupNewDisplayDeviceInternal(displayToken, Case::Display::TYPE, state,
1042 displaySurface, producer);
1043
1044 // --------------------------------------------------------------------
1045 // Postconditions
1046
1047 ASSERT_TRUE(device != nullptr);
1048 EXPECT_EQ(Case::Display::TYPE, device->getDisplayType());
1049 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
1050 EXPECT_EQ(Case::Display::WIDTH, device->getWidth());
1051 EXPECT_EQ(Case::Display::HEIGHT, device->getHeight());
1052 EXPECT_EQ(Case::WideColorSupport::WIDE_COLOR_SUPPORTED, device->hasWideColorGamut());
1053 EXPECT_EQ(Case::HdrSupport::HDR10_SUPPORTED, device->hasHDR10Support());
1054 EXPECT_EQ(Case::HdrSupport::HDR_HLG_SUPPORTED, device->hasHLGSupport());
1055 EXPECT_EQ(Case::HdrSupport::HDR_DOLBY_VISION_SUPPORTED, device->hasDolbyVisionSupport());
Lloyd Pique3c085a02018-05-09 19:38:32 -07001056 // Note: This is not Case::Display::HWC_ACTIVE_CONFIG_ID as the ids are
1057 // remapped, and the test only ever sets up one config. If there were an error
1058 // looking up the remapped index, device->getActiveConfig() would be -1 instead.
1059 EXPECT_EQ(0, device->getActiveConfig());
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001060 EXPECT_EQ(Case::PerFrameMetadataSupport::PER_FRAME_METADATA_KEYS,
1061 device->getSupportedPerFrameMetadata());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001062}
1063
1064TEST_F(SetupNewDisplayDeviceInternalTest, createSimplePrimaryDisplay) {
1065 setupNewDisplayDeviceInternalTest<SimplePrimaryDisplayCase>();
1066}
1067
1068TEST_F(SetupNewDisplayDeviceInternalTest, createSimpleExternalDisplay) {
1069 setupNewDisplayDeviceInternalTest<SimpleExternalDisplayCase>();
1070}
1071
1072TEST_F(SetupNewDisplayDeviceInternalTest, createNonHwcVirtualDisplay) {
1073 setupNewDisplayDeviceInternalTest<NonHwcVirtualDisplayCase>();
1074}
1075
1076TEST_F(SetupNewDisplayDeviceInternalTest, createHwcVirtualDisplay) {
1077 // We need to resize this so that the HWC thinks the virtual display
1078 // is something it created.
1079 mFlinger.mutableHwcDisplayData().resize(3);
1080
1081 setupNewDisplayDeviceInternalTest<HwcVirtualDisplayCase>();
1082}
1083
1084TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3Display) {
1085 setupNewDisplayDeviceInternalTest<WideColorP3ColorimetricDisplayCase>();
1086}
1087
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001088TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10Display) {
1089 setupNewDisplayDeviceInternalTest<Hdr10DisplayCase>();
1090}
1091
1092TEST_F(SetupNewDisplayDeviceInternalTest, createHdrHlgDisplay) {
1093 setupNewDisplayDeviceInternalTest<HdrHlgDisplayCase>();
1094}
1095
1096TEST_F(SetupNewDisplayDeviceInternalTest, createHdrDolbyVisionDisplay) {
1097 setupNewDisplayDeviceInternalTest<HdrDolbyVisionDisplayCase>();
1098}
1099
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001100TEST_F(SetupNewDisplayDeviceInternalTest, createHdrSmpte2086DisplayCase) {
1101 setupNewDisplayDeviceInternalTest<HdrSmpte2086DisplayCase>();
1102}
1103
1104TEST_F(SetupNewDisplayDeviceInternalTest, createHdrCta816_3_DisplayCase) {
1105 setupNewDisplayDeviceInternalTest<HdrCta861_3_DisplayCase>();
1106}
1107
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001108/* ------------------------------------------------------------------------
1109 * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded)
1110 */
1111
1112class HandleTransactionLockedTest : public DisplayTransactionTest {
1113public:
1114 template <typename Case>
1115 void setupCommonPreconditions();
1116
1117 template <typename Case>
1118 void setupCommonCallExpectationsForConnectProcessing();
1119
1120 template <typename Case>
1121 void setupCommonCallExpectationsForDisconnectProcessing();
1122
1123 template <typename Case>
1124 void processesHotplugConnectCommon();
1125
1126 template <typename Case>
1127 void ignoresHotplugConnectCommon();
1128
1129 template <typename Case>
1130 void processesHotplugDisconnectCommon();
1131
1132 template <typename Case>
1133 void verifyDisplayIsConnected(const sp<IBinder>& displayToken);
1134
1135 template <typename Case>
1136 void verifyPhysicalDisplayIsConnected();
1137
1138 void verifyDisplayIsNotConnected(const sp<IBinder>& displayToken);
1139};
1140
1141template <typename Case>
1142void HandleTransactionLockedTest::setupCommonPreconditions() {
1143 // Wide color displays support is configured appropriately
1144 Case::WideColorSupport::injectConfigChange(this);
1145
1146 // SurfaceFlinger will use a test-controlled factory for BufferQueues
1147 injectFakeBufferQueueFactory();
1148
1149 // SurfaceFlinger will use a test-controlled factory for native window
1150 // surfaces.
1151 injectFakeNativeWindowSurfaceFactory();
1152}
1153
1154template <typename Case>
1155void HandleTransactionLockedTest::setupCommonCallExpectationsForConnectProcessing() {
1156 Case::Display::setupHwcHotplugCallExpectations(this);
1157
1158 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1159 Case::Display::setupFramebufferProducerBufferQueueCallExpectations(this);
1160 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1161 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
1162
1163 Case::WideColorSupport::setupComposerCallExpectations(this);
1164 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001165 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001166
1167 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
1168 EXPECT_CALL(*mEventThread, onHotplugReceived(Case::Display::TYPE, true)).Times(1);
1169}
1170
1171template <typename Case>
1172void HandleTransactionLockedTest::setupCommonCallExpectationsForDisconnectProcessing() {
1173 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
1174 EXPECT_CALL(*mEventThread, onHotplugReceived(Case::Display::TYPE, false)).Times(1);
1175}
1176
1177template <typename Case>
1178void HandleTransactionLockedTest::verifyDisplayIsConnected(const sp<IBinder>& displayToken) {
1179 // The display device should have been set up in the list of displays.
1180 ASSERT_TRUE(hasDisplayDevice(displayToken));
1181 const auto& device = getDisplayDevice(displayToken);
1182 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
1183 EXPECT_EQ(Case::Display::TYPE == DisplayDevice::DISPLAY_PRIMARY, device->isPrimary());
1184
1185 // The display should have been set up in the current display state
1186 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1187 const auto& current = getCurrentDisplayState(displayToken);
1188 EXPECT_EQ(Case::Display::TYPE, current.type);
1189
1190 // The display should have been set up in the drawing display state
1191 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1192 const auto& draw = getDrawingDisplayState(displayToken);
1193 EXPECT_EQ(Case::Display::TYPE, draw.type);
1194}
1195
1196template <typename Case>
1197void HandleTransactionLockedTest::verifyPhysicalDisplayIsConnected() {
1198 // HWComposer should have an entry for the display
1199 EXPECT_TRUE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1200
1201 // The display should be set up as a built-in display.
1202 static_assert(0 <= Case::Display::TYPE &&
1203 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES,
1204 "Must use a valid physical display type index for the fixed-size array");
Dominik Laskowskieecd6592018-05-29 10:25:41 -07001205 auto& displayToken = mFlinger.mutableDisplayTokens()[Case::Display::TYPE];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001206 ASSERT_TRUE(displayToken != nullptr);
1207
1208 verifyDisplayIsConnected<Case>(displayToken);
1209}
1210
1211void HandleTransactionLockedTest::verifyDisplayIsNotConnected(const sp<IBinder>& displayToken) {
1212 EXPECT_FALSE(hasDisplayDevice(displayToken));
1213 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1214 EXPECT_FALSE(hasDrawingDisplayState(displayToken));
1215}
1216
1217template <typename Case>
1218void HandleTransactionLockedTest::processesHotplugConnectCommon() {
1219 // --------------------------------------------------------------------
1220 // Preconditions
1221
1222 setupCommonPreconditions<Case>();
1223
1224 // A hotplug connect event is enqueued for a display
1225 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001226
1227 // --------------------------------------------------------------------
1228 // Call Expectations
1229
1230 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillOnce(Return(false));
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001231
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001232 setupCommonCallExpectationsForConnectProcessing<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001233
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001234 // --------------------------------------------------------------------
1235 // Invocation
Lloyd Piquee39cad22017-12-20 17:01:29 -08001236
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001237 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
Lloyd Piquee39cad22017-12-20 17:01:29 -08001238
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001239 // --------------------------------------------------------------------
1240 // Postconditions
1241
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001242 verifyPhysicalDisplayIsConnected<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001243
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001244 // --------------------------------------------------------------------
1245 // Cleanup conditions
1246
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001247 EXPECT_CALL(*mComposer,
1248 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001249 .WillOnce(Return(Error::NONE));
1250 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Piquef58625d2017-12-19 13:22:33 -08001251}
1252
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001253template <typename Case>
1254void HandleTransactionLockedTest::ignoresHotplugConnectCommon() {
1255 // --------------------------------------------------------------------
1256 // Preconditions
1257
1258 setupCommonPreconditions<Case>();
1259
1260 // A hotplug connect event is enqueued for a display
1261 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1262
1263 // --------------------------------------------------------------------
1264 // Invocation
1265
1266 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1267
1268 // --------------------------------------------------------------------
1269 // Postconditions
1270
1271 // HWComposer should not have an entry for the display
1272 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1273}
1274
1275template <typename Case>
1276void HandleTransactionLockedTest::processesHotplugDisconnectCommon() {
1277 // --------------------------------------------------------------------
1278 // Preconditions
1279
1280 setupCommonPreconditions<Case>();
1281
1282 // A hotplug disconnect event is enqueued for a display
1283 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1284
1285 // The display is already completely set up.
1286 Case::Display::injectHwcDisplay(this);
1287 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1288 existing.inject();
1289
1290 // --------------------------------------------------------------------
1291 // Call Expectations
1292
1293 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1294
1295 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1296
1297 // --------------------------------------------------------------------
1298 // Invocation
1299
1300 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1301
1302 // --------------------------------------------------------------------
1303 // Postconditions
1304
1305 // HWComposer should not have an entry for the display
1306 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1307
1308 // The display should not be set up as a built-in display.
1309 ASSERT_TRUE(0 <= Case::Display::TYPE &&
1310 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES);
Dominik Laskowskieecd6592018-05-29 10:25:41 -07001311 auto displayToken = mFlinger.mutableDisplayTokens()[Case::Display::TYPE];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001312 EXPECT_TRUE(displayToken == nullptr);
1313
1314 // The existing token should have been removed
1315 verifyDisplayIsNotConnected(existing.token());
1316}
1317
1318TEST_F(HandleTransactionLockedTest, processesHotplugConnectPrimaryDisplay) {
1319 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1320}
1321
1322TEST_F(HandleTransactionLockedTest,
1323 processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected) {
1324 // Inject an external display.
1325 ExternalDisplayVariant::injectHwcDisplay(this);
1326
1327 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1328}
1329
1330TEST_F(HandleTransactionLockedTest, processesHotplugConnectExternalDisplay) {
1331 // Inject a primary display.
1332 PrimaryDisplayVariant::injectHwcDisplay(this);
1333
1334 processesHotplugConnectCommon<SimpleExternalDisplayCase>();
1335}
1336
1337TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected) {
1338 // Inject both a primary and external display.
1339 PrimaryDisplayVariant::injectHwcDisplay(this);
1340 ExternalDisplayVariant::injectHwcDisplay(this);
1341
1342 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1343
1344 ignoresHotplugConnectCommon<SimpleTertiaryDisplayCase>();
1345}
1346
1347TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfExternalForVrComposer) {
1348 // Inject a primary display.
1349 PrimaryDisplayVariant::injectHwcDisplay(this);
1350
1351 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(true));
1352
1353 ignoresHotplugConnectCommon<SimpleExternalDisplayCase>();
1354}
1355
1356TEST_F(HandleTransactionLockedTest, processHotplugDisconnectPrimaryDisplay) {
1357 processesHotplugDisconnectCommon<SimplePrimaryDisplayCase>();
1358}
1359
1360TEST_F(HandleTransactionLockedTest, processHotplugDisconnectExternalDisplay) {
1361 processesHotplugDisconnectCommon<SimpleExternalDisplayCase>();
1362}
1363
1364TEST_F(HandleTransactionLockedTest, processesHotplugConnectThenDisconnectPrimary) {
1365 using Case = SimplePrimaryDisplayCase;
1366
1367 // --------------------------------------------------------------------
1368 // Preconditions
1369
1370 setupCommonPreconditions<Case>();
1371
1372 // A hotplug connect event is enqueued for a display
1373 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1374 // A hotplug disconnect event is also enqueued for the same display
1375 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1376
1377 // --------------------------------------------------------------------
1378 // Call Expectations
1379
1380 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1381
1382 setupCommonCallExpectationsForConnectProcessing<Case>();
1383 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1384
1385 EXPECT_CALL(*mComposer,
1386 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1387 .WillOnce(Return(Error::NONE));
1388 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1389
1390 // --------------------------------------------------------------------
1391 // Invocation
1392
1393 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1394
1395 // --------------------------------------------------------------------
1396 // Postconditions
1397
1398 // HWComposer should not have an entry for the display
1399 EXPECT_FALSE(hasHwcDisplay(Case::Display::HWC_DISPLAY_ID));
1400
1401 // The display should not be set up as a primary built-in display.
1402 ASSERT_TRUE(0 <= Case::Display::TYPE &&
1403 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES);
Dominik Laskowskieecd6592018-05-29 10:25:41 -07001404 auto displayToken = mFlinger.mutableDisplayTokens()[Case::Display::TYPE];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001405 EXPECT_TRUE(displayToken == nullptr);
1406}
1407
1408TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectThenConnectPrimary) {
1409 using Case = SimplePrimaryDisplayCase;
1410
1411 // --------------------------------------------------------------------
1412 // Preconditions
1413
1414 setupCommonPreconditions<Case>();
1415
1416 // The display is already completely set up.
1417 Case::Display::injectHwcDisplay(this);
1418 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1419 existing.inject();
1420
1421 // A hotplug disconnect event is enqueued for a display
1422 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1423 // A hotplug connect event is also enqueued for the same display
1424 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1425
1426 // --------------------------------------------------------------------
1427 // Call Expectations
1428
1429 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1430
1431 setupCommonCallExpectationsForConnectProcessing<Case>();
1432 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1433
1434 // --------------------------------------------------------------------
1435 // Invocation
1436
1437 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1438
1439 // --------------------------------------------------------------------
1440 // Postconditions
1441
1442 // The existing token should have been removed
1443 verifyDisplayIsNotConnected(existing.token());
1444 static_assert(0 <= Case::Display::TYPE &&
1445 Case::Display::TYPE < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES,
1446 "Display type must be a built-in display");
Dominik Laskowskieecd6592018-05-29 10:25:41 -07001447 EXPECT_NE(existing.token(), mFlinger.mutableDisplayTokens()[Case::Display::TYPE]);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001448
1449 // A new display should be connected in its place
1450
1451 verifyPhysicalDisplayIsConnected<Case>();
1452
1453 // --------------------------------------------------------------------
1454 // Cleanup conditions
1455
1456 EXPECT_CALL(*mComposer,
1457 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1458 .WillOnce(Return(Error::NONE));
1459 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1460}
1461
1462TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAdded) {
1463 using Case = HwcVirtualDisplayCase;
1464
1465 // --------------------------------------------------------------------
1466 // Preconditions
1467
1468 // The HWC supports at least one virtual display
1469 injectMockComposer(1);
1470
1471 setupCommonPreconditions<Case>();
1472
1473 // A virtual display was added to the current state, and it has a
1474 // surface(producer)
1475 sp<BBinder> displayToken = new BBinder();
Lloyd Pique4c2ac022018-04-27 12:08:03 -07001476
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001477 DisplayDeviceState info;
1478 info.type = Case::Display::TYPE;
1479 info.isSecure = static_cast<bool>(Case::Display::SECURE);
1480
1481 sp<mock::GraphicBufferProducer> surface{new mock::GraphicBufferProducer()};
1482 info.surface = surface;
1483 mFlinger.mutableCurrentState().displays.add(displayToken, info);
1484
1485 // --------------------------------------------------------------------
1486 // Call Expectations
1487
1488 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1489 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1490
1491 EXPECT_CALL(*surface, query(NATIVE_WINDOW_WIDTH, _))
1492 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::WIDTH), Return(NO_ERROR)));
1493 EXPECT_CALL(*surface, query(NATIVE_WINDOW_HEIGHT, _))
1494 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::HEIGHT), Return(NO_ERROR)));
1495 EXPECT_CALL(*surface, query(NATIVE_WINDOW_FORMAT, _))
1496 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT),
1497 Return(NO_ERROR)));
1498 EXPECT_CALL(*surface, query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, _))
1499 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(NO_ERROR)));
1500
1501 EXPECT_CALL(*surface, setAsyncMode(true)).Times(1);
1502
1503 EXPECT_CALL(*mProducer, connect(_, _, _, _)).Times(1);
1504 EXPECT_CALL(*mProducer, disconnect(_, _)).Times(1);
1505
1506 Case::Display::setupHwcVirtualDisplayCreationCallExpectations(this);
1507 Case::WideColorSupport::setupComposerCallExpectations(this);
1508 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001509 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001510
1511 // --------------------------------------------------------------------
1512 // Invocation
1513
1514 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1515
1516 // --------------------------------------------------------------------
1517 // Postconditions
1518
1519 // The display device should have been set up in the list of displays.
1520 verifyDisplayIsConnected<Case>(displayToken);
1521
1522 // --------------------------------------------------------------------
1523 // Cleanup conditions
1524
1525 EXPECT_CALL(*mComposer, destroyVirtualDisplay(Case::Display::HWC_DISPLAY_ID))
1526 .WillOnce(Return(Error::NONE));
1527 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1528}
1529
1530TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAddedWithNoSurface) {
1531 using Case = HwcVirtualDisplayCase;
1532
1533 // --------------------------------------------------------------------
1534 // Preconditions
1535
1536 // The HWC supports at least one virtual display
1537 injectMockComposer(1);
1538
1539 setupCommonPreconditions<Case>();
1540
1541 // A virtual display was added to the current state, but it does not have a
1542 // surface.
1543 sp<BBinder> displayToken = new BBinder();
1544
1545 DisplayDeviceState info;
1546 info.type = Case::Display::TYPE;
1547 info.isSecure = static_cast<bool>(Case::Display::SECURE);
1548
1549 mFlinger.mutableCurrentState().displays.add(displayToken, info);
1550
1551 // --------------------------------------------------------------------
1552 // Call Expectations
1553
1554 // --------------------------------------------------------------------
1555 // Invocation
1556
1557 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1558
1559 // --------------------------------------------------------------------
1560 // Postconditions
1561
1562 // There will not be a display device set up.
1563 EXPECT_FALSE(hasDisplayDevice(displayToken));
1564
1565 // The drawing display state will be set from the current display state.
1566 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1567 const auto& draw = getDrawingDisplayState(displayToken);
1568 EXPECT_EQ(Case::Display::TYPE, draw.type);
1569}
1570
1571TEST_F(HandleTransactionLockedTest, processesVirtualDisplayRemoval) {
1572 using Case = HwcVirtualDisplayCase;
1573
1574 // --------------------------------------------------------------------
1575 // Preconditions
1576
1577 // A virtual display is set up but is removed from the current state.
1578 mFlinger.mutableHwcDisplayData().resize(3);
1579 Case::Display::injectHwcDisplay(this);
1580 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1581 existing.inject();
1582 mFlinger.mutableCurrentState().displays.removeItem(existing.token());
1583
1584 // --------------------------------------------------------------------
1585 // Call Expectations
1586
1587 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1588
1589 // --------------------------------------------------------------------
1590 // Invocation
1591
1592 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1593
1594 // --------------------------------------------------------------------
1595 // Postconditions
1596
1597 // The existing token should have been removed
1598 verifyDisplayIsNotConnected(existing.token());
1599}
1600
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001601TEST_F(HandleTransactionLockedTest, processesDisplayLayerStackChanges) {
1602 using Case = NonHwcVirtualDisplayCase;
1603
1604 constexpr uint32_t oldLayerStack = 0u;
1605 constexpr uint32_t newLayerStack = 123u;
1606
1607 // --------------------------------------------------------------------
1608 // Preconditions
1609
1610 // A display is set up
1611 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1612 display.inject();
1613
1614 // There is a change to the layerStack state
1615 display.mutableDrawingDisplayState().layerStack = oldLayerStack;
1616 display.mutableCurrentDisplayState().layerStack = newLayerStack;
1617
1618 // --------------------------------------------------------------------
1619 // Invocation
1620
1621 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1622
1623 // --------------------------------------------------------------------
1624 // Postconditions
1625
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001626 EXPECT_EQ(newLayerStack, display.mutableDisplayDevice()->getLayerStack());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001627}
1628
1629TEST_F(HandleTransactionLockedTest, processesDisplayTransformChanges) {
1630 using Case = NonHwcVirtualDisplayCase;
1631
1632 constexpr int oldTransform = 0;
1633 constexpr int newTransform = 2;
1634
1635 // --------------------------------------------------------------------
1636 // Preconditions
1637
1638 // A display is set up
1639 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1640 display.inject();
1641
1642 // There is a change to the orientation state
1643 display.mutableDrawingDisplayState().orientation = oldTransform;
1644 display.mutableCurrentDisplayState().orientation = newTransform;
1645
1646 // --------------------------------------------------------------------
1647 // Invocation
1648
1649 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1650
1651 // --------------------------------------------------------------------
1652 // Postconditions
1653
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001654 EXPECT_EQ(newTransform, display.mutableDisplayDevice()->getOrientation());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001655}
1656
1657TEST_F(HandleTransactionLockedTest, processesDisplayViewportChanges) {
1658 using Case = NonHwcVirtualDisplayCase;
1659
1660 const Rect oldViewport(0, 0, 0, 0);
1661 const Rect newViewport(0, 0, 123, 456);
1662
1663 // --------------------------------------------------------------------
1664 // Preconditions
1665
1666 // A display is set up
1667 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1668 display.inject();
1669
1670 // There is a change to the viewport state
1671 display.mutableDrawingDisplayState().viewport = oldViewport;
1672 display.mutableCurrentDisplayState().viewport = newViewport;
1673
1674 // --------------------------------------------------------------------
1675 // Invocation
1676
1677 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1678
1679 // --------------------------------------------------------------------
1680 // Postconditions
1681
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001682 EXPECT_EQ(newViewport, display.mutableDisplayDevice()->getViewport());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001683}
1684
1685TEST_F(HandleTransactionLockedTest, processesDisplayFrameChanges) {
1686 using Case = NonHwcVirtualDisplayCase;
1687
1688 const Rect oldFrame(0, 0, 0, 0);
1689 const Rect newFrame(0, 0, 123, 456);
1690
1691 // --------------------------------------------------------------------
1692 // Preconditions
1693
1694 // A display is set up
1695 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1696 display.inject();
1697
1698 // There is a change to the viewport state
1699 display.mutableDrawingDisplayState().frame = oldFrame;
1700 display.mutableCurrentDisplayState().frame = newFrame;
1701
1702 // --------------------------------------------------------------------
1703 // Invocation
1704
1705 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1706
1707 // --------------------------------------------------------------------
1708 // Postconditions
1709
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001710 EXPECT_EQ(newFrame, display.mutableDisplayDevice()->getFrame());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001711}
1712
1713TEST_F(HandleTransactionLockedTest, processesDisplayWidthChanges) {
1714 using Case = NonHwcVirtualDisplayCase;
1715
1716 constexpr int oldWidth = 0;
1717 constexpr int oldHeight = 10;
1718 constexpr int newWidth = 123;
1719
1720 // --------------------------------------------------------------------
1721 // Preconditions
1722
1723 // A display is set up
1724 auto nativeWindow = new mock::NativeWindow();
1725 auto displaySurface = new mock::DisplaySurface();
1726 auto renderSurface = new RE::mock::Surface();
1727 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1728 display.setNativeWindow(nativeWindow);
1729 display.setDisplaySurface(displaySurface);
1730 display.setRenderSurface(std::unique_ptr<RE::Surface>(renderSurface));
1731 display.inject();
1732
1733 // There is a change to the viewport state
1734 display.mutableDrawingDisplayState().width = oldWidth;
1735 display.mutableDrawingDisplayState().height = oldHeight;
1736 display.mutableCurrentDisplayState().width = newWidth;
1737 display.mutableCurrentDisplayState().height = oldHeight;
1738
1739 // --------------------------------------------------------------------
1740 // Call Expectations
1741
1742 EXPECT_CALL(*renderSurface, setNativeWindow(nullptr)).Times(1);
1743 EXPECT_CALL(*displaySurface, resizeBuffers(newWidth, oldHeight)).Times(1);
1744 EXPECT_CALL(*renderSurface, setNativeWindow(nativeWindow)).Times(1);
1745 EXPECT_CALL(*renderSurface, queryWidth()).WillOnce(Return(newWidth));
1746 EXPECT_CALL(*renderSurface, queryHeight()).WillOnce(Return(oldHeight));
1747
1748 // --------------------------------------------------------------------
1749 // Invocation
1750
1751 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1752}
1753
1754TEST_F(HandleTransactionLockedTest, processesDisplayHeightChanges) {
1755 using Case = NonHwcVirtualDisplayCase;
1756
1757 constexpr int oldWidth = 0;
1758 constexpr int oldHeight = 10;
1759 constexpr int newHeight = 123;
1760
1761 // --------------------------------------------------------------------
1762 // Preconditions
1763
1764 // A display is set up
1765 auto nativeWindow = new mock::NativeWindow();
1766 auto displaySurface = new mock::DisplaySurface();
1767 auto renderSurface = new RE::mock::Surface();
1768 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1769 display.setNativeWindow(nativeWindow);
1770 display.setDisplaySurface(displaySurface);
1771 display.setRenderSurface(std::unique_ptr<RE::Surface>(renderSurface));
1772 display.inject();
1773
1774 // There is a change to the viewport state
1775 display.mutableDrawingDisplayState().width = oldWidth;
1776 display.mutableDrawingDisplayState().height = oldHeight;
1777 display.mutableCurrentDisplayState().width = oldWidth;
1778 display.mutableCurrentDisplayState().height = newHeight;
1779
1780 // --------------------------------------------------------------------
1781 // Call Expectations
1782
1783 EXPECT_CALL(*renderSurface, setNativeWindow(nullptr)).Times(1);
1784 EXPECT_CALL(*displaySurface, resizeBuffers(oldWidth, newHeight)).Times(1);
1785 EXPECT_CALL(*renderSurface, setNativeWindow(nativeWindow)).Times(1);
1786 EXPECT_CALL(*renderSurface, queryWidth()).WillOnce(Return(oldWidth));
1787 EXPECT_CALL(*renderSurface, queryHeight()).WillOnce(Return(newHeight));
1788
1789 // --------------------------------------------------------------------
1790 // Invocation
1791
1792 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1793}
1794
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001795/* ------------------------------------------------------------------------
1796 * SurfaceFlinger::setDisplayStateLocked
1797 */
1798
1799TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithUnknownDisplay) {
1800 // --------------------------------------------------------------------
1801 // Preconditions
1802
1803 // We have an unknown display token not associated with a known display
1804 sp<BBinder> displayToken = new BBinder();
1805
1806 // The requested display state references the unknown display.
1807 DisplayState state;
1808 state.what = DisplayState::eLayerStackChanged;
1809 state.token = displayToken;
1810 state.layerStack = 456;
1811
1812 // --------------------------------------------------------------------
1813 // Invocation
1814
1815 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1816
1817 // --------------------------------------------------------------------
1818 // Postconditions
1819
1820 // The returned flags are empty
1821 EXPECT_EQ(0u, flags);
1822
1823 // The display token still doesn't match anything known.
1824 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1825}
1826
1827TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithInvalidDisplay) {
1828 using Case = InvalidDisplayCase;
1829
1830 // --------------------------------------------------------------------
1831 // Preconditions
1832
1833 // An invalid display is set up
1834 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1835 display.inject();
1836
1837 // The invalid display has some state
1838 display.mutableCurrentDisplayState().layerStack = 654u;
1839
1840 // The requested display state tries to change the display state.
1841 DisplayState state;
1842 state.what = DisplayState::eLayerStackChanged;
1843 state.token = display.token();
1844 state.layerStack = 456;
1845
1846 // --------------------------------------------------------------------
1847 // Invocation
1848
1849 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1850
1851 // --------------------------------------------------------------------
1852 // Postconditions
1853
1854 // The returned flags are empty
1855 EXPECT_EQ(0u, flags);
1856
1857 // The current display layer stack value is unchanged.
1858 EXPECT_EQ(654u, getCurrentDisplayState(display.token()).layerStack);
1859}
1860
1861TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWhenNoChanges) {
1862 using Case = SimplePrimaryDisplayCase;
1863
1864 // --------------------------------------------------------------------
1865 // Preconditions
1866
1867 // A display is already set up
1868 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1869 display.inject();
1870
1871 // No changes are made to the display
1872 DisplayState state;
1873 state.what = 0;
1874 state.token = display.token();
1875
1876 // --------------------------------------------------------------------
1877 // Invocation
1878
1879 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1880
1881 // --------------------------------------------------------------------
1882 // Postconditions
1883
1884 // The returned flags are empty
1885 EXPECT_EQ(0u, flags);
1886}
1887
1888TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSurfaceDidNotChange) {
1889 using Case = SimplePrimaryDisplayCase;
1890
1891 // --------------------------------------------------------------------
1892 // Preconditions
1893
1894 // A display is already set up
1895 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1896 display.inject();
1897
1898 // There is a surface that can be set.
1899 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
1900
1901 // The current display state has the surface set
1902 display.mutableCurrentDisplayState().surface = surface;
1903
1904 // The incoming request sets the same surface
1905 DisplayState state;
1906 state.what = DisplayState::eSurfaceChanged;
1907 state.token = display.token();
1908 state.surface = surface;
1909
1910 // --------------------------------------------------------------------
1911 // Invocation
1912
1913 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1914
1915 // --------------------------------------------------------------------
1916 // Postconditions
1917
1918 // The returned flags are empty
1919 EXPECT_EQ(0u, flags);
1920
1921 // The current display state is unchanged.
1922 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
1923}
1924
1925TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfSurfaceChanged) {
1926 using Case = SimplePrimaryDisplayCase;
1927
1928 // --------------------------------------------------------------------
1929 // Preconditions
1930
1931 // A display is already set up
1932 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1933 display.inject();
1934
1935 // There is a surface that can be set.
1936 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
1937
1938 // The current display state does not have a surface
1939 display.mutableCurrentDisplayState().surface = nullptr;
1940
1941 // The incoming request sets a surface
1942 DisplayState state;
1943 state.what = DisplayState::eSurfaceChanged;
1944 state.token = display.token();
1945 state.surface = surface;
1946
1947 // --------------------------------------------------------------------
1948 // Invocation
1949
1950 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1951
1952 // --------------------------------------------------------------------
1953 // Postconditions
1954
1955 // The returned flags indicate a transaction is needed
1956 EXPECT_EQ(eDisplayTransactionNeeded, flags);
1957
1958 // The current display layer stack state is set to the new value
1959 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
1960}
1961
1962TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfLayerStackDidNotChange) {
1963 using Case = SimplePrimaryDisplayCase;
1964
1965 // --------------------------------------------------------------------
1966 // Preconditions
1967
1968 // A display is already set up
1969 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1970 display.inject();
1971
1972 // The display has a layer stack set
1973 display.mutableCurrentDisplayState().layerStack = 456u;
1974
1975 // The incoming request sets the same layer stack
1976 DisplayState state;
1977 state.what = DisplayState::eLayerStackChanged;
1978 state.token = display.token();
1979 state.layerStack = 456u;
1980
1981 // --------------------------------------------------------------------
1982 // Invocation
1983
1984 uint32_t flags = mFlinger.setDisplayStateLocked(state);
1985
1986 // --------------------------------------------------------------------
1987 // Postconditions
1988
1989 // The returned flags are empty
1990 EXPECT_EQ(0u, flags);
1991
1992 // The current display state is unchanged
1993 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
1994}
1995
1996TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfLayerStackChanged) {
1997 using Case = SimplePrimaryDisplayCase;
1998
1999 // --------------------------------------------------------------------
2000 // Preconditions
2001
2002 // A display is set up
2003 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2004 display.inject();
2005
2006 // The display has a layer stack set
2007 display.mutableCurrentDisplayState().layerStack = 654u;
2008
2009 // The incoming request sets a different layer stack
2010 DisplayState state;
2011 state.what = DisplayState::eLayerStackChanged;
2012 state.token = display.token();
2013 state.layerStack = 456u;
2014
2015 // --------------------------------------------------------------------
2016 // Invocation
2017
2018 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2019
2020 // --------------------------------------------------------------------
2021 // Postconditions
2022
2023 // The returned flags indicate a transaction is needed
2024 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2025
2026 // The desired display state has been set to the new value.
2027 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2028}
2029
2030TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfProjectionDidNotChange) {
2031 using Case = SimplePrimaryDisplayCase;
2032 constexpr int initialOrientation = 180;
2033 const Rect initialFrame = {1, 2, 3, 4};
2034 const Rect initialViewport = {5, 6, 7, 8};
2035
2036 // --------------------------------------------------------------------
2037 // Preconditions
2038
2039 // A display is set up
2040 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2041 display.inject();
2042
2043 // The current display state projection state is all set
2044 display.mutableCurrentDisplayState().orientation = initialOrientation;
2045 display.mutableCurrentDisplayState().frame = initialFrame;
2046 display.mutableCurrentDisplayState().viewport = initialViewport;
2047
2048 // The incoming request sets the same projection state
2049 DisplayState state;
2050 state.what = DisplayState::eDisplayProjectionChanged;
2051 state.token = display.token();
2052 state.orientation = initialOrientation;
2053 state.frame = initialFrame;
2054 state.viewport = initialViewport;
2055
2056 // --------------------------------------------------------------------
2057 // Invocation
2058
2059 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2060
2061 // --------------------------------------------------------------------
2062 // Postconditions
2063
2064 // The returned flags are empty
2065 EXPECT_EQ(0u, flags);
2066
2067 // The current display state is unchanged
2068 EXPECT_EQ(initialOrientation, display.getCurrentDisplayState().orientation);
2069
2070 EXPECT_EQ(initialFrame, display.getCurrentDisplayState().frame);
2071 EXPECT_EQ(initialViewport, display.getCurrentDisplayState().viewport);
2072}
2073
2074TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfOrientationChanged) {
2075 using Case = SimplePrimaryDisplayCase;
2076 constexpr int initialOrientation = 90;
2077 constexpr int desiredOrientation = 180;
2078
2079 // --------------------------------------------------------------------
2080 // Preconditions
2081
2082 // A display is set up
2083 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2084 display.inject();
2085
2086 // The current display state has an orientation set
2087 display.mutableCurrentDisplayState().orientation = initialOrientation;
2088
2089 // The incoming request sets a different orientation
2090 DisplayState state;
2091 state.what = DisplayState::eDisplayProjectionChanged;
2092 state.token = display.token();
2093 state.orientation = desiredOrientation;
2094
2095 // --------------------------------------------------------------------
2096 // Invocation
2097
2098 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2099
2100 // --------------------------------------------------------------------
2101 // Postconditions
2102
2103 // The returned flags indicate a transaction is needed
2104 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2105
2106 // The current display state has the new value.
2107 EXPECT_EQ(desiredOrientation, display.getCurrentDisplayState().orientation);
2108}
2109
2110TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfFrameChanged) {
2111 using Case = SimplePrimaryDisplayCase;
2112 const Rect initialFrame = {0, 0, 0, 0};
2113 const Rect desiredFrame = {5, 6, 7, 8};
2114
2115 // --------------------------------------------------------------------
2116 // Preconditions
2117
2118 // A display is set up
2119 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2120 display.inject();
2121
2122 // The current display state does not have a frame
2123 display.mutableCurrentDisplayState().frame = initialFrame;
2124
2125 // The incoming request sets a frame
2126 DisplayState state;
2127 state.what = DisplayState::eDisplayProjectionChanged;
2128 state.token = display.token();
2129 state.frame = desiredFrame;
2130
2131 // --------------------------------------------------------------------
2132 // Invocation
2133
2134 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2135
2136 // --------------------------------------------------------------------
2137 // Postconditions
2138
2139 // The returned flags indicate a transaction is needed
2140 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2141
2142 // The current display state has the new value.
2143 EXPECT_EQ(desiredFrame, display.getCurrentDisplayState().frame);
2144}
2145
2146TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfViewportChanged) {
2147 using Case = SimplePrimaryDisplayCase;
2148 const Rect initialViewport = {0, 0, 0, 0};
2149 const Rect desiredViewport = {5, 6, 7, 8};
2150
2151 // --------------------------------------------------------------------
2152 // Preconditions
2153
2154 // A display is set up
2155 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2156 display.inject();
2157
2158 // The current display state does not have a viewport
2159 display.mutableCurrentDisplayState().viewport = initialViewport;
2160
2161 // The incoming request sets a viewport
2162 DisplayState state;
2163 state.what = DisplayState::eDisplayProjectionChanged;
2164 state.token = display.token();
2165 state.viewport = desiredViewport;
2166
2167 // --------------------------------------------------------------------
2168 // Invocation
2169
2170 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2171
2172 // --------------------------------------------------------------------
2173 // Postconditions
2174
2175 // The returned flags indicate a transaction is needed
2176 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2177
2178 // The current display state has the new value.
2179 EXPECT_EQ(desiredViewport, display.getCurrentDisplayState().viewport);
2180}
2181
2182TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSizeDidNotChange) {
2183 using Case = SimplePrimaryDisplayCase;
2184 constexpr uint32_t initialWidth = 1024;
2185 constexpr uint32_t initialHeight = 768;
2186
2187 // --------------------------------------------------------------------
2188 // Preconditions
2189
2190 // A display is set up
2191 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2192 display.inject();
2193
2194 // The current display state has a size set
2195 display.mutableCurrentDisplayState().width = initialWidth;
2196 display.mutableCurrentDisplayState().height = initialHeight;
2197
2198 // The incoming request sets the same display size
2199 DisplayState state;
2200 state.what = DisplayState::eDisplaySizeChanged;
2201 state.token = display.token();
2202 state.width = initialWidth;
2203 state.height = initialHeight;
2204
2205 // --------------------------------------------------------------------
2206 // Invocation
2207
2208 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2209
2210 // --------------------------------------------------------------------
2211 // Postconditions
2212
2213 // The returned flags are empty
2214 EXPECT_EQ(0u, flags);
2215
2216 // The current display state is unchanged
2217 EXPECT_EQ(initialWidth, display.getCurrentDisplayState().width);
2218 EXPECT_EQ(initialHeight, display.getCurrentDisplayState().height);
2219}
2220
2221TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfWidthChanged) {
2222 using Case = SimplePrimaryDisplayCase;
2223 constexpr uint32_t initialWidth = 0;
2224 constexpr uint32_t desiredWidth = 1024;
2225
2226 // --------------------------------------------------------------------
2227 // Preconditions
2228
2229 // A display is set up
2230 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2231 display.inject();
2232
2233 // The display does not yet have a width
2234 display.mutableCurrentDisplayState().width = initialWidth;
2235
2236 // The incoming request sets a display width
2237 DisplayState state;
2238 state.what = DisplayState::eDisplaySizeChanged;
2239 state.token = display.token();
2240 state.width = desiredWidth;
2241
2242 // --------------------------------------------------------------------
2243 // Invocation
2244
2245 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2246
2247 // --------------------------------------------------------------------
2248 // Postconditions
2249
2250 // The returned flags indicate a transaction is needed
2251 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2252
2253 // The current display state has the new value.
2254 EXPECT_EQ(desiredWidth, display.getCurrentDisplayState().width);
2255}
2256
2257TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfHeightChanged) {
2258 using Case = SimplePrimaryDisplayCase;
2259 constexpr uint32_t initialHeight = 0;
2260 constexpr uint32_t desiredHeight = 768;
2261
2262 // --------------------------------------------------------------------
2263 // Preconditions
2264
2265 // A display is set up
2266 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2267 display.inject();
2268
2269 // The display does not yet have a height
2270 display.mutableCurrentDisplayState().height = initialHeight;
2271
2272 // The incoming request sets a display height
2273 DisplayState state;
2274 state.what = DisplayState::eDisplaySizeChanged;
2275 state.token = display.token();
2276 state.height = desiredHeight;
2277
2278 // --------------------------------------------------------------------
2279 // Invocation
2280
2281 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2282
2283 // --------------------------------------------------------------------
2284 // Postconditions
2285
2286 // The returned flags indicate a transaction is needed
2287 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2288
2289 // The current display state has the new value.
2290 EXPECT_EQ(desiredHeight, display.getCurrentDisplayState().height);
2291}
2292
Lloyd Pique86016da2018-03-01 16:09:38 -08002293/* ------------------------------------------------------------------------
2294 * SurfaceFlinger::onInitializeDisplays
2295 */
2296
2297TEST_F(DisplayTransactionTest, onInitializeDisplaysSetsUpPrimaryDisplay) {
2298 using Case = SimplePrimaryDisplayCase;
2299
2300 // --------------------------------------------------------------------
2301 // Preconditions
2302
2303 // A primary display is set up
2304 Case::Display::injectHwcDisplay(this);
2305 auto primaryDisplay = Case::Display::makeFakeExistingDisplayInjector(this);
2306 primaryDisplay.inject();
2307
2308 // --------------------------------------------------------------------
2309 // Call Expectations
2310
2311 // We expect the surface interceptor to possibly be used, but we treat it as
2312 // disabled since it is called as a side effect rather than directly by this
2313 // function.
2314 EXPECT_CALL(*mSurfaceInterceptor, isEnabled()).WillOnce(Return(false));
2315
2316 // We expect a call to get the active display config.
2317 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
2318
2319 // We expect invalidate() to be invoked once to trigger display transaction
2320 // processing.
2321 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
2322
2323 // --------------------------------------------------------------------
2324 // Invocation
2325
2326 mFlinger.onInitializeDisplays();
2327
2328 // --------------------------------------------------------------------
2329 // Postconditions
2330
2331 // The primary display should have a current state
2332 ASSERT_TRUE(hasCurrentDisplayState(primaryDisplay.token()));
2333 const auto& primaryDisplayState = getCurrentDisplayState(primaryDisplay.token());
2334 // The layer stack state should be set to zero
2335 EXPECT_EQ(0u, primaryDisplayState.layerStack);
2336 // The orientation state should be set to zero
2337 EXPECT_EQ(0, primaryDisplayState.orientation);
2338
2339 // The frame state should be set to INVALID
2340 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.frame);
2341
2342 // The viewport state should be set to INVALID
2343 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.viewport);
2344
2345 // The width and height should both be zero
2346 EXPECT_EQ(0u, primaryDisplayState.width);
2347 EXPECT_EQ(0u, primaryDisplayState.height);
2348
2349 // The display should be set to HWC_POWER_MODE_NORMAL
2350 ASSERT_TRUE(hasDisplayDevice(primaryDisplay.token()));
2351 auto displayDevice = primaryDisplay.mutableDisplayDevice();
2352 EXPECT_EQ(HWC_POWER_MODE_NORMAL, displayDevice->getPowerMode());
2353
2354 // The display refresh period should be set in the frame tracker.
2355 FrameStats stats;
2356 mFlinger.getAnimFrameTracker().getStats(&stats);
2357 EXPECT_EQ(DEFAULT_REFRESH_RATE, stats.refreshPeriodNano);
2358
2359 // The display transaction needed flag should be set.
2360 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
2361
2362 // The compositor timing should be set to default values
2363 const auto& compositorTiming = mFlinger.getCompositorTiming();
2364 EXPECT_EQ(-DEFAULT_REFRESH_RATE, compositorTiming.deadline);
2365 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.interval);
2366 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.presentLatency);
2367}
2368
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002369/* ------------------------------------------------------------------------
2370 * SurfaceFlinger::setPowerModeInternal
2371 */
2372
2373// Used when we simulate a display that supports doze.
2374struct DozeIsSupportedVariant {
2375 static constexpr bool DOZE_SUPPORTED = true;
2376 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2377 IComposerClient::PowerMode::DOZE;
2378 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2379 IComposerClient::PowerMode::DOZE_SUSPEND;
2380};
2381
2382// Used when we simulate a display that does not support doze.
2383struct DozeNotSupportedVariant {
2384 static constexpr bool DOZE_SUPPORTED = false;
2385 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2386 IComposerClient::PowerMode::ON;
2387 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2388 IComposerClient::PowerMode::ON;
2389};
2390
2391struct EventThreadBaseSupportedVariant {
2392 static void setupEventAndEventControlThreadNoCallExpectations(DisplayTransactionTest* test) {
2393 // The event control thread should not be notified.
2394 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(_)).Times(0);
2395
2396 // The event thread should not be notified.
2397 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(0);
2398 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(0);
2399 }
2400};
2401
2402struct EventThreadNotSupportedVariant : public EventThreadBaseSupportedVariant {
2403 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2404 // These calls are only expected for the primary display.
2405
2406 // Instead expect no calls.
2407 setupEventAndEventControlThreadNoCallExpectations(test);
2408 }
2409
2410 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2411 // These calls are only expected for the primary display.
2412
2413 // Instead expect no calls.
2414 setupEventAndEventControlThreadNoCallExpectations(test);
2415 }
2416};
2417
2418struct EventThreadIsSupportedVariant : public EventThreadBaseSupportedVariant {
2419 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2420 // The event control thread should be notified to enable vsyncs
2421 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(true)).Times(1);
2422
2423 // The event thread should be notified that the screen was acquired.
2424 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(1);
2425 }
2426
2427 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2428 // There should be a call to setVsyncEnabled(false)
2429 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(false)).Times(1);
2430
2431 // The event thread should not be notified that the screen was released.
2432 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(1);
2433 }
2434};
2435
2436// --------------------------------------------------------------------
2437// Note:
2438//
2439// There are a large number of transitions we could test, however we only test a
2440// selected subset which provides complete test coverage of the implementation.
2441// --------------------------------------------------------------------
2442
2443template <int initialPowerMode, int targetPowerMode>
2444struct TransitionVariantCommon {
2445 static constexpr auto INITIAL_POWER_MODE = initialPowerMode;
2446 static constexpr auto TARGET_POWER_MODE = targetPowerMode;
2447
2448 static void verifyPostconditions(DisplayTransactionTest*) {}
2449};
2450
2451struct TransitionOffToOnVariant
2452 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_NORMAL> {
2453 template <typename Case>
2454 static void setupCallExpectations(DisplayTransactionTest* test) {
2455 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2456 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
2457 Case::setupRepaintEverythingCallExpectations(test);
2458 }
2459
2460 static void verifyPostconditions(DisplayTransactionTest* test) {
2461 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2462 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2463 }
2464};
2465
2466struct TransitionOffToDozeSuspendVariant
2467 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_DOZE_SUSPEND> {
2468 template <typename Case>
2469 static void setupCallExpectations(DisplayTransactionTest* test) {
2470 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2471 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2472 Case::setupRepaintEverythingCallExpectations(test);
2473 }
2474
2475 static void verifyPostconditions(DisplayTransactionTest* test) {
2476 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2477 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2478 }
2479};
2480
2481struct TransitionOnToOffVariant
2482 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_OFF> {
2483 template <typename Case>
2484 static void setupCallExpectations(DisplayTransactionTest* test) {
2485 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
2486 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2487 }
2488
2489 static void verifyPostconditions(DisplayTransactionTest* test) {
2490 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2491 }
2492};
2493
2494struct TransitionDozeSuspendToOffVariant
2495 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_OFF> {
2496 template <typename Case>
2497 static void setupCallExpectations(DisplayTransactionTest* test) {
2498 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2499 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2500 }
2501
2502 static void verifyPostconditions(DisplayTransactionTest* test) {
2503 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2504 }
2505};
2506
2507struct TransitionOnToDozeVariant
2508 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE> {
2509 template <typename Case>
2510 static void setupCallExpectations(DisplayTransactionTest* test) {
2511 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2512 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2513 }
2514};
2515
2516struct TransitionDozeSuspendToDozeVariant
2517 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_DOZE> {
2518 template <typename Case>
2519 static void setupCallExpectations(DisplayTransactionTest* test) {
2520 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
2521 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2522 }
2523};
2524
2525struct TransitionDozeToOnVariant
2526 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE, HWC_POWER_MODE_NORMAL> {
2527 template <typename Case>
2528 static void setupCallExpectations(DisplayTransactionTest* test) {
2529 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2530 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2531 }
2532};
2533
2534struct TransitionDozeSuspendToOnVariant
2535 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_NORMAL> {
2536 template <typename Case>
2537 static void setupCallExpectations(DisplayTransactionTest* test) {
2538 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
2539 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2540 }
2541};
2542
2543struct TransitionOnToDozeSuspendVariant
2544 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE_SUSPEND> {
2545 template <typename Case>
2546 static void setupCallExpectations(DisplayTransactionTest* test) {
2547 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
2548 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2549 }
2550};
2551
2552struct TransitionOnToUnknownVariant
2553 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_LEET> {
2554 template <typename Case>
2555 static void setupCallExpectations(DisplayTransactionTest* test) {
2556 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2557 Case::setupNoComposerPowerModeCallExpectations(test);
2558 }
2559};
2560
2561// --------------------------------------------------------------------
2562// Note:
2563//
2564// Rather than testing the cartesian product of of
2565// DozeIsSupported/DozeNotSupported with all other options, we use one for one
2566// display type, and the other for another display type.
2567// --------------------------------------------------------------------
2568
2569template <typename DisplayVariant, typename DozeVariant, typename EventThreadVariant,
2570 typename TransitionVariant>
2571struct DisplayPowerCase {
2572 using Display = DisplayVariant;
2573 using Doze = DozeVariant;
2574 using EventThread = EventThreadVariant;
2575 using Transition = TransitionVariant;
2576
2577 static auto injectDisplayWithInitialPowerMode(DisplayTransactionTest* test, int mode) {
2578 Display::injectHwcDisplay(test);
2579 auto display = Display::makeFakeExistingDisplayInjector(test);
2580 display.inject();
2581 display.mutableDisplayDevice()->setPowerMode(mode);
2582 return display;
2583 }
2584
2585 static void setInitialPrimaryHWVsyncEnabled(DisplayTransactionTest* test, bool enabled) {
2586 test->mFlinger.mutablePrimaryHWVsyncEnabled() = enabled;
2587 }
2588
2589 static void setupRepaintEverythingCallExpectations(DisplayTransactionTest* test) {
2590 EXPECT_CALL(*test->mMessageQueue, invalidate()).Times(1);
2591 }
2592
2593 static void setupSurfaceInterceptorCallExpectations(DisplayTransactionTest* test, int mode) {
2594 EXPECT_CALL(*test->mSurfaceInterceptor, isEnabled()).WillOnce(Return(true));
2595 EXPECT_CALL(*test->mSurfaceInterceptor, savePowerModeUpdate(_, mode)).Times(1);
2596 }
2597
2598 static void setupComposerCallExpectations(DisplayTransactionTest* test,
2599 IComposerClient::PowerMode mode) {
2600 // Any calls to get the active config will return a default value.
2601 EXPECT_CALL(*test->mComposer, getActiveConfig(Display::HWC_DISPLAY_ID, _))
2602 .WillRepeatedly(DoAll(SetArgPointee<1>(Display::HWC_ACTIVE_CONFIG_ID),
2603 Return(Error::NONE)));
2604
2605 // Any calls to get whether the display supports dozing will return the value set by the
2606 // policy variant.
2607 EXPECT_CALL(*test->mComposer, getDozeSupport(Display::HWC_DISPLAY_ID, _))
2608 .WillRepeatedly(DoAll(SetArgPointee<1>(Doze::DOZE_SUPPORTED), Return(Error::NONE)));
2609
2610 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, mode)).Times(1);
2611 }
2612
2613 static void setupNoComposerPowerModeCallExpectations(DisplayTransactionTest* test) {
2614 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, _)).Times(0);
2615 }
2616};
2617
2618// A sample configuration for the primary display.
2619// In addition to having event thread support, we emulate doze support.
2620template <typename TransitionVariant>
2621using PrimaryDisplayPowerCase = DisplayPowerCase<PrimaryDisplayVariant, DozeIsSupportedVariant,
2622 EventThreadIsSupportedVariant, TransitionVariant>;
2623
2624// A sample configuration for the external display.
2625// In addition to not having event thread support, we emulate not having doze
2626// support.
2627template <typename TransitionVariant>
2628using ExternalDisplayPowerCase =
2629 DisplayPowerCase<ExternalDisplayVariant, DozeNotSupportedVariant,
2630 EventThreadNotSupportedVariant, TransitionVariant>;
2631
2632class SetPowerModeInternalTest : public DisplayTransactionTest {
2633public:
2634 template <typename Case>
2635 void transitionDisplayCommon();
2636};
2637
2638template <int PowerMode>
2639struct PowerModeInitialVSyncEnabled : public std::false_type {};
2640
2641template <>
2642struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_NORMAL> : public std::true_type {};
2643
2644template <>
2645struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_DOZE> : public std::true_type {};
2646
2647template <typename Case>
2648void SetPowerModeInternalTest::transitionDisplayCommon() {
2649 // --------------------------------------------------------------------
2650 // Preconditions
2651
2652 auto display =
2653 Case::injectDisplayWithInitialPowerMode(this, Case::Transition::INITIAL_POWER_MODE);
2654 Case::setInitialPrimaryHWVsyncEnabled(this,
2655 PowerModeInitialVSyncEnabled<
2656 Case::Transition::INITIAL_POWER_MODE>::value);
2657
2658 // --------------------------------------------------------------------
2659 // Call Expectations
2660
2661 Case::setupSurfaceInterceptorCallExpectations(this, Case::Transition::TARGET_POWER_MODE);
2662 Case::Transition::template setupCallExpectations<Case>(this);
2663
2664 // --------------------------------------------------------------------
2665 // Invocation
2666
2667 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(),
2668 Case::Transition::TARGET_POWER_MODE);
2669
2670 // --------------------------------------------------------------------
2671 // Postconditions
2672
2673 Case::Transition::verifyPostconditions(this);
2674}
2675
2676TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfNoChange) {
2677 using Case = SimplePrimaryDisplayCase;
2678
2679 // --------------------------------------------------------------------
2680 // Preconditions
2681
2682 // A primary display device is set up
2683 Case::Display::injectHwcDisplay(this);
2684 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2685 display.inject();
2686
Dominik Laskowskia2edf612018-06-01 13:15:16 -07002687 // The display is already set to HWC_POWER_MODE_NORMAL
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002688 display.mutableDisplayDevice()->setPowerMode(HWC_POWER_MODE_NORMAL);
2689
2690 // --------------------------------------------------------------------
2691 // Invocation
2692
2693 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_NORMAL);
2694
2695 // --------------------------------------------------------------------
2696 // Postconditions
2697
2698 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2699}
2700
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002701TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfVirtualDisplay) {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002702 using Case = HwcVirtualDisplayCase;
2703
2704 // --------------------------------------------------------------------
2705 // Preconditions
2706
2707 // We need to resize this so that the HWC thinks the virtual display
2708 // is something it created.
2709 mFlinger.mutableHwcDisplayData().resize(3);
2710
2711 // A virtual display device is set up
2712 Case::Display::injectHwcDisplay(this);
2713 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2714 display.inject();
2715
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002716 // The display is set to HWC_POWER_MODE_NORMAL
2717 getDisplayDevice(display.token())->setPowerMode(HWC_POWER_MODE_NORMAL);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002718
2719 // --------------------------------------------------------------------
2720 // Invocation
2721
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002722 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_OFF);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002723
2724 // --------------------------------------------------------------------
2725 // Postconditions
2726
2727 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2728}
2729
2730TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnPrimaryDisplay) {
2731 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToOnVariant>>();
2732}
2733
2734TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendPrimaryDisplay) {
2735 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
2736}
2737
2738TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffPrimaryDisplay) {
2739 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToOffVariant>>();
2740}
2741
2742TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffPrimaryDisplay) {
2743 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
2744}
2745
2746TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozePrimaryDisplay) {
2747 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeVariant>>();
2748}
2749
2750TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozePrimaryDisplay) {
2751 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
2752}
2753
2754TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnPrimaryDisplay) {
2755 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeToOnVariant>>();
2756}
2757
2758TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnPrimaryDisplay) {
2759 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
2760}
2761
2762TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendPrimaryDisplay) {
2763 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
2764}
2765
2766TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownPrimaryDisplay) {
2767 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToUnknownVariant>>();
2768}
2769
2770TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnExternalDisplay) {
2771 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToOnVariant>>();
2772}
2773
2774TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendExternalDisplay) {
2775 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
2776}
2777
2778TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffExternalDisplay) {
2779 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToOffVariant>>();
2780}
2781
2782TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffExternalDisplay) {
2783 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
2784}
2785
2786TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeExternalDisplay) {
2787 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeVariant>>();
2788}
2789
2790TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozeExternalDisplay) {
2791 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
2792}
2793
2794TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnExternalDisplay) {
2795 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeToOnVariant>>();
2796}
2797
2798TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnExternalDisplay) {
2799 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
2800}
2801
2802TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendExternalDisplay) {
2803 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
2804}
2805
2806TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownExternalDisplay) {
2807 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToUnknownVariant>>();
2808}
2809
Lloyd Piquef58625d2017-12-19 13:22:33 -08002810} // namespace
2811} // namespace android