Use destroy in transaction for animation

Previously, the code calls destroy which will be invoked immediately so there
needed to be ways to delay destroy. This caused some overhead where there
needed to be Runnables to ensure client state updates were called before the
destroy. Instead, use the Transaction.destroy so the destroy doesn't get invoked
until apply is called. This allows any other client states to get updated before
the destroy is called.

This is specifically necessary for reparent calls since a Surface can be
reparented from a Surface that's about to get destroyed to a valid Surface
that's not getting destroyed. This change ensures that Surfaces will get
reparented before Surfaces are destroyed when Transactions are applied.

Original CL: ag/3573288
Reason for revert: Original issue is fixed in ag/3647191.

Fixes: 72953020
Test: SurfaceAnimatorTest
Change-Id: I8bc58e0efc2ae1558c122a40eb52e9c874c0c997
diff --git a/services/core/java/com/android/server/wm/DisplayContent.java b/services/core/java/com/android/server/wm/DisplayContent.java
index 13357b8..16228d7 100644
--- a/services/core/java/com/android/server/wm/DisplayContent.java
+++ b/services/core/java/com/android/server/wm/DisplayContent.java
@@ -382,11 +382,6 @@
      */
     private int mSurfaceSize;
 
-    /**
-     * A list of surfaces to be destroyed after {@link #mPendingTransaction} is applied.
-     */
-    private final ArrayList<SurfaceControl> mPendingDestroyingSurfaces = new ArrayList<>();
-
     /** Temporary float array to retrieve 3x3 matrix values. */
     private final float[] mTmpFloats = new float[9];
 
@@ -1954,10 +1949,6 @@
                 }
             }
             mService.mAnimator.removeDisplayLocked(mDisplayId);
-
-            // The pending transaction won't be applied so we should
-            // just clean up any surfaces pending destruction.
-            onPendingTransactionApplied();
         } finally {
             mRemovingDisplay = false;
         }
@@ -3874,22 +3865,6 @@
     }
 
     @Override
-    public void destroyAfterPendingTransaction(SurfaceControl surface) {
-        mPendingDestroyingSurfaces.add(surface);
-    }
-
-    /**
-     * Destroys any surfaces that have been put into the pending list with
-     * {@link #destroyAfterPendingTransaction}.
-     */
-    void onPendingTransactionApplied() {
-        for (int i = mPendingDestroyingSurfaces.size() - 1; i >= 0; i--) {
-            mPendingDestroyingSurfaces.get(i).destroy();
-        }
-        mPendingDestroyingSurfaces.clear();
-    }
-
-    @Override
     void prepareSurfaces() {
         final ScreenRotationAnimation screenRotationAnimation =
                 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
@@ -3903,6 +3878,7 @@
             mPendingTransaction.setAlpha(mWindowingLayer,
                     screenRotationAnimation.getEnterTransformation().getAlpha());
         }
+
         super.prepareSurfaces();
     }