Use a single distance-field cache instance for all sizes of a given font.
[profile/ivi/qtdeclarative.git] / src / quick / scenegraph / qsgdefaultdistancefieldglyphcache_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 QtQml 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 QSGDEFAULTDISTANCEFIELDGLYPHCACHE_H
43 #define QSGDEFAULTDISTANCEFIELDGLYPHCACHE_H
44
45 #include "qsgadaptationlayer_p.h"
46 #include <QtGui/qopenglfunctions.h>
47 #include <qopenglshaderprogram.h>
48 #include <QtGui/private/qopenglengineshadersource_p.h>
49
50 QT_BEGIN_NAMESPACE
51
52 class Q_QUICK_EXPORT QSGDefaultDistanceFieldGlyphCache : public QSGDistanceFieldGlyphCache
53 {
54 public:
55     QSGDefaultDistanceFieldGlyphCache(QSGDistanceFieldGlyphCacheManager *man, QOpenGLContext *c, const QRawFont &font);
56     virtual ~QSGDefaultDistanceFieldGlyphCache();
57
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);
62
63     bool cacheIsFull() const {
64         return m_textures.count() == m_maxTextureCount
65                 && textureIsFull(m_currentTexture);
66     }
67     bool useWorkaroundBrokenFBOReadback() const;
68     int maxTextureSize() const;
69
70     void setMaxTextureCount(int max) { m_maxTextureCount = max; }
71     int maxTextureCount() const { return m_maxTextureCount; }
72
73 private:
74     struct TextureInfo {
75         GLuint texture;
76         QSize size;
77         int currX;
78         int currY;
79         QImage image;
80
81         TextureInfo() : texture(0), currX(0), currY(0)
82         { }
83     };
84
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(); }
88
89     TextureInfo *createTextureInfo()
90     {
91         m_textures.append(TextureInfo());
92         return &m_textures.last();
93     }
94
95     void createBlitProgram()
96     {
97         m_blitProgram = new QOpenGLShaderProgram;
98         {
99             QString source;
100             source.append(QLatin1String(qopenglslMainWithTexCoordsVertexShader));
101             source.append(QLatin1String(qopenglslUntransformedPositionVertexShader));
102
103             QOpenGLShader *vertexShader = new QOpenGLShader(QOpenGLShader::Vertex, m_blitProgram);
104             vertexShader->compileSourceCode(source);
105
106             m_blitProgram->addShader(vertexShader);
107         }
108         {
109             QString source;
110             source.append(QLatin1String(qopenglslMainFragmentShader));
111             source.append(QLatin1String(qopenglslImageSrcFragmentShader));
112
113             QOpenGLShader *fragmentShader = new QOpenGLShader(QOpenGLShader::Fragment, m_blitProgram);
114             fragmentShader->compileSourceCode(source);
115
116             m_blitProgram->addShader(fragmentShader);
117         }
118         m_blitProgram->bindAttributeLocation("vertexCoordsArray", QT_VERTEX_COORDS_ATTR);
119         m_blitProgram->bindAttributeLocation("textureCoordArray", QT_TEXTURE_COORDS_ATTR);
120         m_blitProgram->link();
121     }
122
123     mutable int m_maxTextureSize;
124     int m_maxTextureCount;
125
126     TextureInfo *m_currentTexture;
127     QList<TextureInfo> m_textures;
128     QHash<glyph_t, TextureInfo *> m_glyphsTexture;
129     GLuint m_fbo;
130     QSet<glyph_t> m_unusedGlyphs;
131
132     QOpenGLShaderProgram *m_blitProgram;
133     GLfloat m_blitVertexCoordinateArray[8];
134     GLfloat m_blitTextureCoordinateArray[8];
135 };
136
137 QT_END_NAMESPACE
138
139 #endif // QSGDEFAULTDISTANCEFIELDGLYPHCACHE_H