blob: 463d095b8c2de13ff888704bccfa00d4f7af31b0 [file] [log] [blame]
Lloyd Pique32cbe282018-10-19 13:09:22 -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
17#include <cmath>
18
Lloyd Pique17ca7422019-11-14 14:24:10 -080019#include <android-base/stringprintf.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070020#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070021#include <compositionengine/impl/Output.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080022#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Pique56eba802019-08-28 15:45:25 -070023#include <compositionengine/impl/OutputLayerCompositionState.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070024#include <compositionengine/mock/CompositionEngine.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070025#include <compositionengine/mock/DisplayColorProfile.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080026#include <compositionengine/mock/Layer.h>
27#include <compositionengine/mock/LayerFE.h>
28#include <compositionengine/mock/OutputLayer.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070029#include <compositionengine/mock/RenderSurface.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070030#include <gtest/gtest.h>
Lloyd Pique56eba802019-08-28 15:45:25 -070031#include <renderengine/mock/RenderEngine.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070032#include <ui/Rect.h>
33#include <ui/Region.h>
34
Lloyd Pique17ca7422019-11-14 14:24:10 -080035#include "CallOrderStateMachineHelper.h"
Lloyd Pique07178e32019-11-19 19:15:26 -080036#include "MockHWC2.h"
Lloyd Pique32cbe282018-10-19 13:09:22 -070037#include "RegionMatcher.h"
Lloyd Pique32cbe282018-10-19 13:09:22 -070038
39namespace android::compositionengine {
40namespace {
41
Lloyd Pique56eba802019-08-28 15:45:25 -070042using testing::_;
Lloyd Pique03561a62019-11-19 18:34:52 -080043using testing::ByMove;
Lloyd Piquea4863342019-12-04 18:45:02 -080044using testing::ByRef;
Lloyd Pique17ca7422019-11-14 14:24:10 -080045using testing::DoAll;
Vishnu Nair9b079a22020-01-21 14:36:08 -080046using testing::ElementsAre;
Lloyd Pique6818fa52019-12-03 12:32:13 -080047using testing::ElementsAreArray;
Lloyd Pique07178e32019-11-19 19:15:26 -080048using testing::Eq;
Lloyd Piquefaa3f192019-11-14 14:05:09 -080049using testing::InSequence;
Lloyd Pique6818fa52019-12-03 12:32:13 -080050using testing::Invoke;
51using testing::IsEmpty;
Lloyd Pique17ca7422019-11-14 14:24:10 -080052using testing::Mock;
Vishnu Nair9b079a22020-01-21 14:36:08 -080053using testing::Pointee;
Lloyd Pique07178e32019-11-19 19:15:26 -080054using testing::Property;
Lloyd Piquefaa3f192019-11-14 14:05:09 -080055using testing::Ref;
Lloyd Pique31cb2942018-10-19 17:23:03 -070056using testing::Return;
Lloyd Pique32cbe282018-10-19 13:09:22 -070057using testing::ReturnRef;
Lloyd Pique17ca7422019-11-14 14:24:10 -080058using testing::SetArgPointee;
Lloyd Pique32cbe282018-10-19 13:09:22 -070059using testing::StrictMock;
60
Lloyd Pique56eba802019-08-28 15:45:25 -070061constexpr auto TR_IDENT = 0u;
62constexpr auto TR_ROT_90 = HAL_TRANSFORM_ROT_90;
Vishnu Nair9b079a22020-01-21 14:36:08 -080063constexpr auto MAX_CLIENT_COMPOSITION_CACHE_SIZE = 3;
Lloyd Pique56eba802019-08-28 15:45:25 -070064
Lloyd Pique3eb1b212019-03-07 21:15:40 -080065const mat4 kIdentity;
Lloyd Pique0a456232020-01-16 17:51:13 -080066const mat4 kNonIdentityHalf = mat4() * 0.5f;
67const mat4 kNonIdentityQuarter = mat4() * 0.25f;
Lloyd Pique3eb1b212019-03-07 21:15:40 -080068
Lloyd Pique17ca7422019-11-14 14:24:10 -080069constexpr OutputColorSetting kVendorSpecifiedOutputColorSetting =
70 static_cast<OutputColorSetting>(0x100);
71
Lloyd Piquefaa3f192019-11-14 14:05:09 -080072struct OutputPartialMockBase : public impl::Output {
73 // compositionengine::Output overrides
74 const OutputCompositionState& getState() const override { return mState; }
75 OutputCompositionState& editState() override { return mState; }
76
77 // Use mocks for all the remaining virtual functions
78 // not implemented by the base implementation class.
79 MOCK_CONST_METHOD0(getOutputLayerCount, size_t());
80 MOCK_CONST_METHOD1(getOutputLayerOrderedByZByIndex, compositionengine::OutputLayer*(size_t));
81 MOCK_METHOD3(ensureOutputLayer,
82 compositionengine::OutputLayer*(std::optional<size_t>,
83 const std::shared_ptr<compositionengine::Layer>&,
84 const sp<LayerFE>&));
85 MOCK_METHOD0(finalizePendingOutputLayers, void());
86 MOCK_METHOD0(clearOutputLayers, void());
87 MOCK_CONST_METHOD1(dumpState, void(std::string&));
88 MOCK_CONST_METHOD0(getCompositionEngine, const CompositionEngine&());
89 MOCK_METHOD2(injectOutputLayerForTest,
90 compositionengine::OutputLayer*(const std::shared_ptr<compositionengine::Layer>&,
91 const sp<LayerFE>&));
92 MOCK_METHOD1(injectOutputLayerForTest, void(std::unique_ptr<OutputLayer>));
93
94 impl::OutputCompositionState mState;
95};
96
Lloyd Pique66d68602019-02-13 14:23:31 -080097struct OutputTest : public testing::Test {
Lloyd Pique01c77c12019-04-17 12:48:32 -070098 class Output : public impl::Output {
99 public:
100 using impl::Output::injectOutputLayerForTest;
101 virtual void injectOutputLayerForTest(std::unique_ptr<compositionengine::OutputLayer>) = 0;
102 };
103
104 static std::shared_ptr<Output> createOutput(
105 const compositionengine::CompositionEngine& compositionEngine) {
106 return impl::createOutputTemplated<Output>(compositionEngine);
107 }
108
Lloyd Pique31cb2942018-10-19 17:23:03 -0700109 OutputTest() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700110 mOutput->setDisplayColorProfileForTest(
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700111 std::unique_ptr<DisplayColorProfile>(mDisplayColorProfile));
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700112 mOutput->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(mRenderSurface));
Lloyd Piqueef958122019-02-05 18:00:12 -0800113
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700114 mOutput->editState().bounds = kDefaultDisplaySize;
Lloyd Pique31cb2942018-10-19 17:23:03 -0700115 }
Lloyd Pique32cbe282018-10-19 13:09:22 -0700116
Lloyd Piqueef958122019-02-05 18:00:12 -0800117 static const Rect kDefaultDisplaySize;
118
Lloyd Pique32cbe282018-10-19 13:09:22 -0700119 StrictMock<mock::CompositionEngine> mCompositionEngine;
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700120 mock::DisplayColorProfile* mDisplayColorProfile = new StrictMock<mock::DisplayColorProfile>();
Lloyd Pique31cb2942018-10-19 17:23:03 -0700121 mock::RenderSurface* mRenderSurface = new StrictMock<mock::RenderSurface>();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700122 std::shared_ptr<Output> mOutput = createOutput(mCompositionEngine);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700123};
124
Lloyd Piquec0ee6ba2019-11-14 12:55:53 -0800125// Extension of the base test useful for checking interactions with the LayerFE
126// functions to latch composition state.
127struct OutputLatchFEStateTest : public OutputTest {
128 OutputLatchFEStateTest() {
129 EXPECT_CALL(*mOutputLayer1, getLayer()).WillRepeatedly(ReturnRef(mLayer1));
130 EXPECT_CALL(*mOutputLayer2, getLayer()).WillRepeatedly(ReturnRef(mLayer2));
131 EXPECT_CALL(*mOutputLayer3, getLayer()).WillRepeatedly(ReturnRef(mLayer3));
132
133 EXPECT_CALL(*mOutputLayer1, getLayerFE()).WillRepeatedly(ReturnRef(mLayer1FE));
134 EXPECT_CALL(*mOutputLayer2, getLayerFE()).WillRepeatedly(ReturnRef(mLayer2FE));
135 EXPECT_CALL(*mOutputLayer3, getLayerFE()).WillRepeatedly(ReturnRef(mLayer3FE));
136
137 EXPECT_CALL(mLayer1, editFEState()).WillRepeatedly(ReturnRef(mLayer1FEState));
138 EXPECT_CALL(mLayer2, editFEState()).WillRepeatedly(ReturnRef(mLayer2FEState));
139 EXPECT_CALL(mLayer3, editFEState()).WillRepeatedly(ReturnRef(mLayer3FEState));
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800140
141 EXPECT_CALL(mLayer1, getFEState()).WillRepeatedly(ReturnRef(mLayer1FEState));
142 EXPECT_CALL(mLayer2, getFEState()).WillRepeatedly(ReturnRef(mLayer2FEState));
143 EXPECT_CALL(mLayer3, getFEState()).WillRepeatedly(ReturnRef(mLayer3FEState));
Lloyd Piquec0ee6ba2019-11-14 12:55:53 -0800144 }
145
146 void injectLayer(std::unique_ptr<mock::OutputLayer> layer) {
147 mOutput->injectOutputLayerForTest(std::unique_ptr<OutputLayer>(layer.release()));
148 }
149
150 std::unique_ptr<mock::OutputLayer> mOutputLayer1{new StrictMock<mock::OutputLayer>};
151 std::unique_ptr<mock::OutputLayer> mOutputLayer2{new StrictMock<mock::OutputLayer>};
152 std::unique_ptr<mock::OutputLayer> mOutputLayer3{new StrictMock<mock::OutputLayer>};
153
154 StrictMock<mock::Layer> mLayer1;
155 StrictMock<mock::Layer> mLayer2;
156 StrictMock<mock::Layer> mLayer3;
157
158 StrictMock<mock::LayerFE> mLayer1FE;
159 StrictMock<mock::LayerFE> mLayer2FE;
160 StrictMock<mock::LayerFE> mLayer3FE;
161
162 LayerFECompositionState mLayer1FEState;
163 LayerFECompositionState mLayer2FEState;
164 LayerFECompositionState mLayer3FEState;
165};
166
Lloyd Piqueef958122019-02-05 18:00:12 -0800167const Rect OutputTest::kDefaultDisplaySize{100, 200};
168
Lloyd Pique17ca7422019-11-14 14:24:10 -0800169using ColorProfile = compositionengine::Output::ColorProfile;
170
171void dumpColorProfile(ColorProfile profile, std::string& result, const char* name) {
172 android::base::StringAppendF(&result, "%s (%s[%d] %s[%d] %s[%d] %s[%d]) ", name,
173 toString(profile.mode).c_str(), profile.mode,
174 toString(profile.dataspace).c_str(), profile.dataspace,
175 toString(profile.renderIntent).c_str(), profile.renderIntent,
176 toString(profile.colorSpaceAgnosticDataspace).c_str(),
177 profile.colorSpaceAgnosticDataspace);
178}
179
180// Checks for a ColorProfile match
181MATCHER_P(ColorProfileEq, expected, "") {
182 std::string buf;
183 buf.append("ColorProfiles are not equal\n");
184 dumpColorProfile(expected, buf, "expected value");
185 dumpColorProfile(arg, buf, "actual value");
186 *result_listener << buf;
187
188 return (expected.mode == arg.mode) && (expected.dataspace == arg.dataspace) &&
189 (expected.renderIntent == arg.renderIntent) &&
190 (expected.colorSpaceAgnosticDataspace == arg.colorSpaceAgnosticDataspace);
191}
192
Lloyd Pique66d68602019-02-13 14:23:31 -0800193/*
Lloyd Pique32cbe282018-10-19 13:09:22 -0700194 * Basic construction
195 */
196
Lloyd Pique31cb2942018-10-19 17:23:03 -0700197TEST_F(OutputTest, canInstantiateOutput) {
198 // The validation check checks each required component.
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700199 EXPECT_CALL(*mDisplayColorProfile, isValid()).WillOnce(Return(true));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700200 EXPECT_CALL(*mRenderSurface, isValid()).WillOnce(Return(true));
201
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700202 EXPECT_TRUE(mOutput->isValid());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700203
204 // If we take away the required components, it is no longer valid.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700205 mOutput->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700206
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700207 EXPECT_CALL(*mDisplayColorProfile, isValid()).WillOnce(Return(true));
208
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700209 EXPECT_FALSE(mOutput->isValid());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700210}
Lloyd Pique32cbe282018-10-19 13:09:22 -0700211
Lloyd Pique66d68602019-02-13 14:23:31 -0800212/*
Lloyd Pique32cbe282018-10-19 13:09:22 -0700213 * Output::setCompositionEnabled()
214 */
215
216TEST_F(OutputTest, setCompositionEnabledDoesNothingIfAlreadyEnabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700217 mOutput->editState().isEnabled = true;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700218
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700219 mOutput->setCompositionEnabled(true);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700220
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700221 EXPECT_TRUE(mOutput->getState().isEnabled);
222 EXPECT_THAT(mOutput->getState().dirtyRegion, RegionEq(Region()));
Lloyd Pique32cbe282018-10-19 13:09:22 -0700223}
224
225TEST_F(OutputTest, setCompositionEnabledSetsEnabledAndDirtiesEntireOutput) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700226 mOutput->editState().isEnabled = false;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700227
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700228 mOutput->setCompositionEnabled(true);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700229
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700230 EXPECT_TRUE(mOutput->getState().isEnabled);
231 EXPECT_THAT(mOutput->getState().dirtyRegion, RegionEq(Region(kDefaultDisplaySize)));
Lloyd Pique32cbe282018-10-19 13:09:22 -0700232}
233
234TEST_F(OutputTest, setCompositionEnabledSetsDisabledAndDirtiesEntireOutput) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700235 mOutput->editState().isEnabled = true;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700236
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700237 mOutput->setCompositionEnabled(false);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700238
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700239 EXPECT_FALSE(mOutput->getState().isEnabled);
240 EXPECT_THAT(mOutput->getState().dirtyRegion, RegionEq(Region(kDefaultDisplaySize)));
Lloyd Pique32cbe282018-10-19 13:09:22 -0700241}
242
Lloyd Pique66d68602019-02-13 14:23:31 -0800243/*
Lloyd Pique32cbe282018-10-19 13:09:22 -0700244 * Output::setProjection()
245 */
246
247TEST_F(OutputTest, setProjectionTriviallyWorks) {
248 const ui::Transform transform{ui::Transform::ROT_180};
249 const int32_t orientation = 123;
250 const Rect frame{1, 2, 3, 4};
251 const Rect viewport{5, 6, 7, 8};
Lloyd Piquee8fe4742020-01-21 15:26:18 -0800252 const Rect sourceClip{9, 10, 11, 12};
253 const Rect destinationClip{13, 14, 15, 16};
Lloyd Pique32cbe282018-10-19 13:09:22 -0700254 const bool needsFiltering = true;
255
Lloyd Piquee8fe4742020-01-21 15:26:18 -0800256 mOutput->setProjection(transform, orientation, frame, viewport, sourceClip, destinationClip,
257 needsFiltering);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700258
Lloyd Piqueea629282019-12-03 15:57:10 -0800259 EXPECT_THAT(mOutput->getState().transform, transform);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700260 EXPECT_EQ(orientation, mOutput->getState().orientation);
261 EXPECT_EQ(frame, mOutput->getState().frame);
262 EXPECT_EQ(viewport, mOutput->getState().viewport);
Lloyd Piquee8fe4742020-01-21 15:26:18 -0800263 EXPECT_EQ(sourceClip, mOutput->getState().sourceClip);
264 EXPECT_EQ(destinationClip, mOutput->getState().destinationClip);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700265 EXPECT_EQ(needsFiltering, mOutput->getState().needsFiltering);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700266}
267
Lloyd Pique66d68602019-02-13 14:23:31 -0800268/*
Lloyd Pique32cbe282018-10-19 13:09:22 -0700269 * Output::setBounds()
270 */
271
272TEST_F(OutputTest, setBoundsSetsSizeAndDirtiesEntireOutput) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800273 const ui::Size displaySize{200, 400};
Lloyd Pique31cb2942018-10-19 17:23:03 -0700274
275 EXPECT_CALL(*mRenderSurface, setDisplaySize(displaySize)).Times(1);
276 EXPECT_CALL(*mRenderSurface, getSize()).WillOnce(ReturnRef(displaySize));
277
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700278 mOutput->setBounds(displaySize);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700279
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700280 EXPECT_EQ(Rect(displaySize), mOutput->getState().bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700281
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700282 EXPECT_THAT(mOutput->getState().dirtyRegion, RegionEq(Region(Rect(displaySize))));
Lloyd Pique32cbe282018-10-19 13:09:22 -0700283}
284
Lloyd Pique66d68602019-02-13 14:23:31 -0800285/*
Lloyd Pique32cbe282018-10-19 13:09:22 -0700286 * Output::setLayerStackFilter()
287 */
288
289TEST_F(OutputTest, setLayerStackFilterSetsFilterAndDirtiesEntireOutput) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700290 const uint32_t layerStack = 123u;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700291 mOutput->setLayerStackFilter(layerStack, true);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700292
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700293 EXPECT_TRUE(mOutput->getState().layerStackInternal);
294 EXPECT_EQ(layerStack, mOutput->getState().layerStackId);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700295
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700296 EXPECT_THAT(mOutput->getState().dirtyRegion, RegionEq(Region(kDefaultDisplaySize)));
Lloyd Pique32cbe282018-10-19 13:09:22 -0700297}
298
Lloyd Pique66d68602019-02-13 14:23:31 -0800299/*
Lloyd Pique32cbe282018-10-19 13:09:22 -0700300 * Output::setColorTransform
301 */
302
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800303TEST_F(OutputTest, setColorTransformWithNoChangeFlaggedSkipsUpdates) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700304 mOutput->editState().colorTransformMatrix = kIdentity;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700305
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800306 // If no colorTransformMatrix is set the update should be skipped.
307 CompositionRefreshArgs refreshArgs;
308 refreshArgs.colorTransformMatrix = std::nullopt;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700309
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700310 mOutput->setColorTransform(refreshArgs);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700311
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800312 // The internal state should be unchanged
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700313 EXPECT_EQ(kIdentity, mOutput->getState().colorTransformMatrix);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800314
315 // No dirty region should be set
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700316 EXPECT_THAT(mOutput->getState().dirtyRegion, RegionEq(Region()));
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800317}
Lloyd Piqueef958122019-02-05 18:00:12 -0800318
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800319TEST_F(OutputTest, setColorTransformWithNoActualChangeSkipsUpdates) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700320 mOutput->editState().colorTransformMatrix = kIdentity;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700321
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800322 // Attempting to set the same colorTransformMatrix that is already set should
323 // also skip the update.
324 CompositionRefreshArgs refreshArgs;
325 refreshArgs.colorTransformMatrix = kIdentity;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700326
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700327 mOutput->setColorTransform(refreshArgs);
Lloyd Pique77f79a22019-04-29 15:55:40 -0700328
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800329 // The internal state should be unchanged
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700330 EXPECT_EQ(kIdentity, mOutput->getState().colorTransformMatrix);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800331
332 // No dirty region should be set
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700333 EXPECT_THAT(mOutput->getState().dirtyRegion, RegionEq(Region()));
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800334}
335
336TEST_F(OutputTest, setColorTransformPerformsUpdateToIdentity) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700337 mOutput->editState().colorTransformMatrix = kNonIdentityHalf;
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800338
339 // Setting a different colorTransformMatrix should perform the update.
340 CompositionRefreshArgs refreshArgs;
341 refreshArgs.colorTransformMatrix = kIdentity;
342
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700343 mOutput->setColorTransform(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800344
345 // The internal state should have been updated
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700346 EXPECT_EQ(kIdentity, mOutput->getState().colorTransformMatrix);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800347
348 // The dirtyRegion should be set to the full display size
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700349 EXPECT_THAT(mOutput->getState().dirtyRegion, RegionEq(Region(kDefaultDisplaySize)));
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800350}
Lloyd Pique77f79a22019-04-29 15:55:40 -0700351
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800352TEST_F(OutputTest, setColorTransformPerformsUpdateForIdentityToHalf) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700353 mOutput->editState().colorTransformMatrix = kIdentity;
Lloyd Pique77f79a22019-04-29 15:55:40 -0700354
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800355 // Setting a different colorTransformMatrix should perform the update.
356 CompositionRefreshArgs refreshArgs;
357 refreshArgs.colorTransformMatrix = kNonIdentityHalf;
Lloyd Pique77f79a22019-04-29 15:55:40 -0700358
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700359 mOutput->setColorTransform(refreshArgs);
Lloyd Piqueef958122019-02-05 18:00:12 -0800360
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800361 // The internal state should have been updated
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700362 EXPECT_EQ(kNonIdentityHalf, mOutput->getState().colorTransformMatrix);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800363
364 // The dirtyRegion should be set to the full display size
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700365 EXPECT_THAT(mOutput->getState().dirtyRegion, RegionEq(Region(kDefaultDisplaySize)));
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800366}
367
368TEST_F(OutputTest, setColorTransformPerformsUpdateForHalfToQuarter) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700369 mOutput->editState().colorTransformMatrix = kNonIdentityHalf;
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800370
371 // Setting a different colorTransformMatrix should perform the update.
372 CompositionRefreshArgs refreshArgs;
373 refreshArgs.colorTransformMatrix = kNonIdentityQuarter;
374
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700375 mOutput->setColorTransform(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800376
377 // The internal state should have been updated
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700378 EXPECT_EQ(kNonIdentityQuarter, mOutput->getState().colorTransformMatrix);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800379
380 // The dirtyRegion should be set to the full display size
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700381 EXPECT_THAT(mOutput->getState().dirtyRegion, RegionEq(Region(kDefaultDisplaySize)));
Lloyd Pique32cbe282018-10-19 13:09:22 -0700382}
383
Lloyd Pique66d68602019-02-13 14:23:31 -0800384/*
Lloyd Pique17ca7422019-11-14 14:24:10 -0800385 * Output::setColorProfile
Lloyd Pique32cbe282018-10-19 13:09:22 -0700386 */
387
Lloyd Pique17ca7422019-11-14 14:24:10 -0800388using OutputSetColorProfileTest = OutputTest;
389
390TEST_F(OutputSetColorProfileTest, setsStateAndDirtiesOutputIfChanged) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800391 using ColorProfile = Output::ColorProfile;
392
Lloyd Piquef5275482019-01-29 18:42:42 -0800393 EXPECT_CALL(*mDisplayColorProfile,
394 getTargetDataspace(ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3,
395 ui::Dataspace::UNKNOWN))
396 .WillOnce(Return(ui::Dataspace::UNKNOWN));
Lloyd Piqueef958122019-02-05 18:00:12 -0800397 EXPECT_CALL(*mRenderSurface, setBufferDataspace(ui::Dataspace::DISPLAY_P3)).Times(1);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700398
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700399 mOutput->setColorProfile(ColorProfile{ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3,
400 ui::RenderIntent::TONE_MAP_COLORIMETRIC,
401 ui::Dataspace::UNKNOWN});
Lloyd Pique32cbe282018-10-19 13:09:22 -0700402
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700403 EXPECT_EQ(ui::ColorMode::DISPLAY_P3, mOutput->getState().colorMode);
404 EXPECT_EQ(ui::Dataspace::DISPLAY_P3, mOutput->getState().dataspace);
405 EXPECT_EQ(ui::RenderIntent::TONE_MAP_COLORIMETRIC, mOutput->getState().renderIntent);
406 EXPECT_EQ(ui::Dataspace::UNKNOWN, mOutput->getState().targetDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800407
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700408 EXPECT_THAT(mOutput->getState().dirtyRegion, RegionEq(Region(kDefaultDisplaySize)));
Lloyd Piqueef958122019-02-05 18:00:12 -0800409}
410
Lloyd Pique17ca7422019-11-14 14:24:10 -0800411TEST_F(OutputSetColorProfileTest, doesNothingIfNoChange) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800412 using ColorProfile = Output::ColorProfile;
413
Lloyd Piquef5275482019-01-29 18:42:42 -0800414 EXPECT_CALL(*mDisplayColorProfile,
415 getTargetDataspace(ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3,
416 ui::Dataspace::UNKNOWN))
417 .WillOnce(Return(ui::Dataspace::UNKNOWN));
418
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700419 mOutput->editState().colorMode = ui::ColorMode::DISPLAY_P3;
420 mOutput->editState().dataspace = ui::Dataspace::DISPLAY_P3;
421 mOutput->editState().renderIntent = ui::RenderIntent::TONE_MAP_COLORIMETRIC;
422 mOutput->editState().targetDataspace = ui::Dataspace::UNKNOWN;
Lloyd Piqueef958122019-02-05 18:00:12 -0800423
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700424 mOutput->setColorProfile(ColorProfile{ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3,
425 ui::RenderIntent::TONE_MAP_COLORIMETRIC,
426 ui::Dataspace::UNKNOWN});
Lloyd Piqueef958122019-02-05 18:00:12 -0800427
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700428 EXPECT_THAT(mOutput->getState().dirtyRegion, RegionEq(Region()));
Lloyd Pique32cbe282018-10-19 13:09:22 -0700429}
430
Lloyd Pique66d68602019-02-13 14:23:31 -0800431/*
Lloyd Pique31cb2942018-10-19 17:23:03 -0700432 * Output::setRenderSurface()
433 */
434
435TEST_F(OutputTest, setRenderSurfaceResetsBounds) {
436 const ui::Size newDisplaySize{640, 480};
437
438 mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
439 EXPECT_CALL(*renderSurface, getSize()).WillOnce(ReturnRef(newDisplaySize));
440
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700441 mOutput->setRenderSurface(std::unique_ptr<RenderSurface>(renderSurface));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700442
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700443 EXPECT_EQ(Rect(newDisplaySize), mOutput->getState().bounds);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700444}
445
Lloyd Pique66d68602019-02-13 14:23:31 -0800446/*
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000447 * Output::getDirtyRegion()
Lloyd Pique32cbe282018-10-19 13:09:22 -0700448 */
449
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000450TEST_F(OutputTest, getDirtyRegionWithRepaintEverythingTrue) {
451 const Rect viewport{100, 200};
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700452 mOutput->editState().viewport = viewport;
453 mOutput->editState().dirtyRegion.set(50, 300);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700454
455 {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700456 Region result = mOutput->getDirtyRegion(true);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700457
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000458 EXPECT_THAT(result, RegionEq(Region(viewport)));
Lloyd Pique32cbe282018-10-19 13:09:22 -0700459 }
460}
461
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000462TEST_F(OutputTest, getDirtyRegionWithRepaintEverythingFalse) {
463 const Rect viewport{100, 200};
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700464 mOutput->editState().viewport = viewport;
465 mOutput->editState().dirtyRegion.set(50, 300);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700466
467 {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700468 Region result = mOutput->getDirtyRegion(false);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700469
470 // The dirtyRegion should be clipped to the display bounds.
471 EXPECT_THAT(result, RegionEq(Region(Rect(50, 200))));
472 }
Lloyd Pique32cbe282018-10-19 13:09:22 -0700473}
474
Lloyd Pique66d68602019-02-13 14:23:31 -0800475/*
Lloyd Piqueef36b002019-01-23 17:52:04 -0800476 * Output::belongsInOutput()
477 */
478
479TEST_F(OutputTest, belongsInOutputFiltersAsExpected) {
480 const uint32_t layerStack1 = 123u;
481 const uint32_t layerStack2 = 456u;
482
483 // If the output accepts layerStack1 and internal-only layers....
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700484 mOutput->setLayerStackFilter(layerStack1, true);
Lloyd Piqueef36b002019-01-23 17:52:04 -0800485
Lloyd Piquec6687342019-03-07 21:34:57 -0800486 // A layer with no layerStack does not belong to it, internal-only or not.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700487 EXPECT_FALSE(mOutput->belongsInOutput(std::nullopt, false));
488 EXPECT_FALSE(mOutput->belongsInOutput(std::nullopt, true));
Lloyd Piquec6687342019-03-07 21:34:57 -0800489
Lloyd Piqueef36b002019-01-23 17:52:04 -0800490 // Any layer with layerStack1 belongs to it, internal-only or not.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700491 EXPECT_TRUE(mOutput->belongsInOutput(layerStack1, false));
492 EXPECT_TRUE(mOutput->belongsInOutput(layerStack1, true));
493 EXPECT_FALSE(mOutput->belongsInOutput(layerStack2, true));
494 EXPECT_FALSE(mOutput->belongsInOutput(layerStack2, false));
Lloyd Piqueef36b002019-01-23 17:52:04 -0800495
496 // If the output accepts layerStack21 but not internal-only layers...
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700497 mOutput->setLayerStackFilter(layerStack1, false);
Lloyd Piqueef36b002019-01-23 17:52:04 -0800498
499 // Only non-internal layers with layerStack1 belong to it.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700500 EXPECT_TRUE(mOutput->belongsInOutput(layerStack1, false));
501 EXPECT_FALSE(mOutput->belongsInOutput(layerStack1, true));
502 EXPECT_FALSE(mOutput->belongsInOutput(layerStack2, true));
503 EXPECT_FALSE(mOutput->belongsInOutput(layerStack2, false));
Lloyd Piqueef36b002019-01-23 17:52:04 -0800504}
505
Lloyd Pique66c20c42019-03-07 21:44:02 -0800506TEST_F(OutputTest, belongsInOutputFiltersLayersAsExpected) {
507 StrictMock<mock::Layer> layer;
Lloyd Pique9755fb72019-03-26 14:44:40 -0700508 LayerFECompositionState layerFEState;
Lloyd Pique66c20c42019-03-07 21:44:02 -0800509
Lloyd Pique9755fb72019-03-26 14:44:40 -0700510 EXPECT_CALL(layer, getFEState()).WillRepeatedly(ReturnRef(layerFEState));
Lloyd Pique66c20c42019-03-07 21:44:02 -0800511
512 const uint32_t layerStack1 = 123u;
513 const uint32_t layerStack2 = 456u;
514
515 // If the output accepts layerStack1 and internal-only layers....
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700516 mOutput->setLayerStackFilter(layerStack1, true);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800517
518 // A null layer pointer does not belong to the output
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700519 EXPECT_FALSE(mOutput->belongsInOutput(nullptr));
Lloyd Pique66c20c42019-03-07 21:44:02 -0800520
521 // A layer with no layerStack does not belong to it, internal-only or not.
Lloyd Pique9755fb72019-03-26 14:44:40 -0700522 layerFEState.layerStackId = std::nullopt;
523 layerFEState.internalOnly = false;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700524 EXPECT_FALSE(mOutput->belongsInOutput(&layer));
Lloyd Pique66c20c42019-03-07 21:44:02 -0800525
Lloyd Pique9755fb72019-03-26 14:44:40 -0700526 layerFEState.layerStackId = std::nullopt;
527 layerFEState.internalOnly = true;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700528 EXPECT_FALSE(mOutput->belongsInOutput(&layer));
Lloyd Pique66c20c42019-03-07 21:44:02 -0800529
530 // Any layer with layerStack1 belongs to it, internal-only or not.
Lloyd Pique9755fb72019-03-26 14:44:40 -0700531 layerFEState.layerStackId = layerStack1;
532 layerFEState.internalOnly = false;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700533 EXPECT_TRUE(mOutput->belongsInOutput(&layer));
Lloyd Pique66c20c42019-03-07 21:44:02 -0800534
Lloyd Pique9755fb72019-03-26 14:44:40 -0700535 layerFEState.layerStackId = layerStack1;
536 layerFEState.internalOnly = true;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700537 EXPECT_TRUE(mOutput->belongsInOutput(&layer));
Lloyd Pique66c20c42019-03-07 21:44:02 -0800538
Lloyd Pique9755fb72019-03-26 14:44:40 -0700539 layerFEState.layerStackId = layerStack2;
540 layerFEState.internalOnly = true;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700541 EXPECT_FALSE(mOutput->belongsInOutput(&layer));
Lloyd Pique66c20c42019-03-07 21:44:02 -0800542
Lloyd Pique9755fb72019-03-26 14:44:40 -0700543 layerFEState.layerStackId = layerStack2;
544 layerFEState.internalOnly = false;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700545 EXPECT_FALSE(mOutput->belongsInOutput(&layer));
Lloyd Pique66c20c42019-03-07 21:44:02 -0800546
547 // If the output accepts layerStack1 but not internal-only layers...
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700548 mOutput->setLayerStackFilter(layerStack1, false);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800549
550 // A null layer pointer does not belong to the output
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700551 EXPECT_FALSE(mOutput->belongsInOutput(nullptr));
Lloyd Pique66c20c42019-03-07 21:44:02 -0800552
553 // Only non-internal layers with layerStack1 belong to it.
Lloyd Pique9755fb72019-03-26 14:44:40 -0700554 layerFEState.layerStackId = layerStack1;
555 layerFEState.internalOnly = false;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700556 EXPECT_TRUE(mOutput->belongsInOutput(&layer));
Lloyd Pique66c20c42019-03-07 21:44:02 -0800557
Lloyd Pique9755fb72019-03-26 14:44:40 -0700558 layerFEState.layerStackId = layerStack1;
559 layerFEState.internalOnly = true;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700560 EXPECT_FALSE(mOutput->belongsInOutput(&layer));
Lloyd Pique66c20c42019-03-07 21:44:02 -0800561
Lloyd Pique9755fb72019-03-26 14:44:40 -0700562 layerFEState.layerStackId = layerStack2;
563 layerFEState.internalOnly = true;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700564 EXPECT_FALSE(mOutput->belongsInOutput(&layer));
Lloyd Pique66c20c42019-03-07 21:44:02 -0800565
Lloyd Pique9755fb72019-03-26 14:44:40 -0700566 layerFEState.layerStackId = layerStack2;
567 layerFEState.internalOnly = false;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700568 EXPECT_FALSE(mOutput->belongsInOutput(&layer));
Lloyd Pique66c20c42019-03-07 21:44:02 -0800569}
570
Lloyd Pique66d68602019-02-13 14:23:31 -0800571/*
Lloyd Piquecc01a452018-12-04 17:24:00 -0800572 * Output::getOutputLayerForLayer()
573 */
574
575TEST_F(OutputTest, getOutputLayerForLayerWorks) {
576 mock::OutputLayer* outputLayer1 = new StrictMock<mock::OutputLayer>();
577 mock::OutputLayer* outputLayer2 = new StrictMock<mock::OutputLayer>();
578
Lloyd Pique01c77c12019-04-17 12:48:32 -0700579 mOutput->injectOutputLayerForTest(std::unique_ptr<OutputLayer>(outputLayer1));
580 mOutput->injectOutputLayerForTest(nullptr);
581 mOutput->injectOutputLayerForTest(std::unique_ptr<OutputLayer>(outputLayer2));
Lloyd Piquecc01a452018-12-04 17:24:00 -0800582
583 StrictMock<mock::Layer> layer;
584 StrictMock<mock::Layer> otherLayer;
585
586 // If the input layer matches the first OutputLayer, it will be returned.
587 EXPECT_CALL(*outputLayer1, getLayer()).WillOnce(ReturnRef(layer));
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700588 EXPECT_EQ(outputLayer1, mOutput->getOutputLayerForLayer(&layer));
Lloyd Piquecc01a452018-12-04 17:24:00 -0800589
590 // If the input layer matches the second OutputLayer, it will be returned.
591 EXPECT_CALL(*outputLayer1, getLayer()).WillOnce(ReturnRef(otherLayer));
592 EXPECT_CALL(*outputLayer2, getLayer()).WillOnce(ReturnRef(layer));
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700593 EXPECT_EQ(outputLayer2, mOutput->getOutputLayerForLayer(&layer));
Lloyd Piquecc01a452018-12-04 17:24:00 -0800594
595 // If the input layer does not match an output layer, null will be returned.
596 EXPECT_CALL(*outputLayer1, getLayer()).WillOnce(ReturnRef(otherLayer));
597 EXPECT_CALL(*outputLayer2, getLayer()).WillOnce(ReturnRef(otherLayer));
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700598 EXPECT_EQ(nullptr, mOutput->getOutputLayerForLayer(&layer));
Lloyd Piquecc01a452018-12-04 17:24:00 -0800599}
600
Lloyd Pique66d68602019-02-13 14:23:31 -0800601/*
Lloyd Piquec9e60032019-11-14 11:47:26 -0800602 * Output::setReleasedLayers()
603 */
604
605using OutputSetReleasedLayersTest = OutputTest;
606
607TEST_F(OutputSetReleasedLayersTest, setReleasedLayersTakesGivenLayers) {
608 sp<StrictMock<mock::LayerFE>> layer1FE{new StrictMock<mock::LayerFE>()};
609 sp<StrictMock<mock::LayerFE>> layer2FE{new StrictMock<mock::LayerFE>()};
610 sp<StrictMock<mock::LayerFE>> layer3FE{new StrictMock<mock::LayerFE>()};
611
612 Output::ReleasedLayers layers;
613 layers.push_back(layer1FE);
614 layers.push_back(layer2FE);
615 layers.push_back(layer3FE);
616
617 mOutput->setReleasedLayers(std::move(layers));
618
619 const auto& setLayers = mOutput->getReleasedLayersForTest();
620 ASSERT_EQ(3u, setLayers.size());
621 ASSERT_EQ(layer1FE.get(), setLayers[0].promote().get());
622 ASSERT_EQ(layer2FE.get(), setLayers[1].promote().get());
623 ASSERT_EQ(layer3FE.get(), setLayers[2].promote().get());
624}
625
626/*
Lloyd Piquec0ee6ba2019-11-14 12:55:53 -0800627 * Output::updateLayerStateFromFE()
628 */
629
630using OutputUpdateLayerStateFromFETest = OutputLatchFEStateTest;
631
632TEST_F(OutputUpdateLayerStateFromFETest, handlesNoOutputLayerCase) {
633 CompositionRefreshArgs refreshArgs;
634
635 mOutput->updateLayerStateFromFE(refreshArgs);
636}
637
638TEST_F(OutputUpdateLayerStateFromFETest, latchesContentStateForAllContainedLayers) {
639 EXPECT_CALL(mLayer1FE,
640 latchCompositionState(Ref(mLayer1FEState), LayerFE::StateSubset::Content));
641 EXPECT_CALL(mLayer2FE,
642 latchCompositionState(Ref(mLayer2FEState), LayerFE::StateSubset::Content));
643 EXPECT_CALL(mLayer3FE,
644 latchCompositionState(Ref(mLayer3FEState), LayerFE::StateSubset::Content));
645
646 // Note: Must be performed after any expectations on these mocks
647 injectLayer(std::move(mOutputLayer1));
648 injectLayer(std::move(mOutputLayer2));
649 injectLayer(std::move(mOutputLayer3));
650
651 CompositionRefreshArgs refreshArgs;
652 refreshArgs.updatingGeometryThisFrame = false;
653
654 mOutput->updateLayerStateFromFE(refreshArgs);
655}
656
657TEST_F(OutputUpdateLayerStateFromFETest, latchesGeometryAndContentStateForAllContainedLayers) {
658 EXPECT_CALL(mLayer1FE,
659 latchCompositionState(Ref(mLayer1FEState),
660 LayerFE::StateSubset::GeometryAndContent));
661 EXPECT_CALL(mLayer2FE,
662 latchCompositionState(Ref(mLayer2FEState),
663 LayerFE::StateSubset::GeometryAndContent));
664 EXPECT_CALL(mLayer3FE,
665 latchCompositionState(Ref(mLayer3FEState),
666 LayerFE::StateSubset::GeometryAndContent));
667
668 // Note: Must be performed after any expectations on these mocks
669 injectLayer(std::move(mOutputLayer1));
670 injectLayer(std::move(mOutputLayer2));
671 injectLayer(std::move(mOutputLayer3));
672
673 CompositionRefreshArgs refreshArgs;
674 refreshArgs.updatingGeometryThisFrame = true;
675
676 mOutput->updateLayerStateFromFE(refreshArgs);
677}
678
679/*
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800680 * Output::updateAndWriteCompositionState()
681 */
682
Lloyd Piqueef63b612019-11-14 13:19:56 -0800683using OutputUpdateAndWriteCompositionStateTest = OutputLatchFEStateTest;
684
685TEST_F(OutputUpdateAndWriteCompositionStateTest, doesNothingIfLayers) {
686 mOutput->editState().isEnabled = true;
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800687
688 CompositionRefreshArgs args;
689 mOutput->updateAndWriteCompositionState(args);
690}
691
Lloyd Piqueef63b612019-11-14 13:19:56 -0800692TEST_F(OutputUpdateAndWriteCompositionStateTest, doesNothingIfOutputNotEnabled) {
693 mOutput->editState().isEnabled = false;
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800694
Lloyd Piqueef63b612019-11-14 13:19:56 -0800695 injectLayer(std::move(mOutputLayer1));
696 injectLayer(std::move(mOutputLayer2));
697 injectLayer(std::move(mOutputLayer3));
698
699 CompositionRefreshArgs args;
700 mOutput->updateAndWriteCompositionState(args);
701}
702
703TEST_F(OutputUpdateAndWriteCompositionStateTest, updatesLayerContentForAllLayers) {
704 EXPECT_CALL(*mOutputLayer1, updateCompositionState(false, false));
705 EXPECT_CALL(*mOutputLayer1, writeStateToHWC(false));
706 EXPECT_CALL(*mOutputLayer2, updateCompositionState(false, false));
707 EXPECT_CALL(*mOutputLayer2, writeStateToHWC(false));
708 EXPECT_CALL(*mOutputLayer3, updateCompositionState(false, false));
709 EXPECT_CALL(*mOutputLayer3, writeStateToHWC(false));
710
711 injectLayer(std::move(mOutputLayer1));
712 injectLayer(std::move(mOutputLayer2));
713 injectLayer(std::move(mOutputLayer3));
714
715 mOutput->editState().isEnabled = true;
716
717 CompositionRefreshArgs args;
718 args.updatingGeometryThisFrame = false;
719 args.devOptForceClientComposition = false;
720 mOutput->updateAndWriteCompositionState(args);
721}
722
723TEST_F(OutputUpdateAndWriteCompositionStateTest, updatesLayerGeometryAndContentForAllLayers) {
724 EXPECT_CALL(*mOutputLayer1, updateCompositionState(true, false));
725 EXPECT_CALL(*mOutputLayer1, writeStateToHWC(true));
726 EXPECT_CALL(*mOutputLayer2, updateCompositionState(true, false));
727 EXPECT_CALL(*mOutputLayer2, writeStateToHWC(true));
728 EXPECT_CALL(*mOutputLayer3, updateCompositionState(true, false));
729 EXPECT_CALL(*mOutputLayer3, writeStateToHWC(true));
730
731 injectLayer(std::move(mOutputLayer1));
732 injectLayer(std::move(mOutputLayer2));
733 injectLayer(std::move(mOutputLayer3));
734
735 mOutput->editState().isEnabled = true;
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800736
737 CompositionRefreshArgs args;
738 args.updatingGeometryThisFrame = true;
Lloyd Piqueef63b612019-11-14 13:19:56 -0800739 args.devOptForceClientComposition = false;
740 mOutput->updateAndWriteCompositionState(args);
741}
742
743TEST_F(OutputUpdateAndWriteCompositionStateTest, forcesClientCompositionForAllLayers) {
744 EXPECT_CALL(*mOutputLayer1, updateCompositionState(false, true));
745 EXPECT_CALL(*mOutputLayer1, writeStateToHWC(false));
746 EXPECT_CALL(*mOutputLayer2, updateCompositionState(false, true));
747 EXPECT_CALL(*mOutputLayer2, writeStateToHWC(false));
748 EXPECT_CALL(*mOutputLayer3, updateCompositionState(false, true));
749 EXPECT_CALL(*mOutputLayer3, writeStateToHWC(false));
750
751 injectLayer(std::move(mOutputLayer1));
752 injectLayer(std::move(mOutputLayer2));
753 injectLayer(std::move(mOutputLayer3));
754
755 mOutput->editState().isEnabled = true;
756
757 CompositionRefreshArgs args;
758 args.updatingGeometryThisFrame = false;
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800759 args.devOptForceClientComposition = true;
760 mOutput->updateAndWriteCompositionState(args);
761}
762
763/*
Lloyd Pique66d68602019-02-13 14:23:31 -0800764 * Output::prepareFrame()
765 */
766
767struct OutputPrepareFrameTest : public testing::Test {
Lloyd Piquefaa3f192019-11-14 14:05:09 -0800768 struct OutputPartialMock : public OutputPartialMockBase {
Lloyd Pique739afaf2019-11-21 16:40:05 -0800769 // Sets up the helper functions called by the function under test to use
770 // mock implementations.
Lloyd Pique66d68602019-02-13 14:23:31 -0800771 MOCK_METHOD0(chooseCompositionStrategy, void());
772 };
773
774 OutputPrepareFrameTest() {
775 mOutput.setDisplayColorProfileForTest(
776 std::unique_ptr<DisplayColorProfile>(mDisplayColorProfile));
777 mOutput.setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(mRenderSurface));
778 }
779
780 StrictMock<mock::CompositionEngine> mCompositionEngine;
781 mock::DisplayColorProfile* mDisplayColorProfile = new StrictMock<mock::DisplayColorProfile>();
782 mock::RenderSurface* mRenderSurface = new StrictMock<mock::RenderSurface>();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700783 StrictMock<OutputPartialMock> mOutput;
Lloyd Pique66d68602019-02-13 14:23:31 -0800784};
785
786TEST_F(OutputPrepareFrameTest, takesEarlyOutIfNotEnabled) {
787 mOutput.editState().isEnabled = false;
788
789 mOutput.prepareFrame();
790}
791
792TEST_F(OutputPrepareFrameTest, delegatesToChooseCompositionStrategyAndRenderSurface) {
793 mOutput.editState().isEnabled = true;
794 mOutput.editState().usesClientComposition = false;
795 mOutput.editState().usesDeviceComposition = true;
796
797 EXPECT_CALL(mOutput, chooseCompositionStrategy()).Times(1);
798 EXPECT_CALL(*mRenderSurface, prepareFrame(false, true));
799
800 mOutput.prepareFrame();
801}
802
803// Note: Use OutputTest and not OutputPrepareFrameTest, so the real
804// base chooseCompositionStrategy() is invoked.
805TEST_F(OutputTest, prepareFrameSetsClientCompositionOnlyByDefault) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700806 mOutput->editState().isEnabled = true;
807 mOutput->editState().usesClientComposition = false;
808 mOutput->editState().usesDeviceComposition = true;
Lloyd Pique66d68602019-02-13 14:23:31 -0800809
810 EXPECT_CALL(*mRenderSurface, prepareFrame(true, false));
811
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700812 mOutput->prepareFrame();
Lloyd Pique66d68602019-02-13 14:23:31 -0800813
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700814 EXPECT_TRUE(mOutput->getState().usesClientComposition);
815 EXPECT_FALSE(mOutput->getState().usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800816}
817
Lloyd Pique56eba802019-08-28 15:45:25 -0700818/*
Lloyd Piqueb62cebc2019-11-20 18:31:52 -0800819 * Output::prepare()
820 */
821
822struct OutputPrepareTest : public testing::Test {
823 struct OutputPartialMock : public OutputPartialMockBase {
824 // Sets up the helper functions called by the function under test to use
825 // mock implementations.
826 MOCK_METHOD2(rebuildLayerStacks,
827 void(const compositionengine::CompositionRefreshArgs&,
828 compositionengine::LayerFESet&));
829 };
830
831 StrictMock<OutputPartialMock> mOutput;
832 CompositionRefreshArgs mRefreshArgs;
Lloyd Piquefe0ee9e2019-11-22 16:30:30 -0800833 LayerFESet mGeomSnapshots;
Lloyd Piqueb62cebc2019-11-20 18:31:52 -0800834};
835
836TEST_F(OutputPrepareTest, justInvokesRebuildLayerStacks) {
837 InSequence seq;
838 EXPECT_CALL(mOutput, rebuildLayerStacks(Ref(mRefreshArgs), Ref(mGeomSnapshots)));
839
840 mOutput.prepare(mRefreshArgs, mGeomSnapshots);
841}
842
843/*
844 * Output::rebuildLayerStacks()
845 */
846
847struct OutputRebuildLayerStacksTest : public testing::Test {
848 struct OutputPartialMock : public OutputPartialMockBase {
849 // Sets up the helper functions called by the function under test to use
850 // mock implementations.
851 MOCK_METHOD2(collectVisibleLayers,
852 void(const compositionengine::CompositionRefreshArgs&,
853 compositionengine::Output::CoverageState&));
854 };
855
856 OutputRebuildLayerStacksTest() {
857 mOutput.mState.isEnabled = true;
858 mOutput.mState.transform = kIdentityTransform;
859 mOutput.mState.bounds = kOutputBounds;
860
861 mRefreshArgs.updatingOutputGeometryThisFrame = true;
862
863 mCoverageAboveCoveredLayersToSet = Region(Rect(0, 0, 10, 10));
864
865 EXPECT_CALL(mOutput, collectVisibleLayers(Ref(mRefreshArgs), _))
866 .WillRepeatedly(Invoke(this, &OutputRebuildLayerStacksTest::setTestCoverageValues));
867 }
868
869 void setTestCoverageValues(const CompositionRefreshArgs&,
870 compositionengine::Output::CoverageState& state) {
871 state.aboveCoveredLayers = mCoverageAboveCoveredLayersToSet;
872 state.aboveOpaqueLayers = mCoverageAboveOpaqueLayersToSet;
873 state.dirtyRegion = mCoverageDirtyRegionToSet;
874 }
875
876 static const ui::Transform kIdentityTransform;
877 static const ui::Transform kRotate90Transform;
878 static const Rect kOutputBounds;
879
880 StrictMock<OutputPartialMock> mOutput;
881 CompositionRefreshArgs mRefreshArgs;
Lloyd Piquefe0ee9e2019-11-22 16:30:30 -0800882 LayerFESet mGeomSnapshots;
Lloyd Piqueb62cebc2019-11-20 18:31:52 -0800883 Region mCoverageAboveCoveredLayersToSet;
884 Region mCoverageAboveOpaqueLayersToSet;
885 Region mCoverageDirtyRegionToSet;
886};
887
888const ui::Transform OutputRebuildLayerStacksTest::kIdentityTransform{TR_IDENT, 1920, 1080};
889const ui::Transform OutputRebuildLayerStacksTest::kRotate90Transform{TR_ROT_90, 1920, 1080};
890const Rect OutputRebuildLayerStacksTest::kOutputBounds{0, 0, 1920, 1080};
891
892TEST_F(OutputRebuildLayerStacksTest, doesNothingIfNotEnabled) {
893 mOutput.mState.isEnabled = false;
894
895 mOutput.rebuildLayerStacks(mRefreshArgs, mGeomSnapshots);
896}
897
898TEST_F(OutputRebuildLayerStacksTest, doesNothingIfNotUpdatingGeometryThisFrame) {
899 mRefreshArgs.updatingOutputGeometryThisFrame = false;
900
901 mOutput.rebuildLayerStacks(mRefreshArgs, mGeomSnapshots);
902}
903
904TEST_F(OutputRebuildLayerStacksTest, computesUndefinedRegionWithNoRotationAndFullCoverage) {
905 mOutput.mState.transform = kIdentityTransform;
906
907 mCoverageAboveOpaqueLayersToSet = Region(Rect(0, 0, 1920, 1080));
908
909 mOutput.rebuildLayerStacks(mRefreshArgs, mGeomSnapshots);
910
911 EXPECT_THAT(mOutput.mState.undefinedRegion, RegionEq(Region(Rect(0, 0, 0, 0))));
912}
913
914TEST_F(OutputRebuildLayerStacksTest, computesUndefinedRegionWithNoRotationAndPartialCoverage) {
915 mOutput.mState.transform = kIdentityTransform;
916
917 mCoverageAboveOpaqueLayersToSet = Region(Rect(0, 0, 960, 1080));
918
919 mOutput.rebuildLayerStacks(mRefreshArgs, mGeomSnapshots);
920
921 EXPECT_THAT(mOutput.mState.undefinedRegion, RegionEq(Region(Rect(960, 0, 1920, 1080))));
922}
923
924TEST_F(OutputRebuildLayerStacksTest, computesUndefinedRegionWith90RotationAndFullCoverage) {
925 mOutput.mState.transform = kRotate90Transform;
926
927 mCoverageAboveOpaqueLayersToSet = Region(Rect(0, 0, 1080, 1920));
928
929 mOutput.rebuildLayerStacks(mRefreshArgs, mGeomSnapshots);
930
931 EXPECT_THAT(mOutput.mState.undefinedRegion, RegionEq(Region(Rect(0, 0, 0, 0))));
932}
933
934TEST_F(OutputRebuildLayerStacksTest, computesUndefinedRegionWith90RotationAndPartialCoverage) {
935 mOutput.mState.transform = kRotate90Transform;
936
937 mCoverageAboveOpaqueLayersToSet = Region(Rect(0, 0, 1080, 960));
938
939 mOutput.rebuildLayerStacks(mRefreshArgs, mGeomSnapshots);
940
941 EXPECT_THAT(mOutput.mState.undefinedRegion, RegionEq(Region(Rect(0, 0, 960, 1080))));
942}
943
944TEST_F(OutputRebuildLayerStacksTest, addsToDirtyRegionWithNoRotation) {
945 mOutput.mState.transform = kIdentityTransform;
946 mOutput.mState.dirtyRegion = Region(Rect(960, 0, 1920, 1080));
947
948 mCoverageDirtyRegionToSet = Region(Rect(0, 0, 960, 1080));
949
950 mOutput.rebuildLayerStacks(mRefreshArgs, mGeomSnapshots);
951
952 EXPECT_THAT(mOutput.mState.dirtyRegion, RegionEq(Region(Rect(0, 0, 1920, 1080))));
953}
954
955TEST_F(OutputRebuildLayerStacksTest, addsToDirtyRegionWith90Rotation) {
956 mOutput.mState.transform = kRotate90Transform;
957 mOutput.mState.dirtyRegion = Region(Rect(0, 960, 1080, 1920));
958
959 mCoverageDirtyRegionToSet = Region(Rect(0, 0, 1080, 960));
960
961 mOutput.rebuildLayerStacks(mRefreshArgs, mGeomSnapshots);
962
963 EXPECT_THAT(mOutput.mState.dirtyRegion, RegionEq(Region(Rect(0, 0, 1080, 1920))));
964}
965
966/*
967 * Output::collectVisibleLayers()
968 */
969
Lloyd Pique1ef93222019-11-21 16:41:53 -0800970struct OutputCollectVisibleLayersTest : public testing::Test {
971 struct OutputPartialMock : public OutputPartialMockBase {
972 // Sets up the helper functions called by the function under test to use
973 // mock implementations.
974 MOCK_METHOD2(ensureOutputLayerIfVisible,
975 void(std::shared_ptr<compositionengine::Layer>,
976 compositionengine::Output::CoverageState&));
977 MOCK_METHOD1(setReleasedLayers, void(const compositionengine::CompositionRefreshArgs&));
978 MOCK_METHOD0(finalizePendingOutputLayers, void());
979 };
980
981 struct Layer {
982 Layer() {
983 EXPECT_CALL(outputLayer, getState()).WillRepeatedly(ReturnRef(outputLayerState));
984 EXPECT_CALL(outputLayer, editState()).WillRepeatedly(ReturnRef(outputLayerState));
985 }
986
987 StrictMock<mock::OutputLayer> outputLayer;
988 std::shared_ptr<StrictMock<mock::Layer>> layer{new StrictMock<mock::Layer>()};
989 impl::OutputLayerCompositionState outputLayerState;
990 };
991
992 OutputCollectVisibleLayersTest() {
Lloyd Pique0a456232020-01-16 17:51:13 -0800993 EXPECT_CALL(mOutput, getOutputLayerCount()).WillRepeatedly(Return(3u));
Lloyd Pique1ef93222019-11-21 16:41:53 -0800994 EXPECT_CALL(mOutput, getOutputLayerOrderedByZByIndex(0))
995 .WillRepeatedly(Return(&mLayer1.outputLayer));
996 EXPECT_CALL(mOutput, getOutputLayerOrderedByZByIndex(1))
997 .WillRepeatedly(Return(&mLayer2.outputLayer));
998 EXPECT_CALL(mOutput, getOutputLayerOrderedByZByIndex(2))
999 .WillRepeatedly(Return(&mLayer3.outputLayer));
1000
1001 mRefreshArgs.layers.push_back(mLayer1.layer);
1002 mRefreshArgs.layers.push_back(mLayer2.layer);
1003 mRefreshArgs.layers.push_back(mLayer3.layer);
1004 }
1005
1006 StrictMock<OutputPartialMock> mOutput;
1007 CompositionRefreshArgs mRefreshArgs;
Lloyd Piquefe0ee9e2019-11-22 16:30:30 -08001008 LayerFESet mGeomSnapshots;
1009 Output::CoverageState mCoverageState{mGeomSnapshots};
Lloyd Pique1ef93222019-11-21 16:41:53 -08001010 Layer mLayer1;
1011 Layer mLayer2;
1012 Layer mLayer3;
1013};
1014
1015TEST_F(OutputCollectVisibleLayersTest, doesMinimalWorkIfNoLayers) {
1016 mRefreshArgs.layers.clear();
Lloyd Pique0a456232020-01-16 17:51:13 -08001017 EXPECT_CALL(mOutput, getOutputLayerCount()).WillRepeatedly(Return(0u));
Lloyd Pique1ef93222019-11-21 16:41:53 -08001018
1019 EXPECT_CALL(mOutput, setReleasedLayers(Ref(mRefreshArgs)));
1020 EXPECT_CALL(mOutput, finalizePendingOutputLayers());
1021
1022 mOutput.collectVisibleLayers(mRefreshArgs, mCoverageState);
1023}
1024
1025TEST_F(OutputCollectVisibleLayersTest, processesCandidateLayersReversedAndSetsOutputLayerZ) {
1026 // Enforce a call order sequence for this test.
1027 InSequence seq;
1028
1029 // Layer coverage is evaluated from front to back!
1030 EXPECT_CALL(mOutput, ensureOutputLayerIfVisible(Eq(mLayer3.layer), Ref(mCoverageState)));
1031 EXPECT_CALL(mOutput, ensureOutputLayerIfVisible(Eq(mLayer2.layer), Ref(mCoverageState)));
1032 EXPECT_CALL(mOutput, ensureOutputLayerIfVisible(Eq(mLayer1.layer), Ref(mCoverageState)));
1033
1034 EXPECT_CALL(mOutput, setReleasedLayers(Ref(mRefreshArgs)));
1035 EXPECT_CALL(mOutput, finalizePendingOutputLayers());
1036
1037 mOutput.collectVisibleLayers(mRefreshArgs, mCoverageState);
1038
1039 // Ensure all output layers have been assigned a simple/flattened z-order.
1040 EXPECT_EQ(0u, mLayer1.outputLayerState.z);
1041 EXPECT_EQ(1u, mLayer2.outputLayerState.z);
1042 EXPECT_EQ(2u, mLayer3.outputLayerState.z);
1043}
Lloyd Piqueb62cebc2019-11-20 18:31:52 -08001044
1045/*
1046 * Output::ensureOutputLayerIfVisible()
1047 */
1048
Lloyd Piquefe0ee9e2019-11-22 16:30:30 -08001049struct OutputEnsureOutputLayerIfVisibleTest : public testing::Test {
1050 struct OutputPartialMock : public OutputPartialMockBase {
1051 // Sets up the helper functions called by the function under test to use
1052 // mock implementations.
1053 MOCK_CONST_METHOD1(belongsInOutput, bool(const compositionengine::Layer*));
1054 MOCK_CONST_METHOD1(getOutputLayerOrderedByZByIndex, OutputLayer*(size_t));
1055 MOCK_METHOD3(ensureOutputLayer,
1056 compositionengine::OutputLayer*(
1057 std::optional<size_t>,
1058 const std::shared_ptr<compositionengine::Layer>&, const sp<LayerFE>&));
1059 };
1060
1061 OutputEnsureOutputLayerIfVisibleTest() {
1062 EXPECT_CALL(*mLayer, getLayerFE()).WillRepeatedly(Return(mLayerFE));
1063 EXPECT_CALL(*mLayer, getFEState()).WillRepeatedly(ReturnRef(mLayerFEState));
1064 EXPECT_CALL(*mLayer, editFEState()).WillRepeatedly(ReturnRef(mLayerFEState));
1065
1066 EXPECT_CALL(mOutput, belongsInOutput(mLayer.get())).WillRepeatedly(Return(true));
Lloyd Pique0a456232020-01-16 17:51:13 -08001067 EXPECT_CALL(mOutput, getOutputLayerCount()).WillRepeatedly(Return(1u));
Lloyd Piquefe0ee9e2019-11-22 16:30:30 -08001068 EXPECT_CALL(mOutput, getOutputLayerOrderedByZByIndex(0u))
1069 .WillRepeatedly(Return(&mOutputLayer));
1070
1071 EXPECT_CALL(mOutputLayer, getState()).WillRepeatedly(ReturnRef(mOutputLayerState));
1072 EXPECT_CALL(mOutputLayer, editState()).WillRepeatedly(ReturnRef(mOutputLayerState));
1073 EXPECT_CALL(mOutputLayer, getLayer()).WillRepeatedly(ReturnRef(*mLayer.get()));
1074
1075 mOutput.mState.bounds = Rect(0, 0, 200, 300);
1076 mOutput.mState.viewport = Rect(0, 0, 200, 300);
1077 mOutput.mState.transform = ui::Transform(TR_IDENT, 200, 300);
1078
1079 mLayerFEState.isVisible = true;
1080 mLayerFEState.isOpaque = true;
1081 mLayerFEState.contentDirty = true;
1082 mLayerFEState.geomLayerBounds = FloatRect{0, 0, 100, 200};
1083 mLayerFEState.geomLayerTransform = ui::Transform(TR_IDENT, 100, 200);
1084 mLayerFEState.transparentRegionHint = Region(Rect(0, 0, 100, 100));
1085
1086 mOutputLayerState.visibleRegion = Region(Rect(0, 0, 50, 200));
1087 mOutputLayerState.coveredRegion = Region(Rect(50, 0, 100, 200));
1088
1089 mGeomSnapshots.insert(mLayerFE);
1090 }
1091
1092 static const Region kEmptyRegion;
1093 static const Region kFullBoundsNoRotation;
1094 static const Region kRightHalfBoundsNoRotation;
1095 static const Region kLowerHalfBoundsNoRotation;
1096 static const Region kFullBounds90Rotation;
1097
1098 StrictMock<OutputPartialMock> mOutput;
1099 LayerFESet mGeomSnapshots;
1100 Output::CoverageState mCoverageState{mGeomSnapshots};
1101
1102 std::shared_ptr<mock::Layer> mLayer{new StrictMock<mock::Layer>()};
1103 sp<StrictMock<mock::LayerFE>> mLayerFE{new StrictMock<mock::LayerFE>()};
1104 LayerFECompositionState mLayerFEState;
1105 mock::OutputLayer mOutputLayer;
1106 impl::OutputLayerCompositionState mOutputLayerState;
1107};
1108
1109const Region OutputEnsureOutputLayerIfVisibleTest::kEmptyRegion = Region(Rect(0, 0, 0, 0));
1110const Region OutputEnsureOutputLayerIfVisibleTest::kFullBoundsNoRotation =
1111 Region(Rect(0, 0, 100, 200));
1112const Region OutputEnsureOutputLayerIfVisibleTest::kRightHalfBoundsNoRotation =
1113 Region(Rect(0, 100, 100, 200));
1114const Region OutputEnsureOutputLayerIfVisibleTest::kLowerHalfBoundsNoRotation =
1115 Region(Rect(50, 0, 100, 200));
1116const Region OutputEnsureOutputLayerIfVisibleTest::kFullBounds90Rotation =
1117 Region(Rect(0, 0, 200, 100));
1118
1119TEST_F(OutputEnsureOutputLayerIfVisibleTest, doesNothingIfNoLayerFE) {
1120 EXPECT_CALL(*mLayer, getLayerFE).WillOnce(Return(sp<LayerFE>()));
1121
1122 mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
1123}
1124
1125TEST_F(OutputEnsureOutputLayerIfVisibleTest, performsGeomLatchBeforeCheckingIfLayerBelongs) {
1126 EXPECT_CALL(mOutput, belongsInOutput(mLayer.get())).WillOnce(Return(false));
1127 EXPECT_CALL(*mLayerFE.get(),
1128 latchCompositionState(Ref(mLayerFEState),
1129 compositionengine::LayerFE::StateSubset::BasicGeometry));
1130
1131 mGeomSnapshots.clear();
1132
1133 mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
1134}
1135
1136TEST_F(OutputEnsureOutputLayerIfVisibleTest,
1137 skipsLatchIfAlreadyLatchedBeforeCheckingIfLayerBelongs) {
1138 EXPECT_CALL(mOutput, belongsInOutput(mLayer.get())).WillOnce(Return(false));
1139
1140 mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
1141}
1142
1143TEST_F(OutputEnsureOutputLayerIfVisibleTest, takesEarlyOutIfLayerNotVisible) {
1144 mLayerFEState.isVisible = false;
1145
1146 mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
1147}
1148
1149TEST_F(OutputEnsureOutputLayerIfVisibleTest, takesEarlyOutIfLayerHasEmptyVisibleRegion) {
1150 mLayerFEState.geomLayerBounds = FloatRect{0, 0, 0, 0};
1151
1152 mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
1153}
1154
1155TEST_F(OutputEnsureOutputLayerIfVisibleTest, takesNotSoEarlyOutifDrawRegionEmpty) {
1156 mOutput.mState.bounds = Rect(0, 0, 0, 0);
1157
1158 mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
1159}
1160
1161TEST_F(OutputEnsureOutputLayerIfVisibleTest,
1162 handlesCreatingOutputLayerForOpaqueDirtyNotRotatedLayer) {
1163 mLayerFEState.isOpaque = true;
1164 mLayerFEState.contentDirty = true;
1165 mLayerFEState.geomLayerTransform = ui::Transform(TR_IDENT, 100, 200);
1166
1167 EXPECT_CALL(mOutput, getOutputLayerCount()).WillOnce(Return(0u));
1168 EXPECT_CALL(mOutput, ensureOutputLayer(Eq(std::nullopt), Eq(mLayer), Eq(mLayerFE)))
1169 .WillOnce(Return(&mOutputLayer));
1170
1171 mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
1172
1173 EXPECT_THAT(mCoverageState.dirtyRegion, RegionEq(kFullBoundsNoRotation));
1174 EXPECT_THAT(mCoverageState.aboveCoveredLayers, RegionEq(kFullBoundsNoRotation));
1175 EXPECT_THAT(mCoverageState.aboveOpaqueLayers, RegionEq(kFullBoundsNoRotation));
1176
1177 EXPECT_THAT(mOutputLayerState.visibleRegion, RegionEq(kFullBoundsNoRotation));
1178 EXPECT_THAT(mOutputLayerState.visibleNonTransparentRegion, RegionEq(kFullBoundsNoRotation));
1179 EXPECT_THAT(mOutputLayerState.coveredRegion, RegionEq(kEmptyRegion));
1180 EXPECT_THAT(mOutputLayerState.outputSpaceVisibleRegion, RegionEq(kFullBoundsNoRotation));
1181}
1182
1183TEST_F(OutputEnsureOutputLayerIfVisibleTest,
1184 handlesUpdatingOutputLayerForOpaqueDirtyNotRotatedLayer) {
1185 mLayerFEState.isOpaque = true;
1186 mLayerFEState.contentDirty = true;
1187 mLayerFEState.geomLayerTransform = ui::Transform(TR_IDENT, 100, 200);
1188
1189 EXPECT_CALL(mOutput, ensureOutputLayer(Eq(0u), Eq(mLayer), Eq(mLayerFE)))
1190 .WillOnce(Return(&mOutputLayer));
1191
1192 mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
1193
1194 EXPECT_THAT(mCoverageState.dirtyRegion, RegionEq(kFullBoundsNoRotation));
1195 EXPECT_THAT(mCoverageState.aboveCoveredLayers, RegionEq(kFullBoundsNoRotation));
1196 EXPECT_THAT(mCoverageState.aboveOpaqueLayers, RegionEq(kFullBoundsNoRotation));
1197
1198 EXPECT_THAT(mOutputLayerState.visibleRegion, RegionEq(kFullBoundsNoRotation));
1199 EXPECT_THAT(mOutputLayerState.visibleNonTransparentRegion, RegionEq(kFullBoundsNoRotation));
1200 EXPECT_THAT(mOutputLayerState.coveredRegion, RegionEq(kEmptyRegion));
1201 EXPECT_THAT(mOutputLayerState.outputSpaceVisibleRegion, RegionEq(kFullBoundsNoRotation));
1202}
1203
1204TEST_F(OutputEnsureOutputLayerIfVisibleTest,
1205 handlesCreatingOutputLayerForTransparentDirtyNotRotatedLayer) {
1206 mLayerFEState.isOpaque = false;
1207 mLayerFEState.contentDirty = true;
1208 mLayerFEState.geomLayerTransform = ui::Transform(TR_IDENT, 100, 200);
1209
1210 EXPECT_CALL(mOutput, getOutputLayerCount()).WillOnce(Return(0u));
1211 EXPECT_CALL(mOutput, ensureOutputLayer(Eq(std::nullopt), Eq(mLayer), Eq(mLayerFE)))
1212 .WillOnce(Return(&mOutputLayer));
1213
1214 mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
1215
1216 EXPECT_THAT(mCoverageState.dirtyRegion, RegionEq(kFullBoundsNoRotation));
1217 EXPECT_THAT(mCoverageState.aboveCoveredLayers, RegionEq(kFullBoundsNoRotation));
1218 EXPECT_THAT(mCoverageState.aboveOpaqueLayers, RegionEq(kEmptyRegion));
1219
1220 EXPECT_THAT(mOutputLayerState.visibleRegion, RegionEq(kFullBoundsNoRotation));
1221 EXPECT_THAT(mOutputLayerState.visibleNonTransparentRegion,
1222 RegionEq(kRightHalfBoundsNoRotation));
1223 EXPECT_THAT(mOutputLayerState.coveredRegion, RegionEq(kEmptyRegion));
1224 EXPECT_THAT(mOutputLayerState.outputSpaceVisibleRegion, RegionEq(kFullBoundsNoRotation));
1225}
1226
1227TEST_F(OutputEnsureOutputLayerIfVisibleTest,
1228 handlesUpdatingOutputLayerForTransparentDirtyNotRotatedLayer) {
1229 mLayerFEState.isOpaque = false;
1230 mLayerFEState.contentDirty = true;
1231 mLayerFEState.geomLayerTransform = ui::Transform(TR_IDENT, 100, 200);
1232
1233 EXPECT_CALL(mOutput, ensureOutputLayer(Eq(0u), Eq(mLayer), Eq(mLayerFE)))
1234 .WillOnce(Return(&mOutputLayer));
1235
1236 mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
1237
1238 EXPECT_THAT(mCoverageState.dirtyRegion, RegionEq(kFullBoundsNoRotation));
1239 EXPECT_THAT(mCoverageState.aboveCoveredLayers, RegionEq(kFullBoundsNoRotation));
1240 EXPECT_THAT(mCoverageState.aboveOpaqueLayers, RegionEq(kEmptyRegion));
1241
1242 EXPECT_THAT(mOutputLayerState.visibleRegion, RegionEq(kFullBoundsNoRotation));
1243 EXPECT_THAT(mOutputLayerState.visibleNonTransparentRegion,
1244 RegionEq(kRightHalfBoundsNoRotation));
1245 EXPECT_THAT(mOutputLayerState.coveredRegion, RegionEq(kEmptyRegion));
1246 EXPECT_THAT(mOutputLayerState.outputSpaceVisibleRegion, RegionEq(kFullBoundsNoRotation));
1247}
1248
1249TEST_F(OutputEnsureOutputLayerIfVisibleTest,
1250 handlesCreatingOutputLayerForOpaqueNonDirtyNotRotatedLayer) {
1251 mLayerFEState.isOpaque = true;
1252 mLayerFEState.contentDirty = false;
1253 mLayerFEState.geomLayerTransform = ui::Transform(TR_IDENT, 100, 200);
1254
1255 EXPECT_CALL(mOutput, getOutputLayerCount()).WillOnce(Return(0u));
1256 EXPECT_CALL(mOutput, ensureOutputLayer(Eq(std::nullopt), Eq(mLayer), Eq(mLayerFE)))
1257 .WillOnce(Return(&mOutputLayer));
1258
1259 mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
1260
1261 EXPECT_THAT(mCoverageState.dirtyRegion, RegionEq(kFullBoundsNoRotation));
1262 EXPECT_THAT(mCoverageState.aboveCoveredLayers, RegionEq(kFullBoundsNoRotation));
1263 EXPECT_THAT(mCoverageState.aboveOpaqueLayers, RegionEq(kFullBoundsNoRotation));
1264
1265 EXPECT_THAT(mOutputLayerState.visibleRegion, RegionEq(kFullBoundsNoRotation));
1266 EXPECT_THAT(mOutputLayerState.visibleNonTransparentRegion, RegionEq(kFullBoundsNoRotation));
1267 EXPECT_THAT(mOutputLayerState.coveredRegion, RegionEq(kEmptyRegion));
1268 EXPECT_THAT(mOutputLayerState.outputSpaceVisibleRegion, RegionEq(kFullBoundsNoRotation));
1269}
1270
1271TEST_F(OutputEnsureOutputLayerIfVisibleTest,
1272 handlesUpdatingOutputLayerForOpaqueNonDirtyNotRotatedLayer) {
1273 mLayerFEState.isOpaque = true;
1274 mLayerFEState.contentDirty = false;
1275 mLayerFEState.geomLayerTransform = ui::Transform(TR_IDENT, 100, 200);
1276
1277 EXPECT_CALL(mOutput, ensureOutputLayer(Eq(0u), Eq(mLayer), Eq(mLayerFE)))
1278 .WillOnce(Return(&mOutputLayer));
1279
1280 mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
1281
1282 EXPECT_THAT(mCoverageState.dirtyRegion, RegionEq(kLowerHalfBoundsNoRotation));
1283 EXPECT_THAT(mCoverageState.aboveCoveredLayers, RegionEq(kFullBoundsNoRotation));
1284 EXPECT_THAT(mCoverageState.aboveOpaqueLayers, RegionEq(kFullBoundsNoRotation));
1285
1286 EXPECT_THAT(mOutputLayerState.visibleRegion, RegionEq(kFullBoundsNoRotation));
1287 EXPECT_THAT(mOutputLayerState.visibleNonTransparentRegion, RegionEq(kFullBoundsNoRotation));
1288 EXPECT_THAT(mOutputLayerState.coveredRegion, RegionEq(kEmptyRegion));
1289 EXPECT_THAT(mOutputLayerState.outputSpaceVisibleRegion, RegionEq(kFullBoundsNoRotation));
1290}
1291
1292TEST_F(OutputEnsureOutputLayerIfVisibleTest,
1293 handlesCreatingOutputLayerForOpaqueDirtyRotated90Layer) {
1294 mLayerFEState.isOpaque = true;
1295 mLayerFEState.contentDirty = true;
1296 mLayerFEState.geomLayerBounds = FloatRect{0, 0, 200, 100};
1297 mLayerFEState.geomLayerTransform = ui::Transform(TR_ROT_90, 100, 200);
1298 mOutputLayerState.visibleRegion = Region(Rect(0, 0, 100, 100));
1299 mOutputLayerState.coveredRegion = Region(Rect(100, 0, 200, 100));
1300
1301 EXPECT_CALL(mOutput, getOutputLayerCount()).WillOnce(Return(0u));
1302 EXPECT_CALL(mOutput, ensureOutputLayer(Eq(std::nullopt), Eq(mLayer), Eq(mLayerFE)))
1303 .WillOnce(Return(&mOutputLayer));
1304
1305 mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
1306
1307 EXPECT_THAT(mCoverageState.dirtyRegion, RegionEq(kFullBoundsNoRotation));
1308 EXPECT_THAT(mCoverageState.aboveCoveredLayers, RegionEq(kFullBoundsNoRotation));
1309 EXPECT_THAT(mCoverageState.aboveOpaqueLayers, RegionEq(kFullBoundsNoRotation));
1310
1311 EXPECT_THAT(mOutputLayerState.visibleRegion, RegionEq(kFullBoundsNoRotation));
1312 EXPECT_THAT(mOutputLayerState.visibleNonTransparentRegion, RegionEq(kFullBoundsNoRotation));
1313 EXPECT_THAT(mOutputLayerState.coveredRegion, RegionEq(kEmptyRegion));
1314 EXPECT_THAT(mOutputLayerState.outputSpaceVisibleRegion, RegionEq(kFullBoundsNoRotation));
1315}
1316
1317TEST_F(OutputEnsureOutputLayerIfVisibleTest,
1318 handlesUpdatingOutputLayerForOpaqueDirtyRotated90Layer) {
1319 mLayerFEState.isOpaque = true;
1320 mLayerFEState.contentDirty = true;
1321 mLayerFEState.geomLayerBounds = FloatRect{0, 0, 200, 100};
1322 mLayerFEState.geomLayerTransform = ui::Transform(TR_ROT_90, 100, 200);
1323 mOutputLayerState.visibleRegion = Region(Rect(0, 0, 100, 100));
1324 mOutputLayerState.coveredRegion = Region(Rect(100, 0, 200, 100));
1325
1326 EXPECT_CALL(mOutput, ensureOutputLayer(Eq(0u), Eq(mLayer), Eq(mLayerFE)))
1327 .WillOnce(Return(&mOutputLayer));
1328
1329 mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
1330
1331 EXPECT_THAT(mCoverageState.dirtyRegion, RegionEq(kFullBoundsNoRotation));
1332 EXPECT_THAT(mCoverageState.aboveCoveredLayers, RegionEq(kFullBoundsNoRotation));
1333 EXPECT_THAT(mCoverageState.aboveOpaqueLayers, RegionEq(kFullBoundsNoRotation));
1334
1335 EXPECT_THAT(mOutputLayerState.visibleRegion, RegionEq(kFullBoundsNoRotation));
1336 EXPECT_THAT(mOutputLayerState.visibleNonTransparentRegion, RegionEq(kFullBoundsNoRotation));
1337 EXPECT_THAT(mOutputLayerState.coveredRegion, RegionEq(kEmptyRegion));
1338 EXPECT_THAT(mOutputLayerState.outputSpaceVisibleRegion, RegionEq(kFullBoundsNoRotation));
1339}
1340
1341TEST_F(OutputEnsureOutputLayerIfVisibleTest,
1342 handlesCreatingOutputLayerForOpaqueDirtyNotRotatedLayerRotatedOutput) {
1343 mLayerFEState.isOpaque = true;
1344 mLayerFEState.contentDirty = true;
1345 mLayerFEState.geomLayerTransform = ui::Transform(TR_IDENT, 100, 200);
1346
1347 mOutput.mState.viewport = Rect(0, 0, 300, 200);
1348 mOutput.mState.transform = ui::Transform(TR_ROT_90, 200, 300);
1349
1350 EXPECT_CALL(mOutput, getOutputLayerCount()).WillOnce(Return(0u));
1351 EXPECT_CALL(mOutput, ensureOutputLayer(Eq(std::nullopt), Eq(mLayer), Eq(mLayerFE)))
1352 .WillOnce(Return(&mOutputLayer));
1353
1354 mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
1355
1356 EXPECT_THAT(mCoverageState.dirtyRegion, RegionEq(kFullBoundsNoRotation));
1357 EXPECT_THAT(mCoverageState.aboveCoveredLayers, RegionEq(kFullBoundsNoRotation));
1358 EXPECT_THAT(mCoverageState.aboveOpaqueLayers, RegionEq(kFullBoundsNoRotation));
1359
1360 EXPECT_THAT(mOutputLayerState.visibleRegion, RegionEq(kFullBoundsNoRotation));
1361 EXPECT_THAT(mOutputLayerState.visibleNonTransparentRegion, RegionEq(kFullBoundsNoRotation));
1362 EXPECT_THAT(mOutputLayerState.coveredRegion, RegionEq(kEmptyRegion));
1363 EXPECT_THAT(mOutputLayerState.outputSpaceVisibleRegion, RegionEq(kFullBounds90Rotation));
1364}
1365
1366TEST_F(OutputEnsureOutputLayerIfVisibleTest,
1367 handlesUpdatingOutputLayerForOpaqueDirtyNotRotatedLayerRotatedOutput) {
1368 mLayerFEState.isOpaque = true;
1369 mLayerFEState.contentDirty = true;
1370 mLayerFEState.geomLayerTransform = ui::Transform(TR_IDENT, 100, 200);
1371
1372 mOutput.mState.viewport = Rect(0, 0, 300, 200);
1373 mOutput.mState.transform = ui::Transform(TR_ROT_90, 200, 300);
1374
1375 EXPECT_CALL(mOutput, ensureOutputLayer(Eq(0u), Eq(mLayer), Eq(mLayerFE)))
1376 .WillOnce(Return(&mOutputLayer));
1377
1378 mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
1379
1380 EXPECT_THAT(mCoverageState.dirtyRegion, RegionEq(kFullBoundsNoRotation));
1381 EXPECT_THAT(mCoverageState.aboveCoveredLayers, RegionEq(kFullBoundsNoRotation));
1382 EXPECT_THAT(mCoverageState.aboveOpaqueLayers, RegionEq(kFullBoundsNoRotation));
1383
1384 EXPECT_THAT(mOutputLayerState.visibleRegion, RegionEq(kFullBoundsNoRotation));
1385 EXPECT_THAT(mOutputLayerState.visibleNonTransparentRegion, RegionEq(kFullBoundsNoRotation));
1386 EXPECT_THAT(mOutputLayerState.coveredRegion, RegionEq(kEmptyRegion));
1387 EXPECT_THAT(mOutputLayerState.outputSpaceVisibleRegion, RegionEq(kFullBounds90Rotation));
1388}
1389
1390TEST_F(OutputEnsureOutputLayerIfVisibleTest,
1391 handlesCreatingOutputLayerForOpaqueDirtyArbitraryTransformLayer) {
1392 ui::Transform arbitraryTransform;
1393 arbitraryTransform.set(1, 1, -1, 1);
1394 arbitraryTransform.set(0, 100);
1395
1396 mLayerFEState.isOpaque = true;
1397 mLayerFEState.contentDirty = true;
1398 mLayerFEState.geomLayerBounds = FloatRect{0, 0, 100, 200};
1399 mLayerFEState.geomLayerTransform = arbitraryTransform;
1400
1401 EXPECT_CALL(mOutput, getOutputLayerCount()).WillOnce(Return(0u));
1402 EXPECT_CALL(mOutput, ensureOutputLayer(Eq(std::nullopt), Eq(mLayer), Eq(mLayerFE)))
1403 .WillOnce(Return(&mOutputLayer));
1404
1405 mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
1406
1407 const Region kRegion = Region(Rect(0, 0, 300, 300));
1408 const Region kRegionClipped = Region(Rect(0, 0, 200, 300));
1409
1410 EXPECT_THAT(mCoverageState.dirtyRegion, RegionEq(kRegion));
1411 EXPECT_THAT(mCoverageState.aboveCoveredLayers, RegionEq(kRegion));
1412 EXPECT_THAT(mCoverageState.aboveOpaqueLayers, RegionEq(kEmptyRegion));
1413
1414 EXPECT_THAT(mOutputLayerState.visibleRegion, RegionEq(kRegion));
1415 EXPECT_THAT(mOutputLayerState.visibleNonTransparentRegion, RegionEq(kRegion));
1416 EXPECT_THAT(mOutputLayerState.coveredRegion, RegionEq(kEmptyRegion));
1417 EXPECT_THAT(mOutputLayerState.outputSpaceVisibleRegion, RegionEq(kRegionClipped));
1418}
1419
1420TEST_F(OutputEnsureOutputLayerIfVisibleTest, coverageAccumulatesTest) {
1421 mLayerFEState.isOpaque = false;
1422 mLayerFEState.contentDirty = true;
1423 mLayerFEState.geomLayerTransform = ui::Transform(TR_IDENT, 100, 200);
1424
1425 mCoverageState.dirtyRegion = Region(Rect(0, 0, 500, 500));
1426 mCoverageState.aboveCoveredLayers = Region(Rect(50, 0, 150, 200));
1427 mCoverageState.aboveOpaqueLayers = Region(Rect(50, 0, 150, 200));
1428
1429 EXPECT_CALL(mOutput, ensureOutputLayer(Eq(0u), Eq(mLayer), Eq(mLayerFE)))
1430 .WillOnce(Return(&mOutputLayer));
1431
1432 mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
1433
1434 const Region kExpectedDirtyRegion = Region(Rect(0, 0, 500, 500));
1435 const Region kExpectedAboveCoveredRegion = Region(Rect(0, 0, 150, 200));
1436 const Region kExpectedAboveOpaqueRegion = Region(Rect(50, 0, 150, 200));
1437 const Region kExpectedLayerVisibleRegion = Region(Rect(0, 0, 50, 200));
1438 const Region kExpectedLayerCoveredRegion = Region(Rect(50, 0, 100, 200));
1439 const Region kExpectedLayerVisibleNonTransparentRegion = Region(Rect(0, 100, 50, 200));
1440
1441 EXPECT_THAT(mCoverageState.dirtyRegion, RegionEq(kExpectedDirtyRegion));
1442 EXPECT_THAT(mCoverageState.aboveCoveredLayers, RegionEq(kExpectedAboveCoveredRegion));
1443 EXPECT_THAT(mCoverageState.aboveOpaqueLayers, RegionEq(kExpectedAboveOpaqueRegion));
1444
1445 EXPECT_THAT(mOutputLayerState.visibleRegion, RegionEq(kExpectedLayerVisibleRegion));
1446 EXPECT_THAT(mOutputLayerState.visibleNonTransparentRegion,
1447 RegionEq(kExpectedLayerVisibleNonTransparentRegion));
1448 EXPECT_THAT(mOutputLayerState.coveredRegion, RegionEq(kExpectedLayerCoveredRegion));
1449 EXPECT_THAT(mOutputLayerState.outputSpaceVisibleRegion, RegionEq(kExpectedLayerVisibleRegion));
1450}
Lloyd Piqueb62cebc2019-11-20 18:31:52 -08001451
Vishnu Naira483b4a2019-12-12 15:07:52 -08001452TEST_F(OutputEnsureOutputLayerIfVisibleTest, coverageAccumulatesWithShadowsTest) {
1453 ui::Transform translate;
1454 translate.set(50, 50);
1455 mLayerFEState.geomLayerTransform = translate;
1456 mLayerFEState.shadowRadius = 10.0f;
1457
1458 mCoverageState.dirtyRegion = Region(Rect(0, 0, 500, 500));
1459 // half of the layer including the casting shadow is covered and opaque
1460 mCoverageState.aboveCoveredLayers = Region(Rect(40, 40, 100, 260));
1461 mCoverageState.aboveOpaqueLayers = Region(Rect(40, 40, 100, 260));
1462
1463 EXPECT_CALL(mOutput, ensureOutputLayer(Eq(0u), Eq(mLayer), Eq(mLayerFE)))
1464 .WillOnce(Return(&mOutputLayer));
1465
1466 mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
1467
1468 const Region kExpectedDirtyRegion = Region(Rect(0, 0, 500, 500));
1469 const Region kExpectedAboveCoveredRegion = Region(Rect(40, 40, 160, 260));
1470 // add starting opaque region to the opaque half of the casting layer bounds
1471 const Region kExpectedAboveOpaqueRegion =
1472 Region(Rect(40, 40, 100, 260)).orSelf(Rect(100, 50, 150, 250));
1473 const Region kExpectedLayerVisibleRegion = Region(Rect(100, 40, 160, 260));
1474 const Region kExpectedoutputSpaceLayerVisibleRegion = Region(Rect(100, 50, 150, 250));
1475 const Region kExpectedLayerCoveredRegion = Region(Rect(40, 40, 100, 260));
1476 const Region kExpectedLayerVisibleNonTransparentRegion = Region(Rect(100, 40, 160, 260));
1477 const Region kExpectedLayerShadowRegion =
1478 Region(Rect(40, 40, 160, 260)).subtractSelf(Rect(50, 50, 150, 250));
1479
1480 EXPECT_THAT(mCoverageState.dirtyRegion, RegionEq(kExpectedDirtyRegion));
1481 EXPECT_THAT(mCoverageState.aboveCoveredLayers, RegionEq(kExpectedAboveCoveredRegion));
1482 EXPECT_THAT(mCoverageState.aboveOpaqueLayers, RegionEq(kExpectedAboveOpaqueRegion));
1483
1484 EXPECT_THAT(mOutputLayerState.visibleRegion, RegionEq(kExpectedLayerVisibleRegion));
1485 EXPECT_THAT(mOutputLayerState.visibleNonTransparentRegion,
1486 RegionEq(kExpectedLayerVisibleNonTransparentRegion));
1487 EXPECT_THAT(mOutputLayerState.coveredRegion, RegionEq(kExpectedLayerCoveredRegion));
1488 EXPECT_THAT(mOutputLayerState.outputSpaceVisibleRegion,
1489 RegionEq(kExpectedoutputSpaceLayerVisibleRegion));
1490 EXPECT_THAT(mOutputLayerState.shadowRegion, RegionEq(kExpectedLayerShadowRegion));
1491 EXPECT_FALSE(kExpectedLayerVisibleRegion.subtract(kExpectedLayerShadowRegion).isEmpty());
1492}
1493
1494TEST_F(OutputEnsureOutputLayerIfVisibleTest, shadowRegionOnlyTest) {
1495 ui::Transform translate;
1496 translate.set(50, 50);
1497 mLayerFEState.geomLayerTransform = translate;
1498 mLayerFEState.shadowRadius = 10.0f;
1499
1500 mCoverageState.dirtyRegion = Region(Rect(0, 0, 500, 500));
1501 // Casting layer is covered by an opaque region leaving only part of its shadow to be drawn
1502 mCoverageState.aboveCoveredLayers = Region(Rect(40, 40, 150, 260));
1503 mCoverageState.aboveOpaqueLayers = Region(Rect(40, 40, 150, 260));
1504
1505 EXPECT_CALL(mOutput, ensureOutputLayer(Eq(0u), Eq(mLayer), Eq(mLayerFE)))
1506 .WillOnce(Return(&mOutputLayer));
1507
1508 mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
1509
1510 const Region kExpectedLayerVisibleRegion = Region(Rect(150, 40, 160, 260));
1511 const Region kExpectedLayerShadowRegion =
1512 Region(Rect(40, 40, 160, 260)).subtractSelf(Rect(50, 50, 150, 250));
1513
1514 EXPECT_THAT(mOutputLayerState.visibleRegion, RegionEq(kExpectedLayerVisibleRegion));
1515 EXPECT_THAT(mOutputLayerState.shadowRegion, RegionEq(kExpectedLayerShadowRegion));
1516 EXPECT_TRUE(kExpectedLayerVisibleRegion.subtract(kExpectedLayerShadowRegion).isEmpty());
1517}
1518
1519TEST_F(OutputEnsureOutputLayerIfVisibleTest, takesNotSoEarlyOutifLayerWithShadowIsCovered) {
1520 ui::Transform translate;
1521 translate.set(50, 50);
1522 mLayerFEState.geomLayerTransform = translate;
1523 mLayerFEState.shadowRadius = 10.0f;
1524
1525 mCoverageState.dirtyRegion = Region(Rect(0, 0, 500, 500));
1526 // Casting layer and its shadows are covered by an opaque region
1527 mCoverageState.aboveCoveredLayers = Region(Rect(40, 40, 160, 260));
1528 mCoverageState.aboveOpaqueLayers = Region(Rect(40, 40, 160, 260));
1529
1530 mOutput.ensureOutputLayerIfVisible(mLayer, mCoverageState);
1531}
1532
Lloyd Piqueb62cebc2019-11-20 18:31:52 -08001533/*
Lloyd Piquefaa3f192019-11-14 14:05:09 -08001534 * Output::present()
1535 */
1536
1537struct OutputPresentTest : public testing::Test {
1538 struct OutputPartialMock : public OutputPartialMockBase {
Lloyd Pique739afaf2019-11-21 16:40:05 -08001539 // Sets up the helper functions called by the function under test to use
1540 // mock implementations.
Lloyd Piquefaa3f192019-11-14 14:05:09 -08001541 MOCK_METHOD1(updateColorProfile, void(const compositionengine::CompositionRefreshArgs&));
1542 MOCK_METHOD1(updateAndWriteCompositionState,
1543 void(const compositionengine::CompositionRefreshArgs&));
1544 MOCK_METHOD1(setColorTransform, void(const compositionengine::CompositionRefreshArgs&));
1545 MOCK_METHOD0(beginFrame, void());
1546 MOCK_METHOD0(prepareFrame, void());
1547 MOCK_METHOD1(devOptRepaintFlash, void(const compositionengine::CompositionRefreshArgs&));
1548 MOCK_METHOD1(finishFrame, void(const compositionengine::CompositionRefreshArgs&));
1549 MOCK_METHOD0(postFramebuffer, void());
1550 };
1551
1552 StrictMock<OutputPartialMock> mOutput;
1553};
1554
1555TEST_F(OutputPresentTest, justInvokesChildFunctionsInSequence) {
1556 CompositionRefreshArgs args;
1557
1558 InSequence seq;
1559 EXPECT_CALL(mOutput, updateColorProfile(Ref(args)));
1560 EXPECT_CALL(mOutput, updateAndWriteCompositionState(Ref(args)));
1561 EXPECT_CALL(mOutput, setColorTransform(Ref(args)));
1562 EXPECT_CALL(mOutput, beginFrame());
1563 EXPECT_CALL(mOutput, prepareFrame());
1564 EXPECT_CALL(mOutput, devOptRepaintFlash(Ref(args)));
1565 EXPECT_CALL(mOutput, finishFrame(Ref(args)));
1566 EXPECT_CALL(mOutput, postFramebuffer());
1567
1568 mOutput.present(args);
1569}
1570
1571/*
1572 * Output::updateColorProfile()
1573 */
1574
Lloyd Pique17ca7422019-11-14 14:24:10 -08001575struct OutputUpdateColorProfileTest : public testing::Test {
1576 using TestType = OutputUpdateColorProfileTest;
1577
1578 struct OutputPartialMock : public OutputPartialMockBase {
Lloyd Pique739afaf2019-11-21 16:40:05 -08001579 // Sets up the helper functions called by the function under test to use
1580 // mock implementations.
Lloyd Pique17ca7422019-11-14 14:24:10 -08001581 MOCK_METHOD1(setColorProfile, void(const ColorProfile&));
1582 };
1583
1584 struct Layer {
1585 Layer() {
1586 EXPECT_CALL(mOutputLayer, getLayer()).WillRepeatedly(ReturnRef(mLayer));
1587 EXPECT_CALL(mOutputLayer, getLayerFE()).WillRepeatedly(ReturnRef(mLayerFE));
1588 EXPECT_CALL(mLayer, getFEState()).WillRepeatedly(ReturnRef(mLayerFEState));
1589 }
1590
1591 StrictMock<mock::OutputLayer> mOutputLayer;
1592 StrictMock<mock::Layer> mLayer;
1593 StrictMock<mock::LayerFE> mLayerFE;
1594 LayerFECompositionState mLayerFEState;
1595 };
1596
1597 OutputUpdateColorProfileTest() {
1598 mOutput.setDisplayColorProfileForTest(
1599 std::unique_ptr<DisplayColorProfile>(mDisplayColorProfile));
1600 mOutput.setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(mRenderSurface));
1601
1602 EXPECT_CALL(mOutput, getOutputLayerOrderedByZByIndex(0))
1603 .WillRepeatedly(Return(&mLayer1.mOutputLayer));
1604 EXPECT_CALL(mOutput, getOutputLayerOrderedByZByIndex(1))
1605 .WillRepeatedly(Return(&mLayer2.mOutputLayer));
1606 EXPECT_CALL(mOutput, getOutputLayerOrderedByZByIndex(2))
1607 .WillRepeatedly(Return(&mLayer3.mOutputLayer));
1608 }
1609
1610 struct ExecuteState : public CallOrderStateMachineHelper<TestType, ExecuteState> {
1611 void execute() { getInstance()->mOutput.updateColorProfile(getInstance()->mRefreshArgs); }
1612 };
1613
1614 mock::DisplayColorProfile* mDisplayColorProfile = new StrictMock<mock::DisplayColorProfile>();
1615 mock::RenderSurface* mRenderSurface = new StrictMock<mock::RenderSurface>();
1616 StrictMock<OutputPartialMock> mOutput;
1617
1618 Layer mLayer1;
1619 Layer mLayer2;
1620 Layer mLayer3;
1621
1622 CompositionRefreshArgs mRefreshArgs;
1623};
1624
1625// TODO(b/144522012): Refactor Output::updateColorProfile and the related code
1626// to make it easier to write unit tests.
1627
1628TEST_F(OutputUpdateColorProfileTest, setsAColorProfileWhenUnmanaged) {
1629 // When the outputColorSetting is set to kUnmanaged, the implementation sets
1630 // a simple default color profile without looking at anything else.
1631
Lloyd Pique0a456232020-01-16 17:51:13 -08001632 EXPECT_CALL(mOutput, getOutputLayerCount()).WillRepeatedly(Return(3u));
Lloyd Pique17ca7422019-11-14 14:24:10 -08001633 EXPECT_CALL(mOutput,
1634 setColorProfile(ColorProfileEq(
1635 ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
1636 ui::RenderIntent::COLORIMETRIC, ui::Dataspace::UNKNOWN})));
1637
1638 mRefreshArgs.outputColorSetting = OutputColorSetting::kUnmanaged;
1639 mRefreshArgs.colorSpaceAgnosticDataspace = ui::Dataspace::UNKNOWN;
1640
1641 mOutput.updateColorProfile(mRefreshArgs);
1642}
1643
1644struct OutputUpdateColorProfileTest_GetBestColorModeResultBecomesSetProfile
1645 : public OutputUpdateColorProfileTest {
1646 OutputUpdateColorProfileTest_GetBestColorModeResultBecomesSetProfile() {
Lloyd Pique0a456232020-01-16 17:51:13 -08001647 EXPECT_CALL(mOutput, getOutputLayerCount()).WillRepeatedly(Return(0u));
Lloyd Pique17ca7422019-11-14 14:24:10 -08001648 mRefreshArgs.outputColorSetting = OutputColorSetting::kEnhanced;
1649 mRefreshArgs.colorSpaceAgnosticDataspace = ui::Dataspace::UNKNOWN;
1650 }
1651
1652 struct ExpectBestColorModeCallResultUsedToSetColorProfileState
1653 : public CallOrderStateMachineHelper<
1654 TestType, ExpectBestColorModeCallResultUsedToSetColorProfileState> {
1655 [[nodiscard]] auto expectBestColorModeCallResultUsedToSetColorProfile(
1656 ui::ColorMode colorMode, ui::Dataspace dataspace, ui::RenderIntent renderIntent) {
1657 EXPECT_CALL(*getInstance()->mDisplayColorProfile,
1658 getBestColorMode(ui::Dataspace::V0_SRGB, ui::RenderIntent::ENHANCE, _, _,
1659 _))
1660 .WillOnce(DoAll(SetArgPointee<2>(dataspace), SetArgPointee<3>(colorMode),
1661 SetArgPointee<4>(renderIntent)));
1662 EXPECT_CALL(getInstance()->mOutput,
1663 setColorProfile(
1664 ColorProfileEq(ColorProfile{colorMode, dataspace, renderIntent,
1665 ui::Dataspace::UNKNOWN})));
1666 return nextState<ExecuteState>();
1667 }
1668 };
1669
1670 // Call this member function to start using the mini-DSL defined above.
1671 [[nodiscard]] auto verify() {
1672 return ExpectBestColorModeCallResultUsedToSetColorProfileState::make(this);
1673 }
1674};
1675
1676TEST_F(OutputUpdateColorProfileTest_GetBestColorModeResultBecomesSetProfile,
1677 Native_Unknown_Colorimetric_Set) {
1678 verify().expectBestColorModeCallResultUsedToSetColorProfile(ui::ColorMode::NATIVE,
1679 ui::Dataspace::UNKNOWN,
1680 ui::RenderIntent::COLORIMETRIC)
1681 .execute();
1682}
1683
1684TEST_F(OutputUpdateColorProfileTest_GetBestColorModeResultBecomesSetProfile,
1685 DisplayP3_DisplayP3_Enhance_Set) {
1686 verify().expectBestColorModeCallResultUsedToSetColorProfile(ui::ColorMode::DISPLAY_P3,
1687 ui::Dataspace::DISPLAY_P3,
1688 ui::RenderIntent::ENHANCE)
1689 .execute();
1690}
1691
1692struct OutputUpdateColorProfileTest_ColorSpaceAgnosticeDataspaceAffectsSetColorProfile
1693 : public OutputUpdateColorProfileTest {
1694 OutputUpdateColorProfileTest_ColorSpaceAgnosticeDataspaceAffectsSetColorProfile() {
Lloyd Pique0a456232020-01-16 17:51:13 -08001695 EXPECT_CALL(mOutput, getOutputLayerCount()).WillRepeatedly(Return(0u));
Lloyd Pique17ca7422019-11-14 14:24:10 -08001696 EXPECT_CALL(*mDisplayColorProfile,
1697 getBestColorMode(ui::Dataspace::V0_SRGB, ui::RenderIntent::ENHANCE, _, _, _))
1698 .WillRepeatedly(DoAll(SetArgPointee<2>(ui::Dataspace::UNKNOWN),
1699 SetArgPointee<3>(ui::ColorMode::NATIVE),
1700 SetArgPointee<4>(ui::RenderIntent::COLORIMETRIC)));
1701 mRefreshArgs.outputColorSetting = OutputColorSetting::kEnhanced;
1702 }
1703
1704 struct IfColorSpaceAgnosticDataspaceSetToState
1705 : public CallOrderStateMachineHelper<TestType, IfColorSpaceAgnosticDataspaceSetToState> {
1706 [[nodiscard]] auto ifColorSpaceAgnosticDataspaceSetTo(ui::Dataspace dataspace) {
1707 getInstance()->mRefreshArgs.colorSpaceAgnosticDataspace = dataspace;
1708 return nextState<ThenExpectSetColorProfileCallUsesColorSpaceAgnosticDataspaceState>();
1709 }
1710 };
1711
1712 struct ThenExpectSetColorProfileCallUsesColorSpaceAgnosticDataspaceState
1713 : public CallOrderStateMachineHelper<
1714 TestType, ThenExpectSetColorProfileCallUsesColorSpaceAgnosticDataspaceState> {
1715 [[nodiscard]] auto thenExpectSetColorProfileCallUsesColorSpaceAgnosticDataspace(
1716 ui::Dataspace dataspace) {
1717 EXPECT_CALL(getInstance()->mOutput,
1718 setColorProfile(ColorProfileEq(
1719 ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
1720 ui::RenderIntent::COLORIMETRIC, dataspace})));
1721 return nextState<ExecuteState>();
1722 }
1723 };
1724
1725 // Call this member function to start using the mini-DSL defined above.
1726 [[nodiscard]] auto verify() { return IfColorSpaceAgnosticDataspaceSetToState::make(this); }
1727};
1728
1729TEST_F(OutputUpdateColorProfileTest_ColorSpaceAgnosticeDataspaceAffectsSetColorProfile, DisplayP3) {
1730 verify().ifColorSpaceAgnosticDataspaceSetTo(ui::Dataspace::DISPLAY_P3)
1731 .thenExpectSetColorProfileCallUsesColorSpaceAgnosticDataspace(ui::Dataspace::DISPLAY_P3)
1732 .execute();
1733}
1734
1735TEST_F(OutputUpdateColorProfileTest_ColorSpaceAgnosticeDataspaceAffectsSetColorProfile, V0_SRGB) {
1736 verify().ifColorSpaceAgnosticDataspaceSetTo(ui::Dataspace::V0_SRGB)
1737 .thenExpectSetColorProfileCallUsesColorSpaceAgnosticDataspace(ui::Dataspace::V0_SRGB)
1738 .execute();
1739}
1740
1741struct OutputUpdateColorProfileTest_TopmostLayerPreferenceSetsOutputPreference
1742 : public OutputUpdateColorProfileTest {
1743 // Internally the implementation looks through the dataspaces of all the
1744 // visible layers. The topmost one that also has an actual dataspace
1745 // preference set is used to drive subsequent choices.
1746
1747 OutputUpdateColorProfileTest_TopmostLayerPreferenceSetsOutputPreference() {
1748 mRefreshArgs.outputColorSetting = OutputColorSetting::kEnhanced;
1749 mRefreshArgs.colorSpaceAgnosticDataspace = ui::Dataspace::UNKNOWN;
1750
Lloyd Pique0a456232020-01-16 17:51:13 -08001751 EXPECT_CALL(mOutput, getOutputLayerCount()).WillRepeatedly(Return(3u));
Lloyd Pique17ca7422019-11-14 14:24:10 -08001752 EXPECT_CALL(mOutput, setColorProfile(_)).WillRepeatedly(Return());
1753 }
1754
1755 struct IfTopLayerDataspaceState
1756 : public CallOrderStateMachineHelper<TestType, IfTopLayerDataspaceState> {
1757 [[nodiscard]] auto ifTopLayerIs(ui::Dataspace dataspace) {
1758 getInstance()->mLayer3.mLayerFEState.dataspace = dataspace;
1759 return nextState<AndIfMiddleLayerDataspaceState>();
1760 }
1761 [[nodiscard]] auto ifTopLayerHasNoPreference() {
1762 return ifTopLayerIs(ui::Dataspace::UNKNOWN);
1763 }
1764 };
1765
1766 struct AndIfMiddleLayerDataspaceState
1767 : public CallOrderStateMachineHelper<TestType, AndIfMiddleLayerDataspaceState> {
1768 [[nodiscard]] auto andIfMiddleLayerIs(ui::Dataspace dataspace) {
1769 getInstance()->mLayer2.mLayerFEState.dataspace = dataspace;
1770 return nextState<AndIfBottomLayerDataspaceState>();
1771 }
1772 [[nodiscard]] auto andIfMiddleLayerHasNoPreference() {
1773 return andIfMiddleLayerIs(ui::Dataspace::UNKNOWN);
1774 }
1775 };
1776
1777 struct AndIfBottomLayerDataspaceState
1778 : public CallOrderStateMachineHelper<TestType, AndIfBottomLayerDataspaceState> {
1779 [[nodiscard]] auto andIfBottomLayerIs(ui::Dataspace dataspace) {
1780 getInstance()->mLayer1.mLayerFEState.dataspace = dataspace;
1781 return nextState<ThenExpectBestColorModeCallUsesState>();
1782 }
1783 [[nodiscard]] auto andIfBottomLayerHasNoPreference() {
1784 return andIfBottomLayerIs(ui::Dataspace::UNKNOWN);
1785 }
1786 };
1787
1788 struct ThenExpectBestColorModeCallUsesState
1789 : public CallOrderStateMachineHelper<TestType, ThenExpectBestColorModeCallUsesState> {
1790 [[nodiscard]] auto thenExpectBestColorModeCallUses(ui::Dataspace dataspace) {
1791 EXPECT_CALL(*getInstance()->mDisplayColorProfile,
1792 getBestColorMode(dataspace, _, _, _, _));
1793 return nextState<ExecuteState>();
1794 }
1795 };
1796
1797 // Call this member function to start using the mini-DSL defined above.
1798 [[nodiscard]] auto verify() { return IfTopLayerDataspaceState::make(this); }
1799};
1800
1801TEST_F(OutputUpdateColorProfileTest_TopmostLayerPreferenceSetsOutputPreference,
1802 noStrongLayerPrefenceUses_V0_SRGB) {
1803 // If none of the layers indicate a preference, then V0_SRGB is the
1804 // preferred choice (subject to additional checks).
1805 verify().ifTopLayerHasNoPreference()
1806 .andIfMiddleLayerHasNoPreference()
1807 .andIfBottomLayerHasNoPreference()
1808 .thenExpectBestColorModeCallUses(ui::Dataspace::V0_SRGB)
1809 .execute();
1810}
1811
1812TEST_F(OutputUpdateColorProfileTest_TopmostLayerPreferenceSetsOutputPreference,
1813 ifTopmostUses_DisplayP3_Then_DisplayP3_Chosen) {
1814 // If only the topmost layer has a preference, then that is what is chosen.
1815 verify().ifTopLayerIs(ui::Dataspace::DISPLAY_P3)
1816 .andIfMiddleLayerHasNoPreference()
1817 .andIfBottomLayerHasNoPreference()
1818 .thenExpectBestColorModeCallUses(ui::Dataspace::DISPLAY_P3)
1819 .execute();
1820}
1821
1822TEST_F(OutputUpdateColorProfileTest_TopmostLayerPreferenceSetsOutputPreference,
1823 ifMiddleUses_DisplayP3_Then_DisplayP3_Chosen) {
1824 // If only the middle layer has a preference, that that is what is chosen.
1825 verify().ifTopLayerHasNoPreference()
1826 .andIfMiddleLayerIs(ui::Dataspace::DISPLAY_P3)
1827 .andIfBottomLayerHasNoPreference()
1828 .thenExpectBestColorModeCallUses(ui::Dataspace::DISPLAY_P3)
1829 .execute();
1830}
1831
1832TEST_F(OutputUpdateColorProfileTest_TopmostLayerPreferenceSetsOutputPreference,
1833 ifBottomUses_DisplayP3_Then_DisplayP3_Chosen) {
1834 // If only the middle layer has a preference, that that is what is chosen.
1835 verify().ifTopLayerHasNoPreference()
1836 .andIfMiddleLayerHasNoPreference()
1837 .andIfBottomLayerIs(ui::Dataspace::DISPLAY_P3)
1838 .thenExpectBestColorModeCallUses(ui::Dataspace::DISPLAY_P3)
1839 .execute();
1840}
1841
1842TEST_F(OutputUpdateColorProfileTest_TopmostLayerPreferenceSetsOutputPreference,
1843 ifTopUses_DisplayBT2020_AndBottomUses_DisplayP3_Then_DisplayBT2020_Chosen) {
1844 // If multiple layers have a preference, the topmost value is what is used.
1845 verify().ifTopLayerIs(ui::Dataspace::DISPLAY_BT2020)
1846 .andIfMiddleLayerHasNoPreference()
1847 .andIfBottomLayerIs(ui::Dataspace::DISPLAY_P3)
1848 .thenExpectBestColorModeCallUses(ui::Dataspace::DISPLAY_BT2020)
1849 .execute();
1850}
1851
1852TEST_F(OutputUpdateColorProfileTest_TopmostLayerPreferenceSetsOutputPreference,
1853 ifTopUses_DisplayP3_AndBottomUses_V0_SRGB_Then_DisplayP3_Chosen) {
1854 // If multiple layers have a preference, the topmost value is what is used.
1855 verify().ifTopLayerIs(ui::Dataspace::DISPLAY_P3)
1856 .andIfMiddleLayerHasNoPreference()
1857 .andIfBottomLayerIs(ui::Dataspace::DISPLAY_BT2020)
1858 .thenExpectBestColorModeCallUses(ui::Dataspace::DISPLAY_P3)
1859 .execute();
1860}
1861
1862struct OutputUpdateColorProfileTest_ForceOutputColorOverrides
1863 : public OutputUpdateColorProfileTest {
1864 // If CompositionRefreshArgs::forceOutputColorMode is set to some specific
1865 // values, it overrides the layer dataspace choice.
1866
1867 OutputUpdateColorProfileTest_ForceOutputColorOverrides() {
1868 mRefreshArgs.outputColorSetting = OutputColorSetting::kEnhanced;
1869 mRefreshArgs.colorSpaceAgnosticDataspace = ui::Dataspace::UNKNOWN;
1870
1871 mLayer1.mLayerFEState.dataspace = ui::Dataspace::DISPLAY_BT2020;
1872
Lloyd Pique0a456232020-01-16 17:51:13 -08001873 EXPECT_CALL(mOutput, getOutputLayerCount()).WillRepeatedly(Return(1u));
Lloyd Pique17ca7422019-11-14 14:24:10 -08001874 EXPECT_CALL(mOutput, setColorProfile(_)).WillRepeatedly(Return());
1875 }
1876
1877 struct IfForceOutputColorModeState
1878 : public CallOrderStateMachineHelper<TestType, IfForceOutputColorModeState> {
1879 [[nodiscard]] auto ifForceOutputColorMode(ui::ColorMode colorMode) {
1880 getInstance()->mRefreshArgs.forceOutputColorMode = colorMode;
1881 return nextState<ThenExpectBestColorModeCallUsesState>();
1882 }
1883 [[nodiscard]] auto ifNoOverride() { return ifForceOutputColorMode(ui::ColorMode::NATIVE); }
1884 };
1885
1886 struct ThenExpectBestColorModeCallUsesState
1887 : public CallOrderStateMachineHelper<TestType, ThenExpectBestColorModeCallUsesState> {
1888 [[nodiscard]] auto thenExpectBestColorModeCallUses(ui::Dataspace dataspace) {
1889 EXPECT_CALL(*getInstance()->mDisplayColorProfile,
1890 getBestColorMode(dataspace, _, _, _, _));
1891 return nextState<ExecuteState>();
1892 }
1893 };
1894
1895 // Call this member function to start using the mini-DSL defined above.
1896 [[nodiscard]] auto verify() { return IfForceOutputColorModeState::make(this); }
1897};
1898
1899TEST_F(OutputUpdateColorProfileTest_ForceOutputColorOverrides, NoOverride_DoesNotOverride) {
1900 // By default the layer state is used to set the preferred dataspace
1901 verify().ifNoOverride()
1902 .thenExpectBestColorModeCallUses(ui::Dataspace::DISPLAY_BT2020)
1903 .execute();
1904}
1905
1906TEST_F(OutputUpdateColorProfileTest_ForceOutputColorOverrides, SRGB_Override_USES_V0_SRGB) {
1907 // Setting ui::ColorMode::SRGB overrides it with ui::Dataspace::V0_SRGB
1908 verify().ifForceOutputColorMode(ui::ColorMode::SRGB)
1909 .thenExpectBestColorModeCallUses(ui::Dataspace::V0_SRGB)
1910 .execute();
1911}
1912
1913TEST_F(OutputUpdateColorProfileTest_ForceOutputColorOverrides, DisplayP3_Override_Uses_DisplayP3) {
1914 // Setting ui::ColorMode::DISPLAY_P3 overrides it with ui::Dataspace::DISPLAY_P3
1915 verify().ifForceOutputColorMode(ui::ColorMode::DISPLAY_P3)
1916 .thenExpectBestColorModeCallUses(ui::Dataspace::DISPLAY_P3)
1917 .execute();
1918}
1919
1920// HDR output requires all layers to be compatible with the chosen HDR
1921// dataspace, along with there being proper support.
1922struct OutputUpdateColorProfileTest_Hdr : public OutputUpdateColorProfileTest {
1923 OutputUpdateColorProfileTest_Hdr() {
1924 mRefreshArgs.outputColorSetting = OutputColorSetting::kEnhanced;
1925 mRefreshArgs.colorSpaceAgnosticDataspace = ui::Dataspace::UNKNOWN;
Lloyd Pique0a456232020-01-16 17:51:13 -08001926 EXPECT_CALL(mOutput, getOutputLayerCount()).WillRepeatedly(Return(2u));
Lloyd Pique17ca7422019-11-14 14:24:10 -08001927 EXPECT_CALL(mOutput, setColorProfile(_)).WillRepeatedly(Return());
1928 }
1929
1930 static constexpr ui::Dataspace kNonHdrDataspace = ui::Dataspace::DISPLAY_P3;
1931 static constexpr ui::Dataspace BT2020_PQ = ui::Dataspace::BT2020_PQ;
1932 static constexpr ui::Dataspace BT2020_HLG = ui::Dataspace::BT2020_HLG;
1933 static constexpr ui::Dataspace DISPLAY_P3 = ui::Dataspace::DISPLAY_P3;
1934
1935 struct IfTopLayerDataspaceState
1936 : public CallOrderStateMachineHelper<TestType, IfTopLayerDataspaceState> {
1937 [[nodiscard]] auto ifTopLayerIs(ui::Dataspace dataspace) {
1938 getInstance()->mLayer2.mLayerFEState.dataspace = dataspace;
1939 return nextState<AndTopLayerCompositionTypeState>();
1940 }
1941 [[nodiscard]] auto ifTopLayerIsNotHdr() { return ifTopLayerIs(kNonHdrDataspace); }
1942 };
1943
1944 struct AndTopLayerCompositionTypeState
1945 : public CallOrderStateMachineHelper<TestType, AndTopLayerCompositionTypeState> {
1946 [[nodiscard]] auto andTopLayerIsREComposed(bool renderEngineComposed) {
1947 getInstance()->mLayer2.mLayerFEState.forceClientComposition = renderEngineComposed;
1948 return nextState<AndIfBottomLayerDataspaceState>();
1949 }
1950 };
1951
1952 struct AndIfBottomLayerDataspaceState
1953 : public CallOrderStateMachineHelper<TestType, AndIfBottomLayerDataspaceState> {
1954 [[nodiscard]] auto andIfBottomLayerIs(ui::Dataspace dataspace) {
1955 getInstance()->mLayer1.mLayerFEState.dataspace = dataspace;
1956 return nextState<AndBottomLayerCompositionTypeState>();
1957 }
1958 [[nodiscard]] auto andIfBottomLayerIsNotHdr() {
1959 return andIfBottomLayerIs(kNonHdrDataspace);
1960 }
1961 };
1962
1963 struct AndBottomLayerCompositionTypeState
1964 : public CallOrderStateMachineHelper<TestType, AndBottomLayerCompositionTypeState> {
1965 [[nodiscard]] auto andBottomLayerIsREComposed(bool renderEngineComposed) {
1966 getInstance()->mLayer1.mLayerFEState.forceClientComposition = renderEngineComposed;
1967 return nextState<AndIfHasLegacySupportState>();
1968 }
1969 };
1970
1971 struct AndIfHasLegacySupportState
1972 : public CallOrderStateMachineHelper<TestType, AndIfHasLegacySupportState> {
1973 [[nodiscard]] auto andIfLegacySupportFor(ui::Dataspace dataspace, bool legacySupport) {
1974 EXPECT_CALL(*getInstance()->mDisplayColorProfile, hasLegacyHdrSupport(dataspace))
1975 .WillOnce(Return(legacySupport));
1976 return nextState<ThenExpectBestColorModeCallUsesState>();
1977 }
1978 };
1979
1980 struct ThenExpectBestColorModeCallUsesState
1981 : public CallOrderStateMachineHelper<TestType, ThenExpectBestColorModeCallUsesState> {
1982 [[nodiscard]] auto thenExpectBestColorModeCallUses(ui::Dataspace dataspace) {
1983 EXPECT_CALL(*getInstance()->mDisplayColorProfile,
1984 getBestColorMode(dataspace, _, _, _, _));
1985 return nextState<ExecuteState>();
1986 }
1987 };
1988
1989 // Call this member function to start using the mini-DSL defined above.
1990 [[nodiscard]] auto verify() { return IfTopLayerDataspaceState::make(this); }
1991};
1992
1993TEST_F(OutputUpdateColorProfileTest_Hdr, PQ_HW_On_PQ_HW_Uses_PQ) {
1994 // If all layers use BT2020_PQ, and there are no other special conditions,
1995 // BT2020_PQ is used.
1996 verify().ifTopLayerIs(BT2020_PQ)
1997 .andTopLayerIsREComposed(false)
1998 .andIfBottomLayerIs(BT2020_PQ)
1999 .andBottomLayerIsREComposed(false)
2000 .andIfLegacySupportFor(BT2020_PQ, false)
2001 .thenExpectBestColorModeCallUses(BT2020_PQ)
2002 .execute();
2003}
2004
2005TEST_F(OutputUpdateColorProfileTest_Hdr, PQ_HW_On_PQ_HW_IfPQHasLegacySupport_Uses_DisplayP3) {
2006 // BT2020_PQ is not used if there is only legacy support for it.
2007 verify().ifTopLayerIs(BT2020_PQ)
2008 .andTopLayerIsREComposed(false)
2009 .andIfBottomLayerIs(BT2020_PQ)
2010 .andBottomLayerIsREComposed(false)
2011 .andIfLegacySupportFor(BT2020_PQ, true)
2012 .thenExpectBestColorModeCallUses(DISPLAY_P3)
2013 .execute();
2014}
2015
2016TEST_F(OutputUpdateColorProfileTest_Hdr, PQ_HW_On_PQ_RE_Uses_PQ) {
2017 // BT2020_PQ is still used if the bottom layer is RenderEngine composed.
2018 verify().ifTopLayerIs(BT2020_PQ)
2019 .andTopLayerIsREComposed(false)
2020 .andIfBottomLayerIs(BT2020_PQ)
2021 .andBottomLayerIsREComposed(true)
2022 .andIfLegacySupportFor(BT2020_PQ, false)
2023 .thenExpectBestColorModeCallUses(BT2020_PQ)
2024 .execute();
2025}
2026
2027TEST_F(OutputUpdateColorProfileTest_Hdr, PQ_RE_On_PQ_HW_Uses_DisplayP3) {
2028 // BT2020_PQ is not used if the top layer is RenderEngine composed.
2029 verify().ifTopLayerIs(BT2020_PQ)
2030 .andTopLayerIsREComposed(true)
2031 .andIfBottomLayerIs(BT2020_PQ)
2032 .andBottomLayerIsREComposed(false)
2033 .andIfLegacySupportFor(BT2020_PQ, false)
2034 .thenExpectBestColorModeCallUses(DISPLAY_P3)
2035 .execute();
2036}
2037
2038TEST_F(OutputUpdateColorProfileTest_Hdr, PQ_HW_On_HLG_HW_Uses_PQ) {
2039 // If there is mixed HLG/PQ use, and the topmost layer is PQ, then PQ is used if there
2040 // are no other special conditions.
2041 verify().ifTopLayerIs(BT2020_PQ)
2042 .andTopLayerIsREComposed(false)
2043 .andIfBottomLayerIs(BT2020_HLG)
2044 .andBottomLayerIsREComposed(false)
2045 .andIfLegacySupportFor(BT2020_PQ, false)
2046 .thenExpectBestColorModeCallUses(BT2020_PQ)
2047 .execute();
2048}
2049
2050TEST_F(OutputUpdateColorProfileTest_Hdr, PQ_HW_On_HLG_HW_IfPQHasLegacySupport_Uses_DisplayP3) {
2051 // BT2020_PQ is not used if there is only legacy support for it.
2052 verify().ifTopLayerIs(BT2020_PQ)
2053 .andTopLayerIsREComposed(false)
2054 .andIfBottomLayerIs(BT2020_HLG)
2055 .andBottomLayerIsREComposed(false)
2056 .andIfLegacySupportFor(BT2020_PQ, true)
2057 .thenExpectBestColorModeCallUses(DISPLAY_P3)
2058 .execute();
2059}
2060
2061TEST_F(OutputUpdateColorProfileTest_Hdr, PQ_HW_On_HLG_RE_Uses_PQ) {
2062 // BT2020_PQ is used if the bottom HLG layer is RenderEngine composed.
2063 verify().ifTopLayerIs(BT2020_PQ)
2064 .andTopLayerIsREComposed(false)
2065 .andIfBottomLayerIs(BT2020_HLG)
2066 .andBottomLayerIsREComposed(true)
2067 .andIfLegacySupportFor(BT2020_PQ, false)
2068 .thenExpectBestColorModeCallUses(BT2020_PQ)
2069 .execute();
2070}
2071
2072TEST_F(OutputUpdateColorProfileTest_Hdr, PQ_RE_On_HLG_HW_Uses_DisplayP3) {
2073 // BT2020_PQ is not used if the top PQ layer is RenderEngine composed.
2074 verify().ifTopLayerIs(BT2020_PQ)
2075 .andTopLayerIsREComposed(true)
2076 .andIfBottomLayerIs(BT2020_HLG)
2077 .andBottomLayerIsREComposed(false)
2078 .andIfLegacySupportFor(BT2020_PQ, false)
2079 .thenExpectBestColorModeCallUses(DISPLAY_P3)
2080 .execute();
2081}
2082
2083TEST_F(OutputUpdateColorProfileTest_Hdr, HLG_HW_On_PQ_HW_Uses_PQ) {
2084 // If there is mixed HLG/PQ use, and the topmost layer is HLG, then PQ is
2085 // used if there are no other special conditions.
2086 verify().ifTopLayerIs(BT2020_HLG)
2087 .andTopLayerIsREComposed(false)
2088 .andIfBottomLayerIs(BT2020_PQ)
2089 .andBottomLayerIsREComposed(false)
2090 .andIfLegacySupportFor(BT2020_PQ, false)
2091 .thenExpectBestColorModeCallUses(BT2020_PQ)
2092 .execute();
2093}
2094
2095TEST_F(OutputUpdateColorProfileTest_Hdr, HLG_HW_On_PQ_HW_IfPQHasLegacySupport_Uses_DisplayP3) {
2096 // BT2020_PQ is not used if there is only legacy support for it.
2097 verify().ifTopLayerIs(BT2020_HLG)
2098 .andTopLayerIsREComposed(false)
2099 .andIfBottomLayerIs(BT2020_PQ)
2100 .andBottomLayerIsREComposed(false)
2101 .andIfLegacySupportFor(BT2020_PQ, true)
2102 .thenExpectBestColorModeCallUses(DISPLAY_P3)
2103 .execute();
2104}
2105
2106TEST_F(OutputUpdateColorProfileTest_Hdr, HLG_HW_On_PQ_RE_Uses_DisplayP3) {
2107 // BT2020_PQ is not used if the bottom PQ layer is RenderEngine composed.
2108 verify().ifTopLayerIs(BT2020_HLG)
2109 .andTopLayerIsREComposed(false)
2110 .andIfBottomLayerIs(BT2020_PQ)
2111 .andBottomLayerIsREComposed(true)
2112 .andIfLegacySupportFor(BT2020_PQ, false)
2113 .thenExpectBestColorModeCallUses(DISPLAY_P3)
2114 .execute();
2115}
2116
2117TEST_F(OutputUpdateColorProfileTest_Hdr, HLG_RE_On_PQ_HW_Uses_PQ) {
2118 // BT2020_PQ is still used if the top HLG layer is RenderEngine composed.
2119 verify().ifTopLayerIs(BT2020_HLG)
2120 .andTopLayerIsREComposed(true)
2121 .andIfBottomLayerIs(BT2020_PQ)
2122 .andBottomLayerIsREComposed(false)
2123 .andIfLegacySupportFor(BT2020_PQ, false)
2124 .thenExpectBestColorModeCallUses(BT2020_PQ)
2125 .execute();
2126}
2127
2128TEST_F(OutputUpdateColorProfileTest_Hdr, HLG_HW_On_HLG_HW_Uses_HLG) {
2129 // If all layers use HLG then HLG is used if there are no other special
2130 // conditions.
2131 verify().ifTopLayerIs(BT2020_HLG)
2132 .andTopLayerIsREComposed(false)
2133 .andIfBottomLayerIs(BT2020_HLG)
2134 .andBottomLayerIsREComposed(false)
2135 .andIfLegacySupportFor(BT2020_HLG, false)
2136 .thenExpectBestColorModeCallUses(BT2020_HLG)
2137 .execute();
2138}
2139
2140TEST_F(OutputUpdateColorProfileTest_Hdr, HLG_HW_On_HLG_HW_IfPQHasLegacySupport_Uses_DisplayP3) {
2141 // BT2020_HLG is not used if there is legacy support for it.
2142 verify().ifTopLayerIs(BT2020_HLG)
2143 .andTopLayerIsREComposed(false)
2144 .andIfBottomLayerIs(BT2020_HLG)
2145 .andBottomLayerIsREComposed(false)
2146 .andIfLegacySupportFor(BT2020_HLG, true)
2147 .thenExpectBestColorModeCallUses(DISPLAY_P3)
2148 .execute();
2149}
2150
2151TEST_F(OutputUpdateColorProfileTest_Hdr, HLG_HW_On_HLG_RE_Uses_HLG) {
2152 // BT2020_HLG is used even if the bottom layer is client composed.
2153 verify().ifTopLayerIs(BT2020_HLG)
2154 .andTopLayerIsREComposed(false)
2155 .andIfBottomLayerIs(BT2020_HLG)
2156 .andBottomLayerIsREComposed(true)
2157 .andIfLegacySupportFor(BT2020_HLG, false)
2158 .thenExpectBestColorModeCallUses(BT2020_HLG)
2159 .execute();
2160}
2161
2162TEST_F(OutputUpdateColorProfileTest_Hdr, HLG_RE_On_HLG_HW_Uses_HLG) {
2163 // BT2020_HLG is used even if the top layer is client composed.
2164 verify().ifTopLayerIs(BT2020_HLG)
2165 .andTopLayerIsREComposed(true)
2166 .andIfBottomLayerIs(BT2020_HLG)
2167 .andBottomLayerIsREComposed(false)
2168 .andIfLegacySupportFor(BT2020_HLG, false)
2169 .thenExpectBestColorModeCallUses(BT2020_HLG)
2170 .execute();
2171}
2172
2173TEST_F(OutputUpdateColorProfileTest_Hdr, PQ_HW_On_NonHdr_HW_Uses_PQ) {
2174 // Even if there are non-HDR layers present, BT2020_PQ can still be used.
2175 verify().ifTopLayerIs(BT2020_PQ)
2176 .andTopLayerIsREComposed(false)
2177 .andIfBottomLayerIsNotHdr()
2178 .andBottomLayerIsREComposed(false)
2179 .andIfLegacySupportFor(BT2020_PQ, false)
2180 .thenExpectBestColorModeCallUses(BT2020_PQ)
2181 .execute();
2182}
2183
2184TEST_F(OutputUpdateColorProfileTest_Hdr, HLG_HW_On_NonHdr_RE_Uses_HLG) {
2185 // If all layers use HLG then HLG is used if there are no other special
2186 // conditions.
2187 verify().ifTopLayerIs(BT2020_HLG)
2188 .andTopLayerIsREComposed(false)
2189 .andIfBottomLayerIsNotHdr()
2190 .andBottomLayerIsREComposed(true)
2191 .andIfLegacySupportFor(BT2020_HLG, false)
2192 .thenExpectBestColorModeCallUses(BT2020_HLG)
2193 .execute();
2194}
2195
2196struct OutputUpdateColorProfile_AffectsChosenRenderIntentTest
2197 : public OutputUpdateColorProfileTest {
2198 // The various values for CompositionRefreshArgs::outputColorSetting affect
2199 // the chosen renderIntent, along with whether the preferred dataspace is an
2200 // HDR dataspace or not.
2201
2202 OutputUpdateColorProfile_AffectsChosenRenderIntentTest() {
2203 mRefreshArgs.outputColorSetting = OutputColorSetting::kEnhanced;
2204 mRefreshArgs.colorSpaceAgnosticDataspace = ui::Dataspace::UNKNOWN;
2205 mLayer1.mLayerFEState.dataspace = ui::Dataspace::BT2020_PQ;
Lloyd Pique0a456232020-01-16 17:51:13 -08002206 EXPECT_CALL(mOutput, getOutputLayerCount()).WillRepeatedly(Return(1u));
Lloyd Pique17ca7422019-11-14 14:24:10 -08002207 EXPECT_CALL(mOutput, setColorProfile(_)).WillRepeatedly(Return());
2208 EXPECT_CALL(*mDisplayColorProfile, hasLegacyHdrSupport(ui::Dataspace::BT2020_PQ))
2209 .WillRepeatedly(Return(false));
2210 }
2211
2212 // The tests here involve enough state and GMock setup that using a mini-DSL
2213 // makes the tests much more readable, and allows the test to focus more on
2214 // the intent than on some of the details.
2215
2216 static constexpr ui::Dataspace kNonHdrDataspace = ui::Dataspace::DISPLAY_P3;
2217 static constexpr ui::Dataspace kHdrDataspace = ui::Dataspace::BT2020_PQ;
2218
2219 struct IfDataspaceChosenState
2220 : public CallOrderStateMachineHelper<TestType, IfDataspaceChosenState> {
2221 [[nodiscard]] auto ifDataspaceChosenIs(ui::Dataspace dataspace) {
2222 getInstance()->mLayer1.mLayerFEState.dataspace = dataspace;
2223 return nextState<AndOutputColorSettingState>();
2224 }
2225 [[nodiscard]] auto ifDataspaceChosenIsNonHdr() {
2226 return ifDataspaceChosenIs(kNonHdrDataspace);
2227 }
2228 [[nodiscard]] auto ifDataspaceChosenIsHdr() { return ifDataspaceChosenIs(kHdrDataspace); }
2229 };
2230
2231 struct AndOutputColorSettingState
2232 : public CallOrderStateMachineHelper<TestType, AndOutputColorSettingState> {
2233 [[nodiscard]] auto andOutputColorSettingIs(OutputColorSetting setting) {
2234 getInstance()->mRefreshArgs.outputColorSetting = setting;
2235 return nextState<ThenExpectBestColorModeCallUsesState>();
2236 }
2237 };
2238
2239 struct ThenExpectBestColorModeCallUsesState
2240 : public CallOrderStateMachineHelper<TestType, ThenExpectBestColorModeCallUsesState> {
2241 [[nodiscard]] auto thenExpectBestColorModeCallUses(ui::RenderIntent intent) {
2242 EXPECT_CALL(*getInstance()->mDisplayColorProfile,
2243 getBestColorMode(getInstance()->mLayer1.mLayerFEState.dataspace, intent, _,
2244 _, _));
2245 return nextState<ExecuteState>();
2246 }
2247 };
2248
2249 // Tests call one of these two helper member functions to start using the
2250 // mini-DSL defined above.
2251 [[nodiscard]] auto verify() { return IfDataspaceChosenState::make(this); }
2252};
2253
2254TEST_F(OutputUpdateColorProfile_AffectsChosenRenderIntentTest,
2255 Managed_NonHdr_Prefers_Colorimetric) {
2256 verify().ifDataspaceChosenIsNonHdr()
2257 .andOutputColorSettingIs(OutputColorSetting::kManaged)
2258 .thenExpectBestColorModeCallUses(ui::RenderIntent::COLORIMETRIC)
2259 .execute();
2260}
2261
2262TEST_F(OutputUpdateColorProfile_AffectsChosenRenderIntentTest,
2263 Managed_Hdr_Prefers_ToneMapColorimetric) {
2264 verify().ifDataspaceChosenIsHdr()
2265 .andOutputColorSettingIs(OutputColorSetting::kManaged)
2266 .thenExpectBestColorModeCallUses(ui::RenderIntent::TONE_MAP_COLORIMETRIC)
2267 .execute();
2268}
2269
2270TEST_F(OutputUpdateColorProfile_AffectsChosenRenderIntentTest, Enhanced_NonHdr_Prefers_Enhance) {
2271 verify().ifDataspaceChosenIsNonHdr()
2272 .andOutputColorSettingIs(OutputColorSetting::kEnhanced)
2273 .thenExpectBestColorModeCallUses(ui::RenderIntent::ENHANCE)
2274 .execute();
2275}
2276
2277TEST_F(OutputUpdateColorProfile_AffectsChosenRenderIntentTest,
2278 Enhanced_Hdr_Prefers_ToneMapEnhance) {
2279 verify().ifDataspaceChosenIsHdr()
2280 .andOutputColorSettingIs(OutputColorSetting::kEnhanced)
2281 .thenExpectBestColorModeCallUses(ui::RenderIntent::TONE_MAP_ENHANCE)
2282 .execute();
2283}
2284
2285TEST_F(OutputUpdateColorProfile_AffectsChosenRenderIntentTest, Vendor_NonHdr_Prefers_Vendor) {
2286 verify().ifDataspaceChosenIsNonHdr()
2287 .andOutputColorSettingIs(kVendorSpecifiedOutputColorSetting)
2288 .thenExpectBestColorModeCallUses(
2289 static_cast<ui::RenderIntent>(kVendorSpecifiedOutputColorSetting))
2290 .execute();
2291}
2292
2293TEST_F(OutputUpdateColorProfile_AffectsChosenRenderIntentTest, Vendor_Hdr_Prefers_Vendor) {
2294 verify().ifDataspaceChosenIsHdr()
2295 .andOutputColorSettingIs(kVendorSpecifiedOutputColorSetting)
2296 .thenExpectBestColorModeCallUses(
2297 static_cast<ui::RenderIntent>(kVendorSpecifiedOutputColorSetting))
2298 .execute();
2299}
Lloyd Piquefaa3f192019-11-14 14:05:09 -08002300
2301/*
2302 * Output::beginFrame()
2303 */
2304
Lloyd Piquee5965952019-11-18 16:16:32 -08002305struct OutputBeginFrameTest : public ::testing::Test {
2306 using TestType = OutputBeginFrameTest;
2307
2308 struct OutputPartialMock : public OutputPartialMockBase {
Lloyd Pique739afaf2019-11-21 16:40:05 -08002309 // Sets up the helper functions called by the function under test to use
2310 // mock implementations.
Lloyd Piquee5965952019-11-18 16:16:32 -08002311 MOCK_CONST_METHOD1(getDirtyRegion, Region(bool));
2312 };
2313
2314 OutputBeginFrameTest() {
2315 mOutput.setDisplayColorProfileForTest(
2316 std::unique_ptr<DisplayColorProfile>(mDisplayColorProfile));
2317 mOutput.setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(mRenderSurface));
2318 }
2319
2320 struct IfGetDirtyRegionExpectationState
2321 : public CallOrderStateMachineHelper<TestType, IfGetDirtyRegionExpectationState> {
2322 [[nodiscard]] auto ifGetDirtyRegionReturns(Region dirtyRegion) {
2323 EXPECT_CALL(getInstance()->mOutput, getDirtyRegion(false))
2324 .WillOnce(Return(dirtyRegion));
2325 return nextState<AndIfGetOutputLayerCountExpectationState>();
2326 }
2327 };
2328
2329 struct AndIfGetOutputLayerCountExpectationState
2330 : public CallOrderStateMachineHelper<TestType, AndIfGetOutputLayerCountExpectationState> {
2331 [[nodiscard]] auto andIfGetOutputLayerCountReturns(size_t layerCount) {
2332 EXPECT_CALL(getInstance()->mOutput, getOutputLayerCount()).WillOnce(Return(layerCount));
2333 return nextState<AndIfLastCompositionHadVisibleLayersState>();
2334 }
2335 };
2336
2337 struct AndIfLastCompositionHadVisibleLayersState
2338 : public CallOrderStateMachineHelper<TestType,
2339 AndIfLastCompositionHadVisibleLayersState> {
2340 [[nodiscard]] auto andIfLastCompositionHadVisibleLayersIs(bool hadOutputLayers) {
2341 getInstance()->mOutput.mState.lastCompositionHadVisibleLayers = hadOutputLayers;
2342 return nextState<ThenExpectRenderSurfaceBeginFrameCallState>();
2343 }
2344 };
2345
2346 struct ThenExpectRenderSurfaceBeginFrameCallState
2347 : public CallOrderStateMachineHelper<TestType,
2348 ThenExpectRenderSurfaceBeginFrameCallState> {
2349 [[nodiscard]] auto thenExpectRenderSurfaceBeginFrameCall(bool mustRecompose) {
2350 EXPECT_CALL(*getInstance()->mRenderSurface, beginFrame(mustRecompose));
2351 return nextState<ExecuteState>();
2352 }
2353 };
2354
2355 struct ExecuteState : public CallOrderStateMachineHelper<TestType, ExecuteState> {
2356 [[nodiscard]] auto execute() {
2357 getInstance()->mOutput.beginFrame();
2358 return nextState<CheckPostconditionHadVisibleLayersState>();
2359 }
2360 };
2361
2362 struct CheckPostconditionHadVisibleLayersState
2363 : public CallOrderStateMachineHelper<TestType, CheckPostconditionHadVisibleLayersState> {
2364 void checkPostconditionHadVisibleLayers(bool expected) {
2365 EXPECT_EQ(expected, getInstance()->mOutput.mState.lastCompositionHadVisibleLayers);
2366 }
2367 };
2368
2369 // Tests call one of these two helper member functions to start using the
2370 // mini-DSL defined above.
2371 [[nodiscard]] auto verify() { return IfGetDirtyRegionExpectationState::make(this); }
2372
2373 static const Region kEmptyRegion;
2374 static const Region kNotEmptyRegion;
2375
2376 mock::DisplayColorProfile* mDisplayColorProfile = new StrictMock<mock::DisplayColorProfile>();
2377 mock::RenderSurface* mRenderSurface = new StrictMock<mock::RenderSurface>();
2378 StrictMock<OutputPartialMock> mOutput;
2379};
2380
2381const Region OutputBeginFrameTest::kEmptyRegion{Rect{0, 0, 0, 0}};
2382const Region OutputBeginFrameTest::kNotEmptyRegion{Rect{0, 0, 1, 1}};
2383
2384TEST_F(OutputBeginFrameTest, hasDirtyHasLayersHadLayersLastFrame) {
2385 verify().ifGetDirtyRegionReturns(kNotEmptyRegion)
2386 .andIfGetOutputLayerCountReturns(1u)
2387 .andIfLastCompositionHadVisibleLayersIs(true)
2388 .thenExpectRenderSurfaceBeginFrameCall(true)
2389 .execute()
2390 .checkPostconditionHadVisibleLayers(true);
2391}
2392
2393TEST_F(OutputBeginFrameTest, hasDirtyNotHasLayersHadLayersLastFrame) {
2394 verify().ifGetDirtyRegionReturns(kNotEmptyRegion)
2395 .andIfGetOutputLayerCountReturns(0u)
2396 .andIfLastCompositionHadVisibleLayersIs(true)
2397 .thenExpectRenderSurfaceBeginFrameCall(true)
2398 .execute()
2399 .checkPostconditionHadVisibleLayers(false);
2400}
2401
2402TEST_F(OutputBeginFrameTest, hasDirtyHasLayersNotHadLayersLastFrame) {
2403 verify().ifGetDirtyRegionReturns(kNotEmptyRegion)
2404 .andIfGetOutputLayerCountReturns(1u)
2405 .andIfLastCompositionHadVisibleLayersIs(false)
2406 .thenExpectRenderSurfaceBeginFrameCall(true)
2407 .execute()
2408 .checkPostconditionHadVisibleLayers(true);
2409}
2410
2411TEST_F(OutputBeginFrameTest, hasDirtyNotHasLayersNotHadLayersLastFrame) {
2412 verify().ifGetDirtyRegionReturns(kNotEmptyRegion)
2413 .andIfGetOutputLayerCountReturns(0u)
2414 .andIfLastCompositionHadVisibleLayersIs(false)
2415 .thenExpectRenderSurfaceBeginFrameCall(false)
2416 .execute()
2417 .checkPostconditionHadVisibleLayers(false);
2418}
2419
2420TEST_F(OutputBeginFrameTest, notHasDirtyHasLayersHadLayersLastFrame) {
2421 verify().ifGetDirtyRegionReturns(kEmptyRegion)
2422 .andIfGetOutputLayerCountReturns(1u)
2423 .andIfLastCompositionHadVisibleLayersIs(true)
2424 .thenExpectRenderSurfaceBeginFrameCall(false)
2425 .execute()
2426 .checkPostconditionHadVisibleLayers(true);
2427}
2428
2429TEST_F(OutputBeginFrameTest, notHasDirtyNotHasLayersHadLayersLastFrame) {
2430 verify().ifGetDirtyRegionReturns(kEmptyRegion)
2431 .andIfGetOutputLayerCountReturns(0u)
2432 .andIfLastCompositionHadVisibleLayersIs(true)
2433 .thenExpectRenderSurfaceBeginFrameCall(false)
2434 .execute()
2435 .checkPostconditionHadVisibleLayers(true);
2436}
2437
2438TEST_F(OutputBeginFrameTest, notHasDirtyHasLayersNotHadLayersLastFrame) {
2439 verify().ifGetDirtyRegionReturns(kEmptyRegion)
2440 .andIfGetOutputLayerCountReturns(1u)
2441 .andIfLastCompositionHadVisibleLayersIs(false)
2442 .thenExpectRenderSurfaceBeginFrameCall(false)
2443 .execute()
2444 .checkPostconditionHadVisibleLayers(false);
2445}
2446
2447TEST_F(OutputBeginFrameTest, notHasDirtyNotHasLayersNotHadLayersLastFrame) {
2448 verify().ifGetDirtyRegionReturns(kEmptyRegion)
2449 .andIfGetOutputLayerCountReturns(0u)
2450 .andIfLastCompositionHadVisibleLayersIs(false)
2451 .thenExpectRenderSurfaceBeginFrameCall(false)
2452 .execute()
2453 .checkPostconditionHadVisibleLayers(false);
2454}
2455
Lloyd Piquefaa3f192019-11-14 14:05:09 -08002456/*
2457 * Output::devOptRepaintFlash()
2458 */
2459
Lloyd Piquedb462d82019-11-19 17:58:46 -08002460struct OutputDevOptRepaintFlashTest : public testing::Test {
2461 struct OutputPartialMock : public OutputPartialMockBase {
Lloyd Pique739afaf2019-11-21 16:40:05 -08002462 // Sets up the helper functions called by the function under test to use
2463 // mock implementations.
Lloyd Piquedb462d82019-11-19 17:58:46 -08002464 MOCK_CONST_METHOD1(getDirtyRegion, Region(bool));
2465 MOCK_METHOD1(composeSurfaces, std::optional<base::unique_fd>(const Region&));
2466 MOCK_METHOD0(postFramebuffer, void());
2467 MOCK_METHOD0(prepareFrame, void());
2468 };
2469
2470 OutputDevOptRepaintFlashTest() {
2471 mOutput.setDisplayColorProfileForTest(
2472 std::unique_ptr<DisplayColorProfile>(mDisplayColorProfile));
2473 mOutput.setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(mRenderSurface));
2474 }
2475
2476 static const Region kEmptyRegion;
2477 static const Region kNotEmptyRegion;
2478
2479 StrictMock<OutputPartialMock> mOutput;
2480 mock::DisplayColorProfile* mDisplayColorProfile = new StrictMock<mock::DisplayColorProfile>();
2481 mock::RenderSurface* mRenderSurface = new StrictMock<mock::RenderSurface>();
2482 CompositionRefreshArgs mRefreshArgs;
2483};
2484
2485const Region OutputDevOptRepaintFlashTest::kEmptyRegion{Rect{0, 0, 0, 0}};
2486const Region OutputDevOptRepaintFlashTest::kNotEmptyRegion{Rect{0, 0, 1, 1}};
2487
2488TEST_F(OutputDevOptRepaintFlashTest, doesNothingIfFlashDelayNotSet) {
2489 mRefreshArgs.devOptFlashDirtyRegionsDelay = {};
2490 mRefreshArgs.repaintEverything = true;
2491 mOutput.mState.isEnabled = true;
2492
2493 mOutput.devOptRepaintFlash(mRefreshArgs);
2494}
2495
2496TEST_F(OutputDevOptRepaintFlashTest, postsAndPreparesANewFrameIfNotEnabled) {
2497 mRefreshArgs.devOptFlashDirtyRegionsDelay = std::chrono::microseconds(1);
2498 mRefreshArgs.repaintEverything = true;
2499 mOutput.mState.isEnabled = false;
2500
2501 InSequence seq;
2502 EXPECT_CALL(mOutput, postFramebuffer());
2503 EXPECT_CALL(mOutput, prepareFrame());
2504
2505 mOutput.devOptRepaintFlash(mRefreshArgs);
2506}
2507
2508TEST_F(OutputDevOptRepaintFlashTest, postsAndPreparesANewFrameIfNotDirty) {
2509 mRefreshArgs.devOptFlashDirtyRegionsDelay = std::chrono::microseconds(1);
2510 mRefreshArgs.repaintEverything = true;
2511 mOutput.mState.isEnabled = true;
2512
2513 InSequence seq;
2514 EXPECT_CALL(mOutput, getDirtyRegion(true)).WillOnce(Return(kEmptyRegion));
2515 EXPECT_CALL(mOutput, postFramebuffer());
2516 EXPECT_CALL(mOutput, prepareFrame());
2517
2518 mOutput.devOptRepaintFlash(mRefreshArgs);
2519}
2520
2521TEST_F(OutputDevOptRepaintFlashTest, alsoComposesSurfacesAndQueuesABufferIfDirty) {
2522 mRefreshArgs.devOptFlashDirtyRegionsDelay = std::chrono::microseconds(1);
2523 mRefreshArgs.repaintEverything = false;
2524 mOutput.mState.isEnabled = true;
2525
2526 InSequence seq;
2527 EXPECT_CALL(mOutput, getDirtyRegion(false)).WillOnce(Return(kNotEmptyRegion));
2528 EXPECT_CALL(mOutput, composeSurfaces(RegionEq(kNotEmptyRegion)));
2529 EXPECT_CALL(*mRenderSurface, queueBuffer(_));
2530 EXPECT_CALL(mOutput, postFramebuffer());
2531 EXPECT_CALL(mOutput, prepareFrame());
2532
2533 mOutput.devOptRepaintFlash(mRefreshArgs);
2534}
2535
Lloyd Piquefaa3f192019-11-14 14:05:09 -08002536/*
2537 * Output::finishFrame()
2538 */
2539
Lloyd Pique03561a62019-11-19 18:34:52 -08002540struct OutputFinishFrameTest : public testing::Test {
2541 struct OutputPartialMock : public OutputPartialMockBase {
Lloyd Pique739afaf2019-11-21 16:40:05 -08002542 // Sets up the helper functions called by the function under test to use
2543 // mock implementations.
Lloyd Pique03561a62019-11-19 18:34:52 -08002544 MOCK_METHOD1(composeSurfaces, std::optional<base::unique_fd>(const Region&));
2545 MOCK_METHOD0(postFramebuffer, void());
2546 };
2547
2548 OutputFinishFrameTest() {
2549 mOutput.setDisplayColorProfileForTest(
2550 std::unique_ptr<DisplayColorProfile>(mDisplayColorProfile));
2551 mOutput.setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(mRenderSurface));
2552 }
2553
2554 StrictMock<OutputPartialMock> mOutput;
2555 mock::DisplayColorProfile* mDisplayColorProfile = new StrictMock<mock::DisplayColorProfile>();
2556 mock::RenderSurface* mRenderSurface = new StrictMock<mock::RenderSurface>();
2557 CompositionRefreshArgs mRefreshArgs;
2558};
2559
2560TEST_F(OutputFinishFrameTest, ifNotEnabledDoesNothing) {
2561 mOutput.mState.isEnabled = false;
2562
2563 mOutput.finishFrame(mRefreshArgs);
2564}
2565
2566TEST_F(OutputFinishFrameTest, takesEarlyOutifComposeSurfacesReturnsNoFence) {
2567 mOutput.mState.isEnabled = true;
2568
2569 InSequence seq;
2570 EXPECT_CALL(mOutput, composeSurfaces(RegionEq(Region::INVALID_REGION)));
2571
2572 mOutput.finishFrame(mRefreshArgs);
2573}
2574
2575TEST_F(OutputFinishFrameTest, queuesBufferIfComposeSurfacesReturnsAFence) {
2576 mOutput.mState.isEnabled = true;
2577
2578 InSequence seq;
2579 EXPECT_CALL(mOutput, composeSurfaces(RegionEq(Region::INVALID_REGION)))
2580 .WillOnce(Return(ByMove(base::unique_fd())));
2581 EXPECT_CALL(*mRenderSurface, queueBuffer(_));
2582
2583 mOutput.finishFrame(mRefreshArgs);
2584}
Lloyd Piquefaa3f192019-11-14 14:05:09 -08002585
2586/*
2587 * Output::postFramebuffer()
2588 */
2589
Lloyd Pique07178e32019-11-19 19:15:26 -08002590struct OutputPostFramebufferTest : public testing::Test {
2591 struct OutputPartialMock : public OutputPartialMockBase {
Lloyd Pique739afaf2019-11-21 16:40:05 -08002592 // Sets up the helper functions called by the function under test to use
2593 // mock implementations.
Lloyd Pique07178e32019-11-19 19:15:26 -08002594 MOCK_METHOD0(presentAndGetFrameFences, compositionengine::Output::FrameFences());
2595 };
2596
2597 struct Layer {
2598 Layer() {
2599 EXPECT_CALL(outputLayer, getLayerFE()).WillRepeatedly(ReturnRef(layerFE));
2600 EXPECT_CALL(outputLayer, getHwcLayer()).WillRepeatedly(Return(&hwc2Layer));
2601 }
2602
2603 StrictMock<mock::OutputLayer> outputLayer;
2604 StrictMock<mock::LayerFE> layerFE;
2605 StrictMock<HWC2::mock::Layer> hwc2Layer;
2606 };
2607
2608 OutputPostFramebufferTest() {
2609 mOutput.setDisplayColorProfileForTest(
2610 std::unique_ptr<DisplayColorProfile>(mDisplayColorProfile));
2611 mOutput.setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(mRenderSurface));
2612
2613 EXPECT_CALL(mOutput, getOutputLayerCount()).WillRepeatedly(Return(3u));
2614 EXPECT_CALL(mOutput, getOutputLayerOrderedByZByIndex(0u))
2615 .WillRepeatedly(Return(&mLayer1.outputLayer));
2616 EXPECT_CALL(mOutput, getOutputLayerOrderedByZByIndex(1u))
2617 .WillRepeatedly(Return(&mLayer2.outputLayer));
2618 EXPECT_CALL(mOutput, getOutputLayerOrderedByZByIndex(2u))
2619 .WillRepeatedly(Return(&mLayer3.outputLayer));
2620 }
2621
2622 StrictMock<OutputPartialMock> mOutput;
2623 mock::DisplayColorProfile* mDisplayColorProfile = new StrictMock<mock::DisplayColorProfile>();
2624 mock::RenderSurface* mRenderSurface = new StrictMock<mock::RenderSurface>();
2625
2626 Layer mLayer1;
2627 Layer mLayer2;
2628 Layer mLayer3;
2629};
2630
2631TEST_F(OutputPostFramebufferTest, ifNotEnabledDoesNothing) {
2632 mOutput.mState.isEnabled = false;
2633
2634 mOutput.postFramebuffer();
2635}
2636
2637TEST_F(OutputPostFramebufferTest, ifEnabledMustFlipThenPresentThenSendPresentCompleted) {
2638 mOutput.mState.isEnabled = true;
2639
2640 compositionengine::Output::FrameFences frameFences;
2641
2642 // This should happen even if there are no output layers.
2643 EXPECT_CALL(mOutput, getOutputLayerCount()).WillOnce(Return(0u));
2644
2645 // For this test in particular we want to make sure the call expectations
2646 // setup below are satisfied in the specific order.
2647 InSequence seq;
2648
2649 EXPECT_CALL(*mRenderSurface, flip());
2650 EXPECT_CALL(mOutput, presentAndGetFrameFences()).WillOnce(Return(frameFences));
2651 EXPECT_CALL(*mRenderSurface, onPresentDisplayCompleted());
2652
2653 mOutput.postFramebuffer();
2654}
2655
2656TEST_F(OutputPostFramebufferTest, releaseFencesAreSentToLayerFE) {
2657 // Simulate getting release fences from each layer, and ensure they are passed to the
2658 // front-end layer interface for each layer correctly.
2659
2660 mOutput.mState.isEnabled = true;
2661
2662 // Create three unique fence instances
2663 sp<Fence> layer1Fence = new Fence();
2664 sp<Fence> layer2Fence = new Fence();
2665 sp<Fence> layer3Fence = new Fence();
2666
Lloyd Piquefe0ee9e2019-11-22 16:30:30 -08002667 Output::FrameFences frameFences;
Lloyd Pique07178e32019-11-19 19:15:26 -08002668 frameFences.layerFences.emplace(&mLayer1.hwc2Layer, layer1Fence);
2669 frameFences.layerFences.emplace(&mLayer2.hwc2Layer, layer2Fence);
2670 frameFences.layerFences.emplace(&mLayer3.hwc2Layer, layer3Fence);
2671
2672 EXPECT_CALL(*mRenderSurface, flip());
2673 EXPECT_CALL(mOutput, presentAndGetFrameFences()).WillOnce(Return(frameFences));
2674 EXPECT_CALL(*mRenderSurface, onPresentDisplayCompleted());
2675
2676 // Compare the pointers values of each fence to make sure the correct ones
2677 // are passed. This happens to work with the current implementation, but
2678 // would not survive certain calls like Fence::merge() which would return a
2679 // new instance.
2680 EXPECT_CALL(mLayer1.layerFE,
2681 onLayerDisplayed(Property(&sp<Fence>::get, Eq(layer1Fence.get()))));
2682 EXPECT_CALL(mLayer2.layerFE,
2683 onLayerDisplayed(Property(&sp<Fence>::get, Eq(layer2Fence.get()))));
2684 EXPECT_CALL(mLayer3.layerFE,
2685 onLayerDisplayed(Property(&sp<Fence>::get, Eq(layer3Fence.get()))));
2686
2687 mOutput.postFramebuffer();
2688}
2689
2690TEST_F(OutputPostFramebufferTest, releaseFencesIncludeClientTargetAcquireFence) {
2691 mOutput.mState.isEnabled = true;
2692 mOutput.mState.usesClientComposition = true;
2693
2694 sp<Fence> clientTargetAcquireFence = new Fence();
2695 sp<Fence> layer1Fence = new Fence();
2696 sp<Fence> layer2Fence = new Fence();
2697 sp<Fence> layer3Fence = new Fence();
Lloyd Piquefe0ee9e2019-11-22 16:30:30 -08002698 Output::FrameFences frameFences;
Lloyd Pique07178e32019-11-19 19:15:26 -08002699 frameFences.clientTargetAcquireFence = clientTargetAcquireFence;
2700 frameFences.layerFences.emplace(&mLayer1.hwc2Layer, layer1Fence);
2701 frameFences.layerFences.emplace(&mLayer2.hwc2Layer, layer2Fence);
2702 frameFences.layerFences.emplace(&mLayer3.hwc2Layer, layer3Fence);
2703
2704 EXPECT_CALL(*mRenderSurface, flip());
2705 EXPECT_CALL(mOutput, presentAndGetFrameFences()).WillOnce(Return(frameFences));
2706 EXPECT_CALL(*mRenderSurface, onPresentDisplayCompleted());
2707
2708 // Fence::merge is called, and since none of the fences are actually valid,
2709 // Fence::NO_FENCE is returned and passed to each onLayerDisplayed() call.
2710 // This is the best we can do without creating a real kernel fence object.
2711 EXPECT_CALL(mLayer1.layerFE, onLayerDisplayed(Fence::NO_FENCE));
2712 EXPECT_CALL(mLayer2.layerFE, onLayerDisplayed(Fence::NO_FENCE));
2713 EXPECT_CALL(mLayer3.layerFE, onLayerDisplayed(Fence::NO_FENCE));
2714
2715 mOutput.postFramebuffer();
2716}
2717
2718TEST_F(OutputPostFramebufferTest, releasedLayersSentPresentFence) {
2719 mOutput.mState.isEnabled = true;
2720 mOutput.mState.usesClientComposition = true;
2721
2722 // This should happen even if there are no (current) output layers.
2723 EXPECT_CALL(mOutput, getOutputLayerCount()).WillOnce(Return(0u));
2724
2725 // Load up the released layers with some mock instances
2726 sp<StrictMock<mock::LayerFE>> releasedLayer1{new StrictMock<mock::LayerFE>()};
2727 sp<StrictMock<mock::LayerFE>> releasedLayer2{new StrictMock<mock::LayerFE>()};
2728 sp<StrictMock<mock::LayerFE>> releasedLayer3{new StrictMock<mock::LayerFE>()};
2729 Output::ReleasedLayers layers;
2730 layers.push_back(releasedLayer1);
2731 layers.push_back(releasedLayer2);
2732 layers.push_back(releasedLayer3);
2733 mOutput.setReleasedLayers(std::move(layers));
2734
2735 // Set up a fake present fence
2736 sp<Fence> presentFence = new Fence();
Lloyd Piquefe0ee9e2019-11-22 16:30:30 -08002737 Output::FrameFences frameFences;
Lloyd Pique07178e32019-11-19 19:15:26 -08002738 frameFences.presentFence = presentFence;
2739
2740 EXPECT_CALL(*mRenderSurface, flip());
2741 EXPECT_CALL(mOutput, presentAndGetFrameFences()).WillOnce(Return(frameFences));
2742 EXPECT_CALL(*mRenderSurface, onPresentDisplayCompleted());
2743
2744 // Each released layer should be given the presentFence.
2745 EXPECT_CALL(*releasedLayer1,
2746 onLayerDisplayed(Property(&sp<Fence>::get, Eq(presentFence.get()))));
2747 EXPECT_CALL(*releasedLayer2,
2748 onLayerDisplayed(Property(&sp<Fence>::get, Eq(presentFence.get()))));
2749 EXPECT_CALL(*releasedLayer3,
2750 onLayerDisplayed(Property(&sp<Fence>::get, Eq(presentFence.get()))));
2751
2752 mOutput.postFramebuffer();
2753
2754 // After the call the list of released layers should have been cleared.
2755 EXPECT_TRUE(mOutput.getReleasedLayersForTest().empty());
2756}
Lloyd Piquefaa3f192019-11-14 14:05:09 -08002757
2758/*
Lloyd Pique56eba802019-08-28 15:45:25 -07002759 * Output::composeSurfaces()
2760 */
2761
2762struct OutputComposeSurfacesTest : public testing::Test {
Lloyd Pique6818fa52019-12-03 12:32:13 -08002763 using TestType = OutputComposeSurfacesTest;
Lloyd Pique56eba802019-08-28 15:45:25 -07002764
Lloyd Piquefaa3f192019-11-14 14:05:09 -08002765 struct OutputPartialMock : public OutputPartialMockBase {
Lloyd Pique739afaf2019-11-21 16:40:05 -08002766 // Sets up the helper functions called by the function under test to use
2767 // mock implementations.
Lloyd Pique56eba802019-08-28 15:45:25 -07002768 MOCK_CONST_METHOD0(getSkipColorTransform, bool());
Vishnu Nair3a7346c2019-12-04 08:09:09 -08002769 MOCK_METHOD3(generateClientCompositionRequests,
Vishnu Nair9b079a22020-01-21 14:36:08 -08002770 std::vector<LayerFE::LayerSettings>(bool, Region&, ui::Dataspace));
Lloyd Pique56eba802019-08-28 15:45:25 -07002771 MOCK_METHOD2(appendRegionFlashRequests,
Vishnu Nair9b079a22020-01-21 14:36:08 -08002772 void(const Region&, std::vector<LayerFE::LayerSettings>&));
Lloyd Pique56eba802019-08-28 15:45:25 -07002773 MOCK_METHOD1(setExpensiveRenderingExpected, void(bool));
2774 };
2775
2776 OutputComposeSurfacesTest() {
2777 mOutput.setDisplayColorProfileForTest(
2778 std::unique_ptr<DisplayColorProfile>(mDisplayColorProfile));
2779 mOutput.setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(mRenderSurface));
Vishnu Nair9b079a22020-01-21 14:36:08 -08002780 mOutput.cacheClientCompositionRequests(MAX_CLIENT_COMPOSITION_CACHE_SIZE);
Lloyd Pique56eba802019-08-28 15:45:25 -07002781
Lloyd Pique6818fa52019-12-03 12:32:13 -08002782 mOutput.mState.frame = kDefaultOutputFrame;
2783 mOutput.mState.viewport = kDefaultOutputViewport;
Lloyd Piquee8fe4742020-01-21 15:26:18 -08002784 mOutput.mState.sourceClip = kDefaultOutputSourceClip;
2785 mOutput.mState.destinationClip = kDefaultOutputDestinationClip;
Lloyd Pique6818fa52019-12-03 12:32:13 -08002786 mOutput.mState.transform = ui::Transform{kDefaultOutputOrientation};
2787 mOutput.mState.orientation = kDefaultOutputOrientation;
2788 mOutput.mState.dataspace = kDefaultOutputDataspace;
2789 mOutput.mState.colorTransformMatrix = kDefaultColorTransformMat;
2790 mOutput.mState.isSecure = false;
2791 mOutput.mState.needsFiltering = false;
2792 mOutput.mState.usesClientComposition = true;
2793 mOutput.mState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08002794 mOutput.mState.reusedClientComposition = false;
Lloyd Pique56eba802019-08-28 15:45:25 -07002795
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07002796 EXPECT_CALL(mOutput, getCompositionEngine()).WillRepeatedly(ReturnRef(mCompositionEngine));
Lloyd Pique56eba802019-08-28 15:45:25 -07002797 EXPECT_CALL(mCompositionEngine, getRenderEngine()).WillRepeatedly(ReturnRef(mRenderEngine));
Alec Mourie4034bb2019-11-19 12:45:54 -08002798 EXPECT_CALL(mCompositionEngine, getTimeStats())
2799 .WillRepeatedly(ReturnRef(*mTimeStats.get()));
Lloyd Pique6818fa52019-12-03 12:32:13 -08002800 EXPECT_CALL(*mDisplayColorProfile, getHdrCapabilities())
2801 .WillRepeatedly(ReturnRef(kHdrCapabilities));
Lloyd Pique56eba802019-08-28 15:45:25 -07002802 }
2803
Lloyd Pique6818fa52019-12-03 12:32:13 -08002804 struct ExecuteState : public CallOrderStateMachineHelper<TestType, ExecuteState> {
2805 auto execute() {
2806 getInstance()->mReadyFence = getInstance()->mOutput.composeSurfaces(kDebugRegion);
2807 return nextState<FenceCheckState>();
2808 }
2809 };
2810
2811 struct FenceCheckState : public CallOrderStateMachineHelper<TestType, FenceCheckState> {
2812 void expectNoFenceWasReturned() { EXPECT_FALSE(getInstance()->mReadyFence); }
2813
2814 void expectAFenceWasReturned() { EXPECT_TRUE(getInstance()->mReadyFence); }
2815 };
2816
2817 // Call this member function to start using the mini-DSL defined above.
2818 [[nodiscard]] auto verify() { return ExecuteState::make(this); }
2819
2820 static constexpr uint32_t kDefaultOutputOrientation = TR_IDENT;
2821 static constexpr ui::Dataspace kDefaultOutputDataspace = ui::Dataspace::UNKNOWN;
2822 static constexpr ui::Dataspace kExpensiveOutputDataspace = ui::Dataspace::DISPLAY_P3;
2823 static constexpr float kDefaultMaxLuminance = 0.9f;
2824 static constexpr float kDefaultAvgLuminance = 0.7f;
2825 static constexpr float kDefaultMinLuminance = 0.1f;
2826
2827 static const Rect kDefaultOutputFrame;
2828 static const Rect kDefaultOutputViewport;
Lloyd Piquee8fe4742020-01-21 15:26:18 -08002829 static const Rect kDefaultOutputSourceClip;
2830 static const Rect kDefaultOutputDestinationClip;
Lloyd Pique6818fa52019-12-03 12:32:13 -08002831 static const mat4 kDefaultColorTransformMat;
2832
2833 static const Region kDebugRegion;
2834 static const HdrCapabilities kHdrCapabilities;
2835
Lloyd Pique56eba802019-08-28 15:45:25 -07002836 StrictMock<mock::CompositionEngine> mCompositionEngine;
2837 StrictMock<renderengine::mock::RenderEngine> mRenderEngine;
Alec Mourie4034bb2019-11-19 12:45:54 -08002838 // TODO: make this is a proper mock.
2839 std::shared_ptr<TimeStats> mTimeStats = std::make_shared<android::impl::TimeStats>();
Lloyd Pique56eba802019-08-28 15:45:25 -07002840 mock::DisplayColorProfile* mDisplayColorProfile = new StrictMock<mock::DisplayColorProfile>();
2841 mock::RenderSurface* mRenderSurface = new StrictMock<mock::RenderSurface>();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07002842 StrictMock<OutputPartialMock> mOutput;
Lloyd Pique56eba802019-08-28 15:45:25 -07002843 sp<GraphicBuffer> mOutputBuffer = new GraphicBuffer();
Lloyd Pique6818fa52019-12-03 12:32:13 -08002844
2845 std::optional<base::unique_fd> mReadyFence;
Lloyd Pique56eba802019-08-28 15:45:25 -07002846};
2847
2848const Rect OutputComposeSurfacesTest::kDefaultOutputFrame{1001, 1002, 1003, 1004};
2849const Rect OutputComposeSurfacesTest::kDefaultOutputViewport{1005, 1006, 1007, 1008};
Lloyd Piquee8fe4742020-01-21 15:26:18 -08002850const Rect OutputComposeSurfacesTest::kDefaultOutputSourceClip{1009, 1010, 1011, 1012};
2851const Rect OutputComposeSurfacesTest::kDefaultOutputDestinationClip{1013, 1014, 1015, 1016};
Lloyd Pique0a456232020-01-16 17:51:13 -08002852const mat4 OutputComposeSurfacesTest::kDefaultColorTransformMat{mat4() * 0.5f};
Lloyd Pique6818fa52019-12-03 12:32:13 -08002853const Region OutputComposeSurfacesTest::kDebugRegion{Rect{100, 101, 102, 103}};
2854const HdrCapabilities OutputComposeSurfacesTest::
2855 kHdrCapabilities{{},
2856 OutputComposeSurfacesTest::kDefaultMaxLuminance,
2857 OutputComposeSurfacesTest::kDefaultAvgLuminance,
2858 OutputComposeSurfacesTest::kDefaultMinLuminance};
Lloyd Pique56eba802019-08-28 15:45:25 -07002859
Lloyd Piquea76ce462020-01-14 13:06:37 -08002860TEST_F(OutputComposeSurfacesTest, doesNothingButSignalNoExpensiveRenderingIfNoClientComposition) {
Lloyd Pique6818fa52019-12-03 12:32:13 -08002861 mOutput.mState.usesClientComposition = false;
Lloyd Pique56eba802019-08-28 15:45:25 -07002862
Lloyd Piquea76ce462020-01-14 13:06:37 -08002863 EXPECT_CALL(mOutput, setExpensiveRenderingExpected(false));
2864
Lloyd Pique6818fa52019-12-03 12:32:13 -08002865 verify().execute().expectAFenceWasReturned();
Lloyd Pique56eba802019-08-28 15:45:25 -07002866}
2867
Lloyd Pique6818fa52019-12-03 12:32:13 -08002868TEST_F(OutputComposeSurfacesTest, doesMinimalWorkIfDequeueBufferFails) {
2869 EXPECT_CALL(mOutput, getSkipColorTransform()).WillRepeatedly(Return(false));
2870 EXPECT_CALL(*mDisplayColorProfile, hasWideColorGamut()).WillRepeatedly(Return(true));
2871 EXPECT_CALL(mRenderEngine, supportsProtectedContent()).WillRepeatedly(Return(false));
2872 EXPECT_CALL(mOutput, generateClientCompositionRequests(_, _, kDefaultOutputDataspace))
Vishnu Nair9b079a22020-01-21 14:36:08 -08002873 .WillRepeatedly(Return(std::vector<LayerFE::LayerSettings>{}));
Lloyd Pique6818fa52019-12-03 12:32:13 -08002874 EXPECT_CALL(mOutput, appendRegionFlashRequests(RegionEq(kDebugRegion), _))
2875 .WillRepeatedly(Return());
Lloyd Pique56eba802019-08-28 15:45:25 -07002876
Lloyd Pique6818fa52019-12-03 12:32:13 -08002877 EXPECT_CALL(*mRenderSurface, dequeueBuffer(_)).WillOnce(Return(nullptr));
Lloyd Pique56eba802019-08-28 15:45:25 -07002878
Lloyd Pique6818fa52019-12-03 12:32:13 -08002879 verify().execute().expectNoFenceWasReturned();
2880}
Lloyd Pique56eba802019-08-28 15:45:25 -07002881
Lloyd Pique6818fa52019-12-03 12:32:13 -08002882TEST_F(OutputComposeSurfacesTest, handlesZeroCompositionRequests) {
2883 EXPECT_CALL(mOutput, getSkipColorTransform()).WillRepeatedly(Return(false));
2884 EXPECT_CALL(*mDisplayColorProfile, hasWideColorGamut()).WillRepeatedly(Return(true));
2885 EXPECT_CALL(mRenderEngine, supportsProtectedContent()).WillRepeatedly(Return(false));
2886 EXPECT_CALL(mOutput, generateClientCompositionRequests(_, _, kDefaultOutputDataspace))
Vishnu Nair9b079a22020-01-21 14:36:08 -08002887 .WillRepeatedly(Return(std::vector<LayerFE::LayerSettings>{}));
Lloyd Pique6818fa52019-12-03 12:32:13 -08002888 EXPECT_CALL(mOutput, appendRegionFlashRequests(RegionEq(kDebugRegion), _))
2889 .WillRepeatedly(Return());
Lloyd Pique56eba802019-08-28 15:45:25 -07002890
Lloyd Pique6818fa52019-12-03 12:32:13 -08002891 EXPECT_CALL(*mRenderSurface, dequeueBuffer(_)).WillRepeatedly(Return(mOutputBuffer));
2892 EXPECT_CALL(mRenderEngine, drawLayers(_, IsEmpty(), _, true, _, _))
2893 .WillRepeatedly(Return(NO_ERROR));
Lloyd Pique56eba802019-08-28 15:45:25 -07002894
Lloyd Pique6818fa52019-12-03 12:32:13 -08002895 verify().execute().expectAFenceWasReturned();
2896}
Lloyd Pique56eba802019-08-28 15:45:25 -07002897
Lloyd Pique6818fa52019-12-03 12:32:13 -08002898TEST_F(OutputComposeSurfacesTest, buildsAndRendersRequestList) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08002899 LayerFE::LayerSettings r1;
2900 LayerFE::LayerSettings r2;
Lloyd Pique6818fa52019-12-03 12:32:13 -08002901
2902 r1.geometry.boundaries = FloatRect{1, 2, 3, 4};
2903 r2.geometry.boundaries = FloatRect{5, 6, 7, 8};
2904
2905 EXPECT_CALL(mOutput, getSkipColorTransform()).WillRepeatedly(Return(false));
2906 EXPECT_CALL(*mDisplayColorProfile, hasWideColorGamut()).WillRepeatedly(Return(true));
2907 EXPECT_CALL(mRenderEngine, supportsProtectedContent()).WillRepeatedly(Return(false));
2908 EXPECT_CALL(mOutput, generateClientCompositionRequests(_, _, kDefaultOutputDataspace))
Vishnu Nair9b079a22020-01-21 14:36:08 -08002909 .WillRepeatedly(Return(std::vector<LayerFE::LayerSettings>{r1}));
Lloyd Pique6818fa52019-12-03 12:32:13 -08002910 EXPECT_CALL(mOutput, appendRegionFlashRequests(RegionEq(kDebugRegion), _))
2911 .WillRepeatedly(
2912 Invoke([&](const Region&,
Vishnu Nair9b079a22020-01-21 14:36:08 -08002913 std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique6818fa52019-12-03 12:32:13 -08002914 clientCompositionLayers.emplace_back(r2);
2915 }));
2916
2917 EXPECT_CALL(*mRenderSurface, dequeueBuffer(_)).WillRepeatedly(Return(mOutputBuffer));
Vishnu Nair9b079a22020-01-21 14:36:08 -08002918 EXPECT_CALL(mRenderEngine, drawLayers(_, ElementsAre(Pointee(r1), Pointee(r2)), _, true, _, _))
Lloyd Pique6818fa52019-12-03 12:32:13 -08002919 .WillRepeatedly(Return(NO_ERROR));
2920
2921 verify().execute().expectAFenceWasReturned();
2922}
2923
Vishnu Nair9b079a22020-01-21 14:36:08 -08002924TEST_F(OutputComposeSurfacesTest, renderDuplicateClientCompositionRequestsWithoutCache) {
2925 mOutput.cacheClientCompositionRequests(0);
2926 LayerFE::LayerSettings r1;
2927 LayerFE::LayerSettings r2;
2928
2929 r1.geometry.boundaries = FloatRect{1, 2, 3, 4};
2930 r2.geometry.boundaries = FloatRect{5, 6, 7, 8};
2931
2932 EXPECT_CALL(mOutput, getSkipColorTransform()).WillRepeatedly(Return(false));
2933 EXPECT_CALL(*mDisplayColorProfile, hasWideColorGamut()).WillRepeatedly(Return(true));
2934 EXPECT_CALL(mRenderEngine, supportsProtectedContent()).WillRepeatedly(Return(false));
2935 EXPECT_CALL(mOutput, generateClientCompositionRequests(_, _, kDefaultOutputDataspace))
2936 .WillRepeatedly(Return(std::vector<LayerFE::LayerSettings>{r1, r2}));
2937 EXPECT_CALL(mOutput, appendRegionFlashRequests(RegionEq(kDebugRegion), _))
2938 .WillRepeatedly(Return());
2939
2940 EXPECT_CALL(*mRenderSurface, dequeueBuffer(_)).WillRepeatedly(Return(mOutputBuffer));
2941 EXPECT_CALL(mRenderEngine, drawLayers(_, ElementsAre(Pointee(r1), Pointee(r2)), _, true, _, _))
2942 .Times(2)
2943 .WillOnce(Return(NO_ERROR));
2944
2945 verify().execute().expectAFenceWasReturned();
2946 EXPECT_FALSE(mOutput.mState.reusedClientComposition);
2947
2948 verify().execute().expectAFenceWasReturned();
2949 EXPECT_FALSE(mOutput.mState.reusedClientComposition);
2950}
2951
2952TEST_F(OutputComposeSurfacesTest, skipDuplicateClientCompositionRequests) {
2953 mOutput.cacheClientCompositionRequests(3);
2954 LayerFE::LayerSettings r1;
2955 LayerFE::LayerSettings r2;
2956
2957 r1.geometry.boundaries = FloatRect{1, 2, 3, 4};
2958 r2.geometry.boundaries = FloatRect{5, 6, 7, 8};
2959
2960 EXPECT_CALL(mOutput, getSkipColorTransform()).WillRepeatedly(Return(false));
2961 EXPECT_CALL(*mDisplayColorProfile, hasWideColorGamut()).WillRepeatedly(Return(true));
2962 EXPECT_CALL(mRenderEngine, supportsProtectedContent()).WillRepeatedly(Return(false));
2963 EXPECT_CALL(mOutput, generateClientCompositionRequests(_, _, kDefaultOutputDataspace))
2964 .WillRepeatedly(Return(std::vector<LayerFE::LayerSettings>{r1, r2}));
2965 EXPECT_CALL(mOutput, appendRegionFlashRequests(RegionEq(kDebugRegion), _))
2966 .WillRepeatedly(Return());
2967
2968 EXPECT_CALL(*mRenderSurface, dequeueBuffer(_)).WillRepeatedly(Return(mOutputBuffer));
2969 EXPECT_CALL(mRenderEngine, drawLayers(_, ElementsAre(Pointee(r1), Pointee(r2)), _, true, _, _))
2970 .WillOnce(Return(NO_ERROR));
2971 EXPECT_CALL(mOutput, setExpensiveRenderingExpected(false));
2972
2973 verify().execute().expectAFenceWasReturned();
2974 EXPECT_FALSE(mOutput.mState.reusedClientComposition);
2975
2976 // We do not expect another call to draw layers.
2977 verify().execute().expectAFenceWasReturned();
2978 EXPECT_TRUE(mOutput.mState.reusedClientComposition);
2979}
2980
2981TEST_F(OutputComposeSurfacesTest, clientCompositionIfBufferChanges) {
2982 LayerFE::LayerSettings r1;
2983 LayerFE::LayerSettings r2;
2984
2985 r1.geometry.boundaries = FloatRect{1, 2, 3, 4};
2986 r2.geometry.boundaries = FloatRect{5, 6, 7, 8};
2987
2988 EXPECT_CALL(mOutput, getSkipColorTransform()).WillRepeatedly(Return(false));
2989 EXPECT_CALL(*mDisplayColorProfile, hasWideColorGamut()).WillRepeatedly(Return(true));
2990 EXPECT_CALL(mRenderEngine, supportsProtectedContent()).WillRepeatedly(Return(false));
2991 EXPECT_CALL(mOutput, generateClientCompositionRequests(_, _, kDefaultOutputDataspace))
2992 .WillRepeatedly(Return(std::vector<LayerFE::LayerSettings>{r1, r2}));
2993 EXPECT_CALL(mOutput, appendRegionFlashRequests(RegionEq(kDebugRegion), _))
2994 .WillRepeatedly(Return());
2995
2996 sp<GraphicBuffer> otherOutputBuffer = new GraphicBuffer();
2997 EXPECT_CALL(*mRenderSurface, dequeueBuffer(_))
2998 .WillOnce(Return(mOutputBuffer))
2999 .WillOnce(Return(otherOutputBuffer));
3000 EXPECT_CALL(mRenderEngine, drawLayers(_, ElementsAre(Pointee(r1), Pointee(r2)), _, true, _, _))
3001 .WillRepeatedly(Return(NO_ERROR));
3002
3003 verify().execute().expectAFenceWasReturned();
3004 EXPECT_FALSE(mOutput.mState.reusedClientComposition);
3005
3006 verify().execute().expectAFenceWasReturned();
3007 EXPECT_FALSE(mOutput.mState.reusedClientComposition);
3008}
3009
3010TEST_F(OutputComposeSurfacesTest, clientCompositionIfRequestChanges) {
3011 LayerFE::LayerSettings r1;
3012 LayerFE::LayerSettings r2;
3013 LayerFE::LayerSettings r3;
3014
3015 r1.geometry.boundaries = FloatRect{1, 2, 3, 4};
3016 r2.geometry.boundaries = FloatRect{5, 6, 7, 8};
3017 r3.geometry.boundaries = FloatRect{5, 6, 7, 9};
3018
3019 EXPECT_CALL(mOutput, getSkipColorTransform()).WillRepeatedly(Return(false));
3020 EXPECT_CALL(*mDisplayColorProfile, hasWideColorGamut()).WillRepeatedly(Return(true));
3021 EXPECT_CALL(mRenderEngine, supportsProtectedContent()).WillRepeatedly(Return(false));
3022 EXPECT_CALL(mOutput, generateClientCompositionRequests(_, _, kDefaultOutputDataspace))
3023 .WillOnce(Return(std::vector<LayerFE::LayerSettings>{r1, r2}))
3024 .WillOnce(Return(std::vector<LayerFE::LayerSettings>{r1, r3}));
3025 EXPECT_CALL(mOutput, appendRegionFlashRequests(RegionEq(kDebugRegion), _))
3026 .WillRepeatedly(Return());
3027
3028 EXPECT_CALL(*mRenderSurface, dequeueBuffer(_)).WillRepeatedly(Return(mOutputBuffer));
3029 EXPECT_CALL(mRenderEngine, drawLayers(_, ElementsAre(Pointee(r1), Pointee(r2)), _, true, _, _))
3030 .WillOnce(Return(NO_ERROR));
3031 EXPECT_CALL(mRenderEngine, drawLayers(_, ElementsAre(Pointee(r1), Pointee(r3)), _, true, _, _))
3032 .WillOnce(Return(NO_ERROR));
3033
3034 verify().execute().expectAFenceWasReturned();
3035 EXPECT_FALSE(mOutput.mState.reusedClientComposition);
3036
3037 verify().execute().expectAFenceWasReturned();
3038 EXPECT_FALSE(mOutput.mState.reusedClientComposition);
3039}
3040
Lloyd Pique6818fa52019-12-03 12:32:13 -08003041struct OutputComposeSurfacesTest_UsesExpectedDisplaySettings : public OutputComposeSurfacesTest {
3042 OutputComposeSurfacesTest_UsesExpectedDisplaySettings() {
3043 EXPECT_CALL(mRenderEngine, supportsProtectedContent()).WillRepeatedly(Return(false));
3044 EXPECT_CALL(mOutput, generateClientCompositionRequests(_, _, kDefaultOutputDataspace))
Vishnu Nair9b079a22020-01-21 14:36:08 -08003045 .WillRepeatedly(Return(std::vector<LayerFE::LayerSettings>{}));
Lloyd Pique6818fa52019-12-03 12:32:13 -08003046 EXPECT_CALL(mOutput, appendRegionFlashRequests(RegionEq(kDebugRegion), _))
3047 .WillRepeatedly(Return());
3048 EXPECT_CALL(*mRenderSurface, dequeueBuffer(_)).WillRepeatedly(Return(mOutputBuffer));
3049 }
3050
3051 struct MixedCompositionState
3052 : public CallOrderStateMachineHelper<TestType, MixedCompositionState> {
3053 auto ifMixedCompositionIs(bool used) {
3054 getInstance()->mOutput.mState.usesDeviceComposition = used;
3055 return nextState<OutputUsesHdrState>();
3056 }
3057 };
3058
3059 struct OutputUsesHdrState : public CallOrderStateMachineHelper<TestType, OutputUsesHdrState> {
3060 auto andIfUsesHdr(bool used) {
3061 EXPECT_CALL(*getInstance()->mDisplayColorProfile, hasWideColorGamut())
3062 .WillOnce(Return(used));
3063 return nextState<SkipColorTransformState>();
3064 }
3065 };
3066
3067 struct SkipColorTransformState
3068 : public CallOrderStateMachineHelper<TestType, SkipColorTransformState> {
3069 auto andIfSkipColorTransform(bool skip) {
3070 // May be called zero or one times.
3071 EXPECT_CALL(getInstance()->mOutput, getSkipColorTransform())
3072 .WillRepeatedly(Return(skip));
3073 return nextState<ExpectDisplaySettingsState>();
3074 }
3075 };
3076
3077 struct ExpectDisplaySettingsState
3078 : public CallOrderStateMachineHelper<TestType, ExpectDisplaySettingsState> {
3079 auto thenExpectDisplaySettingsUsed(renderengine::DisplaySettings settings) {
3080 EXPECT_CALL(getInstance()->mRenderEngine, drawLayers(settings, _, _, true, _, _))
3081 .WillOnce(Return(NO_ERROR));
3082 return nextState<ExecuteState>();
3083 }
3084 };
3085
3086 // Call this member function to start using the mini-DSL defined above.
3087 [[nodiscard]] auto verify() { return MixedCompositionState::make(this); }
3088};
3089
3090TEST_F(OutputComposeSurfacesTest_UsesExpectedDisplaySettings, forHdrMixedComposition) {
3091 verify().ifMixedCompositionIs(true)
3092 .andIfUsesHdr(true)
3093 .andIfSkipColorTransform(false)
Lloyd Piquee8fe4742020-01-21 15:26:18 -08003094 .thenExpectDisplaySettingsUsed({kDefaultOutputDestinationClip, kDefaultOutputSourceClip,
3095 mat4(), kDefaultMaxLuminance, kDefaultOutputDataspace,
3096 mat4(), Region::INVALID_REGION,
3097 kDefaultOutputOrientation})
Lloyd Pique6818fa52019-12-03 12:32:13 -08003098 .execute()
3099 .expectAFenceWasReturned();
3100}
3101
3102TEST_F(OutputComposeSurfacesTest_UsesExpectedDisplaySettings, forNonHdrMixedComposition) {
3103 verify().ifMixedCompositionIs(true)
3104 .andIfUsesHdr(false)
3105 .andIfSkipColorTransform(false)
Lloyd Piquee8fe4742020-01-21 15:26:18 -08003106 .thenExpectDisplaySettingsUsed({kDefaultOutputDestinationClip, kDefaultOutputSourceClip,
3107 mat4(), kDefaultMaxLuminance, kDefaultOutputDataspace,
3108 mat4(), Region::INVALID_REGION,
3109 kDefaultOutputOrientation})
Lloyd Pique6818fa52019-12-03 12:32:13 -08003110 .execute()
3111 .expectAFenceWasReturned();
3112}
3113
3114TEST_F(OutputComposeSurfacesTest_UsesExpectedDisplaySettings, forHdrOnlyClientComposition) {
3115 verify().ifMixedCompositionIs(false)
3116 .andIfUsesHdr(true)
3117 .andIfSkipColorTransform(false)
Lloyd Piquee8fe4742020-01-21 15:26:18 -08003118 .thenExpectDisplaySettingsUsed({kDefaultOutputDestinationClip, kDefaultOutputSourceClip,
3119 mat4(), kDefaultMaxLuminance, kDefaultOutputDataspace,
Lloyd Pique6818fa52019-12-03 12:32:13 -08003120 kDefaultColorTransformMat, Region::INVALID_REGION,
3121 kDefaultOutputOrientation})
3122 .execute()
3123 .expectAFenceWasReturned();
3124}
3125
3126TEST_F(OutputComposeSurfacesTest_UsesExpectedDisplaySettings, forNonHdrOnlyClientComposition) {
3127 verify().ifMixedCompositionIs(false)
3128 .andIfUsesHdr(false)
3129 .andIfSkipColorTransform(false)
Lloyd Piquee8fe4742020-01-21 15:26:18 -08003130 .thenExpectDisplaySettingsUsed({kDefaultOutputDestinationClip, kDefaultOutputSourceClip,
3131 mat4(), kDefaultMaxLuminance, kDefaultOutputDataspace,
Lloyd Pique6818fa52019-12-03 12:32:13 -08003132 kDefaultColorTransformMat, Region::INVALID_REGION,
3133 kDefaultOutputOrientation})
3134 .execute()
3135 .expectAFenceWasReturned();
3136}
3137
3138TEST_F(OutputComposeSurfacesTest_UsesExpectedDisplaySettings,
3139 usesExpectedDisplaySettingsForHdrOnlyClientCompositionWithSkipClientTransform) {
3140 verify().ifMixedCompositionIs(false)
3141 .andIfUsesHdr(true)
3142 .andIfSkipColorTransform(true)
Lloyd Piquee8fe4742020-01-21 15:26:18 -08003143 .thenExpectDisplaySettingsUsed({kDefaultOutputDestinationClip, kDefaultOutputSourceClip,
3144 mat4(), kDefaultMaxLuminance, kDefaultOutputDataspace,
3145 mat4(), Region::INVALID_REGION,
3146 kDefaultOutputOrientation})
Lloyd Pique6818fa52019-12-03 12:32:13 -08003147 .execute()
3148 .expectAFenceWasReturned();
3149}
3150
3151struct OutputComposeSurfacesTest_HandlesProtectedContent : public OutputComposeSurfacesTest {
3152 struct Layer {
3153 Layer() {
3154 EXPECT_CALL(mOutputLayer, getLayer()).WillRepeatedly(ReturnRef(mLayer));
3155 EXPECT_CALL(mLayer, getFEState()).WillRepeatedly(ReturnRef(mLayerFEState));
3156 }
3157
3158 StrictMock<mock::OutputLayer> mOutputLayer;
3159 StrictMock<mock::Layer> mLayer;
3160 LayerFECompositionState mLayerFEState;
3161 };
3162
3163 OutputComposeSurfacesTest_HandlesProtectedContent() {
3164 mLayer1.mLayerFEState.hasProtectedContent = false;
3165 mLayer2.mLayerFEState.hasProtectedContent = false;
3166
3167 EXPECT_CALL(mOutput, getOutputLayerCount()).WillRepeatedly(Return(2u));
3168 EXPECT_CALL(mOutput, getOutputLayerOrderedByZByIndex(0u))
3169 .WillRepeatedly(Return(&mLayer1.mOutputLayer));
3170 EXPECT_CALL(mOutput, getOutputLayerOrderedByZByIndex(1u))
3171 .WillRepeatedly(Return(&mLayer2.mOutputLayer));
3172
3173 EXPECT_CALL(mOutput, getSkipColorTransform()).WillRepeatedly(Return(false));
3174
3175 EXPECT_CALL(*mDisplayColorProfile, hasWideColorGamut()).WillRepeatedly(Return(true));
3176
3177 EXPECT_CALL(mOutput, generateClientCompositionRequests(_, _, _))
Vishnu Nair9b079a22020-01-21 14:36:08 -08003178 .WillRepeatedly(Return(std::vector<LayerFE::LayerSettings>{}));
Lloyd Pique6818fa52019-12-03 12:32:13 -08003179 EXPECT_CALL(mOutput, appendRegionFlashRequests(RegionEq(kDebugRegion), _))
3180 .WillRepeatedly(Return());
3181 EXPECT_CALL(*mRenderSurface, dequeueBuffer(_)).WillRepeatedly(Return(mOutputBuffer));
3182 EXPECT_CALL(mRenderEngine, drawLayers(_, _, _, true, _, _))
3183 .WillRepeatedly(Return(NO_ERROR));
3184 }
3185
3186 Layer mLayer1;
3187 Layer mLayer2;
3188};
3189
3190TEST_F(OutputComposeSurfacesTest_HandlesProtectedContent, ifDisplayIsNotSecure) {
3191 mOutput.mState.isSecure = false;
3192 mLayer2.mLayerFEState.hasProtectedContent = true;
3193 EXPECT_CALL(mRenderEngine, supportsProtectedContent()).WillRepeatedly(Return(true));
3194
3195 mOutput.composeSurfaces(kDebugRegion);
3196}
3197
3198TEST_F(OutputComposeSurfacesTest_HandlesProtectedContent, ifRenderEngineDoesNotSupportIt) {
3199 mOutput.mState.isSecure = true;
3200 mLayer2.mLayerFEState.hasProtectedContent = true;
3201 EXPECT_CALL(mRenderEngine, supportsProtectedContent()).WillRepeatedly(Return(false));
3202
3203 mOutput.composeSurfaces(kDebugRegion);
3204}
3205
3206TEST_F(OutputComposeSurfacesTest_HandlesProtectedContent, ifNoProtectedContentLayers) {
3207 mOutput.mState.isSecure = true;
3208 mLayer2.mLayerFEState.hasProtectedContent = false;
3209 EXPECT_CALL(mRenderEngine, supportsProtectedContent()).WillRepeatedly(Return(true));
3210 EXPECT_CALL(mRenderEngine, isProtected).WillOnce(Return(true)).WillOnce(Return(false));
3211 EXPECT_CALL(*mRenderSurface, isProtected).WillOnce(Return(true));
3212 EXPECT_CALL(mRenderEngine, useProtectedContext(false));
3213 EXPECT_CALL(*mRenderSurface, setProtected(false));
3214
3215 mOutput.composeSurfaces(kDebugRegion);
3216}
3217
3218TEST_F(OutputComposeSurfacesTest_HandlesProtectedContent, ifNotEnabled) {
3219 mOutput.mState.isSecure = true;
3220 mLayer2.mLayerFEState.hasProtectedContent = true;
3221 EXPECT_CALL(mRenderEngine, supportsProtectedContent()).WillRepeatedly(Return(true));
3222
3223 // For this test, we also check the call order of key functions.
3224 InSequence seq;
3225
3226 EXPECT_CALL(mRenderEngine, isProtected).WillOnce(Return(false));
3227 EXPECT_CALL(mRenderEngine, useProtectedContext(true));
3228 EXPECT_CALL(*mRenderSurface, isProtected).WillOnce(Return(false));
3229 EXPECT_CALL(mRenderEngine, isProtected).WillOnce(Return(true));
3230 EXPECT_CALL(*mRenderSurface, setProtected(true));
3231 // Must happen after setting the protected content state.
3232 EXPECT_CALL(*mRenderSurface, dequeueBuffer(_)).WillRepeatedly(Return(mOutputBuffer));
3233 EXPECT_CALL(mRenderEngine, drawLayers(_, _, _, true, _, _)).WillOnce(Return(NO_ERROR));
3234
3235 mOutput.composeSurfaces(kDebugRegion);
3236}
3237
3238TEST_F(OutputComposeSurfacesTest_HandlesProtectedContent, ifAlreadyEnabledEverywhere) {
3239 mOutput.mState.isSecure = true;
3240 mLayer2.mLayerFEState.hasProtectedContent = true;
3241 EXPECT_CALL(mRenderEngine, supportsProtectedContent()).WillRepeatedly(Return(true));
3242 EXPECT_CALL(mRenderEngine, isProtected).WillOnce(Return(true));
3243 EXPECT_CALL(*mRenderSurface, isProtected).WillOnce(Return(true));
3244
3245 mOutput.composeSurfaces(kDebugRegion);
3246}
3247
3248TEST_F(OutputComposeSurfacesTest_HandlesProtectedContent, ifFailsToEnableInRenderEngine) {
3249 mOutput.mState.isSecure = true;
3250 mLayer2.mLayerFEState.hasProtectedContent = true;
3251 EXPECT_CALL(mRenderEngine, supportsProtectedContent()).WillRepeatedly(Return(true));
3252 EXPECT_CALL(mRenderEngine, isProtected).WillOnce(Return(false)).WillOnce(Return(false));
3253 EXPECT_CALL(*mRenderSurface, isProtected).WillOnce(Return(false));
3254 EXPECT_CALL(mRenderEngine, useProtectedContext(true));
3255
3256 mOutput.composeSurfaces(kDebugRegion);
3257}
3258
3259TEST_F(OutputComposeSurfacesTest_HandlesProtectedContent, ifAlreadyEnabledInRenderEngine) {
3260 mOutput.mState.isSecure = true;
3261 mLayer2.mLayerFEState.hasProtectedContent = true;
3262 EXPECT_CALL(mRenderEngine, supportsProtectedContent()).WillRepeatedly(Return(true));
3263 EXPECT_CALL(mRenderEngine, isProtected).WillOnce(Return(true)).WillOnce(Return(true));
3264 EXPECT_CALL(*mRenderSurface, isProtected).WillOnce(Return(false));
3265 EXPECT_CALL(*mRenderSurface, setProtected(true));
3266
3267 mOutput.composeSurfaces(kDebugRegion);
3268}
3269
3270TEST_F(OutputComposeSurfacesTest_HandlesProtectedContent, ifAlreadyEnabledInRenderSurface) {
3271 mOutput.mState.isSecure = true;
3272 mLayer2.mLayerFEState.hasProtectedContent = true;
3273 EXPECT_CALL(mRenderEngine, supportsProtectedContent()).WillRepeatedly(Return(true));
3274 EXPECT_CALL(mRenderEngine, isProtected).WillOnce(Return(false));
3275 EXPECT_CALL(*mRenderSurface, isProtected).WillOnce(Return(true));
3276 EXPECT_CALL(mRenderEngine, useProtectedContext(true));
3277
3278 mOutput.composeSurfaces(kDebugRegion);
3279}
3280
3281struct OutputComposeSurfacesTest_SetsExpensiveRendering : public OutputComposeSurfacesTest {
3282 OutputComposeSurfacesTest_SetsExpensiveRendering() {
3283 EXPECT_CALL(mOutput, getSkipColorTransform()).WillRepeatedly(Return(false));
3284 EXPECT_CALL(*mDisplayColorProfile, hasWideColorGamut()).WillRepeatedly(Return(true));
3285 EXPECT_CALL(mRenderEngine, supportsProtectedContent()).WillRepeatedly(Return(false));
3286 EXPECT_CALL(mOutput, appendRegionFlashRequests(RegionEq(kDebugRegion), _))
3287 .WillRepeatedly(Return());
3288 EXPECT_CALL(*mRenderSurface, dequeueBuffer(_)).WillRepeatedly(Return(mOutputBuffer));
3289 }
3290};
3291
3292TEST_F(OutputComposeSurfacesTest_SetsExpensiveRendering, IfExepensiveOutputDataspaceIsUsed) {
3293 mOutput.mState.dataspace = kExpensiveOutputDataspace;
3294
3295 EXPECT_CALL(mOutput, generateClientCompositionRequests(_, _, kExpensiveOutputDataspace))
Vishnu Nair9b079a22020-01-21 14:36:08 -08003296 .WillOnce(Return(std::vector<LayerFE::LayerSettings>{}));
Lloyd Pique6818fa52019-12-03 12:32:13 -08003297
3298 // For this test, we also check the call order of key functions.
3299 InSequence seq;
3300
3301 EXPECT_CALL(mOutput, setExpensiveRenderingExpected(true));
3302 EXPECT_CALL(mRenderEngine, drawLayers(_, _, _, true, _, _)).WillOnce(Return(NO_ERROR));
Lloyd Pique6818fa52019-12-03 12:32:13 -08003303
3304 mOutput.composeSurfaces(kDebugRegion);
Lloyd Pique56eba802019-08-28 15:45:25 -07003305}
3306
3307/*
3308 * Output::generateClientCompositionRequests()
3309 */
3310
3311struct GenerateClientCompositionRequestsTest : public testing::Test {
Lloyd Piquefaa3f192019-11-14 14:05:09 -08003312 struct OutputPartialMock : public OutputPartialMockBase {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07003313 // compositionengine::Output overrides
Vishnu Nair9b079a22020-01-21 14:36:08 -08003314 std::vector<LayerFE::LayerSettings> generateClientCompositionRequests(
Vishnu Nair3a7346c2019-12-04 08:09:09 -08003315 bool supportsProtectedContent, Region& clearRegion,
3316 ui::Dataspace dataspace) override {
Lloyd Pique56eba802019-08-28 15:45:25 -07003317 return impl::Output::generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -08003318 clearRegion, dataspace);
Lloyd Pique56eba802019-08-28 15:45:25 -07003319 }
3320 };
3321
Lloyd Piquea4863342019-12-04 18:45:02 -08003322 struct Layer {
3323 Layer() {
3324 EXPECT_CALL(mOutputLayer, getState()).WillRepeatedly(ReturnRef(mOutputLayerState));
3325 EXPECT_CALL(mOutputLayer, editState()).WillRepeatedly(ReturnRef(mOutputLayerState));
3326 EXPECT_CALL(mOutputLayer, getLayer()).WillRepeatedly(ReturnRef(mLayer));
3327 EXPECT_CALL(mOutputLayer, getLayerFE()).WillRepeatedly(ReturnRef(mLayerFE));
3328 EXPECT_CALL(mLayer, getFEState()).WillRepeatedly(ReturnRef(mLayerFEState));
3329 }
3330
3331 StrictMock<mock::OutputLayer> mOutputLayer;
3332 StrictMock<mock::Layer> mLayer;
3333 StrictMock<mock::LayerFE> mLayerFE;
3334 LayerFECompositionState mLayerFEState;
3335 impl::OutputLayerCompositionState mOutputLayerState;
Vishnu Nair9b079a22020-01-21 14:36:08 -08003336 LayerFE::LayerSettings mLayerSettings;
Lloyd Piquea4863342019-12-04 18:45:02 -08003337 };
3338
Lloyd Pique56eba802019-08-28 15:45:25 -07003339 GenerateClientCompositionRequestsTest() {
Lloyd Piquea4863342019-12-04 18:45:02 -08003340 mOutput.mState.needsFiltering = false;
3341
Lloyd Pique56eba802019-08-28 15:45:25 -07003342 mOutput.setDisplayColorProfileForTest(
3343 std::unique_ptr<DisplayColorProfile>(mDisplayColorProfile));
3344 mOutput.setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(mRenderSurface));
3345 }
3346
Lloyd Pique56eba802019-08-28 15:45:25 -07003347 mock::DisplayColorProfile* mDisplayColorProfile = new StrictMock<mock::DisplayColorProfile>();
3348 mock::RenderSurface* mRenderSurface = new StrictMock<mock::RenderSurface>();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07003349 StrictMock<OutputPartialMock> mOutput;
Lloyd Pique56eba802019-08-28 15:45:25 -07003350};
3351
Lloyd Piquea4863342019-12-04 18:45:02 -08003352struct GenerateClientCompositionRequestsTest_ThreeLayers
3353 : public GenerateClientCompositionRequestsTest {
3354 GenerateClientCompositionRequestsTest_ThreeLayers() {
3355 mOutput.mState.frame = kDisplayFrame;
3356 mOutput.mState.viewport = kDisplayViewport;
Lloyd Piquee8fe4742020-01-21 15:26:18 -08003357 mOutput.mState.sourceClip = kDisplaySourceClip;
3358 mOutput.mState.destinationClip = kDisplayDestinationClip;
Lloyd Piquea4863342019-12-04 18:45:02 -08003359 mOutput.mState.transform = ui::Transform{kDisplayOrientation};
3360 mOutput.mState.orientation = kDisplayOrientation;
3361 mOutput.mState.needsFiltering = false;
3362 mOutput.mState.isSecure = false;
Lloyd Pique56eba802019-08-28 15:45:25 -07003363
Lloyd Piquea4863342019-12-04 18:45:02 -08003364 for (size_t i = 0; i < mLayers.size(); i++) {
3365 mLayers[i].mOutputLayerState.clearClientTarget = false;
3366 mLayers[i].mOutputLayerState.visibleRegion = Region(kDisplayFrame);
3367 mLayers[i].mLayerFEState.isOpaque = true;
Vishnu Nair9b079a22020-01-21 14:36:08 -08003368 mLayers[i].mLayerSettings.geometry.boundaries =
Lloyd Piquea4863342019-12-04 18:45:02 -08003369 FloatRect{static_cast<float>(i + 1), 0.f, 0.f, 0.f};
Vishnu Nair9b079a22020-01-21 14:36:08 -08003370 mLayers[i].mLayerSettings.source.solidColor = {1.0f, 1.0f, 1.0f};
3371 mLayers[i].mLayerSettings.alpha = 1.0f;
3372 mLayers[i].mLayerSettings.disableBlending = false;
Lloyd Pique56eba802019-08-28 15:45:25 -07003373
Lloyd Piquea4863342019-12-04 18:45:02 -08003374 EXPECT_CALL(mOutput, getOutputLayerOrderedByZByIndex(i))
3375 .WillRepeatedly(Return(&mLayers[i].mOutputLayer));
3376 EXPECT_CALL(mLayers[i].mOutputLayer, requiresClientComposition())
3377 .WillRepeatedly(Return(true));
3378 EXPECT_CALL(mLayers[i].mOutputLayer, needsFiltering()).WillRepeatedly(Return(false));
3379 }
Lloyd Pique56eba802019-08-28 15:45:25 -07003380
Lloyd Piquea4863342019-12-04 18:45:02 -08003381 EXPECT_CALL(mOutput, getOutputLayerCount()).WillRepeatedly(Return(mLayers.size()));
3382 }
Lloyd Pique56eba802019-08-28 15:45:25 -07003383
Lloyd Piquea4863342019-12-04 18:45:02 -08003384 static constexpr uint32_t kDisplayOrientation = TR_IDENT;
3385 static constexpr ui::Dataspace kDisplayDataspace = ui::Dataspace::UNKNOWN;
Lloyd Pique56eba802019-08-28 15:45:25 -07003386
Lloyd Piquea4863342019-12-04 18:45:02 -08003387 static const Rect kDisplayFrame;
3388 static const Rect kDisplayViewport;
Lloyd Piquee8fe4742020-01-21 15:26:18 -08003389 static const Rect kDisplaySourceClip;
3390 static const Rect kDisplayDestinationClip;
Lloyd Pique56eba802019-08-28 15:45:25 -07003391
Lloyd Piquea4863342019-12-04 18:45:02 -08003392 std::array<Layer, 3> mLayers;
3393};
Lloyd Pique56eba802019-08-28 15:45:25 -07003394
Lloyd Piquea4863342019-12-04 18:45:02 -08003395const Rect GenerateClientCompositionRequestsTest_ThreeLayers::kDisplayFrame(0, 0, 100, 200);
3396const Rect GenerateClientCompositionRequestsTest_ThreeLayers::kDisplayViewport(0, 0, 101, 201);
Lloyd Piquee8fe4742020-01-21 15:26:18 -08003397const Rect GenerateClientCompositionRequestsTest_ThreeLayers::kDisplaySourceClip(0, 0, 102, 202);
3398const Rect GenerateClientCompositionRequestsTest_ThreeLayers::kDisplayDestinationClip(0, 0, 103,
3399 203);
Lloyd Pique56eba802019-08-28 15:45:25 -07003400
Lloyd Piquea4863342019-12-04 18:45:02 -08003401TEST_F(GenerateClientCompositionRequestsTest_ThreeLayers, handlesNoClientCompostionLayers) {
3402 EXPECT_CALL(mLayers[0].mOutputLayer, requiresClientComposition()).WillOnce(Return(false));
3403 EXPECT_CALL(mLayers[1].mOutputLayer, requiresClientComposition()).WillOnce(Return(false));
3404 EXPECT_CALL(mLayers[2].mOutputLayer, requiresClientComposition()).WillOnce(Return(false));
Lloyd Pique56eba802019-08-28 15:45:25 -07003405
Lloyd Piquea4863342019-12-04 18:45:02 -08003406 Region accumClearRegion(Rect(10, 11, 12, 13));
3407 auto requests = mOutput.generateClientCompositionRequests(false /* supportsProtectedContent */,
3408 accumClearRegion, kDisplayDataspace);
Lloyd Pique56eba802019-08-28 15:45:25 -07003409 EXPECT_EQ(0u, requests.size());
Lloyd Piquea4863342019-12-04 18:45:02 -08003410 EXPECT_THAT(accumClearRegion, RegionEq(Region(Rect(10, 11, 12, 13))));
Lloyd Pique56eba802019-08-28 15:45:25 -07003411}
3412
Lloyd Piquea4863342019-12-04 18:45:02 -08003413TEST_F(GenerateClientCompositionRequestsTest_ThreeLayers, requiresVisibleRegionAfterViewportClip) {
3414 mLayers[0].mOutputLayerState.visibleRegion = Region(Rect(10, 10, 10, 10));
3415 mLayers[1].mOutputLayerState.visibleRegion = Region(Rect(4000, 0, 4010, 10));
3416 mLayers[2].mOutputLayerState.visibleRegion = Region(Rect(-10, -10, 0, 0));
3417
3418 Region accumClearRegion(Rect(10, 11, 12, 13));
3419 auto requests = mOutput.generateClientCompositionRequests(false /* supportsProtectedContent */,
3420 accumClearRegion, kDisplayDataspace);
3421 EXPECT_EQ(0u, requests.size());
3422 EXPECT_THAT(accumClearRegion, RegionEq(Region(Rect(10, 11, 12, 13))));
3423}
3424
3425TEST_F(GenerateClientCompositionRequestsTest_ThreeLayers, gathersClientCompositionRequests) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08003426 LayerFE::LayerSettings mShadowSettings;
3427 mShadowSettings.source.solidColor = {0.1f, 0.1f, 0.1f};
Lloyd Piquea4863342019-12-04 18:45:02 -08003428
3429 EXPECT_CALL(mLayers[0].mLayerFE, prepareClientComposition(_)).WillOnce(Return(std::nullopt));
3430 EXPECT_CALL(mLayers[1].mLayerFE, prepareClientComposition(_))
Vishnu Nair9b079a22020-01-21 14:36:08 -08003431 .WillOnce(Return(mLayers[1].mLayerSettings));
Lloyd Piquea4863342019-12-04 18:45:02 -08003432 EXPECT_CALL(mLayers[1].mLayerFE,
Vishnu Nair9b079a22020-01-21 14:36:08 -08003433 prepareShadowClientComposition(mLayers[1].mLayerSettings, kDisplayViewport,
Lloyd Piquea4863342019-12-04 18:45:02 -08003434 kDisplayDataspace))
3435 .WillOnce(Return(std::nullopt));
3436 EXPECT_CALL(mLayers[2].mLayerFE, prepareClientComposition(_))
Vishnu Nair9b079a22020-01-21 14:36:08 -08003437 .WillOnce(Return(mLayers[2].mLayerSettings));
Lloyd Piquea4863342019-12-04 18:45:02 -08003438 EXPECT_CALL(mLayers[2].mLayerFE,
Vishnu Nair9b079a22020-01-21 14:36:08 -08003439 prepareShadowClientComposition(mLayers[2].mLayerSettings, kDisplayViewport,
Lloyd Piquea4863342019-12-04 18:45:02 -08003440 kDisplayDataspace))
Vishnu Nair9b079a22020-01-21 14:36:08 -08003441 .WillOnce(Return(mShadowSettings));
Lloyd Piquea4863342019-12-04 18:45:02 -08003442
3443 Region accumClearRegion(Rect(10, 11, 12, 13));
3444 auto requests = mOutput.generateClientCompositionRequests(false /* supportsProtectedContent */,
3445 accumClearRegion, kDisplayDataspace);
3446 ASSERT_EQ(3u, requests.size());
Vishnu Nair9b079a22020-01-21 14:36:08 -08003447 EXPECT_EQ(mLayers[1].mLayerSettings, requests[0]);
3448 EXPECT_EQ(mShadowSettings, requests[1]);
3449 EXPECT_EQ(mLayers[2].mLayerSettings, requests[2]);
Lloyd Piquea4863342019-12-04 18:45:02 -08003450
3451 EXPECT_THAT(accumClearRegion, RegionEq(Region(Rect(10, 11, 12, 13))));
3452
3453 // Check that a timestamp was set for the layers that generated requests
3454 EXPECT_TRUE(0 == mLayers[0].mOutputLayerState.clientCompositionTimestamp);
3455 EXPECT_TRUE(0 != mLayers[1].mOutputLayerState.clientCompositionTimestamp);
3456 EXPECT_TRUE(0 != mLayers[2].mOutputLayerState.clientCompositionTimestamp);
3457}
3458
3459TEST_F(GenerateClientCompositionRequestsTest_ThreeLayers,
3460 onlyClientComposesClientComposedLayersIfNoClearingNeeded) {
3461 EXPECT_CALL(mLayers[0].mOutputLayer, requiresClientComposition()).WillOnce(Return(false));
3462 EXPECT_CALL(mLayers[1].mOutputLayer, requiresClientComposition()).WillOnce(Return(false));
3463 EXPECT_CALL(mLayers[2].mOutputLayer, requiresClientComposition()).WillOnce(Return(true));
3464
3465 mLayers[0].mOutputLayerState.clearClientTarget = false;
3466 mLayers[1].mOutputLayerState.clearClientTarget = false;
3467 mLayers[2].mOutputLayerState.clearClientTarget = false;
3468
3469 mLayers[0].mLayerFEState.isOpaque = true;
3470 mLayers[1].mLayerFEState.isOpaque = true;
3471 mLayers[2].mLayerFEState.isOpaque = true;
3472
3473 EXPECT_CALL(mLayers[2].mLayerFE, prepareClientComposition(_))
Vishnu Nair9b079a22020-01-21 14:36:08 -08003474 .WillOnce(Return(mLayers[2].mLayerSettings));
Lloyd Piquea4863342019-12-04 18:45:02 -08003475 EXPECT_CALL(mLayers[2].mLayerFE, prepareShadowClientComposition(_, _, _))
3476 .WillOnce(Return(std::nullopt));
3477
3478 Region accumClearRegion(Rect(10, 11, 12, 13));
3479 auto requests = mOutput.generateClientCompositionRequests(false /* supportsProtectedContent */,
3480 accumClearRegion, kDisplayDataspace);
3481 ASSERT_EQ(1u, requests.size());
Vishnu Nair9b079a22020-01-21 14:36:08 -08003482 EXPECT_EQ(mLayers[2].mLayerSettings, requests[0]);
Lloyd Piquea4863342019-12-04 18:45:02 -08003483
3484 EXPECT_THAT(accumClearRegion, RegionEq(Region(Rect(10, 11, 12, 13))));
3485}
3486
3487TEST_F(GenerateClientCompositionRequestsTest_ThreeLayers,
3488 onlyClientComposesClientComposedLayersIfOthersAreNotOpaque) {
3489 EXPECT_CALL(mLayers[0].mOutputLayer, requiresClientComposition()).WillOnce(Return(false));
3490 EXPECT_CALL(mLayers[1].mOutputLayer, requiresClientComposition()).WillOnce(Return(false));
3491 EXPECT_CALL(mLayers[2].mOutputLayer, requiresClientComposition()).WillOnce(Return(true));
3492
3493 mLayers[0].mOutputLayerState.clearClientTarget = true;
3494 mLayers[1].mOutputLayerState.clearClientTarget = true;
3495 mLayers[2].mOutputLayerState.clearClientTarget = true;
3496
3497 mLayers[0].mLayerFEState.isOpaque = false;
3498 mLayers[1].mLayerFEState.isOpaque = false;
3499 mLayers[2].mLayerFEState.isOpaque = false;
3500
3501 EXPECT_CALL(mLayers[2].mLayerFE, prepareClientComposition(_))
Vishnu Nair9b079a22020-01-21 14:36:08 -08003502 .WillOnce(Return(mLayers[2].mLayerSettings));
Lloyd Piquea4863342019-12-04 18:45:02 -08003503 EXPECT_CALL(mLayers[2].mLayerFE, prepareShadowClientComposition(_, _, _))
3504 .WillOnce(Return(std::nullopt));
3505
3506 Region accumClearRegion(Rect(10, 11, 12, 13));
3507 auto requests = mOutput.generateClientCompositionRequests(false /* supportsProtectedContent */,
3508 accumClearRegion, kDisplayDataspace);
3509 ASSERT_EQ(1u, requests.size());
Vishnu Nair9b079a22020-01-21 14:36:08 -08003510 EXPECT_EQ(mLayers[2].mLayerSettings, requests[0]);
Lloyd Piquea4863342019-12-04 18:45:02 -08003511
3512 EXPECT_THAT(accumClearRegion, RegionEq(Region(Rect(10, 11, 12, 13))));
3513}
3514
3515TEST_F(GenerateClientCompositionRequestsTest_ThreeLayers, clearsHWCLayersIfOpaqueAndNotFirst) {
Lloyd Piquec2d54d42019-08-28 18:04:21 -07003516 // If client composition is performed with some layers set to use device
3517 // composition, device layers after the first layer (device or client) will
3518 // clear the frame buffer if they are opaque and if that layer has a flag
3519 // set to do so. The first layer is skipped as the frame buffer is already
3520 // expected to be clear.
3521
Lloyd Piquea4863342019-12-04 18:45:02 -08003522 EXPECT_CALL(mLayers[0].mOutputLayer, requiresClientComposition()).WillOnce(Return(false));
3523 EXPECT_CALL(mLayers[1].mOutputLayer, requiresClientComposition()).WillOnce(Return(false));
3524 EXPECT_CALL(mLayers[2].mOutputLayer, requiresClientComposition()).WillOnce(Return(true));
Lloyd Piquec2d54d42019-08-28 18:04:21 -07003525
Lloyd Piquea4863342019-12-04 18:45:02 -08003526 mLayers[0].mOutputLayerState.clearClientTarget = true;
3527 mLayers[1].mOutputLayerState.clearClientTarget = true;
3528 mLayers[2].mOutputLayerState.clearClientTarget = true;
Lloyd Piquec2d54d42019-08-28 18:04:21 -07003529
Lloyd Piquea4863342019-12-04 18:45:02 -08003530 mLayers[0].mLayerFEState.isOpaque = true;
3531 mLayers[1].mLayerFEState.isOpaque = true;
3532 mLayers[2].mLayerFEState.isOpaque = true;
Lloyd Piquec2d54d42019-08-28 18:04:21 -07003533
Lloyd Piquea4863342019-12-04 18:45:02 -08003534 EXPECT_CALL(mLayers[1].mLayerFE, prepareClientComposition(_))
Vishnu Nair9b079a22020-01-21 14:36:08 -08003535 .WillOnce(Return(mLayers[1].mLayerSettings));
Lloyd Piquea4863342019-12-04 18:45:02 -08003536 EXPECT_CALL(mLayers[2].mLayerFE, prepareClientComposition(_))
Vishnu Nair9b079a22020-01-21 14:36:08 -08003537 .WillOnce(Return(mLayers[2].mLayerSettings));
Lloyd Piquea4863342019-12-04 18:45:02 -08003538 EXPECT_CALL(mLayers[2].mLayerFE, prepareShadowClientComposition(_, _, _))
3539 .WillOnce(Return(std::nullopt));
Lloyd Piquec2d54d42019-08-28 18:04:21 -07003540
Lloyd Piquea4863342019-12-04 18:45:02 -08003541 Region accumClearRegion(Rect(10, 11, 12, 13));
3542 auto requests = mOutput.generateClientCompositionRequests(false /* supportsProtectedContent */,
3543 accumClearRegion, kDisplayDataspace);
3544 ASSERT_EQ(2u, requests.size());
Lloyd Piquec2d54d42019-08-28 18:04:21 -07003545
Lloyd Piquea4863342019-12-04 18:45:02 -08003546 // The second layer is expected to be rendered as alpha=0 black with no blending
Vishnu Nair9b079a22020-01-21 14:36:08 -08003547 EXPECT_EQ(mLayers[1].mLayerSettings.geometry.boundaries, requests[0].geometry.boundaries);
Lloyd Piquea4863342019-12-04 18:45:02 -08003548 EXPECT_FALSE(requests[0].source.buffer.buffer);
3549 EXPECT_EQ((half3{0.f, 0.f, 0.f}), requests[0].source.solidColor);
Lloyd Pique0a456232020-01-16 17:51:13 -08003550 EXPECT_EQ(0.f, static_cast<float>(requests[0].alpha));
Lloyd Piquea4863342019-12-04 18:45:02 -08003551 EXPECT_EQ(true, requests[0].disableBlending);
Lloyd Piquec2d54d42019-08-28 18:04:21 -07003552
Vishnu Nair9b079a22020-01-21 14:36:08 -08003553 EXPECT_EQ(mLayers[2].mLayerSettings, requests[1]);
Lloyd Piquec2d54d42019-08-28 18:04:21 -07003554
Lloyd Piquea4863342019-12-04 18:45:02 -08003555 EXPECT_THAT(accumClearRegion, RegionEq(Region(Rect(10, 11, 12, 13))));
3556}
Lloyd Piquec2d54d42019-08-28 18:04:21 -07003557
Lloyd Piquea4863342019-12-04 18:45:02 -08003558TEST_F(GenerateClientCompositionRequestsTest_ThreeLayers,
3559 clippedVisibleRegionUsedToGenerateRequest) {
3560 mLayers[0].mOutputLayerState.visibleRegion = Region(Rect(10, 10, 20, 20));
3561 mLayers[1].mOutputLayerState.visibleRegion = Region(Rect(-10, -10, 30, 30));
3562 mLayers[2].mOutputLayerState.visibleRegion = Region(Rect(-10, 0, 40, 4000));
Lloyd Piquec2d54d42019-08-28 18:04:21 -07003563
Lloyd Piquea4863342019-12-04 18:45:02 -08003564 Region accumClearRegion(Rect(10, 11, 12, 13));
3565
3566 compositionengine::LayerFE::ClientCompositionTargetSettings layer0TargetSettings{
3567 Region(Rect(10, 10, 20, 20)),
3568 false, /* identity transform */
3569 false, /* needs filtering */
3570 false, /* secure */
3571 false, /* supports protected content */
3572 accumClearRegion,
3573 };
3574 compositionengine::LayerFE::ClientCompositionTargetSettings layer1TargetSettings{
3575 Region(Rect(0, 0, 30, 30)),
3576 false, /* identity transform */
3577 false, /* needs filtering */
3578 false, /* secure */
3579 false, /* supports protected content */
3580 accumClearRegion,
3581 };
3582 compositionengine::LayerFE::ClientCompositionTargetSettings layer2TargetSettings{
3583 Region(Rect(0, 0, 40, 201)),
3584 false, /* identity transform */
3585 false, /* needs filtering */
3586 false, /* secure */
3587 false, /* supports protected content */
3588 accumClearRegion,
3589 };
3590
3591 EXPECT_CALL(mLayers[0].mLayerFE, prepareClientComposition(Eq(ByRef(layer0TargetSettings))))
3592 .WillOnce(Return(std::nullopt));
3593 EXPECT_CALL(mLayers[1].mLayerFE, prepareClientComposition(Eq(ByRef(layer1TargetSettings))))
3594 .WillOnce(Return(std::nullopt));
3595 EXPECT_CALL(mLayers[2].mLayerFE, prepareClientComposition(Eq(ByRef(layer2TargetSettings))))
3596 .WillOnce(Return(std::nullopt));
3597
3598 static_cast<void>(
3599 mOutput.generateClientCompositionRequests(false /* supportsProtectedContent */,
3600 accumClearRegion, kDisplayDataspace));
3601}
3602
3603TEST_F(GenerateClientCompositionRequestsTest_ThreeLayers,
3604 perLayerNeedsFilteringUsedToGenerateRequests) {
3605 mOutput.mState.needsFiltering = false;
3606 EXPECT_CALL(mLayers[0].mOutputLayer, needsFiltering()).WillRepeatedly(Return(true));
3607
3608 Region accumClearRegion(Rect(10, 11, 12, 13));
3609
3610 compositionengine::LayerFE::ClientCompositionTargetSettings layer0TargetSettings{
3611 Region(kDisplayFrame),
3612 false, /* identity transform */
3613 true, /* needs filtering */
3614 false, /* secure */
3615 false, /* supports protected content */
3616 accumClearRegion,
3617 };
3618 compositionengine::LayerFE::ClientCompositionTargetSettings layer1TargetSettings{
3619 Region(kDisplayFrame),
3620 false, /* identity transform */
3621 false, /* needs filtering */
3622 false, /* secure */
3623 false, /* supports protected content */
3624 accumClearRegion,
3625 };
3626 compositionengine::LayerFE::ClientCompositionTargetSettings layer2TargetSettings{
3627 Region(kDisplayFrame),
3628 false, /* identity transform */
3629 false, /* needs filtering */
3630 false, /* secure */
3631 false, /* supports protected content */
3632 accumClearRegion,
3633 };
3634
3635 EXPECT_CALL(mLayers[0].mLayerFE, prepareClientComposition(Eq(ByRef(layer0TargetSettings))))
3636 .WillOnce(Return(std::nullopt));
3637 EXPECT_CALL(mLayers[1].mLayerFE, prepareClientComposition(Eq(ByRef(layer1TargetSettings))))
3638 .WillOnce(Return(std::nullopt));
3639 EXPECT_CALL(mLayers[2].mLayerFE, prepareClientComposition(Eq(ByRef(layer2TargetSettings))))
3640 .WillOnce(Return(std::nullopt));
3641
3642 static_cast<void>(
3643 mOutput.generateClientCompositionRequests(false /* supportsProtectedContent */,
3644 accumClearRegion, kDisplayDataspace));
3645}
3646
3647TEST_F(GenerateClientCompositionRequestsTest_ThreeLayers,
3648 wholeOutputNeedsFilteringUsedToGenerateRequests) {
3649 mOutput.mState.needsFiltering = true;
3650 EXPECT_CALL(mLayers[0].mOutputLayer, needsFiltering()).WillRepeatedly(Return(true));
3651
3652 Region accumClearRegion(Rect(10, 11, 12, 13));
3653
3654 compositionengine::LayerFE::ClientCompositionTargetSettings layer0TargetSettings{
3655 Region(kDisplayFrame),
3656 false, /* identity transform */
3657 true, /* needs filtering */
3658 false, /* secure */
3659 false, /* supports protected content */
3660 accumClearRegion,
3661 };
3662 compositionengine::LayerFE::ClientCompositionTargetSettings layer1TargetSettings{
3663 Region(kDisplayFrame),
3664 false, /* identity transform */
3665 true, /* needs filtering */
3666 false, /* secure */
3667 false, /* supports protected content */
3668 accumClearRegion,
3669 };
3670 compositionengine::LayerFE::ClientCompositionTargetSettings layer2TargetSettings{
3671 Region(kDisplayFrame),
3672 false, /* identity transform */
3673 true, /* needs filtering */
3674 false, /* secure */
3675 false, /* supports protected content */
3676 accumClearRegion,
3677 };
3678
3679 EXPECT_CALL(mLayers[0].mLayerFE, prepareClientComposition(Eq(ByRef(layer0TargetSettings))))
3680 .WillOnce(Return(std::nullopt));
3681 EXPECT_CALL(mLayers[1].mLayerFE, prepareClientComposition(Eq(ByRef(layer1TargetSettings))))
3682 .WillOnce(Return(std::nullopt));
3683 EXPECT_CALL(mLayers[2].mLayerFE, prepareClientComposition(Eq(ByRef(layer2TargetSettings))))
3684 .WillOnce(Return(std::nullopt));
3685
3686 static_cast<void>(
3687 mOutput.generateClientCompositionRequests(false /* supportsProtectedContent */,
3688 accumClearRegion, kDisplayDataspace));
3689}
3690
3691TEST_F(GenerateClientCompositionRequestsTest_ThreeLayers,
3692 wholeOutputSecurityUsedToGenerateRequests) {
3693 mOutput.mState.isSecure = true;
3694
3695 Region accumClearRegion(Rect(10, 11, 12, 13));
3696
3697 compositionengine::LayerFE::ClientCompositionTargetSettings layer0TargetSettings{
3698 Region(kDisplayFrame),
3699 false, /* identity transform */
3700 false, /* needs filtering */
3701 true, /* secure */
3702 false, /* supports protected content */
3703 accumClearRegion,
3704 };
3705 compositionengine::LayerFE::ClientCompositionTargetSettings layer1TargetSettings{
3706 Region(kDisplayFrame),
3707 false, /* identity transform */
3708 false, /* needs filtering */
3709 true, /* secure */
3710 false, /* supports protected content */
3711 accumClearRegion,
3712 };
3713 compositionengine::LayerFE::ClientCompositionTargetSettings layer2TargetSettings{
3714 Region(kDisplayFrame),
3715 false, /* identity transform */
3716 false, /* needs filtering */
3717 true, /* secure */
3718 false, /* supports protected content */
3719 accumClearRegion,
3720 };
3721
3722 EXPECT_CALL(mLayers[0].mLayerFE, prepareClientComposition(Eq(ByRef(layer0TargetSettings))))
3723 .WillOnce(Return(std::nullopt));
3724 EXPECT_CALL(mLayers[1].mLayerFE, prepareClientComposition(Eq(ByRef(layer1TargetSettings))))
3725 .WillOnce(Return(std::nullopt));
3726 EXPECT_CALL(mLayers[2].mLayerFE, prepareClientComposition(Eq(ByRef(layer2TargetSettings))))
3727 .WillOnce(Return(std::nullopt));
3728
3729 static_cast<void>(
3730 mOutput.generateClientCompositionRequests(false /* supportsProtectedContent */,
3731 accumClearRegion, kDisplayDataspace));
3732}
3733
3734TEST_F(GenerateClientCompositionRequestsTest_ThreeLayers,
3735 protectedContentSupportUsedToGenerateRequests) {
3736 Region accumClearRegion(Rect(10, 11, 12, 13));
3737
3738 compositionengine::LayerFE::ClientCompositionTargetSettings layer0TargetSettings{
3739 Region(kDisplayFrame),
3740 false, /* identity transform */
3741 false, /* needs filtering */
3742 false, /* secure */
3743 true, /* supports protected content */
3744 accumClearRegion,
3745 };
3746 compositionengine::LayerFE::ClientCompositionTargetSettings layer1TargetSettings{
3747 Region(kDisplayFrame),
3748 false, /* identity transform */
3749 false, /* needs filtering */
3750 false, /* secure */
3751 true, /* supports protected content */
3752 accumClearRegion,
3753 };
3754 compositionengine::LayerFE::ClientCompositionTargetSettings layer2TargetSettings{
3755 Region(kDisplayFrame),
3756 false, /* identity transform */
3757 false, /* needs filtering */
3758 false, /* secure */
3759 true, /* supports protected content */
3760 accumClearRegion,
3761 };
3762
3763 EXPECT_CALL(mLayers[0].mLayerFE, prepareClientComposition(Eq(ByRef(layer0TargetSettings))))
3764 .WillOnce(Return(std::nullopt));
3765 EXPECT_CALL(mLayers[1].mLayerFE, prepareClientComposition(Eq(ByRef(layer1TargetSettings))))
3766 .WillOnce(Return(std::nullopt));
3767 EXPECT_CALL(mLayers[2].mLayerFE, prepareClientComposition(Eq(ByRef(layer2TargetSettings))))
3768 .WillOnce(Return(std::nullopt));
3769
3770 static_cast<void>(mOutput.generateClientCompositionRequests(true /* supportsProtectedContent */,
3771 accumClearRegion,
3772 kDisplayDataspace));
3773}
3774
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08003775TEST_F(OutputUpdateAndWriteCompositionStateTest, handlesBackgroundBlurRequests) {
3776 // Layer requesting blur, or below, should request client composition.
3777 EXPECT_CALL(*mOutputLayer1, updateCompositionState(false, true));
3778 EXPECT_CALL(*mOutputLayer1, writeStateToHWC(false));
3779 EXPECT_CALL(*mOutputLayer2, updateCompositionState(false, true));
3780 EXPECT_CALL(*mOutputLayer2, writeStateToHWC(false));
3781 EXPECT_CALL(*mOutputLayer3, updateCompositionState(false, false));
3782 EXPECT_CALL(*mOutputLayer3, writeStateToHWC(false));
3783
3784 mLayer2FEState.backgroundBlurRadius = 10;
3785
3786 injectLayer(std::move(mOutputLayer1));
3787 injectLayer(std::move(mOutputLayer2));
3788 injectLayer(std::move(mOutputLayer3));
3789
3790 mOutput->editState().isEnabled = true;
3791
3792 CompositionRefreshArgs args;
3793 args.updatingGeometryThisFrame = false;
3794 args.devOptForceClientComposition = false;
3795 mOutput->updateAndWriteCompositionState(args);
3796}
3797
Lloyd Piquea4863342019-12-04 18:45:02 -08003798TEST_F(GenerateClientCompositionRequestsTest, handlesLandscapeModeSplitScreenRequests) {
3799 // In split-screen landscape mode, the screen is rotated 90 degrees, with
3800 // one layer on the left covering the left side of the output, and one layer
3801 // on the right covering that side of the output.
Lloyd Piquec2d54d42019-08-28 18:04:21 -07003802
3803 const Rect kPortraitFrame(0, 0, 1000, 2000);
3804 const Rect kPortraitViewport(0, 0, 2000, 1000);
Lloyd Piquee8fe4742020-01-21 15:26:18 -08003805 const Rect kPortraitSourceClip(0, 0, 1000, 2000);
3806 const Rect kPortraitDestinationClip(0, 0, 1000, 2000);
Lloyd Piquec2d54d42019-08-28 18:04:21 -07003807 const uint32_t kPortraitOrientation = TR_ROT_90;
Lloyd Piquea4863342019-12-04 18:45:02 -08003808 constexpr ui::Dataspace kOutputDataspace = ui::Dataspace::DISPLAY_P3;
Lloyd Piquec2d54d42019-08-28 18:04:21 -07003809
Lloyd Piquea4863342019-12-04 18:45:02 -08003810 mOutput.mState.frame = kPortraitFrame;
3811 mOutput.mState.viewport = kPortraitViewport;
Lloyd Piquee8fe4742020-01-21 15:26:18 -08003812 mOutput.mState.sourceClip = kPortraitSourceClip;
3813 mOutput.mState.destinationClip = kPortraitDestinationClip;
Lloyd Piquea4863342019-12-04 18:45:02 -08003814 mOutput.mState.transform = ui::Transform{kPortraitOrientation};
3815 mOutput.mState.orientation = kPortraitOrientation;
3816 mOutput.mState.needsFiltering = false;
3817 mOutput.mState.isSecure = true;
Lloyd Piquec2d54d42019-08-28 18:04:21 -07003818
Lloyd Piquea4863342019-12-04 18:45:02 -08003819 Layer leftLayer;
3820 Layer rightLayer;
Lloyd Piquec2d54d42019-08-28 18:04:21 -07003821
Lloyd Piquea4863342019-12-04 18:45:02 -08003822 leftLayer.mOutputLayerState.clearClientTarget = false;
3823 leftLayer.mOutputLayerState.visibleRegion = Region(Rect(0, 0, 1000, 1000));
3824 leftLayer.mLayerFEState.isOpaque = true;
Vishnu Nair9b079a22020-01-21 14:36:08 -08003825 leftLayer.mLayerSettings.source.solidColor = {1.f, 0.f, 0.f};
Lloyd Piquec2d54d42019-08-28 18:04:21 -07003826
Lloyd Piquea4863342019-12-04 18:45:02 -08003827 rightLayer.mOutputLayerState.clearClientTarget = false;
3828 rightLayer.mOutputLayerState.visibleRegion = Region(Rect(1000, 0, 2000, 1000));
3829 rightLayer.mLayerFEState.isOpaque = true;
Vishnu Nair9b079a22020-01-21 14:36:08 -08003830 rightLayer.mLayerSettings.source.solidColor = {0.f, 1.f, 0.f};
Lloyd Piquea4863342019-12-04 18:45:02 -08003831
3832 EXPECT_CALL(mOutput, getOutputLayerCount()).WillRepeatedly(Return(2u));
3833 EXPECT_CALL(mOutput, getOutputLayerOrderedByZByIndex(0u))
3834 .WillRepeatedly(Return(&leftLayer.mOutputLayer));
3835 EXPECT_CALL(mOutput, getOutputLayerOrderedByZByIndex(1u))
3836 .WillRepeatedly(Return(&rightLayer.mOutputLayer));
3837
3838 Region accumClearRegion(Rect(10, 11, 12, 13));
3839
3840 compositionengine::LayerFE::ClientCompositionTargetSettings leftLayerSettings{
3841 Region(Rect(0, 0, 1000, 1000)),
3842 false, /* identity transform */
3843 false, /* needs filtering */
3844 true, /* secure */
3845 true, /* supports protected content */
3846 accumClearRegion,
3847 };
3848
3849 EXPECT_CALL(leftLayer.mOutputLayer, requiresClientComposition()).WillRepeatedly(Return(true));
3850 EXPECT_CALL(leftLayer.mOutputLayer, needsFiltering()).WillRepeatedly(Return(false));
3851 EXPECT_CALL(leftLayer.mLayerFE, prepareClientComposition(Eq(ByRef(leftLayerSettings))))
Vishnu Nair9b079a22020-01-21 14:36:08 -08003852 .WillOnce(Return(leftLayer.mLayerSettings));
Lloyd Piquea4863342019-12-04 18:45:02 -08003853 EXPECT_CALL(leftLayer.mLayerFE,
Vishnu Nair9b079a22020-01-21 14:36:08 -08003854 prepareShadowClientComposition(leftLayer.mLayerSettings, kPortraitViewport,
Lloyd Piquea4863342019-12-04 18:45:02 -08003855 kOutputDataspace))
3856 .WillOnce(Return(std::nullopt));
3857
3858 compositionengine::LayerFE::ClientCompositionTargetSettings rightLayerSettings{
3859 Region(Rect(1000, 0, 2000, 1000)),
3860 false, /* identity transform */
3861 false, /* needs filtering */
3862 true, /* secure */
3863 true, /* supports protected content */
3864 accumClearRegion,
3865 };
3866
3867 EXPECT_CALL(rightLayer.mOutputLayer, requiresClientComposition()).WillRepeatedly(Return(true));
3868 EXPECT_CALL(rightLayer.mOutputLayer, needsFiltering()).WillRepeatedly(Return(false));
3869 EXPECT_CALL(rightLayer.mLayerFE, prepareClientComposition(Eq(ByRef(rightLayerSettings))))
Vishnu Nair9b079a22020-01-21 14:36:08 -08003870 .WillOnce(Return(rightLayer.mLayerSettings));
Lloyd Piquea4863342019-12-04 18:45:02 -08003871 EXPECT_CALL(rightLayer.mLayerFE,
Vishnu Nair9b079a22020-01-21 14:36:08 -08003872 prepareShadowClientComposition(rightLayer.mLayerSettings, kPortraitViewport,
Lloyd Piquea4863342019-12-04 18:45:02 -08003873 kOutputDataspace))
3874 .WillOnce(Return(std::nullopt));
3875
3876 constexpr bool supportsProtectedContent = true;
3877 auto requests = mOutput.generateClientCompositionRequests(supportsProtectedContent,
3878 accumClearRegion, kOutputDataspace);
3879 ASSERT_EQ(2u, requests.size());
Vishnu Nair9b079a22020-01-21 14:36:08 -08003880 EXPECT_EQ(leftLayer.mLayerSettings, requests[0]);
3881 EXPECT_EQ(rightLayer.mLayerSettings, requests[1]);
Lloyd Piquec2d54d42019-08-28 18:04:21 -07003882}
3883
Vishnu Naira483b4a2019-12-12 15:07:52 -08003884TEST_F(GenerateClientCompositionRequestsTest_ThreeLayers,
3885 shadowRegionOnlyVisibleSkipsContentComposition) {
3886 const Rect kContentWithShadow(40, 40, 70, 90);
3887 const Rect kContent(50, 50, 60, 80);
3888 const Region kShadowRegion = Region(kContentWithShadow).subtract(kContent);
3889 const Region kPartialShadowRegion = Region(kContentWithShadow).subtract(Rect(40, 40, 60, 80));
3890
Vishnu Nair9b079a22020-01-21 14:36:08 -08003891 LayerFE::LayerSettings mShadowSettings;
3892 mShadowSettings.source.solidColor = {0.1f, 0.1f, 0.1f};
Vishnu Naira483b4a2019-12-12 15:07:52 -08003893
3894 mLayers[2].mOutputLayerState.visibleRegion = kPartialShadowRegion;
3895 mLayers[2].mOutputLayerState.shadowRegion = kShadowRegion;
3896
3897 EXPECT_CALL(mLayers[0].mOutputLayer, requiresClientComposition()).WillOnce(Return(false));
3898 EXPECT_CALL(mLayers[1].mOutputLayer, requiresClientComposition()).WillOnce(Return(false));
3899 EXPECT_CALL(mLayers[2].mLayerFE, prepareClientComposition(_))
Vishnu Nair9b079a22020-01-21 14:36:08 -08003900 .WillOnce(Return(mLayers[2].mLayerSettings));
Vishnu Naira483b4a2019-12-12 15:07:52 -08003901 EXPECT_CALL(mLayers[2].mLayerFE,
Vishnu Nair9b079a22020-01-21 14:36:08 -08003902 prepareShadowClientComposition(mLayers[2].mLayerSettings, kDisplayViewport,
Vishnu Naira483b4a2019-12-12 15:07:52 -08003903 kDisplayDataspace))
Vishnu Nair9b079a22020-01-21 14:36:08 -08003904 .WillOnce(Return(mShadowSettings));
Vishnu Naira483b4a2019-12-12 15:07:52 -08003905
3906 Region accumClearRegion(Rect(10, 11, 12, 13));
3907 auto requests = mOutput.generateClientCompositionRequests(false /* supportsProtectedContent */,
3908 accumClearRegion, kDisplayDataspace);
3909 ASSERT_EQ(1u, requests.size());
3910
Vishnu Nair9b079a22020-01-21 14:36:08 -08003911 EXPECT_EQ(mShadowSettings, requests[0]);
Vishnu Naira483b4a2019-12-12 15:07:52 -08003912}
3913
3914TEST_F(GenerateClientCompositionRequestsTest_ThreeLayers,
3915 shadowRegionWithContentVisibleRequestsContentAndShadowComposition) {
3916 const Rect kContentWithShadow(40, 40, 70, 90);
3917 const Rect kContent(50, 50, 60, 80);
3918 const Region kShadowRegion = Region(kContentWithShadow).subtract(kContent);
3919 const Region kPartialContentWithPartialShadowRegion =
3920 Region(kContentWithShadow).subtract(Rect(40, 40, 50, 80));
3921
Vishnu Nair9b079a22020-01-21 14:36:08 -08003922 LayerFE::LayerSettings mShadowSettings;
3923 mShadowSettings.source.solidColor = {0.1f, 0.1f, 0.1f};
Vishnu Naira483b4a2019-12-12 15:07:52 -08003924
3925 mLayers[2].mOutputLayerState.visibleRegion = kPartialContentWithPartialShadowRegion;
3926 mLayers[2].mOutputLayerState.shadowRegion = kShadowRegion;
3927
3928 EXPECT_CALL(mLayers[0].mOutputLayer, requiresClientComposition()).WillOnce(Return(false));
3929 EXPECT_CALL(mLayers[1].mOutputLayer, requiresClientComposition()).WillOnce(Return(false));
3930 EXPECT_CALL(mLayers[2].mLayerFE, prepareClientComposition(_))
Vishnu Nair9b079a22020-01-21 14:36:08 -08003931 .WillOnce(Return(mLayers[2].mLayerSettings));
Vishnu Naira483b4a2019-12-12 15:07:52 -08003932 EXPECT_CALL(mLayers[2].mLayerFE,
Vishnu Nair9b079a22020-01-21 14:36:08 -08003933 prepareShadowClientComposition(mLayers[2].mLayerSettings, kDisplayViewport,
Vishnu Naira483b4a2019-12-12 15:07:52 -08003934 kDisplayDataspace))
Vishnu Nair9b079a22020-01-21 14:36:08 -08003935 .WillOnce(Return(mShadowSettings));
Vishnu Naira483b4a2019-12-12 15:07:52 -08003936
3937 Region accumClearRegion(Rect(10, 11, 12, 13));
3938 auto requests = mOutput.generateClientCompositionRequests(false /* supportsProtectedContent */,
3939 accumClearRegion, kDisplayDataspace);
3940 ASSERT_EQ(2u, requests.size());
3941
Vishnu Nair9b079a22020-01-21 14:36:08 -08003942 EXPECT_EQ(mShadowSettings, requests[0]);
3943 EXPECT_EQ(mLayers[2].mLayerSettings, requests[1]);
Vishnu Naira483b4a2019-12-12 15:07:52 -08003944}
3945
Lloyd Pique32cbe282018-10-19 13:09:22 -07003946} // namespace
3947} // namespace android::compositionengine