From fba41ea10b5d5703f06ad5514aec6a3e8e8f6f14 Mon Sep 17 00:00:00 2001 From: Gunnar Sletta Date: Mon, 20 Jun 2011 09:20:14 +0200 Subject: [PATCH] Implement caching of the distance fields... Change-Id: I823341b9b53e885dcd004e17e319cf033dc21c45 --- .../scenegraph/qsgdistancefieldglyphcache.cpp | 37 +++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/declarative/scenegraph/qsgdistancefieldglyphcache.cpp b/src/declarative/scenegraph/qsgdistancefieldglyphcache.cpp index 31e654b..0011492 100644 --- a/src/declarative/scenegraph/qsgdistancefieldglyphcache.cpp +++ b/src/declarative/scenegraph/qsgdistancefieldglyphcache.cpp @@ -51,6 +51,7 @@ #include #include #include +#include QT_BEGIN_NAMESPACE @@ -917,11 +918,38 @@ void QSGDistanceFieldGlyphCache::updateCache() resizeTexture((requiredWidth), (requiredHeight)); glBindTexture(GL_TEXTURE_2D, m_textureData->texture); + // ### Remove before final release + static bool cacheDistanceFields = QApplication::arguments().contains("--cache-distance-fields"); + + QString tmpPath = QString::fromLatin1("%1/.qt/").arg(QDir::tempPath()); + QString keyBase = QString::fromLatin1("%1%2%3_%4_%5_%6.fontblob") + .arg(tmpPath) + .arg(m_font.familyName()) + .arg(m_font.styleName()) + .arg(m_font.weight()) + .arg(m_font.style()); + + if (cacheDistanceFields && !QFile::exists(tmpPath)) + QDir(tmpPath).mkpath(tmpPath); + for (int i = 0; i < m_textureData->pendingGlyphs.size(); ++i) { glyph_t glyphIndex = m_textureData->pendingGlyphs.at(i); - QImage glyph = renderDistanceFieldGlyph(glyphIndex); TexCoord c = m_textureData->texCoords.value(glyphIndex); + if (cacheDistanceFields) { + QString key = keyBase.arg(glyphIndex); + QFile file(key); + if (file.open(QFile::ReadOnly)) { + int fileSize = file.size(); + int dim = sqrt(fileSize); + QByteArray blob = file.readAll(); + glTexSubImage2D(GL_TEXTURE_2D, 0, c.x, c.y, dim, dim, GL_ALPHA, GL_UNSIGNED_BYTE, blob.constData()); + continue; + } + } + + QImage glyph = renderDistanceFieldGlyph(glyphIndex); + if (ctx->d_ptr->workaround_brokenFBOReadBack) { QPainter p(&m_textureData->image); p.setCompositionMode(QPainter::CompositionMode_Source); @@ -931,6 +959,13 @@ void QSGDistanceFieldGlyphCache::updateCache() convert_to_Format_Alpha(&glyph); glTexSubImage2D(GL_TEXTURE_2D, 0, c.x, c.y, glyph.width(), glyph.height(), GL_ALPHA, GL_UNSIGNED_BYTE, glyph.constBits()); + + if (cacheDistanceFields) { + QString key = keyBase.arg(glyphIndex); + QFile file(key); + file.open(QFile::WriteOnly); + file.write((const char *) glyph.constBits(), glyph.width() * glyph.height()); + } } m_textureData->pendingGlyphs.reset(); } -- 2.7.4