1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #ifndef ADAPTATIONINTERFACES_H
43 #define ADAPTATIONINTERFACES_H
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>
60 #include <QtQuick/private/qquicktext_p.h>
68 class TextureReference;
69 class QSGDistanceFieldGlyphCacheManager;
70 class QSGDistanceFieldGlyphNode;
72 // TODO: Rename from XInterface to AbstractX.
73 class Q_QUICK_EXPORT QSGRectangleNode : public QSGGeometryNode
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;
84 virtual void update() = 0;
88 class Q_QUICK_EXPORT QSGImageNode : public QSGGeometryNode
91 virtual void setTargetRect(const QRectF &rect) = 0;
92 virtual void setSourceRect(const QRectF &rect) = 0;
93 virtual void setTexture(QSGTexture *texture) = 0;
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;
100 virtual void update() = 0;
104 class Q_QUICK_EXPORT QSGGlyphNode : public QSGGeometryNode
107 enum AntialiasingMode
110 LowQualitySubPixelAntialiasing,
111 HighQualitySubPixelAntialiasing
114 QSGGlyphNode() : m_ownerElement(0) {}
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;
122 virtual QRectF boundingRect() const { return m_bounding_rect; }
123 virtual void setBoundingRect(const QRectF &bounds) { m_bounding_rect = bounds; }
125 virtual void setPreferredAntialiasingMode(AntialiasingMode) = 0;
127 virtual void update() = 0;
129 void setOwnerElement(QQuickItem *ownerElement) { m_ownerElement = ownerElement; }
130 QQuickItem *ownerElement() const { return m_ownerElement; }
133 QRectF m_bounding_rect;
134 QQuickItem *m_ownerElement;
137 class Q_QUICK_EXPORT QSGDistanceFieldGlyphConsumer
140 virtual ~QSGDistanceFieldGlyphConsumer() {}
142 virtual void invalidateGlyphs(const QVector<quint32> &glyphs) = 0;
145 class Q_QUICK_EXPORT QSGDistanceFieldGlyphCache
148 QSGDistanceFieldGlyphCache(QSGDistanceFieldGlyphCacheManager *man, QOpenGLContext *c, const QRawFont &font);
149 virtual ~QSGDistanceFieldGlyphCache();
157 bool isNull() const { return width == 0 || height == 0; }
168 TexCoord() : x(0), y(0), width(-1), height(-1), xMargin(0), yMargin(0) { }
170 bool isNull() const { return width <= 0 || height <= 0; }
171 bool isValid() const { return width >= 0 && height >= 0; }
178 Texture() : textureId(0), size(QSize()) { }
179 bool operator == (const Texture &other) const { return textureId == other.textureId; }
182 const QSGDistanceFieldGlyphCacheManager *manager() const { return m_manager; }
184 const QRawFont &referenceFont() const { return m_referenceFont; }
186 qreal fontScale(qreal pixelSize) const
188 return pixelSize / QT_DISTANCEFIELD_BASEFONTSIZE(m_doubleGlyphResolution);
190 int distanceFieldRadius() const
192 return QT_DISTANCEFIELD_DEFAULT_RADIUS / QT_DISTANCEFIELD_SCALE(m_doubleGlyphResolution);
194 int glyphCount() const { return m_glyphCount; }
195 bool doubleGlyphResolution() const { return m_doubleGlyphResolution; }
197 Metrics glyphMetrics(glyph_t glyph, qreal pixelSize);
198 inline TexCoord glyphTexCoord(glyph_t glyph);
199 inline const Texture *glyphTexture(glyph_t glyph);
201 void populate(const QVector<glyph_t> &glyphs);
202 void release(const QVector<glyph_t> &glyphs);
206 void registerGlyphNode(QSGDistanceFieldGlyphConsumer *node) { m_registeredNodes.append(node); }
207 void unregisterGlyphNode(QSGDistanceFieldGlyphConsumer *node) { m_registeredNodes.removeOne(node); }
209 virtual void registerOwnerElement(QQuickItem *ownerElement);
210 virtual void unregisterOwnerElement(QQuickItem *ownerElement);
211 virtual void processPendingGlyphs();
214 struct GlyphPosition {
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;
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);
229 void updateTexture(GLuint oldTex, GLuint newTex, const QSize &newTexSize);
231 inline bool containsGlyph(glyph_t glyph);
232 GLuint textureIdForGlyph(glyph_t glyph) const;
243 GlyphData() : texture(0), ref(0) { }
246 GlyphData &glyphData(glyph_t glyph);
248 QSGDistanceFieldGlyphCacheManager *m_manager;
250 QRawFont m_referenceFont;
253 bool m_doubleGlyphResolution;
255 QList<Texture> m_textures;
256 QHash<glyph_t, GlyphData> m_glyphsData;
257 QDataBuffer<glyph_t> m_pendingGlyphs;
258 QLinkedList<QSGDistanceFieldGlyphConsumer*> m_registeredNodes;
260 static Texture s_emptyTexture;
263 inline QSGDistanceFieldGlyphCache::TexCoord QSGDistanceFieldGlyphCache::glyphTexCoord(glyph_t glyph)
265 return glyphData(glyph).texCoord;
268 inline const QSGDistanceFieldGlyphCache::Texture *QSGDistanceFieldGlyphCache::glyphTexture(glyph_t glyph)
270 return glyphData(glyph).texture;
273 inline void QSGDistanceFieldGlyphCache::removeGlyph(glyph_t glyph)
275 GlyphData &gd = glyphData(glyph);
276 gd.texCoord = TexCoord();
277 gd.texture = &s_emptyTexture;
280 inline bool QSGDistanceFieldGlyphCache::containsGlyph(glyph_t glyph)
282 return glyphData(glyph).texCoord.isValid();