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