1// Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qtextureatlas_p.h"
5#include "qtextureatlas_p_p.h"
6#include <Qt3DRender/qtexturedata.h>
7#include <Qt3DRender/qabstracttextureimage.h>
8
9QT_BEGIN_NAMESPACE
10
11using namespace Qt3DCore;
12
13namespace Qt3DExtras {
14
15QTextureAtlasData::QTextureAtlasData(int w, int h, QImage::Format fmt)
16 : m_image(w, h, fmt)
17{
18 m_image.fill(pixel: 0);
19}
20
21QTextureAtlasData::~QTextureAtlasData()
22{
23}
24
25void QTextureAtlasData::addImage(const AtlasTexture &texture, const QImage &image)
26{
27 QMutexLocker lock(&m_mutex);
28
29 Update update;
30 update.textureInfo = texture;
31 update.image = image;
32 m_updates << update;
33}
34
35QByteArray QTextureAtlasData::createUpdatedImageData()
36{
37 m_mutex.lock();
38 const QList<Update> updates = std::move(m_updates);
39 m_mutex.unlock();
40
41 // copy sub-images into the actual texture image
42 for (const Update &update : updates) {
43 const QImage &image = update.image;
44
45 const int padding = update.textureInfo.padding;
46 const QRect imgRect = update.textureInfo.position;
47 const QRect alloc = imgRect.adjusted(xp1: -padding, yp1: -padding, xp2: padding, yp2: padding);
48
49 // bytes per pixel
50 if (image.depth() != m_image.depth()) {
51 qWarning() << "[QTextureAtlas] Image depth does not match. Original =" << m_image.depth() << ", Sub-Image =" << image.depth();
52 continue;
53 }
54 int bpp = image.depth() / 8;
55
56 // copy image contents into texture image
57 // use image border pixels to fill the padding region
58 for (int y = alloc.top(); y <= alloc.bottom(); y++) {
59 uchar *dstLine = m_image.scanLine(y);
60
61 uchar *dstPadL = &dstLine[bpp * alloc.left()];
62 uchar *dstPadR = &dstLine[bpp * imgRect.right()];
63 uchar *dstImg = &dstLine[bpp * imgRect.left()];
64
65 // do padding with 0 in the upper/lower padding parts around the actual image
66 if (y < imgRect.top() || y > imgRect.bottom()) {
67 memset(s: dstPadL, c: 0, n: bpp * (imgRect.width() + 2 * padding));
68 continue;
69 }
70
71 // copy left and right padding pixels
72 memset(s: dstPadL, c: 0, n: bpp * padding);
73 memset(s: dstPadR, c: 0, n: bpp * padding);
74
75 // copy image scanline
76 const int ySrc = qBound(min: 0, val: y - imgRect.top(), max: image.height()-1);
77 memcpy(dest: dstImg, src: image.scanLine(ySrc), n: bpp * imgRect.width());
78 }
79 }
80
81 return QByteArray(reinterpret_cast<const char*>(m_image.constBits()), m_image.sizeInBytes());
82}
83
84QTextureAtlasPrivate::QTextureAtlasPrivate()
85 : Qt3DRender::QAbstractTexturePrivate()
86{
87 m_target = Qt3DRender::QAbstractTexture::TargetAutomatic;
88 m_format = Qt3DRender::QAbstractTexture::RGBA8_UNorm;
89 m_width = 256;
90 m_height = 256;
91 m_depth = 1;
92}
93
94QTextureAtlasPrivate::~QTextureAtlasPrivate()
95{
96}
97
98QTextureAtlasGenerator::QTextureAtlasGenerator(const QTextureAtlasPrivate *texAtlas)
99 : m_data(texAtlas->m_data)
100 , m_format(texAtlas->m_format)
101 , m_pixelFormat(texAtlas->m_pixelFormat)
102 , m_generation(texAtlas->m_currGen)
103 , m_atlasId(texAtlas->m_id)
104{
105}
106
107QTextureAtlasGenerator::~QTextureAtlasGenerator()
108{
109}
110
111Qt3DRender::QTextureDataPtr QTextureAtlasGenerator::operator()()
112{
113 Qt3DRender::QTextureImageDataPtr texImage = Qt3DRender::QTextureImageDataPtr::create();
114 texImage->setTarget(QOpenGLTexture::Target2D);
115 texImage->setWidth(m_data->width());
116 texImage->setHeight(m_data->height());
117 texImage->setDepth(1);
118 texImage->setFaces(1);
119 texImage->setLayers(1);
120 texImage->setMipLevels(1);
121 texImage->setFormat(static_cast<QOpenGLTexture::TextureFormat>(m_format));
122 texImage->setPixelFormat(m_pixelFormat);
123 texImage->setPixelType(QOpenGLTexture::UInt8);
124
125 const QByteArray bytes = m_data->createUpdatedImageData();
126 texImage->setData(data: bytes, blockSize: 1);
127
128 Qt3DRender::QTextureDataPtr generatedData = Qt3DRender::QTextureDataPtr::create();
129 generatedData->setTarget(Qt3DRender::QAbstractTexture::Target2D);
130 generatedData->setFormat(m_format);
131 generatedData->setWidth(m_data->width());
132 generatedData->setHeight(m_data->height());
133 generatedData->setDepth(1);
134 generatedData->setLayers(1);
135 generatedData->addImageData(imageData: texImage);
136
137 return generatedData;
138}
139
140bool QTextureAtlasGenerator::operator==(const QTextureGenerator &other) const
141{
142 const QTextureAtlasGenerator *otherFunctor = Qt3DCore::functor_cast<QTextureAtlasGenerator>(other: &other);
143 return (otherFunctor != nullptr
144 && otherFunctor->m_data == m_data
145 && otherFunctor->m_atlasId == m_atlasId
146 && otherFunctor->m_generation == m_generation);
147}
148
149QTextureAtlas::QTextureAtlas(Qt3DCore::QNode *parent)
150 : QAbstractTexture(*new QTextureAtlasPrivate(), parent)
151{
152}
153
154QOpenGLTexture::PixelFormat QTextureAtlas::pixelFormat() const
155{
156 Q_D(const QTextureAtlas);
157 return d->m_pixelFormat;
158}
159
160void QTextureAtlas::setPixelFormat(QOpenGLTexture::PixelFormat fmt)
161{
162 Q_D(QTextureAtlas);
163 d->m_pixelFormat = fmt;
164}
165
166QTextureAtlas::~QTextureAtlas()
167{
168}
169
170QTextureAtlas::TextureId QTextureAtlas::addImage(const QImage &image, int padding)
171{
172 Q_D(QTextureAtlas);
173
174 // lazily create image and allocator to allow setWidth/setHeight after object construction
175 if (!d->m_allocator) {
176 Q_ASSERT(d->m_data.isNull());
177
178 d->m_allocator.reset(other: new AreaAllocator(QSize(width(), height())));
179 d->m_data = QTextureAtlasDataPtr::create(arguments: width(), arguments: height(), arguments: image.format());
180 }
181
182 const QSize allocSz = image.size() + QSize(2 * padding, 2 * padding);
183
184 // try to allocate space within image space
185 const QRect alloc = d->m_allocator->allocate(size: allocSz);
186 if (alloc.isEmpty())
187 return InvalidTexture;
188
189 const QRect imgRect = alloc.adjusted(xp1: padding, yp1: padding, xp2: -padding, yp2: -padding);
190 AtlasTexture tex;
191 tex.position = imgRect;
192 tex.padding = padding;
193
194 // store texture
195 TextureId id = d->m_currId++;
196 d->m_textures[id] = tex;
197 d->m_data->addImage(texture: tex, image);
198
199 // update data functor
200 d->m_currGen++;
201 d->setDataFunctor(QTextureAtlasGeneratorPtr::create(arguments: d));
202
203 return id;
204}
205
206void QTextureAtlas::removeImage(TextureId id)
207{
208 Q_D(QTextureAtlas);
209 auto it = d->m_textures.find(key: id);
210 if (it != d->m_textures.end()) {
211 QRect imgRect = it->position;
212 imgRect.adjust(dx1: -it->padding, dy1: -it->padding, dx2: 2*it->padding, dy2: 2*it->padding);
213
214 if (d->m_allocator)
215 d->m_allocator->deallocate(rect: imgRect);
216 d->m_textures.erase(it);
217 }
218}
219
220bool QTextureAtlas::hasImage(TextureId id) const
221{
222 Q_D(const QTextureAtlas);
223 return d->m_textures.contains(key: id);
224}
225
226qsizetype QTextureAtlas::imageCount() const
227{
228 Q_D(const QTextureAtlas);
229 return d->m_textures.size();
230}
231
232QRect QTextureAtlas::imagePosition(TextureId id) const
233{
234 Q_D(const QTextureAtlas);
235 const auto it = d->m_textures.find(key: id);
236 return (it != d->m_textures.cend()) ? it->position : QRect();
237}
238
239QRectF QTextureAtlas::imageTexCoords(TextureId id) const
240{
241 Q_D(const QTextureAtlas);
242 const auto it = d->m_textures.find(key: id);
243 if (it != d->m_textures.cend()) {
244 const float w = d->m_data->width();
245 const float h = d->m_data->height();
246 return QRectF(static_cast<float>(it->position.x()) / w,
247 static_cast<float>(it->position.y()) / h,
248 static_cast<float>(it->position.width()) / w,
249 static_cast<float>(it->position.height()) / h);
250 }
251 return QRectF();
252}
253
254int QTextureAtlas::imagePadding(TextureId id) const
255{
256 Q_D(const QTextureAtlas);
257 const auto it = d->m_textures.find(key: id);
258 return (it != d->m_textures.cend()) ? it->padding : -1;
259}
260
261} // namespace Qt3DExtras
262
263QT_END_NAMESPACE
264
265#include "moc_qtextureatlas_p.cpp"
266

source code of qt3d/src/extras/text/qtextureatlas.cpp