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 QSGOPENGLATLASTEXTURE_P_H |
41 | #define QSGOPENGLATLASTEXTURE_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 | |
56 | #include <QtGui/qopengl.h> |
57 | |
58 | #include <QtQuick/QSGTexture> |
59 | #include <QtQuick/private/qsgplaintexture_p.h> |
60 | #include <QtQuick/private/qsgareaallocator_p.h> |
61 | |
62 | QT_BEGIN_NAMESPACE |
63 | |
64 | namespace QSGCompressedAtlasTexture { |
65 | class Atlas; |
66 | } |
67 | class QSGCompressedTextureFactory; |
68 | |
69 | namespace QSGOpenGLAtlasTexture |
70 | { |
71 | |
72 | class Texture; |
73 | class TextureBase; |
74 | class Atlas; |
75 | |
76 | class Manager : public QObject |
77 | { |
78 | Q_OBJECT |
79 | |
80 | public: |
81 | Manager(const QSize &surfacePixelSize); |
82 | ~Manager(); |
83 | |
84 | QSGTexture *create(const QImage &image, bool hasAlphaChannel); |
85 | QSGTexture *create(const QSGCompressedTextureFactory *factory); |
86 | void invalidate(); |
87 | |
88 | private: |
89 | Atlas *m_atlas; |
90 | // set of atlases for different compressed formats |
91 | QHash<unsigned int, QSGCompressedAtlasTexture::Atlas*> m_atlases; |
92 | |
93 | QSize m_atlas_size; |
94 | int m_atlas_size_limit; |
95 | }; |
96 | |
97 | class AtlasBase : public QObject |
98 | { |
99 | Q_OBJECT |
100 | public: |
101 | AtlasBase(const QSize &size); |
102 | ~AtlasBase(); |
103 | |
104 | void invalidate(); |
105 | |
106 | int textureId() const; |
107 | void bind(QSGTexture::Filtering filtering); |
108 | |
109 | void remove(TextureBase *t); |
110 | |
111 | QSize size() const { return m_size; } |
112 | |
113 | protected: |
114 | virtual void generateTexture() = 0; |
115 | virtual void uploadPendingTexture(int i) = 0; |
116 | |
117 | protected: |
118 | QSGAreaAllocator m_allocator; |
119 | unsigned int m_texture_id; |
120 | QSize m_size; |
121 | QList<TextureBase *> m_pending_uploads; |
122 | |
123 | private: |
124 | bool m_allocated; |
125 | }; |
126 | |
127 | class Atlas : public AtlasBase |
128 | { |
129 | public: |
130 | Atlas(const QSize &size); |
131 | ~Atlas(); |
132 | |
133 | void generateTexture() override; |
134 | void uploadPendingTexture(int i) override; |
135 | |
136 | void upload(Texture *texture); |
137 | void uploadBgra(Texture *texture); |
138 | |
139 | Texture *create(const QImage &image); |
140 | |
141 | uint internalFormat() const { return m_internalFormat; } |
142 | uint externalFormat() const { return m_externalFormat; } |
143 | |
144 | private: |
145 | uint m_internalFormat; |
146 | uint m_externalFormat; |
147 | |
148 | int m_atlas_transient_image_threshold; |
149 | |
150 | uint m_use_bgra_fallback: 1; |
151 | uint m_debug_overlay : 1; |
152 | }; |
153 | |
154 | class TextureBase : public QSGTexture |
155 | { |
156 | Q_OBJECT |
157 | public: |
158 | TextureBase(AtlasBase *atlas, const QRect &textureRect); |
159 | ~TextureBase(); |
160 | |
161 | int textureId() const override { return m_atlas->textureId(); } |
162 | bool isAtlasTexture() const override { return true; } |
163 | |
164 | QRect atlasSubRect() const { return m_allocated_rect; } |
165 | |
166 | void bind() override; |
167 | |
168 | protected: |
169 | QRect m_allocated_rect; |
170 | AtlasBase *m_atlas; |
171 | }; |
172 | |
173 | class Texture : public TextureBase |
174 | { |
175 | Q_OBJECT |
176 | public: |
177 | Texture(Atlas *atlas, const QRect &textureRect, const QImage &image); |
178 | ~Texture(); |
179 | |
180 | QSize textureSize() const override { return atlasSubRectWithoutPadding().size(); } |
181 | void setHasAlphaChannel(bool alpha) { m_has_alpha = alpha; } |
182 | bool hasAlphaChannel() const override { return m_has_alpha; } |
183 | bool hasMipmaps() const override { return false; } |
184 | |
185 | QRectF normalizedTextureSubRect() const override { return m_texture_coords_rect; } |
186 | |
187 | QRect atlasSubRect() const { return m_allocated_rect; } |
188 | QRect atlasSubRectWithoutPadding() const { return m_allocated_rect.adjusted(xp1: 1, yp1: 1, xp2: -1, yp2: -1); } |
189 | |
190 | QSGTexture *removedFromAtlas() const override; |
191 | |
192 | void releaseImage() { m_image = QImage(); } |
193 | const QImage &image() const { return m_image; } |
194 | |
195 | private: |
196 | QRectF m_texture_coords_rect; |
197 | QImage m_image; |
198 | mutable QSGPlainTexture *m_nonatlas_texture; |
199 | bool m_has_alpha; |
200 | }; |
201 | |
202 | } |
203 | |
204 | QT_END_NAMESPACE |
205 | |
206 | #endif |
207 | |