blob: 2af0480cd084a1f304de9f6249ef9f2adfb24666 [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
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070026// 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 Pique4603f3c2020-02-11 12:06:56 -080030#include <gmock/gmock.h>
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070031#pragma clang diagnostic pop
32
Lloyd Pique4603f3c2020-02-11 12:06:56 -080033#include <gui/LayerMetadata.h>
34#include <log/log.h>
35
Marin Shalamanov23c44202020-12-22 19:09:20 +010036#include "DisplayHardware/DisplayMode.h"
Lloyd Pique4603f3c2020-02-11 12:06:56 -080037#include "DisplayHardware/HWComposer.h"
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +010038#include "DisplayHardware/Hal.h"
Alec Mouriff793872022-01-13 17:45:06 -080039#include "DisplayIdentificationTestHelpers.h"
Lloyd Pique4603f3c2020-02-11 12:06:56 -080040#include "mock/DisplayHardware/MockComposer.h"
Lloyd Piquea516c002021-05-07 14:36:58 -070041#include "mock/DisplayHardware/MockHWC2.h"
Lloyd Pique4603f3c2020-02-11 12:06:56 -080042
43// TODO(b/129481165): remove the #pragma below and fix conversion issues
44#pragma clang diagnostic pop // ignored "-Wconversion"
45
46namespace android {
47namespace {
48
Marin Shalamanov6e840172020-12-14 22:13:28 +010049namespace V2_1 = hardware::graphics::composer::V2_1;
50namespace V2_4 = hardware::graphics::composer::V2_4;
51
52using Hwc2::Config;
Peiyong Line9d809e2020-04-14 13:10:48 -070053
Lloyd Pique4603f3c2020-02-11 12:06:56 -080054using ::testing::_;
55using ::testing::DoAll;
56using ::testing::ElementsAreArray;
57using ::testing::Return;
58using ::testing::SetArgPointee;
59using ::testing::StrictMock;
60
Marin Shalamanov8b196592021-08-09 16:24:42 +020061TEST(HWComposerTest, isHeadless) {
62 Hwc2::mock::Composer* mHal = new StrictMock<Hwc2::mock::Composer>();
63 impl::HWComposer hwc{std::unique_ptr<Hwc2::Composer>(mHal)};
64 ASSERT_TRUE(hwc.isHeadless());
65
66 const hal::HWDisplayId hwcId = 1;
67
68 EXPECT_CALL(*mHal, getDisplayIdentificationData(_, _, _))
69 .WillOnce(DoAll(SetArgPointee<2>(getExternalEdid()),
70 Return(hardware::graphics::composer::V2_1::Error::NONE)));
71
72 EXPECT_CALL(*mHal, setVsyncEnabled(_, _));
73 EXPECT_CALL(*mHal, setClientTargetSlotCount(_));
74
75 auto info = hwc.onHotplug(hwcId, hal::Connection::CONNECTED);
76 ASSERT_TRUE(info);
77 auto displayId = info->id;
78 ASSERT_FALSE(hwc.isHeadless());
79
80 hwc.disconnectDisplay(displayId);
81 ASSERT_TRUE(hwc.isHeadless());
82}
83
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070084struct MockHWC2ComposerCallback final : StrictMock<HWC2::ComposerCallback> {
Dominik Laskowski0deb06e2021-04-16 23:18:31 -070085 MOCK_METHOD2(onComposerHalHotplug, void(hal::HWDisplayId, hal::Connection));
86 MOCK_METHOD1(onComposerHalRefresh, void(hal::HWDisplayId));
87 MOCK_METHOD3(onComposerHalVsync,
88 void(hal::HWDisplayId, int64_t timestamp, std::optional<hal::VsyncPeriodNanos>));
89 MOCK_METHOD2(onComposerHalVsyncPeriodTimingChanged,
90 void(hal::HWDisplayId, const hal::VsyncPeriodChangeTimeline&));
91 MOCK_METHOD1(onComposerHalSeamlessPossible, void(hal::HWDisplayId));
Lloyd Pique4603f3c2020-02-11 12:06:56 -080092};
93
Dominik Laskowski0deb06e2021-04-16 23:18:31 -070094struct HWComposerSetCallbackTest : testing::Test {
Lloyd Pique4603f3c2020-02-11 12:06:56 -080095 Hwc2::mock::Composer* mHal = new StrictMock<Hwc2::mock::Composer>();
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070096 MockHWC2ComposerCallback mCallback;
Lloyd Pique4603f3c2020-02-11 12:06:56 -080097};
98
Dominik Laskowski0deb06e2021-04-16 23:18:31 -070099TEST_F(HWComposerSetCallbackTest, loadsLayerMetadataSupport) {
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800100 const std::string kMetadata1Name = "com.example.metadata.1";
101 constexpr bool kMetadata1Mandatory = false;
102 const std::string kMetadata2Name = "com.example.metadata.2";
103 constexpr bool kMetadata2Mandatory = true;
104
Peiyong Line9d809e2020-04-14 13:10:48 -0700105 EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<hal::Capability>{}));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800106 EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_))
Peiyong Line9d809e2020-04-14 13:10:48 -0700107 .WillOnce(DoAll(SetArgPointee<0>(std::vector<hal::LayerGenericMetadataKey>{
108 {kMetadata1Name, kMetadata1Mandatory},
109 {kMetadata2Name, kMetadata2Mandatory},
110 }),
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800111 Return(hardware::graphics::composer::V2_4::Error::NONE)));
112 EXPECT_CALL(*mHal, registerCallback(_));
Ady Abraham4d211cf2021-12-14 16:19:03 -0800113 EXPECT_CALL(*mHal, isSupported(Hwc2::Composer::OptionalFeature::RefreshRateSwitching))
114 .WillOnce(Return(false));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800115
116 impl::HWComposer hwc{std::unique_ptr<Hwc2::Composer>(mHal)};
Dominik Laskowski0deb06e2021-04-16 23:18:31 -0700117 hwc.setCallback(&mCallback);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800118
119 const auto& supported = hwc.getSupportedLayerGenericMetadata();
120 EXPECT_EQ(2u, supported.size());
121 EXPECT_EQ(1u, supported.count(kMetadata1Name));
122 EXPECT_EQ(kMetadata1Mandatory, supported.find(kMetadata1Name)->second);
123 EXPECT_EQ(1u, supported.count(kMetadata2Name));
124 EXPECT_EQ(kMetadata2Mandatory, supported.find(kMetadata2Name)->second);
125}
126
Dominik Laskowski0deb06e2021-04-16 23:18:31 -0700127TEST_F(HWComposerSetCallbackTest, handlesUnsupportedCallToGetLayerGenericMetadataKeys) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700128 EXPECT_CALL(*mHal, getCapabilities()).WillOnce(Return(std::vector<hal::Capability>{}));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800129 EXPECT_CALL(*mHal, getLayerGenericMetadataKeys(_))
130 .WillOnce(Return(hardware::graphics::composer::V2_4::Error::UNSUPPORTED));
131 EXPECT_CALL(*mHal, registerCallback(_));
Ady Abraham4d211cf2021-12-14 16:19:03 -0800132 EXPECT_CALL(*mHal, isSupported(Hwc2::Composer::OptionalFeature::RefreshRateSwitching))
133 .WillOnce(Return(false));
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800134
135 impl::HWComposer hwc{std::unique_ptr<Hwc2::Composer>(mHal)};
Dominik Laskowski0deb06e2021-04-16 23:18:31 -0700136 hwc.setCallback(&mCallback);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800137
138 const auto& supported = hwc.getSupportedLayerGenericMetadata();
139 EXPECT_EQ(0u, supported.size());
140}
141
142struct HWComposerLayerTest : public testing::Test {
Peiyong Line9d809e2020-04-14 13:10:48 -0700143 static constexpr hal::HWDisplayId kDisplayId = static_cast<hal::HWDisplayId>(1001);
144 static constexpr hal::HWLayerId kLayerId = static_cast<hal::HWLayerId>(1002);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800145
Peiyong Line9d809e2020-04-14 13:10:48 -0700146 HWComposerLayerTest(const std::unordered_set<hal::Capability>& capabilities)
Lloyd Piquea516c002021-05-07 14:36:58 -0700147 : mCapabilies(capabilities) {
148 EXPECT_CALL(mDisplay, getId()).WillRepeatedly(Return(kDisplayId));
149 }
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800150
Lloyd Piquea516c002021-05-07 14:36:58 -0700151 ~HWComposerLayerTest() override {
152 EXPECT_CALL(mDisplay, onLayerDestroyed(kLayerId));
153 EXPECT_CALL(*mHal, destroyLayer(kDisplayId, kLayerId));
154 }
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800155
156 std::unique_ptr<Hwc2::mock::Composer> mHal{new StrictMock<Hwc2::mock::Composer>()};
Peiyong Line9d809e2020-04-14 13:10:48 -0700157 const std::unordered_set<hal::Capability> mCapabilies;
Lloyd Piquea516c002021-05-07 14:36:58 -0700158 StrictMock<HWC2::mock::Display> mDisplay;
159 HWC2::impl::Layer mLayer{*mHal, mCapabilies, mDisplay, kLayerId};
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800160};
161
162struct HWComposerLayerGenericMetadataTest : public HWComposerLayerTest {
163 static const std::string kLayerGenericMetadata1Name;
164 static constexpr bool kLayerGenericMetadata1Mandatory = false;
165 static const std::vector<uint8_t> kLayerGenericMetadata1Value;
166 static const std::string kLayerGenericMetadata2Name;
167 static constexpr bool kLayerGenericMetadata2Mandatory = true;
168 static const std::vector<uint8_t> kLayerGenericMetadata2Value;
169
170 HWComposerLayerGenericMetadataTest() : HWComposerLayerTest({}) {}
171};
172
173const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Name =
174 "com.example.metadata.1";
175
176const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata1Value = {1u,
177 2u,
178 3u};
179
180const std::string HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Name =
181 "com.example.metadata.2";
182
183const std::vector<uint8_t> HWComposerLayerGenericMetadataTest::kLayerGenericMetadata2Value = {45u,
184 67u};
185
186TEST_F(HWComposerLayerGenericMetadataTest, forwardsSupportedMetadata) {
187 EXPECT_CALL(*mHal,
188 setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata1Name,
189 kLayerGenericMetadata1Mandatory,
190 kLayerGenericMetadata1Value))
191 .WillOnce(Return(hardware::graphics::composer::V2_4::Error::NONE));
192 auto result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata1Name,
193 kLayerGenericMetadata1Mandatory,
194 kLayerGenericMetadata1Value);
Peiyong Line9d809e2020-04-14 13:10:48 -0700195 EXPECT_EQ(hal::Error::NONE, result);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800196
197 EXPECT_CALL(*mHal,
198 setLayerGenericMetadata(kDisplayId, kLayerId, kLayerGenericMetadata2Name,
199 kLayerGenericMetadata2Mandatory,
200 kLayerGenericMetadata2Value))
201 .WillOnce(Return(hardware::graphics::composer::V2_4::Error::UNSUPPORTED));
202 result = mLayer.setLayerGenericMetadata(kLayerGenericMetadata2Name,
203 kLayerGenericMetadata2Mandatory,
204 kLayerGenericMetadata2Value);
Peiyong Line9d809e2020-04-14 13:10:48 -0700205 EXPECT_EQ(hal::Error::UNSUPPORTED, result);
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800206}
207
208} // namespace
Dominik Laskowski3dce4f42021-03-08 20:48:28 -0800209} // namespace android