File: LayerInfoTest.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 (286 lines) | stat: -rw-r--r-- 11,458 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
/*
 * Copyright 2020 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 "LayerInfoTest"

#include <gtest/gtest.h>

#include <scheduler/Fps.h>

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

#include <com_android_graphics_surfaceflinger_flags.h>

namespace android::scheduler {

using android::mock::createDisplayMode;

class LayerInfoTest : public testing::Test {
protected:
    using FrameTimeData = LayerInfo::FrameTimeData;

    static constexpr Fps LO_FPS = 30_Hz;
    static constexpr Fps HI_FPS = 90_Hz;

    LayerInfoTest() { mFlinger.resetScheduler(mScheduler); }

    void setFrameTimes(const std::deque<FrameTimeData>& frameTimes) {
        layerInfo.mFrameTimes = frameTimes;
    }

    void setLastRefreshRate(Fps fps) {
        layerInfo.mLastRefreshRate.reported = fps;
        layerInfo.mLastRefreshRate.calculated = fps;
    }

    auto calculateAverageFrameTime() { return layerInfo.calculateAverageFrameTime(); }

    LayerInfo layerInfo{"TestLayerInfo", 0, LayerHistory::LayerVoteType::Heuristic};

    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 {

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

TEST_F(LayerInfoTest, prefersPresentTime) {
    std::deque<FrameTimeData> frameTimes;
    constexpr auto kExpectedFps = 50_Hz;
    constexpr auto kPeriod = kExpectedFps.getPeriodNsecs();
    constexpr int kNumFrames = 10;
    for (int i = 1; i <= kNumFrames; i++) {
        frameTimes.push_back(FrameTimeData{.presentTime = kPeriod * i,
                                           .queueTime = 0,
                                           .pendingModeChange = false});
    }
    setFrameTimes(frameTimes);
    const auto averageFrameTime = calculateAverageFrameTime();
    ASSERT_TRUE(averageFrameTime.has_value());
    ASSERT_EQ(kExpectedFps, Fps::fromPeriodNsecs(*averageFrameTime));
}

TEST_F(LayerInfoTest, fallbacksToQueueTimeIfNoPresentTime) {
    std::deque<FrameTimeData> frameTimes;
    constexpr auto kExpectedFps = 50_Hz;
    constexpr auto kPeriod = kExpectedFps.getPeriodNsecs();
    constexpr int kNumFrames = 10;
    for (int i = 1; i <= kNumFrames; i++) {
        frameTimes.push_back(FrameTimeData{.presentTime = 0,
                                           .queueTime = kPeriod * i,
                                           .pendingModeChange = false});
    }
    setFrameTimes(frameTimes);
    setLastRefreshRate(20_Hz); // Set to some valid value.
    const auto averageFrameTime = calculateAverageFrameTime();
    ASSERT_TRUE(averageFrameTime.has_value());
    ASSERT_EQ(kExpectedFps, Fps::fromPeriodNsecs(*averageFrameTime));
}

TEST_F(LayerInfoTest, returnsNulloptIfThereWasConfigChange) {
    std::deque<FrameTimeData> frameTimesWithoutConfigChange;
    const auto period = (50_Hz).getPeriodNsecs();
    constexpr int kNumFrames = 10;
    for (int i = 1; i <= kNumFrames; i++) {
        frameTimesWithoutConfigChange.push_back(FrameTimeData{.presentTime = period * i,
                                                              .queueTime = period * i,
                                                              .pendingModeChange = false});
    }

    setFrameTimes(frameTimesWithoutConfigChange);
    ASSERT_TRUE(calculateAverageFrameTime().has_value());

    {
        // Config change in the first record
        auto frameTimes = frameTimesWithoutConfigChange;
        frameTimes[0].pendingModeChange = true;
        setFrameTimes(frameTimes);
        ASSERT_FALSE(calculateAverageFrameTime().has_value());
    }

    {
        // Config change in the last record
        auto frameTimes = frameTimesWithoutConfigChange;
        frameTimes[frameTimes.size() - 1].pendingModeChange = true;
        setFrameTimes(frameTimes);
        ASSERT_FALSE(calculateAverageFrameTime().has_value());
    }

    {
        // Config change in the middle
        auto frameTimes = frameTimesWithoutConfigChange;
        frameTimes[frameTimes.size() / 2].pendingModeChange = true;
        setFrameTimes(frameTimes);
        ASSERT_FALSE(calculateAverageFrameTime().has_value());
    }
}

// A frame can be recorded twice with very close presentation or queue times.
// Make sure that this doesn't influence the calculated average FPS.
TEST_F(LayerInfoTest, ignoresSmallPeriods) {
    std::deque<FrameTimeData> frameTimes;
    constexpr auto kExpectedFps = 50_Hz;
    constexpr auto kExpectedPeriod = kExpectedFps.getPeriodNsecs();
    constexpr auto kSmallPeriod = (250_Hz).getPeriodNsecs();
    constexpr int kNumIterations = 10;
    for (int i = 1; i <= kNumIterations; i++) {
        frameTimes.push_back(FrameTimeData{.presentTime = kExpectedPeriod * i,
                                           .queueTime = 0,
                                           .pendingModeChange = false});

        // A duplicate frame
        frameTimes.push_back(FrameTimeData{.presentTime = kExpectedPeriod * i + kSmallPeriod,
                                           .queueTime = 0,
                                           .pendingModeChange = false});
    }
    setFrameTimes(frameTimes);
    const auto averageFrameTime = calculateAverageFrameTime();
    ASSERT_TRUE(averageFrameTime.has_value());
    ASSERT_EQ(kExpectedFps, Fps::fromPeriodNsecs(*averageFrameTime));
}

// There may be a big period of time between two frames. Make sure that
// this doesn't influence the calculated average FPS.
TEST_F(LayerInfoTest, ignoresLargePeriods) {
    std::deque<FrameTimeData> frameTimes;
    constexpr auto kExpectedFps = 50_Hz;
    constexpr auto kExpectedPeriod = kExpectedFps.getPeriodNsecs();
    constexpr auto kLargePeriod = (9_Hz).getPeriodNsecs();

    auto record = [&](nsecs_t time) {
        frameTimes.push_back(
                FrameTimeData{.presentTime = time, .queueTime = 0, .pendingModeChange = false});
    };

    auto time = kExpectedPeriod; // Start with non-zero time.
    record(time);
    time += kLargePeriod;
    record(time);
    constexpr int kNumIterations = 10;
    for (int i = 1; i <= kNumIterations; i++) {
        time += kExpectedPeriod;
        record(time);
    }

    setFrameTimes(frameTimes);
    const auto averageFrameTime = calculateAverageFrameTime();
    ASSERT_TRUE(averageFrameTime.has_value());
    ASSERT_EQ(kExpectedFps, Fps::fromPeriodNsecs(*averageFrameTime));
}

TEST_F(LayerInfoTest, getRefreshRateVote_explicitVote) {
    LayerInfo::LayerVote vote = {.type = LayerHistory::LayerVoteType::ExplicitDefault,
                                 .fps = 20_Hz};
    layerInfo.setLayerVote(vote);

    auto actualVotes =
            layerInfo.getRefreshRateVote(*mScheduler->refreshRateSelector(), systemTime());
    ASSERT_EQ(actualVotes.size(), 1u);
    ASSERT_EQ(actualVotes[0].type, vote.type);
    ASSERT_EQ(actualVotes[0].fps, vote.fps);
    ASSERT_EQ(actualVotes[0].seamlessness, vote.seamlessness);
    ASSERT_EQ(actualVotes[0].category, vote.category);
}

TEST_F(LayerInfoTest, getRefreshRateVote_explicitVoteWithCategory) {
    LayerInfo::LayerVote vote = {.type = LayerHistory::LayerVoteType::ExplicitDefault,
                                 .fps = 20_Hz,
                                 .category = FrameRateCategory::High,
                                 .categorySmoothSwitchOnly = true};
    layerInfo.setLayerVote(vote);

    auto actualVotes =
            layerInfo.getRefreshRateVote(*mScheduler->refreshRateSelector(), systemTime());
    ASSERT_EQ(actualVotes.size(), 2u);
    ASSERT_EQ(actualVotes[0].type, LayerHistory::LayerVoteType::ExplicitCategory);
    ASSERT_EQ(actualVotes[0].category, vote.category);
    ASSERT_TRUE(actualVotes[0].categorySmoothSwitchOnly);
    ASSERT_EQ(actualVotes[1].type, vote.type);
    ASSERT_EQ(actualVotes[1].fps, vote.fps);
    ASSERT_EQ(actualVotes[1].seamlessness, vote.seamlessness);
    ASSERT_EQ(actualVotes[1].category, FrameRateCategory::Default);
    ASSERT_TRUE(actualVotes[1].categorySmoothSwitchOnly);
}

TEST_F(LayerInfoTest, getRefreshRateVote_explicitCategory) {
    LayerInfo::LayerVote vote = {.type = LayerHistory::LayerVoteType::ExplicitDefault,
                                 .category = FrameRateCategory::High};
    layerInfo.setLayerVote(vote);

    auto actualVotes =
            layerInfo.getRefreshRateVote(*mScheduler->refreshRateSelector(), systemTime());
    ASSERT_EQ(actualVotes.size(), 1u);
    ASSERT_EQ(actualVotes[0].type, LayerHistory::LayerVoteType::ExplicitCategory);
    ASSERT_EQ(actualVotes[0].category, vote.category);
    ASSERT_EQ(actualVotes[0].fps, 0_Hz);
}

TEST_F(LayerInfoTest, getRefreshRateVote_categoryNoPreference) {
    LayerInfo::LayerVote vote = {.type = LayerHistory::LayerVoteType::ExplicitDefault,
                                 .category = FrameRateCategory::NoPreference};
    layerInfo.setLayerVote(vote);

    auto actualVotes =
            layerInfo.getRefreshRateVote(*mScheduler->refreshRateSelector(), systemTime());
    ASSERT_EQ(actualVotes.size(), 1u);
    ASSERT_EQ(actualVotes[0].type, LayerHistory::LayerVoteType::ExplicitCategory);
    ASSERT_EQ(actualVotes[0].category, vote.category);
    ASSERT_EQ(actualVotes[0].fps, 0_Hz);
}

TEST_F(LayerInfoTest, getRefreshRateVote_noData) {
    LayerInfo::LayerVote vote = {
            .type = LayerHistory::LayerVoteType::Heuristic,
    };
    layerInfo.setLayerVote(vote);

    auto actualVotes =
            layerInfo.getRefreshRateVote(*mScheduler->refreshRateSelector(), systemTime());
    ASSERT_EQ(actualVotes.size(), 1u);
    ASSERT_EQ(actualVotes[0].type, LayerHistory::LayerVoteType::Max);
    ASSERT_EQ(actualVotes[0].fps, vote.fps);
}

TEST_F(LayerInfoTest, isFrontBuffered) {
    SET_FLAG_FOR_TEST(flags::vrr_config, true);
    ASSERT_FALSE(layerInfo.isFrontBuffered());

    LayerProps prop = {.isFrontBuffered = true};
    layerInfo.setLastPresentTime(0, 0, LayerHistory::LayerUpdateType::Buffer, true, prop);
    ASSERT_TRUE(layerInfo.isFrontBuffered());

    prop.isFrontBuffered = false;
    layerInfo.setLastPresentTime(0, 0, LayerHistory::LayerUpdateType::Buffer, true, prop);
    ASSERT_FALSE(layerInfo.isFrontBuffered());
}

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