[sf] fix crash when taking a screenshot
If a screenshot is requested immediately after it was created,
there might be a chance the layer does not exist in the main
thread data structures. This can happen if the screenshot
request is handled before main thread can create layers
added from the binder thread. Make sure we don't crash
when taking a screenshot by checking if the layer exists before
checking if it's secure.
Fixes: 312545483
Test: presubmit
Test: atest com.android.server.wm.ZOrderingTests#testImeScreenshotLayer
Change-Id: If765794e0fd9eb6c2bc2c65d8bc4c7523d905fa6
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index c15e74f..49adffe 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -8948,7 +8948,10 @@
.genericLayerMetadataKeyMap = getGenericLayerMetadataKeyMap(),
.skipRoundCornersWhenProtected =
!getRenderEngine().supportsProtectedContent()};
- args.rootSnapshot.isSecure = mLayerLifecycleManager.isLayerSecure(rootLayerId);
+ // The layer may not exist if it was just created and a screenshot was requested immediately
+ // after. In this case, the hierarchy will be empty so we will not render any layers.
+ args.rootSnapshot.isSecure = mLayerLifecycleManager.getLayerFromId(rootLayerId) &&
+ mLayerLifecycleManager.isLayerSecure(rootLayerId);
mLayerSnapshotBuilder.update(args);
auto getLayerSnapshotsFn =