Fix issue #5384631: hw windows not resizing correctly

When the SystemUi becomes visible, the activity window resizes.
The hardware renderer was not begin resized to suit, so it was drawing
to a surface larger than that of the activity window, and some of the
rendering (like the action bar) appeared off the screen.

The fix is to keep track of the surface size in HardwareRenderer and to
recreate the surface when the size changes.

This change also removes the BUFFER_CHANGE flag from WindowManager.LayoutParams.
The only reason the flag existed was to trigger a hardware surface recreation,
but checking the old/new size is a more direct way of handling this.

Change-Id: I9d6bf6385794886d1d93c60609c170864cdcdfab
diff --git a/core/java/android/view/HardwareRenderer.java b/core/java/android/view/HardwareRenderer.java
index 23d1b0f..b86d21d 100644
--- a/core/java/android/view/HardwareRenderer.java
+++ b/core/java/android/view/HardwareRenderer.java
@@ -184,6 +184,22 @@
     abstract void setup(int width, int height);
 
     /**
+     * Gets the current width of the surface. This is the width that the surface
+     * was last set to in a call to {@link #setup(int, int)}.
+     *
+     * @return the current width of the surface
+     */
+    abstract int getWidth();
+
+    /**
+     * Gets the current height of the surface. This is the height that the surface
+     * was last set to in a call to {@link #setup(int, int)}.
+     *
+     * @return the current width of the surface
+     */
+    abstract int getHeight();
+
+    /**
      * Interface used to receive callbacks whenever a view is drawn by
      * a hardware renderer instance.
      */
@@ -362,6 +378,7 @@
         static EGLDisplay sEglDisplay;
         static EGLConfig sEglConfig;
         static final Object[] sEglLock = new Object[0];
+        int mWidth = -1, mHeight = -1;
 
         static final ThreadLocal<EGLContext> sEglContextStorage = new ThreadLocal<EGLContext>();
 
@@ -714,9 +731,21 @@
         void setup(int width, int height) {
             if (validate()) {
                 mCanvas.setViewport(width, height);
+                mWidth = width;
+                mHeight = height;
             }
         }
 
+        @Override
+        int getWidth() {
+            return mWidth;
+        }
+
+        @Override
+        int getHeight() {
+            return mHeight;
+        }
+
         boolean canDraw() {
             return mGl != null && mCanvas != null;
         }        
diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java
index e7c91f9..615a5f6 100644
--- a/core/java/android/view/ViewRootImpl.java
+++ b/core/java/android/view/ViewRootImpl.java
@@ -860,7 +860,6 @@
         CompatibilityInfo compatibilityInfo = mCompatibilityInfo.get();
         if (compatibilityInfo.supportsScreen() == mLastInCompatMode) {
             params = lp;
-            windowAttributesChanges |= WindowManager.LayoutParams.BUFFER_CHANGED;
             fullRedrawNeeded = true;
             mLayoutRequested = true;
             if (mLastInCompatMode) {
@@ -1078,7 +1077,6 @@
                             ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) |
                             resizeMode;
                     params = lp;
-                    windowAttributesChanges |= WindowManager.LayoutParams.BUFFER_CHANGED;
                 }
             }
         }
@@ -1375,13 +1373,15 @@
                 }
             }
 
-            if (hwInitialized || ((windowShouldResize || (params != null &&
-                    (windowAttributesChanges & WindowManager.LayoutParams.BUFFER_CHANGED) != 0)) &&
-                    mAttachInfo.mHardwareRenderer != null &&
-                    mAttachInfo.mHardwareRenderer.isEnabled())) {
-                mAttachInfo.mHardwareRenderer.setup(mWidth, mHeight);
-                if (!hwInitialized && mAttachInfo.mHardwareRenderer.isEnabled()) {
-                    mAttachInfo.mHardwareRenderer.invalidate(mHolder);
+            if (mAttachInfo.mHardwareRenderer != null &&
+                    mAttachInfo.mHardwareRenderer.isEnabled()) {
+                if (hwInitialized || windowShouldResize ||
+                        mWidth != mAttachInfo.mHardwareRenderer.getWidth() ||
+                        mHeight != mAttachInfo.mHardwareRenderer.getHeight()) {
+                    mAttachInfo.mHardwareRenderer.setup(mWidth, mHeight);
+                    if (!hwInitialized) {
+                        mAttachInfo.mHardwareRenderer.invalidate(mHolder);
+                    }
                 }
             }
 
diff --git a/core/java/android/view/WindowManager.java b/core/java/android/view/WindowManager.java
index 17a516c..99acb3f 100644
--- a/core/java/android/view/WindowManager.java
+++ b/core/java/android/view/WindowManager.java
@@ -1260,8 +1260,6 @@
         /** {@hide} */
         public static final int PRIVATE_FLAGS_CHANGED = 1<<16;
         /** {@hide} */
-        public static final int BUFFER_CHANGED = 1<<17;
-        /** {@hide} */
         public static final int EVERYTHING_CHANGED = 0xffffffff;
 
         // internal buffer to backup/restore parameters under compatibility mode.
@@ -1272,11 +1270,11 @@
     
             if (width != o.width) {
                 width = o.width;
-                changes |= LAYOUT_CHANGED | BUFFER_CHANGED;
+                changes |= LAYOUT_CHANGED;
             }
             if (height != o.height) {
                 height = o.height;
-                changes |= LAYOUT_CHANGED | BUFFER_CHANGED;
+                changes |= LAYOUT_CHANGED;
             }
             if (x != o.x) {
                 x = o.x;
@@ -1288,19 +1286,19 @@
             }
             if (horizontalWeight != o.horizontalWeight) {
                 horizontalWeight = o.horizontalWeight;
-                changes |= LAYOUT_CHANGED | BUFFER_CHANGED;
+                changes |= LAYOUT_CHANGED;
             }
             if (verticalWeight != o.verticalWeight) {
                 verticalWeight = o.verticalWeight;
-                changes |= LAYOUT_CHANGED | BUFFER_CHANGED;
+                changes |= LAYOUT_CHANGED;
             }
             if (horizontalMargin != o.horizontalMargin) {
                 horizontalMargin = o.horizontalMargin;
-                changes |= LAYOUT_CHANGED | BUFFER_CHANGED;
+                changes |= LAYOUT_CHANGED;
             }
             if (verticalMargin != o.verticalMargin) {
                 verticalMargin = o.verticalMargin;
-                changes |= LAYOUT_CHANGED | BUFFER_CHANGED;
+                changes |= LAYOUT_CHANGED;
             }
             if (type != o.type) {
                 type = o.type;
@@ -1308,7 +1306,7 @@
             }
             if (flags != o.flags) {
                 flags = o.flags;
-                changes |= FLAGS_CHANGED | BUFFER_CHANGED;
+                changes |= FLAGS_CHANGED;
             }
             if (privateFlags != o.privateFlags) {
                 privateFlags = o.privateFlags;
@@ -1320,11 +1318,11 @@
             }
             if (gravity != o.gravity) {
                 gravity = o.gravity;
-                changes |= LAYOUT_CHANGED | BUFFER_CHANGED;
+                changes |= LAYOUT_CHANGED;
             }
             if (format != o.format) {
                 format = o.format;
-                changes |= FORMAT_CHANGED | BUFFER_CHANGED;
+                changes |= FORMAT_CHANGED;
             }
             if (windowAnimations != o.windowAnimations) {
                 windowAnimations = o.windowAnimations;
@@ -1363,7 +1361,7 @@
     
             if (screenOrientation != o.screenOrientation) {
                 screenOrientation = o.screenOrientation;
-                changes |= SCREEN_ORIENTATION_CHANGED | BUFFER_CHANGED;
+                changes |= SCREEN_ORIENTATION_CHANGED;
             }
 
             if (systemUiVisibility != o.systemUiVisibility