blob: aa1fc1a4747173248791e5a065f424e18a334fcc [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
Dominik Laskowski075d3172018-05-24 15:50:06 -070020#include <type_traits>
21
Lloyd Piquef58625d2017-12-19 13:22:33 -080022#include <gmock/gmock.h>
23#include <gtest/gtest.h>
24
25#include <log/log.h>
26
Valerie Hau9758ae02018-10-09 16:05:09 -070027#include <ui/DebugUtils.h>
Dominik Laskowski075d3172018-05-24 15:50:06 -070028
29#include "DisplayIdentificationTest.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080030#include "TestableSurfaceFlinger.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080031#include "mock/DisplayHardware/MockComposer.h"
32#include "mock/DisplayHardware/MockDisplaySurface.h"
Lloyd Pique41be5d22018-06-21 13:11:48 -070033#include "mock/MockDispSync.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080034#include "mock/MockEventControlThread.h"
35#include "mock/MockEventThread.h"
36#include "mock/MockMessageQueue.h"
37#include "mock/MockNativeWindowSurface.h"
38#include "mock/MockSurfaceInterceptor.h"
39#include "mock/RenderEngine/MockRenderEngine.h"
40#include "mock/gui/MockGraphicBufferConsumer.h"
41#include "mock/gui/MockGraphicBufferProducer.h"
42#include "mock/system/window/MockNativeWindow.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080043
44namespace android {
45namespace {
46
Lloyd Piquee39cad22017-12-20 17:01:29 -080047using testing::_;
48using testing::ByMove;
49using testing::DoAll;
50using testing::Mock;
51using testing::Return;
52using testing::SetArgPointee;
53
Lloyd Piqued883d5a2018-04-27 19:32:30 -070054using android::Hwc2::ColorMode;
Lloyd Piquee39cad22017-12-20 17:01:29 -080055using android::Hwc2::Error;
Lloyd Piqued883d5a2018-04-27 19:32:30 -070056using android::Hwc2::Hdr;
Lloyd Piquee39cad22017-12-20 17:01:29 -080057using android::Hwc2::IComposer;
58using android::Hwc2::IComposerClient;
Lloyd Piqued883d5a2018-04-27 19:32:30 -070059using android::Hwc2::PerFrameMetadataKey;
60using android::Hwc2::RenderIntent;
Lloyd Piquee39cad22017-12-20 17:01:29 -080061
Lloyd Piquec11e0d32018-01-22 18:44:59 -080062using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector;
63using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
Lloyd Pique1fa4d462018-01-22 18:03:16 -080064using HotplugEvent = TestableSurfaceFlinger::HotplugEvent;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080065using HWC2Display = TestableSurfaceFlinger::HWC2Display;
Lloyd Piquebc792092018-01-17 11:52:30 -080066
Lloyd Piquec11e0d32018-01-22 18:44:59 -080067constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666;
Lloyd Piquee39cad22017-12-20 17:01:29 -080068constexpr int32_t DEFAULT_DPI = 320;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080069constexpr int DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT = HAL_PIXEL_FORMAT_RGB_565;
Lloyd Piquee39cad22017-12-20 17:01:29 -080070
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -080071constexpr int HWC_POWER_MODE_LEET = 1337; // An out of range power mode value
72
Lloyd Piquec11e0d32018-01-22 18:44:59 -080073/* ------------------------------------------------------------------------
74 * Boolean avoidance
75 *
76 * To make calls and template instantiations more readable, we define some
77 * local enums along with an implicit bool conversion.
78 */
79
80#define BOOL_SUBSTITUTE(TYPENAME) enum class TYPENAME : bool { FALSE = false, TRUE = true };
81
Lloyd Piquec11e0d32018-01-22 18:44:59 -080082BOOL_SUBSTITUTE(Async);
Dominik Laskowski075d3172018-05-24 15:50:06 -070083BOOL_SUBSTITUTE(Critical);
84BOOL_SUBSTITUTE(Primary);
Lloyd Piquec11e0d32018-01-22 18:44:59 -080085BOOL_SUBSTITUTE(Secure);
Dominik Laskowski075d3172018-05-24 15:50:06 -070086BOOL_SUBSTITUTE(Virtual);
Lloyd Piquec11e0d32018-01-22 18:44:59 -080087
88/* ------------------------------------------------------------------------
89 *
90 */
Lloyd Pique1fa4d462018-01-22 18:03:16 -080091
Lloyd Piquef58625d2017-12-19 13:22:33 -080092class DisplayTransactionTest : public testing::Test {
Lloyd Piquec11e0d32018-01-22 18:44:59 -080093public:
Lloyd Piquef58625d2017-12-19 13:22:33 -080094 DisplayTransactionTest();
95 ~DisplayTransactionTest() override;
96
Lloyd Pique1fa4d462018-01-22 18:03:16 -080097 // --------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -080098 // Mock/Fake injection
Lloyd Piquef58625d2017-12-19 13:22:33 -080099
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800100 void injectMockComposer(int virtualDisplayCount);
101 void injectFakeBufferQueueFactory();
102 void injectFakeNativeWindowSurfaceFactory();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800103
104 // --------------------------------------------------------------------
105 // Postcondition helpers
106
Dominik Laskowski075d3172018-05-24 15:50:06 -0700107 bool hasPhysicalHwcDisplay(hwc2_display_t hwcDisplayId);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800108 bool hasTransactionFlagSet(int flag);
109 bool hasDisplayDevice(sp<IBinder> displayToken);
110 sp<DisplayDevice> getDisplayDevice(sp<IBinder> displayToken);
111 bool hasCurrentDisplayState(sp<IBinder> displayToken);
112 const DisplayDeviceState& getCurrentDisplayState(sp<IBinder> displayToken);
113 bool hasDrawingDisplayState(sp<IBinder> displayToken);
114 const DisplayDeviceState& getDrawingDisplayState(sp<IBinder> displayToken);
115
116 // --------------------------------------------------------------------
117 // Test instances
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800118
Lloyd Piquef58625d2017-12-19 13:22:33 -0800119 TestableSurfaceFlinger mFlinger;
Lloyd Piquee39cad22017-12-20 17:01:29 -0800120 mock::EventThread* mEventThread = new mock::EventThread();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800121 mock::EventControlThread* mEventControlThread = new mock::EventControlThread();
Alec Mouriba013fa2018-10-16 12:43:11 -0700122 sp<mock::NativeWindow> mNativeWindow = new mock::NativeWindow();
Alec Mouri8147b0e2018-10-09 20:57:19 -0700123 sp<GraphicBuffer> mBuffer = new GraphicBuffer();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800124
125 // These mocks are created by the test, but are destroyed by SurfaceFlinger
126 // by virtue of being stored into a std::unique_ptr. However we still need
127 // to keep a reference to them for use in setting up call expectations.
Peiyong Lin833074a2018-08-28 11:53:54 -0700128 renderengine::mock::RenderEngine* mRenderEngine = new renderengine::mock::RenderEngine();
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800129 Hwc2::mock::Composer* mComposer = nullptr;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800130 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
131 mock::SurfaceInterceptor* mSurfaceInterceptor = new mock::SurfaceInterceptor();
Lloyd Pique41be5d22018-06-21 13:11:48 -0700132 mock::DispSync* mPrimaryDispSync = new mock::DispSync();
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800133
134 // These mocks are created only when expected to be created via a factory.
135 sp<mock::GraphicBufferConsumer> mConsumer;
136 sp<mock::GraphicBufferProducer> mProducer;
Lloyd Pique22098362018-09-13 11:46:49 -0700137 surfaceflinger::mock::NativeWindowSurface* mNativeWindowSurface = nullptr;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800138};
139
140DisplayTransactionTest::DisplayTransactionTest() {
141 const ::testing::TestInfo* const test_info =
142 ::testing::UnitTest::GetInstance()->current_test_info();
143 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800144
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800145 // Default to no wide color display support configured
146 mFlinger.mutableHasWideColorDisplay() = false;
Peiyong Lin13effd12018-07-24 17:01:47 -0700147 mFlinger.mutableUseColorManagement() = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800148 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
149
150 // Default to using HWC virtual displays
151 mFlinger.mutableUseHwcVirtualDisplays() = true;
152
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800153 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
154 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
155 });
156
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800157 mFlinger.setCreateNativeWindowSurface([](auto) {
158 ADD_FAILURE() << "Unexpected request to create a native window surface.";
159 return nullptr;
160 });
161
162 mFlinger.mutableEventControlThread().reset(mEventControlThread);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800163 mFlinger.mutableEventThread().reset(mEventThread);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800164 mFlinger.mutableEventQueue().reset(mMessageQueue);
Peiyong Lin833074a2018-08-28 11:53:54 -0700165 mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800166 mFlinger.mutableInterceptor().reset(mSurfaceInterceptor);
Lloyd Pique41be5d22018-06-21 13:11:48 -0700167 mFlinger.mutablePrimaryDispSync().reset(mPrimaryDispSync);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800168
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800169 injectMockComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -0800170}
171
172DisplayTransactionTest::~DisplayTransactionTest() {
173 const ::testing::TestInfo* const test_info =
174 ::testing::UnitTest::GetInstance()->current_test_info();
175 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
176}
177
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800178void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
179 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800180 EXPECT_CALL(*mComposer, getCapabilities())
181 .WillOnce(Return(std::vector<IComposer::Capability>()));
182 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
183 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -0800184
Lloyd Piquee39cad22017-12-20 17:01:29 -0800185 Mock::VerifyAndClear(mComposer);
186}
187
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800188void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800189 // This setup is only expected once per test.
190 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
191
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800192 mConsumer = new mock::GraphicBufferConsumer();
193 mProducer = new mock::GraphicBufferProducer();
194
195 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
196 *outProducer = mProducer;
197 *outConsumer = mConsumer;
198 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800199}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800200
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800201void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800202 // This setup is only expected once per test.
203 ASSERT_TRUE(mNativeWindowSurface == nullptr);
204
Lloyd Pique22098362018-09-13 11:46:49 -0700205 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800206
Lloyd Pique22098362018-09-13 11:46:49 -0700207 mFlinger.setCreateNativeWindowSurface([this](auto) {
208 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
209 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800210}
211
Dominik Laskowski075d3172018-05-24 15:50:06 -0700212bool DisplayTransactionTest::hasPhysicalHwcDisplay(hwc2_display_t hwcDisplayId) {
213 return mFlinger.mutableHwcPhysicalDisplayIdMap().count(hwcDisplayId) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800214}
215
216bool DisplayTransactionTest::hasTransactionFlagSet(int flag) {
217 return mFlinger.mutableTransactionFlags() & flag;
218}
219
220bool DisplayTransactionTest::hasDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700221 return mFlinger.mutableDisplays().count(displayToken) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800222}
223
224sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700225 return mFlinger.mutableDisplays()[displayToken];
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800226}
227
228bool DisplayTransactionTest::hasCurrentDisplayState(sp<IBinder> displayToken) {
229 return mFlinger.mutableCurrentState().displays.indexOfKey(displayToken) >= 0;
230}
231
232const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(sp<IBinder> displayToken) {
233 return mFlinger.mutableCurrentState().displays.valueFor(displayToken);
234}
235
236bool DisplayTransactionTest::hasDrawingDisplayState(sp<IBinder> displayToken) {
237 return mFlinger.mutableDrawingState().displays.indexOfKey(displayToken) >= 0;
238}
239
240const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(sp<IBinder> displayToken) {
241 return mFlinger.mutableDrawingState().displays.valueFor(displayToken);
242}
243
244/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800245 *
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800246 */
247
Dominik Laskowski075d3172018-05-24 15:50:06 -0700248template <typename PhysicalDisplay>
249struct PhysicalDisplayId {};
250
251template <DisplayId displayId>
252using VirtualDisplayId = std::integral_constant<DisplayId, displayId>;
253
254struct NoDisplayId {};
255
256template <typename>
257struct IsPhysicalDisplayId : std::bool_constant<false> {};
258
259template <typename PhysicalDisplay>
260struct IsPhysicalDisplayId<PhysicalDisplayId<PhysicalDisplay>> : std::bool_constant<true> {};
261
262template <typename>
263struct DisplayIdGetter;
264
265template <typename PhysicalDisplay>
266struct DisplayIdGetter<PhysicalDisplayId<PhysicalDisplay>> {
267 static std::optional<DisplayId> get() {
268 if (!PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
269 return getFallbackDisplayId(static_cast<bool>(PhysicalDisplay::PRIMARY)
270 ? HWC_DISPLAY_PRIMARY
271 : HWC_DISPLAY_EXTERNAL);
272 }
273
274 const auto info =
275 parseDisplayIdentificationData(PhysicalDisplay::PORT,
276 PhysicalDisplay::GET_IDENTIFICATION_DATA());
277 return info ? std::make_optional(info->id) : std::nullopt;
278 }
279};
280
281template <DisplayId displayId>
282struct DisplayIdGetter<VirtualDisplayId<displayId>> {
283 static std::optional<DisplayId> get() { return displayId; }
284};
285
286template <>
287struct DisplayIdGetter<NoDisplayId> {
288 static std::optional<DisplayId> get() { return {}; }
289};
290
291// DisplayIdType can be:
292// 1) PhysicalDisplayId<...> for generated ID of physical display backed by HWC.
293// 2) VirtualDisplayId<...> for hard-coded ID of virtual display backed by HWC.
294// 3) NoDisplayId for virtual display without HWC backing.
295template <typename DisplayIdType, int width, int height, Critical critical, Async async,
296 Secure secure, Primary primary, int grallocUsage>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800297struct DisplayVariant {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700298 using DISPLAY_ID = DisplayIdGetter<DisplayIdType>;
299
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800300 // The display width and height
301 static constexpr int WIDTH = width;
302 static constexpr int HEIGHT = height;
303
304 static constexpr int GRALLOC_USAGE = grallocUsage;
305
Dominik Laskowski075d3172018-05-24 15:50:06 -0700306 // Whether the display is virtual or physical
307 static constexpr Virtual VIRTUAL =
308 IsPhysicalDisplayId<DisplayIdType>{} ? Virtual::FALSE : Virtual::TRUE;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800309
310 // When creating native window surfaces for the framebuffer, whether those should be critical
311 static constexpr Critical CRITICAL = critical;
312
313 // When creating native window surfaces for the framebuffer, whether those should be async
314 static constexpr Async ASYNC = async;
315
316 // Whether the display should be treated as secure
317 static constexpr Secure SECURE = secure;
318
Dominik Laskowski075d3172018-05-24 15:50:06 -0700319 // Whether the display is primary
320 static constexpr Primary PRIMARY = primary;
321
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800322 static auto makeFakeExistingDisplayInjector(DisplayTransactionTest* test) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700323 auto injector =
324 FakeDisplayDeviceInjector(test->mFlinger, DISPLAY_ID::get(),
325 static_cast<bool>(VIRTUAL), static_cast<bool>(PRIMARY));
326
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800327 injector.setSecure(static_cast<bool>(SECURE));
Alec Mouriba013fa2018-10-16 12:43:11 -0700328 injector.setNativeWindow(test->mNativeWindow);
329
330 // Creating a DisplayDevice requires getting default dimensions from the
331 // native window.
332 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
333 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
334 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
335 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800336 return injector;
337 }
338
339 // Called by tests to set up any native window creation call expectations.
340 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
341 EXPECT_CALL(*test->mNativeWindowSurface, getNativeWindow())
342 .WillOnce(Return(test->mNativeWindow));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800343
Alec Mouriba013fa2018-10-16 12:43:11 -0700344 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
345 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
346 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
347 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800348 }
349
350 static void setupFramebufferConsumerBufferQueueCallExpectations(DisplayTransactionTest* test) {
351 EXPECT_CALL(*test->mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR));
352 EXPECT_CALL(*test->mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR));
353 EXPECT_CALL(*test->mConsumer, setConsumerUsageBits(GRALLOC_USAGE))
354 .WillRepeatedly(Return(NO_ERROR));
355 EXPECT_CALL(*test->mConsumer, setDefaultBufferSize(WIDTH, HEIGHT))
356 .WillRepeatedly(Return(NO_ERROR));
357 EXPECT_CALL(*test->mConsumer, setMaxAcquiredBufferCount(_))
358 .WillRepeatedly(Return(NO_ERROR));
359 }
360
361 static void setupFramebufferProducerBufferQueueCallExpectations(DisplayTransactionTest* test) {
362 EXPECT_CALL(*test->mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return());
363 }
364};
365
Dominik Laskowski075d3172018-05-24 15:50:06 -0700366template <hwc2_display_t hwcDisplayId, HWC2::DisplayType hwcDisplayType, typename DisplayVariant,
367 typename PhysicalDisplay = void>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800368struct HwcDisplayVariant {
369 // The display id supplied by the HWC
370 static constexpr hwc2_display_t HWC_DISPLAY_ID = hwcDisplayId;
371
372 // The HWC display type
373 static constexpr HWC2::DisplayType HWC_DISPLAY_TYPE = hwcDisplayType;
374
375 // The HWC active configuration id
Lloyd Pique3c085a02018-05-09 19:38:32 -0700376 static constexpr int HWC_ACTIVE_CONFIG_ID = 2001;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800377
378 static void injectPendingHotplugEvent(DisplayTransactionTest* test,
379 HWC2::Connection connection) {
380 test->mFlinger.mutablePendingHotplugEvents().emplace_back(
381 HotplugEvent{HWC_DISPLAY_ID, connection});
382 }
383
384 // Called by tests to inject a HWC display setup
385 static void injectHwcDisplay(DisplayTransactionTest* test) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700386 const auto displayId = DisplayVariant::DISPLAY_ID::get();
387 ASSERT_TRUE(displayId);
388 FakeHwcDisplayInjector(*displayId, HWC_DISPLAY_TYPE,
389 static_cast<bool>(DisplayVariant::PRIMARY))
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800390 .setHwcDisplayId(HWC_DISPLAY_ID)
391 .setWidth(DisplayVariant::WIDTH)
392 .setHeight(DisplayVariant::HEIGHT)
393 .setActiveConfig(HWC_ACTIVE_CONFIG_ID)
394 .inject(&test->mFlinger, test->mComposer);
395 }
396
397 static void setupHwcHotplugCallExpectations(DisplayTransactionTest* test) {
398 EXPECT_CALL(*test->mComposer, getDisplayType(HWC_DISPLAY_ID, _))
399 .WillOnce(DoAll(SetArgPointee<1>(static_cast<IComposerClient::DisplayType>(
400 HWC_DISPLAY_TYPE)),
401 Return(Error::NONE)));
402 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
403 EXPECT_CALL(*test->mComposer, getDisplayConfigs(HWC_DISPLAY_ID, _))
404 .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{HWC_ACTIVE_CONFIG_ID}),
405 Return(Error::NONE)));
406 EXPECT_CALL(*test->mComposer,
407 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
408 IComposerClient::Attribute::WIDTH, _))
409 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::WIDTH), Return(Error::NONE)));
410 EXPECT_CALL(*test->mComposer,
411 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
412 IComposerClient::Attribute::HEIGHT, _))
413 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::HEIGHT), Return(Error::NONE)));
414 EXPECT_CALL(*test->mComposer,
415 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
416 IComposerClient::Attribute::VSYNC_PERIOD, _))
417 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE)));
418 EXPECT_CALL(*test->mComposer,
419 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
420 IComposerClient::Attribute::DPI_X, _))
421 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
422 EXPECT_CALL(*test->mComposer,
423 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
424 IComposerClient::Attribute::DPI_Y, _))
425 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700426
427 if (PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
428 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
429 .WillOnce(DoAll(SetArgPointee<1>(PhysicalDisplay::PORT),
430 SetArgPointee<2>(PhysicalDisplay::GET_IDENTIFICATION_DATA()),
431 Return(Error::NONE)));
432 } else {
433 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
434 .WillOnce(Return(Error::UNSUPPORTED));
435 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800436 }
437
438 // Called by tests to set up HWC call expectations
439 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
440 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY_ID, _))
Lloyd Pique3c085a02018-05-09 19:38:32 -0700441 .WillRepeatedly(DoAll(SetArgPointee<1>(HWC_ACTIVE_CONFIG_ID), Return(Error::NONE)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800442 }
443};
444
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800445// Physical displays are expected to be synchronous, secure, and have a HWC display for output.
446constexpr uint32_t GRALLOC_USAGE_PHYSICAL_DISPLAY =
447 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
448
Dominik Laskowski075d3172018-05-24 15:50:06 -0700449template <hwc2_display_t hwcDisplayId, typename PhysicalDisplay, int width, int height,
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800450 Critical critical>
451struct PhysicalDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700452 : DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height, critical, Async::FALSE,
453 Secure::TRUE, PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
454 HwcDisplayVariant<hwcDisplayId, HWC2::DisplayType::Physical,
455 DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height,
456 critical, Async::FALSE, Secure::TRUE,
457 PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
458 PhysicalDisplay> {};
459
460template <bool hasIdentificationData>
461struct PrimaryDisplay {
462 static constexpr Primary PRIMARY = Primary::TRUE;
463 static constexpr uint8_t PORT = 255;
464 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
465 static constexpr auto GET_IDENTIFICATION_DATA = getInternalEdid;
466};
467
468template <bool hasIdentificationData>
469struct ExternalDisplay {
470 static constexpr Primary PRIMARY = Primary::FALSE;
471 static constexpr uint8_t PORT = 254;
472 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
473 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
474};
475
476struct TertiaryDisplay {
477 static constexpr Primary PRIMARY = Primary::FALSE;
478};
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800479
480// A primary display is a physical display that is critical
481using PrimaryDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700482 PhysicalDisplayVariant<1001, PrimaryDisplay<false>, 3840, 2160, Critical::TRUE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800483
484// An external display is physical display that is not critical.
485using ExternalDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700486 PhysicalDisplayVariant<1002, ExternalDisplay<false>, 1920, 1280, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800487
488using TertiaryDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700489 PhysicalDisplayVariant<1003, TertiaryDisplay, 1600, 1200, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800490
491// A virtual display not supported by the HWC.
492constexpr uint32_t GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY = 0;
493
494template <int width, int height, Secure secure>
495struct NonHwcVirtualDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700496 : DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
497 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY> {
498 using Base = DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
499 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>;
500
501 static void injectHwcDisplay(DisplayTransactionTest*) {}
502
503 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
504 EXPECT_CALL(*test->mComposer, getActiveConfig(_, _)).Times(0);
505 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800506
507 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
508 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
509 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
510 }
511};
512
513// A virtual display supported by the HWC.
514constexpr uint32_t GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY = GRALLOC_USAGE_HW_COMPOSER;
515
516template <int width, int height, Secure secure>
517struct HwcVirtualDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700518 : DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE, secure,
519 Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>,
520 HwcDisplayVariant<
521 1010, HWC2::DisplayType::Virtual,
522 DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
523 secure, Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>> {
524 using Base = DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
525 secure, Primary::FALSE, GRALLOC_USAGE_HW_COMPOSER>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800526 using Self = HwcVirtualDisplayVariant<width, height, secure>;
527
528 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
529 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
530 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
531 }
532
533 static void setupHwcVirtualDisplayCreationCallExpectations(DisplayTransactionTest* test) {
534 EXPECT_CALL(*test->mComposer, createVirtualDisplay(Base::WIDTH, Base::HEIGHT, _, _))
535 .WillOnce(DoAll(SetArgPointee<3>(Self::HWC_DISPLAY_ID), Return(Error::NONE)));
536 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
537 }
538};
539
540// For this variant, SurfaceFlinger should not configure itself with wide
541// display support, so the display should not be configured for wide-color
542// support.
543struct WideColorSupportNotConfiguredVariant {
544 static constexpr bool WIDE_COLOR_SUPPORTED = false;
545
546 static void injectConfigChange(DisplayTransactionTest* test) {
547 test->mFlinger.mutableHasWideColorDisplay() = false;
Peiyong Lin13effd12018-07-24 17:01:47 -0700548 test->mFlinger.mutableUseColorManagement() = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800549 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
550 }
551
552 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
553 EXPECT_CALL(*test->mComposer, getColorModes(_, _)).Times(0);
554 EXPECT_CALL(*test->mComposer, getRenderIntents(_, _, _)).Times(0);
555 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
556 }
557};
558
559// For this variant, SurfaceFlinger should configure itself with wide display
560// support, and the display should respond with an non-empty list of supported
561// color modes. Wide-color support should be configured.
562template <typename Display>
563struct WideColorP3ColorimetricSupportedVariant {
564 static constexpr bool WIDE_COLOR_SUPPORTED = true;
565
566 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700567 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800568 test->mFlinger.mutableHasWideColorDisplay() = true;
569 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
570 }
571
572 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
573 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
574 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>({ColorMode::DISPLAY_P3})),
575 Return(Error::NONE)));
576 EXPECT_CALL(*test->mComposer,
577 getRenderIntents(Display::HWC_DISPLAY_ID, ColorMode::DISPLAY_P3, _))
578 .WillOnce(DoAll(SetArgPointee<2>(
579 std::vector<RenderIntent>({RenderIntent::COLORIMETRIC})),
580 Return(Error::NONE)));
581 EXPECT_CALL(*test->mComposer,
582 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::SRGB,
583 RenderIntent::COLORIMETRIC))
584 .WillOnce(Return(Error::NONE));
585 }
586};
587
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800588// For this variant, SurfaceFlinger should configure itself with wide display
589// support, but the display should respond with an empty list of supported color
590// modes. Wide-color support for the display should not be configured.
591template <typename Display>
592struct WideColorNotSupportedVariant {
593 static constexpr bool WIDE_COLOR_SUPPORTED = false;
594
595 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700596 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800597 test->mFlinger.mutableHasWideColorDisplay() = true;
598 }
599
600 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
601 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
602 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>()), Return(Error::NONE)));
Chia-I Wu614e1422018-05-23 02:17:03 -0700603 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800604 }
605};
606
607// For this variant, the display is not a HWC display, so no HDR support should
608// be configured.
609struct NonHwcDisplayHdrSupportVariant {
610 static constexpr bool HDR10_SUPPORTED = false;
611 static constexpr bool HDR_HLG_SUPPORTED = false;
612 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
613 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
614 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
615 }
616};
617
618// For this variant, the composer should respond with a non-empty list of HDR
619// modes containing HDR10, so HDR10 support should be configured.
620template <typename Display>
621struct Hdr10SupportedVariant {
622 static constexpr bool HDR10_SUPPORTED = true;
623 static constexpr bool HDR_HLG_SUPPORTED = false;
624 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
625 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
626 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
627 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HDR10})),
628 Return(Error::NONE)));
629 }
630};
631
632// For this variant, the composer should respond with a non-empty list of HDR
633// modes containing HLG, so HLG support should be configured.
634template <typename Display>
635struct HdrHlgSupportedVariant {
636 static constexpr bool HDR10_SUPPORTED = false;
637 static constexpr bool HDR_HLG_SUPPORTED = true;
638 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
639 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
640 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
641 .WillOnce(
642 DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HLG})), Return(Error::NONE)));
643 }
644};
645
646// For this variant, the composer should respond with a non-empty list of HDR
647// modes containing DOLBY_VISION, so DOLBY_VISION support should be configured.
648template <typename Display>
649struct HdrDolbyVisionSupportedVariant {
650 static constexpr bool HDR10_SUPPORTED = false;
651 static constexpr bool HDR_HLG_SUPPORTED = false;
652 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = true;
653 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
654 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
655 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::DOLBY_VISION})),
656 Return(Error::NONE)));
657 }
658};
659
660// For this variant, the composer should respond with am empty list of HDR
661// modes, so no HDR support should be configured.
662template <typename Display>
663struct HdrNotSupportedVariant {
664 static constexpr bool HDR10_SUPPORTED = false;
665 static constexpr bool HDR_HLG_SUPPORTED = false;
666 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
667 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
668 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
669 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
670 }
671};
672
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700673struct NonHwcPerFrameMetadataSupportVariant {
674 static constexpr int PER_FRAME_METADATA_KEYS = 0;
675 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800676 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(_)).Times(0);
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700677 }
678};
679
680template <typename Display>
681struct NoPerFrameMetadataSupportVariant {
682 static constexpr int PER_FRAME_METADATA_KEYS = 0;
683 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800684 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
685 .WillOnce(Return(std::vector<PerFrameMetadataKey>()));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700686 }
687};
688
689template <typename Display>
690struct Smpte2086PerFrameMetadataSupportVariant {
691 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::SMPTE2086;
692 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800693 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
694 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700695 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X,
696 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y,
697 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X,
698 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y,
699 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X,
700 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y,
701 PerFrameMetadataKey::WHITE_POINT_X,
702 PerFrameMetadataKey::WHITE_POINT_Y,
703 PerFrameMetadataKey::MAX_LUMINANCE,
704 PerFrameMetadataKey::MIN_LUMINANCE,
Chia-I Wud7e01d72018-06-21 13:39:09 +0800705 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700706 }
707};
708
709template <typename Display>
710struct Cta861_3_PerFrameMetadataSupportVariant {
711 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::CTA861_3;
712 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800713 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
714 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700715 PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL,
716 PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL,
Chia-I Wud7e01d72018-06-21 13:39:09 +0800717 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700718 }
719};
720
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800721/* ------------------------------------------------------------------------
722 * Typical display configurations to test
723 */
724
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700725template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy,
726 typename PerFrameMetadataSupportPolicy>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800727struct Case {
728 using Display = DisplayPolicy;
729 using WideColorSupport = WideColorSupportPolicy;
730 using HdrSupport = HdrSupportPolicy;
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700731 using PerFrameMetadataSupport = PerFrameMetadataSupportPolicy;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800732};
733
734using SimplePrimaryDisplayCase =
735 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700736 HdrNotSupportedVariant<PrimaryDisplayVariant>,
737 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800738using SimpleExternalDisplayCase =
739 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700740 HdrNotSupportedVariant<ExternalDisplayVariant>,
741 NoPerFrameMetadataSupportVariant<ExternalDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800742using SimpleTertiaryDisplayCase =
743 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700744 HdrNotSupportedVariant<TertiaryDisplayVariant>,
745 NoPerFrameMetadataSupportVariant<TertiaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800746using NonHwcVirtualDisplayCase =
747 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700748 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant,
749 NonHwcPerFrameMetadataSupportVariant>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800750using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
751using HwcVirtualDisplayCase =
752 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
Lloyd Pique438e9e72018-09-04 18:06:08 -0700753 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>,
tangrobin6753a022018-08-10 10:58:54 +0800754 NoPerFrameMetadataSupportVariant<SimpleHwcVirtualDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800755using WideColorP3ColorimetricDisplayCase =
756 Case<PrimaryDisplayVariant, WideColorP3ColorimetricSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700757 HdrNotSupportedVariant<PrimaryDisplayVariant>,
758 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800759using Hdr10DisplayCase =
760 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700761 Hdr10SupportedVariant<PrimaryDisplayVariant>,
762 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800763using HdrHlgDisplayCase =
764 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700765 HdrHlgSupportedVariant<PrimaryDisplayVariant>,
766 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800767using HdrDolbyVisionDisplayCase =
768 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700769 HdrDolbyVisionSupportedVariant<PrimaryDisplayVariant>,
770 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
771using HdrSmpte2086DisplayCase =
772 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
773 HdrNotSupportedVariant<PrimaryDisplayVariant>,
774 Smpte2086PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
775using HdrCta861_3_DisplayCase =
776 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
777 HdrNotSupportedVariant<PrimaryDisplayVariant>,
778 Cta861_3_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Dominik Laskowskif07b85b2018-06-11 12:49:15 -0700779
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800780/* ------------------------------------------------------------------------
Lloyd Pique6cf11032018-01-22 18:57:44 -0800781 *
782 * SurfaceFlinger::onHotplugReceived
783 */
784
785TEST_F(DisplayTransactionTest, hotplugEnqueuesEventsForDisplayTransaction) {
786 constexpr int currentSequenceId = 123;
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700787 constexpr hwc2_display_t hwcDisplayId1 = 456;
788 constexpr hwc2_display_t hwcDisplayId2 = 654;
Lloyd Pique6cf11032018-01-22 18:57:44 -0800789
790 // --------------------------------------------------------------------
791 // Preconditions
792
793 // Set the current sequence id for accepted events
794 mFlinger.mutableComposerSequenceId() = currentSequenceId;
795
796 // Set the main thread id so that the current thread does not appear to be
797 // the main thread.
798 mFlinger.mutableMainThreadId() = std::thread::id();
799
800 // --------------------------------------------------------------------
801 // Call Expectations
802
803 // We expect invalidate() to be invoked once to trigger display transaction
804 // processing.
805 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
806
807 // --------------------------------------------------------------------
808 // Invocation
809
810 // Simulate two hotplug events (a connect and a disconnect)
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700811 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId1, HWC2::Connection::Connected);
812 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId2, HWC2::Connection::Disconnected);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800813
814 // --------------------------------------------------------------------
815 // Postconditions
816
817 // The display transaction needed flag should be set.
818 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
819
820 // All events should be in the pending event queue.
821 const auto& pendingEvents = mFlinger.mutablePendingHotplugEvents();
822 ASSERT_EQ(2u, pendingEvents.size());
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700823 EXPECT_EQ(hwcDisplayId1, pendingEvents[0].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800824 EXPECT_EQ(HWC2::Connection::Connected, pendingEvents[0].connection);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700825 EXPECT_EQ(hwcDisplayId2, pendingEvents[1].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800826 EXPECT_EQ(HWC2::Connection::Disconnected, pendingEvents[1].connection);
827}
828
829TEST_F(DisplayTransactionTest, hotplugDiscardsUnexpectedEvents) {
830 constexpr int currentSequenceId = 123;
831 constexpr int otherSequenceId = 321;
832 constexpr hwc2_display_t displayId = 456;
833
834 // --------------------------------------------------------------------
835 // Preconditions
836
837 // Set the current sequence id for accepted events
838 mFlinger.mutableComposerSequenceId() = currentSequenceId;
839
840 // Set the main thread id so that the current thread does not appear to be
841 // the main thread.
842 mFlinger.mutableMainThreadId() = std::thread::id();
843
844 // --------------------------------------------------------------------
845 // Call Expectations
846
847 // We do not expect any calls to invalidate().
848 EXPECT_CALL(*mMessageQueue, invalidate()).Times(0);
849
850 // --------------------------------------------------------------------
851 // Invocation
852
853 // Call with an unexpected sequence id
854 mFlinger.onHotplugReceived(otherSequenceId, displayId, HWC2::Connection::Invalid);
855
856 // --------------------------------------------------------------------
857 // Postconditions
858
859 // The display transaction needed flag should not be set
860 EXPECT_FALSE(hasTransactionFlagSet(eDisplayTransactionNeeded));
861
862 // There should be no pending events
863 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
864}
865
866TEST_F(DisplayTransactionTest, hotplugProcessesEnqueuedEventsIfCalledOnMainThread) {
867 constexpr int currentSequenceId = 123;
868 constexpr hwc2_display_t displayId1 = 456;
869
870 // --------------------------------------------------------------------
871 // Note:
872 // --------------------------------------------------------------------
873 // This test case is a bit tricky. We want to verify that
874 // onHotplugReceived() calls processDisplayHotplugEventsLocked(), but we
875 // don't really want to provide coverage for everything the later function
876 // does as there are specific tests for it.
877 // --------------------------------------------------------------------
878
879 // --------------------------------------------------------------------
880 // Preconditions
881
882 // Set the current sequence id for accepted events
883 mFlinger.mutableComposerSequenceId() = currentSequenceId;
884
885 // Set the main thread id so that the current thread does appear to be the
886 // main thread.
887 mFlinger.mutableMainThreadId() = std::this_thread::get_id();
888
889 // --------------------------------------------------------------------
890 // Call Expectations
891
892 // We expect invalidate() to be invoked once to trigger display transaction
893 // processing.
894 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
895
896 // --------------------------------------------------------------------
897 // Invocation
898
899 // Simulate a disconnect on a display id that is not connected. This should
900 // be enqueued by onHotplugReceived(), and dequeued by
901 // processDisplayHotplugEventsLocked(), but then ignored as invalid.
902 mFlinger.onHotplugReceived(currentSequenceId, displayId1, HWC2::Connection::Disconnected);
903
904 // --------------------------------------------------------------------
905 // Postconditions
906
907 // The display transaction needed flag should be set.
908 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
909
910 // There should be no event queued on return, as it should have been
911 // processed.
912 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
913}
914
915/* ------------------------------------------------------------------------
Lloyd Piquea482f992018-01-22 19:00:34 -0800916 * SurfaceFlinger::createDisplay
917 */
918
919TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForNonsecureDisplay) {
920 const String8 name("virtual.test");
921
922 // --------------------------------------------------------------------
923 // Call Expectations
924
925 // The call should notify the interceptor that a display was created.
926 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
927
928 // --------------------------------------------------------------------
929 // Invocation
930
931 sp<IBinder> displayToken = mFlinger.createDisplay(name, false);
932
933 // --------------------------------------------------------------------
934 // Postconditions
935
936 // The display should have been added to the current state
937 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
938 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700939 EXPECT_TRUE(display.isVirtual());
940 EXPECT_FALSE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -0800941 EXPECT_EQ(name.string(), display.displayName);
942
943 // --------------------------------------------------------------------
944 // Cleanup conditions
945
946 // Destroying the display invalidates the display state.
947 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
948}
949
950TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForSecureDisplay) {
951 const String8 name("virtual.test");
952
953 // --------------------------------------------------------------------
954 // Call Expectations
955
956 // The call should notify the interceptor that a display was created.
957 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
958
959 // --------------------------------------------------------------------
960 // Invocation
961
962 sp<IBinder> displayToken = mFlinger.createDisplay(name, true);
963
964 // --------------------------------------------------------------------
965 // Postconditions
966
967 // The display should have been added to the current state
968 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
969 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700970 EXPECT_TRUE(display.isVirtual());
971 EXPECT_TRUE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -0800972 EXPECT_EQ(name.string(), display.displayName);
973
974 // --------------------------------------------------------------------
975 // Cleanup conditions
976
977 // Destroying the display invalidates the display state.
978 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
979}
980
981/* ------------------------------------------------------------------------
982 * SurfaceFlinger::destroyDisplay
983 */
984
985TEST_F(DisplayTransactionTest, destroyDisplayClearsCurrentStateForDisplay) {
986 using Case = NonHwcVirtualDisplayCase;
987
988 // --------------------------------------------------------------------
989 // Preconditions
990
991 // A virtual display exists
992 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
993 existing.inject();
994
995 // --------------------------------------------------------------------
996 // Call Expectations
997
998 // The call should notify the interceptor that a display was created.
999 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
1000
1001 // Destroying the display invalidates the display state.
1002 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1003
1004 // --------------------------------------------------------------------
1005 // Invocation
1006
1007 mFlinger.destroyDisplay(existing.token());
1008
1009 // --------------------------------------------------------------------
1010 // Postconditions
1011
1012 // The display should have been removed from the current state
1013 EXPECT_FALSE(hasCurrentDisplayState(existing.token()));
1014
1015 // Ths display should still exist in the drawing state
1016 EXPECT_TRUE(hasDrawingDisplayState(existing.token()));
1017
1018 // The display transaction needed flasg should be set
1019 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1020}
1021
1022TEST_F(DisplayTransactionTest, destroyDisplayHandlesUnknownDisplay) {
1023 // --------------------------------------------------------------------
1024 // Preconditions
1025
1026 sp<BBinder> displayToken = new BBinder();
1027
1028 // --------------------------------------------------------------------
1029 // Invocation
1030
1031 mFlinger.destroyDisplay(displayToken);
1032}
1033
1034/* ------------------------------------------------------------------------
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001035 * SurfaceFlinger::resetDisplayState
1036 */
1037
1038TEST_F(DisplayTransactionTest, resetDisplayStateClearsState) {
1039 using Case = NonHwcVirtualDisplayCase;
1040
1041 // --------------------------------------------------------------------
1042 // Preconditions
1043
1044 // vsync is enabled and available
1045 mFlinger.mutablePrimaryHWVsyncEnabled() = true;
1046 mFlinger.mutableHWVsyncAvailable() = true;
1047
1048 // A display exists
1049 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1050 existing.inject();
1051
1052 // --------------------------------------------------------------------
1053 // Call Expectations
1054
1055 // The call disable vsyncs
1056 EXPECT_CALL(*mEventControlThread, setVsyncEnabled(false)).Times(1);
1057
Lloyd Pique41be5d22018-06-21 13:11:48 -07001058 // The call ends any display resyncs
1059 EXPECT_CALL(*mPrimaryDispSync, endResync()).Times(1);
1060
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001061 // --------------------------------------------------------------------
1062 // Invocation
1063
1064 mFlinger.resetDisplayState();
1065
1066 // --------------------------------------------------------------------
1067 // Postconditions
1068
1069 // vsyncs should be off and not available.
1070 EXPECT_FALSE(mFlinger.mutablePrimaryHWVsyncEnabled());
1071 EXPECT_FALSE(mFlinger.mutableHWVsyncAvailable());
1072
1073 // The display should have been removed from the display map.
1074 EXPECT_FALSE(hasDisplayDevice(existing.token()));
1075
1076 // The display should still exist in the current state
1077 EXPECT_TRUE(hasCurrentDisplayState(existing.token()));
1078
1079 // The display should have been removed from the drawing state
1080 EXPECT_FALSE(hasDrawingDisplayState(existing.token()));
1081}
1082
1083/* ------------------------------------------------------------------------
Valerie Hau9758ae02018-10-09 16:05:09 -07001084 * DisplayDevice::GetBestColorMode
1085 */
1086class GetBestColorModeTest : public DisplayTransactionTest {
1087public:
Dominik Laskowski075d3172018-05-24 15:50:06 -07001088 static constexpr DisplayId DEFAULT_DISPLAY_ID = 777;
1089
Valerie Hau9758ae02018-10-09 16:05:09 -07001090 GetBestColorModeTest()
1091 : DisplayTransactionTest(),
Dominik Laskowski075d3172018-05-24 15:50:06 -07001092 mInjector(FakeDisplayDeviceInjector(mFlinger, DEFAULT_DISPLAY_ID, false /* isVirtual */,
1093 true /* isPrimary */)) {}
Valerie Hau9758ae02018-10-09 16:05:09 -07001094
1095 void setHasWideColorGamut(bool hasWideColorGamut) { mHasWideColorGamut = hasWideColorGamut; }
1096
1097 void addHwcColorModesMapping(ui::ColorMode colorMode,
1098 std::vector<ui::RenderIntent> renderIntents) {
1099 mHwcColorModes[colorMode] = renderIntents;
1100 }
1101
1102 void setInputDataspace(ui::Dataspace dataspace) { mInputDataspace = dataspace; }
1103
1104 void setInputRenderIntent(ui::RenderIntent renderIntent) { mInputRenderIntent = renderIntent; }
1105
1106 void getBestColorMode() {
1107 mInjector.setHwcColorModes(mHwcColorModes);
1108 mInjector.setHasWideColorGamut(mHasWideColorGamut);
Alec Mouriba013fa2018-10-16 12:43:11 -07001109 mInjector.setNativeWindow(mNativeWindow);
1110
1111 // Creating a DisplayDevice requires getting default dimensions from the
1112 // native window.
1113 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
1114 .WillRepeatedly(DoAll(SetArgPointee<1>(1080 /* arbitrary */), Return(0)));
1115 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
1116 .WillRepeatedly(DoAll(SetArgPointee<1>(1920 /* arbitrary */), Return(0)));
Alec Mouri8147b0e2018-10-09 20:57:19 -07001117 EXPECT_CALL(*mNativeWindow, perform(13)).Times(1);
Valerie Hau9758ae02018-10-09 16:05:09 -07001118 auto displayDevice = mInjector.inject();
1119
1120 displayDevice->getBestColorMode(mInputDataspace, mInputRenderIntent, &mOutDataspace,
1121 &mOutColorMode, &mOutRenderIntent);
1122 }
1123
1124 ui::Dataspace mOutDataspace;
1125 ui::ColorMode mOutColorMode;
1126 ui::RenderIntent mOutRenderIntent;
1127
1128private:
1129 ui::Dataspace mInputDataspace;
1130 ui::RenderIntent mInputRenderIntent;
1131 bool mHasWideColorGamut = false;
1132 std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> mHwcColorModes;
1133 FakeDisplayDeviceInjector mInjector;
1134};
1135
1136TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeSRGB) {
1137 addHwcColorModesMapping(ui::ColorMode::SRGB,
1138 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1139 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1140 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1141 setHasWideColorGamut(true);
1142
1143 getBestColorMode();
1144
1145 ASSERT_EQ(ui::Dataspace::SRGB, mOutDataspace);
1146 ASSERT_EQ(ui::ColorMode::SRGB, mOutColorMode);
1147 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1148}
1149
1150TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDisplayP3) {
1151 addHwcColorModesMapping(ui::ColorMode::DISPLAY_P3,
1152 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1153 addHwcColorModesMapping(ui::ColorMode::SRGB,
1154 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1155 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1156 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1157 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1158 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1159 setHasWideColorGamut(true);
1160
1161 getBestColorMode();
1162
1163 ASSERT_EQ(ui::Dataspace::DISPLAY_P3, mOutDataspace);
1164 ASSERT_EQ(ui::ColorMode::DISPLAY_P3, mOutColorMode);
1165 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1166}
1167
1168TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDISPLAY_BT2020) {
1169 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1170 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1171 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1172 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1173 setHasWideColorGamut(true);
1174
1175 getBestColorMode();
1176
1177 ASSERT_EQ(ui::Dataspace::DISPLAY_BT2020, mOutDataspace);
1178 ASSERT_EQ(ui::ColorMode::DISPLAY_BT2020, mOutColorMode);
1179 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1180}
1181
1182/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001183 * SurfaceFlinger::setupNewDisplayDeviceInternal
1184 */
1185
1186class SetupNewDisplayDeviceInternalTest : public DisplayTransactionTest {
1187public:
1188 template <typename T>
1189 void setupNewDisplayDeviceInternalTest();
1190};
1191
1192template <typename Case>
1193void SetupNewDisplayDeviceInternalTest::setupNewDisplayDeviceInternalTest() {
1194 const sp<BBinder> displayToken = new BBinder();
1195 const sp<mock::DisplaySurface> displaySurface = new mock::DisplaySurface();
1196 const sp<mock::GraphicBufferProducer> producer = new mock::GraphicBufferProducer();
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001197
1198 // --------------------------------------------------------------------
1199 // Preconditions
1200
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001201 // Wide color displays support is configured appropriately
1202 Case::WideColorSupport::injectConfigChange(this);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001203
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001204 // The display is setup with the HWC.
1205 Case::Display::injectHwcDisplay(this);
1206
1207 // SurfaceFlinger will use a test-controlled factory for native window
1208 // surfaces.
1209 injectFakeNativeWindowSurfaceFactory();
1210
1211 // --------------------------------------------------------------------
1212 // Call Expectations
1213
1214 // Various native window calls will be made.
1215 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
Lloyd Pique3c085a02018-05-09 19:38:32 -07001216 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001217 Case::WideColorSupport::setupComposerCallExpectations(this);
1218 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001219 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001220
1221 // --------------------------------------------------------------------
1222 // Invocation
1223
1224 DisplayDeviceState state;
Dominik Laskowski075d3172018-05-24 15:50:06 -07001225 state.displayId = static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1226 : Case::Display::DISPLAY_ID::get();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001227 state.isSecure = static_cast<bool>(Case::Display::SECURE);
1228
Dominik Laskowski075d3172018-05-24 15:50:06 -07001229 auto device =
1230 mFlinger.setupNewDisplayDeviceInternal(displayToken, Case::Display::DISPLAY_ID::get(),
1231 state, displaySurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001232
1233 // --------------------------------------------------------------------
1234 // Postconditions
1235
1236 ASSERT_TRUE(device != nullptr);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001237 EXPECT_EQ(Case::Display::DISPLAY_ID::get(), device->getId());
1238 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), device->isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001239 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001240 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001241 EXPECT_EQ(Case::Display::WIDTH, device->getWidth());
1242 EXPECT_EQ(Case::Display::HEIGHT, device->getHeight());
1243 EXPECT_EQ(Case::WideColorSupport::WIDE_COLOR_SUPPORTED, device->hasWideColorGamut());
1244 EXPECT_EQ(Case::HdrSupport::HDR10_SUPPORTED, device->hasHDR10Support());
1245 EXPECT_EQ(Case::HdrSupport::HDR_HLG_SUPPORTED, device->hasHLGSupport());
1246 EXPECT_EQ(Case::HdrSupport::HDR_DOLBY_VISION_SUPPORTED, device->hasDolbyVisionSupport());
Lloyd Pique3c085a02018-05-09 19:38:32 -07001247 // Note: This is not Case::Display::HWC_ACTIVE_CONFIG_ID as the ids are
1248 // remapped, and the test only ever sets up one config. If there were an error
1249 // looking up the remapped index, device->getActiveConfig() would be -1 instead.
1250 EXPECT_EQ(0, device->getActiveConfig());
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001251 EXPECT_EQ(Case::PerFrameMetadataSupport::PER_FRAME_METADATA_KEYS,
1252 device->getSupportedPerFrameMetadata());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001253}
1254
1255TEST_F(SetupNewDisplayDeviceInternalTest, createSimplePrimaryDisplay) {
1256 setupNewDisplayDeviceInternalTest<SimplePrimaryDisplayCase>();
1257}
1258
1259TEST_F(SetupNewDisplayDeviceInternalTest, createSimpleExternalDisplay) {
1260 setupNewDisplayDeviceInternalTest<SimpleExternalDisplayCase>();
1261}
1262
1263TEST_F(SetupNewDisplayDeviceInternalTest, createNonHwcVirtualDisplay) {
1264 setupNewDisplayDeviceInternalTest<NonHwcVirtualDisplayCase>();
1265}
1266
1267TEST_F(SetupNewDisplayDeviceInternalTest, createHwcVirtualDisplay) {
Dominik Laskowski075d3172018-05-24 15:50:06 -07001268 using Case = HwcVirtualDisplayCase;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001269
Dominik Laskowski075d3172018-05-24 15:50:06 -07001270 // Insert display data so that the HWC thinks it created the virtual display.
1271 const auto displayId = Case::Display::DISPLAY_ID::get();
1272 ASSERT_TRUE(displayId);
1273 mFlinger.mutableHwcDisplayData()[*displayId] = {};
1274
1275 setupNewDisplayDeviceInternalTest<Case>();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001276}
1277
1278TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3Display) {
1279 setupNewDisplayDeviceInternalTest<WideColorP3ColorimetricDisplayCase>();
1280}
1281
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001282TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10Display) {
1283 setupNewDisplayDeviceInternalTest<Hdr10DisplayCase>();
1284}
1285
1286TEST_F(SetupNewDisplayDeviceInternalTest, createHdrHlgDisplay) {
1287 setupNewDisplayDeviceInternalTest<HdrHlgDisplayCase>();
1288}
1289
1290TEST_F(SetupNewDisplayDeviceInternalTest, createHdrDolbyVisionDisplay) {
1291 setupNewDisplayDeviceInternalTest<HdrDolbyVisionDisplayCase>();
1292}
1293
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001294TEST_F(SetupNewDisplayDeviceInternalTest, createHdrSmpte2086DisplayCase) {
1295 setupNewDisplayDeviceInternalTest<HdrSmpte2086DisplayCase>();
1296}
1297
1298TEST_F(SetupNewDisplayDeviceInternalTest, createHdrCta816_3_DisplayCase) {
1299 setupNewDisplayDeviceInternalTest<HdrCta861_3_DisplayCase>();
1300}
1301
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001302/* ------------------------------------------------------------------------
1303 * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded)
1304 */
1305
1306class HandleTransactionLockedTest : public DisplayTransactionTest {
1307public:
1308 template <typename Case>
1309 void setupCommonPreconditions();
1310
1311 template <typename Case>
1312 void setupCommonCallExpectationsForConnectProcessing();
1313
1314 template <typename Case>
1315 void setupCommonCallExpectationsForDisconnectProcessing();
1316
1317 template <typename Case>
1318 void processesHotplugConnectCommon();
1319
1320 template <typename Case>
1321 void ignoresHotplugConnectCommon();
1322
1323 template <typename Case>
1324 void processesHotplugDisconnectCommon();
1325
1326 template <typename Case>
1327 void verifyDisplayIsConnected(const sp<IBinder>& displayToken);
1328
1329 template <typename Case>
1330 void verifyPhysicalDisplayIsConnected();
1331
1332 void verifyDisplayIsNotConnected(const sp<IBinder>& displayToken);
1333};
1334
1335template <typename Case>
1336void HandleTransactionLockedTest::setupCommonPreconditions() {
1337 // Wide color displays support is configured appropriately
1338 Case::WideColorSupport::injectConfigChange(this);
1339
1340 // SurfaceFlinger will use a test-controlled factory for BufferQueues
1341 injectFakeBufferQueueFactory();
1342
1343 // SurfaceFlinger will use a test-controlled factory for native window
1344 // surfaces.
1345 injectFakeNativeWindowSurfaceFactory();
1346}
1347
1348template <typename Case>
1349void HandleTransactionLockedTest::setupCommonCallExpectationsForConnectProcessing() {
1350 Case::Display::setupHwcHotplugCallExpectations(this);
1351
1352 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1353 Case::Display::setupFramebufferProducerBufferQueueCallExpectations(this);
1354 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1355 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
1356
1357 Case::WideColorSupport::setupComposerCallExpectations(this);
1358 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001359 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001360
1361 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001362 EXPECT_CALL(*mEventThread,
Dominik Laskowski075d3172018-05-24 15:50:06 -07001363 onHotplugReceived(static_cast<bool>(Case::Display::PRIMARY)
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001364 ? EventThread::DisplayType::Primary
1365 : EventThread::DisplayType::External,
1366 true))
1367 .Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001368}
1369
1370template <typename Case>
1371void HandleTransactionLockedTest::setupCommonCallExpectationsForDisconnectProcessing() {
1372 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001373 EXPECT_CALL(*mEventThread,
Dominik Laskowski075d3172018-05-24 15:50:06 -07001374 onHotplugReceived(static_cast<bool>(Case::Display::PRIMARY)
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001375 ? EventThread::DisplayType::Primary
1376 : EventThread::DisplayType::External,
1377 false))
1378 .Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001379}
1380
1381template <typename Case>
1382void HandleTransactionLockedTest::verifyDisplayIsConnected(const sp<IBinder>& displayToken) {
1383 // The display device should have been set up in the list of displays.
1384 ASSERT_TRUE(hasDisplayDevice(displayToken));
1385 const auto& device = getDisplayDevice(displayToken);
1386 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001387 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001388
1389 // The display should have been set up in the current display state
1390 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1391 const auto& current = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001392 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), current.isVirtual());
1393 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1394 : Case::Display::DISPLAY_ID::get(),
1395 current.displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001396
1397 // The display should have been set up in the drawing display state
1398 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1399 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001400 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
1401 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1402 : Case::Display::DISPLAY_ID::get(),
1403 draw.displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001404}
1405
1406template <typename Case>
1407void HandleTransactionLockedTest::verifyPhysicalDisplayIsConnected() {
1408 // HWComposer should have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001409 EXPECT_TRUE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001410
Dominik Laskowski075d3172018-05-24 15:50:06 -07001411 // SF should have a display token.
1412 const auto displayId = Case::Display::DISPLAY_ID::get();
1413 ASSERT_TRUE(displayId);
1414 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1415 auto& displayToken = mFlinger.mutablePhysicalDisplayTokens()[*displayId];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001416
1417 verifyDisplayIsConnected<Case>(displayToken);
1418}
1419
1420void HandleTransactionLockedTest::verifyDisplayIsNotConnected(const sp<IBinder>& displayToken) {
1421 EXPECT_FALSE(hasDisplayDevice(displayToken));
1422 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1423 EXPECT_FALSE(hasDrawingDisplayState(displayToken));
1424}
1425
1426template <typename Case>
1427void HandleTransactionLockedTest::processesHotplugConnectCommon() {
1428 // --------------------------------------------------------------------
1429 // Preconditions
1430
1431 setupCommonPreconditions<Case>();
1432
1433 // A hotplug connect event is enqueued for a display
1434 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001435
1436 // --------------------------------------------------------------------
1437 // Call Expectations
1438
1439 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillOnce(Return(false));
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001440
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001441 setupCommonCallExpectationsForConnectProcessing<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001442
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001443 // --------------------------------------------------------------------
1444 // Invocation
Lloyd Piquee39cad22017-12-20 17:01:29 -08001445
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001446 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
Lloyd Piquee39cad22017-12-20 17:01:29 -08001447
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001448 // --------------------------------------------------------------------
1449 // Postconditions
1450
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001451 verifyPhysicalDisplayIsConnected<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001452
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001453 // --------------------------------------------------------------------
1454 // Cleanup conditions
1455
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001456 EXPECT_CALL(*mComposer,
1457 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001458 .WillOnce(Return(Error::NONE));
1459 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Piquef58625d2017-12-19 13:22:33 -08001460}
1461
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001462template <typename Case>
1463void HandleTransactionLockedTest::ignoresHotplugConnectCommon() {
1464 // --------------------------------------------------------------------
1465 // Preconditions
1466
1467 setupCommonPreconditions<Case>();
1468
1469 // A hotplug connect event is enqueued for a display
1470 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1471
1472 // --------------------------------------------------------------------
1473 // Invocation
1474
1475 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1476
1477 // --------------------------------------------------------------------
1478 // Postconditions
1479
1480 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001481 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001482}
1483
1484template <typename Case>
1485void HandleTransactionLockedTest::processesHotplugDisconnectCommon() {
1486 // --------------------------------------------------------------------
1487 // Preconditions
1488
1489 setupCommonPreconditions<Case>();
1490
1491 // A hotplug disconnect event is enqueued for a display
1492 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1493
1494 // The display is already completely set up.
1495 Case::Display::injectHwcDisplay(this);
1496 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1497 existing.inject();
1498
1499 // --------------------------------------------------------------------
1500 // Call Expectations
1501
1502 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
Lloyd Pique438e9e72018-09-04 18:06:08 -07001503 EXPECT_CALL(*mComposer, getDisplayIdentificationData(Case::Display::HWC_DISPLAY_ID, _, _))
1504 .Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001505
1506 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1507
1508 // --------------------------------------------------------------------
1509 // Invocation
1510
1511 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1512
1513 // --------------------------------------------------------------------
1514 // Postconditions
1515
1516 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001517 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001518
Dominik Laskowski075d3172018-05-24 15:50:06 -07001519 // SF should not have a display token.
1520 const auto displayId = Case::Display::DISPLAY_ID::get();
1521 ASSERT_TRUE(displayId);
1522 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001523
1524 // The existing token should have been removed
1525 verifyDisplayIsNotConnected(existing.token());
1526}
1527
1528TEST_F(HandleTransactionLockedTest, processesHotplugConnectPrimaryDisplay) {
1529 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1530}
1531
1532TEST_F(HandleTransactionLockedTest,
1533 processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected) {
1534 // Inject an external display.
1535 ExternalDisplayVariant::injectHwcDisplay(this);
1536
1537 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1538}
1539
1540TEST_F(HandleTransactionLockedTest, processesHotplugConnectExternalDisplay) {
1541 // Inject a primary display.
1542 PrimaryDisplayVariant::injectHwcDisplay(this);
1543
1544 processesHotplugConnectCommon<SimpleExternalDisplayCase>();
1545}
1546
1547TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected) {
1548 // Inject both a primary and external display.
1549 PrimaryDisplayVariant::injectHwcDisplay(this);
1550 ExternalDisplayVariant::injectHwcDisplay(this);
1551
1552 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1553
1554 ignoresHotplugConnectCommon<SimpleTertiaryDisplayCase>();
1555}
1556
1557TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfExternalForVrComposer) {
1558 // Inject a primary display.
1559 PrimaryDisplayVariant::injectHwcDisplay(this);
1560
1561 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(true));
1562
1563 ignoresHotplugConnectCommon<SimpleExternalDisplayCase>();
1564}
1565
1566TEST_F(HandleTransactionLockedTest, processHotplugDisconnectPrimaryDisplay) {
1567 processesHotplugDisconnectCommon<SimplePrimaryDisplayCase>();
1568}
1569
1570TEST_F(HandleTransactionLockedTest, processHotplugDisconnectExternalDisplay) {
1571 processesHotplugDisconnectCommon<SimpleExternalDisplayCase>();
1572}
1573
1574TEST_F(HandleTransactionLockedTest, processesHotplugConnectThenDisconnectPrimary) {
1575 using Case = SimplePrimaryDisplayCase;
1576
1577 // --------------------------------------------------------------------
1578 // Preconditions
1579
1580 setupCommonPreconditions<Case>();
1581
1582 // A hotplug connect event is enqueued for a display
1583 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1584 // A hotplug disconnect event is also enqueued for the same display
1585 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1586
1587 // --------------------------------------------------------------------
1588 // Call Expectations
1589
1590 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1591
1592 setupCommonCallExpectationsForConnectProcessing<Case>();
1593 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1594
1595 EXPECT_CALL(*mComposer,
1596 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1597 .WillOnce(Return(Error::NONE));
1598 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1599
1600 // --------------------------------------------------------------------
1601 // Invocation
1602
1603 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1604
1605 // --------------------------------------------------------------------
1606 // Postconditions
1607
1608 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001609 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001610
Dominik Laskowski075d3172018-05-24 15:50:06 -07001611 // SF should not have a display token.
1612 const auto displayId = Case::Display::DISPLAY_ID::get();
1613 ASSERT_TRUE(displayId);
1614 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001615}
1616
1617TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectThenConnectPrimary) {
1618 using Case = SimplePrimaryDisplayCase;
1619
1620 // --------------------------------------------------------------------
1621 // Preconditions
1622
1623 setupCommonPreconditions<Case>();
1624
1625 // The display is already completely set up.
1626 Case::Display::injectHwcDisplay(this);
1627 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1628 existing.inject();
1629
1630 // A hotplug disconnect event is enqueued for a display
1631 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1632 // A hotplug connect event is also enqueued for the same display
1633 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1634
1635 // --------------------------------------------------------------------
1636 // Call Expectations
1637
1638 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1639
1640 setupCommonCallExpectationsForConnectProcessing<Case>();
1641 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1642
1643 // --------------------------------------------------------------------
1644 // Invocation
1645
1646 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1647
1648 // --------------------------------------------------------------------
1649 // Postconditions
1650
1651 // The existing token should have been removed
1652 verifyDisplayIsNotConnected(existing.token());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001653 const auto displayId = Case::Display::DISPLAY_ID::get();
1654 ASSERT_TRUE(displayId);
1655 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1656 EXPECT_NE(existing.token(), mFlinger.mutablePhysicalDisplayTokens()[*displayId]);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001657
1658 // A new display should be connected in its place
1659
1660 verifyPhysicalDisplayIsConnected<Case>();
1661
1662 // --------------------------------------------------------------------
1663 // Cleanup conditions
1664
1665 EXPECT_CALL(*mComposer,
1666 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1667 .WillOnce(Return(Error::NONE));
1668 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1669}
1670
1671TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAdded) {
1672 using Case = HwcVirtualDisplayCase;
1673
1674 // --------------------------------------------------------------------
1675 // Preconditions
1676
1677 // The HWC supports at least one virtual display
1678 injectMockComposer(1);
1679
1680 setupCommonPreconditions<Case>();
1681
1682 // A virtual display was added to the current state, and it has a
1683 // surface(producer)
1684 sp<BBinder> displayToken = new BBinder();
Lloyd Pique4c2ac022018-04-27 12:08:03 -07001685
Dominik Laskowski075d3172018-05-24 15:50:06 -07001686 DisplayDeviceState state;
1687 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001688
1689 sp<mock::GraphicBufferProducer> surface{new mock::GraphicBufferProducer()};
Dominik Laskowski075d3172018-05-24 15:50:06 -07001690 state.surface = surface;
1691 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001692
1693 // --------------------------------------------------------------------
1694 // Call Expectations
1695
1696 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1697 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1698
1699 EXPECT_CALL(*surface, query(NATIVE_WINDOW_WIDTH, _))
1700 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::WIDTH), Return(NO_ERROR)));
1701 EXPECT_CALL(*surface, query(NATIVE_WINDOW_HEIGHT, _))
1702 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::HEIGHT), Return(NO_ERROR)));
1703 EXPECT_CALL(*surface, query(NATIVE_WINDOW_FORMAT, _))
1704 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT),
1705 Return(NO_ERROR)));
1706 EXPECT_CALL(*surface, query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, _))
1707 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(NO_ERROR)));
1708
1709 EXPECT_CALL(*surface, setAsyncMode(true)).Times(1);
1710
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001711 EXPECT_CALL(*mProducer, disconnect(_, _)).Times(1);
1712
1713 Case::Display::setupHwcVirtualDisplayCreationCallExpectations(this);
1714 Case::WideColorSupport::setupComposerCallExpectations(this);
1715 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001716 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001717
1718 // --------------------------------------------------------------------
1719 // Invocation
1720
1721 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1722
1723 // --------------------------------------------------------------------
1724 // Postconditions
1725
1726 // The display device should have been set up in the list of displays.
1727 verifyDisplayIsConnected<Case>(displayToken);
1728
1729 // --------------------------------------------------------------------
1730 // Cleanup conditions
1731
1732 EXPECT_CALL(*mComposer, destroyVirtualDisplay(Case::Display::HWC_DISPLAY_ID))
1733 .WillOnce(Return(Error::NONE));
1734 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1735}
1736
1737TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAddedWithNoSurface) {
1738 using Case = HwcVirtualDisplayCase;
1739
1740 // --------------------------------------------------------------------
1741 // Preconditions
1742
1743 // The HWC supports at least one virtual display
1744 injectMockComposer(1);
1745
1746 setupCommonPreconditions<Case>();
1747
1748 // A virtual display was added to the current state, but it does not have a
1749 // surface.
1750 sp<BBinder> displayToken = new BBinder();
1751
Dominik Laskowski075d3172018-05-24 15:50:06 -07001752 DisplayDeviceState state;
1753 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001754
Dominik Laskowski075d3172018-05-24 15:50:06 -07001755 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001756
1757 // --------------------------------------------------------------------
1758 // Call Expectations
1759
1760 // --------------------------------------------------------------------
1761 // Invocation
1762
1763 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1764
1765 // --------------------------------------------------------------------
1766 // Postconditions
1767
1768 // There will not be a display device set up.
1769 EXPECT_FALSE(hasDisplayDevice(displayToken));
1770
1771 // The drawing display state will be set from the current display state.
1772 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1773 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001774 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001775}
1776
1777TEST_F(HandleTransactionLockedTest, processesVirtualDisplayRemoval) {
1778 using Case = HwcVirtualDisplayCase;
1779
1780 // --------------------------------------------------------------------
1781 // Preconditions
1782
1783 // A virtual display is set up but is removed from the current state.
Dominik Laskowski075d3172018-05-24 15:50:06 -07001784 const auto displayId = Case::Display::DISPLAY_ID::get();
1785 ASSERT_TRUE(displayId);
1786 mFlinger.mutableHwcDisplayData()[*displayId] = {};
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001787 Case::Display::injectHwcDisplay(this);
1788 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1789 existing.inject();
1790 mFlinger.mutableCurrentState().displays.removeItem(existing.token());
1791
1792 // --------------------------------------------------------------------
1793 // Call Expectations
1794
1795 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1796
1797 // --------------------------------------------------------------------
1798 // Invocation
1799
1800 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1801
1802 // --------------------------------------------------------------------
1803 // Postconditions
1804
1805 // The existing token should have been removed
1806 verifyDisplayIsNotConnected(existing.token());
1807}
1808
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001809TEST_F(HandleTransactionLockedTest, processesDisplayLayerStackChanges) {
1810 using Case = NonHwcVirtualDisplayCase;
1811
1812 constexpr uint32_t oldLayerStack = 0u;
1813 constexpr uint32_t newLayerStack = 123u;
1814
1815 // --------------------------------------------------------------------
1816 // Preconditions
1817
1818 // A display is set up
1819 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1820 display.inject();
1821
1822 // There is a change to the layerStack state
1823 display.mutableDrawingDisplayState().layerStack = oldLayerStack;
1824 display.mutableCurrentDisplayState().layerStack = newLayerStack;
1825
1826 // --------------------------------------------------------------------
1827 // Invocation
1828
1829 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1830
1831 // --------------------------------------------------------------------
1832 // Postconditions
1833
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001834 EXPECT_EQ(newLayerStack, display.mutableDisplayDevice()->getLayerStack());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001835}
1836
1837TEST_F(HandleTransactionLockedTest, processesDisplayTransformChanges) {
1838 using Case = NonHwcVirtualDisplayCase;
1839
1840 constexpr int oldTransform = 0;
1841 constexpr int newTransform = 2;
1842
1843 // --------------------------------------------------------------------
1844 // Preconditions
1845
1846 // A display is set up
1847 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1848 display.inject();
1849
1850 // There is a change to the orientation state
1851 display.mutableDrawingDisplayState().orientation = oldTransform;
1852 display.mutableCurrentDisplayState().orientation = newTransform;
1853
1854 // --------------------------------------------------------------------
1855 // Invocation
1856
1857 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1858
1859 // --------------------------------------------------------------------
1860 // Postconditions
1861
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001862 EXPECT_EQ(newTransform, display.mutableDisplayDevice()->getOrientation());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001863}
1864
1865TEST_F(HandleTransactionLockedTest, processesDisplayViewportChanges) {
1866 using Case = NonHwcVirtualDisplayCase;
1867
1868 const Rect oldViewport(0, 0, 0, 0);
1869 const Rect newViewport(0, 0, 123, 456);
1870
1871 // --------------------------------------------------------------------
1872 // Preconditions
1873
1874 // A display is set up
1875 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1876 display.inject();
1877
1878 // There is a change to the viewport state
1879 display.mutableDrawingDisplayState().viewport = oldViewport;
1880 display.mutableCurrentDisplayState().viewport = newViewport;
1881
1882 // --------------------------------------------------------------------
1883 // Invocation
1884
1885 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1886
1887 // --------------------------------------------------------------------
1888 // Postconditions
1889
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001890 EXPECT_EQ(newViewport, display.mutableDisplayDevice()->getViewport());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001891}
1892
1893TEST_F(HandleTransactionLockedTest, processesDisplayFrameChanges) {
1894 using Case = NonHwcVirtualDisplayCase;
1895
1896 const Rect oldFrame(0, 0, 0, 0);
1897 const Rect newFrame(0, 0, 123, 456);
1898
1899 // --------------------------------------------------------------------
1900 // Preconditions
1901
1902 // A display is set up
1903 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1904 display.inject();
1905
1906 // There is a change to the viewport state
1907 display.mutableDrawingDisplayState().frame = oldFrame;
1908 display.mutableCurrentDisplayState().frame = newFrame;
1909
1910 // --------------------------------------------------------------------
1911 // Invocation
1912
1913 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1914
1915 // --------------------------------------------------------------------
1916 // Postconditions
1917
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001918 EXPECT_EQ(newFrame, display.mutableDisplayDevice()->getFrame());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001919}
1920
1921TEST_F(HandleTransactionLockedTest, processesDisplayWidthChanges) {
1922 using Case = NonHwcVirtualDisplayCase;
1923
1924 constexpr int oldWidth = 0;
1925 constexpr int oldHeight = 10;
1926 constexpr int newWidth = 123;
1927
1928 // --------------------------------------------------------------------
1929 // Preconditions
1930
1931 // A display is set up
1932 auto nativeWindow = new mock::NativeWindow();
1933 auto displaySurface = new mock::DisplaySurface();
Alec Mouri8147b0e2018-10-09 20:57:19 -07001934 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001935 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1936 display.setNativeWindow(nativeWindow);
1937 display.setDisplaySurface(displaySurface);
Alec Mouri8147b0e2018-10-09 20:57:19 -07001938 // Setup injection expections
1939 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
1940 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
1941 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
1942 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
1943 EXPECT_CALL(*nativeWindow, perform(13)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001944 display.inject();
1945
1946 // There is a change to the viewport state
1947 display.mutableDrawingDisplayState().width = oldWidth;
1948 display.mutableDrawingDisplayState().height = oldHeight;
1949 display.mutableCurrentDisplayState().width = newWidth;
1950 display.mutableCurrentDisplayState().height = oldHeight;
1951
1952 // --------------------------------------------------------------------
1953 // Call Expectations
1954
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001955 EXPECT_CALL(*displaySurface, resizeBuffers(newWidth, oldHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001956
1957 // --------------------------------------------------------------------
1958 // Invocation
1959
1960 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1961}
1962
1963TEST_F(HandleTransactionLockedTest, processesDisplayHeightChanges) {
1964 using Case = NonHwcVirtualDisplayCase;
1965
1966 constexpr int oldWidth = 0;
1967 constexpr int oldHeight = 10;
1968 constexpr int newHeight = 123;
1969
1970 // --------------------------------------------------------------------
1971 // Preconditions
1972
1973 // A display is set up
1974 auto nativeWindow = new mock::NativeWindow();
1975 auto displaySurface = new mock::DisplaySurface();
Alec Mouri8147b0e2018-10-09 20:57:19 -07001976 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001977 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1978 display.setNativeWindow(nativeWindow);
1979 display.setDisplaySurface(displaySurface);
Alec Mouri8147b0e2018-10-09 20:57:19 -07001980 // Setup injection expections
1981 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
1982 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
1983 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
1984 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
1985 EXPECT_CALL(*nativeWindow, perform(13)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001986 display.inject();
1987
1988 // There is a change to the viewport state
1989 display.mutableDrawingDisplayState().width = oldWidth;
1990 display.mutableDrawingDisplayState().height = oldHeight;
1991 display.mutableCurrentDisplayState().width = oldWidth;
1992 display.mutableCurrentDisplayState().height = newHeight;
1993
1994 // --------------------------------------------------------------------
1995 // Call Expectations
1996
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001997 EXPECT_CALL(*displaySurface, resizeBuffers(oldWidth, newHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001998
1999 // --------------------------------------------------------------------
2000 // Invocation
2001
2002 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2003}
2004
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002005/* ------------------------------------------------------------------------
2006 * SurfaceFlinger::setDisplayStateLocked
2007 */
2008
2009TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithUnknownDisplay) {
2010 // --------------------------------------------------------------------
2011 // Preconditions
2012
2013 // We have an unknown display token not associated with a known display
2014 sp<BBinder> displayToken = new BBinder();
2015
2016 // The requested display state references the unknown display.
2017 DisplayState state;
2018 state.what = DisplayState::eLayerStackChanged;
2019 state.token = displayToken;
2020 state.layerStack = 456;
2021
2022 // --------------------------------------------------------------------
2023 // Invocation
2024
2025 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2026
2027 // --------------------------------------------------------------------
2028 // Postconditions
2029
2030 // The returned flags are empty
2031 EXPECT_EQ(0u, flags);
2032
2033 // The display token still doesn't match anything known.
2034 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
2035}
2036
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002037TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWhenNoChanges) {
2038 using Case = SimplePrimaryDisplayCase;
2039
2040 // --------------------------------------------------------------------
2041 // Preconditions
2042
2043 // A display is already set up
2044 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2045 display.inject();
2046
2047 // No changes are made to the display
2048 DisplayState state;
2049 state.what = 0;
2050 state.token = display.token();
2051
2052 // --------------------------------------------------------------------
2053 // Invocation
2054
2055 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2056
2057 // --------------------------------------------------------------------
2058 // Postconditions
2059
2060 // The returned flags are empty
2061 EXPECT_EQ(0u, flags);
2062}
2063
2064TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSurfaceDidNotChange) {
2065 using Case = SimplePrimaryDisplayCase;
2066
2067 // --------------------------------------------------------------------
2068 // Preconditions
2069
2070 // A display is already set up
2071 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2072 display.inject();
2073
2074 // There is a surface that can be set.
2075 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2076
2077 // The current display state has the surface set
2078 display.mutableCurrentDisplayState().surface = surface;
2079
2080 // The incoming request sets the same surface
2081 DisplayState state;
2082 state.what = DisplayState::eSurfaceChanged;
2083 state.token = display.token();
2084 state.surface = surface;
2085
2086 // --------------------------------------------------------------------
2087 // Invocation
2088
2089 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2090
2091 // --------------------------------------------------------------------
2092 // Postconditions
2093
2094 // The returned flags are empty
2095 EXPECT_EQ(0u, flags);
2096
2097 // The current display state is unchanged.
2098 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2099}
2100
2101TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfSurfaceChanged) {
2102 using Case = SimplePrimaryDisplayCase;
2103
2104 // --------------------------------------------------------------------
2105 // Preconditions
2106
2107 // A display is already set up
2108 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2109 display.inject();
2110
2111 // There is a surface that can be set.
2112 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2113
2114 // The current display state does not have a surface
2115 display.mutableCurrentDisplayState().surface = nullptr;
2116
2117 // The incoming request sets a surface
2118 DisplayState state;
2119 state.what = DisplayState::eSurfaceChanged;
2120 state.token = display.token();
2121 state.surface = surface;
2122
2123 // --------------------------------------------------------------------
2124 // Invocation
2125
2126 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2127
2128 // --------------------------------------------------------------------
2129 // Postconditions
2130
2131 // The returned flags indicate a transaction is needed
2132 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2133
2134 // The current display layer stack state is set to the new value
2135 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2136}
2137
2138TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfLayerStackDidNotChange) {
2139 using Case = SimplePrimaryDisplayCase;
2140
2141 // --------------------------------------------------------------------
2142 // Preconditions
2143
2144 // A display is already set up
2145 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2146 display.inject();
2147
2148 // The display has a layer stack set
2149 display.mutableCurrentDisplayState().layerStack = 456u;
2150
2151 // The incoming request sets the same layer stack
2152 DisplayState state;
2153 state.what = DisplayState::eLayerStackChanged;
2154 state.token = display.token();
2155 state.layerStack = 456u;
2156
2157 // --------------------------------------------------------------------
2158 // Invocation
2159
2160 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2161
2162 // --------------------------------------------------------------------
2163 // Postconditions
2164
2165 // The returned flags are empty
2166 EXPECT_EQ(0u, flags);
2167
2168 // The current display state is unchanged
2169 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2170}
2171
2172TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfLayerStackChanged) {
2173 using Case = SimplePrimaryDisplayCase;
2174
2175 // --------------------------------------------------------------------
2176 // Preconditions
2177
2178 // A display is set up
2179 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2180 display.inject();
2181
2182 // The display has a layer stack set
2183 display.mutableCurrentDisplayState().layerStack = 654u;
2184
2185 // The incoming request sets a different layer stack
2186 DisplayState state;
2187 state.what = DisplayState::eLayerStackChanged;
2188 state.token = display.token();
2189 state.layerStack = 456u;
2190
2191 // --------------------------------------------------------------------
2192 // Invocation
2193
2194 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2195
2196 // --------------------------------------------------------------------
2197 // Postconditions
2198
2199 // The returned flags indicate a transaction is needed
2200 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2201
2202 // The desired display state has been set to the new value.
2203 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2204}
2205
2206TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfProjectionDidNotChange) {
2207 using Case = SimplePrimaryDisplayCase;
2208 constexpr int initialOrientation = 180;
2209 const Rect initialFrame = {1, 2, 3, 4};
2210 const Rect initialViewport = {5, 6, 7, 8};
2211
2212 // --------------------------------------------------------------------
2213 // Preconditions
2214
2215 // A display is set up
2216 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2217 display.inject();
2218
2219 // The current display state projection state is all set
2220 display.mutableCurrentDisplayState().orientation = initialOrientation;
2221 display.mutableCurrentDisplayState().frame = initialFrame;
2222 display.mutableCurrentDisplayState().viewport = initialViewport;
2223
2224 // The incoming request sets the same projection state
2225 DisplayState state;
2226 state.what = DisplayState::eDisplayProjectionChanged;
2227 state.token = display.token();
2228 state.orientation = initialOrientation;
2229 state.frame = initialFrame;
2230 state.viewport = initialViewport;
2231
2232 // --------------------------------------------------------------------
2233 // Invocation
2234
2235 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2236
2237 // --------------------------------------------------------------------
2238 // Postconditions
2239
2240 // The returned flags are empty
2241 EXPECT_EQ(0u, flags);
2242
2243 // The current display state is unchanged
2244 EXPECT_EQ(initialOrientation, display.getCurrentDisplayState().orientation);
2245
2246 EXPECT_EQ(initialFrame, display.getCurrentDisplayState().frame);
2247 EXPECT_EQ(initialViewport, display.getCurrentDisplayState().viewport);
2248}
2249
2250TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfOrientationChanged) {
2251 using Case = SimplePrimaryDisplayCase;
2252 constexpr int initialOrientation = 90;
2253 constexpr int desiredOrientation = 180;
2254
2255 // --------------------------------------------------------------------
2256 // Preconditions
2257
2258 // A display is set up
2259 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2260 display.inject();
2261
2262 // The current display state has an orientation set
2263 display.mutableCurrentDisplayState().orientation = initialOrientation;
2264
2265 // The incoming request sets a different orientation
2266 DisplayState state;
2267 state.what = DisplayState::eDisplayProjectionChanged;
2268 state.token = display.token();
2269 state.orientation = desiredOrientation;
2270
2271 // --------------------------------------------------------------------
2272 // Invocation
2273
2274 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2275
2276 // --------------------------------------------------------------------
2277 // Postconditions
2278
2279 // The returned flags indicate a transaction is needed
2280 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2281
2282 // The current display state has the new value.
2283 EXPECT_EQ(desiredOrientation, display.getCurrentDisplayState().orientation);
2284}
2285
2286TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfFrameChanged) {
2287 using Case = SimplePrimaryDisplayCase;
2288 const Rect initialFrame = {0, 0, 0, 0};
2289 const Rect desiredFrame = {5, 6, 7, 8};
2290
2291 // --------------------------------------------------------------------
2292 // Preconditions
2293
2294 // A display is set up
2295 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2296 display.inject();
2297
2298 // The current display state does not have a frame
2299 display.mutableCurrentDisplayState().frame = initialFrame;
2300
2301 // The incoming request sets a frame
2302 DisplayState state;
2303 state.what = DisplayState::eDisplayProjectionChanged;
2304 state.token = display.token();
2305 state.frame = desiredFrame;
2306
2307 // --------------------------------------------------------------------
2308 // Invocation
2309
2310 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2311
2312 // --------------------------------------------------------------------
2313 // Postconditions
2314
2315 // The returned flags indicate a transaction is needed
2316 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2317
2318 // The current display state has the new value.
2319 EXPECT_EQ(desiredFrame, display.getCurrentDisplayState().frame);
2320}
2321
2322TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfViewportChanged) {
2323 using Case = SimplePrimaryDisplayCase;
2324 const Rect initialViewport = {0, 0, 0, 0};
2325 const Rect desiredViewport = {5, 6, 7, 8};
2326
2327 // --------------------------------------------------------------------
2328 // Preconditions
2329
2330 // A display is set up
2331 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2332 display.inject();
2333
2334 // The current display state does not have a viewport
2335 display.mutableCurrentDisplayState().viewport = initialViewport;
2336
2337 // The incoming request sets a viewport
2338 DisplayState state;
2339 state.what = DisplayState::eDisplayProjectionChanged;
2340 state.token = display.token();
2341 state.viewport = desiredViewport;
2342
2343 // --------------------------------------------------------------------
2344 // Invocation
2345
2346 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2347
2348 // --------------------------------------------------------------------
2349 // Postconditions
2350
2351 // The returned flags indicate a transaction is needed
2352 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2353
2354 // The current display state has the new value.
2355 EXPECT_EQ(desiredViewport, display.getCurrentDisplayState().viewport);
2356}
2357
2358TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSizeDidNotChange) {
2359 using Case = SimplePrimaryDisplayCase;
2360 constexpr uint32_t initialWidth = 1024;
2361 constexpr uint32_t initialHeight = 768;
2362
2363 // --------------------------------------------------------------------
2364 // Preconditions
2365
2366 // A display is set up
2367 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2368 display.inject();
2369
2370 // The current display state has a size set
2371 display.mutableCurrentDisplayState().width = initialWidth;
2372 display.mutableCurrentDisplayState().height = initialHeight;
2373
2374 // The incoming request sets the same display size
2375 DisplayState state;
2376 state.what = DisplayState::eDisplaySizeChanged;
2377 state.token = display.token();
2378 state.width = initialWidth;
2379 state.height = initialHeight;
2380
2381 // --------------------------------------------------------------------
2382 // Invocation
2383
2384 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2385
2386 // --------------------------------------------------------------------
2387 // Postconditions
2388
2389 // The returned flags are empty
2390 EXPECT_EQ(0u, flags);
2391
2392 // The current display state is unchanged
2393 EXPECT_EQ(initialWidth, display.getCurrentDisplayState().width);
2394 EXPECT_EQ(initialHeight, display.getCurrentDisplayState().height);
2395}
2396
2397TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfWidthChanged) {
2398 using Case = SimplePrimaryDisplayCase;
2399 constexpr uint32_t initialWidth = 0;
2400 constexpr uint32_t desiredWidth = 1024;
2401
2402 // --------------------------------------------------------------------
2403 // Preconditions
2404
2405 // A display is set up
2406 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2407 display.inject();
2408
2409 // The display does not yet have a width
2410 display.mutableCurrentDisplayState().width = initialWidth;
2411
2412 // The incoming request sets a display width
2413 DisplayState state;
2414 state.what = DisplayState::eDisplaySizeChanged;
2415 state.token = display.token();
2416 state.width = desiredWidth;
2417
2418 // --------------------------------------------------------------------
2419 // Invocation
2420
2421 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2422
2423 // --------------------------------------------------------------------
2424 // Postconditions
2425
2426 // The returned flags indicate a transaction is needed
2427 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2428
2429 // The current display state has the new value.
2430 EXPECT_EQ(desiredWidth, display.getCurrentDisplayState().width);
2431}
2432
2433TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfHeightChanged) {
2434 using Case = SimplePrimaryDisplayCase;
2435 constexpr uint32_t initialHeight = 0;
2436 constexpr uint32_t desiredHeight = 768;
2437
2438 // --------------------------------------------------------------------
2439 // Preconditions
2440
2441 // A display is set up
2442 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2443 display.inject();
2444
2445 // The display does not yet have a height
2446 display.mutableCurrentDisplayState().height = initialHeight;
2447
2448 // The incoming request sets a display height
2449 DisplayState state;
2450 state.what = DisplayState::eDisplaySizeChanged;
2451 state.token = display.token();
2452 state.height = desiredHeight;
2453
2454 // --------------------------------------------------------------------
2455 // Invocation
2456
2457 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2458
2459 // --------------------------------------------------------------------
2460 // Postconditions
2461
2462 // The returned flags indicate a transaction is needed
2463 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2464
2465 // The current display state has the new value.
2466 EXPECT_EQ(desiredHeight, display.getCurrentDisplayState().height);
2467}
2468
Lloyd Pique86016da2018-03-01 16:09:38 -08002469/* ------------------------------------------------------------------------
2470 * SurfaceFlinger::onInitializeDisplays
2471 */
2472
2473TEST_F(DisplayTransactionTest, onInitializeDisplaysSetsUpPrimaryDisplay) {
2474 using Case = SimplePrimaryDisplayCase;
2475
2476 // --------------------------------------------------------------------
2477 // Preconditions
2478
2479 // A primary display is set up
2480 Case::Display::injectHwcDisplay(this);
2481 auto primaryDisplay = Case::Display::makeFakeExistingDisplayInjector(this);
2482 primaryDisplay.inject();
2483
2484 // --------------------------------------------------------------------
2485 // Call Expectations
2486
2487 // We expect the surface interceptor to possibly be used, but we treat it as
2488 // disabled since it is called as a side effect rather than directly by this
2489 // function.
2490 EXPECT_CALL(*mSurfaceInterceptor, isEnabled()).WillOnce(Return(false));
2491
2492 // We expect a call to get the active display config.
2493 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
2494
2495 // We expect invalidate() to be invoked once to trigger display transaction
2496 // processing.
2497 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
2498
2499 // --------------------------------------------------------------------
2500 // Invocation
2501
2502 mFlinger.onInitializeDisplays();
2503
2504 // --------------------------------------------------------------------
2505 // Postconditions
2506
2507 // The primary display should have a current state
2508 ASSERT_TRUE(hasCurrentDisplayState(primaryDisplay.token()));
2509 const auto& primaryDisplayState = getCurrentDisplayState(primaryDisplay.token());
2510 // The layer stack state should be set to zero
2511 EXPECT_EQ(0u, primaryDisplayState.layerStack);
2512 // The orientation state should be set to zero
2513 EXPECT_EQ(0, primaryDisplayState.orientation);
2514
2515 // The frame state should be set to INVALID
2516 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.frame);
2517
2518 // The viewport state should be set to INVALID
2519 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.viewport);
2520
2521 // The width and height should both be zero
2522 EXPECT_EQ(0u, primaryDisplayState.width);
2523 EXPECT_EQ(0u, primaryDisplayState.height);
2524
2525 // The display should be set to HWC_POWER_MODE_NORMAL
2526 ASSERT_TRUE(hasDisplayDevice(primaryDisplay.token()));
2527 auto displayDevice = primaryDisplay.mutableDisplayDevice();
2528 EXPECT_EQ(HWC_POWER_MODE_NORMAL, displayDevice->getPowerMode());
2529
2530 // The display refresh period should be set in the frame tracker.
2531 FrameStats stats;
2532 mFlinger.getAnimFrameTracker().getStats(&stats);
2533 EXPECT_EQ(DEFAULT_REFRESH_RATE, stats.refreshPeriodNano);
2534
2535 // The display transaction needed flag should be set.
2536 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
2537
2538 // The compositor timing should be set to default values
2539 const auto& compositorTiming = mFlinger.getCompositorTiming();
2540 EXPECT_EQ(-DEFAULT_REFRESH_RATE, compositorTiming.deadline);
2541 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.interval);
2542 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.presentLatency);
2543}
2544
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002545/* ------------------------------------------------------------------------
2546 * SurfaceFlinger::setPowerModeInternal
2547 */
2548
2549// Used when we simulate a display that supports doze.
2550struct DozeIsSupportedVariant {
2551 static constexpr bool DOZE_SUPPORTED = true;
2552 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2553 IComposerClient::PowerMode::DOZE;
2554 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2555 IComposerClient::PowerMode::DOZE_SUSPEND;
2556};
2557
2558// Used when we simulate a display that does not support doze.
2559struct DozeNotSupportedVariant {
2560 static constexpr bool DOZE_SUPPORTED = false;
2561 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2562 IComposerClient::PowerMode::ON;
2563 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2564 IComposerClient::PowerMode::ON;
2565};
2566
2567struct EventThreadBaseSupportedVariant {
2568 static void setupEventAndEventControlThreadNoCallExpectations(DisplayTransactionTest* test) {
2569 // The event control thread should not be notified.
2570 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(_)).Times(0);
2571
2572 // The event thread should not be notified.
2573 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(0);
2574 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(0);
2575 }
2576};
2577
2578struct EventThreadNotSupportedVariant : public EventThreadBaseSupportedVariant {
2579 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2580 // These calls are only expected for the primary display.
2581
2582 // Instead expect no calls.
2583 setupEventAndEventControlThreadNoCallExpectations(test);
2584 }
2585
2586 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2587 // These calls are only expected for the primary display.
2588
2589 // Instead expect no calls.
2590 setupEventAndEventControlThreadNoCallExpectations(test);
2591 }
2592};
2593
2594struct EventThreadIsSupportedVariant : public EventThreadBaseSupportedVariant {
2595 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2596 // The event control thread should be notified to enable vsyncs
2597 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(true)).Times(1);
2598
2599 // The event thread should be notified that the screen was acquired.
2600 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(1);
2601 }
2602
2603 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2604 // There should be a call to setVsyncEnabled(false)
2605 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(false)).Times(1);
2606
2607 // The event thread should not be notified that the screen was released.
2608 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(1);
2609 }
2610};
2611
Lloyd Pique41be5d22018-06-21 13:11:48 -07002612struct DispSyncIsSupportedVariant {
2613 static void setupBeginResyncCallExpectations(DisplayTransactionTest* test) {
2614 EXPECT_CALL(*test->mPrimaryDispSync, reset()).Times(1);
2615 EXPECT_CALL(*test->mPrimaryDispSync, setPeriod(DEFAULT_REFRESH_RATE)).Times(1);
2616 EXPECT_CALL(*test->mPrimaryDispSync, beginResync()).Times(1);
2617 }
2618
2619 static void setupEndResyncCallExpectations(DisplayTransactionTest* test) {
2620 EXPECT_CALL(*test->mPrimaryDispSync, endResync()).Times(1);
2621 }
2622};
2623
2624struct DispSyncNotSupportedVariant {
2625 static void setupBeginResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2626
2627 static void setupEndResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2628};
2629
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002630// --------------------------------------------------------------------
2631// Note:
2632//
2633// There are a large number of transitions we could test, however we only test a
2634// selected subset which provides complete test coverage of the implementation.
2635// --------------------------------------------------------------------
2636
2637template <int initialPowerMode, int targetPowerMode>
2638struct TransitionVariantCommon {
2639 static constexpr auto INITIAL_POWER_MODE = initialPowerMode;
2640 static constexpr auto TARGET_POWER_MODE = targetPowerMode;
2641
2642 static void verifyPostconditions(DisplayTransactionTest*) {}
2643};
2644
2645struct TransitionOffToOnVariant
2646 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_NORMAL> {
2647 template <typename Case>
2648 static void setupCallExpectations(DisplayTransactionTest* test) {
2649 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2650 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002651 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002652 Case::setupRepaintEverythingCallExpectations(test);
2653 }
2654
2655 static void verifyPostconditions(DisplayTransactionTest* test) {
2656 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2657 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2658 }
2659};
2660
2661struct TransitionOffToDozeSuspendVariant
2662 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_DOZE_SUSPEND> {
2663 template <typename Case>
2664 static void setupCallExpectations(DisplayTransactionTest* test) {
2665 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2666 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2667 Case::setupRepaintEverythingCallExpectations(test);
2668 }
2669
2670 static void verifyPostconditions(DisplayTransactionTest* test) {
2671 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2672 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2673 }
2674};
2675
2676struct TransitionOnToOffVariant
2677 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_OFF> {
2678 template <typename Case>
2679 static void setupCallExpectations(DisplayTransactionTest* test) {
2680 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002681 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002682 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2683 }
2684
2685 static void verifyPostconditions(DisplayTransactionTest* test) {
2686 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2687 }
2688};
2689
2690struct TransitionDozeSuspendToOffVariant
2691 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_OFF> {
2692 template <typename Case>
2693 static void setupCallExpectations(DisplayTransactionTest* test) {
2694 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2695 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2696 }
2697
2698 static void verifyPostconditions(DisplayTransactionTest* test) {
2699 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2700 }
2701};
2702
2703struct TransitionOnToDozeVariant
2704 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE> {
2705 template <typename Case>
2706 static void setupCallExpectations(DisplayTransactionTest* test) {
2707 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2708 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2709 }
2710};
2711
2712struct TransitionDozeSuspendToDozeVariant
2713 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_DOZE> {
2714 template <typename Case>
2715 static void setupCallExpectations(DisplayTransactionTest* test) {
2716 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002717 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002718 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2719 }
2720};
2721
2722struct TransitionDozeToOnVariant
2723 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE, HWC_POWER_MODE_NORMAL> {
2724 template <typename Case>
2725 static void setupCallExpectations(DisplayTransactionTest* test) {
2726 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2727 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2728 }
2729};
2730
2731struct TransitionDozeSuspendToOnVariant
2732 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_NORMAL> {
2733 template <typename Case>
2734 static void setupCallExpectations(DisplayTransactionTest* test) {
2735 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002736 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002737 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2738 }
2739};
2740
2741struct TransitionOnToDozeSuspendVariant
2742 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE_SUSPEND> {
2743 template <typename Case>
2744 static void setupCallExpectations(DisplayTransactionTest* test) {
2745 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002746 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002747 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2748 }
2749};
2750
2751struct TransitionOnToUnknownVariant
2752 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_LEET> {
2753 template <typename Case>
2754 static void setupCallExpectations(DisplayTransactionTest* test) {
2755 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2756 Case::setupNoComposerPowerModeCallExpectations(test);
2757 }
2758};
2759
2760// --------------------------------------------------------------------
2761// Note:
2762//
2763// Rather than testing the cartesian product of of
2764// DozeIsSupported/DozeNotSupported with all other options, we use one for one
2765// display type, and the other for another display type.
2766// --------------------------------------------------------------------
2767
2768template <typename DisplayVariant, typename DozeVariant, typename EventThreadVariant,
Lloyd Pique41be5d22018-06-21 13:11:48 -07002769 typename DispSyncVariant, typename TransitionVariant>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002770struct DisplayPowerCase {
2771 using Display = DisplayVariant;
2772 using Doze = DozeVariant;
2773 using EventThread = EventThreadVariant;
Lloyd Pique41be5d22018-06-21 13:11:48 -07002774 using DispSync = DispSyncVariant;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002775 using Transition = TransitionVariant;
2776
2777 static auto injectDisplayWithInitialPowerMode(DisplayTransactionTest* test, int mode) {
2778 Display::injectHwcDisplay(test);
2779 auto display = Display::makeFakeExistingDisplayInjector(test);
2780 display.inject();
2781 display.mutableDisplayDevice()->setPowerMode(mode);
2782 return display;
2783 }
2784
2785 static void setInitialPrimaryHWVsyncEnabled(DisplayTransactionTest* test, bool enabled) {
2786 test->mFlinger.mutablePrimaryHWVsyncEnabled() = enabled;
2787 }
2788
2789 static void setupRepaintEverythingCallExpectations(DisplayTransactionTest* test) {
2790 EXPECT_CALL(*test->mMessageQueue, invalidate()).Times(1);
2791 }
2792
2793 static void setupSurfaceInterceptorCallExpectations(DisplayTransactionTest* test, int mode) {
2794 EXPECT_CALL(*test->mSurfaceInterceptor, isEnabled()).WillOnce(Return(true));
2795 EXPECT_CALL(*test->mSurfaceInterceptor, savePowerModeUpdate(_, mode)).Times(1);
2796 }
2797
2798 static void setupComposerCallExpectations(DisplayTransactionTest* test,
2799 IComposerClient::PowerMode mode) {
2800 // Any calls to get the active config will return a default value.
2801 EXPECT_CALL(*test->mComposer, getActiveConfig(Display::HWC_DISPLAY_ID, _))
2802 .WillRepeatedly(DoAll(SetArgPointee<1>(Display::HWC_ACTIVE_CONFIG_ID),
2803 Return(Error::NONE)));
2804
2805 // Any calls to get whether the display supports dozing will return the value set by the
2806 // policy variant.
2807 EXPECT_CALL(*test->mComposer, getDozeSupport(Display::HWC_DISPLAY_ID, _))
2808 .WillRepeatedly(DoAll(SetArgPointee<1>(Doze::DOZE_SUPPORTED), Return(Error::NONE)));
2809
2810 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, mode)).Times(1);
2811 }
2812
2813 static void setupNoComposerPowerModeCallExpectations(DisplayTransactionTest* test) {
2814 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, _)).Times(0);
2815 }
2816};
2817
2818// A sample configuration for the primary display.
2819// In addition to having event thread support, we emulate doze support.
2820template <typename TransitionVariant>
2821using PrimaryDisplayPowerCase = DisplayPowerCase<PrimaryDisplayVariant, DozeIsSupportedVariant,
Lloyd Pique41be5d22018-06-21 13:11:48 -07002822 EventThreadIsSupportedVariant,
2823 DispSyncIsSupportedVariant, TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002824
2825// A sample configuration for the external display.
2826// In addition to not having event thread support, we emulate not having doze
2827// support.
2828template <typename TransitionVariant>
Lloyd Pique41be5d22018-06-21 13:11:48 -07002829using ExternalDisplayPowerCase = DisplayPowerCase<ExternalDisplayVariant, DozeNotSupportedVariant,
2830 EventThreadNotSupportedVariant,
2831 DispSyncNotSupportedVariant, TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002832
2833class SetPowerModeInternalTest : public DisplayTransactionTest {
2834public:
2835 template <typename Case>
2836 void transitionDisplayCommon();
2837};
2838
2839template <int PowerMode>
2840struct PowerModeInitialVSyncEnabled : public std::false_type {};
2841
2842template <>
2843struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_NORMAL> : public std::true_type {};
2844
2845template <>
2846struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_DOZE> : public std::true_type {};
2847
2848template <typename Case>
2849void SetPowerModeInternalTest::transitionDisplayCommon() {
2850 // --------------------------------------------------------------------
2851 // Preconditions
2852
2853 auto display =
2854 Case::injectDisplayWithInitialPowerMode(this, Case::Transition::INITIAL_POWER_MODE);
2855 Case::setInitialPrimaryHWVsyncEnabled(this,
2856 PowerModeInitialVSyncEnabled<
2857 Case::Transition::INITIAL_POWER_MODE>::value);
2858
2859 // --------------------------------------------------------------------
2860 // Call Expectations
2861
2862 Case::setupSurfaceInterceptorCallExpectations(this, Case::Transition::TARGET_POWER_MODE);
2863 Case::Transition::template setupCallExpectations<Case>(this);
2864
2865 // --------------------------------------------------------------------
2866 // Invocation
2867
2868 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(),
2869 Case::Transition::TARGET_POWER_MODE);
2870
2871 // --------------------------------------------------------------------
2872 // Postconditions
2873
2874 Case::Transition::verifyPostconditions(this);
2875}
2876
2877TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfNoChange) {
2878 using Case = SimplePrimaryDisplayCase;
2879
2880 // --------------------------------------------------------------------
2881 // Preconditions
2882
2883 // A primary display device is set up
2884 Case::Display::injectHwcDisplay(this);
2885 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2886 display.inject();
2887
Dominik Laskowskia2edf612018-06-01 13:15:16 -07002888 // The display is already set to HWC_POWER_MODE_NORMAL
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002889 display.mutableDisplayDevice()->setPowerMode(HWC_POWER_MODE_NORMAL);
2890
2891 // --------------------------------------------------------------------
2892 // Invocation
2893
2894 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_NORMAL);
2895
2896 // --------------------------------------------------------------------
2897 // Postconditions
2898
2899 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2900}
2901
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002902TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfVirtualDisplay) {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002903 using Case = HwcVirtualDisplayCase;
2904
2905 // --------------------------------------------------------------------
2906 // Preconditions
2907
Dominik Laskowski075d3172018-05-24 15:50:06 -07002908 // Insert display data so that the HWC thinks it created the virtual display.
2909 const auto displayId = Case::Display::DISPLAY_ID::get();
2910 ASSERT_TRUE(displayId);
2911 mFlinger.mutableHwcDisplayData()[*displayId] = {};
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002912
2913 // A virtual display device is set up
2914 Case::Display::injectHwcDisplay(this);
2915 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2916 display.inject();
2917
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002918 // The display is set to HWC_POWER_MODE_NORMAL
2919 getDisplayDevice(display.token())->setPowerMode(HWC_POWER_MODE_NORMAL);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002920
2921 // --------------------------------------------------------------------
2922 // Invocation
2923
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002924 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_OFF);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002925
2926 // --------------------------------------------------------------------
2927 // Postconditions
2928
2929 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2930}
2931
2932TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnPrimaryDisplay) {
2933 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToOnVariant>>();
2934}
2935
2936TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendPrimaryDisplay) {
2937 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
2938}
2939
2940TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffPrimaryDisplay) {
2941 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToOffVariant>>();
2942}
2943
2944TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffPrimaryDisplay) {
2945 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
2946}
2947
2948TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozePrimaryDisplay) {
2949 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeVariant>>();
2950}
2951
2952TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozePrimaryDisplay) {
2953 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
2954}
2955
2956TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnPrimaryDisplay) {
2957 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeToOnVariant>>();
2958}
2959
2960TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnPrimaryDisplay) {
2961 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
2962}
2963
2964TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendPrimaryDisplay) {
2965 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
2966}
2967
2968TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownPrimaryDisplay) {
2969 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToUnknownVariant>>();
2970}
2971
2972TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnExternalDisplay) {
2973 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToOnVariant>>();
2974}
2975
2976TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendExternalDisplay) {
2977 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
2978}
2979
2980TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffExternalDisplay) {
2981 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToOffVariant>>();
2982}
2983
2984TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffExternalDisplay) {
2985 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
2986}
2987
2988TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeExternalDisplay) {
2989 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeVariant>>();
2990}
2991
2992TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozeExternalDisplay) {
2993 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
2994}
2995
2996TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnExternalDisplay) {
2997 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeToOnVariant>>();
2998}
2999
3000TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnExternalDisplay) {
3001 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
3002}
3003
3004TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendExternalDisplay) {
3005 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
3006}
3007
3008TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownExternalDisplay) {
3009 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToUnknownVariant>>();
3010}
3011
Lloyd Piquef58625d2017-12-19 13:22:33 -08003012} // namespace
3013} // namespace android