Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
| 20 | |
| 21 | #undef LOG_TAG |
| 22 | #define LOG_TAG "LibSurfaceFlingerUnittests" |
| 23 | |
| 24 | #include <vector> |
| 25 | |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 26 | // StrictMock<T> derives from T and is not marked final, so the destructor of T is expected to be |
| 27 | // virtual in case StrictMock<T> is used as a polymorphic base class. That is not the case here. |
| 28 | #pragma clang diagnostic push |
| 29 | #pragma clang diagnostic ignored "-Wnon-virtual-dtor" |
Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 30 | #include <gmock/gmock.h> |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 31 | #pragma clang diagnostic pop |
| 32 | |
Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 33 | #include <gui/LayerMetadata.h> |
| 34 | #include <log/log.h> |
| 35 | |
Marin Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 36 | #include "DisplayHardware/DisplayMode.h" |
Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 37 | #include "DisplayHardware/HWComposer.h" |
Marin Shalamanov | 12c9e5a | 2021-01-07 00:25:35 +0100 | [diff] [blame^] | 38 | #include "DisplayHardware/Hal.h" |
Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 39 | #include "mock/DisplayHardware/MockComposer.h" |
| 40 | |
| 41 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 42 | #pragma clang diagnostic pop // ignored "-Wconversion" |
| 43 | |
| 44 | namespace android { |
| 45 | namespace { |
| 46 | |
Marin Shalamanov | 6e84017 | 2020-12-14 22:13:28 +0100 | [diff] [blame] | 47 | namespace V2_1 = hardware::graphics::composer::V2_1; |
| 48 | namespace V2_4 = hardware::graphics::composer::V2_4; |
| 49 | |
| 50 | using Hwc2::Config; |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 51 | |
Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 52 | using ::testing::_; |
| 53 | using ::testing::DoAll; |
| 54 | using ::testing::ElementsAreArray; |
| 55 | using ::testing::Return; |
| 56 | using ::testing::SetArgPointee; |
| 57 | using ::testing::StrictMock; |
| 58 | |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 59 | struct MockHWC2ComposerCallback final : StrictMock<HWC2::ComposerCallback> { |
| 60 | MOCK_METHOD3(onHotplugReceived, void(int32_t sequenceId, hal::HWDisplayId, hal::Connection)); |
| 61 | MOCK_METHOD2(onRefreshReceived, void(int32_t sequenceId, hal::HWDisplayId)); |
Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 62 | MOCK_METHOD4(onVsyncReceived, |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 63 | void(int32_t sequenceId, hal::HWDisplayId, int64_t timestamp, |
| 64 | std::optional<hal::VsyncPeriodNanos>)); |
Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 65 | MOCK_METHOD3(onVsyncPeriodTimingChangedReceived, |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 66 | void(int32_t sequenceId, hal::HWDisplayId, const hal::VsyncPeriodChangeTimeline&)); |
| 67 | MOCK_METHOD2(onSeamlessPossible, void(int32_t sequenceId, hal::HWDisplayId)); |
Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 68 | }; |
| 69 | |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 70 | struct HWComposerSetConfigurationTest : testing::Test { |
Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 71 | Hwc2::mock::Composer* mHal = new StrictMock<Hwc2::mock::Composer>(); |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 72 | MockHWC2ComposerCallback mCallback; |
Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | TEST_F(HWComposerSetConfigurationTest, loadsLayerMetadataSupport) { |
| 76 | const std::string kMetadata1Name = "com.example.metadata.1"; |
| 77 | constexpr bool kMetadata1Mandatory = false; |
| 78 | const std::string kMetadata2Name = "com.example.metadata.2"; |
| 79 | constexpr bool kMetadata2Mandatory = true; |
| 80 | |
| 81 | EXPECT_CALL(*mHal, getMaxVirtualDisplayCount()).WillOnce(Return(0)); |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 82 | EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<hal::Capability>{})); |
Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 83 | EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_)) |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 84 | .WillOnce(DoAll(SetArgPointee<0>(std::vector<hal::LayerGenericMetadataKey>{ |
| 85 | {kMetadata1Name, kMetadata1Mandatory}, |
| 86 | {kMetadata2Name, kMetadata2Mandatory}, |
| 87 | }), |
Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 88 | Return(hardware::graphics::composer::V2_4::Error::NONE))); |
| 89 | EXPECT_CALL(*mHal, registerCallback(_)); |
| 90 | EXPECT_CALL(*mHal, isVsyncPeriodSwitchSupported()).WillOnce(Return(false)); |
| 91 | |
| 92 | impl::HWComposer hwc{std::unique_ptr<Hwc2::Composer>(mHal)}; |
| 93 | hwc.setConfiguration(&mCallback, 123); |
| 94 | |
| 95 | const auto& supported = hwc.getSupportedLayerGenericMetadata(); |
| 96 | EXPECT_EQ(2u, supported.size()); |
| 97 | EXPECT_EQ(1u, supported.count(kMetadata1Name)); |
| 98 | EXPECT_EQ(kMetadata1Mandatory, supported.find(kMetadata1Name)->second); |
| 99 | EXPECT_EQ(1u, supported.count(kMetadata2Name)); |
| 100 | EXPECT_EQ(kMetadata2Mandatory, supported.find(kMetadata2Name)->second); |
| 101 | } |
| 102 | |
| 103 | TEST_F(HWComposerSetConfigurationTest, handlesUnsupportedCallToGetLayerGenericMetadataKeys) { |
| 104 | EXPECT_CALL(*mHal, getMaxVirtualDisplayCount()).WillOnce(Return(0)); |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 105 | EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<hal::Capability>{})); |
Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 106 | EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_)) |
| 107 | .WillOnce(Return(hardware::graphics::composer::V2_4::Error::UNSUPPORTED)); |
| 108 | EXPECT_CALL(*mHal, registerCallback(_)); |
| 109 | EXPECT_CALL(*mHal, isVsyncPeriodSwitchSupported()).WillOnce(Return(false)); |
| 110 | |
| 111 | impl::HWComposer hwc{std::unique_ptr<Hwc2::Composer>(mHal)}; |
| 112 | hwc.setConfiguration(&mCallback, 123); |
| 113 | |
| 114 | const auto& supported = hwc.getSupportedLayerGenericMetadata(); |
| 115 | EXPECT_EQ(0u, supported.size()); |
| 116 | } |
| 117 | |
| 118 | struct HWComposerLayerTest : public testing::Test { |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 119 | static constexpr hal::HWDisplayId kDisplayId = static_cast<hal::HWDisplayId>(1001); |
| 120 | static constexpr hal::HWLayerId kLayerId = static_cast<hal::HWLayerId>(1002); |
Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 121 | |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 122 | HWComposerLayerTest(const std::unordered_set<hal::Capability>& capabilities) |
Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 123 | : mCapabilies(capabilities) {} |
| 124 | |
| 125 | ~HWComposerLayerTest() override { EXPECT_CALL(*mHal, destroyLayer(kDisplayId, kLayerId)); } |
| 126 | |
| 127 | std::unique_ptr<Hwc2::mock::Composer> mHal{new StrictMock<Hwc2::mock::Composer>()}; |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 128 | const std::unordered_set<hal::Capability> mCapabilies; |
Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 129 | HWC2::impl::Layer mLayer{*mHal, mCapabilies, kDisplayId, kLayerId}; |
| 130 | }; |
| 131 | |
| 132 | struct HWComposerLayerGenericMetadataTest : public HWComposerLayerTest { |
| 133 | static const std::string kLayerGenericMetadata1Name; |
| 134 | static constexpr bool kLayerGenericMetadata1Mandatory = false; |
| 135 | static const std::vector<uint8_t> kLayerGenericMetadata1Value; |
| 136 | static const std::string kLayerGenericMetadata2Name; |
| 137 | static constexpr bool kLayerGenericMetadata2Mandatory = true; |
| 138 | static const std::vector<uint8_t> kLayerGenericMetadata2Value; |
| 139 | |
| 140 | HWComposerLayerGenericMetadataTest() : HWComposerLayerTest({}) {} |
| 141 | }; |
| 142 | |
| 143 | const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Name = |
| 144 | "com.example.metadata.1"; |
| 145 | |
| 146 | const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Value = {1u, |
| 147 | 2u, |
| 148 | 3u}; |
| 149 | |
| 150 | const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Name = |
| 151 | "com.example.metadata.2"; |
| 152 | |
| 153 | const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Value = {45u, |
| 154 | 67u}; |
| 155 | |
| 156 | TEST_F(HWComposerLayerGenericMetadataTest, forwardsSupportedMetadata) { |
| 157 | EXPECT_CALL(*mHal, |
| 158 | setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata1Name, |
| 159 | kLayerGenericMetadata1Mandatory, |
| 160 | kLayerGenericMetadata1Value)) |
| 161 | .WillOnce(Return(hardware::graphics::composer::V2_4::Error::NONE)); |
| 162 | auto result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata1Name, |
| 163 | kLayerGenericMetadata1Mandatory, |
| 164 | kLayerGenericMetadata1Value); |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 165 | EXPECT_EQ(hal::Error::NONE, result); |
Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 166 | |
| 167 | EXPECT_CALL(*mHal, |
| 168 | setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata2Name, |
| 169 | kLayerGenericMetadata2Mandatory, |
| 170 | kLayerGenericMetadata2Value)) |
| 171 | .WillOnce(Return(hardware::graphics::composer::V2_4::Error::UNSUPPORTED)); |
| 172 | result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata2Name, |
| 173 | kLayerGenericMetadata2Mandatory, |
| 174 | kLayerGenericMetadata2Value); |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 175 | EXPECT_EQ(hal::Error::UNSUPPORTED, result); |
Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Marin Shalamanov | 6e84017 | 2020-12-14 22:13:28 +0100 | [diff] [blame] | 178 | class HWComposerConfigsTest : public testing::Test { |
| 179 | public: |
| 180 | Hwc2::mock::Composer* mHal = new StrictMock<Hwc2::mock::Composer>(); |
| 181 | MockHWC2ComposerCallback mCallback; |
| 182 | |
| 183 | void setActiveConfig(Config config) { |
| 184 | EXPECT_CALL(*mHal, getActiveConfig(_, _)) |
| 185 | .WillRepeatedly(DoAll(SetArgPointee<1>(config), Return(V2_1::Error::NONE))); |
| 186 | } |
| 187 | |
| 188 | void setDisplayConfigs(std::vector<Config> configs) { |
| 189 | EXPECT_CALL(*mHal, getDisplayConfigs(_, _)) |
| 190 | .WillOnce(DoAll(SetArgPointee<1>(configs), Return(V2_1::Error::NONE))); |
| 191 | EXPECT_CALL(*mHal, getDisplayAttribute(_, _, _, _)) |
| 192 | .WillRepeatedly(DoAll(SetArgPointee<3>(1), Return(V2_1::Error::NONE))); |
| 193 | } |
| 194 | |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 195 | void testSetActiveModeWithConstraintsCommon(bool isVsyncPeriodSwitchSupported); |
Marin Shalamanov | 6e84017 | 2020-12-14 22:13:28 +0100 | [diff] [blame] | 196 | }; |
| 197 | |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 198 | void HWComposerConfigsTest::testSetActiveModeWithConstraintsCommon( |
Marin Shalamanov | 6e84017 | 2020-12-14 22:13:28 +0100 | [diff] [blame] | 199 | bool isVsyncPeriodSwitchSupported) { |
| 200 | EXPECT_CALL(*mHal, getMaxVirtualDisplayCount()).WillOnce(Return(0)); |
| 201 | EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<hal::Capability>{})); |
| 202 | EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_)).WillOnce(Return(V2_4::Error::UNSUPPORTED)); |
| 203 | EXPECT_CALL(*mHal, registerCallback(_)); |
| 204 | EXPECT_CALL(*mHal, setVsyncEnabled(_, _)).WillRepeatedly(Return(V2_1::Error::NONE)); |
| 205 | EXPECT_CALL(*mHal, getDisplayIdentificationData(_, _, _)) |
| 206 | .WillRepeatedly(Return(V2_1::Error::UNSUPPORTED)); |
| 207 | EXPECT_CALL(*mHal, setClientTargetSlotCount(_)).WillRepeatedly(Return(V2_1::Error::NONE)); |
| 208 | |
| 209 | EXPECT_CALL(*mHal, isVsyncPeriodSwitchSupported()) |
| 210 | .WillRepeatedly(Return(isVsyncPeriodSwitchSupported)); |
| 211 | |
| 212 | if (isVsyncPeriodSwitchSupported) { |
| 213 | EXPECT_CALL(*mHal, setActiveConfigWithConstraints(_, _, _, _)) |
| 214 | .WillRepeatedly(Return(V2_4::Error::NONE)); |
| 215 | } else { |
| 216 | EXPECT_CALL(*mHal, setActiveConfig(_, _)).WillRepeatedly(Return(V2_1::Error::NONE)); |
| 217 | } |
| 218 | |
| 219 | impl::HWComposer hwc{std::unique_ptr<Hwc2::Composer>(mHal)}; |
| 220 | hwc.setConfiguration(&mCallback, 123); |
| 221 | |
| 222 | setDisplayConfigs({15}); |
| 223 | setActiveConfig(15); |
| 224 | |
| 225 | const auto physicalId = PhysicalDisplayId::fromPort(0); |
| 226 | const hal::HWDisplayId hwcId = 0; |
| 227 | hwc.allocatePhysicalDisplay(hwcId, physicalId); |
| 228 | |
| 229 | hal::VsyncPeriodChangeConstraints constraints; |
| 230 | constraints.desiredTimeNanos = systemTime(); |
| 231 | constraints.seamlessRequired = false; |
| 232 | |
| 233 | hal::VsyncPeriodChangeTimeline timeline = {0, 0, 0}; |
Marin Shalamanov | 12c9e5a | 2021-01-07 00:25:35 +0100 | [diff] [blame^] | 234 | constexpr Config kConfigIndex = 0; |
Marin Shalamanov | 6e84017 | 2020-12-14 22:13:28 +0100 | [diff] [blame] | 235 | const auto status = |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 236 | hwc.setActiveModeWithConstraints(physicalId, kConfigIndex, constraints, &timeline); |
Marin Shalamanov | 6e84017 | 2020-12-14 22:13:28 +0100 | [diff] [blame] | 237 | EXPECT_EQ(NO_ERROR, status); |
| 238 | |
| 239 | const std::vector<Config> kConfigs{7, 8, 9, 10, 11}; |
| 240 | // Change the set of supported modes. |
| 241 | setDisplayConfigs(kConfigs); |
| 242 | setActiveConfig(11); |
| 243 | hwc.onHotplug(hwcId, hal::Connection::CONNECTED); |
| 244 | hwc.allocatePhysicalDisplay(hwcId, physicalId); |
| 245 | |
| 246 | for (size_t configIndex = 0; configIndex < kConfigs.size(); configIndex++) { |
Marin Shalamanov | 12c9e5a | 2021-01-07 00:25:35 +0100 | [diff] [blame^] | 247 | const auto status = |
| 248 | hwc.setActiveModeWithConstraints(physicalId, |
| 249 | static_cast<hal::HWConfigId>(configIndex), |
| 250 | constraints, &timeline); |
Marin Shalamanov | 6e84017 | 2020-12-14 22:13:28 +0100 | [diff] [blame] | 251 | EXPECT_EQ(NO_ERROR, status) << "Error when switching to config " << configIndex; |
| 252 | } |
| 253 | } |
| 254 | |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 255 | TEST_F(HWComposerConfigsTest, setActiveModeWithConstraintsWithVsyncSwitchingSupported) { |
| 256 | testSetActiveModeWithConstraintsCommon(/*supported=*/true); |
Marin Shalamanov | 6e84017 | 2020-12-14 22:13:28 +0100 | [diff] [blame] | 257 | } |
| 258 | |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 259 | TEST_F(HWComposerConfigsTest, setActiveModeWithConstraintsWithVsyncSwitchingNotSupported) { |
| 260 | testSetActiveModeWithConstraintsCommon(/*supported=*/false); |
Marin Shalamanov | 6e84017 | 2020-12-14 22:13:28 +0100 | [diff] [blame] | 261 | } |
| 262 | |
Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 263 | } // namespace |
Marin Shalamanov | 6e84017 | 2020-12-14 22:13:28 +0100 | [diff] [blame] | 264 | } // namespace android |