blob: 2c1833bb8fe5036ebef76048145a63bed2e9bad5 [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 Pique3d0c02e2018-10-19 18:38:12 -070022#include <compositionengine/Display.h>
23#include <compositionengine/DisplayColorProfile.h>
Lloyd Pique542307f2018-10-19 13:24:08 -070024#include <compositionengine/mock/DisplaySurface.h>
Lloyd Piquef58625d2017-12-19 13:22:33 -080025#include <gmock/gmock.h>
26#include <gtest/gtest.h>
Lloyd Piquef58625d2017-12-19 13:22:33 -080027#include <log/log.h>
Lloyd Pique3823e7b2018-10-18 16:58:10 -070028#include <renderengine/mock/RenderEngine.h>
Valerie Hau9758ae02018-10-09 16:05:09 -070029#include <ui/DebugUtils.h>
Dominik Laskowski075d3172018-05-24 15:50:06 -070030
31#include "DisplayIdentificationTest.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080032#include "TestableSurfaceFlinger.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080033#include "mock/DisplayHardware/MockComposer.h"
Lloyd Pique41be5d22018-06-21 13:11:48 -070034#include "mock/MockDispSync.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080035#include "mock/MockEventControlThread.h"
36#include "mock/MockEventThread.h"
37#include "mock/MockMessageQueue.h"
38#include "mock/MockNativeWindowSurface.h"
39#include "mock/MockSurfaceInterceptor.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080040#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();
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800121 mock::EventThread* mSFEventThread = new mock::EventThread();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800122 mock::EventControlThread* mEventControlThread = new mock::EventControlThread();
Alec Mouriba013fa2018-10-16 12:43:11 -0700123 sp<mock::NativeWindow> mNativeWindow = new mock::NativeWindow();
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800124 sp<GraphicBuffer> mBuffer = new GraphicBuffer();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800125
126 // These mocks are created by the test, but are destroyed by SurfaceFlinger
127 // by virtue of being stored into a std::unique_ptr. However we still need
128 // to keep a reference to them for use in setting up call expectations.
Peiyong Lin833074a2018-08-28 11:53:54 -0700129 renderengine::mock::RenderEngine* mRenderEngine = new renderengine::mock::RenderEngine();
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800130 Hwc2::mock::Composer* mComposer = nullptr;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800131 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
132 mock::SurfaceInterceptor* mSurfaceInterceptor = new mock::SurfaceInterceptor();
Lloyd Pique41be5d22018-06-21 13:11:48 -0700133 mock::DispSync* mPrimaryDispSync = new mock::DispSync();
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800134
135 // These mocks are created only when expected to be created via a factory.
136 sp<mock::GraphicBufferConsumer> mConsumer;
137 sp<mock::GraphicBufferProducer> mProducer;
Lloyd Pique22098362018-09-13 11:46:49 -0700138 surfaceflinger::mock::NativeWindowSurface* mNativeWindowSurface = 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);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800165 mFlinger.mutableSFEventThread().reset(mSFEventThread);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800166 mFlinger.mutableEventQueue().reset(mMessageQueue);
Peiyong Lin833074a2018-08-28 11:53:54 -0700167 mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800168 mFlinger.mutableInterceptor().reset(mSurfaceInterceptor);
Lloyd Pique41be5d22018-06-21 13:11:48 -0700169 mFlinger.mutablePrimaryDispSync().reset(mPrimaryDispSync);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800170
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800171 injectMockComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -0800172}
173
174DisplayTransactionTest::~DisplayTransactionTest() {
175 const ::testing::TestInfo* const test_info =
176 ::testing::UnitTest::GetInstance()->current_test_info();
177 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
178}
179
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800180void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
181 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800182 EXPECT_CALL(*mComposer, getCapabilities())
183 .WillOnce(Return(std::vector<IComposer::Capability>()));
184 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
185 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -0800186
Lloyd Piquee39cad22017-12-20 17:01:29 -0800187 Mock::VerifyAndClear(mComposer);
188}
189
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800190void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800191 // This setup is only expected once per test.
192 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
193
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800194 mConsumer = new mock::GraphicBufferConsumer();
195 mProducer = new mock::GraphicBufferProducer();
196
197 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
198 *outProducer = mProducer;
199 *outConsumer = mConsumer;
200 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800201}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800202
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800203void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800204 // This setup is only expected once per test.
205 ASSERT_TRUE(mNativeWindowSurface == nullptr);
206
Lloyd Pique22098362018-09-13 11:46:49 -0700207 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800208
Lloyd Pique22098362018-09-13 11:46:49 -0700209 mFlinger.setCreateNativeWindowSurface([this](auto) {
210 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
211 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800212}
213
Dominik Laskowski075d3172018-05-24 15:50:06 -0700214bool DisplayTransactionTest::hasPhysicalHwcDisplay(hwc2_display_t hwcDisplayId) {
215 return mFlinger.mutableHwcPhysicalDisplayIdMap().count(hwcDisplayId) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800216}
217
218bool DisplayTransactionTest::hasTransactionFlagSet(int flag) {
219 return mFlinger.mutableTransactionFlags() & flag;
220}
221
222bool DisplayTransactionTest::hasDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700223 return mFlinger.mutableDisplays().count(displayToken) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800224}
225
226sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700227 return mFlinger.mutableDisplays()[displayToken];
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800228}
229
230bool DisplayTransactionTest::hasCurrentDisplayState(sp<IBinder> displayToken) {
231 return mFlinger.mutableCurrentState().displays.indexOfKey(displayToken) >= 0;
232}
233
234const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(sp<IBinder> displayToken) {
235 return mFlinger.mutableCurrentState().displays.valueFor(displayToken);
236}
237
238bool DisplayTransactionTest::hasDrawingDisplayState(sp<IBinder> displayToken) {
239 return mFlinger.mutableDrawingState().displays.indexOfKey(displayToken) >= 0;
240}
241
242const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(sp<IBinder> displayToken) {
243 return mFlinger.mutableDrawingState().displays.valueFor(displayToken);
244}
245
246/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800247 *
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800248 */
249
Dominik Laskowski075d3172018-05-24 15:50:06 -0700250template <typename PhysicalDisplay>
251struct PhysicalDisplayId {};
252
Dominik Laskowski34157762018-10-31 13:07:19 -0700253template <DisplayId::Type displayId>
254using VirtualDisplayId = std::integral_constant<DisplayId::Type, displayId>;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700255
256struct NoDisplayId {};
257
258template <typename>
259struct IsPhysicalDisplayId : std::bool_constant<false> {};
260
261template <typename PhysicalDisplay>
262struct IsPhysicalDisplayId<PhysicalDisplayId<PhysicalDisplay>> : std::bool_constant<true> {};
263
264template <typename>
265struct DisplayIdGetter;
266
267template <typename PhysicalDisplay>
268struct DisplayIdGetter<PhysicalDisplayId<PhysicalDisplay>> {
269 static std::optional<DisplayId> get() {
270 if (!PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
271 return getFallbackDisplayId(static_cast<bool>(PhysicalDisplay::PRIMARY)
272 ? HWC_DISPLAY_PRIMARY
273 : HWC_DISPLAY_EXTERNAL);
274 }
275
276 const auto info =
277 parseDisplayIdentificationData(PhysicalDisplay::PORT,
278 PhysicalDisplay::GET_IDENTIFICATION_DATA());
279 return info ? std::make_optional(info->id) : std::nullopt;
280 }
281};
282
Dominik Laskowski34157762018-10-31 13:07:19 -0700283template <DisplayId::Type displayId>
Dominik Laskowski075d3172018-05-24 15:50:06 -0700284struct DisplayIdGetter<VirtualDisplayId<displayId>> {
Dominik Laskowski34157762018-10-31 13:07:19 -0700285 static std::optional<DisplayId> get() { return DisplayId{displayId}; }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700286};
287
288template <>
289struct DisplayIdGetter<NoDisplayId> {
290 static std::optional<DisplayId> get() { return {}; }
291};
292
293// DisplayIdType can be:
294// 1) PhysicalDisplayId<...> for generated ID of physical display backed by HWC.
295// 2) VirtualDisplayId<...> for hard-coded ID of virtual display backed by HWC.
296// 3) NoDisplayId for virtual display without HWC backing.
297template <typename DisplayIdType, int width, int height, Critical critical, Async async,
298 Secure secure, Primary primary, int grallocUsage>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800299struct DisplayVariant {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700300 using DISPLAY_ID = DisplayIdGetter<DisplayIdType>;
301
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800302 // The display width and height
303 static constexpr int WIDTH = width;
304 static constexpr int HEIGHT = height;
305
306 static constexpr int GRALLOC_USAGE = grallocUsage;
307
Dominik Laskowski075d3172018-05-24 15:50:06 -0700308 // Whether the display is virtual or physical
309 static constexpr Virtual VIRTUAL =
310 IsPhysicalDisplayId<DisplayIdType>{} ? Virtual::FALSE : Virtual::TRUE;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800311
312 // When creating native window surfaces for the framebuffer, whether those should be critical
313 static constexpr Critical CRITICAL = critical;
314
315 // When creating native window surfaces for the framebuffer, whether those should be async
316 static constexpr Async ASYNC = async;
317
318 // Whether the display should be treated as secure
319 static constexpr Secure SECURE = secure;
320
Dominik Laskowski075d3172018-05-24 15:50:06 -0700321 // Whether the display is primary
322 static constexpr Primary PRIMARY = primary;
323
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800324 static constexpr auto displayType() {
325 return static_cast<bool>(PRIMARY) ? EventThread::DisplayType::Primary
326 : EventThread::DisplayType::External;
327 }
328
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800329 static auto makeFakeExistingDisplayInjector(DisplayTransactionTest* test) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700330 auto injector =
331 FakeDisplayDeviceInjector(test->mFlinger, DISPLAY_ID::get(),
332 static_cast<bool>(VIRTUAL), static_cast<bool>(PRIMARY));
333
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800334 injector.setSecure(static_cast<bool>(SECURE));
Alec Mouriba013fa2018-10-16 12:43:11 -0700335 injector.setNativeWindow(test->mNativeWindow);
336
337 // Creating a DisplayDevice requires getting default dimensions from the
338 // native window.
339 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
340 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
341 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
342 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800343 return injector;
344 }
345
346 // Called by tests to set up any native window creation call expectations.
347 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
348 EXPECT_CALL(*test->mNativeWindowSurface, getNativeWindow())
349 .WillOnce(Return(test->mNativeWindow));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800350
Alec Mouriba013fa2018-10-16 12:43:11 -0700351 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
352 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
353 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
354 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800355 }
356
357 static void setupFramebufferConsumerBufferQueueCallExpectations(DisplayTransactionTest* test) {
358 EXPECT_CALL(*test->mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR));
359 EXPECT_CALL(*test->mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR));
360 EXPECT_CALL(*test->mConsumer, setConsumerUsageBits(GRALLOC_USAGE))
361 .WillRepeatedly(Return(NO_ERROR));
362 EXPECT_CALL(*test->mConsumer, setDefaultBufferSize(WIDTH, HEIGHT))
363 .WillRepeatedly(Return(NO_ERROR));
364 EXPECT_CALL(*test->mConsumer, setMaxAcquiredBufferCount(_))
365 .WillRepeatedly(Return(NO_ERROR));
366 }
367
368 static void setupFramebufferProducerBufferQueueCallExpectations(DisplayTransactionTest* test) {
369 EXPECT_CALL(*test->mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return());
370 }
371};
372
Dominik Laskowski075d3172018-05-24 15:50:06 -0700373template <hwc2_display_t hwcDisplayId, HWC2::DisplayType hwcDisplayType, typename DisplayVariant,
374 typename PhysicalDisplay = void>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800375struct HwcDisplayVariant {
376 // The display id supplied by the HWC
377 static constexpr hwc2_display_t HWC_DISPLAY_ID = hwcDisplayId;
378
379 // The HWC display type
380 static constexpr HWC2::DisplayType HWC_DISPLAY_TYPE = hwcDisplayType;
381
382 // The HWC active configuration id
Lloyd Pique3c085a02018-05-09 19:38:32 -0700383 static constexpr int HWC_ACTIVE_CONFIG_ID = 2001;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800384
385 static void injectPendingHotplugEvent(DisplayTransactionTest* test,
386 HWC2::Connection connection) {
387 test->mFlinger.mutablePendingHotplugEvents().emplace_back(
388 HotplugEvent{HWC_DISPLAY_ID, connection});
389 }
390
391 // Called by tests to inject a HWC display setup
392 static void injectHwcDisplay(DisplayTransactionTest* test) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700393 const auto displayId = DisplayVariant::DISPLAY_ID::get();
394 ASSERT_TRUE(displayId);
395 FakeHwcDisplayInjector(*displayId, HWC_DISPLAY_TYPE,
396 static_cast<bool>(DisplayVariant::PRIMARY))
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800397 .setHwcDisplayId(HWC_DISPLAY_ID)
398 .setWidth(DisplayVariant::WIDTH)
399 .setHeight(DisplayVariant::HEIGHT)
400 .setActiveConfig(HWC_ACTIVE_CONFIG_ID)
401 .inject(&test->mFlinger, test->mComposer);
402 }
403
404 static void setupHwcHotplugCallExpectations(DisplayTransactionTest* test) {
405 EXPECT_CALL(*test->mComposer, getDisplayType(HWC_DISPLAY_ID, _))
406 .WillOnce(DoAll(SetArgPointee<1>(static_cast<IComposerClient::DisplayType>(
407 HWC_DISPLAY_TYPE)),
408 Return(Error::NONE)));
409 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
410 EXPECT_CALL(*test->mComposer, getDisplayConfigs(HWC_DISPLAY_ID, _))
411 .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{HWC_ACTIVE_CONFIG_ID}),
412 Return(Error::NONE)));
413 EXPECT_CALL(*test->mComposer,
414 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
415 IComposerClient::Attribute::WIDTH, _))
416 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::WIDTH), Return(Error::NONE)));
417 EXPECT_CALL(*test->mComposer,
418 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
419 IComposerClient::Attribute::HEIGHT, _))
420 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::HEIGHT), Return(Error::NONE)));
421 EXPECT_CALL(*test->mComposer,
422 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
423 IComposerClient::Attribute::VSYNC_PERIOD, _))
424 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE)));
425 EXPECT_CALL(*test->mComposer,
426 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
427 IComposerClient::Attribute::DPI_X, _))
428 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
429 EXPECT_CALL(*test->mComposer,
430 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
431 IComposerClient::Attribute::DPI_Y, _))
432 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700433
434 if (PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
435 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
436 .WillOnce(DoAll(SetArgPointee<1>(PhysicalDisplay::PORT),
437 SetArgPointee<2>(PhysicalDisplay::GET_IDENTIFICATION_DATA()),
438 Return(Error::NONE)));
439 } else {
440 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
441 .WillOnce(Return(Error::UNSUPPORTED));
442 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800443 }
444
445 // Called by tests to set up HWC call expectations
446 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
447 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY_ID, _))
Lloyd Pique3c085a02018-05-09 19:38:32 -0700448 .WillRepeatedly(DoAll(SetArgPointee<1>(HWC_ACTIVE_CONFIG_ID), Return(Error::NONE)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800449 }
450};
451
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800452// Physical displays are expected to be synchronous, secure, and have a HWC display for output.
453constexpr uint32_t GRALLOC_USAGE_PHYSICAL_DISPLAY =
454 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
455
Dominik Laskowski075d3172018-05-24 15:50:06 -0700456template <hwc2_display_t hwcDisplayId, typename PhysicalDisplay, int width, int height,
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800457 Critical critical>
458struct PhysicalDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700459 : DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height, critical, Async::FALSE,
460 Secure::TRUE, PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
461 HwcDisplayVariant<hwcDisplayId, HWC2::DisplayType::Physical,
462 DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height,
463 critical, Async::FALSE, Secure::TRUE,
464 PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
465 PhysicalDisplay> {};
466
467template <bool hasIdentificationData>
468struct PrimaryDisplay {
469 static constexpr Primary PRIMARY = Primary::TRUE;
470 static constexpr uint8_t PORT = 255;
471 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
472 static constexpr auto GET_IDENTIFICATION_DATA = getInternalEdid;
473};
474
475template <bool hasIdentificationData>
476struct ExternalDisplay {
477 static constexpr Primary PRIMARY = Primary::FALSE;
478 static constexpr uint8_t PORT = 254;
479 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
480 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
481};
482
483struct TertiaryDisplay {
484 static constexpr Primary PRIMARY = Primary::FALSE;
485};
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800486
487// A primary display is a physical display that is critical
488using PrimaryDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700489 PhysicalDisplayVariant<1001, PrimaryDisplay<false>, 3840, 2160, Critical::TRUE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800490
491// An external display is physical display that is not critical.
492using ExternalDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700493 PhysicalDisplayVariant<1002, ExternalDisplay<false>, 1920, 1280, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800494
495using TertiaryDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700496 PhysicalDisplayVariant<1003, TertiaryDisplay, 1600, 1200, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800497
498// A virtual display not supported by the HWC.
499constexpr uint32_t GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY = 0;
500
501template <int width, int height, Secure secure>
502struct NonHwcVirtualDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700503 : DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
504 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY> {
505 using Base = DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
506 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>;
507
508 static void injectHwcDisplay(DisplayTransactionTest*) {}
509
510 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
511 EXPECT_CALL(*test->mComposer, getActiveConfig(_, _)).Times(0);
512 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800513
514 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
515 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
516 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
517 }
518};
519
520// A virtual display supported by the HWC.
521constexpr uint32_t GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY = GRALLOC_USAGE_HW_COMPOSER;
522
523template <int width, int height, Secure secure>
524struct HwcVirtualDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700525 : DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE, secure,
526 Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>,
527 HwcDisplayVariant<
528 1010, HWC2::DisplayType::Virtual,
529 DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
530 secure, Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>> {
531 using Base = DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
532 secure, Primary::FALSE, GRALLOC_USAGE_HW_COMPOSER>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800533 using Self = HwcVirtualDisplayVariant<width, height, secure>;
534
535 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
536 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
537 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
538 }
539
540 static void setupHwcVirtualDisplayCreationCallExpectations(DisplayTransactionTest* test) {
541 EXPECT_CALL(*test->mComposer, createVirtualDisplay(Base::WIDTH, Base::HEIGHT, _, _))
542 .WillOnce(DoAll(SetArgPointee<3>(Self::HWC_DISPLAY_ID), Return(Error::NONE)));
543 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
544 }
545};
546
547// For this variant, SurfaceFlinger should not configure itself with wide
548// display support, so the display should not be configured for wide-color
549// support.
550struct WideColorSupportNotConfiguredVariant {
551 static constexpr bool WIDE_COLOR_SUPPORTED = false;
552
553 static void injectConfigChange(DisplayTransactionTest* test) {
554 test->mFlinger.mutableHasWideColorDisplay() = false;
Peiyong Lin13effd12018-07-24 17:01:47 -0700555 test->mFlinger.mutableUseColorManagement() = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800556 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
557 }
558
559 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
560 EXPECT_CALL(*test->mComposer, getColorModes(_, _)).Times(0);
561 EXPECT_CALL(*test->mComposer, getRenderIntents(_, _, _)).Times(0);
562 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
563 }
564};
565
566// For this variant, SurfaceFlinger should configure itself with wide display
567// support, and the display should respond with an non-empty list of supported
568// color modes. Wide-color support should be configured.
569template <typename Display>
570struct WideColorP3ColorimetricSupportedVariant {
571 static constexpr bool WIDE_COLOR_SUPPORTED = true;
572
573 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700574 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800575 test->mFlinger.mutableHasWideColorDisplay() = true;
576 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
577 }
578
579 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
580 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
581 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>({ColorMode::DISPLAY_P3})),
582 Return(Error::NONE)));
583 EXPECT_CALL(*test->mComposer,
584 getRenderIntents(Display::HWC_DISPLAY_ID, ColorMode::DISPLAY_P3, _))
585 .WillOnce(DoAll(SetArgPointee<2>(
586 std::vector<RenderIntent>({RenderIntent::COLORIMETRIC})),
587 Return(Error::NONE)));
588 EXPECT_CALL(*test->mComposer,
589 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::SRGB,
590 RenderIntent::COLORIMETRIC))
591 .WillOnce(Return(Error::NONE));
592 }
593};
594
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800595// For this variant, SurfaceFlinger should configure itself with wide display
596// support, but the display should respond with an empty list of supported color
597// modes. Wide-color support for the display should not be configured.
598template <typename Display>
599struct WideColorNotSupportedVariant {
600 static constexpr bool WIDE_COLOR_SUPPORTED = false;
601
602 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700603 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800604 test->mFlinger.mutableHasWideColorDisplay() = true;
605 }
606
607 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
608 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
609 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>()), Return(Error::NONE)));
Chia-I Wu614e1422018-05-23 02:17:03 -0700610 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800611 }
612};
613
614// For this variant, the display is not a HWC display, so no HDR support should
615// be configured.
616struct NonHwcDisplayHdrSupportVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800617 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800618 static constexpr bool HDR10_SUPPORTED = false;
619 static constexpr bool HDR_HLG_SUPPORTED = false;
620 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
621 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
622 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
623 }
624};
625
Valerie Haue9e843a2018-12-18 13:39:23 -0800626template <typename Display>
627struct Hdr10PlusSupportedVariant {
628 static constexpr bool HDR10_PLUS_SUPPORTED = true;
629 static constexpr bool HDR10_SUPPORTED = true;
630 static constexpr bool HDR_HLG_SUPPORTED = false;
631 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
632 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
633 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _))
634 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({
635 Hdr::HDR10_PLUS,
636 Hdr::HDR10,
637 })),
638 Return(Error::NONE)));
639 }
640};
641
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800642// For this variant, the composer should respond with a non-empty list of HDR
643// modes containing HDR10, so HDR10 support should be configured.
644template <typename Display>
645struct Hdr10SupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800646 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800647 static constexpr bool HDR10_SUPPORTED = true;
648 static constexpr bool HDR_HLG_SUPPORTED = false;
649 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
650 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
651 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
652 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HDR10})),
653 Return(Error::NONE)));
654 }
655};
656
657// For this variant, the composer should respond with a non-empty list of HDR
658// modes containing HLG, so HLG support should be configured.
659template <typename Display>
660struct HdrHlgSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800661 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800662 static constexpr bool HDR10_SUPPORTED = false;
663 static constexpr bool HDR_HLG_SUPPORTED = true;
664 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
665 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
666 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
667 .WillOnce(
668 DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HLG})), Return(Error::NONE)));
669 }
670};
671
672// For this variant, the composer should respond with a non-empty list of HDR
673// modes containing DOLBY_VISION, so DOLBY_VISION support should be configured.
674template <typename Display>
675struct HdrDolbyVisionSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800676 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800677 static constexpr bool HDR10_SUPPORTED = false;
678 static constexpr bool HDR_HLG_SUPPORTED = false;
679 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = true;
680 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
681 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
682 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::DOLBY_VISION})),
683 Return(Error::NONE)));
684 }
685};
686
687// For this variant, the composer should respond with am empty list of HDR
688// modes, so no HDR support should be configured.
689template <typename Display>
690struct HdrNotSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800691 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800692 static constexpr bool HDR10_SUPPORTED = false;
693 static constexpr bool HDR_HLG_SUPPORTED = false;
694 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
695 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
696 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
697 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
698 }
699};
700
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700701struct NonHwcPerFrameMetadataSupportVariant {
702 static constexpr int PER_FRAME_METADATA_KEYS = 0;
703 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800704 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(_)).Times(0);
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700705 }
706};
707
708template <typename Display>
709struct NoPerFrameMetadataSupportVariant {
710 static constexpr int PER_FRAME_METADATA_KEYS = 0;
711 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800712 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
713 .WillOnce(Return(std::vector<PerFrameMetadataKey>()));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700714 }
715};
716
717template <typename Display>
718struct Smpte2086PerFrameMetadataSupportVariant {
719 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::SMPTE2086;
720 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800721 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
722 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Valerie Haue9e843a2018-12-18 13:39:23 -0800723 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X,
724 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y,
725 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X,
726 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y,
727 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X,
728 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y,
729 PerFrameMetadataKey::WHITE_POINT_X,
730 PerFrameMetadataKey::WHITE_POINT_Y,
731 PerFrameMetadataKey::MAX_LUMINANCE,
732 PerFrameMetadataKey::MIN_LUMINANCE,
733 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700734 }
735};
736
737template <typename Display>
738struct Cta861_3_PerFrameMetadataSupportVariant {
739 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::CTA861_3;
740 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800741 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
742 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Valerie Haue9e843a2018-12-18 13:39:23 -0800743 PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL,
744 PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL,
745 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700746 }
747};
748
Valerie Haue9e843a2018-12-18 13:39:23 -0800749template <typename Display>
750struct Hdr10_Plus_PerFrameMetadataSupportVariant {
751 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::HDR10PLUS;
752 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
753 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
754 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
755 PerFrameMetadataKey::HDR10_PLUS_SEI,
756 })));
757 }
758};
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800759/* ------------------------------------------------------------------------
760 * Typical display configurations to test
761 */
762
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700763template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy,
764 typename PerFrameMetadataSupportPolicy>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800765struct Case {
766 using Display = DisplayPolicy;
767 using WideColorSupport = WideColorSupportPolicy;
768 using HdrSupport = HdrSupportPolicy;
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700769 using PerFrameMetadataSupport = PerFrameMetadataSupportPolicy;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800770};
771
772using SimplePrimaryDisplayCase =
773 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700774 HdrNotSupportedVariant<PrimaryDisplayVariant>,
775 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800776using SimpleExternalDisplayCase =
777 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700778 HdrNotSupportedVariant<ExternalDisplayVariant>,
779 NoPerFrameMetadataSupportVariant<ExternalDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800780using SimpleTertiaryDisplayCase =
781 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700782 HdrNotSupportedVariant<TertiaryDisplayVariant>,
783 NoPerFrameMetadataSupportVariant<TertiaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800784using NonHwcVirtualDisplayCase =
785 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700786 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant,
787 NonHwcPerFrameMetadataSupportVariant>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800788using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
789using HwcVirtualDisplayCase =
790 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
Lloyd Pique438e9e72018-09-04 18:06:08 -0700791 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>,
tangrobin6753a022018-08-10 10:58:54 +0800792 NoPerFrameMetadataSupportVariant<SimpleHwcVirtualDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800793using WideColorP3ColorimetricDisplayCase =
794 Case<PrimaryDisplayVariant, WideColorP3ColorimetricSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700795 HdrNotSupportedVariant<PrimaryDisplayVariant>,
796 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Valerie Haue9e843a2018-12-18 13:39:23 -0800797using Hdr10PlusDisplayCase =
798 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
799 Hdr10SupportedVariant<PrimaryDisplayVariant>,
800 Hdr10_Plus_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800801using Hdr10DisplayCase =
802 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700803 Hdr10SupportedVariant<PrimaryDisplayVariant>,
804 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800805using HdrHlgDisplayCase =
806 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700807 HdrHlgSupportedVariant<PrimaryDisplayVariant>,
808 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800809using HdrDolbyVisionDisplayCase =
810 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700811 HdrDolbyVisionSupportedVariant<PrimaryDisplayVariant>,
812 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
813using HdrSmpte2086DisplayCase =
814 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
815 HdrNotSupportedVariant<PrimaryDisplayVariant>,
816 Smpte2086PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
817using HdrCta861_3_DisplayCase =
818 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
819 HdrNotSupportedVariant<PrimaryDisplayVariant>,
820 Cta861_3_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Dominik Laskowskif07b85b2018-06-11 12:49:15 -0700821
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800822/* ------------------------------------------------------------------------
Lloyd Pique6cf11032018-01-22 18:57:44 -0800823 *
824 * SurfaceFlinger::onHotplugReceived
825 */
826
827TEST_F(DisplayTransactionTest, hotplugEnqueuesEventsForDisplayTransaction) {
828 constexpr int currentSequenceId = 123;
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700829 constexpr hwc2_display_t hwcDisplayId1 = 456;
830 constexpr hwc2_display_t hwcDisplayId2 = 654;
Lloyd Pique6cf11032018-01-22 18:57:44 -0800831
832 // --------------------------------------------------------------------
833 // Preconditions
834
835 // Set the current sequence id for accepted events
836 mFlinger.mutableComposerSequenceId() = currentSequenceId;
837
838 // Set the main thread id so that the current thread does not appear to be
839 // the main thread.
840 mFlinger.mutableMainThreadId() = std::thread::id();
841
842 // --------------------------------------------------------------------
843 // Call Expectations
844
845 // We expect invalidate() to be invoked once to trigger display transaction
846 // processing.
847 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
848
849 // --------------------------------------------------------------------
850 // Invocation
851
852 // Simulate two hotplug events (a connect and a disconnect)
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700853 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId1, HWC2::Connection::Connected);
854 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId2, HWC2::Connection::Disconnected);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800855
856 // --------------------------------------------------------------------
857 // Postconditions
858
859 // The display transaction needed flag should be set.
860 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
861
862 // All events should be in the pending event queue.
863 const auto& pendingEvents = mFlinger.mutablePendingHotplugEvents();
864 ASSERT_EQ(2u, pendingEvents.size());
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700865 EXPECT_EQ(hwcDisplayId1, pendingEvents[0].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800866 EXPECT_EQ(HWC2::Connection::Connected, pendingEvents[0].connection);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700867 EXPECT_EQ(hwcDisplayId2, pendingEvents[1].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800868 EXPECT_EQ(HWC2::Connection::Disconnected, pendingEvents[1].connection);
869}
870
871TEST_F(DisplayTransactionTest, hotplugDiscardsUnexpectedEvents) {
872 constexpr int currentSequenceId = 123;
873 constexpr int otherSequenceId = 321;
874 constexpr hwc2_display_t displayId = 456;
875
876 // --------------------------------------------------------------------
877 // Preconditions
878
879 // Set the current sequence id for accepted events
880 mFlinger.mutableComposerSequenceId() = currentSequenceId;
881
882 // Set the main thread id so that the current thread does not appear to be
883 // the main thread.
884 mFlinger.mutableMainThreadId() = std::thread::id();
885
886 // --------------------------------------------------------------------
887 // Call Expectations
888
889 // We do not expect any calls to invalidate().
890 EXPECT_CALL(*mMessageQueue, invalidate()).Times(0);
891
892 // --------------------------------------------------------------------
893 // Invocation
894
895 // Call with an unexpected sequence id
896 mFlinger.onHotplugReceived(otherSequenceId, displayId, HWC2::Connection::Invalid);
897
898 // --------------------------------------------------------------------
899 // Postconditions
900
901 // The display transaction needed flag should not be set
902 EXPECT_FALSE(hasTransactionFlagSet(eDisplayTransactionNeeded));
903
904 // There should be no pending events
905 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
906}
907
908TEST_F(DisplayTransactionTest, hotplugProcessesEnqueuedEventsIfCalledOnMainThread) {
909 constexpr int currentSequenceId = 123;
910 constexpr hwc2_display_t displayId1 = 456;
911
912 // --------------------------------------------------------------------
913 // Note:
914 // --------------------------------------------------------------------
915 // This test case is a bit tricky. We want to verify that
916 // onHotplugReceived() calls processDisplayHotplugEventsLocked(), but we
917 // don't really want to provide coverage for everything the later function
918 // does as there are specific tests for it.
919 // --------------------------------------------------------------------
920
921 // --------------------------------------------------------------------
922 // Preconditions
923
924 // Set the current sequence id for accepted events
925 mFlinger.mutableComposerSequenceId() = currentSequenceId;
926
927 // Set the main thread id so that the current thread does appear to be the
928 // main thread.
929 mFlinger.mutableMainThreadId() = std::this_thread::get_id();
930
931 // --------------------------------------------------------------------
932 // Call Expectations
933
934 // We expect invalidate() to be invoked once to trigger display transaction
935 // processing.
936 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
937
938 // --------------------------------------------------------------------
939 // Invocation
940
941 // Simulate a disconnect on a display id that is not connected. This should
942 // be enqueued by onHotplugReceived(), and dequeued by
943 // processDisplayHotplugEventsLocked(), but then ignored as invalid.
944 mFlinger.onHotplugReceived(currentSequenceId, displayId1, HWC2::Connection::Disconnected);
945
946 // --------------------------------------------------------------------
947 // Postconditions
948
949 // The display transaction needed flag should be set.
950 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
951
952 // There should be no event queued on return, as it should have been
953 // processed.
954 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
955}
956
957/* ------------------------------------------------------------------------
Lloyd Piquea482f992018-01-22 19:00:34 -0800958 * SurfaceFlinger::createDisplay
959 */
960
961TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForNonsecureDisplay) {
962 const String8 name("virtual.test");
963
964 // --------------------------------------------------------------------
965 // Call Expectations
966
967 // The call should notify the interceptor that a display was created.
968 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
969
970 // --------------------------------------------------------------------
971 // Invocation
972
973 sp<IBinder> displayToken = mFlinger.createDisplay(name, false);
974
975 // --------------------------------------------------------------------
976 // Postconditions
977
978 // The display should have been added to the current state
979 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
980 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700981 EXPECT_TRUE(display.isVirtual());
982 EXPECT_FALSE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -0800983 EXPECT_EQ(name.string(), display.displayName);
984
985 // --------------------------------------------------------------------
986 // Cleanup conditions
987
988 // Destroying the display invalidates the display state.
989 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
990}
991
992TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForSecureDisplay) {
993 const String8 name("virtual.test");
994
995 // --------------------------------------------------------------------
996 // Call Expectations
997
998 // The call should notify the interceptor that a display was created.
999 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
1000
1001 // --------------------------------------------------------------------
1002 // Invocation
1003
1004 sp<IBinder> displayToken = mFlinger.createDisplay(name, true);
1005
1006 // --------------------------------------------------------------------
1007 // Postconditions
1008
1009 // The display should have been added to the current state
1010 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1011 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001012 EXPECT_TRUE(display.isVirtual());
1013 EXPECT_TRUE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -08001014 EXPECT_EQ(name.string(), display.displayName);
1015
1016 // --------------------------------------------------------------------
1017 // Cleanup conditions
1018
1019 // Destroying the display invalidates the display state.
1020 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1021}
1022
1023/* ------------------------------------------------------------------------
1024 * SurfaceFlinger::destroyDisplay
1025 */
1026
1027TEST_F(DisplayTransactionTest, destroyDisplayClearsCurrentStateForDisplay) {
1028 using Case = NonHwcVirtualDisplayCase;
1029
1030 // --------------------------------------------------------------------
1031 // Preconditions
1032
1033 // A virtual display exists
1034 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1035 existing.inject();
1036
1037 // --------------------------------------------------------------------
1038 // Call Expectations
1039
1040 // The call should notify the interceptor that a display was created.
1041 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
1042
1043 // Destroying the display invalidates the display state.
1044 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1045
1046 // --------------------------------------------------------------------
1047 // Invocation
1048
1049 mFlinger.destroyDisplay(existing.token());
1050
1051 // --------------------------------------------------------------------
1052 // Postconditions
1053
1054 // The display should have been removed from the current state
1055 EXPECT_FALSE(hasCurrentDisplayState(existing.token()));
1056
1057 // Ths display should still exist in the drawing state
1058 EXPECT_TRUE(hasDrawingDisplayState(existing.token()));
1059
1060 // The display transaction needed flasg should be set
1061 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1062}
1063
1064TEST_F(DisplayTransactionTest, destroyDisplayHandlesUnknownDisplay) {
1065 // --------------------------------------------------------------------
1066 // Preconditions
1067
1068 sp<BBinder> displayToken = new BBinder();
1069
1070 // --------------------------------------------------------------------
1071 // Invocation
1072
1073 mFlinger.destroyDisplay(displayToken);
1074}
1075
1076/* ------------------------------------------------------------------------
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001077 * SurfaceFlinger::resetDisplayState
1078 */
1079
1080TEST_F(DisplayTransactionTest, resetDisplayStateClearsState) {
1081 using Case = NonHwcVirtualDisplayCase;
1082
1083 // --------------------------------------------------------------------
1084 // Preconditions
1085
1086 // vsync is enabled and available
1087 mFlinger.mutablePrimaryHWVsyncEnabled() = true;
1088 mFlinger.mutableHWVsyncAvailable() = true;
1089
1090 // A display exists
1091 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1092 existing.inject();
1093
1094 // --------------------------------------------------------------------
1095 // Call Expectations
1096
1097 // The call disable vsyncs
1098 EXPECT_CALL(*mEventControlThread, setVsyncEnabled(false)).Times(1);
1099
Lloyd Pique41be5d22018-06-21 13:11:48 -07001100 // The call ends any display resyncs
1101 EXPECT_CALL(*mPrimaryDispSync, endResync()).Times(1);
1102
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001103 // --------------------------------------------------------------------
1104 // Invocation
1105
1106 mFlinger.resetDisplayState();
1107
1108 // --------------------------------------------------------------------
1109 // Postconditions
1110
1111 // vsyncs should be off and not available.
1112 EXPECT_FALSE(mFlinger.mutablePrimaryHWVsyncEnabled());
1113 EXPECT_FALSE(mFlinger.mutableHWVsyncAvailable());
1114
1115 // The display should have been removed from the display map.
1116 EXPECT_FALSE(hasDisplayDevice(existing.token()));
1117
1118 // The display should still exist in the current state
1119 EXPECT_TRUE(hasCurrentDisplayState(existing.token()));
1120
1121 // The display should have been removed from the drawing state
1122 EXPECT_FALSE(hasDrawingDisplayState(existing.token()));
1123}
1124
1125/* ------------------------------------------------------------------------
Valerie Hau9758ae02018-10-09 16:05:09 -07001126 * DisplayDevice::GetBestColorMode
1127 */
1128class GetBestColorModeTest : public DisplayTransactionTest {
1129public:
Dominik Laskowski34157762018-10-31 13:07:19 -07001130 static constexpr DisplayId DEFAULT_DISPLAY_ID = DisplayId{777};
Dominik Laskowski075d3172018-05-24 15:50:06 -07001131
Valerie Hau9758ae02018-10-09 16:05:09 -07001132 GetBestColorModeTest()
1133 : DisplayTransactionTest(),
Dominik Laskowski075d3172018-05-24 15:50:06 -07001134 mInjector(FakeDisplayDeviceInjector(mFlinger, DEFAULT_DISPLAY_ID, false /* isVirtual */,
1135 true /* isPrimary */)) {}
Valerie Hau9758ae02018-10-09 16:05:09 -07001136
1137 void setHasWideColorGamut(bool hasWideColorGamut) { mHasWideColorGamut = hasWideColorGamut; }
1138
1139 void addHwcColorModesMapping(ui::ColorMode colorMode,
1140 std::vector<ui::RenderIntent> renderIntents) {
1141 mHwcColorModes[colorMode] = renderIntents;
1142 }
1143
1144 void setInputDataspace(ui::Dataspace dataspace) { mInputDataspace = dataspace; }
1145
1146 void setInputRenderIntent(ui::RenderIntent renderIntent) { mInputRenderIntent = renderIntent; }
1147
1148 void getBestColorMode() {
1149 mInjector.setHwcColorModes(mHwcColorModes);
1150 mInjector.setHasWideColorGamut(mHasWideColorGamut);
Alec Mouriba013fa2018-10-16 12:43:11 -07001151 mInjector.setNativeWindow(mNativeWindow);
1152
1153 // Creating a DisplayDevice requires getting default dimensions from the
1154 // native window.
1155 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
1156 .WillRepeatedly(DoAll(SetArgPointee<1>(1080 /* arbitrary */), Return(0)));
1157 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
1158 .WillRepeatedly(DoAll(SetArgPointee<1>(1920 /* arbitrary */), Return(0)));
Alec Mouri4efb6c22018-11-16 13:07:47 -08001159 EXPECT_CALL(*mNativeWindow, perform(9)).Times(1);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001160 EXPECT_CALL(*mNativeWindow, perform(13)).Times(1);
Alec Mouri4efb6c22018-11-16 13:07:47 -08001161 EXPECT_CALL(*mNativeWindow, perform(30)).Times(1);
Valerie Hau9758ae02018-10-09 16:05:09 -07001162 auto displayDevice = mInjector.inject();
1163
Lloyd Pique3d0c02e2018-10-19 18:38:12 -07001164 displayDevice->getCompositionDisplay()
1165 ->getDisplayColorProfile()
1166 ->getBestColorMode(mInputDataspace, mInputRenderIntent, &mOutDataspace,
1167 &mOutColorMode, &mOutRenderIntent);
Valerie Hau9758ae02018-10-09 16:05:09 -07001168 }
1169
1170 ui::Dataspace mOutDataspace;
1171 ui::ColorMode mOutColorMode;
1172 ui::RenderIntent mOutRenderIntent;
1173
1174private:
1175 ui::Dataspace mInputDataspace;
1176 ui::RenderIntent mInputRenderIntent;
1177 bool mHasWideColorGamut = false;
1178 std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> mHwcColorModes;
1179 FakeDisplayDeviceInjector mInjector;
1180};
1181
1182TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeSRGB) {
1183 addHwcColorModesMapping(ui::ColorMode::SRGB,
1184 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1185 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1186 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1187 setHasWideColorGamut(true);
1188
1189 getBestColorMode();
1190
Peiyong Lin14724e62018-12-05 07:27:30 -08001191 ASSERT_EQ(ui::Dataspace::V0_SRGB, mOutDataspace);
Valerie Hau9758ae02018-10-09 16:05:09 -07001192 ASSERT_EQ(ui::ColorMode::SRGB, mOutColorMode);
1193 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1194}
1195
1196TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDisplayP3) {
1197 addHwcColorModesMapping(ui::ColorMode::DISPLAY_P3,
1198 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1199 addHwcColorModesMapping(ui::ColorMode::SRGB,
1200 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1201 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1202 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1203 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1204 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1205 setHasWideColorGamut(true);
1206
1207 getBestColorMode();
1208
1209 ASSERT_EQ(ui::Dataspace::DISPLAY_P3, mOutDataspace);
1210 ASSERT_EQ(ui::ColorMode::DISPLAY_P3, mOutColorMode);
1211 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1212}
1213
1214TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDISPLAY_BT2020) {
1215 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1216 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1217 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1218 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1219 setHasWideColorGamut(true);
1220
1221 getBestColorMode();
1222
1223 ASSERT_EQ(ui::Dataspace::DISPLAY_BT2020, mOutDataspace);
1224 ASSERT_EQ(ui::ColorMode::DISPLAY_BT2020, mOutColorMode);
1225 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1226}
1227
1228/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001229 * SurfaceFlinger::setupNewDisplayDeviceInternal
1230 */
1231
1232class SetupNewDisplayDeviceInternalTest : public DisplayTransactionTest {
1233public:
1234 template <typename T>
1235 void setupNewDisplayDeviceInternalTest();
1236};
1237
1238template <typename Case>
1239void SetupNewDisplayDeviceInternalTest::setupNewDisplayDeviceInternalTest() {
1240 const sp<BBinder> displayToken = new BBinder();
Lloyd Pique542307f2018-10-19 13:24:08 -07001241 const sp<compositionengine::mock::DisplaySurface> displaySurface =
1242 new compositionengine::mock::DisplaySurface();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001243 const sp<mock::GraphicBufferProducer> producer = new mock::GraphicBufferProducer();
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001244
1245 // --------------------------------------------------------------------
1246 // Preconditions
1247
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001248 // Wide color displays support is configured appropriately
1249 Case::WideColorSupport::injectConfigChange(this);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001250
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001251 // The display is setup with the HWC.
1252 Case::Display::injectHwcDisplay(this);
1253
1254 // SurfaceFlinger will use a test-controlled factory for native window
1255 // surfaces.
1256 injectFakeNativeWindowSurfaceFactory();
1257
1258 // --------------------------------------------------------------------
1259 // Call Expectations
1260
1261 // Various native window calls will be made.
1262 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
Lloyd Pique3c085a02018-05-09 19:38:32 -07001263 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001264 Case::WideColorSupport::setupComposerCallExpectations(this);
1265 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001266 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001267
1268 // --------------------------------------------------------------------
1269 // Invocation
1270
1271 DisplayDeviceState state;
Dominik Laskowski075d3172018-05-24 15:50:06 -07001272 state.displayId = static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1273 : Case::Display::DISPLAY_ID::get();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001274 state.isSecure = static_cast<bool>(Case::Display::SECURE);
1275
Dominik Laskowski075d3172018-05-24 15:50:06 -07001276 auto device =
1277 mFlinger.setupNewDisplayDeviceInternal(displayToken, Case::Display::DISPLAY_ID::get(),
1278 state, displaySurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001279
1280 // --------------------------------------------------------------------
1281 // Postconditions
1282
1283 ASSERT_TRUE(device != nullptr);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001284 EXPECT_EQ(Case::Display::DISPLAY_ID::get(), device->getId());
1285 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), device->isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001286 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001287 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001288 EXPECT_EQ(Case::Display::WIDTH, device->getWidth());
1289 EXPECT_EQ(Case::Display::HEIGHT, device->getHeight());
1290 EXPECT_EQ(Case::WideColorSupport::WIDE_COLOR_SUPPORTED, device->hasWideColorGamut());
Valerie Haue9e843a2018-12-18 13:39:23 -08001291 EXPECT_EQ(Case::HdrSupport::HDR10_PLUS_SUPPORTED, device->hasHDR10PlusSupport());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001292 EXPECT_EQ(Case::HdrSupport::HDR10_SUPPORTED, device->hasHDR10Support());
1293 EXPECT_EQ(Case::HdrSupport::HDR_HLG_SUPPORTED, device->hasHLGSupport());
1294 EXPECT_EQ(Case::HdrSupport::HDR_DOLBY_VISION_SUPPORTED, device->hasDolbyVisionSupport());
Lloyd Pique3c085a02018-05-09 19:38:32 -07001295 // Note: This is not Case::Display::HWC_ACTIVE_CONFIG_ID as the ids are
1296 // remapped, and the test only ever sets up one config. If there were an error
1297 // looking up the remapped index, device->getActiveConfig() would be -1 instead.
1298 EXPECT_EQ(0, device->getActiveConfig());
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001299 EXPECT_EQ(Case::PerFrameMetadataSupport::PER_FRAME_METADATA_KEYS,
1300 device->getSupportedPerFrameMetadata());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001301}
1302
1303TEST_F(SetupNewDisplayDeviceInternalTest, createSimplePrimaryDisplay) {
1304 setupNewDisplayDeviceInternalTest<SimplePrimaryDisplayCase>();
1305}
1306
1307TEST_F(SetupNewDisplayDeviceInternalTest, createSimpleExternalDisplay) {
1308 setupNewDisplayDeviceInternalTest<SimpleExternalDisplayCase>();
1309}
1310
1311TEST_F(SetupNewDisplayDeviceInternalTest, createNonHwcVirtualDisplay) {
1312 setupNewDisplayDeviceInternalTest<NonHwcVirtualDisplayCase>();
1313}
1314
1315TEST_F(SetupNewDisplayDeviceInternalTest, createHwcVirtualDisplay) {
Dominik Laskowski075d3172018-05-24 15:50:06 -07001316 using Case = HwcVirtualDisplayCase;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001317
Dominik Laskowski075d3172018-05-24 15:50:06 -07001318 // Insert display data so that the HWC thinks it created the virtual display.
1319 const auto displayId = Case::Display::DISPLAY_ID::get();
1320 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08001321 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001322
1323 setupNewDisplayDeviceInternalTest<Case>();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001324}
1325
1326TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3Display) {
1327 setupNewDisplayDeviceInternalTest<WideColorP3ColorimetricDisplayCase>();
1328}
1329
Valerie Haue9e843a2018-12-18 13:39:23 -08001330TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10PlusDisplay) {
1331 setupNewDisplayDeviceInternalTest<Hdr10PlusDisplayCase>();
1332}
1333
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001334TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10Display) {
1335 setupNewDisplayDeviceInternalTest<Hdr10DisplayCase>();
1336}
1337
1338TEST_F(SetupNewDisplayDeviceInternalTest, createHdrHlgDisplay) {
1339 setupNewDisplayDeviceInternalTest<HdrHlgDisplayCase>();
1340}
1341
1342TEST_F(SetupNewDisplayDeviceInternalTest, createHdrDolbyVisionDisplay) {
1343 setupNewDisplayDeviceInternalTest<HdrDolbyVisionDisplayCase>();
1344}
1345
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001346TEST_F(SetupNewDisplayDeviceInternalTest, createHdrSmpte2086DisplayCase) {
1347 setupNewDisplayDeviceInternalTest<HdrSmpte2086DisplayCase>();
1348}
1349
1350TEST_F(SetupNewDisplayDeviceInternalTest, createHdrCta816_3_DisplayCase) {
1351 setupNewDisplayDeviceInternalTest<HdrCta861_3_DisplayCase>();
1352}
1353
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001354/* ------------------------------------------------------------------------
1355 * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded)
1356 */
1357
1358class HandleTransactionLockedTest : public DisplayTransactionTest {
1359public:
1360 template <typename Case>
1361 void setupCommonPreconditions();
1362
1363 template <typename Case>
1364 void setupCommonCallExpectationsForConnectProcessing();
1365
1366 template <typename Case>
1367 void setupCommonCallExpectationsForDisconnectProcessing();
1368
1369 template <typename Case>
1370 void processesHotplugConnectCommon();
1371
1372 template <typename Case>
1373 void ignoresHotplugConnectCommon();
1374
1375 template <typename Case>
1376 void processesHotplugDisconnectCommon();
1377
1378 template <typename Case>
1379 void verifyDisplayIsConnected(const sp<IBinder>& displayToken);
1380
1381 template <typename Case>
1382 void verifyPhysicalDisplayIsConnected();
1383
1384 void verifyDisplayIsNotConnected(const sp<IBinder>& displayToken);
1385};
1386
1387template <typename Case>
1388void HandleTransactionLockedTest::setupCommonPreconditions() {
1389 // Wide color displays support is configured appropriately
1390 Case::WideColorSupport::injectConfigChange(this);
1391
1392 // SurfaceFlinger will use a test-controlled factory for BufferQueues
1393 injectFakeBufferQueueFactory();
1394
1395 // SurfaceFlinger will use a test-controlled factory for native window
1396 // surfaces.
1397 injectFakeNativeWindowSurfaceFactory();
1398}
1399
1400template <typename Case>
1401void HandleTransactionLockedTest::setupCommonCallExpectationsForConnectProcessing() {
1402 Case::Display::setupHwcHotplugCallExpectations(this);
1403
1404 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1405 Case::Display::setupFramebufferProducerBufferQueueCallExpectations(this);
1406 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1407 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
1408
1409 Case::WideColorSupport::setupComposerCallExpectations(this);
1410 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001411 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001412
1413 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
Dominik Laskowski1eba0202019-01-24 09:14:40 -08001414
1415 EXPECT_CALL(*mEventThread, onHotplugReceived(Case::Display::displayType(), true)).Times(1);
1416 EXPECT_CALL(*mSFEventThread, onHotplugReceived(Case::Display::displayType(), true)).Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001417}
1418
1419template <typename Case>
1420void HandleTransactionLockedTest::setupCommonCallExpectationsForDisconnectProcessing() {
1421 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
Dominik Laskowski1eba0202019-01-24 09:14:40 -08001422
1423 EXPECT_CALL(*mEventThread, onHotplugReceived(Case::Display::displayType(), false)).Times(1);
1424 EXPECT_CALL(*mSFEventThread, onHotplugReceived(Case::Display::displayType(), false)).Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001425}
1426
1427template <typename Case>
1428void HandleTransactionLockedTest::verifyDisplayIsConnected(const sp<IBinder>& displayToken) {
1429 // The display device should have been set up in the list of displays.
1430 ASSERT_TRUE(hasDisplayDevice(displayToken));
1431 const auto& device = getDisplayDevice(displayToken);
1432 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001433 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001434
1435 // The display should have been set up in the current display state
1436 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1437 const auto& current = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001438 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), current.isVirtual());
1439 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1440 : Case::Display::DISPLAY_ID::get(),
1441 current.displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001442
1443 // The display should have been set up in the drawing display state
1444 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1445 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001446 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
1447 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1448 : Case::Display::DISPLAY_ID::get(),
1449 draw.displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001450}
1451
1452template <typename Case>
1453void HandleTransactionLockedTest::verifyPhysicalDisplayIsConnected() {
1454 // HWComposer should have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001455 EXPECT_TRUE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001456
Dominik Laskowski075d3172018-05-24 15:50:06 -07001457 // SF should have a display token.
1458 const auto displayId = Case::Display::DISPLAY_ID::get();
1459 ASSERT_TRUE(displayId);
1460 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1461 auto& displayToken = mFlinger.mutablePhysicalDisplayTokens()[*displayId];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001462
1463 verifyDisplayIsConnected<Case>(displayToken);
1464}
1465
1466void HandleTransactionLockedTest::verifyDisplayIsNotConnected(const sp<IBinder>& displayToken) {
1467 EXPECT_FALSE(hasDisplayDevice(displayToken));
1468 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1469 EXPECT_FALSE(hasDrawingDisplayState(displayToken));
1470}
1471
1472template <typename Case>
1473void HandleTransactionLockedTest::processesHotplugConnectCommon() {
1474 // --------------------------------------------------------------------
1475 // Preconditions
1476
1477 setupCommonPreconditions<Case>();
1478
1479 // A hotplug connect event is enqueued for a display
1480 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001481
1482 // --------------------------------------------------------------------
1483 // Call Expectations
1484
1485 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillOnce(Return(false));
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001486
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001487 setupCommonCallExpectationsForConnectProcessing<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001488
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001489 // --------------------------------------------------------------------
1490 // Invocation
Lloyd Piquee39cad22017-12-20 17:01:29 -08001491
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001492 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
Lloyd Piquee39cad22017-12-20 17:01:29 -08001493
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001494 // --------------------------------------------------------------------
1495 // Postconditions
1496
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001497 verifyPhysicalDisplayIsConnected<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001498
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001499 // --------------------------------------------------------------------
1500 // Cleanup conditions
1501
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001502 EXPECT_CALL(*mComposer,
1503 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001504 .WillOnce(Return(Error::NONE));
1505 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Piquef58625d2017-12-19 13:22:33 -08001506}
1507
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001508template <typename Case>
1509void HandleTransactionLockedTest::ignoresHotplugConnectCommon() {
1510 // --------------------------------------------------------------------
1511 // Preconditions
1512
1513 setupCommonPreconditions<Case>();
1514
1515 // A hotplug connect event is enqueued for a display
1516 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1517
1518 // --------------------------------------------------------------------
1519 // Invocation
1520
1521 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1522
1523 // --------------------------------------------------------------------
1524 // Postconditions
1525
1526 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001527 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001528}
1529
1530template <typename Case>
1531void HandleTransactionLockedTest::processesHotplugDisconnectCommon() {
1532 // --------------------------------------------------------------------
1533 // Preconditions
1534
1535 setupCommonPreconditions<Case>();
1536
1537 // A hotplug disconnect event is enqueued for a display
1538 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1539
1540 // The display is already completely set up.
1541 Case::Display::injectHwcDisplay(this);
1542 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1543 existing.inject();
1544
1545 // --------------------------------------------------------------------
1546 // Call Expectations
1547
1548 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
Lloyd Pique438e9e72018-09-04 18:06:08 -07001549 EXPECT_CALL(*mComposer, getDisplayIdentificationData(Case::Display::HWC_DISPLAY_ID, _, _))
1550 .Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001551
1552 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1553
1554 // --------------------------------------------------------------------
1555 // Invocation
1556
1557 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1558
1559 // --------------------------------------------------------------------
1560 // Postconditions
1561
1562 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001563 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001564
Dominik Laskowski075d3172018-05-24 15:50:06 -07001565 // SF should not have a display token.
1566 const auto displayId = Case::Display::DISPLAY_ID::get();
1567 ASSERT_TRUE(displayId);
1568 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001569
1570 // The existing token should have been removed
1571 verifyDisplayIsNotConnected(existing.token());
1572}
1573
1574TEST_F(HandleTransactionLockedTest, processesHotplugConnectPrimaryDisplay) {
1575 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1576}
1577
1578TEST_F(HandleTransactionLockedTest,
1579 processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected) {
1580 // Inject an external display.
1581 ExternalDisplayVariant::injectHwcDisplay(this);
1582
1583 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1584}
1585
1586TEST_F(HandleTransactionLockedTest, processesHotplugConnectExternalDisplay) {
1587 // Inject a primary display.
1588 PrimaryDisplayVariant::injectHwcDisplay(this);
1589
1590 processesHotplugConnectCommon<SimpleExternalDisplayCase>();
1591}
1592
1593TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected) {
1594 // Inject both a primary and external display.
1595 PrimaryDisplayVariant::injectHwcDisplay(this);
1596 ExternalDisplayVariant::injectHwcDisplay(this);
1597
1598 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1599
1600 ignoresHotplugConnectCommon<SimpleTertiaryDisplayCase>();
1601}
1602
1603TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfExternalForVrComposer) {
1604 // Inject a primary display.
1605 PrimaryDisplayVariant::injectHwcDisplay(this);
1606
1607 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(true));
1608
1609 ignoresHotplugConnectCommon<SimpleExternalDisplayCase>();
1610}
1611
1612TEST_F(HandleTransactionLockedTest, processHotplugDisconnectPrimaryDisplay) {
1613 processesHotplugDisconnectCommon<SimplePrimaryDisplayCase>();
1614}
1615
1616TEST_F(HandleTransactionLockedTest, processHotplugDisconnectExternalDisplay) {
1617 processesHotplugDisconnectCommon<SimpleExternalDisplayCase>();
1618}
1619
1620TEST_F(HandleTransactionLockedTest, processesHotplugConnectThenDisconnectPrimary) {
1621 using Case = SimplePrimaryDisplayCase;
1622
1623 // --------------------------------------------------------------------
1624 // Preconditions
1625
1626 setupCommonPreconditions<Case>();
1627
1628 // A hotplug connect event is enqueued for a display
1629 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1630 // A hotplug disconnect event is also enqueued for the same display
1631 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1632
1633 // --------------------------------------------------------------------
1634 // Call Expectations
1635
1636 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1637
1638 setupCommonCallExpectationsForConnectProcessing<Case>();
1639 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1640
1641 EXPECT_CALL(*mComposer,
1642 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1643 .WillOnce(Return(Error::NONE));
1644 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1645
1646 // --------------------------------------------------------------------
1647 // Invocation
1648
1649 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1650
1651 // --------------------------------------------------------------------
1652 // Postconditions
1653
1654 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001655 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001656
Dominik Laskowski075d3172018-05-24 15:50:06 -07001657 // SF should not have a display token.
1658 const auto displayId = Case::Display::DISPLAY_ID::get();
1659 ASSERT_TRUE(displayId);
1660 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001661}
1662
1663TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectThenConnectPrimary) {
1664 using Case = SimplePrimaryDisplayCase;
1665
1666 // --------------------------------------------------------------------
1667 // Preconditions
1668
1669 setupCommonPreconditions<Case>();
1670
1671 // The display is already completely set up.
1672 Case::Display::injectHwcDisplay(this);
1673 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1674 existing.inject();
1675
1676 // A hotplug disconnect event is enqueued for a display
1677 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1678 // A hotplug connect event is also enqueued for the same display
1679 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1680
1681 // --------------------------------------------------------------------
1682 // Call Expectations
1683
1684 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1685
1686 setupCommonCallExpectationsForConnectProcessing<Case>();
1687 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1688
1689 // --------------------------------------------------------------------
1690 // Invocation
1691
1692 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1693
1694 // --------------------------------------------------------------------
1695 // Postconditions
1696
1697 // The existing token should have been removed
1698 verifyDisplayIsNotConnected(existing.token());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001699 const auto displayId = Case::Display::DISPLAY_ID::get();
1700 ASSERT_TRUE(displayId);
1701 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1702 EXPECT_NE(existing.token(), mFlinger.mutablePhysicalDisplayTokens()[*displayId]);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001703
1704 // A new display should be connected in its place
1705
1706 verifyPhysicalDisplayIsConnected<Case>();
1707
1708 // --------------------------------------------------------------------
1709 // Cleanup conditions
1710
1711 EXPECT_CALL(*mComposer,
1712 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1713 .WillOnce(Return(Error::NONE));
1714 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1715}
1716
1717TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAdded) {
1718 using Case = HwcVirtualDisplayCase;
1719
1720 // --------------------------------------------------------------------
1721 // Preconditions
1722
1723 // The HWC supports at least one virtual display
1724 injectMockComposer(1);
1725
1726 setupCommonPreconditions<Case>();
1727
1728 // A virtual display was added to the current state, and it has a
1729 // surface(producer)
1730 sp<BBinder> displayToken = new BBinder();
Lloyd Pique4c2ac022018-04-27 12:08:03 -07001731
Dominik Laskowski075d3172018-05-24 15:50:06 -07001732 DisplayDeviceState state;
1733 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001734
1735 sp<mock::GraphicBufferProducer> surface{new mock::GraphicBufferProducer()};
Dominik Laskowski075d3172018-05-24 15:50:06 -07001736 state.surface = surface;
1737 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001738
1739 // --------------------------------------------------------------------
1740 // Call Expectations
1741
1742 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1743 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1744
1745 EXPECT_CALL(*surface, query(NATIVE_WINDOW_WIDTH, _))
1746 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::WIDTH), Return(NO_ERROR)));
1747 EXPECT_CALL(*surface, query(NATIVE_WINDOW_HEIGHT, _))
1748 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::HEIGHT), Return(NO_ERROR)));
1749 EXPECT_CALL(*surface, query(NATIVE_WINDOW_FORMAT, _))
1750 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT),
1751 Return(NO_ERROR)));
1752 EXPECT_CALL(*surface, query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, _))
1753 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(NO_ERROR)));
1754
1755 EXPECT_CALL(*surface, setAsyncMode(true)).Times(1);
1756
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001757 EXPECT_CALL(*mProducer, disconnect(_, _)).Times(1);
1758
1759 Case::Display::setupHwcVirtualDisplayCreationCallExpectations(this);
1760 Case::WideColorSupport::setupComposerCallExpectations(this);
1761 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001762 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001763
1764 // --------------------------------------------------------------------
1765 // Invocation
1766
1767 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1768
1769 // --------------------------------------------------------------------
1770 // Postconditions
1771
1772 // The display device should have been set up in the list of displays.
1773 verifyDisplayIsConnected<Case>(displayToken);
1774
1775 // --------------------------------------------------------------------
1776 // Cleanup conditions
1777
1778 EXPECT_CALL(*mComposer, destroyVirtualDisplay(Case::Display::HWC_DISPLAY_ID))
1779 .WillOnce(Return(Error::NONE));
1780 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1781}
1782
1783TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAddedWithNoSurface) {
1784 using Case = HwcVirtualDisplayCase;
1785
1786 // --------------------------------------------------------------------
1787 // Preconditions
1788
1789 // The HWC supports at least one virtual display
1790 injectMockComposer(1);
1791
1792 setupCommonPreconditions<Case>();
1793
1794 // A virtual display was added to the current state, but it does not have a
1795 // surface.
1796 sp<BBinder> displayToken = new BBinder();
1797
Dominik Laskowski075d3172018-05-24 15:50:06 -07001798 DisplayDeviceState state;
1799 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001800
Dominik Laskowski075d3172018-05-24 15:50:06 -07001801 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001802
1803 // --------------------------------------------------------------------
1804 // Call Expectations
1805
1806 // --------------------------------------------------------------------
1807 // Invocation
1808
1809 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1810
1811 // --------------------------------------------------------------------
1812 // Postconditions
1813
1814 // There will not be a display device set up.
1815 EXPECT_FALSE(hasDisplayDevice(displayToken));
1816
1817 // The drawing display state will be set from the current display state.
1818 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1819 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001820 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001821}
1822
1823TEST_F(HandleTransactionLockedTest, processesVirtualDisplayRemoval) {
1824 using Case = HwcVirtualDisplayCase;
1825
1826 // --------------------------------------------------------------------
1827 // Preconditions
1828
1829 // A virtual display is set up but is removed from the current state.
Dominik Laskowski075d3172018-05-24 15:50:06 -07001830 const auto displayId = Case::Display::DISPLAY_ID::get();
1831 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08001832 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001833 Case::Display::injectHwcDisplay(this);
1834 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1835 existing.inject();
1836 mFlinger.mutableCurrentState().displays.removeItem(existing.token());
1837
1838 // --------------------------------------------------------------------
1839 // Call Expectations
1840
1841 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1842
1843 // --------------------------------------------------------------------
1844 // Invocation
1845
1846 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1847
1848 // --------------------------------------------------------------------
1849 // Postconditions
1850
1851 // The existing token should have been removed
1852 verifyDisplayIsNotConnected(existing.token());
1853}
1854
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001855TEST_F(HandleTransactionLockedTest, processesDisplayLayerStackChanges) {
1856 using Case = NonHwcVirtualDisplayCase;
1857
1858 constexpr uint32_t oldLayerStack = 0u;
1859 constexpr uint32_t newLayerStack = 123u;
1860
1861 // --------------------------------------------------------------------
1862 // Preconditions
1863
1864 // A display is set up
1865 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1866 display.inject();
1867
1868 // There is a change to the layerStack state
1869 display.mutableDrawingDisplayState().layerStack = oldLayerStack;
1870 display.mutableCurrentDisplayState().layerStack = newLayerStack;
1871
1872 // --------------------------------------------------------------------
1873 // Invocation
1874
1875 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1876
1877 // --------------------------------------------------------------------
1878 // Postconditions
1879
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001880 EXPECT_EQ(newLayerStack, display.mutableDisplayDevice()->getLayerStack());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001881}
1882
1883TEST_F(HandleTransactionLockedTest, processesDisplayTransformChanges) {
1884 using Case = NonHwcVirtualDisplayCase;
1885
1886 constexpr int oldTransform = 0;
1887 constexpr int newTransform = 2;
1888
1889 // --------------------------------------------------------------------
1890 // Preconditions
1891
1892 // A display is set up
1893 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1894 display.inject();
1895
1896 // There is a change to the orientation state
1897 display.mutableDrawingDisplayState().orientation = oldTransform;
1898 display.mutableCurrentDisplayState().orientation = newTransform;
1899
1900 // --------------------------------------------------------------------
1901 // Invocation
1902
1903 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1904
1905 // --------------------------------------------------------------------
1906 // Postconditions
1907
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001908 EXPECT_EQ(newTransform, display.mutableDisplayDevice()->getOrientation());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001909}
1910
1911TEST_F(HandleTransactionLockedTest, processesDisplayViewportChanges) {
1912 using Case = NonHwcVirtualDisplayCase;
1913
1914 const Rect oldViewport(0, 0, 0, 0);
1915 const Rect newViewport(0, 0, 123, 456);
1916
1917 // --------------------------------------------------------------------
1918 // Preconditions
1919
1920 // A display is set up
1921 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1922 display.inject();
1923
1924 // There is a change to the viewport state
1925 display.mutableDrawingDisplayState().viewport = oldViewport;
1926 display.mutableCurrentDisplayState().viewport = newViewport;
1927
1928 // --------------------------------------------------------------------
1929 // Invocation
1930
1931 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1932
1933 // --------------------------------------------------------------------
1934 // Postconditions
1935
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001936 EXPECT_EQ(newViewport, display.mutableDisplayDevice()->getViewport());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001937}
1938
1939TEST_F(HandleTransactionLockedTest, processesDisplayFrameChanges) {
1940 using Case = NonHwcVirtualDisplayCase;
1941
1942 const Rect oldFrame(0, 0, 0, 0);
1943 const Rect newFrame(0, 0, 123, 456);
1944
1945 // --------------------------------------------------------------------
1946 // Preconditions
1947
1948 // A display is set up
1949 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1950 display.inject();
1951
1952 // There is a change to the viewport state
1953 display.mutableDrawingDisplayState().frame = oldFrame;
1954 display.mutableCurrentDisplayState().frame = newFrame;
1955
1956 // --------------------------------------------------------------------
1957 // Invocation
1958
1959 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1960
1961 // --------------------------------------------------------------------
1962 // Postconditions
1963
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001964 EXPECT_EQ(newFrame, display.mutableDisplayDevice()->getFrame());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001965}
1966
1967TEST_F(HandleTransactionLockedTest, processesDisplayWidthChanges) {
1968 using Case = NonHwcVirtualDisplayCase;
1969
1970 constexpr int oldWidth = 0;
1971 constexpr int oldHeight = 10;
1972 constexpr int newWidth = 123;
1973
1974 // --------------------------------------------------------------------
1975 // Preconditions
1976
1977 // A display is set up
1978 auto nativeWindow = new mock::NativeWindow();
Lloyd Pique542307f2018-10-19 13:24:08 -07001979 auto displaySurface = new compositionengine::mock::DisplaySurface();
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001980 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001981 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1982 display.setNativeWindow(nativeWindow);
1983 display.setDisplaySurface(displaySurface);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001984 // Setup injection expections
1985 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
1986 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
1987 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
1988 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
Alec Mouri4efb6c22018-11-16 13:07:47 -08001989 EXPECT_CALL(*nativeWindow, perform(9)).Times(1);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001990 EXPECT_CALL(*nativeWindow, perform(13)).Times(1);
Alec Mouri4efb6c22018-11-16 13:07:47 -08001991 EXPECT_CALL(*nativeWindow, perform(30)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001992 display.inject();
1993
1994 // There is a change to the viewport state
1995 display.mutableDrawingDisplayState().width = oldWidth;
1996 display.mutableDrawingDisplayState().height = oldHeight;
1997 display.mutableCurrentDisplayState().width = newWidth;
1998 display.mutableCurrentDisplayState().height = oldHeight;
1999
2000 // --------------------------------------------------------------------
2001 // Call Expectations
2002
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002003 EXPECT_CALL(*displaySurface, resizeBuffers(newWidth, oldHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002004
2005 // --------------------------------------------------------------------
2006 // Invocation
2007
2008 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2009}
2010
2011TEST_F(HandleTransactionLockedTest, processesDisplayHeightChanges) {
2012 using Case = NonHwcVirtualDisplayCase;
2013
2014 constexpr int oldWidth = 0;
2015 constexpr int oldHeight = 10;
2016 constexpr int newHeight = 123;
2017
2018 // --------------------------------------------------------------------
2019 // Preconditions
2020
2021 // A display is set up
2022 auto nativeWindow = new mock::NativeWindow();
Lloyd Pique542307f2018-10-19 13:24:08 -07002023 auto displaySurface = new compositionengine::mock::DisplaySurface();
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002024 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002025 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2026 display.setNativeWindow(nativeWindow);
2027 display.setDisplaySurface(displaySurface);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002028 // Setup injection expections
2029 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
2030 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
2031 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
2032 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
Alec Mouri4efb6c22018-11-16 13:07:47 -08002033 EXPECT_CALL(*nativeWindow, perform(9)).Times(1);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002034 EXPECT_CALL(*nativeWindow, perform(13)).Times(1);
Alec Mouri4efb6c22018-11-16 13:07:47 -08002035 EXPECT_CALL(*nativeWindow, perform(30)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002036 display.inject();
2037
2038 // There is a change to the viewport state
2039 display.mutableDrawingDisplayState().width = oldWidth;
2040 display.mutableDrawingDisplayState().height = oldHeight;
2041 display.mutableCurrentDisplayState().width = oldWidth;
2042 display.mutableCurrentDisplayState().height = newHeight;
2043
2044 // --------------------------------------------------------------------
2045 // Call Expectations
2046
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002047 EXPECT_CALL(*displaySurface, resizeBuffers(oldWidth, newHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002048
2049 // --------------------------------------------------------------------
2050 // Invocation
2051
2052 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2053}
2054
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002055/* ------------------------------------------------------------------------
2056 * SurfaceFlinger::setDisplayStateLocked
2057 */
2058
2059TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithUnknownDisplay) {
2060 // --------------------------------------------------------------------
2061 // Preconditions
2062
2063 // We have an unknown display token not associated with a known display
2064 sp<BBinder> displayToken = new BBinder();
2065
2066 // The requested display state references the unknown display.
2067 DisplayState state;
2068 state.what = DisplayState::eLayerStackChanged;
2069 state.token = displayToken;
2070 state.layerStack = 456;
2071
2072 // --------------------------------------------------------------------
2073 // Invocation
2074
2075 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2076
2077 // --------------------------------------------------------------------
2078 // Postconditions
2079
2080 // The returned flags are empty
2081 EXPECT_EQ(0u, flags);
2082
2083 // The display token still doesn't match anything known.
2084 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
2085}
2086
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002087TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWhenNoChanges) {
2088 using Case = SimplePrimaryDisplayCase;
2089
2090 // --------------------------------------------------------------------
2091 // Preconditions
2092
2093 // A display is already set up
2094 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2095 display.inject();
2096
2097 // No changes are made to the display
2098 DisplayState state;
2099 state.what = 0;
2100 state.token = display.token();
2101
2102 // --------------------------------------------------------------------
2103 // Invocation
2104
2105 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2106
2107 // --------------------------------------------------------------------
2108 // Postconditions
2109
2110 // The returned flags are empty
2111 EXPECT_EQ(0u, flags);
2112}
2113
2114TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSurfaceDidNotChange) {
2115 using Case = SimplePrimaryDisplayCase;
2116
2117 // --------------------------------------------------------------------
2118 // Preconditions
2119
2120 // A display is already set up
2121 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2122 display.inject();
2123
2124 // There is a surface that can be set.
2125 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2126
2127 // The current display state has the surface set
2128 display.mutableCurrentDisplayState().surface = surface;
2129
2130 // The incoming request sets the same surface
2131 DisplayState state;
2132 state.what = DisplayState::eSurfaceChanged;
2133 state.token = display.token();
2134 state.surface = surface;
2135
2136 // --------------------------------------------------------------------
2137 // Invocation
2138
2139 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2140
2141 // --------------------------------------------------------------------
2142 // Postconditions
2143
2144 // The returned flags are empty
2145 EXPECT_EQ(0u, flags);
2146
2147 // The current display state is unchanged.
2148 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2149}
2150
2151TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfSurfaceChanged) {
2152 using Case = SimplePrimaryDisplayCase;
2153
2154 // --------------------------------------------------------------------
2155 // Preconditions
2156
2157 // A display is already set up
2158 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2159 display.inject();
2160
2161 // There is a surface that can be set.
2162 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2163
2164 // The current display state does not have a surface
2165 display.mutableCurrentDisplayState().surface = nullptr;
2166
2167 // The incoming request sets a surface
2168 DisplayState state;
2169 state.what = DisplayState::eSurfaceChanged;
2170 state.token = display.token();
2171 state.surface = surface;
2172
2173 // --------------------------------------------------------------------
2174 // Invocation
2175
2176 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2177
2178 // --------------------------------------------------------------------
2179 // Postconditions
2180
2181 // The returned flags indicate a transaction is needed
2182 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2183
2184 // The current display layer stack state is set to the new value
2185 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2186}
2187
2188TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfLayerStackDidNotChange) {
2189 using Case = SimplePrimaryDisplayCase;
2190
2191 // --------------------------------------------------------------------
2192 // Preconditions
2193
2194 // A display is already set up
2195 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2196 display.inject();
2197
2198 // The display has a layer stack set
2199 display.mutableCurrentDisplayState().layerStack = 456u;
2200
2201 // The incoming request sets the same layer stack
2202 DisplayState state;
2203 state.what = DisplayState::eLayerStackChanged;
2204 state.token = display.token();
2205 state.layerStack = 456u;
2206
2207 // --------------------------------------------------------------------
2208 // Invocation
2209
2210 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2211
2212 // --------------------------------------------------------------------
2213 // Postconditions
2214
2215 // The returned flags are empty
2216 EXPECT_EQ(0u, flags);
2217
2218 // The current display state is unchanged
2219 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2220}
2221
2222TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfLayerStackChanged) {
2223 using Case = SimplePrimaryDisplayCase;
2224
2225 // --------------------------------------------------------------------
2226 // Preconditions
2227
2228 // A display is set up
2229 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2230 display.inject();
2231
2232 // The display has a layer stack set
2233 display.mutableCurrentDisplayState().layerStack = 654u;
2234
2235 // The incoming request sets a different layer stack
2236 DisplayState state;
2237 state.what = DisplayState::eLayerStackChanged;
2238 state.token = display.token();
2239 state.layerStack = 456u;
2240
2241 // --------------------------------------------------------------------
2242 // Invocation
2243
2244 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2245
2246 // --------------------------------------------------------------------
2247 // Postconditions
2248
2249 // The returned flags indicate a transaction is needed
2250 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2251
2252 // The desired display state has been set to the new value.
2253 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2254}
2255
2256TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfProjectionDidNotChange) {
2257 using Case = SimplePrimaryDisplayCase;
2258 constexpr int initialOrientation = 180;
2259 const Rect initialFrame = {1, 2, 3, 4};
2260 const Rect initialViewport = {5, 6, 7, 8};
2261
2262 // --------------------------------------------------------------------
2263 // Preconditions
2264
2265 // A display is set up
2266 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2267 display.inject();
2268
2269 // The current display state projection state is all set
2270 display.mutableCurrentDisplayState().orientation = initialOrientation;
2271 display.mutableCurrentDisplayState().frame = initialFrame;
2272 display.mutableCurrentDisplayState().viewport = initialViewport;
2273
2274 // The incoming request sets the same projection state
2275 DisplayState state;
2276 state.what = DisplayState::eDisplayProjectionChanged;
2277 state.token = display.token();
2278 state.orientation = initialOrientation;
2279 state.frame = initialFrame;
2280 state.viewport = initialViewport;
2281
2282 // --------------------------------------------------------------------
2283 // Invocation
2284
2285 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2286
2287 // --------------------------------------------------------------------
2288 // Postconditions
2289
2290 // The returned flags are empty
2291 EXPECT_EQ(0u, flags);
2292
2293 // The current display state is unchanged
2294 EXPECT_EQ(initialOrientation, display.getCurrentDisplayState().orientation);
2295
2296 EXPECT_EQ(initialFrame, display.getCurrentDisplayState().frame);
2297 EXPECT_EQ(initialViewport, display.getCurrentDisplayState().viewport);
2298}
2299
2300TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfOrientationChanged) {
2301 using Case = SimplePrimaryDisplayCase;
2302 constexpr int initialOrientation = 90;
2303 constexpr int desiredOrientation = 180;
2304
2305 // --------------------------------------------------------------------
2306 // Preconditions
2307
2308 // A display is set up
2309 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2310 display.inject();
2311
2312 // The current display state has an orientation set
2313 display.mutableCurrentDisplayState().orientation = initialOrientation;
2314
2315 // The incoming request sets a different orientation
2316 DisplayState state;
2317 state.what = DisplayState::eDisplayProjectionChanged;
2318 state.token = display.token();
2319 state.orientation = desiredOrientation;
2320
2321 // --------------------------------------------------------------------
2322 // Invocation
2323
2324 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2325
2326 // --------------------------------------------------------------------
2327 // Postconditions
2328
2329 // The returned flags indicate a transaction is needed
2330 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2331
2332 // The current display state has the new value.
2333 EXPECT_EQ(desiredOrientation, display.getCurrentDisplayState().orientation);
2334}
2335
2336TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfFrameChanged) {
2337 using Case = SimplePrimaryDisplayCase;
2338 const Rect initialFrame = {0, 0, 0, 0};
2339 const Rect desiredFrame = {5, 6, 7, 8};
2340
2341 // --------------------------------------------------------------------
2342 // Preconditions
2343
2344 // A display is set up
2345 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2346 display.inject();
2347
2348 // The current display state does not have a frame
2349 display.mutableCurrentDisplayState().frame = initialFrame;
2350
2351 // The incoming request sets a frame
2352 DisplayState state;
2353 state.what = DisplayState::eDisplayProjectionChanged;
2354 state.token = display.token();
2355 state.frame = desiredFrame;
2356
2357 // --------------------------------------------------------------------
2358 // Invocation
2359
2360 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2361
2362 // --------------------------------------------------------------------
2363 // Postconditions
2364
2365 // The returned flags indicate a transaction is needed
2366 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2367
2368 // The current display state has the new value.
2369 EXPECT_EQ(desiredFrame, display.getCurrentDisplayState().frame);
2370}
2371
2372TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfViewportChanged) {
2373 using Case = SimplePrimaryDisplayCase;
2374 const Rect initialViewport = {0, 0, 0, 0};
2375 const Rect desiredViewport = {5, 6, 7, 8};
2376
2377 // --------------------------------------------------------------------
2378 // Preconditions
2379
2380 // A display is set up
2381 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2382 display.inject();
2383
2384 // The current display state does not have a viewport
2385 display.mutableCurrentDisplayState().viewport = initialViewport;
2386
2387 // The incoming request sets a viewport
2388 DisplayState state;
2389 state.what = DisplayState::eDisplayProjectionChanged;
2390 state.token = display.token();
2391 state.viewport = desiredViewport;
2392
2393 // --------------------------------------------------------------------
2394 // Invocation
2395
2396 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2397
2398 // --------------------------------------------------------------------
2399 // Postconditions
2400
2401 // The returned flags indicate a transaction is needed
2402 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2403
2404 // The current display state has the new value.
2405 EXPECT_EQ(desiredViewport, display.getCurrentDisplayState().viewport);
2406}
2407
2408TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSizeDidNotChange) {
2409 using Case = SimplePrimaryDisplayCase;
2410 constexpr uint32_t initialWidth = 1024;
2411 constexpr uint32_t initialHeight = 768;
2412
2413 // --------------------------------------------------------------------
2414 // Preconditions
2415
2416 // A display is set up
2417 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2418 display.inject();
2419
2420 // The current display state has a size set
2421 display.mutableCurrentDisplayState().width = initialWidth;
2422 display.mutableCurrentDisplayState().height = initialHeight;
2423
2424 // The incoming request sets the same display size
2425 DisplayState state;
2426 state.what = DisplayState::eDisplaySizeChanged;
2427 state.token = display.token();
2428 state.width = initialWidth;
2429 state.height = initialHeight;
2430
2431 // --------------------------------------------------------------------
2432 // Invocation
2433
2434 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2435
2436 // --------------------------------------------------------------------
2437 // Postconditions
2438
2439 // The returned flags are empty
2440 EXPECT_EQ(0u, flags);
2441
2442 // The current display state is unchanged
2443 EXPECT_EQ(initialWidth, display.getCurrentDisplayState().width);
2444 EXPECT_EQ(initialHeight, display.getCurrentDisplayState().height);
2445}
2446
2447TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfWidthChanged) {
2448 using Case = SimplePrimaryDisplayCase;
2449 constexpr uint32_t initialWidth = 0;
2450 constexpr uint32_t desiredWidth = 1024;
2451
2452 // --------------------------------------------------------------------
2453 // Preconditions
2454
2455 // A display is set up
2456 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2457 display.inject();
2458
2459 // The display does not yet have a width
2460 display.mutableCurrentDisplayState().width = initialWidth;
2461
2462 // The incoming request sets a display width
2463 DisplayState state;
2464 state.what = DisplayState::eDisplaySizeChanged;
2465 state.token = display.token();
2466 state.width = desiredWidth;
2467
2468 // --------------------------------------------------------------------
2469 // Invocation
2470
2471 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2472
2473 // --------------------------------------------------------------------
2474 // Postconditions
2475
2476 // The returned flags indicate a transaction is needed
2477 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2478
2479 // The current display state has the new value.
2480 EXPECT_EQ(desiredWidth, display.getCurrentDisplayState().width);
2481}
2482
2483TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfHeightChanged) {
2484 using Case = SimplePrimaryDisplayCase;
2485 constexpr uint32_t initialHeight = 0;
2486 constexpr uint32_t desiredHeight = 768;
2487
2488 // --------------------------------------------------------------------
2489 // Preconditions
2490
2491 // A display is set up
2492 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2493 display.inject();
2494
2495 // The display does not yet have a height
2496 display.mutableCurrentDisplayState().height = initialHeight;
2497
2498 // The incoming request sets a display height
2499 DisplayState state;
2500 state.what = DisplayState::eDisplaySizeChanged;
2501 state.token = display.token();
2502 state.height = desiredHeight;
2503
2504 // --------------------------------------------------------------------
2505 // Invocation
2506
2507 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2508
2509 // --------------------------------------------------------------------
2510 // Postconditions
2511
2512 // The returned flags indicate a transaction is needed
2513 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2514
2515 // The current display state has the new value.
2516 EXPECT_EQ(desiredHeight, display.getCurrentDisplayState().height);
2517}
2518
Lloyd Pique86016da2018-03-01 16:09:38 -08002519/* ------------------------------------------------------------------------
2520 * SurfaceFlinger::onInitializeDisplays
2521 */
2522
2523TEST_F(DisplayTransactionTest, onInitializeDisplaysSetsUpPrimaryDisplay) {
2524 using Case = SimplePrimaryDisplayCase;
2525
2526 // --------------------------------------------------------------------
2527 // Preconditions
2528
2529 // A primary display is set up
2530 Case::Display::injectHwcDisplay(this);
2531 auto primaryDisplay = Case::Display::makeFakeExistingDisplayInjector(this);
2532 primaryDisplay.inject();
2533
2534 // --------------------------------------------------------------------
2535 // Call Expectations
2536
2537 // We expect the surface interceptor to possibly be used, but we treat it as
2538 // disabled since it is called as a side effect rather than directly by this
2539 // function.
2540 EXPECT_CALL(*mSurfaceInterceptor, isEnabled()).WillOnce(Return(false));
2541
2542 // We expect a call to get the active display config.
2543 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
2544
2545 // We expect invalidate() to be invoked once to trigger display transaction
2546 // processing.
2547 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
2548
2549 // --------------------------------------------------------------------
2550 // Invocation
2551
2552 mFlinger.onInitializeDisplays();
2553
2554 // --------------------------------------------------------------------
2555 // Postconditions
2556
2557 // The primary display should have a current state
2558 ASSERT_TRUE(hasCurrentDisplayState(primaryDisplay.token()));
2559 const auto& primaryDisplayState = getCurrentDisplayState(primaryDisplay.token());
2560 // The layer stack state should be set to zero
2561 EXPECT_EQ(0u, primaryDisplayState.layerStack);
2562 // The orientation state should be set to zero
2563 EXPECT_EQ(0, primaryDisplayState.orientation);
2564
2565 // The frame state should be set to INVALID
2566 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.frame);
2567
2568 // The viewport state should be set to INVALID
2569 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.viewport);
2570
2571 // The width and height should both be zero
2572 EXPECT_EQ(0u, primaryDisplayState.width);
2573 EXPECT_EQ(0u, primaryDisplayState.height);
2574
2575 // The display should be set to HWC_POWER_MODE_NORMAL
2576 ASSERT_TRUE(hasDisplayDevice(primaryDisplay.token()));
2577 auto displayDevice = primaryDisplay.mutableDisplayDevice();
2578 EXPECT_EQ(HWC_POWER_MODE_NORMAL, displayDevice->getPowerMode());
2579
2580 // The display refresh period should be set in the frame tracker.
2581 FrameStats stats;
2582 mFlinger.getAnimFrameTracker().getStats(&stats);
2583 EXPECT_EQ(DEFAULT_REFRESH_RATE, stats.refreshPeriodNano);
2584
2585 // The display transaction needed flag should be set.
2586 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
2587
2588 // The compositor timing should be set to default values
2589 const auto& compositorTiming = mFlinger.getCompositorTiming();
2590 EXPECT_EQ(-DEFAULT_REFRESH_RATE, compositorTiming.deadline);
2591 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.interval);
2592 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.presentLatency);
2593}
2594
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002595/* ------------------------------------------------------------------------
2596 * SurfaceFlinger::setPowerModeInternal
2597 */
2598
2599// Used when we simulate a display that supports doze.
Peiyong Lined531a32018-10-26 18:27:56 -07002600template <typename Display>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002601struct DozeIsSupportedVariant {
2602 static constexpr bool DOZE_SUPPORTED = true;
2603 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2604 IComposerClient::PowerMode::DOZE;
2605 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2606 IComposerClient::PowerMode::DOZE_SUSPEND;
Peiyong Lined531a32018-10-26 18:27:56 -07002607
2608 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
2609 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(Display::HWC_DISPLAY_ID, _))
2610 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hwc2::DisplayCapability>(
2611 {Hwc2::DisplayCapability::DOZE})),
2612 Return(Error::NONE)));
2613 }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002614};
2615
Peiyong Lined531a32018-10-26 18:27:56 -07002616template <typename Display>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002617// Used when we simulate a display that does not support doze.
2618struct DozeNotSupportedVariant {
2619 static constexpr bool DOZE_SUPPORTED = false;
2620 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2621 IComposerClient::PowerMode::ON;
2622 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2623 IComposerClient::PowerMode::ON;
Peiyong Lined531a32018-10-26 18:27:56 -07002624
2625 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
2626 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(Display::HWC_DISPLAY_ID, _))
2627 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hwc2::DisplayCapability>({})),
2628 Return(Error::NONE)));
2629 }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002630};
2631
2632struct EventThreadBaseSupportedVariant {
2633 static void setupEventAndEventControlThreadNoCallExpectations(DisplayTransactionTest* test) {
2634 // The event control thread should not be notified.
2635 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(_)).Times(0);
2636
2637 // The event thread should not be notified.
2638 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(0);
2639 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(0);
2640 }
2641};
2642
2643struct EventThreadNotSupportedVariant : public EventThreadBaseSupportedVariant {
2644 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2645 // These calls are only expected for the primary display.
2646
2647 // Instead expect no calls.
2648 setupEventAndEventControlThreadNoCallExpectations(test);
2649 }
2650
2651 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2652 // These calls are only expected for the primary display.
2653
2654 // Instead expect no calls.
2655 setupEventAndEventControlThreadNoCallExpectations(test);
2656 }
2657};
2658
2659struct EventThreadIsSupportedVariant : public EventThreadBaseSupportedVariant {
2660 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2661 // The event control thread should be notified to enable vsyncs
2662 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(true)).Times(1);
2663
2664 // The event thread should be notified that the screen was acquired.
2665 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(1);
2666 }
2667
2668 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2669 // There should be a call to setVsyncEnabled(false)
2670 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(false)).Times(1);
2671
2672 // The event thread should not be notified that the screen was released.
2673 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(1);
2674 }
2675};
2676
Lloyd Pique41be5d22018-06-21 13:11:48 -07002677struct DispSyncIsSupportedVariant {
2678 static void setupBeginResyncCallExpectations(DisplayTransactionTest* test) {
2679 EXPECT_CALL(*test->mPrimaryDispSync, reset()).Times(1);
2680 EXPECT_CALL(*test->mPrimaryDispSync, setPeriod(DEFAULT_REFRESH_RATE)).Times(1);
2681 EXPECT_CALL(*test->mPrimaryDispSync, beginResync()).Times(1);
2682 }
2683
2684 static void setupEndResyncCallExpectations(DisplayTransactionTest* test) {
2685 EXPECT_CALL(*test->mPrimaryDispSync, endResync()).Times(1);
2686 }
2687};
2688
2689struct DispSyncNotSupportedVariant {
2690 static void setupBeginResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2691
2692 static void setupEndResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2693};
2694
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002695// --------------------------------------------------------------------
2696// Note:
2697//
2698// There are a large number of transitions we could test, however we only test a
2699// selected subset which provides complete test coverage of the implementation.
2700// --------------------------------------------------------------------
2701
2702template <int initialPowerMode, int targetPowerMode>
2703struct TransitionVariantCommon {
2704 static constexpr auto INITIAL_POWER_MODE = initialPowerMode;
2705 static constexpr auto TARGET_POWER_MODE = targetPowerMode;
2706
2707 static void verifyPostconditions(DisplayTransactionTest*) {}
2708};
2709
2710struct TransitionOffToOnVariant
2711 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_NORMAL> {
2712 template <typename Case>
2713 static void setupCallExpectations(DisplayTransactionTest* test) {
2714 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2715 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002716 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002717 Case::setupRepaintEverythingCallExpectations(test);
2718 }
2719
2720 static void verifyPostconditions(DisplayTransactionTest* test) {
2721 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2722 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2723 }
2724};
2725
2726struct TransitionOffToDozeSuspendVariant
2727 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_DOZE_SUSPEND> {
2728 template <typename Case>
2729 static void setupCallExpectations(DisplayTransactionTest* test) {
2730 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2731 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2732 Case::setupRepaintEverythingCallExpectations(test);
2733 }
2734
2735 static void verifyPostconditions(DisplayTransactionTest* test) {
2736 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2737 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2738 }
2739};
2740
2741struct TransitionOnToOffVariant
2742 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_OFF> {
2743 template <typename Case>
2744 static void setupCallExpectations(DisplayTransactionTest* test) {
2745 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002746 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002747 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2748 }
2749
2750 static void verifyPostconditions(DisplayTransactionTest* test) {
2751 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2752 }
2753};
2754
2755struct TransitionDozeSuspendToOffVariant
2756 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_OFF> {
2757 template <typename Case>
2758 static void setupCallExpectations(DisplayTransactionTest* test) {
2759 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2760 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2761 }
2762
2763 static void verifyPostconditions(DisplayTransactionTest* test) {
2764 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2765 }
2766};
2767
2768struct TransitionOnToDozeVariant
2769 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE> {
2770 template <typename Case>
2771 static void setupCallExpectations(DisplayTransactionTest* test) {
2772 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2773 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2774 }
2775};
2776
2777struct TransitionDozeSuspendToDozeVariant
2778 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_DOZE> {
2779 template <typename Case>
2780 static void setupCallExpectations(DisplayTransactionTest* test) {
2781 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002782 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002783 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2784 }
2785};
2786
2787struct TransitionDozeToOnVariant
2788 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE, HWC_POWER_MODE_NORMAL> {
2789 template <typename Case>
2790 static void setupCallExpectations(DisplayTransactionTest* test) {
2791 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2792 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2793 }
2794};
2795
2796struct TransitionDozeSuspendToOnVariant
2797 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_NORMAL> {
2798 template <typename Case>
2799 static void setupCallExpectations(DisplayTransactionTest* test) {
2800 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002801 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002802 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2803 }
2804};
2805
2806struct TransitionOnToDozeSuspendVariant
2807 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE_SUSPEND> {
2808 template <typename Case>
2809 static void setupCallExpectations(DisplayTransactionTest* test) {
2810 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002811 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002812 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2813 }
2814};
2815
2816struct TransitionOnToUnknownVariant
2817 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_LEET> {
2818 template <typename Case>
2819 static void setupCallExpectations(DisplayTransactionTest* test) {
2820 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2821 Case::setupNoComposerPowerModeCallExpectations(test);
2822 }
2823};
2824
2825// --------------------------------------------------------------------
2826// Note:
2827//
2828// Rather than testing the cartesian product of of
2829// DozeIsSupported/DozeNotSupported with all other options, we use one for one
2830// display type, and the other for another display type.
2831// --------------------------------------------------------------------
2832
2833template <typename DisplayVariant, typename DozeVariant, typename EventThreadVariant,
Lloyd Pique41be5d22018-06-21 13:11:48 -07002834 typename DispSyncVariant, typename TransitionVariant>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002835struct DisplayPowerCase {
2836 using Display = DisplayVariant;
2837 using Doze = DozeVariant;
2838 using EventThread = EventThreadVariant;
Lloyd Pique41be5d22018-06-21 13:11:48 -07002839 using DispSync = DispSyncVariant;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002840 using Transition = TransitionVariant;
2841
2842 static auto injectDisplayWithInitialPowerMode(DisplayTransactionTest* test, int mode) {
2843 Display::injectHwcDisplay(test);
2844 auto display = Display::makeFakeExistingDisplayInjector(test);
2845 display.inject();
2846 display.mutableDisplayDevice()->setPowerMode(mode);
2847 return display;
2848 }
2849
2850 static void setInitialPrimaryHWVsyncEnabled(DisplayTransactionTest* test, bool enabled) {
2851 test->mFlinger.mutablePrimaryHWVsyncEnabled() = enabled;
2852 }
2853
2854 static void setupRepaintEverythingCallExpectations(DisplayTransactionTest* test) {
2855 EXPECT_CALL(*test->mMessageQueue, invalidate()).Times(1);
2856 }
2857
2858 static void setupSurfaceInterceptorCallExpectations(DisplayTransactionTest* test, int mode) {
2859 EXPECT_CALL(*test->mSurfaceInterceptor, isEnabled()).WillOnce(Return(true));
2860 EXPECT_CALL(*test->mSurfaceInterceptor, savePowerModeUpdate(_, mode)).Times(1);
2861 }
2862
2863 static void setupComposerCallExpectations(DisplayTransactionTest* test,
2864 IComposerClient::PowerMode mode) {
2865 // Any calls to get the active config will return a default value.
2866 EXPECT_CALL(*test->mComposer, getActiveConfig(Display::HWC_DISPLAY_ID, _))
2867 .WillRepeatedly(DoAll(SetArgPointee<1>(Display::HWC_ACTIVE_CONFIG_ID),
2868 Return(Error::NONE)));
2869
2870 // Any calls to get whether the display supports dozing will return the value set by the
2871 // policy variant.
2872 EXPECT_CALL(*test->mComposer, getDozeSupport(Display::HWC_DISPLAY_ID, _))
2873 .WillRepeatedly(DoAll(SetArgPointee<1>(Doze::DOZE_SUPPORTED), Return(Error::NONE)));
2874
2875 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, mode)).Times(1);
2876 }
2877
2878 static void setupNoComposerPowerModeCallExpectations(DisplayTransactionTest* test) {
2879 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, _)).Times(0);
2880 }
2881};
2882
2883// A sample configuration for the primary display.
2884// In addition to having event thread support, we emulate doze support.
2885template <typename TransitionVariant>
Peiyong Lined531a32018-10-26 18:27:56 -07002886using PrimaryDisplayPowerCase =
2887 DisplayPowerCase<PrimaryDisplayVariant, DozeIsSupportedVariant<PrimaryDisplayVariant>,
2888 EventThreadIsSupportedVariant, DispSyncIsSupportedVariant,
2889 TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002890
2891// A sample configuration for the external display.
2892// In addition to not having event thread support, we emulate not having doze
2893// support.
2894template <typename TransitionVariant>
Peiyong Lined531a32018-10-26 18:27:56 -07002895using ExternalDisplayPowerCase =
2896 DisplayPowerCase<ExternalDisplayVariant, DozeNotSupportedVariant<ExternalDisplayVariant>,
2897 EventThreadNotSupportedVariant, DispSyncNotSupportedVariant,
2898 TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002899
2900class SetPowerModeInternalTest : public DisplayTransactionTest {
2901public:
2902 template <typename Case>
2903 void transitionDisplayCommon();
2904};
2905
2906template <int PowerMode>
2907struct PowerModeInitialVSyncEnabled : public std::false_type {};
2908
2909template <>
2910struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_NORMAL> : public std::true_type {};
2911
2912template <>
2913struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_DOZE> : public std::true_type {};
2914
2915template <typename Case>
2916void SetPowerModeInternalTest::transitionDisplayCommon() {
2917 // --------------------------------------------------------------------
2918 // Preconditions
2919
Peiyong Lined531a32018-10-26 18:27:56 -07002920 Case::Doze::setupComposerCallExpectations(this);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002921 auto display =
2922 Case::injectDisplayWithInitialPowerMode(this, Case::Transition::INITIAL_POWER_MODE);
2923 Case::setInitialPrimaryHWVsyncEnabled(this,
2924 PowerModeInitialVSyncEnabled<
2925 Case::Transition::INITIAL_POWER_MODE>::value);
2926
2927 // --------------------------------------------------------------------
2928 // Call Expectations
2929
2930 Case::setupSurfaceInterceptorCallExpectations(this, Case::Transition::TARGET_POWER_MODE);
2931 Case::Transition::template setupCallExpectations<Case>(this);
2932
2933 // --------------------------------------------------------------------
2934 // Invocation
2935
2936 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(),
2937 Case::Transition::TARGET_POWER_MODE);
2938
2939 // --------------------------------------------------------------------
2940 // Postconditions
2941
2942 Case::Transition::verifyPostconditions(this);
2943}
2944
2945TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfNoChange) {
2946 using Case = SimplePrimaryDisplayCase;
2947
2948 // --------------------------------------------------------------------
2949 // Preconditions
2950
2951 // A primary display device is set up
2952 Case::Display::injectHwcDisplay(this);
2953 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2954 display.inject();
2955
Dominik Laskowskia2edf612018-06-01 13:15:16 -07002956 // The display is already set to HWC_POWER_MODE_NORMAL
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002957 display.mutableDisplayDevice()->setPowerMode(HWC_POWER_MODE_NORMAL);
2958
2959 // --------------------------------------------------------------------
2960 // Invocation
2961
2962 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_NORMAL);
2963
2964 // --------------------------------------------------------------------
2965 // Postconditions
2966
2967 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2968}
2969
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002970TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfVirtualDisplay) {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002971 using Case = HwcVirtualDisplayCase;
2972
2973 // --------------------------------------------------------------------
2974 // Preconditions
2975
Dominik Laskowski075d3172018-05-24 15:50:06 -07002976 // Insert display data so that the HWC thinks it created the virtual display.
2977 const auto displayId = Case::Display::DISPLAY_ID::get();
2978 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08002979 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002980
2981 // A virtual display device is set up
2982 Case::Display::injectHwcDisplay(this);
2983 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2984 display.inject();
2985
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002986 // The display is set to HWC_POWER_MODE_NORMAL
2987 getDisplayDevice(display.token())->setPowerMode(HWC_POWER_MODE_NORMAL);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002988
2989 // --------------------------------------------------------------------
2990 // Invocation
2991
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002992 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_OFF);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002993
2994 // --------------------------------------------------------------------
2995 // Postconditions
2996
2997 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2998}
2999
3000TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnPrimaryDisplay) {
3001 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToOnVariant>>();
3002}
3003
3004TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendPrimaryDisplay) {
3005 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
3006}
3007
3008TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffPrimaryDisplay) {
3009 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToOffVariant>>();
3010}
3011
3012TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffPrimaryDisplay) {
3013 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
3014}
3015
3016TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozePrimaryDisplay) {
3017 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeVariant>>();
3018}
3019
3020TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozePrimaryDisplay) {
3021 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
3022}
3023
3024TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnPrimaryDisplay) {
3025 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeToOnVariant>>();
3026}
3027
3028TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnPrimaryDisplay) {
3029 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
3030}
3031
3032TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendPrimaryDisplay) {
3033 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
3034}
3035
3036TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownPrimaryDisplay) {
3037 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToUnknownVariant>>();
3038}
3039
3040TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnExternalDisplay) {
3041 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToOnVariant>>();
3042}
3043
3044TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendExternalDisplay) {
3045 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
3046}
3047
3048TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffExternalDisplay) {
3049 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToOffVariant>>();
3050}
3051
3052TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffExternalDisplay) {
3053 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
3054}
3055
3056TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeExternalDisplay) {
3057 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeVariant>>();
3058}
3059
3060TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozeExternalDisplay) {
3061 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
3062}
3063
3064TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnExternalDisplay) {
3065 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeToOnVariant>>();
3066}
3067
3068TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnExternalDisplay) {
3069 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
3070}
3071
3072TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendExternalDisplay) {
3073 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
3074}
3075
3076TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownExternalDisplay) {
3077 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToUnknownVariant>>();
3078}
3079
Lloyd Piquef58625d2017-12-19 13:22:33 -08003080} // namespace
3081} // namespace android