diff options
author | Yoann Lopes <yoann.lopes@nokia.com> | 2011-01-06 14:27:25 +0100 |
---|---|---|
committer | Yoann Lopes <yoann.lopes@nokia.com> | 2011-01-06 14:27:25 +0100 |
commit | 4904941ebc3ac782b8398ecf1a56839e433a6763 (patch) | |
tree | 376a1000c6abfa0810f21840f1b859cdbfd28da4 | |
parent | 7dd6e1f161069e26e8e175d835f5827ad69a75b7 (diff) |
Use multiple threads for distfieldgen tool.
-rw-r--r-- | tools/distfieldgen/main.cpp | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/tools/distfieldgen/main.cpp b/tools/distfieldgen/main.cpp index 15f6697..84723ec 100644 --- a/tools/distfieldgen/main.cpp +++ b/tools/distfieldgen/main.cpp @@ -84,6 +84,34 @@ QImage renderDistanceField(const QImage &in) return df; } +class DistFieldGenTask : public QRunnable +{ +public: + DistFieldGenTask(int c, int nbGlyph, const QImage &glyph, QMap<int, QImage> *outList) + : QRunnable() + , m_char(c) + , m_nbGlyph(nbGlyph) + , m_glyph(glyph) + , m_outList(outList) + { } + + void run() + { + QImage df = renderDistanceField(m_glyph); + QMutexLocker lock(&m_mutex); + m_outList->insert(m_char, df); + printProgress(float(m_outList->count()) / m_nbGlyph * 100); + } + + static QMutex m_mutex; + int m_char; + int m_nbGlyph; + QImage m_glyph; + QMap<int, QImage> *m_outList; +}; + +QMutex DistFieldGenTask::m_mutex; + int main(int argc, char *argv[]) { QApplication app(argc, argv); @@ -134,7 +162,7 @@ int main(int argc, char *argv[]) printProgress(0); QList<QImage> glyphs; - QList<QImage> distfields; + QMap<int, QImage> distfields; // Render glyphs for (int i = 0; i < charRange; ++i) { @@ -156,11 +184,12 @@ int main(int argc, char *argv[]) // Generate distance field for each glyph int i = 0; foreach (const QImage &g, glyphs) { - distfields.append(renderDistanceField(g)); - printProgress(float(i) / glyphs.count() * 100); + QRunnable *run = new DistFieldGenTask(i, glyphs.count(), g, &distfields); + QThreadPool::globalInstance()->start(run); ++i; } + QThreadPool::globalInstance()->waitForDone(); printProgress(100); printf("\nFinishing\n"); printProgress(0); @@ -171,7 +200,7 @@ int main(int argc, char *argv[]) int combinedWidth = 0; i = 0; QPainter p(&output); - foreach (const QImage &df, distfields) { + foreach (const QImage &df, distfields.values()) { if (combinedWidth % 1024 + df.width() > 1024) combinedWidth = (combinedWidth / 1024 + 1) * 1024; p.drawImage(combinedWidth % 1024, (combinedWidth / 1024) * outSize, df); |