Remove "All rights reserved" line from license headers.
[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 QtDeclarative 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
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     QSGGlyphNode() : m_ownerElement(0) {}
114
115     virtual void setGlyphs(const QPointF &position, const QGlyphRun &glyphs) = 0;
116     virtual void setColor(const QColor &color) = 0;
117     virtual void setStyle(QQuickText::TextStyle style) = 0;
118     virtual void setStyleColor(const QColor &color) = 0;
119     virtual QPointF baseLine() const = 0;
120
121     virtual QRectF boundingRect() const { return m_bounding_rect; }
122     virtual void setBoundingRect(const QRectF &bounds) { m_bounding_rect = bounds; }
123
124     virtual void setPreferredAntialiasingMode(AntialiasingMode) = 0;
125
126     virtual void update() = 0;
127
128     void setOwnerElement(QQuickItem *ownerElement) { m_ownerElement = ownerElement; }
129     QQuickItem *ownerElement() const { return m_ownerElement; }
130
131 protected:
132     QRectF m_bounding_rect;
133     QQuickItem *m_ownerElement;
134 };
135
136 class Q_QUICK_EXPORT QSGDistanceFieldGlyphCache
137 {
138 public:
139     QSGDistanceFieldGlyphCache(QSGDistanceFieldGlyphCacheManager *man, QOpenGLContext *c, const QRawFont &font);
140     virtual ~QSGDistanceFieldGlyphCache();
141
142     struct Metrics {
143         qreal width;
144         qreal height;
145         qreal baselineX;
146         qreal baselineY;
147
148         bool isNull() const { return width == 0 || height == 0; }
149     };
150
151     struct TexCoord {
152         qreal x;
153         qreal y;
154         qreal width;
155         qreal height;
156         qreal xMargin;
157         qreal yMargin;
158
159         TexCoord() : x(0), y(0), width(-1), height(-1), xMargin(0), yMargin(0) { }
160
161         bool isNull() const { return width <= 0 || height <= 0; }
162         bool isValid() const { return width >= 0 && height >= 0; }
163     };
164
165     struct Texture {
166         GLuint textureId;
167         QSize size;
168
169         Texture() : textureId(0), size(QSize()) { }
170         bool operator == (const Texture &other) const { return textureId == other.textureId; }
171     };
172
173     const QSGDistanceFieldGlyphCacheManager *manager() const { return m_manager; }
174
175     const QRawFont &font() const { return m_font; }
176
177     qreal fontScale() const;
178     int distanceFieldRadius() const;
179     int glyphCount() const { return m_glyphCount; }
180     bool doubleGlyphResolution() const { return m_cacheData->doubleGlyphResolution; }
181
182     Metrics glyphMetrics(glyph_t glyph);
183     TexCoord glyphTexCoord(glyph_t glyph) const;
184     const Texture *glyphTexture(glyph_t glyph) const;
185
186     void populate(const QVector<glyph_t> &glyphs);
187     void release(const QVector<glyph_t> &glyphs);
188
189     void update();
190
191     void registerGlyphNode(QSGDistanceFieldGlyphNode *node);
192     void unregisterGlyphNode(QSGDistanceFieldGlyphNode *node);
193
194     virtual void registerOwnerElement(QQuickItem *ownerElement);
195     virtual void unregisterOwnerElement(QQuickItem *ownerElement);
196     virtual void processPendingGlyphs();
197
198 protected:
199     struct GlyphPosition {
200         glyph_t glyph;
201         QPointF position;
202     };
203
204     virtual void requestGlyphs(const QSet<glyph_t> &glyphs) = 0;
205     virtual void storeGlyphs(const QHash<glyph_t, QImage> &glyphs) = 0;
206     virtual void referenceGlyphs(const QSet<glyph_t> &glyphs) = 0;
207     virtual void releaseGlyphs(const QSet<glyph_t> &glyphs) = 0;
208
209     void setGlyphsPosition(const QList<GlyphPosition> &glyphs);
210     void setGlyphsTexture(const QVector<glyph_t> &glyphs, const Texture &tex);
211     void markGlyphsToRender(const QVector<glyph_t> &glyphs);
212     void removeGlyph(glyph_t glyph);
213
214     void updateTexture(GLuint oldTex, GLuint newTex, const QSize &newTexSize);
215
216     bool containsGlyph(glyph_t glyph) const;
217     GLuint textureIdForGlyph(glyph_t glyph) const;
218
219     QOpenGLContext *ctx;
220
221 private:
222     struct GlyphCacheData : public QOpenGLSharedResource {
223         QList<Texture> textures;
224         QHash<glyph_t, Texture*> glyphTextures;
225         QHash<glyph_t, TexCoord> texCoords;
226         QDataBuffer<glyph_t> pendingGlyphs;
227         QHash<glyph_t, QPainterPath> glyphPaths;
228         bool doubleGlyphResolution;
229         QLinkedList<QSGDistanceFieldGlyphNode*> m_registeredNodes;
230         QHash<glyph_t, quint32> glyphRefCount;
231
232         GlyphCacheData(QOpenGLContext *ctx)
233             : QOpenGLSharedResource(ctx->shareGroup())
234             , pendingGlyphs(64)
235             , doubleGlyphResolution(false)
236         {}
237
238         void invalidateResource()
239         {
240             textures.clear();
241             glyphTextures.clear();
242             texCoords.clear();
243         }
244
245         void freeResource(QOpenGLContext *)
246         {
247         }
248     };
249
250     QSGDistanceFieldGlyphCacheManager *m_manager;
251
252     QRawFont m_font;
253     QRawFont m_referenceFont;
254
255     int m_glyphCount;
256     QHash<glyph_t, Metrics> m_metrics;
257
258     GlyphCacheData *cacheData();
259     GlyphCacheData *m_cacheData;
260     static QHash<QString, QOpenGLMultiGroupSharedResource> m_caches_data;
261 };
262
263
264 QT_END_NAMESPACE
265
266 QT_END_HEADER
267
268 #endif