Reference count glyphs in QSGDistanceFieldGlyphCache.
[profile/ivi/qtdeclarative.git] / src / quick / scenegraph / qsgadaptationlayer_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 ADAPTATIONINTERFACES_H
43 #define ADAPTATIONINTERFACES_H
44
45 #include <QtQuick/qsgnode.h>
46 #include <QtQuick/qsgtexture.h>
47 #include <QtCore/qobject.h>
48 #include <QtCore/qrect.h>
49 #include <QtGui/qbrush.h>
50 #include <QtGui/qcolor.h>
51 #include <QtCore/qsharedpointer.h>
52 #include <QtGui/qglyphrun.h>
53 #include <QtCore/qurl.h>
54 #include <private/qfontengine_p.h>
55 #include <QtGui/private/qdatabuffer_p.h>
56 #include <private/qopenglcontext_p.h>
57
58 // ### remove
59 #include <QtQuick/private/qquicktext_p.h>
60
61 QT_BEGIN_HEADER
62
63 QT_BEGIN_NAMESPACE
64
65 class QSGNode;
66 class QImage;
67 class TextureReference;
68 class QSGDistanceFieldGlyphCacheManager;
69 class QSGDistanceFieldGlyphNode;
70
71 // TODO: Rename from XInterface to AbstractX.
72 class Q_QUICK_EXPORT QSGRectangleNode : public QSGGeometryNode
73 {
74 public:
75     virtual void setRect(const QRectF &rect) = 0;
76     virtual void setColor(const QColor &color) = 0;
77     virtual void setPenColor(const QColor &color) = 0;
78     virtual void setPenWidth(qreal width) = 0;
79     virtual void setGradientStops(const QGradientStops &stops) = 0;
80     virtual void setRadius(qreal radius) = 0;
81     virtual void setAligned(bool aligned) = 0;
82
83     virtual void update() = 0;
84 };
85
86
87 class Q_QUICK_EXPORT QSGImageNode : public QSGGeometryNode
88 {
89 public:
90     virtual void setTargetRect(const QRectF &rect) = 0;
91     virtual void setSourceRect(const QRectF &rect) = 0;
92     virtual void setTexture(QSGTexture *texture) = 0;
93
94     virtual void setMipmapFiltering(QSGTexture::Filtering filtering) = 0;
95     virtual void setFiltering(QSGTexture::Filtering filtering) = 0;
96     virtual void setHorizontalWrapMode(QSGTexture::WrapMode wrapMode) = 0;
97     virtual void setVerticalWrapMode(QSGTexture::WrapMode wrapMode) = 0;
98
99     virtual void update() = 0;
100 };
101
102
103 class Q_QUICK_EXPORT QSGGlyphNode : public QSGGeometryNode
104 {
105 public:
106     enum AntialiasingMode
107     {
108         GrayAntialiasing,
109         LowQualitySubPixelAntialiasing,
110         HighQualitySubPixelAntialiasing
111     };
112
113     virtual void setGlyphs(const QPointF &position, const QGlyphRun &glyphs) = 0;
114     virtual void setColor(const QColor &color) = 0;
115     virtual void setStyle(QQuickText::TextStyle style) = 0;
116     virtual void setStyleColor(const QColor &color) = 0;
117     virtual QPointF baseLine() const = 0;
118
119     virtual QRectF boundingRect() const { return m_bounding_rect; }
120     virtual void setBoundingRect(const QRectF &bounds) { m_bounding_rect = bounds; }
121
122     virtual void setPreferredAntialiasingMode(AntialiasingMode) = 0;
123
124     virtual void update() = 0;
125
126 protected:
127     QRectF m_bounding_rect;
128 };
129
130 class Q_QUICK_EXPORT QSGDistanceFieldGlyphCache
131 {
132 public:
133     QSGDistanceFieldGlyphCache(QSGDistanceFieldGlyphCacheManager *man, QOpenGLContext *c, const QRawFont &font);
134     virtual ~QSGDistanceFieldGlyphCache();
135
136     struct Metrics {
137         qreal width;
138         qreal height;
139         qreal baselineX;
140         qreal baselineY;
141
142         bool isNull() const { return width == 0 || height == 0; }
143     };
144
145     struct TexCoord {
146         qreal x;
147         qreal y;
148         qreal width;
149         qreal height;
150         qreal xMargin;
151         qreal yMargin;
152
153         TexCoord() : x(0), y(0), width(-1), height(-1), xMargin(0), yMargin(0) { }
154
155         bool isNull() const { return width <= 0 || height <= 0; }
156         bool isValid() const { return width >= 0 && height >= 0; }
157     };
158
159     struct Texture {
160         GLuint textureId;
161         QSize size;
162
163         Texture() : textureId(0), size(QSize()) { }
164         bool operator == (const Texture &other) const { return textureId == other.textureId; }
165     };
166
167     const QSGDistanceFieldGlyphCacheManager *manager() const { return m_manager; }
168
169     const QRawFont &font() const { return m_font; }
170
171     qreal fontScale() const;
172     int distanceFieldRadius() const;
173     int glyphCount() const { return m_glyphCount; }
174     bool doubleGlyphResolution() const { return m_cacheData->doubleGlyphResolution; }
175
176     Metrics glyphMetrics(glyph_t glyph);
177     TexCoord glyphTexCoord(glyph_t glyph) const;
178     const Texture *glyphTexture(glyph_t glyph) const;
179
180     void populate(const QVector<glyph_t> &glyphs);
181     void release(const QVector<glyph_t> &glyphs);
182
183     void update();
184
185     void registerGlyphNode(QSGDistanceFieldGlyphNode *node);
186     void unregisterGlyphNode(QSGDistanceFieldGlyphNode *node);
187
188 protected:
189     struct GlyphPosition {
190         glyph_t glyph;
191         QPointF position;
192     };
193
194     virtual void requestGlyphs(const QSet<glyph_t> &glyphs) = 0;
195     virtual void storeGlyphs(const QHash<glyph_t, QImage> &glyphs) = 0;
196     virtual void referenceGlyphs(const QSet<glyph_t> &glyphs) = 0;
197     virtual void releaseGlyphs(const QSet<glyph_t> &glyphs) = 0;
198
199     void setGlyphsPosition(const QList<GlyphPosition> &glyphs);
200     void setGlyphsTexture(const QVector<glyph_t> &glyphs, const Texture &tex);
201     void markGlyphsToRender(const QVector<glyph_t> &glyphs);
202     void removeGlyph(glyph_t glyph);
203
204     void updateTexture(GLuint oldTex, GLuint newTex, const QSize &newTexSize);
205
206     bool containsGlyph(glyph_t glyph) const;
207
208     QOpenGLContext *ctx;
209
210 private:
211     struct GlyphCacheData : public QOpenGLSharedResource {
212         QList<Texture> textures;
213         QHash<glyph_t, Texture*> glyphTextures;
214         QHash<glyph_t, TexCoord> texCoords;
215         QDataBuffer<glyph_t> pendingGlyphs;
216         QHash<glyph_t, QPainterPath> glyphPaths;
217         bool doubleGlyphResolution;
218         QLinkedList<QSGDistanceFieldGlyphNode*> m_registeredNodes;
219         QHash<glyph_t, quint32> glyphRefCount;
220
221         GlyphCacheData(QOpenGLContext *ctx)
222             : QOpenGLSharedResource(ctx->shareGroup())
223             , pendingGlyphs(64)
224             , doubleGlyphResolution(false)
225         {}
226
227         void invalidateResource()
228         {
229             textures.clear();
230             glyphTextures.clear();
231             texCoords.clear();
232         }
233
234         void freeResource(QOpenGLContext *)
235         {
236         }
237     };
238
239     QSGDistanceFieldGlyphCacheManager *m_manager;
240
241     QRawFont m_font;
242     QRawFont m_referenceFont;
243
244     int m_glyphCount;
245     QHash<glyph_t, Metrics> m_metrics;
246
247     GlyphCacheData *cacheData();
248     GlyphCacheData *m_cacheData;
249     static QHash<QString, QOpenGLMultiGroupSharedResource> m_caches_data;
250 };
251
252
253 QT_END_NAMESPACE
254
255 QT_END_HEADER
256
257 #endif