| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2018 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 | #include "qsgcompressedatlastexture_p.h" | 
| 41 |  | 
| 42 | #include <QtCore/QVarLengthArray> | 
| 43 | #include <QtCore/QElapsedTimer> | 
| 44 | #include <QtCore/QtMath> | 
| 45 |  | 
| 46 | #include <QtGui/QOpenGLContext> | 
| 47 | #include <QtGui/QGuiApplication> | 
| 48 | #include <QtGui/QScreen> | 
| 49 | #include <QtGui/QSurface> | 
| 50 | #include <QtGui/QWindow> | 
| 51 | #include <QtGui/QOpenGLFunctions> | 
| 52 | #include <QtGui/QOpenGLTexture> | 
| 53 | #include <QDebug> | 
| 54 |  | 
| 55 | #include <private/qqmlglobal_p.h> | 
| 56 | #include <private/qquickprofiler_p.h> | 
| 57 | #include <private/qsgtexture_p.h> | 
| 58 | #include <private/qsgcompressedtexture_p.h> | 
| 59 |  | 
| 60 | QT_BEGIN_NAMESPACE | 
| 61 |  | 
| 62 | static QElapsedTimer qsg_renderer_timer; | 
| 63 |  | 
| 64 | namespace QSGCompressedAtlasTexture | 
| 65 | { | 
| 66 |  | 
| 67 | Atlas::Atlas(const QSize &size, uint format) | 
| 68 |     : QSGOpenGLAtlasTexture::AtlasBase(size) | 
| 69 |     , m_format(format) | 
| 70 | { | 
| 71 | } | 
| 72 |  | 
| 73 | Atlas::~Atlas() | 
| 74 | { | 
| 75 | } | 
| 76 |  | 
| 77 | Texture *Atlas::create(const QByteArray &data, int dataLength, int dataOffset, const QSize &size, const QSize &paddedSize) | 
| 78 | { | 
| 79 |     // No need to lock, as manager already locked it. | 
| 80 |     QRect rect = m_allocator.allocate(size: paddedSize); | 
| 81 |     if (rect.width() > 0 && rect.height() > 0) { | 
| 82 |         Texture *t = new Texture(this, rect, data, dataLength, dataOffset, size); | 
| 83 |         m_pending_uploads << t; | 
| 84 |         return t; | 
| 85 |     } | 
| 86 |     return nullptr; | 
| 87 | } | 
| 88 |  | 
| 89 | void Atlas::generateTexture() | 
| 90 | { | 
| 91 |     int bytesPerBlock = 8; | 
| 92 |     switch (m_format) { | 
| 93 |     case QOpenGLTexture::RGBA8_ETC2_EAC: | 
| 94 |     case QOpenGLTexture::RGBA_DXT3: | 
| 95 |     case QOpenGLTexture::RGBA_DXT5: | 
| 96 |         bytesPerBlock = 16; | 
| 97 |     default: | 
| 98 |         break; | 
| 99 |     } | 
| 100 |  | 
| 101 |     QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions(); | 
| 102 |     funcs->glCompressedTexImage2D(GL_TEXTURE_2D, level: 0, internalformat: m_format, | 
| 103 |                                   width: m_size.width(), height: m_size.height(), border: 0, | 
| 104 |                                   imageSize: (m_size.width() / 4 * m_size.height() / 4) * bytesPerBlock, | 
| 105 |                                   data: nullptr); | 
| 106 | } | 
| 107 |  | 
| 108 | void Atlas::uploadPendingTexture(int i) | 
| 109 | { | 
| 110 |     Texture *texture = static_cast<Texture*>(m_pending_uploads.at(i)); | 
| 111 |  | 
| 112 |     const QRect &r = texture->atlasSubRect(); | 
| 113 |  | 
| 114 |     QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions(); | 
| 115 |     funcs->glCompressedTexSubImage2D(GL_TEXTURE_2D, level: 0, | 
| 116 |                                      xoffset: r.x(), yoffset: r.y(), width: r.width(), height: r.height(), format: m_format, | 
| 117 |                                      imageSize: texture->sizeInBytes(), | 
| 118 |                                      data: texture->data().constData() + texture->dataOffset()); | 
| 119 |  | 
| 120 |     qCDebug(QSG_LOG_TIME_TEXTURE).nospace() << "compressed atlastexture uploaded in: "  << qsg_renderer_timer.elapsed() | 
| 121 |                                        << "ms ("  << texture->textureSize().width() << "x"  | 
| 122 |                                        << texture->textureSize().height() << ")" ; | 
| 123 |  | 
| 124 |     // TODO: consider releasing the data (as is done in the regular atlas)? | 
| 125 |     // The advantage of keeping this data around is that it makes it much easier | 
| 126 |     // to remove the texture from the atlas | 
| 127 | } | 
| 128 |  | 
| 129 | Texture::Texture(Atlas *atlas, const QRect &textureRect, const QByteArray &data, int dataLength, int dataOffset, const QSize &size) | 
| 130 |     : QSGOpenGLAtlasTexture::TextureBase(atlas, textureRect) | 
| 131 |     , m_nonatlas_texture(nullptr) | 
| 132 |     , m_data(data) | 
| 133 |     , m_size(size) | 
| 134 |     , m_dataLength(dataLength) | 
| 135 |     , m_dataOffset(dataOffset) | 
| 136 | { | 
| 137 |     float w = atlas->size().width(); | 
| 138 |     float h = atlas->size().height(); | 
| 139 |     const QRect &r = atlasSubRect(); | 
| 140 |     // offset by half-pixel to prevent bleeding when scaling | 
| 141 |     m_texture_coords_rect = QRectF((r.x() + .5) / w, | 
| 142 |                                    (r.y() + .5) / h, | 
| 143 |                                    (size.width() - 1.) / w, | 
| 144 |                                    (size.height() - 1.) / h); | 
| 145 | } | 
| 146 |  | 
| 147 | Texture::~Texture() | 
| 148 | { | 
| 149 |     delete m_nonatlas_texture; | 
| 150 | } | 
| 151 |  | 
| 152 | bool Texture::hasAlphaChannel() const | 
| 153 | { | 
| 154 |     return !QSGCompressedTexture::formatIsOpaque(glTextureFormat: static_cast<Atlas*>(m_atlas)->format()); | 
| 155 | } | 
| 156 |  | 
| 157 | QSGTexture *Texture::removedFromAtlas() const | 
| 158 | { | 
| 159 |     if (m_nonatlas_texture) { | 
| 160 |         m_nonatlas_texture->setMipmapFiltering(mipmapFiltering()); | 
| 161 |         m_nonatlas_texture->setFiltering(filtering()); | 
| 162 |         return m_nonatlas_texture; | 
| 163 |     } | 
| 164 |  | 
| 165 |     if (!m_data.isEmpty()) { | 
| 166 |         QTextureFileData texData; | 
| 167 |         texData.setData(m_data); | 
| 168 |         texData.setSize(m_size); | 
| 169 |         texData.setGLInternalFormat(static_cast<Atlas*>(m_atlas)->format()); | 
| 170 |         texData.setDataLength(length: m_dataLength); | 
| 171 |         texData.setDataOffset(offset: m_dataOffset); | 
| 172 |         m_nonatlas_texture = new QSGCompressedTexture(texData); | 
| 173 |         m_nonatlas_texture->setMipmapFiltering(mipmapFiltering()); | 
| 174 |         m_nonatlas_texture->setFiltering(filtering()); | 
| 175 |     } | 
| 176 |  | 
| 177 |     return m_nonatlas_texture; | 
| 178 | } | 
| 179 |  | 
| 180 | } | 
| 181 |  | 
| 182 | QT_END_NAMESPACE | 
| 183 |  |