1 /*
2  * Copyright 2023 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 "LayerHistoryIntegrationTest"
19 
20 #include <Layer.h>
21 #include <com_android_graphics_surfaceflinger_flags.h>
22 #include <gmock/gmock.h>
23 #include <gtest/gtest.h>
24 #include <log/log.h>
25 
26 #include <renderengine/mock/FakeExternalTexture.h>
27 
28 #include <common/test/FlagUtils.h>
29 #include "FpsOps.h"
30 #include "LayerHierarchyTest.h"
31 #include "Scheduler/LayerHistory.h"
32 #include "Scheduler/LayerInfo.h"
33 #include "TestableScheduler.h"
34 #include "TestableSurfaceFlinger.h"
35 #include "mock/DisplayHardware/MockDisplayMode.h"
36 #include "mock/MockSchedulerCallback.h"
37 
38 namespace android::scheduler {
39 
40 using android::mock::createDisplayMode;
41 using namespace com::android::graphics::surfaceflinger;
42 
43 class LayerHistoryIntegrationTest : public surfaceflinger::frontend::LayerSnapshotTestBase {
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 
50     static constexpr Fps LO_FPS = 30_Hz;
51     static constexpr auto LO_FPS_PERIOD = LO_FPS.getPeriodNsecs();
52 
53     static constexpr Fps HI_FPS = 90_Hz;
54     static constexpr auto HI_FPS_PERIOD = HI_FPS.getPeriodNsecs();
55 
LayerHistoryIntegrationTest()56     LayerHistoryIntegrationTest() : LayerSnapshotTestBase() {
57         mFlinger.resetScheduler(mScheduler);
58         mLifecycleManager = {};
59         mHierarchyBuilder = {};
60     }
61 
updateLayerSnapshotsAndLayerHistory(nsecs_t now)62     void updateLayerSnapshotsAndLayerHistory(nsecs_t now) {
63         LayerSnapshotTestBase::update(mFlinger.mutableLayerSnapshotBuilder());
64         mFlinger.updateLayerHistory(now);
65     }
66 
setBufferWithPresentTime(sp<Layer> & layer,nsecs_t time)67     void setBufferWithPresentTime(sp<Layer>& layer, nsecs_t time) {
68         uint32_t sequence = static_cast<uint32_t>(layer->sequence);
69         setBuffer(sequence);
70         layer->setDesiredPresentTime(time, false /*autotimestamp*/);
71         updateLayerSnapshotsAndLayerHistory(time);
72     }
73 
history()74     LayerHistory& history() { return mScheduler->mutableLayerHistory(); }
history() const75     const LayerHistory& history() const { return mScheduler->mutableLayerHistory(); }
76 
summarizeLayerHistory(nsecs_t now)77     LayerHistory::Summary summarizeLayerHistory(nsecs_t now) {
78         // LayerHistory::summarize makes no guarantee of the order of the elements in the summary
79         // however, for testing only, a stable order is required, therefore we sort the list here.
80         // Any tests requiring ordered results must create layers with names.
81         auto summary = history().summarize(*mScheduler->refreshRateSelector(), now);
82         std::sort(summary.begin(), summary.end(),
83                   [](const RefreshRateSelector::LayerRequirement& lhs,
84                      const RefreshRateSelector::LayerRequirement& rhs) -> bool {
85                       return lhs.name < rhs.name;
86                   });
87         return summary;
88     }
89 
layerCount() const90     size_t layerCount() const { return mScheduler->layerHistorySize(); }
activeLayerCount() const91     size_t activeLayerCount() const NO_THREAD_SAFETY_ANALYSIS {
92         return history().mActiveLayerInfos.size();
93     }
94 
frequentLayerCount(nsecs_t now) const95     auto frequentLayerCount(nsecs_t now) const NO_THREAD_SAFETY_ANALYSIS {
96         const auto& infos = history().mActiveLayerInfos;
97         return std::count_if(infos.begin(), infos.end(), [now](const auto& pair) {
98             return pair.second.second->isFrequent(now).isFrequent;
99         });
100     }
101 
animatingLayerCount(nsecs_t now) const102     auto animatingLayerCount(nsecs_t now) const NO_THREAD_SAFETY_ANALYSIS {
103         const auto& infos = history().mActiveLayerInfos;
104         return std::count_if(infos.begin(), infos.end(), [now](const auto& pair) {
105             return pair.second.second->isAnimating(now);
106         });
107     }
108 
clearLayerHistoryCount(nsecs_t now) const109     auto clearLayerHistoryCount(nsecs_t now) const NO_THREAD_SAFETY_ANALYSIS {
110         const auto& infos = history().mActiveLayerInfos;
111         return std::count_if(infos.begin(), infos.end(), [now](const auto& pair) {
112             return pair.second.second->isFrequent(now).clearHistory;
113         });
114     }
115 
setDefaultLayerVote(Layer * layer,LayerHistory::LayerVoteType vote)116     void setDefaultLayerVote(Layer* layer,
117                              LayerHistory::LayerVoteType vote) NO_THREAD_SAFETY_ANALYSIS {
118         auto [found, layerPair] = history().findLayer(layer->getSequence());
119         if (found != LayerHistory::LayerStatus::NotFound) {
120             layerPair->second->setDefaultLayerVote(vote);
121         }
122     }
123 
createLegacyAndFrontedEndLayer(uint32_t sequence)124     auto createLegacyAndFrontedEndLayer(uint32_t sequence) {
125         std::string layerName = "test layer:" + std::to_string(sequence);
126         const auto layer =
127                 sp<Layer>::make(LayerCreationArgs{mFlinger.flinger(),
128                                                   nullptr,
129                                                   layerName,
130                                                   0,
131                                                   {},
132                                                   std::make_optional<uint32_t>(sequence)});
133         mFlinger.injectLegacyLayer(layer);
134         createRootLayer(sequence);
135         return layer;
136     }
137 
destroyLayer(sp<Layer> & layer)138     auto destroyLayer(sp<Layer>& layer) {
139         uint32_t sequence = static_cast<uint32_t>(layer->sequence);
140         mFlinger.releaseLegacyLayer(sequence);
141         layer.clear();
142         destroyLayerHandle(sequence);
143     }
144 
recordFramesAndExpect(sp<Layer> & layer,nsecs_t & time,Fps frameRate,Fps desiredRefreshRate,int numFrames)145     void recordFramesAndExpect(sp<Layer>& layer, nsecs_t& time, Fps frameRate,
146                                Fps desiredRefreshRate, int numFrames) {
147         LayerHistory::Summary summary;
148         for (int i = 0; i < numFrames; i++) {
149             setBufferWithPresentTime(layer, time);
150             time += frameRate.getPeriodNsecs();
151 
152             summary = summarizeLayerHistory(time);
153         }
154 
155         ASSERT_EQ(1u, summary.size());
156         ASSERT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
157         ASSERT_EQ(desiredRefreshRate, summary[0].desiredRefreshRate);
158     }
159 
160     std::shared_ptr<RefreshRateSelector> mSelector =
161             std::make_shared<RefreshRateSelector>(makeModes(createDisplayMode(DisplayModeId(0),
162                                                                               LO_FPS),
163                                                             createDisplayMode(DisplayModeId(1),
164                                                                               HI_FPS)),
165                                                   DisplayModeId(0));
166 
167     mock::SchedulerCallback mSchedulerCallback;
168     TestableSurfaceFlinger mFlinger;
169     TestableScheduler* mScheduler = new TestableScheduler(mSelector, mFlinger, mSchedulerCallback);
170 };
171 
172 namespace {
173 
TEST_F(LayerHistoryIntegrationTest,singleLayerNoVoteDefaultCompatibility)174 TEST_F(LayerHistoryIntegrationTest, singleLayerNoVoteDefaultCompatibility) {
175     createLegacyAndFrontedEndLayer(1);
176     nsecs_t time = systemTime();
177 
178     updateLayerSnapshotsAndLayerHistory(time);
179 
180     EXPECT_EQ(1u, layerCount());
181     EXPECT_EQ(0u, activeLayerCount());
182 
183     // No layers returned if no layers are active.
184     EXPECT_TRUE(summarizeLayerHistory(time).empty());
185     EXPECT_EQ(0u, activeLayerCount());
186 
187     setBuffer(1);
188     setDefaultFrameRateCompatibility(1, ANATIVEWINDOW_FRAME_RATE_NO_VOTE);
189     updateLayerSnapshotsAndLayerHistory(time);
190 
191     EXPECT_TRUE(summarizeLayerHistory(time).empty());
192     EXPECT_EQ(1u, activeLayerCount());
193 }
194 
TEST_F(LayerHistoryIntegrationTest,singleLayerMinVoteDefaultCompatibility)195 TEST_F(LayerHistoryIntegrationTest, singleLayerMinVoteDefaultCompatibility) {
196     createLegacyAndFrontedEndLayer(1);
197     nsecs_t time = systemTime();
198     updateLayerSnapshotsAndLayerHistory(time);
199 
200     EXPECT_EQ(1u, layerCount());
201     EXPECT_EQ(0u, activeLayerCount());
202 
203     EXPECT_TRUE(summarizeLayerHistory(time).empty());
204     EXPECT_EQ(0u, activeLayerCount());
205 
206     setBuffer(1);
207     setDefaultFrameRateCompatibility(1, ANATIVEWINDOW_FRAME_RATE_MIN);
208     updateLayerSnapshotsAndLayerHistory(time);
209 
210     auto summary = summarizeLayerHistory(time);
211     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
212 
213     EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
214     EXPECT_EQ(1u, activeLayerCount());
215 }
216 
TEST_F(LayerHistoryIntegrationTest,oneInvisibleLayer)217 TEST_F(LayerHistoryIntegrationTest, oneInvisibleLayer) {
218     createLegacyAndFrontedEndLayer(1);
219     nsecs_t time = systemTime();
220     updateLayerSnapshotsAndLayerHistory(time);
221     EXPECT_EQ(1u, layerCount());
222     EXPECT_EQ(0u, activeLayerCount());
223 
224     setBuffer(1);
225     updateLayerSnapshotsAndLayerHistory(time);
226     auto summary = summarizeLayerHistory(time);
227     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
228     // Layer is still considered inactive so we expect to get Min
229     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
230     EXPECT_EQ(1u, activeLayerCount());
231 
232     hideLayer(1);
233     setBuffer(1);
234     updateLayerSnapshotsAndLayerHistory(time);
235 
236     summary = summarizeLayerHistory(time);
237     EXPECT_TRUE(summarizeLayerHistory(time).empty());
238     EXPECT_EQ(0u, activeLayerCount());
239 }
240 
TEST_F(LayerHistoryIntegrationTest,oneLayerExplicitVote)241 TEST_F(LayerHistoryIntegrationTest, oneLayerExplicitVote) {
242     createLegacyAndFrontedEndLayer(1);
243     setFrameRate(1, 73.4f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT,
244                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
245 
246     EXPECT_EQ(1u, layerCount());
247     EXPECT_EQ(0u, activeLayerCount());
248 
249     nsecs_t time = systemTime();
250     updateLayerSnapshotsAndLayerHistory(time);
251     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
252     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitDefault, summarizeLayerHistory(time)[0].vote);
253     EXPECT_EQ(73.4_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
254     EXPECT_EQ(1u, activeLayerCount());
255     EXPECT_EQ(1, frequentLayerCount(time));
256 }
257 
TEST_F(LayerHistoryIntegrationTest,oneLayerExplicitExactVote)258 TEST_F(LayerHistoryIntegrationTest, oneLayerExplicitExactVote) {
259     createLegacyAndFrontedEndLayer(1);
260     setFrameRate(1, 73.4f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,
261                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
262 
263     EXPECT_EQ(1u, layerCount());
264     EXPECT_EQ(0u, activeLayerCount());
265 
266     nsecs_t time = systemTime();
267     updateLayerSnapshotsAndLayerHistory(time);
268     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
269     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitExactOrMultiple,
270               summarizeLayerHistory(time)[0].vote);
271     EXPECT_EQ(73.4_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
272     EXPECT_EQ(1u, activeLayerCount());
273     EXPECT_EQ(1, frequentLayerCount(time));
274 }
275 
TEST_F(LayerHistoryIntegrationTest,oneLayerExplicitCategory)276 TEST_F(LayerHistoryIntegrationTest, oneLayerExplicitCategory) {
277     SET_FLAG_FOR_TEST(flags::frame_rate_category_mrr, true);
278 
279     createLegacyAndFrontedEndLayer(1);
280     setFrameRateCategory(1, ANATIVEWINDOW_FRAME_RATE_CATEGORY_HIGH);
281 
282     EXPECT_EQ(1u, layerCount());
283     EXPECT_EQ(0u, activeLayerCount());
284 
285     nsecs_t time = systemTime();
286     updateLayerSnapshotsAndLayerHistory(time);
287     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
288     EXPECT_EQ(1u, activeLayerCount());
289     EXPECT_EQ(1, frequentLayerCount(time));
290     // First LayerRequirement is the frame rate specification
291     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitCategory, summarizeLayerHistory(time)[0].vote);
292     EXPECT_EQ(0_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
293     EXPECT_EQ(FrameRateCategory::High, summarizeLayerHistory(time)[0].frameRateCategory);
294 }
295 
TEST_F(LayerHistoryIntegrationTest,multipleLayers)296 TEST_F(LayerHistoryIntegrationTest, multipleLayers) {
297     auto layer1 = createLegacyAndFrontedEndLayer(1);
298     auto layer2 = createLegacyAndFrontedEndLayer(2);
299     auto layer3 = createLegacyAndFrontedEndLayer(3);
300 
301     nsecs_t time = systemTime();
302 
303     EXPECT_EQ(3u, layerCount());
304     EXPECT_EQ(0u, activeLayerCount());
305     EXPECT_EQ(0, frequentLayerCount(time));
306 
307     LayerHistory::Summary summary;
308 
309     // layer1 is active but infrequent.
310     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
311         setBufferWithPresentTime(layer1, time);
312         time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
313         summary = summarizeLayerHistory(time);
314     }
315 
316     ASSERT_EQ(1u, summary.size());
317     EXPECT_EQ(LayerHistory::LayerVoteType::Min, summary[0].vote);
318     EXPECT_EQ(1u, activeLayerCount());
319     EXPECT_EQ(0, frequentLayerCount(time));
320 
321     // layer2 is frequent and has high refresh rate.
322     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
323         setBufferWithPresentTime(layer2, time);
324         time += HI_FPS_PERIOD;
325         summary = summarizeLayerHistory(time);
326     }
327 
328     // layer1 is still active but infrequent.
329     setBufferWithPresentTime(layer1, time);
330 
331     ASSERT_EQ(2u, summary.size());
332     EXPECT_EQ(LayerHistory::LayerVoteType::Min, summary[0].vote);
333     ASSERT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[1].vote);
334     EXPECT_EQ(HI_FPS, summarizeLayerHistory(time)[1].desiredRefreshRate);
335 
336     EXPECT_EQ(2u, activeLayerCount());
337     EXPECT_EQ(1, frequentLayerCount(time));
338 
339     // layer1 is no longer active.
340     // layer2 is frequent and has low refresh rate.
341     for (size_t i = 0; i < 2 * PRESENT_TIME_HISTORY_SIZE; i++) {
342         setBufferWithPresentTime(layer2, time);
343         time += LO_FPS_PERIOD;
344         summary = summarizeLayerHistory(time);
345     }
346 
347     ASSERT_EQ(1u, summary.size());
348     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
349     EXPECT_EQ(LO_FPS, summary[0].desiredRefreshRate);
350     EXPECT_EQ(1u, activeLayerCount());
351     EXPECT_EQ(1, frequentLayerCount(time));
352 
353     // layer2 still has low refresh rate.
354     // layer3 has high refresh rate but not enough history.
355     constexpr int RATIO = LO_FPS_PERIOD / HI_FPS_PERIOD;
356     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE - 1; i++) {
357         if (i % RATIO == 0) {
358             setBufferWithPresentTime(layer2, time);
359         }
360 
361         setBufferWithPresentTime(layer3, time);
362         time += HI_FPS_PERIOD;
363         summary = summarizeLayerHistory(time);
364     }
365 
366     ASSERT_EQ(2u, summary.size());
367     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
368     EXPECT_EQ(LO_FPS, summary[0].desiredRefreshRate);
369     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summary[1].vote);
370     EXPECT_EQ(2u, activeLayerCount());
371     EXPECT_EQ(2, frequentLayerCount(time));
372 
373     // layer3 becomes recently active.
374     setBufferWithPresentTime(layer3, time);
375     summary = summarizeLayerHistory(time);
376     ASSERT_EQ(2u, summary.size());
377     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
378     EXPECT_EQ(LO_FPS, summary[0].desiredRefreshRate);
379     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[1].vote);
380     EXPECT_EQ(HI_FPS, summary[1].desiredRefreshRate);
381     EXPECT_EQ(2u, activeLayerCount());
382     EXPECT_EQ(2, frequentLayerCount(time));
383 
384     // layer1 expires.
385     destroyLayer(layer1);
386     updateLayerSnapshotsAndLayerHistory(time);
387 
388     summary = summarizeLayerHistory(time);
389     ASSERT_EQ(2u, summary.size());
390     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
391     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
392     EXPECT_EQ(LO_FPS, summary[0].desiredRefreshRate);
393     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[1].vote);
394     EXPECT_EQ(HI_FPS, summary[1].desiredRefreshRate);
395     EXPECT_EQ(2u, layerCount());
396     EXPECT_EQ(2u, activeLayerCount());
397     EXPECT_EQ(2, frequentLayerCount(time));
398 
399     // layer2 still has low refresh rate.
400     // layer3 becomes inactive.
401     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
402         setBufferWithPresentTime(layer2, time);
403         time += LO_FPS_PERIOD;
404         summary = summarizeLayerHistory(time);
405     }
406 
407     ASSERT_EQ(1u, summary.size());
408     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
409     EXPECT_EQ(LO_FPS, summary[0].desiredRefreshRate);
410     EXPECT_EQ(1u, activeLayerCount());
411     EXPECT_EQ(1, frequentLayerCount(time));
412 
413     // layer2 expires.
414     destroyLayer(layer2);
415     updateLayerSnapshotsAndLayerHistory(time);
416     summary = summarizeLayerHistory(time);
417     EXPECT_TRUE(summary.empty());
418     EXPECT_EQ(1u, layerCount());
419     EXPECT_EQ(0u, activeLayerCount());
420     EXPECT_EQ(0, frequentLayerCount(time));
421 
422     // layer3 becomes active and has high refresh rate.
423     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE + FREQUENT_LAYER_WINDOW_SIZE + 1; i++) {
424         setBufferWithPresentTime(layer3, time);
425         time += HI_FPS_PERIOD;
426         summary = summarizeLayerHistory(time);
427     }
428 
429     ASSERT_EQ(1u, summary.size());
430     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
431     EXPECT_EQ(HI_FPS, summary[0].desiredRefreshRate);
432     EXPECT_EQ(1u, layerCount());
433     EXPECT_EQ(1u, activeLayerCount());
434     EXPECT_EQ(1, frequentLayerCount(time));
435 
436     // layer3 expires.
437     destroyLayer(layer3);
438     updateLayerSnapshotsAndLayerHistory(time);
439     summary = summarizeLayerHistory(time);
440     EXPECT_TRUE(summary.empty());
441     EXPECT_EQ(0u, layerCount());
442     EXPECT_EQ(0u, activeLayerCount());
443     EXPECT_EQ(0, frequentLayerCount(time));
444 }
445 
TEST_F(LayerHistoryIntegrationTest,inactiveLayers)446 TEST_F(LayerHistoryIntegrationTest, inactiveLayers) {
447     auto layer = createLegacyAndFrontedEndLayer(1);
448     nsecs_t time = systemTime();
449 
450     // the very first updates makes the layer frequent
451     for (size_t i = 0; i < FREQUENT_LAYER_WINDOW_SIZE - 1; i++) {
452         setBufferWithPresentTime(layer, time);
453         time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
454 
455         EXPECT_EQ(1u, layerCount());
456         ASSERT_EQ(1u, summarizeLayerHistory(time).size());
457         EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
458         EXPECT_EQ(1u, activeLayerCount());
459         EXPECT_EQ(1, frequentLayerCount(time));
460     }
461 
462     // the next update with the MAX_FREQUENT_LAYER_PERIOD_NS will get us to infrequent
463     time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
464     setBufferWithPresentTime(layer, time);
465 
466     EXPECT_EQ(1u, layerCount());
467     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
468     EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
469     EXPECT_EQ(1u, activeLayerCount());
470     EXPECT_EQ(0, frequentLayerCount(time));
471 
472     // advance the time for the previous frame to be inactive
473     time += MAX_ACTIVE_LAYER_PERIOD_NS.count();
474 
475     // Now even if we post a quick few frame we should stay infrequent
476     for (size_t i = 0; i < FREQUENT_LAYER_WINDOW_SIZE - 1; i++) {
477         setBufferWithPresentTime(layer, time);
478         time += HI_FPS_PERIOD;
479 
480         EXPECT_EQ(1u, layerCount());
481         ASSERT_EQ(1u, summarizeLayerHistory(time).size());
482         EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
483         EXPECT_EQ(1u, activeLayerCount());
484         EXPECT_EQ(0, frequentLayerCount(time));
485     }
486 
487     // More quick frames will get us to frequent again
488     setBufferWithPresentTime(layer, time);
489     time += HI_FPS_PERIOD;
490 
491     EXPECT_EQ(1u, layerCount());
492     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
493     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
494     EXPECT_EQ(1u, activeLayerCount());
495     EXPECT_EQ(1, frequentLayerCount(time));
496 }
497 
TEST_F(LayerHistoryIntegrationTest,invisibleExplicitLayerIsActive)498 TEST_F(LayerHistoryIntegrationTest, invisibleExplicitLayerIsActive) {
499     SET_FLAG_FOR_TEST(flags::misc1, false);
500 
501     auto explicitVisiblelayer = createLegacyAndFrontedEndLayer(1);
502     auto explicitInvisiblelayer = createLegacyAndFrontedEndLayer(2);
503     hideLayer(2);
504     setFrameRate(1, 60.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,
505                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
506     setFrameRate(2, 90.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,
507                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
508     nsecs_t time = systemTime();
509 
510     // Post a buffer to the layers to make them active
511     setBufferWithPresentTime(explicitVisiblelayer, time);
512     setBufferWithPresentTime(explicitInvisiblelayer, time);
513 
514     EXPECT_EQ(2u, layerCount());
515     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
516     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitExactOrMultiple,
517               summarizeLayerHistory(time)[0].vote);
518     EXPECT_EQ(60_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
519     EXPECT_EQ(2u, activeLayerCount());
520     EXPECT_EQ(2, frequentLayerCount(time));
521 }
522 
TEST_F(LayerHistoryIntegrationTest,invisibleExplicitLayerIsNotActive)523 TEST_F(LayerHistoryIntegrationTest, invisibleExplicitLayerIsNotActive) {
524     SET_FLAG_FOR_TEST(flags::misc1, true);
525 
526     auto explicitVisiblelayer = createLegacyAndFrontedEndLayer(1);
527     auto explicitInvisiblelayer = createLegacyAndFrontedEndLayer(2);
528     hideLayer(2);
529     setFrameRate(1, 60.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,
530                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
531     setFrameRate(2, 90.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,
532                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
533     nsecs_t time = systemTime();
534 
535     // Post a buffer to the layers to make them active
536     setBufferWithPresentTime(explicitVisiblelayer, time);
537     setBufferWithPresentTime(explicitInvisiblelayer, time);
538 
539     EXPECT_EQ(2u, layerCount());
540     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
541     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitExactOrMultiple,
542               summarizeLayerHistory(time)[0].vote);
543     EXPECT_EQ(60_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
544     EXPECT_EQ(1u, activeLayerCount());
545     EXPECT_EQ(1, frequentLayerCount(time));
546 }
547 
TEST_F(LayerHistoryIntegrationTest,infrequentAnimatingLayer)548 TEST_F(LayerHistoryIntegrationTest, infrequentAnimatingLayer) {
549     auto layer = createLegacyAndFrontedEndLayer(1);
550 
551     nsecs_t time = systemTime();
552 
553     EXPECT_EQ(1u, layerCount());
554     EXPECT_EQ(0u, activeLayerCount());
555     EXPECT_EQ(0, frequentLayerCount(time));
556     EXPECT_EQ(0, animatingLayerCount(time));
557 
558     // layer is active but infrequent.
559     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
560         setBufferWithPresentTime(layer, time);
561         time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
562     }
563 
564     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
565     EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
566     EXPECT_EQ(1u, activeLayerCount());
567     EXPECT_EQ(0, frequentLayerCount(time));
568     EXPECT_EQ(0, animatingLayerCount(time));
569 
570     // another update with the same cadence keep in infrequent
571     setBufferWithPresentTime(layer, time);
572     time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
573 
574     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
575     EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
576     EXPECT_EQ(1u, activeLayerCount());
577     EXPECT_EQ(0, frequentLayerCount(time));
578     EXPECT_EQ(0, animatingLayerCount(time));
579 
580     mFlinger.mutableLayerSnapshotBuilder().getSnapshot(1)->changes |=
581             frontend::RequestedLayerState::Changes::Animation;
582     mFlinger.updateLayerHistory(time);
583     // an update as animation will immediately vote for Max
584     time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
585 
586     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
587     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
588     EXPECT_EQ(1u, activeLayerCount());
589     EXPECT_EQ(0, frequentLayerCount(time));
590     EXPECT_EQ(1, animatingLayerCount(time));
591 }
592 
TEST_F(LayerHistoryIntegrationTest,frequentLayerBecomingInfrequentAndBack)593 TEST_F(LayerHistoryIntegrationTest, frequentLayerBecomingInfrequentAndBack) {
594     auto layer = createLegacyAndFrontedEndLayer(1);
595 
596     nsecs_t time = systemTime();
597 
598     EXPECT_EQ(1u, layerCount());
599     EXPECT_EQ(0u, activeLayerCount());
600     EXPECT_EQ(0, frequentLayerCount(time));
601     EXPECT_EQ(0, animatingLayerCount(time));
602 
603     // Fill up the window with frequent updates
604     for (size_t i = 0; i < FREQUENT_LAYER_WINDOW_SIZE; i++) {
605         setBufferWithPresentTime(layer, time);
606         time += (60_Hz).getPeriodNsecs();
607 
608         EXPECT_EQ(1u, layerCount());
609         ASSERT_EQ(1u, summarizeLayerHistory(time).size());
610         EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
611         EXPECT_EQ(1u, activeLayerCount());
612         EXPECT_EQ(1, frequentLayerCount(time));
613     }
614 
615     // posting a buffer after long inactivity should retain the layer as active
616     time += std::chrono::nanoseconds(3s).count();
617     setBufferWithPresentTime(layer, time);
618     EXPECT_EQ(0, clearLayerHistoryCount(time));
619     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
620     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summarizeLayerHistory(time)[0].vote);
621     EXPECT_EQ(60_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
622     EXPECT_EQ(1u, activeLayerCount());
623     EXPECT_EQ(1, frequentLayerCount(time));
624     EXPECT_EQ(0, animatingLayerCount(time));
625 
626     // posting more infrequent buffer should make the layer infrequent
627     time += (MAX_FREQUENT_LAYER_PERIOD_NS + 1ms).count();
628     setBufferWithPresentTime(layer, time);
629     time += (MAX_FREQUENT_LAYER_PERIOD_NS + 1ms).count();
630     setBufferWithPresentTime(layer, time);
631     EXPECT_EQ(0, clearLayerHistoryCount(time));
632     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
633     EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
634     EXPECT_EQ(1u, activeLayerCount());
635     EXPECT_EQ(0, frequentLayerCount(time));
636     EXPECT_EQ(0, animatingLayerCount(time));
637 
638     // posting another buffer should keep the layer infrequent
639     setBufferWithPresentTime(layer, time);
640     EXPECT_EQ(0, clearLayerHistoryCount(time));
641     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
642     EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
643     EXPECT_EQ(1u, activeLayerCount());
644     EXPECT_EQ(0, frequentLayerCount(time));
645     EXPECT_EQ(0, animatingLayerCount(time));
646 
647     // posting more buffers would mean starting of an animation, so making the layer frequent
648     setBufferWithPresentTime(layer, time);
649     setBufferWithPresentTime(layer, time);
650     EXPECT_EQ(1, clearLayerHistoryCount(time));
651     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
652     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
653     EXPECT_EQ(1u, activeLayerCount());
654     EXPECT_EQ(1, frequentLayerCount(time));
655     EXPECT_EQ(0, animatingLayerCount(time));
656 
657     // posting a buffer after long inactivity should retain the layer as active
658     time += std::chrono::nanoseconds(3s).count();
659     setBufferWithPresentTime(layer, time);
660     EXPECT_EQ(0, clearLayerHistoryCount(time));
661     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
662     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
663     EXPECT_EQ(1u, activeLayerCount());
664     EXPECT_EQ(1, frequentLayerCount(time));
665     EXPECT_EQ(0, animatingLayerCount(time));
666 
667     // posting another buffer should keep the layer frequent
668     time += (60_Hz).getPeriodNsecs();
669     setBufferWithPresentTime(layer, time);
670     EXPECT_EQ(0, clearLayerHistoryCount(time));
671     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
672     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
673     EXPECT_EQ(1u, activeLayerCount());
674     EXPECT_EQ(1, frequentLayerCount(time));
675     EXPECT_EQ(0, animatingLayerCount(time));
676 }
677 
TEST_F(LayerHistoryIntegrationTest,inconclusiveLayerBecomingFrequent)678 TEST_F(LayerHistoryIntegrationTest, inconclusiveLayerBecomingFrequent) {
679     auto layer = createLegacyAndFrontedEndLayer(1);
680 
681     nsecs_t time = systemTime();
682 
683     EXPECT_EQ(1u, layerCount());
684     EXPECT_EQ(0u, activeLayerCount());
685     EXPECT_EQ(0, frequentLayerCount(time));
686     EXPECT_EQ(0, animatingLayerCount(time));
687 
688     // Fill up the window with frequent updates
689     for (size_t i = 0; i < FREQUENT_LAYER_WINDOW_SIZE; i++) {
690         setBufferWithPresentTime(layer, time);
691         time += (60_Hz).getPeriodNsecs();
692 
693         EXPECT_EQ(1u, layerCount());
694         ASSERT_EQ(1u, summarizeLayerHistory(time).size());
695         EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
696         EXPECT_EQ(1u, activeLayerCount());
697         EXPECT_EQ(1, frequentLayerCount(time));
698     }
699 
700     // posting infrequent buffers after long inactivity should make the layer
701     // inconclusive but frequent.
702     time += std::chrono::nanoseconds(3s).count();
703     setBufferWithPresentTime(layer, time);
704     time += (MAX_FREQUENT_LAYER_PERIOD_NS + 1ms).count();
705     setBufferWithPresentTime(layer, time);
706     EXPECT_EQ(0, clearLayerHistoryCount(time));
707     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
708     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summarizeLayerHistory(time)[0].vote);
709     EXPECT_EQ(1u, activeLayerCount());
710     EXPECT_EQ(1, frequentLayerCount(time));
711     EXPECT_EQ(0, animatingLayerCount(time));
712 
713     // posting more buffers should make the layer frequent and switch the refresh rate to max
714     // by clearing the history
715     setBufferWithPresentTime(layer, time);
716     setBufferWithPresentTime(layer, time);
717     setBufferWithPresentTime(layer, time);
718     EXPECT_EQ(1, clearLayerHistoryCount(time));
719     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
720     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
721     EXPECT_EQ(1u, activeLayerCount());
722     EXPECT_EQ(1, frequentLayerCount(time));
723     EXPECT_EQ(0, animatingLayerCount(time));
724 }
725 
TEST_F(LayerHistoryIntegrationTest,getFramerate)726 TEST_F(LayerHistoryIntegrationTest, getFramerate) {
727     auto layer = createLegacyAndFrontedEndLayer(1);
728 
729     nsecs_t time = systemTime();
730 
731     EXPECT_EQ(1u, layerCount());
732     EXPECT_EQ(0u, activeLayerCount());
733     EXPECT_EQ(0, frequentLayerCount(time));
734     EXPECT_EQ(0, animatingLayerCount(time));
735 
736     // layer is active but infrequent.
737     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
738         setBufferWithPresentTime(layer, time);
739         time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
740     }
741 
742     float expectedFramerate = 1e9f / MAX_FREQUENT_LAYER_PERIOD_NS.count();
743     EXPECT_FLOAT_EQ(expectedFramerate, history().getLayerFramerate(time, layer->getSequence()));
744 }
745 
TEST_F(LayerHistoryIntegrationTest,heuristicLayer60Hz)746 TEST_F(LayerHistoryIntegrationTest, heuristicLayer60Hz) {
747     auto layer = createLegacyAndFrontedEndLayer(1);
748 
749     nsecs_t time = systemTime();
750     for (float fps = 54.0f; fps < 65.0f; fps += 0.1f) {
751         recordFramesAndExpect(layer, time, Fps::fromValue(fps), 60_Hz, PRESENT_TIME_HISTORY_SIZE);
752     }
753 }
754 
TEST_F(LayerHistoryIntegrationTest,heuristicLayer60_30Hz)755 TEST_F(LayerHistoryIntegrationTest, heuristicLayer60_30Hz) {
756     auto layer = createLegacyAndFrontedEndLayer(1);
757 
758     nsecs_t time = systemTime();
759     recordFramesAndExpect(layer, time, 60_Hz, 60_Hz, PRESENT_TIME_HISTORY_SIZE);
760 
761     recordFramesAndExpect(layer, time, 60_Hz, 60_Hz, PRESENT_TIME_HISTORY_SIZE);
762     recordFramesAndExpect(layer, time, 30_Hz, 60_Hz, PRESENT_TIME_HISTORY_SIZE);
763     recordFramesAndExpect(layer, time, 30_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
764     recordFramesAndExpect(layer, time, 60_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
765     recordFramesAndExpect(layer, time, 60_Hz, 60_Hz, PRESENT_TIME_HISTORY_SIZE);
766 }
767 
TEST_F(LayerHistoryIntegrationTest,heuristicLayerNotOscillating)768 TEST_F(LayerHistoryIntegrationTest, heuristicLayerNotOscillating) {
769     SET_FLAG_FOR_TEST(flags::use_known_refresh_rate_for_fps_consistency, false);
770     auto layer = createLegacyAndFrontedEndLayer(1);
771 
772     nsecs_t time = systemTime();
773 
774     recordFramesAndExpect(layer, time, 27.1_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
775     recordFramesAndExpect(layer, time, 26.9_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
776     recordFramesAndExpect(layer, time, 26_Hz, 24_Hz, PRESENT_TIME_HISTORY_SIZE);
777     recordFramesAndExpect(layer, time, 26.9_Hz, 24_Hz, PRESENT_TIME_HISTORY_SIZE);
778     recordFramesAndExpect(layer, time, 27.1_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
779 }
780 
TEST_F(LayerHistoryIntegrationTest,heuristicLayerNotOscillating_useKnownRefreshRate)781 TEST_F(LayerHistoryIntegrationTest, heuristicLayerNotOscillating_useKnownRefreshRate) {
782     SET_FLAG_FOR_TEST(flags::use_known_refresh_rate_for_fps_consistency, true);
783     auto layer = createLegacyAndFrontedEndLayer(1);
784 
785     nsecs_t time = systemTime();
786 
787     recordFramesAndExpect(layer, time, 27.1_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
788     recordFramesAndExpect(layer, time, 26.9_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
789     recordFramesAndExpect(layer, time, 26_Hz, 24_Hz, PRESENT_TIME_HISTORY_SIZE);
790     recordFramesAndExpect(layer, time, 26.9_Hz, 24_Hz, PRESENT_TIME_HISTORY_SIZE);
791     recordFramesAndExpect(layer, time, 27.1_Hz, 24_Hz, PRESENT_TIME_HISTORY_SIZE);
792     recordFramesAndExpect(layer, time, 27.1_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
793 }
794 
TEST_F(LayerHistoryIntegrationTest,smallDirtyLayer)795 TEST_F(LayerHistoryIntegrationTest, smallDirtyLayer) {
796     auto layer = createLegacyAndFrontedEndLayer(1);
797 
798     nsecs_t time = systemTime();
799 
800     EXPECT_EQ(1u, layerCount());
801     EXPECT_EQ(0u, activeLayerCount());
802     EXPECT_EQ(0, frequentLayerCount(time));
803 
804     LayerHistory::Summary summary;
805 
806     // layer is active but infrequent.
807     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
808         auto props = layer->getLayerProps();
809         if (i % 3 == 0) {
810             props.isSmallDirty = false;
811         } else {
812             props.isSmallDirty = true;
813         }
814 
815         setBufferWithPresentTime(layer, time);
816         time += HI_FPS_PERIOD;
817         summary = summarizeLayerHistory(time);
818     }
819 
820     ASSERT_EQ(1u, summary.size());
821     ASSERT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
822     EXPECT_GE(HI_FPS, summary[0].desiredRefreshRate);
823 }
824 
TEST_F(LayerHistoryIntegrationTest,DISABLED_smallDirtyInMultiLayer)825 TEST_F(LayerHistoryIntegrationTest, DISABLED_smallDirtyInMultiLayer) {
826     auto uiLayer = createLegacyAndFrontedEndLayer(1);
827     auto videoLayer = createLegacyAndFrontedEndLayer(2);
828     setFrameRate(2, 30.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT,
829                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
830 
831     nsecs_t time = systemTime();
832 
833     EXPECT_EQ(2u, layerCount());
834     EXPECT_EQ(0u, activeLayerCount());
835     EXPECT_EQ(0, frequentLayerCount(time));
836 
837     LayerHistory::Summary summary;
838 
839     // uiLayer is updating small dirty.
840     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE + FREQUENT_LAYER_WINDOW_SIZE + 1; i++) {
841         auto props = uiLayer->getLayerProps();
842         props.isSmallDirty = true;
843         setBuffer(1);
844         uiLayer->setDesiredPresentTime(0, false /*autotimestamp*/);
845         updateLayerSnapshotsAndLayerHistory(time);
846         setBufferWithPresentTime(videoLayer, time);
847         summary = summarizeLayerHistory(time);
848     }
849 
850     ASSERT_EQ(1u, summary.size());
851     ASSERT_EQ(LayerHistory::LayerVoteType::ExplicitDefault, summary[0].vote);
852     ASSERT_EQ(30_Hz, summary[0].desiredRefreshRate);
853 }
854 
TEST_F(LayerHistoryIntegrationTest,hidingLayerUpdatesLayerHistory)855 TEST_F(LayerHistoryIntegrationTest, hidingLayerUpdatesLayerHistory) {
856     createLegacyAndFrontedEndLayer(1);
857     nsecs_t time = systemTime();
858     updateLayerSnapshotsAndLayerHistory(time);
859     EXPECT_EQ(1u, layerCount());
860     EXPECT_EQ(0u, activeLayerCount());
861 
862     setBuffer(1);
863     updateLayerSnapshotsAndLayerHistory(time);
864     auto summary = summarizeLayerHistory(time);
865     ASSERT_EQ(1u, summary.size());
866     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summary[0].vote);
867     EXPECT_EQ(1u, activeLayerCount());
868 
869     hideLayer(1);
870     updateLayerSnapshotsAndLayerHistory(time);
871 
872     summary = summarizeLayerHistory(time);
873     EXPECT_TRUE(summary.empty());
874     EXPECT_EQ(0u, activeLayerCount());
875 }
876 
TEST_F(LayerHistoryIntegrationTest,showingLayerUpdatesLayerHistory)877 TEST_F(LayerHistoryIntegrationTest, showingLayerUpdatesLayerHistory) {
878     createLegacyAndFrontedEndLayer(1);
879     nsecs_t time = systemTime();
880     updateLayerSnapshotsAndLayerHistory(time);
881     EXPECT_EQ(1u, layerCount());
882     EXPECT_EQ(0u, activeLayerCount());
883     hideLayer(1);
884     setBuffer(1);
885     updateLayerSnapshotsAndLayerHistory(time);
886     auto summary = summarizeLayerHistory(time);
887     EXPECT_TRUE(summary.empty());
888     EXPECT_EQ(0u, activeLayerCount());
889 
890     showLayer(1);
891     updateLayerSnapshotsAndLayerHistory(time);
892 
893     summary = summarizeLayerHistory(time);
894     ASSERT_EQ(1u, summary.size());
895     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summary[0].vote);
896     EXPECT_EQ(1u, activeLayerCount());
897 }
898 
TEST_F(LayerHistoryIntegrationTest,updatingGeometryUpdatesWeight)899 TEST_F(LayerHistoryIntegrationTest, updatingGeometryUpdatesWeight) {
900     createLegacyAndFrontedEndLayer(1);
901     nsecs_t time = systemTime();
902     updateLayerSnapshotsAndLayerHistory(time);
903     EXPECT_EQ(1u, layerCount());
904     EXPECT_EQ(0u, activeLayerCount());
905 
906     setBuffer(1,
907               std::make_shared<
908                       renderengine::mock::FakeExternalTexture>(100U /*width*/, 100U /*height*/, 1,
909                                                                HAL_PIXEL_FORMAT_RGBA_8888,
910                                                                GRALLOC_USAGE_PROTECTED /*usage*/));
911     mFlinger.setLayerHistoryDisplayArea(100 * 100);
912     updateLayerSnapshotsAndLayerHistory(time);
913     auto summary = summarizeLayerHistory(time);
914     ASSERT_EQ(1u, summary.size());
915     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summary[0].vote);
916     EXPECT_EQ(1u, activeLayerCount());
917 
918     auto startingWeight = summary[0].weight;
919 
920     setMatrix(1, 0.1f, 0.f, 0.f, 0.1f);
921     updateLayerSnapshotsAndLayerHistory(time);
922 
923     summary = summarizeLayerHistory(time);
924     ASSERT_EQ(1u, summary.size());
925     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summary[0].vote);
926     EXPECT_EQ(1u, activeLayerCount());
927     EXPECT_GT(startingWeight, summary[0].weight);
928 }
929 
930 class LayerHistoryIntegrationTestParameterized
931       : public LayerHistoryIntegrationTest,
932         public testing::WithParamInterface<std::chrono::nanoseconds> {};
933 
TEST_P(LayerHistoryIntegrationTestParameterized,HeuristicLayerWithInfrequentLayer)934 TEST_P(LayerHistoryIntegrationTestParameterized, HeuristicLayerWithInfrequentLayer) {
935     std::chrono::nanoseconds infrequentUpdateDelta = GetParam();
936     auto heuristicLayer = createLegacyAndFrontedEndLayer(1);
937     auto infrequentLayer = createLegacyAndFrontedEndLayer(2);
938 
939     const nsecs_t startTime = systemTime();
940 
941     const std::chrono::nanoseconds heuristicUpdateDelta = 41'666'667ns;
942     setBufferWithPresentTime(heuristicLayer, startTime);
943     setBufferWithPresentTime(infrequentLayer, startTime);
944 
945     nsecs_t time = startTime;
946     nsecs_t lastInfrequentUpdate = startTime;
947     const size_t totalInfrequentLayerUpdates = FREQUENT_LAYER_WINDOW_SIZE * 5;
948     size_t infrequentLayerUpdates = 0;
949     while (infrequentLayerUpdates <= totalInfrequentLayerUpdates) {
950         time += heuristicUpdateDelta.count();
951         setBufferWithPresentTime(heuristicLayer, time);
952 
953         if (time - lastInfrequentUpdate >= infrequentUpdateDelta.count()) {
954             ALOGI("submitting infrequent frame [%zu/%zu]", infrequentLayerUpdates,
955                   totalInfrequentLayerUpdates);
956             lastInfrequentUpdate = time;
957             setBufferWithPresentTime(infrequentLayer, time);
958             infrequentLayerUpdates++;
959         }
960 
961         if (time - startTime > PRESENT_TIME_HISTORY_DURATION.count()) {
962             ASSERT_NE(0u, summarizeLayerHistory(time).size());
963             ASSERT_GE(2u, summarizeLayerHistory(time).size());
964 
965             bool max = false;
966             bool min = false;
967             Fps heuristic;
968             for (const auto& layer : summarizeLayerHistory(time)) {
969                 if (layer.vote == LayerHistory::LayerVoteType::Heuristic) {
970                     heuristic = layer.desiredRefreshRate;
971                 } else if (layer.vote == LayerHistory::LayerVoteType::Max) {
972                     max = true;
973                 } else if (layer.vote == LayerHistory::LayerVoteType::Min) {
974                     min = true;
975                 }
976             }
977 
978             if (infrequentLayerUpdates > FREQUENT_LAYER_WINDOW_SIZE) {
979                 EXPECT_EQ(24_Hz, heuristic);
980                 EXPECT_FALSE(max);
981                 if (summarizeLayerHistory(time).size() == 2) {
982                     EXPECT_TRUE(min);
983                 }
984             }
985         }
986     }
987 }
988 
989 class SmallAreaDetectionTest : public LayerHistoryIntegrationTest {
990 protected:
991     static constexpr int32_t DISPLAY_WIDTH = 100;
992     static constexpr int32_t DISPLAY_HEIGHT = 100;
993 
994     static constexpr int32_t kAppId1 = 10100;
995     static constexpr int32_t kAppId2 = 10101;
996 
997     static constexpr float kThreshold1 = 0.05f;
998     static constexpr float kThreshold2 = 0.07f;
999 
SmallAreaDetectionTest()1000     SmallAreaDetectionTest() : LayerHistoryIntegrationTest() {
1001         std::vector<std::pair<int32_t, float>> mappings;
1002         mappings.reserve(2);
1003         mappings.push_back(std::make_pair(kAppId1, kThreshold1));
1004         mappings.push_back(std::make_pair(kAppId2, kThreshold2));
1005 
1006         mScheduler->onActiveDisplayAreaChanged(DISPLAY_WIDTH * DISPLAY_HEIGHT);
1007         mScheduler->updateSmallAreaDetection(mappings);
1008     }
1009 
createLegacyAndFrontedEndLayer(uint32_t sequence)1010     auto createLegacyAndFrontedEndLayer(uint32_t sequence) {
1011         std::string layerName = "test layer:" + std::to_string(sequence);
1012 
1013         LayerCreationArgs args = LayerCreationArgs{mFlinger.flinger(),
1014                                                    nullptr,
1015                                                    layerName,
1016                                                    0,
1017                                                    {},
1018                                                    std::make_optional<uint32_t>(sequence)};
1019         args.ownerUid = kAppId1;
1020         args.metadata.setInt32(gui::METADATA_WINDOW_TYPE, 2); // APPLICATION
1021         const auto layer = sp<Layer>::make(args);
1022         mFlinger.injectLegacyLayer(layer);
1023         createRootLayer(sequence);
1024         return layer;
1025     }
1026 };
1027 
TEST_F(SmallAreaDetectionTest,SmallDirtyLayer)1028 TEST_F(SmallAreaDetectionTest, SmallDirtyLayer) {
1029     SET_FLAG_FOR_TEST(flags::enable_small_area_detection, true);
1030     auto layer = createLegacyAndFrontedEndLayer(1);
1031 
1032     nsecs_t time = systemTime();
1033 
1034     EXPECT_EQ(1u, layerCount());
1035     EXPECT_EQ(0u, activeLayerCount());
1036     EXPECT_EQ(0, frequentLayerCount(time));
1037 
1038     uint32_t sequence = static_cast<uint32_t>(layer->sequence);
1039     setBuffer(sequence);
1040     setDamageRegion(sequence, Region(Rect(10, 10)));
1041     updateLayerSnapshotsAndLayerHistory(time);
1042 
1043     ASSERT_EQ(true, mFlinger.mutableLayerSnapshotBuilder().getSnapshot(1)->isSmallDirty);
1044 }
1045 
TEST_F(SmallAreaDetectionTest,NotSmallDirtyLayer)1046 TEST_F(SmallAreaDetectionTest, NotSmallDirtyLayer) {
1047     SET_FLAG_FOR_TEST(flags::enable_small_area_detection, true);
1048     auto layer = createLegacyAndFrontedEndLayer(1);
1049 
1050     nsecs_t time = systemTime();
1051 
1052     EXPECT_EQ(1u, layerCount());
1053     EXPECT_EQ(0u, activeLayerCount());
1054     EXPECT_EQ(0, frequentLayerCount(time));
1055 
1056     uint32_t sequence = static_cast<uint32_t>(layer->sequence);
1057     setBuffer(sequence);
1058     setDamageRegion(sequence, Region(Rect(50, 50)));
1059     updateLayerSnapshotsAndLayerHistory(time);
1060 
1061     ASSERT_EQ(false, mFlinger.mutableLayerSnapshotBuilder().getSnapshot(1)->isSmallDirty);
1062 }
1063 
TEST_F(SmallAreaDetectionTest,smallDirtyLayerWithMatrix)1064 TEST_F(SmallAreaDetectionTest, smallDirtyLayerWithMatrix) {
1065     SET_FLAG_FOR_TEST(flags::enable_small_area_detection, true);
1066     auto layer = createLegacyAndFrontedEndLayer(1);
1067 
1068     nsecs_t time = systemTime();
1069 
1070     EXPECT_EQ(1u, layerCount());
1071     EXPECT_EQ(0u, activeLayerCount());
1072     EXPECT_EQ(0, frequentLayerCount(time));
1073 
1074     // Original damage region is a small dirty.
1075     uint32_t sequence = static_cast<uint32_t>(layer->sequence);
1076     setBuffer(sequence);
1077     setDamageRegion(sequence, Region(Rect(20, 20)));
1078     updateLayerSnapshotsAndLayerHistory(time);
1079     ASSERT_EQ(true, mFlinger.mutableLayerSnapshotBuilder().getSnapshot(1)->isSmallDirty);
1080 
1081     setMatrix(sequence, 2.0f, 0, 0, 2.0f);
1082     updateLayerSnapshotsAndLayerHistory(time);
1083 
1084     // Verify if the small dirty is scaled.
1085     ASSERT_EQ(false, mFlinger.mutableLayerSnapshotBuilder().getSnapshot(1)->isSmallDirty);
1086 }
1087 
1088 INSTANTIATE_TEST_CASE_P(LeapYearTests, LayerHistoryIntegrationTestParameterized,
1089                         ::testing::Values(1s, 2s, 3s, 4s, 5s));
1090 
1091 } // namespace
1092 } // namespace android::scheduler
1093