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