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 | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 22 | #include <compositionengine/mock/RenderSurface.h> |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 23 | #include <gtest/gtest.h> |
| 24 | #include <ui/Rect.h> |
| 25 | #include <ui/Region.h> |
| 26 | |
| 27 | #include "RegionMatcher.h" |
| 28 | #include "TransformMatcher.h" |
| 29 | |
| 30 | namespace android::compositionengine { |
| 31 | namespace { |
| 32 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 33 | using testing::Return; |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 34 | using testing::ReturnRef; |
| 35 | using testing::StrictMock; |
| 36 | |
| 37 | class OutputTest : public testing::Test { |
| 38 | public: |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 39 | OutputTest() { |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame^] | 40 | mOutput.setDisplayColorProfileForTest( |
| 41 | std::unique_ptr<DisplayColorProfile>(mDisplayColorProfile)); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 42 | mOutput.setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(mRenderSurface)); |
| 43 | } |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 44 | ~OutputTest() override = default; |
| 45 | |
| 46 | StrictMock<mock::CompositionEngine> mCompositionEngine; |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame^] | 47 | mock::DisplayColorProfile* mDisplayColorProfile = new StrictMock<mock::DisplayColorProfile>(); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 48 | mock::RenderSurface* mRenderSurface = new StrictMock<mock::RenderSurface>(); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 49 | impl::Output mOutput{mCompositionEngine}; |
| 50 | }; |
| 51 | |
| 52 | /* ------------------------------------------------------------------------ |
| 53 | * Basic construction |
| 54 | */ |
| 55 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 56 | TEST_F(OutputTest, canInstantiateOutput) { |
| 57 | // The validation check checks each required component. |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame^] | 58 | EXPECT_CALL(*mDisplayColorProfile, isValid()).WillOnce(Return(true)); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 59 | EXPECT_CALL(*mRenderSurface, isValid()).WillOnce(Return(true)); |
| 60 | |
| 61 | EXPECT_TRUE(mOutput.isValid()); |
| 62 | |
| 63 | // If we take away the required components, it is no longer valid. |
| 64 | mOutput.setRenderSurfaceForTest(std::unique_ptr<RenderSurface>()); |
| 65 | |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame^] | 66 | EXPECT_CALL(*mDisplayColorProfile, isValid()).WillOnce(Return(true)); |
| 67 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 68 | EXPECT_FALSE(mOutput.isValid()); |
| 69 | } |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 70 | |
| 71 | /* ------------------------------------------------------------------------ |
| 72 | * Output::setCompositionEnabled() |
| 73 | */ |
| 74 | |
| 75 | TEST_F(OutputTest, setCompositionEnabledDoesNothingIfAlreadyEnabled) { |
| 76 | const Rect displaySize{100, 200}; |
| 77 | mOutput.editState().bounds = displaySize; |
| 78 | mOutput.editState().isEnabled = true; |
| 79 | |
| 80 | mOutput.setCompositionEnabled(true); |
| 81 | |
| 82 | EXPECT_TRUE(mOutput.getState().isEnabled); |
| 83 | EXPECT_THAT(mOutput.getState().dirtyRegion, RegionEq(Region())); |
| 84 | } |
| 85 | |
| 86 | TEST_F(OutputTest, setCompositionEnabledSetsEnabledAndDirtiesEntireOutput) { |
| 87 | const Rect displaySize{100, 200}; |
| 88 | mOutput.editState().bounds = displaySize; |
| 89 | mOutput.editState().isEnabled = false; |
| 90 | |
| 91 | mOutput.setCompositionEnabled(true); |
| 92 | |
| 93 | EXPECT_TRUE(mOutput.getState().isEnabled); |
| 94 | EXPECT_THAT(mOutput.getState().dirtyRegion, RegionEq(Region(displaySize))); |
| 95 | } |
| 96 | |
| 97 | TEST_F(OutputTest, setCompositionEnabledSetsDisabledAndDirtiesEntireOutput) { |
| 98 | const Rect displaySize{100, 200}; |
| 99 | mOutput.editState().bounds = displaySize; |
| 100 | mOutput.editState().isEnabled = true; |
| 101 | |
| 102 | mOutput.setCompositionEnabled(false); |
| 103 | |
| 104 | EXPECT_FALSE(mOutput.getState().isEnabled); |
| 105 | EXPECT_THAT(mOutput.getState().dirtyRegion, RegionEq(Region(displaySize))); |
| 106 | } |
| 107 | |
| 108 | /* ------------------------------------------------------------------------ |
| 109 | * Output::setProjection() |
| 110 | */ |
| 111 | |
| 112 | TEST_F(OutputTest, setProjectionTriviallyWorks) { |
| 113 | const ui::Transform transform{ui::Transform::ROT_180}; |
| 114 | const int32_t orientation = 123; |
| 115 | const Rect frame{1, 2, 3, 4}; |
| 116 | const Rect viewport{5, 6, 7, 8}; |
| 117 | const Rect scissor{9, 10, 11, 12}; |
| 118 | const bool needsFiltering = true; |
| 119 | |
| 120 | mOutput.setProjection(transform, orientation, frame, viewport, scissor, needsFiltering); |
| 121 | |
| 122 | EXPECT_THAT(mOutput.getState().transform, TransformEq(transform)); |
| 123 | EXPECT_EQ(orientation, mOutput.getState().orientation); |
| 124 | EXPECT_EQ(frame, mOutput.getState().frame); |
| 125 | EXPECT_EQ(viewport, mOutput.getState().viewport); |
| 126 | EXPECT_EQ(scissor, mOutput.getState().scissor); |
| 127 | EXPECT_EQ(needsFiltering, mOutput.getState().needsFiltering); |
| 128 | } |
| 129 | |
| 130 | /* ------------------------------------------------------------------------ |
| 131 | * Output::setBounds() |
| 132 | */ |
| 133 | |
| 134 | TEST_F(OutputTest, setBoundsSetsSizeAndDirtiesEntireOutput) { |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 135 | const ui::Size displaySize{100, 200}; |
| 136 | |
| 137 | EXPECT_CALL(*mRenderSurface, setDisplaySize(displaySize)).Times(1); |
| 138 | EXPECT_CALL(*mRenderSurface, getSize()).WillOnce(ReturnRef(displaySize)); |
| 139 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 140 | mOutput.setBounds(displaySize); |
| 141 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 142 | EXPECT_EQ(Rect(displaySize), mOutput.getState().bounds); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 143 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 144 | EXPECT_THAT(mOutput.getState().dirtyRegion, RegionEq(Region(Rect(displaySize)))); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /* ------------------------------------------------------------------------ |
| 148 | * Output::setLayerStackFilter() |
| 149 | */ |
| 150 | |
| 151 | TEST_F(OutputTest, setLayerStackFilterSetsFilterAndDirtiesEntireOutput) { |
| 152 | const Rect displaySize{100, 200}; |
| 153 | mOutput.editState().bounds = displaySize; |
| 154 | |
| 155 | const uint32_t layerStack = 123u; |
| 156 | mOutput.setLayerStackFilter(true, layerStack); |
| 157 | |
| 158 | EXPECT_TRUE(mOutput.getState().singleLayerStack); |
| 159 | EXPECT_EQ(layerStack, mOutput.getState().singleLayerStackId); |
| 160 | |
| 161 | EXPECT_THAT(mOutput.getState().dirtyRegion, RegionEq(Region(displaySize))); |
| 162 | } |
| 163 | |
| 164 | /* ------------------------------------------------------------------------ |
| 165 | * Output::setColorTransform |
| 166 | */ |
| 167 | |
| 168 | TEST_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 | |
| 176 | // Non-identity matrix sets a non-identity state value |
| 177 | const mat4 nonIdentity = mat4() * 2; |
| 178 | |
| 179 | mOutput.setColorTransform(nonIdentity); |
| 180 | |
| 181 | EXPECT_EQ(HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX, mOutput.getState().colorTransform); |
| 182 | } |
| 183 | |
| 184 | /* ------------------------------------------------------------------------ |
| 185 | * Output::setColorMode |
| 186 | */ |
| 187 | |
| 188 | TEST_F(OutputTest, setColorModeSetsModeUnlessNoChange) { |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 189 | EXPECT_CALL(*mRenderSurface, setBufferDataspace(ui::Dataspace::SRGB)).Times(1); |
| 190 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 191 | mOutput.setColorMode(ui::ColorMode::BT2100_PQ, ui::Dataspace::SRGB, |
| 192 | ui::RenderIntent::TONE_MAP_COLORIMETRIC); |
| 193 | |
| 194 | EXPECT_EQ(ui::ColorMode::BT2100_PQ, mOutput.getState().colorMode); |
| 195 | EXPECT_EQ(ui::Dataspace::SRGB, mOutput.getState().dataspace); |
| 196 | EXPECT_EQ(ui::RenderIntent::TONE_MAP_COLORIMETRIC, mOutput.getState().renderIntent); |
| 197 | } |
| 198 | |
| 199 | /* ------------------------------------------------------------------------ |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 200 | * Output::setRenderSurface() |
| 201 | */ |
| 202 | |
| 203 | TEST_F(OutputTest, setRenderSurfaceResetsBounds) { |
| 204 | const ui::Size newDisplaySize{640, 480}; |
| 205 | |
| 206 | mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>(); |
| 207 | EXPECT_CALL(*renderSurface, getSize()).WillOnce(ReturnRef(newDisplaySize)); |
| 208 | |
| 209 | mOutput.setRenderSurface(std::unique_ptr<RenderSurface>(renderSurface)); |
| 210 | |
| 211 | EXPECT_EQ(Rect(newDisplaySize), mOutput.getState().bounds); |
| 212 | } |
| 213 | |
| 214 | /* ------------------------------------------------------------------------ |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 215 | * Output::getPhysicalSpaceDirtyRegion() |
| 216 | */ |
| 217 | |
| 218 | TEST_F(OutputTest, getPhysicalSpaceDirtyRegionWithRepaintEverythingTrue) { |
| 219 | const Rect displaySize{100, 200}; |
| 220 | mOutput.editState().bounds = displaySize; |
| 221 | mOutput.editState().dirtyRegion.set(50, 300); |
| 222 | |
| 223 | { |
| 224 | Region result = mOutput.getPhysicalSpaceDirtyRegion(true); |
| 225 | |
| 226 | EXPECT_THAT(result, RegionEq(Region(displaySize))); |
| 227 | } |
| 228 | |
| 229 | // For repaint everything == true, the returned value does not depend on the display |
| 230 | // rotation. |
| 231 | mOutput.editState().transform.set(ui::Transform::ROT_90, 0, 0); |
| 232 | |
| 233 | { |
| 234 | Region result = mOutput.getPhysicalSpaceDirtyRegion(true); |
| 235 | |
| 236 | EXPECT_THAT(result, RegionEq(Region(displaySize))); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | TEST_F(OutputTest, getPhysicalSpaceDirtyRegionWithRepaintEverythingFalse) { |
| 241 | const Rect displaySize{100, 200}; |
| 242 | mOutput.editState().bounds = displaySize; |
| 243 | mOutput.editState().dirtyRegion.set(50, 300); |
| 244 | |
| 245 | { |
| 246 | Region result = mOutput.getPhysicalSpaceDirtyRegion(false); |
| 247 | |
| 248 | // The dirtyRegion should be clipped to the display bounds. |
| 249 | EXPECT_THAT(result, RegionEq(Region(Rect(50, 200)))); |
| 250 | } |
| 251 | |
| 252 | mOutput.editState().transform.set(ui::Transform::ROT_90, displaySize.getWidth(), |
| 253 | displaySize.getHeight()); |
| 254 | |
| 255 | { |
| 256 | Region result = mOutput.getPhysicalSpaceDirtyRegion(false); |
| 257 | |
| 258 | // The dirtyRegion should be rotated and clipped to the display bounds. |
| 259 | EXPECT_THAT(result, RegionEq(Region(Rect(100, 50)))); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | } // namespace |
| 264 | } // namespace android::compositionengine |