blob: 6086a05380f04a7eb2b15603ac7523caddd58c0f [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
Lais Andrade3a6e47d2020-04-02 11:20:16 +010026#include <android/hardware/power/Boost.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070027#include <compositionengine/Display.h>
28#include <compositionengine/DisplayColorProfile.h>
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070029#include <compositionengine/impl/Display.h>
Lloyd Pique33050472019-12-19 17:12:44 -080030#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070031#include <compositionengine/mock/Display.h>
32#include <compositionengine/mock/DisplayColorProfile.h>
Lloyd Pique542307f2018-10-19 13:24:08 -070033#include <compositionengine/mock/DisplaySurface.h>
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070034#include <compositionengine/mock/RenderSurface.h>
Lloyd Piquef58625d2017-12-19 13:22:33 -080035#include <gmock/gmock.h>
36#include <gtest/gtest.h>
Lloyd Pique1ebe0902019-10-04 14:47:13 -070037#include <gui/mock/GraphicBufferConsumer.h>
38#include <gui/mock/GraphicBufferProducer.h>
Lloyd Piquef58625d2017-12-19 13:22:33 -080039#include <log/log.h>
Lloyd Pique3823e7b2018-10-18 16:58:10 -070040#include <renderengine/mock/RenderEngine.h>
Valerie Hau9758ae02018-10-09 16:05:09 -070041#include <ui/DebugUtils.h>
Dominik Laskowski075d3172018-05-24 15:50:06 -070042
43#include "DisplayIdentificationTest.h"
Ana Krulecafb45842019-02-13 13:33:03 -080044#include "TestableScheduler.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080045#include "TestableSurfaceFlinger.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080046#include "mock/DisplayHardware/MockComposer.h"
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070047#include "mock/DisplayHardware/MockPowerAdvisor.h"
Lloyd Pique41be5d22018-06-21 13:11:48 -070048#include "mock/MockDispSync.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080049#include "mock/MockEventThread.h"
50#include "mock/MockMessageQueue.h"
51#include "mock/MockNativeWindowSurface.h"
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070052#include "mock/MockSchedulerCallback.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080053#include "mock/MockSurfaceInterceptor.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080054#include "mock/system/window/MockNativeWindow.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080055
56namespace android {
57namespace {
58
Peiyong Line9d809e2020-04-14 13:10:48 -070059namespace hal = android::hardware::graphics::composer::hal;
60
Lais Andrade3a6e47d2020-04-02 11:20:16 +010061using android::hardware::power::Boost;
62
Lloyd Piquee39cad22017-12-20 17:01:29 -080063using testing::_;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070064using testing::AnyNumber;
Lloyd Piquee39cad22017-12-20 17:01:29 -080065using testing::DoAll;
66using testing::Mock;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080067using testing::ResultOf;
Lloyd Piquee39cad22017-12-20 17:01:29 -080068using testing::Return;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070069using testing::ReturnRefOfCopy;
Lloyd Piquee39cad22017-12-20 17:01:29 -080070using testing::SetArgPointee;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070071using testing::StrictMock;
Lloyd Piquee39cad22017-12-20 17:01:29 -080072
Peiyong Line9d809e2020-04-14 13:10:48 -070073using hal::ColorMode;
74using hal::Connection;
75using hal::DisplayCapability;
76using hal::DisplayType;
77using hal::Error;
78using hal::Hdr;
79using hal::HWDisplayId;
80using hal::IComposer;
81using hal::IComposerClient;
82using hal::PerFrameMetadataKey;
83using hal::PowerMode;
84using hal::RenderIntent;
Lloyd Piquee39cad22017-12-20 17:01:29 -080085
Lloyd Piquec11e0d32018-01-22 18:44:59 -080086using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector;
87using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
Lloyd Pique1fa4d462018-01-22 18:03:16 -080088using HotplugEvent = TestableSurfaceFlinger::HotplugEvent;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080089using HWC2Display = TestableSurfaceFlinger::HWC2Display;
Lloyd Piquebc792092018-01-17 11:52:30 -080090
Ady Abraham2492a022020-07-24 11:09:55 -070091constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'667;
Lloyd Piquee39cad22017-12-20 17:01:29 -080092constexpr int32_t DEFAULT_DPI = 320;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080093constexpr int DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT = HAL_PIXEL_FORMAT_RGB_565;
Lloyd Piquee39cad22017-12-20 17:01:29 -080094
Peiyong Lin65248e02020-04-18 21:15:07 -070095constexpr int POWER_MODE_LEET = 1337; // An out of range power mode value
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -080096
Lloyd Piquec11e0d32018-01-22 18:44:59 -080097/* ------------------------------------------------------------------------
98 * Boolean avoidance
99 *
100 * To make calls and template instantiations more readable, we define some
101 * local enums along with an implicit bool conversion.
102 */
103
104#define BOOL_SUBSTITUTE(TYPENAME) enum class TYPENAME : bool { FALSE = false, TRUE = true };
105
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800106BOOL_SUBSTITUTE(Async);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700107BOOL_SUBSTITUTE(Critical);
108BOOL_SUBSTITUTE(Primary);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800109BOOL_SUBSTITUTE(Secure);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700110BOOL_SUBSTITUTE(Virtual);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800111
112/* ------------------------------------------------------------------------
113 *
114 */
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800115
Lloyd Piquef58625d2017-12-19 13:22:33 -0800116class DisplayTransactionTest : public testing::Test {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800117public:
Lloyd Piquef58625d2017-12-19 13:22:33 -0800118 DisplayTransactionTest();
119 ~DisplayTransactionTest() override;
120
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800121 // --------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800122 // Mock/Fake injection
Lloyd Piquef58625d2017-12-19 13:22:33 -0800123
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700124 void injectMockScheduler();
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800125 void injectMockComposer(int virtualDisplayCount);
126 void injectFakeBufferQueueFactory();
127 void injectFakeNativeWindowSurfaceFactory();
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700128 sp<DisplayDevice> injectDefaultInternalDisplay(std::function<void(FakeDisplayDeviceInjector&)>);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800129
130 // --------------------------------------------------------------------
131 // Postcondition helpers
132
Peiyong Line9d809e2020-04-14 13:10:48 -0700133 bool hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800134 bool hasTransactionFlagSet(int flag);
135 bool hasDisplayDevice(sp<IBinder> displayToken);
136 sp<DisplayDevice> getDisplayDevice(sp<IBinder> displayToken);
137 bool hasCurrentDisplayState(sp<IBinder> displayToken);
138 const DisplayDeviceState& getCurrentDisplayState(sp<IBinder> displayToken);
139 bool hasDrawingDisplayState(sp<IBinder> displayToken);
140 const DisplayDeviceState& getDrawingDisplayState(sp<IBinder> displayToken);
141
142 // --------------------------------------------------------------------
143 // Test instances
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800144
Lloyd Piquef58625d2017-12-19 13:22:33 -0800145 TestableSurfaceFlinger mFlinger;
Alec Mouriba013fa2018-10-16 12:43:11 -0700146 sp<mock::NativeWindow> mNativeWindow = new mock::NativeWindow();
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800147 sp<GraphicBuffer> mBuffer = new GraphicBuffer();
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700148 Hwc2::mock::PowerAdvisor mPowerAdvisor;
Lloyd Piquee39cad22017-12-20 17:01:29 -0800149
150 // These mocks are created by the test, but are destroyed by SurfaceFlinger
151 // by virtue of being stored into a std::unique_ptr. However we still need
152 // to keep a reference to them for use in setting up call expectations.
Peiyong Lin833074a2018-08-28 11:53:54 -0700153 renderengine::mock::RenderEngine* mRenderEngine = new renderengine::mock::RenderEngine();
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800154 Hwc2::mock::Composer* mComposer = nullptr;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800155 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
156 mock::SurfaceInterceptor* mSurfaceInterceptor = new mock::SurfaceInterceptor();
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700157
158 mock::DispSync* mPrimaryDispSync = new mock::DispSync;
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700159 mock::SchedulerCallback mSchedulerCallback;
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700160 mock::EventThread* mEventThread = new mock::EventThread;
161 mock::EventThread* mSFEventThread = new mock::EventThread;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800162
163 // These mocks are created only when expected to be created via a factory.
164 sp<mock::GraphicBufferConsumer> mConsumer;
165 sp<mock::GraphicBufferProducer> mProducer;
Lloyd Pique22098362018-09-13 11:46:49 -0700166 surfaceflinger::mock::NativeWindowSurface* mNativeWindowSurface = nullptr;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800167};
168
169DisplayTransactionTest::DisplayTransactionTest() {
170 const ::testing::TestInfo* const test_info =
171 ::testing::UnitTest::GetInstance()->current_test_info();
172 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800173
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800174 // Default to no wide color display support configured
175 mFlinger.mutableHasWideColorDisplay() = false;
Peiyong Lin13effd12018-07-24 17:01:47 -0700176 mFlinger.mutableUseColorManagement() = false;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800177 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800178
179 // Default to using HWC virtual displays
180 mFlinger.mutableUseHwcVirtualDisplays() = true;
181
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800182 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
183 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
184 });
185
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800186 mFlinger.setCreateNativeWindowSurface([](auto) {
187 ADD_FAILURE() << "Unexpected request to create a native window surface.";
188 return nullptr;
189 });
190
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700191 injectMockScheduler();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800192 mFlinger.mutableEventQueue().reset(mMessageQueue);
Peiyong Lin833074a2018-08-28 11:53:54 -0700193 mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800194 mFlinger.mutableInterceptor().reset(mSurfaceInterceptor);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800195
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800196 injectMockComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -0800197}
198
199DisplayTransactionTest::~DisplayTransactionTest() {
200 const ::testing::TestInfo* const test_info =
201 ::testing::UnitTest::GetInstance()->current_test_info();
202 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
203}
204
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700205void DisplayTransactionTest::injectMockScheduler() {
Ana Krulecafb45842019-02-13 13:33:03 -0800206 EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_));
Dominik Laskowski98041832019-08-01 18:35:59 -0700207 EXPECT_CALL(*mEventThread, createEventConnection(_, _))
208 .WillOnce(Return(new EventThreadConnection(mEventThread, ResyncCallback(),
209 ISurfaceComposer::eConfigChangedSuppress)));
210
Ana Krulecafb45842019-02-13 13:33:03 -0800211 EXPECT_CALL(*mSFEventThread, registerDisplayEventConnection(_));
Dominik Laskowski98041832019-08-01 18:35:59 -0700212 EXPECT_CALL(*mSFEventThread, createEventConnection(_, _))
213 .WillOnce(Return(new EventThreadConnection(mSFEventThread, ResyncCallback(),
214 ISurfaceComposer::eConfigChangedSuppress)));
Ana Krulecafb45842019-02-13 13:33:03 -0800215
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700216 mFlinger.setupScheduler(std::unique_ptr<DispSync>(mPrimaryDispSync),
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700217 std::unique_ptr<EventThread>(mEventThread),
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700218 std::unique_ptr<EventThread>(mSFEventThread), &mSchedulerCallback);
Ana Krulecafb45842019-02-13 13:33:03 -0800219}
220
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800221void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
222 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800223 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
224 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -0800225
Lloyd Piquee39cad22017-12-20 17:01:29 -0800226 Mock::VerifyAndClear(mComposer);
227}
228
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800229void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800230 // This setup is only expected once per test.
231 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
232
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800233 mConsumer = new mock::GraphicBufferConsumer();
234 mProducer = new mock::GraphicBufferProducer();
235
236 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
237 *outProducer = mProducer;
238 *outConsumer = mConsumer;
239 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800240}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800241
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800242void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800243 // This setup is only expected once per test.
244 ASSERT_TRUE(mNativeWindowSurface == nullptr);
245
Lloyd Pique22098362018-09-13 11:46:49 -0700246 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800247
Lloyd Pique22098362018-09-13 11:46:49 -0700248 mFlinger.setCreateNativeWindowSurface([this](auto) {
249 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
250 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800251}
252
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700253sp<DisplayDevice> DisplayTransactionTest::injectDefaultInternalDisplay(
254 std::function<void(FakeDisplayDeviceInjector&)> injectExtra) {
Marin Shalamanova524a092020-07-27 21:39:55 +0200255 constexpr PhysicalDisplayId DEFAULT_DISPLAY_ID(777);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700256 constexpr int DEFAULT_DISPLAY_WIDTH = 1080;
257 constexpr int DEFAULT_DISPLAY_HEIGHT = 1920;
Peiyong Line9d809e2020-04-14 13:10:48 -0700258 constexpr HWDisplayId DEFAULT_DISPLAY_HWC_DISPLAY_ID = 0;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700259
260 // The DisplayDevice is required to have a framebuffer (behind the
261 // ANativeWindow interface) which uses the actual hardware display
262 // size.
263 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
264 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_DISPLAY_WIDTH), Return(0)));
265 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
266 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_DISPLAY_HEIGHT), Return(0)));
267 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT));
268 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT));
269 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64));
270 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT)).Times(AnyNumber());
271
272 auto compositionDisplay = compositionengine::impl::
273 createDisplay(mFlinger.getCompositionEngine(),
274 compositionengine::DisplayCreationArgsBuilder()
275 .setPhysical(
276 {DEFAULT_DISPLAY_ID, DisplayConnectionType::Internal})
277 .setPixels({DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT})
278 .setPowerAdvisor(&mPowerAdvisor)
279 .build());
280
281 auto injector =
282 FakeDisplayDeviceInjector(mFlinger, compositionDisplay, DisplayConnectionType::Internal,
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100283 DEFAULT_DISPLAY_HWC_DISPLAY_ID, true /* isPrimary */);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700284
285 injector.setNativeWindow(mNativeWindow);
286 if (injectExtra) {
287 injectExtra(injector);
288 }
289
290 auto displayDevice = injector.inject();
291
292 Mock::VerifyAndClear(mNativeWindow.get());
293
294 return displayDevice;
295}
296
Peiyong Line9d809e2020-04-14 13:10:48 -0700297bool DisplayTransactionTest::hasPhysicalHwcDisplay(HWDisplayId hwcDisplayId) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700298 return mFlinger.mutableHwcPhysicalDisplayIdMap().count(hwcDisplayId) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800299}
300
301bool DisplayTransactionTest::hasTransactionFlagSet(int flag) {
302 return mFlinger.mutableTransactionFlags() & flag;
303}
304
305bool DisplayTransactionTest::hasDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700306 return mFlinger.mutableDisplays().count(displayToken) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800307}
308
309sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700310 return mFlinger.mutableDisplays()[displayToken];
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800311}
312
313bool DisplayTransactionTest::hasCurrentDisplayState(sp<IBinder> displayToken) {
314 return mFlinger.mutableCurrentState().displays.indexOfKey(displayToken) >= 0;
315}
316
317const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(sp<IBinder> displayToken) {
318 return mFlinger.mutableCurrentState().displays.valueFor(displayToken);
319}
320
321bool DisplayTransactionTest::hasDrawingDisplayState(sp<IBinder> displayToken) {
322 return mFlinger.mutableDrawingState().displays.indexOfKey(displayToken) >= 0;
323}
324
325const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(sp<IBinder> displayToken) {
326 return mFlinger.mutableDrawingState().displays.valueFor(displayToken);
327}
328
329/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800330 *
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800331 */
332
Dominik Laskowski075d3172018-05-24 15:50:06 -0700333template <typename PhysicalDisplay>
Marin Shalamanova524a092020-07-27 21:39:55 +0200334struct PhysicalDisplayIdType {};
Dominik Laskowski075d3172018-05-24 15:50:06 -0700335
Marin Shalamanova524a092020-07-27 21:39:55 +0200336template <uint64_t displayId>
337using VirtualDisplayIdType = std::integral_constant<uint64_t, displayId>;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700338
339struct NoDisplayId {};
340
341template <typename>
342struct IsPhysicalDisplayId : std::bool_constant<false> {};
343
344template <typename PhysicalDisplay>
Marin Shalamanova524a092020-07-27 21:39:55 +0200345struct IsPhysicalDisplayId<PhysicalDisplayIdType<PhysicalDisplay>> : std::bool_constant<true> {};
Dominik Laskowski075d3172018-05-24 15:50:06 -0700346
347template <typename>
348struct DisplayIdGetter;
349
350template <typename PhysicalDisplay>
Marin Shalamanova524a092020-07-27 21:39:55 +0200351struct DisplayIdGetter<PhysicalDisplayIdType<PhysicalDisplay>> {
352 static std::optional<PhysicalDisplayId> get() {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700353 if (!PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
Marin Shalamanova524a092020-07-27 21:39:55 +0200354 return PhysicalDisplayId::fromPort(static_cast<bool>(PhysicalDisplay::PRIMARY)
355 ? LEGACY_DISPLAY_TYPE_PRIMARY
356 : LEGACY_DISPLAY_TYPE_EXTERNAL);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700357 }
358
359 const auto info =
360 parseDisplayIdentificationData(PhysicalDisplay::PORT,
361 PhysicalDisplay::GET_IDENTIFICATION_DATA());
362 return info ? std::make_optional(info->id) : std::nullopt;
363 }
364};
365
Marin Shalamanova524a092020-07-27 21:39:55 +0200366template <uint64_t displayId>
367struct DisplayIdGetter<VirtualDisplayIdType<displayId>> {
368 // TODO(b/160679868) Use VirtualDisplayId
369 static std::optional<PhysicalDisplayId> get() { return PhysicalDisplayId{displayId}; }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700370};
371
372template <>
373struct DisplayIdGetter<NoDisplayId> {
374 static std::optional<DisplayId> get() { return {}; }
375};
376
Dominik Laskowski55c85402020-01-21 16:25:47 -0800377template <typename>
378struct DisplayConnectionTypeGetter {
379 static constexpr std::optional<DisplayConnectionType> value;
380};
381
382template <typename PhysicalDisplay>
Marin Shalamanova524a092020-07-27 21:39:55 +0200383struct DisplayConnectionTypeGetter<PhysicalDisplayIdType<PhysicalDisplay>> {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800384 static constexpr std::optional<DisplayConnectionType> value = PhysicalDisplay::CONNECTION_TYPE;
385};
386
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100387template <typename>
388struct HwcDisplayIdGetter {
Peiyong Line9d809e2020-04-14 13:10:48 -0700389 static constexpr std::optional<HWDisplayId> value;
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100390};
391
Peiyong Line9d809e2020-04-14 13:10:48 -0700392constexpr HWDisplayId HWC_VIRTUAL_DISPLAY_HWC_DISPLAY_ID = 1010;
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100393
Marin Shalamanova524a092020-07-27 21:39:55 +0200394template <uint64_t displayId>
395struct HwcDisplayIdGetter<VirtualDisplayIdType<displayId>> {
Peiyong Line9d809e2020-04-14 13:10:48 -0700396 static constexpr std::optional<HWDisplayId> value = HWC_VIRTUAL_DISPLAY_HWC_DISPLAY_ID;
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100397};
398
399template <typename PhysicalDisplay>
Marin Shalamanova524a092020-07-27 21:39:55 +0200400struct HwcDisplayIdGetter<PhysicalDisplayIdType<PhysicalDisplay>> {
Peiyong Line9d809e2020-04-14 13:10:48 -0700401 static constexpr std::optional<HWDisplayId> value = PhysicalDisplay::HWC_DISPLAY_ID;
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100402};
403
Dominik Laskowski075d3172018-05-24 15:50:06 -0700404// DisplayIdType can be:
Marin Shalamanova524a092020-07-27 21:39:55 +0200405// 1) PhysicalDisplayIdType<...> for generated ID of physical display backed by HWC.
406// 2) VirtualDisplayIdType<...> for hard-coded ID of virtual display backed by HWC.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700407// 3) NoDisplayId for virtual display without HWC backing.
408template <typename DisplayIdType, int width, int height, Critical critical, Async async,
409 Secure secure, Primary primary, int grallocUsage>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800410struct DisplayVariant {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700411 using DISPLAY_ID = DisplayIdGetter<DisplayIdType>;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800412 using CONNECTION_TYPE = DisplayConnectionTypeGetter<DisplayIdType>;
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100413 using HWC_DISPLAY_ID_OPT = HwcDisplayIdGetter<DisplayIdType>;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700414
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800415 // The display width and height
416 static constexpr int WIDTH = width;
417 static constexpr int HEIGHT = height;
418
419 static constexpr int GRALLOC_USAGE = grallocUsage;
420
Dominik Laskowski075d3172018-05-24 15:50:06 -0700421 // Whether the display is virtual or physical
422 static constexpr Virtual VIRTUAL =
423 IsPhysicalDisplayId<DisplayIdType>{} ? Virtual::FALSE : Virtual::TRUE;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800424
425 // When creating native window surfaces for the framebuffer, whether those should be critical
426 static constexpr Critical CRITICAL = critical;
427
428 // When creating native window surfaces for the framebuffer, whether those should be async
429 static constexpr Async ASYNC = async;
430
431 // Whether the display should be treated as secure
432 static constexpr Secure SECURE = secure;
433
Dominik Laskowski075d3172018-05-24 15:50:06 -0700434 // Whether the display is primary
435 static constexpr Primary PRIMARY = primary;
436
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800437 static auto makeFakeExistingDisplayInjector(DisplayTransactionTest* test) {
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700438 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder();
439 if (auto displayId = DISPLAY_ID::get()) {
440 ceDisplayArgs.setPhysical({*displayId, DisplayConnectionType::Internal});
441 } else {
442 ceDisplayArgs.setUseHwcVirtualDisplays(false);
443 }
444 ceDisplayArgs.setPixels({WIDTH, HEIGHT}).setPowerAdvisor(&test->mPowerAdvisor).build();
445
446 auto compositionDisplay =
447 compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
448 ceDisplayArgs.build());
449
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100450 auto injector = FakeDisplayDeviceInjector(test->mFlinger, compositionDisplay,
451 CONNECTION_TYPE::value, HWC_DISPLAY_ID_OPT::value,
452 static_cast<bool>(PRIMARY));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700453
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800454 injector.setSecure(static_cast<bool>(SECURE));
Alec Mouriba013fa2018-10-16 12:43:11 -0700455 injector.setNativeWindow(test->mNativeWindow);
456
457 // Creating a DisplayDevice requires getting default dimensions from the
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800458 // native window along with some other initial setup.
Alec Mouriba013fa2018-10-16 12:43:11 -0700459 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
460 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
461 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
462 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800463 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT))
464 .WillRepeatedly(Return(0));
465 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT))
466 .WillRepeatedly(Return(0));
467 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64))
468 .WillRepeatedly(Return(0));
chaviw8beb4142019-04-11 13:09:05 -0700469 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT))
470 .WillRepeatedly(Return(0));
471
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800472 return injector;
473 }
474
475 // Called by tests to set up any native window creation call expectations.
476 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
477 EXPECT_CALL(*test->mNativeWindowSurface, getNativeWindow())
478 .WillOnce(Return(test->mNativeWindow));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800479
Alec Mouriba013fa2018-10-16 12:43:11 -0700480 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
481 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
482 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
483 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800484 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT))
485 .WillRepeatedly(Return(0));
486 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT))
487 .WillRepeatedly(Return(0));
488 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64))
489 .WillRepeatedly(Return(0));
chaviw8beb4142019-04-11 13:09:05 -0700490 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT))
491 .WillRepeatedly(Return(0));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800492 }
493
494 static void setupFramebufferConsumerBufferQueueCallExpectations(DisplayTransactionTest* test) {
495 EXPECT_CALL(*test->mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR));
496 EXPECT_CALL(*test->mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR));
497 EXPECT_CALL(*test->mConsumer, setConsumerUsageBits(GRALLOC_USAGE))
498 .WillRepeatedly(Return(NO_ERROR));
499 EXPECT_CALL(*test->mConsumer, setDefaultBufferSize(WIDTH, HEIGHT))
500 .WillRepeatedly(Return(NO_ERROR));
501 EXPECT_CALL(*test->mConsumer, setMaxAcquiredBufferCount(_))
502 .WillRepeatedly(Return(NO_ERROR));
503 }
504
505 static void setupFramebufferProducerBufferQueueCallExpectations(DisplayTransactionTest* test) {
506 EXPECT_CALL(*test->mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return());
507 }
508};
509
Peiyong Line9d809e2020-04-14 13:10:48 -0700510template <HWDisplayId hwcDisplayId, DisplayType hwcDisplayType, typename DisplayVariant,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700511 typename PhysicalDisplay = void>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800512struct HwcDisplayVariant {
513 // The display id supplied by the HWC
Peiyong Line9d809e2020-04-14 13:10:48 -0700514 static constexpr HWDisplayId HWC_DISPLAY_ID = hwcDisplayId;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800515
516 // The HWC display type
Peiyong Line9d809e2020-04-14 13:10:48 -0700517 static constexpr DisplayType HWC_DISPLAY_TYPE = hwcDisplayType;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800518
519 // The HWC active configuration id
Lloyd Pique3c085a02018-05-09 19:38:32 -0700520 static constexpr int HWC_ACTIVE_CONFIG_ID = 2001;
Peiyong Lin65248e02020-04-18 21:15:07 -0700521 static constexpr PowerMode INIT_POWER_MODE = PowerMode::ON;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800522
Peiyong Line9d809e2020-04-14 13:10:48 -0700523 static void injectPendingHotplugEvent(DisplayTransactionTest* test, Connection connection) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800524 test->mFlinger.mutablePendingHotplugEvents().emplace_back(
525 HotplugEvent{HWC_DISPLAY_ID, connection});
526 }
527
528 // Called by tests to inject a HWC display setup
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800529 static void injectHwcDisplayWithNoDefaultCapabilities(DisplayTransactionTest* test) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700530 const auto displayId = DisplayVariant::DISPLAY_ID::get();
531 ASSERT_TRUE(displayId);
532 FakeHwcDisplayInjector(*displayId, HWC_DISPLAY_TYPE,
533 static_cast<bool>(DisplayVariant::PRIMARY))
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800534 .setHwcDisplayId(HWC_DISPLAY_ID)
535 .setWidth(DisplayVariant::WIDTH)
536 .setHeight(DisplayVariant::HEIGHT)
537 .setActiveConfig(HWC_ACTIVE_CONFIG_ID)
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700538 .setPowerMode(INIT_POWER_MODE)
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800539 .inject(&test->mFlinger, test->mComposer);
540 }
541
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800542 // Called by tests to inject a HWC display setup
543 static void injectHwcDisplay(DisplayTransactionTest* test) {
544 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(HWC_DISPLAY_ID, _))
Peiyong Line9d809e2020-04-14 13:10:48 -0700545 .WillOnce(DoAll(SetArgPointee<1>(std::vector<DisplayCapability>({})),
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800546 Return(Error::NONE)));
Peiyong Lin65248e02020-04-18 21:15:07 -0700547 EXPECT_CALL(*test->mComposer, setPowerMode(HWC_DISPLAY_ID, INIT_POWER_MODE))
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700548 .WillOnce(Return(Error::NONE));
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800549 injectHwcDisplayWithNoDefaultCapabilities(test);
550 }
551
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700552 static std::shared_ptr<compositionengine::Display> injectCompositionDisplay(
553 DisplayTransactionTest* test) {
554 const ::testing::TestInfo* const test_info =
555 ::testing::UnitTest::GetInstance()->current_test_info();
556
557 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder()
558 .setPhysical({*DisplayVariant::DISPLAY_ID::get(),
559 PhysicalDisplay::CONNECTION_TYPE})
560 .setPixels({DisplayVariant::WIDTH, DisplayVariant::HEIGHT})
561 .setIsSecure(static_cast<bool>(DisplayVariant::SECURE))
562 .setPowerAdvisor(&test->mPowerAdvisor)
563 .setName(std::string("Injected display for ") +
564 test_info->test_case_name() + "." + test_info->name())
565 .build();
566
567 return compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
568 ceDisplayArgs);
569 }
570
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800571 static void setupHwcHotplugCallExpectations(DisplayTransactionTest* test) {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800572 constexpr auto CONNECTION_TYPE =
573 PhysicalDisplay::CONNECTION_TYPE == DisplayConnectionType::Internal
574 ? IComposerClient::DisplayConnectionType::INTERNAL
575 : IComposerClient::DisplayConnectionType::EXTERNAL;
576
577 EXPECT_CALL(*test->mComposer, getDisplayConnectionType(HWC_DISPLAY_ID, _))
Peiyong Line9d809e2020-04-14 13:10:48 -0700578 .WillOnce(DoAll(SetArgPointee<1>(CONNECTION_TYPE), Return(hal::V2_4::Error::NONE)));
Dominik Laskowski55c85402020-01-21 16:25:47 -0800579
Peiyong Line9d809e2020-04-14 13:10:48 -0700580 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_))
581 .WillOnce(Return(hal::Error::NONE));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800582 EXPECT_CALL(*test->mComposer, getDisplayConfigs(HWC_DISPLAY_ID, _))
583 .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{HWC_ACTIVE_CONFIG_ID}),
584 Return(Error::NONE)));
585 EXPECT_CALL(*test->mComposer,
586 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
587 IComposerClient::Attribute::WIDTH, _))
588 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::WIDTH), Return(Error::NONE)));
589 EXPECT_CALL(*test->mComposer,
590 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
591 IComposerClient::Attribute::HEIGHT, _))
592 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::HEIGHT), Return(Error::NONE)));
593 EXPECT_CALL(*test->mComposer,
594 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
595 IComposerClient::Attribute::VSYNC_PERIOD, _))
596 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE)));
597 EXPECT_CALL(*test->mComposer,
598 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
599 IComposerClient::Attribute::DPI_X, _))
600 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
601 EXPECT_CALL(*test->mComposer,
602 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
603 IComposerClient::Attribute::DPI_Y, _))
604 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
Ady Abraham7159f572019-10-11 11:10:18 -0700605 EXPECT_CALL(*test->mComposer,
606 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
607 IComposerClient::Attribute::CONFIG_GROUP, _))
608 .WillOnce(DoAll(SetArgPointee<3>(-1), Return(Error::NONE)));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700609
610 if (PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
611 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
Marin Shalamanov7ce87642020-05-06 13:45:58 +0200612 .WillOnce(DoAll(SetArgPointee<1>(PhysicalDisplay::PORT),
613 SetArgPointee<2>(PhysicalDisplay::GET_IDENTIFICATION_DATA()),
614 Return(Error::NONE)));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700615 } else {
616 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
Marin Shalamanov7ce87642020-05-06 13:45:58 +0200617 .WillOnce(Return(Error::UNSUPPORTED));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700618 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800619 }
620
621 // Called by tests to set up HWC call expectations
622 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
623 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY_ID, _))
Lloyd Pique3c085a02018-05-09 19:38:32 -0700624 .WillRepeatedly(DoAll(SetArgPointee<1>(HWC_ACTIVE_CONFIG_ID), Return(Error::NONE)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800625 }
626};
627
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800628// Physical displays are expected to be synchronous, secure, and have a HWC display for output.
629constexpr uint32_t GRALLOC_USAGE_PHYSICAL_DISPLAY =
630 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
631
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100632template <typename PhysicalDisplay, int width, int height, Critical critical>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800633struct PhysicalDisplayVariant
Marin Shalamanova524a092020-07-27 21:39:55 +0200634 : DisplayVariant<PhysicalDisplayIdType<PhysicalDisplay>, width, height, critical,
635 Async::FALSE, Secure::TRUE, PhysicalDisplay::PRIMARY,
636 GRALLOC_USAGE_PHYSICAL_DISPLAY>,
Peiyong Line9d809e2020-04-14 13:10:48 -0700637 HwcDisplayVariant<PhysicalDisplay::HWC_DISPLAY_ID, DisplayType::PHYSICAL,
Marin Shalamanova524a092020-07-27 21:39:55 +0200638 DisplayVariant<PhysicalDisplayIdType<PhysicalDisplay>, width, height,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700639 critical, Async::FALSE, Secure::TRUE,
640 PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
641 PhysicalDisplay> {};
642
643template <bool hasIdentificationData>
644struct PrimaryDisplay {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800645 static constexpr auto CONNECTION_TYPE = DisplayConnectionType::Internal;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700646 static constexpr Primary PRIMARY = Primary::TRUE;
647 static constexpr uint8_t PORT = 255;
Peiyong Line9d809e2020-04-14 13:10:48 -0700648 static constexpr HWDisplayId HWC_DISPLAY_ID = 1001;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700649 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
650 static constexpr auto GET_IDENTIFICATION_DATA = getInternalEdid;
651};
652
653template <bool hasIdentificationData>
654struct ExternalDisplay {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800655 static constexpr auto CONNECTION_TYPE = DisplayConnectionType::External;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700656 static constexpr Primary PRIMARY = Primary::FALSE;
657 static constexpr uint8_t PORT = 254;
Peiyong Line9d809e2020-04-14 13:10:48 -0700658 static constexpr HWDisplayId HWC_DISPLAY_ID = 1002;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700659 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
660 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
661};
662
663struct TertiaryDisplay {
664 static constexpr Primary PRIMARY = Primary::FALSE;
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800665 static constexpr uint8_t PORT = 253;
Peiyong Line9d809e2020-04-14 13:10:48 -0700666 static constexpr HWDisplayId HWC_DISPLAY_ID = 1003;
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800667 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700668};
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800669
670// A primary display is a physical display that is critical
671using PrimaryDisplayVariant =
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100672 PhysicalDisplayVariant<PrimaryDisplay<false>, 3840, 2160, Critical::TRUE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800673
674// An external display is physical display that is not critical.
675using ExternalDisplayVariant =
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100676 PhysicalDisplayVariant<ExternalDisplay<false>, 1920, 1280, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800677
Marin Shalamanov4a42d432020-02-12 20:22:26 +0100678using TertiaryDisplayVariant = PhysicalDisplayVariant<TertiaryDisplay, 1600, 1200, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800679
680// A virtual display not supported by the HWC.
681constexpr uint32_t GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY = 0;
682
683template <int width, int height, Secure secure>
684struct NonHwcVirtualDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700685 : DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
686 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY> {
687 using Base = DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
688 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>;
689
690 static void injectHwcDisplay(DisplayTransactionTest*) {}
691
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700692 static std::shared_ptr<compositionengine::Display> injectCompositionDisplay(
693 DisplayTransactionTest* test) {
694 const ::testing::TestInfo* const test_info =
695 ::testing::UnitTest::GetInstance()->current_test_info();
696
697 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder()
698 .setPixels({Base::WIDTH, Base::HEIGHT})
699 .setIsSecure(static_cast<bool>(Base::SECURE))
700 .setPowerAdvisor(&test->mPowerAdvisor)
701 .setName(std::string("Injected display for ") +
702 test_info->test_case_name() + "." + test_info->name())
703 .build();
704
705 return compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
706 ceDisplayArgs);
707 }
708
Dominik Laskowski075d3172018-05-24 15:50:06 -0700709 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
710 EXPECT_CALL(*test->mComposer, getActiveConfig(_, _)).Times(0);
711 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800712
713 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
714 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
715 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
716 }
717};
718
719// A virtual display supported by the HWC.
720constexpr uint32_t GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY = GRALLOC_USAGE_HW_COMPOSER;
721
722template <int width, int height, Secure secure>
723struct HwcVirtualDisplayVariant
Marin Shalamanova524a092020-07-27 21:39:55 +0200724 : DisplayVariant<VirtualDisplayIdType<42>, width, height, Critical::FALSE, Async::TRUE,
725 secure, Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>,
726 HwcDisplayVariant<HWC_VIRTUAL_DISPLAY_HWC_DISPLAY_ID, DisplayType::VIRTUAL,
727 DisplayVariant<VirtualDisplayIdType<42>, width, height, Critical::FALSE,
728 Async::TRUE, secure, Primary::FALSE,
729 GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>> {
730 using Base = DisplayVariant<VirtualDisplayIdType<42>, width, height, Critical::FALSE,
731 Async::TRUE, secure, Primary::FALSE, GRALLOC_USAGE_HW_COMPOSER>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800732 using Self = HwcVirtualDisplayVariant<width, height, secure>;
733
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700734 static std::shared_ptr<compositionengine::Display> injectCompositionDisplay(
735 DisplayTransactionTest* test) {
736 const ::testing::TestInfo* const test_info =
737 ::testing::UnitTest::GetInstance()->current_test_info();
738
739 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder()
740 .setUseHwcVirtualDisplays(false)
741 .setPixels({Base::WIDTH, Base::HEIGHT})
742 .setIsSecure(static_cast<bool>(Base::SECURE))
743 .setPowerAdvisor(&test->mPowerAdvisor)
744 .setName(std::string("Injected display for ") +
745 test_info->test_case_name() + "." + test_info->name())
746 .build();
747
748 auto compositionDisplay =
749 compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
750 ceDisplayArgs);
751 compositionDisplay->setDisplayIdForTesting(Base::DISPLAY_ID::get());
752
753 // Insert display data so that the HWC thinks it created the virtual display.
754 if (const auto displayId = Base::DISPLAY_ID::get()) {
755 test->mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
756 }
757
758 return compositionDisplay;
759 }
760
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800761 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
762 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
763 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
764 }
765
766 static void setupHwcVirtualDisplayCreationCallExpectations(DisplayTransactionTest* test) {
767 EXPECT_CALL(*test->mComposer, createVirtualDisplay(Base::WIDTH, Base::HEIGHT, _, _))
768 .WillOnce(DoAll(SetArgPointee<3>(Self::HWC_DISPLAY_ID), Return(Error::NONE)));
769 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
770 }
771};
772
773// For this variant, SurfaceFlinger should not configure itself with wide
774// display support, so the display should not be configured for wide-color
775// support.
776struct WideColorSupportNotConfiguredVariant {
777 static constexpr bool WIDE_COLOR_SUPPORTED = false;
778
779 static void injectConfigChange(DisplayTransactionTest* test) {
780 test->mFlinger.mutableHasWideColorDisplay() = false;
Peiyong Lin13effd12018-07-24 17:01:47 -0700781 test->mFlinger.mutableUseColorManagement() = false;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800782 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800783 }
784
785 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
786 EXPECT_CALL(*test->mComposer, getColorModes(_, _)).Times(0);
787 EXPECT_CALL(*test->mComposer, getRenderIntents(_, _, _)).Times(0);
788 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
789 }
790};
791
792// For this variant, SurfaceFlinger should configure itself with wide display
793// support, and the display should respond with an non-empty list of supported
794// color modes. Wide-color support should be configured.
795template <typename Display>
796struct WideColorP3ColorimetricSupportedVariant {
797 static constexpr bool WIDE_COLOR_SUPPORTED = true;
798
799 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700800 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800801 test->mFlinger.mutableHasWideColorDisplay() = true;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800802 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800803 }
804
805 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800806 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_DATASPACE)).Times(1);
807
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800808 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
809 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>({ColorMode::DISPLAY_P3})),
810 Return(Error::NONE)));
811 EXPECT_CALL(*test->mComposer,
812 getRenderIntents(Display::HWC_DISPLAY_ID, ColorMode::DISPLAY_P3, _))
813 .WillOnce(DoAll(SetArgPointee<2>(
814 std::vector<RenderIntent>({RenderIntent::COLORIMETRIC})),
815 Return(Error::NONE)));
816 EXPECT_CALL(*test->mComposer,
817 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::SRGB,
818 RenderIntent::COLORIMETRIC))
819 .WillOnce(Return(Error::NONE));
820 }
821};
822
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800823// For this variant, SurfaceFlinger should configure itself with wide display
824// support, but the display should respond with an empty list of supported color
825// modes. Wide-color support for the display should not be configured.
826template <typename Display>
827struct WideColorNotSupportedVariant {
828 static constexpr bool WIDE_COLOR_SUPPORTED = false;
829
830 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700831 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800832 test->mFlinger.mutableHasWideColorDisplay() = true;
833 }
834
835 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
836 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
837 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>()), Return(Error::NONE)));
Chia-I Wu614e1422018-05-23 02:17:03 -0700838 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800839 }
840};
841
842// For this variant, the display is not a HWC display, so no HDR support should
843// be configured.
844struct NonHwcDisplayHdrSupportVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800845 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800846 static constexpr bool HDR10_SUPPORTED = false;
847 static constexpr bool HDR_HLG_SUPPORTED = false;
848 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
849 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
850 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
851 }
852};
853
Valerie Haue9e843a2018-12-18 13:39:23 -0800854template <typename Display>
855struct Hdr10PlusSupportedVariant {
856 static constexpr bool HDR10_PLUS_SUPPORTED = true;
857 static constexpr bool HDR10_SUPPORTED = true;
858 static constexpr bool HDR_HLG_SUPPORTED = false;
859 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
860 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
861 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _))
862 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({
863 Hdr::HDR10_PLUS,
864 Hdr::HDR10,
865 })),
866 Return(Error::NONE)));
867 }
868};
869
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800870// For this variant, the composer should respond with a non-empty list of HDR
871// modes containing HDR10, so HDR10 support should be configured.
872template <typename Display>
873struct Hdr10SupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800874 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800875 static constexpr bool HDR10_SUPPORTED = true;
876 static constexpr bool HDR_HLG_SUPPORTED = false;
877 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
878 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
879 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
880 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HDR10})),
881 Return(Error::NONE)));
882 }
883};
884
885// For this variant, the composer should respond with a non-empty list of HDR
886// modes containing HLG, so HLG support should be configured.
887template <typename Display>
888struct HdrHlgSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800889 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800890 static constexpr bool HDR10_SUPPORTED = false;
891 static constexpr bool HDR_HLG_SUPPORTED = true;
892 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
893 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
894 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
895 .WillOnce(
896 DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HLG})), Return(Error::NONE)));
897 }
898};
899
900// For this variant, the composer should respond with a non-empty list of HDR
901// modes containing DOLBY_VISION, so DOLBY_VISION support should be configured.
902template <typename Display>
903struct HdrDolbyVisionSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800904 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800905 static constexpr bool HDR10_SUPPORTED = false;
906 static constexpr bool HDR_HLG_SUPPORTED = false;
907 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = true;
908 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
909 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
910 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::DOLBY_VISION})),
911 Return(Error::NONE)));
912 }
913};
914
915// For this variant, the composer should respond with am empty list of HDR
916// modes, so no HDR support should be configured.
917template <typename Display>
918struct HdrNotSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800919 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800920 static constexpr bool HDR10_SUPPORTED = false;
921 static constexpr bool HDR_HLG_SUPPORTED = false;
922 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
923 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
924 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
925 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
926 }
927};
928
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700929struct NonHwcPerFrameMetadataSupportVariant {
930 static constexpr int PER_FRAME_METADATA_KEYS = 0;
931 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800932 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(_)).Times(0);
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700933 }
934};
935
936template <typename Display>
937struct NoPerFrameMetadataSupportVariant {
938 static constexpr int PER_FRAME_METADATA_KEYS = 0;
939 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800940 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
941 .WillOnce(Return(std::vector<PerFrameMetadataKey>()));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700942 }
943};
944
945template <typename Display>
946struct Smpte2086PerFrameMetadataSupportVariant {
947 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::SMPTE2086;
948 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800949 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
950 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Valerie Haue9e843a2018-12-18 13:39:23 -0800951 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X,
952 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y,
953 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X,
954 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y,
955 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X,
956 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y,
957 PerFrameMetadataKey::WHITE_POINT_X,
958 PerFrameMetadataKey::WHITE_POINT_Y,
959 PerFrameMetadataKey::MAX_LUMINANCE,
960 PerFrameMetadataKey::MIN_LUMINANCE,
961 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700962 }
963};
964
965template <typename Display>
966struct Cta861_3_PerFrameMetadataSupportVariant {
967 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::CTA861_3;
968 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800969 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
970 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Valerie Haue9e843a2018-12-18 13:39:23 -0800971 PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL,
972 PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL,
973 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700974 }
975};
976
Valerie Haue9e843a2018-12-18 13:39:23 -0800977template <typename Display>
978struct Hdr10_Plus_PerFrameMetadataSupportVariant {
979 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::HDR10PLUS;
980 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
981 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
982 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
983 PerFrameMetadataKey::HDR10_PLUS_SEI,
984 })));
985 }
986};
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800987/* ------------------------------------------------------------------------
988 * Typical display configurations to test
989 */
990
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700991template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy,
992 typename PerFrameMetadataSupportPolicy>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800993struct Case {
994 using Display = DisplayPolicy;
995 using WideColorSupport = WideColorSupportPolicy;
996 using HdrSupport = HdrSupportPolicy;
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700997 using PerFrameMetadataSupport = PerFrameMetadataSupportPolicy;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800998};
999
1000using SimplePrimaryDisplayCase =
1001 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001002 HdrNotSupportedVariant<PrimaryDisplayVariant>,
1003 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001004using SimpleExternalDisplayCase =
1005 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001006 HdrNotSupportedVariant<ExternalDisplayVariant>,
1007 NoPerFrameMetadataSupportVariant<ExternalDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001008using SimpleTertiaryDisplayCase =
1009 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001010 HdrNotSupportedVariant<TertiaryDisplayVariant>,
1011 NoPerFrameMetadataSupportVariant<TertiaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001012using NonHwcVirtualDisplayCase =
1013 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001014 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant,
1015 NonHwcPerFrameMetadataSupportVariant>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001016using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
1017using HwcVirtualDisplayCase =
1018 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
Lloyd Pique438e9e72018-09-04 18:06:08 -07001019 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>,
tangrobin6753a022018-08-10 10:58:54 +08001020 NoPerFrameMetadataSupportVariant<SimpleHwcVirtualDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001021using WideColorP3ColorimetricDisplayCase =
1022 Case<PrimaryDisplayVariant, WideColorP3ColorimetricSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001023 HdrNotSupportedVariant<PrimaryDisplayVariant>,
1024 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Valerie Haue9e843a2018-12-18 13:39:23 -08001025using Hdr10PlusDisplayCase =
1026 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
1027 Hdr10SupportedVariant<PrimaryDisplayVariant>,
1028 Hdr10_Plus_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001029using Hdr10DisplayCase =
1030 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001031 Hdr10SupportedVariant<PrimaryDisplayVariant>,
1032 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001033using HdrHlgDisplayCase =
1034 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001035 HdrHlgSupportedVariant<PrimaryDisplayVariant>,
1036 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001037using HdrDolbyVisionDisplayCase =
1038 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001039 HdrDolbyVisionSupportedVariant<PrimaryDisplayVariant>,
1040 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
1041using HdrSmpte2086DisplayCase =
1042 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
1043 HdrNotSupportedVariant<PrimaryDisplayVariant>,
1044 Smpte2086PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
1045using HdrCta861_3_DisplayCase =
1046 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
1047 HdrNotSupportedVariant<PrimaryDisplayVariant>,
1048 Cta861_3_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Dominik Laskowskif07b85b2018-06-11 12:49:15 -07001049
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001050/* ------------------------------------------------------------------------
Lloyd Pique6cf11032018-01-22 18:57:44 -08001051 *
1052 * SurfaceFlinger::onHotplugReceived
1053 */
1054
1055TEST_F(DisplayTransactionTest, hotplugEnqueuesEventsForDisplayTransaction) {
1056 constexpr int currentSequenceId = 123;
Peiyong Line9d809e2020-04-14 13:10:48 -07001057 constexpr HWDisplayId hwcDisplayId1 = 456;
1058 constexpr HWDisplayId hwcDisplayId2 = 654;
Lloyd Pique6cf11032018-01-22 18:57:44 -08001059
1060 // --------------------------------------------------------------------
1061 // Preconditions
1062
1063 // Set the current sequence id for accepted events
1064 mFlinger.mutableComposerSequenceId() = currentSequenceId;
1065
1066 // Set the main thread id so that the current thread does not appear to be
1067 // the main thread.
1068 mFlinger.mutableMainThreadId() = std::thread::id();
1069
1070 // --------------------------------------------------------------------
1071 // Call Expectations
1072
1073 // We expect invalidate() to be invoked once to trigger display transaction
1074 // processing.
1075 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1076
1077 // --------------------------------------------------------------------
1078 // Invocation
1079
1080 // Simulate two hotplug events (a connect and a disconnect)
Peiyong Line9d809e2020-04-14 13:10:48 -07001081 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId1, Connection::CONNECTED);
1082 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId2, Connection::DISCONNECTED);
Lloyd Pique6cf11032018-01-22 18:57:44 -08001083
1084 // --------------------------------------------------------------------
1085 // Postconditions
1086
1087 // The display transaction needed flag should be set.
1088 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1089
1090 // All events should be in the pending event queue.
1091 const auto& pendingEvents = mFlinger.mutablePendingHotplugEvents();
1092 ASSERT_EQ(2u, pendingEvents.size());
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001093 EXPECT_EQ(hwcDisplayId1, pendingEvents[0].hwcDisplayId);
Peiyong Line9d809e2020-04-14 13:10:48 -07001094 EXPECT_EQ(Connection::CONNECTED, pendingEvents[0].connection);
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001095 EXPECT_EQ(hwcDisplayId2, pendingEvents[1].hwcDisplayId);
Peiyong Line9d809e2020-04-14 13:10:48 -07001096 EXPECT_EQ(Connection::DISCONNECTED, pendingEvents[1].connection);
Lloyd Pique6cf11032018-01-22 18:57:44 -08001097}
1098
1099TEST_F(DisplayTransactionTest, hotplugDiscardsUnexpectedEvents) {
1100 constexpr int currentSequenceId = 123;
1101 constexpr int otherSequenceId = 321;
Peiyong Line9d809e2020-04-14 13:10:48 -07001102 constexpr HWDisplayId displayId = 456;
Lloyd Pique6cf11032018-01-22 18:57:44 -08001103
1104 // --------------------------------------------------------------------
1105 // Preconditions
1106
1107 // Set the current sequence id for accepted events
1108 mFlinger.mutableComposerSequenceId() = currentSequenceId;
1109
1110 // Set the main thread id so that the current thread does not appear to be
1111 // the main thread.
1112 mFlinger.mutableMainThreadId() = std::thread::id();
1113
1114 // --------------------------------------------------------------------
1115 // Call Expectations
1116
1117 // We do not expect any calls to invalidate().
1118 EXPECT_CALL(*mMessageQueue, invalidate()).Times(0);
1119
1120 // --------------------------------------------------------------------
1121 // Invocation
1122
1123 // Call with an unexpected sequence id
Peiyong Line9d809e2020-04-14 13:10:48 -07001124 mFlinger.onHotplugReceived(otherSequenceId, displayId, Connection::INVALID);
Lloyd Pique6cf11032018-01-22 18:57:44 -08001125
1126 // --------------------------------------------------------------------
1127 // Postconditions
1128
1129 // The display transaction needed flag should not be set
1130 EXPECT_FALSE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1131
1132 // There should be no pending events
1133 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
1134}
1135
1136TEST_F(DisplayTransactionTest, hotplugProcessesEnqueuedEventsIfCalledOnMainThread) {
1137 constexpr int currentSequenceId = 123;
Peiyong Line9d809e2020-04-14 13:10:48 -07001138 constexpr HWDisplayId displayId1 = 456;
Lloyd Pique6cf11032018-01-22 18:57:44 -08001139
1140 // --------------------------------------------------------------------
1141 // Note:
1142 // --------------------------------------------------------------------
1143 // This test case is a bit tricky. We want to verify that
1144 // onHotplugReceived() calls processDisplayHotplugEventsLocked(), but we
1145 // don't really want to provide coverage for everything the later function
1146 // does as there are specific tests for it.
1147 // --------------------------------------------------------------------
1148
1149 // --------------------------------------------------------------------
1150 // Preconditions
1151
1152 // Set the current sequence id for accepted events
1153 mFlinger.mutableComposerSequenceId() = currentSequenceId;
1154
1155 // Set the main thread id so that the current thread does appear to be the
1156 // main thread.
1157 mFlinger.mutableMainThreadId() = std::this_thread::get_id();
1158
1159 // --------------------------------------------------------------------
1160 // Call Expectations
1161
1162 // We expect invalidate() to be invoked once to trigger display transaction
1163 // processing.
1164 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1165
1166 // --------------------------------------------------------------------
1167 // Invocation
1168
1169 // Simulate a disconnect on a display id that is not connected. This should
1170 // be enqueued by onHotplugReceived(), and dequeued by
1171 // processDisplayHotplugEventsLocked(), but then ignored as invalid.
Peiyong Line9d809e2020-04-14 13:10:48 -07001172 mFlinger.onHotplugReceived(currentSequenceId, displayId1, Connection::DISCONNECTED);
Lloyd Pique6cf11032018-01-22 18:57:44 -08001173
1174 // --------------------------------------------------------------------
1175 // Postconditions
1176
1177 // The display transaction needed flag should be set.
1178 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1179
1180 // There should be no event queued on return, as it should have been
1181 // processed.
1182 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
1183}
1184
1185/* ------------------------------------------------------------------------
Lloyd Piquea482f992018-01-22 19:00:34 -08001186 * SurfaceFlinger::createDisplay
1187 */
1188
1189TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForNonsecureDisplay) {
1190 const String8 name("virtual.test");
1191
1192 // --------------------------------------------------------------------
1193 // Call Expectations
1194
1195 // The call should notify the interceptor that a display was created.
1196 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
1197
1198 // --------------------------------------------------------------------
1199 // Invocation
1200
1201 sp<IBinder> displayToken = mFlinger.createDisplay(name, false);
1202
1203 // --------------------------------------------------------------------
1204 // Postconditions
1205
1206 // The display should have been added to the current state
1207 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1208 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001209 EXPECT_TRUE(display.isVirtual());
1210 EXPECT_FALSE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -08001211 EXPECT_EQ(name.string(), display.displayName);
1212
1213 // --------------------------------------------------------------------
1214 // Cleanup conditions
1215
1216 // Destroying the display invalidates the display state.
1217 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1218}
1219
1220TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForSecureDisplay) {
1221 const String8 name("virtual.test");
1222
1223 // --------------------------------------------------------------------
1224 // Call Expectations
1225
1226 // The call should notify the interceptor that a display was created.
1227 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
1228
1229 // --------------------------------------------------------------------
1230 // Invocation
1231
1232 sp<IBinder> displayToken = mFlinger.createDisplay(name, true);
1233
1234 // --------------------------------------------------------------------
1235 // Postconditions
1236
1237 // The display should have been added to the current state
1238 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1239 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001240 EXPECT_TRUE(display.isVirtual());
1241 EXPECT_TRUE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -08001242 EXPECT_EQ(name.string(), display.displayName);
1243
1244 // --------------------------------------------------------------------
1245 // Cleanup conditions
1246
1247 // Destroying the display invalidates the display state.
1248 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1249}
1250
1251/* ------------------------------------------------------------------------
1252 * SurfaceFlinger::destroyDisplay
1253 */
1254
1255TEST_F(DisplayTransactionTest, destroyDisplayClearsCurrentStateForDisplay) {
1256 using Case = NonHwcVirtualDisplayCase;
1257
1258 // --------------------------------------------------------------------
1259 // Preconditions
1260
1261 // A virtual display exists
1262 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1263 existing.inject();
1264
1265 // --------------------------------------------------------------------
1266 // Call Expectations
1267
1268 // The call should notify the interceptor that a display was created.
1269 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
1270
1271 // Destroying the display invalidates the display state.
1272 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1273
1274 // --------------------------------------------------------------------
1275 // Invocation
1276
1277 mFlinger.destroyDisplay(existing.token());
1278
1279 // --------------------------------------------------------------------
1280 // Postconditions
1281
1282 // The display should have been removed from the current state
1283 EXPECT_FALSE(hasCurrentDisplayState(existing.token()));
1284
1285 // Ths display should still exist in the drawing state
1286 EXPECT_TRUE(hasDrawingDisplayState(existing.token()));
1287
1288 // The display transaction needed flasg should be set
1289 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1290}
1291
1292TEST_F(DisplayTransactionTest, destroyDisplayHandlesUnknownDisplay) {
1293 // --------------------------------------------------------------------
1294 // Preconditions
1295
1296 sp<BBinder> displayToken = new BBinder();
1297
1298 // --------------------------------------------------------------------
1299 // Invocation
1300
1301 mFlinger.destroyDisplay(displayToken);
1302}
1303
1304/* ------------------------------------------------------------------------
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001305 * SurfaceFlinger::resetDisplayState
1306 */
1307
1308TEST_F(DisplayTransactionTest, resetDisplayStateClearsState) {
1309 using Case = NonHwcVirtualDisplayCase;
1310
1311 // --------------------------------------------------------------------
1312 // Preconditions
1313
1314 // vsync is enabled and available
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -07001315 mFlinger.scheduler()->mutablePrimaryHWVsyncEnabled() = true;
1316 mFlinger.scheduler()->mutableHWVsyncAvailable() = true;
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001317
1318 // A display exists
1319 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1320 existing.inject();
1321
1322 // --------------------------------------------------------------------
1323 // Call Expectations
1324
1325 // The call disable vsyncs
Dominik Laskowski8b01cc02020-07-14 19:02:41 -07001326 EXPECT_CALL(mSchedulerCallback, setVsyncEnabled(false)).Times(1);
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001327
Lloyd Pique41be5d22018-06-21 13:11:48 -07001328 // The call ends any display resyncs
1329 EXPECT_CALL(*mPrimaryDispSync, endResync()).Times(1);
1330
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001331 // --------------------------------------------------------------------
1332 // Invocation
1333
1334 mFlinger.resetDisplayState();
1335
1336 // --------------------------------------------------------------------
1337 // Postconditions
1338
1339 // vsyncs should be off and not available.
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -07001340 EXPECT_FALSE(mFlinger.scheduler()->mutablePrimaryHWVsyncEnabled());
1341 EXPECT_FALSE(mFlinger.scheduler()->mutableHWVsyncAvailable());
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001342
1343 // The display should have been removed from the display map.
1344 EXPECT_FALSE(hasDisplayDevice(existing.token()));
1345
1346 // The display should still exist in the current state
1347 EXPECT_TRUE(hasCurrentDisplayState(existing.token()));
1348
1349 // The display should have been removed from the drawing state
1350 EXPECT_FALSE(hasDrawingDisplayState(existing.token()));
1351}
1352
1353/* ------------------------------------------------------------------------
Lais Andrade3a6e47d2020-04-02 11:20:16 +01001354 * SurfaceFlinger::notifyPowerBoost
1355 */
1356
1357TEST_F(DisplayTransactionTest, notifyPowerBoostNotifiesTouchEvent) {
1358 mFlinger.scheduler()->replaceTouchTimer(100);
1359 std::this_thread::sleep_for(10ms); // wait for callback to be triggered
1360 EXPECT_TRUE(mFlinger.scheduler()->isTouchActive()); // Starting timer activates touch
1361
1362 std::this_thread::sleep_for(110ms); // wait for reset touch timer to expire and trigger callback
1363 EXPECT_FALSE(mFlinger.scheduler()->isTouchActive());
1364
1365 EXPECT_EQ(NO_ERROR, mFlinger.notifyPowerBoost(static_cast<int32_t>(Boost::CAMERA_SHOT)));
1366 std::this_thread::sleep_for(10ms); // wait for callback to maybe be triggered
1367 EXPECT_FALSE(mFlinger.scheduler()->isTouchActive());
1368
1369 std::this_thread::sleep_for(110ms); // wait for reset touch timer to expire and trigger callback
1370 EXPECT_FALSE(mFlinger.scheduler()->isTouchActive());
1371
1372 EXPECT_EQ(NO_ERROR, mFlinger.notifyPowerBoost(static_cast<int32_t>(Boost::INTERACTION)));
1373 std::this_thread::sleep_for(10ms); // wait for callback to be triggered.
1374 EXPECT_TRUE(mFlinger.scheduler()->isTouchActive());
1375}
1376
1377/* ------------------------------------------------------------------------
Valerie Hau9758ae02018-10-09 16:05:09 -07001378 * DisplayDevice::GetBestColorMode
1379 */
1380class GetBestColorModeTest : public DisplayTransactionTest {
1381public:
Valerie Hau9758ae02018-10-09 16:05:09 -07001382 void setHasWideColorGamut(bool hasWideColorGamut) { mHasWideColorGamut = hasWideColorGamut; }
1383
1384 void addHwcColorModesMapping(ui::ColorMode colorMode,
1385 std::vector<ui::RenderIntent> renderIntents) {
1386 mHwcColorModes[colorMode] = renderIntents;
1387 }
1388
1389 void setInputDataspace(ui::Dataspace dataspace) { mInputDataspace = dataspace; }
1390
1391 void setInputRenderIntent(ui::RenderIntent renderIntent) { mInputRenderIntent = renderIntent; }
1392
1393 void getBestColorMode() {
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -07001394 auto displayDevice =
1395 injectDefaultInternalDisplay([this](FakeDisplayDeviceInjector& injector) {
1396 injector.setHwcColorModes(mHwcColorModes);
1397 injector.setHasWideColorGamut(mHasWideColorGamut);
1398 injector.setNativeWindow(mNativeWindow);
1399 });
Valerie Hau9758ae02018-10-09 16:05:09 -07001400
Lloyd Pique3d0c02e2018-10-19 18:38:12 -07001401 displayDevice->getCompositionDisplay()
1402 ->getDisplayColorProfile()
1403 ->getBestColorMode(mInputDataspace, mInputRenderIntent, &mOutDataspace,
1404 &mOutColorMode, &mOutRenderIntent);
Valerie Hau9758ae02018-10-09 16:05:09 -07001405 }
1406
1407 ui::Dataspace mOutDataspace;
1408 ui::ColorMode mOutColorMode;
1409 ui::RenderIntent mOutRenderIntent;
1410
1411private:
1412 ui::Dataspace mInputDataspace;
1413 ui::RenderIntent mInputRenderIntent;
1414 bool mHasWideColorGamut = false;
1415 std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> mHwcColorModes;
Valerie Hau9758ae02018-10-09 16:05:09 -07001416};
1417
1418TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeSRGB) {
1419 addHwcColorModesMapping(ui::ColorMode::SRGB,
1420 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1421 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1422 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1423 setHasWideColorGamut(true);
1424
1425 getBestColorMode();
1426
Peiyong Lin14724e62018-12-05 07:27:30 -08001427 ASSERT_EQ(ui::Dataspace::V0_SRGB, mOutDataspace);
Valerie Hau9758ae02018-10-09 16:05:09 -07001428 ASSERT_EQ(ui::ColorMode::SRGB, mOutColorMode);
1429 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1430}
1431
1432TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDisplayP3) {
1433 addHwcColorModesMapping(ui::ColorMode::DISPLAY_P3,
1434 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1435 addHwcColorModesMapping(ui::ColorMode::SRGB,
1436 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1437 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1438 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1439 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1440 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1441 setHasWideColorGamut(true);
1442
1443 getBestColorMode();
1444
1445 ASSERT_EQ(ui::Dataspace::DISPLAY_P3, mOutDataspace);
1446 ASSERT_EQ(ui::ColorMode::DISPLAY_P3, mOutColorMode);
1447 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1448}
1449
1450TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDISPLAY_BT2020) {
1451 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1452 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1453 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1454 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1455 setHasWideColorGamut(true);
1456
1457 getBestColorMode();
1458
1459 ASSERT_EQ(ui::Dataspace::DISPLAY_BT2020, mOutDataspace);
1460 ASSERT_EQ(ui::ColorMode::DISPLAY_BT2020, mOutColorMode);
1461 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1462}
1463
1464/* ------------------------------------------------------------------------
Lloyd Pique33050472019-12-19 17:12:44 -08001465 * DisplayDevice::setProjection
1466 */
1467
1468class DisplayDeviceSetProjectionTest : public DisplayTransactionTest {
1469public:
Lloyd Pique33050472019-12-19 17:12:44 -08001470 static constexpr int32_t DEFAULT_DISPLAY_WIDTH = 1080; // arbitrary
1471 static constexpr int32_t DEFAULT_DISPLAY_HEIGHT = 1920; // arbitrary
1472
1473 static constexpr int32_t TRANSFORM_FLAGS_ROT_0 = 0;
1474 static constexpr int32_t TRANSFORM_FLAGS_ROT_90 = HAL_TRANSFORM_ROT_90;
1475 static constexpr int32_t TRANSFORM_FLAGS_ROT_180 = HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_FLIP_V;
1476 static constexpr int32_t TRANSFORM_FLAGS_ROT_270 =
1477 HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
1478
1479 DisplayDeviceSetProjectionTest(ui::Size flingerDisplaySize, ui::Size hardwareDisplaySize,
1480 ui::Rotation physicalOrientation)
1481 : mFlingerDisplaySize(flingerDisplaySize),
1482 mHardwareDisplaySize(hardwareDisplaySize),
1483 mPhysicalOrientation(physicalOrientation),
1484 mDisplayDevice(createDisplayDevice()) {}
1485
1486 sp<DisplayDevice> createDisplayDevice() {
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -07001487 return injectDefaultInternalDisplay([this](FakeDisplayDeviceInjector& injector) {
1488 injector.setPhysicalOrientation(mPhysicalOrientation);
1489 });
Lloyd Pique33050472019-12-19 17:12:44 -08001490 }
1491
1492 ui::Size SwapWH(const ui::Size size) const { return ui::Size(size.height, size.width); }
1493
1494 void setProjectionForRotation0() {
1495 // A logical rotation of 0 uses the SurfaceFlinger display size
1496 mDisplayDevice->setProjection(ui::ROTATION_0, Rect(mFlingerDisplaySize),
1497 Rect(mFlingerDisplaySize));
1498 }
1499
1500 void setProjectionForRotation90() {
1501 // A logical rotation of 90 uses the SurfaceFlinger display size with
1502 // the width/height swapped.
1503 mDisplayDevice->setProjection(ui::ROTATION_90, Rect(SwapWH(mFlingerDisplaySize)),
1504 Rect(SwapWH(mFlingerDisplaySize)));
1505 }
1506
1507 void setProjectionForRotation180() {
1508 // A logical rotation of 180 uses the SurfaceFlinger display size
1509 mDisplayDevice->setProjection(ui::ROTATION_180, Rect(mFlingerDisplaySize),
1510 Rect(mFlingerDisplaySize));
1511 }
1512
1513 void setProjectionForRotation270() {
1514 // A logical rotation of 270 uses the SurfaceFlinger display size with
1515 // the width/height swapped.
1516 mDisplayDevice->setProjection(ui::ROTATION_270, Rect(SwapWH(mFlingerDisplaySize)),
1517 Rect(SwapWH(mFlingerDisplaySize)));
1518 }
1519
1520 void expectStateForHardwareTransform0() {
1521 const auto& compositionState = mDisplayDevice->getCompositionDisplay()->getState();
1522 EXPECT_EQ(ui::Transform(TRANSFORM_FLAGS_ROT_0, mHardwareDisplaySize.width,
1523 mHardwareDisplaySize.height),
1524 compositionState.transform);
1525 EXPECT_EQ(TRANSFORM_FLAGS_ROT_0, compositionState.orientation);
Lloyd Piquee8fe4742020-01-21 15:26:18 -08001526 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.destinationClip);
Lloyd Pique33050472019-12-19 17:12:44 -08001527 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.frame);
1528 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.viewport);
1529 EXPECT_EQ(false, compositionState.needsFiltering);
1530 }
1531
1532 void expectStateForHardwareTransform90() {
1533 const auto& compositionState = mDisplayDevice->getCompositionDisplay()->getState();
1534 EXPECT_EQ(ui::Transform(TRANSFORM_FLAGS_ROT_90, mHardwareDisplaySize.width,
1535 mHardwareDisplaySize.height),
1536 compositionState.transform);
1537 EXPECT_EQ(TRANSFORM_FLAGS_ROT_90, compositionState.orientation);
Lloyd Piquee8fe4742020-01-21 15:26:18 -08001538 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.destinationClip);
Lloyd Pique33050472019-12-19 17:12:44 -08001539 // For 90, the frame and viewport have the hardware display size width and height swapped
1540 EXPECT_EQ(Rect(SwapWH(mHardwareDisplaySize)), compositionState.frame);
1541 EXPECT_EQ(Rect(SwapWH(mHardwareDisplaySize)), compositionState.viewport);
1542 EXPECT_EQ(false, compositionState.needsFiltering);
1543 }
1544
1545 void expectStateForHardwareTransform180() {
1546 const auto& compositionState = mDisplayDevice->getCompositionDisplay()->getState();
1547 EXPECT_EQ(ui::Transform(TRANSFORM_FLAGS_ROT_180, mHardwareDisplaySize.width,
1548 mHardwareDisplaySize.height),
1549 compositionState.transform);
1550 EXPECT_EQ(TRANSFORM_FLAGS_ROT_180, compositionState.orientation);
Lloyd Pique33050472019-12-19 17:12:44 -08001551 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.frame);
1552 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.viewport);
1553 EXPECT_EQ(false, compositionState.needsFiltering);
1554 }
1555
1556 void expectStateForHardwareTransform270() {
1557 const auto& compositionState = mDisplayDevice->getCompositionDisplay()->getState();
1558 EXPECT_EQ(ui::Transform(TRANSFORM_FLAGS_ROT_270, mHardwareDisplaySize.width,
1559 mHardwareDisplaySize.height),
1560 compositionState.transform);
1561 EXPECT_EQ(TRANSFORM_FLAGS_ROT_270, compositionState.orientation);
Lloyd Piquee8fe4742020-01-21 15:26:18 -08001562 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.destinationClip);
Lloyd Pique33050472019-12-19 17:12:44 -08001563 // For 270, the frame and viewport have the hardware display size width and height swapped
1564 EXPECT_EQ(Rect(SwapWH(mHardwareDisplaySize)), compositionState.frame);
1565 EXPECT_EQ(Rect(SwapWH(mHardwareDisplaySize)), compositionState.viewport);
1566 EXPECT_EQ(false, compositionState.needsFiltering);
1567 }
1568
1569 const ui::Size mFlingerDisplaySize;
1570 const ui::Size mHardwareDisplaySize;
1571 const ui::Rotation mPhysicalOrientation;
1572 const sp<DisplayDevice> mDisplayDevice;
1573};
1574
1575struct DisplayDeviceSetProjectionTest_Installed0 : public DisplayDeviceSetProjectionTest {
1576 DisplayDeviceSetProjectionTest_Installed0()
1577 : DisplayDeviceSetProjectionTest(ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
1578 ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
1579 ui::ROTATION_0) {}
1580};
1581
1582TEST_F(DisplayDeviceSetProjectionTest_Installed0, checkWith0OutputRotation) {
1583 setProjectionForRotation0();
1584 expectStateForHardwareTransform0();
1585}
1586
1587TEST_F(DisplayDeviceSetProjectionTest_Installed0, checkWith90OutputRotation) {
1588 setProjectionForRotation90();
1589 expectStateForHardwareTransform90();
1590}
1591
1592TEST_F(DisplayDeviceSetProjectionTest_Installed0, checkWith180OutputRotation) {
1593 setProjectionForRotation180();
1594 expectStateForHardwareTransform180();
1595}
1596
1597TEST_F(DisplayDeviceSetProjectionTest_Installed0, checkWith270OutputRotation) {
1598 setProjectionForRotation270();
1599 expectStateForHardwareTransform270();
1600}
1601
1602struct DisplayDeviceSetProjectionTest_Installed90 : public DisplayDeviceSetProjectionTest {
1603 DisplayDeviceSetProjectionTest_Installed90()
1604 : DisplayDeviceSetProjectionTest(ui::Size(DEFAULT_DISPLAY_HEIGHT, DEFAULT_DISPLAY_WIDTH),
1605 ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
1606 ui::ROTATION_90) {}
1607};
1608
1609TEST_F(DisplayDeviceSetProjectionTest_Installed90, checkWith0OutputRotation) {
1610 setProjectionForRotation0();
1611 expectStateForHardwareTransform90();
1612}
1613
1614TEST_F(DisplayDeviceSetProjectionTest_Installed90, checkWith90OutputRotation) {
1615 setProjectionForRotation90();
1616 expectStateForHardwareTransform180();
1617}
1618
1619TEST_F(DisplayDeviceSetProjectionTest_Installed90, checkWith180OutputRotation) {
1620 setProjectionForRotation180();
1621 expectStateForHardwareTransform270();
1622}
1623
1624TEST_F(DisplayDeviceSetProjectionTest_Installed90, checkWith270OutputRotation) {
1625 setProjectionForRotation270();
1626 expectStateForHardwareTransform0();
1627}
1628
1629struct DisplayDeviceSetProjectionTest_Installed180 : public DisplayDeviceSetProjectionTest {
1630 DisplayDeviceSetProjectionTest_Installed180()
1631 : DisplayDeviceSetProjectionTest(ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
1632 ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
1633 ui::ROTATION_180) {}
1634};
1635
1636TEST_F(DisplayDeviceSetProjectionTest_Installed180, checkWith0OutputRotation) {
1637 setProjectionForRotation0();
1638 expectStateForHardwareTransform180();
1639}
1640
1641TEST_F(DisplayDeviceSetProjectionTest_Installed180, checkWith90OutputRotation) {
1642 setProjectionForRotation90();
1643 expectStateForHardwareTransform270();
1644}
1645
1646TEST_F(DisplayDeviceSetProjectionTest_Installed180, checkWith180OutputRotation) {
1647 setProjectionForRotation180();
1648 expectStateForHardwareTransform0();
1649}
1650
1651TEST_F(DisplayDeviceSetProjectionTest_Installed180, checkWith270OutputRotation) {
1652 setProjectionForRotation270();
1653 expectStateForHardwareTransform90();
1654}
1655
1656struct DisplayDeviceSetProjectionTest_Installed270 : public DisplayDeviceSetProjectionTest {
1657 DisplayDeviceSetProjectionTest_Installed270()
1658 : DisplayDeviceSetProjectionTest(ui::Size(DEFAULT_DISPLAY_HEIGHT, DEFAULT_DISPLAY_WIDTH),
1659 ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
1660 ui::ROTATION_270) {}
1661};
1662
1663TEST_F(DisplayDeviceSetProjectionTest_Installed270, checkWith0OutputRotation) {
1664 setProjectionForRotation0();
1665 expectStateForHardwareTransform270();
1666}
1667
1668TEST_F(DisplayDeviceSetProjectionTest_Installed270, checkWith90OutputRotation) {
1669 setProjectionForRotation90();
1670 expectStateForHardwareTransform0();
1671}
1672
1673TEST_F(DisplayDeviceSetProjectionTest_Installed270, checkWith180OutputRotation) {
1674 setProjectionForRotation180();
1675 expectStateForHardwareTransform90();
1676}
1677
1678TEST_F(DisplayDeviceSetProjectionTest_Installed270, checkWith270OutputRotation) {
1679 setProjectionForRotation270();
1680 expectStateForHardwareTransform180();
1681}
1682
1683/* ------------------------------------------------------------------------
Daniel Solomon42d04562019-01-20 21:03:19 -08001684 * SurfaceFlinger::getDisplayNativePrimaries
1685 */
1686
1687class GetDisplayNativePrimaries : public DisplayTransactionTest {
1688public:
1689 GetDisplayNativePrimaries();
1690 void populateDummyDisplayNativePrimaries(ui::DisplayPrimaries& primaries);
1691 void checkDummyDisplayNativePrimaries(const ui::DisplayPrimaries& primaries);
1692
1693private:
1694 static constexpr float mStartingTestValue = 1.0f;
1695};
1696
1697GetDisplayNativePrimaries::GetDisplayNativePrimaries() {
1698 SimplePrimaryDisplayCase::Display::injectHwcDisplay(this);
1699 injectFakeNativeWindowSurfaceFactory();
1700}
1701
1702void GetDisplayNativePrimaries::populateDummyDisplayNativePrimaries(
1703 ui::DisplayPrimaries& primaries) {
1704 float startingVal = mStartingTestValue;
1705 primaries.red.X = startingVal++;
1706 primaries.red.Y = startingVal++;
1707 primaries.red.Z = startingVal++;
1708 primaries.green.X = startingVal++;
1709 primaries.green.Y = startingVal++;
1710 primaries.green.Z = startingVal++;
1711 primaries.blue.X = startingVal++;
1712 primaries.blue.Y = startingVal++;
1713 primaries.blue.Z = startingVal++;
1714 primaries.white.X = startingVal++;
1715 primaries.white.Y = startingVal++;
1716 primaries.white.Z = startingVal++;
1717}
1718
1719void GetDisplayNativePrimaries::checkDummyDisplayNativePrimaries(
1720 const ui::DisplayPrimaries& primaries) {
1721 float startingVal = mStartingTestValue;
1722 EXPECT_EQ(primaries.red.X, startingVal++);
1723 EXPECT_EQ(primaries.red.Y, startingVal++);
1724 EXPECT_EQ(primaries.red.Z, startingVal++);
1725 EXPECT_EQ(primaries.green.X, startingVal++);
1726 EXPECT_EQ(primaries.green.Y, startingVal++);
1727 EXPECT_EQ(primaries.green.Z, startingVal++);
1728 EXPECT_EQ(primaries.blue.X, startingVal++);
1729 EXPECT_EQ(primaries.blue.Y, startingVal++);
1730 EXPECT_EQ(primaries.blue.Z, startingVal++);
1731 EXPECT_EQ(primaries.white.X, startingVal++);
1732 EXPECT_EQ(primaries.white.Y, startingVal++);
1733 EXPECT_EQ(primaries.white.Z, startingVal++);
1734}
1735
1736TEST_F(GetDisplayNativePrimaries, nullDisplayToken) {
1737 ui::DisplayPrimaries primaries;
1738 EXPECT_EQ(BAD_VALUE, mFlinger.getDisplayNativePrimaries(nullptr, primaries));
1739}
1740
Daniel Solomon42d04562019-01-20 21:03:19 -08001741TEST_F(GetDisplayNativePrimaries, internalDisplayWithPrimariesData) {
1742 auto injector = SimplePrimaryDisplayCase::Display::makeFakeExistingDisplayInjector(this);
1743 injector.inject();
1744 auto internalDisplayToken = injector.token();
1745
1746 ui::DisplayPrimaries expectedPrimaries;
1747 populateDummyDisplayNativePrimaries(expectedPrimaries);
1748 mFlinger.setInternalDisplayPrimaries(expectedPrimaries);
1749
1750 ui::DisplayPrimaries primaries;
1751 EXPECT_EQ(NO_ERROR, mFlinger.getDisplayNativePrimaries(internalDisplayToken, primaries));
1752
1753 checkDummyDisplayNativePrimaries(primaries);
1754}
1755
1756TEST_F(GetDisplayNativePrimaries, notInternalDisplayToken) {
1757 sp<BBinder> notInternalDisplayToken = new BBinder();
1758
1759 ui::DisplayPrimaries primaries;
1760 populateDummyDisplayNativePrimaries(primaries);
Dominik Laskowski470df5f2020-04-02 22:27:42 -07001761 EXPECT_EQ(NAME_NOT_FOUND,
1762 mFlinger.getDisplayNativePrimaries(notInternalDisplayToken, primaries));
Daniel Solomon42d04562019-01-20 21:03:19 -08001763
1764 // Check primaries argument wasn't modified in case of failure
1765 checkDummyDisplayNativePrimaries(primaries);
1766}
1767
1768/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001769 * SurfaceFlinger::setupNewDisplayDeviceInternal
1770 */
1771
1772class SetupNewDisplayDeviceInternalTest : public DisplayTransactionTest {
1773public:
1774 template <typename T>
1775 void setupNewDisplayDeviceInternalTest();
1776};
1777
1778template <typename Case>
1779void SetupNewDisplayDeviceInternalTest::setupNewDisplayDeviceInternalTest() {
1780 const sp<BBinder> displayToken = new BBinder();
Lloyd Pique542307f2018-10-19 13:24:08 -07001781 const sp<compositionengine::mock::DisplaySurface> displaySurface =
1782 new compositionengine::mock::DisplaySurface();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001783 const sp<mock::GraphicBufferProducer> producer = new mock::GraphicBufferProducer();
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001784
1785 // --------------------------------------------------------------------
1786 // Preconditions
1787
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001788 // Wide color displays support is configured appropriately
1789 Case::WideColorSupport::injectConfigChange(this);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001790
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001791 // The display is setup with the HWC.
1792 Case::Display::injectHwcDisplay(this);
1793
1794 // SurfaceFlinger will use a test-controlled factory for native window
1795 // surfaces.
1796 injectFakeNativeWindowSurfaceFactory();
1797
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -07001798 // A compositionengine::Display has already been created
1799 auto compositionDisplay = Case::Display::injectCompositionDisplay(this);
1800
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001801 // --------------------------------------------------------------------
1802 // Call Expectations
1803
1804 // Various native window calls will be made.
1805 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
Lloyd Pique3c085a02018-05-09 19:38:32 -07001806 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001807 Case::WideColorSupport::setupComposerCallExpectations(this);
1808 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001809 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001810
1811 // --------------------------------------------------------------------
1812 // Invocation
1813
1814 DisplayDeviceState state;
Dominik Laskowski55c85402020-01-21 16:25:47 -08001815 if (const auto connectionType = Case::Display::CONNECTION_TYPE::value) {
1816 const auto displayId = Case::Display::DISPLAY_ID::get();
1817 ASSERT_TRUE(displayId);
Marin Shalamanov4a42d432020-02-12 20:22:26 +01001818 const auto hwcDisplayId = Case::Display::HWC_DISPLAY_ID_OPT::value;
1819 ASSERT_TRUE(hwcDisplayId);
Marin Shalamanova524a092020-07-27 21:39:55 +02001820 state.physical = {.id = static_cast<PhysicalDisplayId>(*displayId),
1821 .type = *connectionType,
1822 .hwcDisplayId = *hwcDisplayId};
Dominik Laskowski55c85402020-01-21 16:25:47 -08001823 }
1824
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001825 state.isSecure = static_cast<bool>(Case::Display::SECURE);
1826
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -07001827 auto device = mFlinger.setupNewDisplayDeviceInternal(displayToken, compositionDisplay, state,
1828 displaySurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001829
1830 // --------------------------------------------------------------------
1831 // Postconditions
1832
1833 ASSERT_TRUE(device != nullptr);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001834 EXPECT_EQ(Case::Display::DISPLAY_ID::get(), device->getId());
Dominik Laskowski55c85402020-01-21 16:25:47 -08001835 EXPECT_EQ(Case::Display::CONNECTION_TYPE::value, device->getConnectionType());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001836 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), device->isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001837 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001838 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001839 EXPECT_EQ(Case::Display::WIDTH, device->getWidth());
1840 EXPECT_EQ(Case::Display::HEIGHT, device->getHeight());
1841 EXPECT_EQ(Case::WideColorSupport::WIDE_COLOR_SUPPORTED, device->hasWideColorGamut());
Valerie Haue9e843a2018-12-18 13:39:23 -08001842 EXPECT_EQ(Case::HdrSupport::HDR10_PLUS_SUPPORTED, device->hasHDR10PlusSupport());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001843 EXPECT_EQ(Case::HdrSupport::HDR10_SUPPORTED, device->hasHDR10Support());
1844 EXPECT_EQ(Case::HdrSupport::HDR_HLG_SUPPORTED, device->hasHLGSupport());
1845 EXPECT_EQ(Case::HdrSupport::HDR_DOLBY_VISION_SUPPORTED, device->hasDolbyVisionSupport());
Lloyd Pique3c085a02018-05-09 19:38:32 -07001846 // Note: This is not Case::Display::HWC_ACTIVE_CONFIG_ID as the ids are
1847 // remapped, and the test only ever sets up one config. If there were an error
1848 // looking up the remapped index, device->getActiveConfig() would be -1 instead.
Ady Abraham2139f732019-11-13 18:56:40 -08001849 EXPECT_EQ(0, device->getActiveConfig().value());
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001850 EXPECT_EQ(Case::PerFrameMetadataSupport::PER_FRAME_METADATA_KEYS,
1851 device->getSupportedPerFrameMetadata());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001852}
1853
1854TEST_F(SetupNewDisplayDeviceInternalTest, createSimplePrimaryDisplay) {
1855 setupNewDisplayDeviceInternalTest<SimplePrimaryDisplayCase>();
1856}
1857
1858TEST_F(SetupNewDisplayDeviceInternalTest, createSimpleExternalDisplay) {
1859 setupNewDisplayDeviceInternalTest<SimpleExternalDisplayCase>();
1860}
1861
1862TEST_F(SetupNewDisplayDeviceInternalTest, createNonHwcVirtualDisplay) {
1863 setupNewDisplayDeviceInternalTest<NonHwcVirtualDisplayCase>();
1864}
1865
1866TEST_F(SetupNewDisplayDeviceInternalTest, createHwcVirtualDisplay) {
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -07001867 setupNewDisplayDeviceInternalTest<HwcVirtualDisplayCase>();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001868}
1869
1870TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3Display) {
1871 setupNewDisplayDeviceInternalTest<WideColorP3ColorimetricDisplayCase>();
1872}
1873
Valerie Haue9e843a2018-12-18 13:39:23 -08001874TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10PlusDisplay) {
1875 setupNewDisplayDeviceInternalTest<Hdr10PlusDisplayCase>();
1876}
1877
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001878TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10Display) {
1879 setupNewDisplayDeviceInternalTest<Hdr10DisplayCase>();
1880}
1881
1882TEST_F(SetupNewDisplayDeviceInternalTest, createHdrHlgDisplay) {
1883 setupNewDisplayDeviceInternalTest<HdrHlgDisplayCase>();
1884}
1885
1886TEST_F(SetupNewDisplayDeviceInternalTest, createHdrDolbyVisionDisplay) {
1887 setupNewDisplayDeviceInternalTest<HdrDolbyVisionDisplayCase>();
1888}
1889
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001890TEST_F(SetupNewDisplayDeviceInternalTest, createHdrSmpte2086DisplayCase) {
1891 setupNewDisplayDeviceInternalTest<HdrSmpte2086DisplayCase>();
1892}
1893
1894TEST_F(SetupNewDisplayDeviceInternalTest, createHdrCta816_3_DisplayCase) {
1895 setupNewDisplayDeviceInternalTest<HdrCta861_3_DisplayCase>();
1896}
1897
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001898/* ------------------------------------------------------------------------
1899 * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded)
1900 */
1901
1902class HandleTransactionLockedTest : public DisplayTransactionTest {
1903public:
1904 template <typename Case>
1905 void setupCommonPreconditions();
1906
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001907 template <typename Case, bool connected>
1908 static void expectHotplugReceived(mock::EventThread*);
1909
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001910 template <typename Case>
1911 void setupCommonCallExpectationsForConnectProcessing();
1912
1913 template <typename Case>
1914 void setupCommonCallExpectationsForDisconnectProcessing();
1915
1916 template <typename Case>
1917 void processesHotplugConnectCommon();
1918
1919 template <typename Case>
1920 void ignoresHotplugConnectCommon();
1921
1922 template <typename Case>
1923 void processesHotplugDisconnectCommon();
1924
1925 template <typename Case>
1926 void verifyDisplayIsConnected(const sp<IBinder>& displayToken);
1927
1928 template <typename Case>
1929 void verifyPhysicalDisplayIsConnected();
1930
1931 void verifyDisplayIsNotConnected(const sp<IBinder>& displayToken);
1932};
1933
1934template <typename Case>
1935void HandleTransactionLockedTest::setupCommonPreconditions() {
1936 // Wide color displays support is configured appropriately
1937 Case::WideColorSupport::injectConfigChange(this);
1938
1939 // SurfaceFlinger will use a test-controlled factory for BufferQueues
1940 injectFakeBufferQueueFactory();
1941
1942 // SurfaceFlinger will use a test-controlled factory for native window
1943 // surfaces.
1944 injectFakeNativeWindowSurfaceFactory();
1945}
1946
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001947template <typename Case, bool connected>
1948void HandleTransactionLockedTest::expectHotplugReceived(mock::EventThread* eventThread) {
1949 const auto convert = [](auto physicalDisplayId) {
1950 return std::make_optional(DisplayId{physicalDisplayId});
1951 };
1952
1953 EXPECT_CALL(*eventThread,
1954 onHotplugReceived(ResultOf(convert, Case::Display::DISPLAY_ID::get()), connected))
1955 .Times(1);
1956}
1957
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001958template <typename Case>
1959void HandleTransactionLockedTest::setupCommonCallExpectationsForConnectProcessing() {
1960 Case::Display::setupHwcHotplugCallExpectations(this);
1961
1962 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1963 Case::Display::setupFramebufferProducerBufferQueueCallExpectations(this);
1964 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1965 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
1966
1967 Case::WideColorSupport::setupComposerCallExpectations(this);
1968 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001969 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001970
1971 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001972 expectHotplugReceived<Case, true>(mEventThread);
1973 expectHotplugReceived<Case, true>(mSFEventThread);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001974}
1975
1976template <typename Case>
1977void HandleTransactionLockedTest::setupCommonCallExpectationsForDisconnectProcessing() {
1978 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
Dominik Laskowski1eba0202019-01-24 09:14:40 -08001979
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001980 expectHotplugReceived<Case, false>(mEventThread);
1981 expectHotplugReceived<Case, false>(mSFEventThread);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001982}
1983
1984template <typename Case>
1985void HandleTransactionLockedTest::verifyDisplayIsConnected(const sp<IBinder>& displayToken) {
1986 // The display device should have been set up in the list of displays.
1987 ASSERT_TRUE(hasDisplayDevice(displayToken));
1988 const auto& device = getDisplayDevice(displayToken);
1989 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001990 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001991
Dominik Laskowski55c85402020-01-21 16:25:47 -08001992 std::optional<DisplayDeviceState::Physical> expectedPhysical;
1993 if (const auto connectionType = Case::Display::CONNECTION_TYPE::value) {
1994 const auto displayId = Case::Display::DISPLAY_ID::get();
1995 ASSERT_TRUE(displayId);
Marin Shalamanov4a42d432020-02-12 20:22:26 +01001996 const auto hwcDisplayId = Case::Display::HWC_DISPLAY_ID_OPT::value;
1997 ASSERT_TRUE(hwcDisplayId);
Marin Shalamanova524a092020-07-27 21:39:55 +02001998 expectedPhysical = {.id = static_cast<PhysicalDisplayId>(*displayId),
Marin Shalamanov7ce87642020-05-06 13:45:58 +02001999 .type = *connectionType,
2000 .hwcDisplayId = *hwcDisplayId};
Dominik Laskowski55c85402020-01-21 16:25:47 -08002001 }
2002
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002003 // The display should have been set up in the current display state
2004 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
2005 const auto& current = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07002006 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), current.isVirtual());
Dominik Laskowski55c85402020-01-21 16:25:47 -08002007 EXPECT_EQ(expectedPhysical, current.physical);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002008
2009 // The display should have been set up in the drawing display state
2010 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
2011 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07002012 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
Dominik Laskowski55c85402020-01-21 16:25:47 -08002013 EXPECT_EQ(expectedPhysical, draw.physical);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002014}
2015
2016template <typename Case>
2017void HandleTransactionLockedTest::verifyPhysicalDisplayIsConnected() {
2018 // HWComposer should have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07002019 EXPECT_TRUE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002020
Dominik Laskowski075d3172018-05-24 15:50:06 -07002021 // SF should have a display token.
2022 const auto displayId = Case::Display::DISPLAY_ID::get();
2023 ASSERT_TRUE(displayId);
2024 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
2025 auto& displayToken = mFlinger.mutablePhysicalDisplayTokens()[*displayId];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002026
2027 verifyDisplayIsConnected<Case>(displayToken);
2028}
2029
2030void HandleTransactionLockedTest::verifyDisplayIsNotConnected(const sp<IBinder>& displayToken) {
2031 EXPECT_FALSE(hasDisplayDevice(displayToken));
2032 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
2033 EXPECT_FALSE(hasDrawingDisplayState(displayToken));
2034}
2035
2036template <typename Case>
2037void HandleTransactionLockedTest::processesHotplugConnectCommon() {
2038 // --------------------------------------------------------------------
2039 // Preconditions
2040
2041 setupCommonPreconditions<Case>();
2042
2043 // A hotplug connect event is enqueued for a display
Peiyong Line9d809e2020-04-14 13:10:48 -07002044 Case::Display::injectPendingHotplugEvent(this, Connection::CONNECTED);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08002045
2046 // --------------------------------------------------------------------
2047 // Call Expectations
2048
2049 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillOnce(Return(false));
Lloyd Pique1fa4d462018-01-22 18:03:16 -08002050
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002051 setupCommonCallExpectationsForConnectProcessing<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08002052
Lloyd Pique1fa4d462018-01-22 18:03:16 -08002053 // --------------------------------------------------------------------
2054 // Invocation
Lloyd Piquee39cad22017-12-20 17:01:29 -08002055
Lloyd Pique1fa4d462018-01-22 18:03:16 -08002056 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
Lloyd Piquee39cad22017-12-20 17:01:29 -08002057
Lloyd Pique1fa4d462018-01-22 18:03:16 -08002058 // --------------------------------------------------------------------
2059 // Postconditions
2060
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002061 verifyPhysicalDisplayIsConnected<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08002062
Lloyd Pique1fa4d462018-01-22 18:03:16 -08002063 // --------------------------------------------------------------------
2064 // Cleanup conditions
2065
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002066 EXPECT_CALL(*mComposer,
2067 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
Lloyd Pique1fa4d462018-01-22 18:03:16 -08002068 .WillOnce(Return(Error::NONE));
2069 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Piquef58625d2017-12-19 13:22:33 -08002070}
2071
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002072template <typename Case>
2073void HandleTransactionLockedTest::ignoresHotplugConnectCommon() {
2074 // --------------------------------------------------------------------
2075 // Preconditions
2076
2077 setupCommonPreconditions<Case>();
2078
2079 // A hotplug connect event is enqueued for a display
Peiyong Line9d809e2020-04-14 13:10:48 -07002080 Case::Display::injectPendingHotplugEvent(this, Connection::CONNECTED);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002081
2082 // --------------------------------------------------------------------
2083 // Invocation
2084
2085 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2086
2087 // --------------------------------------------------------------------
2088 // Postconditions
2089
2090 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07002091 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002092}
2093
2094template <typename Case>
2095void HandleTransactionLockedTest::processesHotplugDisconnectCommon() {
2096 // --------------------------------------------------------------------
2097 // Preconditions
2098
2099 setupCommonPreconditions<Case>();
2100
2101 // A hotplug disconnect event is enqueued for a display
Peiyong Line9d809e2020-04-14 13:10:48 -07002102 Case::Display::injectPendingHotplugEvent(this, Connection::DISCONNECTED);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002103
2104 // The display is already completely set up.
2105 Case::Display::injectHwcDisplay(this);
2106 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
2107 existing.inject();
2108
2109 // --------------------------------------------------------------------
2110 // Call Expectations
2111
2112 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
Lloyd Pique438e9e72018-09-04 18:06:08 -07002113 EXPECT_CALL(*mComposer, getDisplayIdentificationData(Case::Display::HWC_DISPLAY_ID, _, _))
2114 .Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002115
2116 setupCommonCallExpectationsForDisconnectProcessing<Case>();
2117
2118 // --------------------------------------------------------------------
2119 // Invocation
2120
2121 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2122
2123 // --------------------------------------------------------------------
2124 // Postconditions
2125
2126 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07002127 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002128
Dominik Laskowski075d3172018-05-24 15:50:06 -07002129 // SF should not have a display token.
2130 const auto displayId = Case::Display::DISPLAY_ID::get();
2131 ASSERT_TRUE(displayId);
2132 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002133
2134 // The existing token should have been removed
2135 verifyDisplayIsNotConnected(existing.token());
2136}
2137
2138TEST_F(HandleTransactionLockedTest, processesHotplugConnectPrimaryDisplay) {
2139 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
2140}
2141
2142TEST_F(HandleTransactionLockedTest,
2143 processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected) {
2144 // Inject an external display.
2145 ExternalDisplayVariant::injectHwcDisplay(this);
2146
2147 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
2148}
2149
2150TEST_F(HandleTransactionLockedTest, processesHotplugConnectExternalDisplay) {
2151 // Inject a primary display.
2152 PrimaryDisplayVariant::injectHwcDisplay(this);
2153
2154 processesHotplugConnectCommon<SimpleExternalDisplayCase>();
2155}
2156
2157TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected) {
2158 // Inject both a primary and external display.
2159 PrimaryDisplayVariant::injectHwcDisplay(this);
2160 ExternalDisplayVariant::injectHwcDisplay(this);
2161
Lloyd Pique86fa3db2019-02-04 18:46:01 -08002162 // TODO: This is an unnecessary call.
2163 EXPECT_CALL(*mComposer,
2164 getDisplayIdentificationData(TertiaryDisplayVariant::HWC_DISPLAY_ID, _, _))
2165 .WillOnce(DoAll(SetArgPointee<1>(TertiaryDisplay::PORT),
2166 SetArgPointee<2>(TertiaryDisplay::GET_IDENTIFICATION_DATA()),
2167 Return(Error::NONE)));
2168
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002169 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
2170
2171 ignoresHotplugConnectCommon<SimpleTertiaryDisplayCase>();
2172}
2173
2174TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfExternalForVrComposer) {
2175 // Inject a primary display.
2176 PrimaryDisplayVariant::injectHwcDisplay(this);
2177
2178 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(true));
2179
2180 ignoresHotplugConnectCommon<SimpleExternalDisplayCase>();
2181}
2182
Marin Shalamanov4a42d432020-02-12 20:22:26 +01002183TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectPrimaryDisplay) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002184 processesHotplugDisconnectCommon<SimplePrimaryDisplayCase>();
2185}
2186
Marin Shalamanov4a42d432020-02-12 20:22:26 +01002187TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectExternalDisplay) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002188 processesHotplugDisconnectCommon<SimpleExternalDisplayCase>();
2189}
2190
2191TEST_F(HandleTransactionLockedTest, processesHotplugConnectThenDisconnectPrimary) {
2192 using Case = SimplePrimaryDisplayCase;
2193
2194 // --------------------------------------------------------------------
2195 // Preconditions
2196
2197 setupCommonPreconditions<Case>();
2198
2199 // A hotplug connect event is enqueued for a display
Peiyong Line9d809e2020-04-14 13:10:48 -07002200 Case::Display::injectPendingHotplugEvent(this, Connection::CONNECTED);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002201 // A hotplug disconnect event is also enqueued for the same display
Peiyong Line9d809e2020-04-14 13:10:48 -07002202 Case::Display::injectPendingHotplugEvent(this, Connection::DISCONNECTED);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002203
2204 // --------------------------------------------------------------------
2205 // Call Expectations
2206
2207 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
2208
2209 setupCommonCallExpectationsForConnectProcessing<Case>();
2210 setupCommonCallExpectationsForDisconnectProcessing<Case>();
2211
2212 EXPECT_CALL(*mComposer,
2213 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
2214 .WillOnce(Return(Error::NONE));
2215 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
2216
2217 // --------------------------------------------------------------------
2218 // Invocation
2219
2220 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2221
2222 // --------------------------------------------------------------------
2223 // Postconditions
2224
2225 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07002226 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002227
Dominik Laskowski075d3172018-05-24 15:50:06 -07002228 // SF should not have a display token.
2229 const auto displayId = Case::Display::DISPLAY_ID::get();
2230 ASSERT_TRUE(displayId);
2231 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002232}
2233
2234TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectThenConnectPrimary) {
2235 using Case = SimplePrimaryDisplayCase;
2236
2237 // --------------------------------------------------------------------
2238 // Preconditions
2239
2240 setupCommonPreconditions<Case>();
2241
2242 // The display is already completely set up.
2243 Case::Display::injectHwcDisplay(this);
2244 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
2245 existing.inject();
2246
2247 // A hotplug disconnect event is enqueued for a display
Peiyong Line9d809e2020-04-14 13:10:48 -07002248 Case::Display::injectPendingHotplugEvent(this, Connection::DISCONNECTED);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002249 // A hotplug connect event is also enqueued for the same display
Peiyong Line9d809e2020-04-14 13:10:48 -07002250 Case::Display::injectPendingHotplugEvent(this, Connection::CONNECTED);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002251
2252 // --------------------------------------------------------------------
2253 // Call Expectations
2254
2255 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
2256
2257 setupCommonCallExpectationsForConnectProcessing<Case>();
2258 setupCommonCallExpectationsForDisconnectProcessing<Case>();
2259
2260 // --------------------------------------------------------------------
2261 // Invocation
2262
2263 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2264
2265 // --------------------------------------------------------------------
2266 // Postconditions
2267
2268 // The existing token should have been removed
2269 verifyDisplayIsNotConnected(existing.token());
Dominik Laskowski075d3172018-05-24 15:50:06 -07002270 const auto displayId = Case::Display::DISPLAY_ID::get();
2271 ASSERT_TRUE(displayId);
2272 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
2273 EXPECT_NE(existing.token(), mFlinger.mutablePhysicalDisplayTokens()[*displayId]);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002274
2275 // A new display should be connected in its place
2276
2277 verifyPhysicalDisplayIsConnected<Case>();
2278
2279 // --------------------------------------------------------------------
2280 // Cleanup conditions
2281
2282 EXPECT_CALL(*mComposer,
2283 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
2284 .WillOnce(Return(Error::NONE));
2285 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
2286}
2287
2288TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAdded) {
2289 using Case = HwcVirtualDisplayCase;
2290
2291 // --------------------------------------------------------------------
2292 // Preconditions
2293
2294 // The HWC supports at least one virtual display
2295 injectMockComposer(1);
2296
2297 setupCommonPreconditions<Case>();
2298
2299 // A virtual display was added to the current state, and it has a
2300 // surface(producer)
2301 sp<BBinder> displayToken = new BBinder();
Lloyd Pique4c2ac022018-04-27 12:08:03 -07002302
Dominik Laskowski075d3172018-05-24 15:50:06 -07002303 DisplayDeviceState state;
2304 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002305
2306 sp<mock::GraphicBufferProducer> surface{new mock::GraphicBufferProducer()};
Dominik Laskowski075d3172018-05-24 15:50:06 -07002307 state.surface = surface;
2308 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002309
2310 // --------------------------------------------------------------------
2311 // Call Expectations
2312
2313 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
2314 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
2315
2316 EXPECT_CALL(*surface, query(NATIVE_WINDOW_WIDTH, _))
2317 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::WIDTH), Return(NO_ERROR)));
2318 EXPECT_CALL(*surface, query(NATIVE_WINDOW_HEIGHT, _))
2319 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::HEIGHT), Return(NO_ERROR)));
2320 EXPECT_CALL(*surface, query(NATIVE_WINDOW_FORMAT, _))
2321 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT),
2322 Return(NO_ERROR)));
2323 EXPECT_CALL(*surface, query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, _))
2324 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(NO_ERROR)));
2325
2326 EXPECT_CALL(*surface, setAsyncMode(true)).Times(1);
2327
Lloyd Pique86fa3db2019-02-04 18:46:01 -08002328 EXPECT_CALL(*mProducer, connect(_, NATIVE_WINDOW_API_EGL, false, _)).Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002329 EXPECT_CALL(*mProducer, disconnect(_, _)).Times(1);
2330
2331 Case::Display::setupHwcVirtualDisplayCreationCallExpectations(this);
2332 Case::WideColorSupport::setupComposerCallExpectations(this);
2333 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07002334 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002335
2336 // --------------------------------------------------------------------
2337 // Invocation
2338
2339 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2340
2341 // --------------------------------------------------------------------
2342 // Postconditions
2343
2344 // The display device should have been set up in the list of displays.
2345 verifyDisplayIsConnected<Case>(displayToken);
2346
2347 // --------------------------------------------------------------------
2348 // Cleanup conditions
2349
2350 EXPECT_CALL(*mComposer, destroyVirtualDisplay(Case::Display::HWC_DISPLAY_ID))
2351 .WillOnce(Return(Error::NONE));
2352 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Pique9e9800c2019-02-26 16:26:09 -08002353
2354 // Cleanup
2355 mFlinger.mutableCurrentState().displays.removeItem(displayToken);
2356 mFlinger.mutableDrawingState().displays.removeItem(displayToken);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002357}
2358
2359TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAddedWithNoSurface) {
2360 using Case = HwcVirtualDisplayCase;
2361
2362 // --------------------------------------------------------------------
2363 // Preconditions
2364
2365 // The HWC supports at least one virtual display
2366 injectMockComposer(1);
2367
2368 setupCommonPreconditions<Case>();
2369
2370 // A virtual display was added to the current state, but it does not have a
2371 // surface.
2372 sp<BBinder> displayToken = new BBinder();
2373
Dominik Laskowski075d3172018-05-24 15:50:06 -07002374 DisplayDeviceState state;
2375 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002376
Dominik Laskowski075d3172018-05-24 15:50:06 -07002377 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002378
2379 // --------------------------------------------------------------------
2380 // Call Expectations
2381
2382 // --------------------------------------------------------------------
2383 // Invocation
2384
2385 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2386
2387 // --------------------------------------------------------------------
2388 // Postconditions
2389
2390 // There will not be a display device set up.
2391 EXPECT_FALSE(hasDisplayDevice(displayToken));
2392
2393 // The drawing display state will be set from the current display state.
2394 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
2395 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07002396 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002397}
2398
2399TEST_F(HandleTransactionLockedTest, processesVirtualDisplayRemoval) {
2400 using Case = HwcVirtualDisplayCase;
2401
2402 // --------------------------------------------------------------------
2403 // Preconditions
2404
2405 // A virtual display is set up but is removed from the current state.
Dominik Laskowski075d3172018-05-24 15:50:06 -07002406 const auto displayId = Case::Display::DISPLAY_ID::get();
2407 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08002408 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002409 Case::Display::injectHwcDisplay(this);
2410 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
2411 existing.inject();
2412 mFlinger.mutableCurrentState().displays.removeItem(existing.token());
2413
2414 // --------------------------------------------------------------------
2415 // Call Expectations
2416
2417 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
2418
2419 // --------------------------------------------------------------------
2420 // Invocation
2421
2422 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2423
2424 // --------------------------------------------------------------------
2425 // Postconditions
2426
2427 // The existing token should have been removed
2428 verifyDisplayIsNotConnected(existing.token());
2429}
2430
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002431TEST_F(HandleTransactionLockedTest, processesDisplayLayerStackChanges) {
2432 using Case = NonHwcVirtualDisplayCase;
2433
2434 constexpr uint32_t oldLayerStack = 0u;
2435 constexpr uint32_t newLayerStack = 123u;
2436
2437 // --------------------------------------------------------------------
2438 // Preconditions
2439
2440 // A display is set up
2441 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2442 display.inject();
2443
2444 // There is a change to the layerStack state
2445 display.mutableDrawingDisplayState().layerStack = oldLayerStack;
2446 display.mutableCurrentDisplayState().layerStack = newLayerStack;
2447
2448 // --------------------------------------------------------------------
2449 // Invocation
2450
2451 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2452
2453 // --------------------------------------------------------------------
2454 // Postconditions
2455
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002456 EXPECT_EQ(newLayerStack, display.mutableDisplayDevice()->getLayerStack());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002457}
2458
2459TEST_F(HandleTransactionLockedTest, processesDisplayTransformChanges) {
2460 using Case = NonHwcVirtualDisplayCase;
2461
Dominik Laskowski718f9602019-11-09 20:01:35 -08002462 constexpr ui::Rotation oldTransform = ui::ROTATION_0;
2463 constexpr ui::Rotation newTransform = ui::ROTATION_180;
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002464
2465 // --------------------------------------------------------------------
2466 // Preconditions
2467
2468 // A display is set up
2469 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2470 display.inject();
2471
2472 // There is a change to the orientation state
2473 display.mutableDrawingDisplayState().orientation = oldTransform;
2474 display.mutableCurrentDisplayState().orientation = newTransform;
2475
2476 // --------------------------------------------------------------------
2477 // Invocation
2478
2479 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2480
2481 // --------------------------------------------------------------------
2482 // Postconditions
2483
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002484 EXPECT_EQ(newTransform, display.mutableDisplayDevice()->getOrientation());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002485}
2486
2487TEST_F(HandleTransactionLockedTest, processesDisplayViewportChanges) {
2488 using Case = NonHwcVirtualDisplayCase;
2489
2490 const Rect oldViewport(0, 0, 0, 0);
2491 const Rect newViewport(0, 0, 123, 456);
2492
2493 // --------------------------------------------------------------------
2494 // Preconditions
2495
2496 // A display is set up
2497 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2498 display.inject();
2499
2500 // There is a change to the viewport state
2501 display.mutableDrawingDisplayState().viewport = oldViewport;
2502 display.mutableCurrentDisplayState().viewport = newViewport;
2503
2504 // --------------------------------------------------------------------
2505 // Invocation
2506
2507 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2508
2509 // --------------------------------------------------------------------
2510 // Postconditions
2511
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002512 EXPECT_EQ(newViewport, display.mutableDisplayDevice()->getViewport());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002513}
2514
2515TEST_F(HandleTransactionLockedTest, processesDisplayFrameChanges) {
2516 using Case = NonHwcVirtualDisplayCase;
2517
2518 const Rect oldFrame(0, 0, 0, 0);
2519 const Rect newFrame(0, 0, 123, 456);
2520
2521 // --------------------------------------------------------------------
2522 // Preconditions
2523
2524 // A display is set up
2525 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2526 display.inject();
2527
2528 // There is a change to the viewport state
2529 display.mutableDrawingDisplayState().frame = oldFrame;
2530 display.mutableCurrentDisplayState().frame = newFrame;
2531
2532 // --------------------------------------------------------------------
2533 // Invocation
2534
2535 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2536
2537 // --------------------------------------------------------------------
2538 // Postconditions
2539
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002540 EXPECT_EQ(newFrame, display.mutableDisplayDevice()->getFrame());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002541}
2542
2543TEST_F(HandleTransactionLockedTest, processesDisplayWidthChanges) {
2544 using Case = NonHwcVirtualDisplayCase;
2545
2546 constexpr int oldWidth = 0;
2547 constexpr int oldHeight = 10;
2548 constexpr int newWidth = 123;
2549
2550 // --------------------------------------------------------------------
2551 // Preconditions
2552
2553 // A display is set up
2554 auto nativeWindow = new mock::NativeWindow();
Lloyd Pique542307f2018-10-19 13:24:08 -07002555 auto displaySurface = new compositionengine::mock::DisplaySurface();
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002556 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002557 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2558 display.setNativeWindow(nativeWindow);
2559 display.setDisplaySurface(displaySurface);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002560 // Setup injection expections
2561 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
2562 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
2563 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
2564 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
Lloyd Pique86fa3db2019-02-04 18:46:01 -08002565 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT)).Times(1);
2566 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_API_CONNECT)).Times(1);
2567 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_SET_USAGE64)).Times(1);
chaviw8beb4142019-04-11 13:09:05 -07002568 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002569 display.inject();
2570
2571 // There is a change to the viewport state
2572 display.mutableDrawingDisplayState().width = oldWidth;
2573 display.mutableDrawingDisplayState().height = oldHeight;
2574 display.mutableCurrentDisplayState().width = newWidth;
2575 display.mutableCurrentDisplayState().height = oldHeight;
2576
2577 // --------------------------------------------------------------------
2578 // Call Expectations
2579
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002580 EXPECT_CALL(*displaySurface, resizeBuffers(newWidth, oldHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002581
2582 // --------------------------------------------------------------------
2583 // Invocation
2584
2585 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2586}
2587
2588TEST_F(HandleTransactionLockedTest, processesDisplayHeightChanges) {
2589 using Case = NonHwcVirtualDisplayCase;
2590
2591 constexpr int oldWidth = 0;
2592 constexpr int oldHeight = 10;
2593 constexpr int newHeight = 123;
2594
2595 // --------------------------------------------------------------------
2596 // Preconditions
2597
2598 // A display is set up
2599 auto nativeWindow = new mock::NativeWindow();
Lloyd Pique542307f2018-10-19 13:24:08 -07002600 auto displaySurface = new compositionengine::mock::DisplaySurface();
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002601 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002602 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2603 display.setNativeWindow(nativeWindow);
2604 display.setDisplaySurface(displaySurface);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002605 // Setup injection expections
2606 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
2607 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
2608 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
2609 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
Lloyd Pique86fa3db2019-02-04 18:46:01 -08002610 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT)).Times(1);
2611 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_API_CONNECT)).Times(1);
2612 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_SET_USAGE64)).Times(1);
chaviw8beb4142019-04-11 13:09:05 -07002613 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002614 display.inject();
2615
2616 // There is a change to the viewport state
2617 display.mutableDrawingDisplayState().width = oldWidth;
2618 display.mutableDrawingDisplayState().height = oldHeight;
2619 display.mutableCurrentDisplayState().width = oldWidth;
2620 display.mutableCurrentDisplayState().height = newHeight;
2621
2622 // --------------------------------------------------------------------
2623 // Call Expectations
2624
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002625 EXPECT_CALL(*displaySurface, resizeBuffers(oldWidth, newHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002626
2627 // --------------------------------------------------------------------
2628 // Invocation
2629
2630 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2631}
2632
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002633/* ------------------------------------------------------------------------
2634 * SurfaceFlinger::setDisplayStateLocked
2635 */
2636
2637TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithUnknownDisplay) {
2638 // --------------------------------------------------------------------
2639 // Preconditions
2640
2641 // We have an unknown display token not associated with a known display
2642 sp<BBinder> displayToken = new BBinder();
2643
2644 // The requested display state references the unknown display.
2645 DisplayState state;
2646 state.what = DisplayState::eLayerStackChanged;
2647 state.token = displayToken;
2648 state.layerStack = 456;
2649
2650 // --------------------------------------------------------------------
2651 // Invocation
2652
2653 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2654
2655 // --------------------------------------------------------------------
2656 // Postconditions
2657
2658 // The returned flags are empty
2659 EXPECT_EQ(0u, flags);
2660
2661 // The display token still doesn't match anything known.
2662 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
2663}
2664
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002665TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWhenNoChanges) {
2666 using Case = SimplePrimaryDisplayCase;
2667
2668 // --------------------------------------------------------------------
2669 // Preconditions
2670
2671 // A display is already set up
2672 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2673 display.inject();
2674
2675 // No changes are made to the display
2676 DisplayState state;
2677 state.what = 0;
2678 state.token = display.token();
2679
2680 // --------------------------------------------------------------------
2681 // Invocation
2682
2683 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2684
2685 // --------------------------------------------------------------------
2686 // Postconditions
2687
2688 // The returned flags are empty
2689 EXPECT_EQ(0u, flags);
2690}
2691
2692TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSurfaceDidNotChange) {
2693 using Case = SimplePrimaryDisplayCase;
2694
2695 // --------------------------------------------------------------------
2696 // Preconditions
2697
2698 // A display is already set up
2699 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2700 display.inject();
2701
2702 // There is a surface that can be set.
2703 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2704
2705 // The current display state has the surface set
2706 display.mutableCurrentDisplayState().surface = surface;
2707
2708 // The incoming request sets the same surface
2709 DisplayState state;
2710 state.what = DisplayState::eSurfaceChanged;
2711 state.token = display.token();
2712 state.surface = surface;
2713
2714 // --------------------------------------------------------------------
2715 // Invocation
2716
2717 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2718
2719 // --------------------------------------------------------------------
2720 // Postconditions
2721
2722 // The returned flags are empty
2723 EXPECT_EQ(0u, flags);
2724
2725 // The current display state is unchanged.
2726 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2727}
2728
2729TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfSurfaceChanged) {
2730 using Case = SimplePrimaryDisplayCase;
2731
2732 // --------------------------------------------------------------------
2733 // Preconditions
2734
2735 // A display is already set up
2736 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2737 display.inject();
2738
2739 // There is a surface that can be set.
2740 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2741
2742 // The current display state does not have a surface
2743 display.mutableCurrentDisplayState().surface = nullptr;
2744
2745 // The incoming request sets a surface
2746 DisplayState state;
2747 state.what = DisplayState::eSurfaceChanged;
2748 state.token = display.token();
2749 state.surface = surface;
2750
2751 // --------------------------------------------------------------------
2752 // Invocation
2753
2754 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2755
2756 // --------------------------------------------------------------------
2757 // Postconditions
2758
2759 // The returned flags indicate a transaction is needed
2760 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2761
2762 // The current display layer stack state is set to the new value
2763 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2764}
2765
2766TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfLayerStackDidNotChange) {
2767 using Case = SimplePrimaryDisplayCase;
2768
2769 // --------------------------------------------------------------------
2770 // Preconditions
2771
2772 // A display is already set up
2773 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2774 display.inject();
2775
2776 // The display has a layer stack set
2777 display.mutableCurrentDisplayState().layerStack = 456u;
2778
2779 // The incoming request sets the same layer stack
2780 DisplayState state;
2781 state.what = DisplayState::eLayerStackChanged;
2782 state.token = display.token();
2783 state.layerStack = 456u;
2784
2785 // --------------------------------------------------------------------
2786 // Invocation
2787
2788 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2789
2790 // --------------------------------------------------------------------
2791 // Postconditions
2792
2793 // The returned flags are empty
2794 EXPECT_EQ(0u, flags);
2795
2796 // The current display state is unchanged
2797 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2798}
2799
2800TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfLayerStackChanged) {
2801 using Case = SimplePrimaryDisplayCase;
2802
2803 // --------------------------------------------------------------------
2804 // Preconditions
2805
2806 // A display is set up
2807 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2808 display.inject();
2809
2810 // The display has a layer stack set
2811 display.mutableCurrentDisplayState().layerStack = 654u;
2812
2813 // The incoming request sets a different layer stack
2814 DisplayState state;
2815 state.what = DisplayState::eLayerStackChanged;
2816 state.token = display.token();
2817 state.layerStack = 456u;
2818
2819 // --------------------------------------------------------------------
2820 // Invocation
2821
2822 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2823
2824 // --------------------------------------------------------------------
2825 // Postconditions
2826
2827 // The returned flags indicate a transaction is needed
2828 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2829
2830 // The desired display state has been set to the new value.
2831 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2832}
2833
2834TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfProjectionDidNotChange) {
2835 using Case = SimplePrimaryDisplayCase;
Dominik Laskowski718f9602019-11-09 20:01:35 -08002836 constexpr ui::Rotation initialOrientation = ui::ROTATION_180;
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002837 const Rect initialFrame = {1, 2, 3, 4};
2838 const Rect initialViewport = {5, 6, 7, 8};
2839
2840 // --------------------------------------------------------------------
2841 // Preconditions
2842
2843 // A display is set up
2844 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2845 display.inject();
2846
2847 // The current display state projection state is all set
2848 display.mutableCurrentDisplayState().orientation = initialOrientation;
2849 display.mutableCurrentDisplayState().frame = initialFrame;
2850 display.mutableCurrentDisplayState().viewport = initialViewport;
2851
2852 // The incoming request sets the same projection state
2853 DisplayState state;
2854 state.what = DisplayState::eDisplayProjectionChanged;
2855 state.token = display.token();
2856 state.orientation = initialOrientation;
2857 state.frame = initialFrame;
2858 state.viewport = initialViewport;
2859
2860 // --------------------------------------------------------------------
2861 // Invocation
2862
2863 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2864
2865 // --------------------------------------------------------------------
2866 // Postconditions
2867
2868 // The returned flags are empty
2869 EXPECT_EQ(0u, flags);
2870
2871 // The current display state is unchanged
2872 EXPECT_EQ(initialOrientation, display.getCurrentDisplayState().orientation);
2873
2874 EXPECT_EQ(initialFrame, display.getCurrentDisplayState().frame);
2875 EXPECT_EQ(initialViewport, display.getCurrentDisplayState().viewport);
2876}
2877
2878TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfOrientationChanged) {
2879 using Case = SimplePrimaryDisplayCase;
Dominik Laskowski718f9602019-11-09 20:01:35 -08002880 constexpr ui::Rotation initialOrientation = ui::ROTATION_90;
2881 constexpr ui::Rotation desiredOrientation = ui::ROTATION_180;
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002882
2883 // --------------------------------------------------------------------
2884 // Preconditions
2885
2886 // A display is set up
2887 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2888 display.inject();
2889
2890 // The current display state has an orientation set
2891 display.mutableCurrentDisplayState().orientation = initialOrientation;
2892
2893 // The incoming request sets a different orientation
2894 DisplayState state;
2895 state.what = DisplayState::eDisplayProjectionChanged;
2896 state.token = display.token();
2897 state.orientation = desiredOrientation;
2898
2899 // --------------------------------------------------------------------
2900 // Invocation
2901
2902 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2903
2904 // --------------------------------------------------------------------
2905 // Postconditions
2906
2907 // The returned flags indicate a transaction is needed
2908 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2909
2910 // The current display state has the new value.
2911 EXPECT_EQ(desiredOrientation, display.getCurrentDisplayState().orientation);
2912}
2913
2914TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfFrameChanged) {
2915 using Case = SimplePrimaryDisplayCase;
2916 const Rect initialFrame = {0, 0, 0, 0};
2917 const Rect desiredFrame = {5, 6, 7, 8};
2918
2919 // --------------------------------------------------------------------
2920 // Preconditions
2921
2922 // A display is set up
2923 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2924 display.inject();
2925
2926 // The current display state does not have a frame
2927 display.mutableCurrentDisplayState().frame = initialFrame;
2928
2929 // The incoming request sets a frame
2930 DisplayState state;
2931 state.what = DisplayState::eDisplayProjectionChanged;
2932 state.token = display.token();
2933 state.frame = desiredFrame;
2934
2935 // --------------------------------------------------------------------
2936 // Invocation
2937
2938 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2939
2940 // --------------------------------------------------------------------
2941 // Postconditions
2942
2943 // The returned flags indicate a transaction is needed
2944 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2945
2946 // The current display state has the new value.
2947 EXPECT_EQ(desiredFrame, display.getCurrentDisplayState().frame);
2948}
2949
2950TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfViewportChanged) {
2951 using Case = SimplePrimaryDisplayCase;
2952 const Rect initialViewport = {0, 0, 0, 0};
2953 const Rect desiredViewport = {5, 6, 7, 8};
2954
2955 // --------------------------------------------------------------------
2956 // Preconditions
2957
2958 // A display is set up
2959 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2960 display.inject();
2961
2962 // The current display state does not have a viewport
2963 display.mutableCurrentDisplayState().viewport = initialViewport;
2964
2965 // The incoming request sets a viewport
2966 DisplayState state;
2967 state.what = DisplayState::eDisplayProjectionChanged;
2968 state.token = display.token();
2969 state.viewport = desiredViewport;
2970
2971 // --------------------------------------------------------------------
2972 // Invocation
2973
2974 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2975
2976 // --------------------------------------------------------------------
2977 // Postconditions
2978
2979 // The returned flags indicate a transaction is needed
2980 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2981
2982 // The current display state has the new value.
2983 EXPECT_EQ(desiredViewport, display.getCurrentDisplayState().viewport);
2984}
2985
2986TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSizeDidNotChange) {
2987 using Case = SimplePrimaryDisplayCase;
2988 constexpr uint32_t initialWidth = 1024;
2989 constexpr uint32_t initialHeight = 768;
2990
2991 // --------------------------------------------------------------------
2992 // Preconditions
2993
2994 // A display is set up
2995 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2996 display.inject();
2997
2998 // The current display state has a size set
2999 display.mutableCurrentDisplayState().width = initialWidth;
3000 display.mutableCurrentDisplayState().height = initialHeight;
3001
3002 // The incoming request sets the same display size
3003 DisplayState state;
3004 state.what = DisplayState::eDisplaySizeChanged;
3005 state.token = display.token();
3006 state.width = initialWidth;
3007 state.height = initialHeight;
3008
3009 // --------------------------------------------------------------------
3010 // Invocation
3011
3012 uint32_t flags = mFlinger.setDisplayStateLocked(state);
3013
3014 // --------------------------------------------------------------------
3015 // Postconditions
3016
3017 // The returned flags are empty
3018 EXPECT_EQ(0u, flags);
3019
3020 // The current display state is unchanged
3021 EXPECT_EQ(initialWidth, display.getCurrentDisplayState().width);
3022 EXPECT_EQ(initialHeight, display.getCurrentDisplayState().height);
3023}
3024
3025TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfWidthChanged) {
3026 using Case = SimplePrimaryDisplayCase;
3027 constexpr uint32_t initialWidth = 0;
3028 constexpr uint32_t desiredWidth = 1024;
3029
3030 // --------------------------------------------------------------------
3031 // Preconditions
3032
3033 // A display is set up
3034 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
3035 display.inject();
3036
3037 // The display does not yet have a width
3038 display.mutableCurrentDisplayState().width = initialWidth;
3039
3040 // The incoming request sets a display width
3041 DisplayState state;
3042 state.what = DisplayState::eDisplaySizeChanged;
3043 state.token = display.token();
3044 state.width = desiredWidth;
3045
3046 // --------------------------------------------------------------------
3047 // Invocation
3048
3049 uint32_t flags = mFlinger.setDisplayStateLocked(state);
3050
3051 // --------------------------------------------------------------------
3052 // Postconditions
3053
3054 // The returned flags indicate a transaction is needed
3055 EXPECT_EQ(eDisplayTransactionNeeded, flags);
3056
3057 // The current display state has the new value.
3058 EXPECT_EQ(desiredWidth, display.getCurrentDisplayState().width);
3059}
3060
3061TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfHeightChanged) {
3062 using Case = SimplePrimaryDisplayCase;
3063 constexpr uint32_t initialHeight = 0;
3064 constexpr uint32_t desiredHeight = 768;
3065
3066 // --------------------------------------------------------------------
3067 // Preconditions
3068
3069 // A display is set up
3070 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
3071 display.inject();
3072
3073 // The display does not yet have a height
3074 display.mutableCurrentDisplayState().height = initialHeight;
3075
3076 // The incoming request sets a display height
3077 DisplayState state;
3078 state.what = DisplayState::eDisplaySizeChanged;
3079 state.token = display.token();
3080 state.height = desiredHeight;
3081
3082 // --------------------------------------------------------------------
3083 // Invocation
3084
3085 uint32_t flags = mFlinger.setDisplayStateLocked(state);
3086
3087 // --------------------------------------------------------------------
3088 // Postconditions
3089
3090 // The returned flags indicate a transaction is needed
3091 EXPECT_EQ(eDisplayTransactionNeeded, flags);
3092
3093 // The current display state has the new value.
3094 EXPECT_EQ(desiredHeight, display.getCurrentDisplayState().height);
3095}
3096
Lloyd Pique86016da2018-03-01 16:09:38 -08003097/* ------------------------------------------------------------------------
3098 * SurfaceFlinger::onInitializeDisplays
3099 */
3100
3101TEST_F(DisplayTransactionTest, onInitializeDisplaysSetsUpPrimaryDisplay) {
3102 using Case = SimplePrimaryDisplayCase;
3103
3104 // --------------------------------------------------------------------
3105 // Preconditions
3106
3107 // A primary display is set up
3108 Case::Display::injectHwcDisplay(this);
3109 auto primaryDisplay = Case::Display::makeFakeExistingDisplayInjector(this);
3110 primaryDisplay.inject();
3111
3112 // --------------------------------------------------------------------
3113 // Call Expectations
3114
3115 // We expect the surface interceptor to possibly be used, but we treat it as
3116 // disabled since it is called as a side effect rather than directly by this
3117 // function.
3118 EXPECT_CALL(*mSurfaceInterceptor, isEnabled()).WillOnce(Return(false));
3119
3120 // We expect a call to get the active display config.
3121 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
3122
3123 // We expect invalidate() to be invoked once to trigger display transaction
3124 // processing.
3125 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
3126
Ady Abraham0ed31c92020-04-16 11:48:45 -07003127 EXPECT_CALL(*mPrimaryDispSync, expectedPresentTime(_)).WillRepeatedly(Return(0));
Lloyd Pique86fa3db2019-02-04 18:46:01 -08003128
Lloyd Pique86016da2018-03-01 16:09:38 -08003129 // --------------------------------------------------------------------
3130 // Invocation
3131
3132 mFlinger.onInitializeDisplays();
3133
3134 // --------------------------------------------------------------------
3135 // Postconditions
3136
3137 // The primary display should have a current state
3138 ASSERT_TRUE(hasCurrentDisplayState(primaryDisplay.token()));
3139 const auto& primaryDisplayState = getCurrentDisplayState(primaryDisplay.token());
3140 // The layer stack state should be set to zero
3141 EXPECT_EQ(0u, primaryDisplayState.layerStack);
3142 // The orientation state should be set to zero
Dominik Laskowski718f9602019-11-09 20:01:35 -08003143 EXPECT_EQ(ui::ROTATION_0, primaryDisplayState.orientation);
Lloyd Pique86016da2018-03-01 16:09:38 -08003144
3145 // The frame state should be set to INVALID
3146 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.frame);
3147
3148 // The viewport state should be set to INVALID
3149 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.viewport);
3150
3151 // The width and height should both be zero
3152 EXPECT_EQ(0u, primaryDisplayState.width);
3153 EXPECT_EQ(0u, primaryDisplayState.height);
3154
Peiyong Lin65248e02020-04-18 21:15:07 -07003155 // The display should be set to PowerMode::ON
Lloyd Pique86016da2018-03-01 16:09:38 -08003156 ASSERT_TRUE(hasDisplayDevice(primaryDisplay.token()));
3157 auto displayDevice = primaryDisplay.mutableDisplayDevice();
Peiyong Lin65248e02020-04-18 21:15:07 -07003158 EXPECT_EQ(PowerMode::ON, displayDevice->getPowerMode());
Lloyd Pique86016da2018-03-01 16:09:38 -08003159
3160 // The display refresh period should be set in the frame tracker.
3161 FrameStats stats;
3162 mFlinger.getAnimFrameTracker().getStats(&stats);
3163 EXPECT_EQ(DEFAULT_REFRESH_RATE, stats.refreshPeriodNano);
3164
3165 // The display transaction needed flag should be set.
3166 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
3167
3168 // The compositor timing should be set to default values
3169 const auto& compositorTiming = mFlinger.getCompositorTiming();
3170 EXPECT_EQ(-DEFAULT_REFRESH_RATE, compositorTiming.deadline);
3171 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.interval);
3172 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.presentLatency);
3173}
3174
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003175/* ------------------------------------------------------------------------
3176 * SurfaceFlinger::setPowerModeInternal
3177 */
3178
3179// Used when we simulate a display that supports doze.
Peiyong Lined531a32018-10-26 18:27:56 -07003180template <typename Display>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003181struct DozeIsSupportedVariant {
3182 static constexpr bool DOZE_SUPPORTED = true;
3183 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
3184 IComposerClient::PowerMode::DOZE;
3185 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
3186 IComposerClient::PowerMode::DOZE_SUSPEND;
Peiyong Lined531a32018-10-26 18:27:56 -07003187
3188 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
3189 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(Display::HWC_DISPLAY_ID, _))
Peiyong Line9d809e2020-04-14 13:10:48 -07003190 .WillOnce(DoAll(SetArgPointee<1>(
3191 std::vector<DisplayCapability>({DisplayCapability::DOZE})),
Peiyong Lined531a32018-10-26 18:27:56 -07003192 Return(Error::NONE)));
3193 }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003194};
3195
Peiyong Lined531a32018-10-26 18:27:56 -07003196template <typename Display>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003197// Used when we simulate a display that does not support doze.
3198struct DozeNotSupportedVariant {
3199 static constexpr bool DOZE_SUPPORTED = false;
3200 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
3201 IComposerClient::PowerMode::ON;
3202 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
3203 IComposerClient::PowerMode::ON;
Peiyong Lined531a32018-10-26 18:27:56 -07003204
3205 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
3206 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(Display::HWC_DISPLAY_ID, _))
Peiyong Line9d809e2020-04-14 13:10:48 -07003207 .WillOnce(DoAll(SetArgPointee<1>(std::vector<DisplayCapability>({})),
Peiyong Lined531a32018-10-26 18:27:56 -07003208 Return(Error::NONE)));
3209 }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003210};
3211
3212struct EventThreadBaseSupportedVariant {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -07003213 static void setupVsyncAndEventThreadNoCallExpectations(DisplayTransactionTest* test) {
3214 // The callback should not be notified to toggle VSYNC.
3215 EXPECT_CALL(test->mSchedulerCallback, setVsyncEnabled(_)).Times(0);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003216
3217 // The event thread should not be notified.
3218 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(0);
3219 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(0);
3220 }
3221};
3222
3223struct EventThreadNotSupportedVariant : public EventThreadBaseSupportedVariant {
3224 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
3225 // These calls are only expected for the primary display.
3226
3227 // Instead expect no calls.
Dominik Laskowski8b01cc02020-07-14 19:02:41 -07003228 setupVsyncAndEventThreadNoCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003229 }
3230
3231 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
3232 // These calls are only expected for the primary display.
3233
3234 // Instead expect no calls.
Dominik Laskowski8b01cc02020-07-14 19:02:41 -07003235 setupVsyncAndEventThreadNoCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003236 }
3237};
3238
3239struct EventThreadIsSupportedVariant : public EventThreadBaseSupportedVariant {
3240 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -07003241 // The callback should be notified to enable VSYNC.
3242 EXPECT_CALL(test->mSchedulerCallback, setVsyncEnabled(true)).Times(1);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003243
3244 // The event thread should be notified that the screen was acquired.
3245 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(1);
3246 }
3247
3248 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -07003249 // The callback should be notified to disable VSYNC.
3250 EXPECT_CALL(test->mSchedulerCallback, setVsyncEnabled(false)).Times(1);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003251
3252 // The event thread should not be notified that the screen was released.
3253 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(1);
3254 }
3255};
3256
Lloyd Pique41be5d22018-06-21 13:11:48 -07003257struct DispSyncIsSupportedVariant {
3258 static void setupBeginResyncCallExpectations(DisplayTransactionTest* test) {
Lloyd Pique41be5d22018-06-21 13:11:48 -07003259 EXPECT_CALL(*test->mPrimaryDispSync, setPeriod(DEFAULT_REFRESH_RATE)).Times(1);
3260 EXPECT_CALL(*test->mPrimaryDispSync, beginResync()).Times(1);
3261 }
3262
3263 static void setupEndResyncCallExpectations(DisplayTransactionTest* test) {
3264 EXPECT_CALL(*test->mPrimaryDispSync, endResync()).Times(1);
3265 }
3266};
3267
3268struct DispSyncNotSupportedVariant {
3269 static void setupBeginResyncCallExpectations(DisplayTransactionTest* /* test */) {}
3270
3271 static void setupEndResyncCallExpectations(DisplayTransactionTest* /* test */) {}
3272};
3273
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003274// --------------------------------------------------------------------
3275// Note:
3276//
3277// There are a large number of transitions we could test, however we only test a
3278// selected subset which provides complete test coverage of the implementation.
3279// --------------------------------------------------------------------
3280
Peiyong Lin65248e02020-04-18 21:15:07 -07003281template <PowerMode initialPowerMode, PowerMode targetPowerMode>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003282struct TransitionVariantCommon {
3283 static constexpr auto INITIAL_POWER_MODE = initialPowerMode;
3284 static constexpr auto TARGET_POWER_MODE = targetPowerMode;
3285
3286 static void verifyPostconditions(DisplayTransactionTest*) {}
3287};
3288
Peiyong Lin65248e02020-04-18 21:15:07 -07003289struct TransitionOffToOnVariant : public TransitionVariantCommon<PowerMode::OFF, PowerMode::ON> {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003290 template <typename Case>
3291 static void setupCallExpectations(DisplayTransactionTest* test) {
3292 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
3293 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07003294 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003295 Case::setupRepaintEverythingCallExpectations(test);
3296 }
3297
3298 static void verifyPostconditions(DisplayTransactionTest* test) {
3299 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
3300 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
3301 }
3302};
3303
3304struct TransitionOffToDozeSuspendVariant
Peiyong Lin65248e02020-04-18 21:15:07 -07003305 : public TransitionVariantCommon<PowerMode::OFF, PowerMode::DOZE_SUSPEND> {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003306 template <typename Case>
3307 static void setupCallExpectations(DisplayTransactionTest* test) {
3308 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
Dominik Laskowski8b01cc02020-07-14 19:02:41 -07003309 Case::EventThread::setupVsyncAndEventThreadNoCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003310 Case::setupRepaintEverythingCallExpectations(test);
3311 }
3312
3313 static void verifyPostconditions(DisplayTransactionTest* test) {
3314 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
3315 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
3316 }
3317};
3318
Peiyong Lin65248e02020-04-18 21:15:07 -07003319struct TransitionOnToOffVariant : public TransitionVariantCommon<PowerMode::ON, PowerMode::OFF> {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003320 template <typename Case>
3321 static void setupCallExpectations(DisplayTransactionTest* test) {
3322 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07003323 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003324 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
3325 }
3326
3327 static void verifyPostconditions(DisplayTransactionTest* test) {
3328 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
3329 }
3330};
3331
3332struct TransitionDozeSuspendToOffVariant
Peiyong Lin65248e02020-04-18 21:15:07 -07003333 : public TransitionVariantCommon<PowerMode::DOZE_SUSPEND, PowerMode::OFF> {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003334 template <typename Case>
3335 static void setupCallExpectations(DisplayTransactionTest* test) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -07003336 Case::EventThread::setupVsyncAndEventThreadNoCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003337 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
3338 }
3339
3340 static void verifyPostconditions(DisplayTransactionTest* test) {
3341 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
3342 }
3343};
3344
Peiyong Lin65248e02020-04-18 21:15:07 -07003345struct TransitionOnToDozeVariant : public TransitionVariantCommon<PowerMode::ON, PowerMode::DOZE> {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003346 template <typename Case>
3347 static void setupCallExpectations(DisplayTransactionTest* test) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -07003348 Case::EventThread::setupVsyncAndEventThreadNoCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003349 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
3350 }
3351};
3352
3353struct TransitionDozeSuspendToDozeVariant
Peiyong Lin65248e02020-04-18 21:15:07 -07003354 : public TransitionVariantCommon<PowerMode::DOZE_SUSPEND, PowerMode::DOZE> {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003355 template <typename Case>
3356 static void setupCallExpectations(DisplayTransactionTest* test) {
3357 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07003358 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003359 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
3360 }
3361};
3362
Peiyong Lin65248e02020-04-18 21:15:07 -07003363struct TransitionDozeToOnVariant : public TransitionVariantCommon<PowerMode::DOZE, PowerMode::ON> {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003364 template <typename Case>
3365 static void setupCallExpectations(DisplayTransactionTest* test) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -07003366 Case::EventThread::setupVsyncAndEventThreadNoCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003367 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
3368 }
3369};
3370
3371struct TransitionDozeSuspendToOnVariant
Peiyong Lin65248e02020-04-18 21:15:07 -07003372 : public TransitionVariantCommon<PowerMode::DOZE_SUSPEND, PowerMode::ON> {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003373 template <typename Case>
3374 static void setupCallExpectations(DisplayTransactionTest* test) {
3375 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07003376 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003377 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
3378 }
3379};
3380
3381struct TransitionOnToDozeSuspendVariant
Peiyong Lin65248e02020-04-18 21:15:07 -07003382 : public TransitionVariantCommon<PowerMode::ON, PowerMode::DOZE_SUSPEND> {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003383 template <typename Case>
3384 static void setupCallExpectations(DisplayTransactionTest* test) {
3385 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07003386 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003387 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
3388 }
3389};
3390
3391struct TransitionOnToUnknownVariant
Peiyong Lin65248e02020-04-18 21:15:07 -07003392 : public TransitionVariantCommon<PowerMode::ON, static_cast<PowerMode>(POWER_MODE_LEET)> {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003393 template <typename Case>
3394 static void setupCallExpectations(DisplayTransactionTest* test) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -07003395 Case::EventThread::setupVsyncAndEventThreadNoCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003396 Case::setupNoComposerPowerModeCallExpectations(test);
3397 }
3398};
3399
3400// --------------------------------------------------------------------
3401// Note:
3402//
3403// Rather than testing the cartesian product of of
3404// DozeIsSupported/DozeNotSupported with all other options, we use one for one
3405// display type, and the other for another display type.
3406// --------------------------------------------------------------------
3407
3408template <typename DisplayVariant, typename DozeVariant, typename EventThreadVariant,
Lloyd Pique41be5d22018-06-21 13:11:48 -07003409 typename DispSyncVariant, typename TransitionVariant>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003410struct DisplayPowerCase {
3411 using Display = DisplayVariant;
3412 using Doze = DozeVariant;
3413 using EventThread = EventThreadVariant;
Lloyd Pique41be5d22018-06-21 13:11:48 -07003414 using DispSync = DispSyncVariant;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003415 using Transition = TransitionVariant;
3416
Peiyong Lin65248e02020-04-18 21:15:07 -07003417 static auto injectDisplayWithInitialPowerMode(DisplayTransactionTest* test, PowerMode mode) {
Lloyd Pique86fa3db2019-02-04 18:46:01 -08003418 Display::injectHwcDisplayWithNoDefaultCapabilities(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003419 auto display = Display::makeFakeExistingDisplayInjector(test);
3420 display.inject();
3421 display.mutableDisplayDevice()->setPowerMode(mode);
3422 return display;
3423 }
3424
3425 static void setInitialPrimaryHWVsyncEnabled(DisplayTransactionTest* test, bool enabled) {
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -07003426 test->mFlinger.scheduler()->mutablePrimaryHWVsyncEnabled() = enabled;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003427 }
3428
3429 static void setupRepaintEverythingCallExpectations(DisplayTransactionTest* test) {
3430 EXPECT_CALL(*test->mMessageQueue, invalidate()).Times(1);
3431 }
3432
Peiyong Lin65248e02020-04-18 21:15:07 -07003433 static void setupSurfaceInterceptorCallExpectations(DisplayTransactionTest* test,
3434 PowerMode mode) {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003435 EXPECT_CALL(*test->mSurfaceInterceptor, isEnabled()).WillOnce(Return(true));
Peiyong Lin65248e02020-04-18 21:15:07 -07003436 EXPECT_CALL(*test->mSurfaceInterceptor, savePowerModeUpdate(_, static_cast<int32_t>(mode)))
3437 .Times(1);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003438 }
3439
Peiyong Lin65248e02020-04-18 21:15:07 -07003440 static void setupComposerCallExpectations(DisplayTransactionTest* test, PowerMode mode) {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003441 // Any calls to get the active config will return a default value.
3442 EXPECT_CALL(*test->mComposer, getActiveConfig(Display::HWC_DISPLAY_ID, _))
3443 .WillRepeatedly(DoAll(SetArgPointee<1>(Display::HWC_ACTIVE_CONFIG_ID),
3444 Return(Error::NONE)));
3445
3446 // Any calls to get whether the display supports dozing will return the value set by the
3447 // policy variant.
3448 EXPECT_CALL(*test->mComposer, getDozeSupport(Display::HWC_DISPLAY_ID, _))
3449 .WillRepeatedly(DoAll(SetArgPointee<1>(Doze::DOZE_SUPPORTED), Return(Error::NONE)));
3450
3451 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, mode)).Times(1);
3452 }
3453
3454 static void setupNoComposerPowerModeCallExpectations(DisplayTransactionTest* test) {
3455 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, _)).Times(0);
3456 }
3457};
3458
3459// A sample configuration for the primary display.
3460// In addition to having event thread support, we emulate doze support.
3461template <typename TransitionVariant>
Peiyong Lined531a32018-10-26 18:27:56 -07003462using PrimaryDisplayPowerCase =
3463 DisplayPowerCase<PrimaryDisplayVariant, DozeIsSupportedVariant<PrimaryDisplayVariant>,
3464 EventThreadIsSupportedVariant, DispSyncIsSupportedVariant,
3465 TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003466
3467// A sample configuration for the external display.
3468// In addition to not having event thread support, we emulate not having doze
3469// support.
3470template <typename TransitionVariant>
Peiyong Lined531a32018-10-26 18:27:56 -07003471using ExternalDisplayPowerCase =
3472 DisplayPowerCase<ExternalDisplayVariant, DozeNotSupportedVariant<ExternalDisplayVariant>,
3473 EventThreadNotSupportedVariant, DispSyncNotSupportedVariant,
3474 TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003475
3476class SetPowerModeInternalTest : public DisplayTransactionTest {
3477public:
3478 template <typename Case>
3479 void transitionDisplayCommon();
3480};
3481
Peiyong Lin65248e02020-04-18 21:15:07 -07003482template <PowerMode PowerMode>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003483struct PowerModeInitialVSyncEnabled : public std::false_type {};
3484
3485template <>
Peiyong Lin65248e02020-04-18 21:15:07 -07003486struct PowerModeInitialVSyncEnabled<PowerMode::ON> : public std::true_type {};
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003487
3488template <>
Peiyong Lin65248e02020-04-18 21:15:07 -07003489struct PowerModeInitialVSyncEnabled<PowerMode::DOZE> : public std::true_type {};
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003490
3491template <typename Case>
3492void SetPowerModeInternalTest::transitionDisplayCommon() {
3493 // --------------------------------------------------------------------
3494 // Preconditions
3495
Peiyong Lined531a32018-10-26 18:27:56 -07003496 Case::Doze::setupComposerCallExpectations(this);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003497 auto display =
3498 Case::injectDisplayWithInitialPowerMode(this, Case::Transition::INITIAL_POWER_MODE);
3499 Case::setInitialPrimaryHWVsyncEnabled(this,
3500 PowerModeInitialVSyncEnabled<
3501 Case::Transition::INITIAL_POWER_MODE>::value);
3502
3503 // --------------------------------------------------------------------
3504 // Call Expectations
3505
3506 Case::setupSurfaceInterceptorCallExpectations(this, Case::Transition::TARGET_POWER_MODE);
3507 Case::Transition::template setupCallExpectations<Case>(this);
3508
3509 // --------------------------------------------------------------------
3510 // Invocation
3511
3512 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(),
3513 Case::Transition::TARGET_POWER_MODE);
3514
3515 // --------------------------------------------------------------------
3516 // Postconditions
3517
3518 Case::Transition::verifyPostconditions(this);
3519}
3520
3521TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfNoChange) {
3522 using Case = SimplePrimaryDisplayCase;
3523
3524 // --------------------------------------------------------------------
3525 // Preconditions
3526
3527 // A primary display device is set up
3528 Case::Display::injectHwcDisplay(this);
3529 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
3530 display.inject();
3531
Peiyong Lin65248e02020-04-18 21:15:07 -07003532 // The display is already set to PowerMode::ON
3533 display.mutableDisplayDevice()->setPowerMode(PowerMode::ON);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003534
3535 // --------------------------------------------------------------------
3536 // Invocation
3537
Peiyong Lin65248e02020-04-18 21:15:07 -07003538 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), PowerMode::ON);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003539
3540 // --------------------------------------------------------------------
3541 // Postconditions
3542
Peiyong Lin65248e02020-04-18 21:15:07 -07003543 EXPECT_EQ(PowerMode::ON, display.mutableDisplayDevice()->getPowerMode());
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003544}
3545
Dominik Laskowskieecd6592018-05-29 10:25:41 -07003546TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfVirtualDisplay) {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003547 using Case = HwcVirtualDisplayCase;
3548
3549 // --------------------------------------------------------------------
3550 // Preconditions
3551
Dominik Laskowski075d3172018-05-24 15:50:06 -07003552 // Insert display data so that the HWC thinks it created the virtual display.
3553 const auto displayId = Case::Display::DISPLAY_ID::get();
3554 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08003555 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003556
3557 // A virtual display device is set up
3558 Case::Display::injectHwcDisplay(this);
3559 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
3560 display.inject();
3561
Peiyong Lin65248e02020-04-18 21:15:07 -07003562 // The display is set to PowerMode::ON
3563 getDisplayDevice(display.token())->setPowerMode(PowerMode::ON);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003564
3565 // --------------------------------------------------------------------
3566 // Invocation
3567
Peiyong Lin65248e02020-04-18 21:15:07 -07003568 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), PowerMode::OFF);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003569
3570 // --------------------------------------------------------------------
3571 // Postconditions
3572
Peiyong Lin65248e02020-04-18 21:15:07 -07003573 EXPECT_EQ(PowerMode::ON, display.mutableDisplayDevice()->getPowerMode());
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003574}
3575
3576TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnPrimaryDisplay) {
3577 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToOnVariant>>();
3578}
3579
3580TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendPrimaryDisplay) {
3581 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
3582}
3583
3584TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffPrimaryDisplay) {
3585 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToOffVariant>>();
3586}
3587
3588TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffPrimaryDisplay) {
3589 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
3590}
3591
3592TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozePrimaryDisplay) {
3593 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeVariant>>();
3594}
3595
3596TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozePrimaryDisplay) {
3597 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
3598}
3599
3600TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnPrimaryDisplay) {
3601 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeToOnVariant>>();
3602}
3603
3604TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnPrimaryDisplay) {
3605 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
3606}
3607
3608TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendPrimaryDisplay) {
3609 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
3610}
3611
3612TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownPrimaryDisplay) {
3613 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToUnknownVariant>>();
3614}
3615
3616TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnExternalDisplay) {
3617 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToOnVariant>>();
3618}
3619
3620TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendExternalDisplay) {
3621 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
3622}
3623
3624TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffExternalDisplay) {
3625 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToOffVariant>>();
3626}
3627
3628TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffExternalDisplay) {
3629 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
3630}
3631
3632TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeExternalDisplay) {
3633 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeVariant>>();
3634}
3635
3636TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozeExternalDisplay) {
3637 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
3638}
3639
3640TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnExternalDisplay) {
3641 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeToOnVariant>>();
3642}
3643
3644TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnExternalDisplay) {
3645 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
3646}
3647
3648TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendExternalDisplay) {
3649 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
3650}
3651
3652TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownExternalDisplay) {
3653 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToUnknownVariant>>();
3654}
3655
Lloyd Piquef58625d2017-12-19 13:22:33 -08003656} // namespace
3657} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08003658
3659// TODO(b/129481165): remove the #pragma below and fix conversion issues
3660#pragma clang diagnostic pop // ignored "-Wconversion"