Merge "Null AppWindowThumbnail SurfaceControl after releasing it."
diff --git a/services/core/java/com/android/server/wm/AppWindowThumbnail.java b/services/core/java/com/android/server/wm/AppWindowThumbnail.java
index bbbf11d..9f7cb3d3 100644
--- a/services/core/java/com/android/server/wm/AppWindowThumbnail.java
+++ b/services/core/java/com/android/server/wm/AppWindowThumbnail.java
@@ -49,7 +49,7 @@
     private static final String TAG = TAG_WITH_CLASS_NAME ? "AppWindowThumbnail" : TAG_WM;
 
     private final AppWindowToken mAppToken;
-    private final SurfaceControl mSurfaceControl;
+    private SurfaceControl mSurfaceControl;
     private final SurfaceAnimator mSurfaceAnimator;
     private final int mWidth;
     private final int mHeight;
@@ -68,10 +68,21 @@
      */
     AppWindowThumbnail(Transaction t, AppWindowToken appToken, GraphicBuffer thumbnailHeader,
             boolean relative) {
+        this(t, appToken, thumbnailHeader, relative, new Surface(), null);
+    }
+
+    AppWindowThumbnail(Transaction t, AppWindowToken appToken, GraphicBuffer thumbnailHeader,
+            boolean relative, Surface drawSurface, SurfaceAnimator animator) {
         mAppToken = appToken;
         mRelative = relative;
-        mSurfaceAnimator =
+        if (animator != null) {
+            mSurfaceAnimator = animator;
+        } else {
+            // We can't use a delegating constructor since we need to
+            // reference this::onAnimationFinished
+            mSurfaceAnimator =
                 new SurfaceAnimator(this, this::onAnimationFinished, appToken.mWmService);
+        }
         mWidth = thumbnailHeader.getWidth();
         mHeight = thumbnailHeader.getHeight();
 
@@ -95,7 +106,6 @@
         }
 
         // Transfer the thumbnail to the surface
-        Surface drawSurface = new Surface();
         drawSurface.copyFrom(mSurfaceControl);
         drawSurface.attachAndQueueBuffer(thumbnailHeader);
         drawSurface.release();
@@ -145,6 +155,7 @@
     void destroy() {
         mSurfaceAnimator.cancelAnimation();
         getPendingTransaction().remove(mSurfaceControl);
+        mSurfaceControl = null;
     }
 
     /**
diff --git a/services/tests/wmtests/src/com/android/server/wm/AppWindowThumbnailTest.java b/services/tests/wmtests/src/com/android/server/wm/AppWindowThumbnailTest.java
new file mode 100644
index 0000000..fa0c384
--- /dev/null
+++ b/services/tests/wmtests/src/com/android/server/wm/AppWindowThumbnailTest.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.wm;
+
+import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock;
+
+import static com.android.dx.mockito.inline.extended.ExtendedMockito.when;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import android.graphics.GraphicBuffer;
+import android.graphics.PixelFormat;
+import android.platform.test.annotations.Presubmit;
+import android.view.Surface;
+import android.view.SurfaceControl;
+
+import androidx.test.filters.SmallTest;
+
+import org.junit.Test;
+
+/**
+ * Test class for {@link TaskSnapshotSurface}.
+ *
+ * Build/Install/Run:
+ *  atest FrameworksServicesTest:AppWindowThumbnailTest
+ *
+ */
+@SmallTest
+@Presubmit
+public class AppWindowThumbnailTest extends WindowTestsBase {
+    private AppWindowThumbnail buildThumbnail() {
+        final GraphicBuffer buffer = GraphicBuffer.create(1, 1, PixelFormat.RGBA_8888,
+                GraphicBuffer.USAGE_SW_READ_RARELY | GraphicBuffer.USAGE_SW_WRITE_NEVER);
+        final AppWindowToken mockAwt = mock(AppWindowToken.class);
+        when(mockAwt.getPendingTransaction()).thenReturn(new StubTransaction());
+        when(mockAwt.makeSurface()).thenReturn(new MockSurfaceControlBuilder());
+        return new AppWindowThumbnail(new StubTransaction(), mockAwt,
+                buffer, false, mock(Surface.class), mock(SurfaceAnimator.class));
+    }
+
+    @Test
+    public void testDestroy_nullsSurface() {
+        final AppWindowThumbnail t = buildThumbnail();
+        assertNotNull(t.getSurfaceControl());
+        t.destroy();
+        assertNull(t.getSurfaceControl());
+    }
+}