Separate layout ops from surface ops.

Further work to isolate layout from animation and surface operations.
Remove cruft and minor refactoring.

Change-Id: I6f910ed72c7c614996641c353870c2b2ab5e8bb4
diff --git a/services/java/com/android/server/wm/ScreenRotationAnimation.java b/services/java/com/android/server/wm/ScreenRotationAnimation.java
index 58187b6..ab084f9 100644
--- a/services/java/com/android/server/wm/ScreenRotationAnimation.java
+++ b/services/java/com/android/server/wm/ScreenRotationAnimation.java
@@ -41,7 +41,6 @@
     BlackFrame mBlackFrame;
     int mWidth, mHeight;
 
-    int mSnapshotRotation;
     int mSnapshotDeltaRotation;
     int mOriginalRotation;
     int mOriginalWidth, mOriginalHeight;
@@ -125,8 +124,7 @@
         if (mBlackFrame != null) {
             mBlackFrame.printTo(prefix + "  ", pw);
         }
-        pw.print(prefix); pw.print("mSnapshotRotation="); pw.print(mSnapshotRotation);
-                pw.print(" mSnapshotDeltaRotation="); pw.print(mSnapshotDeltaRotation);
+        pw.print(prefix); pw.print(" mSnapshotDeltaRotation="); pw.print(mSnapshotDeltaRotation);
                 pw.print(" mCurRotation="); pw.println(mCurRotation);
         pw.print(prefix); pw.print("mOriginalRotation="); pw.print(mOriginalRotation);
                 pw.print(" mOriginalWidth="); pw.print(mOriginalWidth);
@@ -173,7 +171,6 @@
         mContext = context;
 
         // Screenshot does NOT include rotation!
-        mSnapshotRotation = 0;
         if (originalRotation == Surface.ROTATION_90
                 || originalRotation == Surface.ROTATION_270) {
             mWidth = originalHeight;
@@ -197,7 +194,7 @@
             try {
                 mSurface = new Surface(session, 0, "FreezeSurface",
                         -1, mWidth, mHeight, PixelFormat.OPAQUE, Surface.FX_SURFACE_SCREENSHOT | Surface.HIDDEN);
-                if (mSurface == null || !mSurface.isValid()) {
+                if (!mSurface.isValid()) {
                     // Screenshot failed, punt.
                     mSurface = null;
                     return;
@@ -281,7 +278,7 @@
         // Compute the transformation matrix that must be applied
         // to the snapshot to make it stay in the same original position
         // with the current screen rotation.
-        int delta = deltaRotation(rotation, mSnapshotRotation);
+        int delta = deltaRotation(rotation, Surface.ROTATION_0);
         createRotationMatrix(delta, mWidth, mHeight, mSnapshotInitialMatrix);
 
         if (DEBUG_STATE) Slog.v(TAG, "**** ROTATION: " + delta);
@@ -703,20 +700,18 @@
     }
 
     void updateSurfaces() {
-        if (!mMoreStartExit && !mMoreFinishExit && !mMoreRotateExit) {
-            if (mSurface != null) {
+        if (mSurface != null) {
+            if (!mMoreStartExit && !mMoreFinishExit && !mMoreRotateExit) {
                 if (DEBUG_STATE) Slog.v(TAG, "Exit animations done, hiding screenshot surface");
                 mSurface.hide();
             }
         }
 
-        if (!mMoreStartFrame && !mMoreFinishFrame && !mMoreRotateFrame) {
-            if (mBlackFrame != null) {
+        if (mBlackFrame != null) {
+            if (!mMoreStartFrame && !mMoreFinishFrame && !mMoreRotateFrame) {
                 if (DEBUG_STATE) Slog.v(TAG, "Frame animations done, hiding black frame");
                 mBlackFrame.hide();
-            }
-        } else {
-            if (mBlackFrame != null) {
+            } else {
                 mBlackFrame.setMatrix(mFrameTransformation.getMatrix());
             }
         }
diff --git a/services/java/com/android/server/wm/WindowManagerService.java b/services/java/com/android/server/wm/WindowManagerService.java
index 4f55217..f4c4069 100644
--- a/services/java/com/android/server/wm/WindowManagerService.java
+++ b/services/java/com/android/server/wm/WindowManagerService.java
@@ -1581,8 +1581,7 @@
                     + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
                     + " commitdrawpending=" + w.mCommitDrawPending);
             if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
-                    && (mWallpaperTarget == w
-                            || (!w.mDrawPending && !w.mCommitDrawPending))) {
+                    && (mWallpaperTarget == w || w.isDrawnLw())) {
                 if (DEBUG_WALLPAPER) Slog.v(TAG,
                         "Found wallpaper activity: #" + i + "=" + w);
                 foundW = w;
@@ -2688,8 +2687,7 @@
                     win.mEnterAnimationPending = true;
                 }
                 if (displayed) {
-                    if (win.mSurface != null && !win.mDrawPending
-                            && !win.mCommitDrawPending && !mDisplayFrozen
+                    if (win.isDrawnLw() && !mDisplayFrozen
                             && mDisplayEnabled && mPolicy.isScreenOnFully()) {
                         applyEnterAnimationLocked(win);
                     }
@@ -3983,8 +3981,7 @@
                 // If we are being set visible, and the starting window is
                 // not yet displayed, then make sure it doesn't get displayed.
                 WindowState swin = wtoken.startingWindow;
-                if (swin != null && (swin.mDrawPending
-                        || swin.mCommitDrawPending)) {
+                if (swin != null && !swin.isDrawnLw()) {
                     swin.mPolicyVisibility = false;
                     swin.mPolicyVisibilityAfterAnim = false;
                  }
@@ -7669,21 +7666,76 @@
             }
         }
 
-        if (mScreenRotationAnimation != null) {
-            if (mScreenRotationAnimation.isAnimating() ||
-                    mScreenRotationAnimation.mFinishAnimReady) {
-                if (mScreenRotationAnimation.stepAnimationLocked(currentTime)) {
-                    mInnerFields.mUpdateRotation = false;
-                    mInnerFields.mAnimating = true;
-                } else {
-                    mInnerFields.mUpdateRotation = true;
-                    mScreenRotationAnimation.kill();
-                    mScreenRotationAnimation = null;
-                }
+        if (mScreenRotationAnimation != null &&
+                (mScreenRotationAnimation.isAnimating() ||
+                        mScreenRotationAnimation.mFinishAnimReady)) {
+            if (mScreenRotationAnimation.stepAnimationLocked(currentTime)) {
+                mInnerFields.mUpdateRotation = false;
+                mInnerFields.mAnimating = true;
+            } else {
+                mInnerFields.mUpdateRotation = true;
+                mScreenRotationAnimation.kill();
+                mScreenRotationAnimation = null;
             }
         }
     }
 
+    private void animateAndUpdateSurfaces(final long currentTime, final int dw, final int dh,
+                                          final int innerDw, final int innerDh, 
+                                          final boolean recoveringMemory) {
+        // Update animations of all applications, including those
+        // associated with exiting/removed apps
+        Surface.openTransaction();
+
+        try {
+            mPendingLayoutChanges = performAnimationsLocked(currentTime, dw, dh,
+                    innerDw, innerDh);
+            updateWindowsAppsAndRotationAnimationsLocked(currentTime, innerDw, innerDh);
+        
+            // THIRD LOOP: Update the surfaces of all windows.
+            
+            if (mScreenRotationAnimation != null) {
+                mScreenRotationAnimation.updateSurfaces();
+            }
+        
+            final int N = mWindows.size();
+            for (int i=N-1; i>=0; i--) {
+                WindowState w = mWindows.get(i);
+                prepareSurfaceLocked(w, recoveringMemory);
+            }
+        
+            if (mDimAnimator != null && mDimAnimator.mDimShown) {
+                mInnerFields.mAnimating |=
+                        mDimAnimator.updateSurface(mInnerFields.mDimming, currentTime,
+                            mDisplayFrozen || !mDisplayEnabled || !mPolicy.isScreenOnFully());
+            }
+        
+            if (!mInnerFields.mBlurring && mBlurShown) {
+                if (SHOW_TRANSACTIONS) Slog.i(TAG, "  BLUR " + mBlurSurface
+                        + ": HIDE");
+                try {
+                    mBlurSurface.hide();
+                } catch (IllegalArgumentException e) {
+                    Slog.w(TAG, "Illegal argument exception hiding blur surface");
+                }
+                mBlurShown = false;
+            }
+        
+            if (mBlackFrame != null) {
+                if (mScreenRotationAnimation != null) {
+                    mBlackFrame.setMatrix(
+                            mScreenRotationAnimation.getEnterTransformation().getMatrix());
+                } else {
+                    mBlackFrame.clearMatrix();
+                }
+            }
+        } catch (RuntimeException e) {
+            Log.wtf(TAG, "Unhandled exception in Window Manager", e);
+        } finally {
+            Surface.closeTransaction();
+        }
+    }
+    
     /**
      * Extracted from {@link #performLayoutAndPlaceSurfacesLockedInner} to reduce size of method.
      *
@@ -8357,7 +8409,7 @@
                     mResizingWindows.add(w);
                 }
             } else if (w.mOrientationChanging) {
-                if (!w.mDrawPending && !w.mCommitDrawPending) {
+                if (w.isDrawnLw()) {
                     if (DEBUG_ORIENTATION) Slog.v(TAG,
                             "Orientation not waiting for draw in "
                             + w + ", surface " + w.mSurface);
@@ -8388,6 +8440,16 @@
         // cases while they are hidden such as when first showing a
         // window.
         
+        if (w.mSurface == null) {
+            if (w.mOrientationChanging) {
+                if (DEBUG_ORIENTATION) {
+                    Slog.v(TAG, "Orientation change skips hidden " + w);
+                }
+                w.mOrientationChanging = false;
+            }
+            return;
+        }
+        
         boolean displayed = false;
 
         w.computeShownFrameLocked();
@@ -8521,8 +8583,7 @@
                 }
             }
 
-            if (w.mLastHidden && !w.mDrawPending
-                    && !w.mCommitDrawPending
+            if (w.mLastHidden && w.isDrawnLw()
                     && !w.mReadyToShow) {
                 if (SHOW_TRANSACTIONS) logSurface(w,
                         "SHOW (performLayout)", null);
@@ -8544,7 +8605,7 @@
 
         if (displayed) {
             if (w.mOrientationChanging) {
-                if (w.mDrawPending || w.mCommitDrawPending) {
+                if (!w.isDrawnLw()) {
                     mInnerFields.mOrientationChangeComplete = false;
                     if (DEBUG_ORIENTATION) Slog.v(TAG,
                             "Orientation continue waiting for draw in " + w);
@@ -8832,48 +8893,20 @@
                 
             } while (mPendingLayoutChanges != 0);
 
-            // Update animations of all applications, including those
-            // associated with exiting/removed apps
-
-            mPendingLayoutChanges = performAnimationsLocked(currentTime, dw, dh,
-                    innerDw, innerDh);
-            updateWindowsAppsAndRotationAnimationsLocked(currentTime, innerDw, innerDh);
-
-            // THIRD LOOP: Update the surfaces of all windows.
-
-            final boolean someoneLosingFocus = mLosingFocus.size() != 0;
+            final boolean someoneLosingFocus = !mLosingFocus.isEmpty();
 
             mInnerFields.mObscured = false;
             mInnerFields.mBlurring = false;
             mInnerFields.mDimming = false;
             mInnerFields.mSyswin = false;
-
-            if (mScreenRotationAnimation != null) {
-                mScreenRotationAnimation.updateSurfaces();
-            }
-
+            
             final int N = mWindows.size();
-
             for (i=N-1; i>=0; i--) {
                 WindowState w = mWindows.get(i);
+                //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
+                w.mContentChanged = false;
 
-                if (w.mSurface != null) {
-                    prepareSurfaceLocked(w, recoveringMemory);
-                } else if (w.mOrientationChanging) {
-                    if (DEBUG_ORIENTATION) {
-                        Slog.v(TAG, "Orientation change skips hidden " + w);
-                    }
-                    w.mOrientationChanging = false;
-                }
-
-                if (w.mContentChanged) {
-                    //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
-                    w.mContentChanged = false;
-                }
-
-                final boolean canBeSeen = w.isDisplayedLw();
-
-                if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
+                if (someoneLosingFocus && w == mCurrentFocus && w.isDisplayedLw()) {
                     focusDisplayed = true;
                 }
 
@@ -8892,37 +8925,15 @@
                     updateWallpaperVisibilityLocked();
                 }
             }
-
-            if (mDimAnimator != null && mDimAnimator.mDimShown) {
-                mInnerFields.mAnimating |=
-                        mDimAnimator.updateSurface(mInnerFields.mDimming, currentTime,
-                            mDisplayFrozen || !mDisplayEnabled || !mPolicy.isScreenOnFully());
-            }
-
-            if (!mInnerFields.mBlurring && mBlurShown) {
-                if (SHOW_TRANSACTIONS) Slog.i(TAG, "  BLUR " + mBlurSurface
-                        + ": HIDE");
-                try {
-                    mBlurSurface.hide();
-                } catch (IllegalArgumentException e) {
-                    Slog.w(TAG, "Illegal argument exception hiding blur surface");
-                }
-                mBlurShown = false;
-            }
-
-            if (mBlackFrame != null) {
-                if (mScreenRotationAnimation != null) {
-                    mBlackFrame.setMatrix(
-                            mScreenRotationAnimation.getEnterTransformation().getMatrix());
-                } else {
-                    mBlackFrame.clearMatrix();
-                }
-            }
         } catch (RuntimeException e) {
             Log.wtf(TAG, "Unhandled exception in Window Manager", e);
+        } finally {
+            Surface.closeTransaction();
         }
 
-        Surface.closeTransaction();
+        // Update animations of all applications, including those
+        // associated with exiting/removed apps
+        animateAndUpdateSurfaces(currentTime, dw, dh, innerDw, innerDh, recoveringMemory);
 
         if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
                 "<<< CLOSE TRANSACTION performLayoutAndPlaceSurfaces");
diff --git a/services/java/com/android/server/wm/WindowState.java b/services/java/com/android/server/wm/WindowState.java
index 48788e7..42ce291 100644
--- a/services/java/com/android/server/wm/WindowState.java
+++ b/services/java/com/android/server/wm/WindowState.java
@@ -1018,7 +1018,7 @@
         if (!mService.mDisplayFrozen && mService.mPolicy.isScreenOnFully()) {
             // We will run animations as long as the display isn't frozen.
 
-            if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
+            if (isDrawnLw() && mAnimation != null) {
                 mHasTransformation = true;
                 mHasLocalTransformation = true;
                 if (!mLocalAnimating) {
@@ -1478,8 +1478,7 @@
      */
     public boolean isDisplayedLw() {
         final AppWindowToken atoken = mAppToken;
-        return mSurface != null && mPolicyVisibility && !mDestroying
-            && !mDrawPending && !mCommitDrawPending
+        return isDrawnLw() && mPolicyVisibility
             && ((!mAttachedHidden &&
                     (atoken == null || !atoken.hiddenRequested))
                     || mAnimating);
@@ -1500,7 +1499,6 @@
      * complete UI in to.
      */
     public boolean isDrawnLw() {
-        final AppWindowToken atoken = mAppToken;
         return mSurface != null && !mDestroying
             && !mDrawPending && !mCommitDrawPending;
     }
@@ -1512,9 +1510,8 @@
     boolean isOpaqueDrawn() {
         return (mAttrs.format == PixelFormat.OPAQUE
                         || mAttrs.type == TYPE_WALLPAPER)
-                && mSurface != null && mAnimation == null
-                && (mAppToken == null || mAppToken.animation == null)
-                && !mDrawPending && !mCommitDrawPending;
+                && isDrawnLw() && mAnimation == null
+                && (mAppToken == null || mAppToken.animation == null);
     }
 
     /**