1 /*
2  * Copyright 2021 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 #undef LOG_TAG
18 #define LOG_TAG "LayerStateTest"
19 
20 #include <aidl/android/hardware/graphics/common/BufferUsage.h>
21 #include <common/include/common/test/FlagUtils.h>
22 #include <compositionengine/impl/OutputLayer.h>
23 #include <compositionengine/impl/planner/LayerState.h>
24 #include <compositionengine/mock/LayerFE.h>
25 #include <compositionengine/mock/OutputLayer.h>
26 #include <gtest/gtest.h>
27 #include <log/log.h>
28 
29 #include "android/hardware_buffer.h"
30 #include "com_android_graphics_surfaceflinger_flags.h"
31 #include "compositionengine/LayerFECompositionState.h"
32 
33 #include <aidl/android/hardware/graphics/composer3/Composition.h>
34 
35 using ::aidl::android::hardware::graphics::common::BufferUsage;
36 using ::aidl::android::hardware::graphics::composer3::Composition;
37 
38 namespace android::compositionengine::impl::planner {
39 namespace {
40 
41 using testing::Return;
42 using testing::ReturnRef;
43 
44 const std::string sDebugName = std::string("Test LayerFE");
45 const std::string sDebugNameTwo = std::string("Test LayerFE2");
46 const constexpr int32_t sSequenceId = 12345;
47 const constexpr int32_t sSequenceIdTwo = 123456;
48 const Rect sRectOne = Rect(10, 20, 30, 40);
49 const Rect sRectTwo = Rect(40, 30, 20, 10);
50 const FloatRect sFloatRectOne = FloatRect(100.f, 200.f, 300.f, 400.f);
51 const FloatRect sFloatRectTwo = FloatRect(400.f, 300.f, 200.f, 100.f);
52 const constexpr float sAlphaOne = 0.25f;
53 const constexpr float sAlphaTwo = 0.5f;
54 const Region sRegionOne = Region(sRectOne);
55 const Region sRegionTwo = Region(sRectTwo);
56 const mat4 sMat4One = mat4::scale(vec4(2.f, 3.f, 1.f, 1.f));
57 native_handle_t* const sFakeSidebandStreamOne = reinterpret_cast<native_handle_t*>(10);
58 native_handle_t* const sFakeSidebandStreamTwo = reinterpret_cast<native_handle_t*>(11);
59 const half4 sHalf4One = half4(0.2f, 0.3f, 0.4f, 0.5f);
60 const half4 sHalf4Two = half4(0.5f, 0.4f, 0.3f, 0.2f);
61 const std::string sMetadataKeyOne = std::string("Meta!");
62 const std::string sMetadataKeyTwo = std::string("Data!");
63 const GenericLayerMetadataEntry sMetadataValueOne = GenericLayerMetadataEntry{
64         .value = std::vector<uint8_t>({1, 2}),
65 };
66 const GenericLayerMetadataEntry sMetadataValueTwo = GenericLayerMetadataEntry{
67         .value = std::vector<uint8_t>({1, 3}),
68 };
69 const constexpr int32_t sBgBlurRadiusOne = 3;
70 const constexpr int32_t sBgBlurRadiusTwo = 4;
71 const BlurRegion sBlurRegionOne = BlurRegion{1, 2.f, 3.f, 4.f, 5.f, 6.f, 7, 8, 9, 10};
72 const BlurRegion sBlurRegionTwo = BlurRegion{2, 3.f, 4.f, 5.f, 6.f, 7.f, 8, 9, 10, 11};
73 
74 struct LayerStateTest : public testing::Test {
LayerStateTestandroid::compositionengine::impl::planner::__anon4a999f600111::LayerStateTest75     LayerStateTest() {
76         const ::testing::TestInfo* const test_info =
77                 ::testing::UnitTest::GetInstance()->current_test_info();
78         ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
79     }
80 
~LayerStateTestandroid::compositionengine::impl::planner::__anon4a999f600111::LayerStateTest81     ~LayerStateTest() {
82         const ::testing::TestInfo* const test_info =
83                 ::testing::UnitTest::GetInstance()->current_test_info();
84         ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
85     }
86 
setupMocksForLayerandroid::compositionengine::impl::planner::__anon4a999f600111::LayerStateTest87     void setupMocksForLayer(mock::OutputLayer& layer, mock::LayerFE& layerFE,
88                             const OutputLayerCompositionState& outputLayerState,
89                             const LayerFECompositionState& layerFEState,
90                             int32_t sequenceId = sSequenceId,
91                             const std::string& debugName = sDebugName) {
92         EXPECT_CALL(layer, getLayerFE()).WillRepeatedly(ReturnRef(layerFE));
93         EXPECT_CALL(layer, getState()).WillRepeatedly(ReturnRef(outputLayerState));
94         EXPECT_CALL(layerFE, getSequence()).WillRepeatedly(Return(sequenceId));
95         EXPECT_CALL(layerFE, getDebugName()).WillRepeatedly(Return(debugName.c_str()));
96         EXPECT_CALL(layerFE, getCompositionState()).WillRepeatedly(Return(&layerFEState));
97     }
98 
verifyUniqueDifferingFieldsandroid::compositionengine::impl::planner::__anon4a999f600111::LayerStateTest99     void verifyUniqueDifferingFields(const LayerState& lhs, const LayerState& rhs) {
100         EXPECT_EQ(lhs.getHash(), rhs.getHash());
101 
102         EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::None), lhs.getDifferingFields(rhs));
103         EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::None), rhs.getDifferingFields(lhs));
104     }
105 
verifyNonUniqueDifferingFieldsandroid::compositionengine::impl::planner::__anon4a999f600111::LayerStateTest106     void verifyNonUniqueDifferingFields(const LayerState& lhs, const LayerState& rhs,
107                                         ftl::Flags<LayerStateField> fields) {
108         EXPECT_NE(lhs.getHash(), rhs.getHash());
109 
110         EXPECT_EQ(fields, lhs.getDifferingFields(rhs));
111         EXPECT_EQ(fields, rhs.getDifferingFields(lhs));
112     }
113 
114     sp<mock::LayerFE> mLayerFE = sp<mock::LayerFE>::make();
115     mock::OutputLayer mOutputLayer;
116     std::unique_ptr<LayerState> mLayerState;
117 
118     static constexpr auto kUsageFlags = static_cast<uint32_t>(
119             AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN);
120 };
121 
TEST_F(LayerStateTest,getOutputLayer)122 TEST_F(LayerStateTest, getOutputLayer) {
123     OutputLayerCompositionState outputLayerCompositionState;
124     LayerFECompositionState layerFECompositionState;
125     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
126                        layerFECompositionState);
127     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
128     EXPECT_EQ(&mOutputLayer, mLayerState->getOutputLayer());
129 }
130 
TEST_F(LayerStateTest,updateOutputLayer)131 TEST_F(LayerStateTest, updateOutputLayer) {
132     OutputLayerCompositionState outputLayerCompositionState;
133     LayerFECompositionState layerFECompositionState;
134     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
135                        layerFECompositionState);
136     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
137     EXPECT_EQ(&mOutputLayer, mLayerState->getOutputLayer());
138 
139     mock::OutputLayer newOutputLayer;
140     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
141     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
142                        layerFECompositionState);
143     mLayerState->update(&newOutputLayer);
144     EXPECT_EQ(&newOutputLayer, mLayerState->getOutputLayer());
145 }
146 
TEST_F(LayerStateTest,getId)147 TEST_F(LayerStateTest, getId) {
148     OutputLayerCompositionState outputLayerCompositionState;
149     LayerFECompositionState layerFECompositionState;
150     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
151                        layerFECompositionState);
152     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
153     EXPECT_EQ(sSequenceId, mLayerState->getId());
154 }
155 
TEST_F(LayerStateTest,updateId)156 TEST_F(LayerStateTest, updateId) {
157     OutputLayerCompositionState outputLayerCompositionState;
158     LayerFECompositionState layerFECompositionState;
159     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
160                        layerFECompositionState);
161     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
162 
163     mock::OutputLayer newOutputLayer;
164     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
165     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
166                        layerFECompositionState, sSequenceIdTwo);
167     ftl::Flags<LayerStateField> updates = mLayerState->update(&newOutputLayer);
168     EXPECT_EQ(sSequenceIdTwo, mLayerState->getId());
169     EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::Id), updates);
170 }
171 
TEST_F(LayerStateTest,compareId)172 TEST_F(LayerStateTest, compareId) {
173     OutputLayerCompositionState outputLayerCompositionState;
174     LayerFECompositionState layerFECompositionState;
175     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
176                        layerFECompositionState);
177     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
178     mock::OutputLayer newOutputLayer;
179     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
180     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
181                        layerFECompositionState, sSequenceIdTwo);
182     auto otherLayerState = std::make_unique<LayerState>(&newOutputLayer);
183 
184     EXPECT_NE(mLayerState->getId(), otherLayerState->getId());
185 
186     // Id is a unique field, so it's not computed in the hash for a layer state.
187     verifyUniqueDifferingFields(*mLayerState, *otherLayerState);
188     EXPECT_FALSE(mLayerState->compare(*otherLayerState));
189     EXPECT_FALSE(otherLayerState->compare(*mLayerState));
190 }
191 
TEST_F(LayerStateTest,getName)192 TEST_F(LayerStateTest, getName) {
193     OutputLayerCompositionState outputLayerCompositionState;
194     LayerFECompositionState layerFECompositionState;
195     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
196                        layerFECompositionState);
197     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
198     EXPECT_EQ(sDebugName, mLayerState->getName());
199 }
200 
TEST_F(LayerStateTest,updateName)201 TEST_F(LayerStateTest, updateName) {
202     OutputLayerCompositionState outputLayerCompositionState;
203     LayerFECompositionState layerFECompositionState;
204     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
205                        layerFECompositionState);
206     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
207 
208     mock::OutputLayer newOutputLayer;
209     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
210     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
211                        layerFECompositionState, sSequenceId, sDebugNameTwo);
212     ftl::Flags<LayerStateField> updates = mLayerState->update(&newOutputLayer);
213     EXPECT_EQ(sDebugNameTwo, mLayerState->getName());
214     EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::Name), updates);
215 }
216 
TEST_F(LayerStateTest,compareName)217 TEST_F(LayerStateTest, compareName) {
218     OutputLayerCompositionState outputLayerCompositionState;
219     LayerFECompositionState layerFECompositionState;
220     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
221                        layerFECompositionState);
222     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
223     mock::OutputLayer newOutputLayer;
224     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
225     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
226                        layerFECompositionState, sSequenceId, sDebugNameTwo);
227     auto otherLayerState = std::make_unique<LayerState>(&newOutputLayer);
228 
229     EXPECT_NE(mLayerState->getName(), otherLayerState->getName());
230 
231     // Name is a unique field, so it's not computed in the hash for a layer state.
232     verifyUniqueDifferingFields(*mLayerState, *otherLayerState);
233     EXPECT_FALSE(mLayerState->compare(*otherLayerState));
234     EXPECT_FALSE(otherLayerState->compare(*mLayerState));
235 }
236 
TEST_F(LayerStateTest,getDisplayFrame)237 TEST_F(LayerStateTest, getDisplayFrame) {
238     OutputLayerCompositionState outputLayerCompositionState;
239     outputLayerCompositionState.displayFrame = sRectOne;
240     LayerFECompositionState layerFECompositionState;
241     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
242                        layerFECompositionState);
243     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
244     EXPECT_EQ(sRectOne, mLayerState->getDisplayFrame());
245 }
246 
TEST_F(LayerStateTest,updateDisplayFrame)247 TEST_F(LayerStateTest, updateDisplayFrame) {
248     OutputLayerCompositionState outputLayerCompositionState;
249     outputLayerCompositionState.displayFrame = sRectOne;
250     LayerFECompositionState layerFECompositionState;
251     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
252                        layerFECompositionState);
253     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
254 
255     mock::OutputLayer newOutputLayer;
256     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
257     OutputLayerCompositionState outputLayerCompositionStateTwo;
258     outputLayerCompositionStateTwo.displayFrame = sRectTwo;
259     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionStateTwo,
260                        layerFECompositionState);
261     ftl::Flags<LayerStateField> updates = mLayerState->update(&newOutputLayer);
262     EXPECT_EQ(sRectTwo, mLayerState->getDisplayFrame());
263     EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::DisplayFrame), updates);
264 }
265 
TEST_F(LayerStateTest,compareDisplayFrame)266 TEST_F(LayerStateTest, compareDisplayFrame) {
267     OutputLayerCompositionState outputLayerCompositionState;
268     outputLayerCompositionState.displayFrame = sRectOne;
269     LayerFECompositionState layerFECompositionState;
270     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
271                        layerFECompositionState);
272     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
273     mock::OutputLayer newOutputLayer;
274     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
275     OutputLayerCompositionState outputLayerCompositionStateTwo;
276     outputLayerCompositionStateTwo.displayFrame = sRectTwo;
277     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionStateTwo,
278                        layerFECompositionState);
279     auto otherLayerState = std::make_unique<LayerState>(&newOutputLayer);
280 
281     EXPECT_NE(mLayerState->getDisplayFrame(), otherLayerState->getDisplayFrame());
282 
283     verifyNonUniqueDifferingFields(*mLayerState, *otherLayerState, LayerStateField::DisplayFrame);
284     EXPECT_TRUE(mLayerState->compare(*otherLayerState));
285     EXPECT_TRUE(otherLayerState->compare(*mLayerState));
286 }
287 
TEST_F(LayerStateTest,getCompositionType)288 TEST_F(LayerStateTest, getCompositionType) {
289     OutputLayerCompositionState outputLayerCompositionState;
290     LayerFECompositionState layerFECompositionState;
291     layerFECompositionState.compositionType = Composition::DEVICE;
292     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
293                        layerFECompositionState);
294     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
295     EXPECT_EQ(Composition::DEVICE, mLayerState->getCompositionType());
296 }
297 
TEST_F(LayerStateTest,getCompositionType_forcedClient)298 TEST_F(LayerStateTest, getCompositionType_forcedClient) {
299     OutputLayerCompositionState outputLayerCompositionState;
300     outputLayerCompositionState.forceClientComposition = true;
301     LayerFECompositionState layerFECompositionState;
302     layerFECompositionState.compositionType = Composition::DEVICE;
303     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
304                        layerFECompositionState);
305     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
306     EXPECT_EQ(Composition::CLIENT, mLayerState->getCompositionType());
307 }
308 
TEST_F(LayerStateTest,getHdrSdrRatio)309 TEST_F(LayerStateTest, getHdrSdrRatio) {
310     OutputLayerCompositionState outputLayerCompositionState;
311     LayerFECompositionState layerFECompositionState;
312     layerFECompositionState.currentHdrSdrRatio = 2.f;
313     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
314                        layerFECompositionState);
315     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
316     EXPECT_EQ(2.f, mLayerState->getHdrSdrRatio());
317 }
318 
TEST_F(LayerStateTest,updateCompositionType)319 TEST_F(LayerStateTest, updateCompositionType) {
320     OutputLayerCompositionState outputLayerCompositionState;
321     LayerFECompositionState layerFECompositionState;
322     layerFECompositionState.compositionType = Composition::DEVICE;
323     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
324                        layerFECompositionState);
325     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
326 
327     mock::OutputLayer newOutputLayer;
328     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
329     LayerFECompositionState layerFECompositionStateTwo;
330     layerFECompositionStateTwo.compositionType = Composition::SOLID_COLOR;
331     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
332                        layerFECompositionStateTwo);
333     ftl::Flags<LayerStateField> updates = mLayerState->update(&newOutputLayer);
334     EXPECT_EQ(Composition::SOLID_COLOR, mLayerState->getCompositionType());
335     EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::CompositionType), updates);
336 }
337 
TEST_F(LayerStateTest,compareCompositionType)338 TEST_F(LayerStateTest, compareCompositionType) {
339     OutputLayerCompositionState outputLayerCompositionState;
340     LayerFECompositionState layerFECompositionState;
341     layerFECompositionState.compositionType = Composition::DEVICE;
342     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
343                        layerFECompositionState);
344     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
345     mock::OutputLayer newOutputLayer;
346     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
347     LayerFECompositionState layerFECompositionStateTwo;
348     layerFECompositionStateTwo.compositionType = Composition::SOLID_COLOR;
349     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
350                        layerFECompositionStateTwo);
351     auto otherLayerState = std::make_unique<LayerState>(&newOutputLayer);
352 
353     EXPECT_NE(mLayerState->getCompositionType(), otherLayerState->getCompositionType());
354 
355     verifyNonUniqueDifferingFields(*mLayerState, *otherLayerState,
356                                    LayerStateField::CompositionType);
357     EXPECT_TRUE(mLayerState->compare(*otherLayerState));
358     EXPECT_TRUE(otherLayerState->compare(*mLayerState));
359 }
360 
TEST_F(LayerStateTest,updateBuffer)361 TEST_F(LayerStateTest, updateBuffer) {
362     OutputLayerCompositionState outputLayerCompositionState;
363     LayerFECompositionState layerFECompositionState;
364     layerFECompositionState.buffer = sp<GraphicBuffer>::make();
365     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
366                        layerFECompositionState);
367     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
368 
369     mock::OutputLayer newOutputLayer;
370     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
371     LayerFECompositionState layerFECompositionStateTwo;
372     layerFECompositionStateTwo.buffer = sp<GraphicBuffer>::make();
373     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
374                        layerFECompositionStateTwo);
375     ftl::Flags<LayerStateField> updates = mLayerState->update(&newOutputLayer);
376     EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::Buffer), updates);
377 }
378 
TEST_F(LayerStateTest,updateBufferSingleBufferedLegacy)379 TEST_F(LayerStateTest, updateBufferSingleBufferedLegacy) {
380     OutputLayerCompositionState outputLayerCompositionState;
381     LayerFECompositionState layerFECompositionState;
382     layerFECompositionState.buffer = sp<GraphicBuffer>::make();
383     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
384                        layerFECompositionState);
385     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
386 
387     mock::OutputLayer newOutputLayer;
388     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
389     LayerFECompositionState layerFECompositionStateTwo;
390     layerFECompositionStateTwo.buffer = sp<GraphicBuffer>::make();
391     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
392                        layerFECompositionStateTwo);
393 
394     for (uint64_t i = 0; i < 10; i++) {
395         layerFECompositionStateTwo.frameNumber = i;
396         setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
397                            layerFECompositionStateTwo);
398         ftl::Flags<LayerStateField> updates = mLayerState->update(&newOutputLayer);
399         EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::Buffer), updates);
400     }
401 }
402 
TEST_F(LayerStateTest,updateBufferSingleBufferedUsage)403 TEST_F(LayerStateTest, updateBufferSingleBufferedUsage) {
404     OutputLayerCompositionState outputLayerCompositionState;
405     LayerFECompositionState layerFECompositionState;
406     layerFECompositionState.buffer = sp<GraphicBuffer>::make();
407     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
408                        layerFECompositionState);
409     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
410 
411     mock::OutputLayer newOutputLayer;
412     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
413     LayerFECompositionState layerFECompositionStateTwo;
414     layerFECompositionStateTwo.buffer = sp<GraphicBuffer>::make();
415     layerFECompositionStateTwo.buffer->usage = static_cast<uint64_t>(BufferUsage::FRONT_BUFFER);
416     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
417                        layerFECompositionStateTwo);
418 
419     for (uint64_t i = 0; i < 10; i++) {
420         setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
421                            layerFECompositionStateTwo);
422         ftl::Flags<LayerStateField> updates = mLayerState->update(&newOutputLayer);
423         EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::Buffer), updates);
424     }
425 }
426 
TEST_F(LayerStateTest,compareBuffer)427 TEST_F(LayerStateTest, compareBuffer) {
428     OutputLayerCompositionState outputLayerCompositionState;
429     LayerFECompositionState layerFECompositionState;
430     layerFECompositionState.buffer = sp<GraphicBuffer>::make();
431     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
432                        layerFECompositionState);
433     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
434     mock::OutputLayer newOutputLayer;
435     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
436     LayerFECompositionState layerFECompositionStateTwo;
437     layerFECompositionStateTwo.buffer = sp<GraphicBuffer>::make();
438     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
439                        layerFECompositionStateTwo);
440     auto otherLayerState = std::make_unique<LayerState>(&newOutputLayer);
441 
442     // A buffer is not a unique field, but the assertions are the same.
443     verifyUniqueDifferingFields(*mLayerState, *otherLayerState);
444 
445     // Buffers are explicitly excluded from comparison
446     EXPECT_FALSE(mLayerState->compare(*otherLayerState));
447     EXPECT_FALSE(otherLayerState->compare(*mLayerState));
448 }
449 
TEST_F(LayerStateTest,updateSourceCrop)450 TEST_F(LayerStateTest, updateSourceCrop) {
451     OutputLayerCompositionState outputLayerCompositionState;
452     outputLayerCompositionState.sourceCrop = sFloatRectOne;
453     LayerFECompositionState layerFECompositionState;
454     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
455                        layerFECompositionState);
456     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
457 
458     mock::OutputLayer newOutputLayer;
459     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
460     OutputLayerCompositionState outputLayerCompositionStateTwo;
461     outputLayerCompositionStateTwo.sourceCrop = sFloatRectTwo;
462     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionStateTwo,
463                        layerFECompositionState);
464     ftl::Flags<LayerStateField> updates = mLayerState->update(&newOutputLayer);
465     EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::SourceCrop), updates);
466 }
467 
TEST_F(LayerStateTest,compareSourceCrop)468 TEST_F(LayerStateTest, compareSourceCrop) {
469     SET_FLAG_FOR_TEST(com::android::graphics::surfaceflinger::flags::
470                               cache_when_source_crop_layer_only_moved,
471                       false);
472     OutputLayerCompositionState outputLayerCompositionState;
473     outputLayerCompositionState.sourceCrop = sFloatRectOne;
474     LayerFECompositionState layerFECompositionState;
475     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
476                        layerFECompositionState);
477     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
478     mock::OutputLayer newOutputLayer;
479     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
480     OutputLayerCompositionState outputLayerCompositionStateTwo;
481     outputLayerCompositionStateTwo.sourceCrop = sFloatRectTwo;
482     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionStateTwo,
483                        layerFECompositionState);
484     auto otherLayerState = std::make_unique<LayerState>(&newOutputLayer);
485 
486     verifyNonUniqueDifferingFields(*mLayerState, *otherLayerState, LayerStateField::SourceCrop);
487 
488     EXPECT_TRUE(mLayerState->compare(*otherLayerState));
489     EXPECT_TRUE(otherLayerState->compare(*mLayerState));
490 }
491 
TEST_F(LayerStateTest,updateBufferTransform)492 TEST_F(LayerStateTest, updateBufferTransform) {
493     OutputLayerCompositionState outputLayerCompositionState;
494     outputLayerCompositionState.bufferTransform = Hwc2::Transform::FLIP_H;
495     LayerFECompositionState layerFECompositionState;
496     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
497                        layerFECompositionState);
498     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
499 
500     mock::OutputLayer newOutputLayer;
501     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
502     OutputLayerCompositionState outputLayerCompositionStateTwo;
503     outputLayerCompositionStateTwo.bufferTransform = Hwc2::Transform::FLIP_V;
504     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionStateTwo,
505                        layerFECompositionState);
506     ftl::Flags<LayerStateField> updates = mLayerState->update(&newOutputLayer);
507     EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::BufferTransform), updates);
508 }
509 
TEST_F(LayerStateTest,compareBufferTransform)510 TEST_F(LayerStateTest, compareBufferTransform) {
511     OutputLayerCompositionState outputLayerCompositionState;
512     outputLayerCompositionState.bufferTransform = Hwc2::Transform::FLIP_H;
513     LayerFECompositionState layerFECompositionState;
514     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
515                        layerFECompositionState);
516     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
517     mock::OutputLayer newOutputLayer;
518     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
519     OutputLayerCompositionState outputLayerCompositionStateTwo;
520     outputLayerCompositionStateTwo.bufferTransform = Hwc2::Transform::FLIP_V;
521     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionStateTwo,
522                        layerFECompositionState);
523     auto otherLayerState = std::make_unique<LayerState>(&newOutputLayer);
524 
525     verifyNonUniqueDifferingFields(*mLayerState, *otherLayerState,
526                                    LayerStateField::BufferTransform);
527 
528     EXPECT_TRUE(mLayerState->compare(*otherLayerState));
529     EXPECT_TRUE(otherLayerState->compare(*mLayerState));
530 }
531 
TEST_F(LayerStateTest,updateBlendMode)532 TEST_F(LayerStateTest, updateBlendMode) {
533     OutputLayerCompositionState outputLayerCompositionState;
534     LayerFECompositionState layerFECompositionState;
535     layerFECompositionState.blendMode = hal::BlendMode::COVERAGE;
536     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
537                        layerFECompositionState);
538     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
539 
540     mock::OutputLayer newOutputLayer;
541     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
542     LayerFECompositionState layerFECompositionStateTwo;
543     layerFECompositionStateTwo.blendMode = hal::BlendMode::PREMULTIPLIED;
544     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
545                        layerFECompositionStateTwo);
546     ftl::Flags<LayerStateField> updates = mLayerState->update(&newOutputLayer);
547     EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::BlendMode), updates);
548 }
549 
TEST_F(LayerStateTest,compareBlendMode)550 TEST_F(LayerStateTest, compareBlendMode) {
551     OutputLayerCompositionState outputLayerCompositionState;
552     LayerFECompositionState layerFECompositionState;
553     layerFECompositionState.blendMode = hal::BlendMode::COVERAGE;
554     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
555                        layerFECompositionState);
556     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
557     mock::OutputLayer newOutputLayer;
558     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
559     LayerFECompositionState layerFECompositionStateTwo;
560     layerFECompositionStateTwo.blendMode = hal::BlendMode::PREMULTIPLIED;
561     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
562                        layerFECompositionStateTwo);
563     auto otherLayerState = std::make_unique<LayerState>(&newOutputLayer);
564 
565     verifyNonUniqueDifferingFields(*mLayerState, *otherLayerState, LayerStateField::BlendMode);
566 
567     EXPECT_TRUE(mLayerState->compare(*otherLayerState));
568     EXPECT_TRUE(otherLayerState->compare(*mLayerState));
569 }
570 
TEST_F(LayerStateTest,updateAlpha)571 TEST_F(LayerStateTest, updateAlpha) {
572     OutputLayerCompositionState outputLayerCompositionState;
573     LayerFECompositionState layerFECompositionState;
574     layerFECompositionState.alpha = sAlphaOne;
575     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
576                        layerFECompositionState);
577     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
578 
579     mock::OutputLayer newOutputLayer;
580     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
581     LayerFECompositionState layerFECompositionStateTwo;
582     layerFECompositionStateTwo.alpha = sAlphaTwo;
583     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
584                        layerFECompositionStateTwo);
585     ftl::Flags<LayerStateField> updates = mLayerState->update(&newOutputLayer);
586     EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::Alpha), updates);
587 }
588 
TEST_F(LayerStateTest,compareAlpha)589 TEST_F(LayerStateTest, compareAlpha) {
590     OutputLayerCompositionState outputLayerCompositionState;
591     LayerFECompositionState layerFECompositionState;
592     layerFECompositionState.alpha = sAlphaOne;
593     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
594                        layerFECompositionState);
595     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
596     mock::OutputLayer newOutputLayer;
597     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
598     LayerFECompositionState layerFECompositionStateTwo;
599     layerFECompositionStateTwo.alpha = sAlphaTwo;
600     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
601                        layerFECompositionStateTwo);
602     auto otherLayerState = std::make_unique<LayerState>(&newOutputLayer);
603 
604     verifyNonUniqueDifferingFields(*mLayerState, *otherLayerState, LayerStateField::Alpha);
605 
606     EXPECT_TRUE(mLayerState->compare(*otherLayerState));
607     EXPECT_TRUE(otherLayerState->compare(*mLayerState));
608 }
609 
TEST_F(LayerStateTest,updateLayerMetadata)610 TEST_F(LayerStateTest, updateLayerMetadata) {
611     OutputLayerCompositionState outputLayerCompositionState;
612     LayerFECompositionState layerFECompositionState;
613     layerFECompositionState.metadata[sMetadataKeyOne] = sMetadataValueOne;
614     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
615                        layerFECompositionState);
616     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
617 
618     mock::OutputLayer newOutputLayer;
619     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
620     LayerFECompositionState layerFECompositionStateTwo;
621     layerFECompositionStateTwo.metadata[sMetadataKeyTwo] = sMetadataValueTwo;
622     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
623                        layerFECompositionStateTwo);
624     ftl::Flags<LayerStateField> updates = mLayerState->update(&newOutputLayer);
625     EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::LayerMetadata), updates);
626 }
627 
TEST_F(LayerStateTest,compareLayerMetadata)628 TEST_F(LayerStateTest, compareLayerMetadata) {
629     OutputLayerCompositionState outputLayerCompositionState;
630     LayerFECompositionState layerFECompositionState;
631     layerFECompositionState.metadata[sMetadataKeyOne] = sMetadataValueOne;
632     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
633                        layerFECompositionState);
634     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
635     mock::OutputLayer newOutputLayer;
636     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
637     LayerFECompositionState layerFECompositionStateTwo;
638     layerFECompositionStateTwo.metadata[sMetadataKeyTwo] = sMetadataValueTwo;
639     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
640                        layerFECompositionStateTwo);
641     auto otherLayerState = std::make_unique<LayerState>(&newOutputLayer);
642 
643     verifyNonUniqueDifferingFields(*mLayerState, *otherLayerState, LayerStateField::LayerMetadata);
644 
645     EXPECT_TRUE(mLayerState->compare(*otherLayerState));
646     EXPECT_TRUE(otherLayerState->compare(*mLayerState));
647 }
648 
TEST_F(LayerStateTest,getVisibleRegion)649 TEST_F(LayerStateTest, getVisibleRegion) {
650     OutputLayerCompositionState outputLayerCompositionState;
651     outputLayerCompositionState.visibleRegion = sRegionOne;
652     LayerFECompositionState layerFECompositionState;
653     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
654                        layerFECompositionState);
655     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
656     EXPECT_TRUE(mLayerState->getVisibleRegion().hasSameRects(sRegionOne));
657 }
658 
TEST_F(LayerStateTest,updateVisibleRegion)659 TEST_F(LayerStateTest, updateVisibleRegion) {
660     OutputLayerCompositionState outputLayerCompositionState;
661     outputLayerCompositionState.visibleRegion = sRegionOne;
662     LayerFECompositionState layerFECompositionState;
663     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
664                        layerFECompositionState);
665     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
666 
667     mock::OutputLayer newOutputLayer;
668     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
669     OutputLayerCompositionState outputLayerCompositionStateTwo;
670     outputLayerCompositionStateTwo.visibleRegion = sRegionTwo;
671     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionStateTwo,
672                        layerFECompositionState);
673     ftl::Flags<LayerStateField> updates = mLayerState->update(&newOutputLayer);
674     EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::VisibleRegion), updates);
675 }
676 
TEST_F(LayerStateTest,compareVisibleRegion)677 TEST_F(LayerStateTest, compareVisibleRegion) {
678     OutputLayerCompositionState outputLayerCompositionState;
679     outputLayerCompositionState.visibleRegion = sRegionOne;
680     LayerFECompositionState layerFECompositionState;
681     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
682                        layerFECompositionState);
683     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
684     mock::OutputLayer newOutputLayer;
685     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
686     OutputLayerCompositionState outputLayerCompositionStateTwo;
687     outputLayerCompositionStateTwo.visibleRegion = sRegionTwo;
688     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionStateTwo,
689                        layerFECompositionState);
690     auto otherLayerState = std::make_unique<LayerState>(&newOutputLayer);
691 
692     verifyNonUniqueDifferingFields(*mLayerState, *otherLayerState, LayerStateField::VisibleRegion);
693 
694     EXPECT_TRUE(mLayerState->compare(*otherLayerState));
695     EXPECT_TRUE(otherLayerState->compare(*mLayerState));
696 }
697 
TEST_F(LayerStateTest,updateDataspace)698 TEST_F(LayerStateTest, updateDataspace) {
699     OutputLayerCompositionState outputLayerCompositionState;
700     outputLayerCompositionState.dataspace = ui::Dataspace::SRGB;
701     LayerFECompositionState layerFECompositionState;
702     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
703                        layerFECompositionState);
704     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
705 
706     mock::OutputLayer newOutputLayer;
707     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
708     OutputLayerCompositionState outputLayerCompositionStateTwo;
709     outputLayerCompositionStateTwo.dataspace = ui::Dataspace::DISPLAY_P3;
710     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionStateTwo,
711                        layerFECompositionState);
712     ftl::Flags<LayerStateField> updates = mLayerState->update(&newOutputLayer);
713     EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::Dataspace), updates);
714 }
715 
TEST_F(LayerStateTest,compareDataspace)716 TEST_F(LayerStateTest, compareDataspace) {
717     OutputLayerCompositionState outputLayerCompositionState;
718     outputLayerCompositionState.dataspace = ui::Dataspace::SRGB;
719     LayerFECompositionState layerFECompositionState;
720     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
721                        layerFECompositionState);
722     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
723     mock::OutputLayer newOutputLayer;
724     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
725     OutputLayerCompositionState outputLayerCompositionStateTwo;
726     outputLayerCompositionStateTwo.dataspace = ui::Dataspace::DISPLAY_P3;
727     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionStateTwo,
728                        layerFECompositionState);
729     auto otherLayerState = std::make_unique<LayerState>(&newOutputLayer);
730 
731     verifyNonUniqueDifferingFields(*mLayerState, *otherLayerState, LayerStateField::Dataspace);
732 
733     EXPECT_TRUE(mLayerState->compare(*otherLayerState));
734     EXPECT_TRUE(otherLayerState->compare(*mLayerState));
735 }
736 
TEST_F(LayerStateTest,updatePixelFormat)737 TEST_F(LayerStateTest, updatePixelFormat) {
738     OutputLayerCompositionState outputLayerCompositionState;
739     LayerFECompositionState layerFECompositionState;
740     layerFECompositionState.buffer =
741             sp<GraphicBuffer>::make(1u, 1u, PIXEL_FORMAT_RGBA_8888, kUsageFlags, "buffer1");
742     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
743                        layerFECompositionState);
744     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
745 
746     mock::OutputLayer newOutputLayer;
747     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
748     LayerFECompositionState layerFECompositionStateTwo;
749     layerFECompositionStateTwo.buffer =
750             sp<GraphicBuffer>::make(1u, 1u, PIXEL_FORMAT_RGBX_8888, kUsageFlags, "buffer2");
751     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
752                        layerFECompositionStateTwo);
753     ftl::Flags<LayerStateField> updates = mLayerState->update(&newOutputLayer);
754     EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::Buffer) |
755                       ftl::Flags<LayerStateField>(LayerStateField::PixelFormat),
756               updates);
757 }
758 
TEST_F(LayerStateTest,comparePixelFormat)759 TEST_F(LayerStateTest, comparePixelFormat) {
760     OutputLayerCompositionState outputLayerCompositionState;
761     LayerFECompositionState layerFECompositionState;
762     layerFECompositionState.buffer =
763             sp<GraphicBuffer>::make(1u, 1u, PIXEL_FORMAT_RGBA_8888, kUsageFlags, "buffer1");
764     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
765                        layerFECompositionState);
766     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
767     mock::OutputLayer newOutputLayer;
768     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
769     LayerFECompositionState layerFECompositionStateTwo;
770     layerFECompositionStateTwo.buffer =
771             sp<GraphicBuffer>::make(1u, 1u, PIXEL_FORMAT_RGBX_8888, kUsageFlags, "buffer2");
772     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
773                        layerFECompositionStateTwo);
774     auto otherLayerState = std::make_unique<LayerState>(&newOutputLayer);
775 
776     verifyNonUniqueDifferingFields(*mLayerState, *otherLayerState,
777                                    ftl::Flags<LayerStateField>(LayerStateField::PixelFormat));
778 
779     EXPECT_TRUE(mLayerState->compare(*otherLayerState));
780     EXPECT_TRUE(otherLayerState->compare(*mLayerState));
781 }
782 
TEST_F(LayerStateTest,updateColorTransform)783 TEST_F(LayerStateTest, updateColorTransform) {
784     OutputLayerCompositionState outputLayerCompositionState;
785     LayerFECompositionState layerFECompositionState;
786     layerFECompositionState.colorTransformIsIdentity = true;
787     layerFECompositionState.colorTransform = mat4();
788     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
789                        layerFECompositionState);
790     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
791 
792     mock::OutputLayer newOutputLayer;
793     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
794     LayerFECompositionState layerFECompositionStateTwo;
795     layerFECompositionStateTwo.colorTransformIsIdentity = false;
796     layerFECompositionStateTwo.colorTransform = sMat4One;
797     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
798                        layerFECompositionStateTwo);
799     ftl::Flags<LayerStateField> updates = mLayerState->update(&newOutputLayer);
800     EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::ColorTransform), updates);
801 }
802 
TEST_F(LayerStateTest,compareColorTransform)803 TEST_F(LayerStateTest, compareColorTransform) {
804     OutputLayerCompositionState outputLayerCompositionState;
805     LayerFECompositionState layerFECompositionState;
806     layerFECompositionState.colorTransformIsIdentity = true;
807     layerFECompositionState.colorTransform = mat4();
808     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
809                        layerFECompositionState);
810     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
811     mock::OutputLayer newOutputLayer;
812     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
813     LayerFECompositionState layerFECompositionStateTwo;
814     layerFECompositionStateTwo.colorTransformIsIdentity = false;
815     layerFECompositionStateTwo.colorTransform = sMat4One;
816     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
817                        layerFECompositionStateTwo);
818     auto otherLayerState = std::make_unique<LayerState>(&newOutputLayer);
819 
820     verifyNonUniqueDifferingFields(*mLayerState, *otherLayerState, LayerStateField::ColorTransform);
821 
822     EXPECT_TRUE(mLayerState->compare(*otherLayerState));
823     EXPECT_TRUE(otherLayerState->compare(*mLayerState));
824 }
825 
TEST_F(LayerStateTest,updateSidebandStream)826 TEST_F(LayerStateTest, updateSidebandStream) {
827     OutputLayerCompositionState outputLayerCompositionState;
828     LayerFECompositionState layerFECompositionState;
829     layerFECompositionState.sidebandStream = NativeHandle::create(sFakeSidebandStreamOne, false);
830     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
831                        layerFECompositionState);
832     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
833 
834     mock::OutputLayer newOutputLayer;
835     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
836     LayerFECompositionState layerFECompositionStateTwo;
837     layerFECompositionStateTwo.sidebandStream = NativeHandle::create(sFakeSidebandStreamTwo, false);
838     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
839                        layerFECompositionStateTwo);
840     ftl::Flags<LayerStateField> updates = mLayerState->update(&newOutputLayer);
841     EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::SidebandStream), updates);
842 }
843 
TEST_F(LayerStateTest,compareSidebandStream)844 TEST_F(LayerStateTest, compareSidebandStream) {
845     OutputLayerCompositionState outputLayerCompositionState;
846     LayerFECompositionState layerFECompositionState;
847     layerFECompositionState.sidebandStream = NativeHandle::create(sFakeSidebandStreamOne, false);
848     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
849                        layerFECompositionState);
850     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
851     mock::OutputLayer newOutputLayer;
852     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
853     LayerFECompositionState layerFECompositionStateTwo;
854     layerFECompositionStateTwo.sidebandStream = NativeHandle::create(sFakeSidebandStreamTwo, false);
855     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
856                        layerFECompositionStateTwo);
857     auto otherLayerState = std::make_unique<LayerState>(&newOutputLayer);
858 
859     verifyNonUniqueDifferingFields(*mLayerState, *otherLayerState, LayerStateField::SidebandStream);
860 
861     EXPECT_TRUE(mLayerState->compare(*otherLayerState));
862     EXPECT_TRUE(otherLayerState->compare(*mLayerState));
863 }
864 
TEST_F(LayerStateTest,updateSolidColor)865 TEST_F(LayerStateTest, updateSolidColor) {
866     OutputLayerCompositionState outputLayerCompositionState;
867     LayerFECompositionState layerFECompositionState;
868     layerFECompositionState.color = sHalf4One;
869     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
870                        layerFECompositionState);
871     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
872 
873     mock::OutputLayer newOutputLayer;
874     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
875     LayerFECompositionState layerFECompositionStateTwo;
876     layerFECompositionStateTwo.color = sHalf4Two;
877     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
878                        layerFECompositionStateTwo);
879     ftl::Flags<LayerStateField> updates = mLayerState->update(&newOutputLayer);
880     EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::SolidColor), updates);
881 }
882 
TEST_F(LayerStateTest,compareSolidColor)883 TEST_F(LayerStateTest, compareSolidColor) {
884     OutputLayerCompositionState outputLayerCompositionState;
885     LayerFECompositionState layerFECompositionState;
886     layerFECompositionState.color = sHalf4One;
887     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
888                        layerFECompositionState);
889     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
890     mock::OutputLayer newOutputLayer;
891     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
892     LayerFECompositionState layerFECompositionStateTwo;
893     layerFECompositionStateTwo.color = sHalf4Two;
894     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
895                        layerFECompositionStateTwo);
896     auto otherLayerState = std::make_unique<LayerState>(&newOutputLayer);
897 
898     verifyNonUniqueDifferingFields(*mLayerState, *otherLayerState, LayerStateField::SolidColor);
899 
900     EXPECT_TRUE(mLayerState->compare(*otherLayerState));
901     EXPECT_TRUE(otherLayerState->compare(*mLayerState));
902 }
903 
TEST_F(LayerStateTest,updateBackgroundBlur)904 TEST_F(LayerStateTest, updateBackgroundBlur) {
905     OutputLayerCompositionState outputLayerCompositionState;
906     LayerFECompositionState layerFECompositionState;
907     layerFECompositionState.backgroundBlurRadius = sBgBlurRadiusOne;
908     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
909                        layerFECompositionState);
910     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
911 
912     mock::OutputLayer newOutputLayer;
913     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
914     LayerFECompositionState layerFECompositionStateTwo;
915     layerFECompositionStateTwo.backgroundBlurRadius = sBgBlurRadiusTwo;
916     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
917                        layerFECompositionStateTwo);
918     ftl::Flags<LayerStateField> updates = mLayerState->update(&newOutputLayer);
919     EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::BackgroundBlurRadius), updates);
920 }
921 
TEST_F(LayerStateTest,compareBackgroundBlur)922 TEST_F(LayerStateTest, compareBackgroundBlur) {
923     OutputLayerCompositionState outputLayerCompositionState;
924     LayerFECompositionState layerFECompositionState;
925     layerFECompositionState.backgroundBlurRadius = sBgBlurRadiusOne;
926     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
927                        layerFECompositionState);
928     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
929     mock::OutputLayer newOutputLayer;
930     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
931     LayerFECompositionState layerFECompositionStateTwo;
932     layerFECompositionStateTwo.backgroundBlurRadius = sBgBlurRadiusTwo;
933     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
934                        layerFECompositionStateTwo);
935     auto otherLayerState = std::make_unique<LayerState>(&newOutputLayer);
936 
937     verifyNonUniqueDifferingFields(*mLayerState, *otherLayerState,
938                                    LayerStateField::BackgroundBlurRadius);
939 
940     EXPECT_TRUE(mLayerState->compare(*otherLayerState));
941     EXPECT_TRUE(otherLayerState->compare(*mLayerState));
942 }
943 
TEST_F(LayerStateTest,updateBlurRegions)944 TEST_F(LayerStateTest, updateBlurRegions) {
945     OutputLayerCompositionState outputLayerCompositionState;
946     LayerFECompositionState layerFECompositionState;
947     layerFECompositionState.blurRegions.push_back(sBlurRegionOne);
948     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
949                        layerFECompositionState);
950     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
951 
952     mock::OutputLayer newOutputLayer;
953     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
954     LayerFECompositionState layerFECompositionStateTwo;
955     layerFECompositionStateTwo.blurRegions.push_back(sBlurRegionTwo);
956     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
957                        layerFECompositionStateTwo);
958     ftl::Flags<LayerStateField> updates = mLayerState->update(&newOutputLayer);
959     EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::BlurRegions), updates);
960 }
961 
TEST_F(LayerStateTest,compareBlurRegions)962 TEST_F(LayerStateTest, compareBlurRegions) {
963     OutputLayerCompositionState outputLayerCompositionState;
964     LayerFECompositionState layerFECompositionState;
965     layerFECompositionState.blurRegions.push_back(sBlurRegionOne);
966     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
967                        layerFECompositionState);
968     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
969     mock::OutputLayer newOutputLayer;
970     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
971     LayerFECompositionState layerFECompositionStateTwo;
972     layerFECompositionStateTwo.blurRegions.push_back(sBlurRegionTwo);
973     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
974                        layerFECompositionStateTwo);
975     auto otherLayerState = std::make_unique<LayerState>(&newOutputLayer);
976 
977     verifyNonUniqueDifferingFields(*mLayerState, *otherLayerState, LayerStateField::BlurRegions);
978 
979     EXPECT_TRUE(mLayerState->compare(*otherLayerState));
980     EXPECT_TRUE(otherLayerState->compare(*mLayerState));
981 }
982 
TEST_F(LayerStateTest,hasBlurBehind_noBlur_returnsFalse)983 TEST_F(LayerStateTest, hasBlurBehind_noBlur_returnsFalse) {
984     OutputLayerCompositionState outputLayerCompositionState;
985     LayerFECompositionState layerFECompositionState;
986     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
987                        layerFECompositionState);
988     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
989     EXPECT_FALSE(mLayerState->hasBlurBehind());
990 }
991 
TEST_F(LayerStateTest,hasBlurBehind_withBackgroundBlur_returnsTrue)992 TEST_F(LayerStateTest, hasBlurBehind_withBackgroundBlur_returnsTrue) {
993     OutputLayerCompositionState outputLayerCompositionState;
994     LayerFECompositionState layerFECompositionState;
995     layerFECompositionState.backgroundBlurRadius = sBgBlurRadiusOne;
996     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
997                        layerFECompositionState);
998     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
999     EXPECT_TRUE(mLayerState->hasBlurBehind());
1000 }
1001 
TEST_F(LayerStateTest,hasBlurBehind_withBlurRegion_returnsTrue)1002 TEST_F(LayerStateTest, hasBlurBehind_withBlurRegion_returnsTrue) {
1003     OutputLayerCompositionState outputLayerCompositionState;
1004     LayerFECompositionState layerFECompositionState;
1005     layerFECompositionState.blurRegions.push_back(sBlurRegionOne);
1006     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
1007                        layerFECompositionState);
1008     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
1009     EXPECT_TRUE(mLayerState->hasBlurBehind());
1010 }
1011 
TEST_F(LayerStateTest,updateCachingHint)1012 TEST_F(LayerStateTest, updateCachingHint) {
1013     OutputLayerCompositionState outputLayerCompositionState;
1014     LayerFECompositionState layerFECompositionState;
1015     layerFECompositionState.cachingHint = gui::CachingHint::Enabled;
1016     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
1017                        layerFECompositionState);
1018     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
1019 
1020     mock::OutputLayer newOutputLayer;
1021     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
1022     LayerFECompositionState layerFECompositionStateTwo;
1023     layerFECompositionStateTwo.cachingHint = gui::CachingHint::Disabled;
1024     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
1025                        layerFECompositionStateTwo);
1026     ftl::Flags<LayerStateField> updates = mLayerState->update(&newOutputLayer);
1027     EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::CachingHint), updates);
1028 }
1029 
TEST_F(LayerStateTest,compareCachingHint)1030 TEST_F(LayerStateTest, compareCachingHint) {
1031     OutputLayerCompositionState outputLayerCompositionState;
1032     LayerFECompositionState layerFECompositionState;
1033     layerFECompositionState.cachingHint = gui::CachingHint::Enabled;
1034     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
1035                        layerFECompositionState);
1036     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
1037     mock::OutputLayer newOutputLayer;
1038     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
1039     LayerFECompositionState layerFECompositionStateTwo;
1040     layerFECompositionStateTwo.cachingHint = gui::CachingHint::Disabled;
1041     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
1042                        layerFECompositionStateTwo);
1043     auto otherLayerState = std::make_unique<LayerState>(&newOutputLayer);
1044 
1045     verifyNonUniqueDifferingFields(*mLayerState, *otherLayerState, LayerStateField::CachingHint);
1046 
1047     EXPECT_TRUE(mLayerState->compare(*otherLayerState));
1048     EXPECT_TRUE(otherLayerState->compare(*mLayerState));
1049 }
1050 
TEST_F(LayerStateTest,updateDimmingEnabled)1051 TEST_F(LayerStateTest, updateDimmingEnabled) {
1052     OutputLayerCompositionState outputLayerCompositionState;
1053     LayerFECompositionState layerFECompositionState;
1054     layerFECompositionState.dimmingEnabled = true;
1055     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
1056                        layerFECompositionState);
1057     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
1058     EXPECT_TRUE(mLayerState->isDimmingEnabled());
1059 
1060     mock::OutputLayer newOutputLayer;
1061     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
1062     LayerFECompositionState layerFECompositionStateTwo;
1063     layerFECompositionStateTwo.dimmingEnabled = false;
1064     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
1065                        layerFECompositionStateTwo);
1066     ftl::Flags<LayerStateField> updates = mLayerState->update(&newOutputLayer);
1067     EXPECT_EQ(ftl::Flags<LayerStateField>(LayerStateField::DimmingEnabled), updates);
1068     EXPECT_FALSE(mLayerState->isDimmingEnabled());
1069 }
1070 
TEST_F(LayerStateTest,compareDimmingEnabled)1071 TEST_F(LayerStateTest, compareDimmingEnabled) {
1072     OutputLayerCompositionState outputLayerCompositionState;
1073     LayerFECompositionState layerFECompositionState;
1074     layerFECompositionState.dimmingEnabled = true;
1075     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
1076                        layerFECompositionState);
1077     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
1078     mock::OutputLayer newOutputLayer;
1079     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
1080     LayerFECompositionState layerFECompositionStateTwo;
1081     layerFECompositionStateTwo.dimmingEnabled = false;
1082     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
1083                        layerFECompositionStateTwo);
1084     auto otherLayerState = std::make_unique<LayerState>(&newOutputLayer);
1085 
1086     verifyNonUniqueDifferingFields(*mLayerState, *otherLayerState, LayerStateField::DimmingEnabled);
1087 
1088     EXPECT_TRUE(mLayerState->compare(*otherLayerState));
1089     EXPECT_TRUE(otherLayerState->compare(*mLayerState));
1090 }
1091 
TEST_F(LayerStateTest,dumpDoesNotCrash)1092 TEST_F(LayerStateTest, dumpDoesNotCrash) {
1093     OutputLayerCompositionState outputLayerCompositionState;
1094     LayerFECompositionState layerFECompositionState;
1095     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
1096                        layerFECompositionState);
1097     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
1098     std::string dump;
1099     mLayerState->dump(dump);
1100     EXPECT_TRUE(dump.size() > 0);
1101 }
1102 
TEST_F(LayerStateTest,framesSinceBufferUpdate)1103 TEST_F(LayerStateTest, framesSinceBufferUpdate) {
1104     OutputLayerCompositionState outputLayerCompositionState;
1105     LayerFECompositionState layerFECompositionState;
1106     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
1107                        layerFECompositionState);
1108     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
1109 
1110     EXPECT_EQ(0, mLayerState->getFramesSinceBufferUpdate());
1111     mLayerState->incrementFramesSinceBufferUpdate();
1112     EXPECT_EQ(1, mLayerState->getFramesSinceBufferUpdate());
1113     mLayerState->resetFramesSinceBufferUpdate();
1114     EXPECT_EQ(0, mLayerState->getFramesSinceBufferUpdate());
1115 }
1116 
TEST_F(LayerStateTest,getNonBufferHash_doesNotCommute)1117 TEST_F(LayerStateTest, getNonBufferHash_doesNotCommute) {
1118     OutputLayerCompositionState outputLayerCompositionState;
1119     outputLayerCompositionState.displayFrame = sRectOne;
1120     LayerFECompositionState layerFECompositionState;
1121     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
1122                        layerFECompositionState);
1123     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
1124     mock::OutputLayer newOutputLayer;
1125     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
1126     OutputLayerCompositionState outputLayerCompositionStateTwo;
1127     outputLayerCompositionStateTwo.displayFrame = sRectTwo;
1128     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionStateTwo,
1129                        layerFECompositionState);
1130     auto otherLayerState = std::make_unique<LayerState>(&newOutputLayer);
1131 
1132     EXPECT_NE(getNonBufferHash({mLayerState.get(), otherLayerState.get()}),
1133               getNonBufferHash({otherLayerState.get(), mLayerState.get()}));
1134 }
1135 
TEST_F(LayerStateTest,getNonBufferHash_isIdempotent)1136 TEST_F(LayerStateTest, getNonBufferHash_isIdempotent) {
1137     OutputLayerCompositionState outputLayerCompositionState;
1138     outputLayerCompositionState.displayFrame = sRectOne;
1139     LayerFECompositionState layerFECompositionState;
1140     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
1141                        layerFECompositionState);
1142     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
1143     mock::OutputLayer newOutputLayer;
1144     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
1145     OutputLayerCompositionState outputLayerCompositionStateTwo;
1146     outputLayerCompositionStateTwo.displayFrame = sRectTwo;
1147     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionStateTwo,
1148                        layerFECompositionState);
1149     auto otherLayerState = std::make_unique<LayerState>(&newOutputLayer);
1150 
1151     EXPECT_EQ(getNonBufferHash({mLayerState.get(), otherLayerState.get()}),
1152               getNonBufferHash({mLayerState.get(), otherLayerState.get()}));
1153 }
1154 
TEST_F(LayerStateTest,getNonBufferHash_filtersOutBuffers)1155 TEST_F(LayerStateTest, getNonBufferHash_filtersOutBuffers) {
1156     OutputLayerCompositionState outputLayerCompositionState;
1157     LayerFECompositionState layerFECompositionState;
1158     layerFECompositionState.buffer = sp<GraphicBuffer>::make();
1159     setupMocksForLayer(mOutputLayer, *mLayerFE, outputLayerCompositionState,
1160                        layerFECompositionState);
1161     mLayerState = std::make_unique<LayerState>(&mOutputLayer);
1162 
1163     mock::OutputLayer newOutputLayer;
1164     sp<mock::LayerFE> newLayerFE = sp<mock::LayerFE>::make();
1165     LayerFECompositionState layerFECompositionStateTwo;
1166     layerFECompositionStateTwo.buffer = sp<GraphicBuffer>::make();
1167     setupMocksForLayer(newOutputLayer, *newLayerFE, outputLayerCompositionState,
1168                        layerFECompositionStateTwo);
1169     auto otherLayerState = std::make_unique<LayerState>(&newOutputLayer);
1170 
1171     EXPECT_EQ(getNonBufferHash({mLayerState.get()}), getNonBufferHash({otherLayerState.get()}));
1172 }
1173 
1174 } // namespace
1175 } // namespace android::compositionengine::impl::planner
1176