blob: f6bf05add874d83e8fa0a45334bd555b6349390d [file] [log] [blame]
Ana Krulecc84d09b2019-11-02 23:10:29 +01001/*
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
Ady Abraham2b55c3b2020-01-16 16:38:51 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Ana Krulecc84d09b2019-11-02 23:10:29 +010021#undef LOG_TAG
22#define LOG_TAG "LibSurfaceFlingerUnittests"
23
24#include <gmock/gmock.h>
25#include <gtest/gtest.h>
26#include <gui/LayerMetadata.h>
27
28#include "BufferQueueLayer.h"
29#include "BufferStateLayer.h"
Vishnu Nairfa247b12020-02-11 08:58:26 -080030#include "EffectLayer.h"
Ana Krulecc84d09b2019-11-02 23:10:29 +010031#include "Layer.h"
32#include "TestableSurfaceFlinger.h"
33#include "mock/DisplayHardware/MockComposer.h"
34#include "mock/MockDispSync.h"
Ana Krulecc84d09b2019-11-02 23:10:29 +010035#include "mock/MockEventThread.h"
36
37namespace android {
38
39using testing::_;
40using testing::DoAll;
41using testing::Mock;
42using testing::Return;
43using testing::SetArgPointee;
44
45using android::Hwc2::IComposer;
46using android::Hwc2::IComposerClient;
47
48using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
49
50/**
51 * This class covers all the test that are related to refresh rate selection.
52 */
53class RefreshRateSelectionTest : public testing::Test {
54public:
55 RefreshRateSelectionTest();
56 ~RefreshRateSelectionTest() override;
57
58protected:
59 static constexpr int DEFAULT_DISPLAY_WIDTH = 1920;
60 static constexpr int DEFAULT_DISPLAY_HEIGHT = 1024;
61 static constexpr uint32_t WIDTH = 100;
62 static constexpr uint32_t HEIGHT = 100;
63 static constexpr uint32_t LAYER_FLAGS = 0;
64 static constexpr int32_t PRIORITY_UNSET = -1;
65
66 void setupScheduler();
67 void setupComposer(int virtualDisplayCount);
68 sp<BufferQueueLayer> createBufferQueueLayer();
69 sp<BufferStateLayer> createBufferStateLayer();
Vishnu Nairfa247b12020-02-11 08:58:26 -080070 sp<EffectLayer> createEffectLayer();
Ana Krulecc84d09b2019-11-02 23:10:29 +010071
72 void setParent(Layer* child, Layer* parent);
73 void commitTransaction(Layer* layer);
74
75 TestableSurfaceFlinger mFlinger;
76 Hwc2::mock::Composer* mComposer = nullptr;
77
78 sp<Client> mClient;
79 sp<Layer> mParent;
80 sp<Layer> mChild;
81 sp<Layer> mGrandChild;
82};
83
84RefreshRateSelectionTest::RefreshRateSelectionTest() {
85 const ::testing::TestInfo* const test_info =
86 ::testing::UnitTest::GetInstance()->current_test_info();
87 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
88
89 setupScheduler();
90 setupComposer(0);
91}
92
93RefreshRateSelectionTest::~RefreshRateSelectionTest() {
94 const ::testing::TestInfo* const test_info =
95 ::testing::UnitTest::GetInstance()->current_test_info();
96 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
97}
98
99sp<BufferQueueLayer> RefreshRateSelectionTest::createBufferQueueLayer() {
100 sp<Client> client;
101 LayerCreationArgs args(mFlinger.flinger(), client, "buffer-queue-layer", WIDTH, HEIGHT,
102 LAYER_FLAGS, LayerMetadata());
103 return new BufferQueueLayer(args);
104}
105
106sp<BufferStateLayer> RefreshRateSelectionTest::createBufferStateLayer() {
107 sp<Client> client;
108 LayerCreationArgs args(mFlinger.flinger(), client, "buffer-queue-layer", WIDTH, HEIGHT,
109 LAYER_FLAGS, LayerMetadata());
110 return new BufferStateLayer(args);
111}
112
Vishnu Nairfa247b12020-02-11 08:58:26 -0800113sp<EffectLayer> RefreshRateSelectionTest::createEffectLayer() {
Ana Krulecc84d09b2019-11-02 23:10:29 +0100114 sp<Client> client;
115 LayerCreationArgs args(mFlinger.flinger(), client, "color-layer", WIDTH, HEIGHT, LAYER_FLAGS,
116 LayerMetadata());
Vishnu Nairfa247b12020-02-11 08:58:26 -0800117 return new EffectLayer(args);
Ana Krulecc84d09b2019-11-02 23:10:29 +0100118}
119
120void RefreshRateSelectionTest::setParent(Layer* child, Layer* parent) {
121 child->setParent(parent);
122}
123
124void RefreshRateSelectionTest::commitTransaction(Layer* layer) {
125 layer->commitTransaction(layer->getCurrentState());
126}
127
128void RefreshRateSelectionTest::setupScheduler() {
129 auto eventThread = std::make_unique<mock::EventThread>();
130 auto sfEventThread = std::make_unique<mock::EventThread>();
131
132 EXPECT_CALL(*eventThread, registerDisplayEventConnection(_));
133 EXPECT_CALL(*eventThread, createEventConnection(_, _))
134 .WillOnce(Return(new EventThreadConnection(eventThread.get(), ResyncCallback(),
135 ISurfaceComposer::eConfigChangedSuppress)));
136
137 EXPECT_CALL(*sfEventThread, registerDisplayEventConnection(_));
138 EXPECT_CALL(*sfEventThread, createEventConnection(_, _))
139 .WillOnce(Return(new EventThreadConnection(sfEventThread.get(), ResyncCallback(),
140 ISurfaceComposer::eConfigChangedSuppress)));
141
142 auto primaryDispSync = std::make_unique<mock::DispSync>();
143
Ady Abraham0ed31c92020-04-16 11:48:45 -0700144 EXPECT_CALL(*primaryDispSync, computeNextRefresh(0, _)).WillRepeatedly(Return(0));
Ana Krulecc84d09b2019-11-02 23:10:29 +0100145 EXPECT_CALL(*primaryDispSync, getPeriod())
146 .WillRepeatedly(Return(FakeHwcDisplayInjector::DEFAULT_REFRESH_RATE));
Ady Abraham0ed31c92020-04-16 11:48:45 -0700147 EXPECT_CALL(*primaryDispSync, expectedPresentTime(_)).WillRepeatedly(Return(0));
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700148 mFlinger.setupScheduler(std::move(primaryDispSync), std::move(eventThread),
Ana Krulecc84d09b2019-11-02 23:10:29 +0100149 std::move(sfEventThread));
150}
151
152void RefreshRateSelectionTest::setupComposer(int virtualDisplayCount) {
153 mComposer = new Hwc2::mock::Composer();
Ana Krulecc84d09b2019-11-02 23:10:29 +0100154 EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount));
155 mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer));
156
157 Mock::VerifyAndClear(mComposer);
158}
159
160namespace {
161/* ------------------------------------------------------------------------
162 * Test cases
163 */
164TEST_F(RefreshRateSelectionTest, testPriorityOnBufferQueueLayers) {
165 mParent = createBufferQueueLayer();
166 mChild = createBufferQueueLayer();
167 setParent(mChild.get(), mParent.get());
168 mGrandChild = createBufferQueueLayer();
169 setParent(mGrandChild.get(), mChild.get());
170
171 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
172 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
173 ASSERT_EQ(PRIORITY_UNSET, mGrandChild->getFrameRateSelectionPriority());
174
175 // Child has its own priority.
176 mGrandChild->setFrameRateSelectionPriority(1);
177 commitTransaction(mGrandChild.get());
178 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
179 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
180 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
181
182 // Child inherits from his parent.
183 mChild->setFrameRateSelectionPriority(1);
184 commitTransaction(mChild.get());
185 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
186 commitTransaction(mGrandChild.get());
187
188 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
189 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
190 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
191
192 // Grandchild inherits from his grand parent.
193 mParent->setFrameRateSelectionPriority(1);
194 commitTransaction(mParent.get());
195 mChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
196 commitTransaction(mChild.get());
197 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
198 commitTransaction(mGrandChild.get());
199 ASSERT_EQ(1, mParent->getFrameRateSelectionPriority());
200 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
201 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
202}
203
204TEST_F(RefreshRateSelectionTest, testPriorityOnBufferStateLayers) {
205 mParent = createBufferStateLayer();
206 mChild = createBufferStateLayer();
207 setParent(mChild.get(), mParent.get());
208 mGrandChild = createBufferStateLayer();
209 setParent(mGrandChild.get(), mChild.get());
210
211 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
212 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
213 ASSERT_EQ(PRIORITY_UNSET, mGrandChild->getFrameRateSelectionPriority());
214
215 // Child has its own priority.
216 mGrandChild->setFrameRateSelectionPriority(1);
217 commitTransaction(mGrandChild.get());
218 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
219 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
220 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
221
222 // Child inherits from his parent.
223 mChild->setFrameRateSelectionPriority(1);
224 commitTransaction(mChild.get());
225 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
226 commitTransaction(mGrandChild.get());
227 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
228 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
229 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
230
231 // Grandchild inherits from his grand parent.
232 mParent->setFrameRateSelectionPriority(1);
233 commitTransaction(mParent.get());
234 mChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
235 commitTransaction(mChild.get());
236 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
237 commitTransaction(mGrandChild.get());
238 ASSERT_EQ(1, mParent->getFrameRateSelectionPriority());
239 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
240 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
241}
242
Vishnu Nairfa247b12020-02-11 08:58:26 -0800243TEST_F(RefreshRateSelectionTest, testPriorityOnEffectLayers) {
244 mParent = createEffectLayer();
245 mChild = createEffectLayer();
Ana Krulecc84d09b2019-11-02 23:10:29 +0100246 setParent(mChild.get(), mParent.get());
Vishnu Nairfa247b12020-02-11 08:58:26 -0800247 mGrandChild = createEffectLayer();
Ana Krulecc84d09b2019-11-02 23:10:29 +0100248 setParent(mGrandChild.get(), mChild.get());
249
250 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
251 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
252 ASSERT_EQ(PRIORITY_UNSET, mGrandChild->getFrameRateSelectionPriority());
253
254 // Child has its own priority.
255 mGrandChild->setFrameRateSelectionPriority(1);
256 commitTransaction(mGrandChild.get());
257 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
258 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
259 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
260
261 // Child inherits from his parent.
262 mChild->setFrameRateSelectionPriority(1);
263 commitTransaction(mChild.get());
264 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
265 commitTransaction(mGrandChild.get());
266 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
267 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
268 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
269
270 // Grandchild inherits from his grand parent.
271 mParent->setFrameRateSelectionPriority(1);
272 commitTransaction(mParent.get());
273 mChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
274 commitTransaction(mChild.get());
275 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
276 commitTransaction(mGrandChild.get());
277 ASSERT_EQ(1, mParent->getFrameRateSelectionPriority());
278 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
279 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
280}
281
282} // namespace
Ady Abraham2b55c3b2020-01-16 16:38:51 -0800283} // namespace android
284
285// TODO(b/129481165): remove the #pragma below and fix conversion issues
286#pragma clang diagnostic pop // ignored "-Wconversion"