1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QOPENGLTEXTUREGLYPHCACHE_P_H
41 #define QOPENGLTEXTUREGLYPHCACHE_P_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API. It exists purely as an
48 // implementation detail. This header file may change from version to
49 // version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include <QtGui/private/qtguiglobal_p.h>
55 #include <private/qtextureglyphcache_p.h>
56 #include <private/qopenglcontext_p.h>
57 #include <qopenglshaderprogram.h>
58 #include <qopenglfunctions.h>
59 #include <qopenglbuffer.h>
60 #include <qopenglvertexarrayobject.h>
61 
62 // #define QT_GL_TEXTURE_GLYPH_CACHE_DEBUG
63 
64 QT_BEGIN_NAMESPACE
65 
66 class QOpenGL2PaintEngineExPrivate;
67 
68 class QOpenGLGlyphTexture : public QOpenGLSharedResource
69 {
70 public:
QOpenGLGlyphTexture(QOpenGLContext * ctx)71     explicit QOpenGLGlyphTexture(QOpenGLContext *ctx)
72         : QOpenGLSharedResource(ctx->shareGroup())
73         , m_width(0)
74         , m_height(0)
75     {
76         if (!ctx->d_func()->workaround_brokenFBOReadBack)
77             QOpenGLFunctions(ctx).glGenFramebuffers(1, &m_fbo);
78 
79 #ifdef QT_GL_TEXTURE_GLYPH_CACHE_DEBUG
80         qDebug(" -> QOpenGLGlyphTexture() %p for context %p.", this, ctx);
81 #endif
82     }
83 
freeResource(QOpenGLContext * context)84     void freeResource(QOpenGLContext *context) override
85     {
86         QOpenGLContext *ctx = context;
87 #ifdef QT_GL_TEXTURE_GLYPH_CACHE_DEBUG
88         qDebug("~QOpenGLGlyphTexture() %p for context %p.", this, ctx);
89 #endif
90         if (!ctx->d_func()->workaround_brokenFBOReadBack)
91             ctx->functions()->glDeleteFramebuffers(1, &m_fbo);
92         if (m_width || m_height)
93             ctx->functions()->glDeleteTextures(1, &m_texture);
94     }
95 
invalidateResource()96     void invalidateResource() override
97     {
98         m_texture = 0;
99         m_fbo = 0;
100         m_width = 0;
101         m_height = 0;
102     }
103 
104     GLuint m_texture;
105     GLuint m_fbo;
106     int m_width;
107     int m_height;
108 };
109 
110 class Q_GUI_EXPORT QOpenGLTextureGlyphCache : public QImageTextureGlyphCache
111 {
112 public:
113     QOpenGLTextureGlyphCache(QFontEngine::GlyphFormat glyphFormat, const QTransform &matrix, const QColor &color = QColor());
114     ~QOpenGLTextureGlyphCache();
115 
116     virtual void createTextureData(int width, int height) override;
117     virtual void resizeTextureData(int width, int height) override;
118     virtual void fillTexture(const Coord &c, glyph_t glyph, QFixed subPixelPosition) override;
119     virtual int glyphPadding() const override;
120     virtual int maxTextureWidth() const override;
121     virtual int maxTextureHeight() const override;
122 
texture()123     inline GLuint texture() const {
124         QOpenGLTextureGlyphCache *that = const_cast<QOpenGLTextureGlyphCache *>(this);
125         QOpenGLGlyphTexture *glyphTexture = that->m_textureResource;
126         return glyphTexture ? glyphTexture->m_texture : 0;
127     }
128 
width()129     inline int width() const {
130         QOpenGLTextureGlyphCache *that = const_cast<QOpenGLTextureGlyphCache *>(this);
131         QOpenGLGlyphTexture *glyphTexture = that->m_textureResource;
132         return glyphTexture ? glyphTexture->m_width : 0;
133     }
height()134     inline int height() const {
135         QOpenGLTextureGlyphCache *that = const_cast<QOpenGLTextureGlyphCache *>(this);
136         QOpenGLGlyphTexture *glyphTexture = that->m_textureResource;
137         return glyphTexture ? glyphTexture->m_height : 0;
138     }
139 
setPaintEnginePrivate(QOpenGL2PaintEngineExPrivate * p)140     inline void setPaintEnginePrivate(QOpenGL2PaintEngineExPrivate *p) { pex = p; }
141 
contextGroup()142     inline const QOpenGLContextGroup *contextGroup() const { return m_textureResource ? m_textureResource->group() : nullptr; }
143 
serialNumber()144     inline int serialNumber() const { return m_serialNumber; }
145 
146     enum FilterMode {
147         Nearest,
148         Linear
149     };
filterMode()150     FilterMode filterMode() const { return m_filterMode; }
setFilterMode(FilterMode m)151     void setFilterMode(FilterMode m) { m_filterMode = m; }
152 
153     void clear();
154 
paintEnginePrivate()155     QOpenGL2PaintEngineExPrivate *paintEnginePrivate() const
156     {
157         return pex;
158     }
159 
160 private:
161     void setupVertexAttribs();
162 
163     QOpenGLGlyphTexture *m_textureResource;
164 
165     QOpenGL2PaintEngineExPrivate *pex;
166     QOpenGLShaderProgram *m_blitProgram;
167     FilterMode m_filterMode;
168 
169     GLfloat m_vertexCoordinateArray[8];
170     GLfloat m_textureCoordinateArray[8];
171 
172     int m_serialNumber;
173 
174     QOpenGLBuffer m_buffer;
175     QOpenGLVertexArrayObject m_vao;
176 };
177 
178 QT_END_NAMESPACE
179 
180 #endif // QOPENGLTEXTUREGLYPHCACHE_P_H
181 
182