Move rotation tracking to DisplayContent

This CL moves rotation tracking from WindowManagerService to
DisplayContent. This way displays can be rotated independently and
rotation of the main display won't affect rotation of secondary
ones.

Bug: 34242678
Test: android.server.cts.ActivityManagerDisplayTests
Test: testRotationNotAffectingSecondaryScreen
Change-Id: Ic46aaa523482b31ff5ec77f0c2908ceda1156fc0
diff --git a/services/core/java/com/android/server/wm/AccessibilityController.java b/services/core/java/com/android/server/wm/AccessibilityController.java
index b7479da..1510dd1 100644
--- a/services/core/java/com/android/server/wm/AccessibilityController.java
+++ b/services/core/java/com/android/server/wm/AccessibilityController.java
@@ -156,9 +156,9 @@
         }
     }
 
-    public void onRotationChangedLocked(DisplayContent displayContent, int rotation) {
+    public void onRotationChangedLocked(DisplayContent displayContent) {
         if (mDisplayMagnifier != null) {
-            mDisplayMagnifier.onRotationChangedLocked(displayContent, rotation);
+            mDisplayMagnifier.onRotationChangedLocked(displayContent);
         }
         if (mWindowsForAccessibilityObserver != null) {
             mWindowsForAccessibilityObserver.scheduleComputeChangedWindowsLocked();
@@ -312,9 +312,10 @@
             mWindowManagerService.scheduleAnimationLocked();
         }
 
-        public void onRotationChangedLocked(DisplayContent displayContent, int rotation) {
+        public void onRotationChangedLocked(DisplayContent displayContent) {
             if (DEBUG_ROTATION) {
-                Slog.i(LOG_TAG, "Rotaton: " + Surface.rotationToString(rotation)
+                final int rotation = displayContent.getRotation();
+                Slog.i(LOG_TAG, "Rotation: " + Surface.rotationToString(rotation)
                         + " displayId: " + displayContent.getDisplayId());
             }
             mMagnifedViewport.onRotationChangedLocked();
diff --git a/services/core/java/com/android/server/wm/DisplayContent.java b/services/core/java/com/android/server/wm/DisplayContent.java
index 5486aa8..cb3a663 100644
--- a/services/core/java/com/android/server/wm/DisplayContent.java
+++ b/services/core/java/com/android/server/wm/DisplayContent.java
@@ -182,6 +182,42 @@
     private final Display mDisplay;
     private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
 
+    /**
+     * Current rotation of the display.
+     * Constants as per {@link android.view.Surface.Rotation}.
+     *
+     * @see WindowManagerService#updateRotationUncheckedLocked(boolean, int)
+     */
+    private int mRotation = 0;
+    /**
+     * Last applied orientation of the display.
+     * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
+     *
+     * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
+     */
+    private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
+    /**
+     * Flag indicating that the application is receiving an orientation that has different metrics
+     * than it expected. E.g. Portrait instead of Landscape.
+     *
+     * @see WindowManagerService#updateRotationUncheckedLocked(boolean, int)
+     */
+    private boolean mAltOrientation = false;
+    /**
+     * Orientation forced by some window. If there is no visible window that specifies orientation
+     * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
+     *
+     * @see NonAppWindowContainers#getOrientation()
+     */
+    private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
+    /**
+     * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
+     * occluded.
+     *
+     * @see NonAppWindowContainers#getOrientation()
+     */
+    private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
+
     Rect mBaseDisplayRect = new Rect();
     private Rect mContentRect = new Rect();
 
@@ -764,6 +800,34 @@
         return mDisplayMetrics;
     }
 
+    int getRotation() {
+        return mRotation;
+    }
+
+    void setRotation(int newRotation) {
+        mRotation = newRotation;
+    }
+
+    int getLastOrientation() {
+        return mLastOrientation;
+    }
+
+    void setLastOrientation(int orientation) {
+        mLastOrientation = orientation;
+    }
+
+    boolean getAltOrientation() {
+        return mAltOrientation;
+    }
+
+    void setAltOrientation(boolean altOrientation) {
+        mAltOrientation = altOrientation;
+    }
+
+    int getLastWindowForcedOrientation() {
+        return mLastWindowForcedOrientation;
+    }
+
     DockedStackDividerController getDockedDividerController() {
         return mDividerControllerLocked;
     }
@@ -884,14 +948,14 @@
         final WindowManagerPolicy policy = mService.mPolicy;
 
         if (mService.mDisplayFrozen) {
-            if (mService.mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
+            if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
                 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
-                        "Display is frozen, return " + mService.mLastWindowForcedOrientation);
+                        "Display is frozen, return " + mLastWindowForcedOrientation);
                 // If the display is frozen, some activities may be in the middle of restarting, and
                 // thus have removed their old window. If the window has the flag to hide the lock
                 // screen, then the lock screen can re-appear and inflict its own orientation on us.
                 // Keep the orientation stable until this all settles down.
-                return mService.mLastWindowForcedOrientation;
+                return mLastWindowForcedOrientation;
             } else if (policy.isKeyguardLocked()) {
                 // Use the last orientation the while the display is frozen with the keyguard
                 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
@@ -899,8 +963,8 @@
                 // things aren't stable while the display is frozen, for example the window could be
                 // momentarily unavailable due to activity relaunch.
                 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
-                        + "return " + mService.mLastOrientation);
-                return mService.mLastOrientation;
+                        + "return " + mLastOrientation);
+                return mLastOrientation;
             }
         } else {
             final int orientation = mAboveAppWindowsContainers.getOrientation();
@@ -2051,7 +2115,7 @@
             Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
         }
 
-        mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mService.mRotation,
+        mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
                 getConfiguration().uiMode);
         if (isDefaultDisplay) {
             // Not needed on non-default displays.
@@ -2712,10 +2776,10 @@
             }
 
             if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
-                    "No app is requesting an orientation, return " + mService.mLastOrientation);
+                    "No app is requesting an orientation, return " + mLastOrientation);
             // The next app has not been requested to be visible, so we keep the current orientation
             // to prevent freezing/unfreezing the display too early.
-            return mService.mLastOrientation;
+            return mLastOrientation;
         }
     }
 
@@ -2766,15 +2830,15 @@
                 final int req = win.mAttrs.screenOrientation;
                 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
                 if (policy.isKeyguardHostWindow(win.mAttrs)) {
-                    mService.mLastKeyguardForcedOrientation = req;
+                    mLastKeyguardForcedOrientation = req;
                 }
-                return (mService.mLastWindowForcedOrientation = req);
+                return (mLastWindowForcedOrientation = req);
             }
 
-            mService.mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
+            mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
 
             if (policy.isKeyguardShowingAndNotOccluded()) {
-                return mService.mLastKeyguardForcedOrientation;
+                return mLastKeyguardForcedOrientation;
             }
 
             return SCREEN_ORIENTATION_UNSET;
diff --git a/services/core/java/com/android/server/wm/RootWindowContainer.java b/services/core/java/com/android/server/wm/RootWindowContainer.java
index 126e080..0222b3d 100644
--- a/services/core/java/com/android/server/wm/RootWindowContainer.java
+++ b/services/core/java/com/android/server/wm/RootWindowContainer.java
@@ -802,6 +802,7 @@
         mHoldScreenWindow = null;
         mObscuringWindow = null;
 
+        // TODO(multi-display): Support these features on secondary screens.
         if (mService.mWatermark != null) {
             mService.mWatermark.positionSurface(defaultDw, defaultDh);
         }
@@ -809,11 +810,12 @@
             mService.mStrictModeFlash.positionSurface(defaultDw, defaultDh);
         }
         if (mService.mCircularDisplayMask != null) {
-            mService.mCircularDisplayMask.positionSurface(defaultDw, defaultDh, mService.mRotation);
+            mService.mCircularDisplayMask.positionSurface(defaultDw, defaultDh,
+                    mService.getDefaultDisplayRotation());
         }
         if (mService.mEmulatorDisplayOverlay != null) {
             mService.mEmulatorDisplayOverlay.positionSurface(defaultDw, defaultDh,
-                    mService.mRotation);
+                    mService.getDefaultDisplayRotation());
         }
 
         boolean focusDisplayed = false;
diff --git a/services/core/java/com/android/server/wm/WindowAnimator.java b/services/core/java/com/android/server/wm/WindowAnimator.java
index 993a918..57fb81c 100644
--- a/services/core/java/com/android/server/wm/WindowAnimator.java
+++ b/services/core/java/com/android/server/wm/WindowAnimator.java
@@ -161,7 +161,7 @@
                             // We just finished rotation animation which means we did not announce
                             // the rotation and waited for it to end, announce now.
                             accessibilityController.onRotationChangedLocked(
-                                    mService.getDefaultDisplayContentLocked(), mService.mRotation);
+                                    mService.getDefaultDisplayContentLocked());
                         }
                     }
                 }
diff --git a/services/core/java/com/android/server/wm/WindowManagerService.java b/services/core/java/com/android/server/wm/WindowManagerService.java
index c0cc923..47c62f9 100644
--- a/services/core/java/com/android/server/wm/WindowManagerService.java
+++ b/services/core/java/com/android/server/wm/WindowManagerService.java
@@ -16,21 +16,15 @@
 
 package com.android.server.wm;
 
-import static android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
 import static android.Manifest.permission.MANAGE_APP_TOKENS;
 import static android.Manifest.permission.REGISTER_WINDOW_MANAGER_LISTENERS;
 import static android.app.ActivityManager.DOCKED_STACK_CREATE_MODE_TOP_OR_LEFT;
 import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
-import static android.app.AppOpsManager.MODE_IGNORED;
 import static android.app.AppOpsManager.OP_SYSTEM_ALERT_WINDOW;
 import static android.app.StatusBarManager.DISABLE_MASK;
 import static android.app.admin.DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED;
 import static android.content.Intent.ACTION_USER_REMOVED;
-import static android.content.Intent.EXTRA_PACKAGE_NAME;
-import static android.content.Intent.EXTRA_UID;
 import static android.content.Intent.EXTRA_USER_HANDLE;
-import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
-import static android.content.pm.PackageManager.PERMISSION_GRANTED;
 import static android.os.UserHandle.USER_NULL;
 import static android.view.Display.DEFAULT_DISPLAY;
 import static android.view.WindowManager.DOCKED_INVALID;
@@ -69,8 +63,6 @@
 import static com.android.server.wm.AppTransition.TRANSIT_UNSET;
 import static com.android.server.wm.AppWindowAnimator.PROLONG_ANIMATION_AT_END;
 import static com.android.server.wm.AppWindowAnimator.PROLONG_ANIMATION_AT_START;
-import static com.android.server.wm.DragResizeMode.DRAG_RESIZE_MODE_DOCKED_DIVIDER;
-import static com.android.server.wm.DragResizeMode.DRAG_RESIZE_MODE_FREEFORM;
 import static com.android.server.wm.KeyguardDisableHandler.KEYGUARD_POLICY_CHANGED;
 import static com.android.server.wm.WindowManagerDebugConfig.DEBUG;
 import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
@@ -112,7 +104,6 @@
 import android.app.ActivityManagerInternal;
 import android.app.AppOpsManager;
 import android.app.IActivityManager;
-import android.app.admin.DevicePolicyManager;
 import android.content.BroadcastReceiver;
 import android.content.ContentResolver;
 import android.content.Context;
@@ -528,11 +519,6 @@
     // The root of the device window hierarchy.
     RootWindowContainer mRoot;
 
-    // TODO: Move several of this states to the RootWindowContainer or DisplayContent
-    int mRotation = 0;
-    int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
-    boolean mAltOrientation = false;
-
     int mDockedStackCreateMode = DOCKED_STACK_CREATE_MODE_TOP_OR_LEFT;
     Rect mDockedStackCreateBounds;
 
@@ -573,8 +559,6 @@
 
     boolean mClientFreezingScreen = false;
     int mAppsFreezingScreen = 0;
-    int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
-    int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
 
     int mLayoutSeq = 0;
 
@@ -1450,7 +1434,7 @@
                 } else {
                     taskBounds = null;
                 }
-                if (mPolicy.getInsetHintLw(win.mAttrs, taskBounds, mRotation,
+                if (mPolicy.getInsetHintLw(win.mAttrs, taskBounds, displayInfo.rotation,
                         displayInfo.logicalWidth, displayInfo.logicalHeight, outContentInsets,
                         outStableInsets, outOutsets)) {
                     res |= WindowManagerGlobal.ADD_FLAG_ALWAYS_CONSUME_NAV_BAR;
@@ -2505,12 +2489,16 @@
     boolean updateOrientationFromAppTokensLocked(boolean inTransaction, int displayId) {
         long ident = Binder.clearCallingIdentity();
         try {
-            final int req = mRoot.getDisplayContent(displayId).getOrientation();
-            if (req != mLastOrientation) {
-                mLastOrientation = req;
+            final DisplayContent dc = mRoot.getDisplayContent(displayId);
+            final int req = dc.getOrientation();
+            if (req != dc.getLastOrientation()) {
+                dc.setLastOrientation(req);
                 //send a message to Policy indicating orientation change to take
                 //action like disabling/enabling sensors etc.,
-                mPolicy.setCurrentOrientationLw(req);
+                // TODO(multi-display): Implement policy for secondary displays.
+                if (dc.isDefaultDisplay) {
+                    mPolicy.setCurrentOrientationLw(req);
+                }
                 if (updateRotationUncheckedLocked(inTransaction, displayId)) {
                     // changed
                     return true;
@@ -2527,10 +2515,18 @@
     // changed the real orientation our applied our screen rotation animation.
     // For example, because a previous screen rotation was in progress.
     boolean rotationNeedsUpdateLocked() {
-        int rotation = mPolicy.rotationForOrientationLw(mLastOrientation, mRotation);
+        // TODO(multi-display): Check for updates on all displays. Need to have per-display policy
+        // to implement WindowManagerPolicy#rotationForOrientationLw() correctly.
+        final DisplayContent defaultDisplayContent = getDefaultDisplayContentLocked();
+        final int lastOrientation = defaultDisplayContent.getLastOrientation();
+        final int oldRotation = defaultDisplayContent.getRotation();
+        final boolean oldAltOrientation = defaultDisplayContent.getAltOrientation();
+
+        final int rotation = mPolicy.rotationForOrientationLw(lastOrientation,
+                oldRotation);
         boolean altOrientation = !mPolicy.rotationHasCompatibleMetricsLw(
-                mLastOrientation, rotation);
-        if (mRotation == rotation && mAltOrientation == altOrientation) {
+                lastOrientation, rotation);
+        if (oldRotation == rotation && oldAltOrientation == altOrientation) {
             return false;
         }
         return true;
@@ -3758,6 +3754,7 @@
      */
     @Override
     public void freezeRotation(int rotation) {
+        // TODO(multi-display): Track which display is rotated.
         if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
                 "freezeRotation()")) {
             throw new SecurityException("Requires SET_ORIENTATION permission");
@@ -3767,12 +3764,14 @@
                     + "rotation constant.");
         }
 
-        if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "freezeRotation: mRotation=" + mRotation);
+        final int defaultDisplayRotation = getDefaultDisplayRotation();
+        if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "freezeRotation: mRotation="
+                + defaultDisplayRotation);
 
         long origId = Binder.clearCallingIdentity();
         try {
             mPolicy.setUserRotationMode(WindowManagerPolicy.USER_ROTATION_LOCKED,
-                    rotation == -1 ? mRotation : rotation);
+                    rotation == -1 ? defaultDisplayRotation : rotation);
         } finally {
             Binder.restoreCallingIdentity(origId);
         }
@@ -3791,7 +3790,8 @@
             throw new SecurityException("Requires SET_ORIENTATION permission");
         }
 
-        if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "thawRotation: mRotation=" + mRotation);
+        if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "thawRotation: mRotation="
+                + getDefaultDisplayRotation());
 
         long origId = Binder.clearCallingIdentity();
         try {
@@ -3915,8 +3915,10 @@
 
         final DisplayContent dc = mRoot.getDisplayContent(displayId);
 
-        final int oldRotation = mRotation;
-        int rotation = mPolicy.rotationForOrientationLw(mLastOrientation, mRotation);
+        final int oldRotation = dc.getRotation();
+        final int lastOrientation = dc.getLastOrientation();
+        final boolean oldAltOrientation = dc.getAltOrientation();
+        int rotation = mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
         final boolean rotateSeamlessly;
 
         if (mPolicy.shouldRotateSeamlessly(oldRotation, rotation)) {
@@ -3956,29 +3958,30 @@
         //       an orientation that has different metrics than it expected.
         //       eg. Portrait instead of Landscape.
 
-        boolean altOrientation = !mPolicy.rotationHasCompatibleMetricsLw(
-                mLastOrientation, rotation);
+        boolean altOrientation = !mPolicy.rotationHasCompatibleMetricsLw(lastOrientation, rotation);
 
-        if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + mLastOrientation
+        if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
                 + ", got rotation " + rotation + " which has "
                 + (altOrientation ? "incompatible" : "compatible") + " metrics");
 
-        if (mRotation == rotation && mAltOrientation == altOrientation) {
+        if (oldRotation == rotation && oldAltOrientation == altOrientation) {
             // No change.
             return false;
         }
 
         if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
-                + (altOrientation ? " (alt)" : "") + " from " + mRotation
-                + (mAltOrientation ? " (alt)" : "") + ", lastOrientation=" + mLastOrientation);
+                + (altOrientation ? " (alt)" : "") + " from " + oldRotation
+                + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
 
-        if (DisplayContent.deltaRotation(rotation, mRotation) != 2) {
+        if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
             mWaitingForConfig = true;
         }
 
-        mRotation = rotation;
-        mAltOrientation = altOrientation;
-        mPolicy.setRotationLw(mRotation);
+        dc.setRotation(rotation);
+        dc.setAltOrientation(altOrientation);
+        if (dc.isDefaultDisplay) {
+            mPolicy.setRotationLw(rotation);
+        }
 
         mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
         mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
@@ -4037,7 +4040,7 @@
 
             if (rotateSeamlessly) {
                 dc.forAllWindows(w -> {
-                        w.mWinAnimator.seamlesslyRotateWindow(oldRotation, mRotation);
+                        w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
                 }, true /* traverseTopToBottom */);
             }
 
@@ -4082,16 +4085,17 @@
         // windows in final state. Otherwise, we make this call at the rotation end.
         if (screenRotationAnimation == null && mAccessibilityController != null
                 && dc.getDisplayId() == DEFAULT_DISPLAY) {
-            mAccessibilityController.onRotationChangedLocked(getDefaultDisplayContentLocked(),
-                    rotation);
+            mAccessibilityController.onRotationChangedLocked(getDefaultDisplayContentLocked());
         }
 
         return true;
     }
 
     @Override
-    public int getRotation() {
-        return mRotation;
+    public int getDefaultDisplayRotation() {
+        synchronized (mWindowMap) {
+            return getDefaultDisplayContentLocked().getRotation();
+        }
     }
 
     @Override
@@ -4128,7 +4132,8 @@
                 // Client died, no cleanup needed.
             }
 
-            return mRotation;
+            // TODO(multi-display): Modify rotation watchers to include display id.
+            return getDefaultDisplayRotation();
         }
     }
 
@@ -4170,10 +4175,9 @@
     @Override
     public int getPreferredOptionsPanelGravity() {
         synchronized (mWindowMap) {
-            final int rotation = getRotation();
-
             // TODO(multidisplay): Assume that such devices physical keys are on the main screen.
             final DisplayContent displayContent = getDefaultDisplayContentLocked();
+            final int rotation = displayContent.getRotation();
             if (displayContent.mInitialDisplayWidth < displayContent.mInitialDisplayHeight) {
                 // On devices with a natural orientation of portrait
                 switch (rotation) {
@@ -4707,9 +4711,14 @@
     private DisplayInfo updateDisplayAndOrientationLocked(int uiMode, int displayId) {
         final DisplayContent displayContent = mRoot.getDisplayContent(displayId);
 
+        // TODO(multi-display): Implement rotation for secondary displays.
+        final boolean isDefaultDisplay = displayContent.isDefaultDisplay;
+        final int displayRotation = displayContent.getRotation();
+        final boolean altDisplayOrientation = displayContent.getAltOrientation();
+
         // Use the effective "visual" dimensions based on current rotation
-        final boolean rotated = (mRotation == Surface.ROTATION_90
-                || mRotation == Surface.ROTATION_270);
+        final boolean rotated = (displayRotation == Surface.ROTATION_90
+                || displayRotation == Surface.ROTATION_270);
         final int realdw = rotated ?
                 displayContent.mBaseDisplayHeight : displayContent.mBaseDisplayWidth;
         final int realdh = rotated ?
@@ -4717,7 +4726,7 @@
         int dw = realdw;
         int dh = realdh;
 
-        if (mAltOrientation) {
+        if (altDisplayOrientation) {
             if (realdw > realdh) {
                 // Turn landscape into portrait.
                 int maxw = (int)(realdh/1.3f);
@@ -4734,18 +4743,21 @@
         }
 
         // Update application display metrics.
-        final int appWidth = mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode, displayId);
-        final int appHeight = mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
+        final int appWidth = mPolicy.getNonDecorDisplayWidth(dw, dh, displayRotation, uiMode,
+                displayId);
+        final int appHeight = mPolicy.getNonDecorDisplayHeight(dw, dh, displayRotation, uiMode,
                 displayId);
         final DisplayInfo displayInfo = displayContent.getDisplayInfo();
-        displayInfo.rotation = mRotation;
+        displayInfo.rotation = displayRotation;
         displayInfo.logicalWidth = dw;
         displayInfo.logicalHeight = dh;
         displayInfo.logicalDensityDpi = displayContent.mBaseDisplayDensity;
         displayInfo.appWidth = appWidth;
         displayInfo.appHeight = appHeight;
-        displayInfo.getLogicalMetrics(mRealDisplayMetrics,
-                CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
+        if (isDefaultDisplay) {
+            displayInfo.getLogicalMetrics(mRealDisplayMetrics,
+                    CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
+        }
         displayInfo.getAppMetrics(mDisplayMetrics);
         if (displayContent.mDisplayScalingDisabled) {
             displayInfo.flags |= Display.FLAG_SCALING_DISABLED;
@@ -4761,8 +4773,10 @@
             Slog.i(TAG_WM, "Set app display size: " + appWidth + " x " + appHeight);
         }
 
-        mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
-                mCompatDisplayMetrics);
+        if (isDefaultDisplay) {
+            mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
+                    mCompatDisplayMetrics);
+        }
         return displayInfo;
     }
 
@@ -4775,13 +4789,13 @@
         config.orientation = (dw <= dh) ? Configuration.ORIENTATION_PORTRAIT :
                 Configuration.ORIENTATION_LANDSCAPE;
         config.screenWidthDp =
-                (int)(mPolicy.getConfigDisplayWidth(dw, dh, mRotation, config.uiMode, displayId) /
-                        mDisplayMetrics.density);
+                (int)(mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation, config.uiMode,
+                        displayId) / mDisplayMetrics.density);
         config.screenHeightDp =
-                (int)(mPolicy.getConfigDisplayHeight(dw, dh, mRotation, config.uiMode, displayId) /
-                        mDisplayMetrics.density);
-        final boolean rotated = (mRotation == Surface.ROTATION_90
-                || mRotation == Surface.ROTATION_270);
+                (int)(mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation, config.uiMode,
+                        displayId) / mDisplayMetrics.density);
+        final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
+                || displayInfo.rotation == Surface.ROTATION_270);
 
         computeSizeRangesAndScreenLayout(displayInfo, displayId, rotated, config.uiMode, dw, dh,
                 mDisplayMetrics.density, config);
@@ -7002,10 +7016,14 @@
                     pw.print(" client="); pw.print(mClientFreezingScreen);
                     pw.print(" apps="); pw.print(mAppsFreezingScreen);
                     pw.print(" waitingForConfig="); pw.println(mWaitingForConfig);
-            pw.print("  mRotation="); pw.print(mRotation);
-                    pw.print(" mAltOrientation="); pw.println(mAltOrientation);
-            pw.print("  mLastWindowForcedOrientation="); pw.print(mLastWindowForcedOrientation);
-                    pw.print(" mLastOrientation="); pw.println(mLastOrientation);
+            final DisplayContent defaultDisplayContent = getDefaultDisplayContentLocked();
+            pw.print("  mRotation="); pw.print(defaultDisplayContent.getRotation());
+                    pw.print(" mAltOrientation=");
+                            pw.println(defaultDisplayContent.getAltOrientation());
+            pw.print("  mLastWindowForcedOrientation=");
+                    pw.print(defaultDisplayContent.getLastWindowForcedOrientation());
+                    pw.print(" mLastOrientation=");
+                            pw.println(defaultDisplayContent.getLastOrientation());
             pw.print("  mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
             pw.print("  Animation settings: disabled="); pw.print(mAnimationsDisabled);
                     pw.print(" window="); pw.print(mWindowAnimationScaleSetting);
diff --git a/services/core/java/com/android/server/wm/WindowState.java b/services/core/java/com/android/server/wm/WindowState.java
index 7825903..ccbc5ba 100644
--- a/services/core/java/com/android/server/wm/WindowState.java
+++ b/services/core/java/com/android/server/wm/WindowState.java
@@ -2835,7 +2835,7 @@
         // Sometimes we save surfaces due to layout invisible directly after rotation occurs.
         // However this means the surface was never laid out in the new orientation.
         // We can only restore to the last rotation we were laid out as visible in.
-        if (mLastVisibleLayoutRotation != mService.mRotation) {
+        if (mLastVisibleLayoutRotation != getDisplayContent().getRotation()) {
             destroySavedSurface();
             return false;
         }
@@ -4359,7 +4359,7 @@
             mWinAnimator.mEnterAnimationPending = true;
         }
 
-        mLastVisibleLayoutRotation = mService.mRotation;
+        mLastVisibleLayoutRotation = getDisplayContent().getRotation();
 
         mWinAnimator.mEnteringAnimation = true;
         if ((result & RELAYOUT_RES_FIRST_TIME) != 0) {