| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QOPENGLTEXTUREGLYPHCACHE_P_H |
| 5 | #define QOPENGLTEXTUREGLYPHCACHE_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtOpenGL/qtopenglglobal.h> |
| 19 | #include <private/qtextureglyphcache_p.h> |
| 20 | #include <private/qopenglcontext_p.h> |
| 21 | #include <qopenglshaderprogram.h> |
| 22 | #include <qopenglfunctions.h> |
| 23 | #include <qopenglbuffer.h> |
| 24 | #include <qopenglvertexarrayobject.h> |
| 25 | |
| 26 | // #define QT_GL_TEXTURE_GLYPH_CACHE_DEBUG |
| 27 | |
| 28 | QT_BEGIN_NAMESPACE |
| 29 | |
| 30 | class QOpenGL2PaintEngineExPrivate; |
| 31 | |
| 32 | class QOpenGLGlyphTexture : public QOpenGLSharedResource |
| 33 | { |
| 34 | public: |
| 35 | explicit QOpenGLGlyphTexture(QOpenGLContext *ctx) |
| 36 | : QOpenGLSharedResource(ctx->shareGroup()) |
| 37 | , m_width(0) |
| 38 | , m_height(0) |
| 39 | { |
| 40 | if (!ctx->d_func()->workaround_brokenFBOReadBack) |
| 41 | QOpenGLFunctions(ctx).glGenFramebuffers(n: 1, framebuffers: &m_fbo); |
| 42 | |
| 43 | #ifdef QT_GL_TEXTURE_GLYPH_CACHE_DEBUG |
| 44 | qDebug(" -> QOpenGLGlyphTexture() %p for context %p." , this, ctx); |
| 45 | #endif |
| 46 | } |
| 47 | |
| 48 | void freeResource(QOpenGLContext *context) override |
| 49 | { |
| 50 | QOpenGLContext *ctx = context; |
| 51 | #ifdef QT_GL_TEXTURE_GLYPH_CACHE_DEBUG |
| 52 | qDebug("~QOpenGLGlyphTexture() %p for context %p." , this, ctx); |
| 53 | #endif |
| 54 | if (!ctx->d_func()->workaround_brokenFBOReadBack) |
| 55 | ctx->functions()->glDeleteFramebuffers(n: 1, framebuffers: &m_fbo); |
| 56 | if (m_width || m_height) |
| 57 | ctx->functions()->glDeleteTextures(n: 1, textures: &m_texture); |
| 58 | } |
| 59 | |
| 60 | void invalidateResource() override |
| 61 | { |
| 62 | m_texture = 0; |
| 63 | m_fbo = 0; |
| 64 | m_width = 0; |
| 65 | m_height = 0; |
| 66 | } |
| 67 | |
| 68 | GLuint m_texture; |
| 69 | GLuint m_fbo; |
| 70 | int m_width; |
| 71 | int m_height; |
| 72 | }; |
| 73 | |
| 74 | class Q_OPENGL_EXPORT QOpenGLTextureGlyphCache : public QImageTextureGlyphCache |
| 75 | { |
| 76 | public: |
| 77 | QOpenGLTextureGlyphCache(QFontEngine::GlyphFormat glyphFormat, const QTransform &matrix, const QColor &color = QColor()); |
| 78 | ~QOpenGLTextureGlyphCache(); |
| 79 | |
| 80 | virtual void createTextureData(int width, int height) override; |
| 81 | virtual void resizeTextureData(int width, int height) override; |
| 82 | virtual void fillTexture(const Coord &c, |
| 83 | glyph_t glyph, |
| 84 | const QFixedPoint &subPixelPosition) override; |
| 85 | virtual int glyphPadding() const override; |
| 86 | virtual int maxTextureWidth() const override; |
| 87 | virtual int maxTextureHeight() const override; |
| 88 | |
| 89 | inline GLuint texture() const { |
| 90 | QOpenGLTextureGlyphCache *that = const_cast<QOpenGLTextureGlyphCache *>(this); |
| 91 | QOpenGLGlyphTexture *glyphTexture = that->m_textureResource; |
| 92 | return glyphTexture ? glyphTexture->m_texture : 0; |
| 93 | } |
| 94 | |
| 95 | inline int width() const { |
| 96 | QOpenGLTextureGlyphCache *that = const_cast<QOpenGLTextureGlyphCache *>(this); |
| 97 | QOpenGLGlyphTexture *glyphTexture = that->m_textureResource; |
| 98 | return glyphTexture ? glyphTexture->m_width : 0; |
| 99 | } |
| 100 | inline int height() const { |
| 101 | QOpenGLTextureGlyphCache *that = const_cast<QOpenGLTextureGlyphCache *>(this); |
| 102 | QOpenGLGlyphTexture *glyphTexture = that->m_textureResource; |
| 103 | return glyphTexture ? glyphTexture->m_height : 0; |
| 104 | } |
| 105 | |
| 106 | inline void setPaintEnginePrivate(QOpenGL2PaintEngineExPrivate *p) { pex = p; } |
| 107 | |
| 108 | inline const QOpenGLContextGroup *contextGroup() const { return m_textureResource ? m_textureResource->group() : nullptr; } |
| 109 | |
| 110 | inline int serialNumber() const { return m_serialNumber; } |
| 111 | |
| 112 | enum FilterMode { |
| 113 | Nearest, |
| 114 | Linear |
| 115 | }; |
| 116 | FilterMode filterMode() const { return m_filterMode; } |
| 117 | void setFilterMode(FilterMode m) { m_filterMode = m; } |
| 118 | |
| 119 | void clear(); |
| 120 | |
| 121 | QOpenGL2PaintEngineExPrivate *paintEnginePrivate() const |
| 122 | { |
| 123 | return pex; |
| 124 | } |
| 125 | |
| 126 | private: |
| 127 | void setupVertexAttribs(); |
| 128 | |
| 129 | QOpenGLGlyphTexture *m_textureResource; |
| 130 | |
| 131 | QOpenGL2PaintEngineExPrivate *pex; |
| 132 | QOpenGLShaderProgram *m_blitProgram; |
| 133 | FilterMode m_filterMode; |
| 134 | |
| 135 | GLfloat m_vertexCoordinateArray[8]; |
| 136 | GLfloat m_textureCoordinateArray[8]; |
| 137 | |
| 138 | int m_serialNumber; |
| 139 | |
| 140 | QOpenGLBuffer m_buffer; |
| 141 | QOpenGLVertexArrayObject m_vao; |
| 142 | }; |
| 143 | |
| 144 | QT_END_NAMESPACE |
| 145 | |
| 146 | #endif // QOPENGLTEXTUREGLYPHCACHE_P_H |
| 147 | |
| 148 | |