From cb50daa74cf2718c21ca12e240ecae3bb6a382de Mon Sep 17 00:00:00 2001 From: Naomi Musgrave Date: Fri, 27 Aug 2021 17:51:34 +0100 Subject: [PATCH] [MediaProjection] Store WindowContext in field When WindowContext is a local variable for starting layer mirroring, the variable is eventually garbage collected. When this occurs when MediaProjection is still running, then DisplayContent fails to transform the captured content from onConfigurationChanged. The destroyed WindowContext triggers unregistering the WindowContainerListener. Even though the WindowToken is still being used by DisplayContent, DisplayContent is no longer able to retrieve the WindowContainer associated with the WindowToken. The WindowToken set when layer mirroring begins is used to indicate the layer of the hierarchy to mirror. Bug: 197579637 Test: Manual Change-Id: Ib8a971d1a85ebfe9db59d9267cc387f4f4d15991 --- .../android/media/projection/MediaProjection.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/media/java/android/media/projection/MediaProjection.java b/media/java/android/media/projection/MediaProjection.java index 72cddc91f436a..353556d70a198 100644 --- a/media/java/android/media/projection/MediaProjection.java +++ b/media/java/android/media/projection/MediaProjection.java @@ -50,6 +50,13 @@ public final class MediaProjection { private final Context mContext; private final Map mCallbacks; + /** + * Store the WindowContext in a field. If it is a local variable, and it is garbage collected + * during a MediaProjection session, the WindowContainer listener no longer exists. + */ + @Nullable + private Context mWindowContext; + /** @hide */ public MediaProjection(Context context, IMediaProjection impl) { mCallbacks = new ArrayMap(); @@ -162,11 +169,11 @@ public final class MediaProjection { */ private VirtualDisplayConfig.Builder buildMirroredVirtualDisplay(@NonNull String name, int width, int height, int dpi) { - Context windowContext = mContext.createWindowContext(mContext.getDisplayNoVerify(), + mWindowContext = mContext.createWindowContext(mContext.getDisplayNoVerify(), TYPE_APPLICATION, null /* options */); final VirtualDisplayConfig.Builder builder = new VirtualDisplayConfig.Builder(name, width, height, dpi); - builder.setWindowTokenClientToMirror(windowContext.getWindowContextToken()); + builder.setWindowTokenClientToMirror(mWindowContext.getWindowContextToken()); return builder; }