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 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);
56 virtual ~QSGDefaultDistanceFieldGlyphCache();
58 void requestGlyphs(const QSet<glyph_t> &glyphs);
59 void storeGlyphs(const QHash<glyph_t, QImage> &glyphs);
60 void referenceGlyphs(const QSet<glyph_t> &glyphs);
61 void releaseGlyphs(const QSet<glyph_t> &glyphs);
63 bool cacheIsFull() const {
64 return m_textures.count() == m_maxTextureCount
65 && textureIsFull(m_currentTexture);
67 bool useWorkaroundBrokenFBOReadback() const;
68 int maxTextureSize() const;
70 void setMaxTextureCount(int max) { m_maxTextureCount = max; }
71 int maxTextureCount() const { return m_maxTextureCount; }
81 TextureInfo() : texture(0), currX(0), currY(0)
85 void createTexture(TextureInfo * texInfo, int width, int height);
86 void resizeTexture(TextureInfo * texInfo, int width, int height);
87 bool textureIsFull (const TextureInfo *tex) const { return tex->currY >= maxTextureSize(); }
89 TextureInfo *createTextureInfo()
91 m_textures.append(TextureInfo());
92 return &m_textures.last();
95 void createBlitProgram()
97 m_blitProgram = new QOpenGLShaderProgram;
100 source.append(QLatin1String(qopenglslMainWithTexCoordsVertexShader));
101 source.append(QLatin1String(qopenglslUntransformedPositionVertexShader));
103 QOpenGLShader *vertexShader = new QOpenGLShader(QOpenGLShader::Vertex, m_blitProgram);
104 vertexShader->compileSourceCode(source);
106 m_blitProgram->addShader(vertexShader);
110 source.append(QLatin1String(qopenglslMainFragmentShader));
111 source.append(QLatin1String(qopenglslImageSrcFragmentShader));
113 QOpenGLShader *fragmentShader = new QOpenGLShader(QOpenGLShader::Fragment, m_blitProgram);
114 fragmentShader->compileSourceCode(source);
116 m_blitProgram->addShader(fragmentShader);
118 m_blitProgram->bindAttributeLocation("vertexCoordsArray", QT_VERTEX_COORDS_ATTR);
119 m_blitProgram->bindAttributeLocation("textureCoordArray", QT_TEXTURE_COORDS_ATTR);
120 m_blitProgram->link();
123 mutable int m_maxTextureSize;
124 int m_maxTextureCount;
126 TextureInfo *m_currentTexture;
127 QList<TextureInfo> m_textures;
128 QHash<glyph_t, TextureInfo *> m_glyphsTexture;
130 QSet<glyph_t> m_unusedGlyphs;
132 QOpenGLShaderProgram *m_blitProgram;
133 GLfloat m_blitVertexCoordinateArray[8];
134 GLfloat m_blitTextureCoordinateArray[8];
139 #endif // QSGDEFAULTDISTANCEFIELDGLYPHCACHE_H