Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make TextureMapper work with GraphicsSurface.
https://bugs.webkit.org/show_bug.cgi?id=87738

Add TextureMapperSurfaceBackingStore, a new backing store
that allows to import textures from a GraphicsSurface.
On Mac the GraphicsSurface is backed by an IOSurface
which must be used with a GL_TEXTURE_RECTANGLE_ARB texture.
Therefore it is also necessary to add new shader programs
for directly painting these textures on screen.

Reviewed by Noam Rosenthal.

* platform/graphics/texmap/TextureMapperBackingStore.cpp:
Add a new TextureMapperBackingStore implementation that can directly
reuse textures as they are being passed from the GraphicsSurface.
(WebCore::TextureMapperSurfaceBackingStore::setGraphicsSurface):
(WebCore::TextureMapperSurfaceBackingStore::texture):
(WebCore::TextureMapperSurfaceBackingStore::paintToTextureMapper):
* platform/graphics/texmap/TextureMapperBackingStore.h:
(GraphicsSurfaceData):
(WebCore::GraphicsSurfaceData::setSurface):
(WebCore::GraphicsSurfaceData::GraphicsSurfaceData):
(TextureMapperSurfaceBackingStore):
(WebCore::TextureMapperSurfaceBackingStore::create):
(WebCore::TextureMapperSurfaceBackingStore::~TextureMapperSurfaceBackingStore):
(WebCore::TextureMapperSurfaceBackingStore::TextureMapperSurfaceBackingStore):

* platform/graphics/texmap/TextureMapperGL.cpp:
Add a drawing function for textures of type GL_TEXTURE_RECTANGLE_ARB.
(WebCore::TextureMapperGL::drawTextureRectangleARB):
* platform/graphics/texmap/TextureMapperShaderManager.cpp:
(WebCore::TextureMapperShaderManager::getShaderProgram):
(WebCore::TextureMapperShaderProgramSimple::create):
(WebCore::TextureMapperShaderProgramSimple::initialize):
(WebCore::TextureMapperShaderProgramSolidColor::create):
(WebCore::TextureMapperShaderProgramSolidColor::initialize):
(WebCore::TextureMapperShaderProgramRectSimple::create):
(WebCore::TextureMapperShaderProgramRectSimple::fragmentShaderSource):
(WebCore::TextureMapperShaderProgramOpacityAndMask::create):
(WebCore::TextureMapperShaderProgramOpacityAndMask::initialize):
(WebCore::TextureMapperShaderProgramRectOpacityAndMask::create):
(WebCore::TextureMapperShaderProgramRectOpacityAndMask::fragmentShaderSource):

* platform/graphics/texmap/TextureMapperShaderManager.h:
Add new shader programs that can be used with GL_TEXTURE_RECTANGLE_ARB textures.
This is mainly necessary, because this type of texture uses non-normalized coordinates.
Further move the calls to initializeProgram() from the constructor into a separate
initialize() function, as initializeProgram() calls vertexShaderSource() (and friends)
and we should not call virtual functions in the constructor.
(WebCore::TextureMapperShaderProgram::initialize):
(TextureMapperShaderProgramSimple):
(WebCore::TextureMapperShaderProgramSimple::TextureMapperShaderProgramSimple):
(TextureMapperShaderProgramRectSimple):
(WebCore::TextureMapperShaderProgramRectSimple::TextureMapperShaderProgramRectSimple):
(TextureMapperShaderProgramOpacityAndMask):
(WebCore::TextureMapperShaderProgramOpacityAndMask::TextureMapperShaderProgramOpacityAndMask):
(TextureMapperShaderProgramRectOpacityAndMask):
(WebCore::TextureMapperShaderProgramRectOpacityAndMask::TextureMapperShaderProgramRectOpacityAndMask):
(TextureMapperShaderProgramSolidColor):
(WebCore::TextureMapperShaderProgramSolidColor::TextureMapperShaderProgramSolidColor):

Canonical link: https://commits.webkit.org/105925@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@119247 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
Zeno Albisser committed Jun 1, 2012
1 parent 50da6f8 commit cac2b48
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 11 deletions.
63 changes: 63 additions & 0 deletions Source/WebCore/ChangeLog
@@ -1,3 +1,66 @@
2012-06-01 Zeno Albisser <zeno@webkit.org>

Make TextureMapper work with GraphicsSurface.
https://bugs.webkit.org/show_bug.cgi?id=87738

Add TextureMapperSurfaceBackingStore, a new backing store
that allows to import textures from a GraphicsSurface.
On Mac the GraphicsSurface is backed by an IOSurface
which must be used with a GL_TEXTURE_RECTANGLE_ARB texture.
Therefore it is also necessary to add new shader programs
for directly painting these textures on screen.

Reviewed by Noam Rosenthal.

* platform/graphics/texmap/TextureMapperBackingStore.cpp:
Add a new TextureMapperBackingStore implementation that can directly
reuse textures as they are being passed from the GraphicsSurface.
(WebCore::TextureMapperSurfaceBackingStore::setGraphicsSurface):
(WebCore::TextureMapperSurfaceBackingStore::texture):
(WebCore::TextureMapperSurfaceBackingStore::paintToTextureMapper):
* platform/graphics/texmap/TextureMapperBackingStore.h:
(GraphicsSurfaceData):
(WebCore::GraphicsSurfaceData::setSurface):
(WebCore::GraphicsSurfaceData::GraphicsSurfaceData):
(TextureMapperSurfaceBackingStore):
(WebCore::TextureMapperSurfaceBackingStore::create):
(WebCore::TextureMapperSurfaceBackingStore::~TextureMapperSurfaceBackingStore):
(WebCore::TextureMapperSurfaceBackingStore::TextureMapperSurfaceBackingStore):

* platform/graphics/texmap/TextureMapperGL.cpp:
Add a drawing function for textures of type GL_TEXTURE_RECTANGLE_ARB.
(WebCore::TextureMapperGL::drawTextureRectangleARB):
* platform/graphics/texmap/TextureMapperShaderManager.cpp:
(WebCore::TextureMapperShaderManager::getShaderProgram):
(WebCore::TextureMapperShaderProgramSimple::create):
(WebCore::TextureMapperShaderProgramSimple::initialize):
(WebCore::TextureMapperShaderProgramSolidColor::create):
(WebCore::TextureMapperShaderProgramSolidColor::initialize):
(WebCore::TextureMapperShaderProgramRectSimple::create):
(WebCore::TextureMapperShaderProgramRectSimple::fragmentShaderSource):
(WebCore::TextureMapperShaderProgramOpacityAndMask::create):
(WebCore::TextureMapperShaderProgramOpacityAndMask::initialize):
(WebCore::TextureMapperShaderProgramRectOpacityAndMask::create):
(WebCore::TextureMapperShaderProgramRectOpacityAndMask::fragmentShaderSource):

* platform/graphics/texmap/TextureMapperShaderManager.h:
Add new shader programs that can be used with GL_TEXTURE_RECTANGLE_ARB textures.
This is mainly necessary, because this type of texture uses non-normalized coordinates.
Further move the calls to initializeProgram() from the constructor into a separate
initialize() function, as initializeProgram() calls vertexShaderSource() (and friends)
and we should not call virtual functions in the constructor.
(WebCore::TextureMapperShaderProgram::initialize):
(TextureMapperShaderProgramSimple):
(WebCore::TextureMapperShaderProgramSimple::TextureMapperShaderProgramSimple):
(TextureMapperShaderProgramRectSimple):
(WebCore::TextureMapperShaderProgramRectSimple::TextureMapperShaderProgramRectSimple):
(TextureMapperShaderProgramOpacityAndMask):
(WebCore::TextureMapperShaderProgramOpacityAndMask::TextureMapperShaderProgramOpacityAndMask):
(TextureMapperShaderProgramRectOpacityAndMask):
(WebCore::TextureMapperShaderProgramRectOpacityAndMask::TextureMapperShaderProgramRectOpacityAndMask):
(TextureMapperShaderProgramSolidColor):
(WebCore::TextureMapperShaderProgramSolidColor::TextureMapperShaderProgramSolidColor):

2012-06-01 Christophe Dumez <christophe.dumez@intel.com>

[EFL] EFL port does not enable WEB_INTENTS_TAG flag
Expand Down
Expand Up @@ -21,11 +21,48 @@
#include "TextureMapperBackingStore.h"

#include "GraphicsLayer.h"
#include "GraphicsSurface.h"
#include "ImageBuffer.h"
#include "TextureMapper.h"
#include "TextureMapperGL.h"

namespace WebCore {

#if USE(GRAPHICS_SURFACE)
void TextureMapperSurfaceBackingStore::setGraphicsSurface(uint32_t graphicsSurfaceToken, const IntSize& surfaceSize)
{
if (graphicsSurfaceToken != m_backBufferGraphicsSurfaceData.m_graphicsSurfaceToken) {
GraphicsSurface::Flags surfaceFlags = GraphicsSurface::SupportsTextureTarget
| GraphicsSurface::SupportsSharing;
m_backBufferGraphicsSurfaceData.setSurface(GraphicsSurface::create(surfaceSize, surfaceFlags, graphicsSurfaceToken));
m_graphicsSurfaceSize = surfaceSize;
}

std::swap(m_backBufferGraphicsSurfaceData, m_frontBufferGraphicsSurfaceData);
}

PassRefPtr<BitmapTexture> TextureMapperSurfaceBackingStore::texture() const
{
// FIXME: Instead of just returning an empty texture, we should wrap the texture contents into a BitmapTexture.
RefPtr<BitmapTexture> emptyTexture;
return emptyTexture;
}

void TextureMapperSurfaceBackingStore::paintToTextureMapper(TextureMapper* textureMapper, const FloatRect& targetRect, const TransformationMatrix& transform, float opacity, BitmapTexture* mask)
{
TransformationMatrix adjustedTransform = transform;
adjustedTransform.multiply(TransformationMatrix::rectToRect(FloatRect(FloatPoint::zero(), m_graphicsSurfaceSize), targetRect));
#if OS(DARWIN)
// This is specific to the Mac implementation of GraphicsSurface. IOSurface requires GL_TEXTURE_RECTANGLE_ARB to be used.
static_cast<TextureMapperGL*>(textureMapper)->drawTextureRectangleARB(m_frontBufferGraphicsSurfaceData.m_textureID, 0, m_graphicsSurfaceSize, targetRect, adjustedTransform, opacity, mask);
#else
UNUSED_PARAM(textureMapper);
UNUSED_PARAM(opacity);
UNUSED_PARAM(mask);
#endif
}
#endif

void TextureMapperTile::updateContents(TextureMapper* textureMapper, Image* image, const IntRect& dirtyRect)
{
IntRect targetRect = enclosingIntRect(m_rect);
Expand Down
Expand Up @@ -21,6 +21,7 @@
#define TextureMapperBackingStore_h

#include "FloatRect.h"
#include "GraphicsSurface.h"
#include "Image.h"
#include "TextureMapper.h"
#include "TextureMapperPlatformLayer.h"
Expand All @@ -38,6 +39,39 @@ class TextureMapperBackingStore : public TextureMapperPlatformLayer, public RefC
virtual ~TextureMapperBackingStore() { }
};

#if USE(GRAPHICS_SURFACE)
struct GraphicsSurfaceData {
void setSurface(PassRefPtr<GraphicsSurface> surface)
{
m_graphicsSurface = surface;
m_graphicsSurfaceToken = m_graphicsSurface->exportToken();
m_textureID = m_graphicsSurface->getTextureID();
}

GraphicsSurfaceData()
: m_textureID(0)
, m_graphicsSurfaceToken(0)
{ }

uint32_t m_textureID;
uint32_t m_graphicsSurfaceToken;
RefPtr<WebCore::GraphicsSurface> m_graphicsSurface;
};

class TextureMapperSurfaceBackingStore : public TextureMapperBackingStore {
public:
static PassRefPtr<TextureMapperSurfaceBackingStore> create() { return adoptRef(new TextureMapperSurfaceBackingStore); }
void setGraphicsSurface(uint32_t graphicsSurfaceToken, const IntSize& surfaceSize);
virtual PassRefPtr<BitmapTexture> texture() const;
virtual void paintToTextureMapper(TextureMapper*, const FloatRect&, const TransformationMatrix&, float, BitmapTexture*);
virtual ~TextureMapperSurfaceBackingStore() { }
private:
IntSize m_graphicsSurfaceSize;
GraphicsSurfaceData m_frontBufferGraphicsSurfaceData;
GraphicsSurfaceData m_backBufferGraphicsSurfaceData;
};
#endif

class TextureMapperTile {
public:
inline PassRefPtr<BitmapTexture> texture() const { return m_texture; }
Expand Down
29 changes: 29 additions & 0 deletions Source/WebCore/platform/graphics/texmap/TextureMapperGL.cpp
Expand Up @@ -21,6 +21,7 @@
#include "TextureMapperGL.h"

#include "GraphicsContext.h"
#include "GraphicsSurface.h"
#include "Image.h"
#include "TextureMapperShaderManager.h"
#include "Timer.h"
Expand Down Expand Up @@ -366,6 +367,34 @@ void TextureMapperGL::drawTexture(const BitmapTexture& texture, const FloatRect&
drawTexture(textureGL.id(), textureGL.isOpaque() ? 0 : SupportsBlending, textureGL.size(), targetRect, matrix, opacity, mask);
}

void TextureMapperGL::drawTextureRectangleARB(uint32_t texture, Flags flags, const IntSize& textureSize, const FloatRect& targetRect, const TransformationMatrix& modelViewMatrix, float opacity, const BitmapTexture* maskTexture)
{
RefPtr<TextureMapperShaderProgram> shaderInfo;
if (maskTexture)
shaderInfo = data().sharedGLData().textureMapperShaderManager.getShaderProgram(TextureMapperShaderManager::RectOpacityAndMask);
else
shaderInfo = data().sharedGLData().textureMapperShaderManager.getShaderProgram(TextureMapperShaderManager::RectSimple);
GL_CMD(glUseProgram(shaderInfo->id()));

GL_CMD(glEnableVertexAttribArray(shaderInfo->vertexAttrib()));
GL_CMD(glActiveTexture(GL_TEXTURE0));
GL_CMD(glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture));
GL_CMD(glUniform1i(shaderInfo->sourceTextureVariable(), 0));

const GLfloat m4src[] = {
targetRect.width(), 0, 0, 0,
0, (flags & ShouldFlipTexture) ? -targetRect.height() : targetRect.height(), 0, 0,
0, 0, 1, 0,
0, (flags & ShouldFlipTexture) ? 1 : 0, 0, 1};

GL_CMD(glUniformMatrix4fv(shaderInfo->sourceMatrixVariable(), 1, GL_FALSE, m4src));

shaderInfo->prepare(opacity, maskTexture);

bool needsBlending = (flags & SupportsBlending) || opacity < 0.99 || maskTexture;
drawRect(targetRect, modelViewMatrix, shaderInfo.get(), GL_TRIANGLE_FAN, needsBlending);
}

void TextureMapperGL::drawTexture(uint32_t texture, Flags flags, const IntSize& textureSize, const FloatRect& targetRect, const TransformationMatrix& modelViewMatrix, float opacity, const BitmapTexture* maskTexture)
{
RefPtr<TextureMapperShaderProgram> shaderInfo;
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/platform/graphics/texmap/TextureMapperGL.h
Expand Up @@ -52,6 +52,7 @@ class TextureMapperGL : public TextureMapper {
virtual void drawBorder(const Color&, float borderWidth, const FloatRect& targetRect, const TransformationMatrix& modelViewMatrix = TransformationMatrix()) OVERRIDE;
virtual void drawTexture(const BitmapTexture&, const FloatRect&, const TransformationMatrix&, float opacity, const BitmapTexture* maskTexture) OVERRIDE;
virtual void drawTexture(uint32_t texture, Flags, const IntSize& textureSize, const FloatRect& targetRect, const TransformationMatrix& modelViewMatrix, float opacity, const BitmapTexture* maskTexture);
virtual void drawTextureRectangleARB(uint32_t texture, Flags, const IntSize& textureSize, const FloatRect& targetRect, const TransformationMatrix& modelViewMatrix, float opacity, const BitmapTexture* maskTexture);
virtual void bindSurface(BitmapTexture* surface) OVERRIDE;
virtual void beginClip(const TransformationMatrix&, const FloatRect&) OVERRIDE;
virtual void beginPainting(PaintFlags = 0) OVERRIDE;
Expand Down
Expand Up @@ -55,6 +55,20 @@ static const char* fragmentShaderSourceOpacityAndMask =
}
);

static const char* fragmentShaderSourceRectOpacityAndMask =
FRAGMENT_SHADER(
uniform sampler2DRect SourceTexture, MaskTexture;
uniform lowp float Opacity;
varying highp vec2 OutTexCoordSource, OutTexCoordMask;
void main(void)
{
lowp vec4 color = texture2DRect(SourceTexture, OutTexCoordSource);
lowp vec4 maskColor = texture2DRect(MaskTexture, OutTexCoordMask);
lowp float fragmentAlpha = Opacity * maskColor.a;
gl_FragColor = vec4(color.rgb * fragmentAlpha, color.a * fragmentAlpha);
}
);

static const char* vertexShaderSourceOpacityAndMask =
VERTEX_SHADER(
uniform mat4 InMatrix, InSourceMatrix;
Expand All @@ -80,6 +94,18 @@ static const char* fragmentShaderSourceSimple =
}
);

static const char* fragmentShaderSourceRectSimple =
FRAGMENT_SHADER(
uniform sampler2DRect SourceTexture;
uniform lowp float Opacity;
varying highp vec2 OutTexCoordSource;
void main(void)
{
lowp vec4 color = texture2DRect(SourceTexture, OutTexCoordSource);
gl_FragColor = vec4(color.rgb * Opacity, color.a * Opacity);
}
);

static const char* vertexShaderSourceSimple =
VERTEX_SHADER(
uniform mat4 InMatrix, InSourceMatrix;
Expand Down Expand Up @@ -131,9 +157,15 @@ PassRefPtr<TextureMapperShaderProgram> TextureMapperShaderManager::getShaderProg
case Simple:
program = TextureMapperShaderProgramSimple::create();
break;
case RectSimple:
program = TextureMapperShaderProgramRectSimple::create();
break;
case OpacityAndMask:
program = TextureMapperShaderProgramOpacityAndMask::create();
break;
case RectOpacityAndMask:
program = TextureMapperShaderProgramRectOpacityAndMask::create();
break;
case SolidColor:
program = TextureMapperShaderProgramSolidColor::create();
break;
Expand Down Expand Up @@ -186,10 +218,13 @@ TextureMapperShaderProgram::~TextureMapperShaderProgram()

PassRefPtr<TextureMapperShaderProgramSimple> TextureMapperShaderProgramSimple::create()
{
return adoptRef(new TextureMapperShaderProgramSimple());
RefPtr<TextureMapperShaderProgramSimple> program = adoptRef(new TextureMapperShaderProgramSimple());
// Avoid calling virtual functions in constructor.
program->initialize();
return program.release();
}

TextureMapperShaderProgramSimple::TextureMapperShaderProgramSimple()
void TextureMapperShaderProgramSimple::initialize()
{
initializeProgram();
getUniformLocation(m_sourceMatrixVariable, "InSourceMatrix");
Expand All @@ -215,10 +250,13 @@ void TextureMapperShaderProgramSimple::prepare(float opacity, const BitmapTextur

PassRefPtr<TextureMapperShaderProgramSolidColor> TextureMapperShaderProgramSolidColor::create()
{
return adoptRef(new TextureMapperShaderProgramSolidColor());
RefPtr<TextureMapperShaderProgramSolidColor> program = adoptRef(new TextureMapperShaderProgramSolidColor());
// Avoid calling virtual functions in constructor.
program->initialize();
return program.release();
}

TextureMapperShaderProgramSolidColor::TextureMapperShaderProgramSolidColor()
void TextureMapperShaderProgramSolidColor::initialize()
{
initializeProgram();
getUniformLocation(m_matrixVariable, "InMatrix");
Expand All @@ -235,12 +273,28 @@ const char* TextureMapperShaderProgramSolidColor::fragmentShaderSource() const
return fragmentShaderSourceSolidColor;
}

PassRefPtr<TextureMapperShaderProgramRectSimple> TextureMapperShaderProgramRectSimple::create()
{
RefPtr<TextureMapperShaderProgramRectSimple> program = adoptRef(new TextureMapperShaderProgramRectSimple());
// Avoid calling virtual functions in constructor.
program->initialize();
return program.release();
}

const char* TextureMapperShaderProgramRectSimple::fragmentShaderSource() const
{
return fragmentShaderSourceRectSimple;
}

PassRefPtr<TextureMapperShaderProgramOpacityAndMask> TextureMapperShaderProgramOpacityAndMask::create()
{
return adoptRef(new TextureMapperShaderProgramOpacityAndMask());
RefPtr<TextureMapperShaderProgramOpacityAndMask> program = adoptRef(new TextureMapperShaderProgramOpacityAndMask());
// Avoid calling virtual functions in constructor.
program->initialize();
return program.release();
}

TextureMapperShaderProgramOpacityAndMask::TextureMapperShaderProgramOpacityAndMask()
void TextureMapperShaderProgramOpacityAndMask::initialize()
{
initializeProgram();
getUniformLocation(m_matrixVariable, "InMatrix");
Expand Down Expand Up @@ -273,6 +327,19 @@ void TextureMapperShaderProgramOpacityAndMask::prepare(float opacity, const Bitm
glActiveTexture(GL_TEXTURE0);
}

PassRefPtr<TextureMapperShaderProgramRectOpacityAndMask> TextureMapperShaderProgramRectOpacityAndMask::create()
{
RefPtr<TextureMapperShaderProgramRectOpacityAndMask> program = adoptRef(new TextureMapperShaderProgramRectOpacityAndMask());
// Avoid calling virtual functions in constructor.
program->initialize();
return program.release();
}

const char* TextureMapperShaderProgramRectOpacityAndMask::fragmentShaderSource() const
{
return fragmentShaderSourceRectOpacityAndMask;
}

TextureMapperShaderManager::TextureMapperShaderManager()
{
ASSERT(initializeOpenGLShims());
Expand Down

0 comments on commit cac2b48

Please sign in to comment.