| 1 | // Copyright (C) 2019 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 QSGRHIDISTANCEFIELDGLYPHCACHE_H |
| 5 | #define QSGRHIDISTANCEFIELDGLYPHCACHE_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 "qsgadaptationlayer_p.h" |
| 19 | #include <private/qsgareaallocator_p.h> |
| 20 | #include <rhi/qrhi.h> |
| 21 | |
| 22 | QT_BEGIN_NAMESPACE |
| 23 | |
| 24 | class QSGDefaultRenderContext; |
| 25 | |
| 26 | class Q_QUICK_EXPORT QSGRhiDistanceFieldGlyphCache : public QSGDistanceFieldGlyphCache |
| 27 | { |
| 28 | public: |
| 29 | QSGRhiDistanceFieldGlyphCache(QSGDefaultRenderContext *rc, const QRawFont &font, int renderTypeQuality); |
| 30 | virtual ~QSGRhiDistanceFieldGlyphCache(); |
| 31 | |
| 32 | void requestGlyphs(const QSet<glyph_t> &glyphs) override; |
| 33 | void storeGlyphs(const QList<QDistanceField> &glyphs) override; |
| 34 | void referenceGlyphs(const QSet<glyph_t> &glyphs) override; |
| 35 | void releaseGlyphs(const QSet<glyph_t> &glyphs) override; |
| 36 | |
| 37 | bool useTextureResizeWorkaround() const; |
| 38 | bool createFullSizeTextures() const; |
| 39 | bool isActive() const override; |
| 40 | int maxTextureSize() const; |
| 41 | |
| 42 | void setMaxTextureCount(int max) { m_maxTextureCount = max; } |
| 43 | int maxTextureCount() const { return m_maxTextureCount; } |
| 44 | |
| 45 | void commitResourceUpdates(QRhiResourceUpdateBatch *mergeInto); |
| 46 | |
| 47 | bool eightBitFormatIsAlphaSwizzled() const override; |
| 48 | bool screenSpaceDerivativesSupported() const override; |
| 49 | |
| 50 | #if defined(QSG_DISTANCEFIELD_CACHE_DEBUG) |
| 51 | void saveTexture(QRhiTexture *texture, const QString &nameBase) const override; |
| 52 | #endif |
| 53 | |
| 54 | private: |
| 55 | bool loadPregeneratedCache(const QRawFont &font); |
| 56 | |
| 57 | struct TextureInfo { |
| 58 | QRhiTexture *texture; |
| 59 | QSize size; |
| 60 | QRect allocatedArea; |
| 61 | QDistanceField image; |
| 62 | int padding = -1; |
| 63 | QVarLengthArray<QRhiTextureUploadEntry, 16> uploads; |
| 64 | |
| 65 | TextureInfo(const QRect &preallocRect = QRect()) : texture(nullptr), allocatedArea(preallocRect) { } |
| 66 | }; |
| 67 | |
| 68 | void createTexture(TextureInfo *texInfo, int width, int height, const void *pixels); |
| 69 | void createTexture(TextureInfo *texInfo, int width, int height); |
| 70 | void resizeTexture(TextureInfo *texInfo, int width, int height); |
| 71 | |
| 72 | TextureInfo *textureInfo(int index) |
| 73 | { |
| 74 | for (int i = m_textures.size(); i <= index; ++i) { |
| 75 | if (createFullSizeTextures()) |
| 76 | m_textures.append(t: QRect(0, 0, maxTextureSize(), maxTextureSize())); |
| 77 | else |
| 78 | m_textures.append(t: TextureInfo()); |
| 79 | } |
| 80 | |
| 81 | return &m_textures[index]; |
| 82 | } |
| 83 | |
| 84 | QSGDefaultRenderContext *m_rc; |
| 85 | QRhi *m_rhi; |
| 86 | mutable int m_maxTextureSize = 0; |
| 87 | int m_maxTextureCount = 3; |
| 88 | QSGAreaAllocator *m_areaAllocator = nullptr; |
| 89 | QList<TextureInfo> m_textures; |
| 90 | QHash<glyph_t, TextureInfo *> m_glyphsTexture; |
| 91 | QSet<glyph_t> m_unusedGlyphs; |
| 92 | QSet<glyph_t> m_referencedGlyphs; |
| 93 | QSet<QRhiTexture *> m_pendingDispose; |
| 94 | }; |
| 95 | |
| 96 | QT_END_NAMESPACE |
| 97 | |
| 98 | #endif // QSGRHIDISTANCEFIELDGLYPHCACHE_H |
| 99 | |