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 QtOpenGL 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 QTEXTUREGLYPHCACHE_GL_P_H |
41 | #define QTEXTUREGLYPHCACHE_GL_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 <private/qtextureglyphcache_p.h> |
55 | #include <private/qgl_p.h> |
56 | #include <qglshaderprogram.h> |
57 | #include <qglframebufferobject.h> |
58 | #include <qopenglfunctions.h> |
59 | |
60 | // #define QT_GL_TEXTURE_GLYPH_CACHE_DEBUG |
61 | |
62 | QT_BEGIN_NAMESPACE |
63 | |
64 | class QGL2PaintEngineExPrivate; |
65 | |
66 | struct QGLGlyphTexture : public QOpenGLSharedResource |
67 | { |
68 | QGLGlyphTexture(const QGLContext *ctx) |
69 | : QOpenGLSharedResource(ctx->contextHandle()->shareGroup()) |
70 | , m_fbo(0) |
71 | , m_width(0) |
72 | , m_height(0) |
73 | { |
74 | if (ctx && QGLFramebufferObject::hasOpenGLFramebufferObjects() && !ctx->d_ptr->workaround_brokenFBOReadBack) |
75 | ctx->contextHandle()->functions()->glGenFramebuffers(n: 1, framebuffers: &m_fbo); |
76 | |
77 | #ifdef QT_GL_TEXTURE_GLYPH_CACHE_DEBUG |
78 | qDebug(" -> QGLGlyphTexture() %p for context %p." , this, ctx); |
79 | #endif |
80 | } |
81 | |
82 | void freeResource(QOpenGLContext *context) override |
83 | { |
84 | const QGLContext *ctx = QGLContext::fromOpenGLContext(platformContext: context); |
85 | #ifdef QT_GL_TEXTURE_GLYPH_CACHE_DEBUG |
86 | qDebug("~QGLGlyphTexture() %p for context %p." , this, ctx); |
87 | #else |
88 | Q_UNUSED(ctx); |
89 | #endif |
90 | if (ctx && m_fbo) |
91 | ctx->contextHandle()->functions()->glDeleteFramebuffers(n: 1, framebuffers: &m_fbo); |
92 | if (m_width || m_height) |
93 | ctx->contextHandle()->functions()->glDeleteTextures(n: 1, textures: &m_texture); |
94 | } |
95 | |
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_OPENGL_EXPORT QGLTextureGlyphCache : public QImageTextureGlyphCache |
111 | { |
112 | public: |
113 | QGLTextureGlyphCache(QFontEngine::GlyphFormat format, const QTransform &matrix); |
114 | ~QGLTextureGlyphCache(); |
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 | |
123 | inline GLuint texture() const { |
124 | QGLTextureGlyphCache *that = const_cast<QGLTextureGlyphCache *>(this); |
125 | QGLGlyphTexture *glyphTexture = that->m_textureResource; |
126 | return glyphTexture ? glyphTexture->m_texture : 0; |
127 | } |
128 | |
129 | inline int width() const { |
130 | QGLTextureGlyphCache *that = const_cast<QGLTextureGlyphCache *>(this); |
131 | QGLGlyphTexture *glyphTexture = that->m_textureResource; |
132 | return glyphTexture ? glyphTexture->m_width : 0; |
133 | } |
134 | inline int height() const { |
135 | QGLTextureGlyphCache *that = const_cast<QGLTextureGlyphCache *>(this); |
136 | QGLGlyphTexture *glyphTexture = that->m_textureResource; |
137 | return glyphTexture ? glyphTexture->m_height : 0; |
138 | } |
139 | |
140 | inline void setPaintEnginePrivate(QGL2PaintEngineExPrivate *p) { pex = p; } |
141 | |
142 | inline const QOpenGLContextGroup *contextGroup() const { return m_textureResource ? m_textureResource->group() : nullptr; } |
143 | |
144 | inline int serialNumber() const { return m_serialNumber; } |
145 | |
146 | enum FilterMode { |
147 | Nearest, |
148 | Linear |
149 | }; |
150 | FilterMode filterMode() const { return m_filterMode; } |
151 | void setFilterMode(FilterMode m) { m_filterMode = m; } |
152 | |
153 | void clear(); |
154 | |
155 | private: |
156 | QGLGlyphTexture *m_textureResource; |
157 | |
158 | QGL2PaintEngineExPrivate *pex; |
159 | QGLShaderProgram *m_blitProgram; |
160 | FilterMode m_filterMode; |
161 | |
162 | GLfloat m_vertexCoordinateArray[8]; |
163 | GLfloat m_textureCoordinateArray[8]; |
164 | |
165 | int m_serialNumber; |
166 | }; |
167 | |
168 | QT_END_NAMESPACE |
169 | |
170 | #endif |
171 | |
172 | |