summaryrefslogtreecommitdiffstats
path: root/src/gui
diff options
context:
space:
mode:
authorEskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>2019-06-17 12:41:59 +0200
committerEskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>2019-06-28 19:45:32 +0000
commit9520ba5a73cd9d6cb8ef47f10837b92494c8221e (patch)
tree510d35ddf6fa3c9d79394df1012493770d378366 /src/gui
parent1ce832648e05ad6226071ec38a5da0ef08d3ab8e (diff)
Improve performance when loading application fonts
Since it was added in Qt 4.2, addAppFont() has been written to first register the font, then immediately invalidate the font database and later reload the font again the next time the db is used. This caused all application fonts to be reloaded *at least* once, an operation which can be quite heavy for large fonts, such as CJK fonts. If an application loaded multiple fonts at different stages of execution, you could end up loading the same fonts a large number of times, since all application fonts would be reregistered every time a new one was added. When calling removeApplicationFont(), this is okay-ish, since we need to remove all traces of the font from the platform database and clearing the whole thing is a convenient way of making sure there is nothing left. There might be more efficient ways of doing this, but unloading fonts is not a common operation, so it is fine to keep this behavior there. This change removes the invalidation of the font database from addAppFont(), since this should not be necessary as long as we are adding fonts. It also removes the reregisterAppFonts flag, which was a bit of a convoluted way of saying that the database had been invalidated and needed repopulating. Instead, we use the same mechanism as for repopulating the system database: We just check if it is currently empty, which means it has been invalidated and the application fonts have to be reregistered. It does not touch the logic in qt_cleanupFontDatabase(), which is kind of broken (it will leave application fonts in memory and reregister them if the application continues to run). But this is only actually called during shutdown (from application destructor). [ChangeLog][QtGui][Text] Fixed an issue where application fonts would be parsed multiple times, causing some unnecessary overhead when during application startup. Task-number: QTBUG-76239 Change-Id: Idfb62f73133b55e0909bb398631c8e762442e95b Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'src/gui')
-rw-r--r--src/gui/text/qfontdatabase.cpp21
1 files changed, 8 insertions, 13 deletions
diff --git a/src/gui/text/qfontdatabase.cpp b/src/gui/text/qfontdatabase.cpp
index 5350a9c5ec..562ee3e2b1 100644
--- a/src/gui/text/qfontdatabase.cpp
+++ b/src/gui/text/qfontdatabase.cpp
@@ -451,8 +451,7 @@ class QFontDatabasePrivate
public:
QFontDatabasePrivate()
: count(0), families(0),
- fallbacksCache(64),
- reregisterAppFonts(false)
+ fallbacksCache(64)
{ }
~QFontDatabasePrivate() {
@@ -488,7 +487,6 @@ public:
};
QVector<ApplicationFont> applicationFonts;
int addAppFont(const QByteArray &fontData, const QString &fileName);
- bool reregisterAppFonts;
bool isApplicationFont(const QString &fileName);
void invalidate();
@@ -897,15 +895,12 @@ static void initializeDb()
QFontDatabasePrivate *db = privateDb();
// init by asking for the platformfontdb for the first time or after invalidation
- if (!db->count)
+ if (!db->count) {
QGuiApplicationPrivate::platformIntegration()->fontDatabase()->populateFontDatabase();
-
- if (db->reregisterAppFonts) {
for (int i = 0; i < db->applicationFonts.count(); i++) {
if (!db->applicationFonts.at(i).families.isEmpty())
registerFont(&db->applicationFonts[i]);
}
- db->reregisterAppFonts = false;
}
}
@@ -1033,11 +1028,7 @@ QFontEngine *loadEngine(int script, const QFontDef &request,
static void registerFont(QFontDatabasePrivate::ApplicationFont *fnt)
{
- QFontDatabasePrivate *db = privateDb();
-
fnt->families = QGuiApplicationPrivate::platformIntegration()->fontDatabase()->addApplicationFont(fnt->data,fnt->fileName);
-
- db->reregisterAppFonts = true;
}
static QtFontStyle *bestStyle(QtFontFoundry *foundry, const QtFontStyle::Key &styleKey,
@@ -2451,13 +2442,18 @@ int QFontDatabasePrivate::addAppFont(const QByteArray &fontData, const QString &
if (font.fileName.isEmpty() && !fontData.isEmpty())
font.fileName = QLatin1String(":qmemoryfonts/") + QString::number(i);
+ bool wasEmpty = privateDb()->count == 0;
registerFont(&font);
if (font.families.isEmpty())
return -1;
applicationFonts[i] = font;
- invalidate();
+ // If the cache has not yet been populated, we need to reload the application font later
+ if (wasEmpty)
+ invalidate();
+ else
+ emit qApp->fontDatabaseChanged();
return i;
}
@@ -2598,7 +2594,6 @@ bool QFontDatabase::removeApplicationFont(int handle)
db->applicationFonts[handle] = QFontDatabasePrivate::ApplicationFont();
- db->reregisterAppFonts = true;
db->invalidate();
return true;
}