Give SystemUI a chance to participate in display rotation

This adds support for registering a single DisplayRotationController
to WMS. It gives a chance for the controller to suggest some
task changes to be executed along with a display rotation. There
is only one because it's a 2-way communication and there is only
intended to be one client for now.

This allows us to move Split and PiP presentation/layout logic out
of WM into systemui because WM no-longer needs to be the one
calculating the new bounds of everything during rotation.

This uses the windowcontainer transaction because all the
configuration changes and the display rotation need to happen
synchronously; otherwise, relayouts can occur after the display
is rotated, but before the configuration changes are applied.
When this happens, apps get incorrect bounds/insets which can
trigger erroneous lifecycle events in the app.

The flow is like this:
1. DisplayContent/Rotation freezes screen
2. DisplayRotationController is notified of a rotation and provided a
   callback.
3. The Controller then evaluates/queues some task changes in
   a transaction and, when done, fires the provided callback.
4. The callback applies the config change transaction and continues
   the rest of the rotation synchronously.

The DisplayWindowController is sys-ui piece that serves as an
interface between system-ui components and display-related wm
logic. For now it just facilitates the rotation calculation, but
in the future it will have more general utility for display logic
like inset/bounds calculation.

Bug: 124011688
Bug: 133381284
Test: Added some wmtests and coretests.
Change-Id: If10695f44fa076725ba17746842f6fbd64da5d20
diff --git a/services/core/java/com/android/server/wm/DisplayRotation.java b/services/core/java/com/android/server/wm/DisplayRotation.java
index 67f1d1b..0c68084 100644
--- a/services/core/java/com/android/server/wm/DisplayRotation.java
+++ b/services/core/java/com/android/server/wm/DisplayRotation.java
@@ -45,15 +45,19 @@
 import android.hardware.power.V1_0.PowerHint;
 import android.net.Uri;
 import android.os.Handler;
+import android.os.RemoteException;
 import android.os.SystemProperties;
 import android.os.UserHandle;
 import android.provider.Settings;
 import android.util.Slog;
 import android.util.SparseArray;
+import android.view.IDisplayWindowRotationCallback;
 import android.view.Surface;
+import android.view.WindowContainerTransaction;
 
 import com.android.internal.R;
 import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.util.function.pooled.PooledLambda;
 import com.android.server.LocalServices;
 import com.android.server.UiThread;
 import com.android.server.policy.WindowManagerPolicy;
@@ -212,6 +216,31 @@
     private boolean mDemoHdmiRotationLock;
     private boolean mDemoRotationLock;
 
+    private static final int REMOTE_ROTATION_TIMEOUT_MS = 800;
+
+    private boolean mIsWaitingForRemoteRotation = false;
+
+    private final Runnable mDisplayRotationHandlerTimeout =
+            new Runnable() {
+                @Override
+                public void run() {
+                    continueRotation(mRotation, null /* transaction */);
+                }
+            };
+
+    private final IDisplayWindowRotationCallback mRemoteRotationCallback =
+            new IDisplayWindowRotationCallback.Stub() {
+                @Override
+                public void continueRotateDisplay(int targetRotation,
+                        WindowContainerTransaction t) {
+                    synchronized (mService.getWindowManagerLock()) {
+                        mService.mH.sendMessage(PooledLambda.obtainMessage(
+                                DisplayRotation::continueRotation, DisplayRotation.this,
+                                targetRotation, t));
+                    }
+                }
+            };
+
     DisplayRotation(WindowManagerService service, DisplayContent displayContent) {
         this(service, displayContent, displayContent.getDisplayPolicy(),
                 service.mDisplayWindowSettings, service.mContext, service.getWindowManagerLock());
@@ -471,9 +500,52 @@
             prepareNormalRotationAnimation();
         }
 
+        // The display is frozen now, give a remote handler (system ui) some time to reposition
+        // things.
+        startRemoteRotation(oldRotation, mRotation);
+
         return true;
     }
 
+    /**
+     * A Remote rotation is when we are waiting for some registered (remote)
+     * {@link IDisplayWindowRotationController} to calculate and return some hierarchy operations
+     *  to perform in sync with the rotation.
+     */
+    boolean isWaitingForRemoteRotation() {
+        return mIsWaitingForRemoteRotation;
+    }
+
+    private void startRemoteRotation(int fromRotation, int toRotation) {
+        if (mService.mDisplayRotationController == null) {
+            return;
+        }
+        mIsWaitingForRemoteRotation = true;
+        try {
+            mService.mDisplayRotationController.onRotateDisplay(mDisplayContent.getDisplayId(),
+                    fromRotation, toRotation, mRemoteRotationCallback);
+            mService.mH.removeCallbacks(mDisplayRotationHandlerTimeout);
+            mService.mH.postDelayed(mDisplayRotationHandlerTimeout, REMOTE_ROTATION_TIMEOUT_MS);
+        } catch (RemoteException e) {
+            mIsWaitingForRemoteRotation = false;
+            return;
+        }
+    }
+
+    private void continueRotation(int targetRotation, WindowContainerTransaction t) {
+        synchronized (mService.mGlobalLock) {
+            if (targetRotation != mRotation || !mIsWaitingForRemoteRotation) {
+                // Drop it, this is either coming from an outdated remote rotation; or, we've
+                // already moved on.
+                return;
+            }
+            mService.mH.removeCallbacks(mDisplayRotationHandlerTimeout);
+            mIsWaitingForRemoteRotation = false;
+            mService.mAtmService.applyContainerTransaction(t);
+            mDisplayContent.sendNewConfiguration();
+        }
+    }
+
     void prepareNormalRotationAnimation() {
         final RotationAnimationPair anim = selectRotationAnimation();
         mService.startFreezingDisplayLocked(anim.mExit, anim.mEnter, mDisplayContent);