• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/LayerMetadata.h>
23 
24 // TODO(b/129481165): remove the #pragma below and fix conversion issues
25 #pragma clang diagnostic push
26 #pragma clang diagnostic ignored "-Wconversion"
27 #include "BufferQueueLayer.h"
28 #include "BufferStateLayer.h"
29 #include "EffectLayer.h"
30 #include "Layer.h"
31 // TODO(b/129481165): remove the #pragma below and fix conversion issues
32 #pragma clang diagnostic pop // ignored "-Wconversion"
33 #include "TestableSurfaceFlinger.h"
34 #include "mock/DisplayHardware/MockComposer.h"
35 #include "mock/MockEventThread.h"
36 #include "mock/MockMessageQueue.h"
37 #include "mock/MockVsyncController.h"
38 
39 namespace android {
40 
41 using testing::_;
42 using testing::DoAll;
43 using testing::Mock;
44 using testing::Return;
45 using testing::SetArgPointee;
46 
47 using android::Hwc2::IComposer;
48 using android::Hwc2::IComposerClient;
49 
50 using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
51 
52 using FrameRate = Layer::FrameRate;
53 using FrameRateCompatibility = Layer::FrameRateCompatibility;
54 
55 class LayerFactory {
56 public:
57     virtual ~LayerFactory() = default;
58 
59     virtual std::string name() = 0;
60     virtual sp<Layer> createLayer(TestableSurfaceFlinger& flinger) = 0;
61 
62 protected:
63     static constexpr uint32_t WIDTH = 100;
64     static constexpr uint32_t HEIGHT = 100;
65     static constexpr uint32_t LAYER_FLAGS = 0;
66 };
67 
68 class BufferQueueLayerFactory : public LayerFactory {
69 public:
name()70     std::string name() override { return "BufferQueueLayer"; }
createLayer(TestableSurfaceFlinger & flinger)71     sp<Layer> createLayer(TestableSurfaceFlinger& flinger) override {
72         sp<Client> client;
73         LayerCreationArgs args(flinger.flinger(), client, "buffer-queue-layer", WIDTH, HEIGHT,
74                                LAYER_FLAGS, LayerMetadata());
75         return new BufferQueueLayer(args);
76     }
77 };
78 
79 class BufferStateLayerFactory : public LayerFactory {
80 public:
name()81     std::string name() override { return "BufferStateLayer"; }
createLayer(TestableSurfaceFlinger & flinger)82     sp<Layer> createLayer(TestableSurfaceFlinger& flinger) override {
83         sp<Client> client;
84         LayerCreationArgs args(flinger.flinger(), client, "buffer-state-layer", WIDTH, HEIGHT,
85                                LAYER_FLAGS, LayerMetadata());
86         return new BufferStateLayer(args);
87     }
88 };
89 
90 class EffectLayerFactory : public LayerFactory {
91 public:
name()92     std::string name() override { return "EffectLayer"; }
createLayer(TestableSurfaceFlinger & flinger)93     sp<Layer> createLayer(TestableSurfaceFlinger& flinger) override {
94         sp<Client> client;
95         LayerCreationArgs args(flinger.flinger(), client, "color-layer", WIDTH, HEIGHT, LAYER_FLAGS,
96                                LayerMetadata());
97         return new EffectLayer(args);
98     }
99 };
100 
PrintToStringParamName(const::testing::TestParamInfo<std::shared_ptr<LayerFactory>> & info)101 std::string PrintToStringParamName(
102         const ::testing::TestParamInfo<std::shared_ptr<LayerFactory>>& info) {
103     return info.param->name();
104 }
105 
106 /**
107  * This class tests the behaviour of Layer::SetFrameRate and Layer::GetFrameRate
108  */
109 class SetFrameRateTest : public ::testing::TestWithParam<std::shared_ptr<LayerFactory>> {
110 protected:
111     const FrameRate FRAME_RATE_VOTE1 = FrameRate(Fps(67.f), FrameRateCompatibility::Default);
112     const FrameRate FRAME_RATE_VOTE2 =
113             FrameRate(Fps(14.f), FrameRateCompatibility::ExactOrMultiple);
114     const FrameRate FRAME_RATE_VOTE3 = FrameRate(Fps(99.f), FrameRateCompatibility::NoVote);
115     const FrameRate FRAME_RATE_TREE = FrameRate(Fps(0.f), FrameRateCompatibility::NoVote);
116     const FrameRate FRAME_RATE_NO_VOTE = FrameRate(Fps(0.f), FrameRateCompatibility::Default);
117 
118     SetFrameRateTest();
119 
120     void setupScheduler();
121 
122     void addChild(sp<Layer> layer, sp<Layer> child);
123     void removeChild(sp<Layer> layer, sp<Layer> child);
124     void commitTransaction();
125 
126     TestableSurfaceFlinger mFlinger;
127     mock::MessageQueue* mMessageQueue = new mock::MessageQueue();
128 
129     std::vector<sp<Layer>> mLayers;
130 };
131 
SetFrameRateTest()132 SetFrameRateTest::SetFrameRateTest() {
133     const ::testing::TestInfo* const test_info =
134             ::testing::UnitTest::GetInstance()->current_test_info();
135     ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
136 
137     mFlinger.mutableUseFrameRateApi() = true;
138 
139     setupScheduler();
140 
141     mFlinger.setupComposer(std::make_unique<Hwc2::mock::Composer>());
142     mFlinger.mutableEventQueue().reset(mMessageQueue);
143 }
144 
addChild(sp<Layer> layer,sp<Layer> child)145 void SetFrameRateTest::addChild(sp<Layer> layer, sp<Layer> child) {
146     layer.get()->addChild(child.get());
147 }
148 
removeChild(sp<Layer> layer,sp<Layer> child)149 void SetFrameRateTest::removeChild(sp<Layer> layer, sp<Layer> child) {
150     layer.get()->removeChild(child.get());
151 }
152 
commitTransaction()153 void SetFrameRateTest::commitTransaction() {
154     for (auto layer : mLayers) {
155         auto c = layer->getDrawingState();
156         layer->commitTransaction(c);
157     }
158 }
159 
setupScheduler()160 void SetFrameRateTest::setupScheduler() {
161     auto eventThread = std::make_unique<mock::EventThread>();
162     auto sfEventThread = std::make_unique<mock::EventThread>();
163 
164     EXPECT_CALL(*eventThread, registerDisplayEventConnection(_));
165     EXPECT_CALL(*eventThread, createEventConnection(_, _))
166             .WillOnce(Return(new EventThreadConnection(eventThread.get(), /*callingUid=*/0,
167                                                        ResyncCallback())));
168 
169     EXPECT_CALL(*sfEventThread, registerDisplayEventConnection(_));
170     EXPECT_CALL(*sfEventThread, createEventConnection(_, _))
171             .WillOnce(Return(new EventThreadConnection(sfEventThread.get(), /*callingUid=*/0,
172                                                        ResyncCallback())));
173 
174     auto vsyncController = std::make_unique<mock::VsyncController>();
175     auto vsyncTracker = std::make_unique<mock::VSyncTracker>();
176 
177     EXPECT_CALL(*vsyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0));
178     EXPECT_CALL(*vsyncTracker, currentPeriod())
179             .WillRepeatedly(Return(FakeHwcDisplayInjector::DEFAULT_VSYNC_PERIOD));
180     EXPECT_CALL(*vsyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0));
181     mFlinger.setupScheduler(std::move(vsyncController), std::move(vsyncTracker),
182                             std::move(eventThread), std::move(sfEventThread), /*callback*/ nullptr,
183                             /*hasMultipleModes*/ true);
184 }
185 
186 namespace {
187 /* ------------------------------------------------------------------------
188  * Test cases
189  */
TEST_P(SetFrameRateTest,SetAndGet)190 TEST_P(SetFrameRateTest, SetAndGet) {
191     EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
192 
193     const auto& layerFactory = GetParam();
194 
195     auto layer = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
196     layer->setFrameRate(FRAME_RATE_VOTE1);
197     commitTransaction();
198     EXPECT_EQ(FRAME_RATE_VOTE1, layer->getFrameRateForLayerTree());
199 }
200 
TEST_P(SetFrameRateTest,SetAndGetParent)201 TEST_P(SetFrameRateTest, SetAndGetParent) {
202     EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
203 
204     const auto& layerFactory = GetParam();
205 
206     auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
207     auto child1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
208     auto child2 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
209 
210     addChild(parent, child1);
211     addChild(child1, child2);
212 
213     child2->setFrameRate(FRAME_RATE_VOTE1);
214     commitTransaction();
215     EXPECT_EQ(FRAME_RATE_TREE, parent->getFrameRateForLayerTree());
216     EXPECT_EQ(FRAME_RATE_TREE, child1->getFrameRateForLayerTree());
217     EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree());
218 
219     child2->setFrameRate(FRAME_RATE_NO_VOTE);
220     commitTransaction();
221     EXPECT_EQ(FRAME_RATE_NO_VOTE, parent->getFrameRateForLayerTree());
222     EXPECT_EQ(FRAME_RATE_NO_VOTE, child1->getFrameRateForLayerTree());
223     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
224 }
225 
TEST_P(SetFrameRateTest,SetAndGetParentAllVote)226 TEST_P(SetFrameRateTest, SetAndGetParentAllVote) {
227     EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
228 
229     const auto& layerFactory = GetParam();
230 
231     auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
232     auto child1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
233     auto child2 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
234 
235     addChild(parent, child1);
236     addChild(child1, child2);
237 
238     child2->setFrameRate(FRAME_RATE_VOTE1);
239     child1->setFrameRate(FRAME_RATE_VOTE2);
240     parent->setFrameRate(FRAME_RATE_VOTE3);
241     commitTransaction();
242     EXPECT_EQ(FRAME_RATE_VOTE3, parent->getFrameRateForLayerTree());
243     EXPECT_EQ(FRAME_RATE_VOTE2, child1->getFrameRateForLayerTree());
244     EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree());
245 
246     child2->setFrameRate(FRAME_RATE_NO_VOTE);
247     commitTransaction();
248     EXPECT_EQ(FRAME_RATE_VOTE3, parent->getFrameRateForLayerTree());
249     EXPECT_EQ(FRAME_RATE_VOTE2, child1->getFrameRateForLayerTree());
250     EXPECT_EQ(FRAME_RATE_VOTE2, child2->getFrameRateForLayerTree());
251 
252     child1->setFrameRate(FRAME_RATE_NO_VOTE);
253     commitTransaction();
254     EXPECT_EQ(FRAME_RATE_VOTE3, parent->getFrameRateForLayerTree());
255     EXPECT_EQ(FRAME_RATE_VOTE3, child1->getFrameRateForLayerTree());
256     EXPECT_EQ(FRAME_RATE_VOTE3, child2->getFrameRateForLayerTree());
257 
258     parent->setFrameRate(FRAME_RATE_NO_VOTE);
259     commitTransaction();
260     EXPECT_EQ(FRAME_RATE_NO_VOTE, parent->getFrameRateForLayerTree());
261     EXPECT_EQ(FRAME_RATE_NO_VOTE, child1->getFrameRateForLayerTree());
262     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
263 }
264 
TEST_P(SetFrameRateTest,SetAndGetChild)265 TEST_P(SetFrameRateTest, SetAndGetChild) {
266     EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
267 
268     const auto& layerFactory = GetParam();
269 
270     auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
271     auto child1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
272     auto child2 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
273 
274     addChild(parent, child1);
275     addChild(child1, child2);
276 
277     parent->setFrameRate(FRAME_RATE_VOTE1);
278     commitTransaction();
279     EXPECT_EQ(FRAME_RATE_VOTE1, parent->getFrameRateForLayerTree());
280     EXPECT_EQ(FRAME_RATE_VOTE1, child1->getFrameRateForLayerTree());
281     EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree());
282 
283     parent->setFrameRate(FRAME_RATE_NO_VOTE);
284     commitTransaction();
285     EXPECT_EQ(FRAME_RATE_NO_VOTE, parent->getFrameRateForLayerTree());
286     EXPECT_EQ(FRAME_RATE_NO_VOTE, child1->getFrameRateForLayerTree());
287     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
288 }
289 
TEST_P(SetFrameRateTest,SetAndGetChildAllVote)290 TEST_P(SetFrameRateTest, SetAndGetChildAllVote) {
291     EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
292 
293     const auto& layerFactory = GetParam();
294 
295     auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
296     auto child1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
297     auto child2 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
298 
299     addChild(parent, child1);
300     addChild(child1, child2);
301 
302     child2->setFrameRate(FRAME_RATE_VOTE1);
303     child1->setFrameRate(FRAME_RATE_VOTE2);
304     parent->setFrameRate(FRAME_RATE_VOTE3);
305     commitTransaction();
306     EXPECT_EQ(FRAME_RATE_VOTE3, parent->getFrameRateForLayerTree());
307     EXPECT_EQ(FRAME_RATE_VOTE2, child1->getFrameRateForLayerTree());
308     EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree());
309 
310     parent->setFrameRate(FRAME_RATE_NO_VOTE);
311     commitTransaction();
312     EXPECT_EQ(FRAME_RATE_TREE, parent->getFrameRateForLayerTree());
313     EXPECT_EQ(FRAME_RATE_VOTE2, child1->getFrameRateForLayerTree());
314     EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree());
315 
316     child1->setFrameRate(FRAME_RATE_NO_VOTE);
317     commitTransaction();
318     EXPECT_EQ(FRAME_RATE_TREE, parent->getFrameRateForLayerTree());
319     EXPECT_EQ(FRAME_RATE_TREE, child1->getFrameRateForLayerTree());
320     EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree());
321 
322     child2->setFrameRate(FRAME_RATE_NO_VOTE);
323     commitTransaction();
324     EXPECT_EQ(FRAME_RATE_NO_VOTE, parent->getFrameRateForLayerTree());
325     EXPECT_EQ(FRAME_RATE_NO_VOTE, child1->getFrameRateForLayerTree());
326     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
327 }
328 
TEST_P(SetFrameRateTest,SetAndGetChildAddAfterVote)329 TEST_P(SetFrameRateTest, SetAndGetChildAddAfterVote) {
330     EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
331 
332     const auto& layerFactory = GetParam();
333 
334     auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
335     auto child1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
336     auto child2 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
337 
338     addChild(parent, child1);
339 
340     parent->setFrameRate(FRAME_RATE_VOTE1);
341     commitTransaction();
342     EXPECT_EQ(FRAME_RATE_VOTE1, parent->getFrameRateForLayerTree());
343     EXPECT_EQ(FRAME_RATE_VOTE1, child1->getFrameRateForLayerTree());
344     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
345 
346     addChild(child1, child2);
347     commitTransaction();
348     EXPECT_EQ(FRAME_RATE_VOTE1, parent->getFrameRateForLayerTree());
349     EXPECT_EQ(FRAME_RATE_VOTE1, child1->getFrameRateForLayerTree());
350     EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree());
351 
352     parent->setFrameRate(FRAME_RATE_NO_VOTE);
353     commitTransaction();
354     EXPECT_EQ(FRAME_RATE_NO_VOTE, parent->getFrameRateForLayerTree());
355     EXPECT_EQ(FRAME_RATE_NO_VOTE, child1->getFrameRateForLayerTree());
356     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
357 }
358 
TEST_P(SetFrameRateTest,SetAndGetChildRemoveAfterVote)359 TEST_P(SetFrameRateTest, SetAndGetChildRemoveAfterVote) {
360     EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
361 
362     const auto& layerFactory = GetParam();
363 
364     auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
365     auto child1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
366     auto child2 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
367 
368     addChild(parent, child1);
369     addChild(child1, child2);
370 
371     parent->setFrameRate(FRAME_RATE_VOTE1);
372     commitTransaction();
373     EXPECT_EQ(FRAME_RATE_VOTE1, parent->getFrameRateForLayerTree());
374     EXPECT_EQ(FRAME_RATE_VOTE1, child1->getFrameRateForLayerTree());
375     EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree());
376 
377     removeChild(child1, child2);
378     commitTransaction();
379     EXPECT_EQ(FRAME_RATE_VOTE1, parent->getFrameRateForLayerTree());
380     EXPECT_EQ(FRAME_RATE_VOTE1, child1->getFrameRateForLayerTree());
381     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
382 
383     parent->setFrameRate(FRAME_RATE_NO_VOTE);
384     commitTransaction();
385     EXPECT_EQ(FRAME_RATE_NO_VOTE, parent->getFrameRateForLayerTree());
386     EXPECT_EQ(FRAME_RATE_NO_VOTE, child1->getFrameRateForLayerTree());
387     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
388 }
389 
TEST_P(SetFrameRateTest,SetAndGetParentNotInTree)390 TEST_P(SetFrameRateTest, SetAndGetParentNotInTree) {
391     EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
392 
393     const auto& layerFactory = GetParam();
394 
395     auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
396     auto child1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
397     auto child2 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
398     auto child2_1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
399 
400     addChild(parent, child1);
401     addChild(child1, child2);
402     addChild(child1, child2_1);
403 
404     child2->setFrameRate(FRAME_RATE_VOTE1);
405     commitTransaction();
406     EXPECT_EQ(FRAME_RATE_TREE, parent->getFrameRateForLayerTree());
407     EXPECT_EQ(FRAME_RATE_TREE, child1->getFrameRateForLayerTree());
408     EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree());
409     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2_1->getFrameRateForLayerTree());
410 
411     child2->setFrameRate(FRAME_RATE_NO_VOTE);
412     commitTransaction();
413     EXPECT_EQ(FRAME_RATE_NO_VOTE, parent->getFrameRateForLayerTree());
414     EXPECT_EQ(FRAME_RATE_NO_VOTE, child1->getFrameRateForLayerTree());
415     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
416     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2_1->getFrameRateForLayerTree());
417 }
418 
419 INSTANTIATE_TEST_SUITE_P(PerLayerType, SetFrameRateTest,
420                          testing::Values(std::make_shared<BufferQueueLayerFactory>(),
421                                          std::make_shared<BufferStateLayerFactory>(),
422                                          std::make_shared<EffectLayerFactory>()),
423                          PrintToStringParamName);
424 
TEST_F(SetFrameRateTest,ValidateFrameRate)425 TEST_F(SetFrameRateTest, ValidateFrameRate) {
426     EXPECT_TRUE(ValidateFrameRate(60.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT,
427                                   ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, ""));
428     EXPECT_TRUE(ValidateFrameRate(60.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT,
429                                   ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, ""));
430     EXPECT_TRUE(ValidateFrameRate(60.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT,
431                                   ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS, ""));
432     EXPECT_TRUE(ValidateFrameRate(60.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,
433                                   ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, ""));
434     EXPECT_TRUE(ValidateFrameRate(60.0f, ANATIVEWINDOW_FRAME_RATE_EXACT,
435                                   ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, "",
436                                   /*privileged=*/true));
437 
438     EXPECT_FALSE(ValidateFrameRate(-1, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT,
439                                    ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, ""));
440     EXPECT_FALSE(ValidateFrameRate(1.0f / 0.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT,
441                                    ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, ""));
442     EXPECT_FALSE(ValidateFrameRate(0.0f / 0.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT,
443                                    ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, ""));
444 
445     EXPECT_FALSE(ValidateFrameRate(60.0f, ANATIVEWINDOW_FRAME_RATE_EXACT,
446                                    ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, ""));
447 
448     // Invalid compatibility
449     EXPECT_FALSE(
450             ValidateFrameRate(60.0f, -1, ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, ""));
451     EXPECT_FALSE(ValidateFrameRate(60.0f, 2, ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, ""));
452 
453     // Invalid change frame rate strategy
454     EXPECT_FALSE(ValidateFrameRate(60.0f, ANATIVEWINDOW_FRAME_RATE_EXACT, -1, ""));
455     EXPECT_FALSE(ValidateFrameRate(60.0f, ANATIVEWINDOW_FRAME_RATE_EXACT, 2, ""));
456 }
457 
TEST_P(SetFrameRateTest,SetOnParentActivatesTree)458 TEST_P(SetFrameRateTest, SetOnParentActivatesTree) {
459     const auto& layerFactory = GetParam();
460 
461     auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
462     if (!parent->isVisible()) {
463         // This is a hack as all the test layers except EffectLayer are not visible,
464         // but since the logic is unified in Layer, it should be fine.
465         return;
466     }
467 
468     auto child = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
469     addChild(parent, child);
470 
471     parent->setFrameRate(FRAME_RATE_VOTE1);
472     commitTransaction();
473 
474     mFlinger.mutableScheduler()
475             .mutableLayerHistory()
476             ->record(parent.get(), 0, 0, LayerHistory::LayerUpdateType::Buffer);
477     mFlinger.mutableScheduler()
478             .mutableLayerHistory()
479             ->record(child.get(), 0, 0, LayerHistory::LayerUpdateType::Buffer);
480 
481     const auto layerHistorySummary =
482             mFlinger.mutableScheduler().mutableLayerHistory()->summarize(0);
483     ASSERT_EQ(2u, layerHistorySummary.size());
484     EXPECT_TRUE(FRAME_RATE_VOTE1.rate.equalsWithMargin(layerHistorySummary[0].desiredRefreshRate));
485     EXPECT_TRUE(FRAME_RATE_VOTE1.rate.equalsWithMargin(layerHistorySummary[1].desiredRefreshRate));
486 }
487 
TEST_P(SetFrameRateTest,addChildForParentWithTreeVote)488 TEST_P(SetFrameRateTest, addChildForParentWithTreeVote) {
489     EXPECT_CALL(*mMessageQueue, invalidate()).Times(1);
490 
491     const auto& layerFactory = GetParam();
492 
493     const auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
494     const auto child1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
495     const auto child2 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
496     const auto childOfChild1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger));
497 
498     addChild(parent, child1);
499     addChild(child1, childOfChild1);
500 
501     childOfChild1->setFrameRate(FRAME_RATE_VOTE1);
502     commitTransaction();
503     EXPECT_EQ(FRAME_RATE_TREE, parent->getFrameRateForLayerTree());
504     EXPECT_EQ(FRAME_RATE_TREE, child1->getFrameRateForLayerTree());
505     EXPECT_EQ(FRAME_RATE_VOTE1, childOfChild1->getFrameRateForLayerTree());
506     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
507 
508     addChild(parent, child2);
509     commitTransaction();
510     EXPECT_EQ(FRAME_RATE_TREE, parent->getFrameRateForLayerTree());
511     EXPECT_EQ(FRAME_RATE_TREE, child1->getFrameRateForLayerTree());
512     EXPECT_EQ(FRAME_RATE_VOTE1, childOfChild1->getFrameRateForLayerTree());
513     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
514 
515     childOfChild1->setFrameRate(FRAME_RATE_NO_VOTE);
516     commitTransaction();
517     EXPECT_EQ(FRAME_RATE_NO_VOTE, parent->getFrameRateForLayerTree());
518     EXPECT_EQ(FRAME_RATE_NO_VOTE, child1->getFrameRateForLayerTree());
519     EXPECT_EQ(FRAME_RATE_NO_VOTE, childOfChild1->getFrameRateForLayerTree());
520     EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree());
521 }
522 
523 } // namespace
524 } // namespace android
525