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 #undef LOG_TAG
18 #define LOG_TAG "LibSurfaceFlingerUnittests"
19 
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include <gui/FrameRateUtils.h>
23 #include <gui/LayerMetadata.h>
24 
25 // TODO(b/129481165): remove the #pragma below and fix conversion issues
26 #pragma clang diagnostic push
27 #pragma clang diagnostic ignored "-Wconversion"
28 #include "Layer.h"
29 // TODO(b/129481165): remove the #pragma below and fix conversion issues
30 #pragma clang diagnostic pop // ignored "-Wconversion"
31 #include "FpsOps.h"
32 #include "LayerTestUtils.h"
33 #include "TestableSurfaceFlinger.h"
34 #include "mock/DisplayHardware/MockComposer.h"
35 #include "mock/MockVsyncController.h"
36 
37 namespace android {
38 
39 using testing::DoAll;
40 using testing::Mock;
41 using testing::SetArgPointee;
42 
43 using android::Hwc2::IComposer;
44 using android::Hwc2::IComposerClient;
45 
46 using scheduler::LayerHistory;
47 
48 using FrameRate = Layer::FrameRate;
49 using FrameRateCompatibility = Layer::FrameRateCompatibility;
50 
51 /**
52  * This class tests the behaviour of Layer::SetFrameRate and Layer::GetFrameRate
53  */
54 class SetFrameRateTest : public BaseLayerTest {
55 protected:
56     const FrameRate FRAME_RATE_VOTE1 = FrameRate(67_Hz, FrameRateCompatibility::Default);
57     const FrameRate FRAME_RATE_VOTE2 = FrameRate(14_Hz, FrameRateCompatibility::ExactOrMultiple);
58     const FrameRate FRAME_RATE_VOTE3 = FrameRate(99_Hz, FrameRateCompatibility::NoVote);
59     const FrameRate FRAME_RATE_TREE = FrameRate(Fps(), FrameRateCompatibility::NoVote);
60     const FrameRate FRAME_RATE_NO_VOTE = FrameRate(Fps(), FrameRateCompatibility::Default);
61 
62     SetFrameRateTest();
63 
64     void addChild(sp<Layer> layer, sp<Layer> child);
65     void removeChild(sp<Layer> layer, sp<Layer> child);
66     void commitTransaction();
67 
68     std::vector<sp<Layer>> mLayers;
69 };
70 
SetFrameRateTest()71 SetFrameRateTest::SetFrameRateTest() {
72     const ::testing::TestInfo* const test_info =
73             ::testing::UnitTest::GetInstance()->current_test_info();
74     ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
75 
76     mFlinger.setupComposer(std::make_unique<Hwc2::mock::Composer>());
77 }
78 
addChild(sp<Layer> layer,sp<Layer> child)79 void SetFrameRateTest::addChild(sp<Layer> layer, sp<Layer> child) {
80     layer->addChild(child);
81 }
82 
removeChild(sp<Layer> layer,sp<Layer> child)83 void SetFrameRateTest::removeChild(sp<Layer> layer, sp<Layer> child) {
84     layer->removeChild(child);
85 }
86 
commitTransaction()87 void SetFrameRateTest::commitTransaction() {
88     for (auto layer : mLayers) {
89         layer->commitTransaction();
90     }
91 }
92 
93 namespace {
94 
TEST_P(SetFrameRateTest,SetAndGet)95 TEST_P(SetFrameRateTest, SetAndGet) {
96     EXPECT_CALL(*mFlinger.scheduler(), scheduleFrame()).Times(1);
97 
98     const auto& layerFactory = GetParam();
99 
100     auto layer = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
101     layer->setFrameRate(FRAME_RATE_VOTE1.vote);
102     commitTransaction();
103     EXPECT_EQ(FRAME_RATE_VOTE1, layer->getFrameRateForLayerTree());
104 }
105 
TEST_P(SetFrameRateTest,SetAndGetParent)106 TEST_P(SetFrameRateTest, SetAndGetParent) {
107     EXPECT_CALL(*mFlinger.scheduler(), scheduleFrame()).Times(1);
108 
109     const auto& layerFactory = GetParam();
110 
111     auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
112     auto child1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
113     auto child2 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
114 
115     addChild(parent, child1);
116     addChild(child1, child2);
117 
118     child2->setFrameRate(FRAME_RATE_VOTE1.vote);
119     commitTransaction();
120     EXPECT_EQ(FRAME_RATE_TREE, parent->getFrameRateForLayerTree());
121     EXPECT_EQ(FRAME_RATE_TREE, child1->getFrameRateForLayerTree());
122     EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree());
123 
124     child2->setFrameRate(FRAME_RATE_NO_VOTE.vote);
125     commitTransaction();
126     EXPECT_EQ(FRAME_RATE_NO_VOTE, parent->getFrameRateForLayerTree());
127     EXPECT_EQ(FRAME_RATE_NO_VOTE, child1->getFrameRateForLayerTree());
128     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
129 }
130 
TEST_P(SetFrameRateTest,SetAndGetParentAllVote)131 TEST_P(SetFrameRateTest, SetAndGetParentAllVote) {
132     EXPECT_CALL(*mFlinger.scheduler(), scheduleFrame()).Times(1);
133 
134     const auto& layerFactory = GetParam();
135 
136     auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
137     auto child1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
138     auto child2 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
139 
140     addChild(parent, child1);
141     addChild(child1, child2);
142 
143     child2->setFrameRate(FRAME_RATE_VOTE1.vote);
144     child1->setFrameRate(FRAME_RATE_VOTE2.vote);
145     parent->setFrameRate(FRAME_RATE_VOTE3.vote);
146     commitTransaction();
147     EXPECT_EQ(FRAME_RATE_VOTE3, parent->getFrameRateForLayerTree());
148     EXPECT_EQ(FRAME_RATE_VOTE2, child1->getFrameRateForLayerTree());
149     EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree());
150 
151     child2->setFrameRate(FRAME_RATE_NO_VOTE.vote);
152     commitTransaction();
153     EXPECT_EQ(FRAME_RATE_VOTE3, parent->getFrameRateForLayerTree());
154     EXPECT_EQ(FRAME_RATE_VOTE2, child1->getFrameRateForLayerTree());
155     EXPECT_EQ(FRAME_RATE_VOTE2, child2->getFrameRateForLayerTree());
156 
157     child1->setFrameRate(FRAME_RATE_NO_VOTE.vote);
158     commitTransaction();
159     EXPECT_EQ(FRAME_RATE_VOTE3, parent->getFrameRateForLayerTree());
160     EXPECT_EQ(FRAME_RATE_VOTE3, child1->getFrameRateForLayerTree());
161     EXPECT_EQ(FRAME_RATE_VOTE3, child2->getFrameRateForLayerTree());
162 
163     parent->setFrameRate(FRAME_RATE_NO_VOTE.vote);
164     commitTransaction();
165     EXPECT_EQ(FRAME_RATE_NO_VOTE, parent->getFrameRateForLayerTree());
166     EXPECT_EQ(FRAME_RATE_NO_VOTE, child1->getFrameRateForLayerTree());
167     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
168 }
169 
TEST_P(SetFrameRateTest,SetAndGetChild)170 TEST_P(SetFrameRateTest, SetAndGetChild) {
171     EXPECT_CALL(*mFlinger.scheduler(), scheduleFrame()).Times(1);
172 
173     const auto& layerFactory = GetParam();
174 
175     auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
176     auto child1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
177     auto child2 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
178 
179     addChild(parent, child1);
180     addChild(child1, child2);
181 
182     parent->setFrameRate(FRAME_RATE_VOTE1.vote);
183     commitTransaction();
184     EXPECT_EQ(FRAME_RATE_VOTE1, parent->getFrameRateForLayerTree());
185     EXPECT_EQ(FRAME_RATE_VOTE1, child1->getFrameRateForLayerTree());
186     EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree());
187 
188     parent->setFrameRate(FRAME_RATE_NO_VOTE.vote);
189     commitTransaction();
190     EXPECT_EQ(FRAME_RATE_NO_VOTE, parent->getFrameRateForLayerTree());
191     EXPECT_EQ(FRAME_RATE_NO_VOTE, child1->getFrameRateForLayerTree());
192     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
193 }
194 
TEST_P(SetFrameRateTest,SetAndGetChildAllVote)195 TEST_P(SetFrameRateTest, SetAndGetChildAllVote) {
196     EXPECT_CALL(*mFlinger.scheduler(), scheduleFrame()).Times(1);
197 
198     const auto& layerFactory = GetParam();
199 
200     auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
201     auto child1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
202     auto child2 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
203 
204     addChild(parent, child1);
205     addChild(child1, child2);
206 
207     child2->setFrameRate(FRAME_RATE_VOTE1.vote);
208     child1->setFrameRate(FRAME_RATE_VOTE2.vote);
209     parent->setFrameRate(FRAME_RATE_VOTE3.vote);
210     commitTransaction();
211     EXPECT_EQ(FRAME_RATE_VOTE3, parent->getFrameRateForLayerTree());
212     EXPECT_EQ(FRAME_RATE_VOTE2, child1->getFrameRateForLayerTree());
213     EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree());
214 
215     parent->setFrameRate(FRAME_RATE_NO_VOTE.vote);
216     commitTransaction();
217     EXPECT_EQ(FRAME_RATE_TREE, parent->getFrameRateForLayerTree());
218     EXPECT_EQ(FRAME_RATE_VOTE2, child1->getFrameRateForLayerTree());
219     EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree());
220 
221     child1->setFrameRate(FRAME_RATE_NO_VOTE.vote);
222     commitTransaction();
223     EXPECT_EQ(FRAME_RATE_TREE, parent->getFrameRateForLayerTree());
224     EXPECT_EQ(FRAME_RATE_TREE, child1->getFrameRateForLayerTree());
225     EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree());
226 
227     child2->setFrameRate(FRAME_RATE_NO_VOTE.vote);
228     commitTransaction();
229     EXPECT_EQ(FRAME_RATE_NO_VOTE, parent->getFrameRateForLayerTree());
230     EXPECT_EQ(FRAME_RATE_NO_VOTE, child1->getFrameRateForLayerTree());
231     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
232 }
233 
TEST_P(SetFrameRateTest,SetAndGetChildAddAfterVote)234 TEST_P(SetFrameRateTest, SetAndGetChildAddAfterVote) {
235     EXPECT_CALL(*mFlinger.scheduler(), scheduleFrame()).Times(1);
236 
237     const auto& layerFactory = GetParam();
238 
239     auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
240     auto child1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
241     auto child2 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
242 
243     addChild(parent, child1);
244 
245     parent->setFrameRate(FRAME_RATE_VOTE1.vote);
246     commitTransaction();
247     EXPECT_EQ(FRAME_RATE_VOTE1, parent->getFrameRateForLayerTree());
248     EXPECT_EQ(FRAME_RATE_VOTE1, child1->getFrameRateForLayerTree());
249     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
250 
251     addChild(child1, child2);
252     commitTransaction();
253     EXPECT_EQ(FRAME_RATE_VOTE1, parent->getFrameRateForLayerTree());
254     EXPECT_EQ(FRAME_RATE_VOTE1, child1->getFrameRateForLayerTree());
255     EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree());
256 
257     parent->setFrameRate(FRAME_RATE_NO_VOTE.vote);
258     commitTransaction();
259     EXPECT_EQ(FRAME_RATE_NO_VOTE, parent->getFrameRateForLayerTree());
260     EXPECT_EQ(FRAME_RATE_NO_VOTE, child1->getFrameRateForLayerTree());
261     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
262 }
263 
TEST_P(SetFrameRateTest,SetAndGetChildRemoveAfterVote)264 TEST_P(SetFrameRateTest, SetAndGetChildRemoveAfterVote) {
265     EXPECT_CALL(*mFlinger.scheduler(), scheduleFrame()).Times(1);
266 
267     const auto& layerFactory = GetParam();
268 
269     auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
270     auto child1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
271     auto child2 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
272 
273     addChild(parent, child1);
274     addChild(child1, child2);
275 
276     parent->setFrameRate(FRAME_RATE_VOTE1.vote);
277     commitTransaction();
278     EXPECT_EQ(FRAME_RATE_VOTE1, parent->getFrameRateForLayerTree());
279     EXPECT_EQ(FRAME_RATE_VOTE1, child1->getFrameRateForLayerTree());
280     EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree());
281 
282     removeChild(child1, child2);
283     commitTransaction();
284     EXPECT_EQ(FRAME_RATE_VOTE1, parent->getFrameRateForLayerTree());
285     EXPECT_EQ(FRAME_RATE_VOTE1, child1->getFrameRateForLayerTree());
286     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
287 
288     parent->setFrameRate(FRAME_RATE_NO_VOTE.vote);
289     commitTransaction();
290     EXPECT_EQ(FRAME_RATE_NO_VOTE, parent->getFrameRateForLayerTree());
291     EXPECT_EQ(FRAME_RATE_NO_VOTE, child1->getFrameRateForLayerTree());
292     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
293 }
294 
TEST_P(SetFrameRateTest,SetAndGetParentNotInTree)295 TEST_P(SetFrameRateTest, SetAndGetParentNotInTree) {
296     EXPECT_CALL(*mFlinger.scheduler(), scheduleFrame()).Times(1);
297 
298     const auto& layerFactory = GetParam();
299 
300     auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
301     auto child1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
302     auto child2 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
303     auto child2_1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
304 
305     addChild(parent, child1);
306     addChild(child1, child2);
307     addChild(child1, child2_1);
308 
309     child2->setFrameRate(FRAME_RATE_VOTE1.vote);
310     commitTransaction();
311     EXPECT_EQ(FRAME_RATE_TREE, parent->getFrameRateForLayerTree());
312     EXPECT_EQ(FRAME_RATE_TREE, child1->getFrameRateForLayerTree());
313     EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree());
314     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2_1->getFrameRateForLayerTree());
315 
316     child2->setFrameRate(FRAME_RATE_NO_VOTE.vote);
317     commitTransaction();
318     EXPECT_EQ(FRAME_RATE_NO_VOTE, parent->getFrameRateForLayerTree());
319     EXPECT_EQ(FRAME_RATE_NO_VOTE, child1->getFrameRateForLayerTree());
320     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
321     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2_1->getFrameRateForLayerTree());
322 }
323 
324 INSTANTIATE_TEST_SUITE_P(PerLayerType, SetFrameRateTest,
325                          testing::Values(std::make_shared<BufferStateLayerFactory>(),
326                                          std::make_shared<EffectLayerFactory>()),
327                          PrintToStringParamName);
328 
TEST_P(SetFrameRateTest,SetOnParentActivatesTree)329 TEST_P(SetFrameRateTest, SetOnParentActivatesTree) {
330     const auto& layerFactory = GetParam();
331 
332     auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
333 
334     auto child = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
335     addChild(parent, child);
336 
337     parent->setFrameRate(FRAME_RATE_VOTE1.vote);
338     commitTransaction();
339 
340     auto& history = mFlinger.mutableScheduler().mutableLayerHistory();
341     history.record(parent->getSequence(), parent->getLayerProps(), 0, 0,
342                    LayerHistory::LayerUpdateType::Buffer);
343     history.record(child->getSequence(), child->getLayerProps(), 0, 0,
344                    LayerHistory::LayerUpdateType::Buffer);
345 
346     const auto selectorPtr = mFlinger.mutableScheduler().refreshRateSelector();
347     const auto summary = history.summarize(*selectorPtr, 0);
348 
349     ASSERT_EQ(2u, summary.size());
350     EXPECT_EQ(FRAME_RATE_VOTE1.vote.rate, summary[0].desiredRefreshRate);
351     EXPECT_EQ(FRAME_RATE_VOTE1.vote.rate, summary[1].desiredRefreshRate);
352 }
353 
TEST_P(SetFrameRateTest,addChildForParentWithTreeVote)354 TEST_P(SetFrameRateTest, addChildForParentWithTreeVote) {
355     EXPECT_CALL(*mFlinger.scheduler(), scheduleFrame()).Times(1);
356 
357     const auto& layerFactory = GetParam();
358 
359     const auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
360     const auto child1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
361     const auto child2 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
362     const auto childOfChild1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
363 
364     addChild(parent, child1);
365     addChild(child1, childOfChild1);
366 
367     childOfChild1->setFrameRate(FRAME_RATE_VOTE1.vote);
368     commitTransaction();
369     EXPECT_EQ(FRAME_RATE_TREE, parent->getFrameRateForLayerTree());
370     EXPECT_EQ(FRAME_RATE_TREE, child1->getFrameRateForLayerTree());
371     EXPECT_EQ(FRAME_RATE_VOTE1, childOfChild1->getFrameRateForLayerTree());
372     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
373 
374     addChild(parent, child2);
375     commitTransaction();
376     EXPECT_EQ(FRAME_RATE_TREE, parent->getFrameRateForLayerTree());
377     EXPECT_EQ(FRAME_RATE_TREE, child1->getFrameRateForLayerTree());
378     EXPECT_EQ(FRAME_RATE_VOTE1, childOfChild1->getFrameRateForLayerTree());
379     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
380 
381     childOfChild1->setFrameRate(FRAME_RATE_NO_VOTE.vote);
382     commitTransaction();
383     EXPECT_EQ(FRAME_RATE_NO_VOTE, parent->getFrameRateForLayerTree());
384     EXPECT_EQ(FRAME_RATE_NO_VOTE, child1->getFrameRateForLayerTree());
385     EXPECT_EQ(FRAME_RATE_NO_VOTE, childOfChild1->getFrameRateForLayerTree());
386     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
387 }
388 
389 } // namespace
390 } // namespace android
391