| 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 |  | 
| Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 39 | namespace hal = android::hardware::graphics::composer::hal; | 
|  | 40 |  | 
| Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 41 | using ::testing::_; | 
|  | 42 | using ::testing::DoAll; | 
|  | 43 | using ::testing::ElementsAreArray; | 
|  | 44 | using ::testing::Return; | 
|  | 45 | using ::testing::SetArgPointee; | 
|  | 46 | using ::testing::StrictMock; | 
|  | 47 |  | 
|  | 48 | struct MockHWC2ComposerCallback : public HWC2::ComposerCallback { | 
|  | 49 | ~MockHWC2ComposerCallback() = default; | 
|  | 50 |  | 
|  | 51 | MOCK_METHOD3(onHotplugReceived, | 
| Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 52 | void(int32_t sequenceId, hal::HWDisplayId display, hal::Connection connection)); | 
|  | 53 | MOCK_METHOD2(onRefreshReceived, void(int32_t sequenceId, hal::HWDisplayId display)); | 
| Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 54 | MOCK_METHOD4(onVsyncReceived, | 
| Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 55 | void(int32_t sequenceId, hal::HWDisplayId display, int64_t timestamp, | 
|  | 56 | std::optional<hal::VsyncPeriodNanos> vsyncPeriod)); | 
| Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 57 | MOCK_METHOD3(onVsyncPeriodTimingChangedReceived, | 
| Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 58 | void(int32_t sequenceId, hal::HWDisplayId display, | 
|  | 59 | const hal::VsyncPeriodChangeTimeline& updatedTimeline)); | 
|  | 60 | MOCK_METHOD2(onSeamlessPossible, void(int32_t sequenceId, hal::HWDisplayId display)); | 
| Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 61 | }; | 
|  | 62 |  | 
|  | 63 | struct HWComposerTest : public testing::Test { | 
|  | 64 | Hwc2::mock::Composer* mHal = new StrictMock<Hwc2::mock::Composer>(); | 
|  | 65 | }; | 
|  | 66 |  | 
|  | 67 | struct HWComposerSetConfigurationTest : public HWComposerTest { | 
|  | 68 | StrictMock<MockHWC2ComposerCallback> mCallback; | 
|  | 69 | }; | 
|  | 70 |  | 
|  | 71 | TEST_F(HWComposerSetConfigurationTest, loadsLayerMetadataSupport) { | 
|  | 72 | const std::string kMetadata1Name = "com.example.metadata.1"; | 
|  | 73 | constexpr bool kMetadata1Mandatory = false; | 
|  | 74 | const std::string kMetadata2Name = "com.example.metadata.2"; | 
|  | 75 | constexpr bool kMetadata2Mandatory = true; | 
|  | 76 |  | 
|  | 77 | EXPECT_CALL(*mHal, getMaxVirtualDisplayCount()).WillOnce(Return(0)); | 
| Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 78 | EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<hal::Capability>{})); | 
| Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 79 | EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_)) | 
| Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 80 | .WillOnce(DoAll(SetArgPointee<0>(std::vector<hal::LayerGenericMetadataKey>{ | 
|  | 81 | {kMetadata1Name, kMetadata1Mandatory}, | 
|  | 82 | {kMetadata2Name, kMetadata2Mandatory}, | 
|  | 83 | }), | 
| Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 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)); | 
| Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 101 | EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<hal::Capability>{})); | 
| Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 102 | EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_)) | 
|  | 103 | .WillOnce(Return(hardware::graphics::composer::V2_4::Error::UNSUPPORTED)); | 
|  | 104 | EXPECT_CALL(*mHal, registerCallback(_)); | 
|  | 105 | EXPECT_CALL(*mHal, isVsyncPeriodSwitchSupported()).WillOnce(Return(false)); | 
|  | 106 |  | 
|  | 107 | impl::HWComposer hwc{std::unique_ptr<Hwc2::Composer>(mHal)}; | 
|  | 108 | hwc.setConfiguration(&mCallback, 123); | 
|  | 109 |  | 
|  | 110 | const auto& supported = hwc.getSupportedLayerGenericMetadata(); | 
|  | 111 | EXPECT_EQ(0u, supported.size()); | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | struct HWComposerLayerTest : public testing::Test { | 
| Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 115 | static constexpr hal::HWDisplayId kDisplayId = static_cast<hal::HWDisplayId>(1001); | 
|  | 116 | static constexpr hal::HWLayerId kLayerId = static_cast<hal::HWLayerId>(1002); | 
| Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 117 |  | 
| Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 118 | HWComposerLayerTest(const std::unordered_set<hal::Capability>& capabilities) | 
| Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 119 | : mCapabilies(capabilities) {} | 
|  | 120 |  | 
|  | 121 | ~HWComposerLayerTest() override { EXPECT_CALL(*mHal, destroyLayer(kDisplayId, kLayerId)); } | 
|  | 122 |  | 
|  | 123 | std::unique_ptr<Hwc2::mock::Composer> mHal{new StrictMock<Hwc2::mock::Composer>()}; | 
| Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 124 | const std::unordered_set<hal::Capability> mCapabilies; | 
| Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 125 | HWC2::impl::Layer mLayer{*mHal, mCapabilies, kDisplayId, kLayerId}; | 
|  | 126 | }; | 
|  | 127 |  | 
|  | 128 | struct HWComposerLayerGenericMetadataTest : public HWComposerLayerTest { | 
|  | 129 | static const std::string kLayerGenericMetadata1Name; | 
|  | 130 | static constexpr bool kLayerGenericMetadata1Mandatory = false; | 
|  | 131 | static const std::vector<uint8_t> kLayerGenericMetadata1Value; | 
|  | 132 | static const std::string kLayerGenericMetadata2Name; | 
|  | 133 | static constexpr bool kLayerGenericMetadata2Mandatory = true; | 
|  | 134 | static const std::vector<uint8_t> kLayerGenericMetadata2Value; | 
|  | 135 |  | 
|  | 136 | HWComposerLayerGenericMetadataTest() : HWComposerLayerTest({}) {} | 
|  | 137 | }; | 
|  | 138 |  | 
|  | 139 | const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Name = | 
|  | 140 | "com.example.metadata.1"; | 
|  | 141 |  | 
|  | 142 | const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Value = {1u, | 
|  | 143 | 2u, | 
|  | 144 | 3u}; | 
|  | 145 |  | 
|  | 146 | const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Name = | 
|  | 147 | "com.example.metadata.2"; | 
|  | 148 |  | 
|  | 149 | const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Value = {45u, | 
|  | 150 | 67u}; | 
|  | 151 |  | 
|  | 152 | TEST_F(HWComposerLayerGenericMetadataTest, forwardsSupportedMetadata) { | 
|  | 153 | EXPECT_CALL(*mHal, | 
|  | 154 | setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata1Name, | 
|  | 155 | kLayerGenericMetadata1Mandatory, | 
|  | 156 | kLayerGenericMetadata1Value)) | 
|  | 157 | .WillOnce(Return(hardware::graphics::composer::V2_4::Error::NONE)); | 
|  | 158 | auto result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata1Name, | 
|  | 159 | kLayerGenericMetadata1Mandatory, | 
|  | 160 | kLayerGenericMetadata1Value); | 
| Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 161 | EXPECT_EQ(hal::Error::NONE, result); | 
| Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 162 |  | 
|  | 163 | EXPECT_CALL(*mHal, | 
|  | 164 | setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata2Name, | 
|  | 165 | kLayerGenericMetadata2Mandatory, | 
|  | 166 | kLayerGenericMetadata2Value)) | 
|  | 167 | .WillOnce(Return(hardware::graphics::composer::V2_4::Error::UNSUPPORTED)); | 
|  | 168 | result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata2Name, | 
|  | 169 | kLayerGenericMetadata2Mandatory, | 
|  | 170 | kLayerGenericMetadata2Value); | 
| Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 171 | EXPECT_EQ(hal::Error::UNSUPPORTED, result); | 
| Lloyd Pique | 4603f3c | 2020-02-11 12:06:56 -0800 | [diff] [blame] | 172 | } | 
|  | 173 |  | 
|  | 174 | } // namespace | 
|  | 175 | } // namespace android |