Ensure we change SurfaceView size from UI thread.

We need to change the SurfaceView size from the UI thread
so that we can appropriately deliver the SurfaceChanged
callback. We also need to not preserve geometry
in this case, as if we don't update the surface
and layout size together we could get scaling. This still has
some potential for holes, as transactions are not synced with
the parent renderer, but we have other methods to avoid
these in the case of resizing. This fixes the remaining
issues with content sizing and surface view "out of sync".

Bug: 27780983
Bug: 27687126
Bug: 27676101

Change-Id: Idd7864f00e5cf7a4eb32dd66c0b389292a788069
diff --git a/core/java/android/view/IWindowSession.aidl b/core/java/android/view/IWindowSession.aidl
index a1e2e94..8e1609c 100644
--- a/core/java/android/view/IWindowSession.aidl
+++ b/core/java/android/view/IWindowSession.aidl
@@ -116,14 +116,11 @@
      *  @param top The new top position
      *  @param right The new right position
      *  @param bottom The new bottom position
-     *  @param requestedWidth The new requested width
-     *  @param requestedHeight The new requested height
      *  @param deferTransactionUntilFrame Frame number from our parent (attached) to
      *  defer this action until.
      *  @param outFrame Rect in which is placed the new position/size on screen.
      */
     void repositionChild(IWindow childWindow, int left, int top, int right, int bottom,
-            int requestedWidth, int requestedHeight,
             long deferTransactionUntilFrame, out Rect outFrame);
 
     /*
diff --git a/core/java/android/view/SurfaceView.java b/core/java/android/view/SurfaceView.java
index 477ffd9..8a8fb43 100644
--- a/core/java/android/view/SurfaceView.java
+++ b/core/java/android/view/SurfaceView.java
@@ -490,7 +490,7 @@
                               | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                               | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                               ;
-                if (!creating && !force && !mUpdateWindowNeeded) {
+                if (!creating && !force && !mUpdateWindowNeeded && !sizeChanged) {
                     mLayout.privateFlags |=
                             WindowManager.LayoutParams.PRIVATE_FLAG_PRESERVE_GEOMETRY;
                 } else {
@@ -584,18 +584,6 @@
 
                     mSurface.transferFrom(mNewSurface);
                     if (visible && mSurface.isValid()) {
-                        // We set SCALING_MODE_NO_SCALE_CROP to allow the WindowManager
-                        // to update our Surface crop without requiring a new buffer from
-                        // us. In the default mode of SCALING_MODE_FREEZE, surface geometry
-                        // state (which includes crop) is only applied when a buffer
-                        // with appropriate geometry is available. During drag resize
-                        // it is quite frequent that a matching buffer will not be available
-                        // (because we are constantly being resized and have fallen behind).
-                        // However in such situations the WindowManager still needs to be able
-                        // to update our crop to ensure we stay within the bounds of the containing
-                        // window.
-                        mSurface.setScalingMode(Surface.SCALING_MODE_NO_SCALE_CROP);
-
                         if (!mSurfaceCreated && (surfaceChanged || visibleChanged)) {
                             mSurfaceCreated = true;
                             mIsCreating = true;
@@ -666,7 +654,6 @@
                             mLocation[0], mLocation[1]));
                     mSession.repositionChild(mWindow, mWindowSpaceLeft, mWindowSpaceTop,
                             mLocation[0], mLocation[1],
-                            mWindowSpaceWidth, mWindowSpaceHeight,
                             -1, mWinFrame);
                 } catch (RemoteException ex) {
                     Log.e(TAG, "Exception from relayout", ex);
@@ -703,7 +690,6 @@
             }
             // Just using mRTLastReportedPosition as a dummy rect here
             session.repositionChild(window, left, top, right, bottom,
-                    mWindowSpaceWidth, mWindowSpaceHeight,
                     frameNumber,
                     mRTLastReportedPosition);
             // Now overwrite mRTLastReportedPosition with our values
diff --git a/services/core/java/com/android/server/wm/Session.java b/services/core/java/com/android/server/wm/Session.java
index c0c1ed8..daeb860 100644
--- a/services/core/java/com/android/server/wm/Session.java
+++ b/services/core/java/com/android/server/wm/Session.java
@@ -195,10 +195,8 @@
 
     @Override
     public void repositionChild(IWindow window, int left, int top, int right, int bottom,
-            int requestedWidth, int requestedHeight,
             long deferTransactionUntilFrame, Rect outFrame) {
         mService.repositionChild(this, window, left, top, right, bottom,
-                requestedWidth, requestedHeight,
                 deferTransactionUntilFrame, outFrame);
     }
 
diff --git a/services/core/java/com/android/server/wm/WindowManagerService.java b/services/core/java/com/android/server/wm/WindowManagerService.java
index 5771d69..a3e7976 100644
--- a/services/core/java/com/android/server/wm/WindowManagerService.java
+++ b/services/core/java/com/android/server/wm/WindowManagerService.java
@@ -2531,7 +2531,6 @@
 
     void repositionChild(Session session, IWindow client,
             int left, int top, int right, int bottom,
-            int requestedWidth, int requestedHeight,
             long deferTransactionUntilFrame, Rect outFrame) {
         Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "repositionChild");
         long origId = Binder.clearCallingIdentity();
@@ -2547,7 +2546,6 @@
                             "repositionChild called but window is not"
                             + "attached to a parent win=" + win);
                 }
-                win.setRequestedSize(requestedWidth, requestedHeight);
 
                 win.mAttrs.x = left;
                 win.mAttrs.y = top;
diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeWindowSession.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeWindowSession.java
index 53adb41..5a6a00f 100644
--- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeWindowSession.java
+++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeWindowSession.java
@@ -97,7 +97,6 @@
 
     @Override
     public void repositionChild(IWindow window, int left, int top, int right, int bottom,
-            int requestedWidth, int requestedHeight,
             long deferTransactionUntilFrame, Rect outFrame) {
         // pass for now.
         return;