Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <cmath> |
| 18 | |
| 19 | #include <compositionengine/impl/Output.h> |
| 20 | #include <compositionengine/mock/CompositionEngine.h> |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 21 | #include <compositionengine/mock/DisplayColorProfile.h> |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 22 | #include <compositionengine/mock/Layer.h> |
| 23 | #include <compositionengine/mock/LayerFE.h> |
| 24 | #include <compositionengine/mock/OutputLayer.h> |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 25 | #include <compositionengine/mock/RenderSurface.h> |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 26 | #include <gtest/gtest.h> |
| 27 | #include <ui/Rect.h> |
| 28 | #include <ui/Region.h> |
| 29 | |
| 30 | #include "RegionMatcher.h" |
| 31 | #include "TransformMatcher.h" |
| 32 | |
| 33 | namespace android::compositionengine { |
| 34 | namespace { |
| 35 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 36 | using testing::Return; |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 37 | using testing::ReturnRef; |
| 38 | using testing::StrictMock; |
| 39 | |
| 40 | class OutputTest : public testing::Test { |
| 41 | public: |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 42 | OutputTest() { |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 43 | mOutput.setDisplayColorProfileForTest( |
| 44 | std::unique_ptr<DisplayColorProfile>(mDisplayColorProfile)); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 45 | mOutput.setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(mRenderSurface)); |
| 46 | } |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 47 | ~OutputTest() override = default; |
| 48 | |
| 49 | StrictMock<mock::CompositionEngine> mCompositionEngine; |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 50 | mock::DisplayColorProfile* mDisplayColorProfile = new StrictMock<mock::DisplayColorProfile>(); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 51 | mock::RenderSurface* mRenderSurface = new StrictMock<mock::RenderSurface>(); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 52 | impl::Output mOutput{mCompositionEngine}; |
| 53 | }; |
| 54 | |
| 55 | /* ------------------------------------------------------------------------ |
| 56 | * Basic construction |
| 57 | */ |
| 58 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 59 | TEST_F(OutputTest, canInstantiateOutput) { |
| 60 | // The validation check checks each required component. |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 61 | EXPECT_CALL(*mDisplayColorProfile, isValid()).WillOnce(Return(true)); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 62 | EXPECT_CALL(*mRenderSurface, isValid()).WillOnce(Return(true)); |
| 63 | |
| 64 | EXPECT_TRUE(mOutput.isValid()); |
| 65 | |
| 66 | // If we take away the required components, it is no longer valid. |
| 67 | mOutput.setRenderSurfaceForTest(std::unique_ptr<RenderSurface>()); |
| 68 | |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 69 | EXPECT_CALL(*mDisplayColorProfile, isValid()).WillOnce(Return(true)); |
| 70 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 71 | EXPECT_FALSE(mOutput.isValid()); |
| 72 | } |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 73 | |
| 74 | /* ------------------------------------------------------------------------ |
| 75 | * Output::setCompositionEnabled() |
| 76 | */ |
| 77 | |
| 78 | TEST_F(OutputTest, setCompositionEnabledDoesNothingIfAlreadyEnabled) { |
| 79 | const Rect displaySize{100, 200}; |
| 80 | mOutput.editState().bounds = displaySize; |
| 81 | mOutput.editState().isEnabled = true; |
| 82 | |
| 83 | mOutput.setCompositionEnabled(true); |
| 84 | |
| 85 | EXPECT_TRUE(mOutput.getState().isEnabled); |
| 86 | EXPECT_THAT(mOutput.getState().dirtyRegion, RegionEq(Region())); |
| 87 | } |
| 88 | |
| 89 | TEST_F(OutputTest, setCompositionEnabledSetsEnabledAndDirtiesEntireOutput) { |
| 90 | const Rect displaySize{100, 200}; |
| 91 | mOutput.editState().bounds = displaySize; |
| 92 | mOutput.editState().isEnabled = false; |
| 93 | |
| 94 | mOutput.setCompositionEnabled(true); |
| 95 | |
| 96 | EXPECT_TRUE(mOutput.getState().isEnabled); |
| 97 | EXPECT_THAT(mOutput.getState().dirtyRegion, RegionEq(Region(displaySize))); |
| 98 | } |
| 99 | |
| 100 | TEST_F(OutputTest, setCompositionEnabledSetsDisabledAndDirtiesEntireOutput) { |
| 101 | const Rect displaySize{100, 200}; |
| 102 | mOutput.editState().bounds = displaySize; |
| 103 | mOutput.editState().isEnabled = true; |
| 104 | |
| 105 | mOutput.setCompositionEnabled(false); |
| 106 | |
| 107 | EXPECT_FALSE(mOutput.getState().isEnabled); |
| 108 | EXPECT_THAT(mOutput.getState().dirtyRegion, RegionEq(Region(displaySize))); |
| 109 | } |
| 110 | |
| 111 | /* ------------------------------------------------------------------------ |
| 112 | * Output::setProjection() |
| 113 | */ |
| 114 | |
| 115 | TEST_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 | |
| 137 | TEST_F(OutputTest, setBoundsSetsSizeAndDirtiesEntireOutput) { |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 138 | const ui::Size displaySize{100, 200}; |
| 139 | |
| 140 | EXPECT_CALL(*mRenderSurface, setDisplaySize(displaySize)).Times(1); |
| 141 | EXPECT_CALL(*mRenderSurface, getSize()).WillOnce(ReturnRef(displaySize)); |
| 142 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 143 | mOutput.setBounds(displaySize); |
| 144 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 145 | EXPECT_EQ(Rect(displaySize), mOutput.getState().bounds); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 146 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 147 | EXPECT_THAT(mOutput.getState().dirtyRegion, RegionEq(Region(Rect(displaySize)))); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /* ------------------------------------------------------------------------ |
| 151 | * Output::setLayerStackFilter() |
| 152 | */ |
| 153 | |
| 154 | TEST_F(OutputTest, setLayerStackFilterSetsFilterAndDirtiesEntireOutput) { |
| 155 | const Rect displaySize{100, 200}; |
| 156 | mOutput.editState().bounds = displaySize; |
| 157 | |
| 158 | const uint32_t layerStack = 123u; |
Lloyd Pique | ef36b00 | 2019-01-23 17:52:04 -0800 | [diff] [blame] | 159 | mOutput.setLayerStackFilter(layerStack, true); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 160 | |
Lloyd Pique | ef36b00 | 2019-01-23 17:52:04 -0800 | [diff] [blame] | 161 | EXPECT_TRUE(mOutput.getState().layerStackInternal); |
| 162 | EXPECT_EQ(layerStack, mOutput.getState().layerStackId); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 163 | |
| 164 | EXPECT_THAT(mOutput.getState().dirtyRegion, RegionEq(Region(displaySize))); |
| 165 | } |
| 166 | |
| 167 | /* ------------------------------------------------------------------------ |
| 168 | * Output::setColorTransform |
| 169 | */ |
| 170 | |
| 171 | TEST_F(OutputTest, setColorTransformSetsTransform) { |
| 172 | // Identity matrix sets an identity state value |
| 173 | const mat4 identity; |
| 174 | |
| 175 | mOutput.setColorTransform(identity); |
| 176 | |
| 177 | EXPECT_EQ(HAL_COLOR_TRANSFORM_IDENTITY, mOutput.getState().colorTransform); |
| 178 | |
| 179 | // 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); |
| 185 | } |
| 186 | |
| 187 | /* ------------------------------------------------------------------------ |
| 188 | * Output::setColorMode |
| 189 | */ |
| 190 | |
| 191 | TEST_F(OutputTest, setColorModeSetsModeUnlessNoChange) { |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 192 | EXPECT_CALL(*mRenderSurface, setBufferDataspace(ui::Dataspace::SRGB)).Times(1); |
| 193 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 194 | mOutput.setColorMode(ui::ColorMode::BT2100_PQ, ui::Dataspace::SRGB, |
| 195 | ui::RenderIntent::TONE_MAP_COLORIMETRIC); |
| 196 | |
| 197 | EXPECT_EQ(ui::ColorMode::BT2100_PQ, mOutput.getState().colorMode); |
| 198 | EXPECT_EQ(ui::Dataspace::SRGB, mOutput.getState().dataspace); |
| 199 | EXPECT_EQ(ui::RenderIntent::TONE_MAP_COLORIMETRIC, mOutput.getState().renderIntent); |
| 200 | } |
| 201 | |
| 202 | /* ------------------------------------------------------------------------ |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 203 | * Output::setRenderSurface() |
| 204 | */ |
| 205 | |
| 206 | TEST_F(OutputTest, setRenderSurfaceResetsBounds) { |
| 207 | const ui::Size newDisplaySize{640, 480}; |
| 208 | |
| 209 | mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>(); |
| 210 | EXPECT_CALL(*renderSurface, getSize()).WillOnce(ReturnRef(newDisplaySize)); |
| 211 | |
| 212 | mOutput.setRenderSurface(std::unique_ptr<RenderSurface>(renderSurface)); |
| 213 | |
| 214 | EXPECT_EQ(Rect(newDisplaySize), mOutput.getState().bounds); |
| 215 | } |
| 216 | |
| 217 | /* ------------------------------------------------------------------------ |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame^] | 218 | * Output::getDirtyRegion() |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 219 | */ |
| 220 | |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame^] | 221 | TEST_F(OutputTest, getDirtyRegionWithRepaintEverythingTrue) { |
| 222 | const Rect viewport{100, 200}; |
| 223 | mOutput.editState().viewport = viewport; |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 224 | mOutput.editState().dirtyRegion.set(50, 300); |
| 225 | |
| 226 | { |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame^] | 227 | Region result = mOutput.getDirtyRegion(true); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 228 | |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame^] | 229 | EXPECT_THAT(result, RegionEq(Region(viewport))); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame^] | 233 | TEST_F(OutputTest, getDirtyRegionWithRepaintEverythingFalse) { |
| 234 | const Rect viewport{100, 200}; |
| 235 | mOutput.editState().viewport = viewport; |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 236 | mOutput.editState().dirtyRegion.set(50, 300); |
| 237 | |
| 238 | { |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame^] | 239 | Region result = mOutput.getDirtyRegion(false); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 240 | |
| 241 | // The dirtyRegion should be clipped to the display bounds. |
| 242 | EXPECT_THAT(result, RegionEq(Region(Rect(50, 200)))); |
| 243 | } |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Lloyd Pique | ef36b00 | 2019-01-23 17:52:04 -0800 | [diff] [blame] | 246 | /* ------------------------------------------------------------------------ |
| 247 | * Output::belongsInOutput() |
| 248 | */ |
| 249 | |
| 250 | TEST_F(OutputTest, belongsInOutputFiltersAsExpected) { |
| 251 | const uint32_t layerStack1 = 123u; |
| 252 | const uint32_t layerStack2 = 456u; |
| 253 | |
| 254 | // If the output accepts layerStack1 and internal-only layers.... |
| 255 | mOutput.setLayerStackFilter(layerStack1, true); |
| 256 | |
| 257 | // Any layer with layerStack1 belongs to it, internal-only or not. |
| 258 | EXPECT_TRUE(mOutput.belongsInOutput(layerStack1, false)); |
| 259 | EXPECT_TRUE(mOutput.belongsInOutput(layerStack1, true)); |
| 260 | EXPECT_FALSE(mOutput.belongsInOutput(layerStack2, true)); |
| 261 | EXPECT_FALSE(mOutput.belongsInOutput(layerStack2, false)); |
| 262 | |
| 263 | // If the output accepts layerStack21 but not internal-only layers... |
| 264 | mOutput.setLayerStackFilter(layerStack1, false); |
| 265 | |
| 266 | // Only non-internal layers with layerStack1 belong to it. |
| 267 | EXPECT_TRUE(mOutput.belongsInOutput(layerStack1, false)); |
| 268 | EXPECT_FALSE(mOutput.belongsInOutput(layerStack1, true)); |
| 269 | EXPECT_FALSE(mOutput.belongsInOutput(layerStack2, true)); |
| 270 | EXPECT_FALSE(mOutput.belongsInOutput(layerStack2, false)); |
| 271 | } |
| 272 | |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 273 | /* ------------------------------------------------------------------------ |
| 274 | * Output::getOutputLayerForLayer() |
| 275 | */ |
| 276 | |
| 277 | TEST_F(OutputTest, getOutputLayerForLayerWorks) { |
| 278 | mock::OutputLayer* outputLayer1 = new StrictMock<mock::OutputLayer>(); |
| 279 | mock::OutputLayer* outputLayer2 = new StrictMock<mock::OutputLayer>(); |
| 280 | |
| 281 | Output::OutputLayers outputLayers; |
| 282 | outputLayers.emplace_back(std::unique_ptr<OutputLayer>(outputLayer1)); |
| 283 | outputLayers.emplace_back(nullptr); |
| 284 | outputLayers.emplace_back(std::unique_ptr<OutputLayer>(outputLayer2)); |
| 285 | mOutput.setOutputLayersOrderedByZ(std::move(outputLayers)); |
| 286 | |
| 287 | StrictMock<mock::Layer> layer; |
| 288 | StrictMock<mock::Layer> otherLayer; |
| 289 | |
| 290 | // If the input layer matches the first OutputLayer, it will be returned. |
| 291 | EXPECT_CALL(*outputLayer1, getLayer()).WillOnce(ReturnRef(layer)); |
| 292 | EXPECT_EQ(outputLayer1, mOutput.getOutputLayerForLayer(&layer)); |
| 293 | |
| 294 | // If the input layer matches the second OutputLayer, it will be returned. |
| 295 | EXPECT_CALL(*outputLayer1, getLayer()).WillOnce(ReturnRef(otherLayer)); |
| 296 | EXPECT_CALL(*outputLayer2, getLayer()).WillOnce(ReturnRef(layer)); |
| 297 | EXPECT_EQ(outputLayer2, mOutput.getOutputLayerForLayer(&layer)); |
| 298 | |
| 299 | // If the input layer does not match an output layer, null will be returned. |
| 300 | EXPECT_CALL(*outputLayer1, getLayer()).WillOnce(ReturnRef(otherLayer)); |
| 301 | EXPECT_CALL(*outputLayer2, getLayer()).WillOnce(ReturnRef(otherLayer)); |
| 302 | EXPECT_EQ(nullptr, mOutput.getOutputLayerForLayer(&layer)); |
| 303 | } |
| 304 | |
| 305 | /* ------------------------------------------------------------------------ |
| 306 | * Output::getOrCreateOutputLayer() |
| 307 | */ |
| 308 | |
| 309 | TEST_F(OutputTest, getOrCreateOutputLayerWorks) { |
| 310 | mock::OutputLayer* existingOutputLayer = new StrictMock<mock::OutputLayer>(); |
| 311 | |
| 312 | Output::OutputLayers outputLayers; |
| 313 | outputLayers.emplace_back(nullptr); |
| 314 | outputLayers.emplace_back(std::unique_ptr<OutputLayer>(existingOutputLayer)); |
| 315 | mOutput.setOutputLayersOrderedByZ(std::move(outputLayers)); |
| 316 | |
| 317 | std::shared_ptr<mock::Layer> layer{new StrictMock<mock::Layer>()}; |
| 318 | sp<LayerFE> layerFE{new StrictMock<mock::LayerFE>()}; |
| 319 | |
| 320 | StrictMock<mock::Layer> otherLayer; |
| 321 | |
| 322 | { |
| 323 | // If there is no OutputLayer corresponding to the input layer, a |
| 324 | // new OutputLayer is constructed and returned. |
| 325 | EXPECT_CALL(*existingOutputLayer, getLayer()).WillOnce(ReturnRef(otherLayer)); |
| 326 | auto result = mOutput.getOrCreateOutputLayer(layer, layerFE); |
| 327 | EXPECT_NE(existingOutputLayer, result.get()); |
| 328 | EXPECT_TRUE(result.get() != nullptr); |
| 329 | EXPECT_EQ(layer.get(), &result->getLayer()); |
| 330 | EXPECT_EQ(layerFE.get(), &result->getLayerFE()); |
| 331 | |
| 332 | // The entries in the ordered array should be unchanged. |
| 333 | auto& outputLayers = mOutput.getOutputLayersOrderedByZ(); |
| 334 | EXPECT_EQ(nullptr, outputLayers[0].get()); |
| 335 | EXPECT_EQ(existingOutputLayer, outputLayers[1].get()); |
| 336 | } |
| 337 | |
| 338 | { |
| 339 | // If there is an existing OutputLayer for the requested layer, an owned |
| 340 | // pointer is returned |
| 341 | EXPECT_CALL(*existingOutputLayer, getLayer()).WillOnce(ReturnRef(*layer)); |
| 342 | auto result = mOutput.getOrCreateOutputLayer(layer, layerFE); |
| 343 | EXPECT_EQ(existingOutputLayer, result.get()); |
| 344 | |
| 345 | // The corresponding entry in the ordered array should be cleared. |
| 346 | auto& outputLayers = mOutput.getOutputLayersOrderedByZ(); |
| 347 | EXPECT_EQ(nullptr, outputLayers[0].get()); |
| 348 | EXPECT_EQ(nullptr, outputLayers[1].get()); |
| 349 | } |
| 350 | } |
| 351 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 352 | } // namespace |
| 353 | } // namespace android::compositionengine |