| Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2023 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 <ftl/optional.h> | 
|  | 18 | #include <gtest/gtest.h> | 
|  | 19 |  | 
|  | 20 | #include <scheduler/Fps.h> | 
|  | 21 | #include <scheduler/FrameTargeter.h> | 
|  | 22 | #include <scheduler/IVsyncSource.h> | 
|  | 23 |  | 
|  | 24 | using namespace std::chrono_literals; | 
|  | 25 |  | 
|  | 26 | namespace android::scheduler { | 
|  | 27 | namespace { | 
|  | 28 |  | 
|  | 29 | struct VsyncSource final : IVsyncSource { | 
|  | 30 | VsyncSource(Period period, TimePoint deadline) : vsyncPeriod(period), vsyncDeadline(deadline) {} | 
|  | 31 |  | 
|  | 32 | const Period vsyncPeriod; | 
|  | 33 | const TimePoint vsyncDeadline; | 
|  | 34 |  | 
|  | 35 | Period period() const override { return vsyncPeriod; } | 
|  | 36 | TimePoint vsyncDeadlineAfter(TimePoint) const override { return vsyncDeadline; } | 
|  | 37 | }; | 
|  | 38 |  | 
|  | 39 | } // namespace | 
|  | 40 |  | 
|  | 41 | class FrameTargeterTest : public testing::Test { | 
|  | 42 | public: | 
|  | 43 | const auto& target() const { return mTargeter.target(); } | 
|  | 44 |  | 
|  | 45 | struct Frame { | 
|  | 46 | Frame(FrameTargeterTest* testPtr, VsyncId vsyncId, TimePoint& frameBeginTime, | 
|  | 47 | Duration frameDuration, Fps refreshRate, | 
|  | 48 | FrameTargeter::IsFencePendingFuncPtr isFencePendingFuncPtr = Frame::fenceSignaled, | 
|  | 49 | const ftl::Optional<VsyncSource>& vsyncSourceOpt = std::nullopt) | 
|  | 50 | : testPtr(testPtr), frameBeginTime(frameBeginTime), period(refreshRate.getPeriod()) { | 
|  | 51 | const FrameTargeter::BeginFrameArgs args{.frameBeginTime = frameBeginTime, | 
|  | 52 | .vsyncId = vsyncId, | 
|  | 53 | .expectedVsyncTime = | 
|  | 54 | frameBeginTime + frameDuration, | 
|  | 55 | .sfWorkDuration = 10ms}; | 
|  | 56 |  | 
|  | 57 | testPtr->mTargeter.beginFrame(args, | 
|  | 58 | vsyncSourceOpt | 
|  | 59 | .or_else([&] { | 
|  | 60 | return std::make_optional( | 
|  | 61 | VsyncSource(period, | 
|  | 62 | args.expectedVsyncTime)); | 
|  | 63 | }) | 
|  | 64 | .value(), | 
|  | 65 | isFencePendingFuncPtr); | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | FenceTimePtr end(CompositionCoverage coverage = CompositionCoverage::Hwc) { | 
|  | 69 | if (ended) return nullptr; | 
|  | 70 | ended = true; | 
|  | 71 |  | 
|  | 72 | auto [fence, fenceTime] = testPtr->mFenceMap.makePendingFenceForTest(); | 
|  | 73 | testPtr->mTargeter.setPresentFence(std::move(fence), fenceTime); | 
|  | 74 |  | 
|  | 75 | testPtr->mTargeter.endFrame({.compositionCoverage = coverage}); | 
|  | 76 | return fenceTime; | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | ~Frame() { | 
|  | 80 | end(); | 
|  | 81 | frameBeginTime += period; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | static bool fencePending(const FenceTimePtr&, int) { return true; } | 
|  | 85 | static bool fenceSignaled(const FenceTimePtr&, int) { return false; } | 
|  | 86 |  | 
|  | 87 | FrameTargeterTest* const testPtr; | 
|  | 88 |  | 
|  | 89 | TimePoint& frameBeginTime; | 
|  | 90 | const Period period; | 
|  | 91 |  | 
|  | 92 | bool ended = false; | 
|  | 93 | }; | 
|  | 94 |  | 
|  | 95 | private: | 
|  | 96 | FenceToFenceTimeMap mFenceMap; | 
|  | 97 |  | 
|  | 98 | static constexpr bool kBackpressureGpuComposition = true; | 
| Dominik Laskowski | 419840f | 2023-01-28 16:16:19 -0500 | [diff] [blame] | 99 | FrameTargeter mTargeter{PhysicalDisplayId::fromPort(13), kBackpressureGpuComposition}; | 
| Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 100 | }; | 
|  | 101 |  | 
|  | 102 | TEST_F(FrameTargeterTest, targetsFrames) { | 
|  | 103 | VsyncId vsyncId{42}; | 
|  | 104 | { | 
|  | 105 | TimePoint frameBeginTime(989ms); | 
|  | 106 | const Frame frame(this, vsyncId++, frameBeginTime, 10ms, 60_Hz); | 
|  | 107 |  | 
|  | 108 | EXPECT_EQ(target().vsyncId(), VsyncId{42}); | 
|  | 109 | EXPECT_EQ(target().frameBeginTime(), TimePoint(989ms)); | 
|  | 110 | EXPECT_EQ(target().expectedPresentTime(), TimePoint(999ms)); | 
|  | 111 | EXPECT_EQ(target().expectedFrameDuration(), 10ms); | 
|  | 112 | } | 
|  | 113 | { | 
|  | 114 | TimePoint frameBeginTime(1100ms); | 
|  | 115 | const Frame frame(this, vsyncId++, frameBeginTime, 11ms, 60_Hz); | 
|  | 116 |  | 
|  | 117 | EXPECT_EQ(target().vsyncId(), VsyncId{43}); | 
|  | 118 | EXPECT_EQ(target().frameBeginTime(), TimePoint(1100ms)); | 
|  | 119 | EXPECT_EQ(target().expectedPresentTime(), TimePoint(1111ms)); | 
|  | 120 | EXPECT_EQ(target().expectedFrameDuration(), 11ms); | 
|  | 121 | } | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | TEST_F(FrameTargeterTest, inflatesExpectedPresentTime) { | 
|  | 125 | // Negative such that `expectedVsyncTime` is in the past. | 
|  | 126 | constexpr Duration kFrameDuration = -3ms; | 
|  | 127 | TimePoint frameBeginTime(777ms); | 
|  | 128 |  | 
|  | 129 | constexpr Fps kRefreshRate = 120_Hz; | 
|  | 130 | const VsyncSource vsyncSource(kRefreshRate.getPeriod(), frameBeginTime + 5ms); | 
|  | 131 | const Frame frame(this, VsyncId{123}, frameBeginTime, kFrameDuration, kRefreshRate, | 
|  | 132 | Frame::fenceSignaled, vsyncSource); | 
|  | 133 |  | 
|  | 134 | EXPECT_EQ(target().expectedPresentTime(), vsyncSource.vsyncDeadline + vsyncSource.vsyncPeriod); | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | TEST_F(FrameTargeterTest, recallsPastVsync) { | 
|  | 138 | VsyncId vsyncId{111}; | 
|  | 139 | TimePoint frameBeginTime(1000ms); | 
|  | 140 | constexpr Fps kRefreshRate = 60_Hz; | 
|  | 141 | constexpr Period kPeriod = kRefreshRate.getPeriod(); | 
|  | 142 | constexpr Duration kFrameDuration = 13ms; | 
|  | 143 |  | 
|  | 144 | for (int n = 5; n-- > 0;) { | 
|  | 145 | Frame frame(this, vsyncId++, frameBeginTime, kFrameDuration, kRefreshRate); | 
|  | 146 | const auto fence = frame.end(); | 
|  | 147 |  | 
|  | 148 | EXPECT_EQ(target().pastVsyncTime(kPeriod), frameBeginTime + kFrameDuration - kPeriod); | 
|  | 149 | EXPECT_EQ(target().presentFenceForPastVsync(kPeriod), fence); | 
|  | 150 | } | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | TEST_F(FrameTargeterTest, recallsPastVsyncTwoVsyncsAhead) { | 
|  | 154 | VsyncId vsyncId{222}; | 
|  | 155 | TimePoint frameBeginTime(2000ms); | 
|  | 156 | constexpr Fps kRefreshRate = 120_Hz; | 
|  | 157 | constexpr Period kPeriod = kRefreshRate.getPeriod(); | 
|  | 158 | constexpr Duration kFrameDuration = 10ms; | 
|  | 159 |  | 
|  | 160 | FenceTimePtr previousFence = FenceTime::NO_FENCE; | 
|  | 161 |  | 
|  | 162 | for (int n = 5; n-- > 0;) { | 
|  | 163 | Frame frame(this, vsyncId++, frameBeginTime, kFrameDuration, kRefreshRate); | 
|  | 164 | const auto fence = frame.end(); | 
|  | 165 |  | 
|  | 166 | EXPECT_EQ(target().pastVsyncTime(kPeriod), frameBeginTime + kFrameDuration - 2 * kPeriod); | 
|  | 167 | EXPECT_EQ(target().presentFenceForPastVsync(kPeriod), previousFence); | 
|  | 168 |  | 
|  | 169 | previousFence = fence; | 
|  | 170 | } | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | TEST_F(FrameTargeterTest, doesNotDetectEarlyPresentIfNoFence) { | 
|  | 174 | constexpr Period kPeriod = (60_Hz).getPeriod(); | 
|  | 175 | EXPECT_EQ(target().presentFenceForPastVsync(kPeriod), FenceTime::NO_FENCE); | 
|  | 176 | EXPECT_FALSE(target().wouldPresentEarly(kPeriod)); | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | TEST_F(FrameTargeterTest, detectsEarlyPresent) { | 
|  | 180 | VsyncId vsyncId{333}; | 
|  | 181 | TimePoint frameBeginTime(3000ms); | 
|  | 182 | constexpr Fps kRefreshRate = 60_Hz; | 
|  | 183 | constexpr Period kPeriod = kRefreshRate.getPeriod(); | 
|  | 184 |  | 
|  | 185 | // The target is not early while past present fences are pending. | 
|  | 186 | for (int n = 3; n-- > 0;) { | 
|  | 187 | const Frame frame(this, vsyncId++, frameBeginTime, 10ms, kRefreshRate); | 
|  | 188 | EXPECT_FALSE(target().wouldPresentEarly(kPeriod)); | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | // The target is early if the past present fence was signaled. | 
|  | 192 | Frame frame(this, vsyncId++, frameBeginTime, 10ms, kRefreshRate); | 
|  | 193 | const auto fence = frame.end(); | 
|  | 194 | fence->signalForTest(frameBeginTime.ns()); | 
|  | 195 |  | 
|  | 196 | EXPECT_TRUE(target().wouldPresentEarly(kPeriod)); | 
|  | 197 | } | 
|  | 198 |  | 
|  | 199 | TEST_F(FrameTargeterTest, detectsEarlyPresentTwoVsyncsAhead) { | 
|  | 200 | VsyncId vsyncId{444}; | 
|  | 201 | TimePoint frameBeginTime(4000ms); | 
|  | 202 | constexpr Fps kRefreshRate = 120_Hz; | 
|  | 203 | constexpr Period kPeriod = kRefreshRate.getPeriod(); | 
|  | 204 |  | 
|  | 205 | // The target is not early while past present fences are pending. | 
|  | 206 | for (int n = 3; n-- > 0;) { | 
|  | 207 | const Frame frame(this, vsyncId++, frameBeginTime, 10ms, kRefreshRate); | 
|  | 208 | EXPECT_FALSE(target().wouldPresentEarly(kPeriod)); | 
|  | 209 | } | 
|  | 210 |  | 
|  | 211 | Frame frame(this, vsyncId++, frameBeginTime, 10ms, kRefreshRate); | 
|  | 212 | const auto fence = frame.end(); | 
|  | 213 | fence->signalForTest(frameBeginTime.ns()); | 
|  | 214 |  | 
|  | 215 | // The target is two VSYNCs ahead, so the past present fence is still pending. | 
|  | 216 | EXPECT_FALSE(target().wouldPresentEarly(kPeriod)); | 
|  | 217 |  | 
|  | 218 | { const Frame frame(this, vsyncId++, frameBeginTime, 10ms, kRefreshRate); } | 
|  | 219 |  | 
|  | 220 | // The target is early if the past present fence was signaled. | 
|  | 221 | EXPECT_TRUE(target().wouldPresentEarly(kPeriod)); | 
|  | 222 | } | 
|  | 223 |  | 
|  | 224 | TEST_F(FrameTargeterTest, detectsEarlyPresentThreeVsyncsAhead) { | 
|  | 225 | TimePoint frameBeginTime(5000ms); | 
|  | 226 | constexpr Fps kRefreshRate = 144_Hz; | 
|  | 227 | constexpr Period kPeriod = kRefreshRate.getPeriod(); | 
|  | 228 |  | 
|  | 229 | const Frame frame(this, VsyncId{555}, frameBeginTime, 16ms, kRefreshRate); | 
|  | 230 |  | 
|  | 231 | // The target is more than two VSYNCs ahead, but present fences are not tracked that far back. | 
|  | 232 | EXPECT_TRUE(target().wouldPresentEarly(kPeriod)); | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | TEST_F(FrameTargeterTest, detectsMissedFrames) { | 
|  | 236 | VsyncId vsyncId{555}; | 
|  | 237 | TimePoint frameBeginTime(5000ms); | 
|  | 238 | constexpr Fps kRefreshRate = 60_Hz; | 
|  | 239 | constexpr Period kPeriod = kRefreshRate.getPeriod(); | 
|  | 240 |  | 
|  | 241 | EXPECT_FALSE(target().isFramePending()); | 
|  | 242 | EXPECT_FALSE(target().didMissFrame()); | 
|  | 243 | EXPECT_FALSE(target().didMissHwcFrame()); | 
|  | 244 |  | 
|  | 245 | { | 
|  | 246 | const Frame frame(this, vsyncId++, frameBeginTime, 10ms, kRefreshRate); | 
|  | 247 | EXPECT_FALSE(target().isFramePending()); | 
|  | 248 |  | 
|  | 249 | // The frame did not miss if the past present fence is invalid. | 
|  | 250 | EXPECT_FALSE(target().didMissFrame()); | 
|  | 251 | EXPECT_FALSE(target().didMissHwcFrame()); | 
|  | 252 | } | 
|  | 253 | { | 
|  | 254 | Frame frame(this, vsyncId++, frameBeginTime, 10ms, kRefreshRate, Frame::fencePending); | 
|  | 255 | EXPECT_TRUE(target().isFramePending()); | 
|  | 256 |  | 
|  | 257 | // The frame missed if the past present fence is pending. | 
|  | 258 | EXPECT_TRUE(target().didMissFrame()); | 
|  | 259 | EXPECT_TRUE(target().didMissHwcFrame()); | 
|  | 260 |  | 
|  | 261 | frame.end(CompositionCoverage::Gpu); | 
|  | 262 | } | 
|  | 263 | { | 
|  | 264 | const Frame frame(this, vsyncId++, frameBeginTime, 10ms, kRefreshRate, Frame::fencePending); | 
|  | 265 | EXPECT_TRUE(target().isFramePending()); | 
|  | 266 |  | 
|  | 267 | // The GPU frame missed if the past present fence is pending. | 
|  | 268 | EXPECT_TRUE(target().didMissFrame()); | 
|  | 269 | EXPECT_FALSE(target().didMissHwcFrame()); | 
|  | 270 | } | 
|  | 271 | { | 
|  | 272 | Frame frame(this, vsyncId++, frameBeginTime, 10ms, kRefreshRate); | 
|  | 273 | EXPECT_FALSE(target().isFramePending()); | 
|  | 274 |  | 
|  | 275 | const auto fence = frame.end(); | 
|  | 276 | const auto expectedPresentTime = target().expectedPresentTime(); | 
|  | 277 | fence->signalForTest(expectedPresentTime.ns() + kPeriod.ns() / 2 + 1); | 
|  | 278 | } | 
|  | 279 | { | 
|  | 280 | Frame frame(this, vsyncId++, frameBeginTime, 10ms, kRefreshRate); | 
|  | 281 | EXPECT_FALSE(target().isFramePending()); | 
|  | 282 |  | 
|  | 283 | const auto fence = frame.end(); | 
|  | 284 | const auto expectedPresentTime = target().expectedPresentTime(); | 
|  | 285 | fence->signalForTest(expectedPresentTime.ns() + kPeriod.ns() / 2); | 
|  | 286 |  | 
|  | 287 | // The frame missed if the past present fence was signaled but not within slop. | 
|  | 288 | EXPECT_TRUE(target().didMissFrame()); | 
|  | 289 | EXPECT_TRUE(target().didMissHwcFrame()); | 
|  | 290 | } | 
|  | 291 | { | 
|  | 292 | Frame frame(this, vsyncId++, frameBeginTime, 10ms, kRefreshRate); | 
|  | 293 | EXPECT_FALSE(target().isFramePending()); | 
|  | 294 |  | 
|  | 295 | // The frame did not miss if the past present fence was signaled within slop. | 
|  | 296 | EXPECT_FALSE(target().didMissFrame()); | 
|  | 297 | EXPECT_FALSE(target().didMissHwcFrame()); | 
|  | 298 | } | 
|  | 299 | } | 
|  | 300 |  | 
|  | 301 | } // namespace android::scheduler |