Remove "All rights reserved" line from license headers.
[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 QtDeclarative 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
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);
61
62     bool cacheIsFull() const { return m_textureData->currY >= maxTextureSize(); }
63     bool useWorkaroundBrokenFBOReadback() const;
64     int maxTextureSize() const;
65
66 private:
67     void createTexture(int width, int height);
68     void resizeTexture(int width, int height);
69
70     mutable int m_maxTextureSize;
71
72     struct DistanceFieldTextureData : public QOpenGLSharedResource {
73         GLuint texture;
74         GLuint fbo;
75         QSize size;
76         QSet<glyph_t> unusedGlyphs;
77         int currX;
78         int currY;
79         QImage image;
80
81         QOpenGLShaderProgram *blitProgram;
82         GLfloat blitVertexCoordinateArray[8];
83         GLfloat blitTextureCoordinateArray[8];
84
85         DistanceFieldTextureData(QOpenGLContext *ctx)
86             : QOpenGLSharedResource(ctx->shareGroup())
87             , texture(0)
88             , fbo(0)
89             , currX(0)
90             , currY(0)
91             , blitProgram(0)
92         {
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;
101
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;
110         }
111
112         void invalidateResource()
113         {
114             texture = 0;
115             fbo = 0;
116             size = QSize();
117             delete blitProgram;
118             blitProgram = 0;
119         }
120
121         void freeResource(QOpenGLContext *ctx)
122         {
123             glDeleteTextures(1, &texture);
124             ctx->functions()->glDeleteFramebuffers(1, &fbo);
125             delete blitProgram;
126             blitProgram = 0;
127         }
128
129         void createBlitProgram()
130         {
131             blitProgram = new QOpenGLShaderProgram;
132             {
133                 QString source;
134                 source.append(QLatin1String(qopenglslMainWithTexCoordsVertexShader));
135                 source.append(QLatin1String(qopenglslUntransformedPositionVertexShader));
136
137                 QOpenGLShader *vertexShader = new QOpenGLShader(QOpenGLShader::Vertex, blitProgram);
138                 vertexShader->compileSourceCode(source);
139
140                 blitProgram->addShader(vertexShader);
141             }
142             {
143                 QString source;
144                 source.append(QLatin1String(qopenglslMainFragmentShader));
145                 source.append(QLatin1String(qopenglslImageSrcFragmentShader));
146
147                 QOpenGLShader *fragmentShader = new QOpenGLShader(QOpenGLShader::Fragment, blitProgram);
148                 fragmentShader->compileSourceCode(source);
149
150                 blitProgram->addShader(fragmentShader);
151             }
152             blitProgram->bindAttributeLocation("vertexCoordsArray", QT_VERTEX_COORDS_ATTR);
153             blitProgram->bindAttributeLocation("textureCoordArray", QT_TEXTURE_COORDS_ATTR);
154             blitProgram->link();
155         }
156     };
157
158     DistanceFieldTextureData *textureData(QOpenGLContext *c);
159     DistanceFieldTextureData *m_textureData;
160     static QHash<QString, QOpenGLMultiGroupSharedResource> m_textures_data;
161 };
162
163 QT_END_NAMESPACE
164
165 #endif // QSGDEFAULTDISTANCEFIELDGLYPHCACHE_H