Optimize rect-shaped layers.

This brings back an optimization disabled in HC-MR1. This time the
correct geometry is generated to avoid unnecessary blending.

Change-Id: Id56404dc46bb84c75facc25c18488a690741b592
diff --git a/libs/hwui/Layer.h b/libs/hwui/Layer.h
index bb28437..26e240f 100644
--- a/libs/hwui/Layer.h
+++ b/libs/hwui/Layer.h
@@ -103,6 +103,11 @@
      * have been drawn.
      */
     Region region;
+    /**
+     * If the region is a rectangle, coordinates of the
+     * region are stored here.
+     */
+    Rect regionRect;
 
     /**
      * Color filter used to draw this layer. Optional.
diff --git a/libs/hwui/LayerRenderer.cpp b/libs/hwui/LayerRenderer.cpp
index f92e20b..ba110ec 100644
--- a/libs/hwui/LayerRenderer.cpp
+++ b/libs/hwui/LayerRenderer.cpp
@@ -92,11 +92,7 @@
 
 void LayerRenderer::generateMesh() {
 #if RENDER_LAYERS_AS_REGIONS
-#if RENDER_LAYERS_RECT_AS_RECT
     if (mLayer->region.isRect() || mLayer->region.isEmpty()) {
-#else
-    if (mLayer->region.isEmpty()) {
-#endif
         if (mLayer->mesh) {
             delete mLayer->mesh;
             delete mLayer->meshIndices;
@@ -105,6 +101,20 @@
             mLayer->meshIndices = NULL;
             mLayer->meshElementCount = 0;
         }
+
+        const android::Rect& bounds = mLayer->region.getBounds();
+        mLayer->regionRect.set(bounds.leftTop().x, bounds.leftTop().y,
+                bounds.rightBottom().x, bounds.rightBottom().y);
+
+        const float texX = 1.0f / float(mLayer->width);
+        const float texY = 1.0f / float(mLayer->height);
+        const float height = mLayer->layer.getHeight();
+        mLayer->texCoords.set(
+                mLayer->regionRect.left * texX,
+                (height - mLayer->regionRect.top) * texY,
+                mLayer->regionRect.right * texX,
+                (height - mLayer->regionRect.bottom) * texY);
+
         return;
     }
 
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index d9d7d23..bdab520 100644
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -646,13 +646,11 @@
 
 void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
 #if RENDER_LAYERS_AS_REGIONS
-#if RENDER_LAYERS_RECT_AS_RECT
     if (layer->region.isRect()) {
-        composeLayerRect(layer, rect);
+        composeLayerRect(layer, layer->regionRect);
         layer->region.clear();
         return;
     }
-#endif
 
     if (!layer->region.isEmpty()) {
         size_t count;
@@ -1658,14 +1656,9 @@
 
 #if RENDER_LAYERS_AS_REGIONS
     if (!layer->region.isEmpty()) {
-#if RENDER_LAYERS_RECT_AS_RECT
         if (layer->region.isRect()) {
-            const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
-            composeLayerRect(layer, r);
+            composeLayerRect(layer, layer->regionRect);
         } else if (layer->mesh) {
-#else
-        if (layer->mesh) {
-#endif
             const float a = alpha / 255.0f;
             const Rect& rect = layer->layer;
 
@@ -1675,13 +1668,11 @@
             setupDrawColorFilter();
             setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
             setupDrawProgram();
+            setupDrawModelViewTranslate(x, y,
+                    x + layer->layer.getWidth(), y + layer->layer.getHeight());
             setupDrawPureColorUniforms();
             setupDrawColorFilterUniforms();
             setupDrawTexture(layer->texture);
-            // TODO: The current layer, if any, will be dirtied with the bounding box
-            //       of the layer we are drawing. Since the layer we are drawing has
-            //       a mesh, we know the dirty region, we should use it instead
-            setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
             setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
 
             glDrawElements(GL_TRIANGLES, layer->meshElementCount,
diff --git a/libs/hwui/Properties.h b/libs/hwui/Properties.h
index 1aef99b..2d8b6f3 100644
--- a/libs/hwui/Properties.h
+++ b/libs/hwui/Properties.h
@@ -27,8 +27,6 @@
 
 // If turned on, layers drawn inside FBOs are optimized with regions
 #define RENDER_LAYERS_AS_REGIONS 1
-// If turned on, layers that map to a single rect are drawn as a rect
-#define RENDER_LAYERS_RECT_AS_RECT 0
 
 /**
  * Debug level for app developers.