blob: 6d00cccb48f355b6f204d8aa423373c44d315ff5 [file] [log] [blame]
Lloyd Piquef58625d2017-12-19 13:22:33 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Lloyd Piquef58625d2017-12-19 13:22:33 -080021#undef LOG_TAG
22#define LOG_TAG "LibSurfaceFlingerUnittests"
23
Dominik Laskowski075d3172018-05-24 15:50:06 -070024#include <type_traits>
25
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070026#include <compositionengine/Display.h>
27#include <compositionengine/DisplayColorProfile.h>
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070028#include <compositionengine/impl/Display.h>
Lloyd Pique33050472019-12-19 17:12:44 -080029#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070030#include <compositionengine/mock/Display.h>
31#include <compositionengine/mock/DisplayColorProfile.h>
Lloyd Pique542307f2018-10-19 13:24:08 -070032#include <compositionengine/mock/DisplaySurface.h>
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070033#include <compositionengine/mock/RenderSurface.h>
Lloyd Piquef58625d2017-12-19 13:22:33 -080034#include <gmock/gmock.h>
35#include <gtest/gtest.h>
Lloyd Pique1ebe0902019-10-04 14:47:13 -070036#include <gui/mock/GraphicBufferConsumer.h>
37#include <gui/mock/GraphicBufferProducer.h>
Lloyd Piquef58625d2017-12-19 13:22:33 -080038#include <log/log.h>
Lloyd Pique3823e7b2018-10-18 16:58:10 -070039#include <renderengine/mock/RenderEngine.h>
Valerie Hau9758ae02018-10-09 16:05:09 -070040#include <ui/DebugUtils.h>
Dominik Laskowski075d3172018-05-24 15:50:06 -070041
42#include "DisplayIdentificationTest.h"
Ana Krulecafb45842019-02-13 13:33:03 -080043#include "TestableScheduler.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080044#include "TestableSurfaceFlinger.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080045#include "mock/DisplayHardware/MockComposer.h"
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070046#include "mock/DisplayHardware/MockPowerAdvisor.h"
Lloyd Pique41be5d22018-06-21 13:11:48 -070047#include "mock/MockDispSync.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080048#include "mock/MockEventControlThread.h"
49#include "mock/MockEventThread.h"
50#include "mock/MockMessageQueue.h"
51#include "mock/MockNativeWindowSurface.h"
52#include "mock/MockSurfaceInterceptor.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080053#include "mock/system/window/MockNativeWindow.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080054
55namespace android {
56namespace {
57
Lloyd Piquee39cad22017-12-20 17:01:29 -080058using testing::_;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070059using testing::AnyNumber;
Lloyd Piquee39cad22017-12-20 17:01:29 -080060using testing::DoAll;
61using testing::Mock;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080062using testing::ResultOf;
Lloyd Piquee39cad22017-12-20 17:01:29 -080063using testing::Return;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070064using testing::ReturnRefOfCopy;
Lloyd Piquee39cad22017-12-20 17:01:29 -080065using testing::SetArgPointee;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070066using testing::StrictMock;
Lloyd Piquee39cad22017-12-20 17:01:29 -080067
Lloyd Piqued883d5a2018-04-27 19:32:30 -070068using android::Hwc2::ColorMode;
Lloyd Piquee39cad22017-12-20 17:01:29 -080069using android::Hwc2::Error;
Lloyd Piqued883d5a2018-04-27 19:32:30 -070070using android::Hwc2::Hdr;
Lloyd Piquee39cad22017-12-20 17:01:29 -080071using android::Hwc2::IComposer;
72using android::Hwc2::IComposerClient;
Lloyd Piqued883d5a2018-04-27 19:32:30 -070073using android::Hwc2::PerFrameMetadataKey;
74using android::Hwc2::RenderIntent;
Lloyd Piquee39cad22017-12-20 17:01:29 -080075
Lloyd Piquec11e0d32018-01-22 18:44:59 -080076using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector;
77using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
Lloyd Pique1fa4d462018-01-22 18:03:16 -080078using HotplugEvent = TestableSurfaceFlinger::HotplugEvent;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080079using HWC2Display = TestableSurfaceFlinger::HWC2Display;
Lloyd Piquebc792092018-01-17 11:52:30 -080080
Lloyd Piquec11e0d32018-01-22 18:44:59 -080081constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666;
Lloyd Piquee39cad22017-12-20 17:01:29 -080082constexpr int32_t DEFAULT_DPI = 320;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080083constexpr int DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT = HAL_PIXEL_FORMAT_RGB_565;
Lloyd Piquee39cad22017-12-20 17:01:29 -080084
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -080085constexpr int HWC_POWER_MODE_LEET = 1337; // An out of range power mode value
86
Lloyd Piquec11e0d32018-01-22 18:44:59 -080087/* ------------------------------------------------------------------------
88 * Boolean avoidance
89 *
90 * To make calls and template instantiations more readable, we define some
91 * local enums along with an implicit bool conversion.
92 */
93
94#define BOOL_SUBSTITUTE(TYPENAME) enum class TYPENAME : bool { FALSE = false, TRUE = true };
95
Lloyd Piquec11e0d32018-01-22 18:44:59 -080096BOOL_SUBSTITUTE(Async);
Dominik Laskowski075d3172018-05-24 15:50:06 -070097BOOL_SUBSTITUTE(Critical);
98BOOL_SUBSTITUTE(Primary);
Lloyd Piquec11e0d32018-01-22 18:44:59 -080099BOOL_SUBSTITUTE(Secure);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700100BOOL_SUBSTITUTE(Virtual);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800101
102/* ------------------------------------------------------------------------
103 *
104 */
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800105
Lloyd Piquef58625d2017-12-19 13:22:33 -0800106class DisplayTransactionTest : public testing::Test {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800107public:
Lloyd Piquef58625d2017-12-19 13:22:33 -0800108 DisplayTransactionTest();
109 ~DisplayTransactionTest() override;
110
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800111 // --------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800112 // Mock/Fake injection
Lloyd Piquef58625d2017-12-19 13:22:33 -0800113
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700114 void injectMockScheduler();
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800115 void injectMockComposer(int virtualDisplayCount);
116 void injectFakeBufferQueueFactory();
117 void injectFakeNativeWindowSurfaceFactory();
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700118 sp<DisplayDevice> injectDefaultInternalDisplay(std::function<void(FakeDisplayDeviceInjector&)>);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800119
120 // --------------------------------------------------------------------
121 // Postcondition helpers
122
Dominik Laskowski075d3172018-05-24 15:50:06 -0700123 bool hasPhysicalHwcDisplay(hwc2_display_t hwcDisplayId);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800124 bool hasTransactionFlagSet(int flag);
125 bool hasDisplayDevice(sp<IBinder> displayToken);
126 sp<DisplayDevice> getDisplayDevice(sp<IBinder> displayToken);
127 bool hasCurrentDisplayState(sp<IBinder> displayToken);
128 const DisplayDeviceState& getCurrentDisplayState(sp<IBinder> displayToken);
129 bool hasDrawingDisplayState(sp<IBinder> displayToken);
130 const DisplayDeviceState& getDrawingDisplayState(sp<IBinder> displayToken);
131
132 // --------------------------------------------------------------------
133 // Test instances
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800134
Lloyd Piquef58625d2017-12-19 13:22:33 -0800135 TestableSurfaceFlinger mFlinger;
Alec Mouriba013fa2018-10-16 12:43:11 -0700136 sp<mock::NativeWindow> mNativeWindow = new mock::NativeWindow();
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800137 sp<GraphicBuffer> mBuffer = new GraphicBuffer();
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700138 Hwc2::mock::PowerAdvisor mPowerAdvisor;
Lloyd Piquee39cad22017-12-20 17:01:29 -0800139
140 // These mocks are created by the test, but are destroyed by SurfaceFlinger
141 // by virtue of being stored into a std::unique_ptr. However we still need
142 // to keep a reference to them for use in setting up call expectations.
Peiyong Lin833074a2018-08-28 11:53:54 -0700143 renderengine::mock::RenderEngine* mRenderEngine = new renderengine::mock::RenderEngine();
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800144 Hwc2::mock::Composer* mComposer = nullptr;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800145 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
146 mock::SurfaceInterceptor* mSurfaceInterceptor = new mock::SurfaceInterceptor();
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700147
148 mock::DispSync* mPrimaryDispSync = new mock::DispSync;
149 mock::EventControlThread* mEventControlThread = new mock::EventControlThread;
150 mock::EventThread* mEventThread = new mock::EventThread;
151 mock::EventThread* mSFEventThread = new mock::EventThread;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800152
153 // These mocks are created only when expected to be created via a factory.
154 sp<mock::GraphicBufferConsumer> mConsumer;
155 sp<mock::GraphicBufferProducer> mProducer;
Lloyd Pique22098362018-09-13 11:46:49 -0700156 surfaceflinger::mock::NativeWindowSurface* mNativeWindowSurface = nullptr;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800157};
158
159DisplayTransactionTest::DisplayTransactionTest() {
160 const ::testing::TestInfo* const test_info =
161 ::testing::UnitTest::GetInstance()->current_test_info();
162 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800163
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800164 // Default to no wide color display support configured
165 mFlinger.mutableHasWideColorDisplay() = false;
Peiyong Lin13effd12018-07-24 17:01:47 -0700166 mFlinger.mutableUseColorManagement() = false;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800167 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800168
169 // Default to using HWC virtual displays
170 mFlinger.mutableUseHwcVirtualDisplays() = true;
171
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800172 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
173 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
174 });
175
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800176 mFlinger.setCreateNativeWindowSurface([](auto) {
177 ADD_FAILURE() << "Unexpected request to create a native window surface.";
178 return nullptr;
179 });
180
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700181 injectMockScheduler();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800182 mFlinger.mutableEventQueue().reset(mMessageQueue);
Peiyong Lin833074a2018-08-28 11:53:54 -0700183 mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800184 mFlinger.mutableInterceptor().reset(mSurfaceInterceptor);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800185
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800186 injectMockComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -0800187}
188
189DisplayTransactionTest::~DisplayTransactionTest() {
190 const ::testing::TestInfo* const test_info =
191 ::testing::UnitTest::GetInstance()->current_test_info();
192 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
193}
194
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700195void DisplayTransactionTest::injectMockScheduler() {
Ana Krulecafb45842019-02-13 13:33:03 -0800196 EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_));
Dominik Laskowski98041832019-08-01 18:35:59 -0700197 EXPECT_CALL(*mEventThread, createEventConnection(_, _))
198 .WillOnce(Return(new EventThreadConnection(mEventThread, ResyncCallback(),
199 ISurfaceComposer::eConfigChangedSuppress)));
200
Ana Krulecafb45842019-02-13 13:33:03 -0800201 EXPECT_CALL(*mSFEventThread, registerDisplayEventConnection(_));
Dominik Laskowski98041832019-08-01 18:35:59 -0700202 EXPECT_CALL(*mSFEventThread, createEventConnection(_, _))
203 .WillOnce(Return(new EventThreadConnection(mSFEventThread, ResyncCallback(),
204 ISurfaceComposer::eConfigChangedSuppress)));
Ana Krulecafb45842019-02-13 13:33:03 -0800205
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700206 mFlinger.setupScheduler(std::unique_ptr<DispSync>(mPrimaryDispSync),
207 std::unique_ptr<EventControlThread>(mEventControlThread),
208 std::unique_ptr<EventThread>(mEventThread),
209 std::unique_ptr<EventThread>(mSFEventThread));
Ana Krulecafb45842019-02-13 13:33:03 -0800210}
211
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800212void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
213 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800214 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
215 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -0800216
Lloyd Piquee39cad22017-12-20 17:01:29 -0800217 Mock::VerifyAndClear(mComposer);
218}
219
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800220void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800221 // This setup is only expected once per test.
222 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
223
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800224 mConsumer = new mock::GraphicBufferConsumer();
225 mProducer = new mock::GraphicBufferProducer();
226
227 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
228 *outProducer = mProducer;
229 *outConsumer = mConsumer;
230 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800231}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800232
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800233void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800234 // This setup is only expected once per test.
235 ASSERT_TRUE(mNativeWindowSurface == nullptr);
236
Lloyd Pique22098362018-09-13 11:46:49 -0700237 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800238
Lloyd Pique22098362018-09-13 11:46:49 -0700239 mFlinger.setCreateNativeWindowSurface([this](auto) {
240 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
241 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800242}
243
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700244sp<DisplayDevice> DisplayTransactionTest::injectDefaultInternalDisplay(
245 std::function<void(FakeDisplayDeviceInjector&)> injectExtra) {
246 constexpr DisplayId DEFAULT_DISPLAY_ID = DisplayId{777};
247 constexpr int DEFAULT_DISPLAY_WIDTH = 1080;
248 constexpr int DEFAULT_DISPLAY_HEIGHT = 1920;
249
250 // The DisplayDevice is required to have a framebuffer (behind the
251 // ANativeWindow interface) which uses the actual hardware display
252 // size.
253 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
254 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_DISPLAY_WIDTH), Return(0)));
255 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
256 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_DISPLAY_HEIGHT), Return(0)));
257 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT));
258 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT));
259 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64));
260 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT)).Times(AnyNumber());
261
262 auto compositionDisplay = compositionengine::impl::
263 createDisplay(mFlinger.getCompositionEngine(),
264 compositionengine::DisplayCreationArgsBuilder()
265 .setPhysical(
266 {DEFAULT_DISPLAY_ID, DisplayConnectionType::Internal})
267 .setPixels({DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT})
268 .setPowerAdvisor(&mPowerAdvisor)
269 .build());
270
271 auto injector =
272 FakeDisplayDeviceInjector(mFlinger, compositionDisplay, DisplayConnectionType::Internal,
273 true /* isPrimary */);
274
275 injector.setNativeWindow(mNativeWindow);
276 if (injectExtra) {
277 injectExtra(injector);
278 }
279
280 auto displayDevice = injector.inject();
281
282 Mock::VerifyAndClear(mNativeWindow.get());
283
284 return displayDevice;
285}
286
Dominik Laskowski075d3172018-05-24 15:50:06 -0700287bool DisplayTransactionTest::hasPhysicalHwcDisplay(hwc2_display_t hwcDisplayId) {
288 return mFlinger.mutableHwcPhysicalDisplayIdMap().count(hwcDisplayId) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800289}
290
291bool DisplayTransactionTest::hasTransactionFlagSet(int flag) {
292 return mFlinger.mutableTransactionFlags() & flag;
293}
294
295bool DisplayTransactionTest::hasDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700296 return mFlinger.mutableDisplays().count(displayToken) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800297}
298
299sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700300 return mFlinger.mutableDisplays()[displayToken];
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800301}
302
303bool DisplayTransactionTest::hasCurrentDisplayState(sp<IBinder> displayToken) {
304 return mFlinger.mutableCurrentState().displays.indexOfKey(displayToken) >= 0;
305}
306
307const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(sp<IBinder> displayToken) {
308 return mFlinger.mutableCurrentState().displays.valueFor(displayToken);
309}
310
311bool DisplayTransactionTest::hasDrawingDisplayState(sp<IBinder> displayToken) {
312 return mFlinger.mutableDrawingState().displays.indexOfKey(displayToken) >= 0;
313}
314
315const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(sp<IBinder> displayToken) {
316 return mFlinger.mutableDrawingState().displays.valueFor(displayToken);
317}
318
319/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800320 *
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800321 */
322
Dominik Laskowski075d3172018-05-24 15:50:06 -0700323template <typename PhysicalDisplay>
324struct PhysicalDisplayId {};
325
Dominik Laskowski34157762018-10-31 13:07:19 -0700326template <DisplayId::Type displayId>
327using VirtualDisplayId = std::integral_constant<DisplayId::Type, displayId>;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700328
329struct NoDisplayId {};
330
331template <typename>
332struct IsPhysicalDisplayId : std::bool_constant<false> {};
333
334template <typename PhysicalDisplay>
335struct IsPhysicalDisplayId<PhysicalDisplayId<PhysicalDisplay>> : std::bool_constant<true> {};
336
337template <typename>
338struct DisplayIdGetter;
339
340template <typename PhysicalDisplay>
341struct DisplayIdGetter<PhysicalDisplayId<PhysicalDisplay>> {
342 static std::optional<DisplayId> get() {
343 if (!PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
344 return getFallbackDisplayId(static_cast<bool>(PhysicalDisplay::PRIMARY)
345 ? HWC_DISPLAY_PRIMARY
346 : HWC_DISPLAY_EXTERNAL);
347 }
348
349 const auto info =
350 parseDisplayIdentificationData(PhysicalDisplay::PORT,
351 PhysicalDisplay::GET_IDENTIFICATION_DATA());
352 return info ? std::make_optional(info->id) : std::nullopt;
353 }
354};
355
Dominik Laskowski34157762018-10-31 13:07:19 -0700356template <DisplayId::Type displayId>
Dominik Laskowski075d3172018-05-24 15:50:06 -0700357struct DisplayIdGetter<VirtualDisplayId<displayId>> {
Dominik Laskowski34157762018-10-31 13:07:19 -0700358 static std::optional<DisplayId> get() { return DisplayId{displayId}; }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700359};
360
361template <>
362struct DisplayIdGetter<NoDisplayId> {
363 static std::optional<DisplayId> get() { return {}; }
364};
365
Dominik Laskowski55c85402020-01-21 16:25:47 -0800366template <typename>
367struct DisplayConnectionTypeGetter {
368 static constexpr std::optional<DisplayConnectionType> value;
369};
370
371template <typename PhysicalDisplay>
372struct DisplayConnectionTypeGetter<PhysicalDisplayId<PhysicalDisplay>> {
373 static constexpr std::optional<DisplayConnectionType> value = PhysicalDisplay::CONNECTION_TYPE;
374};
375
Dominik Laskowski075d3172018-05-24 15:50:06 -0700376// DisplayIdType can be:
377// 1) PhysicalDisplayId<...> for generated ID of physical display backed by HWC.
378// 2) VirtualDisplayId<...> for hard-coded ID of virtual display backed by HWC.
379// 3) NoDisplayId for virtual display without HWC backing.
380template <typename DisplayIdType, int width, int height, Critical critical, Async async,
381 Secure secure, Primary primary, int grallocUsage>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800382struct DisplayVariant {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700383 using DISPLAY_ID = DisplayIdGetter<DisplayIdType>;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800384 using CONNECTION_TYPE = DisplayConnectionTypeGetter<DisplayIdType>;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700385
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800386 // The display width and height
387 static constexpr int WIDTH = width;
388 static constexpr int HEIGHT = height;
389
390 static constexpr int GRALLOC_USAGE = grallocUsage;
391
Dominik Laskowski075d3172018-05-24 15:50:06 -0700392 // Whether the display is virtual or physical
393 static constexpr Virtual VIRTUAL =
394 IsPhysicalDisplayId<DisplayIdType>{} ? Virtual::FALSE : Virtual::TRUE;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800395
396 // When creating native window surfaces for the framebuffer, whether those should be critical
397 static constexpr Critical CRITICAL = critical;
398
399 // When creating native window surfaces for the framebuffer, whether those should be async
400 static constexpr Async ASYNC = async;
401
402 // Whether the display should be treated as secure
403 static constexpr Secure SECURE = secure;
404
Dominik Laskowski075d3172018-05-24 15:50:06 -0700405 // Whether the display is primary
406 static constexpr Primary PRIMARY = primary;
407
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800408 static auto makeFakeExistingDisplayInjector(DisplayTransactionTest* test) {
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700409 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder();
410 if (auto displayId = DISPLAY_ID::get()) {
411 ceDisplayArgs.setPhysical({*displayId, DisplayConnectionType::Internal});
412 } else {
413 ceDisplayArgs.setUseHwcVirtualDisplays(false);
414 }
415 ceDisplayArgs.setPixels({WIDTH, HEIGHT}).setPowerAdvisor(&test->mPowerAdvisor).build();
416
417 auto compositionDisplay =
418 compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
419 ceDisplayArgs.build());
420
Dominik Laskowski075d3172018-05-24 15:50:06 -0700421 auto injector =
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700422 FakeDisplayDeviceInjector(test->mFlinger, compositionDisplay,
423 CONNECTION_TYPE::value, static_cast<bool>(PRIMARY));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700424
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800425 injector.setSecure(static_cast<bool>(SECURE));
Alec Mouriba013fa2018-10-16 12:43:11 -0700426 injector.setNativeWindow(test->mNativeWindow);
427
428 // Creating a DisplayDevice requires getting default dimensions from the
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800429 // native window along with some other initial setup.
Alec Mouriba013fa2018-10-16 12:43:11 -0700430 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
431 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
432 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
433 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800434 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT))
435 .WillRepeatedly(Return(0));
436 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT))
437 .WillRepeatedly(Return(0));
438 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64))
439 .WillRepeatedly(Return(0));
chaviw8beb4142019-04-11 13:09:05 -0700440 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT))
441 .WillRepeatedly(Return(0));
442
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800443 return injector;
444 }
445
446 // Called by tests to set up any native window creation call expectations.
447 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
448 EXPECT_CALL(*test->mNativeWindowSurface, getNativeWindow())
449 .WillOnce(Return(test->mNativeWindow));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800450
Alec Mouriba013fa2018-10-16 12:43:11 -0700451 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
452 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
453 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
454 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800455 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT))
456 .WillRepeatedly(Return(0));
457 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT))
458 .WillRepeatedly(Return(0));
459 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64))
460 .WillRepeatedly(Return(0));
chaviw8beb4142019-04-11 13:09:05 -0700461 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT))
462 .WillRepeatedly(Return(0));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800463 }
464
465 static void setupFramebufferConsumerBufferQueueCallExpectations(DisplayTransactionTest* test) {
466 EXPECT_CALL(*test->mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR));
467 EXPECT_CALL(*test->mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR));
468 EXPECT_CALL(*test->mConsumer, setConsumerUsageBits(GRALLOC_USAGE))
469 .WillRepeatedly(Return(NO_ERROR));
470 EXPECT_CALL(*test->mConsumer, setDefaultBufferSize(WIDTH, HEIGHT))
471 .WillRepeatedly(Return(NO_ERROR));
472 EXPECT_CALL(*test->mConsumer, setMaxAcquiredBufferCount(_))
473 .WillRepeatedly(Return(NO_ERROR));
474 }
475
476 static void setupFramebufferProducerBufferQueueCallExpectations(DisplayTransactionTest* test) {
477 EXPECT_CALL(*test->mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return());
478 }
479};
480
Dominik Laskowski075d3172018-05-24 15:50:06 -0700481template <hwc2_display_t hwcDisplayId, HWC2::DisplayType hwcDisplayType, typename DisplayVariant,
482 typename PhysicalDisplay = void>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800483struct HwcDisplayVariant {
484 // The display id supplied by the HWC
485 static constexpr hwc2_display_t HWC_DISPLAY_ID = hwcDisplayId;
486
487 // The HWC display type
488 static constexpr HWC2::DisplayType HWC_DISPLAY_TYPE = hwcDisplayType;
489
490 // The HWC active configuration id
Lloyd Pique3c085a02018-05-09 19:38:32 -0700491 static constexpr int HWC_ACTIVE_CONFIG_ID = 2001;
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700492 static constexpr int INIT_POWER_MODE = HWC_POWER_MODE_NORMAL;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800493
494 static void injectPendingHotplugEvent(DisplayTransactionTest* test,
495 HWC2::Connection connection) {
496 test->mFlinger.mutablePendingHotplugEvents().emplace_back(
497 HotplugEvent{HWC_DISPLAY_ID, connection});
498 }
499
500 // Called by tests to inject a HWC display setup
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800501 static void injectHwcDisplayWithNoDefaultCapabilities(DisplayTransactionTest* test) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700502 const auto displayId = DisplayVariant::DISPLAY_ID::get();
503 ASSERT_TRUE(displayId);
504 FakeHwcDisplayInjector(*displayId, HWC_DISPLAY_TYPE,
505 static_cast<bool>(DisplayVariant::PRIMARY))
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800506 .setHwcDisplayId(HWC_DISPLAY_ID)
507 .setWidth(DisplayVariant::WIDTH)
508 .setHeight(DisplayVariant::HEIGHT)
509 .setActiveConfig(HWC_ACTIVE_CONFIG_ID)
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700510 .setPowerMode(INIT_POWER_MODE)
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800511 .inject(&test->mFlinger, test->mComposer);
512 }
513
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800514 // Called by tests to inject a HWC display setup
515 static void injectHwcDisplay(DisplayTransactionTest* test) {
516 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(HWC_DISPLAY_ID, _))
517 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hwc2::DisplayCapability>({})),
518 Return(Error::NONE)));
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700519 EXPECT_CALL(*test->mComposer,
520 setPowerMode(HWC_DISPLAY_ID,
521 static_cast<Hwc2::IComposerClient::PowerMode>(INIT_POWER_MODE)))
522 .WillOnce(Return(Error::NONE));
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800523 injectHwcDisplayWithNoDefaultCapabilities(test);
524 }
525
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700526 static std::shared_ptr<compositionengine::Display> injectCompositionDisplay(
527 DisplayTransactionTest* test) {
528 const ::testing::TestInfo* const test_info =
529 ::testing::UnitTest::GetInstance()->current_test_info();
530
531 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder()
532 .setPhysical({*DisplayVariant::DISPLAY_ID::get(),
533 PhysicalDisplay::CONNECTION_TYPE})
534 .setPixels({DisplayVariant::WIDTH, DisplayVariant::HEIGHT})
535 .setIsSecure(static_cast<bool>(DisplayVariant::SECURE))
536 .setPowerAdvisor(&test->mPowerAdvisor)
537 .setName(std::string("Injected display for ") +
538 test_info->test_case_name() + "." + test_info->name())
539 .build();
540
541 return compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
542 ceDisplayArgs);
543 }
544
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800545 static void setupHwcHotplugCallExpectations(DisplayTransactionTest* test) {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800546 constexpr auto CONNECTION_TYPE =
547 PhysicalDisplay::CONNECTION_TYPE == DisplayConnectionType::Internal
548 ? IComposerClient::DisplayConnectionType::INTERNAL
549 : IComposerClient::DisplayConnectionType::EXTERNAL;
550
551 EXPECT_CALL(*test->mComposer, getDisplayConnectionType(HWC_DISPLAY_ID, _))
552 .WillOnce(
553 DoAll(SetArgPointee<1>(CONNECTION_TYPE), Return(Hwc2::V2_4::Error::NONE)));
554
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800555 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
556 EXPECT_CALL(*test->mComposer, getDisplayConfigs(HWC_DISPLAY_ID, _))
557 .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{HWC_ACTIVE_CONFIG_ID}),
558 Return(Error::NONE)));
559 EXPECT_CALL(*test->mComposer,
560 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
561 IComposerClient::Attribute::WIDTH, _))
562 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::WIDTH), Return(Error::NONE)));
563 EXPECT_CALL(*test->mComposer,
564 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
565 IComposerClient::Attribute::HEIGHT, _))
566 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::HEIGHT), Return(Error::NONE)));
567 EXPECT_CALL(*test->mComposer,
568 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
569 IComposerClient::Attribute::VSYNC_PERIOD, _))
570 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE)));
571 EXPECT_CALL(*test->mComposer,
572 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
573 IComposerClient::Attribute::DPI_X, _))
574 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
575 EXPECT_CALL(*test->mComposer,
576 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
577 IComposerClient::Attribute::DPI_Y, _))
578 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
Ady Abraham7159f572019-10-11 11:10:18 -0700579 EXPECT_CALL(*test->mComposer,
580 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
581 IComposerClient::Attribute::CONFIG_GROUP, _))
582 .WillOnce(DoAll(SetArgPointee<3>(-1), Return(Error::NONE)));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700583
584 if (PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
585 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
586 .WillOnce(DoAll(SetArgPointee<1>(PhysicalDisplay::PORT),
587 SetArgPointee<2>(PhysicalDisplay::GET_IDENTIFICATION_DATA()),
588 Return(Error::NONE)));
589 } else {
590 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
591 .WillOnce(Return(Error::UNSUPPORTED));
592 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800593 }
594
595 // Called by tests to set up HWC call expectations
596 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
597 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY_ID, _))
Lloyd Pique3c085a02018-05-09 19:38:32 -0700598 .WillRepeatedly(DoAll(SetArgPointee<1>(HWC_ACTIVE_CONFIG_ID), Return(Error::NONE)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800599 }
600};
601
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800602// Physical displays are expected to be synchronous, secure, and have a HWC display for output.
603constexpr uint32_t GRALLOC_USAGE_PHYSICAL_DISPLAY =
604 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
605
Dominik Laskowski075d3172018-05-24 15:50:06 -0700606template <hwc2_display_t hwcDisplayId, typename PhysicalDisplay, int width, int height,
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800607 Critical critical>
608struct PhysicalDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700609 : DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height, critical, Async::FALSE,
610 Secure::TRUE, PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
611 HwcDisplayVariant<hwcDisplayId, HWC2::DisplayType::Physical,
612 DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height,
613 critical, Async::FALSE, Secure::TRUE,
614 PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
615 PhysicalDisplay> {};
616
617template <bool hasIdentificationData>
618struct PrimaryDisplay {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800619 static constexpr auto CONNECTION_TYPE = DisplayConnectionType::Internal;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700620 static constexpr Primary PRIMARY = Primary::TRUE;
621 static constexpr uint8_t PORT = 255;
622 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
623 static constexpr auto GET_IDENTIFICATION_DATA = getInternalEdid;
624};
625
626template <bool hasIdentificationData>
627struct ExternalDisplay {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800628 static constexpr auto CONNECTION_TYPE = DisplayConnectionType::External;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700629 static constexpr Primary PRIMARY = Primary::FALSE;
630 static constexpr uint8_t PORT = 254;
631 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
632 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
633};
634
635struct TertiaryDisplay {
636 static constexpr Primary PRIMARY = Primary::FALSE;
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800637 static constexpr uint8_t PORT = 253;
638 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700639};
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800640
641// A primary display is a physical display that is critical
642using PrimaryDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700643 PhysicalDisplayVariant<1001, PrimaryDisplay<false>, 3840, 2160, Critical::TRUE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800644
645// An external display is physical display that is not critical.
646using ExternalDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700647 PhysicalDisplayVariant<1002, ExternalDisplay<false>, 1920, 1280, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800648
649using TertiaryDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700650 PhysicalDisplayVariant<1003, TertiaryDisplay, 1600, 1200, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800651
652// A virtual display not supported by the HWC.
653constexpr uint32_t GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY = 0;
654
655template <int width, int height, Secure secure>
656struct NonHwcVirtualDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700657 : DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
658 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY> {
659 using Base = DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
660 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>;
661
662 static void injectHwcDisplay(DisplayTransactionTest*) {}
663
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700664 static std::shared_ptr<compositionengine::Display> injectCompositionDisplay(
665 DisplayTransactionTest* test) {
666 const ::testing::TestInfo* const test_info =
667 ::testing::UnitTest::GetInstance()->current_test_info();
668
669 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder()
670 .setPixels({Base::WIDTH, Base::HEIGHT})
671 .setIsSecure(static_cast<bool>(Base::SECURE))
672 .setPowerAdvisor(&test->mPowerAdvisor)
673 .setName(std::string("Injected display for ") +
674 test_info->test_case_name() + "." + test_info->name())
675 .build();
676
677 return compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
678 ceDisplayArgs);
679 }
680
Dominik Laskowski075d3172018-05-24 15:50:06 -0700681 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
682 EXPECT_CALL(*test->mComposer, getActiveConfig(_, _)).Times(0);
683 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800684
685 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
686 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
687 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
688 }
689};
690
691// A virtual display supported by the HWC.
692constexpr uint32_t GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY = GRALLOC_USAGE_HW_COMPOSER;
693
694template <int width, int height, Secure secure>
695struct HwcVirtualDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700696 : DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE, secure,
697 Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>,
698 HwcDisplayVariant<
699 1010, HWC2::DisplayType::Virtual,
700 DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
701 secure, Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>> {
702 using Base = DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
703 secure, Primary::FALSE, GRALLOC_USAGE_HW_COMPOSER>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800704 using Self = HwcVirtualDisplayVariant<width, height, secure>;
705
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700706 static std::shared_ptr<compositionengine::Display> injectCompositionDisplay(
707 DisplayTransactionTest* test) {
708 const ::testing::TestInfo* const test_info =
709 ::testing::UnitTest::GetInstance()->current_test_info();
710
711 auto ceDisplayArgs = compositionengine::DisplayCreationArgsBuilder()
712 .setUseHwcVirtualDisplays(false)
713 .setPixels({Base::WIDTH, Base::HEIGHT})
714 .setIsSecure(static_cast<bool>(Base::SECURE))
715 .setPowerAdvisor(&test->mPowerAdvisor)
716 .setName(std::string("Injected display for ") +
717 test_info->test_case_name() + "." + test_info->name())
718 .build();
719
720 auto compositionDisplay =
721 compositionengine::impl::createDisplay(test->mFlinger.getCompositionEngine(),
722 ceDisplayArgs);
723 compositionDisplay->setDisplayIdForTesting(Base::DISPLAY_ID::get());
724
725 // Insert display data so that the HWC thinks it created the virtual display.
726 if (const auto displayId = Base::DISPLAY_ID::get()) {
727 test->mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
728 }
729
730 return compositionDisplay;
731 }
732
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800733 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
734 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
735 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
736 }
737
738 static void setupHwcVirtualDisplayCreationCallExpectations(DisplayTransactionTest* test) {
739 EXPECT_CALL(*test->mComposer, createVirtualDisplay(Base::WIDTH, Base::HEIGHT, _, _))
740 .WillOnce(DoAll(SetArgPointee<3>(Self::HWC_DISPLAY_ID), Return(Error::NONE)));
741 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
742 }
743};
744
745// For this variant, SurfaceFlinger should not configure itself with wide
746// display support, so the display should not be configured for wide-color
747// support.
748struct WideColorSupportNotConfiguredVariant {
749 static constexpr bool WIDE_COLOR_SUPPORTED = false;
750
751 static void injectConfigChange(DisplayTransactionTest* test) {
752 test->mFlinger.mutableHasWideColorDisplay() = false;
Peiyong Lin13effd12018-07-24 17:01:47 -0700753 test->mFlinger.mutableUseColorManagement() = false;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800754 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800755 }
756
757 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
758 EXPECT_CALL(*test->mComposer, getColorModes(_, _)).Times(0);
759 EXPECT_CALL(*test->mComposer, getRenderIntents(_, _, _)).Times(0);
760 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
761 }
762};
763
764// For this variant, SurfaceFlinger should configure itself with wide display
765// support, and the display should respond with an non-empty list of supported
766// color modes. Wide-color support should be configured.
767template <typename Display>
768struct WideColorP3ColorimetricSupportedVariant {
769 static constexpr bool WIDE_COLOR_SUPPORTED = true;
770
771 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700772 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800773 test->mFlinger.mutableHasWideColorDisplay() = true;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800774 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800775 }
776
777 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800778 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_DATASPACE)).Times(1);
779
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800780 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
781 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>({ColorMode::DISPLAY_P3})),
782 Return(Error::NONE)));
783 EXPECT_CALL(*test->mComposer,
784 getRenderIntents(Display::HWC_DISPLAY_ID, ColorMode::DISPLAY_P3, _))
785 .WillOnce(DoAll(SetArgPointee<2>(
786 std::vector<RenderIntent>({RenderIntent::COLORIMETRIC})),
787 Return(Error::NONE)));
788 EXPECT_CALL(*test->mComposer,
789 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::SRGB,
790 RenderIntent::COLORIMETRIC))
791 .WillOnce(Return(Error::NONE));
792 }
793};
794
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800795// For this variant, SurfaceFlinger should configure itself with wide display
796// support, but the display should respond with an empty list of supported color
797// modes. Wide-color support for the display should not be configured.
798template <typename Display>
799struct WideColorNotSupportedVariant {
800 static constexpr bool WIDE_COLOR_SUPPORTED = false;
801
802 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700803 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800804 test->mFlinger.mutableHasWideColorDisplay() = true;
805 }
806
807 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
808 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
809 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>()), Return(Error::NONE)));
Chia-I Wu614e1422018-05-23 02:17:03 -0700810 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800811 }
812};
813
814// For this variant, the display is not a HWC display, so no HDR support should
815// be configured.
816struct NonHwcDisplayHdrSupportVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800817 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800818 static constexpr bool HDR10_SUPPORTED = false;
819 static constexpr bool HDR_HLG_SUPPORTED = false;
820 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
821 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
822 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
823 }
824};
825
Valerie Haue9e843a2018-12-18 13:39:23 -0800826template <typename Display>
827struct Hdr10PlusSupportedVariant {
828 static constexpr bool HDR10_PLUS_SUPPORTED = true;
829 static constexpr bool HDR10_SUPPORTED = true;
830 static constexpr bool HDR_HLG_SUPPORTED = false;
831 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
832 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
833 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _))
834 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({
835 Hdr::HDR10_PLUS,
836 Hdr::HDR10,
837 })),
838 Return(Error::NONE)));
839 }
840};
841
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800842// For this variant, the composer should respond with a non-empty list of HDR
843// modes containing HDR10, so HDR10 support should be configured.
844template <typename Display>
845struct Hdr10SupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800846 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800847 static constexpr bool HDR10_SUPPORTED = true;
848 static constexpr bool HDR_HLG_SUPPORTED = false;
849 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
850 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
851 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
852 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HDR10})),
853 Return(Error::NONE)));
854 }
855};
856
857// For this variant, the composer should respond with a non-empty list of HDR
858// modes containing HLG, so HLG support should be configured.
859template <typename Display>
860struct HdrHlgSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800861 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800862 static constexpr bool HDR10_SUPPORTED = false;
863 static constexpr bool HDR_HLG_SUPPORTED = true;
864 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
865 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
866 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
867 .WillOnce(
868 DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HLG})), Return(Error::NONE)));
869 }
870};
871
872// For this variant, the composer should respond with a non-empty list of HDR
873// modes containing DOLBY_VISION, so DOLBY_VISION support should be configured.
874template <typename Display>
875struct HdrDolbyVisionSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800876 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800877 static constexpr bool HDR10_SUPPORTED = false;
878 static constexpr bool HDR_HLG_SUPPORTED = false;
879 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = true;
880 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
881 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
882 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::DOLBY_VISION})),
883 Return(Error::NONE)));
884 }
885};
886
887// For this variant, the composer should respond with am empty list of HDR
888// modes, so no HDR support should be configured.
889template <typename Display>
890struct HdrNotSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800891 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800892 static constexpr bool HDR10_SUPPORTED = false;
893 static constexpr bool HDR_HLG_SUPPORTED = false;
894 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
895 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
896 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
897 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
898 }
899};
900
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700901struct NonHwcPerFrameMetadataSupportVariant {
902 static constexpr int PER_FRAME_METADATA_KEYS = 0;
903 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800904 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(_)).Times(0);
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700905 }
906};
907
908template <typename Display>
909struct NoPerFrameMetadataSupportVariant {
910 static constexpr int PER_FRAME_METADATA_KEYS = 0;
911 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800912 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
913 .WillOnce(Return(std::vector<PerFrameMetadataKey>()));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700914 }
915};
916
917template <typename Display>
918struct Smpte2086PerFrameMetadataSupportVariant {
919 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::SMPTE2086;
920 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800921 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
922 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Valerie Haue9e843a2018-12-18 13:39:23 -0800923 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X,
924 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y,
925 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X,
926 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y,
927 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X,
928 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y,
929 PerFrameMetadataKey::WHITE_POINT_X,
930 PerFrameMetadataKey::WHITE_POINT_Y,
931 PerFrameMetadataKey::MAX_LUMINANCE,
932 PerFrameMetadataKey::MIN_LUMINANCE,
933 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700934 }
935};
936
937template <typename Display>
938struct Cta861_3_PerFrameMetadataSupportVariant {
939 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::CTA861_3;
940 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800941 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
942 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Valerie Haue9e843a2018-12-18 13:39:23 -0800943 PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL,
944 PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL,
945 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700946 }
947};
948
Valerie Haue9e843a2018-12-18 13:39:23 -0800949template <typename Display>
950struct Hdr10_Plus_PerFrameMetadataSupportVariant {
951 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::HDR10PLUS;
952 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
953 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
954 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
955 PerFrameMetadataKey::HDR10_PLUS_SEI,
956 })));
957 }
958};
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800959/* ------------------------------------------------------------------------
960 * Typical display configurations to test
961 */
962
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700963template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy,
964 typename PerFrameMetadataSupportPolicy>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800965struct Case {
966 using Display = DisplayPolicy;
967 using WideColorSupport = WideColorSupportPolicy;
968 using HdrSupport = HdrSupportPolicy;
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700969 using PerFrameMetadataSupport = PerFrameMetadataSupportPolicy;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800970};
971
972using SimplePrimaryDisplayCase =
973 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700974 HdrNotSupportedVariant<PrimaryDisplayVariant>,
975 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800976using SimpleExternalDisplayCase =
977 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700978 HdrNotSupportedVariant<ExternalDisplayVariant>,
979 NoPerFrameMetadataSupportVariant<ExternalDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800980using SimpleTertiaryDisplayCase =
981 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700982 HdrNotSupportedVariant<TertiaryDisplayVariant>,
983 NoPerFrameMetadataSupportVariant<TertiaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800984using NonHwcVirtualDisplayCase =
985 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700986 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant,
987 NonHwcPerFrameMetadataSupportVariant>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800988using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
989using HwcVirtualDisplayCase =
990 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
Lloyd Pique438e9e72018-09-04 18:06:08 -0700991 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>,
tangrobin6753a022018-08-10 10:58:54 +0800992 NoPerFrameMetadataSupportVariant<SimpleHwcVirtualDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800993using WideColorP3ColorimetricDisplayCase =
994 Case<PrimaryDisplayVariant, WideColorP3ColorimetricSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700995 HdrNotSupportedVariant<PrimaryDisplayVariant>,
996 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Valerie Haue9e843a2018-12-18 13:39:23 -0800997using Hdr10PlusDisplayCase =
998 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
999 Hdr10SupportedVariant<PrimaryDisplayVariant>,
1000 Hdr10_Plus_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001001using Hdr10DisplayCase =
1002 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001003 Hdr10SupportedVariant<PrimaryDisplayVariant>,
1004 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001005using HdrHlgDisplayCase =
1006 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001007 HdrHlgSupportedVariant<PrimaryDisplayVariant>,
1008 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001009using HdrDolbyVisionDisplayCase =
1010 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001011 HdrDolbyVisionSupportedVariant<PrimaryDisplayVariant>,
1012 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
1013using HdrSmpte2086DisplayCase =
1014 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
1015 HdrNotSupportedVariant<PrimaryDisplayVariant>,
1016 Smpte2086PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
1017using HdrCta861_3_DisplayCase =
1018 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
1019 HdrNotSupportedVariant<PrimaryDisplayVariant>,
1020 Cta861_3_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Dominik Laskowskif07b85b2018-06-11 12:49:15 -07001021
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001022/* ------------------------------------------------------------------------
Lloyd Pique6cf11032018-01-22 18:57:44 -08001023 *
1024 * SurfaceFlinger::onHotplugReceived
1025 */
1026
1027TEST_F(DisplayTransactionTest, hotplugEnqueuesEventsForDisplayTransaction) {
1028 constexpr int currentSequenceId = 123;
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001029 constexpr hwc2_display_t hwcDisplayId1 = 456;
1030 constexpr hwc2_display_t hwcDisplayId2 = 654;
Lloyd Pique6cf11032018-01-22 18:57:44 -08001031
1032 // --------------------------------------------------------------------
1033 // Preconditions
1034
1035 // Set the current sequence id for accepted events
1036 mFlinger.mutableComposerSequenceId() = currentSequenceId;
1037
1038 // Set the main thread id so that the current thread does not appear to be
1039 // the main thread.
1040 mFlinger.mutableMainThreadId() = std::thread::id();
1041
1042 // --------------------------------------------------------------------
1043 // Call Expectations
1044
1045 // We expect invalidate() to be invoked once to trigger display transaction
1046 // processing.
1047 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1048
1049 // --------------------------------------------------------------------
1050 // Invocation
1051
1052 // Simulate two hotplug events (a connect and a disconnect)
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001053 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId1, HWC2::Connection::Connected);
1054 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId2, HWC2::Connection::Disconnected);
Lloyd Pique6cf11032018-01-22 18:57:44 -08001055
1056 // --------------------------------------------------------------------
1057 // Postconditions
1058
1059 // The display transaction needed flag should be set.
1060 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1061
1062 // All events should be in the pending event queue.
1063 const auto& pendingEvents = mFlinger.mutablePendingHotplugEvents();
1064 ASSERT_EQ(2u, pendingEvents.size());
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001065 EXPECT_EQ(hwcDisplayId1, pendingEvents[0].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -08001066 EXPECT_EQ(HWC2::Connection::Connected, pendingEvents[0].connection);
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001067 EXPECT_EQ(hwcDisplayId2, pendingEvents[1].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -08001068 EXPECT_EQ(HWC2::Connection::Disconnected, pendingEvents[1].connection);
1069}
1070
1071TEST_F(DisplayTransactionTest, hotplugDiscardsUnexpectedEvents) {
1072 constexpr int currentSequenceId = 123;
1073 constexpr int otherSequenceId = 321;
1074 constexpr hwc2_display_t displayId = 456;
1075
1076 // --------------------------------------------------------------------
1077 // Preconditions
1078
1079 // Set the current sequence id for accepted events
1080 mFlinger.mutableComposerSequenceId() = currentSequenceId;
1081
1082 // Set the main thread id so that the current thread does not appear to be
1083 // the main thread.
1084 mFlinger.mutableMainThreadId() = std::thread::id();
1085
1086 // --------------------------------------------------------------------
1087 // Call Expectations
1088
1089 // We do not expect any calls to invalidate().
1090 EXPECT_CALL(*mMessageQueue, invalidate()).Times(0);
1091
1092 // --------------------------------------------------------------------
1093 // Invocation
1094
1095 // Call with an unexpected sequence id
1096 mFlinger.onHotplugReceived(otherSequenceId, displayId, HWC2::Connection::Invalid);
1097
1098 // --------------------------------------------------------------------
1099 // Postconditions
1100
1101 // The display transaction needed flag should not be set
1102 EXPECT_FALSE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1103
1104 // There should be no pending events
1105 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
1106}
1107
1108TEST_F(DisplayTransactionTest, hotplugProcessesEnqueuedEventsIfCalledOnMainThread) {
1109 constexpr int currentSequenceId = 123;
1110 constexpr hwc2_display_t displayId1 = 456;
1111
1112 // --------------------------------------------------------------------
1113 // Note:
1114 // --------------------------------------------------------------------
1115 // This test case is a bit tricky. We want to verify that
1116 // onHotplugReceived() calls processDisplayHotplugEventsLocked(), but we
1117 // don't really want to provide coverage for everything the later function
1118 // does as there are specific tests for it.
1119 // --------------------------------------------------------------------
1120
1121 // --------------------------------------------------------------------
1122 // Preconditions
1123
1124 // Set the current sequence id for accepted events
1125 mFlinger.mutableComposerSequenceId() = currentSequenceId;
1126
1127 // Set the main thread id so that the current thread does appear to be the
1128 // main thread.
1129 mFlinger.mutableMainThreadId() = std::this_thread::get_id();
1130
1131 // --------------------------------------------------------------------
1132 // Call Expectations
1133
1134 // We expect invalidate() to be invoked once to trigger display transaction
1135 // processing.
1136 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1137
1138 // --------------------------------------------------------------------
1139 // Invocation
1140
1141 // Simulate a disconnect on a display id that is not connected. This should
1142 // be enqueued by onHotplugReceived(), and dequeued by
1143 // processDisplayHotplugEventsLocked(), but then ignored as invalid.
1144 mFlinger.onHotplugReceived(currentSequenceId, displayId1, HWC2::Connection::Disconnected);
1145
1146 // --------------------------------------------------------------------
1147 // Postconditions
1148
1149 // The display transaction needed flag should be set.
1150 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1151
1152 // There should be no event queued on return, as it should have been
1153 // processed.
1154 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
1155}
1156
1157/* ------------------------------------------------------------------------
Lloyd Piquea482f992018-01-22 19:00:34 -08001158 * SurfaceFlinger::createDisplay
1159 */
1160
1161TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForNonsecureDisplay) {
1162 const String8 name("virtual.test");
1163
1164 // --------------------------------------------------------------------
1165 // Call Expectations
1166
1167 // The call should notify the interceptor that a display was created.
1168 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
1169
1170 // --------------------------------------------------------------------
1171 // Invocation
1172
1173 sp<IBinder> displayToken = mFlinger.createDisplay(name, false);
1174
1175 // --------------------------------------------------------------------
1176 // Postconditions
1177
1178 // The display should have been added to the current state
1179 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1180 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001181 EXPECT_TRUE(display.isVirtual());
1182 EXPECT_FALSE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -08001183 EXPECT_EQ(name.string(), display.displayName);
1184
1185 // --------------------------------------------------------------------
1186 // Cleanup conditions
1187
1188 // Destroying the display invalidates the display state.
1189 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1190}
1191
1192TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForSecureDisplay) {
1193 const String8 name("virtual.test");
1194
1195 // --------------------------------------------------------------------
1196 // Call Expectations
1197
1198 // The call should notify the interceptor that a display was created.
1199 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
1200
1201 // --------------------------------------------------------------------
1202 // Invocation
1203
1204 sp<IBinder> displayToken = mFlinger.createDisplay(name, true);
1205
1206 // --------------------------------------------------------------------
1207 // Postconditions
1208
1209 // The display should have been added to the current state
1210 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1211 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001212 EXPECT_TRUE(display.isVirtual());
1213 EXPECT_TRUE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -08001214 EXPECT_EQ(name.string(), display.displayName);
1215
1216 // --------------------------------------------------------------------
1217 // Cleanup conditions
1218
1219 // Destroying the display invalidates the display state.
1220 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1221}
1222
1223/* ------------------------------------------------------------------------
1224 * SurfaceFlinger::destroyDisplay
1225 */
1226
1227TEST_F(DisplayTransactionTest, destroyDisplayClearsCurrentStateForDisplay) {
1228 using Case = NonHwcVirtualDisplayCase;
1229
1230 // --------------------------------------------------------------------
1231 // Preconditions
1232
1233 // A virtual display exists
1234 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1235 existing.inject();
1236
1237 // --------------------------------------------------------------------
1238 // Call Expectations
1239
1240 // The call should notify the interceptor that a display was created.
1241 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
1242
1243 // Destroying the display invalidates the display state.
1244 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1245
1246 // --------------------------------------------------------------------
1247 // Invocation
1248
1249 mFlinger.destroyDisplay(existing.token());
1250
1251 // --------------------------------------------------------------------
1252 // Postconditions
1253
1254 // The display should have been removed from the current state
1255 EXPECT_FALSE(hasCurrentDisplayState(existing.token()));
1256
1257 // Ths display should still exist in the drawing state
1258 EXPECT_TRUE(hasDrawingDisplayState(existing.token()));
1259
1260 // The display transaction needed flasg should be set
1261 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1262}
1263
1264TEST_F(DisplayTransactionTest, destroyDisplayHandlesUnknownDisplay) {
1265 // --------------------------------------------------------------------
1266 // Preconditions
1267
1268 sp<BBinder> displayToken = new BBinder();
1269
1270 // --------------------------------------------------------------------
1271 // Invocation
1272
1273 mFlinger.destroyDisplay(displayToken);
1274}
1275
1276/* ------------------------------------------------------------------------
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001277 * SurfaceFlinger::resetDisplayState
1278 */
1279
1280TEST_F(DisplayTransactionTest, resetDisplayStateClearsState) {
1281 using Case = NonHwcVirtualDisplayCase;
1282
1283 // --------------------------------------------------------------------
1284 // Preconditions
1285
1286 // vsync is enabled and available
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -07001287 mFlinger.scheduler()->mutablePrimaryHWVsyncEnabled() = true;
1288 mFlinger.scheduler()->mutableHWVsyncAvailable() = true;
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001289
1290 // A display exists
1291 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1292 existing.inject();
1293
1294 // --------------------------------------------------------------------
1295 // Call Expectations
1296
1297 // The call disable vsyncs
1298 EXPECT_CALL(*mEventControlThread, setVsyncEnabled(false)).Times(1);
1299
Lloyd Pique41be5d22018-06-21 13:11:48 -07001300 // The call ends any display resyncs
1301 EXPECT_CALL(*mPrimaryDispSync, endResync()).Times(1);
1302
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001303 // --------------------------------------------------------------------
1304 // Invocation
1305
1306 mFlinger.resetDisplayState();
1307
1308 // --------------------------------------------------------------------
1309 // Postconditions
1310
1311 // vsyncs should be off and not available.
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -07001312 EXPECT_FALSE(mFlinger.scheduler()->mutablePrimaryHWVsyncEnabled());
1313 EXPECT_FALSE(mFlinger.scheduler()->mutableHWVsyncAvailable());
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001314
1315 // The display should have been removed from the display map.
1316 EXPECT_FALSE(hasDisplayDevice(existing.token()));
1317
1318 // The display should still exist in the current state
1319 EXPECT_TRUE(hasCurrentDisplayState(existing.token()));
1320
1321 // The display should have been removed from the drawing state
1322 EXPECT_FALSE(hasDrawingDisplayState(existing.token()));
1323}
1324
1325/* ------------------------------------------------------------------------
Valerie Hau9758ae02018-10-09 16:05:09 -07001326 * DisplayDevice::GetBestColorMode
1327 */
1328class GetBestColorModeTest : public DisplayTransactionTest {
1329public:
Valerie Hau9758ae02018-10-09 16:05:09 -07001330 void setHasWideColorGamut(bool hasWideColorGamut) { mHasWideColorGamut = hasWideColorGamut; }
1331
1332 void addHwcColorModesMapping(ui::ColorMode colorMode,
1333 std::vector<ui::RenderIntent> renderIntents) {
1334 mHwcColorModes[colorMode] = renderIntents;
1335 }
1336
1337 void setInputDataspace(ui::Dataspace dataspace) { mInputDataspace = dataspace; }
1338
1339 void setInputRenderIntent(ui::RenderIntent renderIntent) { mInputRenderIntent = renderIntent; }
1340
1341 void getBestColorMode() {
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -07001342 auto displayDevice =
1343 injectDefaultInternalDisplay([this](FakeDisplayDeviceInjector& injector) {
1344 injector.setHwcColorModes(mHwcColorModes);
1345 injector.setHasWideColorGamut(mHasWideColorGamut);
1346 injector.setNativeWindow(mNativeWindow);
1347 });
Valerie Hau9758ae02018-10-09 16:05:09 -07001348
Lloyd Pique3d0c02e2018-10-19 18:38:12 -07001349 displayDevice->getCompositionDisplay()
1350 ->getDisplayColorProfile()
1351 ->getBestColorMode(mInputDataspace, mInputRenderIntent, &mOutDataspace,
1352 &mOutColorMode, &mOutRenderIntent);
Valerie Hau9758ae02018-10-09 16:05:09 -07001353 }
1354
1355 ui::Dataspace mOutDataspace;
1356 ui::ColorMode mOutColorMode;
1357 ui::RenderIntent mOutRenderIntent;
1358
1359private:
1360 ui::Dataspace mInputDataspace;
1361 ui::RenderIntent mInputRenderIntent;
1362 bool mHasWideColorGamut = false;
1363 std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> mHwcColorModes;
Valerie Hau9758ae02018-10-09 16:05:09 -07001364};
1365
1366TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeSRGB) {
1367 addHwcColorModesMapping(ui::ColorMode::SRGB,
1368 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1369 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1370 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1371 setHasWideColorGamut(true);
1372
1373 getBestColorMode();
1374
Peiyong Lin14724e62018-12-05 07:27:30 -08001375 ASSERT_EQ(ui::Dataspace::V0_SRGB, mOutDataspace);
Valerie Hau9758ae02018-10-09 16:05:09 -07001376 ASSERT_EQ(ui::ColorMode::SRGB, mOutColorMode);
1377 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1378}
1379
1380TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDisplayP3) {
1381 addHwcColorModesMapping(ui::ColorMode::DISPLAY_P3,
1382 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1383 addHwcColorModesMapping(ui::ColorMode::SRGB,
1384 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1385 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1386 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1387 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1388 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1389 setHasWideColorGamut(true);
1390
1391 getBestColorMode();
1392
1393 ASSERT_EQ(ui::Dataspace::DISPLAY_P3, mOutDataspace);
1394 ASSERT_EQ(ui::ColorMode::DISPLAY_P3, mOutColorMode);
1395 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1396}
1397
1398TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDISPLAY_BT2020) {
1399 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1400 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1401 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1402 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1403 setHasWideColorGamut(true);
1404
1405 getBestColorMode();
1406
1407 ASSERT_EQ(ui::Dataspace::DISPLAY_BT2020, mOutDataspace);
1408 ASSERT_EQ(ui::ColorMode::DISPLAY_BT2020, mOutColorMode);
1409 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1410}
1411
1412/* ------------------------------------------------------------------------
Lloyd Pique33050472019-12-19 17:12:44 -08001413 * DisplayDevice::setProjection
1414 */
1415
1416class DisplayDeviceSetProjectionTest : public DisplayTransactionTest {
1417public:
Lloyd Pique33050472019-12-19 17:12:44 -08001418 static constexpr int32_t DEFAULT_DISPLAY_WIDTH = 1080; // arbitrary
1419 static constexpr int32_t DEFAULT_DISPLAY_HEIGHT = 1920; // arbitrary
1420
1421 static constexpr int32_t TRANSFORM_FLAGS_ROT_0 = 0;
1422 static constexpr int32_t TRANSFORM_FLAGS_ROT_90 = HAL_TRANSFORM_ROT_90;
1423 static constexpr int32_t TRANSFORM_FLAGS_ROT_180 = HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_FLIP_V;
1424 static constexpr int32_t TRANSFORM_FLAGS_ROT_270 =
1425 HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
1426
1427 DisplayDeviceSetProjectionTest(ui::Size flingerDisplaySize, ui::Size hardwareDisplaySize,
1428 ui::Rotation physicalOrientation)
1429 : mFlingerDisplaySize(flingerDisplaySize),
1430 mHardwareDisplaySize(hardwareDisplaySize),
1431 mPhysicalOrientation(physicalOrientation),
1432 mDisplayDevice(createDisplayDevice()) {}
1433
1434 sp<DisplayDevice> createDisplayDevice() {
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -07001435 return injectDefaultInternalDisplay([this](FakeDisplayDeviceInjector& injector) {
1436 injector.setPhysicalOrientation(mPhysicalOrientation);
1437 });
Lloyd Pique33050472019-12-19 17:12:44 -08001438 }
1439
1440 ui::Size SwapWH(const ui::Size size) const { return ui::Size(size.height, size.width); }
1441
1442 void setProjectionForRotation0() {
1443 // A logical rotation of 0 uses the SurfaceFlinger display size
1444 mDisplayDevice->setProjection(ui::ROTATION_0, Rect(mFlingerDisplaySize),
1445 Rect(mFlingerDisplaySize));
1446 }
1447
1448 void setProjectionForRotation90() {
1449 // A logical rotation of 90 uses the SurfaceFlinger display size with
1450 // the width/height swapped.
1451 mDisplayDevice->setProjection(ui::ROTATION_90, Rect(SwapWH(mFlingerDisplaySize)),
1452 Rect(SwapWH(mFlingerDisplaySize)));
1453 }
1454
1455 void setProjectionForRotation180() {
1456 // A logical rotation of 180 uses the SurfaceFlinger display size
1457 mDisplayDevice->setProjection(ui::ROTATION_180, Rect(mFlingerDisplaySize),
1458 Rect(mFlingerDisplaySize));
1459 }
1460
1461 void setProjectionForRotation270() {
1462 // A logical rotation of 270 uses the SurfaceFlinger display size with
1463 // the width/height swapped.
1464 mDisplayDevice->setProjection(ui::ROTATION_270, Rect(SwapWH(mFlingerDisplaySize)),
1465 Rect(SwapWH(mFlingerDisplaySize)));
1466 }
1467
1468 void expectStateForHardwareTransform0() {
1469 const auto& compositionState = mDisplayDevice->getCompositionDisplay()->getState();
1470 EXPECT_EQ(ui::Transform(TRANSFORM_FLAGS_ROT_0, mHardwareDisplaySize.width,
1471 mHardwareDisplaySize.height),
1472 compositionState.transform);
1473 EXPECT_EQ(TRANSFORM_FLAGS_ROT_0, compositionState.orientation);
Lloyd Piquee8fe4742020-01-21 15:26:18 -08001474 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.sourceClip);
1475 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.destinationClip);
Lloyd Pique33050472019-12-19 17:12:44 -08001476 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.frame);
1477 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.viewport);
1478 EXPECT_EQ(false, compositionState.needsFiltering);
1479 }
1480
1481 void expectStateForHardwareTransform90() {
1482 const auto& compositionState = mDisplayDevice->getCompositionDisplay()->getState();
1483 EXPECT_EQ(ui::Transform(TRANSFORM_FLAGS_ROT_90, mHardwareDisplaySize.width,
1484 mHardwareDisplaySize.height),
1485 compositionState.transform);
1486 EXPECT_EQ(TRANSFORM_FLAGS_ROT_90, compositionState.orientation);
Lloyd Piquee8fe4742020-01-21 15:26:18 -08001487 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.sourceClip);
1488 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.destinationClip);
Lloyd Pique33050472019-12-19 17:12:44 -08001489 // For 90, the frame and viewport have the hardware display size width and height swapped
1490 EXPECT_EQ(Rect(SwapWH(mHardwareDisplaySize)), compositionState.frame);
1491 EXPECT_EQ(Rect(SwapWH(mHardwareDisplaySize)), compositionState.viewport);
1492 EXPECT_EQ(false, compositionState.needsFiltering);
1493 }
1494
1495 void expectStateForHardwareTransform180() {
1496 const auto& compositionState = mDisplayDevice->getCompositionDisplay()->getState();
1497 EXPECT_EQ(ui::Transform(TRANSFORM_FLAGS_ROT_180, mHardwareDisplaySize.width,
1498 mHardwareDisplaySize.height),
1499 compositionState.transform);
1500 EXPECT_EQ(TRANSFORM_FLAGS_ROT_180, compositionState.orientation);
Lloyd Piquee8fe4742020-01-21 15:26:18 -08001501 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.sourceClip);
1502 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.destinationClip);
Lloyd Pique33050472019-12-19 17:12:44 -08001503 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.frame);
1504 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.viewport);
1505 EXPECT_EQ(false, compositionState.needsFiltering);
1506 }
1507
1508 void expectStateForHardwareTransform270() {
1509 const auto& compositionState = mDisplayDevice->getCompositionDisplay()->getState();
1510 EXPECT_EQ(ui::Transform(TRANSFORM_FLAGS_ROT_270, mHardwareDisplaySize.width,
1511 mHardwareDisplaySize.height),
1512 compositionState.transform);
1513 EXPECT_EQ(TRANSFORM_FLAGS_ROT_270, compositionState.orientation);
Lloyd Piquee8fe4742020-01-21 15:26:18 -08001514 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.sourceClip);
1515 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.destinationClip);
Lloyd Pique33050472019-12-19 17:12:44 -08001516 // For 270, the frame and viewport have the hardware display size width and height swapped
1517 EXPECT_EQ(Rect(SwapWH(mHardwareDisplaySize)), compositionState.frame);
1518 EXPECT_EQ(Rect(SwapWH(mHardwareDisplaySize)), compositionState.viewport);
1519 EXPECT_EQ(false, compositionState.needsFiltering);
1520 }
1521
1522 const ui::Size mFlingerDisplaySize;
1523 const ui::Size mHardwareDisplaySize;
1524 const ui::Rotation mPhysicalOrientation;
1525 const sp<DisplayDevice> mDisplayDevice;
1526};
1527
1528struct DisplayDeviceSetProjectionTest_Installed0 : public DisplayDeviceSetProjectionTest {
1529 DisplayDeviceSetProjectionTest_Installed0()
1530 : DisplayDeviceSetProjectionTest(ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
1531 ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
1532 ui::ROTATION_0) {}
1533};
1534
1535TEST_F(DisplayDeviceSetProjectionTest_Installed0, checkWith0OutputRotation) {
1536 setProjectionForRotation0();
1537 expectStateForHardwareTransform0();
1538}
1539
1540TEST_F(DisplayDeviceSetProjectionTest_Installed0, checkWith90OutputRotation) {
1541 setProjectionForRotation90();
1542 expectStateForHardwareTransform90();
1543}
1544
1545TEST_F(DisplayDeviceSetProjectionTest_Installed0, checkWith180OutputRotation) {
1546 setProjectionForRotation180();
1547 expectStateForHardwareTransform180();
1548}
1549
1550TEST_F(DisplayDeviceSetProjectionTest_Installed0, checkWith270OutputRotation) {
1551 setProjectionForRotation270();
1552 expectStateForHardwareTransform270();
1553}
1554
1555struct DisplayDeviceSetProjectionTest_Installed90 : public DisplayDeviceSetProjectionTest {
1556 DisplayDeviceSetProjectionTest_Installed90()
1557 : DisplayDeviceSetProjectionTest(ui::Size(DEFAULT_DISPLAY_HEIGHT, DEFAULT_DISPLAY_WIDTH),
1558 ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
1559 ui::ROTATION_90) {}
1560};
1561
1562TEST_F(DisplayDeviceSetProjectionTest_Installed90, checkWith0OutputRotation) {
1563 setProjectionForRotation0();
1564 expectStateForHardwareTransform90();
1565}
1566
1567TEST_F(DisplayDeviceSetProjectionTest_Installed90, checkWith90OutputRotation) {
1568 setProjectionForRotation90();
1569 expectStateForHardwareTransform180();
1570}
1571
1572TEST_F(DisplayDeviceSetProjectionTest_Installed90, checkWith180OutputRotation) {
1573 setProjectionForRotation180();
1574 expectStateForHardwareTransform270();
1575}
1576
1577TEST_F(DisplayDeviceSetProjectionTest_Installed90, checkWith270OutputRotation) {
1578 setProjectionForRotation270();
1579 expectStateForHardwareTransform0();
1580}
1581
1582struct DisplayDeviceSetProjectionTest_Installed180 : public DisplayDeviceSetProjectionTest {
1583 DisplayDeviceSetProjectionTest_Installed180()
1584 : DisplayDeviceSetProjectionTest(ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
1585 ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
1586 ui::ROTATION_180) {}
1587};
1588
1589TEST_F(DisplayDeviceSetProjectionTest_Installed180, checkWith0OutputRotation) {
1590 setProjectionForRotation0();
1591 expectStateForHardwareTransform180();
1592}
1593
1594TEST_F(DisplayDeviceSetProjectionTest_Installed180, checkWith90OutputRotation) {
1595 setProjectionForRotation90();
1596 expectStateForHardwareTransform270();
1597}
1598
1599TEST_F(DisplayDeviceSetProjectionTest_Installed180, checkWith180OutputRotation) {
1600 setProjectionForRotation180();
1601 expectStateForHardwareTransform0();
1602}
1603
1604TEST_F(DisplayDeviceSetProjectionTest_Installed180, checkWith270OutputRotation) {
1605 setProjectionForRotation270();
1606 expectStateForHardwareTransform90();
1607}
1608
1609struct DisplayDeviceSetProjectionTest_Installed270 : public DisplayDeviceSetProjectionTest {
1610 DisplayDeviceSetProjectionTest_Installed270()
1611 : DisplayDeviceSetProjectionTest(ui::Size(DEFAULT_DISPLAY_HEIGHT, DEFAULT_DISPLAY_WIDTH),
1612 ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
1613 ui::ROTATION_270) {}
1614};
1615
1616TEST_F(DisplayDeviceSetProjectionTest_Installed270, checkWith0OutputRotation) {
1617 setProjectionForRotation0();
1618 expectStateForHardwareTransform270();
1619}
1620
1621TEST_F(DisplayDeviceSetProjectionTest_Installed270, checkWith90OutputRotation) {
1622 setProjectionForRotation90();
1623 expectStateForHardwareTransform0();
1624}
1625
1626TEST_F(DisplayDeviceSetProjectionTest_Installed270, checkWith180OutputRotation) {
1627 setProjectionForRotation180();
1628 expectStateForHardwareTransform90();
1629}
1630
1631TEST_F(DisplayDeviceSetProjectionTest_Installed270, checkWith270OutputRotation) {
1632 setProjectionForRotation270();
1633 expectStateForHardwareTransform180();
1634}
1635
1636/* ------------------------------------------------------------------------
Daniel Solomon42d04562019-01-20 21:03:19 -08001637 * SurfaceFlinger::getDisplayNativePrimaries
1638 */
1639
1640class GetDisplayNativePrimaries : public DisplayTransactionTest {
1641public:
1642 GetDisplayNativePrimaries();
1643 void populateDummyDisplayNativePrimaries(ui::DisplayPrimaries& primaries);
1644 void checkDummyDisplayNativePrimaries(const ui::DisplayPrimaries& primaries);
1645
1646private:
1647 static constexpr float mStartingTestValue = 1.0f;
1648};
1649
1650GetDisplayNativePrimaries::GetDisplayNativePrimaries() {
1651 SimplePrimaryDisplayCase::Display::injectHwcDisplay(this);
1652 injectFakeNativeWindowSurfaceFactory();
1653}
1654
1655void GetDisplayNativePrimaries::populateDummyDisplayNativePrimaries(
1656 ui::DisplayPrimaries& primaries) {
1657 float startingVal = mStartingTestValue;
1658 primaries.red.X = startingVal++;
1659 primaries.red.Y = startingVal++;
1660 primaries.red.Z = startingVal++;
1661 primaries.green.X = startingVal++;
1662 primaries.green.Y = startingVal++;
1663 primaries.green.Z = startingVal++;
1664 primaries.blue.X = startingVal++;
1665 primaries.blue.Y = startingVal++;
1666 primaries.blue.Z = startingVal++;
1667 primaries.white.X = startingVal++;
1668 primaries.white.Y = startingVal++;
1669 primaries.white.Z = startingVal++;
1670}
1671
1672void GetDisplayNativePrimaries::checkDummyDisplayNativePrimaries(
1673 const ui::DisplayPrimaries& primaries) {
1674 float startingVal = mStartingTestValue;
1675 EXPECT_EQ(primaries.red.X, startingVal++);
1676 EXPECT_EQ(primaries.red.Y, startingVal++);
1677 EXPECT_EQ(primaries.red.Z, startingVal++);
1678 EXPECT_EQ(primaries.green.X, startingVal++);
1679 EXPECT_EQ(primaries.green.Y, startingVal++);
1680 EXPECT_EQ(primaries.green.Z, startingVal++);
1681 EXPECT_EQ(primaries.blue.X, startingVal++);
1682 EXPECT_EQ(primaries.blue.Y, startingVal++);
1683 EXPECT_EQ(primaries.blue.Z, startingVal++);
1684 EXPECT_EQ(primaries.white.X, startingVal++);
1685 EXPECT_EQ(primaries.white.Y, startingVal++);
1686 EXPECT_EQ(primaries.white.Z, startingVal++);
1687}
1688
1689TEST_F(GetDisplayNativePrimaries, nullDisplayToken) {
1690 ui::DisplayPrimaries primaries;
1691 EXPECT_EQ(BAD_VALUE, mFlinger.getDisplayNativePrimaries(nullptr, primaries));
1692}
1693
Daniel Solomon42d04562019-01-20 21:03:19 -08001694TEST_F(GetDisplayNativePrimaries, internalDisplayWithPrimariesData) {
1695 auto injector = SimplePrimaryDisplayCase::Display::makeFakeExistingDisplayInjector(this);
1696 injector.inject();
1697 auto internalDisplayToken = injector.token();
1698
1699 ui::DisplayPrimaries expectedPrimaries;
1700 populateDummyDisplayNativePrimaries(expectedPrimaries);
1701 mFlinger.setInternalDisplayPrimaries(expectedPrimaries);
1702
1703 ui::DisplayPrimaries primaries;
1704 EXPECT_EQ(NO_ERROR, mFlinger.getDisplayNativePrimaries(internalDisplayToken, primaries));
1705
1706 checkDummyDisplayNativePrimaries(primaries);
1707}
1708
1709TEST_F(GetDisplayNativePrimaries, notInternalDisplayToken) {
1710 sp<BBinder> notInternalDisplayToken = new BBinder();
1711
1712 ui::DisplayPrimaries primaries;
1713 populateDummyDisplayNativePrimaries(primaries);
1714 EXPECT_EQ(BAD_VALUE, mFlinger.getDisplayNativePrimaries(notInternalDisplayToken, primaries));
1715
1716 // Check primaries argument wasn't modified in case of failure
1717 checkDummyDisplayNativePrimaries(primaries);
1718}
1719
1720/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001721 * SurfaceFlinger::setupNewDisplayDeviceInternal
1722 */
1723
1724class SetupNewDisplayDeviceInternalTest : public DisplayTransactionTest {
1725public:
1726 template <typename T>
1727 void setupNewDisplayDeviceInternalTest();
1728};
1729
1730template <typename Case>
1731void SetupNewDisplayDeviceInternalTest::setupNewDisplayDeviceInternalTest() {
1732 const sp<BBinder> displayToken = new BBinder();
Lloyd Pique542307f2018-10-19 13:24:08 -07001733 const sp<compositionengine::mock::DisplaySurface> displaySurface =
1734 new compositionengine::mock::DisplaySurface();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001735 const sp<mock::GraphicBufferProducer> producer = new mock::GraphicBufferProducer();
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001736
1737 // --------------------------------------------------------------------
1738 // Preconditions
1739
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001740 // Wide color displays support is configured appropriately
1741 Case::WideColorSupport::injectConfigChange(this);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001742
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001743 // The display is setup with the HWC.
1744 Case::Display::injectHwcDisplay(this);
1745
1746 // SurfaceFlinger will use a test-controlled factory for native window
1747 // surfaces.
1748 injectFakeNativeWindowSurfaceFactory();
1749
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -07001750 // A compositionengine::Display has already been created
1751 auto compositionDisplay = Case::Display::injectCompositionDisplay(this);
1752
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001753 // --------------------------------------------------------------------
1754 // Call Expectations
1755
1756 // Various native window calls will be made.
1757 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
Lloyd Pique3c085a02018-05-09 19:38:32 -07001758 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001759 Case::WideColorSupport::setupComposerCallExpectations(this);
1760 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001761 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001762
1763 // --------------------------------------------------------------------
1764 // Invocation
1765
1766 DisplayDeviceState state;
Dominik Laskowski55c85402020-01-21 16:25:47 -08001767 if (const auto connectionType = Case::Display::CONNECTION_TYPE::value) {
1768 const auto displayId = Case::Display::DISPLAY_ID::get();
1769 ASSERT_TRUE(displayId);
1770 state.physical = {*displayId, *connectionType};
1771 }
1772
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001773 state.isSecure = static_cast<bool>(Case::Display::SECURE);
1774
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -07001775 auto device = mFlinger.setupNewDisplayDeviceInternal(displayToken, compositionDisplay, state,
1776 displaySurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001777
1778 // --------------------------------------------------------------------
1779 // Postconditions
1780
1781 ASSERT_TRUE(device != nullptr);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001782 EXPECT_EQ(Case::Display::DISPLAY_ID::get(), device->getId());
Dominik Laskowski55c85402020-01-21 16:25:47 -08001783 EXPECT_EQ(Case::Display::CONNECTION_TYPE::value, device->getConnectionType());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001784 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), device->isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001785 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001786 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001787 EXPECT_EQ(Case::Display::WIDTH, device->getWidth());
1788 EXPECT_EQ(Case::Display::HEIGHT, device->getHeight());
1789 EXPECT_EQ(Case::WideColorSupport::WIDE_COLOR_SUPPORTED, device->hasWideColorGamut());
Valerie Haue9e843a2018-12-18 13:39:23 -08001790 EXPECT_EQ(Case::HdrSupport::HDR10_PLUS_SUPPORTED, device->hasHDR10PlusSupport());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001791 EXPECT_EQ(Case::HdrSupport::HDR10_SUPPORTED, device->hasHDR10Support());
1792 EXPECT_EQ(Case::HdrSupport::HDR_HLG_SUPPORTED, device->hasHLGSupport());
1793 EXPECT_EQ(Case::HdrSupport::HDR_DOLBY_VISION_SUPPORTED, device->hasDolbyVisionSupport());
Lloyd Pique3c085a02018-05-09 19:38:32 -07001794 // Note: This is not Case::Display::HWC_ACTIVE_CONFIG_ID as the ids are
1795 // remapped, and the test only ever sets up one config. If there were an error
1796 // looking up the remapped index, device->getActiveConfig() would be -1 instead.
Ady Abraham2139f732019-11-13 18:56:40 -08001797 EXPECT_EQ(0, device->getActiveConfig().value());
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001798 EXPECT_EQ(Case::PerFrameMetadataSupport::PER_FRAME_METADATA_KEYS,
1799 device->getSupportedPerFrameMetadata());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001800}
1801
1802TEST_F(SetupNewDisplayDeviceInternalTest, createSimplePrimaryDisplay) {
1803 setupNewDisplayDeviceInternalTest<SimplePrimaryDisplayCase>();
1804}
1805
1806TEST_F(SetupNewDisplayDeviceInternalTest, createSimpleExternalDisplay) {
1807 setupNewDisplayDeviceInternalTest<SimpleExternalDisplayCase>();
1808}
1809
1810TEST_F(SetupNewDisplayDeviceInternalTest, createNonHwcVirtualDisplay) {
1811 setupNewDisplayDeviceInternalTest<NonHwcVirtualDisplayCase>();
1812}
1813
1814TEST_F(SetupNewDisplayDeviceInternalTest, createHwcVirtualDisplay) {
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -07001815 setupNewDisplayDeviceInternalTest<HwcVirtualDisplayCase>();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001816}
1817
1818TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3Display) {
1819 setupNewDisplayDeviceInternalTest<WideColorP3ColorimetricDisplayCase>();
1820}
1821
Valerie Haue9e843a2018-12-18 13:39:23 -08001822TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10PlusDisplay) {
1823 setupNewDisplayDeviceInternalTest<Hdr10PlusDisplayCase>();
1824}
1825
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001826TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10Display) {
1827 setupNewDisplayDeviceInternalTest<Hdr10DisplayCase>();
1828}
1829
1830TEST_F(SetupNewDisplayDeviceInternalTest, createHdrHlgDisplay) {
1831 setupNewDisplayDeviceInternalTest<HdrHlgDisplayCase>();
1832}
1833
1834TEST_F(SetupNewDisplayDeviceInternalTest, createHdrDolbyVisionDisplay) {
1835 setupNewDisplayDeviceInternalTest<HdrDolbyVisionDisplayCase>();
1836}
1837
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001838TEST_F(SetupNewDisplayDeviceInternalTest, createHdrSmpte2086DisplayCase) {
1839 setupNewDisplayDeviceInternalTest<HdrSmpte2086DisplayCase>();
1840}
1841
1842TEST_F(SetupNewDisplayDeviceInternalTest, createHdrCta816_3_DisplayCase) {
1843 setupNewDisplayDeviceInternalTest<HdrCta861_3_DisplayCase>();
1844}
1845
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001846/* ------------------------------------------------------------------------
1847 * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded)
1848 */
1849
1850class HandleTransactionLockedTest : public DisplayTransactionTest {
1851public:
1852 template <typename Case>
1853 void setupCommonPreconditions();
1854
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001855 template <typename Case, bool connected>
1856 static void expectHotplugReceived(mock::EventThread*);
1857
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001858 template <typename Case>
1859 void setupCommonCallExpectationsForConnectProcessing();
1860
1861 template <typename Case>
1862 void setupCommonCallExpectationsForDisconnectProcessing();
1863
1864 template <typename Case>
1865 void processesHotplugConnectCommon();
1866
1867 template <typename Case>
1868 void ignoresHotplugConnectCommon();
1869
1870 template <typename Case>
1871 void processesHotplugDisconnectCommon();
1872
1873 template <typename Case>
1874 void verifyDisplayIsConnected(const sp<IBinder>& displayToken);
1875
1876 template <typename Case>
1877 void verifyPhysicalDisplayIsConnected();
1878
1879 void verifyDisplayIsNotConnected(const sp<IBinder>& displayToken);
1880};
1881
1882template <typename Case>
1883void HandleTransactionLockedTest::setupCommonPreconditions() {
1884 // Wide color displays support is configured appropriately
1885 Case::WideColorSupport::injectConfigChange(this);
1886
1887 // SurfaceFlinger will use a test-controlled factory for BufferQueues
1888 injectFakeBufferQueueFactory();
1889
1890 // SurfaceFlinger will use a test-controlled factory for native window
1891 // surfaces.
1892 injectFakeNativeWindowSurfaceFactory();
1893}
1894
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001895template <typename Case, bool connected>
1896void HandleTransactionLockedTest::expectHotplugReceived(mock::EventThread* eventThread) {
1897 const auto convert = [](auto physicalDisplayId) {
1898 return std::make_optional(DisplayId{physicalDisplayId});
1899 };
1900
1901 EXPECT_CALL(*eventThread,
1902 onHotplugReceived(ResultOf(convert, Case::Display::DISPLAY_ID::get()), connected))
1903 .Times(1);
1904}
1905
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001906template <typename Case>
1907void HandleTransactionLockedTest::setupCommonCallExpectationsForConnectProcessing() {
1908 Case::Display::setupHwcHotplugCallExpectations(this);
1909
1910 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1911 Case::Display::setupFramebufferProducerBufferQueueCallExpectations(this);
1912 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1913 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
1914
1915 Case::WideColorSupport::setupComposerCallExpectations(this);
1916 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001917 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001918
1919 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001920 expectHotplugReceived<Case, true>(mEventThread);
1921 expectHotplugReceived<Case, true>(mSFEventThread);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001922}
1923
1924template <typename Case>
1925void HandleTransactionLockedTest::setupCommonCallExpectationsForDisconnectProcessing() {
1926 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
Dominik Laskowski1eba0202019-01-24 09:14:40 -08001927
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001928 expectHotplugReceived<Case, false>(mEventThread);
1929 expectHotplugReceived<Case, false>(mSFEventThread);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001930}
1931
1932template <typename Case>
1933void HandleTransactionLockedTest::verifyDisplayIsConnected(const sp<IBinder>& displayToken) {
1934 // The display device should have been set up in the list of displays.
1935 ASSERT_TRUE(hasDisplayDevice(displayToken));
1936 const auto& device = getDisplayDevice(displayToken);
1937 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001938 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001939
Dominik Laskowski55c85402020-01-21 16:25:47 -08001940 std::optional<DisplayDeviceState::Physical> expectedPhysical;
1941 if (const auto connectionType = Case::Display::CONNECTION_TYPE::value) {
1942 const auto displayId = Case::Display::DISPLAY_ID::get();
1943 ASSERT_TRUE(displayId);
1944 expectedPhysical = {*displayId, *connectionType};
1945 }
1946
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001947 // The display should have been set up in the current display state
1948 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1949 const auto& current = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001950 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), current.isVirtual());
Dominik Laskowski55c85402020-01-21 16:25:47 -08001951 EXPECT_EQ(expectedPhysical, current.physical);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001952
1953 // The display should have been set up in the drawing display state
1954 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1955 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001956 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
Dominik Laskowski55c85402020-01-21 16:25:47 -08001957 EXPECT_EQ(expectedPhysical, draw.physical);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001958}
1959
1960template <typename Case>
1961void HandleTransactionLockedTest::verifyPhysicalDisplayIsConnected() {
1962 // HWComposer should have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001963 EXPECT_TRUE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001964
Dominik Laskowski075d3172018-05-24 15:50:06 -07001965 // SF should have a display token.
1966 const auto displayId = Case::Display::DISPLAY_ID::get();
1967 ASSERT_TRUE(displayId);
1968 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1969 auto& displayToken = mFlinger.mutablePhysicalDisplayTokens()[*displayId];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001970
1971 verifyDisplayIsConnected<Case>(displayToken);
1972}
1973
1974void HandleTransactionLockedTest::verifyDisplayIsNotConnected(const sp<IBinder>& displayToken) {
1975 EXPECT_FALSE(hasDisplayDevice(displayToken));
1976 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1977 EXPECT_FALSE(hasDrawingDisplayState(displayToken));
1978}
1979
1980template <typename Case>
1981void HandleTransactionLockedTest::processesHotplugConnectCommon() {
1982 // --------------------------------------------------------------------
1983 // Preconditions
1984
1985 setupCommonPreconditions<Case>();
1986
1987 // A hotplug connect event is enqueued for a display
1988 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001989
1990 // --------------------------------------------------------------------
1991 // Call Expectations
1992
1993 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillOnce(Return(false));
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001994
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001995 setupCommonCallExpectationsForConnectProcessing<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001996
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001997 // --------------------------------------------------------------------
1998 // Invocation
Lloyd Piquee39cad22017-12-20 17:01:29 -08001999
Lloyd Pique1fa4d462018-01-22 18:03:16 -08002000 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
Lloyd Piquee39cad22017-12-20 17:01:29 -08002001
Lloyd Pique1fa4d462018-01-22 18:03:16 -08002002 // --------------------------------------------------------------------
2003 // Postconditions
2004
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002005 verifyPhysicalDisplayIsConnected<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08002006
Lloyd Pique1fa4d462018-01-22 18:03:16 -08002007 // --------------------------------------------------------------------
2008 // Cleanup conditions
2009
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002010 EXPECT_CALL(*mComposer,
2011 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
Lloyd Pique1fa4d462018-01-22 18:03:16 -08002012 .WillOnce(Return(Error::NONE));
2013 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Piquef58625d2017-12-19 13:22:33 -08002014}
2015
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002016template <typename Case>
2017void HandleTransactionLockedTest::ignoresHotplugConnectCommon() {
2018 // --------------------------------------------------------------------
2019 // Preconditions
2020
2021 setupCommonPreconditions<Case>();
2022
2023 // A hotplug connect event is enqueued for a display
2024 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
2025
2026 // --------------------------------------------------------------------
2027 // Invocation
2028
2029 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2030
2031 // --------------------------------------------------------------------
2032 // Postconditions
2033
2034 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07002035 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002036}
2037
2038template <typename Case>
2039void HandleTransactionLockedTest::processesHotplugDisconnectCommon() {
2040 // --------------------------------------------------------------------
2041 // Preconditions
2042
2043 setupCommonPreconditions<Case>();
2044
2045 // A hotplug disconnect event is enqueued for a display
2046 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
2047
2048 // The display is already completely set up.
2049 Case::Display::injectHwcDisplay(this);
2050 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
2051 existing.inject();
2052
2053 // --------------------------------------------------------------------
2054 // Call Expectations
2055
2056 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
Lloyd Pique438e9e72018-09-04 18:06:08 -07002057 EXPECT_CALL(*mComposer, getDisplayIdentificationData(Case::Display::HWC_DISPLAY_ID, _, _))
2058 .Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002059
2060 setupCommonCallExpectationsForDisconnectProcessing<Case>();
2061
2062 // --------------------------------------------------------------------
2063 // Invocation
2064
2065 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2066
2067 // --------------------------------------------------------------------
2068 // Postconditions
2069
2070 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07002071 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002072
Dominik Laskowski075d3172018-05-24 15:50:06 -07002073 // SF should not have a display token.
2074 const auto displayId = Case::Display::DISPLAY_ID::get();
2075 ASSERT_TRUE(displayId);
2076 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002077
2078 // The existing token should have been removed
2079 verifyDisplayIsNotConnected(existing.token());
2080}
2081
2082TEST_F(HandleTransactionLockedTest, processesHotplugConnectPrimaryDisplay) {
2083 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
2084}
2085
2086TEST_F(HandleTransactionLockedTest,
2087 processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected) {
2088 // Inject an external display.
2089 ExternalDisplayVariant::injectHwcDisplay(this);
2090
2091 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
2092}
2093
2094TEST_F(HandleTransactionLockedTest, processesHotplugConnectExternalDisplay) {
2095 // Inject a primary display.
2096 PrimaryDisplayVariant::injectHwcDisplay(this);
2097
2098 processesHotplugConnectCommon<SimpleExternalDisplayCase>();
2099}
2100
2101TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected) {
2102 // Inject both a primary and external display.
2103 PrimaryDisplayVariant::injectHwcDisplay(this);
2104 ExternalDisplayVariant::injectHwcDisplay(this);
2105
Lloyd Pique86fa3db2019-02-04 18:46:01 -08002106 // TODO: This is an unnecessary call.
2107 EXPECT_CALL(*mComposer,
2108 getDisplayIdentificationData(TertiaryDisplayVariant::HWC_DISPLAY_ID, _, _))
2109 .WillOnce(DoAll(SetArgPointee<1>(TertiaryDisplay::PORT),
2110 SetArgPointee<2>(TertiaryDisplay::GET_IDENTIFICATION_DATA()),
2111 Return(Error::NONE)));
2112
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002113 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
2114
2115 ignoresHotplugConnectCommon<SimpleTertiaryDisplayCase>();
2116}
2117
2118TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfExternalForVrComposer) {
2119 // Inject a primary display.
2120 PrimaryDisplayVariant::injectHwcDisplay(this);
2121
2122 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(true));
2123
2124 ignoresHotplugConnectCommon<SimpleExternalDisplayCase>();
2125}
2126
2127TEST_F(HandleTransactionLockedTest, processHotplugDisconnectPrimaryDisplay) {
2128 processesHotplugDisconnectCommon<SimplePrimaryDisplayCase>();
2129}
2130
2131TEST_F(HandleTransactionLockedTest, processHotplugDisconnectExternalDisplay) {
2132 processesHotplugDisconnectCommon<SimpleExternalDisplayCase>();
2133}
2134
2135TEST_F(HandleTransactionLockedTest, processesHotplugConnectThenDisconnectPrimary) {
2136 using Case = SimplePrimaryDisplayCase;
2137
2138 // --------------------------------------------------------------------
2139 // Preconditions
2140
2141 setupCommonPreconditions<Case>();
2142
2143 // A hotplug connect event is enqueued for a display
2144 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
2145 // A hotplug disconnect event is also enqueued for the same display
2146 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
2147
2148 // --------------------------------------------------------------------
2149 // Call Expectations
2150
2151 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
2152
2153 setupCommonCallExpectationsForConnectProcessing<Case>();
2154 setupCommonCallExpectationsForDisconnectProcessing<Case>();
2155
2156 EXPECT_CALL(*mComposer,
2157 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
2158 .WillOnce(Return(Error::NONE));
2159 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
2160
2161 // --------------------------------------------------------------------
2162 // Invocation
2163
2164 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2165
2166 // --------------------------------------------------------------------
2167 // Postconditions
2168
2169 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07002170 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002171
Dominik Laskowski075d3172018-05-24 15:50:06 -07002172 // SF should not have a display token.
2173 const auto displayId = Case::Display::DISPLAY_ID::get();
2174 ASSERT_TRUE(displayId);
2175 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002176}
2177
2178TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectThenConnectPrimary) {
2179 using Case = SimplePrimaryDisplayCase;
2180
2181 // --------------------------------------------------------------------
2182 // Preconditions
2183
2184 setupCommonPreconditions<Case>();
2185
2186 // The display is already completely set up.
2187 Case::Display::injectHwcDisplay(this);
2188 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
2189 existing.inject();
2190
2191 // A hotplug disconnect event is enqueued for a display
2192 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
2193 // A hotplug connect event is also enqueued for the same display
2194 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
2195
2196 // --------------------------------------------------------------------
2197 // Call Expectations
2198
2199 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
2200
2201 setupCommonCallExpectationsForConnectProcessing<Case>();
2202 setupCommonCallExpectationsForDisconnectProcessing<Case>();
2203
2204 // --------------------------------------------------------------------
2205 // Invocation
2206
2207 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2208
2209 // --------------------------------------------------------------------
2210 // Postconditions
2211
2212 // The existing token should have been removed
2213 verifyDisplayIsNotConnected(existing.token());
Dominik Laskowski075d3172018-05-24 15:50:06 -07002214 const auto displayId = Case::Display::DISPLAY_ID::get();
2215 ASSERT_TRUE(displayId);
2216 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
2217 EXPECT_NE(existing.token(), mFlinger.mutablePhysicalDisplayTokens()[*displayId]);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002218
2219 // A new display should be connected in its place
2220
2221 verifyPhysicalDisplayIsConnected<Case>();
2222
2223 // --------------------------------------------------------------------
2224 // Cleanup conditions
2225
2226 EXPECT_CALL(*mComposer,
2227 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
2228 .WillOnce(Return(Error::NONE));
2229 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
2230}
2231
2232TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAdded) {
2233 using Case = HwcVirtualDisplayCase;
2234
2235 // --------------------------------------------------------------------
2236 // Preconditions
2237
2238 // The HWC supports at least one virtual display
2239 injectMockComposer(1);
2240
2241 setupCommonPreconditions<Case>();
2242
2243 // A virtual display was added to the current state, and it has a
2244 // surface(producer)
2245 sp<BBinder> displayToken = new BBinder();
Lloyd Pique4c2ac022018-04-27 12:08:03 -07002246
Dominik Laskowski075d3172018-05-24 15:50:06 -07002247 DisplayDeviceState state;
2248 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002249
2250 sp<mock::GraphicBufferProducer> surface{new mock::GraphicBufferProducer()};
Dominik Laskowski075d3172018-05-24 15:50:06 -07002251 state.surface = surface;
2252 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002253
2254 // --------------------------------------------------------------------
2255 // Call Expectations
2256
2257 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
2258 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
2259
2260 EXPECT_CALL(*surface, query(NATIVE_WINDOW_WIDTH, _))
2261 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::WIDTH), Return(NO_ERROR)));
2262 EXPECT_CALL(*surface, query(NATIVE_WINDOW_HEIGHT, _))
2263 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::HEIGHT), Return(NO_ERROR)));
2264 EXPECT_CALL(*surface, query(NATIVE_WINDOW_FORMAT, _))
2265 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT),
2266 Return(NO_ERROR)));
2267 EXPECT_CALL(*surface, query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, _))
2268 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(NO_ERROR)));
2269
2270 EXPECT_CALL(*surface, setAsyncMode(true)).Times(1);
2271
Lloyd Pique86fa3db2019-02-04 18:46:01 -08002272 EXPECT_CALL(*mProducer, connect(_, NATIVE_WINDOW_API_EGL, false, _)).Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002273 EXPECT_CALL(*mProducer, disconnect(_, _)).Times(1);
2274
2275 Case::Display::setupHwcVirtualDisplayCreationCallExpectations(this);
2276 Case::WideColorSupport::setupComposerCallExpectations(this);
2277 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07002278 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002279
2280 // --------------------------------------------------------------------
2281 // Invocation
2282
2283 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2284
2285 // --------------------------------------------------------------------
2286 // Postconditions
2287
2288 // The display device should have been set up in the list of displays.
2289 verifyDisplayIsConnected<Case>(displayToken);
2290
2291 // --------------------------------------------------------------------
2292 // Cleanup conditions
2293
2294 EXPECT_CALL(*mComposer, destroyVirtualDisplay(Case::Display::HWC_DISPLAY_ID))
2295 .WillOnce(Return(Error::NONE));
2296 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Pique9e9800c2019-02-26 16:26:09 -08002297
2298 // Cleanup
2299 mFlinger.mutableCurrentState().displays.removeItem(displayToken);
2300 mFlinger.mutableDrawingState().displays.removeItem(displayToken);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002301}
2302
2303TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAddedWithNoSurface) {
2304 using Case = HwcVirtualDisplayCase;
2305
2306 // --------------------------------------------------------------------
2307 // Preconditions
2308
2309 // The HWC supports at least one virtual display
2310 injectMockComposer(1);
2311
2312 setupCommonPreconditions<Case>();
2313
2314 // A virtual display was added to the current state, but it does not have a
2315 // surface.
2316 sp<BBinder> displayToken = new BBinder();
2317
Dominik Laskowski075d3172018-05-24 15:50:06 -07002318 DisplayDeviceState state;
2319 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002320
Dominik Laskowski075d3172018-05-24 15:50:06 -07002321 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002322
2323 // --------------------------------------------------------------------
2324 // Call Expectations
2325
2326 // --------------------------------------------------------------------
2327 // Invocation
2328
2329 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2330
2331 // --------------------------------------------------------------------
2332 // Postconditions
2333
2334 // There will not be a display device set up.
2335 EXPECT_FALSE(hasDisplayDevice(displayToken));
2336
2337 // The drawing display state will be set from the current display state.
2338 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
2339 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07002340 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002341}
2342
2343TEST_F(HandleTransactionLockedTest, processesVirtualDisplayRemoval) {
2344 using Case = HwcVirtualDisplayCase;
2345
2346 // --------------------------------------------------------------------
2347 // Preconditions
2348
2349 // A virtual display is set up but is removed from the current state.
Dominik Laskowski075d3172018-05-24 15:50:06 -07002350 const auto displayId = Case::Display::DISPLAY_ID::get();
2351 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08002352 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08002353 Case::Display::injectHwcDisplay(this);
2354 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
2355 existing.inject();
2356 mFlinger.mutableCurrentState().displays.removeItem(existing.token());
2357
2358 // --------------------------------------------------------------------
2359 // Call Expectations
2360
2361 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
2362
2363 // --------------------------------------------------------------------
2364 // Invocation
2365
2366 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2367
2368 // --------------------------------------------------------------------
2369 // Postconditions
2370
2371 // The existing token should have been removed
2372 verifyDisplayIsNotConnected(existing.token());
2373}
2374
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002375TEST_F(HandleTransactionLockedTest, processesDisplayLayerStackChanges) {
2376 using Case = NonHwcVirtualDisplayCase;
2377
2378 constexpr uint32_t oldLayerStack = 0u;
2379 constexpr uint32_t newLayerStack = 123u;
2380
2381 // --------------------------------------------------------------------
2382 // Preconditions
2383
2384 // A display is set up
2385 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2386 display.inject();
2387
2388 // There is a change to the layerStack state
2389 display.mutableDrawingDisplayState().layerStack = oldLayerStack;
2390 display.mutableCurrentDisplayState().layerStack = newLayerStack;
2391
2392 // --------------------------------------------------------------------
2393 // Invocation
2394
2395 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2396
2397 // --------------------------------------------------------------------
2398 // Postconditions
2399
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002400 EXPECT_EQ(newLayerStack, display.mutableDisplayDevice()->getLayerStack());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002401}
2402
2403TEST_F(HandleTransactionLockedTest, processesDisplayTransformChanges) {
2404 using Case = NonHwcVirtualDisplayCase;
2405
Dominik Laskowski718f9602019-11-09 20:01:35 -08002406 constexpr ui::Rotation oldTransform = ui::ROTATION_0;
2407 constexpr ui::Rotation newTransform = ui::ROTATION_180;
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002408
2409 // --------------------------------------------------------------------
2410 // Preconditions
2411
2412 // A display is set up
2413 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2414 display.inject();
2415
2416 // There is a change to the orientation state
2417 display.mutableDrawingDisplayState().orientation = oldTransform;
2418 display.mutableCurrentDisplayState().orientation = newTransform;
2419
2420 // --------------------------------------------------------------------
2421 // Invocation
2422
2423 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2424
2425 // --------------------------------------------------------------------
2426 // Postconditions
2427
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002428 EXPECT_EQ(newTransform, display.mutableDisplayDevice()->getOrientation());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002429}
2430
2431TEST_F(HandleTransactionLockedTest, processesDisplayViewportChanges) {
2432 using Case = NonHwcVirtualDisplayCase;
2433
2434 const Rect oldViewport(0, 0, 0, 0);
2435 const Rect newViewport(0, 0, 123, 456);
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 viewport state
2445 display.mutableDrawingDisplayState().viewport = oldViewport;
2446 display.mutableCurrentDisplayState().viewport = newViewport;
2447
2448 // --------------------------------------------------------------------
2449 // Invocation
2450
2451 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2452
2453 // --------------------------------------------------------------------
2454 // Postconditions
2455
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002456 EXPECT_EQ(newViewport, display.mutableDisplayDevice()->getViewport());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002457}
2458
2459TEST_F(HandleTransactionLockedTest, processesDisplayFrameChanges) {
2460 using Case = NonHwcVirtualDisplayCase;
2461
2462 const Rect oldFrame(0, 0, 0, 0);
2463 const Rect newFrame(0, 0, 123, 456);
2464
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 viewport state
2473 display.mutableDrawingDisplayState().frame = oldFrame;
2474 display.mutableCurrentDisplayState().frame = newFrame;
2475
2476 // --------------------------------------------------------------------
2477 // Invocation
2478
2479 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2480
2481 // --------------------------------------------------------------------
2482 // Postconditions
2483
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002484 EXPECT_EQ(newFrame, display.mutableDisplayDevice()->getFrame());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002485}
2486
2487TEST_F(HandleTransactionLockedTest, processesDisplayWidthChanges) {
2488 using Case = NonHwcVirtualDisplayCase;
2489
2490 constexpr int oldWidth = 0;
2491 constexpr int oldHeight = 10;
2492 constexpr int newWidth = 123;
2493
2494 // --------------------------------------------------------------------
2495 // Preconditions
2496
2497 // A display is set up
2498 auto nativeWindow = new mock::NativeWindow();
Lloyd Pique542307f2018-10-19 13:24:08 -07002499 auto displaySurface = new compositionengine::mock::DisplaySurface();
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002500 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002501 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2502 display.setNativeWindow(nativeWindow);
2503 display.setDisplaySurface(displaySurface);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002504 // Setup injection expections
2505 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
2506 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
2507 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
2508 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
Lloyd Pique86fa3db2019-02-04 18:46:01 -08002509 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT)).Times(1);
2510 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_API_CONNECT)).Times(1);
2511 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_SET_USAGE64)).Times(1);
chaviw8beb4142019-04-11 13:09:05 -07002512 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002513 display.inject();
2514
2515 // There is a change to the viewport state
2516 display.mutableDrawingDisplayState().width = oldWidth;
2517 display.mutableDrawingDisplayState().height = oldHeight;
2518 display.mutableCurrentDisplayState().width = newWidth;
2519 display.mutableCurrentDisplayState().height = oldHeight;
2520
2521 // --------------------------------------------------------------------
2522 // Call Expectations
2523
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002524 EXPECT_CALL(*displaySurface, resizeBuffers(newWidth, oldHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002525
2526 // --------------------------------------------------------------------
2527 // Invocation
2528
2529 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2530}
2531
2532TEST_F(HandleTransactionLockedTest, processesDisplayHeightChanges) {
2533 using Case = NonHwcVirtualDisplayCase;
2534
2535 constexpr int oldWidth = 0;
2536 constexpr int oldHeight = 10;
2537 constexpr int newHeight = 123;
2538
2539 // --------------------------------------------------------------------
2540 // Preconditions
2541
2542 // A display is set up
2543 auto nativeWindow = new mock::NativeWindow();
Lloyd Pique542307f2018-10-19 13:24:08 -07002544 auto displaySurface = new compositionengine::mock::DisplaySurface();
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002545 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002546 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2547 display.setNativeWindow(nativeWindow);
2548 display.setDisplaySurface(displaySurface);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002549 // Setup injection expections
2550 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
2551 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
2552 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
2553 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
Lloyd Pique86fa3db2019-02-04 18:46:01 -08002554 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT)).Times(1);
2555 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_API_CONNECT)).Times(1);
2556 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_SET_USAGE64)).Times(1);
chaviw8beb4142019-04-11 13:09:05 -07002557 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002558 display.inject();
2559
2560 // There is a change to the viewport state
2561 display.mutableDrawingDisplayState().width = oldWidth;
2562 display.mutableDrawingDisplayState().height = oldHeight;
2563 display.mutableCurrentDisplayState().width = oldWidth;
2564 display.mutableCurrentDisplayState().height = newHeight;
2565
2566 // --------------------------------------------------------------------
2567 // Call Expectations
2568
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002569 EXPECT_CALL(*displaySurface, resizeBuffers(oldWidth, newHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002570
2571 // --------------------------------------------------------------------
2572 // Invocation
2573
2574 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2575}
2576
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002577/* ------------------------------------------------------------------------
2578 * SurfaceFlinger::setDisplayStateLocked
2579 */
2580
2581TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithUnknownDisplay) {
2582 // --------------------------------------------------------------------
2583 // Preconditions
2584
2585 // We have an unknown display token not associated with a known display
2586 sp<BBinder> displayToken = new BBinder();
2587
2588 // The requested display state references the unknown display.
2589 DisplayState state;
2590 state.what = DisplayState::eLayerStackChanged;
2591 state.token = displayToken;
2592 state.layerStack = 456;
2593
2594 // --------------------------------------------------------------------
2595 // Invocation
2596
2597 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2598
2599 // --------------------------------------------------------------------
2600 // Postconditions
2601
2602 // The returned flags are empty
2603 EXPECT_EQ(0u, flags);
2604
2605 // The display token still doesn't match anything known.
2606 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
2607}
2608
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002609TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWhenNoChanges) {
2610 using Case = SimplePrimaryDisplayCase;
2611
2612 // --------------------------------------------------------------------
2613 // Preconditions
2614
2615 // A display is already set up
2616 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2617 display.inject();
2618
2619 // No changes are made to the display
2620 DisplayState state;
2621 state.what = 0;
2622 state.token = display.token();
2623
2624 // --------------------------------------------------------------------
2625 // Invocation
2626
2627 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2628
2629 // --------------------------------------------------------------------
2630 // Postconditions
2631
2632 // The returned flags are empty
2633 EXPECT_EQ(0u, flags);
2634}
2635
2636TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSurfaceDidNotChange) {
2637 using Case = SimplePrimaryDisplayCase;
2638
2639 // --------------------------------------------------------------------
2640 // Preconditions
2641
2642 // A display is already set up
2643 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2644 display.inject();
2645
2646 // There is a surface that can be set.
2647 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2648
2649 // The current display state has the surface set
2650 display.mutableCurrentDisplayState().surface = surface;
2651
2652 // The incoming request sets the same surface
2653 DisplayState state;
2654 state.what = DisplayState::eSurfaceChanged;
2655 state.token = display.token();
2656 state.surface = surface;
2657
2658 // --------------------------------------------------------------------
2659 // Invocation
2660
2661 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2662
2663 // --------------------------------------------------------------------
2664 // Postconditions
2665
2666 // The returned flags are empty
2667 EXPECT_EQ(0u, flags);
2668
2669 // The current display state is unchanged.
2670 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2671}
2672
2673TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfSurfaceChanged) {
2674 using Case = SimplePrimaryDisplayCase;
2675
2676 // --------------------------------------------------------------------
2677 // Preconditions
2678
2679 // A display is already set up
2680 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2681 display.inject();
2682
2683 // There is a surface that can be set.
2684 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2685
2686 // The current display state does not have a surface
2687 display.mutableCurrentDisplayState().surface = nullptr;
2688
2689 // The incoming request sets a surface
2690 DisplayState state;
2691 state.what = DisplayState::eSurfaceChanged;
2692 state.token = display.token();
2693 state.surface = surface;
2694
2695 // --------------------------------------------------------------------
2696 // Invocation
2697
2698 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2699
2700 // --------------------------------------------------------------------
2701 // Postconditions
2702
2703 // The returned flags indicate a transaction is needed
2704 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2705
2706 // The current display layer stack state is set to the new value
2707 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2708}
2709
2710TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfLayerStackDidNotChange) {
2711 using Case = SimplePrimaryDisplayCase;
2712
2713 // --------------------------------------------------------------------
2714 // Preconditions
2715
2716 // A display is already set up
2717 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2718 display.inject();
2719
2720 // The display has a layer stack set
2721 display.mutableCurrentDisplayState().layerStack = 456u;
2722
2723 // The incoming request sets the same layer stack
2724 DisplayState state;
2725 state.what = DisplayState::eLayerStackChanged;
2726 state.token = display.token();
2727 state.layerStack = 456u;
2728
2729 // --------------------------------------------------------------------
2730 // Invocation
2731
2732 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2733
2734 // --------------------------------------------------------------------
2735 // Postconditions
2736
2737 // The returned flags are empty
2738 EXPECT_EQ(0u, flags);
2739
2740 // The current display state is unchanged
2741 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2742}
2743
2744TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfLayerStackChanged) {
2745 using Case = SimplePrimaryDisplayCase;
2746
2747 // --------------------------------------------------------------------
2748 // Preconditions
2749
2750 // A display is set up
2751 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2752 display.inject();
2753
2754 // The display has a layer stack set
2755 display.mutableCurrentDisplayState().layerStack = 654u;
2756
2757 // The incoming request sets a different layer stack
2758 DisplayState state;
2759 state.what = DisplayState::eLayerStackChanged;
2760 state.token = display.token();
2761 state.layerStack = 456u;
2762
2763 // --------------------------------------------------------------------
2764 // Invocation
2765
2766 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2767
2768 // --------------------------------------------------------------------
2769 // Postconditions
2770
2771 // The returned flags indicate a transaction is needed
2772 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2773
2774 // The desired display state has been set to the new value.
2775 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2776}
2777
2778TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfProjectionDidNotChange) {
2779 using Case = SimplePrimaryDisplayCase;
Dominik Laskowski718f9602019-11-09 20:01:35 -08002780 constexpr ui::Rotation initialOrientation = ui::ROTATION_180;
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002781 const Rect initialFrame = {1, 2, 3, 4};
2782 const Rect initialViewport = {5, 6, 7, 8};
2783
2784 // --------------------------------------------------------------------
2785 // Preconditions
2786
2787 // A display is set up
2788 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2789 display.inject();
2790
2791 // The current display state projection state is all set
2792 display.mutableCurrentDisplayState().orientation = initialOrientation;
2793 display.mutableCurrentDisplayState().frame = initialFrame;
2794 display.mutableCurrentDisplayState().viewport = initialViewport;
2795
2796 // The incoming request sets the same projection state
2797 DisplayState state;
2798 state.what = DisplayState::eDisplayProjectionChanged;
2799 state.token = display.token();
2800 state.orientation = initialOrientation;
2801 state.frame = initialFrame;
2802 state.viewport = initialViewport;
2803
2804 // --------------------------------------------------------------------
2805 // Invocation
2806
2807 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2808
2809 // --------------------------------------------------------------------
2810 // Postconditions
2811
2812 // The returned flags are empty
2813 EXPECT_EQ(0u, flags);
2814
2815 // The current display state is unchanged
2816 EXPECT_EQ(initialOrientation, display.getCurrentDisplayState().orientation);
2817
2818 EXPECT_EQ(initialFrame, display.getCurrentDisplayState().frame);
2819 EXPECT_EQ(initialViewport, display.getCurrentDisplayState().viewport);
2820}
2821
2822TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfOrientationChanged) {
2823 using Case = SimplePrimaryDisplayCase;
Dominik Laskowski718f9602019-11-09 20:01:35 -08002824 constexpr ui::Rotation initialOrientation = ui::ROTATION_90;
2825 constexpr ui::Rotation desiredOrientation = ui::ROTATION_180;
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002826
2827 // --------------------------------------------------------------------
2828 // Preconditions
2829
2830 // A display is set up
2831 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2832 display.inject();
2833
2834 // The current display state has an orientation set
2835 display.mutableCurrentDisplayState().orientation = initialOrientation;
2836
2837 // The incoming request sets a different orientation
2838 DisplayState state;
2839 state.what = DisplayState::eDisplayProjectionChanged;
2840 state.token = display.token();
2841 state.orientation = desiredOrientation;
2842
2843 // --------------------------------------------------------------------
2844 // Invocation
2845
2846 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2847
2848 // --------------------------------------------------------------------
2849 // Postconditions
2850
2851 // The returned flags indicate a transaction is needed
2852 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2853
2854 // The current display state has the new value.
2855 EXPECT_EQ(desiredOrientation, display.getCurrentDisplayState().orientation);
2856}
2857
2858TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfFrameChanged) {
2859 using Case = SimplePrimaryDisplayCase;
2860 const Rect initialFrame = {0, 0, 0, 0};
2861 const Rect desiredFrame = {5, 6, 7, 8};
2862
2863 // --------------------------------------------------------------------
2864 // Preconditions
2865
2866 // A display is set up
2867 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2868 display.inject();
2869
2870 // The current display state does not have a frame
2871 display.mutableCurrentDisplayState().frame = initialFrame;
2872
2873 // The incoming request sets a frame
2874 DisplayState state;
2875 state.what = DisplayState::eDisplayProjectionChanged;
2876 state.token = display.token();
2877 state.frame = desiredFrame;
2878
2879 // --------------------------------------------------------------------
2880 // Invocation
2881
2882 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2883
2884 // --------------------------------------------------------------------
2885 // Postconditions
2886
2887 // The returned flags indicate a transaction is needed
2888 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2889
2890 // The current display state has the new value.
2891 EXPECT_EQ(desiredFrame, display.getCurrentDisplayState().frame);
2892}
2893
2894TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfViewportChanged) {
2895 using Case = SimplePrimaryDisplayCase;
2896 const Rect initialViewport = {0, 0, 0, 0};
2897 const Rect desiredViewport = {5, 6, 7, 8};
2898
2899 // --------------------------------------------------------------------
2900 // Preconditions
2901
2902 // A display is set up
2903 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2904 display.inject();
2905
2906 // The current display state does not have a viewport
2907 display.mutableCurrentDisplayState().viewport = initialViewport;
2908
2909 // The incoming request sets a viewport
2910 DisplayState state;
2911 state.what = DisplayState::eDisplayProjectionChanged;
2912 state.token = display.token();
2913 state.viewport = desiredViewport;
2914
2915 // --------------------------------------------------------------------
2916 // Invocation
2917
2918 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2919
2920 // --------------------------------------------------------------------
2921 // Postconditions
2922
2923 // The returned flags indicate a transaction is needed
2924 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2925
2926 // The current display state has the new value.
2927 EXPECT_EQ(desiredViewport, display.getCurrentDisplayState().viewport);
2928}
2929
2930TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSizeDidNotChange) {
2931 using Case = SimplePrimaryDisplayCase;
2932 constexpr uint32_t initialWidth = 1024;
2933 constexpr uint32_t initialHeight = 768;
2934
2935 // --------------------------------------------------------------------
2936 // Preconditions
2937
2938 // A display is set up
2939 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2940 display.inject();
2941
2942 // The current display state has a size set
2943 display.mutableCurrentDisplayState().width = initialWidth;
2944 display.mutableCurrentDisplayState().height = initialHeight;
2945
2946 // The incoming request sets the same display size
2947 DisplayState state;
2948 state.what = DisplayState::eDisplaySizeChanged;
2949 state.token = display.token();
2950 state.width = initialWidth;
2951 state.height = initialHeight;
2952
2953 // --------------------------------------------------------------------
2954 // Invocation
2955
2956 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2957
2958 // --------------------------------------------------------------------
2959 // Postconditions
2960
2961 // The returned flags are empty
2962 EXPECT_EQ(0u, flags);
2963
2964 // The current display state is unchanged
2965 EXPECT_EQ(initialWidth, display.getCurrentDisplayState().width);
2966 EXPECT_EQ(initialHeight, display.getCurrentDisplayState().height);
2967}
2968
2969TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfWidthChanged) {
2970 using Case = SimplePrimaryDisplayCase;
2971 constexpr uint32_t initialWidth = 0;
2972 constexpr uint32_t desiredWidth = 1024;
2973
2974 // --------------------------------------------------------------------
2975 // Preconditions
2976
2977 // A display is set up
2978 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2979 display.inject();
2980
2981 // The display does not yet have a width
2982 display.mutableCurrentDisplayState().width = initialWidth;
2983
2984 // The incoming request sets a display width
2985 DisplayState state;
2986 state.what = DisplayState::eDisplaySizeChanged;
2987 state.token = display.token();
2988 state.width = desiredWidth;
2989
2990 // --------------------------------------------------------------------
2991 // Invocation
2992
2993 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2994
2995 // --------------------------------------------------------------------
2996 // Postconditions
2997
2998 // The returned flags indicate a transaction is needed
2999 EXPECT_EQ(eDisplayTransactionNeeded, flags);
3000
3001 // The current display state has the new value.
3002 EXPECT_EQ(desiredWidth, display.getCurrentDisplayState().width);
3003}
3004
3005TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfHeightChanged) {
3006 using Case = SimplePrimaryDisplayCase;
3007 constexpr uint32_t initialHeight = 0;
3008 constexpr uint32_t desiredHeight = 768;
3009
3010 // --------------------------------------------------------------------
3011 // Preconditions
3012
3013 // A display is set up
3014 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
3015 display.inject();
3016
3017 // The display does not yet have a height
3018 display.mutableCurrentDisplayState().height = initialHeight;
3019
3020 // The incoming request sets a display height
3021 DisplayState state;
3022 state.what = DisplayState::eDisplaySizeChanged;
3023 state.token = display.token();
3024 state.height = desiredHeight;
3025
3026 // --------------------------------------------------------------------
3027 // Invocation
3028
3029 uint32_t flags = mFlinger.setDisplayStateLocked(state);
3030
3031 // --------------------------------------------------------------------
3032 // Postconditions
3033
3034 // The returned flags indicate a transaction is needed
3035 EXPECT_EQ(eDisplayTransactionNeeded, flags);
3036
3037 // The current display state has the new value.
3038 EXPECT_EQ(desiredHeight, display.getCurrentDisplayState().height);
3039}
3040
Lloyd Pique86016da2018-03-01 16:09:38 -08003041/* ------------------------------------------------------------------------
3042 * SurfaceFlinger::onInitializeDisplays
3043 */
3044
3045TEST_F(DisplayTransactionTest, onInitializeDisplaysSetsUpPrimaryDisplay) {
3046 using Case = SimplePrimaryDisplayCase;
3047
3048 // --------------------------------------------------------------------
3049 // Preconditions
3050
3051 // A primary display is set up
3052 Case::Display::injectHwcDisplay(this);
3053 auto primaryDisplay = Case::Display::makeFakeExistingDisplayInjector(this);
3054 primaryDisplay.inject();
3055
3056 // --------------------------------------------------------------------
3057 // Call Expectations
3058
3059 // We expect the surface interceptor to possibly be used, but we treat it as
3060 // disabled since it is called as a side effect rather than directly by this
3061 // function.
3062 EXPECT_CALL(*mSurfaceInterceptor, isEnabled()).WillOnce(Return(false));
3063
3064 // We expect a call to get the active display config.
3065 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
3066
3067 // We expect invalidate() to be invoked once to trigger display transaction
3068 // processing.
3069 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
3070
Lloyd Pique86fa3db2019-02-04 18:46:01 -08003071 EXPECT_CALL(*mPrimaryDispSync, expectedPresentTime()).WillRepeatedly(Return(0));
3072
Lloyd Pique86016da2018-03-01 16:09:38 -08003073 // --------------------------------------------------------------------
3074 // Invocation
3075
3076 mFlinger.onInitializeDisplays();
3077
3078 // --------------------------------------------------------------------
3079 // Postconditions
3080
3081 // The primary display should have a current state
3082 ASSERT_TRUE(hasCurrentDisplayState(primaryDisplay.token()));
3083 const auto& primaryDisplayState = getCurrentDisplayState(primaryDisplay.token());
3084 // The layer stack state should be set to zero
3085 EXPECT_EQ(0u, primaryDisplayState.layerStack);
3086 // The orientation state should be set to zero
Dominik Laskowski718f9602019-11-09 20:01:35 -08003087 EXPECT_EQ(ui::ROTATION_0, primaryDisplayState.orientation);
Lloyd Pique86016da2018-03-01 16:09:38 -08003088
3089 // The frame state should be set to INVALID
3090 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.frame);
3091
3092 // The viewport state should be set to INVALID
3093 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.viewport);
3094
3095 // The width and height should both be zero
3096 EXPECT_EQ(0u, primaryDisplayState.width);
3097 EXPECT_EQ(0u, primaryDisplayState.height);
3098
3099 // The display should be set to HWC_POWER_MODE_NORMAL
3100 ASSERT_TRUE(hasDisplayDevice(primaryDisplay.token()));
3101 auto displayDevice = primaryDisplay.mutableDisplayDevice();
3102 EXPECT_EQ(HWC_POWER_MODE_NORMAL, displayDevice->getPowerMode());
3103
3104 // The display refresh period should be set in the frame tracker.
3105 FrameStats stats;
3106 mFlinger.getAnimFrameTracker().getStats(&stats);
3107 EXPECT_EQ(DEFAULT_REFRESH_RATE, stats.refreshPeriodNano);
3108
3109 // The display transaction needed flag should be set.
3110 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
3111
3112 // The compositor timing should be set to default values
3113 const auto& compositorTiming = mFlinger.getCompositorTiming();
3114 EXPECT_EQ(-DEFAULT_REFRESH_RATE, compositorTiming.deadline);
3115 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.interval);
3116 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.presentLatency);
3117}
3118
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003119/* ------------------------------------------------------------------------
3120 * SurfaceFlinger::setPowerModeInternal
3121 */
3122
3123// Used when we simulate a display that supports doze.
Peiyong Lined531a32018-10-26 18:27:56 -07003124template <typename Display>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003125struct DozeIsSupportedVariant {
3126 static constexpr bool DOZE_SUPPORTED = true;
3127 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
3128 IComposerClient::PowerMode::DOZE;
3129 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
3130 IComposerClient::PowerMode::DOZE_SUSPEND;
Peiyong Lined531a32018-10-26 18:27:56 -07003131
3132 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
3133 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(Display::HWC_DISPLAY_ID, _))
3134 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hwc2::DisplayCapability>(
3135 {Hwc2::DisplayCapability::DOZE})),
3136 Return(Error::NONE)));
3137 }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003138};
3139
Peiyong Lined531a32018-10-26 18:27:56 -07003140template <typename Display>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003141// Used when we simulate a display that does not support doze.
3142struct DozeNotSupportedVariant {
3143 static constexpr bool DOZE_SUPPORTED = false;
3144 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
3145 IComposerClient::PowerMode::ON;
3146 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
3147 IComposerClient::PowerMode::ON;
Peiyong Lined531a32018-10-26 18:27:56 -07003148
3149 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
3150 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(Display::HWC_DISPLAY_ID, _))
3151 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hwc2::DisplayCapability>({})),
3152 Return(Error::NONE)));
3153 }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003154};
3155
3156struct EventThreadBaseSupportedVariant {
3157 static void setupEventAndEventControlThreadNoCallExpectations(DisplayTransactionTest* test) {
3158 // The event control thread should not be notified.
3159 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(_)).Times(0);
3160
3161 // The event thread should not be notified.
3162 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(0);
3163 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(0);
3164 }
3165};
3166
3167struct EventThreadNotSupportedVariant : public EventThreadBaseSupportedVariant {
3168 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
3169 // These calls are only expected for the primary display.
3170
3171 // Instead expect no calls.
3172 setupEventAndEventControlThreadNoCallExpectations(test);
3173 }
3174
3175 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
3176 // These calls are only expected for the primary display.
3177
3178 // Instead expect no calls.
3179 setupEventAndEventControlThreadNoCallExpectations(test);
3180 }
3181};
3182
3183struct EventThreadIsSupportedVariant : public EventThreadBaseSupportedVariant {
3184 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
3185 // The event control thread should be notified to enable vsyncs
3186 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(true)).Times(1);
3187
3188 // The event thread should be notified that the screen was acquired.
3189 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(1);
3190 }
3191
3192 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
3193 // There should be a call to setVsyncEnabled(false)
3194 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(false)).Times(1);
3195
3196 // The event thread should not be notified that the screen was released.
3197 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(1);
3198 }
3199};
3200
Lloyd Pique41be5d22018-06-21 13:11:48 -07003201struct DispSyncIsSupportedVariant {
3202 static void setupBeginResyncCallExpectations(DisplayTransactionTest* test) {
Lloyd Pique41be5d22018-06-21 13:11:48 -07003203 EXPECT_CALL(*test->mPrimaryDispSync, setPeriod(DEFAULT_REFRESH_RATE)).Times(1);
3204 EXPECT_CALL(*test->mPrimaryDispSync, beginResync()).Times(1);
3205 }
3206
3207 static void setupEndResyncCallExpectations(DisplayTransactionTest* test) {
3208 EXPECT_CALL(*test->mPrimaryDispSync, endResync()).Times(1);
3209 }
3210};
3211
3212struct DispSyncNotSupportedVariant {
3213 static void setupBeginResyncCallExpectations(DisplayTransactionTest* /* test */) {}
3214
3215 static void setupEndResyncCallExpectations(DisplayTransactionTest* /* test */) {}
3216};
3217
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003218// --------------------------------------------------------------------
3219// Note:
3220//
3221// There are a large number of transitions we could test, however we only test a
3222// selected subset which provides complete test coverage of the implementation.
3223// --------------------------------------------------------------------
3224
3225template <int initialPowerMode, int targetPowerMode>
3226struct TransitionVariantCommon {
3227 static constexpr auto INITIAL_POWER_MODE = initialPowerMode;
3228 static constexpr auto TARGET_POWER_MODE = targetPowerMode;
3229
3230 static void verifyPostconditions(DisplayTransactionTest*) {}
3231};
3232
3233struct TransitionOffToOnVariant
3234 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_NORMAL> {
3235 template <typename Case>
3236 static void setupCallExpectations(DisplayTransactionTest* test) {
3237 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
3238 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07003239 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003240 Case::setupRepaintEverythingCallExpectations(test);
3241 }
3242
3243 static void verifyPostconditions(DisplayTransactionTest* test) {
3244 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
3245 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
3246 }
3247};
3248
3249struct TransitionOffToDozeSuspendVariant
3250 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_DOZE_SUSPEND> {
3251 template <typename Case>
3252 static void setupCallExpectations(DisplayTransactionTest* test) {
3253 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
3254 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
3255 Case::setupRepaintEverythingCallExpectations(test);
3256 }
3257
3258 static void verifyPostconditions(DisplayTransactionTest* test) {
3259 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
3260 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
3261 }
3262};
3263
3264struct TransitionOnToOffVariant
3265 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_OFF> {
3266 template <typename Case>
3267 static void setupCallExpectations(DisplayTransactionTest* test) {
3268 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07003269 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003270 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
3271 }
3272
3273 static void verifyPostconditions(DisplayTransactionTest* test) {
3274 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
3275 }
3276};
3277
3278struct TransitionDozeSuspendToOffVariant
3279 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_OFF> {
3280 template <typename Case>
3281 static void setupCallExpectations(DisplayTransactionTest* test) {
3282 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
3283 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
3284 }
3285
3286 static void verifyPostconditions(DisplayTransactionTest* test) {
3287 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
3288 }
3289};
3290
3291struct TransitionOnToDozeVariant
3292 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE> {
3293 template <typename Case>
3294 static void setupCallExpectations(DisplayTransactionTest* test) {
3295 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
3296 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
3297 }
3298};
3299
3300struct TransitionDozeSuspendToDozeVariant
3301 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_DOZE> {
3302 template <typename Case>
3303 static void setupCallExpectations(DisplayTransactionTest* test) {
3304 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07003305 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003306 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
3307 }
3308};
3309
3310struct TransitionDozeToOnVariant
3311 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE, HWC_POWER_MODE_NORMAL> {
3312 template <typename Case>
3313 static void setupCallExpectations(DisplayTransactionTest* test) {
3314 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
3315 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
3316 }
3317};
3318
3319struct TransitionDozeSuspendToOnVariant
3320 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_NORMAL> {
3321 template <typename Case>
3322 static void setupCallExpectations(DisplayTransactionTest* test) {
3323 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07003324 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003325 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
3326 }
3327};
3328
3329struct TransitionOnToDozeSuspendVariant
3330 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE_SUSPEND> {
3331 template <typename Case>
3332 static void setupCallExpectations(DisplayTransactionTest* test) {
3333 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07003334 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003335 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
3336 }
3337};
3338
3339struct TransitionOnToUnknownVariant
3340 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_LEET> {
3341 template <typename Case>
3342 static void setupCallExpectations(DisplayTransactionTest* test) {
3343 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
3344 Case::setupNoComposerPowerModeCallExpectations(test);
3345 }
3346};
3347
3348// --------------------------------------------------------------------
3349// Note:
3350//
3351// Rather than testing the cartesian product of of
3352// DozeIsSupported/DozeNotSupported with all other options, we use one for one
3353// display type, and the other for another display type.
3354// --------------------------------------------------------------------
3355
3356template <typename DisplayVariant, typename DozeVariant, typename EventThreadVariant,
Lloyd Pique41be5d22018-06-21 13:11:48 -07003357 typename DispSyncVariant, typename TransitionVariant>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003358struct DisplayPowerCase {
3359 using Display = DisplayVariant;
3360 using Doze = DozeVariant;
3361 using EventThread = EventThreadVariant;
Lloyd Pique41be5d22018-06-21 13:11:48 -07003362 using DispSync = DispSyncVariant;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003363 using Transition = TransitionVariant;
3364
3365 static auto injectDisplayWithInitialPowerMode(DisplayTransactionTest* test, int mode) {
Lloyd Pique86fa3db2019-02-04 18:46:01 -08003366 Display::injectHwcDisplayWithNoDefaultCapabilities(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003367 auto display = Display::makeFakeExistingDisplayInjector(test);
3368 display.inject();
3369 display.mutableDisplayDevice()->setPowerMode(mode);
3370 return display;
3371 }
3372
3373 static void setInitialPrimaryHWVsyncEnabled(DisplayTransactionTest* test, bool enabled) {
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -07003374 test->mFlinger.scheduler()->mutablePrimaryHWVsyncEnabled() = enabled;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003375 }
3376
3377 static void setupRepaintEverythingCallExpectations(DisplayTransactionTest* test) {
3378 EXPECT_CALL(*test->mMessageQueue, invalidate()).Times(1);
3379 }
3380
3381 static void setupSurfaceInterceptorCallExpectations(DisplayTransactionTest* test, int mode) {
3382 EXPECT_CALL(*test->mSurfaceInterceptor, isEnabled()).WillOnce(Return(true));
3383 EXPECT_CALL(*test->mSurfaceInterceptor, savePowerModeUpdate(_, mode)).Times(1);
3384 }
3385
3386 static void setupComposerCallExpectations(DisplayTransactionTest* test,
3387 IComposerClient::PowerMode mode) {
3388 // Any calls to get the active config will return a default value.
3389 EXPECT_CALL(*test->mComposer, getActiveConfig(Display::HWC_DISPLAY_ID, _))
3390 .WillRepeatedly(DoAll(SetArgPointee<1>(Display::HWC_ACTIVE_CONFIG_ID),
3391 Return(Error::NONE)));
3392
3393 // Any calls to get whether the display supports dozing will return the value set by the
3394 // policy variant.
3395 EXPECT_CALL(*test->mComposer, getDozeSupport(Display::HWC_DISPLAY_ID, _))
3396 .WillRepeatedly(DoAll(SetArgPointee<1>(Doze::DOZE_SUPPORTED), Return(Error::NONE)));
3397
3398 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, mode)).Times(1);
3399 }
3400
3401 static void setupNoComposerPowerModeCallExpectations(DisplayTransactionTest* test) {
3402 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, _)).Times(0);
3403 }
3404};
3405
3406// A sample configuration for the primary display.
3407// In addition to having event thread support, we emulate doze support.
3408template <typename TransitionVariant>
Peiyong Lined531a32018-10-26 18:27:56 -07003409using PrimaryDisplayPowerCase =
3410 DisplayPowerCase<PrimaryDisplayVariant, DozeIsSupportedVariant<PrimaryDisplayVariant>,
3411 EventThreadIsSupportedVariant, DispSyncIsSupportedVariant,
3412 TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003413
3414// A sample configuration for the external display.
3415// In addition to not having event thread support, we emulate not having doze
3416// support.
3417template <typename TransitionVariant>
Peiyong Lined531a32018-10-26 18:27:56 -07003418using ExternalDisplayPowerCase =
3419 DisplayPowerCase<ExternalDisplayVariant, DozeNotSupportedVariant<ExternalDisplayVariant>,
3420 EventThreadNotSupportedVariant, DispSyncNotSupportedVariant,
3421 TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003422
3423class SetPowerModeInternalTest : public DisplayTransactionTest {
3424public:
3425 template <typename Case>
3426 void transitionDisplayCommon();
3427};
3428
3429template <int PowerMode>
3430struct PowerModeInitialVSyncEnabled : public std::false_type {};
3431
3432template <>
3433struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_NORMAL> : public std::true_type {};
3434
3435template <>
3436struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_DOZE> : public std::true_type {};
3437
3438template <typename Case>
3439void SetPowerModeInternalTest::transitionDisplayCommon() {
3440 // --------------------------------------------------------------------
3441 // Preconditions
3442
Peiyong Lined531a32018-10-26 18:27:56 -07003443 Case::Doze::setupComposerCallExpectations(this);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003444 auto display =
3445 Case::injectDisplayWithInitialPowerMode(this, Case::Transition::INITIAL_POWER_MODE);
3446 Case::setInitialPrimaryHWVsyncEnabled(this,
3447 PowerModeInitialVSyncEnabled<
3448 Case::Transition::INITIAL_POWER_MODE>::value);
3449
3450 // --------------------------------------------------------------------
3451 // Call Expectations
3452
3453 Case::setupSurfaceInterceptorCallExpectations(this, Case::Transition::TARGET_POWER_MODE);
3454 Case::Transition::template setupCallExpectations<Case>(this);
3455
3456 // --------------------------------------------------------------------
3457 // Invocation
3458
3459 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(),
3460 Case::Transition::TARGET_POWER_MODE);
3461
3462 // --------------------------------------------------------------------
3463 // Postconditions
3464
3465 Case::Transition::verifyPostconditions(this);
3466}
3467
3468TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfNoChange) {
3469 using Case = SimplePrimaryDisplayCase;
3470
3471 // --------------------------------------------------------------------
3472 // Preconditions
3473
3474 // A primary display device is set up
3475 Case::Display::injectHwcDisplay(this);
3476 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
3477 display.inject();
3478
Dominik Laskowskia2edf612018-06-01 13:15:16 -07003479 // The display is already set to HWC_POWER_MODE_NORMAL
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003480 display.mutableDisplayDevice()->setPowerMode(HWC_POWER_MODE_NORMAL);
3481
3482 // --------------------------------------------------------------------
3483 // Invocation
3484
3485 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_NORMAL);
3486
3487 // --------------------------------------------------------------------
3488 // Postconditions
3489
3490 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
3491}
3492
Dominik Laskowskieecd6592018-05-29 10:25:41 -07003493TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfVirtualDisplay) {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003494 using Case = HwcVirtualDisplayCase;
3495
3496 // --------------------------------------------------------------------
3497 // Preconditions
3498
Dominik Laskowski075d3172018-05-24 15:50:06 -07003499 // Insert display data so that the HWC thinks it created the virtual display.
3500 const auto displayId = Case::Display::DISPLAY_ID::get();
3501 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08003502 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003503
3504 // A virtual display device is set up
3505 Case::Display::injectHwcDisplay(this);
3506 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
3507 display.inject();
3508
Dominik Laskowskieecd6592018-05-29 10:25:41 -07003509 // The display is set to HWC_POWER_MODE_NORMAL
3510 getDisplayDevice(display.token())->setPowerMode(HWC_POWER_MODE_NORMAL);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003511
3512 // --------------------------------------------------------------------
3513 // Invocation
3514
Dominik Laskowskieecd6592018-05-29 10:25:41 -07003515 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_OFF);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003516
3517 // --------------------------------------------------------------------
3518 // Postconditions
3519
3520 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
3521}
3522
3523TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnPrimaryDisplay) {
3524 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToOnVariant>>();
3525}
3526
3527TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendPrimaryDisplay) {
3528 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
3529}
3530
3531TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffPrimaryDisplay) {
3532 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToOffVariant>>();
3533}
3534
3535TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffPrimaryDisplay) {
3536 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
3537}
3538
3539TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozePrimaryDisplay) {
3540 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeVariant>>();
3541}
3542
3543TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozePrimaryDisplay) {
3544 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
3545}
3546
3547TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnPrimaryDisplay) {
3548 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeToOnVariant>>();
3549}
3550
3551TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnPrimaryDisplay) {
3552 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
3553}
3554
3555TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendPrimaryDisplay) {
3556 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
3557}
3558
3559TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownPrimaryDisplay) {
3560 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToUnknownVariant>>();
3561}
3562
3563TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnExternalDisplay) {
3564 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToOnVariant>>();
3565}
3566
3567TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendExternalDisplay) {
3568 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
3569}
3570
3571TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffExternalDisplay) {
3572 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToOffVariant>>();
3573}
3574
3575TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffExternalDisplay) {
3576 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
3577}
3578
3579TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeExternalDisplay) {
3580 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeVariant>>();
3581}
3582
3583TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozeExternalDisplay) {
3584 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
3585}
3586
3587TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnExternalDisplay) {
3588 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeToOnVariant>>();
3589}
3590
3591TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnExternalDisplay) {
3592 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
3593}
3594
3595TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendExternalDisplay) {
3596 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
3597}
3598
3599TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownExternalDisplay) {
3600 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToUnknownVariant>>();
3601}
3602
Lloyd Piquef58625d2017-12-19 13:22:33 -08003603} // namespace
3604} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08003605
3606// TODO(b/129481165): remove the #pragma below and fix conversion issues
3607#pragma clang diagnostic pop // ignored "-Wconversion"