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 | 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> |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 27 | #include <system/window.h> |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 28 | |
| 29 | #include "MockHWComposer.h" |
| 30 | |
| 31 | namespace android::compositionengine { |
| 32 | namespace { |
| 33 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 34 | using testing::Return; |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 35 | using testing::ReturnRef; |
| 36 | using testing::StrictMock; |
| 37 | |
| 38 | constexpr DisplayId DEFAULT_DISPLAY_ID = DisplayId{42}; |
| 39 | |
| 40 | class DisplayTest : public testing::Test { |
| 41 | public: |
| 42 | ~DisplayTest() override = default; |
| 43 | |
| 44 | StrictMock<android::mock::HWComposer> mHwComposer; |
| 45 | StrictMock<mock::CompositionEngine> mCompositionEngine; |
| 46 | impl::Display mDisplay{mCompositionEngine, |
| 47 | DisplayCreationArgsBuilder().setDisplayId(DEFAULT_DISPLAY_ID).build()}; |
| 48 | }; |
| 49 | |
| 50 | /* ------------------------------------------------------------------------ |
| 51 | * Basic construction |
| 52 | */ |
| 53 | |
| 54 | TEST_F(DisplayTest, canInstantiateDisplay) { |
| 55 | { |
| 56 | constexpr DisplayId display1 = DisplayId{123u}; |
| 57 | auto display = |
| 58 | impl::createDisplay(mCompositionEngine, |
| 59 | DisplayCreationArgsBuilder().setDisplayId(display1).build()); |
| 60 | EXPECT_FALSE(display->isSecure()); |
| 61 | EXPECT_FALSE(display->isVirtual()); |
| 62 | EXPECT_EQ(display1, display->getId()); |
| 63 | } |
| 64 | |
| 65 | { |
| 66 | constexpr DisplayId display2 = DisplayId{546u}; |
| 67 | auto display = impl::createDisplay(mCompositionEngine, |
| 68 | DisplayCreationArgsBuilder() |
| 69 | .setIsSecure(true) |
| 70 | .setDisplayId(display2) |
| 71 | .build()); |
| 72 | EXPECT_TRUE(display->isSecure()); |
| 73 | EXPECT_FALSE(display->isVirtual()); |
| 74 | EXPECT_EQ(display2, display->getId()); |
| 75 | } |
| 76 | |
| 77 | { |
| 78 | constexpr DisplayId display3 = DisplayId{789u}; |
| 79 | auto display = impl::createDisplay(mCompositionEngine, |
| 80 | DisplayCreationArgsBuilder() |
| 81 | .setIsVirtual(true) |
| 82 | .setDisplayId(display3) |
| 83 | .build()); |
| 84 | EXPECT_FALSE(display->isSecure()); |
| 85 | EXPECT_TRUE(display->isVirtual()); |
| 86 | EXPECT_EQ(display3, display->getId()); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /* ------------------------------------------------------------------------ |
| 91 | * Display::disconnect() |
| 92 | */ |
| 93 | |
| 94 | TEST_F(DisplayTest, disconnectDisconnectsDisplay) { |
| 95 | EXPECT_CALL(mCompositionEngine, getHwComposer()).WillRepeatedly(ReturnRef(mHwComposer)); |
| 96 | |
| 97 | // The first call to disconnect will disconnect the display with the HWC and |
| 98 | // set mHwcId to -1. |
| 99 | EXPECT_CALL(mHwComposer, disconnectDisplay(DEFAULT_DISPLAY_ID)).Times(1); |
| 100 | mDisplay.disconnect(); |
| 101 | EXPECT_FALSE(mDisplay.getId()); |
| 102 | |
| 103 | // Subsequent calls will do nothing, |
| 104 | EXPECT_CALL(mHwComposer, disconnectDisplay(DEFAULT_DISPLAY_ID)).Times(0); |
| 105 | mDisplay.disconnect(); |
| 106 | EXPECT_FALSE(mDisplay.getId()); |
| 107 | } |
| 108 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 109 | /* ------------------------------------------------------------------------ |
| 110 | * Display::setColorTransform() |
| 111 | */ |
| 112 | |
| 113 | TEST_F(DisplayTest, setColorTransformSetsTransform) { |
| 114 | // Identity matrix sets an identity state value |
| 115 | const mat4 identity; |
| 116 | |
| 117 | EXPECT_CALL(mCompositionEngine, getHwComposer()).WillRepeatedly(ReturnRef(mHwComposer)); |
| 118 | |
| 119 | EXPECT_CALL(mHwComposer, setColorTransform(DEFAULT_DISPLAY_ID, identity)).Times(1); |
| 120 | |
| 121 | mDisplay.setColorTransform(identity); |
| 122 | |
| 123 | EXPECT_EQ(HAL_COLOR_TRANSFORM_IDENTITY, mDisplay.getState().colorTransform); |
| 124 | |
| 125 | // Non-identity matrix sets a non-identity state value |
| 126 | const mat4 nonIdentity = mat4() * 2; |
| 127 | |
| 128 | EXPECT_CALL(mHwComposer, setColorTransform(DEFAULT_DISPLAY_ID, nonIdentity)).Times(1); |
| 129 | |
| 130 | mDisplay.setColorTransform(nonIdentity); |
| 131 | |
| 132 | EXPECT_EQ(HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX, mDisplay.getState().colorTransform); |
| 133 | } |
| 134 | |
| 135 | /* ------------------------------------------------------------------------ |
| 136 | * Display::setColorMode() |
| 137 | */ |
| 138 | |
| 139 | TEST_F(DisplayTest, setColorModeSetsModeUnlessNoChange) { |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 140 | mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>(); |
| 141 | mDisplay.setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface)); |
| 142 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 143 | EXPECT_CALL(mCompositionEngine, getHwComposer()).WillRepeatedly(ReturnRef(mHwComposer)); |
| 144 | |
| 145 | // These values are expected to be the initial state. |
| 146 | ASSERT_EQ(ui::ColorMode::NATIVE, mDisplay.getState().colorMode); |
| 147 | ASSERT_EQ(ui::Dataspace::UNKNOWN, mDisplay.getState().dataspace); |
| 148 | ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mDisplay.getState().renderIntent); |
| 149 | |
| 150 | // Otherwise if the values are unchanged, nothing happens |
| 151 | mDisplay.setColorMode(ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN, |
| 152 | ui::RenderIntent::COLORIMETRIC); |
| 153 | |
| 154 | EXPECT_EQ(ui::ColorMode::NATIVE, mDisplay.getState().colorMode); |
| 155 | EXPECT_EQ(ui::Dataspace::UNKNOWN, mDisplay.getState().dataspace); |
| 156 | EXPECT_EQ(ui::RenderIntent::COLORIMETRIC, mDisplay.getState().renderIntent); |
| 157 | |
| 158 | // Otherwise if the values are different, updates happen |
Lloyd Pique | ef95812 | 2019-02-05 18:00:12 -0800 | [diff] [blame^] | 159 | EXPECT_CALL(*renderSurface, setBufferDataspace(ui::Dataspace::DISPLAY_P3)).Times(1); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 160 | EXPECT_CALL(mHwComposer, |
Lloyd Pique | ef95812 | 2019-02-05 18:00:12 -0800 | [diff] [blame^] | 161 | setActiveColorMode(DEFAULT_DISPLAY_ID, ui::ColorMode::DISPLAY_P3, |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 162 | ui::RenderIntent::TONE_MAP_COLORIMETRIC)) |
| 163 | .Times(1); |
| 164 | |
Lloyd Pique | ef95812 | 2019-02-05 18:00:12 -0800 | [diff] [blame^] | 165 | mDisplay.setColorMode(ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3, |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 166 | ui::RenderIntent::TONE_MAP_COLORIMETRIC); |
| 167 | |
Lloyd Pique | ef95812 | 2019-02-05 18:00:12 -0800 | [diff] [blame^] | 168 | EXPECT_EQ(ui::ColorMode::DISPLAY_P3, mDisplay.getState().colorMode); |
| 169 | EXPECT_EQ(ui::Dataspace::DISPLAY_P3, mDisplay.getState().dataspace); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 170 | EXPECT_EQ(ui::RenderIntent::TONE_MAP_COLORIMETRIC, mDisplay.getState().renderIntent); |
| 171 | } |
| 172 | |
| 173 | TEST_F(DisplayTest, setColorModeDoesNothingForVirtualDisplay) { |
| 174 | impl::Display virtualDisplay{mCompositionEngine, |
| 175 | DisplayCreationArgs{false, true, DEFAULT_DISPLAY_ID}}; |
| 176 | |
Lloyd Pique | ef95812 | 2019-02-05 18:00:12 -0800 | [diff] [blame^] | 177 | virtualDisplay.setColorMode(ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3, |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 178 | ui::RenderIntent::TONE_MAP_COLORIMETRIC); |
| 179 | |
| 180 | EXPECT_EQ(ui::ColorMode::NATIVE, virtualDisplay.getState().colorMode); |
| 181 | EXPECT_EQ(ui::Dataspace::UNKNOWN, virtualDisplay.getState().dataspace); |
| 182 | EXPECT_EQ(ui::RenderIntent::COLORIMETRIC, virtualDisplay.getState().renderIntent); |
| 183 | } |
| 184 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 185 | /* ------------------------------------------------------------------------ |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 186 | * Display::createDisplayColorProfile() |
| 187 | */ |
| 188 | |
| 189 | TEST_F(DisplayTest, createDisplayColorProfileSetsDisplayColorProfile) { |
| 190 | EXPECT_TRUE(mDisplay.getDisplayColorProfile() == nullptr); |
| 191 | mDisplay.createDisplayColorProfile( |
| 192 | DisplayColorProfileCreationArgs{false, HdrCapabilities(), 0, |
| 193 | DisplayColorProfileCreationArgs::HwcColorModes()}); |
| 194 | EXPECT_TRUE(mDisplay.getDisplayColorProfile() != nullptr); |
| 195 | } |
| 196 | |
| 197 | /* ------------------------------------------------------------------------ |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 198 | * Display::createRenderSurface() |
| 199 | */ |
| 200 | |
| 201 | TEST_F(DisplayTest, createRenderSurfaceSetsRenderSurface) { |
| 202 | EXPECT_TRUE(mDisplay.getRenderSurface() == nullptr); |
| 203 | mDisplay.createRenderSurface(RenderSurfaceCreationArgs{640, 480, nullptr, nullptr}); |
| 204 | EXPECT_TRUE(mDisplay.getRenderSurface() != nullptr); |
| 205 | } |
| 206 | |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 207 | } // namespace |
| 208 | } // namespace android::compositionengine |