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::__anon834e7eb10111::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::__anon834e7eb10111::OutputLayerTest::OutputLayer92         const compositionengine::Output& getOutput() const override { return mOutput; }
getLayerFEandroid::compositionengine::__anon834e7eb10111::OutputLayerTest::OutputLayer93         compositionengine::LayerFE& getLayerFE() const override { return mLayerFE; }
getStateandroid::compositionengine::__anon834e7eb10111::OutputLayerTest::OutputLayer94         const impl::OutputLayerCompositionState& getState() const override { return mState; }
editStateandroid::compositionengine::__anon834e7eb10111::OutputLayerTest::OutputLayer95         impl::OutputLayerCompositionState& editState() override { return mState; }
96 
97         // compositionengine::impl::OutputLayer overrides
dumpStateandroid::compositionengine::__anon834e7eb10111::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::__anon834e7eb10111::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::__anon834e7eb10111::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::__anon834e7eb10111::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::__anon834e7eb10111::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::__anon834e7eb10111::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.shadowSettings.length = 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.shadowSettings.length = 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::__anon834e7eb10111::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::__anon834e7eb10111::OutputLayerPartialMockForUpdateCompositionState544     const compositionengine::Output& getOutput() const override { return mOutput; }
getLayerFEandroid::compositionengine::__anon834e7eb10111::OutputLayerPartialMockForUpdateCompositionState545     compositionengine::LayerFE& getLayerFE() const override { return mLayerFE; }
getStateandroid::compositionengine::__anon834e7eb10111::OutputLayerPartialMockForUpdateCompositionState546     const impl::OutputLayerCompositionState& getState() const override { return mState; }
editStateandroid::compositionengine::__anon834e7eb10111::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::__anon834e7eb10111::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::__anon834e7eb10111::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::__anon834e7eb10111::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.dataspace = 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     // If the output is HDR, then don't blind the user with a colorspace agnostic dataspace
664     // drawing all white
665     mOutputState.dataspace = ui::Dataspace::BT2020_PQ;
666 
667     mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
668 
669     EXPECT_EQ(ui::Dataspace::DISPLAY_P3, mOutputLayer.getState().dataspace);
670 }
671 
TEST_F(OutputLayerUpdateCompositionStateTest,setsOutputLayerColorspaceWith170mReplacement)672 TEST_F(OutputLayerUpdateCompositionStateTest, setsOutputLayerColorspaceWith170mReplacement) {
673     mLayerFEState.dataspace = ui::Dataspace::TRANSFER_SMPTE_170M;
674     mOutputState.treat170mAsSrgb = false;
675     mLayerFEState.isColorspaceAgnostic = false;
676 
677     mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
678 
679     EXPECT_EQ(ui::Dataspace::TRANSFER_SMPTE_170M, mOutputLayer.getState().dataspace);
680 
681     // Rewrite SMPTE 170M as sRGB
682     mOutputState.treat170mAsSrgb = true;
683     mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
684 
685     EXPECT_EQ(ui::Dataspace::TRANSFER_SRGB, mOutputLayer.getState().dataspace);
686 }
687 
TEST_F(OutputLayerUpdateCompositionStateTest,setsWhitePointNitsAndDimmingRatioCorrectly)688 TEST_F(OutputLayerUpdateCompositionStateTest, setsWhitePointNitsAndDimmingRatioCorrectly) {
689     mOutputState.sdrWhitePointNits = 200.f;
690     mOutputState.displayBrightnessNits = 800.f;
691 
692     mLayerFEState.dataspace = ui::Dataspace::DISPLAY_P3;
693     mLayerFEState.isColorspaceAgnostic = false;
694     mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
695     EXPECT_EQ(mOutputState.sdrWhitePointNits, mOutputLayer.getState().whitePointNits);
696     EXPECT_EQ(mOutputState.sdrWhitePointNits / mOutputState.displayBrightnessNits,
697               mOutputLayer.getState().dimmingRatio);
698 
699     mLayerFEState.dimmingEnabled = false;
700     mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
701     EXPECT_EQ(mOutputState.displayBrightnessNits, mOutputLayer.getState().whitePointNits);
702     EXPECT_EQ(1.f, mOutputLayer.getState().dimmingRatio);
703 
704     // change dimmingEnabled back to true.
705     mLayerFEState.dimmingEnabled = true;
706     mLayerFEState.dataspace = ui::Dataspace::BT2020_ITU_PQ;
707     mLayerFEState.isColorspaceAgnostic = false;
708     mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
709 
710     EXPECT_EQ(mOutputState.displayBrightnessNits, mOutputLayer.getState().whitePointNits);
711     EXPECT_EQ(1.f, mOutputLayer.getState().dimmingRatio);
712 }
713 
TEST_F(OutputLayerUpdateCompositionStateTest,doesNotRecomputeGeometryIfNotRequested)714 TEST_F(OutputLayerUpdateCompositionStateTest, doesNotRecomputeGeometryIfNotRequested) {
715     mOutputLayer.editState().forceClientComposition = false;
716 
717     mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
718 
719     EXPECT_EQ(false, mOutputLayer.getState().forceClientComposition);
720 }
721 
TEST_F(OutputLayerUpdateCompositionStateTest,doesNotClearForceClientCompositionIfNotDoingGeometry)722 TEST_F(OutputLayerUpdateCompositionStateTest,
723        doesNotClearForceClientCompositionIfNotDoingGeometry) {
724     mOutputLayer.editState().forceClientComposition = true;
725 
726     mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
727 
728     EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
729 }
730 
TEST_F(OutputLayerUpdateCompositionStateTest,clientCompositionForcedFromFrontEndFlagAtAnyTime)731 TEST_F(OutputLayerUpdateCompositionStateTest, clientCompositionForcedFromFrontEndFlagAtAnyTime) {
732     mLayerFEState.forceClientComposition = true;
733     mOutputLayer.editState().forceClientComposition = false;
734 
735     mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
736 
737     EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
738 }
739 
TEST_F(OutputLayerUpdateCompositionStateTest,clientCompositionForcedFromUnsupportedDataspaceAtAnyTime)740 TEST_F(OutputLayerUpdateCompositionStateTest,
741        clientCompositionForcedFromUnsupportedDataspaceAtAnyTime) {
742     mOutputLayer.editState().forceClientComposition = false;
743     EXPECT_CALL(mDisplayColorProfile, isDataspaceSupported(_)).WillRepeatedly(Return(false));
744 
745     mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
746 
747     EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
748 }
749 
TEST_F(OutputLayerUpdateCompositionStateTest,clientCompositionForcedFromArgumentFlag)750 TEST_F(OutputLayerUpdateCompositionStateTest, clientCompositionForcedFromArgumentFlag) {
751     mLayerFEState.forceClientComposition = false;
752     mOutputLayer.editState().forceClientComposition = false;
753 
754     mOutputLayer.updateCompositionState(false, true, ui::Transform::RotationFlags::ROT_0);
755 
756     EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
757 
758     mOutputLayer.editState().forceClientComposition = false;
759 
760     setupGeometryChildCallValues(ui::Transform::RotationFlags::ROT_0);
761 
762     mOutputLayer.updateCompositionState(true, true, ui::Transform::RotationFlags::ROT_0);
763 
764     EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
765 }
766 
767 /*
768  * OutputLayer::writeStateToHWC()
769  */
770 
771 struct OutputLayerWriteStateToHWCTest : public OutputLayerTest {
772     static constexpr hal::Error kError = hal::Error::UNSUPPORTED;
773     static constexpr FloatRect kSourceCrop{11.f, 12.f, 13.f, 14.f};
774     static constexpr Hwc2::Transform kBufferTransform = static_cast<Hwc2::Transform>(31);
775     static constexpr Hwc2::Transform kOverrideBufferTransform = static_cast<Hwc2::Transform>(0);
776     static constexpr Hwc2::IComposerClient::BlendMode kBlendMode =
777             static_cast<Hwc2::IComposerClient::BlendMode>(41);
778     static constexpr Hwc2::IComposerClient::BlendMode kOverrideBlendMode =
779             Hwc2::IComposerClient::BlendMode::PREMULTIPLIED;
780     static constexpr float kAlpha = 51.f;
781     static constexpr float kOverrideAlpha = 1.f;
782     static constexpr float kSkipAlpha = 0.f;
783     static constexpr ui::Dataspace kDataspace = static_cast<ui::Dataspace>(71);
784     static constexpr ui::Dataspace kOverrideDataspace = static_cast<ui::Dataspace>(72);
785     static constexpr int kSupportedPerFrameMetadata = 101;
786     static constexpr int kExpectedHwcSlot = 0;
787     static constexpr int kOverrideHwcSlot = impl::HwcBufferCache::kOverrideBufferSlot;
788     static constexpr bool kLayerGenericMetadata1Mandatory = true;
789     static constexpr bool kLayerGenericMetadata2Mandatory = true;
790     static constexpr float kWhitePointNits = 200.f;
791     static constexpr float kSdrWhitePointNits = 100.f;
792     static constexpr float kDisplayBrightnessNits = 400.f;
793     static constexpr float kLayerBrightness = kWhitePointNits / kDisplayBrightnessNits;
794     static constexpr float kOverrideLayerBrightness = kSdrWhitePointNits / kDisplayBrightnessNits;
795 
796     static const half4 kColor;
797     static const Rect kDisplayFrame;
798     static const Rect kOverrideDisplayFrame;
799     static const FloatRect kOverrideSourceCrop;
800     static const Region kOutputSpaceVisibleRegion;
801     static const Region kOverrideVisibleRegion;
802     static const mat4 kColorTransform;
803     static const Region kSurfaceDamage;
804     static const Region kOverrideSurfaceDamage;
805     static const HdrMetadata kHdrMetadata;
806     static native_handle_t* kSidebandStreamHandle;
807     static const sp<GraphicBuffer> kBuffer;
808     static const sp<GraphicBuffer> kOverrideBuffer;
809     static const sp<Fence> kFence;
810     static const sp<Fence> kOverrideFence;
811     static const std::string kLayerGenericMetadata1Key;
812     static const std::vector<uint8_t> kLayerGenericMetadata1Value;
813     static const std::string kLayerGenericMetadata2Key;
814     static const std::vector<uint8_t> kLayerGenericMetadata2Value;
815 
OutputLayerWriteStateToHWCTestandroid::compositionengine::__anon834e7eb10111::OutputLayerWriteStateToHWCTest816     OutputLayerWriteStateToHWCTest() {
817         auto& outputLayerState = mOutputLayer.editState();
818         outputLayerState.hwc = impl::OutputLayerCompositionState::Hwc(mHwcLayer);
819 
820         outputLayerState.displayFrame = kDisplayFrame;
821         outputLayerState.sourceCrop = kSourceCrop;
822         outputLayerState.bufferTransform = static_cast<Hwc2::Transform>(kBufferTransform);
823         outputLayerState.outputSpaceVisibleRegion = kOutputSpaceVisibleRegion;
824         outputLayerState.dataspace = kDataspace;
825         outputLayerState.whitePointNits = kWhitePointNits;
826         outputLayerState.dimmingRatio = kLayerBrightness;
827 
828         mLayerFEState.blendMode = kBlendMode;
829         mLayerFEState.alpha = kAlpha;
830         mLayerFEState.colorTransform = kColorTransform;
831         mLayerFEState.color = kColor;
832         mLayerFEState.surfaceDamage = kSurfaceDamage;
833         mLayerFEState.hdrMetadata = kHdrMetadata;
834         mLayerFEState.sidebandStream = NativeHandle::create(kSidebandStreamHandle, false);
835         mLayerFEState.buffer = kBuffer;
836         mLayerFEState.acquireFence = kFence;
837 
838         mOutputState.displayBrightnessNits = kDisplayBrightnessNits;
839         mOutputState.sdrWhitePointNits = kSdrWhitePointNits;
840 
841         EXPECT_CALL(mOutput, getDisplayColorProfile())
842                 .WillRepeatedly(Return(&mDisplayColorProfile));
843         EXPECT_CALL(mDisplayColorProfile, getSupportedPerFrameMetadata())
844                 .WillRepeatedly(Return(kSupportedPerFrameMetadata));
845     }
846     // Some tests may need to simulate unsupported HWC calls
847     enum class SimulateUnsupported { None, ColorTransform };
848 
includeGenericLayerMetadataInStateandroid::compositionengine::__anon834e7eb10111::OutputLayerWriteStateToHWCTest849     void includeGenericLayerMetadataInState() {
850         mLayerFEState.metadata[kLayerGenericMetadata1Key] = {kLayerGenericMetadata1Mandatory,
851                                                              kLayerGenericMetadata1Value};
852         mLayerFEState.metadata[kLayerGenericMetadata2Key] = {kLayerGenericMetadata2Mandatory,
853                                                              kLayerGenericMetadata2Value};
854     }
855 
includeOverrideInfoandroid::compositionengine::__anon834e7eb10111::OutputLayerWriteStateToHWCTest856     void includeOverrideInfo() {
857         auto& overrideInfo = mOutputLayer.editState().overrideInfo;
858 
859         overrideInfo.buffer = std::make_shared<
860                 renderengine::impl::ExternalTexture>(kOverrideBuffer, mRenderEngine,
861                                                      renderengine::impl::ExternalTexture::Usage::
862                                                                      READABLE |
863                                                              renderengine::impl::ExternalTexture::
864                                                                      Usage::WRITEABLE);
865         overrideInfo.acquireFence = kOverrideFence;
866         overrideInfo.displayFrame = kOverrideDisplayFrame;
867         overrideInfo.dataspace = kOverrideDataspace;
868         overrideInfo.damageRegion = kOverrideSurfaceDamage;
869         overrideInfo.visibleRegion = kOverrideVisibleRegion;
870     }
871 
expectGeometryCommonCallsandroid::compositionengine::__anon834e7eb10111::OutputLayerWriteStateToHWCTest872     void expectGeometryCommonCalls(Rect displayFrame = kDisplayFrame,
873                                    FloatRect sourceCrop = kSourceCrop,
874                                    Hwc2::Transform bufferTransform = kBufferTransform,
875                                    Hwc2::IComposerClient::BlendMode blendMode = kBlendMode,
876                                    float alpha = kAlpha) {
877         EXPECT_CALL(*mHwcLayer, setDisplayFrame(displayFrame)).WillOnce(Return(kError));
878         EXPECT_CALL(*mHwcLayer, setSourceCrop(sourceCrop)).WillOnce(Return(kError));
879         EXPECT_CALL(*mHwcLayer, setZOrder(_)).WillOnce(Return(kError));
880         EXPECT_CALL(*mHwcLayer, setTransform(bufferTransform)).WillOnce(Return(kError));
881 
882         EXPECT_CALL(*mHwcLayer, setBlendMode(blendMode)).WillOnce(Return(kError));
883         EXPECT_CALL(*mHwcLayer, setPlaneAlpha(alpha)).WillOnce(Return(kError));
884     }
885 
expectPerFrameCommonCallsandroid::compositionengine::__anon834e7eb10111::OutputLayerWriteStateToHWCTest886     void expectPerFrameCommonCalls(SimulateUnsupported unsupported = SimulateUnsupported::None,
887                                    ui::Dataspace dataspace = kDataspace,
888                                    const Region& visibleRegion = kOutputSpaceVisibleRegion,
889                                    const Region& surfaceDamage = kSurfaceDamage,
890                                    float brightness = kLayerBrightness,
891                                    const Region& blockingRegion = Region()) {
892         EXPECT_CALL(*mHwcLayer, setVisibleRegion(RegionEq(visibleRegion))).WillOnce(Return(kError));
893         EXPECT_CALL(*mHwcLayer, setDataspace(dataspace)).WillOnce(Return(kError));
894         EXPECT_CALL(*mHwcLayer, setBrightness(brightness)).WillOnce(Return(kError));
895         EXPECT_CALL(*mHwcLayer, setColorTransform(kColorTransform))
896                 .WillOnce(Return(unsupported == SimulateUnsupported::ColorTransform
897                                          ? hal::Error::UNSUPPORTED
898                                          : hal::Error::NONE));
899         EXPECT_CALL(*mHwcLayer, setSurfaceDamage(RegionEq(surfaceDamage))).WillOnce(Return(kError));
900         EXPECT_CALL(*mHwcLayer, setBlockingRegion(RegionEq(blockingRegion)))
901                 .WillOnce(Return(kError));
902     }
903 
expectSetCompositionTypeCallandroid::compositionengine::__anon834e7eb10111::OutputLayerWriteStateToHWCTest904     void expectSetCompositionTypeCall(Composition compositionType) {
905         EXPECT_CALL(*mHwcLayer, setCompositionType(compositionType)).WillOnce(Return(kError));
906     }
907 
expectNoSetCompositionTypeCallandroid::compositionengine::__anon834e7eb10111::OutputLayerWriteStateToHWCTest908     void expectNoSetCompositionTypeCall() {
909         EXPECT_CALL(*mHwcLayer, setCompositionType(_)).Times(0);
910     }
911 
expectSetColorCallandroid::compositionengine::__anon834e7eb10111::OutputLayerWriteStateToHWCTest912     void expectSetColorCall() {
913         const aidl::android::hardware::graphics::composer3::Color color = {kColor.r, kColor.g,
914                                                                            kColor.b, 1.0f};
915 
916         EXPECT_CALL(*mHwcLayer, setColor(ColorEq(color))).WillOnce(Return(kError));
917     }
918 
expectSetSidebandHandleCallandroid::compositionengine::__anon834e7eb10111::OutputLayerWriteStateToHWCTest919     void expectSetSidebandHandleCall() {
920         EXPECT_CALL(*mHwcLayer, setSidebandStream(kSidebandStreamHandle));
921     }
922 
expectSetHdrMetadataAndBufferCallsandroid::compositionengine::__anon834e7eb10111::OutputLayerWriteStateToHWCTest923     void expectSetHdrMetadataAndBufferCalls(uint32_t hwcSlot = kExpectedHwcSlot,
924                                             sp<GraphicBuffer> buffer = kBuffer,
925                                             sp<Fence> fence = kFence) {
926         EXPECT_CALL(*mHwcLayer, setPerFrameMetadata(kSupportedPerFrameMetadata, kHdrMetadata));
927         EXPECT_CALL(*mHwcLayer, setBuffer(hwcSlot, buffer, fence));
928     }
929 
expectGenericLayerMetadataCallsandroid::compositionengine::__anon834e7eb10111::OutputLayerWriteStateToHWCTest930     void expectGenericLayerMetadataCalls() {
931         // Note: Can be in any order.
932         EXPECT_CALL(*mHwcLayer,
933                     setLayerGenericMetadata(kLayerGenericMetadata1Key,
934                                             kLayerGenericMetadata1Mandatory,
935                                             kLayerGenericMetadata1Value));
936         EXPECT_CALL(*mHwcLayer,
937                     setLayerGenericMetadata(kLayerGenericMetadata2Key,
938                                             kLayerGenericMetadata2Mandatory,
939                                             kLayerGenericMetadata2Value));
940     }
941 
942     std::shared_ptr<HWC2::mock::Layer> mHwcLayer{std::make_shared<StrictMock<HWC2::mock::Layer>>()};
943     StrictMock<mock::DisplayColorProfile> mDisplayColorProfile;
944     renderengine::mock::RenderEngine mRenderEngine;
945 };
946 
947 const half4 OutputLayerWriteStateToHWCTest::kColor{81.f / 255.f, 82.f / 255.f, 83.f / 255.f,
948                                                    84.f / 255.f};
949 const Rect OutputLayerWriteStateToHWCTest::kDisplayFrame{1001, 1002, 1003, 10044};
950 const Rect OutputLayerWriteStateToHWCTest::kOverrideDisplayFrame{1002, 1003, 1004, 20044};
951 const FloatRect OutputLayerWriteStateToHWCTest::kOverrideSourceCrop{0.f, 0.f, 4.f, 5.f};
952 const Region OutputLayerWriteStateToHWCTest::kOutputSpaceVisibleRegion{
953         Rect{1005, 1006, 1007, 1008}};
954 const Region OutputLayerWriteStateToHWCTest::kOverrideVisibleRegion{Rect{1006, 1007, 1008, 1009}};
955 const mat4 OutputLayerWriteStateToHWCTest::kColorTransform{
956         1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016,
957         1017, 1018, 1019, 1020, 1021, 1022, 1023, 1024,
958 };
959 const Region OutputLayerWriteStateToHWCTest::kSurfaceDamage{Rect{1025, 1026, 1027, 1028}};
960 const Region OutputLayerWriteStateToHWCTest::kOverrideSurfaceDamage{Rect{1026, 1027, 1028, 1029}};
961 const HdrMetadata OutputLayerWriteStateToHWCTest::kHdrMetadata{{/* LightFlattenable */}, 1029};
962 native_handle_t* OutputLayerWriteStateToHWCTest::kSidebandStreamHandle =
963         reinterpret_cast<native_handle_t*>(1031);
964 const sp<GraphicBuffer> OutputLayerWriteStateToHWCTest::kBuffer =
965         sp<GraphicBuffer>::make(1, 2, PIXEL_FORMAT_RGBA_8888,
966                                 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN |
967                                         AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN);
968 const sp<GraphicBuffer> OutputLayerWriteStateToHWCTest::kOverrideBuffer =
969         sp<GraphicBuffer>::make(4, 5, PIXEL_FORMAT_RGBA_8888,
970                                 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN |
971                                         AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN);
972 const sp<Fence> OutputLayerWriteStateToHWCTest::kFence;
973 const sp<Fence> OutputLayerWriteStateToHWCTest::kOverrideFence = sp<Fence>::make();
974 const std::string OutputLayerWriteStateToHWCTest::kLayerGenericMetadata1Key =
975         "com.example.metadata.1";
976 const std::vector<uint8_t> OutputLayerWriteStateToHWCTest::kLayerGenericMetadata1Value{{1, 2, 3}};
977 const std::string OutputLayerWriteStateToHWCTest::kLayerGenericMetadata2Key =
978         "com.example.metadata.2";
979 const std::vector<uint8_t> OutputLayerWriteStateToHWCTest::kLayerGenericMetadata2Value{
980         {4, 5, 6, 7}};
981 
TEST_F(OutputLayerWriteStateToHWCTest,doesNothingIfNoFECompositionState)982 TEST_F(OutputLayerWriteStateToHWCTest, doesNothingIfNoFECompositionState) {
983     EXPECT_CALL(mLayerFE, getCompositionState()).WillOnce(Return(nullptr));
984 
985     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
986                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
987 }
988 
TEST_F(OutputLayerWriteStateToHWCTest,doesNothingIfNoHWCState)989 TEST_F(OutputLayerWriteStateToHWCTest, doesNothingIfNoHWCState) {
990     mOutputLayer.editState().hwc.reset();
991 
992     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
993                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
994 }
995 
TEST_F(OutputLayerWriteStateToHWCTest,doesNothingIfNoHWCLayer)996 TEST_F(OutputLayerWriteStateToHWCTest, doesNothingIfNoHWCLayer) {
997     mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc(nullptr);
998 
999     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1000                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1001 }
1002 
TEST_F(OutputLayerWriteStateToHWCTest,canSetAllState)1003 TEST_F(OutputLayerWriteStateToHWCTest, canSetAllState) {
1004     expectGeometryCommonCalls();
1005     expectPerFrameCommonCalls();
1006 
1007     expectNoSetCompositionTypeCall();
1008     EXPECT_CALL(mLayerFE, hasRoundedCorners()).WillOnce(Return(false));
1009 
1010     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1011                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1012 }
1013 
TEST_F(OutputLayerTest,displayInstallOrientationBufferTransformSetTo90)1014 TEST_F(OutputLayerTest, displayInstallOrientationBufferTransformSetTo90) {
1015     mLayerFEState.geomBufferUsesDisplayInverseTransform = false;
1016     mLayerFEState.geomLayerTransform = ui::Transform{TR_IDENT};
1017     // This test simulates a scenario where displayInstallOrientation is set to
1018     // ROT_90. This only has an effect on the transform; orientation stays 0 (see
1019     // DisplayDevice::setProjection).
1020     mOutputState.displaySpace.setOrientation(ui::ROTATION_0);
1021     mOutputState.transform = ui::Transform{TR_ROT_90};
1022     // Buffers are pre-rotated based on the transform hint (ROT_90); their
1023     // geomBufferTransform is set to the inverse transform.
1024     mLayerFEState.geomBufferTransform = TR_ROT_270;
1025 
1026     EXPECT_EQ(TR_IDENT, mOutputLayer.calculateOutputRelativeBufferTransform(ui::Transform::ROT_90));
1027 }
1028 
TEST_F(OutputLayerWriteStateToHWCTest,canSetPerFrameStateForSolidColor)1029 TEST_F(OutputLayerWriteStateToHWCTest, canSetPerFrameStateForSolidColor) {
1030     mLayerFEState.compositionType = Composition::SOLID_COLOR;
1031 
1032     expectPerFrameCommonCalls();
1033 
1034     // Setting the composition type should happen before setting the color. We
1035     // check this in this test only by setting up an testing::InSeqeuence
1036     // instance before setting up the two expectations.
1037     InSequence s;
1038     expectSetCompositionTypeCall(Composition::SOLID_COLOR);
1039     expectSetColorCall();
1040 
1041     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1042                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1043 }
1044 
TEST_F(OutputLayerWriteStateToHWCTest,canSetPerFrameStateForSideband)1045 TEST_F(OutputLayerWriteStateToHWCTest, canSetPerFrameStateForSideband) {
1046     mLayerFEState.compositionType = Composition::SIDEBAND;
1047 
1048     expectPerFrameCommonCalls();
1049     expectSetSidebandHandleCall();
1050     expectSetCompositionTypeCall(Composition::SIDEBAND);
1051 
1052     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1053                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1054 }
1055 
TEST_F(OutputLayerWriteStateToHWCTest,canSetPerFrameStateForCursor)1056 TEST_F(OutputLayerWriteStateToHWCTest, canSetPerFrameStateForCursor) {
1057     mLayerFEState.compositionType = Composition::CURSOR;
1058 
1059     expectPerFrameCommonCalls();
1060     expectSetHdrMetadataAndBufferCalls();
1061     expectSetCompositionTypeCall(Composition::CURSOR);
1062 
1063     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1064                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1065 }
1066 
TEST_F(OutputLayerWriteStateToHWCTest,canSetPerFrameStateForDevice)1067 TEST_F(OutputLayerWriteStateToHWCTest, canSetPerFrameStateForDevice) {
1068     mLayerFEState.compositionType = Composition::DEVICE;
1069 
1070     expectPerFrameCommonCalls();
1071     expectSetHdrMetadataAndBufferCalls();
1072     expectSetCompositionTypeCall(Composition::DEVICE);
1073 
1074     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1075                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1076 }
1077 
TEST_F(OutputLayerWriteStateToHWCTest,compositionTypeIsNotSetIfUnchanged)1078 TEST_F(OutputLayerWriteStateToHWCTest, compositionTypeIsNotSetIfUnchanged) {
1079     (*mOutputLayer.editState().hwc).hwcCompositionType = Composition::SOLID_COLOR;
1080 
1081     mLayerFEState.compositionType = Composition::SOLID_COLOR;
1082 
1083     expectPerFrameCommonCalls();
1084     expectSetColorCall();
1085     expectNoSetCompositionTypeCall();
1086 
1087     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1088                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1089 }
1090 
TEST_F(OutputLayerWriteStateToHWCTest,compositionTypeIsSetToClientIfColorTransformNotSupported)1091 TEST_F(OutputLayerWriteStateToHWCTest, compositionTypeIsSetToClientIfColorTransformNotSupported) {
1092     mLayerFEState.compositionType = Composition::SOLID_COLOR;
1093 
1094     expectPerFrameCommonCalls(SimulateUnsupported::ColorTransform);
1095     expectSetColorCall();
1096     expectSetCompositionTypeCall(Composition::CLIENT);
1097 
1098     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1099                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1100 }
1101 
TEST_F(OutputLayerWriteStateToHWCTest,compositionTypeIsSetToClientIfClientCompositionForced)1102 TEST_F(OutputLayerWriteStateToHWCTest, compositionTypeIsSetToClientIfClientCompositionForced) {
1103     mOutputLayer.editState().forceClientComposition = true;
1104 
1105     mLayerFEState.compositionType = Composition::SOLID_COLOR;
1106 
1107     expectPerFrameCommonCalls();
1108     expectSetColorCall();
1109     expectSetCompositionTypeCall(Composition::CLIENT);
1110 
1111     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1112                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1113 }
1114 
TEST_F(OutputLayerWriteStateToHWCTest,allStateIncludesMetadataIfPresent)1115 TEST_F(OutputLayerWriteStateToHWCTest, allStateIncludesMetadataIfPresent) {
1116     mLayerFEState.compositionType = Composition::DEVICE;
1117     includeGenericLayerMetadataInState();
1118 
1119     expectGeometryCommonCalls();
1120     expectPerFrameCommonCalls();
1121     expectSetHdrMetadataAndBufferCalls();
1122     expectGenericLayerMetadataCalls();
1123     expectSetCompositionTypeCall(Composition::DEVICE);
1124 
1125     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1126                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1127 }
1128 
TEST_F(OutputLayerWriteStateToHWCTest,perFrameStateDoesNotIncludeMetadataIfPresent)1129 TEST_F(OutputLayerWriteStateToHWCTest, perFrameStateDoesNotIncludeMetadataIfPresent) {
1130     mLayerFEState.compositionType = Composition::DEVICE;
1131     includeGenericLayerMetadataInState();
1132 
1133     expectPerFrameCommonCalls();
1134     expectSetHdrMetadataAndBufferCalls();
1135     expectSetCompositionTypeCall(Composition::DEVICE);
1136 
1137     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1138                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1139 }
1140 
TEST_F(OutputLayerWriteStateToHWCTest,overriddenSkipLayerDoesNotSendBuffer)1141 TEST_F(OutputLayerWriteStateToHWCTest, overriddenSkipLayerDoesNotSendBuffer) {
1142     mLayerFEState.compositionType = Composition::DEVICE;
1143     includeOverrideInfo();
1144 
1145     expectGeometryCommonCalls(kOverrideDisplayFrame, kOverrideSourceCrop, kOverrideBufferTransform,
1146                               kOverrideBlendMode, kSkipAlpha);
1147     expectPerFrameCommonCalls(SimulateUnsupported::None, kOverrideDataspace, kOverrideVisibleRegion,
1148                               kOverrideSurfaceDamage, kOverrideLayerBrightness);
1149     expectSetHdrMetadataAndBufferCalls();
1150     expectSetCompositionTypeCall(Composition::DEVICE);
1151 
1152     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ true, 0,
1153                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1154 }
1155 
TEST_F(OutputLayerWriteStateToHWCTest,overriddenSkipLayerForSolidColorDoesNotSendBuffer)1156 TEST_F(OutputLayerWriteStateToHWCTest, overriddenSkipLayerForSolidColorDoesNotSendBuffer) {
1157     mLayerFEState.compositionType = Composition::SOLID_COLOR;
1158     includeOverrideInfo();
1159 
1160     expectGeometryCommonCalls(kOverrideDisplayFrame, kOverrideSourceCrop, kOverrideBufferTransform,
1161                               kOverrideBlendMode, kSkipAlpha);
1162     expectPerFrameCommonCalls(SimulateUnsupported::None, kOverrideDataspace, kOverrideVisibleRegion,
1163                               kOverrideSurfaceDamage, kOverrideLayerBrightness);
1164     expectSetHdrMetadataAndBufferCalls();
1165     expectSetCompositionTypeCall(Composition::DEVICE);
1166 
1167     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ true, 0,
1168                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1169 }
1170 
TEST_F(OutputLayerWriteStateToHWCTest,includesOverrideInfoIfPresent)1171 TEST_F(OutputLayerWriteStateToHWCTest, includesOverrideInfoIfPresent) {
1172     mLayerFEState.compositionType = Composition::DEVICE;
1173     includeOverrideInfo();
1174 
1175     expectGeometryCommonCalls(kOverrideDisplayFrame, kOverrideSourceCrop, kOverrideBufferTransform,
1176                               kOverrideBlendMode, kOverrideAlpha);
1177     expectPerFrameCommonCalls(SimulateUnsupported::None, kOverrideDataspace, kOverrideVisibleRegion,
1178                               kOverrideSurfaceDamage, kOverrideLayerBrightness);
1179     expectSetHdrMetadataAndBufferCalls(kOverrideHwcSlot, kOverrideBuffer, kOverrideFence);
1180     expectSetCompositionTypeCall(Composition::DEVICE);
1181 
1182     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1183                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1184 }
1185 
TEST_F(OutputLayerWriteStateToHWCTest,includesOverrideInfoForSolidColorIfPresent)1186 TEST_F(OutputLayerWriteStateToHWCTest, includesOverrideInfoForSolidColorIfPresent) {
1187     mLayerFEState.compositionType = Composition::SOLID_COLOR;
1188     includeOverrideInfo();
1189 
1190     expectGeometryCommonCalls(kOverrideDisplayFrame, kOverrideSourceCrop, kOverrideBufferTransform,
1191                               kOverrideBlendMode, kOverrideAlpha);
1192     expectPerFrameCommonCalls(SimulateUnsupported::None, kOverrideDataspace, kOverrideVisibleRegion,
1193                               kOverrideSurfaceDamage, kOverrideLayerBrightness);
1194     expectSetHdrMetadataAndBufferCalls(kOverrideHwcSlot, kOverrideBuffer, kOverrideFence);
1195     expectSetCompositionTypeCall(Composition::DEVICE);
1196 
1197     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1198                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1199 }
1200 
TEST_F(OutputLayerWriteStateToHWCTest,previousOverriddenLayerSendsSurfaceDamage)1201 TEST_F(OutputLayerWriteStateToHWCTest, previousOverriddenLayerSendsSurfaceDamage) {
1202     mLayerFEState.compositionType = Composition::DEVICE;
1203     mOutputLayer.editState().hwc->stateOverridden = true;
1204 
1205     expectGeometryCommonCalls();
1206     expectPerFrameCommonCalls(SimulateUnsupported::None, kDataspace, kOutputSpaceVisibleRegion,
1207                               Region::INVALID_REGION);
1208     expectSetHdrMetadataAndBufferCalls();
1209     expectSetCompositionTypeCall(Composition::DEVICE);
1210 
1211     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1212                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1213 }
1214 
TEST_F(OutputLayerWriteStateToHWCTest,previousSkipLayerSendsUpdatedDeviceCompositionInfo)1215 TEST_F(OutputLayerWriteStateToHWCTest, previousSkipLayerSendsUpdatedDeviceCompositionInfo) {
1216     mLayerFEState.compositionType = Composition::DEVICE;
1217     mOutputLayer.editState().hwc->stateOverridden = true;
1218     mOutputLayer.editState().hwc->layerSkipped = true;
1219     mOutputLayer.editState().hwc->hwcCompositionType = Composition::DEVICE;
1220 
1221     expectGeometryCommonCalls();
1222     expectPerFrameCommonCalls(SimulateUnsupported::None, kDataspace, kOutputSpaceVisibleRegion,
1223                               Region::INVALID_REGION);
1224     expectSetHdrMetadataAndBufferCalls();
1225     expectSetCompositionTypeCall(Composition::DEVICE);
1226 
1227     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1228                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1229 }
1230 
TEST_F(OutputLayerWriteStateToHWCTest,previousSkipLayerSendsUpdatedClientCompositionInfo)1231 TEST_F(OutputLayerWriteStateToHWCTest, previousSkipLayerSendsUpdatedClientCompositionInfo) {
1232     mLayerFEState.compositionType = Composition::DEVICE;
1233     mOutputLayer.editState().forceClientComposition = true;
1234     mOutputLayer.editState().hwc->stateOverridden = true;
1235     mOutputLayer.editState().hwc->layerSkipped = true;
1236     mOutputLayer.editState().hwc->hwcCompositionType = Composition::CLIENT;
1237 
1238     expectGeometryCommonCalls();
1239     expectPerFrameCommonCalls(SimulateUnsupported::None, kDataspace, kOutputSpaceVisibleRegion,
1240                               Region::INVALID_REGION);
1241     expectSetHdrMetadataAndBufferCalls();
1242     expectSetCompositionTypeCall(Composition::CLIENT);
1243 
1244     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1245                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1246 }
1247 
TEST_F(OutputLayerWriteStateToHWCTest,peekThroughChangesBlendMode)1248 TEST_F(OutputLayerWriteStateToHWCTest, peekThroughChangesBlendMode) {
1249     auto peekThroughLayerFE = sp<NiceMock<compositionengine::mock::LayerFE>>::make();
1250     OutputLayer peekThroughLayer{mOutput, *peekThroughLayerFE};
1251 
1252     mOutputLayer.mState.overrideInfo.peekThroughLayer = &peekThroughLayer;
1253 
1254     expectGeometryCommonCalls(kDisplayFrame, kSourceCrop, kBufferTransform,
1255                               Hwc2::IComposerClient::BlendMode::PREMULTIPLIED);
1256     expectPerFrameCommonCalls();
1257 
1258     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1259                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1260 }
1261 
TEST_F(OutputLayerWriteStateToHWCTest,isPeekingThroughSetsOverride)1262 TEST_F(OutputLayerWriteStateToHWCTest, isPeekingThroughSetsOverride) {
1263     expectGeometryCommonCalls();
1264     expectPerFrameCommonCalls();
1265 
1266     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1267                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ true);
1268     EXPECT_TRUE(mOutputLayer.getState().hwc->stateOverridden);
1269 }
1270 
TEST_F(OutputLayerWriteStateToHWCTest,zIsOverriddenSetsOverride)1271 TEST_F(OutputLayerWriteStateToHWCTest, zIsOverriddenSetsOverride) {
1272     expectGeometryCommonCalls();
1273     expectPerFrameCommonCalls();
1274 
1275     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1276                                  /*zIsOverridden*/ true, /*isPeekingThrough*/
1277                                  false);
1278     EXPECT_TRUE(mOutputLayer.getState().hwc->stateOverridden);
1279 }
1280 
TEST_F(OutputLayerWriteStateToHWCTest,roundedCornersForceClientComposition)1281 TEST_F(OutputLayerWriteStateToHWCTest, roundedCornersForceClientComposition) {
1282     expectGeometryCommonCalls();
1283     expectPerFrameCommonCalls();
1284     EXPECT_CALL(mLayerFE, hasRoundedCorners()).WillOnce(Return(true));
1285     expectSetCompositionTypeCall(Composition::CLIENT);
1286 
1287     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1288                                  /*zIsOverridden*/ false, /*isPeekingThrough*/
1289                                  false);
1290 }
1291 
TEST_F(OutputLayerWriteStateToHWCTest,roundedCornersPeekingThroughAllowsDeviceComposition)1292 TEST_F(OutputLayerWriteStateToHWCTest, roundedCornersPeekingThroughAllowsDeviceComposition) {
1293     expectGeometryCommonCalls();
1294     expectPerFrameCommonCalls();
1295     expectSetHdrMetadataAndBufferCalls();
1296     EXPECT_CALL(mLayerFE, hasRoundedCorners()).WillRepeatedly(Return(true));
1297     expectSetCompositionTypeCall(Composition::DEVICE);
1298 
1299     mLayerFEState.compositionType = Composition::DEVICE;
1300     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1301                                  /*zIsOverridden*/ false, /*isPeekingThrough*/
1302                                  true);
1303     EXPECT_EQ(Composition::DEVICE, mOutputLayer.getState().hwc->hwcCompositionType);
1304 }
1305 
TEST_F(OutputLayerWriteStateToHWCTest,setBlockingRegion)1306 TEST_F(OutputLayerWriteStateToHWCTest, setBlockingRegion) {
1307     mLayerFEState.compositionType = Composition::DISPLAY_DECORATION;
1308     const auto blockingRegion = Region(Rect(0, 0, 1000, 1000));
1309     mOutputLayer.editState().outputSpaceBlockingRegionHint = blockingRegion;
1310 
1311     expectGeometryCommonCalls();
1312     expectPerFrameCommonCalls(SimulateUnsupported::None, kDataspace, kOutputSpaceVisibleRegion,
1313                               kSurfaceDamage, kLayerBrightness, blockingRegion);
1314     expectSetHdrMetadataAndBufferCalls();
1315     expectSetCompositionTypeCall(Composition::DISPLAY_DECORATION);
1316 
1317     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1318                                  /*zIsOverridden*/ false, /*isPeekingThrough*/
1319                                  false);
1320 }
1321 
TEST_F(OutputLayerWriteStateToHWCTest,setCompositionTypeRefreshRateIndicator)1322 TEST_F(OutputLayerWriteStateToHWCTest, setCompositionTypeRefreshRateIndicator) {
1323     mLayerFEState.compositionType = Composition::REFRESH_RATE_INDICATOR;
1324 
1325     expectGeometryCommonCalls();
1326     expectPerFrameCommonCalls();
1327     expectSetHdrMetadataAndBufferCalls();
1328     expectSetCompositionTypeCall(Composition::REFRESH_RATE_INDICATOR);
1329 
1330     mOutputLayer.writeStateToHWC(/*includeGeometry*/ true, /*skipLayer*/ false, 0,
1331                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1332 }
1333 
1334 /*
1335  * OutputLayer::uncacheBuffers
1336  */
1337 struct OutputLayerUncacheBufferTest : public OutputLayerTest {
1338     static const sp<GraphicBuffer> kBuffer1;
1339     static const sp<GraphicBuffer> kBuffer2;
1340     static const sp<GraphicBuffer> kBuffer3;
1341     static const sp<Fence> kFence;
1342 
OutputLayerUncacheBufferTestandroid::compositionengine::__anon834e7eb10111::OutputLayerUncacheBufferTest1343     OutputLayerUncacheBufferTest() {
1344         auto& outputLayerState = mOutputLayer.editState();
1345         outputLayerState.hwc = impl::OutputLayerCompositionState::Hwc(mHwcLayer_);
1346 
1347         mLayerFEState.compositionType = Composition::DEVICE;
1348         mLayerFEState.acquireFence = kFence;
1349 
1350         ON_CALL(mOutput, getDisplayColorProfile()).WillByDefault(Return(&mDisplayColorProfile));
1351     }
1352 
1353     std::shared_ptr<HWC2::mock::Layer> mHwcLayer_{std::make_shared<NiceMock<HWC2::mock::Layer>>()};
1354     HWC2::mock::Layer& mHwcLayer = *mHwcLayer_;
1355     NiceMock<mock::DisplayColorProfile> mDisplayColorProfile;
1356 };
1357 
1358 const sp<GraphicBuffer> OutputLayerUncacheBufferTest::kBuffer1 =
1359         sp<GraphicBuffer>::make(1, 2, PIXEL_FORMAT_RGBA_8888,
1360                                 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN |
1361                                         AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN);
1362 const sp<GraphicBuffer> OutputLayerUncacheBufferTest::kBuffer2 =
1363         sp<GraphicBuffer>::make(2, 3, PIXEL_FORMAT_RGBA_8888,
1364                                 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN |
1365                                         AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN);
1366 const sp<GraphicBuffer> OutputLayerUncacheBufferTest::kBuffer3 =
1367         sp<GraphicBuffer>::make(4, 5, PIXEL_FORMAT_RGBA_8888,
1368                                 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN |
1369                                         AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN);
1370 const sp<Fence> OutputLayerUncacheBufferTest::kFence = sp<Fence>::make();
1371 
TEST_F(OutputLayerUncacheBufferTest,canUncacheAndReuseSlot)1372 TEST_F(OutputLayerUncacheBufferTest, canUncacheAndReuseSlot) {
1373     // Buffer1 is stored in slot 0
1374     mLayerFEState.buffer = kBuffer1;
1375     EXPECT_CALL(mHwcLayer, setBuffer(/*slot*/ 0, kBuffer1, kFence));
1376     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1377                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1378     Mock::VerifyAndClearExpectations(&mHwcLayer);
1379 
1380     // Buffer2 is stored in slot 1
1381     mLayerFEState.buffer = kBuffer2;
1382     EXPECT_CALL(mHwcLayer, setBuffer(/*slot*/ 1, kBuffer2, kFence));
1383     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1384                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1385     Mock::VerifyAndClearExpectations(&mHwcLayer);
1386 
1387     // Buffer3 is stored in slot 2
1388     mLayerFEState.buffer = kBuffer3;
1389     EXPECT_CALL(mHwcLayer, setBuffer(/*slot*/ 2, kBuffer3, kFence));
1390     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1391                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1392     Mock::VerifyAndClearExpectations(&mHwcLayer);
1393 
1394     // Buffer2 becomes the active buffer again (with a nullptr) and reuses slot 1
1395     mLayerFEState.buffer = kBuffer2;
1396     sp<GraphicBuffer> nullBuffer = nullptr;
1397     EXPECT_CALL(mHwcLayer, setBuffer(/*slot*/ 1, nullBuffer, kFence));
1398     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1399                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1400     Mock::VerifyAndClearExpectations(&mHwcLayer);
1401 
1402     // Buffer slots are cleared
1403     std::vector<uint32_t> slotsToClear = {0, 2, 1}; // order doesn't matter
1404     EXPECT_CALL(mHwcLayer, setBufferSlotsToClear(slotsToClear, /*activeBufferSlot*/ 1));
1405     // Uncache the active buffer in between other buffers to exercise correct algorithmic behavior.
1406     mOutputLayer.uncacheBuffers({kBuffer1->getId(), kBuffer2->getId(), kBuffer3->getId()});
1407     Mock::VerifyAndClearExpectations(&mHwcLayer);
1408 
1409     // Buffer1 becomes active again, and rather than allocating a new slot, or re-using slot 0,
1410     // the active buffer slot (slot 1 for Buffer2) is reused first, which allows HWC to free the
1411     // memory for the active buffer. Note: slot 1 is different from the first and last buffer slot
1412     // requested to be cleared in slotsToClear (slot 1), above, indicating that the algorithm
1413     // correctly identifies the active buffer as the buffer in slot 1, despite ping-ponging.
1414     mLayerFEState.buffer = kBuffer1;
1415     EXPECT_CALL(mHwcLayer, setBuffer(/*slot*/ 1, kBuffer1, kFence));
1416     mOutputLayer.writeStateToHWC(/*includeGeometry*/ false, /*skipLayer*/ false, 0,
1417                                  /*zIsOverridden*/ false, /*isPeekingThrough*/ false);
1418     Mock::VerifyAndClearExpectations(&mHwcLayer);
1419 }
1420 
1421 /*
1422  * OutputLayer::writeCursorPositionToHWC()
1423  */
1424 
1425 struct OutputLayerWriteCursorPositionToHWCTest : public OutputLayerTest {
1426     static constexpr int kDefaultTransform = TR_IDENT;
1427     static constexpr hal::Error kDefaultError = hal::Error::UNSUPPORTED;
1428 
1429     static const Rect kDefaultDisplayViewport;
1430     static const Rect kDefaultCursorFrame;
1431 
OutputLayerWriteCursorPositionToHWCTestandroid::compositionengine::__anon834e7eb10111::OutputLayerWriteCursorPositionToHWCTest1432     OutputLayerWriteCursorPositionToHWCTest() {
1433         auto& outputLayerState = mOutputLayer.editState();
1434         outputLayerState.hwc = impl::OutputLayerCompositionState::Hwc(mHwcLayer);
1435 
1436         mLayerFEState.cursorFrame = kDefaultCursorFrame;
1437 
1438         mOutputState.layerStackSpace.setContent(kDefaultDisplayViewport);
1439         mOutputState.transform = ui::Transform{kDefaultTransform};
1440     }
1441 
1442     std::shared_ptr<HWC2::mock::Layer> mHwcLayer{std::make_shared<StrictMock<HWC2::mock::Layer>>()};
1443 };
1444 
1445 const Rect OutputLayerWriteCursorPositionToHWCTest::kDefaultDisplayViewport{0, 0, 1920, 1080};
1446 const Rect OutputLayerWriteCursorPositionToHWCTest::kDefaultCursorFrame{1, 2, 3, 4};
1447 
TEST_F(OutputLayerWriteCursorPositionToHWCTest,doesNothingIfNoFECompositionState)1448 TEST_F(OutputLayerWriteCursorPositionToHWCTest, doesNothingIfNoFECompositionState) {
1449     EXPECT_CALL(mLayerFE, getCompositionState()).WillOnce(Return(nullptr));
1450 
1451     mOutputLayer.writeCursorPositionToHWC();
1452 }
1453 
TEST_F(OutputLayerWriteCursorPositionToHWCTest,writeCursorPositionToHWCHandlesNoHwcState)1454 TEST_F(OutputLayerWriteCursorPositionToHWCTest, writeCursorPositionToHWCHandlesNoHwcState) {
1455     mOutputLayer.editState().hwc.reset();
1456 
1457     mOutputLayer.writeCursorPositionToHWC();
1458 }
1459 
TEST_F(OutputLayerWriteCursorPositionToHWCTest,writeCursorPositionToHWCWritesStateToHWC)1460 TEST_F(OutputLayerWriteCursorPositionToHWCTest, writeCursorPositionToHWCWritesStateToHWC) {
1461     EXPECT_CALL(*mHwcLayer, setCursorPosition(1, 2)).WillOnce(Return(kDefaultError));
1462 
1463     mOutputLayer.writeCursorPositionToHWC();
1464 }
1465 
TEST_F(OutputLayerWriteCursorPositionToHWCTest,writeCursorPositionToHWCIntersectedWithViewport)1466 TEST_F(OutputLayerWriteCursorPositionToHWCTest, writeCursorPositionToHWCIntersectedWithViewport) {
1467     mLayerFEState.cursorFrame = Rect{3000, 3000, 3016, 3016};
1468 
1469     EXPECT_CALL(*mHwcLayer, setCursorPosition(1920, 1080)).WillOnce(Return(kDefaultError));
1470 
1471     mOutputLayer.writeCursorPositionToHWC();
1472 }
1473 
TEST_F(OutputLayerWriteCursorPositionToHWCTest,writeCursorPositionToHWCRotatedByTransform)1474 TEST_F(OutputLayerWriteCursorPositionToHWCTest, writeCursorPositionToHWCRotatedByTransform) {
1475     mOutputState.transform = ui::Transform{TR_ROT_90};
1476 
1477     EXPECT_CALL(*mHwcLayer, setCursorPosition(-4, 1)).WillOnce(Return(kDefaultError));
1478 
1479     mOutputLayer.writeCursorPositionToHWC();
1480 }
1481 
1482 /*
1483  * OutputLayer::getHwcLayer()
1484  */
1485 
TEST_F(OutputLayerTest,getHwcLayerHandlesNoHwcState)1486 TEST_F(OutputLayerTest, getHwcLayerHandlesNoHwcState) {
1487     mOutputLayer.editState().hwc.reset();
1488 
1489     EXPECT_TRUE(mOutputLayer.getHwcLayer() == nullptr);
1490 }
1491 
TEST_F(OutputLayerTest,getHwcLayerHandlesNoHwcLayer)1492 TEST_F(OutputLayerTest, getHwcLayerHandlesNoHwcLayer) {
1493     mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
1494 
1495     EXPECT_TRUE(mOutputLayer.getHwcLayer() == nullptr);
1496 }
1497 
TEST_F(OutputLayerTest,getHwcLayerReturnsHwcLayer)1498 TEST_F(OutputLayerTest, getHwcLayerReturnsHwcLayer) {
1499     auto hwcLayer = std::make_shared<StrictMock<HWC2::mock::Layer>>();
1500     mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{hwcLayer};
1501 
1502     EXPECT_EQ(hwcLayer.get(), mOutputLayer.getHwcLayer());
1503 }
1504 
1505 /*
1506  * OutputLayer::requiresClientComposition()
1507  */
1508 
TEST_F(OutputLayerTest,requiresClientCompositionReturnsTrueIfNoHWC2State)1509 TEST_F(OutputLayerTest, requiresClientCompositionReturnsTrueIfNoHWC2State) {
1510     mOutputLayer.editState().hwc.reset();
1511 
1512     EXPECT_TRUE(mOutputLayer.requiresClientComposition());
1513 }
1514 
TEST_F(OutputLayerTest,requiresClientCompositionReturnsTrueIfSetToClientComposition)1515 TEST_F(OutputLayerTest, requiresClientCompositionReturnsTrueIfSetToClientComposition) {
1516     mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
1517     mOutputLayer.editState().hwc->hwcCompositionType = Composition::CLIENT;
1518 
1519     EXPECT_TRUE(mOutputLayer.requiresClientComposition());
1520 }
1521 
TEST_F(OutputLayerTest,requiresClientCompositionReturnsFalseIfSetToDeviceComposition)1522 TEST_F(OutputLayerTest, requiresClientCompositionReturnsFalseIfSetToDeviceComposition) {
1523     mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
1524     mOutputLayer.editState().hwc->hwcCompositionType = Composition::DEVICE;
1525 
1526     EXPECT_FALSE(mOutputLayer.requiresClientComposition());
1527 }
1528 
1529 /*
1530  * OutputLayer::isHardwareCursor()
1531  */
1532 
TEST_F(OutputLayerTest,isHardwareCursorReturnsFalseIfNoHWC2State)1533 TEST_F(OutputLayerTest, isHardwareCursorReturnsFalseIfNoHWC2State) {
1534     mOutputLayer.editState().hwc.reset();
1535 
1536     EXPECT_FALSE(mOutputLayer.isHardwareCursor());
1537 }
1538 
TEST_F(OutputLayerTest,isHardwareCursorReturnsTrueIfSetToCursorComposition)1539 TEST_F(OutputLayerTest, isHardwareCursorReturnsTrueIfSetToCursorComposition) {
1540     mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
1541     mOutputLayer.editState().hwc->hwcCompositionType = Composition::CURSOR;
1542 
1543     EXPECT_TRUE(mOutputLayer.isHardwareCursor());
1544 }
1545 
TEST_F(OutputLayerTest,isHardwareCursorReturnsFalseIfSetToDeviceComposition)1546 TEST_F(OutputLayerTest, isHardwareCursorReturnsFalseIfSetToDeviceComposition) {
1547     mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
1548     mOutputLayer.editState().hwc->hwcCompositionType = Composition::DEVICE;
1549 
1550     EXPECT_FALSE(mOutputLayer.isHardwareCursor());
1551 }
1552 
1553 /*
1554  * OutputLayer::applyDeviceCompositionTypeChange()
1555  */
1556 
TEST_F(OutputLayerTest,applyDeviceCompositionTypeChangeSetsNewType)1557 TEST_F(OutputLayerTest, applyDeviceCompositionTypeChangeSetsNewType) {
1558     mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
1559     mOutputLayer.editState().hwc->hwcCompositionType = Composition::DEVICE;
1560 
1561     mOutputLayer.applyDeviceCompositionTypeChange(Composition::CLIENT);
1562 
1563     ASSERT_TRUE(mOutputLayer.getState().hwc);
1564     EXPECT_EQ(Composition::CLIENT, mOutputLayer.getState().hwc->hwcCompositionType);
1565 }
1566 
1567 /*
1568  * OutputLayer::prepareForDeviceLayerRequests()
1569  */
1570 
TEST_F(OutputLayerTest,prepareForDeviceLayerRequestsResetsRequestState)1571 TEST_F(OutputLayerTest, prepareForDeviceLayerRequestsResetsRequestState) {
1572     mOutputLayer.editState().clearClientTarget = true;
1573 
1574     mOutputLayer.prepareForDeviceLayerRequests();
1575 
1576     EXPECT_FALSE(mOutputLayer.getState().clearClientTarget);
1577 }
1578 
1579 /*
1580  * OutputLayer::applyDeviceLayerRequest()
1581  */
1582 
TEST_F(OutputLayerTest,applyDeviceLayerRequestHandlesClearClientTarget)1583 TEST_F(OutputLayerTest, applyDeviceLayerRequestHandlesClearClientTarget) {
1584     mOutputLayer.editState().clearClientTarget = false;
1585 
1586     mOutputLayer.applyDeviceLayerRequest(Hwc2::IComposerClient::LayerRequest::CLEAR_CLIENT_TARGET);
1587 
1588     EXPECT_TRUE(mOutputLayer.getState().clearClientTarget);
1589 }
1590 
TEST_F(OutputLayerTest,applyDeviceLayerRequestHandlesUnknownRequest)1591 TEST_F(OutputLayerTest, applyDeviceLayerRequestHandlesUnknownRequest) {
1592     mOutputLayer.editState().clearClientTarget = false;
1593 
1594     mOutputLayer.applyDeviceLayerRequest(static_cast<Hwc2::IComposerClient::LayerRequest>(0));
1595 
1596     EXPECT_FALSE(mOutputLayer.getState().clearClientTarget);
1597 }
1598 
1599 /*
1600  * OutputLayer::needsFiltering()
1601  */
1602 
TEST_F(OutputLayerTest,needsFilteringReturnsFalseIfDisplaySizeSameAsSourceSize)1603 TEST_F(OutputLayerTest, needsFilteringReturnsFalseIfDisplaySizeSameAsSourceSize) {
1604     mOutputLayer.editState().displayFrame = Rect(100, 100, 200, 200);
1605     mOutputLayer.editState().sourceCrop = FloatRect{0.f, 0.f, 100.f, 100.f};
1606 
1607     EXPECT_FALSE(mOutputLayer.needsFiltering());
1608 }
1609 
TEST_F(OutputLayerTest,needsFilteringReturnsTrueIfDisplaySizeDifferentFromSourceSize)1610 TEST_F(OutputLayerTest, needsFilteringReturnsTrueIfDisplaySizeDifferentFromSourceSize) {
1611     mOutputLayer.editState().displayFrame = Rect(100, 100, 200, 200);
1612     mOutputLayer.editState().sourceCrop = FloatRect{0.f, 0.f, 100.1f, 100.1f};
1613 
1614     EXPECT_TRUE(mOutputLayer.needsFiltering());
1615 }
1616 
TEST_F(OutputLayerTest,needsFilteringReturnsFalseIfRotatedDisplaySizeSameAsSourceSize)1617 TEST_F(OutputLayerTest, needsFilteringReturnsFalseIfRotatedDisplaySizeSameAsSourceSize) {
1618     mOutputLayer.editState().displayFrame = Rect(100, 100, 300, 200);
1619     mOutputLayer.editState().sourceCrop = FloatRect{0.f, 0.f, 100.f, 200.f};
1620     mOutputLayer.editState().bufferTransform = Hwc2::Transform::ROT_90;
1621 
1622     EXPECT_FALSE(mOutputLayer.needsFiltering());
1623 }
1624 
TEST_F(OutputLayerTest,needsFilteringReturnsTrueIfRotatedDisplaySizeDiffersFromSourceSize)1625 TEST_F(OutputLayerTest, needsFilteringReturnsTrueIfRotatedDisplaySizeDiffersFromSourceSize) {
1626     mOutputLayer.editState().displayFrame = Rect(100, 100, 300, 200);
1627     mOutputLayer.editState().sourceCrop = FloatRect{0.f, 0.f, 100.f, 200.f};
1628 
1629     EXPECT_TRUE(mOutputLayer.needsFiltering());
1630 }
1631 
1632 } // namespace
1633 } // namespace android::compositionengine
1634