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