Implement caching of the distance fields...
authorGunnar Sletta <gunnar.sletta@nokia.com>
Mon, 20 Jun 2011 07:20:14 +0000 (09:20 +0200)
committerGunnar Sletta <gunnar.sletta@nokia.com>
Mon, 20 Jun 2011 07:20:14 +0000 (09:20 +0200)
Change-Id: I823341b9b53e885dcd004e17e319cf033dc21c45

src/declarative/scenegraph/qsgdistancefieldglyphcache.cpp

index 31e654b..0011492 100644 (file)
@@ -51,6 +51,7 @@
 #include <qglfunctions.h>
 #include <qglyphrun.h>
 #include <qrawfont.h>
+#include <qdir.h>
 
 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();
 }