diff options
-rw-r--r-- | src/gui/text/qfont.cpp | 41 | ||||
-rw-r--r-- | src/gui/text/qfont_p.h | 1 | ||||
-rw-r--r-- | tests/auto/gui/text/qfontcache/tst_qfontcache.cpp | 47 |
3 files changed, 74 insertions, 15 deletions
diff --git a/src/gui/text/qfont.cpp b/src/gui/text/qfont.cpp index 194cccaed9..76ced18099 100644 --- a/src/gui/text/qfont.cpp +++ b/src/gui/text/qfont.cpp @@ -2878,7 +2878,10 @@ static QBasicAtomicInt font_cache_id = Q_BASIC_ATOMIC_INITIALIZER(0); QFontCache::QFontCache() : QObject(), total_cost(0), max_cost(min_cost), - current_timestamp(0), fast(false), timer_id(-1), + current_timestamp(0), fast(false), + autoClean(QGuiApplication::instance() + && (QGuiApplication::instance()->thread() == QThread::currentThread())), + timer_id(-1), m_id(font_cache_id.fetchAndAddRelaxed(1) + 1) { } @@ -3048,10 +3051,14 @@ void QFontCache::increaseCost(uint cost) if (total_cost > max_cost) { max_cost = total_cost; + if (!autoClean) + return; + if (timer_id == -1 || ! fast) { FC_DEBUG(" TIMER: starting fast timer (%d ms)", fast_timeout); - if (timer_id != -1) killTimer(timer_id); + if (timer_id != -1) + killTimer(timer_id); timer_id = startTimer(fast_timeout); fast = true; } @@ -3142,22 +3149,26 @@ void QFontCache::decreaseCache() FC_DEBUG(" after sweep, in use %u kb, total %u kb, max %u kb, new max %u kb", in_use_cost, total_cost, max_cost, new_max_cost); - if (new_max_cost == max_cost) { - if (fast) { - FC_DEBUG(" cannot shrink cache, slowing timer"); + if (autoClean) { + if (new_max_cost == max_cost) { + if (fast) { + FC_DEBUG(" cannot shrink cache, slowing timer"); - killTimer(timer_id); - timer_id = startTimer(slow_timeout); - fast = false; - } + if (timer_id != -1) { + killTimer(timer_id); + timer_id = startTimer(slow_timeout); + fast = false; + } - return; - } else if (! fast) { - FC_DEBUG(" dropping into passing gear"); + return; + } else if (! fast) { + FC_DEBUG(" dropping into passing gear"); - killTimer(timer_id); - timer_id = startTimer(fast_timeout); - fast = true; + if (timer_id != -1) + killTimer(timer_id); + timer_id = startTimer(fast_timeout); + fast = true; } + } } max_cost = new_max_cost; diff --git a/src/gui/text/qfont_p.h b/src/gui/text/qfont_p.h index f285111c4a..156ca4a5a5 100644 --- a/src/gui/text/qfont_p.h +++ b/src/gui/text/qfont_p.h @@ -295,6 +295,7 @@ private: uint total_cost, max_cost; uint current_timestamp; bool fast; + const bool autoClean; int timer_id; const int m_id; }; diff --git a/tests/auto/gui/text/qfontcache/tst_qfontcache.cpp b/tests/auto/gui/text/qfontcache/tst_qfontcache.cpp index 3a7eebdc64..17d199210d 100644 --- a/tests/auto/gui/text/qfontcache/tst_qfontcache.cpp +++ b/tests/auto/gui/text/qfontcache/tst_qfontcache.cpp @@ -47,6 +47,7 @@ private slots: void engineData(); void engineDataFamilies_data(); void engineDataFamilies(); + void threadedAccess(); void clear(); }; @@ -227,5 +228,51 @@ for (int i = 0; i < leakedEngines.size(); ++i) qWarning() << i << leakedEngines. #endif } +struct MessageHandler +{ + MessageHandler() + { + oldMessageHandler = qInstallMessageHandler(myMessageHandler); + messages.clear(); + } + ~MessageHandler() + { + qInstallMessageHandler(oldMessageHandler); + } + + inline static bool receivedMessage = false; + inline static QtMessageHandler oldMessageHandler = nullptr; + inline static QStringList messages; + static void myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &text) + { + if (!text.startsWith("Populating font family aliases took")) { + receivedMessage = true; + messages += text; + } + if (oldMessageHandler) + oldMessageHandler(type, context, text); + } +}; + + +void tst_QFontCache::threadedAccess() +{ + MessageHandler messageHandler; + auto lambda = []{ + for (const auto &family : QFontDatabase::families()) { + QFont font(family); + QFontMetrics fontMetrics(font); + fontMetrics.height(); + } + }; + auto *qThread = QThread::create(lambda); + qThread->start(); + qThread->wait(); + + std::thread stdThread(lambda); + stdThread.join(); + QVERIFY2(!messageHandler.receivedMessage, qPrintable(messageHandler.messages.join('\n'))); +} + QTEST_MAIN(tst_QFontCache) #include "tst_qfontcache.moc" |