1 #undef LOG_TAG
2 #define LOG_TAG "LayerHistoryUnittests"
3
4 #include <gmock/gmock.h>
5 #include <gtest/gtest.h>
6
7 #include <log/log.h>
8
9 #include <mutex>
10 #include <thread>
11
12 #include "Scheduler/LayerHistory.h"
13
14 using testing::_;
15 using testing::Return;
16
17 namespace android {
18 namespace scheduler {
19
20 class LayerHistoryTest : public testing::Test {
21 public:
22 LayerHistoryTest();
23 ~LayerHistoryTest() override;
24
25 protected:
26 std::unique_ptr<LayerHistory> mLayerHistory;
27
28 static constexpr float MAX_REFRESH_RATE = 90.f;
29 };
30
LayerHistoryTest()31 LayerHistoryTest::LayerHistoryTest() {
32 mLayerHistory = std::make_unique<LayerHistory>();
33 }
~LayerHistoryTest()34 LayerHistoryTest::~LayerHistoryTest() {}
35
36 namespace {
TEST_F(LayerHistoryTest,oneLayer)37 TEST_F(LayerHistoryTest, oneLayer) {
38 std::unique_ptr<LayerHistory::LayerHandle> testLayer =
39 mLayerHistory->createLayer("TestLayer", MAX_REFRESH_RATE);
40 mLayerHistory->setVisibility(testLayer, true);
41
42 mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
43 EXPECT_FLOAT_EQ(0.f, mLayerHistory->getDesiredRefreshRateAndHDR().first);
44
45 mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
46 mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
47 mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
48 // This is still 0, because the layer is not considered recently active if it
49 // has been present in less than 10 frames.
50 EXPECT_FLOAT_EQ(0.f, mLayerHistory->getDesiredRefreshRateAndHDR().first);
51 mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
52 mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
53 mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
54 mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
55 mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
56 mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
57 // This should be MAX_REFRESH_RATE as we have more than 10 samples
58 EXPECT_FLOAT_EQ(MAX_REFRESH_RATE, mLayerHistory->getDesiredRefreshRateAndHDR().first);
59 }
60
TEST_F(LayerHistoryTest,oneHDRLayer)61 TEST_F(LayerHistoryTest, oneHDRLayer) {
62 std::unique_ptr<LayerHistory::LayerHandle> testLayer =
63 mLayerHistory->createLayer("TestHDRLayer", MAX_REFRESH_RATE);
64 mLayerHistory->setVisibility(testLayer, true);
65
66 mLayerHistory->insert(testLayer, 0, true /*isHDR*/);
67 EXPECT_FLOAT_EQ(0.0f, mLayerHistory->getDesiredRefreshRateAndHDR().first);
68 EXPECT_EQ(true, mLayerHistory->getDesiredRefreshRateAndHDR().second);
69
70 mLayerHistory->setVisibility(testLayer, false);
71 EXPECT_FLOAT_EQ(0.0f, mLayerHistory->getDesiredRefreshRateAndHDR().first);
72 EXPECT_EQ(false, mLayerHistory->getDesiredRefreshRateAndHDR().second);
73 }
74
TEST_F(LayerHistoryTest,explicitTimestamp)75 TEST_F(LayerHistoryTest, explicitTimestamp) {
76 std::unique_ptr<LayerHistory::LayerHandle> test30FpsLayer =
77 mLayerHistory->createLayer("30FpsLayer", MAX_REFRESH_RATE);
78 mLayerHistory->setVisibility(test30FpsLayer, true);
79
80 nsecs_t startTime = systemTime();
81 for (int i = 0; i < 31; i++) {
82 mLayerHistory->insert(test30FpsLayer, startTime + (i * 33333333), false /*isHDR*/);
83 }
84
85 EXPECT_FLOAT_EQ(30.f, mLayerHistory->getDesiredRefreshRateAndHDR().first);
86 }
87
TEST_F(LayerHistoryTest,multipleLayers)88 TEST_F(LayerHistoryTest, multipleLayers) {
89 std::unique_ptr<LayerHistory::LayerHandle> testLayer =
90 mLayerHistory->createLayer("TestLayer", MAX_REFRESH_RATE);
91 mLayerHistory->setVisibility(testLayer, true);
92 std::unique_ptr<LayerHistory::LayerHandle> test30FpsLayer =
93 mLayerHistory->createLayer("30FpsLayer", MAX_REFRESH_RATE);
94 mLayerHistory->setVisibility(test30FpsLayer, true);
95 std::unique_ptr<LayerHistory::LayerHandle> testLayer2 =
96 mLayerHistory->createLayer("TestLayer2", MAX_REFRESH_RATE);
97 mLayerHistory->setVisibility(testLayer2, true);
98
99 nsecs_t startTime = systemTime();
100 for (int i = 0; i < 10; i++) {
101 mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
102 }
103 EXPECT_FLOAT_EQ(MAX_REFRESH_RATE, mLayerHistory->getDesiredRefreshRateAndHDR().first);
104
105 startTime = systemTime();
106 for (int i = 0; i < 10; i++) {
107 mLayerHistory->insert(test30FpsLayer, startTime + (i * 33333333), false /*isHDR*/);
108 }
109 EXPECT_FLOAT_EQ(MAX_REFRESH_RATE, mLayerHistory->getDesiredRefreshRateAndHDR().first);
110
111 for (int i = 10; i < 30; i++) {
112 mLayerHistory->insert(test30FpsLayer, startTime + (i * 33333333), false /*isHDR*/);
113 }
114 EXPECT_FLOAT_EQ(MAX_REFRESH_RATE, mLayerHistory->getDesiredRefreshRateAndHDR().first);
115
116 // This frame is only around for 9 occurrences, so it doesn't throw
117 // anything off.
118 for (int i = 0; i < 9; i++) {
119 mLayerHistory->insert(testLayer2, 0, false /*isHDR*/);
120 }
121 EXPECT_FLOAT_EQ(MAX_REFRESH_RATE, mLayerHistory->getDesiredRefreshRateAndHDR().first);
122 // After 100 ms frames become obsolete.
123 std::this_thread::sleep_for(std::chrono::milliseconds(500));
124 // Insert the 31st frame.
125 mLayerHistory->insert(test30FpsLayer, startTime + (30 * 33333333), false /*isHDR*/);
126 EXPECT_FLOAT_EQ(30.f, mLayerHistory->getDesiredRefreshRateAndHDR().first);
127 }
128
129 } // namespace
130 } // namespace scheduler
131 } // namespace android