blob: 91b304cca8c6fe3d15b6a0bf6e5bdde737de4031 [file] [log] [blame]
Lloyd Pique4603f3c2020-02-11 12:06:56 -08001/*
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
36namespace android {
37namespace {
38
Peiyong Line9d809e2020-04-14 13:10:48 -070039namespace hal = android::hardware::graphics::composer::hal;
40
Lloyd Pique4603f3c2020-02-11 12:06:56 -080041using ::testing::_;
42using ::testing::DoAll;
43using ::testing::ElementsAreArray;
44using ::testing::Return;
45using ::testing::SetArgPointee;
46using ::testing::StrictMock;
47
48struct MockHWC2ComposerCallback : public HWC2::ComposerCallback {
49 ~MockHWC2ComposerCallback() = default;
50
51 MOCK_METHOD3(onHotplugReceived,
Peiyong Line9d809e2020-04-14 13:10:48 -070052 void(int32_t sequenceId, hal::HWDisplayId display, hal::Connection connection));
53 MOCK_METHOD2(onRefreshReceived, void(int32_t sequenceId, hal::HWDisplayId display));
Lloyd Pique4603f3c2020-02-11 12:06:56 -080054 MOCK_METHOD4(onVsyncReceived,
Peiyong Line9d809e2020-04-14 13:10:48 -070055 void(int32_t sequenceId, hal::HWDisplayId display, int64_t timestamp,
56 std::optional<hal::VsyncPeriodNanos> vsyncPeriod));
Lloyd Pique4603f3c2020-02-11 12:06:56 -080057 MOCK_METHOD3(onVsyncPeriodTimingChangedReceived,
Peiyong Line9d809e2020-04-14 13:10:48 -070058 void(int32_t sequenceId, hal::HWDisplayId display,
59 const hal::VsyncPeriodChangeTimeline& updatedTimeline));
60 MOCK_METHOD2(onSeamlessPossible, void(int32_t sequenceId, hal::HWDisplayId display));
Lloyd Pique4603f3c2020-02-11 12:06:56 -080061};
62
63struct HWComposerTest : public testing::Test {
64 Hwc2::mock::Composer* mHal = new StrictMock<Hwc2::mock::Composer>();
65};
66
67struct HWComposerSetConfigurationTest : public HWComposerTest {
68 StrictMock<MockHWC2ComposerCallback> mCallback;
69};
70
71TEST_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 Line9d809e2020-04-14 13:10:48 -070078 EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<hal::Capability>{}));
Lloyd Pique4603f3c2020-02-11 12:06:56 -080079 EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_))
Peiyong Line9d809e2020-04-14 13:10:48 -070080 .WillOnce(DoAll(SetArgPointee<0>(std::vector<hal::LayerGenericMetadataKey>{
81 {kMetadata1Name, kMetadata1Mandatory},
82 {kMetadata2Name, kMetadata2Mandatory},
83 }),
Lloyd Pique4603f3c2020-02-11 12:06:56 -080084 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
99TEST_F(HWComposerSetConfigurationTest, handlesUnsupportedCallToGetLayerGenericMetadataKeys) {
100 EXPECT_CALL(*mHal, getMaxVirtualDisplayCount()).WillOnce(Return(0));
Peiyong Line9d809e2020-04-14 13:10:48 -0700101 EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<hal::Capability>{}));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800102 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
114struct HWComposerLayerTest : public testing::Test {
Peiyong Line9d809e2020-04-14 13:10:48 -0700115 static constexpr hal::HWDisplayId kDisplayId = static_cast<hal::HWDisplayId>(1001);
116 static constexpr hal::HWLayerId kLayerId = static_cast<hal::HWLayerId>(1002);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800117
Peiyong Line9d809e2020-04-14 13:10:48 -0700118 HWComposerLayerTest(const std::unordered_set<hal::Capability>& capabilities)
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800119 : 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 Line9d809e2020-04-14 13:10:48 -0700124 const std::unordered_set<hal::Capability> mCapabilies;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800125 HWC2::impl::Layer mLayer{*mHal, mCapabilies, kDisplayId, kLayerId};
126};
127
128struct 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
139const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Name =
140 "com.example.metadata.1";
141
142const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Value = {1u,
143 2u,
144 3u};
145
146const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Name =
147 "com.example.metadata.2";
148
149const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Value = {45u,
150 67u};
151
152TEST_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 Line9d809e2020-04-14 13:10:48 -0700161 EXPECT_EQ(hal::Error::NONE, result);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800162
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 Line9d809e2020-04-14 13:10:48 -0700171 EXPECT_EQ(hal::Error::UNSUPPORTED, result);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800172}
173
174} // namespace
175} // namespace android