From b271abeaf11c9fe0a05d8405f03f6a20218214ed Mon Sep 17 00:00:00 2001 From: Yoann Lopes Date: Fri, 16 Mar 2012 12:09:29 +0100 Subject: Use a single distance-field cache instance for all sizes of a given font. Previously we had a different cache instance for each font size, but they all shared the same textures and positions. Only the glyph metrics were specific to each font size. The metrics for each font sizes are now calculated by scaling the metrics of the distance-field size (54px). Change-Id: I0d9016990dedd93318893506afb1dba7a5249e2e Reviewed-by: Eskil Abrahamsen Blomfeldt --- .../qsgdefaultdistancefieldglyphcache_p.h | 161 +++++++-------------- 1 file changed, 53 insertions(+), 108 deletions(-) (limited to 'src/quick/scenegraph/qsgdefaultdistancefieldglyphcache_p.h') diff --git a/src/quick/scenegraph/qsgdefaultdistancefieldglyphcache_p.h b/src/quick/scenegraph/qsgdefaultdistancefieldglyphcache_p.h index 12bbcce060..38cc649b3a 100644 --- a/src/quick/scenegraph/qsgdefaultdistancefieldglyphcache_p.h +++ b/src/quick/scenegraph/qsgdefaultdistancefieldglyphcache_p.h @@ -53,6 +53,7 @@ class Q_QUICK_EXPORT QSGDefaultDistanceFieldGlyphCache : public QSGDistanceField { public: QSGDefaultDistanceFieldGlyphCache(QSGDistanceFieldGlyphCacheManager *man, QOpenGLContext *c, const QRawFont &font); + virtual ~QSGDefaultDistanceFieldGlyphCache(); void requestGlyphs(const QSet &glyphs); void storeGlyphs(const QHash &glyphs); @@ -60,8 +61,8 @@ public: void releaseGlyphs(const QSet &glyphs); bool cacheIsFull() const { - return m_textureData->textures.count() == m_maxTextureCount - && textureIsFull(m_textureData->currentTexture); + return m_textures.count() == m_maxTextureCount + && textureIsFull(m_currentTexture); } bool useWorkaroundBrokenFBOReadback() const; int maxTextureSize() const; @@ -70,123 +71,67 @@ public: int maxTextureCount() const { return m_maxTextureCount; } private: - mutable int m_maxTextureSize; - int m_maxTextureCount; + struct TextureInfo { + GLuint texture; + QSize size; + int currX; + int currY; + QImage image; + + TextureInfo() : texture(0), currX(0), currY(0) + { } + }; - struct DistanceFieldTextureData : public QOpenGLSharedResource { - struct TextureInfo { - GLuint texture; - QSize size; - int currX; - int currY; - QImage image; - - TextureInfo() : texture(0), currX(0), currY(0) - { } - }; - - TextureInfo *currentTexture; - QList textures; - QHash glyphsTexture; - GLuint fbo; - QSet unusedGlyphs; - - QOpenGLShaderProgram *blitProgram; - GLfloat blitVertexCoordinateArray[8]; - GLfloat blitTextureCoordinateArray[8]; - - TextureInfo *addTexture() - { - textures.append(TextureInfo()); - return &textures.last(); - } + void createTexture(TextureInfo * texInfo, int width, int height); + void resizeTexture(TextureInfo * texInfo, int width, int height); + bool textureIsFull (const TextureInfo *tex) const { return tex->currY >= maxTextureSize(); } - DistanceFieldTextureData(QOpenGLContext *ctx) - : QOpenGLSharedResource(ctx->shareGroup()) - , fbo(0) - , blitProgram(0) - { - currentTexture = addTexture(); - - blitVertexCoordinateArray[0] = -1.0f; - blitVertexCoordinateArray[1] = -1.0f; - blitVertexCoordinateArray[2] = 1.0f; - blitVertexCoordinateArray[3] = -1.0f; - blitVertexCoordinateArray[4] = 1.0f; - blitVertexCoordinateArray[5] = 1.0f; - blitVertexCoordinateArray[6] = -1.0f; - blitVertexCoordinateArray[7] = 1.0f; - - blitTextureCoordinateArray[0] = 0.0f; - blitTextureCoordinateArray[1] = 0.0f; - blitTextureCoordinateArray[2] = 1.0f; - blitTextureCoordinateArray[3] = 0.0f; - blitTextureCoordinateArray[4] = 1.0f; - blitTextureCoordinateArray[5] = 1.0f; - blitTextureCoordinateArray[6] = 0.0f; - blitTextureCoordinateArray[7] = 1.0f; - } + TextureInfo *createTextureInfo() + { + m_textures.append(TextureInfo()); + return &m_textures.last(); + } - void invalidateResource() + void createBlitProgram() + { + m_blitProgram = new QOpenGLShaderProgram; { - glyphsTexture.clear(); - textures.clear(); - fbo = 0; - delete blitProgram; - blitProgram = 0; + QString source; + source.append(QLatin1String(qopenglslMainWithTexCoordsVertexShader)); + source.append(QLatin1String(qopenglslUntransformedPositionVertexShader)); - currentTexture = addTexture(); - } + QOpenGLShader *vertexShader = new QOpenGLShader(QOpenGLShader::Vertex, m_blitProgram); + vertexShader->compileSourceCode(source); - void freeResource(QOpenGLContext *ctx) - { - glyphsTexture.clear(); - for (int i = 0; i < textures.count(); ++i) - glDeleteTextures(1, &textures[i].texture); - textures.clear(); - ctx->functions()->glDeleteFramebuffers(1, &fbo); - delete blitProgram; - blitProgram = 0; - - currentTexture = addTexture(); + m_blitProgram->addShader(vertexShader); } - - void createBlitProgram() { - blitProgram = new QOpenGLShaderProgram; - { - QString source; - source.append(QLatin1String(qopenglslMainWithTexCoordsVertexShader)); - source.append(QLatin1String(qopenglslUntransformedPositionVertexShader)); - - QOpenGLShader *vertexShader = new QOpenGLShader(QOpenGLShader::Vertex, blitProgram); - vertexShader->compileSourceCode(source); - - blitProgram->addShader(vertexShader); - } - { - QString source; - source.append(QLatin1String(qopenglslMainFragmentShader)); - source.append(QLatin1String(qopenglslImageSrcFragmentShader)); - - QOpenGLShader *fragmentShader = new QOpenGLShader(QOpenGLShader::Fragment, blitProgram); - fragmentShader->compileSourceCode(source); - - blitProgram->addShader(fragmentShader); - } - blitProgram->bindAttributeLocation("vertexCoordsArray", QT_VERTEX_COORDS_ATTR); - blitProgram->bindAttributeLocation("textureCoordArray", QT_TEXTURE_COORDS_ATTR); - blitProgram->link(); + QString source; + source.append(QLatin1String(qopenglslMainFragmentShader)); + source.append(QLatin1String(qopenglslImageSrcFragmentShader)); + + QOpenGLShader *fragmentShader = new QOpenGLShader(QOpenGLShader::Fragment, m_blitProgram); + fragmentShader->compileSourceCode(source); + + m_blitProgram->addShader(fragmentShader); } - }; + m_blitProgram->bindAttributeLocation("vertexCoordsArray", QT_VERTEX_COORDS_ATTR); + m_blitProgram->bindAttributeLocation("textureCoordArray", QT_TEXTURE_COORDS_ATTR); + m_blitProgram->link(); + } + + mutable int m_maxTextureSize; + int m_maxTextureCount; - void createTexture(DistanceFieldTextureData::TextureInfo * texInfo, int width, int height); - void resizeTexture(DistanceFieldTextureData::TextureInfo * texInfo, int width, int height); - bool textureIsFull (const DistanceFieldTextureData::TextureInfo *tex) const { return tex->currY >= maxTextureSize(); } + TextureInfo *m_currentTexture; + QList m_textures; + QHash m_glyphsTexture; + GLuint m_fbo; + QSet m_unusedGlyphs; - DistanceFieldTextureData *textureData(QOpenGLContext *c); - DistanceFieldTextureData *m_textureData; - static QHash m_textures_data; + QOpenGLShaderProgram *m_blitProgram; + GLfloat m_blitVertexCoordinateArray[8]; + GLfloat m_blitTextureCoordinateArray[8]; }; QT_END_NAMESPACE -- cgit v1.2.3