1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #ifndef QSGDEFAULTDISTANCEFIELDGLYPHCACHE_H
43 #define QSGDEFAULTDISTANCEFIELDGLYPHCACHE_H
45 #include "qsgadaptationlayer_p.h"
46 #include <QtGui/qopenglfunctions.h>
47 #include <qopenglshaderprogram.h>
48 #include <QtGui/private/qopenglengineshadersource_p.h>
52 class Q_QUICK_EXPORT QSGDefaultDistanceFieldGlyphCache : public QSGDistanceFieldGlyphCache
55 QSGDefaultDistanceFieldGlyphCache(QSGDistanceFieldGlyphCacheManager *man, QOpenGLContext *c, const QRawFont &font);
57 void requestGlyphs(const QSet<glyph_t> &glyphs);
58 void storeGlyphs(const QHash<glyph_t, QImage> &glyphs);
59 void referenceGlyphs(const QSet<glyph_t> &glyphs);
60 void releaseGlyphs(const QSet<glyph_t> &glyphs);
62 bool cacheIsFull() const { return m_textureData->currY >= maxTextureSize(); }
63 bool useWorkaroundBrokenFBOReadback() const;
64 int maxTextureSize() const;
67 void createTexture(int width, int height);
68 void resizeTexture(int width, int height);
70 mutable int m_maxTextureSize;
72 struct DistanceFieldTextureData : public QOpenGLSharedResource {
76 QSet<glyph_t> unusedGlyphs;
81 QOpenGLShaderProgram *blitProgram;
82 GLfloat blitVertexCoordinateArray[8];
83 GLfloat blitTextureCoordinateArray[8];
85 DistanceFieldTextureData(QOpenGLContext *ctx)
86 : QOpenGLSharedResource(ctx->shareGroup())
93 blitVertexCoordinateArray[0] = -1.0f;
94 blitVertexCoordinateArray[1] = -1.0f;
95 blitVertexCoordinateArray[2] = 1.0f;
96 blitVertexCoordinateArray[3] = -1.0f;
97 blitVertexCoordinateArray[4] = 1.0f;
98 blitVertexCoordinateArray[5] = 1.0f;
99 blitVertexCoordinateArray[6] = -1.0f;
100 blitVertexCoordinateArray[7] = 1.0f;
102 blitTextureCoordinateArray[0] = 0.0f;
103 blitTextureCoordinateArray[1] = 0.0f;
104 blitTextureCoordinateArray[2] = 1.0f;
105 blitTextureCoordinateArray[3] = 0.0f;
106 blitTextureCoordinateArray[4] = 1.0f;
107 blitTextureCoordinateArray[5] = 1.0f;
108 blitTextureCoordinateArray[6] = 0.0f;
109 blitTextureCoordinateArray[7] = 1.0f;
112 void invalidateResource()
121 void freeResource(QOpenGLContext *ctx)
123 glDeleteTextures(1, &texture);
124 ctx->functions()->glDeleteFramebuffers(1, &fbo);
129 void createBlitProgram()
131 blitProgram = new QOpenGLShaderProgram;
134 source.append(QLatin1String(qopenglslMainWithTexCoordsVertexShader));
135 source.append(QLatin1String(qopenglslUntransformedPositionVertexShader));
137 QOpenGLShader *vertexShader = new QOpenGLShader(QOpenGLShader::Vertex, blitProgram);
138 vertexShader->compileSourceCode(source);
140 blitProgram->addShader(vertexShader);
144 source.append(QLatin1String(qopenglslMainFragmentShader));
145 source.append(QLatin1String(qopenglslImageSrcFragmentShader));
147 QOpenGLShader *fragmentShader = new QOpenGLShader(QOpenGLShader::Fragment, blitProgram);
148 fragmentShader->compileSourceCode(source);
150 blitProgram->addShader(fragmentShader);
152 blitProgram->bindAttributeLocation("vertexCoordsArray", QT_VERTEX_COORDS_ATTR);
153 blitProgram->bindAttributeLocation("textureCoordArray", QT_TEXTURE_COORDS_ATTR);
158 DistanceFieldTextureData *textureData(QOpenGLContext *c);
159 DistanceFieldTextureData *m_textureData;
160 static QHash<QString, QOpenGLMultiGroupSharedResource> m_textures_data;
165 #endif // QSGDEFAULTDISTANCEFIELDGLYPHCACHE_H