blob: de52242c54f83e98028d00b39159949d35881e7a [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 {
608 static constexpr bool HDR10_SUPPORTED = false;
609 static constexpr bool HDR_HLG_SUPPORTED = false;
610 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
611 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
612 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
613 }
614};
615
616// For this variant, the composer should respond with a non-empty list of HDR
617// modes containing HDR10, so HDR10 support should be configured.
618template <typename Display>
619struct Hdr10SupportedVariant {
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(Display::HWC_DISPLAY_ID, _, _, _, _))
625 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HDR10})),
626 Return(Error::NONE)));
627 }
628};
629
630// For this variant, the composer should respond with a non-empty list of HDR
631// modes containing HLG, so HLG support should be configured.
632template <typename Display>
633struct HdrHlgSupportedVariant {
634 static constexpr bool HDR10_SUPPORTED = false;
635 static constexpr bool HDR_HLG_SUPPORTED = true;
636 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
637 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
638 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
639 .WillOnce(
640 DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HLG})), Return(Error::NONE)));
641 }
642};
643
644// For this variant, the composer should respond with a non-empty list of HDR
645// modes containing DOLBY_VISION, so DOLBY_VISION support should be configured.
646template <typename Display>
647struct HdrDolbyVisionSupportedVariant {
648 static constexpr bool HDR10_SUPPORTED = false;
649 static constexpr bool HDR_HLG_SUPPORTED = false;
650 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = true;
651 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
652 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
653 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::DOLBY_VISION})),
654 Return(Error::NONE)));
655 }
656};
657
658// For this variant, the composer should respond with am empty list of HDR
659// modes, so no HDR support should be configured.
660template <typename Display>
661struct HdrNotSupportedVariant {
662 static constexpr bool HDR10_SUPPORTED = false;
663 static constexpr bool HDR_HLG_SUPPORTED = false;
664 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
665 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
666 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
667 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
668 }
669};
670
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700671struct NonHwcPerFrameMetadataSupportVariant {
672 static constexpr int PER_FRAME_METADATA_KEYS = 0;
673 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800674 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(_)).Times(0);
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700675 }
676};
677
678template <typename Display>
679struct NoPerFrameMetadataSupportVariant {
680 static constexpr int PER_FRAME_METADATA_KEYS = 0;
681 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800682 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
683 .WillOnce(Return(std::vector<PerFrameMetadataKey>()));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700684 }
685};
686
687template <typename Display>
688struct Smpte2086PerFrameMetadataSupportVariant {
689 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::SMPTE2086;
690 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800691 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
692 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700693 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X,
694 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y,
695 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X,
696 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y,
697 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X,
698 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y,
699 PerFrameMetadataKey::WHITE_POINT_X,
700 PerFrameMetadataKey::WHITE_POINT_Y,
701 PerFrameMetadataKey::MAX_LUMINANCE,
702 PerFrameMetadataKey::MIN_LUMINANCE,
Chia-I Wud7e01d72018-06-21 13:39:09 +0800703 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700704 }
705};
706
707template <typename Display>
708struct Cta861_3_PerFrameMetadataSupportVariant {
709 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::CTA861_3;
710 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800711 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
712 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700713 PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL,
714 PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL,
Chia-I Wud7e01d72018-06-21 13:39:09 +0800715 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700716 }
717};
718
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800719/* ------------------------------------------------------------------------
720 * Typical display configurations to test
721 */
722
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700723template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy,
724 typename PerFrameMetadataSupportPolicy>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800725struct Case {
726 using Display = DisplayPolicy;
727 using WideColorSupport = WideColorSupportPolicy;
728 using HdrSupport = HdrSupportPolicy;
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700729 using PerFrameMetadataSupport = PerFrameMetadataSupportPolicy;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800730};
731
732using SimplePrimaryDisplayCase =
733 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700734 HdrNotSupportedVariant<PrimaryDisplayVariant>,
735 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800736using SimpleExternalDisplayCase =
737 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700738 HdrNotSupportedVariant<ExternalDisplayVariant>,
739 NoPerFrameMetadataSupportVariant<ExternalDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800740using SimpleTertiaryDisplayCase =
741 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700742 HdrNotSupportedVariant<TertiaryDisplayVariant>,
743 NoPerFrameMetadataSupportVariant<TertiaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800744using NonHwcVirtualDisplayCase =
745 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700746 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant,
747 NonHwcPerFrameMetadataSupportVariant>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800748using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
749using HwcVirtualDisplayCase =
750 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
Lloyd Pique438e9e72018-09-04 18:06:08 -0700751 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>,
tangrobin6753a022018-08-10 10:58:54 +0800752 NoPerFrameMetadataSupportVariant<SimpleHwcVirtualDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800753using WideColorP3ColorimetricDisplayCase =
754 Case<PrimaryDisplayVariant, WideColorP3ColorimetricSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700755 HdrNotSupportedVariant<PrimaryDisplayVariant>,
756 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800757using Hdr10DisplayCase =
758 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700759 Hdr10SupportedVariant<PrimaryDisplayVariant>,
760 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800761using HdrHlgDisplayCase =
762 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700763 HdrHlgSupportedVariant<PrimaryDisplayVariant>,
764 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800765using HdrDolbyVisionDisplayCase =
766 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700767 HdrDolbyVisionSupportedVariant<PrimaryDisplayVariant>,
768 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
769using HdrSmpte2086DisplayCase =
770 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
771 HdrNotSupportedVariant<PrimaryDisplayVariant>,
772 Smpte2086PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
773using HdrCta861_3_DisplayCase =
774 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
775 HdrNotSupportedVariant<PrimaryDisplayVariant>,
776 Cta861_3_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Dominik Laskowskif07b85b2018-06-11 12:49:15 -0700777
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800778/* ------------------------------------------------------------------------
Lloyd Pique6cf11032018-01-22 18:57:44 -0800779 *
780 * SurfaceFlinger::onHotplugReceived
781 */
782
783TEST_F(DisplayTransactionTest, hotplugEnqueuesEventsForDisplayTransaction) {
784 constexpr int currentSequenceId = 123;
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700785 constexpr hwc2_display_t hwcDisplayId1 = 456;
786 constexpr hwc2_display_t hwcDisplayId2 = 654;
Lloyd Pique6cf11032018-01-22 18:57:44 -0800787
788 // --------------------------------------------------------------------
789 // Preconditions
790
791 // Set the current sequence id for accepted events
792 mFlinger.mutableComposerSequenceId() = currentSequenceId;
793
794 // Set the main thread id so that the current thread does not appear to be
795 // the main thread.
796 mFlinger.mutableMainThreadId() = std::thread::id();
797
798 // --------------------------------------------------------------------
799 // Call Expectations
800
801 // We expect invalidate() to be invoked once to trigger display transaction
802 // processing.
803 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
804
805 // --------------------------------------------------------------------
806 // Invocation
807
808 // Simulate two hotplug events (a connect and a disconnect)
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700809 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId1, HWC2::Connection::Connected);
810 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId2, HWC2::Connection::Disconnected);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800811
812 // --------------------------------------------------------------------
813 // Postconditions
814
815 // The display transaction needed flag should be set.
816 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
817
818 // All events should be in the pending event queue.
819 const auto& pendingEvents = mFlinger.mutablePendingHotplugEvents();
820 ASSERT_EQ(2u, pendingEvents.size());
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700821 EXPECT_EQ(hwcDisplayId1, pendingEvents[0].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800822 EXPECT_EQ(HWC2::Connection::Connected, pendingEvents[0].connection);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700823 EXPECT_EQ(hwcDisplayId2, pendingEvents[1].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800824 EXPECT_EQ(HWC2::Connection::Disconnected, pendingEvents[1].connection);
825}
826
827TEST_F(DisplayTransactionTest, hotplugDiscardsUnexpectedEvents) {
828 constexpr int currentSequenceId = 123;
829 constexpr int otherSequenceId = 321;
830 constexpr hwc2_display_t displayId = 456;
831
832 // --------------------------------------------------------------------
833 // Preconditions
834
835 // Set the current sequence id for accepted events
836 mFlinger.mutableComposerSequenceId() = currentSequenceId;
837
838 // Set the main thread id so that the current thread does not appear to be
839 // the main thread.
840 mFlinger.mutableMainThreadId() = std::thread::id();
841
842 // --------------------------------------------------------------------
843 // Call Expectations
844
845 // We do not expect any calls to invalidate().
846 EXPECT_CALL(*mMessageQueue, invalidate()).Times(0);
847
848 // --------------------------------------------------------------------
849 // Invocation
850
851 // Call with an unexpected sequence id
852 mFlinger.onHotplugReceived(otherSequenceId, displayId, HWC2::Connection::Invalid);
853
854 // --------------------------------------------------------------------
855 // Postconditions
856
857 // The display transaction needed flag should not be set
858 EXPECT_FALSE(hasTransactionFlagSet(eDisplayTransactionNeeded));
859
860 // There should be no pending events
861 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
862}
863
864TEST_F(DisplayTransactionTest, hotplugProcessesEnqueuedEventsIfCalledOnMainThread) {
865 constexpr int currentSequenceId = 123;
866 constexpr hwc2_display_t displayId1 = 456;
867
868 // --------------------------------------------------------------------
869 // Note:
870 // --------------------------------------------------------------------
871 // This test case is a bit tricky. We want to verify that
872 // onHotplugReceived() calls processDisplayHotplugEventsLocked(), but we
873 // don't really want to provide coverage for everything the later function
874 // does as there are specific tests for it.
875 // --------------------------------------------------------------------
876
877 // --------------------------------------------------------------------
878 // Preconditions
879
880 // Set the current sequence id for accepted events
881 mFlinger.mutableComposerSequenceId() = currentSequenceId;
882
883 // Set the main thread id so that the current thread does appear to be the
884 // main thread.
885 mFlinger.mutableMainThreadId() = std::this_thread::get_id();
886
887 // --------------------------------------------------------------------
888 // Call Expectations
889
890 // We expect invalidate() to be invoked once to trigger display transaction
891 // processing.
892 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
893
894 // --------------------------------------------------------------------
895 // Invocation
896
897 // Simulate a disconnect on a display id that is not connected. This should
898 // be enqueued by onHotplugReceived(), and dequeued by
899 // processDisplayHotplugEventsLocked(), but then ignored as invalid.
900 mFlinger.onHotplugReceived(currentSequenceId, displayId1, HWC2::Connection::Disconnected);
901
902 // --------------------------------------------------------------------
903 // Postconditions
904
905 // The display transaction needed flag should be set.
906 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
907
908 // There should be no event queued on return, as it should have been
909 // processed.
910 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
911}
912
913/* ------------------------------------------------------------------------
Lloyd Piquea482f992018-01-22 19:00:34 -0800914 * SurfaceFlinger::createDisplay
915 */
916
917TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForNonsecureDisplay) {
918 const String8 name("virtual.test");
919
920 // --------------------------------------------------------------------
921 // Call Expectations
922
923 // The call should notify the interceptor that a display was created.
924 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
925
926 // --------------------------------------------------------------------
927 // Invocation
928
929 sp<IBinder> displayToken = mFlinger.createDisplay(name, false);
930
931 // --------------------------------------------------------------------
932 // Postconditions
933
934 // The display should have been added to the current state
935 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
936 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700937 EXPECT_TRUE(display.isVirtual());
938 EXPECT_FALSE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -0800939 EXPECT_EQ(name.string(), display.displayName);
940
941 // --------------------------------------------------------------------
942 // Cleanup conditions
943
944 // Destroying the display invalidates the display state.
945 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
946}
947
948TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForSecureDisplay) {
949 const String8 name("virtual.test");
950
951 // --------------------------------------------------------------------
952 // Call Expectations
953
954 // The call should notify the interceptor that a display was created.
955 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
956
957 // --------------------------------------------------------------------
958 // Invocation
959
960 sp<IBinder> displayToken = mFlinger.createDisplay(name, true);
961
962 // --------------------------------------------------------------------
963 // Postconditions
964
965 // The display should have been added to the current state
966 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
967 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700968 EXPECT_TRUE(display.isVirtual());
969 EXPECT_TRUE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -0800970 EXPECT_EQ(name.string(), display.displayName);
971
972 // --------------------------------------------------------------------
973 // Cleanup conditions
974
975 // Destroying the display invalidates the display state.
976 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
977}
978
979/* ------------------------------------------------------------------------
980 * SurfaceFlinger::destroyDisplay
981 */
982
983TEST_F(DisplayTransactionTest, destroyDisplayClearsCurrentStateForDisplay) {
984 using Case = NonHwcVirtualDisplayCase;
985
986 // --------------------------------------------------------------------
987 // Preconditions
988
989 // A virtual display exists
990 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
991 existing.inject();
992
993 // --------------------------------------------------------------------
994 // Call Expectations
995
996 // The call should notify the interceptor that a display was created.
997 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
998
999 // Destroying the display invalidates the display state.
1000 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1001
1002 // --------------------------------------------------------------------
1003 // Invocation
1004
1005 mFlinger.destroyDisplay(existing.token());
1006
1007 // --------------------------------------------------------------------
1008 // Postconditions
1009
1010 // The display should have been removed from the current state
1011 EXPECT_FALSE(hasCurrentDisplayState(existing.token()));
1012
1013 // Ths display should still exist in the drawing state
1014 EXPECT_TRUE(hasDrawingDisplayState(existing.token()));
1015
1016 // The display transaction needed flasg should be set
1017 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1018}
1019
1020TEST_F(DisplayTransactionTest, destroyDisplayHandlesUnknownDisplay) {
1021 // --------------------------------------------------------------------
1022 // Preconditions
1023
1024 sp<BBinder> displayToken = new BBinder();
1025
1026 // --------------------------------------------------------------------
1027 // Invocation
1028
1029 mFlinger.destroyDisplay(displayToken);
1030}
1031
1032/* ------------------------------------------------------------------------
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001033 * SurfaceFlinger::resetDisplayState
1034 */
1035
1036TEST_F(DisplayTransactionTest, resetDisplayStateClearsState) {
1037 using Case = NonHwcVirtualDisplayCase;
1038
1039 // --------------------------------------------------------------------
1040 // Preconditions
1041
1042 // vsync is enabled and available
1043 mFlinger.mutablePrimaryHWVsyncEnabled() = true;
1044 mFlinger.mutableHWVsyncAvailable() = true;
1045
1046 // A display exists
1047 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1048 existing.inject();
1049
1050 // --------------------------------------------------------------------
1051 // Call Expectations
1052
1053 // The call disable vsyncs
1054 EXPECT_CALL(*mEventControlThread, setVsyncEnabled(false)).Times(1);
1055
Lloyd Pique41be5d22018-06-21 13:11:48 -07001056 // The call ends any display resyncs
1057 EXPECT_CALL(*mPrimaryDispSync, endResync()).Times(1);
1058
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001059 // --------------------------------------------------------------------
1060 // Invocation
1061
1062 mFlinger.resetDisplayState();
1063
1064 // --------------------------------------------------------------------
1065 // Postconditions
1066
1067 // vsyncs should be off and not available.
1068 EXPECT_FALSE(mFlinger.mutablePrimaryHWVsyncEnabled());
1069 EXPECT_FALSE(mFlinger.mutableHWVsyncAvailable());
1070
1071 // The display should have been removed from the display map.
1072 EXPECT_FALSE(hasDisplayDevice(existing.token()));
1073
1074 // The display should still exist in the current state
1075 EXPECT_TRUE(hasCurrentDisplayState(existing.token()));
1076
1077 // The display should have been removed from the drawing state
1078 EXPECT_FALSE(hasDrawingDisplayState(existing.token()));
1079}
1080
1081/* ------------------------------------------------------------------------
Valerie Hau9758ae02018-10-09 16:05:09 -07001082 * DisplayDevice::GetBestColorMode
1083 */
1084class GetBestColorModeTest : public DisplayTransactionTest {
1085public:
Dominik Laskowski34157762018-10-31 13:07:19 -07001086 static constexpr DisplayId DEFAULT_DISPLAY_ID = DisplayId{777};
Dominik Laskowski075d3172018-05-24 15:50:06 -07001087
Valerie Hau9758ae02018-10-09 16:05:09 -07001088 GetBestColorModeTest()
1089 : DisplayTransactionTest(),
Dominik Laskowski075d3172018-05-24 15:50:06 -07001090 mInjector(FakeDisplayDeviceInjector(mFlinger, DEFAULT_DISPLAY_ID, false /* isVirtual */,
1091 true /* isPrimary */)) {}
Valerie Hau9758ae02018-10-09 16:05:09 -07001092
1093 void setHasWideColorGamut(bool hasWideColorGamut) { mHasWideColorGamut = hasWideColorGamut; }
1094
1095 void addHwcColorModesMapping(ui::ColorMode colorMode,
1096 std::vector<ui::RenderIntent> renderIntents) {
1097 mHwcColorModes[colorMode] = renderIntents;
1098 }
1099
1100 void setInputDataspace(ui::Dataspace dataspace) { mInputDataspace = dataspace; }
1101
1102 void setInputRenderIntent(ui::RenderIntent renderIntent) { mInputRenderIntent = renderIntent; }
1103
1104 void getBestColorMode() {
1105 mInjector.setHwcColorModes(mHwcColorModes);
1106 mInjector.setHasWideColorGamut(mHasWideColorGamut);
Alec Mouriba013fa2018-10-16 12:43:11 -07001107 mInjector.setNativeWindow(mNativeWindow);
1108
1109 // Creating a DisplayDevice requires getting default dimensions from the
1110 // native window.
1111 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
1112 .WillRepeatedly(DoAll(SetArgPointee<1>(1080 /* arbitrary */), Return(0)));
1113 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
1114 .WillRepeatedly(DoAll(SetArgPointee<1>(1920 /* arbitrary */), Return(0)));
Alec Mouri4efb6c22018-11-16 13:07:47 -08001115 EXPECT_CALL(*mNativeWindow, perform(9)).Times(1);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001116 EXPECT_CALL(*mNativeWindow, perform(13)).Times(1);
Alec Mouri4efb6c22018-11-16 13:07:47 -08001117 EXPECT_CALL(*mNativeWindow, perform(30)).Times(1);
Valerie Hau9758ae02018-10-09 16:05:09 -07001118 auto displayDevice = mInjector.inject();
1119
1120 displayDevice->getBestColorMode(mInputDataspace, mInputRenderIntent, &mOutDataspace,
1121 &mOutColorMode, &mOutRenderIntent);
1122 }
1123
1124 ui::Dataspace mOutDataspace;
1125 ui::ColorMode mOutColorMode;
1126 ui::RenderIntent mOutRenderIntent;
1127
1128private:
1129 ui::Dataspace mInputDataspace;
1130 ui::RenderIntent mInputRenderIntent;
1131 bool mHasWideColorGamut = false;
1132 std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> mHwcColorModes;
1133 FakeDisplayDeviceInjector mInjector;
1134};
1135
1136TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeSRGB) {
1137 addHwcColorModesMapping(ui::ColorMode::SRGB,
1138 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1139 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1140 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1141 setHasWideColorGamut(true);
1142
1143 getBestColorMode();
1144
Peiyong Lin14724e62018-12-05 07:27:30 -08001145 ASSERT_EQ(ui::Dataspace::V0_SRGB, mOutDataspace);
Valerie Hau9758ae02018-10-09 16:05:09 -07001146 ASSERT_EQ(ui::ColorMode::SRGB, mOutColorMode);
1147 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1148}
1149
1150TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDisplayP3) {
1151 addHwcColorModesMapping(ui::ColorMode::DISPLAY_P3,
1152 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1153 addHwcColorModesMapping(ui::ColorMode::SRGB,
1154 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1155 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1156 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1157 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1158 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1159 setHasWideColorGamut(true);
1160
1161 getBestColorMode();
1162
1163 ASSERT_EQ(ui::Dataspace::DISPLAY_P3, mOutDataspace);
1164 ASSERT_EQ(ui::ColorMode::DISPLAY_P3, mOutColorMode);
1165 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1166}
1167
1168TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDISPLAY_BT2020) {
1169 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1170 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1171 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1172 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1173 setHasWideColorGamut(true);
1174
1175 getBestColorMode();
1176
1177 ASSERT_EQ(ui::Dataspace::DISPLAY_BT2020, mOutDataspace);
1178 ASSERT_EQ(ui::ColorMode::DISPLAY_BT2020, mOutColorMode);
1179 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1180}
1181
1182/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001183 * SurfaceFlinger::setupNewDisplayDeviceInternal
1184 */
1185
1186class SetupNewDisplayDeviceInternalTest : public DisplayTransactionTest {
1187public:
1188 template <typename T>
1189 void setupNewDisplayDeviceInternalTest();
1190};
1191
1192template <typename Case>
1193void SetupNewDisplayDeviceInternalTest::setupNewDisplayDeviceInternalTest() {
1194 const sp<BBinder> displayToken = new BBinder();
1195 const sp<mock::DisplaySurface> displaySurface = new mock::DisplaySurface();
1196 const sp<mock::GraphicBufferProducer> producer = new mock::GraphicBufferProducer();
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001197
1198 // --------------------------------------------------------------------
1199 // Preconditions
1200
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001201 // Wide color displays support is configured appropriately
1202 Case::WideColorSupport::injectConfigChange(this);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001203
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001204 // The display is setup with the HWC.
1205 Case::Display::injectHwcDisplay(this);
1206
1207 // SurfaceFlinger will use a test-controlled factory for native window
1208 // surfaces.
1209 injectFakeNativeWindowSurfaceFactory();
1210
1211 // --------------------------------------------------------------------
1212 // Call Expectations
1213
1214 // Various native window calls will be made.
1215 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
Lloyd Pique3c085a02018-05-09 19:38:32 -07001216 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001217 Case::WideColorSupport::setupComposerCallExpectations(this);
1218 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001219 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001220
1221 // --------------------------------------------------------------------
1222 // Invocation
1223
1224 DisplayDeviceState state;
Dominik Laskowski075d3172018-05-24 15:50:06 -07001225 state.displayId = static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1226 : Case::Display::DISPLAY_ID::get();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001227 state.isSecure = static_cast<bool>(Case::Display::SECURE);
1228
Dominik Laskowski075d3172018-05-24 15:50:06 -07001229 auto device =
1230 mFlinger.setupNewDisplayDeviceInternal(displayToken, Case::Display::DISPLAY_ID::get(),
1231 state, displaySurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001232
1233 // --------------------------------------------------------------------
1234 // Postconditions
1235
1236 ASSERT_TRUE(device != nullptr);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001237 EXPECT_EQ(Case::Display::DISPLAY_ID::get(), device->getId());
1238 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), device->isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001239 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001240 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001241 EXPECT_EQ(Case::Display::WIDTH, device->getWidth());
1242 EXPECT_EQ(Case::Display::HEIGHT, device->getHeight());
1243 EXPECT_EQ(Case::WideColorSupport::WIDE_COLOR_SUPPORTED, device->hasWideColorGamut());
1244 EXPECT_EQ(Case::HdrSupport::HDR10_SUPPORTED, device->hasHDR10Support());
1245 EXPECT_EQ(Case::HdrSupport::HDR_HLG_SUPPORTED, device->hasHLGSupport());
1246 EXPECT_EQ(Case::HdrSupport::HDR_DOLBY_VISION_SUPPORTED, device->hasDolbyVisionSupport());
Lloyd Pique3c085a02018-05-09 19:38:32 -07001247 // Note: This is not Case::Display::HWC_ACTIVE_CONFIG_ID as the ids are
1248 // remapped, and the test only ever sets up one config. If there were an error
1249 // looking up the remapped index, device->getActiveConfig() would be -1 instead.
1250 EXPECT_EQ(0, device->getActiveConfig());
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001251 EXPECT_EQ(Case::PerFrameMetadataSupport::PER_FRAME_METADATA_KEYS,
1252 device->getSupportedPerFrameMetadata());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001253}
1254
1255TEST_F(SetupNewDisplayDeviceInternalTest, createSimplePrimaryDisplay) {
1256 setupNewDisplayDeviceInternalTest<SimplePrimaryDisplayCase>();
1257}
1258
1259TEST_F(SetupNewDisplayDeviceInternalTest, createSimpleExternalDisplay) {
1260 setupNewDisplayDeviceInternalTest<SimpleExternalDisplayCase>();
1261}
1262
1263TEST_F(SetupNewDisplayDeviceInternalTest, createNonHwcVirtualDisplay) {
1264 setupNewDisplayDeviceInternalTest<NonHwcVirtualDisplayCase>();
1265}
1266
1267TEST_F(SetupNewDisplayDeviceInternalTest, createHwcVirtualDisplay) {
Dominik Laskowski075d3172018-05-24 15:50:06 -07001268 using Case = HwcVirtualDisplayCase;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001269
Dominik Laskowski075d3172018-05-24 15:50:06 -07001270 // Insert display data so that the HWC thinks it created the virtual display.
1271 const auto displayId = Case::Display::DISPLAY_ID::get();
1272 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08001273 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001274
1275 setupNewDisplayDeviceInternalTest<Case>();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001276}
1277
1278TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3Display) {
1279 setupNewDisplayDeviceInternalTest<WideColorP3ColorimetricDisplayCase>();
1280}
1281
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001282TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10Display) {
1283 setupNewDisplayDeviceInternalTest<Hdr10DisplayCase>();
1284}
1285
1286TEST_F(SetupNewDisplayDeviceInternalTest, createHdrHlgDisplay) {
1287 setupNewDisplayDeviceInternalTest<HdrHlgDisplayCase>();
1288}
1289
1290TEST_F(SetupNewDisplayDeviceInternalTest, createHdrDolbyVisionDisplay) {
1291 setupNewDisplayDeviceInternalTest<HdrDolbyVisionDisplayCase>();
1292}
1293
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001294TEST_F(SetupNewDisplayDeviceInternalTest, createHdrSmpte2086DisplayCase) {
1295 setupNewDisplayDeviceInternalTest<HdrSmpte2086DisplayCase>();
1296}
1297
1298TEST_F(SetupNewDisplayDeviceInternalTest, createHdrCta816_3_DisplayCase) {
1299 setupNewDisplayDeviceInternalTest<HdrCta861_3_DisplayCase>();
1300}
1301
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001302/* ------------------------------------------------------------------------
1303 * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded)
1304 */
1305
1306class HandleTransactionLockedTest : public DisplayTransactionTest {
1307public:
1308 template <typename Case>
1309 void setupCommonPreconditions();
1310
1311 template <typename Case>
1312 void setupCommonCallExpectationsForConnectProcessing();
1313
1314 template <typename Case>
1315 void setupCommonCallExpectationsForDisconnectProcessing();
1316
1317 template <typename Case>
1318 void processesHotplugConnectCommon();
1319
1320 template <typename Case>
1321 void ignoresHotplugConnectCommon();
1322
1323 template <typename Case>
1324 void processesHotplugDisconnectCommon();
1325
1326 template <typename Case>
1327 void verifyDisplayIsConnected(const sp<IBinder>& displayToken);
1328
1329 template <typename Case>
1330 void verifyPhysicalDisplayIsConnected();
1331
1332 void verifyDisplayIsNotConnected(const sp<IBinder>& displayToken);
1333};
1334
1335template <typename Case>
1336void HandleTransactionLockedTest::setupCommonPreconditions() {
1337 // Wide color displays support is configured appropriately
1338 Case::WideColorSupport::injectConfigChange(this);
1339
1340 // SurfaceFlinger will use a test-controlled factory for BufferQueues
1341 injectFakeBufferQueueFactory();
1342
1343 // SurfaceFlinger will use a test-controlled factory for native window
1344 // surfaces.
1345 injectFakeNativeWindowSurfaceFactory();
1346}
1347
1348template <typename Case>
1349void HandleTransactionLockedTest::setupCommonCallExpectationsForConnectProcessing() {
1350 Case::Display::setupHwcHotplugCallExpectations(this);
1351
1352 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1353 Case::Display::setupFramebufferProducerBufferQueueCallExpectations(this);
1354 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1355 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
1356
1357 Case::WideColorSupport::setupComposerCallExpectations(this);
1358 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001359 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001360
1361 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001362 EXPECT_CALL(*mEventThread,
Dominik Laskowski075d3172018-05-24 15:50:06 -07001363 onHotplugReceived(static_cast<bool>(Case::Display::PRIMARY)
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001364 ? EventThread::DisplayType::Primary
1365 : EventThread::DisplayType::External,
1366 true))
1367 .Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001368}
1369
1370template <typename Case>
1371void HandleTransactionLockedTest::setupCommonCallExpectationsForDisconnectProcessing() {
1372 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001373 EXPECT_CALL(*mEventThread,
Dominik Laskowski075d3172018-05-24 15:50:06 -07001374 onHotplugReceived(static_cast<bool>(Case::Display::PRIMARY)
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001375 ? EventThread::DisplayType::Primary
1376 : EventThread::DisplayType::External,
1377 false))
1378 .Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001379}
1380
1381template <typename Case>
1382void HandleTransactionLockedTest::verifyDisplayIsConnected(const sp<IBinder>& displayToken) {
1383 // The display device should have been set up in the list of displays.
1384 ASSERT_TRUE(hasDisplayDevice(displayToken));
1385 const auto& device = getDisplayDevice(displayToken);
1386 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001387 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001388
1389 // The display should have been set up in the current display state
1390 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1391 const auto& current = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001392 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), current.isVirtual());
1393 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1394 : Case::Display::DISPLAY_ID::get(),
1395 current.displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001396
1397 // The display should have been set up in the drawing display state
1398 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1399 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001400 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
1401 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1402 : Case::Display::DISPLAY_ID::get(),
1403 draw.displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001404}
1405
1406template <typename Case>
1407void HandleTransactionLockedTest::verifyPhysicalDisplayIsConnected() {
1408 // HWComposer should have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001409 EXPECT_TRUE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001410
Dominik Laskowski075d3172018-05-24 15:50:06 -07001411 // SF should have a display token.
1412 const auto displayId = Case::Display::DISPLAY_ID::get();
1413 ASSERT_TRUE(displayId);
1414 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1415 auto& displayToken = mFlinger.mutablePhysicalDisplayTokens()[*displayId];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001416
1417 verifyDisplayIsConnected<Case>(displayToken);
1418}
1419
1420void HandleTransactionLockedTest::verifyDisplayIsNotConnected(const sp<IBinder>& displayToken) {
1421 EXPECT_FALSE(hasDisplayDevice(displayToken));
1422 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1423 EXPECT_FALSE(hasDrawingDisplayState(displayToken));
1424}
1425
1426template <typename Case>
1427void HandleTransactionLockedTest::processesHotplugConnectCommon() {
1428 // --------------------------------------------------------------------
1429 // Preconditions
1430
1431 setupCommonPreconditions<Case>();
1432
1433 // A hotplug connect event is enqueued for a display
1434 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001435
1436 // --------------------------------------------------------------------
1437 // Call Expectations
1438
1439 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillOnce(Return(false));
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001440
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001441 setupCommonCallExpectationsForConnectProcessing<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001442
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001443 // --------------------------------------------------------------------
1444 // Invocation
Lloyd Piquee39cad22017-12-20 17:01:29 -08001445
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001446 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
Lloyd Piquee39cad22017-12-20 17:01:29 -08001447
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001448 // --------------------------------------------------------------------
1449 // Postconditions
1450
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001451 verifyPhysicalDisplayIsConnected<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001452
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001453 // --------------------------------------------------------------------
1454 // Cleanup conditions
1455
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001456 EXPECT_CALL(*mComposer,
1457 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001458 .WillOnce(Return(Error::NONE));
1459 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Piquef58625d2017-12-19 13:22:33 -08001460}
1461
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001462template <typename Case>
1463void HandleTransactionLockedTest::ignoresHotplugConnectCommon() {
1464 // --------------------------------------------------------------------
1465 // Preconditions
1466
1467 setupCommonPreconditions<Case>();
1468
1469 // A hotplug connect event is enqueued for a display
1470 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1471
1472 // --------------------------------------------------------------------
1473 // Invocation
1474
1475 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1476
1477 // --------------------------------------------------------------------
1478 // Postconditions
1479
1480 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001481 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001482}
1483
1484template <typename Case>
1485void HandleTransactionLockedTest::processesHotplugDisconnectCommon() {
1486 // --------------------------------------------------------------------
1487 // Preconditions
1488
1489 setupCommonPreconditions<Case>();
1490
1491 // A hotplug disconnect event is enqueued for a display
1492 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1493
1494 // The display is already completely set up.
1495 Case::Display::injectHwcDisplay(this);
1496 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1497 existing.inject();
1498
1499 // --------------------------------------------------------------------
1500 // Call Expectations
1501
1502 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
Lloyd Pique438e9e72018-09-04 18:06:08 -07001503 EXPECT_CALL(*mComposer, getDisplayIdentificationData(Case::Display::HWC_DISPLAY_ID, _, _))
1504 .Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001505
1506 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1507
1508 // --------------------------------------------------------------------
1509 // Invocation
1510
1511 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1512
1513 // --------------------------------------------------------------------
1514 // Postconditions
1515
1516 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001517 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001518
Dominik Laskowski075d3172018-05-24 15:50:06 -07001519 // SF should not have a display token.
1520 const auto displayId = Case::Display::DISPLAY_ID::get();
1521 ASSERT_TRUE(displayId);
1522 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001523
1524 // The existing token should have been removed
1525 verifyDisplayIsNotConnected(existing.token());
1526}
1527
1528TEST_F(HandleTransactionLockedTest, processesHotplugConnectPrimaryDisplay) {
1529 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1530}
1531
1532TEST_F(HandleTransactionLockedTest,
1533 processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected) {
1534 // Inject an external display.
1535 ExternalDisplayVariant::injectHwcDisplay(this);
1536
1537 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1538}
1539
1540TEST_F(HandleTransactionLockedTest, processesHotplugConnectExternalDisplay) {
1541 // Inject a primary display.
1542 PrimaryDisplayVariant::injectHwcDisplay(this);
1543
1544 processesHotplugConnectCommon<SimpleExternalDisplayCase>();
1545}
1546
1547TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected) {
1548 // Inject both a primary and external display.
1549 PrimaryDisplayVariant::injectHwcDisplay(this);
1550 ExternalDisplayVariant::injectHwcDisplay(this);
1551
1552 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1553
1554 ignoresHotplugConnectCommon<SimpleTertiaryDisplayCase>();
1555}
1556
1557TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfExternalForVrComposer) {
1558 // Inject a primary display.
1559 PrimaryDisplayVariant::injectHwcDisplay(this);
1560
1561 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(true));
1562
1563 ignoresHotplugConnectCommon<SimpleExternalDisplayCase>();
1564}
1565
1566TEST_F(HandleTransactionLockedTest, processHotplugDisconnectPrimaryDisplay) {
1567 processesHotplugDisconnectCommon<SimplePrimaryDisplayCase>();
1568}
1569
1570TEST_F(HandleTransactionLockedTest, processHotplugDisconnectExternalDisplay) {
1571 processesHotplugDisconnectCommon<SimpleExternalDisplayCase>();
1572}
1573
1574TEST_F(HandleTransactionLockedTest, processesHotplugConnectThenDisconnectPrimary) {
1575 using Case = SimplePrimaryDisplayCase;
1576
1577 // --------------------------------------------------------------------
1578 // Preconditions
1579
1580 setupCommonPreconditions<Case>();
1581
1582 // A hotplug connect event is enqueued for a display
1583 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1584 // A hotplug disconnect event is also enqueued for the same display
1585 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1586
1587 // --------------------------------------------------------------------
1588 // Call Expectations
1589
1590 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1591
1592 setupCommonCallExpectationsForConnectProcessing<Case>();
1593 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1594
1595 EXPECT_CALL(*mComposer,
1596 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1597 .WillOnce(Return(Error::NONE));
1598 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1599
1600 // --------------------------------------------------------------------
1601 // Invocation
1602
1603 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1604
1605 // --------------------------------------------------------------------
1606 // Postconditions
1607
1608 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001609 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001610
Dominik Laskowski075d3172018-05-24 15:50:06 -07001611 // SF should not have a display token.
1612 const auto displayId = Case::Display::DISPLAY_ID::get();
1613 ASSERT_TRUE(displayId);
1614 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001615}
1616
1617TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectThenConnectPrimary) {
1618 using Case = SimplePrimaryDisplayCase;
1619
1620 // --------------------------------------------------------------------
1621 // Preconditions
1622
1623 setupCommonPreconditions<Case>();
1624
1625 // The display is already completely set up.
1626 Case::Display::injectHwcDisplay(this);
1627 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1628 existing.inject();
1629
1630 // A hotplug disconnect event is enqueued for a display
1631 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1632 // A hotplug connect event is also enqueued for the same display
1633 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1634
1635 // --------------------------------------------------------------------
1636 // Call Expectations
1637
1638 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1639
1640 setupCommonCallExpectationsForConnectProcessing<Case>();
1641 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1642
1643 // --------------------------------------------------------------------
1644 // Invocation
1645
1646 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1647
1648 // --------------------------------------------------------------------
1649 // Postconditions
1650
1651 // The existing token should have been removed
1652 verifyDisplayIsNotConnected(existing.token());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001653 const auto displayId = Case::Display::DISPLAY_ID::get();
1654 ASSERT_TRUE(displayId);
1655 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1656 EXPECT_NE(existing.token(), mFlinger.mutablePhysicalDisplayTokens()[*displayId]);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001657
1658 // A new display should be connected in its place
1659
1660 verifyPhysicalDisplayIsConnected<Case>();
1661
1662 // --------------------------------------------------------------------
1663 // Cleanup conditions
1664
1665 EXPECT_CALL(*mComposer,
1666 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1667 .WillOnce(Return(Error::NONE));
1668 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1669}
1670
1671TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAdded) {
1672 using Case = HwcVirtualDisplayCase;
1673
1674 // --------------------------------------------------------------------
1675 // Preconditions
1676
1677 // The HWC supports at least one virtual display
1678 injectMockComposer(1);
1679
1680 setupCommonPreconditions<Case>();
1681
1682 // A virtual display was added to the current state, and it has a
1683 // surface(producer)
1684 sp<BBinder> displayToken = new BBinder();
Lloyd Pique4c2ac022018-04-27 12:08:03 -07001685
Dominik Laskowski075d3172018-05-24 15:50:06 -07001686 DisplayDeviceState state;
1687 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001688
1689 sp<mock::GraphicBufferProducer> surface{new mock::GraphicBufferProducer()};
Dominik Laskowski075d3172018-05-24 15:50:06 -07001690 state.surface = surface;
1691 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001692
1693 // --------------------------------------------------------------------
1694 // Call Expectations
1695
1696 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1697 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1698
1699 EXPECT_CALL(*surface, query(NATIVE_WINDOW_WIDTH, _))
1700 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::WIDTH), Return(NO_ERROR)));
1701 EXPECT_CALL(*surface, query(NATIVE_WINDOW_HEIGHT, _))
1702 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::HEIGHT), Return(NO_ERROR)));
1703 EXPECT_CALL(*surface, query(NATIVE_WINDOW_FORMAT, _))
1704 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT),
1705 Return(NO_ERROR)));
1706 EXPECT_CALL(*surface, query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, _))
1707 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(NO_ERROR)));
1708
1709 EXPECT_CALL(*surface, setAsyncMode(true)).Times(1);
1710
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001711 EXPECT_CALL(*mProducer, disconnect(_, _)).Times(1);
1712
1713 Case::Display::setupHwcVirtualDisplayCreationCallExpectations(this);
1714 Case::WideColorSupport::setupComposerCallExpectations(this);
1715 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001716 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001717
1718 // --------------------------------------------------------------------
1719 // Invocation
1720
1721 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1722
1723 // --------------------------------------------------------------------
1724 // Postconditions
1725
1726 // The display device should have been set up in the list of displays.
1727 verifyDisplayIsConnected<Case>(displayToken);
1728
1729 // --------------------------------------------------------------------
1730 // Cleanup conditions
1731
1732 EXPECT_CALL(*mComposer, destroyVirtualDisplay(Case::Display::HWC_DISPLAY_ID))
1733 .WillOnce(Return(Error::NONE));
1734 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1735}
1736
1737TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAddedWithNoSurface) {
1738 using Case = HwcVirtualDisplayCase;
1739
1740 // --------------------------------------------------------------------
1741 // Preconditions
1742
1743 // The HWC supports at least one virtual display
1744 injectMockComposer(1);
1745
1746 setupCommonPreconditions<Case>();
1747
1748 // A virtual display was added to the current state, but it does not have a
1749 // surface.
1750 sp<BBinder> displayToken = new BBinder();
1751
Dominik Laskowski075d3172018-05-24 15:50:06 -07001752 DisplayDeviceState state;
1753 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001754
Dominik Laskowski075d3172018-05-24 15:50:06 -07001755 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001756
1757 // --------------------------------------------------------------------
1758 // Call Expectations
1759
1760 // --------------------------------------------------------------------
1761 // Invocation
1762
1763 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1764
1765 // --------------------------------------------------------------------
1766 // Postconditions
1767
1768 // There will not be a display device set up.
1769 EXPECT_FALSE(hasDisplayDevice(displayToken));
1770
1771 // The drawing display state will be set from the current display state.
1772 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1773 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001774 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001775}
1776
1777TEST_F(HandleTransactionLockedTest, processesVirtualDisplayRemoval) {
1778 using Case = HwcVirtualDisplayCase;
1779
1780 // --------------------------------------------------------------------
1781 // Preconditions
1782
1783 // A virtual display is set up but is removed from the current state.
Dominik Laskowski075d3172018-05-24 15:50:06 -07001784 const auto displayId = Case::Display::DISPLAY_ID::get();
1785 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08001786 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001787 Case::Display::injectHwcDisplay(this);
1788 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1789 existing.inject();
1790 mFlinger.mutableCurrentState().displays.removeItem(existing.token());
1791
1792 // --------------------------------------------------------------------
1793 // Call Expectations
1794
1795 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1796
1797 // --------------------------------------------------------------------
1798 // Invocation
1799
1800 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1801
1802 // --------------------------------------------------------------------
1803 // Postconditions
1804
1805 // The existing token should have been removed
1806 verifyDisplayIsNotConnected(existing.token());
1807}
1808
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001809TEST_F(HandleTransactionLockedTest, processesDisplayLayerStackChanges) {
1810 using Case = NonHwcVirtualDisplayCase;
1811
1812 constexpr uint32_t oldLayerStack = 0u;
1813 constexpr uint32_t newLayerStack = 123u;
1814
1815 // --------------------------------------------------------------------
1816 // Preconditions
1817
1818 // A display is set up
1819 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1820 display.inject();
1821
1822 // There is a change to the layerStack state
1823 display.mutableDrawingDisplayState().layerStack = oldLayerStack;
1824 display.mutableCurrentDisplayState().layerStack = newLayerStack;
1825
1826 // --------------------------------------------------------------------
1827 // Invocation
1828
1829 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1830
1831 // --------------------------------------------------------------------
1832 // Postconditions
1833
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001834 EXPECT_EQ(newLayerStack, display.mutableDisplayDevice()->getLayerStack());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001835}
1836
1837TEST_F(HandleTransactionLockedTest, processesDisplayTransformChanges) {
1838 using Case = NonHwcVirtualDisplayCase;
1839
1840 constexpr int oldTransform = 0;
1841 constexpr int newTransform = 2;
1842
1843 // --------------------------------------------------------------------
1844 // Preconditions
1845
1846 // A display is set up
1847 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1848 display.inject();
1849
1850 // There is a change to the orientation state
1851 display.mutableDrawingDisplayState().orientation = oldTransform;
1852 display.mutableCurrentDisplayState().orientation = newTransform;
1853
1854 // --------------------------------------------------------------------
1855 // Invocation
1856
1857 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1858
1859 // --------------------------------------------------------------------
1860 // Postconditions
1861
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001862 EXPECT_EQ(newTransform, display.mutableDisplayDevice()->getOrientation());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001863}
1864
1865TEST_F(HandleTransactionLockedTest, processesDisplayViewportChanges) {
1866 using Case = NonHwcVirtualDisplayCase;
1867
1868 const Rect oldViewport(0, 0, 0, 0);
1869 const Rect newViewport(0, 0, 123, 456);
1870
1871 // --------------------------------------------------------------------
1872 // Preconditions
1873
1874 // A display is set up
1875 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1876 display.inject();
1877
1878 // There is a change to the viewport state
1879 display.mutableDrawingDisplayState().viewport = oldViewport;
1880 display.mutableCurrentDisplayState().viewport = newViewport;
1881
1882 // --------------------------------------------------------------------
1883 // Invocation
1884
1885 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1886
1887 // --------------------------------------------------------------------
1888 // Postconditions
1889
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001890 EXPECT_EQ(newViewport, display.mutableDisplayDevice()->getViewport());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001891}
1892
1893TEST_F(HandleTransactionLockedTest, processesDisplayFrameChanges) {
1894 using Case = NonHwcVirtualDisplayCase;
1895
1896 const Rect oldFrame(0, 0, 0, 0);
1897 const Rect newFrame(0, 0, 123, 456);
1898
1899 // --------------------------------------------------------------------
1900 // Preconditions
1901
1902 // A display is set up
1903 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1904 display.inject();
1905
1906 // There is a change to the viewport state
1907 display.mutableDrawingDisplayState().frame = oldFrame;
1908 display.mutableCurrentDisplayState().frame = newFrame;
1909
1910 // --------------------------------------------------------------------
1911 // Invocation
1912
1913 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1914
1915 // --------------------------------------------------------------------
1916 // Postconditions
1917
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001918 EXPECT_EQ(newFrame, display.mutableDisplayDevice()->getFrame());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001919}
1920
1921TEST_F(HandleTransactionLockedTest, processesDisplayWidthChanges) {
1922 using Case = NonHwcVirtualDisplayCase;
1923
1924 constexpr int oldWidth = 0;
1925 constexpr int oldHeight = 10;
1926 constexpr int newWidth = 123;
1927
1928 // --------------------------------------------------------------------
1929 // Preconditions
1930
1931 // A display is set up
1932 auto nativeWindow = new mock::NativeWindow();
1933 auto displaySurface = new mock::DisplaySurface();
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001934 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001935 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1936 display.setNativeWindow(nativeWindow);
1937 display.setDisplaySurface(displaySurface);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001938 // Setup injection expections
1939 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
1940 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
1941 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
1942 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
Alec Mouri4efb6c22018-11-16 13:07:47 -08001943 EXPECT_CALL(*nativeWindow, perform(9)).Times(1);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001944 EXPECT_CALL(*nativeWindow, perform(13)).Times(1);
Alec Mouri4efb6c22018-11-16 13:07:47 -08001945 EXPECT_CALL(*nativeWindow, perform(30)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001946 display.inject();
1947
1948 // There is a change to the viewport state
1949 display.mutableDrawingDisplayState().width = oldWidth;
1950 display.mutableDrawingDisplayState().height = oldHeight;
1951 display.mutableCurrentDisplayState().width = newWidth;
1952 display.mutableCurrentDisplayState().height = oldHeight;
1953
1954 // --------------------------------------------------------------------
1955 // Call Expectations
1956
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001957 EXPECT_CALL(*displaySurface, resizeBuffers(newWidth, oldHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001958
1959 // --------------------------------------------------------------------
1960 // Invocation
1961
1962 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1963}
1964
1965TEST_F(HandleTransactionLockedTest, processesDisplayHeightChanges) {
1966 using Case = NonHwcVirtualDisplayCase;
1967
1968 constexpr int oldWidth = 0;
1969 constexpr int oldHeight = 10;
1970 constexpr int newHeight = 123;
1971
1972 // --------------------------------------------------------------------
1973 // Preconditions
1974
1975 // A display is set up
1976 auto nativeWindow = new mock::NativeWindow();
1977 auto displaySurface = new mock::DisplaySurface();
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001978 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001979 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1980 display.setNativeWindow(nativeWindow);
1981 display.setDisplaySurface(displaySurface);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001982 // Setup injection expections
1983 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
1984 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
1985 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
1986 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
Alec Mouri4efb6c22018-11-16 13:07:47 -08001987 EXPECT_CALL(*nativeWindow, perform(9)).Times(1);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001988 EXPECT_CALL(*nativeWindow, perform(13)).Times(1);
Alec Mouri4efb6c22018-11-16 13:07:47 -08001989 EXPECT_CALL(*nativeWindow, perform(30)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001990 display.inject();
1991
1992 // There is a change to the viewport state
1993 display.mutableDrawingDisplayState().width = oldWidth;
1994 display.mutableDrawingDisplayState().height = oldHeight;
1995 display.mutableCurrentDisplayState().width = oldWidth;
1996 display.mutableCurrentDisplayState().height = newHeight;
1997
1998 // --------------------------------------------------------------------
1999 // Call Expectations
2000
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002001 EXPECT_CALL(*displaySurface, resizeBuffers(oldWidth, newHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002002
2003 // --------------------------------------------------------------------
2004 // Invocation
2005
2006 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2007}
2008
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002009/* ------------------------------------------------------------------------
2010 * SurfaceFlinger::setDisplayStateLocked
2011 */
2012
2013TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithUnknownDisplay) {
2014 // --------------------------------------------------------------------
2015 // Preconditions
2016
2017 // We have an unknown display token not associated with a known display
2018 sp<BBinder> displayToken = new BBinder();
2019
2020 // The requested display state references the unknown display.
2021 DisplayState state;
2022 state.what = DisplayState::eLayerStackChanged;
2023 state.token = displayToken;
2024 state.layerStack = 456;
2025
2026 // --------------------------------------------------------------------
2027 // Invocation
2028
2029 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2030
2031 // --------------------------------------------------------------------
2032 // Postconditions
2033
2034 // The returned flags are empty
2035 EXPECT_EQ(0u, flags);
2036
2037 // The display token still doesn't match anything known.
2038 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
2039}
2040
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002041TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWhenNoChanges) {
2042 using Case = SimplePrimaryDisplayCase;
2043
2044 // --------------------------------------------------------------------
2045 // Preconditions
2046
2047 // A display is already set up
2048 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2049 display.inject();
2050
2051 // No changes are made to the display
2052 DisplayState state;
2053 state.what = 0;
2054 state.token = display.token();
2055
2056 // --------------------------------------------------------------------
2057 // Invocation
2058
2059 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2060
2061 // --------------------------------------------------------------------
2062 // Postconditions
2063
2064 // The returned flags are empty
2065 EXPECT_EQ(0u, flags);
2066}
2067
2068TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSurfaceDidNotChange) {
2069 using Case = SimplePrimaryDisplayCase;
2070
2071 // --------------------------------------------------------------------
2072 // Preconditions
2073
2074 // A display is already set up
2075 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2076 display.inject();
2077
2078 // There is a surface that can be set.
2079 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2080
2081 // The current display state has the surface set
2082 display.mutableCurrentDisplayState().surface = surface;
2083
2084 // The incoming request sets the same surface
2085 DisplayState state;
2086 state.what = DisplayState::eSurfaceChanged;
2087 state.token = display.token();
2088 state.surface = surface;
2089
2090 // --------------------------------------------------------------------
2091 // Invocation
2092
2093 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2094
2095 // --------------------------------------------------------------------
2096 // Postconditions
2097
2098 // The returned flags are empty
2099 EXPECT_EQ(0u, flags);
2100
2101 // The current display state is unchanged.
2102 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2103}
2104
2105TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfSurfaceChanged) {
2106 using Case = SimplePrimaryDisplayCase;
2107
2108 // --------------------------------------------------------------------
2109 // Preconditions
2110
2111 // A display is already set up
2112 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2113 display.inject();
2114
2115 // There is a surface that can be set.
2116 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2117
2118 // The current display state does not have a surface
2119 display.mutableCurrentDisplayState().surface = nullptr;
2120
2121 // The incoming request sets a surface
2122 DisplayState state;
2123 state.what = DisplayState::eSurfaceChanged;
2124 state.token = display.token();
2125 state.surface = surface;
2126
2127 // --------------------------------------------------------------------
2128 // Invocation
2129
2130 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2131
2132 // --------------------------------------------------------------------
2133 // Postconditions
2134
2135 // The returned flags indicate a transaction is needed
2136 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2137
2138 // The current display layer stack state is set to the new value
2139 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2140}
2141
2142TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfLayerStackDidNotChange) {
2143 using Case = SimplePrimaryDisplayCase;
2144
2145 // --------------------------------------------------------------------
2146 // Preconditions
2147
2148 // A display is already set up
2149 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2150 display.inject();
2151
2152 // The display has a layer stack set
2153 display.mutableCurrentDisplayState().layerStack = 456u;
2154
2155 // The incoming request sets the same layer stack
2156 DisplayState state;
2157 state.what = DisplayState::eLayerStackChanged;
2158 state.token = display.token();
2159 state.layerStack = 456u;
2160
2161 // --------------------------------------------------------------------
2162 // Invocation
2163
2164 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2165
2166 // --------------------------------------------------------------------
2167 // Postconditions
2168
2169 // The returned flags are empty
2170 EXPECT_EQ(0u, flags);
2171
2172 // The current display state is unchanged
2173 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2174}
2175
2176TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfLayerStackChanged) {
2177 using Case = SimplePrimaryDisplayCase;
2178
2179 // --------------------------------------------------------------------
2180 // Preconditions
2181
2182 // A display is set up
2183 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2184 display.inject();
2185
2186 // The display has a layer stack set
2187 display.mutableCurrentDisplayState().layerStack = 654u;
2188
2189 // The incoming request sets a different layer stack
2190 DisplayState state;
2191 state.what = DisplayState::eLayerStackChanged;
2192 state.token = display.token();
2193 state.layerStack = 456u;
2194
2195 // --------------------------------------------------------------------
2196 // Invocation
2197
2198 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2199
2200 // --------------------------------------------------------------------
2201 // Postconditions
2202
2203 // The returned flags indicate a transaction is needed
2204 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2205
2206 // The desired display state has been set to the new value.
2207 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2208}
2209
2210TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfProjectionDidNotChange) {
2211 using Case = SimplePrimaryDisplayCase;
2212 constexpr int initialOrientation = 180;
2213 const Rect initialFrame = {1, 2, 3, 4};
2214 const Rect initialViewport = {5, 6, 7, 8};
2215
2216 // --------------------------------------------------------------------
2217 // Preconditions
2218
2219 // A display is set up
2220 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2221 display.inject();
2222
2223 // The current display state projection state is all set
2224 display.mutableCurrentDisplayState().orientation = initialOrientation;
2225 display.mutableCurrentDisplayState().frame = initialFrame;
2226 display.mutableCurrentDisplayState().viewport = initialViewport;
2227
2228 // The incoming request sets the same projection state
2229 DisplayState state;
2230 state.what = DisplayState::eDisplayProjectionChanged;
2231 state.token = display.token();
2232 state.orientation = initialOrientation;
2233 state.frame = initialFrame;
2234 state.viewport = initialViewport;
2235
2236 // --------------------------------------------------------------------
2237 // Invocation
2238
2239 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2240
2241 // --------------------------------------------------------------------
2242 // Postconditions
2243
2244 // The returned flags are empty
2245 EXPECT_EQ(0u, flags);
2246
2247 // The current display state is unchanged
2248 EXPECT_EQ(initialOrientation, display.getCurrentDisplayState().orientation);
2249
2250 EXPECT_EQ(initialFrame, display.getCurrentDisplayState().frame);
2251 EXPECT_EQ(initialViewport, display.getCurrentDisplayState().viewport);
2252}
2253
2254TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfOrientationChanged) {
2255 using Case = SimplePrimaryDisplayCase;
2256 constexpr int initialOrientation = 90;
2257 constexpr int desiredOrientation = 180;
2258
2259 // --------------------------------------------------------------------
2260 // Preconditions
2261
2262 // A display is set up
2263 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2264 display.inject();
2265
2266 // The current display state has an orientation set
2267 display.mutableCurrentDisplayState().orientation = initialOrientation;
2268
2269 // The incoming request sets a different orientation
2270 DisplayState state;
2271 state.what = DisplayState::eDisplayProjectionChanged;
2272 state.token = display.token();
2273 state.orientation = desiredOrientation;
2274
2275 // --------------------------------------------------------------------
2276 // Invocation
2277
2278 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2279
2280 // --------------------------------------------------------------------
2281 // Postconditions
2282
2283 // The returned flags indicate a transaction is needed
2284 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2285
2286 // The current display state has the new value.
2287 EXPECT_EQ(desiredOrientation, display.getCurrentDisplayState().orientation);
2288}
2289
2290TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfFrameChanged) {
2291 using Case = SimplePrimaryDisplayCase;
2292 const Rect initialFrame = {0, 0, 0, 0};
2293 const Rect desiredFrame = {5, 6, 7, 8};
2294
2295 // --------------------------------------------------------------------
2296 // Preconditions
2297
2298 // A display is set up
2299 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2300 display.inject();
2301
2302 // The current display state does not have a frame
2303 display.mutableCurrentDisplayState().frame = initialFrame;
2304
2305 // The incoming request sets a frame
2306 DisplayState state;
2307 state.what = DisplayState::eDisplayProjectionChanged;
2308 state.token = display.token();
2309 state.frame = desiredFrame;
2310
2311 // --------------------------------------------------------------------
2312 // Invocation
2313
2314 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2315
2316 // --------------------------------------------------------------------
2317 // Postconditions
2318
2319 // The returned flags indicate a transaction is needed
2320 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2321
2322 // The current display state has the new value.
2323 EXPECT_EQ(desiredFrame, display.getCurrentDisplayState().frame);
2324}
2325
2326TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfViewportChanged) {
2327 using Case = SimplePrimaryDisplayCase;
2328 const Rect initialViewport = {0, 0, 0, 0};
2329 const Rect desiredViewport = {5, 6, 7, 8};
2330
2331 // --------------------------------------------------------------------
2332 // Preconditions
2333
2334 // A display is set up
2335 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2336 display.inject();
2337
2338 // The current display state does not have a viewport
2339 display.mutableCurrentDisplayState().viewport = initialViewport;
2340
2341 // The incoming request sets a viewport
2342 DisplayState state;
2343 state.what = DisplayState::eDisplayProjectionChanged;
2344 state.token = display.token();
2345 state.viewport = desiredViewport;
2346
2347 // --------------------------------------------------------------------
2348 // Invocation
2349
2350 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2351
2352 // --------------------------------------------------------------------
2353 // Postconditions
2354
2355 // The returned flags indicate a transaction is needed
2356 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2357
2358 // The current display state has the new value.
2359 EXPECT_EQ(desiredViewport, display.getCurrentDisplayState().viewport);
2360}
2361
2362TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSizeDidNotChange) {
2363 using Case = SimplePrimaryDisplayCase;
2364 constexpr uint32_t initialWidth = 1024;
2365 constexpr uint32_t initialHeight = 768;
2366
2367 // --------------------------------------------------------------------
2368 // Preconditions
2369
2370 // A display is set up
2371 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2372 display.inject();
2373
2374 // The current display state has a size set
2375 display.mutableCurrentDisplayState().width = initialWidth;
2376 display.mutableCurrentDisplayState().height = initialHeight;
2377
2378 // The incoming request sets the same display size
2379 DisplayState state;
2380 state.what = DisplayState::eDisplaySizeChanged;
2381 state.token = display.token();
2382 state.width = initialWidth;
2383 state.height = initialHeight;
2384
2385 // --------------------------------------------------------------------
2386 // Invocation
2387
2388 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2389
2390 // --------------------------------------------------------------------
2391 // Postconditions
2392
2393 // The returned flags are empty
2394 EXPECT_EQ(0u, flags);
2395
2396 // The current display state is unchanged
2397 EXPECT_EQ(initialWidth, display.getCurrentDisplayState().width);
2398 EXPECT_EQ(initialHeight, display.getCurrentDisplayState().height);
2399}
2400
2401TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfWidthChanged) {
2402 using Case = SimplePrimaryDisplayCase;
2403 constexpr uint32_t initialWidth = 0;
2404 constexpr uint32_t desiredWidth = 1024;
2405
2406 // --------------------------------------------------------------------
2407 // Preconditions
2408
2409 // A display is set up
2410 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2411 display.inject();
2412
2413 // The display does not yet have a width
2414 display.mutableCurrentDisplayState().width = initialWidth;
2415
2416 // The incoming request sets a display width
2417 DisplayState state;
2418 state.what = DisplayState::eDisplaySizeChanged;
2419 state.token = display.token();
2420 state.width = desiredWidth;
2421
2422 // --------------------------------------------------------------------
2423 // Invocation
2424
2425 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2426
2427 // --------------------------------------------------------------------
2428 // Postconditions
2429
2430 // The returned flags indicate a transaction is needed
2431 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2432
2433 // The current display state has the new value.
2434 EXPECT_EQ(desiredWidth, display.getCurrentDisplayState().width);
2435}
2436
2437TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfHeightChanged) {
2438 using Case = SimplePrimaryDisplayCase;
2439 constexpr uint32_t initialHeight = 0;
2440 constexpr uint32_t desiredHeight = 768;
2441
2442 // --------------------------------------------------------------------
2443 // Preconditions
2444
2445 // A display is set up
2446 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2447 display.inject();
2448
2449 // The display does not yet have a height
2450 display.mutableCurrentDisplayState().height = initialHeight;
2451
2452 // The incoming request sets a display height
2453 DisplayState state;
2454 state.what = DisplayState::eDisplaySizeChanged;
2455 state.token = display.token();
2456 state.height = desiredHeight;
2457
2458 // --------------------------------------------------------------------
2459 // Invocation
2460
2461 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2462
2463 // --------------------------------------------------------------------
2464 // Postconditions
2465
2466 // The returned flags indicate a transaction is needed
2467 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2468
2469 // The current display state has the new value.
2470 EXPECT_EQ(desiredHeight, display.getCurrentDisplayState().height);
2471}
2472
Lloyd Pique86016da2018-03-01 16:09:38 -08002473/* ------------------------------------------------------------------------
2474 * SurfaceFlinger::onInitializeDisplays
2475 */
2476
2477TEST_F(DisplayTransactionTest, onInitializeDisplaysSetsUpPrimaryDisplay) {
2478 using Case = SimplePrimaryDisplayCase;
2479
2480 // --------------------------------------------------------------------
2481 // Preconditions
2482
2483 // A primary display is set up
2484 Case::Display::injectHwcDisplay(this);
2485 auto primaryDisplay = Case::Display::makeFakeExistingDisplayInjector(this);
2486 primaryDisplay.inject();
2487
2488 // --------------------------------------------------------------------
2489 // Call Expectations
2490
2491 // We expect the surface interceptor to possibly be used, but we treat it as
2492 // disabled since it is called as a side effect rather than directly by this
2493 // function.
2494 EXPECT_CALL(*mSurfaceInterceptor, isEnabled()).WillOnce(Return(false));
2495
2496 // We expect a call to get the active display config.
2497 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
2498
2499 // We expect invalidate() to be invoked once to trigger display transaction
2500 // processing.
2501 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
2502
2503 // --------------------------------------------------------------------
2504 // Invocation
2505
2506 mFlinger.onInitializeDisplays();
2507
2508 // --------------------------------------------------------------------
2509 // Postconditions
2510
2511 // The primary display should have a current state
2512 ASSERT_TRUE(hasCurrentDisplayState(primaryDisplay.token()));
2513 const auto& primaryDisplayState = getCurrentDisplayState(primaryDisplay.token());
2514 // The layer stack state should be set to zero
2515 EXPECT_EQ(0u, primaryDisplayState.layerStack);
2516 // The orientation state should be set to zero
2517 EXPECT_EQ(0, primaryDisplayState.orientation);
2518
2519 // The frame state should be set to INVALID
2520 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.frame);
2521
2522 // The viewport state should be set to INVALID
2523 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.viewport);
2524
2525 // The width and height should both be zero
2526 EXPECT_EQ(0u, primaryDisplayState.width);
2527 EXPECT_EQ(0u, primaryDisplayState.height);
2528
2529 // The display should be set to HWC_POWER_MODE_NORMAL
2530 ASSERT_TRUE(hasDisplayDevice(primaryDisplay.token()));
2531 auto displayDevice = primaryDisplay.mutableDisplayDevice();
2532 EXPECT_EQ(HWC_POWER_MODE_NORMAL, displayDevice->getPowerMode());
2533
2534 // The display refresh period should be set in the frame tracker.
2535 FrameStats stats;
2536 mFlinger.getAnimFrameTracker().getStats(&stats);
2537 EXPECT_EQ(DEFAULT_REFRESH_RATE, stats.refreshPeriodNano);
2538
2539 // The display transaction needed flag should be set.
2540 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
2541
2542 // The compositor timing should be set to default values
2543 const auto& compositorTiming = mFlinger.getCompositorTiming();
2544 EXPECT_EQ(-DEFAULT_REFRESH_RATE, compositorTiming.deadline);
2545 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.interval);
2546 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.presentLatency);
2547}
2548
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002549/* ------------------------------------------------------------------------
2550 * SurfaceFlinger::setPowerModeInternal
2551 */
2552
2553// Used when we simulate a display that supports doze.
Peiyong Lined531a32018-10-26 18:27:56 -07002554template <typename Display>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002555struct DozeIsSupportedVariant {
2556 static constexpr bool DOZE_SUPPORTED = true;
2557 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2558 IComposerClient::PowerMode::DOZE;
2559 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2560 IComposerClient::PowerMode::DOZE_SUSPEND;
Peiyong Lined531a32018-10-26 18:27:56 -07002561
2562 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
2563 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(Display::HWC_DISPLAY_ID, _))
2564 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hwc2::DisplayCapability>(
2565 {Hwc2::DisplayCapability::DOZE})),
2566 Return(Error::NONE)));
2567 }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002568};
2569
Peiyong Lined531a32018-10-26 18:27:56 -07002570template <typename Display>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002571// Used when we simulate a display that does not support doze.
2572struct DozeNotSupportedVariant {
2573 static constexpr bool DOZE_SUPPORTED = false;
2574 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2575 IComposerClient::PowerMode::ON;
2576 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2577 IComposerClient::PowerMode::ON;
Peiyong Lined531a32018-10-26 18:27:56 -07002578
2579 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
2580 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(Display::HWC_DISPLAY_ID, _))
2581 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hwc2::DisplayCapability>({})),
2582 Return(Error::NONE)));
2583 }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002584};
2585
2586struct EventThreadBaseSupportedVariant {
2587 static void setupEventAndEventControlThreadNoCallExpectations(DisplayTransactionTest* test) {
2588 // The event control thread should not be notified.
2589 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(_)).Times(0);
2590
2591 // The event thread should not be notified.
2592 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(0);
2593 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(0);
2594 }
2595};
2596
2597struct EventThreadNotSupportedVariant : public EventThreadBaseSupportedVariant {
2598 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2599 // These calls are only expected for the primary display.
2600
2601 // Instead expect no calls.
2602 setupEventAndEventControlThreadNoCallExpectations(test);
2603 }
2604
2605 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2606 // These calls are only expected for the primary display.
2607
2608 // Instead expect no calls.
2609 setupEventAndEventControlThreadNoCallExpectations(test);
2610 }
2611};
2612
2613struct EventThreadIsSupportedVariant : public EventThreadBaseSupportedVariant {
2614 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2615 // The event control thread should be notified to enable vsyncs
2616 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(true)).Times(1);
2617
2618 // The event thread should be notified that the screen was acquired.
2619 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(1);
2620 }
2621
2622 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2623 // There should be a call to setVsyncEnabled(false)
2624 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(false)).Times(1);
2625
2626 // The event thread should not be notified that the screen was released.
2627 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(1);
2628 }
2629};
2630
Lloyd Pique41be5d22018-06-21 13:11:48 -07002631struct DispSyncIsSupportedVariant {
2632 static void setupBeginResyncCallExpectations(DisplayTransactionTest* test) {
2633 EXPECT_CALL(*test->mPrimaryDispSync, reset()).Times(1);
2634 EXPECT_CALL(*test->mPrimaryDispSync, setPeriod(DEFAULT_REFRESH_RATE)).Times(1);
2635 EXPECT_CALL(*test->mPrimaryDispSync, beginResync()).Times(1);
2636 }
2637
2638 static void setupEndResyncCallExpectations(DisplayTransactionTest* test) {
2639 EXPECT_CALL(*test->mPrimaryDispSync, endResync()).Times(1);
2640 }
2641};
2642
2643struct DispSyncNotSupportedVariant {
2644 static void setupBeginResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2645
2646 static void setupEndResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2647};
2648
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002649// --------------------------------------------------------------------
2650// Note:
2651//
2652// There are a large number of transitions we could test, however we only test a
2653// selected subset which provides complete test coverage of the implementation.
2654// --------------------------------------------------------------------
2655
2656template <int initialPowerMode, int targetPowerMode>
2657struct TransitionVariantCommon {
2658 static constexpr auto INITIAL_POWER_MODE = initialPowerMode;
2659 static constexpr auto TARGET_POWER_MODE = targetPowerMode;
2660
2661 static void verifyPostconditions(DisplayTransactionTest*) {}
2662};
2663
2664struct TransitionOffToOnVariant
2665 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_NORMAL> {
2666 template <typename Case>
2667 static void setupCallExpectations(DisplayTransactionTest* test) {
2668 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2669 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002670 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002671 Case::setupRepaintEverythingCallExpectations(test);
2672 }
2673
2674 static void verifyPostconditions(DisplayTransactionTest* test) {
2675 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2676 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2677 }
2678};
2679
2680struct TransitionOffToDozeSuspendVariant
2681 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_DOZE_SUSPEND> {
2682 template <typename Case>
2683 static void setupCallExpectations(DisplayTransactionTest* test) {
2684 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2685 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2686 Case::setupRepaintEverythingCallExpectations(test);
2687 }
2688
2689 static void verifyPostconditions(DisplayTransactionTest* test) {
2690 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2691 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2692 }
2693};
2694
2695struct TransitionOnToOffVariant
2696 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_OFF> {
2697 template <typename Case>
2698 static void setupCallExpectations(DisplayTransactionTest* test) {
2699 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002700 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002701 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2702 }
2703
2704 static void verifyPostconditions(DisplayTransactionTest* test) {
2705 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2706 }
2707};
2708
2709struct TransitionDozeSuspendToOffVariant
2710 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_OFF> {
2711 template <typename Case>
2712 static void setupCallExpectations(DisplayTransactionTest* test) {
2713 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2714 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2715 }
2716
2717 static void verifyPostconditions(DisplayTransactionTest* test) {
2718 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2719 }
2720};
2721
2722struct TransitionOnToDozeVariant
2723 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE> {
2724 template <typename Case>
2725 static void setupCallExpectations(DisplayTransactionTest* test) {
2726 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2727 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2728 }
2729};
2730
2731struct TransitionDozeSuspendToDozeVariant
2732 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_DOZE> {
2733 template <typename Case>
2734 static void setupCallExpectations(DisplayTransactionTest* test) {
2735 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002736 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002737 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2738 }
2739};
2740
2741struct TransitionDozeToOnVariant
2742 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE, HWC_POWER_MODE_NORMAL> {
2743 template <typename Case>
2744 static void setupCallExpectations(DisplayTransactionTest* test) {
2745 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2746 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2747 }
2748};
2749
2750struct TransitionDozeSuspendToOnVariant
2751 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_NORMAL> {
2752 template <typename Case>
2753 static void setupCallExpectations(DisplayTransactionTest* test) {
2754 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002755 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002756 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2757 }
2758};
2759
2760struct TransitionOnToDozeSuspendVariant
2761 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE_SUSPEND> {
2762 template <typename Case>
2763 static void setupCallExpectations(DisplayTransactionTest* test) {
2764 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002765 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002766 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2767 }
2768};
2769
2770struct TransitionOnToUnknownVariant
2771 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_LEET> {
2772 template <typename Case>
2773 static void setupCallExpectations(DisplayTransactionTest* test) {
2774 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2775 Case::setupNoComposerPowerModeCallExpectations(test);
2776 }
2777};
2778
2779// --------------------------------------------------------------------
2780// Note:
2781//
2782// Rather than testing the cartesian product of of
2783// DozeIsSupported/DozeNotSupported with all other options, we use one for one
2784// display type, and the other for another display type.
2785// --------------------------------------------------------------------
2786
2787template <typename DisplayVariant, typename DozeVariant, typename EventThreadVariant,
Lloyd Pique41be5d22018-06-21 13:11:48 -07002788 typename DispSyncVariant, typename TransitionVariant>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002789struct DisplayPowerCase {
2790 using Display = DisplayVariant;
2791 using Doze = DozeVariant;
2792 using EventThread = EventThreadVariant;
Lloyd Pique41be5d22018-06-21 13:11:48 -07002793 using DispSync = DispSyncVariant;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002794 using Transition = TransitionVariant;
2795
2796 static auto injectDisplayWithInitialPowerMode(DisplayTransactionTest* test, int mode) {
2797 Display::injectHwcDisplay(test);
2798 auto display = Display::makeFakeExistingDisplayInjector(test);
2799 display.inject();
2800 display.mutableDisplayDevice()->setPowerMode(mode);
2801 return display;
2802 }
2803
2804 static void setInitialPrimaryHWVsyncEnabled(DisplayTransactionTest* test, bool enabled) {
2805 test->mFlinger.mutablePrimaryHWVsyncEnabled() = enabled;
2806 }
2807
2808 static void setupRepaintEverythingCallExpectations(DisplayTransactionTest* test) {
2809 EXPECT_CALL(*test->mMessageQueue, invalidate()).Times(1);
2810 }
2811
2812 static void setupSurfaceInterceptorCallExpectations(DisplayTransactionTest* test, int mode) {
2813 EXPECT_CALL(*test->mSurfaceInterceptor, isEnabled()).WillOnce(Return(true));
2814 EXPECT_CALL(*test->mSurfaceInterceptor, savePowerModeUpdate(_, mode)).Times(1);
2815 }
2816
2817 static void setupComposerCallExpectations(DisplayTransactionTest* test,
2818 IComposerClient::PowerMode mode) {
2819 // Any calls to get the active config will return a default value.
2820 EXPECT_CALL(*test->mComposer, getActiveConfig(Display::HWC_DISPLAY_ID, _))
2821 .WillRepeatedly(DoAll(SetArgPointee<1>(Display::HWC_ACTIVE_CONFIG_ID),
2822 Return(Error::NONE)));
2823
2824 // Any calls to get whether the display supports dozing will return the value set by the
2825 // policy variant.
2826 EXPECT_CALL(*test->mComposer, getDozeSupport(Display::HWC_DISPLAY_ID, _))
2827 .WillRepeatedly(DoAll(SetArgPointee<1>(Doze::DOZE_SUPPORTED), Return(Error::NONE)));
2828
2829 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, mode)).Times(1);
2830 }
2831
2832 static void setupNoComposerPowerModeCallExpectations(DisplayTransactionTest* test) {
2833 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, _)).Times(0);
2834 }
2835};
2836
2837// A sample configuration for the primary display.
2838// In addition to having event thread support, we emulate doze support.
2839template <typename TransitionVariant>
Peiyong Lined531a32018-10-26 18:27:56 -07002840using PrimaryDisplayPowerCase =
2841 DisplayPowerCase<PrimaryDisplayVariant, DozeIsSupportedVariant<PrimaryDisplayVariant>,
2842 EventThreadIsSupportedVariant, DispSyncIsSupportedVariant,
2843 TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002844
2845// A sample configuration for the external display.
2846// In addition to not having event thread support, we emulate not having doze
2847// support.
2848template <typename TransitionVariant>
Peiyong Lined531a32018-10-26 18:27:56 -07002849using ExternalDisplayPowerCase =
2850 DisplayPowerCase<ExternalDisplayVariant, DozeNotSupportedVariant<ExternalDisplayVariant>,
2851 EventThreadNotSupportedVariant, DispSyncNotSupportedVariant,
2852 TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002853
2854class SetPowerModeInternalTest : public DisplayTransactionTest {
2855public:
2856 template <typename Case>
2857 void transitionDisplayCommon();
2858};
2859
2860template <int PowerMode>
2861struct PowerModeInitialVSyncEnabled : public std::false_type {};
2862
2863template <>
2864struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_NORMAL> : public std::true_type {};
2865
2866template <>
2867struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_DOZE> : public std::true_type {};
2868
2869template <typename Case>
2870void SetPowerModeInternalTest::transitionDisplayCommon() {
2871 // --------------------------------------------------------------------
2872 // Preconditions
2873
Peiyong Lined531a32018-10-26 18:27:56 -07002874 Case::Doze::setupComposerCallExpectations(this);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002875 auto display =
2876 Case::injectDisplayWithInitialPowerMode(this, Case::Transition::INITIAL_POWER_MODE);
2877 Case::setInitialPrimaryHWVsyncEnabled(this,
2878 PowerModeInitialVSyncEnabled<
2879 Case::Transition::INITIAL_POWER_MODE>::value);
2880
2881 // --------------------------------------------------------------------
2882 // Call Expectations
2883
2884 Case::setupSurfaceInterceptorCallExpectations(this, Case::Transition::TARGET_POWER_MODE);
2885 Case::Transition::template setupCallExpectations<Case>(this);
2886
2887 // --------------------------------------------------------------------
2888 // Invocation
2889
2890 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(),
2891 Case::Transition::TARGET_POWER_MODE);
2892
2893 // --------------------------------------------------------------------
2894 // Postconditions
2895
2896 Case::Transition::verifyPostconditions(this);
2897}
2898
2899TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfNoChange) {
2900 using Case = SimplePrimaryDisplayCase;
2901
2902 // --------------------------------------------------------------------
2903 // Preconditions
2904
2905 // A primary display device is set up
2906 Case::Display::injectHwcDisplay(this);
2907 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2908 display.inject();
2909
Dominik Laskowskia2edf612018-06-01 13:15:16 -07002910 // The display is already set to HWC_POWER_MODE_NORMAL
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002911 display.mutableDisplayDevice()->setPowerMode(HWC_POWER_MODE_NORMAL);
2912
2913 // --------------------------------------------------------------------
2914 // Invocation
2915
2916 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_NORMAL);
2917
2918 // --------------------------------------------------------------------
2919 // Postconditions
2920
2921 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2922}
2923
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002924TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfVirtualDisplay) {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002925 using Case = HwcVirtualDisplayCase;
2926
2927 // --------------------------------------------------------------------
2928 // Preconditions
2929
Dominik Laskowski075d3172018-05-24 15:50:06 -07002930 // Insert display data so that the HWC thinks it created the virtual display.
2931 const auto displayId = Case::Display::DISPLAY_ID::get();
2932 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08002933 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002934
2935 // A virtual display device is set up
2936 Case::Display::injectHwcDisplay(this);
2937 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2938 display.inject();
2939
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002940 // The display is set to HWC_POWER_MODE_NORMAL
2941 getDisplayDevice(display.token())->setPowerMode(HWC_POWER_MODE_NORMAL);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002942
2943 // --------------------------------------------------------------------
2944 // Invocation
2945
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002946 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_OFF);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002947
2948 // --------------------------------------------------------------------
2949 // Postconditions
2950
2951 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2952}
2953
2954TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnPrimaryDisplay) {
2955 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToOnVariant>>();
2956}
2957
2958TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendPrimaryDisplay) {
2959 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
2960}
2961
2962TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffPrimaryDisplay) {
2963 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToOffVariant>>();
2964}
2965
2966TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffPrimaryDisplay) {
2967 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
2968}
2969
2970TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozePrimaryDisplay) {
2971 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeVariant>>();
2972}
2973
2974TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozePrimaryDisplay) {
2975 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
2976}
2977
2978TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnPrimaryDisplay) {
2979 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeToOnVariant>>();
2980}
2981
2982TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnPrimaryDisplay) {
2983 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
2984}
2985
2986TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendPrimaryDisplay) {
2987 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
2988}
2989
2990TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownPrimaryDisplay) {
2991 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToUnknownVariant>>();
2992}
2993
2994TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnExternalDisplay) {
2995 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToOnVariant>>();
2996}
2997
2998TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendExternalDisplay) {
2999 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
3000}
3001
3002TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffExternalDisplay) {
3003 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToOffVariant>>();
3004}
3005
3006TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffExternalDisplay) {
3007 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
3008}
3009
3010TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeExternalDisplay) {
3011 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeVariant>>();
3012}
3013
3014TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozeExternalDisplay) {
3015 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
3016}
3017
3018TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnExternalDisplay) {
3019 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeToOnVariant>>();
3020}
3021
3022TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnExternalDisplay) {
3023 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
3024}
3025
3026TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendExternalDisplay) {
3027 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
3028}
3029
3030TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownExternalDisplay) {
3031 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToUnknownVariant>>();
3032}
3033
Lloyd Piquef58625d2017-12-19 13:22:33 -08003034} // namespace
3035} // namespace android