blob: 33563eaed61e4976d4c9892e6c900417f12d88f6 [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 Pique542307f2018-10-19 13:24:08 -070028#include <compositionengine/mock/DisplaySurface.h>
Lloyd Piquef58625d2017-12-19 13:22:33 -080029#include <gmock/gmock.h>
30#include <gtest/gtest.h>
Lloyd Pique1ebe0902019-10-04 14:47:13 -070031#include <gui/mock/GraphicBufferConsumer.h>
32#include <gui/mock/GraphicBufferProducer.h>
Lloyd Piquef58625d2017-12-19 13:22:33 -080033#include <log/log.h>
Lloyd Pique3823e7b2018-10-18 16:58:10 -070034#include <renderengine/mock/RenderEngine.h>
Valerie Hau9758ae02018-10-09 16:05:09 -070035#include <ui/DebugUtils.h>
Dominik Laskowski075d3172018-05-24 15:50:06 -070036
37#include "DisplayIdentificationTest.h"
Ana Krulecafb45842019-02-13 13:33:03 -080038#include "TestableScheduler.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080039#include "TestableSurfaceFlinger.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080040#include "mock/DisplayHardware/MockComposer.h"
Lloyd Pique41be5d22018-06-21 13:11:48 -070041#include "mock/MockDispSync.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080042#include "mock/MockEventControlThread.h"
43#include "mock/MockEventThread.h"
44#include "mock/MockMessageQueue.h"
45#include "mock/MockNativeWindowSurface.h"
46#include "mock/MockSurfaceInterceptor.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080047#include "mock/system/window/MockNativeWindow.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080048
49namespace android {
50namespace {
51
Lloyd Piquee39cad22017-12-20 17:01:29 -080052using testing::_;
Lloyd Piquee39cad22017-12-20 17:01:29 -080053using testing::DoAll;
54using testing::Mock;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080055using testing::ResultOf;
Lloyd Piquee39cad22017-12-20 17:01:29 -080056using testing::Return;
57using testing::SetArgPointee;
58
Lloyd Piqued883d5a2018-04-27 19:32:30 -070059using android::Hwc2::ColorMode;
Lloyd Piquee39cad22017-12-20 17:01:29 -080060using android::Hwc2::Error;
Lloyd Piqued883d5a2018-04-27 19:32:30 -070061using android::Hwc2::Hdr;
Lloyd Piquee39cad22017-12-20 17:01:29 -080062using android::Hwc2::IComposer;
63using android::Hwc2::IComposerClient;
Lloyd Piqued883d5a2018-04-27 19:32:30 -070064using android::Hwc2::PerFrameMetadataKey;
65using android::Hwc2::RenderIntent;
Lloyd Piquee39cad22017-12-20 17:01:29 -080066
Lloyd Piquec11e0d32018-01-22 18:44:59 -080067using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector;
68using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
Lloyd Pique1fa4d462018-01-22 18:03:16 -080069using HotplugEvent = TestableSurfaceFlinger::HotplugEvent;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080070using HWC2Display = TestableSurfaceFlinger::HWC2Display;
Lloyd Piquebc792092018-01-17 11:52:30 -080071
Lloyd Piquec11e0d32018-01-22 18:44:59 -080072constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666;
Lloyd Piquee39cad22017-12-20 17:01:29 -080073constexpr int32_t DEFAULT_DPI = 320;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080074constexpr int DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT = HAL_PIXEL_FORMAT_RGB_565;
Lloyd Piquee39cad22017-12-20 17:01:29 -080075
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -080076constexpr int HWC_POWER_MODE_LEET = 1337; // An out of range power mode value
77
Lloyd Piquec11e0d32018-01-22 18:44:59 -080078/* ------------------------------------------------------------------------
79 * Boolean avoidance
80 *
81 * To make calls and template instantiations more readable, we define some
82 * local enums along with an implicit bool conversion.
83 */
84
85#define BOOL_SUBSTITUTE(TYPENAME) enum class TYPENAME : bool { FALSE = false, TRUE = true };
86
Lloyd Piquec11e0d32018-01-22 18:44:59 -080087BOOL_SUBSTITUTE(Async);
Dominik Laskowski075d3172018-05-24 15:50:06 -070088BOOL_SUBSTITUTE(Critical);
89BOOL_SUBSTITUTE(Primary);
Lloyd Piquec11e0d32018-01-22 18:44:59 -080090BOOL_SUBSTITUTE(Secure);
Dominik Laskowski075d3172018-05-24 15:50:06 -070091BOOL_SUBSTITUTE(Virtual);
Lloyd Piquec11e0d32018-01-22 18:44:59 -080092
93/* ------------------------------------------------------------------------
94 *
95 */
Lloyd Pique1fa4d462018-01-22 18:03:16 -080096
Lloyd Piquef58625d2017-12-19 13:22:33 -080097class DisplayTransactionTest : public testing::Test {
Lloyd Piquec11e0d32018-01-22 18:44:59 -080098public:
Lloyd Piquef58625d2017-12-19 13:22:33 -080099 DisplayTransactionTest();
100 ~DisplayTransactionTest() override;
101
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800102 // --------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800103 // Mock/Fake injection
Lloyd Piquef58625d2017-12-19 13:22:33 -0800104
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700105 void injectMockScheduler();
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800106 void injectMockComposer(int virtualDisplayCount);
107 void injectFakeBufferQueueFactory();
108 void injectFakeNativeWindowSurfaceFactory();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800109
110 // --------------------------------------------------------------------
111 // Postcondition helpers
112
Dominik Laskowski075d3172018-05-24 15:50:06 -0700113 bool hasPhysicalHwcDisplay(hwc2_display_t hwcDisplayId);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800114 bool hasTransactionFlagSet(int flag);
115 bool hasDisplayDevice(sp<IBinder> displayToken);
116 sp<DisplayDevice> getDisplayDevice(sp<IBinder> displayToken);
117 bool hasCurrentDisplayState(sp<IBinder> displayToken);
118 const DisplayDeviceState& getCurrentDisplayState(sp<IBinder> displayToken);
119 bool hasDrawingDisplayState(sp<IBinder> displayToken);
120 const DisplayDeviceState& getDrawingDisplayState(sp<IBinder> displayToken);
121
122 // --------------------------------------------------------------------
123 // Test instances
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800124
Lloyd Piquef58625d2017-12-19 13:22:33 -0800125 TestableSurfaceFlinger mFlinger;
Alec Mouriba013fa2018-10-16 12:43:11 -0700126 sp<mock::NativeWindow> mNativeWindow = new mock::NativeWindow();
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800127 sp<GraphicBuffer> mBuffer = new GraphicBuffer();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800128
129 // These mocks are created by the test, but are destroyed by SurfaceFlinger
130 // by virtue of being stored into a std::unique_ptr. However we still need
131 // to keep a reference to them for use in setting up call expectations.
Peiyong Lin833074a2018-08-28 11:53:54 -0700132 renderengine::mock::RenderEngine* mRenderEngine = new renderengine::mock::RenderEngine();
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800133 Hwc2::mock::Composer* mComposer = nullptr;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800134 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
135 mock::SurfaceInterceptor* mSurfaceInterceptor = new mock::SurfaceInterceptor();
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700136
137 mock::DispSync* mPrimaryDispSync = new mock::DispSync;
138 mock::EventControlThread* mEventControlThread = new mock::EventControlThread;
139 mock::EventThread* mEventThread = new mock::EventThread;
140 mock::EventThread* mSFEventThread = new mock::EventThread;
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800141
142 // These mocks are created only when expected to be created via a factory.
143 sp<mock::GraphicBufferConsumer> mConsumer;
144 sp<mock::GraphicBufferProducer> mProducer;
Lloyd Pique22098362018-09-13 11:46:49 -0700145 surfaceflinger::mock::NativeWindowSurface* mNativeWindowSurface = nullptr;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800146};
147
148DisplayTransactionTest::DisplayTransactionTest() {
149 const ::testing::TestInfo* const test_info =
150 ::testing::UnitTest::GetInstance()->current_test_info();
151 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800152
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800153 // Default to no wide color display support configured
154 mFlinger.mutableHasWideColorDisplay() = false;
Peiyong Lin13effd12018-07-24 17:01:47 -0700155 mFlinger.mutableUseColorManagement() = false;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800156 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800157
158 // Default to using HWC virtual displays
159 mFlinger.mutableUseHwcVirtualDisplays() = true;
160
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800161 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
162 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
163 });
164
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800165 mFlinger.setCreateNativeWindowSurface([](auto) {
166 ADD_FAILURE() << "Unexpected request to create a native window surface.";
167 return nullptr;
168 });
169
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700170 injectMockScheduler();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800171 mFlinger.mutableEventQueue().reset(mMessageQueue);
Peiyong Lin833074a2018-08-28 11:53:54 -0700172 mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800173 mFlinger.mutableInterceptor().reset(mSurfaceInterceptor);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800174
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800175 injectMockComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -0800176}
177
178DisplayTransactionTest::~DisplayTransactionTest() {
179 const ::testing::TestInfo* const test_info =
180 ::testing::UnitTest::GetInstance()->current_test_info();
181 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
182}
183
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700184void DisplayTransactionTest::injectMockScheduler() {
Ana Krulecafb45842019-02-13 13:33:03 -0800185 EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_));
Dominik Laskowski98041832019-08-01 18:35:59 -0700186 EXPECT_CALL(*mEventThread, createEventConnection(_, _))
187 .WillOnce(Return(new EventThreadConnection(mEventThread, ResyncCallback(),
188 ISurfaceComposer::eConfigChangedSuppress)));
189
Ana Krulecafb45842019-02-13 13:33:03 -0800190 EXPECT_CALL(*mSFEventThread, registerDisplayEventConnection(_));
Dominik Laskowski98041832019-08-01 18:35:59 -0700191 EXPECT_CALL(*mSFEventThread, createEventConnection(_, _))
192 .WillOnce(Return(new EventThreadConnection(mSFEventThread, ResyncCallback(),
193 ISurfaceComposer::eConfigChangedSuppress)));
Ana Krulecafb45842019-02-13 13:33:03 -0800194
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700195 mFlinger.setupScheduler(std::unique_ptr<DispSync>(mPrimaryDispSync),
196 std::unique_ptr<EventControlThread>(mEventControlThread),
197 std::unique_ptr<EventThread>(mEventThread),
198 std::unique_ptr<EventThread>(mSFEventThread));
Ana Krulecafb45842019-02-13 13:33:03 -0800199}
200
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800201void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
202 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800203 EXPECT_CALL(*mComposer, getCapabilities())
204 .WillOnce(Return(std::vector<IComposer::Capability>()));
205 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
206 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -0800207
Lloyd Piquee39cad22017-12-20 17:01:29 -0800208 Mock::VerifyAndClear(mComposer);
209}
210
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800211void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800212 // This setup is only expected once per test.
213 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
214
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800215 mConsumer = new mock::GraphicBufferConsumer();
216 mProducer = new mock::GraphicBufferProducer();
217
218 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
219 *outProducer = mProducer;
220 *outConsumer = mConsumer;
221 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800222}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800223
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800224void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800225 // This setup is only expected once per test.
226 ASSERT_TRUE(mNativeWindowSurface == nullptr);
227
Lloyd Pique22098362018-09-13 11:46:49 -0700228 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800229
Lloyd Pique22098362018-09-13 11:46:49 -0700230 mFlinger.setCreateNativeWindowSurface([this](auto) {
231 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
232 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800233}
234
Dominik Laskowski075d3172018-05-24 15:50:06 -0700235bool DisplayTransactionTest::hasPhysicalHwcDisplay(hwc2_display_t hwcDisplayId) {
236 return mFlinger.mutableHwcPhysicalDisplayIdMap().count(hwcDisplayId) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800237}
238
239bool DisplayTransactionTest::hasTransactionFlagSet(int flag) {
240 return mFlinger.mutableTransactionFlags() & flag;
241}
242
243bool DisplayTransactionTest::hasDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700244 return mFlinger.mutableDisplays().count(displayToken) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800245}
246
247sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700248 return mFlinger.mutableDisplays()[displayToken];
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800249}
250
251bool DisplayTransactionTest::hasCurrentDisplayState(sp<IBinder> displayToken) {
252 return mFlinger.mutableCurrentState().displays.indexOfKey(displayToken) >= 0;
253}
254
255const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(sp<IBinder> displayToken) {
256 return mFlinger.mutableCurrentState().displays.valueFor(displayToken);
257}
258
259bool DisplayTransactionTest::hasDrawingDisplayState(sp<IBinder> displayToken) {
260 return mFlinger.mutableDrawingState().displays.indexOfKey(displayToken) >= 0;
261}
262
263const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(sp<IBinder> displayToken) {
264 return mFlinger.mutableDrawingState().displays.valueFor(displayToken);
265}
266
267/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800268 *
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800269 */
270
Dominik Laskowski075d3172018-05-24 15:50:06 -0700271template <typename PhysicalDisplay>
272struct PhysicalDisplayId {};
273
Dominik Laskowski34157762018-10-31 13:07:19 -0700274template <DisplayId::Type displayId>
275using VirtualDisplayId = std::integral_constant<DisplayId::Type, displayId>;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700276
277struct NoDisplayId {};
278
279template <typename>
280struct IsPhysicalDisplayId : std::bool_constant<false> {};
281
282template <typename PhysicalDisplay>
283struct IsPhysicalDisplayId<PhysicalDisplayId<PhysicalDisplay>> : std::bool_constant<true> {};
284
285template <typename>
286struct DisplayIdGetter;
287
288template <typename PhysicalDisplay>
289struct DisplayIdGetter<PhysicalDisplayId<PhysicalDisplay>> {
290 static std::optional<DisplayId> get() {
291 if (!PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
292 return getFallbackDisplayId(static_cast<bool>(PhysicalDisplay::PRIMARY)
293 ? HWC_DISPLAY_PRIMARY
294 : HWC_DISPLAY_EXTERNAL);
295 }
296
297 const auto info =
298 parseDisplayIdentificationData(PhysicalDisplay::PORT,
299 PhysicalDisplay::GET_IDENTIFICATION_DATA());
300 return info ? std::make_optional(info->id) : std::nullopt;
301 }
302};
303
Dominik Laskowski34157762018-10-31 13:07:19 -0700304template <DisplayId::Type displayId>
Dominik Laskowski075d3172018-05-24 15:50:06 -0700305struct DisplayIdGetter<VirtualDisplayId<displayId>> {
Dominik Laskowski34157762018-10-31 13:07:19 -0700306 static std::optional<DisplayId> get() { return DisplayId{displayId}; }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700307};
308
309template <>
310struct DisplayIdGetter<NoDisplayId> {
311 static std::optional<DisplayId> get() { return {}; }
312};
313
314// DisplayIdType can be:
315// 1) PhysicalDisplayId<...> for generated ID of physical display backed by HWC.
316// 2) VirtualDisplayId<...> for hard-coded ID of virtual display backed by HWC.
317// 3) NoDisplayId for virtual display without HWC backing.
318template <typename DisplayIdType, int width, int height, Critical critical, Async async,
319 Secure secure, Primary primary, int grallocUsage>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800320struct DisplayVariant {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700321 using DISPLAY_ID = DisplayIdGetter<DisplayIdType>;
322
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800323 // The display width and height
324 static constexpr int WIDTH = width;
325 static constexpr int HEIGHT = height;
326
327 static constexpr int GRALLOC_USAGE = grallocUsage;
328
Dominik Laskowski075d3172018-05-24 15:50:06 -0700329 // Whether the display is virtual or physical
330 static constexpr Virtual VIRTUAL =
331 IsPhysicalDisplayId<DisplayIdType>{} ? Virtual::FALSE : Virtual::TRUE;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800332
333 // When creating native window surfaces for the framebuffer, whether those should be critical
334 static constexpr Critical CRITICAL = critical;
335
336 // When creating native window surfaces for the framebuffer, whether those should be async
337 static constexpr Async ASYNC = async;
338
339 // Whether the display should be treated as secure
340 static constexpr Secure SECURE = secure;
341
Dominik Laskowski075d3172018-05-24 15:50:06 -0700342 // Whether the display is primary
343 static constexpr Primary PRIMARY = primary;
344
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800345 static auto makeFakeExistingDisplayInjector(DisplayTransactionTest* test) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700346 auto injector =
347 FakeDisplayDeviceInjector(test->mFlinger, DISPLAY_ID::get(),
348 static_cast<bool>(VIRTUAL), static_cast<bool>(PRIMARY));
349
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800350 injector.setSecure(static_cast<bool>(SECURE));
Alec Mouriba013fa2018-10-16 12:43:11 -0700351 injector.setNativeWindow(test->mNativeWindow);
352
353 // Creating a DisplayDevice requires getting default dimensions from the
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800354 // native window along with some other initial setup.
Alec Mouriba013fa2018-10-16 12:43:11 -0700355 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
356 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
357 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
358 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800359 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT))
360 .WillRepeatedly(Return(0));
361 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT))
362 .WillRepeatedly(Return(0));
363 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64))
364 .WillRepeatedly(Return(0));
chaviw8beb4142019-04-11 13:09:05 -0700365 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT))
366 .WillRepeatedly(Return(0));
367
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800368 return injector;
369 }
370
371 // Called by tests to set up any native window creation call expectations.
372 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
373 EXPECT_CALL(*test->mNativeWindowSurface, getNativeWindow())
374 .WillOnce(Return(test->mNativeWindow));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800375
Alec Mouriba013fa2018-10-16 12:43:11 -0700376 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
377 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
378 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
379 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800380 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT))
381 .WillRepeatedly(Return(0));
382 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT))
383 .WillRepeatedly(Return(0));
384 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64))
385 .WillRepeatedly(Return(0));
chaviw8beb4142019-04-11 13:09:05 -0700386 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT))
387 .WillRepeatedly(Return(0));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800388 }
389
390 static void setupFramebufferConsumerBufferQueueCallExpectations(DisplayTransactionTest* test) {
391 EXPECT_CALL(*test->mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR));
392 EXPECT_CALL(*test->mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR));
393 EXPECT_CALL(*test->mConsumer, setConsumerUsageBits(GRALLOC_USAGE))
394 .WillRepeatedly(Return(NO_ERROR));
395 EXPECT_CALL(*test->mConsumer, setDefaultBufferSize(WIDTH, HEIGHT))
396 .WillRepeatedly(Return(NO_ERROR));
397 EXPECT_CALL(*test->mConsumer, setMaxAcquiredBufferCount(_))
398 .WillRepeatedly(Return(NO_ERROR));
399 }
400
401 static void setupFramebufferProducerBufferQueueCallExpectations(DisplayTransactionTest* test) {
402 EXPECT_CALL(*test->mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return());
403 }
404};
405
Dominik Laskowski075d3172018-05-24 15:50:06 -0700406template <hwc2_display_t hwcDisplayId, HWC2::DisplayType hwcDisplayType, typename DisplayVariant,
407 typename PhysicalDisplay = void>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800408struct HwcDisplayVariant {
409 // The display id supplied by the HWC
410 static constexpr hwc2_display_t HWC_DISPLAY_ID = hwcDisplayId;
411
412 // The HWC display type
413 static constexpr HWC2::DisplayType HWC_DISPLAY_TYPE = hwcDisplayType;
414
415 // The HWC active configuration id
Lloyd Pique3c085a02018-05-09 19:38:32 -0700416 static constexpr int HWC_ACTIVE_CONFIG_ID = 2001;
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700417 static constexpr int INIT_POWER_MODE = HWC_POWER_MODE_NORMAL;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800418
419 static void injectPendingHotplugEvent(DisplayTransactionTest* test,
420 HWC2::Connection connection) {
421 test->mFlinger.mutablePendingHotplugEvents().emplace_back(
422 HotplugEvent{HWC_DISPLAY_ID, connection});
423 }
424
425 // Called by tests to inject a HWC display setup
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800426 static void injectHwcDisplayWithNoDefaultCapabilities(DisplayTransactionTest* test) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700427 const auto displayId = DisplayVariant::DISPLAY_ID::get();
428 ASSERT_TRUE(displayId);
429 FakeHwcDisplayInjector(*displayId, HWC_DISPLAY_TYPE,
430 static_cast<bool>(DisplayVariant::PRIMARY))
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800431 .setHwcDisplayId(HWC_DISPLAY_ID)
432 .setWidth(DisplayVariant::WIDTH)
433 .setHeight(DisplayVariant::HEIGHT)
434 .setActiveConfig(HWC_ACTIVE_CONFIG_ID)
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700435 .setPowerMode(INIT_POWER_MODE)
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800436 .inject(&test->mFlinger, test->mComposer);
437 }
438
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800439 // Called by tests to inject a HWC display setup
440 static void injectHwcDisplay(DisplayTransactionTest* test) {
441 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(HWC_DISPLAY_ID, _))
442 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hwc2::DisplayCapability>({})),
443 Return(Error::NONE)));
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700444 EXPECT_CALL(*test->mComposer,
445 setPowerMode(HWC_DISPLAY_ID,
446 static_cast<Hwc2::IComposerClient::PowerMode>(INIT_POWER_MODE)))
447 .WillOnce(Return(Error::NONE));
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800448 injectHwcDisplayWithNoDefaultCapabilities(test);
449 }
450
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800451 static void setupHwcHotplugCallExpectations(DisplayTransactionTest* test) {
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800452 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
453 EXPECT_CALL(*test->mComposer, getDisplayConfigs(HWC_DISPLAY_ID, _))
454 .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{HWC_ACTIVE_CONFIG_ID}),
455 Return(Error::NONE)));
456 EXPECT_CALL(*test->mComposer,
457 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
458 IComposerClient::Attribute::WIDTH, _))
459 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::WIDTH), Return(Error::NONE)));
460 EXPECT_CALL(*test->mComposer,
461 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
462 IComposerClient::Attribute::HEIGHT, _))
463 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::HEIGHT), Return(Error::NONE)));
464 EXPECT_CALL(*test->mComposer,
465 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
466 IComposerClient::Attribute::VSYNC_PERIOD, _))
467 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE)));
468 EXPECT_CALL(*test->mComposer,
469 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
470 IComposerClient::Attribute::DPI_X, _))
471 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
472 EXPECT_CALL(*test->mComposer,
473 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
474 IComposerClient::Attribute::DPI_Y, _))
475 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
Ady Abraham7159f572019-10-11 11:10:18 -0700476 EXPECT_CALL(*test->mComposer,
477 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
478 IComposerClient::Attribute::CONFIG_GROUP, _))
479 .WillOnce(DoAll(SetArgPointee<3>(-1), Return(Error::NONE)));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700480
481 if (PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
482 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
483 .WillOnce(DoAll(SetArgPointee<1>(PhysicalDisplay::PORT),
484 SetArgPointee<2>(PhysicalDisplay::GET_IDENTIFICATION_DATA()),
485 Return(Error::NONE)));
486 } else {
487 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
488 .WillOnce(Return(Error::UNSUPPORTED));
489 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800490 }
491
492 // Called by tests to set up HWC call expectations
493 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
494 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY_ID, _))
Lloyd Pique3c085a02018-05-09 19:38:32 -0700495 .WillRepeatedly(DoAll(SetArgPointee<1>(HWC_ACTIVE_CONFIG_ID), Return(Error::NONE)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800496 }
497};
498
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800499// Physical displays are expected to be synchronous, secure, and have a HWC display for output.
500constexpr uint32_t GRALLOC_USAGE_PHYSICAL_DISPLAY =
501 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
502
Dominik Laskowski075d3172018-05-24 15:50:06 -0700503template <hwc2_display_t hwcDisplayId, typename PhysicalDisplay, int width, int height,
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800504 Critical critical>
505struct PhysicalDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700506 : DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height, critical, Async::FALSE,
507 Secure::TRUE, PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
508 HwcDisplayVariant<hwcDisplayId, HWC2::DisplayType::Physical,
509 DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height,
510 critical, Async::FALSE, Secure::TRUE,
511 PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
512 PhysicalDisplay> {};
513
514template <bool hasIdentificationData>
515struct PrimaryDisplay {
516 static constexpr Primary PRIMARY = Primary::TRUE;
517 static constexpr uint8_t PORT = 255;
518 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
519 static constexpr auto GET_IDENTIFICATION_DATA = getInternalEdid;
520};
521
522template <bool hasIdentificationData>
523struct ExternalDisplay {
524 static constexpr Primary PRIMARY = Primary::FALSE;
525 static constexpr uint8_t PORT = 254;
526 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
527 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
528};
529
530struct TertiaryDisplay {
531 static constexpr Primary PRIMARY = Primary::FALSE;
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800532 static constexpr uint8_t PORT = 253;
533 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700534};
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800535
536// A primary display is a physical display that is critical
537using PrimaryDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700538 PhysicalDisplayVariant<1001, PrimaryDisplay<false>, 3840, 2160, Critical::TRUE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800539
540// An external display is physical display that is not critical.
541using ExternalDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700542 PhysicalDisplayVariant<1002, ExternalDisplay<false>, 1920, 1280, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800543
544using TertiaryDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700545 PhysicalDisplayVariant<1003, TertiaryDisplay, 1600, 1200, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800546
547// A virtual display not supported by the HWC.
548constexpr uint32_t GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY = 0;
549
550template <int width, int height, Secure secure>
551struct NonHwcVirtualDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700552 : DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
553 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY> {
554 using Base = DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
555 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>;
556
557 static void injectHwcDisplay(DisplayTransactionTest*) {}
558
559 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
560 EXPECT_CALL(*test->mComposer, getActiveConfig(_, _)).Times(0);
561 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800562
563 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
564 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
565 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
566 }
567};
568
569// A virtual display supported by the HWC.
570constexpr uint32_t GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY = GRALLOC_USAGE_HW_COMPOSER;
571
572template <int width, int height, Secure secure>
573struct HwcVirtualDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700574 : DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE, secure,
575 Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>,
576 HwcDisplayVariant<
577 1010, HWC2::DisplayType::Virtual,
578 DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
579 secure, Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>> {
580 using Base = DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
581 secure, Primary::FALSE, GRALLOC_USAGE_HW_COMPOSER>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800582 using Self = HwcVirtualDisplayVariant<width, height, secure>;
583
584 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
585 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
586 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
587 }
588
589 static void setupHwcVirtualDisplayCreationCallExpectations(DisplayTransactionTest* test) {
590 EXPECT_CALL(*test->mComposer, createVirtualDisplay(Base::WIDTH, Base::HEIGHT, _, _))
591 .WillOnce(DoAll(SetArgPointee<3>(Self::HWC_DISPLAY_ID), Return(Error::NONE)));
592 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
593 }
594};
595
596// For this variant, SurfaceFlinger should not configure itself with wide
597// display support, so the display should not be configured for wide-color
598// support.
599struct WideColorSupportNotConfiguredVariant {
600 static constexpr bool WIDE_COLOR_SUPPORTED = false;
601
602 static void injectConfigChange(DisplayTransactionTest* test) {
603 test->mFlinger.mutableHasWideColorDisplay() = false;
Peiyong Lin13effd12018-07-24 17:01:47 -0700604 test->mFlinger.mutableUseColorManagement() = false;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800605 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800606 }
607
608 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
609 EXPECT_CALL(*test->mComposer, getColorModes(_, _)).Times(0);
610 EXPECT_CALL(*test->mComposer, getRenderIntents(_, _, _)).Times(0);
611 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
612 }
613};
614
615// For this variant, SurfaceFlinger should configure itself with wide display
616// support, and the display should respond with an non-empty list of supported
617// color modes. Wide-color support should be configured.
618template <typename Display>
619struct WideColorP3ColorimetricSupportedVariant {
620 static constexpr bool WIDE_COLOR_SUPPORTED = true;
621
622 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700623 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800624 test->mFlinger.mutableHasWideColorDisplay() = true;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800625 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::kUnmanaged;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800626 }
627
628 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Lloyd Pique86fa3db2019-02-04 18:46:01 -0800629 EXPECT_CALL(*test->mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_DATASPACE)).Times(1);
630
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800631 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
632 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>({ColorMode::DISPLAY_P3})),
633 Return(Error::NONE)));
634 EXPECT_CALL(*test->mComposer,
635 getRenderIntents(Display::HWC_DISPLAY_ID, ColorMode::DISPLAY_P3, _))
636 .WillOnce(DoAll(SetArgPointee<2>(
637 std::vector<RenderIntent>({RenderIntent::COLORIMETRIC})),
638 Return(Error::NONE)));
639 EXPECT_CALL(*test->mComposer,
640 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::SRGB,
641 RenderIntent::COLORIMETRIC))
642 .WillOnce(Return(Error::NONE));
643 }
644};
645
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800646// For this variant, SurfaceFlinger should configure itself with wide display
647// support, but the display should respond with an empty list of supported color
648// modes. Wide-color support for the display should not be configured.
649template <typename Display>
650struct WideColorNotSupportedVariant {
651 static constexpr bool WIDE_COLOR_SUPPORTED = false;
652
653 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700654 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800655 test->mFlinger.mutableHasWideColorDisplay() = true;
656 }
657
658 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
659 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
660 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>()), Return(Error::NONE)));
Chia-I Wu614e1422018-05-23 02:17:03 -0700661 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800662 }
663};
664
665// For this variant, the display is not a HWC display, so no HDR support should
666// be configured.
667struct NonHwcDisplayHdrSupportVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800668 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800669 static constexpr bool HDR10_SUPPORTED = false;
670 static constexpr bool HDR_HLG_SUPPORTED = false;
671 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
672 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
673 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
674 }
675};
676
Valerie Haue9e843a2018-12-18 13:39:23 -0800677template <typename Display>
678struct Hdr10PlusSupportedVariant {
679 static constexpr bool HDR10_PLUS_SUPPORTED = true;
680 static constexpr bool HDR10_SUPPORTED = true;
681 static constexpr bool HDR_HLG_SUPPORTED = false;
682 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
683 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
684 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _))
685 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({
686 Hdr::HDR10_PLUS,
687 Hdr::HDR10,
688 })),
689 Return(Error::NONE)));
690 }
691};
692
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800693// For this variant, the composer should respond with a non-empty list of HDR
694// modes containing HDR10, so HDR10 support should be configured.
695template <typename Display>
696struct Hdr10SupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800697 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800698 static constexpr bool HDR10_SUPPORTED = true;
699 static constexpr bool HDR_HLG_SUPPORTED = false;
700 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
701 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
702 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
703 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HDR10})),
704 Return(Error::NONE)));
705 }
706};
707
708// For this variant, the composer should respond with a non-empty list of HDR
709// modes containing HLG, so HLG support should be configured.
710template <typename Display>
711struct HdrHlgSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800712 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800713 static constexpr bool HDR10_SUPPORTED = false;
714 static constexpr bool HDR_HLG_SUPPORTED = true;
715 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
716 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
717 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
718 .WillOnce(
719 DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HLG})), Return(Error::NONE)));
720 }
721};
722
723// For this variant, the composer should respond with a non-empty list of HDR
724// modes containing DOLBY_VISION, so DOLBY_VISION support should be configured.
725template <typename Display>
726struct HdrDolbyVisionSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800727 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800728 static constexpr bool HDR10_SUPPORTED = false;
729 static constexpr bool HDR_HLG_SUPPORTED = false;
730 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = true;
731 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
732 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
733 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::DOLBY_VISION})),
734 Return(Error::NONE)));
735 }
736};
737
738// For this variant, the composer should respond with am empty list of HDR
739// modes, so no HDR support should be configured.
740template <typename Display>
741struct HdrNotSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800742 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800743 static constexpr bool HDR10_SUPPORTED = false;
744 static constexpr bool HDR_HLG_SUPPORTED = false;
745 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
746 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
747 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
748 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
749 }
750};
751
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700752struct NonHwcPerFrameMetadataSupportVariant {
753 static constexpr int PER_FRAME_METADATA_KEYS = 0;
754 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800755 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(_)).Times(0);
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700756 }
757};
758
759template <typename Display>
760struct NoPerFrameMetadataSupportVariant {
761 static constexpr int PER_FRAME_METADATA_KEYS = 0;
762 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800763 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
764 .WillOnce(Return(std::vector<PerFrameMetadataKey>()));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700765 }
766};
767
768template <typename Display>
769struct Smpte2086PerFrameMetadataSupportVariant {
770 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::SMPTE2086;
771 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800772 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
773 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Valerie Haue9e843a2018-12-18 13:39:23 -0800774 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X,
775 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y,
776 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X,
777 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y,
778 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X,
779 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y,
780 PerFrameMetadataKey::WHITE_POINT_X,
781 PerFrameMetadataKey::WHITE_POINT_Y,
782 PerFrameMetadataKey::MAX_LUMINANCE,
783 PerFrameMetadataKey::MIN_LUMINANCE,
784 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700785 }
786};
787
788template <typename Display>
789struct Cta861_3_PerFrameMetadataSupportVariant {
790 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::CTA861_3;
791 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800792 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
793 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Valerie Haue9e843a2018-12-18 13:39:23 -0800794 PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL,
795 PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL,
796 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700797 }
798};
799
Valerie Haue9e843a2018-12-18 13:39:23 -0800800template <typename Display>
801struct Hdr10_Plus_PerFrameMetadataSupportVariant {
802 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::HDR10PLUS;
803 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
804 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
805 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
806 PerFrameMetadataKey::HDR10_PLUS_SEI,
807 })));
808 }
809};
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800810/* ------------------------------------------------------------------------
811 * Typical display configurations to test
812 */
813
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700814template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy,
815 typename PerFrameMetadataSupportPolicy>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800816struct Case {
817 using Display = DisplayPolicy;
818 using WideColorSupport = WideColorSupportPolicy;
819 using HdrSupport = HdrSupportPolicy;
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700820 using PerFrameMetadataSupport = PerFrameMetadataSupportPolicy;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800821};
822
823using SimplePrimaryDisplayCase =
824 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700825 HdrNotSupportedVariant<PrimaryDisplayVariant>,
826 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800827using SimpleExternalDisplayCase =
828 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700829 HdrNotSupportedVariant<ExternalDisplayVariant>,
830 NoPerFrameMetadataSupportVariant<ExternalDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800831using SimpleTertiaryDisplayCase =
832 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700833 HdrNotSupportedVariant<TertiaryDisplayVariant>,
834 NoPerFrameMetadataSupportVariant<TertiaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800835using NonHwcVirtualDisplayCase =
836 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700837 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant,
838 NonHwcPerFrameMetadataSupportVariant>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800839using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
840using HwcVirtualDisplayCase =
841 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
Lloyd Pique438e9e72018-09-04 18:06:08 -0700842 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>,
tangrobin6753a022018-08-10 10:58:54 +0800843 NoPerFrameMetadataSupportVariant<SimpleHwcVirtualDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800844using WideColorP3ColorimetricDisplayCase =
845 Case<PrimaryDisplayVariant, WideColorP3ColorimetricSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700846 HdrNotSupportedVariant<PrimaryDisplayVariant>,
847 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Valerie Haue9e843a2018-12-18 13:39:23 -0800848using Hdr10PlusDisplayCase =
849 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
850 Hdr10SupportedVariant<PrimaryDisplayVariant>,
851 Hdr10_Plus_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800852using Hdr10DisplayCase =
853 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700854 Hdr10SupportedVariant<PrimaryDisplayVariant>,
855 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800856using HdrHlgDisplayCase =
857 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700858 HdrHlgSupportedVariant<PrimaryDisplayVariant>,
859 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800860using HdrDolbyVisionDisplayCase =
861 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700862 HdrDolbyVisionSupportedVariant<PrimaryDisplayVariant>,
863 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
864using HdrSmpte2086DisplayCase =
865 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
866 HdrNotSupportedVariant<PrimaryDisplayVariant>,
867 Smpte2086PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
868using HdrCta861_3_DisplayCase =
869 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
870 HdrNotSupportedVariant<PrimaryDisplayVariant>,
871 Cta861_3_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Dominik Laskowskif07b85b2018-06-11 12:49:15 -0700872
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800873/* ------------------------------------------------------------------------
Lloyd Pique6cf11032018-01-22 18:57:44 -0800874 *
875 * SurfaceFlinger::onHotplugReceived
876 */
877
878TEST_F(DisplayTransactionTest, hotplugEnqueuesEventsForDisplayTransaction) {
879 constexpr int currentSequenceId = 123;
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700880 constexpr hwc2_display_t hwcDisplayId1 = 456;
881 constexpr hwc2_display_t hwcDisplayId2 = 654;
Lloyd Pique6cf11032018-01-22 18:57:44 -0800882
883 // --------------------------------------------------------------------
884 // Preconditions
885
886 // Set the current sequence id for accepted events
887 mFlinger.mutableComposerSequenceId() = currentSequenceId;
888
889 // Set the main thread id so that the current thread does not appear to be
890 // the main thread.
891 mFlinger.mutableMainThreadId() = std::thread::id();
892
893 // --------------------------------------------------------------------
894 // Call Expectations
895
896 // We expect invalidate() to be invoked once to trigger display transaction
897 // processing.
898 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
899
900 // --------------------------------------------------------------------
901 // Invocation
902
903 // Simulate two hotplug events (a connect and a disconnect)
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700904 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId1, HWC2::Connection::Connected);
905 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId2, HWC2::Connection::Disconnected);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800906
907 // --------------------------------------------------------------------
908 // Postconditions
909
910 // The display transaction needed flag should be set.
911 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
912
913 // All events should be in the pending event queue.
914 const auto& pendingEvents = mFlinger.mutablePendingHotplugEvents();
915 ASSERT_EQ(2u, pendingEvents.size());
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700916 EXPECT_EQ(hwcDisplayId1, pendingEvents[0].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800917 EXPECT_EQ(HWC2::Connection::Connected, pendingEvents[0].connection);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700918 EXPECT_EQ(hwcDisplayId2, pendingEvents[1].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800919 EXPECT_EQ(HWC2::Connection::Disconnected, pendingEvents[1].connection);
920}
921
922TEST_F(DisplayTransactionTest, hotplugDiscardsUnexpectedEvents) {
923 constexpr int currentSequenceId = 123;
924 constexpr int otherSequenceId = 321;
925 constexpr hwc2_display_t displayId = 456;
926
927 // --------------------------------------------------------------------
928 // Preconditions
929
930 // Set the current sequence id for accepted events
931 mFlinger.mutableComposerSequenceId() = currentSequenceId;
932
933 // Set the main thread id so that the current thread does not appear to be
934 // the main thread.
935 mFlinger.mutableMainThreadId() = std::thread::id();
936
937 // --------------------------------------------------------------------
938 // Call Expectations
939
940 // We do not expect any calls to invalidate().
941 EXPECT_CALL(*mMessageQueue, invalidate()).Times(0);
942
943 // --------------------------------------------------------------------
944 // Invocation
945
946 // Call with an unexpected sequence id
947 mFlinger.onHotplugReceived(otherSequenceId, displayId, HWC2::Connection::Invalid);
948
949 // --------------------------------------------------------------------
950 // Postconditions
951
952 // The display transaction needed flag should not be set
953 EXPECT_FALSE(hasTransactionFlagSet(eDisplayTransactionNeeded));
954
955 // There should be no pending events
956 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
957}
958
959TEST_F(DisplayTransactionTest, hotplugProcessesEnqueuedEventsIfCalledOnMainThread) {
960 constexpr int currentSequenceId = 123;
961 constexpr hwc2_display_t displayId1 = 456;
962
963 // --------------------------------------------------------------------
964 // Note:
965 // --------------------------------------------------------------------
966 // This test case is a bit tricky. We want to verify that
967 // onHotplugReceived() calls processDisplayHotplugEventsLocked(), but we
968 // don't really want to provide coverage for everything the later function
969 // does as there are specific tests for it.
970 // --------------------------------------------------------------------
971
972 // --------------------------------------------------------------------
973 // Preconditions
974
975 // Set the current sequence id for accepted events
976 mFlinger.mutableComposerSequenceId() = currentSequenceId;
977
978 // Set the main thread id so that the current thread does appear to be the
979 // main thread.
980 mFlinger.mutableMainThreadId() = std::this_thread::get_id();
981
982 // --------------------------------------------------------------------
983 // Call Expectations
984
985 // We expect invalidate() to be invoked once to trigger display transaction
986 // processing.
987 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
988
989 // --------------------------------------------------------------------
990 // Invocation
991
992 // Simulate a disconnect on a display id that is not connected. This should
993 // be enqueued by onHotplugReceived(), and dequeued by
994 // processDisplayHotplugEventsLocked(), but then ignored as invalid.
995 mFlinger.onHotplugReceived(currentSequenceId, displayId1, HWC2::Connection::Disconnected);
996
997 // --------------------------------------------------------------------
998 // Postconditions
999
1000 // The display transaction needed flag should be set.
1001 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1002
1003 // There should be no event queued on return, as it should have been
1004 // processed.
1005 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
1006}
1007
1008/* ------------------------------------------------------------------------
Lloyd Piquea482f992018-01-22 19:00:34 -08001009 * SurfaceFlinger::createDisplay
1010 */
1011
1012TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForNonsecureDisplay) {
1013 const String8 name("virtual.test");
1014
1015 // --------------------------------------------------------------------
1016 // Call Expectations
1017
1018 // The call should notify the interceptor that a display was created.
1019 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
1020
1021 // --------------------------------------------------------------------
1022 // Invocation
1023
1024 sp<IBinder> displayToken = mFlinger.createDisplay(name, false);
1025
1026 // --------------------------------------------------------------------
1027 // Postconditions
1028
1029 // The display should have been added to the current state
1030 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1031 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001032 EXPECT_TRUE(display.isVirtual());
1033 EXPECT_FALSE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -08001034 EXPECT_EQ(name.string(), display.displayName);
1035
1036 // --------------------------------------------------------------------
1037 // Cleanup conditions
1038
1039 // Destroying the display invalidates the display state.
1040 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1041}
1042
1043TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForSecureDisplay) {
1044 const String8 name("virtual.test");
1045
1046 // --------------------------------------------------------------------
1047 // Call Expectations
1048
1049 // The call should notify the interceptor that a display was created.
1050 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
1051
1052 // --------------------------------------------------------------------
1053 // Invocation
1054
1055 sp<IBinder> displayToken = mFlinger.createDisplay(name, true);
1056
1057 // --------------------------------------------------------------------
1058 // Postconditions
1059
1060 // The display should have been added to the current state
1061 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1062 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001063 EXPECT_TRUE(display.isVirtual());
1064 EXPECT_TRUE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -08001065 EXPECT_EQ(name.string(), display.displayName);
1066
1067 // --------------------------------------------------------------------
1068 // Cleanup conditions
1069
1070 // Destroying the display invalidates the display state.
1071 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1072}
1073
1074/* ------------------------------------------------------------------------
1075 * SurfaceFlinger::destroyDisplay
1076 */
1077
1078TEST_F(DisplayTransactionTest, destroyDisplayClearsCurrentStateForDisplay) {
1079 using Case = NonHwcVirtualDisplayCase;
1080
1081 // --------------------------------------------------------------------
1082 // Preconditions
1083
1084 // A virtual display exists
1085 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1086 existing.inject();
1087
1088 // --------------------------------------------------------------------
1089 // Call Expectations
1090
1091 // The call should notify the interceptor that a display was created.
1092 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
1093
1094 // Destroying the display invalidates the display state.
1095 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1096
1097 // --------------------------------------------------------------------
1098 // Invocation
1099
1100 mFlinger.destroyDisplay(existing.token());
1101
1102 // --------------------------------------------------------------------
1103 // Postconditions
1104
1105 // The display should have been removed from the current state
1106 EXPECT_FALSE(hasCurrentDisplayState(existing.token()));
1107
1108 // Ths display should still exist in the drawing state
1109 EXPECT_TRUE(hasDrawingDisplayState(existing.token()));
1110
1111 // The display transaction needed flasg should be set
1112 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1113}
1114
1115TEST_F(DisplayTransactionTest, destroyDisplayHandlesUnknownDisplay) {
1116 // --------------------------------------------------------------------
1117 // Preconditions
1118
1119 sp<BBinder> displayToken = new BBinder();
1120
1121 // --------------------------------------------------------------------
1122 // Invocation
1123
1124 mFlinger.destroyDisplay(displayToken);
1125}
1126
1127/* ------------------------------------------------------------------------
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001128 * SurfaceFlinger::resetDisplayState
1129 */
1130
1131TEST_F(DisplayTransactionTest, resetDisplayStateClearsState) {
1132 using Case = NonHwcVirtualDisplayCase;
1133
1134 // --------------------------------------------------------------------
1135 // Preconditions
1136
1137 // vsync is enabled and available
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -07001138 mFlinger.scheduler()->mutablePrimaryHWVsyncEnabled() = true;
1139 mFlinger.scheduler()->mutableHWVsyncAvailable() = true;
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001140
1141 // A display exists
1142 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1143 existing.inject();
1144
1145 // --------------------------------------------------------------------
1146 // Call Expectations
1147
1148 // The call disable vsyncs
1149 EXPECT_CALL(*mEventControlThread, setVsyncEnabled(false)).Times(1);
1150
Lloyd Pique41be5d22018-06-21 13:11:48 -07001151 // The call ends any display resyncs
1152 EXPECT_CALL(*mPrimaryDispSync, endResync()).Times(1);
1153
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001154 // --------------------------------------------------------------------
1155 // Invocation
1156
1157 mFlinger.resetDisplayState();
1158
1159 // --------------------------------------------------------------------
1160 // Postconditions
1161
1162 // vsyncs should be off and not available.
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -07001163 EXPECT_FALSE(mFlinger.scheduler()->mutablePrimaryHWVsyncEnabled());
1164 EXPECT_FALSE(mFlinger.scheduler()->mutableHWVsyncAvailable());
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001165
1166 // The display should have been removed from the display map.
1167 EXPECT_FALSE(hasDisplayDevice(existing.token()));
1168
1169 // The display should still exist in the current state
1170 EXPECT_TRUE(hasCurrentDisplayState(existing.token()));
1171
1172 // The display should have been removed from the drawing state
1173 EXPECT_FALSE(hasDrawingDisplayState(existing.token()));
1174}
1175
1176/* ------------------------------------------------------------------------
Valerie Hau9758ae02018-10-09 16:05:09 -07001177 * DisplayDevice::GetBestColorMode
1178 */
1179class GetBestColorModeTest : public DisplayTransactionTest {
1180public:
Dominik Laskowski34157762018-10-31 13:07:19 -07001181 static constexpr DisplayId DEFAULT_DISPLAY_ID = DisplayId{777};
Dominik Laskowski075d3172018-05-24 15:50:06 -07001182
Valerie Hau9758ae02018-10-09 16:05:09 -07001183 GetBestColorModeTest()
1184 : DisplayTransactionTest(),
Dominik Laskowski075d3172018-05-24 15:50:06 -07001185 mInjector(FakeDisplayDeviceInjector(mFlinger, DEFAULT_DISPLAY_ID, false /* isVirtual */,
1186 true /* isPrimary */)) {}
Valerie Hau9758ae02018-10-09 16:05:09 -07001187
1188 void setHasWideColorGamut(bool hasWideColorGamut) { mHasWideColorGamut = hasWideColorGamut; }
1189
1190 void addHwcColorModesMapping(ui::ColorMode colorMode,
1191 std::vector<ui::RenderIntent> renderIntents) {
1192 mHwcColorModes[colorMode] = renderIntents;
1193 }
1194
1195 void setInputDataspace(ui::Dataspace dataspace) { mInputDataspace = dataspace; }
1196
1197 void setInputRenderIntent(ui::RenderIntent renderIntent) { mInputRenderIntent = renderIntent; }
1198
1199 void getBestColorMode() {
1200 mInjector.setHwcColorModes(mHwcColorModes);
1201 mInjector.setHasWideColorGamut(mHasWideColorGamut);
Alec Mouriba013fa2018-10-16 12:43:11 -07001202 mInjector.setNativeWindow(mNativeWindow);
1203
1204 // Creating a DisplayDevice requires getting default dimensions from the
1205 // native window.
1206 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
1207 .WillRepeatedly(DoAll(SetArgPointee<1>(1080 /* arbitrary */), Return(0)));
1208 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
1209 .WillRepeatedly(DoAll(SetArgPointee<1>(1920 /* arbitrary */), Return(0)));
Lloyd Pique86fa3db2019-02-04 18:46:01 -08001210 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT)).Times(1);
1211 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT)).Times(1);
1212 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64)).Times(1);
chaviw8beb4142019-04-11 13:09:05 -07001213 EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT)).Times(1);
Valerie Hau9758ae02018-10-09 16:05:09 -07001214 auto displayDevice = mInjector.inject();
1215
Lloyd Pique3d0c02e2018-10-19 18:38:12 -07001216 displayDevice->getCompositionDisplay()
1217 ->getDisplayColorProfile()
1218 ->getBestColorMode(mInputDataspace, mInputRenderIntent, &mOutDataspace,
1219 &mOutColorMode, &mOutRenderIntent);
Valerie Hau9758ae02018-10-09 16:05:09 -07001220 }
1221
1222 ui::Dataspace mOutDataspace;
1223 ui::ColorMode mOutColorMode;
1224 ui::RenderIntent mOutRenderIntent;
1225
1226private:
1227 ui::Dataspace mInputDataspace;
1228 ui::RenderIntent mInputRenderIntent;
1229 bool mHasWideColorGamut = false;
1230 std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> mHwcColorModes;
1231 FakeDisplayDeviceInjector mInjector;
1232};
1233
1234TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeSRGB) {
1235 addHwcColorModesMapping(ui::ColorMode::SRGB,
1236 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1237 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1238 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1239 setHasWideColorGamut(true);
1240
1241 getBestColorMode();
1242
Peiyong Lin14724e62018-12-05 07:27:30 -08001243 ASSERT_EQ(ui::Dataspace::V0_SRGB, mOutDataspace);
Valerie Hau9758ae02018-10-09 16:05:09 -07001244 ASSERT_EQ(ui::ColorMode::SRGB, mOutColorMode);
1245 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1246}
1247
1248TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDisplayP3) {
1249 addHwcColorModesMapping(ui::ColorMode::DISPLAY_P3,
1250 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1251 addHwcColorModesMapping(ui::ColorMode::SRGB,
1252 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1253 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1254 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1255 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1256 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1257 setHasWideColorGamut(true);
1258
1259 getBestColorMode();
1260
1261 ASSERT_EQ(ui::Dataspace::DISPLAY_P3, mOutDataspace);
1262 ASSERT_EQ(ui::ColorMode::DISPLAY_P3, mOutColorMode);
1263 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1264}
1265
1266TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDISPLAY_BT2020) {
1267 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1268 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1269 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1270 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1271 setHasWideColorGamut(true);
1272
1273 getBestColorMode();
1274
1275 ASSERT_EQ(ui::Dataspace::DISPLAY_BT2020, mOutDataspace);
1276 ASSERT_EQ(ui::ColorMode::DISPLAY_BT2020, mOutColorMode);
1277 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1278}
1279
1280/* ------------------------------------------------------------------------
Daniel Solomon42d04562019-01-20 21:03:19 -08001281 * SurfaceFlinger::getDisplayNativePrimaries
1282 */
1283
1284class GetDisplayNativePrimaries : public DisplayTransactionTest {
1285public:
1286 GetDisplayNativePrimaries();
1287 void populateDummyDisplayNativePrimaries(ui::DisplayPrimaries& primaries);
1288 void checkDummyDisplayNativePrimaries(const ui::DisplayPrimaries& primaries);
1289
1290private:
1291 static constexpr float mStartingTestValue = 1.0f;
1292};
1293
1294GetDisplayNativePrimaries::GetDisplayNativePrimaries() {
1295 SimplePrimaryDisplayCase::Display::injectHwcDisplay(this);
1296 injectFakeNativeWindowSurfaceFactory();
1297}
1298
1299void GetDisplayNativePrimaries::populateDummyDisplayNativePrimaries(
1300 ui::DisplayPrimaries& primaries) {
1301 float startingVal = mStartingTestValue;
1302 primaries.red.X = startingVal++;
1303 primaries.red.Y = startingVal++;
1304 primaries.red.Z = startingVal++;
1305 primaries.green.X = startingVal++;
1306 primaries.green.Y = startingVal++;
1307 primaries.green.Z = startingVal++;
1308 primaries.blue.X = startingVal++;
1309 primaries.blue.Y = startingVal++;
1310 primaries.blue.Z = startingVal++;
1311 primaries.white.X = startingVal++;
1312 primaries.white.Y = startingVal++;
1313 primaries.white.Z = startingVal++;
1314}
1315
1316void GetDisplayNativePrimaries::checkDummyDisplayNativePrimaries(
1317 const ui::DisplayPrimaries& primaries) {
1318 float startingVal = mStartingTestValue;
1319 EXPECT_EQ(primaries.red.X, startingVal++);
1320 EXPECT_EQ(primaries.red.Y, startingVal++);
1321 EXPECT_EQ(primaries.red.Z, startingVal++);
1322 EXPECT_EQ(primaries.green.X, startingVal++);
1323 EXPECT_EQ(primaries.green.Y, startingVal++);
1324 EXPECT_EQ(primaries.green.Z, startingVal++);
1325 EXPECT_EQ(primaries.blue.X, startingVal++);
1326 EXPECT_EQ(primaries.blue.Y, startingVal++);
1327 EXPECT_EQ(primaries.blue.Z, startingVal++);
1328 EXPECT_EQ(primaries.white.X, startingVal++);
1329 EXPECT_EQ(primaries.white.Y, startingVal++);
1330 EXPECT_EQ(primaries.white.Z, startingVal++);
1331}
1332
1333TEST_F(GetDisplayNativePrimaries, nullDisplayToken) {
1334 ui::DisplayPrimaries primaries;
1335 EXPECT_EQ(BAD_VALUE, mFlinger.getDisplayNativePrimaries(nullptr, primaries));
1336}
1337
Daniel Solomon42d04562019-01-20 21:03:19 -08001338TEST_F(GetDisplayNativePrimaries, internalDisplayWithPrimariesData) {
1339 auto injector = SimplePrimaryDisplayCase::Display::makeFakeExistingDisplayInjector(this);
1340 injector.inject();
1341 auto internalDisplayToken = injector.token();
1342
1343 ui::DisplayPrimaries expectedPrimaries;
1344 populateDummyDisplayNativePrimaries(expectedPrimaries);
1345 mFlinger.setInternalDisplayPrimaries(expectedPrimaries);
1346
1347 ui::DisplayPrimaries primaries;
1348 EXPECT_EQ(NO_ERROR, mFlinger.getDisplayNativePrimaries(internalDisplayToken, primaries));
1349
1350 checkDummyDisplayNativePrimaries(primaries);
1351}
1352
1353TEST_F(GetDisplayNativePrimaries, notInternalDisplayToken) {
1354 sp<BBinder> notInternalDisplayToken = new BBinder();
1355
1356 ui::DisplayPrimaries primaries;
1357 populateDummyDisplayNativePrimaries(primaries);
1358 EXPECT_EQ(BAD_VALUE, mFlinger.getDisplayNativePrimaries(notInternalDisplayToken, primaries));
1359
1360 // Check primaries argument wasn't modified in case of failure
1361 checkDummyDisplayNativePrimaries(primaries);
1362}
1363
1364/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001365 * SurfaceFlinger::setupNewDisplayDeviceInternal
1366 */
1367
1368class SetupNewDisplayDeviceInternalTest : public DisplayTransactionTest {
1369public:
1370 template <typename T>
1371 void setupNewDisplayDeviceInternalTest();
1372};
1373
1374template <typename Case>
1375void SetupNewDisplayDeviceInternalTest::setupNewDisplayDeviceInternalTest() {
1376 const sp<BBinder> displayToken = new BBinder();
Lloyd Pique542307f2018-10-19 13:24:08 -07001377 const sp<compositionengine::mock::DisplaySurface> displaySurface =
1378 new compositionengine::mock::DisplaySurface();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001379 const sp<mock::GraphicBufferProducer> producer = new mock::GraphicBufferProducer();
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001380
1381 // --------------------------------------------------------------------
1382 // Preconditions
1383
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001384 // Wide color displays support is configured appropriately
1385 Case::WideColorSupport::injectConfigChange(this);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001386
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001387 // The display is setup with the HWC.
1388 Case::Display::injectHwcDisplay(this);
1389
1390 // SurfaceFlinger will use a test-controlled factory for native window
1391 // surfaces.
1392 injectFakeNativeWindowSurfaceFactory();
1393
1394 // --------------------------------------------------------------------
1395 // Call Expectations
1396
1397 // Various native window calls will be made.
1398 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
Lloyd Pique3c085a02018-05-09 19:38:32 -07001399 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001400 Case::WideColorSupport::setupComposerCallExpectations(this);
1401 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001402 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001403
1404 // --------------------------------------------------------------------
1405 // Invocation
1406
1407 DisplayDeviceState state;
Dominik Laskowski075d3172018-05-24 15:50:06 -07001408 state.displayId = static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1409 : Case::Display::DISPLAY_ID::get();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001410 state.isSecure = static_cast<bool>(Case::Display::SECURE);
1411
Dominik Laskowski075d3172018-05-24 15:50:06 -07001412 auto device =
1413 mFlinger.setupNewDisplayDeviceInternal(displayToken, Case::Display::DISPLAY_ID::get(),
1414 state, displaySurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001415
1416 // --------------------------------------------------------------------
1417 // Postconditions
1418
1419 ASSERT_TRUE(device != nullptr);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001420 EXPECT_EQ(Case::Display::DISPLAY_ID::get(), device->getId());
1421 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), device->isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001422 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001423 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001424 EXPECT_EQ(Case::Display::WIDTH, device->getWidth());
1425 EXPECT_EQ(Case::Display::HEIGHT, device->getHeight());
1426 EXPECT_EQ(Case::WideColorSupport::WIDE_COLOR_SUPPORTED, device->hasWideColorGamut());
Valerie Haue9e843a2018-12-18 13:39:23 -08001427 EXPECT_EQ(Case::HdrSupport::HDR10_PLUS_SUPPORTED, device->hasHDR10PlusSupport());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001428 EXPECT_EQ(Case::HdrSupport::HDR10_SUPPORTED, device->hasHDR10Support());
1429 EXPECT_EQ(Case::HdrSupport::HDR_HLG_SUPPORTED, device->hasHLGSupport());
1430 EXPECT_EQ(Case::HdrSupport::HDR_DOLBY_VISION_SUPPORTED, device->hasDolbyVisionSupport());
Lloyd Pique3c085a02018-05-09 19:38:32 -07001431 // Note: This is not Case::Display::HWC_ACTIVE_CONFIG_ID as the ids are
1432 // remapped, and the test only ever sets up one config. If there were an error
1433 // looking up the remapped index, device->getActiveConfig() would be -1 instead.
Ady Abraham2139f732019-11-13 18:56:40 -08001434 EXPECT_EQ(0, device->getActiveConfig().value());
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001435 EXPECT_EQ(Case::PerFrameMetadataSupport::PER_FRAME_METADATA_KEYS,
1436 device->getSupportedPerFrameMetadata());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001437}
1438
1439TEST_F(SetupNewDisplayDeviceInternalTest, createSimplePrimaryDisplay) {
1440 setupNewDisplayDeviceInternalTest<SimplePrimaryDisplayCase>();
1441}
1442
1443TEST_F(SetupNewDisplayDeviceInternalTest, createSimpleExternalDisplay) {
1444 setupNewDisplayDeviceInternalTest<SimpleExternalDisplayCase>();
1445}
1446
1447TEST_F(SetupNewDisplayDeviceInternalTest, createNonHwcVirtualDisplay) {
1448 setupNewDisplayDeviceInternalTest<NonHwcVirtualDisplayCase>();
1449}
1450
1451TEST_F(SetupNewDisplayDeviceInternalTest, createHwcVirtualDisplay) {
Dominik Laskowski075d3172018-05-24 15:50:06 -07001452 using Case = HwcVirtualDisplayCase;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001453
Dominik Laskowski075d3172018-05-24 15:50:06 -07001454 // Insert display data so that the HWC thinks it created the virtual display.
1455 const auto displayId = Case::Display::DISPLAY_ID::get();
1456 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08001457 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001458
1459 setupNewDisplayDeviceInternalTest<Case>();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001460}
1461
1462TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3Display) {
1463 setupNewDisplayDeviceInternalTest<WideColorP3ColorimetricDisplayCase>();
1464}
1465
Valerie Haue9e843a2018-12-18 13:39:23 -08001466TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10PlusDisplay) {
1467 setupNewDisplayDeviceInternalTest<Hdr10PlusDisplayCase>();
1468}
1469
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001470TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10Display) {
1471 setupNewDisplayDeviceInternalTest<Hdr10DisplayCase>();
1472}
1473
1474TEST_F(SetupNewDisplayDeviceInternalTest, createHdrHlgDisplay) {
1475 setupNewDisplayDeviceInternalTest<HdrHlgDisplayCase>();
1476}
1477
1478TEST_F(SetupNewDisplayDeviceInternalTest, createHdrDolbyVisionDisplay) {
1479 setupNewDisplayDeviceInternalTest<HdrDolbyVisionDisplayCase>();
1480}
1481
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001482TEST_F(SetupNewDisplayDeviceInternalTest, createHdrSmpte2086DisplayCase) {
1483 setupNewDisplayDeviceInternalTest<HdrSmpte2086DisplayCase>();
1484}
1485
1486TEST_F(SetupNewDisplayDeviceInternalTest, createHdrCta816_3_DisplayCase) {
1487 setupNewDisplayDeviceInternalTest<HdrCta861_3_DisplayCase>();
1488}
1489
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001490/* ------------------------------------------------------------------------
1491 * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded)
1492 */
1493
1494class HandleTransactionLockedTest : public DisplayTransactionTest {
1495public:
1496 template <typename Case>
1497 void setupCommonPreconditions();
1498
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001499 template <typename Case, bool connected>
1500 static void expectHotplugReceived(mock::EventThread*);
1501
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001502 template <typename Case>
1503 void setupCommonCallExpectationsForConnectProcessing();
1504
1505 template <typename Case>
1506 void setupCommonCallExpectationsForDisconnectProcessing();
1507
1508 template <typename Case>
1509 void processesHotplugConnectCommon();
1510
1511 template <typename Case>
1512 void ignoresHotplugConnectCommon();
1513
1514 template <typename Case>
1515 void processesHotplugDisconnectCommon();
1516
1517 template <typename Case>
1518 void verifyDisplayIsConnected(const sp<IBinder>& displayToken);
1519
1520 template <typename Case>
1521 void verifyPhysicalDisplayIsConnected();
1522
1523 void verifyDisplayIsNotConnected(const sp<IBinder>& displayToken);
1524};
1525
1526template <typename Case>
1527void HandleTransactionLockedTest::setupCommonPreconditions() {
1528 // Wide color displays support is configured appropriately
1529 Case::WideColorSupport::injectConfigChange(this);
1530
1531 // SurfaceFlinger will use a test-controlled factory for BufferQueues
1532 injectFakeBufferQueueFactory();
1533
1534 // SurfaceFlinger will use a test-controlled factory for native window
1535 // surfaces.
1536 injectFakeNativeWindowSurfaceFactory();
1537}
1538
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001539template <typename Case, bool connected>
1540void HandleTransactionLockedTest::expectHotplugReceived(mock::EventThread* eventThread) {
1541 const auto convert = [](auto physicalDisplayId) {
1542 return std::make_optional(DisplayId{physicalDisplayId});
1543 };
1544
1545 EXPECT_CALL(*eventThread,
1546 onHotplugReceived(ResultOf(convert, Case::Display::DISPLAY_ID::get()), connected))
1547 .Times(1);
1548}
1549
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001550template <typename Case>
1551void HandleTransactionLockedTest::setupCommonCallExpectationsForConnectProcessing() {
1552 Case::Display::setupHwcHotplugCallExpectations(this);
1553
1554 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1555 Case::Display::setupFramebufferProducerBufferQueueCallExpectations(this);
1556 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1557 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
1558
1559 Case::WideColorSupport::setupComposerCallExpectations(this);
1560 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001561 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001562
1563 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001564 expectHotplugReceived<Case, true>(mEventThread);
1565 expectHotplugReceived<Case, true>(mSFEventThread);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001566}
1567
1568template <typename Case>
1569void HandleTransactionLockedTest::setupCommonCallExpectationsForDisconnectProcessing() {
1570 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
Dominik Laskowski1eba0202019-01-24 09:14:40 -08001571
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001572 expectHotplugReceived<Case, false>(mEventThread);
1573 expectHotplugReceived<Case, false>(mSFEventThread);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001574}
1575
1576template <typename Case>
1577void HandleTransactionLockedTest::verifyDisplayIsConnected(const sp<IBinder>& displayToken) {
1578 // The display device should have been set up in the list of displays.
1579 ASSERT_TRUE(hasDisplayDevice(displayToken));
1580 const auto& device = getDisplayDevice(displayToken);
1581 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001582 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001583
1584 // The display should have been set up in the current display state
1585 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1586 const auto& current = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001587 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), current.isVirtual());
1588 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1589 : Case::Display::DISPLAY_ID::get(),
1590 current.displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001591
1592 // The display should have been set up in the drawing display state
1593 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1594 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001595 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
1596 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1597 : Case::Display::DISPLAY_ID::get(),
1598 draw.displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001599}
1600
1601template <typename Case>
1602void HandleTransactionLockedTest::verifyPhysicalDisplayIsConnected() {
1603 // HWComposer should have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001604 EXPECT_TRUE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001605
Dominik Laskowski075d3172018-05-24 15:50:06 -07001606 // SF should have a display token.
1607 const auto displayId = Case::Display::DISPLAY_ID::get();
1608 ASSERT_TRUE(displayId);
1609 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1610 auto& displayToken = mFlinger.mutablePhysicalDisplayTokens()[*displayId];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001611
1612 verifyDisplayIsConnected<Case>(displayToken);
1613}
1614
1615void HandleTransactionLockedTest::verifyDisplayIsNotConnected(const sp<IBinder>& displayToken) {
1616 EXPECT_FALSE(hasDisplayDevice(displayToken));
1617 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1618 EXPECT_FALSE(hasDrawingDisplayState(displayToken));
1619}
1620
1621template <typename Case>
1622void HandleTransactionLockedTest::processesHotplugConnectCommon() {
1623 // --------------------------------------------------------------------
1624 // Preconditions
1625
1626 setupCommonPreconditions<Case>();
1627
1628 // A hotplug connect event is enqueued for a display
1629 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001630
1631 // --------------------------------------------------------------------
1632 // Call Expectations
1633
1634 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillOnce(Return(false));
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001635
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001636 setupCommonCallExpectationsForConnectProcessing<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001637
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001638 // --------------------------------------------------------------------
1639 // Invocation
Lloyd Piquee39cad22017-12-20 17:01:29 -08001640
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001641 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
Lloyd Piquee39cad22017-12-20 17:01:29 -08001642
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001643 // --------------------------------------------------------------------
1644 // Postconditions
1645
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001646 verifyPhysicalDisplayIsConnected<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001647
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001648 // --------------------------------------------------------------------
1649 // Cleanup conditions
1650
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001651 EXPECT_CALL(*mComposer,
1652 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001653 .WillOnce(Return(Error::NONE));
1654 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Piquef58625d2017-12-19 13:22:33 -08001655}
1656
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001657template <typename Case>
1658void HandleTransactionLockedTest::ignoresHotplugConnectCommon() {
1659 // --------------------------------------------------------------------
1660 // Preconditions
1661
1662 setupCommonPreconditions<Case>();
1663
1664 // A hotplug connect event is enqueued for a display
1665 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1666
1667 // --------------------------------------------------------------------
1668 // Invocation
1669
1670 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1671
1672 // --------------------------------------------------------------------
1673 // Postconditions
1674
1675 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001676 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001677}
1678
1679template <typename Case>
1680void HandleTransactionLockedTest::processesHotplugDisconnectCommon() {
1681 // --------------------------------------------------------------------
1682 // Preconditions
1683
1684 setupCommonPreconditions<Case>();
1685
1686 // A hotplug disconnect event is enqueued for a display
1687 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1688
1689 // The display is already completely set up.
1690 Case::Display::injectHwcDisplay(this);
1691 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1692 existing.inject();
1693
1694 // --------------------------------------------------------------------
1695 // Call Expectations
1696
1697 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
Lloyd Pique438e9e72018-09-04 18:06:08 -07001698 EXPECT_CALL(*mComposer, getDisplayIdentificationData(Case::Display::HWC_DISPLAY_ID, _, _))
1699 .Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001700
1701 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1702
1703 // --------------------------------------------------------------------
1704 // Invocation
1705
1706 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1707
1708 // --------------------------------------------------------------------
1709 // Postconditions
1710
1711 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001712 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001713
Dominik Laskowski075d3172018-05-24 15:50:06 -07001714 // SF should not have a display token.
1715 const auto displayId = Case::Display::DISPLAY_ID::get();
1716 ASSERT_TRUE(displayId);
1717 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001718
1719 // The existing token should have been removed
1720 verifyDisplayIsNotConnected(existing.token());
1721}
1722
1723TEST_F(HandleTransactionLockedTest, processesHotplugConnectPrimaryDisplay) {
1724 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1725}
1726
1727TEST_F(HandleTransactionLockedTest,
1728 processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected) {
1729 // Inject an external display.
1730 ExternalDisplayVariant::injectHwcDisplay(this);
1731
1732 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1733}
1734
1735TEST_F(HandleTransactionLockedTest, processesHotplugConnectExternalDisplay) {
1736 // Inject a primary display.
1737 PrimaryDisplayVariant::injectHwcDisplay(this);
1738
1739 processesHotplugConnectCommon<SimpleExternalDisplayCase>();
1740}
1741
1742TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected) {
1743 // Inject both a primary and external display.
1744 PrimaryDisplayVariant::injectHwcDisplay(this);
1745 ExternalDisplayVariant::injectHwcDisplay(this);
1746
Lloyd Pique86fa3db2019-02-04 18:46:01 -08001747 // TODO: This is an unnecessary call.
1748 EXPECT_CALL(*mComposer,
1749 getDisplayIdentificationData(TertiaryDisplayVariant::HWC_DISPLAY_ID, _, _))
1750 .WillOnce(DoAll(SetArgPointee<1>(TertiaryDisplay::PORT),
1751 SetArgPointee<2>(TertiaryDisplay::GET_IDENTIFICATION_DATA()),
1752 Return(Error::NONE)));
1753
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001754 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1755
1756 ignoresHotplugConnectCommon<SimpleTertiaryDisplayCase>();
1757}
1758
1759TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfExternalForVrComposer) {
1760 // Inject a primary display.
1761 PrimaryDisplayVariant::injectHwcDisplay(this);
1762
1763 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(true));
1764
1765 ignoresHotplugConnectCommon<SimpleExternalDisplayCase>();
1766}
1767
1768TEST_F(HandleTransactionLockedTest, processHotplugDisconnectPrimaryDisplay) {
1769 processesHotplugDisconnectCommon<SimplePrimaryDisplayCase>();
1770}
1771
1772TEST_F(HandleTransactionLockedTest, processHotplugDisconnectExternalDisplay) {
1773 processesHotplugDisconnectCommon<SimpleExternalDisplayCase>();
1774}
1775
1776TEST_F(HandleTransactionLockedTest, processesHotplugConnectThenDisconnectPrimary) {
1777 using Case = SimplePrimaryDisplayCase;
1778
1779 // --------------------------------------------------------------------
1780 // Preconditions
1781
1782 setupCommonPreconditions<Case>();
1783
1784 // A hotplug connect event is enqueued for a display
1785 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1786 // A hotplug disconnect event is also enqueued for the same display
1787 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1788
1789 // --------------------------------------------------------------------
1790 // Call Expectations
1791
1792 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1793
1794 setupCommonCallExpectationsForConnectProcessing<Case>();
1795 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1796
1797 EXPECT_CALL(*mComposer,
1798 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1799 .WillOnce(Return(Error::NONE));
1800 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1801
1802 // --------------------------------------------------------------------
1803 // Invocation
1804
1805 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1806
1807 // --------------------------------------------------------------------
1808 // Postconditions
1809
1810 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001811 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001812
Dominik Laskowski075d3172018-05-24 15:50:06 -07001813 // SF should not have a display token.
1814 const auto displayId = Case::Display::DISPLAY_ID::get();
1815 ASSERT_TRUE(displayId);
1816 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001817}
1818
1819TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectThenConnectPrimary) {
1820 using Case = SimplePrimaryDisplayCase;
1821
1822 // --------------------------------------------------------------------
1823 // Preconditions
1824
1825 setupCommonPreconditions<Case>();
1826
1827 // The display is already completely set up.
1828 Case::Display::injectHwcDisplay(this);
1829 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1830 existing.inject();
1831
1832 // A hotplug disconnect event is enqueued for a display
1833 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1834 // A hotplug connect event is also enqueued for the same display
1835 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1836
1837 // --------------------------------------------------------------------
1838 // Call Expectations
1839
1840 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1841
1842 setupCommonCallExpectationsForConnectProcessing<Case>();
1843 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1844
1845 // --------------------------------------------------------------------
1846 // Invocation
1847
1848 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1849
1850 // --------------------------------------------------------------------
1851 // Postconditions
1852
1853 // The existing token should have been removed
1854 verifyDisplayIsNotConnected(existing.token());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001855 const auto displayId = Case::Display::DISPLAY_ID::get();
1856 ASSERT_TRUE(displayId);
1857 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1858 EXPECT_NE(existing.token(), mFlinger.mutablePhysicalDisplayTokens()[*displayId]);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001859
1860 // A new display should be connected in its place
1861
1862 verifyPhysicalDisplayIsConnected<Case>();
1863
1864 // --------------------------------------------------------------------
1865 // Cleanup conditions
1866
1867 EXPECT_CALL(*mComposer,
1868 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1869 .WillOnce(Return(Error::NONE));
1870 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1871}
1872
1873TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAdded) {
1874 using Case = HwcVirtualDisplayCase;
1875
1876 // --------------------------------------------------------------------
1877 // Preconditions
1878
1879 // The HWC supports at least one virtual display
1880 injectMockComposer(1);
1881
1882 setupCommonPreconditions<Case>();
1883
1884 // A virtual display was added to the current state, and it has a
1885 // surface(producer)
1886 sp<BBinder> displayToken = new BBinder();
Lloyd Pique4c2ac022018-04-27 12:08:03 -07001887
Dominik Laskowski075d3172018-05-24 15:50:06 -07001888 DisplayDeviceState state;
1889 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001890
1891 sp<mock::GraphicBufferProducer> surface{new mock::GraphicBufferProducer()};
Dominik Laskowski075d3172018-05-24 15:50:06 -07001892 state.surface = surface;
1893 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001894
1895 // --------------------------------------------------------------------
1896 // Call Expectations
1897
1898 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1899 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1900
1901 EXPECT_CALL(*surface, query(NATIVE_WINDOW_WIDTH, _))
1902 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::WIDTH), Return(NO_ERROR)));
1903 EXPECT_CALL(*surface, query(NATIVE_WINDOW_HEIGHT, _))
1904 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::HEIGHT), Return(NO_ERROR)));
1905 EXPECT_CALL(*surface, query(NATIVE_WINDOW_FORMAT, _))
1906 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT),
1907 Return(NO_ERROR)));
1908 EXPECT_CALL(*surface, query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, _))
1909 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(NO_ERROR)));
1910
1911 EXPECT_CALL(*surface, setAsyncMode(true)).Times(1);
1912
Lloyd Pique86fa3db2019-02-04 18:46:01 -08001913 EXPECT_CALL(*mProducer, connect(_, NATIVE_WINDOW_API_EGL, false, _)).Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001914 EXPECT_CALL(*mProducer, disconnect(_, _)).Times(1);
1915
1916 Case::Display::setupHwcVirtualDisplayCreationCallExpectations(this);
1917 Case::WideColorSupport::setupComposerCallExpectations(this);
1918 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001919 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001920
1921 // --------------------------------------------------------------------
1922 // Invocation
1923
1924 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1925
1926 // --------------------------------------------------------------------
1927 // Postconditions
1928
1929 // The display device should have been set up in the list of displays.
1930 verifyDisplayIsConnected<Case>(displayToken);
1931
1932 // --------------------------------------------------------------------
1933 // Cleanup conditions
1934
1935 EXPECT_CALL(*mComposer, destroyVirtualDisplay(Case::Display::HWC_DISPLAY_ID))
1936 .WillOnce(Return(Error::NONE));
1937 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Pique9e9800c2019-02-26 16:26:09 -08001938
1939 // Cleanup
1940 mFlinger.mutableCurrentState().displays.removeItem(displayToken);
1941 mFlinger.mutableDrawingState().displays.removeItem(displayToken);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001942}
1943
1944TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAddedWithNoSurface) {
1945 using Case = HwcVirtualDisplayCase;
1946
1947 // --------------------------------------------------------------------
1948 // Preconditions
1949
1950 // The HWC supports at least one virtual display
1951 injectMockComposer(1);
1952
1953 setupCommonPreconditions<Case>();
1954
1955 // A virtual display was added to the current state, but it does not have a
1956 // surface.
1957 sp<BBinder> displayToken = new BBinder();
1958
Dominik Laskowski075d3172018-05-24 15:50:06 -07001959 DisplayDeviceState state;
1960 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001961
Dominik Laskowski075d3172018-05-24 15:50:06 -07001962 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001963
1964 // --------------------------------------------------------------------
1965 // Call Expectations
1966
1967 // --------------------------------------------------------------------
1968 // Invocation
1969
1970 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1971
1972 // --------------------------------------------------------------------
1973 // Postconditions
1974
1975 // There will not be a display device set up.
1976 EXPECT_FALSE(hasDisplayDevice(displayToken));
1977
1978 // The drawing display state will be set from the current display state.
1979 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1980 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001981 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001982}
1983
1984TEST_F(HandleTransactionLockedTest, processesVirtualDisplayRemoval) {
1985 using Case = HwcVirtualDisplayCase;
1986
1987 // --------------------------------------------------------------------
1988 // Preconditions
1989
1990 // A virtual display is set up but is removed from the current state.
Dominik Laskowski075d3172018-05-24 15:50:06 -07001991 const auto displayId = Case::Display::DISPLAY_ID::get();
1992 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08001993 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001994 Case::Display::injectHwcDisplay(this);
1995 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1996 existing.inject();
1997 mFlinger.mutableCurrentState().displays.removeItem(existing.token());
1998
1999 // --------------------------------------------------------------------
2000 // Call Expectations
2001
2002 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
2003
2004 // --------------------------------------------------------------------
2005 // Invocation
2006
2007 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2008
2009 // --------------------------------------------------------------------
2010 // Postconditions
2011
2012 // The existing token should have been removed
2013 verifyDisplayIsNotConnected(existing.token());
2014}
2015
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002016TEST_F(HandleTransactionLockedTest, processesDisplayLayerStackChanges) {
2017 using Case = NonHwcVirtualDisplayCase;
2018
2019 constexpr uint32_t oldLayerStack = 0u;
2020 constexpr uint32_t newLayerStack = 123u;
2021
2022 // --------------------------------------------------------------------
2023 // Preconditions
2024
2025 // A display is set up
2026 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2027 display.inject();
2028
2029 // There is a change to the layerStack state
2030 display.mutableDrawingDisplayState().layerStack = oldLayerStack;
2031 display.mutableCurrentDisplayState().layerStack = newLayerStack;
2032
2033 // --------------------------------------------------------------------
2034 // Invocation
2035
2036 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2037
2038 // --------------------------------------------------------------------
2039 // Postconditions
2040
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002041 EXPECT_EQ(newLayerStack, display.mutableDisplayDevice()->getLayerStack());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002042}
2043
2044TEST_F(HandleTransactionLockedTest, processesDisplayTransformChanges) {
2045 using Case = NonHwcVirtualDisplayCase;
2046
Dominik Laskowski718f9602019-11-09 20:01:35 -08002047 constexpr ui::Rotation oldTransform = ui::ROTATION_0;
2048 constexpr ui::Rotation newTransform = ui::ROTATION_180;
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002049
2050 // --------------------------------------------------------------------
2051 // Preconditions
2052
2053 // A display is set up
2054 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2055 display.inject();
2056
2057 // There is a change to the orientation state
2058 display.mutableDrawingDisplayState().orientation = oldTransform;
2059 display.mutableCurrentDisplayState().orientation = newTransform;
2060
2061 // --------------------------------------------------------------------
2062 // Invocation
2063
2064 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2065
2066 // --------------------------------------------------------------------
2067 // Postconditions
2068
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002069 EXPECT_EQ(newTransform, display.mutableDisplayDevice()->getOrientation());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002070}
2071
2072TEST_F(HandleTransactionLockedTest, processesDisplayViewportChanges) {
2073 using Case = NonHwcVirtualDisplayCase;
2074
2075 const Rect oldViewport(0, 0, 0, 0);
2076 const Rect newViewport(0, 0, 123, 456);
2077
2078 // --------------------------------------------------------------------
2079 // Preconditions
2080
2081 // A display is set up
2082 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2083 display.inject();
2084
2085 // There is a change to the viewport state
2086 display.mutableDrawingDisplayState().viewport = oldViewport;
2087 display.mutableCurrentDisplayState().viewport = newViewport;
2088
2089 // --------------------------------------------------------------------
2090 // Invocation
2091
2092 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2093
2094 // --------------------------------------------------------------------
2095 // Postconditions
2096
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002097 EXPECT_EQ(newViewport, display.mutableDisplayDevice()->getViewport());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002098}
2099
2100TEST_F(HandleTransactionLockedTest, processesDisplayFrameChanges) {
2101 using Case = NonHwcVirtualDisplayCase;
2102
2103 const Rect oldFrame(0, 0, 0, 0);
2104 const Rect newFrame(0, 0, 123, 456);
2105
2106 // --------------------------------------------------------------------
2107 // Preconditions
2108
2109 // A display is set up
2110 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2111 display.inject();
2112
2113 // There is a change to the viewport state
2114 display.mutableDrawingDisplayState().frame = oldFrame;
2115 display.mutableCurrentDisplayState().frame = newFrame;
2116
2117 // --------------------------------------------------------------------
2118 // Invocation
2119
2120 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2121
2122 // --------------------------------------------------------------------
2123 // Postconditions
2124
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002125 EXPECT_EQ(newFrame, display.mutableDisplayDevice()->getFrame());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002126}
2127
2128TEST_F(HandleTransactionLockedTest, processesDisplayWidthChanges) {
2129 using Case = NonHwcVirtualDisplayCase;
2130
2131 constexpr int oldWidth = 0;
2132 constexpr int oldHeight = 10;
2133 constexpr int newWidth = 123;
2134
2135 // --------------------------------------------------------------------
2136 // Preconditions
2137
2138 // A display is set up
2139 auto nativeWindow = new mock::NativeWindow();
Lloyd Pique542307f2018-10-19 13:24:08 -07002140 auto displaySurface = new compositionengine::mock::DisplaySurface();
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002141 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002142 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2143 display.setNativeWindow(nativeWindow);
2144 display.setDisplaySurface(displaySurface);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002145 // Setup injection expections
2146 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
2147 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
2148 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
2149 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
Lloyd Pique86fa3db2019-02-04 18:46:01 -08002150 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT)).Times(1);
2151 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_API_CONNECT)).Times(1);
2152 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_SET_USAGE64)).Times(1);
chaviw8beb4142019-04-11 13:09:05 -07002153 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002154 display.inject();
2155
2156 // There is a change to the viewport state
2157 display.mutableDrawingDisplayState().width = oldWidth;
2158 display.mutableDrawingDisplayState().height = oldHeight;
2159 display.mutableCurrentDisplayState().width = newWidth;
2160 display.mutableCurrentDisplayState().height = oldHeight;
2161
2162 // --------------------------------------------------------------------
2163 // Call Expectations
2164
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002165 EXPECT_CALL(*displaySurface, resizeBuffers(newWidth, oldHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002166
2167 // --------------------------------------------------------------------
2168 // Invocation
2169
2170 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2171}
2172
2173TEST_F(HandleTransactionLockedTest, processesDisplayHeightChanges) {
2174 using Case = NonHwcVirtualDisplayCase;
2175
2176 constexpr int oldWidth = 0;
2177 constexpr int oldHeight = 10;
2178 constexpr int newHeight = 123;
2179
2180 // --------------------------------------------------------------------
2181 // Preconditions
2182
2183 // A display is set up
2184 auto nativeWindow = new mock::NativeWindow();
Lloyd Pique542307f2018-10-19 13:24:08 -07002185 auto displaySurface = new compositionengine::mock::DisplaySurface();
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002186 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002187 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2188 display.setNativeWindow(nativeWindow);
2189 display.setDisplaySurface(displaySurface);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002190 // Setup injection expections
2191 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
2192 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
2193 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
2194 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
Lloyd Pique86fa3db2019-02-04 18:46:01 -08002195 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT)).Times(1);
2196 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_API_CONNECT)).Times(1);
2197 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_SET_USAGE64)).Times(1);
chaviw8beb4142019-04-11 13:09:05 -07002198 EXPECT_CALL(*nativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002199 display.inject();
2200
2201 // There is a change to the viewport state
2202 display.mutableDrawingDisplayState().width = oldWidth;
2203 display.mutableDrawingDisplayState().height = oldHeight;
2204 display.mutableCurrentDisplayState().width = oldWidth;
2205 display.mutableCurrentDisplayState().height = newHeight;
2206
2207 // --------------------------------------------------------------------
2208 // Call Expectations
2209
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002210 EXPECT_CALL(*displaySurface, resizeBuffers(oldWidth, newHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002211
2212 // --------------------------------------------------------------------
2213 // Invocation
2214
2215 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2216}
2217
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002218/* ------------------------------------------------------------------------
2219 * SurfaceFlinger::setDisplayStateLocked
2220 */
2221
2222TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithUnknownDisplay) {
2223 // --------------------------------------------------------------------
2224 // Preconditions
2225
2226 // We have an unknown display token not associated with a known display
2227 sp<BBinder> displayToken = new BBinder();
2228
2229 // The requested display state references the unknown display.
2230 DisplayState state;
2231 state.what = DisplayState::eLayerStackChanged;
2232 state.token = displayToken;
2233 state.layerStack = 456;
2234
2235 // --------------------------------------------------------------------
2236 // Invocation
2237
2238 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2239
2240 // --------------------------------------------------------------------
2241 // Postconditions
2242
2243 // The returned flags are empty
2244 EXPECT_EQ(0u, flags);
2245
2246 // The display token still doesn't match anything known.
2247 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
2248}
2249
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002250TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWhenNoChanges) {
2251 using Case = SimplePrimaryDisplayCase;
2252
2253 // --------------------------------------------------------------------
2254 // Preconditions
2255
2256 // A display is already set up
2257 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2258 display.inject();
2259
2260 // No changes are made to the display
2261 DisplayState state;
2262 state.what = 0;
2263 state.token = display.token();
2264
2265 // --------------------------------------------------------------------
2266 // Invocation
2267
2268 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2269
2270 // --------------------------------------------------------------------
2271 // Postconditions
2272
2273 // The returned flags are empty
2274 EXPECT_EQ(0u, flags);
2275}
2276
2277TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSurfaceDidNotChange) {
2278 using Case = SimplePrimaryDisplayCase;
2279
2280 // --------------------------------------------------------------------
2281 // Preconditions
2282
2283 // A display is already set up
2284 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2285 display.inject();
2286
2287 // There is a surface that can be set.
2288 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2289
2290 // The current display state has the surface set
2291 display.mutableCurrentDisplayState().surface = surface;
2292
2293 // The incoming request sets the same surface
2294 DisplayState state;
2295 state.what = DisplayState::eSurfaceChanged;
2296 state.token = display.token();
2297 state.surface = surface;
2298
2299 // --------------------------------------------------------------------
2300 // Invocation
2301
2302 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2303
2304 // --------------------------------------------------------------------
2305 // Postconditions
2306
2307 // The returned flags are empty
2308 EXPECT_EQ(0u, flags);
2309
2310 // The current display state is unchanged.
2311 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2312}
2313
2314TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfSurfaceChanged) {
2315 using Case = SimplePrimaryDisplayCase;
2316
2317 // --------------------------------------------------------------------
2318 // Preconditions
2319
2320 // A display is already set up
2321 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2322 display.inject();
2323
2324 // There is a surface that can be set.
2325 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2326
2327 // The current display state does not have a surface
2328 display.mutableCurrentDisplayState().surface = nullptr;
2329
2330 // The incoming request sets a surface
2331 DisplayState state;
2332 state.what = DisplayState::eSurfaceChanged;
2333 state.token = display.token();
2334 state.surface = surface;
2335
2336 // --------------------------------------------------------------------
2337 // Invocation
2338
2339 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2340
2341 // --------------------------------------------------------------------
2342 // Postconditions
2343
2344 // The returned flags indicate a transaction is needed
2345 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2346
2347 // The current display layer stack state is set to the new value
2348 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2349}
2350
2351TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfLayerStackDidNotChange) {
2352 using Case = SimplePrimaryDisplayCase;
2353
2354 // --------------------------------------------------------------------
2355 // Preconditions
2356
2357 // A display is already set up
2358 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2359 display.inject();
2360
2361 // The display has a layer stack set
2362 display.mutableCurrentDisplayState().layerStack = 456u;
2363
2364 // The incoming request sets the same layer stack
2365 DisplayState state;
2366 state.what = DisplayState::eLayerStackChanged;
2367 state.token = display.token();
2368 state.layerStack = 456u;
2369
2370 // --------------------------------------------------------------------
2371 // Invocation
2372
2373 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2374
2375 // --------------------------------------------------------------------
2376 // Postconditions
2377
2378 // The returned flags are empty
2379 EXPECT_EQ(0u, flags);
2380
2381 // The current display state is unchanged
2382 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2383}
2384
2385TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfLayerStackChanged) {
2386 using Case = SimplePrimaryDisplayCase;
2387
2388 // --------------------------------------------------------------------
2389 // Preconditions
2390
2391 // A display is set up
2392 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2393 display.inject();
2394
2395 // The display has a layer stack set
2396 display.mutableCurrentDisplayState().layerStack = 654u;
2397
2398 // The incoming request sets a different layer stack
2399 DisplayState state;
2400 state.what = DisplayState::eLayerStackChanged;
2401 state.token = display.token();
2402 state.layerStack = 456u;
2403
2404 // --------------------------------------------------------------------
2405 // Invocation
2406
2407 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2408
2409 // --------------------------------------------------------------------
2410 // Postconditions
2411
2412 // The returned flags indicate a transaction is needed
2413 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2414
2415 // The desired display state has been set to the new value.
2416 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2417}
2418
2419TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfProjectionDidNotChange) {
2420 using Case = SimplePrimaryDisplayCase;
Dominik Laskowski718f9602019-11-09 20:01:35 -08002421 constexpr ui::Rotation initialOrientation = ui::ROTATION_180;
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002422 const Rect initialFrame = {1, 2, 3, 4};
2423 const Rect initialViewport = {5, 6, 7, 8};
2424
2425 // --------------------------------------------------------------------
2426 // Preconditions
2427
2428 // A display is set up
2429 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2430 display.inject();
2431
2432 // The current display state projection state is all set
2433 display.mutableCurrentDisplayState().orientation = initialOrientation;
2434 display.mutableCurrentDisplayState().frame = initialFrame;
2435 display.mutableCurrentDisplayState().viewport = initialViewport;
2436
2437 // The incoming request sets the same projection state
2438 DisplayState state;
2439 state.what = DisplayState::eDisplayProjectionChanged;
2440 state.token = display.token();
2441 state.orientation = initialOrientation;
2442 state.frame = initialFrame;
2443 state.viewport = initialViewport;
2444
2445 // --------------------------------------------------------------------
2446 // Invocation
2447
2448 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2449
2450 // --------------------------------------------------------------------
2451 // Postconditions
2452
2453 // The returned flags are empty
2454 EXPECT_EQ(0u, flags);
2455
2456 // The current display state is unchanged
2457 EXPECT_EQ(initialOrientation, display.getCurrentDisplayState().orientation);
2458
2459 EXPECT_EQ(initialFrame, display.getCurrentDisplayState().frame);
2460 EXPECT_EQ(initialViewport, display.getCurrentDisplayState().viewport);
2461}
2462
2463TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfOrientationChanged) {
2464 using Case = SimplePrimaryDisplayCase;
Dominik Laskowski718f9602019-11-09 20:01:35 -08002465 constexpr ui::Rotation initialOrientation = ui::ROTATION_90;
2466 constexpr ui::Rotation desiredOrientation = ui::ROTATION_180;
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002467
2468 // --------------------------------------------------------------------
2469 // Preconditions
2470
2471 // A display is set up
2472 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2473 display.inject();
2474
2475 // The current display state has an orientation set
2476 display.mutableCurrentDisplayState().orientation = initialOrientation;
2477
2478 // The incoming request sets a different orientation
2479 DisplayState state;
2480 state.what = DisplayState::eDisplayProjectionChanged;
2481 state.token = display.token();
2482 state.orientation = desiredOrientation;
2483
2484 // --------------------------------------------------------------------
2485 // Invocation
2486
2487 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2488
2489 // --------------------------------------------------------------------
2490 // Postconditions
2491
2492 // The returned flags indicate a transaction is needed
2493 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2494
2495 // The current display state has the new value.
2496 EXPECT_EQ(desiredOrientation, display.getCurrentDisplayState().orientation);
2497}
2498
2499TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfFrameChanged) {
2500 using Case = SimplePrimaryDisplayCase;
2501 const Rect initialFrame = {0, 0, 0, 0};
2502 const Rect desiredFrame = {5, 6, 7, 8};
2503
2504 // --------------------------------------------------------------------
2505 // Preconditions
2506
2507 // A display is set up
2508 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2509 display.inject();
2510
2511 // The current display state does not have a frame
2512 display.mutableCurrentDisplayState().frame = initialFrame;
2513
2514 // The incoming request sets a frame
2515 DisplayState state;
2516 state.what = DisplayState::eDisplayProjectionChanged;
2517 state.token = display.token();
2518 state.frame = desiredFrame;
2519
2520 // --------------------------------------------------------------------
2521 // Invocation
2522
2523 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2524
2525 // --------------------------------------------------------------------
2526 // Postconditions
2527
2528 // The returned flags indicate a transaction is needed
2529 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2530
2531 // The current display state has the new value.
2532 EXPECT_EQ(desiredFrame, display.getCurrentDisplayState().frame);
2533}
2534
2535TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfViewportChanged) {
2536 using Case = SimplePrimaryDisplayCase;
2537 const Rect initialViewport = {0, 0, 0, 0};
2538 const Rect desiredViewport = {5, 6, 7, 8};
2539
2540 // --------------------------------------------------------------------
2541 // Preconditions
2542
2543 // A display is set up
2544 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2545 display.inject();
2546
2547 // The current display state does not have a viewport
2548 display.mutableCurrentDisplayState().viewport = initialViewport;
2549
2550 // The incoming request sets a viewport
2551 DisplayState state;
2552 state.what = DisplayState::eDisplayProjectionChanged;
2553 state.token = display.token();
2554 state.viewport = desiredViewport;
2555
2556 // --------------------------------------------------------------------
2557 // Invocation
2558
2559 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2560
2561 // --------------------------------------------------------------------
2562 // Postconditions
2563
2564 // The returned flags indicate a transaction is needed
2565 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2566
2567 // The current display state has the new value.
2568 EXPECT_EQ(desiredViewport, display.getCurrentDisplayState().viewport);
2569}
2570
2571TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSizeDidNotChange) {
2572 using Case = SimplePrimaryDisplayCase;
2573 constexpr uint32_t initialWidth = 1024;
2574 constexpr uint32_t initialHeight = 768;
2575
2576 // --------------------------------------------------------------------
2577 // Preconditions
2578
2579 // A display is set up
2580 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2581 display.inject();
2582
2583 // The current display state has a size set
2584 display.mutableCurrentDisplayState().width = initialWidth;
2585 display.mutableCurrentDisplayState().height = initialHeight;
2586
2587 // The incoming request sets the same display size
2588 DisplayState state;
2589 state.what = DisplayState::eDisplaySizeChanged;
2590 state.token = display.token();
2591 state.width = initialWidth;
2592 state.height = initialHeight;
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 current display state is unchanged
2606 EXPECT_EQ(initialWidth, display.getCurrentDisplayState().width);
2607 EXPECT_EQ(initialHeight, display.getCurrentDisplayState().height);
2608}
2609
2610TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfWidthChanged) {
2611 using Case = SimplePrimaryDisplayCase;
2612 constexpr uint32_t initialWidth = 0;
2613 constexpr uint32_t desiredWidth = 1024;
2614
2615 // --------------------------------------------------------------------
2616 // Preconditions
2617
2618 // A display is set up
2619 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2620 display.inject();
2621
2622 // The display does not yet have a width
2623 display.mutableCurrentDisplayState().width = initialWidth;
2624
2625 // The incoming request sets a display width
2626 DisplayState state;
2627 state.what = DisplayState::eDisplaySizeChanged;
2628 state.token = display.token();
2629 state.width = desiredWidth;
2630
2631 // --------------------------------------------------------------------
2632 // Invocation
2633
2634 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2635
2636 // --------------------------------------------------------------------
2637 // Postconditions
2638
2639 // The returned flags indicate a transaction is needed
2640 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2641
2642 // The current display state has the new value.
2643 EXPECT_EQ(desiredWidth, display.getCurrentDisplayState().width);
2644}
2645
2646TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfHeightChanged) {
2647 using Case = SimplePrimaryDisplayCase;
2648 constexpr uint32_t initialHeight = 0;
2649 constexpr uint32_t desiredHeight = 768;
2650
2651 // --------------------------------------------------------------------
2652 // Preconditions
2653
2654 // A display is set up
2655 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2656 display.inject();
2657
2658 // The display does not yet have a height
2659 display.mutableCurrentDisplayState().height = initialHeight;
2660
2661 // The incoming request sets a display height
2662 DisplayState state;
2663 state.what = DisplayState::eDisplaySizeChanged;
2664 state.token = display.token();
2665 state.height = desiredHeight;
2666
2667 // --------------------------------------------------------------------
2668 // Invocation
2669
2670 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2671
2672 // --------------------------------------------------------------------
2673 // Postconditions
2674
2675 // The returned flags indicate a transaction is needed
2676 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2677
2678 // The current display state has the new value.
2679 EXPECT_EQ(desiredHeight, display.getCurrentDisplayState().height);
2680}
2681
Lloyd Pique86016da2018-03-01 16:09:38 -08002682/* ------------------------------------------------------------------------
2683 * SurfaceFlinger::onInitializeDisplays
2684 */
2685
2686TEST_F(DisplayTransactionTest, onInitializeDisplaysSetsUpPrimaryDisplay) {
2687 using Case = SimplePrimaryDisplayCase;
2688
2689 // --------------------------------------------------------------------
2690 // Preconditions
2691
2692 // A primary display is set up
2693 Case::Display::injectHwcDisplay(this);
2694 auto primaryDisplay = Case::Display::makeFakeExistingDisplayInjector(this);
2695 primaryDisplay.inject();
2696
2697 // --------------------------------------------------------------------
2698 // Call Expectations
2699
2700 // We expect the surface interceptor to possibly be used, but we treat it as
2701 // disabled since it is called as a side effect rather than directly by this
2702 // function.
2703 EXPECT_CALL(*mSurfaceInterceptor, isEnabled()).WillOnce(Return(false));
2704
2705 // We expect a call to get the active display config.
2706 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
2707
2708 // We expect invalidate() to be invoked once to trigger display transaction
2709 // processing.
2710 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
2711
Lloyd Pique86fa3db2019-02-04 18:46:01 -08002712 EXPECT_CALL(*mPrimaryDispSync, expectedPresentTime()).WillRepeatedly(Return(0));
2713
Lloyd Pique86016da2018-03-01 16:09:38 -08002714 // --------------------------------------------------------------------
2715 // Invocation
2716
2717 mFlinger.onInitializeDisplays();
2718
2719 // --------------------------------------------------------------------
2720 // Postconditions
2721
2722 // The primary display should have a current state
2723 ASSERT_TRUE(hasCurrentDisplayState(primaryDisplay.token()));
2724 const auto& primaryDisplayState = getCurrentDisplayState(primaryDisplay.token());
2725 // The layer stack state should be set to zero
2726 EXPECT_EQ(0u, primaryDisplayState.layerStack);
2727 // The orientation state should be set to zero
Dominik Laskowski718f9602019-11-09 20:01:35 -08002728 EXPECT_EQ(ui::ROTATION_0, primaryDisplayState.orientation);
Lloyd Pique86016da2018-03-01 16:09:38 -08002729
2730 // The frame state should be set to INVALID
2731 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.frame);
2732
2733 // The viewport state should be set to INVALID
2734 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.viewport);
2735
2736 // The width and height should both be zero
2737 EXPECT_EQ(0u, primaryDisplayState.width);
2738 EXPECT_EQ(0u, primaryDisplayState.height);
2739
2740 // The display should be set to HWC_POWER_MODE_NORMAL
2741 ASSERT_TRUE(hasDisplayDevice(primaryDisplay.token()));
2742 auto displayDevice = primaryDisplay.mutableDisplayDevice();
2743 EXPECT_EQ(HWC_POWER_MODE_NORMAL, displayDevice->getPowerMode());
2744
2745 // The display refresh period should be set in the frame tracker.
2746 FrameStats stats;
2747 mFlinger.getAnimFrameTracker().getStats(&stats);
2748 EXPECT_EQ(DEFAULT_REFRESH_RATE, stats.refreshPeriodNano);
2749
2750 // The display transaction needed flag should be set.
2751 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
2752
2753 // The compositor timing should be set to default values
2754 const auto& compositorTiming = mFlinger.getCompositorTiming();
2755 EXPECT_EQ(-DEFAULT_REFRESH_RATE, compositorTiming.deadline);
2756 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.interval);
2757 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.presentLatency);
2758}
2759
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002760/* ------------------------------------------------------------------------
2761 * SurfaceFlinger::setPowerModeInternal
2762 */
2763
2764// Used when we simulate a display that supports doze.
Peiyong Lined531a32018-10-26 18:27:56 -07002765template <typename Display>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002766struct DozeIsSupportedVariant {
2767 static constexpr bool DOZE_SUPPORTED = true;
2768 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2769 IComposerClient::PowerMode::DOZE;
2770 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2771 IComposerClient::PowerMode::DOZE_SUSPEND;
Peiyong Lined531a32018-10-26 18:27:56 -07002772
2773 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
2774 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(Display::HWC_DISPLAY_ID, _))
2775 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hwc2::DisplayCapability>(
2776 {Hwc2::DisplayCapability::DOZE})),
2777 Return(Error::NONE)));
2778 }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002779};
2780
Peiyong Lined531a32018-10-26 18:27:56 -07002781template <typename Display>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002782// Used when we simulate a display that does not support doze.
2783struct DozeNotSupportedVariant {
2784 static constexpr bool DOZE_SUPPORTED = false;
2785 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2786 IComposerClient::PowerMode::ON;
2787 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2788 IComposerClient::PowerMode::ON;
Peiyong Lined531a32018-10-26 18:27:56 -07002789
2790 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
2791 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(Display::HWC_DISPLAY_ID, _))
2792 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hwc2::DisplayCapability>({})),
2793 Return(Error::NONE)));
2794 }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002795};
2796
2797struct EventThreadBaseSupportedVariant {
2798 static void setupEventAndEventControlThreadNoCallExpectations(DisplayTransactionTest* test) {
2799 // The event control thread should not be notified.
2800 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(_)).Times(0);
2801
2802 // The event thread should not be notified.
2803 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(0);
2804 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(0);
2805 }
2806};
2807
2808struct EventThreadNotSupportedVariant : public EventThreadBaseSupportedVariant {
2809 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2810 // These calls are only expected for the primary display.
2811
2812 // Instead expect no calls.
2813 setupEventAndEventControlThreadNoCallExpectations(test);
2814 }
2815
2816 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2817 // These calls are only expected for the primary display.
2818
2819 // Instead expect no calls.
2820 setupEventAndEventControlThreadNoCallExpectations(test);
2821 }
2822};
2823
2824struct EventThreadIsSupportedVariant : public EventThreadBaseSupportedVariant {
2825 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2826 // The event control thread should be notified to enable vsyncs
2827 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(true)).Times(1);
2828
2829 // The event thread should be notified that the screen was acquired.
2830 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(1);
2831 }
2832
2833 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2834 // There should be a call to setVsyncEnabled(false)
2835 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(false)).Times(1);
2836
2837 // The event thread should not be notified that the screen was released.
2838 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(1);
2839 }
2840};
2841
Lloyd Pique41be5d22018-06-21 13:11:48 -07002842struct DispSyncIsSupportedVariant {
2843 static void setupBeginResyncCallExpectations(DisplayTransactionTest* test) {
Lloyd Pique41be5d22018-06-21 13:11:48 -07002844 EXPECT_CALL(*test->mPrimaryDispSync, setPeriod(DEFAULT_REFRESH_RATE)).Times(1);
2845 EXPECT_CALL(*test->mPrimaryDispSync, beginResync()).Times(1);
2846 }
2847
2848 static void setupEndResyncCallExpectations(DisplayTransactionTest* test) {
2849 EXPECT_CALL(*test->mPrimaryDispSync, endResync()).Times(1);
2850 }
2851};
2852
2853struct DispSyncNotSupportedVariant {
2854 static void setupBeginResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2855
2856 static void setupEndResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2857};
2858
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002859// --------------------------------------------------------------------
2860// Note:
2861//
2862// There are a large number of transitions we could test, however we only test a
2863// selected subset which provides complete test coverage of the implementation.
2864// --------------------------------------------------------------------
2865
2866template <int initialPowerMode, int targetPowerMode>
2867struct TransitionVariantCommon {
2868 static constexpr auto INITIAL_POWER_MODE = initialPowerMode;
2869 static constexpr auto TARGET_POWER_MODE = targetPowerMode;
2870
2871 static void verifyPostconditions(DisplayTransactionTest*) {}
2872};
2873
2874struct TransitionOffToOnVariant
2875 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_NORMAL> {
2876 template <typename Case>
2877 static void setupCallExpectations(DisplayTransactionTest* test) {
2878 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2879 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002880 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002881 Case::setupRepaintEverythingCallExpectations(test);
2882 }
2883
2884 static void verifyPostconditions(DisplayTransactionTest* test) {
2885 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2886 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2887 }
2888};
2889
2890struct TransitionOffToDozeSuspendVariant
2891 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_DOZE_SUSPEND> {
2892 template <typename Case>
2893 static void setupCallExpectations(DisplayTransactionTest* test) {
2894 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2895 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2896 Case::setupRepaintEverythingCallExpectations(test);
2897 }
2898
2899 static void verifyPostconditions(DisplayTransactionTest* test) {
2900 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2901 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2902 }
2903};
2904
2905struct TransitionOnToOffVariant
2906 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_OFF> {
2907 template <typename Case>
2908 static void setupCallExpectations(DisplayTransactionTest* test) {
2909 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002910 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002911 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2912 }
2913
2914 static void verifyPostconditions(DisplayTransactionTest* test) {
2915 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2916 }
2917};
2918
2919struct TransitionDozeSuspendToOffVariant
2920 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_OFF> {
2921 template <typename Case>
2922 static void setupCallExpectations(DisplayTransactionTest* test) {
2923 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2924 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2925 }
2926
2927 static void verifyPostconditions(DisplayTransactionTest* test) {
2928 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2929 }
2930};
2931
2932struct TransitionOnToDozeVariant
2933 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE> {
2934 template <typename Case>
2935 static void setupCallExpectations(DisplayTransactionTest* test) {
2936 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2937 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2938 }
2939};
2940
2941struct TransitionDozeSuspendToDozeVariant
2942 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_DOZE> {
2943 template <typename Case>
2944 static void setupCallExpectations(DisplayTransactionTest* test) {
2945 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002946 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002947 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2948 }
2949};
2950
2951struct TransitionDozeToOnVariant
2952 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE, HWC_POWER_MODE_NORMAL> {
2953 template <typename Case>
2954 static void setupCallExpectations(DisplayTransactionTest* test) {
2955 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2956 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2957 }
2958};
2959
2960struct TransitionDozeSuspendToOnVariant
2961 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_NORMAL> {
2962 template <typename Case>
2963 static void setupCallExpectations(DisplayTransactionTest* test) {
2964 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002965 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002966 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2967 }
2968};
2969
2970struct TransitionOnToDozeSuspendVariant
2971 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE_SUSPEND> {
2972 template <typename Case>
2973 static void setupCallExpectations(DisplayTransactionTest* test) {
2974 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002975 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002976 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2977 }
2978};
2979
2980struct TransitionOnToUnknownVariant
2981 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_LEET> {
2982 template <typename Case>
2983 static void setupCallExpectations(DisplayTransactionTest* test) {
2984 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2985 Case::setupNoComposerPowerModeCallExpectations(test);
2986 }
2987};
2988
2989// --------------------------------------------------------------------
2990// Note:
2991//
2992// Rather than testing the cartesian product of of
2993// DozeIsSupported/DozeNotSupported with all other options, we use one for one
2994// display type, and the other for another display type.
2995// --------------------------------------------------------------------
2996
2997template <typename DisplayVariant, typename DozeVariant, typename EventThreadVariant,
Lloyd Pique41be5d22018-06-21 13:11:48 -07002998 typename DispSyncVariant, typename TransitionVariant>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002999struct DisplayPowerCase {
3000 using Display = DisplayVariant;
3001 using Doze = DozeVariant;
3002 using EventThread = EventThreadVariant;
Lloyd Pique41be5d22018-06-21 13:11:48 -07003003 using DispSync = DispSyncVariant;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003004 using Transition = TransitionVariant;
3005
3006 static auto injectDisplayWithInitialPowerMode(DisplayTransactionTest* test, int mode) {
Lloyd Pique86fa3db2019-02-04 18:46:01 -08003007 Display::injectHwcDisplayWithNoDefaultCapabilities(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003008 auto display = Display::makeFakeExistingDisplayInjector(test);
3009 display.inject();
3010 display.mutableDisplayDevice()->setPowerMode(mode);
3011 return display;
3012 }
3013
3014 static void setInitialPrimaryHWVsyncEnabled(DisplayTransactionTest* test, bool enabled) {
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -07003015 test->mFlinger.scheduler()->mutablePrimaryHWVsyncEnabled() = enabled;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003016 }
3017
3018 static void setupRepaintEverythingCallExpectations(DisplayTransactionTest* test) {
3019 EXPECT_CALL(*test->mMessageQueue, invalidate()).Times(1);
3020 }
3021
3022 static void setupSurfaceInterceptorCallExpectations(DisplayTransactionTest* test, int mode) {
3023 EXPECT_CALL(*test->mSurfaceInterceptor, isEnabled()).WillOnce(Return(true));
3024 EXPECT_CALL(*test->mSurfaceInterceptor, savePowerModeUpdate(_, mode)).Times(1);
3025 }
3026
3027 static void setupComposerCallExpectations(DisplayTransactionTest* test,
3028 IComposerClient::PowerMode mode) {
3029 // Any calls to get the active config will return a default value.
3030 EXPECT_CALL(*test->mComposer, getActiveConfig(Display::HWC_DISPLAY_ID, _))
3031 .WillRepeatedly(DoAll(SetArgPointee<1>(Display::HWC_ACTIVE_CONFIG_ID),
3032 Return(Error::NONE)));
3033
3034 // Any calls to get whether the display supports dozing will return the value set by the
3035 // policy variant.
3036 EXPECT_CALL(*test->mComposer, getDozeSupport(Display::HWC_DISPLAY_ID, _))
3037 .WillRepeatedly(DoAll(SetArgPointee<1>(Doze::DOZE_SUPPORTED), Return(Error::NONE)));
3038
3039 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, mode)).Times(1);
3040 }
3041
3042 static void setupNoComposerPowerModeCallExpectations(DisplayTransactionTest* test) {
3043 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, _)).Times(0);
3044 }
3045};
3046
3047// A sample configuration for the primary display.
3048// In addition to having event thread support, we emulate doze support.
3049template <typename TransitionVariant>
Peiyong Lined531a32018-10-26 18:27:56 -07003050using PrimaryDisplayPowerCase =
3051 DisplayPowerCase<PrimaryDisplayVariant, DozeIsSupportedVariant<PrimaryDisplayVariant>,
3052 EventThreadIsSupportedVariant, DispSyncIsSupportedVariant,
3053 TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003054
3055// A sample configuration for the external display.
3056// In addition to not having event thread support, we emulate not having doze
3057// support.
3058template <typename TransitionVariant>
Peiyong Lined531a32018-10-26 18:27:56 -07003059using ExternalDisplayPowerCase =
3060 DisplayPowerCase<ExternalDisplayVariant, DozeNotSupportedVariant<ExternalDisplayVariant>,
3061 EventThreadNotSupportedVariant, DispSyncNotSupportedVariant,
3062 TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003063
3064class SetPowerModeInternalTest : public DisplayTransactionTest {
3065public:
3066 template <typename Case>
3067 void transitionDisplayCommon();
3068};
3069
3070template <int PowerMode>
3071struct PowerModeInitialVSyncEnabled : public std::false_type {};
3072
3073template <>
3074struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_NORMAL> : public std::true_type {};
3075
3076template <>
3077struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_DOZE> : public std::true_type {};
3078
3079template <typename Case>
3080void SetPowerModeInternalTest::transitionDisplayCommon() {
3081 // --------------------------------------------------------------------
3082 // Preconditions
3083
Peiyong Lined531a32018-10-26 18:27:56 -07003084 Case::Doze::setupComposerCallExpectations(this);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003085 auto display =
3086 Case::injectDisplayWithInitialPowerMode(this, Case::Transition::INITIAL_POWER_MODE);
3087 Case::setInitialPrimaryHWVsyncEnabled(this,
3088 PowerModeInitialVSyncEnabled<
3089 Case::Transition::INITIAL_POWER_MODE>::value);
3090
3091 // --------------------------------------------------------------------
3092 // Call Expectations
3093
3094 Case::setupSurfaceInterceptorCallExpectations(this, Case::Transition::TARGET_POWER_MODE);
3095 Case::Transition::template setupCallExpectations<Case>(this);
3096
3097 // --------------------------------------------------------------------
3098 // Invocation
3099
3100 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(),
3101 Case::Transition::TARGET_POWER_MODE);
3102
3103 // --------------------------------------------------------------------
3104 // Postconditions
3105
3106 Case::Transition::verifyPostconditions(this);
3107}
3108
3109TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfNoChange) {
3110 using Case = SimplePrimaryDisplayCase;
3111
3112 // --------------------------------------------------------------------
3113 // Preconditions
3114
3115 // A primary display device is set up
3116 Case::Display::injectHwcDisplay(this);
3117 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
3118 display.inject();
3119
Dominik Laskowskia2edf612018-06-01 13:15:16 -07003120 // The display is already set to HWC_POWER_MODE_NORMAL
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003121 display.mutableDisplayDevice()->setPowerMode(HWC_POWER_MODE_NORMAL);
3122
3123 // --------------------------------------------------------------------
3124 // Invocation
3125
3126 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_NORMAL);
3127
3128 // --------------------------------------------------------------------
3129 // Postconditions
3130
3131 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
3132}
3133
Dominik Laskowskieecd6592018-05-29 10:25:41 -07003134TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfVirtualDisplay) {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003135 using Case = HwcVirtualDisplayCase;
3136
3137 // --------------------------------------------------------------------
3138 // Preconditions
3139
Dominik Laskowski075d3172018-05-24 15:50:06 -07003140 // Insert display data so that the HWC thinks it created the virtual display.
3141 const auto displayId = Case::Display::DISPLAY_ID::get();
3142 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08003143 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003144
3145 // A virtual display device is set up
3146 Case::Display::injectHwcDisplay(this);
3147 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
3148 display.inject();
3149
Dominik Laskowskieecd6592018-05-29 10:25:41 -07003150 // The display is set to HWC_POWER_MODE_NORMAL
3151 getDisplayDevice(display.token())->setPowerMode(HWC_POWER_MODE_NORMAL);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003152
3153 // --------------------------------------------------------------------
3154 // Invocation
3155
Dominik Laskowskieecd6592018-05-29 10:25:41 -07003156 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_OFF);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08003157
3158 // --------------------------------------------------------------------
3159 // Postconditions
3160
3161 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
3162}
3163
3164TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnPrimaryDisplay) {
3165 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToOnVariant>>();
3166}
3167
3168TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendPrimaryDisplay) {
3169 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
3170}
3171
3172TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffPrimaryDisplay) {
3173 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToOffVariant>>();
3174}
3175
3176TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffPrimaryDisplay) {
3177 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
3178}
3179
3180TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozePrimaryDisplay) {
3181 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeVariant>>();
3182}
3183
3184TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozePrimaryDisplay) {
3185 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
3186}
3187
3188TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnPrimaryDisplay) {
3189 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeToOnVariant>>();
3190}
3191
3192TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnPrimaryDisplay) {
3193 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
3194}
3195
3196TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendPrimaryDisplay) {
3197 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
3198}
3199
3200TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownPrimaryDisplay) {
3201 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToUnknownVariant>>();
3202}
3203
3204TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnExternalDisplay) {
3205 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToOnVariant>>();
3206}
3207
3208TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendExternalDisplay) {
3209 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
3210}
3211
3212TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffExternalDisplay) {
3213 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToOffVariant>>();
3214}
3215
3216TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffExternalDisplay) {
3217 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
3218}
3219
3220TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeExternalDisplay) {
3221 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeVariant>>();
3222}
3223
3224TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozeExternalDisplay) {
3225 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
3226}
3227
3228TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnExternalDisplay) {
3229 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeToOnVariant>>();
3230}
3231
3232TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnExternalDisplay) {
3233 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
3234}
3235
3236TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendExternalDisplay) {
3237 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
3238}
3239
3240TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownExternalDisplay) {
3241 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToUnknownVariant>>();
3242}
3243
Lloyd Piquef58625d2017-12-19 13:22:33 -08003244} // namespace
3245} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08003246
3247// TODO(b/129481165): remove the #pragma below and fix conversion issues
3248#pragma clang diagnostic pop // ignored "-Wconversion"