From 80c69853d64b73e5d62ed33bea1b10680557ee3e Mon Sep 17 00:00:00 2001 From: Michael Jurka Date: Fri, 16 Dec 2011 14:16:32 -0800 Subject: [PATCH] simplifying code to prevent drawing off-screen On certain graphics drivers, if you draw to a off- screen buffer that's not used, it can lead to poor performance. We were running into this in launcher; when setChildrenLayersEnabled was called on a CellLayout, that triggered a re-draw of that CellLayout's hardware layer, even if that CellLayout wasn't visible. This CL changes PagedView so that pages that aren't going to be rendered are set to View.INVISIBLE, preventing re-drawing of their hardware layer. --- src/com/android/launcher2/PagedView.java | 26 ++++++++++++----- src/com/android/launcher2/Workspace.java | 37 ++---------------------- 2 files changed, 21 insertions(+), 42 deletions(-) diff --git a/src/com/android/launcher2/PagedView.java b/src/com/android/launcher2/PagedView.java index b6efcfa79f..ef360427bc 100644 --- a/src/com/android/launcher2/PagedView.java +++ b/src/com/android/launcher2/PagedView.java @@ -727,17 +727,16 @@ public abstract class PagedView extends ViewGroup { final int pageCount = getChildCount(); if (pageCount > 0) { final int screenWidth = getMeasuredWidth(); - int x = (int) getPageAt(0).getRight(); int leftScreen = 0; int rightScreen = 0; - while (x <= mScrollX && leftScreen < pageCount - 1) { + while (leftScreen < pageCount - 1 && + getPageAt(leftScreen).getRight() <= mScrollX) { leftScreen++; - x = getPageAt(leftScreen).getRight(); } rightScreen = leftScreen; - while (x < mScrollX + screenWidth && rightScreen < pageCount - 1) { + while (rightScreen < pageCount - 1 && + getPageAt(rightScreen + 1).getLeft() < mScrollX + screenWidth) { rightScreen++; - x = (int) getPageAt(rightScreen).getRight(); } range[0] = leftScreen; range[1] = rightScreen; @@ -773,8 +772,21 @@ public abstract class PagedView extends ViewGroup { canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft, mScrollY + mBottom - mTop); - for (int i = rightScreen; i >= leftScreen; i--) { - drawChild(canvas, getPageAt(i), drawingTime); + // On certain graphics drivers, if you draw to a off-screen buffer that's not + // used, it can lead to poor performance. We were running into this when + // setChildrenLayersEnabled was called on a CellLayout; that triggered a re-draw + // of that CellLayout's hardware layer, even if that CellLayout wasn't visible. + // As a fix, below we set pages that aren't going to be rendered are to be + // View.INVISIBLE, preventing re-drawing of their hardware layer + for (int i = getChildCount() - 1; i >= 0; i--) { + final View v = getPageAt(i); + if (leftScreen <= i && i <= rightScreen && + v.getAlpha() > ViewConfiguration.ALPHA_THRESHOLD) { + v.setVisibility(VISIBLE); + drawChild(canvas, v, drawingTime); + } else { + v.setVisibility(INVISIBLE); + } } canvas.restore(); } diff --git a/src/com/android/launcher2/Workspace.java b/src/com/android/launcher2/Workspace.java index d74a2c3863..148c1ba567 100644 --- a/src/com/android/launcher2/Workspace.java +++ b/src/com/android/launcher2/Workspace.java @@ -1268,32 +1268,6 @@ public class Workspace extends SmoothPagedView return (mBackground != null && mBackgroundAlpha > 0.0f && mDrawBackground); } - 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) { - for (int i = leftScreen; i <= rightScreen; i++) { - 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); @@ -1400,13 +1374,8 @@ public class Workspace extends SmoothPagedView if (enableChildrenLayers != mChildrenLayersEnabled) { mChildrenLayersEnabled = enableChildrenLayers; - // calling setChildrenLayersEnabled on a view that's not visible/rendered - // causes slowdowns on some graphics cards, so we only disable it here and leave - // the enabling to dispatchDraw - if (!enableChildrenLayers) { - for (int i = 0; i < getPageCount(); i++) { - ((ViewGroup)getChildAt(i)).setChildrenLayersEnabled(false); - } + for (int i = 0; i < getPageCount(); i++) { + ((ViewGroup)getChildAt(i)).setChildrenLayersEnabled(mChildrenLayersEnabled); } } } @@ -1731,7 +1700,6 @@ public class Workspace extends SmoothPagedView } } } - syncChildrenLayersEnabledOnVisiblePages(); } }); @@ -1771,7 +1739,6 @@ public class Workspace extends SmoothPagedView // Fade the background gradient away animateBackgroundGradient(0f, true); } - syncChildrenLayersEnabledOnVisiblePages(); return anim; }