diff options
author | Romain Guy <romainguy@android.com> | 2010-07-16 23:13:33 -0700 |
---|---|---|
committer | Romain Guy <romainguy@android.com> | 2010-07-16 23:18:27 -0700 |
commit | f9764a4f532561f6e2e985ff3b25112f1132ce44 (patch) | |
tree | 910110cc35d1d3e2a48f000734ec50f6e53ced0f /libs/hwui | |
parent | 7fac2e18339f765320d759e8d4c090f92431959e (diff) | |
download | frameworks_base-f9764a4f532561f6e2e985ff3b25112f1132ce44.zip frameworks_base-f9764a4f532561f6e2e985ff3b25112f1132ce44.tar.gz frameworks_base-f9764a4f532561f6e2e985ff3b25112f1132ce44.tar.bz2 |
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
Diffstat (limited to 'libs/hwui')
-rw-r--r-- | libs/hwui/OpenGLRenderer.cpp | 6 | ||||
-rw-r--r-- | libs/hwui/OpenGLRenderer.h | 6 | ||||
-rw-r--r-- | libs/hwui/Program.cpp | 24 | ||||
-rw-r--r-- | libs/hwui/Program.h | 39 | ||||
-rw-r--r-- | libs/hwui/shaders/drawLinearGradient.frag | 14 | ||||
-rw-r--r-- | libs/hwui/shaders/drawLinearGradient.vert | 16 |
6 files changed, 102 insertions, 3 deletions
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 @@ OpenGLRenderer::OpenGLRenderer(): mDrawColorProgram = new DrawColorProgram; mDrawTextureProgram = new DrawTextureProgram; + mDrawLinearGradientProgram = new DrawLinearGradientProgram; mCurrentProgram = mDrawTextureProgram; mShader = kShaderNone; @@ -521,6 +522,7 @@ void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, void OpenGLRenderer::resetShader() { mShader = OpenGLRenderer::kShaderNone; + mShaderKey = NULL; mShaderBlend = false; mShaderTileX = SkShader::kClamp_TileMode; mShaderTileY = SkShader::kClamp_TileMode; @@ -536,9 +538,11 @@ void OpenGLRenderer::setupBitmapShader(SkBitmap* bitmap, SkShader::TileMode tile 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 @@ public: 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 @@ private: sp<Program> mCurrentProgram; sp<DrawColorProgram> mDrawColorProgram; sp<DrawTextureProgram> mDrawTextureProgram; + sp<DrawLinearGradientProgram> mDrawLinearGradientProgram; // Used to draw textured quads TextureVertex mDrawTextureVertices[4]; @@ -323,6 +324,7 @@ private: // 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 @@ namespace uirenderer { #include "shaders/drawTexture.vert" #include "shaders/drawTexture.frag" +#include "shaders/drawLinearGradient.vert" +#include "shaders/drawLinearGradient.frag" + /////////////////////////////////////////////////////////////////////////////// // Base program /////////////////////////////////////////////////////////////////////////////// @@ -178,5 +181,26 @@ void DrawTextureProgram::remove() { 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 @@ public: * Name of the color uniform. */ int color; + /** * Name of the transform uniform. */ @@ -189,6 +190,44 @@ public: 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; +} + +); |