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