| 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 QtQuick 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 QSGOPENGLDISTANCEFIELDGLYPHCACHE_H |
| 41 | #define QSGOPENGLDISTANCEFIELDGLYPHCACHE_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 "qsgadaptationlayer_p.h" |
| 55 | #include <QtGui/qopenglfunctions.h> |
| 56 | #include <qopenglshaderprogram.h> |
| 57 | #include <qopenglbuffer.h> |
| 58 | #include <qopenglvertexarrayobject.h> |
| 59 | #include <QtGui/private/qopenglengineshadersource_p.h> |
| 60 | #include <private/qsgareaallocator_p.h> |
| 61 | |
| 62 | QT_BEGIN_NAMESPACE |
| 63 | |
| 64 | class QOpenGLSharedResourceGuard; |
| 65 | #if !defined(QT_OPENGL_ES_2) |
| 66 | class QOpenGLFunctions_3_2_Core; |
| 67 | #endif |
| 68 | |
| 69 | class Q_QUICK_PRIVATE_EXPORT QSGOpenGLDistanceFieldGlyphCache : public QSGDistanceFieldGlyphCache |
| 70 | { |
| 71 | public: |
| 72 | QSGOpenGLDistanceFieldGlyphCache(QOpenGLContext *c, const QRawFont &font); |
| 73 | virtual ~QSGOpenGLDistanceFieldGlyphCache(); |
| 74 | |
| 75 | void requestGlyphs(const QSet<glyph_t> &glyphs) override; |
| 76 | void storeGlyphs(const QList<QDistanceField> &glyphs) override; |
| 77 | void referenceGlyphs(const QSet<glyph_t> &glyphs) override; |
| 78 | void releaseGlyphs(const QSet<glyph_t> &glyphs) override; |
| 79 | |
| 80 | bool useTextureResizeWorkaround() const; |
| 81 | bool useTextureUploadWorkaround() const; |
| 82 | bool createFullSizeTextures() const; |
| 83 | |
| 84 | void setMaxTextureCount(int max) { m_maxTextureCount = max; } |
| 85 | int maxTextureCount() const { return m_maxTextureCount; } |
| 86 | |
| 87 | bool eightBitFormatIsAlphaSwizzled() const override { return !m_coreProfile; } |
| 88 | |
| 89 | private: |
| 90 | bool loadPregeneratedCache(const QRawFont &font); |
| 91 | inline bool isCoreProfile() const { return m_coreProfile; } |
| 92 | |
| 93 | struct TextureInfo { |
| 94 | GLuint texture; |
| 95 | QSize size; |
| 96 | QRect allocatedArea; |
| 97 | QDistanceField image; |
| 98 | int padding = -1; |
| 99 | |
| 100 | TextureInfo(const QRect &preallocRect = QRect(0, 0, 1, 1)) : texture(0), allocatedArea(preallocRect) { } |
| 101 | }; |
| 102 | |
| 103 | void createTexture(TextureInfo * texInfo, int width, int height, const void *pixels); |
| 104 | void createTexture(TextureInfo * texInfo, int width, int height); |
| 105 | void resizeTexture(TextureInfo * texInfo, int width, int height); |
| 106 | |
| 107 | TextureInfo *textureInfo(int index) |
| 108 | { |
| 109 | Q_ASSERT(m_maxTextureWidth > 0 && m_maxTextureHeight > 0); |
| 110 | for (int i = m_textures.count(); i <= index; ++i) { |
| 111 | if (createFullSizeTextures()) |
| 112 | m_textures.append(t: QRect(0, 0, m_maxTextureWidth, m_maxTextureWidth)); |
| 113 | else |
| 114 | m_textures.append(t: TextureInfo()); |
| 115 | } |
| 116 | |
| 117 | return &m_textures[index]; |
| 118 | } |
| 119 | |
| 120 | void createBlitProgram() |
| 121 | { |
| 122 | m_blitProgram = new QOpenGLShaderProgram; |
| 123 | { |
| 124 | const QString source = QLatin1String(qopenglslMainWithTexCoordsVertexShader) |
| 125 | + QLatin1String(qopenglslUntransformedPositionVertexShader); |
| 126 | |
| 127 | m_blitProgram->addCacheableShaderFromSourceCode(type: QOpenGLShader::Vertex, source); |
| 128 | } |
| 129 | { |
| 130 | const QString source = QLatin1String(qopenglslMainFragmentShader) |
| 131 | + QLatin1String(qopenglslImageSrcFragmentShader); |
| 132 | |
| 133 | m_blitProgram->addCacheableShaderFromSourceCode(type: QOpenGLShader::Fragment, source); |
| 134 | } |
| 135 | m_blitProgram->bindAttributeLocation(name: "vertexCoordsArray" , location: QT_VERTEX_COORDS_ATTR); |
| 136 | m_blitProgram->bindAttributeLocation(name: "textureCoordArray" , location: QT_TEXTURE_COORDS_ATTR); |
| 137 | m_blitProgram->link(); |
| 138 | } |
| 139 | |
| 140 | int m_maxTextureWidth; |
| 141 | int m_maxTextureHeight; |
| 142 | int m_maxTextureCount; |
| 143 | bool m_coreProfile; |
| 144 | |
| 145 | QList<TextureInfo> m_textures; |
| 146 | QHash<glyph_t, TextureInfo *> m_glyphsTexture; |
| 147 | QSet<glyph_t> m_unusedGlyphs; |
| 148 | |
| 149 | QSGAreaAllocator *m_areaAllocator; |
| 150 | |
| 151 | QOpenGLShaderProgram *m_blitProgram; |
| 152 | QOpenGLBuffer m_blitBuffer; |
| 153 | QOpenGLVertexArrayObject m_vao; |
| 154 | |
| 155 | QOpenGLSharedResourceGuard *m_fboGuard; |
| 156 | QOpenGLFunctions *m_funcs; |
| 157 | #if !defined(QT_OPENGL_ES_2) |
| 158 | QOpenGLFunctions_3_2_Core *m_coreFuncs; |
| 159 | #endif |
| 160 | }; |
| 161 | |
| 162 | QT_END_NAMESPACE |
| 163 | |
| 164 | #endif // QSGOPENGLDISTANCEFIELDGLYPHCACHE_H |
| 165 | |