Allows to render with an OpenGL context inside a TextureView.

Change-Id: I59453f7fc3997f0502a1c5d325d37fed376fabc7
diff --git a/libs/hwui/Layer.h b/libs/hwui/Layer.h
index 16566b8..0310bc3 100644
--- a/libs/hwui/Layer.h
+++ b/libs/hwui/Layer.h
@@ -47,6 +47,7 @@
         meshElementCount = 0;
         isCacheable = true;
         isTextureLayer = false;
+        renderTarget = GL_TEXTURE_2D;
     }
 
     ~Layer() {
@@ -155,6 +156,11 @@
      * Optional texture coordinates transform.
      */
     mat4 texTransform;
+
+    /**
+     * Indicates the render target.
+     */
+    GLenum renderTarget;
 }; // struct Layer
 
 }; // namespace uirenderer
diff --git a/libs/hwui/LayerRenderer.cpp b/libs/hwui/LayerRenderer.cpp
index e167336..f316ba7 100644
--- a/libs/hwui/LayerRenderer.cpp
+++ b/libs/hwui/LayerRenderer.cpp
@@ -185,17 +185,7 @@
     layer->region.clear();
 
     glActiveTexture(GL_TEXTURE0);
-
     glGenTextures(1, &layer->texture);
-    glBindTexture(GL_TEXTURE_EXTERNAL_OES, layer->texture);
-
-    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
-
-    glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-    glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-
-    glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
-    glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 
     return layer;
 }
@@ -280,7 +270,7 @@
 }
 
 void LayerRenderer::updateTextureLayer(Layer* layer, uint32_t width, uint32_t height,
-        float* transform) {
+        GLenum renderTarget, float* transform) {
     if (layer) {
         layer->width = width;
         layer->height = height;
@@ -288,6 +278,15 @@
         layer->region.set(width, height);
         layer->regionRect.set(0.0f, 0.0f, width, height);
         layer->texTransform.load(transform);
+        layer->renderTarget = renderTarget;
+
+        glBindTexture(layer->renderTarget, layer->texture);
+
+        glTexParameteri(layer->renderTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+        glTexParameteri(layer->renderTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+
+        glTexParameteri(layer->renderTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+        glTexParameteri(layer->renderTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
     }
 }
 
diff --git a/libs/hwui/LayerRenderer.h b/libs/hwui/LayerRenderer.h
index b3cd5db..59cab96 100644
--- a/libs/hwui/LayerRenderer.h
+++ b/libs/hwui/LayerRenderer.h
@@ -57,7 +57,7 @@
     static Layer* createLayer(uint32_t width, uint32_t height, bool isOpaque = false);
     static bool resizeLayer(Layer* layer, uint32_t width, uint32_t height);
     static void updateTextureLayer(Layer* layer, uint32_t width, uint32_t height,
-            float* transform);
+            GLenum renderTarget, float* transform);
     static void destroyLayer(Layer* layer);
     static void destroyLayerDeferred(Layer* layer);
 
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index 34d8fd3..71976cd 100644
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -637,7 +637,12 @@
     float alpha = layer->alpha / 255.0f;
 
     setupDraw();
-    setupDrawWithExternalTexture();
+    if (layer->renderTarget == GL_TEXTURE_2D) {
+        setupDrawWithTexture();
+    } else {
+        setupDrawWithExternalTexture();
+    }
+    setupDrawTextureTransform();
     setupDrawColor(alpha, alpha, alpha, alpha);
     setupDrawColorFilter();
     setupDrawBlending(layer->blend, layer->mode);
@@ -645,8 +650,12 @@
     setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
     setupDrawPureColorUniforms();
     setupDrawColorFilterUniforms();
-    setupDrawExternalTexture(layer->texture);
-    setupDrawTextureTransform(layer->texTransform);
+    if (layer->renderTarget == GL_TEXTURE_2D) {
+        setupDrawTexture(layer->texture);
+    } else {
+        setupDrawExternalTexture(layer->texture);
+    }
+    setupDrawTextureTransformUniforms(layer->texTransform);
     setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
 
     glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
@@ -1095,7 +1104,11 @@
     glEnableVertexAttribArray(mTexCoordsSlot);
 }
 
-void OpenGLRenderer::setupDrawTextureTransform(mat4& transform) {
+void OpenGLRenderer::setupDrawTextureTransform() {
+    mDescription.hasTextureTransform = true;
+}
+
+void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
     glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
             GL_FALSE, &transform.data[0]);
 }
diff --git a/libs/hwui/OpenGLRenderer.h b/libs/hwui/OpenGLRenderer.h
index 0ffd70b..22309f8 100644
--- a/libs/hwui/OpenGLRenderer.h
+++ b/libs/hwui/OpenGLRenderer.h
@@ -465,7 +465,8 @@
     void setupDrawSimpleMesh();
     void setupDrawTexture(GLuint texture);
     void setupDrawExternalTexture(GLuint texture);
-    void setupDrawTextureTransform(mat4& transform);
+    void setupDrawTextureTransform();
+    void setupDrawTextureTransformUniforms(mat4& transform);
     void setupDrawMesh(GLvoid* vertices, GLvoid* texCoords = NULL, GLuint vbo = 0);
     void setupDrawVertices(GLvoid* vertices);
     void setupDrawAALine(GLvoid* vertices, GLvoid* distanceCoords, float strokeWidth);
diff --git a/libs/hwui/ProgramCache.cpp b/libs/hwui/ProgramCache.cpp
index 62ac2ba..297f5a9 100644
--- a/libs/hwui/ProgramCache.cpp
+++ b/libs/hwui/ProgramCache.cpp
@@ -385,7 +385,7 @@
     }
     // Uniforms
     shader.append(gVS_Header_Uniforms);
-    if (description.hasExternalTexture) {
+    if (description.hasTextureTransform) {
         shader.append(gVS_Header_Uniforms_TextureTransform);
     }
     if (description.hasGradient) {
@@ -415,11 +415,10 @@
 
     // Begin the shader
     shader.append(gVS_Main); {
-        if (description.hasTexture) {
-            shader.append(gVS_Main_OutTexCoords);
-        }
-        if (description.hasExternalTexture) {
+        if (description.hasTextureTransform) {
             shader.append(gVS_Main_OutTransformedTexCoords);
+        } else if (description.hasTexture || description.hasExternalTexture) {
+            shader.append(gVS_Main_OutTexCoords);
         }
         if (description.hasWidth) {
             shader.append(gVS_Main_Width);
@@ -487,8 +486,7 @@
     }
     if (description.hasTexture) {
         shader.append(gFS_Uniforms_TextureSampler);
-    }
-    if (description.hasExternalTexture) {
+    } else if (description.hasExternalTexture) {
         shader.append(gFS_Uniforms_ExternalTextureSampler);
     }
     if (description.hasWidth) {
diff --git a/libs/hwui/ProgramCache.h b/libs/hwui/ProgramCache.h
index 70909fd..21eb099 100644
--- a/libs/hwui/ProgramCache.h
+++ b/libs/hwui/ProgramCache.h
@@ -78,6 +78,7 @@
 #define PROGRAM_HAS_WIDTH_SHIFT 37
 
 #define PROGRAM_HAS_EXTERNAL_TEXTURE_SHIFT 38
+#define PROGRAM_HAS_TEXTURE_TRANSFORM_SHIFT 39
 
 ///////////////////////////////////////////////////////////////////////////////
 // Types
@@ -116,6 +117,7 @@
     bool hasTexture;
     bool hasAlpha8Texture;
     bool hasExternalTexture;
+    bool hasTextureTransform;
 
     // Modulate, this should only be set when setColor() return true
     bool modulate;
@@ -155,6 +157,7 @@
         hasTexture = false;
         hasAlpha8Texture = false;
         hasExternalTexture = false;
+        hasTextureTransform = false;
 
         hasWidth = false;
 
@@ -245,6 +248,7 @@
         if (isPoint) key |= programid(0x1) << PROGRAM_IS_POINT_SHIFT;
         if (hasWidth) key |= programid(0x1) << PROGRAM_HAS_WIDTH_SHIFT;
         if (hasExternalTexture) key |= programid(0x1) << PROGRAM_HAS_EXTERNAL_TEXTURE_SHIFT;
+        if (hasTextureTransform) key |= programid(0x1) << PROGRAM_HAS_TEXTURE_TRANSFORM_SHIFT;
         return key;
     }