blob: ff84a620f76eb0c9a2a20102056c33e84eb38089 [file] [log] [blame]
Lloyd Piquef58625d2017-12-19 13:22:33 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#undef LOG_TAG
18#define LOG_TAG "LibSurfaceFlingerUnittests"
19
Dominik Laskowski075d3172018-05-24 15:50:06 -070020#include <type_traits>
21
Lloyd Piquef58625d2017-12-19 13:22:33 -080022#include <gmock/gmock.h>
23#include <gtest/gtest.h>
Lloyd Piquef58625d2017-12-19 13:22:33 -080024#include <log/log.h>
Lloyd Pique3823e7b2018-10-18 16:58:10 -070025#include <renderengine/mock/RenderEngine.h>
Valerie Hau9758ae02018-10-09 16:05:09 -070026#include <ui/DebugUtils.h>
Dominik Laskowski075d3172018-05-24 15:50:06 -070027
28#include "DisplayIdentificationTest.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080029#include "TestableSurfaceFlinger.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080030#include "mock/DisplayHardware/MockComposer.h"
31#include "mock/DisplayHardware/MockDisplaySurface.h"
Lloyd Pique41be5d22018-06-21 13:11:48 -070032#include "mock/MockDispSync.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080033#include "mock/MockEventControlThread.h"
34#include "mock/MockEventThread.h"
35#include "mock/MockMessageQueue.h"
36#include "mock/MockNativeWindowSurface.h"
37#include "mock/MockSurfaceInterceptor.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080038#include "mock/gui/MockGraphicBufferConsumer.h"
39#include "mock/gui/MockGraphicBufferProducer.h"
40#include "mock/system/window/MockNativeWindow.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080041
42namespace android {
43namespace {
44
Lloyd Piquee39cad22017-12-20 17:01:29 -080045using testing::_;
46using testing::ByMove;
47using testing::DoAll;
48using testing::Mock;
49using testing::Return;
50using testing::SetArgPointee;
51
Lloyd Piqued883d5a2018-04-27 19:32:30 -070052using android::Hwc2::ColorMode;
Lloyd Piquee39cad22017-12-20 17:01:29 -080053using android::Hwc2::Error;
Lloyd Piqued883d5a2018-04-27 19:32:30 -070054using android::Hwc2::Hdr;
Lloyd Piquee39cad22017-12-20 17:01:29 -080055using android::Hwc2::IComposer;
56using android::Hwc2::IComposerClient;
Lloyd Piqued883d5a2018-04-27 19:32:30 -070057using android::Hwc2::PerFrameMetadataKey;
58using android::Hwc2::RenderIntent;
Lloyd Piquee39cad22017-12-20 17:01:29 -080059
Lloyd Piquec11e0d32018-01-22 18:44:59 -080060using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector;
61using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
Lloyd Pique1fa4d462018-01-22 18:03:16 -080062using HotplugEvent = TestableSurfaceFlinger::HotplugEvent;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080063using HWC2Display = TestableSurfaceFlinger::HWC2Display;
Lloyd Piquebc792092018-01-17 11:52:30 -080064
Lloyd Piquec11e0d32018-01-22 18:44:59 -080065constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666;
Lloyd Piquee39cad22017-12-20 17:01:29 -080066constexpr int32_t DEFAULT_DPI = 320;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080067constexpr int DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT = HAL_PIXEL_FORMAT_RGB_565;
Lloyd Piquee39cad22017-12-20 17:01:29 -080068
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -080069constexpr int HWC_POWER_MODE_LEET = 1337; // An out of range power mode value
70
Lloyd Piquec11e0d32018-01-22 18:44:59 -080071/* ------------------------------------------------------------------------
72 * Boolean avoidance
73 *
74 * To make calls and template instantiations more readable, we define some
75 * local enums along with an implicit bool conversion.
76 */
77
78#define BOOL_SUBSTITUTE(TYPENAME) enum class TYPENAME : bool { FALSE = false, TRUE = true };
79
Lloyd Piquec11e0d32018-01-22 18:44:59 -080080BOOL_SUBSTITUTE(Async);
Dominik Laskowski075d3172018-05-24 15:50:06 -070081BOOL_SUBSTITUTE(Critical);
82BOOL_SUBSTITUTE(Primary);
Lloyd Piquec11e0d32018-01-22 18:44:59 -080083BOOL_SUBSTITUTE(Secure);
Dominik Laskowski075d3172018-05-24 15:50:06 -070084BOOL_SUBSTITUTE(Virtual);
Lloyd Piquec11e0d32018-01-22 18:44:59 -080085
86/* ------------------------------------------------------------------------
87 *
88 */
Lloyd Pique1fa4d462018-01-22 18:03:16 -080089
Lloyd Piquef58625d2017-12-19 13:22:33 -080090class DisplayTransactionTest : public testing::Test {
Lloyd Piquec11e0d32018-01-22 18:44:59 -080091public:
Lloyd Piquef58625d2017-12-19 13:22:33 -080092 DisplayTransactionTest();
93 ~DisplayTransactionTest() override;
94
Lloyd Pique1fa4d462018-01-22 18:03:16 -080095 // --------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -080096 // Mock/Fake injection
Lloyd Piquef58625d2017-12-19 13:22:33 -080097
Lloyd Piquec11e0d32018-01-22 18:44:59 -080098 void injectMockComposer(int virtualDisplayCount);
99 void injectFakeBufferQueueFactory();
100 void injectFakeNativeWindowSurfaceFactory();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800101
102 // --------------------------------------------------------------------
103 // Postcondition helpers
104
Dominik Laskowski075d3172018-05-24 15:50:06 -0700105 bool hasPhysicalHwcDisplay(hwc2_display_t hwcDisplayId);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800106 bool hasTransactionFlagSet(int flag);
107 bool hasDisplayDevice(sp<IBinder> displayToken);
108 sp<DisplayDevice> getDisplayDevice(sp<IBinder> displayToken);
109 bool hasCurrentDisplayState(sp<IBinder> displayToken);
110 const DisplayDeviceState& getCurrentDisplayState(sp<IBinder> displayToken);
111 bool hasDrawingDisplayState(sp<IBinder> displayToken);
112 const DisplayDeviceState& getDrawingDisplayState(sp<IBinder> displayToken);
113
114 // --------------------------------------------------------------------
115 // Test instances
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800116
Lloyd Piquef58625d2017-12-19 13:22:33 -0800117 TestableSurfaceFlinger mFlinger;
Lloyd Piquee39cad22017-12-20 17:01:29 -0800118 mock::EventThread* mEventThread = new mock::EventThread();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800119 mock::EventControlThread* mEventControlThread = new mock::EventControlThread();
Alec Mouriba013fa2018-10-16 12:43:11 -0700120 sp<mock::NativeWindow> mNativeWindow = new mock::NativeWindow();
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800121 sp<GraphicBuffer> mBuffer = new GraphicBuffer();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800122
123 // These mocks are created by the test, but are destroyed by SurfaceFlinger
124 // by virtue of being stored into a std::unique_ptr. However we still need
125 // to keep a reference to them for use in setting up call expectations.
Peiyong Lin833074a2018-08-28 11:53:54 -0700126 renderengine::mock::RenderEngine* mRenderEngine = new renderengine::mock::RenderEngine();
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800127 Hwc2::mock::Composer* mComposer = nullptr;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800128 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
129 mock::SurfaceInterceptor* mSurfaceInterceptor = new mock::SurfaceInterceptor();
Lloyd Pique41be5d22018-06-21 13:11:48 -0700130 mock::DispSync* mPrimaryDispSync = new mock::DispSync();
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800131
132 // These mocks are created only when expected to be created via a factory.
133 sp<mock::GraphicBufferConsumer> mConsumer;
134 sp<mock::GraphicBufferProducer> mProducer;
Lloyd Pique22098362018-09-13 11:46:49 -0700135 surfaceflinger::mock::NativeWindowSurface* mNativeWindowSurface = nullptr;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800136};
137
138DisplayTransactionTest::DisplayTransactionTest() {
139 const ::testing::TestInfo* const test_info =
140 ::testing::UnitTest::GetInstance()->current_test_info();
141 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800142
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800143 // Default to no wide color display support configured
144 mFlinger.mutableHasWideColorDisplay() = false;
Peiyong Lin13effd12018-07-24 17:01:47 -0700145 mFlinger.mutableUseColorManagement() = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800146 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
147
148 // Default to using HWC virtual displays
149 mFlinger.mutableUseHwcVirtualDisplays() = true;
150
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800151 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
152 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
153 });
154
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800155 mFlinger.setCreateNativeWindowSurface([](auto) {
156 ADD_FAILURE() << "Unexpected request to create a native window surface.";
157 return nullptr;
158 });
159
160 mFlinger.mutableEventControlThread().reset(mEventControlThread);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800161 mFlinger.mutableEventThread().reset(mEventThread);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800162 mFlinger.mutableEventQueue().reset(mMessageQueue);
Peiyong Lin833074a2018-08-28 11:53:54 -0700163 mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800164 mFlinger.mutableInterceptor().reset(mSurfaceInterceptor);
Lloyd Pique41be5d22018-06-21 13:11:48 -0700165 mFlinger.mutablePrimaryDispSync().reset(mPrimaryDispSync);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800166
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800167 injectMockComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -0800168}
169
170DisplayTransactionTest::~DisplayTransactionTest() {
171 const ::testing::TestInfo* const test_info =
172 ::testing::UnitTest::GetInstance()->current_test_info();
173 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
174}
175
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800176void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
177 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800178 EXPECT_CALL(*mComposer, getCapabilities())
179 .WillOnce(Return(std::vector<IComposer::Capability>()));
180 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
181 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -0800182
Lloyd Piquee39cad22017-12-20 17:01:29 -0800183 Mock::VerifyAndClear(mComposer);
184}
185
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800186void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800187 // This setup is only expected once per test.
188 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
189
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800190 mConsumer = new mock::GraphicBufferConsumer();
191 mProducer = new mock::GraphicBufferProducer();
192
193 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
194 *outProducer = mProducer;
195 *outConsumer = mConsumer;
196 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800197}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800198
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800199void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800200 // This setup is only expected once per test.
201 ASSERT_TRUE(mNativeWindowSurface == nullptr);
202
Lloyd Pique22098362018-09-13 11:46:49 -0700203 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800204
Lloyd Pique22098362018-09-13 11:46:49 -0700205 mFlinger.setCreateNativeWindowSurface([this](auto) {
206 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
207 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800208}
209
Dominik Laskowski075d3172018-05-24 15:50:06 -0700210bool DisplayTransactionTest::hasPhysicalHwcDisplay(hwc2_display_t hwcDisplayId) {
211 return mFlinger.mutableHwcPhysicalDisplayIdMap().count(hwcDisplayId) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800212}
213
214bool DisplayTransactionTest::hasTransactionFlagSet(int flag) {
215 return mFlinger.mutableTransactionFlags() & flag;
216}
217
218bool DisplayTransactionTest::hasDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700219 return mFlinger.mutableDisplays().count(displayToken) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800220}
221
222sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700223 return mFlinger.mutableDisplays()[displayToken];
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800224}
225
226bool DisplayTransactionTest::hasCurrentDisplayState(sp<IBinder> displayToken) {
227 return mFlinger.mutableCurrentState().displays.indexOfKey(displayToken) >= 0;
228}
229
230const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(sp<IBinder> displayToken) {
231 return mFlinger.mutableCurrentState().displays.valueFor(displayToken);
232}
233
234bool DisplayTransactionTest::hasDrawingDisplayState(sp<IBinder> displayToken) {
235 return mFlinger.mutableDrawingState().displays.indexOfKey(displayToken) >= 0;
236}
237
238const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(sp<IBinder> displayToken) {
239 return mFlinger.mutableDrawingState().displays.valueFor(displayToken);
240}
241
242/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800243 *
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800244 */
245
Dominik Laskowski075d3172018-05-24 15:50:06 -0700246template <typename PhysicalDisplay>
247struct PhysicalDisplayId {};
248
Dominik Laskowski34157762018-10-31 13:07:19 -0700249template <DisplayId::Type displayId>
250using VirtualDisplayId = std::integral_constant<DisplayId::Type, displayId>;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700251
252struct NoDisplayId {};
253
254template <typename>
255struct IsPhysicalDisplayId : std::bool_constant<false> {};
256
257template <typename PhysicalDisplay>
258struct IsPhysicalDisplayId<PhysicalDisplayId<PhysicalDisplay>> : std::bool_constant<true> {};
259
260template <typename>
261struct DisplayIdGetter;
262
263template <typename PhysicalDisplay>
264struct DisplayIdGetter<PhysicalDisplayId<PhysicalDisplay>> {
265 static std::optional<DisplayId> get() {
266 if (!PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
267 return getFallbackDisplayId(static_cast<bool>(PhysicalDisplay::PRIMARY)
268 ? HWC_DISPLAY_PRIMARY
269 : HWC_DISPLAY_EXTERNAL);
270 }
271
272 const auto info =
273 parseDisplayIdentificationData(PhysicalDisplay::PORT,
274 PhysicalDisplay::GET_IDENTIFICATION_DATA());
275 return info ? std::make_optional(info->id) : std::nullopt;
276 }
277};
278
Dominik Laskowski34157762018-10-31 13:07:19 -0700279template <DisplayId::Type displayId>
Dominik Laskowski075d3172018-05-24 15:50:06 -0700280struct DisplayIdGetter<VirtualDisplayId<displayId>> {
Dominik Laskowski34157762018-10-31 13:07:19 -0700281 static std::optional<DisplayId> get() { return DisplayId{displayId}; }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700282};
283
284template <>
285struct DisplayIdGetter<NoDisplayId> {
286 static std::optional<DisplayId> get() { return {}; }
287};
288
289// DisplayIdType can be:
290// 1) PhysicalDisplayId<...> for generated ID of physical display backed by HWC.
291// 2) VirtualDisplayId<...> for hard-coded ID of virtual display backed by HWC.
292// 3) NoDisplayId for virtual display without HWC backing.
293template <typename DisplayIdType, int width, int height, Critical critical, Async async,
294 Secure secure, Primary primary, int grallocUsage>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800295struct DisplayVariant {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700296 using DISPLAY_ID = DisplayIdGetter<DisplayIdType>;
297
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800298 // The display width and height
299 static constexpr int WIDTH = width;
300 static constexpr int HEIGHT = height;
301
302 static constexpr int GRALLOC_USAGE = grallocUsage;
303
Dominik Laskowski075d3172018-05-24 15:50:06 -0700304 // Whether the display is virtual or physical
305 static constexpr Virtual VIRTUAL =
306 IsPhysicalDisplayId<DisplayIdType>{} ? Virtual::FALSE : Virtual::TRUE;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800307
308 // When creating native window surfaces for the framebuffer, whether those should be critical
309 static constexpr Critical CRITICAL = critical;
310
311 // When creating native window surfaces for the framebuffer, whether those should be async
312 static constexpr Async ASYNC = async;
313
314 // Whether the display should be treated as secure
315 static constexpr Secure SECURE = secure;
316
Dominik Laskowski075d3172018-05-24 15:50:06 -0700317 // Whether the display is primary
318 static constexpr Primary PRIMARY = primary;
319
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800320 static auto makeFakeExistingDisplayInjector(DisplayTransactionTest* test) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700321 auto injector =
322 FakeDisplayDeviceInjector(test->mFlinger, DISPLAY_ID::get(),
323 static_cast<bool>(VIRTUAL), static_cast<bool>(PRIMARY));
324
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800325 injector.setSecure(static_cast<bool>(SECURE));
Alec Mouriba013fa2018-10-16 12:43:11 -0700326 injector.setNativeWindow(test->mNativeWindow);
327
328 // Creating a DisplayDevice requires getting default dimensions from the
329 // native window.
330 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
331 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
332 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
333 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800334 return injector;
335 }
336
337 // Called by tests to set up any native window creation call expectations.
338 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
339 EXPECT_CALL(*test->mNativeWindowSurface, getNativeWindow())
340 .WillOnce(Return(test->mNativeWindow));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800341
Alec Mouriba013fa2018-10-16 12:43:11 -0700342 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
343 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
344 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
345 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800346 }
347
348 static void setupFramebufferConsumerBufferQueueCallExpectations(DisplayTransactionTest* test) {
349 EXPECT_CALL(*test->mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR));
350 EXPECT_CALL(*test->mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR));
351 EXPECT_CALL(*test->mConsumer, setConsumerUsageBits(GRALLOC_USAGE))
352 .WillRepeatedly(Return(NO_ERROR));
353 EXPECT_CALL(*test->mConsumer, setDefaultBufferSize(WIDTH, HEIGHT))
354 .WillRepeatedly(Return(NO_ERROR));
355 EXPECT_CALL(*test->mConsumer, setMaxAcquiredBufferCount(_))
356 .WillRepeatedly(Return(NO_ERROR));
357 }
358
359 static void setupFramebufferProducerBufferQueueCallExpectations(DisplayTransactionTest* test) {
360 EXPECT_CALL(*test->mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return());
361 }
362};
363
Dominik Laskowski075d3172018-05-24 15:50:06 -0700364template <hwc2_display_t hwcDisplayId, HWC2::DisplayType hwcDisplayType, typename DisplayVariant,
365 typename PhysicalDisplay = void>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800366struct HwcDisplayVariant {
367 // The display id supplied by the HWC
368 static constexpr hwc2_display_t HWC_DISPLAY_ID = hwcDisplayId;
369
370 // The HWC display type
371 static constexpr HWC2::DisplayType HWC_DISPLAY_TYPE = hwcDisplayType;
372
373 // The HWC active configuration id
Lloyd Pique3c085a02018-05-09 19:38:32 -0700374 static constexpr int HWC_ACTIVE_CONFIG_ID = 2001;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800375
376 static void injectPendingHotplugEvent(DisplayTransactionTest* test,
377 HWC2::Connection connection) {
378 test->mFlinger.mutablePendingHotplugEvents().emplace_back(
379 HotplugEvent{HWC_DISPLAY_ID, connection});
380 }
381
382 // Called by tests to inject a HWC display setup
383 static void injectHwcDisplay(DisplayTransactionTest* test) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700384 const auto displayId = DisplayVariant::DISPLAY_ID::get();
385 ASSERT_TRUE(displayId);
386 FakeHwcDisplayInjector(*displayId, HWC_DISPLAY_TYPE,
387 static_cast<bool>(DisplayVariant::PRIMARY))
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800388 .setHwcDisplayId(HWC_DISPLAY_ID)
389 .setWidth(DisplayVariant::WIDTH)
390 .setHeight(DisplayVariant::HEIGHT)
391 .setActiveConfig(HWC_ACTIVE_CONFIG_ID)
392 .inject(&test->mFlinger, test->mComposer);
393 }
394
395 static void setupHwcHotplugCallExpectations(DisplayTransactionTest* test) {
396 EXPECT_CALL(*test->mComposer, getDisplayType(HWC_DISPLAY_ID, _))
397 .WillOnce(DoAll(SetArgPointee<1>(static_cast<IComposerClient::DisplayType>(
398 HWC_DISPLAY_TYPE)),
399 Return(Error::NONE)));
400 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
401 EXPECT_CALL(*test->mComposer, getDisplayConfigs(HWC_DISPLAY_ID, _))
402 .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{HWC_ACTIVE_CONFIG_ID}),
403 Return(Error::NONE)));
404 EXPECT_CALL(*test->mComposer,
405 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
406 IComposerClient::Attribute::WIDTH, _))
407 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::WIDTH), Return(Error::NONE)));
408 EXPECT_CALL(*test->mComposer,
409 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
410 IComposerClient::Attribute::HEIGHT, _))
411 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::HEIGHT), Return(Error::NONE)));
412 EXPECT_CALL(*test->mComposer,
413 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
414 IComposerClient::Attribute::VSYNC_PERIOD, _))
415 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE)));
416 EXPECT_CALL(*test->mComposer,
417 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
418 IComposerClient::Attribute::DPI_X, _))
419 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
420 EXPECT_CALL(*test->mComposer,
421 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
422 IComposerClient::Attribute::DPI_Y, _))
423 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700424
425 if (PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
426 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
427 .WillOnce(DoAll(SetArgPointee<1>(PhysicalDisplay::PORT),
428 SetArgPointee<2>(PhysicalDisplay::GET_IDENTIFICATION_DATA()),
429 Return(Error::NONE)));
430 } else {
431 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
432 .WillOnce(Return(Error::UNSUPPORTED));
433 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800434 }
435
436 // Called by tests to set up HWC call expectations
437 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
438 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY_ID, _))
Lloyd Pique3c085a02018-05-09 19:38:32 -0700439 .WillRepeatedly(DoAll(SetArgPointee<1>(HWC_ACTIVE_CONFIG_ID), Return(Error::NONE)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800440 }
441};
442
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800443// Physical displays are expected to be synchronous, secure, and have a HWC display for output.
444constexpr uint32_t GRALLOC_USAGE_PHYSICAL_DISPLAY =
445 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
446
Dominik Laskowski075d3172018-05-24 15:50:06 -0700447template <hwc2_display_t hwcDisplayId, typename PhysicalDisplay, int width, int height,
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800448 Critical critical>
449struct PhysicalDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700450 : DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height, critical, Async::FALSE,
451 Secure::TRUE, PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
452 HwcDisplayVariant<hwcDisplayId, HWC2::DisplayType::Physical,
453 DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height,
454 critical, Async::FALSE, Secure::TRUE,
455 PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
456 PhysicalDisplay> {};
457
458template <bool hasIdentificationData>
459struct PrimaryDisplay {
460 static constexpr Primary PRIMARY = Primary::TRUE;
461 static constexpr uint8_t PORT = 255;
462 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
463 static constexpr auto GET_IDENTIFICATION_DATA = getInternalEdid;
464};
465
466template <bool hasIdentificationData>
467struct ExternalDisplay {
468 static constexpr Primary PRIMARY = Primary::FALSE;
469 static constexpr uint8_t PORT = 254;
470 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
471 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
472};
473
474struct TertiaryDisplay {
475 static constexpr Primary PRIMARY = Primary::FALSE;
476};
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800477
478// A primary display is a physical display that is critical
479using PrimaryDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700480 PhysicalDisplayVariant<1001, PrimaryDisplay<false>, 3840, 2160, Critical::TRUE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800481
482// An external display is physical display that is not critical.
483using ExternalDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700484 PhysicalDisplayVariant<1002, ExternalDisplay<false>, 1920, 1280, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800485
486using TertiaryDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700487 PhysicalDisplayVariant<1003, TertiaryDisplay, 1600, 1200, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800488
489// A virtual display not supported by the HWC.
490constexpr uint32_t GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY = 0;
491
492template <int width, int height, Secure secure>
493struct NonHwcVirtualDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700494 : DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
495 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY> {
496 using Base = DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
497 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>;
498
499 static void injectHwcDisplay(DisplayTransactionTest*) {}
500
501 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
502 EXPECT_CALL(*test->mComposer, getActiveConfig(_, _)).Times(0);
503 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800504
505 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
506 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
507 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
508 }
509};
510
511// A virtual display supported by the HWC.
512constexpr uint32_t GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY = GRALLOC_USAGE_HW_COMPOSER;
513
514template <int width, int height, Secure secure>
515struct HwcVirtualDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700516 : DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE, secure,
517 Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>,
518 HwcDisplayVariant<
519 1010, HWC2::DisplayType::Virtual,
520 DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
521 secure, Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>> {
522 using Base = DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
523 secure, Primary::FALSE, GRALLOC_USAGE_HW_COMPOSER>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800524 using Self = HwcVirtualDisplayVariant<width, height, secure>;
525
526 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
527 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
528 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
529 }
530
531 static void setupHwcVirtualDisplayCreationCallExpectations(DisplayTransactionTest* test) {
532 EXPECT_CALL(*test->mComposer, createVirtualDisplay(Base::WIDTH, Base::HEIGHT, _, _))
533 .WillOnce(DoAll(SetArgPointee<3>(Self::HWC_DISPLAY_ID), Return(Error::NONE)));
534 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
535 }
536};
537
538// For this variant, SurfaceFlinger should not configure itself with wide
539// display support, so the display should not be configured for wide-color
540// support.
541struct WideColorSupportNotConfiguredVariant {
542 static constexpr bool WIDE_COLOR_SUPPORTED = false;
543
544 static void injectConfigChange(DisplayTransactionTest* test) {
545 test->mFlinger.mutableHasWideColorDisplay() = false;
Peiyong Lin13effd12018-07-24 17:01:47 -0700546 test->mFlinger.mutableUseColorManagement() = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800547 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
548 }
549
550 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
551 EXPECT_CALL(*test->mComposer, getColorModes(_, _)).Times(0);
552 EXPECT_CALL(*test->mComposer, getRenderIntents(_, _, _)).Times(0);
553 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
554 }
555};
556
557// For this variant, SurfaceFlinger should configure itself with wide display
558// support, and the display should respond with an non-empty list of supported
559// color modes. Wide-color support should be configured.
560template <typename Display>
561struct WideColorP3ColorimetricSupportedVariant {
562 static constexpr bool WIDE_COLOR_SUPPORTED = true;
563
564 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700565 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800566 test->mFlinger.mutableHasWideColorDisplay() = true;
567 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
568 }
569
570 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
571 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
572 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>({ColorMode::DISPLAY_P3})),
573 Return(Error::NONE)));
574 EXPECT_CALL(*test->mComposer,
575 getRenderIntents(Display::HWC_DISPLAY_ID, ColorMode::DISPLAY_P3, _))
576 .WillOnce(DoAll(SetArgPointee<2>(
577 std::vector<RenderIntent>({RenderIntent::COLORIMETRIC})),
578 Return(Error::NONE)));
579 EXPECT_CALL(*test->mComposer,
580 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::SRGB,
581 RenderIntent::COLORIMETRIC))
582 .WillOnce(Return(Error::NONE));
583 }
584};
585
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800586// For this variant, SurfaceFlinger should configure itself with wide display
587// support, but the display should respond with an empty list of supported color
588// modes. Wide-color support for the display should not be configured.
589template <typename Display>
590struct WideColorNotSupportedVariant {
591 static constexpr bool WIDE_COLOR_SUPPORTED = false;
592
593 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700594 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800595 test->mFlinger.mutableHasWideColorDisplay() = true;
596 }
597
598 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
599 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
600 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>()), Return(Error::NONE)));
Chia-I Wu614e1422018-05-23 02:17:03 -0700601 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800602 }
603};
604
605// For this variant, the display is not a HWC display, so no HDR support should
606// be configured.
607struct NonHwcDisplayHdrSupportVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800608 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800609 static constexpr bool HDR10_SUPPORTED = false;
610 static constexpr bool HDR_HLG_SUPPORTED = false;
611 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
612 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
613 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
614 }
615};
616
Valerie Haue9e843a2018-12-18 13:39:23 -0800617template <typename Display>
618struct Hdr10PlusSupportedVariant {
619 static constexpr bool HDR10_PLUS_SUPPORTED = true;
620 static constexpr bool HDR10_SUPPORTED = true;
621 static constexpr bool HDR_HLG_SUPPORTED = false;
622 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
623 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
624 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _))
625 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({
626 Hdr::HDR10_PLUS,
627 Hdr::HDR10,
628 })),
629 Return(Error::NONE)));
630 }
631};
632
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800633// For this variant, the composer should respond with a non-empty list of HDR
634// modes containing HDR10, so HDR10 support should be configured.
635template <typename Display>
636struct Hdr10SupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800637 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800638 static constexpr bool HDR10_SUPPORTED = true;
639 static constexpr bool HDR_HLG_SUPPORTED = false;
640 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
641 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
642 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
643 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HDR10})),
644 Return(Error::NONE)));
645 }
646};
647
648// For this variant, the composer should respond with a non-empty list of HDR
649// modes containing HLG, so HLG support should be configured.
650template <typename Display>
651struct HdrHlgSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800652 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800653 static constexpr bool HDR10_SUPPORTED = false;
654 static constexpr bool HDR_HLG_SUPPORTED = true;
655 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
656 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
657 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
658 .WillOnce(
659 DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HLG})), Return(Error::NONE)));
660 }
661};
662
663// For this variant, the composer should respond with a non-empty list of HDR
664// modes containing DOLBY_VISION, so DOLBY_VISION support should be configured.
665template <typename Display>
666struct HdrDolbyVisionSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800667 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800668 static constexpr bool HDR10_SUPPORTED = false;
669 static constexpr bool HDR_HLG_SUPPORTED = false;
670 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = true;
671 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
672 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
673 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::DOLBY_VISION})),
674 Return(Error::NONE)));
675 }
676};
677
678// For this variant, the composer should respond with am empty list of HDR
679// modes, so no HDR support should be configured.
680template <typename Display>
681struct HdrNotSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800682 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800683 static constexpr bool HDR10_SUPPORTED = false;
684 static constexpr bool HDR_HLG_SUPPORTED = false;
685 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
686 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
687 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
688 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
689 }
690};
691
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700692struct NonHwcPerFrameMetadataSupportVariant {
693 static constexpr int PER_FRAME_METADATA_KEYS = 0;
694 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800695 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(_)).Times(0);
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700696 }
697};
698
699template <typename Display>
700struct NoPerFrameMetadataSupportVariant {
701 static constexpr int PER_FRAME_METADATA_KEYS = 0;
702 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800703 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
704 .WillOnce(Return(std::vector<PerFrameMetadataKey>()));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700705 }
706};
707
708template <typename Display>
709struct Smpte2086PerFrameMetadataSupportVariant {
710 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::SMPTE2086;
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>({
Valerie Haue9e843a2018-12-18 13:39:23 -0800714 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X,
715 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y,
716 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X,
717 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y,
718 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X,
719 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y,
720 PerFrameMetadataKey::WHITE_POINT_X,
721 PerFrameMetadataKey::WHITE_POINT_Y,
722 PerFrameMetadataKey::MAX_LUMINANCE,
723 PerFrameMetadataKey::MIN_LUMINANCE,
724 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700725 }
726};
727
728template <typename Display>
729struct Cta861_3_PerFrameMetadataSupportVariant {
730 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::CTA861_3;
731 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800732 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
733 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Valerie Haue9e843a2018-12-18 13:39:23 -0800734 PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL,
735 PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL,
736 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700737 }
738};
739
Valerie Haue9e843a2018-12-18 13:39:23 -0800740template <typename Display>
741struct Hdr10_Plus_PerFrameMetadataSupportVariant {
742 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::HDR10PLUS;
743 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
744 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
745 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
746 PerFrameMetadataKey::HDR10_PLUS_SEI,
747 })));
748 }
749};
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800750/* ------------------------------------------------------------------------
751 * Typical display configurations to test
752 */
753
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700754template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy,
755 typename PerFrameMetadataSupportPolicy>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800756struct Case {
757 using Display = DisplayPolicy;
758 using WideColorSupport = WideColorSupportPolicy;
759 using HdrSupport = HdrSupportPolicy;
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700760 using PerFrameMetadataSupport = PerFrameMetadataSupportPolicy;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800761};
762
763using SimplePrimaryDisplayCase =
764 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700765 HdrNotSupportedVariant<PrimaryDisplayVariant>,
766 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800767using SimpleExternalDisplayCase =
768 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700769 HdrNotSupportedVariant<ExternalDisplayVariant>,
770 NoPerFrameMetadataSupportVariant<ExternalDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800771using SimpleTertiaryDisplayCase =
772 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700773 HdrNotSupportedVariant<TertiaryDisplayVariant>,
774 NoPerFrameMetadataSupportVariant<TertiaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800775using NonHwcVirtualDisplayCase =
776 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700777 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant,
778 NonHwcPerFrameMetadataSupportVariant>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800779using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
780using HwcVirtualDisplayCase =
781 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
Lloyd Pique438e9e72018-09-04 18:06:08 -0700782 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>,
tangrobin6753a022018-08-10 10:58:54 +0800783 NoPerFrameMetadataSupportVariant<SimpleHwcVirtualDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800784using WideColorP3ColorimetricDisplayCase =
785 Case<PrimaryDisplayVariant, WideColorP3ColorimetricSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700786 HdrNotSupportedVariant<PrimaryDisplayVariant>,
787 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Valerie Haue9e843a2018-12-18 13:39:23 -0800788using Hdr10PlusDisplayCase =
789 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
790 Hdr10SupportedVariant<PrimaryDisplayVariant>,
791 Hdr10_Plus_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800792using Hdr10DisplayCase =
793 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700794 Hdr10SupportedVariant<PrimaryDisplayVariant>,
795 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800796using HdrHlgDisplayCase =
797 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700798 HdrHlgSupportedVariant<PrimaryDisplayVariant>,
799 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800800using HdrDolbyVisionDisplayCase =
801 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700802 HdrDolbyVisionSupportedVariant<PrimaryDisplayVariant>,
803 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
804using HdrSmpte2086DisplayCase =
805 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
806 HdrNotSupportedVariant<PrimaryDisplayVariant>,
807 Smpte2086PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
808using HdrCta861_3_DisplayCase =
809 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
810 HdrNotSupportedVariant<PrimaryDisplayVariant>,
811 Cta861_3_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Dominik Laskowskif07b85b2018-06-11 12:49:15 -0700812
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800813/* ------------------------------------------------------------------------
Lloyd Pique6cf11032018-01-22 18:57:44 -0800814 *
815 * SurfaceFlinger::onHotplugReceived
816 */
817
818TEST_F(DisplayTransactionTest, hotplugEnqueuesEventsForDisplayTransaction) {
819 constexpr int currentSequenceId = 123;
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700820 constexpr hwc2_display_t hwcDisplayId1 = 456;
821 constexpr hwc2_display_t hwcDisplayId2 = 654;
Lloyd Pique6cf11032018-01-22 18:57:44 -0800822
823 // --------------------------------------------------------------------
824 // Preconditions
825
826 // Set the current sequence id for accepted events
827 mFlinger.mutableComposerSequenceId() = currentSequenceId;
828
829 // Set the main thread id so that the current thread does not appear to be
830 // the main thread.
831 mFlinger.mutableMainThreadId() = std::thread::id();
832
833 // --------------------------------------------------------------------
834 // Call Expectations
835
836 // We expect invalidate() to be invoked once to trigger display transaction
837 // processing.
838 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
839
840 // --------------------------------------------------------------------
841 // Invocation
842
843 // Simulate two hotplug events (a connect and a disconnect)
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700844 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId1, HWC2::Connection::Connected);
845 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId2, HWC2::Connection::Disconnected);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800846
847 // --------------------------------------------------------------------
848 // Postconditions
849
850 // The display transaction needed flag should be set.
851 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
852
853 // All events should be in the pending event queue.
854 const auto& pendingEvents = mFlinger.mutablePendingHotplugEvents();
855 ASSERT_EQ(2u, pendingEvents.size());
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700856 EXPECT_EQ(hwcDisplayId1, pendingEvents[0].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800857 EXPECT_EQ(HWC2::Connection::Connected, pendingEvents[0].connection);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700858 EXPECT_EQ(hwcDisplayId2, pendingEvents[1].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800859 EXPECT_EQ(HWC2::Connection::Disconnected, pendingEvents[1].connection);
860}
861
862TEST_F(DisplayTransactionTest, hotplugDiscardsUnexpectedEvents) {
863 constexpr int currentSequenceId = 123;
864 constexpr int otherSequenceId = 321;
865 constexpr hwc2_display_t displayId = 456;
866
867 // --------------------------------------------------------------------
868 // Preconditions
869
870 // Set the current sequence id for accepted events
871 mFlinger.mutableComposerSequenceId() = currentSequenceId;
872
873 // Set the main thread id so that the current thread does not appear to be
874 // the main thread.
875 mFlinger.mutableMainThreadId() = std::thread::id();
876
877 // --------------------------------------------------------------------
878 // Call Expectations
879
880 // We do not expect any calls to invalidate().
881 EXPECT_CALL(*mMessageQueue, invalidate()).Times(0);
882
883 // --------------------------------------------------------------------
884 // Invocation
885
886 // Call with an unexpected sequence id
887 mFlinger.onHotplugReceived(otherSequenceId, displayId, HWC2::Connection::Invalid);
888
889 // --------------------------------------------------------------------
890 // Postconditions
891
892 // The display transaction needed flag should not be set
893 EXPECT_FALSE(hasTransactionFlagSet(eDisplayTransactionNeeded));
894
895 // There should be no pending events
896 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
897}
898
899TEST_F(DisplayTransactionTest, hotplugProcessesEnqueuedEventsIfCalledOnMainThread) {
900 constexpr int currentSequenceId = 123;
901 constexpr hwc2_display_t displayId1 = 456;
902
903 // --------------------------------------------------------------------
904 // Note:
905 // --------------------------------------------------------------------
906 // This test case is a bit tricky. We want to verify that
907 // onHotplugReceived() calls processDisplayHotplugEventsLocked(), but we
908 // don't really want to provide coverage for everything the later function
909 // does as there are specific tests for it.
910 // --------------------------------------------------------------------
911
912 // --------------------------------------------------------------------
913 // Preconditions
914
915 // Set the current sequence id for accepted events
916 mFlinger.mutableComposerSequenceId() = currentSequenceId;
917
918 // Set the main thread id so that the current thread does appear to be the
919 // main thread.
920 mFlinger.mutableMainThreadId() = std::this_thread::get_id();
921
922 // --------------------------------------------------------------------
923 // Call Expectations
924
925 // We expect invalidate() to be invoked once to trigger display transaction
926 // processing.
927 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
928
929 // --------------------------------------------------------------------
930 // Invocation
931
932 // Simulate a disconnect on a display id that is not connected. This should
933 // be enqueued by onHotplugReceived(), and dequeued by
934 // processDisplayHotplugEventsLocked(), but then ignored as invalid.
935 mFlinger.onHotplugReceived(currentSequenceId, displayId1, HWC2::Connection::Disconnected);
936
937 // --------------------------------------------------------------------
938 // Postconditions
939
940 // The display transaction needed flag should be set.
941 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
942
943 // There should be no event queued on return, as it should have been
944 // processed.
945 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
946}
947
948/* ------------------------------------------------------------------------
Lloyd Piquea482f992018-01-22 19:00:34 -0800949 * SurfaceFlinger::createDisplay
950 */
951
952TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForNonsecureDisplay) {
953 const String8 name("virtual.test");
954
955 // --------------------------------------------------------------------
956 // Call Expectations
957
958 // The call should notify the interceptor that a display was created.
959 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
960
961 // --------------------------------------------------------------------
962 // Invocation
963
964 sp<IBinder> displayToken = mFlinger.createDisplay(name, false);
965
966 // --------------------------------------------------------------------
967 // Postconditions
968
969 // The display should have been added to the current state
970 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
971 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700972 EXPECT_TRUE(display.isVirtual());
973 EXPECT_FALSE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -0800974 EXPECT_EQ(name.string(), display.displayName);
975
976 // --------------------------------------------------------------------
977 // Cleanup conditions
978
979 // Destroying the display invalidates the display state.
980 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
981}
982
983TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForSecureDisplay) {
984 const String8 name("virtual.test");
985
986 // --------------------------------------------------------------------
987 // Call Expectations
988
989 // The call should notify the interceptor that a display was created.
990 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
991
992 // --------------------------------------------------------------------
993 // Invocation
994
995 sp<IBinder> displayToken = mFlinger.createDisplay(name, true);
996
997 // --------------------------------------------------------------------
998 // Postconditions
999
1000 // The display should have been added to the current state
1001 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1002 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001003 EXPECT_TRUE(display.isVirtual());
1004 EXPECT_TRUE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -08001005 EXPECT_EQ(name.string(), display.displayName);
1006
1007 // --------------------------------------------------------------------
1008 // Cleanup conditions
1009
1010 // Destroying the display invalidates the display state.
1011 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1012}
1013
1014/* ------------------------------------------------------------------------
1015 * SurfaceFlinger::destroyDisplay
1016 */
1017
1018TEST_F(DisplayTransactionTest, destroyDisplayClearsCurrentStateForDisplay) {
1019 using Case = NonHwcVirtualDisplayCase;
1020
1021 // --------------------------------------------------------------------
1022 // Preconditions
1023
1024 // A virtual display exists
1025 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1026 existing.inject();
1027
1028 // --------------------------------------------------------------------
1029 // Call Expectations
1030
1031 // The call should notify the interceptor that a display was created.
1032 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
1033
1034 // Destroying the display invalidates the display state.
1035 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1036
1037 // --------------------------------------------------------------------
1038 // Invocation
1039
1040 mFlinger.destroyDisplay(existing.token());
1041
1042 // --------------------------------------------------------------------
1043 // Postconditions
1044
1045 // The display should have been removed from the current state
1046 EXPECT_FALSE(hasCurrentDisplayState(existing.token()));
1047
1048 // Ths display should still exist in the drawing state
1049 EXPECT_TRUE(hasDrawingDisplayState(existing.token()));
1050
1051 // The display transaction needed flasg should be set
1052 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1053}
1054
1055TEST_F(DisplayTransactionTest, destroyDisplayHandlesUnknownDisplay) {
1056 // --------------------------------------------------------------------
1057 // Preconditions
1058
1059 sp<BBinder> displayToken = new BBinder();
1060
1061 // --------------------------------------------------------------------
1062 // Invocation
1063
1064 mFlinger.destroyDisplay(displayToken);
1065}
1066
1067/* ------------------------------------------------------------------------
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001068 * SurfaceFlinger::resetDisplayState
1069 */
1070
1071TEST_F(DisplayTransactionTest, resetDisplayStateClearsState) {
1072 using Case = NonHwcVirtualDisplayCase;
1073
1074 // --------------------------------------------------------------------
1075 // Preconditions
1076
1077 // vsync is enabled and available
1078 mFlinger.mutablePrimaryHWVsyncEnabled() = true;
1079 mFlinger.mutableHWVsyncAvailable() = true;
1080
1081 // A display exists
1082 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1083 existing.inject();
1084
1085 // --------------------------------------------------------------------
1086 // Call Expectations
1087
1088 // The call disable vsyncs
1089 EXPECT_CALL(*mEventControlThread, setVsyncEnabled(false)).Times(1);
1090
Lloyd Pique41be5d22018-06-21 13:11:48 -07001091 // The call ends any display resyncs
1092 EXPECT_CALL(*mPrimaryDispSync, endResync()).Times(1);
1093
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001094 // --------------------------------------------------------------------
1095 // Invocation
1096
1097 mFlinger.resetDisplayState();
1098
1099 // --------------------------------------------------------------------
1100 // Postconditions
1101
1102 // vsyncs should be off and not available.
1103 EXPECT_FALSE(mFlinger.mutablePrimaryHWVsyncEnabled());
1104 EXPECT_FALSE(mFlinger.mutableHWVsyncAvailable());
1105
1106 // The display should have been removed from the display map.
1107 EXPECT_FALSE(hasDisplayDevice(existing.token()));
1108
1109 // The display should still exist in the current state
1110 EXPECT_TRUE(hasCurrentDisplayState(existing.token()));
1111
1112 // The display should have been removed from the drawing state
1113 EXPECT_FALSE(hasDrawingDisplayState(existing.token()));
1114}
1115
1116/* ------------------------------------------------------------------------
Valerie Hau9758ae02018-10-09 16:05:09 -07001117 * DisplayDevice::GetBestColorMode
1118 */
1119class GetBestColorModeTest : public DisplayTransactionTest {
1120public:
Dominik Laskowski34157762018-10-31 13:07:19 -07001121 static constexpr DisplayId DEFAULT_DISPLAY_ID = DisplayId{777};
Dominik Laskowski075d3172018-05-24 15:50:06 -07001122
Valerie Hau9758ae02018-10-09 16:05:09 -07001123 GetBestColorModeTest()
1124 : DisplayTransactionTest(),
Dominik Laskowski075d3172018-05-24 15:50:06 -07001125 mInjector(FakeDisplayDeviceInjector(mFlinger, DEFAULT_DISPLAY_ID, false /* isVirtual */,
1126 true /* isPrimary */)) {}
Valerie Hau9758ae02018-10-09 16:05:09 -07001127
1128 void setHasWideColorGamut(bool hasWideColorGamut) { mHasWideColorGamut = hasWideColorGamut; }
1129
1130 void addHwcColorModesMapping(ui::ColorMode colorMode,
1131 std::vector<ui::RenderIntent> renderIntents) {
1132 mHwcColorModes[colorMode] = renderIntents;
1133 }
1134
1135 void setInputDataspace(ui::Dataspace dataspace) { mInputDataspace = dataspace; }
1136
1137 void setInputRenderIntent(ui::RenderIntent renderIntent) { mInputRenderIntent = renderIntent; }
1138
1139 void getBestColorMode() {
1140 mInjector.setHwcColorModes(mHwcColorModes);
1141 mInjector.setHasWideColorGamut(mHasWideColorGamut);
Alec Mouriba013fa2018-10-16 12:43:11 -07001142 mInjector.setNativeWindow(mNativeWindow);
1143
1144 // Creating a DisplayDevice requires getting default dimensions from the
1145 // native window.
1146 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
1147 .WillRepeatedly(DoAll(SetArgPointee<1>(1080 /* arbitrary */), Return(0)));
1148 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
1149 .WillRepeatedly(DoAll(SetArgPointee<1>(1920 /* arbitrary */), Return(0)));
Alec Mouri4efb6c22018-11-16 13:07:47 -08001150 EXPECT_CALL(*mNativeWindow, perform(9)).Times(1);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001151 EXPECT_CALL(*mNativeWindow, perform(13)).Times(1);
Alec Mouri4efb6c22018-11-16 13:07:47 -08001152 EXPECT_CALL(*mNativeWindow, perform(30)).Times(1);
Valerie Hau9758ae02018-10-09 16:05:09 -07001153 auto displayDevice = mInjector.inject();
1154
1155 displayDevice->getBestColorMode(mInputDataspace, mInputRenderIntent, &mOutDataspace,
1156 &mOutColorMode, &mOutRenderIntent);
1157 }
1158
1159 ui::Dataspace mOutDataspace;
1160 ui::ColorMode mOutColorMode;
1161 ui::RenderIntent mOutRenderIntent;
1162
1163private:
1164 ui::Dataspace mInputDataspace;
1165 ui::RenderIntent mInputRenderIntent;
1166 bool mHasWideColorGamut = false;
1167 std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> mHwcColorModes;
1168 FakeDisplayDeviceInjector mInjector;
1169};
1170
1171TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeSRGB) {
1172 addHwcColorModesMapping(ui::ColorMode::SRGB,
1173 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1174 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1175 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1176 setHasWideColorGamut(true);
1177
1178 getBestColorMode();
1179
Peiyong Lin14724e62018-12-05 07:27:30 -08001180 ASSERT_EQ(ui::Dataspace::V0_SRGB, mOutDataspace);
Valerie Hau9758ae02018-10-09 16:05:09 -07001181 ASSERT_EQ(ui::ColorMode::SRGB, mOutColorMode);
1182 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1183}
1184
1185TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDisplayP3) {
1186 addHwcColorModesMapping(ui::ColorMode::DISPLAY_P3,
1187 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1188 addHwcColorModesMapping(ui::ColorMode::SRGB,
1189 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1190 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1191 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1192 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1193 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1194 setHasWideColorGamut(true);
1195
1196 getBestColorMode();
1197
1198 ASSERT_EQ(ui::Dataspace::DISPLAY_P3, mOutDataspace);
1199 ASSERT_EQ(ui::ColorMode::DISPLAY_P3, mOutColorMode);
1200 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1201}
1202
1203TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDISPLAY_BT2020) {
1204 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1205 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1206 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1207 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1208 setHasWideColorGamut(true);
1209
1210 getBestColorMode();
1211
1212 ASSERT_EQ(ui::Dataspace::DISPLAY_BT2020, mOutDataspace);
1213 ASSERT_EQ(ui::ColorMode::DISPLAY_BT2020, mOutColorMode);
1214 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1215}
1216
1217/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001218 * SurfaceFlinger::setupNewDisplayDeviceInternal
1219 */
1220
1221class SetupNewDisplayDeviceInternalTest : public DisplayTransactionTest {
1222public:
1223 template <typename T>
1224 void setupNewDisplayDeviceInternalTest();
1225};
1226
1227template <typename Case>
1228void SetupNewDisplayDeviceInternalTest::setupNewDisplayDeviceInternalTest() {
1229 const sp<BBinder> displayToken = new BBinder();
1230 const sp<mock::DisplaySurface> displaySurface = new mock::DisplaySurface();
1231 const sp<mock::GraphicBufferProducer> producer = new mock::GraphicBufferProducer();
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001232
1233 // --------------------------------------------------------------------
1234 // Preconditions
1235
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001236 // Wide color displays support is configured appropriately
1237 Case::WideColorSupport::injectConfigChange(this);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001238
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001239 // The display is setup with the HWC.
1240 Case::Display::injectHwcDisplay(this);
1241
1242 // SurfaceFlinger will use a test-controlled factory for native window
1243 // surfaces.
1244 injectFakeNativeWindowSurfaceFactory();
1245
1246 // --------------------------------------------------------------------
1247 // Call Expectations
1248
1249 // Various native window calls will be made.
1250 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
Lloyd Pique3c085a02018-05-09 19:38:32 -07001251 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001252 Case::WideColorSupport::setupComposerCallExpectations(this);
1253 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001254 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001255
1256 // --------------------------------------------------------------------
1257 // Invocation
1258
1259 DisplayDeviceState state;
Dominik Laskowski075d3172018-05-24 15:50:06 -07001260 state.displayId = static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1261 : Case::Display::DISPLAY_ID::get();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001262 state.isSecure = static_cast<bool>(Case::Display::SECURE);
1263
Dominik Laskowski075d3172018-05-24 15:50:06 -07001264 auto device =
1265 mFlinger.setupNewDisplayDeviceInternal(displayToken, Case::Display::DISPLAY_ID::get(),
1266 state, displaySurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001267
1268 // --------------------------------------------------------------------
1269 // Postconditions
1270
1271 ASSERT_TRUE(device != nullptr);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001272 EXPECT_EQ(Case::Display::DISPLAY_ID::get(), device->getId());
1273 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), device->isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001274 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001275 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001276 EXPECT_EQ(Case::Display::WIDTH, device->getWidth());
1277 EXPECT_EQ(Case::Display::HEIGHT, device->getHeight());
1278 EXPECT_EQ(Case::WideColorSupport::WIDE_COLOR_SUPPORTED, device->hasWideColorGamut());
Valerie Haue9e843a2018-12-18 13:39:23 -08001279 EXPECT_EQ(Case::HdrSupport::HDR10_PLUS_SUPPORTED, device->hasHDR10PlusSupport());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001280 EXPECT_EQ(Case::HdrSupport::HDR10_SUPPORTED, device->hasHDR10Support());
1281 EXPECT_EQ(Case::HdrSupport::HDR_HLG_SUPPORTED, device->hasHLGSupport());
1282 EXPECT_EQ(Case::HdrSupport::HDR_DOLBY_VISION_SUPPORTED, device->hasDolbyVisionSupport());
Lloyd Pique3c085a02018-05-09 19:38:32 -07001283 // Note: This is not Case::Display::HWC_ACTIVE_CONFIG_ID as the ids are
1284 // remapped, and the test only ever sets up one config. If there were an error
1285 // looking up the remapped index, device->getActiveConfig() would be -1 instead.
1286 EXPECT_EQ(0, device->getActiveConfig());
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001287 EXPECT_EQ(Case::PerFrameMetadataSupport::PER_FRAME_METADATA_KEYS,
1288 device->getSupportedPerFrameMetadata());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001289}
1290
1291TEST_F(SetupNewDisplayDeviceInternalTest, createSimplePrimaryDisplay) {
1292 setupNewDisplayDeviceInternalTest<SimplePrimaryDisplayCase>();
1293}
1294
1295TEST_F(SetupNewDisplayDeviceInternalTest, createSimpleExternalDisplay) {
1296 setupNewDisplayDeviceInternalTest<SimpleExternalDisplayCase>();
1297}
1298
1299TEST_F(SetupNewDisplayDeviceInternalTest, createNonHwcVirtualDisplay) {
1300 setupNewDisplayDeviceInternalTest<NonHwcVirtualDisplayCase>();
1301}
1302
1303TEST_F(SetupNewDisplayDeviceInternalTest, createHwcVirtualDisplay) {
Dominik Laskowski075d3172018-05-24 15:50:06 -07001304 using Case = HwcVirtualDisplayCase;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001305
Dominik Laskowski075d3172018-05-24 15:50:06 -07001306 // Insert display data so that the HWC thinks it created the virtual display.
1307 const auto displayId = Case::Display::DISPLAY_ID::get();
1308 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08001309 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001310
1311 setupNewDisplayDeviceInternalTest<Case>();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001312}
1313
1314TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3Display) {
1315 setupNewDisplayDeviceInternalTest<WideColorP3ColorimetricDisplayCase>();
1316}
1317
Valerie Haue9e843a2018-12-18 13:39:23 -08001318TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10PlusDisplay) {
1319 setupNewDisplayDeviceInternalTest<Hdr10PlusDisplayCase>();
1320}
1321
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001322TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10Display) {
1323 setupNewDisplayDeviceInternalTest<Hdr10DisplayCase>();
1324}
1325
1326TEST_F(SetupNewDisplayDeviceInternalTest, createHdrHlgDisplay) {
1327 setupNewDisplayDeviceInternalTest<HdrHlgDisplayCase>();
1328}
1329
1330TEST_F(SetupNewDisplayDeviceInternalTest, createHdrDolbyVisionDisplay) {
1331 setupNewDisplayDeviceInternalTest<HdrDolbyVisionDisplayCase>();
1332}
1333
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001334TEST_F(SetupNewDisplayDeviceInternalTest, createHdrSmpte2086DisplayCase) {
1335 setupNewDisplayDeviceInternalTest<HdrSmpte2086DisplayCase>();
1336}
1337
1338TEST_F(SetupNewDisplayDeviceInternalTest, createHdrCta816_3_DisplayCase) {
1339 setupNewDisplayDeviceInternalTest<HdrCta861_3_DisplayCase>();
1340}
1341
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001342/* ------------------------------------------------------------------------
1343 * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded)
1344 */
1345
1346class HandleTransactionLockedTest : public DisplayTransactionTest {
1347public:
1348 template <typename Case>
1349 void setupCommonPreconditions();
1350
1351 template <typename Case>
1352 void setupCommonCallExpectationsForConnectProcessing();
1353
1354 template <typename Case>
1355 void setupCommonCallExpectationsForDisconnectProcessing();
1356
1357 template <typename Case>
1358 void processesHotplugConnectCommon();
1359
1360 template <typename Case>
1361 void ignoresHotplugConnectCommon();
1362
1363 template <typename Case>
1364 void processesHotplugDisconnectCommon();
1365
1366 template <typename Case>
1367 void verifyDisplayIsConnected(const sp<IBinder>& displayToken);
1368
1369 template <typename Case>
1370 void verifyPhysicalDisplayIsConnected();
1371
1372 void verifyDisplayIsNotConnected(const sp<IBinder>& displayToken);
1373};
1374
1375template <typename Case>
1376void HandleTransactionLockedTest::setupCommonPreconditions() {
1377 // Wide color displays support is configured appropriately
1378 Case::WideColorSupport::injectConfigChange(this);
1379
1380 // SurfaceFlinger will use a test-controlled factory for BufferQueues
1381 injectFakeBufferQueueFactory();
1382
1383 // SurfaceFlinger will use a test-controlled factory for native window
1384 // surfaces.
1385 injectFakeNativeWindowSurfaceFactory();
1386}
1387
1388template <typename Case>
1389void HandleTransactionLockedTest::setupCommonCallExpectationsForConnectProcessing() {
1390 Case::Display::setupHwcHotplugCallExpectations(this);
1391
1392 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1393 Case::Display::setupFramebufferProducerBufferQueueCallExpectations(this);
1394 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1395 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
1396
1397 Case::WideColorSupport::setupComposerCallExpectations(this);
1398 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001399 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001400
1401 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001402 EXPECT_CALL(*mEventThread,
Dominik Laskowski075d3172018-05-24 15:50:06 -07001403 onHotplugReceived(static_cast<bool>(Case::Display::PRIMARY)
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001404 ? EventThread::DisplayType::Primary
1405 : EventThread::DisplayType::External,
1406 true))
1407 .Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001408}
1409
1410template <typename Case>
1411void HandleTransactionLockedTest::setupCommonCallExpectationsForDisconnectProcessing() {
1412 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001413 EXPECT_CALL(*mEventThread,
Dominik Laskowski075d3172018-05-24 15:50:06 -07001414 onHotplugReceived(static_cast<bool>(Case::Display::PRIMARY)
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001415 ? EventThread::DisplayType::Primary
1416 : EventThread::DisplayType::External,
1417 false))
1418 .Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001419}
1420
1421template <typename Case>
1422void HandleTransactionLockedTest::verifyDisplayIsConnected(const sp<IBinder>& displayToken) {
1423 // The display device should have been set up in the list of displays.
1424 ASSERT_TRUE(hasDisplayDevice(displayToken));
1425 const auto& device = getDisplayDevice(displayToken);
1426 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001427 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001428
1429 // The display should have been set up in the current display state
1430 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1431 const auto& current = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001432 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), current.isVirtual());
1433 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1434 : Case::Display::DISPLAY_ID::get(),
1435 current.displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001436
1437 // The display should have been set up in the drawing display state
1438 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1439 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001440 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
1441 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1442 : Case::Display::DISPLAY_ID::get(),
1443 draw.displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001444}
1445
1446template <typename Case>
1447void HandleTransactionLockedTest::verifyPhysicalDisplayIsConnected() {
1448 // HWComposer should have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001449 EXPECT_TRUE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001450
Dominik Laskowski075d3172018-05-24 15:50:06 -07001451 // SF should have a display token.
1452 const auto displayId = Case::Display::DISPLAY_ID::get();
1453 ASSERT_TRUE(displayId);
1454 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1455 auto& displayToken = mFlinger.mutablePhysicalDisplayTokens()[*displayId];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001456
1457 verifyDisplayIsConnected<Case>(displayToken);
1458}
1459
1460void HandleTransactionLockedTest::verifyDisplayIsNotConnected(const sp<IBinder>& displayToken) {
1461 EXPECT_FALSE(hasDisplayDevice(displayToken));
1462 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1463 EXPECT_FALSE(hasDrawingDisplayState(displayToken));
1464}
1465
1466template <typename Case>
1467void HandleTransactionLockedTest::processesHotplugConnectCommon() {
1468 // --------------------------------------------------------------------
1469 // Preconditions
1470
1471 setupCommonPreconditions<Case>();
1472
1473 // A hotplug connect event is enqueued for a display
1474 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001475
1476 // --------------------------------------------------------------------
1477 // Call Expectations
1478
1479 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillOnce(Return(false));
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001480
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001481 setupCommonCallExpectationsForConnectProcessing<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001482
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001483 // --------------------------------------------------------------------
1484 // Invocation
Lloyd Piquee39cad22017-12-20 17:01:29 -08001485
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001486 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
Lloyd Piquee39cad22017-12-20 17:01:29 -08001487
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001488 // --------------------------------------------------------------------
1489 // Postconditions
1490
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001491 verifyPhysicalDisplayIsConnected<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001492
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001493 // --------------------------------------------------------------------
1494 // Cleanup conditions
1495
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001496 EXPECT_CALL(*mComposer,
1497 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001498 .WillOnce(Return(Error::NONE));
1499 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Piquef58625d2017-12-19 13:22:33 -08001500}
1501
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001502template <typename Case>
1503void HandleTransactionLockedTest::ignoresHotplugConnectCommon() {
1504 // --------------------------------------------------------------------
1505 // Preconditions
1506
1507 setupCommonPreconditions<Case>();
1508
1509 // A hotplug connect event is enqueued for a display
1510 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1511
1512 // --------------------------------------------------------------------
1513 // Invocation
1514
1515 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1516
1517 // --------------------------------------------------------------------
1518 // Postconditions
1519
1520 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001521 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001522}
1523
1524template <typename Case>
1525void HandleTransactionLockedTest::processesHotplugDisconnectCommon() {
1526 // --------------------------------------------------------------------
1527 // Preconditions
1528
1529 setupCommonPreconditions<Case>();
1530
1531 // A hotplug disconnect event is enqueued for a display
1532 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1533
1534 // The display is already completely set up.
1535 Case::Display::injectHwcDisplay(this);
1536 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1537 existing.inject();
1538
1539 // --------------------------------------------------------------------
1540 // Call Expectations
1541
1542 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
Lloyd Pique438e9e72018-09-04 18:06:08 -07001543 EXPECT_CALL(*mComposer, getDisplayIdentificationData(Case::Display::HWC_DISPLAY_ID, _, _))
1544 .Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001545
1546 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1547
1548 // --------------------------------------------------------------------
1549 // Invocation
1550
1551 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1552
1553 // --------------------------------------------------------------------
1554 // Postconditions
1555
1556 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001557 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001558
Dominik Laskowski075d3172018-05-24 15:50:06 -07001559 // SF should not have a display token.
1560 const auto displayId = Case::Display::DISPLAY_ID::get();
1561 ASSERT_TRUE(displayId);
1562 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001563
1564 // The existing token should have been removed
1565 verifyDisplayIsNotConnected(existing.token());
1566}
1567
1568TEST_F(HandleTransactionLockedTest, processesHotplugConnectPrimaryDisplay) {
1569 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1570}
1571
1572TEST_F(HandleTransactionLockedTest,
1573 processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected) {
1574 // Inject an external display.
1575 ExternalDisplayVariant::injectHwcDisplay(this);
1576
1577 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1578}
1579
1580TEST_F(HandleTransactionLockedTest, processesHotplugConnectExternalDisplay) {
1581 // Inject a primary display.
1582 PrimaryDisplayVariant::injectHwcDisplay(this);
1583
1584 processesHotplugConnectCommon<SimpleExternalDisplayCase>();
1585}
1586
1587TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected) {
1588 // Inject both a primary and external display.
1589 PrimaryDisplayVariant::injectHwcDisplay(this);
1590 ExternalDisplayVariant::injectHwcDisplay(this);
1591
1592 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1593
1594 ignoresHotplugConnectCommon<SimpleTertiaryDisplayCase>();
1595}
1596
1597TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfExternalForVrComposer) {
1598 // Inject a primary display.
1599 PrimaryDisplayVariant::injectHwcDisplay(this);
1600
1601 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(true));
1602
1603 ignoresHotplugConnectCommon<SimpleExternalDisplayCase>();
1604}
1605
1606TEST_F(HandleTransactionLockedTest, processHotplugDisconnectPrimaryDisplay) {
1607 processesHotplugDisconnectCommon<SimplePrimaryDisplayCase>();
1608}
1609
1610TEST_F(HandleTransactionLockedTest, processHotplugDisconnectExternalDisplay) {
1611 processesHotplugDisconnectCommon<SimpleExternalDisplayCase>();
1612}
1613
1614TEST_F(HandleTransactionLockedTest, processesHotplugConnectThenDisconnectPrimary) {
1615 using Case = SimplePrimaryDisplayCase;
1616
1617 // --------------------------------------------------------------------
1618 // Preconditions
1619
1620 setupCommonPreconditions<Case>();
1621
1622 // A hotplug connect event is enqueued for a display
1623 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1624 // A hotplug disconnect event is also enqueued for the same display
1625 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1626
1627 // --------------------------------------------------------------------
1628 // Call Expectations
1629
1630 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1631
1632 setupCommonCallExpectationsForConnectProcessing<Case>();
1633 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1634
1635 EXPECT_CALL(*mComposer,
1636 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1637 .WillOnce(Return(Error::NONE));
1638 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1639
1640 // --------------------------------------------------------------------
1641 // Invocation
1642
1643 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1644
1645 // --------------------------------------------------------------------
1646 // Postconditions
1647
1648 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001649 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001650
Dominik Laskowski075d3172018-05-24 15:50:06 -07001651 // SF should not have a display token.
1652 const auto displayId = Case::Display::DISPLAY_ID::get();
1653 ASSERT_TRUE(displayId);
1654 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001655}
1656
1657TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectThenConnectPrimary) {
1658 using Case = SimplePrimaryDisplayCase;
1659
1660 // --------------------------------------------------------------------
1661 // Preconditions
1662
1663 setupCommonPreconditions<Case>();
1664
1665 // The display is already completely set up.
1666 Case::Display::injectHwcDisplay(this);
1667 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1668 existing.inject();
1669
1670 // A hotplug disconnect event is enqueued for a display
1671 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1672 // A hotplug connect event is also enqueued for the same display
1673 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1674
1675 // --------------------------------------------------------------------
1676 // Call Expectations
1677
1678 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1679
1680 setupCommonCallExpectationsForConnectProcessing<Case>();
1681 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1682
1683 // --------------------------------------------------------------------
1684 // Invocation
1685
1686 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1687
1688 // --------------------------------------------------------------------
1689 // Postconditions
1690
1691 // The existing token should have been removed
1692 verifyDisplayIsNotConnected(existing.token());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001693 const auto displayId = Case::Display::DISPLAY_ID::get();
1694 ASSERT_TRUE(displayId);
1695 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1696 EXPECT_NE(existing.token(), mFlinger.mutablePhysicalDisplayTokens()[*displayId]);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001697
1698 // A new display should be connected in its place
1699
1700 verifyPhysicalDisplayIsConnected<Case>();
1701
1702 // --------------------------------------------------------------------
1703 // Cleanup conditions
1704
1705 EXPECT_CALL(*mComposer,
1706 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1707 .WillOnce(Return(Error::NONE));
1708 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1709}
1710
1711TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAdded) {
1712 using Case = HwcVirtualDisplayCase;
1713
1714 // --------------------------------------------------------------------
1715 // Preconditions
1716
1717 // The HWC supports at least one virtual display
1718 injectMockComposer(1);
1719
1720 setupCommonPreconditions<Case>();
1721
1722 // A virtual display was added to the current state, and it has a
1723 // surface(producer)
1724 sp<BBinder> displayToken = new BBinder();
Lloyd Pique4c2ac022018-04-27 12:08:03 -07001725
Dominik Laskowski075d3172018-05-24 15:50:06 -07001726 DisplayDeviceState state;
1727 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001728
1729 sp<mock::GraphicBufferProducer> surface{new mock::GraphicBufferProducer()};
Dominik Laskowski075d3172018-05-24 15:50:06 -07001730 state.surface = surface;
1731 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001732
1733 // --------------------------------------------------------------------
1734 // Call Expectations
1735
1736 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1737 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1738
1739 EXPECT_CALL(*surface, query(NATIVE_WINDOW_WIDTH, _))
1740 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::WIDTH), Return(NO_ERROR)));
1741 EXPECT_CALL(*surface, query(NATIVE_WINDOW_HEIGHT, _))
1742 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::HEIGHT), Return(NO_ERROR)));
1743 EXPECT_CALL(*surface, query(NATIVE_WINDOW_FORMAT, _))
1744 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT),
1745 Return(NO_ERROR)));
1746 EXPECT_CALL(*surface, query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, _))
1747 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(NO_ERROR)));
1748
1749 EXPECT_CALL(*surface, setAsyncMode(true)).Times(1);
1750
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001751 EXPECT_CALL(*mProducer, disconnect(_, _)).Times(1);
1752
1753 Case::Display::setupHwcVirtualDisplayCreationCallExpectations(this);
1754 Case::WideColorSupport::setupComposerCallExpectations(this);
1755 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001756 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001757
1758 // --------------------------------------------------------------------
1759 // Invocation
1760
1761 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1762
1763 // --------------------------------------------------------------------
1764 // Postconditions
1765
1766 // The display device should have been set up in the list of displays.
1767 verifyDisplayIsConnected<Case>(displayToken);
1768
1769 // --------------------------------------------------------------------
1770 // Cleanup conditions
1771
1772 EXPECT_CALL(*mComposer, destroyVirtualDisplay(Case::Display::HWC_DISPLAY_ID))
1773 .WillOnce(Return(Error::NONE));
1774 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1775}
1776
1777TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAddedWithNoSurface) {
1778 using Case = HwcVirtualDisplayCase;
1779
1780 // --------------------------------------------------------------------
1781 // Preconditions
1782
1783 // The HWC supports at least one virtual display
1784 injectMockComposer(1);
1785
1786 setupCommonPreconditions<Case>();
1787
1788 // A virtual display was added to the current state, but it does not have a
1789 // surface.
1790 sp<BBinder> displayToken = new BBinder();
1791
Dominik Laskowski075d3172018-05-24 15:50:06 -07001792 DisplayDeviceState state;
1793 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001794
Dominik Laskowski075d3172018-05-24 15:50:06 -07001795 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001796
1797 // --------------------------------------------------------------------
1798 // Call Expectations
1799
1800 // --------------------------------------------------------------------
1801 // Invocation
1802
1803 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1804
1805 // --------------------------------------------------------------------
1806 // Postconditions
1807
1808 // There will not be a display device set up.
1809 EXPECT_FALSE(hasDisplayDevice(displayToken));
1810
1811 // The drawing display state will be set from the current display state.
1812 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1813 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001814 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001815}
1816
1817TEST_F(HandleTransactionLockedTest, processesVirtualDisplayRemoval) {
1818 using Case = HwcVirtualDisplayCase;
1819
1820 // --------------------------------------------------------------------
1821 // Preconditions
1822
1823 // A virtual display is set up but is removed from the current state.
Dominik Laskowski075d3172018-05-24 15:50:06 -07001824 const auto displayId = Case::Display::DISPLAY_ID::get();
1825 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08001826 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001827 Case::Display::injectHwcDisplay(this);
1828 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1829 existing.inject();
1830 mFlinger.mutableCurrentState().displays.removeItem(existing.token());
1831
1832 // --------------------------------------------------------------------
1833 // Call Expectations
1834
1835 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1836
1837 // --------------------------------------------------------------------
1838 // Invocation
1839
1840 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1841
1842 // --------------------------------------------------------------------
1843 // Postconditions
1844
1845 // The existing token should have been removed
1846 verifyDisplayIsNotConnected(existing.token());
1847}
1848
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001849TEST_F(HandleTransactionLockedTest, processesDisplayLayerStackChanges) {
1850 using Case = NonHwcVirtualDisplayCase;
1851
1852 constexpr uint32_t oldLayerStack = 0u;
1853 constexpr uint32_t newLayerStack = 123u;
1854
1855 // --------------------------------------------------------------------
1856 // Preconditions
1857
1858 // A display is set up
1859 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1860 display.inject();
1861
1862 // There is a change to the layerStack state
1863 display.mutableDrawingDisplayState().layerStack = oldLayerStack;
1864 display.mutableCurrentDisplayState().layerStack = newLayerStack;
1865
1866 // --------------------------------------------------------------------
1867 // Invocation
1868
1869 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1870
1871 // --------------------------------------------------------------------
1872 // Postconditions
1873
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001874 EXPECT_EQ(newLayerStack, display.mutableDisplayDevice()->getLayerStack());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001875}
1876
1877TEST_F(HandleTransactionLockedTest, processesDisplayTransformChanges) {
1878 using Case = NonHwcVirtualDisplayCase;
1879
1880 constexpr int oldTransform = 0;
1881 constexpr int newTransform = 2;
1882
1883 // --------------------------------------------------------------------
1884 // Preconditions
1885
1886 // A display is set up
1887 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1888 display.inject();
1889
1890 // There is a change to the orientation state
1891 display.mutableDrawingDisplayState().orientation = oldTransform;
1892 display.mutableCurrentDisplayState().orientation = newTransform;
1893
1894 // --------------------------------------------------------------------
1895 // Invocation
1896
1897 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1898
1899 // --------------------------------------------------------------------
1900 // Postconditions
1901
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001902 EXPECT_EQ(newTransform, display.mutableDisplayDevice()->getOrientation());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001903}
1904
1905TEST_F(HandleTransactionLockedTest, processesDisplayViewportChanges) {
1906 using Case = NonHwcVirtualDisplayCase;
1907
1908 const Rect oldViewport(0, 0, 0, 0);
1909 const Rect newViewport(0, 0, 123, 456);
1910
1911 // --------------------------------------------------------------------
1912 // Preconditions
1913
1914 // A display is set up
1915 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1916 display.inject();
1917
1918 // There is a change to the viewport state
1919 display.mutableDrawingDisplayState().viewport = oldViewport;
1920 display.mutableCurrentDisplayState().viewport = newViewport;
1921
1922 // --------------------------------------------------------------------
1923 // Invocation
1924
1925 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1926
1927 // --------------------------------------------------------------------
1928 // Postconditions
1929
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001930 EXPECT_EQ(newViewport, display.mutableDisplayDevice()->getViewport());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001931}
1932
1933TEST_F(HandleTransactionLockedTest, processesDisplayFrameChanges) {
1934 using Case = NonHwcVirtualDisplayCase;
1935
1936 const Rect oldFrame(0, 0, 0, 0);
1937 const Rect newFrame(0, 0, 123, 456);
1938
1939 // --------------------------------------------------------------------
1940 // Preconditions
1941
1942 // A display is set up
1943 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1944 display.inject();
1945
1946 // There is a change to the viewport state
1947 display.mutableDrawingDisplayState().frame = oldFrame;
1948 display.mutableCurrentDisplayState().frame = newFrame;
1949
1950 // --------------------------------------------------------------------
1951 // Invocation
1952
1953 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1954
1955 // --------------------------------------------------------------------
1956 // Postconditions
1957
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001958 EXPECT_EQ(newFrame, display.mutableDisplayDevice()->getFrame());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001959}
1960
1961TEST_F(HandleTransactionLockedTest, processesDisplayWidthChanges) {
1962 using Case = NonHwcVirtualDisplayCase;
1963
1964 constexpr int oldWidth = 0;
1965 constexpr int oldHeight = 10;
1966 constexpr int newWidth = 123;
1967
1968 // --------------------------------------------------------------------
1969 // Preconditions
1970
1971 // A display is set up
1972 auto nativeWindow = new mock::NativeWindow();
1973 auto displaySurface = new mock::DisplaySurface();
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001974 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001975 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1976 display.setNativeWindow(nativeWindow);
1977 display.setDisplaySurface(displaySurface);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001978 // Setup injection expections
1979 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
1980 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
1981 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
1982 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
Alec Mouri4efb6c22018-11-16 13:07:47 -08001983 EXPECT_CALL(*nativeWindow, perform(9)).Times(1);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001984 EXPECT_CALL(*nativeWindow, perform(13)).Times(1);
Alec Mouri4efb6c22018-11-16 13:07:47 -08001985 EXPECT_CALL(*nativeWindow, perform(30)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001986 display.inject();
1987
1988 // There is a change to the viewport state
1989 display.mutableDrawingDisplayState().width = oldWidth;
1990 display.mutableDrawingDisplayState().height = oldHeight;
1991 display.mutableCurrentDisplayState().width = newWidth;
1992 display.mutableCurrentDisplayState().height = oldHeight;
1993
1994 // --------------------------------------------------------------------
1995 // Call Expectations
1996
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001997 EXPECT_CALL(*displaySurface, resizeBuffers(newWidth, oldHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001998
1999 // --------------------------------------------------------------------
2000 // Invocation
2001
2002 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2003}
2004
2005TEST_F(HandleTransactionLockedTest, processesDisplayHeightChanges) {
2006 using Case = NonHwcVirtualDisplayCase;
2007
2008 constexpr int oldWidth = 0;
2009 constexpr int oldHeight = 10;
2010 constexpr int newHeight = 123;
2011
2012 // --------------------------------------------------------------------
2013 // Preconditions
2014
2015 // A display is set up
2016 auto nativeWindow = new mock::NativeWindow();
2017 auto displaySurface = new mock::DisplaySurface();
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002018 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002019 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2020 display.setNativeWindow(nativeWindow);
2021 display.setDisplaySurface(displaySurface);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002022 // Setup injection expections
2023 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
2024 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
2025 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
2026 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
Alec Mouri4efb6c22018-11-16 13:07:47 -08002027 EXPECT_CALL(*nativeWindow, perform(9)).Times(1);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002028 EXPECT_CALL(*nativeWindow, perform(13)).Times(1);
Alec Mouri4efb6c22018-11-16 13:07:47 -08002029 EXPECT_CALL(*nativeWindow, perform(30)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002030 display.inject();
2031
2032 // There is a change to the viewport state
2033 display.mutableDrawingDisplayState().width = oldWidth;
2034 display.mutableDrawingDisplayState().height = oldHeight;
2035 display.mutableCurrentDisplayState().width = oldWidth;
2036 display.mutableCurrentDisplayState().height = newHeight;
2037
2038 // --------------------------------------------------------------------
2039 // Call Expectations
2040
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002041 EXPECT_CALL(*displaySurface, resizeBuffers(oldWidth, newHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002042
2043 // --------------------------------------------------------------------
2044 // Invocation
2045
2046 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2047}
2048
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002049/* ------------------------------------------------------------------------
2050 * SurfaceFlinger::setDisplayStateLocked
2051 */
2052
2053TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithUnknownDisplay) {
2054 // --------------------------------------------------------------------
2055 // Preconditions
2056
2057 // We have an unknown display token not associated with a known display
2058 sp<BBinder> displayToken = new BBinder();
2059
2060 // The requested display state references the unknown display.
2061 DisplayState state;
2062 state.what = DisplayState::eLayerStackChanged;
2063 state.token = displayToken;
2064 state.layerStack = 456;
2065
2066 // --------------------------------------------------------------------
2067 // Invocation
2068
2069 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2070
2071 // --------------------------------------------------------------------
2072 // Postconditions
2073
2074 // The returned flags are empty
2075 EXPECT_EQ(0u, flags);
2076
2077 // The display token still doesn't match anything known.
2078 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
2079}
2080
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002081TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWhenNoChanges) {
2082 using Case = SimplePrimaryDisplayCase;
2083
2084 // --------------------------------------------------------------------
2085 // Preconditions
2086
2087 // A display is already set up
2088 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2089 display.inject();
2090
2091 // No changes are made to the display
2092 DisplayState state;
2093 state.what = 0;
2094 state.token = display.token();
2095
2096 // --------------------------------------------------------------------
2097 // Invocation
2098
2099 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2100
2101 // --------------------------------------------------------------------
2102 // Postconditions
2103
2104 // The returned flags are empty
2105 EXPECT_EQ(0u, flags);
2106}
2107
2108TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSurfaceDidNotChange) {
2109 using Case = SimplePrimaryDisplayCase;
2110
2111 // --------------------------------------------------------------------
2112 // Preconditions
2113
2114 // A display is already set up
2115 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2116 display.inject();
2117
2118 // There is a surface that can be set.
2119 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2120
2121 // The current display state has the surface set
2122 display.mutableCurrentDisplayState().surface = surface;
2123
2124 // The incoming request sets the same surface
2125 DisplayState state;
2126 state.what = DisplayState::eSurfaceChanged;
2127 state.token = display.token();
2128 state.surface = surface;
2129
2130 // --------------------------------------------------------------------
2131 // Invocation
2132
2133 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2134
2135 // --------------------------------------------------------------------
2136 // Postconditions
2137
2138 // The returned flags are empty
2139 EXPECT_EQ(0u, flags);
2140
2141 // The current display state is unchanged.
2142 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2143}
2144
2145TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfSurfaceChanged) {
2146 using Case = SimplePrimaryDisplayCase;
2147
2148 // --------------------------------------------------------------------
2149 // Preconditions
2150
2151 // A display is already set up
2152 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2153 display.inject();
2154
2155 // There is a surface that can be set.
2156 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2157
2158 // The current display state does not have a surface
2159 display.mutableCurrentDisplayState().surface = nullptr;
2160
2161 // The incoming request sets a surface
2162 DisplayState state;
2163 state.what = DisplayState::eSurfaceChanged;
2164 state.token = display.token();
2165 state.surface = surface;
2166
2167 // --------------------------------------------------------------------
2168 // Invocation
2169
2170 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2171
2172 // --------------------------------------------------------------------
2173 // Postconditions
2174
2175 // The returned flags indicate a transaction is needed
2176 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2177
2178 // The current display layer stack state is set to the new value
2179 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2180}
2181
2182TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfLayerStackDidNotChange) {
2183 using Case = SimplePrimaryDisplayCase;
2184
2185 // --------------------------------------------------------------------
2186 // Preconditions
2187
2188 // A display is already set up
2189 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2190 display.inject();
2191
2192 // The display has a layer stack set
2193 display.mutableCurrentDisplayState().layerStack = 456u;
2194
2195 // The incoming request sets the same layer stack
2196 DisplayState state;
2197 state.what = DisplayState::eLayerStackChanged;
2198 state.token = display.token();
2199 state.layerStack = 456u;
2200
2201 // --------------------------------------------------------------------
2202 // Invocation
2203
2204 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2205
2206 // --------------------------------------------------------------------
2207 // Postconditions
2208
2209 // The returned flags are empty
2210 EXPECT_EQ(0u, flags);
2211
2212 // The current display state is unchanged
2213 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2214}
2215
2216TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfLayerStackChanged) {
2217 using Case = SimplePrimaryDisplayCase;
2218
2219 // --------------------------------------------------------------------
2220 // Preconditions
2221
2222 // A display is set up
2223 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2224 display.inject();
2225
2226 // The display has a layer stack set
2227 display.mutableCurrentDisplayState().layerStack = 654u;
2228
2229 // The incoming request sets a different layer stack
2230 DisplayState state;
2231 state.what = DisplayState::eLayerStackChanged;
2232 state.token = display.token();
2233 state.layerStack = 456u;
2234
2235 // --------------------------------------------------------------------
2236 // Invocation
2237
2238 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2239
2240 // --------------------------------------------------------------------
2241 // Postconditions
2242
2243 // The returned flags indicate a transaction is needed
2244 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2245
2246 // The desired display state has been set to the new value.
2247 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2248}
2249
2250TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfProjectionDidNotChange) {
2251 using Case = SimplePrimaryDisplayCase;
2252 constexpr int initialOrientation = 180;
2253 const Rect initialFrame = {1, 2, 3, 4};
2254 const Rect initialViewport = {5, 6, 7, 8};
2255
2256 // --------------------------------------------------------------------
2257 // Preconditions
2258
2259 // A display is set up
2260 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2261 display.inject();
2262
2263 // The current display state projection state is all set
2264 display.mutableCurrentDisplayState().orientation = initialOrientation;
2265 display.mutableCurrentDisplayState().frame = initialFrame;
2266 display.mutableCurrentDisplayState().viewport = initialViewport;
2267
2268 // The incoming request sets the same projection state
2269 DisplayState state;
2270 state.what = DisplayState::eDisplayProjectionChanged;
2271 state.token = display.token();
2272 state.orientation = initialOrientation;
2273 state.frame = initialFrame;
2274 state.viewport = initialViewport;
2275
2276 // --------------------------------------------------------------------
2277 // Invocation
2278
2279 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2280
2281 // --------------------------------------------------------------------
2282 // Postconditions
2283
2284 // The returned flags are empty
2285 EXPECT_EQ(0u, flags);
2286
2287 // The current display state is unchanged
2288 EXPECT_EQ(initialOrientation, display.getCurrentDisplayState().orientation);
2289
2290 EXPECT_EQ(initialFrame, display.getCurrentDisplayState().frame);
2291 EXPECT_EQ(initialViewport, display.getCurrentDisplayState().viewport);
2292}
2293
2294TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfOrientationChanged) {
2295 using Case = SimplePrimaryDisplayCase;
2296 constexpr int initialOrientation = 90;
2297 constexpr int desiredOrientation = 180;
2298
2299 // --------------------------------------------------------------------
2300 // Preconditions
2301
2302 // A display is set up
2303 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2304 display.inject();
2305
2306 // The current display state has an orientation set
2307 display.mutableCurrentDisplayState().orientation = initialOrientation;
2308
2309 // The incoming request sets a different orientation
2310 DisplayState state;
2311 state.what = DisplayState::eDisplayProjectionChanged;
2312 state.token = display.token();
2313 state.orientation = desiredOrientation;
2314
2315 // --------------------------------------------------------------------
2316 // Invocation
2317
2318 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2319
2320 // --------------------------------------------------------------------
2321 // Postconditions
2322
2323 // The returned flags indicate a transaction is needed
2324 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2325
2326 // The current display state has the new value.
2327 EXPECT_EQ(desiredOrientation, display.getCurrentDisplayState().orientation);
2328}
2329
2330TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfFrameChanged) {
2331 using Case = SimplePrimaryDisplayCase;
2332 const Rect initialFrame = {0, 0, 0, 0};
2333 const Rect desiredFrame = {5, 6, 7, 8};
2334
2335 // --------------------------------------------------------------------
2336 // Preconditions
2337
2338 // A display is set up
2339 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2340 display.inject();
2341
2342 // The current display state does not have a frame
2343 display.mutableCurrentDisplayState().frame = initialFrame;
2344
2345 // The incoming request sets a frame
2346 DisplayState state;
2347 state.what = DisplayState::eDisplayProjectionChanged;
2348 state.token = display.token();
2349 state.frame = desiredFrame;
2350
2351 // --------------------------------------------------------------------
2352 // Invocation
2353
2354 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2355
2356 // --------------------------------------------------------------------
2357 // Postconditions
2358
2359 // The returned flags indicate a transaction is needed
2360 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2361
2362 // The current display state has the new value.
2363 EXPECT_EQ(desiredFrame, display.getCurrentDisplayState().frame);
2364}
2365
2366TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfViewportChanged) {
2367 using Case = SimplePrimaryDisplayCase;
2368 const Rect initialViewport = {0, 0, 0, 0};
2369 const Rect desiredViewport = {5, 6, 7, 8};
2370
2371 // --------------------------------------------------------------------
2372 // Preconditions
2373
2374 // A display is set up
2375 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2376 display.inject();
2377
2378 // The current display state does not have a viewport
2379 display.mutableCurrentDisplayState().viewport = initialViewport;
2380
2381 // The incoming request sets a viewport
2382 DisplayState state;
2383 state.what = DisplayState::eDisplayProjectionChanged;
2384 state.token = display.token();
2385 state.viewport = desiredViewport;
2386
2387 // --------------------------------------------------------------------
2388 // Invocation
2389
2390 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2391
2392 // --------------------------------------------------------------------
2393 // Postconditions
2394
2395 // The returned flags indicate a transaction is needed
2396 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2397
2398 // The current display state has the new value.
2399 EXPECT_EQ(desiredViewport, display.getCurrentDisplayState().viewport);
2400}
2401
2402TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSizeDidNotChange) {
2403 using Case = SimplePrimaryDisplayCase;
2404 constexpr uint32_t initialWidth = 1024;
2405 constexpr uint32_t initialHeight = 768;
2406
2407 // --------------------------------------------------------------------
2408 // Preconditions
2409
2410 // A display is set up
2411 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2412 display.inject();
2413
2414 // The current display state has a size set
2415 display.mutableCurrentDisplayState().width = initialWidth;
2416 display.mutableCurrentDisplayState().height = initialHeight;
2417
2418 // The incoming request sets the same display size
2419 DisplayState state;
2420 state.what = DisplayState::eDisplaySizeChanged;
2421 state.token = display.token();
2422 state.width = initialWidth;
2423 state.height = initialHeight;
2424
2425 // --------------------------------------------------------------------
2426 // Invocation
2427
2428 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2429
2430 // --------------------------------------------------------------------
2431 // Postconditions
2432
2433 // The returned flags are empty
2434 EXPECT_EQ(0u, flags);
2435
2436 // The current display state is unchanged
2437 EXPECT_EQ(initialWidth, display.getCurrentDisplayState().width);
2438 EXPECT_EQ(initialHeight, display.getCurrentDisplayState().height);
2439}
2440
2441TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfWidthChanged) {
2442 using Case = SimplePrimaryDisplayCase;
2443 constexpr uint32_t initialWidth = 0;
2444 constexpr uint32_t desiredWidth = 1024;
2445
2446 // --------------------------------------------------------------------
2447 // Preconditions
2448
2449 // A display is set up
2450 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2451 display.inject();
2452
2453 // The display does not yet have a width
2454 display.mutableCurrentDisplayState().width = initialWidth;
2455
2456 // The incoming request sets a display width
2457 DisplayState state;
2458 state.what = DisplayState::eDisplaySizeChanged;
2459 state.token = display.token();
2460 state.width = desiredWidth;
2461
2462 // --------------------------------------------------------------------
2463 // Invocation
2464
2465 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2466
2467 // --------------------------------------------------------------------
2468 // Postconditions
2469
2470 // The returned flags indicate a transaction is needed
2471 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2472
2473 // The current display state has the new value.
2474 EXPECT_EQ(desiredWidth, display.getCurrentDisplayState().width);
2475}
2476
2477TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfHeightChanged) {
2478 using Case = SimplePrimaryDisplayCase;
2479 constexpr uint32_t initialHeight = 0;
2480 constexpr uint32_t desiredHeight = 768;
2481
2482 // --------------------------------------------------------------------
2483 // Preconditions
2484
2485 // A display is set up
2486 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2487 display.inject();
2488
2489 // The display does not yet have a height
2490 display.mutableCurrentDisplayState().height = initialHeight;
2491
2492 // The incoming request sets a display height
2493 DisplayState state;
2494 state.what = DisplayState::eDisplaySizeChanged;
2495 state.token = display.token();
2496 state.height = desiredHeight;
2497
2498 // --------------------------------------------------------------------
2499 // Invocation
2500
2501 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2502
2503 // --------------------------------------------------------------------
2504 // Postconditions
2505
2506 // The returned flags indicate a transaction is needed
2507 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2508
2509 // The current display state has the new value.
2510 EXPECT_EQ(desiredHeight, display.getCurrentDisplayState().height);
2511}
2512
Lloyd Pique86016da2018-03-01 16:09:38 -08002513/* ------------------------------------------------------------------------
2514 * SurfaceFlinger::onInitializeDisplays
2515 */
2516
2517TEST_F(DisplayTransactionTest, onInitializeDisplaysSetsUpPrimaryDisplay) {
2518 using Case = SimplePrimaryDisplayCase;
2519
2520 // --------------------------------------------------------------------
2521 // Preconditions
2522
2523 // A primary display is set up
2524 Case::Display::injectHwcDisplay(this);
2525 auto primaryDisplay = Case::Display::makeFakeExistingDisplayInjector(this);
2526 primaryDisplay.inject();
2527
2528 // --------------------------------------------------------------------
2529 // Call Expectations
2530
2531 // We expect the surface interceptor to possibly be used, but we treat it as
2532 // disabled since it is called as a side effect rather than directly by this
2533 // function.
2534 EXPECT_CALL(*mSurfaceInterceptor, isEnabled()).WillOnce(Return(false));
2535
2536 // We expect a call to get the active display config.
2537 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
2538
2539 // We expect invalidate() to be invoked once to trigger display transaction
2540 // processing.
2541 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
2542
2543 // --------------------------------------------------------------------
2544 // Invocation
2545
2546 mFlinger.onInitializeDisplays();
2547
2548 // --------------------------------------------------------------------
2549 // Postconditions
2550
2551 // The primary display should have a current state
2552 ASSERT_TRUE(hasCurrentDisplayState(primaryDisplay.token()));
2553 const auto& primaryDisplayState = getCurrentDisplayState(primaryDisplay.token());
2554 // The layer stack state should be set to zero
2555 EXPECT_EQ(0u, primaryDisplayState.layerStack);
2556 // The orientation state should be set to zero
2557 EXPECT_EQ(0, primaryDisplayState.orientation);
2558
2559 // The frame state should be set to INVALID
2560 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.frame);
2561
2562 // The viewport state should be set to INVALID
2563 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.viewport);
2564
2565 // The width and height should both be zero
2566 EXPECT_EQ(0u, primaryDisplayState.width);
2567 EXPECT_EQ(0u, primaryDisplayState.height);
2568
2569 // The display should be set to HWC_POWER_MODE_NORMAL
2570 ASSERT_TRUE(hasDisplayDevice(primaryDisplay.token()));
2571 auto displayDevice = primaryDisplay.mutableDisplayDevice();
2572 EXPECT_EQ(HWC_POWER_MODE_NORMAL, displayDevice->getPowerMode());
2573
2574 // The display refresh period should be set in the frame tracker.
2575 FrameStats stats;
2576 mFlinger.getAnimFrameTracker().getStats(&stats);
2577 EXPECT_EQ(DEFAULT_REFRESH_RATE, stats.refreshPeriodNano);
2578
2579 // The display transaction needed flag should be set.
2580 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
2581
2582 // The compositor timing should be set to default values
2583 const auto& compositorTiming = mFlinger.getCompositorTiming();
2584 EXPECT_EQ(-DEFAULT_REFRESH_RATE, compositorTiming.deadline);
2585 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.interval);
2586 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.presentLatency);
2587}
2588
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002589/* ------------------------------------------------------------------------
2590 * SurfaceFlinger::setPowerModeInternal
2591 */
2592
2593// Used when we simulate a display that supports doze.
Peiyong Lined531a32018-10-26 18:27:56 -07002594template <typename Display>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002595struct DozeIsSupportedVariant {
2596 static constexpr bool DOZE_SUPPORTED = true;
2597 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2598 IComposerClient::PowerMode::DOZE;
2599 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2600 IComposerClient::PowerMode::DOZE_SUSPEND;
Peiyong Lined531a32018-10-26 18:27:56 -07002601
2602 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
2603 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(Display::HWC_DISPLAY_ID, _))
2604 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hwc2::DisplayCapability>(
2605 {Hwc2::DisplayCapability::DOZE})),
2606 Return(Error::NONE)));
2607 }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002608};
2609
Peiyong Lined531a32018-10-26 18:27:56 -07002610template <typename Display>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002611// Used when we simulate a display that does not support doze.
2612struct DozeNotSupportedVariant {
2613 static constexpr bool DOZE_SUPPORTED = false;
2614 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2615 IComposerClient::PowerMode::ON;
2616 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2617 IComposerClient::PowerMode::ON;
Peiyong Lined531a32018-10-26 18:27:56 -07002618
2619 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
2620 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(Display::HWC_DISPLAY_ID, _))
2621 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hwc2::DisplayCapability>({})),
2622 Return(Error::NONE)));
2623 }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002624};
2625
2626struct EventThreadBaseSupportedVariant {
2627 static void setupEventAndEventControlThreadNoCallExpectations(DisplayTransactionTest* test) {
2628 // The event control thread should not be notified.
2629 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(_)).Times(0);
2630
2631 // The event thread should not be notified.
2632 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(0);
2633 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(0);
2634 }
2635};
2636
2637struct EventThreadNotSupportedVariant : public EventThreadBaseSupportedVariant {
2638 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2639 // These calls are only expected for the primary display.
2640
2641 // Instead expect no calls.
2642 setupEventAndEventControlThreadNoCallExpectations(test);
2643 }
2644
2645 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2646 // These calls are only expected for the primary display.
2647
2648 // Instead expect no calls.
2649 setupEventAndEventControlThreadNoCallExpectations(test);
2650 }
2651};
2652
2653struct EventThreadIsSupportedVariant : public EventThreadBaseSupportedVariant {
2654 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2655 // The event control thread should be notified to enable vsyncs
2656 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(true)).Times(1);
2657
2658 // The event thread should be notified that the screen was acquired.
2659 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(1);
2660 }
2661
2662 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2663 // There should be a call to setVsyncEnabled(false)
2664 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(false)).Times(1);
2665
2666 // The event thread should not be notified that the screen was released.
2667 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(1);
2668 }
2669};
2670
Lloyd Pique41be5d22018-06-21 13:11:48 -07002671struct DispSyncIsSupportedVariant {
2672 static void setupBeginResyncCallExpectations(DisplayTransactionTest* test) {
2673 EXPECT_CALL(*test->mPrimaryDispSync, reset()).Times(1);
2674 EXPECT_CALL(*test->mPrimaryDispSync, setPeriod(DEFAULT_REFRESH_RATE)).Times(1);
2675 EXPECT_CALL(*test->mPrimaryDispSync, beginResync()).Times(1);
2676 }
2677
2678 static void setupEndResyncCallExpectations(DisplayTransactionTest* test) {
2679 EXPECT_CALL(*test->mPrimaryDispSync, endResync()).Times(1);
2680 }
2681};
2682
2683struct DispSyncNotSupportedVariant {
2684 static void setupBeginResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2685
2686 static void setupEndResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2687};
2688
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002689// --------------------------------------------------------------------
2690// Note:
2691//
2692// There are a large number of transitions we could test, however we only test a
2693// selected subset which provides complete test coverage of the implementation.
2694// --------------------------------------------------------------------
2695
2696template <int initialPowerMode, int targetPowerMode>
2697struct TransitionVariantCommon {
2698 static constexpr auto INITIAL_POWER_MODE = initialPowerMode;
2699 static constexpr auto TARGET_POWER_MODE = targetPowerMode;
2700
2701 static void verifyPostconditions(DisplayTransactionTest*) {}
2702};
2703
2704struct TransitionOffToOnVariant
2705 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_NORMAL> {
2706 template <typename Case>
2707 static void setupCallExpectations(DisplayTransactionTest* test) {
2708 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2709 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002710 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002711 Case::setupRepaintEverythingCallExpectations(test);
2712 }
2713
2714 static void verifyPostconditions(DisplayTransactionTest* test) {
2715 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2716 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2717 }
2718};
2719
2720struct TransitionOffToDozeSuspendVariant
2721 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_DOZE_SUSPEND> {
2722 template <typename Case>
2723 static void setupCallExpectations(DisplayTransactionTest* test) {
2724 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2725 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2726 Case::setupRepaintEverythingCallExpectations(test);
2727 }
2728
2729 static void verifyPostconditions(DisplayTransactionTest* test) {
2730 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2731 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2732 }
2733};
2734
2735struct TransitionOnToOffVariant
2736 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_OFF> {
2737 template <typename Case>
2738 static void setupCallExpectations(DisplayTransactionTest* test) {
2739 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002740 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002741 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2742 }
2743
2744 static void verifyPostconditions(DisplayTransactionTest* test) {
2745 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2746 }
2747};
2748
2749struct TransitionDozeSuspendToOffVariant
2750 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_OFF> {
2751 template <typename Case>
2752 static void setupCallExpectations(DisplayTransactionTest* test) {
2753 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2754 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2755 }
2756
2757 static void verifyPostconditions(DisplayTransactionTest* test) {
2758 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2759 }
2760};
2761
2762struct TransitionOnToDozeVariant
2763 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE> {
2764 template <typename Case>
2765 static void setupCallExpectations(DisplayTransactionTest* test) {
2766 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2767 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2768 }
2769};
2770
2771struct TransitionDozeSuspendToDozeVariant
2772 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_DOZE> {
2773 template <typename Case>
2774 static void setupCallExpectations(DisplayTransactionTest* test) {
2775 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002776 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002777 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2778 }
2779};
2780
2781struct TransitionDozeToOnVariant
2782 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE, HWC_POWER_MODE_NORMAL> {
2783 template <typename Case>
2784 static void setupCallExpectations(DisplayTransactionTest* test) {
2785 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2786 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2787 }
2788};
2789
2790struct TransitionDozeSuspendToOnVariant
2791 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_NORMAL> {
2792 template <typename Case>
2793 static void setupCallExpectations(DisplayTransactionTest* test) {
2794 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002795 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002796 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2797 }
2798};
2799
2800struct TransitionOnToDozeSuspendVariant
2801 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE_SUSPEND> {
2802 template <typename Case>
2803 static void setupCallExpectations(DisplayTransactionTest* test) {
2804 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002805 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002806 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2807 }
2808};
2809
2810struct TransitionOnToUnknownVariant
2811 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_LEET> {
2812 template <typename Case>
2813 static void setupCallExpectations(DisplayTransactionTest* test) {
2814 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2815 Case::setupNoComposerPowerModeCallExpectations(test);
2816 }
2817};
2818
2819// --------------------------------------------------------------------
2820// Note:
2821//
2822// Rather than testing the cartesian product of of
2823// DozeIsSupported/DozeNotSupported with all other options, we use one for one
2824// display type, and the other for another display type.
2825// --------------------------------------------------------------------
2826
2827template <typename DisplayVariant, typename DozeVariant, typename EventThreadVariant,
Lloyd Pique41be5d22018-06-21 13:11:48 -07002828 typename DispSyncVariant, typename TransitionVariant>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002829struct DisplayPowerCase {
2830 using Display = DisplayVariant;
2831 using Doze = DozeVariant;
2832 using EventThread = EventThreadVariant;
Lloyd Pique41be5d22018-06-21 13:11:48 -07002833 using DispSync = DispSyncVariant;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002834 using Transition = TransitionVariant;
2835
2836 static auto injectDisplayWithInitialPowerMode(DisplayTransactionTest* test, int mode) {
2837 Display::injectHwcDisplay(test);
2838 auto display = Display::makeFakeExistingDisplayInjector(test);
2839 display.inject();
2840 display.mutableDisplayDevice()->setPowerMode(mode);
2841 return display;
2842 }
2843
2844 static void setInitialPrimaryHWVsyncEnabled(DisplayTransactionTest* test, bool enabled) {
2845 test->mFlinger.mutablePrimaryHWVsyncEnabled() = enabled;
2846 }
2847
2848 static void setupRepaintEverythingCallExpectations(DisplayTransactionTest* test) {
2849 EXPECT_CALL(*test->mMessageQueue, invalidate()).Times(1);
2850 }
2851
2852 static void setupSurfaceInterceptorCallExpectations(DisplayTransactionTest* test, int mode) {
2853 EXPECT_CALL(*test->mSurfaceInterceptor, isEnabled()).WillOnce(Return(true));
2854 EXPECT_CALL(*test->mSurfaceInterceptor, savePowerModeUpdate(_, mode)).Times(1);
2855 }
2856
2857 static void setupComposerCallExpectations(DisplayTransactionTest* test,
2858 IComposerClient::PowerMode mode) {
2859 // Any calls to get the active config will return a default value.
2860 EXPECT_CALL(*test->mComposer, getActiveConfig(Display::HWC_DISPLAY_ID, _))
2861 .WillRepeatedly(DoAll(SetArgPointee<1>(Display::HWC_ACTIVE_CONFIG_ID),
2862 Return(Error::NONE)));
2863
2864 // Any calls to get whether the display supports dozing will return the value set by the
2865 // policy variant.
2866 EXPECT_CALL(*test->mComposer, getDozeSupport(Display::HWC_DISPLAY_ID, _))
2867 .WillRepeatedly(DoAll(SetArgPointee<1>(Doze::DOZE_SUPPORTED), Return(Error::NONE)));
2868
2869 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, mode)).Times(1);
2870 }
2871
2872 static void setupNoComposerPowerModeCallExpectations(DisplayTransactionTest* test) {
2873 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, _)).Times(0);
2874 }
2875};
2876
2877// A sample configuration for the primary display.
2878// In addition to having event thread support, we emulate doze support.
2879template <typename TransitionVariant>
Peiyong Lined531a32018-10-26 18:27:56 -07002880using PrimaryDisplayPowerCase =
2881 DisplayPowerCase<PrimaryDisplayVariant, DozeIsSupportedVariant<PrimaryDisplayVariant>,
2882 EventThreadIsSupportedVariant, DispSyncIsSupportedVariant,
2883 TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002884
2885// A sample configuration for the external display.
2886// In addition to not having event thread support, we emulate not having doze
2887// support.
2888template <typename TransitionVariant>
Peiyong Lined531a32018-10-26 18:27:56 -07002889using ExternalDisplayPowerCase =
2890 DisplayPowerCase<ExternalDisplayVariant, DozeNotSupportedVariant<ExternalDisplayVariant>,
2891 EventThreadNotSupportedVariant, DispSyncNotSupportedVariant,
2892 TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002893
2894class SetPowerModeInternalTest : public DisplayTransactionTest {
2895public:
2896 template <typename Case>
2897 void transitionDisplayCommon();
2898};
2899
2900template <int PowerMode>
2901struct PowerModeInitialVSyncEnabled : public std::false_type {};
2902
2903template <>
2904struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_NORMAL> : public std::true_type {};
2905
2906template <>
2907struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_DOZE> : public std::true_type {};
2908
2909template <typename Case>
2910void SetPowerModeInternalTest::transitionDisplayCommon() {
2911 // --------------------------------------------------------------------
2912 // Preconditions
2913
Peiyong Lined531a32018-10-26 18:27:56 -07002914 Case::Doze::setupComposerCallExpectations(this);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002915 auto display =
2916 Case::injectDisplayWithInitialPowerMode(this, Case::Transition::INITIAL_POWER_MODE);
2917 Case::setInitialPrimaryHWVsyncEnabled(this,
2918 PowerModeInitialVSyncEnabled<
2919 Case::Transition::INITIAL_POWER_MODE>::value);
2920
2921 // --------------------------------------------------------------------
2922 // Call Expectations
2923
2924 Case::setupSurfaceInterceptorCallExpectations(this, Case::Transition::TARGET_POWER_MODE);
2925 Case::Transition::template setupCallExpectations<Case>(this);
2926
2927 // --------------------------------------------------------------------
2928 // Invocation
2929
2930 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(),
2931 Case::Transition::TARGET_POWER_MODE);
2932
2933 // --------------------------------------------------------------------
2934 // Postconditions
2935
2936 Case::Transition::verifyPostconditions(this);
2937}
2938
2939TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfNoChange) {
2940 using Case = SimplePrimaryDisplayCase;
2941
2942 // --------------------------------------------------------------------
2943 // Preconditions
2944
2945 // A primary display device is set up
2946 Case::Display::injectHwcDisplay(this);
2947 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2948 display.inject();
2949
Dominik Laskowskia2edf612018-06-01 13:15:16 -07002950 // The display is already set to HWC_POWER_MODE_NORMAL
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002951 display.mutableDisplayDevice()->setPowerMode(HWC_POWER_MODE_NORMAL);
2952
2953 // --------------------------------------------------------------------
2954 // Invocation
2955
2956 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_NORMAL);
2957
2958 // --------------------------------------------------------------------
2959 // Postconditions
2960
2961 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2962}
2963
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002964TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfVirtualDisplay) {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002965 using Case = HwcVirtualDisplayCase;
2966
2967 // --------------------------------------------------------------------
2968 // Preconditions
2969
Dominik Laskowski075d3172018-05-24 15:50:06 -07002970 // Insert display data so that the HWC thinks it created the virtual display.
2971 const auto displayId = Case::Display::DISPLAY_ID::get();
2972 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08002973 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002974
2975 // A virtual display device is set up
2976 Case::Display::injectHwcDisplay(this);
2977 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2978 display.inject();
2979
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002980 // The display is set to HWC_POWER_MODE_NORMAL
2981 getDisplayDevice(display.token())->setPowerMode(HWC_POWER_MODE_NORMAL);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002982
2983 // --------------------------------------------------------------------
2984 // Invocation
2985
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002986 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_OFF);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002987
2988 // --------------------------------------------------------------------
2989 // Postconditions
2990
2991 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2992}
2993
2994TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnPrimaryDisplay) {
2995 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToOnVariant>>();
2996}
2997
2998TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendPrimaryDisplay) {
2999 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
3000}
3001
3002TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffPrimaryDisplay) {
3003 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToOffVariant>>();
3004}
3005
3006TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffPrimaryDisplay) {
3007 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
3008}
3009
3010TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozePrimaryDisplay) {
3011 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeVariant>>();
3012}
3013
3014TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozePrimaryDisplay) {
3015 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
3016}
3017
3018TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnPrimaryDisplay) {
3019 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeToOnVariant>>();
3020}
3021
3022TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnPrimaryDisplay) {
3023 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
3024}
3025
3026TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendPrimaryDisplay) {
3027 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
3028}
3029
3030TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownPrimaryDisplay) {
3031 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToUnknownVariant>>();
3032}
3033
3034TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnExternalDisplay) {
3035 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToOnVariant>>();
3036}
3037
3038TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendExternalDisplay) {
3039 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
3040}
3041
3042TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffExternalDisplay) {
3043 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToOffVariant>>();
3044}
3045
3046TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffExternalDisplay) {
3047 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
3048}
3049
3050TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeExternalDisplay) {
3051 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeVariant>>();
3052}
3053
3054TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozeExternalDisplay) {
3055 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
3056}
3057
3058TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnExternalDisplay) {
3059 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeToOnVariant>>();
3060}
3061
3062TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnExternalDisplay) {
3063 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
3064}
3065
3066TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendExternalDisplay) {
3067 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
3068}
3069
3070TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownExternalDisplay) {
3071 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToUnknownVariant>>();
3072}
3073
Lloyd Piquef58625d2017-12-19 13:22:33 -08003074} // namespace
3075} // namespace android