blob: a84af3a82fed680cf565deaa326daecf7f3baba8 [file] [log] [blame]
Lloyd Pique32cbe282018-10-19 13:09:22 -07001/*
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 <cmath>
18
19#include <compositionengine/impl/Output.h>
20#include <compositionengine/mock/CompositionEngine.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070021#include <compositionengine/mock/DisplayColorProfile.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080022#include <compositionengine/mock/Layer.h>
23#include <compositionengine/mock/LayerFE.h>
24#include <compositionengine/mock/OutputLayer.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070025#include <compositionengine/mock/RenderSurface.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070026#include <gtest/gtest.h>
27#include <ui/Rect.h>
28#include <ui/Region.h>
29
30#include "RegionMatcher.h"
31#include "TransformMatcher.h"
32
33namespace android::compositionengine {
34namespace {
35
Lloyd Pique31cb2942018-10-19 17:23:03 -070036using testing::Return;
Lloyd Pique32cbe282018-10-19 13:09:22 -070037using testing::ReturnRef;
38using testing::StrictMock;
39
40class OutputTest : public testing::Test {
41public:
Lloyd Pique31cb2942018-10-19 17:23:03 -070042 OutputTest() {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070043 mOutput.setDisplayColorProfileForTest(
44 std::unique_ptr<DisplayColorProfile>(mDisplayColorProfile));
Lloyd Pique31cb2942018-10-19 17:23:03 -070045 mOutput.setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(mRenderSurface));
Lloyd Piqueef958122019-02-05 18:00:12 -080046
47 mOutput.editState().bounds = kDefaultDisplaySize;
Lloyd Pique31cb2942018-10-19 17:23:03 -070048 }
Lloyd Pique32cbe282018-10-19 13:09:22 -070049 ~OutputTest() override = default;
50
Lloyd Piqueef958122019-02-05 18:00:12 -080051 static const Rect kDefaultDisplaySize;
52
Lloyd Pique32cbe282018-10-19 13:09:22 -070053 StrictMock<mock::CompositionEngine> mCompositionEngine;
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070054 mock::DisplayColorProfile* mDisplayColorProfile = new StrictMock<mock::DisplayColorProfile>();
Lloyd Pique31cb2942018-10-19 17:23:03 -070055 mock::RenderSurface* mRenderSurface = new StrictMock<mock::RenderSurface>();
Lloyd Pique32cbe282018-10-19 13:09:22 -070056 impl::Output mOutput{mCompositionEngine};
57};
58
Lloyd Piqueef958122019-02-05 18:00:12 -080059const Rect OutputTest::kDefaultDisplaySize{100, 200};
60
Lloyd Pique32cbe282018-10-19 13:09:22 -070061/* ------------------------------------------------------------------------
62 * Basic construction
63 */
64
Lloyd Pique31cb2942018-10-19 17:23:03 -070065TEST_F(OutputTest, canInstantiateOutput) {
66 // The validation check checks each required component.
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070067 EXPECT_CALL(*mDisplayColorProfile, isValid()).WillOnce(Return(true));
Lloyd Pique31cb2942018-10-19 17:23:03 -070068 EXPECT_CALL(*mRenderSurface, isValid()).WillOnce(Return(true));
69
70 EXPECT_TRUE(mOutput.isValid());
71
72 // If we take away the required components, it is no longer valid.
73 mOutput.setRenderSurfaceForTest(std::unique_ptr<RenderSurface>());
74
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070075 EXPECT_CALL(*mDisplayColorProfile, isValid()).WillOnce(Return(true));
76
Lloyd Pique31cb2942018-10-19 17:23:03 -070077 EXPECT_FALSE(mOutput.isValid());
78}
Lloyd Pique32cbe282018-10-19 13:09:22 -070079
80/* ------------------------------------------------------------------------
81 * Output::setCompositionEnabled()
82 */
83
84TEST_F(OutputTest, setCompositionEnabledDoesNothingIfAlreadyEnabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -070085 mOutput.editState().isEnabled = true;
86
87 mOutput.setCompositionEnabled(true);
88
89 EXPECT_TRUE(mOutput.getState().isEnabled);
90 EXPECT_THAT(mOutput.getState().dirtyRegion, RegionEq(Region()));
91}
92
93TEST_F(OutputTest, setCompositionEnabledSetsEnabledAndDirtiesEntireOutput) {
Lloyd Pique32cbe282018-10-19 13:09:22 -070094 mOutput.editState().isEnabled = false;
95
96 mOutput.setCompositionEnabled(true);
97
98 EXPECT_TRUE(mOutput.getState().isEnabled);
Lloyd Piqueef958122019-02-05 18:00:12 -080099 EXPECT_THAT(mOutput.getState().dirtyRegion, RegionEq(Region(kDefaultDisplaySize)));
Lloyd Pique32cbe282018-10-19 13:09:22 -0700100}
101
102TEST_F(OutputTest, setCompositionEnabledSetsDisabledAndDirtiesEntireOutput) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700103 mOutput.editState().isEnabled = true;
104
105 mOutput.setCompositionEnabled(false);
106
107 EXPECT_FALSE(mOutput.getState().isEnabled);
Lloyd Piqueef958122019-02-05 18:00:12 -0800108 EXPECT_THAT(mOutput.getState().dirtyRegion, RegionEq(Region(kDefaultDisplaySize)));
Lloyd Pique32cbe282018-10-19 13:09:22 -0700109}
110
111/* ------------------------------------------------------------------------
112 * Output::setProjection()
113 */
114
115TEST_F(OutputTest, setProjectionTriviallyWorks) {
116 const ui::Transform transform{ui::Transform::ROT_180};
117 const int32_t orientation = 123;
118 const Rect frame{1, 2, 3, 4};
119 const Rect viewport{5, 6, 7, 8};
120 const Rect scissor{9, 10, 11, 12};
121 const bool needsFiltering = true;
122
123 mOutput.setProjection(transform, orientation, frame, viewport, scissor, needsFiltering);
124
125 EXPECT_THAT(mOutput.getState().transform, TransformEq(transform));
126 EXPECT_EQ(orientation, mOutput.getState().orientation);
127 EXPECT_EQ(frame, mOutput.getState().frame);
128 EXPECT_EQ(viewport, mOutput.getState().viewport);
129 EXPECT_EQ(scissor, mOutput.getState().scissor);
130 EXPECT_EQ(needsFiltering, mOutput.getState().needsFiltering);
131}
132
133/* ------------------------------------------------------------------------
134 * Output::setBounds()
135 */
136
137TEST_F(OutputTest, setBoundsSetsSizeAndDirtiesEntireOutput) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800138 const ui::Size displaySize{200, 400};
Lloyd Pique31cb2942018-10-19 17:23:03 -0700139
140 EXPECT_CALL(*mRenderSurface, setDisplaySize(displaySize)).Times(1);
141 EXPECT_CALL(*mRenderSurface, getSize()).WillOnce(ReturnRef(displaySize));
142
Lloyd Pique32cbe282018-10-19 13:09:22 -0700143 mOutput.setBounds(displaySize);
144
Lloyd Pique31cb2942018-10-19 17:23:03 -0700145 EXPECT_EQ(Rect(displaySize), mOutput.getState().bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700146
Lloyd Pique31cb2942018-10-19 17:23:03 -0700147 EXPECT_THAT(mOutput.getState().dirtyRegion, RegionEq(Region(Rect(displaySize))));
Lloyd Pique32cbe282018-10-19 13:09:22 -0700148}
149
150/* ------------------------------------------------------------------------
151 * Output::setLayerStackFilter()
152 */
153
154TEST_F(OutputTest, setLayerStackFilterSetsFilterAndDirtiesEntireOutput) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700155 const uint32_t layerStack = 123u;
Lloyd Piqueef36b002019-01-23 17:52:04 -0800156 mOutput.setLayerStackFilter(layerStack, true);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700157
Lloyd Piqueef36b002019-01-23 17:52:04 -0800158 EXPECT_TRUE(mOutput.getState().layerStackInternal);
159 EXPECT_EQ(layerStack, mOutput.getState().layerStackId);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700160
Lloyd Piqueef958122019-02-05 18:00:12 -0800161 EXPECT_THAT(mOutput.getState().dirtyRegion, RegionEq(Region(kDefaultDisplaySize)));
Lloyd Pique32cbe282018-10-19 13:09:22 -0700162}
163
164/* ------------------------------------------------------------------------
165 * Output::setColorTransform
166 */
167
168TEST_F(OutputTest, setColorTransformSetsTransform) {
169 // Identity matrix sets an identity state value
170 const mat4 identity;
171
172 mOutput.setColorTransform(identity);
173
174 EXPECT_EQ(HAL_COLOR_TRANSFORM_IDENTITY, mOutput.getState().colorTransform);
175
Lloyd Piqueef958122019-02-05 18:00:12 -0800176 // Since identity is the default, the dirty region should be unchanged (empty)
177 EXPECT_THAT(mOutput.getState().dirtyRegion, RegionEq(Region()));
178
Lloyd Pique32cbe282018-10-19 13:09:22 -0700179 // Non-identity matrix sets a non-identity state value
180 const mat4 nonIdentity = mat4() * 2;
181
182 mOutput.setColorTransform(nonIdentity);
183
184 EXPECT_EQ(HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX, mOutput.getState().colorTransform);
Lloyd Piqueef958122019-02-05 18:00:12 -0800185
186 // Since this is a state change, the entire output should now be dirty.
187 EXPECT_THAT(mOutput.getState().dirtyRegion, RegionEq(Region(kDefaultDisplaySize)));
Lloyd Pique32cbe282018-10-19 13:09:22 -0700188}
189
190/* ------------------------------------------------------------------------
191 * Output::setColorMode
192 */
193
Lloyd Piqueef958122019-02-05 18:00:12 -0800194TEST_F(OutputTest, setColorModeSetsStateAndDirtiesOutputIfChanged) {
195 EXPECT_CALL(*mRenderSurface, setBufferDataspace(ui::Dataspace::DISPLAY_P3)).Times(1);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700196
Lloyd Piqueef958122019-02-05 18:00:12 -0800197 mOutput.setColorMode(ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3,
Lloyd Pique32cbe282018-10-19 13:09:22 -0700198 ui::RenderIntent::TONE_MAP_COLORIMETRIC);
199
Lloyd Piqueef958122019-02-05 18:00:12 -0800200 EXPECT_EQ(ui::ColorMode::DISPLAY_P3, mOutput.getState().colorMode);
201 EXPECT_EQ(ui::Dataspace::DISPLAY_P3, mOutput.getState().dataspace);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700202 EXPECT_EQ(ui::RenderIntent::TONE_MAP_COLORIMETRIC, mOutput.getState().renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800203 EXPECT_THAT(mOutput.getState().dirtyRegion, RegionEq(Region(kDefaultDisplaySize)));
204}
205
206TEST_F(OutputTest, setColorModeDoesNothingIfNoChange) {
207 mOutput.editState().colorMode = ui::ColorMode::DISPLAY_P3;
208 mOutput.editState().dataspace = ui::Dataspace::DISPLAY_P3;
209 mOutput.editState().renderIntent = ui::RenderIntent::TONE_MAP_COLORIMETRIC;
210
211 mOutput.setColorMode(ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3,
212 ui::RenderIntent::TONE_MAP_COLORIMETRIC);
213
214 EXPECT_THAT(mOutput.getState().dirtyRegion, RegionEq(Region()));
Lloyd Pique32cbe282018-10-19 13:09:22 -0700215}
216
217/* ------------------------------------------------------------------------
Lloyd Pique31cb2942018-10-19 17:23:03 -0700218 * Output::setRenderSurface()
219 */
220
221TEST_F(OutputTest, setRenderSurfaceResetsBounds) {
222 const ui::Size newDisplaySize{640, 480};
223
224 mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
225 EXPECT_CALL(*renderSurface, getSize()).WillOnce(ReturnRef(newDisplaySize));
226
227 mOutput.setRenderSurface(std::unique_ptr<RenderSurface>(renderSurface));
228
229 EXPECT_EQ(Rect(newDisplaySize), mOutput.getState().bounds);
230}
231
232/* ------------------------------------------------------------------------
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000233 * Output::getDirtyRegion()
Lloyd Pique32cbe282018-10-19 13:09:22 -0700234 */
235
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000236TEST_F(OutputTest, getDirtyRegionWithRepaintEverythingTrue) {
237 const Rect viewport{100, 200};
238 mOutput.editState().viewport = viewport;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700239 mOutput.editState().dirtyRegion.set(50, 300);
240
241 {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000242 Region result = mOutput.getDirtyRegion(true);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700243
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000244 EXPECT_THAT(result, RegionEq(Region(viewport)));
Lloyd Pique32cbe282018-10-19 13:09:22 -0700245 }
246}
247
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000248TEST_F(OutputTest, getDirtyRegionWithRepaintEverythingFalse) {
249 const Rect viewport{100, 200};
250 mOutput.editState().viewport = viewport;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700251 mOutput.editState().dirtyRegion.set(50, 300);
252
253 {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000254 Region result = mOutput.getDirtyRegion(false);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700255
256 // The dirtyRegion should be clipped to the display bounds.
257 EXPECT_THAT(result, RegionEq(Region(Rect(50, 200))));
258 }
Lloyd Pique32cbe282018-10-19 13:09:22 -0700259}
260
Lloyd Piqueef36b002019-01-23 17:52:04 -0800261/* ------------------------------------------------------------------------
262 * Output::belongsInOutput()
263 */
264
265TEST_F(OutputTest, belongsInOutputFiltersAsExpected) {
266 const uint32_t layerStack1 = 123u;
267 const uint32_t layerStack2 = 456u;
268
269 // If the output accepts layerStack1 and internal-only layers....
270 mOutput.setLayerStackFilter(layerStack1, true);
271
272 // Any layer with layerStack1 belongs to it, internal-only or not.
273 EXPECT_TRUE(mOutput.belongsInOutput(layerStack1, false));
274 EXPECT_TRUE(mOutput.belongsInOutput(layerStack1, true));
275 EXPECT_FALSE(mOutput.belongsInOutput(layerStack2, true));
276 EXPECT_FALSE(mOutput.belongsInOutput(layerStack2, false));
277
278 // If the output accepts layerStack21 but not internal-only layers...
279 mOutput.setLayerStackFilter(layerStack1, false);
280
281 // Only non-internal layers with layerStack1 belong to it.
282 EXPECT_TRUE(mOutput.belongsInOutput(layerStack1, false));
283 EXPECT_FALSE(mOutput.belongsInOutput(layerStack1, true));
284 EXPECT_FALSE(mOutput.belongsInOutput(layerStack2, true));
285 EXPECT_FALSE(mOutput.belongsInOutput(layerStack2, false));
286}
287
Lloyd Piquecc01a452018-12-04 17:24:00 -0800288/* ------------------------------------------------------------------------
289 * Output::getOutputLayerForLayer()
290 */
291
292TEST_F(OutputTest, getOutputLayerForLayerWorks) {
293 mock::OutputLayer* outputLayer1 = new StrictMock<mock::OutputLayer>();
294 mock::OutputLayer* outputLayer2 = new StrictMock<mock::OutputLayer>();
295
296 Output::OutputLayers outputLayers;
297 outputLayers.emplace_back(std::unique_ptr<OutputLayer>(outputLayer1));
298 outputLayers.emplace_back(nullptr);
299 outputLayers.emplace_back(std::unique_ptr<OutputLayer>(outputLayer2));
300 mOutput.setOutputLayersOrderedByZ(std::move(outputLayers));
301
302 StrictMock<mock::Layer> layer;
303 StrictMock<mock::Layer> otherLayer;
304
305 // If the input layer matches the first OutputLayer, it will be returned.
306 EXPECT_CALL(*outputLayer1, getLayer()).WillOnce(ReturnRef(layer));
307 EXPECT_EQ(outputLayer1, mOutput.getOutputLayerForLayer(&layer));
308
309 // If the input layer matches the second OutputLayer, it will be returned.
310 EXPECT_CALL(*outputLayer1, getLayer()).WillOnce(ReturnRef(otherLayer));
311 EXPECT_CALL(*outputLayer2, getLayer()).WillOnce(ReturnRef(layer));
312 EXPECT_EQ(outputLayer2, mOutput.getOutputLayerForLayer(&layer));
313
314 // If the input layer does not match an output layer, null will be returned.
315 EXPECT_CALL(*outputLayer1, getLayer()).WillOnce(ReturnRef(otherLayer));
316 EXPECT_CALL(*outputLayer2, getLayer()).WillOnce(ReturnRef(otherLayer));
317 EXPECT_EQ(nullptr, mOutput.getOutputLayerForLayer(&layer));
318}
319
320/* ------------------------------------------------------------------------
321 * Output::getOrCreateOutputLayer()
322 */
323
324TEST_F(OutputTest, getOrCreateOutputLayerWorks) {
325 mock::OutputLayer* existingOutputLayer = new StrictMock<mock::OutputLayer>();
326
327 Output::OutputLayers outputLayers;
328 outputLayers.emplace_back(nullptr);
329 outputLayers.emplace_back(std::unique_ptr<OutputLayer>(existingOutputLayer));
330 mOutput.setOutputLayersOrderedByZ(std::move(outputLayers));
331
332 std::shared_ptr<mock::Layer> layer{new StrictMock<mock::Layer>()};
333 sp<LayerFE> layerFE{new StrictMock<mock::LayerFE>()};
334
335 StrictMock<mock::Layer> otherLayer;
336
337 {
338 // If there is no OutputLayer corresponding to the input layer, a
339 // new OutputLayer is constructed and returned.
340 EXPECT_CALL(*existingOutputLayer, getLayer()).WillOnce(ReturnRef(otherLayer));
Lloyd Pique07e33212018-12-18 16:33:37 -0800341 auto result = mOutput.getOrCreateOutputLayer(std::nullopt, layer, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800342 EXPECT_NE(existingOutputLayer, result.get());
343 EXPECT_TRUE(result.get() != nullptr);
344 EXPECT_EQ(layer.get(), &result->getLayer());
345 EXPECT_EQ(layerFE.get(), &result->getLayerFE());
346
347 // The entries in the ordered array should be unchanged.
348 auto& outputLayers = mOutput.getOutputLayersOrderedByZ();
349 EXPECT_EQ(nullptr, outputLayers[0].get());
350 EXPECT_EQ(existingOutputLayer, outputLayers[1].get());
351 }
352
353 {
354 // If there is an existing OutputLayer for the requested layer, an owned
355 // pointer is returned
356 EXPECT_CALL(*existingOutputLayer, getLayer()).WillOnce(ReturnRef(*layer));
Lloyd Pique07e33212018-12-18 16:33:37 -0800357 auto result = mOutput.getOrCreateOutputLayer(std::nullopt, layer, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800358 EXPECT_EQ(existingOutputLayer, result.get());
359
360 // The corresponding entry in the ordered array should be cleared.
361 auto& outputLayers = mOutput.getOutputLayersOrderedByZ();
362 EXPECT_EQ(nullptr, outputLayers[0].get());
363 EXPECT_EQ(nullptr, outputLayers[1].get());
364 }
365}
366
Lloyd Pique32cbe282018-10-19 13:09:22 -0700367} // namespace
368} // namespace android::compositionengine