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