Add program for linear gradient.

This change adds a new DrawLinearGradientProgram class to enable the drawing
of linear gradients. Two new vertex and fragment shaders are introduced,
based on DrawTextureProgram's shaders.

Change-Id: I885afc076bb6cef8cd3962ae21a086fa6a03bf96
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index 1431563..731862b 100644
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -124,6 +124,7 @@
 
     mDrawColorProgram = new DrawColorProgram;
     mDrawTextureProgram = new DrawTextureProgram;
+    mDrawLinearGradientProgram = new DrawLinearGradientProgram;
     mCurrentProgram = mDrawTextureProgram;
 
     mShader = kShaderNone;
@@ -521,6 +522,7 @@
 
 void OpenGLRenderer::resetShader() {
     mShader = OpenGLRenderer::kShaderNone;
+    mShaderKey = NULL;
     mShaderBlend = false;
     mShaderTileX = SkShader::kClamp_TileMode;
     mShaderTileY = SkShader::kClamp_TileMode;
@@ -536,9 +538,11 @@
     mShaderMatrix = matrix;
 }
 
-void OpenGLRenderer::setupLinearGradientShader(float* bounds, uint32_t* colors,
+void OpenGLRenderer::setupLinearGradientShader(SkShader* shader, float* bounds,uint32_t* colors,
         float* positions, SkShader::TileMode tileMode, SkMatrix* matrix, bool hasAlpha) {
+    // TODO: We should use a struct to describe each shader
     mShader = OpenGLRenderer::kShaderLinearGradient;
+    mShaderKey = shader;
     mShaderBlend = hasAlpha;
     mShaderTileX = tileMode;
     mShaderTileY = tileMode;
diff --git a/libs/hwui/OpenGLRenderer.h b/libs/hwui/OpenGLRenderer.h
index b3cef88..5a530eb 100644
--- a/libs/hwui/OpenGLRenderer.h
+++ b/libs/hwui/OpenGLRenderer.h
@@ -103,8 +103,8 @@
     void resetShader();
     void setupBitmapShader(SkBitmap* bitmap, SkShader::TileMode tileX, SkShader::TileMode tileY,
             SkMatrix* matrix, bool hasAlpha);
-    void setupLinearGradientShader(float* bounds, uint32_t* colors, float* positions,
-            SkShader::TileMode tileMode, SkMatrix* matrix, bool hasAlpha);
+    void setupLinearGradientShader(SkShader* shader, float* bounds, uint32_t* colors,
+            float* positions, SkShader::TileMode tileMode, SkMatrix* matrix, bool hasAlpha);
 
 private:
     /**
@@ -309,6 +309,7 @@
     sp<Program> mCurrentProgram;
     sp<DrawColorProgram> mDrawColorProgram;
     sp<DrawTextureProgram> mDrawTextureProgram;
+    sp<DrawLinearGradientProgram> mDrawLinearGradientProgram;
 
     // Used to draw textured quads
     TextureVertex mDrawTextureVertices[4];
@@ -323,6 +324,7 @@
 
     // Skia shaders
     ShaderType mShader;
+    SkShader* mShaderKey;
     bool mShaderBlend;
     SkShader::TileMode mShaderTileX;
     SkShader::TileMode mShaderTileY;
diff --git a/libs/hwui/Program.cpp b/libs/hwui/Program.cpp
index 609b28a..6202ba3 100644
--- a/libs/hwui/Program.cpp
+++ b/libs/hwui/Program.cpp
@@ -33,6 +33,9 @@
 #include "shaders/drawTexture.vert"
 #include "shaders/drawTexture.frag"
 
+#include "shaders/drawLinearGradient.vert"
+#include "shaders/drawLinearGradient.frag"
+
 ///////////////////////////////////////////////////////////////////////////////
 // Base program
 ///////////////////////////////////////////////////////////////////////////////
@@ -178,5 +181,26 @@
     glDisableVertexAttribArray(texCoords);
 }
 
+///////////////////////////////////////////////////////////////////////////////
+// Draw linear gradient
+///////////////////////////////////////////////////////////////////////////////
+
+DrawLinearGradientProgram::DrawLinearGradientProgram():
+        DrawColorProgram(gDrawLinearGradientVertexShader, gDrawLinearGradientFragmentShader) {
+    gradient = addUniform("gradient");
+    gradientLength = addUniform("gradientLength");
+    sampler = addUniform("sampler");
+}
+
+void DrawLinearGradientProgram::use() {
+    DrawColorProgram::use();
+    glActiveTexture(GL_TEXTURE0);
+    glUniform1i(sampler, 0);
+}
+
+void DrawLinearGradientProgram::remove() {
+    DrawColorProgram::remove();
+}
+
 }; // namespace uirenderer
 }; // namespace android
diff --git a/libs/hwui/Program.h b/libs/hwui/Program.h
index d90bcaf..d7970d9 100644
--- a/libs/hwui/Program.h
+++ b/libs/hwui/Program.h
@@ -148,6 +148,7 @@
      * Name of the color uniform.
      */
     int color;
+
     /**
      * Name of the transform uniform.
      */
@@ -189,6 +190,44 @@
     int texCoords;
 };
 
+/**
+ * Program used to draw linear gradients. In addition to everything that the
+ * DrawColorProgram supports, the following two attributes must be specified:
+ *      vec2 gradient, the vector describing the linear gradient
+ *      float gradientLength, the invert of the magnitude of the gradient vector
+ *      sampler2D sampler, the texture sampler
+ */
+class DrawLinearGradientProgram: public DrawColorProgram {
+public:
+    DrawLinearGradientProgram();
+
+    /**
+     * Binds this program to the GL context.
+     */
+    virtual void use();
+
+    /**
+     * Marks this program as unused. This will not unbind
+     * the program from the GL context.
+     */
+    virtual void remove();
+
+    /**
+     * Name of the linear gradient vector.
+     */
+    int gradient;
+
+    /**
+     * Name of the inverse of linear gradient vector's magnitude.
+     */
+    int gradientLength;
+
+    /**
+     * Name of the texture sampler uniform.
+     */
+    int sampler;
+};
+
 }; // namespace uirenderer
 }; // namespace android
 
diff --git a/libs/hwui/shaders/drawLinearGradient.frag b/libs/hwui/shaders/drawLinearGradient.frag
new file mode 100644
index 0000000..469c662
--- /dev/null
+++ b/libs/hwui/shaders/drawLinearGradient.frag
@@ -0,0 +1,14 @@
+SHADER_SOURCE(gDrawLinearGradientFragmentShader,
+
+precision mediump float;
+
+varying float index;
+
+uniform vec4 color;
+uniform sampler2D sampler;
+
+void main(void) {
+    gl_FragColor = texture2D(sampler, vec2(index, 0.5)) * color;
+}
+
+);
diff --git a/libs/hwui/shaders/drawLinearGradient.vert b/libs/hwui/shaders/drawLinearGradient.vert
new file mode 100644
index 0000000..963dc87
--- /dev/null
+++ b/libs/hwui/shaders/drawLinearGradient.vert
@@ -0,0 +1,16 @@
+SHADER_SOURCE(gDrawLinearGradientVertexShader,
+
+attribute vec4 position;
+
+uniform float gradientLength;
+uniform vec2 gradient;
+uniform mat4 transform;
+
+varying float index;
+
+void main(void) {
+    gl_Position = transform * position;
+    index = dot(gl_Position.xy, gradient) * gradientLength;
+}
+
+);