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 MIN_REFRESH_RATE = 30.f;
29 static constexpr float MAX_REFRESH_RATE = 90.f;
30 static constexpr auto RELEVANT_FRAME_THRESHOLD = 90u;
31 static constexpr uint64_t THIRTY_FPS_INTERVAL = 33'333'333;
32
forceRelevancy(const std::unique_ptr<LayerHistory::LayerHandle> & testLayer)33 void forceRelevancy(const std::unique_ptr<LayerHistory::LayerHandle>& testLayer) {
34 mLayerHistory->setVisibility(testLayer, true);
35 for (auto i = 0u; i < RELEVANT_FRAME_THRESHOLD; i++) {
36 mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
37 }
38 };
39 };
40
LayerHistoryTest()41 LayerHistoryTest::LayerHistoryTest() {
42 mLayerHistory = std::make_unique<LayerHistory>();
43 }
~LayerHistoryTest()44 LayerHistoryTest::~LayerHistoryTest() {}
45
46 namespace {
TEST_F(LayerHistoryTest,oneLayer)47 TEST_F(LayerHistoryTest, oneLayer) {
48 std::unique_ptr<LayerHistory::LayerHandle> testLayer =
49 mLayerHistory->createLayer("TestLayer", MIN_REFRESH_RATE, MAX_REFRESH_RATE);
50 mLayerHistory->setVisibility(testLayer, true);
51 for (auto i = 0u; i < RELEVANT_FRAME_THRESHOLD; i++) {
52 EXPECT_FLOAT_EQ(0.f, mLayerHistory->getDesiredRefreshRateAndHDR().first);
53 mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
54 }
55
56 // Add a few more. This time we should get MAX refresh rate as the layer
57 // becomes relevant
58 static constexpr auto A_FEW = 10;
59 for (auto i = 0u; i < A_FEW; i++) {
60 EXPECT_FLOAT_EQ(MAX_REFRESH_RATE, mLayerHistory->getDesiredRefreshRateAndHDR().first);
61 mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
62 }
63 }
64
TEST_F(LayerHistoryTest,oneHDRLayer)65 TEST_F(LayerHistoryTest, oneHDRLayer) {
66 std::unique_ptr<LayerHistory::LayerHandle> testLayer =
67 mLayerHistory->createLayer("TestHDRLayer", MIN_REFRESH_RATE, MAX_REFRESH_RATE);
68 mLayerHistory->setVisibility(testLayer, true);
69
70 mLayerHistory->insert(testLayer, 0, true /*isHDR*/);
71 EXPECT_FLOAT_EQ(0.0f, mLayerHistory->getDesiredRefreshRateAndHDR().first);
72 EXPECT_EQ(true, mLayerHistory->getDesiredRefreshRateAndHDR().second);
73
74 mLayerHistory->setVisibility(testLayer, false);
75 EXPECT_FLOAT_EQ(0.0f, mLayerHistory->getDesiredRefreshRateAndHDR().first);
76 EXPECT_EQ(false, mLayerHistory->getDesiredRefreshRateAndHDR().second);
77 }
78
TEST_F(LayerHistoryTest,explicitTimestamp)79 TEST_F(LayerHistoryTest, explicitTimestamp) {
80 std::unique_ptr<LayerHistory::LayerHandle> test30FpsLayer =
81 mLayerHistory->createLayer("30FpsLayer", MIN_REFRESH_RATE, MAX_REFRESH_RATE);
82 mLayerHistory->setVisibility(test30FpsLayer, true);
83
84 nsecs_t startTime = systemTime();
85 for (int i = 0; i < RELEVANT_FRAME_THRESHOLD; i++) {
86 mLayerHistory->insert(test30FpsLayer, startTime + (i * THIRTY_FPS_INTERVAL),
87 false /*isHDR*/);
88 }
89
90 EXPECT_FLOAT_EQ(30.f, mLayerHistory->getDesiredRefreshRateAndHDR().first);
91 }
92
TEST_F(LayerHistoryTest,multipleLayers)93 TEST_F(LayerHistoryTest, multipleLayers) {
94 std::unique_ptr<LayerHistory::LayerHandle> testLayer =
95 mLayerHistory->createLayer("TestLayer", MIN_REFRESH_RATE, MAX_REFRESH_RATE);
96 mLayerHistory->setVisibility(testLayer, true);
97 std::unique_ptr<LayerHistory::LayerHandle> test30FpsLayer =
98 mLayerHistory->createLayer("30FpsLayer", MIN_REFRESH_RATE, MAX_REFRESH_RATE);
99 mLayerHistory->setVisibility(test30FpsLayer, true);
100 std::unique_ptr<LayerHistory::LayerHandle> testLayer2 =
101 mLayerHistory->createLayer("TestLayer2", MIN_REFRESH_RATE, MAX_REFRESH_RATE);
102 mLayerHistory->setVisibility(testLayer2, true);
103
104 nsecs_t startTime = systemTime();
105 for (int i = 0; i < RELEVANT_FRAME_THRESHOLD; i++) {
106 mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
107 }
108 EXPECT_FLOAT_EQ(MAX_REFRESH_RATE, mLayerHistory->getDesiredRefreshRateAndHDR().first);
109
110 startTime = systemTime();
111 for (int i = 0; i < RELEVANT_FRAME_THRESHOLD; i++) {
112 mLayerHistory->insert(test30FpsLayer, startTime + (i * THIRTY_FPS_INTERVAL),
113 false /*isHDR*/);
114 }
115 EXPECT_FLOAT_EQ(MAX_REFRESH_RATE, mLayerHistory->getDesiredRefreshRateAndHDR().first);
116
117 for (int i = 10; i < RELEVANT_FRAME_THRESHOLD; i++) {
118 mLayerHistory->insert(test30FpsLayer, startTime + (i * THIRTY_FPS_INTERVAL),
119 false /*isHDR*/);
120 }
121 EXPECT_FLOAT_EQ(MAX_REFRESH_RATE, mLayerHistory->getDesiredRefreshRateAndHDR().first);
122
123 // This frame is only around for 9 occurrences, so it doesn't throw
124 // anything off.
125 for (int i = 0; i < 9; i++) {
126 mLayerHistory->insert(testLayer2, 0, false /*isHDR*/);
127 }
128 EXPECT_FLOAT_EQ(MAX_REFRESH_RATE, mLayerHistory->getDesiredRefreshRateAndHDR().first);
129 // After 1200 ms frames become obsolete.
130 std::this_thread::sleep_for(std::chrono::milliseconds(1500));
131
132 mLayerHistory->insert(test30FpsLayer,
133 startTime + (RELEVANT_FRAME_THRESHOLD * THIRTY_FPS_INTERVAL),
134 false /*isHDR*/);
135 EXPECT_FLOAT_EQ(30.f, mLayerHistory->getDesiredRefreshRateAndHDR().first);
136 }
137
138 } // namespace
139 } // namespace scheduler
140 } // namespace android