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 // 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"
30 #include <gmock/gmock.h>
31 #pragma clang diagnostic pop
32
33 #include <gui/LayerMetadata.h>
34 #include <log/log.h>
35
36 #include "DisplayHardware/DisplayMode.h"
37 #include "DisplayHardware/HWComposer.h"
38 #include "DisplayHardware/Hal.h"
39 #include "mock/DisplayHardware/MockComposer.h"
40 #include "mock/DisplayHardware/MockHWC2.h"
41
42 // TODO(b/129481165): remove the #pragma below and fix conversion issues
43 #pragma clang diagnostic pop // ignored "-Wconversion"
44
45 namespace android {
46 namespace {
47
48 namespace V2_1 = hardware::graphics::composer::V2_1;
49 namespace V2_4 = hardware::graphics::composer::V2_4;
50
51 using Hwc2::Config;
52
53 using ::testing::_;
54 using ::testing::DoAll;
55 using ::testing::ElementsAreArray;
56 using ::testing::Return;
57 using ::testing::SetArgPointee;
58 using ::testing::StrictMock;
59
60 struct MockHWC2ComposerCallback final : StrictMock<HWC2::ComposerCallback> {
61 MOCK_METHOD2(onComposerHalHotplug, void(hal::HWDisplayId, hal::Connection));
62 MOCK_METHOD1(onComposerHalRefresh, void(hal::HWDisplayId));
63 MOCK_METHOD3(onComposerHalVsync,
64 void(hal::HWDisplayId, int64_t timestamp, std::optional<hal::VsyncPeriodNanos>));
65 MOCK_METHOD2(onComposerHalVsyncPeriodTimingChanged,
66 void(hal::HWDisplayId, const hal::VsyncPeriodChangeTimeline&));
67 MOCK_METHOD1(onComposerHalSeamlessPossible, void(hal::HWDisplayId));
68 };
69
70 struct HWComposerSetCallbackTest : testing::Test {
71 Hwc2::mock::Composer* mHal = new StrictMock<Hwc2::mock::Composer>();
72 MockHWC2ComposerCallback mCallback;
73 };
74
TEST_F(HWComposerSetCallbackTest,loadsLayerMetadataSupport)75 TEST_F(HWComposerSetCallbackTest, 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, getCapabilities()).WillOnce(Return(std::vector<hal::Capability>{}));
82 EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_))
83 .WillOnce(DoAll(SetArgPointee<0>(std::vector<hal::LayerGenericMetadataKey>{
84 {kMetadata1Name, kMetadata1Mandatory},
85 {kMetadata2Name, kMetadata2Mandatory},
86 }),
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.setCallback(&mCallback);
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
TEST_F(HWComposerSetCallbackTest,handlesUnsupportedCallToGetLayerGenericMetadataKeys)102 TEST_F(HWComposerSetCallbackTest, handlesUnsupportedCallToGetLayerGenericMetadataKeys) {
103 EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<hal::Capability>{}));
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.setCallback(&mCallback);
111
112 const auto& supported = hwc.getSupportedLayerGenericMetadata();
113 EXPECT_EQ(0u, supported.size());
114 }
115
116 struct HWComposerLayerTest : public testing::Test {
117 static constexpr hal::HWDisplayId kDisplayId = static_cast<hal::HWDisplayId>(1001);
118 static constexpr hal::HWLayerId kLayerId = static_cast<hal::HWLayerId>(1002);
119
HWComposerLayerTestandroid::__anon4095a77a0111::HWComposerLayerTest120 HWComposerLayerTest(const std::unordered_set<hal::Capability>& capabilities)
121 : mCapabilies(capabilities) {
122 EXPECT_CALL(mDisplay, getId()).WillRepeatedly(Return(kDisplayId));
123 }
124
~HWComposerLayerTestandroid::__anon4095a77a0111::HWComposerLayerTest125 ~HWComposerLayerTest() override {
126 EXPECT_CALL(mDisplay, onLayerDestroyed(kLayerId));
127 EXPECT_CALL(*mHal, destroyLayer(kDisplayId, kLayerId));
128 }
129
130 std::unique_ptr<Hwc2::mock::Composer> mHal{new StrictMock<Hwc2::mock::Composer>()};
131 const std::unordered_set<hal::Capability> mCapabilies;
132 StrictMock<HWC2::mock::Display> mDisplay;
133 HWC2::impl::Layer mLayer{*mHal, mCapabilies, mDisplay, kLayerId};
134 };
135
136 struct HWComposerLayerGenericMetadataTest : public HWComposerLayerTest {
137 static const std::string kLayerGenericMetadata1Name;
138 static constexpr bool kLayerGenericMetadata1Mandatory = false;
139 static const std::vector<uint8_t> kLayerGenericMetadata1Value;
140 static const std::string kLayerGenericMetadata2Name;
141 static constexpr bool kLayerGenericMetadata2Mandatory = true;
142 static const std::vector<uint8_t> kLayerGenericMetadata2Value;
143
HWComposerLayerGenericMetadataTestandroid::__anon4095a77a0111::HWComposerLayerGenericMetadataTest144 HWComposerLayerGenericMetadataTest() : HWComposerLayerTest({}) {}
145 };
146
147 const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Name =
148 "com.example.metadata.1";
149
150 const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Value = {1u,
151 2u,
152 3u};
153
154 const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Name =
155 "com.example.metadata.2";
156
157 const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Value = {45u,
158 67u};
159
TEST_F(HWComposerLayerGenericMetadataTest,forwardsSupportedMetadata)160 TEST_F(HWComposerLayerGenericMetadataTest, forwardsSupportedMetadata) {
161 EXPECT_CALL(*mHal,
162 setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata1Name,
163 kLayerGenericMetadata1Mandatory,
164 kLayerGenericMetadata1Value))
165 .WillOnce(Return(hardware::graphics::composer::V2_4::Error::NONE));
166 auto result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata1Name,
167 kLayerGenericMetadata1Mandatory,
168 kLayerGenericMetadata1Value);
169 EXPECT_EQ(hal::Error::NONE, result);
170
171 EXPECT_CALL(*mHal,
172 setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata2Name,
173 kLayerGenericMetadata2Mandatory,
174 kLayerGenericMetadata2Value))
175 .WillOnce(Return(hardware::graphics::composer::V2_4::Error::UNSUPPORTED));
176 result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata2Name,
177 kLayerGenericMetadata2Mandatory,
178 kLayerGenericMetadata2Value);
179 EXPECT_EQ(hal::Error::UNSUPPORTED, result);
180 }
181
182 } // namespace
183 } // namespace android
184