Share depth-stencil buffers between ShaderEffectSources of same size.
[profile/ivi/qtdeclarative.git] / src / quick / scenegraph / qsgadaptationlayer_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
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 #include <private/qdistancefield_p.h>
58
59 // ### remove
60 #include <QtQuick/private/qquicktext_p.h>
61
62 QT_BEGIN_HEADER
63
64 QT_BEGIN_NAMESPACE
65
66 class QSGNode;
67 class QImage;
68 class TextureReference;
69 class QSGDistanceFieldGlyphCacheManager;
70 class QSGDistanceFieldGlyphNode;
71
72 // TODO: Rename from XInterface to AbstractX.
73 class Q_QUICK_EXPORT QSGRectangleNode : public QSGGeometryNode
74 {
75 public:
76     virtual void setRect(const QRectF &rect) = 0;
77     virtual void setColor(const QColor &color) = 0;
78     virtual void setPenColor(const QColor &color) = 0;
79     virtual void setPenWidth(qreal width) = 0;
80     virtual void setGradientStops(const QGradientStops &stops) = 0;
81     virtual void setRadius(qreal radius) = 0;
82     virtual void setAligned(bool aligned) = 0;
83
84     virtual void update() = 0;
85 };
86
87
88 class Q_QUICK_EXPORT QSGImageNode : public QSGGeometryNode
89 {
90 public:
91     virtual void setTargetRect(const QRectF &rect) = 0;
92     virtual void setSourceRect(const QRectF &rect) = 0;
93     virtual void setTexture(QSGTexture *texture) = 0;
94
95     virtual void setMipmapFiltering(QSGTexture::Filtering filtering) = 0;
96     virtual void setFiltering(QSGTexture::Filtering filtering) = 0;
97     virtual void setHorizontalWrapMode(QSGTexture::WrapMode wrapMode) = 0;
98     virtual void setVerticalWrapMode(QSGTexture::WrapMode wrapMode) = 0;
99
100     virtual void update() = 0;
101 };
102
103
104 class Q_QUICK_EXPORT QSGGlyphNode : public QSGGeometryNode
105 {
106 public:
107     enum AntialiasingMode
108     {
109         GrayAntialiasing,
110         LowQualitySubPixelAntialiasing,
111         HighQualitySubPixelAntialiasing
112     };
113
114     QSGGlyphNode() : m_ownerElement(0) {}
115
116     virtual void setGlyphs(const QPointF &position, const QGlyphRun &glyphs) = 0;
117     virtual void setColor(const QColor &color) = 0;
118     virtual void setStyle(QQuickText::TextStyle style) = 0;
119     virtual void setStyleColor(const QColor &color) = 0;
120     virtual QPointF baseLine() const = 0;
121
122     virtual QRectF boundingRect() const { return m_bounding_rect; }
123     virtual void setBoundingRect(const QRectF &bounds) { m_bounding_rect = bounds; }
124
125     virtual void setPreferredAntialiasingMode(AntialiasingMode) = 0;
126
127     virtual void update() = 0;
128
129     void setOwnerElement(QQuickItem *ownerElement) { m_ownerElement = ownerElement; }
130     QQuickItem *ownerElement() const { return m_ownerElement; }
131
132 protected:
133     QRectF m_bounding_rect;
134     QQuickItem *m_ownerElement;
135 };
136
137 class Q_QUICK_EXPORT QSGDistanceFieldGlyphConsumer
138 {
139 public:
140     virtual ~QSGDistanceFieldGlyphConsumer() {}
141
142     virtual void invalidateGlyphs(const QVector<quint32> &glyphs) = 0;
143 };
144
145 class Q_QUICK_EXPORT QSGDistanceFieldGlyphCache
146 {
147 public:
148     QSGDistanceFieldGlyphCache(QSGDistanceFieldGlyphCacheManager *man, QOpenGLContext *c, const QRawFont &font);
149     virtual ~QSGDistanceFieldGlyphCache();
150
151     struct Metrics {
152         qreal width;
153         qreal height;
154         qreal baselineX;
155         qreal baselineY;
156
157         bool isNull() const { return width == 0 || height == 0; }
158     };
159
160     struct TexCoord {
161         qreal x;
162         qreal y;
163         qreal width;
164         qreal height;
165         qreal xMargin;
166         qreal yMargin;
167
168         TexCoord() : x(0), y(0), width(-1), height(-1), xMargin(0), yMargin(0) { }
169
170         bool isNull() const { return width <= 0 || height <= 0; }
171         bool isValid() const { return width >= 0 && height >= 0; }
172     };
173
174     struct Texture {
175         GLuint textureId;
176         QSize size;
177
178         Texture() : textureId(0), size(QSize()) { }
179         bool operator == (const Texture &other) const { return textureId == other.textureId; }
180     };
181
182     const QSGDistanceFieldGlyphCacheManager *manager() const { return m_manager; }
183
184     const QRawFont &referenceFont() const { return m_referenceFont; }
185
186     qreal fontScale(qreal pixelSize) const
187     {
188         return pixelSize / QT_DISTANCEFIELD_BASEFONTSIZE(m_doubleGlyphResolution);
189     }
190     int distanceFieldRadius() const
191     {
192         return QT_DISTANCEFIELD_DEFAULT_RADIUS / QT_DISTANCEFIELD_SCALE(m_doubleGlyphResolution);
193     }
194     int glyphCount() const { return m_glyphCount; }
195     bool doubleGlyphResolution() const { return m_doubleGlyphResolution; }
196
197     Metrics glyphMetrics(glyph_t glyph, qreal pixelSize);
198     inline TexCoord glyphTexCoord(glyph_t glyph);
199     inline const Texture *glyphTexture(glyph_t glyph);
200
201     void populate(const QVector<glyph_t> &glyphs);
202     void release(const QVector<glyph_t> &glyphs);
203
204     void update();
205
206     void registerGlyphNode(QSGDistanceFieldGlyphConsumer *node) { m_registeredNodes.append(node); }
207     void unregisterGlyphNode(QSGDistanceFieldGlyphConsumer *node) { m_registeredNodes.removeOne(node); }
208
209     virtual void registerOwnerElement(QQuickItem *ownerElement);
210     virtual void unregisterOwnerElement(QQuickItem *ownerElement);
211     virtual void processPendingGlyphs();
212
213 protected:
214     struct GlyphPosition {
215         glyph_t glyph;
216         QPointF position;
217     };
218
219     virtual void requestGlyphs(const QSet<glyph_t> &glyphs) = 0;
220     virtual void storeGlyphs(const QHash<glyph_t, QImage> &glyphs) = 0;
221     virtual void referenceGlyphs(const QSet<glyph_t> &glyphs) = 0;
222     virtual void releaseGlyphs(const QSet<glyph_t> &glyphs) = 0;
223
224     void setGlyphsPosition(const QList<GlyphPosition> &glyphs);
225     void setGlyphsTexture(const QVector<glyph_t> &glyphs, const Texture &tex);
226     void markGlyphsToRender(const QVector<glyph_t> &glyphs);
227     inline void removeGlyph(glyph_t glyph);
228
229     void updateTexture(GLuint oldTex, GLuint newTex, const QSize &newTexSize);
230
231     inline bool containsGlyph(glyph_t glyph);
232     GLuint textureIdForGlyph(glyph_t glyph) const;
233
234     QOpenGLContext *ctx;
235
236 private:
237     struct GlyphData {
238         Texture *texture;
239         TexCoord texCoord;
240         QRectF boundingRect;
241         quint32 ref;
242
243         GlyphData() : texture(0), ref(0) { }
244     };
245
246     GlyphData &glyphData(glyph_t glyph);
247
248     QSGDistanceFieldGlyphCacheManager *m_manager;
249
250     QRawFont m_referenceFont;
251     int m_glyphCount;
252
253     bool m_doubleGlyphResolution;
254
255     QList<Texture> m_textures;
256     QHash<glyph_t, GlyphData> m_glyphsData;
257     QDataBuffer<glyph_t> m_pendingGlyphs;
258     QLinkedList<QSGDistanceFieldGlyphConsumer*> m_registeredNodes;
259
260     static Texture s_emptyTexture;
261 };
262
263 inline QSGDistanceFieldGlyphCache::TexCoord QSGDistanceFieldGlyphCache::glyphTexCoord(glyph_t glyph)
264 {
265     return glyphData(glyph).texCoord;
266 }
267
268 inline const QSGDistanceFieldGlyphCache::Texture *QSGDistanceFieldGlyphCache::glyphTexture(glyph_t glyph)
269 {
270     return glyphData(glyph).texture;
271 }
272
273 inline void QSGDistanceFieldGlyphCache::removeGlyph(glyph_t glyph)
274 {
275     GlyphData &gd = glyphData(glyph);
276     gd.texCoord = TexCoord();
277     gd.texture = &s_emptyTexture;
278 }
279
280 inline bool QSGDistanceFieldGlyphCache::containsGlyph(glyph_t glyph)
281 {
282     return glyphData(glyph).texCoord.isValid();
283 }
284
285
286 QT_END_NAMESPACE
287
288 QT_END_HEADER
289
290 #endif