aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/scenegraph/qsgdefaultdistancefieldglyphcache.cpp
diff options
context:
space:
mode:
authorLaszlo Agocs <laszlo.agocs@digia.com>2014-04-08 15:05:21 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-05-23 12:23:09 +0200
commite950557e1133e8aac65a453597ab35400a5b9a10 (patch)
tree835e04f12a584274cec65efaa6071001247417ee /src/quick/scenegraph/qsgdefaultdistancefieldglyphcache.cpp
parent8a28462c13907800caf44c16580e0b2a2ee99f69 (diff)
Avoid direct GL calls in Quick
Change-Id: I9b8673fb3292c9d5ad2f9e8e63f56dc661699be6 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
Diffstat (limited to 'src/quick/scenegraph/qsgdefaultdistancefieldglyphcache.cpp')
-rw-r--r--src/quick/scenegraph/qsgdefaultdistancefieldglyphcache.cpp189
1 files changed, 95 insertions, 94 deletions
diff --git a/src/quick/scenegraph/qsgdefaultdistancefieldglyphcache.cpp b/src/quick/scenegraph/qsgdefaultdistancefieldglyphcache.cpp
index 48b405467b..5257cfda3d 100644
--- a/src/quick/scenegraph/qsgdefaultdistancefieldglyphcache.cpp
+++ b/src/quick/scenegraph/qsgdefaultdistancefieldglyphcache.cpp
@@ -63,8 +63,9 @@ QSGDefaultDistanceFieldGlyphCache::QSGDefaultDistanceFieldGlyphCache(QSGDistance
, m_blitProgram(0)
, m_blitBuffer(QOpenGLBuffer::VertexBuffer)
, m_fboGuard(0)
+ , m_funcs(c->functions())
#if !defined(QT_OPENGL_ES_2)
- , m_funcs(0)
+ , m_coreFuncs(0)
#endif
{
m_blitBuffer.create();
@@ -80,7 +81,7 @@ QSGDefaultDistanceFieldGlyphCache::QSGDefaultDistanceFieldGlyphCache(QSGDistance
QSGDefaultDistanceFieldGlyphCache::~QSGDefaultDistanceFieldGlyphCache()
{
for (int i = 0; i < m_textures.count(); ++i)
- glDeleteTextures(1, &m_textures[i].texture);
+ m_funcs->glDeleteTextures(1, &m_textures[i].texture);
if (m_fboGuard != 0)
m_fboGuard->free();
@@ -144,10 +145,10 @@ void QSGDefaultDistanceFieldGlyphCache::storeGlyphs(const QList<QDistanceField>
QHash<TextureInfo *, QVector<glyph_t> > glyphTextures;
GLint alignment = 4; // default value
- glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
+ m_funcs->glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
// Distance field data is always tightly packed
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+ m_funcs->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
for (int i = 0; i < glyphs.size(); ++i) {
QDistanceField glyph = glyphs.at(i);
@@ -156,7 +157,7 @@ void QSGDefaultDistanceFieldGlyphCache::storeGlyphs(const QList<QDistanceField>
TextureInfo *texInfo = m_glyphsTexture.value(glyphIndex);
resizeTexture(texInfo, texInfo->allocatedArea.width(), texInfo->allocatedArea.height());
- glBindTexture(GL_TEXTURE_2D, texInfo->texture);
+ m_funcs->glBindTexture(GL_TEXTURE_2D, texInfo->texture);
glyphTextures[texInfo].append(glyphIndex);
@@ -181,21 +182,21 @@ void QSGDefaultDistanceFieldGlyphCache::storeGlyphs(const QList<QDistanceField>
#endif
if (useTextureUploadWorkaround()) {
for (int i = 0; i < glyph.height(); ++i) {
- glTexSubImage2D(GL_TEXTURE_2D, 0,
- c.x, c.y + i, glyph.width(),1,
- format, GL_UNSIGNED_BYTE,
- glyph.scanLine(i));
+ m_funcs->glTexSubImage2D(GL_TEXTURE_2D, 0,
+ c.x, c.y + i, glyph.width(),1,
+ format, GL_UNSIGNED_BYTE,
+ glyph.scanLine(i));
}
} else {
- glTexSubImage2D(GL_TEXTURE_2D, 0,
- c.x, c.y, glyph.width(), glyph.height(),
- format, GL_UNSIGNED_BYTE,
- glyph.constBits());
+ m_funcs->glTexSubImage2D(GL_TEXTURE_2D, 0,
+ c.x, c.y, glyph.width(), glyph.height(),
+ format, GL_UNSIGNED_BYTE,
+ glyph.constBits());
}
}
// restore to previous alignment
- glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
+ m_funcs->glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
QHash<TextureInfo *, QVector<glyph_t> >::const_iterator i;
for (i = glyphTextures.constBegin(); i != glyphTextures.constEnd(); ++i) {
@@ -221,18 +222,18 @@ void QSGDefaultDistanceFieldGlyphCache::createTexture(TextureInfo *texInfo, int
if (useTextureResizeWorkaround() && texInfo->image.isNull())
texInfo->image = QDistanceField(width, height);
- while (glGetError() != GL_NO_ERROR) { }
+ while (m_funcs->glGetError() != GL_NO_ERROR) { }
- glGenTextures(1, &texInfo->texture);
- glBindTexture(GL_TEXTURE_2D, texInfo->texture);
+ m_funcs->glGenTextures(1, &texInfo->texture);
+ m_funcs->glBindTexture(GL_TEXTURE_2D, texInfo->texture);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
#if !defined(QT_OPENGL_ES_2)
if (!QOpenGLContext::currentContext()->isOpenGLES())
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
+ m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
const GLint internalFormat = isCoreProfile() ? GL_R8 : GL_ALPHA;
const GLenum format = isCoreProfile() ? GL_RED : GL_ALPHA;
#else
@@ -240,14 +241,14 @@ void QSGDefaultDistanceFieldGlyphCache::createTexture(TextureInfo *texInfo, int
const GLenum format = GL_ALPHA;
#endif
- glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, GL_UNSIGNED_BYTE, 0);
+ m_funcs->glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, GL_UNSIGNED_BYTE, 0);
texInfo->size = QSize(width, height);
- GLuint error = glGetError();
+ GLuint error = m_funcs->glGetError();
if (error != GL_NO_ERROR) {
- glBindTexture(GL_TEXTURE_2D, 0);
- glDeleteTextures(1, &texInfo->texture);
+ m_funcs->glBindTexture(GL_TEXTURE_2D, 0);
+ m_funcs->glDeleteTextures(1, &texInfo->texture);
texInfo->texture = 0;
}
@@ -281,42 +282,42 @@ void QSGDefaultDistanceFieldGlyphCache::resizeTexture(TextureInfo *texInfo, int
// For an OpenGL Core Profile we can use http://www.opengl.org/wiki/Framebuffer#Blitting
// to efficiently copy the contents of the old texture to the new texture
// TODO: Use ARB_copy_image if available of if we have >=4.3 context
- if (!m_funcs) {
- m_funcs = ctx->versionFunctions<QOpenGLFunctions_3_2_Core>();
- Q_ASSERT(m_funcs);
- m_funcs->initializeOpenGLFunctions();
+ if (!m_coreFuncs) {
+ m_coreFuncs = ctx->versionFunctions<QOpenGLFunctions_3_2_Core>();
+ Q_ASSERT(m_coreFuncs);
+ m_coreFuncs->initializeOpenGLFunctions();
}
// Create a framebuffer object to which we can attach our old and new textures (to
// the first two color buffer attachment points)
if (!m_fboGuard) {
GLuint fbo;
- m_funcs->glGenFramebuffers(1, &fbo);
+ m_coreFuncs->glGenFramebuffers(1, &fbo);
m_fboGuard = new QOpenGLSharedResourceGuard(ctx, fbo, freeFramebufferFunc);
}
// Bind the FBO to both the GL_READ_FRAMEBUFFER? and GL_DRAW_FRAMEBUFFER targets
- m_funcs->glBindFramebuffer(GL_FRAMEBUFFER, m_fboGuard->id());
+ m_coreFuncs->glBindFramebuffer(GL_FRAMEBUFFER, m_fboGuard->id());
// Bind the old texture to GL_COLOR_ATTACHMENT0
- m_funcs->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
+ m_coreFuncs->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, oldTexture, 0);
// Bind the new texture to GL_COLOR_ATTACHMENT1
- m_funcs->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1,
+ m_coreFuncs->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1,
GL_TEXTURE_2D, texInfo->texture, 0);
// Set the source and destination buffers
- m_funcs->glReadBuffer(GL_COLOR_ATTACHMENT0);
- m_funcs->glDrawBuffer(GL_COLOR_ATTACHMENT1);
+ m_coreFuncs->glReadBuffer(GL_COLOR_ATTACHMENT0);
+ m_coreFuncs->glDrawBuffer(GL_COLOR_ATTACHMENT1);
// Do the blit
- m_funcs->glBlitFramebuffer(0, 0, oldWidth, oldHeight,
+ m_coreFuncs->glBlitFramebuffer(0, 0, oldWidth, oldHeight,
0, 0, oldWidth, oldHeight,
GL_COLOR_BUFFER_BIT, GL_NEAREST);
// Reset the default framebuffer
- m_funcs->glBindFramebuffer(GL_FRAMEBUFFER, 0);
+ m_coreFuncs->glBindFramebuffer(GL_FRAMEBUFFER, 0);
return;
} else if (useTextureResizeWorkaround()) {
@@ -324,8 +325,8 @@ void QSGDefaultDistanceFieldGlyphCache::resizeTexture(TextureInfo *texInfo, int
if (useTextureResizeWorkaround()) {
#endif
GLint alignment = 4; // default value
- glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+ m_funcs->glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
+ m_funcs->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
#if !defined(QT_OPENGL_ES_2)
const GLenum format = isCoreProfile() ? GL_RED : GL_ALPHA;
@@ -335,22 +336,22 @@ void QSGDefaultDistanceFieldGlyphCache::resizeTexture(TextureInfo *texInfo, int
if (useTextureUploadWorkaround()) {
for (int i = 0; i < texInfo->image.height(); ++i) {
- glTexSubImage2D(GL_TEXTURE_2D, 0,
- 0, i, oldWidth, 1,
- format, GL_UNSIGNED_BYTE,
- texInfo->image.scanLine(i));
+ m_funcs->glTexSubImage2D(GL_TEXTURE_2D, 0,
+ 0, i, oldWidth, 1,
+ format, GL_UNSIGNED_BYTE,
+ texInfo->image.scanLine(i));
}
} else {
- glTexSubImage2D(GL_TEXTURE_2D, 0,
- 0, 0, oldWidth, oldHeight,
- format, GL_UNSIGNED_BYTE,
- texInfo->image.constBits());
+ m_funcs->glTexSubImage2D(GL_TEXTURE_2D, 0,
+ 0, 0, oldWidth, oldHeight,
+ format, GL_UNSIGNED_BYTE,
+ texInfo->image.constBits());
}
- glPixelStorei(GL_UNPACK_ALIGNMENT, alignment); // restore to previous value
+ m_funcs->glPixelStorei(GL_UNPACK_ALIGNMENT, alignment); // restore to previous value
texInfo->image = texInfo->image.copy(0, 0, width, height);
- glDeleteTextures(1, &oldTexture);
+ m_funcs->glDeleteTextures(1, &oldTexture);
return;
}
@@ -361,30 +362,30 @@ void QSGDefaultDistanceFieldGlyphCache::resizeTexture(TextureInfo *texInfo, int
if (!m_fboGuard) {
GLuint fbo;
- ctx->functions()->glGenFramebuffers(1, &fbo);
+ m_funcs->glGenFramebuffers(1, &fbo);
m_fboGuard = new QOpenGLSharedResourceGuard(ctx, fbo, freeFramebufferFunc);
}
- ctx->functions()->glBindFramebuffer(GL_FRAMEBUFFER, m_fboGuard->id());
+ m_funcs->glBindFramebuffer(GL_FRAMEBUFFER, m_fboGuard->id());
GLuint tmp_texture;
- glGenTextures(1, &tmp_texture);
- glBindTexture(GL_TEXTURE_2D, tmp_texture);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ m_funcs->glGenTextures(1, &tmp_texture);
+ m_funcs->glBindTexture(GL_TEXTURE_2D, tmp_texture);
+ m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
#if !defined(QT_OPENGL_ES_2)
if (!ctx->isOpenGLES())
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
+ m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
#endif
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, oldWidth, oldHeight, 0,
- GL_RGBA, GL_UNSIGNED_BYTE, NULL);
- glBindTexture(GL_TEXTURE_2D, 0);
- ctx->functions()->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
- GL_TEXTURE_2D, tmp_texture, 0);
+ m_funcs->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, oldWidth, oldHeight, 0,
+ GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+ m_funcs->glBindTexture(GL_TEXTURE_2D, 0);
+ m_funcs->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
+ GL_TEXTURE_2D, tmp_texture, 0);
- ctx->functions()->glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, oldTexture);
+ m_funcs->glActiveTexture(GL_TEXTURE0);
+ m_funcs->glBindTexture(GL_TEXTURE_2D, oldTexture);
// save current render states
GLboolean stencilTestEnabled;
@@ -393,19 +394,19 @@ void QSGDefaultDistanceFieldGlyphCache::resizeTexture(TextureInfo *texInfo, int
GLboolean blendEnabled;
GLint viewport[4];
GLint oldProgram;
- glGetBooleanv(GL_STENCIL_TEST, &stencilTestEnabled);
- glGetBooleanv(GL_DEPTH_TEST, &depthTestEnabled);
- glGetBooleanv(GL_SCISSOR_TEST, &scissorTestEnabled);
- glGetBooleanv(GL_BLEND, &blendEnabled);
- glGetIntegerv(GL_VIEWPORT, &viewport[0]);
- glGetIntegerv(GL_CURRENT_PROGRAM, &oldProgram);
+ m_funcs->glGetBooleanv(GL_STENCIL_TEST, &stencilTestEnabled);
+ m_funcs->glGetBooleanv(GL_DEPTH_TEST, &depthTestEnabled);
+ m_funcs->glGetBooleanv(GL_SCISSOR_TEST, &scissorTestEnabled);
+ m_funcs->glGetBooleanv(GL_BLEND, &blendEnabled);
+ m_funcs->glGetIntegerv(GL_VIEWPORT, &viewport[0]);
+ m_funcs->glGetIntegerv(GL_CURRENT_PROGRAM, &oldProgram);
- glDisable(GL_STENCIL_TEST);
- glDisable(GL_DEPTH_TEST);
- glDisable(GL_SCISSOR_TEST);
- glDisable(GL_BLEND);
+ m_funcs->glDisable(GL_STENCIL_TEST);
+ m_funcs->glDisable(GL_DEPTH_TEST);
+ m_funcs->glDisable(GL_SCISSOR_TEST);
+ m_funcs->glDisable(GL_BLEND);
- glViewport(0, 0, oldWidth, oldHeight);
+ m_funcs->glViewport(0, 0, oldWidth, oldHeight);
const bool vaoInit = m_vao.isCreated();
if (isCoreProfile()) {
@@ -425,35 +426,35 @@ void QSGDefaultDistanceFieldGlyphCache::resizeTexture(TextureInfo *texInfo, int
m_blitProgram->disableAttributeArray(int(QT_OPACITY_ATTR));
m_blitProgram->setUniformValue("imageTexture", GLuint(0));
- glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+ m_funcs->glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
- glBindTexture(GL_TEXTURE_2D, texInfo->texture);
+ m_funcs->glBindTexture(GL_TEXTURE_2D, texInfo->texture);
if (useTextureUploadWorkaround()) {
for (int i = 0; i < oldHeight; ++i)
- glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, i, 0, i, oldWidth, 1);
+ m_funcs->glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, i, 0, i, oldWidth, 1);
} else {
- glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, oldWidth, oldHeight);
+ m_funcs->glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, oldWidth, oldHeight);
}
- ctx->functions()->glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
- GL_RENDERBUFFER, 0);
- glDeleteTextures(1, &tmp_texture);
- glDeleteTextures(1, &oldTexture);
+ m_funcs->glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
+ GL_RENDERBUFFER, 0);
+ m_funcs->glDeleteTextures(1, &tmp_texture);
+ m_funcs->glDeleteTextures(1, &oldTexture);
- ctx->functions()->glBindFramebuffer(GL_FRAMEBUFFER, 0);
+ m_funcs->glBindFramebuffer(GL_FRAMEBUFFER, 0);
// restore render states
if (stencilTestEnabled)
- glEnable(GL_STENCIL_TEST);
+ m_funcs->glEnable(GL_STENCIL_TEST);
if (depthTestEnabled)
- glEnable(GL_DEPTH_TEST);
+ m_funcs->glEnable(GL_DEPTH_TEST);
if (scissorTestEnabled)
- glEnable(GL_SCISSOR_TEST);
+ m_funcs->glEnable(GL_SCISSOR_TEST);
if (blendEnabled)
- glEnable(GL_BLEND);
- glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
- ctx->functions()->glUseProgram(oldProgram);
+ m_funcs->glEnable(GL_BLEND);
+ m_funcs->glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
+ m_funcs->glUseProgram(oldProgram);
m_blitProgram->disableAttributeArray(int(QT_VERTEX_COORDS_ATTR));
m_blitProgram->disableAttributeArray(int(QT_TEXTURE_COORDS_ATTR));
@@ -479,7 +480,7 @@ bool QSGDefaultDistanceFieldGlyphCache::useTextureUploadWorkaround() const
static bool set = false;
static bool useWorkaround = false;
if (!set) {
- useWorkaround = qstrcmp(reinterpret_cast<const char*>(glGetString(GL_RENDERER)),
+ useWorkaround = qstrcmp(reinterpret_cast<const char*>(m_funcs->glGetString(GL_RENDERER)),
"Mali-400 MP") == 0;
set = true;
}
@@ -489,7 +490,7 @@ bool QSGDefaultDistanceFieldGlyphCache::useTextureUploadWorkaround() const
int QSGDefaultDistanceFieldGlyphCache::maxTextureSize() const
{
if (!m_maxTextureSize)
- glGetIntegerv(GL_MAX_TEXTURE_SIZE, &m_maxTextureSize);
+ m_funcs->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &m_maxTextureSize);
return m_maxTextureSize;
}