1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef DISTANCEFIELDGLYPHCACHE_H
43 #define DISTANCEFIELDGLYPHCACHE_H
47 #include <private/qgl_p.h>
48 #include <private/qfont_p.h>
49 #include <private/qfontengine_p.h>
50 #include <QtGui/private/qdatabuffer_p.h>
54 class QGLShaderProgram;
56 typedef float (*ThresholdFunc)(float glyphScale);
57 typedef float (*AntialiasingSpreadFunc)(float glyphScale);
59 class Q_DECLARATIVE_EXPORT QSGDistanceFieldGlyphCache : public QObject
63 ~QSGDistanceFieldGlyphCache();
65 static QSGDistanceFieldGlyphCache *get(const QGLContext *ctx, const QRawFont &font);
73 bool isNull() const { return width == 0 || height == 0; }
75 Metrics glyphMetrics(glyph_t glyph);
85 TexCoord() : x(0), y(0), width(0), height(0), xMargin(0), yMargin(0) { }
87 bool isNull() const { return width == 0 || height == 0; }
89 TexCoord glyphTexCoord(glyph_t glyph);
92 QSize textureSize() const;
93 int maxTextureSize() const;
94 qreal fontScale() const;
95 int distanceFieldRadius() const;
96 QImage renderDistanceFieldGlyph(glyph_t glyph) const;
98 int glyphCount() const;
100 void populate(int count, const glyph_t *glyphs);
101 void derefGlyphs(int count, const glyph_t *glyphs);
104 bool cacheIsFull() const { return m_textureData->currY >= maxTextureSize(); }
106 bool useWorkaroundBrokenFBOReadback() const;
108 static bool distanceFieldEnabled();
110 ThresholdFunc thresholdFunc() const { return m_threshold_func; }
111 void setThresholdFunc(ThresholdFunc func) { m_threshold_func = func; }
113 AntialiasingSpreadFunc antialiasingSpreadFunc() const { return m_antialiasingSpread_func; }
114 void setAntialiasingSpreadFunc(AntialiasingSpreadFunc func) { m_antialiasingSpread_func = func; }
117 void onContextDestroyed(const QGLContext *context);
120 QSGDistanceFieldGlyphCache(const QGLContext *c, const QRawFont &font);
122 void createTexture(int width, int height);
123 void resizeTexture(int width, int height);
125 static QHash<QPair<const QGLContext *, QFontEngine *>, QSGDistanceFieldGlyphCache *> m_caches;
128 QRawFont m_referenceFont;
131 QHash<glyph_t, Metrics> m_metrics;
132 mutable int m_maxTextureSize;
134 struct DistanceFieldTextureData {
138 QHash<glyph_t, TexCoord> texCoords;
139 QDataBuffer<glyph_t> pendingGlyphs;
140 QHash<glyph_t, quint32> glyphRefCount;
141 QSet<glyph_t> unusedGlyphs;
145 bool doubleGlyphResolution;
147 DistanceFieldTextureData(const QGLContext *)
153 , doubleGlyphResolution(false)
156 DistanceFieldTextureData *textureData();
157 DistanceFieldTextureData *m_textureData;
158 static QHash<QFontEngine *, QGLContextGroupResource<DistanceFieldTextureData> > m_textures_data;
160 const QGLContext *ctx;
161 QGLShaderProgram *m_blitProgram;
162 GLfloat m_vertexCoordinateArray[8];
163 GLfloat m_textureCoordinateArray[8];
165 ThresholdFunc m_threshold_func;
166 AntialiasingSpreadFunc m_antialiasingSpread_func;
171 #endif // DISTANCEFIELDGLYPHCACHE_H