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