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 QtDeclarative 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>
59 #include <QtQuick/private/qquicktext_p.h>
67 class TextureReference;
68 class QSGDistanceFieldGlyphCacheManager;
69 class QSGDistanceFieldGlyphNode;
71 // TODO: Rename from XInterface to AbstractX.
72 class Q_QUICK_EXPORT QSGRectangleNode : public QSGGeometryNode
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;
83 virtual void update() = 0;
87 class Q_QUICK_EXPORT QSGImageNode : public QSGGeometryNode
90 virtual void setTargetRect(const QRectF &rect) = 0;
91 virtual void setSourceRect(const QRectF &rect) = 0;
92 virtual void setTexture(QSGTexture *texture) = 0;
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;
99 virtual void update() = 0;
103 class Q_QUICK_EXPORT QSGGlyphNode : public QSGGeometryNode
106 enum AntialiasingMode
109 LowQualitySubPixelAntialiasing,
110 HighQualitySubPixelAntialiasing
113 QSGGlyphNode() : m_ownerElement(0) {}
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;
121 virtual QRectF boundingRect() const { return m_bounding_rect; }
122 virtual void setBoundingRect(const QRectF &bounds) { m_bounding_rect = bounds; }
124 virtual void setPreferredAntialiasingMode(AntialiasingMode) = 0;
126 virtual void update() = 0;
128 void setOwnerElement(QQuickItem *ownerElement) { m_ownerElement = ownerElement; }
129 QQuickItem *ownerElement() const { return m_ownerElement; }
132 QRectF m_bounding_rect;
133 QQuickItem *m_ownerElement;
136 class Q_QUICK_EXPORT QSGDistanceFieldGlyphCache
139 QSGDistanceFieldGlyphCache(QSGDistanceFieldGlyphCacheManager *man, QOpenGLContext *c, const QRawFont &font);
140 virtual ~QSGDistanceFieldGlyphCache();
148 bool isNull() const { return width == 0 || height == 0; }
159 TexCoord() : x(0), y(0), width(-1), height(-1), xMargin(0), yMargin(0) { }
161 bool isNull() const { return width <= 0 || height <= 0; }
162 bool isValid() const { return width >= 0 && height >= 0; }
169 Texture() : textureId(0), size(QSize()) { }
170 bool operator == (const Texture &other) const { return textureId == other.textureId; }
173 const QSGDistanceFieldGlyphCacheManager *manager() const { return m_manager; }
175 const QRawFont &font() const { return m_font; }
177 qreal fontScale() const;
178 int distanceFieldRadius() const;
179 int glyphCount() const { return m_glyphCount; }
180 bool doubleGlyphResolution() const { return m_cacheData->doubleGlyphResolution; }
182 Metrics glyphMetrics(glyph_t glyph);
183 TexCoord glyphTexCoord(glyph_t glyph) const;
184 const Texture *glyphTexture(glyph_t glyph) const;
186 void populate(const QVector<glyph_t> &glyphs);
187 void release(const QVector<glyph_t> &glyphs);
191 void registerGlyphNode(QSGDistanceFieldGlyphNode *node);
192 void unregisterGlyphNode(QSGDistanceFieldGlyphNode *node);
194 virtual void registerOwnerElement(QQuickItem *ownerElement);
195 virtual void unregisterOwnerElement(QQuickItem *ownerElement);
196 virtual void processPendingGlyphs();
199 struct GlyphPosition {
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;
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);
214 void updateTexture(GLuint oldTex, GLuint newTex, const QSize &newTexSize);
216 bool containsGlyph(glyph_t glyph) const;
217 GLuint textureIdForGlyph(glyph_t glyph) const;
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;
232 GlyphCacheData(QOpenGLContext *ctx)
233 : QOpenGLSharedResource(ctx->shareGroup())
235 , doubleGlyphResolution(false)
238 void invalidateResource()
241 glyphTextures.clear();
245 void freeResource(QOpenGLContext *)
250 QSGDistanceFieldGlyphCacheManager *m_manager;
253 QRawFont m_referenceFont;
256 QHash<glyph_t, Metrics> m_metrics;
258 GlyphCacheData *cacheData();
259 GlyphCacheData *m_cacheData;
260 static QHash<QString, QOpenGLMultiGroupSharedResource> m_caches_data;