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