blob: cd0323555c9eab41a0426babcfd74916f5ca495e [file] [log] [blame]
Lloyd Pique45a165a2018-10-19 11:54:47 -07001/*
2 * Copyright 2019 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
Lloyd Pique45a165a2018-10-19 11:54:47 -070017#include <cmath>
18
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070019#include <compositionengine/DisplayColorProfileCreationArgs.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070020#include <compositionengine/DisplayCreationArgs.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070021#include <compositionengine/DisplaySurface.h>
22#include <compositionengine/RenderSurfaceCreationArgs.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070023#include <compositionengine/impl/Display.h>
Lloyd Piquec6607552019-12-02 17:57:39 -080024#include <compositionengine/impl/RenderSurface.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070025#include <compositionengine/mock/CompositionEngine.h>
Lloyd Piquef5275482019-01-29 18:42:42 -080026#include <compositionengine/mock/DisplayColorProfile.h>
Lloyd Piquec6607552019-12-02 17:57:39 -080027#include <compositionengine/mock/DisplaySurface.h>
Lloyd Piquedf336d92019-03-07 21:38:42 -080028#include <compositionengine/mock/LayerFE.h>
chaviw8beb4142019-04-11 13:09:05 -070029#include <compositionengine/mock/NativeWindow.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080030#include <compositionengine/mock/OutputLayer.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070031#include <compositionengine/mock/RenderSurface.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070032#include <gtest/gtest.h>
Lloyd Piquee9eff972020-05-05 12:36:44 -070033#include <renderengine/mock/RenderEngine.h>
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070034#include <ui/Rect.h>
Marin Shalamanov228f46b2021-01-28 21:11:45 +010035#include <ui/StaticDisplayInfo.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070036
Lloyd Pique66d68602019-02-13 14:23:31 -080037#include "MockHWC2.h"
Lloyd Pique45a165a2018-10-19 11:54:47 -070038#include "MockHWComposer.h"
Lloyd Pique688abd42019-02-15 15:42:24 -080039#include "MockPowerAdvisor.h"
Alec Mouricdf16792021-12-10 13:16:06 -080040#include "ftl/future.h"
Lloyd Pique45a165a2018-10-19 11:54:47 -070041
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050042#include <aidl/android/hardware/graphics/composer3/Composition.h>
43
Ady Abrahamde549d42022-01-26 19:19:17 -080044using aidl::android::hardware::graphics::composer3::Capability;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050045using aidl::android::hardware::graphics::composer3::Composition;
46
Lloyd Pique45a165a2018-10-19 11:54:47 -070047namespace android::compositionengine {
48namespace {
49
Peiyong Line9d809e2020-04-14 13:10:48 -070050namespace hal = android::hardware::graphics::composer::hal;
51
Lloyd Piquef5275482019-01-29 18:42:42 -080052using testing::_;
Alec Mouricdf16792021-12-10 13:16:06 -080053using testing::ByMove;
Lloyd Pique66d68602019-02-13 14:23:31 -080054using testing::DoAll;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070055using testing::Eq;
Lloyd Piquec6607552019-12-02 17:57:39 -080056using testing::InSequence;
57using testing::NiceMock;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070058using testing::Pointee;
59using testing::Ref;
Lloyd Pique31cb2942018-10-19 17:23:03 -070060using testing::Return;
Lloyd Pique45a165a2018-10-19 11:54:47 -070061using testing::ReturnRef;
Lloyd Pique66d68602019-02-13 14:23:31 -080062using testing::Sequence;
63using testing::SetArgPointee;
Lloyd Pique45a165a2018-10-19 11:54:47 -070064using testing::StrictMock;
65
Dominik Laskowski3dce4f42021-03-08 20:48:28 -080066constexpr PhysicalDisplayId DEFAULT_DISPLAY_ID = PhysicalDisplayId::fromPort(123u);
67constexpr HalVirtualDisplayId HAL_VIRTUAL_DISPLAY_ID{456u};
68constexpr GpuVirtualDisplayId GPU_VIRTUAL_DISPLAY_ID{789u};
Dominik Laskowskif1833852021-03-23 15:06:50 -070069
Dominik Laskowski3dce4f42021-03-08 20:48:28 -080070constexpr ui::Size DEFAULT_RESOLUTION{1920, 1080};
Lloyd Pique45a165a2018-10-19 11:54:47 -070071
Lloyd Piquede196652020-01-22 17:29:58 -080072struct Layer {
73 Layer() {
74 EXPECT_CALL(*outputLayer, getLayerFE()).WillRepeatedly(ReturnRef(*layerFE));
75 EXPECT_CALL(*outputLayer, getHwcLayer()).WillRepeatedly(Return(&hwc2Layer));
76 }
77
Ady Abrahame0eafa82022-02-02 19:30:47 -080078 sp<StrictMock<mock::LayerFE>> layerFE = sp<StrictMock<mock::LayerFE>>::make();
Lloyd Piquede196652020-01-22 17:29:58 -080079 StrictMock<mock::OutputLayer>* outputLayer = new StrictMock<mock::OutputLayer>();
80 StrictMock<HWC2::mock::Layer> hwc2Layer;
81};
82
83struct LayerNoHWC2Layer {
84 LayerNoHWC2Layer() {
85 EXPECT_CALL(*outputLayer, getLayerFE()).WillRepeatedly(ReturnRef(*layerFE));
86 EXPECT_CALL(*outputLayer, getHwcLayer()).WillRepeatedly(Return(nullptr));
87 }
88
Ady Abrahame0eafa82022-02-02 19:30:47 -080089 sp<StrictMock<mock::LayerFE>> layerFE = sp<StrictMock<mock::LayerFE>>::make();
Lloyd Piquede196652020-01-22 17:29:58 -080090 StrictMock<mock::OutputLayer>* outputLayer = new StrictMock<mock::OutputLayer>();
91};
92
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070093struct DisplayTestCommon : public testing::Test {
94 // Uses the full implementation of a display
95 class FullImplDisplay : public impl::Display {
Lloyd Pique01c77c12019-04-17 12:48:32 -070096 public:
Lloyd Pique01c77c12019-04-17 12:48:32 -070097 using impl::Display::injectOutputLayerForTest;
98 virtual void injectOutputLayerForTest(std::unique_ptr<compositionengine::OutputLayer>) = 0;
99 };
100
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700101 // Uses a special implementation with key internal member functions set up
102 // as mock implementations, to allow for easier testing.
103 struct PartialMockDisplay : public impl::Display {
104 PartialMockDisplay(const compositionengine::CompositionEngine& compositionEngine)
105 : mCompositionEngine(compositionEngine) {}
106
107 // compositionengine::Output overrides
108 const OutputCompositionState& getState() const override { return mState; }
109 OutputCompositionState& editState() override { return mState; }
110
111 // compositionengine::impl::Output overrides
112 const CompositionEngine& getCompositionEngine() const override {
113 return mCompositionEngine;
114 };
115
116 // Mock implementation overrides
117 MOCK_CONST_METHOD0(getOutputLayerCount, size_t());
118 MOCK_CONST_METHOD1(getOutputLayerOrderedByZByIndex,
119 compositionengine::OutputLayer*(size_t));
120 MOCK_METHOD2(ensureOutputLayer,
121 compositionengine::OutputLayer*(std::optional<size_t>, const sp<LayerFE>&));
122 MOCK_METHOD0(finalizePendingOutputLayers, void());
123 MOCK_METHOD0(clearOutputLayers, void());
124 MOCK_CONST_METHOD1(dumpState, void(std::string&));
125 MOCK_METHOD1(injectOutputLayerForTest, compositionengine::OutputLayer*(const sp<LayerFE>&));
126 MOCK_METHOD1(injectOutputLayerForTest, void(std::unique_ptr<OutputLayer>));
127 MOCK_CONST_METHOD0(anyLayersRequireClientComposition, bool());
128 MOCK_CONST_METHOD0(allLayersRequireClientComposition, bool());
129 MOCK_METHOD1(applyChangedTypesToLayers, void(const impl::Display::ChangedTypes&));
130 MOCK_METHOD1(applyDisplayRequests, void(const impl::Display::DisplayRequests&));
131 MOCK_METHOD1(applyLayerRequestsToLayers, void(const impl::Display::LayerRequests&));
132
133 const compositionengine::CompositionEngine& mCompositionEngine;
134 impl::OutputCompositionState mState;
135 };
136
137 static std::string getDisplayNameFromCurrentTest() {
138 const ::testing::TestInfo* const test_info =
139 ::testing::UnitTest::GetInstance()->current_test_info();
140 return std::string("display for ") + test_info->test_case_name() + "." + test_info->name();
141 }
142
143 template <typename Display>
Lloyd Pique01c77c12019-04-17 12:48:32 -0700144 static std::shared_ptr<Display> createDisplay(
145 const compositionengine::CompositionEngine& compositionEngine,
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700146 compositionengine::DisplayCreationArgs args) {
147 args.name = getDisplayNameFromCurrentTest();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700148 return impl::createDisplayTemplated<Display>(compositionEngine, args);
149 }
150
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700151 template <typename Display>
152 static std::shared_ptr<StrictMock<Display>> createPartialMockDisplay(
153 const compositionengine::CompositionEngine& compositionEngine,
154 compositionengine::DisplayCreationArgs args) {
155 args.name = getDisplayNameFromCurrentTest();
156 auto display = std::make_shared<StrictMock<Display>>(compositionEngine);
Lloyd Pique66d68602019-02-13 14:23:31 -0800157
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700158 display->setConfiguration(args);
159
160 return display;
161 }
162
163 DisplayTestCommon() {
164 EXPECT_CALL(mCompositionEngine, getHwComposer()).WillRepeatedly(ReturnRef(mHwComposer));
Lloyd Piquee9eff972020-05-05 12:36:44 -0700165 EXPECT_CALL(mCompositionEngine, getRenderEngine()).WillRepeatedly(ReturnRef(mRenderEngine));
166 EXPECT_CALL(mRenderEngine, supportsProtectedContent()).WillRepeatedly(Return(false));
Peiyong Lin09f910f2020-09-25 10:54:13 -0700167 EXPECT_CALL(mRenderEngine, isProtected()).WillRepeatedly(Return(false));
Kriti Dang646f8ec2022-01-18 14:35:02 +0100168 EXPECT_CALL(mHwComposer, getBootDisplayModeSupport()).WillRepeatedly(Return(false));
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700169 }
170
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700171 DisplayCreationArgs getDisplayCreationArgsForPhysicalDisplay() {
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700172 return DisplayCreationArgsBuilder()
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800173 .setId(DEFAULT_DISPLAY_ID)
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800174 .setPixels(DEFAULT_RESOLUTION)
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700175 .setIsSecure(true)
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700176 .setPowerAdvisor(&mPowerAdvisor)
177 .build();
178 }
179
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800180 DisplayCreationArgs getDisplayCreationArgsForGpuVirtualDisplay() {
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700181 return DisplayCreationArgsBuilder()
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800182 .setId(GPU_VIRTUAL_DISPLAY_ID)
183 .setPixels(DEFAULT_RESOLUTION)
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700184 .setIsSecure(false)
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700185 .setPowerAdvisor(&mPowerAdvisor)
186 .build();
187 }
188
189 StrictMock<android::mock::HWComposer> mHwComposer;
190 StrictMock<Hwc2::mock::PowerAdvisor> mPowerAdvisor;
Lloyd Piquee9eff972020-05-05 12:36:44 -0700191 StrictMock<renderengine::mock::RenderEngine> mRenderEngine;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700192 StrictMock<mock::CompositionEngine> mCompositionEngine;
193 sp<mock::NativeWindow> mNativeWindow = new StrictMock<mock::NativeWindow>();
194};
195
196struct PartialMockDisplayTestCommon : public DisplayTestCommon {
197 using Display = DisplayTestCommon::PartialMockDisplay;
198 std::shared_ptr<Display> mDisplay =
199 createPartialMockDisplay<Display>(mCompositionEngine,
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700200 getDisplayCreationArgsForPhysicalDisplay());
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700201};
202
203struct FullDisplayImplTestCommon : public DisplayTestCommon {
204 using Display = DisplayTestCommon::FullImplDisplay;
205 std::shared_ptr<Display> mDisplay =
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700206 createDisplay<Display>(mCompositionEngine, getDisplayCreationArgsForPhysicalDisplay());
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700207};
208
209struct DisplayWithLayersTestCommon : public FullDisplayImplTestCommon {
210 DisplayWithLayersTestCommon() {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700211 mDisplay->injectOutputLayerForTest(
Lloyd Piquede196652020-01-22 17:29:58 -0800212 std::unique_ptr<compositionengine::OutputLayer>(mLayer1.outputLayer));
Lloyd Pique01c77c12019-04-17 12:48:32 -0700213 mDisplay->injectOutputLayerForTest(
Lloyd Piquede196652020-01-22 17:29:58 -0800214 std::unique_ptr<compositionengine::OutputLayer>(mLayer2.outputLayer));
Lloyd Pique01c77c12019-04-17 12:48:32 -0700215 mDisplay->injectOutputLayerForTest(
Lloyd Piquede196652020-01-22 17:29:58 -0800216 std::unique_ptr<compositionengine::OutputLayer>(mLayer3.outputLayer));
Lloyd Pique66d68602019-02-13 14:23:31 -0800217 }
Lloyd Pique45a165a2018-10-19 11:54:47 -0700218
Lloyd Piquede196652020-01-22 17:29:58 -0800219 Layer mLayer1;
220 Layer mLayer2;
221 LayerNoHWC2Layer mLayer3;
222 StrictMock<HWC2::mock::Layer> hwc2LayerUnknown;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700223 std::shared_ptr<Display> mDisplay =
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700224 createDisplay<Display>(mCompositionEngine, getDisplayCreationArgsForPhysicalDisplay());
Lloyd Pique45a165a2018-10-19 11:54:47 -0700225};
226
Lloyd Pique66d68602019-02-13 14:23:31 -0800227/*
Lloyd Pique45a165a2018-10-19 11:54:47 -0700228 * Basic construction
229 */
230
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700231struct DisplayCreationTest : public DisplayTestCommon {
232 using Display = DisplayTestCommon::FullImplDisplay;
233};
Lloyd Pique45a165a2018-10-19 11:54:47 -0700234
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700235TEST_F(DisplayCreationTest, createPhysicalInternalDisplay) {
236 auto display =
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700237 impl::createDisplay(mCompositionEngine, getDisplayCreationArgsForPhysicalDisplay());
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700238 EXPECT_TRUE(display->isSecure());
239 EXPECT_FALSE(display->isVirtual());
240 EXPECT_EQ(DEFAULT_DISPLAY_ID, display->getId());
241}
Lloyd Pique45a165a2018-10-19 11:54:47 -0700242
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800243TEST_F(DisplayCreationTest, createGpuVirtualDisplay) {
244 auto display =
245 impl::createDisplay(mCompositionEngine, getDisplayCreationArgsForGpuVirtualDisplay());
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700246 EXPECT_FALSE(display->isSecure());
247 EXPECT_TRUE(display->isVirtual());
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200248 EXPECT_TRUE(GpuVirtualDisplayId::tryCast(display->getId()));
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700249}
250
251/*
252 * Display::setConfiguration()
253 */
254
255using DisplaySetConfigurationTest = PartialMockDisplayTestCommon;
256
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700257TEST_F(DisplaySetConfigurationTest, configuresPhysicalDisplay) {
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800258 mDisplay->setConfiguration(DisplayCreationArgsBuilder()
259 .setId(DEFAULT_DISPLAY_ID)
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800260 .setPixels(DEFAULT_RESOLUTION)
261 .setIsSecure(true)
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800262 .setPowerAdvisor(&mPowerAdvisor)
263 .setName(getDisplayNameFromCurrentTest())
264 .build());
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700265
266 EXPECT_EQ(DEFAULT_DISPLAY_ID, mDisplay->getId());
267 EXPECT_TRUE(mDisplay->isSecure());
268 EXPECT_FALSE(mDisplay->isVirtual());
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700269 EXPECT_FALSE(mDisplay->isValid());
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700270
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700271 const auto& filter = mDisplay->getState().layerFilter;
272 EXPECT_EQ(ui::INVALID_LAYER_STACK, filter.layerStack);
273 EXPECT_FALSE(filter.toInternalDisplay);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700274}
275
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800276TEST_F(DisplaySetConfigurationTest, configuresHalVirtualDisplay) {
277 mDisplay->setConfiguration(DisplayCreationArgsBuilder()
278 .setId(HAL_VIRTUAL_DISPLAY_ID)
279 .setPixels(DEFAULT_RESOLUTION)
280 .setIsSecure(false)
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800281 .setPowerAdvisor(&mPowerAdvisor)
282 .setName(getDisplayNameFromCurrentTest())
283 .build());
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700284
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800285 EXPECT_EQ(HAL_VIRTUAL_DISPLAY_ID, mDisplay->getId());
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700286 EXPECT_FALSE(mDisplay->isSecure());
287 EXPECT_TRUE(mDisplay->isVirtual());
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700288 EXPECT_FALSE(mDisplay->isValid());
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700289
290 const auto& filter = mDisplay->getState().layerFilter;
291 EXPECT_EQ(ui::INVALID_LAYER_STACK, filter.layerStack);
292 EXPECT_FALSE(filter.toInternalDisplay);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700293}
294
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800295TEST_F(DisplaySetConfigurationTest, configuresGpuVirtualDisplay) {
296 mDisplay->setConfiguration(DisplayCreationArgsBuilder()
297 .setId(GPU_VIRTUAL_DISPLAY_ID)
298 .setPixels(DEFAULT_RESOLUTION)
299 .setIsSecure(false)
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800300 .setPowerAdvisor(&mPowerAdvisor)
301 .setName(getDisplayNameFromCurrentTest())
302 .build());
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700303
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800304 EXPECT_EQ(GPU_VIRTUAL_DISPLAY_ID, mDisplay->getId());
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700305 EXPECT_FALSE(mDisplay->isSecure());
306 EXPECT_TRUE(mDisplay->isVirtual());
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700307 EXPECT_FALSE(mDisplay->isValid());
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700308
309 const auto& filter = mDisplay->getState().layerFilter;
310 EXPECT_EQ(ui::INVALID_LAYER_STACK, filter.layerStack);
311 EXPECT_FALSE(filter.toInternalDisplay);
Lloyd Pique45a165a2018-10-19 11:54:47 -0700312}
313
Lloyd Pique66d68602019-02-13 14:23:31 -0800314/*
Lloyd Pique45a165a2018-10-19 11:54:47 -0700315 * Display::disconnect()
316 */
317
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700318using DisplayDisconnectTest = PartialMockDisplayTestCommon;
319
320TEST_F(DisplayDisconnectTest, disconnectsDisplay) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200321 // The first call to disconnect will disconnect the display with the HWC.
322 EXPECT_CALL(mHwComposer, disconnectDisplay(HalDisplayId(DEFAULT_DISPLAY_ID))).Times(1);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700323 mDisplay->disconnect();
Lloyd Pique45a165a2018-10-19 11:54:47 -0700324
325 // Subsequent calls will do nothing,
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200326 EXPECT_CALL(mHwComposer, disconnectDisplay(HalDisplayId(DEFAULT_DISPLAY_ID))).Times(0);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700327 mDisplay->disconnect();
Lloyd Pique45a165a2018-10-19 11:54:47 -0700328}
329
Lloyd Pique66d68602019-02-13 14:23:31 -0800330/*
Lloyd Pique32cbe282018-10-19 13:09:22 -0700331 * Display::setColorTransform()
332 */
333
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700334using DisplaySetColorTransformTest = PartialMockDisplayTestCommon;
335
336TEST_F(DisplaySetColorTransformTest, setsTransform) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800337 // No change does nothing
338 CompositionRefreshArgs refreshArgs;
339 refreshArgs.colorTransformMatrix = std::nullopt;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700340 mDisplay->setColorTransform(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800341
Lloyd Pique32cbe282018-10-19 13:09:22 -0700342 // Identity matrix sets an identity state value
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800343 const mat4 kIdentity;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700344
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200345 EXPECT_CALL(mHwComposer, setColorTransform(HalDisplayId(DEFAULT_DISPLAY_ID), kIdentity))
346 .Times(1);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700347
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800348 refreshArgs.colorTransformMatrix = kIdentity;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700349 mDisplay->setColorTransform(refreshArgs);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700350
351 // Non-identity matrix sets a non-identity state value
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800352 const mat4 kNonIdentity = mat4() * 2;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700353
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200354 EXPECT_CALL(mHwComposer, setColorTransform(HalDisplayId(DEFAULT_DISPLAY_ID), kNonIdentity))
355 .Times(1);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700356
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800357 refreshArgs.colorTransformMatrix = kNonIdentity;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700358 mDisplay->setColorTransform(refreshArgs);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700359}
360
Lloyd Pique66d68602019-02-13 14:23:31 -0800361/*
Lloyd Pique32cbe282018-10-19 13:09:22 -0700362 * Display::setColorMode()
363 */
364
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700365using DisplaySetColorModeTest = PartialMockDisplayTestCommon;
366
367TEST_F(DisplaySetColorModeTest, setsModeUnlessNoChange) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800368 using ColorProfile = Output::ColorProfile;
369
Lloyd Pique31cb2942018-10-19 17:23:03 -0700370 mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700371 mDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
Lloyd Piquef5275482019-01-29 18:42:42 -0800372 mock::DisplayColorProfile* colorProfile = new StrictMock<mock::DisplayColorProfile>();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700373 mDisplay->setDisplayColorProfileForTest(std::unique_ptr<DisplayColorProfile>(colorProfile));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700374
Lloyd Piquef5275482019-01-29 18:42:42 -0800375 EXPECT_CALL(*colorProfile, getTargetDataspace(_, _, _))
376 .WillRepeatedly(Return(ui::Dataspace::UNKNOWN));
Lloyd Pique32cbe282018-10-19 13:09:22 -0700377
378 // These values are expected to be the initial state.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700379 ASSERT_EQ(ui::ColorMode::NATIVE, mDisplay->getState().colorMode);
380 ASSERT_EQ(ui::Dataspace::UNKNOWN, mDisplay->getState().dataspace);
381 ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mDisplay->getState().renderIntent);
382 ASSERT_EQ(ui::Dataspace::UNKNOWN, mDisplay->getState().targetDataspace);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700383
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700384 // Otherwise if the values are unchanged, nothing happens
385 mDisplay->setColorProfile(ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
386 ui::RenderIntent::COLORIMETRIC, ui::Dataspace::UNKNOWN});
Lloyd Pique32cbe282018-10-19 13:09:22 -0700387
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700388 EXPECT_EQ(ui::ColorMode::NATIVE, mDisplay->getState().colorMode);
389 EXPECT_EQ(ui::Dataspace::UNKNOWN, mDisplay->getState().dataspace);
390 EXPECT_EQ(ui::RenderIntent::COLORIMETRIC, mDisplay->getState().renderIntent);
391 EXPECT_EQ(ui::Dataspace::UNKNOWN, mDisplay->getState().targetDataspace);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700392
393 // Otherwise if the values are different, updates happen
Lloyd Piqueef958122019-02-05 18:00:12 -0800394 EXPECT_CALL(*renderSurface, setBufferDataspace(ui::Dataspace::DISPLAY_P3)).Times(1);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700395 EXPECT_CALL(mHwComposer,
Lloyd Piqueef958122019-02-05 18:00:12 -0800396 setActiveColorMode(DEFAULT_DISPLAY_ID, ui::ColorMode::DISPLAY_P3,
Lloyd Pique32cbe282018-10-19 13:09:22 -0700397 ui::RenderIntent::TONE_MAP_COLORIMETRIC))
398 .Times(1);
399
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700400 mDisplay->setColorProfile(ColorProfile{ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3,
401 ui::RenderIntent::TONE_MAP_COLORIMETRIC,
402 ui::Dataspace::UNKNOWN});
Lloyd Pique32cbe282018-10-19 13:09:22 -0700403
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700404 EXPECT_EQ(ui::ColorMode::DISPLAY_P3, mDisplay->getState().colorMode);
405 EXPECT_EQ(ui::Dataspace::DISPLAY_P3, mDisplay->getState().dataspace);
406 EXPECT_EQ(ui::RenderIntent::TONE_MAP_COLORIMETRIC, mDisplay->getState().renderIntent);
407 EXPECT_EQ(ui::Dataspace::UNKNOWN, mDisplay->getState().targetDataspace);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700408}
409
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700410TEST_F(DisplaySetColorModeTest, doesNothingForVirtualDisplay) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800411 using ColorProfile = Output::ColorProfile;
412
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800413 auto args = getDisplayCreationArgsForGpuVirtualDisplay();
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700414 std::shared_ptr<impl::Display> virtualDisplay = impl::createDisplay(mCompositionEngine, args);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700415
Lloyd Piquef5275482019-01-29 18:42:42 -0800416 mock::DisplayColorProfile* colorProfile = new StrictMock<mock::DisplayColorProfile>();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700417 virtualDisplay->setDisplayColorProfileForTest(
Lloyd Piquef5275482019-01-29 18:42:42 -0800418 std::unique_ptr<DisplayColorProfile>(colorProfile));
419
420 EXPECT_CALL(*colorProfile,
421 getTargetDataspace(ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3,
422 ui::Dataspace::UNKNOWN))
423 .WillOnce(Return(ui::Dataspace::UNKNOWN));
424
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700425 virtualDisplay->setColorProfile(
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800426 ColorProfile{ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3,
427 ui::RenderIntent::TONE_MAP_COLORIMETRIC, ui::Dataspace::UNKNOWN});
Lloyd Pique32cbe282018-10-19 13:09:22 -0700428
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700429 EXPECT_EQ(ui::ColorMode::NATIVE, virtualDisplay->getState().colorMode);
430 EXPECT_EQ(ui::Dataspace::UNKNOWN, virtualDisplay->getState().dataspace);
431 EXPECT_EQ(ui::RenderIntent::COLORIMETRIC, virtualDisplay->getState().renderIntent);
432 EXPECT_EQ(ui::Dataspace::UNKNOWN, virtualDisplay->getState().targetDataspace);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700433}
434
Lloyd Pique66d68602019-02-13 14:23:31 -0800435/*
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700436 * Display::createDisplayColorProfile()
437 */
438
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700439using DisplayCreateColorProfileTest = PartialMockDisplayTestCommon;
440
441TEST_F(DisplayCreateColorProfileTest, setsDisplayColorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700442 EXPECT_TRUE(mDisplay->getDisplayColorProfile() == nullptr);
443 mDisplay->createDisplayColorProfile(
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700444 DisplayColorProfileCreationArgs{false, HdrCapabilities(), 0,
445 DisplayColorProfileCreationArgs::HwcColorModes()});
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700446 EXPECT_TRUE(mDisplay->getDisplayColorProfile() != nullptr);
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700447}
448
Lloyd Pique66d68602019-02-13 14:23:31 -0800449/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700450 * Display::createRenderSurface()
451 */
452
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700453using DisplayCreateRenderSurfaceTest = PartialMockDisplayTestCommon;
454
455TEST_F(DisplayCreateRenderSurfaceTest, setsRenderSurface) {
chaviw8beb4142019-04-11 13:09:05 -0700456 EXPECT_CALL(*mNativeWindow, disconnect(NATIVE_WINDOW_API_EGL)).WillRepeatedly(Return(NO_ERROR));
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700457 EXPECT_TRUE(mDisplay->getRenderSurface() == nullptr);
Dominik Laskowski50121d52021-04-23 13:01:16 -0700458 mDisplay->createRenderSurface(RenderSurfaceCreationArgsBuilder()
459 .setDisplayWidth(640)
460 .setDisplayHeight(480)
461 .setNativeWindow(mNativeWindow)
462 .build());
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700463 EXPECT_TRUE(mDisplay->getRenderSurface() != nullptr);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700464}
465
Lloyd Pique66d68602019-02-13 14:23:31 -0800466/*
Lloyd Piquedf336d92019-03-07 21:38:42 -0800467 * Display::createOutputLayer()
468 */
469
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700470using DisplayCreateOutputLayerTest = FullDisplayImplTestCommon;
471
472TEST_F(DisplayCreateOutputLayerTest, setsHwcLayer) {
Ady Abrahame0eafa82022-02-02 19:30:47 -0800473 sp<StrictMock<mock::LayerFE>> layerFE = sp<StrictMock<mock::LayerFE>>::make();
Lloyd Piquea516c002021-05-07 14:36:58 -0700474 auto hwcLayer = std::make_shared<StrictMock<HWC2::mock::Layer>>();
Lloyd Piquedf336d92019-03-07 21:38:42 -0800475
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200476 EXPECT_CALL(mHwComposer, createLayer(HalDisplayId(DEFAULT_DISPLAY_ID)))
Lloyd Piquea516c002021-05-07 14:36:58 -0700477 .WillOnce(Return(hwcLayer));
Lloyd Piquedf336d92019-03-07 21:38:42 -0800478
Lloyd Piquede196652020-01-22 17:29:58 -0800479 auto outputLayer = mDisplay->createOutputLayer(layerFE);
Lloyd Piquedf336d92019-03-07 21:38:42 -0800480
Lloyd Piquea516c002021-05-07 14:36:58 -0700481 EXPECT_EQ(hwcLayer.get(), outputLayer->getHwcLayer());
Lloyd Piquedf336d92019-03-07 21:38:42 -0800482
Lloyd Piquedf336d92019-03-07 21:38:42 -0800483 outputLayer.reset();
484}
485
486/*
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800487 * Display::setReleasedLayers()
488 */
489
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700490using DisplaySetReleasedLayersTest = DisplayWithLayersTestCommon;
491
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800492TEST_F(DisplaySetReleasedLayersTest, doesNothingIfGpuDisplay) {
493 auto args = getDisplayCreationArgsForGpuVirtualDisplay();
494 std::shared_ptr<impl::Display> gpuDisplay = impl::createDisplay(mCompositionEngine, args);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800495
496 sp<mock::LayerFE> layerXLayerFE = new StrictMock<mock::LayerFE>();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800497
498 {
499 Output::ReleasedLayers releasedLayers;
500 releasedLayers.emplace_back(layerXLayerFE);
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800501 gpuDisplay->setReleasedLayers(std::move(releasedLayers));
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800502 }
503
504 CompositionRefreshArgs refreshArgs;
Lloyd Piquede196652020-01-22 17:29:58 -0800505 refreshArgs.layersWithQueuedFrames.push_back(layerXLayerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800506
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800507 gpuDisplay->setReleasedLayers(refreshArgs);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800508
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800509 const auto& releasedLayers = gpuDisplay->getReleasedLayersForTest();
510 ASSERT_EQ(1u, releasedLayers.size());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800511}
512
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700513TEST_F(DisplaySetReleasedLayersTest, doesNothingIfNoLayersWithQueuedFrames) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800514 sp<mock::LayerFE> layerXLayerFE = new StrictMock<mock::LayerFE>();
515
516 {
517 Output::ReleasedLayers releasedLayers;
518 releasedLayers.emplace_back(layerXLayerFE);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700519 mDisplay->setReleasedLayers(std::move(releasedLayers));
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800520 }
521
522 CompositionRefreshArgs refreshArgs;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700523 mDisplay->setReleasedLayers(refreshArgs);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800524
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700525 const auto& releasedLayers = mDisplay->getReleasedLayersForTest();
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800526 ASSERT_EQ(1u, releasedLayers.size());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800527}
528
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700529TEST_F(DisplaySetReleasedLayersTest, setReleasedLayers) {
Lloyd Piquede196652020-01-22 17:29:58 -0800530 sp<mock::LayerFE> unknownLayer = new StrictMock<mock::LayerFE>();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800531
532 CompositionRefreshArgs refreshArgs;
Lloyd Piquede196652020-01-22 17:29:58 -0800533 refreshArgs.layersWithQueuedFrames.push_back(mLayer1.layerFE);
534 refreshArgs.layersWithQueuedFrames.push_back(mLayer2.layerFE);
535 refreshArgs.layersWithQueuedFrames.push_back(unknownLayer);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800536
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700537 mDisplay->setReleasedLayers(refreshArgs);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800538
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700539 const auto& releasedLayers = mDisplay->getReleasedLayersForTest();
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800540 ASSERT_EQ(2u, releasedLayers.size());
Lloyd Piquede196652020-01-22 17:29:58 -0800541 ASSERT_EQ(mLayer1.layerFE.get(), releasedLayers[0].promote().get());
542 ASSERT_EQ(mLayer2.layerFE.get(), releasedLayers[1].promote().get());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800543}
544
545/*
Lloyd Pique66d68602019-02-13 14:23:31 -0800546 * Display::chooseCompositionStrategy()
547 */
548
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700549using DisplayChooseCompositionStrategyTest = PartialMockDisplayTestCommon;
Lloyd Pique66d68602019-02-13 14:23:31 -0800550
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800551TEST_F(DisplayChooseCompositionStrategyTest, takesEarlyOutIfGpuDisplay) {
552 auto args = getDisplayCreationArgsForGpuVirtualDisplay();
553 std::shared_ptr<Display> gpuDisplay =
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700554 createPartialMockDisplay<Display>(mCompositionEngine, args);
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800555 EXPECT_TRUE(GpuVirtualDisplayId::tryCast(gpuDisplay->getId()));
Lloyd Pique66d68602019-02-13 14:23:31 -0800556
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800557 gpuDisplay->chooseCompositionStrategy();
Lloyd Pique66d68602019-02-13 14:23:31 -0800558
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800559 auto& state = gpuDisplay->getState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800560 EXPECT_TRUE(state.usesClientComposition);
561 EXPECT_FALSE(state.usesDeviceComposition);
562}
563
564TEST_F(DisplayChooseCompositionStrategyTest, takesEarlyOutOnHwcError) {
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700565 EXPECT_CALL(*mDisplay, anyLayersRequireClientComposition()).WillOnce(Return(false));
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200566 EXPECT_CALL(mHwComposer,
Ady Abraham43065bd2021-12-10 17:22:15 -0800567 getDeviceCompositionChanges(HalDisplayId(DEFAULT_DISPLAY_ID), false, _, _, _, _))
Lloyd Pique66d68602019-02-13 14:23:31 -0800568 .WillOnce(Return(INVALID_OPERATION));
569
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700570 mDisplay->chooseCompositionStrategy();
Lloyd Pique66d68602019-02-13 14:23:31 -0800571
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700572 auto& state = mDisplay->getState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800573 EXPECT_TRUE(state.usesClientComposition);
574 EXPECT_FALSE(state.usesDeviceComposition);
575}
576
577TEST_F(DisplayChooseCompositionStrategyTest, normalOperation) {
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700578 // Since two calls are made to anyLayersRequireClientComposition with different return
579 // values, use a Sequence to control the matching so the values are returned in a known
580 // order.
Lloyd Pique66d68602019-02-13 14:23:31 -0800581 Sequence s;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700582 EXPECT_CALL(*mDisplay, anyLayersRequireClientComposition())
583 .InSequence(s)
584 .WillOnce(Return(true));
585 EXPECT_CALL(*mDisplay, anyLayersRequireClientComposition())
Lloyd Pique66d68602019-02-13 14:23:31 -0800586 .InSequence(s)
587 .WillOnce(Return(false));
588
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700589 EXPECT_CALL(mHwComposer,
Ady Abraham43065bd2021-12-10 17:22:15 -0800590 getDeviceCompositionChanges(HalDisplayId(DEFAULT_DISPLAY_ID), true, _, _, _, _))
Lloyd Pique66d68602019-02-13 14:23:31 -0800591 .WillOnce(Return(NO_ERROR));
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700592 EXPECT_CALL(*mDisplay, allLayersRequireClientComposition()).WillOnce(Return(false));
Lloyd Pique66d68602019-02-13 14:23:31 -0800593
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700594 mDisplay->chooseCompositionStrategy();
Lloyd Pique66d68602019-02-13 14:23:31 -0800595
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700596 auto& state = mDisplay->getState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800597 EXPECT_FALSE(state.usesClientComposition);
598 EXPECT_TRUE(state.usesDeviceComposition);
599}
600
Alec Mouricdf16792021-12-10 13:16:06 -0800601TEST_F(DisplayChooseCompositionStrategyTest, normalOperationWithDisplayBrightness) {
602 // Since two calls are made to anyLayersRequireClientComposition with different return
603 // values, use a Sequence to control the matching so the values are returned in a known
604 // order.
605 constexpr float kDisplayBrightness = 0.5f;
606 Sequence s;
607 EXPECT_CALL(*mDisplay, anyLayersRequireClientComposition())
608 .InSequence(s)
609 .WillOnce(Return(true));
610 EXPECT_CALL(*mDisplay, anyLayersRequireClientComposition())
611 .InSequence(s)
612 .WillOnce(Return(false));
613 EXPECT_CALL(mHwComposer,
614 setDisplayBrightness(DEFAULT_DISPLAY_ID, kDisplayBrightness,
615 Hwc2::Composer::DisplayBrightnessOptions{.applyImmediately =
616 false}))
617 .WillOnce(Return(ByMove(ftl::yield<status_t>(NO_ERROR))));
618
619 EXPECT_CALL(mHwComposer,
620 getDeviceCompositionChanges(HalDisplayId(DEFAULT_DISPLAY_ID), true, _, _, _, _))
621 .WillOnce(Return(NO_ERROR));
622 EXPECT_CALL(*mDisplay, allLayersRequireClientComposition()).WillOnce(Return(false));
623
624 mDisplay->setNextBrightness(kDisplayBrightness);
625 mDisplay->chooseCompositionStrategy();
626
627 auto& state = mDisplay->getState();
628 EXPECT_FALSE(state.usesClientComposition);
629 EXPECT_TRUE(state.usesDeviceComposition);
630 EXPECT_FALSE(state.displayBrightness.has_value());
631}
632
Lloyd Pique66d68602019-02-13 14:23:31 -0800633TEST_F(DisplayChooseCompositionStrategyTest, normalOperationWithChanges) {
634 android::HWComposer::DeviceRequestedChanges changes{
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500635 {{nullptr, Composition::CLIENT}},
Peiyong Line9d809e2020-04-14 13:10:48 -0700636 hal::DisplayRequest::FLIP_CLIENT_TARGET,
637 {{nullptr, hal::LayerRequest::CLEAR_CLIENT_TARGET}},
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700638 {hal::PixelFormat::RGBA_8888, hal::Dataspace::UNKNOWN},
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700639 -1.f,
Lloyd Pique66d68602019-02-13 14:23:31 -0800640 };
641
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700642 // Since two calls are made to anyLayersRequireClientComposition with different return
643 // values, use a Sequence to control the matching so the values are returned in a known
644 // order.
Lloyd Pique66d68602019-02-13 14:23:31 -0800645 Sequence s;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700646 EXPECT_CALL(*mDisplay, anyLayersRequireClientComposition())
647 .InSequence(s)
648 .WillOnce(Return(true));
649 EXPECT_CALL(*mDisplay, anyLayersRequireClientComposition())
Lloyd Pique66d68602019-02-13 14:23:31 -0800650 .InSequence(s)
651 .WillOnce(Return(false));
652
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700653 EXPECT_CALL(mHwComposer,
Ady Abraham43065bd2021-12-10 17:22:15 -0800654 getDeviceCompositionChanges(HalDisplayId(DEFAULT_DISPLAY_ID), true, _, _, _, _))
655 .WillOnce(DoAll(SetArgPointee<5>(changes), Return(NO_ERROR)));
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700656 EXPECT_CALL(*mDisplay, applyChangedTypesToLayers(changes.changedTypes)).Times(1);
657 EXPECT_CALL(*mDisplay, applyDisplayRequests(changes.displayRequests)).Times(1);
658 EXPECT_CALL(*mDisplay, applyLayerRequestsToLayers(changes.layerRequests)).Times(1);
659 EXPECT_CALL(*mDisplay, allLayersRequireClientComposition()).WillOnce(Return(false));
Lloyd Pique66d68602019-02-13 14:23:31 -0800660
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700661 mDisplay->chooseCompositionStrategy();
Lloyd Pique66d68602019-02-13 14:23:31 -0800662
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700663 auto& state = mDisplay->getState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800664 EXPECT_FALSE(state.usesClientComposition);
665 EXPECT_TRUE(state.usesDeviceComposition);
666}
667
668/*
Lloyd Pique688abd42019-02-15 15:42:24 -0800669 * Display::getSkipColorTransform()
670 */
671
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700672using DisplayGetSkipColorTransformTest = DisplayWithLayersTestCommon;
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500673using aidl::android::hardware::graphics::composer3::DisplayCapability;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700674
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800675TEST_F(DisplayGetSkipColorTransformTest, checksCapabilityIfGpuDisplay) {
Ady Abrahamde549d42022-01-26 19:19:17 -0800676 EXPECT_CALL(mHwComposer, hasCapability(Capability::SKIP_CLIENT_COLOR_TRANSFORM))
Dominik Laskowski1162e472020-04-02 19:02:47 -0700677 .WillOnce(Return(true));
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800678 auto args = getDisplayCreationArgsForGpuVirtualDisplay();
679 auto gpuDisplay{impl::createDisplay(mCompositionEngine, args)};
680 EXPECT_TRUE(gpuDisplay->getSkipColorTransform());
Lloyd Pique688abd42019-02-15 15:42:24 -0800681}
682
Dominik Laskowski1162e472020-04-02 19:02:47 -0700683TEST_F(DisplayGetSkipColorTransformTest, checksDisplayCapability) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800684 EXPECT_CALL(mHwComposer,
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200685 hasDisplayCapability(HalDisplayId(DEFAULT_DISPLAY_ID),
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500686 DisplayCapability::SKIP_CLIENT_COLOR_TRANSFORM))
Lloyd Pique688abd42019-02-15 15:42:24 -0800687 .WillOnce(Return(true));
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700688 EXPECT_TRUE(mDisplay->getSkipColorTransform());
Lloyd Pique688abd42019-02-15 15:42:24 -0800689}
690
691/*
Lloyd Pique66d68602019-02-13 14:23:31 -0800692 * Display::anyLayersRequireClientComposition()
693 */
694
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700695using DisplayAnyLayersRequireClientCompositionTest = DisplayWithLayersTestCommon;
696
697TEST_F(DisplayAnyLayersRequireClientCompositionTest, returnsFalse) {
Lloyd Piquede196652020-01-22 17:29:58 -0800698 EXPECT_CALL(*mLayer1.outputLayer, requiresClientComposition()).WillOnce(Return(false));
699 EXPECT_CALL(*mLayer2.outputLayer, requiresClientComposition()).WillOnce(Return(false));
700 EXPECT_CALL(*mLayer3.outputLayer, requiresClientComposition()).WillOnce(Return(false));
Lloyd Pique66d68602019-02-13 14:23:31 -0800701
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700702 EXPECT_FALSE(mDisplay->anyLayersRequireClientComposition());
Lloyd Pique66d68602019-02-13 14:23:31 -0800703}
704
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700705TEST_F(DisplayAnyLayersRequireClientCompositionTest, returnsTrue) {
Lloyd Piquede196652020-01-22 17:29:58 -0800706 EXPECT_CALL(*mLayer1.outputLayer, requiresClientComposition()).WillOnce(Return(false));
707 EXPECT_CALL(*mLayer2.outputLayer, requiresClientComposition()).WillOnce(Return(true));
Lloyd Pique66d68602019-02-13 14:23:31 -0800708
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700709 EXPECT_TRUE(mDisplay->anyLayersRequireClientComposition());
Lloyd Pique66d68602019-02-13 14:23:31 -0800710}
711
712/*
713 * Display::allLayersRequireClientComposition()
714 */
715
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700716using DisplayAllLayersRequireClientCompositionTest = DisplayWithLayersTestCommon;
717
718TEST_F(DisplayAllLayersRequireClientCompositionTest, returnsTrue) {
Lloyd Piquede196652020-01-22 17:29:58 -0800719 EXPECT_CALL(*mLayer1.outputLayer, requiresClientComposition()).WillOnce(Return(true));
720 EXPECT_CALL(*mLayer2.outputLayer, requiresClientComposition()).WillOnce(Return(true));
721 EXPECT_CALL(*mLayer3.outputLayer, requiresClientComposition()).WillOnce(Return(true));
Lloyd Pique66d68602019-02-13 14:23:31 -0800722
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700723 EXPECT_TRUE(mDisplay->allLayersRequireClientComposition());
Lloyd Pique66d68602019-02-13 14:23:31 -0800724}
725
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700726TEST_F(DisplayAllLayersRequireClientCompositionTest, returnsFalse) {
Lloyd Piquede196652020-01-22 17:29:58 -0800727 EXPECT_CALL(*mLayer1.outputLayer, requiresClientComposition()).WillOnce(Return(true));
728 EXPECT_CALL(*mLayer2.outputLayer, requiresClientComposition()).WillOnce(Return(false));
Lloyd Pique66d68602019-02-13 14:23:31 -0800729
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700730 EXPECT_FALSE(mDisplay->allLayersRequireClientComposition());
Lloyd Pique66d68602019-02-13 14:23:31 -0800731}
732
733/*
734 * Display::applyChangedTypesToLayers()
735 */
736
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700737using DisplayApplyChangedTypesToLayersTest = DisplayWithLayersTestCommon;
738
739TEST_F(DisplayApplyChangedTypesToLayersTest, takesEarlyOutIfNoChangedLayers) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700740 mDisplay->applyChangedTypesToLayers(impl::Display::ChangedTypes());
Lloyd Pique66d68602019-02-13 14:23:31 -0800741}
742
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700743TEST_F(DisplayApplyChangedTypesToLayersTest, appliesChanges) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500744 EXPECT_CALL(*mLayer1.outputLayer, applyDeviceCompositionTypeChange(Composition::CLIENT))
Lloyd Pique66d68602019-02-13 14:23:31 -0800745 .Times(1);
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500746 EXPECT_CALL(*mLayer2.outputLayer, applyDeviceCompositionTypeChange(Composition::DEVICE))
Lloyd Pique66d68602019-02-13 14:23:31 -0800747 .Times(1);
748
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700749 mDisplay->applyChangedTypesToLayers(impl::Display::ChangedTypes{
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500750 {&mLayer1.hwc2Layer, Composition::CLIENT},
751 {&mLayer2.hwc2Layer, Composition::DEVICE},
752 {&hwc2LayerUnknown, Composition::SOLID_COLOR},
Lloyd Pique66d68602019-02-13 14:23:31 -0800753 });
754}
755
756/*
757 * Display::applyDisplayRequests()
758 */
759
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700760using DisplayApplyDisplayRequestsTest = DisplayWithLayersTestCommon;
761
762TEST_F(DisplayApplyDisplayRequestsTest, handlesNoRequests) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700763 mDisplay->applyDisplayRequests(static_cast<hal::DisplayRequest>(0));
Lloyd Pique66d68602019-02-13 14:23:31 -0800764
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700765 auto& state = mDisplay->getState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800766 EXPECT_FALSE(state.flipClientTarget);
767}
768
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700769TEST_F(DisplayApplyDisplayRequestsTest, handlesFlipClientTarget) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700770 mDisplay->applyDisplayRequests(hal::DisplayRequest::FLIP_CLIENT_TARGET);
Lloyd Pique66d68602019-02-13 14:23:31 -0800771
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700772 auto& state = mDisplay->getState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800773 EXPECT_TRUE(state.flipClientTarget);
774}
775
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700776TEST_F(DisplayApplyDisplayRequestsTest, handlesWriteClientTargetToOutput) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700777 mDisplay->applyDisplayRequests(hal::DisplayRequest::WRITE_CLIENT_TARGET_TO_OUTPUT);
Lloyd Pique66d68602019-02-13 14:23:31 -0800778
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700779 auto& state = mDisplay->getState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800780 EXPECT_FALSE(state.flipClientTarget);
781}
782
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700783TEST_F(DisplayApplyDisplayRequestsTest, handlesAllRequestFlagsSet) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700784 mDisplay->applyDisplayRequests(static_cast<hal::DisplayRequest>(~0));
Lloyd Pique66d68602019-02-13 14:23:31 -0800785
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700786 auto& state = mDisplay->getState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800787 EXPECT_TRUE(state.flipClientTarget);
788}
789
790/*
791 * Display::applyLayerRequestsToLayers()
792 */
793
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700794using DisplayApplyLayerRequestsToLayersTest = DisplayWithLayersTestCommon;
795
796TEST_F(DisplayApplyLayerRequestsToLayersTest, preparesAllLayers) {
Lloyd Piquede196652020-01-22 17:29:58 -0800797 EXPECT_CALL(*mLayer1.outputLayer, prepareForDeviceLayerRequests()).Times(1);
798 EXPECT_CALL(*mLayer2.outputLayer, prepareForDeviceLayerRequests()).Times(1);
799 EXPECT_CALL(*mLayer3.outputLayer, prepareForDeviceLayerRequests()).Times(1);
Lloyd Pique66d68602019-02-13 14:23:31 -0800800
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700801 mDisplay->applyLayerRequestsToLayers(impl::Display::LayerRequests());
Lloyd Pique66d68602019-02-13 14:23:31 -0800802}
803
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700804TEST_F(DisplayApplyLayerRequestsToLayersTest, appliesDeviceLayerRequests) {
Lloyd Piquede196652020-01-22 17:29:58 -0800805 EXPECT_CALL(*mLayer1.outputLayer, prepareForDeviceLayerRequests()).Times(1);
806 EXPECT_CALL(*mLayer2.outputLayer, prepareForDeviceLayerRequests()).Times(1);
807 EXPECT_CALL(*mLayer3.outputLayer, prepareForDeviceLayerRequests()).Times(1);
Lloyd Pique66d68602019-02-13 14:23:31 -0800808
Lloyd Piquede196652020-01-22 17:29:58 -0800809 EXPECT_CALL(*mLayer1.outputLayer,
Lloyd Pique66d68602019-02-13 14:23:31 -0800810 applyDeviceLayerRequest(Hwc2::IComposerClient::LayerRequest::CLEAR_CLIENT_TARGET))
811 .Times(1);
812
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700813 mDisplay->applyLayerRequestsToLayers(impl::Display::LayerRequests{
Peiyong Line9d809e2020-04-14 13:10:48 -0700814 {&mLayer1.hwc2Layer, hal::LayerRequest::CLEAR_CLIENT_TARGET},
815 {&hwc2LayerUnknown, hal::LayerRequest::CLEAR_CLIENT_TARGET},
Lloyd Pique66d68602019-02-13 14:23:31 -0800816 });
817}
818
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800819/*
Ady Abraham0094dc62021-06-03 10:08:33 -0700820 * Display::applyClientTargetRequests()
821 */
822
823using DisplayApplyClientTargetRequests = DisplayWithLayersTestCommon;
824
825TEST_F(DisplayApplyLayerRequestsToLayersTest, applyClientTargetRequests) {
826 Display::ClientTargetProperty clientTargetProperty = {
827 .pixelFormat = hal::PixelFormat::RGB_565,
828 .dataspace = hal::Dataspace::STANDARD_BT470M,
829 };
830
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700831 static constexpr float kWhitePointNits = 800.f;
832
Ady Abraham0094dc62021-06-03 10:08:33 -0700833 mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
834 mDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
835
836 EXPECT_CALL(*renderSurface, setBufferPixelFormat(clientTargetProperty.pixelFormat));
837 EXPECT_CALL(*renderSurface, setBufferDataspace(clientTargetProperty.dataspace));
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700838 mDisplay->applyClientTargetRequests(clientTargetProperty, kWhitePointNits);
Ady Abraham0094dc62021-06-03 10:08:33 -0700839
840 auto& state = mDisplay->getState();
841 EXPECT_EQ(clientTargetProperty.dataspace, state.dataspace);
Alec Mourif8d093d2022-02-10 15:16:59 -0800842 EXPECT_EQ(kWhitePointNits, state.clientTargetBrightness);
Ady Abraham0094dc62021-06-03 10:08:33 -0700843}
844
845/*
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800846 * Display::presentAndGetFrameFences()
847 */
848
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700849using DisplayPresentAndGetFrameFencesTest = DisplayWithLayersTestCommon;
850
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800851TEST_F(DisplayPresentAndGetFrameFencesTest, returnsNoFencesOnGpuDisplay) {
852 auto args = getDisplayCreationArgsForGpuVirtualDisplay();
853 auto gpuDisplay{impl::createDisplay(mCompositionEngine, args)};
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800854
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800855 auto result = gpuDisplay->presentAndGetFrameFences();
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800856
857 ASSERT_TRUE(result.presentFence.get());
858 EXPECT_FALSE(result.presentFence->isValid());
859 EXPECT_EQ(0u, result.layerFences.size());
860}
861
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700862TEST_F(DisplayPresentAndGetFrameFencesTest, returnsPresentAndLayerFences) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800863 sp<Fence> presentFence = new Fence();
864 sp<Fence> layer1Fence = new Fence();
865 sp<Fence> layer2Fence = new Fence();
866
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700867 EXPECT_CALL(mHwComposer, presentAndGetReleaseFences(HalDisplayId(DEFAULT_DISPLAY_ID), _, _))
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700868 .Times(1);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200869 EXPECT_CALL(mHwComposer, getPresentFence(HalDisplayId(DEFAULT_DISPLAY_ID)))
870 .WillOnce(Return(presentFence));
871 EXPECT_CALL(mHwComposer,
872 getLayerReleaseFence(HalDisplayId(DEFAULT_DISPLAY_ID), &mLayer1.hwc2Layer))
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800873 .WillOnce(Return(layer1Fence));
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200874 EXPECT_CALL(mHwComposer,
875 getLayerReleaseFence(HalDisplayId(DEFAULT_DISPLAY_ID), &mLayer2.hwc2Layer))
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800876 .WillOnce(Return(layer2Fence));
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200877 EXPECT_CALL(mHwComposer, clearReleaseFences(HalDisplayId(DEFAULT_DISPLAY_ID))).Times(1);
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800878
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700879 auto result = mDisplay->presentAndGetFrameFences();
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800880
881 EXPECT_EQ(presentFence, result.presentFence);
882
883 EXPECT_EQ(2u, result.layerFences.size());
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800884 ASSERT_EQ(1u, result.layerFences.count(&mLayer1.hwc2Layer));
Lloyd Piquede196652020-01-22 17:29:58 -0800885 EXPECT_EQ(layer1Fence, result.layerFences[&mLayer1.hwc2Layer]);
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800886 ASSERT_EQ(1u, result.layerFences.count(&mLayer2.hwc2Layer));
Lloyd Piquede196652020-01-22 17:29:58 -0800887 EXPECT_EQ(layer2Fence, result.layerFences[&mLayer2.hwc2Layer]);
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800888}
889
Lloyd Pique688abd42019-02-15 15:42:24 -0800890/*
891 * Display::setExpensiveRenderingExpected()
892 */
893
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700894using DisplaySetExpensiveRenderingExpectedTest = DisplayWithLayersTestCommon;
895
896TEST_F(DisplaySetExpensiveRenderingExpectedTest, forwardsToPowerAdvisor) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800897 EXPECT_CALL(mPowerAdvisor, setExpensiveRenderingExpected(DEFAULT_DISPLAY_ID, true)).Times(1);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700898 mDisplay->setExpensiveRenderingExpected(true);
Lloyd Pique688abd42019-02-15 15:42:24 -0800899
900 EXPECT_CALL(mPowerAdvisor, setExpensiveRenderingExpected(DEFAULT_DISPLAY_ID, false)).Times(1);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700901 mDisplay->setExpensiveRenderingExpected(false);
Lloyd Pique688abd42019-02-15 15:42:24 -0800902}
903
Lloyd Piqued3d69882019-02-28 16:03:46 -0800904/*
905 * Display::finishFrame()
906 */
907
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700908using DisplayFinishFrameTest = DisplayWithLayersTestCommon;
909
910TEST_F(DisplayFinishFrameTest, doesNotSkipCompositionIfNotDirtyOnHwcDisplay) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800911 mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700912 mDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
Lloyd Piqued3d69882019-02-28 16:03:46 -0800913
914 // We expect no calls to queueBuffer if composition was skipped.
915 EXPECT_CALL(*renderSurface, queueBuffer(_)).Times(1);
916
Lloyd Piquea76ce462020-01-14 13:06:37 -0800917 // Expect a call to signal no expensive rendering since there is no client composition.
918 EXPECT_CALL(mPowerAdvisor, setExpensiveRenderingExpected(DEFAULT_DISPLAY_ID, false));
919
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700920 mDisplay->editState().isEnabled = true;
921 mDisplay->editState().usesClientComposition = false;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000922 mDisplay->editState().layerStackSpace.setContent(Rect(0, 0, 1, 1));
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700923 mDisplay->editState().dirtyRegion = Region::INVALID_REGION;
Lloyd Piqued3d69882019-02-28 16:03:46 -0800924
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700925 mDisplay->finishFrame({});
Lloyd Piqued3d69882019-02-28 16:03:46 -0800926}
927
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700928TEST_F(DisplayFinishFrameTest, skipsCompositionIfNotDirty) {
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800929 auto args = getDisplayCreationArgsForGpuVirtualDisplay();
930 std::shared_ptr<impl::Display> gpuDisplay = impl::createDisplay(mCompositionEngine, args);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800931
932 mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800933 gpuDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
Lloyd Piqued3d69882019-02-28 16:03:46 -0800934
935 // We expect no calls to queueBuffer if composition was skipped.
936 EXPECT_CALL(*renderSurface, queueBuffer(_)).Times(0);
937
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800938 gpuDisplay->editState().isEnabled = true;
939 gpuDisplay->editState().usesClientComposition = false;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000940 gpuDisplay->editState().layerStackSpace.setContent(Rect(0, 0, 1, 1));
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800941 gpuDisplay->editState().dirtyRegion = Region::INVALID_REGION;
Lloyd Piqued3d69882019-02-28 16:03:46 -0800942
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700943 gpuDisplay->finishFrame({});
Lloyd Piqued3d69882019-02-28 16:03:46 -0800944}
945
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700946TEST_F(DisplayFinishFrameTest, performsCompositionIfDirty) {
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800947 auto args = getDisplayCreationArgsForGpuVirtualDisplay();
948 std::shared_ptr<impl::Display> gpuDisplay = impl::createDisplay(mCompositionEngine, args);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800949
950 mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800951 gpuDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
Lloyd Piqued3d69882019-02-28 16:03:46 -0800952
953 // We expect a single call to queueBuffer when composition is not skipped.
954 EXPECT_CALL(*renderSurface, queueBuffer(_)).Times(1);
955
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800956 gpuDisplay->editState().isEnabled = true;
957 gpuDisplay->editState().usesClientComposition = false;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000958 gpuDisplay->editState().layerStackSpace.setContent(Rect(0, 0, 1, 1));
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800959 gpuDisplay->editState().dirtyRegion = Region(Rect(0, 0, 1, 1));
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700960 gpuDisplay->finishFrame({});
Lloyd Piqued3d69882019-02-28 16:03:46 -0800961}
962
Lloyd Piquec6607552019-12-02 17:57:39 -0800963/*
964 * Display functional tests
965 */
966
967struct DisplayFunctionalTest : public testing::Test {
968 class Display : public impl::Display {
969 public:
Lloyd Piquec6607552019-12-02 17:57:39 -0800970 using impl::Display::injectOutputLayerForTest;
971 virtual void injectOutputLayerForTest(std::unique_ptr<compositionengine::OutputLayer>) = 0;
972 };
973
Lloyd Piquec6607552019-12-02 17:57:39 -0800974 DisplayFunctionalTest() {
975 EXPECT_CALL(mCompositionEngine, getHwComposer()).WillRepeatedly(ReturnRef(mHwComposer));
Kriti Dang646f8ec2022-01-18 14:35:02 +0100976 mDisplay = createDisplay();
977 mRenderSurface = createRenderSurface();
Lloyd Piquec6607552019-12-02 17:57:39 -0800978 mDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(mRenderSurface));
979 }
980
981 NiceMock<android::mock::HWComposer> mHwComposer;
982 NiceMock<Hwc2::mock::PowerAdvisor> mPowerAdvisor;
983 NiceMock<mock::CompositionEngine> mCompositionEngine;
984 sp<mock::NativeWindow> mNativeWindow = new NiceMock<mock::NativeWindow>();
985 sp<mock::DisplaySurface> mDisplaySurface = new NiceMock<mock::DisplaySurface>();
Kriti Dang646f8ec2022-01-18 14:35:02 +0100986 std::shared_ptr<Display> mDisplay;
987 impl::RenderSurface* mRenderSurface;
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800988
Kriti Dang646f8ec2022-01-18 14:35:02 +0100989 std::shared_ptr<Display> createDisplay() {
990 return impl::createDisplayTemplated<Display>(mCompositionEngine,
991 DisplayCreationArgsBuilder()
992 .setId(DEFAULT_DISPLAY_ID)
993 .setPixels(DEFAULT_RESOLUTION)
994 .setIsSecure(true)
995 .setPowerAdvisor(&mPowerAdvisor)
996 .build());
997 ;
998 }
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -0700999
Kriti Dang646f8ec2022-01-18 14:35:02 +01001000 impl::RenderSurface* createRenderSurface() {
1001 return new impl::RenderSurface{mCompositionEngine, *mDisplay,
1002 RenderSurfaceCreationArgsBuilder()
1003 .setDisplayWidth(DEFAULT_RESOLUTION.width)
1004 .setDisplayHeight(DEFAULT_RESOLUTION.height)
1005 .setNativeWindow(mNativeWindow)
1006 .setDisplaySurface(mDisplaySurface)
1007 .build()};
1008 }
Lloyd Piquec6607552019-12-02 17:57:39 -08001009};
1010
1011TEST_F(DisplayFunctionalTest, postFramebufferCriticalCallsAreOrdered) {
1012 InSequence seq;
1013
1014 mDisplay->editState().isEnabled = true;
1015
Ady Abrahamec7aa8a2021-06-28 12:37:09 -07001016 EXPECT_CALL(mHwComposer, presentAndGetReleaseFences(_, _, _));
Lloyd Piquec6607552019-12-02 17:57:39 -08001017 EXPECT_CALL(*mDisplaySurface, onFrameCommitted());
1018
1019 mDisplay->postFramebuffer();
1020}
1021
Lloyd Pique45a165a2018-10-19 11:54:47 -07001022} // namespace
1023} // namespace android::compositionengine