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