Latch and relase buffers for offscreen layers on main thread.
The current code was calling fakeVsync, which was latching and releasing
buffers on a binder thread when a new frame arrived. This was causing a
race with the latchBuffer on the main thread. This change calls
latchAndReleaseBuffer on the main thread for offscreen layers to ensure
we don't block dequeueBuffer but also don't race the other latchBuffer
call.
Fixes: 139118508
Test: Race was only reproducible in monkey tests
Change-Id: Ie7d87aa775c215de3fb2322b73849402596cc3fc
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 04e7bf5..3375649 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -2887,14 +2887,6 @@
}
}
-void SurfaceFlinger::latchAndReleaseBuffer(const sp<Layer>& layer) {
- if (layer->hasReadyFrame()) {
- bool ignored = false;
- layer->latchBuffer(ignored, systemTime(), 0 /* expectedPresentTime */);
- }
- layer->releasePendingBuffer(systemTime());
-}
-
void SurfaceFlinger::commitTransaction()
{
if (!mLayersPendingRemoval.isEmpty()) {
@@ -2905,7 +2897,7 @@
// Ensure any buffers set to display on any children are released.
if (l->isRemovedFromCurrentState()) {
- latchAndReleaseBuffer(l);
+ l->latchAndReleaseBuffer();
}
// If the layer has been removed and has no parent, then it will not be reachable
@@ -3163,6 +3155,14 @@
}
});
+ // The client can continue submitting buffers for offscreen layers, but they will not
+ // be shown on screen. Therefore, we need to latch and release buffers of offscreen
+ // layers to ensure dequeueBuffer doesn't block indefinitely.
+ for (Layer* offscreenLayer : mOffscreenLayers) {
+ offscreenLayer->traverseInZOrder(LayerVector::StateSet::Drawing,
+ [&](Layer* l) { l->latchAndReleaseBuffer(); });
+ }
+
if (!mLayersWithQueuedFrames.empty()) {
// mStateLock is needed for latchBuffer as LayerRejecter::reject()
// writes to Layer current state. See also b/119481871