blob: e4eb1d5a9361c4c457b3e429b04bf741905281c3 [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;
Peiyong Lin833074a2018-08-28 11:53:54 -0700138 renderengine::mock::Surface* mRenderSurface = nullptr;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800139};
140
141DisplayTransactionTest::DisplayTransactionTest() {
142 const ::testing::TestInfo* const test_info =
143 ::testing::UnitTest::GetInstance()->current_test_info();
144 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800145
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800146 // Default to no wide color display support configured
147 mFlinger.mutableHasWideColorDisplay() = false;
Peiyong Lin13effd12018-07-24 17:01:47 -0700148 mFlinger.mutableUseColorManagement() = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800149 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
150
151 // Default to using HWC virtual displays
152 mFlinger.mutableUseHwcVirtualDisplays() = true;
153
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800154 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
155 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
156 });
157
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800158 mFlinger.setCreateNativeWindowSurface([](auto) {
159 ADD_FAILURE() << "Unexpected request to create a native window surface.";
160 return nullptr;
161 });
162
163 mFlinger.mutableEventControlThread().reset(mEventControlThread);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800164 mFlinger.mutableEventThread().reset(mEventThread);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800165 mFlinger.mutableEventQueue().reset(mMessageQueue);
Peiyong Lin833074a2018-08-28 11:53:54 -0700166 mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800167 mFlinger.mutableInterceptor().reset(mSurfaceInterceptor);
Lloyd Pique41be5d22018-06-21 13:11:48 -0700168 mFlinger.mutablePrimaryDispSync().reset(mPrimaryDispSync);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800169
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800170 injectMockComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -0800171}
172
173DisplayTransactionTest::~DisplayTransactionTest() {
174 const ::testing::TestInfo* const test_info =
175 ::testing::UnitTest::GetInstance()->current_test_info();
176 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
177}
178
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800179void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
180 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800181 EXPECT_CALL(*mComposer, getCapabilities())
182 .WillOnce(Return(std::vector<IComposer::Capability>()));
183 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
184 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -0800185
Lloyd Piquee39cad22017-12-20 17:01:29 -0800186 Mock::VerifyAndClear(mComposer);
187}
188
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800189void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800190 // This setup is only expected once per test.
191 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
192
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800193 mConsumer = new mock::GraphicBufferConsumer();
194 mProducer = new mock::GraphicBufferProducer();
195
196 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
197 *outProducer = mProducer;
198 *outConsumer = mConsumer;
199 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800200}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800201
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800202void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800203 // This setup is only expected once per test.
204 ASSERT_TRUE(mNativeWindowSurface == nullptr);
205
Lloyd Pique22098362018-09-13 11:46:49 -0700206 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800207
Lloyd Pique22098362018-09-13 11:46:49 -0700208 mFlinger.setCreateNativeWindowSurface([this](auto) {
209 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
210 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800211}
212
Dominik Laskowski075d3172018-05-24 15:50:06 -0700213bool DisplayTransactionTest::hasPhysicalHwcDisplay(hwc2_display_t hwcDisplayId) {
214 return mFlinger.mutableHwcPhysicalDisplayIdMap().count(hwcDisplayId) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800215}
216
217bool DisplayTransactionTest::hasTransactionFlagSet(int flag) {
218 return mFlinger.mutableTransactionFlags() & flag;
219}
220
221bool DisplayTransactionTest::hasDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700222 return mFlinger.mutableDisplays().count(displayToken) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800223}
224
225sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700226 return mFlinger.mutableDisplays()[displayToken];
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800227}
228
229bool DisplayTransactionTest::hasCurrentDisplayState(sp<IBinder> displayToken) {
230 return mFlinger.mutableCurrentState().displays.indexOfKey(displayToken) >= 0;
231}
232
233const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(sp<IBinder> displayToken) {
234 return mFlinger.mutableCurrentState().displays.valueFor(displayToken);
235}
236
237bool DisplayTransactionTest::hasDrawingDisplayState(sp<IBinder> displayToken) {
238 return mFlinger.mutableDrawingState().displays.indexOfKey(displayToken) >= 0;
239}
240
241const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(sp<IBinder> displayToken) {
242 return mFlinger.mutableDrawingState().displays.valueFor(displayToken);
243}
244
245/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800246 *
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800247 */
248
Dominik Laskowski075d3172018-05-24 15:50:06 -0700249template <typename PhysicalDisplay>
250struct PhysicalDisplayId {};
251
252template <DisplayId displayId>
253using VirtualDisplayId = std::integral_constant<DisplayId, displayId>;
254
255struct NoDisplayId {};
256
257template <typename>
258struct IsPhysicalDisplayId : std::bool_constant<false> {};
259
260template <typename PhysicalDisplay>
261struct IsPhysicalDisplayId<PhysicalDisplayId<PhysicalDisplay>> : std::bool_constant<true> {};
262
263template <typename>
264struct DisplayIdGetter;
265
266template <typename PhysicalDisplay>
267struct DisplayIdGetter<PhysicalDisplayId<PhysicalDisplay>> {
268 static std::optional<DisplayId> get() {
269 if (!PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
270 return getFallbackDisplayId(static_cast<bool>(PhysicalDisplay::PRIMARY)
271 ? HWC_DISPLAY_PRIMARY
272 : HWC_DISPLAY_EXTERNAL);
273 }
274
275 const auto info =
276 parseDisplayIdentificationData(PhysicalDisplay::PORT,
277 PhysicalDisplay::GET_IDENTIFICATION_DATA());
278 return info ? std::make_optional(info->id) : std::nullopt;
279 }
280};
281
282template <DisplayId displayId>
283struct DisplayIdGetter<VirtualDisplayId<displayId>> {
284 static std::optional<DisplayId> get() { return displayId; }
285};
286
287template <>
288struct DisplayIdGetter<NoDisplayId> {
289 static std::optional<DisplayId> get() { return {}; }
290};
291
292// DisplayIdType can be:
293// 1) PhysicalDisplayId<...> for generated ID of physical display backed by HWC.
294// 2) VirtualDisplayId<...> for hard-coded ID of virtual display backed by HWC.
295// 3) NoDisplayId for virtual display without HWC backing.
296template <typename DisplayIdType, int width, int height, Critical critical, Async async,
297 Secure secure, Primary primary, int grallocUsage>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800298struct DisplayVariant {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700299 using DISPLAY_ID = DisplayIdGetter<DisplayIdType>;
300
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800301 // The display width and height
302 static constexpr int WIDTH = width;
303 static constexpr int HEIGHT = height;
304
305 static constexpr int GRALLOC_USAGE = grallocUsage;
306
Dominik Laskowski075d3172018-05-24 15:50:06 -0700307 // Whether the display is virtual or physical
308 static constexpr Virtual VIRTUAL =
309 IsPhysicalDisplayId<DisplayIdType>{} ? Virtual::FALSE : Virtual::TRUE;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800310
311 // When creating native window surfaces for the framebuffer, whether those should be critical
312 static constexpr Critical CRITICAL = critical;
313
314 // When creating native window surfaces for the framebuffer, whether those should be async
315 static constexpr Async ASYNC = async;
316
317 // Whether the display should be treated as secure
318 static constexpr Secure SECURE = secure;
319
Dominik Laskowski075d3172018-05-24 15:50:06 -0700320 // Whether the display is primary
321 static constexpr Primary PRIMARY = primary;
322
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800323 static auto makeFakeExistingDisplayInjector(DisplayTransactionTest* test) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700324 auto injector =
325 FakeDisplayDeviceInjector(test->mFlinger, DISPLAY_ID::get(),
326 static_cast<bool>(VIRTUAL), static_cast<bool>(PRIMARY));
327
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800328 injector.setSecure(static_cast<bool>(SECURE));
Alec Mouriba013fa2018-10-16 12:43:11 -0700329 injector.setNativeWindow(test->mNativeWindow);
330
331 // Creating a DisplayDevice requires getting default dimensions from the
332 // native window.
333 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
334 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
335 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
336 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800337 return injector;
338 }
339
340 // Called by tests to set up any native window creation call expectations.
341 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
342 EXPECT_CALL(*test->mNativeWindowSurface, getNativeWindow())
343 .WillOnce(Return(test->mNativeWindow));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800344
345 // For simplicity, we only expect to create a single render surface for
346 // each test.
347 ASSERT_TRUE(test->mRenderSurface == nullptr);
Peiyong Lin833074a2018-08-28 11:53:54 -0700348 test->mRenderSurface = new renderengine::mock::Surface();
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800349 EXPECT_CALL(*test->mRenderEngine, createSurface())
Peiyong Lin833074a2018-08-28 11:53:54 -0700350 .WillOnce(Return(ByMove(
351 std::unique_ptr<renderengine::Surface>(test->mRenderSurface))));
Alec Mouriba013fa2018-10-16 12:43:11 -0700352 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
353 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
354 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
355 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
356
Alec Mouri8147b0e2018-10-09 20:57:19 -0700357 // Creating a DisplayDevice requires getting default dimensions from the
358 // native window.
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800359 EXPECT_CALL(*test->mRenderSurface, setAsync(static_cast<bool>(ASYNC))).Times(1);
360 EXPECT_CALL(*test->mRenderSurface, setCritical(static_cast<bool>(CRITICAL))).Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800361 }
362
363 static void setupFramebufferConsumerBufferQueueCallExpectations(DisplayTransactionTest* test) {
364 EXPECT_CALL(*test->mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR));
365 EXPECT_CALL(*test->mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR));
366 EXPECT_CALL(*test->mConsumer, setConsumerUsageBits(GRALLOC_USAGE))
367 .WillRepeatedly(Return(NO_ERROR));
368 EXPECT_CALL(*test->mConsumer, setDefaultBufferSize(WIDTH, HEIGHT))
369 .WillRepeatedly(Return(NO_ERROR));
370 EXPECT_CALL(*test->mConsumer, setMaxAcquiredBufferCount(_))
371 .WillRepeatedly(Return(NO_ERROR));
372 }
373
374 static void setupFramebufferProducerBufferQueueCallExpectations(DisplayTransactionTest* test) {
375 EXPECT_CALL(*test->mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return());
376 }
377};
378
Dominik Laskowski075d3172018-05-24 15:50:06 -0700379template <hwc2_display_t hwcDisplayId, HWC2::DisplayType hwcDisplayType, typename DisplayVariant,
380 typename PhysicalDisplay = void>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800381struct HwcDisplayVariant {
382 // The display id supplied by the HWC
383 static constexpr hwc2_display_t HWC_DISPLAY_ID = hwcDisplayId;
384
385 // The HWC display type
386 static constexpr HWC2::DisplayType HWC_DISPLAY_TYPE = hwcDisplayType;
387
388 // The HWC active configuration id
Lloyd Pique3c085a02018-05-09 19:38:32 -0700389 static constexpr int HWC_ACTIVE_CONFIG_ID = 2001;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800390
391 static void injectPendingHotplugEvent(DisplayTransactionTest* test,
392 HWC2::Connection connection) {
393 test->mFlinger.mutablePendingHotplugEvents().emplace_back(
394 HotplugEvent{HWC_DISPLAY_ID, connection});
395 }
396
397 // Called by tests to inject a HWC display setup
398 static void injectHwcDisplay(DisplayTransactionTest* test) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700399 const auto displayId = DisplayVariant::DISPLAY_ID::get();
400 ASSERT_TRUE(displayId);
401 FakeHwcDisplayInjector(*displayId, HWC_DISPLAY_TYPE,
402 static_cast<bool>(DisplayVariant::PRIMARY))
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800403 .setHwcDisplayId(HWC_DISPLAY_ID)
404 .setWidth(DisplayVariant::WIDTH)
405 .setHeight(DisplayVariant::HEIGHT)
406 .setActiveConfig(HWC_ACTIVE_CONFIG_ID)
407 .inject(&test->mFlinger, test->mComposer);
408 }
409
410 static void setupHwcHotplugCallExpectations(DisplayTransactionTest* test) {
411 EXPECT_CALL(*test->mComposer, getDisplayType(HWC_DISPLAY_ID, _))
412 .WillOnce(DoAll(SetArgPointee<1>(static_cast<IComposerClient::DisplayType>(
413 HWC_DISPLAY_TYPE)),
414 Return(Error::NONE)));
415 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
416 EXPECT_CALL(*test->mComposer, getDisplayConfigs(HWC_DISPLAY_ID, _))
417 .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{HWC_ACTIVE_CONFIG_ID}),
418 Return(Error::NONE)));
419 EXPECT_CALL(*test->mComposer,
420 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
421 IComposerClient::Attribute::WIDTH, _))
422 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::WIDTH), Return(Error::NONE)));
423 EXPECT_CALL(*test->mComposer,
424 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
425 IComposerClient::Attribute::HEIGHT, _))
426 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::HEIGHT), Return(Error::NONE)));
427 EXPECT_CALL(*test->mComposer,
428 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
429 IComposerClient::Attribute::VSYNC_PERIOD, _))
430 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE)));
431 EXPECT_CALL(*test->mComposer,
432 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
433 IComposerClient::Attribute::DPI_X, _))
434 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
435 EXPECT_CALL(*test->mComposer,
436 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
437 IComposerClient::Attribute::DPI_Y, _))
438 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700439
440 if (PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
441 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
442 .WillOnce(DoAll(SetArgPointee<1>(PhysicalDisplay::PORT),
443 SetArgPointee<2>(PhysicalDisplay::GET_IDENTIFICATION_DATA()),
444 Return(Error::NONE)));
445 } else {
446 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
447 .WillOnce(Return(Error::UNSUPPORTED));
448 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800449 }
450
451 // Called by tests to set up HWC call expectations
452 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
453 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY_ID, _))
Lloyd Pique3c085a02018-05-09 19:38:32 -0700454 .WillRepeatedly(DoAll(SetArgPointee<1>(HWC_ACTIVE_CONFIG_ID), Return(Error::NONE)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800455 }
456};
457
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800458// Physical displays are expected to be synchronous, secure, and have a HWC display for output.
459constexpr uint32_t GRALLOC_USAGE_PHYSICAL_DISPLAY =
460 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
461
Dominik Laskowski075d3172018-05-24 15:50:06 -0700462template <hwc2_display_t hwcDisplayId, typename PhysicalDisplay, int width, int height,
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800463 Critical critical>
464struct PhysicalDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700465 : DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height, critical, Async::FALSE,
466 Secure::TRUE, PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
467 HwcDisplayVariant<hwcDisplayId, HWC2::DisplayType::Physical,
468 DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height,
469 critical, Async::FALSE, Secure::TRUE,
470 PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
471 PhysicalDisplay> {};
472
473template <bool hasIdentificationData>
474struct PrimaryDisplay {
475 static constexpr Primary PRIMARY = Primary::TRUE;
476 static constexpr uint8_t PORT = 255;
477 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
478 static constexpr auto GET_IDENTIFICATION_DATA = getInternalEdid;
479};
480
481template <bool hasIdentificationData>
482struct ExternalDisplay {
483 static constexpr Primary PRIMARY = Primary::FALSE;
484 static constexpr uint8_t PORT = 254;
485 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
486 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
487};
488
489struct TertiaryDisplay {
490 static constexpr Primary PRIMARY = Primary::FALSE;
491};
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800492
493// A primary display is a physical display that is critical
494using PrimaryDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700495 PhysicalDisplayVariant<1001, PrimaryDisplay<false>, 3840, 2160, Critical::TRUE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800496
497// An external display is physical display that is not critical.
498using ExternalDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700499 PhysicalDisplayVariant<1002, ExternalDisplay<false>, 1920, 1280, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800500
501using TertiaryDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700502 PhysicalDisplayVariant<1003, TertiaryDisplay, 1600, 1200, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800503
504// A virtual display not supported by the HWC.
505constexpr uint32_t GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY = 0;
506
507template <int width, int height, Secure secure>
508struct NonHwcVirtualDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700509 : DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
510 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY> {
511 using Base = DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
512 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>;
513
514 static void injectHwcDisplay(DisplayTransactionTest*) {}
515
516 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
517 EXPECT_CALL(*test->mComposer, getActiveConfig(_, _)).Times(0);
518 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800519
520 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
521 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
522 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
523 }
524};
525
526// A virtual display supported by the HWC.
527constexpr uint32_t GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY = GRALLOC_USAGE_HW_COMPOSER;
528
529template <int width, int height, Secure secure>
530struct HwcVirtualDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700531 : DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE, secure,
532 Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>,
533 HwcDisplayVariant<
534 1010, HWC2::DisplayType::Virtual,
535 DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
536 secure, Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>> {
537 using Base = DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
538 secure, Primary::FALSE, GRALLOC_USAGE_HW_COMPOSER>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800539 using Self = HwcVirtualDisplayVariant<width, height, secure>;
540
541 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
542 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
543 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
544 }
545
546 static void setupHwcVirtualDisplayCreationCallExpectations(DisplayTransactionTest* test) {
547 EXPECT_CALL(*test->mComposer, createVirtualDisplay(Base::WIDTH, Base::HEIGHT, _, _))
548 .WillOnce(DoAll(SetArgPointee<3>(Self::HWC_DISPLAY_ID), Return(Error::NONE)));
549 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
550 }
551};
552
553// For this variant, SurfaceFlinger should not configure itself with wide
554// display support, so the display should not be configured for wide-color
555// support.
556struct WideColorSupportNotConfiguredVariant {
557 static constexpr bool WIDE_COLOR_SUPPORTED = false;
558
559 static void injectConfigChange(DisplayTransactionTest* test) {
560 test->mFlinger.mutableHasWideColorDisplay() = false;
Peiyong Lin13effd12018-07-24 17:01:47 -0700561 test->mFlinger.mutableUseColorManagement() = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800562 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
563 }
564
565 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
566 EXPECT_CALL(*test->mComposer, getColorModes(_, _)).Times(0);
567 EXPECT_CALL(*test->mComposer, getRenderIntents(_, _, _)).Times(0);
568 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
569 }
570};
571
572// For this variant, SurfaceFlinger should configure itself with wide display
573// support, and the display should respond with an non-empty list of supported
574// color modes. Wide-color support should be configured.
575template <typename Display>
576struct WideColorP3ColorimetricSupportedVariant {
577 static constexpr bool WIDE_COLOR_SUPPORTED = true;
578
579 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700580 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800581 test->mFlinger.mutableHasWideColorDisplay() = true;
582 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
583 }
584
585 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
586 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
587 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>({ColorMode::DISPLAY_P3})),
588 Return(Error::NONE)));
589 EXPECT_CALL(*test->mComposer,
590 getRenderIntents(Display::HWC_DISPLAY_ID, ColorMode::DISPLAY_P3, _))
591 .WillOnce(DoAll(SetArgPointee<2>(
592 std::vector<RenderIntent>({RenderIntent::COLORIMETRIC})),
593 Return(Error::NONE)));
594 EXPECT_CALL(*test->mComposer,
595 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::SRGB,
596 RenderIntent::COLORIMETRIC))
597 .WillOnce(Return(Error::NONE));
598 }
599};
600
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800601// For this variant, SurfaceFlinger should configure itself with wide display
602// support, but the display should respond with an empty list of supported color
603// modes. Wide-color support for the display should not be configured.
604template <typename Display>
605struct WideColorNotSupportedVariant {
606 static constexpr bool WIDE_COLOR_SUPPORTED = false;
607
608 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700609 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800610 test->mFlinger.mutableHasWideColorDisplay() = true;
611 }
612
613 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
614 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
615 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>()), Return(Error::NONE)));
Chia-I Wu614e1422018-05-23 02:17:03 -0700616 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800617 }
618};
619
620// For this variant, the display is not a HWC display, so no HDR support should
621// be configured.
622struct NonHwcDisplayHdrSupportVariant {
623 static constexpr bool HDR10_SUPPORTED = false;
624 static constexpr bool HDR_HLG_SUPPORTED = false;
625 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
626 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
627 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
628 }
629};
630
631// For this variant, the composer should respond with a non-empty list of HDR
632// modes containing HDR10, so HDR10 support should be configured.
633template <typename Display>
634struct Hdr10SupportedVariant {
635 static constexpr bool HDR10_SUPPORTED = true;
636 static constexpr bool HDR_HLG_SUPPORTED = false;
637 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
638 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
639 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
640 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HDR10})),
641 Return(Error::NONE)));
642 }
643};
644
645// For this variant, the composer should respond with a non-empty list of HDR
646// modes containing HLG, so HLG support should be configured.
647template <typename Display>
648struct HdrHlgSupportedVariant {
649 static constexpr bool HDR10_SUPPORTED = false;
650 static constexpr bool HDR_HLG_SUPPORTED = true;
651 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
652 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
653 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
654 .WillOnce(
655 DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HLG})), Return(Error::NONE)));
656 }
657};
658
659// For this variant, the composer should respond with a non-empty list of HDR
660// modes containing DOLBY_VISION, so DOLBY_VISION support should be configured.
661template <typename Display>
662struct HdrDolbyVisionSupportedVariant {
663 static constexpr bool HDR10_SUPPORTED = false;
664 static constexpr bool HDR_HLG_SUPPORTED = false;
665 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = true;
666 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
667 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
668 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::DOLBY_VISION})),
669 Return(Error::NONE)));
670 }
671};
672
673// For this variant, the composer should respond with am empty list of HDR
674// modes, so no HDR support should be configured.
675template <typename Display>
676struct HdrNotSupportedVariant {
677 static constexpr bool HDR10_SUPPORTED = false;
678 static constexpr bool HDR_HLG_SUPPORTED = false;
679 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
680 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
681 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
682 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
683 }
684};
685
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700686struct NonHwcPerFrameMetadataSupportVariant {
687 static constexpr int PER_FRAME_METADATA_KEYS = 0;
688 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800689 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(_)).Times(0);
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700690 }
691};
692
693template <typename Display>
694struct NoPerFrameMetadataSupportVariant {
695 static constexpr int PER_FRAME_METADATA_KEYS = 0;
696 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800697 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
698 .WillOnce(Return(std::vector<PerFrameMetadataKey>()));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700699 }
700};
701
702template <typename Display>
703struct Smpte2086PerFrameMetadataSupportVariant {
704 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::SMPTE2086;
705 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800706 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
707 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700708 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X,
709 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y,
710 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X,
711 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y,
712 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X,
713 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y,
714 PerFrameMetadataKey::WHITE_POINT_X,
715 PerFrameMetadataKey::WHITE_POINT_Y,
716 PerFrameMetadataKey::MAX_LUMINANCE,
717 PerFrameMetadataKey::MIN_LUMINANCE,
Chia-I Wud7e01d72018-06-21 13:39:09 +0800718 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700719 }
720};
721
722template <typename Display>
723struct Cta861_3_PerFrameMetadataSupportVariant {
724 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::CTA861_3;
725 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800726 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
727 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700728 PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL,
729 PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL,
Chia-I Wud7e01d72018-06-21 13:39:09 +0800730 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700731 }
732};
733
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800734/* ------------------------------------------------------------------------
735 * Typical display configurations to test
736 */
737
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700738template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy,
739 typename PerFrameMetadataSupportPolicy>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800740struct Case {
741 using Display = DisplayPolicy;
742 using WideColorSupport = WideColorSupportPolicy;
743 using HdrSupport = HdrSupportPolicy;
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700744 using PerFrameMetadataSupport = PerFrameMetadataSupportPolicy;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800745};
746
747using SimplePrimaryDisplayCase =
748 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700749 HdrNotSupportedVariant<PrimaryDisplayVariant>,
750 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800751using SimpleExternalDisplayCase =
752 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700753 HdrNotSupportedVariant<ExternalDisplayVariant>,
754 NoPerFrameMetadataSupportVariant<ExternalDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800755using SimpleTertiaryDisplayCase =
756 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700757 HdrNotSupportedVariant<TertiaryDisplayVariant>,
758 NoPerFrameMetadataSupportVariant<TertiaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800759using NonHwcVirtualDisplayCase =
760 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700761 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant,
762 NonHwcPerFrameMetadataSupportVariant>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800763using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
764using HwcVirtualDisplayCase =
765 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
Lloyd Pique438e9e72018-09-04 18:06:08 -0700766 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>,
tangrobin6753a022018-08-10 10:58:54 +0800767 NoPerFrameMetadataSupportVariant<SimpleHwcVirtualDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800768using WideColorP3ColorimetricDisplayCase =
769 Case<PrimaryDisplayVariant, WideColorP3ColorimetricSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700770 HdrNotSupportedVariant<PrimaryDisplayVariant>,
771 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800772using Hdr10DisplayCase =
773 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700774 Hdr10SupportedVariant<PrimaryDisplayVariant>,
775 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800776using HdrHlgDisplayCase =
777 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700778 HdrHlgSupportedVariant<PrimaryDisplayVariant>,
779 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800780using HdrDolbyVisionDisplayCase =
781 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700782 HdrDolbyVisionSupportedVariant<PrimaryDisplayVariant>,
783 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
784using HdrSmpte2086DisplayCase =
785 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
786 HdrNotSupportedVariant<PrimaryDisplayVariant>,
787 Smpte2086PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
788using HdrCta861_3_DisplayCase =
789 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
790 HdrNotSupportedVariant<PrimaryDisplayVariant>,
791 Cta861_3_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Dominik Laskowskif07b85b2018-06-11 12:49:15 -0700792
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800793/* ------------------------------------------------------------------------
Lloyd Pique6cf11032018-01-22 18:57:44 -0800794 *
795 * SurfaceFlinger::onHotplugReceived
796 */
797
798TEST_F(DisplayTransactionTest, hotplugEnqueuesEventsForDisplayTransaction) {
799 constexpr int currentSequenceId = 123;
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700800 constexpr hwc2_display_t hwcDisplayId1 = 456;
801 constexpr hwc2_display_t hwcDisplayId2 = 654;
Lloyd Pique6cf11032018-01-22 18:57:44 -0800802
803 // --------------------------------------------------------------------
804 // Preconditions
805
806 // Set the current sequence id for accepted events
807 mFlinger.mutableComposerSequenceId() = currentSequenceId;
808
809 // Set the main thread id so that the current thread does not appear to be
810 // the main thread.
811 mFlinger.mutableMainThreadId() = std::thread::id();
812
813 // --------------------------------------------------------------------
814 // Call Expectations
815
816 // We expect invalidate() to be invoked once to trigger display transaction
817 // processing.
818 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
819
820 // --------------------------------------------------------------------
821 // Invocation
822
823 // Simulate two hotplug events (a connect and a disconnect)
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700824 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId1, HWC2::Connection::Connected);
825 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId2, HWC2::Connection::Disconnected);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800826
827 // --------------------------------------------------------------------
828 // Postconditions
829
830 // The display transaction needed flag should be set.
831 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
832
833 // All events should be in the pending event queue.
834 const auto& pendingEvents = mFlinger.mutablePendingHotplugEvents();
835 ASSERT_EQ(2u, pendingEvents.size());
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700836 EXPECT_EQ(hwcDisplayId1, pendingEvents[0].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800837 EXPECT_EQ(HWC2::Connection::Connected, pendingEvents[0].connection);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700838 EXPECT_EQ(hwcDisplayId2, pendingEvents[1].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800839 EXPECT_EQ(HWC2::Connection::Disconnected, pendingEvents[1].connection);
840}
841
842TEST_F(DisplayTransactionTest, hotplugDiscardsUnexpectedEvents) {
843 constexpr int currentSequenceId = 123;
844 constexpr int otherSequenceId = 321;
845 constexpr hwc2_display_t displayId = 456;
846
847 // --------------------------------------------------------------------
848 // Preconditions
849
850 // Set the current sequence id for accepted events
851 mFlinger.mutableComposerSequenceId() = currentSequenceId;
852
853 // Set the main thread id so that the current thread does not appear to be
854 // the main thread.
855 mFlinger.mutableMainThreadId() = std::thread::id();
856
857 // --------------------------------------------------------------------
858 // Call Expectations
859
860 // We do not expect any calls to invalidate().
861 EXPECT_CALL(*mMessageQueue, invalidate()).Times(0);
862
863 // --------------------------------------------------------------------
864 // Invocation
865
866 // Call with an unexpected sequence id
867 mFlinger.onHotplugReceived(otherSequenceId, displayId, HWC2::Connection::Invalid);
868
869 // --------------------------------------------------------------------
870 // Postconditions
871
872 // The display transaction needed flag should not be set
873 EXPECT_FALSE(hasTransactionFlagSet(eDisplayTransactionNeeded));
874
875 // There should be no pending events
876 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
877}
878
879TEST_F(DisplayTransactionTest, hotplugProcessesEnqueuedEventsIfCalledOnMainThread) {
880 constexpr int currentSequenceId = 123;
881 constexpr hwc2_display_t displayId1 = 456;
882
883 // --------------------------------------------------------------------
884 // Note:
885 // --------------------------------------------------------------------
886 // This test case is a bit tricky. We want to verify that
887 // onHotplugReceived() calls processDisplayHotplugEventsLocked(), but we
888 // don't really want to provide coverage for everything the later function
889 // does as there are specific tests for it.
890 // --------------------------------------------------------------------
891
892 // --------------------------------------------------------------------
893 // Preconditions
894
895 // Set the current sequence id for accepted events
896 mFlinger.mutableComposerSequenceId() = currentSequenceId;
897
898 // Set the main thread id so that the current thread does appear to be the
899 // main thread.
900 mFlinger.mutableMainThreadId() = std::this_thread::get_id();
901
902 // --------------------------------------------------------------------
903 // Call Expectations
904
905 // We expect invalidate() to be invoked once to trigger display transaction
906 // processing.
907 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
908
909 // --------------------------------------------------------------------
910 // Invocation
911
912 // Simulate a disconnect on a display id that is not connected. This should
913 // be enqueued by onHotplugReceived(), and dequeued by
914 // processDisplayHotplugEventsLocked(), but then ignored as invalid.
915 mFlinger.onHotplugReceived(currentSequenceId, displayId1, HWC2::Connection::Disconnected);
916
917 // --------------------------------------------------------------------
918 // Postconditions
919
920 // The display transaction needed flag should be set.
921 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
922
923 // There should be no event queued on return, as it should have been
924 // processed.
925 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
926}
927
928/* ------------------------------------------------------------------------
Lloyd Piquea482f992018-01-22 19:00:34 -0800929 * SurfaceFlinger::createDisplay
930 */
931
932TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForNonsecureDisplay) {
933 const String8 name("virtual.test");
934
935 // --------------------------------------------------------------------
936 // Call Expectations
937
938 // The call should notify the interceptor that a display was created.
939 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
940
941 // --------------------------------------------------------------------
942 // Invocation
943
944 sp<IBinder> displayToken = mFlinger.createDisplay(name, false);
945
946 // --------------------------------------------------------------------
947 // Postconditions
948
949 // The display should have been added to the current state
950 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
951 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700952 EXPECT_TRUE(display.isVirtual());
953 EXPECT_FALSE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -0800954 EXPECT_EQ(name.string(), display.displayName);
955
956 // --------------------------------------------------------------------
957 // Cleanup conditions
958
959 // Destroying the display invalidates the display state.
960 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
961}
962
963TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForSecureDisplay) {
964 const String8 name("virtual.test");
965
966 // --------------------------------------------------------------------
967 // Call Expectations
968
969 // The call should notify the interceptor that a display was created.
970 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
971
972 // --------------------------------------------------------------------
973 // Invocation
974
975 sp<IBinder> displayToken = mFlinger.createDisplay(name, true);
976
977 // --------------------------------------------------------------------
978 // Postconditions
979
980 // The display should have been added to the current state
981 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
982 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700983 EXPECT_TRUE(display.isVirtual());
984 EXPECT_TRUE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -0800985 EXPECT_EQ(name.string(), display.displayName);
986
987 // --------------------------------------------------------------------
988 // Cleanup conditions
989
990 // Destroying the display invalidates the display state.
991 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
992}
993
994/* ------------------------------------------------------------------------
995 * SurfaceFlinger::destroyDisplay
996 */
997
998TEST_F(DisplayTransactionTest, destroyDisplayClearsCurrentStateForDisplay) {
999 using Case = NonHwcVirtualDisplayCase;
1000
1001 // --------------------------------------------------------------------
1002 // Preconditions
1003
1004 // A virtual display exists
1005 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1006 existing.inject();
1007
1008 // --------------------------------------------------------------------
1009 // Call Expectations
1010
1011 // The call should notify the interceptor that a display was created.
1012 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
1013
1014 // Destroying the display invalidates the display state.
1015 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1016
1017 // --------------------------------------------------------------------
1018 // Invocation
1019
1020 mFlinger.destroyDisplay(existing.token());
1021
1022 // --------------------------------------------------------------------
1023 // Postconditions
1024
1025 // The display should have been removed from the current state
1026 EXPECT_FALSE(hasCurrentDisplayState(existing.token()));
1027
1028 // Ths display should still exist in the drawing state
1029 EXPECT_TRUE(hasDrawingDisplayState(existing.token()));
1030
1031 // The display transaction needed flasg should be set
1032 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1033}
1034
1035TEST_F(DisplayTransactionTest, destroyDisplayHandlesUnknownDisplay) {
1036 // --------------------------------------------------------------------
1037 // Preconditions
1038
1039 sp<BBinder> displayToken = new BBinder();
1040
1041 // --------------------------------------------------------------------
1042 // Invocation
1043
1044 mFlinger.destroyDisplay(displayToken);
1045}
1046
1047/* ------------------------------------------------------------------------
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001048 * SurfaceFlinger::resetDisplayState
1049 */
1050
1051TEST_F(DisplayTransactionTest, resetDisplayStateClearsState) {
1052 using Case = NonHwcVirtualDisplayCase;
1053
1054 // --------------------------------------------------------------------
1055 // Preconditions
1056
1057 // vsync is enabled and available
1058 mFlinger.mutablePrimaryHWVsyncEnabled() = true;
1059 mFlinger.mutableHWVsyncAvailable() = true;
1060
1061 // A display exists
1062 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1063 existing.inject();
1064
1065 // --------------------------------------------------------------------
1066 // Call Expectations
1067
1068 // The call disable vsyncs
1069 EXPECT_CALL(*mEventControlThread, setVsyncEnabled(false)).Times(1);
1070
Lloyd Pique41be5d22018-06-21 13:11:48 -07001071 // The call ends any display resyncs
1072 EXPECT_CALL(*mPrimaryDispSync, endResync()).Times(1);
1073
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001074 // --------------------------------------------------------------------
1075 // Invocation
1076
1077 mFlinger.resetDisplayState();
1078
1079 // --------------------------------------------------------------------
1080 // Postconditions
1081
1082 // vsyncs should be off and not available.
1083 EXPECT_FALSE(mFlinger.mutablePrimaryHWVsyncEnabled());
1084 EXPECT_FALSE(mFlinger.mutableHWVsyncAvailable());
1085
1086 // The display should have been removed from the display map.
1087 EXPECT_FALSE(hasDisplayDevice(existing.token()));
1088
1089 // The display should still exist in the current state
1090 EXPECT_TRUE(hasCurrentDisplayState(existing.token()));
1091
1092 // The display should have been removed from the drawing state
1093 EXPECT_FALSE(hasDrawingDisplayState(existing.token()));
1094}
1095
1096/* ------------------------------------------------------------------------
Valerie Hau9758ae02018-10-09 16:05:09 -07001097 * DisplayDevice::GetBestColorMode
1098 */
1099class GetBestColorModeTest : public DisplayTransactionTest {
1100public:
Dominik Laskowski075d3172018-05-24 15:50:06 -07001101 static constexpr DisplayId DEFAULT_DISPLAY_ID = 777;
1102
Valerie Hau9758ae02018-10-09 16:05:09 -07001103 GetBestColorModeTest()
1104 : DisplayTransactionTest(),
Dominik Laskowski075d3172018-05-24 15:50:06 -07001105 mInjector(FakeDisplayDeviceInjector(mFlinger, DEFAULT_DISPLAY_ID, false /* isVirtual */,
1106 true /* isPrimary */)) {}
Valerie Hau9758ae02018-10-09 16:05:09 -07001107
1108 void setHasWideColorGamut(bool hasWideColorGamut) { mHasWideColorGamut = hasWideColorGamut; }
1109
1110 void addHwcColorModesMapping(ui::ColorMode colorMode,
1111 std::vector<ui::RenderIntent> renderIntents) {
1112 mHwcColorModes[colorMode] = renderIntents;
1113 }
1114
1115 void setInputDataspace(ui::Dataspace dataspace) { mInputDataspace = dataspace; }
1116
1117 void setInputRenderIntent(ui::RenderIntent renderIntent) { mInputRenderIntent = renderIntent; }
1118
1119 void getBestColorMode() {
1120 mInjector.setHwcColorModes(mHwcColorModes);
1121 mInjector.setHasWideColorGamut(mHasWideColorGamut);
Alec Mouriba013fa2018-10-16 12:43:11 -07001122 mInjector.setNativeWindow(mNativeWindow);
1123
1124 // Creating a DisplayDevice requires getting default dimensions from the
1125 // native window.
1126 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
1127 .WillRepeatedly(DoAll(SetArgPointee<1>(1080 /* arbitrary */), Return(0)));
1128 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
1129 .WillRepeatedly(DoAll(SetArgPointee<1>(1920 /* arbitrary */), Return(0)));
Alec Mouri8147b0e2018-10-09 20:57:19 -07001130 EXPECT_CALL(*mNativeWindow, perform(13)).Times(1);
Valerie Hau9758ae02018-10-09 16:05:09 -07001131 auto displayDevice = mInjector.inject();
1132
1133 displayDevice->getBestColorMode(mInputDataspace, mInputRenderIntent, &mOutDataspace,
1134 &mOutColorMode, &mOutRenderIntent);
1135 }
1136
1137 ui::Dataspace mOutDataspace;
1138 ui::ColorMode mOutColorMode;
1139 ui::RenderIntent mOutRenderIntent;
1140
1141private:
1142 ui::Dataspace mInputDataspace;
1143 ui::RenderIntent mInputRenderIntent;
1144 bool mHasWideColorGamut = false;
1145 std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> mHwcColorModes;
1146 FakeDisplayDeviceInjector mInjector;
1147};
1148
1149TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeSRGB) {
1150 addHwcColorModesMapping(ui::ColorMode::SRGB,
1151 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1152 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1153 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1154 setHasWideColorGamut(true);
1155
1156 getBestColorMode();
1157
1158 ASSERT_EQ(ui::Dataspace::SRGB, mOutDataspace);
1159 ASSERT_EQ(ui::ColorMode::SRGB, mOutColorMode);
1160 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1161}
1162
1163TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDisplayP3) {
1164 addHwcColorModesMapping(ui::ColorMode::DISPLAY_P3,
1165 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1166 addHwcColorModesMapping(ui::ColorMode::SRGB,
1167 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1168 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1169 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1170 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1171 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1172 setHasWideColorGamut(true);
1173
1174 getBestColorMode();
1175
1176 ASSERT_EQ(ui::Dataspace::DISPLAY_P3, mOutDataspace);
1177 ASSERT_EQ(ui::ColorMode::DISPLAY_P3, mOutColorMode);
1178 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1179}
1180
1181TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDISPLAY_BT2020) {
1182 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1183 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1184 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1185 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1186 setHasWideColorGamut(true);
1187
1188 getBestColorMode();
1189
1190 ASSERT_EQ(ui::Dataspace::DISPLAY_BT2020, mOutDataspace);
1191 ASSERT_EQ(ui::ColorMode::DISPLAY_BT2020, mOutColorMode);
1192 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1193}
1194
1195/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001196 * SurfaceFlinger::setupNewDisplayDeviceInternal
1197 */
1198
1199class SetupNewDisplayDeviceInternalTest : public DisplayTransactionTest {
1200public:
1201 template <typename T>
1202 void setupNewDisplayDeviceInternalTest();
1203};
1204
1205template <typename Case>
1206void SetupNewDisplayDeviceInternalTest::setupNewDisplayDeviceInternalTest() {
1207 const sp<BBinder> displayToken = new BBinder();
1208 const sp<mock::DisplaySurface> displaySurface = new mock::DisplaySurface();
1209 const sp<mock::GraphicBufferProducer> producer = new mock::GraphicBufferProducer();
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001210
1211 // --------------------------------------------------------------------
1212 // Preconditions
1213
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001214 // Wide color displays support is configured appropriately
1215 Case::WideColorSupport::injectConfigChange(this);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001216
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001217 // The display is setup with the HWC.
1218 Case::Display::injectHwcDisplay(this);
1219
1220 // SurfaceFlinger will use a test-controlled factory for native window
1221 // surfaces.
1222 injectFakeNativeWindowSurfaceFactory();
1223
1224 // --------------------------------------------------------------------
1225 // Call Expectations
1226
1227 // Various native window calls will be made.
1228 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
Lloyd Pique3c085a02018-05-09 19:38:32 -07001229 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001230 Case::WideColorSupport::setupComposerCallExpectations(this);
1231 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001232 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001233
1234 // --------------------------------------------------------------------
1235 // Invocation
1236
1237 DisplayDeviceState state;
Dominik Laskowski075d3172018-05-24 15:50:06 -07001238 state.displayId = static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1239 : Case::Display::DISPLAY_ID::get();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001240 state.isSecure = static_cast<bool>(Case::Display::SECURE);
1241
Dominik Laskowski075d3172018-05-24 15:50:06 -07001242 auto device =
1243 mFlinger.setupNewDisplayDeviceInternal(displayToken, Case::Display::DISPLAY_ID::get(),
1244 state, displaySurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001245
1246 // --------------------------------------------------------------------
1247 // Postconditions
1248
1249 ASSERT_TRUE(device != nullptr);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001250 EXPECT_EQ(Case::Display::DISPLAY_ID::get(), device->getId());
1251 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), device->isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001252 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001253 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001254 EXPECT_EQ(Case::Display::WIDTH, device->getWidth());
1255 EXPECT_EQ(Case::Display::HEIGHT, device->getHeight());
1256 EXPECT_EQ(Case::WideColorSupport::WIDE_COLOR_SUPPORTED, device->hasWideColorGamut());
1257 EXPECT_EQ(Case::HdrSupport::HDR10_SUPPORTED, device->hasHDR10Support());
1258 EXPECT_EQ(Case::HdrSupport::HDR_HLG_SUPPORTED, device->hasHLGSupport());
1259 EXPECT_EQ(Case::HdrSupport::HDR_DOLBY_VISION_SUPPORTED, device->hasDolbyVisionSupport());
Lloyd Pique3c085a02018-05-09 19:38:32 -07001260 // Note: This is not Case::Display::HWC_ACTIVE_CONFIG_ID as the ids are
1261 // remapped, and the test only ever sets up one config. If there were an error
1262 // looking up the remapped index, device->getActiveConfig() would be -1 instead.
1263 EXPECT_EQ(0, device->getActiveConfig());
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001264 EXPECT_EQ(Case::PerFrameMetadataSupport::PER_FRAME_METADATA_KEYS,
1265 device->getSupportedPerFrameMetadata());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001266}
1267
1268TEST_F(SetupNewDisplayDeviceInternalTest, createSimplePrimaryDisplay) {
1269 setupNewDisplayDeviceInternalTest<SimplePrimaryDisplayCase>();
1270}
1271
1272TEST_F(SetupNewDisplayDeviceInternalTest, createSimpleExternalDisplay) {
1273 setupNewDisplayDeviceInternalTest<SimpleExternalDisplayCase>();
1274}
1275
1276TEST_F(SetupNewDisplayDeviceInternalTest, createNonHwcVirtualDisplay) {
1277 setupNewDisplayDeviceInternalTest<NonHwcVirtualDisplayCase>();
1278}
1279
1280TEST_F(SetupNewDisplayDeviceInternalTest, createHwcVirtualDisplay) {
Dominik Laskowski075d3172018-05-24 15:50:06 -07001281 using Case = HwcVirtualDisplayCase;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001282
Dominik Laskowski075d3172018-05-24 15:50:06 -07001283 // Insert display data so that the HWC thinks it created the virtual display.
1284 const auto displayId = Case::Display::DISPLAY_ID::get();
1285 ASSERT_TRUE(displayId);
1286 mFlinger.mutableHwcDisplayData()[*displayId] = {};
1287
1288 setupNewDisplayDeviceInternalTest<Case>();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001289}
1290
1291TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3Display) {
1292 setupNewDisplayDeviceInternalTest<WideColorP3ColorimetricDisplayCase>();
1293}
1294
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001295TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10Display) {
1296 setupNewDisplayDeviceInternalTest<Hdr10DisplayCase>();
1297}
1298
1299TEST_F(SetupNewDisplayDeviceInternalTest, createHdrHlgDisplay) {
1300 setupNewDisplayDeviceInternalTest<HdrHlgDisplayCase>();
1301}
1302
1303TEST_F(SetupNewDisplayDeviceInternalTest, createHdrDolbyVisionDisplay) {
1304 setupNewDisplayDeviceInternalTest<HdrDolbyVisionDisplayCase>();
1305}
1306
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001307TEST_F(SetupNewDisplayDeviceInternalTest, createHdrSmpte2086DisplayCase) {
1308 setupNewDisplayDeviceInternalTest<HdrSmpte2086DisplayCase>();
1309}
1310
1311TEST_F(SetupNewDisplayDeviceInternalTest, createHdrCta816_3_DisplayCase) {
1312 setupNewDisplayDeviceInternalTest<HdrCta861_3_DisplayCase>();
1313}
1314
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001315/* ------------------------------------------------------------------------
1316 * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded)
1317 */
1318
1319class HandleTransactionLockedTest : public DisplayTransactionTest {
1320public:
1321 template <typename Case>
1322 void setupCommonPreconditions();
1323
1324 template <typename Case>
1325 void setupCommonCallExpectationsForConnectProcessing();
1326
1327 template <typename Case>
1328 void setupCommonCallExpectationsForDisconnectProcessing();
1329
1330 template <typename Case>
1331 void processesHotplugConnectCommon();
1332
1333 template <typename Case>
1334 void ignoresHotplugConnectCommon();
1335
1336 template <typename Case>
1337 void processesHotplugDisconnectCommon();
1338
1339 template <typename Case>
1340 void verifyDisplayIsConnected(const sp<IBinder>& displayToken);
1341
1342 template <typename Case>
1343 void verifyPhysicalDisplayIsConnected();
1344
1345 void verifyDisplayIsNotConnected(const sp<IBinder>& displayToken);
1346};
1347
1348template <typename Case>
1349void HandleTransactionLockedTest::setupCommonPreconditions() {
1350 // Wide color displays support is configured appropriately
1351 Case::WideColorSupport::injectConfigChange(this);
1352
1353 // SurfaceFlinger will use a test-controlled factory for BufferQueues
1354 injectFakeBufferQueueFactory();
1355
1356 // SurfaceFlinger will use a test-controlled factory for native window
1357 // surfaces.
1358 injectFakeNativeWindowSurfaceFactory();
1359}
1360
1361template <typename Case>
1362void HandleTransactionLockedTest::setupCommonCallExpectationsForConnectProcessing() {
1363 Case::Display::setupHwcHotplugCallExpectations(this);
1364
1365 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1366 Case::Display::setupFramebufferProducerBufferQueueCallExpectations(this);
1367 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1368 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
1369
1370 Case::WideColorSupport::setupComposerCallExpectations(this);
1371 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001372 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001373
1374 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001375 EXPECT_CALL(*mEventThread,
Dominik Laskowski075d3172018-05-24 15:50:06 -07001376 onHotplugReceived(static_cast<bool>(Case::Display::PRIMARY)
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001377 ? EventThread::DisplayType::Primary
1378 : EventThread::DisplayType::External,
1379 true))
1380 .Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001381}
1382
1383template <typename Case>
1384void HandleTransactionLockedTest::setupCommonCallExpectationsForDisconnectProcessing() {
1385 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001386 EXPECT_CALL(*mEventThread,
Dominik Laskowski075d3172018-05-24 15:50:06 -07001387 onHotplugReceived(static_cast<bool>(Case::Display::PRIMARY)
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001388 ? EventThread::DisplayType::Primary
1389 : EventThread::DisplayType::External,
1390 false))
1391 .Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001392}
1393
1394template <typename Case>
1395void HandleTransactionLockedTest::verifyDisplayIsConnected(const sp<IBinder>& displayToken) {
1396 // The display device should have been set up in the list of displays.
1397 ASSERT_TRUE(hasDisplayDevice(displayToken));
1398 const auto& device = getDisplayDevice(displayToken);
1399 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001400 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001401
1402 // The display should have been set up in the current display state
1403 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1404 const auto& current = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001405 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), current.isVirtual());
1406 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1407 : Case::Display::DISPLAY_ID::get(),
1408 current.displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001409
1410 // The display should have been set up in the drawing display state
1411 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1412 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001413 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
1414 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1415 : Case::Display::DISPLAY_ID::get(),
1416 draw.displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001417}
1418
1419template <typename Case>
1420void HandleTransactionLockedTest::verifyPhysicalDisplayIsConnected() {
1421 // HWComposer should have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001422 EXPECT_TRUE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001423
Dominik Laskowski075d3172018-05-24 15:50:06 -07001424 // SF should have a display token.
1425 const auto displayId = Case::Display::DISPLAY_ID::get();
1426 ASSERT_TRUE(displayId);
1427 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1428 auto& displayToken = mFlinger.mutablePhysicalDisplayTokens()[*displayId];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001429
1430 verifyDisplayIsConnected<Case>(displayToken);
1431}
1432
1433void HandleTransactionLockedTest::verifyDisplayIsNotConnected(const sp<IBinder>& displayToken) {
1434 EXPECT_FALSE(hasDisplayDevice(displayToken));
1435 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1436 EXPECT_FALSE(hasDrawingDisplayState(displayToken));
1437}
1438
1439template <typename Case>
1440void HandleTransactionLockedTest::processesHotplugConnectCommon() {
1441 // --------------------------------------------------------------------
1442 // Preconditions
1443
1444 setupCommonPreconditions<Case>();
1445
1446 // A hotplug connect event is enqueued for a display
1447 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001448
1449 // --------------------------------------------------------------------
1450 // Call Expectations
1451
1452 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillOnce(Return(false));
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001453
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001454 setupCommonCallExpectationsForConnectProcessing<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001455
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001456 // --------------------------------------------------------------------
1457 // Invocation
Lloyd Piquee39cad22017-12-20 17:01:29 -08001458
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001459 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
Lloyd Piquee39cad22017-12-20 17:01:29 -08001460
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001461 // --------------------------------------------------------------------
1462 // Postconditions
1463
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001464 verifyPhysicalDisplayIsConnected<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001465
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001466 // --------------------------------------------------------------------
1467 // Cleanup conditions
1468
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001469 EXPECT_CALL(*mComposer,
1470 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001471 .WillOnce(Return(Error::NONE));
1472 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Piquef58625d2017-12-19 13:22:33 -08001473}
1474
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001475template <typename Case>
1476void HandleTransactionLockedTest::ignoresHotplugConnectCommon() {
1477 // --------------------------------------------------------------------
1478 // Preconditions
1479
1480 setupCommonPreconditions<Case>();
1481
1482 // A hotplug connect event is enqueued for a display
1483 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1484
1485 // --------------------------------------------------------------------
1486 // Invocation
1487
1488 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1489
1490 // --------------------------------------------------------------------
1491 // Postconditions
1492
1493 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001494 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001495}
1496
1497template <typename Case>
1498void HandleTransactionLockedTest::processesHotplugDisconnectCommon() {
1499 // --------------------------------------------------------------------
1500 // Preconditions
1501
1502 setupCommonPreconditions<Case>();
1503
1504 // A hotplug disconnect event is enqueued for a display
1505 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1506
1507 // The display is already completely set up.
1508 Case::Display::injectHwcDisplay(this);
1509 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1510 existing.inject();
1511
1512 // --------------------------------------------------------------------
1513 // Call Expectations
1514
1515 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
Lloyd Pique438e9e72018-09-04 18:06:08 -07001516 EXPECT_CALL(*mComposer, getDisplayIdentificationData(Case::Display::HWC_DISPLAY_ID, _, _))
1517 .Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001518
1519 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1520
1521 // --------------------------------------------------------------------
1522 // Invocation
1523
1524 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1525
1526 // --------------------------------------------------------------------
1527 // Postconditions
1528
1529 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001530 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001531
Dominik Laskowski075d3172018-05-24 15:50:06 -07001532 // SF should not have a display token.
1533 const auto displayId = Case::Display::DISPLAY_ID::get();
1534 ASSERT_TRUE(displayId);
1535 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001536
1537 // The existing token should have been removed
1538 verifyDisplayIsNotConnected(existing.token());
1539}
1540
1541TEST_F(HandleTransactionLockedTest, processesHotplugConnectPrimaryDisplay) {
1542 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1543}
1544
1545TEST_F(HandleTransactionLockedTest,
1546 processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected) {
1547 // Inject an external display.
1548 ExternalDisplayVariant::injectHwcDisplay(this);
1549
1550 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1551}
1552
1553TEST_F(HandleTransactionLockedTest, processesHotplugConnectExternalDisplay) {
1554 // Inject a primary display.
1555 PrimaryDisplayVariant::injectHwcDisplay(this);
1556
1557 processesHotplugConnectCommon<SimpleExternalDisplayCase>();
1558}
1559
1560TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected) {
1561 // Inject both a primary and external display.
1562 PrimaryDisplayVariant::injectHwcDisplay(this);
1563 ExternalDisplayVariant::injectHwcDisplay(this);
1564
1565 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1566
1567 ignoresHotplugConnectCommon<SimpleTertiaryDisplayCase>();
1568}
1569
1570TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfExternalForVrComposer) {
1571 // Inject a primary display.
1572 PrimaryDisplayVariant::injectHwcDisplay(this);
1573
1574 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(true));
1575
1576 ignoresHotplugConnectCommon<SimpleExternalDisplayCase>();
1577}
1578
1579TEST_F(HandleTransactionLockedTest, processHotplugDisconnectPrimaryDisplay) {
1580 processesHotplugDisconnectCommon<SimplePrimaryDisplayCase>();
1581}
1582
1583TEST_F(HandleTransactionLockedTest, processHotplugDisconnectExternalDisplay) {
1584 processesHotplugDisconnectCommon<SimpleExternalDisplayCase>();
1585}
1586
1587TEST_F(HandleTransactionLockedTest, processesHotplugConnectThenDisconnectPrimary) {
1588 using Case = SimplePrimaryDisplayCase;
1589
1590 // --------------------------------------------------------------------
1591 // Preconditions
1592
1593 setupCommonPreconditions<Case>();
1594
1595 // A hotplug connect event is enqueued for a display
1596 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1597 // A hotplug disconnect event is also enqueued for the same display
1598 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1599
1600 // --------------------------------------------------------------------
1601 // Call Expectations
1602
1603 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1604
1605 setupCommonCallExpectationsForConnectProcessing<Case>();
1606 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1607
1608 EXPECT_CALL(*mComposer,
1609 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1610 .WillOnce(Return(Error::NONE));
1611 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1612
1613 // --------------------------------------------------------------------
1614 // Invocation
1615
1616 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1617
1618 // --------------------------------------------------------------------
1619 // Postconditions
1620
1621 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001622 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001623
Dominik Laskowski075d3172018-05-24 15:50:06 -07001624 // SF should not have a display token.
1625 const auto displayId = Case::Display::DISPLAY_ID::get();
1626 ASSERT_TRUE(displayId);
1627 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001628}
1629
1630TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectThenConnectPrimary) {
1631 using Case = SimplePrimaryDisplayCase;
1632
1633 // --------------------------------------------------------------------
1634 // Preconditions
1635
1636 setupCommonPreconditions<Case>();
1637
1638 // The display is already completely set up.
1639 Case::Display::injectHwcDisplay(this);
1640 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1641 existing.inject();
1642
1643 // A hotplug disconnect event is enqueued for a display
1644 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1645 // A hotplug connect event is also enqueued for the same display
1646 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1647
1648 // --------------------------------------------------------------------
1649 // Call Expectations
1650
1651 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1652
1653 setupCommonCallExpectationsForConnectProcessing<Case>();
1654 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1655
1656 // --------------------------------------------------------------------
1657 // Invocation
1658
1659 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1660
1661 // --------------------------------------------------------------------
1662 // Postconditions
1663
1664 // The existing token should have been removed
1665 verifyDisplayIsNotConnected(existing.token());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001666 const auto displayId = Case::Display::DISPLAY_ID::get();
1667 ASSERT_TRUE(displayId);
1668 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1669 EXPECT_NE(existing.token(), mFlinger.mutablePhysicalDisplayTokens()[*displayId]);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001670
1671 // A new display should be connected in its place
1672
1673 verifyPhysicalDisplayIsConnected<Case>();
1674
1675 // --------------------------------------------------------------------
1676 // Cleanup conditions
1677
1678 EXPECT_CALL(*mComposer,
1679 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1680 .WillOnce(Return(Error::NONE));
1681 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1682}
1683
1684TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAdded) {
1685 using Case = HwcVirtualDisplayCase;
1686
1687 // --------------------------------------------------------------------
1688 // Preconditions
1689
1690 // The HWC supports at least one virtual display
1691 injectMockComposer(1);
1692
1693 setupCommonPreconditions<Case>();
1694
1695 // A virtual display was added to the current state, and it has a
1696 // surface(producer)
1697 sp<BBinder> displayToken = new BBinder();
Lloyd Pique4c2ac022018-04-27 12:08:03 -07001698
Dominik Laskowski075d3172018-05-24 15:50:06 -07001699 DisplayDeviceState state;
1700 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001701
1702 sp<mock::GraphicBufferProducer> surface{new mock::GraphicBufferProducer()};
Dominik Laskowski075d3172018-05-24 15:50:06 -07001703 state.surface = surface;
1704 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001705
1706 // --------------------------------------------------------------------
1707 // Call Expectations
1708
1709 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1710 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1711
1712 EXPECT_CALL(*surface, query(NATIVE_WINDOW_WIDTH, _))
1713 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::WIDTH), Return(NO_ERROR)));
1714 EXPECT_CALL(*surface, query(NATIVE_WINDOW_HEIGHT, _))
1715 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::HEIGHT), Return(NO_ERROR)));
1716 EXPECT_CALL(*surface, query(NATIVE_WINDOW_FORMAT, _))
1717 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT),
1718 Return(NO_ERROR)));
1719 EXPECT_CALL(*surface, query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, _))
1720 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(NO_ERROR)));
1721
1722 EXPECT_CALL(*surface, setAsyncMode(true)).Times(1);
1723
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001724 EXPECT_CALL(*mProducer, disconnect(_, _)).Times(1);
1725
1726 Case::Display::setupHwcVirtualDisplayCreationCallExpectations(this);
1727 Case::WideColorSupport::setupComposerCallExpectations(this);
1728 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001729 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001730
1731 // --------------------------------------------------------------------
1732 // Invocation
1733
1734 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1735
1736 // --------------------------------------------------------------------
1737 // Postconditions
1738
1739 // The display device should have been set up in the list of displays.
1740 verifyDisplayIsConnected<Case>(displayToken);
1741
1742 // --------------------------------------------------------------------
1743 // Cleanup conditions
1744
1745 EXPECT_CALL(*mComposer, destroyVirtualDisplay(Case::Display::HWC_DISPLAY_ID))
1746 .WillOnce(Return(Error::NONE));
1747 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1748}
1749
1750TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAddedWithNoSurface) {
1751 using Case = HwcVirtualDisplayCase;
1752
1753 // --------------------------------------------------------------------
1754 // Preconditions
1755
1756 // The HWC supports at least one virtual display
1757 injectMockComposer(1);
1758
1759 setupCommonPreconditions<Case>();
1760
1761 // A virtual display was added to the current state, but it does not have a
1762 // surface.
1763 sp<BBinder> displayToken = new BBinder();
1764
Dominik Laskowski075d3172018-05-24 15:50:06 -07001765 DisplayDeviceState state;
1766 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001767
Dominik Laskowski075d3172018-05-24 15:50:06 -07001768 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001769
1770 // --------------------------------------------------------------------
1771 // Call Expectations
1772
1773 // --------------------------------------------------------------------
1774 // Invocation
1775
1776 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1777
1778 // --------------------------------------------------------------------
1779 // Postconditions
1780
1781 // There will not be a display device set up.
1782 EXPECT_FALSE(hasDisplayDevice(displayToken));
1783
1784 // The drawing display state will be set from the current display state.
1785 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1786 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001787 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001788}
1789
1790TEST_F(HandleTransactionLockedTest, processesVirtualDisplayRemoval) {
1791 using Case = HwcVirtualDisplayCase;
1792
1793 // --------------------------------------------------------------------
1794 // Preconditions
1795
1796 // A virtual display is set up but is removed from the current state.
Dominik Laskowski075d3172018-05-24 15:50:06 -07001797 const auto displayId = Case::Display::DISPLAY_ID::get();
1798 ASSERT_TRUE(displayId);
1799 mFlinger.mutableHwcDisplayData()[*displayId] = {};
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001800 Case::Display::injectHwcDisplay(this);
1801 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1802 existing.inject();
1803 mFlinger.mutableCurrentState().displays.removeItem(existing.token());
1804
1805 // --------------------------------------------------------------------
1806 // Call Expectations
1807
1808 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1809
1810 // --------------------------------------------------------------------
1811 // Invocation
1812
1813 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1814
1815 // --------------------------------------------------------------------
1816 // Postconditions
1817
1818 // The existing token should have been removed
1819 verifyDisplayIsNotConnected(existing.token());
1820}
1821
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001822TEST_F(HandleTransactionLockedTest, processesDisplayLayerStackChanges) {
1823 using Case = NonHwcVirtualDisplayCase;
1824
1825 constexpr uint32_t oldLayerStack = 0u;
1826 constexpr uint32_t newLayerStack = 123u;
1827
1828 // --------------------------------------------------------------------
1829 // Preconditions
1830
1831 // A display is set up
1832 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1833 display.inject();
1834
1835 // There is a change to the layerStack state
1836 display.mutableDrawingDisplayState().layerStack = oldLayerStack;
1837 display.mutableCurrentDisplayState().layerStack = newLayerStack;
1838
1839 // --------------------------------------------------------------------
1840 // Invocation
1841
1842 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1843
1844 // --------------------------------------------------------------------
1845 // Postconditions
1846
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001847 EXPECT_EQ(newLayerStack, display.mutableDisplayDevice()->getLayerStack());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001848}
1849
1850TEST_F(HandleTransactionLockedTest, processesDisplayTransformChanges) {
1851 using Case = NonHwcVirtualDisplayCase;
1852
1853 constexpr int oldTransform = 0;
1854 constexpr int newTransform = 2;
1855
1856 // --------------------------------------------------------------------
1857 // Preconditions
1858
1859 // A display is set up
1860 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1861 display.inject();
1862
1863 // There is a change to the orientation state
1864 display.mutableDrawingDisplayState().orientation = oldTransform;
1865 display.mutableCurrentDisplayState().orientation = newTransform;
1866
1867 // --------------------------------------------------------------------
1868 // Invocation
1869
1870 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1871
1872 // --------------------------------------------------------------------
1873 // Postconditions
1874
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001875 EXPECT_EQ(newTransform, display.mutableDisplayDevice()->getOrientation());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001876}
1877
1878TEST_F(HandleTransactionLockedTest, processesDisplayViewportChanges) {
1879 using Case = NonHwcVirtualDisplayCase;
1880
1881 const Rect oldViewport(0, 0, 0, 0);
1882 const Rect newViewport(0, 0, 123, 456);
1883
1884 // --------------------------------------------------------------------
1885 // Preconditions
1886
1887 // A display is set up
1888 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1889 display.inject();
1890
1891 // There is a change to the viewport state
1892 display.mutableDrawingDisplayState().viewport = oldViewport;
1893 display.mutableCurrentDisplayState().viewport = newViewport;
1894
1895 // --------------------------------------------------------------------
1896 // Invocation
1897
1898 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1899
1900 // --------------------------------------------------------------------
1901 // Postconditions
1902
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001903 EXPECT_EQ(newViewport, display.mutableDisplayDevice()->getViewport());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001904}
1905
1906TEST_F(HandleTransactionLockedTest, processesDisplayFrameChanges) {
1907 using Case = NonHwcVirtualDisplayCase;
1908
1909 const Rect oldFrame(0, 0, 0, 0);
1910 const Rect newFrame(0, 0, 123, 456);
1911
1912 // --------------------------------------------------------------------
1913 // Preconditions
1914
1915 // A display is set up
1916 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1917 display.inject();
1918
1919 // There is a change to the viewport state
1920 display.mutableDrawingDisplayState().frame = oldFrame;
1921 display.mutableCurrentDisplayState().frame = newFrame;
1922
1923 // --------------------------------------------------------------------
1924 // Invocation
1925
1926 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1927
1928 // --------------------------------------------------------------------
1929 // Postconditions
1930
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001931 EXPECT_EQ(newFrame, display.mutableDisplayDevice()->getFrame());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001932}
1933
1934TEST_F(HandleTransactionLockedTest, processesDisplayWidthChanges) {
1935 using Case = NonHwcVirtualDisplayCase;
1936
1937 constexpr int oldWidth = 0;
1938 constexpr int oldHeight = 10;
1939 constexpr int newWidth = 123;
1940
1941 // --------------------------------------------------------------------
1942 // Preconditions
1943
1944 // A display is set up
1945 auto nativeWindow = new mock::NativeWindow();
1946 auto displaySurface = new mock::DisplaySurface();
Peiyong Lin833074a2018-08-28 11:53:54 -07001947 auto renderSurface = new renderengine::mock::Surface();
Alec Mouri8147b0e2018-10-09 20:57:19 -07001948 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001949 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1950 display.setNativeWindow(nativeWindow);
1951 display.setDisplaySurface(displaySurface);
Peiyong Lin833074a2018-08-28 11:53:54 -07001952 display.setRenderSurface(std::unique_ptr<renderengine::Surface>(renderSurface));
Alec Mouri8147b0e2018-10-09 20:57:19 -07001953 // Setup injection expections
1954 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
1955 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
1956 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
1957 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
1958 EXPECT_CALL(*nativeWindow, perform(13)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001959 display.inject();
1960
1961 // There is a change to the viewport state
1962 display.mutableDrawingDisplayState().width = oldWidth;
1963 display.mutableDrawingDisplayState().height = oldHeight;
1964 display.mutableCurrentDisplayState().width = newWidth;
1965 display.mutableCurrentDisplayState().height = oldHeight;
1966
1967 // --------------------------------------------------------------------
1968 // Call Expectations
1969
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001970 EXPECT_CALL(*displaySurface, resizeBuffers(newWidth, oldHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001971
1972 // --------------------------------------------------------------------
1973 // Invocation
1974
1975 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1976}
1977
1978TEST_F(HandleTransactionLockedTest, processesDisplayHeightChanges) {
1979 using Case = NonHwcVirtualDisplayCase;
1980
1981 constexpr int oldWidth = 0;
1982 constexpr int oldHeight = 10;
1983 constexpr int newHeight = 123;
1984
1985 // --------------------------------------------------------------------
1986 // Preconditions
1987
1988 // A display is set up
1989 auto nativeWindow = new mock::NativeWindow();
1990 auto displaySurface = new mock::DisplaySurface();
Peiyong Lin833074a2018-08-28 11:53:54 -07001991 auto renderSurface = new renderengine::mock::Surface();
Alec Mouri8147b0e2018-10-09 20:57:19 -07001992 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001993 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1994 display.setNativeWindow(nativeWindow);
1995 display.setDisplaySurface(displaySurface);
Peiyong Lin833074a2018-08-28 11:53:54 -07001996 display.setRenderSurface(std::unique_ptr<renderengine::Surface>(renderSurface));
Alec Mouri8147b0e2018-10-09 20:57:19 -07001997 // Setup injection expections
1998 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
1999 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
2000 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
2001 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
2002 EXPECT_CALL(*nativeWindow, perform(13)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002003 display.inject();
2004
2005 // There is a change to the viewport state
2006 display.mutableDrawingDisplayState().width = oldWidth;
2007 display.mutableDrawingDisplayState().height = oldHeight;
2008 display.mutableCurrentDisplayState().width = oldWidth;
2009 display.mutableCurrentDisplayState().height = newHeight;
2010
2011 // --------------------------------------------------------------------
2012 // Call Expectations
2013
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002014 EXPECT_CALL(*displaySurface, resizeBuffers(oldWidth, newHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002015
2016 // --------------------------------------------------------------------
2017 // Invocation
2018
2019 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2020}
2021
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002022/* ------------------------------------------------------------------------
2023 * SurfaceFlinger::setDisplayStateLocked
2024 */
2025
2026TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithUnknownDisplay) {
2027 // --------------------------------------------------------------------
2028 // Preconditions
2029
2030 // We have an unknown display token not associated with a known display
2031 sp<BBinder> displayToken = new BBinder();
2032
2033 // The requested display state references the unknown display.
2034 DisplayState state;
2035 state.what = DisplayState::eLayerStackChanged;
2036 state.token = displayToken;
2037 state.layerStack = 456;
2038
2039 // --------------------------------------------------------------------
2040 // Invocation
2041
2042 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2043
2044 // --------------------------------------------------------------------
2045 // Postconditions
2046
2047 // The returned flags are empty
2048 EXPECT_EQ(0u, flags);
2049
2050 // The display token still doesn't match anything known.
2051 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
2052}
2053
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002054TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWhenNoChanges) {
2055 using Case = SimplePrimaryDisplayCase;
2056
2057 // --------------------------------------------------------------------
2058 // Preconditions
2059
2060 // A display is already set up
2061 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2062 display.inject();
2063
2064 // No changes are made to the display
2065 DisplayState state;
2066 state.what = 0;
2067 state.token = display.token();
2068
2069 // --------------------------------------------------------------------
2070 // Invocation
2071
2072 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2073
2074 // --------------------------------------------------------------------
2075 // Postconditions
2076
2077 // The returned flags are empty
2078 EXPECT_EQ(0u, flags);
2079}
2080
2081TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSurfaceDidNotChange) {
2082 using Case = SimplePrimaryDisplayCase;
2083
2084 // --------------------------------------------------------------------
2085 // Preconditions
2086
2087 // A display is already set up
2088 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2089 display.inject();
2090
2091 // There is a surface that can be set.
2092 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2093
2094 // The current display state has the surface set
2095 display.mutableCurrentDisplayState().surface = surface;
2096
2097 // The incoming request sets the same surface
2098 DisplayState state;
2099 state.what = DisplayState::eSurfaceChanged;
2100 state.token = display.token();
2101 state.surface = surface;
2102
2103 // --------------------------------------------------------------------
2104 // Invocation
2105
2106 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2107
2108 // --------------------------------------------------------------------
2109 // Postconditions
2110
2111 // The returned flags are empty
2112 EXPECT_EQ(0u, flags);
2113
2114 // The current display state is unchanged.
2115 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2116}
2117
2118TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfSurfaceChanged) {
2119 using Case = SimplePrimaryDisplayCase;
2120
2121 // --------------------------------------------------------------------
2122 // Preconditions
2123
2124 // A display is already set up
2125 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2126 display.inject();
2127
2128 // There is a surface that can be set.
2129 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2130
2131 // The current display state does not have a surface
2132 display.mutableCurrentDisplayState().surface = nullptr;
2133
2134 // The incoming request sets a surface
2135 DisplayState state;
2136 state.what = DisplayState::eSurfaceChanged;
2137 state.token = display.token();
2138 state.surface = surface;
2139
2140 // --------------------------------------------------------------------
2141 // Invocation
2142
2143 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2144
2145 // --------------------------------------------------------------------
2146 // Postconditions
2147
2148 // The returned flags indicate a transaction is needed
2149 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2150
2151 // The current display layer stack state is set to the new value
2152 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2153}
2154
2155TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfLayerStackDidNotChange) {
2156 using Case = SimplePrimaryDisplayCase;
2157
2158 // --------------------------------------------------------------------
2159 // Preconditions
2160
2161 // A display is already set up
2162 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2163 display.inject();
2164
2165 // The display has a layer stack set
2166 display.mutableCurrentDisplayState().layerStack = 456u;
2167
2168 // The incoming request sets the same layer stack
2169 DisplayState state;
2170 state.what = DisplayState::eLayerStackChanged;
2171 state.token = display.token();
2172 state.layerStack = 456u;
2173
2174 // --------------------------------------------------------------------
2175 // Invocation
2176
2177 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2178
2179 // --------------------------------------------------------------------
2180 // Postconditions
2181
2182 // The returned flags are empty
2183 EXPECT_EQ(0u, flags);
2184
2185 // The current display state is unchanged
2186 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2187}
2188
2189TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfLayerStackChanged) {
2190 using Case = SimplePrimaryDisplayCase;
2191
2192 // --------------------------------------------------------------------
2193 // Preconditions
2194
2195 // A display is set up
2196 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2197 display.inject();
2198
2199 // The display has a layer stack set
2200 display.mutableCurrentDisplayState().layerStack = 654u;
2201
2202 // The incoming request sets a different layer stack
2203 DisplayState state;
2204 state.what = DisplayState::eLayerStackChanged;
2205 state.token = display.token();
2206 state.layerStack = 456u;
2207
2208 // --------------------------------------------------------------------
2209 // Invocation
2210
2211 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2212
2213 // --------------------------------------------------------------------
2214 // Postconditions
2215
2216 // The returned flags indicate a transaction is needed
2217 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2218
2219 // The desired display state has been set to the new value.
2220 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2221}
2222
2223TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfProjectionDidNotChange) {
2224 using Case = SimplePrimaryDisplayCase;
2225 constexpr int initialOrientation = 180;
2226 const Rect initialFrame = {1, 2, 3, 4};
2227 const Rect initialViewport = {5, 6, 7, 8};
2228
2229 // --------------------------------------------------------------------
2230 // Preconditions
2231
2232 // A display is set up
2233 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2234 display.inject();
2235
2236 // The current display state projection state is all set
2237 display.mutableCurrentDisplayState().orientation = initialOrientation;
2238 display.mutableCurrentDisplayState().frame = initialFrame;
2239 display.mutableCurrentDisplayState().viewport = initialViewport;
2240
2241 // The incoming request sets the same projection state
2242 DisplayState state;
2243 state.what = DisplayState::eDisplayProjectionChanged;
2244 state.token = display.token();
2245 state.orientation = initialOrientation;
2246 state.frame = initialFrame;
2247 state.viewport = initialViewport;
2248
2249 // --------------------------------------------------------------------
2250 // Invocation
2251
2252 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2253
2254 // --------------------------------------------------------------------
2255 // Postconditions
2256
2257 // The returned flags are empty
2258 EXPECT_EQ(0u, flags);
2259
2260 // The current display state is unchanged
2261 EXPECT_EQ(initialOrientation, display.getCurrentDisplayState().orientation);
2262
2263 EXPECT_EQ(initialFrame, display.getCurrentDisplayState().frame);
2264 EXPECT_EQ(initialViewport, display.getCurrentDisplayState().viewport);
2265}
2266
2267TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfOrientationChanged) {
2268 using Case = SimplePrimaryDisplayCase;
2269 constexpr int initialOrientation = 90;
2270 constexpr int desiredOrientation = 180;
2271
2272 // --------------------------------------------------------------------
2273 // Preconditions
2274
2275 // A display is set up
2276 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2277 display.inject();
2278
2279 // The current display state has an orientation set
2280 display.mutableCurrentDisplayState().orientation = initialOrientation;
2281
2282 // The incoming request sets a different orientation
2283 DisplayState state;
2284 state.what = DisplayState::eDisplayProjectionChanged;
2285 state.token = display.token();
2286 state.orientation = desiredOrientation;
2287
2288 // --------------------------------------------------------------------
2289 // Invocation
2290
2291 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2292
2293 // --------------------------------------------------------------------
2294 // Postconditions
2295
2296 // The returned flags indicate a transaction is needed
2297 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2298
2299 // The current display state has the new value.
2300 EXPECT_EQ(desiredOrientation, display.getCurrentDisplayState().orientation);
2301}
2302
2303TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfFrameChanged) {
2304 using Case = SimplePrimaryDisplayCase;
2305 const Rect initialFrame = {0, 0, 0, 0};
2306 const Rect desiredFrame = {5, 6, 7, 8};
2307
2308 // --------------------------------------------------------------------
2309 // Preconditions
2310
2311 // A display is set up
2312 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2313 display.inject();
2314
2315 // The current display state does not have a frame
2316 display.mutableCurrentDisplayState().frame = initialFrame;
2317
2318 // The incoming request sets a frame
2319 DisplayState state;
2320 state.what = DisplayState::eDisplayProjectionChanged;
2321 state.token = display.token();
2322 state.frame = desiredFrame;
2323
2324 // --------------------------------------------------------------------
2325 // Invocation
2326
2327 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2328
2329 // --------------------------------------------------------------------
2330 // Postconditions
2331
2332 // The returned flags indicate a transaction is needed
2333 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2334
2335 // The current display state has the new value.
2336 EXPECT_EQ(desiredFrame, display.getCurrentDisplayState().frame);
2337}
2338
2339TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfViewportChanged) {
2340 using Case = SimplePrimaryDisplayCase;
2341 const Rect initialViewport = {0, 0, 0, 0};
2342 const Rect desiredViewport = {5, 6, 7, 8};
2343
2344 // --------------------------------------------------------------------
2345 // Preconditions
2346
2347 // A display is set up
2348 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2349 display.inject();
2350
2351 // The current display state does not have a viewport
2352 display.mutableCurrentDisplayState().viewport = initialViewport;
2353
2354 // The incoming request sets a viewport
2355 DisplayState state;
2356 state.what = DisplayState::eDisplayProjectionChanged;
2357 state.token = display.token();
2358 state.viewport = desiredViewport;
2359
2360 // --------------------------------------------------------------------
2361 // Invocation
2362
2363 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2364
2365 // --------------------------------------------------------------------
2366 // Postconditions
2367
2368 // The returned flags indicate a transaction is needed
2369 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2370
2371 // The current display state has the new value.
2372 EXPECT_EQ(desiredViewport, display.getCurrentDisplayState().viewport);
2373}
2374
2375TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSizeDidNotChange) {
2376 using Case = SimplePrimaryDisplayCase;
2377 constexpr uint32_t initialWidth = 1024;
2378 constexpr uint32_t initialHeight = 768;
2379
2380 // --------------------------------------------------------------------
2381 // Preconditions
2382
2383 // A display is set up
2384 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2385 display.inject();
2386
2387 // The current display state has a size set
2388 display.mutableCurrentDisplayState().width = initialWidth;
2389 display.mutableCurrentDisplayState().height = initialHeight;
2390
2391 // The incoming request sets the same display size
2392 DisplayState state;
2393 state.what = DisplayState::eDisplaySizeChanged;
2394 state.token = display.token();
2395 state.width = initialWidth;
2396 state.height = initialHeight;
2397
2398 // --------------------------------------------------------------------
2399 // Invocation
2400
2401 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2402
2403 // --------------------------------------------------------------------
2404 // Postconditions
2405
2406 // The returned flags are empty
2407 EXPECT_EQ(0u, flags);
2408
2409 // The current display state is unchanged
2410 EXPECT_EQ(initialWidth, display.getCurrentDisplayState().width);
2411 EXPECT_EQ(initialHeight, display.getCurrentDisplayState().height);
2412}
2413
2414TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfWidthChanged) {
2415 using Case = SimplePrimaryDisplayCase;
2416 constexpr uint32_t initialWidth = 0;
2417 constexpr uint32_t desiredWidth = 1024;
2418
2419 // --------------------------------------------------------------------
2420 // Preconditions
2421
2422 // A display is set up
2423 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2424 display.inject();
2425
2426 // The display does not yet have a width
2427 display.mutableCurrentDisplayState().width = initialWidth;
2428
2429 // The incoming request sets a display width
2430 DisplayState state;
2431 state.what = DisplayState::eDisplaySizeChanged;
2432 state.token = display.token();
2433 state.width = desiredWidth;
2434
2435 // --------------------------------------------------------------------
2436 // Invocation
2437
2438 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2439
2440 // --------------------------------------------------------------------
2441 // Postconditions
2442
2443 // The returned flags indicate a transaction is needed
2444 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2445
2446 // The current display state has the new value.
2447 EXPECT_EQ(desiredWidth, display.getCurrentDisplayState().width);
2448}
2449
2450TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfHeightChanged) {
2451 using Case = SimplePrimaryDisplayCase;
2452 constexpr uint32_t initialHeight = 0;
2453 constexpr uint32_t desiredHeight = 768;
2454
2455 // --------------------------------------------------------------------
2456 // Preconditions
2457
2458 // A display is set up
2459 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2460 display.inject();
2461
2462 // The display does not yet have a height
2463 display.mutableCurrentDisplayState().height = initialHeight;
2464
2465 // The incoming request sets a display height
2466 DisplayState state;
2467 state.what = DisplayState::eDisplaySizeChanged;
2468 state.token = display.token();
2469 state.height = desiredHeight;
2470
2471 // --------------------------------------------------------------------
2472 // Invocation
2473
2474 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2475
2476 // --------------------------------------------------------------------
2477 // Postconditions
2478
2479 // The returned flags indicate a transaction is needed
2480 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2481
2482 // The current display state has the new value.
2483 EXPECT_EQ(desiredHeight, display.getCurrentDisplayState().height);
2484}
2485
Lloyd Pique86016da2018-03-01 16:09:38 -08002486/* ------------------------------------------------------------------------
2487 * SurfaceFlinger::onInitializeDisplays
2488 */
2489
2490TEST_F(DisplayTransactionTest, onInitializeDisplaysSetsUpPrimaryDisplay) {
2491 using Case = SimplePrimaryDisplayCase;
2492
2493 // --------------------------------------------------------------------
2494 // Preconditions
2495
2496 // A primary display is set up
2497 Case::Display::injectHwcDisplay(this);
2498 auto primaryDisplay = Case::Display::makeFakeExistingDisplayInjector(this);
2499 primaryDisplay.inject();
2500
2501 // --------------------------------------------------------------------
2502 // Call Expectations
2503
2504 // We expect the surface interceptor to possibly be used, but we treat it as
2505 // disabled since it is called as a side effect rather than directly by this
2506 // function.
2507 EXPECT_CALL(*mSurfaceInterceptor, isEnabled()).WillOnce(Return(false));
2508
2509 // We expect a call to get the active display config.
2510 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
2511
2512 // We expect invalidate() to be invoked once to trigger display transaction
2513 // processing.
2514 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
2515
2516 // --------------------------------------------------------------------
2517 // Invocation
2518
2519 mFlinger.onInitializeDisplays();
2520
2521 // --------------------------------------------------------------------
2522 // Postconditions
2523
2524 // The primary display should have a current state
2525 ASSERT_TRUE(hasCurrentDisplayState(primaryDisplay.token()));
2526 const auto& primaryDisplayState = getCurrentDisplayState(primaryDisplay.token());
2527 // The layer stack state should be set to zero
2528 EXPECT_EQ(0u, primaryDisplayState.layerStack);
2529 // The orientation state should be set to zero
2530 EXPECT_EQ(0, primaryDisplayState.orientation);
2531
2532 // The frame state should be set to INVALID
2533 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.frame);
2534
2535 // The viewport state should be set to INVALID
2536 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.viewport);
2537
2538 // The width and height should both be zero
2539 EXPECT_EQ(0u, primaryDisplayState.width);
2540 EXPECT_EQ(0u, primaryDisplayState.height);
2541
2542 // The display should be set to HWC_POWER_MODE_NORMAL
2543 ASSERT_TRUE(hasDisplayDevice(primaryDisplay.token()));
2544 auto displayDevice = primaryDisplay.mutableDisplayDevice();
2545 EXPECT_EQ(HWC_POWER_MODE_NORMAL, displayDevice->getPowerMode());
2546
2547 // The display refresh period should be set in the frame tracker.
2548 FrameStats stats;
2549 mFlinger.getAnimFrameTracker().getStats(&stats);
2550 EXPECT_EQ(DEFAULT_REFRESH_RATE, stats.refreshPeriodNano);
2551
2552 // The display transaction needed flag should be set.
2553 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
2554
2555 // The compositor timing should be set to default values
2556 const auto& compositorTiming = mFlinger.getCompositorTiming();
2557 EXPECT_EQ(-DEFAULT_REFRESH_RATE, compositorTiming.deadline);
2558 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.interval);
2559 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.presentLatency);
2560}
2561
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002562/* ------------------------------------------------------------------------
2563 * SurfaceFlinger::setPowerModeInternal
2564 */
2565
2566// Used when we simulate a display that supports doze.
2567struct DozeIsSupportedVariant {
2568 static constexpr bool DOZE_SUPPORTED = true;
2569 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2570 IComposerClient::PowerMode::DOZE;
2571 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2572 IComposerClient::PowerMode::DOZE_SUSPEND;
2573};
2574
2575// Used when we simulate a display that does not support doze.
2576struct DozeNotSupportedVariant {
2577 static constexpr bool DOZE_SUPPORTED = false;
2578 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2579 IComposerClient::PowerMode::ON;
2580 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2581 IComposerClient::PowerMode::ON;
2582};
2583
2584struct EventThreadBaseSupportedVariant {
2585 static void setupEventAndEventControlThreadNoCallExpectations(DisplayTransactionTest* test) {
2586 // The event control thread should not be notified.
2587 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(_)).Times(0);
2588
2589 // The event thread should not be notified.
2590 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(0);
2591 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(0);
2592 }
2593};
2594
2595struct EventThreadNotSupportedVariant : public EventThreadBaseSupportedVariant {
2596 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2597 // These calls are only expected for the primary display.
2598
2599 // Instead expect no calls.
2600 setupEventAndEventControlThreadNoCallExpectations(test);
2601 }
2602
2603 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2604 // These calls are only expected for the primary display.
2605
2606 // Instead expect no calls.
2607 setupEventAndEventControlThreadNoCallExpectations(test);
2608 }
2609};
2610
2611struct EventThreadIsSupportedVariant : public EventThreadBaseSupportedVariant {
2612 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2613 // The event control thread should be notified to enable vsyncs
2614 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(true)).Times(1);
2615
2616 // The event thread should be notified that the screen was acquired.
2617 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(1);
2618 }
2619
2620 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2621 // There should be a call to setVsyncEnabled(false)
2622 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(false)).Times(1);
2623
2624 // The event thread should not be notified that the screen was released.
2625 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(1);
2626 }
2627};
2628
Lloyd Pique41be5d22018-06-21 13:11:48 -07002629struct DispSyncIsSupportedVariant {
2630 static void setupBeginResyncCallExpectations(DisplayTransactionTest* test) {
2631 EXPECT_CALL(*test->mPrimaryDispSync, reset()).Times(1);
2632 EXPECT_CALL(*test->mPrimaryDispSync, setPeriod(DEFAULT_REFRESH_RATE)).Times(1);
2633 EXPECT_CALL(*test->mPrimaryDispSync, beginResync()).Times(1);
2634 }
2635
2636 static void setupEndResyncCallExpectations(DisplayTransactionTest* test) {
2637 EXPECT_CALL(*test->mPrimaryDispSync, endResync()).Times(1);
2638 }
2639};
2640
2641struct DispSyncNotSupportedVariant {
2642 static void setupBeginResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2643
2644 static void setupEndResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2645};
2646
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002647// --------------------------------------------------------------------
2648// Note:
2649//
2650// There are a large number of transitions we could test, however we only test a
2651// selected subset which provides complete test coverage of the implementation.
2652// --------------------------------------------------------------------
2653
2654template <int initialPowerMode, int targetPowerMode>
2655struct TransitionVariantCommon {
2656 static constexpr auto INITIAL_POWER_MODE = initialPowerMode;
2657 static constexpr auto TARGET_POWER_MODE = targetPowerMode;
2658
2659 static void verifyPostconditions(DisplayTransactionTest*) {}
2660};
2661
2662struct TransitionOffToOnVariant
2663 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_NORMAL> {
2664 template <typename Case>
2665 static void setupCallExpectations(DisplayTransactionTest* test) {
2666 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2667 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002668 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002669 Case::setupRepaintEverythingCallExpectations(test);
2670 }
2671
2672 static void verifyPostconditions(DisplayTransactionTest* test) {
2673 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2674 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2675 }
2676};
2677
2678struct TransitionOffToDozeSuspendVariant
2679 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_DOZE_SUSPEND> {
2680 template <typename Case>
2681 static void setupCallExpectations(DisplayTransactionTest* test) {
2682 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2683 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2684 Case::setupRepaintEverythingCallExpectations(test);
2685 }
2686
2687 static void verifyPostconditions(DisplayTransactionTest* test) {
2688 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2689 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2690 }
2691};
2692
2693struct TransitionOnToOffVariant
2694 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_OFF> {
2695 template <typename Case>
2696 static void setupCallExpectations(DisplayTransactionTest* test) {
2697 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002698 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002699 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2700 }
2701
2702 static void verifyPostconditions(DisplayTransactionTest* test) {
2703 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2704 }
2705};
2706
2707struct TransitionDozeSuspendToOffVariant
2708 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_OFF> {
2709 template <typename Case>
2710 static void setupCallExpectations(DisplayTransactionTest* test) {
2711 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2712 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2713 }
2714
2715 static void verifyPostconditions(DisplayTransactionTest* test) {
2716 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2717 }
2718};
2719
2720struct TransitionOnToDozeVariant
2721 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE> {
2722 template <typename Case>
2723 static void setupCallExpectations(DisplayTransactionTest* test) {
2724 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2725 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2726 }
2727};
2728
2729struct TransitionDozeSuspendToDozeVariant
2730 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_DOZE> {
2731 template <typename Case>
2732 static void setupCallExpectations(DisplayTransactionTest* test) {
2733 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002734 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002735 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2736 }
2737};
2738
2739struct TransitionDozeToOnVariant
2740 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE, HWC_POWER_MODE_NORMAL> {
2741 template <typename Case>
2742 static void setupCallExpectations(DisplayTransactionTest* test) {
2743 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2744 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2745 }
2746};
2747
2748struct TransitionDozeSuspendToOnVariant
2749 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_NORMAL> {
2750 template <typename Case>
2751 static void setupCallExpectations(DisplayTransactionTest* test) {
2752 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002753 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002754 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2755 }
2756};
2757
2758struct TransitionOnToDozeSuspendVariant
2759 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE_SUSPEND> {
2760 template <typename Case>
2761 static void setupCallExpectations(DisplayTransactionTest* test) {
2762 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002763 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002764 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2765 }
2766};
2767
2768struct TransitionOnToUnknownVariant
2769 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_LEET> {
2770 template <typename Case>
2771 static void setupCallExpectations(DisplayTransactionTest* test) {
2772 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2773 Case::setupNoComposerPowerModeCallExpectations(test);
2774 }
2775};
2776
2777// --------------------------------------------------------------------
2778// Note:
2779//
2780// Rather than testing the cartesian product of of
2781// DozeIsSupported/DozeNotSupported with all other options, we use one for one
2782// display type, and the other for another display type.
2783// --------------------------------------------------------------------
2784
2785template <typename DisplayVariant, typename DozeVariant, typename EventThreadVariant,
Lloyd Pique41be5d22018-06-21 13:11:48 -07002786 typename DispSyncVariant, typename TransitionVariant>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002787struct DisplayPowerCase {
2788 using Display = DisplayVariant;
2789 using Doze = DozeVariant;
2790 using EventThread = EventThreadVariant;
Lloyd Pique41be5d22018-06-21 13:11:48 -07002791 using DispSync = DispSyncVariant;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002792 using Transition = TransitionVariant;
2793
2794 static auto injectDisplayWithInitialPowerMode(DisplayTransactionTest* test, int mode) {
2795 Display::injectHwcDisplay(test);
2796 auto display = Display::makeFakeExistingDisplayInjector(test);
2797 display.inject();
2798 display.mutableDisplayDevice()->setPowerMode(mode);
2799 return display;
2800 }
2801
2802 static void setInitialPrimaryHWVsyncEnabled(DisplayTransactionTest* test, bool enabled) {
2803 test->mFlinger.mutablePrimaryHWVsyncEnabled() = enabled;
2804 }
2805
2806 static void setupRepaintEverythingCallExpectations(DisplayTransactionTest* test) {
2807 EXPECT_CALL(*test->mMessageQueue, invalidate()).Times(1);
2808 }
2809
2810 static void setupSurfaceInterceptorCallExpectations(DisplayTransactionTest* test, int mode) {
2811 EXPECT_CALL(*test->mSurfaceInterceptor, isEnabled()).WillOnce(Return(true));
2812 EXPECT_CALL(*test->mSurfaceInterceptor, savePowerModeUpdate(_, mode)).Times(1);
2813 }
2814
2815 static void setupComposerCallExpectations(DisplayTransactionTest* test,
2816 IComposerClient::PowerMode mode) {
2817 // Any calls to get the active config will return a default value.
2818 EXPECT_CALL(*test->mComposer, getActiveConfig(Display::HWC_DISPLAY_ID, _))
2819 .WillRepeatedly(DoAll(SetArgPointee<1>(Display::HWC_ACTIVE_CONFIG_ID),
2820 Return(Error::NONE)));
2821
2822 // Any calls to get whether the display supports dozing will return the value set by the
2823 // policy variant.
2824 EXPECT_CALL(*test->mComposer, getDozeSupport(Display::HWC_DISPLAY_ID, _))
2825 .WillRepeatedly(DoAll(SetArgPointee<1>(Doze::DOZE_SUPPORTED), Return(Error::NONE)));
2826
2827 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, mode)).Times(1);
2828 }
2829
2830 static void setupNoComposerPowerModeCallExpectations(DisplayTransactionTest* test) {
2831 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, _)).Times(0);
2832 }
2833};
2834
2835// A sample configuration for the primary display.
2836// In addition to having event thread support, we emulate doze support.
2837template <typename TransitionVariant>
2838using PrimaryDisplayPowerCase = DisplayPowerCase<PrimaryDisplayVariant, DozeIsSupportedVariant,
Lloyd Pique41be5d22018-06-21 13:11:48 -07002839 EventThreadIsSupportedVariant,
2840 DispSyncIsSupportedVariant, TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002841
2842// A sample configuration for the external display.
2843// In addition to not having event thread support, we emulate not having doze
2844// support.
2845template <typename TransitionVariant>
Lloyd Pique41be5d22018-06-21 13:11:48 -07002846using ExternalDisplayPowerCase = DisplayPowerCase<ExternalDisplayVariant, DozeNotSupportedVariant,
2847 EventThreadNotSupportedVariant,
2848 DispSyncNotSupportedVariant, TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002849
2850class SetPowerModeInternalTest : public DisplayTransactionTest {
2851public:
2852 template <typename Case>
2853 void transitionDisplayCommon();
2854};
2855
2856template <int PowerMode>
2857struct PowerModeInitialVSyncEnabled : public std::false_type {};
2858
2859template <>
2860struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_NORMAL> : public std::true_type {};
2861
2862template <>
2863struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_DOZE> : public std::true_type {};
2864
2865template <typename Case>
2866void SetPowerModeInternalTest::transitionDisplayCommon() {
2867 // --------------------------------------------------------------------
2868 // Preconditions
2869
2870 auto display =
2871 Case::injectDisplayWithInitialPowerMode(this, Case::Transition::INITIAL_POWER_MODE);
2872 Case::setInitialPrimaryHWVsyncEnabled(this,
2873 PowerModeInitialVSyncEnabled<
2874 Case::Transition::INITIAL_POWER_MODE>::value);
2875
2876 // --------------------------------------------------------------------
2877 // Call Expectations
2878
2879 Case::setupSurfaceInterceptorCallExpectations(this, Case::Transition::TARGET_POWER_MODE);
2880 Case::Transition::template setupCallExpectations<Case>(this);
2881
2882 // --------------------------------------------------------------------
2883 // Invocation
2884
2885 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(),
2886 Case::Transition::TARGET_POWER_MODE);
2887
2888 // --------------------------------------------------------------------
2889 // Postconditions
2890
2891 Case::Transition::verifyPostconditions(this);
2892}
2893
2894TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfNoChange) {
2895 using Case = SimplePrimaryDisplayCase;
2896
2897 // --------------------------------------------------------------------
2898 // Preconditions
2899
2900 // A primary display device is set up
2901 Case::Display::injectHwcDisplay(this);
2902 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2903 display.inject();
2904
Dominik Laskowskia2edf612018-06-01 13:15:16 -07002905 // The display is already set to HWC_POWER_MODE_NORMAL
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002906 display.mutableDisplayDevice()->setPowerMode(HWC_POWER_MODE_NORMAL);
2907
2908 // --------------------------------------------------------------------
2909 // Invocation
2910
2911 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_NORMAL);
2912
2913 // --------------------------------------------------------------------
2914 // Postconditions
2915
2916 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2917}
2918
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002919TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfVirtualDisplay) {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002920 using Case = HwcVirtualDisplayCase;
2921
2922 // --------------------------------------------------------------------
2923 // Preconditions
2924
Dominik Laskowski075d3172018-05-24 15:50:06 -07002925 // Insert display data so that the HWC thinks it created the virtual display.
2926 const auto displayId = Case::Display::DISPLAY_ID::get();
2927 ASSERT_TRUE(displayId);
2928 mFlinger.mutableHwcDisplayData()[*displayId] = {};
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002929
2930 // A virtual display device is set up
2931 Case::Display::injectHwcDisplay(this);
2932 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2933 display.inject();
2934
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002935 // The display is set to HWC_POWER_MODE_NORMAL
2936 getDisplayDevice(display.token())->setPowerMode(HWC_POWER_MODE_NORMAL);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002937
2938 // --------------------------------------------------------------------
2939 // Invocation
2940
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002941 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_OFF);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002942
2943 // --------------------------------------------------------------------
2944 // Postconditions
2945
2946 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2947}
2948
2949TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnPrimaryDisplay) {
2950 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToOnVariant>>();
2951}
2952
2953TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendPrimaryDisplay) {
2954 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
2955}
2956
2957TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffPrimaryDisplay) {
2958 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToOffVariant>>();
2959}
2960
2961TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffPrimaryDisplay) {
2962 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
2963}
2964
2965TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozePrimaryDisplay) {
2966 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeVariant>>();
2967}
2968
2969TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozePrimaryDisplay) {
2970 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
2971}
2972
2973TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnPrimaryDisplay) {
2974 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeToOnVariant>>();
2975}
2976
2977TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnPrimaryDisplay) {
2978 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
2979}
2980
2981TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendPrimaryDisplay) {
2982 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
2983}
2984
2985TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownPrimaryDisplay) {
2986 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToUnknownVariant>>();
2987}
2988
2989TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnExternalDisplay) {
2990 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToOnVariant>>();
2991}
2992
2993TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendExternalDisplay) {
2994 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
2995}
2996
2997TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffExternalDisplay) {
2998 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToOffVariant>>();
2999}
3000
3001TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffExternalDisplay) {
3002 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
3003}
3004
3005TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeExternalDisplay) {
3006 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeVariant>>();
3007}
3008
3009TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozeExternalDisplay) {
3010 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
3011}
3012
3013TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnExternalDisplay) {
3014 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeToOnVariant>>();
3015}
3016
3017TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnExternalDisplay) {
3018 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
3019}
3020
3021TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendExternalDisplay) {
3022 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
3023}
3024
3025TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownExternalDisplay) {
3026 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToUnknownVariant>>();
3027}
3028
Lloyd Piquef58625d2017-12-19 13:22:33 -08003029} // namespace
3030} // namespace android