[TexMap] Clean up TextureMapperShaderProgram
authorzandobersek@gmail.com <zandobersek@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 5 May 2016 06:42:32 +0000 (06:42 +0000)
committerzandobersek@gmail.com <zandobersek@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 5 May 2016 06:42:32 +0000 (06:42 +0000)
https://bugs.webkit.org/show_bug.cgi?id=157302

Reviewed by Carlos Garcia Campos.

Clean up the header and the implementation file of the
TextureMapperShaderProgram class. Reference the GraphicsContext3D
object through a Ref<>, and have the context() return a reference,
not a pointer.

Sort the parts of the implementation file into something more
sensible. StringBuilder usage in create() is improved. getLocation()
only needs to do one lookup in the m_variables map by using ensure().

No new tests -- no change in behavior.

* platform/graphics/surfaces/GLTransportSurface.cpp:
(WebCore::GLTransportSurface::initializeShaderProgram):
* platform/graphics/texmap/TextureMapperGL.cpp:
(WebCore::TextureMapperGLData::SharedGLData::getShaderProgram):
(WebCore::prepareFilterProgram):
* platform/graphics/texmap/TextureMapperShaderProgram.cpp:
(WebCore::TextureMapperShaderProgram::create):
(WebCore::TextureMapperShaderProgram::TextureMapperShaderProgram):
(WebCore::TextureMapperShaderProgram::~TextureMapperShaderProgram):
(WebCore::TextureMapperShaderProgram::setMatrix):
(WebCore::TextureMapperShaderProgram::getLocation):
* platform/graphics/texmap/TextureMapperShaderProgram.h:
(WebCore::TextureMapperShaderProgram::context):

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

Source/WebCore/ChangeLog
Source/WebCore/platform/graphics/surfaces/GLTransportSurface.cpp
Source/WebCore/platform/graphics/texmap/TextureMapperGL.cpp
Source/WebCore/platform/graphics/texmap/TextureMapperShaderProgram.cpp
Source/WebCore/platform/graphics/texmap/TextureMapperShaderProgram.h

index 2d4868b..167659c 100644 (file)
@@ -1,3 +1,35 @@
+2016-05-04  Zan Dobersek  <zdobersek@igalia.com>
+
+        [TexMap] Clean up TextureMapperShaderProgram
+        https://bugs.webkit.org/show_bug.cgi?id=157302
+
+        Reviewed by Carlos Garcia Campos.
+
+        Clean up the header and the implementation file of the
+        TextureMapperShaderProgram class. Reference the GraphicsContext3D
+        object through a Ref<>, and have the context() return a reference,
+        not a pointer.
+
+        Sort the parts of the implementation file into something more
+        sensible. StringBuilder usage in create() is improved. getLocation()
+        only needs to do one lookup in the m_variables map by using ensure().
+
+        No new tests -- no change in behavior.
+
+        * platform/graphics/surfaces/GLTransportSurface.cpp:
+        (WebCore::GLTransportSurface::initializeShaderProgram):
+        * platform/graphics/texmap/TextureMapperGL.cpp:
+        (WebCore::TextureMapperGLData::SharedGLData::getShaderProgram):
+        (WebCore::prepareFilterProgram):
+        * platform/graphics/texmap/TextureMapperShaderProgram.cpp:
+        (WebCore::TextureMapperShaderProgram::create):
+        (WebCore::TextureMapperShaderProgram::TextureMapperShaderProgram):
+        (WebCore::TextureMapperShaderProgram::~TextureMapperShaderProgram):
+        (WebCore::TextureMapperShaderProgram::setMatrix):
+        (WebCore::TextureMapperShaderProgram::getLocation):
+        * platform/graphics/texmap/TextureMapperShaderProgram.h:
+        (WebCore::TextureMapperShaderProgram::context):
+
 2016-05-03  Darin Adler  <darin@apple.com>
 
         Start on dictionary support for IDL, getting enough to work for one dictionary
index d103e5b..6994aec 100644 (file)
@@ -170,7 +170,7 @@ void GLTransportSurface::initializeShaderProgram()
     vertexArrayObjectSupported = m_context3D->getExtensions()->supports("GL_OES_vertex_array_object");
 
     TextureMapperShaderProgram::Options options = TextureMapperShaderProgram::Texture;
-    m_shaderProgram = TextureMapperShaderProgram::create(m_context3D, options);
+    m_shaderProgram = TextureMapperShaderProgram::create(*m_context3D, options);
 
     ::glUseProgram(m_shaderProgram->programID());
     ::glUniform1i(m_shaderProgram->samplerLocation(), 0);
index 625fa90..55a135c 100644 (file)
@@ -347,7 +347,7 @@ static float* gaussianKernel()
 
 static void prepareFilterProgram(TextureMapperShaderProgram* program, const FilterOperation& operation, unsigned pass, const IntSize& size, GC3Duint contentTexture)
 {
-    RefPtr<GraphicsContext3D> context = program->context();
+    Ref<GraphicsContext3D> context = program->context();
     context->useProgram(program->programID());
 
     switch (operation.type()) {
index 07a746d..2d65280 100644 (file)
 
 #if USE(TEXTURE_MAPPER_GL)
 
-#include "LengthFunctions.h"
 #include "Logging.h"
 #include "TextureMapperGL.h"
-
 #include <wtf/text/StringBuilder.h>
 
-#define STRINGIFY(...) #__VA_ARGS__
-
 namespace WebCore {
 
 static inline bool compositingLogEnabled()
@@ -43,76 +39,7 @@ static inline bool compositingLogEnabled()
 #endif
 }
 
-TextureMapperShaderProgram::TextureMapperShaderProgram(PassRefPtr<GraphicsContext3D> context, const String& vertex, const String& fragment)
-    : m_context(context)
-{
-    m_vertexShader = m_context->createShader(GraphicsContext3D::VERTEX_SHADER);
-    m_fragmentShader = m_context->createShader(GraphicsContext3D::FRAGMENT_SHADER);
-    m_context->shaderSource(m_vertexShader, vertex);
-    m_context->shaderSource(m_fragmentShader, fragment);
-    m_id = m_context->createProgram();
-    m_context->compileShader(m_vertexShader);
-    m_context->compileShader(m_fragmentShader);
-    m_context->attachShader(m_id, m_vertexShader);
-    m_context->attachShader(m_id, m_fragmentShader);
-    m_context->linkProgram(m_id);
-
-    if (!compositingLogEnabled())
-        return;
-
-    if (m_context->getError() == GraphicsContext3D::NO_ERROR)
-        return;
-
-    String log = m_context->getShaderInfoLog(m_vertexShader);
-    LOG(Compositing, "Vertex shader log: %s\n", log.utf8().data());
-    log = m_context->getShaderInfoLog(m_fragmentShader);
-    LOG(Compositing, "Fragment shader log: %s\n", log.utf8().data());
-    log = m_context->getProgramInfoLog(m_id);
-    LOG(Compositing, "Program log: %s\n", log.utf8().data());
-}
-
-void TextureMapperShaderProgram::setMatrix(GC3Duint location, const TransformationMatrix& matrix)
-{
-    TransformationMatrix::FloatMatrix4 floatMatrix;
-    matrix.toColumnMajorFloatArray(floatMatrix);
-    m_context->uniformMatrix4fv(location, 1, false, floatMatrix);
-}
-
-GC3Duint TextureMapperShaderProgram::getLocation(const AtomicString& name, VariableType type)
-{
-    HashMap<AtomicString, GC3Duint>::iterator it = m_variables.find(name);
-    if (it != m_variables.end())
-        return it->value;
-
-    GC3Duint location = 0;
-    switch (type) {
-    case UniformVariable:
-        location = m_context->getUniformLocation(m_id, name);
-        break;
-    case AttribVariable:
-        location = m_context->getAttribLocation(m_id, name);
-        break;
-    default:
-        ASSERT_NOT_REACHED();
-        break;
-    }
-
-    m_variables.add(name, location);
-    return location;
-}
-
-TextureMapperShaderProgram::~TextureMapperShaderProgram()
-{
-    Platform3DObject programID = m_id;
-    if (!programID)
-        return;
-
-    m_context->detachShader(programID, m_vertexShader);
-    m_context->deleteShader(m_vertexShader);
-    m_context->detachShader(programID, m_fragmentShader);
-    m_context->deleteShader(m_fragmentShader);
-    m_context->deleteProgram(programID);
-}
+#define STRINGIFY(...) #__VA_ARGS__
 
 #define GLSL_DIRECTIVE(...) "#"#__VA_ARGS__"\n"
 
@@ -373,13 +300,13 @@ static const char* fragmentTemplate =
         }
     );
 
-PassRefPtr<TextureMapperShaderProgram> TextureMapperShaderProgram::create(PassRefPtr<GraphicsContext3D> context, TextureMapperShaderProgram::Options options)
+Ref<TextureMapperShaderProgram> TextureMapperShaderProgram::create(Ref<GraphicsContext3D>&& context, TextureMapperShaderProgram::Options options)
 {
-    StringBuilder shaderBuilder;
 #define SET_APPLIER_FROM_OPTIONS(Applier) \
-    shaderBuilder.append(\
+    optionsApplierBuilder.append(\
         (options & TextureMapperShaderProgram::Applier) ? ENABLE_APPLIER(Applier) : DISABLE_APPLIER(Applier))
 
+    StringBuilder optionsApplierBuilder;
     SET_APPLIER_FROM_OPTIONS(Texture);
     SET_APPLIER_FROM_OPTIONS(Rect);
     SET_APPLIER_FROM_OPTIONS(SolidColor);
@@ -396,16 +323,83 @@ PassRefPtr<TextureMapperShaderProgram> TextureMapperShaderProgram::create(PassRe
     SET_APPLIER_FROM_OPTIONS(BlurFilter);
     SET_APPLIER_FROM_OPTIONS(AlphaBlur);
     SET_APPLIER_FROM_OPTIONS(ContentTexture);
-    StringBuilder vertexBuilder;
-    vertexBuilder.append(shaderBuilder.toString());
-    vertexBuilder.append(vertexTemplate);
-    shaderBuilder.append(fragmentTemplate);
 
-    String vertexSource = vertexBuilder.toString();
-    String fragmentSource = shaderBuilder.toString();
+    StringBuilder vertexShaderBuilder;
+    vertexShaderBuilder.append(optionsApplierBuilder.toString());
+    vertexShaderBuilder.append(vertexTemplate);
+
+    StringBuilder fragmentShaderBuilder;
+    fragmentShaderBuilder.append(optionsApplierBuilder.toString());
+    fragmentShaderBuilder.append(fragmentTemplate);
 
-    return adoptRef(new TextureMapperShaderProgram(context, vertexSource, fragmentSource));
+    return adoptRef(*new TextureMapperShaderProgram(WTFMove(context), vertexShaderBuilder.toString(), fragmentShaderBuilder.toString()));
 }
 
+TextureMapperShaderProgram::TextureMapperShaderProgram(Ref<GraphicsContext3D>&& context, const String& vertex, const String& fragment)
+    : m_context(WTFMove(context))
+{
+    m_vertexShader = m_context->createShader(GraphicsContext3D::VERTEX_SHADER);
+    m_context->shaderSource(m_vertexShader, vertex);
+
+    m_fragmentShader = m_context->createShader(GraphicsContext3D::FRAGMENT_SHADER);
+    m_context->shaderSource(m_fragmentShader, fragment);
+
+    m_id = m_context->createProgram();
+    m_context->compileShader(m_vertexShader);
+    m_context->compileShader(m_fragmentShader);
+    m_context->attachShader(m_id, m_vertexShader);
+    m_context->attachShader(m_id, m_fragmentShader);
+    m_context->linkProgram(m_id);
+
+    if (!compositingLogEnabled())
+        return;
+
+    if (m_context->getError() == GraphicsContext3D::NO_ERROR)
+        return;
+
+    String log = m_context->getShaderInfoLog(m_vertexShader);
+    LOG(Compositing, "Vertex shader log: %s\n", log.utf8().data());
+    log = m_context->getShaderInfoLog(m_fragmentShader);
+    LOG(Compositing, "Fragment shader log: %s\n", log.utf8().data());
+    log = m_context->getProgramInfoLog(m_id);
+    LOG(Compositing, "Program log: %s\n", log.utf8().data());
+}
+
+TextureMapperShaderProgram::~TextureMapperShaderProgram()
+{
+    if (!m_id)
+        return;
+
+    m_context->detachShader(m_id, m_vertexShader);
+    m_context->deleteShader(m_vertexShader);
+    m_context->detachShader(m_id, m_fragmentShader);
+    m_context->deleteShader(m_fragmentShader);
+    m_context->deleteProgram(m_id);
+}
+
+void TextureMapperShaderProgram::setMatrix(GC3Duint location, const TransformationMatrix& matrix)
+{
+    TransformationMatrix::FloatMatrix4 floatMatrix;
+    matrix.toColumnMajorFloatArray(floatMatrix);
+    m_context->uniformMatrix4fv(location, 1, false, floatMatrix);
+}
+
+GC3Duint TextureMapperShaderProgram::getLocation(const AtomicString& name, VariableType type)
+{
+    auto addResult = m_variables.ensure(name,
+        [this, &name, type] {
+            switch (type) {
+            case UniformVariable:
+                return m_context->getUniformLocation(m_id, name);
+            case AttribVariable:
+                return m_context->getAttribLocation(m_id, name);
+            }
+            ASSERT_NOT_REACHED();
+            return 0;
+        });
+    return addResult.iterator->value;
 }
+
+} // namespace WebCore
+
 #endif // USE(TEXTURE_MAPPER_GL)
index 2038416..9a8955a 100644 (file)
 #define TextureMapperShaderProgram_h
 
 #if USE(TEXTURE_MAPPER_GL)
+
 #include "GraphicsContext3D.h"
 #include "TransformationMatrix.h"
 #include <wtf/HashMap.h>
-#include <wtf/PassRefPtr.h>
-#include <wtf/RefPtr.h>
+#include <wtf/Ref.h>
 #include <wtf/text/AtomicStringHash.h>
 
 namespace WebCore {
+
 #define TEXMAP_DECLARE_VARIABLE(Accessor, Name, Type) GC3Duint Accessor##Location() { static const AtomicString name(Name); return getLocation(name, Type); }
 #define TEXMAP_DECLARE_UNIFORM(Accessor) TEXMAP_DECLARE_VARIABLE(Accessor, "u_"#Accessor, UniformVariable)
 #define TEXMAP_DECLARE_ATTRIBUTE(Accessor) TEXMAP_DECLARE_VARIABLE(Accessor, "a_"#Accessor, AttribVariable)
@@ -58,10 +59,11 @@ public:
 
     typedef unsigned Options;
 
-    static PassRefPtr<TextureMapperShaderProgram> create(PassRefPtr<GraphicsContext3D>, Options);
+    static Ref<TextureMapperShaderProgram> create(Ref<GraphicsContext3D>&&, Options);
     virtual ~TextureMapperShaderProgram();
+
     Platform3DObject programID() const { return m_id; }
-    GraphicsContext3D* context() { return m_context.get(); }
+    GraphicsContext3D& context() { return m_context; }
 
     TEXMAP_DECLARE_ATTRIBUTE(vertex)
 
@@ -83,14 +85,15 @@ public:
     void setMatrix(GC3Duint, const TransformationMatrix&);
 
 private:
-    TextureMapperShaderProgram(PassRefPtr<GraphicsContext3D>, const String& vertexShaderSource, const String& fragmentShaderSource);
+    TextureMapperShaderProgram(Ref<GraphicsContext3D>&&, const String& vertexShaderSource, const String& fragmentShaderSource);
+
     Platform3DObject m_vertexShader;
     Platform3DObject m_fragmentShader;
 
     enum VariableType { UniformVariable, AttribVariable };
     GC3Duint getLocation(const AtomicString&, VariableType);
 
-    RefPtr<GraphicsContext3D> m_context;
+    Ref<GraphicsContext3D> m_context;
     Platform3DObject m_id;
     HashMap<AtomicString, GC3Duint> m_variables;
 };