Keep fixed rotation transformed surface position for animation leash

The orientation of the target window can be different than who controls
the leash. In order to let the animator use the leash without additional
transformation, the current position of surface needs to be preserved.

Also fix a flaky test due to the display was frozen.

Fixes: 153561756
Fixes: 154030098
Test: atest DisplayContentTests#testApplyTopFixedRotationTransform
      ActivityRecordTests#testActivityOnCancelFixedRotationTransform
Change-Id: I4b75e830b4d63143c6825f0fa5ccbf333b2ba6ff
diff --git a/services/core/java/com/android/server/wm/WindowContainer.java b/services/core/java/com/android/server/wm/WindowContainer.java
index 7219164..77530fb 100644
--- a/services/core/java/com/android/server/wm/WindowContainer.java
+++ b/services/core/java/com/android/server/wm/WindowContainer.java
@@ -2304,14 +2304,18 @@
         }
     }
 
+    void resetSurfacePositionForAnimationLeash(Transaction t) {
+        t.setPosition(mSurfaceControl, 0, 0);
+        mLastSurfacePosition.set(0, 0);
+    }
+
     @Override
     public void onAnimationLeashCreated(Transaction t, SurfaceControl leash) {
         mLastLayer = -1;
         reassignLayer(t);
 
         // Leash is now responsible for position, so set our position to 0.
-        t.setPosition(mSurfaceControl, 0, 0);
-        mLastSurfacePosition.set(0, 0);
+        resetSurfacePositionForAnimationLeash(t);
     }
 
     @Override
diff --git a/services/core/java/com/android/server/wm/WindowState.java b/services/core/java/com/android/server/wm/WindowState.java
index 2e1b907..5f2e14f 100644
--- a/services/core/java/com/android/server/wm/WindowState.java
+++ b/services/core/java/com/android/server/wm/WindowState.java
@@ -5250,16 +5250,6 @@
     }
 
     @Override
-    public void onAnimationLeashCreated(Transaction t, SurfaceControl leash) {
-        super.onAnimationLeashCreated(t, leash);
-    }
-
-    @Override
-    public void onAnimationLeashLost(Transaction t) {
-        super.onAnimationLeashLost(t);
-    }
-
-    @Override
     @VisibleForTesting
     void updateSurfacePosition(Transaction t) {
         if (mSurfaceControl == null) {
diff --git a/services/core/java/com/android/server/wm/WindowToken.java b/services/core/java/com/android/server/wm/WindowToken.java
index b4e770f..21c7687 100644
--- a/services/core/java/com/android/server/wm/WindowToken.java
+++ b/services/core/java/com/android/server/wm/WindowToken.java
@@ -632,6 +632,15 @@
         }
     }
 
+    @Override
+    void resetSurfacePositionForAnimationLeash(SurfaceControl.Transaction t) {
+        // Keep the transformed position to animate because the surface will show in different
+        // rotation than the animator of leash.
+        if (!isFixedRotationTransforming()) {
+            super.resetSurfacePositionForAnimationLeash(t);
+        }
+    }
+
     /**
      * Gives a chance to this {@link WindowToken} to adjust the {@link
      * android.view.WindowManager.LayoutParams} of its windows.
diff --git a/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java b/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java
index bc66fa7..5bbb4d9 100644
--- a/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java
@@ -1324,7 +1324,7 @@
 
         display.rotateInDifferentOrientationIfNeeded(mActivity);
         display.mFixedRotationLaunchingApp = mActivity;
-        displayRotation.updateRotationUnchecked(false /* forceUpdate */);
+        displayRotation.updateRotationUnchecked(true /* forceUpdate */);
 
         assertTrue(displayRotation.isRotatingSeamlessly());
 
diff --git a/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java b/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java
index 3f47b87..3fd81b4 100644
--- a/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java
@@ -90,6 +90,7 @@
 import android.view.IWindowManager;
 import android.view.MotionEvent;
 import android.view.Surface;
+import android.view.SurfaceControl.Transaction;
 import android.view.ViewRootImpl;
 import android.view.WindowManager;
 import android.view.test.InsetsModeSession;
@@ -1040,6 +1041,12 @@
         assertEquals(config90.orientation, app.getConfiguration().orientation);
         assertEquals(config90.windowConfiguration.getBounds(), app.getBounds());
 
+        // Make wallaper laid out with the fixed rotation transform.
+        final WindowToken wallpaperToken = mWallpaperWindow.mToken;
+        wallpaperToken.linkFixedRotationTransform(app);
+        mWallpaperWindow.mLayoutNeeded = true;
+        performLayout(mDisplayContent);
+
         // Force the negative offset to verify it can be updated.
         mWallpaperWindow.mWinAnimator.mXOffset = mWallpaperWindow.mWinAnimator.mYOffset = -1;
         assertTrue(mDisplayContent.mWallpaperController.updateWallpaperOffset(mWallpaperWindow,
@@ -1047,6 +1054,13 @@
         assertThat(mWallpaperWindow.mWinAnimator.mXOffset).isGreaterThan(-1);
         assertThat(mWallpaperWindow.mWinAnimator.mYOffset).isGreaterThan(-1);
 
+        // The wallpaper need to animate with transformed position, so its surface position should
+        // not be reset.
+        final Transaction t = wallpaperToken.getPendingTransaction();
+        spyOn(t);
+        mWallpaperWindow.mToken.onAnimationLeashCreated(t, null /* leash */);
+        verify(t, never()).setPosition(any(), eq(0), eq(0));
+
         mDisplayContent.mAppTransition.notifyAppTransitionFinishedLocked(app.token);
 
         // The animation in old rotation should be cancelled.