• 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 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wextra"
20 
21 #undef LOG_TAG
22 #define LOG_TAG "LayerHistoryTest"
23 
24 #include <Layer.h>
25 #include <gmock/gmock.h>
26 #include <gtest/gtest.h>
27 #include <log/log.h>
28 
29 #include "Scheduler/LayerHistory.h"
30 #include "Scheduler/LayerInfo.h"
31 #include "TestableScheduler.h"
32 #include "TestableSurfaceFlinger.h"
33 #include "mock/MockLayer.h"
34 #include "mock/MockSchedulerCallback.h"
35 
36 using testing::_;
37 using testing::Return;
38 
39 namespace android {
40 
41 namespace scheduler {
42 
43 class LayerHistoryTest : public testing::Test {
44 protected:
45     static constexpr auto PRESENT_TIME_HISTORY_SIZE = LayerInfo::HISTORY_SIZE;
46     static constexpr auto MAX_FREQUENT_LAYER_PERIOD_NS = LayerInfo::kMaxPeriodForFrequentLayerNs;
47     static constexpr auto FREQUENT_LAYER_WINDOW_SIZE = LayerInfo::kFrequentLayerWindowSize;
48     static constexpr auto PRESENT_TIME_HISTORY_DURATION = LayerInfo::HISTORY_DURATION;
49     static constexpr auto REFRESH_RATE_AVERAGE_HISTORY_DURATION =
50             LayerInfo::RefreshRateHistory::HISTORY_DURATION;
51 
52     static constexpr Fps LO_FPS{30.f};
53     static constexpr auto LO_FPS_PERIOD = LO_FPS.getPeriodNsecs();
54 
55     static constexpr Fps HI_FPS{90.f};
56     static constexpr auto HI_FPS_PERIOD = HI_FPS.getPeriodNsecs();
57 
LayerHistoryTest()58     LayerHistoryTest() { mFlinger.resetScheduler(mScheduler); }
59 
SetUp()60     void SetUp() override { ASSERT_TRUE(mScheduler->hasLayerHistory()); }
61 
history()62     LayerHistory& history() { return *mScheduler->mutableLayerHistory(); }
history() const63     const LayerHistory& history() const { return *mScheduler->mutableLayerHistory(); }
64 
layerCount() const65     size_t layerCount() const { return mScheduler->layerHistorySize(); }
activeLayerCount() const66     size_t activeLayerCount() const NO_THREAD_SAFETY_ANALYSIS { return history().mActiveLayersEnd; }
67 
frequentLayerCount(nsecs_t now) const68     auto frequentLayerCount(nsecs_t now) const NO_THREAD_SAFETY_ANALYSIS {
69         const auto& infos = history().mLayerInfos;
70         return std::count_if(infos.begin(),
71                              infos.begin() + static_cast<long>(history().mActiveLayersEnd),
72                              [now](const auto& pair) { return pair.second->isFrequent(now); });
73     }
74 
animatingLayerCount(nsecs_t now) const75     auto animatingLayerCount(nsecs_t now) const NO_THREAD_SAFETY_ANALYSIS {
76         const auto& infos = history().mLayerInfos;
77         return std::count_if(infos.begin(),
78                              infos.begin() + static_cast<long>(history().mActiveLayersEnd),
79                              [now](const auto& pair) { return pair.second->isAnimating(now); });
80     }
81 
setDefaultLayerVote(Layer * layer,LayerHistory::LayerVoteType vote)82     void setDefaultLayerVote(Layer* layer,
83                              LayerHistory::LayerVoteType vote) NO_THREAD_SAFETY_ANALYSIS {
84         for (auto& [layerUnsafe, info] : history().mLayerInfos) {
85             if (layerUnsafe == layer) {
86                 info->setDefaultLayerVote(vote);
87                 return;
88             }
89         }
90     }
91 
createLayer()92     auto createLayer() { return sp<mock::MockLayer>(new mock::MockLayer(mFlinger.flinger())); }
createLayer(std::string name)93     auto createLayer(std::string name) {
94         return sp<mock::MockLayer>(new mock::MockLayer(mFlinger.flinger(), std::move(name)));
95     }
96 
recordFramesAndExpect(const sp<mock::MockLayer> & layer,nsecs_t & time,Fps frameRate,Fps desiredRefreshRate,int numFrames)97     void recordFramesAndExpect(const sp<mock::MockLayer>& layer, nsecs_t& time, Fps frameRate,
98                                Fps desiredRefreshRate, int numFrames) {
99         LayerHistory::Summary summary;
100         for (int i = 0; i < numFrames; i++) {
101             history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
102             time += frameRate.getPeriodNsecs();
103 
104             summary = history().summarize(time);
105         }
106 
107         ASSERT_EQ(1, summary.size());
108         ASSERT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
109         ASSERT_TRUE(desiredRefreshRate.equalsWithMargin(summary[0].desiredRefreshRate))
110                 << "Frame rate is " << frameRate;
111     }
112 
113     RefreshRateConfigs mConfigs{{DisplayMode::Builder(0)
114                                          .setId(DisplayModeId(0))
115                                          .setVsyncPeriod(int32_t(LO_FPS_PERIOD))
116                                          .setGroup(0)
117                                          .build(),
118                                  DisplayMode::Builder(1)
119                                          .setId(DisplayModeId(1))
120                                          .setVsyncPeriod(int32_t(HI_FPS_PERIOD))
121                                          .setGroup(0)
122                                          .build()},
123                                 DisplayModeId(0)};
124 
125     mock::NoOpSchedulerCallback mSchedulerCallback;
126 
127     TestableScheduler* const mScheduler = new TestableScheduler(mConfigs, mSchedulerCallback);
128 
129     TestableSurfaceFlinger mFlinger;
130 };
131 
132 namespace {
133 
TEST_F(LayerHistoryTest,oneLayer)134 TEST_F(LayerHistoryTest, oneLayer) {
135     const auto layer = createLayer();
136     EXPECT_CALL(*layer, isVisible()).WillRepeatedly(Return(true));
137     EXPECT_CALL(*layer, getFrameRateForLayerTree()).WillRepeatedly(Return(Layer::FrameRate()));
138 
139     EXPECT_EQ(1, layerCount());
140     EXPECT_EQ(0, activeLayerCount());
141 
142     const nsecs_t time = systemTime();
143 
144     // No layers returned if no layers are active.
145     EXPECT_TRUE(history().summarize(time).empty());
146     EXPECT_EQ(0, activeLayerCount());
147 
148     // Max returned if active layers have insufficient history.
149     for (int i = 0; i < PRESENT_TIME_HISTORY_SIZE - 1; i++) {
150         history().record(layer.get(), 0, time, LayerHistory::LayerUpdateType::Buffer);
151         ASSERT_EQ(1, history().summarize(time).size());
152         EXPECT_EQ(LayerHistory::LayerVoteType::Max, history().summarize(time)[0].vote);
153         EXPECT_EQ(1, activeLayerCount());
154     }
155 
156     // Max is returned since we have enough history but there is no timestamp votes.
157     for (int i = 0; i < 10; i++) {
158         history().record(layer.get(), 0, time, LayerHistory::LayerUpdateType::Buffer);
159         ASSERT_EQ(1, history().summarize(time).size());
160         EXPECT_EQ(LayerHistory::LayerVoteType::Max, history().summarize(time)[0].vote);
161         EXPECT_EQ(1, activeLayerCount());
162     }
163 }
164 
TEST_F(LayerHistoryTest,oneInvisibleLayer)165 TEST_F(LayerHistoryTest, oneInvisibleLayer) {
166     const auto layer = createLayer();
167     EXPECT_CALL(*layer, isVisible()).WillRepeatedly(Return(true));
168     EXPECT_CALL(*layer, getFrameRateForLayerTree()).WillRepeatedly(Return(Layer::FrameRate()));
169 
170     EXPECT_EQ(1, layerCount());
171     EXPECT_EQ(0, activeLayerCount());
172 
173     nsecs_t time = systemTime();
174 
175     history().record(layer.get(), 0, time, LayerHistory::LayerUpdateType::Buffer);
176     auto summary = history().summarize(time);
177     ASSERT_EQ(1, history().summarize(time).size());
178     // Layer is still considered inactive so we expect to get Min
179     EXPECT_EQ(LayerHistory::LayerVoteType::Max, history().summarize(time)[0].vote);
180     EXPECT_EQ(1, activeLayerCount());
181 
182     EXPECT_CALL(*layer, isVisible()).WillRepeatedly(Return(false));
183     history().record(layer.get(), 0, time, LayerHistory::LayerUpdateType::Buffer);
184 
185     summary = history().summarize(time);
186     EXPECT_TRUE(history().summarize(time).empty());
187     EXPECT_EQ(0, activeLayerCount());
188 }
189 
TEST_F(LayerHistoryTest,explicitTimestamp)190 TEST_F(LayerHistoryTest, explicitTimestamp) {
191     const auto layer = createLayer();
192     EXPECT_CALL(*layer, isVisible()).WillRepeatedly(Return(true));
193     EXPECT_CALL(*layer, getFrameRateForLayerTree()).WillRepeatedly(Return(Layer::FrameRate()));
194 
195     EXPECT_EQ(1, layerCount());
196     EXPECT_EQ(0, activeLayerCount());
197 
198     nsecs_t time = systemTime();
199     for (int i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
200         history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
201         time += LO_FPS_PERIOD;
202     }
203 
204     ASSERT_EQ(1, history().summarize(time).size());
205     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, history().summarize(time)[0].vote);
206     EXPECT_TRUE(LO_FPS.equalsWithMargin(history().summarize(time)[0].desiredRefreshRate));
207     EXPECT_EQ(1, activeLayerCount());
208     EXPECT_EQ(1, frequentLayerCount(time));
209 }
210 
TEST_F(LayerHistoryTest,oneLayerNoVote)211 TEST_F(LayerHistoryTest, oneLayerNoVote) {
212     const auto layer = createLayer();
213     EXPECT_CALL(*layer, isVisible()).WillRepeatedly(Return(true));
214     EXPECT_CALL(*layer, getFrameRateForLayerTree()).WillRepeatedly(Return(Layer::FrameRate()));
215 
216     setDefaultLayerVote(layer.get(), LayerHistory::LayerVoteType::NoVote);
217 
218     EXPECT_EQ(1, layerCount());
219     EXPECT_EQ(0, activeLayerCount());
220 
221     nsecs_t time = systemTime();
222     for (int i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
223         history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
224         time += HI_FPS_PERIOD;
225     }
226 
227     ASSERT_TRUE(history().summarize(time).empty());
228     EXPECT_EQ(1, activeLayerCount());
229     EXPECT_EQ(1, frequentLayerCount(time));
230 
231     // layer became inactive
232     time += MAX_ACTIVE_LAYER_PERIOD_NS.count();
233     ASSERT_TRUE(history().summarize(time).empty());
234     EXPECT_EQ(0, activeLayerCount());
235     EXPECT_EQ(0, frequentLayerCount(time));
236 }
237 
TEST_F(LayerHistoryTest,oneLayerMinVote)238 TEST_F(LayerHistoryTest, oneLayerMinVote) {
239     const auto layer = createLayer();
240     EXPECT_CALL(*layer, isVisible()).WillRepeatedly(Return(true));
241     EXPECT_CALL(*layer, getFrameRateForLayerTree()).WillRepeatedly(Return(Layer::FrameRate()));
242 
243     setDefaultLayerVote(layer.get(), LayerHistory::LayerVoteType::Min);
244 
245     EXPECT_EQ(1, layerCount());
246     EXPECT_EQ(0, activeLayerCount());
247 
248     nsecs_t time = systemTime();
249     for (int i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
250         history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
251         time += HI_FPS_PERIOD;
252     }
253 
254     ASSERT_EQ(1, history().summarize(time).size());
255     EXPECT_EQ(LayerHistory::LayerVoteType::Min, history().summarize(time)[0].vote);
256     EXPECT_EQ(1, activeLayerCount());
257     EXPECT_EQ(1, frequentLayerCount(time));
258 
259     // layer became inactive
260     time += MAX_ACTIVE_LAYER_PERIOD_NS.count();
261     ASSERT_TRUE(history().summarize(time).empty());
262     EXPECT_EQ(0, activeLayerCount());
263     EXPECT_EQ(0, frequentLayerCount(time));
264 }
265 
TEST_F(LayerHistoryTest,oneLayerMaxVote)266 TEST_F(LayerHistoryTest, oneLayerMaxVote) {
267     const auto layer = createLayer();
268     EXPECT_CALL(*layer, isVisible()).WillRepeatedly(Return(true));
269     EXPECT_CALL(*layer, getFrameRateForLayerTree()).WillRepeatedly(Return(Layer::FrameRate()));
270 
271     setDefaultLayerVote(layer.get(), LayerHistory::LayerVoteType::Max);
272 
273     EXPECT_EQ(1, layerCount());
274     EXPECT_EQ(0, activeLayerCount());
275 
276     nsecs_t time = systemTime();
277     for (int i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
278         history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
279         time += LO_FPS_PERIOD;
280     }
281 
282     ASSERT_EQ(1, history().summarize(time).size());
283     EXPECT_EQ(LayerHistory::LayerVoteType::Max, history().summarize(time)[0].vote);
284     EXPECT_EQ(1, activeLayerCount());
285     EXPECT_EQ(1, frequentLayerCount(time));
286 
287     // layer became inactive
288     time += MAX_ACTIVE_LAYER_PERIOD_NS.count();
289     ASSERT_TRUE(history().summarize(time).empty());
290     EXPECT_EQ(0, activeLayerCount());
291     EXPECT_EQ(0, frequentLayerCount(time));
292 }
293 
TEST_F(LayerHistoryTest,oneLayerExplicitVote)294 TEST_F(LayerHistoryTest, oneLayerExplicitVote) {
295     auto layer = createLayer();
296     EXPECT_CALL(*layer, isVisible()).WillRepeatedly(Return(true));
297     EXPECT_CALL(*layer, getFrameRateForLayerTree())
298             .WillRepeatedly(
299                     Return(Layer::FrameRate(Fps(73.4f), Layer::FrameRateCompatibility::Default)));
300 
301     EXPECT_EQ(1, layerCount());
302     EXPECT_EQ(0, activeLayerCount());
303 
304     nsecs_t time = systemTime();
305     for (int i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
306         history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
307         time += HI_FPS_PERIOD;
308     }
309 
310     ASSERT_EQ(1, history().summarize(time).size());
311     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitDefault, history().summarize(time)[0].vote);
312     EXPECT_TRUE(Fps(73.4f).equalsWithMargin(history().summarize(time)[0].desiredRefreshRate));
313     EXPECT_EQ(1, activeLayerCount());
314     EXPECT_EQ(1, frequentLayerCount(time));
315 
316     // layer became inactive, but the vote stays
317     setDefaultLayerVote(layer.get(), LayerHistory::LayerVoteType::Heuristic);
318     time += MAX_ACTIVE_LAYER_PERIOD_NS.count();
319     ASSERT_EQ(1, history().summarize(time).size());
320     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitDefault, history().summarize(time)[0].vote);
321     EXPECT_TRUE(Fps(73.4f).equalsWithMargin(history().summarize(time)[0].desiredRefreshRate));
322     EXPECT_EQ(1, activeLayerCount());
323     EXPECT_EQ(0, frequentLayerCount(time));
324 }
325 
TEST_F(LayerHistoryTest,oneLayerExplicitExactVote)326 TEST_F(LayerHistoryTest, oneLayerExplicitExactVote) {
327     auto layer = createLayer();
328     EXPECT_CALL(*layer, isVisible()).WillRepeatedly(Return(true));
329     EXPECT_CALL(*layer, getFrameRateForLayerTree())
330             .WillRepeatedly(Return(
331                     Layer::FrameRate(Fps(73.4f), Layer::FrameRateCompatibility::ExactOrMultiple)));
332 
333     EXPECT_EQ(1, layerCount());
334     EXPECT_EQ(0, activeLayerCount());
335 
336     nsecs_t time = systemTime();
337     for (int i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
338         history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
339         time += HI_FPS_PERIOD;
340     }
341 
342     ASSERT_EQ(1, history().summarize(time).size());
343     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitExactOrMultiple,
344               history().summarize(time)[0].vote);
345     EXPECT_TRUE(Fps(73.4f).equalsWithMargin(history().summarize(time)[0].desiredRefreshRate));
346     EXPECT_EQ(1, activeLayerCount());
347     EXPECT_EQ(1, frequentLayerCount(time));
348 
349     // layer became inactive, but the vote stays
350     setDefaultLayerVote(layer.get(), LayerHistory::LayerVoteType::Heuristic);
351     time += MAX_ACTIVE_LAYER_PERIOD_NS.count();
352     ASSERT_EQ(1, history().summarize(time).size());
353     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitExactOrMultiple,
354               history().summarize(time)[0].vote);
355     EXPECT_TRUE(Fps(73.4f).equalsWithMargin(history().summarize(time)[0].desiredRefreshRate));
356     EXPECT_EQ(1, activeLayerCount());
357     EXPECT_EQ(0, frequentLayerCount(time));
358 }
359 
TEST_F(LayerHistoryTest,multipleLayers)360 TEST_F(LayerHistoryTest, multipleLayers) {
361     auto layer1 = createLayer();
362     auto layer2 = createLayer();
363     auto layer3 = createLayer();
364 
365     EXPECT_CALL(*layer1, isVisible()).WillRepeatedly(Return(true));
366     EXPECT_CALL(*layer1, getFrameRateForLayerTree()).WillRepeatedly(Return(Layer::FrameRate()));
367 
368     EXPECT_CALL(*layer2, isVisible()).WillRepeatedly(Return(true));
369     EXPECT_CALL(*layer2, getFrameRateForLayerTree()).WillRepeatedly(Return(Layer::FrameRate()));
370 
371     EXPECT_CALL(*layer3, isVisible()).WillRepeatedly(Return(true));
372     EXPECT_CALL(*layer3, getFrameRateForLayerTree()).WillRepeatedly(Return(Layer::FrameRate()));
373 
374     nsecs_t time = systemTime();
375 
376     EXPECT_EQ(3, layerCount());
377     EXPECT_EQ(0, activeLayerCount());
378     EXPECT_EQ(0, frequentLayerCount(time));
379 
380     LayerHistory::Summary summary;
381 
382     // layer1 is active but infrequent.
383     for (int i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
384         history().record(layer1.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
385         time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
386         summary = history().summarize(time);
387     }
388 
389     ASSERT_EQ(1, summary.size());
390     EXPECT_EQ(LayerHistory::LayerVoteType::Min, summary[0].vote);
391     EXPECT_EQ(1, activeLayerCount());
392     EXPECT_EQ(0, frequentLayerCount(time));
393 
394     // layer2 is frequent and has high refresh rate.
395     for (int i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
396         history().record(layer2.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
397         time += HI_FPS_PERIOD;
398         summary = history().summarize(time);
399     }
400 
401     // layer1 is still active but infrequent.
402     history().record(layer1.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
403 
404     ASSERT_EQ(2, summary.size());
405     EXPECT_EQ(LayerHistory::LayerVoteType::Min, summary[0].vote);
406     ASSERT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[1].vote);
407     EXPECT_TRUE(HI_FPS.equalsWithMargin(history().summarize(time)[1].desiredRefreshRate));
408 
409     EXPECT_EQ(2, activeLayerCount());
410     EXPECT_EQ(1, frequentLayerCount(time));
411 
412     // layer1 is no longer active.
413     // layer2 is frequent and has low refresh rate.
414     for (int i = 0; i < 2 * PRESENT_TIME_HISTORY_SIZE; i++) {
415         history().record(layer2.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
416         time += LO_FPS_PERIOD;
417         summary = history().summarize(time);
418     }
419 
420     ASSERT_EQ(1, summary.size());
421     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
422     EXPECT_TRUE(LO_FPS.equalsWithMargin(summary[0].desiredRefreshRate));
423     EXPECT_EQ(1, activeLayerCount());
424     EXPECT_EQ(1, frequentLayerCount(time));
425 
426     // layer2 still has low refresh rate.
427     // layer3 has high refresh rate but not enough history.
428     constexpr int RATIO = LO_FPS_PERIOD / HI_FPS_PERIOD;
429     for (int i = 0; i < PRESENT_TIME_HISTORY_SIZE - 1; i++) {
430         if (i % RATIO == 0) {
431             history().record(layer2.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
432         }
433 
434         history().record(layer3.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
435         time += HI_FPS_PERIOD;
436         summary = history().summarize(time);
437     }
438 
439     ASSERT_EQ(2, summary.size());
440     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
441     EXPECT_TRUE(LO_FPS.equalsWithMargin(summary[0].desiredRefreshRate));
442     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summary[1].vote);
443     EXPECT_EQ(2, activeLayerCount());
444     EXPECT_EQ(2, frequentLayerCount(time));
445 
446     // layer3 becomes recently active.
447     history().record(layer3.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
448     summary = history().summarize(time);
449     ASSERT_EQ(2, summary.size());
450     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
451     EXPECT_TRUE(LO_FPS.equalsWithMargin(summary[0].desiredRefreshRate));
452     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[1].vote);
453     EXPECT_TRUE(HI_FPS.equalsWithMargin(summary[1].desiredRefreshRate));
454     EXPECT_EQ(2, activeLayerCount());
455     EXPECT_EQ(2, frequentLayerCount(time));
456 
457     // layer1 expires.
458     layer1.clear();
459     summary = history().summarize(time);
460     ASSERT_EQ(2, summary.size());
461     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
462     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
463     EXPECT_TRUE(LO_FPS.equalsWithMargin(summary[0].desiredRefreshRate));
464     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[1].vote);
465     EXPECT_TRUE(HI_FPS.equalsWithMargin(summary[1].desiredRefreshRate));
466     EXPECT_EQ(2, layerCount());
467     EXPECT_EQ(2, activeLayerCount());
468     EXPECT_EQ(2, frequentLayerCount(time));
469 
470     // layer2 still has low refresh rate.
471     // layer3 becomes inactive.
472     for (int i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
473         history().record(layer2.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
474         time += LO_FPS_PERIOD;
475         summary = history().summarize(time);
476     }
477 
478     ASSERT_EQ(1, summary.size());
479     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
480     EXPECT_TRUE(LO_FPS.equalsWithMargin(summary[0].desiredRefreshRate));
481     EXPECT_EQ(1, activeLayerCount());
482     EXPECT_EQ(1, frequentLayerCount(time));
483 
484     // layer2 expires.
485     layer2.clear();
486     summary = history().summarize(time);
487     EXPECT_TRUE(summary.empty());
488     EXPECT_EQ(1, layerCount());
489     EXPECT_EQ(0, activeLayerCount());
490     EXPECT_EQ(0, frequentLayerCount(time));
491 
492     // layer3 becomes active and has high refresh rate.
493     for (int i = 0; i < PRESENT_TIME_HISTORY_SIZE + FREQUENT_LAYER_WINDOW_SIZE + 1; i++) {
494         history().record(layer3.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
495         time += HI_FPS_PERIOD;
496         summary = history().summarize(time);
497     }
498 
499     ASSERT_EQ(1, summary.size());
500     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
501     EXPECT_TRUE(HI_FPS.equalsWithMargin(summary[0].desiredRefreshRate));
502     EXPECT_EQ(1, layerCount());
503     EXPECT_EQ(1, activeLayerCount());
504     EXPECT_EQ(1, frequentLayerCount(time));
505 
506     // layer3 expires.
507     layer3.clear();
508     summary = history().summarize(time);
509     EXPECT_TRUE(summary.empty());
510     EXPECT_EQ(0, layerCount());
511     EXPECT_EQ(0, activeLayerCount());
512     EXPECT_EQ(0, frequentLayerCount(time));
513 }
514 
TEST_F(LayerHistoryTest,inactiveLayers)515 TEST_F(LayerHistoryTest, inactiveLayers) {
516     auto layer = createLayer();
517 
518     EXPECT_CALL(*layer, isVisible()).WillRepeatedly(Return(true));
519     EXPECT_CALL(*layer, getFrameRateForLayerTree()).WillRepeatedly(Return(Layer::FrameRate()));
520 
521     nsecs_t time = systemTime();
522 
523     // the very first updates makes the layer frequent
524     for (int i = 0; i < FREQUENT_LAYER_WINDOW_SIZE - 1; i++) {
525         history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
526         time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
527 
528         EXPECT_EQ(1, layerCount());
529         ASSERT_EQ(1, history().summarize(time).size());
530         EXPECT_EQ(LayerHistory::LayerVoteType::Max, history().summarize(time)[0].vote);
531         EXPECT_EQ(1, activeLayerCount());
532         EXPECT_EQ(1, frequentLayerCount(time));
533     }
534 
535     // the next update with the MAX_FREQUENT_LAYER_PERIOD_NS will get us to infrequent
536     history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
537     time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
538 
539     EXPECT_EQ(1, layerCount());
540     ASSERT_EQ(1, history().summarize(time).size());
541     EXPECT_EQ(LayerHistory::LayerVoteType::Min, history().summarize(time)[0].vote);
542     EXPECT_EQ(1, activeLayerCount());
543     EXPECT_EQ(0, frequentLayerCount(time));
544 
545     // advance the time for the previous frame to be inactive
546     time += MAX_ACTIVE_LAYER_PERIOD_NS.count();
547 
548     // Now event if we post a quick few frame we should stay infrequent
549     for (int i = 0; i < FREQUENT_LAYER_WINDOW_SIZE - 1; i++) {
550         history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
551         time += HI_FPS_PERIOD;
552 
553         EXPECT_EQ(1, layerCount());
554         ASSERT_EQ(1, history().summarize(time).size());
555         EXPECT_EQ(LayerHistory::LayerVoteType::Min, history().summarize(time)[0].vote);
556         EXPECT_EQ(1, activeLayerCount());
557         EXPECT_EQ(0, frequentLayerCount(time));
558     }
559 
560     // More quick frames will get us to frequent again
561     history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
562     time += HI_FPS_PERIOD;
563 
564     EXPECT_EQ(1, layerCount());
565     ASSERT_EQ(1, history().summarize(time).size());
566     EXPECT_EQ(LayerHistory::LayerVoteType::Max, history().summarize(time)[0].vote);
567     EXPECT_EQ(1, activeLayerCount());
568     EXPECT_EQ(1, frequentLayerCount(time));
569 }
570 
TEST_F(LayerHistoryTest,invisibleExplicitLayer)571 TEST_F(LayerHistoryTest, invisibleExplicitLayer) {
572     auto explicitVisiblelayer = createLayer();
573     auto explicitInvisiblelayer = createLayer();
574 
575     EXPECT_CALL(*explicitVisiblelayer, isVisible()).WillRepeatedly(Return(true));
576     EXPECT_CALL(*explicitVisiblelayer, getFrameRateForLayerTree())
577             .WillRepeatedly(Return(
578                     Layer::FrameRate(Fps(60.0f), Layer::FrameRateCompatibility::ExactOrMultiple)));
579 
580     EXPECT_CALL(*explicitInvisiblelayer, isVisible()).WillRepeatedly(Return(false));
581     EXPECT_CALL(*explicitInvisiblelayer, getFrameRateForLayerTree())
582             .WillRepeatedly(Return(
583                     Layer::FrameRate(Fps(90.0f), Layer::FrameRateCompatibility::ExactOrMultiple)));
584 
585     nsecs_t time = systemTime();
586 
587     // Post a buffer to the layers to make them active
588     history().record(explicitVisiblelayer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
589     history().record(explicitInvisiblelayer.get(), time, time,
590                      LayerHistory::LayerUpdateType::Buffer);
591 
592     EXPECT_EQ(2, layerCount());
593     ASSERT_EQ(1, history().summarize(time).size());
594     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitExactOrMultiple,
595               history().summarize(time)[0].vote);
596     EXPECT_TRUE(Fps(60.0f).equalsWithMargin(history().summarize(time)[0].desiredRefreshRate));
597     EXPECT_EQ(2, activeLayerCount());
598     EXPECT_EQ(2, frequentLayerCount(time));
599 }
600 
TEST_F(LayerHistoryTest,infrequentAnimatingLayer)601 TEST_F(LayerHistoryTest, infrequentAnimatingLayer) {
602     auto layer = createLayer();
603 
604     EXPECT_CALL(*layer, isVisible()).WillRepeatedly(Return(true));
605     EXPECT_CALL(*layer, getFrameRateForLayerTree()).WillRepeatedly(Return(Layer::FrameRate()));
606 
607     nsecs_t time = systemTime();
608 
609     EXPECT_EQ(1, layerCount());
610     EXPECT_EQ(0, activeLayerCount());
611     EXPECT_EQ(0, frequentLayerCount(time));
612     EXPECT_EQ(0, animatingLayerCount(time));
613 
614     // layer is active but infrequent.
615     for (int i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
616         history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
617         time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
618     }
619 
620     ASSERT_EQ(1, history().summarize(time).size());
621     EXPECT_EQ(LayerHistory::LayerVoteType::Min, history().summarize(time)[0].vote);
622     EXPECT_EQ(1, activeLayerCount());
623     EXPECT_EQ(0, frequentLayerCount(time));
624     EXPECT_EQ(0, animatingLayerCount(time));
625 
626     // another update with the same cadence keep in infrequent
627     history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
628     time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
629 
630     ASSERT_EQ(1, history().summarize(time).size());
631     EXPECT_EQ(LayerHistory::LayerVoteType::Min, history().summarize(time)[0].vote);
632     EXPECT_EQ(1, activeLayerCount());
633     EXPECT_EQ(0, frequentLayerCount(time));
634     EXPECT_EQ(0, animatingLayerCount(time));
635 
636     // an update as animation will immediately vote for Max
637     history().record(layer.get(), time, time, LayerHistory::LayerUpdateType::AnimationTX);
638     time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
639 
640     ASSERT_EQ(1, history().summarize(time).size());
641     EXPECT_EQ(LayerHistory::LayerVoteType::Max, history().summarize(time)[0].vote);
642     EXPECT_EQ(1, activeLayerCount());
643     EXPECT_EQ(0, frequentLayerCount(time));
644     EXPECT_EQ(1, animatingLayerCount(time));
645 }
646 
TEST_F(LayerHistoryTest,heuristicLayer60Hz)647 TEST_F(LayerHistoryTest, heuristicLayer60Hz) {
648     const auto layer = createLayer();
649     EXPECT_CALL(*layer, isVisible()).WillRepeatedly(Return(true));
650     EXPECT_CALL(*layer, getFrameRateForLayerTree()).WillRepeatedly(Return(Layer::FrameRate()));
651 
652     nsecs_t time = systemTime();
653     for (float fps = 54.0f; fps < 65.0f; fps += 0.1f) {
654         recordFramesAndExpect(layer, time, Fps(fps), Fps(60.0f), PRESENT_TIME_HISTORY_SIZE);
655     }
656 }
657 
TEST_F(LayerHistoryTest,heuristicLayer60_30Hz)658 TEST_F(LayerHistoryTest, heuristicLayer60_30Hz) {
659     const auto layer = createLayer();
660     EXPECT_CALL(*layer, isVisible()).WillRepeatedly(Return(true));
661     EXPECT_CALL(*layer, getFrameRateForLayerTree()).WillRepeatedly(Return(Layer::FrameRate()));
662 
663     nsecs_t time = systemTime();
664     recordFramesAndExpect(layer, time, Fps(60.0f), Fps(60.0f), PRESENT_TIME_HISTORY_SIZE);
665 
666     recordFramesAndExpect(layer, time, Fps(60.0f), Fps(60.0f), PRESENT_TIME_HISTORY_SIZE);
667     recordFramesAndExpect(layer, time, Fps(30.0f), Fps(60.0f), PRESENT_TIME_HISTORY_SIZE);
668     recordFramesAndExpect(layer, time, Fps(30.0f), Fps(30.0f), PRESENT_TIME_HISTORY_SIZE);
669     recordFramesAndExpect(layer, time, Fps(60.0f), Fps(30.0f), PRESENT_TIME_HISTORY_SIZE);
670     recordFramesAndExpect(layer, time, Fps(60.0f), Fps(60.0f), PRESENT_TIME_HISTORY_SIZE);
671 }
672 
TEST_F(LayerHistoryTest,heuristicLayerNotOscillating)673 TEST_F(LayerHistoryTest, heuristicLayerNotOscillating) {
674     const auto layer = createLayer();
675     EXPECT_CALL(*layer, isVisible()).WillRepeatedly(Return(true));
676     EXPECT_CALL(*layer, getFrameRateForLayerTree()).WillRepeatedly(Return(Layer::FrameRate()));
677 
678     nsecs_t time = systemTime();
679 
680     recordFramesAndExpect(layer, time, Fps(27.10f), Fps(30.0f), PRESENT_TIME_HISTORY_SIZE);
681     recordFramesAndExpect(layer, time, Fps(26.90f), Fps(30.0f), PRESENT_TIME_HISTORY_SIZE);
682     recordFramesAndExpect(layer, time, Fps(26.00f), Fps(24.0f), PRESENT_TIME_HISTORY_SIZE);
683     recordFramesAndExpect(layer, time, Fps(26.90f), Fps(24.0f), PRESENT_TIME_HISTORY_SIZE);
684     recordFramesAndExpect(layer, time, Fps(27.10f), Fps(30.0f), PRESENT_TIME_HISTORY_SIZE);
685 }
686 
687 class LayerHistoryTestParameterized : public LayerHistoryTest,
688                                       public testing::WithParamInterface<std::chrono::nanoseconds> {
689 };
690 
TEST_P(LayerHistoryTestParameterized,HeuristicLayerWithInfrequentLayer)691 TEST_P(LayerHistoryTestParameterized, HeuristicLayerWithInfrequentLayer) {
692     std::chrono::nanoseconds infrequentUpdateDelta = GetParam();
693     auto heuristicLayer = createLayer("HeuristicLayer");
694 
695     EXPECT_CALL(*heuristicLayer, isVisible()).WillRepeatedly(Return(true));
696     EXPECT_CALL(*heuristicLayer, getFrameRateForLayerTree())
697             .WillRepeatedly(Return(Layer::FrameRate()));
698 
699     auto infrequentLayer = createLayer("InfrequentLayer");
700     EXPECT_CALL(*infrequentLayer, isVisible()).WillRepeatedly(Return(true));
701     EXPECT_CALL(*infrequentLayer, getFrameRateForLayerTree())
702             .WillRepeatedly(Return(Layer::FrameRate()));
703 
704     const nsecs_t startTime = systemTime();
705 
706     const std::chrono::nanoseconds heuristicUpdateDelta = 41'666'667ns;
707     history().record(heuristicLayer.get(), startTime, startTime,
708                      LayerHistory::LayerUpdateType::Buffer);
709     history().record(infrequentLayer.get(), startTime, startTime,
710                      LayerHistory::LayerUpdateType::Buffer);
711 
712     nsecs_t time = startTime;
713     nsecs_t lastInfrequentUpdate = startTime;
714     const int totalInfrequentLayerUpdates = FREQUENT_LAYER_WINDOW_SIZE * 5;
715     int infrequentLayerUpdates = 0;
716     while (infrequentLayerUpdates <= totalInfrequentLayerUpdates) {
717         time += heuristicUpdateDelta.count();
718         history().record(heuristicLayer.get(), time, time, LayerHistory::LayerUpdateType::Buffer);
719 
720         if (time - lastInfrequentUpdate >= infrequentUpdateDelta.count()) {
721             ALOGI("submitting infrequent frame [%d/%d]", infrequentLayerUpdates,
722                   totalInfrequentLayerUpdates);
723             lastInfrequentUpdate = time;
724             history().record(infrequentLayer.get(), time, time,
725                              LayerHistory::LayerUpdateType::Buffer);
726             infrequentLayerUpdates++;
727         }
728 
729         if (time - startTime > PRESENT_TIME_HISTORY_DURATION.count()) {
730             ASSERT_NE(0, history().summarize(time).size());
731             ASSERT_GE(2, history().summarize(time).size());
732 
733             bool max = false;
734             bool min = false;
735             Fps heuristic{0.0};
736             for (const auto& layer : history().summarize(time)) {
737                 if (layer.vote == LayerHistory::LayerVoteType::Heuristic) {
738                     heuristic = layer.desiredRefreshRate;
739                 } else if (layer.vote == LayerHistory::LayerVoteType::Max) {
740                     max = true;
741                 } else if (layer.vote == LayerHistory::LayerVoteType::Min) {
742                     min = true;
743                 }
744             }
745 
746             if (infrequentLayerUpdates > FREQUENT_LAYER_WINDOW_SIZE) {
747                 EXPECT_TRUE(Fps(24.0f).equalsWithMargin(heuristic));
748                 EXPECT_FALSE(max);
749                 if (history().summarize(time).size() == 2) {
750                     EXPECT_TRUE(min);
751                 }
752             }
753         }
754     }
755 }
756 
757 INSTANTIATE_TEST_CASE_P(LeapYearTests, LayerHistoryTestParameterized,
758                         ::testing::Values(1s, 2s, 3s, 4s, 5s));
759 
760 } // namespace
761 } // namespace scheduler
762 } // namespace android
763 
764 // TODO(b/129481165): remove the #pragma below and fix conversion issues
765 #pragma clang diagnostic pop // ignored "-Wextra"