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