Better backend for hardware layers.

With this new backend, a hardware layer is only recreated when
its associated view is udpated. This offers fast composition
in GL and fast update of the layer in GL as well.

Change-Id: I97c43a612f5955c6bf1c192c8ca4af10fdf1d076
diff --git a/libs/hwui/Android.mk b/libs/hwui/Android.mk
index 29158e5..c49be93 100644
--- a/libs/hwui/Android.mk
+++ b/libs/hwui/Android.mk
@@ -13,6 +13,7 @@
 		FboCache.cpp \
 		GradientCache.cpp \
 		LayerCache.cpp \
+		LayerRenderer.cpp \
 		Matrix.cpp \
 		OpenGLDebugRenderer.cpp \
 		OpenGLRenderer.cpp \
diff --git a/libs/hwui/DisplayListRenderer.cpp b/libs/hwui/DisplayListRenderer.cpp
index afb82bf..fdb4e8c 100644
--- a/libs/hwui/DisplayListRenderer.cpp
+++ b/libs/hwui/DisplayListRenderer.cpp
@@ -241,6 +241,11 @@
                 renderer.drawDisplayList(getDisplayList());
             }
             break;
+            case DrawLayer: {
+                renderer.drawLayer(getInt(), getFloat(), getFloat(), getFloat(), getFloat(),
+                        getFloat(), getFloat(), getPaint());
+            }
+            break;
             case DrawBitmap: {
                 renderer.drawBitmap(getBitmap(), getFloat(), getFloat(), getPaint());
             }
@@ -483,6 +488,16 @@
     addDisplayList(displayList);
 }
 
+void DisplayListRenderer::drawLayer(int texture, float left, float top, float right, float bottom,
+        float u, float v, SkPaint* paint) {
+    addOp(DisplayList::DrawLayer);
+    addInt(texture);
+    addBounds(left, top, right, bottom);
+    addFloat(u);
+    addFloat(v);
+    addPaint(paint);
+}
+
 void DisplayListRenderer::drawBitmap(SkBitmap* bitmap, float left, float top,
         SkPaint* paint) {
     addOp(DisplayList::DrawBitmap);
diff --git a/libs/hwui/DisplayListRenderer.h b/libs/hwui/DisplayListRenderer.h
index fedb174..62cb0e8 100644
--- a/libs/hwui/DisplayListRenderer.h
+++ b/libs/hwui/DisplayListRenderer.h
@@ -93,6 +93,7 @@
         ConcatMatrix,
         ClipRect,
         DrawDisplayList,
+        DrawLayer,
         DrawBitmap,
         DrawBitmapMatrix,
         DrawBitmapRect,
@@ -245,6 +246,8 @@
     bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op);
 
     void drawDisplayList(DisplayList* displayList);
+    void drawLayer(int texture, float left, float top, float right, float bottom,
+            float u, float v, SkPaint* paint);
     void drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint);
     void drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint);
     void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop,
diff --git a/libs/hwui/LayerRenderer.cpp b/libs/hwui/LayerRenderer.cpp
new file mode 100644
index 0000000..3583b86
--- /dev/null
+++ b/libs/hwui/LayerRenderer.cpp
@@ -0,0 +1,121 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "OpenGLRenderer"
+
+#include "LayerRenderer.h"
+
+namespace android {
+namespace uirenderer {
+
+///////////////////////////////////////////////////////////////////////////////
+// Rendering
+///////////////////////////////////////////////////////////////////////////////
+
+void LayerRenderer::prepare(bool opaque) {
+    glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &mPreviousFbo);
+    glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
+    OpenGLRenderer::prepare(opaque);
+}
+
+void LayerRenderer::finish() {
+    OpenGLRenderer::finish();
+    glBindFramebuffer(GL_FRAMEBUFFER, mPreviousFbo);
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// Static functions
+///////////////////////////////////////////////////////////////////////////////
+
+GLuint LayerRenderer::createLayer(uint32_t width, uint32_t height,
+        uint32_t* layerWidth, uint32_t* layerHeight, GLuint* texture) {
+    GLuint previousFbo;
+    glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &previousFbo);
+
+    GLuint fbo = 0;
+    glGenFramebuffers(1, &fbo);
+    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
+
+    glActiveTexture(GL_TEXTURE0);
+    glGenTextures(1, texture);
+    glBindTexture(GL_TEXTURE_2D, *texture);
+
+    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
+
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+
+    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
+            GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+
+    if (glGetError() != GL_NO_ERROR) {
+        glDeleteBuffers(1, &fbo);
+        glDeleteTextures(1, texture);
+        glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
+        return 0;
+    }
+
+    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
+                *texture, 0);
+
+    if (glGetError() != GL_NO_ERROR) {
+        glDeleteBuffers(1, &fbo);
+        glDeleteTextures(1, texture);
+        glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
+        return 0;
+    }
+
+    glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
+
+    *layerWidth = width;
+    *layerHeight = height;
+
+    return fbo;
+}
+
+void LayerRenderer::resizeLayer(GLuint fbo, GLuint texture, uint32_t width, uint32_t height,
+        uint32_t* layerWidth, uint32_t* layerHeight) {
+    glActiveTexture(GL_TEXTURE0);
+    glBindTexture(GL_TEXTURE_2D, texture);
+
+    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
+            GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+
+    if (glGetError() != GL_NO_ERROR) {
+        glDeleteBuffers(1, &fbo);
+        glDeleteTextures(1, texture);
+        glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
+
+        *layerWidth = 0;
+        *layerHeight = 0;
+
+        return;
+    }
+
+    *layerWidth = width;
+    *layerHeight = height;
+}
+
+void LayerRenderer::destroyLayer(GLuint fbo, GLuint texture) {
+    if (fbo) glDeleteFramebuffers(1, &fbo);
+    if (texture) glDeleteTextures(1, &texture);
+}
+
+}; // namespace uirenderer
+}; // namespace android
diff --git a/libs/hwui/LayerRenderer.h b/libs/hwui/LayerRenderer.h
new file mode 100644
index 0000000..a8f1ff7
--- /dev/null
+++ b/libs/hwui/LayerRenderer.h
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_HWUI_LAYER_RENDERER_H
+#define ANDROID_HWUI_LAYER_RENDERER_H
+
+#include "OpenGLRenderer.h"
+
+namespace android {
+namespace uirenderer {
+
+///////////////////////////////////////////////////////////////////////////////
+// Renderer
+///////////////////////////////////////////////////////////////////////////////
+
+class LayerRenderer: public OpenGLRenderer {
+public:
+    LayerRenderer(GLuint fbo): mFbo(fbo) {
+    }
+
+    ~LayerRenderer() {
+    }
+
+    void prepare(bool opaque);
+    void finish();
+
+    static GLuint createLayer(uint32_t width, uint32_t height,
+            uint32_t* layerWidth, uint32_t* layerHeight, GLuint* texture);
+    static void resizeLayer(GLuint fbo, GLuint texture, uint32_t width, uint32_t height,
+            uint32_t* layerWidth, uint32_t* layerHeight);
+    static void destroyLayer(GLuint fbo, GLuint texture);
+
+private:
+    GLuint mFbo;
+    GLuint mPreviousFbo;
+
+}; // class LayerRenderer
+
+}; // namespace uirenderer
+}; // namespace android
+
+#endif // ANDROID_HWUI_LAYER_RENDERER_H
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index 99bb6f0..aefefe4 100644
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -178,7 +178,7 @@
 #endif
 }
 
-void OpenGLRenderer::acquireContext() {
+void OpenGLRenderer::interrupt() {
     if (mCaches.currentProgram) {
         if (mCaches.currentProgram->isInUse()) {
             mCaches.currentProgram->remove();
@@ -188,7 +188,11 @@
     mCaches.unbindMeshBuffer();
 }
 
-void OpenGLRenderer::releaseContext() {
+void OpenGLRenderer::acquireContext() {
+    interrupt();
+}
+
+void OpenGLRenderer::resume() {
     glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
 
     glEnable(GL_SCISSOR_TEST);
@@ -205,6 +209,10 @@
     glBlendEquation(GL_FUNC_ADD);
 }
 
+void OpenGLRenderer::releaseContext() {
+    resume();
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 // State management
 ///////////////////////////////////////////////////////////////////////////////
@@ -1477,6 +1485,30 @@
     finishDrawTexture();
 }
 
+void OpenGLRenderer::drawLayer(int texture, float left, float top, float right, float bottom,
+        float u, float v, SkPaint* paint) {
+    if (quickReject(left, top, right, bottom)) {
+        return;
+    }
+
+    glActiveTexture(gTextureUnits[0]);
+    if (!texture) return;
+
+    mCaches.unbindMeshBuffer();
+    resetDrawTextureTexCoords(0.0f, v, u, 0.0f);
+
+    int alpha;
+    SkXfermode::Mode mode;
+    getAlphaAndMode(paint, &alpha, &mode);
+
+    // TODO: Should get the blend info from the caller
+    drawTextureMesh(left, top, right, bottom, texture, alpha / 255.0f, mode, true,
+            &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
+            GL_TRIANGLE_STRIP, gMeshCount);
+
+    resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 // Shaders
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/libs/hwui/OpenGLRenderer.h b/libs/hwui/OpenGLRenderer.h
index 8f93f5b..5f45915 100644
--- a/libs/hwui/OpenGLRenderer.h
+++ b/libs/hwui/OpenGLRenderer.h
@@ -64,6 +64,10 @@
     virtual void prepare(bool opaque);
     virtual void finish();
 
+    // These two calls must not be recorded in display lists
+    void interrupt();
+    void resume();
+
     virtual void acquireContext();
     virtual void releaseContext();
 
@@ -91,6 +95,8 @@
     virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op);
 
     virtual void drawDisplayList(DisplayList* displayList);
+    virtual void drawLayer(int texture, float left, float top, float right, float bottom,
+            float u, float v, SkPaint* paint);
     virtual void drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint);
     virtual void drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint);
     virtual void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop,