Better cache for layers, reduce memory usage and increase framerate.

Change-Id: I5ff864a361db4791bd5ff6be716f7ce692ef572d
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index 5399668..62c590d 100644
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -385,8 +385,7 @@
 
     glActiveTexture(GL_TEXTURE0);
 
-    LayerSize size(bounds.getWidth(), bounds.getHeight());
-    Layer* layer = mCaches.layerCache.get(size);
+    Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
     if (!layer) {
         return false;
     }
@@ -394,6 +393,8 @@
     layer->mode = mode;
     layer->alpha = alpha;
     layer->layer.set(bounds);
+    layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
+            bounds.getWidth() / float(layer->width), 0.0f);
 
     // Save the layer in the snapshot
     snapshot->flags |= Snapshot::kFlagIsLayer;
@@ -420,7 +421,7 @@
         // Initialize the texture if needed
         if (layer->empty) {
             layer->empty = false;
-            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.width, size.height, 0,
+            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
                     GL_RGBA, GL_UNSIGNED_BYTE, NULL);
         }
 
@@ -455,12 +456,12 @@
 
         // TODO: Workaround for b/3054204
         glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, mHeight - bounds.bottom,
-                bounds.getWidth(), bounds.getHeight(), 0);
+                layer->width, layer->height, 0);
 
         // TODO: Waiting for b/3054204 to be fixed
         // if (layer->empty) {
         //     glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, mHeight - bounds.bottom,
-        //             bounds.getWidth(), bounds.getHeight(), 0);
+        //             layer->width, layer->height, 0);
         //     layer->empty = false;
         // } else {
         //     glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left, mHeight - bounds.bottom,
@@ -502,8 +503,8 @@
                 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
     }
 
-    // Layers are already drawn with a top-left origin, don't flip the texture
-    resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
+    const Rect& texCoords = layer->texCoords;
+    resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
 
     if (fboLayer) {
         drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
@@ -526,9 +527,8 @@
         mCaches.fboCache.put(current->fbo);
     }
 
-    LayerSize size(rect.getWidth(), rect.getHeight());
     // Failing to add the layer to the cache should happen only if the layer is too large
-    if (!mCaches.layerCache.put(size, layer)) {
+    if (!mCaches.layerCache.put(layer)) {
         LAYER_LOGD("Deleting layer");
         glDeleteTextures(1, &layer->texture);
         delete layer;