SF: Factor out expected present time computation
Compute expected present time once and pass it to traversed layers
instead of calling back into SF for each layer. This will simplify
synchronization for per-display refresh.
Also, recompute on Binder transaction, since the cached value computed
on invalidate may be stale.
Bug: 130554049
Bug: 123715322
Test: go/wm-smoke
Change-Id: I7153a728360e789dc4f97d2c39c4bdaa14183624
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 99440a6..cd80f2b 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -1693,15 +1693,15 @@
return fence != Fence::NO_FENCE && (fence->getStatus() == Fence::Status::Unsignaled);
}
-void SurfaceFlinger::populateExpectedPresentTime() NO_THREAD_SAFETY_ANALYSIS {
+void SurfaceFlinger::populateExpectedPresentTime() {
DisplayStatInfo stats;
mScheduler->getDisplayStatInfo(&stats);
const nsecs_t presentTime = mScheduler->getDispSyncExpectedPresentTime();
// Inflate the expected present time if we're targetting the next vsync.
- mExpectedPresentTime =
- mVsyncModulator.getOffsets().sf < mPhaseOffsets->getOffsetThresholdForNextVsync()
- ? presentTime
- : presentTime + stats.vsyncPeriod;
+ mExpectedPresentTime.store(mVsyncModulator.getOffsets().sf <
+ mPhaseOffsets->getOffsetThresholdForNextVsync()
+ ? presentTime
+ : presentTime + stats.vsyncPeriod);
}
void SurfaceFlinger::onMessageReceived(int32_t what) NO_THREAD_SAFETY_ANALYSIS {
@@ -2856,9 +2856,11 @@
void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
{
+ const nsecs_t expectedPresentTime = mExpectedPresentTime.load();
+
// Notify all layers of available frames
- mCurrentState.traverseInZOrder([](Layer* layer) {
- layer->notifyAvailableFrames();
+ mCurrentState.traverseInZOrder([expectedPresentTime](Layer* layer) {
+ layer->notifyAvailableFrames(expectedPresentTime);
});
/*
@@ -3057,7 +3059,7 @@
void SurfaceFlinger::latchAndReleaseBuffer(const sp<Layer>& layer) {
if (layer->hasReadyFrame()) {
bool ignored = false;
- layer->latchBuffer(ignored, systemTime());
+ layer->latchBuffer(ignored, systemTime(), 0 /* expectedPresentTime */);
}
layer->releasePendingBuffer(systemTime());
}
@@ -3305,6 +3307,8 @@
bool frameQueued = false;
bool newDataLatched = false;
+ const nsecs_t expectedPresentTime = mExpectedPresentTime.load();
+
// Store the set of layers that need updates. This set must not change as
// buffers are being latched, as this could result in a deadlock.
// Example: Two producers share the same command stream and:
@@ -3317,7 +3321,6 @@
mDrawingState.traverseInZOrder([&](Layer* layer) {
if (layer->hasReadyFrame()) {
frameQueued = true;
- const nsecs_t expectedPresentTime = getExpectedPresentTime();
if (layer->shouldPresentNow(expectedPresentTime)) {
mLayersWithQueuedFrames.push_back(layer);
} else {
@@ -3335,7 +3338,7 @@
Mutex::Autolock lock(mStateLock);
for (auto& layer : mLayersWithQueuedFrames) {
- if (layer->latchBuffer(visibleRegions, latchTime)) {
+ if (layer->latchBuffer(visibleRegions, latchTime, expectedPresentTime)) {
mLayersPendingRefresh.push_back(layer);
}
layer->useSurfaceDamage();
@@ -3656,6 +3659,7 @@
while (!transactionQueue.empty()) {
const auto& transaction = transactionQueue.front();
if (!transactionIsReadyToBeApplied(transaction.desiredPresentTime,
+ true /* useCachedExpectedPresentTime */,
transaction.states)) {
setTransactionFlags(eTransactionFlushNeeded);
break;
@@ -3708,8 +3712,12 @@
}
bool SurfaceFlinger::transactionIsReadyToBeApplied(int64_t desiredPresentTime,
+ bool useCachedExpectedPresentTime,
const Vector<ComposerState>& states) {
- nsecs_t expectedPresentTime = getExpectedPresentTime();
+ if (!useCachedExpectedPresentTime)
+ populateExpectedPresentTime();
+
+ const nsecs_t expectedPresentTime = mExpectedPresentTime.load();
// Do not present if the desiredPresentTime has not passed unless it is more than one second
// in the future. We ignore timestamps more than 1 second in the future for stability reasons.
if (desiredPresentTime >= 0 && desiredPresentTime >= expectedPresentTime &&
@@ -3764,8 +3772,10 @@
itr = mTransactionQueues.find(applyToken);
}
}
- if (itr != mTransactionQueues.end() ||
- !transactionIsReadyToBeApplied(desiredPresentTime, states)) {
+
+ // Expected present time is computed and cached on invalidate, so it may be stale.
+ if (itr != mTransactionQueues.end() || !transactionIsReadyToBeApplied(
+ desiredPresentTime, false /* useCachedExpectedPresentTime */, states)) {
mTransactionQueues[applyToken].emplace(states, displays, flags, desiredPresentTime,
uncacheBuffer, listenerCallbacks, postTime,
privileged);