blob: 7138afdf8b4d65e606a835fd5b54c6f66d4121ee [file] [log] [blame]
Dominik Laskowskib418dd72023-06-13 17:31:04 -04001/*
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 <gui/TraceUtils.h>
18
19#include <scheduler/FrameTargeter.h>
20#include <scheduler/IVsyncSource.h>
21
22namespace android::scheduler {
23
24TimePoint FrameTarget::pastVsyncTime(Period vsyncPeriod) const {
25 // TODO(b/267315508): Generalize to N VSYNCs.
26 const int shift = static_cast<int>(targetsVsyncsAhead<2>(vsyncPeriod));
27 return mExpectedPresentTime - Period::fromNs(vsyncPeriod.ns() << shift);
28}
29
30const FenceTimePtr& FrameTarget::presentFenceForPastVsync(Period vsyncPeriod) const {
31 // TODO(b/267315508): Generalize to N VSYNCs.
32 const size_t i = static_cast<size_t>(targetsVsyncsAhead<2>(vsyncPeriod));
33 return mPresentFences[i].fenceTime;
34}
35
36bool FrameTarget::wouldPresentEarly(Period vsyncPeriod) const {
37 // TODO(b/241285475): Since this is called during `composite`, the calls to `targetsVsyncsAhead`
38 // should use `TimePoint::now()` in case of delays since `mFrameBeginTime`.
39
40 // TODO(b/267315508): Generalize to N VSYNCs.
41 if (targetsVsyncsAhead<3>(vsyncPeriod)) {
42 return true;
43 }
44
45 const auto fence = presentFenceForPastVsync(vsyncPeriod);
46 return fence->isValid() && fence->getSignalTime() != Fence::SIGNAL_TIME_PENDING;
47}
48
49void FrameTargeter::beginFrame(const BeginFrameArgs& args, const IVsyncSource& vsyncSource) {
50 return beginFrame(args, vsyncSource, &FrameTargeter::isFencePending);
51}
52
53void FrameTargeter::beginFrame(const BeginFrameArgs& args, const IVsyncSource& vsyncSource,
54 IsFencePendingFuncPtr isFencePendingFuncPtr) {
55 mVsyncId = args.vsyncId;
56 mFrameBeginTime = args.frameBeginTime;
57
58 // The `expectedVsyncTime`, which was predicted when this frame was scheduled, is normally in
59 // the future relative to `frameBeginTime`, but may not be for delayed frames. Adjust
60 // `mExpectedPresentTime` accordingly, but not `mScheduledPresentTime`.
61 const TimePoint lastScheduledPresentTime = mScheduledPresentTime;
62 mScheduledPresentTime = args.expectedVsyncTime;
63
64 const Period vsyncPeriod = vsyncSource.period();
65
66 // Calculate the expected present time once and use the cached value throughout this frame to
67 // make sure all layers are seeing this same value.
68 if (args.expectedVsyncTime >= args.frameBeginTime) {
69 mExpectedPresentTime = args.expectedVsyncTime;
70 } else {
71 mExpectedPresentTime = vsyncSource.vsyncDeadlineAfter(args.frameBeginTime);
72 if (args.sfWorkDuration > vsyncPeriod) {
73 // Inflate the expected present time if we're targeting the next VSYNC.
74 mExpectedPresentTime += vsyncPeriod;
75 }
76 }
77
78 ATRACE_FORMAT("%s %" PRId64 " vsyncIn %.2fms%s", __func__, ftl::to_underlying(args.vsyncId),
79 ticks<std::milli, float>(mExpectedPresentTime - TimePoint::now()),
80 mExpectedPresentTime == args.expectedVsyncTime ? "" : " (adjusted)");
81
82 const FenceTimePtr& pastPresentFence = presentFenceForPastVsync(vsyncPeriod);
83
84 // In cases where the present fence is about to fire, give it a small grace period instead of
85 // giving up on the frame.
86 //
87 // TODO(b/280667110): The grace period should depend on `sfWorkDuration` and `vsyncPeriod` being
88 // approximately equal, not whether backpressure propagation is enabled.
89 const int graceTimeForPresentFenceMs = static_cast<int>(
90 mBackpressureGpuComposition || !mCompositionCoverage.test(CompositionCoverage::Gpu));
91
92 // Pending frames may trigger backpressure propagation.
93 const auto& isFencePending = *isFencePendingFuncPtr;
94 mFramePending = pastPresentFence != FenceTime::NO_FENCE &&
95 isFencePending(pastPresentFence, graceTimeForPresentFenceMs);
96
97 // A frame is missed if the prior frame is still pending. If no longer pending, then we still
98 // count the frame as missed if the predicted present time was further in the past than when the
99 // fence actually fired. Add some slop to correct for drift. This should generally be smaller
100 // than a typical frame duration, but should not be so small that it reports reasonable drift as
101 // a missed frame.
102 mFrameMissed = mFramePending || [&] {
103 const nsecs_t pastPresentTime = pastPresentFence->getSignalTime();
104 if (pastPresentTime < 0) return false;
105 const nsecs_t frameMissedSlop = vsyncPeriod.ns() / 2;
106 return lastScheduledPresentTime.ns() < pastPresentTime - frameMissedSlop;
107 }();
108
109 mHwcFrameMissed = mFrameMissed && mCompositionCoverage.test(CompositionCoverage::Hwc);
110 mGpuFrameMissed = mFrameMissed && mCompositionCoverage.test(CompositionCoverage::Gpu);
111
112 if (mFrameMissed) mFrameMissedCount++;
113 if (mHwcFrameMissed) mHwcFrameMissedCount++;
114 if (mGpuFrameMissed) mGpuFrameMissedCount++;
115}
116
117void FrameTargeter::endFrame(const CompositeResult& result) {
118 mCompositionCoverage = result.compositionCoverage;
119}
120
121FenceTimePtr FrameTargeter::setPresentFence(sp<Fence> presentFence) {
122 auto presentFenceTime = std::make_shared<FenceTime>(presentFence);
123 return setPresentFence(std::move(presentFence), std::move(presentFenceTime));
124}
125
126FenceTimePtr FrameTargeter::setPresentFence(sp<Fence> presentFence, FenceTimePtr presentFenceTime) {
127 mPresentFences[1] = mPresentFences[0];
128 mPresentFences[0] = {std::move(presentFence), presentFenceTime};
129 return presentFenceTime;
130}
131
132void FrameTargeter::dump(utils::Dumper& dumper) const {
133 using namespace std::string_view_literals;
134
135 utils::Dumper::Section section(dumper, "Frame Targeting"sv);
136
137 // There are scripts and tests that expect this (rather than "name=value") format.
138 dumper.dump({}, "Total missed frame count: " + std::to_string(mFrameMissedCount));
139 dumper.dump({}, "HWC missed frame count: " + std::to_string(mHwcFrameMissedCount));
140 dumper.dump({}, "GPU missed frame count: " + std::to_string(mGpuFrameMissedCount));
141}
142
143bool FrameTargeter::isFencePending(const FenceTimePtr& fence, int graceTimeMs) {
144 ATRACE_CALL();
145 const status_t status = fence->wait(graceTimeMs);
146
147 // This is the same as Fence::Status::Unsignaled, but it saves a call to getStatus,
148 // which calls wait(0) again internally.
149 return status == -ETIME;
150}
151
152} // namespace android::scheduler