0284d5ab67d53c1698b2757ab2e0ea30afe814b2
[profile/ivi/qtdeclarative.git] / src / declarative / scenegraph / qsgdistancefieldglyphcache_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef DISTANCEFIELDGLYPHCACHE_H
43 #define DISTANCEFIELDGLYPHCACHE_H
44
45 #include <qgl.h>
46 #include <qrawfont.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>
51
52 QT_BEGIN_NAMESPACE
53
54 class QGLShaderProgram;
55
56 typedef float (*ThresholdFunc)(float glyphScale);
57 typedef float (*AntialiasingSpreadFunc)(float glyphScale);
58
59 class Q_DECLARATIVE_EXPORT QSGDistanceFieldGlyphCache : public QObject
60 {
61     Q_OBJECT
62 public:
63     ~QSGDistanceFieldGlyphCache();
64
65     static QSGDistanceFieldGlyphCache *get(const QGLContext *ctx, const QRawFont &font);
66
67     struct Metrics {
68         qreal width;
69         qreal height;
70         qreal baselineX;
71         qreal baselineY;
72
73         bool isNull() const { return width == 0 || height == 0; }
74     };
75     Metrics glyphMetrics(glyph_t glyph);
76
77     struct TexCoord {
78         qreal x;
79         qreal y;
80         qreal width;
81         qreal height;
82         qreal xMargin;
83         qreal yMargin;
84
85         TexCoord() : x(0), y(0), width(0), height(0), xMargin(0), yMargin(0) { }
86
87         bool isNull() const { return width == 0 || height == 0; }
88     };
89     TexCoord glyphTexCoord(glyph_t glyph);
90
91     GLuint texture();
92     QSize textureSize() const;
93     int maxTextureSize() const;
94     qreal fontScale() const;
95     int distanceFieldRadius() const;
96     QImage renderDistanceFieldGlyph(glyph_t glyph) const;
97
98     int glyphCount() const;
99
100     void populate(int count, const glyph_t *glyphs);
101     void derefGlyphs(int count, const glyph_t *glyphs);
102     void updateCache();
103
104     bool cacheIsFull() const { return m_textureData->currY >= maxTextureSize(); }
105
106     bool useWorkaroundBrokenFBOReadback() const;
107
108     static bool distanceFieldEnabled();
109
110     ThresholdFunc thresholdFunc() const { return m_threshold_func; }
111     void setThresholdFunc(ThresholdFunc func) { m_threshold_func = func; }
112
113     AntialiasingSpreadFunc antialiasingSpreadFunc() const { return m_antialiasingSpread_func; }
114     void setAntialiasingSpreadFunc(AntialiasingSpreadFunc func) { m_antialiasingSpread_func = func; }
115
116 private Q_SLOTS:
117     void onContextDestroyed(const QGLContext *context);
118
119 private:
120     QSGDistanceFieldGlyphCache(const QGLContext *c, const QRawFont &font);
121
122     void createTexture(int width, int height);
123     void resizeTexture(int width, int height);
124
125     static QHash<QPair<const QGLContext *, QFontEngine *>, QSGDistanceFieldGlyphCache *> m_caches;
126
127     QRawFont m_font;
128     QRawFont m_referenceFont;
129
130     int m_glyphCount;
131     QHash<glyph_t, Metrics> m_metrics;
132     mutable int m_maxTextureSize;
133
134     struct DistanceFieldTextureData {
135         GLuint texture;
136         GLuint fbo;
137         QSize size;
138         QHash<glyph_t, TexCoord> texCoords;
139         QDataBuffer<glyph_t> pendingGlyphs;
140         QHash<glyph_t, quint32> glyphRefCount;
141         QSet<glyph_t> unusedGlyphs;
142         int currX;
143         int currY;
144         QImage image;
145         bool doubleGlyphResolution;
146
147         DistanceFieldTextureData(const QGLContext *)
148             : texture(0)
149             , fbo(0)
150             , pendingGlyphs(64)
151             , currX(0)
152             , currY(0)
153             , doubleGlyphResolution(false)
154         { }
155     };
156     DistanceFieldTextureData *textureData();
157     DistanceFieldTextureData *m_textureData;
158     static QHash<QFontEngine *, QGLContextGroupResource<DistanceFieldTextureData> > m_textures_data;
159
160     const QGLContext *ctx;
161     QGLShaderProgram *m_blitProgram;
162     GLfloat m_vertexCoordinateArray[8];
163     GLfloat m_textureCoordinateArray[8];
164
165     ThresholdFunc m_threshold_func;
166     AntialiasingSpreadFunc m_antialiasingSpread_func;
167 };
168
169 QT_END_NAMESPACE
170
171 #endif // DISTANCEFIELDGLYPHCACHE_H