Change SF power hints to use early frame predictions
Currently we issue hints at the end of the frame reporting what happened
that frame. However, this is often too late anyway and lacks critical
information not known until after the frame completes, like gpu timing.
By waiting until the next frame start and using those timings along with
information about gpu duration, the new expected present time, and the
new wakeup time, we can instead use those timings to predict the
duration of the upcoming frame and signal in advance of a frame drop.
This patch also changes how and where frame timing is done and measures
gpu duration so we can predict if a cpu speedup can help gpu composition
happen earlier, preventing a drop that way.
Bug: b/195990840
Test: manual
Change-Id: I5f0ae796198631fced503dce8e6c495885fb256a
diff --git a/services/surfaceflinger/DisplayHardware/HWComposer.cpp b/services/surfaceflinger/DisplayHardware/HWComposer.cpp
index 79e4c75..18678e6 100644
--- a/services/surfaceflinger/DisplayHardware/HWComposer.cpp
+++ b/services/surfaceflinger/DisplayHardware/HWComposer.cpp
@@ -739,6 +739,13 @@
});
}
+bool HWComposer::getValidateSkipped(HalDisplayId displayId) const {
+ if (mDisplayData.count(displayId) == 0) {
+ return false;
+ }
+ return mDisplayData.at(displayId).validateWasSkipped;
+}
+
status_t HWComposer::setBootDisplayMode(PhysicalDisplayId displayId,
hal::HWConfigId displayModeId) {
RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
diff --git a/services/surfaceflinger/DisplayHardware/HWComposer.h b/services/surfaceflinger/DisplayHardware/HWComposer.h
index 7dc10ea..4ff4731 100644
--- a/services/surfaceflinger/DisplayHardware/HWComposer.h
+++ b/services/surfaceflinger/DisplayHardware/HWComposer.h
@@ -199,6 +199,9 @@
PhysicalDisplayId, float brightness, float brightnessNits,
const Hwc2::Composer::DisplayBrightnessOptions&) = 0;
+ // Get whether the display skipped validation on the latest present
+ virtual bool getValidateSkipped(HalDisplayId displayId) const = 0;
+
// Events handling ---------------------------------------------------------
// Returns stable display ID (and display name on connection of new or previously disconnected
@@ -397,6 +400,8 @@
status_t setActiveColorMode(PhysicalDisplayId, ui::ColorMode, ui::RenderIntent) override;
+ bool getValidateSkipped(HalDisplayId displayId) const override;
+
// Composer 2.4
ui::DisplayConnectionType getDisplayConnectionType(PhysicalDisplayId) const override;
bool isVsyncPeriodSwitchSupported(PhysicalDisplayId) const override;
diff --git a/services/surfaceflinger/DisplayHardware/PowerAdvisor.cpp b/services/surfaceflinger/DisplayHardware/PowerAdvisor.cpp
index 659efd8..20e38d0 100644
--- a/services/surfaceflinger/DisplayHardware/PowerAdvisor.cpp
+++ b/services/surfaceflinger/DisplayHardware/PowerAdvisor.cpp
@@ -174,7 +174,7 @@
return mPowerHintSessionRunning;
}
-void PowerAdvisor::setTargetWorkDuration(int64_t targetDurationNanos) {
+void PowerAdvisor::setTargetWorkDuration(int64_t targetDuration) {
if (!usePowerHintSession()) {
ALOGV("Power hint session target duration cannot be set, skipping");
return;
@@ -183,26 +183,44 @@
std::lock_guard lock(mPowerHalMutex);
HalWrapper* const halWrapper = getPowerHal();
if (halWrapper != nullptr) {
- halWrapper->setTargetWorkDuration(targetDurationNanos - kTargetSafetyMargin.count());
+ halWrapper->setTargetWorkDuration(targetDuration);
}
}
}
-void PowerAdvisor::sendActualWorkDuration(int64_t actualDurationNanos, nsecs_t timeStampNanos) {
+void PowerAdvisor::sendActualWorkDuration() {
if (!mBootFinished || !usePowerHintSession()) {
ALOGV("Actual work duration power hint cannot be sent, skipping");
return;
}
- {
+ const std::optional<nsecs_t> actualDuration = estimateWorkDuration(false);
+ if (actualDuration.has_value()) {
std::lock_guard lock(mPowerHalMutex);
HalWrapper* const halWrapper = getPowerHal();
if (halWrapper != nullptr) {
- halWrapper->sendActualWorkDuration(actualDurationNanos, timeStampNanos);
+ halWrapper->sendActualWorkDuration(*actualDuration + kTargetSafetyMargin.count(),
+ systemTime());
}
}
}
-// needs to be set after the flag is known but before PowerAdvisor enters onBootFinished
+void PowerAdvisor::sendPredictedWorkDuration() {
+ if (!mBootFinished || !usePowerHintSession()) {
+ ALOGV("Actual work duration power hint cannot be sent, skipping");
+ return;
+ }
+
+ const std::optional<nsecs_t> predictedDuration = estimateWorkDuration(true);
+
+ if (predictedDuration.has_value()) {
+ std::lock_guard lock(mPowerHalMutex);
+ HalWrapper* const halWrapper = getPowerHal();
+ if (halWrapper != nullptr) {
+ halWrapper->sendActualWorkDuration(*predictedDuration, systemTime());
+ }
+ }
+}
+
void PowerAdvisor::enablePowerHint(bool enabled) {
mPowerHintEnabled = enabled;
}
@@ -222,6 +240,282 @@
return mPowerHintSessionRunning;
}
+void PowerAdvisor::setGpuFenceTime(DisplayId displayId, std::unique_ptr<FenceTime>&& fenceTime) {
+ DisplayTimingData& displayData = mDisplayTimingData[displayId];
+ if (displayData.gpuEndFenceTime) {
+ nsecs_t signalTime = displayData.gpuEndFenceTime->getSignalTime();
+ if (signalTime != Fence::SIGNAL_TIME_INVALID && signalTime != Fence::SIGNAL_TIME_PENDING) {
+ for (auto&& [_, otherDisplayData] : mDisplayTimingData) {
+ // If the previous display started before us but ended after we should have
+ // started, then it likely delayed our start time and we must compensate for that.
+ // Displays finishing earlier should have already made their way through this call
+ // and swapped their timing into "lastValid" from "latest", so we check that here.
+ if (!otherDisplayData.lastValidGpuStartTime.has_value()) continue;
+ if ((*otherDisplayData.lastValidGpuStartTime < *displayData.gpuStartTime) &&
+ (*otherDisplayData.lastValidGpuEndTime > *displayData.gpuStartTime)) {
+ displayData.lastValidGpuStartTime = *otherDisplayData.lastValidGpuEndTime;
+ break;
+ }
+ }
+ displayData.lastValidGpuStartTime = displayData.gpuStartTime;
+ displayData.lastValidGpuEndTime = signalTime;
+ }
+ }
+ displayData.gpuEndFenceTime = std::move(fenceTime);
+ displayData.gpuStartTime = systemTime();
+}
+
+void PowerAdvisor::setValidateTiming(DisplayId displayId, nsecs_t validateStartTime,
+ nsecs_t validateEndTime) {
+ DisplayTimingData& displayData = mDisplayTimingData[displayId];
+ displayData.validateStartTime = validateStartTime;
+ displayData.validateEndTime = validateEndTime;
+}
+
+void PowerAdvisor::setPresentTiming(DisplayId displayId, nsecs_t presentStartTime,
+ nsecs_t presentEndTime) {
+ DisplayTimingData& displayData = mDisplayTimingData[displayId];
+ displayData.presentStartTime = presentStartTime;
+ displayData.presentEndTime = presentEndTime;
+}
+
+void PowerAdvisor::setSkippedValidate(DisplayId displayId, bool skipped) {
+ mDisplayTimingData[displayId].skippedValidate = skipped;
+}
+
+void PowerAdvisor::setRequiresClientComposition(DisplayId displayId,
+ bool requiresClientComposition) {
+ mDisplayTimingData[displayId].usedClientComposition = requiresClientComposition;
+}
+
+void PowerAdvisor::setExpectedPresentTime(nsecs_t expectedPresentTime) {
+ mExpectedPresentTimes.append(expectedPresentTime);
+}
+
+void PowerAdvisor::setFrameDelay(nsecs_t frameDelayDuration) {
+ mFrameDelayDuration = frameDelayDuration;
+}
+
+void PowerAdvisor::setPresentDelayedTime(
+ DisplayId displayId, std::chrono::steady_clock::time_point earliestFrameStartTime) {
+ mDisplayTimingData[displayId].presentDelayedTime =
+ (earliestFrameStartTime - std::chrono::steady_clock::now()).count() + systemTime();
+}
+
+void PowerAdvisor::setCommitStart(nsecs_t commitStartTime) {
+ mCommitStartTimes.append(commitStartTime);
+}
+
+void PowerAdvisor::setCompositeEnd(nsecs_t compositeEnd) {
+ mLastCompositeEndTime = compositeEnd;
+ // calculate the postcomp time here as well
+ std::vector<DisplayId>&& displays = getOrderedDisplayIds(&DisplayTimingData::presentEndTime);
+ DisplayTimingData& timingData = mDisplayTimingData[displays.back()];
+ mLastPostcompDuration = compositeEnd -
+ (timingData.skippedValidate ? *timingData.validateEndTime : *timingData.presentEndTime);
+}
+
+void PowerAdvisor::setDisplays(std::vector<DisplayId>& displayIds) {
+ mDisplayIds = displayIds;
+}
+
+void PowerAdvisor::setTotalFrameTargetWorkDuration(nsecs_t targetDuration) {
+ mTotalFrameTargetDuration = targetDuration;
+}
+
+std::vector<DisplayId> PowerAdvisor::getOrderedDisplayIds(
+ std::optional<nsecs_t> DisplayTimingData::*sortBy) {
+ std::vector<DisplayId> sortedDisplays;
+ std::copy_if(mDisplayIds.begin(), mDisplayIds.end(), std::back_inserter(sortedDisplays),
+ [&](DisplayId id) {
+ return mDisplayTimingData.count(id) &&
+ (mDisplayTimingData[id].*sortBy).has_value();
+ });
+ std::sort(sortedDisplays.begin(), sortedDisplays.end(), [&](DisplayId idA, DisplayId idB) {
+ return *(mDisplayTimingData[idA].*sortBy) < *(mDisplayTimingData[idB].*sortBy);
+ });
+ return sortedDisplays;
+}
+
+std::optional<nsecs_t> PowerAdvisor::estimateWorkDuration(bool earlyHint) {
+ if (earlyHint && (!mExpectedPresentTimes.isFull() || !mCommitStartTimes.isFull())) {
+ return std::nullopt;
+ }
+
+ // Tracks when we finish presenting to hwc
+ nsecs_t estimatedEndTime = mCommitStartTimes[0];
+
+ // How long we spent this frame not doing anything, waiting for fences or vsync
+ nsecs_t idleDuration = 0;
+
+ // Most recent previous gpu end time in the current frame, probably from a prior display, used
+ // as the start time for the next gpu operation if it ran over time since it probably blocked
+ std::optional<nsecs_t> previousValidGpuEndTime;
+
+ // The currently estimated gpu end time for the frame,
+ // used to accumulate gpu time as we iterate over the active displays
+ std::optional<nsecs_t> estimatedGpuEndTime;
+
+ // If we're predicting at the start of the frame, we use last frame as our reference point
+ // If we're predicting at the end of the frame, we use the current frame as a reference point
+ nsecs_t referenceFrameStartTime = (earlyHint ? mCommitStartTimes[-1] : mCommitStartTimes[0]);
+
+ // We need an idea of when the last present fence fired and how long it made us wait
+ // If we're predicting at the start of the frame, we want frame n-2's present fence time
+ // If we're predicting at the end of the frame we want frame n-1's present time
+ nsecs_t referenceFenceTime =
+ (earlyHint ? mExpectedPresentTimes[-2] : mExpectedPresentTimes[-1]);
+ // The timing info for the previously calculated display, if there was one
+ std::optional<DisplayTimeline> previousDisplayReferenceTiming;
+ std::vector<DisplayId>&& displayIds =
+ getOrderedDisplayIds(&DisplayTimingData::presentStartTime);
+ DisplayTimeline referenceTiming, estimatedTiming;
+
+ // Iterate over the displays in the same order they are presented
+ for (DisplayId displayId : displayIds) {
+ if (mDisplayTimingData.count(displayId) == 0) {
+ continue;
+ }
+
+ auto& displayData = mDisplayTimingData.at(displayId);
+ referenceTiming = displayData.calculateDisplayTimeline(referenceFenceTime);
+
+ // If this is the first display, add the pre-present time to the total
+ if (!previousDisplayReferenceTiming.has_value()) {
+ estimatedEndTime += referenceTiming.prePresentTime - referenceFrameStartTime;
+ } else { // Otherwise add last display's postprocessing time to the total
+ estimatedEndTime += referenceTiming.prePresentTime -
+ previousDisplayReferenceTiming->postPresentTime;
+ }
+
+ estimatedTiming = referenceTiming.estimateTimelineFromReference(mExpectedPresentTimes[-1],
+ estimatedEndTime);
+ // Update predicted present finish time with this display's present time
+ estimatedEndTime = estimatedTiming.postPresentTime;
+
+ // Track how long we spent waiting for the fence, can be excluded from the timing estimate
+ idleDuration += estimatedTiming.probablyWaitsForFence
+ ? mExpectedPresentTimes[-1] - estimatedTiming.preFenceWaitTime
+ : 0;
+
+ // Track how long we spent waiting to present, can be excluded from the timing estimate
+ idleDuration +=
+ !earlyHint ? referenceTiming.presentStartTime - referenceTiming.prePresentTime : 0;
+
+ // Estimate the reference frame's gpu timing
+ auto gpuTiming = displayData.estimateGpuTiming(previousValidGpuEndTime);
+ if (gpuTiming.has_value()) {
+ previousValidGpuEndTime = gpuTiming->startTime + gpuTiming->duration;
+
+ // Estimate the prediction frame's gpu end time from the reference frame
+ estimatedGpuEndTime =
+ std::max(estimatedTiming.prePresentTime, estimatedGpuEndTime.value_or(0)) +
+ gpuTiming->duration;
+ }
+ previousDisplayReferenceTiming = referenceTiming;
+ }
+ ATRACE_INT64("Idle duration", idleDuration);
+
+ // Don't count time spent idly waiting in the estimate as we could do more work in that time
+ estimatedEndTime -= idleDuration;
+
+ // We finish the frame when both present and the gpu are done, so wait for the later of the two
+ // Also add the frame delay duration since the target did not move while we were delayed
+ nsecs_t totalDuration = mFrameDelayDuration +
+ std::max(estimatedEndTime, estimatedGpuEndTime.value_or(0)) - mCommitStartTimes[0];
+
+ // We finish SurfaceFlinger when post-composition finishes, so add that in here
+ nsecs_t flingerDuration = estimatedEndTime + mLastPostcompDuration - mCommitStartTimes[0];
+ nsecs_t combinedDuration = combineTimingEstimates(totalDuration, flingerDuration);
+
+ return std::make_optional(combinedDuration);
+}
+
+nsecs_t PowerAdvisor::combineTimingEstimates(nsecs_t totalDuration, nsecs_t flingerDuration) {
+ nsecs_t targetDuration;
+ {
+ std::lock_guard lock(mPowerHalMutex);
+ targetDuration = *getPowerHal()->getTargetWorkDuration();
+ }
+ if (!mTotalFrameTargetDuration.has_value()) return flingerDuration;
+
+ // Normalize total to the flinger target (vsync period) since that's how often we actually send
+ // hints
+ nsecs_t normalizedTotalDuration = (targetDuration * totalDuration) / *mTotalFrameTargetDuration;
+ return std::max(flingerDuration, normalizedTotalDuration);
+}
+
+PowerAdvisor::DisplayTimeline PowerAdvisor::DisplayTimeline::estimateTimelineFromReference(
+ nsecs_t fenceTime, nsecs_t displayStartTime) {
+ DisplayTimeline estimated;
+ estimated.prePresentTime = displayStartTime;
+
+ // We don't predict waiting for vsync alignment yet
+ estimated.presentStartTime = estimated.prePresentTime;
+
+ // For now just re-use last frame's post-present duration and assume it will not change much
+ // How long we expect to run before we start waiting for the fence
+ estimated.preFenceWaitTime = estimated.presentStartTime + (preFenceWaitTime - presentStartTime);
+ estimated.probablyWaitsForFence = fenceTime > estimated.preFenceWaitTime;
+ estimated.postPresentTime = postFenceDuration +
+ (estimated.probablyWaitsForFence ? fenceTime : estimated.preFenceWaitTime);
+ return estimated;
+}
+
+PowerAdvisor::DisplayTimeline PowerAdvisor::DisplayTimingData::calculateDisplayTimeline(
+ nsecs_t fenceTime) {
+ DisplayTimeline timeline;
+ // How long between calling present from flinger and trying to wait on the fence in HWC
+ const nsecs_t preFenceWaitDelay =
+ (skippedValidate ? kPrefenceDelaySkippedValidate : kPrefenceDelayValidated).count();
+
+ // Did our reference frame wait for an earliest present time before calling the HWC
+ const bool waitedOnPresentTime = presentDelayedTime.has_value() &&
+ *presentDelayedTime > *presentStartTime && *presentDelayedTime < *presentEndTime;
+
+ // Use validate start here if we skipped it because we did validate + present together
+ timeline.prePresentTime = skippedValidate ? *validateStartTime : *presentStartTime;
+
+ // Use validate end here if we skipped it because we did validate + present together
+ timeline.postPresentTime = skippedValidate ? *validateEndTime : *presentEndTime;
+
+ // When we think we started waiting for the fence after calling into present
+ // This is after any time spent waiting for the earliest present time
+ timeline.presentStartTime =
+ (waitedOnPresentTime ? *presentDelayedTime : timeline.prePresentTime);
+ timeline.preFenceWaitTime = timeline.presentStartTime + preFenceWaitDelay;
+ timeline.probablyWaitsForFence =
+ fenceTime > timeline.preFenceWaitTime && fenceTime < timeline.postPresentTime;
+
+ // How long we ran after we finished waiting for the fence but before present happened
+ timeline.postFenceDuration = timeline.postPresentTime -
+ (timeline.probablyWaitsForFence ? fenceTime : timeline.preFenceWaitTime);
+ return timeline;
+}
+
+std::optional<PowerAdvisor::GpuTimeline> PowerAdvisor::DisplayTimingData::estimateGpuTiming(
+ std::optional<nsecs_t> previousEnd) {
+ if (!(usedClientComposition && lastValidGpuStartTime.has_value() && gpuEndFenceTime)) {
+ return std::nullopt;
+ }
+ const nsecs_t latestGpuStartTime = std::max(previousEnd.value_or(0), *gpuStartTime);
+ const nsecs_t latestGpuEndTime = gpuEndFenceTime->getSignalTime();
+ nsecs_t gpuDuration = 0;
+ if (latestGpuEndTime != Fence::SIGNAL_TIME_INVALID &&
+ latestGpuEndTime != Fence::SIGNAL_TIME_PENDING) {
+ // If we know how long the most recent gpu duration was, use that
+ gpuDuration = latestGpuEndTime - latestGpuStartTime;
+ } else if (lastValidGpuEndTime.has_value()) {
+ // If we don't have the fence data, use the most recent information we do have
+ gpuDuration = *lastValidGpuEndTime - *lastValidGpuStartTime;
+ if (latestGpuEndTime == Fence::SIGNAL_TIME_PENDING) {
+ // If pending but went over the previous duration, use current time as the end
+ gpuDuration = std::max(gpuDuration, systemTime() - latestGpuStartTime);
+ }
+ }
+ return GpuTimeline{.duration = gpuDuration, .startTime = latestGpuStartTime};
+}
+
class HidlPowerHalWrapper : public PowerAdvisor::HalWrapper {
public:
HidlPowerHalWrapper(sp<V1_3::IPower> powerHal) : mPowerHal(std::move(powerHal)) {}
@@ -303,6 +597,10 @@
}
mSupportsPowerHint = checkPowerHintSessionSupported();
+
+ mAllowedActualDeviation =
+ base::GetIntProperty<nsecs_t>("debug.sf.allowed_actual_deviation",
+ std::chrono::nanoseconds(250us).count());
}
AidlPowerHalWrapper::~AidlPowerHalWrapper() {
@@ -310,7 +608,7 @@
mPowerHintSession->close();
mPowerHintSession = nullptr;
}
-};
+}
std::unique_ptr<PowerAdvisor::HalWrapper> AidlPowerHalWrapper::connect() {
// This only waits if the service is actually declared
@@ -348,7 +646,7 @@
return ret.isOk();
}
-// only version 2+ of the aidl supports power hint sessions, hidl has no support
+// Only version 2+ of the aidl supports power hint sessions, hidl has no support
bool AidlPowerHalWrapper::supportsPowerHintSession() {
return mSupportsPowerHint;
}
@@ -402,30 +700,14 @@
return isPowerHintSessionRunning();
}
-bool AidlPowerHalWrapper::shouldSetTargetDuration(int64_t targetDurationNanos) {
- if (targetDurationNanos <= 0) {
- return false;
- }
- // report if the change in target from our last submission to now exceeds the threshold
- return abs(1.0 -
- static_cast<double>(mLastTargetDurationSent) /
- static_cast<double>(targetDurationNanos)) >= kAllowedTargetDeviationPercent;
-}
-
-void AidlPowerHalWrapper::setTargetWorkDuration(int64_t targetDurationNanos) {
+void AidlPowerHalWrapper::setTargetWorkDuration(int64_t targetDuration) {
ATRACE_CALL();
- mTargetDuration = targetDurationNanos;
- if (sTraceHintSessionData) ATRACE_INT64("Time target", targetDurationNanos);
- if (!sNormalizeTarget && isPowerHintSessionRunning() &&
- shouldSetTargetDuration(targetDurationNanos)) {
- if (mLastActualDurationSent.has_value()) {
- // update the error term here since we are actually sending an update to powerhal
- if (sTraceHintSessionData)
- ATRACE_INT64("Target error term", targetDurationNanos - *mLastActualDurationSent);
- }
- ALOGV("Sending target time: %" PRId64 "ns", targetDurationNanos);
- mLastTargetDurationSent = targetDurationNanos;
- auto ret = mPowerHintSession->updateTargetWorkDuration(targetDurationNanos);
+ mTargetDuration = targetDuration;
+ if (sTraceHintSessionData) ATRACE_INT64("Time target", targetDuration);
+ if (isPowerHintSessionRunning() && (targetDuration != mLastTargetDurationSent)) {
+ ALOGV("Sending target time: %" PRId64 "ns", targetDuration);
+ mLastTargetDurationSent = targetDuration;
+ auto ret = mPowerHintSession->updateTargetWorkDuration(targetDuration);
if (!ret.isOk()) {
ALOGW("Failed to set power hint target work duration with error: %s",
ret.exceptionMessage().c_str());
@@ -434,8 +716,8 @@
}
}
-bool AidlPowerHalWrapper::shouldReportActualDurationsNow() {
- // report if we have never reported before or are approaching a stale session
+bool AidlPowerHalWrapper::shouldReportActualDurations() {
+ // Report if we have never reported before or are approaching a stale session
if (!mLastActualDurationSent.has_value() ||
(systemTime() - mLastActualReportTimestamp) > kStaleTimeout.count()) {
return true;
@@ -444,65 +726,42 @@
if (!mActualDuration.has_value()) {
return false;
}
-
- // duration of most recent timing
- const double mostRecentActualDuration = static_cast<double>(*mActualDuration);
- // duration of the last timing actually reported to the powerhal
- const double lastReportedActualDuration = static_cast<double>(*mLastActualDurationSent);
-
- // report if the change in duration from then to now exceeds the threshold
- return abs(1.0 - mostRecentActualDuration / lastReportedActualDuration) >=
- kAllowedActualDeviationPercent;
+ // Report if the change in actual duration exceeds the threshold
+ return abs(*mActualDuration - *mLastActualDurationSent) > mAllowedActualDeviation;
}
-void AidlPowerHalWrapper::sendActualWorkDuration(int64_t actualDurationNanos,
- nsecs_t timeStampNanos) {
+void AidlPowerHalWrapper::sendActualWorkDuration(int64_t actualDuration, nsecs_t timestamp) {
ATRACE_CALL();
- if (actualDurationNanos < 0 || !isPowerHintSessionRunning()) {
+ if (actualDuration < 0 || !isPowerHintSessionRunning()) {
ALOGV("Failed to send actual work duration, skipping");
return;
}
- nsecs_t reportedDuration = actualDurationNanos;
+ const nsecs_t reportedDuration = actualDuration;
- // normalize the sent values to a pre-set target
- if (sNormalizeTarget) {
- reportedDuration += mLastTargetDurationSent - mTargetDuration;
- } else {
- // when target duration change is within deviation and not updated, adjust the actual
- // duration proportionally based on the difference, e.g. if new target is 5ms longer than
- // last reported but actual duration is the same as last target, we want to report a smaller
- // actual work duration now to indicate that we are overshooting
- if (mLastTargetDurationSent != kDefaultTarget.count() && mTargetDuration != 0) {
- reportedDuration =
- static_cast<int64_t>(static_cast<long double>(mLastTargetDurationSent) /
- mTargetDuration * actualDurationNanos);
- mActualDuration = reportedDuration;
- }
- }
mActualDuration = reportedDuration;
WorkDuration duration;
duration.durationNanos = reportedDuration;
- duration.timeStampNanos = timeStampNanos;
+ duration.timeStampNanos = timestamp;
mPowerHintQueue.push_back(duration);
if (sTraceHintSessionData) {
- ATRACE_INT64("Measured duration", actualDurationNanos);
- ATRACE_INT64("Target error term", mTargetDuration - actualDurationNanos);
+ ATRACE_INT64("Measured duration", actualDuration);
+ ATRACE_INT64("Target error term", actualDuration - mTargetDuration);
ATRACE_INT64("Reported duration", reportedDuration);
ATRACE_INT64("Reported target", mLastTargetDurationSent);
- ATRACE_INT64("Reported target error term", mLastTargetDurationSent - reportedDuration);
+ ATRACE_INT64("Reported target error term", reportedDuration - mLastTargetDurationSent);
}
ALOGV("Sending actual work duration of: %" PRId64 " on reported target: %" PRId64
" with error: %" PRId64,
- reportedDuration, mLastTargetDurationSent, mLastTargetDurationSent - reportedDuration);
+ reportedDuration, mLastTargetDurationSent, reportedDuration - mLastTargetDurationSent);
// This rate limiter queues similar duration reports to the powerhal into
// batches to avoid excessive binder calls. The criteria to send a given batch
// are outlined in shouldReportActualDurationsNow()
- if (shouldReportActualDurationsNow()) {
+ if (shouldReportActualDurations()) {
ALOGV("Sending hint update batch");
mLastActualReportTimestamp = systemTime();
auto ret = mPowerHintSession->reportActualWorkDuration(mPowerHintQueue);
@@ -512,8 +771,8 @@
mShouldReconnectHal = true;
}
mPowerHintQueue.clear();
- // we save the non-normalized value here to detect % changes
- mLastActualDurationSent = reportedDuration;
+ // We save the actual duration here for rate limiting
+ mLastActualDurationSent = actualDuration;
}
}
@@ -529,12 +788,13 @@
return mTargetDuration;
}
+void AidlPowerHalWrapper::setAllowedActualDeviation(nsecs_t allowedDeviation) {
+ mAllowedActualDeviation = allowedDeviation;
+}
+
const bool AidlPowerHalWrapper::sTraceHintSessionData =
base::GetBoolProperty(std::string("debug.sf.trace_hint_sessions"), false);
-const bool AidlPowerHalWrapper::sNormalizeTarget =
- base::GetBoolProperty(std::string("debug.sf.normalize_hint_session_durations"), false);
-
PowerAdvisor::HalWrapper* PowerAdvisor::getPowerHal() {
static std::unique_ptr<HalWrapper> sHalWrapper = nullptr;
static bool sHasHal = true;
@@ -543,7 +803,7 @@
return nullptr;
}
- // grab old hint session values before we destroy any existing wrapper
+ // Grab old hint session values before we destroy any existing wrapper
std::vector<int32_t> oldPowerHintSessionThreadIds;
std::optional<int64_t> oldTargetWorkDuration;
@@ -560,7 +820,7 @@
if (sHalWrapper != nullptr) {
auto wrapper = sHalWrapper.get();
- // if the wrapper is fine, return it, but if it indicates a reconnect, remake it
+ // If the wrapper is fine, return it, but if it indicates a reconnect, remake it
if (!wrapper->shouldReconnectHAL()) {
return wrapper;
}
@@ -568,7 +828,7 @@
sHalWrapper = nullptr;
}
- // at this point, we know for sure there is no running session
+ // At this point, we know for sure there is no running session
mPowerHintSessionRunning = false;
// First attempt to connect to the AIDL Power HAL
@@ -579,13 +839,12 @@
sHalWrapper = HidlPowerHalWrapper::connect();
} else {
ALOGD("Successfully connecting AIDL Power HAL");
- // if AIDL, pass on any existing hint session values
- // thread ids always safe to set
+ // If AIDL, pass on any existing hint session values
sHalWrapper->setPowerHintSessionThreadIds(oldPowerHintSessionThreadIds);
- // only set duration and start if duration is defined
+ // Only set duration and start if duration is defined
if (oldTargetWorkDuration.has_value()) {
sHalWrapper->setTargetWorkDuration(*oldTargetWorkDuration);
- // only start if possible to run and both threadids and duration are defined
+ // Only start if possible to run and both threadids and duration are defined
if (usePowerHintSession() && !oldPowerHintSessionThreadIds.empty()) {
mPowerHintSessionRunning = sHalWrapper->startPowerHintSession();
}
diff --git a/services/surfaceflinger/DisplayHardware/PowerAdvisor.h b/services/surfaceflinger/DisplayHardware/PowerAdvisor.h
index 61bb32b..19a3b88 100644
--- a/services/surfaceflinger/DisplayHardware/PowerAdvisor.h
+++ b/services/surfaceflinger/DisplayHardware/PowerAdvisor.h
@@ -18,11 +18,15 @@
#include <atomic>
#include <chrono>
+#include <unordered_map>
#include <unordered_set>
+#include <ui/DisplayId.h>
+#include <ui/FenceTime.h>
#include <utils/Mutex.h>
#include <android/hardware/power/IPower.h>
+#include <compositionengine/impl/OutputCompositionState.h>
#include <ui/DisplayIdentification.h>
#include "../Scheduler/OneShotTimer.h"
@@ -44,13 +48,47 @@
virtual void setExpensiveRenderingExpected(DisplayId displayId, bool expected) = 0;
virtual bool isUsingExpensiveRendering() = 0;
virtual void notifyDisplayUpdateImminent() = 0;
+ // Checks both if it supports and if it's enabled
virtual bool usePowerHintSession() = 0;
virtual bool supportsPowerHintSession() = 0;
virtual bool isPowerHintSessionRunning() = 0;
- virtual void setTargetWorkDuration(int64_t targetDurationNanos) = 0;
- virtual void sendActualWorkDuration(int64_t actualDurationNanos, nsecs_t timestamp) = 0;
+ // Sends a power hint that updates to the target work duration for the frame
+ virtual void setTargetWorkDuration(nsecs_t targetDuration) = 0;
+ // Sends a power hint for the actual known work duration at the end of the frame
+ virtual void sendActualWorkDuration() = 0;
+ // Sends a power hint for the upcoming frame predicted from previous frame timing
+ virtual void sendPredictedWorkDuration() = 0;
+ // Sets whether the power hint session is enabled
virtual void enablePowerHint(bool enabled) = 0;
+ // Initializes the power hint session
virtual bool startPowerHintSession(const std::vector<int32_t>& threadIds) = 0;
+ // Provides PowerAdvisor with a copy of the gpu fence so it can determine the gpu end time
+ virtual void setGpuFenceTime(DisplayId displayId, std::unique_ptr<FenceTime>&& fenceTime) = 0;
+ // Reports the start and end times of a present call this frame for a given display
+ virtual void setValidateTiming(DisplayId displayId, nsecs_t validateStartTime,
+ nsecs_t validateEndTime) = 0;
+ // Reports the start and end times of a present call this frame for a given display
+ virtual void setPresentTiming(DisplayId displayId, nsecs_t presentStartTime,
+ nsecs_t presentEndTime) = 0;
+ virtual void setExpectedPresentTime(nsecs_t expectedPresentTime) = 0;
+ // Reports whether a display used client composition this frame
+ virtual void setRequiresClientComposition(DisplayId displayId,
+ bool requiresClientComposition) = 0;
+ // Reports whether a given display skipped validation this frame
+ virtual void setSkippedValidate(DisplayId displayId, bool skipped) = 0;
+ // Reports how much a given display delayed its present call this frame
+ virtual void setPresentDelayedTime(
+ DisplayId displayId, std::chrono::steady_clock::time_point earliestFrameStartTime) = 0;
+ // Reports the start delay for SurfaceFlinger this frame
+ virtual void setFrameDelay(nsecs_t frameDelayDuration) = 0;
+ // Reports the SurfaceFlinger commit start time this frame
+ virtual void setCommitStart(nsecs_t commitStartTime) = 0;
+ // Reports the SurfaceFlinger composite end time this frame
+ virtual void setCompositeEnd(nsecs_t compositeEndTime) = 0;
+ // Reports the list of the currently active displays
+ virtual void setDisplays(std::vector<DisplayId>& displayIds) = 0;
+ // Sets the target duration for the entire pipeline including the gpu
+ virtual void setTotalFrameTargetWorkDuration(nsecs_t targetDuration) = 0;
};
namespace impl {
@@ -70,12 +108,11 @@
virtual void restartPowerHintSession() = 0;
virtual void setPowerHintSessionThreadIds(const std::vector<int32_t>& threadIds) = 0;
virtual bool startPowerHintSession() = 0;
- virtual void setTargetWorkDuration(int64_t targetDurationNanos) = 0;
- virtual void sendActualWorkDuration(int64_t actualDurationNanos,
- nsecs_t timeStampNanos) = 0;
+ virtual void setTargetWorkDuration(nsecs_t targetDuration) = 0;
+ virtual void sendActualWorkDuration(nsecs_t actualDuration, nsecs_t timestamp) = 0;
virtual bool shouldReconnectHAL() = 0;
virtual std::vector<int32_t> getPowerHintSessionThreadIds() = 0;
- virtual std::optional<int64_t> getTargetWorkDuration() = 0;
+ virtual std::optional<nsecs_t> getTargetWorkDuration() = 0;
};
PowerAdvisor(SurfaceFlinger& flinger);
@@ -89,10 +126,28 @@
bool usePowerHintSession() override;
bool supportsPowerHintSession() override;
bool isPowerHintSessionRunning() override;
- void setTargetWorkDuration(int64_t targetDurationNanos) override;
- void sendActualWorkDuration(int64_t actualDurationNanos, nsecs_t timestamp) override;
+ void setTargetWorkDuration(nsecs_t targetDuration) override;
+ void sendActualWorkDuration() override;
+ void sendPredictedWorkDuration() override;
void enablePowerHint(bool enabled) override;
bool startPowerHintSession(const std::vector<int32_t>& threadIds) override;
+ void setGpuFenceTime(DisplayId displayId, std::unique_ptr<FenceTime>&& fenceTime);
+ void setValidateTiming(DisplayId displayId, nsecs_t valiateStartTime,
+ nsecs_t validateEndTime) override;
+ void setPresentTiming(DisplayId displayId, nsecs_t presentStartTime,
+ nsecs_t presentEndTime) override;
+ void setSkippedValidate(DisplayId displayId, bool skipped) override;
+ void setRequiresClientComposition(DisplayId displayId, bool requiresClientComposition) override;
+ void setExpectedPresentTime(nsecs_t expectedPresentTime) override;
+ void setPresentDelayedTime(
+ DisplayId displayId,
+ std::chrono::steady_clock::time_point earliestFrameStartTime) override;
+
+ void setFrameDelay(nsecs_t frameDelayDuration) override;
+ void setCommitStart(nsecs_t commitStartTime) override;
+ void setCompositeEnd(nsecs_t compositeEndTime) override;
+ void setDisplays(std::vector<DisplayId>& displayIds) override;
+ void setTotalFrameTargetWorkDuration(nsecs_t targetDuration) override;
private:
HalWrapper* getPowerHal() REQUIRES(mPowerHalMutex);
@@ -100,15 +155,6 @@
std::mutex mPowerHalMutex;
std::atomic_bool mBootFinished = false;
- std::optional<bool> mPowerHintEnabled;
- std::optional<bool> mSupportsPowerHint;
- bool mPowerHintSessionRunning = false;
-
- // An adjustable safety margin which moves the "target" earlier to allow flinger to
- // go a bit over without dropping a frame, especially since we can't measure
- // the exact time HWC finishes composition so "actual" durations are measured
- // from the end of present() instead, which is a bit later.
- static constexpr const std::chrono::nanoseconds kTargetSafetyMargin = 2ms;
std::unordered_set<DisplayId> mExpensiveDisplays;
bool mNotifiedExpensiveRendering = false;
@@ -117,6 +163,108 @@
const bool mUseScreenUpdateTimer;
std::atomic_bool mSendUpdateImminent = true;
scheduler::OneShotTimer mScreenUpdateTimer;
+
+ // Higher-level timing data used for estimation
+ struct DisplayTimeline {
+ nsecs_t prePresentTime = -1;
+ nsecs_t postPresentTime = -1;
+ // Usually equals prePresentTime but can be delayed if we wait for the next valid vsync
+ nsecs_t presentStartTime = -1;
+ // When we think we started waiting for the fence after calling into present and
+ // after potentially waiting for the earliest present time
+ nsecs_t preFenceWaitTime = -1;
+ // How long we ran after we finished waiting for the fence but before present happened
+ nsecs_t postFenceDuration = 0;
+ // Are we likely to have waited for the present fence during composition
+ bool probablyWaitsForFence = false;
+ // Estimate one frame's timeline from that of a previous frame
+ DisplayTimeline estimateTimelineFromReference(nsecs_t fenceTime, nsecs_t displayStartTime);
+ };
+
+ struct GpuTimeline {
+ nsecs_t duration = 0;
+ nsecs_t startTime = -1;
+ };
+
+ // Power hint session data recorded from the pipeline
+ struct DisplayTimingData {
+ std::unique_ptr<FenceTime> gpuEndFenceTime;
+ std::optional<nsecs_t> gpuStartTime;
+ std::optional<nsecs_t> lastValidGpuEndTime;
+ std::optional<nsecs_t> lastValidGpuStartTime;
+ std::optional<nsecs_t> presentStartTime;
+ std::optional<nsecs_t> presentEndTime;
+ std::optional<nsecs_t> validateStartTime;
+ std::optional<nsecs_t> validateEndTime;
+ std::optional<nsecs_t> presentDelayedTime;
+ bool usedClientComposition = false;
+ bool skippedValidate = false;
+ // Calculate high-level timing milestones from more granular display timing data
+ DisplayTimeline calculateDisplayTimeline(nsecs_t fenceTime);
+ // Estimate the gpu duration for a given display from previous gpu timing data
+ std::optional<GpuTimeline> estimateGpuTiming(std::optional<nsecs_t> previousEnd);
+ };
+
+ template <class T, size_t N>
+ class RingBuffer {
+ std::array<T, N> elements = {};
+ size_t mIndex = 0;
+ size_t numElements = 0;
+
+ public:
+ void append(T item) {
+ mIndex = (mIndex + 1) % N;
+ numElements = std::min(N, numElements + 1);
+ elements[mIndex] = item;
+ }
+ bool isFull() const { return numElements == N; }
+ // Allows access like [0] == current, [-1] = previous, etc..
+ T& operator[](int offset) {
+ size_t positiveOffset =
+ static_cast<size_t>((offset % static_cast<int>(N)) + static_cast<int>(N));
+ return elements[(mIndex + positiveOffset) % N];
+ }
+ };
+
+ // Filter and sort the display ids by a given property
+ std::vector<DisplayId> getOrderedDisplayIds(std::optional<nsecs_t> DisplayTimingData::*sortBy);
+ // Estimates a frame's total work duration including gpu time.
+ // Runs either at the beginning or end of a frame, using the most recent data available
+ std::optional<nsecs_t> estimateWorkDuration(bool earlyHint);
+ // There are two different targets and actual work durations we care about,
+ // this normalizes them together and takes the max of the two
+ nsecs_t combineTimingEstimates(nsecs_t totalDuration, nsecs_t flingerDuration);
+
+ std::unordered_map<DisplayId, DisplayTimingData> mDisplayTimingData;
+
+ // Current frame's delay
+ nsecs_t mFrameDelayDuration = 0;
+ // Last frame's composite end time
+ nsecs_t mLastCompositeEndTime = -1;
+ // Last frame's post-composition duration
+ nsecs_t mLastPostcompDuration = 0;
+ // Buffer of recent commit start times
+ RingBuffer<nsecs_t, 2> mCommitStartTimes;
+ // Buffer of recent expected present times
+ RingBuffer<nsecs_t, 3> mExpectedPresentTimes;
+ // Target for the entire pipeline including gpu
+ std::optional<nsecs_t> mTotalFrameTargetDuration;
+ // Updated list of display IDs
+ std::vector<DisplayId> mDisplayIds;
+
+ std::optional<bool> mPowerHintEnabled;
+ std::optional<bool> mSupportsPowerHint;
+ bool mPowerHintSessionRunning = false;
+
+ // An adjustable safety margin which moves the "target" earlier to allow flinger to
+ // go a bit over without dropping a frame, especially since we can't measure
+ // the exact time HWC finishes composition so "actual" durations are measured
+ // from the end of present() instead, which is a bit later.
+ static constexpr const std::chrono::nanoseconds kTargetSafetyMargin = 1ms;
+
+ // How long we expect hwc to run after the present call until it waits for the fence
+ static constexpr const std::chrono::nanoseconds kPrefenceDelayValidated = 150us;
+ static constexpr const std::chrono::nanoseconds kPrefenceDelaySkippedValidate = 250us;
};
class AidlPowerHalWrapper : public PowerAdvisor::HalWrapper {
@@ -133,50 +281,50 @@
void restartPowerHintSession() override;
void setPowerHintSessionThreadIds(const std::vector<int32_t>& threadIds) override;
bool startPowerHintSession() override;
- void setTargetWorkDuration(int64_t targetDurationNanos) override;
- void sendActualWorkDuration(int64_t actualDurationNanos, nsecs_t timeStampNanos) override;
+ void setTargetWorkDuration(nsecs_t targetDuration) override;
+ void sendActualWorkDuration(nsecs_t actualDuration, nsecs_t timestamp) override;
bool shouldReconnectHAL() override;
std::vector<int32_t> getPowerHintSessionThreadIds() override;
- std::optional<int64_t> getTargetWorkDuration() override;
+ std::optional<nsecs_t> getTargetWorkDuration() override;
private:
+ friend class AidlPowerHalWrapperTest;
+
bool checkPowerHintSessionSupported();
void closePowerHintSession();
- bool shouldReportActualDurationsNow();
- bool shouldSetTargetDuration(int64_t targetDurationNanos);
+ bool shouldReportActualDurations();
+
+ // Used for testing
+ void setAllowedActualDeviation(nsecs_t);
const sp<hardware::power::IPower> mPowerHal = nullptr;
bool mHasExpensiveRendering = false;
bool mHasDisplayUpdateImminent = false;
// Used to indicate an error state and need for reconstruction
bool mShouldReconnectHal = false;
- // This is not thread safe, but is currently protected by mPowerHalMutex so it needs no lock
+
+ // Power hint session data
+
+ // Concurrent access for this is protected by mPowerHalMutex
sp<hardware::power::IPowerHintSession> mPowerHintSession = nullptr;
// Queue of actual durations saved to report
std::vector<hardware::power::WorkDuration> mPowerHintQueue;
- // The latest un-normalized values we have received for target and actual
- int64_t mTargetDuration = kDefaultTarget.count();
- std::optional<int64_t> mActualDuration;
+ // The latest values we have received for target and actual
+ nsecs_t mTargetDuration = kDefaultTarget.count();
+ std::optional<nsecs_t> mActualDuration;
// The list of thread ids, stored so we can restart the session from this class if needed
std::vector<int32_t> mPowerHintThreadIds;
- bool mSupportsPowerHint;
+ bool mSupportsPowerHint = false;
// Keep track of the last messages sent for rate limiter change detection
- std::optional<int64_t> mLastActualDurationSent;
- // timestamp of the last report we sent, used to avoid stale sessions
- int64_t mLastActualReportTimestamp = 0;
- int64_t mLastTargetDurationSent = kDefaultTarget.count();
- // Whether to normalize all the actual values as error terms relative to a constant target
- // This saves a binder call by not setting the target, and should not affect the pid values
- static const bool sNormalizeTarget;
+ std::optional<nsecs_t> mLastActualDurationSent;
+ // Timestamp of the last report we sent, used to avoid stale sessions
+ nsecs_t mLastActualReportTimestamp = 0;
+ nsecs_t mLastTargetDurationSent = kDefaultTarget.count();
+ // Max amount the error term can vary without causing an actual value report
+ nsecs_t mAllowedActualDeviation = -1;
// Whether we should emit ATRACE_INT data for hint sessions
static const bool sTraceHintSessionData;
-
- // Max percent the actual duration can vary without causing a report (eg: 0.1 = 10%)
- static constexpr double kAllowedActualDeviationPercent = 0.1;
- // Max percent the target duration can vary without causing a report (eg: 0.1 = 10%)
- static constexpr double kAllowedTargetDeviationPercent = 0.1;
- // Target used for init and normalization, the actual value does not really matter
- static constexpr const std::chrono::nanoseconds kDefaultTarget = 50ms;
+ static constexpr const std::chrono::nanoseconds kDefaultTarget = 16ms;
// Amount of time after the last message was sent before the session goes stale
// actually 100ms but we use 80 here to ideally avoid going stale
static constexpr const std::chrono::nanoseconds kStaleTimeout = 80ms;