blob: 2e030a1823da67f4991ddaab8c9acfc78abcea2c [file] [log] [blame]
Lloyd Piquecc01a452018-12-04 17:24:00 -08001/*
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>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070018#include <compositionengine/impl/OutputLayerCompositionState.h>
Lloyd Pique07e33212018-12-18 16:33:37 -080019#include <compositionengine/mock/CompositionEngine.h>
Lloyd Piquef5275482019-01-29 18:42:42 -080020#include <compositionengine/mock/DisplayColorProfile.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080021#include <compositionengine/mock/Layer.h>
22#include <compositionengine/mock/LayerFE.h>
23#include <compositionengine/mock/Output.h>
24#include <gtest/gtest.h>
25
Lloyd Pique07e33212018-12-18 16:33:37 -080026#include "MockHWC2.h"
27#include "MockHWComposer.h"
Lloyd Piquef5275482019-01-29 18:42:42 -080028#include "RegionMatcher.h"
Lloyd Pique07e33212018-12-18 16:33:37 -080029
Lloyd Piquecc01a452018-12-04 17:24:00 -080030namespace android::compositionengine {
31namespace {
32
Lloyd Piquea83776c2019-01-29 18:42:32 -080033using testing::_;
Lloyd Pique46b72df2019-10-29 13:19:27 -070034using testing::InSequence;
Lloyd Piquea83776c2019-01-29 18:42:32 -080035using testing::Return;
36using testing::ReturnRef;
Lloyd Piquecc01a452018-12-04 17:24:00 -080037using testing::StrictMock;
38
Lloyd Piquea83776c2019-01-29 18:42:32 -080039constexpr auto TR_IDENT = 0u;
40constexpr auto TR_FLP_H = HAL_TRANSFORM_FLIP_H;
41constexpr auto TR_FLP_V = HAL_TRANSFORM_FLIP_V;
42constexpr auto TR_ROT_90 = HAL_TRANSFORM_ROT_90;
43constexpr auto TR_ROT_180 = TR_FLP_H | TR_FLP_V;
44constexpr auto TR_ROT_270 = TR_ROT_90 | TR_ROT_180;
45
46const std::string kOutputName{"Test Output"};
47
Lloyd Piquef5275482019-01-29 18:42:42 -080048MATCHER_P(ColorEq, expected, "") {
49 *result_listener << "Colors are not equal\n";
50 *result_listener << "expected " << expected.r << " " << expected.g << " " << expected.b << " "
51 << expected.a << "\n";
52 *result_listener << "actual " << arg.r << " " << arg.g << " " << arg.b << " " << arg.a << "\n";
53
54 return expected.r == arg.r && expected.g == arg.g && expected.b == arg.b && expected.a == arg.a;
55}
56
Lloyd Pique66d68602019-02-13 14:23:31 -080057struct OutputLayerTest : public testing::Test {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070058 struct OutputLayer final : public impl::OutputLayer {
59 OutputLayer(const compositionengine::Output& output,
60 std::shared_ptr<compositionengine::Layer> layer,
61 sp<compositionengine::LayerFE> layerFE)
62 : mOutput(output), mLayer(layer), mLayerFE(layerFE) {}
63 ~OutputLayer() override = default;
64
65 // compositionengine::OutputLayer overrides
66 const compositionengine::Output& getOutput() const override { return mOutput; }
67 compositionengine::Layer& getLayer() const override { return *mLayer; }
68 compositionengine::LayerFE& getLayerFE() const override { return *mLayerFE; }
69 const impl::OutputLayerCompositionState& getState() const override { return mState; }
70 impl::OutputLayerCompositionState& editState() override { return mState; }
71
72 // compositionengine::impl::OutputLayer overrides
73 void dumpState(std::string& out) const override { mState.dump(out); }
74
75 const compositionengine::Output& mOutput;
76 std::shared_ptr<compositionengine::Layer> mLayer;
77 sp<compositionengine::LayerFE> mLayerFE;
78 impl::OutputLayerCompositionState mState;
79 };
80
Lloyd Piquea83776c2019-01-29 18:42:32 -080081 OutputLayerTest() {
82 EXPECT_CALL(*mLayerFE, getDebugName()).WillRepeatedly(Return("Test LayerFE"));
83 EXPECT_CALL(mOutput, getName()).WillRepeatedly(ReturnRef(kOutputName));
84
Lloyd Pique9755fb72019-03-26 14:44:40 -070085 EXPECT_CALL(*mLayer, getFEState()).WillRepeatedly(ReturnRef(mLayerFEState));
Lloyd Piquea83776c2019-01-29 18:42:32 -080086 EXPECT_CALL(mOutput, getState()).WillRepeatedly(ReturnRef(mOutputState));
87 }
88
Lloyd Piquecc01a452018-12-04 17:24:00 -080089 compositionengine::mock::Output mOutput;
90 std::shared_ptr<compositionengine::mock::Layer> mLayer{
91 new StrictMock<compositionengine::mock::Layer>()};
92 sp<compositionengine::mock::LayerFE> mLayerFE{
93 new StrictMock<compositionengine::mock::LayerFE>()};
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070094 OutputLayer mOutputLayer{mOutput, mLayer, mLayerFE};
Lloyd Piquea83776c2019-01-29 18:42:32 -080095
Lloyd Pique9755fb72019-03-26 14:44:40 -070096 LayerFECompositionState mLayerFEState;
Lloyd Piquea83776c2019-01-29 18:42:32 -080097 impl::OutputCompositionState mOutputState;
Lloyd Piquecc01a452018-12-04 17:24:00 -080098};
99
Lloyd Piquea83776c2019-01-29 18:42:32 -0800100/*
Lloyd Piquecc01a452018-12-04 17:24:00 -0800101 * Basic construction
102 */
103
104TEST_F(OutputLayerTest, canInstantiateOutputLayer) {}
105
Lloyd Piquea83776c2019-01-29 18:42:32 -0800106/*
Lloyd Piquedf336d92019-03-07 21:38:42 -0800107 * OutputLayer::setHwcLayer()
Lloyd Pique07e33212018-12-18 16:33:37 -0800108 */
109
Lloyd Piquedf336d92019-03-07 21:38:42 -0800110TEST_F(OutputLayerTest, settingNullHwcLayerSetsEmptyHwcState) {
Lloyd Pique07e33212018-12-18 16:33:37 -0800111 StrictMock<compositionengine::mock::CompositionEngine> compositionEngine;
112
Lloyd Piquedf336d92019-03-07 21:38:42 -0800113 mOutputLayer.setHwcLayer(nullptr);
Lloyd Pique07e33212018-12-18 16:33:37 -0800114
115 EXPECT_FALSE(mOutputLayer.getState().hwc);
116}
117
Lloyd Piquedf336d92019-03-07 21:38:42 -0800118TEST_F(OutputLayerTest, settingHwcLayerSetsHwcState) {
119 auto hwcLayer = std::make_shared<StrictMock<HWC2::mock::Layer>>();
Lloyd Pique07e33212018-12-18 16:33:37 -0800120
Lloyd Piquedf336d92019-03-07 21:38:42 -0800121 mOutputLayer.setHwcLayer(hwcLayer);
Lloyd Pique07e33212018-12-18 16:33:37 -0800122
Lloyd Piquea83776c2019-01-29 18:42:32 -0800123 const auto& outputLayerState = mOutputLayer.getState();
124 ASSERT_TRUE(outputLayerState.hwc);
Lloyd Pique07e33212018-12-18 16:33:37 -0800125
Lloyd Piquea83776c2019-01-29 18:42:32 -0800126 const auto& hwcState = *outputLayerState.hwc;
Lloyd Piquedf336d92019-03-07 21:38:42 -0800127 EXPECT_EQ(hwcLayer, hwcState.hwcLayer);
Lloyd Pique07e33212018-12-18 16:33:37 -0800128}
129
Lloyd Piquea83776c2019-01-29 18:42:32 -0800130/*
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000131 * OutputLayer::calculateOutputSourceCrop()
132 */
133
134struct OutputLayerSourceCropTest : public OutputLayerTest {
135 OutputLayerSourceCropTest() {
136 // Set reasonable default values for a simple case. Each test will
137 // set one specific value to something different.
Lloyd Pique9755fb72019-03-26 14:44:40 -0700138 mLayerFEState.geomUsesSourceCrop = true;
139 mLayerFEState.geomContentCrop = Rect{0, 0, 1920, 1080};
140 mLayerFEState.transparentRegionHint = Region{};
141 mLayerFEState.geomLayerBounds = FloatRect{0.f, 0.f, 1920.f, 1080.f};
142 mLayerFEState.geomLayerTransform = ui::Transform{TR_IDENT};
143 mLayerFEState.geomBufferSize = Rect{0, 0, 1920, 1080};
144 mLayerFEState.geomBufferTransform = TR_IDENT;
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000145
146 mOutputState.viewport = Rect{0, 0, 1920, 1080};
147 }
148
149 FloatRect calculateOutputSourceCrop() {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700150 mLayerFEState.geomInverseLayerTransform = mLayerFEState.geomLayerTransform.inverse();
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000151
152 return mOutputLayer.calculateOutputSourceCrop();
153 }
154};
155
156TEST_F(OutputLayerSourceCropTest, computesEmptyIfSourceCropNotUsed) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700157 mLayerFEState.geomUsesSourceCrop = false;
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000158
159 const FloatRect expected{};
Lloyd Piqueea629282019-12-03 15:57:10 -0800160 EXPECT_THAT(calculateOutputSourceCrop(), expected);
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000161}
162
163TEST_F(OutputLayerSourceCropTest, correctForSimpleDefaultCase) {
164 const FloatRect expected{0.f, 0.f, 1920.f, 1080.f};
Lloyd Piqueea629282019-12-03 15:57:10 -0800165 EXPECT_THAT(calculateOutputSourceCrop(), expected);
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000166}
167
168TEST_F(OutputLayerSourceCropTest, handlesBoundsOutsideViewport) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700169 mLayerFEState.geomLayerBounds = FloatRect{-2000.f, -2000.f, 2000.f, 2000.f};
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000170
171 const FloatRect expected{0.f, 0.f, 1920.f, 1080.f};
Lloyd Piqueea629282019-12-03 15:57:10 -0800172 EXPECT_THAT(calculateOutputSourceCrop(), expected);
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000173}
174
175TEST_F(OutputLayerSourceCropTest, handlesBoundsOutsideViewportRotated) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700176 mLayerFEState.geomLayerBounds = FloatRect{-2000.f, -2000.f, 2000.f, 2000.f};
177 mLayerFEState.geomLayerTransform.set(HAL_TRANSFORM_ROT_90, 1920, 1080);
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000178
179 const FloatRect expected{0.f, 0.f, 1080.f, 1080.f};
Lloyd Piqueea629282019-12-03 15:57:10 -0800180 EXPECT_THAT(calculateOutputSourceCrop(), expected);
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000181}
182
183TEST_F(OutputLayerSourceCropTest, calculateOutputSourceCropWorksWithATransformedBuffer) {
184 struct Entry {
185 uint32_t bufferInvDisplay;
186 uint32_t buffer;
187 uint32_t display;
188 FloatRect expected;
189 };
190 // Not an exhaustive list of cases, but hopefully enough.
191 const std::array<Entry, 12> testData = {
192 // clang-format off
193 // inv buffer display expected
194 /* 0 */ Entry{false, TR_IDENT, TR_IDENT, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
195 /* 1 */ Entry{false, TR_IDENT, TR_ROT_90, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
196 /* 2 */ Entry{false, TR_IDENT, TR_ROT_180, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
197 /* 3 */ Entry{false, TR_IDENT, TR_ROT_270, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
198
199 /* 4 */ Entry{true, TR_IDENT, TR_IDENT, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
200 /* 5 */ Entry{true, TR_IDENT, TR_ROT_90, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
201 /* 6 */ Entry{true, TR_IDENT, TR_ROT_180, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
202 /* 7 */ Entry{true, TR_IDENT, TR_ROT_270, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
203
204 /* 8 */ Entry{false, TR_IDENT, TR_IDENT, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
205 /* 9 */ Entry{false, TR_ROT_90, TR_ROT_90, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
206 /* 10 */ Entry{false, TR_ROT_180, TR_ROT_180, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
207 /* 11 */ Entry{false, TR_ROT_270, TR_ROT_270, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
208
209 // clang-format on
210 };
211
212 for (size_t i = 0; i < testData.size(); i++) {
213 const auto& entry = testData[i];
214
Lloyd Pique9755fb72019-03-26 14:44:40 -0700215 mLayerFEState.geomBufferUsesDisplayInverseTransform = entry.bufferInvDisplay;
216 mLayerFEState.geomBufferTransform = entry.buffer;
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000217 mOutputState.orientation = entry.display;
218
Lloyd Piqueea629282019-12-03 15:57:10 -0800219 EXPECT_THAT(calculateOutputSourceCrop(), entry.expected) << "entry " << i;
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000220 }
221}
222
223TEST_F(OutputLayerSourceCropTest, geomContentCropAffectsCrop) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700224 mLayerFEState.geomContentCrop = Rect{0, 0, 960, 540};
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000225
226 const FloatRect expected{0.f, 0.f, 960.f, 540.f};
Lloyd Piqueea629282019-12-03 15:57:10 -0800227 EXPECT_THAT(calculateOutputSourceCrop(), expected);
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000228}
229
230TEST_F(OutputLayerSourceCropTest, viewportAffectsCrop) {
231 mOutputState.viewport = Rect{0, 0, 960, 540};
232
233 const FloatRect expected{0.f, 0.f, 960.f, 540.f};
Lloyd Piqueea629282019-12-03 15:57:10 -0800234 EXPECT_THAT(calculateOutputSourceCrop(), expected);
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000235}
236
237/*
Lloyd Piquea83776c2019-01-29 18:42:32 -0800238 * OutputLayer::calculateOutputDisplayFrame()
239 */
240
241struct OutputLayerDisplayFrameTest : public OutputLayerTest {
242 OutputLayerDisplayFrameTest() {
243 // Set reasonable default values for a simple case. Each test will
244 // set one specific value to something different.
245
Lloyd Pique9755fb72019-03-26 14:44:40 -0700246 mLayerFEState.transparentRegionHint = Region{};
247 mLayerFEState.geomLayerTransform = ui::Transform{TR_IDENT};
248 mLayerFEState.geomBufferSize = Rect{0, 0, 1920, 1080};
249 mLayerFEState.geomBufferUsesDisplayInverseTransform = false;
250 mLayerFEState.geomCrop = Rect{0, 0, 1920, 1080};
251 mLayerFEState.geomLayerBounds = FloatRect{0.f, 0.f, 1920.f, 1080.f};
Lloyd Piquea83776c2019-01-29 18:42:32 -0800252
253 mOutputState.viewport = Rect{0, 0, 1920, 1080};
254 mOutputState.transform = ui::Transform{TR_IDENT};
255 }
256
257 Rect calculateOutputDisplayFrame() {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700258 mLayerFEState.geomInverseLayerTransform = mLayerFEState.geomLayerTransform.inverse();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800259
260 return mOutputLayer.calculateOutputDisplayFrame();
261 }
262};
263
264TEST_F(OutputLayerDisplayFrameTest, correctForSimpleDefaultCase) {
265 const Rect expected{0, 0, 1920, 1080};
Lloyd Piqueea629282019-12-03 15:57:10 -0800266 EXPECT_THAT(calculateOutputDisplayFrame(), expected);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800267}
268
269TEST_F(OutputLayerDisplayFrameTest, fullActiveTransparentRegionReturnsEmptyFrame) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700270 mLayerFEState.transparentRegionHint = Region{Rect{0, 0, 1920, 1080}};
Lloyd Piquea83776c2019-01-29 18:42:32 -0800271 const Rect expected{0, 0, 0, 0};
Lloyd Piqueea629282019-12-03 15:57:10 -0800272 EXPECT_THAT(calculateOutputDisplayFrame(), expected);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800273}
274
275TEST_F(OutputLayerDisplayFrameTest, cropAffectsDisplayFrame) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700276 mLayerFEState.geomCrop = Rect{100, 200, 300, 500};
Lloyd Piquea83776c2019-01-29 18:42:32 -0800277 const Rect expected{100, 200, 300, 500};
Lloyd Piqueea629282019-12-03 15:57:10 -0800278 EXPECT_THAT(calculateOutputDisplayFrame(), expected);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800279}
280
281TEST_F(OutputLayerDisplayFrameTest, cropAffectsDisplayFrameRotated) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700282 mLayerFEState.geomCrop = Rect{100, 200, 300, 500};
283 mLayerFEState.geomLayerTransform.set(HAL_TRANSFORM_ROT_90, 1920, 1080);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800284 const Rect expected{1420, 100, 1720, 300};
Lloyd Piqueea629282019-12-03 15:57:10 -0800285 EXPECT_THAT(calculateOutputDisplayFrame(), expected);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800286}
287
288TEST_F(OutputLayerDisplayFrameTest, emptyGeomCropIsNotUsedToComputeFrame) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700289 mLayerFEState.geomCrop = Rect{};
Lloyd Piquea83776c2019-01-29 18:42:32 -0800290 const Rect expected{0, 0, 1920, 1080};
Lloyd Piqueea629282019-12-03 15:57:10 -0800291 EXPECT_THAT(calculateOutputDisplayFrame(), expected);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800292}
293
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000294TEST_F(OutputLayerDisplayFrameTest, geomLayerBoundsAffectsFrame) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700295 mLayerFEState.geomLayerBounds = FloatRect{0.f, 0.f, 960.f, 540.f};
Lloyd Piquea83776c2019-01-29 18:42:32 -0800296 const Rect expected{0, 0, 960, 540};
Lloyd Piqueea629282019-12-03 15:57:10 -0800297 EXPECT_THAT(calculateOutputDisplayFrame(), expected);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800298}
299
300TEST_F(OutputLayerDisplayFrameTest, viewportAffectsFrame) {
301 mOutputState.viewport = Rect{0, 0, 960, 540};
302 const Rect expected{0, 0, 960, 540};
Lloyd Piqueea629282019-12-03 15:57:10 -0800303 EXPECT_THAT(calculateOutputDisplayFrame(), expected);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800304}
305
306TEST_F(OutputLayerDisplayFrameTest, outputTransformAffectsDisplayFrame) {
307 mOutputState.transform = ui::Transform{HAL_TRANSFORM_ROT_90};
308 const Rect expected{-1080, 0, 0, 1920};
Lloyd Piqueea629282019-12-03 15:57:10 -0800309 EXPECT_THAT(calculateOutputDisplayFrame(), expected);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800310}
311
312/*
313 * OutputLayer::calculateOutputRelativeBufferTransform()
314 */
315
316TEST_F(OutputLayerTest, calculateOutputRelativeBufferTransformTestsNeeded) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700317 mLayerFEState.geomBufferUsesDisplayInverseTransform = false;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800318
319 struct Entry {
320 uint32_t layer;
321 uint32_t buffer;
322 uint32_t display;
323 uint32_t expected;
324 };
325 // Not an exhaustive list of cases, but hopefully enough.
326 const std::array<Entry, 24> testData = {
327 // clang-format off
328 // layer buffer display expected
329 /* 0 */ Entry{TR_IDENT, TR_IDENT, TR_IDENT, TR_IDENT},
330 /* 1 */ Entry{TR_IDENT, TR_IDENT, TR_ROT_90, TR_ROT_90},
331 /* 2 */ Entry{TR_IDENT, TR_IDENT, TR_ROT_180, TR_ROT_180},
332 /* 3 */ Entry{TR_IDENT, TR_IDENT, TR_ROT_270, TR_ROT_270},
333
334 /* 4 */ Entry{TR_IDENT, TR_FLP_H, TR_IDENT, TR_FLP_H ^ TR_IDENT},
335 /* 5 */ Entry{TR_IDENT, TR_FLP_H, TR_ROT_90, TR_FLP_H ^ TR_ROT_90},
336 /* 6 */ Entry{TR_IDENT, TR_FLP_H, TR_ROT_180, TR_FLP_H ^ TR_ROT_180},
337 /* 7 */ Entry{TR_IDENT, TR_FLP_H, TR_ROT_270, TR_FLP_H ^ TR_ROT_270},
338
339 /* 8 */ Entry{TR_IDENT, TR_FLP_V, TR_IDENT, TR_FLP_V},
340 /* 9 */ Entry{TR_IDENT, TR_ROT_90, TR_ROT_90, TR_ROT_180},
341 /* 10 */ Entry{TR_IDENT, TR_ROT_180, TR_ROT_180, TR_IDENT},
342 /* 11 */ Entry{TR_IDENT, TR_ROT_270, TR_ROT_270, TR_ROT_180},
343
344 /* 12 */ Entry{TR_ROT_90, TR_IDENT, TR_IDENT, TR_IDENT ^ TR_ROT_90},
345 /* 13 */ Entry{TR_ROT_90, TR_FLP_H, TR_ROT_90, TR_FLP_H ^ TR_ROT_180},
346 /* 14 */ Entry{TR_ROT_90, TR_IDENT, TR_ROT_180, TR_IDENT ^ TR_ROT_270},
347 /* 15 */ Entry{TR_ROT_90, TR_FLP_H, TR_ROT_270, TR_FLP_H ^ TR_IDENT},
348
349 /* 16 */ Entry{TR_ROT_180, TR_FLP_H, TR_IDENT, TR_FLP_H ^ TR_ROT_180},
350 /* 17 */ Entry{TR_ROT_180, TR_IDENT, TR_ROT_90, TR_IDENT ^ TR_ROT_270},
351 /* 18 */ Entry{TR_ROT_180, TR_FLP_H, TR_ROT_180, TR_FLP_H ^ TR_IDENT},
352 /* 19 */ Entry{TR_ROT_180, TR_IDENT, TR_ROT_270, TR_IDENT ^ TR_ROT_90},
353
354 /* 20 */ Entry{TR_ROT_270, TR_IDENT, TR_IDENT, TR_IDENT ^ TR_ROT_270},
355 /* 21 */ Entry{TR_ROT_270, TR_FLP_H, TR_ROT_90, TR_FLP_H ^ TR_IDENT},
356 /* 22 */ Entry{TR_ROT_270, TR_FLP_H, TR_ROT_180, TR_FLP_H ^ TR_ROT_90},
357 /* 23 */ Entry{TR_ROT_270, TR_IDENT, TR_ROT_270, TR_IDENT ^ TR_ROT_180},
358 // clang-format on
359 };
360
361 for (size_t i = 0; i < testData.size(); i++) {
362 const auto& entry = testData[i];
363
Lloyd Pique9755fb72019-03-26 14:44:40 -0700364 mLayerFEState.geomLayerTransform.set(entry.layer, 1920, 1080);
365 mLayerFEState.geomBufferTransform = entry.buffer;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800366 mOutputState.orientation = entry.display;
367
368 auto actual = mOutputLayer.calculateOutputRelativeBufferTransform();
369 EXPECT_EQ(entry.expected, actual) << "entry " << i;
370 }
371}
372
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000373TEST_F(OutputLayerTest,
374 calculateOutputRelativeBufferTransformTestWithOfBufferUsesDisplayInverseTransform) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700375 mLayerFEState.geomBufferUsesDisplayInverseTransform = true;
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000376
377 struct Entry {
378 uint32_t layer;
379 uint32_t buffer;
380 uint32_t display;
381 uint32_t expected;
382 };
383 // Not an exhaustive list of cases, but hopefully enough.
384 const std::array<Entry, 24> testData = {
385 // clang-format off
386 // layer buffer display expected
387 /* 0 */ Entry{TR_IDENT, TR_IDENT, TR_IDENT, TR_IDENT},
388 /* 1 */ Entry{TR_IDENT, TR_IDENT, TR_ROT_90, TR_IDENT},
389 /* 2 */ Entry{TR_IDENT, TR_IDENT, TR_ROT_180, TR_IDENT},
390 /* 3 */ Entry{TR_IDENT, TR_IDENT, TR_ROT_270, TR_IDENT},
391
392 /* 4 */ Entry{TR_IDENT, TR_FLP_H, TR_IDENT, TR_FLP_H},
393 /* 5 */ Entry{TR_IDENT, TR_FLP_H, TR_ROT_90, TR_FLP_H},
394 /* 6 */ Entry{TR_IDENT, TR_FLP_H, TR_ROT_180, TR_FLP_H},
395 /* 7 */ Entry{TR_IDENT, TR_FLP_H, TR_ROT_270, TR_FLP_H},
396
397 /* 8 */ Entry{TR_IDENT, TR_FLP_V, TR_IDENT, TR_FLP_V},
398 /* 9 */ Entry{TR_IDENT, TR_ROT_90, TR_ROT_90, TR_ROT_90},
399 /* 10 */ Entry{TR_IDENT, TR_ROT_180, TR_ROT_180, TR_ROT_180},
400 /* 11 */ Entry{TR_IDENT, TR_ROT_270, TR_ROT_270, TR_ROT_270},
401
402 /* 12 */ Entry{TR_ROT_90, TR_IDENT, TR_IDENT, TR_IDENT},
403 /* 13 */ Entry{TR_ROT_90, TR_FLP_H, TR_ROT_90, TR_FLP_H},
404 /* 14 */ Entry{TR_ROT_90, TR_IDENT, TR_ROT_180, TR_IDENT},
405 /* 15 */ Entry{TR_ROT_90, TR_FLP_H, TR_ROT_270, TR_FLP_H},
406
407 /* 16 */ Entry{TR_ROT_180, TR_FLP_H, TR_IDENT, TR_FLP_H},
408 /* 17 */ Entry{TR_ROT_180, TR_IDENT, TR_ROT_90, TR_IDENT},
409 /* 18 */ Entry{TR_ROT_180, TR_FLP_H, TR_ROT_180, TR_FLP_H},
410 /* 19 */ Entry{TR_ROT_180, TR_IDENT, TR_ROT_270, TR_IDENT},
411
412 /* 20 */ Entry{TR_ROT_270, TR_IDENT, TR_IDENT, TR_IDENT},
413 /* 21 */ Entry{TR_ROT_270, TR_FLP_H, TR_ROT_90, TR_FLP_H},
414 /* 22 */ Entry{TR_ROT_270, TR_FLP_H, TR_ROT_180, TR_FLP_H},
415 /* 23 */ Entry{TR_ROT_270, TR_IDENT, TR_ROT_270, TR_IDENT},
416 // clang-format on
417 };
418
419 for (size_t i = 0; i < testData.size(); i++) {
420 const auto& entry = testData[i];
421
Lloyd Pique9755fb72019-03-26 14:44:40 -0700422 mLayerFEState.geomLayerTransform = ui::Transform{entry.layer};
423 mLayerFEState.geomBufferTransform = entry.buffer;
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000424 mOutputState.orientation = entry.display;
425
426 auto actual = mOutputLayer.calculateOutputRelativeBufferTransform();
427 EXPECT_EQ(entry.expected, actual) << "entry " << i;
428 }
429}
430
431/*
432 * OutputLayer::updateCompositionState()
433 */
434
435struct OutputLayerPartialMockForUpdateCompositionState : public impl::OutputLayer {
436 OutputLayerPartialMockForUpdateCompositionState(const compositionengine::Output& output,
437 std::shared_ptr<compositionengine::Layer> layer,
438 sp<compositionengine::LayerFE> layerFE)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700439 : mOutput(output), mLayer(layer), mLayerFE(layerFE) {}
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000440 // Mock everything called by updateCompositionState to simplify testing it.
441 MOCK_CONST_METHOD0(calculateOutputSourceCrop, FloatRect());
442 MOCK_CONST_METHOD0(calculateOutputDisplayFrame, Rect());
443 MOCK_CONST_METHOD0(calculateOutputRelativeBufferTransform, uint32_t());
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700444
445 // compositionengine::OutputLayer overrides
446 const compositionengine::Output& getOutput() const override { return mOutput; }
447 compositionengine::Layer& getLayer() const override { return *mLayer; }
448 compositionengine::LayerFE& getLayerFE() const override { return *mLayerFE; }
449 const impl::OutputLayerCompositionState& getState() const override { return mState; }
450 impl::OutputLayerCompositionState& editState() override { return mState; }
451
452 // These need implementations though are not expected to be called.
453 MOCK_CONST_METHOD1(dumpState, void(std::string&));
454
455 const compositionengine::Output& mOutput;
456 std::shared_ptr<compositionengine::Layer> mLayer;
457 sp<compositionengine::LayerFE> mLayerFE;
458 impl::OutputLayerCompositionState mState;
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000459};
460
461struct OutputLayerUpdateCompositionStateTest : public OutputLayerTest {
462public:
463 OutputLayerUpdateCompositionStateTest() {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700464 EXPECT_CALL(*mLayer, getFEState()).WillRepeatedly(ReturnRef(mLayerFEState));
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000465 EXPECT_CALL(mOutput, getState()).WillRepeatedly(ReturnRef(mOutputState));
Lloyd Piquef5275482019-01-29 18:42:42 -0800466 EXPECT_CALL(mOutput, getDisplayColorProfile())
467 .WillRepeatedly(Return(&mDisplayColorProfile));
468 EXPECT_CALL(mDisplayColorProfile, isDataspaceSupported(_)).WillRepeatedly(Return(true));
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000469 }
470
471 ~OutputLayerUpdateCompositionStateTest() = default;
472
473 void setupGeometryChildCallValues() {
474 EXPECT_CALL(mOutputLayer, calculateOutputSourceCrop()).WillOnce(Return(kSourceCrop));
475 EXPECT_CALL(mOutputLayer, calculateOutputDisplayFrame()).WillOnce(Return(kDisplayFrame));
476 EXPECT_CALL(mOutputLayer, calculateOutputRelativeBufferTransform())
477 .WillOnce(Return(mBufferTransform));
478 }
479
480 void validateComputedGeometryState() {
481 const auto& state = mOutputLayer.getState();
482 EXPECT_EQ(kSourceCrop, state.sourceCrop);
483 EXPECT_EQ(kDisplayFrame, state.displayFrame);
484 EXPECT_EQ(static_cast<Hwc2::Transform>(mBufferTransform), state.bufferTransform);
485 }
486
487 const FloatRect kSourceCrop{1.f, 2.f, 3.f, 4.f};
488 const Rect kDisplayFrame{11, 12, 13, 14};
489 uint32_t mBufferTransform{21};
490
491 using OutputLayer = OutputLayerPartialMockForUpdateCompositionState;
492 StrictMock<OutputLayer> mOutputLayer{mOutput, mLayer, mLayerFE};
Lloyd Piquef5275482019-01-29 18:42:42 -0800493 StrictMock<mock::DisplayColorProfile> mDisplayColorProfile;
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000494};
495
496TEST_F(OutputLayerUpdateCompositionStateTest, setsStateNormally) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700497 mLayerFEState.isSecure = true;
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000498 mOutputState.isSecure = true;
Lloyd Piquefe671022019-09-24 10:43:03 -0700499 mOutputLayer.editState().forceClientComposition = true;
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000500
501 setupGeometryChildCallValues();
502
Lloyd Pique7a234912019-10-03 11:54:27 -0700503 mOutputLayer.updateCompositionState(true, false);
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000504
505 validateComputedGeometryState();
506
507 EXPECT_EQ(false, mOutputLayer.getState().forceClientComposition);
508}
509
510TEST_F(OutputLayerUpdateCompositionStateTest,
511 alsoSetsForceCompositionIfSecureLayerOnNonsecureOutput) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700512 mLayerFEState.isSecure = true;
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000513 mOutputState.isSecure = false;
514
515 setupGeometryChildCallValues();
516
Lloyd Pique7a234912019-10-03 11:54:27 -0700517 mOutputLayer.updateCompositionState(true, false);
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000518
519 validateComputedGeometryState();
520
521 EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
522}
523
524TEST_F(OutputLayerUpdateCompositionStateTest,
525 alsoSetsForceCompositionIfUnsupportedBufferTransform) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700526 mLayerFEState.isSecure = true;
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000527 mOutputState.isSecure = true;
528
529 mBufferTransform = ui::Transform::ROT_INVALID;
530
531 setupGeometryChildCallValues();
532
Lloyd Pique7a234912019-10-03 11:54:27 -0700533 mOutputLayer.updateCompositionState(true, false);
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000534
535 validateComputedGeometryState();
536
537 EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
538}
539
Lloyd Piquef5275482019-01-29 18:42:42 -0800540TEST_F(OutputLayerUpdateCompositionStateTest, setsOutputLayerColorspaceCorrectly) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700541 mLayerFEState.dataspace = ui::Dataspace::DISPLAY_P3;
Lloyd Piquef5275482019-01-29 18:42:42 -0800542 mOutputState.targetDataspace = ui::Dataspace::V0_SCRGB;
543
544 // If the layer is not colorspace agnostic, the output layer dataspace
545 // should use the layers requested colorspace.
Lloyd Pique9755fb72019-03-26 14:44:40 -0700546 mLayerFEState.isColorspaceAgnostic = false;
Lloyd Piquef5275482019-01-29 18:42:42 -0800547
Lloyd Pique7a234912019-10-03 11:54:27 -0700548 mOutputLayer.updateCompositionState(false, false);
Lloyd Piquef5275482019-01-29 18:42:42 -0800549
550 EXPECT_EQ(ui::Dataspace::DISPLAY_P3, mOutputLayer.getState().dataspace);
551
552 // If the layer is colorspace agnostic, the output layer dataspace
553 // should use the colorspace chosen for the whole output.
Lloyd Pique9755fb72019-03-26 14:44:40 -0700554 mLayerFEState.isColorspaceAgnostic = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800555
Lloyd Pique7a234912019-10-03 11:54:27 -0700556 mOutputLayer.updateCompositionState(false, false);
Lloyd Piquef5275482019-01-29 18:42:42 -0800557
558 EXPECT_EQ(ui::Dataspace::V0_SCRGB, mOutputLayer.getState().dataspace);
559}
560
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000561TEST_F(OutputLayerUpdateCompositionStateTest, doesNotRecomputeGeometryIfNotRequested) {
Lloyd Piquefe671022019-09-24 10:43:03 -0700562 mOutputLayer.editState().forceClientComposition = false;
563
Lloyd Pique7a234912019-10-03 11:54:27 -0700564 mOutputLayer.updateCompositionState(false, false);
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000565
566 EXPECT_EQ(false, mOutputLayer.getState().forceClientComposition);
567}
568
Lloyd Piquefe671022019-09-24 10:43:03 -0700569TEST_F(OutputLayerUpdateCompositionStateTest,
570 doesNotClearForceClientCompositionIfNotDoingGeometry) {
571 mOutputLayer.editState().forceClientComposition = true;
572
Lloyd Pique7a234912019-10-03 11:54:27 -0700573 mOutputLayer.updateCompositionState(false, false);
Lloyd Piquefe671022019-09-24 10:43:03 -0700574
575 EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
576}
577
Lloyd Piquef5275482019-01-29 18:42:42 -0800578TEST_F(OutputLayerUpdateCompositionStateTest, clientCompositionForcedFromFrontEndFlagAtAnyTime) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700579 mLayerFEState.forceClientComposition = true;
Lloyd Piquefe671022019-09-24 10:43:03 -0700580 mOutputLayer.editState().forceClientComposition = false;
Lloyd Piquef5275482019-01-29 18:42:42 -0800581
Lloyd Pique7a234912019-10-03 11:54:27 -0700582 mOutputLayer.updateCompositionState(false, false);
Lloyd Piquef5275482019-01-29 18:42:42 -0800583
584 EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
585}
586
587TEST_F(OutputLayerUpdateCompositionStateTest,
588 clientCompositionForcedFromUnsupportedDataspaceAtAnyTime) {
Lloyd Piquefe671022019-09-24 10:43:03 -0700589 mOutputLayer.editState().forceClientComposition = false;
Lloyd Piquef5275482019-01-29 18:42:42 -0800590 EXPECT_CALL(mDisplayColorProfile, isDataspaceSupported(_)).WillRepeatedly(Return(false));
591
Lloyd Pique7a234912019-10-03 11:54:27 -0700592 mOutputLayer.updateCompositionState(false, false);
593
594 EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
595}
596
597TEST_F(OutputLayerUpdateCompositionStateTest, clientCompositionForcedFromArgumentFlag) {
598 mLayerFEState.forceClientComposition = false;
599 mOutputLayer.editState().forceClientComposition = false;
600
601 mOutputLayer.updateCompositionState(false, true);
602
603 EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
604
605 mOutputLayer.editState().forceClientComposition = false;
606
607 setupGeometryChildCallValues();
608
609 mOutputLayer.updateCompositionState(true, true);
Lloyd Piquef5275482019-01-29 18:42:42 -0800610
611 EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
612}
613
Lloyd Piquea83776c2019-01-29 18:42:32 -0800614/*
615 * OutputLayer::writeStateToHWC()
616 */
617
618struct OutputLayerWriteStateToHWCTest : public OutputLayerTest {
619 static constexpr HWC2::Error kError = HWC2::Error::Unsupported;
620 static constexpr FloatRect kSourceCrop{11.f, 12.f, 13.f, 14.f};
621 static constexpr uint32_t kZOrder = 21u;
622 static constexpr Hwc2::Transform kBufferTransform = static_cast<Hwc2::Transform>(31);
623 static constexpr Hwc2::IComposerClient::BlendMode kBlendMode =
624 static_cast<Hwc2::IComposerClient::BlendMode>(41);
625 static constexpr float kAlpha = 51.f;
626 static constexpr uint32_t kType = 61u;
627 static constexpr uint32_t kAppId = 62u;
Lloyd Piquef5275482019-01-29 18:42:42 -0800628 static constexpr ui::Dataspace kDataspace = static_cast<ui::Dataspace>(71);
629 static constexpr int kSupportedPerFrameMetadata = 101;
630 static constexpr int kExpectedHwcSlot = 0;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800631
Lloyd Piquef5275482019-01-29 18:42:42 -0800632 static const half4 kColor;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800633 static const Rect kDisplayFrame;
Lloyd Piquea2468662019-03-07 21:31:06 -0800634 static const Region kOutputSpaceVisibleRegion;
Lloyd Piquef5275482019-01-29 18:42:42 -0800635 static const mat4 kColorTransform;
636 static const Region kSurfaceDamage;
637 static const HdrMetadata kHdrMetadata;
638 static native_handle_t* kSidebandStreamHandle;
639 static const sp<GraphicBuffer> kBuffer;
640 static const sp<Fence> kFence;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800641
642 OutputLayerWriteStateToHWCTest() {
643 auto& outputLayerState = mOutputLayer.editState();
644 outputLayerState.hwc = impl::OutputLayerCompositionState::Hwc(mHwcLayer);
645
646 outputLayerState.displayFrame = kDisplayFrame;
647 outputLayerState.sourceCrop = kSourceCrop;
648 outputLayerState.z = kZOrder;
649 outputLayerState.bufferTransform = static_cast<Hwc2::Transform>(kBufferTransform);
Lloyd Piquea2468662019-03-07 21:31:06 -0800650 outputLayerState.outputSpaceVisibleRegion = kOutputSpaceVisibleRegion;
Lloyd Piquef5275482019-01-29 18:42:42 -0800651 outputLayerState.dataspace = kDataspace;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800652
Lloyd Pique9755fb72019-03-26 14:44:40 -0700653 mLayerFEState.blendMode = kBlendMode;
654 mLayerFEState.alpha = kAlpha;
655 mLayerFEState.type = kType;
656 mLayerFEState.appId = kAppId;
657 mLayerFEState.colorTransform = kColorTransform;
658 mLayerFEState.color = kColor;
659 mLayerFEState.surfaceDamage = kSurfaceDamage;
660 mLayerFEState.hdrMetadata = kHdrMetadata;
661 mLayerFEState.sidebandStream = NativeHandle::create(kSidebandStreamHandle, false);
662 mLayerFEState.buffer = kBuffer;
663 mLayerFEState.bufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
664 mLayerFEState.acquireFence = kFence;
Lloyd Piquef5275482019-01-29 18:42:42 -0800665
666 EXPECT_CALL(mOutput, getDisplayColorProfile())
667 .WillRepeatedly(Return(&mDisplayColorProfile));
668 EXPECT_CALL(mDisplayColorProfile, getSupportedPerFrameMetadata())
669 .WillRepeatedly(Return(kSupportedPerFrameMetadata));
Lloyd Piquea83776c2019-01-29 18:42:32 -0800670 }
671
Lloyd Piquef5275482019-01-29 18:42:42 -0800672 // Some tests may need to simulate unsupported HWC calls
673 enum class SimulateUnsupported { None, ColorTransform };
674
Lloyd Piquea83776c2019-01-29 18:42:32 -0800675 void expectGeometryCommonCalls() {
676 EXPECT_CALL(*mHwcLayer, setDisplayFrame(kDisplayFrame)).WillOnce(Return(kError));
677 EXPECT_CALL(*mHwcLayer, setSourceCrop(kSourceCrop)).WillOnce(Return(kError));
678 EXPECT_CALL(*mHwcLayer, setZOrder(kZOrder)).WillOnce(Return(kError));
679 EXPECT_CALL(*mHwcLayer, setTransform(static_cast<HWC2::Transform>(kBufferTransform)))
680 .WillOnce(Return(kError));
681
682 EXPECT_CALL(*mHwcLayer, setBlendMode(static_cast<HWC2::BlendMode>(kBlendMode)))
683 .WillOnce(Return(kError));
684 EXPECT_CALL(*mHwcLayer, setPlaneAlpha(kAlpha)).WillOnce(Return(kError));
685 EXPECT_CALL(*mHwcLayer, setInfo(kType, kAppId)).WillOnce(Return(kError));
686 }
687
Lloyd Piquef5275482019-01-29 18:42:42 -0800688 void expectPerFrameCommonCalls(SimulateUnsupported unsupported = SimulateUnsupported::None) {
Lloyd Piquea2468662019-03-07 21:31:06 -0800689 EXPECT_CALL(*mHwcLayer, setVisibleRegion(RegionEq(kOutputSpaceVisibleRegion)))
Lloyd Piquef5275482019-01-29 18:42:42 -0800690 .WillOnce(Return(kError));
691 EXPECT_CALL(*mHwcLayer, setDataspace(kDataspace)).WillOnce(Return(kError));
692 EXPECT_CALL(*mHwcLayer, setColorTransform(kColorTransform))
693 .WillOnce(Return(unsupported == SimulateUnsupported::ColorTransform
694 ? HWC2::Error::Unsupported
695 : HWC2::Error::None));
696 EXPECT_CALL(*mHwcLayer, setSurfaceDamage(RegionEq(kSurfaceDamage)))
697 .WillOnce(Return(kError));
698 }
699
700 void expectSetCompositionTypeCall(Hwc2::IComposerClient::Composition compositionType) {
701 EXPECT_CALL(*mHwcLayer, setCompositionType(static_cast<HWC2::Composition>(compositionType)))
702 .WillOnce(Return(kError));
703 }
704
705 void expectNoSetCompositionTypeCall() {
706 EXPECT_CALL(*mHwcLayer, setCompositionType(_)).Times(0);
707 }
708
709 void expectSetColorCall() {
710 hwc_color_t color = {static_cast<uint8_t>(std::round(kColor.r * 255)),
711 static_cast<uint8_t>(std::round(kColor.g * 255)),
712 static_cast<uint8_t>(std::round(kColor.b * 255)), 255};
713
714 EXPECT_CALL(*mHwcLayer, setColor(ColorEq(color))).WillOnce(Return(kError));
715 }
716
717 void expectSetSidebandHandleCall() {
718 EXPECT_CALL(*mHwcLayer, setSidebandStream(kSidebandStreamHandle));
719 }
720
721 void expectSetHdrMetadataAndBufferCalls() {
722 EXPECT_CALL(*mHwcLayer, setPerFrameMetadata(kSupportedPerFrameMetadata, kHdrMetadata));
723 EXPECT_CALL(*mHwcLayer, setBuffer(kExpectedHwcSlot, kBuffer, kFence));
724 }
725
Lloyd Piquea83776c2019-01-29 18:42:32 -0800726 std::shared_ptr<HWC2::mock::Layer> mHwcLayer{std::make_shared<StrictMock<HWC2::mock::Layer>>()};
Lloyd Piquef5275482019-01-29 18:42:42 -0800727 StrictMock<mock::DisplayColorProfile> mDisplayColorProfile;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800728};
729
Lloyd Piquef5275482019-01-29 18:42:42 -0800730const half4 OutputLayerWriteStateToHWCTest::kColor{81.f / 255.f, 82.f / 255.f, 83.f / 255.f,
731 84.f / 255.f};
Lloyd Piquea83776c2019-01-29 18:42:32 -0800732const Rect OutputLayerWriteStateToHWCTest::kDisplayFrame{1001, 1002, 1003, 10044};
Lloyd Piquea2468662019-03-07 21:31:06 -0800733const Region OutputLayerWriteStateToHWCTest::kOutputSpaceVisibleRegion{
734 Rect{1005, 1006, 1007, 1008}};
Lloyd Piquef5275482019-01-29 18:42:42 -0800735const mat4 OutputLayerWriteStateToHWCTest::kColorTransform{
736 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016,
737 1017, 1018, 1019, 1020, 1021, 1022, 1023, 1024,
738};
739const Region OutputLayerWriteStateToHWCTest::kSurfaceDamage{Rect{1025, 1026, 1027, 1028}};
740const HdrMetadata OutputLayerWriteStateToHWCTest::kHdrMetadata{{/* LightFlattenable */}, 1029};
741native_handle_t* OutputLayerWriteStateToHWCTest::kSidebandStreamHandle =
742 reinterpret_cast<native_handle_t*>(1031);
743const sp<GraphicBuffer> OutputLayerWriteStateToHWCTest::kBuffer;
744const sp<Fence> OutputLayerWriteStateToHWCTest::kFence;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800745
746TEST_F(OutputLayerWriteStateToHWCTest, doesNothingIfNoHWCState) {
747 mOutputLayer.editState().hwc.reset();
748
749 mOutputLayer.writeStateToHWC(true);
750}
751
752TEST_F(OutputLayerWriteStateToHWCTest, doesNothingIfNoHWCLayer) {
753 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc(nullptr);
754
755 mOutputLayer.writeStateToHWC(true);
756}
757
Lloyd Piquef5275482019-01-29 18:42:42 -0800758TEST_F(OutputLayerWriteStateToHWCTest, canSetAllState) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800759 expectGeometryCommonCalls();
Lloyd Piquef5275482019-01-29 18:42:42 -0800760 expectPerFrameCommonCalls();
761
762 expectNoSetCompositionTypeCall();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800763
764 mOutputLayer.writeStateToHWC(true);
765}
766
Lloyd Piquef5275482019-01-29 18:42:42 -0800767TEST_F(OutputLayerWriteStateToHWCTest, canSetPerFrameStateForSolidColor) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700768 mLayerFEState.compositionType = Hwc2::IComposerClient::Composition::SOLID_COLOR;
Lloyd Piquef5275482019-01-29 18:42:42 -0800769
770 expectPerFrameCommonCalls();
Lloyd Pique46b72df2019-10-29 13:19:27 -0700771
772 // Setting the composition type should happen before setting the color. We
773 // check this in this test only by setting up an testing::InSeqeuence
774 // instance before setting up the two expectations.
775 InSequence s;
Lloyd Piquef5275482019-01-29 18:42:42 -0800776 expectSetCompositionTypeCall(Hwc2::IComposerClient::Composition::SOLID_COLOR);
Lloyd Pique46b72df2019-10-29 13:19:27 -0700777 expectSetColorCall();
Lloyd Piquef5275482019-01-29 18:42:42 -0800778
779 mOutputLayer.writeStateToHWC(false);
780}
781
782TEST_F(OutputLayerWriteStateToHWCTest, canSetPerFrameStateForSideband) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700783 mLayerFEState.compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
Lloyd Piquef5275482019-01-29 18:42:42 -0800784
785 expectPerFrameCommonCalls();
786 expectSetSidebandHandleCall();
787 expectSetCompositionTypeCall(Hwc2::IComposerClient::Composition::SIDEBAND);
788
789 mOutputLayer.writeStateToHWC(false);
790}
791
792TEST_F(OutputLayerWriteStateToHWCTest, canSetPerFrameStateForCursor) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700793 mLayerFEState.compositionType = Hwc2::IComposerClient::Composition::CURSOR;
Lloyd Piquef5275482019-01-29 18:42:42 -0800794
795 expectPerFrameCommonCalls();
796 expectSetHdrMetadataAndBufferCalls();
797 expectSetCompositionTypeCall(Hwc2::IComposerClient::Composition::CURSOR);
798
799 mOutputLayer.writeStateToHWC(false);
800}
801
802TEST_F(OutputLayerWriteStateToHWCTest, canSetPerFrameStateForDevice) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700803 mLayerFEState.compositionType = Hwc2::IComposerClient::Composition::DEVICE;
Lloyd Piquef5275482019-01-29 18:42:42 -0800804
805 expectPerFrameCommonCalls();
806 expectSetHdrMetadataAndBufferCalls();
807 expectSetCompositionTypeCall(Hwc2::IComposerClient::Composition::DEVICE);
808
809 mOutputLayer.writeStateToHWC(false);
810}
811
812TEST_F(OutputLayerWriteStateToHWCTest, compositionTypeIsNotSetIfUnchanged) {
813 (*mOutputLayer.editState().hwc).hwcCompositionType =
814 Hwc2::IComposerClient::Composition::SOLID_COLOR;
815
Lloyd Pique9755fb72019-03-26 14:44:40 -0700816 mLayerFEState.compositionType = Hwc2::IComposerClient::Composition::SOLID_COLOR;
Lloyd Piquef5275482019-01-29 18:42:42 -0800817
818 expectPerFrameCommonCalls();
819 expectSetColorCall();
820 expectNoSetCompositionTypeCall();
821
822 mOutputLayer.writeStateToHWC(false);
823}
824
825TEST_F(OutputLayerWriteStateToHWCTest, compositionTypeIsSetToClientIfColorTransformNotSupported) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700826 mLayerFEState.compositionType = Hwc2::IComposerClient::Composition::SOLID_COLOR;
Lloyd Piquef5275482019-01-29 18:42:42 -0800827
828 expectPerFrameCommonCalls(SimulateUnsupported::ColorTransform);
829 expectSetColorCall();
830 expectSetCompositionTypeCall(Hwc2::IComposerClient::Composition::CLIENT);
831
832 mOutputLayer.writeStateToHWC(false);
833}
834
835TEST_F(OutputLayerWriteStateToHWCTest, compositionTypeIsSetToClientIfClientCompositionForced) {
836 mOutputLayer.editState().forceClientComposition = true;
837
Lloyd Pique9755fb72019-03-26 14:44:40 -0700838 mLayerFEState.compositionType = Hwc2::IComposerClient::Composition::SOLID_COLOR;
Lloyd Piquef5275482019-01-29 18:42:42 -0800839
840 expectPerFrameCommonCalls();
841 expectSetColorCall();
842 expectSetCompositionTypeCall(Hwc2::IComposerClient::Composition::CLIENT);
843
844 mOutputLayer.writeStateToHWC(false);
845}
846
Lloyd Pique66d68602019-02-13 14:23:31 -0800847/*
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800848 * OutputLayer::writeCursorPositionToHWC()
849 */
850
851struct OutputLayerWriteCursorPositionToHWCTest : public OutputLayerTest {
852 static constexpr int kDefaultTransform = TR_IDENT;
853 static constexpr HWC2::Error kDefaultError = HWC2::Error::Unsupported;
854
855 static const Rect kDefaultDisplayViewport;
856 static const Rect kDefaultCursorFrame;
857
858 OutputLayerWriteCursorPositionToHWCTest() {
859 auto& outputLayerState = mOutputLayer.editState();
860 outputLayerState.hwc = impl::OutputLayerCompositionState::Hwc(mHwcLayer);
861
Lloyd Pique9755fb72019-03-26 14:44:40 -0700862 mLayerFEState.cursorFrame = kDefaultCursorFrame;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800863
864 mOutputState.viewport = kDefaultDisplayViewport;
865 mOutputState.transform = ui::Transform{kDefaultTransform};
866 }
867
868 std::shared_ptr<HWC2::mock::Layer> mHwcLayer{std::make_shared<StrictMock<HWC2::mock::Layer>>()};
869};
870
871const Rect OutputLayerWriteCursorPositionToHWCTest::kDefaultDisplayViewport{0, 0, 1920, 1080};
872const Rect OutputLayerWriteCursorPositionToHWCTest::kDefaultCursorFrame{1, 2, 3, 4};
873
874TEST_F(OutputLayerWriteCursorPositionToHWCTest, writeCursorPositionToHWCHandlesNoHwcState) {
875 mOutputLayer.editState().hwc.reset();
876
877 mOutputLayer.writeCursorPositionToHWC();
878}
879
880TEST_F(OutputLayerWriteCursorPositionToHWCTest, writeCursorPositionToHWCWritesStateToHWC) {
881 EXPECT_CALL(*mHwcLayer, setCursorPosition(1, 2)).WillOnce(Return(kDefaultError));
882
883 mOutputLayer.writeCursorPositionToHWC();
884}
885
886TEST_F(OutputLayerWriteCursorPositionToHWCTest, writeCursorPositionToHWCIntersectedWithViewport) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700887 mLayerFEState.cursorFrame = Rect{3000, 3000, 3016, 3016};
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800888
889 EXPECT_CALL(*mHwcLayer, setCursorPosition(1920, 1080)).WillOnce(Return(kDefaultError));
890
891 mOutputLayer.writeCursorPositionToHWC();
892}
893
894TEST_F(OutputLayerWriteCursorPositionToHWCTest, writeCursorPositionToHWCRotatedByTransform) {
895 mOutputState.transform = ui::Transform{TR_ROT_90};
896
897 EXPECT_CALL(*mHwcLayer, setCursorPosition(-4, 1)).WillOnce(Return(kDefaultError));
898
899 mOutputLayer.writeCursorPositionToHWC();
900}
901
902/*
Lloyd Pique66d68602019-02-13 14:23:31 -0800903 * OutputLayer::getHwcLayer()
904 */
905
906TEST_F(OutputLayerTest, getHwcLayerHandlesNoHwcState) {
907 mOutputLayer.editState().hwc.reset();
908
909 EXPECT_TRUE(mOutputLayer.getHwcLayer() == nullptr);
910}
911
912TEST_F(OutputLayerTest, getHwcLayerHandlesNoHwcLayer) {
913 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
914
915 EXPECT_TRUE(mOutputLayer.getHwcLayer() == nullptr);
916}
917
918TEST_F(OutputLayerTest, getHwcLayerReturnsHwcLayer) {
919 auto hwcLayer = std::make_shared<StrictMock<HWC2::mock::Layer>>();
920 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{hwcLayer};
921
922 EXPECT_EQ(hwcLayer.get(), mOutputLayer.getHwcLayer());
923}
924
925/*
926 * OutputLayer::requiresClientComposition()
927 */
928
929TEST_F(OutputLayerTest, requiresClientCompositionReturnsTrueIfNoHWC2State) {
930 mOutputLayer.editState().hwc.reset();
931
932 EXPECT_TRUE(mOutputLayer.requiresClientComposition());
933}
934
935TEST_F(OutputLayerTest, requiresClientCompositionReturnsTrueIfSetToClientComposition) {
936 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
937 mOutputLayer.editState().hwc->hwcCompositionType = Hwc2::IComposerClient::Composition::CLIENT;
938
939 EXPECT_TRUE(mOutputLayer.requiresClientComposition());
940}
941
942TEST_F(OutputLayerTest, requiresClientCompositionReturnsFalseIfSetToDeviceComposition) {
943 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
944 mOutputLayer.editState().hwc->hwcCompositionType = Hwc2::IComposerClient::Composition::DEVICE;
945
946 EXPECT_FALSE(mOutputLayer.requiresClientComposition());
947}
948
949/*
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800950 * OutputLayer::isHardwareCursor()
951 */
952
953TEST_F(OutputLayerTest, isHardwareCursorReturnsFalseIfNoHWC2State) {
954 mOutputLayer.editState().hwc.reset();
955
956 EXPECT_FALSE(mOutputLayer.isHardwareCursor());
957}
958
959TEST_F(OutputLayerTest, isHardwareCursorReturnsTrueIfSetToCursorComposition) {
960 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
961 mOutputLayer.editState().hwc->hwcCompositionType = Hwc2::IComposerClient::Composition::CURSOR;
962
963 EXPECT_TRUE(mOutputLayer.isHardwareCursor());
964}
965
966TEST_F(OutputLayerTest, isHardwareCursorReturnsFalseIfSetToDeviceComposition) {
967 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
968 mOutputLayer.editState().hwc->hwcCompositionType = Hwc2::IComposerClient::Composition::DEVICE;
969
970 EXPECT_FALSE(mOutputLayer.isHardwareCursor());
971}
972
973/*
Lloyd Pique66d68602019-02-13 14:23:31 -0800974 * OutputLayer::applyDeviceCompositionTypeChange()
975 */
976
977TEST_F(OutputLayerTest, applyDeviceCompositionTypeChangeSetsNewType) {
978 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
979 mOutputLayer.editState().hwc->hwcCompositionType = Hwc2::IComposerClient::Composition::DEVICE;
980
981 mOutputLayer.applyDeviceCompositionTypeChange(Hwc2::IComposerClient::Composition::CLIENT);
982
983 ASSERT_TRUE(mOutputLayer.getState().hwc);
984 EXPECT_EQ(Hwc2::IComposerClient::Composition::CLIENT,
985 mOutputLayer.getState().hwc->hwcCompositionType);
986}
987
988/*
989 * OutputLayer::prepareForDeviceLayerRequests()
990 */
991
992TEST_F(OutputLayerTest, prepareForDeviceLayerRequestsResetsRequestState) {
993 mOutputLayer.editState().clearClientTarget = true;
994
995 mOutputLayer.prepareForDeviceLayerRequests();
996
997 EXPECT_FALSE(mOutputLayer.getState().clearClientTarget);
998}
999
1000/*
1001 * OutputLayer::applyDeviceLayerRequest()
1002 */
1003
1004TEST_F(OutputLayerTest, applyDeviceLayerRequestHandlesClearClientTarget) {
1005 mOutputLayer.editState().clearClientTarget = false;
1006
1007 mOutputLayer.applyDeviceLayerRequest(Hwc2::IComposerClient::LayerRequest::CLEAR_CLIENT_TARGET);
1008
1009 EXPECT_TRUE(mOutputLayer.getState().clearClientTarget);
1010}
1011
1012TEST_F(OutputLayerTest, applyDeviceLayerRequestHandlesUnknownRequest) {
1013 mOutputLayer.editState().clearClientTarget = false;
1014
1015 mOutputLayer.applyDeviceLayerRequest(static_cast<Hwc2::IComposerClient::LayerRequest>(0));
1016
1017 EXPECT_FALSE(mOutputLayer.getState().clearClientTarget);
1018}
1019
Lloyd Pique688abd42019-02-15 15:42:24 -08001020/*
1021 * OutputLayer::needsFiltering()
1022 */
1023
1024TEST_F(OutputLayerTest, needsFilteringReturnsFalseIfDisplaySizeSameAsSourceSize) {
1025 mOutputLayer.editState().displayFrame = Rect(100, 100, 200, 200);
1026 mOutputLayer.editState().sourceCrop = FloatRect{0.f, 0.f, 100.f, 100.f};
1027
1028 EXPECT_FALSE(mOutputLayer.needsFiltering());
1029}
1030
1031TEST_F(OutputLayerTest, needsFilteringReturnsTrueIfDisplaySizeDifferentFromSourceSize) {
1032 mOutputLayer.editState().displayFrame = Rect(100, 100, 200, 200);
1033 mOutputLayer.editState().sourceCrop = FloatRect{0.f, 0.f, 100.1f, 100.1f};
1034
1035 EXPECT_TRUE(mOutputLayer.needsFiltering());
1036}
1037
Lloyd Piquecc01a452018-12-04 17:24:00 -08001038} // namespace
1039} // namespace android::compositionengine