1 /*
2 * Copyright 2022 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 #include <gtest/gtest.h>
18
19 #include <algorithm>
20 #include <array>
21
22 #include <scheduler/PresentLatencyTracker.h>
23 #include <ui/FenceTime.h>
24
25 namespace android::scheduler {
26
TEST(PresentLatencyTrackerTest,skipsInvalidFences)27 TEST(PresentLatencyTrackerTest, skipsInvalidFences) {
28 PresentLatencyTracker tracker;
29
30 const TimePoint kCompositeTime = TimePoint::fromNs(999);
31 EXPECT_EQ(tracker.trackPendingFrame(kCompositeTime, FenceTime::NO_FENCE), Duration::zero());
32 EXPECT_EQ(tracker.trackPendingFrame(kCompositeTime, FenceTime::NO_FENCE), Duration::zero());
33 EXPECT_EQ(tracker.trackPendingFrame(kCompositeTime, FenceTime::NO_FENCE), Duration::zero());
34
35 FenceToFenceTimeMap fenceMap;
36 const auto [fence, fenceTime] = fenceMap.makePendingFenceForTest();
37 EXPECT_EQ(tracker.trackPendingFrame(kCompositeTime, fenceTime), Duration::zero());
38
39 fenceTime->signalForTest(9999);
40
41 EXPECT_EQ(tracker.trackPendingFrame(kCompositeTime, FenceTime::NO_FENCE),
42 Duration::fromNs(9000));
43 }
44
TEST(PresentLatencyTrackerTest,tracksPendingFrames)45 TEST(PresentLatencyTrackerTest, tracksPendingFrames) {
46 PresentLatencyTracker tracker;
47
48 FenceToFenceTimeMap fenceMap;
49 std::array<FenceToFenceTimeMap::FencePair, PresentLatencyTracker::kMaxPendingFrames> fences;
50 std::generate(fences.begin(), fences.end(),
51 [&fenceMap] { return fenceMap.makePendingFenceForTest(); });
52
53 // The present latency is 0 if all fences are pending.
54 const TimePoint kCompositeTime = TimePoint::fromNs(1234);
55 for (const auto& [fence, fenceTime] : fences) {
56 EXPECT_EQ(tracker.trackPendingFrame(kCompositeTime, fenceTime), Duration::zero());
57 }
58
59 // If multiple frames have been presented...
60 constexpr size_t kPresentCount = fences.size() / 2;
61 for (size_t i = 0; i < kPresentCount; i++) {
62 fences[i].second->signalForTest(kCompositeTime.ns() + static_cast<nsecs_t>(i));
63 }
64
65 const auto fence = fenceMap.makePendingFenceForTest();
66
67 // ...then the present latency is measured using the latest frame.
68 constexpr Duration kPresentLatency = Duration::fromNs(static_cast<nsecs_t>(kPresentCount) - 1);
69 EXPECT_EQ(tracker.trackPendingFrame(kCompositeTime, fence.second), kPresentLatency);
70 }
71
72 } // namespace android::scheduler
73