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