[Texmap] Consolidate the common parts of TextureMapperGL::drawTexture
authoryael.aharon@nokia.com <yael.aharon@nokia.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 24 Feb 2012 17:02:46 +0000 (17:02 +0000)
committeryael.aharon@nokia.com <yael.aharon@nokia.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 24 Feb 2012 17:02:46 +0000 (17:02 +0000)
https://bugs.webkit.org/show_bug.cgi?id=79143

Reviewed by Noam Rosenthal.

Combine the two drawTexture methods into one, and extract the part that
could not be combined into its own method.
No new tests. Refactoring only.

* platform/graphics/texmap/TextureMapperGL.cpp:
(WebCore):
(WebCore::TextureMapperGL::drawTexture):
* platform/graphics/texmap/TextureMapperGL.h:
(WebCore):
(BitmapTextureGL):
(WebCore::BitmapTextureGL::~BitmapTextureGL):
(WebCore::BitmapTextureGL::id):
(WebCore::BitmapTextureGL::relativeSize):
(WebCore::BitmapTextureGL::setTextureMapper):
(WebCore::BitmapTextureGL::BitmapTextureGL):
* platform/graphics/texmap/TextureMapperShaderManager.cpp:
(WebCore::TextureMapperShaderProgramOpacityAndMask::fragmentShaderSource):
(WebCore):
(WebCore::TextureMapperShaderProgramOpacityAndMask::prepare):
* platform/graphics/texmap/TextureMapperShaderManager.h:
(WebCore):
(WebCore::TextureMapperShaderProgram::prepare):
(WebCore::TextureMapperShaderProgram::matrixVariable):
(WebCore::TextureMapperShaderProgram::sourceMatrixVariable):
(WebCore::TextureMapperShaderProgram::sourceTextureVariable):
(WebCore::TextureMapperShaderProgram::opacityVariable):
(TextureMapperShaderProgram):
(TextureMapperShaderProgramSimple):
(TextureMapperShaderProgramOpacityAndMask):
(WebCore::TextureMapperShaderProgramOpacityAndMask::maskMatrixVariable):
(WebCore::TextureMapperShaderProgramOpacityAndMask::maskTextureVariable):

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

Source/WebCore/ChangeLog
Source/WebCore/platform/graphics/texmap/TextureMapperGL.cpp
Source/WebCore/platform/graphics/texmap/TextureMapperGL.h
Source/WebCore/platform/graphics/texmap/TextureMapperShaderManager.cpp
Source/WebCore/platform/graphics/texmap/TextureMapperShaderManager.h

index 9d0bb28..0ca8964 100644 (file)
@@ -1,3 +1,42 @@
+2012-02-24  Yael Aharon  <yael.aharon@nokia.com>
+
+        [Texmap] Consolidate the common parts of TextureMapperGL::drawTexture
+        https://bugs.webkit.org/show_bug.cgi?id=79143
+
+        Reviewed by Noam Rosenthal.
+
+        Combine the two drawTexture methods into one, and extract the part that
+        could not be combined into its own method.
+        No new tests. Refactoring only.
+
+        * platform/graphics/texmap/TextureMapperGL.cpp:
+        (WebCore):
+        (WebCore::TextureMapperGL::drawTexture):
+        * platform/graphics/texmap/TextureMapperGL.h:
+        (WebCore):
+        (BitmapTextureGL):
+        (WebCore::BitmapTextureGL::~BitmapTextureGL):
+        (WebCore::BitmapTextureGL::id):
+        (WebCore::BitmapTextureGL::relativeSize):
+        (WebCore::BitmapTextureGL::setTextureMapper):
+        (WebCore::BitmapTextureGL::BitmapTextureGL):
+        * platform/graphics/texmap/TextureMapperShaderManager.cpp:
+        (WebCore::TextureMapperShaderProgramOpacityAndMask::fragmentShaderSource):
+        (WebCore):
+        (WebCore::TextureMapperShaderProgramOpacityAndMask::prepare):
+        * platform/graphics/texmap/TextureMapperShaderManager.h:
+        (WebCore):
+        (WebCore::TextureMapperShaderProgram::prepare):
+        (WebCore::TextureMapperShaderProgram::matrixVariable):
+        (WebCore::TextureMapperShaderProgram::sourceMatrixVariable):
+        (WebCore::TextureMapperShaderProgram::sourceTextureVariable):
+        (WebCore::TextureMapperShaderProgram::opacityVariable):
+        (TextureMapperShaderProgram):
+        (TextureMapperShaderProgramSimple):
+        (TextureMapperShaderProgramOpacityAndMask):
+        (WebCore::TextureMapperShaderProgramOpacityAndMask::maskMatrixVariable):
+        (WebCore::TextureMapperShaderProgramOpacityAndMask::maskTextureVariable):
+
 2012-02-24  Daniel Bates  <dbates@webkit.org>
 
         style element and link element for CSS stylesheet should emit load/error event when sheet loads/fails to load
index 746a1d8..80cf9fa 100644 (file)
@@ -223,42 +223,6 @@ struct TextureMapperGLData {
     RefPtr<BitmapTexture> currentSurface;
 };
 
-class BitmapTextureGL : public BitmapTexture {
-public:
-    virtual void destroy();
-    virtual IntSize size() const;
-    virtual bool isValid() const;
-    virtual void didReset();
-    void bind();
-    void initializeStencil();
-    ~BitmapTextureGL() { destroy(); }
-    virtual uint32_t id() const { return m_id; }
-    inline FloatSize relativeSize() const { return m_relativeSize; }
-    void setTextureMapper(TextureMapperGL* texmap) { m_textureMapper = texmap; }
-    void updateContents(Image*, const IntRect&, const IntRect&, PixelFormat);
-    void updateContents(const void*, const IntRect&);
-
-private:
-    GLuint m_id;
-    FloatSize m_relativeSize;
-    IntSize m_textureSize;
-    IntRect m_dirtyRect;
-    GLuint m_fbo;
-    GLuint m_rbo;
-    bool m_surfaceNeedsReset;
-    TextureMapperGL* m_textureMapper;
-    BitmapTextureGL()
-        : m_id(0)
-        , m_fbo(0)
-        , m_rbo(0)
-        , m_surfaceNeedsReset(true)
-        , m_textureMapper(0)
-    {
-    }
-
-    friend class TextureMapperGL;
-};
-
 void TextureMapperGLData::initializeStencil()
 {
     if (currentSurface) {
@@ -354,15 +318,11 @@ void TextureMapperGL::drawTexture(const BitmapTexture& texture, const FloatRect&
 
 void TextureMapperGL::drawTexture(uint32_t texture, Flags flags, const FloatSize& relativeSize, const FloatRect& targetRect, const TransformationMatrix& modelViewMatrix, float opacity, const BitmapTexture* maskTexture)
 {
+    RefPtr<TextureMapperShaderProgram> shaderInfo;
     if (maskTexture)
-        drawTextureWithMaskAndOpacity(texture, flags, relativeSize, targetRect, modelViewMatrix, opacity, maskTexture);
+        shaderInfo = data().sharedGLData().textureMapperShaderManager.getShaderProgram<TextureMapperShaderProgramOpacityAndMask>();
     else
-        drawTextureSimple(texture, flags, relativeSize, targetRect, modelViewMatrix, opacity, maskTexture);
-}
-
-void TextureMapperGL::drawTextureWithMaskAndOpacity(uint32_t texture, Flags flags, const FloatSize& relativeSize, const FloatRect& targetRect, const TransformationMatrix& modelViewMatrix, float opacity, const BitmapTexture* maskTexture)
-{
-    RefPtr<TextureMapperShaderProgramOpacityAndMask> shaderInfo = data().sharedGLData().textureMapperShaderManager.getShaderProgram<TextureMapperShaderProgramOpacityAndMask>();
+        shaderInfo = data().sharedGLData().textureMapperShaderManager.getShaderProgram<TextureMapperShaderProgramSimple>();
 
     GL_CMD(glUseProgram(shaderInfo->id()))
     GL_CMD(glEnableVertexAttribArray(shaderInfo->vertexAttrib()))
@@ -392,63 +352,10 @@ void TextureMapperGL::drawTextureWithMaskAndOpacity(uint32_t texture, Flags flag
     GL_CMD(glUniformMatrix4fv(shaderInfo->matrixVariable(), 1, GL_FALSE, m4))
     GL_CMD(glUniformMatrix4fv(shaderInfo->sourceMatrixVariable(), 1, GL_FALSE, m4src))
     GL_CMD(glUniform1i(shaderInfo->sourceTextureVariable(), 0))
-    GL_CMD(glUniform1f(shaderInfo->opacityVariable(), opacity))
-
-    if (maskTexture->isValid()) {
-        const BitmapTextureGL* maskTextureGL = static_cast<const BitmapTextureGL*>(maskTexture);
-        GL_CMD(glActiveTexture(GL_TEXTURE1))
-        GL_CMD(glBindTexture(GL_TEXTURE_2D, maskTextureGL->id()))
-        const GLfloat m4mask[] = {maskTextureGL->relativeSize().width(), 0, 0, 0,
-                                         0, maskTextureGL->relativeSize().height(), 0, 0,
-                                         0, 0, 1, 0,
-                                         0, 0, 0, 1};
-        GL_CMD(glUniformMatrix4fv(shaderInfo->maskMatrixVariable(), 1, GL_FALSE, m4mask));
-        GL_CMD(glUniform1i(shaderInfo->maskTextureVariable(), 1))
-        GL_CMD(glActiveTexture(GL_TEXTURE0))
-    }
-
-    GL_CMD(glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA))
-    GL_CMD(glEnable(GL_BLEND))
-
-    GL_CMD(glDrawArrays(GL_TRIANGLE_FAN, 0, 4))
-    GL_CMD(glDisableVertexAttribArray(shaderInfo->vertexAttrib()))
-}
-
-void TextureMapperGL::drawTextureSimple(uint32_t texture, Flags flags, const FloatSize& relativeSize, const FloatRect& targetRect, const TransformationMatrix& modelViewMatrix, float opacity, const BitmapTexture*)
-{
-    RefPtr<TextureMapperShaderProgramSimple> shaderInfo = data().sharedGLData().textureMapperShaderManager.getShaderProgram<TextureMapperShaderProgramSimple>();
-
-    GL_CMD(glUseProgram(shaderInfo->id()))
-    GL_CMD(glEnableVertexAttribArray(shaderInfo->vertexAttrib()))
-    GL_CMD(glActiveTexture(GL_TEXTURE0))
-    GL_CMD(glBindTexture(GL_TEXTURE_2D, texture))
-    GL_CMD(glBindBuffer(GL_ARRAY_BUFFER, 0))
-    const GLfloat unitRect[] = {0, 0, 1, 0, 1, 1, 0, 1};
-    GL_CMD(glVertexAttribPointer(shaderInfo->vertexAttrib(), 2, GL_FLOAT, GL_FALSE, 0, unitRect))
-
-    TransformationMatrix matrix = TransformationMatrix(data().projectionMatrix).multiply(modelViewMatrix).multiply(TransformationMatrix(
-            targetRect.width(), 0, 0, 0,
-            0, targetRect.height(), 0, 0,
-            0, 0, 1, 0,
-            targetRect.x(), targetRect.y(), 0, 1));
 
-    const GLfloat m4[] = {
-        matrix.m11(), matrix.m12(), matrix.m13(), matrix.m14(),
-        matrix.m21(), matrix.m22(), matrix.m23(), matrix.m24(),
-        matrix.m31(), matrix.m32(), matrix.m33(), matrix.m34(),
-        matrix.m41(), matrix.m42(), matrix.m43(), matrix.m44()
-    };
-    const GLfloat m4src[] = {relativeSize.width(), 0, 0, 0,
-                                     0, relativeSize.height() * ((flags & ShouldFlipTexture) ? -1 : 1), 0, 0,
-                                     0, 0, 1, 0,
-                                     0, (flags & ShouldFlipTexture) ? relativeSize.height() : 0, 0, 1};
-
-    GL_CMD(glUniformMatrix4fv(shaderInfo->matrixVariable(), 1, GL_FALSE, m4))
-    GL_CMD(glUniformMatrix4fv(shaderInfo->sourceMatrixVariable(), 1, GL_FALSE, m4src))
-    GL_CMD(glUniform1i(shaderInfo->sourceTextureVariable(), 0))
-    GL_CMD(glUniform1f(shaderInfo->opacityVariable(), opacity))
+    shaderInfo->prepare(opacity, maskTexture);
 
-    bool needsBlending = (flags & SupportsBlending) || opacity < 0.99;
+    bool needsBlending = (flags & SupportsBlending) || opacity < 0.99 || maskTexture;
 
     if (needsBlending) {
         GL_CMD(glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA))
index 3e59890..9e47401 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "FloatQuad.h"
 #include "IntSize.h"
+#include "OpenGLShims.h"
 #include "TextureMapper.h"
 #include "TransformationMatrix.h"
 
@@ -31,6 +32,7 @@ namespace WebCore {
 
 class TextureMapperGLData;
 class GraphicsContext;
+class TextureMapperShaderProgram;
 
 // An OpenGL-ES2 implementation of TextureMapper.
 class TextureMapperGL : public TextureMapper {
@@ -48,8 +50,6 @@ public:
     // reimps from TextureMapper
     virtual void drawTexture(const BitmapTexture&, const FloatRect&, const TransformationMatrix&, float opacity, const BitmapTexture* maskTexture);
     virtual void drawTexture(uint32_t texture, Flags, const FloatSize&, const FloatRect&, const TransformationMatrix&, float opacity, const BitmapTexture* maskTexture);
-    virtual void drawTextureWithMaskAndOpacity(uint32_t texture, Flags, const FloatSize&, const FloatRect&, const TransformationMatrix&, float opacity, const BitmapTexture* maskTexture);
-    virtual void drawTextureSimple(uint32_t texture, Flags, const FloatSize&, const FloatRect&, const TransformationMatrix&, float opacity, const BitmapTexture* maskTexture);
     virtual void bindSurface(BitmapTexture* surface);
     virtual void beginClip(const TransformationMatrix&, const FloatRect&);
     virtual void beginPainting();
@@ -73,6 +73,42 @@ private:
     friend class BitmapTextureGL;
 };
 
+class BitmapTextureGL : public BitmapTexture {
+public:
+    virtual void destroy();
+    virtual IntSize size() const;
+    virtual bool isValid() const;
+    virtual void didReset();
+    void bind();
+    void initializeStencil();
+    ~BitmapTextureGL() { destroy(); }
+    virtual uint32_t id() const { return m_id; }
+    inline FloatSize relativeSize() const { return m_relativeSize; }
+    void setTextureMapper(TextureMapperGL* texmap) { m_textureMapper = texmap; }
+    void updateContents(Image*, const IntRect&, const IntRect&, PixelFormat);
+    void updateContents(const void*, const IntRect&);
+
+private:
+    GLuint m_id;
+    FloatSize m_relativeSize;
+    IntSize m_textureSize;
+    IntRect m_dirtyRect;
+    GLuint m_fbo;
+    GLuint m_rbo;
+    bool m_surfaceNeedsReset;
+    TextureMapperGL* m_textureMapper;
+    BitmapTextureGL()
+        : m_id(0)
+        , m_fbo(0)
+        , m_rbo(0)
+        , m_surfaceNeedsReset(true)
+        , m_textureMapper(0)
+    {
+    }
+
+    friend class TextureMapperGL;
+};
+
 // An offscreen buffer to be rendered by software.
 static inline int nextPowerOfTwo(int num)
 {
index 3eb34d1..245b573 100644 (file)
@@ -22,6 +22,8 @@
 
 #if USE(ACCELERATED_COMPOSITING) && USE(TEXTURE_MAPPER)
 
+#include "TextureMapperGL.h"
+
 namespace WebCore {
 
 #ifndef TEXMAP_OPENGL_ES_2
@@ -154,6 +156,11 @@ const char* TextureMapperShaderProgramSimple::fragmentShaderSource()
     return fragmentShaderSourceSimple;
 }
 
+void TextureMapperShaderProgramSimple::prepare(float opacity, const BitmapTexture* maskTexture)
+{
+    glUniform1f(m_opacityVariable, opacity);
+}
+
 PassRefPtr<TextureMapperShaderProgramOpacityAndMask> TextureMapperShaderProgramOpacityAndMask::create()
 {
     return adoptRef(new TextureMapperShaderProgramOpacityAndMask());
@@ -180,6 +187,24 @@ const char* TextureMapperShaderProgramOpacityAndMask::fragmentShaderSource()
     return fragmentShaderSourceOpacityAndMask;
 }
 
+void TextureMapperShaderProgramOpacityAndMask::prepare(float opacity, const BitmapTexture* maskTexture)
+{
+    glUniform1f(m_opacityVariable, opacity);
+    if (!maskTexture || !maskTexture->isValid())
+        return;
+
+    const BitmapTextureGL* maskTextureGL = static_cast<const BitmapTextureGL*>(maskTexture);
+    glActiveTexture(GL_TEXTURE1);
+    glBindTexture(GL_TEXTURE_2D, maskTextureGL->id());
+    const GLfloat m4mask[] = {maskTextureGL->relativeSize().width(), 0, 0, 0,
+                                     0, maskTextureGL->relativeSize().height(), 0, 0,
+                                     0, 0, 1, 0,
+                                     0, 0, 0, 1};
+    glUniformMatrix4fv(m_maskMatrixVariable, 1, GL_FALSE, m4mask);
+    glUniform1i(m_maskTextureVariable, 1);
+    glActiveTexture(GL_TEXTURE0);
+}
+
 TextureMapperShaderManager::TextureMapperShaderManager()
 {
     ASSERT(initializeOpenGLShims());
index 53aeaaa..1f0b5c5 100644 (file)
@@ -35,6 +35,7 @@ namespace WebCore {
 
 typedef void* ShaderType;
 
+class BitmapTexture;
 class TextureMapperShaderManager;
 
 class TextureMapperShaderProgram : public RefCounted<TextureMapperShaderProgram> {
@@ -51,6 +52,12 @@ public:
         return &type;
     }
 
+    virtual void prepare(float opacity, const BitmapTexture*) { }
+    GLint matrixVariable() { return m_matrixVariable; }
+    GLint sourceMatrixVariable() { return m_sourceMatrixVariable; }
+    GLint sourceTextureVariable() { return m_sourceTextureVariable; }
+    GLint opacityVariable() { return m_opacityVariable; }
+
 protected:
     void getUniformLocation(GLint& var, const char* name);
     void initializeProgram();
@@ -61,47 +68,37 @@ protected:
     GLuint m_vertexAttrib;
     GLuint m_vertexShader;
     GLuint m_fragmentShader;
+    GLint m_matrixVariable;
+    GLint m_sourceMatrixVariable;
+    GLint m_sourceTextureVariable;
+    GLint m_opacityVariable;
 };
 
 class TextureMapperShaderProgramSimple : public TextureMapperShaderProgram {
 public:
     static PassRefPtr<TextureMapperShaderProgramSimple> create();
-    GLint matrixVariable() { return m_matrixVariable; }
-    GLint sourceMatrixVariable() { return m_sourceMatrixVariable; }
-    GLint sourceTextureVariable() { return m_sourceTextureVariable; }
-    GLint opacityVariable() { return m_opacityVariable; }
+    virtual void prepare(float opacity, const BitmapTexture*);
 
 private:
     virtual const char* vertexShaderSource();
     virtual const char* fragmentShaderSource();
     TextureMapperShaderProgramSimple();
-    GLint m_matrixVariable;
-    GLint m_sourceMatrixVariable;
-    GLint m_sourceTextureVariable;
-    GLint m_opacityVariable;
 };
 
 class TextureMapperShaderProgramOpacityAndMask : public TextureMapperShaderProgram {
 public:
     static PassRefPtr<TextureMapperShaderProgramOpacityAndMask> create();
-    GLint sourceMatrixVariable() { return m_sourceMatrixVariable; }
-    GLint matrixVariable() { return m_matrixVariable; }
+    virtual void prepare(float opacity, const BitmapTexture*);
     GLint maskMatrixVariable() { return m_maskMatrixVariable; }
-    GLint sourceTextureVariable() { return m_sourceTextureVariable; }
     GLint maskTextureVariable() { return m_maskTextureVariable; }
-    GLint opacityVariable() { return m_opacityVariable; }
 
 private:
     static int m_classID;
     virtual const char* vertexShaderSource();
     virtual const char* fragmentShaderSource();
     TextureMapperShaderProgramOpacityAndMask();
-    GLint m_sourceMatrixVariable;
-    GLint m_matrixVariable;
     GLint m_maskMatrixVariable;
-    GLint m_sourceTextureVariable;
     GLint m_maskTextureVariable;
-    GLint m_opacityVariable;
 };
 
 class TextureMapperShaderManager {