OSDN Git Service

Fix transparency bug, and add support for contrast
authorNicolas Roard <nicolasroard@google.com>
Tue, 2 Aug 2011 18:48:26 +0000 (11:48 -0700)
committerNicolas Roard <nicolasroard@google.com>
Tue, 2 Aug 2011 21:28:09 +0000 (14:28 -0700)
bug:5111072 bug:5111259

Change-Id: Icc5d7c9f3fc9390f89b8c76eefe8a9d704116c57

Source/WebCore/platform/graphics/android/ShaderProgram.cpp
Source/WebCore/platform/graphics/android/ShaderProgram.h

index ed1ce11..c4abeab 100644 (file)
@@ -128,14 +128,16 @@ static const char gSurfaceTextureOESFragmentShaderInverted[] =
     "precision mediump float;\n"
     "varying vec2 v_texCoord; \n"
     "uniform float alpha; \n"
+    "uniform float contrast; \n"
     "uniform samplerExternalOES s_texture; \n"
     "void main() {\n"
-    "  gl_FragColor = texture2D(s_texture, v_texCoord); \n"
-    "  float color = 1.0 - (gl_FragColor.r + gl_FragColor.g + gl_FragColor.b) / 3.0; \n"
-    "  gl_FragColor.r = color; \n"
-    "  gl_FragColor.g = color; \n"
-    "  gl_FragColor.b = color; \n"
-    "  gl_FragColor *= alpha; "
+    "  vec4 pixel = texture2D(s_texture, v_texCoord); \n"
+    "  float a = pixel.a; \n"
+    "  float color = a - (0.2989 * pixel.r + 0.5866 * pixel.g + 0.1145 * pixel.b);\n"
+    "  color = ((color - a/2.0) * contrast) + a/2.0; \n"
+    "  pixel.rgb = vec3(color, color, color); \n "
+    "  gl_FragColor = pixel; \n"
+    "  gl_FragColor *= alpha; \n"
     "}\n";
 
 GLuint ShaderProgram::loadShader(GLenum shaderType, const char* pSource)
@@ -207,6 +209,7 @@ GLuint ShaderProgram::createProgram(const char* pVertexSource, const char* pFrag
 
 ShaderProgram::ShaderProgram()
     : m_blendingEnabled(false)
+    , m_contrast(1)
 {
     init();
 }
@@ -264,6 +267,7 @@ void ShaderProgram::init()
     m_hSTOESProjectionMatrixInverted =
         glGetUniformLocation(m_surfTexOESProgramInverted, "projectionMatrix");
     m_hSTOESAlphaInverted = glGetUniformLocation(m_surfTexOESProgramInverted, "alpha");
+    m_hSTOESContrastInverted = glGetUniformLocation(m_surfTexOESProgramInverted, "contrast");
     m_hSTOESTexSamplerInverted = glGetUniformLocation(m_surfTexOESProgramInverted, "s_texture");
     m_hSTOESPositionInverted = glGetAttribLocation(m_surfTexOESProgramInverted, "vPosition");
 
@@ -338,7 +342,8 @@ void ShaderProgram::drawQuadInternal(SkRect& geometry,
                                      GLint texSampler,
                                      GLenum textureTarget,
                                      GLint position,
-                                     GLint alpha)
+                                     GLint alpha,
+                                     GLint contrast)
 {
     glUseProgram(program);
     setProjectionMatrix(geometry, projectionMatrixHandle);
@@ -355,6 +360,8 @@ void ShaderProgram::drawQuadInternal(SkRect& geometry,
     glEnableVertexAttribArray(position);
     glVertexAttribPointer(position, 2, GL_FLOAT, GL_FALSE, 0, 0);
     glUniform1f(alpha, opacity);
+    if (contrast != -1)
+        glUniform1f(contrast, m_contrast);
 
     setBlendingState(opacity < 1.0);
     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
@@ -379,7 +386,8 @@ void ShaderProgram::drawQuad(SkRect& geometry, int textureId, float opacity,
         drawQuadInternal(geometry, textureId, opacity, m_surfTexOESProgramInverted,
                          m_hSTOESProjectionMatrixInverted,
                          m_hSTOESTexSamplerInverted, GL_TEXTURE_EXTERNAL_OES,
-                         m_hSTOESPositionInverted, m_hSTOESAlphaInverted);
+                         m_hSTOESPositionInverted, m_hSTOESAlphaInverted,
+                         m_hSTOESContrastInverted);
     } else if (!textureTarget
                && !TilesManager::instance()->invertedScreen()) {
         drawQuadInternal(geometry, textureId, opacity, m_surfTex2DProgram,
@@ -523,7 +531,8 @@ void ShaderProgram::drawLayerQuadInternal(const GLfloat* projectionMatrix,
                                           int textureId, float opacity,
                                           GLenum textureTarget, GLint program,
                                           GLint matrix, GLint texSample,
-                                          GLint position, GLint alpha)
+                                          GLint position, GLint alpha,
+                                          GLint contrast)
 {
     glUseProgram(program);
     glUniformMatrix4fv(matrix, 1, GL_FALSE, projectionMatrix);
@@ -541,6 +550,8 @@ void ShaderProgram::drawLayerQuadInternal(const GLfloat* projectionMatrix,
     glEnableVertexAttribArray(position);
     glVertexAttribPointer(position, 2, GL_FLOAT, GL_FALSE, 0, 0);
     glUniform1f(alpha, opacity);
+    if (contrast != -1)
+        glUniform1f(contrast, m_contrast);
 }
 
 
@@ -573,7 +584,8 @@ void ShaderProgram::drawLayerQuad(const TransformationMatrix& drawMatrix,
         drawLayerQuadInternal(projectionMatrix, textureId, opacity,
                               GL_TEXTURE_EXTERNAL_OES, m_surfTexOESProgramInverted,
                               m_hSTOESProjectionMatrixInverted, m_hSTOESTexSamplerInverted,
-                              m_hSTOESPositionInverted, m_hSTOESAlphaInverted);
+                              m_hSTOESPositionInverted, m_hSTOESAlphaInverted,
+                              m_hSTOESContrastInverted);
     } else if (!textureTarget
                && !TilesManager::instance()->invertedScreen()) {
         drawLayerQuadInternal(projectionMatrix, textureId, opacity,
index 244f2be..5c1f324 100644 (file)
@@ -25,6 +25,8 @@
 #include "TransformationMatrix.h"
 #include <GLES2/gl2.h>
 
+#define MAX_CONTRAST 5
+
 namespace WebCore {
 
 class ShaderProgram {
@@ -77,6 +79,15 @@ public:
     IntRect clippedRectWithViewport(const IntRect& rect, int margin = 0);
 
     void resetBlending();
+    float contrast() { return m_contrast; }
+    void setContrast(float c) {
+        float contrast = c;
+        if (contrast < 0)
+            contrast = 0;
+        if (contrast > MAX_CONTRAST)
+            contrast = MAX_CONTRAST;
+        m_contrast = contrast;
+    }
 
 private:
     GLuint loadShader(GLenum shaderType, const char* pSource);
@@ -88,12 +99,12 @@ private:
     void drawQuadInternal(SkRect& geometry, GLint textureId, float opacity,
                           GLint program, GLint projectionMatrixHandle,
                           GLint texSampler, GLenum textureTarget,
-                          GLint position, GLint alpha);
+                          GLint position, GLint alpha, GLint contrast = -1);
 
     void drawLayerQuadInternal(const GLfloat* projectionMatrix, int textureId,
                                float opacity, GLenum textureTarget, GLint program,
                                GLint matrix, GLint texSample,
-                               GLint position, GLint alpha);
+                               GLint position, GLint alpha, GLint contrast = -1);
 
     bool m_blendingEnabled;
 
@@ -141,9 +152,12 @@ private:
 
     GLint m_hSTOESProjectionMatrixInverted;
     GLint m_hSTOESAlphaInverted;
+    GLint m_hSTOESContrastInverted;
     GLint m_hSTOESTexSamplerInverted;
     GLint m_hSTOESPositionInverted;
 
+    float m_contrast;
+
     // attribs
     GLint m_hPosition;
     GLint m_hVideoPosition;