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