Set initial position for WindowAnimation and reparent animation leash.

1. Set the initial position for the WindowAnimationSpec to be the stack
position.
2. Reparent the animation leash to mAnimationLayer
3. Updated Surface names for clarity.

Test: Opening apps animate from the correct start location. When apps
are animating, their layers are reparented to the correct animation
layer.

Change-Id: I3e3a1e45f0a0cf9d471dee105abd9bce05d1e91d
diff --git a/services/core/java/com/android/server/wm/AppWindowToken.java b/services/core/java/com/android/server/wm/AppWindowToken.java
index 04a1beb..444a05d 100644
--- a/services/core/java/com/android/server/wm/AppWindowToken.java
+++ b/services/core/java/com/android/server/wm/AppWindowToken.java
@@ -52,7 +52,6 @@
 import static com.android.server.wm.proto.AppWindowTokenProto.WINDOW_TOKEN;
 
 import android.annotation.CallSuper;
-import android.annotation.NonNull;
 import android.app.Activity;
 import android.content.res.Configuration;
 import android.graphics.GraphicBuffer;
@@ -216,6 +215,8 @@
     /** Whether this token should be boosted at the top of all app window tokens. */
     private boolean mNeedsZBoost;
 
+    private final Point mTmpPoint = new Point();
+
     AppWindowToken(WindowManagerService service, IApplicationToken token, boolean voiceInteraction,
             DisplayContent dc, long inputDispatchingTimeoutNanos, boolean fullscreen,
             boolean showForAllUsers, int targetSdk, int orientation, int rotationAnimationHint,
@@ -1503,6 +1504,12 @@
                 true /* topToBottom */);
     }
 
+    @Override
+    public SurfaceControl.Builder makeAnimationLeash() {
+        return super.makeAnimationLeash()
+                .setParent(getAppAnimationLayer());
+    }
+
     boolean applyAnimationLocked(WindowManager.LayoutParams lp, int transit, boolean enter,
             boolean isVoiceInteraction) {
 
@@ -1521,8 +1528,13 @@
         if (okToAnimate()) {
             final Animation a = loadAnimation(lp, transit, enter, isVoiceInteraction);
             if (a != null) {
+                final TaskStack stack = getStack();
+                mTmpPoint.set(0, 0);
+                if (stack != null) {
+                    stack.getRelativePosition(mTmpPoint);
+                }
                 final AnimationAdapter adapter = new LocalAnimationAdapter(
-                        new WindowAnimationSpec(a, new Point(),
+                        new WindowAnimationSpec(a, mTmpPoint,
                                 mService.mAppTransition.canSkipFirstFrame()),
                         mService.mSurfaceAnimationRunner);
                 startAnimation(getPendingTransaction(), adapter, !isVisible());
diff --git a/services/core/java/com/android/server/wm/DisplayContent.java b/services/core/java/com/android/server/wm/DisplayContent.java
index 8d7f432..d053015 100644
--- a/services/core/java/com/android/server/wm/DisplayContent.java
+++ b/services/core/java/com/android/server/wm/DisplayContent.java
@@ -136,7 +136,6 @@
 import android.util.MutableBoolean;
 import android.util.Slog;
 import android.util.proto.ProtoOutputStream;
-import android.view.animation.Transformation;
 import android.view.Display;
 import android.view.DisplayInfo;
 import android.view.InputDevice;
@@ -3176,7 +3175,7 @@
         /**
          * A control placed at the appropriate level for transitions to occur.
          */
-        SurfaceControl mAnimationLayer = null;
+        SurfaceControl mAppAnimationLayer = null;
 
         // Cached reference to some special stacks we tend to get a lot so we don't need to loop
         // through the list to find them.
@@ -3522,19 +3521,28 @@
             // The appropriate place for App-Transitions to occur is right
             // above all other animations but still below things in the Picture-and-Picture
             // windowing mode.
-            if (mAnimationLayer != null) {
-                t.setLayer(mAnimationLayer, layer++);
+            if (mAppAnimationLayer != null) {
+                t.setLayer(mAppAnimationLayer, layer++);
             }
         }
 
         @Override
+        SurfaceControl getAppAnimationLayer() {
+            return mAppAnimationLayer;
+        }
+
+        @Override
         void onParentSet() {
             super.onParentSet();
             if (getParent() != null) {
-                mAnimationLayer = makeSurface().build();
+                mAppAnimationLayer = makeChildSurface(null)
+                        .setName("animationLayer")
+                        .build();
+                getPendingTransaction().show(mAppAnimationLayer);
+                scheduleAnimation();
             } else {
-                mAnimationLayer.destroy();
-                mAnimationLayer = null;
+                mAppAnimationLayer.destroy();
+                mAppAnimationLayer = null;
             }
         }
     }
diff --git a/services/core/java/com/android/server/wm/TaskSnapshotController.java b/services/core/java/com/android/server/wm/TaskSnapshotController.java
index c091157..f79719c 100644
--- a/services/core/java/com/android/server/wm/TaskSnapshotController.java
+++ b/services/core/java/com/android/server/wm/TaskSnapshotController.java
@@ -93,6 +93,8 @@
     private final ArraySet<Task> mTmpTasks = new ArraySet<>();
     private final Handler mHandler = new Handler();
 
+    private final Rect mTmpRect = new Rect();
+
     /**
      * Flag indicating whether we are running on an Android TV device.
      */
@@ -223,11 +225,11 @@
 
         final boolean isLowRamDevice = ActivityManager.isLowRamDeviceStatic();
         final float scaleFraction = isLowRamDevice ? REDUCED_SCALE : 1f;
-        final Rect taskFrame = new Rect();
-        task.getBounds(taskFrame);
+        task.getBounds(mTmpRect);
+        mTmpRect.offsetTo(0, 0);
 
         final GraphicBuffer buffer = SurfaceControl.captureLayers(
-                task.getSurfaceControl().getHandle(), taskFrame, scaleFraction);
+                task.getSurfaceControl().getHandle(), mTmpRect, scaleFraction);
 
         if (buffer == null || buffer.getWidth() <= 1 || buffer.getHeight() <= 1) {
             if (DEBUG_SCREENSHOT) {
diff --git a/services/core/java/com/android/server/wm/WindowContainer.java b/services/core/java/com/android/server/wm/WindowContainer.java
index f297cc8..7641cbc 100644
--- a/services/core/java/com/android/server/wm/WindowContainer.java
+++ b/services/core/java/com/android/server/wm/WindowContainer.java
@@ -1020,6 +1020,17 @@
         return makeSurface();
     }
 
+    /**
+     * @return The layer on which all app animations are happening.
+     */
+    SurfaceControl getAppAnimationLayer() {
+        final WindowContainer parent = getParent();
+        if (parent != null) {
+            return parent.getAppAnimationLayer();
+        }
+        return null;
+    }
+
     @Override
     public void commitPendingTransaction() {
         scheduleAnimation();