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/OutputLayer.h>
18 #include <compositionengine/impl/OutputLayerCompositionState.h>
19 #include <compositionengine/mock/CompositionEngine.h>
20 #include <compositionengine/mock/DisplayColorProfile.h>
21 #include <compositionengine/mock/LayerFE.h>
22 #include <compositionengine/mock/Output.h>
23 #include <gtest/gtest.h>
24
25 #include "MockHWC2.h"
26 #include "MockHWComposer.h"
27 #include "RegionMatcher.h"
28
29 namespace android::compositionengine {
30 namespace {
31
32 namespace hal = android::hardware::graphics::composer::hal;
33
34 using testing::_;
35 using testing::InSequence;
36 using testing::Return;
37 using testing::ReturnRef;
38 using testing::StrictMock;
39
40 constexpr auto TR_IDENT = 0u;
41 constexpr auto TR_FLP_H = HAL_TRANSFORM_FLIP_H;
42 constexpr auto TR_FLP_V = HAL_TRANSFORM_FLIP_V;
43 constexpr auto TR_ROT_90 = HAL_TRANSFORM_ROT_90;
44 constexpr auto TR_ROT_180 = TR_FLP_H | TR_FLP_V;
45 constexpr auto TR_ROT_270 = TR_ROT_90 | TR_ROT_180;
46
47 const std::string kOutputName{"Test Output"};
48
49 MATCHER_P(ColorEq, expected, "") {
50 *result_listener << "Colors are not equal\n";
51 *result_listener << "expected " << expected.r << " " << expected.g << " " << expected.b << " "
52 << expected.a << "\n";
53 *result_listener << "actual " << arg.r << " " << arg.g << " " << arg.b << " " << arg.a << "\n";
54
55 return expected.r == arg.r && expected.g == arg.g && expected.b == arg.b && expected.a == arg.a;
56 }
57
58 struct OutputLayerTest : public testing::Test {
59 struct OutputLayer final : public impl::OutputLayer {
OutputLayerandroid::compositionengine::__anon834e7eb10111::OutputLayerTest::OutputLayer60 OutputLayer(const compositionengine::Output& output, sp<compositionengine::LayerFE> layerFE)
61 : mOutput(output), mLayerFE(layerFE) {}
62 ~OutputLayer() override = default;
63
64 // compositionengine::OutputLayer overrides
getOutputandroid::compositionengine::__anon834e7eb10111::OutputLayerTest::OutputLayer65 const compositionengine::Output& getOutput() const override { return mOutput; }
getLayerFEandroid::compositionengine::__anon834e7eb10111::OutputLayerTest::OutputLayer66 compositionengine::LayerFE& getLayerFE() const override { return *mLayerFE; }
getStateandroid::compositionengine::__anon834e7eb10111::OutputLayerTest::OutputLayer67 const impl::OutputLayerCompositionState& getState() const override { return mState; }
editStateandroid::compositionengine::__anon834e7eb10111::OutputLayerTest::OutputLayer68 impl::OutputLayerCompositionState& editState() override { return mState; }
69
70 // compositionengine::impl::OutputLayer overrides
dumpStateandroid::compositionengine::__anon834e7eb10111::OutputLayerTest::OutputLayer71 void dumpState(std::string& out) const override { mState.dump(out); }
72
73 const compositionengine::Output& mOutput;
74 sp<compositionengine::LayerFE> mLayerFE;
75 impl::OutputLayerCompositionState mState;
76 };
77
OutputLayerTestandroid::compositionengine::__anon834e7eb10111::OutputLayerTest78 OutputLayerTest() {
79 EXPECT_CALL(*mLayerFE, getDebugName()).WillRepeatedly(Return("Test LayerFE"));
80 EXPECT_CALL(mOutput, getName()).WillRepeatedly(ReturnRef(kOutputName));
81
82 EXPECT_CALL(*mLayerFE, getCompositionState()).WillRepeatedly(Return(&mLayerFEState));
83 EXPECT_CALL(mOutput, getState()).WillRepeatedly(ReturnRef(mOutputState));
84 }
85
86 compositionengine::mock::Output mOutput;
87 sp<compositionengine::mock::LayerFE> mLayerFE{
88 new StrictMock<compositionengine::mock::LayerFE>()};
89 OutputLayer mOutputLayer{mOutput, mLayerFE};
90
91 LayerFECompositionState mLayerFEState;
92 impl::OutputCompositionState mOutputState;
93 };
94
95 /*
96 * Basic construction
97 */
98
TEST_F(OutputLayerTest,canInstantiateOutputLayer)99 TEST_F(OutputLayerTest, canInstantiateOutputLayer) {}
100
101 /*
102 * OutputLayer::setHwcLayer()
103 */
104
TEST_F(OutputLayerTest,settingNullHwcLayerSetsEmptyHwcState)105 TEST_F(OutputLayerTest, settingNullHwcLayerSetsEmptyHwcState) {
106 StrictMock<compositionengine::mock::CompositionEngine> compositionEngine;
107
108 mOutputLayer.setHwcLayer(nullptr);
109
110 EXPECT_FALSE(mOutputLayer.getState().hwc);
111 }
112
TEST_F(OutputLayerTest,settingHwcLayerSetsHwcState)113 TEST_F(OutputLayerTest, settingHwcLayerSetsHwcState) {
114 auto hwcLayer = std::make_shared<StrictMock<HWC2::mock::Layer>>();
115
116 mOutputLayer.setHwcLayer(hwcLayer);
117
118 const auto& outputLayerState = mOutputLayer.getState();
119 ASSERT_TRUE(outputLayerState.hwc);
120
121 const auto& hwcState = *outputLayerState.hwc;
122 EXPECT_EQ(hwcLayer, hwcState.hwcLayer);
123 }
124
125 /*
126 * OutputLayer::calculateOutputSourceCrop()
127 */
128
129 struct OutputLayerSourceCropTest : public OutputLayerTest {
OutputLayerSourceCropTestandroid::compositionengine::__anon834e7eb10111::OutputLayerSourceCropTest130 OutputLayerSourceCropTest() {
131 // Set reasonable default values for a simple case. Each test will
132 // set one specific value to something different.
133 mLayerFEState.geomUsesSourceCrop = true;
134 mLayerFEState.geomContentCrop = Rect{0, 0, 1920, 1080};
135 mLayerFEState.transparentRegionHint = Region{};
136 mLayerFEState.geomLayerBounds = FloatRect{0.f, 0.f, 1920.f, 1080.f};
137 mLayerFEState.geomLayerTransform = ui::Transform{TR_IDENT};
138 mLayerFEState.geomBufferSize = Rect{0, 0, 1920, 1080};
139 mLayerFEState.geomBufferTransform = TR_IDENT;
140
141 mOutputState.viewport = Rect{0, 0, 1920, 1080};
142 }
143
calculateOutputSourceCropandroid::compositionengine::__anon834e7eb10111::OutputLayerSourceCropTest144 FloatRect calculateOutputSourceCrop() {
145 mLayerFEState.geomInverseLayerTransform = mLayerFEState.geomLayerTransform.inverse();
146
147 return mOutputLayer.calculateOutputSourceCrop();
148 }
149 };
150
TEST_F(OutputLayerSourceCropTest,computesEmptyIfSourceCropNotUsed)151 TEST_F(OutputLayerSourceCropTest, computesEmptyIfSourceCropNotUsed) {
152 mLayerFEState.geomUsesSourceCrop = false;
153
154 const FloatRect expected{};
155 EXPECT_THAT(calculateOutputSourceCrop(), expected);
156 }
157
TEST_F(OutputLayerSourceCropTest,correctForSimpleDefaultCase)158 TEST_F(OutputLayerSourceCropTest, correctForSimpleDefaultCase) {
159 const FloatRect expected{0.f, 0.f, 1920.f, 1080.f};
160 EXPECT_THAT(calculateOutputSourceCrop(), expected);
161 }
162
TEST_F(OutputLayerSourceCropTest,handlesBoundsOutsideViewport)163 TEST_F(OutputLayerSourceCropTest, handlesBoundsOutsideViewport) {
164 mLayerFEState.geomLayerBounds = FloatRect{-2000.f, -2000.f, 2000.f, 2000.f};
165
166 const FloatRect expected{0.f, 0.f, 1920.f, 1080.f};
167 EXPECT_THAT(calculateOutputSourceCrop(), expected);
168 }
169
TEST_F(OutputLayerSourceCropTest,handlesBoundsOutsideViewportRotated)170 TEST_F(OutputLayerSourceCropTest, handlesBoundsOutsideViewportRotated) {
171 mLayerFEState.geomLayerBounds = FloatRect{-2000.f, -2000.f, 2000.f, 2000.f};
172 mLayerFEState.geomLayerTransform.set(HAL_TRANSFORM_ROT_90, 1920, 1080);
173
174 const FloatRect expected{0.f, 0.f, 1080.f, 1080.f};
175 EXPECT_THAT(calculateOutputSourceCrop(), expected);
176 }
177
TEST_F(OutputLayerSourceCropTest,calculateOutputSourceCropWorksWithATransformedBuffer)178 TEST_F(OutputLayerSourceCropTest, calculateOutputSourceCropWorksWithATransformedBuffer) {
179 struct Entry {
180 uint32_t bufferInvDisplay;
181 uint32_t buffer;
182 uint32_t display;
183 FloatRect expected;
184 };
185 // Not an exhaustive list of cases, but hopefully enough.
186 const std::array<Entry, 12> testData = {
187 // clang-format off
188 // inv buffer display expected
189 /* 0 */ Entry{false, TR_IDENT, TR_IDENT, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
190 /* 1 */ Entry{false, TR_IDENT, TR_ROT_90, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
191 /* 2 */ Entry{false, TR_IDENT, TR_ROT_180, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
192 /* 3 */ Entry{false, TR_IDENT, TR_ROT_270, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
193
194 /* 4 */ Entry{true, TR_IDENT, TR_IDENT, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
195 /* 5 */ Entry{true, TR_IDENT, TR_ROT_90, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
196 /* 6 */ Entry{true, TR_IDENT, TR_ROT_180, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
197 /* 7 */ Entry{true, TR_IDENT, TR_ROT_270, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
198
199 /* 8 */ Entry{false, TR_IDENT, TR_IDENT, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
200 /* 9 */ Entry{false, TR_ROT_90, TR_ROT_90, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
201 /* 10 */ Entry{false, TR_ROT_180, TR_ROT_180, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
202 /* 11 */ Entry{false, TR_ROT_270, TR_ROT_270, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
203
204 // clang-format on
205 };
206
207 for (size_t i = 0; i < testData.size(); i++) {
208 const auto& entry = testData[i];
209
210 mLayerFEState.geomBufferUsesDisplayInverseTransform = entry.bufferInvDisplay;
211 mLayerFEState.geomBufferTransform = entry.buffer;
212 mOutputState.orientation = entry.display;
213
214 EXPECT_THAT(calculateOutputSourceCrop(), entry.expected) << "entry " << i;
215 }
216 }
217
TEST_F(OutputLayerSourceCropTest,geomContentCropAffectsCrop)218 TEST_F(OutputLayerSourceCropTest, geomContentCropAffectsCrop) {
219 mLayerFEState.geomContentCrop = Rect{0, 0, 960, 540};
220
221 const FloatRect expected{0.f, 0.f, 960.f, 540.f};
222 EXPECT_THAT(calculateOutputSourceCrop(), expected);
223 }
224
TEST_F(OutputLayerSourceCropTest,viewportAffectsCrop)225 TEST_F(OutputLayerSourceCropTest, viewportAffectsCrop) {
226 mOutputState.viewport = Rect{0, 0, 960, 540};
227
228 const FloatRect expected{0.f, 0.f, 960.f, 540.f};
229 EXPECT_THAT(calculateOutputSourceCrop(), expected);
230 }
231
232 /*
233 * OutputLayer::calculateOutputDisplayFrame()
234 */
235
236 struct OutputLayerDisplayFrameTest : public OutputLayerTest {
OutputLayerDisplayFrameTestandroid::compositionengine::__anon834e7eb10111::OutputLayerDisplayFrameTest237 OutputLayerDisplayFrameTest() {
238 // Set reasonable default values for a simple case. Each test will
239 // set one specific value to something different.
240
241 mLayerFEState.transparentRegionHint = Region{};
242 mLayerFEState.geomLayerTransform = ui::Transform{TR_IDENT};
243 mLayerFEState.geomBufferSize = Rect{0, 0, 1920, 1080};
244 mLayerFEState.geomBufferUsesDisplayInverseTransform = false;
245 mLayerFEState.geomCrop = Rect{0, 0, 1920, 1080};
246 mLayerFEState.geomLayerBounds = FloatRect{0.f, 0.f, 1920.f, 1080.f};
247
248 mOutputState.viewport = Rect{0, 0, 1920, 1080};
249 mOutputState.transform = ui::Transform{TR_IDENT};
250 }
251
calculateOutputDisplayFrameandroid::compositionengine::__anon834e7eb10111::OutputLayerDisplayFrameTest252 Rect calculateOutputDisplayFrame() {
253 mLayerFEState.geomInverseLayerTransform = mLayerFEState.geomLayerTransform.inverse();
254
255 return mOutputLayer.calculateOutputDisplayFrame();
256 }
257 };
258
TEST_F(OutputLayerDisplayFrameTest,correctForSimpleDefaultCase)259 TEST_F(OutputLayerDisplayFrameTest, correctForSimpleDefaultCase) {
260 const Rect expected{0, 0, 1920, 1080};
261 EXPECT_THAT(calculateOutputDisplayFrame(), expected);
262 }
263
TEST_F(OutputLayerDisplayFrameTest,fullActiveTransparentRegionReturnsEmptyFrame)264 TEST_F(OutputLayerDisplayFrameTest, fullActiveTransparentRegionReturnsEmptyFrame) {
265 mLayerFEState.transparentRegionHint = Region{Rect{0, 0, 1920, 1080}};
266 const Rect expected{0, 0, 0, 0};
267 EXPECT_THAT(calculateOutputDisplayFrame(), expected);
268 }
269
TEST_F(OutputLayerDisplayFrameTest,cropAffectsDisplayFrame)270 TEST_F(OutputLayerDisplayFrameTest, cropAffectsDisplayFrame) {
271 mLayerFEState.geomCrop = Rect{100, 200, 300, 500};
272 const Rect expected{100, 200, 300, 500};
273 EXPECT_THAT(calculateOutputDisplayFrame(), expected);
274 }
275
TEST_F(OutputLayerDisplayFrameTest,cropAffectsDisplayFrameRotated)276 TEST_F(OutputLayerDisplayFrameTest, cropAffectsDisplayFrameRotated) {
277 mLayerFEState.geomCrop = Rect{100, 200, 300, 500};
278 mLayerFEState.geomLayerTransform.set(HAL_TRANSFORM_ROT_90, 1920, 1080);
279 const Rect expected{1420, 100, 1720, 300};
280 EXPECT_THAT(calculateOutputDisplayFrame(), expected);
281 }
282
TEST_F(OutputLayerDisplayFrameTest,emptyGeomCropIsNotUsedToComputeFrame)283 TEST_F(OutputLayerDisplayFrameTest, emptyGeomCropIsNotUsedToComputeFrame) {
284 mLayerFEState.geomCrop = Rect{};
285 const Rect expected{0, 0, 1920, 1080};
286 EXPECT_THAT(calculateOutputDisplayFrame(), expected);
287 }
288
TEST_F(OutputLayerDisplayFrameTest,geomLayerBoundsAffectsFrame)289 TEST_F(OutputLayerDisplayFrameTest, geomLayerBoundsAffectsFrame) {
290 mLayerFEState.geomLayerBounds = FloatRect{0.f, 0.f, 960.f, 540.f};
291 const Rect expected{0, 0, 960, 540};
292 EXPECT_THAT(calculateOutputDisplayFrame(), expected);
293 }
294
TEST_F(OutputLayerDisplayFrameTest,viewportAffectsFrame)295 TEST_F(OutputLayerDisplayFrameTest, viewportAffectsFrame) {
296 mOutputState.viewport = Rect{0, 0, 960, 540};
297 const Rect expected{0, 0, 960, 540};
298 EXPECT_THAT(calculateOutputDisplayFrame(), expected);
299 }
300
TEST_F(OutputLayerDisplayFrameTest,outputTransformAffectsDisplayFrame)301 TEST_F(OutputLayerDisplayFrameTest, outputTransformAffectsDisplayFrame) {
302 mOutputState.transform = ui::Transform{HAL_TRANSFORM_ROT_90};
303 const Rect expected{-1080, 0, 0, 1920};
304 EXPECT_THAT(calculateOutputDisplayFrame(), expected);
305 }
306
307 /*
308 * OutputLayer::calculateOutputRelativeBufferTransform()
309 */
310
TEST_F(OutputLayerTest,calculateOutputRelativeBufferTransformTestsNeeded)311 TEST_F(OutputLayerTest, calculateOutputRelativeBufferTransformTestsNeeded) {
312 mLayerFEState.geomBufferUsesDisplayInverseTransform = false;
313
314 struct Entry {
315 uint32_t layer;
316 uint32_t buffer;
317 uint32_t display;
318 uint32_t expected;
319 };
320 // Not an exhaustive list of cases, but hopefully enough.
321 const std::array<Entry, 24> testData = {
322 // clang-format off
323 // layer buffer display expected
324 /* 0 */ Entry{TR_IDENT, TR_IDENT, TR_IDENT, TR_IDENT},
325 /* 1 */ Entry{TR_IDENT, TR_IDENT, TR_ROT_90, TR_ROT_90},
326 /* 2 */ Entry{TR_IDENT, TR_IDENT, TR_ROT_180, TR_ROT_180},
327 /* 3 */ Entry{TR_IDENT, TR_IDENT, TR_ROT_270, TR_ROT_270},
328
329 /* 4 */ Entry{TR_IDENT, TR_FLP_H, TR_IDENT, TR_FLP_H ^ TR_IDENT},
330 /* 5 */ Entry{TR_IDENT, TR_FLP_H, TR_ROT_90, TR_FLP_H ^ TR_ROT_90},
331 /* 6 */ Entry{TR_IDENT, TR_FLP_H, TR_ROT_180, TR_FLP_H ^ TR_ROT_180},
332 /* 7 */ Entry{TR_IDENT, TR_FLP_H, TR_ROT_270, TR_FLP_H ^ TR_ROT_270},
333
334 /* 8 */ Entry{TR_IDENT, TR_FLP_V, TR_IDENT, TR_FLP_V},
335 /* 9 */ Entry{TR_IDENT, TR_ROT_90, TR_ROT_90, TR_ROT_180},
336 /* 10 */ Entry{TR_IDENT, TR_ROT_180, TR_ROT_180, TR_IDENT},
337 /* 11 */ Entry{TR_IDENT, TR_ROT_270, TR_ROT_270, TR_ROT_180},
338
339 /* 12 */ Entry{TR_ROT_90, TR_IDENT, TR_IDENT, TR_IDENT ^ TR_ROT_90},
340 /* 13 */ Entry{TR_ROT_90, TR_FLP_H, TR_ROT_90, TR_FLP_H ^ TR_ROT_180},
341 /* 14 */ Entry{TR_ROT_90, TR_IDENT, TR_ROT_180, TR_IDENT ^ TR_ROT_270},
342 /* 15 */ Entry{TR_ROT_90, TR_FLP_H, TR_ROT_270, TR_FLP_H ^ TR_IDENT},
343
344 /* 16 */ Entry{TR_ROT_180, TR_FLP_H, TR_IDENT, TR_FLP_H ^ TR_ROT_180},
345 /* 17 */ Entry{TR_ROT_180, TR_IDENT, TR_ROT_90, TR_IDENT ^ TR_ROT_270},
346 /* 18 */ Entry{TR_ROT_180, TR_FLP_H, TR_ROT_180, TR_FLP_H ^ TR_IDENT},
347 /* 19 */ Entry{TR_ROT_180, TR_IDENT, TR_ROT_270, TR_IDENT ^ TR_ROT_90},
348
349 /* 20 */ Entry{TR_ROT_270, TR_IDENT, TR_IDENT, TR_IDENT ^ TR_ROT_270},
350 /* 21 */ Entry{TR_ROT_270, TR_FLP_H, TR_ROT_90, TR_FLP_H ^ TR_IDENT},
351 /* 22 */ Entry{TR_ROT_270, TR_FLP_H, TR_ROT_180, TR_FLP_H ^ TR_ROT_90},
352 /* 23 */ Entry{TR_ROT_270, TR_IDENT, TR_ROT_270, TR_IDENT ^ TR_ROT_180},
353 // clang-format on
354 };
355
356 for (size_t i = 0; i < testData.size(); i++) {
357 const auto& entry = testData[i];
358
359 mLayerFEState.geomLayerTransform.set(entry.layer, 1920, 1080);
360 mLayerFEState.geomBufferTransform = entry.buffer;
361 mOutputState.orientation = entry.display;
362 mOutputState.transform = ui::Transform{entry.display};
363
364 const auto actual = mOutputLayer.calculateOutputRelativeBufferTransform(entry.display);
365 EXPECT_EQ(entry.expected, actual) << "entry " << i;
366 }
367 }
368
TEST_F(OutputLayerTest,calculateOutputRelativeBufferTransformTestWithOfBufferUsesDisplayInverseTransform)369 TEST_F(OutputLayerTest,
370 calculateOutputRelativeBufferTransformTestWithOfBufferUsesDisplayInverseTransform) {
371 mLayerFEState.geomBufferUsesDisplayInverseTransform = true;
372
373 struct Entry {
374 uint32_t layer; /* shouldn't affect the result, so we just use arbitrary values */
375 uint32_t buffer;
376 uint32_t display;
377 uint32_t internal;
378 uint32_t expected;
379 };
380 const std::array<Entry, 64> testData = {
381 // clang-format off
382 // layer buffer display internal expected
383 Entry{TR_IDENT, TR_IDENT, TR_IDENT, TR_IDENT, TR_IDENT},
384 Entry{TR_IDENT, TR_IDENT, TR_IDENT, TR_ROT_90, TR_ROT_270},
385 Entry{TR_IDENT, TR_IDENT, TR_IDENT, TR_ROT_180, TR_ROT_180},
386 Entry{TR_IDENT, TR_IDENT, TR_IDENT, TR_ROT_270, TR_ROT_90},
387
388 Entry{TR_IDENT, TR_IDENT, TR_ROT_90, TR_IDENT, TR_ROT_90},
389 Entry{TR_ROT_90, TR_IDENT, TR_ROT_90, TR_ROT_90, TR_IDENT},
390 Entry{TR_ROT_180, TR_IDENT, TR_ROT_90, TR_ROT_180, TR_ROT_270},
391 Entry{TR_ROT_90, TR_IDENT, TR_ROT_90, TR_ROT_270, TR_ROT_180},
392
393 Entry{TR_ROT_180, TR_IDENT, TR_ROT_180, TR_IDENT, TR_ROT_180},
394 Entry{TR_ROT_90, TR_IDENT, TR_ROT_180, TR_ROT_90, TR_ROT_90},
395 Entry{TR_ROT_180, TR_IDENT, TR_ROT_180, TR_ROT_180, TR_IDENT},
396 Entry{TR_ROT_270, TR_IDENT, TR_ROT_180, TR_ROT_270, TR_ROT_270},
397
398 Entry{TR_ROT_270, TR_IDENT, TR_ROT_270, TR_IDENT, TR_ROT_270},
399 Entry{TR_ROT_270, TR_IDENT, TR_ROT_270, TR_ROT_90, TR_ROT_180},
400 Entry{TR_ROT_180, TR_IDENT, TR_ROT_270, TR_ROT_180, TR_ROT_90},
401 Entry{TR_IDENT, TR_IDENT, TR_ROT_270, TR_ROT_270, TR_IDENT},
402
403 // layer buffer display internal expected
404 Entry{TR_IDENT, TR_ROT_90, TR_IDENT, TR_IDENT, TR_ROT_90},
405 Entry{TR_ROT_90, TR_ROT_90, TR_IDENT, TR_ROT_90, TR_IDENT},
406 Entry{TR_ROT_180, TR_ROT_90, TR_IDENT, TR_ROT_180, TR_ROT_270},
407 Entry{TR_ROT_270, TR_ROT_90, TR_IDENT, TR_ROT_270, TR_ROT_180},
408
409 Entry{TR_ROT_90, TR_ROT_90, TR_ROT_90, TR_IDENT, TR_ROT_180},
410 Entry{TR_ROT_90, TR_ROT_90, TR_ROT_90, TR_ROT_90, TR_ROT_90},
411 Entry{TR_ROT_90, TR_ROT_90, TR_ROT_90, TR_ROT_180, TR_IDENT},
412 Entry{TR_ROT_270, TR_ROT_90, TR_ROT_90, TR_ROT_270, TR_ROT_270},
413
414 Entry{TR_IDENT, TR_ROT_90, TR_ROT_180, TR_IDENT, TR_ROT_270},
415 Entry{TR_ROT_90, TR_ROT_90, TR_ROT_180, TR_ROT_90, TR_ROT_180},
416 Entry{TR_ROT_180, TR_ROT_90, TR_ROT_180, TR_ROT_180, TR_ROT_90},
417 Entry{TR_ROT_90, TR_ROT_90, TR_ROT_180, TR_ROT_270, TR_IDENT},
418
419 Entry{TR_IDENT, TR_ROT_90, TR_ROT_270, TR_IDENT, TR_IDENT},
420 Entry{TR_ROT_270, TR_ROT_90, TR_ROT_270, TR_ROT_90, TR_ROT_270},
421 Entry{TR_ROT_180, TR_ROT_90, TR_ROT_270, TR_ROT_180, TR_ROT_180},
422 Entry{TR_ROT_270, TR_ROT_90, TR_ROT_270, TR_ROT_270, TR_ROT_90},
423
424 // layer buffer display internal expected
425 Entry{TR_IDENT, TR_ROT_180, TR_IDENT, TR_IDENT, TR_ROT_180},
426 Entry{TR_IDENT, TR_ROT_180, TR_IDENT, TR_ROT_90, TR_ROT_90},
427 Entry{TR_ROT_180, TR_ROT_180, TR_IDENT, TR_ROT_180, TR_IDENT},
428 Entry{TR_ROT_270, TR_ROT_180, TR_IDENT, TR_ROT_270, TR_ROT_270},
429
430 Entry{TR_IDENT, TR_ROT_180, TR_ROT_90, TR_IDENT, TR_ROT_270},
431 Entry{TR_ROT_90, TR_ROT_180, TR_ROT_90, TR_ROT_90, TR_ROT_180},
432 Entry{TR_ROT_180, TR_ROT_180, TR_ROT_90, TR_ROT_180, TR_ROT_90},
433 Entry{TR_ROT_180, TR_ROT_180, TR_ROT_90, TR_ROT_270, TR_IDENT},
434
435 Entry{TR_IDENT, TR_ROT_180, TR_ROT_180, TR_IDENT, TR_IDENT},
436 Entry{TR_ROT_180, TR_ROT_180, TR_ROT_180, TR_ROT_90, TR_ROT_270},
437 Entry{TR_ROT_180, TR_ROT_180, TR_ROT_180, TR_ROT_180, TR_ROT_180},
438 Entry{TR_ROT_270, TR_ROT_180, TR_ROT_180, TR_ROT_270, TR_ROT_90},
439
440 Entry{TR_ROT_270, TR_ROT_180, TR_ROT_270, TR_IDENT, TR_ROT_90},
441 Entry{TR_ROT_180, TR_ROT_180, TR_ROT_270, TR_ROT_90, TR_IDENT},
442 Entry{TR_ROT_180, TR_ROT_180, TR_ROT_270, TR_ROT_180, TR_ROT_270},
443 Entry{TR_ROT_270, TR_ROT_180, TR_ROT_270, TR_ROT_270, TR_ROT_180},
444
445 // layer buffer display internal expected
446 Entry{TR_IDENT, TR_ROT_270, TR_IDENT, TR_IDENT, TR_ROT_270},
447 Entry{TR_ROT_90, TR_ROT_270, TR_IDENT, TR_ROT_90, TR_ROT_180},
448 Entry{TR_ROT_270, TR_ROT_270, TR_IDENT, TR_ROT_180, TR_ROT_90},
449 Entry{TR_IDENT, TR_ROT_270, TR_IDENT, TR_ROT_270, TR_IDENT},
450
451 Entry{TR_ROT_270, TR_ROT_270, TR_ROT_90, TR_IDENT, TR_IDENT},
452 Entry{TR_ROT_90, TR_ROT_270, TR_ROT_90, TR_ROT_90, TR_ROT_270},
453 Entry{TR_ROT_180, TR_ROT_270, TR_ROT_90, TR_ROT_180, TR_ROT_180},
454 Entry{TR_ROT_90, TR_ROT_270, TR_ROT_90, TR_ROT_270, TR_ROT_90},
455
456 Entry{TR_IDENT, TR_ROT_270, TR_ROT_180, TR_IDENT, TR_ROT_90},
457 Entry{TR_ROT_270, TR_ROT_270, TR_ROT_180, TR_ROT_90, TR_IDENT},
458 Entry{TR_ROT_180, TR_ROT_270, TR_ROT_180, TR_ROT_180, TR_ROT_270},
459 Entry{TR_ROT_270, TR_ROT_270, TR_ROT_180, TR_ROT_270, TR_ROT_180},
460
461 Entry{TR_IDENT, TR_ROT_270, TR_ROT_270, TR_IDENT, TR_ROT_180},
462 Entry{TR_ROT_90, TR_ROT_270, TR_ROT_270, TR_ROT_90, TR_ROT_90},
463 Entry{TR_ROT_270, TR_ROT_270, TR_ROT_270, TR_ROT_180, TR_IDENT},
464 Entry{TR_ROT_270, TR_ROT_270, TR_ROT_270, TR_ROT_270, TR_ROT_270},
465 // clang-format on
466 };
467
468 for (size_t i = 0; i < testData.size(); i++) {
469 const auto& entry = testData[i];
470
471 mLayerFEState.geomLayerTransform.set(entry.layer, 1920, 1080);
472 mLayerFEState.geomBufferTransform = entry.buffer;
473 mOutputState.orientation = entry.display;
474 mOutputState.transform = ui::Transform{entry.display};
475
476 const auto actual = mOutputLayer.calculateOutputRelativeBufferTransform(entry.internal);
477 EXPECT_EQ(entry.expected, actual) << "entry " << i;
478 }
479 }
480
481 /*
482 * OutputLayer::updateCompositionState()
483 */
484
485 struct OutputLayerPartialMockForUpdateCompositionState : public impl::OutputLayer {
OutputLayerPartialMockForUpdateCompositionStateandroid::compositionengine::__anon834e7eb10111::OutputLayerPartialMockForUpdateCompositionState486 OutputLayerPartialMockForUpdateCompositionState(const compositionengine::Output& output,
487 sp<compositionengine::LayerFE> layerFE)
488 : mOutput(output), mLayerFE(layerFE) {}
489 // Mock everything called by updateCompositionState to simplify testing it.
490 MOCK_CONST_METHOD0(calculateOutputSourceCrop, FloatRect());
491 MOCK_CONST_METHOD0(calculateOutputDisplayFrame, Rect());
492 MOCK_CONST_METHOD1(calculateOutputRelativeBufferTransform, uint32_t(uint32_t));
493
494 // compositionengine::OutputLayer overrides
getOutputandroid::compositionengine::__anon834e7eb10111::OutputLayerPartialMockForUpdateCompositionState495 const compositionengine::Output& getOutput() const override { return mOutput; }
getLayerFEandroid::compositionengine::__anon834e7eb10111::OutputLayerPartialMockForUpdateCompositionState496 compositionengine::LayerFE& getLayerFE() const override { return *mLayerFE; }
getStateandroid::compositionengine::__anon834e7eb10111::OutputLayerPartialMockForUpdateCompositionState497 const impl::OutputLayerCompositionState& getState() const override { return mState; }
editStateandroid::compositionengine::__anon834e7eb10111::OutputLayerPartialMockForUpdateCompositionState498 impl::OutputLayerCompositionState& editState() override { return mState; }
499
500 // These need implementations though are not expected to be called.
501 MOCK_CONST_METHOD1(dumpState, void(std::string&));
502
503 const compositionengine::Output& mOutput;
504 sp<compositionengine::LayerFE> mLayerFE;
505 impl::OutputLayerCompositionState mState;
506 };
507
508 struct OutputLayerUpdateCompositionStateTest : public OutputLayerTest {
509 public:
OutputLayerUpdateCompositionStateTestandroid::compositionengine::__anon834e7eb10111::OutputLayerUpdateCompositionStateTest510 OutputLayerUpdateCompositionStateTest() {
511 EXPECT_CALL(mOutput, getState()).WillRepeatedly(ReturnRef(mOutputState));
512 EXPECT_CALL(mOutput, getDisplayColorProfile())
513 .WillRepeatedly(Return(&mDisplayColorProfile));
514 EXPECT_CALL(mDisplayColorProfile, isDataspaceSupported(_)).WillRepeatedly(Return(true));
515 }
516
517 ~OutputLayerUpdateCompositionStateTest() = default;
518
setupGeometryChildCallValuesandroid::compositionengine::__anon834e7eb10111::OutputLayerUpdateCompositionStateTest519 void setupGeometryChildCallValues(ui::Transform::RotationFlags internalDisplayRotationFlags) {
520 EXPECT_CALL(mOutputLayer, calculateOutputSourceCrop()).WillOnce(Return(kSourceCrop));
521 EXPECT_CALL(mOutputLayer, calculateOutputDisplayFrame()).WillOnce(Return(kDisplayFrame));
522 EXPECT_CALL(mOutputLayer,
523 calculateOutputRelativeBufferTransform(internalDisplayRotationFlags))
524 .WillOnce(Return(mBufferTransform));
525 }
526
validateComputedGeometryStateandroid::compositionengine::__anon834e7eb10111::OutputLayerUpdateCompositionStateTest527 void validateComputedGeometryState() {
528 const auto& state = mOutputLayer.getState();
529 EXPECT_EQ(kSourceCrop, state.sourceCrop);
530 EXPECT_EQ(kDisplayFrame, state.displayFrame);
531 EXPECT_EQ(static_cast<Hwc2::Transform>(mBufferTransform), state.bufferTransform);
532 }
533
534 const FloatRect kSourceCrop{1.f, 2.f, 3.f, 4.f};
535 const Rect kDisplayFrame{11, 12, 13, 14};
536 uint32_t mBufferTransform{21};
537
538 using OutputLayer = OutputLayerPartialMockForUpdateCompositionState;
539 StrictMock<OutputLayer> mOutputLayer{mOutput, mLayerFE};
540 StrictMock<mock::DisplayColorProfile> mDisplayColorProfile;
541 };
542
TEST_F(OutputLayerUpdateCompositionStateTest,doesNothingIfNoFECompositionState)543 TEST_F(OutputLayerUpdateCompositionStateTest, doesNothingIfNoFECompositionState) {
544 EXPECT_CALL(*mLayerFE, getCompositionState()).WillOnce(Return(nullptr));
545
546 mOutputLayer.updateCompositionState(true, false, ui::Transform::RotationFlags::ROT_90);
547 }
548
TEST_F(OutputLayerUpdateCompositionStateTest,setsStateNormally)549 TEST_F(OutputLayerUpdateCompositionStateTest, setsStateNormally) {
550 mLayerFEState.isSecure = true;
551 mOutputState.isSecure = true;
552 mOutputLayer.editState().forceClientComposition = true;
553
554 setupGeometryChildCallValues(ui::Transform::RotationFlags::ROT_90);
555
556 mOutputLayer.updateCompositionState(true, false, ui::Transform::RotationFlags::ROT_90);
557
558 validateComputedGeometryState();
559
560 EXPECT_EQ(false, mOutputLayer.getState().forceClientComposition);
561 }
562
TEST_F(OutputLayerUpdateCompositionStateTest,alsoSetsForceCompositionIfSecureLayerOnNonsecureOutput)563 TEST_F(OutputLayerUpdateCompositionStateTest,
564 alsoSetsForceCompositionIfSecureLayerOnNonsecureOutput) {
565 mLayerFEState.isSecure = true;
566 mOutputState.isSecure = false;
567
568 setupGeometryChildCallValues(ui::Transform::RotationFlags::ROT_0);
569
570 mOutputLayer.updateCompositionState(true, false, ui::Transform::RotationFlags::ROT_0);
571
572 validateComputedGeometryState();
573
574 EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
575 }
576
TEST_F(OutputLayerUpdateCompositionStateTest,alsoSetsForceCompositionIfUnsupportedBufferTransform)577 TEST_F(OutputLayerUpdateCompositionStateTest,
578 alsoSetsForceCompositionIfUnsupportedBufferTransform) {
579 mLayerFEState.isSecure = true;
580 mOutputState.isSecure = true;
581
582 mBufferTransform = ui::Transform::ROT_INVALID;
583
584 setupGeometryChildCallValues(ui::Transform::RotationFlags::ROT_0);
585
586 mOutputLayer.updateCompositionState(true, false, ui::Transform::RotationFlags::ROT_0);
587
588 validateComputedGeometryState();
589
590 EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
591 }
592
TEST_F(OutputLayerUpdateCompositionStateTest,setsOutputLayerColorspaceCorrectly)593 TEST_F(OutputLayerUpdateCompositionStateTest, setsOutputLayerColorspaceCorrectly) {
594 mLayerFEState.dataspace = ui::Dataspace::DISPLAY_P3;
595 mOutputState.targetDataspace = ui::Dataspace::V0_SCRGB;
596
597 // If the layer is not colorspace agnostic, the output layer dataspace
598 // should use the layers requested colorspace.
599 mLayerFEState.isColorspaceAgnostic = false;
600
601 mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
602
603 EXPECT_EQ(ui::Dataspace::DISPLAY_P3, mOutputLayer.getState().dataspace);
604
605 // If the layer is colorspace agnostic, the output layer dataspace
606 // should use the colorspace chosen for the whole output.
607 mLayerFEState.isColorspaceAgnostic = true;
608
609 mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
610
611 EXPECT_EQ(ui::Dataspace::V0_SCRGB, mOutputLayer.getState().dataspace);
612 }
613
TEST_F(OutputLayerUpdateCompositionStateTest,doesNotRecomputeGeometryIfNotRequested)614 TEST_F(OutputLayerUpdateCompositionStateTest, doesNotRecomputeGeometryIfNotRequested) {
615 mOutputLayer.editState().forceClientComposition = false;
616
617 mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
618
619 EXPECT_EQ(false, mOutputLayer.getState().forceClientComposition);
620 }
621
TEST_F(OutputLayerUpdateCompositionStateTest,doesNotClearForceClientCompositionIfNotDoingGeometry)622 TEST_F(OutputLayerUpdateCompositionStateTest,
623 doesNotClearForceClientCompositionIfNotDoingGeometry) {
624 mOutputLayer.editState().forceClientComposition = true;
625
626 mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
627
628 EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
629 }
630
TEST_F(OutputLayerUpdateCompositionStateTest,clientCompositionForcedFromFrontEndFlagAtAnyTime)631 TEST_F(OutputLayerUpdateCompositionStateTest, clientCompositionForcedFromFrontEndFlagAtAnyTime) {
632 mLayerFEState.forceClientComposition = true;
633 mOutputLayer.editState().forceClientComposition = false;
634
635 mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
636
637 EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
638 }
639
TEST_F(OutputLayerUpdateCompositionStateTest,clientCompositionForcedFromUnsupportedDataspaceAtAnyTime)640 TEST_F(OutputLayerUpdateCompositionStateTest,
641 clientCompositionForcedFromUnsupportedDataspaceAtAnyTime) {
642 mOutputLayer.editState().forceClientComposition = false;
643 EXPECT_CALL(mDisplayColorProfile, isDataspaceSupported(_)).WillRepeatedly(Return(false));
644
645 mOutputLayer.updateCompositionState(false, false, ui::Transform::RotationFlags::ROT_0);
646
647 EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
648 }
649
TEST_F(OutputLayerUpdateCompositionStateTest,clientCompositionForcedFromArgumentFlag)650 TEST_F(OutputLayerUpdateCompositionStateTest, clientCompositionForcedFromArgumentFlag) {
651 mLayerFEState.forceClientComposition = false;
652 mOutputLayer.editState().forceClientComposition = false;
653
654 mOutputLayer.updateCompositionState(false, true, ui::Transform::RotationFlags::ROT_0);
655
656 EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
657
658 mOutputLayer.editState().forceClientComposition = false;
659
660 setupGeometryChildCallValues(ui::Transform::RotationFlags::ROT_0);
661
662 mOutputLayer.updateCompositionState(true, true, ui::Transform::RotationFlags::ROT_0);
663
664 EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
665 }
666
667 /*
668 * OutputLayer::writeStateToHWC()
669 */
670
671 struct OutputLayerWriteStateToHWCTest : public OutputLayerTest {
672 static constexpr hal::Error kError = hal::Error::UNSUPPORTED;
673 static constexpr FloatRect kSourceCrop{11.f, 12.f, 13.f, 14.f};
674 static constexpr uint32_t kZOrder = 21u;
675 static constexpr Hwc2::Transform kBufferTransform = static_cast<Hwc2::Transform>(31);
676 static constexpr Hwc2::IComposerClient::BlendMode kBlendMode =
677 static_cast<Hwc2::IComposerClient::BlendMode>(41);
678 static constexpr float kAlpha = 51.f;
679 static constexpr uint32_t kType = 61u;
680 static constexpr uint32_t kAppId = 62u;
681 static constexpr ui::Dataspace kDataspace = static_cast<ui::Dataspace>(71);
682 static constexpr int kSupportedPerFrameMetadata = 101;
683 static constexpr int kExpectedHwcSlot = 0;
684 static constexpr bool kLayerGenericMetadata1Mandatory = true;
685 static constexpr bool kLayerGenericMetadata2Mandatory = true;
686
687 static const half4 kColor;
688 static const Rect kDisplayFrame;
689 static const Region kOutputSpaceVisibleRegion;
690 static const mat4 kColorTransform;
691 static const Region kSurfaceDamage;
692 static const HdrMetadata kHdrMetadata;
693 static native_handle_t* kSidebandStreamHandle;
694 static const sp<GraphicBuffer> kBuffer;
695 static const sp<Fence> kFence;
696 static const std::string kLayerGenericMetadata1Key;
697 static const std::vector<uint8_t> kLayerGenericMetadata1Value;
698 static const std::string kLayerGenericMetadata2Key;
699 static const std::vector<uint8_t> kLayerGenericMetadata2Value;
700
OutputLayerWriteStateToHWCTestandroid::compositionengine::__anon834e7eb10111::OutputLayerWriteStateToHWCTest701 OutputLayerWriteStateToHWCTest() {
702 auto& outputLayerState = mOutputLayer.editState();
703 outputLayerState.hwc = impl::OutputLayerCompositionState::Hwc(mHwcLayer);
704
705 outputLayerState.displayFrame = kDisplayFrame;
706 outputLayerState.sourceCrop = kSourceCrop;
707 outputLayerState.z = kZOrder;
708 outputLayerState.bufferTransform = static_cast<Hwc2::Transform>(kBufferTransform);
709 outputLayerState.outputSpaceVisibleRegion = kOutputSpaceVisibleRegion;
710 outputLayerState.dataspace = kDataspace;
711
712 mLayerFEState.blendMode = kBlendMode;
713 mLayerFEState.alpha = kAlpha;
714 mLayerFEState.type = kType;
715 mLayerFEState.appId = kAppId;
716 mLayerFEState.colorTransform = kColorTransform;
717 mLayerFEState.color = kColor;
718 mLayerFEState.surfaceDamage = kSurfaceDamage;
719 mLayerFEState.hdrMetadata = kHdrMetadata;
720 mLayerFEState.sidebandStream = NativeHandle::create(kSidebandStreamHandle, false);
721 mLayerFEState.buffer = kBuffer;
722 mLayerFEState.bufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
723 mLayerFEState.acquireFence = kFence;
724
725 EXPECT_CALL(mOutput, getDisplayColorProfile())
726 .WillRepeatedly(Return(&mDisplayColorProfile));
727 EXPECT_CALL(mDisplayColorProfile, getSupportedPerFrameMetadata())
728 .WillRepeatedly(Return(kSupportedPerFrameMetadata));
729 }
730
731 // Some tests may need to simulate unsupported HWC calls
732 enum class SimulateUnsupported { None, ColorTransform };
733
includeGenericLayerMetadataInStateandroid::compositionengine::__anon834e7eb10111::OutputLayerWriteStateToHWCTest734 void includeGenericLayerMetadataInState() {
735 mLayerFEState.metadata[kLayerGenericMetadata1Key] = {kLayerGenericMetadata1Mandatory,
736 kLayerGenericMetadata1Value};
737 mLayerFEState.metadata[kLayerGenericMetadata2Key] = {kLayerGenericMetadata2Mandatory,
738 kLayerGenericMetadata2Value};
739 }
740
expectGeometryCommonCallsandroid::compositionengine::__anon834e7eb10111::OutputLayerWriteStateToHWCTest741 void expectGeometryCommonCalls() {
742 EXPECT_CALL(*mHwcLayer, setDisplayFrame(kDisplayFrame)).WillOnce(Return(kError));
743 EXPECT_CALL(*mHwcLayer, setSourceCrop(kSourceCrop)).WillOnce(Return(kError));
744 EXPECT_CALL(*mHwcLayer, setZOrder(kZOrder)).WillOnce(Return(kError));
745 EXPECT_CALL(*mHwcLayer, setTransform(kBufferTransform)).WillOnce(Return(kError));
746
747 EXPECT_CALL(*mHwcLayer, setBlendMode(kBlendMode)).WillOnce(Return(kError));
748 EXPECT_CALL(*mHwcLayer, setPlaneAlpha(kAlpha)).WillOnce(Return(kError));
749 EXPECT_CALL(*mHwcLayer, setInfo(kType, kAppId)).WillOnce(Return(kError));
750 }
751
expectPerFrameCommonCallsandroid::compositionengine::__anon834e7eb10111::OutputLayerWriteStateToHWCTest752 void expectPerFrameCommonCalls(SimulateUnsupported unsupported = SimulateUnsupported::None) {
753 EXPECT_CALL(*mHwcLayer, setVisibleRegion(RegionEq(kOutputSpaceVisibleRegion)))
754 .WillOnce(Return(kError));
755 EXPECT_CALL(*mHwcLayer, setDataspace(kDataspace)).WillOnce(Return(kError));
756 EXPECT_CALL(*mHwcLayer, setColorTransform(kColorTransform))
757 .WillOnce(Return(unsupported == SimulateUnsupported::ColorTransform
758 ? hal::Error::UNSUPPORTED
759 : hal::Error::NONE));
760 EXPECT_CALL(*mHwcLayer, setSurfaceDamage(RegionEq(kSurfaceDamage)))
761 .WillOnce(Return(kError));
762 }
763
expectSetCompositionTypeCallandroid::compositionengine::__anon834e7eb10111::OutputLayerWriteStateToHWCTest764 void expectSetCompositionTypeCall(Hwc2::IComposerClient::Composition compositionType) {
765 EXPECT_CALL(*mHwcLayer, setCompositionType(compositionType)).WillOnce(Return(kError));
766 }
767
expectNoSetCompositionTypeCallandroid::compositionengine::__anon834e7eb10111::OutputLayerWriteStateToHWCTest768 void expectNoSetCompositionTypeCall() {
769 EXPECT_CALL(*mHwcLayer, setCompositionType(_)).Times(0);
770 }
771
expectSetColorCallandroid::compositionengine::__anon834e7eb10111::OutputLayerWriteStateToHWCTest772 void expectSetColorCall() {
773 const hal::Color color = {static_cast<uint8_t>(std::round(kColor.r * 255)),
774 static_cast<uint8_t>(std::round(kColor.g * 255)),
775 static_cast<uint8_t>(std::round(kColor.b * 255)), 255};
776
777 EXPECT_CALL(*mHwcLayer, setColor(ColorEq(color))).WillOnce(Return(kError));
778 }
779
expectSetSidebandHandleCallandroid::compositionengine::__anon834e7eb10111::OutputLayerWriteStateToHWCTest780 void expectSetSidebandHandleCall() {
781 EXPECT_CALL(*mHwcLayer, setSidebandStream(kSidebandStreamHandle));
782 }
783
expectSetHdrMetadataAndBufferCallsandroid::compositionengine::__anon834e7eb10111::OutputLayerWriteStateToHWCTest784 void expectSetHdrMetadataAndBufferCalls() {
785 EXPECT_CALL(*mHwcLayer, setPerFrameMetadata(kSupportedPerFrameMetadata, kHdrMetadata));
786 EXPECT_CALL(*mHwcLayer, setBuffer(kExpectedHwcSlot, kBuffer, kFence));
787 }
788
expectGenericLayerMetadataCallsandroid::compositionengine::__anon834e7eb10111::OutputLayerWriteStateToHWCTest789 void expectGenericLayerMetadataCalls() {
790 // Note: Can be in any order.
791 EXPECT_CALL(*mHwcLayer,
792 setLayerGenericMetadata(kLayerGenericMetadata1Key,
793 kLayerGenericMetadata1Mandatory,
794 kLayerGenericMetadata1Value));
795 EXPECT_CALL(*mHwcLayer,
796 setLayerGenericMetadata(kLayerGenericMetadata2Key,
797 kLayerGenericMetadata2Mandatory,
798 kLayerGenericMetadata2Value));
799 }
800
801 std::shared_ptr<HWC2::mock::Layer> mHwcLayer{std::make_shared<StrictMock<HWC2::mock::Layer>>()};
802 StrictMock<mock::DisplayColorProfile> mDisplayColorProfile;
803 };
804
805 const half4 OutputLayerWriteStateToHWCTest::kColor{81.f / 255.f, 82.f / 255.f, 83.f / 255.f,
806 84.f / 255.f};
807 const Rect OutputLayerWriteStateToHWCTest::kDisplayFrame{1001, 1002, 1003, 10044};
808 const Region OutputLayerWriteStateToHWCTest::kOutputSpaceVisibleRegion{
809 Rect{1005, 1006, 1007, 1008}};
810 const mat4 OutputLayerWriteStateToHWCTest::kColorTransform{
811 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016,
812 1017, 1018, 1019, 1020, 1021, 1022, 1023, 1024,
813 };
814 const Region OutputLayerWriteStateToHWCTest::kSurfaceDamage{Rect{1025, 1026, 1027, 1028}};
815 const HdrMetadata OutputLayerWriteStateToHWCTest::kHdrMetadata{{/* LightFlattenable */}, 1029};
816 native_handle_t* OutputLayerWriteStateToHWCTest::kSidebandStreamHandle =
817 reinterpret_cast<native_handle_t*>(1031);
818 const sp<GraphicBuffer> OutputLayerWriteStateToHWCTest::kBuffer;
819 const sp<Fence> OutputLayerWriteStateToHWCTest::kFence;
820 const std::string OutputLayerWriteStateToHWCTest::kLayerGenericMetadata1Key =
821 "com.example.metadata.1";
822 const std::vector<uint8_t> OutputLayerWriteStateToHWCTest::kLayerGenericMetadata1Value{{1, 2, 3}};
823 const std::string OutputLayerWriteStateToHWCTest::kLayerGenericMetadata2Key =
824 "com.example.metadata.2";
825 const std::vector<uint8_t> OutputLayerWriteStateToHWCTest::kLayerGenericMetadata2Value{
826 {4, 5, 6, 7}};
827
TEST_F(OutputLayerWriteStateToHWCTest,doesNothingIfNoFECompositionState)828 TEST_F(OutputLayerWriteStateToHWCTest, doesNothingIfNoFECompositionState) {
829 EXPECT_CALL(*mLayerFE, getCompositionState()).WillOnce(Return(nullptr));
830
831 mOutputLayer.writeStateToHWC(true);
832 }
833
TEST_F(OutputLayerWriteStateToHWCTest,doesNothingIfNoHWCState)834 TEST_F(OutputLayerWriteStateToHWCTest, doesNothingIfNoHWCState) {
835 mOutputLayer.editState().hwc.reset();
836
837 mOutputLayer.writeStateToHWC(true);
838 }
839
TEST_F(OutputLayerWriteStateToHWCTest,doesNothingIfNoHWCLayer)840 TEST_F(OutputLayerWriteStateToHWCTest, doesNothingIfNoHWCLayer) {
841 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc(nullptr);
842
843 mOutputLayer.writeStateToHWC(true);
844 }
845
TEST_F(OutputLayerWriteStateToHWCTest,canSetAllState)846 TEST_F(OutputLayerWriteStateToHWCTest, canSetAllState) {
847 expectGeometryCommonCalls();
848 expectPerFrameCommonCalls();
849
850 expectNoSetCompositionTypeCall();
851
852 mOutputLayer.writeStateToHWC(true);
853 }
854
TEST_F(OutputLayerTest,displayInstallOrientationBufferTransformSetTo90)855 TEST_F(OutputLayerTest, displayInstallOrientationBufferTransformSetTo90) {
856 mLayerFEState.geomBufferUsesDisplayInverseTransform = false;
857 mLayerFEState.geomLayerTransform = ui::Transform{TR_IDENT};
858 // This test simulates a scenario where displayInstallOrientation is set to
859 // ROT_90. This only has an effect on the transform; orientation stays 0 (see
860 // DisplayDevice::setProjection).
861 mOutputState.orientation = TR_IDENT;
862 mOutputState.transform = ui::Transform{TR_ROT_90};
863 // Buffers are pre-rotated based on the transform hint (ROT_90); their
864 // geomBufferTransform is set to the inverse transform.
865 mLayerFEState.geomBufferTransform = TR_ROT_270;
866
867 EXPECT_EQ(TR_IDENT, mOutputLayer.calculateOutputRelativeBufferTransform(ui::Transform::ROT_90));
868 }
869
TEST_F(OutputLayerWriteStateToHWCTest,canSetPerFrameStateForSolidColor)870 TEST_F(OutputLayerWriteStateToHWCTest, canSetPerFrameStateForSolidColor) {
871 mLayerFEState.compositionType = Hwc2::IComposerClient::Composition::SOLID_COLOR;
872
873 expectPerFrameCommonCalls();
874
875 // Setting the composition type should happen before setting the color. We
876 // check this in this test only by setting up an testing::InSeqeuence
877 // instance before setting up the two expectations.
878 InSequence s;
879 expectSetCompositionTypeCall(Hwc2::IComposerClient::Composition::SOLID_COLOR);
880 expectSetColorCall();
881
882 mOutputLayer.writeStateToHWC(false);
883 }
884
TEST_F(OutputLayerWriteStateToHWCTest,canSetPerFrameStateForSideband)885 TEST_F(OutputLayerWriteStateToHWCTest, canSetPerFrameStateForSideband) {
886 mLayerFEState.compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
887
888 expectPerFrameCommonCalls();
889 expectSetSidebandHandleCall();
890 expectSetCompositionTypeCall(Hwc2::IComposerClient::Composition::SIDEBAND);
891
892 mOutputLayer.writeStateToHWC(false);
893 }
894
TEST_F(OutputLayerWriteStateToHWCTest,canSetPerFrameStateForCursor)895 TEST_F(OutputLayerWriteStateToHWCTest, canSetPerFrameStateForCursor) {
896 mLayerFEState.compositionType = Hwc2::IComposerClient::Composition::CURSOR;
897
898 expectPerFrameCommonCalls();
899 expectSetHdrMetadataAndBufferCalls();
900 expectSetCompositionTypeCall(Hwc2::IComposerClient::Composition::CURSOR);
901
902 mOutputLayer.writeStateToHWC(false);
903 }
904
TEST_F(OutputLayerWriteStateToHWCTest,canSetPerFrameStateForDevice)905 TEST_F(OutputLayerWriteStateToHWCTest, canSetPerFrameStateForDevice) {
906 mLayerFEState.compositionType = Hwc2::IComposerClient::Composition::DEVICE;
907
908 expectPerFrameCommonCalls();
909 expectSetHdrMetadataAndBufferCalls();
910 expectSetCompositionTypeCall(Hwc2::IComposerClient::Composition::DEVICE);
911
912 mOutputLayer.writeStateToHWC(false);
913 }
914
TEST_F(OutputLayerWriteStateToHWCTest,compositionTypeIsNotSetIfUnchanged)915 TEST_F(OutputLayerWriteStateToHWCTest, compositionTypeIsNotSetIfUnchanged) {
916 (*mOutputLayer.editState().hwc).hwcCompositionType =
917 Hwc2::IComposerClient::Composition::SOLID_COLOR;
918
919 mLayerFEState.compositionType = Hwc2::IComposerClient::Composition::SOLID_COLOR;
920
921 expectPerFrameCommonCalls();
922 expectSetColorCall();
923 expectNoSetCompositionTypeCall();
924
925 mOutputLayer.writeStateToHWC(false);
926 }
927
TEST_F(OutputLayerWriteStateToHWCTest,compositionTypeIsSetToClientIfColorTransformNotSupported)928 TEST_F(OutputLayerWriteStateToHWCTest, compositionTypeIsSetToClientIfColorTransformNotSupported) {
929 mLayerFEState.compositionType = Hwc2::IComposerClient::Composition::SOLID_COLOR;
930
931 expectPerFrameCommonCalls(SimulateUnsupported::ColorTransform);
932 expectSetColorCall();
933 expectSetCompositionTypeCall(Hwc2::IComposerClient::Composition::CLIENT);
934
935 mOutputLayer.writeStateToHWC(false);
936 }
937
TEST_F(OutputLayerWriteStateToHWCTest,compositionTypeIsSetToClientIfClientCompositionForced)938 TEST_F(OutputLayerWriteStateToHWCTest, compositionTypeIsSetToClientIfClientCompositionForced) {
939 mOutputLayer.editState().forceClientComposition = true;
940
941 mLayerFEState.compositionType = Hwc2::IComposerClient::Composition::SOLID_COLOR;
942
943 expectPerFrameCommonCalls();
944 expectSetColorCall();
945 expectSetCompositionTypeCall(Hwc2::IComposerClient::Composition::CLIENT);
946
947 mOutputLayer.writeStateToHWC(false);
948 }
949
TEST_F(OutputLayerWriteStateToHWCTest,allStateIncludesMetadataIfPresent)950 TEST_F(OutputLayerWriteStateToHWCTest, allStateIncludesMetadataIfPresent) {
951 mLayerFEState.compositionType = Hwc2::IComposerClient::Composition::DEVICE;
952 includeGenericLayerMetadataInState();
953
954 expectGeometryCommonCalls();
955 expectPerFrameCommonCalls();
956 expectSetHdrMetadataAndBufferCalls();
957 expectGenericLayerMetadataCalls();
958 expectSetCompositionTypeCall(Hwc2::IComposerClient::Composition::DEVICE);
959
960 mOutputLayer.writeStateToHWC(true);
961 }
962
TEST_F(OutputLayerWriteStateToHWCTest,perFrameStateDoesNotIncludeMetadataIfPresent)963 TEST_F(OutputLayerWriteStateToHWCTest, perFrameStateDoesNotIncludeMetadataIfPresent) {
964 mLayerFEState.compositionType = Hwc2::IComposerClient::Composition::DEVICE;
965 includeGenericLayerMetadataInState();
966
967 expectPerFrameCommonCalls();
968 expectSetHdrMetadataAndBufferCalls();
969 expectSetCompositionTypeCall(Hwc2::IComposerClient::Composition::DEVICE);
970
971 mOutputLayer.writeStateToHWC(false);
972 }
973
974 /*
975 * OutputLayer::writeCursorPositionToHWC()
976 */
977
978 struct OutputLayerWriteCursorPositionToHWCTest : public OutputLayerTest {
979 static constexpr int kDefaultTransform = TR_IDENT;
980 static constexpr hal::Error kDefaultError = hal::Error::UNSUPPORTED;
981
982 static const Rect kDefaultDisplayViewport;
983 static const Rect kDefaultCursorFrame;
984
OutputLayerWriteCursorPositionToHWCTestandroid::compositionengine::__anon834e7eb10111::OutputLayerWriteCursorPositionToHWCTest985 OutputLayerWriteCursorPositionToHWCTest() {
986 auto& outputLayerState = mOutputLayer.editState();
987 outputLayerState.hwc = impl::OutputLayerCompositionState::Hwc(mHwcLayer);
988
989 mLayerFEState.cursorFrame = kDefaultCursorFrame;
990
991 mOutputState.viewport = kDefaultDisplayViewport;
992 mOutputState.transform = ui::Transform{kDefaultTransform};
993 }
994
995 std::shared_ptr<HWC2::mock::Layer> mHwcLayer{std::make_shared<StrictMock<HWC2::mock::Layer>>()};
996 };
997
998 const Rect OutputLayerWriteCursorPositionToHWCTest::kDefaultDisplayViewport{0, 0, 1920, 1080};
999 const Rect OutputLayerWriteCursorPositionToHWCTest::kDefaultCursorFrame{1, 2, 3, 4};
1000
TEST_F(OutputLayerWriteCursorPositionToHWCTest,doesNothingIfNoFECompositionState)1001 TEST_F(OutputLayerWriteCursorPositionToHWCTest, doesNothingIfNoFECompositionState) {
1002 EXPECT_CALL(*mLayerFE, getCompositionState()).WillOnce(Return(nullptr));
1003
1004 mOutputLayer.writeCursorPositionToHWC();
1005 }
1006
TEST_F(OutputLayerWriteCursorPositionToHWCTest,writeCursorPositionToHWCHandlesNoHwcState)1007 TEST_F(OutputLayerWriteCursorPositionToHWCTest, writeCursorPositionToHWCHandlesNoHwcState) {
1008 mOutputLayer.editState().hwc.reset();
1009
1010 mOutputLayer.writeCursorPositionToHWC();
1011 }
1012
TEST_F(OutputLayerWriteCursorPositionToHWCTest,writeCursorPositionToHWCWritesStateToHWC)1013 TEST_F(OutputLayerWriteCursorPositionToHWCTest, writeCursorPositionToHWCWritesStateToHWC) {
1014 EXPECT_CALL(*mHwcLayer, setCursorPosition(1, 2)).WillOnce(Return(kDefaultError));
1015
1016 mOutputLayer.writeCursorPositionToHWC();
1017 }
1018
TEST_F(OutputLayerWriteCursorPositionToHWCTest,writeCursorPositionToHWCIntersectedWithViewport)1019 TEST_F(OutputLayerWriteCursorPositionToHWCTest, writeCursorPositionToHWCIntersectedWithViewport) {
1020 mLayerFEState.cursorFrame = Rect{3000, 3000, 3016, 3016};
1021
1022 EXPECT_CALL(*mHwcLayer, setCursorPosition(1920, 1080)).WillOnce(Return(kDefaultError));
1023
1024 mOutputLayer.writeCursorPositionToHWC();
1025 }
1026
TEST_F(OutputLayerWriteCursorPositionToHWCTest,writeCursorPositionToHWCRotatedByTransform)1027 TEST_F(OutputLayerWriteCursorPositionToHWCTest, writeCursorPositionToHWCRotatedByTransform) {
1028 mOutputState.transform = ui::Transform{TR_ROT_90};
1029
1030 EXPECT_CALL(*mHwcLayer, setCursorPosition(-4, 1)).WillOnce(Return(kDefaultError));
1031
1032 mOutputLayer.writeCursorPositionToHWC();
1033 }
1034
1035 /*
1036 * OutputLayer::getHwcLayer()
1037 */
1038
TEST_F(OutputLayerTest,getHwcLayerHandlesNoHwcState)1039 TEST_F(OutputLayerTest, getHwcLayerHandlesNoHwcState) {
1040 mOutputLayer.editState().hwc.reset();
1041
1042 EXPECT_TRUE(mOutputLayer.getHwcLayer() == nullptr);
1043 }
1044
TEST_F(OutputLayerTest,getHwcLayerHandlesNoHwcLayer)1045 TEST_F(OutputLayerTest, getHwcLayerHandlesNoHwcLayer) {
1046 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
1047
1048 EXPECT_TRUE(mOutputLayer.getHwcLayer() == nullptr);
1049 }
1050
TEST_F(OutputLayerTest,getHwcLayerReturnsHwcLayer)1051 TEST_F(OutputLayerTest, getHwcLayerReturnsHwcLayer) {
1052 auto hwcLayer = std::make_shared<StrictMock<HWC2::mock::Layer>>();
1053 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{hwcLayer};
1054
1055 EXPECT_EQ(hwcLayer.get(), mOutputLayer.getHwcLayer());
1056 }
1057
1058 /*
1059 * OutputLayer::requiresClientComposition()
1060 */
1061
TEST_F(OutputLayerTest,requiresClientCompositionReturnsTrueIfNoHWC2State)1062 TEST_F(OutputLayerTest, requiresClientCompositionReturnsTrueIfNoHWC2State) {
1063 mOutputLayer.editState().hwc.reset();
1064
1065 EXPECT_TRUE(mOutputLayer.requiresClientComposition());
1066 }
1067
TEST_F(OutputLayerTest,requiresClientCompositionReturnsTrueIfSetToClientComposition)1068 TEST_F(OutputLayerTest, requiresClientCompositionReturnsTrueIfSetToClientComposition) {
1069 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
1070 mOutputLayer.editState().hwc->hwcCompositionType = Hwc2::IComposerClient::Composition::CLIENT;
1071
1072 EXPECT_TRUE(mOutputLayer.requiresClientComposition());
1073 }
1074
TEST_F(OutputLayerTest,requiresClientCompositionReturnsFalseIfSetToDeviceComposition)1075 TEST_F(OutputLayerTest, requiresClientCompositionReturnsFalseIfSetToDeviceComposition) {
1076 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
1077 mOutputLayer.editState().hwc->hwcCompositionType = Hwc2::IComposerClient::Composition::DEVICE;
1078
1079 EXPECT_FALSE(mOutputLayer.requiresClientComposition());
1080 }
1081
1082 /*
1083 * OutputLayer::isHardwareCursor()
1084 */
1085
TEST_F(OutputLayerTest,isHardwareCursorReturnsFalseIfNoHWC2State)1086 TEST_F(OutputLayerTest, isHardwareCursorReturnsFalseIfNoHWC2State) {
1087 mOutputLayer.editState().hwc.reset();
1088
1089 EXPECT_FALSE(mOutputLayer.isHardwareCursor());
1090 }
1091
TEST_F(OutputLayerTest,isHardwareCursorReturnsTrueIfSetToCursorComposition)1092 TEST_F(OutputLayerTest, isHardwareCursorReturnsTrueIfSetToCursorComposition) {
1093 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
1094 mOutputLayer.editState().hwc->hwcCompositionType = Hwc2::IComposerClient::Composition::CURSOR;
1095
1096 EXPECT_TRUE(mOutputLayer.isHardwareCursor());
1097 }
1098
TEST_F(OutputLayerTest,isHardwareCursorReturnsFalseIfSetToDeviceComposition)1099 TEST_F(OutputLayerTest, isHardwareCursorReturnsFalseIfSetToDeviceComposition) {
1100 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
1101 mOutputLayer.editState().hwc->hwcCompositionType = Hwc2::IComposerClient::Composition::DEVICE;
1102
1103 EXPECT_FALSE(mOutputLayer.isHardwareCursor());
1104 }
1105
1106 /*
1107 * OutputLayer::applyDeviceCompositionTypeChange()
1108 */
1109
TEST_F(OutputLayerTest,applyDeviceCompositionTypeChangeSetsNewType)1110 TEST_F(OutputLayerTest, applyDeviceCompositionTypeChangeSetsNewType) {
1111 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
1112 mOutputLayer.editState().hwc->hwcCompositionType = Hwc2::IComposerClient::Composition::DEVICE;
1113
1114 mOutputLayer.applyDeviceCompositionTypeChange(Hwc2::IComposerClient::Composition::CLIENT);
1115
1116 ASSERT_TRUE(mOutputLayer.getState().hwc);
1117 EXPECT_EQ(Hwc2::IComposerClient::Composition::CLIENT,
1118 mOutputLayer.getState().hwc->hwcCompositionType);
1119 }
1120
1121 /*
1122 * OutputLayer::prepareForDeviceLayerRequests()
1123 */
1124
TEST_F(OutputLayerTest,prepareForDeviceLayerRequestsResetsRequestState)1125 TEST_F(OutputLayerTest, prepareForDeviceLayerRequestsResetsRequestState) {
1126 mOutputLayer.editState().clearClientTarget = true;
1127
1128 mOutputLayer.prepareForDeviceLayerRequests();
1129
1130 EXPECT_FALSE(mOutputLayer.getState().clearClientTarget);
1131 }
1132
1133 /*
1134 * OutputLayer::applyDeviceLayerRequest()
1135 */
1136
TEST_F(OutputLayerTest,applyDeviceLayerRequestHandlesClearClientTarget)1137 TEST_F(OutputLayerTest, applyDeviceLayerRequestHandlesClearClientTarget) {
1138 mOutputLayer.editState().clearClientTarget = false;
1139
1140 mOutputLayer.applyDeviceLayerRequest(Hwc2::IComposerClient::LayerRequest::CLEAR_CLIENT_TARGET);
1141
1142 EXPECT_TRUE(mOutputLayer.getState().clearClientTarget);
1143 }
1144
TEST_F(OutputLayerTest,applyDeviceLayerRequestHandlesUnknownRequest)1145 TEST_F(OutputLayerTest, applyDeviceLayerRequestHandlesUnknownRequest) {
1146 mOutputLayer.editState().clearClientTarget = false;
1147
1148 mOutputLayer.applyDeviceLayerRequest(static_cast<Hwc2::IComposerClient::LayerRequest>(0));
1149
1150 EXPECT_FALSE(mOutputLayer.getState().clearClientTarget);
1151 }
1152
1153 /*
1154 * OutputLayer::needsFiltering()
1155 */
1156
TEST_F(OutputLayerTest,needsFilteringReturnsFalseIfDisplaySizeSameAsSourceSize)1157 TEST_F(OutputLayerTest, needsFilteringReturnsFalseIfDisplaySizeSameAsSourceSize) {
1158 mOutputLayer.editState().displayFrame = Rect(100, 100, 200, 200);
1159 mOutputLayer.editState().sourceCrop = FloatRect{0.f, 0.f, 100.f, 100.f};
1160
1161 EXPECT_FALSE(mOutputLayer.needsFiltering());
1162 }
1163
TEST_F(OutputLayerTest,needsFilteringReturnsTrueIfDisplaySizeDifferentFromSourceSize)1164 TEST_F(OutputLayerTest, needsFilteringReturnsTrueIfDisplaySizeDifferentFromSourceSize) {
1165 mOutputLayer.editState().displayFrame = Rect(100, 100, 200, 200);
1166 mOutputLayer.editState().sourceCrop = FloatRect{0.f, 0.f, 100.1f, 100.1f};
1167
1168 EXPECT_TRUE(mOutputLayer.needsFiltering());
1169 }
1170
1171 } // namespace
1172 } // namespace android::compositionengine
1173