Potential fix for out-of-date hardware layers

Might also further help bug where IMG GPU slows
down when rendering into hardware layers that are
not rendered to framebuffer

Bugs # 5614712, 5616414, 5628998

Change-Id: I85ff11889aa28f6985dd68e7f1e57d0da2150aa5
diff --git a/src/com/android/launcher2/Workspace.java b/src/com/android/launcher2/Workspace.java
index 0598cd9..fd5a3fc 100644
--- a/src/com/android/launcher2/Workspace.java
+++ b/src/com/android/launcher2/Workspace.java
@@ -57,6 +57,7 @@
 import android.view.DragEvent;
 import android.view.MotionEvent;
 import android.view.View;
+import android.view.ViewConfiguration;
 import android.view.ViewGroup;
 import android.view.animation.DecelerateInterpolator;
 import android.widget.ImageView;
@@ -1280,21 +1281,34 @@
         return (mBackground != null && mBackgroundAlpha > 0.0f && mDrawBackground);
     }
 
-    @Override
-    protected void dispatchDraw(Canvas canvas) {
+    public void scrollTo (int x, int y) {
+        super.scrollTo(x, y);
+        syncChildrenLayersEnabledOnVisiblePages();
+    }
+
+    // This method just applies the value mChildrenLayersEnabled to all the pages that
+    // will be rendered on the next frame.
+    // We do this because calling setChildrenLayersEnabled on a view that's not
+    // visible/rendered causes slowdowns on some graphics cards
+    private void syncChildrenLayersEnabledOnVisiblePages() {
         if (mChildrenLayersEnabled) {
             getVisiblePages(mTempVisiblePagesRange);
             final int leftScreen = mTempVisiblePagesRange[0];
             final int rightScreen = mTempVisiblePagesRange[1];
             if (leftScreen != -1 && rightScreen != -1) {
-                // calling setChildrenLayersEnabled on a view that's not visible/rendered
-                // causes slowdowns on some graphics cards, so we set it here to be sure
-                // it's only called when it's safe (ie when the view will be rendered)
                 for (int i = leftScreen; i <= rightScreen; i++) {
-                    ((ViewGroup)getPageAt(i)).setChildrenLayersEnabled(true);
+                    ViewGroup page = (ViewGroup) getPageAt(i);
+                    if (page.getVisibility() == VISIBLE &&
+                            page.getAlpha() > ViewConfiguration.ALPHA_THRESHOLD) {
+                        ((ViewGroup)getPageAt(i)).setChildrenLayersEnabled(true);
+                    }
                 }
             }
         }
+    }
+
+    @Override
+    protected void dispatchDraw(Canvas canvas) {
         super.dispatchDraw(canvas);
 
         if (mInScrollArea && !LauncherApplication.isScreenLarge()) {
@@ -1723,6 +1737,7 @@
                                 b * mNewBackgroundAlphaMultipliers[i]);
                         cl.setFastAlpha(a * mOldAlphas[i] + b * mNewAlphas[i]);
                     }
+                    syncChildrenLayersEnabledOnVisiblePages();
                 }
             });
 
@@ -1760,6 +1775,7 @@
             // Fade the background gradient away
             animateBackgroundGradient(0f, true);
         }
+        syncChildrenLayersEnabledOnVisiblePages();
     }
 
     /**