blob: b64a7fc240c761b9e07db5f56fbdd5345d0aaa50 [file] [log] [blame]
Lloyd Piquef58625d2017-12-19 13:22:33 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#undef LOG_TAG
18#define LOG_TAG "LibSurfaceFlingerUnittests"
19
Dominik Laskowski075d3172018-05-24 15:50:06 -070020#include <type_traits>
21
Lloyd Pique542307f2018-10-19 13:24:08 -070022#include <compositionengine/mock/DisplaySurface.h>
Lloyd Piquef58625d2017-12-19 13:22:33 -080023#include <gmock/gmock.h>
24#include <gtest/gtest.h>
Lloyd Piquef58625d2017-12-19 13:22:33 -080025#include <log/log.h>
Lloyd Pique3823e7b2018-10-18 16:58:10 -070026#include <renderengine/mock/RenderEngine.h>
Valerie Hau9758ae02018-10-09 16:05:09 -070027#include <ui/DebugUtils.h>
Dominik Laskowski075d3172018-05-24 15:50:06 -070028
29#include "DisplayIdentificationTest.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080030#include "TestableSurfaceFlinger.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080031#include "mock/DisplayHardware/MockComposer.h"
Lloyd Pique41be5d22018-06-21 13:11:48 -070032#include "mock/MockDispSync.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080033#include "mock/MockEventControlThread.h"
34#include "mock/MockEventThread.h"
35#include "mock/MockMessageQueue.h"
36#include "mock/MockNativeWindowSurface.h"
37#include "mock/MockSurfaceInterceptor.h"
Lloyd Piquecbe00012018-02-02 15:40:42 -080038#include "mock/gui/MockGraphicBufferConsumer.h"
39#include "mock/gui/MockGraphicBufferProducer.h"
40#include "mock/system/window/MockNativeWindow.h"
Lloyd Piquef58625d2017-12-19 13:22:33 -080041
42namespace android {
43namespace {
44
Lloyd Piquee39cad22017-12-20 17:01:29 -080045using testing::_;
46using testing::ByMove;
47using testing::DoAll;
48using testing::Mock;
49using testing::Return;
50using testing::SetArgPointee;
51
Lloyd Piqued883d5a2018-04-27 19:32:30 -070052using android::Hwc2::ColorMode;
Lloyd Piquee39cad22017-12-20 17:01:29 -080053using android::Hwc2::Error;
Lloyd Piqued883d5a2018-04-27 19:32:30 -070054using android::Hwc2::Hdr;
Lloyd Piquee39cad22017-12-20 17:01:29 -080055using android::Hwc2::IComposer;
56using android::Hwc2::IComposerClient;
Lloyd Piqued883d5a2018-04-27 19:32:30 -070057using android::Hwc2::PerFrameMetadataKey;
58using android::Hwc2::RenderIntent;
Lloyd Piquee39cad22017-12-20 17:01:29 -080059
Lloyd Piquec11e0d32018-01-22 18:44:59 -080060using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector;
61using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
Lloyd Pique1fa4d462018-01-22 18:03:16 -080062using HotplugEvent = TestableSurfaceFlinger::HotplugEvent;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080063using HWC2Display = TestableSurfaceFlinger::HWC2Display;
Lloyd Piquebc792092018-01-17 11:52:30 -080064
Lloyd Piquec11e0d32018-01-22 18:44:59 -080065constexpr int32_t DEFAULT_REFRESH_RATE = 16'666'666;
Lloyd Piquee39cad22017-12-20 17:01:29 -080066constexpr int32_t DEFAULT_DPI = 320;
Lloyd Piquec11e0d32018-01-22 18:44:59 -080067constexpr int DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT = HAL_PIXEL_FORMAT_RGB_565;
Lloyd Piquee39cad22017-12-20 17:01:29 -080068
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -080069constexpr int HWC_POWER_MODE_LEET = 1337; // An out of range power mode value
70
Lloyd Piquec11e0d32018-01-22 18:44:59 -080071/* ------------------------------------------------------------------------
72 * Boolean avoidance
73 *
74 * To make calls and template instantiations more readable, we define some
75 * local enums along with an implicit bool conversion.
76 */
77
78#define BOOL_SUBSTITUTE(TYPENAME) enum class TYPENAME : bool { FALSE = false, TRUE = true };
79
Lloyd Piquec11e0d32018-01-22 18:44:59 -080080BOOL_SUBSTITUTE(Async);
Dominik Laskowski075d3172018-05-24 15:50:06 -070081BOOL_SUBSTITUTE(Critical);
82BOOL_SUBSTITUTE(Primary);
Lloyd Piquec11e0d32018-01-22 18:44:59 -080083BOOL_SUBSTITUTE(Secure);
Dominik Laskowski075d3172018-05-24 15:50:06 -070084BOOL_SUBSTITUTE(Virtual);
Lloyd Piquec11e0d32018-01-22 18:44:59 -080085
86/* ------------------------------------------------------------------------
87 *
88 */
Lloyd Pique1fa4d462018-01-22 18:03:16 -080089
Lloyd Piquef58625d2017-12-19 13:22:33 -080090class DisplayTransactionTest : public testing::Test {
Lloyd Piquec11e0d32018-01-22 18:44:59 -080091public:
Lloyd Piquef58625d2017-12-19 13:22:33 -080092 DisplayTransactionTest();
93 ~DisplayTransactionTest() override;
94
Lloyd Pique1fa4d462018-01-22 18:03:16 -080095 // --------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -080096 // Mock/Fake injection
Lloyd Piquef58625d2017-12-19 13:22:33 -080097
Lloyd Piquec11e0d32018-01-22 18:44:59 -080098 void injectMockComposer(int virtualDisplayCount);
99 void injectFakeBufferQueueFactory();
100 void injectFakeNativeWindowSurfaceFactory();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800101
102 // --------------------------------------------------------------------
103 // Postcondition helpers
104
Dominik Laskowski075d3172018-05-24 15:50:06 -0700105 bool hasPhysicalHwcDisplay(hwc2_display_t hwcDisplayId);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800106 bool hasTransactionFlagSet(int flag);
107 bool hasDisplayDevice(sp<IBinder> displayToken);
108 sp<DisplayDevice> getDisplayDevice(sp<IBinder> displayToken);
109 bool hasCurrentDisplayState(sp<IBinder> displayToken);
110 const DisplayDeviceState& getCurrentDisplayState(sp<IBinder> displayToken);
111 bool hasDrawingDisplayState(sp<IBinder> displayToken);
112 const DisplayDeviceState& getDrawingDisplayState(sp<IBinder> displayToken);
113
114 // --------------------------------------------------------------------
115 // Test instances
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800116
Lloyd Piquef58625d2017-12-19 13:22:33 -0800117 TestableSurfaceFlinger mFlinger;
Lloyd Piquee39cad22017-12-20 17:01:29 -0800118 mock::EventThread* mEventThread = new mock::EventThread();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800119 mock::EventControlThread* mEventControlThread = new mock::EventControlThread();
Alec Mouriba013fa2018-10-16 12:43:11 -0700120 sp<mock::NativeWindow> mNativeWindow = new mock::NativeWindow();
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800121 sp<GraphicBuffer> mBuffer = new GraphicBuffer();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800122
123 // These mocks are created by the test, but are destroyed by SurfaceFlinger
124 // by virtue of being stored into a std::unique_ptr. However we still need
125 // to keep a reference to them for use in setting up call expectations.
Peiyong Lin833074a2018-08-28 11:53:54 -0700126 renderengine::mock::RenderEngine* mRenderEngine = new renderengine::mock::RenderEngine();
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800127 Hwc2::mock::Composer* mComposer = nullptr;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800128 mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
129 mock::SurfaceInterceptor* mSurfaceInterceptor = new mock::SurfaceInterceptor();
Lloyd Pique41be5d22018-06-21 13:11:48 -0700130 mock::DispSync* mPrimaryDispSync = new mock::DispSync();
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800131
132 // These mocks are created only when expected to be created via a factory.
133 sp<mock::GraphicBufferConsumer> mConsumer;
134 sp<mock::GraphicBufferProducer> mProducer;
Lloyd Pique22098362018-09-13 11:46:49 -0700135 surfaceflinger::mock::NativeWindowSurface* mNativeWindowSurface = nullptr;
Lloyd Piquef58625d2017-12-19 13:22:33 -0800136};
137
138DisplayTransactionTest::DisplayTransactionTest() {
139 const ::testing::TestInfo* const test_info =
140 ::testing::UnitTest::GetInstance()->current_test_info();
141 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
Lloyd Piquee39cad22017-12-20 17:01:29 -0800142
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800143 // Default to no wide color display support configured
144 mFlinger.mutableHasWideColorDisplay() = false;
Peiyong Lin13effd12018-07-24 17:01:47 -0700145 mFlinger.mutableUseColorManagement() = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800146 mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
147
148 // Default to using HWC virtual displays
149 mFlinger.mutableUseHwcVirtualDisplays() = true;
150
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800151 mFlinger.setCreateBufferQueueFunction([](auto, auto, auto) {
152 ADD_FAILURE() << "Unexpected request to create a buffer queue.";
153 });
154
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800155 mFlinger.setCreateNativeWindowSurface([](auto) {
156 ADD_FAILURE() << "Unexpected request to create a native window surface.";
157 return nullptr;
158 });
159
160 mFlinger.mutableEventControlThread().reset(mEventControlThread);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800161 mFlinger.mutableEventThread().reset(mEventThread);
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800162 mFlinger.mutableEventQueue().reset(mMessageQueue);
Peiyong Lin833074a2018-08-28 11:53:54 -0700163 mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800164 mFlinger.mutableInterceptor().reset(mSurfaceInterceptor);
Lloyd Pique41be5d22018-06-21 13:11:48 -0700165 mFlinger.mutablePrimaryDispSync().reset(mPrimaryDispSync);
Lloyd Piquee39cad22017-12-20 17:01:29 -0800166
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800167 injectMockComposer(0);
Lloyd Piquef58625d2017-12-19 13:22:33 -0800168}
169
170DisplayTransactionTest::~DisplayTransactionTest() {
171 const ::testing::TestInfo* const test_info =
172 ::testing::UnitTest::GetInstance()->current_test_info();
173 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
174}
175
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800176void DisplayTransactionTest::injectMockComposer(int virtualDisplayCount) {
177 mComposer = new Hwc2::mock::Composer();
Lloyd Piquee39cad22017-12-20 17:01:29 -0800178 EXPECT_CALL(*mComposer, getCapabilities())
179 .WillOnce(Return(std::vector<IComposer::Capability>()));
180 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
181 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
Lloyd Piquef58625d2017-12-19 13:22:33 -0800182
Lloyd Piquee39cad22017-12-20 17:01:29 -0800183 Mock::VerifyAndClear(mComposer);
184}
185
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800186void DisplayTransactionTest::injectFakeBufferQueueFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800187 // This setup is only expected once per test.
188 ASSERT_TRUE(mConsumer == nullptr && mProducer == nullptr);
189
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800190 mConsumer = new mock::GraphicBufferConsumer();
191 mProducer = new mock::GraphicBufferProducer();
192
193 mFlinger.setCreateBufferQueueFunction([this](auto outProducer, auto outConsumer, bool) {
194 *outProducer = mProducer;
195 *outConsumer = mConsumer;
196 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800197}
Lloyd Pique5b36f3f2018-01-17 11:57:07 -0800198
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800199void DisplayTransactionTest::injectFakeNativeWindowSurfaceFactory() {
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800200 // This setup is only expected once per test.
201 ASSERT_TRUE(mNativeWindowSurface == nullptr);
202
Lloyd Pique22098362018-09-13 11:46:49 -0700203 mNativeWindowSurface = new surfaceflinger::mock::NativeWindowSurface();
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800204
Lloyd Pique22098362018-09-13 11:46:49 -0700205 mFlinger.setCreateNativeWindowSurface([this](auto) {
206 return std::unique_ptr<surfaceflinger::NativeWindowSurface>(mNativeWindowSurface);
207 });
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800208}
209
Dominik Laskowski075d3172018-05-24 15:50:06 -0700210bool DisplayTransactionTest::hasPhysicalHwcDisplay(hwc2_display_t hwcDisplayId) {
211 return mFlinger.mutableHwcPhysicalDisplayIdMap().count(hwcDisplayId) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800212}
213
214bool DisplayTransactionTest::hasTransactionFlagSet(int flag) {
215 return mFlinger.mutableTransactionFlags() & flag;
216}
217
218bool DisplayTransactionTest::hasDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700219 return mFlinger.mutableDisplays().count(displayToken) == 1;
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800220}
221
222sp<DisplayDevice> DisplayTransactionTest::getDisplayDevice(sp<IBinder> displayToken) {
Dominik Laskowski9fae1022018-05-29 13:17:40 -0700223 return mFlinger.mutableDisplays()[displayToken];
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800224}
225
226bool DisplayTransactionTest::hasCurrentDisplayState(sp<IBinder> displayToken) {
227 return mFlinger.mutableCurrentState().displays.indexOfKey(displayToken) >= 0;
228}
229
230const DisplayDeviceState& DisplayTransactionTest::getCurrentDisplayState(sp<IBinder> displayToken) {
231 return mFlinger.mutableCurrentState().displays.valueFor(displayToken);
232}
233
234bool DisplayTransactionTest::hasDrawingDisplayState(sp<IBinder> displayToken) {
235 return mFlinger.mutableDrawingState().displays.indexOfKey(displayToken) >= 0;
236}
237
238const DisplayDeviceState& DisplayTransactionTest::getDrawingDisplayState(sp<IBinder> displayToken) {
239 return mFlinger.mutableDrawingState().displays.valueFor(displayToken);
240}
241
242/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800243 *
Lloyd Pique1fa4d462018-01-22 18:03:16 -0800244 */
245
Dominik Laskowski075d3172018-05-24 15:50:06 -0700246template <typename PhysicalDisplay>
247struct PhysicalDisplayId {};
248
Dominik Laskowski34157762018-10-31 13:07:19 -0700249template <DisplayId::Type displayId>
250using VirtualDisplayId = std::integral_constant<DisplayId::Type, displayId>;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700251
252struct NoDisplayId {};
253
254template <typename>
255struct IsPhysicalDisplayId : std::bool_constant<false> {};
256
257template <typename PhysicalDisplay>
258struct IsPhysicalDisplayId<PhysicalDisplayId<PhysicalDisplay>> : std::bool_constant<true> {};
259
260template <typename>
261struct DisplayIdGetter;
262
263template <typename PhysicalDisplay>
264struct DisplayIdGetter<PhysicalDisplayId<PhysicalDisplay>> {
265 static std::optional<DisplayId> get() {
266 if (!PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
267 return getFallbackDisplayId(static_cast<bool>(PhysicalDisplay::PRIMARY)
268 ? HWC_DISPLAY_PRIMARY
269 : HWC_DISPLAY_EXTERNAL);
270 }
271
272 const auto info =
273 parseDisplayIdentificationData(PhysicalDisplay::PORT,
274 PhysicalDisplay::GET_IDENTIFICATION_DATA());
275 return info ? std::make_optional(info->id) : std::nullopt;
276 }
277};
278
Dominik Laskowski34157762018-10-31 13:07:19 -0700279template <DisplayId::Type displayId>
Dominik Laskowski075d3172018-05-24 15:50:06 -0700280struct DisplayIdGetter<VirtualDisplayId<displayId>> {
Dominik Laskowski34157762018-10-31 13:07:19 -0700281 static std::optional<DisplayId> get() { return DisplayId{displayId}; }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700282};
283
284template <>
285struct DisplayIdGetter<NoDisplayId> {
286 static std::optional<DisplayId> get() { return {}; }
287};
288
289// DisplayIdType can be:
290// 1) PhysicalDisplayId<...> for generated ID of physical display backed by HWC.
291// 2) VirtualDisplayId<...> for hard-coded ID of virtual display backed by HWC.
292// 3) NoDisplayId for virtual display without HWC backing.
293template <typename DisplayIdType, int width, int height, Critical critical, Async async,
294 Secure secure, Primary primary, int grallocUsage>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800295struct DisplayVariant {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700296 using DISPLAY_ID = DisplayIdGetter<DisplayIdType>;
297
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800298 // The display width and height
299 static constexpr int WIDTH = width;
300 static constexpr int HEIGHT = height;
301
302 static constexpr int GRALLOC_USAGE = grallocUsage;
303
Dominik Laskowski075d3172018-05-24 15:50:06 -0700304 // Whether the display is virtual or physical
305 static constexpr Virtual VIRTUAL =
306 IsPhysicalDisplayId<DisplayIdType>{} ? Virtual::FALSE : Virtual::TRUE;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800307
308 // When creating native window surfaces for the framebuffer, whether those should be critical
309 static constexpr Critical CRITICAL = critical;
310
311 // When creating native window surfaces for the framebuffer, whether those should be async
312 static constexpr Async ASYNC = async;
313
314 // Whether the display should be treated as secure
315 static constexpr Secure SECURE = secure;
316
Dominik Laskowski075d3172018-05-24 15:50:06 -0700317 // Whether the display is primary
318 static constexpr Primary PRIMARY = primary;
319
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800320 static auto makeFakeExistingDisplayInjector(DisplayTransactionTest* test) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700321 auto injector =
322 FakeDisplayDeviceInjector(test->mFlinger, DISPLAY_ID::get(),
323 static_cast<bool>(VIRTUAL), static_cast<bool>(PRIMARY));
324
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800325 injector.setSecure(static_cast<bool>(SECURE));
Alec Mouriba013fa2018-10-16 12:43:11 -0700326 injector.setNativeWindow(test->mNativeWindow);
327
328 // Creating a DisplayDevice requires getting default dimensions from the
329 // native window.
330 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
331 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
332 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
333 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800334 return injector;
335 }
336
337 // Called by tests to set up any native window creation call expectations.
338 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
339 EXPECT_CALL(*test->mNativeWindowSurface, getNativeWindow())
340 .WillOnce(Return(test->mNativeWindow));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800341
Alec Mouriba013fa2018-10-16 12:43:11 -0700342 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
343 .WillRepeatedly(DoAll(SetArgPointee<1>(WIDTH), Return(0)));
344 EXPECT_CALL(*test->mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
345 .WillRepeatedly(DoAll(SetArgPointee<1>(HEIGHT), Return(0)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800346 }
347
348 static void setupFramebufferConsumerBufferQueueCallExpectations(DisplayTransactionTest* test) {
349 EXPECT_CALL(*test->mConsumer, consumerConnect(_, false)).WillOnce(Return(NO_ERROR));
350 EXPECT_CALL(*test->mConsumer, setConsumerName(_)).WillRepeatedly(Return(NO_ERROR));
351 EXPECT_CALL(*test->mConsumer, setConsumerUsageBits(GRALLOC_USAGE))
352 .WillRepeatedly(Return(NO_ERROR));
353 EXPECT_CALL(*test->mConsumer, setDefaultBufferSize(WIDTH, HEIGHT))
354 .WillRepeatedly(Return(NO_ERROR));
355 EXPECT_CALL(*test->mConsumer, setMaxAcquiredBufferCount(_))
356 .WillRepeatedly(Return(NO_ERROR));
357 }
358
359 static void setupFramebufferProducerBufferQueueCallExpectations(DisplayTransactionTest* test) {
360 EXPECT_CALL(*test->mProducer, allocateBuffers(0, 0, 0, 0)).WillRepeatedly(Return());
361 }
362};
363
Dominik Laskowski075d3172018-05-24 15:50:06 -0700364template <hwc2_display_t hwcDisplayId, HWC2::DisplayType hwcDisplayType, typename DisplayVariant,
365 typename PhysicalDisplay = void>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800366struct HwcDisplayVariant {
367 // The display id supplied by the HWC
368 static constexpr hwc2_display_t HWC_DISPLAY_ID = hwcDisplayId;
369
370 // The HWC display type
371 static constexpr HWC2::DisplayType HWC_DISPLAY_TYPE = hwcDisplayType;
372
373 // The HWC active configuration id
Lloyd Pique3c085a02018-05-09 19:38:32 -0700374 static constexpr int HWC_ACTIVE_CONFIG_ID = 2001;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800375
376 static void injectPendingHotplugEvent(DisplayTransactionTest* test,
377 HWC2::Connection connection) {
378 test->mFlinger.mutablePendingHotplugEvents().emplace_back(
379 HotplugEvent{HWC_DISPLAY_ID, connection});
380 }
381
382 // Called by tests to inject a HWC display setup
383 static void injectHwcDisplay(DisplayTransactionTest* test) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700384 const auto displayId = DisplayVariant::DISPLAY_ID::get();
385 ASSERT_TRUE(displayId);
386 FakeHwcDisplayInjector(*displayId, HWC_DISPLAY_TYPE,
387 static_cast<bool>(DisplayVariant::PRIMARY))
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800388 .setHwcDisplayId(HWC_DISPLAY_ID)
389 .setWidth(DisplayVariant::WIDTH)
390 .setHeight(DisplayVariant::HEIGHT)
391 .setActiveConfig(HWC_ACTIVE_CONFIG_ID)
392 .inject(&test->mFlinger, test->mComposer);
393 }
394
395 static void setupHwcHotplugCallExpectations(DisplayTransactionTest* test) {
396 EXPECT_CALL(*test->mComposer, getDisplayType(HWC_DISPLAY_ID, _))
397 .WillOnce(DoAll(SetArgPointee<1>(static_cast<IComposerClient::DisplayType>(
398 HWC_DISPLAY_TYPE)),
399 Return(Error::NONE)));
400 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
401 EXPECT_CALL(*test->mComposer, getDisplayConfigs(HWC_DISPLAY_ID, _))
402 .WillOnce(DoAll(SetArgPointee<1>(std::vector<unsigned>{HWC_ACTIVE_CONFIG_ID}),
403 Return(Error::NONE)));
404 EXPECT_CALL(*test->mComposer,
405 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
406 IComposerClient::Attribute::WIDTH, _))
407 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::WIDTH), Return(Error::NONE)));
408 EXPECT_CALL(*test->mComposer,
409 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
410 IComposerClient::Attribute::HEIGHT, _))
411 .WillOnce(DoAll(SetArgPointee<3>(DisplayVariant::HEIGHT), Return(Error::NONE)));
412 EXPECT_CALL(*test->mComposer,
413 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
414 IComposerClient::Attribute::VSYNC_PERIOD, _))
415 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_REFRESH_RATE), Return(Error::NONE)));
416 EXPECT_CALL(*test->mComposer,
417 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
418 IComposerClient::Attribute::DPI_X, _))
419 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
420 EXPECT_CALL(*test->mComposer,
421 getDisplayAttribute(HWC_DISPLAY_ID, HWC_ACTIVE_CONFIG_ID,
422 IComposerClient::Attribute::DPI_Y, _))
423 .WillOnce(DoAll(SetArgPointee<3>(DEFAULT_DPI), Return(Error::NONE)));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700424
425 if (PhysicalDisplay::HAS_IDENTIFICATION_DATA) {
426 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
427 .WillOnce(DoAll(SetArgPointee<1>(PhysicalDisplay::PORT),
428 SetArgPointee<2>(PhysicalDisplay::GET_IDENTIFICATION_DATA()),
429 Return(Error::NONE)));
430 } else {
431 EXPECT_CALL(*test->mComposer, getDisplayIdentificationData(HWC_DISPLAY_ID, _, _))
432 .WillOnce(Return(Error::UNSUPPORTED));
433 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800434 }
435
436 // Called by tests to set up HWC call expectations
437 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
438 EXPECT_CALL(*test->mComposer, getActiveConfig(HWC_DISPLAY_ID, _))
Lloyd Pique3c085a02018-05-09 19:38:32 -0700439 .WillRepeatedly(DoAll(SetArgPointee<1>(HWC_ACTIVE_CONFIG_ID), Return(Error::NONE)));
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800440 }
441};
442
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800443// Physical displays are expected to be synchronous, secure, and have a HWC display for output.
444constexpr uint32_t GRALLOC_USAGE_PHYSICAL_DISPLAY =
445 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
446
Dominik Laskowski075d3172018-05-24 15:50:06 -0700447template <hwc2_display_t hwcDisplayId, typename PhysicalDisplay, int width, int height,
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800448 Critical critical>
449struct PhysicalDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700450 : DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height, critical, Async::FALSE,
451 Secure::TRUE, PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
452 HwcDisplayVariant<hwcDisplayId, HWC2::DisplayType::Physical,
453 DisplayVariant<PhysicalDisplayId<PhysicalDisplay>, width, height,
454 critical, Async::FALSE, Secure::TRUE,
455 PhysicalDisplay::PRIMARY, GRALLOC_USAGE_PHYSICAL_DISPLAY>,
456 PhysicalDisplay> {};
457
458template <bool hasIdentificationData>
459struct PrimaryDisplay {
460 static constexpr Primary PRIMARY = Primary::TRUE;
461 static constexpr uint8_t PORT = 255;
462 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
463 static constexpr auto GET_IDENTIFICATION_DATA = getInternalEdid;
464};
465
466template <bool hasIdentificationData>
467struct ExternalDisplay {
468 static constexpr Primary PRIMARY = Primary::FALSE;
469 static constexpr uint8_t PORT = 254;
470 static constexpr bool HAS_IDENTIFICATION_DATA = hasIdentificationData;
471 static constexpr auto GET_IDENTIFICATION_DATA = getExternalEdid;
472};
473
474struct TertiaryDisplay {
475 static constexpr Primary PRIMARY = Primary::FALSE;
476};
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800477
478// A primary display is a physical display that is critical
479using PrimaryDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700480 PhysicalDisplayVariant<1001, PrimaryDisplay<false>, 3840, 2160, Critical::TRUE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800481
482// An external display is physical display that is not critical.
483using ExternalDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700484 PhysicalDisplayVariant<1002, ExternalDisplay<false>, 1920, 1280, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800485
486using TertiaryDisplayVariant =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700487 PhysicalDisplayVariant<1003, TertiaryDisplay, 1600, 1200, Critical::FALSE>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800488
489// A virtual display not supported by the HWC.
490constexpr uint32_t GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY = 0;
491
492template <int width, int height, Secure secure>
493struct NonHwcVirtualDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700494 : DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
495 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY> {
496 using Base = DisplayVariant<NoDisplayId, width, height, Critical::FALSE, Async::TRUE, secure,
497 Primary::FALSE, GRALLOC_USAGE_NONHWC_VIRTUAL_DISPLAY>;
498
499 static void injectHwcDisplay(DisplayTransactionTest*) {}
500
501 static void setupHwcGetActiveConfigCallExpectations(DisplayTransactionTest* test) {
502 EXPECT_CALL(*test->mComposer, getActiveConfig(_, _)).Times(0);
503 }
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800504
505 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
506 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
507 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
508 }
509};
510
511// A virtual display supported by the HWC.
512constexpr uint32_t GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY = GRALLOC_USAGE_HW_COMPOSER;
513
514template <int width, int height, Secure secure>
515struct HwcVirtualDisplayVariant
Dominik Laskowski075d3172018-05-24 15:50:06 -0700516 : DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE, secure,
517 Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>,
518 HwcDisplayVariant<
519 1010, HWC2::DisplayType::Virtual,
520 DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
521 secure, Primary::FALSE, GRALLOC_USAGE_HWC_VIRTUAL_DISPLAY>> {
522 using Base = DisplayVariant<VirtualDisplayId<42>, width, height, Critical::FALSE, Async::TRUE,
523 secure, Primary::FALSE, GRALLOC_USAGE_HW_COMPOSER>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800524 using Self = HwcVirtualDisplayVariant<width, height, secure>;
525
526 static void setupNativeWindowSurfaceCreationCallExpectations(DisplayTransactionTest* test) {
527 Base::setupNativeWindowSurfaceCreationCallExpectations(test);
528 EXPECT_CALL(*test->mNativeWindow, setSwapInterval(0)).Times(1);
529 }
530
531 static void setupHwcVirtualDisplayCreationCallExpectations(DisplayTransactionTest* test) {
532 EXPECT_CALL(*test->mComposer, createVirtualDisplay(Base::WIDTH, Base::HEIGHT, _, _))
533 .WillOnce(DoAll(SetArgPointee<3>(Self::HWC_DISPLAY_ID), Return(Error::NONE)));
534 EXPECT_CALL(*test->mComposer, setClientTargetSlotCount(_)).WillOnce(Return(Error::NONE));
535 }
536};
537
538// For this variant, SurfaceFlinger should not configure itself with wide
539// display support, so the display should not be configured for wide-color
540// support.
541struct WideColorSupportNotConfiguredVariant {
542 static constexpr bool WIDE_COLOR_SUPPORTED = false;
543
544 static void injectConfigChange(DisplayTransactionTest* test) {
545 test->mFlinger.mutableHasWideColorDisplay() = false;
Peiyong Lin13effd12018-07-24 17:01:47 -0700546 test->mFlinger.mutableUseColorManagement() = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800547 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
548 }
549
550 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
551 EXPECT_CALL(*test->mComposer, getColorModes(_, _)).Times(0);
552 EXPECT_CALL(*test->mComposer, getRenderIntents(_, _, _)).Times(0);
553 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
554 }
555};
556
557// For this variant, SurfaceFlinger should configure itself with wide display
558// support, and the display should respond with an non-empty list of supported
559// color modes. Wide-color support should be configured.
560template <typename Display>
561struct WideColorP3ColorimetricSupportedVariant {
562 static constexpr bool WIDE_COLOR_SUPPORTED = true;
563
564 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700565 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800566 test->mFlinger.mutableHasWideColorDisplay() = true;
567 test->mFlinger.mutableDisplayColorSetting() = DisplayColorSetting::UNMANAGED;
568 }
569
570 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
571 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
572 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>({ColorMode::DISPLAY_P3})),
573 Return(Error::NONE)));
574 EXPECT_CALL(*test->mComposer,
575 getRenderIntents(Display::HWC_DISPLAY_ID, ColorMode::DISPLAY_P3, _))
576 .WillOnce(DoAll(SetArgPointee<2>(
577 std::vector<RenderIntent>({RenderIntent::COLORIMETRIC})),
578 Return(Error::NONE)));
579 EXPECT_CALL(*test->mComposer,
580 setColorMode(Display::HWC_DISPLAY_ID, ColorMode::SRGB,
581 RenderIntent::COLORIMETRIC))
582 .WillOnce(Return(Error::NONE));
583 }
584};
585
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800586// For this variant, SurfaceFlinger should configure itself with wide display
587// support, but the display should respond with an empty list of supported color
588// modes. Wide-color support for the display should not be configured.
589template <typename Display>
590struct WideColorNotSupportedVariant {
591 static constexpr bool WIDE_COLOR_SUPPORTED = false;
592
593 static void injectConfigChange(DisplayTransactionTest* test) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700594 test->mFlinger.mutableUseColorManagement() = true;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800595 test->mFlinger.mutableHasWideColorDisplay() = true;
596 }
597
598 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
599 EXPECT_CALL(*test->mComposer, getColorModes(Display::HWC_DISPLAY_ID, _))
600 .WillOnce(DoAll(SetArgPointee<1>(std::vector<ColorMode>()), Return(Error::NONE)));
Chia-I Wu614e1422018-05-23 02:17:03 -0700601 EXPECT_CALL(*test->mComposer, setColorMode(_, _, _)).Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800602 }
603};
604
605// For this variant, the display is not a HWC display, so no HDR support should
606// be configured.
607struct NonHwcDisplayHdrSupportVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800608 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800609 static constexpr bool HDR10_SUPPORTED = false;
610 static constexpr bool HDR_HLG_SUPPORTED = false;
611 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
612 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
613 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _)).Times(0);
614 }
615};
616
Valerie Haue9e843a2018-12-18 13:39:23 -0800617template <typename Display>
618struct Hdr10PlusSupportedVariant {
619 static constexpr bool HDR10_PLUS_SUPPORTED = true;
620 static constexpr bool HDR10_SUPPORTED = true;
621 static constexpr bool HDR_HLG_SUPPORTED = false;
622 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
623 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
624 EXPECT_CALL(*test->mComposer, getHdrCapabilities(_, _, _, _, _))
625 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({
626 Hdr::HDR10_PLUS,
627 Hdr::HDR10,
628 })),
629 Return(Error::NONE)));
630 }
631};
632
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800633// For this variant, the composer should respond with a non-empty list of HDR
634// modes containing HDR10, so HDR10 support should be configured.
635template <typename Display>
636struct Hdr10SupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800637 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800638 static constexpr bool HDR10_SUPPORTED = true;
639 static constexpr bool HDR_HLG_SUPPORTED = false;
640 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
641 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
642 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
643 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HDR10})),
644 Return(Error::NONE)));
645 }
646};
647
648// For this variant, the composer should respond with a non-empty list of HDR
649// modes containing HLG, so HLG support should be configured.
650template <typename Display>
651struct HdrHlgSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800652 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800653 static constexpr bool HDR10_SUPPORTED = false;
654 static constexpr bool HDR_HLG_SUPPORTED = true;
655 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
656 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
657 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
658 .WillOnce(
659 DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::HLG})), Return(Error::NONE)));
660 }
661};
662
663// For this variant, the composer should respond with a non-empty list of HDR
664// modes containing DOLBY_VISION, so DOLBY_VISION support should be configured.
665template <typename Display>
666struct HdrDolbyVisionSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800667 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800668 static constexpr bool HDR10_SUPPORTED = false;
669 static constexpr bool HDR_HLG_SUPPORTED = false;
670 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = true;
671 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
672 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
673 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>({Hdr::DOLBY_VISION})),
674 Return(Error::NONE)));
675 }
676};
677
678// For this variant, the composer should respond with am empty list of HDR
679// modes, so no HDR support should be configured.
680template <typename Display>
681struct HdrNotSupportedVariant {
Valerie Haue9e843a2018-12-18 13:39:23 -0800682 static constexpr bool HDR10_PLUS_SUPPORTED = false;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800683 static constexpr bool HDR10_SUPPORTED = false;
684 static constexpr bool HDR_HLG_SUPPORTED = false;
685 static constexpr bool HDR_DOLBY_VISION_SUPPORTED = false;
686 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
687 EXPECT_CALL(*test->mComposer, getHdrCapabilities(Display::HWC_DISPLAY_ID, _, _, _, _))
688 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hdr>()), Return(Error::NONE)));
689 }
690};
691
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700692struct NonHwcPerFrameMetadataSupportVariant {
693 static constexpr int PER_FRAME_METADATA_KEYS = 0;
694 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800695 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(_)).Times(0);
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700696 }
697};
698
699template <typename Display>
700struct NoPerFrameMetadataSupportVariant {
701 static constexpr int PER_FRAME_METADATA_KEYS = 0;
702 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800703 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
704 .WillOnce(Return(std::vector<PerFrameMetadataKey>()));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700705 }
706};
707
708template <typename Display>
709struct Smpte2086PerFrameMetadataSupportVariant {
710 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::SMPTE2086;
711 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800712 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
713 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Valerie Haue9e843a2018-12-18 13:39:23 -0800714 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X,
715 PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y,
716 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X,
717 PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y,
718 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X,
719 PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y,
720 PerFrameMetadataKey::WHITE_POINT_X,
721 PerFrameMetadataKey::WHITE_POINT_Y,
722 PerFrameMetadataKey::MAX_LUMINANCE,
723 PerFrameMetadataKey::MIN_LUMINANCE,
724 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700725 }
726};
727
728template <typename Display>
729struct Cta861_3_PerFrameMetadataSupportVariant {
730 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::CTA861_3;
731 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
Chia-I Wud7e01d72018-06-21 13:39:09 +0800732 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
733 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
Valerie Haue9e843a2018-12-18 13:39:23 -0800734 PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL,
735 PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL,
736 })));
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700737 }
738};
739
Valerie Haue9e843a2018-12-18 13:39:23 -0800740template <typename Display>
741struct Hdr10_Plus_PerFrameMetadataSupportVariant {
742 static constexpr int PER_FRAME_METADATA_KEYS = HdrMetadata::Type::HDR10PLUS;
743 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
744 EXPECT_CALL(*test->mComposer, getPerFrameMetadataKeys(Display::HWC_DISPLAY_ID))
745 .WillOnce(Return(std::vector<PerFrameMetadataKey>({
746 PerFrameMetadataKey::HDR10_PLUS_SEI,
747 })));
748 }
749};
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800750/* ------------------------------------------------------------------------
751 * Typical display configurations to test
752 */
753
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700754template <typename DisplayPolicy, typename WideColorSupportPolicy, typename HdrSupportPolicy,
755 typename PerFrameMetadataSupportPolicy>
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800756struct Case {
757 using Display = DisplayPolicy;
758 using WideColorSupport = WideColorSupportPolicy;
759 using HdrSupport = HdrSupportPolicy;
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700760 using PerFrameMetadataSupport = PerFrameMetadataSupportPolicy;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800761};
762
763using SimplePrimaryDisplayCase =
764 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700765 HdrNotSupportedVariant<PrimaryDisplayVariant>,
766 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800767using SimpleExternalDisplayCase =
768 Case<ExternalDisplayVariant, WideColorNotSupportedVariant<ExternalDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700769 HdrNotSupportedVariant<ExternalDisplayVariant>,
770 NoPerFrameMetadataSupportVariant<ExternalDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800771using SimpleTertiaryDisplayCase =
772 Case<TertiaryDisplayVariant, WideColorNotSupportedVariant<TertiaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700773 HdrNotSupportedVariant<TertiaryDisplayVariant>,
774 NoPerFrameMetadataSupportVariant<TertiaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800775using NonHwcVirtualDisplayCase =
776 Case<NonHwcVirtualDisplayVariant<1024, 768, Secure::FALSE>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700777 WideColorSupportNotConfiguredVariant, NonHwcDisplayHdrSupportVariant,
778 NonHwcPerFrameMetadataSupportVariant>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800779using SimpleHwcVirtualDisplayVariant = HwcVirtualDisplayVariant<1024, 768, Secure::TRUE>;
780using HwcVirtualDisplayCase =
781 Case<SimpleHwcVirtualDisplayVariant, WideColorSupportNotConfiguredVariant,
Lloyd Pique438e9e72018-09-04 18:06:08 -0700782 HdrNotSupportedVariant<SimpleHwcVirtualDisplayVariant>,
tangrobin6753a022018-08-10 10:58:54 +0800783 NoPerFrameMetadataSupportVariant<SimpleHwcVirtualDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800784using WideColorP3ColorimetricDisplayCase =
785 Case<PrimaryDisplayVariant, WideColorP3ColorimetricSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700786 HdrNotSupportedVariant<PrimaryDisplayVariant>,
787 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Valerie Haue9e843a2018-12-18 13:39:23 -0800788using Hdr10PlusDisplayCase =
789 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
790 Hdr10SupportedVariant<PrimaryDisplayVariant>,
791 Hdr10_Plus_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800792using Hdr10DisplayCase =
793 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700794 Hdr10SupportedVariant<PrimaryDisplayVariant>,
795 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800796using HdrHlgDisplayCase =
797 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700798 HdrHlgSupportedVariant<PrimaryDisplayVariant>,
799 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800800using HdrDolbyVisionDisplayCase =
801 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
Lloyd Piqued883d5a2018-04-27 19:32:30 -0700802 HdrDolbyVisionSupportedVariant<PrimaryDisplayVariant>,
803 NoPerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
804using HdrSmpte2086DisplayCase =
805 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
806 HdrNotSupportedVariant<PrimaryDisplayVariant>,
807 Smpte2086PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
808using HdrCta861_3_DisplayCase =
809 Case<PrimaryDisplayVariant, WideColorNotSupportedVariant<PrimaryDisplayVariant>,
810 HdrNotSupportedVariant<PrimaryDisplayVariant>,
811 Cta861_3_PerFrameMetadataSupportVariant<PrimaryDisplayVariant>>;
Dominik Laskowskif07b85b2018-06-11 12:49:15 -0700812
Lloyd Piquec11e0d32018-01-22 18:44:59 -0800813/* ------------------------------------------------------------------------
Lloyd Pique6cf11032018-01-22 18:57:44 -0800814 *
815 * SurfaceFlinger::onHotplugReceived
816 */
817
818TEST_F(DisplayTransactionTest, hotplugEnqueuesEventsForDisplayTransaction) {
819 constexpr int currentSequenceId = 123;
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700820 constexpr hwc2_display_t hwcDisplayId1 = 456;
821 constexpr hwc2_display_t hwcDisplayId2 = 654;
Lloyd Pique6cf11032018-01-22 18:57:44 -0800822
823 // --------------------------------------------------------------------
824 // Preconditions
825
826 // Set the current sequence id for accepted events
827 mFlinger.mutableComposerSequenceId() = currentSequenceId;
828
829 // Set the main thread id so that the current thread does not appear to be
830 // the main thread.
831 mFlinger.mutableMainThreadId() = std::thread::id();
832
833 // --------------------------------------------------------------------
834 // Call Expectations
835
836 // We expect invalidate() to be invoked once to trigger display transaction
837 // processing.
838 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
839
840 // --------------------------------------------------------------------
841 // Invocation
842
843 // Simulate two hotplug events (a connect and a disconnect)
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700844 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId1, HWC2::Connection::Connected);
845 mFlinger.onHotplugReceived(currentSequenceId, hwcDisplayId2, HWC2::Connection::Disconnected);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800846
847 // --------------------------------------------------------------------
848 // Postconditions
849
850 // The display transaction needed flag should be set.
851 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
852
853 // All events should be in the pending event queue.
854 const auto& pendingEvents = mFlinger.mutablePendingHotplugEvents();
855 ASSERT_EQ(2u, pendingEvents.size());
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700856 EXPECT_EQ(hwcDisplayId1, pendingEvents[0].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800857 EXPECT_EQ(HWC2::Connection::Connected, pendingEvents[0].connection);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700858 EXPECT_EQ(hwcDisplayId2, pendingEvents[1].hwcDisplayId);
Lloyd Pique6cf11032018-01-22 18:57:44 -0800859 EXPECT_EQ(HWC2::Connection::Disconnected, pendingEvents[1].connection);
860}
861
862TEST_F(DisplayTransactionTest, hotplugDiscardsUnexpectedEvents) {
863 constexpr int currentSequenceId = 123;
864 constexpr int otherSequenceId = 321;
865 constexpr hwc2_display_t displayId = 456;
866
867 // --------------------------------------------------------------------
868 // Preconditions
869
870 // Set the current sequence id for accepted events
871 mFlinger.mutableComposerSequenceId() = currentSequenceId;
872
873 // Set the main thread id so that the current thread does not appear to be
874 // the main thread.
875 mFlinger.mutableMainThreadId() = std::thread::id();
876
877 // --------------------------------------------------------------------
878 // Call Expectations
879
880 // We do not expect any calls to invalidate().
881 EXPECT_CALL(*mMessageQueue, invalidate()).Times(0);
882
883 // --------------------------------------------------------------------
884 // Invocation
885
886 // Call with an unexpected sequence id
887 mFlinger.onHotplugReceived(otherSequenceId, displayId, HWC2::Connection::Invalid);
888
889 // --------------------------------------------------------------------
890 // Postconditions
891
892 // The display transaction needed flag should not be set
893 EXPECT_FALSE(hasTransactionFlagSet(eDisplayTransactionNeeded));
894
895 // There should be no pending events
896 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
897}
898
899TEST_F(DisplayTransactionTest, hotplugProcessesEnqueuedEventsIfCalledOnMainThread) {
900 constexpr int currentSequenceId = 123;
901 constexpr hwc2_display_t displayId1 = 456;
902
903 // --------------------------------------------------------------------
904 // Note:
905 // --------------------------------------------------------------------
906 // This test case is a bit tricky. We want to verify that
907 // onHotplugReceived() calls processDisplayHotplugEventsLocked(), but we
908 // don't really want to provide coverage for everything the later function
909 // does as there are specific tests for it.
910 // --------------------------------------------------------------------
911
912 // --------------------------------------------------------------------
913 // Preconditions
914
915 // Set the current sequence id for accepted events
916 mFlinger.mutableComposerSequenceId() = currentSequenceId;
917
918 // Set the main thread id so that the current thread does appear to be the
919 // main thread.
920 mFlinger.mutableMainThreadId() = std::this_thread::get_id();
921
922 // --------------------------------------------------------------------
923 // Call Expectations
924
925 // We expect invalidate() to be invoked once to trigger display transaction
926 // processing.
927 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
928
929 // --------------------------------------------------------------------
930 // Invocation
931
932 // Simulate a disconnect on a display id that is not connected. This should
933 // be enqueued by onHotplugReceived(), and dequeued by
934 // processDisplayHotplugEventsLocked(), but then ignored as invalid.
935 mFlinger.onHotplugReceived(currentSequenceId, displayId1, HWC2::Connection::Disconnected);
936
937 // --------------------------------------------------------------------
938 // Postconditions
939
940 // The display transaction needed flag should be set.
941 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
942
943 // There should be no event queued on return, as it should have been
944 // processed.
945 EXPECT_TRUE(mFlinger.mutablePendingHotplugEvents().empty());
946}
947
948/* ------------------------------------------------------------------------
Lloyd Piquea482f992018-01-22 19:00:34 -0800949 * SurfaceFlinger::createDisplay
950 */
951
952TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForNonsecureDisplay) {
953 const String8 name("virtual.test");
954
955 // --------------------------------------------------------------------
956 // Call Expectations
957
958 // The call should notify the interceptor that a display was created.
959 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
960
961 // --------------------------------------------------------------------
962 // Invocation
963
964 sp<IBinder> displayToken = mFlinger.createDisplay(name, false);
965
966 // --------------------------------------------------------------------
967 // Postconditions
968
969 // The display should have been added to the current state
970 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
971 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700972 EXPECT_TRUE(display.isVirtual());
973 EXPECT_FALSE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -0800974 EXPECT_EQ(name.string(), display.displayName);
975
976 // --------------------------------------------------------------------
977 // Cleanup conditions
978
979 // Destroying the display invalidates the display state.
980 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
981}
982
983TEST_F(DisplayTransactionTest, createDisplaySetsCurrentStateForSecureDisplay) {
984 const String8 name("virtual.test");
985
986 // --------------------------------------------------------------------
987 // Call Expectations
988
989 // The call should notify the interceptor that a display was created.
990 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
991
992 // --------------------------------------------------------------------
993 // Invocation
994
995 sp<IBinder> displayToken = mFlinger.createDisplay(name, true);
996
997 // --------------------------------------------------------------------
998 // Postconditions
999
1000 // The display should have been added to the current state
1001 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1002 const auto& display = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001003 EXPECT_TRUE(display.isVirtual());
1004 EXPECT_TRUE(display.isSecure);
Lloyd Piquea482f992018-01-22 19:00:34 -08001005 EXPECT_EQ(name.string(), display.displayName);
1006
1007 // --------------------------------------------------------------------
1008 // Cleanup conditions
1009
1010 // Destroying the display invalidates the display state.
1011 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1012}
1013
1014/* ------------------------------------------------------------------------
1015 * SurfaceFlinger::destroyDisplay
1016 */
1017
1018TEST_F(DisplayTransactionTest, destroyDisplayClearsCurrentStateForDisplay) {
1019 using Case = NonHwcVirtualDisplayCase;
1020
1021 // --------------------------------------------------------------------
1022 // Preconditions
1023
1024 // A virtual display exists
1025 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1026 existing.inject();
1027
1028 // --------------------------------------------------------------------
1029 // Call Expectations
1030
1031 // The call should notify the interceptor that a display was created.
1032 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
1033
1034 // Destroying the display invalidates the display state.
1035 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
1036
1037 // --------------------------------------------------------------------
1038 // Invocation
1039
1040 mFlinger.destroyDisplay(existing.token());
1041
1042 // --------------------------------------------------------------------
1043 // Postconditions
1044
1045 // The display should have been removed from the current state
1046 EXPECT_FALSE(hasCurrentDisplayState(existing.token()));
1047
1048 // Ths display should still exist in the drawing state
1049 EXPECT_TRUE(hasDrawingDisplayState(existing.token()));
1050
1051 // The display transaction needed flasg should be set
1052 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
1053}
1054
1055TEST_F(DisplayTransactionTest, destroyDisplayHandlesUnknownDisplay) {
1056 // --------------------------------------------------------------------
1057 // Preconditions
1058
1059 sp<BBinder> displayToken = new BBinder();
1060
1061 // --------------------------------------------------------------------
1062 // Invocation
1063
1064 mFlinger.destroyDisplay(displayToken);
1065}
1066
1067/* ------------------------------------------------------------------------
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001068 * SurfaceFlinger::resetDisplayState
1069 */
1070
1071TEST_F(DisplayTransactionTest, resetDisplayStateClearsState) {
1072 using Case = NonHwcVirtualDisplayCase;
1073
1074 // --------------------------------------------------------------------
1075 // Preconditions
1076
1077 // vsync is enabled and available
1078 mFlinger.mutablePrimaryHWVsyncEnabled() = true;
1079 mFlinger.mutableHWVsyncAvailable() = true;
1080
1081 // A display exists
1082 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1083 existing.inject();
1084
1085 // --------------------------------------------------------------------
1086 // Call Expectations
1087
1088 // The call disable vsyncs
1089 EXPECT_CALL(*mEventControlThread, setVsyncEnabled(false)).Times(1);
1090
Lloyd Pique41be5d22018-06-21 13:11:48 -07001091 // The call ends any display resyncs
1092 EXPECT_CALL(*mPrimaryDispSync, endResync()).Times(1);
1093
Lloyd Piqued6fbb8a2018-01-22 19:08:36 -08001094 // --------------------------------------------------------------------
1095 // Invocation
1096
1097 mFlinger.resetDisplayState();
1098
1099 // --------------------------------------------------------------------
1100 // Postconditions
1101
1102 // vsyncs should be off and not available.
1103 EXPECT_FALSE(mFlinger.mutablePrimaryHWVsyncEnabled());
1104 EXPECT_FALSE(mFlinger.mutableHWVsyncAvailable());
1105
1106 // The display should have been removed from the display map.
1107 EXPECT_FALSE(hasDisplayDevice(existing.token()));
1108
1109 // The display should still exist in the current state
1110 EXPECT_TRUE(hasCurrentDisplayState(existing.token()));
1111
1112 // The display should have been removed from the drawing state
1113 EXPECT_FALSE(hasDrawingDisplayState(existing.token()));
1114}
1115
1116/* ------------------------------------------------------------------------
Valerie Hau9758ae02018-10-09 16:05:09 -07001117 * DisplayDevice::GetBestColorMode
1118 */
1119class GetBestColorModeTest : public DisplayTransactionTest {
1120public:
Dominik Laskowski34157762018-10-31 13:07:19 -07001121 static constexpr DisplayId DEFAULT_DISPLAY_ID = DisplayId{777};
Dominik Laskowski075d3172018-05-24 15:50:06 -07001122
Valerie Hau9758ae02018-10-09 16:05:09 -07001123 GetBestColorModeTest()
1124 : DisplayTransactionTest(),
Dominik Laskowski075d3172018-05-24 15:50:06 -07001125 mInjector(FakeDisplayDeviceInjector(mFlinger, DEFAULT_DISPLAY_ID, false /* isVirtual */,
1126 true /* isPrimary */)) {}
Valerie Hau9758ae02018-10-09 16:05:09 -07001127
1128 void setHasWideColorGamut(bool hasWideColorGamut) { mHasWideColorGamut = hasWideColorGamut; }
1129
1130 void addHwcColorModesMapping(ui::ColorMode colorMode,
1131 std::vector<ui::RenderIntent> renderIntents) {
1132 mHwcColorModes[colorMode] = renderIntents;
1133 }
1134
1135 void setInputDataspace(ui::Dataspace dataspace) { mInputDataspace = dataspace; }
1136
1137 void setInputRenderIntent(ui::RenderIntent renderIntent) { mInputRenderIntent = renderIntent; }
1138
1139 void getBestColorMode() {
1140 mInjector.setHwcColorModes(mHwcColorModes);
1141 mInjector.setHasWideColorGamut(mHasWideColorGamut);
Alec Mouriba013fa2018-10-16 12:43:11 -07001142 mInjector.setNativeWindow(mNativeWindow);
1143
1144 // Creating a DisplayDevice requires getting default dimensions from the
1145 // native window.
1146 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
1147 .WillRepeatedly(DoAll(SetArgPointee<1>(1080 /* arbitrary */), Return(0)));
1148 EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
1149 .WillRepeatedly(DoAll(SetArgPointee<1>(1920 /* arbitrary */), Return(0)));
Alec Mouri4efb6c22018-11-16 13:07:47 -08001150 EXPECT_CALL(*mNativeWindow, perform(9)).Times(1);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001151 EXPECT_CALL(*mNativeWindow, perform(13)).Times(1);
Alec Mouri4efb6c22018-11-16 13:07:47 -08001152 EXPECT_CALL(*mNativeWindow, perform(30)).Times(1);
Valerie Hau9758ae02018-10-09 16:05:09 -07001153 auto displayDevice = mInjector.inject();
1154
1155 displayDevice->getBestColorMode(mInputDataspace, mInputRenderIntent, &mOutDataspace,
1156 &mOutColorMode, &mOutRenderIntent);
1157 }
1158
1159 ui::Dataspace mOutDataspace;
1160 ui::ColorMode mOutColorMode;
1161 ui::RenderIntent mOutRenderIntent;
1162
1163private:
1164 ui::Dataspace mInputDataspace;
1165 ui::RenderIntent mInputRenderIntent;
1166 bool mHasWideColorGamut = false;
1167 std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> mHwcColorModes;
1168 FakeDisplayDeviceInjector mInjector;
1169};
1170
1171TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeSRGB) {
1172 addHwcColorModesMapping(ui::ColorMode::SRGB,
1173 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1174 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1175 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1176 setHasWideColorGamut(true);
1177
1178 getBestColorMode();
1179
Peiyong Lin14724e62018-12-05 07:27:30 -08001180 ASSERT_EQ(ui::Dataspace::V0_SRGB, mOutDataspace);
Valerie Hau9758ae02018-10-09 16:05:09 -07001181 ASSERT_EQ(ui::ColorMode::SRGB, mOutColorMode);
1182 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1183}
1184
1185TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDisplayP3) {
1186 addHwcColorModesMapping(ui::ColorMode::DISPLAY_P3,
1187 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1188 addHwcColorModesMapping(ui::ColorMode::SRGB,
1189 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1190 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1191 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1192 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1193 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1194 setHasWideColorGamut(true);
1195
1196 getBestColorMode();
1197
1198 ASSERT_EQ(ui::Dataspace::DISPLAY_P3, mOutDataspace);
1199 ASSERT_EQ(ui::ColorMode::DISPLAY_P3, mOutColorMode);
1200 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1201}
1202
1203TEST_F(GetBestColorModeTest, DataspaceDisplayP3_ColorModeDISPLAY_BT2020) {
1204 addHwcColorModesMapping(ui::ColorMode::DISPLAY_BT2020,
1205 std::vector<ui::RenderIntent>(1, RenderIntent::COLORIMETRIC));
1206 setInputDataspace(ui::Dataspace::DISPLAY_P3);
1207 setInputRenderIntent(ui::RenderIntent::COLORIMETRIC);
1208 setHasWideColorGamut(true);
1209
1210 getBestColorMode();
1211
1212 ASSERT_EQ(ui::Dataspace::DISPLAY_BT2020, mOutDataspace);
1213 ASSERT_EQ(ui::ColorMode::DISPLAY_BT2020, mOutColorMode);
1214 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mOutRenderIntent);
1215}
1216
1217/* ------------------------------------------------------------------------
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001218 * SurfaceFlinger::setupNewDisplayDeviceInternal
1219 */
1220
1221class SetupNewDisplayDeviceInternalTest : public DisplayTransactionTest {
1222public:
1223 template <typename T>
1224 void setupNewDisplayDeviceInternalTest();
1225};
1226
1227template <typename Case>
1228void SetupNewDisplayDeviceInternalTest::setupNewDisplayDeviceInternalTest() {
1229 const sp<BBinder> displayToken = new BBinder();
Lloyd Pique542307f2018-10-19 13:24:08 -07001230 const sp<compositionengine::mock::DisplaySurface> displaySurface =
1231 new compositionengine::mock::DisplaySurface();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001232 const sp<mock::GraphicBufferProducer> producer = new mock::GraphicBufferProducer();
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001233
1234 // --------------------------------------------------------------------
1235 // Preconditions
1236
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001237 // Wide color displays support is configured appropriately
1238 Case::WideColorSupport::injectConfigChange(this);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001239
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001240 // The display is setup with the HWC.
1241 Case::Display::injectHwcDisplay(this);
1242
1243 // SurfaceFlinger will use a test-controlled factory for native window
1244 // surfaces.
1245 injectFakeNativeWindowSurfaceFactory();
1246
1247 // --------------------------------------------------------------------
1248 // Call Expectations
1249
1250 // Various native window calls will be made.
1251 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
Lloyd Pique3c085a02018-05-09 19:38:32 -07001252 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001253 Case::WideColorSupport::setupComposerCallExpectations(this);
1254 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001255 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001256
1257 // --------------------------------------------------------------------
1258 // Invocation
1259
1260 DisplayDeviceState state;
Dominik Laskowski075d3172018-05-24 15:50:06 -07001261 state.displayId = static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1262 : Case::Display::DISPLAY_ID::get();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001263 state.isSecure = static_cast<bool>(Case::Display::SECURE);
1264
Dominik Laskowski075d3172018-05-24 15:50:06 -07001265 auto device =
1266 mFlinger.setupNewDisplayDeviceInternal(displayToken, Case::Display::DISPLAY_ID::get(),
1267 state, displaySurface, producer);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001268
1269 // --------------------------------------------------------------------
1270 // Postconditions
1271
1272 ASSERT_TRUE(device != nullptr);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001273 EXPECT_EQ(Case::Display::DISPLAY_ID::get(), device->getId());
1274 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), device->isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001275 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001276 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001277 EXPECT_EQ(Case::Display::WIDTH, device->getWidth());
1278 EXPECT_EQ(Case::Display::HEIGHT, device->getHeight());
1279 EXPECT_EQ(Case::WideColorSupport::WIDE_COLOR_SUPPORTED, device->hasWideColorGamut());
Valerie Haue9e843a2018-12-18 13:39:23 -08001280 EXPECT_EQ(Case::HdrSupport::HDR10_PLUS_SUPPORTED, device->hasHDR10PlusSupport());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001281 EXPECT_EQ(Case::HdrSupport::HDR10_SUPPORTED, device->hasHDR10Support());
1282 EXPECT_EQ(Case::HdrSupport::HDR_HLG_SUPPORTED, device->hasHLGSupport());
1283 EXPECT_EQ(Case::HdrSupport::HDR_DOLBY_VISION_SUPPORTED, device->hasDolbyVisionSupport());
Lloyd Pique3c085a02018-05-09 19:38:32 -07001284 // Note: This is not Case::Display::HWC_ACTIVE_CONFIG_ID as the ids are
1285 // remapped, and the test only ever sets up one config. If there were an error
1286 // looking up the remapped index, device->getActiveConfig() would be -1 instead.
1287 EXPECT_EQ(0, device->getActiveConfig());
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001288 EXPECT_EQ(Case::PerFrameMetadataSupport::PER_FRAME_METADATA_KEYS,
1289 device->getSupportedPerFrameMetadata());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001290}
1291
1292TEST_F(SetupNewDisplayDeviceInternalTest, createSimplePrimaryDisplay) {
1293 setupNewDisplayDeviceInternalTest<SimplePrimaryDisplayCase>();
1294}
1295
1296TEST_F(SetupNewDisplayDeviceInternalTest, createSimpleExternalDisplay) {
1297 setupNewDisplayDeviceInternalTest<SimpleExternalDisplayCase>();
1298}
1299
1300TEST_F(SetupNewDisplayDeviceInternalTest, createNonHwcVirtualDisplay) {
1301 setupNewDisplayDeviceInternalTest<NonHwcVirtualDisplayCase>();
1302}
1303
1304TEST_F(SetupNewDisplayDeviceInternalTest, createHwcVirtualDisplay) {
Dominik Laskowski075d3172018-05-24 15:50:06 -07001305 using Case = HwcVirtualDisplayCase;
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001306
Dominik Laskowski075d3172018-05-24 15:50:06 -07001307 // Insert display data so that the HWC thinks it created the virtual display.
1308 const auto displayId = Case::Display::DISPLAY_ID::get();
1309 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08001310 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001311
1312 setupNewDisplayDeviceInternalTest<Case>();
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001313}
1314
1315TEST_F(SetupNewDisplayDeviceInternalTest, createWideColorP3Display) {
1316 setupNewDisplayDeviceInternalTest<WideColorP3ColorimetricDisplayCase>();
1317}
1318
Valerie Haue9e843a2018-12-18 13:39:23 -08001319TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10PlusDisplay) {
1320 setupNewDisplayDeviceInternalTest<Hdr10PlusDisplayCase>();
1321}
1322
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001323TEST_F(SetupNewDisplayDeviceInternalTest, createHdr10Display) {
1324 setupNewDisplayDeviceInternalTest<Hdr10DisplayCase>();
1325}
1326
1327TEST_F(SetupNewDisplayDeviceInternalTest, createHdrHlgDisplay) {
1328 setupNewDisplayDeviceInternalTest<HdrHlgDisplayCase>();
1329}
1330
1331TEST_F(SetupNewDisplayDeviceInternalTest, createHdrDolbyVisionDisplay) {
1332 setupNewDisplayDeviceInternalTest<HdrDolbyVisionDisplayCase>();
1333}
1334
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001335TEST_F(SetupNewDisplayDeviceInternalTest, createHdrSmpte2086DisplayCase) {
1336 setupNewDisplayDeviceInternalTest<HdrSmpte2086DisplayCase>();
1337}
1338
1339TEST_F(SetupNewDisplayDeviceInternalTest, createHdrCta816_3_DisplayCase) {
1340 setupNewDisplayDeviceInternalTest<HdrCta861_3_DisplayCase>();
1341}
1342
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001343/* ------------------------------------------------------------------------
1344 * SurfaceFlinger::handleTransactionLocked(eDisplayTransactionNeeded)
1345 */
1346
1347class HandleTransactionLockedTest : public DisplayTransactionTest {
1348public:
1349 template <typename Case>
1350 void setupCommonPreconditions();
1351
1352 template <typename Case>
1353 void setupCommonCallExpectationsForConnectProcessing();
1354
1355 template <typename Case>
1356 void setupCommonCallExpectationsForDisconnectProcessing();
1357
1358 template <typename Case>
1359 void processesHotplugConnectCommon();
1360
1361 template <typename Case>
1362 void ignoresHotplugConnectCommon();
1363
1364 template <typename Case>
1365 void processesHotplugDisconnectCommon();
1366
1367 template <typename Case>
1368 void verifyDisplayIsConnected(const sp<IBinder>& displayToken);
1369
1370 template <typename Case>
1371 void verifyPhysicalDisplayIsConnected();
1372
1373 void verifyDisplayIsNotConnected(const sp<IBinder>& displayToken);
1374};
1375
1376template <typename Case>
1377void HandleTransactionLockedTest::setupCommonPreconditions() {
1378 // Wide color displays support is configured appropriately
1379 Case::WideColorSupport::injectConfigChange(this);
1380
1381 // SurfaceFlinger will use a test-controlled factory for BufferQueues
1382 injectFakeBufferQueueFactory();
1383
1384 // SurfaceFlinger will use a test-controlled factory for native window
1385 // surfaces.
1386 injectFakeNativeWindowSurfaceFactory();
1387}
1388
1389template <typename Case>
1390void HandleTransactionLockedTest::setupCommonCallExpectationsForConnectProcessing() {
1391 Case::Display::setupHwcHotplugCallExpectations(this);
1392
1393 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1394 Case::Display::setupFramebufferProducerBufferQueueCallExpectations(this);
1395 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1396 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
1397
1398 Case::WideColorSupport::setupComposerCallExpectations(this);
1399 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001400 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001401
1402 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayCreation(_)).Times(1);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001403 EXPECT_CALL(*mEventThread,
Dominik Laskowski075d3172018-05-24 15:50:06 -07001404 onHotplugReceived(static_cast<bool>(Case::Display::PRIMARY)
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001405 ? EventThread::DisplayType::Primary
1406 : EventThread::DisplayType::External,
1407 true))
1408 .Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001409}
1410
1411template <typename Case>
1412void HandleTransactionLockedTest::setupCommonCallExpectationsForDisconnectProcessing() {
1413 EXPECT_CALL(*mSurfaceInterceptor, saveDisplayDeletion(_)).Times(1);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001414 EXPECT_CALL(*mEventThread,
Dominik Laskowski075d3172018-05-24 15:50:06 -07001415 onHotplugReceived(static_cast<bool>(Case::Display::PRIMARY)
Dominik Laskowski00a6fa22018-06-06 16:42:02 -07001416 ? EventThread::DisplayType::Primary
1417 : EventThread::DisplayType::External,
1418 false))
1419 .Times(1);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001420}
1421
1422template <typename Case>
1423void HandleTransactionLockedTest::verifyDisplayIsConnected(const sp<IBinder>& displayToken) {
1424 // The display device should have been set up in the list of displays.
1425 ASSERT_TRUE(hasDisplayDevice(displayToken));
1426 const auto& device = getDisplayDevice(displayToken);
1427 EXPECT_EQ(static_cast<bool>(Case::Display::SECURE), device->isSecure());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001428 EXPECT_EQ(static_cast<bool>(Case::Display::PRIMARY), device->isPrimary());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001429
1430 // The display should have been set up in the current display state
1431 ASSERT_TRUE(hasCurrentDisplayState(displayToken));
1432 const auto& current = getCurrentDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001433 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), current.isVirtual());
1434 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1435 : Case::Display::DISPLAY_ID::get(),
1436 current.displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001437
1438 // The display should have been set up in the drawing display state
1439 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1440 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001441 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
1442 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL) ? std::nullopt
1443 : Case::Display::DISPLAY_ID::get(),
1444 draw.displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001445}
1446
1447template <typename Case>
1448void HandleTransactionLockedTest::verifyPhysicalDisplayIsConnected() {
1449 // HWComposer should have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001450 EXPECT_TRUE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001451
Dominik Laskowski075d3172018-05-24 15:50:06 -07001452 // SF should have a display token.
1453 const auto displayId = Case::Display::DISPLAY_ID::get();
1454 ASSERT_TRUE(displayId);
1455 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1456 auto& displayToken = mFlinger.mutablePhysicalDisplayTokens()[*displayId];
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001457
1458 verifyDisplayIsConnected<Case>(displayToken);
1459}
1460
1461void HandleTransactionLockedTest::verifyDisplayIsNotConnected(const sp<IBinder>& displayToken) {
1462 EXPECT_FALSE(hasDisplayDevice(displayToken));
1463 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
1464 EXPECT_FALSE(hasDrawingDisplayState(displayToken));
1465}
1466
1467template <typename Case>
1468void HandleTransactionLockedTest::processesHotplugConnectCommon() {
1469 // --------------------------------------------------------------------
1470 // Preconditions
1471
1472 setupCommonPreconditions<Case>();
1473
1474 // A hotplug connect event is enqueued for a display
1475 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001476
1477 // --------------------------------------------------------------------
1478 // Call Expectations
1479
1480 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillOnce(Return(false));
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001481
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001482 setupCommonCallExpectationsForConnectProcessing<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001483
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001484 // --------------------------------------------------------------------
1485 // Invocation
Lloyd Piquee39cad22017-12-20 17:01:29 -08001486
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001487 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
Lloyd Piquee39cad22017-12-20 17:01:29 -08001488
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001489 // --------------------------------------------------------------------
1490 // Postconditions
1491
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001492 verifyPhysicalDisplayIsConnected<Case>();
Lloyd Piquee39cad22017-12-20 17:01:29 -08001493
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001494 // --------------------------------------------------------------------
1495 // Cleanup conditions
1496
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001497 EXPECT_CALL(*mComposer,
1498 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
Lloyd Pique1fa4d462018-01-22 18:03:16 -08001499 .WillOnce(Return(Error::NONE));
1500 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
Lloyd Piquef58625d2017-12-19 13:22:33 -08001501}
1502
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001503template <typename Case>
1504void HandleTransactionLockedTest::ignoresHotplugConnectCommon() {
1505 // --------------------------------------------------------------------
1506 // Preconditions
1507
1508 setupCommonPreconditions<Case>();
1509
1510 // A hotplug connect event is enqueued for a display
1511 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1512
1513 // --------------------------------------------------------------------
1514 // Invocation
1515
1516 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1517
1518 // --------------------------------------------------------------------
1519 // Postconditions
1520
1521 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001522 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001523}
1524
1525template <typename Case>
1526void HandleTransactionLockedTest::processesHotplugDisconnectCommon() {
1527 // --------------------------------------------------------------------
1528 // Preconditions
1529
1530 setupCommonPreconditions<Case>();
1531
1532 // A hotplug disconnect event is enqueued for a display
1533 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1534
1535 // The display is already completely set up.
1536 Case::Display::injectHwcDisplay(this);
1537 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1538 existing.inject();
1539
1540 // --------------------------------------------------------------------
1541 // Call Expectations
1542
1543 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
Lloyd Pique438e9e72018-09-04 18:06:08 -07001544 EXPECT_CALL(*mComposer, getDisplayIdentificationData(Case::Display::HWC_DISPLAY_ID, _, _))
1545 .Times(0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001546
1547 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1548
1549 // --------------------------------------------------------------------
1550 // Invocation
1551
1552 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1553
1554 // --------------------------------------------------------------------
1555 // Postconditions
1556
1557 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001558 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001559
Dominik Laskowski075d3172018-05-24 15:50:06 -07001560 // SF should not have a display token.
1561 const auto displayId = Case::Display::DISPLAY_ID::get();
1562 ASSERT_TRUE(displayId);
1563 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001564
1565 // The existing token should have been removed
1566 verifyDisplayIsNotConnected(existing.token());
1567}
1568
1569TEST_F(HandleTransactionLockedTest, processesHotplugConnectPrimaryDisplay) {
1570 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1571}
1572
1573TEST_F(HandleTransactionLockedTest,
1574 processesHotplugConnectPrimaryDisplayWithExternalAlreadyConnected) {
1575 // Inject an external display.
1576 ExternalDisplayVariant::injectHwcDisplay(this);
1577
1578 processesHotplugConnectCommon<SimplePrimaryDisplayCase>();
1579}
1580
1581TEST_F(HandleTransactionLockedTest, processesHotplugConnectExternalDisplay) {
1582 // Inject a primary display.
1583 PrimaryDisplayVariant::injectHwcDisplay(this);
1584
1585 processesHotplugConnectCommon<SimpleExternalDisplayCase>();
1586}
1587
1588TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfPrimaryAndExternalAlreadyConnected) {
1589 // Inject both a primary and external display.
1590 PrimaryDisplayVariant::injectHwcDisplay(this);
1591 ExternalDisplayVariant::injectHwcDisplay(this);
1592
1593 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1594
1595 ignoresHotplugConnectCommon<SimpleTertiaryDisplayCase>();
1596}
1597
1598TEST_F(HandleTransactionLockedTest, ignoresHotplugConnectIfExternalForVrComposer) {
1599 // Inject a primary display.
1600 PrimaryDisplayVariant::injectHwcDisplay(this);
1601
1602 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(true));
1603
1604 ignoresHotplugConnectCommon<SimpleExternalDisplayCase>();
1605}
1606
1607TEST_F(HandleTransactionLockedTest, processHotplugDisconnectPrimaryDisplay) {
1608 processesHotplugDisconnectCommon<SimplePrimaryDisplayCase>();
1609}
1610
1611TEST_F(HandleTransactionLockedTest, processHotplugDisconnectExternalDisplay) {
1612 processesHotplugDisconnectCommon<SimpleExternalDisplayCase>();
1613}
1614
1615TEST_F(HandleTransactionLockedTest, processesHotplugConnectThenDisconnectPrimary) {
1616 using Case = SimplePrimaryDisplayCase;
1617
1618 // --------------------------------------------------------------------
1619 // Preconditions
1620
1621 setupCommonPreconditions<Case>();
1622
1623 // A hotplug connect event is enqueued for a display
1624 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1625 // A hotplug disconnect event is also enqueued for the same display
1626 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1627
1628 // --------------------------------------------------------------------
1629 // Call Expectations
1630
1631 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1632
1633 setupCommonCallExpectationsForConnectProcessing<Case>();
1634 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1635
1636 EXPECT_CALL(*mComposer,
1637 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1638 .WillOnce(Return(Error::NONE));
1639 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1640
1641 // --------------------------------------------------------------------
1642 // Invocation
1643
1644 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1645
1646 // --------------------------------------------------------------------
1647 // Postconditions
1648
1649 // HWComposer should not have an entry for the display
Dominik Laskowski075d3172018-05-24 15:50:06 -07001650 EXPECT_FALSE(hasPhysicalHwcDisplay(Case::Display::HWC_DISPLAY_ID));
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001651
Dominik Laskowski075d3172018-05-24 15:50:06 -07001652 // SF should not have a display token.
1653 const auto displayId = Case::Display::DISPLAY_ID::get();
1654 ASSERT_TRUE(displayId);
1655 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 0);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001656}
1657
1658TEST_F(HandleTransactionLockedTest, processesHotplugDisconnectThenConnectPrimary) {
1659 using Case = SimplePrimaryDisplayCase;
1660
1661 // --------------------------------------------------------------------
1662 // Preconditions
1663
1664 setupCommonPreconditions<Case>();
1665
1666 // The display is already completely set up.
1667 Case::Display::injectHwcDisplay(this);
1668 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1669 existing.inject();
1670
1671 // A hotplug disconnect event is enqueued for a display
1672 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Disconnected);
1673 // A hotplug connect event is also enqueued for the same display
1674 Case::Display::injectPendingHotplugEvent(this, HWC2::Connection::Connected);
1675
1676 // --------------------------------------------------------------------
1677 // Call Expectations
1678
1679 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1680
1681 setupCommonCallExpectationsForConnectProcessing<Case>();
1682 setupCommonCallExpectationsForDisconnectProcessing<Case>();
1683
1684 // --------------------------------------------------------------------
1685 // Invocation
1686
1687 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1688
1689 // --------------------------------------------------------------------
1690 // Postconditions
1691
1692 // The existing token should have been removed
1693 verifyDisplayIsNotConnected(existing.token());
Dominik Laskowski075d3172018-05-24 15:50:06 -07001694 const auto displayId = Case::Display::DISPLAY_ID::get();
1695 ASSERT_TRUE(displayId);
1696 ASSERT_TRUE(mFlinger.mutablePhysicalDisplayTokens().count(*displayId) == 1);
1697 EXPECT_NE(existing.token(), mFlinger.mutablePhysicalDisplayTokens()[*displayId]);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001698
1699 // A new display should be connected in its place
1700
1701 verifyPhysicalDisplayIsConnected<Case>();
1702
1703 // --------------------------------------------------------------------
1704 // Cleanup conditions
1705
1706 EXPECT_CALL(*mComposer,
1707 setVsyncEnabled(Case::Display::HWC_DISPLAY_ID, IComposerClient::Vsync::DISABLE))
1708 .WillOnce(Return(Error::NONE));
1709 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1710}
1711
1712TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAdded) {
1713 using Case = HwcVirtualDisplayCase;
1714
1715 // --------------------------------------------------------------------
1716 // Preconditions
1717
1718 // The HWC supports at least one virtual display
1719 injectMockComposer(1);
1720
1721 setupCommonPreconditions<Case>();
1722
1723 // A virtual display was added to the current state, and it has a
1724 // surface(producer)
1725 sp<BBinder> displayToken = new BBinder();
Lloyd Pique4c2ac022018-04-27 12:08:03 -07001726
Dominik Laskowski075d3172018-05-24 15:50:06 -07001727 DisplayDeviceState state;
1728 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001729
1730 sp<mock::GraphicBufferProducer> surface{new mock::GraphicBufferProducer()};
Dominik Laskowski075d3172018-05-24 15:50:06 -07001731 state.surface = surface;
1732 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001733
1734 // --------------------------------------------------------------------
1735 // Call Expectations
1736
1737 Case::Display::setupFramebufferConsumerBufferQueueCallExpectations(this);
1738 Case::Display::setupNativeWindowSurfaceCreationCallExpectations(this);
1739
1740 EXPECT_CALL(*surface, query(NATIVE_WINDOW_WIDTH, _))
1741 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::WIDTH), Return(NO_ERROR)));
1742 EXPECT_CALL(*surface, query(NATIVE_WINDOW_HEIGHT, _))
1743 .WillRepeatedly(DoAll(SetArgPointee<1>(Case::Display::HEIGHT), Return(NO_ERROR)));
1744 EXPECT_CALL(*surface, query(NATIVE_WINDOW_FORMAT, _))
1745 .WillRepeatedly(DoAll(SetArgPointee<1>(DEFAULT_VIRTUAL_DISPLAY_SURFACE_FORMAT),
1746 Return(NO_ERROR)));
1747 EXPECT_CALL(*surface, query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, _))
1748 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(NO_ERROR)));
1749
1750 EXPECT_CALL(*surface, setAsyncMode(true)).Times(1);
1751
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001752 EXPECT_CALL(*mProducer, disconnect(_, _)).Times(1);
1753
1754 Case::Display::setupHwcVirtualDisplayCreationCallExpectations(this);
1755 Case::WideColorSupport::setupComposerCallExpectations(this);
1756 Case::HdrSupport::setupComposerCallExpectations(this);
Lloyd Piqued883d5a2018-04-27 19:32:30 -07001757 Case::PerFrameMetadataSupport::setupComposerCallExpectations(this);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001758
1759 // --------------------------------------------------------------------
1760 // Invocation
1761
1762 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1763
1764 // --------------------------------------------------------------------
1765 // Postconditions
1766
1767 // The display device should have been set up in the list of displays.
1768 verifyDisplayIsConnected<Case>(displayToken);
1769
1770 // --------------------------------------------------------------------
1771 // Cleanup conditions
1772
1773 EXPECT_CALL(*mComposer, destroyVirtualDisplay(Case::Display::HWC_DISPLAY_ID))
1774 .WillOnce(Return(Error::NONE));
1775 EXPECT_CALL(*mConsumer, consumerDisconnect()).WillOnce(Return(NO_ERROR));
1776}
1777
1778TEST_F(HandleTransactionLockedTest, processesVirtualDisplayAddedWithNoSurface) {
1779 using Case = HwcVirtualDisplayCase;
1780
1781 // --------------------------------------------------------------------
1782 // Preconditions
1783
1784 // The HWC supports at least one virtual display
1785 injectMockComposer(1);
1786
1787 setupCommonPreconditions<Case>();
1788
1789 // A virtual display was added to the current state, but it does not have a
1790 // surface.
1791 sp<BBinder> displayToken = new BBinder();
1792
Dominik Laskowski075d3172018-05-24 15:50:06 -07001793 DisplayDeviceState state;
1794 state.isSecure = static_cast<bool>(Case::Display::SECURE);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001795
Dominik Laskowski075d3172018-05-24 15:50:06 -07001796 mFlinger.mutableCurrentState().displays.add(displayToken, state);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001797
1798 // --------------------------------------------------------------------
1799 // Call Expectations
1800
1801 // --------------------------------------------------------------------
1802 // Invocation
1803
1804 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1805
1806 // --------------------------------------------------------------------
1807 // Postconditions
1808
1809 // There will not be a display device set up.
1810 EXPECT_FALSE(hasDisplayDevice(displayToken));
1811
1812 // The drawing display state will be set from the current display state.
1813 ASSERT_TRUE(hasDrawingDisplayState(displayToken));
1814 const auto& draw = getDrawingDisplayState(displayToken);
Dominik Laskowski075d3172018-05-24 15:50:06 -07001815 EXPECT_EQ(static_cast<bool>(Case::Display::VIRTUAL), draw.isVirtual());
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001816}
1817
1818TEST_F(HandleTransactionLockedTest, processesVirtualDisplayRemoval) {
1819 using Case = HwcVirtualDisplayCase;
1820
1821 // --------------------------------------------------------------------
1822 // Preconditions
1823
1824 // A virtual display is set up but is removed from the current state.
Dominik Laskowski075d3172018-05-24 15:50:06 -07001825 const auto displayId = Case::Display::DISPLAY_ID::get();
1826 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08001827 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Lloyd Piquec11e0d32018-01-22 18:44:59 -08001828 Case::Display::injectHwcDisplay(this);
1829 auto existing = Case::Display::makeFakeExistingDisplayInjector(this);
1830 existing.inject();
1831 mFlinger.mutableCurrentState().displays.removeItem(existing.token());
1832
1833 // --------------------------------------------------------------------
1834 // Call Expectations
1835
1836 EXPECT_CALL(*mComposer, isUsingVrComposer()).WillRepeatedly(Return(false));
1837
1838 // --------------------------------------------------------------------
1839 // Invocation
1840
1841 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1842
1843 // --------------------------------------------------------------------
1844 // Postconditions
1845
1846 // The existing token should have been removed
1847 verifyDisplayIsNotConnected(existing.token());
1848}
1849
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001850TEST_F(HandleTransactionLockedTest, processesDisplayLayerStackChanges) {
1851 using Case = NonHwcVirtualDisplayCase;
1852
1853 constexpr uint32_t oldLayerStack = 0u;
1854 constexpr uint32_t newLayerStack = 123u;
1855
1856 // --------------------------------------------------------------------
1857 // Preconditions
1858
1859 // A display is set up
1860 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1861 display.inject();
1862
1863 // There is a change to the layerStack state
1864 display.mutableDrawingDisplayState().layerStack = oldLayerStack;
1865 display.mutableCurrentDisplayState().layerStack = newLayerStack;
1866
1867 // --------------------------------------------------------------------
1868 // Invocation
1869
1870 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1871
1872 // --------------------------------------------------------------------
1873 // Postconditions
1874
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001875 EXPECT_EQ(newLayerStack, display.mutableDisplayDevice()->getLayerStack());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001876}
1877
1878TEST_F(HandleTransactionLockedTest, processesDisplayTransformChanges) {
1879 using Case = NonHwcVirtualDisplayCase;
1880
1881 constexpr int oldTransform = 0;
1882 constexpr int newTransform = 2;
1883
1884 // --------------------------------------------------------------------
1885 // Preconditions
1886
1887 // A display is set up
1888 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1889 display.inject();
1890
1891 // There is a change to the orientation state
1892 display.mutableDrawingDisplayState().orientation = oldTransform;
1893 display.mutableCurrentDisplayState().orientation = newTransform;
1894
1895 // --------------------------------------------------------------------
1896 // Invocation
1897
1898 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1899
1900 // --------------------------------------------------------------------
1901 // Postconditions
1902
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001903 EXPECT_EQ(newTransform, display.mutableDisplayDevice()->getOrientation());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001904}
1905
1906TEST_F(HandleTransactionLockedTest, processesDisplayViewportChanges) {
1907 using Case = NonHwcVirtualDisplayCase;
1908
1909 const Rect oldViewport(0, 0, 0, 0);
1910 const Rect newViewport(0, 0, 123, 456);
1911
1912 // --------------------------------------------------------------------
1913 // Preconditions
1914
1915 // A display is set up
1916 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1917 display.inject();
1918
1919 // There is a change to the viewport state
1920 display.mutableDrawingDisplayState().viewport = oldViewport;
1921 display.mutableCurrentDisplayState().viewport = newViewport;
1922
1923 // --------------------------------------------------------------------
1924 // Invocation
1925
1926 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1927
1928 // --------------------------------------------------------------------
1929 // Postconditions
1930
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001931 EXPECT_EQ(newViewport, display.mutableDisplayDevice()->getViewport());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001932}
1933
1934TEST_F(HandleTransactionLockedTest, processesDisplayFrameChanges) {
1935 using Case = NonHwcVirtualDisplayCase;
1936
1937 const Rect oldFrame(0, 0, 0, 0);
1938 const Rect newFrame(0, 0, 123, 456);
1939
1940 // --------------------------------------------------------------------
1941 // Preconditions
1942
1943 // A display is set up
1944 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1945 display.inject();
1946
1947 // There is a change to the viewport state
1948 display.mutableDrawingDisplayState().frame = oldFrame;
1949 display.mutableCurrentDisplayState().frame = newFrame;
1950
1951 // --------------------------------------------------------------------
1952 // Invocation
1953
1954 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
1955
1956 // --------------------------------------------------------------------
1957 // Postconditions
1958
Lloyd Pique9d9cf402018-02-16 17:47:13 -08001959 EXPECT_EQ(newFrame, display.mutableDisplayDevice()->getFrame());
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001960}
1961
1962TEST_F(HandleTransactionLockedTest, processesDisplayWidthChanges) {
1963 using Case = NonHwcVirtualDisplayCase;
1964
1965 constexpr int oldWidth = 0;
1966 constexpr int oldHeight = 10;
1967 constexpr int newWidth = 123;
1968
1969 // --------------------------------------------------------------------
1970 // Preconditions
1971
1972 // A display is set up
1973 auto nativeWindow = new mock::NativeWindow();
Lloyd Pique542307f2018-10-19 13:24:08 -07001974 auto displaySurface = new compositionengine::mock::DisplaySurface();
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001975 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001976 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
1977 display.setNativeWindow(nativeWindow);
1978 display.setDisplaySurface(displaySurface);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001979 // Setup injection expections
1980 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
1981 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
1982 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
1983 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
Alec Mouri4efb6c22018-11-16 13:07:47 -08001984 EXPECT_CALL(*nativeWindow, perform(9)).Times(1);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08001985 EXPECT_CALL(*nativeWindow, perform(13)).Times(1);
Alec Mouri4efb6c22018-11-16 13:07:47 -08001986 EXPECT_CALL(*nativeWindow, perform(30)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001987 display.inject();
1988
1989 // There is a change to the viewport state
1990 display.mutableDrawingDisplayState().width = oldWidth;
1991 display.mutableDrawingDisplayState().height = oldHeight;
1992 display.mutableCurrentDisplayState().width = newWidth;
1993 display.mutableCurrentDisplayState().height = oldHeight;
1994
1995 // --------------------------------------------------------------------
1996 // Call Expectations
1997
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001998 EXPECT_CALL(*displaySurface, resizeBuffers(newWidth, oldHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08001999
2000 // --------------------------------------------------------------------
2001 // Invocation
2002
2003 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2004}
2005
2006TEST_F(HandleTransactionLockedTest, processesDisplayHeightChanges) {
2007 using Case = NonHwcVirtualDisplayCase;
2008
2009 constexpr int oldWidth = 0;
2010 constexpr int oldHeight = 10;
2011 constexpr int newHeight = 123;
2012
2013 // --------------------------------------------------------------------
2014 // Preconditions
2015
2016 // A display is set up
2017 auto nativeWindow = new mock::NativeWindow();
Lloyd Pique542307f2018-10-19 13:24:08 -07002018 auto displaySurface = new compositionengine::mock::DisplaySurface();
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002019 sp<GraphicBuffer> buf = new GraphicBuffer();
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002020 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2021 display.setNativeWindow(nativeWindow);
2022 display.setDisplaySurface(displaySurface);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002023 // Setup injection expections
2024 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_WIDTH, _))
2025 .WillOnce(DoAll(SetArgPointee<1>(oldWidth), Return(0)));
2026 EXPECT_CALL(*nativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
2027 .WillOnce(DoAll(SetArgPointee<1>(oldHeight), Return(0)));
Alec Mouri4efb6c22018-11-16 13:07:47 -08002028 EXPECT_CALL(*nativeWindow, perform(9)).Times(1);
Alec Mouri0a9c7b82018-11-16 13:05:25 -08002029 EXPECT_CALL(*nativeWindow, perform(13)).Times(1);
Alec Mouri4efb6c22018-11-16 13:07:47 -08002030 EXPECT_CALL(*nativeWindow, perform(30)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002031 display.inject();
2032
2033 // There is a change to the viewport state
2034 display.mutableDrawingDisplayState().width = oldWidth;
2035 display.mutableDrawingDisplayState().height = oldHeight;
2036 display.mutableCurrentDisplayState().width = oldWidth;
2037 display.mutableCurrentDisplayState().height = newHeight;
2038
2039 // --------------------------------------------------------------------
2040 // Call Expectations
2041
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002042 EXPECT_CALL(*displaySurface, resizeBuffers(oldWidth, newHeight)).Times(1);
Lloyd Pique0fa1d4c2018-01-22 18:54:42 -08002043
2044 // --------------------------------------------------------------------
2045 // Invocation
2046
2047 mFlinger.handleTransactionLocked(eDisplayTransactionNeeded);
2048}
2049
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002050/* ------------------------------------------------------------------------
2051 * SurfaceFlinger::setDisplayStateLocked
2052 */
2053
2054TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWithUnknownDisplay) {
2055 // --------------------------------------------------------------------
2056 // Preconditions
2057
2058 // We have an unknown display token not associated with a known display
2059 sp<BBinder> displayToken = new BBinder();
2060
2061 // The requested display state references the unknown display.
2062 DisplayState state;
2063 state.what = DisplayState::eLayerStackChanged;
2064 state.token = displayToken;
2065 state.layerStack = 456;
2066
2067 // --------------------------------------------------------------------
2068 // Invocation
2069
2070 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2071
2072 // --------------------------------------------------------------------
2073 // Postconditions
2074
2075 // The returned flags are empty
2076 EXPECT_EQ(0u, flags);
2077
2078 // The display token still doesn't match anything known.
2079 EXPECT_FALSE(hasCurrentDisplayState(displayToken));
2080}
2081
Lloyd Pique9d9cf402018-02-16 17:47:13 -08002082TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingWhenNoChanges) {
2083 using Case = SimplePrimaryDisplayCase;
2084
2085 // --------------------------------------------------------------------
2086 // Preconditions
2087
2088 // A display is already set up
2089 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2090 display.inject();
2091
2092 // No changes are made to the display
2093 DisplayState state;
2094 state.what = 0;
2095 state.token = display.token();
2096
2097 // --------------------------------------------------------------------
2098 // Invocation
2099
2100 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2101
2102 // --------------------------------------------------------------------
2103 // Postconditions
2104
2105 // The returned flags are empty
2106 EXPECT_EQ(0u, flags);
2107}
2108
2109TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSurfaceDidNotChange) {
2110 using Case = SimplePrimaryDisplayCase;
2111
2112 // --------------------------------------------------------------------
2113 // Preconditions
2114
2115 // A display is already set up
2116 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2117 display.inject();
2118
2119 // There is a surface that can be set.
2120 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2121
2122 // The current display state has the surface set
2123 display.mutableCurrentDisplayState().surface = surface;
2124
2125 // The incoming request sets the same surface
2126 DisplayState state;
2127 state.what = DisplayState::eSurfaceChanged;
2128 state.token = display.token();
2129 state.surface = surface;
2130
2131 // --------------------------------------------------------------------
2132 // Invocation
2133
2134 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2135
2136 // --------------------------------------------------------------------
2137 // Postconditions
2138
2139 // The returned flags are empty
2140 EXPECT_EQ(0u, flags);
2141
2142 // The current display state is unchanged.
2143 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2144}
2145
2146TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfSurfaceChanged) {
2147 using Case = SimplePrimaryDisplayCase;
2148
2149 // --------------------------------------------------------------------
2150 // Preconditions
2151
2152 // A display is already set up
2153 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2154 display.inject();
2155
2156 // There is a surface that can be set.
2157 sp<mock::GraphicBufferProducer> surface = new mock::GraphicBufferProducer();
2158
2159 // The current display state does not have a surface
2160 display.mutableCurrentDisplayState().surface = nullptr;
2161
2162 // The incoming request sets a surface
2163 DisplayState state;
2164 state.what = DisplayState::eSurfaceChanged;
2165 state.token = display.token();
2166 state.surface = surface;
2167
2168 // --------------------------------------------------------------------
2169 // Invocation
2170
2171 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2172
2173 // --------------------------------------------------------------------
2174 // Postconditions
2175
2176 // The returned flags indicate a transaction is needed
2177 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2178
2179 // The current display layer stack state is set to the new value
2180 EXPECT_EQ(surface.get(), display.getCurrentDisplayState().surface.get());
2181}
2182
2183TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfLayerStackDidNotChange) {
2184 using Case = SimplePrimaryDisplayCase;
2185
2186 // --------------------------------------------------------------------
2187 // Preconditions
2188
2189 // A display is already set up
2190 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2191 display.inject();
2192
2193 // The display has a layer stack set
2194 display.mutableCurrentDisplayState().layerStack = 456u;
2195
2196 // The incoming request sets the same layer stack
2197 DisplayState state;
2198 state.what = DisplayState::eLayerStackChanged;
2199 state.token = display.token();
2200 state.layerStack = 456u;
2201
2202 // --------------------------------------------------------------------
2203 // Invocation
2204
2205 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2206
2207 // --------------------------------------------------------------------
2208 // Postconditions
2209
2210 // The returned flags are empty
2211 EXPECT_EQ(0u, flags);
2212
2213 // The current display state is unchanged
2214 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2215}
2216
2217TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfLayerStackChanged) {
2218 using Case = SimplePrimaryDisplayCase;
2219
2220 // --------------------------------------------------------------------
2221 // Preconditions
2222
2223 // A display is set up
2224 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2225 display.inject();
2226
2227 // The display has a layer stack set
2228 display.mutableCurrentDisplayState().layerStack = 654u;
2229
2230 // The incoming request sets a different layer stack
2231 DisplayState state;
2232 state.what = DisplayState::eLayerStackChanged;
2233 state.token = display.token();
2234 state.layerStack = 456u;
2235
2236 // --------------------------------------------------------------------
2237 // Invocation
2238
2239 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2240
2241 // --------------------------------------------------------------------
2242 // Postconditions
2243
2244 // The returned flags indicate a transaction is needed
2245 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2246
2247 // The desired display state has been set to the new value.
2248 EXPECT_EQ(456u, display.getCurrentDisplayState().layerStack);
2249}
2250
2251TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfProjectionDidNotChange) {
2252 using Case = SimplePrimaryDisplayCase;
2253 constexpr int initialOrientation = 180;
2254 const Rect initialFrame = {1, 2, 3, 4};
2255 const Rect initialViewport = {5, 6, 7, 8};
2256
2257 // --------------------------------------------------------------------
2258 // Preconditions
2259
2260 // A display is set up
2261 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2262 display.inject();
2263
2264 // The current display state projection state is all set
2265 display.mutableCurrentDisplayState().orientation = initialOrientation;
2266 display.mutableCurrentDisplayState().frame = initialFrame;
2267 display.mutableCurrentDisplayState().viewport = initialViewport;
2268
2269 // The incoming request sets the same projection state
2270 DisplayState state;
2271 state.what = DisplayState::eDisplayProjectionChanged;
2272 state.token = display.token();
2273 state.orientation = initialOrientation;
2274 state.frame = initialFrame;
2275 state.viewport = initialViewport;
2276
2277 // --------------------------------------------------------------------
2278 // Invocation
2279
2280 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2281
2282 // --------------------------------------------------------------------
2283 // Postconditions
2284
2285 // The returned flags are empty
2286 EXPECT_EQ(0u, flags);
2287
2288 // The current display state is unchanged
2289 EXPECT_EQ(initialOrientation, display.getCurrentDisplayState().orientation);
2290
2291 EXPECT_EQ(initialFrame, display.getCurrentDisplayState().frame);
2292 EXPECT_EQ(initialViewport, display.getCurrentDisplayState().viewport);
2293}
2294
2295TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfOrientationChanged) {
2296 using Case = SimplePrimaryDisplayCase;
2297 constexpr int initialOrientation = 90;
2298 constexpr int desiredOrientation = 180;
2299
2300 // --------------------------------------------------------------------
2301 // Preconditions
2302
2303 // A display is set up
2304 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2305 display.inject();
2306
2307 // The current display state has an orientation set
2308 display.mutableCurrentDisplayState().orientation = initialOrientation;
2309
2310 // The incoming request sets a different orientation
2311 DisplayState state;
2312 state.what = DisplayState::eDisplayProjectionChanged;
2313 state.token = display.token();
2314 state.orientation = desiredOrientation;
2315
2316 // --------------------------------------------------------------------
2317 // Invocation
2318
2319 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2320
2321 // --------------------------------------------------------------------
2322 // Postconditions
2323
2324 // The returned flags indicate a transaction is needed
2325 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2326
2327 // The current display state has the new value.
2328 EXPECT_EQ(desiredOrientation, display.getCurrentDisplayState().orientation);
2329}
2330
2331TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfFrameChanged) {
2332 using Case = SimplePrimaryDisplayCase;
2333 const Rect initialFrame = {0, 0, 0, 0};
2334 const Rect desiredFrame = {5, 6, 7, 8};
2335
2336 // --------------------------------------------------------------------
2337 // Preconditions
2338
2339 // A display is set up
2340 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2341 display.inject();
2342
2343 // The current display state does not have a frame
2344 display.mutableCurrentDisplayState().frame = initialFrame;
2345
2346 // The incoming request sets a frame
2347 DisplayState state;
2348 state.what = DisplayState::eDisplayProjectionChanged;
2349 state.token = display.token();
2350 state.frame = desiredFrame;
2351
2352 // --------------------------------------------------------------------
2353 // Invocation
2354
2355 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2356
2357 // --------------------------------------------------------------------
2358 // Postconditions
2359
2360 // The returned flags indicate a transaction is needed
2361 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2362
2363 // The current display state has the new value.
2364 EXPECT_EQ(desiredFrame, display.getCurrentDisplayState().frame);
2365}
2366
2367TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfViewportChanged) {
2368 using Case = SimplePrimaryDisplayCase;
2369 const Rect initialViewport = {0, 0, 0, 0};
2370 const Rect desiredViewport = {5, 6, 7, 8};
2371
2372 // --------------------------------------------------------------------
2373 // Preconditions
2374
2375 // A display is set up
2376 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2377 display.inject();
2378
2379 // The current display state does not have a viewport
2380 display.mutableCurrentDisplayState().viewport = initialViewport;
2381
2382 // The incoming request sets a viewport
2383 DisplayState state;
2384 state.what = DisplayState::eDisplayProjectionChanged;
2385 state.token = display.token();
2386 state.viewport = desiredViewport;
2387
2388 // --------------------------------------------------------------------
2389 // Invocation
2390
2391 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2392
2393 // --------------------------------------------------------------------
2394 // Postconditions
2395
2396 // The returned flags indicate a transaction is needed
2397 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2398
2399 // The current display state has the new value.
2400 EXPECT_EQ(desiredViewport, display.getCurrentDisplayState().viewport);
2401}
2402
2403TEST_F(DisplayTransactionTest, setDisplayStateLockedDoesNothingIfSizeDidNotChange) {
2404 using Case = SimplePrimaryDisplayCase;
2405 constexpr uint32_t initialWidth = 1024;
2406 constexpr uint32_t initialHeight = 768;
2407
2408 // --------------------------------------------------------------------
2409 // Preconditions
2410
2411 // A display is set up
2412 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2413 display.inject();
2414
2415 // The current display state has a size set
2416 display.mutableCurrentDisplayState().width = initialWidth;
2417 display.mutableCurrentDisplayState().height = initialHeight;
2418
2419 // The incoming request sets the same display size
2420 DisplayState state;
2421 state.what = DisplayState::eDisplaySizeChanged;
2422 state.token = display.token();
2423 state.width = initialWidth;
2424 state.height = initialHeight;
2425
2426 // --------------------------------------------------------------------
2427 // Invocation
2428
2429 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2430
2431 // --------------------------------------------------------------------
2432 // Postconditions
2433
2434 // The returned flags are empty
2435 EXPECT_EQ(0u, flags);
2436
2437 // The current display state is unchanged
2438 EXPECT_EQ(initialWidth, display.getCurrentDisplayState().width);
2439 EXPECT_EQ(initialHeight, display.getCurrentDisplayState().height);
2440}
2441
2442TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfWidthChanged) {
2443 using Case = SimplePrimaryDisplayCase;
2444 constexpr uint32_t initialWidth = 0;
2445 constexpr uint32_t desiredWidth = 1024;
2446
2447 // --------------------------------------------------------------------
2448 // Preconditions
2449
2450 // A display is set up
2451 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2452 display.inject();
2453
2454 // The display does not yet have a width
2455 display.mutableCurrentDisplayState().width = initialWidth;
2456
2457 // The incoming request sets a display width
2458 DisplayState state;
2459 state.what = DisplayState::eDisplaySizeChanged;
2460 state.token = display.token();
2461 state.width = desiredWidth;
2462
2463 // --------------------------------------------------------------------
2464 // Invocation
2465
2466 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2467
2468 // --------------------------------------------------------------------
2469 // Postconditions
2470
2471 // The returned flags indicate a transaction is needed
2472 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2473
2474 // The current display state has the new value.
2475 EXPECT_EQ(desiredWidth, display.getCurrentDisplayState().width);
2476}
2477
2478TEST_F(DisplayTransactionTest, setDisplayStateLockedRequestsUpdateIfHeightChanged) {
2479 using Case = SimplePrimaryDisplayCase;
2480 constexpr uint32_t initialHeight = 0;
2481 constexpr uint32_t desiredHeight = 768;
2482
2483 // --------------------------------------------------------------------
2484 // Preconditions
2485
2486 // A display is set up
2487 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2488 display.inject();
2489
2490 // The display does not yet have a height
2491 display.mutableCurrentDisplayState().height = initialHeight;
2492
2493 // The incoming request sets a display height
2494 DisplayState state;
2495 state.what = DisplayState::eDisplaySizeChanged;
2496 state.token = display.token();
2497 state.height = desiredHeight;
2498
2499 // --------------------------------------------------------------------
2500 // Invocation
2501
2502 uint32_t flags = mFlinger.setDisplayStateLocked(state);
2503
2504 // --------------------------------------------------------------------
2505 // Postconditions
2506
2507 // The returned flags indicate a transaction is needed
2508 EXPECT_EQ(eDisplayTransactionNeeded, flags);
2509
2510 // The current display state has the new value.
2511 EXPECT_EQ(desiredHeight, display.getCurrentDisplayState().height);
2512}
2513
Lloyd Pique86016da2018-03-01 16:09:38 -08002514/* ------------------------------------------------------------------------
2515 * SurfaceFlinger::onInitializeDisplays
2516 */
2517
2518TEST_F(DisplayTransactionTest, onInitializeDisplaysSetsUpPrimaryDisplay) {
2519 using Case = SimplePrimaryDisplayCase;
2520
2521 // --------------------------------------------------------------------
2522 // Preconditions
2523
2524 // A primary display is set up
2525 Case::Display::injectHwcDisplay(this);
2526 auto primaryDisplay = Case::Display::makeFakeExistingDisplayInjector(this);
2527 primaryDisplay.inject();
2528
2529 // --------------------------------------------------------------------
2530 // Call Expectations
2531
2532 // We expect the surface interceptor to possibly be used, but we treat it as
2533 // disabled since it is called as a side effect rather than directly by this
2534 // function.
2535 EXPECT_CALL(*mSurfaceInterceptor, isEnabled()).WillOnce(Return(false));
2536
2537 // We expect a call to get the active display config.
2538 Case::Display::setupHwcGetActiveConfigCallExpectations(this);
2539
2540 // We expect invalidate() to be invoked once to trigger display transaction
2541 // processing.
2542 EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
2543
2544 // --------------------------------------------------------------------
2545 // Invocation
2546
2547 mFlinger.onInitializeDisplays();
2548
2549 // --------------------------------------------------------------------
2550 // Postconditions
2551
2552 // The primary display should have a current state
2553 ASSERT_TRUE(hasCurrentDisplayState(primaryDisplay.token()));
2554 const auto& primaryDisplayState = getCurrentDisplayState(primaryDisplay.token());
2555 // The layer stack state should be set to zero
2556 EXPECT_EQ(0u, primaryDisplayState.layerStack);
2557 // The orientation state should be set to zero
2558 EXPECT_EQ(0, primaryDisplayState.orientation);
2559
2560 // The frame state should be set to INVALID
2561 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.frame);
2562
2563 // The viewport state should be set to INVALID
2564 EXPECT_EQ(Rect::INVALID_RECT, primaryDisplayState.viewport);
2565
2566 // The width and height should both be zero
2567 EXPECT_EQ(0u, primaryDisplayState.width);
2568 EXPECT_EQ(0u, primaryDisplayState.height);
2569
2570 // The display should be set to HWC_POWER_MODE_NORMAL
2571 ASSERT_TRUE(hasDisplayDevice(primaryDisplay.token()));
2572 auto displayDevice = primaryDisplay.mutableDisplayDevice();
2573 EXPECT_EQ(HWC_POWER_MODE_NORMAL, displayDevice->getPowerMode());
2574
2575 // The display refresh period should be set in the frame tracker.
2576 FrameStats stats;
2577 mFlinger.getAnimFrameTracker().getStats(&stats);
2578 EXPECT_EQ(DEFAULT_REFRESH_RATE, stats.refreshPeriodNano);
2579
2580 // The display transaction needed flag should be set.
2581 EXPECT_TRUE(hasTransactionFlagSet(eDisplayTransactionNeeded));
2582
2583 // The compositor timing should be set to default values
2584 const auto& compositorTiming = mFlinger.getCompositorTiming();
2585 EXPECT_EQ(-DEFAULT_REFRESH_RATE, compositorTiming.deadline);
2586 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.interval);
2587 EXPECT_EQ(DEFAULT_REFRESH_RATE, compositorTiming.presentLatency);
2588}
2589
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002590/* ------------------------------------------------------------------------
2591 * SurfaceFlinger::setPowerModeInternal
2592 */
2593
2594// Used when we simulate a display that supports doze.
Peiyong Lined531a32018-10-26 18:27:56 -07002595template <typename Display>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002596struct DozeIsSupportedVariant {
2597 static constexpr bool DOZE_SUPPORTED = true;
2598 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2599 IComposerClient::PowerMode::DOZE;
2600 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2601 IComposerClient::PowerMode::DOZE_SUSPEND;
Peiyong Lined531a32018-10-26 18:27:56 -07002602
2603 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
2604 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(Display::HWC_DISPLAY_ID, _))
2605 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hwc2::DisplayCapability>(
2606 {Hwc2::DisplayCapability::DOZE})),
2607 Return(Error::NONE)));
2608 }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002609};
2610
Peiyong Lined531a32018-10-26 18:27:56 -07002611template <typename Display>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002612// Used when we simulate a display that does not support doze.
2613struct DozeNotSupportedVariant {
2614 static constexpr bool DOZE_SUPPORTED = false;
2615 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE =
2616 IComposerClient::PowerMode::ON;
2617 static constexpr IComposerClient::PowerMode ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND =
2618 IComposerClient::PowerMode::ON;
Peiyong Lined531a32018-10-26 18:27:56 -07002619
2620 static void setupComposerCallExpectations(DisplayTransactionTest* test) {
2621 EXPECT_CALL(*test->mComposer, getDisplayCapabilities(Display::HWC_DISPLAY_ID, _))
2622 .WillOnce(DoAll(SetArgPointee<1>(std::vector<Hwc2::DisplayCapability>({})),
2623 Return(Error::NONE)));
2624 }
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002625};
2626
2627struct EventThreadBaseSupportedVariant {
2628 static void setupEventAndEventControlThreadNoCallExpectations(DisplayTransactionTest* test) {
2629 // The event control thread should not be notified.
2630 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(_)).Times(0);
2631
2632 // The event thread should not be notified.
2633 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(0);
2634 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(0);
2635 }
2636};
2637
2638struct EventThreadNotSupportedVariant : public EventThreadBaseSupportedVariant {
2639 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2640 // These calls are only expected for the primary display.
2641
2642 // Instead expect no calls.
2643 setupEventAndEventControlThreadNoCallExpectations(test);
2644 }
2645
2646 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2647 // These calls are only expected for the primary display.
2648
2649 // Instead expect no calls.
2650 setupEventAndEventControlThreadNoCallExpectations(test);
2651 }
2652};
2653
2654struct EventThreadIsSupportedVariant : public EventThreadBaseSupportedVariant {
2655 static void setupAcquireAndEnableVsyncCallExpectations(DisplayTransactionTest* test) {
2656 // The event control thread should be notified to enable vsyncs
2657 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(true)).Times(1);
2658
2659 // The event thread should be notified that the screen was acquired.
2660 EXPECT_CALL(*test->mEventThread, onScreenAcquired()).Times(1);
2661 }
2662
2663 static void setupReleaseAndDisableVsyncCallExpectations(DisplayTransactionTest* test) {
2664 // There should be a call to setVsyncEnabled(false)
2665 EXPECT_CALL(*test->mEventControlThread, setVsyncEnabled(false)).Times(1);
2666
2667 // The event thread should not be notified that the screen was released.
2668 EXPECT_CALL(*test->mEventThread, onScreenReleased()).Times(1);
2669 }
2670};
2671
Lloyd Pique41be5d22018-06-21 13:11:48 -07002672struct DispSyncIsSupportedVariant {
2673 static void setupBeginResyncCallExpectations(DisplayTransactionTest* test) {
2674 EXPECT_CALL(*test->mPrimaryDispSync, reset()).Times(1);
2675 EXPECT_CALL(*test->mPrimaryDispSync, setPeriod(DEFAULT_REFRESH_RATE)).Times(1);
2676 EXPECT_CALL(*test->mPrimaryDispSync, beginResync()).Times(1);
2677 }
2678
2679 static void setupEndResyncCallExpectations(DisplayTransactionTest* test) {
2680 EXPECT_CALL(*test->mPrimaryDispSync, endResync()).Times(1);
2681 }
2682};
2683
2684struct DispSyncNotSupportedVariant {
2685 static void setupBeginResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2686
2687 static void setupEndResyncCallExpectations(DisplayTransactionTest* /* test */) {}
2688};
2689
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002690// --------------------------------------------------------------------
2691// Note:
2692//
2693// There are a large number of transitions we could test, however we only test a
2694// selected subset which provides complete test coverage of the implementation.
2695// --------------------------------------------------------------------
2696
2697template <int initialPowerMode, int targetPowerMode>
2698struct TransitionVariantCommon {
2699 static constexpr auto INITIAL_POWER_MODE = initialPowerMode;
2700 static constexpr auto TARGET_POWER_MODE = targetPowerMode;
2701
2702 static void verifyPostconditions(DisplayTransactionTest*) {}
2703};
2704
2705struct TransitionOffToOnVariant
2706 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_NORMAL> {
2707 template <typename Case>
2708 static void setupCallExpectations(DisplayTransactionTest* test) {
2709 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2710 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002711 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002712 Case::setupRepaintEverythingCallExpectations(test);
2713 }
2714
2715 static void verifyPostconditions(DisplayTransactionTest* test) {
2716 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2717 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2718 }
2719};
2720
2721struct TransitionOffToDozeSuspendVariant
2722 : public TransitionVariantCommon<HWC_POWER_MODE_OFF, HWC_POWER_MODE_DOZE_SUSPEND> {
2723 template <typename Case>
2724 static void setupCallExpectations(DisplayTransactionTest* test) {
2725 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2726 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2727 Case::setupRepaintEverythingCallExpectations(test);
2728 }
2729
2730 static void verifyPostconditions(DisplayTransactionTest* test) {
2731 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2732 EXPECT_TRUE(test->mFlinger.getHasPoweredOff());
2733 }
2734};
2735
2736struct TransitionOnToOffVariant
2737 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_OFF> {
2738 template <typename Case>
2739 static void setupCallExpectations(DisplayTransactionTest* test) {
2740 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002741 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002742 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2743 }
2744
2745 static void verifyPostconditions(DisplayTransactionTest* test) {
2746 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2747 }
2748};
2749
2750struct TransitionDozeSuspendToOffVariant
2751 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_OFF> {
2752 template <typename Case>
2753 static void setupCallExpectations(DisplayTransactionTest* test) {
2754 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2755 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
2756 }
2757
2758 static void verifyPostconditions(DisplayTransactionTest* test) {
2759 EXPECT_TRUE(test->mFlinger.getVisibleRegionsDirty());
2760 }
2761};
2762
2763struct TransitionOnToDozeVariant
2764 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE> {
2765 template <typename Case>
2766 static void setupCallExpectations(DisplayTransactionTest* test) {
2767 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2768 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2769 }
2770};
2771
2772struct TransitionDozeSuspendToDozeVariant
2773 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_DOZE> {
2774 template <typename Case>
2775 static void setupCallExpectations(DisplayTransactionTest* test) {
2776 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002777 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002778 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
2779 }
2780};
2781
2782struct TransitionDozeToOnVariant
2783 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE, HWC_POWER_MODE_NORMAL> {
2784 template <typename Case>
2785 static void setupCallExpectations(DisplayTransactionTest* test) {
2786 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2787 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2788 }
2789};
2790
2791struct TransitionDozeSuspendToOnVariant
2792 : public TransitionVariantCommon<HWC_POWER_MODE_DOZE_SUSPEND, HWC_POWER_MODE_NORMAL> {
2793 template <typename Case>
2794 static void setupCallExpectations(DisplayTransactionTest* test) {
2795 Case::EventThread::setupAcquireAndEnableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002796 Case::DispSync::setupBeginResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002797 Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
2798 }
2799};
2800
2801struct TransitionOnToDozeSuspendVariant
2802 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_DOZE_SUSPEND> {
2803 template <typename Case>
2804 static void setupCallExpectations(DisplayTransactionTest* test) {
2805 Case::EventThread::setupReleaseAndDisableVsyncCallExpectations(test);
Lloyd Pique41be5d22018-06-21 13:11:48 -07002806 Case::DispSync::setupEndResyncCallExpectations(test);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002807 Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
2808 }
2809};
2810
2811struct TransitionOnToUnknownVariant
2812 : public TransitionVariantCommon<HWC_POWER_MODE_NORMAL, HWC_POWER_MODE_LEET> {
2813 template <typename Case>
2814 static void setupCallExpectations(DisplayTransactionTest* test) {
2815 Case::EventThread::setupEventAndEventControlThreadNoCallExpectations(test);
2816 Case::setupNoComposerPowerModeCallExpectations(test);
2817 }
2818};
2819
2820// --------------------------------------------------------------------
2821// Note:
2822//
2823// Rather than testing the cartesian product of of
2824// DozeIsSupported/DozeNotSupported with all other options, we use one for one
2825// display type, and the other for another display type.
2826// --------------------------------------------------------------------
2827
2828template <typename DisplayVariant, typename DozeVariant, typename EventThreadVariant,
Lloyd Pique41be5d22018-06-21 13:11:48 -07002829 typename DispSyncVariant, typename TransitionVariant>
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002830struct DisplayPowerCase {
2831 using Display = DisplayVariant;
2832 using Doze = DozeVariant;
2833 using EventThread = EventThreadVariant;
Lloyd Pique41be5d22018-06-21 13:11:48 -07002834 using DispSync = DispSyncVariant;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002835 using Transition = TransitionVariant;
2836
2837 static auto injectDisplayWithInitialPowerMode(DisplayTransactionTest* test, int mode) {
2838 Display::injectHwcDisplay(test);
2839 auto display = Display::makeFakeExistingDisplayInjector(test);
2840 display.inject();
2841 display.mutableDisplayDevice()->setPowerMode(mode);
2842 return display;
2843 }
2844
2845 static void setInitialPrimaryHWVsyncEnabled(DisplayTransactionTest* test, bool enabled) {
2846 test->mFlinger.mutablePrimaryHWVsyncEnabled() = enabled;
2847 }
2848
2849 static void setupRepaintEverythingCallExpectations(DisplayTransactionTest* test) {
2850 EXPECT_CALL(*test->mMessageQueue, invalidate()).Times(1);
2851 }
2852
2853 static void setupSurfaceInterceptorCallExpectations(DisplayTransactionTest* test, int mode) {
2854 EXPECT_CALL(*test->mSurfaceInterceptor, isEnabled()).WillOnce(Return(true));
2855 EXPECT_CALL(*test->mSurfaceInterceptor, savePowerModeUpdate(_, mode)).Times(1);
2856 }
2857
2858 static void setupComposerCallExpectations(DisplayTransactionTest* test,
2859 IComposerClient::PowerMode mode) {
2860 // Any calls to get the active config will return a default value.
2861 EXPECT_CALL(*test->mComposer, getActiveConfig(Display::HWC_DISPLAY_ID, _))
2862 .WillRepeatedly(DoAll(SetArgPointee<1>(Display::HWC_ACTIVE_CONFIG_ID),
2863 Return(Error::NONE)));
2864
2865 // Any calls to get whether the display supports dozing will return the value set by the
2866 // policy variant.
2867 EXPECT_CALL(*test->mComposer, getDozeSupport(Display::HWC_DISPLAY_ID, _))
2868 .WillRepeatedly(DoAll(SetArgPointee<1>(Doze::DOZE_SUPPORTED), Return(Error::NONE)));
2869
2870 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, mode)).Times(1);
2871 }
2872
2873 static void setupNoComposerPowerModeCallExpectations(DisplayTransactionTest* test) {
2874 EXPECT_CALL(*test->mComposer, setPowerMode(Display::HWC_DISPLAY_ID, _)).Times(0);
2875 }
2876};
2877
2878// A sample configuration for the primary display.
2879// In addition to having event thread support, we emulate doze support.
2880template <typename TransitionVariant>
Peiyong Lined531a32018-10-26 18:27:56 -07002881using PrimaryDisplayPowerCase =
2882 DisplayPowerCase<PrimaryDisplayVariant, DozeIsSupportedVariant<PrimaryDisplayVariant>,
2883 EventThreadIsSupportedVariant, DispSyncIsSupportedVariant,
2884 TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002885
2886// A sample configuration for the external display.
2887// In addition to not having event thread support, we emulate not having doze
2888// support.
2889template <typename TransitionVariant>
Peiyong Lined531a32018-10-26 18:27:56 -07002890using ExternalDisplayPowerCase =
2891 DisplayPowerCase<ExternalDisplayVariant, DozeNotSupportedVariant<ExternalDisplayVariant>,
2892 EventThreadNotSupportedVariant, DispSyncNotSupportedVariant,
2893 TransitionVariant>;
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002894
2895class SetPowerModeInternalTest : public DisplayTransactionTest {
2896public:
2897 template <typename Case>
2898 void transitionDisplayCommon();
2899};
2900
2901template <int PowerMode>
2902struct PowerModeInitialVSyncEnabled : public std::false_type {};
2903
2904template <>
2905struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_NORMAL> : public std::true_type {};
2906
2907template <>
2908struct PowerModeInitialVSyncEnabled<HWC_POWER_MODE_DOZE> : public std::true_type {};
2909
2910template <typename Case>
2911void SetPowerModeInternalTest::transitionDisplayCommon() {
2912 // --------------------------------------------------------------------
2913 // Preconditions
2914
Peiyong Lined531a32018-10-26 18:27:56 -07002915 Case::Doze::setupComposerCallExpectations(this);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002916 auto display =
2917 Case::injectDisplayWithInitialPowerMode(this, Case::Transition::INITIAL_POWER_MODE);
2918 Case::setInitialPrimaryHWVsyncEnabled(this,
2919 PowerModeInitialVSyncEnabled<
2920 Case::Transition::INITIAL_POWER_MODE>::value);
2921
2922 // --------------------------------------------------------------------
2923 // Call Expectations
2924
2925 Case::setupSurfaceInterceptorCallExpectations(this, Case::Transition::TARGET_POWER_MODE);
2926 Case::Transition::template setupCallExpectations<Case>(this);
2927
2928 // --------------------------------------------------------------------
2929 // Invocation
2930
2931 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(),
2932 Case::Transition::TARGET_POWER_MODE);
2933
2934 // --------------------------------------------------------------------
2935 // Postconditions
2936
2937 Case::Transition::verifyPostconditions(this);
2938}
2939
2940TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfNoChange) {
2941 using Case = SimplePrimaryDisplayCase;
2942
2943 // --------------------------------------------------------------------
2944 // Preconditions
2945
2946 // A primary display device is set up
2947 Case::Display::injectHwcDisplay(this);
2948 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2949 display.inject();
2950
Dominik Laskowskia2edf612018-06-01 13:15:16 -07002951 // The display is already set to HWC_POWER_MODE_NORMAL
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002952 display.mutableDisplayDevice()->setPowerMode(HWC_POWER_MODE_NORMAL);
2953
2954 // --------------------------------------------------------------------
2955 // Invocation
2956
2957 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_NORMAL);
2958
2959 // --------------------------------------------------------------------
2960 // Postconditions
2961
2962 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2963}
2964
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002965TEST_F(SetPowerModeInternalTest, setPowerModeInternalDoesNothingIfVirtualDisplay) {
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002966 using Case = HwcVirtualDisplayCase;
2967
2968 // --------------------------------------------------------------------
2969 // Preconditions
2970
Dominik Laskowski075d3172018-05-24 15:50:06 -07002971 // Insert display data so that the HWC thinks it created the virtual display.
2972 const auto displayId = Case::Display::DISPLAY_ID::get();
2973 ASSERT_TRUE(displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -08002974 mFlinger.mutableHwcDisplayData().try_emplace(*displayId);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002975
2976 // A virtual display device is set up
2977 Case::Display::injectHwcDisplay(this);
2978 auto display = Case::Display::makeFakeExistingDisplayInjector(this);
2979 display.inject();
2980
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002981 // The display is set to HWC_POWER_MODE_NORMAL
2982 getDisplayDevice(display.token())->setPowerMode(HWC_POWER_MODE_NORMAL);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002983
2984 // --------------------------------------------------------------------
2985 // Invocation
2986
Dominik Laskowskieecd6592018-05-29 10:25:41 -07002987 mFlinger.setPowerModeInternal(display.mutableDisplayDevice(), HWC_POWER_MODE_OFF);
Lloyd Pique7d4aa6c2018-03-01 16:36:35 -08002988
2989 // --------------------------------------------------------------------
2990 // Postconditions
2991
2992 EXPECT_EQ(HWC_POWER_MODE_NORMAL, display.mutableDisplayDevice()->getPowerMode());
2993}
2994
2995TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnPrimaryDisplay) {
2996 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToOnVariant>>();
2997}
2998
2999TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendPrimaryDisplay) {
3000 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
3001}
3002
3003TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffPrimaryDisplay) {
3004 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToOffVariant>>();
3005}
3006
3007TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffPrimaryDisplay) {
3008 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
3009}
3010
3011TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozePrimaryDisplay) {
3012 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeVariant>>();
3013}
3014
3015TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozePrimaryDisplay) {
3016 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
3017}
3018
3019TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnPrimaryDisplay) {
3020 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeToOnVariant>>();
3021}
3022
3023TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnPrimaryDisplay) {
3024 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
3025}
3026
3027TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendPrimaryDisplay) {
3028 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
3029}
3030
3031TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownPrimaryDisplay) {
3032 transitionDisplayCommon<PrimaryDisplayPowerCase<TransitionOnToUnknownVariant>>();
3033}
3034
3035TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToOnExternalDisplay) {
3036 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToOnVariant>>();
3037}
3038
3039TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOffToDozeSuspendExternalDisplay) {
3040 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOffToDozeSuspendVariant>>();
3041}
3042
3043TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToOffExternalDisplay) {
3044 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToOffVariant>>();
3045}
3046
3047TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOffExternalDisplay) {
3048 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOffVariant>>();
3049}
3050
3051TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeExternalDisplay) {
3052 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeVariant>>();
3053}
3054
3055TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToDozeExternalDisplay) {
3056 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToDozeVariant>>();
3057}
3058
3059TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeToOnExternalDisplay) {
3060 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeToOnVariant>>();
3061}
3062
3063TEST_F(SetPowerModeInternalTest, transitionsDisplayFromDozeSuspendToOnExternalDisplay) {
3064 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionDozeSuspendToOnVariant>>();
3065}
3066
3067TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToDozeSuspendExternalDisplay) {
3068 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToDozeSuspendVariant>>();
3069}
3070
3071TEST_F(SetPowerModeInternalTest, transitionsDisplayFromOnToUnknownExternalDisplay) {
3072 transitionDisplayCommon<ExternalDisplayPowerCase<TransitionOnToUnknownVariant>>();
3073}
3074
Lloyd Piquef58625d2017-12-19 13:22:33 -08003075} // namespace
3076} // namespace android