| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2019 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 QSGRHIATLASTEXTURE_P_H | 
| 41 | #define QSGRHIATLASTEXTURE_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 <QtCore/QSize> | 
| 55 | #include <QtQuick/private/qsgplaintexture_p.h> | 
| 56 | #include <QtQuick/private/qsgareaallocator_p.h> | 
| 57 | #include <QtGui/QSurface> | 
| 58 | #include <QtGui/private/qrhi_p.h> | 
| 59 |  | 
| 60 | QT_BEGIN_NAMESPACE | 
| 61 |  | 
| 62 | class QSGDefaultRenderContext; | 
| 63 |  | 
| 64 | namespace QSGCompressedAtlasTexture { | 
| 65 |     class Atlas; | 
| 66 | } | 
| 67 | class QSGCompressedTextureFactory; | 
| 68 |  | 
| 69 | namespace QSGRhiAtlasTexture | 
| 70 | { | 
| 71 |  | 
| 72 | class Texture; | 
| 73 | class TextureBase; | 
| 74 | class TextureBasePrivate; | 
| 75 | class Atlas; | 
| 76 |  | 
| 77 | class Manager : public QObject | 
| 78 | { | 
| 79 |     Q_OBJECT | 
| 80 |  | 
| 81 | public: | 
| 82 |     Manager(QSGDefaultRenderContext *rc, const QSize &surfacePixelSize, QSurface *maybeSurface); | 
| 83 |     ~Manager(); | 
| 84 |  | 
| 85 |     QSGTexture *create(const QImage &image, bool hasAlphaChannel); | 
| 86 |     QSGTexture *create(const QSGCompressedTextureFactory *factory); | 
| 87 |     void invalidate(); | 
| 88 |  | 
| 89 | private: | 
| 90 |     QSGDefaultRenderContext *m_rc; | 
| 91 |     QRhi *m_rhi; | 
| 92 |     Atlas *m_atlas = nullptr; | 
| 93 |     // set of atlases for different compressed formats | 
| 94 |     QHash<unsigned int, QSGCompressedAtlasTexture::Atlas*> m_atlases; | 
| 95 |  | 
| 96 |     QSize m_atlas_size; | 
| 97 |     int m_atlas_size_limit; | 
| 98 | }; | 
| 99 |  | 
| 100 | class AtlasBase : public QObject | 
| 101 | { | 
| 102 |     Q_OBJECT | 
| 103 | public: | 
| 104 |     AtlasBase(QSGDefaultRenderContext *rc, const QSize &size); | 
| 105 |     ~AtlasBase(); | 
| 106 |  | 
| 107 |     void invalidate(); | 
| 108 |     void updateRhiTexture(QRhiResourceUpdateBatch *resourceUpdates); | 
| 109 |     void remove(TextureBase *t); | 
| 110 |  | 
| 111 |     QSGDefaultRenderContext *renderContext() const { return m_rc; } | 
| 112 |     QRhi *rhi() const { return m_rhi; } | 
| 113 |     QRhiTexture *texture() const { return m_texture; } | 
| 114 |     QSize size() const { return m_size; } | 
| 115 |  | 
| 116 | protected: | 
| 117 |     virtual bool generateTexture() = 0; | 
| 118 |     virtual void enqueueTextureUpload(TextureBase *t, QRhiResourceUpdateBatch *resourceUpdates) = 0; | 
| 119 |  | 
| 120 | protected: | 
| 121 |     QSGDefaultRenderContext *m_rc; | 
| 122 |     QRhi *m_rhi; | 
| 123 |     QSGAreaAllocator m_allocator; | 
| 124 |     QRhiTexture *m_texture = nullptr; | 
| 125 |     QSize m_size; | 
| 126 |     QVector<TextureBase *> m_pending_uploads; | 
| 127 |     friend class TextureBase; | 
| 128 |     friend class TextureBasePrivate; | 
| 129 |  | 
| 130 | private: | 
| 131 |     bool m_allocated = false; | 
| 132 | }; | 
| 133 |  | 
| 134 | class Atlas : public AtlasBase | 
| 135 | { | 
| 136 | public: | 
| 137 |     Atlas(QSGDefaultRenderContext *rc, const QSize &size); | 
| 138 |     ~Atlas(); | 
| 139 |  | 
| 140 |     bool generateTexture() override; | 
| 141 |     void enqueueTextureUpload(TextureBase *t, QRhiResourceUpdateBatch *resourceUpdates) override; | 
| 142 |  | 
| 143 |     Texture *create(const QImage &image); | 
| 144 |  | 
| 145 |     QRhiTexture::Format format() const { return m_format; } | 
| 146 |  | 
| 147 | private: | 
| 148 |     QRhiTexture::Format m_format; | 
| 149 |     int m_atlas_transient_image_threshold = 0; | 
| 150 |  | 
| 151 |     uint m_debug_overlay : 1; | 
| 152 | }; | 
| 153 |  | 
| 154 | class TextureBase : public QSGTexture | 
| 155 | { | 
| 156 |     Q_DECLARE_PRIVATE(TextureBase) | 
| 157 |     Q_OBJECT | 
| 158 | public: | 
| 159 |     TextureBase(AtlasBase *atlas, const QRect &textureRect); | 
| 160 |     ~TextureBase(); | 
| 161 |  | 
| 162 |     int textureId() const override { return 0; } // not used | 
| 163 |     void bind() override { } // not used | 
| 164 |  | 
| 165 |     bool isAtlasTexture() const override { return true; } | 
| 166 |     QRect atlasSubRect() const { return m_allocated_rect; } | 
| 167 |  | 
| 168 |     QRhiResourceUpdateBatch *workResourceUpdateBatch() const; | 
| 169 |  | 
| 170 | protected: | 
| 171 |     QRect m_allocated_rect; | 
| 172 |     AtlasBase *m_atlas; | 
| 173 | }; | 
| 174 |  | 
| 175 | class TextureBasePrivate : public QSGTexturePrivate | 
| 176 | { | 
| 177 |     Q_DECLARE_PUBLIC(TextureBase) | 
| 178 | public: | 
| 179 |     int comparisonKey() const override; | 
| 180 |     QRhiTexture *rhiTexture() const override; | 
| 181 |     void updateRhiTexture(QRhi *rhi, QRhiResourceUpdateBatch *resourceUpdates) override; | 
| 182 | }; | 
| 183 |  | 
| 184 | class Texture : public TextureBase | 
| 185 | { | 
| 186 |     Q_OBJECT | 
| 187 | public: | 
| 188 |     Texture(Atlas *atlas, const QRect &textureRect, const QImage &image); | 
| 189 |     ~Texture(); | 
| 190 |  | 
| 191 |     QSize textureSize() const override { return atlasSubRectWithoutPadding().size(); } | 
| 192 |     void setHasAlphaChannel(bool alpha) { m_has_alpha = alpha; } | 
| 193 |     bool hasAlphaChannel() const override { return m_has_alpha; } | 
| 194 |     bool hasMipmaps() const override { return false; } | 
| 195 |  | 
| 196 |     QRectF normalizedTextureSubRect() const override { return m_texture_coords_rect; } | 
| 197 |  | 
| 198 |     QRect atlasSubRect() const { return m_allocated_rect; } | 
| 199 |     QRect atlasSubRectWithoutPadding() const { return m_allocated_rect.adjusted(xp1: 1, yp1: 1, xp2: -1, yp2: -1); } | 
| 200 |  | 
| 201 |     QSGTexture *removedFromAtlas() const override; | 
| 202 |  | 
| 203 |     void releaseImage() { m_image = QImage(); } | 
| 204 |     const QImage &image() const { return m_image; } | 
| 205 |  | 
| 206 | private: | 
| 207 |     QRectF m_texture_coords_rect; | 
| 208 |     QImage m_image; | 
| 209 |     mutable QSGPlainTexture *m_nonatlas_texture = nullptr; | 
| 210 |     bool m_has_alpha; | 
| 211 | }; | 
| 212 |  | 
| 213 | } | 
| 214 |  | 
| 215 | QT_END_NAMESPACE | 
| 216 |  | 
| 217 | #endif | 
| 218 |  |