• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <compositionengine/impl/HwcBufferCache.h>
18 #include <compositionengine/impl/OutputLayer.h>
19 #include <compositionengine/impl/OutputLayerCompositionState.h>
20 #include <compositionengine/mock/CompositionEngine.h>
21 #include <compositionengine/mock/DisplayColorProfile.h>
22 #include <compositionengine/mock/LayerFE.h>
23 #include <compositionengine/mock/Output.h>
24 #include <gtest/gtest.h>
25 #include <log/log.h>
26 
27 #include <renderengine/impl/ExternalTexture.h>
28 #include <renderengine/mock/RenderEngine.h>
29 #include <ui/PixelFormat.h>
30 #include "MockHWC2.h"
31 #include "MockHWComposer.h"
32 #include "RegionMatcher.h"
33 
34 #include <aidl/android/hardware/graphics/composer3/Composition.h>
35 
36 using aidl::android::hardware::graphics::composer3::Composition;
37 
38 namespace android::compositionengine {
39 namespace {
40 
41 namespace hal = android::hardware::graphics::composer::hal;
42 
43 using testing::_;
44 using testing::InSequence;
45 using testing::Mock;
46 using testing::NiceMock;
47 using testing::Return;
48 using testing::ReturnRef;
49 using testing::StrictMock;
50 
51 constexpr auto TR_IDENT = 0u;
52 constexpr auto TR_FLP_H = HAL_TRANSFORM_FLIP_H;
53 constexpr auto TR_FLP_V = HAL_TRANSFORM_FLIP_V;
54 constexpr auto TR_ROT_90 = HAL_TRANSFORM_ROT_90;
55 constexpr auto TR_ROT_180 = TR_FLP_H | TR_FLP_V;
56 constexpr auto TR_ROT_270 = TR_ROT_90 | TR_ROT_180;
57 
58 const std::string kOutputName{"Test Output"};
59 
60 MATCHER_P(ColorEq, expected, "") {
61     *result_listener << "Colors are not equal\n";
62     *result_listener << "expected " << expected.r << " " << expected.g << " " << expected.b << " "
63                      << expected.a << "\n";
64     *result_listener << "actual " << arg.r << " " << arg.g << " " << arg.b << " " << arg.a << "\n";
65 
66     return expected.r == arg.r && expected.g == arg.g && expected.b == arg.b && expected.a == arg.a;
67 }
68 
toRotation(uint32_t rotationFlag)69 ui::Rotation toRotation(uint32_t rotationFlag) {
70     switch (rotationFlag) {
71         case ui::Transform::RotationFlags::ROT_0:
72             return ui::ROTATION_0;
73         case ui::Transform::RotationFlags::ROT_90:
74             return ui::ROTATION_90;
75         case ui::Transform::RotationFlags::ROT_180:
76             return ui::ROTATION_180;
77         case ui::Transform::RotationFlags::ROT_270:
78             return ui::ROTATION_270;
79         default:
80             LOG_FATAL("Unexpected rotation flag %d", rotationFlag);
81             return ui::Rotation(-1);
82     }
83 }
84 
85 struct OutputLayerTest : public testing::Test {
86     struct OutputLayer final : public impl::OutputLayer {
OutputLayerandroid::compositionengine::__anon7a9c7e560111::OutputLayerTest::OutputLayer87         OutputLayer(const compositionengine::Output& output, compositionengine::LayerFE& layerFE)
88               : mOutput(output), mLayerFE(layerFE) {}
89         ~OutputLayer() override = default;
90 
91         // compositionengine::OutputLayer overrides
getOutputandroid::compositionengine::__anon7a9c7e560111::OutputLayerTest::OutputLayer92         const compositionengine::Output& getOutput() const override { return mOutput; }
getLayerFEandroid::compositionengine::__anon7a9c7e560111::OutputLayerTest::OutputLayer93         compositionengine::LayerFE& getLayerFE() const override { return mLayerFE; }
getStateandroid::compositionengine::__anon7a9c7e560111::OutputLayerTest::OutputLayer94         const impl::OutputLayerCompositionState& getState() const override { return mState; }
editStateandroid::compositionengine::__anon7a9c7e560111::OutputLayerTest::OutputLayer95         impl::OutputLayerCompositionState& editState() override { return mState; }
96 
97         // compositionengine::impl::OutputLayer overrides
dumpStateandroid::compositionengine::__anon7a9c7e560111::OutputLayerTest::OutputLayer98         void dumpState(std::string& out) const override { mState.dump(out); }
99 
100         const compositionengine::Output& mOutput;
101         compositionengine::LayerFE& mLayerFE;
102         impl::OutputLayerCompositionState mState;
103     };
104 
OutputLayerTestandroid::compositionengine::__anon7a9c7e560111::OutputLayerTest105     OutputLayerTest() {
106         ON_CALL(mLayerFE, getDebugName()).WillByDefault(Return("Test LayerFE"));
107         ON_CALL(mOutput, getName()).WillByDefault(ReturnRef(kOutputName));
108 
109         ON_CALL(mLayerFE, getCompositionState()).WillByDefault(Return(&mLayerFEState));
110         ON_CALL(mOutput, getState()).WillByDefault(ReturnRef(mOutputState));
111     }
112 
113     NiceMock<compositionengine::mock::Output> mOutput;
114     sp<NiceMock<compositionengine::mock::LayerFE>> mLayerFE_ =
115             sp<NiceMock<compositionengine::mock::LayerFE>>::make();
116     NiceMock<compositionengine::mock::LayerFE>& mLayerFE = *mLayerFE_;
117     OutputLayer mOutputLayer{mOutput, mLayerFE};
118 
119     LayerFECompositionState mLayerFEState;
120     impl::OutputCompositionState mOutputState;
121 };
122 
123 /*
124  * Basic construction
125  */
126 
TEST_F(OutputLayerTest,canInstantiateOutputLayer)127 TEST_F(OutputLayerTest, canInstantiateOutputLayer) {}
128 
129 /*
130  * OutputLayer::setHwcLayer()
131  */
132 
TEST_F(OutputLayerTest,settingNullHwcLayerSetsEmptyHwcState)133 TEST_F(OutputLayerTest, settingNullHwcLayerSetsEmptyHwcState) {
134     StrictMock<compositionengine::mock::CompositionEngine> compositionEngine;
135 
136     mOutputLayer.setHwcLayer(nullptr);
137 
138     EXPECT_FALSE(mOutputLayer.getState().hwc);
139 }
140 
TEST_F(OutputLayerTest,settingHwcLayerSetsHwcState)141 TEST_F(OutputLayerTest, settingHwcLayerSetsHwcState) {
142     auto hwcLayer = std::make_shared<StrictMock<HWC2::mock::Layer>>();
143 
144     mOutputLayer.setHwcLayer(hwcLayer);
145 
146     const auto& outputLayerState = mOutputLayer.getState();
147     ASSERT_TRUE(outputLayerState.hwc);
148 
149     const auto& hwcState = *outputLayerState.hwc;
150     EXPECT_EQ(hwcLayer, hwcState.hwcLayer);
151 }
152 
153 /*
154  * OutputLayer::calculateOutputSourceCrop()
155  */
156 
157 struct OutputLayerSourceCropTest : public OutputLayerTest {
OutputLayerSourceCropTestandroid::compositionengine::__anon7a9c7e560111::OutputLayerSourceCropTest158     OutputLayerSourceCropTest() {
159         // Set reasonable default values for a simple case. Each test will
160         // set one specific value to something different.
161         mLayerFEState.geomUsesSourceCrop = true;
162         mLayerFEState.geomContentCrop = Rect{0, 0, 1920, 1080};
163         mLayerFEState.transparentRegionHint = Region{};
164         mLayerFEState.geomLayerBounds = FloatRect{0.f, 0.f, 1920.f, 1080.f};
165         mLayerFEState.geomLayerTransform = ui::Transform{TR_IDENT};
166         mLayerFEState.geomBufferSize = Rect{0, 0, 1920, 1080};
167         mLayerFEState.geomBufferTransform = TR_IDENT;
168 
169         mOutputState.layerStackSpace.setContent(Rect{0, 0, 1920, 1080});
170     }
171 
calculateOutputSourceCropandroid::compositionengine::__anon7a9c7e560111::OutputLayerSourceCropTest172     FloatRect calculateOutputSourceCrop() {
173         mLayerFEState.geomInverseLayerTransform = mLayerFEState.geomLayerTransform.inverse();
174 
175         return mOutputLayer.calculateOutputSourceCrop(ui::Transform::RotationFlags::ROT_0);
176     }
177 };
178 
TEST_F(OutputLayerSourceCropTest,computesEmptyIfSourceCropNotUsed)179 TEST_F(OutputLayerSourceCropTest, computesEmptyIfSourceCropNotUsed) {
180     mLayerFEState.geomUsesSourceCrop = false;
181 
182     const FloatRect expected{};
183     EXPECT_THAT(calculateOutputSourceCrop(), expected);
184 }
185 
TEST_F(OutputLayerSourceCropTest,correctForSimpleDefaultCase)186 TEST_F(OutputLayerSourceCropTest, correctForSimpleDefaultCase) {
187     const FloatRect expected{0.f, 0.f, 1920.f, 1080.f};
188     EXPECT_THAT(calculateOutputSourceCrop(), expected);
189 }
190 
TEST_F(OutputLayerSourceCropTest,handlesBoundsOutsideViewport)191 TEST_F(OutputLayerSourceCropTest, handlesBoundsOutsideViewport) {
192     mLayerFEState.geomLayerBounds = FloatRect{-2000.f, -2000.f, 2000.f, 2000.f};
193 
194     const FloatRect expected{0.f, 0.f, 1920.f, 1080.f};
195     EXPECT_THAT(calculateOutputSourceCrop(), expected);
196 }
197 
TEST_F(OutputLayerSourceCropTest,handlesBoundsOutsideViewportRotated)198 TEST_F(OutputLayerSourceCropTest, handlesBoundsOutsideViewportRotated) {
199     mLayerFEState.geomLayerBounds = FloatRect{-2000.f, -2000.f, 2000.f, 2000.f};
200     mLayerFEState.geomLayerTransform.set(HAL_TRANSFORM_ROT_90, 1920, 1080);
201 
202     const FloatRect expected{0.f, 0.f, 1080.f, 1080.f};
203     EXPECT_THAT(calculateOutputSourceCrop(), expected);
204 }
205 
TEST_F(OutputLayerSourceCropTest,calculateOutputSourceCropWorksWithATransformedBuffer)206 TEST_F(OutputLayerSourceCropTest, calculateOutputSourceCropWorksWithATransformedBuffer) {
207     struct Entry {
208         uint32_t bufferInvDisplay;
209         uint32_t buffer;
210         uint32_t display;
211         FloatRect expected;
212     };
213     // Not an exhaustive list of cases, but hopefully enough.
214     const std::array<Entry, 12> testData = {
215             // clang-format off
216             //             inv      buffer      display     expected
217             /*  0 */ Entry{false,   TR_IDENT,   TR_IDENT,   FloatRect{0.f, 0.f, 1920.f, 1080.f}},
218             /*  1 */ Entry{false,   TR_IDENT,   TR_ROT_90,  FloatRect{0.f, 0.f, 1920.f, 1080.f}},
219             /*  2 */ Entry{false,   TR_IDENT,   TR_ROT_180, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
220             /*  3 */ Entry{false,   TR_IDENT,   TR_ROT_270, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
221 
222             /*  4 */ Entry{true,    TR_IDENT,   TR_IDENT,   FloatRect{0.f, 0.f, 1920.f, 1080.f}},
223             /*  5 */ Entry{true,    TR_IDENT,   TR_ROT_90,  FloatRect{0.f, 0.f, 1920.f, 1080.f}},
224             /*  6 */ Entry{true,    TR_IDENT,   TR_ROT_180, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
225             /*  7 */ Entry{true,    TR_IDENT,   TR_ROT_270, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
226 
227             /*  8 */ Entry{false,   TR_IDENT,   TR_IDENT,   FloatRect{0.f, 0.f, 1920.f, 1080.f}},
228             /*  9 */ Entry{false,   TR_ROT_90,  TR_ROT_90,  FloatRect{0.f, 0.f, 1920.f, 1080.f}},
229             /* 10 */ Entry{false,   TR_ROT_180, TR_ROT_180, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
230             /* 11 */ Entry{false,   TR_ROT_270, TR_ROT_270, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
231 
232             // clang-format on
233     };
234 
235     for (size_t i = 0; i < testData.size(); i++) {
236         const auto& entry = testData[i];
237 
238         mLayerFEState.geomBufferUsesDisplayInverseTransform = entry.bufferInvDisplay;
239         mLayerFEState.geomBufferTransform = entry.buffer;
240         mOutputState.displaySpace.setOrientation(toRotation(entry.display));
241 
242         EXPECT_THAT(calculateOutputSourceCrop(), entry.expected) << "entry " << i;
243     }
244 }
245 
TEST_F(OutputLayerSourceCropTest,geomContentCropAffectsCrop)246 TEST_F(OutputLayerSourceCropTest, geomContentCropAffectsCrop) {
247     mLayerFEState.geomContentCrop = Rect{0, 0, 960, 540};
248 
249     const FloatRect expected{0.f, 0.f, 960.f, 540.f};
250     EXPECT_THAT(calculateOutputSourceCrop(), expected);
251 }
252 
TEST_F(OutputLayerSourceCropTest,viewportAffectsCrop)253 TEST_F(OutputLayerSourceCropTest, viewportAffectsCrop) {
254     mOutputState.layerStackSpace.setContent(Rect{0, 0, 960, 540});
255 
256     const FloatRect expected{0.f, 0.f, 960.f, 540.f};
257     EXPECT_THAT(calculateOutputSourceCrop(), expected);
258 }
259 
260 /*
261  * OutputLayer::calculateOutputDisplayFrame()
262  */
263 
264 struct OutputLayerDisplayFrameTest : public OutputLayerTest {
OutputLayerDisplayFrameTestandroid::compositionengine::__anon7a9c7e560111::OutputLayerDisplayFrameTest265     OutputLayerDisplayFrameTest() {
266         // Set reasonable default values for a simple case. Each test will
267         // set one specific value to something different.
268 
269         mLayerFEState.transparentRegionHint = Region{};
270         mLayerFEState.geomLayerTransform = ui::Transform{TR_IDENT};
271         mLayerFEState.geomBufferSize = Rect{0, 0, 1920, 1080};
272         mLayerFEState.geomBufferUsesDisplayInverseTransform = false;
273         mLayerFEState.geomCrop = Rect{0, 0, 1920, 1080};
274         mLayerFEState.geomLayerBounds = FloatRect{0.f, 0.f, 1920.f, 1080.f};
275 
276         mOutputState.layerStackSpace.setContent(Rect{0, 0, 1920, 1080});
277         mOutputState.transform = ui::Transform{TR_IDENT};
278     }
279 
calculateOutputDisplayFrameandroid::compositionengine::__anon7a9c7e560111::OutputLayerDisplayFrameTest280     Rect calculateOutputDisplayFrame() {
281         mLayerFEState.geomInverseLayerTransform = mLayerFEState.geomLayerTransform.inverse();
282 
283         return mOutputLayer.calculateOutputDisplayFrame();
284     }
285 };
286 
TEST_F(OutputLayerDisplayFrameTest,correctForSimpleDefaultCase)287 TEST_F(OutputLayerDisplayFrameTest, correctForSimpleDefaultCase) {
288     const Rect expected{0, 0, 1920, 1080};
289     EXPECT_THAT(calculateOutputDisplayFrame(), expected);
290 }
291 
TEST_F(OutputLayerDisplayFrameTest,fullActiveTransparentRegionReturnsEmptyFrame)292 TEST_F(OutputLayerDisplayFrameTest, fullActiveTransparentRegionReturnsEmptyFrame) {
293     mLayerFEState.transparentRegionHint = Region{Rect{0, 0, 1920, 1080}};
294     const Rect expected{0, 0, 0, 0};
295     EXPECT_THAT(calculateOutputDisplayFrame(), expected);
296 }
297 
TEST_F(OutputLayerDisplayFrameTest,cropAffectsDisplayFrame)298 TEST_F(OutputLayerDisplayFrameTest, cropAffectsDisplayFrame) {
299     mLayerFEState.geomCrop = Rect{100, 200, 300, 500};
300     const Rect expected{100, 200, 300, 500};
301     EXPECT_THAT(calculateOutputDisplayFrame(), expected);
302 }
303 
TEST_F(OutputLayerDisplayFrameTest,cropAffectsDisplayFrameRotated)304 TEST_F(OutputLayerDisplayFrameTest, cropAffectsDisplayFrameRotated) {
305     mLayerFEState.geomCrop = Rect{100, 200, 300, 500};
306     mLayerFEState.geomLayerTransform.set(HAL_TRANSFORM_ROT_90, 1920, 1080);
307     const Rect expected{1420, 100, 1720, 300};
308     EXPECT_THAT(calculateOutputDisplayFrame(), expected);
309 }
310 
TEST_F(OutputLayerDisplayFrameTest,emptyGeomCropIsNotUsedToComputeFrame)311 TEST_F(OutputLayerDisplayFrameTest, emptyGeomCropIsNotUsedToComputeFrame) {
312     mLayerFEState.geomCrop = Rect{};
313     const Rect expected{0, 0, 1920, 1080};
314     EXPECT_THAT(calculateOutputDisplayFrame(), expected);
315 }
316 
TEST_F(OutputLayerDisplayFrameTest,geomLayerBoundsAffectsFrame)317 TEST_F(OutputLayerDisplayFrameTest, geomLayerBoundsAffectsFrame) {
318     mLayerFEState.geomLayerBounds = FloatRect{0.f, 0.f, 960.f, 540.f};
319     const Rect expected{0, 0, 960, 540};
320     EXPECT_THAT(calculateOutputDisplayFrame(), expected);
321 }
322 
TEST_F(OutputLayerDisplayFrameTest,viewportAffectsFrame)323 TEST_F(OutputLayerDisplayFrameTest, viewportAffectsFrame) {
324     mOutputState.layerStackSpace.setContent(Rect{0, 0, 960, 540});
325     const Rect expected{0, 0, 960, 540};
326     EXPECT_THAT(calculateOutputDisplayFrame(), expected);
327 }
328 
TEST_F(OutputLayerDisplayFrameTest,outputTransformAffectsDisplayFrame)329 TEST_F(OutputLayerDisplayFrameTest, outputTransformAffectsDisplayFrame) {
330     mOutputState.transform = ui::Transform{HAL_TRANSFORM_ROT_90};
331     const Rect expected{-1080, 0, 0, 1920};
332     EXPECT_THAT(calculateOutputDisplayFrame(), expected);
333 }
334 
TEST_F(OutputLayerDisplayFrameTest,shadowExpandsDisplayFrame)335 TEST_F(OutputLayerDisplayFrameTest, shadowExpandsDisplayFrame) {
336     const int kShadowRadius = 5;
337     mLayerFEState.shadowRadius = kShadowRadius;
338     mLayerFEState.forceClientComposition = true;
339 
340     mLayerFEState.geomLayerBounds = FloatRect{100.f, 100.f, 200.f, 200.f};
341     Rect expected{mLayerFEState.geomLayerBounds};
342     expected.inset(-kShadowRadius, -kShadowRadius, -kShadowRadius, -kShadowRadius);
343     EXPECT_THAT(calculateOutputDisplayFrame(), expected);
344 }
345 
TEST_F(OutputLayerDisplayFrameTest,shadowExpandsDisplayFrame_onlyIfForcingClientComposition)346 TEST_F(OutputLayerDisplayFrameTest, shadowExpandsDisplayFrame_onlyIfForcingClientComposition) {
347     const int kShadowRadius = 5;
348     mLayerFEState.shadowRadius = kShadowRadius;
349     mLayerFEState.forceClientComposition = false;
350 
351     mLayerFEState.geomLayerBounds = FloatRect{100.f, 100.f, 200.f, 200.f};
352     Rect expected{mLayerFEState.geomLayerBounds};
353     EXPECT_THAT(calculateOutputDisplayFrame(), expected);
354 }
355 
356 /*
357  * OutputLayer::calculateOutputRelativeBufferTransform()
358  */
359 
TEST_F(OutputLayerTest,calculateOutputRelativeBufferTransformTestsNeeded)360 TEST_F(OutputLayerTest, calculateOutputRelativeBufferTransformTestsNeeded) {
361     mLayerFEState.geomBufferUsesDisplayInverseTransform = false;
362 
363     struct Entry {
364         uint32_t layer;
365         uint32_t buffer;
366         uint32_t display;
367         uint32_t expected;
368     };
369     // Not an exhaustive list of cases, but hopefully enough.
370     const std::array<Entry, 24> testData = {
371             // clang-format off
372             //             layer       buffer      display     expected
373             /*  0 */ Entry{TR_IDENT,   TR_IDENT,   TR_IDENT,   TR_IDENT},
374             /*  1 */ Entry{TR_IDENT,   TR_IDENT,   TR_ROT_90,  TR_ROT_90},
375             /*  2 */ Entry{TR_IDENT,   TR_IDENT,   TR_ROT_180, TR_ROT_180},
376             /*  3 */ Entry{TR_IDENT,   TR_IDENT,   TR_ROT_270, TR_ROT_270},
377 
378             /*  4 */ Entry{TR_IDENT,   TR_FLP_H,   TR_IDENT,   TR_FLP_H ^ TR_IDENT},
379             /*  5 */ Entry{TR_IDENT,   TR_FLP_H,   TR_ROT_90,  TR_FLP_H ^ TR_ROT_90},
380             /*  6 */ Entry{TR_IDENT,   TR_FLP_H,   TR_ROT_180, TR_FLP_H ^ TR_ROT_180},
381             /*  7 */ Entry{TR_IDENT,   TR_FLP_H,   TR_ROT_270, TR_FLP_H ^ TR_ROT_270},
382 
383             /*  8 */ Entry{TR_IDENT,   TR_FLP_V,   TR_IDENT,   TR_FLP_V},
384             /*  9 */ Entry{TR_IDENT,   TR_ROT_90,  TR_ROT_90,  TR_ROT_180},
385             /* 10 */ Entry{TR_IDENT,   TR_ROT_180, TR_ROT_180, TR_IDENT},
386             /* 11 */ Entry{TR_IDENT,   TR_ROT_270, TR_ROT_270, TR_ROT_180},
387 
388             /* 12 */ Entry{TR_ROT_90,  TR_IDENT,   TR_IDENT,   TR_IDENT ^ TR_ROT_90},
389             /* 13 */ Entry{TR_ROT_90,  TR_FLP_H,   TR_ROT_90,  TR_FLP_H ^ TR_ROT_180},
390             /* 14 */ Entry{TR_ROT_90,  TR_IDENT,   TR_ROT_180, TR_IDENT ^ TR_ROT_270},
391             /* 15 */ Entry{TR_ROT_90,  TR_FLP_H,   TR_ROT_270, TR_FLP_H ^ TR_IDENT},
392 
393             /* 16 */ Entry{TR_ROT_180, TR_FLP_H,   TR_IDENT,   TR_FLP_H ^ TR_ROT_180},
394             /* 17 */ Entry{TR_ROT_180, TR_IDENT,   TR_ROT_90,  TR_IDENT ^ TR_ROT_270},
395             /* 18 */ Entry{TR_ROT_180, TR_FLP_H,   TR_ROT_180, TR_FLP_H ^ TR_IDENT},
396             /* 19 */ Entry{TR_ROT_180, TR_IDENT,   TR_ROT_270, TR_IDENT ^ TR_ROT_90},
397 
398             /* 20 */ Entry{TR_ROT_270, TR_IDENT,   TR_IDENT,   TR_IDENT ^ TR_ROT_270},
399             /* 21 */ Entry{TR_ROT_270, TR_FLP_H,   TR_ROT_90,  TR_FLP_H ^ TR_IDENT},
400             /* 22 */ Entry{TR_ROT_270, TR_FLP_H,   TR_ROT_180, TR_FLP_H ^ TR_ROT_90},
401             /* 23 */ Entry{TR_ROT_270, TR_IDENT,   TR_ROT_270, TR_IDENT ^ TR_ROT_180},
402             // clang-format on
403     };
404 
405     for (size_t i = 0; i < testData.size(); i++) {
406         const auto& entry = testData[i];
407 
408         mLayerFEState.geomLayerTransform.set(entry.layer, 1920, 1080);
409         mLayerFEState.geomBufferTransform = entry.buffer;
410         mOutputState.displaySpace.setOrientation(toRotation(entry.display));
411         mOutputState.transform = ui::Transform{entry.display};
412 
413         const auto actual = mOutputLayer.calculateOutputRelativeBufferTransform(entry.display);
414         EXPECT_EQ(entry.expected, actual) << "entry " << i;
415     }
416 }
417 
TEST_F(OutputLayerTest,calculateOutputRelativeBufferTransformTestWithOfBufferUsesDisplayInverseTransform)418 TEST_F(OutputLayerTest,
419        calculateOutputRelativeBufferTransformTestWithOfBufferUsesDisplayInverseTransform) {
420     mLayerFEState.geomBufferUsesDisplayInverseTransform = true;
421 
422     struct Entry {
423         uint32_t layer; /* shouldn't affect the result, so we just use arbitrary values */
424         uint32_t buffer;
425         uint32_t display;
426         uint32_t internal;
427         uint32_t expected;
428     };
429     const std::array<Entry, 64> testData = {
430             // clang-format off
431             //    layer       buffer      display     internal    expected
432             Entry{TR_IDENT,   TR_IDENT,   TR_IDENT,   TR_IDENT,   TR_IDENT},
433             Entry{TR_IDENT,   TR_IDENT,   TR_IDENT,   TR_ROT_90,  TR_ROT_270},
434             Entry{TR_IDENT,   TR_IDENT,   TR_IDENT,   TR_ROT_180, TR_ROT_180},
435             Entry{TR_IDENT,   TR_IDENT,   TR_IDENT,   TR_ROT_270, TR_ROT_90},
436 
437             Entry{TR_IDENT,   TR_IDENT,   TR_ROT_90,  TR_IDENT,   TR_ROT_90},
438             Entry{TR_ROT_90,  TR_IDENT,   TR_ROT_90,  TR_ROT_90,  TR_IDENT},
439             Entry{TR_ROT_180, TR_IDENT,   TR_ROT_90,  TR_ROT_180, TR_ROT_270},
440             Entry{TR_ROT_90,  TR_IDENT,   TR_ROT_90,  TR_ROT_270, TR_ROT_180},
441 
442             Entry{TR_ROT_180, TR_IDENT,   TR_ROT_180, TR_IDENT,   TR_ROT_180},
443             Entry{TR_ROT_90,  TR_IDENT,   TR_ROT_180, TR_ROT_90,  TR_ROT_90},
444             Entry{TR_ROT_180, TR_IDENT,   TR_ROT_180, TR_ROT_180, TR_IDENT},
445             Entry{TR_ROT_270, TR_IDENT,   TR_ROT_180, TR_ROT_270, TR_ROT_270},
446 
447             Entry{TR_ROT_270, TR_IDENT,   TR_ROT_270, TR_IDENT,   TR_ROT_270},
448             Entry{TR_ROT_270, TR_IDENT,   TR_ROT_270, TR_ROT_90,  TR_ROT_180},
449             Entry{TR_ROT_180, TR_IDENT,   TR_ROT_270, TR_ROT_180, TR_ROT_90},
450             Entry{TR_IDENT,   TR_IDENT,   TR_ROT_270, TR_ROT_270, TR_IDENT},
451 
452             //    layer       buffer      display     internal    expected
453             Entry{TR_IDENT,   TR_ROT_90,  TR_IDENT,   TR_IDENT,   TR_ROT_90},
454             Entry{TR_ROT_90,  TR_ROT_90,  TR_IDENT,   TR_ROT_90,  TR_IDENT},
455             Entry{TR_ROT_180, TR_ROT_90,  TR_IDENT,   TR_ROT_180, TR_ROT_270},
456             Entry{TR_ROT_270, TR_ROT_90,  TR_IDENT,   TR_ROT_270, TR_ROT_180},
457 
458             Entry{TR_ROT_90,  TR_ROT_90,  TR_ROT_90,  TR_IDENT,   TR_ROT_180},
459             Entry{TR_ROT_90,  TR_ROT_90,  TR_ROT_90,  TR_ROT_90,  TR_ROT_90},
460             Entry{TR_ROT_90,  TR_ROT_90,  TR_ROT_90,  TR_ROT_180, TR_IDENT},
461             Entry{TR_ROT_270, TR_ROT_90,  TR_ROT_90,  TR_ROT_270, TR_ROT_270},
462 
463             Entry{TR_IDENT,   TR_ROT_90,  TR_ROT_180, TR_IDENT,   TR_ROT_270},
464             Entry{TR_ROT_90,  TR_ROT_90,  TR_ROT_180, TR_ROT_90,  TR_ROT_180},
465             Entry{TR_ROT_180, TR_ROT_90,  TR_ROT_180, TR_ROT_180, TR_ROT_90},
466             Entry{TR_ROT_90,  TR_ROT_90,  TR_ROT_180, TR_ROT_270, TR_IDENT},
467 
468             Entry{TR_IDENT,   TR_ROT_90,  TR_ROT_270, TR_IDENT,   TR_IDENT},
469             Entry{TR_ROT_270, TR_ROT_90,  TR_ROT_270, TR_ROT_90,  TR_ROT_270},
470             Entry{TR_ROT_180, TR_ROT_90,  TR_ROT_270, TR_ROT_180, TR_ROT_180},
471             Entry{TR_ROT_270, TR_ROT_90,  TR_ROT_270, TR_ROT_270, TR_ROT_90},
472 
473             //    layer       buffer      display     internal    expected
474             Entry{TR_IDENT,   TR_ROT_180, TR_IDENT,   TR_IDENT,   TR_ROT_180},
475             Entry{TR_IDENT,   TR_ROT_180, TR_IDENT,   TR_ROT_90,  TR_ROT_90},
476             Entry{TR_ROT_180, TR_ROT_180, TR_IDENT,   TR_ROT_180, TR_IDENT},
477             Entry{TR_ROT_270, TR_ROT_180, TR_IDENT,   TR_ROT_270, TR_ROT_270},
478 
479             Entry{TR_IDENT,   TR_ROT_180, TR_ROT_90,  TR_IDENT,   TR_ROT_270},
480             Entry{TR_ROT_90,  TR_ROT_180, TR_ROT_90,  TR_ROT_90,  TR_ROT_180},
481             Entry{TR_ROT_180, TR_ROT_180, TR_ROT_90,  TR_ROT_180, TR_ROT_90},
482             Entry{TR_ROT_180, TR_ROT_180, TR_ROT_90,  TR_ROT_270, TR_IDENT},
483 
484             Entry{TR_IDENT,   TR_ROT_180, TR_ROT_180, TR_IDENT,   TR_IDENT},
485             Entry{TR_ROT_180, TR_ROT_180, TR_ROT_180, TR_ROT_90,  TR_ROT_270},
486             Entry{TR_ROT_180, TR_ROT_180, TR_ROT_180, TR_ROT_180, TR_ROT_180},
487             Entry{TR_ROT_270, TR_ROT_180, TR_ROT_180, TR_ROT_270, TR_ROT_90},
488 
489             Entry{TR_ROT_270, TR_ROT_180, TR_ROT_270, TR_IDENT,   TR_ROT_90},
490             Entry{TR_ROT_180, TR_ROT_180, TR_ROT_270, TR_ROT_90,  TR_IDENT},
491             Entry{TR_ROT_180, TR_ROT_180, TR_ROT_270, TR_ROT_180, TR_ROT_270},
492             Entry{TR_ROT_270, TR_ROT_180, TR_ROT_270, TR_ROT_270, TR_ROT_180},
493 
494             //    layer       buffer      display     internal    expected
495             Entry{TR_IDENT,   TR_ROT_270, TR_IDENT,   TR_IDENT,   TR_ROT_270},
496             Entry{TR_ROT_90,  TR_ROT_270, TR_IDENT,   TR_ROT_90,  TR_ROT_180},
497             Entry{TR_ROT_270, TR_ROT_270, TR_IDENT,   TR_ROT_180, TR_ROT_90},
498             Entry{TR_IDENT,   TR_ROT_270, TR_IDENT,   TR_ROT_270, TR_IDENT},
499 
500             Entry{TR_ROT_270, TR_ROT_270, TR_ROT_90,  TR_IDENT,   TR_IDENT},
501             Entry{TR_ROT_90,  TR_ROT_270, TR_ROT_90,  TR_ROT_90,  TR_ROT_270},
502             Entry{TR_ROT_180, TR_ROT_270, TR_ROT_90,  TR_ROT_180, TR_ROT_180},
503             Entry{TR_ROT_90,  TR_ROT_270, TR_ROT_90,  TR_ROT_270, TR_ROT_90},
504 
505             Entry{TR_IDENT,   TR_ROT_270, TR_ROT_180, TR_IDENT,   TR_ROT_90},
506             Entry{TR_ROT_270, TR_ROT_270, TR_ROT_180, TR_ROT_90,  TR_IDENT},
507             Entry{TR_ROT_180, TR_ROT_270, TR_ROT_180, TR_ROT_180, TR_ROT_270},
508             Entry{TR_ROT_270, TR_ROT_270, TR_ROT_180, TR_ROT_270, TR_ROT_180},
509 
510             Entry{TR_IDENT,   TR_ROT_270, TR_ROT_270, TR_IDENT,   TR_ROT_180},
511             Entry{TR_ROT_90,  TR_ROT_270, TR_ROT_270, TR_ROT_90,  TR_ROT_90},
512             Entry{TR_ROT_270, TR_ROT_270, TR_ROT_270, TR_ROT_180, TR_IDENT},
513             Entry{TR_ROT_270, TR_ROT_270, TR_ROT_270, TR_ROT_270, TR_ROT_270},
514             // clang-format on
515     };
516 
517     for (size_t i = 0; i < testData.size(); i++) {
518         const auto& entry = testData[i];
519 
520         mLayerFEState.geomLayerTransform.set(entry.layer, 1920, 1080);
521         mLayerFEState.geomBufferTransform = entry.buffer;
522         mOutputState.displaySpace.setOrientation(toRotation(entry.display));
523         mOutputState.transform = ui::Transform{entry.display};
524 
525         const auto actual = mOutputLayer.calculateOutputRelativeBufferTransform(entry.internal);
526         EXPECT_EQ(entry.expected, actual) << "entry " << i;
527     }
528 }
529 
530 /*
531  * OutputLayer::updateCompositionState()
532  */
533 
534 struct OutputLayerPartialMockForUpdateCompositionState : public impl::OutputLayer {
OutputLayerPartialMockForUpdateCompositionStateandroid::compositionengine::__anon7a9c7e560111::OutputLayerPartialMockForUpdateCompositionState535     OutputLayerPartialMockForUpdateCompositionState(const compositionengine::Output& output,
536                                                     compositionengine::LayerFE& layerFE)
537           : mOutput(output), mLayerFE(layerFE) {}
538     // Mock everything called by updateCompositionState to simplify testing it.
539     MOCK_CONST_METHOD1(calculateOutputSourceCrop, FloatRect(uint32_t));
540     MOCK_CONST_METHOD0(calculateOutputDisplayFrame, Rect());
541     MOCK_CONST_METHOD1(calculateOutputRelativeBufferTransform, uint32_t(uint32_t));
542 
543     // compositionengine::OutputLayer overrides
getOutputandroid::compositionengine::__anon7a9c7e560111::OutputLayerPartialMockForUpdateCompositionState544     const compositionengine::Output& getOutput() const override { return mOutput; }
getLayerFEandroid::compositionengine::__anon7a9c7e560111::OutputLayerPartialMockForUpdateCompositionState545     compositionengine::LayerFE& getLayerFE() const override { return mLayerFE; }
getStateandroid::compositionengine::__anon7a9c7e560111::OutputLayerPartialMockForUpdateCompositionState546     const impl::OutputLayerCompositionState& getState() const override { return mState; }
editStateandroid::compositionengine::__anon7a9c7e560111::OutputLayerPartialMockForUpdateCompositionState547     impl::OutputLayerCompositionState& editState() override { return mState; }
548 
549     // These need implementations though are not expected to be called.
550     MOCK_CONST_METHOD1(dumpState, void(std::string&));
551 
552     const compositionengine::Output& mOutput;
553     compositionengine::LayerFE& mLayerFE;
554     impl::OutputLayerCompositionState mState;
555 };
556 
557 struct OutputLayerUpdateCompositionStateTest : public OutputLayerTest {
558 public:
OutputLayerUpdateCompositionStateTestandroid::compositionengine::__anon7a9c7e560111::OutputLayerUpdateCompositionStateTest559     OutputLayerUpdateCompositionStateTest() {
560         EXPECT_CALL(mOutput, getState()).WillRepeatedly(ReturnRef(mOutputState));
561         EXPECT_CALL(mOutput, getDisplayColorProfile())
562                 .WillRepeatedly(Return(&mDisplayColorProfile));
563         EXPECT_CALL(mDisplayColorProfile, isDataspaceSupported(_)).WillRepeatedly(Return(true));
564     }
565 
566     ~OutputLayerUpdateCompositionStateTest() = default;
567 
setupGeometryChildCallValuesandroid::compositionengine::__anon7a9c7e560111::OutputLayerUpdateCompositionStateTest568     void setupGeometryChildCallValues(ui::Transform::RotationFlags internalDisplayRotationFlags) {
569         EXPECT_CALL(mOutputLayer, calculateOutputSourceCrop(internalDisplayRotationFlags))
570                 .WillOnce(Return(kSourceCrop));
571         EXPECT_CALL(mOutputLayer, calculateOutputDisplayFrame()).WillOnce(Return(kDisplayFrame));
572         EXPECT_CALL(mOutputLayer,
573                     calculateOutputRelativeBufferTransform(internalDisplayRotationFlags))
574                 .WillOnce(Return(mBufferTransform));
575     }
576 
validateComputedGeometryStateandroid::compositionengine::__anon7a9c7e560111::OutputLayerUpdateCompositionStateTest577     void validateComputedGeometryState() {
578         const auto& state = mOutputLayer.getState();
579         EXPECT_EQ(kSourceCrop, state.sourceCrop);
580         EXPECT_EQ(kDisplayFrame, state.displayFrame);
581         EXPECT_EQ(static_cast<Hwc2::Transform>(mBufferTransform), state.bufferTransform);
582     }
583 
584     const FloatRect kSourceCrop{1.f, 2.f, 3.f, 4.f};
585     const Rect kDisplayFrame{11, 12, 13, 14};
586     uint32_t mBufferTransform{21};
587 
588     using OutputLayer = OutputLayerPartialMockForUpdateCompositionState;
589     StrictMock<OutputLayer> mOutputLayer{mOutput, mLayerFE};
590     StrictMock<mock::DisplayColorProfile> mDisplayColorProfile;
591 };
592 
TEST_F(OutputLayerUpdateCompositionStateTest,doesNothingIfNoFECompositionState)593 TEST_F(OutputLayerUpdateCompositionStateTest, doesNothingIfNoFECompositionState) {
594     EXPECT_CALL(mLayerFE, getCompositionState()).WillOnce(Return(nullptr));
595 
596     mOutputLayer.updateCompositionState(true, false, ui::Transform::RotationFlags::ROT_90);
597 }
598 
TEST_F(OutputLayerUpdateCompositionStateTest,setsStateNormally)599 TEST_F(OutputLayerUpdateCompositionStateTest, setsStateNormally) {
600     mLayerFEState.isSecure = true;
601     mOutputState.isSecure = true;
602     mOutputLayer.editState().forceClientComposition = true;
603 
604     setupGeometryChildCallValues(ui::Transform::RotationFlags::ROT_90);
605 
606     mOutputLayer.updateCompositionState(true, false, ui::Transform::RotationFlags::ROT_90);
607 
608     validateComputedGeometryState();
609 
610     EXPECT_EQ(false, mOutputLayer.getState().forceClientComposition);
611 }
612 
TEST_F(OutputLayerUpdateCompositionStateTest,alsoSetsForceCompositionIfSecureLayerOnNonsecureOutput)613 TEST_F(OutputLayerUpdateCompositionStateTest,
614        alsoSetsForceCompositionIfSecureLayerOnNonsecureOutput) {
615     mLayerFEState.isSecure = true;
616     mOutputState.isSecure = false;
617 
618     setupGeometryChildCallValues(ui::Transform::RotationFlags::ROT_0);
619 
620     mOutputLayer.updateCompositionState(true, false, ui::Transform::RotationFlags::ROT_0);
621 
622     validateComputedGeometryState();
623 
624     EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
625 }
626 
TEST_F(OutputLayerUpdateCompositionStateTest,alsoSetsForceCompositionIfUnsupportedBufferTransform)627 TEST_F(OutputLayerUpdateCompositionStateTest,
628        alsoSetsForceCompositionIfUnsupportedBufferTransform) {
629     mLayerFEState.isSecure = true;
630     mOutputState.isSecure = true;
631 
632     mBufferTransform = ui::Transform::ROT_INVALID;
633 
634     setupGeometryChildCallValues(ui::Transform::RotationFlags::ROT_0);
635 
636     mOutputLayer.updateCompositionState(true, false, ui::Transform::RotationFlags::ROT_0);
637 
638     validateComputedGeometryState();
639 
640     EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
641 }
642 
TEST_F(OutputLayerUpdateCompositionStateTest,setsOutputLayerColorspaceCorrectly)643 TEST_F(OutputLayerUpdateCompositionStateTest, setsOutputLayerColorspaceCorrectly) {
644     mLayerFEState.dataspace = ui::Dataspace::DISPLAY_P3;
645     mOutputState.targetDataspace = ui::Dataspace::V0_SCRGB;
646 
647     // If the layer is not colorspace agnostic, the output layer dataspace
648     // should use the layers requested colorspace.
649     mLayerFEState.isColorspaceAgnostic = false;
650 
651     mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
652 
653     EXPECT_EQ(ui::Dataspace::DISPLAY_P3, mOutputLayer.getState().dataspace);
654 
655     // If the layer is colorspace agnostic, the output layer dataspace
656     // should use the colorspace chosen for the whole output.
657     mLayerFEState.isColorspaceAgnostic = true;
658 
659     mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
660 
661     EXPECT_EQ(ui::Dataspace::V0_SCRGB, mOutputLayer.getState().dataspace);
662 }
663 
TEST_F(OutputLayerUpdateCompositionStateTest,setsOutputLayerColorspaceWith170mReplacement)664 TEST_F(OutputLayerUpdateCompositionStateTest, setsOutputLayerColorspaceWith170mReplacement) {
665     mLayerFEState.dataspace = ui::Dataspace::TRANSFER_SMPTE_170M;
666     mOutputState.targetDataspace = ui::Dataspace::V0_SCRGB;
667     mOutputState.treat170mAsSrgb = false;
668     mLayerFEState.isColorspaceAgnostic = false;
669 
670     mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
671 
672     EXPECT_EQ(ui::Dataspace::TRANSFER_SMPTE_170M, mOutputLayer.getState().dataspace);
673 
674     // Rewrite SMPTE 170M as sRGB
675     mOutputState.treat170mAsSrgb = true;
676     mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
677 
678     EXPECT_EQ(ui::Dataspace::TRANSFER_SRGB, mOutputLayer.getState().dataspace);
679 }
680 
TEST_F(OutputLayerUpdateCompositionStateTest,setsWhitePointNitsAndDimmingRatioCorrectly)681 TEST_F(OutputLayerUpdateCompositionStateTest, setsWhitePointNitsAndDimmingRatioCorrectly) {
682     mOutputState.sdrWhitePointNits = 200.f;
683     mOutputState.displayBrightnessNits = 800.f;
684 
685     mLayerFEState.dataspace = ui::Dataspace::DISPLAY_P3;
686     mLayerFEState.isColorspaceAgnostic = false;
687     mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
688     EXPECT_EQ(mOutputState.sdrWhitePointNits, mOutputLayer.getState().whitePointNits);
689     EXPECT_EQ(mOutputState.sdrWhitePointNits / mOutputState.displayBrightnessNits,
690               mOutputLayer.getState().dimmingRatio);
691 
692     mLayerFEState.dimmingEnabled = false;
693     mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
694     EXPECT_EQ(mOutputState.displayBrightnessNits, mOutputLayer.getState().whitePointNits);
695     EXPECT_EQ(1.f, mOutputLayer.getState().dimmingRatio);
696 
697     // change dimmingEnabled back to true.
698     mLayerFEState.dimmingEnabled = true;
699     mLayerFEState.dataspace = ui::Dataspace::BT2020_ITU_PQ;
700     mLayerFEState.isColorspaceAgnostic = false;
701     mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
702 
703     EXPECT_EQ(mOutputState.displayBrightnessNits, mOutputLayer.getState().whitePointNits);
704     EXPECT_EQ(1.f, mOutputLayer.getState().dimmingRatio);
705 }
706 
TEST_F(OutputLayerUpdateCompositionStateTest,doesNotRecomputeGeometryIfNotRequested)707 TEST_F(OutputLayerUpdateCompositionStateTest, doesNotRecomputeGeometryIfNotRequested) {
708     mOutputLayer.editState().forceClientComposition = false;
709 
710     mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
711 
712     EXPECT_EQ(false, mOutputLayer.getState().forceClientComposition);
713 }
714 
TEST_F(OutputLayerUpdateCompositionStateTest,doesNotClearForceClientCompositionIfNotDoingGeometry)715 TEST_F(OutputLayerUpdateCompositionStateTest,
716        doesNotClearForceClientCompositionIfNotDoingGeometry) {
717     mOutputLayer.editState().forceClientComposition = true;
718 
719     mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
720 
721     EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
722 }
723 
TEST_F(OutputLayerUpdateCompositionStateTest,clientCompositionForcedFromFrontEndFlagAtAnyTime)724 TEST_F(OutputLayerUpdateCompositionStateTest, clientCompositionForcedFromFrontEndFlagAtAnyTime) {
725     mLayerFEState.forceClientComposition = true;
726     mOutputLayer.editState().forceClientComposition = false;
727 
728     mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
729 
730     EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
731 }
732 
TEST_F(OutputLayerUpdateCompositionStateTest,clientCompositionForcedFromUnsupportedDataspaceAtAnyTime)733 TEST_F(OutputLayerUpdateCompositionStateTest,
734        clientCompositionForcedFromUnsupportedDataspaceAtAnyTime) {
735     mOutputLayer.editState().forceClientComposition = false;
736     EXPECT_CALL(mDisplayColorProfile, isDataspaceSupported(_)).WillRepeatedly(Return(false));
737 
738     mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
739 
740     EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
741 }
742 
TEST_F(OutputLayerUpdateCompositionStateTest,clientCompositionForcedFromArgumentFlag)743 TEST_F(OutputLayerUpdateCompositionStateTest, clientCompositionForcedFromArgumentFlag) {
744     mLayerFEState.forceClientComposition = false;
745     mOutputLayer.editState().forceClientComposition = false;
746 
747     mOutputLayer.updateCompositionState(false, true, ui::Transform::RotationFlags::ROT_0);
748 
749     EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
750 
751     mOutputLayer.editState().forceClientComposition = false;
752 
753     setupGeometryChildCallValues(ui::Transform::RotationFlags::ROT_0);
754 
755     mOutputLayer.updateCompositionState(true, true, ui::Transform::RotationFlags::ROT_0);
756 
757     EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
758 }
759 
760 /*
761  * OutputLayer::writeStateToHWC()
762  */
763 
764 struct OutputLayerWriteStateToHWCTest : public OutputLayerTest {
765     static constexpr hal::Error kError = hal::Error::UNSUPPORTED;
766     static constexpr FloatRect kSourceCrop{11.f, 12.f, 13.f, 14.f};
767     static constexpr Hwc2::Transform kBufferTransform = static_cast<Hwc2::Transform>(31);
768     static constexpr Hwc2::Transform kOverrideBufferTransform = static_cast<Hwc2::Transform>(0);
769     static constexpr Hwc2::IComposerClient::BlendMode kBlendMode =
770             static_cast<Hwc2::IComposerClient::BlendMode>(41);
771     static constexpr Hwc2::IComposerClient::BlendMode kOverrideBlendMode =
772             Hwc2::IComposerClient::BlendMode::PREMULTIPLIED;
773     static constexpr float kAlpha = 51.f;
774     static constexpr float kOverrideAlpha = 1.f;
775     static constexpr float kSkipAlpha = 0.f;
776     static constexpr ui::Dataspace kDataspace = static_cast<ui::Dataspace>(71);
777     static constexpr ui::Dataspace kOverrideDataspace = static_cast<ui::Dataspace>(72);
778     static constexpr int kSupportedPerFrameMetadata = 101;
779     static constexpr int kExpectedHwcSlot = 0;
780     static constexpr int kOverrideHwcSlot = impl::HwcBufferCache::kOverrideBufferSlot;
781     static constexpr bool kLayerGenericMetadata1Mandatory = true;
782     static constexpr bool kLayerGenericMetadata2Mandatory = true;
783     static constexpr float kWhitePointNits = 200.f;
784     static constexpr float kSdrWhitePointNits = 100.f;
785     static constexpr float kDisplayBrightnessNits = 400.f;
786     static constexpr float kLayerBrightness = kWhitePointNits / kDisplayBrightnessNits;
787     static constexpr float kOverrideLayerBrightness = kSdrWhitePointNits / kDisplayBrightnessNits;
788 
789     static const half4 kColor;
790     static const Rect kDisplayFrame;
791     static const Rect kOverrideDisplayFrame;
792     static const FloatRect kOverrideSourceCrop;
793     static const Region kOutputSpaceVisibleRegion;
794     static const Region kOverrideVisibleRegion;
795     static const mat4 kColorTransform;
796     static const Region kSurfaceDamage;
797     static const Region kOverrideSurfaceDamage;
798     static const HdrMetadata kHdrMetadata;
799     static native_handle_t* kSidebandStreamHandle;
800     static const sp<GraphicBuffer> kBuffer;
801     static const sp<GraphicBuffer> kOverrideBuffer;
802     static const sp<Fence> kFence;
803     static const sp<Fence> kOverrideFence;
804     static const std::string kLayerGenericMetadata1Key;
805     static const std::vector<uint8_t> kLayerGenericMetadata1Value;
806     static const std::string kLayerGenericMetadata2Key;
807     static const std::vector<uint8_t> kLayerGenericMetadata2Value;
808 
OutputLayerWriteStateToHWCTestandroid::compositionengine::__anon7a9c7e560111::OutputLayerWriteStateToHWCTest809     OutputLayerWriteStateToHWCTest() {
810         auto& outputLayerState = mOutputLayer.editState();
811         outputLayerState.hwc = impl::OutputLayerCompositionState::Hwc(mHwcLayer);
812 
813         outputLayerState.displayFrame = kDisplayFrame;
814         outputLayerState.sourceCrop = kSourceCrop;
815         outputLayerState.bufferTransform = static_cast<Hwc2::Transform>(kBufferTransform);
816         outputLayerState.outputSpaceVisibleRegion = kOutputSpaceVisibleRegion;
817         outputLayerState.dataspace = kDataspace;
818         outputLayerState.whitePointNits = kWhitePointNits;
819         outputLayerState.dimmingRatio = kLayerBrightness;
820 
821         mLayerFEState.blendMode = kBlendMode;
822         mLayerFEState.alpha = kAlpha;
823         mLayerFEState.colorTransform = kColorTransform;
824         mLayerFEState.color = kColor;
825         mLayerFEState.surfaceDamage = kSurfaceDamage;
826         mLayerFEState.hdrMetadata = kHdrMetadata;
827         mLayerFEState.sidebandStream = NativeHandle::create(kSidebandStreamHandle, false);
828         mLayerFEState.buffer = kBuffer;
829         mLayerFEState.acquireFence = kFence;
830 
831         mOutputState.displayBrightnessNits = kDisplayBrightnessNits;
832         mOutputState.sdrWhitePointNits = kSdrWhitePointNits;
833 
834         EXPECT_CALL(mOutput, getDisplayColorProfile())
835                 .WillRepeatedly(Return(&mDisplayColorProfile));
836         EXPECT_CALL(mDisplayColorProfile, getSupportedPerFrameMetadata())
837                 .WillRepeatedly(Return(kSupportedPerFrameMetadata));
838     }
839     // Some tests may need to simulate unsupported HWC calls
840     enum class SimulateUnsupported { None, ColorTransform };
841 
includeGenericLayerMetadataInStateandroid::compositionengine::__anon7a9c7e560111::OutputLayerWriteStateToHWCTest842     void includeGenericLayerMetadataInState() {
843         mLayerFEState.metadata[kLayerGenericMetadata1Key] = {kLayerGenericMetadata1Mandatory,
844                                                              kLayerGenericMetadata1Value};
845         mLayerFEState.metadata[kLayerGenericMetadata2Key] = {kLayerGenericMetadata2Mandatory,
846                                                              kLayerGenericMetadata2Value};
847     }
848 
includeOverrideInfoandroid::compositionengine::__anon7a9c7e560111::OutputLayerWriteStateToHWCTest849     void includeOverrideInfo() {
850         auto& overrideInfo = mOutputLayer.editState().overrideInfo;
851 
852         overrideInfo.buffer = std::make_shared<
853                 renderengine::impl::ExternalTexture>(kOverrideBuffer, mRenderEngine,
854                                                      renderengine::impl::ExternalTexture::Usage::
855                                                                      READABLE |
856                                                              renderengine::impl::ExternalTexture::
857                                                                      Usage::WRITEABLE);
858         overrideInfo.acquireFence = kOverrideFence;
859         overrideInfo.displayFrame = kOverrideDisplayFrame;
860         overrideInfo.dataspace = kOverrideDataspace;
861         overrideInfo.damageRegion = kOverrideSurfaceDamage;
862         overrideInfo.visibleRegion = kOverrideVisibleRegion;
863     }
864 
expectGeometryCommonCallsandroid::compositionengine::__anon7a9c7e560111::OutputLayerWriteStateToHWCTest865     void expectGeometryCommonCalls(Rect displayFrame = kDisplayFrame,
866                                    FloatRect sourceCrop = kSourceCrop,
867                                    Hwc2::Transform bufferTransform = kBufferTransform,
868                                    Hwc2::IComposerClient::BlendMode blendMode = kBlendMode,
869                                    float alpha = kAlpha) {
870         EXPECT_CALL(*mHwcLayer, setDisplayFrame(displayFrame)).WillOnce(Return(kError));
871         EXPECT_CALL(*mHwcLayer, setSourceCrop(sourceCrop)).WillOnce(Return(kError));
872         EXPECT_CALL(*mHwcLayer, setZOrder(_)).WillOnce(Return(kError));
873         EXPECT_CALL(*mHwcLayer, setTransform(bufferTransform)).WillOnce(Return(kError));
874 
875         EXPECT_CALL(*mHwcLayer, setBlendMode(blendMode)).WillOnce(Return(kError));
876         EXPECT_CALL(*mHwcLayer, setPlaneAlpha(alpha)).WillOnce(Return(kError));
877     }
878 
expectPerFrameCommonCallsandroid::compositionengine::__anon7a9c7e560111::OutputLayerWriteStateToHWCTest879     void expectPerFrameCommonCalls(SimulateUnsupported unsupported = SimulateUnsupported::None,
880                                    ui::Dataspace dataspace = kDataspace,
881                                    const Region& visibleRegion = kOutputSpaceVisibleRegion,
882                                    const Region& surfaceDamage = kSurfaceDamage,
883                                    float brightness = kLayerBrightness,
884                                    const Region& blockingRegion = Region()) {
885         EXPECT_CALL(*mHwcLayer, setVisibleRegion(RegionEq(visibleRegion))).WillOnce(Return(kError));
886         EXPECT_CALL(*mHwcLayer, setDataspace(dataspace)).WillOnce(Return(kError));
887         EXPECT_CALL(*mHwcLayer, setBrightness(brightness)).WillOnce(Return(kError));
888         EXPECT_CALL(*mHwcLayer, setColorTransform(kColorTransform))
889                 .WillOnce(Return(unsupported == SimulateUnsupported::ColorTransform
890                                          ? hal::Error::UNSUPPORTED
891                                          : hal::Error::NONE));
892         EXPECT_CALL(*mHwcLayer, setSurfaceDamage(RegionEq(surfaceDamage))).WillOnce(Return(kError));
893         EXPECT_CALL(*mHwcLayer, setBlockingRegion(RegionEq(blockingRegion)))
894                 .WillOnce(Return(kError));
895     }
896 
expectSetCompositionTypeCallandroid::compositionengine::__anon7a9c7e560111::OutputLayerWriteStateToHWCTest897     void expectSetCompositionTypeCall(Composition compositionType) {
898         EXPECT_CALL(*mHwcLayer, setCompositionType(compositionType)).WillOnce(Return(kError));
899     }
900 
expectNoSetCompositionTypeCallandroid::compositionengine::__anon7a9c7e560111::OutputLayerWriteStateToHWCTest901     void expectNoSetCompositionTypeCall() {
902         EXPECT_CALL(*mHwcLayer, setCompositionType(_)).Times(0);
903     }
904 
expectSetColorCallandroid::compositionengine::__anon7a9c7e560111::OutputLayerWriteStateToHWCTest905     void expectSetColorCall() {
906         const aidl::android::hardware::graphics::composer3::Color color = {kColor.r, kColor.g,
907                                                                            kColor.b, 1.0f};
908 
909         EXPECT_CALL(*mHwcLayer, setColor(ColorEq(color))).WillOnce(Return(kError));
910     }
911 
expectSetSidebandHandleCallandroid::compositionengine::__anon7a9c7e560111::OutputLayerWriteStateToHWCTest912     void expectSetSidebandHandleCall() {
913         EXPECT_CALL(*mHwcLayer, setSidebandStream(kSidebandStreamHandle));
914     }
915 
expectSetHdrMetadataAndBufferCallsandroid::compositionengine::__anon7a9c7e560111::OutputLayerWriteStateToHWCTest916     void expectSetHdrMetadataAndBufferCalls(uint32_t hwcSlot = kExpectedHwcSlot,
917                                             sp<GraphicBuffer> buffer = kBuffer,
918                                             sp<Fence> fence = kFence) {
919         EXPECT_CALL(*mHwcLayer, setPerFrameMetadata(kSupportedPerFrameMetadata, kHdrMetadata));
920         EXPECT_CALL(*mHwcLayer, setBuffer(hwcSlot, buffer, fence));
921     }
922 
expectGenericLayerMetadataCallsandroid::compositionengine::__anon7a9c7e560111::OutputLayerWriteStateToHWCTest923     void expectGenericLayerMetadataCalls() {
924         // Note: Can be in any order.
925         EXPECT_CALL(*mHwcLayer,
926                     setLayerGenericMetadata(kLayerGenericMetadata1Key,
927                                             kLayerGenericMetadata1Mandatory,
928                                             kLayerGenericMetadata1Value));
929         EXPECT_CALL(*mHwcLayer,
930                     setLayerGenericMetadata(kLayerGenericMetadata2Key,
931                                             kLayerGenericMetadata2Mandatory,
932                                             kLayerGenericMetadata2Value));
933     }
934 
935     std::shared_ptr<HWC2::mock::Layer> mHwcLayer{std::make_shared<StrictMock<HWC2::mock::Layer>>()};
936     StrictMock<mock::DisplayColorProfile> mDisplayColorProfile;
937     renderengine::mock::RenderEngine mRenderEngine;
938 };
939 
940 const half4 OutputLayerWriteStateToHWCTest::kColor{81.f / 255.f, 82.f / 255.f, 83.f / 255.f,
941                                                    84.f / 255.f};
942 const Rect OutputLayerWriteStateToHWCTest::kDisplayFrame{1001, 1002, 1003, 10044};
943 const Rect OutputLayerWriteStateToHWCTest::kOverrideDisplayFrame{1002, 1003, 1004, 20044};
944 const FloatRect OutputLayerWriteStateToHWCTest::kOverrideSourceCrop{0.f, 0.f, 4.f, 5.f};
945 const Region OutputLayerWriteStateToHWCTest::kOutputSpaceVisibleRegion{
946         Rect{1005, 1006, 1007, 1008}};
947 const Region OutputLayerWriteStateToHWCTest::kOverrideVisibleRegion{Rect{1006, 1007, 1008, 1009}};
948 const mat4 OutputLayerWriteStateToHWCTest::kColorTransform{
949         1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016,
950         1017, 1018, 1019, 1020, 1021, 1022, 1023, 1024,
951 };
952 const Region OutputLayerWriteStateToHWCTest::kSurfaceDamage{Rect{1025, 1026, 1027, 1028}};
953 const Region OutputLayerWriteStateToHWCTest::kOverrideSurfaceDamage{Rect{1026, 1027, 1028, 1029}};
954 const HdrMetadata OutputLayerWriteStateToHWCTest::kHdrMetadata{{/* LightFlattenable */}, 1029};
955 native_handle_t* OutputLayerWriteStateToHWCTest::kSidebandStreamHandle =
956         reinterpret_cast<native_handle_t*>(1031);
957 const sp<GraphicBuffer> OutputLayerWriteStateToHWCTest::kBuffer =
958         sp<GraphicBuffer>::make(1, 2, PIXEL_FORMAT_RGBA_8888,
959                                 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN |
960                                         AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN);
961 const sp<GraphicBuffer> OutputLayerWriteStateToHWCTest::kOverrideBuffer =
962         sp<GraphicBuffer>::make(4, 5, PIXEL_FORMAT_RGBA_8888,
963                                 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN |
964                                         AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN);
965 const sp<Fence> OutputLayerWriteStateToHWCTest::kFence;
966 const sp<Fence> OutputLayerWriteStateToHWCTest::kOverrideFence = sp<Fence>::make();
967 const std::string OutputLayerWriteStateToHWCTest::kLayerGenericMetadata1Key =
968         "com.example.metadata.1";
969 const std::vector<uint8_t> OutputLayerWriteStateToHWCTest::kLayerGenericMetadata1Value{{1, 2, 3}};
970 const std::string OutputLayerWriteStateToHWCTest::kLayerGenericMetadata2Key =
971         "com.example.metadata.2";
972 const std::vector<uint8_t> OutputLayerWriteStateToHWCTest::kLayerGenericMetadata2Value{
973         {4, 5, 6, 7}};
974 
TEST_F(OutputLayerWriteStateToHWCTest,doesNothingIfNoFECompositionState)975 TEST_F(OutputLayerWriteStateToHWCTest, doesNothingIfNoFECompositionState) {
976     EXPECT_CALL(mLayerFE, getCompositionState()).WillOnce(Return(nullptr));
977 
978     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
979                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
980 }
981 
TEST_F(OutputLayerWriteStateToHWCTest,doesNothingIfNoHWCState)982 TEST_F(OutputLayerWriteStateToHWCTest, doesNothingIfNoHWCState) {
983     mOutputLayer.editState().hwc.reset();
984 
985     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
986                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
987 }
988 
TEST_F(OutputLayerWriteStateToHWCTest,doesNothingIfNoHWCLayer)989 TEST_F(OutputLayerWriteStateToHWCTest, doesNothingIfNoHWCLayer) {
990     mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc(nullptr);
991 
992     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
993                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
994 }
995 
TEST_F(OutputLayerWriteStateToHWCTest,canSetAllState)996 TEST_F(OutputLayerWriteStateToHWCTest, canSetAllState) {
997     expectGeometryCommonCalls();
998     expectPerFrameCommonCalls();
999 
1000     expectNoSetCompositionTypeCall();
1001     EXPECT_CALL(mLayerFE, hasRoundedCorners()).WillOnce(Return(false));
1002 
1003     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1004                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1005 }
1006 
TEST_F(OutputLayerTest,displayInstallOrientationBufferTransformSetTo90)1007 TEST_F(OutputLayerTest, displayInstallOrientationBufferTransformSetTo90) {
1008     mLayerFEState.geomBufferUsesDisplayInverseTransform = false;
1009     mLayerFEState.geomLayerTransform = ui::Transform{TR_IDENT};
1010     // This test simulates a scenario where displayInstallOrientation is set to
1011     // ROT_90. This only has an effect on the transform; orientation stays 0 (see
1012     // DisplayDevice::setProjection).
1013     mOutputState.displaySpace.setOrientation(ui::ROTATION_0);
1014     mOutputState.transform = ui::Transform{TR_ROT_90};
1015     // Buffers are pre-rotated based on the transform hint (ROT_90); their
1016     // geomBufferTransform is set to the inverse transform.
1017     mLayerFEState.geomBufferTransform = TR_ROT_270;
1018 
1019     EXPECT_EQ(TR_IDENT, mOutputLayer.calculateOutputRelativeBufferTransform(ui::Transform::ROT_90));
1020 }
1021 
TEST_F(OutputLayerWriteStateToHWCTest,canSetPerFrameStateForSolidColor)1022 TEST_F(OutputLayerWriteStateToHWCTest, canSetPerFrameStateForSolidColor) {
1023     mLayerFEState.compositionType = Composition::SOLID_COLOR;
1024 
1025     expectPerFrameCommonCalls();
1026 
1027     // Setting the composition type should happen before setting the color. We
1028     // check this in this test only by setting up an testing::InSeqeuence
1029     // instance before setting up the two expectations.
1030     InSequence s;
1031     expectSetCompositionTypeCall(Composition::SOLID_COLOR);
1032     expectSetColorCall();
1033 
1034     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1035                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1036 }
1037 
TEST_F(OutputLayerWriteStateToHWCTest,canSetPerFrameStateForSideband)1038 TEST_F(OutputLayerWriteStateToHWCTest, canSetPerFrameStateForSideband) {
1039     mLayerFEState.compositionType = Composition::SIDEBAND;
1040 
1041     expectPerFrameCommonCalls();
1042     expectSetSidebandHandleCall();
1043     expectSetCompositionTypeCall(Composition::SIDEBAND);
1044 
1045     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1046                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1047 }
1048 
TEST_F(OutputLayerWriteStateToHWCTest,canSetPerFrameStateForCursor)1049 TEST_F(OutputLayerWriteStateToHWCTest, canSetPerFrameStateForCursor) {
1050     mLayerFEState.compositionType = Composition::CURSOR;
1051 
1052     expectPerFrameCommonCalls();
1053     expectSetHdrMetadataAndBufferCalls();
1054     expectSetCompositionTypeCall(Composition::CURSOR);
1055 
1056     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1057                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1058 }
1059 
TEST_F(OutputLayerWriteStateToHWCTest,canSetPerFrameStateForDevice)1060 TEST_F(OutputLayerWriteStateToHWCTest, canSetPerFrameStateForDevice) {
1061     mLayerFEState.compositionType = Composition::DEVICE;
1062 
1063     expectPerFrameCommonCalls();
1064     expectSetHdrMetadataAndBufferCalls();
1065     expectSetCompositionTypeCall(Composition::DEVICE);
1066 
1067     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1068                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1069 }
1070 
TEST_F(OutputLayerWriteStateToHWCTest,compositionTypeIsNotSetIfUnchanged)1071 TEST_F(OutputLayerWriteStateToHWCTest, compositionTypeIsNotSetIfUnchanged) {
1072     (*mOutputLayer.editState().hwc).hwcCompositionType = Composition::SOLID_COLOR;
1073 
1074     mLayerFEState.compositionType = Composition::SOLID_COLOR;
1075 
1076     expectPerFrameCommonCalls();
1077     expectSetColorCall();
1078     expectNoSetCompositionTypeCall();
1079 
1080     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1081                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1082 }
1083 
TEST_F(OutputLayerWriteStateToHWCTest,compositionTypeIsSetToClientIfColorTransformNotSupported)1084 TEST_F(OutputLayerWriteStateToHWCTest, compositionTypeIsSetToClientIfColorTransformNotSupported) {
1085     mLayerFEState.compositionType = Composition::SOLID_COLOR;
1086 
1087     expectPerFrameCommonCalls(SimulateUnsupported::ColorTransform);
1088     expectSetColorCall();
1089     expectSetCompositionTypeCall(Composition::CLIENT);
1090 
1091     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1092                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1093 }
1094 
TEST_F(OutputLayerWriteStateToHWCTest,compositionTypeIsSetToClientIfClientCompositionForced)1095 TEST_F(OutputLayerWriteStateToHWCTest, compositionTypeIsSetToClientIfClientCompositionForced) {
1096     mOutputLayer.editState().forceClientComposition = true;
1097 
1098     mLayerFEState.compositionType = Composition::SOLID_COLOR;
1099 
1100     expectPerFrameCommonCalls();
1101     expectSetColorCall();
1102     expectSetCompositionTypeCall(Composition::CLIENT);
1103 
1104     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1105                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1106 }
1107 
TEST_F(OutputLayerWriteStateToHWCTest,allStateIncludesMetadataIfPresent)1108 TEST_F(OutputLayerWriteStateToHWCTest, allStateIncludesMetadataIfPresent) {
1109     mLayerFEState.compositionType = Composition::DEVICE;
1110     includeGenericLayerMetadataInState();
1111 
1112     expectGeometryCommonCalls();
1113     expectPerFrameCommonCalls();
1114     expectSetHdrMetadataAndBufferCalls();
1115     expectGenericLayerMetadataCalls();
1116     expectSetCompositionTypeCall(Composition::DEVICE);
1117 
1118     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1119                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1120 }
1121 
TEST_F(OutputLayerWriteStateToHWCTest,perFrameStateDoesNotIncludeMetadataIfPresent)1122 TEST_F(OutputLayerWriteStateToHWCTest, perFrameStateDoesNotIncludeMetadataIfPresent) {
1123     mLayerFEState.compositionType = Composition::DEVICE;
1124     includeGenericLayerMetadataInState();
1125 
1126     expectPerFrameCommonCalls();
1127     expectSetHdrMetadataAndBufferCalls();
1128     expectSetCompositionTypeCall(Composition::DEVICE);
1129 
1130     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1131                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1132 }
1133 
TEST_F(OutputLayerWriteStateToHWCTest,overriddenSkipLayerDoesNotSendBuffer)1134 TEST_F(OutputLayerWriteStateToHWCTest, overriddenSkipLayerDoesNotSendBuffer) {
1135     mLayerFEState.compositionType = Composition::DEVICE;
1136     includeOverrideInfo();
1137 
1138     expectGeometryCommonCalls(kOverrideDisplayFrame, kOverrideSourceCrop, kOverrideBufferTransform,
1139                               kOverrideBlendMode, kSkipAlpha);
1140     expectPerFrameCommonCalls(SimulateUnsupported::None, kOverrideDataspace, kOverrideVisibleRegion,
1141                               kOverrideSurfaceDamage, kOverrideLayerBrightness);
1142     expectSetHdrMetadataAndBufferCalls();
1143     expectSetCompositionTypeCall(Composition::DEVICE);
1144 
1145     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ true, 0,
1146                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1147 }
1148 
TEST_F(OutputLayerWriteStateToHWCTest,overriddenSkipLayerForSolidColorDoesNotSendBuffer)1149 TEST_F(OutputLayerWriteStateToHWCTest, overriddenSkipLayerForSolidColorDoesNotSendBuffer) {
1150     mLayerFEState.compositionType = Composition::SOLID_COLOR;
1151     includeOverrideInfo();
1152 
1153     expectGeometryCommonCalls(kOverrideDisplayFrame, kOverrideSourceCrop, kOverrideBufferTransform,
1154                               kOverrideBlendMode, kSkipAlpha);
1155     expectPerFrameCommonCalls(SimulateUnsupported::None, kOverrideDataspace, kOverrideVisibleRegion,
1156                               kOverrideSurfaceDamage, kOverrideLayerBrightness);
1157     expectSetHdrMetadataAndBufferCalls();
1158     expectSetCompositionTypeCall(Composition::DEVICE);
1159 
1160     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ true, 0,
1161                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1162 }
1163 
TEST_F(OutputLayerWriteStateToHWCTest,includesOverrideInfoIfPresent)1164 TEST_F(OutputLayerWriteStateToHWCTest, includesOverrideInfoIfPresent) {
1165     mLayerFEState.compositionType = Composition::DEVICE;
1166     includeOverrideInfo();
1167 
1168     expectGeometryCommonCalls(kOverrideDisplayFrame, kOverrideSourceCrop, kOverrideBufferTransform,
1169                               kOverrideBlendMode, kOverrideAlpha);
1170     expectPerFrameCommonCalls(SimulateUnsupported::None, kOverrideDataspace, kOverrideVisibleRegion,
1171                               kOverrideSurfaceDamage, kOverrideLayerBrightness);
1172     expectSetHdrMetadataAndBufferCalls(kOverrideHwcSlot, kOverrideBuffer, kOverrideFence);
1173     expectSetCompositionTypeCall(Composition::DEVICE);
1174 
1175     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1176                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1177 }
1178 
TEST_F(OutputLayerWriteStateToHWCTest,includesOverrideInfoForSolidColorIfPresent)1179 TEST_F(OutputLayerWriteStateToHWCTest, includesOverrideInfoForSolidColorIfPresent) {
1180     mLayerFEState.compositionType = Composition::SOLID_COLOR;
1181     includeOverrideInfo();
1182 
1183     expectGeometryCommonCalls(kOverrideDisplayFrame, kOverrideSourceCrop, kOverrideBufferTransform,
1184                               kOverrideBlendMode, kOverrideAlpha);
1185     expectPerFrameCommonCalls(SimulateUnsupported::None, kOverrideDataspace, kOverrideVisibleRegion,
1186                               kOverrideSurfaceDamage, kOverrideLayerBrightness);
1187     expectSetHdrMetadataAndBufferCalls(kOverrideHwcSlot, kOverrideBuffer, kOverrideFence);
1188     expectSetCompositionTypeCall(Composition::DEVICE);
1189 
1190     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1191                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1192 }
1193 
TEST_F(OutputLayerWriteStateToHWCTest,previousOverriddenLayerSendsSurfaceDamage)1194 TEST_F(OutputLayerWriteStateToHWCTest, previousOverriddenLayerSendsSurfaceDamage) {
1195     mLayerFEState.compositionType = Composition::DEVICE;
1196     mOutputLayer.editState().hwc->stateOverridden = true;
1197 
1198     expectGeometryCommonCalls();
1199     expectPerFrameCommonCalls(SimulateUnsupported::None, kDataspace, kOutputSpaceVisibleRegion,
1200                               Region::INVALID_REGION);
1201     expectSetHdrMetadataAndBufferCalls();
1202     expectSetCompositionTypeCall(Composition::DEVICE);
1203 
1204     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1205                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1206 }
1207 
TEST_F(OutputLayerWriteStateToHWCTest,previousSkipLayerSendsUpdatedDeviceCompositionInfo)1208 TEST_F(OutputLayerWriteStateToHWCTest, previousSkipLayerSendsUpdatedDeviceCompositionInfo) {
1209     mLayerFEState.compositionType = Composition::DEVICE;
1210     mOutputLayer.editState().hwc->stateOverridden = true;
1211     mOutputLayer.editState().hwc->layerSkipped = true;
1212     mOutputLayer.editState().hwc->hwcCompositionType = Composition::DEVICE;
1213 
1214     expectGeometryCommonCalls();
1215     expectPerFrameCommonCalls(SimulateUnsupported::None, kDataspace, kOutputSpaceVisibleRegion,
1216                               Region::INVALID_REGION);
1217     expectSetHdrMetadataAndBufferCalls();
1218     expectSetCompositionTypeCall(Composition::DEVICE);
1219 
1220     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1221                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1222 }
1223 
TEST_F(OutputLayerWriteStateToHWCTest,previousSkipLayerSendsUpdatedClientCompositionInfo)1224 TEST_F(OutputLayerWriteStateToHWCTest, previousSkipLayerSendsUpdatedClientCompositionInfo) {
1225     mLayerFEState.compositionType = Composition::DEVICE;
1226     mOutputLayer.editState().forceClientComposition = true;
1227     mOutputLayer.editState().hwc->stateOverridden = true;
1228     mOutputLayer.editState().hwc->layerSkipped = true;
1229     mOutputLayer.editState().hwc->hwcCompositionType = Composition::CLIENT;
1230 
1231     expectGeometryCommonCalls();
1232     expectPerFrameCommonCalls(SimulateUnsupported::None, kDataspace, kOutputSpaceVisibleRegion,
1233                               Region::INVALID_REGION);
1234     expectSetHdrMetadataAndBufferCalls();
1235     expectSetCompositionTypeCall(Composition::CLIENT);
1236 
1237     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1238                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1239 }
1240 
TEST_F(OutputLayerWriteStateToHWCTest,peekThroughChangesBlendMode)1241 TEST_F(OutputLayerWriteStateToHWCTest, peekThroughChangesBlendMode) {
1242     auto peekThroughLayerFE = sp<NiceMock<compositionengine::mock::LayerFE>>::make();
1243     OutputLayer peekThroughLayer{mOutput, *peekThroughLayerFE};
1244 
1245     mOutputLayer.mState.overrideInfo.peekThroughLayer = &peekThroughLayer;
1246 
1247     expectGeometryCommonCalls(kDisplayFrame, kSourceCrop, kBufferTransform,
1248                               Hwc2::IComposerClient::BlendMode::PREMULTIPLIED);
1249     expectPerFrameCommonCalls();
1250 
1251     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1252                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1253 }
1254 
TEST_F(OutputLayerWriteStateToHWCTest,isPeekingThroughSetsOverride)1255 TEST_F(OutputLayerWriteStateToHWCTest, isPeekingThroughSetsOverride) {
1256     expectGeometryCommonCalls();
1257     expectPerFrameCommonCalls();
1258 
1259     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1260                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ true);
1261     EXPECT_TRUE(mOutputLayer.getState().hwc->stateOverridden);
1262 }
1263 
TEST_F(OutputLayerWriteStateToHWCTest,zIsOverriddenSetsOverride)1264 TEST_F(OutputLayerWriteStateToHWCTest, zIsOverriddenSetsOverride) {
1265     expectGeometryCommonCalls();
1266     expectPerFrameCommonCalls();
1267 
1268     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1269                                  /*zIsOverridden*/ true, /*isPeekingThrough*/
1270                                  false);
1271     EXPECT_TRUE(mOutputLayer.getState().hwc->stateOverridden);
1272 }
1273 
TEST_F(OutputLayerWriteStateToHWCTest,roundedCornersForceClientComposition)1274 TEST_F(OutputLayerWriteStateToHWCTest, roundedCornersForceClientComposition) {
1275     expectGeometryCommonCalls();
1276     expectPerFrameCommonCalls();
1277     EXPECT_CALL(mLayerFE, hasRoundedCorners()).WillOnce(Return(true));
1278     expectSetCompositionTypeCall(Composition::CLIENT);
1279 
1280     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1281                                  /*zIsOverridden*/ false, /*isPeekingThrough*/
1282                                  false);
1283 }
1284 
TEST_F(OutputLayerWriteStateToHWCTest,roundedCornersPeekingThroughAllowsDeviceComposition)1285 TEST_F(OutputLayerWriteStateToHWCTest, roundedCornersPeekingThroughAllowsDeviceComposition) {
1286     expectGeometryCommonCalls();
1287     expectPerFrameCommonCalls();
1288     expectSetHdrMetadataAndBufferCalls();
1289     EXPECT_CALL(mLayerFE, hasRoundedCorners()).WillRepeatedly(Return(true));
1290     expectSetCompositionTypeCall(Composition::DEVICE);
1291 
1292     mLayerFEState.compositionType = Composition::DEVICE;
1293     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1294                                  /*zIsOverridden*/ false, /*isPeekingThrough*/
1295                                  true);
1296     EXPECT_EQ(Composition::DEVICE, mOutputLayer.getState().hwc->hwcCompositionType);
1297 }
1298 
TEST_F(OutputLayerWriteStateToHWCTest,setBlockingRegion)1299 TEST_F(OutputLayerWriteStateToHWCTest, setBlockingRegion) {
1300     mLayerFEState.compositionType = Composition::DISPLAY_DECORATION;
1301     const auto blockingRegion = Region(Rect(0, 0, 1000, 1000));
1302     mOutputLayer.editState().outputSpaceBlockingRegionHint = blockingRegion;
1303 
1304     expectGeometryCommonCalls();
1305     expectPerFrameCommonCalls(SimulateUnsupported::None, kDataspace, kOutputSpaceVisibleRegion,
1306                               kSurfaceDamage, kLayerBrightness, blockingRegion);
1307     expectSetHdrMetadataAndBufferCalls();
1308     expectSetCompositionTypeCall(Composition::DISPLAY_DECORATION);
1309 
1310     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1311                                  /*zIsOverridden*/ false, /*isPeekingThrough*/
1312                                  false);
1313 }
1314 
TEST_F(OutputLayerWriteStateToHWCTest,setCompositionTypeRefreshRateIndicator)1315 TEST_F(OutputLayerWriteStateToHWCTest, setCompositionTypeRefreshRateIndicator) {
1316     mLayerFEState.compositionType = Composition::REFRESH_RATE_INDICATOR;
1317 
1318     expectGeometryCommonCalls();
1319     expectPerFrameCommonCalls();
1320     expectSetHdrMetadataAndBufferCalls();
1321     expectSetCompositionTypeCall(Composition::REFRESH_RATE_INDICATOR);
1322 
1323     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1324                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1325 }
1326 
1327 /*
1328  * OutputLayer::uncacheBuffers
1329  */
1330 struct OutputLayerUncacheBufferTest : public OutputLayerTest {
1331     static const sp<GraphicBuffer> kBuffer1;
1332     static const sp<GraphicBuffer> kBuffer2;
1333     static const sp<GraphicBuffer> kBuffer3;
1334     static const sp<Fence> kFence;
1335 
OutputLayerUncacheBufferTestandroid::compositionengine::__anon7a9c7e560111::OutputLayerUncacheBufferTest1336     OutputLayerUncacheBufferTest() {
1337         auto& outputLayerState = mOutputLayer.editState();
1338         outputLayerState.hwc = impl::OutputLayerCompositionState::Hwc(mHwcLayer_);
1339 
1340         mLayerFEState.compositionType = Composition::DEVICE;
1341         mLayerFEState.acquireFence = kFence;
1342 
1343         ON_CALL(mOutput, getDisplayColorProfile()).WillByDefault(Return(&mDisplayColorProfile));
1344     }
1345 
1346     std::shared_ptr<HWC2::mock::Layer> mHwcLayer_{std::make_shared<NiceMock<HWC2::mock::Layer>>()};
1347     HWC2::mock::Layer& mHwcLayer = *mHwcLayer_;
1348     NiceMock<mock::DisplayColorProfile> mDisplayColorProfile;
1349 };
1350 
1351 const sp<GraphicBuffer> OutputLayerUncacheBufferTest::kBuffer1 =
1352         sp<GraphicBuffer>::make(1, 2, PIXEL_FORMAT_RGBA_8888,
1353                                 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN |
1354                                         AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN);
1355 const sp<GraphicBuffer> OutputLayerUncacheBufferTest::kBuffer2 =
1356         sp<GraphicBuffer>::make(2, 3, PIXEL_FORMAT_RGBA_8888,
1357                                 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN |
1358                                         AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN);
1359 const sp<GraphicBuffer> OutputLayerUncacheBufferTest::kBuffer3 =
1360         sp<GraphicBuffer>::make(4, 5, PIXEL_FORMAT_RGBA_8888,
1361                                 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN |
1362                                         AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN);
1363 const sp<Fence> OutputLayerUncacheBufferTest::kFence = sp<Fence>::make();
1364 
TEST_F(OutputLayerUncacheBufferTest,canUncacheAndReuseSlot)1365 TEST_F(OutputLayerUncacheBufferTest, canUncacheAndReuseSlot) {
1366     // Buffer1 is stored in slot 0
1367     mLayerFEState.buffer = kBuffer1;
1368     EXPECT_CALL(mHwcLayer, setBuffer(/*slot*/ 0, kBuffer1, kFence));
1369     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1370                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1371     Mock::VerifyAndClearExpectations(&mHwcLayer);
1372 
1373     // Buffer2 is stored in slot 1
1374     mLayerFEState.buffer = kBuffer2;
1375     EXPECT_CALL(mHwcLayer, setBuffer(/*slot*/ 1, kBuffer2, kFence));
1376     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1377                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1378     Mock::VerifyAndClearExpectations(&mHwcLayer);
1379 
1380     // Buffer3 is stored in slot 2
1381     mLayerFEState.buffer = kBuffer3;
1382     EXPECT_CALL(mHwcLayer, setBuffer(/*slot*/ 2, kBuffer3, kFence));
1383     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1384                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1385     Mock::VerifyAndClearExpectations(&mHwcLayer);
1386 
1387     // Buffer2 becomes the active buffer again (with a nullptr) and reuses slot 1
1388     mLayerFEState.buffer = kBuffer2;
1389     sp<GraphicBuffer> nullBuffer = nullptr;
1390     EXPECT_CALL(mHwcLayer, setBuffer(/*slot*/ 1, nullBuffer, kFence));
1391     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1392                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1393     Mock::VerifyAndClearExpectations(&mHwcLayer);
1394 
1395     // Buffer slots are cleared
1396     std::vector<uint32_t> slotsToClear = {0, 2, 1}; // order doesn't matter
1397     EXPECT_CALL(mHwcLayer, setBufferSlotsToClear(slotsToClear, /*activeBufferSlot*/ 1));
1398     // Uncache the active buffer in between other buffers to exercise correct algorithmic behavior.
1399     mOutputLayer.uncacheBuffers({kBuffer1->getId(), kBuffer2->getId(), kBuffer3->getId()});
1400     Mock::VerifyAndClearExpectations(&mHwcLayer);
1401 
1402     // Buffer1 becomes active again, and rather than allocating a new slot, or re-using slot 0,
1403     // the active buffer slot (slot 1 for Buffer2) is reused first, which allows HWC to free the
1404     // memory for the active buffer. Note: slot 1 is different from the first and last buffer slot
1405     // requested to be cleared in slotsToClear (slot 1), above, indicating that the algorithm
1406     // correctly identifies the active buffer as the buffer in slot 1, despite ping-ponging.
1407     mLayerFEState.buffer = kBuffer1;
1408     EXPECT_CALL(mHwcLayer, setBuffer(/*slot*/ 1, kBuffer1, kFence));
1409     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1410                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1411     Mock::VerifyAndClearExpectations(&mHwcLayer);
1412 }
1413 
1414 /*
1415  * OutputLayer::writeCursorPositionToHWC()
1416  */
1417 
1418 struct OutputLayerWriteCursorPositionToHWCTest : public OutputLayerTest {
1419     static constexpr int kDefaultTransform = TR_IDENT;
1420     static constexpr hal::Error kDefaultError = hal::Error::UNSUPPORTED;
1421 
1422     static const Rect kDefaultDisplayViewport;
1423     static const Rect kDefaultCursorFrame;
1424 
OutputLayerWriteCursorPositionToHWCTestandroid::compositionengine::__anon7a9c7e560111::OutputLayerWriteCursorPositionToHWCTest1425     OutputLayerWriteCursorPositionToHWCTest() {
1426         auto& outputLayerState = mOutputLayer.editState();
1427         outputLayerState.hwc = impl::OutputLayerCompositionState::Hwc(mHwcLayer);
1428 
1429         mLayerFEState.cursorFrame = kDefaultCursorFrame;
1430 
1431         mOutputState.layerStackSpace.setContent(kDefaultDisplayViewport);
1432         mOutputState.transform = ui::Transform{kDefaultTransform};
1433     }
1434 
1435     std::shared_ptr<HWC2::mock::Layer> mHwcLayer{std::make_shared<StrictMock<HWC2::mock::Layer>>()};
1436 };
1437 
1438 const Rect OutputLayerWriteCursorPositionToHWCTest::kDefaultDisplayViewport{0, 0, 1920, 1080};
1439 const Rect OutputLayerWriteCursorPositionToHWCTest::kDefaultCursorFrame{1, 2, 3, 4};
1440 
TEST_F(OutputLayerWriteCursorPositionToHWCTest,doesNothingIfNoFECompositionState)1441 TEST_F(OutputLayerWriteCursorPositionToHWCTest, doesNothingIfNoFECompositionState) {
1442     EXPECT_CALL(mLayerFE, getCompositionState()).WillOnce(Return(nullptr));
1443 
1444     mOutputLayer.writeCursorPositionToHWC();
1445 }
1446 
TEST_F(OutputLayerWriteCursorPositionToHWCTest,writeCursorPositionToHWCHandlesNoHwcState)1447 TEST_F(OutputLayerWriteCursorPositionToHWCTest, writeCursorPositionToHWCHandlesNoHwcState) {
1448     mOutputLayer.editState().hwc.reset();
1449 
1450     mOutputLayer.writeCursorPositionToHWC();
1451 }
1452 
TEST_F(OutputLayerWriteCursorPositionToHWCTest,writeCursorPositionToHWCWritesStateToHWC)1453 TEST_F(OutputLayerWriteCursorPositionToHWCTest, writeCursorPositionToHWCWritesStateToHWC) {
1454     EXPECT_CALL(*mHwcLayer, setCursorPosition(1, 2)).WillOnce(Return(kDefaultError));
1455 
1456     mOutputLayer.writeCursorPositionToHWC();
1457 }
1458 
TEST_F(OutputLayerWriteCursorPositionToHWCTest,writeCursorPositionToHWCIntersectedWithViewport)1459 TEST_F(OutputLayerWriteCursorPositionToHWCTest, writeCursorPositionToHWCIntersectedWithViewport) {
1460     mLayerFEState.cursorFrame = Rect{3000, 3000, 3016, 3016};
1461 
1462     EXPECT_CALL(*mHwcLayer, setCursorPosition(1920, 1080)).WillOnce(Return(kDefaultError));
1463 
1464     mOutputLayer.writeCursorPositionToHWC();
1465 }
1466 
TEST_F(OutputLayerWriteCursorPositionToHWCTest,writeCursorPositionToHWCRotatedByTransform)1467 TEST_F(OutputLayerWriteCursorPositionToHWCTest, writeCursorPositionToHWCRotatedByTransform) {
1468     mOutputState.transform = ui::Transform{TR_ROT_90};
1469 
1470     EXPECT_CALL(*mHwcLayer, setCursorPosition(-4, 1)).WillOnce(Return(kDefaultError));
1471 
1472     mOutputLayer.writeCursorPositionToHWC();
1473 }
1474 
1475 /*
1476  * OutputLayer::getHwcLayer()
1477  */
1478 
TEST_F(OutputLayerTest,getHwcLayerHandlesNoHwcState)1479 TEST_F(OutputLayerTest, getHwcLayerHandlesNoHwcState) {
1480     mOutputLayer.editState().hwc.reset();
1481 
1482     EXPECT_TRUE(mOutputLayer.getHwcLayer() == nullptr);
1483 }
1484 
TEST_F(OutputLayerTest,getHwcLayerHandlesNoHwcLayer)1485 TEST_F(OutputLayerTest, getHwcLayerHandlesNoHwcLayer) {
1486     mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
1487 
1488     EXPECT_TRUE(mOutputLayer.getHwcLayer() == nullptr);
1489 }
1490 
TEST_F(OutputLayerTest,getHwcLayerReturnsHwcLayer)1491 TEST_F(OutputLayerTest, getHwcLayerReturnsHwcLayer) {
1492     auto hwcLayer = std::make_shared<StrictMock<HWC2::mock::Layer>>();
1493     mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{hwcLayer};
1494 
1495     EXPECT_EQ(hwcLayer.get(), mOutputLayer.getHwcLayer());
1496 }
1497 
1498 /*
1499  * OutputLayer::requiresClientComposition()
1500  */
1501 
TEST_F(OutputLayerTest,requiresClientCompositionReturnsTrueIfNoHWC2State)1502 TEST_F(OutputLayerTest, requiresClientCompositionReturnsTrueIfNoHWC2State) {
1503     mOutputLayer.editState().hwc.reset();
1504 
1505     EXPECT_TRUE(mOutputLayer.requiresClientComposition());
1506 }
1507 
TEST_F(OutputLayerTest,requiresClientCompositionReturnsTrueIfSetToClientComposition)1508 TEST_F(OutputLayerTest, requiresClientCompositionReturnsTrueIfSetToClientComposition) {
1509     mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
1510     mOutputLayer.editState().hwc->hwcCompositionType = Composition::CLIENT;
1511 
1512     EXPECT_TRUE(mOutputLayer.requiresClientComposition());
1513 }
1514 
TEST_F(OutputLayerTest,requiresClientCompositionReturnsFalseIfSetToDeviceComposition)1515 TEST_F(OutputLayerTest, requiresClientCompositionReturnsFalseIfSetToDeviceComposition) {
1516     mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
1517     mOutputLayer.editState().hwc->hwcCompositionType = Composition::DEVICE;
1518 
1519     EXPECT_FALSE(mOutputLayer.requiresClientComposition());
1520 }
1521 
1522 /*
1523  * OutputLayer::isHardwareCursor()
1524  */
1525 
TEST_F(OutputLayerTest,isHardwareCursorReturnsFalseIfNoHWC2State)1526 TEST_F(OutputLayerTest, isHardwareCursorReturnsFalseIfNoHWC2State) {
1527     mOutputLayer.editState().hwc.reset();
1528 
1529     EXPECT_FALSE(mOutputLayer.isHardwareCursor());
1530 }
1531 
TEST_F(OutputLayerTest,isHardwareCursorReturnsTrueIfSetToCursorComposition)1532 TEST_F(OutputLayerTest, isHardwareCursorReturnsTrueIfSetToCursorComposition) {
1533     mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
1534     mOutputLayer.editState().hwc->hwcCompositionType = Composition::CURSOR;
1535 
1536     EXPECT_TRUE(mOutputLayer.isHardwareCursor());
1537 }
1538 
TEST_F(OutputLayerTest,isHardwareCursorReturnsFalseIfSetToDeviceComposition)1539 TEST_F(OutputLayerTest, isHardwareCursorReturnsFalseIfSetToDeviceComposition) {
1540     mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
1541     mOutputLayer.editState().hwc->hwcCompositionType = Composition::DEVICE;
1542 
1543     EXPECT_FALSE(mOutputLayer.isHardwareCursor());
1544 }
1545 
1546 /*
1547  * OutputLayer::applyDeviceCompositionTypeChange()
1548  */
1549 
TEST_F(OutputLayerTest,applyDeviceCompositionTypeChangeSetsNewType)1550 TEST_F(OutputLayerTest, applyDeviceCompositionTypeChangeSetsNewType) {
1551     mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
1552     mOutputLayer.editState().hwc->hwcCompositionType = Composition::DEVICE;
1553 
1554     mOutputLayer.applyDeviceCompositionTypeChange(Composition::CLIENT);
1555 
1556     ASSERT_TRUE(mOutputLayer.getState().hwc);
1557     EXPECT_EQ(Composition::CLIENT, mOutputLayer.getState().hwc->hwcCompositionType);
1558 }
1559 
1560 /*
1561  * OutputLayer::prepareForDeviceLayerRequests()
1562  */
1563 
TEST_F(OutputLayerTest,prepareForDeviceLayerRequestsResetsRequestState)1564 TEST_F(OutputLayerTest, prepareForDeviceLayerRequestsResetsRequestState) {
1565     mOutputLayer.editState().clearClientTarget = true;
1566 
1567     mOutputLayer.prepareForDeviceLayerRequests();
1568 
1569     EXPECT_FALSE(mOutputLayer.getState().clearClientTarget);
1570 }
1571 
1572 /*
1573  * OutputLayer::applyDeviceLayerRequest()
1574  */
1575 
TEST_F(OutputLayerTest,applyDeviceLayerRequestHandlesClearClientTarget)1576 TEST_F(OutputLayerTest, applyDeviceLayerRequestHandlesClearClientTarget) {
1577     mOutputLayer.editState().clearClientTarget = false;
1578 
1579     mOutputLayer.applyDeviceLayerRequest(Hwc2::IComposerClient::LayerRequest::CLEAR_CLIENT_TARGET);
1580 
1581     EXPECT_TRUE(mOutputLayer.getState().clearClientTarget);
1582 }
1583 
TEST_F(OutputLayerTest,applyDeviceLayerRequestHandlesUnknownRequest)1584 TEST_F(OutputLayerTest, applyDeviceLayerRequestHandlesUnknownRequest) {
1585     mOutputLayer.editState().clearClientTarget = false;
1586 
1587     mOutputLayer.applyDeviceLayerRequest(static_cast<Hwc2::IComposerClient::LayerRequest>(0));
1588 
1589     EXPECT_FALSE(mOutputLayer.getState().clearClientTarget);
1590 }
1591 
1592 /*
1593  * OutputLayer::needsFiltering()
1594  */
1595 
TEST_F(OutputLayerTest,needsFilteringReturnsFalseIfDisplaySizeSameAsSourceSize)1596 TEST_F(OutputLayerTest, needsFilteringReturnsFalseIfDisplaySizeSameAsSourceSize) {
1597     mOutputLayer.editState().displayFrame = Rect(100, 100, 200, 200);
1598     mOutputLayer.editState().sourceCrop = FloatRect{0.f, 0.f, 100.f, 100.f};
1599 
1600     EXPECT_FALSE(mOutputLayer.needsFiltering());
1601 }
1602 
TEST_F(OutputLayerTest,needsFilteringReturnsTrueIfDisplaySizeDifferentFromSourceSize)1603 TEST_F(OutputLayerTest, needsFilteringReturnsTrueIfDisplaySizeDifferentFromSourceSize) {
1604     mOutputLayer.editState().displayFrame = Rect(100, 100, 200, 200);
1605     mOutputLayer.editState().sourceCrop = FloatRect{0.f, 0.f, 100.1f, 100.1f};
1606 
1607     EXPECT_TRUE(mOutputLayer.needsFiltering());
1608 }
1609 
1610 } // namespace
1611 } // namespace android::compositionengine
1612