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 android::mock::createVrrDisplayMode;
42 using namespace com::android::graphics::surfaceflinger;
43 
44 class LayerHistoryIntegrationTest : public surfaceflinger::frontend::LayerSnapshotTestBase {
45 protected:
46     static constexpr auto PRESENT_TIME_HISTORY_SIZE = LayerInfo::HISTORY_SIZE;
47     static constexpr auto MAX_FREQUENT_LAYER_PERIOD_NS = LayerInfo::kMaxPeriodForFrequentLayerNs;
48     static constexpr auto FREQUENT_LAYER_WINDOW_SIZE = LayerInfo::kFrequentLayerWindowSize;
49     static constexpr auto PRESENT_TIME_HISTORY_DURATION = LayerInfo::HISTORY_DURATION;
50 
51     static constexpr Fps LO_FPS = 30_Hz;
52     static constexpr auto LO_FPS_PERIOD = LO_FPS.getPeriodNsecs();
53 
54     static constexpr Fps HI_FPS = 90_Hz;
55     static constexpr auto HI_FPS_PERIOD = HI_FPS.getPeriodNsecs();
56 
57     static constexpr auto kVrrModeId = DisplayModeId(2);
58 
LayerHistoryIntegrationTest()59     LayerHistoryIntegrationTest() : LayerSnapshotTestBase() {
60         mFlinger.resetScheduler(mScheduler);
61         mLifecycleManager = {};
62         mHierarchyBuilder = {};
63     }
64 
updateLayerSnapshotsAndLayerHistory(nsecs_t now)65     void updateLayerSnapshotsAndLayerHistory(nsecs_t now) {
66         LayerSnapshotTestBase::update(mFlinger.mutableLayerSnapshotBuilder());
67         mFlinger.updateLayerHistory(now);
68     }
69 
setBufferWithPresentTime(sp<Layer> & layer,nsecs_t time)70     void setBufferWithPresentTime(sp<Layer>& layer, nsecs_t time) {
71         uint32_t sequence = static_cast<uint32_t>(layer->sequence);
72         setBuffer(sequence);
73         layer->setDesiredPresentTime(time, false /*autotimestamp*/);
74         updateLayerSnapshotsAndLayerHistory(time);
75     }
76 
setFrontBufferWithPresentTime(sp<Layer> & layer,nsecs_t time)77     void setFrontBufferWithPresentTime(sp<Layer>& layer, nsecs_t time) {
78         uint32_t sequence = static_cast<uint32_t>(layer->sequence);
79         setFrontBuffer(sequence);
80         layer->setDesiredPresentTime(time, false /*autotimestamp*/);
81         updateLayerSnapshotsAndLayerHistory(time);
82     }
83 
history()84     LayerHistory& history() { return mScheduler->mutableLayerHistory(); }
history() const85     const LayerHistory& history() const { return mScheduler->mutableLayerHistory(); }
86 
summarizeLayerHistory(nsecs_t now)87     LayerHistory::Summary summarizeLayerHistory(nsecs_t now) {
88         // LayerHistory::summarize makes no guarantee of the order of the elements in the summary
89         // however, for testing only, a stable order is required, therefore we sort the list here.
90         // Any tests requiring ordered results must create layers with names.
91         auto summary = history().summarize(*mScheduler->refreshRateSelector(), now);
92         std::sort(summary.begin(), summary.end(),
93                   [](const RefreshRateSelector::LayerRequirement& lhs,
94                      const RefreshRateSelector::LayerRequirement& rhs) -> bool {
95                       return lhs.name < rhs.name;
96                   });
97         return summary;
98     }
99 
layerCount() const100     size_t layerCount() const { return mScheduler->layerHistorySize(); }
activeLayerCount() const101     size_t activeLayerCount() const NO_THREAD_SAFETY_ANALYSIS {
102         return history().mActiveLayerInfos.size();
103     }
104 
frequentLayerCount(nsecs_t now) const105     auto frequentLayerCount(nsecs_t now) const NO_THREAD_SAFETY_ANALYSIS {
106         const auto& infos = history().mActiveLayerInfos;
107         return std::count_if(infos.begin(), infos.end(), [now](const auto& pair) {
108             return pair.second.second->isFrequent(now).isFrequent;
109         });
110     }
111 
animatingLayerCount(nsecs_t now) const112     auto animatingLayerCount(nsecs_t now) const NO_THREAD_SAFETY_ANALYSIS {
113         const auto& infos = history().mActiveLayerInfos;
114         return std::count_if(infos.begin(), infos.end(), [now](const auto& pair) {
115             return pair.second.second->isAnimating(now);
116         });
117     }
118 
clearLayerHistoryCount(nsecs_t now) const119     auto clearLayerHistoryCount(nsecs_t now) const NO_THREAD_SAFETY_ANALYSIS {
120         const auto& infos = history().mActiveLayerInfos;
121         return std::count_if(infos.begin(), infos.end(), [now](const auto& pair) {
122             return pair.second.second->isFrequent(now).clearHistory;
123         });
124     }
125 
setDefaultLayerVote(Layer * layer,LayerHistory::LayerVoteType vote)126     void setDefaultLayerVote(Layer* layer,
127                              LayerHistory::LayerVoteType vote) NO_THREAD_SAFETY_ANALYSIS {
128         auto [found, layerPair] = history().findLayer(layer->getSequence());
129         if (found != LayerHistory::LayerStatus::NotFound) {
130             layerPair->second->setDefaultLayerVote(vote);
131         }
132     }
133 
createLegacyAndFrontedEndLayer(uint32_t sequence)134     auto createLegacyAndFrontedEndLayer(uint32_t sequence) {
135         std::string layerName = "test layer:" + std::to_string(sequence);
136         const auto layer =
137                 sp<Layer>::make(LayerCreationArgs{mFlinger.flinger(),
138                                                   nullptr,
139                                                   layerName,
140                                                   0,
141                                                   {},
142                                                   std::make_optional<uint32_t>(sequence)});
143         mFlinger.injectLegacyLayer(layer);
144         createRootLayer(sequence);
145         return layer;
146     }
147 
createLegacyAndFrontedEndLayerWithUid(uint32_t sequence,gui::Uid uid)148     auto createLegacyAndFrontedEndLayerWithUid(uint32_t sequence, gui::Uid uid) {
149         std::string layerName = "test layer:" + std::to_string(sequence);
150         auto args = LayerCreationArgs{mFlinger.flinger(),
151                                       nullptr,
152                                       layerName,
153                                       0,
154                                       {},
155                                       std::make_optional<uint32_t>(sequence)};
156         args.ownerUid = uid.val();
157         const auto layer = sp<Layer>::make(args);
158         mFlinger.injectLegacyLayer(layer);
159         createRootLayerWithUid(sequence, uid);
160         return layer;
161     }
162 
destroyLayer(sp<Layer> & layer)163     auto destroyLayer(sp<Layer>& layer) {
164         uint32_t sequence = static_cast<uint32_t>(layer->sequence);
165         mFlinger.releaseLegacyLayer(sequence);
166         layer.clear();
167         destroyLayerHandle(sequence);
168     }
169 
recordFramesAndExpect(sp<Layer> & layer,nsecs_t & time,Fps frameRate,Fps desiredRefreshRate,int numFrames)170     void recordFramesAndExpect(sp<Layer>& layer, nsecs_t& time, Fps frameRate,
171                                Fps desiredRefreshRate, int numFrames) {
172         LayerHistory::Summary summary;
173         for (int i = 0; i < numFrames; i++) {
174             setBufferWithPresentTime(layer, time);
175             time += frameRate.getPeriodNsecs();
176 
177             summary = summarizeLayerHistory(time);
178         }
179 
180         ASSERT_EQ(1u, summary.size());
181         ASSERT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
182         ASSERT_EQ(desiredRefreshRate, summary[0].desiredRefreshRate);
183     }
184 
185     std::shared_ptr<RefreshRateSelector> mSelector = std::make_shared<RefreshRateSelector>(
186             makeModes(createDisplayMode(DisplayModeId(0), LO_FPS),
187                       createDisplayMode(DisplayModeId(1), HI_FPS),
188                       createVrrDisplayMode(kVrrModeId, HI_FPS,
189                                            hal::VrrConfig{.minFrameIntervalNs =
190                                                                   HI_FPS.getPeriodNsecs()})),
191             DisplayModeId(0));
192 
193     mock::SchedulerCallback mSchedulerCallback;
194     TestableSurfaceFlinger mFlinger;
195     TestableScheduler* mScheduler = new TestableScheduler(mSelector, mFlinger, mSchedulerCallback);
196 };
197 
198 namespace {
199 
TEST_F(LayerHistoryIntegrationTest,singleLayerNoVoteDefaultCompatibility)200 TEST_F(LayerHistoryIntegrationTest, singleLayerNoVoteDefaultCompatibility) {
201     createLegacyAndFrontedEndLayer(1);
202     nsecs_t time = systemTime();
203 
204     updateLayerSnapshotsAndLayerHistory(time);
205 
206     EXPECT_EQ(1u, layerCount());
207     EXPECT_EQ(0u, activeLayerCount());
208 
209     // No layers returned if no layers are active.
210     EXPECT_TRUE(summarizeLayerHistory(time).empty());
211     EXPECT_EQ(0u, activeLayerCount());
212 
213     setBuffer(1);
214     setDefaultFrameRateCompatibility(1, ANATIVEWINDOW_FRAME_RATE_NO_VOTE);
215     updateLayerSnapshotsAndLayerHistory(time);
216 
217     EXPECT_TRUE(summarizeLayerHistory(time).empty());
218     EXPECT_EQ(1u, activeLayerCount());
219 }
220 
TEST_F(LayerHistoryIntegrationTest,singleLayerMinVoteDefaultCompatibility)221 TEST_F(LayerHistoryIntegrationTest, singleLayerMinVoteDefaultCompatibility) {
222     createLegacyAndFrontedEndLayer(1);
223     nsecs_t time = systemTime();
224     updateLayerSnapshotsAndLayerHistory(time);
225 
226     EXPECT_EQ(1u, layerCount());
227     EXPECT_EQ(0u, activeLayerCount());
228 
229     EXPECT_TRUE(summarizeLayerHistory(time).empty());
230     EXPECT_EQ(0u, activeLayerCount());
231 
232     setBuffer(1);
233     setDefaultFrameRateCompatibility(1, ANATIVEWINDOW_FRAME_RATE_MIN);
234     updateLayerSnapshotsAndLayerHistory(time);
235 
236     auto summary = summarizeLayerHistory(time);
237     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
238 
239     EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
240     EXPECT_EQ(1u, activeLayerCount());
241 }
242 
TEST_F(LayerHistoryIntegrationTest,oneLayer)243 TEST_F(LayerHistoryIntegrationTest, oneLayer) {
244     createLegacyAndFrontedEndLayer(1);
245     nsecs_t time = systemTime();
246     updateLayerSnapshotsAndLayerHistory(time);
247 
248     EXPECT_EQ(1u, layerCount());
249     EXPECT_EQ(0u, activeLayerCount());
250 
251     // No layers returned if no layers are active.
252     EXPECT_TRUE(summarizeLayerHistory(time).empty());
253     EXPECT_EQ(0u, activeLayerCount());
254 
255     // Max returned if active layers have insufficient history.
256     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE - 1; i++) {
257         setBuffer(1);
258         updateLayerSnapshotsAndLayerHistory(time);
259         ASSERT_EQ(1u, summarizeLayerHistory(time).size());
260         EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
261         EXPECT_EQ(1u, activeLayerCount());
262         time += LO_FPS_PERIOD;
263     }
264 
265     // Max is returned since we have enough history but there is no timestamp votes.
266     for (size_t i = 0; i < 10; i++) {
267         setBuffer(1);
268         updateLayerSnapshotsAndLayerHistory(time);
269         ASSERT_EQ(1u, summarizeLayerHistory(time).size());
270         EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
271         EXPECT_EQ(1u, activeLayerCount());
272         time += LO_FPS_PERIOD;
273     }
274 }
275 
TEST_F(LayerHistoryIntegrationTest,gameFrameRateOverrideMapping)276 TEST_F(LayerHistoryIntegrationTest, gameFrameRateOverrideMapping) {
277     SET_FLAG_FOR_TEST(flags::game_default_frame_rate, true);
278 
279     history().updateGameDefaultFrameRateOverride(FrameRateOverride({0, 60.0f}));
280 
281     auto overridePair = history().getGameFrameRateOverride(0);
282     EXPECT_EQ(0_Hz, overridePair.first);
283     EXPECT_EQ(60_Hz, overridePair.second);
284 
285     history().updateGameModeFrameRateOverride(FrameRateOverride({0, 40.0f}));
286     history().updateGameModeFrameRateOverride(FrameRateOverride({1, 120.0f}));
287 
288     overridePair = history().getGameFrameRateOverride(0);
289     EXPECT_EQ(40_Hz, overridePair.first);
290     EXPECT_EQ(60_Hz, overridePair.second);
291 
292     overridePair = history().getGameFrameRateOverride(1);
293     EXPECT_EQ(120_Hz, overridePair.first);
294     EXPECT_EQ(0_Hz, overridePair.second);
295 
296     history().updateGameDefaultFrameRateOverride(FrameRateOverride({0, 0.0f}));
297     history().updateGameModeFrameRateOverride(FrameRateOverride({1, 0.0f}));
298 
299     overridePair = history().getGameFrameRateOverride(0);
300     EXPECT_EQ(40_Hz, overridePair.first);
301     EXPECT_EQ(0_Hz, overridePair.second);
302 
303     overridePair = history().getGameFrameRateOverride(1);
304     EXPECT_EQ(0_Hz, overridePair.first);
305     EXPECT_EQ(0_Hz, overridePair.second);
306 }
307 
TEST_F(LayerHistoryIntegrationTest,oneLayerGameFrameRateOverride)308 TEST_F(LayerHistoryIntegrationTest, oneLayerGameFrameRateOverride) {
309     SET_FLAG_FOR_TEST(flags::game_default_frame_rate, true);
310 
311     const uid_t uid = 0;
312     const Fps gameDefaultFrameRate = Fps::fromValue(30.0f);
313     const Fps gameModeFrameRate = Fps::fromValue(60.0f);
314 
315     auto layer = createLegacyAndFrontedEndLayerWithUid(1, gui::Uid(uid));
316     showLayer(1);
317 
318     nsecs_t time = systemTime();
319     updateLayerSnapshotsAndLayerHistory(time);
320 
321     EXPECT_EQ(1u, layerCount());
322     EXPECT_EQ(0u, activeLayerCount());
323 
324     // update game default frame rate override
325     history().updateGameDefaultFrameRateOverride(
326             FrameRateOverride({uid, gameDefaultFrameRate.getValue()}));
327 
328     LayerHistory::Summary summary;
329     scheduler::LayerProps layerProps = {
330             .visible = true,
331             .bounds = {0, 0, 100, 100},
332             .transform = {},
333             .setFrameRateVote = {},
334             .frameRateSelectionPriority = Layer::PRIORITY_UNSET,
335             .isSmallDirty = false,
336             .isFrontBuffered = false,
337     };
338 
339     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
340         setBufferWithPresentTime(layer, time);
341         time += gameDefaultFrameRate.getPeriodNsecs();
342         summary = summarizeLayerHistory(time);
343     }
344 
345     ASSERT_EQ(1u, summary.size());
346     ASSERT_EQ(LayerHistory::LayerVoteType::ExplicitDefault, summary[0].vote);
347     ASSERT_EQ(30.0_Hz, summary[0].desiredRefreshRate);
348 
349     // test against setFrameRate vote
350     setFrameRate(1,
351                  Layer::FrameRate(Fps::fromValue(120.0f), Layer::FrameRateCompatibility::Default));
352     updateLayerSnapshotsAndLayerHistory(time);
353 
354     const Fps setFrameRate = Fps::fromValue(120.0f);
355     layerProps.setFrameRateVote =
356             Layer::FrameRate(setFrameRate, Layer::FrameRateCompatibility::Default);
357 
358     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
359         setBufferWithPresentTime(layer, time);
360         time += setFrameRate.getPeriodNsecs();
361         summary = summarizeLayerHistory(time);
362     }
363 
364     ASSERT_EQ(1u, summary.size());
365     ASSERT_EQ(LayerHistory::LayerVoteType::ExplicitDefault, summary[0].vote);
366     ASSERT_EQ(120.0_Hz, summary[0].desiredRefreshRate);
367 
368     // update game mode frame rate override
369     history().updateGameModeFrameRateOverride(
370             FrameRateOverride({uid, gameModeFrameRate.getValue()}));
371 
372     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
373         setBufferWithPresentTime(layer, time);
374         time += gameModeFrameRate.getPeriodNsecs();
375         summary = summarizeLayerHistory(time);
376     }
377 
378     ASSERT_EQ(1u, summary.size());
379     ASSERT_EQ(LayerHistory::LayerVoteType::ExplicitDefault, summary[0].vote);
380     ASSERT_EQ(60.0_Hz, summary[0].desiredRefreshRate);
381 }
382 
TEST_F(LayerHistoryIntegrationTest,oneInvisibleLayer)383 TEST_F(LayerHistoryIntegrationTest, oneInvisibleLayer) {
384     createLegacyAndFrontedEndLayer(1);
385     nsecs_t time = systemTime();
386     updateLayerSnapshotsAndLayerHistory(time);
387     EXPECT_EQ(1u, layerCount());
388     EXPECT_EQ(0u, activeLayerCount());
389 
390     setBuffer(1);
391     updateLayerSnapshotsAndLayerHistory(time);
392     auto summary = summarizeLayerHistory(time);
393     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
394     // Layer is still considered inactive so we expect to get Min
395     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
396     EXPECT_EQ(1u, activeLayerCount());
397 
398     hideLayer(1);
399     setBuffer(1);
400     updateLayerSnapshotsAndLayerHistory(time);
401 
402     summary = summarizeLayerHistory(time);
403     EXPECT_TRUE(summarizeLayerHistory(time).empty());
404     EXPECT_EQ(0u, activeLayerCount());
405 }
406 
TEST_F(LayerHistoryIntegrationTest,explicitTimestamp)407 TEST_F(LayerHistoryIntegrationTest, explicitTimestamp) {
408     auto layer = createLegacyAndFrontedEndLayer(1);
409     showLayer(1);
410     nsecs_t time = systemTime();
411     updateLayerSnapshotsAndLayerHistory(time);
412 
413     EXPECT_EQ(1u, layerCount());
414     EXPECT_EQ(0u, activeLayerCount());
415 
416     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
417         setBufferWithPresentTime(layer, time);
418         time += LO_FPS_PERIOD;
419     }
420 
421     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
422     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summarizeLayerHistory(time)[0].vote);
423     EXPECT_EQ(LO_FPS, summarizeLayerHistory(time)[0].desiredRefreshRate);
424     EXPECT_EQ(1u, activeLayerCount());
425     EXPECT_EQ(1, frequentLayerCount(time));
426 }
427 
TEST_F(LayerHistoryIntegrationTest,oneLayerNoVote)428 TEST_F(LayerHistoryIntegrationTest, oneLayerNoVote) {
429     auto layer = createLegacyAndFrontedEndLayer(1);
430     showLayer(1);
431     nsecs_t time = systemTime();
432     updateLayerSnapshotsAndLayerHistory(time);
433 
434     setDefaultLayerVote(layer.get(), LayerHistory::LayerVoteType::NoVote);
435 
436     EXPECT_EQ(1u, layerCount());
437     EXPECT_EQ(0u, activeLayerCount());
438 
439     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
440         setBufferWithPresentTime(layer, time);
441         time += HI_FPS_PERIOD;
442     }
443 
444     ASSERT_TRUE(summarizeLayerHistory(time).empty());
445     EXPECT_EQ(1u, activeLayerCount());
446     EXPECT_EQ(1, frequentLayerCount(time));
447 
448     // layer became inactive
449     time += MAX_ACTIVE_LAYER_PERIOD_NS.count();
450     ASSERT_TRUE(summarizeLayerHistory(time).empty());
451     EXPECT_EQ(0u, activeLayerCount());
452     EXPECT_EQ(0, frequentLayerCount(time));
453 }
454 
TEST_F(LayerHistoryIntegrationTest,oneLayerMinVote)455 TEST_F(LayerHistoryIntegrationTest, oneLayerMinVote) {
456     auto layer = createLegacyAndFrontedEndLayer(1);
457     showLayer(1);
458     nsecs_t time = systemTime();
459     updateLayerSnapshotsAndLayerHistory(time);
460 
461     setDefaultLayerVote(layer.get(), LayerHistory::LayerVoteType::Min);
462 
463     EXPECT_EQ(1u, layerCount());
464     EXPECT_EQ(0u, activeLayerCount());
465 
466     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
467         setBufferWithPresentTime(layer, time);
468         time += HI_FPS_PERIOD;
469     }
470 
471     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
472     EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
473     EXPECT_EQ(1u, activeLayerCount());
474     EXPECT_EQ(1, frequentLayerCount(time));
475 
476     // layer became inactive
477     time += MAX_ACTIVE_LAYER_PERIOD_NS.count();
478     ASSERT_TRUE(summarizeLayerHistory(time).empty());
479     EXPECT_EQ(0u, activeLayerCount());
480     EXPECT_EQ(0, frequentLayerCount(time));
481 }
482 
TEST_F(LayerHistoryIntegrationTest,oneLayerMaxVote)483 TEST_F(LayerHistoryIntegrationTest, oneLayerMaxVote) {
484     auto layer = createLegacyAndFrontedEndLayer(1);
485     showLayer(1);
486     nsecs_t time = systemTime();
487     updateLayerSnapshotsAndLayerHistory(time);
488 
489     setDefaultLayerVote(layer.get(), LayerHistory::LayerVoteType::Max);
490 
491     EXPECT_EQ(1u, layerCount());
492     EXPECT_EQ(0u, activeLayerCount());
493 
494     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
495         setBufferWithPresentTime(layer, time);
496         time += LO_FPS_PERIOD;
497     }
498 
499     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
500     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
501     EXPECT_EQ(1u, activeLayerCount());
502     EXPECT_EQ(1, frequentLayerCount(time));
503 
504     // layer became inactive
505     time += MAX_ACTIVE_LAYER_PERIOD_NS.count();
506     ASSERT_TRUE(summarizeLayerHistory(time).empty());
507     EXPECT_EQ(0u, activeLayerCount());
508     EXPECT_EQ(0, frequentLayerCount(time));
509 }
510 
TEST_F(LayerHistoryIntegrationTest,oneLayerExplicitVote)511 TEST_F(LayerHistoryIntegrationTest, oneLayerExplicitVote) {
512     createLegacyAndFrontedEndLayer(1);
513     setFrameRate(1, 73.4f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT,
514                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
515 
516     EXPECT_EQ(1u, layerCount());
517     EXPECT_EQ(0u, activeLayerCount());
518 
519     nsecs_t time = systemTime();
520     updateLayerSnapshotsAndLayerHistory(time);
521     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
522     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitDefault, summarizeLayerHistory(time)[0].vote);
523     EXPECT_EQ(73.4_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
524     EXPECT_EQ(1u, activeLayerCount());
525     EXPECT_EQ(1, frequentLayerCount(time));
526 }
527 
TEST_F(LayerHistoryIntegrationTest,oneLayerExplicitExactVote)528 TEST_F(LayerHistoryIntegrationTest, oneLayerExplicitExactVote) {
529     createLegacyAndFrontedEndLayer(1);
530     setFrameRate(1, 73.4f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,
531                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
532 
533     EXPECT_EQ(1u, layerCount());
534     EXPECT_EQ(0u, activeLayerCount());
535 
536     nsecs_t time = systemTime();
537     updateLayerSnapshotsAndLayerHistory(time);
538     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
539     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitExactOrMultiple,
540               summarizeLayerHistory(time)[0].vote);
541     EXPECT_EQ(73.4_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
542     EXPECT_EQ(1u, activeLayerCount());
543     EXPECT_EQ(1, frequentLayerCount(time));
544 }
545 
TEST_F(LayerHistoryIntegrationTest,oneLayerExplicitExactVote2)546 TEST_F(LayerHistoryIntegrationTest, oneLayerExplicitExactVote2) {
547     auto layer = createLegacyAndFrontedEndLayer(1);
548     setFrameRate(1, 73.4f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,
549                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
550 
551     EXPECT_EQ(1u, layerCount());
552     EXPECT_EQ(0u, activeLayerCount());
553 
554     nsecs_t time = systemTime();
555     updateLayerSnapshotsAndLayerHistory(time);
556 
557     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
558         setBufferWithPresentTime(layer, time);
559         time += HI_FPS_PERIOD;
560     }
561 
562     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
563     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitExactOrMultiple,
564               summarizeLayerHistory(time)[0].vote);
565     EXPECT_EQ(73.4_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
566     EXPECT_EQ(1u, activeLayerCount());
567     EXPECT_EQ(1, frequentLayerCount(time));
568 
569     // layer became infrequent, but the vote stays
570     setDefaultLayerVote(layer.get(), LayerHistory::LayerVoteType::Heuristic);
571     time += MAX_ACTIVE_LAYER_PERIOD_NS.count();
572     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
573     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitExactOrMultiple,
574               summarizeLayerHistory(time)[0].vote);
575     EXPECT_EQ(73.4_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
576     EXPECT_EQ(1u, activeLayerCount());
577     EXPECT_EQ(0, frequentLayerCount(time));
578 }
579 
TEST_F(LayerHistoryIntegrationTest,oneLayerExplicitGte_vrr)580 TEST_F(LayerHistoryIntegrationTest, oneLayerExplicitGte_vrr) {
581     // Set the test to be on a vrr mode.
582     SET_FLAG_FOR_TEST(flags::vrr_config, true);
583     mSelector->setActiveMode(kVrrModeId, HI_FPS);
584 
585     auto layer = createLegacyAndFrontedEndLayer(1);
586     showLayer(1);
587     setFrameRate(1, (33_Hz).getValue(), ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_GTE,
588                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
589     setFrameRateCategory(1, 0);
590 
591     EXPECT_EQ(1u, layerCount());
592     EXPECT_EQ(0u, activeLayerCount());
593 
594     nsecs_t time = systemTime();
595     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
596         setBufferWithPresentTime(layer, time);
597         time += HI_FPS_PERIOD;
598     }
599 
600     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
601     EXPECT_EQ(1u, activeLayerCount());
602     EXPECT_EQ(1, frequentLayerCount(time));
603     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitGte, summarizeLayerHistory(time)[0].vote);
604     EXPECT_EQ(33_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
605     EXPECT_EQ(FrameRateCategory::Default, summarizeLayerHistory(time)[0].frameRateCategory);
606 
607     // layer became inactive, but the vote stays
608     setDefaultLayerVote(layer.get(), LayerHistory::LayerVoteType::Heuristic);
609     time += MAX_ACTIVE_LAYER_PERIOD_NS.count();
610     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
611     EXPECT_EQ(1u, activeLayerCount());
612     EXPECT_EQ(0, frequentLayerCount(time));
613     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitGte, summarizeLayerHistory(time)[0].vote);
614     EXPECT_EQ(33_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
615     EXPECT_EQ(FrameRateCategory::Default, summarizeLayerHistory(time)[0].frameRateCategory);
616 }
617 
618 // Test for MRR device with VRR features enabled.
TEST_F(LayerHistoryIntegrationTest,oneLayerExplicitGte_nonVrr)619 TEST_F(LayerHistoryIntegrationTest, oneLayerExplicitGte_nonVrr) {
620     SET_FLAG_FOR_TEST(flags::frame_rate_category_mrr, true);
621     // The vrr_config flag is explicitly not set false because this test for an MRR device
622     // should still work in a VRR-capable world.
623 
624     auto layer = createLegacyAndFrontedEndLayer(1);
625     showLayer(1);
626     setFrameRate(1, (33_Hz).getValue(), ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_GTE,
627                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
628     setFrameRateCategory(1, 0);
629 
630     EXPECT_EQ(1u, layerCount());
631     EXPECT_EQ(0u, activeLayerCount());
632 
633     nsecs_t time = systemTime();
634     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
635         setBufferWithPresentTime(layer, time);
636         time += HI_FPS_PERIOD;
637     }
638 
639     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
640     EXPECT_EQ(1u, activeLayerCount());
641     EXPECT_EQ(1, frequentLayerCount(time));
642     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
643     EXPECT_EQ(0_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
644     EXPECT_EQ(FrameRateCategory::Default, summarizeLayerHistory(time)[0].frameRateCategory);
645 
646     // layer became infrequent, but the vote stays
647     setDefaultLayerVote(layer.get(), LayerHistory::LayerVoteType::Heuristic);
648     time += MAX_ACTIVE_LAYER_PERIOD_NS.count();
649     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
650     EXPECT_EQ(1u, activeLayerCount());
651     EXPECT_EQ(0, frequentLayerCount(time));
652     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
653     EXPECT_EQ(0_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
654     EXPECT_EQ(FrameRateCategory::Default, summarizeLayerHistory(time)[0].frameRateCategory);
655 }
656 
TEST_F(LayerHistoryIntegrationTest,oneLayerGteNoVote_arr)657 TEST_F(LayerHistoryIntegrationTest, oneLayerGteNoVote_arr) {
658     SET_FLAG_FOR_TEST(flags::arr_setframerate_gte_enum, true);
659     // Set the test to be on a vrr mode.
660     SET_FLAG_FOR_TEST(flags::vrr_config, true);
661     mSelector->setActiveMode(kVrrModeId, HI_FPS);
662 
663     auto layer = createLegacyAndFrontedEndLayer(1);
664     showLayer(1);
665     setFrameRate(1, (0_Hz).getValue(), ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_GTE,
666                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
667 
668     EXPECT_EQ(1u, layerCount());
669     EXPECT_EQ(0u, activeLayerCount());
670 
671     nsecs_t time = systemTime();
672     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
673         setBufferWithPresentTime(layer, time);
674         time += HI_FPS_PERIOD;
675     }
676 
677     // Layer is active but GTE with 0 should be considered NoVote, thus nothing from summarize.
678     ASSERT_EQ(0u, summarizeLayerHistory(time).size());
679     EXPECT_EQ(1u, activeLayerCount());
680     EXPECT_EQ(1, frequentLayerCount(time));
681 
682     // layer became inactive.
683     setDefaultLayerVote(layer.get(), LayerHistory::LayerVoteType::Heuristic);
684     time += MAX_ACTIVE_LAYER_PERIOD_NS.count();
685     ASSERT_EQ(0u, summarizeLayerHistory(time).size());
686     EXPECT_EQ(0u, activeLayerCount());
687     EXPECT_EQ(0, frequentLayerCount(time));
688 }
689 
TEST_F(LayerHistoryIntegrationTest,oneLayerGteNoVote_mrr)690 TEST_F(LayerHistoryIntegrationTest, oneLayerGteNoVote_mrr) {
691     SET_FLAG_FOR_TEST(flags::arr_setframerate_gte_enum, true);
692     // True by default on MRR devices as well, but the device is not set to VRR mode.
693     SET_FLAG_FOR_TEST(flags::vrr_config, true);
694 
695     auto layer = createLegacyAndFrontedEndLayer(1);
696     showLayer(1);
697     setFrameRate(1, (0_Hz).getValue(), ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_GTE,
698                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
699     setFrameRateCategory(1, 0);
700 
701     EXPECT_EQ(1u, layerCount());
702     EXPECT_EQ(0u, activeLayerCount());
703 
704     nsecs_t time = systemTime();
705     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
706         setBufferWithPresentTime(layer, time);
707         time += HI_FPS_PERIOD;
708     }
709 
710     // Layer is active but GTE with 0 should be considered NoVote, thus nothing from summarize.
711     ASSERT_EQ(0u, summarizeLayerHistory(time).size());
712     EXPECT_EQ(1u, activeLayerCount());
713     EXPECT_EQ(1, frequentLayerCount(time));
714 
715     // layer became inactive.
716     setDefaultLayerVote(layer.get(), LayerHistory::LayerVoteType::Heuristic);
717     time += MAX_ACTIVE_LAYER_PERIOD_NS.count();
718     ASSERT_EQ(0u, summarizeLayerHistory(time).size());
719     EXPECT_EQ(0u, activeLayerCount());
720     EXPECT_EQ(0, frequentLayerCount(time));
721 }
722 
TEST_F(LayerHistoryIntegrationTest,oneLayerExplicitVoteWithCategory_vrrFeatureOff)723 TEST_F(LayerHistoryIntegrationTest, oneLayerExplicitVoteWithCategory_vrrFeatureOff) {
724     SET_FLAG_FOR_TEST(flags::frame_rate_category_mrr, false);
725 
726     auto layer = createLegacyAndFrontedEndLayer(1);
727     showLayer(1);
728     setFrameRate(1, (73.4_Hz).getValue(), ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT,
729                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
730     setFrameRateCategory(1, ANATIVEWINDOW_FRAME_RATE_CATEGORY_HIGH);
731 
732     // Set default to Min so it is obvious that the vote reset triggered.
733     setDefaultLayerVote(layer.get(), LayerHistory::LayerVoteType::Min);
734 
735     EXPECT_EQ(1u, layerCount());
736     EXPECT_EQ(0u, activeLayerCount());
737 
738     nsecs_t time = systemTime();
739     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
740         setBufferWithPresentTime(layer, time);
741         time += HI_FPS_PERIOD;
742     }
743 
744     // There is only 1 LayerRequirement due to the disabled flag frame_rate_category_mrr.
745     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
746     EXPECT_EQ(1u, activeLayerCount());
747     EXPECT_EQ(1, frequentLayerCount(time));
748     EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
749     EXPECT_EQ(0_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
750     EXPECT_EQ(FrameRateCategory::Default, summarizeLayerHistory(time)[0].frameRateCategory);
751 }
752 
753 // This test case should be the same as oneLayerNoVote except instead of layer vote is NoVote,
754 // the category is NoPreference.
TEST_F(LayerHistoryIntegrationTest,oneLayerCategoryNoPreference)755 TEST_F(LayerHistoryIntegrationTest, oneLayerCategoryNoPreference) {
756     SET_FLAG_FOR_TEST(flags::frame_rate_category_mrr, true);
757 
758     auto layer = createLegacyAndFrontedEndLayer(1);
759     showLayer(1);
760     setFrameRate(1, (0_Hz).getValue(), ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT,
761                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
762     setFrameRateCategory(1, ANATIVEWINDOW_FRAME_RATE_CATEGORY_NO_PREFERENCE);
763 
764     EXPECT_EQ(1u, layerCount());
765     EXPECT_EQ(0u, activeLayerCount());
766 
767     nsecs_t time = systemTime();
768     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
769         setBufferWithPresentTime(layer, time);
770         time += HI_FPS_PERIOD;
771     }
772 
773     EXPECT_EQ(1u, summarizeLayerHistory(time).size());
774     EXPECT_EQ(1u, activeLayerCount());
775     EXPECT_EQ(1, frequentLayerCount(time));
776 
777     // layer became infrequent
778     time += MAX_ACTIVE_LAYER_PERIOD_NS.count();
779     EXPECT_EQ(1u, summarizeLayerHistory(time).size());
780     EXPECT_EQ(1u, activeLayerCount());
781     EXPECT_EQ(0, frequentLayerCount(time));
782 }
783 
784 // Tests MRR NoPreference-only vote, no game default override. Expects vote reset.
TEST_F(LayerHistoryIntegrationTest,oneLayerCategoryNoPreference_mrr)785 TEST_F(LayerHistoryIntegrationTest, oneLayerCategoryNoPreference_mrr) {
786     SET_FLAG_FOR_TEST(flags::frame_rate_category_mrr, false);
787     SET_FLAG_FOR_TEST(flags::game_default_frame_rate, true);
788     SET_FLAG_FOR_TEST(flags::vrr_config, true);
789 
790     const LayerHistory::LayerVoteType defaultVote = LayerHistory::LayerVoteType::Min;
791 
792     auto layer = createLegacyAndFrontedEndLayer(1);
793     setDefaultLayerVote(layer.get(), defaultVote);
794     showLayer(1);
795     setFrameRate(1, (0_Hz).getValue(), ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT,
796                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
797     setFrameRateCategory(1, ANATIVEWINDOW_FRAME_RATE_CATEGORY_NO_PREFERENCE);
798 
799     EXPECT_EQ(1u, layerCount());
800     EXPECT_EQ(0u, activeLayerCount());
801 
802     nsecs_t time = systemTime();
803     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
804         setBufferWithPresentTime(layer, time);
805         time += HI_FPS_PERIOD;
806     }
807 
808     EXPECT_EQ(1u, summarizeLayerHistory(time).size());
809     EXPECT_EQ(1u, activeLayerCount());
810     EXPECT_EQ(1, frequentLayerCount(time));
811     EXPECT_EQ(defaultVote, summarizeLayerHistory(time)[0].vote);
812     EXPECT_EQ(0_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
813     EXPECT_EQ(FrameRateCategory::Default, summarizeLayerHistory(time)[0].frameRateCategory);
814 }
815 
816 // Tests VRR NoPreference-only vote, no game default override. Expects NoPreference, *not* vote
817 // reset.
TEST_F(LayerHistoryIntegrationTest,oneLayerCategoryNoPreference_vrr)818 TEST_F(LayerHistoryIntegrationTest, oneLayerCategoryNoPreference_vrr) {
819     SET_FLAG_FOR_TEST(flags::frame_rate_category_mrr, false);
820     SET_FLAG_FOR_TEST(flags::game_default_frame_rate, true);
821     SET_FLAG_FOR_TEST(flags::vrr_config, true);
822     mSelector->setActiveMode(kVrrModeId, HI_FPS);
823 
824     const LayerHistory::LayerVoteType defaultVote = LayerHistory::LayerVoteType::Min;
825 
826     auto layer = createLegacyAndFrontedEndLayer(1);
827     setDefaultLayerVote(layer.get(), defaultVote);
828     showLayer(1);
829     setFrameRate(1, (0_Hz).getValue(), ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT,
830                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
831     setFrameRateCategory(1, ANATIVEWINDOW_FRAME_RATE_CATEGORY_NO_PREFERENCE);
832 
833     EXPECT_EQ(1u, layerCount());
834     EXPECT_EQ(0u, activeLayerCount());
835 
836     nsecs_t time = systemTime();
837     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
838         setBufferWithPresentTime(layer, time);
839         time += HI_FPS_PERIOD;
840     }
841 
842     EXPECT_EQ(1u, summarizeLayerHistory(time).size());
843     EXPECT_EQ(1u, activeLayerCount());
844     EXPECT_EQ(1, frequentLayerCount(time));
845     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitCategory, summarizeLayerHistory(time)[0].vote);
846     EXPECT_EQ(0_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
847     EXPECT_EQ(FrameRateCategory::NoPreference, summarizeLayerHistory(time)[0].frameRateCategory);
848 }
849 
TEST_F(LayerHistoryIntegrationTest,oneLayerCategoryNoPreferenceWithGameDefault_vrr)850 TEST_F(LayerHistoryIntegrationTest, oneLayerCategoryNoPreferenceWithGameDefault_vrr) {
851     SET_FLAG_FOR_TEST(flags::frame_rate_category_mrr, false);
852     SET_FLAG_FOR_TEST(flags::game_default_frame_rate, true);
853     SET_FLAG_FOR_TEST(flags::vrr_config, true);
854     mSelector->setActiveMode(kVrrModeId, HI_FPS);
855 
856     const Fps gameDefaultFrameRate = Fps::fromValue(30.0f);
857     const uid_t uid = 456;
858 
859     history().updateGameDefaultFrameRateOverride(
860             FrameRateOverride({uid, gameDefaultFrameRate.getValue()}));
861 
862     auto layer = createLegacyAndFrontedEndLayerWithUid(1, gui::Uid(uid));
863     showLayer(1);
864     setFrameRate(1, (0_Hz).getValue(), ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT,
865                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
866     setFrameRateCategory(1, ANATIVEWINDOW_FRAME_RATE_CATEGORY_NO_PREFERENCE);
867 
868     EXPECT_EQ(1u, layerCount());
869     EXPECT_EQ(0u, activeLayerCount());
870 
871     nsecs_t time = systemTime();
872     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
873         setBufferWithPresentTime(layer, time);
874         time += HI_FPS_PERIOD;
875     }
876 
877     EXPECT_EQ(1u, summarizeLayerHistory(time).size());
878     EXPECT_EQ(1u, activeLayerCount());
879     EXPECT_EQ(1, frequentLayerCount(time));
880     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitDefault, summarizeLayerHistory(time)[0].vote);
881     EXPECT_EQ(gameDefaultFrameRate, summarizeLayerHistory(time)[0].desiredRefreshRate);
882     EXPECT_EQ(FrameRateCategory::Default, summarizeLayerHistory(time)[0].frameRateCategory);
883 }
884 
TEST_F(LayerHistoryIntegrationTest,oneLayerCategoryNoPreferenceWithGameDefault_mrr)885 TEST_F(LayerHistoryIntegrationTest, oneLayerCategoryNoPreferenceWithGameDefault_mrr) {
886     SET_FLAG_FOR_TEST(flags::frame_rate_category_mrr, false);
887     SET_FLAG_FOR_TEST(flags::game_default_frame_rate, true);
888     SET_FLAG_FOR_TEST(flags::vrr_config, true);
889 
890     const Fps gameDefaultFrameRate = Fps::fromValue(30.0f);
891     const uid_t uid = 456;
892 
893     history().updateGameDefaultFrameRateOverride(
894             FrameRateOverride({uid, gameDefaultFrameRate.getValue()}));
895 
896     auto layer = createLegacyAndFrontedEndLayerWithUid(1, gui::Uid(uid));
897     showLayer(1);
898     setFrameRate(1, (0_Hz).getValue(), ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT,
899                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
900     setFrameRateCategory(1, ANATIVEWINDOW_FRAME_RATE_CATEGORY_NO_PREFERENCE);
901 
902     EXPECT_EQ(1u, layerCount());
903     EXPECT_EQ(0u, activeLayerCount());
904 
905     nsecs_t time = systemTime();
906     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
907         setBufferWithPresentTime(layer, time);
908         time += HI_FPS_PERIOD;
909     }
910 
911     EXPECT_EQ(1u, summarizeLayerHistory(time).size());
912     EXPECT_EQ(1u, activeLayerCount());
913     EXPECT_EQ(1, frequentLayerCount(time));
914     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitDefault, summarizeLayerHistory(time)[0].vote);
915     EXPECT_EQ(gameDefaultFrameRate, summarizeLayerHistory(time)[0].desiredRefreshRate);
916     EXPECT_EQ(FrameRateCategory::Default, summarizeLayerHistory(time)[0].frameRateCategory);
917 }
918 
TEST_F(LayerHistoryIntegrationTest,oneLayerNoVoteWithGameDefault_vrr)919 TEST_F(LayerHistoryIntegrationTest, oneLayerNoVoteWithGameDefault_vrr) {
920     SET_FLAG_FOR_TEST(flags::frame_rate_category_mrr, false);
921     SET_FLAG_FOR_TEST(flags::game_default_frame_rate, true);
922     SET_FLAG_FOR_TEST(flags::vrr_config, true);
923     mSelector->setActiveMode(kVrrModeId, HI_FPS);
924 
925     const Fps gameDefaultFrameRate = Fps::fromValue(30.0f);
926     const uid_t uid = 456;
927 
928     history().updateGameDefaultFrameRateOverride(
929             FrameRateOverride({uid, gameDefaultFrameRate.getValue()}));
930 
931     auto layer = createLegacyAndFrontedEndLayerWithUid(1, gui::Uid(uid));
932     showLayer(1);
933     setFrameRate(1, (0_Hz).getValue(), ANATIVEWINDOW_FRAME_RATE_NO_VOTE,
934                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
935 
936     EXPECT_EQ(1u, layerCount());
937     EXPECT_EQ(0u, activeLayerCount());
938 
939     nsecs_t time = systemTime();
940     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
941         setBufferWithPresentTime(layer, time);
942         time += HI_FPS_PERIOD;
943     }
944 
945     // Expect NoVote to be skipped in summarize.
946     EXPECT_EQ(0u, summarizeLayerHistory(time).size());
947     EXPECT_EQ(1u, activeLayerCount());
948     EXPECT_EQ(1, frequentLayerCount(time));
949 }
950 
TEST_F(LayerHistoryIntegrationTest,oneLayerNoVoteWithGameDefault_mrr)951 TEST_F(LayerHistoryIntegrationTest, oneLayerNoVoteWithGameDefault_mrr) {
952     SET_FLAG_FOR_TEST(flags::frame_rate_category_mrr, false);
953     SET_FLAG_FOR_TEST(flags::game_default_frame_rate, true);
954     SET_FLAG_FOR_TEST(flags::vrr_config, true);
955 
956     const Fps gameDefaultFrameRate = Fps::fromValue(30.0f);
957     const uid_t uid = 456;
958 
959     history().updateGameDefaultFrameRateOverride(
960             FrameRateOverride({uid, gameDefaultFrameRate.getValue()}));
961 
962     auto layer = createLegacyAndFrontedEndLayerWithUid(1, gui::Uid(uid));
963     showLayer(1);
964     setFrameRate(1, (0_Hz).getValue(), ANATIVEWINDOW_FRAME_RATE_NO_VOTE,
965                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
966 
967     EXPECT_EQ(1u, layerCount());
968     EXPECT_EQ(0u, activeLayerCount());
969 
970     nsecs_t time = systemTime();
971     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
972         setBufferWithPresentTime(layer, time);
973         time += HI_FPS_PERIOD;
974     }
975 
976     // Expect NoVote to be skipped in summarize.
977     EXPECT_EQ(0u, summarizeLayerHistory(time).size());
978     EXPECT_EQ(1u, activeLayerCount());
979     EXPECT_EQ(1, frequentLayerCount(time));
980 }
981 
TEST_F(LayerHistoryIntegrationTest,oneLayerExplicitVoteWithCategory)982 TEST_F(LayerHistoryIntegrationTest, oneLayerExplicitVoteWithCategory) {
983     SET_FLAG_FOR_TEST(flags::frame_rate_category_mrr, true);
984 
985     auto layer = createLegacyAndFrontedEndLayer(1);
986     showLayer(1);
987     setFrameRate(1, (73.4_Hz).getValue(), ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT,
988                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
989     setFrameRateCategory(1, ANATIVEWINDOW_FRAME_RATE_CATEGORY_HIGH);
990 
991     EXPECT_EQ(1u, layerCount());
992     EXPECT_EQ(0u, activeLayerCount());
993 
994     nsecs_t time = systemTime();
995     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
996         setBufferWithPresentTime(layer, time);
997         time += HI_FPS_PERIOD;
998     }
999 
1000     // There are 2 LayerRequirement's due to the frame rate category.
1001     ASSERT_EQ(2u, summarizeLayerHistory(time).size());
1002     EXPECT_EQ(1u, activeLayerCount());
1003     EXPECT_EQ(1, frequentLayerCount(time));
1004     // First LayerRequirement is the layer's category specification
1005     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitCategory, summarizeLayerHistory(time)[0].vote);
1006     EXPECT_EQ(0_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
1007     EXPECT_EQ(FrameRateCategory::High, summarizeLayerHistory(time)[0].frameRateCategory);
1008 
1009     // Second LayerRequirement is the frame rate specification
1010     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitDefault, summarizeLayerHistory(time)[1].vote);
1011     EXPECT_EQ(73.4_Hz, summarizeLayerHistory(time)[1].desiredRefreshRate);
1012     EXPECT_EQ(FrameRateCategory::Default, summarizeLayerHistory(time)[1].frameRateCategory);
1013 
1014     // layer became infrequent, but the vote stays
1015     setDefaultLayerVote(layer.get(), LayerHistory::LayerVoteType::Heuristic);
1016     time += MAX_ACTIVE_LAYER_PERIOD_NS.count();
1017     ASSERT_EQ(2u, summarizeLayerHistory(time).size());
1018     EXPECT_EQ(1u, activeLayerCount());
1019     EXPECT_EQ(0, frequentLayerCount(time));
1020     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitCategory, summarizeLayerHistory(time)[0].vote);
1021     EXPECT_EQ(0_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
1022     EXPECT_EQ(FrameRateCategory::High, summarizeLayerHistory(time)[0].frameRateCategory);
1023 }
1024 
TEST_F(LayerHistoryIntegrationTest,oneLayerExplicitVoteWithCategoryNotVisibleDoesNotVote)1025 TEST_F(LayerHistoryIntegrationTest, oneLayerExplicitVoteWithCategoryNotVisibleDoesNotVote) {
1026     SET_FLAG_FOR_TEST(flags::misc1, true);
1027 
1028     auto layer = createLegacyAndFrontedEndLayer(1);
1029     hideLayer(1);
1030     setFrameRate(1, (12.34_Hz).getValue(), ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT,
1031                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
1032     setFrameRateCategory(1, ANATIVEWINDOW_FRAME_RATE_CATEGORY_HIGH);
1033 
1034     EXPECT_EQ(1u, layerCount());
1035     EXPECT_EQ(0u, activeLayerCount());
1036 
1037     nsecs_t time = systemTime();
1038     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
1039         setBufferWithPresentTime(layer, time);
1040         time += HI_FPS_PERIOD;
1041     }
1042 
1043     // Layer is not visible, so the layer is moved to inactive, infrequent, and it will not have
1044     // votes to consider for refresh rate selection.
1045     ASSERT_EQ(0u, summarizeLayerHistory(time).size());
1046     EXPECT_EQ(0u, activeLayerCount());
1047     EXPECT_EQ(0, frequentLayerCount(time));
1048 }
1049 
TEST_F(LayerHistoryIntegrationTest,invisibleExplicitLayer)1050 TEST_F(LayerHistoryIntegrationTest, invisibleExplicitLayer) {
1051     SET_FLAG_FOR_TEST(flags::misc1, false);
1052 
1053     auto explicitVisiblelayer = createLegacyAndFrontedEndLayer(1);
1054     showLayer(1);
1055     setFrameRate(1, (60_Hz).getValue(), ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE, 0);
1056 
1057     auto explicitInvisiblelayer = createLegacyAndFrontedEndLayer(2);
1058     hideLayer(2);
1059     setFrameRate(2, (90_Hz).getValue(), ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE, 0);
1060 
1061     nsecs_t time = systemTime();
1062 
1063     // Post a buffer to the layers to make them active
1064     setBuffer(1);
1065     setBuffer(2);
1066     updateLayerSnapshotsAndLayerHistory(time);
1067 
1068     EXPECT_EQ(2u, layerCount());
1069     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1070     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitExactOrMultiple,
1071               summarizeLayerHistory(time)[0].vote);
1072     EXPECT_EQ(60_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
1073     EXPECT_EQ(2u, activeLayerCount());
1074     EXPECT_EQ(2, frequentLayerCount(time));
1075 }
1076 
TEST_F(LayerHistoryIntegrationTest,invisibleExplicitLayerDoesNotVote)1077 TEST_F(LayerHistoryIntegrationTest, invisibleExplicitLayerDoesNotVote) {
1078     SET_FLAG_FOR_TEST(flags::misc1, true);
1079 
1080     auto explicitVisiblelayer = createLegacyAndFrontedEndLayer(1);
1081     showLayer(1);
1082     setFrameRate(1, (60_Hz).getValue(), ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE, 0);
1083 
1084     auto explicitInvisiblelayer = createLegacyAndFrontedEndLayer(2);
1085     hideLayer(2);
1086     setFrameRate(2, (90_Hz).getValue(), ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE, 0);
1087 
1088     nsecs_t time = systemTime();
1089 
1090     // Post a buffer to the layers to make them active
1091     setBuffer(1);
1092     setBuffer(2);
1093     updateLayerSnapshotsAndLayerHistory(time);
1094 
1095     EXPECT_EQ(2u, layerCount());
1096     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1097     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitExactOrMultiple,
1098               summarizeLayerHistory(time)[0].vote);
1099     EXPECT_EQ(60_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
1100     EXPECT_EQ(1u, activeLayerCount());
1101     EXPECT_EQ(1, frequentLayerCount(time));
1102 }
1103 
TEST_F(LayerHistoryIntegrationTest,frontBufferedLayerVotesMax)1104 TEST_F(LayerHistoryIntegrationTest, frontBufferedLayerVotesMax) {
1105     SET_FLAG_FOR_TEST(flags::vrr_config, true);
1106 
1107     auto layer = createLegacyAndFrontedEndLayer(1);
1108     setFrontBuffer(1);
1109     showLayer(1);
1110 
1111     nsecs_t time = systemTime();
1112 
1113     EXPECT_EQ(1u, layerCount());
1114     EXPECT_EQ(0u, activeLayerCount());
1115     EXPECT_EQ(0, frequentLayerCount(time));
1116     EXPECT_EQ(0, animatingLayerCount(time));
1117 
1118     // layer is active but infrequent.
1119     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
1120         setFrontBufferWithPresentTime(layer, time);
1121         time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
1122     }
1123 
1124     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1125     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
1126     EXPECT_EQ(1u, activeLayerCount());
1127     EXPECT_EQ(0, frequentLayerCount(time));
1128     EXPECT_EQ(0, animatingLayerCount(time));
1129 
1130     // Layer still active due to front buffering, but it's infrequent.
1131     time += MAX_ACTIVE_LAYER_PERIOD_NS.count();
1132     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1133     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
1134     EXPECT_EQ(1u, activeLayerCount());
1135     EXPECT_EQ(0, frequentLayerCount(time));
1136     EXPECT_EQ(0, animatingLayerCount(time));
1137 }
1138 
TEST_F(LayerHistoryIntegrationTest,oneLayerExplicitCategory)1139 TEST_F(LayerHistoryIntegrationTest, oneLayerExplicitCategory) {
1140     SET_FLAG_FOR_TEST(flags::frame_rate_category_mrr, true);
1141 
1142     createLegacyAndFrontedEndLayer(1);
1143     setFrameRateCategory(1, ANATIVEWINDOW_FRAME_RATE_CATEGORY_HIGH);
1144 
1145     EXPECT_EQ(1u, layerCount());
1146     EXPECT_EQ(0u, activeLayerCount());
1147 
1148     nsecs_t time = systemTime();
1149     updateLayerSnapshotsAndLayerHistory(time);
1150     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1151     EXPECT_EQ(1u, activeLayerCount());
1152     EXPECT_EQ(1, frequentLayerCount(time));
1153     // First LayerRequirement is the frame rate specification
1154     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitCategory, summarizeLayerHistory(time)[0].vote);
1155     EXPECT_EQ(0_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
1156     EXPECT_EQ(FrameRateCategory::High, summarizeLayerHistory(time)[0].frameRateCategory);
1157 }
1158 
TEST_F(LayerHistoryIntegrationTest,oneLayerExplicitVoteWithFixedSourceAndNoPreferenceCategory)1159 TEST_F(LayerHistoryIntegrationTest, oneLayerExplicitVoteWithFixedSourceAndNoPreferenceCategory) {
1160     SET_FLAG_FOR_TEST(flags::frame_rate_category_mrr, false);
1161 
1162     auto layer = createLegacyAndFrontedEndLayer(1);
1163     setFrameRate(1, (45.6_Hz).getValue(), ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,
1164                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
1165     setFrameRateCategory(1, ANATIVEWINDOW_FRAME_RATE_CATEGORY_NO_PREFERENCE);
1166 
1167     EXPECT_EQ(1u, layerCount());
1168     EXPECT_EQ(0u, activeLayerCount());
1169 
1170     nsecs_t time = systemTime();
1171     updateLayerSnapshotsAndLayerHistory(time);
1172     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
1173         setBufferWithPresentTime(layer, time);
1174         time += HI_FPS_PERIOD;
1175     }
1176 
1177     // There are 2 LayerRequirement's due to the frame rate category.
1178     ASSERT_EQ(2u, summarizeLayerHistory(time).size());
1179     EXPECT_EQ(1u, activeLayerCount());
1180     EXPECT_EQ(1, frequentLayerCount(time));
1181     // First LayerRequirement is the layer's category specification
1182     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitCategory, summarizeLayerHistory(time)[0].vote);
1183     EXPECT_EQ(0_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
1184     EXPECT_EQ(FrameRateCategory::NoPreference, summarizeLayerHistory(time)[0].frameRateCategory);
1185 
1186     // Second LayerRequirement is the frame rate specification
1187     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitExactOrMultiple,
1188               summarizeLayerHistory(time)[1].vote);
1189     EXPECT_EQ(45.6_Hz, summarizeLayerHistory(time)[1].desiredRefreshRate);
1190     EXPECT_EQ(FrameRateCategory::Default, summarizeLayerHistory(time)[1].frameRateCategory);
1191 
1192     // layer became infrequent, but the vote stays
1193     time += MAX_ACTIVE_LAYER_PERIOD_NS.count();
1194     ASSERT_EQ(2u, summarizeLayerHistory(time).size());
1195     EXPECT_EQ(1u, activeLayerCount());
1196     EXPECT_EQ(0, frequentLayerCount(time));
1197     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitCategory, summarizeLayerHistory(time)[0].vote);
1198     EXPECT_EQ(0_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
1199     EXPECT_EQ(FrameRateCategory::NoPreference, summarizeLayerHistory(time)[0].frameRateCategory);
1200 }
1201 
TEST_F(LayerHistoryIntegrationTest,multipleLayers)1202 TEST_F(LayerHistoryIntegrationTest, multipleLayers) {
1203     auto layer1 = createLegacyAndFrontedEndLayer(1);
1204     auto layer2 = createLegacyAndFrontedEndLayer(2);
1205     auto layer3 = createLegacyAndFrontedEndLayer(3);
1206 
1207     nsecs_t time = systemTime();
1208 
1209     EXPECT_EQ(3u, layerCount());
1210     EXPECT_EQ(0u, activeLayerCount());
1211     EXPECT_EQ(0, frequentLayerCount(time));
1212 
1213     LayerHistory::Summary summary;
1214 
1215     // layer1 is active but infrequent.
1216     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
1217         setBufferWithPresentTime(layer1, time);
1218         time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
1219         summary = summarizeLayerHistory(time);
1220     }
1221 
1222     ASSERT_EQ(1u, summary.size());
1223     EXPECT_EQ(LayerHistory::LayerVoteType::Min, summary[0].vote);
1224     EXPECT_EQ(1u, activeLayerCount());
1225     EXPECT_EQ(0, frequentLayerCount(time));
1226 
1227     // layer2 is frequent and has high refresh rate.
1228     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
1229         setBufferWithPresentTime(layer2, time);
1230         time += HI_FPS_PERIOD;
1231         summary = summarizeLayerHistory(time);
1232     }
1233 
1234     // layer1 is still active but infrequent.
1235     setBufferWithPresentTime(layer1, time);
1236 
1237     ASSERT_EQ(2u, summary.size());
1238     EXPECT_EQ(LayerHistory::LayerVoteType::Min, summary[0].vote);
1239     ASSERT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[1].vote);
1240     EXPECT_EQ(HI_FPS, summarizeLayerHistory(time)[1].desiredRefreshRate);
1241 
1242     EXPECT_EQ(2u, activeLayerCount());
1243     EXPECT_EQ(1, frequentLayerCount(time));
1244 
1245     // layer1 is no longer active.
1246     // layer2 is frequent and has low refresh rate.
1247     for (size_t i = 0; i < 2 * PRESENT_TIME_HISTORY_SIZE; i++) {
1248         setBufferWithPresentTime(layer2, time);
1249         time += LO_FPS_PERIOD;
1250         summary = summarizeLayerHistory(time);
1251     }
1252 
1253     ASSERT_EQ(1u, summary.size());
1254     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
1255     EXPECT_EQ(LO_FPS, summary[0].desiredRefreshRate);
1256     EXPECT_EQ(1u, activeLayerCount());
1257     EXPECT_EQ(1, frequentLayerCount(time));
1258 
1259     // layer2 still has low refresh rate.
1260     // layer3 has high refresh rate but not enough history.
1261     constexpr int RATIO = LO_FPS_PERIOD / HI_FPS_PERIOD;
1262     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE - 1; i++) {
1263         if (i % RATIO == 0) {
1264             setBufferWithPresentTime(layer2, time);
1265         }
1266 
1267         setBufferWithPresentTime(layer3, time);
1268         time += HI_FPS_PERIOD;
1269         summary = summarizeLayerHistory(time);
1270     }
1271 
1272     ASSERT_EQ(2u, summary.size());
1273     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
1274     EXPECT_EQ(LO_FPS, summary[0].desiredRefreshRate);
1275     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summary[1].vote);
1276     EXPECT_EQ(2u, activeLayerCount());
1277     EXPECT_EQ(2, frequentLayerCount(time));
1278 
1279     // layer3 becomes recently active.
1280     setBufferWithPresentTime(layer3, time);
1281     summary = summarizeLayerHistory(time);
1282     ASSERT_EQ(2u, summary.size());
1283     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
1284     EXPECT_EQ(LO_FPS, summary[0].desiredRefreshRate);
1285     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[1].vote);
1286     EXPECT_EQ(HI_FPS, summary[1].desiredRefreshRate);
1287     EXPECT_EQ(2u, activeLayerCount());
1288     EXPECT_EQ(2, frequentLayerCount(time));
1289 
1290     // layer1 expires.
1291     destroyLayer(layer1);
1292     updateLayerSnapshotsAndLayerHistory(time);
1293 
1294     summary = summarizeLayerHistory(time);
1295     ASSERT_EQ(2u, summary.size());
1296     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
1297     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
1298     EXPECT_EQ(LO_FPS, summary[0].desiredRefreshRate);
1299     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[1].vote);
1300     EXPECT_EQ(HI_FPS, summary[1].desiredRefreshRate);
1301     EXPECT_EQ(2u, layerCount());
1302     EXPECT_EQ(2u, activeLayerCount());
1303     EXPECT_EQ(2, frequentLayerCount(time));
1304 
1305     // layer2 still has low refresh rate.
1306     // layer3 becomes inactive.
1307     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
1308         setBufferWithPresentTime(layer2, time);
1309         time += LO_FPS_PERIOD;
1310         summary = summarizeLayerHistory(time);
1311     }
1312 
1313     ASSERT_EQ(1u, summary.size());
1314     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
1315     EXPECT_EQ(LO_FPS, summary[0].desiredRefreshRate);
1316     EXPECT_EQ(1u, activeLayerCount());
1317     EXPECT_EQ(1, frequentLayerCount(time));
1318 
1319     // layer2 expires.
1320     destroyLayer(layer2);
1321     updateLayerSnapshotsAndLayerHistory(time);
1322     summary = summarizeLayerHistory(time);
1323     EXPECT_TRUE(summary.empty());
1324     EXPECT_EQ(1u, layerCount());
1325     EXPECT_EQ(0u, activeLayerCount());
1326     EXPECT_EQ(0, frequentLayerCount(time));
1327 
1328     // layer3 becomes active and has high refresh rate.
1329     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE + FREQUENT_LAYER_WINDOW_SIZE + 1; i++) {
1330         setBufferWithPresentTime(layer3, time);
1331         time += HI_FPS_PERIOD;
1332         summary = summarizeLayerHistory(time);
1333     }
1334 
1335     ASSERT_EQ(1u, summary.size());
1336     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
1337     EXPECT_EQ(HI_FPS, summary[0].desiredRefreshRate);
1338     EXPECT_EQ(1u, layerCount());
1339     EXPECT_EQ(1u, activeLayerCount());
1340     EXPECT_EQ(1, frequentLayerCount(time));
1341 
1342     // layer3 expires.
1343     destroyLayer(layer3);
1344     updateLayerSnapshotsAndLayerHistory(time);
1345     summary = summarizeLayerHistory(time);
1346     EXPECT_TRUE(summary.empty());
1347     EXPECT_EQ(0u, layerCount());
1348     EXPECT_EQ(0u, activeLayerCount());
1349     EXPECT_EQ(0, frequentLayerCount(time));
1350 }
1351 
TEST_F(LayerHistoryIntegrationTest,inactiveLayers)1352 TEST_F(LayerHistoryIntegrationTest, inactiveLayers) {
1353     auto layer = createLegacyAndFrontedEndLayer(1);
1354     nsecs_t time = systemTime();
1355 
1356     // the very first updates makes the layer frequent
1357     for (size_t i = 0; i < FREQUENT_LAYER_WINDOW_SIZE - 1; i++) {
1358         setBufferWithPresentTime(layer, time);
1359         time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
1360 
1361         EXPECT_EQ(1u, layerCount());
1362         ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1363         EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
1364         EXPECT_EQ(1u, activeLayerCount());
1365         EXPECT_EQ(1, frequentLayerCount(time));
1366     }
1367 
1368     // the next update with the MAX_FREQUENT_LAYER_PERIOD_NS will get us to infrequent
1369     time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
1370     setBufferWithPresentTime(layer, time);
1371 
1372     EXPECT_EQ(1u, layerCount());
1373     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1374     EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
1375     EXPECT_EQ(1u, activeLayerCount());
1376     EXPECT_EQ(0, frequentLayerCount(time));
1377 
1378     // advance the time for the previous frame to be inactive
1379     time += MAX_ACTIVE_LAYER_PERIOD_NS.count();
1380 
1381     // Now even if we post a quick few frame we should stay infrequent
1382     for (size_t i = 0; i < FREQUENT_LAYER_WINDOW_SIZE - 1; i++) {
1383         setBufferWithPresentTime(layer, time);
1384         time += HI_FPS_PERIOD;
1385 
1386         EXPECT_EQ(1u, layerCount());
1387         ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1388         EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
1389         EXPECT_EQ(1u, activeLayerCount());
1390         EXPECT_EQ(0, frequentLayerCount(time));
1391     }
1392 
1393     // More quick frames will get us to frequent again
1394     setBufferWithPresentTime(layer, time);
1395     time += HI_FPS_PERIOD;
1396 
1397     EXPECT_EQ(1u, layerCount());
1398     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1399     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
1400     EXPECT_EQ(1u, activeLayerCount());
1401     EXPECT_EQ(1, frequentLayerCount(time));
1402 }
1403 
TEST_F(LayerHistoryIntegrationTest,invisibleExplicitLayerIsActive)1404 TEST_F(LayerHistoryIntegrationTest, invisibleExplicitLayerIsActive) {
1405     SET_FLAG_FOR_TEST(flags::misc1, false);
1406 
1407     auto explicitVisiblelayer = createLegacyAndFrontedEndLayer(1);
1408     auto explicitInvisiblelayer = createLegacyAndFrontedEndLayer(2);
1409     hideLayer(2);
1410     setFrameRate(1, 60.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,
1411                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
1412     setFrameRate(2, 90.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,
1413                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
1414     nsecs_t time = systemTime();
1415 
1416     // Post a buffer to the layers to make them active
1417     setBufferWithPresentTime(explicitVisiblelayer, time);
1418     setBufferWithPresentTime(explicitInvisiblelayer, time);
1419 
1420     EXPECT_EQ(2u, layerCount());
1421     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1422     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitExactOrMultiple,
1423               summarizeLayerHistory(time)[0].vote);
1424     EXPECT_EQ(60_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
1425     EXPECT_EQ(2u, activeLayerCount());
1426     EXPECT_EQ(2, frequentLayerCount(time));
1427 }
1428 
TEST_F(LayerHistoryIntegrationTest,invisibleExplicitLayerIsNotActive)1429 TEST_F(LayerHistoryIntegrationTest, invisibleExplicitLayerIsNotActive) {
1430     SET_FLAG_FOR_TEST(flags::misc1, true);
1431 
1432     auto explicitVisiblelayer = createLegacyAndFrontedEndLayer(1);
1433     auto explicitInvisiblelayer = createLegacyAndFrontedEndLayer(2);
1434     hideLayer(2);
1435     setFrameRate(1, 60.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,
1436                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
1437     setFrameRate(2, 90.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,
1438                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
1439     nsecs_t time = systemTime();
1440 
1441     // Post a buffer to the layers to make them active
1442     setBufferWithPresentTime(explicitVisiblelayer, time);
1443     setBufferWithPresentTime(explicitInvisiblelayer, time);
1444 
1445     EXPECT_EQ(2u, layerCount());
1446     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1447     EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitExactOrMultiple,
1448               summarizeLayerHistory(time)[0].vote);
1449     EXPECT_EQ(60_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
1450     EXPECT_EQ(1u, activeLayerCount());
1451     EXPECT_EQ(1, frequentLayerCount(time));
1452 }
1453 
TEST_F(LayerHistoryIntegrationTest,infrequentAnimatingLayer)1454 TEST_F(LayerHistoryIntegrationTest, infrequentAnimatingLayer) {
1455     auto layer = createLegacyAndFrontedEndLayer(1);
1456 
1457     nsecs_t time = systemTime();
1458 
1459     EXPECT_EQ(1u, layerCount());
1460     EXPECT_EQ(0u, activeLayerCount());
1461     EXPECT_EQ(0, frequentLayerCount(time));
1462     EXPECT_EQ(0, animatingLayerCount(time));
1463 
1464     // layer is active but infrequent.
1465     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
1466         setBufferWithPresentTime(layer, time);
1467         time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
1468     }
1469 
1470     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1471     EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
1472     EXPECT_EQ(1u, activeLayerCount());
1473     EXPECT_EQ(0, frequentLayerCount(time));
1474     EXPECT_EQ(0, animatingLayerCount(time));
1475 
1476     // another update with the same cadence keep in infrequent
1477     setBufferWithPresentTime(layer, time);
1478     time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
1479 
1480     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1481     EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
1482     EXPECT_EQ(1u, activeLayerCount());
1483     EXPECT_EQ(0, frequentLayerCount(time));
1484     EXPECT_EQ(0, animatingLayerCount(time));
1485 
1486     mFlinger.mutableLayerSnapshotBuilder().getSnapshot(1)->changes |=
1487             frontend::RequestedLayerState::Changes::Animation;
1488     mFlinger.updateLayerHistory(time);
1489     // an update as animation will immediately vote for Max
1490     time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
1491 
1492     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1493     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
1494     EXPECT_EQ(1u, activeLayerCount());
1495     EXPECT_EQ(0, frequentLayerCount(time));
1496     EXPECT_EQ(1, animatingLayerCount(time));
1497 }
1498 
TEST_F(LayerHistoryIntegrationTest,frequentLayerBecomingInfrequentAndBack)1499 TEST_F(LayerHistoryIntegrationTest, frequentLayerBecomingInfrequentAndBack) {
1500     auto layer = createLegacyAndFrontedEndLayer(1);
1501 
1502     nsecs_t time = systemTime();
1503 
1504     EXPECT_EQ(1u, layerCount());
1505     EXPECT_EQ(0u, activeLayerCount());
1506     EXPECT_EQ(0, frequentLayerCount(time));
1507     EXPECT_EQ(0, animatingLayerCount(time));
1508 
1509     // Fill up the window with frequent updates
1510     for (size_t i = 0; i < FREQUENT_LAYER_WINDOW_SIZE; i++) {
1511         setBufferWithPresentTime(layer, time);
1512         time += (60_Hz).getPeriodNsecs();
1513 
1514         EXPECT_EQ(1u, layerCount());
1515         ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1516         EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
1517         EXPECT_EQ(1u, activeLayerCount());
1518         EXPECT_EQ(1, frequentLayerCount(time));
1519     }
1520 
1521     // posting a buffer after long inactivity should retain the layer as active
1522     time += std::chrono::nanoseconds(3s).count();
1523     setBufferWithPresentTime(layer, time);
1524     EXPECT_EQ(0, clearLayerHistoryCount(time));
1525     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1526     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summarizeLayerHistory(time)[0].vote);
1527     EXPECT_EQ(60_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
1528     EXPECT_EQ(1u, activeLayerCount());
1529     EXPECT_EQ(1, frequentLayerCount(time));
1530     EXPECT_EQ(0, animatingLayerCount(time));
1531 
1532     // posting more infrequent buffer should make the layer infrequent
1533     time += (MAX_FREQUENT_LAYER_PERIOD_NS + 1ms).count();
1534     setBufferWithPresentTime(layer, time);
1535     time += (MAX_FREQUENT_LAYER_PERIOD_NS + 1ms).count();
1536     setBufferWithPresentTime(layer, time);
1537     EXPECT_EQ(0, clearLayerHistoryCount(time));
1538     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1539     EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
1540     EXPECT_EQ(1u, activeLayerCount());
1541     EXPECT_EQ(0, frequentLayerCount(time));
1542     EXPECT_EQ(0, animatingLayerCount(time));
1543 
1544     // posting another buffer should keep the layer infrequent
1545     setBufferWithPresentTime(layer, time);
1546     EXPECT_EQ(0, clearLayerHistoryCount(time));
1547     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1548     EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
1549     EXPECT_EQ(1u, activeLayerCount());
1550     EXPECT_EQ(0, frequentLayerCount(time));
1551     EXPECT_EQ(0, animatingLayerCount(time));
1552 
1553     // posting more buffers would mean starting of an animation, so making the layer frequent
1554     setBufferWithPresentTime(layer, time);
1555     setBufferWithPresentTime(layer, time);
1556     EXPECT_EQ(1, clearLayerHistoryCount(time));
1557     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1558     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
1559     EXPECT_EQ(1u, activeLayerCount());
1560     EXPECT_EQ(1, frequentLayerCount(time));
1561     EXPECT_EQ(0, animatingLayerCount(time));
1562 
1563     // posting a buffer after long inactivity should retain the layer as active
1564     time += std::chrono::nanoseconds(3s).count();
1565     setBufferWithPresentTime(layer, time);
1566     EXPECT_EQ(0, clearLayerHistoryCount(time));
1567     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1568     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
1569     EXPECT_EQ(1u, activeLayerCount());
1570     EXPECT_EQ(1, frequentLayerCount(time));
1571     EXPECT_EQ(0, animatingLayerCount(time));
1572 
1573     // posting another buffer should keep the layer frequent
1574     time += (60_Hz).getPeriodNsecs();
1575     setBufferWithPresentTime(layer, time);
1576     EXPECT_EQ(0, clearLayerHistoryCount(time));
1577     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1578     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
1579     EXPECT_EQ(1u, activeLayerCount());
1580     EXPECT_EQ(1, frequentLayerCount(time));
1581     EXPECT_EQ(0, animatingLayerCount(time));
1582 }
1583 
TEST_F(LayerHistoryIntegrationTest,inconclusiveLayerBecomingFrequent)1584 TEST_F(LayerHistoryIntegrationTest, inconclusiveLayerBecomingFrequent) {
1585     auto layer = createLegacyAndFrontedEndLayer(1);
1586 
1587     nsecs_t time = systemTime();
1588 
1589     EXPECT_EQ(1u, layerCount());
1590     EXPECT_EQ(0u, activeLayerCount());
1591     EXPECT_EQ(0, frequentLayerCount(time));
1592     EXPECT_EQ(0, animatingLayerCount(time));
1593 
1594     // Fill up the window with frequent updates
1595     for (size_t i = 0; i < FREQUENT_LAYER_WINDOW_SIZE; i++) {
1596         setBufferWithPresentTime(layer, time);
1597         time += (60_Hz).getPeriodNsecs();
1598 
1599         EXPECT_EQ(1u, layerCount());
1600         ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1601         EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
1602         EXPECT_EQ(1u, activeLayerCount());
1603         EXPECT_EQ(1, frequentLayerCount(time));
1604     }
1605 
1606     // posting infrequent buffers after long inactivity should make the layer
1607     // inconclusive but frequent.
1608     time += std::chrono::nanoseconds(3s).count();
1609     setBufferWithPresentTime(layer, time);
1610     time += (MAX_FREQUENT_LAYER_PERIOD_NS + 1ms).count();
1611     setBufferWithPresentTime(layer, time);
1612     EXPECT_EQ(0, clearLayerHistoryCount(time));
1613     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1614     EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summarizeLayerHistory(time)[0].vote);
1615     EXPECT_EQ(1u, activeLayerCount());
1616     EXPECT_EQ(1, frequentLayerCount(time));
1617     EXPECT_EQ(0, animatingLayerCount(time));
1618 
1619     // posting more buffers should make the layer frequent and switch the refresh rate to max
1620     // by clearing the history
1621     setBufferWithPresentTime(layer, time);
1622     setBufferWithPresentTime(layer, time);
1623     setBufferWithPresentTime(layer, time);
1624     EXPECT_EQ(1, clearLayerHistoryCount(time));
1625     ASSERT_EQ(1u, summarizeLayerHistory(time).size());
1626     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
1627     EXPECT_EQ(1u, activeLayerCount());
1628     EXPECT_EQ(1, frequentLayerCount(time));
1629     EXPECT_EQ(0, animatingLayerCount(time));
1630 }
1631 
TEST_F(LayerHistoryIntegrationTest,getFramerate)1632 TEST_F(LayerHistoryIntegrationTest, getFramerate) {
1633     auto layer = createLegacyAndFrontedEndLayer(1);
1634 
1635     nsecs_t time = systemTime();
1636 
1637     EXPECT_EQ(1u, layerCount());
1638     EXPECT_EQ(0u, activeLayerCount());
1639     EXPECT_EQ(0, frequentLayerCount(time));
1640     EXPECT_EQ(0, animatingLayerCount(time));
1641 
1642     // layer is active but infrequent.
1643     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
1644         setBufferWithPresentTime(layer, time);
1645         time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
1646     }
1647 
1648     float expectedFramerate = 1e9f / MAX_FREQUENT_LAYER_PERIOD_NS.count();
1649     EXPECT_FLOAT_EQ(expectedFramerate, history().getLayerFramerate(time, layer->getSequence()));
1650 }
1651 
TEST_F(LayerHistoryIntegrationTest,heuristicLayer60Hz)1652 TEST_F(LayerHistoryIntegrationTest, heuristicLayer60Hz) {
1653     auto layer = createLegacyAndFrontedEndLayer(1);
1654 
1655     nsecs_t time = systemTime();
1656     for (float fps = 54.0f; fps < 65.0f; fps += 0.1f) {
1657         recordFramesAndExpect(layer, time, Fps::fromValue(fps), 60_Hz, PRESENT_TIME_HISTORY_SIZE);
1658     }
1659 }
1660 
TEST_F(LayerHistoryIntegrationTest,heuristicLayer60_30Hz)1661 TEST_F(LayerHistoryIntegrationTest, heuristicLayer60_30Hz) {
1662     auto layer = createLegacyAndFrontedEndLayer(1);
1663 
1664     nsecs_t time = systemTime();
1665     recordFramesAndExpect(layer, time, 60_Hz, 60_Hz, PRESENT_TIME_HISTORY_SIZE);
1666 
1667     recordFramesAndExpect(layer, time, 60_Hz, 60_Hz, PRESENT_TIME_HISTORY_SIZE);
1668     recordFramesAndExpect(layer, time, 30_Hz, 60_Hz, PRESENT_TIME_HISTORY_SIZE);
1669     recordFramesAndExpect(layer, time, 30_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
1670     recordFramesAndExpect(layer, time, 60_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
1671     recordFramesAndExpect(layer, time, 60_Hz, 60_Hz, PRESENT_TIME_HISTORY_SIZE);
1672 }
1673 
TEST_F(LayerHistoryIntegrationTest,heuristicLayerNotOscillating)1674 TEST_F(LayerHistoryIntegrationTest, heuristicLayerNotOscillating) {
1675     SET_FLAG_FOR_TEST(flags::use_known_refresh_rate_for_fps_consistency, false);
1676     auto layer = createLegacyAndFrontedEndLayer(1);
1677 
1678     nsecs_t time = systemTime();
1679 
1680     recordFramesAndExpect(layer, time, 27.1_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
1681     recordFramesAndExpect(layer, time, 26.9_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
1682     recordFramesAndExpect(layer, time, 26_Hz, 24_Hz, PRESENT_TIME_HISTORY_SIZE);
1683     recordFramesAndExpect(layer, time, 26.9_Hz, 24_Hz, PRESENT_TIME_HISTORY_SIZE);
1684     recordFramesAndExpect(layer, time, 27.1_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
1685 }
1686 
TEST_F(LayerHistoryIntegrationTest,heuristicLayerNotOscillating_useKnownRefreshRate)1687 TEST_F(LayerHistoryIntegrationTest, heuristicLayerNotOscillating_useKnownRefreshRate) {
1688     SET_FLAG_FOR_TEST(flags::use_known_refresh_rate_for_fps_consistency, true);
1689     auto layer = createLegacyAndFrontedEndLayer(1);
1690 
1691     nsecs_t time = systemTime();
1692 
1693     recordFramesAndExpect(layer, time, 27.1_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
1694     recordFramesAndExpect(layer, time, 26.9_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
1695     recordFramesAndExpect(layer, time, 26_Hz, 24_Hz, PRESENT_TIME_HISTORY_SIZE);
1696     recordFramesAndExpect(layer, time, 26.9_Hz, 24_Hz, PRESENT_TIME_HISTORY_SIZE);
1697     recordFramesAndExpect(layer, time, 27.1_Hz, 24_Hz, PRESENT_TIME_HISTORY_SIZE);
1698     recordFramesAndExpect(layer, time, 27.1_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
1699 }
1700 
TEST_F(LayerHistoryIntegrationTest,smallDirtyLayer)1701 TEST_F(LayerHistoryIntegrationTest, smallDirtyLayer) {
1702     auto layer = createLegacyAndFrontedEndLayer(1);
1703 
1704     nsecs_t time = systemTime();
1705 
1706     EXPECT_EQ(1u, layerCount());
1707     EXPECT_EQ(0u, activeLayerCount());
1708     EXPECT_EQ(0, frequentLayerCount(time));
1709 
1710     LayerHistory::Summary summary;
1711 
1712     // layer is active but infrequent.
1713     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
1714         scheduler::LayerProps props = {
1715                 .visible = false,
1716                 .bounds = {0, 0, 100, 100},
1717                 .transform = {},
1718                 .setFrameRateVote = {},
1719                 .frameRateSelectionPriority = Layer::PRIORITY_UNSET,
1720                 .isSmallDirty = false,
1721                 .isFrontBuffered = false,
1722         };
1723         if (i % 3 == 0) {
1724             props.isSmallDirty = false;
1725         } else {
1726             props.isSmallDirty = true;
1727         }
1728 
1729         setBufferWithPresentTime(layer, time);
1730         time += HI_FPS_PERIOD;
1731         summary = summarizeLayerHistory(time);
1732     }
1733 
1734     ASSERT_EQ(1u, summary.size());
1735     ASSERT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
1736     EXPECT_GE(HI_FPS, summary[0].desiredRefreshRate);
1737 }
1738 
TEST_F(LayerHistoryIntegrationTest,DISABLED_smallDirtyInMultiLayer)1739 TEST_F(LayerHistoryIntegrationTest, DISABLED_smallDirtyInMultiLayer) {
1740     auto uiLayer = createLegacyAndFrontedEndLayer(1);
1741     auto videoLayer = createLegacyAndFrontedEndLayer(2);
1742     setFrameRate(2, 30.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT,
1743                  ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
1744 
1745     nsecs_t time = systemTime();
1746 
1747     EXPECT_EQ(2u, layerCount());
1748     EXPECT_EQ(0u, activeLayerCount());
1749     EXPECT_EQ(0, frequentLayerCount(time));
1750 
1751     LayerHistory::Summary summary;
1752 
1753     // uiLayer is updating small dirty.
1754     for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE + FREQUENT_LAYER_WINDOW_SIZE + 1; i++) {
1755         scheduler::LayerProps props = {
1756                 .visible = false,
1757                 .bounds = {0, 0, 100, 100},
1758                 .transform = {},
1759                 .setFrameRateVote = {},
1760                 .frameRateSelectionPriority = Layer::PRIORITY_UNSET,
1761                 .isSmallDirty = true,
1762                 .isFrontBuffered = false,
1763         };
1764         setBuffer(1);
1765         uiLayer->setDesiredPresentTime(0, false /*autotimestamp*/);
1766         updateLayerSnapshotsAndLayerHistory(time);
1767         setBufferWithPresentTime(videoLayer, time);
1768         summary = summarizeLayerHistory(time);
1769     }
1770 
1771     ASSERT_EQ(1u, summary.size());
1772     ASSERT_EQ(LayerHistory::LayerVoteType::ExplicitDefault, summary[0].vote);
1773     ASSERT_EQ(30_Hz, summary[0].desiredRefreshRate);
1774 }
1775 
TEST_F(LayerHistoryIntegrationTest,hidingLayerUpdatesLayerHistory)1776 TEST_F(LayerHistoryIntegrationTest, hidingLayerUpdatesLayerHistory) {
1777     createLegacyAndFrontedEndLayer(1);
1778     nsecs_t time = systemTime();
1779     updateLayerSnapshotsAndLayerHistory(time);
1780     EXPECT_EQ(1u, layerCount());
1781     EXPECT_EQ(0u, activeLayerCount());
1782 
1783     setBuffer(1);
1784     updateLayerSnapshotsAndLayerHistory(time);
1785     auto summary = summarizeLayerHistory(time);
1786     ASSERT_EQ(1u, summary.size());
1787     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summary[0].vote);
1788     EXPECT_EQ(1u, activeLayerCount());
1789 
1790     hideLayer(1);
1791     updateLayerSnapshotsAndLayerHistory(time);
1792 
1793     summary = summarizeLayerHistory(time);
1794     EXPECT_TRUE(summary.empty());
1795     EXPECT_EQ(0u, activeLayerCount());
1796 }
1797 
TEST_F(LayerHistoryIntegrationTest,showingLayerUpdatesLayerHistory)1798 TEST_F(LayerHistoryIntegrationTest, showingLayerUpdatesLayerHistory) {
1799     createLegacyAndFrontedEndLayer(1);
1800     nsecs_t time = systemTime();
1801     updateLayerSnapshotsAndLayerHistory(time);
1802     EXPECT_EQ(1u, layerCount());
1803     EXPECT_EQ(0u, activeLayerCount());
1804     hideLayer(1);
1805     setBuffer(1);
1806     updateLayerSnapshotsAndLayerHistory(time);
1807     auto summary = summarizeLayerHistory(time);
1808     EXPECT_TRUE(summary.empty());
1809     EXPECT_EQ(0u, activeLayerCount());
1810 
1811     showLayer(1);
1812     updateLayerSnapshotsAndLayerHistory(time);
1813 
1814     summary = summarizeLayerHistory(time);
1815     ASSERT_EQ(1u, summary.size());
1816     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summary[0].vote);
1817     EXPECT_EQ(1u, activeLayerCount());
1818 }
1819 
TEST_F(LayerHistoryIntegrationTest,updatingGeometryUpdatesWeight)1820 TEST_F(LayerHistoryIntegrationTest, updatingGeometryUpdatesWeight) {
1821     createLegacyAndFrontedEndLayer(1);
1822     nsecs_t time = systemTime();
1823     updateLayerSnapshotsAndLayerHistory(time);
1824     EXPECT_EQ(1u, layerCount());
1825     EXPECT_EQ(0u, activeLayerCount());
1826 
1827     setBuffer(1,
1828               std::make_shared<
1829                       renderengine::mock::FakeExternalTexture>(100U /*width*/, 100U /*height*/, 1,
1830                                                                HAL_PIXEL_FORMAT_RGBA_8888,
1831                                                                GRALLOC_USAGE_PROTECTED /*usage*/));
1832     mFlinger.setLayerHistoryDisplayArea(100 * 100);
1833     updateLayerSnapshotsAndLayerHistory(time);
1834     auto summary = summarizeLayerHistory(time);
1835     ASSERT_EQ(1u, summary.size());
1836     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summary[0].vote);
1837     EXPECT_EQ(1u, activeLayerCount());
1838 
1839     auto startingWeight = summary[0].weight;
1840 
1841     setMatrix(1, 0.1f, 0.f, 0.f, 0.1f);
1842     updateLayerSnapshotsAndLayerHistory(time);
1843 
1844     summary = summarizeLayerHistory(time);
1845     ASSERT_EQ(1u, summary.size());
1846     EXPECT_EQ(LayerHistory::LayerVoteType::Max, summary[0].vote);
1847     EXPECT_EQ(1u, activeLayerCount());
1848     EXPECT_GT(startingWeight, summary[0].weight);
1849 }
1850 
1851 class LayerHistoryIntegrationTestParameterized
1852       : public LayerHistoryIntegrationTest,
1853         public testing::WithParamInterface<std::chrono::nanoseconds> {};
1854 
TEST_P(LayerHistoryIntegrationTestParameterized,HeuristicLayerWithInfrequentLayer)1855 TEST_P(LayerHistoryIntegrationTestParameterized, HeuristicLayerWithInfrequentLayer) {
1856     std::chrono::nanoseconds infrequentUpdateDelta = GetParam();
1857     auto heuristicLayer = createLegacyAndFrontedEndLayer(1);
1858     auto infrequentLayer = createLegacyAndFrontedEndLayer(2);
1859 
1860     const nsecs_t startTime = systemTime();
1861 
1862     const std::chrono::nanoseconds heuristicUpdateDelta = 41'666'667ns;
1863     setBufferWithPresentTime(heuristicLayer, startTime);
1864     setBufferWithPresentTime(infrequentLayer, startTime);
1865 
1866     nsecs_t time = startTime;
1867     nsecs_t lastInfrequentUpdate = startTime;
1868     const size_t totalInfrequentLayerUpdates = FREQUENT_LAYER_WINDOW_SIZE * 5;
1869     size_t infrequentLayerUpdates = 0;
1870     while (infrequentLayerUpdates <= totalInfrequentLayerUpdates) {
1871         time += heuristicUpdateDelta.count();
1872         setBufferWithPresentTime(heuristicLayer, time);
1873 
1874         if (time - lastInfrequentUpdate >= infrequentUpdateDelta.count()) {
1875             ALOGI("submitting infrequent frame [%zu/%zu]", infrequentLayerUpdates,
1876                   totalInfrequentLayerUpdates);
1877             lastInfrequentUpdate = time;
1878             setBufferWithPresentTime(infrequentLayer, time);
1879             infrequentLayerUpdates++;
1880         }
1881 
1882         if (time - startTime > PRESENT_TIME_HISTORY_DURATION.count()) {
1883             ASSERT_NE(0u, summarizeLayerHistory(time).size());
1884             ASSERT_GE(2u, summarizeLayerHistory(time).size());
1885 
1886             bool max = false;
1887             bool min = false;
1888             Fps heuristic;
1889             for (const auto& layer : summarizeLayerHistory(time)) {
1890                 if (layer.vote == LayerHistory::LayerVoteType::Heuristic) {
1891                     heuristic = layer.desiredRefreshRate;
1892                 } else if (layer.vote == LayerHistory::LayerVoteType::Max) {
1893                     max = true;
1894                 } else if (layer.vote == LayerHistory::LayerVoteType::Min) {
1895                     min = true;
1896                 }
1897             }
1898 
1899             if (infrequentLayerUpdates > FREQUENT_LAYER_WINDOW_SIZE) {
1900                 EXPECT_EQ(24_Hz, heuristic);
1901                 EXPECT_FALSE(max);
1902                 if (summarizeLayerHistory(time).size() == 2) {
1903                     EXPECT_TRUE(min);
1904                 }
1905             }
1906         }
1907     }
1908 }
1909 
1910 class SmallAreaDetectionTest : public LayerHistoryIntegrationTest {
1911 protected:
1912     static constexpr int32_t DISPLAY_WIDTH = 100;
1913     static constexpr int32_t DISPLAY_HEIGHT = 100;
1914 
1915     static constexpr int32_t kAppId1 = 10100;
1916     static constexpr int32_t kAppId2 = 10101;
1917 
1918     static constexpr float kThreshold1 = 0.05f;
1919     static constexpr float kThreshold2 = 0.07f;
1920 
SmallAreaDetectionTest()1921     SmallAreaDetectionTest() : LayerHistoryIntegrationTest() {
1922         std::vector<std::pair<int32_t, float>> mappings;
1923         mappings.reserve(2);
1924         mappings.push_back(std::make_pair(kAppId1, kThreshold1));
1925         mappings.push_back(std::make_pair(kAppId2, kThreshold2));
1926 
1927         mScheduler->onActiveDisplayAreaChanged(DISPLAY_WIDTH * DISPLAY_HEIGHT);
1928         mScheduler->updateSmallAreaDetection(mappings);
1929     }
1930 
createLegacyAndFrontedEndLayer(uint32_t sequence)1931     auto createLegacyAndFrontedEndLayer(uint32_t sequence) {
1932         std::string layerName = "test layer:" + std::to_string(sequence);
1933 
1934         LayerCreationArgs args = LayerCreationArgs{mFlinger.flinger(),
1935                                                    nullptr,
1936                                                    layerName,
1937                                                    0,
1938                                                    {},
1939                                                    std::make_optional<uint32_t>(sequence)};
1940         args.ownerUid = kAppId1;
1941         args.metadata.setInt32(gui::METADATA_WINDOW_TYPE, 2); // APPLICATION
1942         const auto layer = sp<Layer>::make(args);
1943         mFlinger.injectLegacyLayer(layer);
1944         createRootLayer(sequence);
1945         return layer;
1946     }
1947 };
1948 
TEST_F(SmallAreaDetectionTest,SmallDirtyLayer)1949 TEST_F(SmallAreaDetectionTest, SmallDirtyLayer) {
1950     SET_FLAG_FOR_TEST(flags::enable_small_area_detection, true);
1951     auto layer = createLegacyAndFrontedEndLayer(1);
1952 
1953     nsecs_t time = systemTime();
1954 
1955     EXPECT_EQ(1u, layerCount());
1956     EXPECT_EQ(0u, activeLayerCount());
1957     EXPECT_EQ(0, frequentLayerCount(time));
1958 
1959     uint32_t sequence = static_cast<uint32_t>(layer->sequence);
1960     setBuffer(sequence);
1961     setDamageRegion(sequence, Region(Rect(10, 10)));
1962     updateLayerSnapshotsAndLayerHistory(time);
1963 
1964     ASSERT_EQ(true, mFlinger.mutableLayerSnapshotBuilder().getSnapshot(1)->isSmallDirty);
1965 }
1966 
TEST_F(SmallAreaDetectionTest,NotSmallDirtyLayer)1967 TEST_F(SmallAreaDetectionTest, NotSmallDirtyLayer) {
1968     SET_FLAG_FOR_TEST(flags::enable_small_area_detection, true);
1969     auto layer = createLegacyAndFrontedEndLayer(1);
1970 
1971     nsecs_t time = systemTime();
1972 
1973     EXPECT_EQ(1u, layerCount());
1974     EXPECT_EQ(0u, activeLayerCount());
1975     EXPECT_EQ(0, frequentLayerCount(time));
1976 
1977     uint32_t sequence = static_cast<uint32_t>(layer->sequence);
1978     setBuffer(sequence);
1979     setDamageRegion(sequence, Region(Rect(50, 50)));
1980     updateLayerSnapshotsAndLayerHistory(time);
1981 
1982     ASSERT_EQ(false, mFlinger.mutableLayerSnapshotBuilder().getSnapshot(1)->isSmallDirty);
1983 }
1984 
TEST_F(SmallAreaDetectionTest,smallDirtyLayerWithMatrix)1985 TEST_F(SmallAreaDetectionTest, smallDirtyLayerWithMatrix) {
1986     SET_FLAG_FOR_TEST(flags::enable_small_area_detection, true);
1987     auto layer = createLegacyAndFrontedEndLayer(1);
1988 
1989     nsecs_t time = systemTime();
1990 
1991     EXPECT_EQ(1u, layerCount());
1992     EXPECT_EQ(0u, activeLayerCount());
1993     EXPECT_EQ(0, frequentLayerCount(time));
1994 
1995     // Original damage region is a small dirty.
1996     uint32_t sequence = static_cast<uint32_t>(layer->sequence);
1997     setBuffer(sequence);
1998     setDamageRegion(sequence, Region(Rect(20, 20)));
1999     updateLayerSnapshotsAndLayerHistory(time);
2000     ASSERT_EQ(true, mFlinger.mutableLayerSnapshotBuilder().getSnapshot(1)->isSmallDirty);
2001 
2002     setMatrix(sequence, 2.0f, 0, 0, 2.0f);
2003     updateLayerSnapshotsAndLayerHistory(time);
2004 
2005     // Verify if the small dirty is scaled.
2006     ASSERT_EQ(false, mFlinger.mutableLayerSnapshotBuilder().getSnapshot(1)->isSmallDirty);
2007 }
2008 
2009 INSTANTIATE_TEST_CASE_P(LeapYearTests, LayerHistoryIntegrationTestParameterized,
2010                         ::testing::Values(1s, 2s, 3s, 4s, 5s));
2011 
2012 } // namespace
2013 } // namespace android::scheduler
2014