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 | |
| 26 | #include <gmock/gmock.h> |
| 27 | #include <gui/LayerMetadata.h> |
| 28 | #include <log/log.h> |
| 29 | |
| 30 | #include "DisplayHardware/HWComposer.h" |
| 31 | #include "mock/DisplayHardware/MockComposer.h" |
| 32 | |
| 33 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 34 | #pragma clang diagnostic pop // ignored "-Wconversion" |
| 35 | |
| 36 | namespace android { |
| 37 | namespace { |
| 38 | |
| 39 | using ::testing::_; |
| 40 | using ::testing::DoAll; |
| 41 | using ::testing::ElementsAreArray; |
| 42 | using ::testing::Return; |
| 43 | using ::testing::SetArgPointee; |
| 44 | using ::testing::StrictMock; |
| 45 | |
| 46 | struct MockHWC2ComposerCallback : public HWC2::ComposerCallback { |
| 47 | ~MockHWC2ComposerCallback() = default; |
| 48 | |
| 49 | MOCK_METHOD3(onHotplugReceived, |
| 50 | void(int32_t sequenceId, hwc2_display_t display, HWC2::Connection connection)); |
| 51 | MOCK_METHOD2(onRefreshReceived, void(int32_t sequenceId, hwc2_display_t display)); |
| 52 | MOCK_METHOD4(onVsyncReceived, |
| 53 | void(int32_t sequenceId, hwc2_display_t display, int64_t timestamp, |
| 54 | std::optional<hwc2_vsync_period_t> vsyncPeriod)); |
| 55 | MOCK_METHOD3(onVsyncPeriodTimingChangedReceived, |
| 56 | void(int32_t sequenceId, hwc2_display_t display, |
| 57 | const hwc_vsync_period_change_timeline_t& updatedTimeline)); |
| 58 | MOCK_METHOD2(onSeamlessPossible, void(int32_t sequenceId, hwc2_display_t display)); |
| 59 | }; |
| 60 | |
| 61 | struct HWComposerTest : public testing::Test { |
| 62 | Hwc2::mock::Composer* mHal = new StrictMock<Hwc2::mock::Composer>(); |
| 63 | }; |
| 64 | |
| 65 | struct HWComposerSetConfigurationTest : public HWComposerTest { |
| 66 | StrictMock<MockHWC2ComposerCallback> mCallback; |
| 67 | }; |
| 68 | |
| 69 | TEST_F(HWComposerSetConfigurationTest, loadsLayerMetadataSupport) { |
| 70 | const std::string kMetadata1Name = "com.example.metadata.1"; |
| 71 | constexpr bool kMetadata1Mandatory = false; |
| 72 | const std::string kMetadata2Name = "com.example.metadata.2"; |
| 73 | constexpr bool kMetadata2Mandatory = true; |
| 74 | |
| 75 | EXPECT_CALL(*mHal, getMaxVirtualDisplayCount()).WillOnce(Return(0)); |
| 76 | EXPECT_CALL(*mHal, getCapabilities()) |
| 77 | .WillOnce(Return(std::vector<Hwc2::IComposer::Capability>{})); |
| 78 | EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_)) |
| 79 | .WillOnce(DoAll(SetArgPointee<0>( |
| 80 | std::vector<Hwc2::IComposerClient::LayerGenericMetadataKey>{ |
| 81 | {kMetadata1Name, kMetadata1Mandatory}, |
| 82 | {kMetadata2Name, kMetadata2Mandatory}, |
| 83 | }), |
| 84 | Return(hardware::graphics::composer::V2_4::Error::NONE))); |
| 85 | EXPECT_CALL(*mHal, registerCallback(_)); |
| 86 | EXPECT_CALL(*mHal, isVsyncPeriodSwitchSupported()).WillOnce(Return(false)); |
| 87 | |
| 88 | impl::HWComposer hwc{std::unique_ptr<Hwc2::Composer>(mHal)}; |
| 89 | hwc.setConfiguration(&mCallback, 123); |
| 90 | |
| 91 | const auto& supported = hwc.getSupportedLayerGenericMetadata(); |
| 92 | EXPECT_EQ(2u, supported.size()); |
| 93 | EXPECT_EQ(1u, supported.count(kMetadata1Name)); |
| 94 | EXPECT_EQ(kMetadata1Mandatory, supported.find(kMetadata1Name)->second); |
| 95 | EXPECT_EQ(1u, supported.count(kMetadata2Name)); |
| 96 | EXPECT_EQ(kMetadata2Mandatory, supported.find(kMetadata2Name)->second); |
| 97 | } |
| 98 | |
| 99 | TEST_F(HWComposerSetConfigurationTest, handlesUnsupportedCallToGetLayerGenericMetadataKeys) { |
| 100 | EXPECT_CALL(*mHal, getMaxVirtualDisplayCount()).WillOnce(Return(0)); |
| 101 | EXPECT_CALL(*mHal, getCapabilities()) |
| 102 | .WillOnce(Return(std::vector<Hwc2::IComposer::Capability>{})); |
| 103 | EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_)) |
| 104 | .WillOnce(Return(hardware::graphics::composer::V2_4::Error::UNSUPPORTED)); |
| 105 | EXPECT_CALL(*mHal, registerCallback(_)); |
| 106 | EXPECT_CALL(*mHal, isVsyncPeriodSwitchSupported()).WillOnce(Return(false)); |
| 107 | |
| 108 | impl::HWComposer hwc{std::unique_ptr<Hwc2::Composer>(mHal)}; |
| 109 | hwc.setConfiguration(&mCallback, 123); |
| 110 | |
| 111 | const auto& supported = hwc.getSupportedLayerGenericMetadata(); |
| 112 | EXPECT_EQ(0u, supported.size()); |
| 113 | } |
| 114 | |
| 115 | struct HWComposerLayerTest : public testing::Test { |
| 116 | static constexpr hwc2_display_t kDisplayId = static_cast<hwc2_display_t>(1001); |
| 117 | static constexpr hwc2_layer_t kLayerId = static_cast<hwc2_layer_t>(1002); |
| 118 | |
| 119 | HWComposerLayerTest(const std::unordered_set<HWC2::Capability>& capabilities) |
| 120 | : mCapabilies(capabilities) {} |
| 121 | |
| 122 | ~HWComposerLayerTest() override { EXPECT_CALL(*mHal, destroyLayer(kDisplayId, kLayerId)); } |
| 123 | |
| 124 | std::unique_ptr<Hwc2::mock::Composer> mHal{new StrictMock<Hwc2::mock::Composer>()}; |
| 125 | const std::unordered_set<HWC2::Capability> mCapabilies; |
| 126 | HWC2::impl::Layer mLayer{*mHal, mCapabilies, kDisplayId, kLayerId}; |
| 127 | }; |
| 128 | |
| 129 | struct HWComposerLayerGenericMetadataTest : public HWComposerLayerTest { |
| 130 | static const std::string kLayerGenericMetadata1Name; |
| 131 | static constexpr bool kLayerGenericMetadata1Mandatory = false; |
| 132 | static const std::vector<uint8_t> kLayerGenericMetadata1Value; |
| 133 | static const std::string kLayerGenericMetadata2Name; |
| 134 | static constexpr bool kLayerGenericMetadata2Mandatory = true; |
| 135 | static const std::vector<uint8_t> kLayerGenericMetadata2Value; |
| 136 | |
| 137 | HWComposerLayerGenericMetadataTest() : HWComposerLayerTest({}) {} |
| 138 | }; |
| 139 | |
| 140 | const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Name = |
| 141 | "com.example.metadata.1"; |
| 142 | |
| 143 | const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Value = {1u, |
| 144 | 2u, |
| 145 | 3u}; |
| 146 | |
| 147 | const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Name = |
| 148 | "com.example.metadata.2"; |
| 149 | |
| 150 | const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Value = {45u, |
| 151 | 67u}; |
| 152 | |
| 153 | TEST_F(HWComposerLayerGenericMetadataTest, forwardsSupportedMetadata) { |
| 154 | EXPECT_CALL(*mHal, |
| 155 | setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata1Name, |
| 156 | kLayerGenericMetadata1Mandatory, |
| 157 | kLayerGenericMetadata1Value)) |
| 158 | .WillOnce(Return(hardware::graphics::composer::V2_4::Error::NONE)); |
| 159 | auto result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata1Name, |
| 160 | kLayerGenericMetadata1Mandatory, |
| 161 | kLayerGenericMetadata1Value); |
| 162 | EXPECT_EQ(HWC2::Error::None, result); |
| 163 | |
| 164 | EXPECT_CALL(*mHal, |
| 165 | setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata2Name, |
| 166 | kLayerGenericMetadata2Mandatory, |
| 167 | kLayerGenericMetadata2Value)) |
| 168 | .WillOnce(Return(hardware::graphics::composer::V2_4::Error::UNSUPPORTED)); |
| 169 | result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata2Name, |
| 170 | kLayerGenericMetadata2Mandatory, |
| 171 | kLayerGenericMetadata2Value); |
| 172 | EXPECT_EQ(HWC2::Error::Unsupported, result); |
| 173 | } |
| 174 | |
| 175 | } // namespace |
| 176 | } // namespace android |