Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -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 | |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 17 | #include <cmath> |
| 18 | |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 19 | #include <compositionengine/DisplayColorProfileCreationArgs.h> |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 20 | #include <compositionengine/DisplayCreationArgs.h> |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 21 | #include <compositionengine/DisplaySurface.h> |
| 22 | #include <compositionengine/RenderSurfaceCreationArgs.h> |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 23 | #include <compositionengine/impl/Display.h> |
| 24 | #include <compositionengine/mock/CompositionEngine.h> |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 25 | #include <compositionengine/mock/DisplayColorProfile.h> |
chaviw | 8beb414 | 2019-04-11 13:09:05 -0700 | [diff] [blame] | 26 | #include <compositionengine/mock/NativeWindow.h> |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 27 | #include <compositionengine/mock/OutputLayer.h> |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 28 | #include <compositionengine/mock/RenderSurface.h> |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 29 | #include <gtest/gtest.h> |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 30 | |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 31 | #include "MockHWC2.h" |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 32 | #include "MockHWComposer.h" |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 33 | #include "MockPowerAdvisor.h" |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 34 | |
| 35 | namespace android::compositionengine { |
| 36 | namespace { |
| 37 | |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 38 | using testing::_; |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 39 | using testing::DoAll; |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 40 | using testing::Return; |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 41 | using testing::ReturnRef; |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 42 | using testing::Sequence; |
| 43 | using testing::SetArgPointee; |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 44 | using testing::StrictMock; |
| 45 | |
| 46 | constexpr DisplayId DEFAULT_DISPLAY_ID = DisplayId{42}; |
| 47 | |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 48 | struct DisplayTest : public testing::Test { |
| 49 | DisplayTest() { |
| 50 | EXPECT_CALL(mCompositionEngine, getHwComposer()).WillRepeatedly(ReturnRef(mHwComposer)); |
| 51 | EXPECT_CALL(*mLayer1, getHwcLayer()).WillRepeatedly(Return(&mHWC2Layer1)); |
| 52 | EXPECT_CALL(*mLayer2, getHwcLayer()).WillRepeatedly(Return(&mHWC2Layer2)); |
| 53 | EXPECT_CALL(*mLayer3, getHwcLayer()).WillRepeatedly(Return(nullptr)); |
| 54 | |
| 55 | std::vector<std::unique_ptr<OutputLayer>> layers; |
| 56 | layers.emplace_back(mLayer1); |
| 57 | layers.emplace_back(mLayer2); |
| 58 | layers.emplace_back(mLayer3); |
| 59 | mDisplay.setOutputLayersOrderedByZ(std::move(layers)); |
| 60 | } |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 61 | |
| 62 | StrictMock<android::mock::HWComposer> mHwComposer; |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 63 | StrictMock<Hwc2::mock::PowerAdvisor> mPowerAdvisor; |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 64 | StrictMock<mock::CompositionEngine> mCompositionEngine; |
chaviw | 8beb414 | 2019-04-11 13:09:05 -0700 | [diff] [blame] | 65 | sp<mock::NativeWindow> mNativeWindow = new StrictMock<mock::NativeWindow>(); |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 66 | StrictMock<HWC2::mock::Layer> mHWC2Layer1; |
| 67 | StrictMock<HWC2::mock::Layer> mHWC2Layer2; |
| 68 | StrictMock<HWC2::mock::Layer> mHWC2LayerUnknown; |
| 69 | mock::OutputLayer* mLayer1 = new StrictMock<mock::OutputLayer>(); |
| 70 | mock::OutputLayer* mLayer2 = new StrictMock<mock::OutputLayer>(); |
| 71 | mock::OutputLayer* mLayer3 = new StrictMock<mock::OutputLayer>(); |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 72 | impl::Display mDisplay{mCompositionEngine, |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 73 | DisplayCreationArgsBuilder() |
| 74 | .setDisplayId(DEFAULT_DISPLAY_ID) |
| 75 | .setPowerAdvisor(&mPowerAdvisor) |
| 76 | .build()}; |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 79 | /* |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 80 | * Basic construction |
| 81 | */ |
| 82 | |
| 83 | TEST_F(DisplayTest, canInstantiateDisplay) { |
| 84 | { |
| 85 | constexpr DisplayId display1 = DisplayId{123u}; |
| 86 | auto display = |
| 87 | impl::createDisplay(mCompositionEngine, |
| 88 | DisplayCreationArgsBuilder().setDisplayId(display1).build()); |
| 89 | EXPECT_FALSE(display->isSecure()); |
| 90 | EXPECT_FALSE(display->isVirtual()); |
| 91 | EXPECT_EQ(display1, display->getId()); |
| 92 | } |
| 93 | |
| 94 | { |
| 95 | constexpr DisplayId display2 = DisplayId{546u}; |
| 96 | auto display = impl::createDisplay(mCompositionEngine, |
| 97 | DisplayCreationArgsBuilder() |
| 98 | .setIsSecure(true) |
| 99 | .setDisplayId(display2) |
| 100 | .build()); |
| 101 | EXPECT_TRUE(display->isSecure()); |
| 102 | EXPECT_FALSE(display->isVirtual()); |
| 103 | EXPECT_EQ(display2, display->getId()); |
| 104 | } |
| 105 | |
| 106 | { |
| 107 | constexpr DisplayId display3 = DisplayId{789u}; |
| 108 | auto display = impl::createDisplay(mCompositionEngine, |
| 109 | DisplayCreationArgsBuilder() |
| 110 | .setIsVirtual(true) |
| 111 | .setDisplayId(display3) |
| 112 | .build()); |
| 113 | EXPECT_FALSE(display->isSecure()); |
| 114 | EXPECT_TRUE(display->isVirtual()); |
| 115 | EXPECT_EQ(display3, display->getId()); |
| 116 | } |
| 117 | } |
| 118 | |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 119 | /* |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 120 | * Display::disconnect() |
| 121 | */ |
| 122 | |
| 123 | TEST_F(DisplayTest, disconnectDisconnectsDisplay) { |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 124 | // The first call to disconnect will disconnect the display with the HWC and |
| 125 | // set mHwcId to -1. |
| 126 | EXPECT_CALL(mHwComposer, disconnectDisplay(DEFAULT_DISPLAY_ID)).Times(1); |
| 127 | mDisplay.disconnect(); |
| 128 | EXPECT_FALSE(mDisplay.getId()); |
| 129 | |
| 130 | // Subsequent calls will do nothing, |
| 131 | EXPECT_CALL(mHwComposer, disconnectDisplay(DEFAULT_DISPLAY_ID)).Times(0); |
| 132 | mDisplay.disconnect(); |
| 133 | EXPECT_FALSE(mDisplay.getId()); |
| 134 | } |
| 135 | |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 136 | /* |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 137 | * Display::setColorTransform() |
| 138 | */ |
| 139 | |
| 140 | TEST_F(DisplayTest, setColorTransformSetsTransform) { |
| 141 | // Identity matrix sets an identity state value |
| 142 | const mat4 identity; |
| 143 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 144 | EXPECT_CALL(mHwComposer, setColorTransform(DEFAULT_DISPLAY_ID, identity)).Times(1); |
| 145 | |
| 146 | mDisplay.setColorTransform(identity); |
| 147 | |
| 148 | EXPECT_EQ(HAL_COLOR_TRANSFORM_IDENTITY, mDisplay.getState().colorTransform); |
| 149 | |
| 150 | // Non-identity matrix sets a non-identity state value |
| 151 | const mat4 nonIdentity = mat4() * 2; |
| 152 | |
| 153 | EXPECT_CALL(mHwComposer, setColorTransform(DEFAULT_DISPLAY_ID, nonIdentity)).Times(1); |
| 154 | |
| 155 | mDisplay.setColorTransform(nonIdentity); |
| 156 | |
| 157 | EXPECT_EQ(HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX, mDisplay.getState().colorTransform); |
| 158 | } |
| 159 | |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 160 | /* |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 161 | * Display::setColorMode() |
| 162 | */ |
| 163 | |
| 164 | TEST_F(DisplayTest, setColorModeSetsModeUnlessNoChange) { |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 165 | mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>(); |
| 166 | mDisplay.setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface)); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 167 | mock::DisplayColorProfile* colorProfile = new StrictMock<mock::DisplayColorProfile>(); |
| 168 | mDisplay.setDisplayColorProfileForTest(std::unique_ptr<DisplayColorProfile>(colorProfile)); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 169 | |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 170 | EXPECT_CALL(*colorProfile, getTargetDataspace(_, _, _)) |
| 171 | .WillRepeatedly(Return(ui::Dataspace::UNKNOWN)); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 172 | |
| 173 | // These values are expected to be the initial state. |
| 174 | ASSERT_EQ(ui::ColorMode::NATIVE, mDisplay.getState().colorMode); |
| 175 | ASSERT_EQ(ui::Dataspace::UNKNOWN, mDisplay.getState().dataspace); |
| 176 | ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mDisplay.getState().renderIntent); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 177 | ASSERT_EQ(ui::Dataspace::UNKNOWN, mDisplay.getState().targetDataspace); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 178 | |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 179 | // If the set values are unchanged, nothing happens |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 180 | mDisplay.setColorMode(ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN, |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 181 | ui::RenderIntent::COLORIMETRIC, ui::Dataspace::UNKNOWN); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 182 | |
| 183 | EXPECT_EQ(ui::ColorMode::NATIVE, mDisplay.getState().colorMode); |
| 184 | EXPECT_EQ(ui::Dataspace::UNKNOWN, mDisplay.getState().dataspace); |
| 185 | EXPECT_EQ(ui::RenderIntent::COLORIMETRIC, mDisplay.getState().renderIntent); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 186 | EXPECT_EQ(ui::Dataspace::UNKNOWN, mDisplay.getState().targetDataspace); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 187 | |
| 188 | // Otherwise if the values are different, updates happen |
Lloyd Pique | ef95812 | 2019-02-05 18:00:12 -0800 | [diff] [blame] | 189 | EXPECT_CALL(*renderSurface, setBufferDataspace(ui::Dataspace::DISPLAY_P3)).Times(1); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 190 | EXPECT_CALL(mHwComposer, |
Lloyd Pique | ef95812 | 2019-02-05 18:00:12 -0800 | [diff] [blame] | 191 | setActiveColorMode(DEFAULT_DISPLAY_ID, ui::ColorMode::DISPLAY_P3, |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 192 | ui::RenderIntent::TONE_MAP_COLORIMETRIC)) |
| 193 | .Times(1); |
| 194 | |
Lloyd Pique | ef95812 | 2019-02-05 18:00:12 -0800 | [diff] [blame] | 195 | mDisplay.setColorMode(ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3, |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 196 | ui::RenderIntent::TONE_MAP_COLORIMETRIC, ui::Dataspace::UNKNOWN); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 197 | |
Lloyd Pique | ef95812 | 2019-02-05 18:00:12 -0800 | [diff] [blame] | 198 | EXPECT_EQ(ui::ColorMode::DISPLAY_P3, mDisplay.getState().colorMode); |
| 199 | EXPECT_EQ(ui::Dataspace::DISPLAY_P3, mDisplay.getState().dataspace); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 200 | EXPECT_EQ(ui::RenderIntent::TONE_MAP_COLORIMETRIC, mDisplay.getState().renderIntent); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 201 | EXPECT_EQ(ui::Dataspace::UNKNOWN, mDisplay.getState().targetDataspace); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | TEST_F(DisplayTest, setColorModeDoesNothingForVirtualDisplay) { |
| 205 | impl::Display virtualDisplay{mCompositionEngine, |
| 206 | DisplayCreationArgs{false, true, DEFAULT_DISPLAY_ID}}; |
| 207 | |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 208 | mock::DisplayColorProfile* colorProfile = new StrictMock<mock::DisplayColorProfile>(); |
| 209 | virtualDisplay.setDisplayColorProfileForTest( |
| 210 | std::unique_ptr<DisplayColorProfile>(colorProfile)); |
| 211 | |
| 212 | EXPECT_CALL(*colorProfile, |
| 213 | getTargetDataspace(ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3, |
| 214 | ui::Dataspace::UNKNOWN)) |
| 215 | .WillOnce(Return(ui::Dataspace::UNKNOWN)); |
| 216 | |
Lloyd Pique | ef95812 | 2019-02-05 18:00:12 -0800 | [diff] [blame] | 217 | virtualDisplay.setColorMode(ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3, |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 218 | ui::RenderIntent::TONE_MAP_COLORIMETRIC, ui::Dataspace::UNKNOWN); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 219 | |
| 220 | EXPECT_EQ(ui::ColorMode::NATIVE, virtualDisplay.getState().colorMode); |
| 221 | EXPECT_EQ(ui::Dataspace::UNKNOWN, virtualDisplay.getState().dataspace); |
| 222 | EXPECT_EQ(ui::RenderIntent::COLORIMETRIC, virtualDisplay.getState().renderIntent); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 223 | EXPECT_EQ(ui::Dataspace::UNKNOWN, mDisplay.getState().targetDataspace); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 226 | /* |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 227 | * Display::createDisplayColorProfile() |
| 228 | */ |
| 229 | |
| 230 | TEST_F(DisplayTest, createDisplayColorProfileSetsDisplayColorProfile) { |
| 231 | EXPECT_TRUE(mDisplay.getDisplayColorProfile() == nullptr); |
| 232 | mDisplay.createDisplayColorProfile( |
| 233 | DisplayColorProfileCreationArgs{false, HdrCapabilities(), 0, |
| 234 | DisplayColorProfileCreationArgs::HwcColorModes()}); |
| 235 | EXPECT_TRUE(mDisplay.getDisplayColorProfile() != nullptr); |
| 236 | } |
| 237 | |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 238 | /* |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 239 | * Display::createRenderSurface() |
| 240 | */ |
| 241 | |
| 242 | TEST_F(DisplayTest, createRenderSurfaceSetsRenderSurface) { |
chaviw | 8beb414 | 2019-04-11 13:09:05 -0700 | [diff] [blame] | 243 | EXPECT_CALL(*mNativeWindow, disconnect(NATIVE_WINDOW_API_EGL)).WillRepeatedly(Return(NO_ERROR)); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 244 | EXPECT_TRUE(mDisplay.getRenderSurface() == nullptr); |
chaviw | 8beb414 | 2019-04-11 13:09:05 -0700 | [diff] [blame] | 245 | mDisplay.createRenderSurface(RenderSurfaceCreationArgs{640, 480, mNativeWindow, nullptr}); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 246 | EXPECT_TRUE(mDisplay.getRenderSurface() != nullptr); |
| 247 | } |
| 248 | |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 249 | /* |
| 250 | * Display::chooseCompositionStrategy() |
| 251 | */ |
| 252 | |
| 253 | struct DisplayChooseCompositionStrategyTest : public testing::Test { |
| 254 | struct DisplayPartialMock : public impl::Display { |
| 255 | DisplayPartialMock(const compositionengine::CompositionEngine& compositionEngine, |
| 256 | compositionengine::DisplayCreationArgs&& args) |
| 257 | : impl::Display(compositionEngine, std::move(args)) {} |
| 258 | |
| 259 | // Sets up the helper functions called by chooseCompositionStrategy to |
| 260 | // use a mock implementations. |
| 261 | MOCK_CONST_METHOD0(anyLayersRequireClientComposition, bool()); |
| 262 | MOCK_CONST_METHOD0(allLayersRequireClientComposition, bool()); |
| 263 | MOCK_METHOD1(applyChangedTypesToLayers, void(const impl::Display::ChangedTypes&)); |
| 264 | MOCK_METHOD1(applyDisplayRequests, void(const impl::Display::DisplayRequests&)); |
| 265 | MOCK_METHOD1(applyLayerRequestsToLayers, void(const impl::Display::LayerRequests&)); |
| 266 | }; |
| 267 | |
| 268 | DisplayChooseCompositionStrategyTest() { |
| 269 | EXPECT_CALL(mCompositionEngine, getHwComposer()).WillRepeatedly(ReturnRef(mHwComposer)); |
| 270 | } |
| 271 | |
| 272 | StrictMock<android::mock::HWComposer> mHwComposer; |
| 273 | StrictMock<mock::CompositionEngine> mCompositionEngine; |
| 274 | StrictMock<DisplayPartialMock> |
| 275 | mDisplay{mCompositionEngine, |
| 276 | DisplayCreationArgsBuilder().setDisplayId(DEFAULT_DISPLAY_ID).build()}; |
| 277 | }; |
| 278 | |
| 279 | TEST_F(DisplayChooseCompositionStrategyTest, takesEarlyOutIfNotAHwcDisplay) { |
| 280 | impl::Display nonHwcDisplay{mCompositionEngine, DisplayCreationArgsBuilder().build()}; |
| 281 | EXPECT_FALSE(nonHwcDisplay.getId()); |
| 282 | |
| 283 | nonHwcDisplay.chooseCompositionStrategy(); |
| 284 | |
| 285 | auto& state = nonHwcDisplay.getState(); |
| 286 | EXPECT_TRUE(state.usesClientComposition); |
| 287 | EXPECT_FALSE(state.usesDeviceComposition); |
| 288 | } |
| 289 | |
| 290 | TEST_F(DisplayChooseCompositionStrategyTest, takesEarlyOutOnHwcError) { |
| 291 | EXPECT_CALL(mDisplay, anyLayersRequireClientComposition()).WillOnce(Return(false)); |
| 292 | EXPECT_CALL(mHwComposer, getDeviceCompositionChanges(DEFAULT_DISPLAY_ID, false, _)) |
| 293 | .WillOnce(Return(INVALID_OPERATION)); |
| 294 | |
| 295 | mDisplay.chooseCompositionStrategy(); |
| 296 | |
| 297 | auto& state = mDisplay.getState(); |
| 298 | EXPECT_TRUE(state.usesClientComposition); |
| 299 | EXPECT_FALSE(state.usesDeviceComposition); |
| 300 | } |
| 301 | |
| 302 | TEST_F(DisplayChooseCompositionStrategyTest, normalOperation) { |
| 303 | // Since two calls are made to anyLayersRequireClientComposition with different return values, |
| 304 | // use a Sequence to control the matching so the values are returned in a known order. |
| 305 | Sequence s; |
| 306 | EXPECT_CALL(mDisplay, anyLayersRequireClientComposition()).InSequence(s).WillOnce(Return(true)); |
| 307 | EXPECT_CALL(mDisplay, anyLayersRequireClientComposition()) |
| 308 | .InSequence(s) |
| 309 | .WillOnce(Return(false)); |
| 310 | |
| 311 | EXPECT_CALL(mHwComposer, getDeviceCompositionChanges(DEFAULT_DISPLAY_ID, true, _)) |
| 312 | .WillOnce(Return(NO_ERROR)); |
| 313 | EXPECT_CALL(mDisplay, allLayersRequireClientComposition()).WillOnce(Return(false)); |
| 314 | |
| 315 | mDisplay.chooseCompositionStrategy(); |
| 316 | |
| 317 | auto& state = mDisplay.getState(); |
| 318 | EXPECT_FALSE(state.usesClientComposition); |
| 319 | EXPECT_TRUE(state.usesDeviceComposition); |
| 320 | } |
| 321 | |
| 322 | TEST_F(DisplayChooseCompositionStrategyTest, normalOperationWithChanges) { |
| 323 | android::HWComposer::DeviceRequestedChanges changes{ |
| 324 | {{nullptr, HWC2::Composition::Client}}, |
| 325 | HWC2::DisplayRequest::FlipClientTarget, |
| 326 | {{nullptr, HWC2::LayerRequest::ClearClientTarget}}, |
| 327 | }; |
| 328 | |
| 329 | // Since two calls are made to anyLayersRequireClientComposition with different return values, |
| 330 | // use a Sequence to control the matching so the values are returned in a known order. |
| 331 | Sequence s; |
| 332 | EXPECT_CALL(mDisplay, anyLayersRequireClientComposition()).InSequence(s).WillOnce(Return(true)); |
| 333 | EXPECT_CALL(mDisplay, anyLayersRequireClientComposition()) |
| 334 | .InSequence(s) |
| 335 | .WillOnce(Return(false)); |
| 336 | |
| 337 | EXPECT_CALL(mHwComposer, getDeviceCompositionChanges(DEFAULT_DISPLAY_ID, true, _)) |
| 338 | .WillOnce(DoAll(SetArgPointee<2>(changes), Return(NO_ERROR))); |
| 339 | EXPECT_CALL(mDisplay, applyChangedTypesToLayers(changes.changedTypes)).Times(1); |
| 340 | EXPECT_CALL(mDisplay, applyDisplayRequests(changes.displayRequests)).Times(1); |
| 341 | EXPECT_CALL(mDisplay, applyLayerRequestsToLayers(changes.layerRequests)).Times(1); |
| 342 | EXPECT_CALL(mDisplay, allLayersRequireClientComposition()).WillOnce(Return(false)); |
| 343 | |
| 344 | mDisplay.chooseCompositionStrategy(); |
| 345 | |
| 346 | auto& state = mDisplay.getState(); |
| 347 | EXPECT_FALSE(state.usesClientComposition); |
| 348 | EXPECT_TRUE(state.usesDeviceComposition); |
| 349 | } |
| 350 | |
| 351 | /* |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 352 | * Display::getSkipColorTransform() |
| 353 | */ |
| 354 | |
| 355 | TEST_F(DisplayTest, getSkipColorTransformDoesNothingIfNonHwcDisplay) { |
| 356 | auto nonHwcDisplay{ |
| 357 | impl::createDisplay(mCompositionEngine, DisplayCreationArgsBuilder().build())}; |
| 358 | EXPECT_FALSE(nonHwcDisplay->getSkipColorTransform()); |
| 359 | } |
| 360 | |
| 361 | TEST_F(DisplayTest, getSkipColorTransformChecksHwcCapability) { |
| 362 | EXPECT_CALL(mHwComposer, |
| 363 | hasDisplayCapability(std::make_optional(DEFAULT_DISPLAY_ID), |
| 364 | HWC2::DisplayCapability::SkipClientColorTransform)) |
| 365 | .WillOnce(Return(true)); |
| 366 | EXPECT_TRUE(mDisplay.getSkipColorTransform()); |
| 367 | } |
| 368 | |
| 369 | /* |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 370 | * Display::anyLayersRequireClientComposition() |
| 371 | */ |
| 372 | |
| 373 | TEST_F(DisplayTest, anyLayersRequireClientCompositionReturnsFalse) { |
| 374 | EXPECT_CALL(*mLayer1, requiresClientComposition()).WillOnce(Return(false)); |
| 375 | EXPECT_CALL(*mLayer2, requiresClientComposition()).WillOnce(Return(false)); |
| 376 | EXPECT_CALL(*mLayer3, requiresClientComposition()).WillOnce(Return(false)); |
| 377 | |
| 378 | EXPECT_FALSE(mDisplay.anyLayersRequireClientComposition()); |
| 379 | } |
| 380 | |
| 381 | TEST_F(DisplayTest, anyLayersRequireClientCompositionReturnsTrue) { |
| 382 | EXPECT_CALL(*mLayer1, requiresClientComposition()).WillOnce(Return(false)); |
| 383 | EXPECT_CALL(*mLayer2, requiresClientComposition()).WillOnce(Return(true)); |
| 384 | |
| 385 | EXPECT_TRUE(mDisplay.anyLayersRequireClientComposition()); |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | * Display::allLayersRequireClientComposition() |
| 390 | */ |
| 391 | |
| 392 | TEST_F(DisplayTest, allLayersRequireClientCompositionReturnsTrue) { |
| 393 | EXPECT_CALL(*mLayer1, requiresClientComposition()).WillOnce(Return(true)); |
| 394 | EXPECT_CALL(*mLayer2, requiresClientComposition()).WillOnce(Return(true)); |
| 395 | EXPECT_CALL(*mLayer3, requiresClientComposition()).WillOnce(Return(true)); |
| 396 | |
| 397 | EXPECT_TRUE(mDisplay.allLayersRequireClientComposition()); |
| 398 | } |
| 399 | |
| 400 | TEST_F(DisplayTest, allLayersRequireClientCompositionReturnsFalse) { |
| 401 | EXPECT_CALL(*mLayer1, requiresClientComposition()).WillOnce(Return(true)); |
| 402 | EXPECT_CALL(*mLayer2, requiresClientComposition()).WillOnce(Return(false)); |
| 403 | |
| 404 | EXPECT_FALSE(mDisplay.allLayersRequireClientComposition()); |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * Display::applyChangedTypesToLayers() |
| 409 | */ |
| 410 | |
| 411 | TEST_F(DisplayTest, applyChangedTypesToLayersTakesEarlyOutIfNoChangedLayers) { |
| 412 | mDisplay.applyChangedTypesToLayers(impl::Display::ChangedTypes()); |
| 413 | } |
| 414 | |
| 415 | TEST_F(DisplayTest, applyChangedTypesToLayersAppliesChanges) { |
| 416 | EXPECT_CALL(*mLayer1, |
| 417 | applyDeviceCompositionTypeChange(Hwc2::IComposerClient::Composition::CLIENT)) |
| 418 | .Times(1); |
| 419 | EXPECT_CALL(*mLayer2, |
| 420 | applyDeviceCompositionTypeChange(Hwc2::IComposerClient::Composition::DEVICE)) |
| 421 | .Times(1); |
| 422 | |
| 423 | mDisplay.applyChangedTypesToLayers(impl::Display::ChangedTypes{ |
| 424 | {&mHWC2Layer1, HWC2::Composition::Client}, |
| 425 | {&mHWC2Layer2, HWC2::Composition::Device}, |
| 426 | {&mHWC2LayerUnknown, HWC2::Composition::SolidColor}, |
| 427 | }); |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * Display::applyDisplayRequests() |
| 432 | */ |
| 433 | |
| 434 | TEST_F(DisplayTest, applyDisplayRequestsToLayersHandlesNoRequests) { |
| 435 | mDisplay.applyDisplayRequests(static_cast<HWC2::DisplayRequest>(0)); |
| 436 | |
| 437 | auto& state = mDisplay.getState(); |
| 438 | EXPECT_FALSE(state.flipClientTarget); |
| 439 | } |
| 440 | |
| 441 | TEST_F(DisplayTest, applyDisplayRequestsToLayersHandlesFlipClientTarget) { |
| 442 | mDisplay.applyDisplayRequests(HWC2::DisplayRequest::FlipClientTarget); |
| 443 | |
| 444 | auto& state = mDisplay.getState(); |
| 445 | EXPECT_TRUE(state.flipClientTarget); |
| 446 | } |
| 447 | |
| 448 | TEST_F(DisplayTest, applyDisplayRequestsToLayersHandlesWriteClientTargetToOutput) { |
| 449 | mDisplay.applyDisplayRequests(HWC2::DisplayRequest::WriteClientTargetToOutput); |
| 450 | |
| 451 | auto& state = mDisplay.getState(); |
| 452 | EXPECT_FALSE(state.flipClientTarget); |
| 453 | } |
| 454 | |
| 455 | TEST_F(DisplayTest, applyDisplayRequestsToLayersHandlesAllRequestFlagsSet) { |
| 456 | mDisplay.applyDisplayRequests(static_cast<HWC2::DisplayRequest>(~0)); |
| 457 | |
| 458 | auto& state = mDisplay.getState(); |
| 459 | EXPECT_TRUE(state.flipClientTarget); |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * Display::applyLayerRequestsToLayers() |
| 464 | */ |
| 465 | |
| 466 | TEST_F(DisplayTest, applyLayerRequestsToLayersPreparesAllLayers) { |
| 467 | EXPECT_CALL(*mLayer1, prepareForDeviceLayerRequests()).Times(1); |
| 468 | EXPECT_CALL(*mLayer2, prepareForDeviceLayerRequests()).Times(1); |
| 469 | EXPECT_CALL(*mLayer3, prepareForDeviceLayerRequests()).Times(1); |
| 470 | |
| 471 | mDisplay.applyLayerRequestsToLayers(impl::Display::LayerRequests()); |
| 472 | } |
| 473 | |
| 474 | TEST_F(DisplayTest, applyLayerRequestsToLayers2) { |
| 475 | EXPECT_CALL(*mLayer1, prepareForDeviceLayerRequests()).Times(1); |
| 476 | EXPECT_CALL(*mLayer2, prepareForDeviceLayerRequests()).Times(1); |
| 477 | EXPECT_CALL(*mLayer3, prepareForDeviceLayerRequests()).Times(1); |
| 478 | |
| 479 | EXPECT_CALL(*mLayer1, |
| 480 | applyDeviceLayerRequest(Hwc2::IComposerClient::LayerRequest::CLEAR_CLIENT_TARGET)) |
| 481 | .Times(1); |
| 482 | |
| 483 | mDisplay.applyLayerRequestsToLayers(impl::Display::LayerRequests{ |
| 484 | {&mHWC2Layer1, HWC2::LayerRequest::ClearClientTarget}, |
| 485 | {&mHWC2LayerUnknown, HWC2::LayerRequest::ClearClientTarget}, |
| 486 | }); |
| 487 | } |
| 488 | |
Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 489 | /* |
| 490 | * Display::presentAndGetFrameFences() |
| 491 | */ |
| 492 | |
| 493 | TEST_F(DisplayTest, presentAndGetFrameFencesReturnsNoFencesOnNonHwcDisplay) { |
| 494 | auto nonHwcDisplay{ |
| 495 | impl::createDisplay(mCompositionEngine, DisplayCreationArgsBuilder().build())}; |
| 496 | |
| 497 | auto result = nonHwcDisplay->presentAndGetFrameFences(); |
| 498 | |
| 499 | ASSERT_TRUE(result.presentFence.get()); |
| 500 | EXPECT_FALSE(result.presentFence->isValid()); |
| 501 | EXPECT_EQ(0u, result.layerFences.size()); |
| 502 | } |
| 503 | |
| 504 | TEST_F(DisplayTest, presentAndGetFrameFencesReturnsPresentAndLayerFences) { |
| 505 | sp<Fence> presentFence = new Fence(); |
| 506 | sp<Fence> layer1Fence = new Fence(); |
| 507 | sp<Fence> layer2Fence = new Fence(); |
| 508 | |
| 509 | EXPECT_CALL(mHwComposer, presentAndGetReleaseFences(DEFAULT_DISPLAY_ID)).Times(1); |
| 510 | EXPECT_CALL(mHwComposer, getPresentFence(DEFAULT_DISPLAY_ID)).WillOnce(Return(presentFence)); |
| 511 | EXPECT_CALL(mHwComposer, getLayerReleaseFence(DEFAULT_DISPLAY_ID, &mHWC2Layer1)) |
| 512 | .WillOnce(Return(layer1Fence)); |
| 513 | EXPECT_CALL(mHwComposer, getLayerReleaseFence(DEFAULT_DISPLAY_ID, &mHWC2Layer2)) |
| 514 | .WillOnce(Return(layer2Fence)); |
| 515 | EXPECT_CALL(mHwComposer, clearReleaseFences(DEFAULT_DISPLAY_ID)).Times(1); |
| 516 | |
| 517 | auto result = mDisplay.presentAndGetFrameFences(); |
| 518 | |
| 519 | EXPECT_EQ(presentFence, result.presentFence); |
| 520 | |
| 521 | EXPECT_EQ(2u, result.layerFences.size()); |
| 522 | ASSERT_EQ(1, result.layerFences.count(&mHWC2Layer1)); |
| 523 | EXPECT_EQ(layer1Fence, result.layerFences[&mHWC2Layer1]); |
| 524 | ASSERT_EQ(1, result.layerFences.count(&mHWC2Layer2)); |
| 525 | EXPECT_EQ(layer2Fence, result.layerFences[&mHWC2Layer2]); |
| 526 | } |
| 527 | |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 528 | /* |
| 529 | * Display::setExpensiveRenderingExpected() |
| 530 | */ |
| 531 | |
| 532 | TEST_F(DisplayTest, setExpensiveRenderingExpectedForwardsToPowerAdvisor) { |
| 533 | EXPECT_CALL(mPowerAdvisor, setExpensiveRenderingExpected(DEFAULT_DISPLAY_ID, true)).Times(1); |
| 534 | mDisplay.setExpensiveRenderingExpected(true); |
| 535 | |
| 536 | EXPECT_CALL(mPowerAdvisor, setExpensiveRenderingExpected(DEFAULT_DISPLAY_ID, false)).Times(1); |
| 537 | mDisplay.setExpensiveRenderingExpected(false); |
| 538 | } |
| 539 | |
Lloyd Pique | d3d6988 | 2019-02-28 16:03:46 -0800 | [diff] [blame^] | 540 | /* |
| 541 | * Display::finishFrame() |
| 542 | */ |
| 543 | |
| 544 | TEST_F(DisplayTest, finishFrameDoesNotSkipCompositionIfNotDirtyOnHwcDisplay) { |
| 545 | mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>(); |
| 546 | mDisplay.setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface)); |
| 547 | |
| 548 | // We expect no calls to queueBuffer if composition was skipped. |
| 549 | EXPECT_CALL(*renderSurface, queueBuffer(_)).Times(1); |
| 550 | |
| 551 | mDisplay.editState().isEnabled = true; |
| 552 | mDisplay.editState().usesClientComposition = false; |
| 553 | mDisplay.editState().viewport = Rect(0, 0, 1, 1); |
| 554 | mDisplay.editState().dirtyRegion = Region::INVALID_REGION; |
| 555 | |
| 556 | CompositionRefreshArgs refreshArgs; |
| 557 | refreshArgs.repaintEverything = false; |
| 558 | |
| 559 | mDisplay.finishFrame(refreshArgs); |
| 560 | } |
| 561 | |
| 562 | TEST_F(DisplayTest, finishFrameSkipsCompositionIfNotDirty) { |
| 563 | std::shared_ptr<impl::Display> nonHwcDisplay{ |
| 564 | impl::createDisplay(mCompositionEngine, DisplayCreationArgsBuilder().build())}; |
| 565 | |
| 566 | mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>(); |
| 567 | nonHwcDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface)); |
| 568 | |
| 569 | // We expect no calls to queueBuffer if composition was skipped. |
| 570 | EXPECT_CALL(*renderSurface, queueBuffer(_)).Times(0); |
| 571 | |
| 572 | nonHwcDisplay->editState().isEnabled = true; |
| 573 | nonHwcDisplay->editState().usesClientComposition = false; |
| 574 | nonHwcDisplay->editState().viewport = Rect(0, 0, 1, 1); |
| 575 | nonHwcDisplay->editState().dirtyRegion = Region::INVALID_REGION; |
| 576 | |
| 577 | CompositionRefreshArgs refreshArgs; |
| 578 | refreshArgs.repaintEverything = false; |
| 579 | |
| 580 | nonHwcDisplay->finishFrame(refreshArgs); |
| 581 | } |
| 582 | |
| 583 | TEST_F(DisplayTest, finishFramePerformsCompositionIfDirty) { |
| 584 | std::shared_ptr<impl::Display> nonHwcDisplay{ |
| 585 | impl::createDisplay(mCompositionEngine, DisplayCreationArgsBuilder().build())}; |
| 586 | |
| 587 | mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>(); |
| 588 | nonHwcDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface)); |
| 589 | |
| 590 | // We expect a single call to queueBuffer when composition is not skipped. |
| 591 | EXPECT_CALL(*renderSurface, queueBuffer(_)).Times(1); |
| 592 | |
| 593 | nonHwcDisplay->editState().isEnabled = true; |
| 594 | nonHwcDisplay->editState().usesClientComposition = false; |
| 595 | nonHwcDisplay->editState().viewport = Rect(0, 0, 1, 1); |
| 596 | nonHwcDisplay->editState().dirtyRegion = Region(Rect(0, 0, 1, 1)); |
| 597 | |
| 598 | CompositionRefreshArgs refreshArgs; |
| 599 | refreshArgs.repaintEverything = false; |
| 600 | |
| 601 | nonHwcDisplay->finishFrame(refreshArgs); |
| 602 | } |
| 603 | |
| 604 | TEST_F(DisplayTest, finishFramePerformsCompositionIfRepaintEverything) { |
| 605 | std::shared_ptr<impl::Display> nonHwcDisplay{ |
| 606 | impl::createDisplay(mCompositionEngine, DisplayCreationArgsBuilder().build())}; |
| 607 | |
| 608 | mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>(); |
| 609 | nonHwcDisplay->setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface)); |
| 610 | |
| 611 | // We expect a single call to queueBuffer when composition is not skipped. |
| 612 | EXPECT_CALL(*renderSurface, queueBuffer(_)).Times(1); |
| 613 | |
| 614 | nonHwcDisplay->editState().isEnabled = true; |
| 615 | nonHwcDisplay->editState().usesClientComposition = false; |
| 616 | nonHwcDisplay->editState().viewport = Rect(0, 0, 1, 1); |
| 617 | nonHwcDisplay->editState().dirtyRegion = Region::INVALID_REGION; |
| 618 | |
| 619 | CompositionRefreshArgs refreshArgs; |
| 620 | refreshArgs.repaintEverything = true; |
| 621 | |
| 622 | nonHwcDisplay->finishFrame(refreshArgs); |
| 623 | } |
| 624 | |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 625 | } // namespace |
| 626 | } // namespace android::compositionengine |