diff options
author | Lars Knoll <lars.knoll@qt.io> | 2017-11-17 10:59:47 +0100 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2019-08-08 11:18:02 +0200 |
commit | afb326f07109da0035112e6f56e683e37b8a5d72 (patch) | |
tree | d80903f40fc0c481b4ea1e34c8cb94d82c056d2a /src/gui | |
parent | 6ac610c79bf7f311ee244d45583eb669ada58781 (diff) |
Refactor lockedAlphaMapForGlyph
Simply return a Glyph pointer and not a QImage to avoid allocating and
deleting lots of d pointers for QImage when drawing text. Saves one
new/delete pair per glyph drawn and speeds up text drawing by 10% for
relatively large glyphs (probably more for smaller ones).
The qtext::paintLayoutToPixmap() benchmark shows a 16% improvement
in performance with this change.
Renamed the method to glyphData().
Change-Id: I7a353de521e4f4321c770fb1ac6043d33f6f332c
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/painting/qpaintengine_raster.cpp | 35 | ||||
-rw-r--r-- | src/gui/text/qfontengine.cpp | 25 | ||||
-rw-r--r-- | src/gui/text/qfontengine_p.h | 23 |
3 files changed, 45 insertions, 38 deletions
diff --git a/src/gui/painting/qpaintengine_raster.cpp b/src/gui/painting/qpaintengine_raster.cpp index 096e4a5c5b..885c46e121 100644 --- a/src/gui/painting/qpaintengine_raster.cpp +++ b/src/gui/painting/qpaintengine_raster.cpp @@ -2908,19 +2908,34 @@ bool QRasterPaintEngine::drawCachedGlyphs(int numGlyphs, const glyph_t *glyphs, for (int i = 0; i < numGlyphs; i++) { QFixed spp = fontEngine->subPixelPositionForX(positions[i].x); - QPoint offset; - const QImage *alphaMap = fontEngine->lockedAlphaMapForGlyph(glyphs[i], spp, neededFormat, s->matrix, - &offset); - if (alphaMap == 0 || alphaMap->isNull()) + const QFontEngine::Glyph *alphaMap = fontEngine->glyphData(glyphs[i], spp, neededFormat, s->matrix); + if (!alphaMap) continue; - alphaPenBlt(alphaMap->constBits(), alphaMap->bytesPerLine(), alphaMap->depth(), - qFloor(positions[i].x) + offset.x(), - qRound(positions[i].y) + offset.y(), - alphaMap->width(), alphaMap->height(), + int depth; + int bytesPerLine; + switch (alphaMap->format) { + case QFontEngine::Format_Mono: + depth = 1; + bytesPerLine = ((alphaMap->width + 31) & ~31) >> 3; + break; + case QFontEngine::Format_A8: + depth = 8; + bytesPerLine = (alphaMap->width + 3) & ~3; + break; + case QFontEngine::Format_A32: + depth = 32; + bytesPerLine = alphaMap->width * 4; + break; + default: + Q_UNREACHABLE(); + }; + + alphaPenBlt(alphaMap->data, bytesPerLine, depth, + qFloor(positions[i].x) + alphaMap->x, + qRound(positions[i].y) - alphaMap->y, + alphaMap->width, alphaMap->height, fontEngine->expectsGammaCorrectedBlending()); - - fontEngine->unlockAlphaMapForGlyph(); } } else { diff --git a/src/gui/text/qfontengine.cpp b/src/gui/text/qfontengine.cpp index 5506d88f02..1895ac8283 100644 --- a/src/gui/text/qfontengine.cpp +++ b/src/gui/text/qfontengine.cpp @@ -923,29 +923,10 @@ QFixed QFontEngine::subPixelPositionForX(QFixed x) const return subPixelPosition; } -QImage *QFontEngine::lockedAlphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition, - QFontEngine::GlyphFormat neededFormat, - const QTransform &t, QPoint *offset) +QFontEngine::Glyph *QFontEngine::glyphData(glyph_t, QFixed, + QFontEngine::GlyphFormat, const QTransform &) { - Q_ASSERT(currentlyLockedAlphaMap.isNull()); - if (neededFormat == Format_None) - neededFormat = Format_A32; - - if (neededFormat != Format_A32) - currentlyLockedAlphaMap = alphaMapForGlyph(glyph, subPixelPosition, t); - else - currentlyLockedAlphaMap = alphaRGBMapForGlyph(glyph, subPixelPosition, t); - - if (offset != 0) - *offset = QPoint(0, 0); - - return ¤tlyLockedAlphaMap; -} - -void QFontEngine::unlockAlphaMapForGlyph() -{ - Q_ASSERT(!currentlyLockedAlphaMap.isNull()); - currentlyLockedAlphaMap = QImage(); + return nullptr; } QImage QFontEngine::alphaMapForGlyph(glyph_t glyph) diff --git a/src/gui/text/qfontengine_p.h b/src/gui/text/qfontengine_p.h index 682395ece6..48dcdbeff7 100644 --- a/src/gui/text/qfontengine_p.h +++ b/src/gui/text/qfontengine_p.h @@ -124,6 +124,22 @@ public: }; Q_DECLARE_FLAGS(ShaperFlags, ShaperFlag) + /* Used with the Freetype font engine. We don't cache glyphs that are too large anyway, so we can make this struct rather small */ + struct Glyph { + Glyph() = default; + ~Glyph() { delete [] data; } + short linearAdvance = 0; + unsigned char width = 0; + unsigned char height = 0; + short x = 0; + short y = 0; + short advance = 0; + signed char format = 0; + uchar *data = nullptr; + private: + Q_DISABLE_COPY(Glyph); + }; + virtual ~QFontEngine(); inline Type type() const { return m_type; } @@ -191,11 +207,7 @@ public: virtual QImage alphaMapForGlyph(glyph_t, QFixed subPixelPosition, const QTransform &t); virtual QImage alphaRGBMapForGlyph(glyph_t, QFixed subPixelPosition, const QTransform &t); virtual QImage bitmapForGlyph(glyph_t, QFixed subPixelPosition, const QTransform &t, const QColor &color = QColor()); - virtual QImage *lockedAlphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition, - GlyphFormat neededFormat, - const QTransform &t = QTransform(), - QPoint *offset = 0); - virtual void unlockAlphaMapForGlyph(); + virtual Glyph *glyphData(glyph_t glyph, QFixed subPixelPosition, GlyphFormat neededFormat, const QTransform &t); virtual bool hasInternalCaching() const { return false; } virtual glyph_metrics_t alphaMapBoundingBox(glyph_t glyph, QFixed /*subPixelPosition*/, const QTransform &matrix, GlyphFormat /*format*/) @@ -346,7 +358,6 @@ public: void loadKerningPairs(QFixed scalingFactor); GlyphFormat glyphFormat; - QImage currentlyLockedAlphaMap; int m_subPixelPositionCount; // Number of positions within a single pixel for this cache inline QVariant userData() const { return m_userData; } |