blob: 88acd04141e64d661af0a05bf9a10d8dc0a46ef5 [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 Pique67e3d9b2019-03-22 23:09:28 +000026#include "FloatRectMatcher.h"
Lloyd Pique07e33212018-12-18 16:33:37 -080027#include "MockHWC2.h"
28#include "MockHWComposer.h"
Lloyd Piquea83776c2019-01-29 18:42:32 -080029#include "RectMatcher.h"
Lloyd Piquef5275482019-01-29 18:42:42 -080030#include "RegionMatcher.h"
Lloyd Pique07e33212018-12-18 16:33:37 -080031
Lloyd Piquecc01a452018-12-04 17:24:00 -080032namespace android::compositionengine {
33namespace {
34
Lloyd Piquea83776c2019-01-29 18:42:32 -080035using testing::_;
Lloyd Pique46b72df2019-10-29 13:19:27 -070036using testing::InSequence;
Lloyd Piquea83776c2019-01-29 18:42:32 -080037using testing::Return;
38using testing::ReturnRef;
Lloyd Piquecc01a452018-12-04 17:24:00 -080039using testing::StrictMock;
40
Lloyd Piquea83776c2019-01-29 18:42:32 -080041constexpr auto TR_IDENT = 0u;
42constexpr auto TR_FLP_H = HAL_TRANSFORM_FLIP_H;
43constexpr auto TR_FLP_V = HAL_TRANSFORM_FLIP_V;
44constexpr auto TR_ROT_90 = HAL_TRANSFORM_ROT_90;
45constexpr auto TR_ROT_180 = TR_FLP_H | TR_FLP_V;
46constexpr auto TR_ROT_270 = TR_ROT_90 | TR_ROT_180;
47
48const std::string kOutputName{"Test Output"};
49
Lloyd Piquef5275482019-01-29 18:42:42 -080050MATCHER_P(ColorEq, expected, "") {
51 *result_listener << "Colors are not equal\n";
52 *result_listener << "expected " << expected.r << " " << expected.g << " " << expected.b << " "
53 << expected.a << "\n";
54 *result_listener << "actual " << arg.r << " " << arg.g << " " << arg.b << " " << arg.a << "\n";
55
56 return expected.r == arg.r && expected.g == arg.g && expected.b == arg.b && expected.a == arg.a;
57}
58
Lloyd Pique66d68602019-02-13 14:23:31 -080059struct OutputLayerTest : public testing::Test {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070060 struct OutputLayer final : public impl::OutputLayer {
61 OutputLayer(const compositionengine::Output& output,
62 std::shared_ptr<compositionengine::Layer> layer,
63 sp<compositionengine::LayerFE> layerFE)
64 : mOutput(output), mLayer(layer), mLayerFE(layerFE) {}
65 ~OutputLayer() override = default;
66
67 // compositionengine::OutputLayer overrides
68 const compositionengine::Output& getOutput() const override { return mOutput; }
69 compositionengine::Layer& getLayer() const override { return *mLayer; }
70 compositionengine::LayerFE& getLayerFE() const override { return *mLayerFE; }
71 const impl::OutputLayerCompositionState& getState() const override { return mState; }
72 impl::OutputLayerCompositionState& editState() override { return mState; }
73
74 // compositionengine::impl::OutputLayer overrides
75 void dumpState(std::string& out) const override { mState.dump(out); }
76
77 const compositionengine::Output& mOutput;
78 std::shared_ptr<compositionengine::Layer> mLayer;
79 sp<compositionengine::LayerFE> mLayerFE;
80 impl::OutputLayerCompositionState mState;
81 };
82
Lloyd Piquea83776c2019-01-29 18:42:32 -080083 OutputLayerTest() {
84 EXPECT_CALL(*mLayerFE, getDebugName()).WillRepeatedly(Return("Test LayerFE"));
85 EXPECT_CALL(mOutput, getName()).WillRepeatedly(ReturnRef(kOutputName));
86
Lloyd Pique9755fb72019-03-26 14:44:40 -070087 EXPECT_CALL(*mLayer, getFEState()).WillRepeatedly(ReturnRef(mLayerFEState));
Lloyd Piquea83776c2019-01-29 18:42:32 -080088 EXPECT_CALL(mOutput, getState()).WillRepeatedly(ReturnRef(mOutputState));
89 }
90
Lloyd Piquecc01a452018-12-04 17:24:00 -080091 compositionengine::mock::Output mOutput;
92 std::shared_ptr<compositionengine::mock::Layer> mLayer{
93 new StrictMock<compositionengine::mock::Layer>()};
94 sp<compositionengine::mock::LayerFE> mLayerFE{
95 new StrictMock<compositionengine::mock::LayerFE>()};
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070096 OutputLayer mOutputLayer{mOutput, mLayer, mLayerFE};
Lloyd Piquea83776c2019-01-29 18:42:32 -080097
Lloyd Pique9755fb72019-03-26 14:44:40 -070098 LayerFECompositionState mLayerFEState;
Lloyd Piquea83776c2019-01-29 18:42:32 -080099 impl::OutputCompositionState mOutputState;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800100};
101
Lloyd Piquea83776c2019-01-29 18:42:32 -0800102/*
Lloyd Piquecc01a452018-12-04 17:24:00 -0800103 * Basic construction
104 */
105
106TEST_F(OutputLayerTest, canInstantiateOutputLayer) {}
107
Lloyd Piquea83776c2019-01-29 18:42:32 -0800108/*
Lloyd Piquedf336d92019-03-07 21:38:42 -0800109 * OutputLayer::setHwcLayer()
Lloyd Pique07e33212018-12-18 16:33:37 -0800110 */
111
Lloyd Piquedf336d92019-03-07 21:38:42 -0800112TEST_F(OutputLayerTest, settingNullHwcLayerSetsEmptyHwcState) {
Lloyd Pique07e33212018-12-18 16:33:37 -0800113 StrictMock<compositionengine::mock::CompositionEngine> compositionEngine;
114
Lloyd Piquedf336d92019-03-07 21:38:42 -0800115 mOutputLayer.setHwcLayer(nullptr);
Lloyd Pique07e33212018-12-18 16:33:37 -0800116
117 EXPECT_FALSE(mOutputLayer.getState().hwc);
118}
119
Lloyd Piquedf336d92019-03-07 21:38:42 -0800120TEST_F(OutputLayerTest, settingHwcLayerSetsHwcState) {
121 auto hwcLayer = std::make_shared<StrictMock<HWC2::mock::Layer>>();
Lloyd Pique07e33212018-12-18 16:33:37 -0800122
Lloyd Piquedf336d92019-03-07 21:38:42 -0800123 mOutputLayer.setHwcLayer(hwcLayer);
Lloyd Pique07e33212018-12-18 16:33:37 -0800124
Lloyd Piquea83776c2019-01-29 18:42:32 -0800125 const auto& outputLayerState = mOutputLayer.getState();
126 ASSERT_TRUE(outputLayerState.hwc);
Lloyd Pique07e33212018-12-18 16:33:37 -0800127
Lloyd Piquea83776c2019-01-29 18:42:32 -0800128 const auto& hwcState = *outputLayerState.hwc;
Lloyd Piquedf336d92019-03-07 21:38:42 -0800129 EXPECT_EQ(hwcLayer, hwcState.hwcLayer);
Lloyd Pique07e33212018-12-18 16:33:37 -0800130}
131
Lloyd Piquea83776c2019-01-29 18:42:32 -0800132/*
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000133 * OutputLayer::calculateOutputSourceCrop()
134 */
135
136struct OutputLayerSourceCropTest : public OutputLayerTest {
137 OutputLayerSourceCropTest() {
138 // Set reasonable default values for a simple case. Each test will
139 // set one specific value to something different.
Lloyd Pique9755fb72019-03-26 14:44:40 -0700140 mLayerFEState.geomUsesSourceCrop = true;
141 mLayerFEState.geomContentCrop = Rect{0, 0, 1920, 1080};
142 mLayerFEState.transparentRegionHint = Region{};
143 mLayerFEState.geomLayerBounds = FloatRect{0.f, 0.f, 1920.f, 1080.f};
144 mLayerFEState.geomLayerTransform = ui::Transform{TR_IDENT};
145 mLayerFEState.geomBufferSize = Rect{0, 0, 1920, 1080};
146 mLayerFEState.geomBufferTransform = TR_IDENT;
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000147
148 mOutputState.viewport = Rect{0, 0, 1920, 1080};
149 }
150
151 FloatRect calculateOutputSourceCrop() {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700152 mLayerFEState.geomInverseLayerTransform = mLayerFEState.geomLayerTransform.inverse();
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000153
154 return mOutputLayer.calculateOutputSourceCrop();
155 }
156};
157
158TEST_F(OutputLayerSourceCropTest, computesEmptyIfSourceCropNotUsed) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700159 mLayerFEState.geomUsesSourceCrop = false;
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000160
161 const FloatRect expected{};
162 EXPECT_THAT(calculateOutputSourceCrop(), FloatRectEq(expected));
163}
164
165TEST_F(OutputLayerSourceCropTest, correctForSimpleDefaultCase) {
166 const FloatRect expected{0.f, 0.f, 1920.f, 1080.f};
167 EXPECT_THAT(calculateOutputSourceCrop(), FloatRectEq(expected));
168}
169
170TEST_F(OutputLayerSourceCropTest, handlesBoundsOutsideViewport) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700171 mLayerFEState.geomLayerBounds = FloatRect{-2000.f, -2000.f, 2000.f, 2000.f};
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000172
173 const FloatRect expected{0.f, 0.f, 1920.f, 1080.f};
174 EXPECT_THAT(calculateOutputSourceCrop(), FloatRectEq(expected));
175}
176
177TEST_F(OutputLayerSourceCropTest, handlesBoundsOutsideViewportRotated) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700178 mLayerFEState.geomLayerBounds = FloatRect{-2000.f, -2000.f, 2000.f, 2000.f};
179 mLayerFEState.geomLayerTransform.set(HAL_TRANSFORM_ROT_90, 1920, 1080);
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000180
181 const FloatRect expected{0.f, 0.f, 1080.f, 1080.f};
182 EXPECT_THAT(calculateOutputSourceCrop(), FloatRectEq(expected));
183}
184
185TEST_F(OutputLayerSourceCropTest, calculateOutputSourceCropWorksWithATransformedBuffer) {
186 struct Entry {
187 uint32_t bufferInvDisplay;
188 uint32_t buffer;
189 uint32_t display;
190 FloatRect expected;
191 };
192 // Not an exhaustive list of cases, but hopefully enough.
193 const std::array<Entry, 12> testData = {
194 // clang-format off
195 // inv buffer display expected
196 /* 0 */ Entry{false, TR_IDENT, TR_IDENT, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
197 /* 1 */ Entry{false, TR_IDENT, TR_ROT_90, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
198 /* 2 */ Entry{false, TR_IDENT, TR_ROT_180, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
199 /* 3 */ Entry{false, TR_IDENT, TR_ROT_270, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
200
201 /* 4 */ Entry{true, TR_IDENT, TR_IDENT, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
202 /* 5 */ Entry{true, TR_IDENT, TR_ROT_90, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
203 /* 6 */ Entry{true, TR_IDENT, TR_ROT_180, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
204 /* 7 */ Entry{true, TR_IDENT, TR_ROT_270, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
205
206 /* 8 */ Entry{false, TR_IDENT, TR_IDENT, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
207 /* 9 */ Entry{false, TR_ROT_90, TR_ROT_90, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
208 /* 10 */ Entry{false, TR_ROT_180, TR_ROT_180, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
209 /* 11 */ Entry{false, TR_ROT_270, TR_ROT_270, FloatRect{0.f, 0.f, 1920.f, 1080.f}},
210
211 // clang-format on
212 };
213
214 for (size_t i = 0; i < testData.size(); i++) {
215 const auto& entry = testData[i];
216
Lloyd Pique9755fb72019-03-26 14:44:40 -0700217 mLayerFEState.geomBufferUsesDisplayInverseTransform = entry.bufferInvDisplay;
218 mLayerFEState.geomBufferTransform = entry.buffer;
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000219 mOutputState.orientation = entry.display;
220
221 EXPECT_THAT(calculateOutputSourceCrop(), FloatRectEq(entry.expected)) << "entry " << i;
222 }
223}
224
225TEST_F(OutputLayerSourceCropTest, geomContentCropAffectsCrop) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700226 mLayerFEState.geomContentCrop = Rect{0, 0, 960, 540};
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000227
228 const FloatRect expected{0.f, 0.f, 960.f, 540.f};
229 EXPECT_THAT(calculateOutputSourceCrop(), FloatRectEq(expected));
230}
231
232TEST_F(OutputLayerSourceCropTest, viewportAffectsCrop) {
233 mOutputState.viewport = Rect{0, 0, 960, 540};
234
235 const FloatRect expected{0.f, 0.f, 960.f, 540.f};
236 EXPECT_THAT(calculateOutputSourceCrop(), FloatRectEq(expected));
237}
238
239/*
Lloyd Piquea83776c2019-01-29 18:42:32 -0800240 * OutputLayer::calculateOutputDisplayFrame()
241 */
242
243struct OutputLayerDisplayFrameTest : public OutputLayerTest {
244 OutputLayerDisplayFrameTest() {
245 // Set reasonable default values for a simple case. Each test will
246 // set one specific value to something different.
247
Lloyd Pique9755fb72019-03-26 14:44:40 -0700248 mLayerFEState.transparentRegionHint = Region{};
249 mLayerFEState.geomLayerTransform = ui::Transform{TR_IDENT};
250 mLayerFEState.geomBufferSize = Rect{0, 0, 1920, 1080};
251 mLayerFEState.geomBufferUsesDisplayInverseTransform = false;
252 mLayerFEState.geomCrop = Rect{0, 0, 1920, 1080};
253 mLayerFEState.geomLayerBounds = FloatRect{0.f, 0.f, 1920.f, 1080.f};
Lloyd Piquea83776c2019-01-29 18:42:32 -0800254
255 mOutputState.viewport = Rect{0, 0, 1920, 1080};
256 mOutputState.transform = ui::Transform{TR_IDENT};
257 }
258
259 Rect calculateOutputDisplayFrame() {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700260 mLayerFEState.geomInverseLayerTransform = mLayerFEState.geomLayerTransform.inverse();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800261
262 return mOutputLayer.calculateOutputDisplayFrame();
263 }
264};
265
266TEST_F(OutputLayerDisplayFrameTest, correctForSimpleDefaultCase) {
267 const Rect expected{0, 0, 1920, 1080};
268 EXPECT_THAT(calculateOutputDisplayFrame(), RectEq(expected));
269}
270
271TEST_F(OutputLayerDisplayFrameTest, fullActiveTransparentRegionReturnsEmptyFrame) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700272 mLayerFEState.transparentRegionHint = Region{Rect{0, 0, 1920, 1080}};
Lloyd Piquea83776c2019-01-29 18:42:32 -0800273 const Rect expected{0, 0, 0, 0};
274 EXPECT_THAT(calculateOutputDisplayFrame(), RectEq(expected));
275}
276
277TEST_F(OutputLayerDisplayFrameTest, cropAffectsDisplayFrame) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700278 mLayerFEState.geomCrop = Rect{100, 200, 300, 500};
Lloyd Piquea83776c2019-01-29 18:42:32 -0800279 const Rect expected{100, 200, 300, 500};
280 EXPECT_THAT(calculateOutputDisplayFrame(), RectEq(expected));
281}
282
283TEST_F(OutputLayerDisplayFrameTest, cropAffectsDisplayFrameRotated) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700284 mLayerFEState.geomCrop = Rect{100, 200, 300, 500};
285 mLayerFEState.geomLayerTransform.set(HAL_TRANSFORM_ROT_90, 1920, 1080);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800286 const Rect expected{1420, 100, 1720, 300};
287 EXPECT_THAT(calculateOutputDisplayFrame(), RectEq(expected));
288}
289
290TEST_F(OutputLayerDisplayFrameTest, emptyGeomCropIsNotUsedToComputeFrame) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700291 mLayerFEState.geomCrop = Rect{};
Lloyd Piquea83776c2019-01-29 18:42:32 -0800292 const Rect expected{0, 0, 1920, 1080};
293 EXPECT_THAT(calculateOutputDisplayFrame(), RectEq(expected));
294}
295
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000296TEST_F(OutputLayerDisplayFrameTest, geomLayerBoundsAffectsFrame) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700297 mLayerFEState.geomLayerBounds = FloatRect{0.f, 0.f, 960.f, 540.f};
Lloyd Piquea83776c2019-01-29 18:42:32 -0800298 const Rect expected{0, 0, 960, 540};
299 EXPECT_THAT(calculateOutputDisplayFrame(), RectEq(expected));
300}
301
302TEST_F(OutputLayerDisplayFrameTest, viewportAffectsFrame) {
303 mOutputState.viewport = Rect{0, 0, 960, 540};
304 const Rect expected{0, 0, 960, 540};
305 EXPECT_THAT(calculateOutputDisplayFrame(), RectEq(expected));
306}
307
308TEST_F(OutputLayerDisplayFrameTest, outputTransformAffectsDisplayFrame) {
309 mOutputState.transform = ui::Transform{HAL_TRANSFORM_ROT_90};
310 const Rect expected{-1080, 0, 0, 1920};
311 EXPECT_THAT(calculateOutputDisplayFrame(), RectEq(expected));
312}
313
314/*
315 * OutputLayer::calculateOutputRelativeBufferTransform()
316 */
317
318TEST_F(OutputLayerTest, calculateOutputRelativeBufferTransformTestsNeeded) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700319 mLayerFEState.geomBufferUsesDisplayInverseTransform = false;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800320
321 struct Entry {
322 uint32_t layer;
323 uint32_t buffer;
324 uint32_t display;
325 uint32_t expected;
326 };
327 // Not an exhaustive list of cases, but hopefully enough.
328 const std::array<Entry, 24> testData = {
329 // clang-format off
330 // layer buffer display expected
331 /* 0 */ Entry{TR_IDENT, TR_IDENT, TR_IDENT, TR_IDENT},
332 /* 1 */ Entry{TR_IDENT, TR_IDENT, TR_ROT_90, TR_ROT_90},
333 /* 2 */ Entry{TR_IDENT, TR_IDENT, TR_ROT_180, TR_ROT_180},
334 /* 3 */ Entry{TR_IDENT, TR_IDENT, TR_ROT_270, TR_ROT_270},
335
336 /* 4 */ Entry{TR_IDENT, TR_FLP_H, TR_IDENT, TR_FLP_H ^ TR_IDENT},
337 /* 5 */ Entry{TR_IDENT, TR_FLP_H, TR_ROT_90, TR_FLP_H ^ TR_ROT_90},
338 /* 6 */ Entry{TR_IDENT, TR_FLP_H, TR_ROT_180, TR_FLP_H ^ TR_ROT_180},
339 /* 7 */ Entry{TR_IDENT, TR_FLP_H, TR_ROT_270, TR_FLP_H ^ TR_ROT_270},
340
341 /* 8 */ Entry{TR_IDENT, TR_FLP_V, TR_IDENT, TR_FLP_V},
342 /* 9 */ Entry{TR_IDENT, TR_ROT_90, TR_ROT_90, TR_ROT_180},
343 /* 10 */ Entry{TR_IDENT, TR_ROT_180, TR_ROT_180, TR_IDENT},
344 /* 11 */ Entry{TR_IDENT, TR_ROT_270, TR_ROT_270, TR_ROT_180},
345
346 /* 12 */ Entry{TR_ROT_90, TR_IDENT, TR_IDENT, TR_IDENT ^ TR_ROT_90},
347 /* 13 */ Entry{TR_ROT_90, TR_FLP_H, TR_ROT_90, TR_FLP_H ^ TR_ROT_180},
348 /* 14 */ Entry{TR_ROT_90, TR_IDENT, TR_ROT_180, TR_IDENT ^ TR_ROT_270},
349 /* 15 */ Entry{TR_ROT_90, TR_FLP_H, TR_ROT_270, TR_FLP_H ^ TR_IDENT},
350
351 /* 16 */ Entry{TR_ROT_180, TR_FLP_H, TR_IDENT, TR_FLP_H ^ TR_ROT_180},
352 /* 17 */ Entry{TR_ROT_180, TR_IDENT, TR_ROT_90, TR_IDENT ^ TR_ROT_270},
353 /* 18 */ Entry{TR_ROT_180, TR_FLP_H, TR_ROT_180, TR_FLP_H ^ TR_IDENT},
354 /* 19 */ Entry{TR_ROT_180, TR_IDENT, TR_ROT_270, TR_IDENT ^ TR_ROT_90},
355
356 /* 20 */ Entry{TR_ROT_270, TR_IDENT, TR_IDENT, TR_IDENT ^ TR_ROT_270},
357 /* 21 */ Entry{TR_ROT_270, TR_FLP_H, TR_ROT_90, TR_FLP_H ^ TR_IDENT},
358 /* 22 */ Entry{TR_ROT_270, TR_FLP_H, TR_ROT_180, TR_FLP_H ^ TR_ROT_90},
359 /* 23 */ Entry{TR_ROT_270, TR_IDENT, TR_ROT_270, TR_IDENT ^ TR_ROT_180},
360 // clang-format on
361 };
362
363 for (size_t i = 0; i < testData.size(); i++) {
364 const auto& entry = testData[i];
365
Lloyd Pique9755fb72019-03-26 14:44:40 -0700366 mLayerFEState.geomLayerTransform.set(entry.layer, 1920, 1080);
367 mLayerFEState.geomBufferTransform = entry.buffer;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800368 mOutputState.orientation = entry.display;
369
370 auto actual = mOutputLayer.calculateOutputRelativeBufferTransform();
371 EXPECT_EQ(entry.expected, actual) << "entry " << i;
372 }
373}
374
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000375TEST_F(OutputLayerTest,
376 calculateOutputRelativeBufferTransformTestWithOfBufferUsesDisplayInverseTransform) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700377 mLayerFEState.geomBufferUsesDisplayInverseTransform = true;
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000378
379 struct Entry {
380 uint32_t layer;
381 uint32_t buffer;
382 uint32_t display;
383 uint32_t expected;
384 };
385 // Not an exhaustive list of cases, but hopefully enough.
386 const std::array<Entry, 24> testData = {
387 // clang-format off
388 // layer buffer display expected
389 /* 0 */ Entry{TR_IDENT, TR_IDENT, TR_IDENT, TR_IDENT},
390 /* 1 */ Entry{TR_IDENT, TR_IDENT, TR_ROT_90, TR_IDENT},
391 /* 2 */ Entry{TR_IDENT, TR_IDENT, TR_ROT_180, TR_IDENT},
392 /* 3 */ Entry{TR_IDENT, TR_IDENT, TR_ROT_270, TR_IDENT},
393
394 /* 4 */ Entry{TR_IDENT, TR_FLP_H, TR_IDENT, TR_FLP_H},
395 /* 5 */ Entry{TR_IDENT, TR_FLP_H, TR_ROT_90, TR_FLP_H},
396 /* 6 */ Entry{TR_IDENT, TR_FLP_H, TR_ROT_180, TR_FLP_H},
397 /* 7 */ Entry{TR_IDENT, TR_FLP_H, TR_ROT_270, TR_FLP_H},
398
399 /* 8 */ Entry{TR_IDENT, TR_FLP_V, TR_IDENT, TR_FLP_V},
400 /* 9 */ Entry{TR_IDENT, TR_ROT_90, TR_ROT_90, TR_ROT_90},
401 /* 10 */ Entry{TR_IDENT, TR_ROT_180, TR_ROT_180, TR_ROT_180},
402 /* 11 */ Entry{TR_IDENT, TR_ROT_270, TR_ROT_270, TR_ROT_270},
403
404 /* 12 */ Entry{TR_ROT_90, TR_IDENT, TR_IDENT, TR_IDENT},
405 /* 13 */ Entry{TR_ROT_90, TR_FLP_H, TR_ROT_90, TR_FLP_H},
406 /* 14 */ Entry{TR_ROT_90, TR_IDENT, TR_ROT_180, TR_IDENT},
407 /* 15 */ Entry{TR_ROT_90, TR_FLP_H, TR_ROT_270, TR_FLP_H},
408
409 /* 16 */ Entry{TR_ROT_180, TR_FLP_H, TR_IDENT, TR_FLP_H},
410 /* 17 */ Entry{TR_ROT_180, TR_IDENT, TR_ROT_90, TR_IDENT},
411 /* 18 */ Entry{TR_ROT_180, TR_FLP_H, TR_ROT_180, TR_FLP_H},
412 /* 19 */ Entry{TR_ROT_180, TR_IDENT, TR_ROT_270, TR_IDENT},
413
414 /* 20 */ Entry{TR_ROT_270, TR_IDENT, TR_IDENT, TR_IDENT},
415 /* 21 */ Entry{TR_ROT_270, TR_FLP_H, TR_ROT_90, TR_FLP_H},
416 /* 22 */ Entry{TR_ROT_270, TR_FLP_H, TR_ROT_180, TR_FLP_H},
417 /* 23 */ Entry{TR_ROT_270, TR_IDENT, TR_ROT_270, TR_IDENT},
418 // clang-format on
419 };
420
421 for (size_t i = 0; i < testData.size(); i++) {
422 const auto& entry = testData[i];
423
Lloyd Pique9755fb72019-03-26 14:44:40 -0700424 mLayerFEState.geomLayerTransform = ui::Transform{entry.layer};
425 mLayerFEState.geomBufferTransform = entry.buffer;
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000426 mOutputState.orientation = entry.display;
427
428 auto actual = mOutputLayer.calculateOutputRelativeBufferTransform();
429 EXPECT_EQ(entry.expected, actual) << "entry " << i;
430 }
431}
432
433/*
434 * OutputLayer::updateCompositionState()
435 */
436
437struct OutputLayerPartialMockForUpdateCompositionState : public impl::OutputLayer {
438 OutputLayerPartialMockForUpdateCompositionState(const compositionengine::Output& output,
439 std::shared_ptr<compositionengine::Layer> layer,
440 sp<compositionengine::LayerFE> layerFE)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700441 : mOutput(output), mLayer(layer), mLayerFE(layerFE) {}
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000442 // Mock everything called by updateCompositionState to simplify testing it.
443 MOCK_CONST_METHOD0(calculateOutputSourceCrop, FloatRect());
444 MOCK_CONST_METHOD0(calculateOutputDisplayFrame, Rect());
445 MOCK_CONST_METHOD0(calculateOutputRelativeBufferTransform, uint32_t());
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700446
447 // compositionengine::OutputLayer overrides
448 const compositionengine::Output& getOutput() const override { return mOutput; }
449 compositionengine::Layer& getLayer() const override { return *mLayer; }
450 compositionengine::LayerFE& getLayerFE() const override { return *mLayerFE; }
451 const impl::OutputLayerCompositionState& getState() const override { return mState; }
452 impl::OutputLayerCompositionState& editState() override { return mState; }
453
454 // These need implementations though are not expected to be called.
455 MOCK_CONST_METHOD1(dumpState, void(std::string&));
456
457 const compositionengine::Output& mOutput;
458 std::shared_ptr<compositionengine::Layer> mLayer;
459 sp<compositionengine::LayerFE> mLayerFE;
460 impl::OutputLayerCompositionState mState;
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000461};
462
463struct OutputLayerUpdateCompositionStateTest : public OutputLayerTest {
464public:
465 OutputLayerUpdateCompositionStateTest() {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700466 EXPECT_CALL(*mLayer, getFEState()).WillRepeatedly(ReturnRef(mLayerFEState));
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000467 EXPECT_CALL(mOutput, getState()).WillRepeatedly(ReturnRef(mOutputState));
Lloyd Piquef5275482019-01-29 18:42:42 -0800468 EXPECT_CALL(mOutput, getDisplayColorProfile())
469 .WillRepeatedly(Return(&mDisplayColorProfile));
470 EXPECT_CALL(mDisplayColorProfile, isDataspaceSupported(_)).WillRepeatedly(Return(true));
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000471 }
472
473 ~OutputLayerUpdateCompositionStateTest() = default;
474
475 void setupGeometryChildCallValues() {
476 EXPECT_CALL(mOutputLayer, calculateOutputSourceCrop()).WillOnce(Return(kSourceCrop));
477 EXPECT_CALL(mOutputLayer, calculateOutputDisplayFrame()).WillOnce(Return(kDisplayFrame));
478 EXPECT_CALL(mOutputLayer, calculateOutputRelativeBufferTransform())
479 .WillOnce(Return(mBufferTransform));
480 }
481
482 void validateComputedGeometryState() {
483 const auto& state = mOutputLayer.getState();
484 EXPECT_EQ(kSourceCrop, state.sourceCrop);
485 EXPECT_EQ(kDisplayFrame, state.displayFrame);
486 EXPECT_EQ(static_cast<Hwc2::Transform>(mBufferTransform), state.bufferTransform);
487 }
488
489 const FloatRect kSourceCrop{1.f, 2.f, 3.f, 4.f};
490 const Rect kDisplayFrame{11, 12, 13, 14};
491 uint32_t mBufferTransform{21};
492
493 using OutputLayer = OutputLayerPartialMockForUpdateCompositionState;
494 StrictMock<OutputLayer> mOutputLayer{mOutput, mLayer, mLayerFE};
Lloyd Piquef5275482019-01-29 18:42:42 -0800495 StrictMock<mock::DisplayColorProfile> mDisplayColorProfile;
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000496};
497
498TEST_F(OutputLayerUpdateCompositionStateTest, setsStateNormally) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700499 mLayerFEState.isSecure = true;
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000500 mOutputState.isSecure = true;
Lloyd Piquefe671022019-09-24 10:43:03 -0700501 mOutputLayer.editState().forceClientComposition = true;
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000502
503 setupGeometryChildCallValues();
504
Lloyd Pique7a234912019-10-03 11:54:27 -0700505 mOutputLayer.updateCompositionState(true, false);
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000506
507 validateComputedGeometryState();
508
509 EXPECT_EQ(false, mOutputLayer.getState().forceClientComposition);
510}
511
512TEST_F(OutputLayerUpdateCompositionStateTest,
513 alsoSetsForceCompositionIfSecureLayerOnNonsecureOutput) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700514 mLayerFEState.isSecure = true;
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000515 mOutputState.isSecure = false;
516
517 setupGeometryChildCallValues();
518
Lloyd Pique7a234912019-10-03 11:54:27 -0700519 mOutputLayer.updateCompositionState(true, false);
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000520
521 validateComputedGeometryState();
522
523 EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
524}
525
526TEST_F(OutputLayerUpdateCompositionStateTest,
527 alsoSetsForceCompositionIfUnsupportedBufferTransform) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700528 mLayerFEState.isSecure = true;
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000529 mOutputState.isSecure = true;
530
531 mBufferTransform = ui::Transform::ROT_INVALID;
532
533 setupGeometryChildCallValues();
534
Lloyd Pique7a234912019-10-03 11:54:27 -0700535 mOutputLayer.updateCompositionState(true, false);
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000536
537 validateComputedGeometryState();
538
539 EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
540}
541
Lloyd Piquef5275482019-01-29 18:42:42 -0800542TEST_F(OutputLayerUpdateCompositionStateTest, setsOutputLayerColorspaceCorrectly) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700543 mLayerFEState.dataspace = ui::Dataspace::DISPLAY_P3;
Lloyd Piquef5275482019-01-29 18:42:42 -0800544 mOutputState.targetDataspace = ui::Dataspace::V0_SCRGB;
545
546 // If the layer is not colorspace agnostic, the output layer dataspace
547 // should use the layers requested colorspace.
Lloyd Pique9755fb72019-03-26 14:44:40 -0700548 mLayerFEState.isColorspaceAgnostic = false;
Lloyd Piquef5275482019-01-29 18:42:42 -0800549
Lloyd Pique7a234912019-10-03 11:54:27 -0700550 mOutputLayer.updateCompositionState(false, false);
Lloyd Piquef5275482019-01-29 18:42:42 -0800551
552 EXPECT_EQ(ui::Dataspace::DISPLAY_P3, mOutputLayer.getState().dataspace);
553
554 // If the layer is colorspace agnostic, the output layer dataspace
555 // should use the colorspace chosen for the whole output.
Lloyd Pique9755fb72019-03-26 14:44:40 -0700556 mLayerFEState.isColorspaceAgnostic = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800557
Lloyd Pique7a234912019-10-03 11:54:27 -0700558 mOutputLayer.updateCompositionState(false, false);
Lloyd Piquef5275482019-01-29 18:42:42 -0800559
560 EXPECT_EQ(ui::Dataspace::V0_SCRGB, mOutputLayer.getState().dataspace);
561}
562
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000563TEST_F(OutputLayerUpdateCompositionStateTest, doesNotRecomputeGeometryIfNotRequested) {
Lloyd Piquefe671022019-09-24 10:43:03 -0700564 mOutputLayer.editState().forceClientComposition = false;
565
Lloyd Pique7a234912019-10-03 11:54:27 -0700566 mOutputLayer.updateCompositionState(false, false);
Lloyd Pique67e3d9b2019-03-22 23:09:28 +0000567
568 EXPECT_EQ(false, mOutputLayer.getState().forceClientComposition);
569}
570
Lloyd Piquefe671022019-09-24 10:43:03 -0700571TEST_F(OutputLayerUpdateCompositionStateTest,
572 doesNotClearForceClientCompositionIfNotDoingGeometry) {
573 mOutputLayer.editState().forceClientComposition = true;
574
Lloyd Pique7a234912019-10-03 11:54:27 -0700575 mOutputLayer.updateCompositionState(false, false);
Lloyd Piquefe671022019-09-24 10:43:03 -0700576
577 EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
578}
579
Lloyd Piquef5275482019-01-29 18:42:42 -0800580TEST_F(OutputLayerUpdateCompositionStateTest, clientCompositionForcedFromFrontEndFlagAtAnyTime) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700581 mLayerFEState.forceClientComposition = true;
Lloyd Piquefe671022019-09-24 10:43:03 -0700582 mOutputLayer.editState().forceClientComposition = false;
Lloyd Piquef5275482019-01-29 18:42:42 -0800583
Lloyd Pique7a234912019-10-03 11:54:27 -0700584 mOutputLayer.updateCompositionState(false, false);
Lloyd Piquef5275482019-01-29 18:42:42 -0800585
586 EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
587}
588
589TEST_F(OutputLayerUpdateCompositionStateTest,
590 clientCompositionForcedFromUnsupportedDataspaceAtAnyTime) {
Lloyd Piquefe671022019-09-24 10:43:03 -0700591 mOutputLayer.editState().forceClientComposition = false;
Lloyd Piquef5275482019-01-29 18:42:42 -0800592 EXPECT_CALL(mDisplayColorProfile, isDataspaceSupported(_)).WillRepeatedly(Return(false));
593
Lloyd Pique7a234912019-10-03 11:54:27 -0700594 mOutputLayer.updateCompositionState(false, false);
595
596 EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
597}
598
599TEST_F(OutputLayerUpdateCompositionStateTest, clientCompositionForcedFromArgumentFlag) {
600 mLayerFEState.forceClientComposition = false;
601 mOutputLayer.editState().forceClientComposition = false;
602
603 mOutputLayer.updateCompositionState(false, true);
604
605 EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
606
607 mOutputLayer.editState().forceClientComposition = false;
608
609 setupGeometryChildCallValues();
610
611 mOutputLayer.updateCompositionState(true, true);
Lloyd Piquef5275482019-01-29 18:42:42 -0800612
613 EXPECT_EQ(true, mOutputLayer.getState().forceClientComposition);
614}
615
Lloyd Piquea83776c2019-01-29 18:42:32 -0800616/*
617 * OutputLayer::writeStateToHWC()
618 */
619
620struct OutputLayerWriteStateToHWCTest : public OutputLayerTest {
621 static constexpr HWC2::Error kError = HWC2::Error::Unsupported;
622 static constexpr FloatRect kSourceCrop{11.f, 12.f, 13.f, 14.f};
623 static constexpr uint32_t kZOrder = 21u;
624 static constexpr Hwc2::Transform kBufferTransform = static_cast<Hwc2::Transform>(31);
625 static constexpr Hwc2::IComposerClient::BlendMode kBlendMode =
626 static_cast<Hwc2::IComposerClient::BlendMode>(41);
627 static constexpr float kAlpha = 51.f;
628 static constexpr uint32_t kType = 61u;
629 static constexpr uint32_t kAppId = 62u;
Lloyd Piquef5275482019-01-29 18:42:42 -0800630 static constexpr ui::Dataspace kDataspace = static_cast<ui::Dataspace>(71);
631 static constexpr int kSupportedPerFrameMetadata = 101;
632 static constexpr int kExpectedHwcSlot = 0;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800633
Lloyd Piquef5275482019-01-29 18:42:42 -0800634 static const half4 kColor;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800635 static const Rect kDisplayFrame;
Lloyd Piquea2468662019-03-07 21:31:06 -0800636 static const Region kOutputSpaceVisibleRegion;
Lloyd Piquef5275482019-01-29 18:42:42 -0800637 static const mat4 kColorTransform;
638 static const Region kSurfaceDamage;
639 static const HdrMetadata kHdrMetadata;
640 static native_handle_t* kSidebandStreamHandle;
641 static const sp<GraphicBuffer> kBuffer;
642 static const sp<Fence> kFence;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800643
644 OutputLayerWriteStateToHWCTest() {
645 auto& outputLayerState = mOutputLayer.editState();
646 outputLayerState.hwc = impl::OutputLayerCompositionState::Hwc(mHwcLayer);
647
648 outputLayerState.displayFrame = kDisplayFrame;
649 outputLayerState.sourceCrop = kSourceCrop;
650 outputLayerState.z = kZOrder;
651 outputLayerState.bufferTransform = static_cast<Hwc2::Transform>(kBufferTransform);
Lloyd Piquea2468662019-03-07 21:31:06 -0800652 outputLayerState.outputSpaceVisibleRegion = kOutputSpaceVisibleRegion;
Lloyd Piquef5275482019-01-29 18:42:42 -0800653 outputLayerState.dataspace = kDataspace;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800654
Lloyd Pique9755fb72019-03-26 14:44:40 -0700655 mLayerFEState.blendMode = kBlendMode;
656 mLayerFEState.alpha = kAlpha;
657 mLayerFEState.type = kType;
658 mLayerFEState.appId = kAppId;
659 mLayerFEState.colorTransform = kColorTransform;
660 mLayerFEState.color = kColor;
661 mLayerFEState.surfaceDamage = kSurfaceDamage;
662 mLayerFEState.hdrMetadata = kHdrMetadata;
663 mLayerFEState.sidebandStream = NativeHandle::create(kSidebandStreamHandle, false);
664 mLayerFEState.buffer = kBuffer;
665 mLayerFEState.bufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
666 mLayerFEState.acquireFence = kFence;
Lloyd Piquef5275482019-01-29 18:42:42 -0800667
668 EXPECT_CALL(mOutput, getDisplayColorProfile())
669 .WillRepeatedly(Return(&mDisplayColorProfile));
670 EXPECT_CALL(mDisplayColorProfile, getSupportedPerFrameMetadata())
671 .WillRepeatedly(Return(kSupportedPerFrameMetadata));
Lloyd Piquea83776c2019-01-29 18:42:32 -0800672 }
673
Lloyd Piquef5275482019-01-29 18:42:42 -0800674 // Some tests may need to simulate unsupported HWC calls
675 enum class SimulateUnsupported { None, ColorTransform };
676
Lloyd Piquea83776c2019-01-29 18:42:32 -0800677 void expectGeometryCommonCalls() {
678 EXPECT_CALL(*mHwcLayer, setDisplayFrame(kDisplayFrame)).WillOnce(Return(kError));
679 EXPECT_CALL(*mHwcLayer, setSourceCrop(kSourceCrop)).WillOnce(Return(kError));
680 EXPECT_CALL(*mHwcLayer, setZOrder(kZOrder)).WillOnce(Return(kError));
681 EXPECT_CALL(*mHwcLayer, setTransform(static_cast<HWC2::Transform>(kBufferTransform)))
682 .WillOnce(Return(kError));
683
684 EXPECT_CALL(*mHwcLayer, setBlendMode(static_cast<HWC2::BlendMode>(kBlendMode)))
685 .WillOnce(Return(kError));
686 EXPECT_CALL(*mHwcLayer, setPlaneAlpha(kAlpha)).WillOnce(Return(kError));
687 EXPECT_CALL(*mHwcLayer, setInfo(kType, kAppId)).WillOnce(Return(kError));
688 }
689
Lloyd Piquef5275482019-01-29 18:42:42 -0800690 void expectPerFrameCommonCalls(SimulateUnsupported unsupported = SimulateUnsupported::None) {
Lloyd Piquea2468662019-03-07 21:31:06 -0800691 EXPECT_CALL(*mHwcLayer, setVisibleRegion(RegionEq(kOutputSpaceVisibleRegion)))
Lloyd Piquef5275482019-01-29 18:42:42 -0800692 .WillOnce(Return(kError));
693 EXPECT_CALL(*mHwcLayer, setDataspace(kDataspace)).WillOnce(Return(kError));
694 EXPECT_CALL(*mHwcLayer, setColorTransform(kColorTransform))
695 .WillOnce(Return(unsupported == SimulateUnsupported::ColorTransform
696 ? HWC2::Error::Unsupported
697 : HWC2::Error::None));
698 EXPECT_CALL(*mHwcLayer, setSurfaceDamage(RegionEq(kSurfaceDamage)))
699 .WillOnce(Return(kError));
700 }
701
702 void expectSetCompositionTypeCall(Hwc2::IComposerClient::Composition compositionType) {
703 EXPECT_CALL(*mHwcLayer, setCompositionType(static_cast<HWC2::Composition>(compositionType)))
704 .WillOnce(Return(kError));
705 }
706
707 void expectNoSetCompositionTypeCall() {
708 EXPECT_CALL(*mHwcLayer, setCompositionType(_)).Times(0);
709 }
710
711 void expectSetColorCall() {
712 hwc_color_t color = {static_cast<uint8_t>(std::round(kColor.r * 255)),
713 static_cast<uint8_t>(std::round(kColor.g * 255)),
714 static_cast<uint8_t>(std::round(kColor.b * 255)), 255};
715
716 EXPECT_CALL(*mHwcLayer, setColor(ColorEq(color))).WillOnce(Return(kError));
717 }
718
719 void expectSetSidebandHandleCall() {
720 EXPECT_CALL(*mHwcLayer, setSidebandStream(kSidebandStreamHandle));
721 }
722
723 void expectSetHdrMetadataAndBufferCalls() {
724 EXPECT_CALL(*mHwcLayer, setPerFrameMetadata(kSupportedPerFrameMetadata, kHdrMetadata));
725 EXPECT_CALL(*mHwcLayer, setBuffer(kExpectedHwcSlot, kBuffer, kFence));
726 }
727
Lloyd Piquea83776c2019-01-29 18:42:32 -0800728 std::shared_ptr<HWC2::mock::Layer> mHwcLayer{std::make_shared<StrictMock<HWC2::mock::Layer>>()};
Lloyd Piquef5275482019-01-29 18:42:42 -0800729 StrictMock<mock::DisplayColorProfile> mDisplayColorProfile;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800730};
731
Lloyd Piquef5275482019-01-29 18:42:42 -0800732const half4 OutputLayerWriteStateToHWCTest::kColor{81.f / 255.f, 82.f / 255.f, 83.f / 255.f,
733 84.f / 255.f};
Lloyd Piquea83776c2019-01-29 18:42:32 -0800734const Rect OutputLayerWriteStateToHWCTest::kDisplayFrame{1001, 1002, 1003, 10044};
Lloyd Piquea2468662019-03-07 21:31:06 -0800735const Region OutputLayerWriteStateToHWCTest::kOutputSpaceVisibleRegion{
736 Rect{1005, 1006, 1007, 1008}};
Lloyd Piquef5275482019-01-29 18:42:42 -0800737const mat4 OutputLayerWriteStateToHWCTest::kColorTransform{
738 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016,
739 1017, 1018, 1019, 1020, 1021, 1022, 1023, 1024,
740};
741const Region OutputLayerWriteStateToHWCTest::kSurfaceDamage{Rect{1025, 1026, 1027, 1028}};
742const HdrMetadata OutputLayerWriteStateToHWCTest::kHdrMetadata{{/* LightFlattenable */}, 1029};
743native_handle_t* OutputLayerWriteStateToHWCTest::kSidebandStreamHandle =
744 reinterpret_cast<native_handle_t*>(1031);
745const sp<GraphicBuffer> OutputLayerWriteStateToHWCTest::kBuffer;
746const sp<Fence> OutputLayerWriteStateToHWCTest::kFence;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800747
748TEST_F(OutputLayerWriteStateToHWCTest, doesNothingIfNoHWCState) {
749 mOutputLayer.editState().hwc.reset();
750
751 mOutputLayer.writeStateToHWC(true);
752}
753
754TEST_F(OutputLayerWriteStateToHWCTest, doesNothingIfNoHWCLayer) {
755 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc(nullptr);
756
757 mOutputLayer.writeStateToHWC(true);
758}
759
Lloyd Piquef5275482019-01-29 18:42:42 -0800760TEST_F(OutputLayerWriteStateToHWCTest, canSetAllState) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800761 expectGeometryCommonCalls();
Lloyd Piquef5275482019-01-29 18:42:42 -0800762 expectPerFrameCommonCalls();
763
764 expectNoSetCompositionTypeCall();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800765
766 mOutputLayer.writeStateToHWC(true);
767}
768
Lloyd Piquef5275482019-01-29 18:42:42 -0800769TEST_F(OutputLayerWriteStateToHWCTest, canSetPerFrameStateForSolidColor) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700770 mLayerFEState.compositionType = Hwc2::IComposerClient::Composition::SOLID_COLOR;
Lloyd Piquef5275482019-01-29 18:42:42 -0800771
772 expectPerFrameCommonCalls();
Lloyd Pique46b72df2019-10-29 13:19:27 -0700773
774 // Setting the composition type should happen before setting the color. We
775 // check this in this test only by setting up an testing::InSeqeuence
776 // instance before setting up the two expectations.
777 InSequence s;
Lloyd Piquef5275482019-01-29 18:42:42 -0800778 expectSetCompositionTypeCall(Hwc2::IComposerClient::Composition::SOLID_COLOR);
Lloyd Pique46b72df2019-10-29 13:19:27 -0700779 expectSetColorCall();
Lloyd Piquef5275482019-01-29 18:42:42 -0800780
781 mOutputLayer.writeStateToHWC(false);
782}
783
784TEST_F(OutputLayerWriteStateToHWCTest, canSetPerFrameStateForSideband) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700785 mLayerFEState.compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
Lloyd Piquef5275482019-01-29 18:42:42 -0800786
787 expectPerFrameCommonCalls();
788 expectSetSidebandHandleCall();
789 expectSetCompositionTypeCall(Hwc2::IComposerClient::Composition::SIDEBAND);
790
791 mOutputLayer.writeStateToHWC(false);
792}
793
794TEST_F(OutputLayerWriteStateToHWCTest, canSetPerFrameStateForCursor) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700795 mLayerFEState.compositionType = Hwc2::IComposerClient::Composition::CURSOR;
Lloyd Piquef5275482019-01-29 18:42:42 -0800796
797 expectPerFrameCommonCalls();
798 expectSetHdrMetadataAndBufferCalls();
799 expectSetCompositionTypeCall(Hwc2::IComposerClient::Composition::CURSOR);
800
801 mOutputLayer.writeStateToHWC(false);
802}
803
804TEST_F(OutputLayerWriteStateToHWCTest, canSetPerFrameStateForDevice) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700805 mLayerFEState.compositionType = Hwc2::IComposerClient::Composition::DEVICE;
Lloyd Piquef5275482019-01-29 18:42:42 -0800806
807 expectPerFrameCommonCalls();
808 expectSetHdrMetadataAndBufferCalls();
809 expectSetCompositionTypeCall(Hwc2::IComposerClient::Composition::DEVICE);
810
811 mOutputLayer.writeStateToHWC(false);
812}
813
814TEST_F(OutputLayerWriteStateToHWCTest, compositionTypeIsNotSetIfUnchanged) {
815 (*mOutputLayer.editState().hwc).hwcCompositionType =
816 Hwc2::IComposerClient::Composition::SOLID_COLOR;
817
Lloyd Pique9755fb72019-03-26 14:44:40 -0700818 mLayerFEState.compositionType = Hwc2::IComposerClient::Composition::SOLID_COLOR;
Lloyd Piquef5275482019-01-29 18:42:42 -0800819
820 expectPerFrameCommonCalls();
821 expectSetColorCall();
822 expectNoSetCompositionTypeCall();
823
824 mOutputLayer.writeStateToHWC(false);
825}
826
827TEST_F(OutputLayerWriteStateToHWCTest, compositionTypeIsSetToClientIfColorTransformNotSupported) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700828 mLayerFEState.compositionType = Hwc2::IComposerClient::Composition::SOLID_COLOR;
Lloyd Piquef5275482019-01-29 18:42:42 -0800829
830 expectPerFrameCommonCalls(SimulateUnsupported::ColorTransform);
831 expectSetColorCall();
832 expectSetCompositionTypeCall(Hwc2::IComposerClient::Composition::CLIENT);
833
834 mOutputLayer.writeStateToHWC(false);
835}
836
837TEST_F(OutputLayerWriteStateToHWCTest, compositionTypeIsSetToClientIfClientCompositionForced) {
838 mOutputLayer.editState().forceClientComposition = true;
839
Lloyd Pique9755fb72019-03-26 14:44:40 -0700840 mLayerFEState.compositionType = Hwc2::IComposerClient::Composition::SOLID_COLOR;
Lloyd Piquef5275482019-01-29 18:42:42 -0800841
842 expectPerFrameCommonCalls();
843 expectSetColorCall();
844 expectSetCompositionTypeCall(Hwc2::IComposerClient::Composition::CLIENT);
845
846 mOutputLayer.writeStateToHWC(false);
847}
848
Lloyd Pique66d68602019-02-13 14:23:31 -0800849/*
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800850 * OutputLayer::writeCursorPositionToHWC()
851 */
852
853struct OutputLayerWriteCursorPositionToHWCTest : public OutputLayerTest {
854 static constexpr int kDefaultTransform = TR_IDENT;
855 static constexpr HWC2::Error kDefaultError = HWC2::Error::Unsupported;
856
857 static const Rect kDefaultDisplayViewport;
858 static const Rect kDefaultCursorFrame;
859
860 OutputLayerWriteCursorPositionToHWCTest() {
861 auto& outputLayerState = mOutputLayer.editState();
862 outputLayerState.hwc = impl::OutputLayerCompositionState::Hwc(mHwcLayer);
863
Lloyd Pique9755fb72019-03-26 14:44:40 -0700864 mLayerFEState.cursorFrame = kDefaultCursorFrame;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800865
866 mOutputState.viewport = kDefaultDisplayViewport;
867 mOutputState.transform = ui::Transform{kDefaultTransform};
868 }
869
870 std::shared_ptr<HWC2::mock::Layer> mHwcLayer{std::make_shared<StrictMock<HWC2::mock::Layer>>()};
871};
872
873const Rect OutputLayerWriteCursorPositionToHWCTest::kDefaultDisplayViewport{0, 0, 1920, 1080};
874const Rect OutputLayerWriteCursorPositionToHWCTest::kDefaultCursorFrame{1, 2, 3, 4};
875
876TEST_F(OutputLayerWriteCursorPositionToHWCTest, writeCursorPositionToHWCHandlesNoHwcState) {
877 mOutputLayer.editState().hwc.reset();
878
879 mOutputLayer.writeCursorPositionToHWC();
880}
881
882TEST_F(OutputLayerWriteCursorPositionToHWCTest, writeCursorPositionToHWCWritesStateToHWC) {
883 EXPECT_CALL(*mHwcLayer, setCursorPosition(1, 2)).WillOnce(Return(kDefaultError));
884
885 mOutputLayer.writeCursorPositionToHWC();
886}
887
888TEST_F(OutputLayerWriteCursorPositionToHWCTest, writeCursorPositionToHWCIntersectedWithViewport) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700889 mLayerFEState.cursorFrame = Rect{3000, 3000, 3016, 3016};
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800890
891 EXPECT_CALL(*mHwcLayer, setCursorPosition(1920, 1080)).WillOnce(Return(kDefaultError));
892
893 mOutputLayer.writeCursorPositionToHWC();
894}
895
896TEST_F(OutputLayerWriteCursorPositionToHWCTest, writeCursorPositionToHWCRotatedByTransform) {
897 mOutputState.transform = ui::Transform{TR_ROT_90};
898
899 EXPECT_CALL(*mHwcLayer, setCursorPosition(-4, 1)).WillOnce(Return(kDefaultError));
900
901 mOutputLayer.writeCursorPositionToHWC();
902}
903
904/*
Lloyd Pique66d68602019-02-13 14:23:31 -0800905 * OutputLayer::getHwcLayer()
906 */
907
908TEST_F(OutputLayerTest, getHwcLayerHandlesNoHwcState) {
909 mOutputLayer.editState().hwc.reset();
910
911 EXPECT_TRUE(mOutputLayer.getHwcLayer() == nullptr);
912}
913
914TEST_F(OutputLayerTest, getHwcLayerHandlesNoHwcLayer) {
915 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
916
917 EXPECT_TRUE(mOutputLayer.getHwcLayer() == nullptr);
918}
919
920TEST_F(OutputLayerTest, getHwcLayerReturnsHwcLayer) {
921 auto hwcLayer = std::make_shared<StrictMock<HWC2::mock::Layer>>();
922 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{hwcLayer};
923
924 EXPECT_EQ(hwcLayer.get(), mOutputLayer.getHwcLayer());
925}
926
927/*
928 * OutputLayer::requiresClientComposition()
929 */
930
931TEST_F(OutputLayerTest, requiresClientCompositionReturnsTrueIfNoHWC2State) {
932 mOutputLayer.editState().hwc.reset();
933
934 EXPECT_TRUE(mOutputLayer.requiresClientComposition());
935}
936
937TEST_F(OutputLayerTest, requiresClientCompositionReturnsTrueIfSetToClientComposition) {
938 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
939 mOutputLayer.editState().hwc->hwcCompositionType = Hwc2::IComposerClient::Composition::CLIENT;
940
941 EXPECT_TRUE(mOutputLayer.requiresClientComposition());
942}
943
944TEST_F(OutputLayerTest, requiresClientCompositionReturnsFalseIfSetToDeviceComposition) {
945 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
946 mOutputLayer.editState().hwc->hwcCompositionType = Hwc2::IComposerClient::Composition::DEVICE;
947
948 EXPECT_FALSE(mOutputLayer.requiresClientComposition());
949}
950
951/*
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800952 * OutputLayer::isHardwareCursor()
953 */
954
955TEST_F(OutputLayerTest, isHardwareCursorReturnsFalseIfNoHWC2State) {
956 mOutputLayer.editState().hwc.reset();
957
958 EXPECT_FALSE(mOutputLayer.isHardwareCursor());
959}
960
961TEST_F(OutputLayerTest, isHardwareCursorReturnsTrueIfSetToCursorComposition) {
962 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
963 mOutputLayer.editState().hwc->hwcCompositionType = Hwc2::IComposerClient::Composition::CURSOR;
964
965 EXPECT_TRUE(mOutputLayer.isHardwareCursor());
966}
967
968TEST_F(OutputLayerTest, isHardwareCursorReturnsFalseIfSetToDeviceComposition) {
969 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
970 mOutputLayer.editState().hwc->hwcCompositionType = Hwc2::IComposerClient::Composition::DEVICE;
971
972 EXPECT_FALSE(mOutputLayer.isHardwareCursor());
973}
974
975/*
Lloyd Pique66d68602019-02-13 14:23:31 -0800976 * OutputLayer::applyDeviceCompositionTypeChange()
977 */
978
979TEST_F(OutputLayerTest, applyDeviceCompositionTypeChangeSetsNewType) {
980 mOutputLayer.editState().hwc = impl::OutputLayerCompositionState::Hwc{nullptr};
981 mOutputLayer.editState().hwc->hwcCompositionType = Hwc2::IComposerClient::Composition::DEVICE;
982
983 mOutputLayer.applyDeviceCompositionTypeChange(Hwc2::IComposerClient::Composition::CLIENT);
984
985 ASSERT_TRUE(mOutputLayer.getState().hwc);
986 EXPECT_EQ(Hwc2::IComposerClient::Composition::CLIENT,
987 mOutputLayer.getState().hwc->hwcCompositionType);
988}
989
990/*
991 * OutputLayer::prepareForDeviceLayerRequests()
992 */
993
994TEST_F(OutputLayerTest, prepareForDeviceLayerRequestsResetsRequestState) {
995 mOutputLayer.editState().clearClientTarget = true;
996
997 mOutputLayer.prepareForDeviceLayerRequests();
998
999 EXPECT_FALSE(mOutputLayer.getState().clearClientTarget);
1000}
1001
1002/*
1003 * OutputLayer::applyDeviceLayerRequest()
1004 */
1005
1006TEST_F(OutputLayerTest, applyDeviceLayerRequestHandlesClearClientTarget) {
1007 mOutputLayer.editState().clearClientTarget = false;
1008
1009 mOutputLayer.applyDeviceLayerRequest(Hwc2::IComposerClient::LayerRequest::CLEAR_CLIENT_TARGET);
1010
1011 EXPECT_TRUE(mOutputLayer.getState().clearClientTarget);
1012}
1013
1014TEST_F(OutputLayerTest, applyDeviceLayerRequestHandlesUnknownRequest) {
1015 mOutputLayer.editState().clearClientTarget = false;
1016
1017 mOutputLayer.applyDeviceLayerRequest(static_cast<Hwc2::IComposerClient::LayerRequest>(0));
1018
1019 EXPECT_FALSE(mOutputLayer.getState().clearClientTarget);
1020}
1021
Lloyd Pique688abd42019-02-15 15:42:24 -08001022/*
1023 * OutputLayer::needsFiltering()
1024 */
1025
1026TEST_F(OutputLayerTest, needsFilteringReturnsFalseIfDisplaySizeSameAsSourceSize) {
1027 mOutputLayer.editState().displayFrame = Rect(100, 100, 200, 200);
1028 mOutputLayer.editState().sourceCrop = FloatRect{0.f, 0.f, 100.f, 100.f};
1029
1030 EXPECT_FALSE(mOutputLayer.needsFiltering());
1031}
1032
1033TEST_F(OutputLayerTest, needsFilteringReturnsTrueIfDisplaySizeDifferentFromSourceSize) {
1034 mOutputLayer.editState().displayFrame = Rect(100, 100, 200, 200);
1035 mOutputLayer.editState().sourceCrop = FloatRect{0.f, 0.f, 100.1f, 100.1f};
1036
1037 EXPECT_TRUE(mOutputLayer.needsFiltering());
1038}
1039
Lloyd Piquecc01a452018-12-04 17:24:00 -08001040} // namespace
1041} // namespace android::compositionengine