summaryrefslogtreecommitdiffstats
path: root/src/gui/text
diff options
context:
space:
mode:
authorEskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@nokia.com>2011-09-16 16:00:04 +0200
committerQt by Nokia <qt-info@nokia.com>2011-09-20 12:00:23 +0200
commit070d9c00c488a5ee6811f04170cf488ead79bf80 (patch)
treeef3511a994b4ec4b8d563393ea2fb1a4783ad095 /src/gui/text
parent57368c7037c6d2a6c8cc18d9d41399bf740abe5c (diff)
Avoid double caching glyphs in raster/QPA/FreeType
Since FreeType has internal caching, it has had a special code path in the raster engine which avoided using Qt's glyph cache, as that would be redundant. However, when compiling with QPA, we want the same behavior without being able to access the QFontEngineFT class. To achieve this, I've made a new abstraction and added it to QFontEngine which allows the FT engine to provide direct pointers into the alpha maps stored by FT. This requires a locking/unlocking mechanism. Yes, the QFontEngine::alphaMap* API is slowly becoming a horrible mess, but due to time constraints, the task of refactoring these functions into a more flexible API needs to wait. I've added a TODO comment for this. Change-Id: I08237403c2967f8fb952258178676f33a87c0353 Reviewed-on: http://codereview.qt-project.org/5155 Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com> Reviewed-by: Samuel Rødal <samuel.rodal@nokia.com>
Diffstat (limited to 'src/gui/text')
-rw-r--r--src/gui/text/qfontengine.cpp40
-rw-r--r--src/gui/text/qfontengine_ft.cpp101
-rw-r--r--src/gui/text/qfontengine_ft_p.h7
-rw-r--r--src/gui/text/qfontengine_p.h9
4 files changed, 141 insertions, 16 deletions
diff --git a/src/gui/text/qfontengine.cpp b/src/gui/text/qfontengine.cpp
index f7f3b0e900..4f18aa3efd 100644
--- a/src/gui/text/qfontengine.cpp
+++ b/src/gui/text/qfontengine.cpp
@@ -636,6 +636,46 @@ QImage QFontEngine::alphaRGBMapForGlyph(glyph_t glyph, QFixed /*subPixelPosition
return rgbMask;
}
+QFixed QFontEngine::subPixelPositionForX(QFixed x)
+{
+ int m_subPixelPositionCount = 4;
+ if (!supportsSubPixelPositions())
+ return 0;
+
+ QFixed subPixelPosition;
+ if (x != 0) {
+ subPixelPosition = x - x.floor();
+ QFixed fraction = (subPixelPosition / QFixed::fromReal(1.0 / m_subPixelPositionCount)).floor();
+ subPixelPosition = fraction / QFixed(m_subPixelPositionCount);
+ }
+ return subPixelPosition;
+}
+
+QImage *QFontEngine::lockedAlphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition,
+ QFontEngine::GlyphFormat neededFormat,
+ const QTransform &t, QPoint *offset)
+{
+ 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, 0, t);
+
+ if (offset != 0)
+ *offset = QPoint(0, 0);
+
+ return &currentlyLockedAlphaMap;
+}
+
+void QFontEngine::unlockAlphaMapForGlyph()
+{
+ Q_ASSERT(!currentlyLockedAlphaMap.isNull());
+ currentlyLockedAlphaMap = QImage();
+}
+
QImage QFontEngine::alphaMapForGlyph(glyph_t glyph)
{
glyph_metrics_t gm = boundingBox(glyph);
diff --git a/src/gui/text/qfontengine_ft.cpp b/src/gui/text/qfontengine_ft.cpp
index 1e7c3a2e1d..a6fb0e8594 100644
--- a/src/gui/text/qfontengine_ft.cpp
+++ b/src/gui/text/qfontengine_ft.cpp
@@ -44,6 +44,7 @@
#include "qtextstream.h"
#include "qvariant.h"
#include "qfontengine_ft_p.h"
+#include "private/qimage_p.h"
#ifndef QT_NO_FREETYPE
@@ -1368,21 +1369,6 @@ QFontEngineFT::QGlyphSet *QFontEngineFT::loadTransformedGlyphSet(const QTransfor
return gs;
}
-QFixed QFontEngineFT::subPixelPositionForX(QFixed x)
-{
- int m_subPixelPositionCount = 4;
- if (!supportsSubPixelPositions())
- return 0;
-
- QFixed subPixelPosition;
- if (x != 0) {
- subPixelPosition = x - x.floor();
- QFixed fraction = (subPixelPosition / QFixed::fromReal(1.0 / m_subPixelPositionCount)).floor();
- subPixelPosition = fraction / QFixed(m_subPixelPositionCount);
- }
- return subPixelPosition;
-}
-
bool QFontEngineFT::loadGlyphs(QGlyphSet *gs, const glyph_t *glyphs, int num_glyphs,
const QFixedPoint *positions,
GlyphFormat format)
@@ -1811,6 +1797,91 @@ glyph_metrics_t QFontEngineFT::alphaMapBoundingBox(glyph_t glyph, QFixed subPixe
return overall;
}
+QImage *QFontEngineFT::lockedAlphaMapForGlyph(glyph_t glyphIndex, QFixed subPixelPosition,
+ QFontEngine::GlyphFormat neededFormat,
+ const QTransform &t, QPoint *offset)
+{
+ Q_ASSERT(currentlyLockedAlphaMap.isNull());
+ lockFace();
+
+ if (isBitmapFont())
+ neededFormat = Format_Mono;
+ else if (neededFormat == Format_None)
+ neededFormat = defaultFormat;
+
+ QFontEngineFT::QGlyphSet *gset = defaultGlyphs();
+ if (t.type() >= QTransform::TxScale) {
+ if (t.isAffine())
+ gset = loadTransformedGlyphSet(t);
+ else
+ gset = 0;
+ }
+
+ if (!gset || gset->outline_drawing || !loadGlyph(gset, glyphIndex, subPixelPosition,
+ neededFormat)) {
+ unlockFace();
+ return QFontEngine::lockedAlphaMapForGlyph(glyphIndex, subPixelPosition, neededFormat, t,
+ offset);
+ }
+
+ QImage::Format format;
+ switch (neededFormat) {
+ case QFontEngine::Format_Mono:
+ format = QImage::Format_Mono;
+ break;
+ case QFontEngine::Format_A8:
+ format = QImage::Format_Indexed8;
+ break;
+ case QFontEngine::Format_A32:
+ format = QImage::Format_ARGB32;
+ break;
+ default:
+ Q_ASSERT(false);
+ format = QImage::Format_Invalid;
+ };
+
+ QFontEngineFT::Glyph *glyph = gset->getGlyph(glyphIndex, subPixelPosition);
+ if (glyph == 0 || glyph->data == 0 || glyph->width == 0 || glyph->height == 0) {
+ unlockFace();
+ return 0;
+ }
+
+ int pitch;
+ switch (neededFormat) {
+ case QFontEngineFT::Format_Mono:
+ pitch = ((glyph->width + 31) & ~31) >> 3;
+ break;
+ case QFontEngineFT::Format_A8:
+ pitch = (glyph->width + 3) & ~3;
+ break;
+ case QFontEngineFT::Format_A32:
+ pitch = glyph->width * 4;
+ break;
+ default:
+ Q_ASSERT(false);
+ pitch = 0;
+ };
+
+ if (offset != 0)
+ *offset = QPoint(glyph->x, -glyph->y);
+
+
+ currentlyLockedAlphaMap = QImage(glyph->data, glyph->width, glyph->height, pitch, format);
+ Q_ASSERT(!currentlyLockedAlphaMap.isNull());
+
+ QImageData *data = currentlyLockedAlphaMap.data_ptr();
+ data->is_locked = true;
+
+ return &currentlyLockedAlphaMap;
+}
+
+void QFontEngineFT::unlockAlphaMapForGlyph()
+{
+ Q_ASSERT(!currentlyLockedAlphaMap.isNull());
+ unlockFace();
+ currentlyLockedAlphaMap = QImage();
+}
+
QImage QFontEngineFT::alphaMapForGlyph(glyph_t g, QFixed subPixelPosition)
{
lockFace();
diff --git a/src/gui/text/qfontengine_ft_p.h b/src/gui/text/qfontengine_ft_p.h
index 62edaeec45..a236b79f68 100644
--- a/src/gui/text/qfontengine_ft_p.h
+++ b/src/gui/text/qfontengine_ft_p.h
@@ -265,6 +265,12 @@ private:
QFixed subPixelPosition,
const QTransform &matrix,
QFontEngine::GlyphFormat format);
+ virtual QImage *lockedAlphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition,
+ GlyphFormat neededFormat, const QTransform &t,
+ QPoint *offset);
+ virtual bool hasInternalCaching() const { return true; }
+ virtual void unlockAlphaMapForGlyph();
+
virtual void removeGlyphFromCache(glyph_t glyph);
virtual int glyphCount() const;
@@ -292,7 +298,6 @@ private:
inline Glyph *cachedGlyph(glyph_t g) const { return defaultGlyphSet.getGlyph(g, 0); }
QGlyphSet *loadTransformedGlyphSet(const QTransform &matrix);
- QFixed subPixelPositionForX(QFixed x);
bool loadGlyphs(QGlyphSet *gs, const glyph_t *glyphs, int num_glyphs,
const QFixedPoint *positions,
GlyphFormat format = Format_Render);
diff --git a/src/gui/text/qfontengine_p.h b/src/gui/text/qfontengine_p.h
index a5858ffc7d..f74302c054 100644
--- a/src/gui/text/qfontengine_p.h
+++ b/src/gui/text/qfontengine_p.h
@@ -167,6 +167,7 @@ public:
};
virtual int synthesized() const { return 0; }
virtual bool supportsSubPixelPositions() const { return false; }
+ QFixed subPixelPositionForX(QFixed x);
virtual QFixed emSquareSize() const { return ascent(); }
@@ -195,11 +196,18 @@ public:
* Create a qimage with the alpha values for the glyph.
* Returns an image indexed_8 with index values ranging from 0=fully transparent to 255=opaque
*/
+ // ### Refactor this into a smaller and more flexible API.
virtual QImage alphaMapForGlyph(glyph_t);
virtual QImage alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition);
virtual QImage alphaMapForGlyph(glyph_t, const QTransform &t);
virtual QImage alphaMapForGlyph(glyph_t, QFixed subPixelPosition, const QTransform &t);
virtual QImage alphaRGBMapForGlyph(glyph_t, QFixed subPixelPosition, int margin, const QTransform &t);
+ virtual QImage *lockedAlphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition,
+ GlyphFormat neededFormat,
+ const QTransform &t = QTransform(),
+ QPoint *offset = 0);
+ virtual void unlockAlphaMapForGlyph();
+ virtual bool hasInternalCaching() const { return false; }
virtual glyph_metrics_t alphaMapBoundingBox(glyph_t glyph, QFixed /*subPixelPosition*/, const QTransform &matrix, GlyphFormat /*format*/)
{
@@ -282,6 +290,7 @@ public:
#endif
int glyphFormat;
+ QImage currentlyLockedAlphaMap;
protected:
static const QVector<QRgb> &grayPalette();