[GTK+][WPE] Allow TextureMapperShaderProgram to perform colorspace conversions with...
authormagomez@igalia.com <magomez@igalia.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 2 Jun 2017 09:18:16 +0000 (09:18 +0000)
committermagomez@igalia.com <magomez@igalia.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 2 Jun 2017 09:18:16 +0000 (09:18 +0000)
https://bugs.webkit.org/show_bug.cgi?id=172749

Reviewed by Žan Doberšek.

Currently TextureMapperGL is only able to paint properly textures that are in RGBA format. There are
situations where we will want it to be able to handle other formats (for example BGRA for gstreamer-gl).
To make this possible without having to perform the color conversion in the CPU, we add a new color
conversion matrix to TextureMapperShaderProgram. This matrix will perform the color space conversion
in the GPU, just after sampling the source texture.

Also, add a new flag to TextureMapperGL to indicate that a color conversion is required to handle a
texture. This flag will be set to the TextureMapperPlatformLayerBuffers as required, and TextureMapperGL
will set the appropriate color space matrix to the shader. Initially only the flag for BGRA->RGBA conversion
is defined, but support for new transformations can be added as needed.

VideoTextureCopierGStreamer also uses the TextureMapperShaderProgram to perform texture sampling, but for
the moment it just sets an identity matrix because when using gstreamer-gl, the format of the video frames
is RGBA. This will be changed soon to use BGRA.

No new tests.

* platform/graphics/gstreamer/VideoTextureCopierGStreamer.cpp:
(WebCore::VideoTextureCopierGStreamer::copyVideoTextureToPlatformTexture):
* platform/graphics/texmap/TextureMapperGL.cpp:
(WebCore::colorSpaceMatrixForFlags):
(WebCore::TextureMapperGL::drawTexturedQuadWithProgram):
* platform/graphics/texmap/TextureMapperGL.h:
* platform/graphics/texmap/TextureMapperShaderProgram.cpp:
* platform/graphics/texmap/TextureMapperShaderProgram.h:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@217706 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/platform/graphics/gstreamer/VideoTextureCopierGStreamer.cpp
Source/WebCore/platform/graphics/texmap/TextureMapperGL.cpp
Source/WebCore/platform/graphics/texmap/TextureMapperGL.h
Source/WebCore/platform/graphics/texmap/TextureMapperShaderProgram.cpp
Source/WebCore/platform/graphics/texmap/TextureMapperShaderProgram.h

index a10fd24..7e44e0f 100644 (file)
@@ -1,3 +1,36 @@
+2017-06-02  Miguel Gomez  <magomez@igalia.com>
+
+        [GTK+][WPE] Allow TextureMapperShaderProgram to perform colorspace conversions with the source texture pixels
+        https://bugs.webkit.org/show_bug.cgi?id=172749
+
+        Reviewed by Žan Doberšek.
+
+        Currently TextureMapperGL is only able to paint properly textures that are in RGBA format. There are
+        situations where we will want it to be able to handle other formats (for example BGRA for gstreamer-gl).
+        To make this possible without having to perform the color conversion in the CPU, we add a new color
+        conversion matrix to TextureMapperShaderProgram. This matrix will perform the color space conversion
+        in the GPU, just after sampling the source texture.
+
+        Also, add a new flag to TextureMapperGL to indicate that a color conversion is required to handle a
+        texture. This flag will be set to the TextureMapperPlatformLayerBuffers as required, and TextureMapperGL
+        will set the appropriate color space matrix to the shader. Initially only the flag for BGRA->RGBA conversion
+        is defined, but support for new transformations can be added as needed.
+
+        VideoTextureCopierGStreamer also uses the TextureMapperShaderProgram to perform texture sampling, but for
+        the moment it just sets an identity matrix because when using gstreamer-gl, the format of the video frames
+        is RGBA. This will be changed soon to use BGRA.
+
+        No new tests.
+
+        * platform/graphics/gstreamer/VideoTextureCopierGStreamer.cpp:
+        (WebCore::VideoTextureCopierGStreamer::copyVideoTextureToPlatformTexture):
+        * platform/graphics/texmap/TextureMapperGL.cpp:
+        (WebCore::colorSpaceMatrixForFlags):
+        (WebCore::TextureMapperGL::drawTexturedQuadWithProgram):
+        * platform/graphics/texmap/TextureMapperGL.h:
+        * platform/graphics/texmap/TextureMapperShaderProgram.cpp:
+        * platform/graphics/texmap/TextureMapperShaderProgram.h:
+
 2017-06-02  Javier Fernandez  <jfernandez@igalia.com>
 
         [css-grid] Margin wrong applied when stretching an orthogonal item in fixed size track
index 0ac9bd8..616d437 100644 (file)
@@ -170,6 +170,7 @@ bool VideoTextureCopierGStreamer::copyVideoTextureToPlatformTexture(Platform3DOb
     m_shaderProgram->setMatrix(m_shaderProgram->modelViewMatrixLocation(), m_modelViewMatrix);
     m_shaderProgram->setMatrix(m_shaderProgram->projectionMatrixLocation(), m_projectionMatrix);
     m_shaderProgram->setMatrix(m_shaderProgram->textureSpaceMatrixLocation(), m_textureSpaceMatrix);
+    m_shaderProgram->setMatrix(m_shaderProgram->textureColorSpaceMatrixLocation(), TransformationMatrix());
 
     // Perform the copy.
     m_context3D->enableVertexAttribArray(m_shaderProgram->vertexLocation());
index 98dc845..cf798f4 100644 (file)
@@ -409,6 +409,16 @@ static void prepareFilterProgram(TextureMapperShaderProgram& program, const Filt
     }
 }
 
+static TransformationMatrix colorSpaceMatrixForFlags(TextureMapperGL::Flags flags)
+{
+    // The matrix is initially the identity one, which means no color conversion.
+    TransformationMatrix matrix;
+    if (flags & TextureMapperGL::ShouldConvertTextureBGRAToRGBA)
+        matrix.setMatrix(0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
+
+    return matrix;
+}
+
 void TextureMapperGL::drawTexture(const BitmapTexture& texture, const FloatRect& targetRect, const TransformationMatrix& matrix, float opacity, unsigned exposedEdges)
 {
     if (!texture.isValid())
@@ -598,6 +608,7 @@ void TextureMapperGL::drawTexturedQuadWithProgram(TextureMapperShaderProgram& pr
         patternTransform.translate(0, -1);
 
     program.setMatrix(program.textureSpaceMatrixLocation(), patternTransform);
+    program.setMatrix(program.textureColorSpaceMatrixLocation(), colorSpaceMatrixForFlags(flags));
     m_context3D->uniform1f(program.opacityLocation(), opacity);
 
     if (opacity < 1)
index e7c80e9..da184a3 100644 (file)
@@ -50,7 +50,8 @@ public:
         ShouldAntialias = 0x08,
         ShouldRotateTexture90 = 0x10,
         ShouldRotateTexture180 = 0x20,
-        ShouldRotateTexture270 = 0x40
+        ShouldRotateTexture270 = 0x40,
+        ShouldConvertTextureBGRAToRGBA = 0x80
     };
 
     typedef int Flags;
index b0c371b..6cb592d 100644 (file)
@@ -144,6 +144,7 @@ static const char* fragmentTemplate =
     STRINGIFY(
         precision TextureSpaceMatrixPrecision float;
         uniform mat4 u_textureSpaceMatrix;
+        uniform mat4 u_textureColorSpaceMatrix;
         precision mediump float;
         uniform SamplerType s_sampler;
         uniform sampler2D s_contentTexture;
@@ -173,7 +174,7 @@ static const char* fragmentTemplate =
 
         void applyManualRepeat(inout vec2 pos) { pos = fract(pos); }
 
-        void applyTexture(inout vec4 color, vec2 texCoord) { color = SamplerFunction(s_sampler, texCoord); }
+        void applyTexture(inout vec4 color, vec2 texCoord) { color = u_textureColorSpaceMatrix * SamplerFunction(s_sampler, texCoord); }
         void applyOpacity(inout vec4 color) { color *= u_opacity; }
         void applyAntialiasing(inout vec4 color) { color *= antialias(); }
 
index 541a42b..da5256b 100644 (file)
@@ -77,6 +77,7 @@ public:
     TEXMAP_DECLARE_UNIFORM(modelViewMatrix)
     TEXMAP_DECLARE_UNIFORM(projectionMatrix)
     TEXMAP_DECLARE_UNIFORM(textureSpaceMatrix)
+    TEXMAP_DECLARE_UNIFORM(textureColorSpaceMatrix)
     TEXMAP_DECLARE_UNIFORM(opacity)
     TEXMAP_DECLARE_UNIFORM(color)
     TEXMAP_DECLARE_UNIFORM(expandedQuadEdgesInScreenSpace)