diff options
author | Konstantin Ritt <ritt.ks@gmail.com> | 2015-12-15 23:38:28 +0400 |
---|---|---|
committer | Konstantin Ritt <ritt.ks@gmail.com> | 2015-12-16 12:46:06 +0000 |
commit | e664251a77417a96e6c3fc27b6d530e208411886 (patch) | |
tree | 490d911b1ceaef52875fa0e12c2019a5ff54cdff | |
parent | 8a401371ea6baeb827c7fa57befd5e183949fca7 (diff) |
Cache font fallback families to reduce startup time
Change-Id: I593708d76d513028ba1b59621b83cbc32e63d4e6
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
-rw-r--r-- | src/gui/text/qfontdatabase.cpp | 55 |
1 files changed, 52 insertions, 3 deletions
diff --git a/src/gui/text/qfontdatabase.cpp b/src/gui/text/qfontdatabase.cpp index 80908122a8..1b9852c20c 100644 --- a/src/gui/text/qfontdatabase.cpp +++ b/src/gui/text/qfontdatabase.cpp @@ -47,6 +47,7 @@ #include <qpa/qplatformfontdatabase.h> #include <qpa/qplatformtheme.h> +#include <QtCore/qcache.h> #include <QtCore/qmath.h> #include <stdlib.h> @@ -413,11 +414,45 @@ void QtFontFamily::ensurePopulated() Q_ASSERT_X(populated, Q_FUNC_INFO, qPrintable(name)); } + +struct FallbacksCacheKey { + QString family; + QFont::Style style; + QFont::StyleHint styleHint; + QChar::Script script; +}; + +inline bool operator==(const FallbacksCacheKey &lhs, const FallbacksCacheKey &rhs) Q_DECL_NOTHROW +{ + return lhs.script == rhs.script && + lhs.styleHint == rhs.styleHint && + lhs.style == rhs.style && + lhs.family == rhs.family; +} + +inline bool operator!=(const FallbacksCacheKey &lhs, const FallbacksCacheKey &rhs) Q_DECL_NOTHROW +{ + return !operator==(lhs, rhs); +} + +inline uint qHash(const FallbacksCacheKey &key, uint seed = 0) Q_DECL_NOTHROW +{ + QtPrivate::QHashCombine hash; + seed = hash(seed, key.family); + seed = hash(seed, int(key.style)); + seed = hash(seed, int(key.styleHint)); + seed = hash(seed, int(key.script)); + return seed; +} + + class QFontDatabasePrivate { public: QFontDatabasePrivate() - : count(0), families(0), reregisterAppFonts(false) + : count(0), families(0), + fallbacksCache(64), + reregisterAppFonts(false) { } ~QFontDatabasePrivate() { @@ -443,6 +478,7 @@ public: int count; QtFontFamily **families; + QCache<FallbacksCacheKey, QStringList> fallbacksCache; struct ApplicationFont { @@ -461,6 +497,8 @@ public: void QFontDatabasePrivate::invalidate() { QFontCache::instance()->clear(); + + fallbacksCache.clear(); free(); QGuiApplicationPrivate::platformIntegration()->fontDatabase()->invalidate(); emit static_cast<QGuiApplication *>(QCoreApplication::instance())->fontDatabaseChanged(); @@ -680,8 +718,10 @@ Q_GLOBAL_STATIC_WITH_ARGS(QMutex, fontDatabaseMutex, (QMutex::Recursive)) void qt_cleanupFontDatabase() { QFontDatabasePrivate *db = privateDb(); - if (db) + if (db) { + db->fallbacksCache.clear(); db->free(); + } } // used in qfontengine_x11.cpp @@ -800,9 +840,15 @@ QStringList QPlatformFontDatabase::fallbacksForFamily(const QString &family, QFo QStringList qt_fallbacksForFamily(const QString &family, QFont::Style style, QFont::StyleHint styleHint, QChar::Script script) { + QFontDatabasePrivate *db = privateDb(); + + const FallbacksCacheKey cacheKey = { family, style, styleHint, script }; + + if (const QStringList *fallbacks = db->fallbacksCache.object(cacheKey)) + return *fallbacks; + // make sure that the db has all fallback families QStringList retList = QGuiApplicationPrivate::platformIntegration()->fontDatabase()->fallbacksForFamily(family,style,styleHint,script); - QFontDatabasePrivate *db = privateDb(); QStringList::iterator i; for (i = retList.begin(); i != retList.end(); ++i) { @@ -818,6 +864,9 @@ QStringList qt_fallbacksForFamily(const QString &family, QFont::Style style, QFo --i; } } + + db->fallbacksCache.insert(cacheKey, new QStringList(retList)); + return retList; } |