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 QSGRHIATLASTEXTURE_P_H
5#define QSGRHIATLASTEXTURE_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 <QtCore/QSize>
19#include <QtQuick/private/qsgplaintexture_p.h>
20#include <QtQuick/private/qsgareaallocator_p.h>
21#include <QtGui/QSurface>
22#include <rhi/qrhi.h>
23
24QT_BEGIN_NAMESPACE
25
26class QSGDefaultRenderContext;
27
28namespace QSGCompressedAtlasTexture {
29 class Atlas;
30}
31class QSGCompressedTextureFactory;
32
33namespace QSGRhiAtlasTexture
34{
35
36class Texture;
37class TextureBase;
38class Atlas;
39
40class Manager : public QObject
41{
42 Q_OBJECT
43
44public:
45 Manager(QSGDefaultRenderContext *rc, const QSize &surfacePixelSize, QSurface *maybeSurface);
46 ~Manager();
47
48 QSGTexture *create(const QImage &image, bool hasAlphaChannel);
49 QSGTexture *create(const QSGCompressedTextureFactory *factory);
50 void invalidate();
51
52private:
53 QSGDefaultRenderContext *m_rc;
54 QRhi *m_rhi;
55 Atlas *m_atlas = nullptr;
56 // set of atlases for different compressed formats
57 QHash<unsigned int, QSGCompressedAtlasTexture::Atlas*> m_atlases;
58
59 QSize m_atlas_size;
60 int m_atlas_size_limit;
61};
62
63class AtlasBase : public QObject
64{
65 Q_OBJECT
66public:
67 AtlasBase(QSGDefaultRenderContext *rc, const QSize &size);
68 ~AtlasBase();
69
70 void invalidate();
71 void commitTextureOperations(QRhiResourceUpdateBatch *resourceUpdates);
72 void remove(TextureBase *t);
73
74 QSGDefaultRenderContext *renderContext() const { return m_rc; }
75 QRhi *rhi() const { return m_rhi; }
76 QRhiTexture *texture() const { return m_texture; }
77 QSize size() const { return m_size; }
78
79protected:
80 virtual bool generateTexture() = 0;
81 virtual void enqueueTextureUpload(TextureBase *t, QRhiResourceUpdateBatch *resourceUpdates) = 0;
82
83protected:
84 QSGDefaultRenderContext *m_rc;
85 QRhi *m_rhi;
86 QSGAreaAllocator m_allocator;
87 QRhiTexture *m_texture = nullptr;
88 QSize m_size;
89 QVector<TextureBase *> m_pending_uploads;
90 friend class TextureBase;
91 friend class TextureBasePrivate;
92
93private:
94 bool m_allocated = false;
95};
96
97class Atlas : public AtlasBase
98{
99public:
100 Atlas(QSGDefaultRenderContext *rc, const QSize &size);
101 ~Atlas();
102
103 bool generateTexture() override;
104 void enqueueTextureUpload(TextureBase *t, QRhiResourceUpdateBatch *resourceUpdates) override;
105
106 Texture *create(const QImage &image);
107
108 QRhiTexture::Format format() const { return m_format; }
109
110private:
111 QRhiTexture::Format m_format;
112 int m_atlas_transient_image_threshold = 0;
113
114 uint m_debug_overlay : 1;
115};
116
117class TextureBase : public QSGTexture
118{
119 Q_OBJECT
120public:
121 TextureBase(AtlasBase *atlas, const QRect &textureRect);
122 ~TextureBase();
123
124 qint64 comparisonKey() const override;
125 QRhiTexture *rhiTexture() const override;
126 void commitTextureOperations(QRhi *rhi, QRhiResourceUpdateBatch *resourceUpdates) override;
127
128 bool isAtlasTexture() const override { return true; }
129 QRect atlasSubRect() const { return m_allocated_rect; }
130
131protected:
132 QRect m_allocated_rect;
133 AtlasBase *m_atlas;
134};
135
136class Texture : public TextureBase
137{
138 Q_OBJECT
139public:
140 Texture(Atlas *atlas, const QRect &textureRect, const QImage &image);
141 ~Texture();
142
143 QSize textureSize() const override { return atlasSubRectWithoutPadding().size(); }
144 void setHasAlphaChannel(bool alpha) { m_has_alpha = alpha; }
145 bool hasAlphaChannel() const override { return m_has_alpha; }
146 bool hasMipmaps() const override { return false; }
147
148 QRectF normalizedTextureSubRect() const override { return m_texture_coords_rect; }
149
150 QRect atlasSubRect() const { return m_allocated_rect; }
151 QRect atlasSubRectWithoutPadding() const { return m_allocated_rect.adjusted(xp1: 1, yp1: 1, xp2: -1, yp2: -1); }
152
153 QSGTexture *removedFromAtlas(QRhiResourceUpdateBatch *resourceUpdates) const override;
154
155 void releaseImage() { m_image = QImage(); }
156 const QImage &image() const { return m_image; }
157
158private:
159 QRectF m_texture_coords_rect;
160 QImage m_image;
161 mutable QSGPlainTexture *m_nonatlas_texture = nullptr;
162 bool m_has_alpha;
163};
164
165}
166
167QT_END_NAMESPACE
168
169#endif
170

source code of qtdeclarative/src/quick/scenegraph/util/qsgrhiatlastexture_p.h