Defer a couple of Surface actions for WSAnimator.

Perform the set-transparent-region-hint operation outside of the
WindowManagerService loop. This is to isolate the Surface operation
from the WindowManagerService inner loop.

Similarly, defer the setWallpaperOffset call so it's animation is not
coupled to the WindowManagerService inner loop.

Note that both operations are still being done on the
WindowManagerService thread.

Change-Id: I97f030b2a9b7cffe91c77342a299bfac6e59e9f8
diff --git a/services/java/com/android/server/wm/WindowManagerService.java b/services/java/com/android/server/wm/WindowManagerService.java
index 28f4534..a4c387c 100644
--- a/services/java/com/android/server/wm/WindowManagerService.java
+++ b/services/java/com/android/server/wm/WindowManagerService.java
@@ -1972,6 +1972,11 @@
         }
     }
 
+    // TODO(cmautner):  Move to WindowAnimator.
+    void setWallpaperOffset(final WindowStateAnimator winAnimator, final int left, final int top) {
+        mH.sendMessage(mH.obtainMessage(H.SET_WALLPAPER_OFFSET, left, top, winAnimator));
+    }
+
     void updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) {
         final int dw = mAppDisplayWidth;
         final int dh = mAppDisplayHeight;
@@ -2010,10 +2015,8 @@
                             if (SHOW_TRANSACTIONS) logSurface(wallpaper,
                                     "POS " + wallpaper.mShownFrame.left
                                     + ", " + wallpaper.mShownFrame.top, null);
-                            winAnimator.mSurfaceX = wallpaper.mShownFrame.left;
-                            winAnimator.mSurfaceY = wallpaper.mShownFrame.top;
-                            winAnimator.mSurface.setPosition(wallpaper.mShownFrame.left,
-                                    wallpaper.mShownFrame.top);
+                            setWallpaperOffset(winAnimator, (int) wallpaper.mShownFrame.left,
+                                (int) wallpaper.mShownFrame.top);
                         } catch (RuntimeException e) {
                             Slog.w(TAG, "Error positioning surface of " + wallpaper
                                     + " pos=(" + wallpaper.mShownFrame.left
@@ -2474,25 +2477,20 @@
             Slog.i(TAG, str);
         }
     }
-    
+
+    // TODO(cmautner): Move to WindowStateAnimator.
+    void setTransparentRegionHint(final WindowStateAnimator winAnimator, final Region region) {
+        mH.sendMessage(mH.obtainMessage(H.SET_TRANSPARENT_REGION,
+                new Pair<WindowStateAnimator, Region>(winAnimator, region)));
+    }
+
     void setTransparentRegionWindow(Session session, IWindow client, Region region) {
         long origId = Binder.clearCallingIdentity();
         try {
             synchronized (mWindowMap) {
                 WindowState w = windowForClientLocked(session, client, false);
                 if ((w != null) && w.mHasSurface) {
-                    if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
-                            ">>> OPEN TRANSACTION setTransparentRegion");
-                    Surface.openTransaction();
-                    try {
-                        if (SHOW_TRANSACTIONS) logSurface(w,
-                                "transparentRegionHint=" + region, null);
-                        w.mWinAnimator.mSurface.setTransparentRegionHint(region);
-                    } finally {
-                        Surface.closeTransaction();
-                        if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
-                                "<<< CLOSE TRANSACTION setTransparentRegion");
-                    }
+                    setTransparentRegionHint(w.mWinAnimator, region);
                 }
             }
         } finally {
@@ -2615,6 +2613,7 @@
         long origId = Binder.clearCallingIdentity();
 
         synchronized(mWindowMap) {
+            // TODO(cmautner): synchronize on mAnimator or win.mWinAnimator.
             WindowState win = windowForClientLocked(session, client, false);
             if (win == null) {
                 return 0;
@@ -6656,6 +6655,10 @@
         public static final int WAITING_FOR_DRAWN_TIMEOUT = 24;
         public static final int BULK_UPDATE_PARAMETERS = 25;
 
+        public static final int ANIMATOR_WHAT_OFFSET = 100000;
+        public static final int SET_TRANSPARENT_REGION = ANIMATOR_WHAT_OFFSET + 1;
+        public static final int SET_WALLPAPER_OFFSET = ANIMATOR_WHAT_OFFSET + 2;
+
         private Session mLastReportedHold;
 
         public H() {
@@ -7079,6 +7082,28 @@
 
                         requestTraversalLocked();
                     }
+                    break;
+                }
+
+                // Animation messages. Move to Window{State}Animator
+                case SET_TRANSPARENT_REGION: {
+                    // TODO(cmautner): Remove sync.
+                    synchronized (mWindowMap) {
+                        Pair<WindowStateAnimator, Region> pair =
+                                (Pair<WindowStateAnimator, Region>) msg.obj;
+                        final WindowStateAnimator winAnimator = pair.first;
+                        winAnimator.setTransparentRegionHint(pair.second);
+                    }
+                    break;
+                }
+
+                case SET_WALLPAPER_OFFSET: {
+                    // TODO(cmautner): Remove sync.
+                    synchronized (mWindowMap) {
+                        final WindowStateAnimator winAnimator = (WindowStateAnimator) msg.obj;
+                        winAnimator.setWallpaperOffset(msg.arg1, msg.arg2);
+                    }
+                    break;
                 }
             }
         }
diff --git a/services/java/com/android/server/wm/WindowStateAnimator.java b/services/java/com/android/server/wm/WindowStateAnimator.java
index e6a2481..8066e00 100644
--- a/services/java/com/android/server/wm/WindowStateAnimator.java
+++ b/services/java/com/android/server/wm/WindowStateAnimator.java
@@ -9,6 +9,7 @@
 import android.graphics.Matrix;
 import android.graphics.PixelFormat;
 import android.graphics.Rect;
+import android.graphics.Region;
 import android.os.RemoteException;
 import android.util.Slog;
 import android.view.Surface;
@@ -357,7 +358,7 @@
 
     boolean finishDrawingLocked() {
         if (mDrawPending) {
-            if (SHOW_TRANSACTIONS || WindowManagerService.DEBUG_ORIENTATION) Slog.v(
+            if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Slog.v(
                 TAG, "finishDrawingLocked: " + this + " in " + mSurface);
             mCommitDrawPending = true;
             mDrawPending = false;
@@ -386,7 +387,7 @@
         if (mSurface == null) {
             mReportDestroySurface = false;
             mSurfacePendingDestroy = false;
-            if (WindowManagerService.DEBUG_ORIENTATION) Slog.i(TAG,
+            if (DEBUG_ORIENTATION) Slog.i(TAG,
                     "createSurface " + this + ": DRAW NOW PENDING");
             mDrawPending = true;
             mCommitDrawPending = false;
@@ -923,6 +924,34 @@
         }
     }
 
+    void setTransparentRegionHint(final Region region) {
+        if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
+            ">>> OPEN TRANSACTION setTransparentRegion");
+        Surface.openTransaction();
+        try {
+            if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin,
+                    "transparentRegionHint=" + region, null);
+            mSurface.setTransparentRegionHint(region);
+        } finally {
+            Surface.closeTransaction();
+            if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
+                    "<<< CLOSE TRANSACTION setTransparentRegion");
+        }
+    }
+
+    void setWallpaperOffset(int left, int top) {
+        Surface.openTransaction();
+        try {
+            mSurfaceX = left;
+            mSurfaceY = top;
+            mSurface.setPosition(left, top);
+        } catch (RuntimeException e) {
+            Slog.w(TAG, "Error positioning surface of " + mWin
+                    + " pos=(" + left + "," + top + ")", e);
+        }
+        Surface.closeTransaction();
+    }
+
     // This must be called while inside a transaction.
     boolean performShowLocked() {
         if (DEBUG_VISIBILITY) {
@@ -1053,6 +1082,7 @@
         applyAnimationLocked(transit, true);
     }
 
+    // TODO(cmautner): Move back to WindowState?
     /**
      * Choose the correct animation and set it to the passed WindowState.
      * @param transit If WindowManagerPolicy.TRANSIT_PREVIEW_DONE and the app window has been drawn