blob: e0133d60106964a77ac7fd7e744fe3a89279301d [file] [log] [blame]
Marin Shalamanov2045d5b2020-12-28 18:11:41 +01001/*
2 * Copyright 2020 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 "LayerInfoTest"
19
20#include <gtest/gtest.h>
21
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080022#include <scheduler/Fps.h>
23
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070024#include "FpsOps.h"
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010025#include "Scheduler/LayerHistory.h"
26#include "Scheduler/LayerInfo.h"
Rachel Leece6e0042023-06-27 11:22:54 -070027#include "TestableScheduler.h"
28#include "TestableSurfaceFlinger.h"
29#include "mock/MockSchedulerCallback.h"
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010030
31namespace android::scheduler {
32
Rachel Leece6e0042023-06-27 11:22:54 -070033using android::mock::createDisplayMode;
34
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010035class LayerInfoTest : public testing::Test {
36protected:
37 using FrameTimeData = LayerInfo::FrameTimeData;
38
Rachel Leece6e0042023-06-27 11:22:54 -070039 static constexpr Fps LO_FPS = 30_Hz;
40 static constexpr Fps HI_FPS = 90_Hz;
41
42 LayerInfoTest() { mFlinger.resetScheduler(mScheduler); }
43
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010044 void setFrameTimes(const std::deque<FrameTimeData>& frameTimes) {
45 layerInfo.mFrameTimes = frameTimes;
46 }
47
48 void setLastRefreshRate(Fps fps) {
49 layerInfo.mLastRefreshRate.reported = fps;
50 layerInfo.mLastRefreshRate.calculated = fps;
51 }
52
53 auto calculateAverageFrameTime() { return layerInfo.calculateAverageFrameTime(); }
54
Ady Abrahambdda8f02021-04-01 16:06:11 -070055 LayerInfo layerInfo{"TestLayerInfo", 0, LayerHistory::LayerVoteType::Heuristic};
Rachel Leece6e0042023-06-27 11:22:54 -070056
57 std::shared_ptr<RefreshRateSelector> mSelector =
58 std::make_shared<RefreshRateSelector>(makeModes(createDisplayMode(DisplayModeId(0),
59 LO_FPS),
60 createDisplayMode(DisplayModeId(1),
61 HI_FPS)),
62 DisplayModeId(0));
63 mock::SchedulerCallback mSchedulerCallback;
64 TestableScheduler* mScheduler = new TestableScheduler(mSelector, mSchedulerCallback);
65 TestableSurfaceFlinger mFlinger;
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010066};
67
68namespace {
69
70TEST_F(LayerInfoTest, prefersPresentTime) {
71 std::deque<FrameTimeData> frameTimes;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070072 constexpr auto kExpectedFps = 50_Hz;
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010073 constexpr auto kPeriod = kExpectedFps.getPeriodNsecs();
74 constexpr int kNumFrames = 10;
75 for (int i = 1; i <= kNumFrames; i++) {
76 frameTimes.push_back(FrameTimeData{.presentTime = kPeriod * i,
77 .queueTime = 0,
Marin Shalamanova7fe3042021-01-29 21:02:08 +010078 .pendingModeChange = false});
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010079 }
80 setFrameTimes(frameTimes);
81 const auto averageFrameTime = calculateAverageFrameTime();
82 ASSERT_TRUE(averageFrameTime.has_value());
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070083 ASSERT_EQ(kExpectedFps, Fps::fromPeriodNsecs(*averageFrameTime));
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010084}
85
86TEST_F(LayerInfoTest, fallbacksToQueueTimeIfNoPresentTime) {
87 std::deque<FrameTimeData> frameTimes;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070088 constexpr auto kExpectedFps = 50_Hz;
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010089 constexpr auto kPeriod = kExpectedFps.getPeriodNsecs();
90 constexpr int kNumFrames = 10;
91 for (int i = 1; i <= kNumFrames; i++) {
92 frameTimes.push_back(FrameTimeData{.presentTime = 0,
93 .queueTime = kPeriod * i,
Marin Shalamanova7fe3042021-01-29 21:02:08 +010094 .pendingModeChange = false});
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010095 }
96 setFrameTimes(frameTimes);
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070097 setLastRefreshRate(20_Hz); // Set to some valid value.
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010098 const auto averageFrameTime = calculateAverageFrameTime();
99 ASSERT_TRUE(averageFrameTime.has_value());
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700100 ASSERT_EQ(kExpectedFps, Fps::fromPeriodNsecs(*averageFrameTime));
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100101}
102
103TEST_F(LayerInfoTest, returnsNulloptIfThereWasConfigChange) {
104 std::deque<FrameTimeData> frameTimesWithoutConfigChange;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700105 const auto period = (50_Hz).getPeriodNsecs();
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100106 constexpr int kNumFrames = 10;
107 for (int i = 1; i <= kNumFrames; i++) {
108 frameTimesWithoutConfigChange.push_back(FrameTimeData{.presentTime = period * i,
109 .queueTime = period * i,
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100110 .pendingModeChange = false});
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100111 }
112
113 setFrameTimes(frameTimesWithoutConfigChange);
114 ASSERT_TRUE(calculateAverageFrameTime().has_value());
115
116 {
117 // Config change in the first record
118 auto frameTimes = frameTimesWithoutConfigChange;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100119 frameTimes[0].pendingModeChange = true;
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100120 setFrameTimes(frameTimes);
121 ASSERT_FALSE(calculateAverageFrameTime().has_value());
122 }
123
124 {
125 // Config change in the last record
126 auto frameTimes = frameTimesWithoutConfigChange;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100127 frameTimes[frameTimes.size() - 1].pendingModeChange = true;
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100128 setFrameTimes(frameTimes);
129 ASSERT_FALSE(calculateAverageFrameTime().has_value());
130 }
131
132 {
133 // Config change in the middle
134 auto frameTimes = frameTimesWithoutConfigChange;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100135 frameTimes[frameTimes.size() / 2].pendingModeChange = true;
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100136 setFrameTimes(frameTimes);
137 ASSERT_FALSE(calculateAverageFrameTime().has_value());
138 }
139}
140
141// A frame can be recorded twice with very close presentation or queue times.
142// Make sure that this doesn't influence the calculated average FPS.
143TEST_F(LayerInfoTest, ignoresSmallPeriods) {
144 std::deque<FrameTimeData> frameTimes;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700145 constexpr auto kExpectedFps = 50_Hz;
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100146 constexpr auto kExpectedPeriod = kExpectedFps.getPeriodNsecs();
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700147 constexpr auto kSmallPeriod = (250_Hz).getPeriodNsecs();
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100148 constexpr int kNumIterations = 10;
149 for (int i = 1; i <= kNumIterations; i++) {
150 frameTimes.push_back(FrameTimeData{.presentTime = kExpectedPeriod * i,
151 .queueTime = 0,
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100152 .pendingModeChange = false});
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100153
154 // A duplicate frame
155 frameTimes.push_back(FrameTimeData{.presentTime = kExpectedPeriod * i + kSmallPeriod,
156 .queueTime = 0,
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100157 .pendingModeChange = false});
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100158 }
159 setFrameTimes(frameTimes);
160 const auto averageFrameTime = calculateAverageFrameTime();
161 ASSERT_TRUE(averageFrameTime.has_value());
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700162 ASSERT_EQ(kExpectedFps, Fps::fromPeriodNsecs(*averageFrameTime));
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100163}
164
165// There may be a big period of time between two frames. Make sure that
166// this doesn't influence the calculated average FPS.
167TEST_F(LayerInfoTest, ignoresLargePeriods) {
168 std::deque<FrameTimeData> frameTimes;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700169 constexpr auto kExpectedFps = 50_Hz;
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100170 constexpr auto kExpectedPeriod = kExpectedFps.getPeriodNsecs();
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700171 constexpr auto kLargePeriod = (9_Hz).getPeriodNsecs();
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100172
173 auto record = [&](nsecs_t time) {
174 frameTimes.push_back(
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100175 FrameTimeData{.presentTime = time, .queueTime = 0, .pendingModeChange = false});
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100176 };
177
178 auto time = kExpectedPeriod; // Start with non-zero time.
179 record(time);
180 time += kLargePeriod;
181 record(time);
182 constexpr int kNumIterations = 10;
183 for (int i = 1; i <= kNumIterations; i++) {
184 time += kExpectedPeriod;
185 record(time);
186 }
187
188 setFrameTimes(frameTimes);
189 const auto averageFrameTime = calculateAverageFrameTime();
190 ASSERT_TRUE(averageFrameTime.has_value());
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700191 ASSERT_EQ(kExpectedFps, Fps::fromPeriodNsecs(*averageFrameTime));
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100192}
193
Rachel Leece6e0042023-06-27 11:22:54 -0700194TEST_F(LayerInfoTest, getRefreshRateVote_explicitVote) {
195 LayerInfo::LayerVote vote = {.type = LayerHistory::LayerVoteType::ExplicitDefault,
196 .fps = 20_Hz};
197 layerInfo.setLayerVote(vote);
198
199 auto actualVotes =
200 layerInfo.getRefreshRateVote(*mScheduler->refreshRateSelector(), systemTime());
201 ASSERT_EQ(actualVotes.size(), 1u);
202 ASSERT_EQ(actualVotes[0].type, vote.type);
203 ASSERT_EQ(actualVotes[0].fps, vote.fps);
204 ASSERT_EQ(actualVotes[0].seamlessness, vote.seamlessness);
205 ASSERT_EQ(actualVotes[0].category, vote.category);
206}
207
208TEST_F(LayerInfoTest, getRefreshRateVote_explicitVoteWithCategory) {
209 LayerInfo::LayerVote vote = {.type = LayerHistory::LayerVoteType::ExplicitDefault,
210 .fps = 20_Hz,
211 .category = FrameRateCategory::High};
212 layerInfo.setLayerVote(vote);
213
214 auto actualVotes =
215 layerInfo.getRefreshRateVote(*mScheduler->refreshRateSelector(), systemTime());
216 ASSERT_EQ(actualVotes.size(), 2u);
217 ASSERT_EQ(actualVotes[0].type, LayerHistory::LayerVoteType::ExplicitCategory);
218 ASSERT_EQ(actualVotes[0].category, vote.category);
219 ASSERT_EQ(actualVotes[1].type, vote.type);
220 ASSERT_EQ(actualVotes[1].fps, vote.fps);
221 ASSERT_EQ(actualVotes[1].seamlessness, vote.seamlessness);
222 ASSERT_EQ(actualVotes[1].category, vote.category);
223}
224
225TEST_F(LayerInfoTest, getRefreshRateVote_explicitCategory) {
226 // When a layer only has a category set, the LayerVoteType should be the LayerInfo's default.
227 // The most common case should be Heuristic.
228 LayerInfo::LayerVote vote = {.type = LayerHistory::LayerVoteType::ExplicitDefault,
229 .category = FrameRateCategory::High};
230 layerInfo.setLayerVote(vote);
231
232 auto actualVotes =
233 layerInfo.getRefreshRateVote(*mScheduler->refreshRateSelector(), systemTime());
234 ASSERT_EQ(actualVotes.size(), 1u);
235 ASSERT_EQ(actualVotes[0].type, LayerHistory::LayerVoteType::ExplicitCategory);
236 ASSERT_EQ(actualVotes[0].category, vote.category);
237}
238
239TEST_F(LayerInfoTest, getRefreshRateVote_noData) {
240 LayerInfo::LayerVote vote = {
241 .type = LayerHistory::LayerVoteType::Heuristic,
242 };
243 layerInfo.setLayerVote(vote);
244
245 auto actualVotes =
246 layerInfo.getRefreshRateVote(*mScheduler->refreshRateSelector(), systemTime());
247 ASSERT_EQ(actualVotes.size(), 1u);
248 ASSERT_EQ(actualVotes[0].type, LayerHistory::LayerVoteType::Max);
249 ASSERT_EQ(actualVotes[0].fps, vote.fps);
250}
251
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100252} // namespace
253} // namespace android::scheduler