<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[70452] trunk/WebCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/70452">70452</a></dd>
<dt>Author</dt> <dd>noam.rosenthal@nokia.com</dd>
<dt>Date</dt> <dd>2010-10-25 07:53:19 -0700 (Mon, 25 Oct 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>2010-10-25  No'am Rosenthal  &lt;noam.rosenthal@nokia.com&gt;

        Reviewed by Kenneth Rohde Christiansen.

        [Texmap] [Qt] Texture mapper initial implementation
        This patch adds the OpenGL implementation of TextureMapper. See the other entries from bug 47070 for details.
        https://bugs.webkit.org/show_bug.cgi?id=47070

        No new tests. Part of a new implementation.

        * platform/graphics/opengl/TextureMapperGL.cpp: Added.
        * platform/graphics/opengl/TextureMapperGL.h: Added.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkWebCoreChangeLog">trunk/WebCore/ChangeLog</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkWebCoreplatformgraphicsopenglTextureMapperGLcpp">trunk/WebCore/platform/graphics/opengl/TextureMapperGL.cpp</a></li>
<li><a href="#trunkWebCoreplatformgraphicsopenglTextureMapperGLh">trunk/WebCore/platform/graphics/opengl/TextureMapperGL.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/WebCore/ChangeLog (70451 => 70452)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/WebCore/ChangeLog        2010-10-25 14:15:43 UTC (rev 70451)
+++ trunk/WebCore/ChangeLog        2010-10-25 14:53:19 UTC (rev 70452)
</span><span class="lines">@@ -1,3 +1,16 @@
</span><ins>+2010-10-25  No'am Rosenthal  &lt;noam.rosenthal@nokia.com&gt;
+
+        Reviewed by Kenneth Rohde Christiansen.
+
+        [Texmap] [Qt] Texture mapper initial implementation
+        This patch adds the OpenGL implementation of TextureMapper. See the other entries from bug 47070 for details.
+        https://bugs.webkit.org/show_bug.cgi?id=47070
+
+        No new tests. Part of a new implementation.
+
+        * platform/graphics/opengl/TextureMapperGL.cpp: Added.
+        * platform/graphics/opengl/TextureMapperGL.h: Added.
+
</ins><span class="cx"> 2010-10-25  Peter Rybin  &lt;peter.rybin@gmail.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Reviewed by Adam Barth.
</span></span></pre></div>
<a id="trunkWebCoreplatformgraphicsopenglTextureMapperGLcpp"></a>
<div class="addfile"><h4>Added: trunk/WebCore/platform/graphics/opengl/TextureMapperGL.cpp (0 => 70452)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/WebCore/platform/graphics/opengl/TextureMapperGL.cpp                                (rev 0)
+++ trunk/WebCore/platform/graphics/opengl/TextureMapperGL.cpp        2010-10-25 14:53:19 UTC (rev 70452)
</span><span class="lines">@@ -0,0 +1,594 @@
</span><ins>+/*
+ Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB.  If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
+ */
+
+#include &quot;config.h&quot;
+#include &quot;TextureMapperGL.h&quot;
+
+#include &quot;GraphicsContext.h&quot;
+#include &quot;HashMap.h&quot;
+#include &quot;Image.h&quot;
+#include &quot;PassRefPtr.h&quot;
+#include &quot;RefCounted.h&quot;
+#include &quot;Timer.h&quot;
+
+#if defined(TEXMAP_OPENGL_ES_2)
+#include &lt;GLES2/gl2.h&gt;
+#elif PLATFORM(MAC)
+#include &lt;gl.h&gt;
+#else
+#include &lt;GL/gl.h&gt;
+#endif
+
+#ifndef TEXMAP_OPENGL_ES2
+extern &quot;C&quot; {
+    void glUniform1f(GLint, GLfloat);
+    void glUniform1i(GLint, GLint);
+    void glVertexAttribPointer(GLuint, GLint, GLenum, GLboolean, GLsizei, const GLvoid*);
+    void glUniform4f(GLint, GLfloat, GLfloat, GLfloat, GLfloat);
+    void glShaderSource(GLuint, GLsizei, const char**, const GLint*);
+    GLuint glCreateShader(GLenum);
+    void glShaderSource(GLuint, GLsizei, const char**, const GLint*);
+    void glCompileShader(GLuint);
+    void glDeleteShader(GLuint);
+    void glUniformMatrix4fv(GLint, GLsizei, GLboolean, const GLfloat*);
+    GLuint glCreateProgram();
+    void glAttachShader(GLuint, GLuint);
+    void glLinkProgram(GLuint);
+    void glUseProgram(GLuint);
+    void glDisableVertexAttribArray(GLuint);
+    void glEnableVertexAttribArray(GLuint);
+    void glBindFramebuffer(GLenum target, GLuint framebuffer);
+    void glDeleteFramebuffers(GLsizei n, const GLuint* framebuffers);
+    void glGenFramebuffers(GLsizei n, GLuint* framebuffers);
+    void glFramebufferTexture2D(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level);
+    void glGetFramebufferAttachmentParameteriv(GLenum target, GLenum attachment, GLenum pname, GLint* params);
+    void glBindBuffer(GLenum, GLuint);
+    void glDeleteBuffers(GLsizei, const GLuint*);
+    void glGenBuffers(GLsizei, GLuint*);
+    void glBufferData(GLenum, GLsizeiptr, const GLvoid*, GLenum);
+    void glBufferSubData(GLenum, GLsizeiptr, GLsizeiptr, const GLvoid*);
+    void glGetProgramInfoLog(GLuint program, GLsizei, GLsizei*, GLchar*);
+
+#if !PLATFORM(MAC)
+    GLint glGetUniformLocation(GLuint, const GLchar*);
+    GLint glBindAttribLocation(GLuint, GLuint, const GLchar*);
+#endif
+}
+#endif
+
+namespace WebCore {
+
+inline static void debugGLCommand(const char* command, int line)
+{
+    const GLenum err = glGetError();
+    if (!err)
+        return;
+    WTFReportError(__FILE__, line, WTF_PRETTY_FUNCTION, &quot;[TextureMapper GL] Command failed: %s (%x)\n&quot;, command, err);
+}
+
+#define DEBUG_GL_COMMANDS
+
+#ifdef DEBUG_GL_COMMANDS
+#define GL_CMD(x) {x, debugGLCommand(#x, __LINE__); }
+#else
+#define GL_CMD(x) x
+#endif
+
+class BitmapTextureGL : public BitmapTexture {
+public:
+    virtual void destroy();
+    virtual IntSize size() const;
+    virtual bool isValid() const;
+    virtual void reset(const IntSize&amp;, bool opaque);
+    virtual PlatformGraphicsContext* beginPaint(const IntRect&amp; dirtyRect);
+    virtual void endPaint();
+    virtual void setContentsToImage(Image*);
+
+private:
+    GLuint m_id;
+    NativeImagePtr m_image;
+    FloatSize m_relativeSize;
+    bool m_opaque;
+    IntSize m_textureSize;
+    RefPtr&lt;RGBA32PremultimpliedBuffer&gt; m_buffer;
+    IntRect m_dirtyRect;
+    GLuint m_fbo;
+    IntSize m_actualSize;
+    bool m_surfaceNeedsReset;
+    BitmapTextureGL()
+        : m_id(0)
+        , m_image(0)
+        , m_opaque(false)
+        , m_fbo(0)
+        , m_surfaceNeedsReset(true)
+    {
+    }
+
+    friend class TextureMapperGL;
+};
+
+static struct TexmapShaderInfo {
+    enum ShaderProgramIndex {
+        SimpleProgram,
+        OpacityAndMaskProgram,
+        TargetProgram,
+        NumPrograms
+    };
+
+    enum ShaderVariableIndex {
+        InMatrixVariable,
+        InSourceMatrixVariable,
+        InMaskMatrixVariable,
+        InVertexVariable,
+
+        OpacityVariable,
+        SourceTextureVariable,
+        MaskTextureVariable,
+        NumVariables
+    };
+
+    struct ProgramInfo {
+        GLuint id;
+        GLint vars[NumVariables];
+    };
+
+    GLint getUniformLocation(ShaderProgramIndex prog, ShaderVariableIndex var, const char* name)
+    {
+        return programs[prog].vars[var] = glGetUniformLocation(programs[prog].id, name);
+    }
+
+    void createShaderProgram(const char* vertexShaderSource, const char* fragmentShaderSource, ShaderProgramIndex index)
+    {
+        GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
+        GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
+        GL_CMD(glShaderSource(vertexShader, 1, &amp;vertexShaderSource, 0))
+        GL_CMD(glShaderSource(fragmentShader, 1, &amp;fragmentShaderSource, 0))
+        GLuint programID = glCreateProgram();
+        GL_CMD(glCompileShader(vertexShader))
+        GL_CMD(glCompileShader(fragmentShader))
+        GL_CMD(glAttachShader(programID, vertexShader))
+        GL_CMD(glAttachShader(programID, fragmentShader))
+        GL_CMD(glBindAttribLocation(programID, 0, &quot;InVertex&quot;))
+        GL_CMD(glLinkProgram(programID))
+        programs[index].id = programID;
+        char infoLog[1024];
+        int len;
+        GL_CMD(glGetProgramInfoLog(programID, 1024, &amp;len, infoLog));
+    }
+
+    ProgramInfo programs[NumPrograms];
+
+} gShaderInfo;
+
+#define TEXMAP_GET_SHADER_VAR_LOCATION(prog, var) \
+            if (gShaderInfo.getUniformLocation(TexmapShaderInfo::prog##Program, TexmapShaderInfo::var##Variable, #var) &lt; 0) \
+                    LOG_ERROR(&quot;Couldn't find variable &quot;#var&quot; in program &quot;#prog&quot;\n&quot;);
+#define TEXMAP_BUILD_SHADER(program) gShaderInfo.createShaderProgram(vertexShaderSource##program, fragmentShaderSource##program, TexmapShaderInfo::program##Program);
+
+TextureMapperGL::TextureMapperGL(GraphicsContext* context)
+    : TextureMapper(context)
+    , m_currentProgram(TexmapShaderInfo::TargetProgram)
+{
+    static bool shadersCompiled = false;
+    if (shadersCompiled)
+        return;
+    shadersCompiled = true;
+#ifndef TEXMAP_OPENGL_ES2
+#define OES2_PERCISION_DEFINITIONS \
+    &quot;#define lowp\n#define highp\n&quot;
+#else
+#define OES2_PERCISION_DEFINITIONS
+#endif
+
+    const char* fragmentShaderSourceOpacityAndMask =
+            OES2_PERCISION_DEFINITIONS
+&quot;               uniform sampler2D SourceTexture, MaskTexture;                       \n&quot;
+&quot;               uniform lowp float Opacity;                                         \n&quot;
+&quot;               varying highp vec2 OutTexCoordSource, OutTexCoordMask;              \n&quot;
+&quot;               void main(void)                                                     \n&quot;
+&quot;               {                                                                   \n&quot;
+&quot;                   lowp vec4 color = texture2D(SourceTexture, OutTexCoordSource);  \n&quot;
+&quot;                   lowp vec4 maskColor = texture2D(MaskTexture, OutTexCoordMask);  \n&quot;
+&quot;                   lowp float o = Opacity * maskColor.a;                           \n&quot;
+&quot;                   gl_FragColor = vec4(color.rgb * o, color.a * o);                \n&quot;
+&quot;               }                                                                   \n&quot;;
+
+    const char* vertexShaderSourceOpacityAndMask =
+            OES2_PERCISION_DEFINITIONS
+&quot;               uniform mat4 InMatrix, InSourceMatrix, InMaskMatrix;            \n&quot;
+&quot;               attribute vec4 InVertex;                                        \n&quot;
+&quot;               varying highp vec2 OutTexCoordSource, OutTexCoordMask;          \n&quot;
+&quot;               void main(void)                                                 \n&quot;
+&quot;               {                                                               \n&quot;
+&quot;                   OutTexCoordSource = vec2(InSourceMatrix * InVertex);        \n&quot;
+&quot;                   OutTexCoordMask = vec2(InMaskMatrix * InVertex);            \n&quot;
+&quot;                   gl_Position = InMatrix * InVertex;                          \n&quot;
+&quot;               }                                                               \n&quot;;
+
+    const char* fragmentShaderSourceSimple =
+            OES2_PERCISION_DEFINITIONS
+&quot;               uniform sampler2D SourceTexture;                                    \n&quot;
+&quot;               uniform lowp float Opacity;                                         \n&quot;
+&quot;               varying highp vec2 OutTexCoordSource;                               \n&quot;
+&quot;               void main(void)                                                     \n&quot;
+&quot;               {                                                                   \n&quot;
+&quot;                   lowp vec4 color = texture2D(SourceTexture, OutTexCoordSource);  \n&quot;
+&quot;                   gl_FragColor = vec4(color.rgb * Opacity, color.a * Opacity);    \n&quot;
+&quot;               }                                                                   \n&quot;;
+
+    const char* vertexShaderSourceSimple =
+            OES2_PERCISION_DEFINITIONS
+&quot;               uniform mat4 InMatrix, InSourceMatrix;                      \n&quot;
+&quot;               attribute vec4 InVertex;                                    \n&quot;
+&quot;               varying highp vec2 OutTexCoordSource;                       \n&quot;
+&quot;               void main(void)                                             \n&quot;
+&quot;               {                                                           \n&quot;
+&quot;                   OutTexCoordSource = vec2(InSourceMatrix * InVertex);    \n&quot;
+&quot;                   gl_Position = InMatrix * InVertex;                      \n&quot;
+&quot;               }                                                           \n&quot;;
+
+    const char* fragmentShaderSourceTarget =
+            OES2_PERCISION_DEFINITIONS
+&quot;               uniform sampler2D SourceTexture;                                            \n&quot;
+&quot;               uniform lowp float Opacity;                                                 \n&quot;
+&quot;               varying highp vec2 OutTexCoordSource;                                       \n&quot;
+&quot;               void main(void)                                                             \n&quot;
+&quot;               {                                                                           \n&quot;
+&quot;                   lowp vec4 color = texture2D(SourceTexture, OutTexCoordSource);          \n&quot;
+&quot;                   gl_FragColor = vec4(color.bgr * Opacity, color.a * Opacity);            \n&quot;
+&quot;               }                                                                           \n&quot;;
+
+    const char* vertexShaderSourceTarget = vertexShaderSourceSimple;
+
+    TEXMAP_BUILD_SHADER(Simple)
+    TEXMAP_BUILD_SHADER(OpacityAndMask)
+    TEXMAP_BUILD_SHADER(Target)
+
+    TEXMAP_GET_SHADER_VAR_LOCATION(OpacityAndMask, InMatrix)
+    TEXMAP_GET_SHADER_VAR_LOCATION(OpacityAndMask, InSourceMatrix)
+    TEXMAP_GET_SHADER_VAR_LOCATION(OpacityAndMask, InMaskMatrix)
+    TEXMAP_GET_SHADER_VAR_LOCATION(OpacityAndMask, SourceTexture)
+    TEXMAP_GET_SHADER_VAR_LOCATION(OpacityAndMask, MaskTexture)
+    TEXMAP_GET_SHADER_VAR_LOCATION(OpacityAndMask, Opacity)
+
+    TEXMAP_GET_SHADER_VAR_LOCATION(Simple, InSourceMatrix)
+    TEXMAP_GET_SHADER_VAR_LOCATION(Simple, InMatrix)
+    TEXMAP_GET_SHADER_VAR_LOCATION(Simple, SourceTexture)
+    TEXMAP_GET_SHADER_VAR_LOCATION(Simple, Opacity)
+
+    TEXMAP_GET_SHADER_VAR_LOCATION(Target, InSourceMatrix)
+    TEXMAP_GET_SHADER_VAR_LOCATION(Target, InMatrix)
+    TEXMAP_GET_SHADER_VAR_LOCATION(Target, SourceTexture)
+    TEXMAP_GET_SHADER_VAR_LOCATION(Target, Opacity)
+}
+
+void TextureMapperGL::drawTexture(const BitmapTexture&amp; texture, const IntRect&amp; targetRect, const TransformationMatrix&amp; modelViewMatrix, float opacity, const BitmapTexture* maskTexture)
+{
+    if (!texture.isValid())
+        return;
+
+    const BitmapTextureGL&amp; textureGL = static_cast&lt;const BitmapTextureGL&amp;&gt;(texture);
+
+    TexmapShaderInfo::ShaderProgramIndex program;
+    if (maskTexture)
+        program = TexmapShaderInfo::OpacityAndMaskProgram;
+    else
+        program = TexmapShaderInfo::SimpleProgram;
+
+    const TexmapShaderInfo::ProgramInfo&amp; programInfo = gShaderInfo.programs[program];
+    if (m_currentProgram != program) {
+        GL_CMD(glUseProgram(programInfo.id))
+        GL_CMD(glDisableVertexAttribArray(gShaderInfo.programs[m_currentProgram].vars[TexmapShaderInfo::InVertexVariable]))
+        m_currentProgram = program;
+        GL_CMD(glEnableVertexAttribArray(programInfo.vars[TexmapShaderInfo::InVertexVariable]))
+    }
+
+    GL_CMD(glDisable(GL_DEPTH_TEST))
+    GL_CMD(glDisable(GL_STENCIL_TEST))
+
+    GL_CMD(glActiveTexture(GL_TEXTURE0))
+    GL_CMD(glBindTexture(GL_TEXTURE_2D, textureGL.m_id))
+    GL_CMD(glBindBuffer(GL_ARRAY_BUFFER, 0))
+    const GLfloat unitRect[] = {0, 0, 1, 0, 1, 1, 0, 1};
+    GL_CMD(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, unitRect))
+
+    TransformationMatrix matrix = TransformationMatrix(m_projectionMatrix).multLeft(modelViewMatrix).multLeft(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[] = {textureGL.m_relativeSize.width(), 0, 0, 0,
+                                     0, textureGL.m_relativeSize.height(), 0, 0,
+                                     0, 0, 1, 0,
+                                     0, 0, 0, 1};
+    GL_CMD(glUniformMatrix4fv(programInfo.vars[TexmapShaderInfo::InMatrixVariable], 1, GL_FALSE, m4))
+    GL_CMD(glUniformMatrix4fv(programInfo.vars[TexmapShaderInfo::InSourceMatrixVariable], 1, GL_FALSE, m4src))
+    GL_CMD(glUniform1i(programInfo.vars[TexmapShaderInfo::SourceTextureVariable], 0))
+    GL_CMD(glUniform1f(programInfo.vars[TexmapShaderInfo::OpacityVariable], opacity))
+
+    if (maskTexture &amp;&amp; maskTexture-&gt;isValid()) {
+        const BitmapTextureGL* maskTextureGL = static_cast&lt;const BitmapTextureGL*&gt;(maskTexture);
+        GL_CMD(glActiveTexture(GL_TEXTURE1))
+        GL_CMD(glBindTexture(GL_TEXTURE_2D, maskTextureGL-&gt;m_id))
+        const GLfloat m4mask[] = {maskTextureGL-&gt;m_relativeSize.width(), 0, 0, 0,
+                                         0, maskTextureGL-&gt;m_relativeSize.height(), 0, 0,
+                                         0, 0, 1, 0,
+                                         0, 0, 0, 1};
+        glUniformMatrix4fv(programInfo.vars[TexmapShaderInfo::InMaskMatrixVariable], 1, GL_FALSE, m4mask);
+        GL_CMD(glUniform1i(programInfo.vars[TexmapShaderInfo::MaskTextureVariable], 1))
+        GL_CMD(glActiveTexture(GL_TEXTURE0))
+    }
+
+
+    if (textureGL.m_opaque &amp;&amp; opacity &gt; 0.99 &amp;&amp; !maskTexture)
+        GL_CMD(glDisable(GL_BLEND))
+    else {
+        GL_CMD(glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA))
+        GL_CMD(glEnable(GL_BLEND))
+    }
+
+    GL_CMD(glDrawArrays(GL_TRIANGLE_FAN, 0, 4))
+}
+
+
+const char* TextureMapperGL::type() const
+{
+    return &quot;OpenGL&quot;;
+}
+
+void BitmapTextureGL::reset(const IntSize&amp; newSize, bool opaque)
+{
+    BitmapTexture::reset(newSize, opaque);
+    m_image = 0;
+    IntSize newTextureSize = nextPowerOfTwo(newSize);
+    bool justCreated = false;
+    if (!m_id) {
+        GL_CMD(glGenTextures(1, &amp;m_id))
+        justCreated = true;
+    }
+
+    if (justCreated || newTextureSize.width() &gt; m_textureSize.width() || newTextureSize.height() &gt; m_textureSize.height()) {
+        m_textureSize = newTextureSize;
+        GL_CMD(glBindTexture(GL_TEXTURE_2D, m_id))
+        GL_CMD(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR))
+        GL_CMD(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR))
+        GL_CMD(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE))
+        GL_CMD(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE))
+        GL_CMD(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_textureSize.width(), m_textureSize.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0))
+    }
+    m_actualSize = newSize;
+    m_relativeSize = FloatSize(float(newSize.width()) / m_textureSize.width(), float(newSize.height()) / m_textureSize.height());
+    m_opaque = opaque;
+    m_surfaceNeedsReset = true;
+}
+
+PlatformGraphicsContext* BitmapTextureGL::beginPaint(const IntRect&amp; dirtyRect)
+{
+    m_buffer = RGBA32PremultimpliedBuffer::create();
+    m_dirtyRect = dirtyRect;
+    return m_buffer-&gt;beginPaint(dirtyRect, m_opaque);
+}
+
+void BitmapTextureGL::endPaint()
+{
+    if (!m_buffer)
+        return;
+    m_buffer-&gt;endPaint();
+    GL_CMD(glBindTexture(GL_TEXTURE_2D, m_id))
+    GL_CMD(glTexSubImage2D(GL_TEXTURE_2D, 0, m_dirtyRect.x(), m_dirtyRect.y(), m_dirtyRect.width(), m_dirtyRect.height(), GL_RGBA, GL_UNSIGNED_BYTE, m_buffer-&gt;data()))
+    m_buffer.clear();
+}
+
+struct TexmapGLShaderTextures {
+    struct Entry {
+        GLuint texture;
+        int refCount;
+    };
+    HashMap&lt;NativeImagePtr, Entry&gt; imageToTexture;
+    GLuint findOrCreate(NativeImagePtr image, bool&amp; found)
+    {
+        HashMap&lt;NativeImagePtr, Entry&gt;::iterator it = imageToTexture.find(image);
+        found = false;
+        if (it != imageToTexture.end()) {
+            it-&gt;second.refCount++;
+            found = true;
+            return it-&gt;second.texture;
+        }
+        Entry entry;
+        GL_CMD(glGenTextures(1, &amp;entry.texture));
+        entry.refCount = 1;
+        imageToTexture.add(image, entry);
+        return entry.texture;
+    }
+
+    bool deref(NativeImagePtr image)
+    {
+        HashMap&lt;NativeImagePtr, Entry&gt;::iterator it = imageToTexture.find(image);
+        if (it != imageToTexture.end()) {
+            if (it-&gt;second.refCount &lt; 2) {
+                imageToTexture.remove(it);
+                return false;
+            }
+        }
+        return true;
+    }
+};
+
+static TexmapGLShaderTextures gTextureRepository;
+
+void BitmapTextureGL::setContentsToImage(Image* image)
+{
+    NativeImagePtr nativeImage = image ? image-&gt;nativeImageForCurrentFrame() : 0;
+    if (!image || !nativeImage) {
+        if (m_image)
+            destroy();
+        return;
+    }
+
+    if (nativeImage == m_image)
+        return;
+    bool found = false;
+    GLuint newTextureID = gTextureRepository.findOrCreate(nativeImage, found);
+    if (newTextureID != m_id) {
+        destroy();
+        m_id = newTextureID;
+        reset(image-&gt;size(), false);
+        m_image = nativeImage;
+        if (!found) {
+            GraphicsContext context(beginPaint(IntRect(0, 0, m_textureSize.width(), m_textureSize.height())));
+            context.drawImage(image, DeviceColorSpace, IntPoint(0, 0), CompositeCopy);
+            endPaint();
+        }
+    }
+}
+
+void BitmapTextureGL::destroy()
+{
+    if (m_id &amp;&amp; (!m_image || !gTextureRepository.deref(m_image)))
+        GL_CMD(glDeleteTextures(1, &amp;m_id))
+    if (m_fbo)
+        GL_CMD(glDeleteFramebuffers(1, &amp;m_fbo))
+
+    m_fbo = 0;
+    m_id = 0;
+    m_textureSize = IntSize();
+    m_relativeSize = FloatSize(1, 1);
+}
+
+bool BitmapTextureGL::isValid() const
+{
+    return m_id;
+}
+
+IntSize BitmapTextureGL::size() const
+{
+    return m_textureSize;
+}
+
+static inline TransformationMatrix createProjectionMatrix(const IntSize&amp; size, bool flip)
+{
+    return TransformationMatrix(2.0 / float(size.width()), 0, 0, 0,
+                                0, (flip ? -2.0 : 2.0) / float(size.height()), 0, 0,
+                                0, 0, -0.000001, 0,
+                                -1, flip ? 1 : -1, 0, 1);
+}
+
+void TextureMapperGL::cleanup()
+{
+}
+
+void TextureMapperGL::bindSurface(BitmapTexture *surfacePointer)
+{
+    BitmapTextureGL* surface = static_cast&lt;BitmapTextureGL*&gt;(surfacePointer);
+
+    if (!surface)
+        return;
+    TransformationMatrix matrix = createProjectionMatrix(surface-&gt;size(), false);
+    matrix.translate(-surface-&gt;offset().x(), -surface-&gt;offset().y());
+
+    if (surface-&gt;m_surfaceNeedsReset || !surface-&gt;m_fbo) {
+        if (!surface-&gt;m_fbo)
+            GL_CMD(glGenFramebuffers(1, &amp;surface-&gt;m_fbo))
+        GL_CMD(glBindFramebuffer(GL_FRAMEBUFFER, surface-&gt;m_fbo))
+        GL_CMD(glBindTexture(GL_TEXTURE_2D, 0))
+        GL_CMD(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, surface-&gt;m_id, 0))
+        GL_CMD(glClearColor(0, 0, 0, 0))
+        GL_CMD(glClear(GL_COLOR_BUFFER_BIT))
+        surface-&gt;m_surfaceNeedsReset = false;
+    } else {
+        GL_CMD(glBindFramebuffer(GL_FRAMEBUFFER, surface-&gt;m_fbo))
+    }
+
+    GL_CMD(glViewport(0, 0, surface-&gt;size().width(), surface-&gt;size().height()))
+    m_projectionMatrix = matrix;
+}
+
+void TextureMapperGL::setClip(const IntRect&amp; rect)
+{
+    GL_CMD(glScissor(rect.x(), rect.y(), rect.width(), rect.height()))
+    GL_CMD(glEnable(GL_SCISSOR_TEST))
+}
+
+
+void TextureMapperGL::paintToTarget(const BitmapTexture&amp; aSurface, const IntSize&amp; surfaceSize, const TransformationMatrix&amp; transform, float opacity, const IntRect&amp; visibleRect)
+{
+    const BitmapTextureGL&amp; surface = static_cast&lt;const BitmapTextureGL&amp;&gt;(aSurface);
+
+    // Create the model-view-projection matrix to display on screen.
+    TransformationMatrix matrix = createProjectionMatrix(surfaceSize, true).multLeft(transform).multLeft(
+                TransformationMatrix(
+                        surface.m_actualSize.width(), 0, 0, 0,
+                        0, surface.m_actualSize.height(), 0, 0,
+                        0, 0, 1, 0,
+                        surface.offset().x(), surface.offset().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[] = {surface.m_relativeSize.width(), 0, 0, 0,
+                                     0, surface.m_relativeSize.height(), 0, 0,
+                                     0, 0, 1, 0,
+                                     0, 0, 0, 1};
+
+    // We already blended the alpha in; the result is premultiplied.
+    GL_CMD(glUseProgram(gShaderInfo.programs[TexmapShaderInfo::TargetProgram].id))
+    GL_CMD(glBindFramebuffer(GL_FRAMEBUFFER, 0))
+    GL_CMD(glViewport(0, 0, surfaceSize.width(), surfaceSize.height()))
+    GL_CMD(glDisable(GL_STENCIL_TEST))
+    const TexmapShaderInfo::ProgramInfo&amp; programInfo = gShaderInfo.programs[TexmapShaderInfo::TargetProgram];
+    GL_CMD(glUniform1f(programInfo.vars[TexmapShaderInfo::OpacityVariable], opacity))
+    GL_CMD(glActiveTexture(GL_TEXTURE0))
+    GL_CMD(glBindTexture(GL_TEXTURE_2D, surface.m_id))
+    GL_CMD(glUniform1i(programInfo.vars[TexmapShaderInfo::SourceTextureVariable], 0))
+    GL_CMD(glEnableVertexAttribArray(programInfo.vars[TexmapShaderInfo::InVertexVariable]))
+    GL_CMD(glUniformMatrix4fv(programInfo.vars[TexmapShaderInfo::InMatrixVariable], 1, GL_FALSE, m4))
+    GL_CMD(glUniformMatrix4fv(programInfo.vars[TexmapShaderInfo::InSourceMatrixVariable], 1, GL_FALSE, m4src))
+    GL_CMD(glBindBuffer(GL_ARRAY_BUFFER, 0))
+    const GLfloat unitRect[] = {0, 0, 1, 0, 1, 1, 0, 1};
+    GL_CMD(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, unitRect))
+    GL_CMD(glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA))
+    GL_CMD(glEnable(GL_BLEND))
+    setClip(visibleRect);
+
+    GL_CMD(glDrawArrays(GL_TRIANGLE_FAN, 0, 4))
+    GL_CMD(glDisableVertexAttribArray(programInfo.vars[TexmapShaderInfo::InVertexVariable]))
+    GL_CMD(glUseProgram(0))
+    GL_CMD(glBindBuffer(GL_ARRAY_BUFFER, 0))
+    m_currentProgram = TexmapShaderInfo::TargetProgram;
+}
+
+PassRefPtr&lt;BitmapTexture&gt; TextureMapperGL::createTexture()
+{
+    return adoptRef(new BitmapTextureGL());
+}
+
+};
</ins></span></pre></div>
<a id="trunkWebCoreplatformgraphicsopenglTextureMapperGLh"></a>
<div class="addfile"><h4>Added: trunk/WebCore/platform/graphics/opengl/TextureMapperGL.h (0 => 70452)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/WebCore/platform/graphics/opengl/TextureMapperGL.h                                (rev 0)
+++ trunk/WebCore/platform/graphics/opengl/TextureMapperGL.h        2010-10-25 14:53:19 UTC (rev 70452)
</span><span class="lines">@@ -0,0 +1,83 @@
</span><ins>+/*
+ Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB.  If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
+ */
+
+#ifndef TextureMapperGL_h
+#define TextureMapperGL_h
+
+#if USE(ACCELERATED_COMPOSITING)
+
+#include &quot;FloatQuad.h&quot;
+#include &quot;IntSize.h&quot;
+#include &quot;TransformationMatrix.h&quot;
+#include &quot;texmap/TextureMapper.h&quot;
+
+namespace WebCore {
+
+// An OpenGL-ES2 implementation of TextureMapper.
+class TextureMapperGL : public TextureMapper {
+public:
+    TextureMapperGL(GraphicsContext* gc);
+    virtual ~TextureMapperGL() {}
+
+    // reimps from TextureMapper
+    virtual void drawTexture(const BitmapTexture&amp; texture, const IntRect&amp;, const TransformationMatrix&amp; transform, float opacity, const BitmapTexture* maskTexture);
+    virtual void bindSurface(BitmapTexture* surface);
+    virtual void setClip(const IntRect&amp;);
+    virtual void paintToTarget(const BitmapTexture&amp;, const IntSize&amp;, const TransformationMatrix&amp;, float opacity, const IntRect&amp; visibleRect);
+    virtual bool allowSurfaceForRoot() const { return true; }
+    virtual PassRefPtr&lt;BitmapTexture&gt; createTexture();
+    virtual const char* type() const;
+    virtual void cleanup();
+
+private:
+    TransformationMatrix m_projectionMatrix;
+    int m_currentProgram;
+};
+
+// An offscreen buffer to be rendered by software.
+class RGBA32PremultimpliedBuffer : public RefCounted&lt;RGBA32PremultimpliedBuffer&gt; {
+public:
+    virtual ~RGBA32PremultimpliedBuffer() {}
+    virtual PlatformGraphicsContext* beginPaint(const IntRect&amp; dirtyRect, bool opaque) = 0;
+    virtual void endPaint() = 0;
+    virtual const void* data() const = 0;
+    static PassRefPtr&lt;RGBA32PremultimpliedBuffer&gt; create();
+};
+
+static inline int nextPowerOfTwo(int num)
+{
+    for (int i = 0x10000000; i &gt; 0; i &gt;&gt;= 1) {
+        if (num == i)
+            return num;
+        if (num &amp; i)
+            return (i &lt;&lt; 1);
+    }
+    return 1;
+}
+
+static inline IntSize nextPowerOfTwo(const IntSize&amp; size)
+{
+    return IntSize(nextPowerOfTwo(size.width()), nextPowerOfTwo(size.height()));
+}
+
+};
+
+#endif
+
+#endif
</ins></span></pre>
</div>
</div>

</body>
</html>