Improve drawing cache speed by selecting the correct opacity and keeping a 32 bits
format when the window is 32 bits.

Change-Id: I46762def67fa7d6a331a75fa8660c6422394ccf2
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index 7d821b5..4c91b6b 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -6188,11 +6188,12 @@
             }
 
             final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
-            final boolean opaque = drawingCacheBackgroundColor != 0 ||
-                (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE);
+            final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
+            final boolean translucentWindow = attachInfo.mTranslucentWindow;
 
             if (width <= 0 || height <= 0 ||
-                    (width * height * (opaque ? 2 : 4) > // Projected bitmap size in bytes
+                     // Projected bitmap size in bytes
+                    (width * height * (opaque && !translucentWindow ? 2 : 4) >
                             ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
                 destroyDrawingCache();
                 return;
@@ -6203,7 +6204,6 @@
                     (mUnscaledDrawingCache == null ? null : mUnscaledDrawingCache.get());
 
             if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
-
                 Bitmap.Config quality;
                 if (!opaque) {
                     switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
@@ -6221,7 +6221,9 @@
                             break;
                     }
                 } else {
-                    quality = Bitmap.Config.RGB_565;
+                    // Optimization for translucent windows
+                    // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
+                    quality = translucentWindow ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
                 }
 
                 // Try to cleanup memory
@@ -6235,6 +6237,7 @@
                     } else {
                         mUnscaledDrawingCache = new SoftReference<Bitmap>(bitmap);
                     }
+                    if (opaque && translucentWindow) bitmap.setHasAlpha(false);
                 } catch (OutOfMemoryError e) {
                     // If there is not enough memory to create the bitmap cache, just
                     // ignore the issue as bitmap caches are not required to draw the
@@ -8821,6 +8824,11 @@
         int mWindowTop;
 
         /**
+         * Indicates whether the window is translucent/transparent
+         */
+        boolean mTranslucentWindow;        
+
+        /**
          * For windows that are full-screen but using insets to layout inside
          * of the screen decorations, these are the current insets for the
          * content of the window.
@@ -9033,8 +9041,8 @@
         public ScrollabilityCache(ViewConfiguration configuration, View host) {
             fadingEdgeLength = configuration.getScaledFadingEdgeLength();
             scrollBarSize = configuration.getScaledScrollBarSize();
-            scrollBarDefaultDelayBeforeFade = configuration.getScrollDefaultDelay();
-            scrollBarFadeDuration = configuration.getScrollBarFadeDuration();
+            scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
+            scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
 
             paint = new Paint();
             matrix = new Matrix();
diff --git a/core/java/android/view/ViewRoot.java b/core/java/android/view/ViewRoot.java
index dba2e04..a6d644b 100644
--- a/core/java/android/view/ViewRoot.java
+++ b/core/java/android/view/ViewRoot.java
@@ -408,7 +408,7 @@
                 }
 
                 boolean restore = false;
-                if (attrs != null && mTranslator != null) {
+                if (mTranslator != null) {
                     restore = true;
                     attrs.backup();
                     mTranslator.translateWindowLayout(attrs);
@@ -422,7 +422,7 @@
                 mSoftInputMode = attrs.softInputMode;
                 mWindowAttributesChanged = true;
                 mAttachInfo.mRootView = view;
-                mAttachInfo.mScalingRequired = mTranslator == null ? false : true;
+                mAttachInfo.mScalingRequired = mTranslator != null;
                 mAttachInfo.mApplicationScale =
                         mTranslator == null ? 1.0f : mTranslator.applicationScale;
                 if (panelParentView != null) {
@@ -680,6 +680,7 @@
             // object is not initialized to its backing store, but soon it
             // will be (assuming the window is visible).
             attachInfo.mSurface = mSurface;
+            attachInfo.mTranslucentWindow = lp.format != PixelFormat.OPAQUE;
             attachInfo.mHasWindowFocus = false;
             attachInfo.mWindowVisibility = viewVisibility;
             attachInfo.mRecomputeGlobalAttributes = false;