File: LayerHistoryIntegrationTest.cpp

package info (click to toggle)
android-platform-tools 35.0.2-1~exp6
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 211,716 kB
  • sloc: cpp: 995,749; java: 290,495; ansic: 145,647; xml: 58,531; python: 39,608; sh: 14,500; javascript: 5,198; asm: 4,866; makefile: 3,115; yacc: 769; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (1092 lines) | stat: -rw-r--r-- 43,270 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
/*
 * Copyright 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#undef LOG_TAG
#define LOG_TAG "LayerHistoryIntegrationTest"

#include <Layer.h>
#include <com_android_graphics_surfaceflinger_flags.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <log/log.h>

#include <renderengine/mock/FakeExternalTexture.h>

#include <common/test/FlagUtils.h>
#include "FpsOps.h"
#include "LayerHierarchyTest.h"
#include "Scheduler/LayerHistory.h"
#include "Scheduler/LayerInfo.h"
#include "TestableScheduler.h"
#include "TestableSurfaceFlinger.h"
#include "mock/DisplayHardware/MockDisplayMode.h"
#include "mock/MockSchedulerCallback.h"

namespace android::scheduler {

using android::mock::createDisplayMode;
using namespace com::android::graphics::surfaceflinger;

class LayerHistoryIntegrationTest : public surfaceflinger::frontend::LayerSnapshotTestBase {
protected:
    static constexpr auto PRESENT_TIME_HISTORY_SIZE = LayerInfo::HISTORY_SIZE;
    static constexpr auto MAX_FREQUENT_LAYER_PERIOD_NS = LayerInfo::kMaxPeriodForFrequentLayerNs;
    static constexpr auto FREQUENT_LAYER_WINDOW_SIZE = LayerInfo::kFrequentLayerWindowSize;
    static constexpr auto PRESENT_TIME_HISTORY_DURATION = LayerInfo::HISTORY_DURATION;

    static constexpr Fps LO_FPS = 30_Hz;
    static constexpr auto LO_FPS_PERIOD = LO_FPS.getPeriodNsecs();

    static constexpr Fps HI_FPS = 90_Hz;
    static constexpr auto HI_FPS_PERIOD = HI_FPS.getPeriodNsecs();

    LayerHistoryIntegrationTest() : LayerSnapshotTestBase() {
        mFlinger.resetScheduler(mScheduler);
        mLifecycleManager = {};
        mHierarchyBuilder = {};
    }

    void updateLayerSnapshotsAndLayerHistory(nsecs_t now) {
        LayerSnapshotTestBase::update(mFlinger.mutableLayerSnapshotBuilder());
        mFlinger.updateLayerHistory(now);
    }

    void setBufferWithPresentTime(sp<Layer>& layer, nsecs_t time) {
        uint32_t sequence = static_cast<uint32_t>(layer->sequence);
        setBuffer(sequence);
        layer->setDesiredPresentTime(time, false /*autotimestamp*/);
        updateLayerSnapshotsAndLayerHistory(time);
    }

    LayerHistory& history() { return mScheduler->mutableLayerHistory(); }
    const LayerHistory& history() const { return mScheduler->mutableLayerHistory(); }

    LayerHistory::Summary summarizeLayerHistory(nsecs_t now) {
        // LayerHistory::summarize makes no guarantee of the order of the elements in the summary
        // however, for testing only, a stable order is required, therefore we sort the list here.
        // Any tests requiring ordered results must create layers with names.
        auto summary = history().summarize(*mScheduler->refreshRateSelector(), now);
        std::sort(summary.begin(), summary.end(),
                  [](const RefreshRateSelector::LayerRequirement& lhs,
                     const RefreshRateSelector::LayerRequirement& rhs) -> bool {
                      return lhs.name < rhs.name;
                  });
        return summary;
    }

    size_t layerCount() const { return mScheduler->layerHistorySize(); }
    size_t activeLayerCount() const NO_THREAD_SAFETY_ANALYSIS {
        return history().mActiveLayerInfos.size();
    }

    auto frequentLayerCount(nsecs_t now) const NO_THREAD_SAFETY_ANALYSIS {
        const auto& infos = history().mActiveLayerInfos;
        return std::count_if(infos.begin(), infos.end(), [now](const auto& pair) {
            return pair.second.second->isFrequent(now).isFrequent;
        });
    }

    auto animatingLayerCount(nsecs_t now) const NO_THREAD_SAFETY_ANALYSIS {
        const auto& infos = history().mActiveLayerInfos;
        return std::count_if(infos.begin(), infos.end(), [now](const auto& pair) {
            return pair.second.second->isAnimating(now);
        });
    }

    auto clearLayerHistoryCount(nsecs_t now) const NO_THREAD_SAFETY_ANALYSIS {
        const auto& infos = history().mActiveLayerInfos;
        return std::count_if(infos.begin(), infos.end(), [now](const auto& pair) {
            return pair.second.second->isFrequent(now).clearHistory;
        });
    }

    void setDefaultLayerVote(Layer* layer,
                             LayerHistory::LayerVoteType vote) NO_THREAD_SAFETY_ANALYSIS {
        auto [found, layerPair] = history().findLayer(layer->getSequence());
        if (found != LayerHistory::LayerStatus::NotFound) {
            layerPair->second->setDefaultLayerVote(vote);
        }
    }

    auto createLegacyAndFrontedEndLayer(uint32_t sequence) {
        std::string layerName = "test layer:" + std::to_string(sequence);
        const auto layer =
                sp<Layer>::make(LayerCreationArgs{mFlinger.flinger(),
                                                  nullptr,
                                                  layerName,
                                                  0,
                                                  {},
                                                  std::make_optional<uint32_t>(sequence)});
        mFlinger.injectLegacyLayer(layer);
        createRootLayer(sequence);
        return layer;
    }

    auto destroyLayer(sp<Layer>& layer) {
        uint32_t sequence = static_cast<uint32_t>(layer->sequence);
        mFlinger.releaseLegacyLayer(sequence);
        layer.clear();
        destroyLayerHandle(sequence);
    }

    void recordFramesAndExpect(sp<Layer>& layer, nsecs_t& time, Fps frameRate,
                               Fps desiredRefreshRate, int numFrames) {
        LayerHistory::Summary summary;
        for (int i = 0; i < numFrames; i++) {
            setBufferWithPresentTime(layer, time);
            time += frameRate.getPeriodNsecs();

            summary = summarizeLayerHistory(time);
        }

        ASSERT_EQ(1u, summary.size());
        ASSERT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
        ASSERT_EQ(desiredRefreshRate, summary[0].desiredRefreshRate);
    }

    std::shared_ptr<RefreshRateSelector> mSelector =
            std::make_shared<RefreshRateSelector>(makeModes(createDisplayMode(DisplayModeId(0),
                                                                              LO_FPS),
                                                            createDisplayMode(DisplayModeId(1),
                                                                              HI_FPS)),
                                                  DisplayModeId(0));

    mock::SchedulerCallback mSchedulerCallback;
    TestableSurfaceFlinger mFlinger;
    TestableScheduler* mScheduler = new TestableScheduler(mSelector, mFlinger, mSchedulerCallback);
};

namespace {

TEST_F(LayerHistoryIntegrationTest, singleLayerNoVoteDefaultCompatibility) {
    createLegacyAndFrontedEndLayer(1);
    nsecs_t time = systemTime();

    updateLayerSnapshotsAndLayerHistory(time);

    EXPECT_EQ(1u, layerCount());
    EXPECT_EQ(0u, activeLayerCount());

    // No layers returned if no layers are active.
    EXPECT_TRUE(summarizeLayerHistory(time).empty());
    EXPECT_EQ(0u, activeLayerCount());

    setBuffer(1);
    setDefaultFrameRateCompatibility(1, ANATIVEWINDOW_FRAME_RATE_NO_VOTE);
    updateLayerSnapshotsAndLayerHistory(time);

    EXPECT_TRUE(summarizeLayerHistory(time).empty());
    EXPECT_EQ(1u, activeLayerCount());
}

TEST_F(LayerHistoryIntegrationTest, singleLayerMinVoteDefaultCompatibility) {
    createLegacyAndFrontedEndLayer(1);
    nsecs_t time = systemTime();
    updateLayerSnapshotsAndLayerHistory(time);

    EXPECT_EQ(1u, layerCount());
    EXPECT_EQ(0u, activeLayerCount());

    EXPECT_TRUE(summarizeLayerHistory(time).empty());
    EXPECT_EQ(0u, activeLayerCount());

    setBuffer(1);
    setDefaultFrameRateCompatibility(1, ANATIVEWINDOW_FRAME_RATE_MIN);
    updateLayerSnapshotsAndLayerHistory(time);

    auto summary = summarizeLayerHistory(time);
    ASSERT_EQ(1u, summarizeLayerHistory(time).size());

    EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
    EXPECT_EQ(1u, activeLayerCount());
}

TEST_F(LayerHistoryIntegrationTest, oneInvisibleLayer) {
    createLegacyAndFrontedEndLayer(1);
    nsecs_t time = systemTime();
    updateLayerSnapshotsAndLayerHistory(time);
    EXPECT_EQ(1u, layerCount());
    EXPECT_EQ(0u, activeLayerCount());

    setBuffer(1);
    updateLayerSnapshotsAndLayerHistory(time);
    auto summary = summarizeLayerHistory(time);
    ASSERT_EQ(1u, summarizeLayerHistory(time).size());
    // Layer is still considered inactive so we expect to get Min
    EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
    EXPECT_EQ(1u, activeLayerCount());

    hideLayer(1);
    setBuffer(1);
    updateLayerSnapshotsAndLayerHistory(time);

    summary = summarizeLayerHistory(time);
    EXPECT_TRUE(summarizeLayerHistory(time).empty());
    EXPECT_EQ(0u, activeLayerCount());
}

TEST_F(LayerHistoryIntegrationTest, oneLayerExplicitVote) {
    createLegacyAndFrontedEndLayer(1);
    setFrameRate(1, 73.4f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT,
                 ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);

    EXPECT_EQ(1u, layerCount());
    EXPECT_EQ(0u, activeLayerCount());

    nsecs_t time = systemTime();
    updateLayerSnapshotsAndLayerHistory(time);
    ASSERT_EQ(1u, summarizeLayerHistory(time).size());
    EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitDefault, summarizeLayerHistory(time)[0].vote);
    EXPECT_EQ(73.4_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
    EXPECT_EQ(1u, activeLayerCount());
    EXPECT_EQ(1, frequentLayerCount(time));
}

TEST_F(LayerHistoryIntegrationTest, oneLayerExplicitExactVote) {
    createLegacyAndFrontedEndLayer(1);
    setFrameRate(1, 73.4f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,
                 ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);

    EXPECT_EQ(1u, layerCount());
    EXPECT_EQ(0u, activeLayerCount());

    nsecs_t time = systemTime();
    updateLayerSnapshotsAndLayerHistory(time);
    ASSERT_EQ(1u, summarizeLayerHistory(time).size());
    EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitExactOrMultiple,
              summarizeLayerHistory(time)[0].vote);
    EXPECT_EQ(73.4_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
    EXPECT_EQ(1u, activeLayerCount());
    EXPECT_EQ(1, frequentLayerCount(time));
}

TEST_F(LayerHistoryIntegrationTest, oneLayerExplicitCategory) {
    createLegacyAndFrontedEndLayer(1);
    setFrameRateCategory(1, ANATIVEWINDOW_FRAME_RATE_CATEGORY_HIGH);

    EXPECT_EQ(1u, layerCount());
    EXPECT_EQ(0u, activeLayerCount());

    nsecs_t time = systemTime();
    updateLayerSnapshotsAndLayerHistory(time);
    ASSERT_EQ(1u, summarizeLayerHistory(time).size());
    EXPECT_EQ(1u, activeLayerCount());
    EXPECT_EQ(1, frequentLayerCount(time));
    // First LayerRequirement is the frame rate specification
    EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitCategory, summarizeLayerHistory(time)[0].vote);
    EXPECT_EQ(0_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
    EXPECT_EQ(FrameRateCategory::High, summarizeLayerHistory(time)[0].frameRateCategory);
}

TEST_F(LayerHistoryIntegrationTest, multipleLayers) {
    auto layer1 = createLegacyAndFrontedEndLayer(1);
    auto layer2 = createLegacyAndFrontedEndLayer(2);
    auto layer3 = createLegacyAndFrontedEndLayer(3);

    nsecs_t time = systemTime();

    EXPECT_EQ(3u, layerCount());
    EXPECT_EQ(0u, activeLayerCount());
    EXPECT_EQ(0, frequentLayerCount(time));

    LayerHistory::Summary summary;

    // layer1 is active but infrequent.
    for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
        setBufferWithPresentTime(layer1, time);
        time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
        summary = summarizeLayerHistory(time);
    }

    ASSERT_EQ(1u, summary.size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Min, summary[0].vote);
    EXPECT_EQ(1u, activeLayerCount());
    EXPECT_EQ(0, frequentLayerCount(time));

    // layer2 is frequent and has high refresh rate.
    for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
        setBufferWithPresentTime(layer2, time);
        time += HI_FPS_PERIOD;
        summary = summarizeLayerHistory(time);
    }

    // layer1 is still active but infrequent.
    setBufferWithPresentTime(layer1, time);

    ASSERT_EQ(2u, summary.size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Min, summary[0].vote);
    ASSERT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[1].vote);
    EXPECT_EQ(HI_FPS, summarizeLayerHistory(time)[1].desiredRefreshRate);

    EXPECT_EQ(2u, activeLayerCount());
    EXPECT_EQ(1, frequentLayerCount(time));

    // layer1 is no longer active.
    // layer2 is frequent and has low refresh rate.
    for (size_t i = 0; i < 2 * PRESENT_TIME_HISTORY_SIZE; i++) {
        setBufferWithPresentTime(layer2, time);
        time += LO_FPS_PERIOD;
        summary = summarizeLayerHistory(time);
    }

    ASSERT_EQ(1u, summary.size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
    EXPECT_EQ(LO_FPS, summary[0].desiredRefreshRate);
    EXPECT_EQ(1u, activeLayerCount());
    EXPECT_EQ(1, frequentLayerCount(time));

    // layer2 still has low refresh rate.
    // layer3 has high refresh rate but not enough history.
    constexpr int RATIO = LO_FPS_PERIOD / HI_FPS_PERIOD;
    for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE - 1; i++) {
        if (i % RATIO == 0) {
            setBufferWithPresentTime(layer2, time);
        }

        setBufferWithPresentTime(layer3, time);
        time += HI_FPS_PERIOD;
        summary = summarizeLayerHistory(time);
    }

    ASSERT_EQ(2u, summary.size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
    EXPECT_EQ(LO_FPS, summary[0].desiredRefreshRate);
    EXPECT_EQ(LayerHistory::LayerVoteType::Max, summary[1].vote);
    EXPECT_EQ(2u, activeLayerCount());
    EXPECT_EQ(2, frequentLayerCount(time));

    // layer3 becomes recently active.
    setBufferWithPresentTime(layer3, time);
    summary = summarizeLayerHistory(time);
    ASSERT_EQ(2u, summary.size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
    EXPECT_EQ(LO_FPS, summary[0].desiredRefreshRate);
    EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[1].vote);
    EXPECT_EQ(HI_FPS, summary[1].desiredRefreshRate);
    EXPECT_EQ(2u, activeLayerCount());
    EXPECT_EQ(2, frequentLayerCount(time));

    // layer1 expires.
    destroyLayer(layer1);
    updateLayerSnapshotsAndLayerHistory(time);

    summary = summarizeLayerHistory(time);
    ASSERT_EQ(2u, summary.size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
    EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
    EXPECT_EQ(LO_FPS, summary[0].desiredRefreshRate);
    EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[1].vote);
    EXPECT_EQ(HI_FPS, summary[1].desiredRefreshRate);
    EXPECT_EQ(2u, layerCount());
    EXPECT_EQ(2u, activeLayerCount());
    EXPECT_EQ(2, frequentLayerCount(time));

    // layer2 still has low refresh rate.
    // layer3 becomes inactive.
    for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
        setBufferWithPresentTime(layer2, time);
        time += LO_FPS_PERIOD;
        summary = summarizeLayerHistory(time);
    }

    ASSERT_EQ(1u, summary.size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
    EXPECT_EQ(LO_FPS, summary[0].desiredRefreshRate);
    EXPECT_EQ(1u, activeLayerCount());
    EXPECT_EQ(1, frequentLayerCount(time));

    // layer2 expires.
    destroyLayer(layer2);
    updateLayerSnapshotsAndLayerHistory(time);
    summary = summarizeLayerHistory(time);
    EXPECT_TRUE(summary.empty());
    EXPECT_EQ(1u, layerCount());
    EXPECT_EQ(0u, activeLayerCount());
    EXPECT_EQ(0, frequentLayerCount(time));

    // layer3 becomes active and has high refresh rate.
    for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE + FREQUENT_LAYER_WINDOW_SIZE + 1; i++) {
        setBufferWithPresentTime(layer3, time);
        time += HI_FPS_PERIOD;
        summary = summarizeLayerHistory(time);
    }

    ASSERT_EQ(1u, summary.size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
    EXPECT_EQ(HI_FPS, summary[0].desiredRefreshRate);
    EXPECT_EQ(1u, layerCount());
    EXPECT_EQ(1u, activeLayerCount());
    EXPECT_EQ(1, frequentLayerCount(time));

    // layer3 expires.
    destroyLayer(layer3);
    updateLayerSnapshotsAndLayerHistory(time);
    summary = summarizeLayerHistory(time);
    EXPECT_TRUE(summary.empty());
    EXPECT_EQ(0u, layerCount());
    EXPECT_EQ(0u, activeLayerCount());
    EXPECT_EQ(0, frequentLayerCount(time));
}

TEST_F(LayerHistoryIntegrationTest, inactiveLayers) {
    auto layer = createLegacyAndFrontedEndLayer(1);
    nsecs_t time = systemTime();

    // the very first updates makes the layer frequent
    for (size_t i = 0; i < FREQUENT_LAYER_WINDOW_SIZE - 1; i++) {
        setBufferWithPresentTime(layer, time);
        time += MAX_FREQUENT_LAYER_PERIOD_NS.count();

        EXPECT_EQ(1u, layerCount());
        ASSERT_EQ(1u, summarizeLayerHistory(time).size());
        EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
        EXPECT_EQ(1u, activeLayerCount());
        EXPECT_EQ(1, frequentLayerCount(time));
    }

    // the next update with the MAX_FREQUENT_LAYER_PERIOD_NS will get us to infrequent
    time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
    setBufferWithPresentTime(layer, time);

    EXPECT_EQ(1u, layerCount());
    ASSERT_EQ(1u, summarizeLayerHistory(time).size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
    EXPECT_EQ(1u, activeLayerCount());
    EXPECT_EQ(0, frequentLayerCount(time));

    // advance the time for the previous frame to be inactive
    time += MAX_ACTIVE_LAYER_PERIOD_NS.count();

    // Now even if we post a quick few frame we should stay infrequent
    for (size_t i = 0; i < FREQUENT_LAYER_WINDOW_SIZE - 1; i++) {
        setBufferWithPresentTime(layer, time);
        time += HI_FPS_PERIOD;

        EXPECT_EQ(1u, layerCount());
        ASSERT_EQ(1u, summarizeLayerHistory(time).size());
        EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
        EXPECT_EQ(1u, activeLayerCount());
        EXPECT_EQ(0, frequentLayerCount(time));
    }

    // More quick frames will get us to frequent again
    setBufferWithPresentTime(layer, time);
    time += HI_FPS_PERIOD;

    EXPECT_EQ(1u, layerCount());
    ASSERT_EQ(1u, summarizeLayerHistory(time).size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
    EXPECT_EQ(1u, activeLayerCount());
    EXPECT_EQ(1, frequentLayerCount(time));
}

TEST_F(LayerHistoryIntegrationTest, invisibleExplicitLayerIsActive) {
    SET_FLAG_FOR_TEST(flags::misc1, false);

    auto explicitVisiblelayer = createLegacyAndFrontedEndLayer(1);
    auto explicitInvisiblelayer = createLegacyAndFrontedEndLayer(2);
    hideLayer(2);
    setFrameRate(1, 60.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,
                 ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
    setFrameRate(2, 90.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,
                 ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
    nsecs_t time = systemTime();

    // Post a buffer to the layers to make them active
    setBufferWithPresentTime(explicitVisiblelayer, time);
    setBufferWithPresentTime(explicitInvisiblelayer, time);

    EXPECT_EQ(2u, layerCount());
    ASSERT_EQ(1u, summarizeLayerHistory(time).size());
    EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitExactOrMultiple,
              summarizeLayerHistory(time)[0].vote);
    EXPECT_EQ(60_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
    EXPECT_EQ(2u, activeLayerCount());
    EXPECT_EQ(2, frequentLayerCount(time));
}

TEST_F(LayerHistoryIntegrationTest, invisibleExplicitLayerIsNotActive) {
    SET_FLAG_FOR_TEST(flags::misc1, true);

    auto explicitVisiblelayer = createLegacyAndFrontedEndLayer(1);
    auto explicitInvisiblelayer = createLegacyAndFrontedEndLayer(2);
    hideLayer(2);
    setFrameRate(1, 60.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,
                 ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
    setFrameRate(2, 90.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,
                 ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
    nsecs_t time = systemTime();

    // Post a buffer to the layers to make them active
    setBufferWithPresentTime(explicitVisiblelayer, time);
    setBufferWithPresentTime(explicitInvisiblelayer, time);

    EXPECT_EQ(2u, layerCount());
    ASSERT_EQ(1u, summarizeLayerHistory(time).size());
    EXPECT_EQ(LayerHistory::LayerVoteType::ExplicitExactOrMultiple,
              summarizeLayerHistory(time)[0].vote);
    EXPECT_EQ(60_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
    EXPECT_EQ(1u, activeLayerCount());
    EXPECT_EQ(1, frequentLayerCount(time));
}

TEST_F(LayerHistoryIntegrationTest, infrequentAnimatingLayer) {
    auto layer = createLegacyAndFrontedEndLayer(1);

    nsecs_t time = systemTime();

    EXPECT_EQ(1u, layerCount());
    EXPECT_EQ(0u, activeLayerCount());
    EXPECT_EQ(0, frequentLayerCount(time));
    EXPECT_EQ(0, animatingLayerCount(time));

    // layer is active but infrequent.
    for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
        setBufferWithPresentTime(layer, time);
        time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
    }

    ASSERT_EQ(1u, summarizeLayerHistory(time).size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
    EXPECT_EQ(1u, activeLayerCount());
    EXPECT_EQ(0, frequentLayerCount(time));
    EXPECT_EQ(0, animatingLayerCount(time));

    // another update with the same cadence keep in infrequent
    setBufferWithPresentTime(layer, time);
    time += MAX_FREQUENT_LAYER_PERIOD_NS.count();

    ASSERT_EQ(1u, summarizeLayerHistory(time).size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
    EXPECT_EQ(1u, activeLayerCount());
    EXPECT_EQ(0, frequentLayerCount(time));
    EXPECT_EQ(0, animatingLayerCount(time));

    mFlinger.mutableLayerSnapshotBuilder().getSnapshot(1)->changes |=
            frontend::RequestedLayerState::Changes::Animation;
    mFlinger.updateLayerHistory(time);
    // an update as animation will immediately vote for Max
    time += MAX_FREQUENT_LAYER_PERIOD_NS.count();

    ASSERT_EQ(1u, summarizeLayerHistory(time).size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
    EXPECT_EQ(1u, activeLayerCount());
    EXPECT_EQ(0, frequentLayerCount(time));
    EXPECT_EQ(1, animatingLayerCount(time));
}

TEST_F(LayerHistoryIntegrationTest, frequentLayerBecomingInfrequentAndBack) {
    auto layer = createLegacyAndFrontedEndLayer(1);

    nsecs_t time = systemTime();

    EXPECT_EQ(1u, layerCount());
    EXPECT_EQ(0u, activeLayerCount());
    EXPECT_EQ(0, frequentLayerCount(time));
    EXPECT_EQ(0, animatingLayerCount(time));

    // Fill up the window with frequent updates
    for (size_t i = 0; i < FREQUENT_LAYER_WINDOW_SIZE; i++) {
        setBufferWithPresentTime(layer, time);
        time += (60_Hz).getPeriodNsecs();

        EXPECT_EQ(1u, layerCount());
        ASSERT_EQ(1u, summarizeLayerHistory(time).size());
        EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
        EXPECT_EQ(1u, activeLayerCount());
        EXPECT_EQ(1, frequentLayerCount(time));
    }

    // posting a buffer after long inactivity should retain the layer as active
    time += std::chrono::nanoseconds(3s).count();
    setBufferWithPresentTime(layer, time);
    EXPECT_EQ(0, clearLayerHistoryCount(time));
    ASSERT_EQ(1u, summarizeLayerHistory(time).size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summarizeLayerHistory(time)[0].vote);
    EXPECT_EQ(60_Hz, summarizeLayerHistory(time)[0].desiredRefreshRate);
    EXPECT_EQ(1u, activeLayerCount());
    EXPECT_EQ(1, frequentLayerCount(time));
    EXPECT_EQ(0, animatingLayerCount(time));

    // posting more infrequent buffer should make the layer infrequent
    time += (MAX_FREQUENT_LAYER_PERIOD_NS + 1ms).count();
    setBufferWithPresentTime(layer, time);
    time += (MAX_FREQUENT_LAYER_PERIOD_NS + 1ms).count();
    setBufferWithPresentTime(layer, time);
    EXPECT_EQ(0, clearLayerHistoryCount(time));
    ASSERT_EQ(1u, summarizeLayerHistory(time).size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
    EXPECT_EQ(1u, activeLayerCount());
    EXPECT_EQ(0, frequentLayerCount(time));
    EXPECT_EQ(0, animatingLayerCount(time));

    // posting another buffer should keep the layer infrequent
    setBufferWithPresentTime(layer, time);
    EXPECT_EQ(0, clearLayerHistoryCount(time));
    ASSERT_EQ(1u, summarizeLayerHistory(time).size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Min, summarizeLayerHistory(time)[0].vote);
    EXPECT_EQ(1u, activeLayerCount());
    EXPECT_EQ(0, frequentLayerCount(time));
    EXPECT_EQ(0, animatingLayerCount(time));

    // posting more buffers would mean starting of an animation, so making the layer frequent
    setBufferWithPresentTime(layer, time);
    setBufferWithPresentTime(layer, time);
    EXPECT_EQ(1, clearLayerHistoryCount(time));
    ASSERT_EQ(1u, summarizeLayerHistory(time).size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
    EXPECT_EQ(1u, activeLayerCount());
    EXPECT_EQ(1, frequentLayerCount(time));
    EXPECT_EQ(0, animatingLayerCount(time));

    // posting a buffer after long inactivity should retain the layer as active
    time += std::chrono::nanoseconds(3s).count();
    setBufferWithPresentTime(layer, time);
    EXPECT_EQ(0, clearLayerHistoryCount(time));
    ASSERT_EQ(1u, summarizeLayerHistory(time).size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
    EXPECT_EQ(1u, activeLayerCount());
    EXPECT_EQ(1, frequentLayerCount(time));
    EXPECT_EQ(0, animatingLayerCount(time));

    // posting another buffer should keep the layer frequent
    time += (60_Hz).getPeriodNsecs();
    setBufferWithPresentTime(layer, time);
    EXPECT_EQ(0, clearLayerHistoryCount(time));
    ASSERT_EQ(1u, summarizeLayerHistory(time).size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
    EXPECT_EQ(1u, activeLayerCount());
    EXPECT_EQ(1, frequentLayerCount(time));
    EXPECT_EQ(0, animatingLayerCount(time));
}

TEST_F(LayerHistoryIntegrationTest, inconclusiveLayerBecomingFrequent) {
    auto layer = createLegacyAndFrontedEndLayer(1);

    nsecs_t time = systemTime();

    EXPECT_EQ(1u, layerCount());
    EXPECT_EQ(0u, activeLayerCount());
    EXPECT_EQ(0, frequentLayerCount(time));
    EXPECT_EQ(0, animatingLayerCount(time));

    // Fill up the window with frequent updates
    for (size_t i = 0; i < FREQUENT_LAYER_WINDOW_SIZE; i++) {
        setBufferWithPresentTime(layer, time);
        time += (60_Hz).getPeriodNsecs();

        EXPECT_EQ(1u, layerCount());
        ASSERT_EQ(1u, summarizeLayerHistory(time).size());
        EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
        EXPECT_EQ(1u, activeLayerCount());
        EXPECT_EQ(1, frequentLayerCount(time));
    }

    // posting infrequent buffers after long inactivity should make the layer
    // inconclusive but frequent.
    time += std::chrono::nanoseconds(3s).count();
    setBufferWithPresentTime(layer, time);
    time += (MAX_FREQUENT_LAYER_PERIOD_NS + 1ms).count();
    setBufferWithPresentTime(layer, time);
    EXPECT_EQ(0, clearLayerHistoryCount(time));
    ASSERT_EQ(1u, summarizeLayerHistory(time).size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Heuristic, summarizeLayerHistory(time)[0].vote);
    EXPECT_EQ(1u, activeLayerCount());
    EXPECT_EQ(1, frequentLayerCount(time));
    EXPECT_EQ(0, animatingLayerCount(time));

    // posting more buffers should make the layer frequent and switch the refresh rate to max
    // by clearing the history
    setBufferWithPresentTime(layer, time);
    setBufferWithPresentTime(layer, time);
    setBufferWithPresentTime(layer, time);
    EXPECT_EQ(1, clearLayerHistoryCount(time));
    ASSERT_EQ(1u, summarizeLayerHistory(time).size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Max, summarizeLayerHistory(time)[0].vote);
    EXPECT_EQ(1u, activeLayerCount());
    EXPECT_EQ(1, frequentLayerCount(time));
    EXPECT_EQ(0, animatingLayerCount(time));
}

TEST_F(LayerHistoryIntegrationTest, getFramerate) {
    auto layer = createLegacyAndFrontedEndLayer(1);

    nsecs_t time = systemTime();

    EXPECT_EQ(1u, layerCount());
    EXPECT_EQ(0u, activeLayerCount());
    EXPECT_EQ(0, frequentLayerCount(time));
    EXPECT_EQ(0, animatingLayerCount(time));

    // layer is active but infrequent.
    for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
        setBufferWithPresentTime(layer, time);
        time += MAX_FREQUENT_LAYER_PERIOD_NS.count();
    }

    float expectedFramerate = 1e9f / MAX_FREQUENT_LAYER_PERIOD_NS.count();
    EXPECT_FLOAT_EQ(expectedFramerate, history().getLayerFramerate(time, layer->getSequence()));
}

TEST_F(LayerHistoryIntegrationTest, heuristicLayer60Hz) {
    auto layer = createLegacyAndFrontedEndLayer(1);

    nsecs_t time = systemTime();
    for (float fps = 54.0f; fps < 65.0f; fps += 0.1f) {
        recordFramesAndExpect(layer, time, Fps::fromValue(fps), 60_Hz, PRESENT_TIME_HISTORY_SIZE);
    }
}

TEST_F(LayerHistoryIntegrationTest, heuristicLayer60_30Hz) {
    auto layer = createLegacyAndFrontedEndLayer(1);

    nsecs_t time = systemTime();
    recordFramesAndExpect(layer, time, 60_Hz, 60_Hz, PRESENT_TIME_HISTORY_SIZE);

    recordFramesAndExpect(layer, time, 60_Hz, 60_Hz, PRESENT_TIME_HISTORY_SIZE);
    recordFramesAndExpect(layer, time, 30_Hz, 60_Hz, PRESENT_TIME_HISTORY_SIZE);
    recordFramesAndExpect(layer, time, 30_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
    recordFramesAndExpect(layer, time, 60_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
    recordFramesAndExpect(layer, time, 60_Hz, 60_Hz, PRESENT_TIME_HISTORY_SIZE);
}

TEST_F(LayerHistoryIntegrationTest, heuristicLayerNotOscillating) {
    SET_FLAG_FOR_TEST(flags::use_known_refresh_rate_for_fps_consistency, false);
    auto layer = createLegacyAndFrontedEndLayer(1);

    nsecs_t time = systemTime();

    recordFramesAndExpect(layer, time, 27.1_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
    recordFramesAndExpect(layer, time, 26.9_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
    recordFramesAndExpect(layer, time, 26_Hz, 24_Hz, PRESENT_TIME_HISTORY_SIZE);
    recordFramesAndExpect(layer, time, 26.9_Hz, 24_Hz, PRESENT_TIME_HISTORY_SIZE);
    recordFramesAndExpect(layer, time, 27.1_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
}

TEST_F(LayerHistoryIntegrationTest, heuristicLayerNotOscillating_useKnownRefreshRate) {
    SET_FLAG_FOR_TEST(flags::use_known_refresh_rate_for_fps_consistency, true);
    auto layer = createLegacyAndFrontedEndLayer(1);

    nsecs_t time = systemTime();

    recordFramesAndExpect(layer, time, 27.1_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
    recordFramesAndExpect(layer, time, 26.9_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
    recordFramesAndExpect(layer, time, 26_Hz, 24_Hz, PRESENT_TIME_HISTORY_SIZE);
    recordFramesAndExpect(layer, time, 26.9_Hz, 24_Hz, PRESENT_TIME_HISTORY_SIZE);
    recordFramesAndExpect(layer, time, 27.1_Hz, 24_Hz, PRESENT_TIME_HISTORY_SIZE);
    recordFramesAndExpect(layer, time, 27.1_Hz, 30_Hz, PRESENT_TIME_HISTORY_SIZE);
}

TEST_F(LayerHistoryIntegrationTest, smallDirtyLayer) {
    auto layer = createLegacyAndFrontedEndLayer(1);

    nsecs_t time = systemTime();

    EXPECT_EQ(1u, layerCount());
    EXPECT_EQ(0u, activeLayerCount());
    EXPECT_EQ(0, frequentLayerCount(time));

    LayerHistory::Summary summary;

    // layer is active but infrequent.
    for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE; i++) {
        auto props = layer->getLayerProps();
        if (i % 3 == 0) {
            props.isSmallDirty = false;
        } else {
            props.isSmallDirty = true;
        }

        setBufferWithPresentTime(layer, time);
        time += HI_FPS_PERIOD;
        summary = summarizeLayerHistory(time);
    }

    ASSERT_EQ(1u, summary.size());
    ASSERT_EQ(LayerHistory::LayerVoteType::Heuristic, summary[0].vote);
    EXPECT_GE(HI_FPS, summary[0].desiredRefreshRate);
}

TEST_F(LayerHistoryIntegrationTest, DISABLED_smallDirtyInMultiLayer) {
    auto uiLayer = createLegacyAndFrontedEndLayer(1);
    auto videoLayer = createLegacyAndFrontedEndLayer(2);
    setFrameRate(2, 30.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT,
                 ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);

    nsecs_t time = systemTime();

    EXPECT_EQ(2u, layerCount());
    EXPECT_EQ(0u, activeLayerCount());
    EXPECT_EQ(0, frequentLayerCount(time));

    LayerHistory::Summary summary;

    // uiLayer is updating small dirty.
    for (size_t i = 0; i < PRESENT_TIME_HISTORY_SIZE + FREQUENT_LAYER_WINDOW_SIZE + 1; i++) {
        auto props = uiLayer->getLayerProps();
        props.isSmallDirty = true;
        setBuffer(1);
        uiLayer->setDesiredPresentTime(0, false /*autotimestamp*/);
        updateLayerSnapshotsAndLayerHistory(time);
        setBufferWithPresentTime(videoLayer, time);
        summary = summarizeLayerHistory(time);
    }

    ASSERT_EQ(1u, summary.size());
    ASSERT_EQ(LayerHistory::LayerVoteType::ExplicitDefault, summary[0].vote);
    ASSERT_EQ(30_Hz, summary[0].desiredRefreshRate);
}

TEST_F(LayerHistoryIntegrationTest, hidingLayerUpdatesLayerHistory) {
    createLegacyAndFrontedEndLayer(1);
    nsecs_t time = systemTime();
    updateLayerSnapshotsAndLayerHistory(time);
    EXPECT_EQ(1u, layerCount());
    EXPECT_EQ(0u, activeLayerCount());

    setBuffer(1);
    updateLayerSnapshotsAndLayerHistory(time);
    auto summary = summarizeLayerHistory(time);
    ASSERT_EQ(1u, summary.size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Max, summary[0].vote);
    EXPECT_EQ(1u, activeLayerCount());

    hideLayer(1);
    updateLayerSnapshotsAndLayerHistory(time);

    summary = summarizeLayerHistory(time);
    EXPECT_TRUE(summary.empty());
    EXPECT_EQ(0u, activeLayerCount());
}

TEST_F(LayerHistoryIntegrationTest, showingLayerUpdatesLayerHistory) {
    createLegacyAndFrontedEndLayer(1);
    nsecs_t time = systemTime();
    updateLayerSnapshotsAndLayerHistory(time);
    EXPECT_EQ(1u, layerCount());
    EXPECT_EQ(0u, activeLayerCount());
    hideLayer(1);
    setBuffer(1);
    updateLayerSnapshotsAndLayerHistory(time);
    auto summary = summarizeLayerHistory(time);
    EXPECT_TRUE(summary.empty());
    EXPECT_EQ(0u, activeLayerCount());

    showLayer(1);
    updateLayerSnapshotsAndLayerHistory(time);

    summary = summarizeLayerHistory(time);
    ASSERT_EQ(1u, summary.size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Max, summary[0].vote);
    EXPECT_EQ(1u, activeLayerCount());
}

TEST_F(LayerHistoryIntegrationTest, updatingGeometryUpdatesWeight) {
    createLegacyAndFrontedEndLayer(1);
    nsecs_t time = systemTime();
    updateLayerSnapshotsAndLayerHistory(time);
    EXPECT_EQ(1u, layerCount());
    EXPECT_EQ(0u, activeLayerCount());

    setBuffer(1,
              std::make_shared<
                      renderengine::mock::FakeExternalTexture>(100U /*width*/, 100U /*height*/, 1,
                                                               HAL_PIXEL_FORMAT_RGBA_8888,
                                                               GRALLOC_USAGE_PROTECTED /*usage*/));
    mFlinger.setLayerHistoryDisplayArea(100 * 100);
    updateLayerSnapshotsAndLayerHistory(time);
    auto summary = summarizeLayerHistory(time);
    ASSERT_EQ(1u, summary.size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Max, summary[0].vote);
    EXPECT_EQ(1u, activeLayerCount());

    auto startingWeight = summary[0].weight;

    setMatrix(1, 0.1f, 0.f, 0.f, 0.1f);
    updateLayerSnapshotsAndLayerHistory(time);

    summary = summarizeLayerHistory(time);
    ASSERT_EQ(1u, summary.size());
    EXPECT_EQ(LayerHistory::LayerVoteType::Max, summary[0].vote);
    EXPECT_EQ(1u, activeLayerCount());
    EXPECT_GT(startingWeight, summary[0].weight);
}

class LayerHistoryIntegrationTestParameterized
      : public LayerHistoryIntegrationTest,
        public testing::WithParamInterface<std::chrono::nanoseconds> {};

TEST_P(LayerHistoryIntegrationTestParameterized, HeuristicLayerWithInfrequentLayer) {
    std::chrono::nanoseconds infrequentUpdateDelta = GetParam();
    auto heuristicLayer = createLegacyAndFrontedEndLayer(1);
    auto infrequentLayer = createLegacyAndFrontedEndLayer(2);

    const nsecs_t startTime = systemTime();

    const std::chrono::nanoseconds heuristicUpdateDelta = 41'666'667ns;
    setBufferWithPresentTime(heuristicLayer, startTime);
    setBufferWithPresentTime(infrequentLayer, startTime);

    nsecs_t time = startTime;
    nsecs_t lastInfrequentUpdate = startTime;
    const size_t totalInfrequentLayerUpdates = FREQUENT_LAYER_WINDOW_SIZE * 5;
    size_t infrequentLayerUpdates = 0;
    while (infrequentLayerUpdates <= totalInfrequentLayerUpdates) {
        time += heuristicUpdateDelta.count();
        setBufferWithPresentTime(heuristicLayer, time);

        if (time - lastInfrequentUpdate >= infrequentUpdateDelta.count()) {
            ALOGI("submitting infrequent frame [%zu/%zu]", infrequentLayerUpdates,
                  totalInfrequentLayerUpdates);
            lastInfrequentUpdate = time;
            setBufferWithPresentTime(infrequentLayer, time);
            infrequentLayerUpdates++;
        }

        if (time - startTime > PRESENT_TIME_HISTORY_DURATION.count()) {
            ASSERT_NE(0u, summarizeLayerHistory(time).size());
            ASSERT_GE(2u, summarizeLayerHistory(time).size());

            bool max = false;
            bool min = false;
            Fps heuristic;
            for (const auto& layer : summarizeLayerHistory(time)) {
                if (layer.vote == LayerHistory::LayerVoteType::Heuristic) {
                    heuristic = layer.desiredRefreshRate;
                } else if (layer.vote == LayerHistory::LayerVoteType::Max) {
                    max = true;
                } else if (layer.vote == LayerHistory::LayerVoteType::Min) {
                    min = true;
                }
            }

            if (infrequentLayerUpdates > FREQUENT_LAYER_WINDOW_SIZE) {
                EXPECT_EQ(24_Hz, heuristic);
                EXPECT_FALSE(max);
                if (summarizeLayerHistory(time).size() == 2) {
                    EXPECT_TRUE(min);
                }
            }
        }
    }
}

class SmallAreaDetectionTest : public LayerHistoryIntegrationTest {
protected:
    static constexpr int32_t DISPLAY_WIDTH = 100;
    static constexpr int32_t DISPLAY_HEIGHT = 100;

    static constexpr int32_t kAppId1 = 10100;
    static constexpr int32_t kAppId2 = 10101;

    static constexpr float kThreshold1 = 0.05f;
    static constexpr float kThreshold2 = 0.07f;

    SmallAreaDetectionTest() : LayerHistoryIntegrationTest() {
        std::vector<std::pair<int32_t, float>> mappings;
        mappings.reserve(2);
        mappings.push_back(std::make_pair(kAppId1, kThreshold1));
        mappings.push_back(std::make_pair(kAppId2, kThreshold2));

        mFlinger.enableNewFrontEnd();

        mScheduler->onActiveDisplayAreaChanged(DISPLAY_WIDTH * DISPLAY_HEIGHT);
        mScheduler->updateSmallAreaDetection(mappings);
    }

    auto createLegacyAndFrontedEndLayer(uint32_t sequence) {
        std::string layerName = "test layer:" + std::to_string(sequence);

        LayerCreationArgs args = LayerCreationArgs{mFlinger.flinger(),
                                                   nullptr,
                                                   layerName,
                                                   0,
                                                   {},
                                                   std::make_optional<uint32_t>(sequence)};
        args.ownerUid = kAppId1;
        args.metadata.setInt32(gui::METADATA_WINDOW_TYPE, 2); // APPLICATION
        const auto layer = sp<Layer>::make(args);
        mFlinger.injectLegacyLayer(layer);
        createRootLayer(sequence);
        return layer;
    }
};

TEST_F(SmallAreaDetectionTest, SmallDirtyLayer) {
    SET_FLAG_FOR_TEST(flags::enable_small_area_detection, true);
    auto layer = createLegacyAndFrontedEndLayer(1);

    nsecs_t time = systemTime();

    EXPECT_EQ(1u, layerCount());
    EXPECT_EQ(0u, activeLayerCount());
    EXPECT_EQ(0, frequentLayerCount(time));

    uint32_t sequence = static_cast<uint32_t>(layer->sequence);
    setBuffer(sequence);
    setDamageRegion(sequence, Region(Rect(10, 10)));
    updateLayerSnapshotsAndLayerHistory(time);

    ASSERT_EQ(true, mFlinger.mutableLayerSnapshotBuilder().getSnapshot(1)->isSmallDirty);
}

TEST_F(SmallAreaDetectionTest, NotSmallDirtyLayer) {
    SET_FLAG_FOR_TEST(flags::enable_small_area_detection, true);
    auto layer = createLegacyAndFrontedEndLayer(1);

    nsecs_t time = systemTime();

    EXPECT_EQ(1u, layerCount());
    EXPECT_EQ(0u, activeLayerCount());
    EXPECT_EQ(0, frequentLayerCount(time));

    uint32_t sequence = static_cast<uint32_t>(layer->sequence);
    setBuffer(sequence);
    setDamageRegion(sequence, Region(Rect(50, 50)));
    updateLayerSnapshotsAndLayerHistory(time);

    ASSERT_EQ(false, mFlinger.mutableLayerSnapshotBuilder().getSnapshot(1)->isSmallDirty);
}

TEST_F(SmallAreaDetectionTest, smallDirtyLayerWithMatrix) {
    SET_FLAG_FOR_TEST(flags::enable_small_area_detection, true);
    auto layer = createLegacyAndFrontedEndLayer(1);

    nsecs_t time = systemTime();

    EXPECT_EQ(1u, layerCount());
    EXPECT_EQ(0u, activeLayerCount());
    EXPECT_EQ(0, frequentLayerCount(time));

    // Original damage region is a small dirty.
    uint32_t sequence = static_cast<uint32_t>(layer->sequence);
    setBuffer(sequence);
    setDamageRegion(sequence, Region(Rect(20, 20)));
    updateLayerSnapshotsAndLayerHistory(time);
    ASSERT_EQ(true, mFlinger.mutableLayerSnapshotBuilder().getSnapshot(1)->isSmallDirty);

    setMatrix(sequence, 2.0f, 0, 0, 2.0f);
    updateLayerSnapshotsAndLayerHistory(time);

    // Verify if the small dirty is scaled.
    ASSERT_EQ(false, mFlinger.mutableLayerSnapshotBuilder().getSnapshot(1)->isSmallDirty);
}

INSTANTIATE_TEST_CASE_P(LeapYearTests, LayerHistoryIntegrationTestParameterized,
                        ::testing::Values(1s, 2s, 3s, 4s, 5s));

} // namespace
} // namespace android::scheduler