blob: 03237789799a0b9c1c8d9d01edab5bb695dc6538 [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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Lloyd Piquef58625d2017-12-19 13:22:33 -080021#undef LOG_TAG
22#define LOG_TAG "LibSurfaceFlingerUnittests"
23
Dominik Laskowski075d3172018-05-24 15:50:06 -070024#include <type_traits>
25
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070026#include <compositionengine/Display.h>
27#include <compositionengine/DisplayColorProfile.h>
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070028#include <compositionengine/impl/Display.h>
Lloyd Pique33050472019-12-19 17:12:44 -080029#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070030#include <compositionengine/mock/Display.h>
31#include <compositionengine/mock/DisplayColorProfile.h>
Lloyd Pique542307f2018-10-19 13:24:08 -070032#include <compositionengine/mock/DisplaySurface.h>
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070033#include <compositionengine/mock/RenderSurface.h>
Lloyd Piquef58625d2017-12-19 13:22:33 -080034#include <gmock/gmock.h>
35#include <gtest/gtest.h>
Lloyd Pique1ebe0902019-10-04 14:47:13 -070036#include <gui/mock/GraphicBufferConsumer.h>
37#include <gui/mock/GraphicBufferProducer.h>
Lloyd Piquef58625d2017-12-19 13:22:33 -080038#include <log/log.h>
Lloyd Pique3823e7b2018-10-18 16:58:10 -070039#include <renderengine/mock/RenderEngine.h>
Valerie Hau9758ae02018-10-09 16:05:09 -070040#include <ui/DebugUtils.h>
Dominik Laskowski075d3172018-05-24 15:50:06 -070041
42#include "DisplayIdentificationTest.h"
Ana Krulecafb45842019-02-13 13:33:03 -080043#include "TestableScheduler.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080044#include "TestableSurfaceFlinger.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080045#include "mock/DisplayHardware/MockComposer.h"
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070046#include "mock/DisplayHardware/MockPowerAdvisor.h"
Lloyd Pique41be5d22018-06-21 13:11:48 -070047#include "mock/MockDispSync.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080048#include "mock/MockEventControlThread.h"
49#include "mock/MockEventThread.h"
50#include "mock/MockMessageQueue.h"
51#include "mock/MockNativeWindowSurface.h"
52#include "mock/MockSurfaceInterceptor.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080053#include "mock/system/window/MockNativeWindow.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080054
55namespace android {
56namespace {
57
Peiyong Line9d809e2020-04-14 13:10:48 -070058namespace hal = android::hardware::graphics::composer::hal;
59
Lloyd Piquee39cad22017-12-20 17:01:29 -080060using testing::_;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070061using testing::AnyNumber;
Lloyd Piquee39cad22017-12-20 17:01:29 -080062using testing::DoAll;
63using testing::Mock;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080064using testing::ResultOf;
Lloyd Piquee39cad22017-12-20 17:01:29 -080065using testing::Return;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070066using testing::ReturnRefOfCopy;
Lloyd Piquee39cad22017-12-20 17:01:29 -080067using testing::SetArgPointee;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070068using testing::StrictMock;
Lloyd Piquee39cad22017-12-20 17:01:29 -080069
Peiyong Line9d809e2020-04-14 13:10:48 -070070using hal::ColorMode;
71using hal::Connection;
72using hal::DisplayCapability;
73using hal::DisplayType;
74using hal::Error;
75using hal::Hdr;
76using hal::HWDisplayId;
77using hal::IComposer;
78using hal::IComposerClient;
79using hal::PerFrameMetadataKey;
80using hal::PowerMode;
81using hal::RenderIntent;
Lloyd Piquee39cad22017-12-20 17:01:29 -080082
Lloyd Piquec11e0d32018-01-22 18:44:59 -080083using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector;
84using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
Lloyd Pique1fa4d462018-01-22 18:03:16 -080085using HotplugEvent = TestableSurfaceFlinger::HotplugEvent;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080086using HWC2Display = TestableSurfaceFlinger::HWC2Display;
Lloyd Piquebc792092018-01-17 11:52:30 -080087
Ady Abrahamf69d11d2020-07-24 11:09:55 -070088constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'667;
Lloyd Piquee39cad22017-12-20 17:01:29 -080089constexpr int32_t DEFAULT_DPI = 320;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080090constexpr int DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT = HAL_PIXEL_FORMAT_RGB_565;
Lloyd Piquee39cad22017-12-20 17:01:29 -080091
Peiyong Lin65248e02020-04-18 21:15:07 -070092constexpr int POWER_MODE_LEET = 1337; // An out of range power mode value
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -080093
Lloyd Piquec11e0d32018-01-22 18:44:59 -080094/* ------------------------------------------------------------------------
95 * Boolean avoidance
96 *
97 * To make calls and template instantiations more readable, we define some
98 * local enums along with an implicit bool conversion.
99 */
100
101#define BOOL_SUBSTITUTE(TYPENAME) enum class TYPENAME : bool { FALSE = false, TRUE = true };
102
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800103BOOL_SUBSTITUTE(Async);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700104BOOL_SUBSTITUTE(Critical);
105BOOL_SUBSTITUTE(Primary);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800106BOOL_SUBSTITUTE(Secure);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700107BOOL_SUBSTITUTE(Virtual);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800108
109/* ------------------------------------------------------------------------
110 *
111 */
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800112
Lloyd Piquef58625d2017-12-19 13:22:33 -0800113class DisplayTransactionTest : public testing::Test {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800114public:
Lloyd Piquef58625d2017-12-19 13:22:33 -0800115 DisplayTransactionTest();
116 ~DisplayTransactionTest() override;
117
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800118 // --------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800119 // Mock/Fake injection
Lloyd Piquef58625d2017-12-19 13:22:33 -0800120
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700121 void injectMockScheduler();
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800122 void injectMockComposer(int virtualDisplayCount);
123 void injectFakeBufferQueueFactory();
124 void injectFakeNativeWindowSurfaceFactory();
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700125 sp<DisplayDevice> injectDefaultInternalDisplay(std::function<void(FakeDisplayDeviceInjector&)>);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800126
127 // --------------------------------------------------------------------
128 // Postcondition helpers
129
Peiyong Line9d809e2020-04-14 13:10:48 -0700130 bool hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800131 bool hasTransactionFlagSet(int flag);
132 bool hasDisplayDevice(sp<IBinder> displayToken);
133 sp<DisplayDevice> getDisplayDevice(sp<IBinder> displayToken);
134 bool hasCurrentDisplayState(sp<IBinder> displayToken);
135 const DisplayDeviceState& getCurrentDisplayState(sp<IBinder> displayToken);
136 bool hasDrawingDisplayState(sp<IBinder> displayToken);
137 const DisplayDeviceState& getDrawingDisplayState(sp<IBinder> displayToken);
138
139 // --------------------------------------------------------------------
140 // Test instances
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800141
Lloyd Piquef58625d2017-12-19 13:22:33 -0800142 TestableSurfaceFlinger mFlinger;
Alec Mouriba013fa2018-10-16 12:43:11 -0700143 sp<mock::NativeWindow> mNativeWindow = new mock::NativeWindow();
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800144 sp<GraphicBuffer> mBuffer = new GraphicBuffer();
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700145 Hwc2::mock::PowerAdvisor mPowerAdvisor;
Lloyd Piquee39cad22017-12-20 17:01:29 -0800146
147 // These mocks are created by the test, but are destroyed by SurfaceFlinger
148 // by virtue of being stored into a std::unique_ptr. However we still need
149 // to keep a reference to them for use in setting up call expectations.
Peiyong Lin833074a2018-08-28 11:53:54 -0700150 renderengine::mock::RenderEngine* mRenderEngine = new renderengine::mock::RenderEngine();
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800151 Hwc2::mock::Composer* mComposer = nullptr;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800152 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
153 mock::SurfaceInterceptor* mSurfaceInterceptor = new mock::SurfaceInterceptor();
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700154
155 mock::DispSync* mPrimaryDispSync = new mock::DispSync;
156 mock::EventControlThread* mEventControlThread = new mock::EventControlThread;
157 mock::EventThread* mEventThread = new mock::EventThread;
158 mock::EventThread* mSFEventThread = new mock::EventThread;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800159
160 // These mocks are created only when expected to be created via a factory.
161 sp<mock::GraphicBufferConsumer> mConsumer;
162 sp<mock::GraphicBufferProducer> mProducer;
Lloyd Pique22098362018-09-13 11:46:49 -0700163 surfaceflinger::mock::NativeWindowSurface* mNativeWindowSurface = nullptr;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800164};
165
166DisplayTransactionTest::DisplayTransactionTest() {
167 const ::testing::TestInfo* const test_info =
168 ::testing::UnitTest::GetInstance()->current_test_info();
169 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800170
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800171 // Default to no wide color display support configured
172 mFlinger.mutableHasWideColorDisplay() = false;
Peiyong Lin13effd12018-07-24 17:01:47 -0700173 mFlinger.mutableUseColorManagement() = false;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800174 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800175
176 // Default to using HWC virtual displays
177 mFlinger.mutableUseHwcVirtualDisplays() = true;
178
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800179 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
180 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
181 });
182
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800183 mFlinger.setCreateNativeWindowSurface([](auto) {
184 ADD_FAILURE() << "Unexpected request to create a native window surface.";
185 return nullptr;
186 });
187
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700188 injectMockScheduler();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800189 mFlinger.mutableEventQueue().reset(mMessageQueue);
Peiyong Lin833074a2018-08-28 11:53:54 -0700190 mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800191 mFlinger.mutableInterceptor().reset(mSurfaceInterceptor);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800192
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800193 injectMockComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -0800194}
195
196DisplayTransactionTest::~DisplayTransactionTest() {
197 const ::testing::TestInfo* const test_info =
198 ::testing::UnitTest::GetInstance()->current_test_info();
199 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
200}
201
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700202void DisplayTransactionTest::injectMockScheduler() {
Ana Krulecafb45842019-02-13 13:33:03 -0800203 EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_));
Dominik Laskowski98041832019-08-01 18:35:59 -0700204 EXPECT_CALL(*mEventThread, createEventConnection(_, _))
205 .WillOnce(Return(new EventThreadConnection(mEventThread, ResyncCallback(),
206 ISurfaceComposer::eConfigChangedSuppress)));
207
Ana Krulecafb45842019-02-13 13:33:03 -0800208 EXPECT_CALL(*mSFEventThread, registerDisplayEventConnection(_));
Dominik Laskowski98041832019-08-01 18:35:59 -0700209 EXPECT_CALL(*mSFEventThread, createEventConnection(_, _))
210 .WillOnce(Return(new EventThreadConnection(mSFEventThread, ResyncCallback(),
211 ISurfaceComposer::eConfigChangedSuppress)));
Ana Krulecafb45842019-02-13 13:33:03 -0800212
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700213 mFlinger.setupScheduler(std::unique_ptr<DispSync>(mPrimaryDispSync),
214 std::unique_ptr<EventControlThread>(mEventControlThread),
215 std::unique_ptr<EventThread>(mEventThread),
216 std::unique_ptr<EventThread>(mSFEventThread));
Ana Krulecafb45842019-02-13 13:33:03 -0800217}
218
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800219void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
220 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800221 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
222 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -0800223
Lloyd Piquee39cad22017-12-20 17:01:29 -0800224 Mock::VerifyAndClear(mComposer);
225}
226
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800227void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800228 // This setup is only expected once per test.
229 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
230
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800231 mConsumer = new mock::GraphicBufferConsumer();
232 mProducer = new mock::GraphicBufferProducer();
233
234 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
235 *outProducer = mProducer;
236 *outConsumer = mConsumer;
237 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800238}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800239
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800240void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800241 // This setup is only expected once per test.
242 ASSERT_TRUE(mNativeWindowSurface == nullptr);
243
Lloyd Pique22098362018-09-13 11:46:49 -0700244 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800245
Lloyd Pique22098362018-09-13 11:46:49 -0700246 mFlinger.setCreateNativeWindowSurface([this](auto) {
247 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
248 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800249}
250
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700251sp<DisplayDevice> DisplayTransactionTest::injectDefaultInternalDisplay(
252 std::function<void(FakeDisplayDeviceInjector&)> injectExtra) {
253 constexpr DisplayId DEFAULT_DISPLAY_ID = DisplayId{777};
254 constexpr int DEFAULT_DISPLAY_WIDTH = 1080;
255 constexpr int DEFAULT_DISPLAY_HEIGHT = 1920;
Peiyong Line9d809e2020-04-14 13:10:48 -0700256 constexpr HWDisplayId DEFAULT_DISPLAY_HWC_DISPLAY_ID = 0;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700257
258 // The DisplayDevice is required to have a framebuffer (behind the
259 // ANativeWindow interface) which uses the actual hardware display
260 // size.
261 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
262 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_DISPLAY_WIDTH), Return(0)));
263 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
264 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_DISPLAY_HEIGHT), Return(0)));
265 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT));
266 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT));
267 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64));
268 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT)).Times(AnyNumber());
269
270 auto compositionDisplay = compositionengine::impl::
271 createDisplay(mFlinger.getCompositionEngine(),
272 compositionengine::DisplayCreationArgsBuilder()
273 .setPhysical(
274 {DEFAULT_DISPLAY_ID, DisplayConnectionType::Internal})
275 .setPixels({DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT})
276 .setPowerAdvisor(&mPowerAdvisor)
277 .build());
278
279 auto injector =
280 FakeDisplayDeviceInjector(mFlinger, compositionDisplay, DisplayConnectionType::Internal,
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100281 DEFAULT_DISPLAY_HWC_DISPLAY_ID, true /* isPrimary */);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700282
283 injector.setNativeWindow(mNativeWindow);
284 if (injectExtra) {
285 injectExtra(injector);
286 }
287
288 auto displayDevice = injector.inject();
289
290 Mock::VerifyAndClear(mNativeWindow.get());
291
292 return displayDevice;
293}
294
Peiyong Line9d809e2020-04-14 13:10:48 -0700295bool DisplayTransactionTest::hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700296 return mFlinger.mutableHwcPhysicalDisplayIdMap().count(hwcDisplayId) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800297}
298
299bool DisplayTransactionTest::hasTransactionFlagSet(int flag) {
300 return mFlinger.mutableTransactionFlags() & flag;
301}
302
303bool DisplayTransactionTest::hasDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700304 return mFlinger.mutableDisplays().count(displayToken) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800305}
306
307sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700308 return mFlinger.mutableDisplays()[displayToken];
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800309}
310
311bool DisplayTransactionTest::hasCurrentDisplayState(sp<IBinder> displayToken) {
312 return mFlinger.mutableCurrentState().displays.indexOfKey(displayToken) >= 0;
313}
314
315const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(sp<IBinder> displayToken) {
316 return mFlinger.mutableCurrentState().displays.valueFor(displayToken);
317}
318
319bool DisplayTransactionTest::hasDrawingDisplayState(sp<IBinder> displayToken) {
320 return mFlinger.mutableDrawingState().displays.indexOfKey(displayToken) >= 0;
321}
322
323const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(sp<IBinder> displayToken) {
324 return mFlinger.mutableDrawingState().displays.valueFor(displayToken);
325}
326
327/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800328 *
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800329 */
330
Dominik Laskowski075d3172018-05-24 15:50:06 -0700331template <typename PhysicalDisplay>
332struct PhysicalDisplayId {};
333
Dominik Laskowski34157762018-10-31 13:07:19 -0700334template <DisplayId::Type displayId>
335using VirtualDisplayId = std::integral_constant<DisplayId::Type, displayId>;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700336
337struct NoDisplayId {};
338
339template <typename>
340struct IsPhysicalDisplayId : std::bool_constant<false> {};
341
342template <typename PhysicalDisplay>
343struct IsPhysicalDisplayId<PhysicalDisplayId<PhysicalDisplay>> : std::bool_constant<true> {};
344
345template <typename>
346struct DisplayIdGetter;
347
348template <typename PhysicalDisplay>
349struct DisplayIdGetter<PhysicalDisplayId<PhysicalDisplay>> {
350 static std::optional<DisplayId> get() {
351 if (!PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
352 return getFallbackDisplayId(static_cast<bool>(PhysicalDisplay::PRIMARY)
Peiyong Lin65248e02020-04-18 21:15:07 -0700353 ? LEGACY_DISPLAY_TYPE_PRIMARY
354 : LEGACY_DISPLAY_TYPE_EXTERNAL);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700355 }
356
357 const auto info =
358 parseDisplayIdentificationData(PhysicalDisplay::PORT,
359 PhysicalDisplay::GET_IDENTIFICATION_DATA());
360 return info ? std::make_optional(info->id) : std::nullopt;
361 }
362};
363
Dominik Laskowski34157762018-10-31 13:07:19 -0700364template <DisplayId::Type displayId>
Dominik Laskowski075d3172018-05-24 15:50:06 -0700365struct DisplayIdGetter<VirtualDisplayId<displayId>> {
Dominik Laskowski34157762018-10-31 13:07:19 -0700366 static std::optional<DisplayId> get() { return DisplayId{displayId}; }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700367};
368
369template <>
370struct DisplayIdGetter<NoDisplayId> {
371 static std::optional<DisplayId> get() { return {}; }
372};
373
Dominik Laskowski55c85402020-01-21 16:25:47 -0800374template <typename>
375struct DisplayConnectionTypeGetter {
376 static constexpr std::optional<DisplayConnectionType> value;
377};
378
379template <typename PhysicalDisplay>
380struct DisplayConnectionTypeGetter<PhysicalDisplayId<PhysicalDisplay>> {
381 static constexpr std::optional<DisplayConnectionType> value = PhysicalDisplay::CONNECTION_TYPE;
382};
383
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100384template <typename>
385struct HwcDisplayIdGetter {
Peiyong Line9d809e2020-04-14 13:10:48 -0700386 static constexpr std::optional<HWDisplayId> value;
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100387};
388
Peiyong Line9d809e2020-04-14 13:10:48 -0700389constexpr HWDisplayId HWC_VIRTUAL_DISPLAY_HWC_DISPLAY_ID = 1010;
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100390
391template <DisplayId::Type displayId>
392struct HwcDisplayIdGetter<VirtualDisplayId<displayId>> {
Peiyong Line9d809e2020-04-14 13:10:48 -0700393 static constexpr std::optional<HWDisplayId> value = HWC_VIRTUAL_DISPLAY_HWC_DISPLAY_ID;
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100394};
395
396template <typename PhysicalDisplay>
397struct HwcDisplayIdGetter<PhysicalDisplayId<PhysicalDisplay>> {
Peiyong Line9d809e2020-04-14 13:10:48 -0700398 static constexpr std::optional<HWDisplayId> value = PhysicalDisplay::HWC_DISPLAY_ID;
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100399};
400
Dominik Laskowski075d3172018-05-24 15:50:06 -0700401// DisplayIdType can be:
402// 1) PhysicalDisplayId<...> for generated ID of physical display backed by HWC.
403// 2) VirtualDisplayId<...> for hard-coded ID of virtual display backed by HWC.
404// 3) NoDisplayId for virtual display without HWC backing.
405template <typename DisplayIdType, int width, int height, Critical critical, Async async,
406 Secure secure, Primary primary, int grallocUsage>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800407struct DisplayVariant {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700408 using DISPLAY_ID = DisplayIdGetter<DisplayIdType>;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800409 using CONNECTION_TYPE = DisplayConnectionTypeGetter<DisplayIdType>;
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100410 using HWC_DISPLAY_ID_OPT = HwcDisplayIdGetter<DisplayIdType>;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700411
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800412 // The display width and height
413 static constexpr int WIDTH = width;
414 static constexpr int HEIGHT = height;
415
416 static constexpr int GRALLOC_USAGE = grallocUsage;
417
Dominik Laskowski075d3172018-05-24 15:50:06 -0700418 // Whether the display is virtual or physical
419 static constexpr Virtual VIRTUAL =
420 IsPhysicalDisplayId<DisplayIdType>{} ? Virtual::FALSE : Virtual::TRUE;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800421
422 // When creating native window surfaces for the framebuffer, whether those should be critical
423 static constexpr Critical CRITICAL = critical;
424
425 // When creating native window surfaces for the framebuffer, whether those should be async
426 static constexpr Async ASYNC = async;
427
428 // Whether the display should be treated as secure
429 static constexpr Secure SECURE = secure;
430
Dominik Laskowski075d3172018-05-24 15:50:06 -0700431 // Whether the display is primary
432 static constexpr Primary PRIMARY = primary;
433
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800434 static auto makeFakeExistingDisplayInjector(DisplayTransactionTest* test) {
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700435 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder();
436 if (auto displayId = DISPLAY_ID::get()) {
437 ceDisplayArgs.setPhysical({*displayId, DisplayConnectionType::Internal});
438 } else {
439 ceDisplayArgs.setUseHwcVirtualDisplays(false);
440 }
441 ceDisplayArgs.setPixels({WIDTH, HEIGHT}).setPowerAdvisor(&test->mPowerAdvisor).build();
442
443 auto compositionDisplay =
444 compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
445 ceDisplayArgs.build());
446
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100447 auto injector = FakeDisplayDeviceInjector(test->mFlinger, compositionDisplay,
448 CONNECTION_TYPE::value, HWC_DISPLAY_ID_OPT::value,
449 static_cast<bool>(PRIMARY));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700450
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800451 injector.setSecure(static_cast<bool>(SECURE));
Alec Mouriba013fa2018-10-16 12:43:11 -0700452 injector.setNativeWindow(test->mNativeWindow);
453
454 // Creating a DisplayDevice requires getting default dimensions from the
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800455 // native window along with some other initial setup.
Alec Mouriba013fa2018-10-16 12:43:11 -0700456 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
457 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
458 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
459 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800460 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT))
461 .WillRepeatedly(Return(0));
462 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT))
463 .WillRepeatedly(Return(0));
464 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64))
465 .WillRepeatedly(Return(0));
chaviw8beb4142019-04-11 13:09:05 -0700466 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT))
467 .WillRepeatedly(Return(0));
468
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800469 return injector;
470 }
471
472 // Called by tests to set up any native window creation call expectations.
473 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
474 EXPECT_CALL(*test->mNativeWindowSurface, getNativeWindow())
475 .WillOnce(Return(test->mNativeWindow));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800476
Alec Mouriba013fa2018-10-16 12:43:11 -0700477 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
478 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
479 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
480 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800481 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT))
482 .WillRepeatedly(Return(0));
483 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT))
484 .WillRepeatedly(Return(0));
485 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64))
486 .WillRepeatedly(Return(0));
chaviw8beb4142019-04-11 13:09:05 -0700487 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT))
488 .WillRepeatedly(Return(0));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800489 }
490
491 static void setupFramebufferConsumerBufferQueueCallExpectations(DisplayTransactionTest* test) {
492 EXPECT_CALL(*test->mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR));
493 EXPECT_CALL(*test->mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR));
494 EXPECT_CALL(*test->mConsumer, setConsumerUsageBits(GRALLOC_USAGE))
495 .WillRepeatedly(Return(NO_ERROR));
496 EXPECT_CALL(*test->mConsumer, setDefaultBufferSize(WIDTH, HEIGHT))
497 .WillRepeatedly(Return(NO_ERROR));
498 EXPECT_CALL(*test->mConsumer, setMaxAcquiredBufferCount(_))
499 .WillRepeatedly(Return(NO_ERROR));
500 }
501
502 static void setupFramebufferProducerBufferQueueCallExpectations(DisplayTransactionTest* test) {
503 EXPECT_CALL(*test->mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return());
504 }
505};
506
Peiyong Line9d809e2020-04-14 13:10:48 -0700507template <HWDisplayId hwcDisplayId, DisplayType hwcDisplayType, typename DisplayVariant,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700508 typename PhysicalDisplay = void>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800509struct HwcDisplayVariant {
510 // The display id supplied by the HWC
Peiyong Line9d809e2020-04-14 13:10:48 -0700511 static constexpr HWDisplayId HWC_DISPLAY_ID = hwcDisplayId;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800512
513 // The HWC display type
Peiyong Line9d809e2020-04-14 13:10:48 -0700514 static constexpr DisplayType HWC_DISPLAY_TYPE = hwcDisplayType;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800515
516 // The HWC active configuration id
Lloyd Pique3c085a02018-05-09 19:38:32 -0700517 static constexpr int HWC_ACTIVE_CONFIG_ID = 2001;
Peiyong Lin65248e02020-04-18 21:15:07 -0700518 static constexpr PowerMode INIT_POWER_MODE = PowerMode::ON;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800519
Peiyong Line9d809e2020-04-14 13:10:48 -0700520 static void injectPendingHotplugEvent(DisplayTransactionTest* test, Connection connection) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800521 test->mFlinger.mutablePendingHotplugEvents().emplace_back(
522 HotplugEvent{HWC_DISPLAY_ID, connection});
523 }
524
525 // Called by tests to inject a HWC display setup
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800526 static void injectHwcDisplayWithNoDefaultCapabilities(DisplayTransactionTest* test) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700527 const auto displayId = DisplayVariant::DISPLAY_ID::get();
528 ASSERT_TRUE(displayId);
529 FakeHwcDisplayInjector(*displayId, HWC_DISPLAY_TYPE,
530 static_cast<bool>(DisplayVariant::PRIMARY))
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800531 .setHwcDisplayId(HWC_DISPLAY_ID)
532 .setWidth(DisplayVariant::WIDTH)
533 .setHeight(DisplayVariant::HEIGHT)
534 .setActiveConfig(HWC_ACTIVE_CONFIG_ID)
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700535 .setPowerMode(INIT_POWER_MODE)
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800536 .inject(&test->mFlinger, test->mComposer);
537 }
538
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800539 // Called by tests to inject a HWC display setup
540 static void injectHwcDisplay(DisplayTransactionTest* test) {
541 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(HWC_DISPLAY_ID, _))
Peiyong Line9d809e2020-04-14 13:10:48 -0700542 .WillOnce(DoAll(SetArgPointee<1>(std::vector<DisplayCapability>({})),
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800543 Return(Error::NONE)));
Peiyong Lin65248e02020-04-18 21:15:07 -0700544 EXPECT_CALL(*test->mComposer, setPowerMode(HWC_DISPLAY_ID, INIT_POWER_MODE))
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700545 .WillOnce(Return(Error::NONE));
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800546 injectHwcDisplayWithNoDefaultCapabilities(test);
547 }
548
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700549 static std::shared_ptr<compositionengine::Display> injectCompositionDisplay(
550 DisplayTransactionTest* test) {
551 const ::testing::TestInfo* const test_info =
552 ::testing::UnitTest::GetInstance()->current_test_info();
553
554 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder()
555 .setPhysical({*DisplayVariant::DISPLAY_ID::get(),
556 PhysicalDisplay::CONNECTION_TYPE})
557 .setPixels({DisplayVariant::WIDTH, DisplayVariant::HEIGHT})
558 .setIsSecure(static_cast<bool>(DisplayVariant::SECURE))
559 .setPowerAdvisor(&test->mPowerAdvisor)
560 .setName(std::string("Injected display for ") +
561 test_info->test_case_name() + "." + test_info->name())
562 .build();
563
564 return compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
565 ceDisplayArgs);
566 }
567
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800568 static void setupHwcHotplugCallExpectations(DisplayTransactionTest* test) {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800569 constexpr auto CONNECTION_TYPE =
570 PhysicalDisplay::CONNECTION_TYPE == DisplayConnectionType::Internal
571 ? IComposerClient::DisplayConnectionType::INTERNAL
572 : IComposerClient::DisplayConnectionType::EXTERNAL;
573
574 EXPECT_CALL(*test->mComposer, getDisplayConnectionType(HWC_DISPLAY_ID, _))
Peiyong Line9d809e2020-04-14 13:10:48 -0700575 .WillOnce(DoAll(SetArgPointee<1>(CONNECTION_TYPE), Return(hal::V2_4::Error::NONE)));
Dominik Laskowski55c85402020-01-21 16:25:47 -0800576
Peiyong Line9d809e2020-04-14 13:10:48 -0700577 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_))
578 .WillOnce(Return(hal::Error::NONE));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800579 EXPECT_CALL(*test->mComposer, getDisplayConfigs(HWC_DISPLAY_ID, _))
580 .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{HWC_ACTIVE_CONFIG_ID}),
581 Return(Error::NONE)));
582 EXPECT_CALL(*test->mComposer,
583 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
584 IComposerClient::Attribute::WIDTH, _))
585 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::WIDTH), Return(Error::NONE)));
586 EXPECT_CALL(*test->mComposer,
587 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
588 IComposerClient::Attribute::HEIGHT, _))
589 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::HEIGHT), Return(Error::NONE)));
590 EXPECT_CALL(*test->mComposer,
591 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
592 IComposerClient::Attribute::VSYNC_PERIOD, _))
593 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE)));
594 EXPECT_CALL(*test->mComposer,
595 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
596 IComposerClient::Attribute::DPI_X, _))
597 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
598 EXPECT_CALL(*test->mComposer,
599 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
600 IComposerClient::Attribute::DPI_Y, _))
601 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
Ady Abraham7159f572019-10-11 11:10:18 -0700602 EXPECT_CALL(*test->mComposer,
603 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
604 IComposerClient::Attribute::CONFIG_GROUP, _))
605 .WillOnce(DoAll(SetArgPointee<3>(-1), Return(Error::NONE)));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700606
607 if (PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
608 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
609 .WillOnce(DoAll(SetArgPointee<1>(PhysicalDisplay::PORT),
610 SetArgPointee<2>(PhysicalDisplay::GET_IDENTIFICATION_DATA()),
611 Return(Error::NONE)));
612 } else {
613 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
614 .WillOnce(Return(Error::UNSUPPORTED));
615 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800616 }
617
618 // Called by tests to set up HWC call expectations
619 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
620 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY_ID, _))
Lloyd Pique3c085a02018-05-09 19:38:32 -0700621 .WillRepeatedly(DoAll(SetArgPointee<1>(HWC_ACTIVE_CONFIG_ID), Return(Error::NONE)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800622 }
623};
624
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800625// Physical displays are expected to be synchronous, secure, and have a HWC display for output.
626constexpr uint32_t GRALLOC_USAGE_PHYSICAL_DISPLAY =
627 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
628
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100629template <typename PhysicalDisplay, int width, int height, Critical critical>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800630struct PhysicalDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700631 : DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height, critical, Async::FALSE,
632 Secure::TRUE, PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
Peiyong Line9d809e2020-04-14 13:10:48 -0700633 HwcDisplayVariant<PhysicalDisplay::HWC_DISPLAY_ID, DisplayType::PHYSICAL,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700634 DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height,
635 critical, Async::FALSE, Secure::TRUE,
636 PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
637 PhysicalDisplay> {};
638
639template <bool hasIdentificationData>
640struct PrimaryDisplay {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800641 static constexpr auto CONNECTION_TYPE = DisplayConnectionType::Internal;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700642 static constexpr Primary PRIMARY = Primary::TRUE;
643 static constexpr uint8_t PORT = 255;
Peiyong Line9d809e2020-04-14 13:10:48 -0700644 static constexpr HWDisplayId HWC_DISPLAY_ID = 1001;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700645 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
646 static constexpr auto GET_IDENTIFICATION_DATA = getInternalEdid;
647};
648
649template <bool hasIdentificationData>
650struct ExternalDisplay {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800651 static constexpr auto CONNECTION_TYPE = DisplayConnectionType::External;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700652 static constexpr Primary PRIMARY = Primary::FALSE;
653 static constexpr uint8_t PORT = 254;
Peiyong Line9d809e2020-04-14 13:10:48 -0700654 static constexpr HWDisplayId HWC_DISPLAY_ID = 1002;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700655 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
656 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
657};
658
659struct TertiaryDisplay {
660 static constexpr Primary PRIMARY = Primary::FALSE;
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800661 static constexpr uint8_t PORT = 253;
Peiyong Line9d809e2020-04-14 13:10:48 -0700662 static constexpr HWDisplayId HWC_DISPLAY_ID = 1003;
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800663 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700664};
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800665
666// A primary display is a physical display that is critical
667using PrimaryDisplayVariant =
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100668 PhysicalDisplayVariant<PrimaryDisplay<false>, 3840, 2160, Critical::TRUE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800669
670// An external display is physical display that is not critical.
671using ExternalDisplayVariant =
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100672 PhysicalDisplayVariant<ExternalDisplay<false>, 1920, 1280, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800673
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100674using TertiaryDisplayVariant = PhysicalDisplayVariant<TertiaryDisplay, 1600, 1200, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800675
676// A virtual display not supported by the HWC.
677constexpr uint32_t GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY = 0;
678
679template <int width, int height, Secure secure>
680struct NonHwcVirtualDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700681 : DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
682 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY> {
683 using Base = DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
684 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>;
685
686 static void injectHwcDisplay(DisplayTransactionTest*) {}
687
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700688 static std::shared_ptr<compositionengine::Display> injectCompositionDisplay(
689 DisplayTransactionTest* test) {
690 const ::testing::TestInfo* const test_info =
691 ::testing::UnitTest::GetInstance()->current_test_info();
692
693 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder()
694 .setPixels({Base::WIDTH, Base::HEIGHT})
695 .setIsSecure(static_cast<bool>(Base::SECURE))
696 .setPowerAdvisor(&test->mPowerAdvisor)
697 .setName(std::string("Injected display for ") +
698 test_info->test_case_name() + "." + test_info->name())
699 .build();
700
701 return compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
702 ceDisplayArgs);
703 }
704
Dominik Laskowski075d3172018-05-24 15:50:06 -0700705 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
706 EXPECT_CALL(*test->mComposer, getActiveConfig(_, _)).Times(0);
707 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800708
709 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
710 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
711 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
712 }
713};
714
715// A virtual display supported by the HWC.
716constexpr uint32_t GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY = GRALLOC_USAGE_HW_COMPOSER;
717
718template <int width, int height, Secure secure>
719struct HwcVirtualDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700720 : DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE, secure,
721 Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>,
722 HwcDisplayVariant<
Peiyong Line9d809e2020-04-14 13:10:48 -0700723 HWC_VIRTUAL_DISPLAY_HWC_DISPLAY_ID, DisplayType::VIRTUAL,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700724 DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
725 secure, Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>> {
726 using Base = DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
727 secure, Primary::FALSE, GRALLOC_USAGE_HW_COMPOSER>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800728 using Self = HwcVirtualDisplayVariant<width, height, secure>;
729
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700730 static std::shared_ptr<compositionengine::Display> injectCompositionDisplay(
731 DisplayTransactionTest* test) {
732 const ::testing::TestInfo* const test_info =
733 ::testing::UnitTest::GetInstance()->current_test_info();
734
735 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder()
736 .setUseHwcVirtualDisplays(false)
737 .setPixels({Base::WIDTH, Base::HEIGHT})
738 .setIsSecure(static_cast<bool>(Base::SECURE))
739 .setPowerAdvisor(&test->mPowerAdvisor)
740 .setName(std::string("Injected display for ") +
741 test_info->test_case_name() + "." + test_info->name())
742 .build();
743
744 auto compositionDisplay =
745 compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
746 ceDisplayArgs);
747 compositionDisplay->setDisplayIdForTesting(Base::DISPLAY_ID::get());
748
749 // Insert display data so that the HWC thinks it created the virtual display.
750 if (const auto displayId = Base::DISPLAY_ID::get()) {
751 test->mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
752 }
753
754 return compositionDisplay;
755 }
756
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800757 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
758 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
759 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
760 }
761
762 static void setupHwcVirtualDisplayCreationCallExpectations(DisplayTransactionTest* test) {
763 EXPECT_CALL(*test->mComposer, createVirtualDisplay(Base::WIDTH, Base::HEIGHT, _, _))
764 .WillOnce(DoAll(SetArgPointee<3>(Self::HWC_DISPLAY_ID), Return(Error::NONE)));
765 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
766 }
767};
768
769// For this variant, SurfaceFlinger should not configure itself with wide
770// display support, so the display should not be configured for wide-color
771// support.
772struct WideColorSupportNotConfiguredVariant {
773 static constexpr bool WIDE_COLOR_SUPPORTED = false;
774
775 static void injectConfigChange(DisplayTransactionTest* test) {
776 test->mFlinger.mutableHasWideColorDisplay() = false;
Peiyong Lin13effd12018-07-24 17:01:47 -0700777 test->mFlinger.mutableUseColorManagement() = false;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800778 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800779 }
780
781 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
782 EXPECT_CALL(*test->mComposer, getColorModes(_, _)).Times(0);
783 EXPECT_CALL(*test->mComposer, getRenderIntents(_, _, _)).Times(0);
784 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
785 }
786};
787
788// For this variant, SurfaceFlinger should configure itself with wide display
789// support, and the display should respond with an non-empty list of supported
790// color modes. Wide-color support should be configured.
791template <typename Display>
792struct WideColorP3ColorimetricSupportedVariant {
793 static constexpr bool WIDE_COLOR_SUPPORTED = true;
794
795 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700796 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800797 test->mFlinger.mutableHasWideColorDisplay() = true;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800798 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800799 }
800
801 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800802 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_DATASPACE)).Times(1);
803
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800804 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
805 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>({ColorMode::DISPLAY_P3})),
806 Return(Error::NONE)));
807 EXPECT_CALL(*test->mComposer,
808 getRenderIntents(Display::HWC_DISPLAY_ID, ColorMode::DISPLAY_P3, _))
809 .WillOnce(DoAll(SetArgPointee<2>(
810 std::vector<RenderIntent>({RenderIntent::COLORIMETRIC})),
811 Return(Error::NONE)));
812 EXPECT_CALL(*test->mComposer,
813 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::SRGB,
814 RenderIntent::COLORIMETRIC))
815 .WillOnce(Return(Error::NONE));
816 }
817};
818
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800819// For this variant, SurfaceFlinger should configure itself with wide display
820// support, but the display should respond with an empty list of supported color
821// modes. Wide-color support for the display should not be configured.
822template <typename Display>
823struct WideColorNotSupportedVariant {
824 static constexpr bool WIDE_COLOR_SUPPORTED = false;
825
826 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700827 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800828 test->mFlinger.mutableHasWideColorDisplay() = true;
829 }
830
831 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
832 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
833 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>()), Return(Error::NONE)));
Chia-I Wu614e1422018-05-23 02:17:03 -0700834 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800835 }
836};
837
838// For this variant, the display is not a HWC display, so no HDR support should
839// be configured.
840struct NonHwcDisplayHdrSupportVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800841 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800842 static constexpr bool HDR10_SUPPORTED = false;
843 static constexpr bool HDR_HLG_SUPPORTED = false;
844 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
845 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
846 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
847 }
848};
849
Valerie Haue9e843a2018-12-18 13:39:23 -0800850template <typename Display>
851struct Hdr10PlusSupportedVariant {
852 static constexpr bool HDR10_PLUS_SUPPORTED = true;
853 static constexpr bool HDR10_SUPPORTED = true;
854 static constexpr bool HDR_HLG_SUPPORTED = false;
855 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
856 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
857 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _))
858 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({
859 Hdr::HDR10_PLUS,
860 Hdr::HDR10,
861 })),
862 Return(Error::NONE)));
863 }
864};
865
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800866// For this variant, the composer should respond with a non-empty list of HDR
867// modes containing HDR10, so HDR10 support should be configured.
868template <typename Display>
869struct Hdr10SupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800870 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800871 static constexpr bool HDR10_SUPPORTED = true;
872 static constexpr bool HDR_HLG_SUPPORTED = false;
873 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
874 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
875 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
876 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HDR10})),
877 Return(Error::NONE)));
878 }
879};
880
881// For this variant, the composer should respond with a non-empty list of HDR
882// modes containing HLG, so HLG support should be configured.
883template <typename Display>
884struct HdrHlgSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800885 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800886 static constexpr bool HDR10_SUPPORTED = false;
887 static constexpr bool HDR_HLG_SUPPORTED = true;
888 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
889 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
890 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
891 .WillOnce(
892 DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HLG})), Return(Error::NONE)));
893 }
894};
895
896// For this variant, the composer should respond with a non-empty list of HDR
897// modes containing DOLBY_VISION, so DOLBY_VISION support should be configured.
898template <typename Display>
899struct HdrDolbyVisionSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800900 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800901 static constexpr bool HDR10_SUPPORTED = false;
902 static constexpr bool HDR_HLG_SUPPORTED = false;
903 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = true;
904 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
905 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
906 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::DOLBY_VISION})),
907 Return(Error::NONE)));
908 }
909};
910
911// For this variant, the composer should respond with am empty list of HDR
912// modes, so no HDR support should be configured.
913template <typename Display>
914struct HdrNotSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800915 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800916 static constexpr bool HDR10_SUPPORTED = false;
917 static constexpr bool HDR_HLG_SUPPORTED = false;
918 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
919 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
920 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
921 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
922 }
923};
924
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700925struct NonHwcPerFrameMetadataSupportVariant {
926 static constexpr int PER_FRAME_METADATA_KEYS = 0;
927 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800928 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(_)).Times(0);
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700929 }
930};
931
932template <typename Display>
933struct NoPerFrameMetadataSupportVariant {
934 static constexpr int PER_FRAME_METADATA_KEYS = 0;
935 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800936 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
937 .WillOnce(Return(std::vector<PerFrameMetadataKey>()));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700938 }
939};
940
941template <typename Display>
942struct Smpte2086PerFrameMetadataSupportVariant {
943 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::SMPTE2086;
944 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800945 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
946 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Valerie Haue9e843a2018-12-18 13:39:23 -0800947 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X,
948 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y,
949 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X,
950 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y,
951 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X,
952 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y,
953 PerFrameMetadataKey::WHITE_POINT_X,
954 PerFrameMetadataKey::WHITE_POINT_Y,
955 PerFrameMetadataKey::MAX_LUMINANCE,
956 PerFrameMetadataKey::MIN_LUMINANCE,
957 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700958 }
959};
960
961template <typename Display>
962struct Cta861_3_PerFrameMetadataSupportVariant {
963 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::CTA861_3;
964 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800965 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
966 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Valerie Haue9e843a2018-12-18 13:39:23 -0800967 PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL,
968 PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL,
969 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700970 }
971};
972
Valerie Haue9e843a2018-12-18 13:39:23 -0800973template <typename Display>
974struct Hdr10_Plus_PerFrameMetadataSupportVariant {
975 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::HDR10PLUS;
976 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
977 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
978 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
979 PerFrameMetadataKey::HDR10_PLUS_SEI,
980 })));
981 }
982};
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800983/* ------------------------------------------------------------------------
984 * Typical display configurations to test
985 */
986
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700987template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy,
988 typename PerFrameMetadataSupportPolicy>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800989struct Case {
990 using Display = DisplayPolicy;
991 using WideColorSupport = WideColorSupportPolicy;
992 using HdrSupport = HdrSupportPolicy;
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700993 using PerFrameMetadataSupport = PerFrameMetadataSupportPolicy;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800994};
995
996using SimplePrimaryDisplayCase =
997 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700998 HdrNotSupportedVariant<PrimaryDisplayVariant>,
999 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001000using SimpleExternalDisplayCase =
1001 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001002 HdrNotSupportedVariant<ExternalDisplayVariant>,
1003 NoPerFrameMetadataSupportVariant<ExternalDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001004using SimpleTertiaryDisplayCase =
1005 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001006 HdrNotSupportedVariant<TertiaryDisplayVariant>,
1007 NoPerFrameMetadataSupportVariant<TertiaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001008using NonHwcVirtualDisplayCase =
1009 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001010 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant,
1011 NonHwcPerFrameMetadataSupportVariant>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001012using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
1013using HwcVirtualDisplayCase =
1014 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
Lloyd Pique438e9e72018-09-04 18:06:08 -07001015 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>,
tangrobin6753a022018-08-10 10:58:54 +08001016 NoPerFrameMetadataSupportVariant<SimpleHwcVirtualDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001017using WideColorP3ColorimetricDisplayCase =
1018 Case<PrimaryDisplayVariant, WideColorP3ColorimetricSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001019 HdrNotSupportedVariant<PrimaryDisplayVariant>,
1020 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Valerie Haue9e843a2018-12-18 13:39:23 -08001021using Hdr10PlusDisplayCase =
1022 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
1023 Hdr10SupportedVariant<PrimaryDisplayVariant>,
1024 Hdr10_Plus_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001025using Hdr10DisplayCase =
1026 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001027 Hdr10SupportedVariant<PrimaryDisplayVariant>,
1028 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001029using HdrHlgDisplayCase =
1030 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001031 HdrHlgSupportedVariant<PrimaryDisplayVariant>,
1032 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001033using HdrDolbyVisionDisplayCase =
1034 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001035 HdrDolbyVisionSupportedVariant<PrimaryDisplayVariant>,
1036 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
1037using HdrSmpte2086DisplayCase =
1038 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
1039 HdrNotSupportedVariant<PrimaryDisplayVariant>,
1040 Smpte2086PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
1041using HdrCta861_3_DisplayCase =
1042 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
1043 HdrNotSupportedVariant<PrimaryDisplayVariant>,
1044 Cta861_3_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Dominik Laskowskif07b85b2018-06-11 12:49:15 -07001045
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001046/* ------------------------------------------------------------------------
Lloyd Pique6cf11032018-01-22 18:57:44 -08001047 *
1048 * SurfaceFlinger::onHotplugReceived
1049 */
1050
1051TEST_F(DisplayTransactionTest, hotplugEnqueuesEventsForDisplayTransaction) {
1052 constexpr int currentSequenceId = 123;
Peiyong Line9d809e2020-04-14 13:10:48 -07001053 constexpr HWDisplayId hwcDisplayId1 = 456;
1054 constexpr HWDisplayId hwcDisplayId2 = 654;
Lloyd Pique6cf11032018-01-22 18:57:44 -08001055
1056 // --------------------------------------------------------------------
1057 // Preconditions
1058
1059 // Set the current sequence id for accepted events
1060 mFlinger.mutableComposerSequenceId() = currentSequenceId;
1061
1062 // Set the main thread id so that the current thread does not appear to be
1063 // the main thread.
1064 mFlinger.mutableMainThreadId() = std::thread::id();
1065
1066 // --------------------------------------------------------------------
1067 // Call Expectations
1068
1069 // We expect invalidate() to be invoked once to trigger display transaction
1070 // processing.
1071 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1072
1073 // --------------------------------------------------------------------
1074 // Invocation
1075
1076 // Simulate two hotplug events (a connect and a disconnect)
Peiyong Line9d809e2020-04-14 13:10:48 -07001077 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId1, Connection::CONNECTED);
1078 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId2, Connection::DISCONNECTED);
Lloyd Pique6cf11032018-01-22 18:57:44 -08001079
1080 // --------------------------------------------------------------------
1081 // Postconditions
1082
1083 // The display transaction needed flag should be set.
1084 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1085
1086 // All events should be in the pending event queue.
1087 const auto& pendingEvents = mFlinger.mutablePendingHotplugEvents();
1088 ASSERT_EQ(2u, pendingEvents.size());
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001089 EXPECT_EQ(hwcDisplayId1, pendingEvents[0].hwcDisplayId);
Peiyong Line9d809e2020-04-14 13:10:48 -07001090 EXPECT_EQ(Connection::CONNECTED, pendingEvents[0].connection);
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001091 EXPECT_EQ(hwcDisplayId2, pendingEvents[1].hwcDisplayId);
Peiyong Line9d809e2020-04-14 13:10:48 -07001092 EXPECT_EQ(Connection::DISCONNECTED, pendingEvents[1].connection);
Lloyd Pique6cf11032018-01-22 18:57:44 -08001093}
1094
1095TEST_F(DisplayTransactionTest, hotplugDiscardsUnexpectedEvents) {
1096 constexpr int currentSequenceId = 123;
1097 constexpr int otherSequenceId = 321;
Peiyong Line9d809e2020-04-14 13:10:48 -07001098 constexpr HWDisplayId displayId = 456;
Lloyd Pique6cf11032018-01-22 18:57:44 -08001099
1100 // --------------------------------------------------------------------
1101 // Preconditions
1102
1103 // Set the current sequence id for accepted events
1104 mFlinger.mutableComposerSequenceId() = currentSequenceId;
1105
1106 // Set the main thread id so that the current thread does not appear to be
1107 // the main thread.
1108 mFlinger.mutableMainThreadId() = std::thread::id();
1109
1110 // --------------------------------------------------------------------
1111 // Call Expectations
1112
1113 // We do not expect any calls to invalidate().
1114 EXPECT_CALL(*mMessageQueue, invalidate()).Times(0);
1115
1116 // --------------------------------------------------------------------
1117 // Invocation
1118
1119 // Call with an unexpected sequence id
Peiyong Line9d809e2020-04-14 13:10:48 -07001120 mFlinger.onHotplugReceived(otherSequenceId, displayId, Connection::INVALID);
Lloyd Pique6cf11032018-01-22 18:57:44 -08001121
1122 // --------------------------------------------------------------------
1123 // Postconditions
1124
1125 // The display transaction needed flag should not be set
1126 EXPECT_FALSE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1127
1128 // There should be no pending events
1129 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
1130}
1131
1132TEST_F(DisplayTransactionTest, hotplugProcessesEnqueuedEventsIfCalledOnMainThread) {
1133 constexpr int currentSequenceId = 123;
Peiyong Line9d809e2020-04-14 13:10:48 -07001134 constexpr HWDisplayId displayId1 = 456;
Lloyd Pique6cf11032018-01-22 18:57:44 -08001135
1136 // --------------------------------------------------------------------
1137 // Note:
1138 // --------------------------------------------------------------------
1139 // This test case is a bit tricky. We want to verify that
1140 // onHotplugReceived() calls processDisplayHotplugEventsLocked(), but we
1141 // don't really want to provide coverage for everything the later function
1142 // does as there are specific tests for it.
1143 // --------------------------------------------------------------------
1144
1145 // --------------------------------------------------------------------
1146 // Preconditions
1147
1148 // Set the current sequence id for accepted events
1149 mFlinger.mutableComposerSequenceId() = currentSequenceId;
1150
1151 // Set the main thread id so that the current thread does appear to be the
1152 // main thread.
1153 mFlinger.mutableMainThreadId() = std::this_thread::get_id();
1154
1155 // --------------------------------------------------------------------
1156 // Call Expectations
1157
1158 // We expect invalidate() to be invoked once to trigger display transaction
1159 // processing.
1160 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1161
1162 // --------------------------------------------------------------------
1163 // Invocation
1164
1165 // Simulate a disconnect on a display id that is not connected. This should
1166 // be enqueued by onHotplugReceived(), and dequeued by
1167 // processDisplayHotplugEventsLocked(), but then ignored as invalid.
Peiyong Line9d809e2020-04-14 13:10:48 -07001168 mFlinger.onHotplugReceived(currentSequenceId, displayId1, Connection::DISCONNECTED);
Lloyd Pique6cf11032018-01-22 18:57:44 -08001169
1170 // --------------------------------------------------------------------
1171 // Postconditions
1172
1173 // The display transaction needed flag should be set.
1174 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1175
1176 // There should be no event queued on return, as it should have been
1177 // processed.
1178 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
1179}
1180
1181/* ------------------------------------------------------------------------
Lloyd Piquea482f992018-01-22 19:00:34 -08001182 * SurfaceFlinger::createDisplay
1183 */
1184
1185TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForNonsecureDisplay) {
1186 const String8 name("virtual.test");
1187
1188 // --------------------------------------------------------------------
1189 // Call Expectations
1190
1191 // The call should notify the interceptor that a display was created.
1192 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
1193
1194 // --------------------------------------------------------------------
1195 // Invocation
1196
1197 sp<IBinder> displayToken = mFlinger.createDisplay(name, false);
1198
1199 // --------------------------------------------------------------------
1200 // Postconditions
1201
1202 // The display should have been added to the current state
1203 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1204 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001205 EXPECT_TRUE(display.isVirtual());
1206 EXPECT_FALSE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -08001207 EXPECT_EQ(name.string(), display.displayName);
1208
1209 // --------------------------------------------------------------------
1210 // Cleanup conditions
1211
1212 // Destroying the display invalidates the display state.
1213 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1214}
1215
1216TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForSecureDisplay) {
1217 const String8 name("virtual.test");
1218
1219 // --------------------------------------------------------------------
1220 // Call Expectations
1221
1222 // The call should notify the interceptor that a display was created.
1223 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
1224
1225 // --------------------------------------------------------------------
1226 // Invocation
1227
1228 sp<IBinder> displayToken = mFlinger.createDisplay(name, true);
1229
1230 // --------------------------------------------------------------------
1231 // Postconditions
1232
1233 // The display should have been added to the current state
1234 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1235 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001236 EXPECT_TRUE(display.isVirtual());
1237 EXPECT_TRUE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -08001238 EXPECT_EQ(name.string(), display.displayName);
1239
1240 // --------------------------------------------------------------------
1241 // Cleanup conditions
1242
1243 // Destroying the display invalidates the display state.
1244 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1245}
1246
1247/* ------------------------------------------------------------------------
1248 * SurfaceFlinger::destroyDisplay
1249 */
1250
1251TEST_F(DisplayTransactionTest, destroyDisplayClearsCurrentStateForDisplay) {
1252 using Case = NonHwcVirtualDisplayCase;
1253
1254 // --------------------------------------------------------------------
1255 // Preconditions
1256
1257 // A virtual display exists
1258 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1259 existing.inject();
1260
1261 // --------------------------------------------------------------------
1262 // Call Expectations
1263
1264 // The call should notify the interceptor that a display was created.
1265 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
1266
1267 // Destroying the display invalidates the display state.
1268 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1269
1270 // --------------------------------------------------------------------
1271 // Invocation
1272
1273 mFlinger.destroyDisplay(existing.token());
1274
1275 // --------------------------------------------------------------------
1276 // Postconditions
1277
1278 // The display should have been removed from the current state
1279 EXPECT_FALSE(hasCurrentDisplayState(existing.token()));
1280
1281 // Ths display should still exist in the drawing state
1282 EXPECT_TRUE(hasDrawingDisplayState(existing.token()));
1283
1284 // The display transaction needed flasg should be set
1285 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1286}
1287
1288TEST_F(DisplayTransactionTest, destroyDisplayHandlesUnknownDisplay) {
1289 // --------------------------------------------------------------------
1290 // Preconditions
1291
1292 sp<BBinder> displayToken = new BBinder();
1293
1294 // --------------------------------------------------------------------
1295 // Invocation
1296
1297 mFlinger.destroyDisplay(displayToken);
1298}
1299
1300/* ------------------------------------------------------------------------
Valerie Hau9758ae02018-10-09 16:05:09 -07001301 * DisplayDevice::GetBestColorMode
1302 */
1303class GetBestColorModeTest : public DisplayTransactionTest {
1304public:
Valerie Hau9758ae02018-10-09 16:05:09 -07001305 void setHasWideColorGamut(bool hasWideColorGamut) { mHasWideColorGamut = hasWideColorGamut; }
1306
1307 void addHwcColorModesMapping(ui::ColorMode colorMode,
1308 std::vector<ui::RenderIntent> renderIntents) {
1309 mHwcColorModes[colorMode] = renderIntents;
1310 }
1311
1312 void setInputDataspace(ui::Dataspace dataspace) { mInputDataspace = dataspace; }
1313
1314 void setInputRenderIntent(ui::RenderIntent renderIntent) { mInputRenderIntent = renderIntent; }
1315
1316 void getBestColorMode() {
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -07001317 auto displayDevice =
1318 injectDefaultInternalDisplay([this](FakeDisplayDeviceInjector& injector) {
1319 injector.setHwcColorModes(mHwcColorModes);
1320 injector.setHasWideColorGamut(mHasWideColorGamut);
1321 injector.setNativeWindow(mNativeWindow);
1322 });
Valerie Hau9758ae02018-10-09 16:05:09 -07001323
Lloyd Pique3d0c02e2018-10-19 18:38:12 -07001324 displayDevice->getCompositionDisplay()
1325 ->getDisplayColorProfile()
1326 ->getBestColorMode(mInputDataspace, mInputRenderIntent, &mOutDataspace,
1327 &mOutColorMode, &mOutRenderIntent);
Valerie Hau9758ae02018-10-09 16:05:09 -07001328 }
1329
1330 ui::Dataspace mOutDataspace;
1331 ui::ColorMode mOutColorMode;
1332 ui::RenderIntent mOutRenderIntent;
1333
1334private:
1335 ui::Dataspace mInputDataspace;
1336 ui::RenderIntent mInputRenderIntent;
1337 bool mHasWideColorGamut = false;
1338 std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> mHwcColorModes;
Valerie Hau9758ae02018-10-09 16:05:09 -07001339};
1340
1341TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeSRGB) {
1342 addHwcColorModesMapping(ui::ColorMode::SRGB,
1343 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1344 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1345 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1346 setHasWideColorGamut(true);
1347
1348 getBestColorMode();
1349
Peiyong Lin14724e62018-12-05 07:27:30 -08001350 ASSERT_EQ(ui::Dataspace::V0_SRGB, mOutDataspace);
Valerie Hau9758ae02018-10-09 16:05:09 -07001351 ASSERT_EQ(ui::ColorMode::SRGB, mOutColorMode);
1352 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1353}
1354
1355TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDisplayP3) {
1356 addHwcColorModesMapping(ui::ColorMode::DISPLAY_P3,
1357 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1358 addHwcColorModesMapping(ui::ColorMode::SRGB,
1359 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1360 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1361 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1362 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1363 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1364 setHasWideColorGamut(true);
1365
1366 getBestColorMode();
1367
1368 ASSERT_EQ(ui::Dataspace::DISPLAY_P3, mOutDataspace);
1369 ASSERT_EQ(ui::ColorMode::DISPLAY_P3, mOutColorMode);
1370 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1371}
1372
1373TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDISPLAY_BT2020) {
1374 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1375 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1376 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1377 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1378 setHasWideColorGamut(true);
1379
1380 getBestColorMode();
1381
1382 ASSERT_EQ(ui::Dataspace::DISPLAY_BT2020, mOutDataspace);
1383 ASSERT_EQ(ui::ColorMode::DISPLAY_BT2020, mOutColorMode);
1384 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1385}
1386
1387/* ------------------------------------------------------------------------
Lloyd Pique33050472019-12-19 17:12:44 -08001388 * DisplayDevice::setProjection
1389 */
1390
1391class DisplayDeviceSetProjectionTest : public DisplayTransactionTest {
1392public:
Lloyd Pique33050472019-12-19 17:12:44 -08001393 static constexpr int32_t DEFAULT_DISPLAY_WIDTH = 1080; // arbitrary
1394 static constexpr int32_t DEFAULT_DISPLAY_HEIGHT = 1920; // arbitrary
1395
1396 static constexpr int32_t TRANSFORM_FLAGS_ROT_0 = 0;
1397 static constexpr int32_t TRANSFORM_FLAGS_ROT_90 = HAL_TRANSFORM_ROT_90;
1398 static constexpr int32_t TRANSFORM_FLAGS_ROT_180 = HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_FLIP_V;
1399 static constexpr int32_t TRANSFORM_FLAGS_ROT_270 =
1400 HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
1401
1402 DisplayDeviceSetProjectionTest(ui::Size flingerDisplaySize, ui::Size hardwareDisplaySize,
1403 ui::Rotation physicalOrientation)
1404 : mFlingerDisplaySize(flingerDisplaySize),
1405 mHardwareDisplaySize(hardwareDisplaySize),
1406 mPhysicalOrientation(physicalOrientation),
1407 mDisplayDevice(createDisplayDevice()) {}
1408
1409 sp<DisplayDevice> createDisplayDevice() {
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -07001410 return injectDefaultInternalDisplay([this](FakeDisplayDeviceInjector& injector) {
1411 injector.setPhysicalOrientation(mPhysicalOrientation);
1412 });
Lloyd Pique33050472019-12-19 17:12:44 -08001413 }
1414
1415 ui::Size SwapWH(const ui::Size size) const { return ui::Size(size.height, size.width); }
1416
1417 void setProjectionForRotation0() {
1418 // A logical rotation of 0 uses the SurfaceFlinger display size
1419 mDisplayDevice->setProjection(ui::ROTATION_0, Rect(mFlingerDisplaySize),
1420 Rect(mFlingerDisplaySize));
1421 }
1422
1423 void setProjectionForRotation90() {
1424 // A logical rotation of 90 uses the SurfaceFlinger display size with
1425 // the width/height swapped.
1426 mDisplayDevice->setProjection(ui::ROTATION_90, Rect(SwapWH(mFlingerDisplaySize)),
1427 Rect(SwapWH(mFlingerDisplaySize)));
1428 }
1429
1430 void setProjectionForRotation180() {
1431 // A logical rotation of 180 uses the SurfaceFlinger display size
1432 mDisplayDevice->setProjection(ui::ROTATION_180, Rect(mFlingerDisplaySize),
1433 Rect(mFlingerDisplaySize));
1434 }
1435
1436 void setProjectionForRotation270() {
1437 // A logical rotation of 270 uses the SurfaceFlinger display size with
1438 // the width/height swapped.
1439 mDisplayDevice->setProjection(ui::ROTATION_270, Rect(SwapWH(mFlingerDisplaySize)),
1440 Rect(SwapWH(mFlingerDisplaySize)));
1441 }
1442
1443 void expectStateForHardwareTransform0() {
1444 const auto& compositionState = mDisplayDevice->getCompositionDisplay()->getState();
1445 EXPECT_EQ(ui::Transform(TRANSFORM_FLAGS_ROT_0, mHardwareDisplaySize.width,
1446 mHardwareDisplaySize.height),
1447 compositionState.transform);
1448 EXPECT_EQ(TRANSFORM_FLAGS_ROT_0, compositionState.orientation);
Lloyd Piquee8fe4742020-01-21 15:26:18 -08001449 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.sourceClip);
1450 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.destinationClip);
Lloyd Pique33050472019-12-19 17:12:44 -08001451 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.frame);
1452 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.viewport);
1453 EXPECT_EQ(false, compositionState.needsFiltering);
1454 }
1455
1456 void expectStateForHardwareTransform90() {
1457 const auto& compositionState = mDisplayDevice->getCompositionDisplay()->getState();
1458 EXPECT_EQ(ui::Transform(TRANSFORM_FLAGS_ROT_90, mHardwareDisplaySize.width,
1459 mHardwareDisplaySize.height),
1460 compositionState.transform);
1461 EXPECT_EQ(TRANSFORM_FLAGS_ROT_90, compositionState.orientation);
Alec Mouri5a6d8572020-03-23 23:56:15 -07001462 EXPECT_EQ(Rect(SwapWH(mHardwareDisplaySize)), compositionState.sourceClip);
Lloyd Piquee8fe4742020-01-21 15:26:18 -08001463 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.destinationClip);
Lloyd Pique33050472019-12-19 17:12:44 -08001464 // For 90, the frame and viewport have the hardware display size width and height swapped
1465 EXPECT_EQ(Rect(SwapWH(mHardwareDisplaySize)), compositionState.frame);
1466 EXPECT_EQ(Rect(SwapWH(mHardwareDisplaySize)), compositionState.viewport);
1467 EXPECT_EQ(false, compositionState.needsFiltering);
1468 }
1469
1470 void expectStateForHardwareTransform180() {
1471 const auto& compositionState = mDisplayDevice->getCompositionDisplay()->getState();
1472 EXPECT_EQ(ui::Transform(TRANSFORM_FLAGS_ROT_180, mHardwareDisplaySize.width,
1473 mHardwareDisplaySize.height),
1474 compositionState.transform);
1475 EXPECT_EQ(TRANSFORM_FLAGS_ROT_180, compositionState.orientation);
Lloyd Piquee8fe4742020-01-21 15:26:18 -08001476 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.sourceClip);
1477 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.destinationClip);
Lloyd Pique33050472019-12-19 17:12:44 -08001478 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.frame);
1479 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.viewport);
1480 EXPECT_EQ(false, compositionState.needsFiltering);
1481 }
1482
1483 void expectStateForHardwareTransform270() {
1484 const auto& compositionState = mDisplayDevice->getCompositionDisplay()->getState();
1485 EXPECT_EQ(ui::Transform(TRANSFORM_FLAGS_ROT_270, mHardwareDisplaySize.width,
1486 mHardwareDisplaySize.height),
1487 compositionState.transform);
1488 EXPECT_EQ(TRANSFORM_FLAGS_ROT_270, compositionState.orientation);
Alec Mouri5a6d8572020-03-23 23:56:15 -07001489 EXPECT_EQ(Rect(SwapWH(mHardwareDisplaySize)), compositionState.sourceClip);
Lloyd Piquee8fe4742020-01-21 15:26:18 -08001490 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.destinationClip);
Lloyd Pique33050472019-12-19 17:12:44 -08001491 // For 270, the frame and viewport have the hardware display size width and height swapped
1492 EXPECT_EQ(Rect(SwapWH(mHardwareDisplaySize)), compositionState.frame);
1493 EXPECT_EQ(Rect(SwapWH(mHardwareDisplaySize)), compositionState.viewport);
1494 EXPECT_EQ(false, compositionState.needsFiltering);
1495 }
1496
1497 const ui::Size mFlingerDisplaySize;
1498 const ui::Size mHardwareDisplaySize;
1499 const ui::Rotation mPhysicalOrientation;
1500 const sp<DisplayDevice> mDisplayDevice;
1501};
1502
1503struct DisplayDeviceSetProjectionTest_Installed0 : public DisplayDeviceSetProjectionTest {
1504 DisplayDeviceSetProjectionTest_Installed0()
1505 : DisplayDeviceSetProjectionTest(ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
1506 ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
1507 ui::ROTATION_0) {}
1508};
1509
1510TEST_F(DisplayDeviceSetProjectionTest_Installed0, checkWith0OutputRotation) {
1511 setProjectionForRotation0();
1512 expectStateForHardwareTransform0();
1513}
1514
1515TEST_F(DisplayDeviceSetProjectionTest_Installed0, checkWith90OutputRotation) {
1516 setProjectionForRotation90();
1517 expectStateForHardwareTransform90();
1518}
1519
1520TEST_F(DisplayDeviceSetProjectionTest_Installed0, checkWith180OutputRotation) {
1521 setProjectionForRotation180();
1522 expectStateForHardwareTransform180();
1523}
1524
1525TEST_F(DisplayDeviceSetProjectionTest_Installed0, checkWith270OutputRotation) {
1526 setProjectionForRotation270();
1527 expectStateForHardwareTransform270();
1528}
1529
1530struct DisplayDeviceSetProjectionTest_Installed90 : public DisplayDeviceSetProjectionTest {
1531 DisplayDeviceSetProjectionTest_Installed90()
1532 : DisplayDeviceSetProjectionTest(ui::Size(DEFAULT_DISPLAY_HEIGHT, DEFAULT_DISPLAY_WIDTH),
1533 ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
1534 ui::ROTATION_90) {}
1535};
1536
1537TEST_F(DisplayDeviceSetProjectionTest_Installed90, checkWith0OutputRotation) {
1538 setProjectionForRotation0();
1539 expectStateForHardwareTransform90();
1540}
1541
1542TEST_F(DisplayDeviceSetProjectionTest_Installed90, checkWith90OutputRotation) {
1543 setProjectionForRotation90();
1544 expectStateForHardwareTransform180();
1545}
1546
1547TEST_F(DisplayDeviceSetProjectionTest_Installed90, checkWith180OutputRotation) {
1548 setProjectionForRotation180();
1549 expectStateForHardwareTransform270();
1550}
1551
1552TEST_F(DisplayDeviceSetProjectionTest_Installed90, checkWith270OutputRotation) {
1553 setProjectionForRotation270();
1554 expectStateForHardwareTransform0();
1555}
1556
1557struct DisplayDeviceSetProjectionTest_Installed180 : public DisplayDeviceSetProjectionTest {
1558 DisplayDeviceSetProjectionTest_Installed180()
1559 : DisplayDeviceSetProjectionTest(ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
1560 ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
1561 ui::ROTATION_180) {}
1562};
1563
1564TEST_F(DisplayDeviceSetProjectionTest_Installed180, checkWith0OutputRotation) {
1565 setProjectionForRotation0();
1566 expectStateForHardwareTransform180();
1567}
1568
1569TEST_F(DisplayDeviceSetProjectionTest_Installed180, checkWith90OutputRotation) {
1570 setProjectionForRotation90();
1571 expectStateForHardwareTransform270();
1572}
1573
1574TEST_F(DisplayDeviceSetProjectionTest_Installed180, checkWith180OutputRotation) {
1575 setProjectionForRotation180();
1576 expectStateForHardwareTransform0();
1577}
1578
1579TEST_F(DisplayDeviceSetProjectionTest_Installed180, checkWith270OutputRotation) {
1580 setProjectionForRotation270();
1581 expectStateForHardwareTransform90();
1582}
1583
1584struct DisplayDeviceSetProjectionTest_Installed270 : public DisplayDeviceSetProjectionTest {
1585 DisplayDeviceSetProjectionTest_Installed270()
1586 : DisplayDeviceSetProjectionTest(ui::Size(DEFAULT_DISPLAY_HEIGHT, DEFAULT_DISPLAY_WIDTH),
1587 ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
1588 ui::ROTATION_270) {}
1589};
1590
1591TEST_F(DisplayDeviceSetProjectionTest_Installed270, checkWith0OutputRotation) {
1592 setProjectionForRotation0();
1593 expectStateForHardwareTransform270();
1594}
1595
1596TEST_F(DisplayDeviceSetProjectionTest_Installed270, checkWith90OutputRotation) {
1597 setProjectionForRotation90();
1598 expectStateForHardwareTransform0();
1599}
1600
1601TEST_F(DisplayDeviceSetProjectionTest_Installed270, checkWith180OutputRotation) {
1602 setProjectionForRotation180();
1603 expectStateForHardwareTransform90();
1604}
1605
1606TEST_F(DisplayDeviceSetProjectionTest_Installed270, checkWith270OutputRotation) {
1607 setProjectionForRotation270();
1608 expectStateForHardwareTransform180();
1609}
1610
1611/* ------------------------------------------------------------------------
Daniel Solomon42d04562019-01-20 21:03:19 -08001612 * SurfaceFlinger::getDisplayNativePrimaries
1613 */
1614
1615class GetDisplayNativePrimaries : public DisplayTransactionTest {
1616public:
1617 GetDisplayNativePrimaries();
1618 void populateDummyDisplayNativePrimaries(ui::DisplayPrimaries& primaries);
1619 void checkDummyDisplayNativePrimaries(const ui::DisplayPrimaries& primaries);
1620
1621private:
1622 static constexpr float mStartingTestValue = 1.0f;
1623};
1624
1625GetDisplayNativePrimaries::GetDisplayNativePrimaries() {
1626 SimplePrimaryDisplayCase::Display::injectHwcDisplay(this);
1627 injectFakeNativeWindowSurfaceFactory();
1628}
1629
1630void GetDisplayNativePrimaries::populateDummyDisplayNativePrimaries(
1631 ui::DisplayPrimaries& primaries) {
1632 float startingVal = mStartingTestValue;
1633 primaries.red.X = startingVal++;
1634 primaries.red.Y = startingVal++;
1635 primaries.red.Z = startingVal++;
1636 primaries.green.X = startingVal++;
1637 primaries.green.Y = startingVal++;
1638 primaries.green.Z = startingVal++;
1639 primaries.blue.X = startingVal++;
1640 primaries.blue.Y = startingVal++;
1641 primaries.blue.Z = startingVal++;
1642 primaries.white.X = startingVal++;
1643 primaries.white.Y = startingVal++;
1644 primaries.white.Z = startingVal++;
1645}
1646
1647void GetDisplayNativePrimaries::checkDummyDisplayNativePrimaries(
1648 const ui::DisplayPrimaries& primaries) {
1649 float startingVal = mStartingTestValue;
1650 EXPECT_EQ(primaries.red.X, startingVal++);
1651 EXPECT_EQ(primaries.red.Y, startingVal++);
1652 EXPECT_EQ(primaries.red.Z, startingVal++);
1653 EXPECT_EQ(primaries.green.X, startingVal++);
1654 EXPECT_EQ(primaries.green.Y, startingVal++);
1655 EXPECT_EQ(primaries.green.Z, startingVal++);
1656 EXPECT_EQ(primaries.blue.X, startingVal++);
1657 EXPECT_EQ(primaries.blue.Y, startingVal++);
1658 EXPECT_EQ(primaries.blue.Z, startingVal++);
1659 EXPECT_EQ(primaries.white.X, startingVal++);
1660 EXPECT_EQ(primaries.white.Y, startingVal++);
1661 EXPECT_EQ(primaries.white.Z, startingVal++);
1662}
1663
1664TEST_F(GetDisplayNativePrimaries, nullDisplayToken) {
1665 ui::DisplayPrimaries primaries;
1666 EXPECT_EQ(BAD_VALUE, mFlinger.getDisplayNativePrimaries(nullptr, primaries));
1667}
1668
Daniel Solomon42d04562019-01-20 21:03:19 -08001669TEST_F(GetDisplayNativePrimaries, internalDisplayWithPrimariesData) {
1670 auto injector = SimplePrimaryDisplayCase::Display::makeFakeExistingDisplayInjector(this);
1671 injector.inject();
1672 auto internalDisplayToken = injector.token();
1673
1674 ui::DisplayPrimaries expectedPrimaries;
1675 populateDummyDisplayNativePrimaries(expectedPrimaries);
1676 mFlinger.setInternalDisplayPrimaries(expectedPrimaries);
1677
1678 ui::DisplayPrimaries primaries;
1679 EXPECT_EQ(NO_ERROR, mFlinger.getDisplayNativePrimaries(internalDisplayToken, primaries));
1680
1681 checkDummyDisplayNativePrimaries(primaries);
1682}
1683
1684TEST_F(GetDisplayNativePrimaries, notInternalDisplayToken) {
1685 sp<BBinder> notInternalDisplayToken = new BBinder();
1686
1687 ui::DisplayPrimaries primaries;
1688 populateDummyDisplayNativePrimaries(primaries);
Dominik Laskowski470df5f2020-04-02 22:27:42 -07001689 EXPECT_EQ(NAME_NOT_FOUND,
1690 mFlinger.getDisplayNativePrimaries(notInternalDisplayToken, primaries));
Daniel Solomon42d04562019-01-20 21:03:19 -08001691
1692 // Check primaries argument wasn't modified in case of failure
1693 checkDummyDisplayNativePrimaries(primaries);
1694}
1695
1696/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001697 * SurfaceFlinger::setupNewDisplayDeviceInternal
1698 */
1699
1700class SetupNewDisplayDeviceInternalTest : public DisplayTransactionTest {
1701public:
1702 template <typename T>
1703 void setupNewDisplayDeviceInternalTest();
1704};
1705
1706template <typename Case>
1707void SetupNewDisplayDeviceInternalTest::setupNewDisplayDeviceInternalTest() {
1708 const sp<BBinder> displayToken = new BBinder();
Lloyd Pique542307f2018-10-19 13:24:08 -07001709 const sp<compositionengine::mock::DisplaySurface> displaySurface =
1710 new compositionengine::mock::DisplaySurface();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001711 const sp<mock::GraphicBufferProducer> producer = new mock::GraphicBufferProducer();
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001712
1713 // --------------------------------------------------------------------
1714 // Preconditions
1715
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001716 // Wide color displays support is configured appropriately
1717 Case::WideColorSupport::injectConfigChange(this);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001718
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001719 // The display is setup with the HWC.
1720 Case::Display::injectHwcDisplay(this);
1721
1722 // SurfaceFlinger will use a test-controlled factory for native window
1723 // surfaces.
1724 injectFakeNativeWindowSurfaceFactory();
1725
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -07001726 // A compositionengine::Display has already been created
1727 auto compositionDisplay = Case::Display::injectCompositionDisplay(this);
1728
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001729 // --------------------------------------------------------------------
1730 // Call Expectations
1731
1732 // Various native window calls will be made.
1733 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
Lloyd Pique3c085a02018-05-09 19:38:32 -07001734 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001735 Case::WideColorSupport::setupComposerCallExpectations(this);
1736 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001737 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001738
1739 // --------------------------------------------------------------------
1740 // Invocation
1741
1742 DisplayDeviceState state;
Dominik Laskowski55c85402020-01-21 16:25:47 -08001743 if (const auto connectionType = Case::Display::CONNECTION_TYPE::value) {
1744 const auto displayId = Case::Display::DISPLAY_ID::get();
1745 ASSERT_TRUE(displayId);
Marin Shalamanov4a42d432020-02-12 20:22:26 +01001746 const auto hwcDisplayId = Case::Display::HWC_DISPLAY_ID_OPT::value;
1747 ASSERT_TRUE(hwcDisplayId);
1748 state.physical = {*displayId, *connectionType, *hwcDisplayId};
Dominik Laskowski55c85402020-01-21 16:25:47 -08001749 }
1750
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001751 state.isSecure = static_cast<bool>(Case::Display::SECURE);
1752
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -07001753 auto device = mFlinger.setupNewDisplayDeviceInternal(displayToken, compositionDisplay, state,
1754 displaySurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001755
1756 // --------------------------------------------------------------------
1757 // Postconditions
1758
1759 ASSERT_TRUE(device != nullptr);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001760 EXPECT_EQ(Case::Display::DISPLAY_ID::get(), device->getId());
Dominik Laskowski55c85402020-01-21 16:25:47 -08001761 EXPECT_EQ(Case::Display::CONNECTION_TYPE::value, device->getConnectionType());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001762 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), device->isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001763 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001764 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001765 EXPECT_EQ(Case::Display::WIDTH, device->getWidth());
1766 EXPECT_EQ(Case::Display::HEIGHT, device->getHeight());
1767 EXPECT_EQ(Case::WideColorSupport::WIDE_COLOR_SUPPORTED, device->hasWideColorGamut());
Valerie Haue9e843a2018-12-18 13:39:23 -08001768 EXPECT_EQ(Case::HdrSupport::HDR10_PLUS_SUPPORTED, device->hasHDR10PlusSupport());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001769 EXPECT_EQ(Case::HdrSupport::HDR10_SUPPORTED, device->hasHDR10Support());
1770 EXPECT_EQ(Case::HdrSupport::HDR_HLG_SUPPORTED, device->hasHLGSupport());
1771 EXPECT_EQ(Case::HdrSupport::HDR_DOLBY_VISION_SUPPORTED, device->hasDolbyVisionSupport());
Lloyd Pique3c085a02018-05-09 19:38:32 -07001772 // Note: This is not Case::Display::HWC_ACTIVE_CONFIG_ID as the ids are
1773 // remapped, and the test only ever sets up one config. If there were an error
1774 // looking up the remapped index, device->getActiveConfig() would be -1 instead.
Ady Abraham2139f732019-11-13 18:56:40 -08001775 EXPECT_EQ(0, device->getActiveConfig().value());
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001776 EXPECT_EQ(Case::PerFrameMetadataSupport::PER_FRAME_METADATA_KEYS,
1777 device->getSupportedPerFrameMetadata());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001778}
1779
1780TEST_F(SetupNewDisplayDeviceInternalTest, createSimplePrimaryDisplay) {
1781 setupNewDisplayDeviceInternalTest<SimplePrimaryDisplayCase>();
1782}
1783
1784TEST_F(SetupNewDisplayDeviceInternalTest, createSimpleExternalDisplay) {
1785 setupNewDisplayDeviceInternalTest<SimpleExternalDisplayCase>();
1786}
1787
1788TEST_F(SetupNewDisplayDeviceInternalTest, createNonHwcVirtualDisplay) {
1789 setupNewDisplayDeviceInternalTest<NonHwcVirtualDisplayCase>();
1790}
1791
1792TEST_F(SetupNewDisplayDeviceInternalTest, createHwcVirtualDisplay) {
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -07001793 setupNewDisplayDeviceInternalTest<HwcVirtualDisplayCase>();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001794}
1795
1796TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3Display) {
1797 setupNewDisplayDeviceInternalTest<WideColorP3ColorimetricDisplayCase>();
1798}
1799
Valerie Haue9e843a2018-12-18 13:39:23 -08001800TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10PlusDisplay) {
1801 setupNewDisplayDeviceInternalTest<Hdr10PlusDisplayCase>();
1802}
1803
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001804TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10Display) {
1805 setupNewDisplayDeviceInternalTest<Hdr10DisplayCase>();
1806}
1807
1808TEST_F(SetupNewDisplayDeviceInternalTest, createHdrHlgDisplay) {
1809 setupNewDisplayDeviceInternalTest<HdrHlgDisplayCase>();
1810}
1811
1812TEST_F(SetupNewDisplayDeviceInternalTest, createHdrDolbyVisionDisplay) {
1813 setupNewDisplayDeviceInternalTest<HdrDolbyVisionDisplayCase>();
1814}
1815
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001816TEST_F(SetupNewDisplayDeviceInternalTest, createHdrSmpte2086DisplayCase) {
1817 setupNewDisplayDeviceInternalTest<HdrSmpte2086DisplayCase>();
1818}
1819
1820TEST_F(SetupNewDisplayDeviceInternalTest, createHdrCta816_3_DisplayCase) {
1821 setupNewDisplayDeviceInternalTest<HdrCta861_3_DisplayCase>();
1822}
1823
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001824/* ------------------------------------------------------------------------
1825 * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded)
1826 */
1827
1828class HandleTransactionLockedTest : public DisplayTransactionTest {
1829public:
1830 template <typename Case>
1831 void setupCommonPreconditions();
1832
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001833 template <typename Case, bool connected>
1834 static void expectHotplugReceived(mock::EventThread*);
1835
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001836 template <typename Case>
1837 void setupCommonCallExpectationsForConnectProcessing();
1838
1839 template <typename Case>
1840 void setupCommonCallExpectationsForDisconnectProcessing();
1841
1842 template <typename Case>
1843 void processesHotplugConnectCommon();
1844
1845 template <typename Case>
1846 void ignoresHotplugConnectCommon();
1847
1848 template <typename Case>
1849 void processesHotplugDisconnectCommon();
1850
1851 template <typename Case>
1852 void verifyDisplayIsConnected(const sp<IBinder>& displayToken);
1853
1854 template <typename Case>
1855 void verifyPhysicalDisplayIsConnected();
1856
1857 void verifyDisplayIsNotConnected(const sp<IBinder>& displayToken);
1858};
1859
1860template <typename Case>
1861void HandleTransactionLockedTest::setupCommonPreconditions() {
1862 // Wide color displays support is configured appropriately
1863 Case::WideColorSupport::injectConfigChange(this);
1864
1865 // SurfaceFlinger will use a test-controlled factory for BufferQueues
1866 injectFakeBufferQueueFactory();
1867
1868 // SurfaceFlinger will use a test-controlled factory for native window
1869 // surfaces.
1870 injectFakeNativeWindowSurfaceFactory();
1871}
1872
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001873template <typename Case, bool connected>
1874void HandleTransactionLockedTest::expectHotplugReceived(mock::EventThread* eventThread) {
1875 const auto convert = [](auto physicalDisplayId) {
1876 return std::make_optional(DisplayId{physicalDisplayId});
1877 };
1878
1879 EXPECT_CALL(*eventThread,
1880 onHotplugReceived(ResultOf(convert, Case::Display::DISPLAY_ID::get()), connected))
1881 .Times(1);
1882}
1883
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001884template <typename Case>
1885void HandleTransactionLockedTest::setupCommonCallExpectationsForConnectProcessing() {
1886 Case::Display::setupHwcHotplugCallExpectations(this);
1887
1888 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1889 Case::Display::setupFramebufferProducerBufferQueueCallExpectations(this);
1890 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1891 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
1892
1893 Case::WideColorSupport::setupComposerCallExpectations(this);
1894 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001895 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001896
1897 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001898 expectHotplugReceived<Case, true>(mEventThread);
1899 expectHotplugReceived<Case, true>(mSFEventThread);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001900}
1901
1902template <typename Case>
1903void HandleTransactionLockedTest::setupCommonCallExpectationsForDisconnectProcessing() {
1904 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
Dominik Laskowski1eba0202019-01-24 09:14:40 -08001905
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001906 expectHotplugReceived<Case, false>(mEventThread);
1907 expectHotplugReceived<Case, false>(mSFEventThread);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001908}
1909
1910template <typename Case>
1911void HandleTransactionLockedTest::verifyDisplayIsConnected(const sp<IBinder>& displayToken) {
1912 // The display device should have been set up in the list of displays.
1913 ASSERT_TRUE(hasDisplayDevice(displayToken));
1914 const auto& device = getDisplayDevice(displayToken);
1915 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001916 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001917
Dominik Laskowski55c85402020-01-21 16:25:47 -08001918 std::optional<DisplayDeviceState::Physical> expectedPhysical;
1919 if (const auto connectionType = Case::Display::CONNECTION_TYPE::value) {
1920 const auto displayId = Case::Display::DISPLAY_ID::get();
1921 ASSERT_TRUE(displayId);
Marin Shalamanov4a42d432020-02-12 20:22:26 +01001922 const auto hwcDisplayId = Case::Display::HWC_DISPLAY_ID_OPT::value;
1923 ASSERT_TRUE(hwcDisplayId);
1924 expectedPhysical = {*displayId, *connectionType, *hwcDisplayId};
Dominik Laskowski55c85402020-01-21 16:25:47 -08001925 }
1926
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001927 // The display should have been set up in the current display state
1928 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1929 const auto& current = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001930 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), current.isVirtual());
Dominik Laskowski55c85402020-01-21 16:25:47 -08001931 EXPECT_EQ(expectedPhysical, current.physical);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001932
1933 // The display should have been set up in the drawing display state
1934 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1935 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001936 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
Dominik Laskowski55c85402020-01-21 16:25:47 -08001937 EXPECT_EQ(expectedPhysical, draw.physical);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001938}
1939
1940template <typename Case>
1941void HandleTransactionLockedTest::verifyPhysicalDisplayIsConnected() {
1942 // HWComposer should have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001943 EXPECT_TRUE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001944
Dominik Laskowski075d3172018-05-24 15:50:06 -07001945 // SF should have a display token.
1946 const auto displayId = Case::Display::DISPLAY_ID::get();
1947 ASSERT_TRUE(displayId);
1948 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1949 auto& displayToken = mFlinger.mutablePhysicalDisplayTokens()[*displayId];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001950
1951 verifyDisplayIsConnected<Case>(displayToken);
1952}
1953
1954void HandleTransactionLockedTest::verifyDisplayIsNotConnected(const sp<IBinder>& displayToken) {
1955 EXPECT_FALSE(hasDisplayDevice(displayToken));
1956 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1957 EXPECT_FALSE(hasDrawingDisplayState(displayToken));
1958}
1959
1960template <typename Case>
1961void HandleTransactionLockedTest::processesHotplugConnectCommon() {
1962 // --------------------------------------------------------------------
1963 // Preconditions
1964
1965 setupCommonPreconditions<Case>();
1966
1967 // A hotplug connect event is enqueued for a display
Peiyong Line9d809e2020-04-14 13:10:48 -07001968 Case::Display::injectPendingHotplugEvent(this, Connection::CONNECTED);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001969
1970 // --------------------------------------------------------------------
1971 // Call Expectations
1972
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001973 setupCommonCallExpectationsForConnectProcessing<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001974
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001975 // --------------------------------------------------------------------
1976 // Invocation
Lloyd Piquee39cad22017-12-20 17:01:29 -08001977
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001978 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
Lloyd Piquee39cad22017-12-20 17:01:29 -08001979
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001980 // --------------------------------------------------------------------
1981 // Postconditions
1982
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001983 verifyPhysicalDisplayIsConnected<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001984
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001985 // --------------------------------------------------------------------
1986 // Cleanup conditions
1987
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001988 EXPECT_CALL(*mComposer,
1989 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001990 .WillOnce(Return(Error::NONE));
1991 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Piquef58625d2017-12-19 13:22:33 -08001992}
1993
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001994template <typename Case>
1995void HandleTransactionLockedTest::ignoresHotplugConnectCommon() {
1996 // --------------------------------------------------------------------
1997 // Preconditions
1998
1999 setupCommonPreconditions<Case>();
2000
2001 // A hotplug connect event is enqueued for a display
Peiyong Line9d809e2020-04-14 13:10:48 -07002002 Case::Display::injectPendingHotplugEvent(this, Connection::CONNECTED);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002003
2004 // --------------------------------------------------------------------
2005 // Invocation
2006
2007 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2008
2009 // --------------------------------------------------------------------
2010 // Postconditions
2011
2012 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07002013 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002014}
2015
2016template <typename Case>
2017void HandleTransactionLockedTest::processesHotplugDisconnectCommon() {
2018 // --------------------------------------------------------------------
2019 // Preconditions
2020
2021 setupCommonPreconditions<Case>();
2022
2023 // A hotplug disconnect event is enqueued for a display
Peiyong Line9d809e2020-04-14 13:10:48 -07002024 Case::Display::injectPendingHotplugEvent(this, Connection::DISCONNECTED);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002025
2026 // The display is already completely set up.
2027 Case::Display::injectHwcDisplay(this);
2028 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
2029 existing.inject();
2030
2031 // --------------------------------------------------------------------
2032 // Call Expectations
2033
Lloyd Pique438e9e72018-09-04 18:06:08 -07002034 EXPECT_CALL(*mComposer, getDisplayIdentificationData(Case::Display::HWC_DISPLAY_ID, _, _))
2035 .Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002036
2037 setupCommonCallExpectationsForDisconnectProcessing<Case>();
2038
2039 // --------------------------------------------------------------------
2040 // Invocation
2041
2042 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2043
2044 // --------------------------------------------------------------------
2045 // Postconditions
2046
2047 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07002048 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002049
Dominik Laskowski075d3172018-05-24 15:50:06 -07002050 // SF should not have a display token.
2051 const auto displayId = Case::Display::DISPLAY_ID::get();
2052 ASSERT_TRUE(displayId);
2053 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002054
2055 // The existing token should have been removed
2056 verifyDisplayIsNotConnected(existing.token());
2057}
2058
2059TEST_F(HandleTransactionLockedTest, processesHotplugConnectPrimaryDisplay) {
2060 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
2061}
2062
2063TEST_F(HandleTransactionLockedTest,
2064 processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected) {
2065 // Inject an external display.
2066 ExternalDisplayVariant::injectHwcDisplay(this);
2067
2068 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
2069}
2070
2071TEST_F(HandleTransactionLockedTest, processesHotplugConnectExternalDisplay) {
2072 // Inject a primary display.
2073 PrimaryDisplayVariant::injectHwcDisplay(this);
2074
2075 processesHotplugConnectCommon<SimpleExternalDisplayCase>();
2076}
2077
2078TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected) {
2079 // Inject both a primary and external display.
2080 PrimaryDisplayVariant::injectHwcDisplay(this);
2081 ExternalDisplayVariant::injectHwcDisplay(this);
2082
Lloyd Pique86fa3db2019-02-04 18:46:01 -08002083 // TODO: This is an unnecessary call.
2084 EXPECT_CALL(*mComposer,
2085 getDisplayIdentificationData(TertiaryDisplayVariant::HWC_DISPLAY_ID, _, _))
2086 .WillOnce(DoAll(SetArgPointee<1>(TertiaryDisplay::PORT),
2087 SetArgPointee<2>(TertiaryDisplay::GET_IDENTIFICATION_DATA()),
2088 Return(Error::NONE)));
2089
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002090 ignoresHotplugConnectCommon<SimpleTertiaryDisplayCase>();
2091}
2092
Marin Shalamanov4a42d432020-02-12 20:22:26 +01002093TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectPrimaryDisplay) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002094 processesHotplugDisconnectCommon<SimplePrimaryDisplayCase>();
2095}
2096
Marin Shalamanov4a42d432020-02-12 20:22:26 +01002097TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectExternalDisplay) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002098 processesHotplugDisconnectCommon<SimpleExternalDisplayCase>();
2099}
2100
2101TEST_F(HandleTransactionLockedTest, processesHotplugConnectThenDisconnectPrimary) {
2102 using Case = SimplePrimaryDisplayCase;
2103
2104 // --------------------------------------------------------------------
2105 // Preconditions
2106
2107 setupCommonPreconditions<Case>();
2108
2109 // A hotplug connect event is enqueued for a display
Peiyong Line9d809e2020-04-14 13:10:48 -07002110 Case::Display::injectPendingHotplugEvent(this, Connection::CONNECTED);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002111 // A hotplug disconnect event is also enqueued for the same display
Peiyong Line9d809e2020-04-14 13:10:48 -07002112 Case::Display::injectPendingHotplugEvent(this, Connection::DISCONNECTED);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002113
2114 // --------------------------------------------------------------------
2115 // Call Expectations
2116
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002117 setupCommonCallExpectationsForConnectProcessing<Case>();
2118 setupCommonCallExpectationsForDisconnectProcessing<Case>();
2119
2120 EXPECT_CALL(*mComposer,
2121 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
2122 .WillOnce(Return(Error::NONE));
2123 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
2124
2125 // --------------------------------------------------------------------
2126 // Invocation
2127
2128 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2129
2130 // --------------------------------------------------------------------
2131 // Postconditions
2132
2133 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07002134 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002135
Dominik Laskowski075d3172018-05-24 15:50:06 -07002136 // SF should not have a display token.
2137 const auto displayId = Case::Display::DISPLAY_ID::get();
2138 ASSERT_TRUE(displayId);
2139 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002140}
2141
2142TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectThenConnectPrimary) {
2143 using Case = SimplePrimaryDisplayCase;
2144
2145 // --------------------------------------------------------------------
2146 // Preconditions
2147
2148 setupCommonPreconditions<Case>();
2149
2150 // The display is already completely set up.
2151 Case::Display::injectHwcDisplay(this);
2152 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
2153 existing.inject();
2154
2155 // A hotplug disconnect event is enqueued for a display
Peiyong Line9d809e2020-04-14 13:10:48 -07002156 Case::Display::injectPendingHotplugEvent(this, Connection::DISCONNECTED);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002157 // A hotplug connect event is also enqueued for the same display
Peiyong Line9d809e2020-04-14 13:10:48 -07002158 Case::Display::injectPendingHotplugEvent(this, Connection::CONNECTED);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002159
2160 // --------------------------------------------------------------------
2161 // Call Expectations
2162
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002163 setupCommonCallExpectationsForConnectProcessing<Case>();
2164 setupCommonCallExpectationsForDisconnectProcessing<Case>();
2165
2166 // --------------------------------------------------------------------
2167 // Invocation
2168
2169 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2170
2171 // --------------------------------------------------------------------
2172 // Postconditions
2173
2174 // The existing token should have been removed
2175 verifyDisplayIsNotConnected(existing.token());
Dominik Laskowski075d3172018-05-24 15:50:06 -07002176 const auto displayId = Case::Display::DISPLAY_ID::get();
2177 ASSERT_TRUE(displayId);
2178 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
2179 EXPECT_NE(existing.token(), mFlinger.mutablePhysicalDisplayTokens()[*displayId]);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002180
2181 // A new display should be connected in its place
2182
2183 verifyPhysicalDisplayIsConnected<Case>();
2184
2185 // --------------------------------------------------------------------
2186 // Cleanup conditions
2187
2188 EXPECT_CALL(*mComposer,
2189 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
2190 .WillOnce(Return(Error::NONE));
2191 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
2192}
2193
2194TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAdded) {
2195 using Case = HwcVirtualDisplayCase;
2196
2197 // --------------------------------------------------------------------
2198 // Preconditions
2199
2200 // The HWC supports at least one virtual display
2201 injectMockComposer(1);
2202
2203 setupCommonPreconditions<Case>();
2204
2205 // A virtual display was added to the current state, and it has a
2206 // surface(producer)
2207 sp<BBinder> displayToken = new BBinder();
Lloyd Pique4c2ac022018-04-27 12:08:03 -07002208
Dominik Laskowski075d3172018-05-24 15:50:06 -07002209 DisplayDeviceState state;
2210 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002211
2212 sp<mock::GraphicBufferProducer> surface{new mock::GraphicBufferProducer()};
Dominik Laskowski075d3172018-05-24 15:50:06 -07002213 state.surface = surface;
2214 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002215
2216 // --------------------------------------------------------------------
2217 // Call Expectations
2218
2219 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
2220 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
2221
2222 EXPECT_CALL(*surface, query(NATIVE_WINDOW_WIDTH, _))
2223 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::WIDTH), Return(NO_ERROR)));
2224 EXPECT_CALL(*surface, query(NATIVE_WINDOW_HEIGHT, _))
2225 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::HEIGHT), Return(NO_ERROR)));
2226 EXPECT_CALL(*surface, query(NATIVE_WINDOW_FORMAT, _))
2227 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT),
2228 Return(NO_ERROR)));
2229 EXPECT_CALL(*surface, query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, _))
2230 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(NO_ERROR)));
2231
2232 EXPECT_CALL(*surface, setAsyncMode(true)).Times(1);
2233
Lloyd Pique86fa3db2019-02-04 18:46:01 -08002234 EXPECT_CALL(*mProducer, connect(_, NATIVE_WINDOW_API_EGL, false, _)).Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002235 EXPECT_CALL(*mProducer, disconnect(_, _)).Times(1);
2236
2237 Case::Display::setupHwcVirtualDisplayCreationCallExpectations(this);
2238 Case::WideColorSupport::setupComposerCallExpectations(this);
2239 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07002240 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002241
2242 // --------------------------------------------------------------------
2243 // Invocation
2244
2245 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2246
2247 // --------------------------------------------------------------------
2248 // Postconditions
2249
2250 // The display device should have been set up in the list of displays.
2251 verifyDisplayIsConnected<Case>(displayToken);
2252
2253 // --------------------------------------------------------------------
2254 // Cleanup conditions
2255
2256 EXPECT_CALL(*mComposer, destroyVirtualDisplay(Case::Display::HWC_DISPLAY_ID))
2257 .WillOnce(Return(Error::NONE));
2258 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Pique9e9800c2019-02-26 16:26:09 -08002259
2260 // Cleanup
2261 mFlinger.mutableCurrentState().displays.removeItem(displayToken);
2262 mFlinger.mutableDrawingState().displays.removeItem(displayToken);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002263}
2264
2265TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAddedWithNoSurface) {
2266 using Case = HwcVirtualDisplayCase;
2267
2268 // --------------------------------------------------------------------
2269 // Preconditions
2270
2271 // The HWC supports at least one virtual display
2272 injectMockComposer(1);
2273
2274 setupCommonPreconditions<Case>();
2275
2276 // A virtual display was added to the current state, but it does not have a
2277 // surface.
2278 sp<BBinder> displayToken = new BBinder();
2279
Dominik Laskowski075d3172018-05-24 15:50:06 -07002280 DisplayDeviceState state;
2281 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002282
Dominik Laskowski075d3172018-05-24 15:50:06 -07002283 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002284
2285 // --------------------------------------------------------------------
2286 // Call Expectations
2287
2288 // --------------------------------------------------------------------
2289 // Invocation
2290
2291 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2292
2293 // --------------------------------------------------------------------
2294 // Postconditions
2295
2296 // There will not be a display device set up.
2297 EXPECT_FALSE(hasDisplayDevice(displayToken));
2298
2299 // The drawing display state will be set from the current display state.
2300 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
2301 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07002302 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002303}
2304
2305TEST_F(HandleTransactionLockedTest, processesVirtualDisplayRemoval) {
2306 using Case = HwcVirtualDisplayCase;
2307
2308 // --------------------------------------------------------------------
2309 // Preconditions
2310
2311 // A virtual display is set up but is removed from the current state.
Dominik Laskowski075d3172018-05-24 15:50:06 -07002312 const auto displayId = Case::Display::DISPLAY_ID::get();
2313 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08002314 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002315 Case::Display::injectHwcDisplay(this);
2316 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
2317 existing.inject();
2318 mFlinger.mutableCurrentState().displays.removeItem(existing.token());
2319
2320 // --------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002321 // Invocation
2322
2323 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2324
2325 // --------------------------------------------------------------------
2326 // Postconditions
2327
2328 // The existing token should have been removed
2329 verifyDisplayIsNotConnected(existing.token());
2330}
2331
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002332TEST_F(HandleTransactionLockedTest, processesDisplayLayerStackChanges) {
2333 using Case = NonHwcVirtualDisplayCase;
2334
2335 constexpr uint32_t oldLayerStack = 0u;
2336 constexpr uint32_t newLayerStack = 123u;
2337
2338 // --------------------------------------------------------------------
2339 // Preconditions
2340
2341 // A display is set up
2342 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2343 display.inject();
2344
2345 // There is a change to the layerStack state
2346 display.mutableDrawingDisplayState().layerStack = oldLayerStack;
2347 display.mutableCurrentDisplayState().layerStack = newLayerStack;
2348
2349 // --------------------------------------------------------------------
2350 // Invocation
2351
2352 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2353
2354 // --------------------------------------------------------------------
2355 // Postconditions
2356
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002357 EXPECT_EQ(newLayerStack, display.mutableDisplayDevice()->getLayerStack());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002358}
2359
2360TEST_F(HandleTransactionLockedTest, processesDisplayTransformChanges) {
2361 using Case = NonHwcVirtualDisplayCase;
2362
Dominik Laskowski718f9602019-11-09 20:01:35 -08002363 constexpr ui::Rotation oldTransform = ui::ROTATION_0;
2364 constexpr ui::Rotation newTransform = ui::ROTATION_180;
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002365
2366 // --------------------------------------------------------------------
2367 // Preconditions
2368
2369 // A display is set up
2370 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2371 display.inject();
2372
2373 // There is a change to the orientation state
2374 display.mutableDrawingDisplayState().orientation = oldTransform;
2375 display.mutableCurrentDisplayState().orientation = newTransform;
2376
2377 // --------------------------------------------------------------------
2378 // Invocation
2379
2380 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2381
2382 // --------------------------------------------------------------------
2383 // Postconditions
2384
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002385 EXPECT_EQ(newTransform, display.mutableDisplayDevice()->getOrientation());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002386}
2387
2388TEST_F(HandleTransactionLockedTest, processesDisplayViewportChanges) {
2389 using Case = NonHwcVirtualDisplayCase;
2390
2391 const Rect oldViewport(0, 0, 0, 0);
2392 const Rect newViewport(0, 0, 123, 456);
2393
2394 // --------------------------------------------------------------------
2395 // Preconditions
2396
2397 // A display is set up
2398 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2399 display.inject();
2400
2401 // There is a change to the viewport state
2402 display.mutableDrawingDisplayState().viewport = oldViewport;
2403 display.mutableCurrentDisplayState().viewport = newViewport;
2404
2405 // --------------------------------------------------------------------
2406 // Invocation
2407
2408 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2409
2410 // --------------------------------------------------------------------
2411 // Postconditions
2412
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002413 EXPECT_EQ(newViewport, display.mutableDisplayDevice()->getViewport());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002414}
2415
2416TEST_F(HandleTransactionLockedTest, processesDisplayFrameChanges) {
2417 using Case = NonHwcVirtualDisplayCase;
2418
2419 const Rect oldFrame(0, 0, 0, 0);
2420 const Rect newFrame(0, 0, 123, 456);
2421
2422 // --------------------------------------------------------------------
2423 // Preconditions
2424
2425 // A display is set up
2426 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2427 display.inject();
2428
2429 // There is a change to the viewport state
2430 display.mutableDrawingDisplayState().frame = oldFrame;
2431 display.mutableCurrentDisplayState().frame = newFrame;
2432
2433 // --------------------------------------------------------------------
2434 // Invocation
2435
2436 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2437
2438 // --------------------------------------------------------------------
2439 // Postconditions
2440
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002441 EXPECT_EQ(newFrame, display.mutableDisplayDevice()->getFrame());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002442}
2443
2444TEST_F(HandleTransactionLockedTest, processesDisplayWidthChanges) {
2445 using Case = NonHwcVirtualDisplayCase;
2446
2447 constexpr int oldWidth = 0;
2448 constexpr int oldHeight = 10;
2449 constexpr int newWidth = 123;
2450
2451 // --------------------------------------------------------------------
2452 // Preconditions
2453
2454 // A display is set up
2455 auto nativeWindow = new mock::NativeWindow();
Lloyd Pique542307f2018-10-19 13:24:08 -07002456 auto displaySurface = new compositionengine::mock::DisplaySurface();
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002457 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002458 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2459 display.setNativeWindow(nativeWindow);
2460 display.setDisplaySurface(displaySurface);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002461 // Setup injection expections
2462 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
2463 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
2464 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
2465 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
Lloyd Pique86fa3db2019-02-04 18:46:01 -08002466 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT)).Times(1);
2467 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_API_CONNECT)).Times(1);
2468 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_SET_USAGE64)).Times(1);
chaviw8beb4142019-04-11 13:09:05 -07002469 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002470 display.inject();
2471
2472 // There is a change to the viewport state
2473 display.mutableDrawingDisplayState().width = oldWidth;
2474 display.mutableDrawingDisplayState().height = oldHeight;
2475 display.mutableCurrentDisplayState().width = newWidth;
2476 display.mutableCurrentDisplayState().height = oldHeight;
2477
2478 // --------------------------------------------------------------------
2479 // Call Expectations
2480
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002481 EXPECT_CALL(*displaySurface, resizeBuffers(newWidth, oldHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002482
2483 // --------------------------------------------------------------------
2484 // Invocation
2485
2486 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2487}
2488
2489TEST_F(HandleTransactionLockedTest, processesDisplayHeightChanges) {
2490 using Case = NonHwcVirtualDisplayCase;
2491
2492 constexpr int oldWidth = 0;
2493 constexpr int oldHeight = 10;
2494 constexpr int newHeight = 123;
2495
2496 // --------------------------------------------------------------------
2497 // Preconditions
2498
2499 // A display is set up
2500 auto nativeWindow = new mock::NativeWindow();
Lloyd Pique542307f2018-10-19 13:24:08 -07002501 auto displaySurface = new compositionengine::mock::DisplaySurface();
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002502 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002503 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2504 display.setNativeWindow(nativeWindow);
2505 display.setDisplaySurface(displaySurface);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002506 // Setup injection expections
2507 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
2508 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
2509 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
2510 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
Lloyd Pique86fa3db2019-02-04 18:46:01 -08002511 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT)).Times(1);
2512 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_API_CONNECT)).Times(1);
2513 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_SET_USAGE64)).Times(1);
chaviw8beb4142019-04-11 13:09:05 -07002514 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002515 display.inject();
2516
2517 // There is a change to the viewport state
2518 display.mutableDrawingDisplayState().width = oldWidth;
2519 display.mutableDrawingDisplayState().height = oldHeight;
2520 display.mutableCurrentDisplayState().width = oldWidth;
2521 display.mutableCurrentDisplayState().height = newHeight;
2522
2523 // --------------------------------------------------------------------
2524 // Call Expectations
2525
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002526 EXPECT_CALL(*displaySurface, resizeBuffers(oldWidth, newHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002527
2528 // --------------------------------------------------------------------
2529 // Invocation
2530
2531 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2532}
2533
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002534/* ------------------------------------------------------------------------
2535 * SurfaceFlinger::setDisplayStateLocked
2536 */
2537
2538TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithUnknownDisplay) {
2539 // --------------------------------------------------------------------
2540 // Preconditions
2541
2542 // We have an unknown display token not associated with a known display
2543 sp<BBinder> displayToken = new BBinder();
2544
2545 // The requested display state references the unknown display.
2546 DisplayState state;
2547 state.what = DisplayState::eLayerStackChanged;
2548 state.token = displayToken;
2549 state.layerStack = 456;
2550
2551 // --------------------------------------------------------------------
2552 // Invocation
2553
2554 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2555
2556 // --------------------------------------------------------------------
2557 // Postconditions
2558
2559 // The returned flags are empty
2560 EXPECT_EQ(0u, flags);
2561
2562 // The display token still doesn't match anything known.
2563 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
2564}
2565
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002566TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWhenNoChanges) {
2567 using Case = SimplePrimaryDisplayCase;
2568
2569 // --------------------------------------------------------------------
2570 // Preconditions
2571
2572 // A display is already set up
2573 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2574 display.inject();
2575
2576 // No changes are made to the display
2577 DisplayState state;
2578 state.what = 0;
2579 state.token = display.token();
2580
2581 // --------------------------------------------------------------------
2582 // Invocation
2583
2584 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2585
2586 // --------------------------------------------------------------------
2587 // Postconditions
2588
2589 // The returned flags are empty
2590 EXPECT_EQ(0u, flags);
2591}
2592
2593TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSurfaceDidNotChange) {
2594 using Case = SimplePrimaryDisplayCase;
2595
2596 // --------------------------------------------------------------------
2597 // Preconditions
2598
2599 // A display is already set up
2600 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2601 display.inject();
2602
2603 // There is a surface that can be set.
2604 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2605
2606 // The current display state has the surface set
2607 display.mutableCurrentDisplayState().surface = surface;
2608
2609 // The incoming request sets the same surface
2610 DisplayState state;
2611 state.what = DisplayState::eSurfaceChanged;
2612 state.token = display.token();
2613 state.surface = surface;
2614
2615 // --------------------------------------------------------------------
2616 // Invocation
2617
2618 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2619
2620 // --------------------------------------------------------------------
2621 // Postconditions
2622
2623 // The returned flags are empty
2624 EXPECT_EQ(0u, flags);
2625
2626 // The current display state is unchanged.
2627 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2628}
2629
2630TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfSurfaceChanged) {
2631 using Case = SimplePrimaryDisplayCase;
2632
2633 // --------------------------------------------------------------------
2634 // Preconditions
2635
2636 // A display is already set up
2637 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2638 display.inject();
2639
2640 // There is a surface that can be set.
2641 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2642
2643 // The current display state does not have a surface
2644 display.mutableCurrentDisplayState().surface = nullptr;
2645
2646 // The incoming request sets a surface
2647 DisplayState state;
2648 state.what = DisplayState::eSurfaceChanged;
2649 state.token = display.token();
2650 state.surface = surface;
2651
2652 // --------------------------------------------------------------------
2653 // Invocation
2654
2655 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2656
2657 // --------------------------------------------------------------------
2658 // Postconditions
2659
2660 // The returned flags indicate a transaction is needed
2661 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2662
2663 // The current display layer stack state is set to the new value
2664 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2665}
2666
2667TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfLayerStackDidNotChange) {
2668 using Case = SimplePrimaryDisplayCase;
2669
2670 // --------------------------------------------------------------------
2671 // Preconditions
2672
2673 // A display is already set up
2674 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2675 display.inject();
2676
2677 // The display has a layer stack set
2678 display.mutableCurrentDisplayState().layerStack = 456u;
2679
2680 // The incoming request sets the same layer stack
2681 DisplayState state;
2682 state.what = DisplayState::eLayerStackChanged;
2683 state.token = display.token();
2684 state.layerStack = 456u;
2685
2686 // --------------------------------------------------------------------
2687 // Invocation
2688
2689 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2690
2691 // --------------------------------------------------------------------
2692 // Postconditions
2693
2694 // The returned flags are empty
2695 EXPECT_EQ(0u, flags);
2696
2697 // The current display state is unchanged
2698 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2699}
2700
2701TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfLayerStackChanged) {
2702 using Case = SimplePrimaryDisplayCase;
2703
2704 // --------------------------------------------------------------------
2705 // Preconditions
2706
2707 // A display is set up
2708 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2709 display.inject();
2710
2711 // The display has a layer stack set
2712 display.mutableCurrentDisplayState().layerStack = 654u;
2713
2714 // The incoming request sets a different layer stack
2715 DisplayState state;
2716 state.what = DisplayState::eLayerStackChanged;
2717 state.token = display.token();
2718 state.layerStack = 456u;
2719
2720 // --------------------------------------------------------------------
2721 // Invocation
2722
2723 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2724
2725 // --------------------------------------------------------------------
2726 // Postconditions
2727
2728 // The returned flags indicate a transaction is needed
2729 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2730
2731 // The desired display state has been set to the new value.
2732 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2733}
2734
2735TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfProjectionDidNotChange) {
2736 using Case = SimplePrimaryDisplayCase;
Dominik Laskowski718f9602019-11-09 20:01:35 -08002737 constexpr ui::Rotation initialOrientation = ui::ROTATION_180;
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002738 const Rect initialFrame = {1, 2, 3, 4};
2739 const Rect initialViewport = {5, 6, 7, 8};
2740
2741 // --------------------------------------------------------------------
2742 // Preconditions
2743
2744 // A display is set up
2745 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2746 display.inject();
2747
2748 // The current display state projection state is all set
2749 display.mutableCurrentDisplayState().orientation = initialOrientation;
2750 display.mutableCurrentDisplayState().frame = initialFrame;
2751 display.mutableCurrentDisplayState().viewport = initialViewport;
2752
2753 // The incoming request sets the same projection state
2754 DisplayState state;
2755 state.what = DisplayState::eDisplayProjectionChanged;
2756 state.token = display.token();
2757 state.orientation = initialOrientation;
2758 state.frame = initialFrame;
2759 state.viewport = initialViewport;
2760
2761 // --------------------------------------------------------------------
2762 // Invocation
2763
2764 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2765
2766 // --------------------------------------------------------------------
2767 // Postconditions
2768
2769 // The returned flags are empty
2770 EXPECT_EQ(0u, flags);
2771
2772 // The current display state is unchanged
2773 EXPECT_EQ(initialOrientation, display.getCurrentDisplayState().orientation);
2774
2775 EXPECT_EQ(initialFrame, display.getCurrentDisplayState().frame);
2776 EXPECT_EQ(initialViewport, display.getCurrentDisplayState().viewport);
2777}
2778
2779TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfOrientationChanged) {
2780 using Case = SimplePrimaryDisplayCase;
Dominik Laskowski718f9602019-11-09 20:01:35 -08002781 constexpr ui::Rotation initialOrientation = ui::ROTATION_90;
2782 constexpr ui::Rotation desiredOrientation = ui::ROTATION_180;
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002783
2784 // --------------------------------------------------------------------
2785 // Preconditions
2786
2787 // A display is set up
2788 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2789 display.inject();
2790
2791 // The current display state has an orientation set
2792 display.mutableCurrentDisplayState().orientation = initialOrientation;
2793
2794 // The incoming request sets a different orientation
2795 DisplayState state;
2796 state.what = DisplayState::eDisplayProjectionChanged;
2797 state.token = display.token();
2798 state.orientation = desiredOrientation;
2799
2800 // --------------------------------------------------------------------
2801 // Invocation
2802
2803 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2804
2805 // --------------------------------------------------------------------
2806 // Postconditions
2807
2808 // The returned flags indicate a transaction is needed
2809 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2810
2811 // The current display state has the new value.
2812 EXPECT_EQ(desiredOrientation, display.getCurrentDisplayState().orientation);
2813}
2814
2815TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfFrameChanged) {
2816 using Case = SimplePrimaryDisplayCase;
2817 const Rect initialFrame = {0, 0, 0, 0};
2818 const Rect desiredFrame = {5, 6, 7, 8};
2819
2820 // --------------------------------------------------------------------
2821 // Preconditions
2822
2823 // A display is set up
2824 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2825 display.inject();
2826
2827 // The current display state does not have a frame
2828 display.mutableCurrentDisplayState().frame = initialFrame;
2829
2830 // The incoming request sets a frame
2831 DisplayState state;
2832 state.what = DisplayState::eDisplayProjectionChanged;
2833 state.token = display.token();
2834 state.frame = desiredFrame;
2835
2836 // --------------------------------------------------------------------
2837 // Invocation
2838
2839 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2840
2841 // --------------------------------------------------------------------
2842 // Postconditions
2843
2844 // The returned flags indicate a transaction is needed
2845 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2846
2847 // The current display state has the new value.
2848 EXPECT_EQ(desiredFrame, display.getCurrentDisplayState().frame);
2849}
2850
2851TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfViewportChanged) {
2852 using Case = SimplePrimaryDisplayCase;
2853 const Rect initialViewport = {0, 0, 0, 0};
2854 const Rect desiredViewport = {5, 6, 7, 8};
2855
2856 // --------------------------------------------------------------------
2857 // Preconditions
2858
2859 // A display is set up
2860 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2861 display.inject();
2862
2863 // The current display state does not have a viewport
2864 display.mutableCurrentDisplayState().viewport = initialViewport;
2865
2866 // The incoming request sets a viewport
2867 DisplayState state;
2868 state.what = DisplayState::eDisplayProjectionChanged;
2869 state.token = display.token();
2870 state.viewport = desiredViewport;
2871
2872 // --------------------------------------------------------------------
2873 // Invocation
2874
2875 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2876
2877 // --------------------------------------------------------------------
2878 // Postconditions
2879
2880 // The returned flags indicate a transaction is needed
2881 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2882
2883 // The current display state has the new value.
2884 EXPECT_EQ(desiredViewport, display.getCurrentDisplayState().viewport);
2885}
2886
2887TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSizeDidNotChange) {
2888 using Case = SimplePrimaryDisplayCase;
2889 constexpr uint32_t initialWidth = 1024;
2890 constexpr uint32_t initialHeight = 768;
2891
2892 // --------------------------------------------------------------------
2893 // Preconditions
2894
2895 // A display is set up
2896 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2897 display.inject();
2898
2899 // The current display state has a size set
2900 display.mutableCurrentDisplayState().width = initialWidth;
2901 display.mutableCurrentDisplayState().height = initialHeight;
2902
2903 // The incoming request sets the same display size
2904 DisplayState state;
2905 state.what = DisplayState::eDisplaySizeChanged;
2906 state.token = display.token();
2907 state.width = initialWidth;
2908 state.height = initialHeight;
2909
2910 // --------------------------------------------------------------------
2911 // Invocation
2912
2913 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2914
2915 // --------------------------------------------------------------------
2916 // Postconditions
2917
2918 // The returned flags are empty
2919 EXPECT_EQ(0u, flags);
2920
2921 // The current display state is unchanged
2922 EXPECT_EQ(initialWidth, display.getCurrentDisplayState().width);
2923 EXPECT_EQ(initialHeight, display.getCurrentDisplayState().height);
2924}
2925
2926TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfWidthChanged) {
2927 using Case = SimplePrimaryDisplayCase;
2928 constexpr uint32_t initialWidth = 0;
2929 constexpr uint32_t desiredWidth = 1024;
2930
2931 // --------------------------------------------------------------------
2932 // Preconditions
2933
2934 // A display is set up
2935 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2936 display.inject();
2937
2938 // The display does not yet have a width
2939 display.mutableCurrentDisplayState().width = initialWidth;
2940
2941 // The incoming request sets a display width
2942 DisplayState state;
2943 state.what = DisplayState::eDisplaySizeChanged;
2944 state.token = display.token();
2945 state.width = desiredWidth;
2946
2947 // --------------------------------------------------------------------
2948 // Invocation
2949
2950 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2951
2952 // --------------------------------------------------------------------
2953 // Postconditions
2954
2955 // The returned flags indicate a transaction is needed
2956 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2957
2958 // The current display state has the new value.
2959 EXPECT_EQ(desiredWidth, display.getCurrentDisplayState().width);
2960}
2961
2962TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfHeightChanged) {
2963 using Case = SimplePrimaryDisplayCase;
2964 constexpr uint32_t initialHeight = 0;
2965 constexpr uint32_t desiredHeight = 768;
2966
2967 // --------------------------------------------------------------------
2968 // Preconditions
2969
2970 // A display is set up
2971 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2972 display.inject();
2973
2974 // The display does not yet have a height
2975 display.mutableCurrentDisplayState().height = initialHeight;
2976
2977 // The incoming request sets a display height
2978 DisplayState state;
2979 state.what = DisplayState::eDisplaySizeChanged;
2980 state.token = display.token();
2981 state.height = desiredHeight;
2982
2983 // --------------------------------------------------------------------
2984 // Invocation
2985
2986 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2987
2988 // --------------------------------------------------------------------
2989 // Postconditions
2990
2991 // The returned flags indicate a transaction is needed
2992 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2993
2994 // The current display state has the new value.
2995 EXPECT_EQ(desiredHeight, display.getCurrentDisplayState().height);
2996}
2997
Lloyd Pique86016da2018-03-01 16:09:38 -08002998/* ------------------------------------------------------------------------
2999 * SurfaceFlinger::onInitializeDisplays
3000 */
3001
3002TEST_F(DisplayTransactionTest, onInitializeDisplaysSetsUpPrimaryDisplay) {
3003 using Case = SimplePrimaryDisplayCase;
3004
3005 // --------------------------------------------------------------------
3006 // Preconditions
3007
3008 // A primary display is set up
3009 Case::Display::injectHwcDisplay(this);
3010 auto primaryDisplay = Case::Display::makeFakeExistingDisplayInjector(this);
3011 primaryDisplay.inject();
3012
3013 // --------------------------------------------------------------------
3014 // Call Expectations
3015
3016 // We expect the surface interceptor to possibly be used, but we treat it as
3017 // disabled since it is called as a side effect rather than directly by this
3018 // function.
3019 EXPECT_CALL(*mSurfaceInterceptor, isEnabled()).WillOnce(Return(false));
3020
3021 // We expect a call to get the active display config.
3022 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
3023
3024 // We expect invalidate() to be invoked once to trigger display transaction
3025 // processing.
3026 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
3027
Ady Abraham0ed31c92020-04-16 11:48:45 -07003028 EXPECT_CALL(*mPrimaryDispSync, expectedPresentTime(_)).WillRepeatedly(Return(0));
Lloyd Pique86fa3db2019-02-04 18:46:01 -08003029
Lloyd Pique86016da2018-03-01 16:09:38 -08003030 // --------------------------------------------------------------------
3031 // Invocation
3032
3033 mFlinger.onInitializeDisplays();
3034
3035 // --------------------------------------------------------------------
3036 // Postconditions
3037
3038 // The primary display should have a current state
3039 ASSERT_TRUE(hasCurrentDisplayState(primaryDisplay.token()));
3040 const auto& primaryDisplayState = getCurrentDisplayState(primaryDisplay.token());
3041 // The layer stack state should be set to zero
3042 EXPECT_EQ(0u, primaryDisplayState.layerStack);
3043 // The orientation state should be set to zero
Dominik Laskowski718f9602019-11-09 20:01:35 -08003044 EXPECT_EQ(ui::ROTATION_0, primaryDisplayState.orientation);
Lloyd Pique86016da2018-03-01 16:09:38 -08003045
3046 // The frame state should be set to INVALID
3047 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.frame);
3048
3049 // The viewport state should be set to INVALID
3050 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.viewport);
3051
3052 // The width and height should both be zero
3053 EXPECT_EQ(0u, primaryDisplayState.width);
3054 EXPECT_EQ(0u, primaryDisplayState.height);
3055
Peiyong Lin65248e02020-04-18 21:15:07 -07003056 // The display should be set to PowerMode::ON
Lloyd Pique86016da2018-03-01 16:09:38 -08003057 ASSERT_TRUE(hasDisplayDevice(primaryDisplay.token()));
3058 auto displayDevice = primaryDisplay.mutableDisplayDevice();
Peiyong Lin65248e02020-04-18 21:15:07 -07003059 EXPECT_EQ(PowerMode::ON, displayDevice->getPowerMode());
Lloyd Pique86016da2018-03-01 16:09:38 -08003060
3061 // The display refresh period should be set in the frame tracker.
3062 FrameStats stats;
3063 mFlinger.getAnimFrameTracker().getStats(&stats);
3064 EXPECT_EQ(DEFAULT_REFRESH_RATE, stats.refreshPeriodNano);
3065
3066 // The display transaction needed flag should be set.
3067 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
3068
3069 // The compositor timing should be set to default values
3070 const auto& compositorTiming = mFlinger.getCompositorTiming();
3071 EXPECT_EQ(-DEFAULT_REFRESH_RATE, compositorTiming.deadline);
3072 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.interval);
3073 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.presentLatency);
3074}
3075
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003076/* ------------------------------------------------------------------------
3077 * SurfaceFlinger::setPowerModeInternal
3078 */
3079
3080// Used when we simulate a display that supports doze.
Peiyong Lined531a32018-10-26 18:27:56 -07003081template <typename Display>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003082struct DozeIsSupportedVariant {
3083 static constexpr bool DOZE_SUPPORTED = true;
3084 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
3085 IComposerClient::PowerMode::DOZE;
3086 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
3087 IComposerClient::PowerMode::DOZE_SUSPEND;
Peiyong Lined531a32018-10-26 18:27:56 -07003088
3089 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
3090 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(Display::HWC_DISPLAY_ID, _))
Peiyong Line9d809e2020-04-14 13:10:48 -07003091 .WillOnce(DoAll(SetArgPointee<1>(
3092 std::vector<DisplayCapability>({DisplayCapability::DOZE})),
Peiyong Lined531a32018-10-26 18:27:56 -07003093 Return(Error::NONE)));
3094 }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003095};
3096
Peiyong Lined531a32018-10-26 18:27:56 -07003097template <typename Display>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003098// Used when we simulate a display that does not support doze.
3099struct DozeNotSupportedVariant {
3100 static constexpr bool DOZE_SUPPORTED = false;
3101 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
3102 IComposerClient::PowerMode::ON;
3103 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
3104 IComposerClient::PowerMode::ON;
Peiyong Lined531a32018-10-26 18:27:56 -07003105
3106 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
3107 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(Display::HWC_DISPLAY_ID, _))
Peiyong Line9d809e2020-04-14 13:10:48 -07003108 .WillOnce(DoAll(SetArgPointee<1>(std::vector<DisplayCapability>({})),
Peiyong Lined531a32018-10-26 18:27:56 -07003109 Return(Error::NONE)));
3110 }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003111};
3112
3113struct EventThreadBaseSupportedVariant {
3114 static void setupEventAndEventControlThreadNoCallExpectations(DisplayTransactionTest* test) {
3115 // The event control thread should not be notified.
3116 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(_)).Times(0);
3117
3118 // The event thread should not be notified.
3119 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(0);
3120 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(0);
3121 }
3122};
3123
3124struct EventThreadNotSupportedVariant : public EventThreadBaseSupportedVariant {
3125 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
3126 // These calls are only expected for the primary display.
3127
3128 // Instead expect no calls.
3129 setupEventAndEventControlThreadNoCallExpectations(test);
3130 }
3131
3132 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
3133 // These calls are only expected for the primary display.
3134
3135 // Instead expect no calls.
3136 setupEventAndEventControlThreadNoCallExpectations(test);
3137 }
3138};
3139
3140struct EventThreadIsSupportedVariant : public EventThreadBaseSupportedVariant {
3141 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
3142 // The event control thread should be notified to enable vsyncs
3143 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(true)).Times(1);
3144
3145 // The event thread should be notified that the screen was acquired.
3146 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(1);
3147 }
3148
3149 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
3150 // There should be a call to setVsyncEnabled(false)
3151 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(false)).Times(1);
3152
3153 // The event thread should not be notified that the screen was released.
3154 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(1);
3155 }
3156};
3157
Lloyd Pique41be5d22018-06-21 13:11:48 -07003158struct DispSyncIsSupportedVariant {
3159 static void setupBeginResyncCallExpectations(DisplayTransactionTest* test) {
Lloyd Pique41be5d22018-06-21 13:11:48 -07003160 EXPECT_CALL(*test->mPrimaryDispSync, setPeriod(DEFAULT_REFRESH_RATE)).Times(1);
3161 EXPECT_CALL(*test->mPrimaryDispSync, beginResync()).Times(1);
3162 }
3163
3164 static void setupEndResyncCallExpectations(DisplayTransactionTest* test) {
3165 EXPECT_CALL(*test->mPrimaryDispSync, endResync()).Times(1);
3166 }
3167};
3168
3169struct DispSyncNotSupportedVariant {
3170 static void setupBeginResyncCallExpectations(DisplayTransactionTest* /* test */) {}
3171
3172 static void setupEndResyncCallExpectations(DisplayTransactionTest* /* test */) {}
3173};
3174
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003175// --------------------------------------------------------------------
3176// Note:
3177//
3178// There are a large number of transitions we could test, however we only test a
3179// selected subset which provides complete test coverage of the implementation.
3180// --------------------------------------------------------------------
3181
Peiyong Lin65248e02020-04-18 21:15:07 -07003182template <PowerMode initialPowerMode, PowerMode targetPowerMode>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003183struct TransitionVariantCommon {
3184 static constexpr auto INITIAL_POWER_MODE = initialPowerMode;
3185 static constexpr auto TARGET_POWER_MODE = targetPowerMode;
3186
3187 static void verifyPostconditions(DisplayTransactionTest*) {}
3188};
3189
Peiyong Lin65248e02020-04-18 21:15:07 -07003190struct TransitionOffToOnVariant : public TransitionVariantCommon<PowerMode::OFF, PowerMode::ON> {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003191 template <typename Case>
3192 static void setupCallExpectations(DisplayTransactionTest* test) {
3193 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
3194 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07003195 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003196 Case::setupRepaintEverythingCallExpectations(test);
3197 }
3198
3199 static void verifyPostconditions(DisplayTransactionTest* test) {
3200 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
3201 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
3202 }
3203};
3204
3205struct TransitionOffToDozeSuspendVariant
Peiyong Lin65248e02020-04-18 21:15:07 -07003206 : public TransitionVariantCommon<PowerMode::OFF, PowerMode::DOZE_SUSPEND> {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003207 template <typename Case>
3208 static void setupCallExpectations(DisplayTransactionTest* test) {
3209 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
3210 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
3211 Case::setupRepaintEverythingCallExpectations(test);
3212 }
3213
3214 static void verifyPostconditions(DisplayTransactionTest* test) {
3215 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
3216 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
3217 }
3218};
3219
Peiyong Lin65248e02020-04-18 21:15:07 -07003220struct TransitionOnToOffVariant : public TransitionVariantCommon<PowerMode::ON, PowerMode::OFF> {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003221 template <typename Case>
3222 static void setupCallExpectations(DisplayTransactionTest* test) {
3223 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07003224 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003225 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
3226 }
3227
3228 static void verifyPostconditions(DisplayTransactionTest* test) {
3229 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
3230 }
3231};
3232
3233struct TransitionDozeSuspendToOffVariant
Peiyong Lin65248e02020-04-18 21:15:07 -07003234 : public TransitionVariantCommon<PowerMode::DOZE_SUSPEND, PowerMode::OFF> {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003235 template <typename Case>
3236 static void setupCallExpectations(DisplayTransactionTest* test) {
3237 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
3238 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
3239 }
3240
3241 static void verifyPostconditions(DisplayTransactionTest* test) {
3242 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
3243 }
3244};
3245
Peiyong Lin65248e02020-04-18 21:15:07 -07003246struct TransitionOnToDozeVariant : public TransitionVariantCommon<PowerMode::ON, PowerMode::DOZE> {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003247 template <typename Case>
3248 static void setupCallExpectations(DisplayTransactionTest* test) {
3249 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
3250 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
3251 }
3252};
3253
3254struct TransitionDozeSuspendToDozeVariant
Peiyong Lin65248e02020-04-18 21:15:07 -07003255 : public TransitionVariantCommon<PowerMode::DOZE_SUSPEND, PowerMode::DOZE> {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003256 template <typename Case>
3257 static void setupCallExpectations(DisplayTransactionTest* test) {
3258 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07003259 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003260 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
3261 }
3262};
3263
Peiyong Lin65248e02020-04-18 21:15:07 -07003264struct TransitionDozeToOnVariant : public TransitionVariantCommon<PowerMode::DOZE, PowerMode::ON> {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003265 template <typename Case>
3266 static void setupCallExpectations(DisplayTransactionTest* test) {
3267 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
3268 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
3269 }
3270};
3271
3272struct TransitionDozeSuspendToOnVariant
Peiyong Lin65248e02020-04-18 21:15:07 -07003273 : public TransitionVariantCommon<PowerMode::DOZE_SUSPEND, PowerMode::ON> {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003274 template <typename Case>
3275 static void setupCallExpectations(DisplayTransactionTest* test) {
3276 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07003277 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003278 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
3279 }
3280};
3281
3282struct TransitionOnToDozeSuspendVariant
Peiyong Lin65248e02020-04-18 21:15:07 -07003283 : public TransitionVariantCommon<PowerMode::ON, PowerMode::DOZE_SUSPEND> {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003284 template <typename Case>
3285 static void setupCallExpectations(DisplayTransactionTest* test) {
3286 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07003287 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003288 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
3289 }
3290};
3291
3292struct TransitionOnToUnknownVariant
Peiyong Lin65248e02020-04-18 21:15:07 -07003293 : public TransitionVariantCommon<PowerMode::ON, static_cast<PowerMode>(POWER_MODE_LEET)> {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003294 template <typename Case>
3295 static void setupCallExpectations(DisplayTransactionTest* test) {
3296 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
3297 Case::setupNoComposerPowerModeCallExpectations(test);
3298 }
3299};
3300
3301// --------------------------------------------------------------------
3302// Note:
3303//
3304// Rather than testing the cartesian product of of
3305// DozeIsSupported/DozeNotSupported with all other options, we use one for one
3306// display type, and the other for another display type.
3307// --------------------------------------------------------------------
3308
3309template <typename DisplayVariant, typename DozeVariant, typename EventThreadVariant,
Lloyd Pique41be5d22018-06-21 13:11:48 -07003310 typename DispSyncVariant, typename TransitionVariant>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003311struct DisplayPowerCase {
3312 using Display = DisplayVariant;
3313 using Doze = DozeVariant;
3314 using EventThread = EventThreadVariant;
Lloyd Pique41be5d22018-06-21 13:11:48 -07003315 using DispSync = DispSyncVariant;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003316 using Transition = TransitionVariant;
3317
Peiyong Lin65248e02020-04-18 21:15:07 -07003318 static auto injectDisplayWithInitialPowerMode(DisplayTransactionTest* test, PowerMode mode) {
Lloyd Pique86fa3db2019-02-04 18:46:01 -08003319 Display::injectHwcDisplayWithNoDefaultCapabilities(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003320 auto display = Display::makeFakeExistingDisplayInjector(test);
3321 display.inject();
3322 display.mutableDisplayDevice()->setPowerMode(mode);
3323 return display;
3324 }
3325
3326 static void setInitialPrimaryHWVsyncEnabled(DisplayTransactionTest* test, bool enabled) {
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -07003327 test->mFlinger.scheduler()->mutablePrimaryHWVsyncEnabled() = enabled;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003328 }
3329
3330 static void setupRepaintEverythingCallExpectations(DisplayTransactionTest* test) {
3331 EXPECT_CALL(*test->mMessageQueue, invalidate()).Times(1);
3332 }
3333
Peiyong Lin65248e02020-04-18 21:15:07 -07003334 static void setupSurfaceInterceptorCallExpectations(DisplayTransactionTest* test,
3335 PowerMode mode) {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003336 EXPECT_CALL(*test->mSurfaceInterceptor, isEnabled()).WillOnce(Return(true));
Peiyong Lin65248e02020-04-18 21:15:07 -07003337 EXPECT_CALL(*test->mSurfaceInterceptor, savePowerModeUpdate(_, static_cast<int32_t>(mode)))
3338 .Times(1);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003339 }
3340
Peiyong Lin65248e02020-04-18 21:15:07 -07003341 static void setupComposerCallExpectations(DisplayTransactionTest* test, PowerMode mode) {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003342 // Any calls to get the active config will return a default value.
3343 EXPECT_CALL(*test->mComposer, getActiveConfig(Display::HWC_DISPLAY_ID, _))
3344 .WillRepeatedly(DoAll(SetArgPointee<1>(Display::HWC_ACTIVE_CONFIG_ID),
3345 Return(Error::NONE)));
3346
3347 // Any calls to get whether the display supports dozing will return the value set by the
3348 // policy variant.
3349 EXPECT_CALL(*test->mComposer, getDozeSupport(Display::HWC_DISPLAY_ID, _))
3350 .WillRepeatedly(DoAll(SetArgPointee<1>(Doze::DOZE_SUPPORTED), Return(Error::NONE)));
3351
3352 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, mode)).Times(1);
3353 }
3354
3355 static void setupNoComposerPowerModeCallExpectations(DisplayTransactionTest* test) {
3356 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, _)).Times(0);
3357 }
3358};
3359
3360// A sample configuration for the primary display.
3361// In addition to having event thread support, we emulate doze support.
3362template <typename TransitionVariant>
Peiyong Lined531a32018-10-26 18:27:56 -07003363using PrimaryDisplayPowerCase =
3364 DisplayPowerCase<PrimaryDisplayVariant, DozeIsSupportedVariant<PrimaryDisplayVariant>,
3365 EventThreadIsSupportedVariant, DispSyncIsSupportedVariant,
3366 TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003367
3368// A sample configuration for the external display.
3369// In addition to not having event thread support, we emulate not having doze
3370// support.
3371template <typename TransitionVariant>
Peiyong Lined531a32018-10-26 18:27:56 -07003372using ExternalDisplayPowerCase =
3373 DisplayPowerCase<ExternalDisplayVariant, DozeNotSupportedVariant<ExternalDisplayVariant>,
3374 EventThreadNotSupportedVariant, DispSyncNotSupportedVariant,
3375 TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003376
3377class SetPowerModeInternalTest : public DisplayTransactionTest {
3378public:
3379 template <typename Case>
3380 void transitionDisplayCommon();
3381};
3382
Peiyong Lin65248e02020-04-18 21:15:07 -07003383template <PowerMode PowerMode>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003384struct PowerModeInitialVSyncEnabled : public std::false_type {};
3385
3386template <>
Peiyong Lin65248e02020-04-18 21:15:07 -07003387struct PowerModeInitialVSyncEnabled<PowerMode::ON> : public std::true_type {};
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003388
3389template <>
Peiyong Lin65248e02020-04-18 21:15:07 -07003390struct PowerModeInitialVSyncEnabled<PowerMode::DOZE> : public std::true_type {};
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003391
3392template <typename Case>
3393void SetPowerModeInternalTest::transitionDisplayCommon() {
3394 // --------------------------------------------------------------------
3395 // Preconditions
3396
Peiyong Lined531a32018-10-26 18:27:56 -07003397 Case::Doze::setupComposerCallExpectations(this);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003398 auto display =
3399 Case::injectDisplayWithInitialPowerMode(this, Case::Transition::INITIAL_POWER_MODE);
3400 Case::setInitialPrimaryHWVsyncEnabled(this,
3401 PowerModeInitialVSyncEnabled<
3402 Case::Transition::INITIAL_POWER_MODE>::value);
3403
3404 // --------------------------------------------------------------------
3405 // Call Expectations
3406
3407 Case::setupSurfaceInterceptorCallExpectations(this, Case::Transition::TARGET_POWER_MODE);
3408 Case::Transition::template setupCallExpectations<Case>(this);
3409
3410 // --------------------------------------------------------------------
3411 // Invocation
3412
3413 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(),
3414 Case::Transition::TARGET_POWER_MODE);
3415
3416 // --------------------------------------------------------------------
3417 // Postconditions
3418
3419 Case::Transition::verifyPostconditions(this);
3420}
3421
3422TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfNoChange) {
3423 using Case = SimplePrimaryDisplayCase;
3424
3425 // --------------------------------------------------------------------
3426 // Preconditions
3427
3428 // A primary display device is set up
3429 Case::Display::injectHwcDisplay(this);
3430 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
3431 display.inject();
3432
Peiyong Lin65248e02020-04-18 21:15:07 -07003433 // The display is already set to PowerMode::ON
3434 display.mutableDisplayDevice()->setPowerMode(PowerMode::ON);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003435
3436 // --------------------------------------------------------------------
3437 // Invocation
3438
Peiyong Lin65248e02020-04-18 21:15:07 -07003439 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), PowerMode::ON);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003440
3441 // --------------------------------------------------------------------
3442 // Postconditions
3443
Peiyong Lin65248e02020-04-18 21:15:07 -07003444 EXPECT_EQ(PowerMode::ON, display.mutableDisplayDevice()->getPowerMode());
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003445}
3446
Dominik Laskowskieecd6592018-05-29 10:25:41 -07003447TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfVirtualDisplay) {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003448 using Case = HwcVirtualDisplayCase;
3449
3450 // --------------------------------------------------------------------
3451 // Preconditions
3452
Dominik Laskowski075d3172018-05-24 15:50:06 -07003453 // Insert display data so that the HWC thinks it created the virtual display.
3454 const auto displayId = Case::Display::DISPLAY_ID::get();
3455 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08003456 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003457
3458 // A virtual display device is set up
3459 Case::Display::injectHwcDisplay(this);
3460 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
3461 display.inject();
3462
Peiyong Lin65248e02020-04-18 21:15:07 -07003463 // The display is set to PowerMode::ON
3464 getDisplayDevice(display.token())->setPowerMode(PowerMode::ON);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003465
3466 // --------------------------------------------------------------------
3467 // Invocation
3468
Peiyong Lin65248e02020-04-18 21:15:07 -07003469 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), PowerMode::OFF);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003470
3471 // --------------------------------------------------------------------
3472 // Postconditions
3473
Peiyong Lin65248e02020-04-18 21:15:07 -07003474 EXPECT_EQ(PowerMode::ON, display.mutableDisplayDevice()->getPowerMode());
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003475}
3476
3477TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnPrimaryDisplay) {
3478 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToOnVariant>>();
3479}
3480
3481TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendPrimaryDisplay) {
3482 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
3483}
3484
3485TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffPrimaryDisplay) {
3486 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToOffVariant>>();
3487}
3488
3489TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffPrimaryDisplay) {
3490 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
3491}
3492
3493TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozePrimaryDisplay) {
3494 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeVariant>>();
3495}
3496
3497TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozePrimaryDisplay) {
3498 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
3499}
3500
3501TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnPrimaryDisplay) {
3502 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeToOnVariant>>();
3503}
3504
3505TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnPrimaryDisplay) {
3506 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
3507}
3508
3509TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendPrimaryDisplay) {
3510 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
3511}
3512
3513TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownPrimaryDisplay) {
3514 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToUnknownVariant>>();
3515}
3516
3517TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnExternalDisplay) {
3518 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToOnVariant>>();
3519}
3520
3521TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendExternalDisplay) {
3522 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
3523}
3524
3525TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffExternalDisplay) {
3526 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToOffVariant>>();
3527}
3528
3529TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffExternalDisplay) {
3530 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
3531}
3532
3533TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeExternalDisplay) {
3534 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeVariant>>();
3535}
3536
3537TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozeExternalDisplay) {
3538 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
3539}
3540
3541TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnExternalDisplay) {
3542 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeToOnVariant>>();
3543}
3544
3545TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnExternalDisplay) {
3546 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
3547}
3548
3549TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendExternalDisplay) {
3550 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
3551}
3552
3553TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownExternalDisplay) {
3554 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToUnknownVariant>>();
3555}
3556
Lloyd Piquef58625d2017-12-19 13:22:33 -08003557} // namespace
3558} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08003559
3560// TODO(b/129481165): remove the #pragma below and fix conversion issues
3561#pragma clang diagnostic pop // ignored "-Wconversion"